1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
31 #include "target-memory.h" /* for gfc_simplify_transfer */
33 /* Types used in equivalence statements. */
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
41 /* Stack to keep track of the nesting of blocks as we move through the
42 code. See resolve_branch() and resolve_code(). */
44 typedef struct code_stack
46 struct gfc_code *head, *current;
47 struct code_stack *prev;
49 /* This bitmap keeps track of the targets valid for a branch from
50 inside this block except for END {IF|SELECT}s of enclosing
52 bitmap reachable_labels;
56 static code_stack *cs_base = NULL;
59 /* Nonzero if we're inside a FORALL block. */
61 static int forall_flag;
63 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
65 static int omp_workshare_flag;
67 /* Nonzero if we are processing a formal arglist. The corresponding function
68 resets the flag each time that it is read. */
69 static int formal_arg_flag = 0;
71 /* True if we are resolving a specification expression. */
72 static int specification_expr = 0;
74 /* The id of the last entry seen. */
75 static int current_entry_id;
77 /* We use bitmaps to determine if a branch target is valid. */
78 static bitmap_obstack labels_obstack;
81 gfc_is_formal_arg (void)
83 return formal_arg_flag;
86 /* Is the symbol host associated? */
88 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
90 for (ns = ns->parent; ns; ns = ns->parent)
99 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
100 an ABSTRACT derived-type. If where is not NULL, an error message with that
101 locus is printed, optionally using name. */
104 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
106 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
111 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
112 name, where, ts->u.derived->name);
114 gfc_error ("ABSTRACT type '%s' used at %L",
115 ts->u.derived->name, where);
125 /* Resolve types of formal argument lists. These have to be done early so that
126 the formal argument lists of module procedures can be copied to the
127 containing module before the individual procedures are resolved
128 individually. We also resolve argument lists of procedures in interface
129 blocks because they are self-contained scoping units.
131 Since a dummy argument cannot be a non-dummy procedure, the only
132 resort left for untyped names are the IMPLICIT types. */
135 resolve_formal_arglist (gfc_symbol *proc)
137 gfc_formal_arglist *f;
141 if (proc->result != NULL)
146 if (gfc_elemental (proc)
147 || sym->attr.pointer || sym->attr.allocatable
148 || (sym->as && sym->as->rank > 0))
150 proc->attr.always_explicit = 1;
151 sym->attr.always_explicit = 1;
156 for (f = proc->formal; f; f = f->next)
162 /* Alternate return placeholder. */
163 if (gfc_elemental (proc))
164 gfc_error ("Alternate return specifier in elemental subroutine "
165 "'%s' at %L is not allowed", proc->name,
167 if (proc->attr.function)
168 gfc_error ("Alternate return specifier in function "
169 "'%s' at %L is not allowed", proc->name,
174 if (sym->attr.if_source != IFSRC_UNKNOWN)
175 resolve_formal_arglist (sym);
177 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
179 if (gfc_pure (proc) && !gfc_pure (sym))
181 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
182 "also be PURE", sym->name, &sym->declared_at);
186 if (gfc_elemental (proc))
188 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
189 "procedure", &sym->declared_at);
193 if (sym->attr.function
194 && sym->ts.type == BT_UNKNOWN
195 && sym->attr.intrinsic)
197 gfc_intrinsic_sym *isym;
198 isym = gfc_find_function (sym->name);
199 if (isym == NULL || !isym->specific)
201 gfc_error ("Unable to find a specific INTRINSIC procedure "
202 "for the reference '%s' at %L", sym->name,
211 if (sym->ts.type == BT_UNKNOWN)
213 if (!sym->attr.function || sym->result == sym)
214 gfc_set_default_type (sym, 1, sym->ns);
217 gfc_resolve_array_spec (sym->as, 0);
219 /* We can't tell if an array with dimension (:) is assumed or deferred
220 shape until we know if it has the pointer or allocatable attributes.
222 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
223 && !(sym->attr.pointer || sym->attr.allocatable))
225 sym->as->type = AS_ASSUMED_SHAPE;
226 for (i = 0; i < sym->as->rank; i++)
227 sym->as->lower[i] = gfc_int_expr (1);
230 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
231 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
232 || sym->attr.optional)
234 proc->attr.always_explicit = 1;
236 proc->result->attr.always_explicit = 1;
239 /* If the flavor is unknown at this point, it has to be a variable.
240 A procedure specification would have already set the type. */
242 if (sym->attr.flavor == FL_UNKNOWN)
243 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
245 if (gfc_pure (proc) && !sym->attr.pointer
246 && sym->attr.flavor != FL_PROCEDURE)
248 if (proc->attr.function && sym->attr.intent != INTENT_IN)
249 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
250 "INTENT(IN)", sym->name, proc->name,
253 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
254 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
255 "have its INTENT specified", sym->name, proc->name,
259 if (gfc_elemental (proc))
263 gfc_error ("Argument '%s' of elemental procedure at %L must "
264 "be scalar", sym->name, &sym->declared_at);
268 if (sym->attr.pointer)
270 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
271 "have the POINTER attribute", sym->name,
276 if (sym->attr.flavor == FL_PROCEDURE)
278 gfc_error ("Dummy procedure '%s' not allowed in elemental "
279 "procedure '%s' at %L", sym->name, proc->name,
285 /* Each dummy shall be specified to be scalar. */
286 if (proc->attr.proc == PROC_ST_FUNCTION)
290 gfc_error ("Argument '%s' of statement function at %L must "
291 "be scalar", sym->name, &sym->declared_at);
295 if (sym->ts.type == BT_CHARACTER)
297 gfc_charlen *cl = sym->ts.u.cl;
298 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
300 gfc_error ("Character-valued argument '%s' of statement "
301 "function at %L must have constant length",
302 sym->name, &sym->declared_at);
312 /* Work function called when searching for symbols that have argument lists
313 associated with them. */
316 find_arglists (gfc_symbol *sym)
318 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
321 resolve_formal_arglist (sym);
325 /* Given a namespace, resolve all formal argument lists within the namespace.
329 resolve_formal_arglists (gfc_namespace *ns)
334 gfc_traverse_ns (ns, find_arglists);
339 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
343 /* If this namespace is not a function or an entry master function,
345 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
346 || sym->attr.entry_master)
349 /* Try to find out of what the return type is. */
350 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
352 t = gfc_set_default_type (sym->result, 0, ns);
354 if (t == FAILURE && !sym->result->attr.untyped)
356 if (sym->result == sym)
357 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
358 sym->name, &sym->declared_at);
359 else if (!sym->result->attr.proc_pointer)
360 gfc_error ("Result '%s' of contained function '%s' at %L has "
361 "no IMPLICIT type", sym->result->name, sym->name,
362 &sym->result->declared_at);
363 sym->result->attr.untyped = 1;
367 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
368 type, lists the only ways a character length value of * can be used:
369 dummy arguments of procedures, named constants, and function results
370 in external functions. Internal function results are not on that list;
371 ergo, not permitted. */
373 if (sym->result->ts.type == BT_CHARACTER)
375 gfc_charlen *cl = sym->result->ts.u.cl;
376 if (!cl || !cl->length)
377 gfc_error ("Character-valued internal function '%s' at %L must "
378 "not be assumed length", sym->name, &sym->declared_at);
383 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
384 introduce duplicates. */
387 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
389 gfc_formal_arglist *f, *new_arglist;
392 for (; new_args != NULL; new_args = new_args->next)
394 new_sym = new_args->sym;
395 /* See if this arg is already in the formal argument list. */
396 for (f = proc->formal; f; f = f->next)
398 if (new_sym == f->sym)
405 /* Add a new argument. Argument order is not important. */
406 new_arglist = gfc_get_formal_arglist ();
407 new_arglist->sym = new_sym;
408 new_arglist->next = proc->formal;
409 proc->formal = new_arglist;
414 /* Flag the arguments that are not present in all entries. */
417 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
419 gfc_formal_arglist *f, *head;
422 for (f = proc->formal; f; f = f->next)
427 for (new_args = head; new_args; new_args = new_args->next)
429 if (new_args->sym == f->sym)
436 f->sym->attr.not_always_present = 1;
441 /* Resolve alternate entry points. If a symbol has multiple entry points we
442 create a new master symbol for the main routine, and turn the existing
443 symbol into an entry point. */
446 resolve_entries (gfc_namespace *ns)
448 gfc_namespace *old_ns;
452 char name[GFC_MAX_SYMBOL_LEN + 1];
453 static int master_count = 0;
455 if (ns->proc_name == NULL)
458 /* No need to do anything if this procedure doesn't have alternate entry
463 /* We may already have resolved alternate entry points. */
464 if (ns->proc_name->attr.entry_master)
467 /* If this isn't a procedure something has gone horribly wrong. */
468 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
470 /* Remember the current namespace. */
471 old_ns = gfc_current_ns;
475 /* Add the main entry point to the list of entry points. */
476 el = gfc_get_entry_list ();
477 el->sym = ns->proc_name;
479 el->next = ns->entries;
481 ns->proc_name->attr.entry = 1;
483 /* If it is a module function, it needs to be in the right namespace
484 so that gfc_get_fake_result_decl can gather up the results. The
485 need for this arose in get_proc_name, where these beasts were
486 left in their own namespace, to keep prior references linked to
487 the entry declaration.*/
488 if (ns->proc_name->attr.function
489 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
492 /* Do the same for entries where the master is not a module
493 procedure. These are retained in the module namespace because
494 of the module procedure declaration. */
495 for (el = el->next; el; el = el->next)
496 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
497 && el->sym->attr.mod_proc)
501 /* Add an entry statement for it. */
508 /* Create a new symbol for the master function. */
509 /* Give the internal function a unique name (within this file).
510 Also include the function name so the user has some hope of figuring
511 out what is going on. */
512 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
513 master_count++, ns->proc_name->name);
514 gfc_get_ha_symbol (name, &proc);
515 gcc_assert (proc != NULL);
517 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
518 if (ns->proc_name->attr.subroutine)
519 gfc_add_subroutine (&proc->attr, proc->name, NULL);
523 gfc_typespec *ts, *fts;
524 gfc_array_spec *as, *fas;
525 gfc_add_function (&proc->attr, proc->name, NULL);
527 fas = ns->entries->sym->as;
528 fas = fas ? fas : ns->entries->sym->result->as;
529 fts = &ns->entries->sym->result->ts;
530 if (fts->type == BT_UNKNOWN)
531 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
532 for (el = ns->entries->next; el; el = el->next)
534 ts = &el->sym->result->ts;
536 as = as ? as : el->sym->result->as;
537 if (ts->type == BT_UNKNOWN)
538 ts = gfc_get_default_type (el->sym->result->name, NULL);
540 if (! gfc_compare_types (ts, fts)
541 || (el->sym->result->attr.dimension
542 != ns->entries->sym->result->attr.dimension)
543 || (el->sym->result->attr.pointer
544 != ns->entries->sym->result->attr.pointer))
546 else if (as && fas && ns->entries->sym->result != el->sym->result
547 && gfc_compare_array_spec (as, fas) == 0)
548 gfc_error ("Function %s at %L has entries with mismatched "
549 "array specifications", ns->entries->sym->name,
550 &ns->entries->sym->declared_at);
551 /* The characteristics need to match and thus both need to have
552 the same string length, i.e. both len=*, or both len=4.
553 Having both len=<variable> is also possible, but difficult to
554 check at compile time. */
555 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
556 && (((ts->u.cl->length && !fts->u.cl->length)
557 ||(!ts->u.cl->length && fts->u.cl->length))
559 && ts->u.cl->length->expr_type
560 != fts->u.cl->length->expr_type)
562 && ts->u.cl->length->expr_type == EXPR_CONSTANT
563 && mpz_cmp (ts->u.cl->length->value.integer,
564 fts->u.cl->length->value.integer) != 0)))
565 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
566 "entries returning variables of different "
567 "string lengths", ns->entries->sym->name,
568 &ns->entries->sym->declared_at);
573 sym = ns->entries->sym->result;
574 /* All result types the same. */
576 if (sym->attr.dimension)
577 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
578 if (sym->attr.pointer)
579 gfc_add_pointer (&proc->attr, NULL);
583 /* Otherwise the result will be passed through a union by
585 proc->attr.mixed_entry_master = 1;
586 for (el = ns->entries; el; el = el->next)
588 sym = el->sym->result;
589 if (sym->attr.dimension)
591 if (el == ns->entries)
592 gfc_error ("FUNCTION result %s can't be an array in "
593 "FUNCTION %s at %L", sym->name,
594 ns->entries->sym->name, &sym->declared_at);
596 gfc_error ("ENTRY result %s can't be an array in "
597 "FUNCTION %s at %L", sym->name,
598 ns->entries->sym->name, &sym->declared_at);
600 else if (sym->attr.pointer)
602 if (el == ns->entries)
603 gfc_error ("FUNCTION result %s can't be a POINTER in "
604 "FUNCTION %s at %L", sym->name,
605 ns->entries->sym->name, &sym->declared_at);
607 gfc_error ("ENTRY result %s can't be a POINTER in "
608 "FUNCTION %s at %L", sym->name,
609 ns->entries->sym->name, &sym->declared_at);
614 if (ts->type == BT_UNKNOWN)
615 ts = gfc_get_default_type (sym->name, NULL);
619 if (ts->kind == gfc_default_integer_kind)
623 if (ts->kind == gfc_default_real_kind
624 || ts->kind == gfc_default_double_kind)
628 if (ts->kind == gfc_default_complex_kind)
632 if (ts->kind == gfc_default_logical_kind)
636 /* We will issue error elsewhere. */
644 if (el == ns->entries)
645 gfc_error ("FUNCTION result %s can't be of type %s "
646 "in FUNCTION %s at %L", sym->name,
647 gfc_typename (ts), ns->entries->sym->name,
650 gfc_error ("ENTRY result %s can't be of type %s "
651 "in FUNCTION %s at %L", sym->name,
652 gfc_typename (ts), ns->entries->sym->name,
659 proc->attr.access = ACCESS_PRIVATE;
660 proc->attr.entry_master = 1;
662 /* Merge all the entry point arguments. */
663 for (el = ns->entries; el; el = el->next)
664 merge_argument_lists (proc, el->sym->formal);
666 /* Check the master formal arguments for any that are not
667 present in all entry points. */
668 for (el = ns->entries; el; el = el->next)
669 check_argument_lists (proc, el->sym->formal);
671 /* Use the master function for the function body. */
672 ns->proc_name = proc;
674 /* Finalize the new symbols. */
675 gfc_commit_symbols ();
677 /* Restore the original namespace. */
678 gfc_current_ns = old_ns;
683 has_default_initializer (gfc_symbol *der)
687 gcc_assert (der->attr.flavor == FL_DERIVED);
688 for (c = der->components; c; c = c->next)
689 if ((c->ts.type != BT_DERIVED && c->initializer)
690 || (c->ts.type == BT_DERIVED
691 && (!c->attr.pointer && has_default_initializer (c->ts.u.derived))))
697 /* Resolve common variables. */
699 resolve_common_vars (gfc_symbol *sym, bool named_common)
701 gfc_symbol *csym = sym;
703 for (; csym; csym = csym->common_next)
705 if (csym->value || csym->attr.data)
707 if (!csym->ns->is_block_data)
708 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
709 "but only in BLOCK DATA initialization is "
710 "allowed", csym->name, &csym->declared_at);
711 else if (!named_common)
712 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
713 "in a blank COMMON but initialization is only "
714 "allowed in named common blocks", csym->name,
718 if (csym->ts.type != BT_DERIVED)
721 if (!(csym->ts.u.derived->attr.sequence
722 || csym->ts.u.derived->attr.is_bind_c))
723 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
724 "has neither the SEQUENCE nor the BIND(C) "
725 "attribute", csym->name, &csym->declared_at);
726 if (csym->ts.u.derived->attr.alloc_comp)
727 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
728 "has an ultimate component that is "
729 "allocatable", csym->name, &csym->declared_at);
730 if (has_default_initializer (csym->ts.u.derived))
731 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
732 "may not have default initializer", csym->name,
735 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
736 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
740 /* Resolve common blocks. */
742 resolve_common_blocks (gfc_symtree *common_root)
746 if (common_root == NULL)
749 if (common_root->left)
750 resolve_common_blocks (common_root->left);
751 if (common_root->right)
752 resolve_common_blocks (common_root->right);
754 resolve_common_vars (common_root->n.common->head, true);
756 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
760 if (sym->attr.flavor == FL_PARAMETER)
761 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
762 sym->name, &common_root->n.common->where, &sym->declared_at);
764 if (sym->attr.intrinsic)
765 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
766 sym->name, &common_root->n.common->where);
767 else if (sym->attr.result
768 ||(sym->attr.function && gfc_current_ns->proc_name == sym))
769 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
770 "that is also a function result", sym->name,
771 &common_root->n.common->where);
772 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
773 && sym->attr.proc != PROC_ST_FUNCTION)
774 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
775 "that is also a global procedure", sym->name,
776 &common_root->n.common->where);
780 /* Resolve contained function types. Because contained functions can call one
781 another, they have to be worked out before any of the contained procedures
784 The good news is that if a function doesn't already have a type, the only
785 way it can get one is through an IMPLICIT type or a RESULT variable, because
786 by definition contained functions are contained namespace they're contained
787 in, not in a sibling or parent namespace. */
790 resolve_contained_functions (gfc_namespace *ns)
792 gfc_namespace *child;
795 resolve_formal_arglists (ns);
797 for (child = ns->contained; child; child = child->sibling)
799 /* Resolve alternate entry points first. */
800 resolve_entries (child);
802 /* Then check function return types. */
803 resolve_contained_fntype (child->proc_name, child);
804 for (el = child->entries; el; el = el->next)
805 resolve_contained_fntype (el->sym, child);
810 /* Resolve all of the elements of a structure constructor and make sure that
811 the types are correct. */
814 resolve_structure_cons (gfc_expr *expr)
816 gfc_constructor *cons;
822 cons = expr->value.constructor;
823 /* A constructor may have references if it is the result of substituting a
824 parameter variable. In this case we just pull out the component we
827 comp = expr->ref->u.c.sym->components;
829 comp = expr->ts.u.derived->components;
831 /* See if the user is trying to invoke a structure constructor for one of
832 the iso_c_binding derived types. */
833 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
834 && expr->ts.u.derived->ts.is_iso_c && cons && cons->expr != NULL)
836 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
837 expr->ts.u.derived->name, &(expr->where));
841 for (; comp; comp = comp->next, cons = cons->next)
848 if (gfc_resolve_expr (cons->expr) == FAILURE)
854 rank = comp->as ? comp->as->rank : 0;
855 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
856 && (comp->attr.allocatable || cons->expr->rank))
858 gfc_error ("The rank of the element in the derived type "
859 "constructor at %L does not match that of the "
860 "component (%d/%d)", &cons->expr->where,
861 cons->expr->rank, rank);
865 /* If we don't have the right type, try to convert it. */
867 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
870 if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
871 gfc_error ("The element in the derived type constructor at %L, "
872 "for pointer component '%s', is %s but should be %s",
873 &cons->expr->where, comp->name,
874 gfc_basic_typename (cons->expr->ts.type),
875 gfc_basic_typename (comp->ts.type));
877 t = gfc_convert_type (cons->expr, &comp->ts, 1);
880 if (cons->expr->expr_type == EXPR_NULL
881 && !(comp->attr.pointer || comp->attr.allocatable
882 || comp->attr.proc_pointer))
885 gfc_error ("The NULL in the derived type constructor at %L is "
886 "being applied to component '%s', which is neither "
887 "a POINTER nor ALLOCATABLE", &cons->expr->where,
891 if (!comp->attr.pointer || cons->expr->expr_type == EXPR_NULL)
894 a = gfc_expr_attr (cons->expr);
896 if (!a.pointer && !a.target)
899 gfc_error ("The element in the derived type constructor at %L, "
900 "for pointer component '%s' should be a POINTER or "
901 "a TARGET", &cons->expr->where, comp->name);
909 /****************** Expression name resolution ******************/
911 /* Returns 0 if a symbol was not declared with a type or
912 attribute declaration statement, nonzero otherwise. */
915 was_declared (gfc_symbol *sym)
921 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
924 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
925 || a.optional || a.pointer || a.save || a.target || a.volatile_
926 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
933 /* Determine if a symbol is generic or not. */
936 generic_sym (gfc_symbol *sym)
940 if (sym->attr.generic ||
941 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
944 if (was_declared (sym) || sym->ns->parent == NULL)
947 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
954 return generic_sym (s);
961 /* Determine if a symbol is specific or not. */
964 specific_sym (gfc_symbol *sym)
968 if (sym->attr.if_source == IFSRC_IFBODY
969 || sym->attr.proc == PROC_MODULE
970 || sym->attr.proc == PROC_INTERNAL
971 || sym->attr.proc == PROC_ST_FUNCTION
972 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
973 || sym->attr.external)
976 if (was_declared (sym) || sym->ns->parent == NULL)
979 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
981 return (s == NULL) ? 0 : specific_sym (s);
985 /* Figure out if the procedure is specific, generic or unknown. */
988 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
992 procedure_kind (gfc_symbol *sym)
994 if (generic_sym (sym))
995 return PTYPE_GENERIC;
997 if (specific_sym (sym))
998 return PTYPE_SPECIFIC;
1000 return PTYPE_UNKNOWN;
1003 /* Check references to assumed size arrays. The flag need_full_assumed_size
1004 is nonzero when matching actual arguments. */
1006 static int need_full_assumed_size = 0;
1009 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1011 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1014 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1015 What should it be? */
1016 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1017 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1018 && (e->ref->u.ar.type == AR_FULL))
1020 gfc_error ("The upper bound in the last dimension must "
1021 "appear in the reference to the assumed size "
1022 "array '%s' at %L", sym->name, &e->where);
1029 /* Look for bad assumed size array references in argument expressions
1030 of elemental and array valued intrinsic procedures. Since this is
1031 called from procedure resolution functions, it only recurses at
1035 resolve_assumed_size_actual (gfc_expr *e)
1040 switch (e->expr_type)
1043 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1048 if (resolve_assumed_size_actual (e->value.op.op1)
1049 || resolve_assumed_size_actual (e->value.op.op2))
1060 /* Check a generic procedure, passed as an actual argument, to see if
1061 there is a matching specific name. If none, it is an error, and if
1062 more than one, the reference is ambiguous. */
1064 count_specific_procs (gfc_expr *e)
1071 sym = e->symtree->n.sym;
1073 for (p = sym->generic; p; p = p->next)
1074 if (strcmp (sym->name, p->sym->name) == 0)
1076 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1082 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1086 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1087 "argument at %L", sym->name, &e->where);
1093 /* See if a call to sym could possibly be a not allowed RECURSION because of
1094 a missing RECURIVE declaration. This means that either sym is the current
1095 context itself, or sym is the parent of a contained procedure calling its
1096 non-RECURSIVE containing procedure.
1097 This also works if sym is an ENTRY. */
1100 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1102 gfc_symbol* proc_sym;
1103 gfc_symbol* context_proc;
1105 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1107 /* If we've got an ENTRY, find real procedure. */
1108 if (sym->attr.entry && sym->ns->entries)
1109 proc_sym = sym->ns->entries->sym;
1113 /* If sym is RECURSIVE, all is well of course. */
1114 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1117 /* Find the context procdure's "real" symbol if it has entries. */
1118 context_proc = (context->entries ? context->entries->sym
1119 : context->proc_name);
1123 /* A call from sym's body to itself is recursion, of course. */
1124 if (context_proc == proc_sym)
1127 /* The same is true if context is a contained procedure and sym the
1129 if (context_proc->attr.contained)
1131 gfc_symbol* parent_proc;
1133 gcc_assert (context->parent);
1134 parent_proc = (context->parent->entries ? context->parent->entries->sym
1135 : context->parent->proc_name);
1137 if (parent_proc == proc_sym)
1145 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1146 its typespec and formal argument list. */
1149 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1151 gfc_intrinsic_sym* isym;
1157 /* We already know this one is an intrinsic, so we don't call
1158 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1159 gfc_find_subroutine directly to check whether it is a function or
1162 if ((isym = gfc_find_function (sym->name)))
1164 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1165 && !sym->attr.implicit_type)
1166 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1167 " ignored", sym->name, &sym->declared_at);
1169 if (!sym->attr.function &&
1170 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1175 else if ((isym = gfc_find_subroutine (sym->name)))
1177 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1179 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1180 " specifier", sym->name, &sym->declared_at);
1184 if (!sym->attr.subroutine &&
1185 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1190 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1195 gfc_copy_formal_args_intr (sym, isym);
1197 /* Check it is actually available in the standard settings. */
1198 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1201 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1202 " available in the current standard settings but %s. Use"
1203 " an appropriate -std=* option or enable -fall-intrinsics"
1204 " in order to use it.",
1205 sym->name, &sym->declared_at, symstd);
1213 /* Resolve a procedure expression, like passing it to a called procedure or as
1214 RHS for a procedure pointer assignment. */
1217 resolve_procedure_expression (gfc_expr* expr)
1221 if (expr->expr_type != EXPR_VARIABLE)
1223 gcc_assert (expr->symtree);
1225 sym = expr->symtree->n.sym;
1227 if (sym->attr.intrinsic)
1228 resolve_intrinsic (sym, &expr->where);
1230 if (sym->attr.flavor != FL_PROCEDURE
1231 || (sym->attr.function && sym->result == sym))
1234 /* A non-RECURSIVE procedure that is used as procedure expression within its
1235 own body is in danger of being called recursively. */
1236 if (is_illegal_recursion (sym, gfc_current_ns))
1237 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1238 " itself recursively. Declare it RECURSIVE or use"
1239 " -frecursive", sym->name, &expr->where);
1245 /* Resolve an actual argument list. Most of the time, this is just
1246 resolving the expressions in the list.
1247 The exception is that we sometimes have to decide whether arguments
1248 that look like procedure arguments are really simple variable
1252 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1253 bool no_formal_args)
1256 gfc_symtree *parent_st;
1258 int save_need_full_assumed_size;
1259 gfc_component *comp;
1261 for (; arg; arg = arg->next)
1266 /* Check the label is a valid branching target. */
1269 if (arg->label->defined == ST_LABEL_UNKNOWN)
1271 gfc_error ("Label %d referenced at %L is never defined",
1272 arg->label->value, &arg->label->where);
1279 if (gfc_is_proc_ptr_comp (e, &comp))
1282 if (e->expr_type == EXPR_PPC)
1284 if (comp->as != NULL)
1285 e->rank = comp->as->rank;
1286 e->expr_type = EXPR_FUNCTION;
1291 if (e->expr_type == EXPR_VARIABLE
1292 && e->symtree->n.sym->attr.generic
1294 && count_specific_procs (e) != 1)
1297 if (e->ts.type != BT_PROCEDURE)
1299 save_need_full_assumed_size = need_full_assumed_size;
1300 if (e->expr_type != EXPR_VARIABLE)
1301 need_full_assumed_size = 0;
1302 if (gfc_resolve_expr (e) != SUCCESS)
1304 need_full_assumed_size = save_need_full_assumed_size;
1308 /* See if the expression node should really be a variable reference. */
1310 sym = e->symtree->n.sym;
1312 if (sym->attr.flavor == FL_PROCEDURE
1313 || sym->attr.intrinsic
1314 || sym->attr.external)
1318 /* If a procedure is not already determined to be something else
1319 check if it is intrinsic. */
1320 if (!sym->attr.intrinsic
1321 && !(sym->attr.external || sym->attr.use_assoc
1322 || sym->attr.if_source == IFSRC_IFBODY)
1323 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1324 sym->attr.intrinsic = 1;
1326 if (sym->attr.proc == PROC_ST_FUNCTION)
1328 gfc_error ("Statement function '%s' at %L is not allowed as an "
1329 "actual argument", sym->name, &e->where);
1332 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1333 sym->attr.subroutine);
1334 if (sym->attr.intrinsic && actual_ok == 0)
1336 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1337 "actual argument", sym->name, &e->where);
1340 if (sym->attr.contained && !sym->attr.use_assoc
1341 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1343 gfc_error ("Internal procedure '%s' is not allowed as an "
1344 "actual argument at %L", sym->name, &e->where);
1347 if (sym->attr.elemental && !sym->attr.intrinsic)
1349 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1350 "allowed as an actual argument at %L", sym->name,
1354 /* Check if a generic interface has a specific procedure
1355 with the same name before emitting an error. */
1356 if (sym->attr.generic && count_specific_procs (e) != 1)
1359 /* Just in case a specific was found for the expression. */
1360 sym = e->symtree->n.sym;
1362 /* If the symbol is the function that names the current (or
1363 parent) scope, then we really have a variable reference. */
1365 if (sym->attr.function && sym->result == sym
1366 && (sym->ns->proc_name == sym
1367 || (sym->ns->parent != NULL
1368 && sym->ns->parent->proc_name == sym)))
1371 /* If all else fails, see if we have a specific intrinsic. */
1372 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1374 gfc_intrinsic_sym *isym;
1376 isym = gfc_find_function (sym->name);
1377 if (isym == NULL || !isym->specific)
1379 gfc_error ("Unable to find a specific INTRINSIC procedure "
1380 "for the reference '%s' at %L", sym->name,
1385 sym->attr.intrinsic = 1;
1386 sym->attr.function = 1;
1389 if (gfc_resolve_expr (e) == FAILURE)
1394 /* See if the name is a module procedure in a parent unit. */
1396 if (was_declared (sym) || sym->ns->parent == NULL)
1399 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1401 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1405 if (parent_st == NULL)
1408 sym = parent_st->n.sym;
1409 e->symtree = parent_st; /* Point to the right thing. */
1411 if (sym->attr.flavor == FL_PROCEDURE
1412 || sym->attr.intrinsic
1413 || sym->attr.external)
1415 if (gfc_resolve_expr (e) == FAILURE)
1421 e->expr_type = EXPR_VARIABLE;
1423 if (sym->as != NULL)
1425 e->rank = sym->as->rank;
1426 e->ref = gfc_get_ref ();
1427 e->ref->type = REF_ARRAY;
1428 e->ref->u.ar.type = AR_FULL;
1429 e->ref->u.ar.as = sym->as;
1432 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1433 primary.c (match_actual_arg). If above code determines that it
1434 is a variable instead, it needs to be resolved as it was not
1435 done at the beginning of this function. */
1436 save_need_full_assumed_size = need_full_assumed_size;
1437 if (e->expr_type != EXPR_VARIABLE)
1438 need_full_assumed_size = 0;
1439 if (gfc_resolve_expr (e) != SUCCESS)
1441 need_full_assumed_size = save_need_full_assumed_size;
1444 /* Check argument list functions %VAL, %LOC and %REF. There is
1445 nothing to do for %REF. */
1446 if (arg->name && arg->name[0] == '%')
1448 if (strncmp ("%VAL", arg->name, 4) == 0)
1450 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1452 gfc_error ("By-value argument at %L is not of numeric "
1459 gfc_error ("By-value argument at %L cannot be an array or "
1460 "an array section", &e->where);
1464 /* Intrinsics are still PROC_UNKNOWN here. However,
1465 since same file external procedures are not resolvable
1466 in gfortran, it is a good deal easier to leave them to
1468 if (ptype != PROC_UNKNOWN
1469 && ptype != PROC_DUMMY
1470 && ptype != PROC_EXTERNAL
1471 && ptype != PROC_MODULE)
1473 gfc_error ("By-value argument at %L is not allowed "
1474 "in this context", &e->where);
1479 /* Statement functions have already been excluded above. */
1480 else if (strncmp ("%LOC", arg->name, 4) == 0
1481 && e->ts.type == BT_PROCEDURE)
1483 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1485 gfc_error ("Passing internal procedure at %L by location "
1486 "not allowed", &e->where);
1497 /* Do the checks of the actual argument list that are specific to elemental
1498 procedures. If called with c == NULL, we have a function, otherwise if
1499 expr == NULL, we have a subroutine. */
1502 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1504 gfc_actual_arglist *arg0;
1505 gfc_actual_arglist *arg;
1506 gfc_symbol *esym = NULL;
1507 gfc_intrinsic_sym *isym = NULL;
1509 gfc_intrinsic_arg *iformal = NULL;
1510 gfc_formal_arglist *eformal = NULL;
1511 bool formal_optional = false;
1512 bool set_by_optional = false;
1516 /* Is this an elemental procedure? */
1517 if (expr && expr->value.function.actual != NULL)
1519 if (expr->value.function.esym != NULL
1520 && expr->value.function.esym->attr.elemental)
1522 arg0 = expr->value.function.actual;
1523 esym = expr->value.function.esym;
1525 else if (expr->value.function.isym != NULL
1526 && expr->value.function.isym->elemental)
1528 arg0 = expr->value.function.actual;
1529 isym = expr->value.function.isym;
1534 else if (c && c->ext.actual != NULL)
1536 arg0 = c->ext.actual;
1538 if (c->resolved_sym)
1539 esym = c->resolved_sym;
1541 esym = c->symtree->n.sym;
1544 if (!esym->attr.elemental)
1550 /* The rank of an elemental is the rank of its array argument(s). */
1551 for (arg = arg0; arg; arg = arg->next)
1553 if (arg->expr != NULL && arg->expr->rank > 0)
1555 rank = arg->expr->rank;
1556 if (arg->expr->expr_type == EXPR_VARIABLE
1557 && arg->expr->symtree->n.sym->attr.optional)
1558 set_by_optional = true;
1560 /* Function specific; set the result rank and shape. */
1564 if (!expr->shape && arg->expr->shape)
1566 expr->shape = gfc_get_shape (rank);
1567 for (i = 0; i < rank; i++)
1568 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1575 /* If it is an array, it shall not be supplied as an actual argument
1576 to an elemental procedure unless an array of the same rank is supplied
1577 as an actual argument corresponding to a nonoptional dummy argument of
1578 that elemental procedure(12.4.1.5). */
1579 formal_optional = false;
1581 iformal = isym->formal;
1583 eformal = esym->formal;
1585 for (arg = arg0; arg; arg = arg->next)
1589 if (eformal->sym && eformal->sym->attr.optional)
1590 formal_optional = true;
1591 eformal = eformal->next;
1593 else if (isym && iformal)
1595 if (iformal->optional)
1596 formal_optional = true;
1597 iformal = iformal->next;
1600 formal_optional = true;
1602 if (pedantic && arg->expr != NULL
1603 && arg->expr->expr_type == EXPR_VARIABLE
1604 && arg->expr->symtree->n.sym->attr.optional
1607 && (set_by_optional || arg->expr->rank != rank)
1608 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1610 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1611 "MISSING, it cannot be the actual argument of an "
1612 "ELEMENTAL procedure unless there is a non-optional "
1613 "argument with the same rank (12.4.1.5)",
1614 arg->expr->symtree->n.sym->name, &arg->expr->where);
1619 for (arg = arg0; arg; arg = arg->next)
1621 if (arg->expr == NULL || arg->expr->rank == 0)
1624 /* Being elemental, the last upper bound of an assumed size array
1625 argument must be present. */
1626 if (resolve_assumed_size_actual (arg->expr))
1629 /* Elemental procedure's array actual arguments must conform. */
1632 if (gfc_check_conformance (arg->expr, e,
1633 "elemental procedure") == FAILURE)
1640 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1641 is an array, the intent inout/out variable needs to be also an array. */
1642 if (rank > 0 && esym && expr == NULL)
1643 for (eformal = esym->formal, arg = arg0; arg && eformal;
1644 arg = arg->next, eformal = eformal->next)
1645 if ((eformal->sym->attr.intent == INTENT_OUT
1646 || eformal->sym->attr.intent == INTENT_INOUT)
1647 && arg->expr && arg->expr->rank == 0)
1649 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1650 "ELEMENTAL subroutine '%s' is a scalar, but another "
1651 "actual argument is an array", &arg->expr->where,
1652 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1653 : "INOUT", eformal->sym->name, esym->name);
1660 /* Go through each actual argument in ACTUAL and see if it can be
1661 implemented as an inlined, non-copying intrinsic. FNSYM is the
1662 function being called, or NULL if not known. */
1665 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1667 gfc_actual_arglist *ap;
1670 for (ap = actual; ap; ap = ap->next)
1672 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1673 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1675 ap->expr->inline_noncopying_intrinsic = 1;
1679 /* This function does the checking of references to global procedures
1680 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1681 77 and 95 standards. It checks for a gsymbol for the name, making
1682 one if it does not already exist. If it already exists, then the
1683 reference being resolved must correspond to the type of gsymbol.
1684 Otherwise, the new symbol is equipped with the attributes of the
1685 reference. The corresponding code that is called in creating
1686 global entities is parse.c.
1688 In addition, for all but -std=legacy, the gsymbols are used to
1689 check the interfaces of external procedures from the same file.
1690 The namespace of the gsymbol is resolved and then, once this is
1691 done the interface is checked. */
1695 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1697 if (!gsym_ns->proc_name->attr.recursive)
1700 if (sym->ns == gsym_ns)
1703 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1710 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1712 if (gsym_ns->entries)
1714 gfc_entry_list *entry = gsym_ns->entries;
1716 for (; entry; entry = entry->next)
1718 if (strcmp (sym->name, entry->sym->name) == 0)
1720 if (strcmp (gsym_ns->proc_name->name,
1721 sym->ns->proc_name->name) == 0)
1725 && strcmp (gsym_ns->proc_name->name,
1726 sym->ns->parent->proc_name->name) == 0)
1735 resolve_global_procedure (gfc_symbol *sym, locus *where,
1736 gfc_actual_arglist **actual, int sub)
1740 enum gfc_symbol_type type;
1742 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1744 gsym = gfc_get_gsymbol (sym->name);
1746 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1747 gfc_global_used (gsym, where);
1749 if (gfc_option.flag_whole_file
1750 && sym->attr.if_source == IFSRC_UNKNOWN
1751 && gsym->type != GSYM_UNKNOWN
1753 && gsym->ns->resolved != -1
1754 && gsym->ns->proc_name
1755 && not_in_recursive (sym, gsym->ns)
1756 && not_entry_self_reference (sym, gsym->ns))
1758 /* Make sure that translation for the gsymbol occurs before
1759 the procedure currently being resolved. */
1760 ns = gsym->ns->resolved ? NULL : gfc_global_ns_list;
1761 for (; ns && ns != gsym->ns; ns = ns->sibling)
1763 if (ns->sibling == gsym->ns)
1765 ns->sibling = gsym->ns->sibling;
1766 gsym->ns->sibling = gfc_global_ns_list;
1767 gfc_global_ns_list = gsym->ns;
1772 if (!gsym->ns->resolved)
1774 gfc_dt_list *old_dt_list;
1776 /* Stash away derived types so that the backend_decls do not
1778 old_dt_list = gfc_derived_types;
1779 gfc_derived_types = NULL;
1781 gfc_resolve (gsym->ns);
1783 /* Store the new derived types with the global namespace. */
1784 if (gfc_derived_types)
1785 gsym->ns->derived_types = gfc_derived_types;
1787 /* Restore the derived types of this namespace. */
1788 gfc_derived_types = old_dt_list;
1791 if (gsym->ns->proc_name->attr.function
1792 && gsym->ns->proc_name->as
1793 && gsym->ns->proc_name->as->rank
1794 && (!sym->as || sym->as->rank != gsym->ns->proc_name->as->rank))
1795 gfc_error ("The reference to function '%s' at %L either needs an "
1796 "explicit INTERFACE or the rank is incorrect", sym->name,
1799 if (gfc_option.flag_whole_file == 1
1800 || ((gfc_option.warn_std & GFC_STD_LEGACY)
1802 !(gfc_option.warn_std & GFC_STD_GNU)))
1803 gfc_errors_to_warnings (1);
1805 gfc_procedure_use (gsym->ns->proc_name, actual, where);
1807 gfc_errors_to_warnings (0);
1810 if (gsym->type == GSYM_UNKNOWN)
1813 gsym->where = *where;
1820 /************* Function resolution *************/
1822 /* Resolve a function call known to be generic.
1823 Section 14.1.2.4.1. */
1826 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1830 if (sym->attr.generic)
1832 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1835 expr->value.function.name = s->name;
1836 expr->value.function.esym = s;
1838 if (s->ts.type != BT_UNKNOWN)
1840 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1841 expr->ts = s->result->ts;
1844 expr->rank = s->as->rank;
1845 else if (s->result != NULL && s->result->as != NULL)
1846 expr->rank = s->result->as->rank;
1848 gfc_set_sym_referenced (expr->value.function.esym);
1853 /* TODO: Need to search for elemental references in generic
1857 if (sym->attr.intrinsic)
1858 return gfc_intrinsic_func_interface (expr, 0);
1865 resolve_generic_f (gfc_expr *expr)
1870 sym = expr->symtree->n.sym;
1874 m = resolve_generic_f0 (expr, sym);
1877 else if (m == MATCH_ERROR)
1881 if (sym->ns->parent == NULL)
1883 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1887 if (!generic_sym (sym))
1891 /* Last ditch attempt. See if the reference is to an intrinsic
1892 that possesses a matching interface. 14.1.2.4 */
1893 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
1895 gfc_error ("There is no specific function for the generic '%s' at %L",
1896 expr->symtree->n.sym->name, &expr->where);
1900 m = gfc_intrinsic_func_interface (expr, 0);
1904 gfc_error ("Generic function '%s' at %L is not consistent with a "
1905 "specific intrinsic interface", expr->symtree->n.sym->name,
1912 /* Resolve a function call known to be specific. */
1915 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
1919 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1921 if (sym->attr.dummy)
1923 sym->attr.proc = PROC_DUMMY;
1927 sym->attr.proc = PROC_EXTERNAL;
1931 if (sym->attr.proc == PROC_MODULE
1932 || sym->attr.proc == PROC_ST_FUNCTION
1933 || sym->attr.proc == PROC_INTERNAL)
1936 if (sym->attr.intrinsic)
1938 m = gfc_intrinsic_func_interface (expr, 1);
1942 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
1943 "with an intrinsic", sym->name, &expr->where);
1951 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1954 expr->ts = sym->result->ts;
1957 expr->value.function.name = sym->name;
1958 expr->value.function.esym = sym;
1959 if (sym->as != NULL)
1960 expr->rank = sym->as->rank;
1967 resolve_specific_f (gfc_expr *expr)
1972 sym = expr->symtree->n.sym;
1976 m = resolve_specific_f0 (sym, expr);
1979 if (m == MATCH_ERROR)
1982 if (sym->ns->parent == NULL)
1985 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1991 gfc_error ("Unable to resolve the specific function '%s' at %L",
1992 expr->symtree->n.sym->name, &expr->where);
1998 /* Resolve a procedure call not known to be generic nor specific. */
2001 resolve_unknown_f (gfc_expr *expr)
2006 sym = expr->symtree->n.sym;
2008 if (sym->attr.dummy)
2010 sym->attr.proc = PROC_DUMMY;
2011 expr->value.function.name = sym->name;
2015 /* See if we have an intrinsic function reference. */
2017 if (gfc_is_intrinsic (sym, 0, expr->where))
2019 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2024 /* The reference is to an external name. */
2026 sym->attr.proc = PROC_EXTERNAL;
2027 expr->value.function.name = sym->name;
2028 expr->value.function.esym = expr->symtree->n.sym;
2030 if (sym->as != NULL)
2031 expr->rank = sym->as->rank;
2033 /* Type of the expression is either the type of the symbol or the
2034 default type of the symbol. */
2037 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2039 if (sym->ts.type != BT_UNKNOWN)
2043 ts = gfc_get_default_type (sym->name, sym->ns);
2045 if (ts->type == BT_UNKNOWN)
2047 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2048 sym->name, &expr->where);
2059 /* Return true, if the symbol is an external procedure. */
2061 is_external_proc (gfc_symbol *sym)
2063 if (!sym->attr.dummy && !sym->attr.contained
2064 && !(sym->attr.intrinsic
2065 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2066 && sym->attr.proc != PROC_ST_FUNCTION
2067 && !sym->attr.use_assoc
2075 /* Figure out if a function reference is pure or not. Also set the name
2076 of the function for a potential error message. Return nonzero if the
2077 function is PURE, zero if not. */
2079 pure_stmt_function (gfc_expr *, gfc_symbol *);
2082 pure_function (gfc_expr *e, const char **name)
2088 if (e->symtree != NULL
2089 && e->symtree->n.sym != NULL
2090 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2091 return pure_stmt_function (e, e->symtree->n.sym);
2093 if (e->value.function.esym)
2095 pure = gfc_pure (e->value.function.esym);
2096 *name = e->value.function.esym->name;
2098 else if (e->value.function.isym)
2100 pure = e->value.function.isym->pure
2101 || e->value.function.isym->elemental;
2102 *name = e->value.function.isym->name;
2106 /* Implicit functions are not pure. */
2108 *name = e->value.function.name;
2116 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2117 int *f ATTRIBUTE_UNUSED)
2121 /* Don't bother recursing into other statement functions
2122 since they will be checked individually for purity. */
2123 if (e->expr_type != EXPR_FUNCTION
2125 || e->symtree->n.sym == sym
2126 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2129 return pure_function (e, &name) ? false : true;
2134 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2136 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2141 is_scalar_expr_ptr (gfc_expr *expr)
2143 gfc_try retval = SUCCESS;
2148 /* See if we have a gfc_ref, which means we have a substring, array
2149 reference, or a component. */
2150 if (expr->ref != NULL)
2153 while (ref->next != NULL)
2159 if (ref->u.ss.length != NULL
2160 && ref->u.ss.length->length != NULL
2162 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2164 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2166 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2167 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2168 if (end - start + 1 != 1)
2175 if (ref->u.ar.type == AR_ELEMENT)
2177 else if (ref->u.ar.type == AR_FULL)
2179 /* The user can give a full array if the array is of size 1. */
2180 if (ref->u.ar.as != NULL
2181 && ref->u.ar.as->rank == 1
2182 && ref->u.ar.as->type == AS_EXPLICIT
2183 && ref->u.ar.as->lower[0] != NULL
2184 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2185 && ref->u.ar.as->upper[0] != NULL
2186 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2188 /* If we have a character string, we need to check if
2189 its length is one. */
2190 if (expr->ts.type == BT_CHARACTER)
2192 if (expr->ts.u.cl == NULL
2193 || expr->ts.u.cl->length == NULL
2194 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2200 /* We have constant lower and upper bounds. If the
2201 difference between is 1, it can be considered a
2203 start = (int) mpz_get_si
2204 (ref->u.ar.as->lower[0]->value.integer);
2205 end = (int) mpz_get_si
2206 (ref->u.ar.as->upper[0]->value.integer);
2207 if (end - start + 1 != 1)
2222 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2224 /* Character string. Make sure it's of length 1. */
2225 if (expr->ts.u.cl == NULL
2226 || expr->ts.u.cl->length == NULL
2227 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2230 else if (expr->rank != 0)
2237 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2238 and, in the case of c_associated, set the binding label based on
2242 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2243 gfc_symbol **new_sym)
2245 char name[GFC_MAX_SYMBOL_LEN + 1];
2246 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2247 int optional_arg = 0, is_pointer = 0;
2248 gfc_try retval = SUCCESS;
2249 gfc_symbol *args_sym;
2250 gfc_typespec *arg_ts;
2252 if (args->expr->expr_type == EXPR_CONSTANT
2253 || args->expr->expr_type == EXPR_OP
2254 || args->expr->expr_type == EXPR_NULL)
2256 gfc_error ("Argument to '%s' at %L is not a variable",
2257 sym->name, &(args->expr->where));
2261 args_sym = args->expr->symtree->n.sym;
2263 /* The typespec for the actual arg should be that stored in the expr
2264 and not necessarily that of the expr symbol (args_sym), because
2265 the actual expression could be a part-ref of the expr symbol. */
2266 arg_ts = &(args->expr->ts);
2268 is_pointer = gfc_is_data_pointer (args->expr);
2270 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2272 /* If the user gave two args then they are providing something for
2273 the optional arg (the second cptr). Therefore, set the name and
2274 binding label to the c_associated for two cptrs. Otherwise,
2275 set c_associated to expect one cptr. */
2279 sprintf (name, "%s_2", sym->name);
2280 sprintf (binding_label, "%s_2", sym->binding_label);
2286 sprintf (name, "%s_1", sym->name);
2287 sprintf (binding_label, "%s_1", sym->binding_label);
2291 /* Get a new symbol for the version of c_associated that
2293 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2295 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2296 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2298 sprintf (name, "%s", sym->name);
2299 sprintf (binding_label, "%s", sym->binding_label);
2301 /* Error check the call. */
2302 if (args->next != NULL)
2304 gfc_error_now ("More actual than formal arguments in '%s' "
2305 "call at %L", name, &(args->expr->where));
2308 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2310 /* Make sure we have either the target or pointer attribute. */
2311 if (!args_sym->attr.target && !is_pointer)
2313 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2314 "a TARGET or an associated pointer",
2316 sym->name, &(args->expr->where));
2320 /* See if we have interoperable type and type param. */
2321 if (verify_c_interop (arg_ts) == SUCCESS
2322 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2324 if (args_sym->attr.target == 1)
2326 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2327 has the target attribute and is interoperable. */
2328 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2329 allocatable variable that has the TARGET attribute and
2330 is not an array of zero size. */
2331 if (args_sym->attr.allocatable == 1)
2333 if (args_sym->attr.dimension != 0
2334 && (args_sym->as && args_sym->as->rank == 0))
2336 gfc_error_now ("Allocatable variable '%s' used as a "
2337 "parameter to '%s' at %L must not be "
2338 "an array of zero size",
2339 args_sym->name, sym->name,
2340 &(args->expr->where));
2346 /* A non-allocatable target variable with C
2347 interoperable type and type parameters must be
2349 if (args_sym && args_sym->attr.dimension)
2351 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2353 gfc_error ("Assumed-shape array '%s' at %L "
2354 "cannot be an argument to the "
2355 "procedure '%s' because "
2356 "it is not C interoperable",
2358 &(args->expr->where), sym->name);
2361 else if (args_sym->as->type == AS_DEFERRED)
2363 gfc_error ("Deferred-shape array '%s' at %L "
2364 "cannot be an argument to the "
2365 "procedure '%s' because "
2366 "it is not C interoperable",
2368 &(args->expr->where), sym->name);
2373 /* Make sure it's not a character string. Arrays of
2374 any type should be ok if the variable is of a C
2375 interoperable type. */
2376 if (arg_ts->type == BT_CHARACTER)
2377 if (arg_ts->u.cl != NULL
2378 && (arg_ts->u.cl->length == NULL
2379 || arg_ts->u.cl->length->expr_type
2382 (arg_ts->u.cl->length->value.integer, 1)
2384 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2386 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2387 "at %L must have a length of 1",
2388 args_sym->name, sym->name,
2389 &(args->expr->where));
2395 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2397 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2399 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2400 "associated scalar POINTER", args_sym->name,
2401 sym->name, &(args->expr->where));
2407 /* The parameter is not required to be C interoperable. If it
2408 is not C interoperable, it must be a nonpolymorphic scalar
2409 with no length type parameters. It still must have either
2410 the pointer or target attribute, and it can be
2411 allocatable (but must be allocated when c_loc is called). */
2412 if (args->expr->rank != 0
2413 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2415 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2416 "scalar", args_sym->name, sym->name,
2417 &(args->expr->where));
2420 else if (arg_ts->type == BT_CHARACTER
2421 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2423 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2424 "%L must have a length of 1",
2425 args_sym->name, sym->name,
2426 &(args->expr->where));
2431 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2433 if (args_sym->attr.flavor != FL_PROCEDURE)
2435 /* TODO: Update this error message to allow for procedure
2436 pointers once they are implemented. */
2437 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2439 args_sym->name, sym->name,
2440 &(args->expr->where));
2443 else if (args_sym->attr.is_bind_c != 1)
2445 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2447 args_sym->name, sym->name,
2448 &(args->expr->where));
2453 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2458 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2459 "iso_c_binding function: '%s'!\n", sym->name);
2466 /* Resolve a function call, which means resolving the arguments, then figuring
2467 out which entity the name refers to. */
2468 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2469 to INTENT(OUT) or INTENT(INOUT). */
2472 resolve_function (gfc_expr *expr)
2474 gfc_actual_arglist *arg;
2479 procedure_type p = PROC_INTRINSIC;
2480 bool no_formal_args;
2484 sym = expr->symtree->n.sym;
2486 if (sym && sym->attr.intrinsic
2487 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2490 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2492 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2496 if (sym && sym->attr.abstract)
2498 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2499 sym->name, &expr->where);
2503 /* Switch off assumed size checking and do this again for certain kinds
2504 of procedure, once the procedure itself is resolved. */
2505 need_full_assumed_size++;
2507 if (expr->symtree && expr->symtree->n.sym)
2508 p = expr->symtree->n.sym->attr.proc;
2510 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2511 if (resolve_actual_arglist (expr->value.function.actual,
2512 p, no_formal_args) == FAILURE)
2515 /* Need to setup the call to the correct c_associated, depending on
2516 the number of cptrs to user gives to compare. */
2517 if (sym && sym->attr.is_iso_c == 1)
2519 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2523 /* Get the symtree for the new symbol (resolved func).
2524 the old one will be freed later, when it's no longer used. */
2525 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2528 /* Resume assumed_size checking. */
2529 need_full_assumed_size--;
2531 /* If the procedure is external, check for usage. */
2532 if (sym && is_external_proc (sym))
2533 resolve_global_procedure (sym, &expr->where,
2534 &expr->value.function.actual, 0);
2536 if (sym && sym->ts.type == BT_CHARACTER
2538 && sym->ts.u.cl->length == NULL
2540 && expr->value.function.esym == NULL
2541 && !sym->attr.contained)
2543 /* Internal procedures are taken care of in resolve_contained_fntype. */
2544 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2545 "be used at %L since it is not a dummy argument",
2546 sym->name, &expr->where);
2550 /* See if function is already resolved. */
2552 if (expr->value.function.name != NULL)
2554 if (expr->ts.type == BT_UNKNOWN)
2560 /* Apply the rules of section 14.1.2. */
2562 switch (procedure_kind (sym))
2565 t = resolve_generic_f (expr);
2568 case PTYPE_SPECIFIC:
2569 t = resolve_specific_f (expr);
2573 t = resolve_unknown_f (expr);
2577 gfc_internal_error ("resolve_function(): bad function type");
2581 /* If the expression is still a function (it might have simplified),
2582 then we check to see if we are calling an elemental function. */
2584 if (expr->expr_type != EXPR_FUNCTION)
2587 temp = need_full_assumed_size;
2588 need_full_assumed_size = 0;
2590 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2593 if (omp_workshare_flag
2594 && expr->value.function.esym
2595 && ! gfc_elemental (expr->value.function.esym))
2597 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2598 "in WORKSHARE construct", expr->value.function.esym->name,
2603 #define GENERIC_ID expr->value.function.isym->id
2604 else if (expr->value.function.actual != NULL
2605 && expr->value.function.isym != NULL
2606 && GENERIC_ID != GFC_ISYM_LBOUND
2607 && GENERIC_ID != GFC_ISYM_LEN
2608 && GENERIC_ID != GFC_ISYM_LOC
2609 && GENERIC_ID != GFC_ISYM_PRESENT)
2611 /* Array intrinsics must also have the last upper bound of an
2612 assumed size array argument. UBOUND and SIZE have to be
2613 excluded from the check if the second argument is anything
2616 for (arg = expr->value.function.actual; arg; arg = arg->next)
2618 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2619 && arg->next != NULL && arg->next->expr)
2621 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2624 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2627 if ((int)mpz_get_si (arg->next->expr->value.integer)
2632 if (arg->expr != NULL
2633 && arg->expr->rank > 0
2634 && resolve_assumed_size_actual (arg->expr))
2640 need_full_assumed_size = temp;
2643 if (!pure_function (expr, &name) && name)
2647 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2648 "FORALL %s", name, &expr->where,
2649 forall_flag == 2 ? "mask" : "block");
2652 else if (gfc_pure (NULL))
2654 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2655 "procedure within a PURE procedure", name, &expr->where);
2660 /* Functions without the RECURSIVE attribution are not allowed to
2661 * call themselves. */
2662 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2665 esym = expr->value.function.esym;
2667 if (is_illegal_recursion (esym, gfc_current_ns))
2669 if (esym->attr.entry && esym->ns->entries)
2670 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2671 " function '%s' is not RECURSIVE",
2672 esym->name, &expr->where, esym->ns->entries->sym->name);
2674 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2675 " is not RECURSIVE", esym->name, &expr->where);
2681 /* Character lengths of use associated functions may contains references to
2682 symbols not referenced from the current program unit otherwise. Make sure
2683 those symbols are marked as referenced. */
2685 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2686 && expr->value.function.esym->attr.use_assoc)
2688 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
2692 && !((expr->value.function.esym
2693 && expr->value.function.esym->attr.elemental)
2695 (expr->value.function.isym
2696 && expr->value.function.isym->elemental)))
2697 find_noncopying_intrinsics (expr->value.function.esym,
2698 expr->value.function.actual);
2700 /* Make sure that the expression has a typespec that works. */
2701 if (expr->ts.type == BT_UNKNOWN)
2703 if (expr->symtree->n.sym->result
2704 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
2705 && !expr->symtree->n.sym->result->attr.proc_pointer)
2706 expr->ts = expr->symtree->n.sym->result->ts;
2713 /************* Subroutine resolution *************/
2716 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2722 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2723 sym->name, &c->loc);
2724 else if (gfc_pure (NULL))
2725 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2731 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2735 if (sym->attr.generic)
2737 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2740 c->resolved_sym = s;
2741 pure_subroutine (c, s);
2745 /* TODO: Need to search for elemental references in generic interface. */
2748 if (sym->attr.intrinsic)
2749 return gfc_intrinsic_sub_interface (c, 0);
2756 resolve_generic_s (gfc_code *c)
2761 sym = c->symtree->n.sym;
2765 m = resolve_generic_s0 (c, sym);
2768 else if (m == MATCH_ERROR)
2772 if (sym->ns->parent == NULL)
2774 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2778 if (!generic_sym (sym))
2782 /* Last ditch attempt. See if the reference is to an intrinsic
2783 that possesses a matching interface. 14.1.2.4 */
2784 sym = c->symtree->n.sym;
2786 if (!gfc_is_intrinsic (sym, 1, c->loc))
2788 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2789 sym->name, &c->loc);
2793 m = gfc_intrinsic_sub_interface (c, 0);
2797 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2798 "intrinsic subroutine interface", sym->name, &c->loc);
2804 /* Set the name and binding label of the subroutine symbol in the call
2805 expression represented by 'c' to include the type and kind of the
2806 second parameter. This function is for resolving the appropriate
2807 version of c_f_pointer() and c_f_procpointer(). For example, a
2808 call to c_f_pointer() for a default integer pointer could have a
2809 name of c_f_pointer_i4. If no second arg exists, which is an error
2810 for these two functions, it defaults to the generic symbol's name
2811 and binding label. */
2814 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2815 char *name, char *binding_label)
2817 gfc_expr *arg = NULL;
2821 /* The second arg of c_f_pointer and c_f_procpointer determines
2822 the type and kind for the procedure name. */
2823 arg = c->ext.actual->next->expr;
2827 /* Set up the name to have the given symbol's name,
2828 plus the type and kind. */
2829 /* a derived type is marked with the type letter 'u' */
2830 if (arg->ts.type == BT_DERIVED)
2833 kind = 0; /* set the kind as 0 for now */
2837 type = gfc_type_letter (arg->ts.type);
2838 kind = arg->ts.kind;
2841 if (arg->ts.type == BT_CHARACTER)
2842 /* Kind info for character strings not needed. */
2845 sprintf (name, "%s_%c%d", sym->name, type, kind);
2846 /* Set up the binding label as the given symbol's label plus
2847 the type and kind. */
2848 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2852 /* If the second arg is missing, set the name and label as
2853 was, cause it should at least be found, and the missing
2854 arg error will be caught by compare_parameters(). */
2855 sprintf (name, "%s", sym->name);
2856 sprintf (binding_label, "%s", sym->binding_label);
2863 /* Resolve a generic version of the iso_c_binding procedure given
2864 (sym) to the specific one based on the type and kind of the
2865 argument(s). Currently, this function resolves c_f_pointer() and
2866 c_f_procpointer based on the type and kind of the second argument
2867 (FPTR). Other iso_c_binding procedures aren't specially handled.
2868 Upon successfully exiting, c->resolved_sym will hold the resolved
2869 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2873 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2875 gfc_symbol *new_sym;
2876 /* this is fine, since we know the names won't use the max */
2877 char name[GFC_MAX_SYMBOL_LEN + 1];
2878 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2879 /* default to success; will override if find error */
2880 match m = MATCH_YES;
2882 /* Make sure the actual arguments are in the necessary order (based on the
2883 formal args) before resolving. */
2884 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2886 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2887 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2889 set_name_and_label (c, sym, name, binding_label);
2891 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2893 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2895 /* Make sure we got a third arg if the second arg has non-zero
2896 rank. We must also check that the type and rank are
2897 correct since we short-circuit this check in
2898 gfc_procedure_use() (called above to sort actual args). */
2899 if (c->ext.actual->next->expr->rank != 0)
2901 if(c->ext.actual->next->next == NULL
2902 || c->ext.actual->next->next->expr == NULL)
2905 gfc_error ("Missing SHAPE parameter for call to %s "
2906 "at %L", sym->name, &(c->loc));
2908 else if (c->ext.actual->next->next->expr->ts.type
2910 || c->ext.actual->next->next->expr->rank != 1)
2913 gfc_error ("SHAPE parameter for call to %s at %L must "
2914 "be a rank 1 INTEGER array", sym->name,
2921 if (m != MATCH_ERROR)
2923 /* the 1 means to add the optional arg to formal list */
2924 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
2926 /* for error reporting, say it's declared where the original was */
2927 new_sym->declared_at = sym->declared_at;
2932 /* no differences for c_loc or c_funloc */
2936 /* set the resolved symbol */
2937 if (m != MATCH_ERROR)
2938 c->resolved_sym = new_sym;
2940 c->resolved_sym = sym;
2946 /* Resolve a subroutine call known to be specific. */
2949 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
2953 if(sym->attr.is_iso_c)
2955 m = gfc_iso_c_sub_interface (c,sym);
2959 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2961 if (sym->attr.dummy)
2963 sym->attr.proc = PROC_DUMMY;
2967 sym->attr.proc = PROC_EXTERNAL;
2971 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
2974 if (sym->attr.intrinsic)
2976 m = gfc_intrinsic_sub_interface (c, 1);
2980 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
2981 "with an intrinsic", sym->name, &c->loc);
2989 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2991 c->resolved_sym = sym;
2992 pure_subroutine (c, sym);
2999 resolve_specific_s (gfc_code *c)
3004 sym = c->symtree->n.sym;
3008 m = resolve_specific_s0 (c, sym);
3011 if (m == MATCH_ERROR)
3014 if (sym->ns->parent == NULL)
3017 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3023 sym = c->symtree->n.sym;
3024 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3025 sym->name, &c->loc);
3031 /* Resolve a subroutine call not known to be generic nor specific. */
3034 resolve_unknown_s (gfc_code *c)
3038 sym = c->symtree->n.sym;
3040 if (sym->attr.dummy)
3042 sym->attr.proc = PROC_DUMMY;
3046 /* See if we have an intrinsic function reference. */
3048 if (gfc_is_intrinsic (sym, 1, c->loc))
3050 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3055 /* The reference is to an external name. */
3058 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3060 c->resolved_sym = sym;
3062 pure_subroutine (c, sym);
3068 /* Resolve a subroutine call. Although it was tempting to use the same code
3069 for functions, subroutines and functions are stored differently and this
3070 makes things awkward. */
3073 resolve_call (gfc_code *c)
3076 procedure_type ptype = PROC_INTRINSIC;
3077 gfc_symbol *csym, *sym;
3078 bool no_formal_args;
3080 csym = c->symtree ? c->symtree->n.sym : NULL;
3082 if (csym && csym->ts.type != BT_UNKNOWN)
3084 gfc_error ("'%s' at %L has a type, which is not consistent with "
3085 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3089 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3092 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3093 sym = st ? st->n.sym : NULL;
3094 if (sym && csym != sym
3095 && sym->ns == gfc_current_ns
3096 && sym->attr.flavor == FL_PROCEDURE
3097 && sym->attr.contained)
3100 if (csym->attr.generic)
3101 c->symtree->n.sym = sym;
3104 csym = c->symtree->n.sym;
3108 /* Subroutines without the RECURSIVE attribution are not allowed to
3109 * call themselves. */
3110 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3112 if (csym->attr.entry && csym->ns->entries)
3113 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3114 " subroutine '%s' is not RECURSIVE",
3115 csym->name, &c->loc, csym->ns->entries->sym->name);
3117 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3118 " is not RECURSIVE", csym->name, &c->loc);
3123 /* Switch off assumed size checking and do this again for certain kinds
3124 of procedure, once the procedure itself is resolved. */
3125 need_full_assumed_size++;
3128 ptype = csym->attr.proc;
3130 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3131 if (resolve_actual_arglist (c->ext.actual, ptype,
3132 no_formal_args) == FAILURE)
3135 /* Resume assumed_size checking. */
3136 need_full_assumed_size--;
3138 /* If external, check for usage. */
3139 if (csym && is_external_proc (csym))
3140 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3143 if (c->resolved_sym == NULL)
3145 c->resolved_isym = NULL;
3146 switch (procedure_kind (csym))
3149 t = resolve_generic_s (c);
3152 case PTYPE_SPECIFIC:
3153 t = resolve_specific_s (c);
3157 t = resolve_unknown_s (c);
3161 gfc_internal_error ("resolve_subroutine(): bad function type");
3165 /* Some checks of elemental subroutine actual arguments. */
3166 if (resolve_elemental_actual (NULL, c) == FAILURE)
3169 if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
3170 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
3175 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3176 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3177 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3178 if their shapes do not match. If either op1->shape or op2->shape is
3179 NULL, return SUCCESS. */
3182 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3189 if (op1->shape != NULL && op2->shape != NULL)
3191 for (i = 0; i < op1->rank; i++)
3193 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3195 gfc_error ("Shapes for operands at %L and %L are not conformable",
3196 &op1->where, &op2->where);
3207 /* Resolve an operator expression node. This can involve replacing the
3208 operation with a user defined function call. */
3211 resolve_operator (gfc_expr *e)
3213 gfc_expr *op1, *op2;
3215 bool dual_locus_error;
3218 /* Resolve all subnodes-- give them types. */
3220 switch (e->value.op.op)
3223 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3226 /* Fall through... */
3229 case INTRINSIC_UPLUS:
3230 case INTRINSIC_UMINUS:
3231 case INTRINSIC_PARENTHESES:
3232 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3237 /* Typecheck the new node. */
3239 op1 = e->value.op.op1;
3240 op2 = e->value.op.op2;
3241 dual_locus_error = false;
3243 if ((op1 && op1->expr_type == EXPR_NULL)
3244 || (op2 && op2->expr_type == EXPR_NULL))
3246 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3250 switch (e->value.op.op)
3252 case INTRINSIC_UPLUS:
3253 case INTRINSIC_UMINUS:
3254 if (op1->ts.type == BT_INTEGER
3255 || op1->ts.type == BT_REAL
3256 || op1->ts.type == BT_COMPLEX)
3262 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3263 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3266 case INTRINSIC_PLUS:
3267 case INTRINSIC_MINUS:
3268 case INTRINSIC_TIMES:
3269 case INTRINSIC_DIVIDE:
3270 case INTRINSIC_POWER:
3271 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3273 gfc_type_convert_binary (e);
3278 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3279 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3280 gfc_typename (&op2->ts));
3283 case INTRINSIC_CONCAT:
3284 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3285 && op1->ts.kind == op2->ts.kind)
3287 e->ts.type = BT_CHARACTER;
3288 e->ts.kind = op1->ts.kind;
3293 _("Operands of string concatenation operator at %%L are %s/%s"),
3294 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3300 case INTRINSIC_NEQV:
3301 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3303 e->ts.type = BT_LOGICAL;
3304 e->ts.kind = gfc_kind_max (op1, op2);
3305 if (op1->ts.kind < e->ts.kind)
3306 gfc_convert_type (op1, &e->ts, 2);
3307 else if (op2->ts.kind < e->ts.kind)
3308 gfc_convert_type (op2, &e->ts, 2);
3312 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3313 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3314 gfc_typename (&op2->ts));
3319 if (op1->ts.type == BT_LOGICAL)
3321 e->ts.type = BT_LOGICAL;
3322 e->ts.kind = op1->ts.kind;
3326 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3327 gfc_typename (&op1->ts));
3331 case INTRINSIC_GT_OS:
3333 case INTRINSIC_GE_OS:
3335 case INTRINSIC_LT_OS:
3337 case INTRINSIC_LE_OS:
3338 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3340 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3344 /* Fall through... */
3347 case INTRINSIC_EQ_OS:
3349 case INTRINSIC_NE_OS:
3350 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3351 && op1->ts.kind == op2->ts.kind)
3353 e->ts.type = BT_LOGICAL;
3354 e->ts.kind = gfc_default_logical_kind;
3358 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3360 gfc_type_convert_binary (e);
3362 e->ts.type = BT_LOGICAL;
3363 e->ts.kind = gfc_default_logical_kind;
3367 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3369 _("Logicals at %%L must be compared with %s instead of %s"),
3370 (e->value.op.op == INTRINSIC_EQ
3371 || e->value.op.op == INTRINSIC_EQ_OS)
3372 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3375 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3376 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3377 gfc_typename (&op2->ts));
3381 case INTRINSIC_USER:
3382 if (e->value.op.uop->op == NULL)
3383 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3384 else if (op2 == NULL)
3385 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3386 e->value.op.uop->name, gfc_typename (&op1->ts));
3388 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3389 e->value.op.uop->name, gfc_typename (&op1->ts),
3390 gfc_typename (&op2->ts));
3394 case INTRINSIC_PARENTHESES:
3396 if (e->ts.type == BT_CHARACTER)
3397 e->ts.u.cl = op1->ts.u.cl;
3401 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3404 /* Deal with arrayness of an operand through an operator. */
3408 switch (e->value.op.op)
3410 case INTRINSIC_PLUS:
3411 case INTRINSIC_MINUS:
3412 case INTRINSIC_TIMES:
3413 case INTRINSIC_DIVIDE:
3414 case INTRINSIC_POWER:
3415 case INTRINSIC_CONCAT:
3419 case INTRINSIC_NEQV:
3421 case INTRINSIC_EQ_OS:
3423 case INTRINSIC_NE_OS:
3425 case INTRINSIC_GT_OS:
3427 case INTRINSIC_GE_OS:
3429 case INTRINSIC_LT_OS:
3431 case INTRINSIC_LE_OS:
3433 if (op1->rank == 0 && op2->rank == 0)
3436 if (op1->rank == 0 && op2->rank != 0)
3438 e->rank = op2->rank;
3440 if (e->shape == NULL)
3441 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3444 if (op1->rank != 0 && op2->rank == 0)
3446 e->rank = op1->rank;
3448 if (e->shape == NULL)
3449 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3452 if (op1->rank != 0 && op2->rank != 0)
3454 if (op1->rank == op2->rank)
3456 e->rank = op1->rank;
3457 if (e->shape == NULL)
3459 t = compare_shapes(op1, op2);
3463 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3468 /* Allow higher level expressions to work. */
3471 /* Try user-defined operators, and otherwise throw an error. */
3472 dual_locus_error = true;
3474 _("Inconsistent ranks for operator at %%L and %%L"));
3481 case INTRINSIC_PARENTHESES:
3483 case INTRINSIC_UPLUS:
3484 case INTRINSIC_UMINUS:
3485 /* Simply copy arrayness attribute */
3486 e->rank = op1->rank;
3488 if (e->shape == NULL)
3489 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3497 /* Attempt to simplify the expression. */
3500 t = gfc_simplify_expr (e, 0);
3501 /* Some calls do not succeed in simplification and return FAILURE
3502 even though there is no error; e.g. variable references to
3503 PARAMETER arrays. */
3504 if (!gfc_is_constant_expr (e))
3513 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3520 if (dual_locus_error)
3521 gfc_error (msg, &op1->where, &op2->where);
3523 gfc_error (msg, &e->where);
3529 /************** Array resolution subroutines **************/
3532 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3535 /* Compare two integer expressions. */
3538 compare_bound (gfc_expr *a, gfc_expr *b)
3542 if (a == NULL || a->expr_type != EXPR_CONSTANT
3543 || b == NULL || b->expr_type != EXPR_CONSTANT)
3546 /* If either of the types isn't INTEGER, we must have
3547 raised an error earlier. */
3549 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3552 i = mpz_cmp (a->value.integer, b->value.integer);
3562 /* Compare an integer expression with an integer. */
3565 compare_bound_int (gfc_expr *a, int b)
3569 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3572 if (a->ts.type != BT_INTEGER)
3573 gfc_internal_error ("compare_bound_int(): Bad expression");
3575 i = mpz_cmp_si (a->value.integer, b);
3585 /* Compare an integer expression with a mpz_t. */
3588 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3592 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3595 if (a->ts.type != BT_INTEGER)
3596 gfc_internal_error ("compare_bound_int(): Bad expression");
3598 i = mpz_cmp (a->value.integer, b);
3608 /* Compute the last value of a sequence given by a triplet.
3609 Return 0 if it wasn't able to compute the last value, or if the
3610 sequence if empty, and 1 otherwise. */
3613 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3614 gfc_expr *stride, mpz_t last)
3618 if (start == NULL || start->expr_type != EXPR_CONSTANT
3619 || end == NULL || end->expr_type != EXPR_CONSTANT
3620 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3623 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3624 || (stride != NULL && stride->ts.type != BT_INTEGER))
3627 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3629 if (compare_bound (start, end) == CMP_GT)
3631 mpz_set (last, end->value.integer);
3635 if (compare_bound_int (stride, 0) == CMP_GT)
3637 /* Stride is positive */
3638 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3643 /* Stride is negative */
3644 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3649 mpz_sub (rem, end->value.integer, start->value.integer);
3650 mpz_tdiv_r (rem, rem, stride->value.integer);
3651 mpz_sub (last, end->value.integer, rem);
3658 /* Compare a single dimension of an array reference to the array
3662 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3666 /* Given start, end and stride values, calculate the minimum and
3667 maximum referenced indexes. */
3669 switch (ar->dimen_type[i])
3675 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3677 gfc_warning ("Array reference at %L is out of bounds "
3678 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3679 mpz_get_si (ar->start[i]->value.integer),
3680 mpz_get_si (as->lower[i]->value.integer), i+1);
3683 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3685 gfc_warning ("Array reference at %L is out of bounds "
3686 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3687 mpz_get_si (ar->start[i]->value.integer),
3688 mpz_get_si (as->upper[i]->value.integer), i+1);
3696 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3697 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3699 comparison comp_start_end = compare_bound (AR_START, AR_END);
3701 /* Check for zero stride, which is not allowed. */
3702 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3704 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3708 /* if start == len || (stride > 0 && start < len)
3709 || (stride < 0 && start > len),
3710 then the array section contains at least one element. In this
3711 case, there is an out-of-bounds access if
3712 (start < lower || start > upper). */
3713 if (compare_bound (AR_START, AR_END) == CMP_EQ
3714 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3715 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3716 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3717 && comp_start_end == CMP_GT))
3719 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3721 gfc_warning ("Lower array reference at %L is out of bounds "
3722 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3723 mpz_get_si (AR_START->value.integer),
3724 mpz_get_si (as->lower[i]->value.integer), i+1);
3727 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3729 gfc_warning ("Lower array reference at %L is out of bounds "
3730 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3731 mpz_get_si (AR_START->value.integer),
3732 mpz_get_si (as->upper[i]->value.integer), i+1);
3737 /* If we can compute the highest index of the array section,
3738 then it also has to be between lower and upper. */
3739 mpz_init (last_value);
3740 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3743 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3745 gfc_warning ("Upper array reference at %L is out of bounds "
3746 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3747 mpz_get_si (last_value),
3748 mpz_get_si (as->lower[i]->value.integer), i+1);
3749 mpz_clear (last_value);
3752 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3754 gfc_warning ("Upper array reference at %L is out of bounds "
3755 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3756 mpz_get_si (last_value),
3757 mpz_get_si (as->upper[i]->value.integer), i+1);
3758 mpz_clear (last_value);
3762 mpz_clear (last_value);
3770 gfc_internal_error ("check_dimension(): Bad array reference");
3777 /* Compare an array reference with an array specification. */
3780 compare_spec_to_ref (gfc_array_ref *ar)
3787 /* TODO: Full array sections are only allowed as actual parameters. */
3788 if (as->type == AS_ASSUMED_SIZE
3789 && (/*ar->type == AR_FULL
3790 ||*/ (ar->type == AR_SECTION
3791 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3793 gfc_error ("Rightmost upper bound of assumed size array section "
3794 "not specified at %L", &ar->where);
3798 if (ar->type == AR_FULL)
3801 if (as->rank != ar->dimen)
3803 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3804 &ar->where, ar->dimen, as->rank);
3808 for (i = 0; i < as->rank; i++)
3809 if (check_dimension (i, ar, as) == FAILURE)
3816 /* Resolve one part of an array index. */
3819 gfc_resolve_index (gfc_expr *index, int check_scalar)
3826 if (gfc_resolve_expr (index) == FAILURE)
3829 if (check_scalar && index->rank != 0)
3831 gfc_error ("Array index at %L must be scalar", &index->where);
3835 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
3837 gfc_error ("Array index at %L must be of INTEGER type, found %s",
3838 &index->where, gfc_basic_typename (index->ts.type));
3842 if (index->ts.type == BT_REAL)
3843 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
3844 &index->where) == FAILURE)
3847 if (index->ts.kind != gfc_index_integer_kind
3848 || index->ts.type != BT_INTEGER)
3851 ts.type = BT_INTEGER;
3852 ts.kind = gfc_index_integer_kind;
3854 gfc_convert_type_warn (index, &ts, 2, 0);
3860 /* Resolve a dim argument to an intrinsic function. */
3863 gfc_resolve_dim_arg (gfc_expr *dim)
3868 if (gfc_resolve_expr (dim) == FAILURE)
3873 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3878 if (dim->ts.type != BT_INTEGER)
3880 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3884 if (dim->ts.kind != gfc_index_integer_kind)
3888 ts.type = BT_INTEGER;
3889 ts.kind = gfc_index_integer_kind;
3891 gfc_convert_type_warn (dim, &ts, 2, 0);
3897 /* Given an expression that contains array references, update those array
3898 references to point to the right array specifications. While this is
3899 filled in during matching, this information is difficult to save and load
3900 in a module, so we take care of it here.
3902 The idea here is that the original array reference comes from the
3903 base symbol. We traverse the list of reference structures, setting
3904 the stored reference to references. Component references can
3905 provide an additional array specification. */
3908 find_array_spec (gfc_expr *e)
3912 gfc_symbol *derived;
3915 as = e->symtree->n.sym->as;
3918 for (ref = e->ref; ref; ref = ref->next)
3923 gfc_internal_error ("find_array_spec(): Missing spec");
3930 if (derived == NULL)
3931 derived = e->symtree->n.sym->ts.u.derived;
3933 c = derived->components;
3935 for (; c; c = c->next)
3936 if (c == ref->u.c.component)
3938 /* Track the sequence of component references. */
3939 if (c->ts.type == BT_DERIVED)
3940 derived = c->ts.u.derived;
3945 gfc_internal_error ("find_array_spec(): Component not found");
3947 if (c->attr.dimension)
3950 gfc_internal_error ("find_array_spec(): unused as(1)");
3961 gfc_internal_error ("find_array_spec(): unused as(2)");
3965 /* Resolve an array reference. */
3968 resolve_array_ref (gfc_array_ref *ar)
3970 int i, check_scalar;
3973 for (i = 0; i < ar->dimen; i++)
3975 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
3977 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
3979 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
3981 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
3986 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
3990 ar->dimen_type[i] = DIMEN_ELEMENT;
3994 ar->dimen_type[i] = DIMEN_VECTOR;
3995 if (e->expr_type == EXPR_VARIABLE
3996 && e->symtree->n.sym->ts.type == BT_DERIVED)
3997 ar->start[i] = gfc_get_parentheses (e);
4001 gfc_error ("Array index at %L is an array of rank %d",
4002 &ar->c_where[i], e->rank);
4007 /* If the reference type is unknown, figure out what kind it is. */
4009 if (ar->type == AR_UNKNOWN)
4011 ar->type = AR_ELEMENT;
4012 for (i = 0; i < ar->dimen; i++)
4013 if (ar->dimen_type[i] == DIMEN_RANGE
4014 || ar->dimen_type[i] == DIMEN_VECTOR)
4016 ar->type = AR_SECTION;
4021 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4029 resolve_substring (gfc_ref *ref)
4031 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4033 if (ref->u.ss.start != NULL)
4035 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4038 if (ref->u.ss.start->ts.type != BT_INTEGER)
4040 gfc_error ("Substring start index at %L must be of type INTEGER",
4041 &ref->u.ss.start->where);
4045 if (ref->u.ss.start->rank != 0)
4047 gfc_error ("Substring start index at %L must be scalar",
4048 &ref->u.ss.start->where);
4052 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4053 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4054 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4056 gfc_error ("Substring start index at %L is less than one",
4057 &ref->u.ss.start->where);
4062 if (ref->u.ss.end != NULL)
4064 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4067 if (ref->u.ss.end->ts.type != BT_INTEGER)
4069 gfc_error ("Substring end index at %L must be of type INTEGER",
4070 &ref->u.ss.end->where);
4074 if (ref->u.ss.end->rank != 0)
4076 gfc_error ("Substring end index at %L must be scalar",
4077 &ref->u.ss.end->where);
4081 if (ref->u.ss.length != NULL
4082 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4083 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4084 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4086 gfc_error ("Substring end index at %L exceeds the string length",
4087 &ref->u.ss.start->where);
4091 if (compare_bound_mpz_t (ref->u.ss.end,
4092 gfc_integer_kinds[k].huge) == CMP_GT
4093 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4094 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4096 gfc_error ("Substring end index at %L is too large",
4097 &ref->u.ss.end->where);
4106 /* This function supplies missing substring charlens. */
4109 gfc_resolve_substring_charlen (gfc_expr *e)
4112 gfc_expr *start, *end;
4114 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4115 if (char_ref->type == REF_SUBSTRING)
4121 gcc_assert (char_ref->next == NULL);
4125 if (e->ts.u.cl->length)
4126 gfc_free_expr (e->ts.u.cl->length);
4127 else if (e->expr_type == EXPR_VARIABLE
4128 && e->symtree->n.sym->attr.dummy)
4132 e->ts.type = BT_CHARACTER;
4133 e->ts.kind = gfc_default_character_kind;
4136 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4138 if (char_ref->u.ss.start)
4139 start = gfc_copy_expr (char_ref->u.ss.start);
4141 start = gfc_int_expr (1);
4143 if (char_ref->u.ss.end)
4144 end = gfc_copy_expr (char_ref->u.ss.end);
4145 else if (e->expr_type == EXPR_VARIABLE)
4146 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4153 /* Length = (end - start +1). */
4154 e->ts.u.cl->length = gfc_subtract (end, start);
4155 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length, gfc_int_expr (1));
4157 e->ts.u.cl->length->ts.type = BT_INTEGER;
4158 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4160 /* Make sure that the length is simplified. */
4161 gfc_simplify_expr (e->ts.u.cl->length, 1);
4162 gfc_resolve_expr (e->ts.u.cl->length);
4166 /* Resolve subtype references. */
4169 resolve_ref (gfc_expr *expr)
4171 int current_part_dimension, n_components, seen_part_dimension;
4174 for (ref = expr->ref; ref; ref = ref->next)
4175 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4177 find_array_spec (expr);
4181 for (ref = expr->ref; ref; ref = ref->next)
4185 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4193 resolve_substring (ref);
4197 /* Check constraints on part references. */
4199 current_part_dimension = 0;
4200 seen_part_dimension = 0;
4203 for (ref = expr->ref; ref; ref = ref->next)
4208 switch (ref->u.ar.type)
4212 current_part_dimension = 1;
4216 current_part_dimension = 0;
4220 gfc_internal_error ("resolve_ref(): Bad array reference");
4226 if (current_part_dimension || seen_part_dimension)
4228 if (ref->u.c.component->attr.pointer)
4230 gfc_error ("Component to the right of a part reference "
4231 "with nonzero rank must not have the POINTER "
4232 "attribute at %L", &expr->where);
4235 else if (ref->u.c.component->attr.allocatable)
4237 gfc_error ("Component to the right of a part reference "
4238 "with nonzero rank must not have the ALLOCATABLE "
4239 "attribute at %L", &expr->where);
4251 if (((ref->type == REF_COMPONENT && n_components > 1)
4252 || ref->next == NULL)
4253 && current_part_dimension
4254 && seen_part_dimension)
4256 gfc_error ("Two or more part references with nonzero rank must "
4257 "not be specified at %L", &expr->where);
4261 if (ref->type == REF_COMPONENT)
4263 if (current_part_dimension)
4264 seen_part_dimension = 1;
4266 /* reset to make sure */
4267 current_part_dimension = 0;
4275 /* Given an expression, determine its shape. This is easier than it sounds.
4276 Leaves the shape array NULL if it is not possible to determine the shape. */
4279 expression_shape (gfc_expr *e)
4281 mpz_t array[GFC_MAX_DIMENSIONS];
4284 if (e->rank == 0 || e->shape != NULL)
4287 for (i = 0; i < e->rank; i++)
4288 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4291 e->shape = gfc_get_shape (e->rank);
4293 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4298 for (i--; i >= 0; i--)
4299 mpz_clear (array[i]);
4303 /* Given a variable expression node, compute the rank of the expression by
4304 examining the base symbol and any reference structures it may have. */
4307 expression_rank (gfc_expr *e)
4312 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4313 could lead to serious confusion... */
4314 gcc_assert (e->expr_type != EXPR_COMPCALL);
4318 if (e->expr_type == EXPR_ARRAY)
4320 /* Constructors can have a rank different from one via RESHAPE(). */
4322 if (e->symtree == NULL)
4328 e->rank = (e->symtree->n.sym->as == NULL)
4329 ? 0 : e->symtree->n.sym->as->rank;
4335 for (ref = e->ref; ref; ref = ref->next)
4337 if (ref->type != REF_ARRAY)
4340 if (ref->u.ar.type == AR_FULL)
4342 rank = ref->u.ar.as->rank;
4346 if (ref->u.ar.type == AR_SECTION)
4348 /* Figure out the rank of the section. */
4350 gfc_internal_error ("expression_rank(): Two array specs");
4352 for (i = 0; i < ref->u.ar.dimen; i++)
4353 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4354 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4364 expression_shape (e);
4368 /* Resolve a variable expression. */
4371 resolve_variable (gfc_expr *e)
4378 if (e->symtree == NULL)
4381 if (e->ref && resolve_ref (e) == FAILURE)
4384 sym = e->symtree->n.sym;
4385 if (sym->attr.flavor == FL_PROCEDURE
4386 && (!sym->attr.function
4387 || (sym->attr.function && sym->result
4388 && sym->result->attr.proc_pointer
4389 && !sym->result->attr.function)))
4391 e->ts.type = BT_PROCEDURE;
4392 goto resolve_procedure;
4395 if (sym->ts.type != BT_UNKNOWN)
4396 gfc_variable_attr (e, &e->ts);
4399 /* Must be a simple variable reference. */
4400 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4405 if (check_assumed_size_reference (sym, e))
4408 /* Deal with forward references to entries during resolve_code, to
4409 satisfy, at least partially, 12.5.2.5. */
4410 if (gfc_current_ns->entries
4411 && current_entry_id == sym->entry_id
4414 && cs_base->current->op != EXEC_ENTRY)
4416 gfc_entry_list *entry;
4417 gfc_formal_arglist *formal;
4421 /* If the symbol is a dummy... */
4422 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4424 entry = gfc_current_ns->entries;
4427 /* ...test if the symbol is a parameter of previous entries. */
4428 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4429 for (formal = entry->sym->formal; formal; formal = formal->next)
4431 if (formal->sym && sym->name == formal->sym->name)
4435 /* If it has not been seen as a dummy, this is an error. */
4438 if (specification_expr)
4439 gfc_error ("Variable '%s', used in a specification expression"
4440 ", is referenced at %L before the ENTRY statement "
4441 "in which it is a parameter",
4442 sym->name, &cs_base->current->loc);
4444 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4445 "statement in which it is a parameter",
4446 sym->name, &cs_base->current->loc);
4451 /* Now do the same check on the specification expressions. */
4452 specification_expr = 1;
4453 if (sym->ts.type == BT_CHARACTER
4454 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
4458 for (n = 0; n < sym->as->rank; n++)
4460 specification_expr = 1;
4461 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4463 specification_expr = 1;
4464 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4467 specification_expr = 0;
4470 /* Update the symbol's entry level. */
4471 sym->entry_id = current_entry_id + 1;
4475 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
4482 /* Checks to see that the correct symbol has been host associated.
4483 The only situation where this arises is that in which a twice
4484 contained function is parsed after the host association is made.
4485 Therefore, on detecting this, change the symbol in the expression
4486 and convert the array reference into an actual arglist if the old
4487 symbol is a variable. */
4489 check_host_association (gfc_expr *e)
4491 gfc_symbol *sym, *old_sym;
4495 gfc_actual_arglist *arg, *tail = NULL;
4496 bool retval = e->expr_type == EXPR_FUNCTION;
4498 /* If the expression is the result of substitution in
4499 interface.c(gfc_extend_expr) because there is no way in
4500 which the host association can be wrong. */
4501 if (e->symtree == NULL
4502 || e->symtree->n.sym == NULL
4503 || e->user_operator)
4506 old_sym = e->symtree->n.sym;
4508 if (gfc_current_ns->parent
4509 && old_sym->ns != gfc_current_ns)
4511 /* Use the 'USE' name so that renamed module symbols are
4512 correctly handled. */
4513 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
4515 if (sym && old_sym != sym
4516 && sym->ts.type == old_sym->ts.type
4517 && sym->attr.flavor == FL_PROCEDURE
4518 && sym->attr.contained)
4520 /* Clear the shape, since it might not be valid. */
4521 if (e->shape != NULL)
4523 for (n = 0; n < e->rank; n++)
4524 mpz_clear (e->shape[n]);
4526 gfc_free (e->shape);
4529 /* Give the expression the right symtree! */
4530 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
4531 gcc_assert (st != NULL);
4533 if (old_sym->attr.flavor == FL_PROCEDURE
4534 || e->expr_type == EXPR_FUNCTION)
4536 /* Original was function so point to the new symbol, since
4537 the actual argument list is already attached to the
4539 e->value.function.esym = NULL;
4544 /* Original was variable so convert array references into
4545 an actual arglist. This does not need any checking now
4546 since gfc_resolve_function will take care of it. */
4547 e->value.function.actual = NULL;
4548 e->expr_type = EXPR_FUNCTION;
4551 /* Ambiguity will not arise if the array reference is not
4552 the last reference. */
4553 for (ref = e->ref; ref; ref = ref->next)
4554 if (ref->type == REF_ARRAY && ref->next == NULL)
4557 gcc_assert (ref->type == REF_ARRAY);
4559 /* Grab the start expressions from the array ref and
4560 copy them into actual arguments. */
4561 for (n = 0; n < ref->u.ar.dimen; n++)
4563 arg = gfc_get_actual_arglist ();
4564 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4565 if (e->value.function.actual == NULL)
4566 tail = e->value.function.actual = arg;
4574 /* Dump the reference list and set the rank. */
4575 gfc_free_ref_list (e->ref);
4577 e->rank = sym->as ? sym->as->rank : 0;
4580 gfc_resolve_expr (e);
4584 /* This might have changed! */
4585 return e->expr_type == EXPR_FUNCTION;
4590 gfc_resolve_character_operator (gfc_expr *e)
4592 gfc_expr *op1 = e->value.op.op1;
4593 gfc_expr *op2 = e->value.op.op2;
4594 gfc_expr *e1 = NULL;
4595 gfc_expr *e2 = NULL;
4597 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
4599 if (op1->ts.u.cl && op1->ts.u.cl->length)
4600 e1 = gfc_copy_expr (op1->ts.u.cl->length);
4601 else if (op1->expr_type == EXPR_CONSTANT)
4602 e1 = gfc_int_expr (op1->value.character.length);
4604 if (op2->ts.u.cl && op2->ts.u.cl->length)
4605 e2 = gfc_copy_expr (op2->ts.u.cl->length);
4606 else if (op2->expr_type == EXPR_CONSTANT)
4607 e2 = gfc_int_expr (op2->value.character.length);
4609 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4614 e->ts.u.cl->length = gfc_add (e1, e2);
4615 e->ts.u.cl->length->ts.type = BT_INTEGER;
4616 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4617 gfc_simplify_expr (e->ts.u.cl->length, 0);
4618 gfc_resolve_expr (e->ts.u.cl->length);
4624 /* Ensure that an character expression has a charlen and, if possible, a
4625 length expression. */
4628 fixup_charlen (gfc_expr *e)
4630 /* The cases fall through so that changes in expression type and the need
4631 for multiple fixes are picked up. In all circumstances, a charlen should
4632 be available for the middle end to hang a backend_decl on. */
4633 switch (e->expr_type)
4636 gfc_resolve_character_operator (e);
4639 if (e->expr_type == EXPR_ARRAY)
4640 gfc_resolve_character_array_constructor (e);
4642 case EXPR_SUBSTRING:
4643 if (!e->ts.u.cl && e->ref)
4644 gfc_resolve_substring_charlen (e);
4648 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4655 /* Update an actual argument to include the passed-object for type-bound
4656 procedures at the right position. */
4658 static gfc_actual_arglist*
4659 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
4662 gcc_assert (argpos > 0);
4666 gfc_actual_arglist* result;
4668 result = gfc_get_actual_arglist ();
4672 result->name = name;
4678 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
4680 lst = update_arglist_pass (NULL, po, argpos - 1, name);
4685 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
4688 extract_compcall_passed_object (gfc_expr* e)
4692 gcc_assert (e->expr_type == EXPR_COMPCALL);
4694 if (e->value.compcall.base_object)
4695 po = gfc_copy_expr (e->value.compcall.base_object);
4698 po = gfc_get_expr ();
4699 po->expr_type = EXPR_VARIABLE;
4700 po->symtree = e->symtree;
4701 po->ref = gfc_copy_ref (e->ref);
4704 if (gfc_resolve_expr (po) == FAILURE)
4711 /* Update the arglist of an EXPR_COMPCALL expression to include the
4715 update_compcall_arglist (gfc_expr* e)
4718 gfc_typebound_proc* tbp;
4720 tbp = e->value.compcall.tbp;
4725 po = extract_compcall_passed_object (e);
4731 gfc_error ("Passed-object at %L must be scalar", &e->where);
4735 if (tbp->nopass || e->value.compcall.ignore_pass)
4741 gcc_assert (tbp->pass_arg_num > 0);
4742 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4750 /* Extract the passed object from a PPC call (a copy of it). */
4753 extract_ppc_passed_object (gfc_expr *e)
4758 po = gfc_get_expr ();
4759 po->expr_type = EXPR_VARIABLE;
4760 po->symtree = e->symtree;
4761 po->ref = gfc_copy_ref (e->ref);
4763 /* Remove PPC reference. */
4765 while ((*ref)->next)
4766 (*ref) = (*ref)->next;
4767 gfc_free_ref_list (*ref);
4770 if (gfc_resolve_expr (po) == FAILURE)
4777 /* Update the actual arglist of a procedure pointer component to include the
4781 update_ppc_arglist (gfc_expr* e)
4785 gfc_typebound_proc* tb;
4787 if (!gfc_is_proc_ptr_comp (e, &ppc))
4794 else if (tb->nopass)
4797 po = extract_ppc_passed_object (e);
4803 gfc_error ("Passed-object at %L must be scalar", &e->where);
4807 gcc_assert (tb->pass_arg_num > 0);
4808 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4816 /* Check that the object a TBP is called on is valid, i.e. it must not be
4817 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
4820 check_typebound_baseobject (gfc_expr* e)
4824 base = extract_compcall_passed_object (e);
4828 gcc_assert (base->ts.type == BT_DERIVED);
4829 if (base->ts.u.derived->attr.abstract)
4831 gfc_error ("Base object for type-bound procedure call at %L is of"
4832 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
4840 /* Resolve a call to a type-bound procedure, either function or subroutine,
4841 statically from the data in an EXPR_COMPCALL expression. The adapted
4842 arglist and the target-procedure symtree are returned. */
4845 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
4846 gfc_actual_arglist** actual)
4848 gcc_assert (e->expr_type == EXPR_COMPCALL);
4849 gcc_assert (!e->value.compcall.tbp->is_generic);
4851 /* Update the actual arglist for PASS. */
4852 if (update_compcall_arglist (e) == FAILURE)
4855 *actual = e->value.compcall.actual;
4856 *target = e->value.compcall.tbp->u.specific;
4858 gfc_free_ref_list (e->ref);
4860 e->value.compcall.actual = NULL;
4866 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
4867 which of the specific bindings (if any) matches the arglist and transform
4868 the expression into a call of that binding. */
4871 resolve_typebound_generic_call (gfc_expr* e)
4873 gfc_typebound_proc* genproc;
4874 const char* genname;
4876 gcc_assert (e->expr_type == EXPR_COMPCALL);
4877 genname = e->value.compcall.name;
4878 genproc = e->value.compcall.tbp;
4880 if (!genproc->is_generic)
4883 /* Try the bindings on this type and in the inheritance hierarchy. */
4884 for (; genproc; genproc = genproc->overridden)
4888 gcc_assert (genproc->is_generic);
4889 for (g = genproc->u.generic; g; g = g->next)
4892 gfc_actual_arglist* args;
4895 gcc_assert (g->specific);
4897 if (g->specific->error)
4900 target = g->specific->u.specific->n.sym;
4902 /* Get the right arglist by handling PASS/NOPASS. */
4903 args = gfc_copy_actual_arglist (e->value.compcall.actual);
4904 if (!g->specific->nopass)
4907 po = extract_compcall_passed_object (e);
4911 gcc_assert (g->specific->pass_arg_num > 0);
4912 gcc_assert (!g->specific->error);
4913 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
4914 g->specific->pass_arg);
4916 resolve_actual_arglist (args, target->attr.proc,
4917 is_external_proc (target) && !target->formal);
4919 /* Check if this arglist matches the formal. */
4920 matches = gfc_arglist_matches_symbol (&args, target);
4922 /* Clean up and break out of the loop if we've found it. */
4923 gfc_free_actual_arglist (args);
4926 e->value.compcall.tbp = g->specific;
4932 /* Nothing matching found! */
4933 gfc_error ("Found no matching specific binding for the call to the GENERIC"
4934 " '%s' at %L", genname, &e->where);
4942 /* Resolve a call to a type-bound subroutine. */
4945 resolve_typebound_call (gfc_code* c)
4947 gfc_actual_arglist* newactual;
4948 gfc_symtree* target;
4950 /* Check that's really a SUBROUTINE. */
4951 if (!c->expr1->value.compcall.tbp->subroutine)
4953 gfc_error ("'%s' at %L should be a SUBROUTINE",
4954 c->expr1->value.compcall.name, &c->loc);
4958 if (check_typebound_baseobject (c->expr1) == FAILURE)
4961 if (resolve_typebound_generic_call (c->expr1) == FAILURE)
4964 /* Transform into an ordinary EXEC_CALL for now. */
4966 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
4969 c->ext.actual = newactual;
4970 c->symtree = target;
4971 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
4973 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
4974 gfc_free_expr (c->expr1);
4977 return resolve_call (c);
4981 /* Resolve a component-call expression. */
4984 resolve_compcall (gfc_expr* e)
4986 gfc_actual_arglist* newactual;
4987 gfc_symtree* target;
4989 /* Check that's really a FUNCTION. */
4990 if (!e->value.compcall.tbp->function)
4992 gfc_error ("'%s' at %L should be a FUNCTION",
4993 e->value.compcall.name, &e->where);
4997 /* These must not be assign-calls! */
4998 gcc_assert (!e->value.compcall.assign);
5000 if (check_typebound_baseobject (e) == FAILURE)
5003 if (resolve_typebound_generic_call (e) == FAILURE)
5005 gcc_assert (!e->value.compcall.tbp->is_generic);
5007 /* Take the rank from the function's symbol. */
5008 if (e->value.compcall.tbp->u.specific->n.sym->as)
5009 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5011 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5012 arglist to the TBP's binding target. */
5014 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5017 e->value.function.actual = newactual;
5018 e->value.function.name = e->value.compcall.name;
5019 e->value.function.esym = target->n.sym;
5020 e->value.function.isym = NULL;
5021 e->symtree = target;
5022 e->ts = target->n.sym->ts;
5023 e->expr_type = EXPR_FUNCTION;
5025 return gfc_resolve_expr (e);
5029 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5032 resolve_ppc_call (gfc_code* c)
5034 gfc_component *comp;
5035 gcc_assert (gfc_is_proc_ptr_comp (c->expr1, &comp));
5037 c->resolved_sym = c->expr1->symtree->n.sym;
5038 c->expr1->expr_type = EXPR_VARIABLE;
5040 if (!comp->attr.subroutine)
5041 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5043 if (resolve_ref (c->expr1) == FAILURE)
5046 if (update_ppc_arglist (c->expr1) == FAILURE)
5049 c->ext.actual = c->expr1->value.compcall.actual;
5051 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5052 comp->formal == NULL) == FAILURE)
5055 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5061 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5064 resolve_expr_ppc (gfc_expr* e)
5066 gfc_component *comp;
5067 gcc_assert (gfc_is_proc_ptr_comp (e, &comp));
5069 /* Convert to EXPR_FUNCTION. */
5070 e->expr_type = EXPR_FUNCTION;
5071 e->value.function.isym = NULL;
5072 e->value.function.actual = e->value.compcall.actual;
5074 if (comp->as != NULL)
5075 e->rank = comp->as->rank;
5077 if (!comp->attr.function)
5078 gfc_add_function (&comp->attr, comp->name, &e->where);
5080 if (resolve_ref (e) == FAILURE)
5083 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5084 comp->formal == NULL) == FAILURE)
5087 if (update_ppc_arglist (e) == FAILURE)
5090 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
5096 /* Resolve an expression. That is, make sure that types of operands agree
5097 with their operators, intrinsic operators are converted to function calls
5098 for overloaded types and unresolved function references are resolved. */
5101 gfc_resolve_expr (gfc_expr *e)
5108 switch (e->expr_type)
5111 t = resolve_operator (e);
5117 if (check_host_association (e))
5118 t = resolve_function (e);
5121 t = resolve_variable (e);
5123 expression_rank (e);
5126 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
5127 && e->ref->type != REF_SUBSTRING)
5128 gfc_resolve_substring_charlen (e);
5133 t = resolve_compcall (e);
5136 case EXPR_SUBSTRING:
5137 t = resolve_ref (e);
5146 t = resolve_expr_ppc (e);
5151 if (resolve_ref (e) == FAILURE)
5154 t = gfc_resolve_array_constructor (e);
5155 /* Also try to expand a constructor. */
5158 expression_rank (e);
5159 gfc_expand_constructor (e);
5162 /* This provides the opportunity for the length of constructors with
5163 character valued function elements to propagate the string length
5164 to the expression. */
5165 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
5166 t = gfc_resolve_character_array_constructor (e);
5170 case EXPR_STRUCTURE:
5171 t = resolve_ref (e);
5175 t = resolve_structure_cons (e);
5179 t = gfc_simplify_expr (e, 0);
5183 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
5186 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
5193 /* Resolve an expression from an iterator. They must be scalar and have
5194 INTEGER or (optionally) REAL type. */
5197 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
5198 const char *name_msgid)
5200 if (gfc_resolve_expr (expr) == FAILURE)
5203 if (expr->rank != 0)
5205 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
5209 if (expr->ts.type != BT_INTEGER)
5211 if (expr->ts.type == BT_REAL)
5214 return gfc_notify_std (GFC_STD_F95_DEL,
5215 "Deleted feature: %s at %L must be integer",
5216 _(name_msgid), &expr->where);
5219 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
5226 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
5234 /* Resolve the expressions in an iterator structure. If REAL_OK is
5235 false allow only INTEGER type iterators, otherwise allow REAL types. */
5238 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
5240 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
5244 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
5246 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5251 if (gfc_resolve_iterator_expr (iter->start, real_ok,
5252 "Start expression in DO loop") == FAILURE)
5255 if (gfc_resolve_iterator_expr (iter->end, real_ok,
5256 "End expression in DO loop") == FAILURE)
5259 if (gfc_resolve_iterator_expr (iter->step, real_ok,
5260 "Step expression in DO loop") == FAILURE)
5263 if (iter->step->expr_type == EXPR_CONSTANT)
5265 if ((iter->step->ts.type == BT_INTEGER
5266 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
5267 || (iter->step->ts.type == BT_REAL
5268 && mpfr_sgn (iter->step->value.real) == 0))
5270 gfc_error ("Step expression in DO loop at %L cannot be zero",
5271 &iter->step->where);
5276 /* Convert start, end, and step to the same type as var. */
5277 if (iter->start->ts.kind != iter->var->ts.kind
5278 || iter->start->ts.type != iter->var->ts.type)
5279 gfc_convert_type (iter->start, &iter->var->ts, 2);
5281 if (iter->end->ts.kind != iter->var->ts.kind
5282 || iter->end->ts.type != iter->var->ts.type)
5283 gfc_convert_type (iter->end, &iter->var->ts, 2);
5285 if (iter->step->ts.kind != iter->var->ts.kind
5286 || iter->step->ts.type != iter->var->ts.type)
5287 gfc_convert_type (iter->step, &iter->var->ts, 2);
5289 if (iter->start->expr_type == EXPR_CONSTANT
5290 && iter->end->expr_type == EXPR_CONSTANT
5291 && iter->step->expr_type == EXPR_CONSTANT)
5294 if (iter->start->ts.type == BT_INTEGER)
5296 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
5297 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
5301 sgn = mpfr_sgn (iter->step->value.real);
5302 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
5304 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
5305 gfc_warning ("DO loop at %L will be executed zero times",
5306 &iter->step->where);
5313 /* Traversal function for find_forall_index. f == 2 signals that
5314 that variable itself is not to be checked - only the references. */
5317 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
5319 if (expr->expr_type != EXPR_VARIABLE)
5322 /* A scalar assignment */
5323 if (!expr->ref || *f == 1)
5325 if (expr->symtree->n.sym == sym)
5337 /* Check whether the FORALL index appears in the expression or not.
5338 Returns SUCCESS if SYM is found in EXPR. */
5341 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
5343 if (gfc_traverse_expr (expr, sym, forall_index, f))
5350 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5351 to be a scalar INTEGER variable. The subscripts and stride are scalar
5352 INTEGERs, and if stride is a constant it must be nonzero.
5353 Furthermore "A subscript or stride in a forall-triplet-spec shall
5354 not contain a reference to any index-name in the
5355 forall-triplet-spec-list in which it appears." (7.5.4.1) */
5358 resolve_forall_iterators (gfc_forall_iterator *it)
5360 gfc_forall_iterator *iter, *iter2;
5362 for (iter = it; iter; iter = iter->next)
5364 if (gfc_resolve_expr (iter->var) == SUCCESS
5365 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
5366 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
5369 if (gfc_resolve_expr (iter->start) == SUCCESS
5370 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
5371 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
5372 &iter->start->where);
5373 if (iter->var->ts.kind != iter->start->ts.kind)
5374 gfc_convert_type (iter->start, &iter->var->ts, 2);
5376 if (gfc_resolve_expr (iter->end) == SUCCESS
5377 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
5378 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
5380 if (iter->var->ts.kind != iter->end->ts.kind)
5381 gfc_convert_type (iter->end, &iter->var->ts, 2);
5383 if (gfc_resolve_expr (iter->stride) == SUCCESS)
5385 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
5386 gfc_error ("FORALL stride expression at %L must be a scalar %s",
5387 &iter->stride->where, "INTEGER");
5389 if (iter->stride->expr_type == EXPR_CONSTANT
5390 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
5391 gfc_error ("FORALL stride expression at %L cannot be zero",
5392 &iter->stride->where);
5394 if (iter->var->ts.kind != iter->stride->ts.kind)
5395 gfc_convert_type (iter->stride, &iter->var->ts, 2);
5398 for (iter = it; iter; iter = iter->next)
5399 for (iter2 = iter; iter2; iter2 = iter2->next)
5401 if (find_forall_index (iter2->start,
5402 iter->var->symtree->n.sym, 0) == SUCCESS
5403 || find_forall_index (iter2->end,
5404 iter->var->symtree->n.sym, 0) == SUCCESS
5405 || find_forall_index (iter2->stride,
5406 iter->var->symtree->n.sym, 0) == SUCCESS)
5407 gfc_error ("FORALL index '%s' may not appear in triplet "
5408 "specification at %L", iter->var->symtree->name,
5409 &iter2->start->where);
5414 /* Given a pointer to a symbol that is a derived type, see if it's
5415 inaccessible, i.e. if it's defined in another module and the components are
5416 PRIVATE. The search is recursive if necessary. Returns zero if no
5417 inaccessible components are found, nonzero otherwise. */
5420 derived_inaccessible (gfc_symbol *sym)
5424 if (sym->attr.use_assoc && sym->attr.private_comp)
5427 for (c = sym->components; c; c = c->next)
5429 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
5437 /* Resolve the argument of a deallocate expression. The expression must be
5438 a pointer or a full array. */
5441 resolve_deallocate_expr (gfc_expr *e)
5443 symbol_attribute attr;
5444 int allocatable, pointer, check_intent_in;
5447 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5448 check_intent_in = 1;
5450 if (gfc_resolve_expr (e) == FAILURE)
5453 if (e->expr_type != EXPR_VARIABLE)
5456 allocatable = e->symtree->n.sym->attr.allocatable;
5457 pointer = e->symtree->n.sym->attr.pointer;
5458 for (ref = e->ref; ref; ref = ref->next)
5461 check_intent_in = 0;
5466 if (ref->u.ar.type != AR_FULL)
5471 allocatable = (ref->u.c.component->as != NULL
5472 && ref->u.c.component->as->type == AS_DEFERRED);
5473 pointer = ref->u.c.component->attr.pointer;
5482 attr = gfc_expr_attr (e);
5484 if (allocatable == 0 && attr.pointer == 0)
5487 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5492 && e->symtree->n.sym->attr.intent == INTENT_IN)
5494 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
5495 e->symtree->n.sym->name, &e->where);
5503 /* Returns true if the expression e contains a reference to the symbol sym. */
5505 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5507 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
5514 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
5516 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
5520 /* Given the expression node e for an allocatable/pointer of derived type to be
5521 allocated, get the expression node to be initialized afterwards (needed for
5522 derived types with default initializers, and derived types with allocatable
5523 components that need nullification.) */
5526 expr_to_initialize (gfc_expr *e)
5532 result = gfc_copy_expr (e);
5534 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
5535 for (ref = result->ref; ref; ref = ref->next)
5536 if (ref->type == REF_ARRAY && ref->next == NULL)
5538 ref->u.ar.type = AR_FULL;
5540 for (i = 0; i < ref->u.ar.dimen; i++)
5541 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
5543 result->rank = ref->u.ar.dimen;
5551 /* Resolve the expression in an ALLOCATE statement, doing the additional
5552 checks to see whether the expression is OK or not. The expression must
5553 have a trailing array reference that gives the size of the array. */
5556 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
5558 int i, pointer, allocatable, dimension, check_intent_in;
5559 symbol_attribute attr;
5560 gfc_ref *ref, *ref2;
5567 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5568 check_intent_in = 1;
5570 if (gfc_resolve_expr (e) == FAILURE)
5573 /* Make sure the expression is allocatable or a pointer. If it is
5574 pointer, the next-to-last reference must be a pointer. */
5578 if (e->expr_type != EXPR_VARIABLE)
5581 attr = gfc_expr_attr (e);
5582 pointer = attr.pointer;
5583 dimension = attr.dimension;
5587 allocatable = e->symtree->n.sym->attr.allocatable;
5588 pointer = e->symtree->n.sym->attr.pointer;
5589 dimension = e->symtree->n.sym->attr.dimension;
5591 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
5594 check_intent_in = 0;
5599 if (ref->next != NULL)
5604 allocatable = (ref->u.c.component->as != NULL
5605 && ref->u.c.component->as->type == AS_DEFERRED);
5607 pointer = ref->u.c.component->attr.pointer;
5608 dimension = ref->u.c.component->attr.dimension;
5619 if (allocatable == 0 && pointer == 0)
5621 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5627 && e->symtree->n.sym->attr.intent == INTENT_IN)
5629 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
5630 e->symtree->n.sym->name, &e->where);
5634 /* Add default initializer for those derived types that need them. */
5635 if (e->ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&e->ts)))
5637 init_st = gfc_get_code ();
5638 init_st->loc = code->loc;
5639 init_st->op = EXEC_INIT_ASSIGN;
5640 init_st->expr1 = expr_to_initialize (e);
5641 init_st->expr2 = init_e;
5642 init_st->next = code->next;
5643 code->next = init_st;
5646 if (pointer || dimension == 0)
5649 /* Make sure the next-to-last reference node is an array specification. */
5651 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
5653 gfc_error ("Array specification required in ALLOCATE statement "
5654 "at %L", &e->where);
5658 /* Make sure that the array section reference makes sense in the
5659 context of an ALLOCATE specification. */
5663 for (i = 0; i < ar->dimen; i++)
5665 if (ref2->u.ar.type == AR_ELEMENT)
5668 switch (ar->dimen_type[i])
5674 if (ar->start[i] != NULL
5675 && ar->end[i] != NULL
5676 && ar->stride[i] == NULL)
5679 /* Fall Through... */
5683 gfc_error ("Bad array specification in ALLOCATE statement at %L",
5690 for (a = code->ext.alloc_list; a; a = a->next)
5692 sym = a->expr->symtree->n.sym;
5694 /* TODO - check derived type components. */
5695 if (sym->ts.type == BT_DERIVED)
5698 if ((ar->start[i] != NULL
5699 && gfc_find_sym_in_expr (sym, ar->start[i]))
5700 || (ar->end[i] != NULL
5701 && gfc_find_sym_in_expr (sym, ar->end[i])))
5703 gfc_error ("'%s' must not appear in the array specification at "
5704 "%L in the same ALLOCATE statement where it is "
5705 "itself allocated", sym->name, &ar->where);
5715 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
5717 gfc_expr *stat, *errmsg, *pe, *qe;
5718 gfc_alloc *a, *p, *q;
5720 stat = code->expr1 ? code->expr1 : NULL;
5722 errmsg = code->expr2 ? code->expr2 : NULL;
5724 /* Check the stat variable. */
5727 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
5728 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
5729 stat->symtree->n.sym->name, &stat->where);
5731 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
5732 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
5735 if ((stat->ts.type != BT_INTEGER
5736 && !(stat->ref && (stat->ref->type == REF_ARRAY
5737 || stat->ref->type == REF_COMPONENT)))
5739 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
5740 "variable", &stat->where);
5742 for (p = code->ext.alloc_list; p; p = p->next)
5743 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
5744 gfc_error ("Stat-variable at %L shall not be %sd within "
5745 "the same %s statement", &stat->where, fcn, fcn);
5748 /* Check the errmsg variable. */
5752 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
5755 if (errmsg->symtree->n.sym->attr.intent == INTENT_IN)
5756 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
5757 errmsg->symtree->n.sym->name, &errmsg->where);
5759 if (gfc_pure (NULL) && gfc_impure_variable (errmsg->symtree->n.sym))
5760 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
5763 if ((errmsg->ts.type != BT_CHARACTER
5765 && (errmsg->ref->type == REF_ARRAY
5766 || errmsg->ref->type == REF_COMPONENT)))
5767 || errmsg->rank > 0 )
5768 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
5769 "variable", &errmsg->where);
5771 for (p = code->ext.alloc_list; p; p = p->next)
5772 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
5773 gfc_error ("Errmsg-variable at %L shall not be %sd within "
5774 "the same %s statement", &errmsg->where, fcn, fcn);
5777 /* Check that an allocate-object appears only once in the statement.
5778 FIXME: Checking derived types is disabled. */
5779 for (p = code->ext.alloc_list; p; p = p->next)
5782 if ((pe->ref && pe->ref->type != REF_COMPONENT)
5783 && (pe->symtree->n.sym->ts.type != BT_DERIVED))
5785 for (q = p->next; q; q = q->next)
5788 if ((qe->ref && qe->ref->type != REF_COMPONENT)
5789 && (qe->symtree->n.sym->ts.type != BT_DERIVED)
5790 && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
5791 gfc_error ("Allocate-object at %L also appears at %L",
5792 &pe->where, &qe->where);
5797 if (strcmp (fcn, "ALLOCATE") == 0)
5799 for (a = code->ext.alloc_list; a; a = a->next)
5800 resolve_allocate_expr (a->expr, code);
5804 for (a = code->ext.alloc_list; a; a = a->next)
5805 resolve_deallocate_expr (a->expr);
5810 /************ SELECT CASE resolution subroutines ************/
5812 /* Callback function for our mergesort variant. Determines interval
5813 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
5814 op1 > op2. Assumes we're not dealing with the default case.
5815 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
5816 There are nine situations to check. */
5819 compare_cases (const gfc_case *op1, const gfc_case *op2)
5823 if (op1->low == NULL) /* op1 = (:L) */
5825 /* op2 = (:N), so overlap. */
5827 /* op2 = (M:) or (M:N), L < M */
5828 if (op2->low != NULL
5829 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
5832 else if (op1->high == NULL) /* op1 = (K:) */
5834 /* op2 = (M:), so overlap. */
5836 /* op2 = (:N) or (M:N), K > N */
5837 if (op2->high != NULL
5838 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
5841 else /* op1 = (K:L) */
5843 if (op2->low == NULL) /* op2 = (:N), K > N */
5844 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
5846 else if (op2->high == NULL) /* op2 = (M:), L < M */
5847 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
5849 else /* op2 = (M:N) */
5853 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
5856 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
5865 /* Merge-sort a double linked case list, detecting overlap in the
5866 process. LIST is the head of the double linked case list before it
5867 is sorted. Returns the head of the sorted list if we don't see any
5868 overlap, or NULL otherwise. */
5871 check_case_overlap (gfc_case *list)
5873 gfc_case *p, *q, *e, *tail;
5874 int insize, nmerges, psize, qsize, cmp, overlap_seen;
5876 /* If the passed list was empty, return immediately. */
5883 /* Loop unconditionally. The only exit from this loop is a return
5884 statement, when we've finished sorting the case list. */
5891 /* Count the number of merges we do in this pass. */
5894 /* Loop while there exists a merge to be done. */
5899 /* Count this merge. */
5902 /* Cut the list in two pieces by stepping INSIZE places
5903 forward in the list, starting from P. */
5906 for (i = 0; i < insize; i++)
5915 /* Now we have two lists. Merge them! */
5916 while (psize > 0 || (qsize > 0 && q != NULL))
5918 /* See from which the next case to merge comes from. */
5921 /* P is empty so the next case must come from Q. */
5926 else if (qsize == 0 || q == NULL)
5935 cmp = compare_cases (p, q);
5938 /* The whole case range for P is less than the
5946 /* The whole case range for Q is greater than
5947 the case range for P. */
5954 /* The cases overlap, or they are the same
5955 element in the list. Either way, we must
5956 issue an error and get the next case from P. */
5957 /* FIXME: Sort P and Q by line number. */
5958 gfc_error ("CASE label at %L overlaps with CASE "
5959 "label at %L", &p->where, &q->where);
5967 /* Add the next element to the merged list. */
5976 /* P has now stepped INSIZE places along, and so has Q. So
5977 they're the same. */
5982 /* If we have done only one merge or none at all, we've
5983 finished sorting the cases. */
5992 /* Otherwise repeat, merging lists twice the size. */
5998 /* Check to see if an expression is suitable for use in a CASE statement.
5999 Makes sure that all case expressions are scalar constants of the same
6000 type. Return FAILURE if anything is wrong. */
6003 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
6005 if (e == NULL) return SUCCESS;
6007 if (e->ts.type != case_expr->ts.type)
6009 gfc_error ("Expression in CASE statement at %L must be of type %s",
6010 &e->where, gfc_basic_typename (case_expr->ts.type));
6014 /* C805 (R808) For a given case-construct, each case-value shall be of
6015 the same type as case-expr. For character type, length differences
6016 are allowed, but the kind type parameters shall be the same. */
6018 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
6020 gfc_error ("Expression in CASE statement at %L must be of kind %d",
6021 &e->where, case_expr->ts.kind);
6025 /* Convert the case value kind to that of case expression kind, if needed.
6026 FIXME: Should a warning be issued? */
6027 if (e->ts.kind != case_expr->ts.kind)
6028 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
6032 gfc_error ("Expression in CASE statement at %L must be scalar",
6041 /* Given a completely parsed select statement, we:
6043 - Validate all expressions and code within the SELECT.
6044 - Make sure that the selection expression is not of the wrong type.
6045 - Make sure that no case ranges overlap.
6046 - Eliminate unreachable cases and unreachable code resulting from
6047 removing case labels.
6049 The standard does allow unreachable cases, e.g. CASE (5:3). But
6050 they are a hassle for code generation, and to prevent that, we just
6051 cut them out here. This is not necessary for overlapping cases
6052 because they are illegal and we never even try to generate code.
6054 We have the additional caveat that a SELECT construct could have
6055 been a computed GOTO in the source code. Fortunately we can fairly
6056 easily work around that here: The case_expr for a "real" SELECT CASE
6057 is in code->expr1, but for a computed GOTO it is in code->expr2. All
6058 we have to do is make sure that the case_expr is a scalar integer
6062 resolve_select (gfc_code *code)
6065 gfc_expr *case_expr;
6066 gfc_case *cp, *default_case, *tail, *head;
6067 int seen_unreachable;
6073 if (code->expr1 == NULL)
6075 /* This was actually a computed GOTO statement. */
6076 case_expr = code->expr2;
6077 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
6078 gfc_error ("Selection expression in computed GOTO statement "
6079 "at %L must be a scalar integer expression",
6082 /* Further checking is not necessary because this SELECT was built
6083 by the compiler, so it should always be OK. Just move the
6084 case_expr from expr2 to expr so that we can handle computed
6085 GOTOs as normal SELECTs from here on. */
6086 code->expr1 = code->expr2;
6091 case_expr = code->expr1;
6093 type = case_expr->ts.type;
6094 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
6096 gfc_error ("Argument of SELECT statement at %L cannot be %s",
6097 &case_expr->where, gfc_typename (&case_expr->ts));
6099 /* Punt. Going on here just produce more garbage error messages. */
6103 if (case_expr->rank != 0)
6105 gfc_error ("Argument of SELECT statement at %L must be a scalar "
6106 "expression", &case_expr->where);
6112 /* PR 19168 has a long discussion concerning a mismatch of the kinds
6113 of the SELECT CASE expression and its CASE values. Walk the lists
6114 of case values, and if we find a mismatch, promote case_expr to
6115 the appropriate kind. */
6117 if (type == BT_LOGICAL || type == BT_INTEGER)
6119 for (body = code->block; body; body = body->block)
6121 /* Walk the case label list. */
6122 for (cp = body->ext.case_list; cp; cp = cp->next)
6124 /* Intercept the DEFAULT case. It does not have a kind. */
6125 if (cp->low == NULL && cp->high == NULL)
6128 /* Unreachable case ranges are discarded, so ignore. */
6129 if (cp->low != NULL && cp->high != NULL
6130 && cp->low != cp->high
6131 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6134 /* FIXME: Should a warning be issued? */
6136 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
6137 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
6139 if (cp->high != NULL
6140 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
6141 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
6146 /* Assume there is no DEFAULT case. */
6147 default_case = NULL;
6152 for (body = code->block; body; body = body->block)
6154 /* Assume the CASE list is OK, and all CASE labels can be matched. */
6156 seen_unreachable = 0;
6158 /* Walk the case label list, making sure that all case labels
6160 for (cp = body->ext.case_list; cp; cp = cp->next)
6162 /* Count the number of cases in the whole construct. */
6165 /* Intercept the DEFAULT case. */
6166 if (cp->low == NULL && cp->high == NULL)
6168 if (default_case != NULL)
6170 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6171 "by a second DEFAULT CASE at %L",
6172 &default_case->where, &cp->where);
6183 /* Deal with single value cases and case ranges. Errors are
6184 issued from the validation function. */
6185 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
6186 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
6192 if (type == BT_LOGICAL
6193 && ((cp->low == NULL || cp->high == NULL)
6194 || cp->low != cp->high))
6196 gfc_error ("Logical range in CASE statement at %L is not "
6197 "allowed", &cp->low->where);
6202 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
6205 value = cp->low->value.logical == 0 ? 2 : 1;
6206 if (value & seen_logical)
6208 gfc_error ("constant logical value in CASE statement "
6209 "is repeated at %L",
6214 seen_logical |= value;
6217 if (cp->low != NULL && cp->high != NULL
6218 && cp->low != cp->high
6219 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6221 if (gfc_option.warn_surprising)
6222 gfc_warning ("Range specification at %L can never "
6223 "be matched", &cp->where);
6225 cp->unreachable = 1;
6226 seen_unreachable = 1;
6230 /* If the case range can be matched, it can also overlap with
6231 other cases. To make sure it does not, we put it in a
6232 double linked list here. We sort that with a merge sort
6233 later on to detect any overlapping cases. */
6237 head->right = head->left = NULL;
6242 tail->right->left = tail;
6249 /* It there was a failure in the previous case label, give up
6250 for this case label list. Continue with the next block. */
6254 /* See if any case labels that are unreachable have been seen.
6255 If so, we eliminate them. This is a bit of a kludge because
6256 the case lists for a single case statement (label) is a
6257 single forward linked lists. */
6258 if (seen_unreachable)
6260 /* Advance until the first case in the list is reachable. */
6261 while (body->ext.case_list != NULL
6262 && body->ext.case_list->unreachable)
6264 gfc_case *n = body->ext.case_list;
6265 body->ext.case_list = body->ext.case_list->next;
6267 gfc_free_case_list (n);
6270 /* Strip all other unreachable cases. */
6271 if (body->ext.case_list)
6273 for (cp = body->ext.case_list; cp->next; cp = cp->next)
6275 if (cp->next->unreachable)
6277 gfc_case *n = cp->next;
6278 cp->next = cp->next->next;
6280 gfc_free_case_list (n);
6287 /* See if there were overlapping cases. If the check returns NULL,
6288 there was overlap. In that case we don't do anything. If head
6289 is non-NULL, we prepend the DEFAULT case. The sorted list can
6290 then used during code generation for SELECT CASE constructs with
6291 a case expression of a CHARACTER type. */
6294 head = check_case_overlap (head);
6296 /* Prepend the default_case if it is there. */
6297 if (head != NULL && default_case)
6299 default_case->left = NULL;
6300 default_case->right = head;
6301 head->left = default_case;
6305 /* Eliminate dead blocks that may be the result if we've seen
6306 unreachable case labels for a block. */
6307 for (body = code; body && body->block; body = body->block)
6309 if (body->block->ext.case_list == NULL)
6311 /* Cut the unreachable block from the code chain. */
6312 gfc_code *c = body->block;
6313 body->block = c->block;
6315 /* Kill the dead block, but not the blocks below it. */
6317 gfc_free_statements (c);
6321 /* More than two cases is legal but insane for logical selects.
6322 Issue a warning for it. */
6323 if (gfc_option.warn_surprising && type == BT_LOGICAL
6325 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
6330 /* Resolve a transfer statement. This is making sure that:
6331 -- a derived type being transferred has only non-pointer components
6332 -- a derived type being transferred doesn't have private components, unless
6333 it's being transferred from the module where the type was defined
6334 -- we're not trying to transfer a whole assumed size array. */
6337 resolve_transfer (gfc_code *code)
6346 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
6349 sym = exp->symtree->n.sym;
6352 /* Go to actual component transferred. */
6353 for (ref = code->expr1->ref; ref; ref = ref->next)
6354 if (ref->type == REF_COMPONENT)
6355 ts = &ref->u.c.component->ts;
6357 if (ts->type == BT_DERIVED)
6359 /* Check that transferred derived type doesn't contain POINTER
6361 if (ts->u.derived->attr.pointer_comp)
6363 gfc_error ("Data transfer element at %L cannot have "
6364 "POINTER components", &code->loc);
6368 if (ts->u.derived->attr.alloc_comp)
6370 gfc_error ("Data transfer element at %L cannot have "
6371 "ALLOCATABLE components", &code->loc);
6375 if (derived_inaccessible (ts->u.derived))
6377 gfc_error ("Data transfer element at %L cannot have "
6378 "PRIVATE components",&code->loc);
6383 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
6384 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
6386 gfc_error ("Data transfer element at %L cannot be a full reference to "
6387 "an assumed-size array", &code->loc);
6393 /*********** Toplevel code resolution subroutines ***********/
6395 /* Find the set of labels that are reachable from this block. We also
6396 record the last statement in each block. */
6399 find_reachable_labels (gfc_code *block)
6406 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
6408 /* Collect labels in this block. We don't keep those corresponding
6409 to END {IF|SELECT}, these are checked in resolve_branch by going
6410 up through the code_stack. */
6411 for (c = block; c; c = c->next)
6413 if (c->here && c->op != EXEC_END_BLOCK)
6414 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
6417 /* Merge with labels from parent block. */
6420 gcc_assert (cs_base->prev->reachable_labels);
6421 bitmap_ior_into (cs_base->reachable_labels,
6422 cs_base->prev->reachable_labels);
6426 /* Given a branch to a label, see if the branch is conforming.
6427 The code node describes where the branch is located. */
6430 resolve_branch (gfc_st_label *label, gfc_code *code)
6437 /* Step one: is this a valid branching target? */
6439 if (label->defined == ST_LABEL_UNKNOWN)
6441 gfc_error ("Label %d referenced at %L is never defined", label->value,
6446 if (label->defined != ST_LABEL_TARGET)
6448 gfc_error ("Statement at %L is not a valid branch target statement "
6449 "for the branch statement at %L", &label->where, &code->loc);
6453 /* Step two: make sure this branch is not a branch to itself ;-) */
6455 if (code->here == label)
6457 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
6461 /* Step three: See if the label is in the same block as the
6462 branching statement. The hard work has been done by setting up
6463 the bitmap reachable_labels. */
6465 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
6468 /* Step four: If we haven't found the label in the bitmap, it may
6469 still be the label of the END of the enclosing block, in which
6470 case we find it by going up the code_stack. */
6472 for (stack = cs_base; stack; stack = stack->prev)
6473 if (stack->current->next && stack->current->next->here == label)
6478 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
6482 /* The label is not in an enclosing block, so illegal. This was
6483 allowed in Fortran 66, so we allow it as extension. No
6484 further checks are necessary in this case. */
6485 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
6486 "as the GOTO statement at %L", &label->where,
6492 /* Check whether EXPR1 has the same shape as EXPR2. */
6495 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
6497 mpz_t shape[GFC_MAX_DIMENSIONS];
6498 mpz_t shape2[GFC_MAX_DIMENSIONS];
6499 gfc_try result = FAILURE;
6502 /* Compare the rank. */
6503 if (expr1->rank != expr2->rank)
6506 /* Compare the size of each dimension. */
6507 for (i=0; i<expr1->rank; i++)
6509 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
6512 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
6515 if (mpz_cmp (shape[i], shape2[i]))
6519 /* When either of the two expression is an assumed size array, we
6520 ignore the comparison of dimension sizes. */
6525 for (i--; i >= 0; i--)
6527 mpz_clear (shape[i]);
6528 mpz_clear (shape2[i]);
6534 /* Check whether a WHERE assignment target or a WHERE mask expression
6535 has the same shape as the outmost WHERE mask expression. */
6538 resolve_where (gfc_code *code, gfc_expr *mask)
6544 cblock = code->block;
6546 /* Store the first WHERE mask-expr of the WHERE statement or construct.
6547 In case of nested WHERE, only the outmost one is stored. */
6548 if (mask == NULL) /* outmost WHERE */
6550 else /* inner WHERE */
6557 /* Check if the mask-expr has a consistent shape with the
6558 outmost WHERE mask-expr. */
6559 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
6560 gfc_error ("WHERE mask at %L has inconsistent shape",
6561 &cblock->expr1->where);
6564 /* the assignment statement of a WHERE statement, or the first
6565 statement in where-body-construct of a WHERE construct */
6566 cnext = cblock->next;
6571 /* WHERE assignment statement */
6574 /* Check shape consistent for WHERE assignment target. */
6575 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
6576 gfc_error ("WHERE assignment target at %L has "
6577 "inconsistent shape", &cnext->expr1->where);
6581 case EXEC_ASSIGN_CALL:
6582 resolve_call (cnext);
6583 if (!cnext->resolved_sym->attr.elemental)
6584 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
6585 &cnext->ext.actual->expr->where);
6588 /* WHERE or WHERE construct is part of a where-body-construct */
6590 resolve_where (cnext, e);
6594 gfc_error ("Unsupported statement inside WHERE at %L",
6597 /* the next statement within the same where-body-construct */
6598 cnext = cnext->next;
6600 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
6601 cblock = cblock->block;
6606 /* Resolve assignment in FORALL construct.
6607 NVAR is the number of FORALL index variables, and VAR_EXPR records the
6608 FORALL index variables. */
6611 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
6615 for (n = 0; n < nvar; n++)
6617 gfc_symbol *forall_index;
6619 forall_index = var_expr[n]->symtree->n.sym;
6621 /* Check whether the assignment target is one of the FORALL index
6623 if ((code->expr1->expr_type == EXPR_VARIABLE)
6624 && (code->expr1->symtree->n.sym == forall_index))
6625 gfc_error ("Assignment to a FORALL index variable at %L",
6626 &code->expr1->where);
6629 /* If one of the FORALL index variables doesn't appear in the
6630 assignment variable, then there could be a many-to-one
6631 assignment. Emit a warning rather than an error because the
6632 mask could be resolving this problem. */
6633 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
6634 gfc_warning ("The FORALL with index '%s' is not used on the "
6635 "left side of the assignment at %L and so might "
6636 "cause multiple assignment to this object",
6637 var_expr[n]->symtree->name, &code->expr1->where);
6643 /* Resolve WHERE statement in FORALL construct. */
6646 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
6647 gfc_expr **var_expr)
6652 cblock = code->block;
6655 /* the assignment statement of a WHERE statement, or the first
6656 statement in where-body-construct of a WHERE construct */
6657 cnext = cblock->next;
6662 /* WHERE assignment statement */
6664 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
6667 /* WHERE operator assignment statement */
6668 case EXEC_ASSIGN_CALL:
6669 resolve_call (cnext);
6670 if (!cnext->resolved_sym->attr.elemental)
6671 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
6672 &cnext->ext.actual->expr->where);
6675 /* WHERE or WHERE construct is part of a where-body-construct */
6677 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
6681 gfc_error ("Unsupported statement inside WHERE at %L",
6684 /* the next statement within the same where-body-construct */
6685 cnext = cnext->next;
6687 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
6688 cblock = cblock->block;
6693 /* Traverse the FORALL body to check whether the following errors exist:
6694 1. For assignment, check if a many-to-one assignment happens.
6695 2. For WHERE statement, check the WHERE body to see if there is any
6696 many-to-one assignment. */
6699 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
6703 c = code->block->next;
6709 case EXEC_POINTER_ASSIGN:
6710 gfc_resolve_assign_in_forall (c, nvar, var_expr);
6713 case EXEC_ASSIGN_CALL:
6717 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
6718 there is no need to handle it here. */
6722 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
6727 /* The next statement in the FORALL body. */
6733 /* Counts the number of iterators needed inside a forall construct, including
6734 nested forall constructs. This is used to allocate the needed memory
6735 in gfc_resolve_forall. */
6738 gfc_count_forall_iterators (gfc_code *code)
6740 int max_iters, sub_iters, current_iters;
6741 gfc_forall_iterator *fa;
6743 gcc_assert(code->op == EXEC_FORALL);
6747 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
6750 code = code->block->next;
6754 if (code->op == EXEC_FORALL)
6756 sub_iters = gfc_count_forall_iterators (code);
6757 if (sub_iters > max_iters)
6758 max_iters = sub_iters;
6763 return current_iters + max_iters;
6767 /* Given a FORALL construct, first resolve the FORALL iterator, then call
6768 gfc_resolve_forall_body to resolve the FORALL body. */
6771 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
6773 static gfc_expr **var_expr;
6774 static int total_var = 0;
6775 static int nvar = 0;
6777 gfc_forall_iterator *fa;
6782 /* Start to resolve a FORALL construct */
6783 if (forall_save == 0)
6785 /* Count the total number of FORALL index in the nested FORALL
6786 construct in order to allocate the VAR_EXPR with proper size. */
6787 total_var = gfc_count_forall_iterators (code);
6789 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
6790 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
6793 /* The information about FORALL iterator, including FORALL index start, end
6794 and stride. The FORALL index can not appear in start, end or stride. */
6795 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
6797 /* Check if any outer FORALL index name is the same as the current
6799 for (i = 0; i < nvar; i++)
6801 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
6803 gfc_error ("An outer FORALL construct already has an index "
6804 "with this name %L", &fa->var->where);
6808 /* Record the current FORALL index. */
6809 var_expr[nvar] = gfc_copy_expr (fa->var);
6813 /* No memory leak. */
6814 gcc_assert (nvar <= total_var);
6817 /* Resolve the FORALL body. */
6818 gfc_resolve_forall_body (code, nvar, var_expr);
6820 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
6821 gfc_resolve_blocks (code->block, ns);
6825 /* Free only the VAR_EXPRs allocated in this frame. */
6826 for (i = nvar; i < tmp; i++)
6827 gfc_free_expr (var_expr[i]);
6831 /* We are in the outermost FORALL construct. */
6832 gcc_assert (forall_save == 0);
6834 /* VAR_EXPR is not needed any more. */
6835 gfc_free (var_expr);
6841 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL ,GOTO and
6844 static void resolve_code (gfc_code *, gfc_namespace *);
6847 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
6851 for (; b; b = b->block)
6853 t = gfc_resolve_expr (b->expr1);
6854 if (gfc_resolve_expr (b->expr2) == FAILURE)
6860 if (t == SUCCESS && b->expr1 != NULL
6861 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
6862 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
6869 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
6870 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
6875 resolve_branch (b->label1, b);
6888 case EXEC_OMP_ATOMIC:
6889 case EXEC_OMP_CRITICAL:
6891 case EXEC_OMP_MASTER:
6892 case EXEC_OMP_ORDERED:
6893 case EXEC_OMP_PARALLEL:
6894 case EXEC_OMP_PARALLEL_DO:
6895 case EXEC_OMP_PARALLEL_SECTIONS:
6896 case EXEC_OMP_PARALLEL_WORKSHARE:
6897 case EXEC_OMP_SECTIONS:
6898 case EXEC_OMP_SINGLE:
6900 case EXEC_OMP_TASKWAIT:
6901 case EXEC_OMP_WORKSHARE:
6905 gfc_internal_error ("resolve_block(): Bad block type");
6908 resolve_code (b->next, ns);
6913 /* Does everything to resolve an ordinary assignment. Returns true
6914 if this is an interface assignment. */
6916 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
6926 if (gfc_extend_assign (code, ns) == SUCCESS)
6928 gfc_symbol* assign_proc;
6931 if (code->op == EXEC_ASSIGN_CALL)
6933 lhs = code->ext.actual->expr;
6934 rhsptr = &code->ext.actual->next->expr;
6935 assign_proc = code->symtree->n.sym;
6939 gfc_actual_arglist* args;
6940 gfc_typebound_proc* tbp;
6942 gcc_assert (code->op == EXEC_COMPCALL);
6944 args = code->expr1->value.compcall.actual;
6946 rhsptr = &args->next->expr;
6948 tbp = code->expr1->value.compcall.tbp;
6949 gcc_assert (!tbp->is_generic);
6950 assign_proc = tbp->u.specific->n.sym;
6953 /* Make a temporary rhs when there is a default initializer
6954 and rhs is the same symbol as the lhs. */
6955 if ((*rhsptr)->expr_type == EXPR_VARIABLE
6956 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
6957 && has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
6958 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
6959 *rhsptr = gfc_get_parentheses (*rhsptr);
6961 resolve_code (code, ns);
6969 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
6970 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
6971 &code->loc) == FAILURE)
6974 /* Handle the case of a BOZ literal on the RHS. */
6975 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
6978 if (gfc_option.warn_surprising)
6979 gfc_warning ("BOZ literal at %L is bitwise transferred "
6980 "non-integer symbol '%s'", &code->loc,
6981 lhs->symtree->n.sym->name);
6983 if (!gfc_convert_boz (rhs, &lhs->ts))
6985 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
6987 if (rc == ARITH_UNDERFLOW)
6988 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
6989 ". This check can be disabled with the option "
6990 "-fno-range-check", &rhs->where);
6991 else if (rc == ARITH_OVERFLOW)
6992 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
6993 ". This check can be disabled with the option "
6994 "-fno-range-check", &rhs->where);
6995 else if (rc == ARITH_NAN)
6996 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
6997 ". This check can be disabled with the option "
6998 "-fno-range-check", &rhs->where);
7004 if (lhs->ts.type == BT_CHARACTER
7005 && gfc_option.warn_character_truncation)
7007 if (lhs->ts.u.cl != NULL
7008 && lhs->ts.u.cl->length != NULL
7009 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7010 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
7012 if (rhs->expr_type == EXPR_CONSTANT)
7013 rlen = rhs->value.character.length;
7015 else if (rhs->ts.u.cl != NULL
7016 && rhs->ts.u.cl->length != NULL
7017 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7018 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
7020 if (rlen && llen && rlen > llen)
7021 gfc_warning_now ("CHARACTER expression will be truncated "
7022 "in assignment (%d/%d) at %L",
7023 llen, rlen, &code->loc);
7026 /* Ensure that a vector index expression for the lvalue is evaluated
7027 to a temporary if the lvalue symbol is referenced in it. */
7030 for (ref = lhs->ref; ref; ref= ref->next)
7031 if (ref->type == REF_ARRAY)
7033 for (n = 0; n < ref->u.ar.dimen; n++)
7034 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
7035 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
7036 ref->u.ar.start[n]))
7038 = gfc_get_parentheses (ref->u.ar.start[n]);
7042 if (gfc_pure (NULL))
7044 if (gfc_impure_variable (lhs->symtree->n.sym))
7046 gfc_error ("Cannot assign to variable '%s' in PURE "
7048 lhs->symtree->n.sym->name,
7053 if (lhs->ts.type == BT_DERIVED
7054 && lhs->expr_type == EXPR_VARIABLE
7055 && lhs->ts.u.derived->attr.pointer_comp
7056 && gfc_impure_variable (rhs->symtree->n.sym))
7058 gfc_error ("The impure variable at %L is assigned to "
7059 "a derived type variable with a POINTER "
7060 "component in a PURE procedure (12.6)",
7066 gfc_check_assign (lhs, rhs, 1);
7070 /* Given a block of code, recursively resolve everything pointed to by this
7074 resolve_code (gfc_code *code, gfc_namespace *ns)
7076 int omp_workshare_save;
7081 frame.prev = cs_base;
7085 find_reachable_labels (code);
7087 for (; code; code = code->next)
7089 frame.current = code;
7090 forall_save = forall_flag;
7092 if (code->op == EXEC_FORALL)
7095 gfc_resolve_forall (code, ns, forall_save);
7098 else if (code->block)
7100 omp_workshare_save = -1;
7103 case EXEC_OMP_PARALLEL_WORKSHARE:
7104 omp_workshare_save = omp_workshare_flag;
7105 omp_workshare_flag = 1;
7106 gfc_resolve_omp_parallel_blocks (code, ns);
7108 case EXEC_OMP_PARALLEL:
7109 case EXEC_OMP_PARALLEL_DO:
7110 case EXEC_OMP_PARALLEL_SECTIONS:
7112 omp_workshare_save = omp_workshare_flag;
7113 omp_workshare_flag = 0;
7114 gfc_resolve_omp_parallel_blocks (code, ns);
7117 gfc_resolve_omp_do_blocks (code, ns);
7119 case EXEC_OMP_WORKSHARE:
7120 omp_workshare_save = omp_workshare_flag;
7121 omp_workshare_flag = 1;
7124 gfc_resolve_blocks (code->block, ns);
7128 if (omp_workshare_save != -1)
7129 omp_workshare_flag = omp_workshare_save;
7133 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
7134 t = gfc_resolve_expr (code->expr1);
7135 forall_flag = forall_save;
7137 if (gfc_resolve_expr (code->expr2) == FAILURE)
7143 case EXEC_END_BLOCK:
7150 case EXEC_ASSIGN_CALL:
7154 /* Keep track of which entry we are up to. */
7155 current_entry_id = code->ext.entry->id;
7159 resolve_where (code, NULL);
7163 if (code->expr1 != NULL)
7165 if (code->expr1->ts.type != BT_INTEGER)
7166 gfc_error ("ASSIGNED GOTO statement at %L requires an "
7167 "INTEGER variable", &code->expr1->where);
7168 else if (code->expr1->symtree->n.sym->attr.assign != 1)
7169 gfc_error ("Variable '%s' has not been assigned a target "
7170 "label at %L", code->expr1->symtree->n.sym->name,
7171 &code->expr1->where);
7174 resolve_branch (code->label1, code);
7178 if (code->expr1 != NULL
7179 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
7180 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
7181 "INTEGER return specifier", &code->expr1->where);
7184 case EXEC_INIT_ASSIGN:
7185 case EXEC_END_PROCEDURE:
7192 if (resolve_ordinary_assign (code, ns))
7197 case EXEC_LABEL_ASSIGN:
7198 if (code->label1->defined == ST_LABEL_UNKNOWN)
7199 gfc_error ("Label %d referenced at %L is never defined",
7200 code->label1->value, &code->label1->where);
7202 && (code->expr1->expr_type != EXPR_VARIABLE
7203 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
7204 || code->expr1->symtree->n.sym->ts.kind
7205 != gfc_default_integer_kind
7206 || code->expr1->symtree->n.sym->as != NULL))
7207 gfc_error ("ASSIGN statement at %L requires a scalar "
7208 "default INTEGER variable", &code->expr1->where);
7211 case EXEC_POINTER_ASSIGN:
7215 gfc_check_pointer_assign (code->expr1, code->expr2);
7218 case EXEC_ARITHMETIC_IF:
7220 && code->expr1->ts.type != BT_INTEGER
7221 && code->expr1->ts.type != BT_REAL)
7222 gfc_error ("Arithmetic IF statement at %L requires a numeric "
7223 "expression", &code->expr1->where);
7225 resolve_branch (code->label1, code);
7226 resolve_branch (code->label2, code);
7227 resolve_branch (code->label3, code);
7231 if (t == SUCCESS && code->expr1 != NULL
7232 && (code->expr1->ts.type != BT_LOGICAL
7233 || code->expr1->rank != 0))
7234 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
7235 &code->expr1->where);
7240 resolve_call (code);
7244 resolve_typebound_call (code);
7248 resolve_ppc_call (code);
7252 /* Select is complicated. Also, a SELECT construct could be
7253 a transformed computed GOTO. */
7254 resolve_select (code);
7258 if (code->ext.iterator != NULL)
7260 gfc_iterator *iter = code->ext.iterator;
7261 if (gfc_resolve_iterator (iter, true) != FAILURE)
7262 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
7267 if (code->expr1 == NULL)
7268 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
7270 && (code->expr1->rank != 0
7271 || code->expr1->ts.type != BT_LOGICAL))
7272 gfc_error ("Exit condition of DO WHILE loop at %L must be "
7273 "a scalar LOGICAL expression", &code->expr1->where);
7278 resolve_allocate_deallocate (code, "ALLOCATE");
7282 case EXEC_DEALLOCATE:
7284 resolve_allocate_deallocate (code, "DEALLOCATE");
7289 if (gfc_resolve_open (code->ext.open) == FAILURE)
7292 resolve_branch (code->ext.open->err, code);
7296 if (gfc_resolve_close (code->ext.close) == FAILURE)
7299 resolve_branch (code->ext.close->err, code);
7302 case EXEC_BACKSPACE:
7306 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
7309 resolve_branch (code->ext.filepos->err, code);
7313 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
7316 resolve_branch (code->ext.inquire->err, code);
7320 gcc_assert (code->ext.inquire != NULL);
7321 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
7324 resolve_branch (code->ext.inquire->err, code);
7328 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
7331 resolve_branch (code->ext.wait->err, code);
7332 resolve_branch (code->ext.wait->end, code);
7333 resolve_branch (code->ext.wait->eor, code);
7338 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
7341 resolve_branch (code->ext.dt->err, code);
7342 resolve_branch (code->ext.dt->end, code);
7343 resolve_branch (code->ext.dt->eor, code);
7347 resolve_transfer (code);
7351 resolve_forall_iterators (code->ext.forall_iterator);
7353 if (code->expr1 != NULL && code->expr1->ts.type != BT_LOGICAL)
7354 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
7355 "expression", &code->expr1->where);
7358 case EXEC_OMP_ATOMIC:
7359 case EXEC_OMP_BARRIER:
7360 case EXEC_OMP_CRITICAL:
7361 case EXEC_OMP_FLUSH:
7363 case EXEC_OMP_MASTER:
7364 case EXEC_OMP_ORDERED:
7365 case EXEC_OMP_SECTIONS:
7366 case EXEC_OMP_SINGLE:
7367 case EXEC_OMP_TASKWAIT:
7368 case EXEC_OMP_WORKSHARE:
7369 gfc_resolve_omp_directive (code, ns);
7372 case EXEC_OMP_PARALLEL:
7373 case EXEC_OMP_PARALLEL_DO:
7374 case EXEC_OMP_PARALLEL_SECTIONS:
7375 case EXEC_OMP_PARALLEL_WORKSHARE:
7377 omp_workshare_save = omp_workshare_flag;
7378 omp_workshare_flag = 0;
7379 gfc_resolve_omp_directive (code, ns);
7380 omp_workshare_flag = omp_workshare_save;
7384 gfc_internal_error ("resolve_code(): Bad statement code");
7388 cs_base = frame.prev;
7392 /* Resolve initial values and make sure they are compatible with
7396 resolve_values (gfc_symbol *sym)
7398 if (sym->value == NULL)
7401 if (gfc_resolve_expr (sym->value) == FAILURE)
7404 gfc_check_assign_symbol (sym, sym->value);
7408 /* Verify the binding labels for common blocks that are BIND(C). The label
7409 for a BIND(C) common block must be identical in all scoping units in which
7410 the common block is declared. Further, the binding label can not collide
7411 with any other global entity in the program. */
7414 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
7416 if (comm_block_tree->n.common->is_bind_c == 1)
7418 gfc_gsymbol *binding_label_gsym;
7419 gfc_gsymbol *comm_name_gsym;
7421 /* See if a global symbol exists by the common block's name. It may
7422 be NULL if the common block is use-associated. */
7423 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
7424 comm_block_tree->n.common->name);
7425 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
7426 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
7427 "with the global entity '%s' at %L",
7428 comm_block_tree->n.common->binding_label,
7429 comm_block_tree->n.common->name,
7430 &(comm_block_tree->n.common->where),
7431 comm_name_gsym->name, &(comm_name_gsym->where));
7432 else if (comm_name_gsym != NULL
7433 && strcmp (comm_name_gsym->name,
7434 comm_block_tree->n.common->name) == 0)
7436 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
7438 if (comm_name_gsym->binding_label == NULL)
7439 /* No binding label for common block stored yet; save this one. */
7440 comm_name_gsym->binding_label =
7441 comm_block_tree->n.common->binding_label;
7443 if (strcmp (comm_name_gsym->binding_label,
7444 comm_block_tree->n.common->binding_label) != 0)
7446 /* Common block names match but binding labels do not. */
7447 gfc_error ("Binding label '%s' for common block '%s' at %L "
7448 "does not match the binding label '%s' for common "
7450 comm_block_tree->n.common->binding_label,
7451 comm_block_tree->n.common->name,
7452 &(comm_block_tree->n.common->where),
7453 comm_name_gsym->binding_label,
7454 comm_name_gsym->name,
7455 &(comm_name_gsym->where));
7460 /* There is no binding label (NAME="") so we have nothing further to
7461 check and nothing to add as a global symbol for the label. */
7462 if (comm_block_tree->n.common->binding_label[0] == '\0' )
7465 binding_label_gsym =
7466 gfc_find_gsymbol (gfc_gsym_root,
7467 comm_block_tree->n.common->binding_label);
7468 if (binding_label_gsym == NULL)
7470 /* Need to make a global symbol for the binding label to prevent
7471 it from colliding with another. */
7472 binding_label_gsym =
7473 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
7474 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
7475 binding_label_gsym->type = GSYM_COMMON;
7479 /* If comm_name_gsym is NULL, the name common block is use
7480 associated and the name could be colliding. */
7481 if (binding_label_gsym->type != GSYM_COMMON)
7482 gfc_error ("Binding label '%s' for common block '%s' at %L "
7483 "collides with the global entity '%s' at %L",
7484 comm_block_tree->n.common->binding_label,
7485 comm_block_tree->n.common->name,
7486 &(comm_block_tree->n.common->where),
7487 binding_label_gsym->name,
7488 &(binding_label_gsym->where));
7489 else if (comm_name_gsym != NULL
7490 && (strcmp (binding_label_gsym->name,
7491 comm_name_gsym->binding_label) != 0)
7492 && (strcmp (binding_label_gsym->sym_name,
7493 comm_name_gsym->name) != 0))
7494 gfc_error ("Binding label '%s' for common block '%s' at %L "
7495 "collides with global entity '%s' at %L",
7496 binding_label_gsym->name, binding_label_gsym->sym_name,
7497 &(comm_block_tree->n.common->where),
7498 comm_name_gsym->name, &(comm_name_gsym->where));
7506 /* Verify any BIND(C) derived types in the namespace so we can report errors
7507 for them once, rather than for each variable declared of that type. */
7510 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
7512 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
7513 && derived_sym->attr.is_bind_c == 1)
7514 verify_bind_c_derived_type (derived_sym);
7520 /* Verify that any binding labels used in a given namespace do not collide
7521 with the names or binding labels of any global symbols. */
7524 gfc_verify_binding_labels (gfc_symbol *sym)
7528 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
7529 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
7531 gfc_gsymbol *bind_c_sym;
7533 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
7534 if (bind_c_sym != NULL
7535 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
7537 if (sym->attr.if_source == IFSRC_DECL
7538 && (bind_c_sym->type != GSYM_SUBROUTINE
7539 && bind_c_sym->type != GSYM_FUNCTION)
7540 && ((sym->attr.contained == 1
7541 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
7542 || (sym->attr.use_assoc == 1
7543 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
7545 /* Make sure global procedures don't collide with anything. */
7546 gfc_error ("Binding label '%s' at %L collides with the global "
7547 "entity '%s' at %L", sym->binding_label,
7548 &(sym->declared_at), bind_c_sym->name,
7549 &(bind_c_sym->where));
7552 else if (sym->attr.contained == 0
7553 && (sym->attr.if_source == IFSRC_IFBODY
7554 && sym->attr.flavor == FL_PROCEDURE)
7555 && (bind_c_sym->sym_name != NULL
7556 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
7558 /* Make sure procedures in interface bodies don't collide. */
7559 gfc_error ("Binding label '%s' in interface body at %L collides "
7560 "with the global entity '%s' at %L",
7562 &(sym->declared_at), bind_c_sym->name,
7563 &(bind_c_sym->where));
7566 else if (sym->attr.contained == 0
7567 && sym->attr.if_source == IFSRC_UNKNOWN)
7568 if ((sym->attr.use_assoc && bind_c_sym->mod_name
7569 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
7570 || sym->attr.use_assoc == 0)
7572 gfc_error ("Binding label '%s' at %L collides with global "
7573 "entity '%s' at %L", sym->binding_label,
7574 &(sym->declared_at), bind_c_sym->name,
7575 &(bind_c_sym->where));
7580 /* Clear the binding label to prevent checking multiple times. */
7581 sym->binding_label[0] = '\0';
7583 else if (bind_c_sym == NULL)
7585 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
7586 bind_c_sym->where = sym->declared_at;
7587 bind_c_sym->sym_name = sym->name;
7589 if (sym->attr.use_assoc == 1)
7590 bind_c_sym->mod_name = sym->module;
7592 if (sym->ns->proc_name != NULL)
7593 bind_c_sym->mod_name = sym->ns->proc_name->name;
7595 if (sym->attr.contained == 0)
7597 if (sym->attr.subroutine)
7598 bind_c_sym->type = GSYM_SUBROUTINE;
7599 else if (sym->attr.function)
7600 bind_c_sym->type = GSYM_FUNCTION;
7608 /* Resolve an index expression. */
7611 resolve_index_expr (gfc_expr *e)
7613 if (gfc_resolve_expr (e) == FAILURE)
7616 if (gfc_simplify_expr (e, 0) == FAILURE)
7619 if (gfc_specification_expr (e) == FAILURE)
7625 /* Resolve a charlen structure. */
7628 resolve_charlen (gfc_charlen *cl)
7637 specification_expr = 1;
7639 if (resolve_index_expr (cl->length) == FAILURE)
7641 specification_expr = 0;
7645 /* "If the character length parameter value evaluates to a negative
7646 value, the length of character entities declared is zero." */
7647 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
7649 gfc_warning_now ("CHARACTER variable has zero length at %L",
7650 &cl->length->where);
7651 gfc_replace_expr (cl->length, gfc_int_expr (0));
7654 /* Check that the character length is not too large. */
7655 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
7656 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
7657 && cl->length->ts.type == BT_INTEGER
7658 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
7660 gfc_error ("String length at %L is too large", &cl->length->where);
7668 /* Test for non-constant shape arrays. */
7671 is_non_constant_shape_array (gfc_symbol *sym)
7677 not_constant = false;
7678 if (sym->as != NULL)
7680 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
7681 has not been simplified; parameter array references. Do the
7682 simplification now. */
7683 for (i = 0; i < sym->as->rank; i++)
7685 e = sym->as->lower[i];
7686 if (e && (resolve_index_expr (e) == FAILURE
7687 || !gfc_is_constant_expr (e)))
7688 not_constant = true;
7690 e = sym->as->upper[i];
7691 if (e && (resolve_index_expr (e) == FAILURE
7692 || !gfc_is_constant_expr (e)))
7693 not_constant = true;
7696 return not_constant;
7699 /* Given a symbol and an initialization expression, add code to initialize
7700 the symbol to the function entry. */
7702 build_init_assign (gfc_symbol *sym, gfc_expr *init)
7706 gfc_namespace *ns = sym->ns;
7708 /* Search for the function namespace if this is a contained
7709 function without an explicit result. */
7710 if (sym->attr.function && sym == sym->result
7711 && sym->name != sym->ns->proc_name->name)
7714 for (;ns; ns = ns->sibling)
7715 if (strcmp (ns->proc_name->name, sym->name) == 0)
7721 gfc_free_expr (init);
7725 /* Build an l-value expression for the result. */
7726 lval = gfc_lval_expr_from_sym (sym);
7728 /* Add the code at scope entry. */
7729 init_st = gfc_get_code ();
7730 init_st->next = ns->code;
7733 /* Assign the default initializer to the l-value. */
7734 init_st->loc = sym->declared_at;
7735 init_st->op = EXEC_INIT_ASSIGN;
7736 init_st->expr1 = lval;
7737 init_st->expr2 = init;
7740 /* Assign the default initializer to a derived type variable or result. */
7743 apply_default_init (gfc_symbol *sym)
7745 gfc_expr *init = NULL;
7747 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
7750 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
7751 init = gfc_default_initializer (&sym->ts);
7756 build_init_assign (sym, init);
7759 /* Build an initializer for a local integer, real, complex, logical, or
7760 character variable, based on the command line flags finit-local-zero,
7761 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
7762 null if the symbol should not have a default initialization. */
7764 build_default_init_expr (gfc_symbol *sym)
7767 gfc_expr *init_expr;
7770 /* These symbols should never have a default initialization. */
7771 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
7772 || sym->attr.external
7774 || sym->attr.pointer
7775 || sym->attr.in_equivalence
7776 || sym->attr.in_common
7779 || sym->attr.cray_pointee
7780 || sym->attr.cray_pointer)
7783 /* Now we'll try to build an initializer expression. */
7784 init_expr = gfc_get_expr ();
7785 init_expr->expr_type = EXPR_CONSTANT;
7786 init_expr->ts.type = sym->ts.type;
7787 init_expr->ts.kind = sym->ts.kind;
7788 init_expr->where = sym->declared_at;
7790 /* We will only initialize integers, reals, complex, logicals, and
7791 characters, and only if the corresponding command-line flags
7792 were set. Otherwise, we free init_expr and return null. */
7793 switch (sym->ts.type)
7796 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
7797 mpz_init_set_si (init_expr->value.integer,
7798 gfc_option.flag_init_integer_value);
7801 gfc_free_expr (init_expr);
7807 mpfr_init (init_expr->value.real);
7808 switch (gfc_option.flag_init_real)
7810 case GFC_INIT_REAL_SNAN:
7811 init_expr->is_snan = 1;
7813 case GFC_INIT_REAL_NAN:
7814 mpfr_set_nan (init_expr->value.real);
7817 case GFC_INIT_REAL_INF:
7818 mpfr_set_inf (init_expr->value.real, 1);
7821 case GFC_INIT_REAL_NEG_INF:
7822 mpfr_set_inf (init_expr->value.real, -1);
7825 case GFC_INIT_REAL_ZERO:
7826 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
7830 gfc_free_expr (init_expr);
7838 mpc_init2 (init_expr->value.complex, mpfr_get_default_prec());
7840 mpfr_init (init_expr->value.complex.r);
7841 mpfr_init (init_expr->value.complex.i);
7843 switch (gfc_option.flag_init_real)
7845 case GFC_INIT_REAL_SNAN:
7846 init_expr->is_snan = 1;
7848 case GFC_INIT_REAL_NAN:
7849 mpfr_set_nan (mpc_realref (init_expr->value.complex));
7850 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
7853 case GFC_INIT_REAL_INF:
7854 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
7855 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
7858 case GFC_INIT_REAL_NEG_INF:
7859 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
7860 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
7863 case GFC_INIT_REAL_ZERO:
7865 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
7867 mpfr_set_ui (init_expr->value.complex.r, 0.0, GFC_RND_MODE);
7868 mpfr_set_ui (init_expr->value.complex.i, 0.0, GFC_RND_MODE);
7873 gfc_free_expr (init_expr);
7880 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
7881 init_expr->value.logical = 0;
7882 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
7883 init_expr->value.logical = 1;
7886 gfc_free_expr (init_expr);
7892 /* For characters, the length must be constant in order to
7893 create a default initializer. */
7894 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
7895 && sym->ts.u.cl->length
7896 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7898 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
7899 init_expr->value.character.length = char_len;
7900 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
7901 for (i = 0; i < char_len; i++)
7902 init_expr->value.character.string[i]
7903 = (unsigned char) gfc_option.flag_init_character_value;
7907 gfc_free_expr (init_expr);
7913 gfc_free_expr (init_expr);
7919 /* Add an initialization expression to a local variable. */
7921 apply_default_init_local (gfc_symbol *sym)
7923 gfc_expr *init = NULL;
7925 /* The symbol should be a variable or a function return value. */
7926 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
7927 || (sym->attr.function && sym->result != sym))
7930 /* Try to build the initializer expression. If we can't initialize
7931 this symbol, then init will be NULL. */
7932 init = build_default_init_expr (sym);
7936 /* For saved variables, we don't want to add an initializer at
7937 function entry, so we just add a static initializer. */
7938 if (sym->attr.save || sym->ns->save_all)
7940 /* Don't clobber an existing initializer! */
7941 gcc_assert (sym->value == NULL);
7946 build_init_assign (sym, init);
7949 /* Resolution of common features of flavors variable and procedure. */
7952 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
7954 /* Constraints on deferred shape variable. */
7955 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
7957 if (sym->attr.allocatable)
7959 if (sym->attr.dimension)
7961 gfc_error ("Allocatable array '%s' at %L must have "
7962 "a deferred shape", sym->name, &sym->declared_at);
7965 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
7966 "may not be ALLOCATABLE", sym->name,
7967 &sym->declared_at) == FAILURE)
7971 if (sym->attr.pointer && sym->attr.dimension)
7973 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
7974 sym->name, &sym->declared_at);
7981 if (!mp_flag && !sym->attr.allocatable
7982 && !sym->attr.pointer && !sym->attr.dummy)
7984 gfc_error ("Array '%s' at %L cannot have a deferred shape",
7985 sym->name, &sym->declared_at);
7993 /* Check if a derived type is extensible. */
7996 type_is_extensible (gfc_symbol *sym)
7998 return !(sym->attr.is_bind_c || sym->attr.sequence);
8002 /* Additional checks for symbols with flavor variable and derived
8003 type. To be called from resolve_fl_variable. */
8006 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
8008 gcc_assert (sym->ts.type == BT_DERIVED);
8010 /* Check to see if a derived type is blocked from being host
8011 associated by the presence of another class I symbol in the same
8012 namespace. 14.6.1.3 of the standard and the discussion on
8013 comp.lang.fortran. */
8014 if (sym->ns != sym->ts.u.derived->ns
8015 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
8018 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
8019 if (s && s->attr.flavor != FL_DERIVED)
8021 gfc_error ("The type '%s' cannot be host associated at %L "
8022 "because it is blocked by an incompatible object "
8023 "of the same name declared at %L",
8024 sym->ts.u.derived->name, &sym->declared_at,
8030 /* 4th constraint in section 11.3: "If an object of a type for which
8031 component-initialization is specified (R429) appears in the
8032 specification-part of a module and does not have the ALLOCATABLE
8033 or POINTER attribute, the object shall have the SAVE attribute."
8035 The check for initializers is performed with
8036 has_default_initializer because gfc_default_initializer generates
8037 a hidden default for allocatable components. */
8038 if (!(sym->value || no_init_flag) && sym->ns->proc_name
8039 && sym->ns->proc_name->attr.flavor == FL_MODULE
8040 && !sym->ns->save_all && !sym->attr.save
8041 && !sym->attr.pointer && !sym->attr.allocatable
8042 && has_default_initializer (sym->ts.u.derived))
8044 gfc_error("Object '%s' at %L must have the SAVE attribute for "
8045 "default initialization of a component",
8046 sym->name, &sym->declared_at);
8050 if (sym->ts.is_class)
8053 if (!type_is_extensible (sym->ts.u.derived))
8055 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
8056 sym->ts.u.derived->name, sym->name, &sym->declared_at);
8061 if (!(sym->attr.dummy || sym->attr.allocatable || sym->attr.pointer))
8063 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
8064 "or pointer", sym->name, &sym->declared_at);
8069 /* Assign default initializer. */
8070 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
8071 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
8073 sym->value = gfc_default_initializer (&sym->ts);
8080 /* Resolve symbols with flavor variable. */
8083 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
8085 int no_init_flag, automatic_flag;
8087 const char *auto_save_msg;
8089 auto_save_msg = "Automatic object '%s' at %L cannot have the "
8092 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
8095 /* Set this flag to check that variables are parameters of all entries.
8096 This check is effected by the call to gfc_resolve_expr through
8097 is_non_constant_shape_array. */
8098 specification_expr = 1;
8100 if (sym->ns->proc_name
8101 && (sym->ns->proc_name->attr.flavor == FL_MODULE
8102 || sym->ns->proc_name->attr.is_main_program)
8103 && !sym->attr.use_assoc
8104 && !sym->attr.allocatable
8105 && !sym->attr.pointer
8106 && is_non_constant_shape_array (sym))
8108 /* The shape of a main program or module array needs to be
8110 gfc_error ("The module or main program array '%s' at %L must "
8111 "have constant shape", sym->name, &sym->declared_at);
8112 specification_expr = 0;
8116 if (sym->ts.type == BT_CHARACTER)
8118 /* Make sure that character string variables with assumed length are
8120 e = sym->ts.u.cl->length;
8121 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
8123 gfc_error ("Entity with assumed character length at %L must be a "
8124 "dummy argument or a PARAMETER", &sym->declared_at);
8128 if (e && sym->attr.save && !gfc_is_constant_expr (e))
8130 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
8134 if (!gfc_is_constant_expr (e)
8135 && !(e->expr_type == EXPR_VARIABLE
8136 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
8137 && sym->ns->proc_name
8138 && (sym->ns->proc_name->attr.flavor == FL_MODULE
8139 || sym->ns->proc_name->attr.is_main_program)
8140 && !sym->attr.use_assoc)
8142 gfc_error ("'%s' at %L must have constant character length "
8143 "in this context", sym->name, &sym->declared_at);
8148 if (sym->value == NULL && sym->attr.referenced)
8149 apply_default_init_local (sym); /* Try to apply a default initialization. */
8151 /* Determine if the symbol may not have an initializer. */
8152 no_init_flag = automatic_flag = 0;
8153 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
8154 || sym->attr.intrinsic || sym->attr.result)
8156 else if (sym->attr.dimension && !sym->attr.pointer
8157 && is_non_constant_shape_array (sym))
8159 no_init_flag = automatic_flag = 1;
8161 /* Also, they must not have the SAVE attribute.
8162 SAVE_IMPLICIT is checked below. */
8163 if (sym->attr.save == SAVE_EXPLICIT)
8165 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
8170 /* Ensure that any initializer is simplified. */
8172 gfc_simplify_expr (sym->value, 1);
8174 /* Reject illegal initializers. */
8175 if (!sym->mark && sym->value)
8177 if (sym->attr.allocatable)
8178 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
8179 sym->name, &sym->declared_at);
8180 else if (sym->attr.external)
8181 gfc_error ("External '%s' at %L cannot have an initializer",
8182 sym->name, &sym->declared_at);
8183 else if (sym->attr.dummy
8184 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
8185 gfc_error ("Dummy '%s' at %L cannot have an initializer",
8186 sym->name, &sym->declared_at);
8187 else if (sym->attr.intrinsic)
8188 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
8189 sym->name, &sym->declared_at);
8190 else if (sym->attr.result)
8191 gfc_error ("Function result '%s' at %L cannot have an initializer",
8192 sym->name, &sym->declared_at);
8193 else if (automatic_flag)
8194 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
8195 sym->name, &sym->declared_at);
8202 if (sym->ts.type == BT_DERIVED)
8203 return resolve_fl_variable_derived (sym, no_init_flag);
8209 /* Resolve a procedure. */
8212 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
8214 gfc_formal_arglist *arg;
8216 if (sym->attr.ambiguous_interfaces && !sym->attr.referenced)
8217 gfc_warning ("Although not referenced, '%s' at %L has ambiguous "
8218 "interfaces", sym->name, &sym->declared_at);
8220 if (sym->attr.function
8221 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
8224 if (sym->ts.type == BT_CHARACTER)
8226 gfc_charlen *cl = sym->ts.u.cl;
8228 if (cl && cl->length && gfc_is_constant_expr (cl->length)
8229 && resolve_charlen (cl) == FAILURE)
8232 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
8234 if (sym->attr.proc == PROC_ST_FUNCTION)
8236 gfc_error ("Character-valued statement function '%s' at %L must "
8237 "have constant length", sym->name, &sym->declared_at);
8241 if (sym->attr.external && sym->formal == NULL
8242 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
8244 gfc_error ("Automatic character length function '%s' at %L must "
8245 "have an explicit interface", sym->name,
8252 /* Ensure that derived type for are not of a private type. Internal
8253 module procedures are excluded by 2.2.3.3 - i.e., they are not
8254 externally accessible and can access all the objects accessible in
8256 if (!(sym->ns->parent
8257 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
8258 && gfc_check_access(sym->attr.access, sym->ns->default_access))
8260 gfc_interface *iface;
8262 for (arg = sym->formal; arg; arg = arg->next)
8265 && arg->sym->ts.type == BT_DERIVED
8266 && !arg->sym->ts.u.derived->attr.use_assoc
8267 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
8268 arg->sym->ts.u.derived->ns->default_access)
8269 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
8270 "PRIVATE type and cannot be a dummy argument"
8271 " of '%s', which is PUBLIC at %L",
8272 arg->sym->name, sym->name, &sym->declared_at)
8275 /* Stop this message from recurring. */
8276 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
8281 /* PUBLIC interfaces may expose PRIVATE procedures that take types
8282 PRIVATE to the containing module. */
8283 for (iface = sym->generic; iface; iface = iface->next)
8285 for (arg = iface->sym->formal; arg; arg = arg->next)
8288 && arg->sym->ts.type == BT_DERIVED
8289 && !arg->sym->ts.u.derived->attr.use_assoc
8290 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
8291 arg->sym->ts.u.derived->ns->default_access)
8292 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
8293 "'%s' in PUBLIC interface '%s' at %L "
8294 "takes dummy arguments of '%s' which is "
8295 "PRIVATE", iface->sym->name, sym->name,
8296 &iface->sym->declared_at,
8297 gfc_typename (&arg->sym->ts)) == FAILURE)
8299 /* Stop this message from recurring. */
8300 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
8306 /* PUBLIC interfaces may expose PRIVATE procedures that take types
8307 PRIVATE to the containing module. */
8308 for (iface = sym->generic; iface; iface = iface->next)
8310 for (arg = iface->sym->formal; arg; arg = arg->next)
8313 && arg->sym->ts.type == BT_DERIVED
8314 && !arg->sym->ts.u.derived->attr.use_assoc
8315 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
8316 arg->sym->ts.u.derived->ns->default_access)
8317 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
8318 "'%s' in PUBLIC interface '%s' at %L "
8319 "takes dummy arguments of '%s' which is "
8320 "PRIVATE", iface->sym->name, sym->name,
8321 &iface->sym->declared_at,
8322 gfc_typename (&arg->sym->ts)) == FAILURE)
8324 /* Stop this message from recurring. */
8325 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
8332 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
8333 && !sym->attr.proc_pointer)
8335 gfc_error ("Function '%s' at %L cannot have an initializer",
8336 sym->name, &sym->declared_at);
8340 /* An external symbol may not have an initializer because it is taken to be
8341 a procedure. Exception: Procedure Pointers. */
8342 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
8344 gfc_error ("External object '%s' at %L may not have an initializer",
8345 sym->name, &sym->declared_at);
8349 /* An elemental function is required to return a scalar 12.7.1 */
8350 if (sym->attr.elemental && sym->attr.function && sym->as)
8352 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
8353 "result", sym->name, &sym->declared_at);
8354 /* Reset so that the error only occurs once. */
8355 sym->attr.elemental = 0;
8359 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
8360 char-len-param shall not be array-valued, pointer-valued, recursive
8361 or pure. ....snip... A character value of * may only be used in the
8362 following ways: (i) Dummy arg of procedure - dummy associates with
8363 actual length; (ii) To declare a named constant; or (iii) External
8364 function - but length must be declared in calling scoping unit. */
8365 if (sym->attr.function
8366 && sym->ts.type == BT_CHARACTER
8367 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
8369 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
8370 || (sym->attr.recursive) || (sym->attr.pure))
8372 if (sym->as && sym->as->rank)
8373 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8374 "array-valued", sym->name, &sym->declared_at);
8376 if (sym->attr.pointer)
8377 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8378 "pointer-valued", sym->name, &sym->declared_at);
8381 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8382 "pure", sym->name, &sym->declared_at);
8384 if (sym->attr.recursive)
8385 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8386 "recursive", sym->name, &sym->declared_at);
8391 /* Appendix B.2 of the standard. Contained functions give an
8392 error anyway. Fixed-form is likely to be F77/legacy. */
8393 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
8394 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
8395 "CHARACTER(*) function '%s' at %L",
8396 sym->name, &sym->declared_at);
8399 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
8401 gfc_formal_arglist *curr_arg;
8402 int has_non_interop_arg = 0;
8404 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
8405 sym->common_block) == FAILURE)
8407 /* Clear these to prevent looking at them again if there was an
8409 sym->attr.is_bind_c = 0;
8410 sym->attr.is_c_interop = 0;
8411 sym->ts.is_c_interop = 0;
8415 /* So far, no errors have been found. */
8416 sym->attr.is_c_interop = 1;
8417 sym->ts.is_c_interop = 1;
8420 curr_arg = sym->formal;
8421 while (curr_arg != NULL)
8423 /* Skip implicitly typed dummy args here. */
8424 if (curr_arg->sym->attr.implicit_type == 0)
8425 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
8426 /* If something is found to fail, record the fact so we
8427 can mark the symbol for the procedure as not being
8428 BIND(C) to try and prevent multiple errors being
8430 has_non_interop_arg = 1;
8432 curr_arg = curr_arg->next;
8435 /* See if any of the arguments were not interoperable and if so, clear
8436 the procedure symbol to prevent duplicate error messages. */
8437 if (has_non_interop_arg != 0)
8439 sym->attr.is_c_interop = 0;
8440 sym->ts.is_c_interop = 0;
8441 sym->attr.is_bind_c = 0;
8445 if (!sym->attr.proc_pointer)
8447 if (sym->attr.save == SAVE_EXPLICIT)
8449 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
8450 "in '%s' at %L", sym->name, &sym->declared_at);
8453 if (sym->attr.intent)
8455 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
8456 "in '%s' at %L", sym->name, &sym->declared_at);
8459 if (sym->attr.subroutine && sym->attr.result)
8461 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
8462 "in '%s' at %L", sym->name, &sym->declared_at);
8465 if (sym->attr.external && sym->attr.function
8466 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
8467 || sym->attr.contained))
8469 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
8470 "in '%s' at %L", sym->name, &sym->declared_at);
8473 if (strcmp ("ppr@", sym->name) == 0)
8475 gfc_error ("Procedure pointer result '%s' at %L "
8476 "is missing the pointer attribute",
8477 sym->ns->proc_name->name, &sym->declared_at);
8486 /* Resolve a list of finalizer procedures. That is, after they have hopefully
8487 been defined and we now know their defined arguments, check that they fulfill
8488 the requirements of the standard for procedures used as finalizers. */
8491 gfc_resolve_finalizers (gfc_symbol* derived)
8493 gfc_finalizer* list;
8494 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
8495 gfc_try result = SUCCESS;
8496 bool seen_scalar = false;
8498 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
8501 /* Walk over the list of finalizer-procedures, check them, and if any one
8502 does not fit in with the standard's definition, print an error and remove
8503 it from the list. */
8504 prev_link = &derived->f2k_derived->finalizers;
8505 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
8511 /* Skip this finalizer if we already resolved it. */
8512 if (list->proc_tree)
8514 prev_link = &(list->next);
8518 /* Check this exists and is a SUBROUTINE. */
8519 if (!list->proc_sym->attr.subroutine)
8521 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
8522 list->proc_sym->name, &list->where);
8526 /* We should have exactly one argument. */
8527 if (!list->proc_sym->formal || list->proc_sym->formal->next)
8529 gfc_error ("FINAL procedure at %L must have exactly one argument",
8533 arg = list->proc_sym->formal->sym;
8535 /* This argument must be of our type. */
8536 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
8538 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
8539 &arg->declared_at, derived->name);
8543 /* It must neither be a pointer nor allocatable nor optional. */
8544 if (arg->attr.pointer)
8546 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
8550 if (arg->attr.allocatable)
8552 gfc_error ("Argument of FINAL procedure at %L must not be"
8553 " ALLOCATABLE", &arg->declared_at);
8556 if (arg->attr.optional)
8558 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
8563 /* It must not be INTENT(OUT). */
8564 if (arg->attr.intent == INTENT_OUT)
8566 gfc_error ("Argument of FINAL procedure at %L must not be"
8567 " INTENT(OUT)", &arg->declared_at);
8571 /* Warn if the procedure is non-scalar and not assumed shape. */
8572 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
8573 && arg->as->type != AS_ASSUMED_SHAPE)
8574 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
8575 " shape argument", &arg->declared_at);
8577 /* Check that it does not match in kind and rank with a FINAL procedure
8578 defined earlier. To really loop over the *earlier* declarations,
8579 we need to walk the tail of the list as new ones were pushed at the
8581 /* TODO: Handle kind parameters once they are implemented. */
8582 my_rank = (arg->as ? arg->as->rank : 0);
8583 for (i = list->next; i; i = i->next)
8585 /* Argument list might be empty; that is an error signalled earlier,
8586 but we nevertheless continued resolving. */
8587 if (i->proc_sym->formal)
8589 gfc_symbol* i_arg = i->proc_sym->formal->sym;
8590 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
8591 if (i_rank == my_rank)
8593 gfc_error ("FINAL procedure '%s' declared at %L has the same"
8594 " rank (%d) as '%s'",
8595 list->proc_sym->name, &list->where, my_rank,
8602 /* Is this the/a scalar finalizer procedure? */
8603 if (!arg->as || arg->as->rank == 0)
8606 /* Find the symtree for this procedure. */
8607 gcc_assert (!list->proc_tree);
8608 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
8610 prev_link = &list->next;
8613 /* Remove wrong nodes immediately from the list so we don't risk any
8614 troubles in the future when they might fail later expectations. */
8618 *prev_link = list->next;
8619 gfc_free_finalizer (i);
8622 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
8623 were nodes in the list, must have been for arrays. It is surely a good
8624 idea to have a scalar version there if there's something to finalize. */
8625 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
8626 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
8627 " defined at %L, suggest also scalar one",
8628 derived->name, &derived->declared_at);
8630 /* TODO: Remove this error when finalization is finished. */
8631 gfc_error ("Finalization at %L is not yet implemented",
8632 &derived->declared_at);
8638 /* Check that it is ok for the typebound procedure proc to override the
8642 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
8645 const gfc_symbol* proc_target;
8646 const gfc_symbol* old_target;
8647 unsigned proc_pass_arg, old_pass_arg, argpos;
8648 gfc_formal_arglist* proc_formal;
8649 gfc_formal_arglist* old_formal;
8651 /* This procedure should only be called for non-GENERIC proc. */
8652 gcc_assert (!proc->n.tb->is_generic);
8654 /* If the overwritten procedure is GENERIC, this is an error. */
8655 if (old->n.tb->is_generic)
8657 gfc_error ("Can't overwrite GENERIC '%s' at %L",
8658 old->name, &proc->n.tb->where);
8662 where = proc->n.tb->where;
8663 proc_target = proc->n.tb->u.specific->n.sym;
8664 old_target = old->n.tb->u.specific->n.sym;
8666 /* Check that overridden binding is not NON_OVERRIDABLE. */
8667 if (old->n.tb->non_overridable)
8669 gfc_error ("'%s' at %L overrides a procedure binding declared"
8670 " NON_OVERRIDABLE", proc->name, &where);
8674 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
8675 if (!old->n.tb->deferred && proc->n.tb->deferred)
8677 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
8678 " non-DEFERRED binding", proc->name, &where);
8682 /* If the overridden binding is PURE, the overriding must be, too. */
8683 if (old_target->attr.pure && !proc_target->attr.pure)
8685 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
8686 proc->name, &where);
8690 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
8691 is not, the overriding must not be either. */
8692 if (old_target->attr.elemental && !proc_target->attr.elemental)
8694 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
8695 " ELEMENTAL", proc->name, &where);
8698 if (!old_target->attr.elemental && proc_target->attr.elemental)
8700 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
8701 " be ELEMENTAL, either", proc->name, &where);
8705 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
8707 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
8709 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
8710 " SUBROUTINE", proc->name, &where);
8714 /* If the overridden binding is a FUNCTION, the overriding must also be a
8715 FUNCTION and have the same characteristics. */
8716 if (old_target->attr.function)
8718 if (!proc_target->attr.function)
8720 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
8721 " FUNCTION", proc->name, &where);
8725 /* FIXME: Do more comprehensive checking (including, for instance, the
8726 rank and array-shape). */
8727 gcc_assert (proc_target->result && old_target->result);
8728 if (!gfc_compare_types (&proc_target->result->ts,
8729 &old_target->result->ts))
8731 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
8732 " matching result types", proc->name, &where);
8737 /* If the overridden binding is PUBLIC, the overriding one must not be
8739 if (old->n.tb->access == ACCESS_PUBLIC
8740 && proc->n.tb->access == ACCESS_PRIVATE)
8742 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
8743 " PRIVATE", proc->name, &where);
8747 /* Compare the formal argument lists of both procedures. This is also abused
8748 to find the position of the passed-object dummy arguments of both
8749 bindings as at least the overridden one might not yet be resolved and we
8750 need those positions in the check below. */
8751 proc_pass_arg = old_pass_arg = 0;
8752 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
8754 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
8757 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
8758 proc_formal && old_formal;
8759 proc_formal = proc_formal->next, old_formal = old_formal->next)
8761 if (proc->n.tb->pass_arg
8762 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
8763 proc_pass_arg = argpos;
8764 if (old->n.tb->pass_arg
8765 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
8766 old_pass_arg = argpos;
8768 /* Check that the names correspond. */
8769 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
8771 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
8772 " to match the corresponding argument of the overridden"
8773 " procedure", proc_formal->sym->name, proc->name, &where,
8774 old_formal->sym->name);
8778 /* Check that the types correspond if neither is the passed-object
8780 /* FIXME: Do more comprehensive testing here. */
8781 if (proc_pass_arg != argpos && old_pass_arg != argpos
8782 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
8784 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L in"
8785 " in respect to the overridden procedure",
8786 proc_formal->sym->name, proc->name, &where);
8792 if (proc_formal || old_formal)
8794 gfc_error ("'%s' at %L must have the same number of formal arguments as"
8795 " the overridden procedure", proc->name, &where);
8799 /* If the overridden binding is NOPASS, the overriding one must also be
8801 if (old->n.tb->nopass && !proc->n.tb->nopass)
8803 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
8804 " NOPASS", proc->name, &where);
8808 /* If the overridden binding is PASS(x), the overriding one must also be
8809 PASS and the passed-object dummy arguments must correspond. */
8810 if (!old->n.tb->nopass)
8812 if (proc->n.tb->nopass)
8814 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
8815 " PASS", proc->name, &where);
8819 if (proc_pass_arg != old_pass_arg)
8821 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
8822 " the same position as the passed-object dummy argument of"
8823 " the overridden procedure", proc->name, &where);
8832 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
8835 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
8836 const char* generic_name, locus where)
8841 gcc_assert (t1->specific && t2->specific);
8842 gcc_assert (!t1->specific->is_generic);
8843 gcc_assert (!t2->specific->is_generic);
8845 sym1 = t1->specific->u.specific->n.sym;
8846 sym2 = t2->specific->u.specific->n.sym;
8848 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
8849 if (sym1->attr.subroutine != sym2->attr.subroutine
8850 || sym1->attr.function != sym2->attr.function)
8852 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
8853 " GENERIC '%s' at %L",
8854 sym1->name, sym2->name, generic_name, &where);
8858 /* Compare the interfaces. */
8859 if (gfc_compare_interfaces (sym1, sym2, NULL, 1, 0, NULL, 0))
8861 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
8862 sym1->name, sym2->name, generic_name, &where);
8870 /* Worker function for resolving a generic procedure binding; this is used to
8871 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
8873 The difference between those cases is finding possible inherited bindings
8874 that are overridden, as one has to look for them in tb_sym_root,
8875 tb_uop_root or tb_op, respectively. Thus the caller must already find
8876 the super-type and set p->overridden correctly. */
8879 resolve_tb_generic_targets (gfc_symbol* super_type,
8880 gfc_typebound_proc* p, const char* name)
8882 gfc_tbp_generic* target;
8883 gfc_symtree* first_target;
8884 gfc_symtree* inherited;
8886 gcc_assert (p && p->is_generic);
8888 /* Try to find the specific bindings for the symtrees in our target-list. */
8889 gcc_assert (p->u.generic);
8890 for (target = p->u.generic; target; target = target->next)
8891 if (!target->specific)
8893 gfc_typebound_proc* overridden_tbp;
8895 const char* target_name;
8897 target_name = target->specific_st->name;
8899 /* Defined for this type directly. */
8900 if (target->specific_st->n.tb)
8902 target->specific = target->specific_st->n.tb;
8903 goto specific_found;
8906 /* Look for an inherited specific binding. */
8909 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
8914 gcc_assert (inherited->n.tb);
8915 target->specific = inherited->n.tb;
8916 goto specific_found;
8920 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
8921 " at %L", target_name, name, &p->where);
8924 /* Once we've found the specific binding, check it is not ambiguous with
8925 other specifics already found or inherited for the same GENERIC. */
8927 gcc_assert (target->specific);
8929 /* This must really be a specific binding! */
8930 if (target->specific->is_generic)
8932 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
8933 " '%s' is GENERIC, too", name, &p->where, target_name);
8937 /* Check those already resolved on this type directly. */
8938 for (g = p->u.generic; g; g = g->next)
8939 if (g != target && g->specific
8940 && check_generic_tbp_ambiguity (target, g, name, p->where)
8944 /* Check for ambiguity with inherited specific targets. */
8945 for (overridden_tbp = p->overridden; overridden_tbp;
8946 overridden_tbp = overridden_tbp->overridden)
8947 if (overridden_tbp->is_generic)
8949 for (g = overridden_tbp->u.generic; g; g = g->next)
8951 gcc_assert (g->specific);
8952 if (check_generic_tbp_ambiguity (target, g,
8953 name, p->where) == FAILURE)
8959 /* If we attempt to "overwrite" a specific binding, this is an error. */
8960 if (p->overridden && !p->overridden->is_generic)
8962 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
8963 " the same name", name, &p->where);
8967 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
8968 all must have the same attributes here. */
8969 first_target = p->u.generic->specific->u.specific;
8970 gcc_assert (first_target);
8971 p->subroutine = first_target->n.sym->attr.subroutine;
8972 p->function = first_target->n.sym->attr.function;
8978 /* Resolve a GENERIC procedure binding for a derived type. */
8981 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
8983 gfc_symbol* super_type;
8985 /* Find the overridden binding if any. */
8986 st->n.tb->overridden = NULL;
8987 super_type = gfc_get_derived_super_type (derived);
8990 gfc_symtree* overridden;
8991 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
8994 if (overridden && overridden->n.tb)
8995 st->n.tb->overridden = overridden->n.tb;
8998 /* Resolve using worker function. */
8999 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
9003 /* Retrieve the target-procedure of an operator binding and do some checks in
9004 common for intrinsic and user-defined type-bound operators. */
9007 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
9009 gfc_symbol* target_proc;
9011 gcc_assert (target->specific && !target->specific->is_generic);
9012 target_proc = target->specific->u.specific->n.sym;
9013 gcc_assert (target_proc);
9015 /* All operator bindings must have a passed-object dummy argument. */
9016 if (target->specific->nopass)
9018 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
9026 /* Resolve a type-bound intrinsic operator. */
9029 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
9030 gfc_typebound_proc* p)
9032 gfc_symbol* super_type;
9033 gfc_tbp_generic* target;
9035 /* If there's already an error here, do nothing (but don't fail again). */
9039 /* Operators should always be GENERIC bindings. */
9040 gcc_assert (p->is_generic);
9042 /* Look for an overridden binding. */
9043 super_type = gfc_get_derived_super_type (derived);
9044 if (super_type && super_type->f2k_derived)
9045 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
9048 p->overridden = NULL;
9050 /* Resolve general GENERIC properties using worker function. */
9051 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
9054 /* Check the targets to be procedures of correct interface. */
9055 for (target = p->u.generic; target; target = target->next)
9057 gfc_symbol* target_proc;
9059 target_proc = get_checked_tb_operator_target (target, p->where);
9063 if (!gfc_check_operator_interface (target_proc, op, p->where))
9075 /* Resolve a type-bound user operator (tree-walker callback). */
9077 static gfc_symbol* resolve_bindings_derived;
9078 static gfc_try resolve_bindings_result;
9080 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
9083 resolve_typebound_user_op (gfc_symtree* stree)
9085 gfc_symbol* super_type;
9086 gfc_tbp_generic* target;
9088 gcc_assert (stree && stree->n.tb);
9090 if (stree->n.tb->error)
9093 /* Operators should always be GENERIC bindings. */
9094 gcc_assert (stree->n.tb->is_generic);
9096 /* Find overridden procedure, if any. */
9097 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
9098 if (super_type && super_type->f2k_derived)
9100 gfc_symtree* overridden;
9101 overridden = gfc_find_typebound_user_op (super_type, NULL,
9102 stree->name, true, NULL);
9104 if (overridden && overridden->n.tb)
9105 stree->n.tb->overridden = overridden->n.tb;
9108 stree->n.tb->overridden = NULL;
9110 /* Resolve basically using worker function. */
9111 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
9115 /* Check the targets to be functions of correct interface. */
9116 for (target = stree->n.tb->u.generic; target; target = target->next)
9118 gfc_symbol* target_proc;
9120 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
9124 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
9131 resolve_bindings_result = FAILURE;
9132 stree->n.tb->error = 1;
9136 /* Resolve the type-bound procedures for a derived type. */
9139 resolve_typebound_procedure (gfc_symtree* stree)
9144 gfc_symbol* super_type;
9145 gfc_component* comp;
9149 /* Undefined specific symbol from GENERIC target definition. */
9153 if (stree->n.tb->error)
9156 /* If this is a GENERIC binding, use that routine. */
9157 if (stree->n.tb->is_generic)
9159 if (resolve_typebound_generic (resolve_bindings_derived, stree)
9165 /* Get the target-procedure to check it. */
9166 gcc_assert (!stree->n.tb->is_generic);
9167 gcc_assert (stree->n.tb->u.specific);
9168 proc = stree->n.tb->u.specific->n.sym;
9169 where = stree->n.tb->where;
9171 /* Default access should already be resolved from the parser. */
9172 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
9174 /* It should be a module procedure or an external procedure with explicit
9175 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
9176 if ((!proc->attr.subroutine && !proc->attr.function)
9177 || (proc->attr.proc != PROC_MODULE
9178 && proc->attr.if_source != IFSRC_IFBODY)
9179 || (proc->attr.abstract && !stree->n.tb->deferred))
9181 gfc_error ("'%s' must be a module procedure or an external procedure with"
9182 " an explicit interface at %L", proc->name, &where);
9185 stree->n.tb->subroutine = proc->attr.subroutine;
9186 stree->n.tb->function = proc->attr.function;
9188 /* Find the super-type of the current derived type. We could do this once and
9189 store in a global if speed is needed, but as long as not I believe this is
9190 more readable and clearer. */
9191 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
9193 /* If PASS, resolve and check arguments if not already resolved / loaded
9194 from a .mod file. */
9195 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
9197 if (stree->n.tb->pass_arg)
9199 gfc_formal_arglist* i;
9201 /* If an explicit passing argument name is given, walk the arg-list
9205 stree->n.tb->pass_arg_num = 1;
9206 for (i = proc->formal; i; i = i->next)
9208 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
9213 ++stree->n.tb->pass_arg_num;
9218 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
9220 proc->name, stree->n.tb->pass_arg, &where,
9221 stree->n.tb->pass_arg);
9227 /* Otherwise, take the first one; there should in fact be at least
9229 stree->n.tb->pass_arg_num = 1;
9232 gfc_error ("Procedure '%s' with PASS at %L must have at"
9233 " least one argument", proc->name, &where);
9236 me_arg = proc->formal->sym;
9239 /* Now check that the argument-type matches. */
9240 gcc_assert (me_arg);
9241 if (me_arg->ts.type != BT_DERIVED
9242 || me_arg->ts.u.derived != resolve_bindings_derived)
9244 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
9245 " the derived-type '%s'", me_arg->name, proc->name,
9246 me_arg->name, &where, resolve_bindings_derived->name);
9250 if (!me_arg->ts.is_class)
9252 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
9253 " at %L", proc->name, &where);
9258 /* If we are extending some type, check that we don't override a procedure
9259 flagged NON_OVERRIDABLE. */
9260 stree->n.tb->overridden = NULL;
9263 gfc_symtree* overridden;
9264 overridden = gfc_find_typebound_proc (super_type, NULL,
9265 stree->name, true, NULL);
9267 if (overridden && overridden->n.tb)
9268 stree->n.tb->overridden = overridden->n.tb;
9270 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
9274 /* See if there's a name collision with a component directly in this type. */
9275 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
9276 if (!strcmp (comp->name, stree->name))
9278 gfc_error ("Procedure '%s' at %L has the same name as a component of"
9280 stree->name, &where, resolve_bindings_derived->name);
9284 /* Try to find a name collision with an inherited component. */
9285 if (super_type && gfc_find_component (super_type, stree->name, true, true))
9287 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
9288 " component of '%s'",
9289 stree->name, &where, resolve_bindings_derived->name);
9293 stree->n.tb->error = 0;
9297 resolve_bindings_result = FAILURE;
9298 stree->n.tb->error = 1;
9302 resolve_typebound_procedures (gfc_symbol* derived)
9306 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
9309 resolve_bindings_derived = derived;
9310 resolve_bindings_result = SUCCESS;
9312 if (derived->f2k_derived->tb_sym_root)
9313 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
9314 &resolve_typebound_procedure);
9316 if (derived->f2k_derived->tb_uop_root)
9317 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
9318 &resolve_typebound_user_op);
9320 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
9322 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
9323 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
9325 resolve_bindings_result = FAILURE;
9328 return resolve_bindings_result;
9332 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
9333 to give all identical derived types the same backend_decl. */
9335 add_dt_to_dt_list (gfc_symbol *derived)
9337 gfc_dt_list *dt_list;
9339 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
9340 if (derived == dt_list->derived)
9343 if (dt_list == NULL)
9345 dt_list = gfc_get_dt_list ();
9346 dt_list->next = gfc_derived_types;
9347 dt_list->derived = derived;
9348 gfc_derived_types = dt_list;
9353 /* Ensure that a derived-type is really not abstract, meaning that every
9354 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
9357 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
9362 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
9364 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
9367 if (st->n.tb && st->n.tb->deferred)
9369 gfc_symtree* overriding;
9370 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
9371 gcc_assert (overriding && overriding->n.tb);
9372 if (overriding->n.tb->deferred)
9374 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
9375 " '%s' is DEFERRED and not overridden",
9376 sub->name, &sub->declared_at, st->name);
9385 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
9387 /* The algorithm used here is to recursively travel up the ancestry of sub
9388 and for each ancestor-type, check all bindings. If any of them is
9389 DEFERRED, look it up starting from sub and see if the found (overriding)
9390 binding is not DEFERRED.
9391 This is not the most efficient way to do this, but it should be ok and is
9392 clearer than something sophisticated. */
9394 gcc_assert (ancestor && ancestor->attr.abstract && !sub->attr.abstract);
9396 /* Walk bindings of this ancestor. */
9397 if (ancestor->f2k_derived)
9400 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
9405 /* Find next ancestor type and recurse on it. */
9406 ancestor = gfc_get_derived_super_type (ancestor);
9408 return ensure_not_abstract (sub, ancestor);
9414 static void resolve_symbol (gfc_symbol *sym);
9417 /* Resolve the components of a derived type. */
9420 resolve_fl_derived (gfc_symbol *sym)
9422 gfc_symbol* super_type;
9426 super_type = gfc_get_derived_super_type (sym);
9428 /* Ensure the extended type gets resolved before we do. */
9429 if (super_type && resolve_fl_derived (super_type) == FAILURE)
9432 /* An ABSTRACT type must be extensible. */
9433 if (sym->attr.abstract && !type_is_extensible (sym))
9435 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
9436 sym->name, &sym->declared_at);
9440 for (c = sym->components; c != NULL; c = c->next)
9442 if (c->attr.proc_pointer && c->ts.interface)
9444 if (c->ts.interface->attr.procedure)
9445 gfc_error ("Interface '%s', used by procedure pointer component "
9446 "'%s' at %L, is declared in a later PROCEDURE statement",
9447 c->ts.interface->name, c->name, &c->loc);
9449 /* Get the attributes from the interface (now resolved). */
9450 if (c->ts.interface->attr.if_source
9451 || c->ts.interface->attr.intrinsic)
9453 gfc_symbol *ifc = c->ts.interface;
9455 if (ifc->formal && !ifc->formal_ns)
9456 resolve_symbol (ifc);
9458 if (ifc->attr.intrinsic)
9459 resolve_intrinsic (ifc, &ifc->declared_at);
9463 c->ts = ifc->result->ts;
9464 c->attr.allocatable = ifc->result->attr.allocatable;
9465 c->attr.pointer = ifc->result->attr.pointer;
9466 c->attr.dimension = ifc->result->attr.dimension;
9467 c->as = gfc_copy_array_spec (ifc->result->as);
9472 c->attr.allocatable = ifc->attr.allocatable;
9473 c->attr.pointer = ifc->attr.pointer;
9474 c->attr.dimension = ifc->attr.dimension;
9475 c->as = gfc_copy_array_spec (ifc->as);
9477 c->ts.interface = ifc;
9478 c->attr.function = ifc->attr.function;
9479 c->attr.subroutine = ifc->attr.subroutine;
9480 gfc_copy_formal_args_ppc (c, ifc);
9482 c->attr.pure = ifc->attr.pure;
9483 c->attr.elemental = ifc->attr.elemental;
9484 c->attr.recursive = ifc->attr.recursive;
9485 c->attr.always_explicit = ifc->attr.always_explicit;
9486 c->attr.ext_attr |= ifc->attr.ext_attr;
9487 /* Replace symbols in array spec. */
9491 for (i = 0; i < c->as->rank; i++)
9493 gfc_expr_replace_comp (c->as->lower[i], c);
9494 gfc_expr_replace_comp (c->as->upper[i], c);
9497 /* Copy char length. */
9498 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
9500 c->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
9501 gfc_expr_replace_comp (c->ts.u.cl->length, c);
9504 else if (c->ts.interface->name[0] != '\0')
9506 gfc_error ("Interface '%s' of procedure pointer component "
9507 "'%s' at %L must be explicit", c->ts.interface->name,
9512 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
9514 c->ts = *gfc_get_default_type (c->name, NULL);
9515 c->attr.implicit_type = 1;
9518 /* Procedure pointer components: Check PASS arg. */
9519 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0)
9523 if (c->tb->pass_arg)
9525 gfc_formal_arglist* i;
9527 /* If an explicit passing argument name is given, walk the arg-list
9531 c->tb->pass_arg_num = 1;
9532 for (i = c->formal; i; i = i->next)
9534 if (!strcmp (i->sym->name, c->tb->pass_arg))
9539 c->tb->pass_arg_num++;
9544 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
9545 "at %L has no argument '%s'", c->name,
9546 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
9553 /* Otherwise, take the first one; there should in fact be at least
9555 c->tb->pass_arg_num = 1;
9558 gfc_error ("Procedure pointer component '%s' with PASS at %L "
9559 "must have at least one argument",
9564 me_arg = c->formal->sym;
9567 /* Now check that the argument-type matches. */
9568 gcc_assert (me_arg);
9569 if (me_arg->ts.type != BT_DERIVED
9570 || me_arg->ts.u.derived != sym)
9572 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
9573 " the derived type '%s'", me_arg->name, c->name,
9574 me_arg->name, &c->loc, sym->name);
9579 /* Check for C453. */
9580 if (me_arg->attr.dimension)
9582 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
9583 "must be scalar", me_arg->name, c->name, me_arg->name,
9589 if (me_arg->attr.pointer)
9591 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
9592 "may not have the POINTER attribute", me_arg->name,
9593 c->name, me_arg->name, &c->loc);
9598 if (me_arg->attr.allocatable)
9600 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
9601 "may not be ALLOCATABLE", me_arg->name, c->name,
9602 me_arg->name, &c->loc);
9607 if (type_is_extensible (sym) && !me_arg->ts.is_class)
9608 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
9609 " at %L", c->name, &c->loc);
9613 /* Check type-spec if this is not the parent-type component. */
9614 if ((!sym->attr.extension || c != sym->components)
9615 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
9618 /* If this type is an extension, see if this component has the same name
9619 as an inherited type-bound procedure. */
9621 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
9623 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
9624 " inherited type-bound procedure",
9625 c->name, sym->name, &c->loc);
9629 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
9631 if (c->ts.u.cl->length == NULL
9632 || (resolve_charlen (c->ts.u.cl) == FAILURE)
9633 || !gfc_is_constant_expr (c->ts.u.cl->length))
9635 gfc_error ("Character length of component '%s' needs to "
9636 "be a constant specification expression at %L",
9638 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
9643 if (c->ts.type == BT_DERIVED
9644 && sym->component_access != ACCESS_PRIVATE
9645 && gfc_check_access (sym->attr.access, sym->ns->default_access)
9646 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
9647 && !c->ts.u.derived->attr.use_assoc
9648 && !gfc_check_access (c->ts.u.derived->attr.access,
9649 c->ts.u.derived->ns->default_access)
9650 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
9651 "is a PRIVATE type and cannot be a component of "
9652 "'%s', which is PUBLIC at %L", c->name,
9653 sym->name, &sym->declared_at) == FAILURE)
9656 if (sym->attr.sequence)
9658 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
9660 gfc_error ("Component %s of SEQUENCE type declared at %L does "
9661 "not have the SEQUENCE attribute",
9662 c->ts.u.derived->name, &sym->declared_at);
9667 if (c->ts.type == BT_DERIVED && c->attr.pointer
9668 && c->ts.u.derived->components == NULL
9669 && !c->ts.u.derived->attr.zero_comp)
9671 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
9672 "that has not been declared", c->name, sym->name,
9678 if (c->ts.type == BT_DERIVED && c->ts.is_class
9679 && !(c->attr.pointer || c->attr.allocatable))
9681 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
9682 "or pointer", c->name, &c->loc);
9686 /* Ensure that all the derived type components are put on the
9687 derived type list; even in formal namespaces, where derived type
9688 pointer components might not have been declared. */
9689 if (c->ts.type == BT_DERIVED
9691 && c->ts.u.derived->components
9693 && sym != c->ts.u.derived)
9694 add_dt_to_dt_list (c->ts.u.derived);
9696 if (c->attr.pointer || c->attr.proc_pointer || c->attr.allocatable
9700 for (i = 0; i < c->as->rank; i++)
9702 if (c->as->lower[i] == NULL
9703 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
9704 || !gfc_is_constant_expr (c->as->lower[i])
9705 || c->as->upper[i] == NULL
9706 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
9707 || !gfc_is_constant_expr (c->as->upper[i]))
9709 gfc_error ("Component '%s' of '%s' at %L must have "
9710 "constant array bounds",
9711 c->name, sym->name, &c->loc);
9717 /* Resolve the type-bound procedures. */
9718 if (resolve_typebound_procedures (sym) == FAILURE)
9721 /* Resolve the finalizer procedures. */
9722 if (gfc_resolve_finalizers (sym) == FAILURE)
9725 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
9726 all DEFERRED bindings are overridden. */
9727 if (super_type && super_type->attr.abstract && !sym->attr.abstract
9728 && ensure_not_abstract (sym, super_type) == FAILURE)
9731 /* Add derived type to the derived type list. */
9732 add_dt_to_dt_list (sym);
9739 resolve_fl_namelist (gfc_symbol *sym)
9744 /* Reject PRIVATE objects in a PUBLIC namelist. */
9745 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
9747 for (nl = sym->namelist; nl; nl = nl->next)
9749 if (!nl->sym->attr.use_assoc
9750 && !is_sym_host_assoc (nl->sym, sym->ns)
9751 && !gfc_check_access(nl->sym->attr.access,
9752 nl->sym->ns->default_access))
9754 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
9755 "cannot be member of PUBLIC namelist '%s' at %L",
9756 nl->sym->name, sym->name, &sym->declared_at);
9760 /* Types with private components that came here by USE-association. */
9761 if (nl->sym->ts.type == BT_DERIVED
9762 && derived_inaccessible (nl->sym->ts.u.derived))
9764 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
9765 "components and cannot be member of namelist '%s' at %L",
9766 nl->sym->name, sym->name, &sym->declared_at);
9770 /* Types with private components that are defined in the same module. */
9771 if (nl->sym->ts.type == BT_DERIVED
9772 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
9773 && !gfc_check_access (nl->sym->ts.u.derived->attr.private_comp
9774 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
9775 nl->sym->ns->default_access))
9777 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
9778 "cannot be a member of PUBLIC namelist '%s' at %L",
9779 nl->sym->name, sym->name, &sym->declared_at);
9785 for (nl = sym->namelist; nl; nl = nl->next)
9787 /* Reject namelist arrays of assumed shape. */
9788 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
9789 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
9790 "must not have assumed shape in namelist "
9791 "'%s' at %L", nl->sym->name, sym->name,
9792 &sym->declared_at) == FAILURE)
9795 /* Reject namelist arrays that are not constant shape. */
9796 if (is_non_constant_shape_array (nl->sym))
9798 gfc_error ("NAMELIST array object '%s' must have constant "
9799 "shape in namelist '%s' at %L", nl->sym->name,
9800 sym->name, &sym->declared_at);
9804 /* Namelist objects cannot have allocatable or pointer components. */
9805 if (nl->sym->ts.type != BT_DERIVED)
9808 if (nl->sym->ts.u.derived->attr.alloc_comp)
9810 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
9811 "have ALLOCATABLE components",
9812 nl->sym->name, sym->name, &sym->declared_at);
9816 if (nl->sym->ts.u.derived->attr.pointer_comp)
9818 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
9819 "have POINTER components",
9820 nl->sym->name, sym->name, &sym->declared_at);
9826 /* 14.1.2 A module or internal procedure represent local entities
9827 of the same type as a namelist member and so are not allowed. */
9828 for (nl = sym->namelist; nl; nl = nl->next)
9830 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
9833 if (nl->sym->attr.function && nl->sym == nl->sym->result)
9834 if ((nl->sym == sym->ns->proc_name)
9836 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
9840 if (nl->sym && nl->sym->name)
9841 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
9842 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
9844 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
9845 "attribute in '%s' at %L", nlsym->name,
9856 resolve_fl_parameter (gfc_symbol *sym)
9858 /* A parameter array's shape needs to be constant. */
9860 && (sym->as->type == AS_DEFERRED
9861 || is_non_constant_shape_array (sym)))
9863 gfc_error ("Parameter array '%s' at %L cannot be automatic "
9864 "or of deferred shape", sym->name, &sym->declared_at);
9868 /* Make sure a parameter that has been implicitly typed still
9869 matches the implicit type, since PARAMETER statements can precede
9870 IMPLICIT statements. */
9871 if (sym->attr.implicit_type
9872 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
9875 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
9876 "later IMPLICIT type", sym->name, &sym->declared_at);
9880 /* Make sure the types of derived parameters are consistent. This
9881 type checking is deferred until resolution because the type may
9882 refer to a derived type from the host. */
9883 if (sym->ts.type == BT_DERIVED
9884 && !gfc_compare_types (&sym->ts, &sym->value->ts))
9886 gfc_error ("Incompatible derived type in PARAMETER at %L",
9887 &sym->value->where);
9894 /* Do anything necessary to resolve a symbol. Right now, we just
9895 assume that an otherwise unknown symbol is a variable. This sort
9896 of thing commonly happens for symbols in module. */
9899 resolve_symbol (gfc_symbol *sym)
9901 int check_constant, mp_flag;
9902 gfc_symtree *symtree;
9903 gfc_symtree *this_symtree;
9907 if (sym->attr.flavor == FL_UNKNOWN)
9910 /* If we find that a flavorless symbol is an interface in one of the
9911 parent namespaces, find its symtree in this namespace, free the
9912 symbol and set the symtree to point to the interface symbol. */
9913 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
9915 symtree = gfc_find_symtree (ns->sym_root, sym->name);
9916 if (symtree && symtree->n.sym->generic)
9918 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
9922 gfc_free_symbol (sym);
9923 symtree->n.sym->refs++;
9924 this_symtree->n.sym = symtree->n.sym;
9929 /* Otherwise give it a flavor according to such attributes as
9931 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
9932 sym->attr.flavor = FL_VARIABLE;
9935 sym->attr.flavor = FL_PROCEDURE;
9936 if (sym->attr.dimension)
9937 sym->attr.function = 1;
9941 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
9942 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
9944 if (sym->attr.procedure && sym->ts.interface
9945 && sym->attr.if_source != IFSRC_DECL)
9947 if (sym->ts.interface == sym)
9949 gfc_error ("PROCEDURE '%s' at %L may not be used as its own "
9950 "interface", sym->name, &sym->declared_at);
9953 if (sym->ts.interface->attr.procedure)
9955 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared"
9956 " in a later PROCEDURE statement", sym->ts.interface->name,
9957 sym->name,&sym->declared_at);
9961 /* Get the attributes from the interface (now resolved). */
9962 if (sym->ts.interface->attr.if_source
9963 || sym->ts.interface->attr.intrinsic)
9965 gfc_symbol *ifc = sym->ts.interface;
9966 resolve_symbol (ifc);
9968 if (ifc->attr.intrinsic)
9969 resolve_intrinsic (ifc, &ifc->declared_at);
9972 sym->ts = ifc->result->ts;
9975 sym->ts.interface = ifc;
9976 sym->attr.function = ifc->attr.function;
9977 sym->attr.subroutine = ifc->attr.subroutine;
9978 gfc_copy_formal_args (sym, ifc);
9980 sym->attr.allocatable = ifc->attr.allocatable;
9981 sym->attr.pointer = ifc->attr.pointer;
9982 sym->attr.pure = ifc->attr.pure;
9983 sym->attr.elemental = ifc->attr.elemental;
9984 sym->attr.dimension = ifc->attr.dimension;
9985 sym->attr.recursive = ifc->attr.recursive;
9986 sym->attr.always_explicit = ifc->attr.always_explicit;
9987 sym->attr.ext_attr |= ifc->attr.ext_attr;
9988 /* Copy array spec. */
9989 sym->as = gfc_copy_array_spec (ifc->as);
9993 for (i = 0; i < sym->as->rank; i++)
9995 gfc_expr_replace_symbols (sym->as->lower[i], sym);
9996 gfc_expr_replace_symbols (sym->as->upper[i], sym);
9999 /* Copy char length. */
10000 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
10002 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
10003 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
10006 else if (sym->ts.interface->name[0] != '\0')
10008 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
10009 sym->ts.interface->name, sym->name, &sym->declared_at);
10014 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
10017 /* Symbols that are module procedures with results (functions) have
10018 the types and array specification copied for type checking in
10019 procedures that call them, as well as for saving to a module
10020 file. These symbols can't stand the scrutiny that their results
10022 mp_flag = (sym->result != NULL && sym->result != sym);
10025 /* Make sure that the intrinsic is consistent with its internal
10026 representation. This needs to be done before assigning a default
10027 type to avoid spurious warnings. */
10028 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
10029 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
10032 /* Assign default type to symbols that need one and don't have one. */
10033 if (sym->ts.type == BT_UNKNOWN)
10035 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
10036 gfc_set_default_type (sym, 1, NULL);
10038 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
10039 && !sym->attr.function && !sym->attr.subroutine
10040 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
10041 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
10043 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
10045 /* The specific case of an external procedure should emit an error
10046 in the case that there is no implicit type. */
10048 gfc_set_default_type (sym, sym->attr.external, NULL);
10051 /* Result may be in another namespace. */
10052 resolve_symbol (sym->result);
10054 if (!sym->result->attr.proc_pointer)
10056 sym->ts = sym->result->ts;
10057 sym->as = gfc_copy_array_spec (sym->result->as);
10058 sym->attr.dimension = sym->result->attr.dimension;
10059 sym->attr.pointer = sym->result->attr.pointer;
10060 sym->attr.allocatable = sym->result->attr.allocatable;
10066 /* Assumed size arrays and assumed shape arrays must be dummy
10069 if (sym->as != NULL
10070 && (sym->as->type == AS_ASSUMED_SIZE
10071 || sym->as->type == AS_ASSUMED_SHAPE)
10072 && sym->attr.dummy == 0)
10074 if (sym->as->type == AS_ASSUMED_SIZE)
10075 gfc_error ("Assumed size array at %L must be a dummy argument",
10076 &sym->declared_at);
10078 gfc_error ("Assumed shape array at %L must be a dummy argument",
10079 &sym->declared_at);
10083 /* Make sure symbols with known intent or optional are really dummy
10084 variable. Because of ENTRY statement, this has to be deferred
10085 until resolution time. */
10087 if (!sym->attr.dummy
10088 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
10090 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
10094 if (sym->attr.value && !sym->attr.dummy)
10096 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
10097 "it is not a dummy argument", sym->name, &sym->declared_at);
10101 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
10103 gfc_charlen *cl = sym->ts.u.cl;
10104 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10106 gfc_error ("Character dummy variable '%s' at %L with VALUE "
10107 "attribute must have constant length",
10108 sym->name, &sym->declared_at);
10112 if (sym->ts.is_c_interop
10113 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
10115 gfc_error ("C interoperable character dummy variable '%s' at %L "
10116 "with VALUE attribute must have length one",
10117 sym->name, &sym->declared_at);
10122 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
10123 do this for something that was implicitly typed because that is handled
10124 in gfc_set_default_type. Handle dummy arguments and procedure
10125 definitions separately. Also, anything that is use associated is not
10126 handled here but instead is handled in the module it is declared in.
10127 Finally, derived type definitions are allowed to be BIND(C) since that
10128 only implies that they're interoperable, and they are checked fully for
10129 interoperability when a variable is declared of that type. */
10130 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
10131 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
10132 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
10134 gfc_try t = SUCCESS;
10136 /* First, make sure the variable is declared at the
10137 module-level scope (J3/04-007, Section 15.3). */
10138 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
10139 sym->attr.in_common == 0)
10141 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
10142 "is neither a COMMON block nor declared at the "
10143 "module level scope", sym->name, &(sym->declared_at));
10146 else if (sym->common_head != NULL)
10148 t = verify_com_block_vars_c_interop (sym->common_head);
10152 /* If type() declaration, we need to verify that the components
10153 of the given type are all C interoperable, etc. */
10154 if (sym->ts.type == BT_DERIVED &&
10155 sym->ts.u.derived->attr.is_c_interop != 1)
10157 /* Make sure the user marked the derived type as BIND(C). If
10158 not, call the verify routine. This could print an error
10159 for the derived type more than once if multiple variables
10160 of that type are declared. */
10161 if (sym->ts.u.derived->attr.is_bind_c != 1)
10162 verify_bind_c_derived_type (sym->ts.u.derived);
10166 /* Verify the variable itself as C interoperable if it
10167 is BIND(C). It is not possible for this to succeed if
10168 the verify_bind_c_derived_type failed, so don't have to handle
10169 any error returned by verify_bind_c_derived_type. */
10170 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
10171 sym->common_block);
10176 /* clear the is_bind_c flag to prevent reporting errors more than
10177 once if something failed. */
10178 sym->attr.is_bind_c = 0;
10183 /* If a derived type symbol has reached this point, without its
10184 type being declared, we have an error. Notice that most
10185 conditions that produce undefined derived types have already
10186 been dealt with. However, the likes of:
10187 implicit type(t) (t) ..... call foo (t) will get us here if
10188 the type is not declared in the scope of the implicit
10189 statement. Change the type to BT_UNKNOWN, both because it is so
10190 and to prevent an ICE. */
10191 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
10192 && !sym->ts.u.derived->attr.zero_comp)
10194 gfc_error ("The derived type '%s' at %L is of type '%s', "
10195 "which has not been defined", sym->name,
10196 &sym->declared_at, sym->ts.u.derived->name);
10197 sym->ts.type = BT_UNKNOWN;
10201 /* Make sure that the derived type has been resolved and that the
10202 derived type is visible in the symbol's namespace, if it is a
10203 module function and is not PRIVATE. */
10204 if (sym->ts.type == BT_DERIVED
10205 && sym->ts.u.derived->attr.use_assoc
10206 && sym->ns->proc_name
10207 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10211 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
10214 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
10215 if (!ds && sym->attr.function
10216 && gfc_check_access (sym->attr.access, sym->ns->default_access))
10218 symtree = gfc_new_symtree (&sym->ns->sym_root,
10219 sym->ts.u.derived->name);
10220 symtree->n.sym = sym->ts.u.derived;
10221 sym->ts.u.derived->refs++;
10225 /* Unless the derived-type declaration is use associated, Fortran 95
10226 does not allow public entries of private derived types.
10227 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
10228 161 in 95-006r3. */
10229 if (sym->ts.type == BT_DERIVED
10230 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
10231 && !sym->ts.u.derived->attr.use_assoc
10232 && gfc_check_access (sym->attr.access, sym->ns->default_access)
10233 && !gfc_check_access (sym->ts.u.derived->attr.access,
10234 sym->ts.u.derived->ns->default_access)
10235 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
10236 "of PRIVATE derived type '%s'",
10237 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
10238 : "variable", sym->name, &sym->declared_at,
10239 sym->ts.u.derived->name) == FAILURE)
10242 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
10243 default initialization is defined (5.1.2.4.4). */
10244 if (sym->ts.type == BT_DERIVED
10246 && sym->attr.intent == INTENT_OUT
10248 && sym->as->type == AS_ASSUMED_SIZE)
10250 for (c = sym->ts.u.derived->components; c; c = c->next)
10252 if (c->initializer)
10254 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
10255 "ASSUMED SIZE and so cannot have a default initializer",
10256 sym->name, &sym->declared_at);
10262 switch (sym->attr.flavor)
10265 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
10270 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
10275 if (resolve_fl_namelist (sym) == FAILURE)
10280 if (resolve_fl_parameter (sym) == FAILURE)
10288 /* Resolve array specifier. Check as well some constraints
10289 on COMMON blocks. */
10291 check_constant = sym->attr.in_common && !sym->attr.pointer;
10293 /* Set the formal_arg_flag so that check_conflict will not throw
10294 an error for host associated variables in the specification
10295 expression for an array_valued function. */
10296 if (sym->attr.function && sym->as)
10297 formal_arg_flag = 1;
10299 gfc_resolve_array_spec (sym->as, check_constant);
10301 formal_arg_flag = 0;
10303 /* Resolve formal namespaces. */
10304 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
10305 && !sym->attr.contained && !sym->attr.intrinsic)
10306 gfc_resolve (sym->formal_ns);
10308 /* Make sure the formal namespace is present. */
10309 if (sym->formal && !sym->formal_ns)
10311 gfc_formal_arglist *formal = sym->formal;
10312 while (formal && !formal->sym)
10313 formal = formal->next;
10317 sym->formal_ns = formal->sym->ns;
10318 sym->formal_ns->refs++;
10322 /* Check threadprivate restrictions. */
10323 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
10324 && (!sym->attr.in_common
10325 && sym->module == NULL
10326 && (sym->ns->proc_name == NULL
10327 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
10328 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
10330 /* If we have come this far we can apply default-initializers, as
10331 described in 14.7.5, to those variables that have not already
10332 been assigned one. */
10333 if (sym->ts.type == BT_DERIVED
10334 && sym->attr.referenced
10335 && sym->ns == gfc_current_ns
10337 && !sym->attr.allocatable
10338 && !sym->attr.alloc_comp)
10340 symbol_attribute *a = &sym->attr;
10342 if ((!a->save && !a->dummy && !a->pointer
10343 && !a->in_common && !a->use_assoc
10344 && !(a->function && sym != sym->result))
10345 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
10346 apply_default_init (sym);
10349 /* If this symbol has a type-spec, check it. */
10350 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
10351 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
10352 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
10358 /************* Resolve DATA statements *************/
10362 gfc_data_value *vnode;
10368 /* Advance the values structure to point to the next value in the data list. */
10371 next_data_value (void)
10373 while (mpz_cmp_ui (values.left, 0) == 0)
10375 if (!gfc_is_constant_expr (values.vnode->expr))
10376 gfc_error ("non-constant DATA value at %L",
10377 &values.vnode->expr->where);
10379 if (values.vnode->next == NULL)
10382 values.vnode = values.vnode->next;
10383 mpz_set (values.left, values.vnode->repeat);
10391 check_data_variable (gfc_data_variable *var, locus *where)
10397 ar_type mark = AR_UNKNOWN;
10399 mpz_t section_index[GFC_MAX_DIMENSIONS];
10405 if (gfc_resolve_expr (var->expr) == FAILURE)
10409 mpz_init_set_si (offset, 0);
10412 if (e->expr_type != EXPR_VARIABLE)
10413 gfc_internal_error ("check_data_variable(): Bad expression");
10415 sym = e->symtree->n.sym;
10417 if (sym->ns->is_block_data && !sym->attr.in_common)
10419 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
10420 sym->name, &sym->declared_at);
10423 if (e->ref == NULL && sym->as)
10425 gfc_error ("DATA array '%s' at %L must be specified in a previous"
10426 " declaration", sym->name, where);
10430 has_pointer = sym->attr.pointer;
10432 for (ref = e->ref; ref; ref = ref->next)
10434 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
10438 && ref->type == REF_ARRAY
10439 && ref->u.ar.type != AR_FULL)
10441 gfc_error ("DATA element '%s' at %L is a pointer and so must "
10442 "be a full array", sym->name, where);
10447 if (e->rank == 0 || has_pointer)
10449 mpz_init_set_ui (size, 1);
10456 /* Find the array section reference. */
10457 for (ref = e->ref; ref; ref = ref->next)
10459 if (ref->type != REF_ARRAY)
10461 if (ref->u.ar.type == AR_ELEMENT)
10467 /* Set marks according to the reference pattern. */
10468 switch (ref->u.ar.type)
10476 /* Get the start position of array section. */
10477 gfc_get_section_index (ar, section_index, &offset);
10482 gcc_unreachable ();
10485 if (gfc_array_size (e, &size) == FAILURE)
10487 gfc_error ("Nonconstant array section at %L in DATA statement",
10489 mpz_clear (offset);
10496 while (mpz_cmp_ui (size, 0) > 0)
10498 if (next_data_value () == FAILURE)
10500 gfc_error ("DATA statement at %L has more variables than values",
10506 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
10510 /* If we have more than one element left in the repeat count,
10511 and we have more than one element left in the target variable,
10512 then create a range assignment. */
10513 /* FIXME: Only done for full arrays for now, since array sections
10515 if (mark == AR_FULL && ref && ref->next == NULL
10516 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
10520 if (mpz_cmp (size, values.left) >= 0)
10522 mpz_init_set (range, values.left);
10523 mpz_sub (size, size, values.left);
10524 mpz_set_ui (values.left, 0);
10528 mpz_init_set (range, size);
10529 mpz_sub (values.left, values.left, size);
10530 mpz_set_ui (size, 0);
10533 gfc_assign_data_value_range (var->expr, values.vnode->expr,
10536 mpz_add (offset, offset, range);
10540 /* Assign initial value to symbol. */
10543 mpz_sub_ui (values.left, values.left, 1);
10544 mpz_sub_ui (size, size, 1);
10546 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
10550 if (mark == AR_FULL)
10551 mpz_add_ui (offset, offset, 1);
10553 /* Modify the array section indexes and recalculate the offset
10554 for next element. */
10555 else if (mark == AR_SECTION)
10556 gfc_advance_section (section_index, ar, &offset);
10560 if (mark == AR_SECTION)
10562 for (i = 0; i < ar->dimen; i++)
10563 mpz_clear (section_index[i]);
10567 mpz_clear (offset);
10573 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
10575 /* Iterate over a list of elements in a DATA statement. */
10578 traverse_data_list (gfc_data_variable *var, locus *where)
10581 iterator_stack frame;
10582 gfc_expr *e, *start, *end, *step;
10583 gfc_try retval = SUCCESS;
10585 mpz_init (frame.value);
10587 start = gfc_copy_expr (var->iter.start);
10588 end = gfc_copy_expr (var->iter.end);
10589 step = gfc_copy_expr (var->iter.step);
10591 if (gfc_simplify_expr (start, 1) == FAILURE
10592 || start->expr_type != EXPR_CONSTANT)
10594 gfc_error ("iterator start at %L does not simplify", &start->where);
10598 if (gfc_simplify_expr (end, 1) == FAILURE
10599 || end->expr_type != EXPR_CONSTANT)
10601 gfc_error ("iterator end at %L does not simplify", &end->where);
10605 if (gfc_simplify_expr (step, 1) == FAILURE
10606 || step->expr_type != EXPR_CONSTANT)
10608 gfc_error ("iterator step at %L does not simplify", &step->where);
10613 mpz_init_set (trip, end->value.integer);
10614 mpz_sub (trip, trip, start->value.integer);
10615 mpz_add (trip, trip, step->value.integer);
10617 mpz_div (trip, trip, step->value.integer);
10619 mpz_set (frame.value, start->value.integer);
10621 frame.prev = iter_stack;
10622 frame.variable = var->iter.var->symtree;
10623 iter_stack = &frame;
10625 while (mpz_cmp_ui (trip, 0) > 0)
10627 if (traverse_data_var (var->list, where) == FAILURE)
10634 e = gfc_copy_expr (var->expr);
10635 if (gfc_simplify_expr (e, 1) == FAILURE)
10643 mpz_add (frame.value, frame.value, step->value.integer);
10645 mpz_sub_ui (trip, trip, 1);
10650 mpz_clear (frame.value);
10652 gfc_free_expr (start);
10653 gfc_free_expr (end);
10654 gfc_free_expr (step);
10656 iter_stack = frame.prev;
10661 /* Type resolve variables in the variable list of a DATA statement. */
10664 traverse_data_var (gfc_data_variable *var, locus *where)
10668 for (; var; var = var->next)
10670 if (var->expr == NULL)
10671 t = traverse_data_list (var, where);
10673 t = check_data_variable (var, where);
10683 /* Resolve the expressions and iterators associated with a data statement.
10684 This is separate from the assignment checking because data lists should
10685 only be resolved once. */
10688 resolve_data_variables (gfc_data_variable *d)
10690 for (; d; d = d->next)
10692 if (d->list == NULL)
10694 if (gfc_resolve_expr (d->expr) == FAILURE)
10699 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
10702 if (resolve_data_variables (d->list) == FAILURE)
10711 /* Resolve a single DATA statement. We implement this by storing a pointer to
10712 the value list into static variables, and then recursively traversing the
10713 variables list, expanding iterators and such. */
10716 resolve_data (gfc_data *d)
10719 if (resolve_data_variables (d->var) == FAILURE)
10722 values.vnode = d->value;
10723 if (d->value == NULL)
10724 mpz_set_ui (values.left, 0);
10726 mpz_set (values.left, d->value->repeat);
10728 if (traverse_data_var (d->var, &d->where) == FAILURE)
10731 /* At this point, we better not have any values left. */
10733 if (next_data_value () == SUCCESS)
10734 gfc_error ("DATA statement at %L has more values than variables",
10739 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
10740 accessed by host or use association, is a dummy argument to a pure function,
10741 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
10742 is storage associated with any such variable, shall not be used in the
10743 following contexts: (clients of this function). */
10745 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
10746 procedure. Returns zero if assignment is OK, nonzero if there is a
10749 gfc_impure_variable (gfc_symbol *sym)
10753 if (sym->attr.use_assoc || sym->attr.in_common)
10756 if (sym->ns != gfc_current_ns)
10757 return !sym->attr.function;
10759 proc = sym->ns->proc_name;
10760 if (sym->attr.dummy && gfc_pure (proc)
10761 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
10763 proc->attr.function))
10766 /* TODO: Sort out what can be storage associated, if anything, and include
10767 it here. In principle equivalences should be scanned but it does not
10768 seem to be possible to storage associate an impure variable this way. */
10773 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
10774 symbol of the current procedure. */
10777 gfc_pure (gfc_symbol *sym)
10779 symbol_attribute attr;
10782 sym = gfc_current_ns->proc_name;
10788 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
10792 /* Test whether the current procedure is elemental or not. */
10795 gfc_elemental (gfc_symbol *sym)
10797 symbol_attribute attr;
10800 sym = gfc_current_ns->proc_name;
10805 return attr.flavor == FL_PROCEDURE && attr.elemental;
10809 /* Warn about unused labels. */
10812 warn_unused_fortran_label (gfc_st_label *label)
10817 warn_unused_fortran_label (label->left);
10819 if (label->defined == ST_LABEL_UNKNOWN)
10822 switch (label->referenced)
10824 case ST_LABEL_UNKNOWN:
10825 gfc_warning ("Label %d at %L defined but not used", label->value,
10829 case ST_LABEL_BAD_TARGET:
10830 gfc_warning ("Label %d at %L defined but cannot be used",
10831 label->value, &label->where);
10838 warn_unused_fortran_label (label->right);
10842 /* Returns the sequence type of a symbol or sequence. */
10845 sequence_type (gfc_typespec ts)
10854 if (ts.u.derived->components == NULL)
10855 return SEQ_NONDEFAULT;
10857 result = sequence_type (ts.u.derived->components->ts);
10858 for (c = ts.u.derived->components->next; c; c = c->next)
10859 if (sequence_type (c->ts) != result)
10865 if (ts.kind != gfc_default_character_kind)
10866 return SEQ_NONDEFAULT;
10868 return SEQ_CHARACTER;
10871 if (ts.kind != gfc_default_integer_kind)
10872 return SEQ_NONDEFAULT;
10874 return SEQ_NUMERIC;
10877 if (!(ts.kind == gfc_default_real_kind
10878 || ts.kind == gfc_default_double_kind))
10879 return SEQ_NONDEFAULT;
10881 return SEQ_NUMERIC;
10884 if (ts.kind != gfc_default_complex_kind)
10885 return SEQ_NONDEFAULT;
10887 return SEQ_NUMERIC;
10890 if (ts.kind != gfc_default_logical_kind)
10891 return SEQ_NONDEFAULT;
10893 return SEQ_NUMERIC;
10896 return SEQ_NONDEFAULT;
10901 /* Resolve derived type EQUIVALENCE object. */
10904 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
10906 gfc_component *c = derived->components;
10911 /* Shall not be an object of nonsequence derived type. */
10912 if (!derived->attr.sequence)
10914 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
10915 "attribute to be an EQUIVALENCE object", sym->name,
10920 /* Shall not have allocatable components. */
10921 if (derived->attr.alloc_comp)
10923 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
10924 "components to be an EQUIVALENCE object",sym->name,
10929 if (sym->attr.in_common && has_default_initializer (sym->ts.u.derived))
10931 gfc_error ("Derived type variable '%s' at %L with default "
10932 "initialization cannot be in EQUIVALENCE with a variable "
10933 "in COMMON", sym->name, &e->where);
10937 for (; c ; c = c->next)
10939 if (c->ts.type == BT_DERIVED
10940 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
10943 /* Shall not be an object of sequence derived type containing a pointer
10944 in the structure. */
10945 if (c->attr.pointer)
10947 gfc_error ("Derived type variable '%s' at %L with pointer "
10948 "component(s) cannot be an EQUIVALENCE object",
10949 sym->name, &e->where);
10957 /* Resolve equivalence object.
10958 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
10959 an allocatable array, an object of nonsequence derived type, an object of
10960 sequence derived type containing a pointer at any level of component
10961 selection, an automatic object, a function name, an entry name, a result
10962 name, a named constant, a structure component, or a subobject of any of
10963 the preceding objects. A substring shall not have length zero. A
10964 derived type shall not have components with default initialization nor
10965 shall two objects of an equivalence group be initialized.
10966 Either all or none of the objects shall have an protected attribute.
10967 The simple constraints are done in symbol.c(check_conflict) and the rest
10968 are implemented here. */
10971 resolve_equivalence (gfc_equiv *eq)
10974 gfc_symbol *first_sym;
10977 locus *last_where = NULL;
10978 seq_type eq_type, last_eq_type;
10979 gfc_typespec *last_ts;
10980 int object, cnt_protected;
10981 const char *value_name;
10985 last_ts = &eq->expr->symtree->n.sym->ts;
10987 first_sym = eq->expr->symtree->n.sym;
10991 for (object = 1; eq; eq = eq->eq, object++)
10995 e->ts = e->symtree->n.sym->ts;
10996 /* match_varspec might not know yet if it is seeing
10997 array reference or substring reference, as it doesn't
10999 if (e->ref && e->ref->type == REF_ARRAY)
11001 gfc_ref *ref = e->ref;
11002 sym = e->symtree->n.sym;
11004 if (sym->attr.dimension)
11006 ref->u.ar.as = sym->as;
11010 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
11011 if (e->ts.type == BT_CHARACTER
11013 && ref->type == REF_ARRAY
11014 && ref->u.ar.dimen == 1
11015 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
11016 && ref->u.ar.stride[0] == NULL)
11018 gfc_expr *start = ref->u.ar.start[0];
11019 gfc_expr *end = ref->u.ar.end[0];
11022 /* Optimize away the (:) reference. */
11023 if (start == NULL && end == NULL)
11026 e->ref = ref->next;
11028 e->ref->next = ref->next;
11033 ref->type = REF_SUBSTRING;
11035 start = gfc_int_expr (1);
11036 ref->u.ss.start = start;
11037 if (end == NULL && e->ts.u.cl)
11038 end = gfc_copy_expr (e->ts.u.cl->length);
11039 ref->u.ss.end = end;
11040 ref->u.ss.length = e->ts.u.cl;
11047 /* Any further ref is an error. */
11050 gcc_assert (ref->type == REF_ARRAY);
11051 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
11057 if (gfc_resolve_expr (e) == FAILURE)
11060 sym = e->symtree->n.sym;
11062 if (sym->attr.is_protected)
11064 if (cnt_protected > 0 && cnt_protected != object)
11066 gfc_error ("Either all or none of the objects in the "
11067 "EQUIVALENCE set at %L shall have the "
11068 "PROTECTED attribute",
11073 /* Shall not equivalence common block variables in a PURE procedure. */
11074 if (sym->ns->proc_name
11075 && sym->ns->proc_name->attr.pure
11076 && sym->attr.in_common)
11078 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
11079 "object in the pure procedure '%s'",
11080 sym->name, &e->where, sym->ns->proc_name->name);
11084 /* Shall not be a named constant. */
11085 if (e->expr_type == EXPR_CONSTANT)
11087 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
11088 "object", sym->name, &e->where);
11092 if (e->ts.type == BT_DERIVED
11093 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
11096 /* Check that the types correspond correctly:
11098 A numeric sequence structure may be equivalenced to another sequence
11099 structure, an object of default integer type, default real type, double
11100 precision real type, default logical type such that components of the
11101 structure ultimately only become associated to objects of the same
11102 kind. A character sequence structure may be equivalenced to an object
11103 of default character kind or another character sequence structure.
11104 Other objects may be equivalenced only to objects of the same type and
11105 kind parameters. */
11107 /* Identical types are unconditionally OK. */
11108 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
11109 goto identical_types;
11111 last_eq_type = sequence_type (*last_ts);
11112 eq_type = sequence_type (sym->ts);
11114 /* Since the pair of objects is not of the same type, mixed or
11115 non-default sequences can be rejected. */
11117 msg = "Sequence %s with mixed components in EQUIVALENCE "
11118 "statement at %L with different type objects";
11120 && last_eq_type == SEQ_MIXED
11121 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
11123 || (eq_type == SEQ_MIXED
11124 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11125 &e->where) == FAILURE))
11128 msg = "Non-default type object or sequence %s in EQUIVALENCE "
11129 "statement at %L with objects of different type";
11131 && last_eq_type == SEQ_NONDEFAULT
11132 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
11133 last_where) == FAILURE)
11134 || (eq_type == SEQ_NONDEFAULT
11135 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11136 &e->where) == FAILURE))
11139 msg ="Non-CHARACTER object '%s' in default CHARACTER "
11140 "EQUIVALENCE statement at %L";
11141 if (last_eq_type == SEQ_CHARACTER
11142 && eq_type != SEQ_CHARACTER
11143 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11144 &e->where) == FAILURE)
11147 msg ="Non-NUMERIC object '%s' in default NUMERIC "
11148 "EQUIVALENCE statement at %L";
11149 if (last_eq_type == SEQ_NUMERIC
11150 && eq_type != SEQ_NUMERIC
11151 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11152 &e->where) == FAILURE)
11157 last_where = &e->where;
11162 /* Shall not be an automatic array. */
11163 if (e->ref->type == REF_ARRAY
11164 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
11166 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
11167 "an EQUIVALENCE object", sym->name, &e->where);
11174 /* Shall not be a structure component. */
11175 if (r->type == REF_COMPONENT)
11177 gfc_error ("Structure component '%s' at %L cannot be an "
11178 "EQUIVALENCE object",
11179 r->u.c.component->name, &e->where);
11183 /* A substring shall not have length zero. */
11184 if (r->type == REF_SUBSTRING)
11186 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
11188 gfc_error ("Substring at %L has length zero",
11189 &r->u.ss.start->where);
11199 /* Resolve function and ENTRY types, issue diagnostics if needed. */
11202 resolve_fntype (gfc_namespace *ns)
11204 gfc_entry_list *el;
11207 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
11210 /* If there are any entries, ns->proc_name is the entry master
11211 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
11213 sym = ns->entries->sym;
11215 sym = ns->proc_name;
11216 if (sym->result == sym
11217 && sym->ts.type == BT_UNKNOWN
11218 && gfc_set_default_type (sym, 0, NULL) == FAILURE
11219 && !sym->attr.untyped)
11221 gfc_error ("Function '%s' at %L has no IMPLICIT type",
11222 sym->name, &sym->declared_at);
11223 sym->attr.untyped = 1;
11226 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
11227 && !sym->attr.contained
11228 && !gfc_check_access (sym->ts.u.derived->attr.access,
11229 sym->ts.u.derived->ns->default_access)
11230 && gfc_check_access (sym->attr.access, sym->ns->default_access))
11232 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
11233 "%L of PRIVATE type '%s'", sym->name,
11234 &sym->declared_at, sym->ts.u.derived->name);
11238 for (el = ns->entries->next; el; el = el->next)
11240 if (el->sym->result == el->sym
11241 && el->sym->ts.type == BT_UNKNOWN
11242 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
11243 && !el->sym->attr.untyped)
11245 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
11246 el->sym->name, &el->sym->declared_at);
11247 el->sym->attr.untyped = 1;
11253 /* 12.3.2.1.1 Defined operators. */
11256 check_uop_procedure (gfc_symbol *sym, locus where)
11258 gfc_formal_arglist *formal;
11260 if (!sym->attr.function)
11262 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
11263 sym->name, &where);
11267 if (sym->ts.type == BT_CHARACTER
11268 && !(sym->ts.u.cl && sym->ts.u.cl->length)
11269 && !(sym->result && sym->result->ts.u.cl
11270 && sym->result->ts.u.cl->length))
11272 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
11273 "character length", sym->name, &where);
11277 formal = sym->formal;
11278 if (!formal || !formal->sym)
11280 gfc_error ("User operator procedure '%s' at %L must have at least "
11281 "one argument", sym->name, &where);
11285 if (formal->sym->attr.intent != INTENT_IN)
11287 gfc_error ("First argument of operator interface at %L must be "
11288 "INTENT(IN)", &where);
11292 if (formal->sym->attr.optional)
11294 gfc_error ("First argument of operator interface at %L cannot be "
11295 "optional", &where);
11299 formal = formal->next;
11300 if (!formal || !formal->sym)
11303 if (formal->sym->attr.intent != INTENT_IN)
11305 gfc_error ("Second argument of operator interface at %L must be "
11306 "INTENT(IN)", &where);
11310 if (formal->sym->attr.optional)
11312 gfc_error ("Second argument of operator interface at %L cannot be "
11313 "optional", &where);
11319 gfc_error ("Operator interface at %L must have, at most, two "
11320 "arguments", &where);
11328 gfc_resolve_uops (gfc_symtree *symtree)
11330 gfc_interface *itr;
11332 if (symtree == NULL)
11335 gfc_resolve_uops (symtree->left);
11336 gfc_resolve_uops (symtree->right);
11338 for (itr = symtree->n.uop->op; itr; itr = itr->next)
11339 check_uop_procedure (itr->sym, itr->sym->declared_at);
11343 /* Examine all of the expressions associated with a program unit,
11344 assign types to all intermediate expressions, make sure that all
11345 assignments are to compatible types and figure out which names
11346 refer to which functions or subroutines. It doesn't check code
11347 block, which is handled by resolve_code. */
11350 resolve_types (gfc_namespace *ns)
11356 gfc_namespace* old_ns = gfc_current_ns;
11358 /* Check that all IMPLICIT types are ok. */
11359 if (!ns->seen_implicit_none)
11362 for (letter = 0; letter != GFC_LETTERS; ++letter)
11363 if (ns->set_flag[letter]
11364 && resolve_typespec_used (&ns->default_type[letter],
11365 &ns->implicit_loc[letter],
11370 gfc_current_ns = ns;
11372 resolve_entries (ns);
11374 resolve_common_vars (ns->blank_common.head, false);
11375 resolve_common_blocks (ns->common_root);
11377 resolve_contained_functions (ns);
11379 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
11381 for (cl = ns->cl_list; cl; cl = cl->next)
11382 resolve_charlen (cl);
11384 gfc_traverse_ns (ns, resolve_symbol);
11386 resolve_fntype (ns);
11388 for (n = ns->contained; n; n = n->sibling)
11390 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
11391 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
11392 "also be PURE", n->proc_name->name,
11393 &n->proc_name->declared_at);
11399 gfc_check_interfaces (ns);
11401 gfc_traverse_ns (ns, resolve_values);
11407 for (d = ns->data; d; d = d->next)
11411 gfc_traverse_ns (ns, gfc_formalize_init_value);
11413 gfc_traverse_ns (ns, gfc_verify_binding_labels);
11415 if (ns->common_root != NULL)
11416 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
11418 for (eq = ns->equiv; eq; eq = eq->next)
11419 resolve_equivalence (eq);
11421 /* Warn about unused labels. */
11422 if (warn_unused_label)
11423 warn_unused_fortran_label (ns->st_labels);
11425 gfc_resolve_uops (ns->uop_root);
11427 gfc_current_ns = old_ns;
11431 /* Call resolve_code recursively. */
11434 resolve_codes (gfc_namespace *ns)
11437 bitmap_obstack old_obstack;
11439 for (n = ns->contained; n; n = n->sibling)
11442 gfc_current_ns = ns;
11444 /* Set to an out of range value. */
11445 current_entry_id = -1;
11447 old_obstack = labels_obstack;
11448 bitmap_obstack_initialize (&labels_obstack);
11450 resolve_code (ns->code, ns);
11452 bitmap_obstack_release (&labels_obstack);
11453 labels_obstack = old_obstack;
11457 /* This function is called after a complete program unit has been compiled.
11458 Its purpose is to examine all of the expressions associated with a program
11459 unit, assign types to all intermediate expressions, make sure that all
11460 assignments are to compatible types and figure out which names refer to
11461 which functions or subroutines. */
11464 gfc_resolve (gfc_namespace *ns)
11466 gfc_namespace *old_ns;
11467 code_stack *old_cs_base;
11473 old_ns = gfc_current_ns;
11474 old_cs_base = cs_base;
11476 resolve_types (ns);
11477 resolve_codes (ns);
11479 gfc_current_ns = old_ns;
11480 cs_base = old_cs_base;