2011-01-18 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / resolve.c
blobf2e722320dda034d768f3ae8cd50aaefba5ff28c
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "flags.h"
26 #include "gfortran.h"
27 #include "obstack.h"
28 #include "bitmap.h"
29 #include "arith.h" /* For gfc_compare_expr(). */
30 #include "dependency.h"
31 #include "data.h"
32 #include "target-memory.h" /* for gfc_simplify_transfer */
33 #include "constructor.h"
35 /* Types used in equivalence statements. */
37 typedef enum seq_type
39 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
41 seq_type;
43 /* Stack to keep track of the nesting of blocks as we move through the
44 code. See resolve_branch() and resolve_code(). */
46 typedef struct code_stack
48 struct gfc_code *head, *current;
49 struct code_stack *prev;
51 /* This bitmap keeps track of the targets valid for a branch from
52 inside this block except for END {IF|SELECT}s of enclosing
53 blocks. */
54 bitmap reachable_labels;
56 code_stack;
58 static code_stack *cs_base = NULL;
61 /* Nonzero if we're inside a FORALL block. */
63 static int forall_flag;
65 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
67 static int omp_workshare_flag;
69 /* Nonzero if we are processing a formal arglist. The corresponding function
70 resets the flag each time that it is read. */
71 static int formal_arg_flag = 0;
73 /* True if we are resolving a specification expression. */
74 static int specification_expr = 0;
76 /* The id of the last entry seen. */
77 static int current_entry_id;
79 /* We use bitmaps to determine if a branch target is valid. */
80 static bitmap_obstack labels_obstack;
82 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
83 static bool inquiry_argument = false;
85 int
86 gfc_is_formal_arg (void)
88 return formal_arg_flag;
91 /* Is the symbol host associated? */
92 static bool
93 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
95 for (ns = ns->parent; ns; ns = ns->parent)
97 if (sym->ns == ns)
98 return true;
101 return false;
104 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
105 an ABSTRACT derived-type. If where is not NULL, an error message with that
106 locus is printed, optionally using name. */
108 static gfc_try
109 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
111 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
113 if (where)
115 if (name)
116 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
117 name, where, ts->u.derived->name);
118 else
119 gfc_error ("ABSTRACT type '%s' used at %L",
120 ts->u.derived->name, where);
123 return FAILURE;
126 return SUCCESS;
130 static void resolve_symbol (gfc_symbol *sym);
131 static gfc_try resolve_intrinsic (gfc_symbol *sym, locus *loc);
134 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
136 static gfc_try
137 resolve_procedure_interface (gfc_symbol *sym)
139 if (sym->ts.interface == sym)
141 gfc_error ("PROCEDURE '%s' at %L may not be used as its own interface",
142 sym->name, &sym->declared_at);
143 return FAILURE;
145 if (sym->ts.interface->attr.procedure)
147 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared "
148 "in a later PROCEDURE statement", sym->ts.interface->name,
149 sym->name, &sym->declared_at);
150 return FAILURE;
153 /* Get the attributes from the interface (now resolved). */
154 if (sym->ts.interface->attr.if_source || sym->ts.interface->attr.intrinsic)
156 gfc_symbol *ifc = sym->ts.interface;
157 resolve_symbol (ifc);
159 if (ifc->attr.intrinsic)
160 resolve_intrinsic (ifc, &ifc->declared_at);
162 if (ifc->result)
163 sym->ts = ifc->result->ts;
164 else
165 sym->ts = ifc->ts;
166 sym->ts.interface = ifc;
167 sym->attr.function = ifc->attr.function;
168 sym->attr.subroutine = ifc->attr.subroutine;
169 gfc_copy_formal_args (sym, ifc);
171 sym->attr.allocatable = ifc->attr.allocatable;
172 sym->attr.pointer = ifc->attr.pointer;
173 sym->attr.pure = ifc->attr.pure;
174 sym->attr.elemental = ifc->attr.elemental;
175 sym->attr.dimension = ifc->attr.dimension;
176 sym->attr.contiguous = ifc->attr.contiguous;
177 sym->attr.recursive = ifc->attr.recursive;
178 sym->attr.always_explicit = ifc->attr.always_explicit;
179 sym->attr.ext_attr |= ifc->attr.ext_attr;
180 sym->attr.is_bind_c = ifc->attr.is_bind_c;
181 /* Copy array spec. */
182 sym->as = gfc_copy_array_spec (ifc->as);
183 if (sym->as)
185 int i;
186 for (i = 0; i < sym->as->rank; i++)
188 gfc_expr_replace_symbols (sym->as->lower[i], sym);
189 gfc_expr_replace_symbols (sym->as->upper[i], sym);
192 /* Copy char length. */
193 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
195 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
196 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
197 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
198 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
199 return FAILURE;
202 else if (sym->ts.interface->name[0] != '\0')
204 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
205 sym->ts.interface->name, sym->name, &sym->declared_at);
206 return FAILURE;
209 return SUCCESS;
213 /* Resolve types of formal argument lists. These have to be done early so that
214 the formal argument lists of module procedures can be copied to the
215 containing module before the individual procedures are resolved
216 individually. We also resolve argument lists of procedures in interface
217 blocks because they are self-contained scoping units.
219 Since a dummy argument cannot be a non-dummy procedure, the only
220 resort left for untyped names are the IMPLICIT types. */
222 static void
223 resolve_formal_arglist (gfc_symbol *proc)
225 gfc_formal_arglist *f;
226 gfc_symbol *sym;
227 int i;
229 if (proc->result != NULL)
230 sym = proc->result;
231 else
232 sym = proc;
234 if (gfc_elemental (proc)
235 || sym->attr.pointer || sym->attr.allocatable
236 || (sym->as && sym->as->rank > 0))
238 proc->attr.always_explicit = 1;
239 sym->attr.always_explicit = 1;
242 formal_arg_flag = 1;
244 for (f = proc->formal; f; f = f->next)
246 sym = f->sym;
248 if (sym == NULL)
250 /* Alternate return placeholder. */
251 if (gfc_elemental (proc))
252 gfc_error ("Alternate return specifier in elemental subroutine "
253 "'%s' at %L is not allowed", proc->name,
254 &proc->declared_at);
255 if (proc->attr.function)
256 gfc_error ("Alternate return specifier in function "
257 "'%s' at %L is not allowed", proc->name,
258 &proc->declared_at);
259 continue;
261 else if (sym->attr.procedure && sym->ts.interface
262 && sym->attr.if_source != IFSRC_DECL)
263 resolve_procedure_interface (sym);
265 if (sym->attr.if_source != IFSRC_UNKNOWN)
266 resolve_formal_arglist (sym);
268 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
270 if (gfc_pure (proc) && !gfc_pure (sym))
272 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
273 "also be PURE", sym->name, &sym->declared_at);
274 continue;
277 if (proc->attr.implicit_pure && !gfc_pure(sym))
278 proc->attr.implicit_pure = 0;
280 if (gfc_elemental (proc))
282 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
283 "procedure", &sym->declared_at);
284 continue;
287 if (sym->attr.function
288 && sym->ts.type == BT_UNKNOWN
289 && sym->attr.intrinsic)
291 gfc_intrinsic_sym *isym;
292 isym = gfc_find_function (sym->name);
293 if (isym == NULL || !isym->specific)
295 gfc_error ("Unable to find a specific INTRINSIC procedure "
296 "for the reference '%s' at %L", sym->name,
297 &sym->declared_at);
299 sym->ts = isym->ts;
302 continue;
305 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
306 && (!sym->attr.function || sym->result == sym))
307 gfc_set_default_type (sym, 1, sym->ns);
309 gfc_resolve_array_spec (sym->as, 0);
311 /* We can't tell if an array with dimension (:) is assumed or deferred
312 shape until we know if it has the pointer or allocatable attributes.
314 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
315 && !(sym->attr.pointer || sym->attr.allocatable))
317 sym->as->type = AS_ASSUMED_SHAPE;
318 for (i = 0; i < sym->as->rank; i++)
319 sym->as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind,
320 NULL, 1);
323 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
324 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
325 || sym->attr.optional)
327 proc->attr.always_explicit = 1;
328 if (proc->result)
329 proc->result->attr.always_explicit = 1;
332 /* If the flavor is unknown at this point, it has to be a variable.
333 A procedure specification would have already set the type. */
335 if (sym->attr.flavor == FL_UNKNOWN)
336 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
338 if (gfc_pure (proc) && !sym->attr.pointer
339 && sym->attr.flavor != FL_PROCEDURE)
341 if (proc->attr.function && sym->attr.intent != INTENT_IN)
342 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
343 "INTENT(IN)", sym->name, proc->name,
344 &sym->declared_at);
346 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
347 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
348 "have its INTENT specified", sym->name, proc->name,
349 &sym->declared_at);
352 if (proc->attr.implicit_pure && !sym->attr.pointer
353 && sym->attr.flavor != FL_PROCEDURE)
355 if (proc->attr.function && sym->attr.intent != INTENT_IN)
356 proc->attr.implicit_pure = 0;
358 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
359 proc->attr.implicit_pure = 0;
362 if (gfc_elemental (proc))
364 /* F2008, C1289. */
365 if (sym->attr.codimension)
367 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
368 "procedure", sym->name, &sym->declared_at);
369 continue;
372 if (sym->as != NULL)
374 gfc_error ("Argument '%s' of elemental procedure at %L must "
375 "be scalar", sym->name, &sym->declared_at);
376 continue;
379 if (sym->attr.allocatable)
381 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
382 "have the ALLOCATABLE attribute", sym->name,
383 &sym->declared_at);
384 continue;
387 if (sym->attr.pointer)
389 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
390 "have the POINTER attribute", sym->name,
391 &sym->declared_at);
392 continue;
395 if (sym->attr.flavor == FL_PROCEDURE)
397 gfc_error ("Dummy procedure '%s' not allowed in elemental "
398 "procedure '%s' at %L", sym->name, proc->name,
399 &sym->declared_at);
400 continue;
403 if (sym->attr.intent == INTENT_UNKNOWN)
405 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
406 "have its INTENT specified", sym->name, proc->name,
407 &sym->declared_at);
408 continue;
412 /* Each dummy shall be specified to be scalar. */
413 if (proc->attr.proc == PROC_ST_FUNCTION)
415 if (sym->as != NULL)
417 gfc_error ("Argument '%s' of statement function at %L must "
418 "be scalar", sym->name, &sym->declared_at);
419 continue;
422 if (sym->ts.type == BT_CHARACTER)
424 gfc_charlen *cl = sym->ts.u.cl;
425 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
427 gfc_error ("Character-valued argument '%s' of statement "
428 "function at %L must have constant length",
429 sym->name, &sym->declared_at);
430 continue;
435 formal_arg_flag = 0;
439 /* Work function called when searching for symbols that have argument lists
440 associated with them. */
442 static void
443 find_arglists (gfc_symbol *sym)
445 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
446 return;
448 resolve_formal_arglist (sym);
452 /* Given a namespace, resolve all formal argument lists within the namespace.
455 static void
456 resolve_formal_arglists (gfc_namespace *ns)
458 if (ns == NULL)
459 return;
461 gfc_traverse_ns (ns, find_arglists);
465 static void
466 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
468 gfc_try t;
470 /* If this namespace is not a function or an entry master function,
471 ignore it. */
472 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
473 || sym->attr.entry_master)
474 return;
476 /* Try to find out of what the return type is. */
477 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
479 t = gfc_set_default_type (sym->result, 0, ns);
481 if (t == FAILURE && !sym->result->attr.untyped)
483 if (sym->result == sym)
484 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
485 sym->name, &sym->declared_at);
486 else if (!sym->result->attr.proc_pointer)
487 gfc_error ("Result '%s' of contained function '%s' at %L has "
488 "no IMPLICIT type", sym->result->name, sym->name,
489 &sym->result->declared_at);
490 sym->result->attr.untyped = 1;
494 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
495 type, lists the only ways a character length value of * can be used:
496 dummy arguments of procedures, named constants, and function results
497 in external functions. Internal function results and results of module
498 procedures are not on this list, ergo, not permitted. */
500 if (sym->result->ts.type == BT_CHARACTER)
502 gfc_charlen *cl = sym->result->ts.u.cl;
503 if (!cl || !cl->length)
505 /* See if this is a module-procedure and adapt error message
506 accordingly. */
507 bool module_proc;
508 gcc_assert (ns->parent && ns->parent->proc_name);
509 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
511 gfc_error ("Character-valued %s '%s' at %L must not be"
512 " assumed length",
513 module_proc ? _("module procedure")
514 : _("internal function"),
515 sym->name, &sym->declared_at);
521 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
522 introduce duplicates. */
524 static void
525 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
527 gfc_formal_arglist *f, *new_arglist;
528 gfc_symbol *new_sym;
530 for (; new_args != NULL; new_args = new_args->next)
532 new_sym = new_args->sym;
533 /* See if this arg is already in the formal argument list. */
534 for (f = proc->formal; f; f = f->next)
536 if (new_sym == f->sym)
537 break;
540 if (f)
541 continue;
543 /* Add a new argument. Argument order is not important. */
544 new_arglist = gfc_get_formal_arglist ();
545 new_arglist->sym = new_sym;
546 new_arglist->next = proc->formal;
547 proc->formal = new_arglist;
552 /* Flag the arguments that are not present in all entries. */
554 static void
555 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
557 gfc_formal_arglist *f, *head;
558 head = new_args;
560 for (f = proc->formal; f; f = f->next)
562 if (f->sym == NULL)
563 continue;
565 for (new_args = head; new_args; new_args = new_args->next)
567 if (new_args->sym == f->sym)
568 break;
571 if (new_args)
572 continue;
574 f->sym->attr.not_always_present = 1;
579 /* Resolve alternate entry points. If a symbol has multiple entry points we
580 create a new master symbol for the main routine, and turn the existing
581 symbol into an entry point. */
583 static void
584 resolve_entries (gfc_namespace *ns)
586 gfc_namespace *old_ns;
587 gfc_code *c;
588 gfc_symbol *proc;
589 gfc_entry_list *el;
590 char name[GFC_MAX_SYMBOL_LEN + 1];
591 static int master_count = 0;
593 if (ns->proc_name == NULL)
594 return;
596 /* No need to do anything if this procedure doesn't have alternate entry
597 points. */
598 if (!ns->entries)
599 return;
601 /* We may already have resolved alternate entry points. */
602 if (ns->proc_name->attr.entry_master)
603 return;
605 /* If this isn't a procedure something has gone horribly wrong. */
606 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
608 /* Remember the current namespace. */
609 old_ns = gfc_current_ns;
611 gfc_current_ns = ns;
613 /* Add the main entry point to the list of entry points. */
614 el = gfc_get_entry_list ();
615 el->sym = ns->proc_name;
616 el->id = 0;
617 el->next = ns->entries;
618 ns->entries = el;
619 ns->proc_name->attr.entry = 1;
621 /* If it is a module function, it needs to be in the right namespace
622 so that gfc_get_fake_result_decl can gather up the results. The
623 need for this arose in get_proc_name, where these beasts were
624 left in their own namespace, to keep prior references linked to
625 the entry declaration.*/
626 if (ns->proc_name->attr.function
627 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
628 el->sym->ns = ns;
630 /* Do the same for entries where the master is not a module
631 procedure. These are retained in the module namespace because
632 of the module procedure declaration. */
633 for (el = el->next; el; el = el->next)
634 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
635 && el->sym->attr.mod_proc)
636 el->sym->ns = ns;
637 el = ns->entries;
639 /* Add an entry statement for it. */
640 c = gfc_get_code ();
641 c->op = EXEC_ENTRY;
642 c->ext.entry = el;
643 c->next = ns->code;
644 ns->code = c;
646 /* Create a new symbol for the master function. */
647 /* Give the internal function a unique name (within this file).
648 Also include the function name so the user has some hope of figuring
649 out what is going on. */
650 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
651 master_count++, ns->proc_name->name);
652 gfc_get_ha_symbol (name, &proc);
653 gcc_assert (proc != NULL);
655 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
656 if (ns->proc_name->attr.subroutine)
657 gfc_add_subroutine (&proc->attr, proc->name, NULL);
658 else
660 gfc_symbol *sym;
661 gfc_typespec *ts, *fts;
662 gfc_array_spec *as, *fas;
663 gfc_add_function (&proc->attr, proc->name, NULL);
664 proc->result = proc;
665 fas = ns->entries->sym->as;
666 fas = fas ? fas : ns->entries->sym->result->as;
667 fts = &ns->entries->sym->result->ts;
668 if (fts->type == BT_UNKNOWN)
669 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
670 for (el = ns->entries->next; el; el = el->next)
672 ts = &el->sym->result->ts;
673 as = el->sym->as;
674 as = as ? as : el->sym->result->as;
675 if (ts->type == BT_UNKNOWN)
676 ts = gfc_get_default_type (el->sym->result->name, NULL);
678 if (! gfc_compare_types (ts, fts)
679 || (el->sym->result->attr.dimension
680 != ns->entries->sym->result->attr.dimension)
681 || (el->sym->result->attr.pointer
682 != ns->entries->sym->result->attr.pointer))
683 break;
684 else if (as && fas && ns->entries->sym->result != el->sym->result
685 && gfc_compare_array_spec (as, fas) == 0)
686 gfc_error ("Function %s at %L has entries with mismatched "
687 "array specifications", ns->entries->sym->name,
688 &ns->entries->sym->declared_at);
689 /* The characteristics need to match and thus both need to have
690 the same string length, i.e. both len=*, or both len=4.
691 Having both len=<variable> is also possible, but difficult to
692 check at compile time. */
693 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
694 && (((ts->u.cl->length && !fts->u.cl->length)
695 ||(!ts->u.cl->length && fts->u.cl->length))
696 || (ts->u.cl->length
697 && ts->u.cl->length->expr_type
698 != fts->u.cl->length->expr_type)
699 || (ts->u.cl->length
700 && ts->u.cl->length->expr_type == EXPR_CONSTANT
701 && mpz_cmp (ts->u.cl->length->value.integer,
702 fts->u.cl->length->value.integer) != 0)))
703 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
704 "entries returning variables of different "
705 "string lengths", ns->entries->sym->name,
706 &ns->entries->sym->declared_at);
709 if (el == NULL)
711 sym = ns->entries->sym->result;
712 /* All result types the same. */
713 proc->ts = *fts;
714 if (sym->attr.dimension)
715 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
716 if (sym->attr.pointer)
717 gfc_add_pointer (&proc->attr, NULL);
719 else
721 /* Otherwise the result will be passed through a union by
722 reference. */
723 proc->attr.mixed_entry_master = 1;
724 for (el = ns->entries; el; el = el->next)
726 sym = el->sym->result;
727 if (sym->attr.dimension)
729 if (el == ns->entries)
730 gfc_error ("FUNCTION result %s can't be an array in "
731 "FUNCTION %s at %L", sym->name,
732 ns->entries->sym->name, &sym->declared_at);
733 else
734 gfc_error ("ENTRY result %s can't be an array in "
735 "FUNCTION %s at %L", sym->name,
736 ns->entries->sym->name, &sym->declared_at);
738 else if (sym->attr.pointer)
740 if (el == ns->entries)
741 gfc_error ("FUNCTION result %s can't be a POINTER in "
742 "FUNCTION %s at %L", sym->name,
743 ns->entries->sym->name, &sym->declared_at);
744 else
745 gfc_error ("ENTRY result %s can't be a POINTER in "
746 "FUNCTION %s at %L", sym->name,
747 ns->entries->sym->name, &sym->declared_at);
749 else
751 ts = &sym->ts;
752 if (ts->type == BT_UNKNOWN)
753 ts = gfc_get_default_type (sym->name, NULL);
754 switch (ts->type)
756 case BT_INTEGER:
757 if (ts->kind == gfc_default_integer_kind)
758 sym = NULL;
759 break;
760 case BT_REAL:
761 if (ts->kind == gfc_default_real_kind
762 || ts->kind == gfc_default_double_kind)
763 sym = NULL;
764 break;
765 case BT_COMPLEX:
766 if (ts->kind == gfc_default_complex_kind)
767 sym = NULL;
768 break;
769 case BT_LOGICAL:
770 if (ts->kind == gfc_default_logical_kind)
771 sym = NULL;
772 break;
773 case BT_UNKNOWN:
774 /* We will issue error elsewhere. */
775 sym = NULL;
776 break;
777 default:
778 break;
780 if (sym)
782 if (el == ns->entries)
783 gfc_error ("FUNCTION result %s can't be of type %s "
784 "in FUNCTION %s at %L", sym->name,
785 gfc_typename (ts), ns->entries->sym->name,
786 &sym->declared_at);
787 else
788 gfc_error ("ENTRY result %s can't be of type %s "
789 "in FUNCTION %s at %L", sym->name,
790 gfc_typename (ts), ns->entries->sym->name,
791 &sym->declared_at);
797 proc->attr.access = ACCESS_PRIVATE;
798 proc->attr.entry_master = 1;
800 /* Merge all the entry point arguments. */
801 for (el = ns->entries; el; el = el->next)
802 merge_argument_lists (proc, el->sym->formal);
804 /* Check the master formal arguments for any that are not
805 present in all entry points. */
806 for (el = ns->entries; el; el = el->next)
807 check_argument_lists (proc, el->sym->formal);
809 /* Use the master function for the function body. */
810 ns->proc_name = proc;
812 /* Finalize the new symbols. */
813 gfc_commit_symbols ();
815 /* Restore the original namespace. */
816 gfc_current_ns = old_ns;
820 /* Resolve common variables. */
821 static void
822 resolve_common_vars (gfc_symbol *sym, bool named_common)
824 gfc_symbol *csym = sym;
826 for (; csym; csym = csym->common_next)
828 if (csym->value || csym->attr.data)
830 if (!csym->ns->is_block_data)
831 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
832 "but only in BLOCK DATA initialization is "
833 "allowed", csym->name, &csym->declared_at);
834 else if (!named_common)
835 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
836 "in a blank COMMON but initialization is only "
837 "allowed in named common blocks", csym->name,
838 &csym->declared_at);
841 if (csym->ts.type != BT_DERIVED)
842 continue;
844 if (!(csym->ts.u.derived->attr.sequence
845 || csym->ts.u.derived->attr.is_bind_c))
846 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
847 "has neither the SEQUENCE nor the BIND(C) "
848 "attribute", csym->name, &csym->declared_at);
849 if (csym->ts.u.derived->attr.alloc_comp)
850 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
851 "has an ultimate component that is "
852 "allocatable", csym->name, &csym->declared_at);
853 if (gfc_has_default_initializer (csym->ts.u.derived))
854 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
855 "may not have default initializer", csym->name,
856 &csym->declared_at);
858 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
859 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
863 /* Resolve common blocks. */
864 static void
865 resolve_common_blocks (gfc_symtree *common_root)
867 gfc_symbol *sym;
869 if (common_root == NULL)
870 return;
872 if (common_root->left)
873 resolve_common_blocks (common_root->left);
874 if (common_root->right)
875 resolve_common_blocks (common_root->right);
877 resolve_common_vars (common_root->n.common->head, true);
879 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
880 if (sym == NULL)
881 return;
883 if (sym->attr.flavor == FL_PARAMETER)
884 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
885 sym->name, &common_root->n.common->where, &sym->declared_at);
887 if (sym->attr.intrinsic)
888 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
889 sym->name, &common_root->n.common->where);
890 else if (sym->attr.result
891 || gfc_is_function_return_value (sym, gfc_current_ns))
892 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
893 "that is also a function result", sym->name,
894 &common_root->n.common->where);
895 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
896 && sym->attr.proc != PROC_ST_FUNCTION)
897 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
898 "that is also a global procedure", sym->name,
899 &common_root->n.common->where);
903 /* Resolve contained function types. Because contained functions can call one
904 another, they have to be worked out before any of the contained procedures
905 can be resolved.
907 The good news is that if a function doesn't already have a type, the only
908 way it can get one is through an IMPLICIT type or a RESULT variable, because
909 by definition contained functions are contained namespace they're contained
910 in, not in a sibling or parent namespace. */
912 static void
913 resolve_contained_functions (gfc_namespace *ns)
915 gfc_namespace *child;
916 gfc_entry_list *el;
918 resolve_formal_arglists (ns);
920 for (child = ns->contained; child; child = child->sibling)
922 /* Resolve alternate entry points first. */
923 resolve_entries (child);
925 /* Then check function return types. */
926 resolve_contained_fntype (child->proc_name, child);
927 for (el = child->entries; el; el = el->next)
928 resolve_contained_fntype (el->sym, child);
933 /* Resolve all of the elements of a structure constructor and make sure that
934 the types are correct. The 'init' flag indicates that the given
935 constructor is an initializer. */
937 static gfc_try
938 resolve_structure_cons (gfc_expr *expr, int init)
940 gfc_constructor *cons;
941 gfc_component *comp;
942 gfc_try t;
943 symbol_attribute a;
945 t = SUCCESS;
947 if (expr->ts.type == BT_DERIVED)
948 resolve_symbol (expr->ts.u.derived);
950 cons = gfc_constructor_first (expr->value.constructor);
951 /* A constructor may have references if it is the result of substituting a
952 parameter variable. In this case we just pull out the component we
953 want. */
954 if (expr->ref)
955 comp = expr->ref->u.c.sym->components;
956 else
957 comp = expr->ts.u.derived->components;
959 /* See if the user is trying to invoke a structure constructor for one of
960 the iso_c_binding derived types. */
961 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
962 && expr->ts.u.derived->ts.is_iso_c && cons
963 && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
965 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
966 expr->ts.u.derived->name, &(expr->where));
967 return FAILURE;
970 /* Return if structure constructor is c_null_(fun)prt. */
971 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
972 && expr->ts.u.derived->ts.is_iso_c && cons
973 && cons->expr && cons->expr->expr_type == EXPR_NULL)
974 return SUCCESS;
976 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
978 int rank;
980 if (!cons->expr)
981 continue;
983 if (gfc_resolve_expr (cons->expr) == FAILURE)
985 t = FAILURE;
986 continue;
989 rank = comp->as ? comp->as->rank : 0;
990 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
991 && (comp->attr.allocatable || cons->expr->rank))
993 gfc_error ("The rank of the element in the derived type "
994 "constructor at %L does not match that of the "
995 "component (%d/%d)", &cons->expr->where,
996 cons->expr->rank, rank);
997 t = FAILURE;
1000 /* If we don't have the right type, try to convert it. */
1002 if (!comp->attr.proc_pointer &&
1003 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1005 t = FAILURE;
1006 if (strcmp (comp->name, "_extends") == 0)
1008 /* Can afford to be brutal with the _extends initializer.
1009 The derived type can get lost because it is PRIVATE
1010 but it is not usage constrained by the standard. */
1011 cons->expr->ts = comp->ts;
1012 t = SUCCESS;
1014 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1015 gfc_error ("The element in the derived type constructor at %L, "
1016 "for pointer component '%s', is %s but should be %s",
1017 &cons->expr->where, comp->name,
1018 gfc_basic_typename (cons->expr->ts.type),
1019 gfc_basic_typename (comp->ts.type));
1020 else
1021 t = gfc_convert_type (cons->expr, &comp->ts, 1);
1024 /* For strings, the length of the constructor should be the same as
1025 the one of the structure, ensure this if the lengths are known at
1026 compile time and when we are dealing with PARAMETER or structure
1027 constructors. */
1028 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1029 && comp->ts.u.cl->length
1030 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1031 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1032 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1033 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1034 comp->ts.u.cl->length->value.integer) != 0)
1036 if (cons->expr->expr_type == EXPR_VARIABLE
1037 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1039 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1040 to make use of the gfc_resolve_character_array_constructor
1041 machinery. The expression is later simplified away to
1042 an array of string literals. */
1043 gfc_expr *para = cons->expr;
1044 cons->expr = gfc_get_expr ();
1045 cons->expr->ts = para->ts;
1046 cons->expr->where = para->where;
1047 cons->expr->expr_type = EXPR_ARRAY;
1048 cons->expr->rank = para->rank;
1049 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1050 gfc_constructor_append_expr (&cons->expr->value.constructor,
1051 para, &cons->expr->where);
1053 if (cons->expr->expr_type == EXPR_ARRAY)
1055 gfc_constructor *p;
1056 p = gfc_constructor_first (cons->expr->value.constructor);
1057 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1059 gfc_charlen *cl, *cl2;
1061 cl2 = NULL;
1062 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1064 if (cl == cons->expr->ts.u.cl)
1065 break;
1066 cl2 = cl;
1069 gcc_assert (cl);
1071 if (cl2)
1072 cl2->next = cl->next;
1074 gfc_free_expr (cl->length);
1075 gfc_free (cl);
1078 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1079 cons->expr->ts.u.cl->length_from_typespec = true;
1080 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1081 gfc_resolve_character_array_constructor (cons->expr);
1085 if (cons->expr->expr_type == EXPR_NULL
1086 && !(comp->attr.pointer || comp->attr.allocatable
1087 || comp->attr.proc_pointer
1088 || (comp->ts.type == BT_CLASS
1089 && (CLASS_DATA (comp)->attr.class_pointer
1090 || CLASS_DATA (comp)->attr.allocatable))))
1092 t = FAILURE;
1093 gfc_error ("The NULL in the derived type constructor at %L is "
1094 "being applied to component '%s', which is neither "
1095 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1096 comp->name);
1099 if (!comp->attr.pointer || comp->attr.proc_pointer
1100 || cons->expr->expr_type == EXPR_NULL)
1101 continue;
1103 a = gfc_expr_attr (cons->expr);
1105 if (!a.pointer && !a.target)
1107 t = FAILURE;
1108 gfc_error ("The element in the derived type constructor at %L, "
1109 "for pointer component '%s' should be a POINTER or "
1110 "a TARGET", &cons->expr->where, comp->name);
1113 if (init)
1115 /* F08:C461. Additional checks for pointer initialization. */
1116 if (a.allocatable)
1118 t = FAILURE;
1119 gfc_error ("Pointer initialization target at %L "
1120 "must not be ALLOCATABLE ", &cons->expr->where);
1122 if (!a.save)
1124 t = FAILURE;
1125 gfc_error ("Pointer initialization target at %L "
1126 "must have the SAVE attribute", &cons->expr->where);
1130 /* F2003, C1272 (3). */
1131 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
1132 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1133 || gfc_is_coindexed (cons->expr)))
1135 t = FAILURE;
1136 gfc_error ("Invalid expression in the derived type constructor for "
1137 "pointer component '%s' at %L in PURE procedure",
1138 comp->name, &cons->expr->where);
1141 if (gfc_implicit_pure (NULL)
1142 && cons->expr->expr_type == EXPR_VARIABLE
1143 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1144 || gfc_is_coindexed (cons->expr)))
1145 gfc_current_ns->proc_name->attr.implicit_pure = 0;
1149 return t;
1153 /****************** Expression name resolution ******************/
1155 /* Returns 0 if a symbol was not declared with a type or
1156 attribute declaration statement, nonzero otherwise. */
1158 static int
1159 was_declared (gfc_symbol *sym)
1161 symbol_attribute a;
1163 a = sym->attr;
1165 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1166 return 1;
1168 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1169 || a.optional || a.pointer || a.save || a.target || a.volatile_
1170 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1171 || a.asynchronous || a.codimension)
1172 return 1;
1174 return 0;
1178 /* Determine if a symbol is generic or not. */
1180 static int
1181 generic_sym (gfc_symbol *sym)
1183 gfc_symbol *s;
1185 if (sym->attr.generic ||
1186 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1187 return 1;
1189 if (was_declared (sym) || sym->ns->parent == NULL)
1190 return 0;
1192 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1194 if (s != NULL)
1196 if (s == sym)
1197 return 0;
1198 else
1199 return generic_sym (s);
1202 return 0;
1206 /* Determine if a symbol is specific or not. */
1208 static int
1209 specific_sym (gfc_symbol *sym)
1211 gfc_symbol *s;
1213 if (sym->attr.if_source == IFSRC_IFBODY
1214 || sym->attr.proc == PROC_MODULE
1215 || sym->attr.proc == PROC_INTERNAL
1216 || sym->attr.proc == PROC_ST_FUNCTION
1217 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1218 || sym->attr.external)
1219 return 1;
1221 if (was_declared (sym) || sym->ns->parent == NULL)
1222 return 0;
1224 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1226 return (s == NULL) ? 0 : specific_sym (s);
1230 /* Figure out if the procedure is specific, generic or unknown. */
1232 typedef enum
1233 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1234 proc_type;
1236 static proc_type
1237 procedure_kind (gfc_symbol *sym)
1239 if (generic_sym (sym))
1240 return PTYPE_GENERIC;
1242 if (specific_sym (sym))
1243 return PTYPE_SPECIFIC;
1245 return PTYPE_UNKNOWN;
1248 /* Check references to assumed size arrays. The flag need_full_assumed_size
1249 is nonzero when matching actual arguments. */
1251 static int need_full_assumed_size = 0;
1253 static bool
1254 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1256 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1257 return false;
1259 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1260 What should it be? */
1261 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1262 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1263 && (e->ref->u.ar.type == AR_FULL))
1265 gfc_error ("The upper bound in the last dimension must "
1266 "appear in the reference to the assumed size "
1267 "array '%s' at %L", sym->name, &e->where);
1268 return true;
1270 return false;
1274 /* Look for bad assumed size array references in argument expressions
1275 of elemental and array valued intrinsic procedures. Since this is
1276 called from procedure resolution functions, it only recurses at
1277 operators. */
1279 static bool
1280 resolve_assumed_size_actual (gfc_expr *e)
1282 if (e == NULL)
1283 return false;
1285 switch (e->expr_type)
1287 case EXPR_VARIABLE:
1288 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1289 return true;
1290 break;
1292 case EXPR_OP:
1293 if (resolve_assumed_size_actual (e->value.op.op1)
1294 || resolve_assumed_size_actual (e->value.op.op2))
1295 return true;
1296 break;
1298 default:
1299 break;
1301 return false;
1305 /* Check a generic procedure, passed as an actual argument, to see if
1306 there is a matching specific name. If none, it is an error, and if
1307 more than one, the reference is ambiguous. */
1308 static int
1309 count_specific_procs (gfc_expr *e)
1311 int n;
1312 gfc_interface *p;
1313 gfc_symbol *sym;
1315 n = 0;
1316 sym = e->symtree->n.sym;
1318 for (p = sym->generic; p; p = p->next)
1319 if (strcmp (sym->name, p->sym->name) == 0)
1321 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1322 sym->name);
1323 n++;
1326 if (n > 1)
1327 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1328 &e->where);
1330 if (n == 0)
1331 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1332 "argument at %L", sym->name, &e->where);
1334 return n;
1338 /* See if a call to sym could possibly be a not allowed RECURSION because of
1339 a missing RECURIVE declaration. This means that either sym is the current
1340 context itself, or sym is the parent of a contained procedure calling its
1341 non-RECURSIVE containing procedure.
1342 This also works if sym is an ENTRY. */
1344 static bool
1345 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1347 gfc_symbol* proc_sym;
1348 gfc_symbol* context_proc;
1349 gfc_namespace* real_context;
1351 if (sym->attr.flavor == FL_PROGRAM)
1352 return false;
1354 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1356 /* If we've got an ENTRY, find real procedure. */
1357 if (sym->attr.entry && sym->ns->entries)
1358 proc_sym = sym->ns->entries->sym;
1359 else
1360 proc_sym = sym;
1362 /* If sym is RECURSIVE, all is well of course. */
1363 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1364 return false;
1366 /* Find the context procedure's "real" symbol if it has entries.
1367 We look for a procedure symbol, so recurse on the parents if we don't
1368 find one (like in case of a BLOCK construct). */
1369 for (real_context = context; ; real_context = real_context->parent)
1371 /* We should find something, eventually! */
1372 gcc_assert (real_context);
1374 context_proc = (real_context->entries ? real_context->entries->sym
1375 : real_context->proc_name);
1377 /* In some special cases, there may not be a proc_name, like for this
1378 invalid code:
1379 real(bad_kind()) function foo () ...
1380 when checking the call to bad_kind ().
1381 In these cases, we simply return here and assume that the
1382 call is ok. */
1383 if (!context_proc)
1384 return false;
1386 if (context_proc->attr.flavor != FL_LABEL)
1387 break;
1390 /* A call from sym's body to itself is recursion, of course. */
1391 if (context_proc == proc_sym)
1392 return true;
1394 /* The same is true if context is a contained procedure and sym the
1395 containing one. */
1396 if (context_proc->attr.contained)
1398 gfc_symbol* parent_proc;
1400 gcc_assert (context->parent);
1401 parent_proc = (context->parent->entries ? context->parent->entries->sym
1402 : context->parent->proc_name);
1404 if (parent_proc == proc_sym)
1405 return true;
1408 return false;
1412 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1413 its typespec and formal argument list. */
1415 static gfc_try
1416 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1418 gfc_intrinsic_sym* isym = NULL;
1419 const char* symstd;
1421 if (sym->formal)
1422 return SUCCESS;
1424 /* We already know this one is an intrinsic, so we don't call
1425 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1426 gfc_find_subroutine directly to check whether it is a function or
1427 subroutine. */
1429 if (sym->intmod_sym_id)
1430 isym = gfc_intrinsic_function_by_id ((gfc_isym_id) sym->intmod_sym_id);
1431 else
1432 isym = gfc_find_function (sym->name);
1434 if (isym)
1436 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1437 && !sym->attr.implicit_type)
1438 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1439 " ignored", sym->name, &sym->declared_at);
1441 if (!sym->attr.function &&
1442 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1443 return FAILURE;
1445 sym->ts = isym->ts;
1447 else if ((isym = gfc_find_subroutine (sym->name)))
1449 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1451 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1452 " specifier", sym->name, &sym->declared_at);
1453 return FAILURE;
1456 if (!sym->attr.subroutine &&
1457 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1458 return FAILURE;
1460 else
1462 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1463 &sym->declared_at);
1464 return FAILURE;
1467 gfc_copy_formal_args_intr (sym, isym);
1469 /* Check it is actually available in the standard settings. */
1470 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1471 == FAILURE)
1473 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1474 " available in the current standard settings but %s. Use"
1475 " an appropriate -std=* option or enable -fall-intrinsics"
1476 " in order to use it.",
1477 sym->name, &sym->declared_at, symstd);
1478 return FAILURE;
1481 return SUCCESS;
1485 /* Resolve a procedure expression, like passing it to a called procedure or as
1486 RHS for a procedure pointer assignment. */
1488 static gfc_try
1489 resolve_procedure_expression (gfc_expr* expr)
1491 gfc_symbol* sym;
1493 if (expr->expr_type != EXPR_VARIABLE)
1494 return SUCCESS;
1495 gcc_assert (expr->symtree);
1497 sym = expr->symtree->n.sym;
1499 if (sym->attr.intrinsic)
1500 resolve_intrinsic (sym, &expr->where);
1502 if (sym->attr.flavor != FL_PROCEDURE
1503 || (sym->attr.function && sym->result == sym))
1504 return SUCCESS;
1506 /* A non-RECURSIVE procedure that is used as procedure expression within its
1507 own body is in danger of being called recursively. */
1508 if (is_illegal_recursion (sym, gfc_current_ns))
1509 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1510 " itself recursively. Declare it RECURSIVE or use"
1511 " -frecursive", sym->name, &expr->where);
1513 return SUCCESS;
1517 /* Resolve an actual argument list. Most of the time, this is just
1518 resolving the expressions in the list.
1519 The exception is that we sometimes have to decide whether arguments
1520 that look like procedure arguments are really simple variable
1521 references. */
1523 static gfc_try
1524 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1525 bool no_formal_args)
1527 gfc_symbol *sym;
1528 gfc_symtree *parent_st;
1529 gfc_expr *e;
1530 int save_need_full_assumed_size;
1532 for (; arg; arg = arg->next)
1534 e = arg->expr;
1535 if (e == NULL)
1537 /* Check the label is a valid branching target. */
1538 if (arg->label)
1540 if (arg->label->defined == ST_LABEL_UNKNOWN)
1542 gfc_error ("Label %d referenced at %L is never defined",
1543 arg->label->value, &arg->label->where);
1544 return FAILURE;
1547 continue;
1550 if (e->expr_type == EXPR_VARIABLE
1551 && e->symtree->n.sym->attr.generic
1552 && no_formal_args
1553 && count_specific_procs (e) != 1)
1554 return FAILURE;
1556 if (e->ts.type != BT_PROCEDURE)
1558 save_need_full_assumed_size = need_full_assumed_size;
1559 if (e->expr_type != EXPR_VARIABLE)
1560 need_full_assumed_size = 0;
1561 if (gfc_resolve_expr (e) != SUCCESS)
1562 return FAILURE;
1563 need_full_assumed_size = save_need_full_assumed_size;
1564 goto argument_list;
1567 /* See if the expression node should really be a variable reference. */
1569 sym = e->symtree->n.sym;
1571 if (sym->attr.flavor == FL_PROCEDURE
1572 || sym->attr.intrinsic
1573 || sym->attr.external)
1575 int actual_ok;
1577 /* If a procedure is not already determined to be something else
1578 check if it is intrinsic. */
1579 if (!sym->attr.intrinsic
1580 && !(sym->attr.external || sym->attr.use_assoc
1581 || sym->attr.if_source == IFSRC_IFBODY)
1582 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1583 sym->attr.intrinsic = 1;
1585 if (sym->attr.proc == PROC_ST_FUNCTION)
1587 gfc_error ("Statement function '%s' at %L is not allowed as an "
1588 "actual argument", sym->name, &e->where);
1591 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1592 sym->attr.subroutine);
1593 if (sym->attr.intrinsic && actual_ok == 0)
1595 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1596 "actual argument", sym->name, &e->where);
1599 if (sym->attr.contained && !sym->attr.use_assoc
1600 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1602 if (gfc_notify_std (GFC_STD_F2008,
1603 "Fortran 2008: Internal procedure '%s' is"
1604 " used as actual argument at %L",
1605 sym->name, &e->where) == FAILURE)
1606 return FAILURE;
1609 if (sym->attr.elemental && !sym->attr.intrinsic)
1611 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1612 "allowed as an actual argument at %L", sym->name,
1613 &e->where);
1616 /* Check if a generic interface has a specific procedure
1617 with the same name before emitting an error. */
1618 if (sym->attr.generic && count_specific_procs (e) != 1)
1619 return FAILURE;
1621 /* Just in case a specific was found for the expression. */
1622 sym = e->symtree->n.sym;
1624 /* If the symbol is the function that names the current (or
1625 parent) scope, then we really have a variable reference. */
1627 if (gfc_is_function_return_value (sym, sym->ns))
1628 goto got_variable;
1630 /* If all else fails, see if we have a specific intrinsic. */
1631 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1633 gfc_intrinsic_sym *isym;
1635 isym = gfc_find_function (sym->name);
1636 if (isym == NULL || !isym->specific)
1638 gfc_error ("Unable to find a specific INTRINSIC procedure "
1639 "for the reference '%s' at %L", sym->name,
1640 &e->where);
1641 return FAILURE;
1643 sym->ts = isym->ts;
1644 sym->attr.intrinsic = 1;
1645 sym->attr.function = 1;
1648 if (gfc_resolve_expr (e) == FAILURE)
1649 return FAILURE;
1650 goto argument_list;
1653 /* See if the name is a module procedure in a parent unit. */
1655 if (was_declared (sym) || sym->ns->parent == NULL)
1656 goto got_variable;
1658 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1660 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1661 return FAILURE;
1664 if (parent_st == NULL)
1665 goto got_variable;
1667 sym = parent_st->n.sym;
1668 e->symtree = parent_st; /* Point to the right thing. */
1670 if (sym->attr.flavor == FL_PROCEDURE
1671 || sym->attr.intrinsic
1672 || sym->attr.external)
1674 if (gfc_resolve_expr (e) == FAILURE)
1675 return FAILURE;
1676 goto argument_list;
1679 got_variable:
1680 e->expr_type = EXPR_VARIABLE;
1681 e->ts = sym->ts;
1682 if (sym->as != NULL)
1684 e->rank = sym->as->rank;
1685 e->ref = gfc_get_ref ();
1686 e->ref->type = REF_ARRAY;
1687 e->ref->u.ar.type = AR_FULL;
1688 e->ref->u.ar.as = sym->as;
1691 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1692 primary.c (match_actual_arg). If above code determines that it
1693 is a variable instead, it needs to be resolved as it was not
1694 done at the beginning of this function. */
1695 save_need_full_assumed_size = need_full_assumed_size;
1696 if (e->expr_type != EXPR_VARIABLE)
1697 need_full_assumed_size = 0;
1698 if (gfc_resolve_expr (e) != SUCCESS)
1699 return FAILURE;
1700 need_full_assumed_size = save_need_full_assumed_size;
1702 argument_list:
1703 /* Check argument list functions %VAL, %LOC and %REF. There is
1704 nothing to do for %REF. */
1705 if (arg->name && arg->name[0] == '%')
1707 if (strncmp ("%VAL", arg->name, 4) == 0)
1709 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1711 gfc_error ("By-value argument at %L is not of numeric "
1712 "type", &e->where);
1713 return FAILURE;
1716 if (e->rank)
1718 gfc_error ("By-value argument at %L cannot be an array or "
1719 "an array section", &e->where);
1720 return FAILURE;
1723 /* Intrinsics are still PROC_UNKNOWN here. However,
1724 since same file external procedures are not resolvable
1725 in gfortran, it is a good deal easier to leave them to
1726 intrinsic.c. */
1727 if (ptype != PROC_UNKNOWN
1728 && ptype != PROC_DUMMY
1729 && ptype != PROC_EXTERNAL
1730 && ptype != PROC_MODULE)
1732 gfc_error ("By-value argument at %L is not allowed "
1733 "in this context", &e->where);
1734 return FAILURE;
1738 /* Statement functions have already been excluded above. */
1739 else if (strncmp ("%LOC", arg->name, 4) == 0
1740 && e->ts.type == BT_PROCEDURE)
1742 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1744 gfc_error ("Passing internal procedure at %L by location "
1745 "not allowed", &e->where);
1746 return FAILURE;
1751 /* Fortran 2008, C1237. */
1752 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1753 && gfc_has_ultimate_pointer (e))
1755 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1756 "component", &e->where);
1757 return FAILURE;
1761 return SUCCESS;
1765 /* Do the checks of the actual argument list that are specific to elemental
1766 procedures. If called with c == NULL, we have a function, otherwise if
1767 expr == NULL, we have a subroutine. */
1769 static gfc_try
1770 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1772 gfc_actual_arglist *arg0;
1773 gfc_actual_arglist *arg;
1774 gfc_symbol *esym = NULL;
1775 gfc_intrinsic_sym *isym = NULL;
1776 gfc_expr *e = NULL;
1777 gfc_intrinsic_arg *iformal = NULL;
1778 gfc_formal_arglist *eformal = NULL;
1779 bool formal_optional = false;
1780 bool set_by_optional = false;
1781 int i;
1782 int rank = 0;
1784 /* Is this an elemental procedure? */
1785 if (expr && expr->value.function.actual != NULL)
1787 if (expr->value.function.esym != NULL
1788 && expr->value.function.esym->attr.elemental)
1790 arg0 = expr->value.function.actual;
1791 esym = expr->value.function.esym;
1793 else if (expr->value.function.isym != NULL
1794 && expr->value.function.isym->elemental)
1796 arg0 = expr->value.function.actual;
1797 isym = expr->value.function.isym;
1799 else
1800 return SUCCESS;
1802 else if (c && c->ext.actual != NULL)
1804 arg0 = c->ext.actual;
1806 if (c->resolved_sym)
1807 esym = c->resolved_sym;
1808 else
1809 esym = c->symtree->n.sym;
1810 gcc_assert (esym);
1812 if (!esym->attr.elemental)
1813 return SUCCESS;
1815 else
1816 return SUCCESS;
1818 /* The rank of an elemental is the rank of its array argument(s). */
1819 for (arg = arg0; arg; arg = arg->next)
1821 if (arg->expr != NULL && arg->expr->rank > 0)
1823 rank = arg->expr->rank;
1824 if (arg->expr->expr_type == EXPR_VARIABLE
1825 && arg->expr->symtree->n.sym->attr.optional)
1826 set_by_optional = true;
1828 /* Function specific; set the result rank and shape. */
1829 if (expr)
1831 expr->rank = rank;
1832 if (!expr->shape && arg->expr->shape)
1834 expr->shape = gfc_get_shape (rank);
1835 for (i = 0; i < rank; i++)
1836 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1839 break;
1843 /* If it is an array, it shall not be supplied as an actual argument
1844 to an elemental procedure unless an array of the same rank is supplied
1845 as an actual argument corresponding to a nonoptional dummy argument of
1846 that elemental procedure(12.4.1.5). */
1847 formal_optional = false;
1848 if (isym)
1849 iformal = isym->formal;
1850 else
1851 eformal = esym->formal;
1853 for (arg = arg0; arg; arg = arg->next)
1855 if (eformal)
1857 if (eformal->sym && eformal->sym->attr.optional)
1858 formal_optional = true;
1859 eformal = eformal->next;
1861 else if (isym && iformal)
1863 if (iformal->optional)
1864 formal_optional = true;
1865 iformal = iformal->next;
1867 else if (isym)
1868 formal_optional = true;
1870 if (pedantic && arg->expr != NULL
1871 && arg->expr->expr_type == EXPR_VARIABLE
1872 && arg->expr->symtree->n.sym->attr.optional
1873 && formal_optional
1874 && arg->expr->rank
1875 && (set_by_optional || arg->expr->rank != rank)
1876 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1878 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1879 "MISSING, it cannot be the actual argument of an "
1880 "ELEMENTAL procedure unless there is a non-optional "
1881 "argument with the same rank (12.4.1.5)",
1882 arg->expr->symtree->n.sym->name, &arg->expr->where);
1883 return FAILURE;
1887 for (arg = arg0; arg; arg = arg->next)
1889 if (arg->expr == NULL || arg->expr->rank == 0)
1890 continue;
1892 /* Being elemental, the last upper bound of an assumed size array
1893 argument must be present. */
1894 if (resolve_assumed_size_actual (arg->expr))
1895 return FAILURE;
1897 /* Elemental procedure's array actual arguments must conform. */
1898 if (e != NULL)
1900 if (gfc_check_conformance (arg->expr, e,
1901 "elemental procedure") == FAILURE)
1902 return FAILURE;
1904 else
1905 e = arg->expr;
1908 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1909 is an array, the intent inout/out variable needs to be also an array. */
1910 if (rank > 0 && esym && expr == NULL)
1911 for (eformal = esym->formal, arg = arg0; arg && eformal;
1912 arg = arg->next, eformal = eformal->next)
1913 if ((eformal->sym->attr.intent == INTENT_OUT
1914 || eformal->sym->attr.intent == INTENT_INOUT)
1915 && arg->expr && arg->expr->rank == 0)
1917 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1918 "ELEMENTAL subroutine '%s' is a scalar, but another "
1919 "actual argument is an array", &arg->expr->where,
1920 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1921 : "INOUT", eformal->sym->name, esym->name);
1922 return FAILURE;
1924 return SUCCESS;
1928 /* This function does the checking of references to global procedures
1929 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1930 77 and 95 standards. It checks for a gsymbol for the name, making
1931 one if it does not already exist. If it already exists, then the
1932 reference being resolved must correspond to the type of gsymbol.
1933 Otherwise, the new symbol is equipped with the attributes of the
1934 reference. The corresponding code that is called in creating
1935 global entities is parse.c.
1937 In addition, for all but -std=legacy, the gsymbols are used to
1938 check the interfaces of external procedures from the same file.
1939 The namespace of the gsymbol is resolved and then, once this is
1940 done the interface is checked. */
1943 static bool
1944 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1946 if (!gsym_ns->proc_name->attr.recursive)
1947 return true;
1949 if (sym->ns == gsym_ns)
1950 return false;
1952 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1953 return false;
1955 return true;
1958 static bool
1959 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1961 if (gsym_ns->entries)
1963 gfc_entry_list *entry = gsym_ns->entries;
1965 for (; entry; entry = entry->next)
1967 if (strcmp (sym->name, entry->sym->name) == 0)
1969 if (strcmp (gsym_ns->proc_name->name,
1970 sym->ns->proc_name->name) == 0)
1971 return false;
1973 if (sym->ns->parent
1974 && strcmp (gsym_ns->proc_name->name,
1975 sym->ns->parent->proc_name->name) == 0)
1976 return false;
1980 return true;
1983 static void
1984 resolve_global_procedure (gfc_symbol *sym, locus *where,
1985 gfc_actual_arglist **actual, int sub)
1987 gfc_gsymbol * gsym;
1988 gfc_namespace *ns;
1989 enum gfc_symbol_type type;
1991 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1993 gsym = gfc_get_gsymbol (sym->name);
1995 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1996 gfc_global_used (gsym, where);
1998 if (gfc_option.flag_whole_file
1999 && (sym->attr.if_source == IFSRC_UNKNOWN
2000 || sym->attr.if_source == IFSRC_IFBODY)
2001 && gsym->type != GSYM_UNKNOWN
2002 && gsym->ns
2003 && gsym->ns->resolved != -1
2004 && gsym->ns->proc_name
2005 && not_in_recursive (sym, gsym->ns)
2006 && not_entry_self_reference (sym, gsym->ns))
2008 gfc_symbol *def_sym;
2010 /* Resolve the gsymbol namespace if needed. */
2011 if (!gsym->ns->resolved)
2013 gfc_dt_list *old_dt_list;
2014 struct gfc_omp_saved_state old_omp_state;
2016 /* Stash away derived types so that the backend_decls do not
2017 get mixed up. */
2018 old_dt_list = gfc_derived_types;
2019 gfc_derived_types = NULL;
2020 /* And stash away openmp state. */
2021 gfc_omp_save_and_clear_state (&old_omp_state);
2023 gfc_resolve (gsym->ns);
2025 /* Store the new derived types with the global namespace. */
2026 if (gfc_derived_types)
2027 gsym->ns->derived_types = gfc_derived_types;
2029 /* Restore the derived types of this namespace. */
2030 gfc_derived_types = old_dt_list;
2031 /* And openmp state. */
2032 gfc_omp_restore_state (&old_omp_state);
2035 /* Make sure that translation for the gsymbol occurs before
2036 the procedure currently being resolved. */
2037 ns = gfc_global_ns_list;
2038 for (; ns && ns != gsym->ns; ns = ns->sibling)
2040 if (ns->sibling == gsym->ns)
2042 ns->sibling = gsym->ns->sibling;
2043 gsym->ns->sibling = gfc_global_ns_list;
2044 gfc_global_ns_list = gsym->ns;
2045 break;
2049 def_sym = gsym->ns->proc_name;
2050 if (def_sym->attr.entry_master)
2052 gfc_entry_list *entry;
2053 for (entry = gsym->ns->entries; entry; entry = entry->next)
2054 if (strcmp (entry->sym->name, sym->name) == 0)
2056 def_sym = entry->sym;
2057 break;
2061 /* Differences in constant character lengths. */
2062 if (sym->attr.function && sym->ts.type == BT_CHARACTER)
2064 long int l1 = 0, l2 = 0;
2065 gfc_charlen *cl1 = sym->ts.u.cl;
2066 gfc_charlen *cl2 = def_sym->ts.u.cl;
2068 if (cl1 != NULL
2069 && cl1->length != NULL
2070 && cl1->length->expr_type == EXPR_CONSTANT)
2071 l1 = mpz_get_si (cl1->length->value.integer);
2073 if (cl2 != NULL
2074 && cl2->length != NULL
2075 && cl2->length->expr_type == EXPR_CONSTANT)
2076 l2 = mpz_get_si (cl2->length->value.integer);
2078 if (l1 && l2 && l1 != l2)
2079 gfc_error ("Character length mismatch in return type of "
2080 "function '%s' at %L (%ld/%ld)", sym->name,
2081 &sym->declared_at, l1, l2);
2084 /* Type mismatch of function return type and expected type. */
2085 if (sym->attr.function
2086 && !gfc_compare_types (&sym->ts, &def_sym->ts))
2087 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2088 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2089 gfc_typename (&def_sym->ts));
2091 if (def_sym->formal && sym->attr.if_source != IFSRC_IFBODY)
2093 gfc_formal_arglist *arg = def_sym->formal;
2094 for ( ; arg; arg = arg->next)
2095 if (!arg->sym)
2096 continue;
2097 /* F2003, 12.3.1.1 (2a); F2008, 12.4.2.2 (2a) */
2098 else if (arg->sym->attr.allocatable
2099 || arg->sym->attr.asynchronous
2100 || arg->sym->attr.optional
2101 || arg->sym->attr.pointer
2102 || arg->sym->attr.target
2103 || arg->sym->attr.value
2104 || arg->sym->attr.volatile_)
2106 gfc_error ("Dummy argument '%s' of procedure '%s' at %L "
2107 "has an attribute that requires an explicit "
2108 "interface for this procedure", arg->sym->name,
2109 sym->name, &sym->declared_at);
2110 break;
2112 /* F2003, 12.3.1.1 (2b); F2008, 12.4.2.2 (2b) */
2113 else if (arg->sym && arg->sym->as
2114 && arg->sym->as->type == AS_ASSUMED_SHAPE)
2116 gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
2117 "argument '%s' must have an explicit interface",
2118 sym->name, &sym->declared_at, arg->sym->name);
2119 break;
2121 /* F2008, 12.4.2.2 (2c) */
2122 else if (arg->sym->attr.codimension)
2124 gfc_error ("Procedure '%s' at %L with coarray dummy argument "
2125 "'%s' must have an explicit interface",
2126 sym->name, &sym->declared_at, arg->sym->name);
2127 break;
2129 /* F2003, 12.3.1.1 (2c); F2008, 12.4.2.2 (2d) */
2130 else if (false) /* TODO: is a parametrized derived type */
2132 gfc_error ("Procedure '%s' at %L with parametrized derived "
2133 "type argument '%s' must have an explicit "
2134 "interface", sym->name, &sym->declared_at,
2135 arg->sym->name);
2136 break;
2138 /* F2003, 12.3.1.1 (2d); F2008, 12.4.2.2 (2e) */
2139 else if (arg->sym->ts.type == BT_CLASS)
2141 gfc_error ("Procedure '%s' at %L with polymorphic dummy "
2142 "argument '%s' must have an explicit interface",
2143 sym->name, &sym->declared_at, arg->sym->name);
2144 break;
2148 if (def_sym->attr.function)
2150 /* F2003, 12.3.1.1 (3a); F2008, 12.4.2.2 (3a) */
2151 if (def_sym->as && def_sym->as->rank
2152 && (!sym->as || sym->as->rank != def_sym->as->rank))
2153 gfc_error ("The reference to function '%s' at %L either needs an "
2154 "explicit INTERFACE or the rank is incorrect", sym->name,
2155 where);
2157 /* F2003, 12.3.1.1 (3b); F2008, 12.4.2.2 (3b) */
2158 if ((def_sym->result->attr.pointer
2159 || def_sym->result->attr.allocatable)
2160 && (sym->attr.if_source != IFSRC_IFBODY
2161 || def_sym->result->attr.pointer
2162 != sym->result->attr.pointer
2163 || def_sym->result->attr.allocatable
2164 != sym->result->attr.allocatable))
2165 gfc_error ("Function '%s' at %L with a POINTER or ALLOCATABLE "
2166 "result must have an explicit interface", sym->name,
2167 where);
2169 /* F2003, 12.3.1.1 (3c); F2008, 12.4.2.2 (3c) */
2170 if (sym->ts.type == BT_CHARACTER && sym->attr.if_source != IFSRC_IFBODY
2171 && def_sym->ts.u.cl->length != NULL)
2173 gfc_charlen *cl = sym->ts.u.cl;
2175 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
2176 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
2178 gfc_error ("Nonconstant character-length function '%s' at %L "
2179 "must have an explicit interface", sym->name,
2180 &sym->declared_at);
2185 /* F2003, 12.3.1.1 (4); F2008, 12.4.2.2 (4) */
2186 if (def_sym->attr.elemental && !sym->attr.elemental)
2188 gfc_error ("ELEMENTAL procedure '%s' at %L must have an explicit "
2189 "interface", sym->name, &sym->declared_at);
2192 /* F2003, 12.3.1.1 (5); F2008, 12.4.2.2 (5) */
2193 if (def_sym->attr.is_bind_c && !sym->attr.is_bind_c)
2195 gfc_error ("Procedure '%s' at %L with BIND(C) attribute must have "
2196 "an explicit interface", sym->name, &sym->declared_at);
2199 if (gfc_option.flag_whole_file == 1
2200 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2201 && !(gfc_option.warn_std & GFC_STD_GNU)))
2202 gfc_errors_to_warnings (1);
2204 if (sym->attr.if_source != IFSRC_IFBODY)
2205 gfc_procedure_use (def_sym, actual, where);
2207 gfc_errors_to_warnings (0);
2210 if (gsym->type == GSYM_UNKNOWN)
2212 gsym->type = type;
2213 gsym->where = *where;
2216 gsym->used = 1;
2220 /************* Function resolution *************/
2222 /* Resolve a function call known to be generic.
2223 Section 14.1.2.4.1. */
2225 static match
2226 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2228 gfc_symbol *s;
2230 if (sym->attr.generic)
2232 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2233 if (s != NULL)
2235 expr->value.function.name = s->name;
2236 expr->value.function.esym = s;
2238 if (s->ts.type != BT_UNKNOWN)
2239 expr->ts = s->ts;
2240 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2241 expr->ts = s->result->ts;
2243 if (s->as != NULL)
2244 expr->rank = s->as->rank;
2245 else if (s->result != NULL && s->result->as != NULL)
2246 expr->rank = s->result->as->rank;
2248 gfc_set_sym_referenced (expr->value.function.esym);
2250 return MATCH_YES;
2253 /* TODO: Need to search for elemental references in generic
2254 interface. */
2257 if (sym->attr.intrinsic)
2258 return gfc_intrinsic_func_interface (expr, 0);
2260 return MATCH_NO;
2264 static gfc_try
2265 resolve_generic_f (gfc_expr *expr)
2267 gfc_symbol *sym;
2268 match m;
2270 sym = expr->symtree->n.sym;
2272 for (;;)
2274 m = resolve_generic_f0 (expr, sym);
2275 if (m == MATCH_YES)
2276 return SUCCESS;
2277 else if (m == MATCH_ERROR)
2278 return FAILURE;
2280 generic:
2281 if (sym->ns->parent == NULL)
2282 break;
2283 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2285 if (sym == NULL)
2286 break;
2287 if (!generic_sym (sym))
2288 goto generic;
2291 /* Last ditch attempt. See if the reference is to an intrinsic
2292 that possesses a matching interface. 14.1.2.4 */
2293 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
2295 gfc_error ("There is no specific function for the generic '%s' at %L",
2296 expr->symtree->n.sym->name, &expr->where);
2297 return FAILURE;
2300 m = gfc_intrinsic_func_interface (expr, 0);
2301 if (m == MATCH_YES)
2302 return SUCCESS;
2303 if (m == MATCH_NO)
2304 gfc_error ("Generic function '%s' at %L is not consistent with a "
2305 "specific intrinsic interface", expr->symtree->n.sym->name,
2306 &expr->where);
2308 return FAILURE;
2312 /* Resolve a function call known to be specific. */
2314 static match
2315 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2317 match m;
2319 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2321 if (sym->attr.dummy)
2323 sym->attr.proc = PROC_DUMMY;
2324 goto found;
2327 sym->attr.proc = PROC_EXTERNAL;
2328 goto found;
2331 if (sym->attr.proc == PROC_MODULE
2332 || sym->attr.proc == PROC_ST_FUNCTION
2333 || sym->attr.proc == PROC_INTERNAL)
2334 goto found;
2336 if (sym->attr.intrinsic)
2338 m = gfc_intrinsic_func_interface (expr, 1);
2339 if (m == MATCH_YES)
2340 return MATCH_YES;
2341 if (m == MATCH_NO)
2342 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2343 "with an intrinsic", sym->name, &expr->where);
2345 return MATCH_ERROR;
2348 return MATCH_NO;
2350 found:
2351 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2353 if (sym->result)
2354 expr->ts = sym->result->ts;
2355 else
2356 expr->ts = sym->ts;
2357 expr->value.function.name = sym->name;
2358 expr->value.function.esym = sym;
2359 if (sym->as != NULL)
2360 expr->rank = sym->as->rank;
2362 return MATCH_YES;
2366 static gfc_try
2367 resolve_specific_f (gfc_expr *expr)
2369 gfc_symbol *sym;
2370 match m;
2372 sym = expr->symtree->n.sym;
2374 for (;;)
2376 m = resolve_specific_f0 (sym, expr);
2377 if (m == MATCH_YES)
2378 return SUCCESS;
2379 if (m == MATCH_ERROR)
2380 return FAILURE;
2382 if (sym->ns->parent == NULL)
2383 break;
2385 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2387 if (sym == NULL)
2388 break;
2391 gfc_error ("Unable to resolve the specific function '%s' at %L",
2392 expr->symtree->n.sym->name, &expr->where);
2394 return SUCCESS;
2398 /* Resolve a procedure call not known to be generic nor specific. */
2400 static gfc_try
2401 resolve_unknown_f (gfc_expr *expr)
2403 gfc_symbol *sym;
2404 gfc_typespec *ts;
2406 sym = expr->symtree->n.sym;
2408 if (sym->attr.dummy)
2410 sym->attr.proc = PROC_DUMMY;
2411 expr->value.function.name = sym->name;
2412 goto set_type;
2415 /* See if we have an intrinsic function reference. */
2417 if (gfc_is_intrinsic (sym, 0, expr->where))
2419 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2420 return SUCCESS;
2421 return FAILURE;
2424 /* The reference is to an external name. */
2426 sym->attr.proc = PROC_EXTERNAL;
2427 expr->value.function.name = sym->name;
2428 expr->value.function.esym = expr->symtree->n.sym;
2430 if (sym->as != NULL)
2431 expr->rank = sym->as->rank;
2433 /* Type of the expression is either the type of the symbol or the
2434 default type of the symbol. */
2436 set_type:
2437 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2439 if (sym->ts.type != BT_UNKNOWN)
2440 expr->ts = sym->ts;
2441 else
2443 ts = gfc_get_default_type (sym->name, sym->ns);
2445 if (ts->type == BT_UNKNOWN)
2447 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2448 sym->name, &expr->where);
2449 return FAILURE;
2451 else
2452 expr->ts = *ts;
2455 return SUCCESS;
2459 /* Return true, if the symbol is an external procedure. */
2460 static bool
2461 is_external_proc (gfc_symbol *sym)
2463 if (!sym->attr.dummy && !sym->attr.contained
2464 && !(sym->attr.intrinsic
2465 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2466 && sym->attr.proc != PROC_ST_FUNCTION
2467 && !sym->attr.proc_pointer
2468 && !sym->attr.use_assoc
2469 && sym->name)
2470 return true;
2472 return false;
2476 /* Figure out if a function reference is pure or not. Also set the name
2477 of the function for a potential error message. Return nonzero if the
2478 function is PURE, zero if not. */
2479 static int
2480 pure_stmt_function (gfc_expr *, gfc_symbol *);
2482 static int
2483 pure_function (gfc_expr *e, const char **name)
2485 int pure;
2487 *name = NULL;
2489 if (e->symtree != NULL
2490 && e->symtree->n.sym != NULL
2491 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2492 return pure_stmt_function (e, e->symtree->n.sym);
2494 if (e->value.function.esym)
2496 pure = gfc_pure (e->value.function.esym);
2497 *name = e->value.function.esym->name;
2499 else if (e->value.function.isym)
2501 pure = e->value.function.isym->pure
2502 || e->value.function.isym->elemental;
2503 *name = e->value.function.isym->name;
2505 else
2507 /* Implicit functions are not pure. */
2508 pure = 0;
2509 *name = e->value.function.name;
2512 return pure;
2516 static bool
2517 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2518 int *f ATTRIBUTE_UNUSED)
2520 const char *name;
2522 /* Don't bother recursing into other statement functions
2523 since they will be checked individually for purity. */
2524 if (e->expr_type != EXPR_FUNCTION
2525 || !e->symtree
2526 || e->symtree->n.sym == sym
2527 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2528 return false;
2530 return pure_function (e, &name) ? false : true;
2534 static int
2535 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2537 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2541 static gfc_try
2542 is_scalar_expr_ptr (gfc_expr *expr)
2544 gfc_try retval = SUCCESS;
2545 gfc_ref *ref;
2546 int start;
2547 int end;
2549 /* See if we have a gfc_ref, which means we have a substring, array
2550 reference, or a component. */
2551 if (expr->ref != NULL)
2553 ref = expr->ref;
2554 while (ref->next != NULL)
2555 ref = ref->next;
2557 switch (ref->type)
2559 case REF_SUBSTRING:
2560 if (ref->u.ss.start == NULL || ref->u.ss.end == NULL
2561 || gfc_dep_compare_expr (ref->u.ss.start, ref->u.ss.end) != 0)
2562 retval = FAILURE;
2563 break;
2565 case REF_ARRAY:
2566 if (ref->u.ar.type == AR_ELEMENT)
2567 retval = SUCCESS;
2568 else if (ref->u.ar.type == AR_FULL)
2570 /* The user can give a full array if the array is of size 1. */
2571 if (ref->u.ar.as != NULL
2572 && ref->u.ar.as->rank == 1
2573 && ref->u.ar.as->type == AS_EXPLICIT
2574 && ref->u.ar.as->lower[0] != NULL
2575 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2576 && ref->u.ar.as->upper[0] != NULL
2577 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2579 /* If we have a character string, we need to check if
2580 its length is one. */
2581 if (expr->ts.type == BT_CHARACTER)
2583 if (expr->ts.u.cl == NULL
2584 || expr->ts.u.cl->length == NULL
2585 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2586 != 0)
2587 retval = FAILURE;
2589 else
2591 /* We have constant lower and upper bounds. If the
2592 difference between is 1, it can be considered a
2593 scalar.
2594 FIXME: Use gfc_dep_compare_expr instead. */
2595 start = (int) mpz_get_si
2596 (ref->u.ar.as->lower[0]->value.integer);
2597 end = (int) mpz_get_si
2598 (ref->u.ar.as->upper[0]->value.integer);
2599 if (end - start + 1 != 1)
2600 retval = FAILURE;
2603 else
2604 retval = FAILURE;
2606 else
2607 retval = FAILURE;
2608 break;
2609 default:
2610 retval = SUCCESS;
2611 break;
2614 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2616 /* Character string. Make sure it's of length 1. */
2617 if (expr->ts.u.cl == NULL
2618 || expr->ts.u.cl->length == NULL
2619 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2620 retval = FAILURE;
2622 else if (expr->rank != 0)
2623 retval = FAILURE;
2625 return retval;
2629 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2630 and, in the case of c_associated, set the binding label based on
2631 the arguments. */
2633 static gfc_try
2634 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2635 gfc_symbol **new_sym)
2637 char name[GFC_MAX_SYMBOL_LEN + 1];
2638 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2639 int optional_arg = 0;
2640 gfc_try retval = SUCCESS;
2641 gfc_symbol *args_sym;
2642 gfc_typespec *arg_ts;
2643 symbol_attribute arg_attr;
2645 if (args->expr->expr_type == EXPR_CONSTANT
2646 || args->expr->expr_type == EXPR_OP
2647 || args->expr->expr_type == EXPR_NULL)
2649 gfc_error ("Argument to '%s' at %L is not a variable",
2650 sym->name, &(args->expr->where));
2651 return FAILURE;
2654 args_sym = args->expr->symtree->n.sym;
2656 /* The typespec for the actual arg should be that stored in the expr
2657 and not necessarily that of the expr symbol (args_sym), because
2658 the actual expression could be a part-ref of the expr symbol. */
2659 arg_ts = &(args->expr->ts);
2660 arg_attr = gfc_expr_attr (args->expr);
2662 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2664 /* If the user gave two args then they are providing something for
2665 the optional arg (the second cptr). Therefore, set the name and
2666 binding label to the c_associated for two cptrs. Otherwise,
2667 set c_associated to expect one cptr. */
2668 if (args->next)
2670 /* two args. */
2671 sprintf (name, "%s_2", sym->name);
2672 sprintf (binding_label, "%s_2", sym->binding_label);
2673 optional_arg = 1;
2675 else
2677 /* one arg. */
2678 sprintf (name, "%s_1", sym->name);
2679 sprintf (binding_label, "%s_1", sym->binding_label);
2680 optional_arg = 0;
2683 /* Get a new symbol for the version of c_associated that
2684 will get called. */
2685 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2687 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2688 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2690 sprintf (name, "%s", sym->name);
2691 sprintf (binding_label, "%s", sym->binding_label);
2693 /* Error check the call. */
2694 if (args->next != NULL)
2696 gfc_error_now ("More actual than formal arguments in '%s' "
2697 "call at %L", name, &(args->expr->where));
2698 retval = FAILURE;
2700 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2702 /* Make sure we have either the target or pointer attribute. */
2703 if (!arg_attr.target && !arg_attr.pointer)
2705 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2706 "a TARGET or an associated pointer",
2707 args_sym->name,
2708 sym->name, &(args->expr->where));
2709 retval = FAILURE;
2712 /* See if we have interoperable type and type param. */
2713 if (verify_c_interop (arg_ts) == SUCCESS
2714 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2716 if (args_sym->attr.target == 1)
2718 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2719 has the target attribute and is interoperable. */
2720 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2721 allocatable variable that has the TARGET attribute and
2722 is not an array of zero size. */
2723 if (args_sym->attr.allocatable == 1)
2725 if (args_sym->attr.dimension != 0
2726 && (args_sym->as && args_sym->as->rank == 0))
2728 gfc_error_now ("Allocatable variable '%s' used as a "
2729 "parameter to '%s' at %L must not be "
2730 "an array of zero size",
2731 args_sym->name, sym->name,
2732 &(args->expr->where));
2733 retval = FAILURE;
2736 else
2738 /* A non-allocatable target variable with C
2739 interoperable type and type parameters must be
2740 interoperable. */
2741 if (args_sym && args_sym->attr.dimension)
2743 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2745 gfc_error ("Assumed-shape array '%s' at %L "
2746 "cannot be an argument to the "
2747 "procedure '%s' because "
2748 "it is not C interoperable",
2749 args_sym->name,
2750 &(args->expr->where), sym->name);
2751 retval = FAILURE;
2753 else if (args_sym->as->type == AS_DEFERRED)
2755 gfc_error ("Deferred-shape array '%s' at %L "
2756 "cannot be an argument to the "
2757 "procedure '%s' because "
2758 "it is not C interoperable",
2759 args_sym->name,
2760 &(args->expr->where), sym->name);
2761 retval = FAILURE;
2765 /* Make sure it's not a character string. Arrays of
2766 any type should be ok if the variable is of a C
2767 interoperable type. */
2768 if (arg_ts->type == BT_CHARACTER)
2769 if (arg_ts->u.cl != NULL
2770 && (arg_ts->u.cl->length == NULL
2771 || arg_ts->u.cl->length->expr_type
2772 != EXPR_CONSTANT
2773 || mpz_cmp_si
2774 (arg_ts->u.cl->length->value.integer, 1)
2775 != 0)
2776 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2778 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2779 "at %L must have a length of 1",
2780 args_sym->name, sym->name,
2781 &(args->expr->where));
2782 retval = FAILURE;
2786 else if (arg_attr.pointer
2787 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2789 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2790 scalar pointer. */
2791 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2792 "associated scalar POINTER", args_sym->name,
2793 sym->name, &(args->expr->where));
2794 retval = FAILURE;
2797 else
2799 /* The parameter is not required to be C interoperable. If it
2800 is not C interoperable, it must be a nonpolymorphic scalar
2801 with no length type parameters. It still must have either
2802 the pointer or target attribute, and it can be
2803 allocatable (but must be allocated when c_loc is called). */
2804 if (args->expr->rank != 0
2805 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2807 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2808 "scalar", args_sym->name, sym->name,
2809 &(args->expr->where));
2810 retval = FAILURE;
2812 else if (arg_ts->type == BT_CHARACTER
2813 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2815 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2816 "%L must have a length of 1",
2817 args_sym->name, sym->name,
2818 &(args->expr->where));
2819 retval = FAILURE;
2821 else if (arg_ts->type == BT_CLASS)
2823 gfc_error_now ("Parameter '%s' to '%s' at %L must not be "
2824 "polymorphic", args_sym->name, sym->name,
2825 &(args->expr->where));
2826 retval = FAILURE;
2830 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2832 if (args_sym->attr.flavor != FL_PROCEDURE)
2834 /* TODO: Update this error message to allow for procedure
2835 pointers once they are implemented. */
2836 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2837 "procedure",
2838 args_sym->name, sym->name,
2839 &(args->expr->where));
2840 retval = FAILURE;
2842 else if (args_sym->attr.is_bind_c != 1)
2844 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2845 "BIND(C)",
2846 args_sym->name, sym->name,
2847 &(args->expr->where));
2848 retval = FAILURE;
2852 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2853 *new_sym = sym;
2855 else
2857 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2858 "iso_c_binding function: '%s'!\n", sym->name);
2861 return retval;
2865 /* Resolve a function call, which means resolving the arguments, then figuring
2866 out which entity the name refers to. */
2868 static gfc_try
2869 resolve_function (gfc_expr *expr)
2871 gfc_actual_arglist *arg;
2872 gfc_symbol *sym;
2873 const char *name;
2874 gfc_try t;
2875 int temp;
2876 procedure_type p = PROC_INTRINSIC;
2877 bool no_formal_args;
2879 sym = NULL;
2880 if (expr->symtree)
2881 sym = expr->symtree->n.sym;
2883 /* If this is a procedure pointer component, it has already been resolved. */
2884 if (gfc_is_proc_ptr_comp (expr, NULL))
2885 return SUCCESS;
2887 if (sym && sym->attr.intrinsic
2888 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2889 return FAILURE;
2891 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2893 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2894 return FAILURE;
2897 /* If this ia a deferred TBP with an abstract interface (which may
2898 of course be referenced), expr->value.function.esym will be set. */
2899 if (sym && sym->attr.abstract && !expr->value.function.esym)
2901 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2902 sym->name, &expr->where);
2903 return FAILURE;
2906 /* Switch off assumed size checking and do this again for certain kinds
2907 of procedure, once the procedure itself is resolved. */
2908 need_full_assumed_size++;
2910 if (expr->symtree && expr->symtree->n.sym)
2911 p = expr->symtree->n.sym->attr.proc;
2913 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2914 inquiry_argument = true;
2915 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2917 if (resolve_actual_arglist (expr->value.function.actual,
2918 p, no_formal_args) == FAILURE)
2920 inquiry_argument = false;
2921 return FAILURE;
2924 inquiry_argument = false;
2926 /* Need to setup the call to the correct c_associated, depending on
2927 the number of cptrs to user gives to compare. */
2928 if (sym && sym->attr.is_iso_c == 1)
2930 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2931 == FAILURE)
2932 return FAILURE;
2934 /* Get the symtree for the new symbol (resolved func).
2935 the old one will be freed later, when it's no longer used. */
2936 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2939 /* Resume assumed_size checking. */
2940 need_full_assumed_size--;
2942 /* If the procedure is external, check for usage. */
2943 if (sym && is_external_proc (sym))
2944 resolve_global_procedure (sym, &expr->where,
2945 &expr->value.function.actual, 0);
2947 if (sym && sym->ts.type == BT_CHARACTER
2948 && sym->ts.u.cl
2949 && sym->ts.u.cl->length == NULL
2950 && !sym->attr.dummy
2951 && expr->value.function.esym == NULL
2952 && !sym->attr.contained)
2954 /* Internal procedures are taken care of in resolve_contained_fntype. */
2955 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2956 "be used at %L since it is not a dummy argument",
2957 sym->name, &expr->where);
2958 return FAILURE;
2961 /* See if function is already resolved. */
2963 if (expr->value.function.name != NULL)
2965 if (expr->ts.type == BT_UNKNOWN)
2966 expr->ts = sym->ts;
2967 t = SUCCESS;
2969 else
2971 /* Apply the rules of section 14.1.2. */
2973 switch (procedure_kind (sym))
2975 case PTYPE_GENERIC:
2976 t = resolve_generic_f (expr);
2977 break;
2979 case PTYPE_SPECIFIC:
2980 t = resolve_specific_f (expr);
2981 break;
2983 case PTYPE_UNKNOWN:
2984 t = resolve_unknown_f (expr);
2985 break;
2987 default:
2988 gfc_internal_error ("resolve_function(): bad function type");
2992 /* If the expression is still a function (it might have simplified),
2993 then we check to see if we are calling an elemental function. */
2995 if (expr->expr_type != EXPR_FUNCTION)
2996 return t;
2998 temp = need_full_assumed_size;
2999 need_full_assumed_size = 0;
3001 if (resolve_elemental_actual (expr, NULL) == FAILURE)
3002 return FAILURE;
3004 if (omp_workshare_flag
3005 && expr->value.function.esym
3006 && ! gfc_elemental (expr->value.function.esym))
3008 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
3009 "in WORKSHARE construct", expr->value.function.esym->name,
3010 &expr->where);
3011 t = FAILURE;
3014 #define GENERIC_ID expr->value.function.isym->id
3015 else if (expr->value.function.actual != NULL
3016 && expr->value.function.isym != NULL
3017 && GENERIC_ID != GFC_ISYM_LBOUND
3018 && GENERIC_ID != GFC_ISYM_LEN
3019 && GENERIC_ID != GFC_ISYM_LOC
3020 && GENERIC_ID != GFC_ISYM_PRESENT)
3022 /* Array intrinsics must also have the last upper bound of an
3023 assumed size array argument. UBOUND and SIZE have to be
3024 excluded from the check if the second argument is anything
3025 than a constant. */
3027 for (arg = expr->value.function.actual; arg; arg = arg->next)
3029 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3030 && arg->next != NULL && arg->next->expr)
3032 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3033 break;
3035 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
3036 break;
3038 if ((int)mpz_get_si (arg->next->expr->value.integer)
3039 < arg->expr->rank)
3040 break;
3043 if (arg->expr != NULL
3044 && arg->expr->rank > 0
3045 && resolve_assumed_size_actual (arg->expr))
3046 return FAILURE;
3049 #undef GENERIC_ID
3051 need_full_assumed_size = temp;
3052 name = NULL;
3054 if (!pure_function (expr, &name) && name)
3056 if (forall_flag)
3058 gfc_error ("reference to non-PURE function '%s' at %L inside a "
3059 "FORALL %s", name, &expr->where,
3060 forall_flag == 2 ? "mask" : "block");
3061 t = FAILURE;
3063 else if (gfc_pure (NULL))
3065 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3066 "procedure within a PURE procedure", name, &expr->where);
3067 t = FAILURE;
3071 if (!pure_function (expr, &name) && name && gfc_implicit_pure (NULL))
3072 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3074 /* Functions without the RECURSIVE attribution are not allowed to
3075 * call themselves. */
3076 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3078 gfc_symbol *esym;
3079 esym = expr->value.function.esym;
3081 if (is_illegal_recursion (esym, gfc_current_ns))
3083 if (esym->attr.entry && esym->ns->entries)
3084 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3085 " function '%s' is not RECURSIVE",
3086 esym->name, &expr->where, esym->ns->entries->sym->name);
3087 else
3088 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3089 " is not RECURSIVE", esym->name, &expr->where);
3091 t = FAILURE;
3095 /* Character lengths of use associated functions may contains references to
3096 symbols not referenced from the current program unit otherwise. Make sure
3097 those symbols are marked as referenced. */
3099 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3100 && expr->value.function.esym->attr.use_assoc)
3102 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3105 /* Make sure that the expression has a typespec that works. */
3106 if (expr->ts.type == BT_UNKNOWN)
3108 if (expr->symtree->n.sym->result
3109 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3110 && !expr->symtree->n.sym->result->attr.proc_pointer)
3111 expr->ts = expr->symtree->n.sym->result->ts;
3114 return t;
3118 /************* Subroutine resolution *************/
3120 static void
3121 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3123 if (gfc_pure (sym))
3124 return;
3126 if (forall_flag)
3127 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3128 sym->name, &c->loc);
3129 else if (gfc_pure (NULL))
3130 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3131 &c->loc);
3135 static match
3136 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3138 gfc_symbol *s;
3140 if (sym->attr.generic)
3142 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3143 if (s != NULL)
3145 c->resolved_sym = s;
3146 pure_subroutine (c, s);
3147 return MATCH_YES;
3150 /* TODO: Need to search for elemental references in generic interface. */
3153 if (sym->attr.intrinsic)
3154 return gfc_intrinsic_sub_interface (c, 0);
3156 return MATCH_NO;
3160 static gfc_try
3161 resolve_generic_s (gfc_code *c)
3163 gfc_symbol *sym;
3164 match m;
3166 sym = c->symtree->n.sym;
3168 for (;;)
3170 m = resolve_generic_s0 (c, sym);
3171 if (m == MATCH_YES)
3172 return SUCCESS;
3173 else if (m == MATCH_ERROR)
3174 return FAILURE;
3176 generic:
3177 if (sym->ns->parent == NULL)
3178 break;
3179 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3181 if (sym == NULL)
3182 break;
3183 if (!generic_sym (sym))
3184 goto generic;
3187 /* Last ditch attempt. See if the reference is to an intrinsic
3188 that possesses a matching interface. 14.1.2.4 */
3189 sym = c->symtree->n.sym;
3191 if (!gfc_is_intrinsic (sym, 1, c->loc))
3193 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3194 sym->name, &c->loc);
3195 return FAILURE;
3198 m = gfc_intrinsic_sub_interface (c, 0);
3199 if (m == MATCH_YES)
3200 return SUCCESS;
3201 if (m == MATCH_NO)
3202 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3203 "intrinsic subroutine interface", sym->name, &c->loc);
3205 return FAILURE;
3209 /* Set the name and binding label of the subroutine symbol in the call
3210 expression represented by 'c' to include the type and kind of the
3211 second parameter. This function is for resolving the appropriate
3212 version of c_f_pointer() and c_f_procpointer(). For example, a
3213 call to c_f_pointer() for a default integer pointer could have a
3214 name of c_f_pointer_i4. If no second arg exists, which is an error
3215 for these two functions, it defaults to the generic symbol's name
3216 and binding label. */
3218 static void
3219 set_name_and_label (gfc_code *c, gfc_symbol *sym,
3220 char *name, char *binding_label)
3222 gfc_expr *arg = NULL;
3223 char type;
3224 int kind;
3226 /* The second arg of c_f_pointer and c_f_procpointer determines
3227 the type and kind for the procedure name. */
3228 arg = c->ext.actual->next->expr;
3230 if (arg != NULL)
3232 /* Set up the name to have the given symbol's name,
3233 plus the type and kind. */
3234 /* a derived type is marked with the type letter 'u' */
3235 if (arg->ts.type == BT_DERIVED)
3237 type = 'd';
3238 kind = 0; /* set the kind as 0 for now */
3240 else
3242 type = gfc_type_letter (arg->ts.type);
3243 kind = arg->ts.kind;
3246 if (arg->ts.type == BT_CHARACTER)
3247 /* Kind info for character strings not needed. */
3248 kind = 0;
3250 sprintf (name, "%s_%c%d", sym->name, type, kind);
3251 /* Set up the binding label as the given symbol's label plus
3252 the type and kind. */
3253 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
3255 else
3257 /* If the second arg is missing, set the name and label as
3258 was, cause it should at least be found, and the missing
3259 arg error will be caught by compare_parameters(). */
3260 sprintf (name, "%s", sym->name);
3261 sprintf (binding_label, "%s", sym->binding_label);
3264 return;
3268 /* Resolve a generic version of the iso_c_binding procedure given
3269 (sym) to the specific one based on the type and kind of the
3270 argument(s). Currently, this function resolves c_f_pointer() and
3271 c_f_procpointer based on the type and kind of the second argument
3272 (FPTR). Other iso_c_binding procedures aren't specially handled.
3273 Upon successfully exiting, c->resolved_sym will hold the resolved
3274 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
3275 otherwise. */
3277 match
3278 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
3280 gfc_symbol *new_sym;
3281 /* this is fine, since we know the names won't use the max */
3282 char name[GFC_MAX_SYMBOL_LEN + 1];
3283 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
3284 /* default to success; will override if find error */
3285 match m = MATCH_YES;
3287 /* Make sure the actual arguments are in the necessary order (based on the
3288 formal args) before resolving. */
3289 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
3291 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3292 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3294 set_name_and_label (c, sym, name, binding_label);
3296 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3298 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
3300 /* Make sure we got a third arg if the second arg has non-zero
3301 rank. We must also check that the type and rank are
3302 correct since we short-circuit this check in
3303 gfc_procedure_use() (called above to sort actual args). */
3304 if (c->ext.actual->next->expr->rank != 0)
3306 if(c->ext.actual->next->next == NULL
3307 || c->ext.actual->next->next->expr == NULL)
3309 m = MATCH_ERROR;
3310 gfc_error ("Missing SHAPE parameter for call to %s "
3311 "at %L", sym->name, &(c->loc));
3313 else if (c->ext.actual->next->next->expr->ts.type
3314 != BT_INTEGER
3315 || c->ext.actual->next->next->expr->rank != 1)
3317 m = MATCH_ERROR;
3318 gfc_error ("SHAPE parameter for call to %s at %L must "
3319 "be a rank 1 INTEGER array", sym->name,
3320 &(c->loc));
3326 if (m != MATCH_ERROR)
3328 /* the 1 means to add the optional arg to formal list */
3329 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
3331 /* for error reporting, say it's declared where the original was */
3332 new_sym->declared_at = sym->declared_at;
3335 else
3337 /* no differences for c_loc or c_funloc */
3338 new_sym = sym;
3341 /* set the resolved symbol */
3342 if (m != MATCH_ERROR)
3343 c->resolved_sym = new_sym;
3344 else
3345 c->resolved_sym = sym;
3347 return m;
3351 /* Resolve a subroutine call known to be specific. */
3353 static match
3354 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3356 match m;
3358 if(sym->attr.is_iso_c)
3360 m = gfc_iso_c_sub_interface (c,sym);
3361 return m;
3364 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3366 if (sym->attr.dummy)
3368 sym->attr.proc = PROC_DUMMY;
3369 goto found;
3372 sym->attr.proc = PROC_EXTERNAL;
3373 goto found;
3376 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3377 goto found;
3379 if (sym->attr.intrinsic)
3381 m = gfc_intrinsic_sub_interface (c, 1);
3382 if (m == MATCH_YES)
3383 return MATCH_YES;
3384 if (m == MATCH_NO)
3385 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3386 "with an intrinsic", sym->name, &c->loc);
3388 return MATCH_ERROR;
3391 return MATCH_NO;
3393 found:
3394 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3396 c->resolved_sym = sym;
3397 pure_subroutine (c, sym);
3399 return MATCH_YES;
3403 static gfc_try
3404 resolve_specific_s (gfc_code *c)
3406 gfc_symbol *sym;
3407 match m;
3409 sym = c->symtree->n.sym;
3411 for (;;)
3413 m = resolve_specific_s0 (c, sym);
3414 if (m == MATCH_YES)
3415 return SUCCESS;
3416 if (m == MATCH_ERROR)
3417 return FAILURE;
3419 if (sym->ns->parent == NULL)
3420 break;
3422 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3424 if (sym == NULL)
3425 break;
3428 sym = c->symtree->n.sym;
3429 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3430 sym->name, &c->loc);
3432 return FAILURE;
3436 /* Resolve a subroutine call not known to be generic nor specific. */
3438 static gfc_try
3439 resolve_unknown_s (gfc_code *c)
3441 gfc_symbol *sym;
3443 sym = c->symtree->n.sym;
3445 if (sym->attr.dummy)
3447 sym->attr.proc = PROC_DUMMY;
3448 goto found;
3451 /* See if we have an intrinsic function reference. */
3453 if (gfc_is_intrinsic (sym, 1, c->loc))
3455 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3456 return SUCCESS;
3457 return FAILURE;
3460 /* The reference is to an external name. */
3462 found:
3463 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3465 c->resolved_sym = sym;
3467 pure_subroutine (c, sym);
3469 return SUCCESS;
3473 /* Resolve a subroutine call. Although it was tempting to use the same code
3474 for functions, subroutines and functions are stored differently and this
3475 makes things awkward. */
3477 static gfc_try
3478 resolve_call (gfc_code *c)
3480 gfc_try t;
3481 procedure_type ptype = PROC_INTRINSIC;
3482 gfc_symbol *csym, *sym;
3483 bool no_formal_args;
3485 csym = c->symtree ? c->symtree->n.sym : NULL;
3487 if (csym && csym->ts.type != BT_UNKNOWN)
3489 gfc_error ("'%s' at %L has a type, which is not consistent with "
3490 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3491 return FAILURE;
3494 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3496 gfc_symtree *st;
3497 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3498 sym = st ? st->n.sym : NULL;
3499 if (sym && csym != sym
3500 && sym->ns == gfc_current_ns
3501 && sym->attr.flavor == FL_PROCEDURE
3502 && sym->attr.contained)
3504 sym->refs++;
3505 if (csym->attr.generic)
3506 c->symtree->n.sym = sym;
3507 else
3508 c->symtree = st;
3509 csym = c->symtree->n.sym;
3513 /* If this ia a deferred TBP with an abstract interface
3514 (which may of course be referenced), c->expr1 will be set. */
3515 if (csym && csym->attr.abstract && !c->expr1)
3517 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3518 csym->name, &c->loc);
3519 return FAILURE;
3522 /* Subroutines without the RECURSIVE attribution are not allowed to
3523 * call themselves. */
3524 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3526 if (csym->attr.entry && csym->ns->entries)
3527 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3528 " subroutine '%s' is not RECURSIVE",
3529 csym->name, &c->loc, csym->ns->entries->sym->name);
3530 else
3531 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3532 " is not RECURSIVE", csym->name, &c->loc);
3534 t = FAILURE;
3537 /* Switch off assumed size checking and do this again for certain kinds
3538 of procedure, once the procedure itself is resolved. */
3539 need_full_assumed_size++;
3541 if (csym)
3542 ptype = csym->attr.proc;
3544 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3545 if (resolve_actual_arglist (c->ext.actual, ptype,
3546 no_formal_args) == FAILURE)
3547 return FAILURE;
3549 /* Resume assumed_size checking. */
3550 need_full_assumed_size--;
3552 /* If external, check for usage. */
3553 if (csym && is_external_proc (csym))
3554 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3556 t = SUCCESS;
3557 if (c->resolved_sym == NULL)
3559 c->resolved_isym = NULL;
3560 switch (procedure_kind (csym))
3562 case PTYPE_GENERIC:
3563 t = resolve_generic_s (c);
3564 break;
3566 case PTYPE_SPECIFIC:
3567 t = resolve_specific_s (c);
3568 break;
3570 case PTYPE_UNKNOWN:
3571 t = resolve_unknown_s (c);
3572 break;
3574 default:
3575 gfc_internal_error ("resolve_subroutine(): bad function type");
3579 /* Some checks of elemental subroutine actual arguments. */
3580 if (resolve_elemental_actual (NULL, c) == FAILURE)
3581 return FAILURE;
3583 return t;
3587 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3588 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3589 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3590 if their shapes do not match. If either op1->shape or op2->shape is
3591 NULL, return SUCCESS. */
3593 static gfc_try
3594 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3596 gfc_try t;
3597 int i;
3599 t = SUCCESS;
3601 if (op1->shape != NULL && op2->shape != NULL)
3603 for (i = 0; i < op1->rank; i++)
3605 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3607 gfc_error ("Shapes for operands at %L and %L are not conformable",
3608 &op1->where, &op2->where);
3609 t = FAILURE;
3610 break;
3615 return t;
3619 /* Resolve an operator expression node. This can involve replacing the
3620 operation with a user defined function call. */
3622 static gfc_try
3623 resolve_operator (gfc_expr *e)
3625 gfc_expr *op1, *op2;
3626 char msg[200];
3627 bool dual_locus_error;
3628 gfc_try t;
3630 /* Resolve all subnodes-- give them types. */
3632 switch (e->value.op.op)
3634 default:
3635 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3636 return FAILURE;
3638 /* Fall through... */
3640 case INTRINSIC_NOT:
3641 case INTRINSIC_UPLUS:
3642 case INTRINSIC_UMINUS:
3643 case INTRINSIC_PARENTHESES:
3644 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3645 return FAILURE;
3646 break;
3649 /* Typecheck the new node. */
3651 op1 = e->value.op.op1;
3652 op2 = e->value.op.op2;
3653 dual_locus_error = false;
3655 if ((op1 && op1->expr_type == EXPR_NULL)
3656 || (op2 && op2->expr_type == EXPR_NULL))
3658 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3659 goto bad_op;
3662 switch (e->value.op.op)
3664 case INTRINSIC_UPLUS:
3665 case INTRINSIC_UMINUS:
3666 if (op1->ts.type == BT_INTEGER
3667 || op1->ts.type == BT_REAL
3668 || op1->ts.type == BT_COMPLEX)
3670 e->ts = op1->ts;
3671 break;
3674 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3675 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3676 goto bad_op;
3678 case INTRINSIC_PLUS:
3679 case INTRINSIC_MINUS:
3680 case INTRINSIC_TIMES:
3681 case INTRINSIC_DIVIDE:
3682 case INTRINSIC_POWER:
3683 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3685 gfc_type_convert_binary (e, 1);
3686 break;
3689 sprintf (msg,
3690 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3691 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3692 gfc_typename (&op2->ts));
3693 goto bad_op;
3695 case INTRINSIC_CONCAT:
3696 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3697 && op1->ts.kind == op2->ts.kind)
3699 e->ts.type = BT_CHARACTER;
3700 e->ts.kind = op1->ts.kind;
3701 break;
3704 sprintf (msg,
3705 _("Operands of string concatenation operator at %%L are %s/%s"),
3706 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3707 goto bad_op;
3709 case INTRINSIC_AND:
3710 case INTRINSIC_OR:
3711 case INTRINSIC_EQV:
3712 case INTRINSIC_NEQV:
3713 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3715 e->ts.type = BT_LOGICAL;
3716 e->ts.kind = gfc_kind_max (op1, op2);
3717 if (op1->ts.kind < e->ts.kind)
3718 gfc_convert_type (op1, &e->ts, 2);
3719 else if (op2->ts.kind < e->ts.kind)
3720 gfc_convert_type (op2, &e->ts, 2);
3721 break;
3724 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3725 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3726 gfc_typename (&op2->ts));
3728 goto bad_op;
3730 case INTRINSIC_NOT:
3731 if (op1->ts.type == BT_LOGICAL)
3733 e->ts.type = BT_LOGICAL;
3734 e->ts.kind = op1->ts.kind;
3735 break;
3738 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3739 gfc_typename (&op1->ts));
3740 goto bad_op;
3742 case INTRINSIC_GT:
3743 case INTRINSIC_GT_OS:
3744 case INTRINSIC_GE:
3745 case INTRINSIC_GE_OS:
3746 case INTRINSIC_LT:
3747 case INTRINSIC_LT_OS:
3748 case INTRINSIC_LE:
3749 case INTRINSIC_LE_OS:
3750 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3752 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3753 goto bad_op;
3756 /* Fall through... */
3758 case INTRINSIC_EQ:
3759 case INTRINSIC_EQ_OS:
3760 case INTRINSIC_NE:
3761 case INTRINSIC_NE_OS:
3762 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3763 && op1->ts.kind == op2->ts.kind)
3765 e->ts.type = BT_LOGICAL;
3766 e->ts.kind = gfc_default_logical_kind;
3767 break;
3770 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3772 gfc_type_convert_binary (e, 1);
3774 e->ts.type = BT_LOGICAL;
3775 e->ts.kind = gfc_default_logical_kind;
3776 break;
3779 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3780 sprintf (msg,
3781 _("Logicals at %%L must be compared with %s instead of %s"),
3782 (e->value.op.op == INTRINSIC_EQ
3783 || e->value.op.op == INTRINSIC_EQ_OS)
3784 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3785 else
3786 sprintf (msg,
3787 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3788 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3789 gfc_typename (&op2->ts));
3791 goto bad_op;
3793 case INTRINSIC_USER:
3794 if (e->value.op.uop->op == NULL)
3795 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3796 else if (op2 == NULL)
3797 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3798 e->value.op.uop->name, gfc_typename (&op1->ts));
3799 else
3801 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3802 e->value.op.uop->name, gfc_typename (&op1->ts),
3803 gfc_typename (&op2->ts));
3804 e->value.op.uop->op->sym->attr.referenced = 1;
3807 goto bad_op;
3809 case INTRINSIC_PARENTHESES:
3810 e->ts = op1->ts;
3811 if (e->ts.type == BT_CHARACTER)
3812 e->ts.u.cl = op1->ts.u.cl;
3813 break;
3815 default:
3816 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3819 /* Deal with arrayness of an operand through an operator. */
3821 t = SUCCESS;
3823 switch (e->value.op.op)
3825 case INTRINSIC_PLUS:
3826 case INTRINSIC_MINUS:
3827 case INTRINSIC_TIMES:
3828 case INTRINSIC_DIVIDE:
3829 case INTRINSIC_POWER:
3830 case INTRINSIC_CONCAT:
3831 case INTRINSIC_AND:
3832 case INTRINSIC_OR:
3833 case INTRINSIC_EQV:
3834 case INTRINSIC_NEQV:
3835 case INTRINSIC_EQ:
3836 case INTRINSIC_EQ_OS:
3837 case INTRINSIC_NE:
3838 case INTRINSIC_NE_OS:
3839 case INTRINSIC_GT:
3840 case INTRINSIC_GT_OS:
3841 case INTRINSIC_GE:
3842 case INTRINSIC_GE_OS:
3843 case INTRINSIC_LT:
3844 case INTRINSIC_LT_OS:
3845 case INTRINSIC_LE:
3846 case INTRINSIC_LE_OS:
3848 if (op1->rank == 0 && op2->rank == 0)
3849 e->rank = 0;
3851 if (op1->rank == 0 && op2->rank != 0)
3853 e->rank = op2->rank;
3855 if (e->shape == NULL)
3856 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3859 if (op1->rank != 0 && op2->rank == 0)
3861 e->rank = op1->rank;
3863 if (e->shape == NULL)
3864 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3867 if (op1->rank != 0 && op2->rank != 0)
3869 if (op1->rank == op2->rank)
3871 e->rank = op1->rank;
3872 if (e->shape == NULL)
3874 t = compare_shapes (op1, op2);
3875 if (t == FAILURE)
3876 e->shape = NULL;
3877 else
3878 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3881 else
3883 /* Allow higher level expressions to work. */
3884 e->rank = 0;
3886 /* Try user-defined operators, and otherwise throw an error. */
3887 dual_locus_error = true;
3888 sprintf (msg,
3889 _("Inconsistent ranks for operator at %%L and %%L"));
3890 goto bad_op;
3894 break;
3896 case INTRINSIC_PARENTHESES:
3897 case INTRINSIC_NOT:
3898 case INTRINSIC_UPLUS:
3899 case INTRINSIC_UMINUS:
3900 /* Simply copy arrayness attribute */
3901 e->rank = op1->rank;
3903 if (e->shape == NULL)
3904 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3906 break;
3908 default:
3909 break;
3912 /* Attempt to simplify the expression. */
3913 if (t == SUCCESS)
3915 t = gfc_simplify_expr (e, 0);
3916 /* Some calls do not succeed in simplification and return FAILURE
3917 even though there is no error; e.g. variable references to
3918 PARAMETER arrays. */
3919 if (!gfc_is_constant_expr (e))
3920 t = SUCCESS;
3922 return t;
3924 bad_op:
3927 bool real_error;
3928 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3929 return SUCCESS;
3931 if (real_error)
3932 return FAILURE;
3935 if (dual_locus_error)
3936 gfc_error (msg, &op1->where, &op2->where);
3937 else
3938 gfc_error (msg, &e->where);
3940 return FAILURE;
3944 /************** Array resolution subroutines **************/
3946 typedef enum
3947 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3948 comparison;
3950 /* Compare two integer expressions. */
3952 static comparison
3953 compare_bound (gfc_expr *a, gfc_expr *b)
3955 int i;
3957 if (a == NULL || a->expr_type != EXPR_CONSTANT
3958 || b == NULL || b->expr_type != EXPR_CONSTANT)
3959 return CMP_UNKNOWN;
3961 /* If either of the types isn't INTEGER, we must have
3962 raised an error earlier. */
3964 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3965 return CMP_UNKNOWN;
3967 i = mpz_cmp (a->value.integer, b->value.integer);
3969 if (i < 0)
3970 return CMP_LT;
3971 if (i > 0)
3972 return CMP_GT;
3973 return CMP_EQ;
3977 /* Compare an integer expression with an integer. */
3979 static comparison
3980 compare_bound_int (gfc_expr *a, int b)
3982 int i;
3984 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3985 return CMP_UNKNOWN;
3987 if (a->ts.type != BT_INTEGER)
3988 gfc_internal_error ("compare_bound_int(): Bad expression");
3990 i = mpz_cmp_si (a->value.integer, b);
3992 if (i < 0)
3993 return CMP_LT;
3994 if (i > 0)
3995 return CMP_GT;
3996 return CMP_EQ;
4000 /* Compare an integer expression with a mpz_t. */
4002 static comparison
4003 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4005 int i;
4007 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4008 return CMP_UNKNOWN;
4010 if (a->ts.type != BT_INTEGER)
4011 gfc_internal_error ("compare_bound_int(): Bad expression");
4013 i = mpz_cmp (a->value.integer, b);
4015 if (i < 0)
4016 return CMP_LT;
4017 if (i > 0)
4018 return CMP_GT;
4019 return CMP_EQ;
4023 /* Compute the last value of a sequence given by a triplet.
4024 Return 0 if it wasn't able to compute the last value, or if the
4025 sequence if empty, and 1 otherwise. */
4027 static int
4028 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4029 gfc_expr *stride, mpz_t last)
4031 mpz_t rem;
4033 if (start == NULL || start->expr_type != EXPR_CONSTANT
4034 || end == NULL || end->expr_type != EXPR_CONSTANT
4035 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4036 return 0;
4038 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4039 || (stride != NULL && stride->ts.type != BT_INTEGER))
4040 return 0;
4042 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
4044 if (compare_bound (start, end) == CMP_GT)
4045 return 0;
4046 mpz_set (last, end->value.integer);
4047 return 1;
4050 if (compare_bound_int (stride, 0) == CMP_GT)
4052 /* Stride is positive */
4053 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4054 return 0;
4056 else
4058 /* Stride is negative */
4059 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4060 return 0;
4063 mpz_init (rem);
4064 mpz_sub (rem, end->value.integer, start->value.integer);
4065 mpz_tdiv_r (rem, rem, stride->value.integer);
4066 mpz_sub (last, end->value.integer, rem);
4067 mpz_clear (rem);
4069 return 1;
4073 /* Compare a single dimension of an array reference to the array
4074 specification. */
4076 static gfc_try
4077 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4079 mpz_t last_value;
4081 if (ar->dimen_type[i] == DIMEN_STAR)
4083 gcc_assert (ar->stride[i] == NULL);
4084 /* This implies [*] as [*:] and [*:3] are not possible. */
4085 if (ar->start[i] == NULL)
4087 gcc_assert (ar->end[i] == NULL);
4088 return SUCCESS;
4092 /* Given start, end and stride values, calculate the minimum and
4093 maximum referenced indexes. */
4095 switch (ar->dimen_type[i])
4097 case DIMEN_VECTOR:
4098 break;
4100 case DIMEN_STAR:
4101 case DIMEN_ELEMENT:
4102 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4104 if (i < as->rank)
4105 gfc_warning ("Array reference at %L is out of bounds "
4106 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4107 mpz_get_si (ar->start[i]->value.integer),
4108 mpz_get_si (as->lower[i]->value.integer), i+1);
4109 else
4110 gfc_warning ("Array reference at %L is out of bounds "
4111 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4112 mpz_get_si (ar->start[i]->value.integer),
4113 mpz_get_si (as->lower[i]->value.integer),
4114 i + 1 - as->rank);
4115 return SUCCESS;
4117 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4119 if (i < as->rank)
4120 gfc_warning ("Array reference at %L is out of bounds "
4121 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4122 mpz_get_si (ar->start[i]->value.integer),
4123 mpz_get_si (as->upper[i]->value.integer), i+1);
4124 else
4125 gfc_warning ("Array reference at %L is out of bounds "
4126 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4127 mpz_get_si (ar->start[i]->value.integer),
4128 mpz_get_si (as->upper[i]->value.integer),
4129 i + 1 - as->rank);
4130 return SUCCESS;
4133 break;
4135 case DIMEN_RANGE:
4137 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4138 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4140 comparison comp_start_end = compare_bound (AR_START, AR_END);
4142 /* Check for zero stride, which is not allowed. */
4143 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4145 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4146 return FAILURE;
4149 /* if start == len || (stride > 0 && start < len)
4150 || (stride < 0 && start > len),
4151 then the array section contains at least one element. In this
4152 case, there is an out-of-bounds access if
4153 (start < lower || start > upper). */
4154 if (compare_bound (AR_START, AR_END) == CMP_EQ
4155 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4156 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4157 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4158 && comp_start_end == CMP_GT))
4160 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
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->lower[i]->value.integer), i+1);
4166 return SUCCESS;
4168 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4170 gfc_warning ("Lower array reference at %L is out of bounds "
4171 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4172 mpz_get_si (AR_START->value.integer),
4173 mpz_get_si (as->upper[i]->value.integer), i+1);
4174 return SUCCESS;
4178 /* If we can compute the highest index of the array section,
4179 then it also has to be between lower and upper. */
4180 mpz_init (last_value);
4181 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4182 last_value))
4184 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4186 gfc_warning ("Upper array reference at %L is out of bounds "
4187 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4188 mpz_get_si (last_value),
4189 mpz_get_si (as->lower[i]->value.integer), i+1);
4190 mpz_clear (last_value);
4191 return SUCCESS;
4193 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4195 gfc_warning ("Upper array reference at %L is out of bounds "
4196 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4197 mpz_get_si (last_value),
4198 mpz_get_si (as->upper[i]->value.integer), i+1);
4199 mpz_clear (last_value);
4200 return SUCCESS;
4203 mpz_clear (last_value);
4205 #undef AR_START
4206 #undef AR_END
4208 break;
4210 default:
4211 gfc_internal_error ("check_dimension(): Bad array reference");
4214 return SUCCESS;
4218 /* Compare an array reference with an array specification. */
4220 static gfc_try
4221 compare_spec_to_ref (gfc_array_ref *ar)
4223 gfc_array_spec *as;
4224 int i;
4226 as = ar->as;
4227 i = as->rank - 1;
4228 /* TODO: Full array sections are only allowed as actual parameters. */
4229 if (as->type == AS_ASSUMED_SIZE
4230 && (/*ar->type == AR_FULL
4231 ||*/ (ar->type == AR_SECTION
4232 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4234 gfc_error ("Rightmost upper bound of assumed size array section "
4235 "not specified at %L", &ar->where);
4236 return FAILURE;
4239 if (ar->type == AR_FULL)
4240 return SUCCESS;
4242 if (as->rank != ar->dimen)
4244 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4245 &ar->where, ar->dimen, as->rank);
4246 return FAILURE;
4249 /* ar->codimen == 0 is a local array. */
4250 if (as->corank != ar->codimen && ar->codimen != 0)
4252 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4253 &ar->where, ar->codimen, as->corank);
4254 return FAILURE;
4257 for (i = 0; i < as->rank; i++)
4258 if (check_dimension (i, ar, as) == FAILURE)
4259 return FAILURE;
4261 /* Local access has no coarray spec. */
4262 if (ar->codimen != 0)
4263 for (i = as->rank; i < as->rank + as->corank; i++)
4265 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate)
4267 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4268 i + 1 - as->rank, &ar->where);
4269 return FAILURE;
4271 if (check_dimension (i, ar, as) == FAILURE)
4272 return FAILURE;
4275 return SUCCESS;
4279 /* Resolve one part of an array index. */
4281 static gfc_try
4282 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4283 int force_index_integer_kind)
4285 gfc_typespec ts;
4287 if (index == NULL)
4288 return SUCCESS;
4290 if (gfc_resolve_expr (index) == FAILURE)
4291 return FAILURE;
4293 if (check_scalar && index->rank != 0)
4295 gfc_error ("Array index at %L must be scalar", &index->where);
4296 return FAILURE;
4299 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4301 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4302 &index->where, gfc_basic_typename (index->ts.type));
4303 return FAILURE;
4306 if (index->ts.type == BT_REAL)
4307 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
4308 &index->where) == FAILURE)
4309 return FAILURE;
4311 if ((index->ts.kind != gfc_index_integer_kind
4312 && force_index_integer_kind)
4313 || index->ts.type != BT_INTEGER)
4315 gfc_clear_ts (&ts);
4316 ts.type = BT_INTEGER;
4317 ts.kind = gfc_index_integer_kind;
4319 gfc_convert_type_warn (index, &ts, 2, 0);
4322 return SUCCESS;
4325 /* Resolve one part of an array index. */
4327 gfc_try
4328 gfc_resolve_index (gfc_expr *index, int check_scalar)
4330 return gfc_resolve_index_1 (index, check_scalar, 1);
4333 /* Resolve a dim argument to an intrinsic function. */
4335 gfc_try
4336 gfc_resolve_dim_arg (gfc_expr *dim)
4338 if (dim == NULL)
4339 return SUCCESS;
4341 if (gfc_resolve_expr (dim) == FAILURE)
4342 return FAILURE;
4344 if (dim->rank != 0)
4346 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4347 return FAILURE;
4351 if (dim->ts.type != BT_INTEGER)
4353 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4354 return FAILURE;
4357 if (dim->ts.kind != gfc_index_integer_kind)
4359 gfc_typespec ts;
4361 gfc_clear_ts (&ts);
4362 ts.type = BT_INTEGER;
4363 ts.kind = gfc_index_integer_kind;
4365 gfc_convert_type_warn (dim, &ts, 2, 0);
4368 return SUCCESS;
4371 /* Given an expression that contains array references, update those array
4372 references to point to the right array specifications. While this is
4373 filled in during matching, this information is difficult to save and load
4374 in a module, so we take care of it here.
4376 The idea here is that the original array reference comes from the
4377 base symbol. We traverse the list of reference structures, setting
4378 the stored reference to references. Component references can
4379 provide an additional array specification. */
4381 static void
4382 find_array_spec (gfc_expr *e)
4384 gfc_array_spec *as;
4385 gfc_component *c;
4386 gfc_symbol *derived;
4387 gfc_ref *ref;
4389 if (e->symtree->n.sym->ts.type == BT_CLASS)
4390 as = CLASS_DATA (e->symtree->n.sym)->as;
4391 else
4392 as = e->symtree->n.sym->as;
4393 derived = NULL;
4395 for (ref = e->ref; ref; ref = ref->next)
4396 switch (ref->type)
4398 case REF_ARRAY:
4399 if (as == NULL)
4400 gfc_internal_error ("find_array_spec(): Missing spec");
4402 ref->u.ar.as = as;
4403 as = NULL;
4404 break;
4406 case REF_COMPONENT:
4407 if (derived == NULL)
4408 derived = e->symtree->n.sym->ts.u.derived;
4410 if (derived->attr.is_class)
4411 derived = derived->components->ts.u.derived;
4413 c = derived->components;
4415 for (; c; c = c->next)
4416 if (c == ref->u.c.component)
4418 /* Track the sequence of component references. */
4419 if (c->ts.type == BT_DERIVED)
4420 derived = c->ts.u.derived;
4421 break;
4424 if (c == NULL)
4425 gfc_internal_error ("find_array_spec(): Component not found");
4427 if (c->attr.dimension)
4429 if (as != NULL)
4430 gfc_internal_error ("find_array_spec(): unused as(1)");
4431 as = c->as;
4434 break;
4436 case REF_SUBSTRING:
4437 break;
4440 if (as != NULL)
4441 gfc_internal_error ("find_array_spec(): unused as(2)");
4445 /* Resolve an array reference. */
4447 static gfc_try
4448 resolve_array_ref (gfc_array_ref *ar)
4450 int i, check_scalar;
4451 gfc_expr *e;
4453 for (i = 0; i < ar->dimen + ar->codimen; i++)
4455 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4457 /* Do not force gfc_index_integer_kind for the start. We can
4458 do fine with any integer kind. This avoids temporary arrays
4459 created for indexing with a vector. */
4460 if (gfc_resolve_index_1 (ar->start[i], check_scalar, 0) == FAILURE)
4461 return FAILURE;
4462 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4463 return FAILURE;
4464 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4465 return FAILURE;
4467 e = ar->start[i];
4469 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4470 switch (e->rank)
4472 case 0:
4473 ar->dimen_type[i] = DIMEN_ELEMENT;
4474 break;
4476 case 1:
4477 ar->dimen_type[i] = DIMEN_VECTOR;
4478 if (e->expr_type == EXPR_VARIABLE
4479 && e->symtree->n.sym->ts.type == BT_DERIVED)
4480 ar->start[i] = gfc_get_parentheses (e);
4481 break;
4483 default:
4484 gfc_error ("Array index at %L is an array of rank %d",
4485 &ar->c_where[i], e->rank);
4486 return FAILURE;
4489 /* Fill in the upper bound, which may be lower than the
4490 specified one for something like a(2:10:5), which is
4491 identical to a(2:7:5). Only relevant for strides not equal
4492 to one. */
4493 if (ar->dimen_type[i] == DIMEN_RANGE
4494 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4495 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0)
4497 mpz_t size, end;
4499 if (gfc_ref_dimen_size (ar, i, &size, &end) == SUCCESS)
4501 if (ar->end[i] == NULL)
4503 ar->end[i] =
4504 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4505 &ar->where);
4506 mpz_set (ar->end[i]->value.integer, end);
4508 else if (ar->end[i]->ts.type == BT_INTEGER
4509 && ar->end[i]->expr_type == EXPR_CONSTANT)
4511 mpz_set (ar->end[i]->value.integer, end);
4513 else
4514 gcc_unreachable ();
4516 mpz_clear (size);
4517 mpz_clear (end);
4522 if (ar->type == AR_FULL && ar->as->rank == 0)
4523 ar->type = AR_ELEMENT;
4525 /* If the reference type is unknown, figure out what kind it is. */
4527 if (ar->type == AR_UNKNOWN)
4529 ar->type = AR_ELEMENT;
4530 for (i = 0; i < ar->dimen; i++)
4531 if (ar->dimen_type[i] == DIMEN_RANGE
4532 || ar->dimen_type[i] == DIMEN_VECTOR)
4534 ar->type = AR_SECTION;
4535 break;
4539 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4540 return FAILURE;
4542 return SUCCESS;
4546 static gfc_try
4547 resolve_substring (gfc_ref *ref)
4549 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4551 if (ref->u.ss.start != NULL)
4553 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4554 return FAILURE;
4556 if (ref->u.ss.start->ts.type != BT_INTEGER)
4558 gfc_error ("Substring start index at %L must be of type INTEGER",
4559 &ref->u.ss.start->where);
4560 return FAILURE;
4563 if (ref->u.ss.start->rank != 0)
4565 gfc_error ("Substring start index at %L must be scalar",
4566 &ref->u.ss.start->where);
4567 return FAILURE;
4570 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4571 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4572 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4574 gfc_error ("Substring start index at %L is less than one",
4575 &ref->u.ss.start->where);
4576 return FAILURE;
4580 if (ref->u.ss.end != NULL)
4582 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4583 return FAILURE;
4585 if (ref->u.ss.end->ts.type != BT_INTEGER)
4587 gfc_error ("Substring end index at %L must be of type INTEGER",
4588 &ref->u.ss.end->where);
4589 return FAILURE;
4592 if (ref->u.ss.end->rank != 0)
4594 gfc_error ("Substring end index at %L must be scalar",
4595 &ref->u.ss.end->where);
4596 return FAILURE;
4599 if (ref->u.ss.length != NULL
4600 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4601 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4602 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4604 gfc_error ("Substring end index at %L exceeds the string length",
4605 &ref->u.ss.start->where);
4606 return FAILURE;
4609 if (compare_bound_mpz_t (ref->u.ss.end,
4610 gfc_integer_kinds[k].huge) == CMP_GT
4611 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4612 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4614 gfc_error ("Substring end index at %L is too large",
4615 &ref->u.ss.end->where);
4616 return FAILURE;
4620 return SUCCESS;
4624 /* This function supplies missing substring charlens. */
4626 void
4627 gfc_resolve_substring_charlen (gfc_expr *e)
4629 gfc_ref *char_ref;
4630 gfc_expr *start, *end;
4632 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4633 if (char_ref->type == REF_SUBSTRING)
4634 break;
4636 if (!char_ref)
4637 return;
4639 gcc_assert (char_ref->next == NULL);
4641 if (e->ts.u.cl)
4643 if (e->ts.u.cl->length)
4644 gfc_free_expr (e->ts.u.cl->length);
4645 else if (e->expr_type == EXPR_VARIABLE
4646 && e->symtree->n.sym->attr.dummy)
4647 return;
4650 e->ts.type = BT_CHARACTER;
4651 e->ts.kind = gfc_default_character_kind;
4653 if (!e->ts.u.cl)
4654 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4656 if (char_ref->u.ss.start)
4657 start = gfc_copy_expr (char_ref->u.ss.start);
4658 else
4659 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4661 if (char_ref->u.ss.end)
4662 end = gfc_copy_expr (char_ref->u.ss.end);
4663 else if (e->expr_type == EXPR_VARIABLE)
4664 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4665 else
4666 end = NULL;
4668 if (!start || !end)
4669 return;
4671 /* Length = (end - start +1). */
4672 e->ts.u.cl->length = gfc_subtract (end, start);
4673 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4674 gfc_get_int_expr (gfc_default_integer_kind,
4675 NULL, 1));
4677 e->ts.u.cl->length->ts.type = BT_INTEGER;
4678 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4680 /* Make sure that the length is simplified. */
4681 gfc_simplify_expr (e->ts.u.cl->length, 1);
4682 gfc_resolve_expr (e->ts.u.cl->length);
4686 /* Resolve subtype references. */
4688 static gfc_try
4689 resolve_ref (gfc_expr *expr)
4691 int current_part_dimension, n_components, seen_part_dimension;
4692 gfc_ref *ref;
4694 for (ref = expr->ref; ref; ref = ref->next)
4695 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4697 find_array_spec (expr);
4698 break;
4701 for (ref = expr->ref; ref; ref = ref->next)
4702 switch (ref->type)
4704 case REF_ARRAY:
4705 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4706 return FAILURE;
4707 break;
4709 case REF_COMPONENT:
4710 break;
4712 case REF_SUBSTRING:
4713 resolve_substring (ref);
4714 break;
4717 /* Check constraints on part references. */
4719 current_part_dimension = 0;
4720 seen_part_dimension = 0;
4721 n_components = 0;
4723 for (ref = expr->ref; ref; ref = ref->next)
4725 switch (ref->type)
4727 case REF_ARRAY:
4728 switch (ref->u.ar.type)
4730 case AR_FULL:
4731 /* Coarray scalar. */
4732 if (ref->u.ar.as->rank == 0)
4734 current_part_dimension = 0;
4735 break;
4737 /* Fall through. */
4738 case AR_SECTION:
4739 current_part_dimension = 1;
4740 break;
4742 case AR_ELEMENT:
4743 current_part_dimension = 0;
4744 break;
4746 case AR_UNKNOWN:
4747 gfc_internal_error ("resolve_ref(): Bad array reference");
4750 break;
4752 case REF_COMPONENT:
4753 if (current_part_dimension || seen_part_dimension)
4755 /* F03:C614. */
4756 if (ref->u.c.component->attr.pointer
4757 || ref->u.c.component->attr.proc_pointer)
4759 gfc_error ("Component to the right of a part reference "
4760 "with nonzero rank must not have the POINTER "
4761 "attribute at %L", &expr->where);
4762 return FAILURE;
4764 else if (ref->u.c.component->attr.allocatable)
4766 gfc_error ("Component to the right of a part reference "
4767 "with nonzero rank must not have the ALLOCATABLE "
4768 "attribute at %L", &expr->where);
4769 return FAILURE;
4773 n_components++;
4774 break;
4776 case REF_SUBSTRING:
4777 break;
4780 if (((ref->type == REF_COMPONENT && n_components > 1)
4781 || ref->next == NULL)
4782 && current_part_dimension
4783 && seen_part_dimension)
4785 gfc_error ("Two or more part references with nonzero rank must "
4786 "not be specified at %L", &expr->where);
4787 return FAILURE;
4790 if (ref->type == REF_COMPONENT)
4792 if (current_part_dimension)
4793 seen_part_dimension = 1;
4795 /* reset to make sure */
4796 current_part_dimension = 0;
4800 return SUCCESS;
4804 /* Given an expression, determine its shape. This is easier than it sounds.
4805 Leaves the shape array NULL if it is not possible to determine the shape. */
4807 static void
4808 expression_shape (gfc_expr *e)
4810 mpz_t array[GFC_MAX_DIMENSIONS];
4811 int i;
4813 if (e->rank == 0 || e->shape != NULL)
4814 return;
4816 for (i = 0; i < e->rank; i++)
4817 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4818 goto fail;
4820 e->shape = gfc_get_shape (e->rank);
4822 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4824 return;
4826 fail:
4827 for (i--; i >= 0; i--)
4828 mpz_clear (array[i]);
4832 /* Given a variable expression node, compute the rank of the expression by
4833 examining the base symbol and any reference structures it may have. */
4835 static void
4836 expression_rank (gfc_expr *e)
4838 gfc_ref *ref;
4839 int i, rank;
4841 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4842 could lead to serious confusion... */
4843 gcc_assert (e->expr_type != EXPR_COMPCALL);
4845 if (e->ref == NULL)
4847 if (e->expr_type == EXPR_ARRAY)
4848 goto done;
4849 /* Constructors can have a rank different from one via RESHAPE(). */
4851 if (e->symtree == NULL)
4853 e->rank = 0;
4854 goto done;
4857 e->rank = (e->symtree->n.sym->as == NULL)
4858 ? 0 : e->symtree->n.sym->as->rank;
4859 goto done;
4862 rank = 0;
4864 for (ref = e->ref; ref; ref = ref->next)
4866 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4867 && ref->u.c.component->attr.function && !ref->next)
4868 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4870 if (ref->type != REF_ARRAY)
4871 continue;
4873 if (ref->u.ar.type == AR_FULL)
4875 rank = ref->u.ar.as->rank;
4876 break;
4879 if (ref->u.ar.type == AR_SECTION)
4881 /* Figure out the rank of the section. */
4882 if (rank != 0)
4883 gfc_internal_error ("expression_rank(): Two array specs");
4885 for (i = 0; i < ref->u.ar.dimen; i++)
4886 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4887 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4888 rank++;
4890 break;
4894 e->rank = rank;
4896 done:
4897 expression_shape (e);
4901 /* Resolve a variable expression. */
4903 static gfc_try
4904 resolve_variable (gfc_expr *e)
4906 gfc_symbol *sym;
4907 gfc_try t;
4909 t = SUCCESS;
4911 if (e->symtree == NULL)
4912 return FAILURE;
4913 sym = e->symtree->n.sym;
4915 /* If this is an associate-name, it may be parsed with an array reference
4916 in error even though the target is scalar. Fail directly in this case. */
4917 if (sym->assoc && !sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
4918 return FAILURE;
4920 /* On the other hand, the parser may not have known this is an array;
4921 in this case, we have to add a FULL reference. */
4922 if (sym->assoc && sym->attr.dimension && !e->ref)
4924 e->ref = gfc_get_ref ();
4925 e->ref->type = REF_ARRAY;
4926 e->ref->u.ar.type = AR_FULL;
4927 e->ref->u.ar.dimen = 0;
4930 if (e->ref && resolve_ref (e) == FAILURE)
4931 return FAILURE;
4933 if (sym->attr.flavor == FL_PROCEDURE
4934 && (!sym->attr.function
4935 || (sym->attr.function && sym->result
4936 && sym->result->attr.proc_pointer
4937 && !sym->result->attr.function)))
4939 e->ts.type = BT_PROCEDURE;
4940 goto resolve_procedure;
4943 if (sym->ts.type != BT_UNKNOWN)
4944 gfc_variable_attr (e, &e->ts);
4945 else
4947 /* Must be a simple variable reference. */
4948 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4949 return FAILURE;
4950 e->ts = sym->ts;
4953 if (check_assumed_size_reference (sym, e))
4954 return FAILURE;
4956 /* Deal with forward references to entries during resolve_code, to
4957 satisfy, at least partially, 12.5.2.5. */
4958 if (gfc_current_ns->entries
4959 && current_entry_id == sym->entry_id
4960 && cs_base
4961 && cs_base->current
4962 && cs_base->current->op != EXEC_ENTRY)
4964 gfc_entry_list *entry;
4965 gfc_formal_arglist *formal;
4966 int n;
4967 bool seen;
4969 /* If the symbol is a dummy... */
4970 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4972 entry = gfc_current_ns->entries;
4973 seen = false;
4975 /* ...test if the symbol is a parameter of previous entries. */
4976 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4977 for (formal = entry->sym->formal; formal; formal = formal->next)
4979 if (formal->sym && sym->name == formal->sym->name)
4980 seen = true;
4983 /* If it has not been seen as a dummy, this is an error. */
4984 if (!seen)
4986 if (specification_expr)
4987 gfc_error ("Variable '%s', used in a specification expression"
4988 ", is referenced at %L before the ENTRY statement "
4989 "in which it is a parameter",
4990 sym->name, &cs_base->current->loc);
4991 else
4992 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4993 "statement in which it is a parameter",
4994 sym->name, &cs_base->current->loc);
4995 t = FAILURE;
4999 /* Now do the same check on the specification expressions. */
5000 specification_expr = 1;
5001 if (sym->ts.type == BT_CHARACTER
5002 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
5003 t = FAILURE;
5005 if (sym->as)
5006 for (n = 0; n < sym->as->rank; n++)
5008 specification_expr = 1;
5009 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
5010 t = FAILURE;
5011 specification_expr = 1;
5012 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
5013 t = FAILURE;
5015 specification_expr = 0;
5017 if (t == SUCCESS)
5018 /* Update the symbol's entry level. */
5019 sym->entry_id = current_entry_id + 1;
5022 /* If a symbol has been host_associated mark it. This is used latter,
5023 to identify if aliasing is possible via host association. */
5024 if (sym->attr.flavor == FL_VARIABLE
5025 && gfc_current_ns->parent
5026 && (gfc_current_ns->parent == sym->ns
5027 || (gfc_current_ns->parent->parent
5028 && gfc_current_ns->parent->parent == sym->ns)))
5029 sym->attr.host_assoc = 1;
5031 resolve_procedure:
5032 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
5033 t = FAILURE;
5035 /* F2008, C617 and C1229. */
5036 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5037 && gfc_is_coindexed (e))
5039 gfc_ref *ref, *ref2 = NULL;
5041 for (ref = e->ref; ref; ref = ref->next)
5043 if (ref->type == REF_COMPONENT)
5044 ref2 = ref;
5045 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5046 break;
5049 for ( ; ref; ref = ref->next)
5050 if (ref->type == REF_COMPONENT)
5051 break;
5053 /* Expression itself is not coindexed object. */
5054 if (ref && e->ts.type == BT_CLASS)
5056 gfc_error ("Polymorphic subobject of coindexed object at %L",
5057 &e->where);
5058 t = FAILURE;
5061 /* Expression itself is coindexed object. */
5062 if (ref == NULL)
5064 gfc_component *c;
5065 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5066 for ( ; c; c = c->next)
5067 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5069 gfc_error ("Coindexed object with polymorphic allocatable "
5070 "subcomponent at %L", &e->where);
5071 t = FAILURE;
5072 break;
5077 return t;
5081 /* Checks to see that the correct symbol has been host associated.
5082 The only situation where this arises is that in which a twice
5083 contained function is parsed after the host association is made.
5084 Therefore, on detecting this, change the symbol in the expression
5085 and convert the array reference into an actual arglist if the old
5086 symbol is a variable. */
5087 static bool
5088 check_host_association (gfc_expr *e)
5090 gfc_symbol *sym, *old_sym;
5091 gfc_symtree *st;
5092 int n;
5093 gfc_ref *ref;
5094 gfc_actual_arglist *arg, *tail = NULL;
5095 bool retval = e->expr_type == EXPR_FUNCTION;
5097 /* If the expression is the result of substitution in
5098 interface.c(gfc_extend_expr) because there is no way in
5099 which the host association can be wrong. */
5100 if (e->symtree == NULL
5101 || e->symtree->n.sym == NULL
5102 || e->user_operator)
5103 return retval;
5105 old_sym = e->symtree->n.sym;
5107 if (gfc_current_ns->parent
5108 && old_sym->ns != gfc_current_ns)
5110 /* Use the 'USE' name so that renamed module symbols are
5111 correctly handled. */
5112 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5114 if (sym && old_sym != sym
5115 && sym->ts.type == old_sym->ts.type
5116 && sym->attr.flavor == FL_PROCEDURE
5117 && sym->attr.contained)
5119 /* Clear the shape, since it might not be valid. */
5120 if (e->shape != NULL)
5122 for (n = 0; n < e->rank; n++)
5123 mpz_clear (e->shape[n]);
5125 gfc_free (e->shape);
5128 /* Give the expression the right symtree! */
5129 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5130 gcc_assert (st != NULL);
5132 if (old_sym->attr.flavor == FL_PROCEDURE
5133 || e->expr_type == EXPR_FUNCTION)
5135 /* Original was function so point to the new symbol, since
5136 the actual argument list is already attached to the
5137 expression. */
5138 e->value.function.esym = NULL;
5139 e->symtree = st;
5141 else
5143 /* Original was variable so convert array references into
5144 an actual arglist. This does not need any checking now
5145 since gfc_resolve_function will take care of it. */
5146 e->value.function.actual = NULL;
5147 e->expr_type = EXPR_FUNCTION;
5148 e->symtree = st;
5150 /* Ambiguity will not arise if the array reference is not
5151 the last reference. */
5152 for (ref = e->ref; ref; ref = ref->next)
5153 if (ref->type == REF_ARRAY && ref->next == NULL)
5154 break;
5156 gcc_assert (ref->type == REF_ARRAY);
5158 /* Grab the start expressions from the array ref and
5159 copy them into actual arguments. */
5160 for (n = 0; n < ref->u.ar.dimen; n++)
5162 arg = gfc_get_actual_arglist ();
5163 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5164 if (e->value.function.actual == NULL)
5165 tail = e->value.function.actual = arg;
5166 else
5168 tail->next = arg;
5169 tail = arg;
5173 /* Dump the reference list and set the rank. */
5174 gfc_free_ref_list (e->ref);
5175 e->ref = NULL;
5176 e->rank = sym->as ? sym->as->rank : 0;
5179 gfc_resolve_expr (e);
5180 sym->refs++;
5183 /* This might have changed! */
5184 return e->expr_type == EXPR_FUNCTION;
5188 static void
5189 gfc_resolve_character_operator (gfc_expr *e)
5191 gfc_expr *op1 = e->value.op.op1;
5192 gfc_expr *op2 = e->value.op.op2;
5193 gfc_expr *e1 = NULL;
5194 gfc_expr *e2 = NULL;
5196 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5198 if (op1->ts.u.cl && op1->ts.u.cl->length)
5199 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5200 else if (op1->expr_type == EXPR_CONSTANT)
5201 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5202 op1->value.character.length);
5204 if (op2->ts.u.cl && op2->ts.u.cl->length)
5205 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5206 else if (op2->expr_type == EXPR_CONSTANT)
5207 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5208 op2->value.character.length);
5210 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5212 if (!e1 || !e2)
5213 return;
5215 e->ts.u.cl->length = gfc_add (e1, e2);
5216 e->ts.u.cl->length->ts.type = BT_INTEGER;
5217 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5218 gfc_simplify_expr (e->ts.u.cl->length, 0);
5219 gfc_resolve_expr (e->ts.u.cl->length);
5221 return;
5225 /* Ensure that an character expression has a charlen and, if possible, a
5226 length expression. */
5228 static void
5229 fixup_charlen (gfc_expr *e)
5231 /* The cases fall through so that changes in expression type and the need
5232 for multiple fixes are picked up. In all circumstances, a charlen should
5233 be available for the middle end to hang a backend_decl on. */
5234 switch (e->expr_type)
5236 case EXPR_OP:
5237 gfc_resolve_character_operator (e);
5239 case EXPR_ARRAY:
5240 if (e->expr_type == EXPR_ARRAY)
5241 gfc_resolve_character_array_constructor (e);
5243 case EXPR_SUBSTRING:
5244 if (!e->ts.u.cl && e->ref)
5245 gfc_resolve_substring_charlen (e);
5247 default:
5248 if (!e->ts.u.cl)
5249 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5251 break;
5256 /* Update an actual argument to include the passed-object for type-bound
5257 procedures at the right position. */
5259 static gfc_actual_arglist*
5260 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5261 const char *name)
5263 gcc_assert (argpos > 0);
5265 if (argpos == 1)
5267 gfc_actual_arglist* result;
5269 result = gfc_get_actual_arglist ();
5270 result->expr = po;
5271 result->next = lst;
5272 if (name)
5273 result->name = name;
5275 return result;
5278 if (lst)
5279 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5280 else
5281 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5282 return lst;
5286 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5288 static gfc_expr*
5289 extract_compcall_passed_object (gfc_expr* e)
5291 gfc_expr* po;
5293 gcc_assert (e->expr_type == EXPR_COMPCALL);
5295 if (e->value.compcall.base_object)
5296 po = gfc_copy_expr (e->value.compcall.base_object);
5297 else
5299 po = gfc_get_expr ();
5300 po->expr_type = EXPR_VARIABLE;
5301 po->symtree = e->symtree;
5302 po->ref = gfc_copy_ref (e->ref);
5303 po->where = e->where;
5306 if (gfc_resolve_expr (po) == FAILURE)
5307 return NULL;
5309 return po;
5313 /* Update the arglist of an EXPR_COMPCALL expression to include the
5314 passed-object. */
5316 static gfc_try
5317 update_compcall_arglist (gfc_expr* e)
5319 gfc_expr* po;
5320 gfc_typebound_proc* tbp;
5322 tbp = e->value.compcall.tbp;
5324 if (tbp->error)
5325 return FAILURE;
5327 po = extract_compcall_passed_object (e);
5328 if (!po)
5329 return FAILURE;
5331 if (tbp->nopass || e->value.compcall.ignore_pass)
5333 gfc_free_expr (po);
5334 return SUCCESS;
5337 gcc_assert (tbp->pass_arg_num > 0);
5338 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5339 tbp->pass_arg_num,
5340 tbp->pass_arg);
5342 return SUCCESS;
5346 /* Extract the passed object from a PPC call (a copy of it). */
5348 static gfc_expr*
5349 extract_ppc_passed_object (gfc_expr *e)
5351 gfc_expr *po;
5352 gfc_ref **ref;
5354 po = gfc_get_expr ();
5355 po->expr_type = EXPR_VARIABLE;
5356 po->symtree = e->symtree;
5357 po->ref = gfc_copy_ref (e->ref);
5358 po->where = e->where;
5360 /* Remove PPC reference. */
5361 ref = &po->ref;
5362 while ((*ref)->next)
5363 ref = &(*ref)->next;
5364 gfc_free_ref_list (*ref);
5365 *ref = NULL;
5367 if (gfc_resolve_expr (po) == FAILURE)
5368 return NULL;
5370 return po;
5374 /* Update the actual arglist of a procedure pointer component to include the
5375 passed-object. */
5377 static gfc_try
5378 update_ppc_arglist (gfc_expr* e)
5380 gfc_expr* po;
5381 gfc_component *ppc;
5382 gfc_typebound_proc* tb;
5384 if (!gfc_is_proc_ptr_comp (e, &ppc))
5385 return FAILURE;
5387 tb = ppc->tb;
5389 if (tb->error)
5390 return FAILURE;
5391 else if (tb->nopass)
5392 return SUCCESS;
5394 po = extract_ppc_passed_object (e);
5395 if (!po)
5396 return FAILURE;
5398 /* F08:R739. */
5399 if (po->rank > 0)
5401 gfc_error ("Passed-object at %L must be scalar", &e->where);
5402 return FAILURE;
5405 /* F08:C611. */
5406 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5408 gfc_error ("Base object for procedure-pointer component call at %L is of"
5409 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
5410 return FAILURE;
5413 gcc_assert (tb->pass_arg_num > 0);
5414 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5415 tb->pass_arg_num,
5416 tb->pass_arg);
5418 return SUCCESS;
5422 /* Check that the object a TBP is called on is valid, i.e. it must not be
5423 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5425 static gfc_try
5426 check_typebound_baseobject (gfc_expr* e)
5428 gfc_expr* base;
5429 gfc_try return_value = FAILURE;
5431 base = extract_compcall_passed_object (e);
5432 if (!base)
5433 return FAILURE;
5435 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5437 /* F08:C611. */
5438 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5440 gfc_error ("Base object for type-bound procedure call at %L is of"
5441 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5442 goto cleanup;
5445 /* F08:C1230. If the procedure called is NOPASS,
5446 the base object must be scalar. */
5447 if (e->value.compcall.tbp->nopass && base->rank > 0)
5449 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5450 " be scalar", &e->where);
5451 goto cleanup;
5454 /* FIXME: Remove once PR 43214 is fixed (TBP with non-scalar PASS). */
5455 if (base->rank > 0)
5457 gfc_error ("Non-scalar base object at %L currently not implemented",
5458 &e->where);
5459 goto cleanup;
5462 return_value = SUCCESS;
5464 cleanup:
5465 gfc_free_expr (base);
5466 return return_value;
5470 /* Resolve a call to a type-bound procedure, either function or subroutine,
5471 statically from the data in an EXPR_COMPCALL expression. The adapted
5472 arglist and the target-procedure symtree are returned. */
5474 static gfc_try
5475 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5476 gfc_actual_arglist** actual)
5478 gcc_assert (e->expr_type == EXPR_COMPCALL);
5479 gcc_assert (!e->value.compcall.tbp->is_generic);
5481 /* Update the actual arglist for PASS. */
5482 if (update_compcall_arglist (e) == FAILURE)
5483 return FAILURE;
5485 *actual = e->value.compcall.actual;
5486 *target = e->value.compcall.tbp->u.specific;
5488 gfc_free_ref_list (e->ref);
5489 e->ref = NULL;
5490 e->value.compcall.actual = NULL;
5492 return SUCCESS;
5496 /* Get the ultimate declared type from an expression. In addition,
5497 return the last class/derived type reference and the copy of the
5498 reference list. */
5499 static gfc_symbol*
5500 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5501 gfc_expr *e)
5503 gfc_symbol *declared;
5504 gfc_ref *ref;
5506 declared = NULL;
5507 if (class_ref)
5508 *class_ref = NULL;
5509 if (new_ref)
5510 *new_ref = gfc_copy_ref (e->ref);
5512 for (ref = e->ref; ref; ref = ref->next)
5514 if (ref->type != REF_COMPONENT)
5515 continue;
5517 if (ref->u.c.component->ts.type == BT_CLASS
5518 || ref->u.c.component->ts.type == BT_DERIVED)
5520 declared = ref->u.c.component->ts.u.derived;
5521 if (class_ref)
5522 *class_ref = ref;
5526 if (declared == NULL)
5527 declared = e->symtree->n.sym->ts.u.derived;
5529 return declared;
5533 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5534 which of the specific bindings (if any) matches the arglist and transform
5535 the expression into a call of that binding. */
5537 static gfc_try
5538 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5540 gfc_typebound_proc* genproc;
5541 const char* genname;
5542 gfc_symtree *st;
5543 gfc_symbol *derived;
5545 gcc_assert (e->expr_type == EXPR_COMPCALL);
5546 genname = e->value.compcall.name;
5547 genproc = e->value.compcall.tbp;
5549 if (!genproc->is_generic)
5550 return SUCCESS;
5552 /* Try the bindings on this type and in the inheritance hierarchy. */
5553 for (; genproc; genproc = genproc->overridden)
5555 gfc_tbp_generic* g;
5557 gcc_assert (genproc->is_generic);
5558 for (g = genproc->u.generic; g; g = g->next)
5560 gfc_symbol* target;
5561 gfc_actual_arglist* args;
5562 bool matches;
5564 gcc_assert (g->specific);
5566 if (g->specific->error)
5567 continue;
5569 target = g->specific->u.specific->n.sym;
5571 /* Get the right arglist by handling PASS/NOPASS. */
5572 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5573 if (!g->specific->nopass)
5575 gfc_expr* po;
5576 po = extract_compcall_passed_object (e);
5577 if (!po)
5578 return FAILURE;
5580 gcc_assert (g->specific->pass_arg_num > 0);
5581 gcc_assert (!g->specific->error);
5582 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5583 g->specific->pass_arg);
5585 resolve_actual_arglist (args, target->attr.proc,
5586 is_external_proc (target) && !target->formal);
5588 /* Check if this arglist matches the formal. */
5589 matches = gfc_arglist_matches_symbol (&args, target);
5591 /* Clean up and break out of the loop if we've found it. */
5592 gfc_free_actual_arglist (args);
5593 if (matches)
5595 e->value.compcall.tbp = g->specific;
5596 genname = g->specific_st->name;
5597 /* Pass along the name for CLASS methods, where the vtab
5598 procedure pointer component has to be referenced. */
5599 if (name)
5600 *name = genname;
5601 goto success;
5606 /* Nothing matching found! */
5607 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5608 " '%s' at %L", genname, &e->where);
5609 return FAILURE;
5611 success:
5612 /* Make sure that we have the right specific instance for the name. */
5613 derived = get_declared_from_expr (NULL, NULL, e);
5615 st = gfc_find_typebound_proc (derived, NULL, genname, false, &e->where);
5616 if (st)
5617 e->value.compcall.tbp = st->n.tb;
5619 return SUCCESS;
5623 /* Resolve a call to a type-bound subroutine. */
5625 static gfc_try
5626 resolve_typebound_call (gfc_code* c, const char **name)
5628 gfc_actual_arglist* newactual;
5629 gfc_symtree* target;
5631 /* Check that's really a SUBROUTINE. */
5632 if (!c->expr1->value.compcall.tbp->subroutine)
5634 gfc_error ("'%s' at %L should be a SUBROUTINE",
5635 c->expr1->value.compcall.name, &c->loc);
5636 return FAILURE;
5639 if (check_typebound_baseobject (c->expr1) == FAILURE)
5640 return FAILURE;
5642 /* Pass along the name for CLASS methods, where the vtab
5643 procedure pointer component has to be referenced. */
5644 if (name)
5645 *name = c->expr1->value.compcall.name;
5647 if (resolve_typebound_generic_call (c->expr1, name) == FAILURE)
5648 return FAILURE;
5650 /* Transform into an ordinary EXEC_CALL for now. */
5652 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5653 return FAILURE;
5655 c->ext.actual = newactual;
5656 c->symtree = target;
5657 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5659 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5661 gfc_free_expr (c->expr1);
5662 c->expr1 = gfc_get_expr ();
5663 c->expr1->expr_type = EXPR_FUNCTION;
5664 c->expr1->symtree = target;
5665 c->expr1->where = c->loc;
5667 return resolve_call (c);
5671 /* Resolve a component-call expression. */
5672 static gfc_try
5673 resolve_compcall (gfc_expr* e, const char **name)
5675 gfc_actual_arglist* newactual;
5676 gfc_symtree* target;
5678 /* Check that's really a FUNCTION. */
5679 if (!e->value.compcall.tbp->function)
5681 gfc_error ("'%s' at %L should be a FUNCTION",
5682 e->value.compcall.name, &e->where);
5683 return FAILURE;
5686 /* These must not be assign-calls! */
5687 gcc_assert (!e->value.compcall.assign);
5689 if (check_typebound_baseobject (e) == FAILURE)
5690 return FAILURE;
5692 /* Pass along the name for CLASS methods, where the vtab
5693 procedure pointer component has to be referenced. */
5694 if (name)
5695 *name = e->value.compcall.name;
5697 if (resolve_typebound_generic_call (e, name) == FAILURE)
5698 return FAILURE;
5699 gcc_assert (!e->value.compcall.tbp->is_generic);
5701 /* Take the rank from the function's symbol. */
5702 if (e->value.compcall.tbp->u.specific->n.sym->as)
5703 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5705 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5706 arglist to the TBP's binding target. */
5708 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5709 return FAILURE;
5711 e->value.function.actual = newactual;
5712 e->value.function.name = NULL;
5713 e->value.function.esym = target->n.sym;
5714 e->value.function.isym = NULL;
5715 e->symtree = target;
5716 e->ts = target->n.sym->ts;
5717 e->expr_type = EXPR_FUNCTION;
5719 /* Resolution is not necessary if this is a class subroutine; this
5720 function only has to identify the specific proc. Resolution of
5721 the call will be done next in resolve_typebound_call. */
5722 return gfc_resolve_expr (e);
5727 /* Resolve a typebound function, or 'method'. First separate all
5728 the non-CLASS references by calling resolve_compcall directly. */
5730 static gfc_try
5731 resolve_typebound_function (gfc_expr* e)
5733 gfc_symbol *declared;
5734 gfc_component *c;
5735 gfc_ref *new_ref;
5736 gfc_ref *class_ref;
5737 gfc_symtree *st;
5738 const char *name;
5739 gfc_typespec ts;
5740 gfc_expr *expr;
5742 st = e->symtree;
5744 /* Deal with typebound operators for CLASS objects. */
5745 expr = e->value.compcall.base_object;
5746 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5748 /* Since the typebound operators are generic, we have to ensure
5749 that any delays in resolution are corrected and that the vtab
5750 is present. */
5751 ts = expr->ts;
5752 declared = ts.u.derived;
5753 c = gfc_find_component (declared, "_vptr", true, true);
5754 if (c->ts.u.derived == NULL)
5755 c->ts.u.derived = gfc_find_derived_vtab (declared);
5757 if (resolve_compcall (e, &name) == FAILURE)
5758 return FAILURE;
5760 /* Use the generic name if it is there. */
5761 name = name ? name : e->value.function.esym->name;
5762 e->symtree = expr->symtree;
5763 e->ref = gfc_copy_ref (expr->ref);
5764 gfc_add_vptr_component (e);
5765 gfc_add_component_ref (e, name);
5766 e->value.function.esym = NULL;
5767 return SUCCESS;
5770 if (st == NULL)
5771 return resolve_compcall (e, NULL);
5773 if (resolve_ref (e) == FAILURE)
5774 return FAILURE;
5776 /* Get the CLASS declared type. */
5777 declared = get_declared_from_expr (&class_ref, &new_ref, e);
5779 /* Weed out cases of the ultimate component being a derived type. */
5780 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5781 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5783 gfc_free_ref_list (new_ref);
5784 return resolve_compcall (e, NULL);
5787 c = gfc_find_component (declared, "_data", true, true);
5788 declared = c->ts.u.derived;
5790 /* Treat the call as if it is a typebound procedure, in order to roll
5791 out the correct name for the specific function. */
5792 if (resolve_compcall (e, &name) == FAILURE)
5793 return FAILURE;
5794 ts = e->ts;
5796 /* Then convert the expression to a procedure pointer component call. */
5797 e->value.function.esym = NULL;
5798 e->symtree = st;
5800 if (new_ref)
5801 e->ref = new_ref;
5803 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5804 gfc_add_vptr_component (e);
5805 gfc_add_component_ref (e, name);
5807 /* Recover the typespec for the expression. This is really only
5808 necessary for generic procedures, where the additional call
5809 to gfc_add_component_ref seems to throw the collection of the
5810 correct typespec. */
5811 e->ts = ts;
5812 return SUCCESS;
5815 /* Resolve a typebound subroutine, or 'method'. First separate all
5816 the non-CLASS references by calling resolve_typebound_call
5817 directly. */
5819 static gfc_try
5820 resolve_typebound_subroutine (gfc_code *code)
5822 gfc_symbol *declared;
5823 gfc_component *c;
5824 gfc_ref *new_ref;
5825 gfc_ref *class_ref;
5826 gfc_symtree *st;
5827 const char *name;
5828 gfc_typespec ts;
5829 gfc_expr *expr;
5831 st = code->expr1->symtree;
5833 /* Deal with typebound operators for CLASS objects. */
5834 expr = code->expr1->value.compcall.base_object;
5835 if (expr && expr->symtree->n.sym->ts.type == BT_CLASS
5836 && code->expr1->value.compcall.name)
5838 /* Since the typebound operators are generic, we have to ensure
5839 that any delays in resolution are corrected and that the vtab
5840 is present. */
5841 ts = expr->symtree->n.sym->ts;
5842 declared = ts.u.derived;
5843 c = gfc_find_component (declared, "_vptr", true, true);
5844 if (c->ts.u.derived == NULL)
5845 c->ts.u.derived = gfc_find_derived_vtab (declared);
5847 if (resolve_typebound_call (code, &name) == FAILURE)
5848 return FAILURE;
5850 /* Use the generic name if it is there. */
5851 name = name ? name : code->expr1->value.function.esym->name;
5852 code->expr1->symtree = expr->symtree;
5853 expr->symtree->n.sym->ts.u.derived = declared;
5854 gfc_add_vptr_component (code->expr1);
5855 gfc_add_component_ref (code->expr1, name);
5856 code->expr1->value.function.esym = NULL;
5857 return SUCCESS;
5860 if (st == NULL)
5861 return resolve_typebound_call (code, NULL);
5863 if (resolve_ref (code->expr1) == FAILURE)
5864 return FAILURE;
5866 /* Get the CLASS declared type. */
5867 get_declared_from_expr (&class_ref, &new_ref, code->expr1);
5869 /* Weed out cases of the ultimate component being a derived type. */
5870 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5871 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5873 gfc_free_ref_list (new_ref);
5874 return resolve_typebound_call (code, NULL);
5877 if (resolve_typebound_call (code, &name) == FAILURE)
5878 return FAILURE;
5879 ts = code->expr1->ts;
5881 /* Then convert the expression to a procedure pointer component call. */
5882 code->expr1->value.function.esym = NULL;
5883 code->expr1->symtree = st;
5885 if (new_ref)
5886 code->expr1->ref = new_ref;
5888 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5889 gfc_add_vptr_component (code->expr1);
5890 gfc_add_component_ref (code->expr1, name);
5892 /* Recover the typespec for the expression. This is really only
5893 necessary for generic procedures, where the additional call
5894 to gfc_add_component_ref seems to throw the collection of the
5895 correct typespec. */
5896 code->expr1->ts = ts;
5897 return SUCCESS;
5901 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5903 static gfc_try
5904 resolve_ppc_call (gfc_code* c)
5906 gfc_component *comp;
5907 bool b;
5909 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
5910 gcc_assert (b);
5912 c->resolved_sym = c->expr1->symtree->n.sym;
5913 c->expr1->expr_type = EXPR_VARIABLE;
5915 if (!comp->attr.subroutine)
5916 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5918 if (resolve_ref (c->expr1) == FAILURE)
5919 return FAILURE;
5921 if (update_ppc_arglist (c->expr1) == FAILURE)
5922 return FAILURE;
5924 c->ext.actual = c->expr1->value.compcall.actual;
5926 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5927 comp->formal == NULL) == FAILURE)
5928 return FAILURE;
5930 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5932 return SUCCESS;
5936 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5938 static gfc_try
5939 resolve_expr_ppc (gfc_expr* e)
5941 gfc_component *comp;
5942 bool b;
5944 b = gfc_is_proc_ptr_comp (e, &comp);
5945 gcc_assert (b);
5947 /* Convert to EXPR_FUNCTION. */
5948 e->expr_type = EXPR_FUNCTION;
5949 e->value.function.isym = NULL;
5950 e->value.function.actual = e->value.compcall.actual;
5951 e->ts = comp->ts;
5952 if (comp->as != NULL)
5953 e->rank = comp->as->rank;
5955 if (!comp->attr.function)
5956 gfc_add_function (&comp->attr, comp->name, &e->where);
5958 if (resolve_ref (e) == FAILURE)
5959 return FAILURE;
5961 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5962 comp->formal == NULL) == FAILURE)
5963 return FAILURE;
5965 if (update_ppc_arglist (e) == FAILURE)
5966 return FAILURE;
5968 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
5970 return SUCCESS;
5974 static bool
5975 gfc_is_expandable_expr (gfc_expr *e)
5977 gfc_constructor *con;
5979 if (e->expr_type == EXPR_ARRAY)
5981 /* Traverse the constructor looking for variables that are flavor
5982 parameter. Parameters must be expanded since they are fully used at
5983 compile time. */
5984 con = gfc_constructor_first (e->value.constructor);
5985 for (; con; con = gfc_constructor_next (con))
5987 if (con->expr->expr_type == EXPR_VARIABLE
5988 && con->expr->symtree
5989 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
5990 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
5991 return true;
5992 if (con->expr->expr_type == EXPR_ARRAY
5993 && gfc_is_expandable_expr (con->expr))
5994 return true;
5998 return false;
6001 /* Resolve an expression. That is, make sure that types of operands agree
6002 with their operators, intrinsic operators are converted to function calls
6003 for overloaded types and unresolved function references are resolved. */
6005 gfc_try
6006 gfc_resolve_expr (gfc_expr *e)
6008 gfc_try t;
6009 bool inquiry_save;
6011 if (e == NULL)
6012 return SUCCESS;
6014 /* inquiry_argument only applies to variables. */
6015 inquiry_save = inquiry_argument;
6016 if (e->expr_type != EXPR_VARIABLE)
6017 inquiry_argument = false;
6019 switch (e->expr_type)
6021 case EXPR_OP:
6022 t = resolve_operator (e);
6023 break;
6025 case EXPR_FUNCTION:
6026 case EXPR_VARIABLE:
6028 if (check_host_association (e))
6029 t = resolve_function (e);
6030 else
6032 t = resolve_variable (e);
6033 if (t == SUCCESS)
6034 expression_rank (e);
6037 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6038 && e->ref->type != REF_SUBSTRING)
6039 gfc_resolve_substring_charlen (e);
6041 break;
6043 case EXPR_COMPCALL:
6044 t = resolve_typebound_function (e);
6045 break;
6047 case EXPR_SUBSTRING:
6048 t = resolve_ref (e);
6049 break;
6051 case EXPR_CONSTANT:
6052 case EXPR_NULL:
6053 t = SUCCESS;
6054 break;
6056 case EXPR_PPC:
6057 t = resolve_expr_ppc (e);
6058 break;
6060 case EXPR_ARRAY:
6061 t = FAILURE;
6062 if (resolve_ref (e) == FAILURE)
6063 break;
6065 t = gfc_resolve_array_constructor (e);
6066 /* Also try to expand a constructor. */
6067 if (t == SUCCESS)
6069 expression_rank (e);
6070 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6071 gfc_expand_constructor (e, false);
6074 /* This provides the opportunity for the length of constructors with
6075 character valued function elements to propagate the string length
6076 to the expression. */
6077 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
6079 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6080 here rather then add a duplicate test for it above. */
6081 gfc_expand_constructor (e, false);
6082 t = gfc_resolve_character_array_constructor (e);
6085 break;
6087 case EXPR_STRUCTURE:
6088 t = resolve_ref (e);
6089 if (t == FAILURE)
6090 break;
6092 t = resolve_structure_cons (e, 0);
6093 if (t == FAILURE)
6094 break;
6096 t = gfc_simplify_expr (e, 0);
6097 break;
6099 default:
6100 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6103 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
6104 fixup_charlen (e);
6106 inquiry_argument = inquiry_save;
6108 return t;
6112 /* Resolve an expression from an iterator. They must be scalar and have
6113 INTEGER or (optionally) REAL type. */
6115 static gfc_try
6116 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6117 const char *name_msgid)
6119 if (gfc_resolve_expr (expr) == FAILURE)
6120 return FAILURE;
6122 if (expr->rank != 0)
6124 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6125 return FAILURE;
6128 if (expr->ts.type != BT_INTEGER)
6130 if (expr->ts.type == BT_REAL)
6132 if (real_ok)
6133 return gfc_notify_std (GFC_STD_F95_DEL,
6134 "Deleted feature: %s at %L must be integer",
6135 _(name_msgid), &expr->where);
6136 else
6138 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6139 &expr->where);
6140 return FAILURE;
6143 else
6145 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6146 return FAILURE;
6149 return SUCCESS;
6153 /* Resolve the expressions in an iterator structure. If REAL_OK is
6154 false allow only INTEGER type iterators, otherwise allow REAL types. */
6156 gfc_try
6157 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
6159 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
6160 == FAILURE)
6161 return FAILURE;
6163 if (gfc_check_vardef_context (iter->var, false, _("iterator variable"))
6164 == FAILURE)
6165 return FAILURE;
6167 if (gfc_resolve_iterator_expr (iter->start, real_ok,
6168 "Start expression in DO loop") == FAILURE)
6169 return FAILURE;
6171 if (gfc_resolve_iterator_expr (iter->end, real_ok,
6172 "End expression in DO loop") == FAILURE)
6173 return FAILURE;
6175 if (gfc_resolve_iterator_expr (iter->step, real_ok,
6176 "Step expression in DO loop") == FAILURE)
6177 return FAILURE;
6179 if (iter->step->expr_type == EXPR_CONSTANT)
6181 if ((iter->step->ts.type == BT_INTEGER
6182 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6183 || (iter->step->ts.type == BT_REAL
6184 && mpfr_sgn (iter->step->value.real) == 0))
6186 gfc_error ("Step expression in DO loop at %L cannot be zero",
6187 &iter->step->where);
6188 return FAILURE;
6192 /* Convert start, end, and step to the same type as var. */
6193 if (iter->start->ts.kind != iter->var->ts.kind
6194 || iter->start->ts.type != iter->var->ts.type)
6195 gfc_convert_type (iter->start, &iter->var->ts, 2);
6197 if (iter->end->ts.kind != iter->var->ts.kind
6198 || iter->end->ts.type != iter->var->ts.type)
6199 gfc_convert_type (iter->end, &iter->var->ts, 2);
6201 if (iter->step->ts.kind != iter->var->ts.kind
6202 || iter->step->ts.type != iter->var->ts.type)
6203 gfc_convert_type (iter->step, &iter->var->ts, 2);
6205 if (iter->start->expr_type == EXPR_CONSTANT
6206 && iter->end->expr_type == EXPR_CONSTANT
6207 && iter->step->expr_type == EXPR_CONSTANT)
6209 int sgn, cmp;
6210 if (iter->start->ts.type == BT_INTEGER)
6212 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6213 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6215 else
6217 sgn = mpfr_sgn (iter->step->value.real);
6218 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6220 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
6221 gfc_warning ("DO loop at %L will be executed zero times",
6222 &iter->step->where);
6225 return SUCCESS;
6229 /* Traversal function for find_forall_index. f == 2 signals that
6230 that variable itself is not to be checked - only the references. */
6232 static bool
6233 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6235 if (expr->expr_type != EXPR_VARIABLE)
6236 return false;
6238 /* A scalar assignment */
6239 if (!expr->ref || *f == 1)
6241 if (expr->symtree->n.sym == sym)
6242 return true;
6243 else
6244 return false;
6247 if (*f == 2)
6248 *f = 1;
6249 return false;
6253 /* Check whether the FORALL index appears in the expression or not.
6254 Returns SUCCESS if SYM is found in EXPR. */
6256 gfc_try
6257 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6259 if (gfc_traverse_expr (expr, sym, forall_index, f))
6260 return SUCCESS;
6261 else
6262 return FAILURE;
6266 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6267 to be a scalar INTEGER variable. The subscripts and stride are scalar
6268 INTEGERs, and if stride is a constant it must be nonzero.
6269 Furthermore "A subscript or stride in a forall-triplet-spec shall
6270 not contain a reference to any index-name in the
6271 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6273 static void
6274 resolve_forall_iterators (gfc_forall_iterator *it)
6276 gfc_forall_iterator *iter, *iter2;
6278 for (iter = it; iter; iter = iter->next)
6280 if (gfc_resolve_expr (iter->var) == SUCCESS
6281 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6282 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6283 &iter->var->where);
6285 if (gfc_resolve_expr (iter->start) == SUCCESS
6286 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6287 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6288 &iter->start->where);
6289 if (iter->var->ts.kind != iter->start->ts.kind)
6290 gfc_convert_type (iter->start, &iter->var->ts, 2);
6292 if (gfc_resolve_expr (iter->end) == SUCCESS
6293 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6294 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6295 &iter->end->where);
6296 if (iter->var->ts.kind != iter->end->ts.kind)
6297 gfc_convert_type (iter->end, &iter->var->ts, 2);
6299 if (gfc_resolve_expr (iter->stride) == SUCCESS)
6301 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6302 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6303 &iter->stride->where, "INTEGER");
6305 if (iter->stride->expr_type == EXPR_CONSTANT
6306 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
6307 gfc_error ("FORALL stride expression at %L cannot be zero",
6308 &iter->stride->where);
6310 if (iter->var->ts.kind != iter->stride->ts.kind)
6311 gfc_convert_type (iter->stride, &iter->var->ts, 2);
6314 for (iter = it; iter; iter = iter->next)
6315 for (iter2 = iter; iter2; iter2 = iter2->next)
6317 if (find_forall_index (iter2->start,
6318 iter->var->symtree->n.sym, 0) == SUCCESS
6319 || find_forall_index (iter2->end,
6320 iter->var->symtree->n.sym, 0) == SUCCESS
6321 || find_forall_index (iter2->stride,
6322 iter->var->symtree->n.sym, 0) == SUCCESS)
6323 gfc_error ("FORALL index '%s' may not appear in triplet "
6324 "specification at %L", iter->var->symtree->name,
6325 &iter2->start->where);
6330 /* Given a pointer to a symbol that is a derived type, see if it's
6331 inaccessible, i.e. if it's defined in another module and the components are
6332 PRIVATE. The search is recursive if necessary. Returns zero if no
6333 inaccessible components are found, nonzero otherwise. */
6335 static int
6336 derived_inaccessible (gfc_symbol *sym)
6338 gfc_component *c;
6340 if (sym->attr.use_assoc && sym->attr.private_comp)
6341 return 1;
6343 for (c = sym->components; c; c = c->next)
6345 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6346 return 1;
6349 return 0;
6353 /* Resolve the argument of a deallocate expression. The expression must be
6354 a pointer or a full array. */
6356 static gfc_try
6357 resolve_deallocate_expr (gfc_expr *e)
6359 symbol_attribute attr;
6360 int allocatable, pointer;
6361 gfc_ref *ref;
6362 gfc_symbol *sym;
6363 gfc_component *c;
6365 if (gfc_resolve_expr (e) == FAILURE)
6366 return FAILURE;
6368 if (e->expr_type != EXPR_VARIABLE)
6369 goto bad;
6371 sym = e->symtree->n.sym;
6373 if (sym->ts.type == BT_CLASS)
6375 allocatable = CLASS_DATA (sym)->attr.allocatable;
6376 pointer = CLASS_DATA (sym)->attr.class_pointer;
6378 else
6380 allocatable = sym->attr.allocatable;
6381 pointer = sym->attr.pointer;
6383 for (ref = e->ref; ref; ref = ref->next)
6385 switch (ref->type)
6387 case REF_ARRAY:
6388 if (ref->u.ar.type != AR_FULL)
6389 allocatable = 0;
6390 break;
6392 case REF_COMPONENT:
6393 c = ref->u.c.component;
6394 if (c->ts.type == BT_CLASS)
6396 allocatable = CLASS_DATA (c)->attr.allocatable;
6397 pointer = CLASS_DATA (c)->attr.class_pointer;
6399 else
6401 allocatable = c->attr.allocatable;
6402 pointer = c->attr.pointer;
6404 break;
6406 case REF_SUBSTRING:
6407 allocatable = 0;
6408 break;
6412 attr = gfc_expr_attr (e);
6414 if (allocatable == 0 && attr.pointer == 0)
6416 bad:
6417 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6418 &e->where);
6419 return FAILURE;
6422 if (pointer
6423 && gfc_check_vardef_context (e, true, _("DEALLOCATE object")) == FAILURE)
6424 return FAILURE;
6425 if (gfc_check_vardef_context (e, false, _("DEALLOCATE object")) == FAILURE)
6426 return FAILURE;
6428 return SUCCESS;
6432 /* Returns true if the expression e contains a reference to the symbol sym. */
6433 static bool
6434 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6436 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6437 return true;
6439 return false;
6442 bool
6443 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6445 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6449 /* Given the expression node e for an allocatable/pointer of derived type to be
6450 allocated, get the expression node to be initialized afterwards (needed for
6451 derived types with default initializers, and derived types with allocatable
6452 components that need nullification.) */
6454 gfc_expr *
6455 gfc_expr_to_initialize (gfc_expr *e)
6457 gfc_expr *result;
6458 gfc_ref *ref;
6459 int i;
6461 result = gfc_copy_expr (e);
6463 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6464 for (ref = result->ref; ref; ref = ref->next)
6465 if (ref->type == REF_ARRAY && ref->next == NULL)
6467 ref->u.ar.type = AR_FULL;
6469 for (i = 0; i < ref->u.ar.dimen; i++)
6470 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6472 result->rank = ref->u.ar.dimen;
6473 break;
6476 return result;
6480 /* If the last ref of an expression is an array ref, return a copy of the
6481 expression with that one removed. Otherwise, a copy of the original
6482 expression. This is used for allocate-expressions and pointer assignment
6483 LHS, where there may be an array specification that needs to be stripped
6484 off when using gfc_check_vardef_context. */
6486 static gfc_expr*
6487 remove_last_array_ref (gfc_expr* e)
6489 gfc_expr* e2;
6490 gfc_ref** r;
6492 e2 = gfc_copy_expr (e);
6493 for (r = &e2->ref; *r; r = &(*r)->next)
6494 if ((*r)->type == REF_ARRAY && !(*r)->next)
6496 gfc_free_ref_list (*r);
6497 *r = NULL;
6498 break;
6501 return e2;
6505 /* Used in resolve_allocate_expr to check that a allocation-object and
6506 a source-expr are conformable. This does not catch all possible
6507 cases; in particular a runtime checking is needed. */
6509 static gfc_try
6510 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6512 gfc_ref *tail;
6513 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6515 /* First compare rank. */
6516 if (tail && e1->rank != tail->u.ar.as->rank)
6518 gfc_error ("Source-expr at %L must be scalar or have the "
6519 "same rank as the allocate-object at %L",
6520 &e1->where, &e2->where);
6521 return FAILURE;
6524 if (e1->shape)
6526 int i;
6527 mpz_t s;
6529 mpz_init (s);
6531 for (i = 0; i < e1->rank; i++)
6533 if (tail->u.ar.end[i])
6535 mpz_set (s, tail->u.ar.end[i]->value.integer);
6536 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6537 mpz_add_ui (s, s, 1);
6539 else
6541 mpz_set (s, tail->u.ar.start[i]->value.integer);
6544 if (mpz_cmp (e1->shape[i], s) != 0)
6546 gfc_error ("Source-expr at %L and allocate-object at %L must "
6547 "have the same shape", &e1->where, &e2->where);
6548 mpz_clear (s);
6549 return FAILURE;
6553 mpz_clear (s);
6556 return SUCCESS;
6560 /* Resolve the expression in an ALLOCATE statement, doing the additional
6561 checks to see whether the expression is OK or not. The expression must
6562 have a trailing array reference that gives the size of the array. */
6564 static gfc_try
6565 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6567 int i, pointer, allocatable, dimension, is_abstract;
6568 int codimension;
6569 symbol_attribute attr;
6570 gfc_ref *ref, *ref2;
6571 gfc_expr *e2;
6572 gfc_array_ref *ar;
6573 gfc_symbol *sym = NULL;
6574 gfc_alloc *a;
6575 gfc_component *c;
6576 gfc_try t;
6578 /* Mark the ultimost array component as being in allocate to allow DIMEN_STAR
6579 checking of coarrays. */
6580 for (ref = e->ref; ref; ref = ref->next)
6581 if (ref->next == NULL)
6582 break;
6584 if (ref && ref->type == REF_ARRAY)
6585 ref->u.ar.in_allocate = true;
6587 if (gfc_resolve_expr (e) == FAILURE)
6588 goto failure;
6590 /* Make sure the expression is allocatable or a pointer. If it is
6591 pointer, the next-to-last reference must be a pointer. */
6593 ref2 = NULL;
6594 if (e->symtree)
6595 sym = e->symtree->n.sym;
6597 /* Check whether ultimate component is abstract and CLASS. */
6598 is_abstract = 0;
6600 if (e->expr_type != EXPR_VARIABLE)
6602 allocatable = 0;
6603 attr = gfc_expr_attr (e);
6604 pointer = attr.pointer;
6605 dimension = attr.dimension;
6606 codimension = attr.codimension;
6608 else
6610 if (sym->ts.type == BT_CLASS)
6612 allocatable = CLASS_DATA (sym)->attr.allocatable;
6613 pointer = CLASS_DATA (sym)->attr.class_pointer;
6614 dimension = CLASS_DATA (sym)->attr.dimension;
6615 codimension = CLASS_DATA (sym)->attr.codimension;
6616 is_abstract = CLASS_DATA (sym)->attr.abstract;
6618 else
6620 allocatable = sym->attr.allocatable;
6621 pointer = sym->attr.pointer;
6622 dimension = sym->attr.dimension;
6623 codimension = sym->attr.codimension;
6626 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6628 switch (ref->type)
6630 case REF_ARRAY:
6631 if (ref->next != NULL)
6632 pointer = 0;
6633 break;
6635 case REF_COMPONENT:
6636 /* F2008, C644. */
6637 if (gfc_is_coindexed (e))
6639 gfc_error ("Coindexed allocatable object at %L",
6640 &e->where);
6641 goto failure;
6644 c = ref->u.c.component;
6645 if (c->ts.type == BT_CLASS)
6647 allocatable = CLASS_DATA (c)->attr.allocatable;
6648 pointer = CLASS_DATA (c)->attr.class_pointer;
6649 dimension = CLASS_DATA (c)->attr.dimension;
6650 codimension = CLASS_DATA (c)->attr.codimension;
6651 is_abstract = CLASS_DATA (c)->attr.abstract;
6653 else
6655 allocatable = c->attr.allocatable;
6656 pointer = c->attr.pointer;
6657 dimension = c->attr.dimension;
6658 codimension = c->attr.codimension;
6659 is_abstract = c->attr.abstract;
6661 break;
6663 case REF_SUBSTRING:
6664 allocatable = 0;
6665 pointer = 0;
6666 break;
6671 if (allocatable == 0 && pointer == 0)
6673 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6674 &e->where);
6675 goto failure;
6678 /* Some checks for the SOURCE tag. */
6679 if (code->expr3)
6681 /* Check F03:C631. */
6682 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6684 gfc_error ("Type of entity at %L is type incompatible with "
6685 "source-expr at %L", &e->where, &code->expr3->where);
6686 goto failure;
6689 /* Check F03:C632 and restriction following Note 6.18. */
6690 if (code->expr3->rank > 0
6691 && conformable_arrays (code->expr3, e) == FAILURE)
6692 goto failure;
6694 /* Check F03:C633. */
6695 if (code->expr3->ts.kind != e->ts.kind)
6697 gfc_error ("The allocate-object at %L and the source-expr at %L "
6698 "shall have the same kind type parameter",
6699 &e->where, &code->expr3->where);
6700 goto failure;
6704 /* Check F08:C629. */
6705 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6706 && !code->expr3)
6708 gcc_assert (e->ts.type == BT_CLASS);
6709 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6710 "type-spec or source-expr", sym->name, &e->where);
6711 goto failure;
6714 /* In the variable definition context checks, gfc_expr_attr is used
6715 on the expression. This is fooled by the array specification
6716 present in e, thus we have to eliminate that one temporarily. */
6717 e2 = remove_last_array_ref (e);
6718 t = SUCCESS;
6719 if (t == SUCCESS && pointer)
6720 t = gfc_check_vardef_context (e2, true, _("ALLOCATE object"));
6721 if (t == SUCCESS)
6722 t = gfc_check_vardef_context (e2, false, _("ALLOCATE object"));
6723 gfc_free_expr (e2);
6724 if (t == FAILURE)
6725 goto failure;
6727 if (!code->expr3)
6729 /* Set up default initializer if needed. */
6730 gfc_typespec ts;
6731 gfc_expr *init_e;
6733 if (code->ext.alloc.ts.type == BT_DERIVED)
6734 ts = code->ext.alloc.ts;
6735 else
6736 ts = e->ts;
6738 if (ts.type == BT_CLASS)
6739 ts = ts.u.derived->components->ts;
6741 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
6743 gfc_code *init_st = gfc_get_code ();
6744 init_st->loc = code->loc;
6745 init_st->op = EXEC_INIT_ASSIGN;
6746 init_st->expr1 = gfc_expr_to_initialize (e);
6747 init_st->expr2 = init_e;
6748 init_st->next = code->next;
6749 code->next = init_st;
6752 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
6754 /* Default initialization via MOLD (non-polymorphic). */
6755 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
6756 gfc_resolve_expr (rhs);
6757 gfc_free_expr (code->expr3);
6758 code->expr3 = rhs;
6761 if (e->ts.type == BT_CLASS)
6763 /* Make sure the vtab symbol is present when
6764 the module variables are generated. */
6765 gfc_typespec ts = e->ts;
6766 if (code->expr3)
6767 ts = code->expr3->ts;
6768 else if (code->ext.alloc.ts.type == BT_DERIVED)
6769 ts = code->ext.alloc.ts;
6770 gfc_find_derived_vtab (ts.u.derived);
6773 if (pointer || (dimension == 0 && codimension == 0))
6774 goto success;
6776 /* Make sure the last reference node is an array specifiction. */
6778 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6779 || (dimension && ref2->u.ar.dimen == 0))
6781 gfc_error ("Array specification required in ALLOCATE statement "
6782 "at %L", &e->where);
6783 goto failure;
6786 /* Make sure that the array section reference makes sense in the
6787 context of an ALLOCATE specification. */
6789 ar = &ref2->u.ar;
6791 if (codimension && ar->codimen == 0)
6793 gfc_error ("Coarray specification required in ALLOCATE statement "
6794 "at %L", &e->where);
6795 goto failure;
6798 for (i = 0; i < ar->dimen; i++)
6800 if (ref2->u.ar.type == AR_ELEMENT)
6801 goto check_symbols;
6803 switch (ar->dimen_type[i])
6805 case DIMEN_ELEMENT:
6806 break;
6808 case DIMEN_RANGE:
6809 if (ar->start[i] != NULL
6810 && ar->end[i] != NULL
6811 && ar->stride[i] == NULL)
6812 break;
6814 /* Fall Through... */
6816 case DIMEN_UNKNOWN:
6817 case DIMEN_VECTOR:
6818 case DIMEN_STAR:
6819 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6820 &e->where);
6821 goto failure;
6824 check_symbols:
6825 for (a = code->ext.alloc.list; a; a = a->next)
6827 sym = a->expr->symtree->n.sym;
6829 /* TODO - check derived type components. */
6830 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6831 continue;
6833 if ((ar->start[i] != NULL
6834 && gfc_find_sym_in_expr (sym, ar->start[i]))
6835 || (ar->end[i] != NULL
6836 && gfc_find_sym_in_expr (sym, ar->end[i])))
6838 gfc_error ("'%s' must not appear in the array specification at "
6839 "%L in the same ALLOCATE statement where it is "
6840 "itself allocated", sym->name, &ar->where);
6841 goto failure;
6846 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
6848 if (ar->dimen_type[i] == DIMEN_ELEMENT
6849 || ar->dimen_type[i] == DIMEN_RANGE)
6851 if (i == (ar->dimen + ar->codimen - 1))
6853 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
6854 "statement at %L", &e->where);
6855 goto failure;
6857 break;
6860 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
6861 && ar->stride[i] == NULL)
6862 break;
6864 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
6865 &e->where);
6866 goto failure;
6869 if (codimension && ar->as->rank == 0)
6871 gfc_error ("Sorry, allocatable scalar coarrays are not yet supported "
6872 "at %L", &e->where);
6873 goto failure;
6876 success:
6877 if (e->ts.deferred)
6879 gfc_error ("Support for entity at %L with deferred type parameter "
6880 "not yet implemented", &e->where);
6881 return FAILURE;
6883 return SUCCESS;
6885 failure:
6886 return FAILURE;
6889 static void
6890 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6892 gfc_expr *stat, *errmsg, *pe, *qe;
6893 gfc_alloc *a, *p, *q;
6895 stat = code->expr1;
6896 errmsg = code->expr2;
6898 /* Check the stat variable. */
6899 if (stat)
6901 gfc_check_vardef_context (stat, false, _("STAT variable"));
6903 if ((stat->ts.type != BT_INTEGER
6904 && !(stat->ref && (stat->ref->type == REF_ARRAY
6905 || stat->ref->type == REF_COMPONENT)))
6906 || stat->rank > 0)
6907 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6908 "variable", &stat->where);
6910 for (p = code->ext.alloc.list; p; p = p->next)
6911 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
6913 gfc_ref *ref1, *ref2;
6914 bool found = true;
6916 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
6917 ref1 = ref1->next, ref2 = ref2->next)
6919 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
6920 continue;
6921 if (ref1->u.c.component->name != ref2->u.c.component->name)
6923 found = false;
6924 break;
6928 if (found)
6930 gfc_error ("Stat-variable at %L shall not be %sd within "
6931 "the same %s statement", &stat->where, fcn, fcn);
6932 break;
6937 /* Check the errmsg variable. */
6938 if (errmsg)
6940 if (!stat)
6941 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6942 &errmsg->where);
6944 gfc_check_vardef_context (errmsg, false, _("ERRMSG variable"));
6946 if ((errmsg->ts.type != BT_CHARACTER
6947 && !(errmsg->ref
6948 && (errmsg->ref->type == REF_ARRAY
6949 || errmsg->ref->type == REF_COMPONENT)))
6950 || errmsg->rank > 0 )
6951 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
6952 "variable", &errmsg->where);
6954 for (p = code->ext.alloc.list; p; p = p->next)
6955 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
6957 gfc_ref *ref1, *ref2;
6958 bool found = true;
6960 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
6961 ref1 = ref1->next, ref2 = ref2->next)
6963 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
6964 continue;
6965 if (ref1->u.c.component->name != ref2->u.c.component->name)
6967 found = false;
6968 break;
6972 if (found)
6974 gfc_error ("Errmsg-variable at %L shall not be %sd within "
6975 "the same %s statement", &errmsg->where, fcn, fcn);
6976 break;
6981 /* Check that an allocate-object appears only once in the statement.
6982 FIXME: Checking derived types is disabled. */
6983 for (p = code->ext.alloc.list; p; p = p->next)
6985 pe = p->expr;
6986 for (q = p->next; q; q = q->next)
6988 qe = q->expr;
6989 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
6991 /* This is a potential collision. */
6992 gfc_ref *pr = pe->ref;
6993 gfc_ref *qr = qe->ref;
6995 /* Follow the references until
6996 a) They start to differ, in which case there is no error;
6997 you can deallocate a%b and a%c in a single statement
6998 b) Both of them stop, which is an error
6999 c) One of them stops, which is also an error. */
7000 while (1)
7002 if (pr == NULL && qr == NULL)
7004 gfc_error ("Allocate-object at %L also appears at %L",
7005 &pe->where, &qe->where);
7006 break;
7008 else if (pr != NULL && qr == NULL)
7010 gfc_error ("Allocate-object at %L is subobject of"
7011 " object at %L", &pe->where, &qe->where);
7012 break;
7014 else if (pr == NULL && qr != NULL)
7016 gfc_error ("Allocate-object at %L is subobject of"
7017 " object at %L", &qe->where, &pe->where);
7018 break;
7020 /* Here, pr != NULL && qr != NULL */
7021 gcc_assert(pr->type == qr->type);
7022 if (pr->type == REF_ARRAY)
7024 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7025 which are legal. */
7026 gcc_assert (qr->type == REF_ARRAY);
7028 if (pr->next && qr->next)
7030 gfc_array_ref *par = &(pr->u.ar);
7031 gfc_array_ref *qar = &(qr->u.ar);
7032 if (gfc_dep_compare_expr (par->start[0],
7033 qar->start[0]) != 0)
7034 break;
7037 else
7039 if (pr->u.c.component->name != qr->u.c.component->name)
7040 break;
7043 pr = pr->next;
7044 qr = qr->next;
7050 if (strcmp (fcn, "ALLOCATE") == 0)
7052 for (a = code->ext.alloc.list; a; a = a->next)
7053 resolve_allocate_expr (a->expr, code);
7055 else
7057 for (a = code->ext.alloc.list; a; a = a->next)
7058 resolve_deallocate_expr (a->expr);
7063 /************ SELECT CASE resolution subroutines ************/
7065 /* Callback function for our mergesort variant. Determines interval
7066 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7067 op1 > op2. Assumes we're not dealing with the default case.
7068 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7069 There are nine situations to check. */
7071 static int
7072 compare_cases (const gfc_case *op1, const gfc_case *op2)
7074 int retval;
7076 if (op1->low == NULL) /* op1 = (:L) */
7078 /* op2 = (:N), so overlap. */
7079 retval = 0;
7080 /* op2 = (M:) or (M:N), L < M */
7081 if (op2->low != NULL
7082 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7083 retval = -1;
7085 else if (op1->high == NULL) /* op1 = (K:) */
7087 /* op2 = (M:), so overlap. */
7088 retval = 0;
7089 /* op2 = (:N) or (M:N), K > N */
7090 if (op2->high != NULL
7091 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7092 retval = 1;
7094 else /* op1 = (K:L) */
7096 if (op2->low == NULL) /* op2 = (:N), K > N */
7097 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7098 ? 1 : 0;
7099 else if (op2->high == NULL) /* op2 = (M:), L < M */
7100 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7101 ? -1 : 0;
7102 else /* op2 = (M:N) */
7104 retval = 0;
7105 /* L < M */
7106 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7107 retval = -1;
7108 /* K > N */
7109 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7110 retval = 1;
7114 return retval;
7118 /* Merge-sort a double linked case list, detecting overlap in the
7119 process. LIST is the head of the double linked case list before it
7120 is sorted. Returns the head of the sorted list if we don't see any
7121 overlap, or NULL otherwise. */
7123 static gfc_case *
7124 check_case_overlap (gfc_case *list)
7126 gfc_case *p, *q, *e, *tail;
7127 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7129 /* If the passed list was empty, return immediately. */
7130 if (!list)
7131 return NULL;
7133 overlap_seen = 0;
7134 insize = 1;
7136 /* Loop unconditionally. The only exit from this loop is a return
7137 statement, when we've finished sorting the case list. */
7138 for (;;)
7140 p = list;
7141 list = NULL;
7142 tail = NULL;
7144 /* Count the number of merges we do in this pass. */
7145 nmerges = 0;
7147 /* Loop while there exists a merge to be done. */
7148 while (p)
7150 int i;
7152 /* Count this merge. */
7153 nmerges++;
7155 /* Cut the list in two pieces by stepping INSIZE places
7156 forward in the list, starting from P. */
7157 psize = 0;
7158 q = p;
7159 for (i = 0; i < insize; i++)
7161 psize++;
7162 q = q->right;
7163 if (!q)
7164 break;
7166 qsize = insize;
7168 /* Now we have two lists. Merge them! */
7169 while (psize > 0 || (qsize > 0 && q != NULL))
7171 /* See from which the next case to merge comes from. */
7172 if (psize == 0)
7174 /* P is empty so the next case must come from Q. */
7175 e = q;
7176 q = q->right;
7177 qsize--;
7179 else if (qsize == 0 || q == NULL)
7181 /* Q is empty. */
7182 e = p;
7183 p = p->right;
7184 psize--;
7186 else
7188 cmp = compare_cases (p, q);
7189 if (cmp < 0)
7191 /* The whole case range for P is less than the
7192 one for Q. */
7193 e = p;
7194 p = p->right;
7195 psize--;
7197 else if (cmp > 0)
7199 /* The whole case range for Q is greater than
7200 the case range for P. */
7201 e = q;
7202 q = q->right;
7203 qsize--;
7205 else
7207 /* The cases overlap, or they are the same
7208 element in the list. Either way, we must
7209 issue an error and get the next case from P. */
7210 /* FIXME: Sort P and Q by line number. */
7211 gfc_error ("CASE label at %L overlaps with CASE "
7212 "label at %L", &p->where, &q->where);
7213 overlap_seen = 1;
7214 e = p;
7215 p = p->right;
7216 psize--;
7220 /* Add the next element to the merged list. */
7221 if (tail)
7222 tail->right = e;
7223 else
7224 list = e;
7225 e->left = tail;
7226 tail = e;
7229 /* P has now stepped INSIZE places along, and so has Q. So
7230 they're the same. */
7231 p = q;
7233 tail->right = NULL;
7235 /* If we have done only one merge or none at all, we've
7236 finished sorting the cases. */
7237 if (nmerges <= 1)
7239 if (!overlap_seen)
7240 return list;
7241 else
7242 return NULL;
7245 /* Otherwise repeat, merging lists twice the size. */
7246 insize *= 2;
7251 /* Check to see if an expression is suitable for use in a CASE statement.
7252 Makes sure that all case expressions are scalar constants of the same
7253 type. Return FAILURE if anything is wrong. */
7255 static gfc_try
7256 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7258 if (e == NULL) return SUCCESS;
7260 if (e->ts.type != case_expr->ts.type)
7262 gfc_error ("Expression in CASE statement at %L must be of type %s",
7263 &e->where, gfc_basic_typename (case_expr->ts.type));
7264 return FAILURE;
7267 /* C805 (R808) For a given case-construct, each case-value shall be of
7268 the same type as case-expr. For character type, length differences
7269 are allowed, but the kind type parameters shall be the same. */
7271 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7273 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7274 &e->where, case_expr->ts.kind);
7275 return FAILURE;
7278 /* Convert the case value kind to that of case expression kind,
7279 if needed */
7281 if (e->ts.kind != case_expr->ts.kind)
7282 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7284 if (e->rank != 0)
7286 gfc_error ("Expression in CASE statement at %L must be scalar",
7287 &e->where);
7288 return FAILURE;
7291 return SUCCESS;
7295 /* Given a completely parsed select statement, we:
7297 - Validate all expressions and code within the SELECT.
7298 - Make sure that the selection expression is not of the wrong type.
7299 - Make sure that no case ranges overlap.
7300 - Eliminate unreachable cases and unreachable code resulting from
7301 removing case labels.
7303 The standard does allow unreachable cases, e.g. CASE (5:3). But
7304 they are a hassle for code generation, and to prevent that, we just
7305 cut them out here. This is not necessary for overlapping cases
7306 because they are illegal and we never even try to generate code.
7308 We have the additional caveat that a SELECT construct could have
7309 been a computed GOTO in the source code. Fortunately we can fairly
7310 easily work around that here: The case_expr for a "real" SELECT CASE
7311 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7312 we have to do is make sure that the case_expr is a scalar integer
7313 expression. */
7315 static void
7316 resolve_select (gfc_code *code)
7318 gfc_code *body;
7319 gfc_expr *case_expr;
7320 gfc_case *cp, *default_case, *tail, *head;
7321 int seen_unreachable;
7322 int seen_logical;
7323 int ncases;
7324 bt type;
7325 gfc_try t;
7327 if (code->expr1 == NULL)
7329 /* This was actually a computed GOTO statement. */
7330 case_expr = code->expr2;
7331 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7332 gfc_error ("Selection expression in computed GOTO statement "
7333 "at %L must be a scalar integer expression",
7334 &case_expr->where);
7336 /* Further checking is not necessary because this SELECT was built
7337 by the compiler, so it should always be OK. Just move the
7338 case_expr from expr2 to expr so that we can handle computed
7339 GOTOs as normal SELECTs from here on. */
7340 code->expr1 = code->expr2;
7341 code->expr2 = NULL;
7342 return;
7345 case_expr = code->expr1;
7347 type = case_expr->ts.type;
7348 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7350 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7351 &case_expr->where, gfc_typename (&case_expr->ts));
7353 /* Punt. Going on here just produce more garbage error messages. */
7354 return;
7357 if (case_expr->rank != 0)
7359 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7360 "expression", &case_expr->where);
7362 /* Punt. */
7363 return;
7367 /* Raise a warning if an INTEGER case value exceeds the range of
7368 the case-expr. Later, all expressions will be promoted to the
7369 largest kind of all case-labels. */
7371 if (type == BT_INTEGER)
7372 for (body = code->block; body; body = body->block)
7373 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7375 if (cp->low
7376 && gfc_check_integer_range (cp->low->value.integer,
7377 case_expr->ts.kind) != ARITH_OK)
7378 gfc_warning ("Expression in CASE statement at %L is "
7379 "not in the range of %s", &cp->low->where,
7380 gfc_typename (&case_expr->ts));
7382 if (cp->high
7383 && cp->low != cp->high
7384 && gfc_check_integer_range (cp->high->value.integer,
7385 case_expr->ts.kind) != ARITH_OK)
7386 gfc_warning ("Expression in CASE statement at %L is "
7387 "not in the range of %s", &cp->high->where,
7388 gfc_typename (&case_expr->ts));
7391 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7392 of the SELECT CASE expression and its CASE values. Walk the lists
7393 of case values, and if we find a mismatch, promote case_expr to
7394 the appropriate kind. */
7396 if (type == BT_LOGICAL || type == BT_INTEGER)
7398 for (body = code->block; body; body = body->block)
7400 /* Walk the case label list. */
7401 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7403 /* Intercept the DEFAULT case. It does not have a kind. */
7404 if (cp->low == NULL && cp->high == NULL)
7405 continue;
7407 /* Unreachable case ranges are discarded, so ignore. */
7408 if (cp->low != NULL && cp->high != NULL
7409 && cp->low != cp->high
7410 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7411 continue;
7413 if (cp->low != NULL
7414 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7415 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7417 if (cp->high != NULL
7418 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7419 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7424 /* Assume there is no DEFAULT case. */
7425 default_case = NULL;
7426 head = tail = NULL;
7427 ncases = 0;
7428 seen_logical = 0;
7430 for (body = code->block; body; body = body->block)
7432 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7433 t = SUCCESS;
7434 seen_unreachable = 0;
7436 /* Walk the case label list, making sure that all case labels
7437 are legal. */
7438 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7440 /* Count the number of cases in the whole construct. */
7441 ncases++;
7443 /* Intercept the DEFAULT case. */
7444 if (cp->low == NULL && cp->high == NULL)
7446 if (default_case != NULL)
7448 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7449 "by a second DEFAULT CASE at %L",
7450 &default_case->where, &cp->where);
7451 t = FAILURE;
7452 break;
7454 else
7456 default_case = cp;
7457 continue;
7461 /* Deal with single value cases and case ranges. Errors are
7462 issued from the validation function. */
7463 if (validate_case_label_expr (cp->low, case_expr) != SUCCESS
7464 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
7466 t = FAILURE;
7467 break;
7470 if (type == BT_LOGICAL
7471 && ((cp->low == NULL || cp->high == NULL)
7472 || cp->low != cp->high))
7474 gfc_error ("Logical range in CASE statement at %L is not "
7475 "allowed", &cp->low->where);
7476 t = FAILURE;
7477 break;
7480 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7482 int value;
7483 value = cp->low->value.logical == 0 ? 2 : 1;
7484 if (value & seen_logical)
7486 gfc_error ("Constant logical value in CASE statement "
7487 "is repeated at %L",
7488 &cp->low->where);
7489 t = FAILURE;
7490 break;
7492 seen_logical |= value;
7495 if (cp->low != NULL && cp->high != NULL
7496 && cp->low != cp->high
7497 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7499 if (gfc_option.warn_surprising)
7500 gfc_warning ("Range specification at %L can never "
7501 "be matched", &cp->where);
7503 cp->unreachable = 1;
7504 seen_unreachable = 1;
7506 else
7508 /* If the case range can be matched, it can also overlap with
7509 other cases. To make sure it does not, we put it in a
7510 double linked list here. We sort that with a merge sort
7511 later on to detect any overlapping cases. */
7512 if (!head)
7514 head = tail = cp;
7515 head->right = head->left = NULL;
7517 else
7519 tail->right = cp;
7520 tail->right->left = tail;
7521 tail = tail->right;
7522 tail->right = NULL;
7527 /* It there was a failure in the previous case label, give up
7528 for this case label list. Continue with the next block. */
7529 if (t == FAILURE)
7530 continue;
7532 /* See if any case labels that are unreachable have been seen.
7533 If so, we eliminate them. This is a bit of a kludge because
7534 the case lists for a single case statement (label) is a
7535 single forward linked lists. */
7536 if (seen_unreachable)
7538 /* Advance until the first case in the list is reachable. */
7539 while (body->ext.block.case_list != NULL
7540 && body->ext.block.case_list->unreachable)
7542 gfc_case *n = body->ext.block.case_list;
7543 body->ext.block.case_list = body->ext.block.case_list->next;
7544 n->next = NULL;
7545 gfc_free_case_list (n);
7548 /* Strip all other unreachable cases. */
7549 if (body->ext.block.case_list)
7551 for (cp = body->ext.block.case_list; cp->next; cp = cp->next)
7553 if (cp->next->unreachable)
7555 gfc_case *n = cp->next;
7556 cp->next = cp->next->next;
7557 n->next = NULL;
7558 gfc_free_case_list (n);
7565 /* See if there were overlapping cases. If the check returns NULL,
7566 there was overlap. In that case we don't do anything. If head
7567 is non-NULL, we prepend the DEFAULT case. The sorted list can
7568 then used during code generation for SELECT CASE constructs with
7569 a case expression of a CHARACTER type. */
7570 if (head)
7572 head = check_case_overlap (head);
7574 /* Prepend the default_case if it is there. */
7575 if (head != NULL && default_case)
7577 default_case->left = NULL;
7578 default_case->right = head;
7579 head->left = default_case;
7583 /* Eliminate dead blocks that may be the result if we've seen
7584 unreachable case labels for a block. */
7585 for (body = code; body && body->block; body = body->block)
7587 if (body->block->ext.block.case_list == NULL)
7589 /* Cut the unreachable block from the code chain. */
7590 gfc_code *c = body->block;
7591 body->block = c->block;
7593 /* Kill the dead block, but not the blocks below it. */
7594 c->block = NULL;
7595 gfc_free_statements (c);
7599 /* More than two cases is legal but insane for logical selects.
7600 Issue a warning for it. */
7601 if (gfc_option.warn_surprising && type == BT_LOGICAL
7602 && ncases > 2)
7603 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7604 &code->loc);
7608 /* Check if a derived type is extensible. */
7610 bool
7611 gfc_type_is_extensible (gfc_symbol *sym)
7613 return !(sym->attr.is_bind_c || sym->attr.sequence);
7617 /* Resolve an associate name: Resolve target and ensure the type-spec is
7618 correct as well as possibly the array-spec. */
7620 static void
7621 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7623 gfc_expr* target;
7625 gcc_assert (sym->assoc);
7626 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7628 /* If this is for SELECT TYPE, the target may not yet be set. In that
7629 case, return. Resolution will be called later manually again when
7630 this is done. */
7631 target = sym->assoc->target;
7632 if (!target)
7633 return;
7634 gcc_assert (!sym->assoc->dangling);
7636 if (resolve_target && gfc_resolve_expr (target) != SUCCESS)
7637 return;
7639 /* For variable targets, we get some attributes from the target. */
7640 if (target->expr_type == EXPR_VARIABLE)
7642 gfc_symbol* tsym;
7644 gcc_assert (target->symtree);
7645 tsym = target->symtree->n.sym;
7647 sym->attr.asynchronous = tsym->attr.asynchronous;
7648 sym->attr.volatile_ = tsym->attr.volatile_;
7650 sym->attr.target = (tsym->attr.target || tsym->attr.pointer);
7653 /* Get type if this was not already set. Note that it can be
7654 some other type than the target in case this is a SELECT TYPE
7655 selector! So we must not update when the type is already there. */
7656 if (sym->ts.type == BT_UNKNOWN)
7657 sym->ts = target->ts;
7658 gcc_assert (sym->ts.type != BT_UNKNOWN);
7660 /* See if this is a valid association-to-variable. */
7661 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7662 && !gfc_has_vector_subscript (target));
7664 /* Finally resolve if this is an array or not. */
7665 if (sym->attr.dimension && target->rank == 0)
7667 gfc_error ("Associate-name '%s' at %L is used as array",
7668 sym->name, &sym->declared_at);
7669 sym->attr.dimension = 0;
7670 return;
7672 if (target->rank > 0)
7673 sym->attr.dimension = 1;
7675 if (sym->attr.dimension)
7677 sym->as = gfc_get_array_spec ();
7678 sym->as->rank = target->rank;
7679 sym->as->type = AS_DEFERRED;
7681 /* Target must not be coindexed, thus the associate-variable
7682 has no corank. */
7683 sym->as->corank = 0;
7688 /* Resolve a SELECT TYPE statement. */
7690 static void
7691 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
7693 gfc_symbol *selector_type;
7694 gfc_code *body, *new_st, *if_st, *tail;
7695 gfc_code *class_is = NULL, *default_case = NULL;
7696 gfc_case *c;
7697 gfc_symtree *st;
7698 char name[GFC_MAX_SYMBOL_LEN];
7699 gfc_namespace *ns;
7700 int error = 0;
7702 ns = code->ext.block.ns;
7703 gfc_resolve (ns);
7705 /* Check for F03:C813. */
7706 if (code->expr1->ts.type != BT_CLASS
7707 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7709 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7710 "at %L", &code->loc);
7711 return;
7714 if (code->expr2)
7716 if (code->expr1->symtree->n.sym->attr.untyped)
7717 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7718 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7720 else
7721 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7723 /* Loop over TYPE IS / CLASS IS cases. */
7724 for (body = code->block; body; body = body->block)
7726 c = body->ext.block.case_list;
7728 /* Check F03:C815. */
7729 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7730 && !gfc_type_is_extensible (c->ts.u.derived))
7732 gfc_error ("Derived type '%s' at %L must be extensible",
7733 c->ts.u.derived->name, &c->where);
7734 error++;
7735 continue;
7738 /* Check F03:C816. */
7739 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7740 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
7742 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7743 c->ts.u.derived->name, &c->where, selector_type->name);
7744 error++;
7745 continue;
7748 /* Intercept the DEFAULT case. */
7749 if (c->ts.type == BT_UNKNOWN)
7751 /* Check F03:C818. */
7752 if (default_case)
7754 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7755 "by a second DEFAULT CASE at %L",
7756 &default_case->ext.block.case_list->where, &c->where);
7757 error++;
7758 continue;
7761 default_case = body;
7765 if (error > 0)
7766 return;
7768 /* Transform SELECT TYPE statement to BLOCK and associate selector to
7769 target if present. If there are any EXIT statements referring to the
7770 SELECT TYPE construct, this is no problem because the gfc_code
7771 reference stays the same and EXIT is equally possible from the BLOCK
7772 it is changed to. */
7773 code->op = EXEC_BLOCK;
7774 if (code->expr2)
7776 gfc_association_list* assoc;
7778 assoc = gfc_get_association_list ();
7779 assoc->st = code->expr1->symtree;
7780 assoc->target = gfc_copy_expr (code->expr2);
7781 /* assoc->variable will be set by resolve_assoc_var. */
7783 code->ext.block.assoc = assoc;
7784 code->expr1->symtree->n.sym->assoc = assoc;
7786 resolve_assoc_var (code->expr1->symtree->n.sym, false);
7788 else
7789 code->ext.block.assoc = NULL;
7791 /* Add EXEC_SELECT to switch on type. */
7792 new_st = gfc_get_code ();
7793 new_st->op = code->op;
7794 new_st->expr1 = code->expr1;
7795 new_st->expr2 = code->expr2;
7796 new_st->block = code->block;
7797 code->expr1 = code->expr2 = NULL;
7798 code->block = NULL;
7799 if (!ns->code)
7800 ns->code = new_st;
7801 else
7802 ns->code->next = new_st;
7803 code = new_st;
7804 code->op = EXEC_SELECT;
7805 gfc_add_vptr_component (code->expr1);
7806 gfc_add_hash_component (code->expr1);
7808 /* Loop over TYPE IS / CLASS IS cases. */
7809 for (body = code->block; body; body = body->block)
7811 c = body->ext.block.case_list;
7813 if (c->ts.type == BT_DERIVED)
7814 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
7815 c->ts.u.derived->hash_value);
7817 else if (c->ts.type == BT_UNKNOWN)
7818 continue;
7820 /* Associate temporary to selector. This should only be done
7821 when this case is actually true, so build a new ASSOCIATE
7822 that does precisely this here (instead of using the
7823 'global' one). */
7825 if (c->ts.type == BT_CLASS)
7826 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
7827 else
7828 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
7829 st = gfc_find_symtree (ns->sym_root, name);
7830 gcc_assert (st->n.sym->assoc);
7831 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
7832 if (c->ts.type == BT_DERIVED)
7833 gfc_add_data_component (st->n.sym->assoc->target);
7835 new_st = gfc_get_code ();
7836 new_st->op = EXEC_BLOCK;
7837 new_st->ext.block.ns = gfc_build_block_ns (ns);
7838 new_st->ext.block.ns->code = body->next;
7839 body->next = new_st;
7841 /* Chain in the new list only if it is marked as dangling. Otherwise
7842 there is a CASE label overlap and this is already used. Just ignore,
7843 the error is diagonsed elsewhere. */
7844 if (st->n.sym->assoc->dangling)
7846 new_st->ext.block.assoc = st->n.sym->assoc;
7847 st->n.sym->assoc->dangling = 0;
7850 resolve_assoc_var (st->n.sym, false);
7853 /* Take out CLASS IS cases for separate treatment. */
7854 body = code;
7855 while (body && body->block)
7857 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
7859 /* Add to class_is list. */
7860 if (class_is == NULL)
7862 class_is = body->block;
7863 tail = class_is;
7865 else
7867 for (tail = class_is; tail->block; tail = tail->block) ;
7868 tail->block = body->block;
7869 tail = tail->block;
7871 /* Remove from EXEC_SELECT list. */
7872 body->block = body->block->block;
7873 tail->block = NULL;
7875 else
7876 body = body->block;
7879 if (class_is)
7881 gfc_symbol *vtab;
7883 if (!default_case)
7885 /* Add a default case to hold the CLASS IS cases. */
7886 for (tail = code; tail->block; tail = tail->block) ;
7887 tail->block = gfc_get_code ();
7888 tail = tail->block;
7889 tail->op = EXEC_SELECT_TYPE;
7890 tail->ext.block.case_list = gfc_get_case ();
7891 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
7892 tail->next = NULL;
7893 default_case = tail;
7896 /* More than one CLASS IS block? */
7897 if (class_is->block)
7899 gfc_code **c1,*c2;
7900 bool swapped;
7901 /* Sort CLASS IS blocks by extension level. */
7904 swapped = false;
7905 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
7907 c2 = (*c1)->block;
7908 /* F03:C817 (check for doubles). */
7909 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
7910 == c2->ext.block.case_list->ts.u.derived->hash_value)
7912 gfc_error ("Double CLASS IS block in SELECT TYPE "
7913 "statement at %L",
7914 &c2->ext.block.case_list->where);
7915 return;
7917 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
7918 < c2->ext.block.case_list->ts.u.derived->attr.extension)
7920 /* Swap. */
7921 (*c1)->block = c2->block;
7922 c2->block = *c1;
7923 *c1 = c2;
7924 swapped = true;
7928 while (swapped);
7931 /* Generate IF chain. */
7932 if_st = gfc_get_code ();
7933 if_st->op = EXEC_IF;
7934 new_st = if_st;
7935 for (body = class_is; body; body = body->block)
7937 new_st->block = gfc_get_code ();
7938 new_st = new_st->block;
7939 new_st->op = EXEC_IF;
7940 /* Set up IF condition: Call _gfortran_is_extension_of. */
7941 new_st->expr1 = gfc_get_expr ();
7942 new_st->expr1->expr_type = EXPR_FUNCTION;
7943 new_st->expr1->ts.type = BT_LOGICAL;
7944 new_st->expr1->ts.kind = 4;
7945 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
7946 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
7947 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
7948 /* Set up arguments. */
7949 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
7950 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
7951 new_st->expr1->value.function.actual->expr->where = code->loc;
7952 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
7953 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
7954 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
7955 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
7956 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
7957 new_st->next = body->next;
7959 if (default_case->next)
7961 new_st->block = gfc_get_code ();
7962 new_st = new_st->block;
7963 new_st->op = EXEC_IF;
7964 new_st->next = default_case->next;
7967 /* Replace CLASS DEFAULT code by the IF chain. */
7968 default_case->next = if_st;
7971 /* Resolve the internal code. This can not be done earlier because
7972 it requires that the sym->assoc of selectors is set already. */
7973 gfc_current_ns = ns;
7974 gfc_resolve_blocks (code->block, gfc_current_ns);
7975 gfc_current_ns = old_ns;
7977 resolve_select (code);
7981 /* Resolve a transfer statement. This is making sure that:
7982 -- a derived type being transferred has only non-pointer components
7983 -- a derived type being transferred doesn't have private components, unless
7984 it's being transferred from the module where the type was defined
7985 -- we're not trying to transfer a whole assumed size array. */
7987 static void
7988 resolve_transfer (gfc_code *code)
7990 gfc_typespec *ts;
7991 gfc_symbol *sym;
7992 gfc_ref *ref;
7993 gfc_expr *exp;
7995 exp = code->expr1;
7997 while (exp != NULL && exp->expr_type == EXPR_OP
7998 && exp->value.op.op == INTRINSIC_PARENTHESES)
7999 exp = exp->value.op.op1;
8001 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8002 && exp->expr_type != EXPR_FUNCTION))
8003 return;
8005 /* If we are reading, the variable will be changed. Note that
8006 code->ext.dt may be NULL if the TRANSFER is related to
8007 an INQUIRE statement -- but in this case, we are not reading, either. */
8008 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8009 && gfc_check_vardef_context (exp, false, _("item in READ")) == FAILURE)
8010 return;
8012 sym = exp->symtree->n.sym;
8013 ts = &sym->ts;
8015 /* Go to actual component transferred. */
8016 for (ref = exp->ref; ref; ref = ref->next)
8017 if (ref->type == REF_COMPONENT)
8018 ts = &ref->u.c.component->ts;
8020 if (ts->type == BT_CLASS)
8022 /* FIXME: Test for defined input/output. */
8023 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8024 "it is processed by a defined input/output procedure",
8025 &code->loc);
8026 return;
8029 if (ts->type == BT_DERIVED)
8031 /* Check that transferred derived type doesn't contain POINTER
8032 components. */
8033 if (ts->u.derived->attr.pointer_comp)
8035 gfc_error ("Data transfer element at %L cannot have "
8036 "POINTER components", &code->loc);
8037 return;
8040 if (ts->u.derived->attr.alloc_comp)
8042 gfc_error ("Data transfer element at %L cannot have "
8043 "ALLOCATABLE components", &code->loc);
8044 return;
8047 if (derived_inaccessible (ts->u.derived))
8049 gfc_error ("Data transfer element at %L cannot have "
8050 "PRIVATE components",&code->loc);
8051 return;
8055 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
8056 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8058 gfc_error ("Data transfer element at %L cannot be a full reference to "
8059 "an assumed-size array", &code->loc);
8060 return;
8065 /*********** Toplevel code resolution subroutines ***********/
8067 /* Find the set of labels that are reachable from this block. We also
8068 record the last statement in each block. */
8070 static void
8071 find_reachable_labels (gfc_code *block)
8073 gfc_code *c;
8075 if (!block)
8076 return;
8078 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8080 /* Collect labels in this block. We don't keep those corresponding
8081 to END {IF|SELECT}, these are checked in resolve_branch by going
8082 up through the code_stack. */
8083 for (c = block; c; c = c->next)
8085 if (c->here && c->op != EXEC_END_BLOCK)
8086 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8089 /* Merge with labels from parent block. */
8090 if (cs_base->prev)
8092 gcc_assert (cs_base->prev->reachable_labels);
8093 bitmap_ior_into (cs_base->reachable_labels,
8094 cs_base->prev->reachable_labels);
8099 static void
8100 resolve_sync (gfc_code *code)
8102 /* Check imageset. The * case matches expr1 == NULL. */
8103 if (code->expr1)
8105 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8106 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8107 "INTEGER expression", &code->expr1->where);
8108 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8109 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8110 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8111 &code->expr1->where);
8112 else if (code->expr1->expr_type == EXPR_ARRAY
8113 && gfc_simplify_expr (code->expr1, 0) == SUCCESS)
8115 gfc_constructor *cons;
8116 cons = gfc_constructor_first (code->expr1->value.constructor);
8117 for (; cons; cons = gfc_constructor_next (cons))
8118 if (cons->expr->expr_type == EXPR_CONSTANT
8119 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8120 gfc_error ("Imageset argument at %L must between 1 and "
8121 "num_images()", &cons->expr->where);
8125 /* Check STAT. */
8126 if (code->expr2
8127 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8128 || code->expr2->expr_type != EXPR_VARIABLE))
8129 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8130 &code->expr2->where);
8132 /* Check ERRMSG. */
8133 if (code->expr3
8134 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8135 || code->expr3->expr_type != EXPR_VARIABLE))
8136 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8137 &code->expr3->where);
8141 /* Given a branch to a label, see if the branch is conforming.
8142 The code node describes where the branch is located. */
8144 static void
8145 resolve_branch (gfc_st_label *label, gfc_code *code)
8147 code_stack *stack;
8149 if (label == NULL)
8150 return;
8152 /* Step one: is this a valid branching target? */
8154 if (label->defined == ST_LABEL_UNKNOWN)
8156 gfc_error ("Label %d referenced at %L is never defined", label->value,
8157 &label->where);
8158 return;
8161 if (label->defined != ST_LABEL_TARGET)
8163 gfc_error ("Statement at %L is not a valid branch target statement "
8164 "for the branch statement at %L", &label->where, &code->loc);
8165 return;
8168 /* Step two: make sure this branch is not a branch to itself ;-) */
8170 if (code->here == label)
8172 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8173 return;
8176 /* Step three: See if the label is in the same block as the
8177 branching statement. The hard work has been done by setting up
8178 the bitmap reachable_labels. */
8180 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8182 /* Check now whether there is a CRITICAL construct; if so, check
8183 whether the label is still visible outside of the CRITICAL block,
8184 which is invalid. */
8185 for (stack = cs_base; stack; stack = stack->prev)
8186 if (stack->current->op == EXEC_CRITICAL
8187 && bitmap_bit_p (stack->reachable_labels, label->value))
8188 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8189 " at %L", &code->loc, &label->where);
8191 return;
8194 /* Step four: If we haven't found the label in the bitmap, it may
8195 still be the label of the END of the enclosing block, in which
8196 case we find it by going up the code_stack. */
8198 for (stack = cs_base; stack; stack = stack->prev)
8200 if (stack->current->next && stack->current->next->here == label)
8201 break;
8202 if (stack->current->op == EXEC_CRITICAL)
8204 /* Note: A label at END CRITICAL does not leave the CRITICAL
8205 construct as END CRITICAL is still part of it. */
8206 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8207 " at %L", &code->loc, &label->where);
8208 return;
8212 if (stack)
8214 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
8215 return;
8218 /* The label is not in an enclosing block, so illegal. This was
8219 allowed in Fortran 66, so we allow it as extension. No
8220 further checks are necessary in this case. */
8221 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8222 "as the GOTO statement at %L", &label->where,
8223 &code->loc);
8224 return;
8228 /* Check whether EXPR1 has the same shape as EXPR2. */
8230 static gfc_try
8231 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8233 mpz_t shape[GFC_MAX_DIMENSIONS];
8234 mpz_t shape2[GFC_MAX_DIMENSIONS];
8235 gfc_try result = FAILURE;
8236 int i;
8238 /* Compare the rank. */
8239 if (expr1->rank != expr2->rank)
8240 return result;
8242 /* Compare the size of each dimension. */
8243 for (i=0; i<expr1->rank; i++)
8245 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
8246 goto ignore;
8248 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
8249 goto ignore;
8251 if (mpz_cmp (shape[i], shape2[i]))
8252 goto over;
8255 /* When either of the two expression is an assumed size array, we
8256 ignore the comparison of dimension sizes. */
8257 ignore:
8258 result = SUCCESS;
8260 over:
8261 for (i--; i >= 0; i--)
8263 mpz_clear (shape[i]);
8264 mpz_clear (shape2[i]);
8266 return result;
8270 /* Check whether a WHERE assignment target or a WHERE mask expression
8271 has the same shape as the outmost WHERE mask expression. */
8273 static void
8274 resolve_where (gfc_code *code, gfc_expr *mask)
8276 gfc_code *cblock;
8277 gfc_code *cnext;
8278 gfc_expr *e = NULL;
8280 cblock = code->block;
8282 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8283 In case of nested WHERE, only the outmost one is stored. */
8284 if (mask == NULL) /* outmost WHERE */
8285 e = cblock->expr1;
8286 else /* inner WHERE */
8287 e = mask;
8289 while (cblock)
8291 if (cblock->expr1)
8293 /* Check if the mask-expr has a consistent shape with the
8294 outmost WHERE mask-expr. */
8295 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
8296 gfc_error ("WHERE mask at %L has inconsistent shape",
8297 &cblock->expr1->where);
8300 /* the assignment statement of a WHERE statement, or the first
8301 statement in where-body-construct of a WHERE construct */
8302 cnext = cblock->next;
8303 while (cnext)
8305 switch (cnext->op)
8307 /* WHERE assignment statement */
8308 case EXEC_ASSIGN:
8310 /* Check shape consistent for WHERE assignment target. */
8311 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
8312 gfc_error ("WHERE assignment target at %L has "
8313 "inconsistent shape", &cnext->expr1->where);
8314 break;
8317 case EXEC_ASSIGN_CALL:
8318 resolve_call (cnext);
8319 if (!cnext->resolved_sym->attr.elemental)
8320 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8321 &cnext->ext.actual->expr->where);
8322 break;
8324 /* WHERE or WHERE construct is part of a where-body-construct */
8325 case EXEC_WHERE:
8326 resolve_where (cnext, e);
8327 break;
8329 default:
8330 gfc_error ("Unsupported statement inside WHERE at %L",
8331 &cnext->loc);
8333 /* the next statement within the same where-body-construct */
8334 cnext = cnext->next;
8336 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8337 cblock = cblock->block;
8342 /* Resolve assignment in FORALL construct.
8343 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8344 FORALL index variables. */
8346 static void
8347 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8349 int n;
8351 for (n = 0; n < nvar; n++)
8353 gfc_symbol *forall_index;
8355 forall_index = var_expr[n]->symtree->n.sym;
8357 /* Check whether the assignment target is one of the FORALL index
8358 variable. */
8359 if ((code->expr1->expr_type == EXPR_VARIABLE)
8360 && (code->expr1->symtree->n.sym == forall_index))
8361 gfc_error ("Assignment to a FORALL index variable at %L",
8362 &code->expr1->where);
8363 else
8365 /* If one of the FORALL index variables doesn't appear in the
8366 assignment variable, then there could be a many-to-one
8367 assignment. Emit a warning rather than an error because the
8368 mask could be resolving this problem. */
8369 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
8370 gfc_warning ("The FORALL with index '%s' is not used on the "
8371 "left side of the assignment at %L and so might "
8372 "cause multiple assignment to this object",
8373 var_expr[n]->symtree->name, &code->expr1->where);
8379 /* Resolve WHERE statement in FORALL construct. */
8381 static void
8382 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8383 gfc_expr **var_expr)
8385 gfc_code *cblock;
8386 gfc_code *cnext;
8388 cblock = code->block;
8389 while (cblock)
8391 /* the assignment statement of a WHERE statement, or the first
8392 statement in where-body-construct of a WHERE construct */
8393 cnext = cblock->next;
8394 while (cnext)
8396 switch (cnext->op)
8398 /* WHERE assignment statement */
8399 case EXEC_ASSIGN:
8400 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8401 break;
8403 /* WHERE operator assignment statement */
8404 case EXEC_ASSIGN_CALL:
8405 resolve_call (cnext);
8406 if (!cnext->resolved_sym->attr.elemental)
8407 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8408 &cnext->ext.actual->expr->where);
8409 break;
8411 /* WHERE or WHERE construct is part of a where-body-construct */
8412 case EXEC_WHERE:
8413 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8414 break;
8416 default:
8417 gfc_error ("Unsupported statement inside WHERE at %L",
8418 &cnext->loc);
8420 /* the next statement within the same where-body-construct */
8421 cnext = cnext->next;
8423 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8424 cblock = cblock->block;
8429 /* Traverse the FORALL body to check whether the following errors exist:
8430 1. For assignment, check if a many-to-one assignment happens.
8431 2. For WHERE statement, check the WHERE body to see if there is any
8432 many-to-one assignment. */
8434 static void
8435 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8437 gfc_code *c;
8439 c = code->block->next;
8440 while (c)
8442 switch (c->op)
8444 case EXEC_ASSIGN:
8445 case EXEC_POINTER_ASSIGN:
8446 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8447 break;
8449 case EXEC_ASSIGN_CALL:
8450 resolve_call (c);
8451 break;
8453 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8454 there is no need to handle it here. */
8455 case EXEC_FORALL:
8456 break;
8457 case EXEC_WHERE:
8458 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8459 break;
8460 default:
8461 break;
8463 /* The next statement in the FORALL body. */
8464 c = c->next;
8469 /* Counts the number of iterators needed inside a forall construct, including
8470 nested forall constructs. This is used to allocate the needed memory
8471 in gfc_resolve_forall. */
8473 static int
8474 gfc_count_forall_iterators (gfc_code *code)
8476 int max_iters, sub_iters, current_iters;
8477 gfc_forall_iterator *fa;
8479 gcc_assert(code->op == EXEC_FORALL);
8480 max_iters = 0;
8481 current_iters = 0;
8483 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8484 current_iters ++;
8486 code = code->block->next;
8488 while (code)
8490 if (code->op == EXEC_FORALL)
8492 sub_iters = gfc_count_forall_iterators (code);
8493 if (sub_iters > max_iters)
8494 max_iters = sub_iters;
8496 code = code->next;
8499 return current_iters + max_iters;
8503 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8504 gfc_resolve_forall_body to resolve the FORALL body. */
8506 static void
8507 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8509 static gfc_expr **var_expr;
8510 static int total_var = 0;
8511 static int nvar = 0;
8512 int old_nvar, tmp;
8513 gfc_forall_iterator *fa;
8514 int i;
8516 old_nvar = nvar;
8518 /* Start to resolve a FORALL construct */
8519 if (forall_save == 0)
8521 /* Count the total number of FORALL index in the nested FORALL
8522 construct in order to allocate the VAR_EXPR with proper size. */
8523 total_var = gfc_count_forall_iterators (code);
8525 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8526 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
8529 /* The information about FORALL iterator, including FORALL index start, end
8530 and stride. The FORALL index can not appear in start, end or stride. */
8531 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8533 /* Check if any outer FORALL index name is the same as the current
8534 one. */
8535 for (i = 0; i < nvar; i++)
8537 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8539 gfc_error ("An outer FORALL construct already has an index "
8540 "with this name %L", &fa->var->where);
8544 /* Record the current FORALL index. */
8545 var_expr[nvar] = gfc_copy_expr (fa->var);
8547 nvar++;
8549 /* No memory leak. */
8550 gcc_assert (nvar <= total_var);
8553 /* Resolve the FORALL body. */
8554 gfc_resolve_forall_body (code, nvar, var_expr);
8556 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8557 gfc_resolve_blocks (code->block, ns);
8559 tmp = nvar;
8560 nvar = old_nvar;
8561 /* Free only the VAR_EXPRs allocated in this frame. */
8562 for (i = nvar; i < tmp; i++)
8563 gfc_free_expr (var_expr[i]);
8565 if (nvar == 0)
8567 /* We are in the outermost FORALL construct. */
8568 gcc_assert (forall_save == 0);
8570 /* VAR_EXPR is not needed any more. */
8571 gfc_free (var_expr);
8572 total_var = 0;
8577 /* Resolve a BLOCK construct statement. */
8579 static void
8580 resolve_block_construct (gfc_code* code)
8582 /* Resolve the BLOCK's namespace. */
8583 gfc_resolve (code->ext.block.ns);
8585 /* For an ASSOCIATE block, the associations (and their targets) are already
8586 resolved during resolve_symbol. */
8590 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8591 DO code nodes. */
8593 static void resolve_code (gfc_code *, gfc_namespace *);
8595 void
8596 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8598 gfc_try t;
8600 for (; b; b = b->block)
8602 t = gfc_resolve_expr (b->expr1);
8603 if (gfc_resolve_expr (b->expr2) == FAILURE)
8604 t = FAILURE;
8606 switch (b->op)
8608 case EXEC_IF:
8609 if (t == SUCCESS && b->expr1 != NULL
8610 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
8611 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8612 &b->expr1->where);
8613 break;
8615 case EXEC_WHERE:
8616 if (t == SUCCESS
8617 && b->expr1 != NULL
8618 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
8619 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8620 &b->expr1->where);
8621 break;
8623 case EXEC_GOTO:
8624 resolve_branch (b->label1, b);
8625 break;
8627 case EXEC_BLOCK:
8628 resolve_block_construct (b);
8629 break;
8631 case EXEC_SELECT:
8632 case EXEC_SELECT_TYPE:
8633 case EXEC_FORALL:
8634 case EXEC_DO:
8635 case EXEC_DO_WHILE:
8636 case EXEC_CRITICAL:
8637 case EXEC_READ:
8638 case EXEC_WRITE:
8639 case EXEC_IOLENGTH:
8640 case EXEC_WAIT:
8641 break;
8643 case EXEC_OMP_ATOMIC:
8644 case EXEC_OMP_CRITICAL:
8645 case EXEC_OMP_DO:
8646 case EXEC_OMP_MASTER:
8647 case EXEC_OMP_ORDERED:
8648 case EXEC_OMP_PARALLEL:
8649 case EXEC_OMP_PARALLEL_DO:
8650 case EXEC_OMP_PARALLEL_SECTIONS:
8651 case EXEC_OMP_PARALLEL_WORKSHARE:
8652 case EXEC_OMP_SECTIONS:
8653 case EXEC_OMP_SINGLE:
8654 case EXEC_OMP_TASK:
8655 case EXEC_OMP_TASKWAIT:
8656 case EXEC_OMP_WORKSHARE:
8657 break;
8659 default:
8660 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
8663 resolve_code (b->next, ns);
8668 /* Does everything to resolve an ordinary assignment. Returns true
8669 if this is an interface assignment. */
8670 static bool
8671 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
8673 bool rval = false;
8674 gfc_expr *lhs;
8675 gfc_expr *rhs;
8676 int llen = 0;
8677 int rlen = 0;
8678 int n;
8679 gfc_ref *ref;
8681 if (gfc_extend_assign (code, ns) == SUCCESS)
8683 gfc_expr** rhsptr;
8685 if (code->op == EXEC_ASSIGN_CALL)
8687 lhs = code->ext.actual->expr;
8688 rhsptr = &code->ext.actual->next->expr;
8690 else
8692 gfc_actual_arglist* args;
8693 gfc_typebound_proc* tbp;
8695 gcc_assert (code->op == EXEC_COMPCALL);
8697 args = code->expr1->value.compcall.actual;
8698 lhs = args->expr;
8699 rhsptr = &args->next->expr;
8701 tbp = code->expr1->value.compcall.tbp;
8702 gcc_assert (!tbp->is_generic);
8705 /* Make a temporary rhs when there is a default initializer
8706 and rhs is the same symbol as the lhs. */
8707 if ((*rhsptr)->expr_type == EXPR_VARIABLE
8708 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
8709 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
8710 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
8711 *rhsptr = gfc_get_parentheses (*rhsptr);
8713 return true;
8716 lhs = code->expr1;
8717 rhs = code->expr2;
8719 if (rhs->is_boz
8720 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
8721 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
8722 &code->loc) == FAILURE)
8723 return false;
8725 /* Handle the case of a BOZ literal on the RHS. */
8726 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
8728 int rc;
8729 if (gfc_option.warn_surprising)
8730 gfc_warning ("BOZ literal at %L is bitwise transferred "
8731 "non-integer symbol '%s'", &code->loc,
8732 lhs->symtree->n.sym->name);
8734 if (!gfc_convert_boz (rhs, &lhs->ts))
8735 return false;
8736 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
8738 if (rc == ARITH_UNDERFLOW)
8739 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
8740 ". This check can be disabled with the option "
8741 "-fno-range-check", &rhs->where);
8742 else if (rc == ARITH_OVERFLOW)
8743 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
8744 ". This check can be disabled with the option "
8745 "-fno-range-check", &rhs->where);
8746 else if (rc == ARITH_NAN)
8747 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
8748 ". This check can be disabled with the option "
8749 "-fno-range-check", &rhs->where);
8750 return false;
8754 if (lhs->ts.type == BT_CHARACTER
8755 && gfc_option.warn_character_truncation)
8757 if (lhs->ts.u.cl != NULL
8758 && lhs->ts.u.cl->length != NULL
8759 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8760 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
8762 if (rhs->expr_type == EXPR_CONSTANT)
8763 rlen = rhs->value.character.length;
8765 else if (rhs->ts.u.cl != NULL
8766 && rhs->ts.u.cl->length != NULL
8767 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8768 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
8770 if (rlen && llen && rlen > llen)
8771 gfc_warning_now ("CHARACTER expression will be truncated "
8772 "in assignment (%d/%d) at %L",
8773 llen, rlen, &code->loc);
8776 /* Ensure that a vector index expression for the lvalue is evaluated
8777 to a temporary if the lvalue symbol is referenced in it. */
8778 if (lhs->rank)
8780 for (ref = lhs->ref; ref; ref= ref->next)
8781 if (ref->type == REF_ARRAY)
8783 for (n = 0; n < ref->u.ar.dimen; n++)
8784 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
8785 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
8786 ref->u.ar.start[n]))
8787 ref->u.ar.start[n]
8788 = gfc_get_parentheses (ref->u.ar.start[n]);
8792 if (gfc_pure (NULL))
8794 if (lhs->ts.type == BT_DERIVED
8795 && lhs->expr_type == EXPR_VARIABLE
8796 && lhs->ts.u.derived->attr.pointer_comp
8797 && rhs->expr_type == EXPR_VARIABLE
8798 && (gfc_impure_variable (rhs->symtree->n.sym)
8799 || gfc_is_coindexed (rhs)))
8801 /* F2008, C1283. */
8802 if (gfc_is_coindexed (rhs))
8803 gfc_error ("Coindexed expression at %L is assigned to "
8804 "a derived type variable with a POINTER "
8805 "component in a PURE procedure",
8806 &rhs->where);
8807 else
8808 gfc_error ("The impure variable at %L is assigned to "
8809 "a derived type variable with a POINTER "
8810 "component in a PURE procedure (12.6)",
8811 &rhs->where);
8812 return rval;
8815 /* Fortran 2008, C1283. */
8816 if (gfc_is_coindexed (lhs))
8818 gfc_error ("Assignment to coindexed variable at %L in a PURE "
8819 "procedure", &rhs->where);
8820 return rval;
8824 if (gfc_implicit_pure (NULL))
8826 if (lhs->expr_type == EXPR_VARIABLE
8827 && lhs->symtree->n.sym != gfc_current_ns->proc_name
8828 && lhs->symtree->n.sym->ns != gfc_current_ns)
8829 gfc_current_ns->proc_name->attr.implicit_pure = 0;
8831 if (lhs->ts.type == BT_DERIVED
8832 && lhs->expr_type == EXPR_VARIABLE
8833 && lhs->ts.u.derived->attr.pointer_comp
8834 && rhs->expr_type == EXPR_VARIABLE
8835 && (gfc_impure_variable (rhs->symtree->n.sym)
8836 || gfc_is_coindexed (rhs)))
8837 gfc_current_ns->proc_name->attr.implicit_pure = 0;
8839 /* Fortran 2008, C1283. */
8840 if (gfc_is_coindexed (lhs))
8841 gfc_current_ns->proc_name->attr.implicit_pure = 0;
8844 /* F03:7.4.1.2. */
8845 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
8846 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
8847 if (lhs->ts.type == BT_CLASS)
8849 gfc_error ("Variable must not be polymorphic in assignment at %L",
8850 &lhs->where);
8851 return false;
8854 /* F2008, Section 7.2.1.2. */
8855 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
8857 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
8858 "component in assignment at %L", &lhs->where);
8859 return false;
8862 gfc_check_assign (lhs, rhs, 1);
8863 return false;
8867 /* Given a block of code, recursively resolve everything pointed to by this
8868 code block. */
8870 static void
8871 resolve_code (gfc_code *code, gfc_namespace *ns)
8873 int omp_workshare_save;
8874 int forall_save;
8875 code_stack frame;
8876 gfc_try t;
8878 frame.prev = cs_base;
8879 frame.head = code;
8880 cs_base = &frame;
8882 find_reachable_labels (code);
8884 for (; code; code = code->next)
8886 frame.current = code;
8887 forall_save = forall_flag;
8889 if (code->op == EXEC_FORALL)
8891 forall_flag = 1;
8892 gfc_resolve_forall (code, ns, forall_save);
8893 forall_flag = 2;
8895 else if (code->block)
8897 omp_workshare_save = -1;
8898 switch (code->op)
8900 case EXEC_OMP_PARALLEL_WORKSHARE:
8901 omp_workshare_save = omp_workshare_flag;
8902 omp_workshare_flag = 1;
8903 gfc_resolve_omp_parallel_blocks (code, ns);
8904 break;
8905 case EXEC_OMP_PARALLEL:
8906 case EXEC_OMP_PARALLEL_DO:
8907 case EXEC_OMP_PARALLEL_SECTIONS:
8908 case EXEC_OMP_TASK:
8909 omp_workshare_save = omp_workshare_flag;
8910 omp_workshare_flag = 0;
8911 gfc_resolve_omp_parallel_blocks (code, ns);
8912 break;
8913 case EXEC_OMP_DO:
8914 gfc_resolve_omp_do_blocks (code, ns);
8915 break;
8916 case EXEC_SELECT_TYPE:
8917 /* Blocks are handled in resolve_select_type because we have
8918 to transform the SELECT TYPE into ASSOCIATE first. */
8919 break;
8920 case EXEC_OMP_WORKSHARE:
8921 omp_workshare_save = omp_workshare_flag;
8922 omp_workshare_flag = 1;
8923 /* FALLTHROUGH */
8924 default:
8925 gfc_resolve_blocks (code->block, ns);
8926 break;
8929 if (omp_workshare_save != -1)
8930 omp_workshare_flag = omp_workshare_save;
8933 t = SUCCESS;
8934 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
8935 t = gfc_resolve_expr (code->expr1);
8936 forall_flag = forall_save;
8938 if (gfc_resolve_expr (code->expr2) == FAILURE)
8939 t = FAILURE;
8941 if (code->op == EXEC_ALLOCATE
8942 && gfc_resolve_expr (code->expr3) == FAILURE)
8943 t = FAILURE;
8945 switch (code->op)
8947 case EXEC_NOP:
8948 case EXEC_END_BLOCK:
8949 case EXEC_CYCLE:
8950 case EXEC_PAUSE:
8951 case EXEC_STOP:
8952 case EXEC_ERROR_STOP:
8953 case EXEC_EXIT:
8954 case EXEC_CONTINUE:
8955 case EXEC_DT_END:
8956 case EXEC_ASSIGN_CALL:
8957 case EXEC_CRITICAL:
8958 break;
8960 case EXEC_SYNC_ALL:
8961 case EXEC_SYNC_IMAGES:
8962 case EXEC_SYNC_MEMORY:
8963 resolve_sync (code);
8964 break;
8966 case EXEC_ENTRY:
8967 /* Keep track of which entry we are up to. */
8968 current_entry_id = code->ext.entry->id;
8969 break;
8971 case EXEC_WHERE:
8972 resolve_where (code, NULL);
8973 break;
8975 case EXEC_GOTO:
8976 if (code->expr1 != NULL)
8978 if (code->expr1->ts.type != BT_INTEGER)
8979 gfc_error ("ASSIGNED GOTO statement at %L requires an "
8980 "INTEGER variable", &code->expr1->where);
8981 else if (code->expr1->symtree->n.sym->attr.assign != 1)
8982 gfc_error ("Variable '%s' has not been assigned a target "
8983 "label at %L", code->expr1->symtree->n.sym->name,
8984 &code->expr1->where);
8986 else
8987 resolve_branch (code->label1, code);
8988 break;
8990 case EXEC_RETURN:
8991 if (code->expr1 != NULL
8992 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
8993 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
8994 "INTEGER return specifier", &code->expr1->where);
8995 break;
8997 case EXEC_INIT_ASSIGN:
8998 case EXEC_END_PROCEDURE:
8999 break;
9001 case EXEC_ASSIGN:
9002 if (t == FAILURE)
9003 break;
9005 if (gfc_check_vardef_context (code->expr1, false, _("assignment"))
9006 == FAILURE)
9007 break;
9009 if (resolve_ordinary_assign (code, ns))
9011 if (code->op == EXEC_COMPCALL)
9012 goto compcall;
9013 else
9014 goto call;
9016 break;
9018 case EXEC_LABEL_ASSIGN:
9019 if (code->label1->defined == ST_LABEL_UNKNOWN)
9020 gfc_error ("Label %d referenced at %L is never defined",
9021 code->label1->value, &code->label1->where);
9022 if (t == SUCCESS
9023 && (code->expr1->expr_type != EXPR_VARIABLE
9024 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
9025 || code->expr1->symtree->n.sym->ts.kind
9026 != gfc_default_integer_kind
9027 || code->expr1->symtree->n.sym->as != NULL))
9028 gfc_error ("ASSIGN statement at %L requires a scalar "
9029 "default INTEGER variable", &code->expr1->where);
9030 break;
9032 case EXEC_POINTER_ASSIGN:
9034 gfc_expr* e;
9036 if (t == FAILURE)
9037 break;
9039 /* This is both a variable definition and pointer assignment
9040 context, so check both of them. For rank remapping, a final
9041 array ref may be present on the LHS and fool gfc_expr_attr
9042 used in gfc_check_vardef_context. Remove it. */
9043 e = remove_last_array_ref (code->expr1);
9044 t = gfc_check_vardef_context (e, true, _("pointer assignment"));
9045 if (t == SUCCESS)
9046 t = gfc_check_vardef_context (e, false, _("pointer assignment"));
9047 gfc_free_expr (e);
9048 if (t == FAILURE)
9049 break;
9051 gfc_check_pointer_assign (code->expr1, code->expr2);
9052 break;
9055 case EXEC_ARITHMETIC_IF:
9056 if (t == SUCCESS
9057 && code->expr1->ts.type != BT_INTEGER
9058 && code->expr1->ts.type != BT_REAL)
9059 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9060 "expression", &code->expr1->where);
9062 resolve_branch (code->label1, code);
9063 resolve_branch (code->label2, code);
9064 resolve_branch (code->label3, code);
9065 break;
9067 case EXEC_IF:
9068 if (t == SUCCESS && code->expr1 != NULL
9069 && (code->expr1->ts.type != BT_LOGICAL
9070 || code->expr1->rank != 0))
9071 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9072 &code->expr1->where);
9073 break;
9075 case EXEC_CALL:
9076 call:
9077 resolve_call (code);
9078 break;
9080 case EXEC_COMPCALL:
9081 compcall:
9082 resolve_typebound_subroutine (code);
9083 break;
9085 case EXEC_CALL_PPC:
9086 resolve_ppc_call (code);
9087 break;
9089 case EXEC_SELECT:
9090 /* Select is complicated. Also, a SELECT construct could be
9091 a transformed computed GOTO. */
9092 resolve_select (code);
9093 break;
9095 case EXEC_SELECT_TYPE:
9096 resolve_select_type (code, ns);
9097 break;
9099 case EXEC_BLOCK:
9100 resolve_block_construct (code);
9101 break;
9103 case EXEC_DO:
9104 if (code->ext.iterator != NULL)
9106 gfc_iterator *iter = code->ext.iterator;
9107 if (gfc_resolve_iterator (iter, true) != FAILURE)
9108 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
9110 break;
9112 case EXEC_DO_WHILE:
9113 if (code->expr1 == NULL)
9114 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9115 if (t == SUCCESS
9116 && (code->expr1->rank != 0
9117 || code->expr1->ts.type != BT_LOGICAL))
9118 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9119 "a scalar LOGICAL expression", &code->expr1->where);
9120 break;
9122 case EXEC_ALLOCATE:
9123 if (t == SUCCESS)
9124 resolve_allocate_deallocate (code, "ALLOCATE");
9126 break;
9128 case EXEC_DEALLOCATE:
9129 if (t == SUCCESS)
9130 resolve_allocate_deallocate (code, "DEALLOCATE");
9132 break;
9134 case EXEC_OPEN:
9135 if (gfc_resolve_open (code->ext.open) == FAILURE)
9136 break;
9138 resolve_branch (code->ext.open->err, code);
9139 break;
9141 case EXEC_CLOSE:
9142 if (gfc_resolve_close (code->ext.close) == FAILURE)
9143 break;
9145 resolve_branch (code->ext.close->err, code);
9146 break;
9148 case EXEC_BACKSPACE:
9149 case EXEC_ENDFILE:
9150 case EXEC_REWIND:
9151 case EXEC_FLUSH:
9152 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
9153 break;
9155 resolve_branch (code->ext.filepos->err, code);
9156 break;
9158 case EXEC_INQUIRE:
9159 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
9160 break;
9162 resolve_branch (code->ext.inquire->err, code);
9163 break;
9165 case EXEC_IOLENGTH:
9166 gcc_assert (code->ext.inquire != NULL);
9167 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
9168 break;
9170 resolve_branch (code->ext.inquire->err, code);
9171 break;
9173 case EXEC_WAIT:
9174 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
9175 break;
9177 resolve_branch (code->ext.wait->err, code);
9178 resolve_branch (code->ext.wait->end, code);
9179 resolve_branch (code->ext.wait->eor, code);
9180 break;
9182 case EXEC_READ:
9183 case EXEC_WRITE:
9184 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
9185 break;
9187 resolve_branch (code->ext.dt->err, code);
9188 resolve_branch (code->ext.dt->end, code);
9189 resolve_branch (code->ext.dt->eor, code);
9190 break;
9192 case EXEC_TRANSFER:
9193 resolve_transfer (code);
9194 break;
9196 case EXEC_FORALL:
9197 resolve_forall_iterators (code->ext.forall_iterator);
9199 if (code->expr1 != NULL
9200 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
9201 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
9202 "expression", &code->expr1->where);
9203 break;
9205 case EXEC_OMP_ATOMIC:
9206 case EXEC_OMP_BARRIER:
9207 case EXEC_OMP_CRITICAL:
9208 case EXEC_OMP_FLUSH:
9209 case EXEC_OMP_DO:
9210 case EXEC_OMP_MASTER:
9211 case EXEC_OMP_ORDERED:
9212 case EXEC_OMP_SECTIONS:
9213 case EXEC_OMP_SINGLE:
9214 case EXEC_OMP_TASKWAIT:
9215 case EXEC_OMP_WORKSHARE:
9216 gfc_resolve_omp_directive (code, ns);
9217 break;
9219 case EXEC_OMP_PARALLEL:
9220 case EXEC_OMP_PARALLEL_DO:
9221 case EXEC_OMP_PARALLEL_SECTIONS:
9222 case EXEC_OMP_PARALLEL_WORKSHARE:
9223 case EXEC_OMP_TASK:
9224 omp_workshare_save = omp_workshare_flag;
9225 omp_workshare_flag = 0;
9226 gfc_resolve_omp_directive (code, ns);
9227 omp_workshare_flag = omp_workshare_save;
9228 break;
9230 default:
9231 gfc_internal_error ("resolve_code(): Bad statement code");
9235 cs_base = frame.prev;
9239 /* Resolve initial values and make sure they are compatible with
9240 the variable. */
9242 static void
9243 resolve_values (gfc_symbol *sym)
9245 gfc_try t;
9247 if (sym->value == NULL)
9248 return;
9250 if (sym->value->expr_type == EXPR_STRUCTURE)
9251 t= resolve_structure_cons (sym->value, 1);
9252 else
9253 t = gfc_resolve_expr (sym->value);
9255 if (t == FAILURE)
9256 return;
9258 gfc_check_assign_symbol (sym, sym->value);
9262 /* Verify the binding labels for common blocks that are BIND(C). The label
9263 for a BIND(C) common block must be identical in all scoping units in which
9264 the common block is declared. Further, the binding label can not collide
9265 with any other global entity in the program. */
9267 static void
9268 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
9270 if (comm_block_tree->n.common->is_bind_c == 1)
9272 gfc_gsymbol *binding_label_gsym;
9273 gfc_gsymbol *comm_name_gsym;
9275 /* See if a global symbol exists by the common block's name. It may
9276 be NULL if the common block is use-associated. */
9277 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
9278 comm_block_tree->n.common->name);
9279 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
9280 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
9281 "with the global entity '%s' at %L",
9282 comm_block_tree->n.common->binding_label,
9283 comm_block_tree->n.common->name,
9284 &(comm_block_tree->n.common->where),
9285 comm_name_gsym->name, &(comm_name_gsym->where));
9286 else if (comm_name_gsym != NULL
9287 && strcmp (comm_name_gsym->name,
9288 comm_block_tree->n.common->name) == 0)
9290 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
9291 as expected. */
9292 if (comm_name_gsym->binding_label == NULL)
9293 /* No binding label for common block stored yet; save this one. */
9294 comm_name_gsym->binding_label =
9295 comm_block_tree->n.common->binding_label;
9296 else
9297 if (strcmp (comm_name_gsym->binding_label,
9298 comm_block_tree->n.common->binding_label) != 0)
9300 /* Common block names match but binding labels do not. */
9301 gfc_error ("Binding label '%s' for common block '%s' at %L "
9302 "does not match the binding label '%s' for common "
9303 "block '%s' at %L",
9304 comm_block_tree->n.common->binding_label,
9305 comm_block_tree->n.common->name,
9306 &(comm_block_tree->n.common->where),
9307 comm_name_gsym->binding_label,
9308 comm_name_gsym->name,
9309 &(comm_name_gsym->where));
9310 return;
9314 /* There is no binding label (NAME="") so we have nothing further to
9315 check and nothing to add as a global symbol for the label. */
9316 if (comm_block_tree->n.common->binding_label[0] == '\0' )
9317 return;
9319 binding_label_gsym =
9320 gfc_find_gsymbol (gfc_gsym_root,
9321 comm_block_tree->n.common->binding_label);
9322 if (binding_label_gsym == NULL)
9324 /* Need to make a global symbol for the binding label to prevent
9325 it from colliding with another. */
9326 binding_label_gsym =
9327 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
9328 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
9329 binding_label_gsym->type = GSYM_COMMON;
9331 else
9333 /* If comm_name_gsym is NULL, the name common block is use
9334 associated and the name could be colliding. */
9335 if (binding_label_gsym->type != GSYM_COMMON)
9336 gfc_error ("Binding label '%s' for common block '%s' at %L "
9337 "collides with the global entity '%s' at %L",
9338 comm_block_tree->n.common->binding_label,
9339 comm_block_tree->n.common->name,
9340 &(comm_block_tree->n.common->where),
9341 binding_label_gsym->name,
9342 &(binding_label_gsym->where));
9343 else if (comm_name_gsym != NULL
9344 && (strcmp (binding_label_gsym->name,
9345 comm_name_gsym->binding_label) != 0)
9346 && (strcmp (binding_label_gsym->sym_name,
9347 comm_name_gsym->name) != 0))
9348 gfc_error ("Binding label '%s' for common block '%s' at %L "
9349 "collides with global entity '%s' at %L",
9350 binding_label_gsym->name, binding_label_gsym->sym_name,
9351 &(comm_block_tree->n.common->where),
9352 comm_name_gsym->name, &(comm_name_gsym->where));
9356 return;
9360 /* Verify any BIND(C) derived types in the namespace so we can report errors
9361 for them once, rather than for each variable declared of that type. */
9363 static void
9364 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
9366 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
9367 && derived_sym->attr.is_bind_c == 1)
9368 verify_bind_c_derived_type (derived_sym);
9370 return;
9374 /* Verify that any binding labels used in a given namespace do not collide
9375 with the names or binding labels of any global symbols. */
9377 static void
9378 gfc_verify_binding_labels (gfc_symbol *sym)
9380 int has_error = 0;
9382 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
9383 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
9385 gfc_gsymbol *bind_c_sym;
9387 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
9388 if (bind_c_sym != NULL
9389 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
9391 if (sym->attr.if_source == IFSRC_DECL
9392 && (bind_c_sym->type != GSYM_SUBROUTINE
9393 && bind_c_sym->type != GSYM_FUNCTION)
9394 && ((sym->attr.contained == 1
9395 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
9396 || (sym->attr.use_assoc == 1
9397 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
9399 /* Make sure global procedures don't collide with anything. */
9400 gfc_error ("Binding label '%s' at %L collides with the global "
9401 "entity '%s' at %L", sym->binding_label,
9402 &(sym->declared_at), bind_c_sym->name,
9403 &(bind_c_sym->where));
9404 has_error = 1;
9406 else if (sym->attr.contained == 0
9407 && (sym->attr.if_source == IFSRC_IFBODY
9408 && sym->attr.flavor == FL_PROCEDURE)
9409 && (bind_c_sym->sym_name != NULL
9410 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
9412 /* Make sure procedures in interface bodies don't collide. */
9413 gfc_error ("Binding label '%s' in interface body at %L collides "
9414 "with the global entity '%s' at %L",
9415 sym->binding_label,
9416 &(sym->declared_at), bind_c_sym->name,
9417 &(bind_c_sym->where));
9418 has_error = 1;
9420 else if (sym->attr.contained == 0
9421 && sym->attr.if_source == IFSRC_UNKNOWN)
9422 if ((sym->attr.use_assoc && bind_c_sym->mod_name
9423 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
9424 || sym->attr.use_assoc == 0)
9426 gfc_error ("Binding label '%s' at %L collides with global "
9427 "entity '%s' at %L", sym->binding_label,
9428 &(sym->declared_at), bind_c_sym->name,
9429 &(bind_c_sym->where));
9430 has_error = 1;
9433 if (has_error != 0)
9434 /* Clear the binding label to prevent checking multiple times. */
9435 sym->binding_label[0] = '\0';
9437 else if (bind_c_sym == NULL)
9439 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
9440 bind_c_sym->where = sym->declared_at;
9441 bind_c_sym->sym_name = sym->name;
9443 if (sym->attr.use_assoc == 1)
9444 bind_c_sym->mod_name = sym->module;
9445 else
9446 if (sym->ns->proc_name != NULL)
9447 bind_c_sym->mod_name = sym->ns->proc_name->name;
9449 if (sym->attr.contained == 0)
9451 if (sym->attr.subroutine)
9452 bind_c_sym->type = GSYM_SUBROUTINE;
9453 else if (sym->attr.function)
9454 bind_c_sym->type = GSYM_FUNCTION;
9458 return;
9462 /* Resolve an index expression. */
9464 static gfc_try
9465 resolve_index_expr (gfc_expr *e)
9467 if (gfc_resolve_expr (e) == FAILURE)
9468 return FAILURE;
9470 if (gfc_simplify_expr (e, 0) == FAILURE)
9471 return FAILURE;
9473 if (gfc_specification_expr (e) == FAILURE)
9474 return FAILURE;
9476 return SUCCESS;
9480 /* Resolve a charlen structure. */
9482 static gfc_try
9483 resolve_charlen (gfc_charlen *cl)
9485 int i, k;
9487 if (cl->resolved)
9488 return SUCCESS;
9490 cl->resolved = 1;
9492 specification_expr = 1;
9494 if (resolve_index_expr (cl->length) == FAILURE)
9496 specification_expr = 0;
9497 return FAILURE;
9500 /* "If the character length parameter value evaluates to a negative
9501 value, the length of character entities declared is zero." */
9502 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
9504 if (gfc_option.warn_surprising)
9505 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
9506 " the length has been set to zero",
9507 &cl->length->where, i);
9508 gfc_replace_expr (cl->length,
9509 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
9512 /* Check that the character length is not too large. */
9513 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
9514 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
9515 && cl->length->ts.type == BT_INTEGER
9516 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
9518 gfc_error ("String length at %L is too large", &cl->length->where);
9519 return FAILURE;
9522 return SUCCESS;
9526 /* Test for non-constant shape arrays. */
9528 static bool
9529 is_non_constant_shape_array (gfc_symbol *sym)
9531 gfc_expr *e;
9532 int i;
9533 bool not_constant;
9535 not_constant = false;
9536 if (sym->as != NULL)
9538 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
9539 has not been simplified; parameter array references. Do the
9540 simplification now. */
9541 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
9543 e = sym->as->lower[i];
9544 if (e && (resolve_index_expr (e) == FAILURE
9545 || !gfc_is_constant_expr (e)))
9546 not_constant = true;
9547 e = sym->as->upper[i];
9548 if (e && (resolve_index_expr (e) == FAILURE
9549 || !gfc_is_constant_expr (e)))
9550 not_constant = true;
9553 return not_constant;
9556 /* Given a symbol and an initialization expression, add code to initialize
9557 the symbol to the function entry. */
9558 static void
9559 build_init_assign (gfc_symbol *sym, gfc_expr *init)
9561 gfc_expr *lval;
9562 gfc_code *init_st;
9563 gfc_namespace *ns = sym->ns;
9565 /* Search for the function namespace if this is a contained
9566 function without an explicit result. */
9567 if (sym->attr.function && sym == sym->result
9568 && sym->name != sym->ns->proc_name->name)
9570 ns = ns->contained;
9571 for (;ns; ns = ns->sibling)
9572 if (strcmp (ns->proc_name->name, sym->name) == 0)
9573 break;
9576 if (ns == NULL)
9578 gfc_free_expr (init);
9579 return;
9582 /* Build an l-value expression for the result. */
9583 lval = gfc_lval_expr_from_sym (sym);
9585 /* Add the code at scope entry. */
9586 init_st = gfc_get_code ();
9587 init_st->next = ns->code;
9588 ns->code = init_st;
9590 /* Assign the default initializer to the l-value. */
9591 init_st->loc = sym->declared_at;
9592 init_st->op = EXEC_INIT_ASSIGN;
9593 init_st->expr1 = lval;
9594 init_st->expr2 = init;
9597 /* Assign the default initializer to a derived type variable or result. */
9599 static void
9600 apply_default_init (gfc_symbol *sym)
9602 gfc_expr *init = NULL;
9604 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9605 return;
9607 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
9608 init = gfc_default_initializer (&sym->ts);
9610 if (init == NULL && sym->ts.type != BT_CLASS)
9611 return;
9613 build_init_assign (sym, init);
9614 sym->attr.referenced = 1;
9617 /* Build an initializer for a local integer, real, complex, logical, or
9618 character variable, based on the command line flags finit-local-zero,
9619 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
9620 null if the symbol should not have a default initialization. */
9621 static gfc_expr *
9622 build_default_init_expr (gfc_symbol *sym)
9624 int char_len;
9625 gfc_expr *init_expr;
9626 int i;
9628 /* These symbols should never have a default initialization. */
9629 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
9630 || sym->attr.external
9631 || sym->attr.dummy
9632 || sym->attr.pointer
9633 || sym->attr.in_equivalence
9634 || sym->attr.in_common
9635 || sym->attr.data
9636 || sym->module
9637 || sym->attr.cray_pointee
9638 || sym->attr.cray_pointer)
9639 return NULL;
9641 /* Now we'll try to build an initializer expression. */
9642 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
9643 &sym->declared_at);
9645 /* We will only initialize integers, reals, complex, logicals, and
9646 characters, and only if the corresponding command-line flags
9647 were set. Otherwise, we free init_expr and return null. */
9648 switch (sym->ts.type)
9650 case BT_INTEGER:
9651 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
9652 mpz_set_si (init_expr->value.integer,
9653 gfc_option.flag_init_integer_value);
9654 else
9656 gfc_free_expr (init_expr);
9657 init_expr = NULL;
9659 break;
9661 case BT_REAL:
9662 switch (gfc_option.flag_init_real)
9664 case GFC_INIT_REAL_SNAN:
9665 init_expr->is_snan = 1;
9666 /* Fall through. */
9667 case GFC_INIT_REAL_NAN:
9668 mpfr_set_nan (init_expr->value.real);
9669 break;
9671 case GFC_INIT_REAL_INF:
9672 mpfr_set_inf (init_expr->value.real, 1);
9673 break;
9675 case GFC_INIT_REAL_NEG_INF:
9676 mpfr_set_inf (init_expr->value.real, -1);
9677 break;
9679 case GFC_INIT_REAL_ZERO:
9680 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
9681 break;
9683 default:
9684 gfc_free_expr (init_expr);
9685 init_expr = NULL;
9686 break;
9688 break;
9690 case BT_COMPLEX:
9691 switch (gfc_option.flag_init_real)
9693 case GFC_INIT_REAL_SNAN:
9694 init_expr->is_snan = 1;
9695 /* Fall through. */
9696 case GFC_INIT_REAL_NAN:
9697 mpfr_set_nan (mpc_realref (init_expr->value.complex));
9698 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
9699 break;
9701 case GFC_INIT_REAL_INF:
9702 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
9703 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
9704 break;
9706 case GFC_INIT_REAL_NEG_INF:
9707 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
9708 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
9709 break;
9711 case GFC_INIT_REAL_ZERO:
9712 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
9713 break;
9715 default:
9716 gfc_free_expr (init_expr);
9717 init_expr = NULL;
9718 break;
9720 break;
9722 case BT_LOGICAL:
9723 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
9724 init_expr->value.logical = 0;
9725 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
9726 init_expr->value.logical = 1;
9727 else
9729 gfc_free_expr (init_expr);
9730 init_expr = NULL;
9732 break;
9734 case BT_CHARACTER:
9735 /* For characters, the length must be constant in order to
9736 create a default initializer. */
9737 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
9738 && sym->ts.u.cl->length
9739 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9741 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
9742 init_expr->value.character.length = char_len;
9743 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
9744 for (i = 0; i < char_len; i++)
9745 init_expr->value.character.string[i]
9746 = (unsigned char) gfc_option.flag_init_character_value;
9748 else
9750 gfc_free_expr (init_expr);
9751 init_expr = NULL;
9753 break;
9755 default:
9756 gfc_free_expr (init_expr);
9757 init_expr = NULL;
9759 return init_expr;
9762 /* Add an initialization expression to a local variable. */
9763 static void
9764 apply_default_init_local (gfc_symbol *sym)
9766 gfc_expr *init = NULL;
9768 /* The symbol should be a variable or a function return value. */
9769 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9770 || (sym->attr.function && sym->result != sym))
9771 return;
9773 /* Try to build the initializer expression. If we can't initialize
9774 this symbol, then init will be NULL. */
9775 init = build_default_init_expr (sym);
9776 if (init == NULL)
9777 return;
9779 /* For saved variables, we don't want to add an initializer at
9780 function entry, so we just add a static initializer. */
9781 if (sym->attr.save || sym->ns->save_all
9782 || gfc_option.flag_max_stack_var_size == 0)
9784 /* Don't clobber an existing initializer! */
9785 gcc_assert (sym->value == NULL);
9786 sym->value = init;
9787 return;
9790 build_init_assign (sym, init);
9794 /* Resolution of common features of flavors variable and procedure. */
9796 static gfc_try
9797 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
9799 /* Constraints on deferred shape variable. */
9800 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
9802 if (sym->attr.allocatable)
9804 if (sym->attr.dimension)
9806 gfc_error ("Allocatable array '%s' at %L must have "
9807 "a deferred shape", sym->name, &sym->declared_at);
9808 return FAILURE;
9810 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
9811 "may not be ALLOCATABLE", sym->name,
9812 &sym->declared_at) == FAILURE)
9813 return FAILURE;
9816 if (sym->attr.pointer && sym->attr.dimension)
9818 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
9819 sym->name, &sym->declared_at);
9820 return FAILURE;
9823 else
9825 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
9826 && !sym->attr.dummy && sym->ts.type != BT_CLASS && !sym->assoc)
9828 gfc_error ("Array '%s' at %L cannot have a deferred shape",
9829 sym->name, &sym->declared_at);
9830 return FAILURE;
9834 /* Constraints on polymorphic variables. */
9835 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
9837 /* F03:C502. */
9838 if (sym->attr.class_ok
9839 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
9841 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
9842 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
9843 &sym->declared_at);
9844 return FAILURE;
9847 /* F03:C509. */
9848 /* Assume that use associated symbols were checked in the module ns.
9849 Class-variables that are associate-names are also something special
9850 and excepted from the test. */
9851 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
9853 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
9854 "or pointer", sym->name, &sym->declared_at);
9855 return FAILURE;
9859 return SUCCESS;
9863 /* Additional checks for symbols with flavor variable and derived
9864 type. To be called from resolve_fl_variable. */
9866 static gfc_try
9867 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
9869 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
9871 /* Check to see if a derived type is blocked from being host
9872 associated by the presence of another class I symbol in the same
9873 namespace. 14.6.1.3 of the standard and the discussion on
9874 comp.lang.fortran. */
9875 if (sym->ns != sym->ts.u.derived->ns
9876 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
9878 gfc_symbol *s;
9879 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
9880 if (s && s->attr.flavor != FL_DERIVED)
9882 gfc_error ("The type '%s' cannot be host associated at %L "
9883 "because it is blocked by an incompatible object "
9884 "of the same name declared at %L",
9885 sym->ts.u.derived->name, &sym->declared_at,
9886 &s->declared_at);
9887 return FAILURE;
9891 /* 4th constraint in section 11.3: "If an object of a type for which
9892 component-initialization is specified (R429) appears in the
9893 specification-part of a module and does not have the ALLOCATABLE
9894 or POINTER attribute, the object shall have the SAVE attribute."
9896 The check for initializers is performed with
9897 gfc_has_default_initializer because gfc_default_initializer generates
9898 a hidden default for allocatable components. */
9899 if (!(sym->value || no_init_flag) && sym->ns->proc_name
9900 && sym->ns->proc_name->attr.flavor == FL_MODULE
9901 && !sym->ns->save_all && !sym->attr.save
9902 && !sym->attr.pointer && !sym->attr.allocatable
9903 && gfc_has_default_initializer (sym->ts.u.derived)
9904 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Implied SAVE for "
9905 "module variable '%s' at %L, needed due to "
9906 "the default initialization", sym->name,
9907 &sym->declared_at) == FAILURE)
9908 return FAILURE;
9910 /* Assign default initializer. */
9911 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
9912 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
9914 sym->value = gfc_default_initializer (&sym->ts);
9917 return SUCCESS;
9921 /* Resolve symbols with flavor variable. */
9923 static gfc_try
9924 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
9926 int no_init_flag, automatic_flag;
9927 gfc_expr *e;
9928 const char *auto_save_msg;
9930 auto_save_msg = "Automatic object '%s' at %L cannot have the "
9931 "SAVE attribute";
9933 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9934 return FAILURE;
9936 /* Set this flag to check that variables are parameters of all entries.
9937 This check is effected by the call to gfc_resolve_expr through
9938 is_non_constant_shape_array. */
9939 specification_expr = 1;
9941 if (sym->ns->proc_name
9942 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9943 || sym->ns->proc_name->attr.is_main_program)
9944 && !sym->attr.use_assoc
9945 && !sym->attr.allocatable
9946 && !sym->attr.pointer
9947 && is_non_constant_shape_array (sym))
9949 /* The shape of a main program or module array needs to be
9950 constant. */
9951 gfc_error ("The module or main program array '%s' at %L must "
9952 "have constant shape", sym->name, &sym->declared_at);
9953 specification_expr = 0;
9954 return FAILURE;
9957 /* Constraints on deferred type parameter. */
9958 if (sym->ts.deferred && !(sym->attr.pointer || sym->attr.allocatable))
9960 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
9961 "requires either the pointer or allocatable attribute",
9962 sym->name, &sym->declared_at);
9963 return FAILURE;
9966 if (sym->ts.type == BT_CHARACTER)
9968 /* Make sure that character string variables with assumed length are
9969 dummy arguments. */
9970 e = sym->ts.u.cl->length;
9971 if (e == NULL && !sym->attr.dummy && !sym->attr.result
9972 && !sym->ts.deferred)
9974 gfc_error ("Entity with assumed character length at %L must be a "
9975 "dummy argument or a PARAMETER", &sym->declared_at);
9976 return FAILURE;
9979 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
9981 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9982 return FAILURE;
9985 if (!gfc_is_constant_expr (e)
9986 && !(e->expr_type == EXPR_VARIABLE
9987 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
9988 && sym->ns->proc_name
9989 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9990 || sym->ns->proc_name->attr.is_main_program)
9991 && !sym->attr.use_assoc)
9993 gfc_error ("'%s' at %L must have constant character length "
9994 "in this context", sym->name, &sym->declared_at);
9995 return FAILURE;
9999 if (sym->value == NULL && sym->attr.referenced)
10000 apply_default_init_local (sym); /* Try to apply a default initialization. */
10002 /* Determine if the symbol may not have an initializer. */
10003 no_init_flag = automatic_flag = 0;
10004 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
10005 || sym->attr.intrinsic || sym->attr.result)
10006 no_init_flag = 1;
10007 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
10008 && is_non_constant_shape_array (sym))
10010 no_init_flag = automatic_flag = 1;
10012 /* Also, they must not have the SAVE attribute.
10013 SAVE_IMPLICIT is checked below. */
10014 if (sym->attr.save == SAVE_EXPLICIT)
10016 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10017 return FAILURE;
10021 /* Ensure that any initializer is simplified. */
10022 if (sym->value)
10023 gfc_simplify_expr (sym->value, 1);
10025 /* Reject illegal initializers. */
10026 if (!sym->mark && sym->value)
10028 if (sym->attr.allocatable)
10029 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10030 sym->name, &sym->declared_at);
10031 else if (sym->attr.external)
10032 gfc_error ("External '%s' at %L cannot have an initializer",
10033 sym->name, &sym->declared_at);
10034 else if (sym->attr.dummy
10035 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
10036 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10037 sym->name, &sym->declared_at);
10038 else if (sym->attr.intrinsic)
10039 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10040 sym->name, &sym->declared_at);
10041 else if (sym->attr.result)
10042 gfc_error ("Function result '%s' at %L cannot have an initializer",
10043 sym->name, &sym->declared_at);
10044 else if (automatic_flag)
10045 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10046 sym->name, &sym->declared_at);
10047 else
10048 goto no_init_error;
10049 return FAILURE;
10052 no_init_error:
10053 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
10054 return resolve_fl_variable_derived (sym, no_init_flag);
10056 return SUCCESS;
10060 /* Resolve a procedure. */
10062 static gfc_try
10063 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
10065 gfc_formal_arglist *arg;
10067 if (sym->attr.function
10068 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
10069 return FAILURE;
10071 if (sym->ts.type == BT_CHARACTER)
10073 gfc_charlen *cl = sym->ts.u.cl;
10075 if (cl && cl->length && gfc_is_constant_expr (cl->length)
10076 && resolve_charlen (cl) == FAILURE)
10077 return FAILURE;
10079 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10080 && sym->attr.proc == PROC_ST_FUNCTION)
10082 gfc_error ("Character-valued statement function '%s' at %L must "
10083 "have constant length", sym->name, &sym->declared_at);
10084 return FAILURE;
10088 /* Ensure that derived type for are not of a private type. Internal
10089 module procedures are excluded by 2.2.3.3 - i.e., they are not
10090 externally accessible and can access all the objects accessible in
10091 the host. */
10092 if (!(sym->ns->parent
10093 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10094 && gfc_check_access(sym->attr.access, sym->ns->default_access))
10096 gfc_interface *iface;
10098 for (arg = sym->formal; arg; arg = arg->next)
10100 if (arg->sym
10101 && arg->sym->ts.type == BT_DERIVED
10102 && !arg->sym->ts.u.derived->attr.use_assoc
10103 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
10104 arg->sym->ts.u.derived->ns->default_access)
10105 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
10106 "PRIVATE type and cannot be a dummy argument"
10107 " of '%s', which is PUBLIC at %L",
10108 arg->sym->name, sym->name, &sym->declared_at)
10109 == FAILURE)
10111 /* Stop this message from recurring. */
10112 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10113 return FAILURE;
10117 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10118 PRIVATE to the containing module. */
10119 for (iface = sym->generic; iface; iface = iface->next)
10121 for (arg = iface->sym->formal; arg; arg = arg->next)
10123 if (arg->sym
10124 && arg->sym->ts.type == BT_DERIVED
10125 && !arg->sym->ts.u.derived->attr.use_assoc
10126 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
10127 arg->sym->ts.u.derived->ns->default_access)
10128 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
10129 "'%s' in PUBLIC interface '%s' at %L "
10130 "takes dummy arguments of '%s' which is "
10131 "PRIVATE", iface->sym->name, sym->name,
10132 &iface->sym->declared_at,
10133 gfc_typename (&arg->sym->ts)) == FAILURE)
10135 /* Stop this message from recurring. */
10136 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10137 return FAILURE;
10142 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10143 PRIVATE to the containing module. */
10144 for (iface = sym->generic; iface; iface = iface->next)
10146 for (arg = iface->sym->formal; arg; arg = arg->next)
10148 if (arg->sym
10149 && arg->sym->ts.type == BT_DERIVED
10150 && !arg->sym->ts.u.derived->attr.use_assoc
10151 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
10152 arg->sym->ts.u.derived->ns->default_access)
10153 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
10154 "'%s' in PUBLIC interface '%s' at %L "
10155 "takes dummy arguments of '%s' which is "
10156 "PRIVATE", iface->sym->name, sym->name,
10157 &iface->sym->declared_at,
10158 gfc_typename (&arg->sym->ts)) == FAILURE)
10160 /* Stop this message from recurring. */
10161 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10162 return FAILURE;
10168 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
10169 && !sym->attr.proc_pointer)
10171 gfc_error ("Function '%s' at %L cannot have an initializer",
10172 sym->name, &sym->declared_at);
10173 return FAILURE;
10176 /* An external symbol may not have an initializer because it is taken to be
10177 a procedure. Exception: Procedure Pointers. */
10178 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
10180 gfc_error ("External object '%s' at %L may not have an initializer",
10181 sym->name, &sym->declared_at);
10182 return FAILURE;
10185 /* An elemental function is required to return a scalar 12.7.1 */
10186 if (sym->attr.elemental && sym->attr.function && sym->as)
10188 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
10189 "result", sym->name, &sym->declared_at);
10190 /* Reset so that the error only occurs once. */
10191 sym->attr.elemental = 0;
10192 return FAILURE;
10195 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
10196 char-len-param shall not be array-valued, pointer-valued, recursive
10197 or pure. ....snip... A character value of * may only be used in the
10198 following ways: (i) Dummy arg of procedure - dummy associates with
10199 actual length; (ii) To declare a named constant; or (iii) External
10200 function - but length must be declared in calling scoping unit. */
10201 if (sym->attr.function
10202 && sym->ts.type == BT_CHARACTER
10203 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
10205 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
10206 || (sym->attr.recursive) || (sym->attr.pure))
10208 if (sym->as && sym->as->rank)
10209 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10210 "array-valued", sym->name, &sym->declared_at);
10212 if (sym->attr.pointer)
10213 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10214 "pointer-valued", sym->name, &sym->declared_at);
10216 if (sym->attr.pure)
10217 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10218 "pure", sym->name, &sym->declared_at);
10220 if (sym->attr.recursive)
10221 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10222 "recursive", sym->name, &sym->declared_at);
10224 return FAILURE;
10227 /* Appendix B.2 of the standard. Contained functions give an
10228 error anyway. Fixed-form is likely to be F77/legacy. */
10229 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
10230 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
10231 "CHARACTER(*) function '%s' at %L",
10232 sym->name, &sym->declared_at);
10235 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
10237 gfc_formal_arglist *curr_arg;
10238 int has_non_interop_arg = 0;
10240 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
10241 sym->common_block) == FAILURE)
10243 /* Clear these to prevent looking at them again if there was an
10244 error. */
10245 sym->attr.is_bind_c = 0;
10246 sym->attr.is_c_interop = 0;
10247 sym->ts.is_c_interop = 0;
10249 else
10251 /* So far, no errors have been found. */
10252 sym->attr.is_c_interop = 1;
10253 sym->ts.is_c_interop = 1;
10256 curr_arg = sym->formal;
10257 while (curr_arg != NULL)
10259 /* Skip implicitly typed dummy args here. */
10260 if (curr_arg->sym->attr.implicit_type == 0)
10261 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
10262 /* If something is found to fail, record the fact so we
10263 can mark the symbol for the procedure as not being
10264 BIND(C) to try and prevent multiple errors being
10265 reported. */
10266 has_non_interop_arg = 1;
10268 curr_arg = curr_arg->next;
10271 /* See if any of the arguments were not interoperable and if so, clear
10272 the procedure symbol to prevent duplicate error messages. */
10273 if (has_non_interop_arg != 0)
10275 sym->attr.is_c_interop = 0;
10276 sym->ts.is_c_interop = 0;
10277 sym->attr.is_bind_c = 0;
10281 if (!sym->attr.proc_pointer)
10283 if (sym->attr.save == SAVE_EXPLICIT)
10285 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
10286 "in '%s' at %L", sym->name, &sym->declared_at);
10287 return FAILURE;
10289 if (sym->attr.intent)
10291 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
10292 "in '%s' at %L", sym->name, &sym->declared_at);
10293 return FAILURE;
10295 if (sym->attr.subroutine && sym->attr.result)
10297 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
10298 "in '%s' at %L", sym->name, &sym->declared_at);
10299 return FAILURE;
10301 if (sym->attr.external && sym->attr.function
10302 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
10303 || sym->attr.contained))
10305 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
10306 "in '%s' at %L", sym->name, &sym->declared_at);
10307 return FAILURE;
10309 if (strcmp ("ppr@", sym->name) == 0)
10311 gfc_error ("Procedure pointer result '%s' at %L "
10312 "is missing the pointer attribute",
10313 sym->ns->proc_name->name, &sym->declared_at);
10314 return FAILURE;
10318 return SUCCESS;
10322 /* Resolve a list of finalizer procedures. That is, after they have hopefully
10323 been defined and we now know their defined arguments, check that they fulfill
10324 the requirements of the standard for procedures used as finalizers. */
10326 static gfc_try
10327 gfc_resolve_finalizers (gfc_symbol* derived)
10329 gfc_finalizer* list;
10330 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
10331 gfc_try result = SUCCESS;
10332 bool seen_scalar = false;
10334 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
10335 return SUCCESS;
10337 /* Walk over the list of finalizer-procedures, check them, and if any one
10338 does not fit in with the standard's definition, print an error and remove
10339 it from the list. */
10340 prev_link = &derived->f2k_derived->finalizers;
10341 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
10343 gfc_symbol* arg;
10344 gfc_finalizer* i;
10345 int my_rank;
10347 /* Skip this finalizer if we already resolved it. */
10348 if (list->proc_tree)
10350 prev_link = &(list->next);
10351 continue;
10354 /* Check this exists and is a SUBROUTINE. */
10355 if (!list->proc_sym->attr.subroutine)
10357 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
10358 list->proc_sym->name, &list->where);
10359 goto error;
10362 /* We should have exactly one argument. */
10363 if (!list->proc_sym->formal || list->proc_sym->formal->next)
10365 gfc_error ("FINAL procedure at %L must have exactly one argument",
10366 &list->where);
10367 goto error;
10369 arg = list->proc_sym->formal->sym;
10371 /* This argument must be of our type. */
10372 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
10374 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
10375 &arg->declared_at, derived->name);
10376 goto error;
10379 /* It must neither be a pointer nor allocatable nor optional. */
10380 if (arg->attr.pointer)
10382 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
10383 &arg->declared_at);
10384 goto error;
10386 if (arg->attr.allocatable)
10388 gfc_error ("Argument of FINAL procedure at %L must not be"
10389 " ALLOCATABLE", &arg->declared_at);
10390 goto error;
10392 if (arg->attr.optional)
10394 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
10395 &arg->declared_at);
10396 goto error;
10399 /* It must not be INTENT(OUT). */
10400 if (arg->attr.intent == INTENT_OUT)
10402 gfc_error ("Argument of FINAL procedure at %L must not be"
10403 " INTENT(OUT)", &arg->declared_at);
10404 goto error;
10407 /* Warn if the procedure is non-scalar and not assumed shape. */
10408 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
10409 && arg->as->type != AS_ASSUMED_SHAPE)
10410 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
10411 " shape argument", &arg->declared_at);
10413 /* Check that it does not match in kind and rank with a FINAL procedure
10414 defined earlier. To really loop over the *earlier* declarations,
10415 we need to walk the tail of the list as new ones were pushed at the
10416 front. */
10417 /* TODO: Handle kind parameters once they are implemented. */
10418 my_rank = (arg->as ? arg->as->rank : 0);
10419 for (i = list->next; i; i = i->next)
10421 /* Argument list might be empty; that is an error signalled earlier,
10422 but we nevertheless continued resolving. */
10423 if (i->proc_sym->formal)
10425 gfc_symbol* i_arg = i->proc_sym->formal->sym;
10426 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
10427 if (i_rank == my_rank)
10429 gfc_error ("FINAL procedure '%s' declared at %L has the same"
10430 " rank (%d) as '%s'",
10431 list->proc_sym->name, &list->where, my_rank,
10432 i->proc_sym->name);
10433 goto error;
10438 /* Is this the/a scalar finalizer procedure? */
10439 if (!arg->as || arg->as->rank == 0)
10440 seen_scalar = true;
10442 /* Find the symtree for this procedure. */
10443 gcc_assert (!list->proc_tree);
10444 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
10446 prev_link = &list->next;
10447 continue;
10449 /* Remove wrong nodes immediately from the list so we don't risk any
10450 troubles in the future when they might fail later expectations. */
10451 error:
10452 result = FAILURE;
10453 i = list;
10454 *prev_link = list->next;
10455 gfc_free_finalizer (i);
10458 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
10459 were nodes in the list, must have been for arrays. It is surely a good
10460 idea to have a scalar version there if there's something to finalize. */
10461 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
10462 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
10463 " defined at %L, suggest also scalar one",
10464 derived->name, &derived->declared_at);
10466 /* TODO: Remove this error when finalization is finished. */
10467 gfc_error ("Finalization at %L is not yet implemented",
10468 &derived->declared_at);
10470 return result;
10474 /* Check that it is ok for the typebound procedure proc to override the
10475 procedure old. */
10477 static gfc_try
10478 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
10480 locus where;
10481 const gfc_symbol* proc_target;
10482 const gfc_symbol* old_target;
10483 unsigned proc_pass_arg, old_pass_arg, argpos;
10484 gfc_formal_arglist* proc_formal;
10485 gfc_formal_arglist* old_formal;
10487 /* This procedure should only be called for non-GENERIC proc. */
10488 gcc_assert (!proc->n.tb->is_generic);
10490 /* If the overwritten procedure is GENERIC, this is an error. */
10491 if (old->n.tb->is_generic)
10493 gfc_error ("Can't overwrite GENERIC '%s' at %L",
10494 old->name, &proc->n.tb->where);
10495 return FAILURE;
10498 where = proc->n.tb->where;
10499 proc_target = proc->n.tb->u.specific->n.sym;
10500 old_target = old->n.tb->u.specific->n.sym;
10502 /* Check that overridden binding is not NON_OVERRIDABLE. */
10503 if (old->n.tb->non_overridable)
10505 gfc_error ("'%s' at %L overrides a procedure binding declared"
10506 " NON_OVERRIDABLE", proc->name, &where);
10507 return FAILURE;
10510 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
10511 if (!old->n.tb->deferred && proc->n.tb->deferred)
10513 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
10514 " non-DEFERRED binding", proc->name, &where);
10515 return FAILURE;
10518 /* If the overridden binding is PURE, the overriding must be, too. */
10519 if (old_target->attr.pure && !proc_target->attr.pure)
10521 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
10522 proc->name, &where);
10523 return FAILURE;
10526 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
10527 is not, the overriding must not be either. */
10528 if (old_target->attr.elemental && !proc_target->attr.elemental)
10530 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
10531 " ELEMENTAL", proc->name, &where);
10532 return FAILURE;
10534 if (!old_target->attr.elemental && proc_target->attr.elemental)
10536 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
10537 " be ELEMENTAL, either", proc->name, &where);
10538 return FAILURE;
10541 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
10542 SUBROUTINE. */
10543 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
10545 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
10546 " SUBROUTINE", proc->name, &where);
10547 return FAILURE;
10550 /* If the overridden binding is a FUNCTION, the overriding must also be a
10551 FUNCTION and have the same characteristics. */
10552 if (old_target->attr.function)
10554 if (!proc_target->attr.function)
10556 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
10557 " FUNCTION", proc->name, &where);
10558 return FAILURE;
10561 /* FIXME: Do more comprehensive checking (including, for instance, the
10562 rank and array-shape). */
10563 gcc_assert (proc_target->result && old_target->result);
10564 if (!gfc_compare_types (&proc_target->result->ts,
10565 &old_target->result->ts))
10567 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
10568 " matching result types", proc->name, &where);
10569 return FAILURE;
10573 /* If the overridden binding is PUBLIC, the overriding one must not be
10574 PRIVATE. */
10575 if (old->n.tb->access == ACCESS_PUBLIC
10576 && proc->n.tb->access == ACCESS_PRIVATE)
10578 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
10579 " PRIVATE", proc->name, &where);
10580 return FAILURE;
10583 /* Compare the formal argument lists of both procedures. This is also abused
10584 to find the position of the passed-object dummy arguments of both
10585 bindings as at least the overridden one might not yet be resolved and we
10586 need those positions in the check below. */
10587 proc_pass_arg = old_pass_arg = 0;
10588 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
10589 proc_pass_arg = 1;
10590 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
10591 old_pass_arg = 1;
10592 argpos = 1;
10593 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
10594 proc_formal && old_formal;
10595 proc_formal = proc_formal->next, old_formal = old_formal->next)
10597 if (proc->n.tb->pass_arg
10598 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
10599 proc_pass_arg = argpos;
10600 if (old->n.tb->pass_arg
10601 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
10602 old_pass_arg = argpos;
10604 /* Check that the names correspond. */
10605 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
10607 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
10608 " to match the corresponding argument of the overridden"
10609 " procedure", proc_formal->sym->name, proc->name, &where,
10610 old_formal->sym->name);
10611 return FAILURE;
10614 /* Check that the types correspond if neither is the passed-object
10615 argument. */
10616 /* FIXME: Do more comprehensive testing here. */
10617 if (proc_pass_arg != argpos && old_pass_arg != argpos
10618 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
10620 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
10621 "in respect to the overridden procedure",
10622 proc_formal->sym->name, proc->name, &where);
10623 return FAILURE;
10626 ++argpos;
10628 if (proc_formal || old_formal)
10630 gfc_error ("'%s' at %L must have the same number of formal arguments as"
10631 " the overridden procedure", proc->name, &where);
10632 return FAILURE;
10635 /* If the overridden binding is NOPASS, the overriding one must also be
10636 NOPASS. */
10637 if (old->n.tb->nopass && !proc->n.tb->nopass)
10639 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
10640 " NOPASS", proc->name, &where);
10641 return FAILURE;
10644 /* If the overridden binding is PASS(x), the overriding one must also be
10645 PASS and the passed-object dummy arguments must correspond. */
10646 if (!old->n.tb->nopass)
10648 if (proc->n.tb->nopass)
10650 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
10651 " PASS", proc->name, &where);
10652 return FAILURE;
10655 if (proc_pass_arg != old_pass_arg)
10657 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
10658 " the same position as the passed-object dummy argument of"
10659 " the overridden procedure", proc->name, &where);
10660 return FAILURE;
10664 return SUCCESS;
10668 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
10670 static gfc_try
10671 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
10672 const char* generic_name, locus where)
10674 gfc_symbol* sym1;
10675 gfc_symbol* sym2;
10677 gcc_assert (t1->specific && t2->specific);
10678 gcc_assert (!t1->specific->is_generic);
10679 gcc_assert (!t2->specific->is_generic);
10681 sym1 = t1->specific->u.specific->n.sym;
10682 sym2 = t2->specific->u.specific->n.sym;
10684 if (sym1 == sym2)
10685 return SUCCESS;
10687 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
10688 if (sym1->attr.subroutine != sym2->attr.subroutine
10689 || sym1->attr.function != sym2->attr.function)
10691 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
10692 " GENERIC '%s' at %L",
10693 sym1->name, sym2->name, generic_name, &where);
10694 return FAILURE;
10697 /* Compare the interfaces. */
10698 if (gfc_compare_interfaces (sym1, sym2, sym2->name, 1, 0, NULL, 0))
10700 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
10701 sym1->name, sym2->name, generic_name, &where);
10702 return FAILURE;
10705 return SUCCESS;
10709 /* Worker function for resolving a generic procedure binding; this is used to
10710 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
10712 The difference between those cases is finding possible inherited bindings
10713 that are overridden, as one has to look for them in tb_sym_root,
10714 tb_uop_root or tb_op, respectively. Thus the caller must already find
10715 the super-type and set p->overridden correctly. */
10717 static gfc_try
10718 resolve_tb_generic_targets (gfc_symbol* super_type,
10719 gfc_typebound_proc* p, const char* name)
10721 gfc_tbp_generic* target;
10722 gfc_symtree* first_target;
10723 gfc_symtree* inherited;
10725 gcc_assert (p && p->is_generic);
10727 /* Try to find the specific bindings for the symtrees in our target-list. */
10728 gcc_assert (p->u.generic);
10729 for (target = p->u.generic; target; target = target->next)
10730 if (!target->specific)
10732 gfc_typebound_proc* overridden_tbp;
10733 gfc_tbp_generic* g;
10734 const char* target_name;
10736 target_name = target->specific_st->name;
10738 /* Defined for this type directly. */
10739 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
10741 target->specific = target->specific_st->n.tb;
10742 goto specific_found;
10745 /* Look for an inherited specific binding. */
10746 if (super_type)
10748 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
10749 true, NULL);
10751 if (inherited)
10753 gcc_assert (inherited->n.tb);
10754 target->specific = inherited->n.tb;
10755 goto specific_found;
10759 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
10760 " at %L", target_name, name, &p->where);
10761 return FAILURE;
10763 /* Once we've found the specific binding, check it is not ambiguous with
10764 other specifics already found or inherited for the same GENERIC. */
10765 specific_found:
10766 gcc_assert (target->specific);
10768 /* This must really be a specific binding! */
10769 if (target->specific->is_generic)
10771 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
10772 " '%s' is GENERIC, too", name, &p->where, target_name);
10773 return FAILURE;
10776 /* Check those already resolved on this type directly. */
10777 for (g = p->u.generic; g; g = g->next)
10778 if (g != target && g->specific
10779 && check_generic_tbp_ambiguity (target, g, name, p->where)
10780 == FAILURE)
10781 return FAILURE;
10783 /* Check for ambiguity with inherited specific targets. */
10784 for (overridden_tbp = p->overridden; overridden_tbp;
10785 overridden_tbp = overridden_tbp->overridden)
10786 if (overridden_tbp->is_generic)
10788 for (g = overridden_tbp->u.generic; g; g = g->next)
10790 gcc_assert (g->specific);
10791 if (check_generic_tbp_ambiguity (target, g,
10792 name, p->where) == FAILURE)
10793 return FAILURE;
10798 /* If we attempt to "overwrite" a specific binding, this is an error. */
10799 if (p->overridden && !p->overridden->is_generic)
10801 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
10802 " the same name", name, &p->where);
10803 return FAILURE;
10806 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
10807 all must have the same attributes here. */
10808 first_target = p->u.generic->specific->u.specific;
10809 gcc_assert (first_target);
10810 p->subroutine = first_target->n.sym->attr.subroutine;
10811 p->function = first_target->n.sym->attr.function;
10813 return SUCCESS;
10817 /* Resolve a GENERIC procedure binding for a derived type. */
10819 static gfc_try
10820 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
10822 gfc_symbol* super_type;
10824 /* Find the overridden binding if any. */
10825 st->n.tb->overridden = NULL;
10826 super_type = gfc_get_derived_super_type (derived);
10827 if (super_type)
10829 gfc_symtree* overridden;
10830 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
10831 true, NULL);
10833 if (overridden && overridden->n.tb)
10834 st->n.tb->overridden = overridden->n.tb;
10837 /* Resolve using worker function. */
10838 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
10842 /* Retrieve the target-procedure of an operator binding and do some checks in
10843 common for intrinsic and user-defined type-bound operators. */
10845 static gfc_symbol*
10846 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
10848 gfc_symbol* target_proc;
10850 gcc_assert (target->specific && !target->specific->is_generic);
10851 target_proc = target->specific->u.specific->n.sym;
10852 gcc_assert (target_proc);
10854 /* All operator bindings must have a passed-object dummy argument. */
10855 if (target->specific->nopass)
10857 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
10858 return NULL;
10861 return target_proc;
10865 /* Resolve a type-bound intrinsic operator. */
10867 static gfc_try
10868 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
10869 gfc_typebound_proc* p)
10871 gfc_symbol* super_type;
10872 gfc_tbp_generic* target;
10874 /* If there's already an error here, do nothing (but don't fail again). */
10875 if (p->error)
10876 return SUCCESS;
10878 /* Operators should always be GENERIC bindings. */
10879 gcc_assert (p->is_generic);
10881 /* Look for an overridden binding. */
10882 super_type = gfc_get_derived_super_type (derived);
10883 if (super_type && super_type->f2k_derived)
10884 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
10885 op, true, NULL);
10886 else
10887 p->overridden = NULL;
10889 /* Resolve general GENERIC properties using worker function. */
10890 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
10891 goto error;
10893 /* Check the targets to be procedures of correct interface. */
10894 for (target = p->u.generic; target; target = target->next)
10896 gfc_symbol* target_proc;
10898 target_proc = get_checked_tb_operator_target (target, p->where);
10899 if (!target_proc)
10900 goto error;
10902 if (!gfc_check_operator_interface (target_proc, op, p->where))
10903 goto error;
10906 return SUCCESS;
10908 error:
10909 p->error = 1;
10910 return FAILURE;
10914 /* Resolve a type-bound user operator (tree-walker callback). */
10916 static gfc_symbol* resolve_bindings_derived;
10917 static gfc_try resolve_bindings_result;
10919 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
10921 static void
10922 resolve_typebound_user_op (gfc_symtree* stree)
10924 gfc_symbol* super_type;
10925 gfc_tbp_generic* target;
10927 gcc_assert (stree && stree->n.tb);
10929 if (stree->n.tb->error)
10930 return;
10932 /* Operators should always be GENERIC bindings. */
10933 gcc_assert (stree->n.tb->is_generic);
10935 /* Find overridden procedure, if any. */
10936 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10937 if (super_type && super_type->f2k_derived)
10939 gfc_symtree* overridden;
10940 overridden = gfc_find_typebound_user_op (super_type, NULL,
10941 stree->name, true, NULL);
10943 if (overridden && overridden->n.tb)
10944 stree->n.tb->overridden = overridden->n.tb;
10946 else
10947 stree->n.tb->overridden = NULL;
10949 /* Resolve basically using worker function. */
10950 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
10951 == FAILURE)
10952 goto error;
10954 /* Check the targets to be functions of correct interface. */
10955 for (target = stree->n.tb->u.generic; target; target = target->next)
10957 gfc_symbol* target_proc;
10959 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
10960 if (!target_proc)
10961 goto error;
10963 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
10964 goto error;
10967 return;
10969 error:
10970 resolve_bindings_result = FAILURE;
10971 stree->n.tb->error = 1;
10975 /* Resolve the type-bound procedures for a derived type. */
10977 static void
10978 resolve_typebound_procedure (gfc_symtree* stree)
10980 gfc_symbol* proc;
10981 locus where;
10982 gfc_symbol* me_arg;
10983 gfc_symbol* super_type;
10984 gfc_component* comp;
10986 gcc_assert (stree);
10988 /* Undefined specific symbol from GENERIC target definition. */
10989 if (!stree->n.tb)
10990 return;
10992 if (stree->n.tb->error)
10993 return;
10995 /* If this is a GENERIC binding, use that routine. */
10996 if (stree->n.tb->is_generic)
10998 if (resolve_typebound_generic (resolve_bindings_derived, stree)
10999 == FAILURE)
11000 goto error;
11001 return;
11004 /* Get the target-procedure to check it. */
11005 gcc_assert (!stree->n.tb->is_generic);
11006 gcc_assert (stree->n.tb->u.specific);
11007 proc = stree->n.tb->u.specific->n.sym;
11008 where = stree->n.tb->where;
11010 /* Default access should already be resolved from the parser. */
11011 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11013 /* It should be a module procedure or an external procedure with explicit
11014 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
11015 if ((!proc->attr.subroutine && !proc->attr.function)
11016 || (proc->attr.proc != PROC_MODULE
11017 && proc->attr.if_source != IFSRC_IFBODY)
11018 || (proc->attr.abstract && !stree->n.tb->deferred))
11020 gfc_error ("'%s' must be a module procedure or an external procedure with"
11021 " an explicit interface at %L", proc->name, &where);
11022 goto error;
11024 stree->n.tb->subroutine = proc->attr.subroutine;
11025 stree->n.tb->function = proc->attr.function;
11027 /* Find the super-type of the current derived type. We could do this once and
11028 store in a global if speed is needed, but as long as not I believe this is
11029 more readable and clearer. */
11030 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11032 /* If PASS, resolve and check arguments if not already resolved / loaded
11033 from a .mod file. */
11034 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11036 if (stree->n.tb->pass_arg)
11038 gfc_formal_arglist* i;
11040 /* If an explicit passing argument name is given, walk the arg-list
11041 and look for it. */
11043 me_arg = NULL;
11044 stree->n.tb->pass_arg_num = 1;
11045 for (i = proc->formal; i; i = i->next)
11047 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
11049 me_arg = i->sym;
11050 break;
11052 ++stree->n.tb->pass_arg_num;
11055 if (!me_arg)
11057 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11058 " argument '%s'",
11059 proc->name, stree->n.tb->pass_arg, &where,
11060 stree->n.tb->pass_arg);
11061 goto error;
11064 else
11066 /* Otherwise, take the first one; there should in fact be at least
11067 one. */
11068 stree->n.tb->pass_arg_num = 1;
11069 if (!proc->formal)
11071 gfc_error ("Procedure '%s' with PASS at %L must have at"
11072 " least one argument", proc->name, &where);
11073 goto error;
11075 me_arg = proc->formal->sym;
11078 /* Now check that the argument-type matches and the passed-object
11079 dummy argument is generally fine. */
11081 gcc_assert (me_arg);
11083 if (me_arg->ts.type != BT_CLASS)
11085 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11086 " at %L", proc->name, &where);
11087 goto error;
11090 if (CLASS_DATA (me_arg)->ts.u.derived
11091 != resolve_bindings_derived)
11093 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11094 " the derived-type '%s'", me_arg->name, proc->name,
11095 me_arg->name, &where, resolve_bindings_derived->name);
11096 goto error;
11099 gcc_assert (me_arg->ts.type == BT_CLASS);
11100 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank > 0)
11102 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11103 " scalar", proc->name, &where);
11104 goto error;
11106 if (CLASS_DATA (me_arg)->attr.allocatable)
11108 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11109 " be ALLOCATABLE", proc->name, &where);
11110 goto error;
11112 if (CLASS_DATA (me_arg)->attr.class_pointer)
11114 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11115 " be POINTER", proc->name, &where);
11116 goto error;
11120 /* If we are extending some type, check that we don't override a procedure
11121 flagged NON_OVERRIDABLE. */
11122 stree->n.tb->overridden = NULL;
11123 if (super_type)
11125 gfc_symtree* overridden;
11126 overridden = gfc_find_typebound_proc (super_type, NULL,
11127 stree->name, true, NULL);
11129 if (overridden && overridden->n.tb)
11130 stree->n.tb->overridden = overridden->n.tb;
11132 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
11133 goto error;
11136 /* See if there's a name collision with a component directly in this type. */
11137 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11138 if (!strcmp (comp->name, stree->name))
11140 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11141 " '%s'",
11142 stree->name, &where, resolve_bindings_derived->name);
11143 goto error;
11146 /* Try to find a name collision with an inherited component. */
11147 if (super_type && gfc_find_component (super_type, stree->name, true, true))
11149 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11150 " component of '%s'",
11151 stree->name, &where, resolve_bindings_derived->name);
11152 goto error;
11155 stree->n.tb->error = 0;
11156 return;
11158 error:
11159 resolve_bindings_result = FAILURE;
11160 stree->n.tb->error = 1;
11164 static gfc_try
11165 resolve_typebound_procedures (gfc_symbol* derived)
11167 int op;
11169 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
11170 return SUCCESS;
11172 resolve_bindings_derived = derived;
11173 resolve_bindings_result = SUCCESS;
11175 /* Make sure the vtab has been generated. */
11176 gfc_find_derived_vtab (derived);
11178 if (derived->f2k_derived->tb_sym_root)
11179 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
11180 &resolve_typebound_procedure);
11182 if (derived->f2k_derived->tb_uop_root)
11183 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
11184 &resolve_typebound_user_op);
11186 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
11188 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
11189 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
11190 p) == FAILURE)
11191 resolve_bindings_result = FAILURE;
11194 return resolve_bindings_result;
11198 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11199 to give all identical derived types the same backend_decl. */
11200 static void
11201 add_dt_to_dt_list (gfc_symbol *derived)
11203 gfc_dt_list *dt_list;
11205 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
11206 if (derived == dt_list->derived)
11207 return;
11209 dt_list = gfc_get_dt_list ();
11210 dt_list->next = gfc_derived_types;
11211 dt_list->derived = derived;
11212 gfc_derived_types = dt_list;
11216 /* Ensure that a derived-type is really not abstract, meaning that every
11217 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11219 static gfc_try
11220 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
11222 if (!st)
11223 return SUCCESS;
11225 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
11226 return FAILURE;
11227 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
11228 return FAILURE;
11230 if (st->n.tb && st->n.tb->deferred)
11232 gfc_symtree* overriding;
11233 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
11234 if (!overriding)
11235 return FAILURE;
11236 gcc_assert (overriding->n.tb);
11237 if (overriding->n.tb->deferred)
11239 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11240 " '%s' is DEFERRED and not overridden",
11241 sub->name, &sub->declared_at, st->name);
11242 return FAILURE;
11246 return SUCCESS;
11249 static gfc_try
11250 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
11252 /* The algorithm used here is to recursively travel up the ancestry of sub
11253 and for each ancestor-type, check all bindings. If any of them is
11254 DEFERRED, look it up starting from sub and see if the found (overriding)
11255 binding is not DEFERRED.
11256 This is not the most efficient way to do this, but it should be ok and is
11257 clearer than something sophisticated. */
11259 gcc_assert (ancestor && !sub->attr.abstract);
11261 if (!ancestor->attr.abstract)
11262 return SUCCESS;
11264 /* Walk bindings of this ancestor. */
11265 if (ancestor->f2k_derived)
11267 gfc_try t;
11268 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
11269 if (t == FAILURE)
11270 return FAILURE;
11273 /* Find next ancestor type and recurse on it. */
11274 ancestor = gfc_get_derived_super_type (ancestor);
11275 if (ancestor)
11276 return ensure_not_abstract (sub, ancestor);
11278 return SUCCESS;
11282 /* Resolve the components of a derived type. */
11284 static gfc_try
11285 resolve_fl_derived (gfc_symbol *sym)
11287 gfc_symbol* super_type;
11288 gfc_component *c;
11290 super_type = gfc_get_derived_super_type (sym);
11292 if (sym->attr.is_class && sym->ts.u.derived == NULL)
11294 /* Fix up incomplete CLASS symbols. */
11295 gfc_component *data = gfc_find_component (sym, "_data", true, true);
11296 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
11297 if (vptr->ts.u.derived == NULL)
11299 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
11300 gcc_assert (vtab);
11301 vptr->ts.u.derived = vtab->ts.u.derived;
11305 /* F2008, C432. */
11306 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
11308 gfc_error ("As extending type '%s' at %L has a coarray component, "
11309 "parent type '%s' shall also have one", sym->name,
11310 &sym->declared_at, super_type->name);
11311 return FAILURE;
11314 /* Ensure the extended type gets resolved before we do. */
11315 if (super_type && resolve_fl_derived (super_type) == FAILURE)
11316 return FAILURE;
11318 /* An ABSTRACT type must be extensible. */
11319 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
11321 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
11322 sym->name, &sym->declared_at);
11323 return FAILURE;
11326 for (c = sym->components; c != NULL; c = c->next)
11328 /* F2008, C442. */
11329 if (c->attr.codimension /* FIXME: c->as check due to PR 43412. */
11330 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
11332 gfc_error ("Coarray component '%s' at %L must be allocatable with "
11333 "deferred shape", c->name, &c->loc);
11334 return FAILURE;
11337 /* F2008, C443. */
11338 if (c->attr.codimension && c->ts.type == BT_DERIVED
11339 && c->ts.u.derived->ts.is_iso_c)
11341 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
11342 "shall not be a coarray", c->name, &c->loc);
11343 return FAILURE;
11346 /* F2008, C444. */
11347 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
11348 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
11349 || c->attr.allocatable))
11351 gfc_error ("Component '%s' at %L with coarray component "
11352 "shall be a nonpointer, nonallocatable scalar",
11353 c->name, &c->loc);
11354 return FAILURE;
11357 /* F2008, C448. */
11358 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
11360 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
11361 "is not an array pointer", c->name, &c->loc);
11362 return FAILURE;
11365 if (c->attr.proc_pointer && c->ts.interface)
11367 if (c->ts.interface->attr.procedure && !sym->attr.vtype)
11368 gfc_error ("Interface '%s', used by procedure pointer component "
11369 "'%s' at %L, is declared in a later PROCEDURE statement",
11370 c->ts.interface->name, c->name, &c->loc);
11372 /* Get the attributes from the interface (now resolved). */
11373 if (c->ts.interface->attr.if_source
11374 || c->ts.interface->attr.intrinsic)
11376 gfc_symbol *ifc = c->ts.interface;
11378 if (ifc->formal && !ifc->formal_ns)
11379 resolve_symbol (ifc);
11381 if (ifc->attr.intrinsic)
11382 resolve_intrinsic (ifc, &ifc->declared_at);
11384 if (ifc->result)
11386 c->ts = ifc->result->ts;
11387 c->attr.allocatable = ifc->result->attr.allocatable;
11388 c->attr.pointer = ifc->result->attr.pointer;
11389 c->attr.dimension = ifc->result->attr.dimension;
11390 c->as = gfc_copy_array_spec (ifc->result->as);
11392 else
11394 c->ts = ifc->ts;
11395 c->attr.allocatable = ifc->attr.allocatable;
11396 c->attr.pointer = ifc->attr.pointer;
11397 c->attr.dimension = ifc->attr.dimension;
11398 c->as = gfc_copy_array_spec (ifc->as);
11400 c->ts.interface = ifc;
11401 c->attr.function = ifc->attr.function;
11402 c->attr.subroutine = ifc->attr.subroutine;
11403 gfc_copy_formal_args_ppc (c, ifc);
11405 c->attr.pure = ifc->attr.pure;
11406 c->attr.elemental = ifc->attr.elemental;
11407 c->attr.recursive = ifc->attr.recursive;
11408 c->attr.always_explicit = ifc->attr.always_explicit;
11409 c->attr.ext_attr |= ifc->attr.ext_attr;
11410 /* Replace symbols in array spec. */
11411 if (c->as)
11413 int i;
11414 for (i = 0; i < c->as->rank; i++)
11416 gfc_expr_replace_comp (c->as->lower[i], c);
11417 gfc_expr_replace_comp (c->as->upper[i], c);
11420 /* Copy char length. */
11421 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
11423 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
11424 gfc_expr_replace_comp (cl->length, c);
11425 if (cl->length && !cl->resolved
11426 && gfc_resolve_expr (cl->length) == FAILURE)
11427 return FAILURE;
11428 c->ts.u.cl = cl;
11431 else if (!sym->attr.vtype && c->ts.interface->name[0] != '\0')
11433 gfc_error ("Interface '%s' of procedure pointer component "
11434 "'%s' at %L must be explicit", c->ts.interface->name,
11435 c->name, &c->loc);
11436 return FAILURE;
11439 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
11441 /* Since PPCs are not implicitly typed, a PPC without an explicit
11442 interface must be a subroutine. */
11443 gfc_add_subroutine (&c->attr, c->name, &c->loc);
11446 /* Procedure pointer components: Check PASS arg. */
11447 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
11448 && !sym->attr.vtype)
11450 gfc_symbol* me_arg;
11452 if (c->tb->pass_arg)
11454 gfc_formal_arglist* i;
11456 /* If an explicit passing argument name is given, walk the arg-list
11457 and look for it. */
11459 me_arg = NULL;
11460 c->tb->pass_arg_num = 1;
11461 for (i = c->formal; i; i = i->next)
11463 if (!strcmp (i->sym->name, c->tb->pass_arg))
11465 me_arg = i->sym;
11466 break;
11468 c->tb->pass_arg_num++;
11471 if (!me_arg)
11473 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
11474 "at %L has no argument '%s'", c->name,
11475 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
11476 c->tb->error = 1;
11477 return FAILURE;
11480 else
11482 /* Otherwise, take the first one; there should in fact be at least
11483 one. */
11484 c->tb->pass_arg_num = 1;
11485 if (!c->formal)
11487 gfc_error ("Procedure pointer component '%s' with PASS at %L "
11488 "must have at least one argument",
11489 c->name, &c->loc);
11490 c->tb->error = 1;
11491 return FAILURE;
11493 me_arg = c->formal->sym;
11496 /* Now check that the argument-type matches. */
11497 gcc_assert (me_arg);
11498 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
11499 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
11500 || (me_arg->ts.type == BT_CLASS
11501 && CLASS_DATA (me_arg)->ts.u.derived != sym))
11503 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11504 " the derived type '%s'", me_arg->name, c->name,
11505 me_arg->name, &c->loc, sym->name);
11506 c->tb->error = 1;
11507 return FAILURE;
11510 /* Check for C453. */
11511 if (me_arg->attr.dimension)
11513 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11514 "must be scalar", me_arg->name, c->name, me_arg->name,
11515 &c->loc);
11516 c->tb->error = 1;
11517 return FAILURE;
11520 if (me_arg->attr.pointer)
11522 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11523 "may not have the POINTER attribute", me_arg->name,
11524 c->name, me_arg->name, &c->loc);
11525 c->tb->error = 1;
11526 return FAILURE;
11529 if (me_arg->attr.allocatable)
11531 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11532 "may not be ALLOCATABLE", me_arg->name, c->name,
11533 me_arg->name, &c->loc);
11534 c->tb->error = 1;
11535 return FAILURE;
11538 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
11539 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11540 " at %L", c->name, &c->loc);
11544 /* Check type-spec if this is not the parent-type component. */
11545 if ((!sym->attr.extension || c != sym->components) && !sym->attr.vtype
11546 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
11547 return FAILURE;
11549 /* If this type is an extension, set the accessibility of the parent
11550 component. */
11551 if (super_type && c == sym->components
11552 && strcmp (super_type->name, c->name) == 0)
11553 c->attr.access = super_type->attr.access;
11555 /* If this type is an extension, see if this component has the same name
11556 as an inherited type-bound procedure. */
11557 if (super_type && !sym->attr.is_class
11558 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
11560 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
11561 " inherited type-bound procedure",
11562 c->name, sym->name, &c->loc);
11563 return FAILURE;
11566 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
11568 if (c->ts.u.cl->length == NULL
11569 || (resolve_charlen (c->ts.u.cl) == FAILURE)
11570 || !gfc_is_constant_expr (c->ts.u.cl->length))
11572 gfc_error ("Character length of component '%s' needs to "
11573 "be a constant specification expression at %L",
11574 c->name,
11575 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
11576 return FAILURE;
11580 if (c->ts.type == BT_DERIVED
11581 && sym->component_access != ACCESS_PRIVATE
11582 && gfc_check_access (sym->attr.access, sym->ns->default_access)
11583 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
11584 && !c->ts.u.derived->attr.use_assoc
11585 && !gfc_check_access (c->ts.u.derived->attr.access,
11586 c->ts.u.derived->ns->default_access)
11587 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
11588 "is a PRIVATE type and cannot be a component of "
11589 "'%s', which is PUBLIC at %L", c->name,
11590 sym->name, &sym->declared_at) == FAILURE)
11591 return FAILURE;
11593 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
11595 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
11596 "type %s", c->name, &c->loc, sym->name);
11597 return FAILURE;
11600 if (sym->attr.sequence)
11602 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
11604 gfc_error ("Component %s of SEQUENCE type declared at %L does "
11605 "not have the SEQUENCE attribute",
11606 c->ts.u.derived->name, &sym->declared_at);
11607 return FAILURE;
11611 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
11612 && c->attr.pointer && c->ts.u.derived->components == NULL
11613 && !c->ts.u.derived->attr.zero_comp)
11615 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11616 "that has not been declared", c->name, sym->name,
11617 &c->loc);
11618 return FAILURE;
11621 if (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.class_pointer
11622 && CLASS_DATA (c)->ts.u.derived->components == NULL
11623 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp)
11625 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11626 "that has not been declared", c->name, sym->name,
11627 &c->loc);
11628 return FAILURE;
11631 /* C437. */
11632 if (c->ts.type == BT_CLASS
11633 && !(CLASS_DATA (c)->attr.class_pointer
11634 || CLASS_DATA (c)->attr.allocatable))
11636 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
11637 "or pointer", c->name, &c->loc);
11638 return FAILURE;
11641 /* Ensure that all the derived type components are put on the
11642 derived type list; even in formal namespaces, where derived type
11643 pointer components might not have been declared. */
11644 if (c->ts.type == BT_DERIVED
11645 && c->ts.u.derived
11646 && c->ts.u.derived->components
11647 && c->attr.pointer
11648 && sym != c->ts.u.derived)
11649 add_dt_to_dt_list (c->ts.u.derived);
11651 if (gfc_resolve_array_spec (c->as, !(c->attr.pointer
11652 || c->attr.proc_pointer
11653 || c->attr.allocatable)) == FAILURE)
11654 return FAILURE;
11657 /* Resolve the type-bound procedures. */
11658 if (resolve_typebound_procedures (sym) == FAILURE)
11659 return FAILURE;
11661 /* Resolve the finalizer procedures. */
11662 if (gfc_resolve_finalizers (sym) == FAILURE)
11663 return FAILURE;
11665 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
11666 all DEFERRED bindings are overridden. */
11667 if (super_type && super_type->attr.abstract && !sym->attr.abstract
11668 && !sym->attr.is_class
11669 && ensure_not_abstract (sym, super_type) == FAILURE)
11670 return FAILURE;
11672 /* Add derived type to the derived type list. */
11673 add_dt_to_dt_list (sym);
11675 return SUCCESS;
11679 static gfc_try
11680 resolve_fl_namelist (gfc_symbol *sym)
11682 gfc_namelist *nl;
11683 gfc_symbol *nlsym;
11685 for (nl = sym->namelist; nl; nl = nl->next)
11687 /* Reject namelist arrays of assumed shape. */
11688 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
11689 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
11690 "must not have assumed shape in namelist "
11691 "'%s' at %L", nl->sym->name, sym->name,
11692 &sym->declared_at) == FAILURE)
11693 return FAILURE;
11695 /* Reject namelist arrays that are not constant shape. */
11696 if (is_non_constant_shape_array (nl->sym))
11698 gfc_error ("NAMELIST array object '%s' must have constant "
11699 "shape in namelist '%s' at %L", nl->sym->name,
11700 sym->name, &sym->declared_at);
11701 return FAILURE;
11704 /* Namelist objects cannot have allocatable or pointer components. */
11705 if (nl->sym->ts.type != BT_DERIVED)
11706 continue;
11708 if (nl->sym->ts.u.derived->attr.alloc_comp)
11710 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
11711 "have ALLOCATABLE components",
11712 nl->sym->name, sym->name, &sym->declared_at);
11713 return FAILURE;
11716 if (nl->sym->ts.u.derived->attr.pointer_comp)
11718 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
11719 "have POINTER components",
11720 nl->sym->name, sym->name, &sym->declared_at);
11721 return FAILURE;
11725 /* Reject PRIVATE objects in a PUBLIC namelist. */
11726 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
11728 for (nl = sym->namelist; nl; nl = nl->next)
11730 if (!nl->sym->attr.use_assoc
11731 && !is_sym_host_assoc (nl->sym, sym->ns)
11732 && !gfc_check_access(nl->sym->attr.access,
11733 nl->sym->ns->default_access))
11735 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
11736 "cannot be member of PUBLIC namelist '%s' at %L",
11737 nl->sym->name, sym->name, &sym->declared_at);
11738 return FAILURE;
11741 /* Types with private components that came here by USE-association. */
11742 if (nl->sym->ts.type == BT_DERIVED
11743 && derived_inaccessible (nl->sym->ts.u.derived))
11745 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
11746 "components and cannot be member of namelist '%s' at %L",
11747 nl->sym->name, sym->name, &sym->declared_at);
11748 return FAILURE;
11751 /* Types with private components that are defined in the same module. */
11752 if (nl->sym->ts.type == BT_DERIVED
11753 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
11754 && !gfc_check_access (nl->sym->ts.u.derived->attr.private_comp
11755 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
11756 nl->sym->ns->default_access))
11758 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
11759 "cannot be a member of PUBLIC namelist '%s' at %L",
11760 nl->sym->name, sym->name, &sym->declared_at);
11761 return FAILURE;
11767 /* 14.1.2 A module or internal procedure represent local entities
11768 of the same type as a namelist member and so are not allowed. */
11769 for (nl = sym->namelist; nl; nl = nl->next)
11771 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
11772 continue;
11774 if (nl->sym->attr.function && nl->sym == nl->sym->result)
11775 if ((nl->sym == sym->ns->proc_name)
11777 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
11778 continue;
11780 nlsym = NULL;
11781 if (nl->sym && nl->sym->name)
11782 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
11783 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
11785 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
11786 "attribute in '%s' at %L", nlsym->name,
11787 &sym->declared_at);
11788 return FAILURE;
11792 return SUCCESS;
11796 static gfc_try
11797 resolve_fl_parameter (gfc_symbol *sym)
11799 /* A parameter array's shape needs to be constant. */
11800 if (sym->as != NULL
11801 && (sym->as->type == AS_DEFERRED
11802 || is_non_constant_shape_array (sym)))
11804 gfc_error ("Parameter array '%s' at %L cannot be automatic "
11805 "or of deferred shape", sym->name, &sym->declared_at);
11806 return FAILURE;
11809 /* Make sure a parameter that has been implicitly typed still
11810 matches the implicit type, since PARAMETER statements can precede
11811 IMPLICIT statements. */
11812 if (sym->attr.implicit_type
11813 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
11814 sym->ns)))
11816 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
11817 "later IMPLICIT type", sym->name, &sym->declared_at);
11818 return FAILURE;
11821 /* Make sure the types of derived parameters are consistent. This
11822 type checking is deferred until resolution because the type may
11823 refer to a derived type from the host. */
11824 if (sym->ts.type == BT_DERIVED
11825 && !gfc_compare_types (&sym->ts, &sym->value->ts))
11827 gfc_error ("Incompatible derived type in PARAMETER at %L",
11828 &sym->value->where);
11829 return FAILURE;
11831 return SUCCESS;
11835 /* Do anything necessary to resolve a symbol. Right now, we just
11836 assume that an otherwise unknown symbol is a variable. This sort
11837 of thing commonly happens for symbols in module. */
11839 static void
11840 resolve_symbol (gfc_symbol *sym)
11842 int check_constant, mp_flag;
11843 gfc_symtree *symtree;
11844 gfc_symtree *this_symtree;
11845 gfc_namespace *ns;
11846 gfc_component *c;
11848 /* Avoid double resolution of function result symbols. */
11849 if ((sym->result || sym->attr.result) && !sym->attr.dummy
11850 && (sym->ns != gfc_current_ns))
11851 return;
11853 if (sym->attr.flavor == FL_UNKNOWN)
11856 /* If we find that a flavorless symbol is an interface in one of the
11857 parent namespaces, find its symtree in this namespace, free the
11858 symbol and set the symtree to point to the interface symbol. */
11859 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
11861 symtree = gfc_find_symtree (ns->sym_root, sym->name);
11862 if (symtree && (symtree->n.sym->generic ||
11863 (symtree->n.sym->attr.flavor == FL_PROCEDURE
11864 && sym->ns->construct_entities)))
11866 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
11867 sym->name);
11868 gfc_release_symbol (sym);
11869 symtree->n.sym->refs++;
11870 this_symtree->n.sym = symtree->n.sym;
11871 return;
11875 /* Otherwise give it a flavor according to such attributes as
11876 it has. */
11877 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
11878 sym->attr.flavor = FL_VARIABLE;
11879 else
11881 sym->attr.flavor = FL_PROCEDURE;
11882 if (sym->attr.dimension)
11883 sym->attr.function = 1;
11887 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
11888 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
11890 if (sym->attr.procedure && sym->ts.interface
11891 && sym->attr.if_source != IFSRC_DECL
11892 && resolve_procedure_interface (sym) == FAILURE)
11893 return;
11895 if (sym->attr.is_protected && !sym->attr.proc_pointer
11896 && (sym->attr.procedure || sym->attr.external))
11898 if (sym->attr.external)
11899 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
11900 "at %L", &sym->declared_at);
11901 else
11902 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
11903 "at %L", &sym->declared_at);
11905 return;
11909 /* F2008, C530. */
11910 if (sym->attr.contiguous
11911 && (!sym->attr.dimension || (sym->as->type != AS_ASSUMED_SHAPE
11912 && !sym->attr.pointer)))
11914 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
11915 "array pointer or an assumed-shape array", sym->name,
11916 &sym->declared_at);
11917 return;
11920 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
11921 return;
11923 /* Symbols that are module procedures with results (functions) have
11924 the types and array specification copied for type checking in
11925 procedures that call them, as well as for saving to a module
11926 file. These symbols can't stand the scrutiny that their results
11927 can. */
11928 mp_flag = (sym->result != NULL && sym->result != sym);
11930 /* Make sure that the intrinsic is consistent with its internal
11931 representation. This needs to be done before assigning a default
11932 type to avoid spurious warnings. */
11933 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
11934 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
11935 return;
11937 /* Resolve associate names. */
11938 if (sym->assoc)
11939 resolve_assoc_var (sym, true);
11941 /* Assign default type to symbols that need one and don't have one. */
11942 if (sym->ts.type == BT_UNKNOWN)
11944 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
11945 gfc_set_default_type (sym, 1, NULL);
11947 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
11948 && !sym->attr.function && !sym->attr.subroutine
11949 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
11950 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
11952 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
11954 /* The specific case of an external procedure should emit an error
11955 in the case that there is no implicit type. */
11956 if (!mp_flag)
11957 gfc_set_default_type (sym, sym->attr.external, NULL);
11958 else
11960 /* Result may be in another namespace. */
11961 resolve_symbol (sym->result);
11963 if (!sym->result->attr.proc_pointer)
11965 sym->ts = sym->result->ts;
11966 sym->as = gfc_copy_array_spec (sym->result->as);
11967 sym->attr.dimension = sym->result->attr.dimension;
11968 sym->attr.pointer = sym->result->attr.pointer;
11969 sym->attr.allocatable = sym->result->attr.allocatable;
11970 sym->attr.contiguous = sym->result->attr.contiguous;
11976 /* Assumed size arrays and assumed shape arrays must be dummy
11977 arguments. Array-spec's of implied-shape should have been resolved to
11978 AS_EXPLICIT already. */
11980 if (sym->as)
11982 gcc_assert (sym->as->type != AS_IMPLIED_SHAPE);
11983 if (((sym->as->type == AS_ASSUMED_SIZE && !sym->as->cp_was_assumed)
11984 || sym->as->type == AS_ASSUMED_SHAPE)
11985 && sym->attr.dummy == 0)
11987 if (sym->as->type == AS_ASSUMED_SIZE)
11988 gfc_error ("Assumed size array at %L must be a dummy argument",
11989 &sym->declared_at);
11990 else
11991 gfc_error ("Assumed shape array at %L must be a dummy argument",
11992 &sym->declared_at);
11993 return;
11997 /* Make sure symbols with known intent or optional are really dummy
11998 variable. Because of ENTRY statement, this has to be deferred
11999 until resolution time. */
12001 if (!sym->attr.dummy
12002 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
12004 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
12005 return;
12008 if (sym->attr.value && !sym->attr.dummy)
12010 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
12011 "it is not a dummy argument", sym->name, &sym->declared_at);
12012 return;
12015 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
12017 gfc_charlen *cl = sym->ts.u.cl;
12018 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12020 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12021 "attribute must have constant length",
12022 sym->name, &sym->declared_at);
12023 return;
12026 if (sym->ts.is_c_interop
12027 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
12029 gfc_error ("C interoperable character dummy variable '%s' at %L "
12030 "with VALUE attribute must have length one",
12031 sym->name, &sym->declared_at);
12032 return;
12036 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
12037 do this for something that was implicitly typed because that is handled
12038 in gfc_set_default_type. Handle dummy arguments and procedure
12039 definitions separately. Also, anything that is use associated is not
12040 handled here but instead is handled in the module it is declared in.
12041 Finally, derived type definitions are allowed to be BIND(C) since that
12042 only implies that they're interoperable, and they are checked fully for
12043 interoperability when a variable is declared of that type. */
12044 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
12045 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
12046 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
12048 gfc_try t = SUCCESS;
12050 /* First, make sure the variable is declared at the
12051 module-level scope (J3/04-007, Section 15.3). */
12052 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
12053 sym->attr.in_common == 0)
12055 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
12056 "is neither a COMMON block nor declared at the "
12057 "module level scope", sym->name, &(sym->declared_at));
12058 t = FAILURE;
12060 else if (sym->common_head != NULL)
12062 t = verify_com_block_vars_c_interop (sym->common_head);
12064 else
12066 /* If type() declaration, we need to verify that the components
12067 of the given type are all C interoperable, etc. */
12068 if (sym->ts.type == BT_DERIVED &&
12069 sym->ts.u.derived->attr.is_c_interop != 1)
12071 /* Make sure the user marked the derived type as BIND(C). If
12072 not, call the verify routine. This could print an error
12073 for the derived type more than once if multiple variables
12074 of that type are declared. */
12075 if (sym->ts.u.derived->attr.is_bind_c != 1)
12076 verify_bind_c_derived_type (sym->ts.u.derived);
12077 t = FAILURE;
12080 /* Verify the variable itself as C interoperable if it
12081 is BIND(C). It is not possible for this to succeed if
12082 the verify_bind_c_derived_type failed, so don't have to handle
12083 any error returned by verify_bind_c_derived_type. */
12084 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
12085 sym->common_block);
12088 if (t == FAILURE)
12090 /* clear the is_bind_c flag to prevent reporting errors more than
12091 once if something failed. */
12092 sym->attr.is_bind_c = 0;
12093 return;
12097 /* If a derived type symbol has reached this point, without its
12098 type being declared, we have an error. Notice that most
12099 conditions that produce undefined derived types have already
12100 been dealt with. However, the likes of:
12101 implicit type(t) (t) ..... call foo (t) will get us here if
12102 the type is not declared in the scope of the implicit
12103 statement. Change the type to BT_UNKNOWN, both because it is so
12104 and to prevent an ICE. */
12105 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
12106 && !sym->ts.u.derived->attr.zero_comp)
12108 gfc_error ("The derived type '%s' at %L is of type '%s', "
12109 "which has not been defined", sym->name,
12110 &sym->declared_at, sym->ts.u.derived->name);
12111 sym->ts.type = BT_UNKNOWN;
12112 return;
12115 /* Make sure that the derived type has been resolved and that the
12116 derived type is visible in the symbol's namespace, if it is a
12117 module function and is not PRIVATE. */
12118 if (sym->ts.type == BT_DERIVED
12119 && sym->ts.u.derived->attr.use_assoc
12120 && sym->ns->proc_name
12121 && sym->ns->proc_name->attr.flavor == FL_MODULE)
12123 gfc_symbol *ds;
12125 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
12126 return;
12128 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
12129 if (!ds && sym->attr.function
12130 && gfc_check_access (sym->attr.access, sym->ns->default_access))
12132 symtree = gfc_new_symtree (&sym->ns->sym_root,
12133 sym->ts.u.derived->name);
12134 symtree->n.sym = sym->ts.u.derived;
12135 sym->ts.u.derived->refs++;
12139 /* Unless the derived-type declaration is use associated, Fortran 95
12140 does not allow public entries of private derived types.
12141 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
12142 161 in 95-006r3. */
12143 if (sym->ts.type == BT_DERIVED
12144 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
12145 && !sym->ts.u.derived->attr.use_assoc
12146 && gfc_check_access (sym->attr.access, sym->ns->default_access)
12147 && !gfc_check_access (sym->ts.u.derived->attr.access,
12148 sym->ts.u.derived->ns->default_access)
12149 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
12150 "of PRIVATE derived type '%s'",
12151 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
12152 : "variable", sym->name, &sym->declared_at,
12153 sym->ts.u.derived->name) == FAILURE)
12154 return;
12156 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
12157 default initialization is defined (5.1.2.4.4). */
12158 if (sym->ts.type == BT_DERIVED
12159 && sym->attr.dummy
12160 && sym->attr.intent == INTENT_OUT
12161 && sym->as
12162 && sym->as->type == AS_ASSUMED_SIZE)
12164 for (c = sym->ts.u.derived->components; c; c = c->next)
12166 if (c->initializer)
12168 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
12169 "ASSUMED SIZE and so cannot have a default initializer",
12170 sym->name, &sym->declared_at);
12171 return;
12176 /* F2008, C526. */
12177 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12178 || sym->attr.codimension)
12179 && sym->attr.result)
12180 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
12181 "a coarray component", sym->name, &sym->declared_at);
12183 /* F2008, C524. */
12184 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
12185 && sym->ts.u.derived->ts.is_iso_c)
12186 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12187 "shall not be a coarray", sym->name, &sym->declared_at);
12189 /* F2008, C525. */
12190 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp
12191 && (sym->attr.codimension || sym->attr.pointer || sym->attr.dimension
12192 || sym->attr.allocatable))
12193 gfc_error ("Variable '%s' at %L with coarray component "
12194 "shall be a nonpointer, nonallocatable scalar",
12195 sym->name, &sym->declared_at);
12197 /* F2008, C526. The function-result case was handled above. */
12198 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12199 || sym->attr.codimension)
12200 && !(sym->attr.allocatable || sym->attr.dummy || sym->attr.save
12201 || sym->ns->proc_name->attr.flavor == FL_MODULE
12202 || sym->ns->proc_name->attr.is_main_program
12203 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
12204 gfc_error ("Variable '%s' at %L is a coarray or has a coarray "
12205 "component and is not ALLOCATABLE, SAVE nor a "
12206 "dummy argument", sym->name, &sym->declared_at);
12207 /* F2008, C528. */ /* FIXME: sym->as check due to PR 43412. */
12208 else if (sym->attr.codimension && !sym->attr.allocatable
12209 && sym->as && sym->as->cotype == AS_DEFERRED)
12210 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
12211 "deferred shape", sym->name, &sym->declared_at);
12212 else if (sym->attr.codimension && sym->attr.allocatable
12213 && (sym->as->type != AS_DEFERRED || sym->as->cotype != AS_DEFERRED))
12214 gfc_error ("Allocatable coarray variable '%s' at %L must have "
12215 "deferred shape", sym->name, &sym->declared_at);
12218 /* F2008, C541. */
12219 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12220 || (sym->attr.codimension && sym->attr.allocatable))
12221 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
12222 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
12223 "allocatable coarray or have coarray components",
12224 sym->name, &sym->declared_at);
12226 if (sym->attr.codimension && sym->attr.dummy
12227 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
12228 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
12229 "procedure '%s'", sym->name, &sym->declared_at,
12230 sym->ns->proc_name->name);
12232 switch (sym->attr.flavor)
12234 case FL_VARIABLE:
12235 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
12236 return;
12237 break;
12239 case FL_PROCEDURE:
12240 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
12241 return;
12242 break;
12244 case FL_NAMELIST:
12245 if (resolve_fl_namelist (sym) == FAILURE)
12246 return;
12247 break;
12249 case FL_PARAMETER:
12250 if (resolve_fl_parameter (sym) == FAILURE)
12251 return;
12252 break;
12254 default:
12255 break;
12258 /* Resolve array specifier. Check as well some constraints
12259 on COMMON blocks. */
12261 check_constant = sym->attr.in_common && !sym->attr.pointer;
12263 /* Set the formal_arg_flag so that check_conflict will not throw
12264 an error for host associated variables in the specification
12265 expression for an array_valued function. */
12266 if (sym->attr.function && sym->as)
12267 formal_arg_flag = 1;
12269 gfc_resolve_array_spec (sym->as, check_constant);
12271 formal_arg_flag = 0;
12273 /* Resolve formal namespaces. */
12274 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
12275 && !sym->attr.contained && !sym->attr.intrinsic)
12276 gfc_resolve (sym->formal_ns);
12278 /* Make sure the formal namespace is present. */
12279 if (sym->formal && !sym->formal_ns)
12281 gfc_formal_arglist *formal = sym->formal;
12282 while (formal && !formal->sym)
12283 formal = formal->next;
12285 if (formal)
12287 sym->formal_ns = formal->sym->ns;
12288 sym->formal_ns->refs++;
12292 /* Check threadprivate restrictions. */
12293 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
12294 && (!sym->attr.in_common
12295 && sym->module == NULL
12296 && (sym->ns->proc_name == NULL
12297 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
12298 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
12300 /* If we have come this far we can apply default-initializers, as
12301 described in 14.7.5, to those variables that have not already
12302 been assigned one. */
12303 if (sym->ts.type == BT_DERIVED
12304 && sym->ns == gfc_current_ns
12305 && !sym->value
12306 && !sym->attr.allocatable
12307 && !sym->attr.alloc_comp)
12309 symbol_attribute *a = &sym->attr;
12311 if ((!a->save && !a->dummy && !a->pointer
12312 && !a->in_common && !a->use_assoc
12313 && (a->referenced || a->result)
12314 && !(a->function && sym != sym->result))
12315 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
12316 apply_default_init (sym);
12319 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
12320 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
12321 && !CLASS_DATA (sym)->attr.class_pointer
12322 && !CLASS_DATA (sym)->attr.allocatable)
12323 apply_default_init (sym);
12325 /* If this symbol has a type-spec, check it. */
12326 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
12327 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
12328 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
12329 == FAILURE)
12330 return;
12334 /************* Resolve DATA statements *************/
12336 static struct
12338 gfc_data_value *vnode;
12339 mpz_t left;
12341 values;
12344 /* Advance the values structure to point to the next value in the data list. */
12346 static gfc_try
12347 next_data_value (void)
12349 while (mpz_cmp_ui (values.left, 0) == 0)
12352 if (values.vnode->next == NULL)
12353 return FAILURE;
12355 values.vnode = values.vnode->next;
12356 mpz_set (values.left, values.vnode->repeat);
12359 return SUCCESS;
12363 static gfc_try
12364 check_data_variable (gfc_data_variable *var, locus *where)
12366 gfc_expr *e;
12367 mpz_t size;
12368 mpz_t offset;
12369 gfc_try t;
12370 ar_type mark = AR_UNKNOWN;
12371 int i;
12372 mpz_t section_index[GFC_MAX_DIMENSIONS];
12373 gfc_ref *ref;
12374 gfc_array_ref *ar;
12375 gfc_symbol *sym;
12376 int has_pointer;
12378 if (gfc_resolve_expr (var->expr) == FAILURE)
12379 return FAILURE;
12381 ar = NULL;
12382 mpz_init_set_si (offset, 0);
12383 e = var->expr;
12385 if (e->expr_type != EXPR_VARIABLE)
12386 gfc_internal_error ("check_data_variable(): Bad expression");
12388 sym = e->symtree->n.sym;
12390 if (sym->ns->is_block_data && !sym->attr.in_common)
12392 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
12393 sym->name, &sym->declared_at);
12396 if (e->ref == NULL && sym->as)
12398 gfc_error ("DATA array '%s' at %L must be specified in a previous"
12399 " declaration", sym->name, where);
12400 return FAILURE;
12403 has_pointer = sym->attr.pointer;
12405 for (ref = e->ref; ref; ref = ref->next)
12407 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
12408 has_pointer = 1;
12410 if (ref->type == REF_ARRAY && ref->u.ar.codimen)
12412 gfc_error ("DATA element '%s' at %L cannot have a coindex",
12413 sym->name, where);
12414 return FAILURE;
12417 if (has_pointer
12418 && ref->type == REF_ARRAY
12419 && ref->u.ar.type != AR_FULL)
12421 gfc_error ("DATA element '%s' at %L is a pointer and so must "
12422 "be a full array", sym->name, where);
12423 return FAILURE;
12427 if (e->rank == 0 || has_pointer)
12429 mpz_init_set_ui (size, 1);
12430 ref = NULL;
12432 else
12434 ref = e->ref;
12436 /* Find the array section reference. */
12437 for (ref = e->ref; ref; ref = ref->next)
12439 if (ref->type != REF_ARRAY)
12440 continue;
12441 if (ref->u.ar.type == AR_ELEMENT)
12442 continue;
12443 break;
12445 gcc_assert (ref);
12447 /* Set marks according to the reference pattern. */
12448 switch (ref->u.ar.type)
12450 case AR_FULL:
12451 mark = AR_FULL;
12452 break;
12454 case AR_SECTION:
12455 ar = &ref->u.ar;
12456 /* Get the start position of array section. */
12457 gfc_get_section_index (ar, section_index, &offset);
12458 mark = AR_SECTION;
12459 break;
12461 default:
12462 gcc_unreachable ();
12465 if (gfc_array_size (e, &size) == FAILURE)
12467 gfc_error ("Nonconstant array section at %L in DATA statement",
12468 &e->where);
12469 mpz_clear (offset);
12470 return FAILURE;
12474 t = SUCCESS;
12476 while (mpz_cmp_ui (size, 0) > 0)
12478 if (next_data_value () == FAILURE)
12480 gfc_error ("DATA statement at %L has more variables than values",
12481 where);
12482 t = FAILURE;
12483 break;
12486 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
12487 if (t == FAILURE)
12488 break;
12490 /* If we have more than one element left in the repeat count,
12491 and we have more than one element left in the target variable,
12492 then create a range assignment. */
12493 /* FIXME: Only done for full arrays for now, since array sections
12494 seem tricky. */
12495 if (mark == AR_FULL && ref && ref->next == NULL
12496 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
12498 mpz_t range;
12500 if (mpz_cmp (size, values.left) >= 0)
12502 mpz_init_set (range, values.left);
12503 mpz_sub (size, size, values.left);
12504 mpz_set_ui (values.left, 0);
12506 else
12508 mpz_init_set (range, size);
12509 mpz_sub (values.left, values.left, size);
12510 mpz_set_ui (size, 0);
12513 t = gfc_assign_data_value_range (var->expr, values.vnode->expr,
12514 offset, range);
12516 mpz_add (offset, offset, range);
12517 mpz_clear (range);
12519 if (t == FAILURE)
12520 break;
12523 /* Assign initial value to symbol. */
12524 else
12526 mpz_sub_ui (values.left, values.left, 1);
12527 mpz_sub_ui (size, size, 1);
12529 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
12530 if (t == FAILURE)
12531 break;
12533 if (mark == AR_FULL)
12534 mpz_add_ui (offset, offset, 1);
12536 /* Modify the array section indexes and recalculate the offset
12537 for next element. */
12538 else if (mark == AR_SECTION)
12539 gfc_advance_section (section_index, ar, &offset);
12543 if (mark == AR_SECTION)
12545 for (i = 0; i < ar->dimen; i++)
12546 mpz_clear (section_index[i]);
12549 mpz_clear (size);
12550 mpz_clear (offset);
12552 return t;
12556 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
12558 /* Iterate over a list of elements in a DATA statement. */
12560 static gfc_try
12561 traverse_data_list (gfc_data_variable *var, locus *where)
12563 mpz_t trip;
12564 iterator_stack frame;
12565 gfc_expr *e, *start, *end, *step;
12566 gfc_try retval = SUCCESS;
12568 mpz_init (frame.value);
12569 mpz_init (trip);
12571 start = gfc_copy_expr (var->iter.start);
12572 end = gfc_copy_expr (var->iter.end);
12573 step = gfc_copy_expr (var->iter.step);
12575 if (gfc_simplify_expr (start, 1) == FAILURE
12576 || start->expr_type != EXPR_CONSTANT)
12578 gfc_error ("start of implied-do loop at %L could not be "
12579 "simplified to a constant value", &start->where);
12580 retval = FAILURE;
12581 goto cleanup;
12583 if (gfc_simplify_expr (end, 1) == FAILURE
12584 || end->expr_type != EXPR_CONSTANT)
12586 gfc_error ("end of implied-do loop at %L could not be "
12587 "simplified to a constant value", &start->where);
12588 retval = FAILURE;
12589 goto cleanup;
12591 if (gfc_simplify_expr (step, 1) == FAILURE
12592 || step->expr_type != EXPR_CONSTANT)
12594 gfc_error ("step of implied-do loop at %L could not be "
12595 "simplified to a constant value", &start->where);
12596 retval = FAILURE;
12597 goto cleanup;
12600 mpz_set (trip, end->value.integer);
12601 mpz_sub (trip, trip, start->value.integer);
12602 mpz_add (trip, trip, step->value.integer);
12604 mpz_div (trip, trip, step->value.integer);
12606 mpz_set (frame.value, start->value.integer);
12608 frame.prev = iter_stack;
12609 frame.variable = var->iter.var->symtree;
12610 iter_stack = &frame;
12612 while (mpz_cmp_ui (trip, 0) > 0)
12614 if (traverse_data_var (var->list, where) == FAILURE)
12616 retval = FAILURE;
12617 goto cleanup;
12620 e = gfc_copy_expr (var->expr);
12621 if (gfc_simplify_expr (e, 1) == FAILURE)
12623 gfc_free_expr (e);
12624 retval = FAILURE;
12625 goto cleanup;
12628 mpz_add (frame.value, frame.value, step->value.integer);
12630 mpz_sub_ui (trip, trip, 1);
12633 cleanup:
12634 mpz_clear (frame.value);
12635 mpz_clear (trip);
12637 gfc_free_expr (start);
12638 gfc_free_expr (end);
12639 gfc_free_expr (step);
12641 iter_stack = frame.prev;
12642 return retval;
12646 /* Type resolve variables in the variable list of a DATA statement. */
12648 static gfc_try
12649 traverse_data_var (gfc_data_variable *var, locus *where)
12651 gfc_try t;
12653 for (; var; var = var->next)
12655 if (var->expr == NULL)
12656 t = traverse_data_list (var, where);
12657 else
12658 t = check_data_variable (var, where);
12660 if (t == FAILURE)
12661 return FAILURE;
12664 return SUCCESS;
12668 /* Resolve the expressions and iterators associated with a data statement.
12669 This is separate from the assignment checking because data lists should
12670 only be resolved once. */
12672 static gfc_try
12673 resolve_data_variables (gfc_data_variable *d)
12675 for (; d; d = d->next)
12677 if (d->list == NULL)
12679 if (gfc_resolve_expr (d->expr) == FAILURE)
12680 return FAILURE;
12682 else
12684 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
12685 return FAILURE;
12687 if (resolve_data_variables (d->list) == FAILURE)
12688 return FAILURE;
12692 return SUCCESS;
12696 /* Resolve a single DATA statement. We implement this by storing a pointer to
12697 the value list into static variables, and then recursively traversing the
12698 variables list, expanding iterators and such. */
12700 static void
12701 resolve_data (gfc_data *d)
12704 if (resolve_data_variables (d->var) == FAILURE)
12705 return;
12707 values.vnode = d->value;
12708 if (d->value == NULL)
12709 mpz_set_ui (values.left, 0);
12710 else
12711 mpz_set (values.left, d->value->repeat);
12713 if (traverse_data_var (d->var, &d->where) == FAILURE)
12714 return;
12716 /* At this point, we better not have any values left. */
12718 if (next_data_value () == SUCCESS)
12719 gfc_error ("DATA statement at %L has more values than variables",
12720 &d->where);
12724 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
12725 accessed by host or use association, is a dummy argument to a pure function,
12726 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
12727 is storage associated with any such variable, shall not be used in the
12728 following contexts: (clients of this function). */
12730 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
12731 procedure. Returns zero if assignment is OK, nonzero if there is a
12732 problem. */
12734 gfc_impure_variable (gfc_symbol *sym)
12736 gfc_symbol *proc;
12737 gfc_namespace *ns;
12739 if (sym->attr.use_assoc || sym->attr.in_common)
12740 return 1;
12742 /* Check if the symbol's ns is inside the pure procedure. */
12743 for (ns = gfc_current_ns; ns; ns = ns->parent)
12745 if (ns == sym->ns)
12746 break;
12747 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
12748 return 1;
12751 proc = sym->ns->proc_name;
12752 if (sym->attr.dummy && gfc_pure (proc)
12753 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
12755 proc->attr.function))
12756 return 1;
12758 /* TODO: Sort out what can be storage associated, if anything, and include
12759 it here. In principle equivalences should be scanned but it does not
12760 seem to be possible to storage associate an impure variable this way. */
12761 return 0;
12765 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
12766 current namespace is inside a pure procedure. */
12769 gfc_pure (gfc_symbol *sym)
12771 symbol_attribute attr;
12772 gfc_namespace *ns;
12774 if (sym == NULL)
12776 /* Check if the current namespace or one of its parents
12777 belongs to a pure procedure. */
12778 for (ns = gfc_current_ns; ns; ns = ns->parent)
12780 sym = ns->proc_name;
12781 if (sym == NULL)
12782 return 0;
12783 attr = sym->attr;
12784 if (attr.flavor == FL_PROCEDURE && attr.pure)
12785 return 1;
12787 return 0;
12790 attr = sym->attr;
12792 return attr.flavor == FL_PROCEDURE && attr.pure;
12796 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
12797 checks if the current namespace is implicitly pure. Note that this
12798 function returns false for a PURE procedure. */
12801 gfc_implicit_pure (gfc_symbol *sym)
12803 symbol_attribute attr;
12805 if (sym == NULL)
12807 /* Check if the current namespace is implicit_pure. */
12808 sym = gfc_current_ns->proc_name;
12809 if (sym == NULL)
12810 return 0;
12811 attr = sym->attr;
12812 if (attr.flavor == FL_PROCEDURE
12813 && attr.implicit_pure && !attr.pure)
12814 return 1;
12815 return 0;
12818 attr = sym->attr;
12820 return attr.flavor == FL_PROCEDURE && attr.implicit_pure && !attr.pure;
12824 /* Test whether the current procedure is elemental or not. */
12827 gfc_elemental (gfc_symbol *sym)
12829 symbol_attribute attr;
12831 if (sym == NULL)
12832 sym = gfc_current_ns->proc_name;
12833 if (sym == NULL)
12834 return 0;
12835 attr = sym->attr;
12837 return attr.flavor == FL_PROCEDURE && attr.elemental;
12841 /* Warn about unused labels. */
12843 static void
12844 warn_unused_fortran_label (gfc_st_label *label)
12846 if (label == NULL)
12847 return;
12849 warn_unused_fortran_label (label->left);
12851 if (label->defined == ST_LABEL_UNKNOWN)
12852 return;
12854 switch (label->referenced)
12856 case ST_LABEL_UNKNOWN:
12857 gfc_warning ("Label %d at %L defined but not used", label->value,
12858 &label->where);
12859 break;
12861 case ST_LABEL_BAD_TARGET:
12862 gfc_warning ("Label %d at %L defined but cannot be used",
12863 label->value, &label->where);
12864 break;
12866 default:
12867 break;
12870 warn_unused_fortran_label (label->right);
12874 /* Returns the sequence type of a symbol or sequence. */
12876 static seq_type
12877 sequence_type (gfc_typespec ts)
12879 seq_type result;
12880 gfc_component *c;
12882 switch (ts.type)
12884 case BT_DERIVED:
12886 if (ts.u.derived->components == NULL)
12887 return SEQ_NONDEFAULT;
12889 result = sequence_type (ts.u.derived->components->ts);
12890 for (c = ts.u.derived->components->next; c; c = c->next)
12891 if (sequence_type (c->ts) != result)
12892 return SEQ_MIXED;
12894 return result;
12896 case BT_CHARACTER:
12897 if (ts.kind != gfc_default_character_kind)
12898 return SEQ_NONDEFAULT;
12900 return SEQ_CHARACTER;
12902 case BT_INTEGER:
12903 if (ts.kind != gfc_default_integer_kind)
12904 return SEQ_NONDEFAULT;
12906 return SEQ_NUMERIC;
12908 case BT_REAL:
12909 if (!(ts.kind == gfc_default_real_kind
12910 || ts.kind == gfc_default_double_kind))
12911 return SEQ_NONDEFAULT;
12913 return SEQ_NUMERIC;
12915 case BT_COMPLEX:
12916 if (ts.kind != gfc_default_complex_kind)
12917 return SEQ_NONDEFAULT;
12919 return SEQ_NUMERIC;
12921 case BT_LOGICAL:
12922 if (ts.kind != gfc_default_logical_kind)
12923 return SEQ_NONDEFAULT;
12925 return SEQ_NUMERIC;
12927 default:
12928 return SEQ_NONDEFAULT;
12933 /* Resolve derived type EQUIVALENCE object. */
12935 static gfc_try
12936 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
12938 gfc_component *c = derived->components;
12940 if (!derived)
12941 return SUCCESS;
12943 /* Shall not be an object of nonsequence derived type. */
12944 if (!derived->attr.sequence)
12946 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
12947 "attribute to be an EQUIVALENCE object", sym->name,
12948 &e->where);
12949 return FAILURE;
12952 /* Shall not have allocatable components. */
12953 if (derived->attr.alloc_comp)
12955 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
12956 "components to be an EQUIVALENCE object",sym->name,
12957 &e->where);
12958 return FAILURE;
12961 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
12963 gfc_error ("Derived type variable '%s' at %L with default "
12964 "initialization cannot be in EQUIVALENCE with a variable "
12965 "in COMMON", sym->name, &e->where);
12966 return FAILURE;
12969 for (; c ; c = c->next)
12971 if (c->ts.type == BT_DERIVED
12972 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
12973 return FAILURE;
12975 /* Shall not be an object of sequence derived type containing a pointer
12976 in the structure. */
12977 if (c->attr.pointer)
12979 gfc_error ("Derived type variable '%s' at %L with pointer "
12980 "component(s) cannot be an EQUIVALENCE object",
12981 sym->name, &e->where);
12982 return FAILURE;
12985 return SUCCESS;
12989 /* Resolve equivalence object.
12990 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
12991 an allocatable array, an object of nonsequence derived type, an object of
12992 sequence derived type containing a pointer at any level of component
12993 selection, an automatic object, a function name, an entry name, a result
12994 name, a named constant, a structure component, or a subobject of any of
12995 the preceding objects. A substring shall not have length zero. A
12996 derived type shall not have components with default initialization nor
12997 shall two objects of an equivalence group be initialized.
12998 Either all or none of the objects shall have an protected attribute.
12999 The simple constraints are done in symbol.c(check_conflict) and the rest
13000 are implemented here. */
13002 static void
13003 resolve_equivalence (gfc_equiv *eq)
13005 gfc_symbol *sym;
13006 gfc_symbol *first_sym;
13007 gfc_expr *e;
13008 gfc_ref *r;
13009 locus *last_where = NULL;
13010 seq_type eq_type, last_eq_type;
13011 gfc_typespec *last_ts;
13012 int object, cnt_protected;
13013 const char *msg;
13015 last_ts = &eq->expr->symtree->n.sym->ts;
13017 first_sym = eq->expr->symtree->n.sym;
13019 cnt_protected = 0;
13021 for (object = 1; eq; eq = eq->eq, object++)
13023 e = eq->expr;
13025 e->ts = e->symtree->n.sym->ts;
13026 /* match_varspec might not know yet if it is seeing
13027 array reference or substring reference, as it doesn't
13028 know the types. */
13029 if (e->ref && e->ref->type == REF_ARRAY)
13031 gfc_ref *ref = e->ref;
13032 sym = e->symtree->n.sym;
13034 if (sym->attr.dimension)
13036 ref->u.ar.as = sym->as;
13037 ref = ref->next;
13040 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
13041 if (e->ts.type == BT_CHARACTER
13042 && ref
13043 && ref->type == REF_ARRAY
13044 && ref->u.ar.dimen == 1
13045 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
13046 && ref->u.ar.stride[0] == NULL)
13048 gfc_expr *start = ref->u.ar.start[0];
13049 gfc_expr *end = ref->u.ar.end[0];
13050 void *mem = NULL;
13052 /* Optimize away the (:) reference. */
13053 if (start == NULL && end == NULL)
13055 if (e->ref == ref)
13056 e->ref = ref->next;
13057 else
13058 e->ref->next = ref->next;
13059 mem = ref;
13061 else
13063 ref->type = REF_SUBSTRING;
13064 if (start == NULL)
13065 start = gfc_get_int_expr (gfc_default_integer_kind,
13066 NULL, 1);
13067 ref->u.ss.start = start;
13068 if (end == NULL && e->ts.u.cl)
13069 end = gfc_copy_expr (e->ts.u.cl->length);
13070 ref->u.ss.end = end;
13071 ref->u.ss.length = e->ts.u.cl;
13072 e->ts.u.cl = NULL;
13074 ref = ref->next;
13075 gfc_free (mem);
13078 /* Any further ref is an error. */
13079 if (ref)
13081 gcc_assert (ref->type == REF_ARRAY);
13082 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
13083 &ref->u.ar.where);
13084 continue;
13088 if (gfc_resolve_expr (e) == FAILURE)
13089 continue;
13091 sym = e->symtree->n.sym;
13093 if (sym->attr.is_protected)
13094 cnt_protected++;
13095 if (cnt_protected > 0 && cnt_protected != object)
13097 gfc_error ("Either all or none of the objects in the "
13098 "EQUIVALENCE set at %L shall have the "
13099 "PROTECTED attribute",
13100 &e->where);
13101 break;
13104 /* Shall not equivalence common block variables in a PURE procedure. */
13105 if (sym->ns->proc_name
13106 && sym->ns->proc_name->attr.pure
13107 && sym->attr.in_common)
13109 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
13110 "object in the pure procedure '%s'",
13111 sym->name, &e->where, sym->ns->proc_name->name);
13112 break;
13115 /* Shall not be a named constant. */
13116 if (e->expr_type == EXPR_CONSTANT)
13118 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
13119 "object", sym->name, &e->where);
13120 continue;
13123 if (e->ts.type == BT_DERIVED
13124 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
13125 continue;
13127 /* Check that the types correspond correctly:
13128 Note 5.28:
13129 A numeric sequence structure may be equivalenced to another sequence
13130 structure, an object of default integer type, default real type, double
13131 precision real type, default logical type such that components of the
13132 structure ultimately only become associated to objects of the same
13133 kind. A character sequence structure may be equivalenced to an object
13134 of default character kind or another character sequence structure.
13135 Other objects may be equivalenced only to objects of the same type and
13136 kind parameters. */
13138 /* Identical types are unconditionally OK. */
13139 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
13140 goto identical_types;
13142 last_eq_type = sequence_type (*last_ts);
13143 eq_type = sequence_type (sym->ts);
13145 /* Since the pair of objects is not of the same type, mixed or
13146 non-default sequences can be rejected. */
13148 msg = "Sequence %s with mixed components in EQUIVALENCE "
13149 "statement at %L with different type objects";
13150 if ((object ==2
13151 && last_eq_type == SEQ_MIXED
13152 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
13153 == FAILURE)
13154 || (eq_type == SEQ_MIXED
13155 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13156 &e->where) == FAILURE))
13157 continue;
13159 msg = "Non-default type object or sequence %s in EQUIVALENCE "
13160 "statement at %L with objects of different type";
13161 if ((object ==2
13162 && last_eq_type == SEQ_NONDEFAULT
13163 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
13164 last_where) == FAILURE)
13165 || (eq_type == SEQ_NONDEFAULT
13166 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13167 &e->where) == FAILURE))
13168 continue;
13170 msg ="Non-CHARACTER object '%s' in default CHARACTER "
13171 "EQUIVALENCE statement at %L";
13172 if (last_eq_type == SEQ_CHARACTER
13173 && eq_type != SEQ_CHARACTER
13174 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13175 &e->where) == FAILURE)
13176 continue;
13178 msg ="Non-NUMERIC object '%s' in default NUMERIC "
13179 "EQUIVALENCE statement at %L";
13180 if (last_eq_type == SEQ_NUMERIC
13181 && eq_type != SEQ_NUMERIC
13182 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13183 &e->where) == FAILURE)
13184 continue;
13186 identical_types:
13187 last_ts =&sym->ts;
13188 last_where = &e->where;
13190 if (!e->ref)
13191 continue;
13193 /* Shall not be an automatic array. */
13194 if (e->ref->type == REF_ARRAY
13195 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
13197 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
13198 "an EQUIVALENCE object", sym->name, &e->where);
13199 continue;
13202 r = e->ref;
13203 while (r)
13205 /* Shall not be a structure component. */
13206 if (r->type == REF_COMPONENT)
13208 gfc_error ("Structure component '%s' at %L cannot be an "
13209 "EQUIVALENCE object",
13210 r->u.c.component->name, &e->where);
13211 break;
13214 /* A substring shall not have length zero. */
13215 if (r->type == REF_SUBSTRING)
13217 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
13219 gfc_error ("Substring at %L has length zero",
13220 &r->u.ss.start->where);
13221 break;
13224 r = r->next;
13230 /* Resolve function and ENTRY types, issue diagnostics if needed. */
13232 static void
13233 resolve_fntype (gfc_namespace *ns)
13235 gfc_entry_list *el;
13236 gfc_symbol *sym;
13238 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
13239 return;
13241 /* If there are any entries, ns->proc_name is the entry master
13242 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
13243 if (ns->entries)
13244 sym = ns->entries->sym;
13245 else
13246 sym = ns->proc_name;
13247 if (sym->result == sym
13248 && sym->ts.type == BT_UNKNOWN
13249 && gfc_set_default_type (sym, 0, NULL) == FAILURE
13250 && !sym->attr.untyped)
13252 gfc_error ("Function '%s' at %L has no IMPLICIT type",
13253 sym->name, &sym->declared_at);
13254 sym->attr.untyped = 1;
13257 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
13258 && !sym->attr.contained
13259 && !gfc_check_access (sym->ts.u.derived->attr.access,
13260 sym->ts.u.derived->ns->default_access)
13261 && gfc_check_access (sym->attr.access, sym->ns->default_access))
13263 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
13264 "%L of PRIVATE type '%s'", sym->name,
13265 &sym->declared_at, sym->ts.u.derived->name);
13268 if (ns->entries)
13269 for (el = ns->entries->next; el; el = el->next)
13271 if (el->sym->result == el->sym
13272 && el->sym->ts.type == BT_UNKNOWN
13273 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
13274 && !el->sym->attr.untyped)
13276 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
13277 el->sym->name, &el->sym->declared_at);
13278 el->sym->attr.untyped = 1;
13284 /* 12.3.2.1.1 Defined operators. */
13286 static gfc_try
13287 check_uop_procedure (gfc_symbol *sym, locus where)
13289 gfc_formal_arglist *formal;
13291 if (!sym->attr.function)
13293 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
13294 sym->name, &where);
13295 return FAILURE;
13298 if (sym->ts.type == BT_CHARACTER
13299 && !(sym->ts.u.cl && sym->ts.u.cl->length)
13300 && !(sym->result && sym->result->ts.u.cl
13301 && sym->result->ts.u.cl->length))
13303 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
13304 "character length", sym->name, &where);
13305 return FAILURE;
13308 formal = sym->formal;
13309 if (!formal || !formal->sym)
13311 gfc_error ("User operator procedure '%s' at %L must have at least "
13312 "one argument", sym->name, &where);
13313 return FAILURE;
13316 if (formal->sym->attr.intent != INTENT_IN)
13318 gfc_error ("First argument of operator interface at %L must be "
13319 "INTENT(IN)", &where);
13320 return FAILURE;
13323 if (formal->sym->attr.optional)
13325 gfc_error ("First argument of operator interface at %L cannot be "
13326 "optional", &where);
13327 return FAILURE;
13330 formal = formal->next;
13331 if (!formal || !formal->sym)
13332 return SUCCESS;
13334 if (formal->sym->attr.intent != INTENT_IN)
13336 gfc_error ("Second argument of operator interface at %L must be "
13337 "INTENT(IN)", &where);
13338 return FAILURE;
13341 if (formal->sym->attr.optional)
13343 gfc_error ("Second argument of operator interface at %L cannot be "
13344 "optional", &where);
13345 return FAILURE;
13348 if (formal->next)
13350 gfc_error ("Operator interface at %L must have, at most, two "
13351 "arguments", &where);
13352 return FAILURE;
13355 return SUCCESS;
13358 static void
13359 gfc_resolve_uops (gfc_symtree *symtree)
13361 gfc_interface *itr;
13363 if (symtree == NULL)
13364 return;
13366 gfc_resolve_uops (symtree->left);
13367 gfc_resolve_uops (symtree->right);
13369 for (itr = symtree->n.uop->op; itr; itr = itr->next)
13370 check_uop_procedure (itr->sym, itr->sym->declared_at);
13374 /* Examine all of the expressions associated with a program unit,
13375 assign types to all intermediate expressions, make sure that all
13376 assignments are to compatible types and figure out which names
13377 refer to which functions or subroutines. It doesn't check code
13378 block, which is handled by resolve_code. */
13380 static void
13381 resolve_types (gfc_namespace *ns)
13383 gfc_namespace *n;
13384 gfc_charlen *cl;
13385 gfc_data *d;
13386 gfc_equiv *eq;
13387 gfc_namespace* old_ns = gfc_current_ns;
13389 /* Check that all IMPLICIT types are ok. */
13390 if (!ns->seen_implicit_none)
13392 unsigned letter;
13393 for (letter = 0; letter != GFC_LETTERS; ++letter)
13394 if (ns->set_flag[letter]
13395 && resolve_typespec_used (&ns->default_type[letter],
13396 &ns->implicit_loc[letter],
13397 NULL) == FAILURE)
13398 return;
13401 gfc_current_ns = ns;
13403 resolve_entries (ns);
13405 resolve_common_vars (ns->blank_common.head, false);
13406 resolve_common_blocks (ns->common_root);
13408 resolve_contained_functions (ns);
13410 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
13412 for (cl = ns->cl_list; cl; cl = cl->next)
13413 resolve_charlen (cl);
13415 gfc_traverse_ns (ns, resolve_symbol);
13417 resolve_fntype (ns);
13419 for (n = ns->contained; n; n = n->sibling)
13421 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
13422 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
13423 "also be PURE", n->proc_name->name,
13424 &n->proc_name->declared_at);
13426 resolve_types (n);
13429 forall_flag = 0;
13430 gfc_check_interfaces (ns);
13432 gfc_traverse_ns (ns, resolve_values);
13434 if (ns->save_all)
13435 gfc_save_all (ns);
13437 iter_stack = NULL;
13438 for (d = ns->data; d; d = d->next)
13439 resolve_data (d);
13441 iter_stack = NULL;
13442 gfc_traverse_ns (ns, gfc_formalize_init_value);
13444 gfc_traverse_ns (ns, gfc_verify_binding_labels);
13446 if (ns->common_root != NULL)
13447 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
13449 for (eq = ns->equiv; eq; eq = eq->next)
13450 resolve_equivalence (eq);
13452 /* Warn about unused labels. */
13453 if (warn_unused_label)
13454 warn_unused_fortran_label (ns->st_labels);
13456 gfc_resolve_uops (ns->uop_root);
13458 gfc_current_ns = old_ns;
13462 /* Call resolve_code recursively. */
13464 static void
13465 resolve_codes (gfc_namespace *ns)
13467 gfc_namespace *n;
13468 bitmap_obstack old_obstack;
13470 if (ns->resolved == 1)
13471 return;
13473 for (n = ns->contained; n; n = n->sibling)
13474 resolve_codes (n);
13476 gfc_current_ns = ns;
13478 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
13479 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
13480 cs_base = NULL;
13482 /* Set to an out of range value. */
13483 current_entry_id = -1;
13485 old_obstack = labels_obstack;
13486 bitmap_obstack_initialize (&labels_obstack);
13488 resolve_code (ns->code, ns);
13490 bitmap_obstack_release (&labels_obstack);
13491 labels_obstack = old_obstack;
13495 /* This function is called after a complete program unit has been compiled.
13496 Its purpose is to examine all of the expressions associated with a program
13497 unit, assign types to all intermediate expressions, make sure that all
13498 assignments are to compatible types and figure out which names refer to
13499 which functions or subroutines. */
13501 void
13502 gfc_resolve (gfc_namespace *ns)
13504 gfc_namespace *old_ns;
13505 code_stack *old_cs_base;
13507 if (ns->resolved)
13508 return;
13510 ns->resolved = -1;
13511 old_ns = gfc_current_ns;
13512 old_cs_base = cs_base;
13514 resolve_types (ns);
13515 resolve_codes (ns);
13517 gfc_current_ns = old_ns;
13518 cs_base = old_cs_base;
13519 ns->resolved = 1;
13521 gfc_run_passes (ns);