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;
1104 gfc_namespace* real_context;
1106 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1108 /* If we've got an ENTRY, find real procedure. */
1109 if (sym->attr.entry && sym->ns->entries)
1110 proc_sym = sym->ns->entries->sym;
1114 /* If sym is RECURSIVE, all is well of course. */
1115 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1118 /* Find the context procedure's "real" symbol if it has entries.
1119 We look for a procedure symbol, so recurse on the parents if we don't
1120 find one (like in case of a BLOCK construct). */
1121 for (real_context = context; ; real_context = real_context->parent)
1123 /* We should find something, eventually! */
1124 gcc_assert (real_context);
1126 context_proc = (real_context->entries ? real_context->entries->sym
1127 : real_context->proc_name);
1129 /* In some special cases, there may not be a proc_name, like for this
1131 real(bad_kind()) function foo () ...
1132 when checking the call to bad_kind ().
1133 In these cases, we simply return here and assume that the
1138 if (context_proc->attr.flavor != FL_LABEL)
1142 /* A call from sym's body to itself is recursion, of course. */
1143 if (context_proc == proc_sym)
1146 /* The same is true if context is a contained procedure and sym the
1148 if (context_proc->attr.contained)
1150 gfc_symbol* parent_proc;
1152 gcc_assert (context->parent);
1153 parent_proc = (context->parent->entries ? context->parent->entries->sym
1154 : context->parent->proc_name);
1156 if (parent_proc == proc_sym)
1164 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1165 its typespec and formal argument list. */
1168 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1170 gfc_intrinsic_sym* isym;
1176 /* We already know this one is an intrinsic, so we don't call
1177 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1178 gfc_find_subroutine directly to check whether it is a function or
1181 if ((isym = gfc_find_function (sym->name)))
1183 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1184 && !sym->attr.implicit_type)
1185 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1186 " ignored", sym->name, &sym->declared_at);
1188 if (!sym->attr.function &&
1189 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1194 else if ((isym = gfc_find_subroutine (sym->name)))
1196 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1198 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1199 " specifier", sym->name, &sym->declared_at);
1203 if (!sym->attr.subroutine &&
1204 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1209 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1214 gfc_copy_formal_args_intr (sym, isym);
1216 /* Check it is actually available in the standard settings. */
1217 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1220 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1221 " available in the current standard settings but %s. Use"
1222 " an appropriate -std=* option or enable -fall-intrinsics"
1223 " in order to use it.",
1224 sym->name, &sym->declared_at, symstd);
1232 /* Resolve a procedure expression, like passing it to a called procedure or as
1233 RHS for a procedure pointer assignment. */
1236 resolve_procedure_expression (gfc_expr* expr)
1240 if (expr->expr_type != EXPR_VARIABLE)
1242 gcc_assert (expr->symtree);
1244 sym = expr->symtree->n.sym;
1246 if (sym->attr.intrinsic)
1247 resolve_intrinsic (sym, &expr->where);
1249 if (sym->attr.flavor != FL_PROCEDURE
1250 || (sym->attr.function && sym->result == sym))
1253 /* A non-RECURSIVE procedure that is used as procedure expression within its
1254 own body is in danger of being called recursively. */
1255 if (is_illegal_recursion (sym, gfc_current_ns))
1256 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1257 " itself recursively. Declare it RECURSIVE or use"
1258 " -frecursive", sym->name, &expr->where);
1264 /* Resolve an actual argument list. Most of the time, this is just
1265 resolving the expressions in the list.
1266 The exception is that we sometimes have to decide whether arguments
1267 that look like procedure arguments are really simple variable
1271 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1272 bool no_formal_args)
1275 gfc_symtree *parent_st;
1277 int save_need_full_assumed_size;
1278 gfc_component *comp;
1280 for (; arg; arg = arg->next)
1285 /* Check the label is a valid branching target. */
1288 if (arg->label->defined == ST_LABEL_UNKNOWN)
1290 gfc_error ("Label %d referenced at %L is never defined",
1291 arg->label->value, &arg->label->where);
1298 if (gfc_is_proc_ptr_comp (e, &comp))
1301 if (e->expr_type == EXPR_PPC)
1303 if (comp->as != NULL)
1304 e->rank = comp->as->rank;
1305 e->expr_type = EXPR_FUNCTION;
1310 if (e->expr_type == EXPR_VARIABLE
1311 && e->symtree->n.sym->attr.generic
1313 && count_specific_procs (e) != 1)
1316 if (e->ts.type != BT_PROCEDURE)
1318 save_need_full_assumed_size = need_full_assumed_size;
1319 if (e->expr_type != EXPR_VARIABLE)
1320 need_full_assumed_size = 0;
1321 if (gfc_resolve_expr (e) != SUCCESS)
1323 need_full_assumed_size = save_need_full_assumed_size;
1327 /* See if the expression node should really be a variable reference. */
1329 sym = e->symtree->n.sym;
1331 if (sym->attr.flavor == FL_PROCEDURE
1332 || sym->attr.intrinsic
1333 || sym->attr.external)
1337 /* If a procedure is not already determined to be something else
1338 check if it is intrinsic. */
1339 if (!sym->attr.intrinsic
1340 && !(sym->attr.external || sym->attr.use_assoc
1341 || sym->attr.if_source == IFSRC_IFBODY)
1342 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1343 sym->attr.intrinsic = 1;
1345 if (sym->attr.proc == PROC_ST_FUNCTION)
1347 gfc_error ("Statement function '%s' at %L is not allowed as an "
1348 "actual argument", sym->name, &e->where);
1351 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1352 sym->attr.subroutine);
1353 if (sym->attr.intrinsic && actual_ok == 0)
1355 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1356 "actual argument", sym->name, &e->where);
1359 if (sym->attr.contained && !sym->attr.use_assoc
1360 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1362 gfc_error ("Internal procedure '%s' is not allowed as an "
1363 "actual argument at %L", sym->name, &e->where);
1366 if (sym->attr.elemental && !sym->attr.intrinsic)
1368 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1369 "allowed as an actual argument at %L", sym->name,
1373 /* Check if a generic interface has a specific procedure
1374 with the same name before emitting an error. */
1375 if (sym->attr.generic && count_specific_procs (e) != 1)
1378 /* Just in case a specific was found for the expression. */
1379 sym = e->symtree->n.sym;
1381 /* If the symbol is the function that names the current (or
1382 parent) scope, then we really have a variable reference. */
1384 if (sym->attr.function && sym->result == sym
1385 && (sym->ns->proc_name == sym
1386 || (sym->ns->parent != NULL
1387 && sym->ns->parent->proc_name == sym)))
1390 /* If all else fails, see if we have a specific intrinsic. */
1391 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1393 gfc_intrinsic_sym *isym;
1395 isym = gfc_find_function (sym->name);
1396 if (isym == NULL || !isym->specific)
1398 gfc_error ("Unable to find a specific INTRINSIC procedure "
1399 "for the reference '%s' at %L", sym->name,
1404 sym->attr.intrinsic = 1;
1405 sym->attr.function = 1;
1408 if (gfc_resolve_expr (e) == FAILURE)
1413 /* See if the name is a module procedure in a parent unit. */
1415 if (was_declared (sym) || sym->ns->parent == NULL)
1418 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1420 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1424 if (parent_st == NULL)
1427 sym = parent_st->n.sym;
1428 e->symtree = parent_st; /* Point to the right thing. */
1430 if (sym->attr.flavor == FL_PROCEDURE
1431 || sym->attr.intrinsic
1432 || sym->attr.external)
1434 if (gfc_resolve_expr (e) == FAILURE)
1440 e->expr_type = EXPR_VARIABLE;
1442 if (sym->as != NULL)
1444 e->rank = sym->as->rank;
1445 e->ref = gfc_get_ref ();
1446 e->ref->type = REF_ARRAY;
1447 e->ref->u.ar.type = AR_FULL;
1448 e->ref->u.ar.as = sym->as;
1451 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1452 primary.c (match_actual_arg). If above code determines that it
1453 is a variable instead, it needs to be resolved as it was not
1454 done at the beginning of this function. */
1455 save_need_full_assumed_size = need_full_assumed_size;
1456 if (e->expr_type != EXPR_VARIABLE)
1457 need_full_assumed_size = 0;
1458 if (gfc_resolve_expr (e) != SUCCESS)
1460 need_full_assumed_size = save_need_full_assumed_size;
1463 /* Check argument list functions %VAL, %LOC and %REF. There is
1464 nothing to do for %REF. */
1465 if (arg->name && arg->name[0] == '%')
1467 if (strncmp ("%VAL", arg->name, 4) == 0)
1469 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1471 gfc_error ("By-value argument at %L is not of numeric "
1478 gfc_error ("By-value argument at %L cannot be an array or "
1479 "an array section", &e->where);
1483 /* Intrinsics are still PROC_UNKNOWN here. However,
1484 since same file external procedures are not resolvable
1485 in gfortran, it is a good deal easier to leave them to
1487 if (ptype != PROC_UNKNOWN
1488 && ptype != PROC_DUMMY
1489 && ptype != PROC_EXTERNAL
1490 && ptype != PROC_MODULE)
1492 gfc_error ("By-value argument at %L is not allowed "
1493 "in this context", &e->where);
1498 /* Statement functions have already been excluded above. */
1499 else if (strncmp ("%LOC", arg->name, 4) == 0
1500 && e->ts.type == BT_PROCEDURE)
1502 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1504 gfc_error ("Passing internal procedure at %L by location "
1505 "not allowed", &e->where);
1516 /* Do the checks of the actual argument list that are specific to elemental
1517 procedures. If called with c == NULL, we have a function, otherwise if
1518 expr == NULL, we have a subroutine. */
1521 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1523 gfc_actual_arglist *arg0;
1524 gfc_actual_arglist *arg;
1525 gfc_symbol *esym = NULL;
1526 gfc_intrinsic_sym *isym = NULL;
1528 gfc_intrinsic_arg *iformal = NULL;
1529 gfc_formal_arglist *eformal = NULL;
1530 bool formal_optional = false;
1531 bool set_by_optional = false;
1535 /* Is this an elemental procedure? */
1536 if (expr && expr->value.function.actual != NULL)
1538 if (expr->value.function.esym != NULL
1539 && expr->value.function.esym->attr.elemental)
1541 arg0 = expr->value.function.actual;
1542 esym = expr->value.function.esym;
1544 else if (expr->value.function.isym != NULL
1545 && expr->value.function.isym->elemental)
1547 arg0 = expr->value.function.actual;
1548 isym = expr->value.function.isym;
1553 else if (c && c->ext.actual != NULL)
1555 arg0 = c->ext.actual;
1557 if (c->resolved_sym)
1558 esym = c->resolved_sym;
1560 esym = c->symtree->n.sym;
1563 if (!esym->attr.elemental)
1569 /* The rank of an elemental is the rank of its array argument(s). */
1570 for (arg = arg0; arg; arg = arg->next)
1572 if (arg->expr != NULL && arg->expr->rank > 0)
1574 rank = arg->expr->rank;
1575 if (arg->expr->expr_type == EXPR_VARIABLE
1576 && arg->expr->symtree->n.sym->attr.optional)
1577 set_by_optional = true;
1579 /* Function specific; set the result rank and shape. */
1583 if (!expr->shape && arg->expr->shape)
1585 expr->shape = gfc_get_shape (rank);
1586 for (i = 0; i < rank; i++)
1587 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1594 /* If it is an array, it shall not be supplied as an actual argument
1595 to an elemental procedure unless an array of the same rank is supplied
1596 as an actual argument corresponding to a nonoptional dummy argument of
1597 that elemental procedure(12.4.1.5). */
1598 formal_optional = false;
1600 iformal = isym->formal;
1602 eformal = esym->formal;
1604 for (arg = arg0; arg; arg = arg->next)
1608 if (eformal->sym && eformal->sym->attr.optional)
1609 formal_optional = true;
1610 eformal = eformal->next;
1612 else if (isym && iformal)
1614 if (iformal->optional)
1615 formal_optional = true;
1616 iformal = iformal->next;
1619 formal_optional = true;
1621 if (pedantic && arg->expr != NULL
1622 && arg->expr->expr_type == EXPR_VARIABLE
1623 && arg->expr->symtree->n.sym->attr.optional
1626 && (set_by_optional || arg->expr->rank != rank)
1627 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1629 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1630 "MISSING, it cannot be the actual argument of an "
1631 "ELEMENTAL procedure unless there is a non-optional "
1632 "argument with the same rank (12.4.1.5)",
1633 arg->expr->symtree->n.sym->name, &arg->expr->where);
1638 for (arg = arg0; arg; arg = arg->next)
1640 if (arg->expr == NULL || arg->expr->rank == 0)
1643 /* Being elemental, the last upper bound of an assumed size array
1644 argument must be present. */
1645 if (resolve_assumed_size_actual (arg->expr))
1648 /* Elemental procedure's array actual arguments must conform. */
1651 if (gfc_check_conformance (arg->expr, e,
1652 "elemental procedure") == FAILURE)
1659 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1660 is an array, the intent inout/out variable needs to be also an array. */
1661 if (rank > 0 && esym && expr == NULL)
1662 for (eformal = esym->formal, arg = arg0; arg && eformal;
1663 arg = arg->next, eformal = eformal->next)
1664 if ((eformal->sym->attr.intent == INTENT_OUT
1665 || eformal->sym->attr.intent == INTENT_INOUT)
1666 && arg->expr && arg->expr->rank == 0)
1668 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1669 "ELEMENTAL subroutine '%s' is a scalar, but another "
1670 "actual argument is an array", &arg->expr->where,
1671 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1672 : "INOUT", eformal->sym->name, esym->name);
1679 /* Go through each actual argument in ACTUAL and see if it can be
1680 implemented as an inlined, non-copying intrinsic. FNSYM is the
1681 function being called, or NULL if not known. */
1684 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1686 gfc_actual_arglist *ap;
1689 for (ap = actual; ap; ap = ap->next)
1691 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1692 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1694 ap->expr->inline_noncopying_intrinsic = 1;
1698 /* This function does the checking of references to global procedures
1699 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1700 77 and 95 standards. It checks for a gsymbol for the name, making
1701 one if it does not already exist. If it already exists, then the
1702 reference being resolved must correspond to the type of gsymbol.
1703 Otherwise, the new symbol is equipped with the attributes of the
1704 reference. The corresponding code that is called in creating
1705 global entities is parse.c.
1707 In addition, for all but -std=legacy, the gsymbols are used to
1708 check the interfaces of external procedures from the same file.
1709 The namespace of the gsymbol is resolved and then, once this is
1710 done the interface is checked. */
1714 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1716 if (!gsym_ns->proc_name->attr.recursive)
1719 if (sym->ns == gsym_ns)
1722 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1729 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1731 if (gsym_ns->entries)
1733 gfc_entry_list *entry = gsym_ns->entries;
1735 for (; entry; entry = entry->next)
1737 if (strcmp (sym->name, entry->sym->name) == 0)
1739 if (strcmp (gsym_ns->proc_name->name,
1740 sym->ns->proc_name->name) == 0)
1744 && strcmp (gsym_ns->proc_name->name,
1745 sym->ns->parent->proc_name->name) == 0)
1754 resolve_global_procedure (gfc_symbol *sym, locus *where,
1755 gfc_actual_arglist **actual, int sub)
1759 enum gfc_symbol_type type;
1761 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1763 gsym = gfc_get_gsymbol (sym->name);
1765 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1766 gfc_global_used (gsym, where);
1768 if (gfc_option.flag_whole_file
1769 && sym->attr.if_source == IFSRC_UNKNOWN
1770 && gsym->type != GSYM_UNKNOWN
1772 && gsym->ns->resolved != -1
1773 && gsym->ns->proc_name
1774 && not_in_recursive (sym, gsym->ns)
1775 && not_entry_self_reference (sym, gsym->ns))
1777 /* Make sure that translation for the gsymbol occurs before
1778 the procedure currently being resolved. */
1779 ns = gsym->ns->resolved ? NULL : gfc_global_ns_list;
1780 for (; ns && ns != gsym->ns; ns = ns->sibling)
1782 if (ns->sibling == gsym->ns)
1784 ns->sibling = gsym->ns->sibling;
1785 gsym->ns->sibling = gfc_global_ns_list;
1786 gfc_global_ns_list = gsym->ns;
1791 if (!gsym->ns->resolved)
1793 gfc_dt_list *old_dt_list;
1795 /* Stash away derived types so that the backend_decls do not
1797 old_dt_list = gfc_derived_types;
1798 gfc_derived_types = NULL;
1800 gfc_resolve (gsym->ns);
1802 /* Store the new derived types with the global namespace. */
1803 if (gfc_derived_types)
1804 gsym->ns->derived_types = gfc_derived_types;
1806 /* Restore the derived types of this namespace. */
1807 gfc_derived_types = old_dt_list;
1810 if (gsym->ns->proc_name->attr.function
1811 && gsym->ns->proc_name->as
1812 && gsym->ns->proc_name->as->rank
1813 && (!sym->as || sym->as->rank != gsym->ns->proc_name->as->rank))
1814 gfc_error ("The reference to function '%s' at %L either needs an "
1815 "explicit INTERFACE or the rank is incorrect", sym->name,
1818 if (gfc_option.flag_whole_file == 1
1819 || ((gfc_option.warn_std & GFC_STD_LEGACY)
1821 !(gfc_option.warn_std & GFC_STD_GNU)))
1822 gfc_errors_to_warnings (1);
1824 gfc_procedure_use (gsym->ns->proc_name, actual, where);
1826 gfc_errors_to_warnings (0);
1829 if (gsym->type == GSYM_UNKNOWN)
1832 gsym->where = *where;
1839 /************* Function resolution *************/
1841 /* Resolve a function call known to be generic.
1842 Section 14.1.2.4.1. */
1845 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1849 if (sym->attr.generic)
1851 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1854 expr->value.function.name = s->name;
1855 expr->value.function.esym = s;
1857 if (s->ts.type != BT_UNKNOWN)
1859 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1860 expr->ts = s->result->ts;
1863 expr->rank = s->as->rank;
1864 else if (s->result != NULL && s->result->as != NULL)
1865 expr->rank = s->result->as->rank;
1867 gfc_set_sym_referenced (expr->value.function.esym);
1872 /* TODO: Need to search for elemental references in generic
1876 if (sym->attr.intrinsic)
1877 return gfc_intrinsic_func_interface (expr, 0);
1884 resolve_generic_f (gfc_expr *expr)
1889 sym = expr->symtree->n.sym;
1893 m = resolve_generic_f0 (expr, sym);
1896 else if (m == MATCH_ERROR)
1900 if (sym->ns->parent == NULL)
1902 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1906 if (!generic_sym (sym))
1910 /* Last ditch attempt. See if the reference is to an intrinsic
1911 that possesses a matching interface. 14.1.2.4 */
1912 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
1914 gfc_error ("There is no specific function for the generic '%s' at %L",
1915 expr->symtree->n.sym->name, &expr->where);
1919 m = gfc_intrinsic_func_interface (expr, 0);
1923 gfc_error ("Generic function '%s' at %L is not consistent with a "
1924 "specific intrinsic interface", expr->symtree->n.sym->name,
1931 /* Resolve a function call known to be specific. */
1934 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
1938 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1940 if (sym->attr.dummy)
1942 sym->attr.proc = PROC_DUMMY;
1946 sym->attr.proc = PROC_EXTERNAL;
1950 if (sym->attr.proc == PROC_MODULE
1951 || sym->attr.proc == PROC_ST_FUNCTION
1952 || sym->attr.proc == PROC_INTERNAL)
1955 if (sym->attr.intrinsic)
1957 m = gfc_intrinsic_func_interface (expr, 1);
1961 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
1962 "with an intrinsic", sym->name, &expr->where);
1970 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1973 expr->ts = sym->result->ts;
1976 expr->value.function.name = sym->name;
1977 expr->value.function.esym = sym;
1978 if (sym->as != NULL)
1979 expr->rank = sym->as->rank;
1986 resolve_specific_f (gfc_expr *expr)
1991 sym = expr->symtree->n.sym;
1995 m = resolve_specific_f0 (sym, expr);
1998 if (m == MATCH_ERROR)
2001 if (sym->ns->parent == NULL)
2004 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2010 gfc_error ("Unable to resolve the specific function '%s' at %L",
2011 expr->symtree->n.sym->name, &expr->where);
2017 /* Resolve a procedure call not known to be generic nor specific. */
2020 resolve_unknown_f (gfc_expr *expr)
2025 sym = expr->symtree->n.sym;
2027 if (sym->attr.dummy)
2029 sym->attr.proc = PROC_DUMMY;
2030 expr->value.function.name = sym->name;
2034 /* See if we have an intrinsic function reference. */
2036 if (gfc_is_intrinsic (sym, 0, expr->where))
2038 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2043 /* The reference is to an external name. */
2045 sym->attr.proc = PROC_EXTERNAL;
2046 expr->value.function.name = sym->name;
2047 expr->value.function.esym = expr->symtree->n.sym;
2049 if (sym->as != NULL)
2050 expr->rank = sym->as->rank;
2052 /* Type of the expression is either the type of the symbol or the
2053 default type of the symbol. */
2056 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2058 if (sym->ts.type != BT_UNKNOWN)
2062 ts = gfc_get_default_type (sym->name, sym->ns);
2064 if (ts->type == BT_UNKNOWN)
2066 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2067 sym->name, &expr->where);
2078 /* Return true, if the symbol is an external procedure. */
2080 is_external_proc (gfc_symbol *sym)
2082 if (!sym->attr.dummy && !sym->attr.contained
2083 && !(sym->attr.intrinsic
2084 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2085 && sym->attr.proc != PROC_ST_FUNCTION
2086 && !sym->attr.use_assoc
2094 /* Figure out if a function reference is pure or not. Also set the name
2095 of the function for a potential error message. Return nonzero if the
2096 function is PURE, zero if not. */
2098 pure_stmt_function (gfc_expr *, gfc_symbol *);
2101 pure_function (gfc_expr *e, const char **name)
2107 if (e->symtree != NULL
2108 && e->symtree->n.sym != NULL
2109 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2110 return pure_stmt_function (e, e->symtree->n.sym);
2112 if (e->value.function.esym)
2114 pure = gfc_pure (e->value.function.esym);
2115 *name = e->value.function.esym->name;
2117 else if (e->value.function.isym)
2119 pure = e->value.function.isym->pure
2120 || e->value.function.isym->elemental;
2121 *name = e->value.function.isym->name;
2125 /* Implicit functions are not pure. */
2127 *name = e->value.function.name;
2135 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2136 int *f ATTRIBUTE_UNUSED)
2140 /* Don't bother recursing into other statement functions
2141 since they will be checked individually for purity. */
2142 if (e->expr_type != EXPR_FUNCTION
2144 || e->symtree->n.sym == sym
2145 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2148 return pure_function (e, &name) ? false : true;
2153 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2155 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2160 is_scalar_expr_ptr (gfc_expr *expr)
2162 gfc_try retval = SUCCESS;
2167 /* See if we have a gfc_ref, which means we have a substring, array
2168 reference, or a component. */
2169 if (expr->ref != NULL)
2172 while (ref->next != NULL)
2178 if (ref->u.ss.length != NULL
2179 && ref->u.ss.length->length != NULL
2181 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2183 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2185 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2186 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2187 if (end - start + 1 != 1)
2194 if (ref->u.ar.type == AR_ELEMENT)
2196 else if (ref->u.ar.type == AR_FULL)
2198 /* The user can give a full array if the array is of size 1. */
2199 if (ref->u.ar.as != NULL
2200 && ref->u.ar.as->rank == 1
2201 && ref->u.ar.as->type == AS_EXPLICIT
2202 && ref->u.ar.as->lower[0] != NULL
2203 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2204 && ref->u.ar.as->upper[0] != NULL
2205 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2207 /* If we have a character string, we need to check if
2208 its length is one. */
2209 if (expr->ts.type == BT_CHARACTER)
2211 if (expr->ts.u.cl == NULL
2212 || expr->ts.u.cl->length == NULL
2213 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2219 /* We have constant lower and upper bounds. If the
2220 difference between is 1, it can be considered a
2222 start = (int) mpz_get_si
2223 (ref->u.ar.as->lower[0]->value.integer);
2224 end = (int) mpz_get_si
2225 (ref->u.ar.as->upper[0]->value.integer);
2226 if (end - start + 1 != 1)
2241 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2243 /* Character string. Make sure it's of length 1. */
2244 if (expr->ts.u.cl == NULL
2245 || expr->ts.u.cl->length == NULL
2246 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2249 else if (expr->rank != 0)
2256 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2257 and, in the case of c_associated, set the binding label based on
2261 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2262 gfc_symbol **new_sym)
2264 char name[GFC_MAX_SYMBOL_LEN + 1];
2265 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2266 int optional_arg = 0, is_pointer = 0;
2267 gfc_try retval = SUCCESS;
2268 gfc_symbol *args_sym;
2269 gfc_typespec *arg_ts;
2271 if (args->expr->expr_type == EXPR_CONSTANT
2272 || args->expr->expr_type == EXPR_OP
2273 || args->expr->expr_type == EXPR_NULL)
2275 gfc_error ("Argument to '%s' at %L is not a variable",
2276 sym->name, &(args->expr->where));
2280 args_sym = args->expr->symtree->n.sym;
2282 /* The typespec for the actual arg should be that stored in the expr
2283 and not necessarily that of the expr symbol (args_sym), because
2284 the actual expression could be a part-ref of the expr symbol. */
2285 arg_ts = &(args->expr->ts);
2287 is_pointer = gfc_is_data_pointer (args->expr);
2289 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2291 /* If the user gave two args then they are providing something for
2292 the optional arg (the second cptr). Therefore, set the name and
2293 binding label to the c_associated for two cptrs. Otherwise,
2294 set c_associated to expect one cptr. */
2298 sprintf (name, "%s_2", sym->name);
2299 sprintf (binding_label, "%s_2", sym->binding_label);
2305 sprintf (name, "%s_1", sym->name);
2306 sprintf (binding_label, "%s_1", sym->binding_label);
2310 /* Get a new symbol for the version of c_associated that
2312 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2314 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2315 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2317 sprintf (name, "%s", sym->name);
2318 sprintf (binding_label, "%s", sym->binding_label);
2320 /* Error check the call. */
2321 if (args->next != NULL)
2323 gfc_error_now ("More actual than formal arguments in '%s' "
2324 "call at %L", name, &(args->expr->where));
2327 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2329 /* Make sure we have either the target or pointer attribute. */
2330 if (!args_sym->attr.target && !is_pointer)
2332 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2333 "a TARGET or an associated pointer",
2335 sym->name, &(args->expr->where));
2339 /* See if we have interoperable type and type param. */
2340 if (verify_c_interop (arg_ts) == SUCCESS
2341 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2343 if (args_sym->attr.target == 1)
2345 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2346 has the target attribute and is interoperable. */
2347 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2348 allocatable variable that has the TARGET attribute and
2349 is not an array of zero size. */
2350 if (args_sym->attr.allocatable == 1)
2352 if (args_sym->attr.dimension != 0
2353 && (args_sym->as && args_sym->as->rank == 0))
2355 gfc_error_now ("Allocatable variable '%s' used as a "
2356 "parameter to '%s' at %L must not be "
2357 "an array of zero size",
2358 args_sym->name, sym->name,
2359 &(args->expr->where));
2365 /* A non-allocatable target variable with C
2366 interoperable type and type parameters must be
2368 if (args_sym && args_sym->attr.dimension)
2370 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2372 gfc_error ("Assumed-shape array '%s' at %L "
2373 "cannot be an argument to the "
2374 "procedure '%s' because "
2375 "it is not C interoperable",
2377 &(args->expr->where), sym->name);
2380 else if (args_sym->as->type == AS_DEFERRED)
2382 gfc_error ("Deferred-shape array '%s' at %L "
2383 "cannot be an argument to the "
2384 "procedure '%s' because "
2385 "it is not C interoperable",
2387 &(args->expr->where), sym->name);
2392 /* Make sure it's not a character string. Arrays of
2393 any type should be ok if the variable is of a C
2394 interoperable type. */
2395 if (arg_ts->type == BT_CHARACTER)
2396 if (arg_ts->u.cl != NULL
2397 && (arg_ts->u.cl->length == NULL
2398 || arg_ts->u.cl->length->expr_type
2401 (arg_ts->u.cl->length->value.integer, 1)
2403 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2405 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2406 "at %L must have a length of 1",
2407 args_sym->name, sym->name,
2408 &(args->expr->where));
2414 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2416 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2418 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2419 "associated scalar POINTER", args_sym->name,
2420 sym->name, &(args->expr->where));
2426 /* The parameter is not required to be C interoperable. If it
2427 is not C interoperable, it must be a nonpolymorphic scalar
2428 with no length type parameters. It still must have either
2429 the pointer or target attribute, and it can be
2430 allocatable (but must be allocated when c_loc is called). */
2431 if (args->expr->rank != 0
2432 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2434 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2435 "scalar", args_sym->name, sym->name,
2436 &(args->expr->where));
2439 else if (arg_ts->type == BT_CHARACTER
2440 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2442 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2443 "%L must have a length of 1",
2444 args_sym->name, sym->name,
2445 &(args->expr->where));
2450 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2452 if (args_sym->attr.flavor != FL_PROCEDURE)
2454 /* TODO: Update this error message to allow for procedure
2455 pointers once they are implemented. */
2456 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2458 args_sym->name, sym->name,
2459 &(args->expr->where));
2462 else if (args_sym->attr.is_bind_c != 1)
2464 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2466 args_sym->name, sym->name,
2467 &(args->expr->where));
2472 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2477 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2478 "iso_c_binding function: '%s'!\n", sym->name);
2485 /* Resolve a function call, which means resolving the arguments, then figuring
2486 out which entity the name refers to. */
2487 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2488 to INTENT(OUT) or INTENT(INOUT). */
2491 resolve_function (gfc_expr *expr)
2493 gfc_actual_arglist *arg;
2498 procedure_type p = PROC_INTRINSIC;
2499 bool no_formal_args;
2503 sym = expr->symtree->n.sym;
2505 if (sym && sym->attr.intrinsic
2506 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2509 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2511 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2515 if (sym && sym->attr.abstract)
2517 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2518 sym->name, &expr->where);
2522 /* Switch off assumed size checking and do this again for certain kinds
2523 of procedure, once the procedure itself is resolved. */
2524 need_full_assumed_size++;
2526 if (expr->symtree && expr->symtree->n.sym)
2527 p = expr->symtree->n.sym->attr.proc;
2529 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2530 if (resolve_actual_arglist (expr->value.function.actual,
2531 p, no_formal_args) == FAILURE)
2534 /* Need to setup the call to the correct c_associated, depending on
2535 the number of cptrs to user gives to compare. */
2536 if (sym && sym->attr.is_iso_c == 1)
2538 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2542 /* Get the symtree for the new symbol (resolved func).
2543 the old one will be freed later, when it's no longer used. */
2544 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2547 /* Resume assumed_size checking. */
2548 need_full_assumed_size--;
2550 /* If the procedure is external, check for usage. */
2551 if (sym && is_external_proc (sym))
2552 resolve_global_procedure (sym, &expr->where,
2553 &expr->value.function.actual, 0);
2555 if (sym && sym->ts.type == BT_CHARACTER
2557 && sym->ts.u.cl->length == NULL
2559 && expr->value.function.esym == NULL
2560 && !sym->attr.contained)
2562 /* Internal procedures are taken care of in resolve_contained_fntype. */
2563 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2564 "be used at %L since it is not a dummy argument",
2565 sym->name, &expr->where);
2569 /* See if function is already resolved. */
2571 if (expr->value.function.name != NULL)
2573 if (expr->ts.type == BT_UNKNOWN)
2579 /* Apply the rules of section 14.1.2. */
2581 switch (procedure_kind (sym))
2584 t = resolve_generic_f (expr);
2587 case PTYPE_SPECIFIC:
2588 t = resolve_specific_f (expr);
2592 t = resolve_unknown_f (expr);
2596 gfc_internal_error ("resolve_function(): bad function type");
2600 /* If the expression is still a function (it might have simplified),
2601 then we check to see if we are calling an elemental function. */
2603 if (expr->expr_type != EXPR_FUNCTION)
2606 temp = need_full_assumed_size;
2607 need_full_assumed_size = 0;
2609 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2612 if (omp_workshare_flag
2613 && expr->value.function.esym
2614 && ! gfc_elemental (expr->value.function.esym))
2616 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2617 "in WORKSHARE construct", expr->value.function.esym->name,
2622 #define GENERIC_ID expr->value.function.isym->id
2623 else if (expr->value.function.actual != NULL
2624 && expr->value.function.isym != NULL
2625 && GENERIC_ID != GFC_ISYM_LBOUND
2626 && GENERIC_ID != GFC_ISYM_LEN
2627 && GENERIC_ID != GFC_ISYM_LOC
2628 && GENERIC_ID != GFC_ISYM_PRESENT)
2630 /* Array intrinsics must also have the last upper bound of an
2631 assumed size array argument. UBOUND and SIZE have to be
2632 excluded from the check if the second argument is anything
2635 for (arg = expr->value.function.actual; arg; arg = arg->next)
2637 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2638 && arg->next != NULL && arg->next->expr)
2640 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2643 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2646 if ((int)mpz_get_si (arg->next->expr->value.integer)
2651 if (arg->expr != NULL
2652 && arg->expr->rank > 0
2653 && resolve_assumed_size_actual (arg->expr))
2659 need_full_assumed_size = temp;
2662 if (!pure_function (expr, &name) && name)
2666 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2667 "FORALL %s", name, &expr->where,
2668 forall_flag == 2 ? "mask" : "block");
2671 else if (gfc_pure (NULL))
2673 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2674 "procedure within a PURE procedure", name, &expr->where);
2679 /* Functions without the RECURSIVE attribution are not allowed to
2680 * call themselves. */
2681 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2684 esym = expr->value.function.esym;
2686 if (is_illegal_recursion (esym, gfc_current_ns))
2688 if (esym->attr.entry && esym->ns->entries)
2689 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2690 " function '%s' is not RECURSIVE",
2691 esym->name, &expr->where, esym->ns->entries->sym->name);
2693 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2694 " is not RECURSIVE", esym->name, &expr->where);
2700 /* Character lengths of use associated functions may contains references to
2701 symbols not referenced from the current program unit otherwise. Make sure
2702 those symbols are marked as referenced. */
2704 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2705 && expr->value.function.esym->attr.use_assoc)
2707 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
2711 && !((expr->value.function.esym
2712 && expr->value.function.esym->attr.elemental)
2714 (expr->value.function.isym
2715 && expr->value.function.isym->elemental)))
2716 find_noncopying_intrinsics (expr->value.function.esym,
2717 expr->value.function.actual);
2719 /* Make sure that the expression has a typespec that works. */
2720 if (expr->ts.type == BT_UNKNOWN)
2722 if (expr->symtree->n.sym->result
2723 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
2724 && !expr->symtree->n.sym->result->attr.proc_pointer)
2725 expr->ts = expr->symtree->n.sym->result->ts;
2732 /************* Subroutine resolution *************/
2735 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2741 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2742 sym->name, &c->loc);
2743 else if (gfc_pure (NULL))
2744 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2750 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2754 if (sym->attr.generic)
2756 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2759 c->resolved_sym = s;
2760 pure_subroutine (c, s);
2764 /* TODO: Need to search for elemental references in generic interface. */
2767 if (sym->attr.intrinsic)
2768 return gfc_intrinsic_sub_interface (c, 0);
2775 resolve_generic_s (gfc_code *c)
2780 sym = c->symtree->n.sym;
2784 m = resolve_generic_s0 (c, sym);
2787 else if (m == MATCH_ERROR)
2791 if (sym->ns->parent == NULL)
2793 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2797 if (!generic_sym (sym))
2801 /* Last ditch attempt. See if the reference is to an intrinsic
2802 that possesses a matching interface. 14.1.2.4 */
2803 sym = c->symtree->n.sym;
2805 if (!gfc_is_intrinsic (sym, 1, c->loc))
2807 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2808 sym->name, &c->loc);
2812 m = gfc_intrinsic_sub_interface (c, 0);
2816 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2817 "intrinsic subroutine interface", sym->name, &c->loc);
2823 /* Set the name and binding label of the subroutine symbol in the call
2824 expression represented by 'c' to include the type and kind of the
2825 second parameter. This function is for resolving the appropriate
2826 version of c_f_pointer() and c_f_procpointer(). For example, a
2827 call to c_f_pointer() for a default integer pointer could have a
2828 name of c_f_pointer_i4. If no second arg exists, which is an error
2829 for these two functions, it defaults to the generic symbol's name
2830 and binding label. */
2833 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2834 char *name, char *binding_label)
2836 gfc_expr *arg = NULL;
2840 /* The second arg of c_f_pointer and c_f_procpointer determines
2841 the type and kind for the procedure name. */
2842 arg = c->ext.actual->next->expr;
2846 /* Set up the name to have the given symbol's name,
2847 plus the type and kind. */
2848 /* a derived type is marked with the type letter 'u' */
2849 if (arg->ts.type == BT_DERIVED)
2852 kind = 0; /* set the kind as 0 for now */
2856 type = gfc_type_letter (arg->ts.type);
2857 kind = arg->ts.kind;
2860 if (arg->ts.type == BT_CHARACTER)
2861 /* Kind info for character strings not needed. */
2864 sprintf (name, "%s_%c%d", sym->name, type, kind);
2865 /* Set up the binding label as the given symbol's label plus
2866 the type and kind. */
2867 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2871 /* If the second arg is missing, set the name and label as
2872 was, cause it should at least be found, and the missing
2873 arg error will be caught by compare_parameters(). */
2874 sprintf (name, "%s", sym->name);
2875 sprintf (binding_label, "%s", sym->binding_label);
2882 /* Resolve a generic version of the iso_c_binding procedure given
2883 (sym) to the specific one based on the type and kind of the
2884 argument(s). Currently, this function resolves c_f_pointer() and
2885 c_f_procpointer based on the type and kind of the second argument
2886 (FPTR). Other iso_c_binding procedures aren't specially handled.
2887 Upon successfully exiting, c->resolved_sym will hold the resolved
2888 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2892 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2894 gfc_symbol *new_sym;
2895 /* this is fine, since we know the names won't use the max */
2896 char name[GFC_MAX_SYMBOL_LEN + 1];
2897 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2898 /* default to success; will override if find error */
2899 match m = MATCH_YES;
2901 /* Make sure the actual arguments are in the necessary order (based on the
2902 formal args) before resolving. */
2903 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2905 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2906 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2908 set_name_and_label (c, sym, name, binding_label);
2910 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2912 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2914 /* Make sure we got a third arg if the second arg has non-zero
2915 rank. We must also check that the type and rank are
2916 correct since we short-circuit this check in
2917 gfc_procedure_use() (called above to sort actual args). */
2918 if (c->ext.actual->next->expr->rank != 0)
2920 if(c->ext.actual->next->next == NULL
2921 || c->ext.actual->next->next->expr == NULL)
2924 gfc_error ("Missing SHAPE parameter for call to %s "
2925 "at %L", sym->name, &(c->loc));
2927 else if (c->ext.actual->next->next->expr->ts.type
2929 || c->ext.actual->next->next->expr->rank != 1)
2932 gfc_error ("SHAPE parameter for call to %s at %L must "
2933 "be a rank 1 INTEGER array", sym->name,
2940 if (m != MATCH_ERROR)
2942 /* the 1 means to add the optional arg to formal list */
2943 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
2945 /* for error reporting, say it's declared where the original was */
2946 new_sym->declared_at = sym->declared_at;
2951 /* no differences for c_loc or c_funloc */
2955 /* set the resolved symbol */
2956 if (m != MATCH_ERROR)
2957 c->resolved_sym = new_sym;
2959 c->resolved_sym = sym;
2965 /* Resolve a subroutine call known to be specific. */
2968 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
2972 if(sym->attr.is_iso_c)
2974 m = gfc_iso_c_sub_interface (c,sym);
2978 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2980 if (sym->attr.dummy)
2982 sym->attr.proc = PROC_DUMMY;
2986 sym->attr.proc = PROC_EXTERNAL;
2990 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
2993 if (sym->attr.intrinsic)
2995 m = gfc_intrinsic_sub_interface (c, 1);
2999 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3000 "with an intrinsic", sym->name, &c->loc);
3008 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3010 c->resolved_sym = sym;
3011 pure_subroutine (c, sym);
3018 resolve_specific_s (gfc_code *c)
3023 sym = c->symtree->n.sym;
3027 m = resolve_specific_s0 (c, sym);
3030 if (m == MATCH_ERROR)
3033 if (sym->ns->parent == NULL)
3036 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3042 sym = c->symtree->n.sym;
3043 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3044 sym->name, &c->loc);
3050 /* Resolve a subroutine call not known to be generic nor specific. */
3053 resolve_unknown_s (gfc_code *c)
3057 sym = c->symtree->n.sym;
3059 if (sym->attr.dummy)
3061 sym->attr.proc = PROC_DUMMY;
3065 /* See if we have an intrinsic function reference. */
3067 if (gfc_is_intrinsic (sym, 1, c->loc))
3069 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3074 /* The reference is to an external name. */
3077 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3079 c->resolved_sym = sym;
3081 pure_subroutine (c, sym);
3087 /* Resolve a subroutine call. Although it was tempting to use the same code
3088 for functions, subroutines and functions are stored differently and this
3089 makes things awkward. */
3092 resolve_call (gfc_code *c)
3095 procedure_type ptype = PROC_INTRINSIC;
3096 gfc_symbol *csym, *sym;
3097 bool no_formal_args;
3099 csym = c->symtree ? c->symtree->n.sym : NULL;
3101 if (csym && csym->ts.type != BT_UNKNOWN)
3103 gfc_error ("'%s' at %L has a type, which is not consistent with "
3104 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3108 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3111 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3112 sym = st ? st->n.sym : NULL;
3113 if (sym && csym != sym
3114 && sym->ns == gfc_current_ns
3115 && sym->attr.flavor == FL_PROCEDURE
3116 && sym->attr.contained)
3119 if (csym->attr.generic)
3120 c->symtree->n.sym = sym;
3123 csym = c->symtree->n.sym;
3127 /* Subroutines without the RECURSIVE attribution are not allowed to
3128 * call themselves. */
3129 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3131 if (csym->attr.entry && csym->ns->entries)
3132 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3133 " subroutine '%s' is not RECURSIVE",
3134 csym->name, &c->loc, csym->ns->entries->sym->name);
3136 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3137 " is not RECURSIVE", csym->name, &c->loc);
3142 /* Switch off assumed size checking and do this again for certain kinds
3143 of procedure, once the procedure itself is resolved. */
3144 need_full_assumed_size++;
3147 ptype = csym->attr.proc;
3149 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3150 if (resolve_actual_arglist (c->ext.actual, ptype,
3151 no_formal_args) == FAILURE)
3154 /* Resume assumed_size checking. */
3155 need_full_assumed_size--;
3157 /* If external, check for usage. */
3158 if (csym && is_external_proc (csym))
3159 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3162 if (c->resolved_sym == NULL)
3164 c->resolved_isym = NULL;
3165 switch (procedure_kind (csym))
3168 t = resolve_generic_s (c);
3171 case PTYPE_SPECIFIC:
3172 t = resolve_specific_s (c);
3176 t = resolve_unknown_s (c);
3180 gfc_internal_error ("resolve_subroutine(): bad function type");
3184 /* Some checks of elemental subroutine actual arguments. */
3185 if (resolve_elemental_actual (NULL, c) == FAILURE)
3188 if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
3189 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
3194 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3195 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3196 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3197 if their shapes do not match. If either op1->shape or op2->shape is
3198 NULL, return SUCCESS. */
3201 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3208 if (op1->shape != NULL && op2->shape != NULL)
3210 for (i = 0; i < op1->rank; i++)
3212 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3214 gfc_error ("Shapes for operands at %L and %L are not conformable",
3215 &op1->where, &op2->where);
3226 /* Resolve an operator expression node. This can involve replacing the
3227 operation with a user defined function call. */
3230 resolve_operator (gfc_expr *e)
3232 gfc_expr *op1, *op2;
3234 bool dual_locus_error;
3237 /* Resolve all subnodes-- give them types. */
3239 switch (e->value.op.op)
3242 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3245 /* Fall through... */
3248 case INTRINSIC_UPLUS:
3249 case INTRINSIC_UMINUS:
3250 case INTRINSIC_PARENTHESES:
3251 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3256 /* Typecheck the new node. */
3258 op1 = e->value.op.op1;
3259 op2 = e->value.op.op2;
3260 dual_locus_error = false;
3262 if ((op1 && op1->expr_type == EXPR_NULL)
3263 || (op2 && op2->expr_type == EXPR_NULL))
3265 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3269 switch (e->value.op.op)
3271 case INTRINSIC_UPLUS:
3272 case INTRINSIC_UMINUS:
3273 if (op1->ts.type == BT_INTEGER
3274 || op1->ts.type == BT_REAL
3275 || op1->ts.type == BT_COMPLEX)
3281 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3282 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3285 case INTRINSIC_PLUS:
3286 case INTRINSIC_MINUS:
3287 case INTRINSIC_TIMES:
3288 case INTRINSIC_DIVIDE:
3289 case INTRINSIC_POWER:
3290 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3292 gfc_type_convert_binary (e);
3297 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3298 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3299 gfc_typename (&op2->ts));
3302 case INTRINSIC_CONCAT:
3303 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3304 && op1->ts.kind == op2->ts.kind)
3306 e->ts.type = BT_CHARACTER;
3307 e->ts.kind = op1->ts.kind;
3312 _("Operands of string concatenation operator at %%L are %s/%s"),
3313 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3319 case INTRINSIC_NEQV:
3320 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3322 e->ts.type = BT_LOGICAL;
3323 e->ts.kind = gfc_kind_max (op1, op2);
3324 if (op1->ts.kind < e->ts.kind)
3325 gfc_convert_type (op1, &e->ts, 2);
3326 else if (op2->ts.kind < e->ts.kind)
3327 gfc_convert_type (op2, &e->ts, 2);
3331 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3332 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3333 gfc_typename (&op2->ts));
3338 if (op1->ts.type == BT_LOGICAL)
3340 e->ts.type = BT_LOGICAL;
3341 e->ts.kind = op1->ts.kind;
3345 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3346 gfc_typename (&op1->ts));
3350 case INTRINSIC_GT_OS:
3352 case INTRINSIC_GE_OS:
3354 case INTRINSIC_LT_OS:
3356 case INTRINSIC_LE_OS:
3357 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3359 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3363 /* Fall through... */
3366 case INTRINSIC_EQ_OS:
3368 case INTRINSIC_NE_OS:
3369 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3370 && op1->ts.kind == op2->ts.kind)
3372 e->ts.type = BT_LOGICAL;
3373 e->ts.kind = gfc_default_logical_kind;
3377 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3379 gfc_type_convert_binary (e);
3381 e->ts.type = BT_LOGICAL;
3382 e->ts.kind = gfc_default_logical_kind;
3386 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3388 _("Logicals at %%L must be compared with %s instead of %s"),
3389 (e->value.op.op == INTRINSIC_EQ
3390 || e->value.op.op == INTRINSIC_EQ_OS)
3391 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3394 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3395 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3396 gfc_typename (&op2->ts));
3400 case INTRINSIC_USER:
3401 if (e->value.op.uop->op == NULL)
3402 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3403 else if (op2 == NULL)
3404 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3405 e->value.op.uop->name, gfc_typename (&op1->ts));
3407 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3408 e->value.op.uop->name, gfc_typename (&op1->ts),
3409 gfc_typename (&op2->ts));
3413 case INTRINSIC_PARENTHESES:
3415 if (e->ts.type == BT_CHARACTER)
3416 e->ts.u.cl = op1->ts.u.cl;
3420 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3423 /* Deal with arrayness of an operand through an operator. */
3427 switch (e->value.op.op)
3429 case INTRINSIC_PLUS:
3430 case INTRINSIC_MINUS:
3431 case INTRINSIC_TIMES:
3432 case INTRINSIC_DIVIDE:
3433 case INTRINSIC_POWER:
3434 case INTRINSIC_CONCAT:
3438 case INTRINSIC_NEQV:
3440 case INTRINSIC_EQ_OS:
3442 case INTRINSIC_NE_OS:
3444 case INTRINSIC_GT_OS:
3446 case INTRINSIC_GE_OS:
3448 case INTRINSIC_LT_OS:
3450 case INTRINSIC_LE_OS:
3452 if (op1->rank == 0 && op2->rank == 0)
3455 if (op1->rank == 0 && op2->rank != 0)
3457 e->rank = op2->rank;
3459 if (e->shape == NULL)
3460 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3463 if (op1->rank != 0 && op2->rank == 0)
3465 e->rank = op1->rank;
3467 if (e->shape == NULL)
3468 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3471 if (op1->rank != 0 && op2->rank != 0)
3473 if (op1->rank == op2->rank)
3475 e->rank = op1->rank;
3476 if (e->shape == NULL)
3478 t = compare_shapes(op1, op2);
3482 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3487 /* Allow higher level expressions to work. */
3490 /* Try user-defined operators, and otherwise throw an error. */
3491 dual_locus_error = true;
3493 _("Inconsistent ranks for operator at %%L and %%L"));
3500 case INTRINSIC_PARENTHESES:
3502 case INTRINSIC_UPLUS:
3503 case INTRINSIC_UMINUS:
3504 /* Simply copy arrayness attribute */
3505 e->rank = op1->rank;
3507 if (e->shape == NULL)
3508 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3516 /* Attempt to simplify the expression. */
3519 t = gfc_simplify_expr (e, 0);
3520 /* Some calls do not succeed in simplification and return FAILURE
3521 even though there is no error; e.g. variable references to
3522 PARAMETER arrays. */
3523 if (!gfc_is_constant_expr (e))
3532 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3539 if (dual_locus_error)
3540 gfc_error (msg, &op1->where, &op2->where);
3542 gfc_error (msg, &e->where);
3548 /************** Array resolution subroutines **************/
3551 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3554 /* Compare two integer expressions. */
3557 compare_bound (gfc_expr *a, gfc_expr *b)
3561 if (a == NULL || a->expr_type != EXPR_CONSTANT
3562 || b == NULL || b->expr_type != EXPR_CONSTANT)
3565 /* If either of the types isn't INTEGER, we must have
3566 raised an error earlier. */
3568 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3571 i = mpz_cmp (a->value.integer, b->value.integer);
3581 /* Compare an integer expression with an integer. */
3584 compare_bound_int (gfc_expr *a, int b)
3588 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3591 if (a->ts.type != BT_INTEGER)
3592 gfc_internal_error ("compare_bound_int(): Bad expression");
3594 i = mpz_cmp_si (a->value.integer, b);
3604 /* Compare an integer expression with a mpz_t. */
3607 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3611 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3614 if (a->ts.type != BT_INTEGER)
3615 gfc_internal_error ("compare_bound_int(): Bad expression");
3617 i = mpz_cmp (a->value.integer, b);
3627 /* Compute the last value of a sequence given by a triplet.
3628 Return 0 if it wasn't able to compute the last value, or if the
3629 sequence if empty, and 1 otherwise. */
3632 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3633 gfc_expr *stride, mpz_t last)
3637 if (start == NULL || start->expr_type != EXPR_CONSTANT
3638 || end == NULL || end->expr_type != EXPR_CONSTANT
3639 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3642 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3643 || (stride != NULL && stride->ts.type != BT_INTEGER))
3646 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3648 if (compare_bound (start, end) == CMP_GT)
3650 mpz_set (last, end->value.integer);
3654 if (compare_bound_int (stride, 0) == CMP_GT)
3656 /* Stride is positive */
3657 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3662 /* Stride is negative */
3663 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3668 mpz_sub (rem, end->value.integer, start->value.integer);
3669 mpz_tdiv_r (rem, rem, stride->value.integer);
3670 mpz_sub (last, end->value.integer, rem);
3677 /* Compare a single dimension of an array reference to the array
3681 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3685 /* Given start, end and stride values, calculate the minimum and
3686 maximum referenced indexes. */
3688 switch (ar->dimen_type[i])
3694 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3696 gfc_warning ("Array reference at %L is out of bounds "
3697 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3698 mpz_get_si (ar->start[i]->value.integer),
3699 mpz_get_si (as->lower[i]->value.integer), i+1);
3702 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3704 gfc_warning ("Array reference at %L is out of bounds "
3705 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3706 mpz_get_si (ar->start[i]->value.integer),
3707 mpz_get_si (as->upper[i]->value.integer), i+1);
3715 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3716 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3718 comparison comp_start_end = compare_bound (AR_START, AR_END);
3720 /* Check for zero stride, which is not allowed. */
3721 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3723 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3727 /* if start == len || (stride > 0 && start < len)
3728 || (stride < 0 && start > len),
3729 then the array section contains at least one element. In this
3730 case, there is an out-of-bounds access if
3731 (start < lower || start > upper). */
3732 if (compare_bound (AR_START, AR_END) == CMP_EQ
3733 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3734 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3735 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3736 && comp_start_end == CMP_GT))
3738 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3740 gfc_warning ("Lower array reference at %L is out of bounds "
3741 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3742 mpz_get_si (AR_START->value.integer),
3743 mpz_get_si (as->lower[i]->value.integer), i+1);
3746 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3748 gfc_warning ("Lower array reference at %L is out of bounds "
3749 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3750 mpz_get_si (AR_START->value.integer),
3751 mpz_get_si (as->upper[i]->value.integer), i+1);
3756 /* If we can compute the highest index of the array section,
3757 then it also has to be between lower and upper. */
3758 mpz_init (last_value);
3759 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3762 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3764 gfc_warning ("Upper array reference at %L is out of bounds "
3765 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3766 mpz_get_si (last_value),
3767 mpz_get_si (as->lower[i]->value.integer), i+1);
3768 mpz_clear (last_value);
3771 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3773 gfc_warning ("Upper array reference at %L is out of bounds "
3774 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3775 mpz_get_si (last_value),
3776 mpz_get_si (as->upper[i]->value.integer), i+1);
3777 mpz_clear (last_value);
3781 mpz_clear (last_value);
3789 gfc_internal_error ("check_dimension(): Bad array reference");
3796 /* Compare an array reference with an array specification. */
3799 compare_spec_to_ref (gfc_array_ref *ar)
3806 /* TODO: Full array sections are only allowed as actual parameters. */
3807 if (as->type == AS_ASSUMED_SIZE
3808 && (/*ar->type == AR_FULL
3809 ||*/ (ar->type == AR_SECTION
3810 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3812 gfc_error ("Rightmost upper bound of assumed size array section "
3813 "not specified at %L", &ar->where);
3817 if (ar->type == AR_FULL)
3820 if (as->rank != ar->dimen)
3822 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3823 &ar->where, ar->dimen, as->rank);
3827 for (i = 0; i < as->rank; i++)
3828 if (check_dimension (i, ar, as) == FAILURE)
3835 /* Resolve one part of an array index. */
3838 gfc_resolve_index (gfc_expr *index, int check_scalar)
3845 if (gfc_resolve_expr (index) == FAILURE)
3848 if (check_scalar && index->rank != 0)
3850 gfc_error ("Array index at %L must be scalar", &index->where);
3854 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
3856 gfc_error ("Array index at %L must be of INTEGER type, found %s",
3857 &index->where, gfc_basic_typename (index->ts.type));
3861 if (index->ts.type == BT_REAL)
3862 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
3863 &index->where) == FAILURE)
3866 if (index->ts.kind != gfc_index_integer_kind
3867 || index->ts.type != BT_INTEGER)
3870 ts.type = BT_INTEGER;
3871 ts.kind = gfc_index_integer_kind;
3873 gfc_convert_type_warn (index, &ts, 2, 0);
3879 /* Resolve a dim argument to an intrinsic function. */
3882 gfc_resolve_dim_arg (gfc_expr *dim)
3887 if (gfc_resolve_expr (dim) == FAILURE)
3892 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3897 if (dim->ts.type != BT_INTEGER)
3899 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3903 if (dim->ts.kind != gfc_index_integer_kind)
3907 ts.type = BT_INTEGER;
3908 ts.kind = gfc_index_integer_kind;
3910 gfc_convert_type_warn (dim, &ts, 2, 0);
3916 /* Given an expression that contains array references, update those array
3917 references to point to the right array specifications. While this is
3918 filled in during matching, this information is difficult to save and load
3919 in a module, so we take care of it here.
3921 The idea here is that the original array reference comes from the
3922 base symbol. We traverse the list of reference structures, setting
3923 the stored reference to references. Component references can
3924 provide an additional array specification. */
3927 find_array_spec (gfc_expr *e)
3931 gfc_symbol *derived;
3934 as = e->symtree->n.sym->as;
3937 for (ref = e->ref; ref; ref = ref->next)
3942 gfc_internal_error ("find_array_spec(): Missing spec");
3949 if (derived == NULL)
3950 derived = e->symtree->n.sym->ts.u.derived;
3952 c = derived->components;
3954 for (; c; c = c->next)
3955 if (c == ref->u.c.component)
3957 /* Track the sequence of component references. */
3958 if (c->ts.type == BT_DERIVED)
3959 derived = c->ts.u.derived;
3964 gfc_internal_error ("find_array_spec(): Component not found");
3966 if (c->attr.dimension)
3969 gfc_internal_error ("find_array_spec(): unused as(1)");
3980 gfc_internal_error ("find_array_spec(): unused as(2)");
3984 /* Resolve an array reference. */
3987 resolve_array_ref (gfc_array_ref *ar)
3989 int i, check_scalar;
3992 for (i = 0; i < ar->dimen; i++)
3994 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
3996 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
3998 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4000 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4005 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4009 ar->dimen_type[i] = DIMEN_ELEMENT;
4013 ar->dimen_type[i] = DIMEN_VECTOR;
4014 if (e->expr_type == EXPR_VARIABLE
4015 && e->symtree->n.sym->ts.type == BT_DERIVED)
4016 ar->start[i] = gfc_get_parentheses (e);
4020 gfc_error ("Array index at %L is an array of rank %d",
4021 &ar->c_where[i], e->rank);
4026 /* If the reference type is unknown, figure out what kind it is. */
4028 if (ar->type == AR_UNKNOWN)
4030 ar->type = AR_ELEMENT;
4031 for (i = 0; i < ar->dimen; i++)
4032 if (ar->dimen_type[i] == DIMEN_RANGE
4033 || ar->dimen_type[i] == DIMEN_VECTOR)
4035 ar->type = AR_SECTION;
4040 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4048 resolve_substring (gfc_ref *ref)
4050 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4052 if (ref->u.ss.start != NULL)
4054 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4057 if (ref->u.ss.start->ts.type != BT_INTEGER)
4059 gfc_error ("Substring start index at %L must be of type INTEGER",
4060 &ref->u.ss.start->where);
4064 if (ref->u.ss.start->rank != 0)
4066 gfc_error ("Substring start index at %L must be scalar",
4067 &ref->u.ss.start->where);
4071 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4072 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4073 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4075 gfc_error ("Substring start index at %L is less than one",
4076 &ref->u.ss.start->where);
4081 if (ref->u.ss.end != NULL)
4083 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4086 if (ref->u.ss.end->ts.type != BT_INTEGER)
4088 gfc_error ("Substring end index at %L must be of type INTEGER",
4089 &ref->u.ss.end->where);
4093 if (ref->u.ss.end->rank != 0)
4095 gfc_error ("Substring end index at %L must be scalar",
4096 &ref->u.ss.end->where);
4100 if (ref->u.ss.length != NULL
4101 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4102 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4103 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4105 gfc_error ("Substring end index at %L exceeds the string length",
4106 &ref->u.ss.start->where);
4110 if (compare_bound_mpz_t (ref->u.ss.end,
4111 gfc_integer_kinds[k].huge) == CMP_GT
4112 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4113 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4115 gfc_error ("Substring end index at %L is too large",
4116 &ref->u.ss.end->where);
4125 /* This function supplies missing substring charlens. */
4128 gfc_resolve_substring_charlen (gfc_expr *e)
4131 gfc_expr *start, *end;
4133 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4134 if (char_ref->type == REF_SUBSTRING)
4140 gcc_assert (char_ref->next == NULL);
4144 if (e->ts.u.cl->length)
4145 gfc_free_expr (e->ts.u.cl->length);
4146 else if (e->expr_type == EXPR_VARIABLE
4147 && e->symtree->n.sym->attr.dummy)
4151 e->ts.type = BT_CHARACTER;
4152 e->ts.kind = gfc_default_character_kind;
4155 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4157 if (char_ref->u.ss.start)
4158 start = gfc_copy_expr (char_ref->u.ss.start);
4160 start = gfc_int_expr (1);
4162 if (char_ref->u.ss.end)
4163 end = gfc_copy_expr (char_ref->u.ss.end);
4164 else if (e->expr_type == EXPR_VARIABLE)
4165 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4172 /* Length = (end - start +1). */
4173 e->ts.u.cl->length = gfc_subtract (end, start);
4174 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length, gfc_int_expr (1));
4176 e->ts.u.cl->length->ts.type = BT_INTEGER;
4177 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4179 /* Make sure that the length is simplified. */
4180 gfc_simplify_expr (e->ts.u.cl->length, 1);
4181 gfc_resolve_expr (e->ts.u.cl->length);
4185 /* Resolve subtype references. */
4188 resolve_ref (gfc_expr *expr)
4190 int current_part_dimension, n_components, seen_part_dimension;
4193 for (ref = expr->ref; ref; ref = ref->next)
4194 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4196 find_array_spec (expr);
4200 for (ref = expr->ref; ref; ref = ref->next)
4204 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4212 resolve_substring (ref);
4216 /* Check constraints on part references. */
4218 current_part_dimension = 0;
4219 seen_part_dimension = 0;
4222 for (ref = expr->ref; ref; ref = ref->next)
4227 switch (ref->u.ar.type)
4231 current_part_dimension = 1;
4235 current_part_dimension = 0;
4239 gfc_internal_error ("resolve_ref(): Bad array reference");
4245 if (current_part_dimension || seen_part_dimension)
4247 if (ref->u.c.component->attr.pointer)
4249 gfc_error ("Component to the right of a part reference "
4250 "with nonzero rank must not have the POINTER "
4251 "attribute at %L", &expr->where);
4254 else if (ref->u.c.component->attr.allocatable)
4256 gfc_error ("Component to the right of a part reference "
4257 "with nonzero rank must not have the ALLOCATABLE "
4258 "attribute at %L", &expr->where);
4270 if (((ref->type == REF_COMPONENT && n_components > 1)
4271 || ref->next == NULL)
4272 && current_part_dimension
4273 && seen_part_dimension)
4275 gfc_error ("Two or more part references with nonzero rank must "
4276 "not be specified at %L", &expr->where);
4280 if (ref->type == REF_COMPONENT)
4282 if (current_part_dimension)
4283 seen_part_dimension = 1;
4285 /* reset to make sure */
4286 current_part_dimension = 0;
4294 /* Given an expression, determine its shape. This is easier than it sounds.
4295 Leaves the shape array NULL if it is not possible to determine the shape. */
4298 expression_shape (gfc_expr *e)
4300 mpz_t array[GFC_MAX_DIMENSIONS];
4303 if (e->rank == 0 || e->shape != NULL)
4306 for (i = 0; i < e->rank; i++)
4307 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4310 e->shape = gfc_get_shape (e->rank);
4312 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4317 for (i--; i >= 0; i--)
4318 mpz_clear (array[i]);
4322 /* Given a variable expression node, compute the rank of the expression by
4323 examining the base symbol and any reference structures it may have. */
4326 expression_rank (gfc_expr *e)
4331 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4332 could lead to serious confusion... */
4333 gcc_assert (e->expr_type != EXPR_COMPCALL);
4337 if (e->expr_type == EXPR_ARRAY)
4339 /* Constructors can have a rank different from one via RESHAPE(). */
4341 if (e->symtree == NULL)
4347 e->rank = (e->symtree->n.sym->as == NULL)
4348 ? 0 : e->symtree->n.sym->as->rank;
4354 for (ref = e->ref; ref; ref = ref->next)
4356 if (ref->type != REF_ARRAY)
4359 if (ref->u.ar.type == AR_FULL)
4361 rank = ref->u.ar.as->rank;
4365 if (ref->u.ar.type == AR_SECTION)
4367 /* Figure out the rank of the section. */
4369 gfc_internal_error ("expression_rank(): Two array specs");
4371 for (i = 0; i < ref->u.ar.dimen; i++)
4372 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4373 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4383 expression_shape (e);
4387 /* Resolve a variable expression. */
4390 resolve_variable (gfc_expr *e)
4397 if (e->symtree == NULL)
4400 if (e->ref && resolve_ref (e) == FAILURE)
4403 sym = e->symtree->n.sym;
4404 if (sym->attr.flavor == FL_PROCEDURE
4405 && (!sym->attr.function
4406 || (sym->attr.function && sym->result
4407 && sym->result->attr.proc_pointer
4408 && !sym->result->attr.function)))
4410 e->ts.type = BT_PROCEDURE;
4411 goto resolve_procedure;
4414 if (sym->ts.type != BT_UNKNOWN)
4415 gfc_variable_attr (e, &e->ts);
4418 /* Must be a simple variable reference. */
4419 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4424 if (check_assumed_size_reference (sym, e))
4427 /* Deal with forward references to entries during resolve_code, to
4428 satisfy, at least partially, 12.5.2.5. */
4429 if (gfc_current_ns->entries
4430 && current_entry_id == sym->entry_id
4433 && cs_base->current->op != EXEC_ENTRY)
4435 gfc_entry_list *entry;
4436 gfc_formal_arglist *formal;
4440 /* If the symbol is a dummy... */
4441 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4443 entry = gfc_current_ns->entries;
4446 /* ...test if the symbol is a parameter of previous entries. */
4447 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4448 for (formal = entry->sym->formal; formal; formal = formal->next)
4450 if (formal->sym && sym->name == formal->sym->name)
4454 /* If it has not been seen as a dummy, this is an error. */
4457 if (specification_expr)
4458 gfc_error ("Variable '%s', used in a specification expression"
4459 ", is referenced at %L before the ENTRY statement "
4460 "in which it is a parameter",
4461 sym->name, &cs_base->current->loc);
4463 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4464 "statement in which it is a parameter",
4465 sym->name, &cs_base->current->loc);
4470 /* Now do the same check on the specification expressions. */
4471 specification_expr = 1;
4472 if (sym->ts.type == BT_CHARACTER
4473 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
4477 for (n = 0; n < sym->as->rank; n++)
4479 specification_expr = 1;
4480 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4482 specification_expr = 1;
4483 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4486 specification_expr = 0;
4489 /* Update the symbol's entry level. */
4490 sym->entry_id = current_entry_id + 1;
4494 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
4501 /* Checks to see that the correct symbol has been host associated.
4502 The only situation where this arises is that in which a twice
4503 contained function is parsed after the host association is made.
4504 Therefore, on detecting this, change the symbol in the expression
4505 and convert the array reference into an actual arglist if the old
4506 symbol is a variable. */
4508 check_host_association (gfc_expr *e)
4510 gfc_symbol *sym, *old_sym;
4514 gfc_actual_arglist *arg, *tail = NULL;
4515 bool retval = e->expr_type == EXPR_FUNCTION;
4517 /* If the expression is the result of substitution in
4518 interface.c(gfc_extend_expr) because there is no way in
4519 which the host association can be wrong. */
4520 if (e->symtree == NULL
4521 || e->symtree->n.sym == NULL
4522 || e->user_operator)
4525 old_sym = e->symtree->n.sym;
4527 if (gfc_current_ns->parent
4528 && old_sym->ns != gfc_current_ns)
4530 /* Use the 'USE' name so that renamed module symbols are
4531 correctly handled. */
4532 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
4534 if (sym && old_sym != sym
4535 && sym->ts.type == old_sym->ts.type
4536 && sym->attr.flavor == FL_PROCEDURE
4537 && sym->attr.contained)
4539 /* Clear the shape, since it might not be valid. */
4540 if (e->shape != NULL)
4542 for (n = 0; n < e->rank; n++)
4543 mpz_clear (e->shape[n]);
4545 gfc_free (e->shape);
4548 /* Give the expression the right symtree! */
4549 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
4550 gcc_assert (st != NULL);
4552 if (old_sym->attr.flavor == FL_PROCEDURE
4553 || e->expr_type == EXPR_FUNCTION)
4555 /* Original was function so point to the new symbol, since
4556 the actual argument list is already attached to the
4558 e->value.function.esym = NULL;
4563 /* Original was variable so convert array references into
4564 an actual arglist. This does not need any checking now
4565 since gfc_resolve_function will take care of it. */
4566 e->value.function.actual = NULL;
4567 e->expr_type = EXPR_FUNCTION;
4570 /* Ambiguity will not arise if the array reference is not
4571 the last reference. */
4572 for (ref = e->ref; ref; ref = ref->next)
4573 if (ref->type == REF_ARRAY && ref->next == NULL)
4576 gcc_assert (ref->type == REF_ARRAY);
4578 /* Grab the start expressions from the array ref and
4579 copy them into actual arguments. */
4580 for (n = 0; n < ref->u.ar.dimen; n++)
4582 arg = gfc_get_actual_arglist ();
4583 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4584 if (e->value.function.actual == NULL)
4585 tail = e->value.function.actual = arg;
4593 /* Dump the reference list and set the rank. */
4594 gfc_free_ref_list (e->ref);
4596 e->rank = sym->as ? sym->as->rank : 0;
4599 gfc_resolve_expr (e);
4603 /* This might have changed! */
4604 return e->expr_type == EXPR_FUNCTION;
4609 gfc_resolve_character_operator (gfc_expr *e)
4611 gfc_expr *op1 = e->value.op.op1;
4612 gfc_expr *op2 = e->value.op.op2;
4613 gfc_expr *e1 = NULL;
4614 gfc_expr *e2 = NULL;
4616 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
4618 if (op1->ts.u.cl && op1->ts.u.cl->length)
4619 e1 = gfc_copy_expr (op1->ts.u.cl->length);
4620 else if (op1->expr_type == EXPR_CONSTANT)
4621 e1 = gfc_int_expr (op1->value.character.length);
4623 if (op2->ts.u.cl && op2->ts.u.cl->length)
4624 e2 = gfc_copy_expr (op2->ts.u.cl->length);
4625 else if (op2->expr_type == EXPR_CONSTANT)
4626 e2 = gfc_int_expr (op2->value.character.length);
4628 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4633 e->ts.u.cl->length = gfc_add (e1, e2);
4634 e->ts.u.cl->length->ts.type = BT_INTEGER;
4635 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4636 gfc_simplify_expr (e->ts.u.cl->length, 0);
4637 gfc_resolve_expr (e->ts.u.cl->length);
4643 /* Ensure that an character expression has a charlen and, if possible, a
4644 length expression. */
4647 fixup_charlen (gfc_expr *e)
4649 /* The cases fall through so that changes in expression type and the need
4650 for multiple fixes are picked up. In all circumstances, a charlen should
4651 be available for the middle end to hang a backend_decl on. */
4652 switch (e->expr_type)
4655 gfc_resolve_character_operator (e);
4658 if (e->expr_type == EXPR_ARRAY)
4659 gfc_resolve_character_array_constructor (e);
4661 case EXPR_SUBSTRING:
4662 if (!e->ts.u.cl && e->ref)
4663 gfc_resolve_substring_charlen (e);
4667 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4674 /* Update an actual argument to include the passed-object for type-bound
4675 procedures at the right position. */
4677 static gfc_actual_arglist*
4678 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
4681 gcc_assert (argpos > 0);
4685 gfc_actual_arglist* result;
4687 result = gfc_get_actual_arglist ();
4691 result->name = name;
4697 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
4699 lst = update_arglist_pass (NULL, po, argpos - 1, name);
4704 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
4707 extract_compcall_passed_object (gfc_expr* e)
4711 gcc_assert (e->expr_type == EXPR_COMPCALL);
4713 if (e->value.compcall.base_object)
4714 po = gfc_copy_expr (e->value.compcall.base_object);
4717 po = gfc_get_expr ();
4718 po->expr_type = EXPR_VARIABLE;
4719 po->symtree = e->symtree;
4720 po->ref = gfc_copy_ref (e->ref);
4723 if (gfc_resolve_expr (po) == FAILURE)
4730 /* Update the arglist of an EXPR_COMPCALL expression to include the
4734 update_compcall_arglist (gfc_expr* e)
4737 gfc_typebound_proc* tbp;
4739 tbp = e->value.compcall.tbp;
4744 po = extract_compcall_passed_object (e);
4750 gfc_error ("Passed-object at %L must be scalar", &e->where);
4754 if (tbp->nopass || e->value.compcall.ignore_pass)
4760 gcc_assert (tbp->pass_arg_num > 0);
4761 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4769 /* Extract the passed object from a PPC call (a copy of it). */
4772 extract_ppc_passed_object (gfc_expr *e)
4777 po = gfc_get_expr ();
4778 po->expr_type = EXPR_VARIABLE;
4779 po->symtree = e->symtree;
4780 po->ref = gfc_copy_ref (e->ref);
4782 /* Remove PPC reference. */
4784 while ((*ref)->next)
4785 (*ref) = (*ref)->next;
4786 gfc_free_ref_list (*ref);
4789 if (gfc_resolve_expr (po) == FAILURE)
4796 /* Update the actual arglist of a procedure pointer component to include the
4800 update_ppc_arglist (gfc_expr* e)
4804 gfc_typebound_proc* tb;
4806 if (!gfc_is_proc_ptr_comp (e, &ppc))
4813 else if (tb->nopass)
4816 po = extract_ppc_passed_object (e);
4822 gfc_error ("Passed-object at %L must be scalar", &e->where);
4826 gcc_assert (tb->pass_arg_num > 0);
4827 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4835 /* Check that the object a TBP is called on is valid, i.e. it must not be
4836 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
4839 check_typebound_baseobject (gfc_expr* e)
4843 base = extract_compcall_passed_object (e);
4847 gcc_assert (base->ts.type == BT_DERIVED);
4848 if (base->ts.u.derived->attr.abstract)
4850 gfc_error ("Base object for type-bound procedure call at %L is of"
4851 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
4859 /* Resolve a call to a type-bound procedure, either function or subroutine,
4860 statically from the data in an EXPR_COMPCALL expression. The adapted
4861 arglist and the target-procedure symtree are returned. */
4864 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
4865 gfc_actual_arglist** actual)
4867 gcc_assert (e->expr_type == EXPR_COMPCALL);
4868 gcc_assert (!e->value.compcall.tbp->is_generic);
4870 /* Update the actual arglist for PASS. */
4871 if (update_compcall_arglist (e) == FAILURE)
4874 *actual = e->value.compcall.actual;
4875 *target = e->value.compcall.tbp->u.specific;
4877 gfc_free_ref_list (e->ref);
4879 e->value.compcall.actual = NULL;
4885 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
4886 which of the specific bindings (if any) matches the arglist and transform
4887 the expression into a call of that binding. */
4890 resolve_typebound_generic_call (gfc_expr* e)
4892 gfc_typebound_proc* genproc;
4893 const char* genname;
4895 gcc_assert (e->expr_type == EXPR_COMPCALL);
4896 genname = e->value.compcall.name;
4897 genproc = e->value.compcall.tbp;
4899 if (!genproc->is_generic)
4902 /* Try the bindings on this type and in the inheritance hierarchy. */
4903 for (; genproc; genproc = genproc->overridden)
4907 gcc_assert (genproc->is_generic);
4908 for (g = genproc->u.generic; g; g = g->next)
4911 gfc_actual_arglist* args;
4914 gcc_assert (g->specific);
4916 if (g->specific->error)
4919 target = g->specific->u.specific->n.sym;
4921 /* Get the right arglist by handling PASS/NOPASS. */
4922 args = gfc_copy_actual_arglist (e->value.compcall.actual);
4923 if (!g->specific->nopass)
4926 po = extract_compcall_passed_object (e);
4930 gcc_assert (g->specific->pass_arg_num > 0);
4931 gcc_assert (!g->specific->error);
4932 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
4933 g->specific->pass_arg);
4935 resolve_actual_arglist (args, target->attr.proc,
4936 is_external_proc (target) && !target->formal);
4938 /* Check if this arglist matches the formal. */
4939 matches = gfc_arglist_matches_symbol (&args, target);
4941 /* Clean up and break out of the loop if we've found it. */
4942 gfc_free_actual_arglist (args);
4945 e->value.compcall.tbp = g->specific;
4951 /* Nothing matching found! */
4952 gfc_error ("Found no matching specific binding for the call to the GENERIC"
4953 " '%s' at %L", genname, &e->where);
4961 /* Resolve a call to a type-bound subroutine. */
4964 resolve_typebound_call (gfc_code* c)
4966 gfc_actual_arglist* newactual;
4967 gfc_symtree* target;
4969 /* Check that's really a SUBROUTINE. */
4970 if (!c->expr1->value.compcall.tbp->subroutine)
4972 gfc_error ("'%s' at %L should be a SUBROUTINE",
4973 c->expr1->value.compcall.name, &c->loc);
4977 if (check_typebound_baseobject (c->expr1) == FAILURE)
4980 if (resolve_typebound_generic_call (c->expr1) == FAILURE)
4983 /* Transform into an ordinary EXEC_CALL for now. */
4985 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
4988 c->ext.actual = newactual;
4989 c->symtree = target;
4990 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
4992 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
4993 gfc_free_expr (c->expr1);
4996 return resolve_call (c);
5000 /* Resolve a component-call expression. */
5003 resolve_compcall (gfc_expr* e)
5005 gfc_actual_arglist* newactual;
5006 gfc_symtree* target;
5008 /* Check that's really a FUNCTION. */
5009 if (!e->value.compcall.tbp->function)
5011 gfc_error ("'%s' at %L should be a FUNCTION",
5012 e->value.compcall.name, &e->where);
5016 /* These must not be assign-calls! */
5017 gcc_assert (!e->value.compcall.assign);
5019 if (check_typebound_baseobject (e) == FAILURE)
5022 if (resolve_typebound_generic_call (e) == FAILURE)
5024 gcc_assert (!e->value.compcall.tbp->is_generic);
5026 /* Take the rank from the function's symbol. */
5027 if (e->value.compcall.tbp->u.specific->n.sym->as)
5028 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5030 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5031 arglist to the TBP's binding target. */
5033 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5036 e->value.function.actual = newactual;
5037 e->value.function.name = e->value.compcall.name;
5038 e->value.function.esym = target->n.sym;
5039 e->value.function.isym = NULL;
5040 e->symtree = target;
5041 e->ts = target->n.sym->ts;
5042 e->expr_type = EXPR_FUNCTION;
5044 return gfc_resolve_expr (e);
5048 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5051 resolve_ppc_call (gfc_code* c)
5053 gfc_component *comp;
5054 gcc_assert (gfc_is_proc_ptr_comp (c->expr1, &comp));
5056 c->resolved_sym = c->expr1->symtree->n.sym;
5057 c->expr1->expr_type = EXPR_VARIABLE;
5059 if (!comp->attr.subroutine)
5060 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5062 if (resolve_ref (c->expr1) == FAILURE)
5065 if (update_ppc_arglist (c->expr1) == FAILURE)
5068 c->ext.actual = c->expr1->value.compcall.actual;
5070 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5071 comp->formal == NULL) == FAILURE)
5074 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5080 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5083 resolve_expr_ppc (gfc_expr* e)
5085 gfc_component *comp;
5086 gcc_assert (gfc_is_proc_ptr_comp (e, &comp));
5088 /* Convert to EXPR_FUNCTION. */
5089 e->expr_type = EXPR_FUNCTION;
5090 e->value.function.isym = NULL;
5091 e->value.function.actual = e->value.compcall.actual;
5093 if (comp->as != NULL)
5094 e->rank = comp->as->rank;
5096 if (!comp->attr.function)
5097 gfc_add_function (&comp->attr, comp->name, &e->where);
5099 if (resolve_ref (e) == FAILURE)
5102 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5103 comp->formal == NULL) == FAILURE)
5106 if (update_ppc_arglist (e) == FAILURE)
5109 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
5115 /* Resolve an expression. That is, make sure that types of operands agree
5116 with their operators, intrinsic operators are converted to function calls
5117 for overloaded types and unresolved function references are resolved. */
5120 gfc_resolve_expr (gfc_expr *e)
5127 switch (e->expr_type)
5130 t = resolve_operator (e);
5136 if (check_host_association (e))
5137 t = resolve_function (e);
5140 t = resolve_variable (e);
5142 expression_rank (e);
5145 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
5146 && e->ref->type != REF_SUBSTRING)
5147 gfc_resolve_substring_charlen (e);
5152 t = resolve_compcall (e);
5155 case EXPR_SUBSTRING:
5156 t = resolve_ref (e);
5165 t = resolve_expr_ppc (e);
5170 if (resolve_ref (e) == FAILURE)
5173 t = gfc_resolve_array_constructor (e);
5174 /* Also try to expand a constructor. */
5177 expression_rank (e);
5178 gfc_expand_constructor (e);
5181 /* This provides the opportunity for the length of constructors with
5182 character valued function elements to propagate the string length
5183 to the expression. */
5184 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
5185 t = gfc_resolve_character_array_constructor (e);
5189 case EXPR_STRUCTURE:
5190 t = resolve_ref (e);
5194 t = resolve_structure_cons (e);
5198 t = gfc_simplify_expr (e, 0);
5202 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
5205 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
5212 /* Resolve an expression from an iterator. They must be scalar and have
5213 INTEGER or (optionally) REAL type. */
5216 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
5217 const char *name_msgid)
5219 if (gfc_resolve_expr (expr) == FAILURE)
5222 if (expr->rank != 0)
5224 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
5228 if (expr->ts.type != BT_INTEGER)
5230 if (expr->ts.type == BT_REAL)
5233 return gfc_notify_std (GFC_STD_F95_DEL,
5234 "Deleted feature: %s at %L must be integer",
5235 _(name_msgid), &expr->where);
5238 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
5245 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
5253 /* Resolve the expressions in an iterator structure. If REAL_OK is
5254 false allow only INTEGER type iterators, otherwise allow REAL types. */
5257 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
5259 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
5263 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
5265 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5270 if (gfc_resolve_iterator_expr (iter->start, real_ok,
5271 "Start expression in DO loop") == FAILURE)
5274 if (gfc_resolve_iterator_expr (iter->end, real_ok,
5275 "End expression in DO loop") == FAILURE)
5278 if (gfc_resolve_iterator_expr (iter->step, real_ok,
5279 "Step expression in DO loop") == FAILURE)
5282 if (iter->step->expr_type == EXPR_CONSTANT)
5284 if ((iter->step->ts.type == BT_INTEGER
5285 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
5286 || (iter->step->ts.type == BT_REAL
5287 && mpfr_sgn (iter->step->value.real) == 0))
5289 gfc_error ("Step expression in DO loop at %L cannot be zero",
5290 &iter->step->where);
5295 /* Convert start, end, and step to the same type as var. */
5296 if (iter->start->ts.kind != iter->var->ts.kind
5297 || iter->start->ts.type != iter->var->ts.type)
5298 gfc_convert_type (iter->start, &iter->var->ts, 2);
5300 if (iter->end->ts.kind != iter->var->ts.kind
5301 || iter->end->ts.type != iter->var->ts.type)
5302 gfc_convert_type (iter->end, &iter->var->ts, 2);
5304 if (iter->step->ts.kind != iter->var->ts.kind
5305 || iter->step->ts.type != iter->var->ts.type)
5306 gfc_convert_type (iter->step, &iter->var->ts, 2);
5308 if (iter->start->expr_type == EXPR_CONSTANT
5309 && iter->end->expr_type == EXPR_CONSTANT
5310 && iter->step->expr_type == EXPR_CONSTANT)
5313 if (iter->start->ts.type == BT_INTEGER)
5315 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
5316 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
5320 sgn = mpfr_sgn (iter->step->value.real);
5321 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
5323 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
5324 gfc_warning ("DO loop at %L will be executed zero times",
5325 &iter->step->where);
5332 /* Traversal function for find_forall_index. f == 2 signals that
5333 that variable itself is not to be checked - only the references. */
5336 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
5338 if (expr->expr_type != EXPR_VARIABLE)
5341 /* A scalar assignment */
5342 if (!expr->ref || *f == 1)
5344 if (expr->symtree->n.sym == sym)
5356 /* Check whether the FORALL index appears in the expression or not.
5357 Returns SUCCESS if SYM is found in EXPR. */
5360 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
5362 if (gfc_traverse_expr (expr, sym, forall_index, f))
5369 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5370 to be a scalar INTEGER variable. The subscripts and stride are scalar
5371 INTEGERs, and if stride is a constant it must be nonzero.
5372 Furthermore "A subscript or stride in a forall-triplet-spec shall
5373 not contain a reference to any index-name in the
5374 forall-triplet-spec-list in which it appears." (7.5.4.1) */
5377 resolve_forall_iterators (gfc_forall_iterator *it)
5379 gfc_forall_iterator *iter, *iter2;
5381 for (iter = it; iter; iter = iter->next)
5383 if (gfc_resolve_expr (iter->var) == SUCCESS
5384 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
5385 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
5388 if (gfc_resolve_expr (iter->start) == SUCCESS
5389 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
5390 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
5391 &iter->start->where);
5392 if (iter->var->ts.kind != iter->start->ts.kind)
5393 gfc_convert_type (iter->start, &iter->var->ts, 2);
5395 if (gfc_resolve_expr (iter->end) == SUCCESS
5396 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
5397 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
5399 if (iter->var->ts.kind != iter->end->ts.kind)
5400 gfc_convert_type (iter->end, &iter->var->ts, 2);
5402 if (gfc_resolve_expr (iter->stride) == SUCCESS)
5404 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
5405 gfc_error ("FORALL stride expression at %L must be a scalar %s",
5406 &iter->stride->where, "INTEGER");
5408 if (iter->stride->expr_type == EXPR_CONSTANT
5409 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
5410 gfc_error ("FORALL stride expression at %L cannot be zero",
5411 &iter->stride->where);
5413 if (iter->var->ts.kind != iter->stride->ts.kind)
5414 gfc_convert_type (iter->stride, &iter->var->ts, 2);
5417 for (iter = it; iter; iter = iter->next)
5418 for (iter2 = iter; iter2; iter2 = iter2->next)
5420 if (find_forall_index (iter2->start,
5421 iter->var->symtree->n.sym, 0) == SUCCESS
5422 || find_forall_index (iter2->end,
5423 iter->var->symtree->n.sym, 0) == SUCCESS
5424 || find_forall_index (iter2->stride,
5425 iter->var->symtree->n.sym, 0) == SUCCESS)
5426 gfc_error ("FORALL index '%s' may not appear in triplet "
5427 "specification at %L", iter->var->symtree->name,
5428 &iter2->start->where);
5433 /* Given a pointer to a symbol that is a derived type, see if it's
5434 inaccessible, i.e. if it's defined in another module and the components are
5435 PRIVATE. The search is recursive if necessary. Returns zero if no
5436 inaccessible components are found, nonzero otherwise. */
5439 derived_inaccessible (gfc_symbol *sym)
5443 if (sym->attr.use_assoc && sym->attr.private_comp)
5446 for (c = sym->components; c; c = c->next)
5448 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
5456 /* Resolve the argument of a deallocate expression. The expression must be
5457 a pointer or a full array. */
5460 resolve_deallocate_expr (gfc_expr *e)
5462 symbol_attribute attr;
5463 int allocatable, pointer, check_intent_in;
5466 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5467 check_intent_in = 1;
5469 if (gfc_resolve_expr (e) == FAILURE)
5472 if (e->expr_type != EXPR_VARIABLE)
5475 allocatable = e->symtree->n.sym->attr.allocatable;
5476 pointer = e->symtree->n.sym->attr.pointer;
5477 for (ref = e->ref; ref; ref = ref->next)
5480 check_intent_in = 0;
5485 if (ref->u.ar.type != AR_FULL)
5490 allocatable = (ref->u.c.component->as != NULL
5491 && ref->u.c.component->as->type == AS_DEFERRED);
5492 pointer = ref->u.c.component->attr.pointer;
5501 attr = gfc_expr_attr (e);
5503 if (allocatable == 0 && attr.pointer == 0)
5506 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5511 && e->symtree->n.sym->attr.intent == INTENT_IN)
5513 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
5514 e->symtree->n.sym->name, &e->where);
5522 /* Returns true if the expression e contains a reference to the symbol sym. */
5524 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5526 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
5533 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
5535 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
5539 /* Given the expression node e for an allocatable/pointer of derived type to be
5540 allocated, get the expression node to be initialized afterwards (needed for
5541 derived types with default initializers, and derived types with allocatable
5542 components that need nullification.) */
5545 expr_to_initialize (gfc_expr *e)
5551 result = gfc_copy_expr (e);
5553 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
5554 for (ref = result->ref; ref; ref = ref->next)
5555 if (ref->type == REF_ARRAY && ref->next == NULL)
5557 ref->u.ar.type = AR_FULL;
5559 for (i = 0; i < ref->u.ar.dimen; i++)
5560 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
5562 result->rank = ref->u.ar.dimen;
5570 /* Resolve the expression in an ALLOCATE statement, doing the additional
5571 checks to see whether the expression is OK or not. The expression must
5572 have a trailing array reference that gives the size of the array. */
5575 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
5577 int i, pointer, allocatable, dimension, check_intent_in;
5578 symbol_attribute attr;
5579 gfc_ref *ref, *ref2;
5586 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5587 check_intent_in = 1;
5589 if (gfc_resolve_expr (e) == FAILURE)
5592 /* Make sure the expression is allocatable or a pointer. If it is
5593 pointer, the next-to-last reference must be a pointer. */
5597 if (e->expr_type != EXPR_VARIABLE)
5600 attr = gfc_expr_attr (e);
5601 pointer = attr.pointer;
5602 dimension = attr.dimension;
5606 allocatable = e->symtree->n.sym->attr.allocatable;
5607 pointer = e->symtree->n.sym->attr.pointer;
5608 dimension = e->symtree->n.sym->attr.dimension;
5610 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
5613 check_intent_in = 0;
5618 if (ref->next != NULL)
5623 allocatable = (ref->u.c.component->as != NULL
5624 && ref->u.c.component->as->type == AS_DEFERRED);
5626 pointer = ref->u.c.component->attr.pointer;
5627 dimension = ref->u.c.component->attr.dimension;
5638 if (allocatable == 0 && pointer == 0)
5640 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5646 && e->symtree->n.sym->attr.intent == INTENT_IN)
5648 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
5649 e->symtree->n.sym->name, &e->where);
5653 /* Add default initializer for those derived types that need them. */
5654 if (e->ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&e->ts)))
5656 init_st = gfc_get_code ();
5657 init_st->loc = code->loc;
5658 init_st->op = EXEC_INIT_ASSIGN;
5659 init_st->expr1 = expr_to_initialize (e);
5660 init_st->expr2 = init_e;
5661 init_st->next = code->next;
5662 code->next = init_st;
5665 if (pointer || dimension == 0)
5668 /* Make sure the next-to-last reference node is an array specification. */
5670 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
5672 gfc_error ("Array specification required in ALLOCATE statement "
5673 "at %L", &e->where);
5677 /* Make sure that the array section reference makes sense in the
5678 context of an ALLOCATE specification. */
5682 for (i = 0; i < ar->dimen; i++)
5684 if (ref2->u.ar.type == AR_ELEMENT)
5687 switch (ar->dimen_type[i])
5693 if (ar->start[i] != NULL
5694 && ar->end[i] != NULL
5695 && ar->stride[i] == NULL)
5698 /* Fall Through... */
5702 gfc_error ("Bad array specification in ALLOCATE statement at %L",
5709 for (a = code->ext.alloc_list; a; a = a->next)
5711 sym = a->expr->symtree->n.sym;
5713 /* TODO - check derived type components. */
5714 if (sym->ts.type == BT_DERIVED)
5717 if ((ar->start[i] != NULL
5718 && gfc_find_sym_in_expr (sym, ar->start[i]))
5719 || (ar->end[i] != NULL
5720 && gfc_find_sym_in_expr (sym, ar->end[i])))
5722 gfc_error ("'%s' must not appear in the array specification at "
5723 "%L in the same ALLOCATE statement where it is "
5724 "itself allocated", sym->name, &ar->where);
5734 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
5736 gfc_expr *stat, *errmsg, *pe, *qe;
5737 gfc_alloc *a, *p, *q;
5739 stat = code->expr1 ? code->expr1 : NULL;
5741 errmsg = code->expr2 ? code->expr2 : NULL;
5743 /* Check the stat variable. */
5746 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
5747 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
5748 stat->symtree->n.sym->name, &stat->where);
5750 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
5751 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
5754 if ((stat->ts.type != BT_INTEGER
5755 && !(stat->ref && (stat->ref->type == REF_ARRAY
5756 || stat->ref->type == REF_COMPONENT)))
5758 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
5759 "variable", &stat->where);
5761 for (p = code->ext.alloc_list; p; p = p->next)
5762 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
5763 gfc_error ("Stat-variable at %L shall not be %sd within "
5764 "the same %s statement", &stat->where, fcn, fcn);
5767 /* Check the errmsg variable. */
5771 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
5774 if (errmsg->symtree->n.sym->attr.intent == INTENT_IN)
5775 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
5776 errmsg->symtree->n.sym->name, &errmsg->where);
5778 if (gfc_pure (NULL) && gfc_impure_variable (errmsg->symtree->n.sym))
5779 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
5782 if ((errmsg->ts.type != BT_CHARACTER
5784 && (errmsg->ref->type == REF_ARRAY
5785 || errmsg->ref->type == REF_COMPONENT)))
5786 || errmsg->rank > 0 )
5787 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
5788 "variable", &errmsg->where);
5790 for (p = code->ext.alloc_list; p; p = p->next)
5791 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
5792 gfc_error ("Errmsg-variable at %L shall not be %sd within "
5793 "the same %s statement", &errmsg->where, fcn, fcn);
5796 /* Check that an allocate-object appears only once in the statement.
5797 FIXME: Checking derived types is disabled. */
5798 for (p = code->ext.alloc_list; p; p = p->next)
5801 if ((pe->ref && pe->ref->type != REF_COMPONENT)
5802 && (pe->symtree->n.sym->ts.type != BT_DERIVED))
5804 for (q = p->next; q; q = q->next)
5807 if ((qe->ref && qe->ref->type != REF_COMPONENT)
5808 && (qe->symtree->n.sym->ts.type != BT_DERIVED)
5809 && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
5810 gfc_error ("Allocate-object at %L also appears at %L",
5811 &pe->where, &qe->where);
5816 if (strcmp (fcn, "ALLOCATE") == 0)
5818 for (a = code->ext.alloc_list; a; a = a->next)
5819 resolve_allocate_expr (a->expr, code);
5823 for (a = code->ext.alloc_list; a; a = a->next)
5824 resolve_deallocate_expr (a->expr);
5829 /************ SELECT CASE resolution subroutines ************/
5831 /* Callback function for our mergesort variant. Determines interval
5832 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
5833 op1 > op2. Assumes we're not dealing with the default case.
5834 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
5835 There are nine situations to check. */
5838 compare_cases (const gfc_case *op1, const gfc_case *op2)
5842 if (op1->low == NULL) /* op1 = (:L) */
5844 /* op2 = (:N), so overlap. */
5846 /* op2 = (M:) or (M:N), L < M */
5847 if (op2->low != NULL
5848 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
5851 else if (op1->high == NULL) /* op1 = (K:) */
5853 /* op2 = (M:), so overlap. */
5855 /* op2 = (:N) or (M:N), K > N */
5856 if (op2->high != NULL
5857 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
5860 else /* op1 = (K:L) */
5862 if (op2->low == NULL) /* op2 = (:N), K > N */
5863 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
5865 else if (op2->high == NULL) /* op2 = (M:), L < M */
5866 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
5868 else /* op2 = (M:N) */
5872 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
5875 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
5884 /* Merge-sort a double linked case list, detecting overlap in the
5885 process. LIST is the head of the double linked case list before it
5886 is sorted. Returns the head of the sorted list if we don't see any
5887 overlap, or NULL otherwise. */
5890 check_case_overlap (gfc_case *list)
5892 gfc_case *p, *q, *e, *tail;
5893 int insize, nmerges, psize, qsize, cmp, overlap_seen;
5895 /* If the passed list was empty, return immediately. */
5902 /* Loop unconditionally. The only exit from this loop is a return
5903 statement, when we've finished sorting the case list. */
5910 /* Count the number of merges we do in this pass. */
5913 /* Loop while there exists a merge to be done. */
5918 /* Count this merge. */
5921 /* Cut the list in two pieces by stepping INSIZE places
5922 forward in the list, starting from P. */
5925 for (i = 0; i < insize; i++)
5934 /* Now we have two lists. Merge them! */
5935 while (psize > 0 || (qsize > 0 && q != NULL))
5937 /* See from which the next case to merge comes from. */
5940 /* P is empty so the next case must come from Q. */
5945 else if (qsize == 0 || q == NULL)
5954 cmp = compare_cases (p, q);
5957 /* The whole case range for P is less than the
5965 /* The whole case range for Q is greater than
5966 the case range for P. */
5973 /* The cases overlap, or they are the same
5974 element in the list. Either way, we must
5975 issue an error and get the next case from P. */
5976 /* FIXME: Sort P and Q by line number. */
5977 gfc_error ("CASE label at %L overlaps with CASE "
5978 "label at %L", &p->where, &q->where);
5986 /* Add the next element to the merged list. */
5995 /* P has now stepped INSIZE places along, and so has Q. So
5996 they're the same. */
6001 /* If we have done only one merge or none at all, we've
6002 finished sorting the cases. */
6011 /* Otherwise repeat, merging lists twice the size. */
6017 /* Check to see if an expression is suitable for use in a CASE statement.
6018 Makes sure that all case expressions are scalar constants of the same
6019 type. Return FAILURE if anything is wrong. */
6022 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
6024 if (e == NULL) return SUCCESS;
6026 if (e->ts.type != case_expr->ts.type)
6028 gfc_error ("Expression in CASE statement at %L must be of type %s",
6029 &e->where, gfc_basic_typename (case_expr->ts.type));
6033 /* C805 (R808) For a given case-construct, each case-value shall be of
6034 the same type as case-expr. For character type, length differences
6035 are allowed, but the kind type parameters shall be the same. */
6037 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
6039 gfc_error ("Expression in CASE statement at %L must be of kind %d",
6040 &e->where, case_expr->ts.kind);
6044 /* Convert the case value kind to that of case expression kind, if needed.
6045 FIXME: Should a warning be issued? */
6046 if (e->ts.kind != case_expr->ts.kind)
6047 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
6051 gfc_error ("Expression in CASE statement at %L must be scalar",
6060 /* Given a completely parsed select statement, we:
6062 - Validate all expressions and code within the SELECT.
6063 - Make sure that the selection expression is not of the wrong type.
6064 - Make sure that no case ranges overlap.
6065 - Eliminate unreachable cases and unreachable code resulting from
6066 removing case labels.
6068 The standard does allow unreachable cases, e.g. CASE (5:3). But
6069 they are a hassle for code generation, and to prevent that, we just
6070 cut them out here. This is not necessary for overlapping cases
6071 because they are illegal and we never even try to generate code.
6073 We have the additional caveat that a SELECT construct could have
6074 been a computed GOTO in the source code. Fortunately we can fairly
6075 easily work around that here: The case_expr for a "real" SELECT CASE
6076 is in code->expr1, but for a computed GOTO it is in code->expr2. All
6077 we have to do is make sure that the case_expr is a scalar integer
6081 resolve_select (gfc_code *code)
6084 gfc_expr *case_expr;
6085 gfc_case *cp, *default_case, *tail, *head;
6086 int seen_unreachable;
6092 if (code->expr1 == NULL)
6094 /* This was actually a computed GOTO statement. */
6095 case_expr = code->expr2;
6096 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
6097 gfc_error ("Selection expression in computed GOTO statement "
6098 "at %L must be a scalar integer expression",
6101 /* Further checking is not necessary because this SELECT was built
6102 by the compiler, so it should always be OK. Just move the
6103 case_expr from expr2 to expr so that we can handle computed
6104 GOTOs as normal SELECTs from here on. */
6105 code->expr1 = code->expr2;
6110 case_expr = code->expr1;
6112 type = case_expr->ts.type;
6113 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
6115 gfc_error ("Argument of SELECT statement at %L cannot be %s",
6116 &case_expr->where, gfc_typename (&case_expr->ts));
6118 /* Punt. Going on here just produce more garbage error messages. */
6122 if (case_expr->rank != 0)
6124 gfc_error ("Argument of SELECT statement at %L must be a scalar "
6125 "expression", &case_expr->where);
6131 /* PR 19168 has a long discussion concerning a mismatch of the kinds
6132 of the SELECT CASE expression and its CASE values. Walk the lists
6133 of case values, and if we find a mismatch, promote case_expr to
6134 the appropriate kind. */
6136 if (type == BT_LOGICAL || type == BT_INTEGER)
6138 for (body = code->block; body; body = body->block)
6140 /* Walk the case label list. */
6141 for (cp = body->ext.case_list; cp; cp = cp->next)
6143 /* Intercept the DEFAULT case. It does not have a kind. */
6144 if (cp->low == NULL && cp->high == NULL)
6147 /* Unreachable case ranges are discarded, so ignore. */
6148 if (cp->low != NULL && cp->high != NULL
6149 && cp->low != cp->high
6150 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6153 /* FIXME: Should a warning be issued? */
6155 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
6156 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
6158 if (cp->high != NULL
6159 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
6160 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
6165 /* Assume there is no DEFAULT case. */
6166 default_case = NULL;
6171 for (body = code->block; body; body = body->block)
6173 /* Assume the CASE list is OK, and all CASE labels can be matched. */
6175 seen_unreachable = 0;
6177 /* Walk the case label list, making sure that all case labels
6179 for (cp = body->ext.case_list; cp; cp = cp->next)
6181 /* Count the number of cases in the whole construct. */
6184 /* Intercept the DEFAULT case. */
6185 if (cp->low == NULL && cp->high == NULL)
6187 if (default_case != NULL)
6189 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6190 "by a second DEFAULT CASE at %L",
6191 &default_case->where, &cp->where);
6202 /* Deal with single value cases and case ranges. Errors are
6203 issued from the validation function. */
6204 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
6205 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
6211 if (type == BT_LOGICAL
6212 && ((cp->low == NULL || cp->high == NULL)
6213 || cp->low != cp->high))
6215 gfc_error ("Logical range in CASE statement at %L is not "
6216 "allowed", &cp->low->where);
6221 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
6224 value = cp->low->value.logical == 0 ? 2 : 1;
6225 if (value & seen_logical)
6227 gfc_error ("constant logical value in CASE statement "
6228 "is repeated at %L",
6233 seen_logical |= value;
6236 if (cp->low != NULL && cp->high != NULL
6237 && cp->low != cp->high
6238 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6240 if (gfc_option.warn_surprising)
6241 gfc_warning ("Range specification at %L can never "
6242 "be matched", &cp->where);
6244 cp->unreachable = 1;
6245 seen_unreachable = 1;
6249 /* If the case range can be matched, it can also overlap with
6250 other cases. To make sure it does not, we put it in a
6251 double linked list here. We sort that with a merge sort
6252 later on to detect any overlapping cases. */
6256 head->right = head->left = NULL;
6261 tail->right->left = tail;
6268 /* It there was a failure in the previous case label, give up
6269 for this case label list. Continue with the next block. */
6273 /* See if any case labels that are unreachable have been seen.
6274 If so, we eliminate them. This is a bit of a kludge because
6275 the case lists for a single case statement (label) is a
6276 single forward linked lists. */
6277 if (seen_unreachable)
6279 /* Advance until the first case in the list is reachable. */
6280 while (body->ext.case_list != NULL
6281 && body->ext.case_list->unreachable)
6283 gfc_case *n = body->ext.case_list;
6284 body->ext.case_list = body->ext.case_list->next;
6286 gfc_free_case_list (n);
6289 /* Strip all other unreachable cases. */
6290 if (body->ext.case_list)
6292 for (cp = body->ext.case_list; cp->next; cp = cp->next)
6294 if (cp->next->unreachable)
6296 gfc_case *n = cp->next;
6297 cp->next = cp->next->next;
6299 gfc_free_case_list (n);
6306 /* See if there were overlapping cases. If the check returns NULL,
6307 there was overlap. In that case we don't do anything. If head
6308 is non-NULL, we prepend the DEFAULT case. The sorted list can
6309 then used during code generation for SELECT CASE constructs with
6310 a case expression of a CHARACTER type. */
6313 head = check_case_overlap (head);
6315 /* Prepend the default_case if it is there. */
6316 if (head != NULL && default_case)
6318 default_case->left = NULL;
6319 default_case->right = head;
6320 head->left = default_case;
6324 /* Eliminate dead blocks that may be the result if we've seen
6325 unreachable case labels for a block. */
6326 for (body = code; body && body->block; body = body->block)
6328 if (body->block->ext.case_list == NULL)
6330 /* Cut the unreachable block from the code chain. */
6331 gfc_code *c = body->block;
6332 body->block = c->block;
6334 /* Kill the dead block, but not the blocks below it. */
6336 gfc_free_statements (c);
6340 /* More than two cases is legal but insane for logical selects.
6341 Issue a warning for it. */
6342 if (gfc_option.warn_surprising && type == BT_LOGICAL
6344 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
6349 /* Resolve a transfer statement. This is making sure that:
6350 -- a derived type being transferred has only non-pointer components
6351 -- a derived type being transferred doesn't have private components, unless
6352 it's being transferred from the module where the type was defined
6353 -- we're not trying to transfer a whole assumed size array. */
6356 resolve_transfer (gfc_code *code)
6365 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
6368 sym = exp->symtree->n.sym;
6371 /* Go to actual component transferred. */
6372 for (ref = code->expr1->ref; ref; ref = ref->next)
6373 if (ref->type == REF_COMPONENT)
6374 ts = &ref->u.c.component->ts;
6376 if (ts->type == BT_DERIVED)
6378 /* Check that transferred derived type doesn't contain POINTER
6380 if (ts->u.derived->attr.pointer_comp)
6382 gfc_error ("Data transfer element at %L cannot have "
6383 "POINTER components", &code->loc);
6387 if (ts->u.derived->attr.alloc_comp)
6389 gfc_error ("Data transfer element at %L cannot have "
6390 "ALLOCATABLE components", &code->loc);
6394 if (derived_inaccessible (ts->u.derived))
6396 gfc_error ("Data transfer element at %L cannot have "
6397 "PRIVATE components",&code->loc);
6402 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
6403 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
6405 gfc_error ("Data transfer element at %L cannot be a full reference to "
6406 "an assumed-size array", &code->loc);
6412 /*********** Toplevel code resolution subroutines ***********/
6414 /* Find the set of labels that are reachable from this block. We also
6415 record the last statement in each block. */
6418 find_reachable_labels (gfc_code *block)
6425 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
6427 /* Collect labels in this block. We don't keep those corresponding
6428 to END {IF|SELECT}, these are checked in resolve_branch by going
6429 up through the code_stack. */
6430 for (c = block; c; c = c->next)
6432 if (c->here && c->op != EXEC_END_BLOCK)
6433 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
6436 /* Merge with labels from parent block. */
6439 gcc_assert (cs_base->prev->reachable_labels);
6440 bitmap_ior_into (cs_base->reachable_labels,
6441 cs_base->prev->reachable_labels);
6445 /* Given a branch to a label, see if the branch is conforming.
6446 The code node describes where the branch is located. */
6449 resolve_branch (gfc_st_label *label, gfc_code *code)
6456 /* Step one: is this a valid branching target? */
6458 if (label->defined == ST_LABEL_UNKNOWN)
6460 gfc_error ("Label %d referenced at %L is never defined", label->value,
6465 if (label->defined != ST_LABEL_TARGET)
6467 gfc_error ("Statement at %L is not a valid branch target statement "
6468 "for the branch statement at %L", &label->where, &code->loc);
6472 /* Step two: make sure this branch is not a branch to itself ;-) */
6474 if (code->here == label)
6476 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
6480 /* Step three: See if the label is in the same block as the
6481 branching statement. The hard work has been done by setting up
6482 the bitmap reachable_labels. */
6484 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
6487 /* Step four: If we haven't found the label in the bitmap, it may
6488 still be the label of the END of the enclosing block, in which
6489 case we find it by going up the code_stack. */
6491 for (stack = cs_base; stack; stack = stack->prev)
6492 if (stack->current->next && stack->current->next->here == label)
6497 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
6501 /* The label is not in an enclosing block, so illegal. This was
6502 allowed in Fortran 66, so we allow it as extension. No
6503 further checks are necessary in this case. */
6504 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
6505 "as the GOTO statement at %L", &label->where,
6511 /* Check whether EXPR1 has the same shape as EXPR2. */
6514 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
6516 mpz_t shape[GFC_MAX_DIMENSIONS];
6517 mpz_t shape2[GFC_MAX_DIMENSIONS];
6518 gfc_try result = FAILURE;
6521 /* Compare the rank. */
6522 if (expr1->rank != expr2->rank)
6525 /* Compare the size of each dimension. */
6526 for (i=0; i<expr1->rank; i++)
6528 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
6531 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
6534 if (mpz_cmp (shape[i], shape2[i]))
6538 /* When either of the two expression is an assumed size array, we
6539 ignore the comparison of dimension sizes. */
6544 for (i--; i >= 0; i--)
6546 mpz_clear (shape[i]);
6547 mpz_clear (shape2[i]);
6553 /* Check whether a WHERE assignment target or a WHERE mask expression
6554 has the same shape as the outmost WHERE mask expression. */
6557 resolve_where (gfc_code *code, gfc_expr *mask)
6563 cblock = code->block;
6565 /* Store the first WHERE mask-expr of the WHERE statement or construct.
6566 In case of nested WHERE, only the outmost one is stored. */
6567 if (mask == NULL) /* outmost WHERE */
6569 else /* inner WHERE */
6576 /* Check if the mask-expr has a consistent shape with the
6577 outmost WHERE mask-expr. */
6578 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
6579 gfc_error ("WHERE mask at %L has inconsistent shape",
6580 &cblock->expr1->where);
6583 /* the assignment statement of a WHERE statement, or the first
6584 statement in where-body-construct of a WHERE construct */
6585 cnext = cblock->next;
6590 /* WHERE assignment statement */
6593 /* Check shape consistent for WHERE assignment target. */
6594 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
6595 gfc_error ("WHERE assignment target at %L has "
6596 "inconsistent shape", &cnext->expr1->where);
6600 case EXEC_ASSIGN_CALL:
6601 resolve_call (cnext);
6602 if (!cnext->resolved_sym->attr.elemental)
6603 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
6604 &cnext->ext.actual->expr->where);
6607 /* WHERE or WHERE construct is part of a where-body-construct */
6609 resolve_where (cnext, e);
6613 gfc_error ("Unsupported statement inside WHERE at %L",
6616 /* the next statement within the same where-body-construct */
6617 cnext = cnext->next;
6619 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
6620 cblock = cblock->block;
6625 /* Resolve assignment in FORALL construct.
6626 NVAR is the number of FORALL index variables, and VAR_EXPR records the
6627 FORALL index variables. */
6630 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
6634 for (n = 0; n < nvar; n++)
6636 gfc_symbol *forall_index;
6638 forall_index = var_expr[n]->symtree->n.sym;
6640 /* Check whether the assignment target is one of the FORALL index
6642 if ((code->expr1->expr_type == EXPR_VARIABLE)
6643 && (code->expr1->symtree->n.sym == forall_index))
6644 gfc_error ("Assignment to a FORALL index variable at %L",
6645 &code->expr1->where);
6648 /* If one of the FORALL index variables doesn't appear in the
6649 assignment variable, then there could be a many-to-one
6650 assignment. Emit a warning rather than an error because the
6651 mask could be resolving this problem. */
6652 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
6653 gfc_warning ("The FORALL with index '%s' is not used on the "
6654 "left side of the assignment at %L and so might "
6655 "cause multiple assignment to this object",
6656 var_expr[n]->symtree->name, &code->expr1->where);
6662 /* Resolve WHERE statement in FORALL construct. */
6665 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
6666 gfc_expr **var_expr)
6671 cblock = code->block;
6674 /* the assignment statement of a WHERE statement, or the first
6675 statement in where-body-construct of a WHERE construct */
6676 cnext = cblock->next;
6681 /* WHERE assignment statement */
6683 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
6686 /* WHERE operator assignment statement */
6687 case EXEC_ASSIGN_CALL:
6688 resolve_call (cnext);
6689 if (!cnext->resolved_sym->attr.elemental)
6690 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
6691 &cnext->ext.actual->expr->where);
6694 /* WHERE or WHERE construct is part of a where-body-construct */
6696 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
6700 gfc_error ("Unsupported statement inside WHERE at %L",
6703 /* the next statement within the same where-body-construct */
6704 cnext = cnext->next;
6706 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
6707 cblock = cblock->block;
6712 /* Traverse the FORALL body to check whether the following errors exist:
6713 1. For assignment, check if a many-to-one assignment happens.
6714 2. For WHERE statement, check the WHERE body to see if there is any
6715 many-to-one assignment. */
6718 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
6722 c = code->block->next;
6728 case EXEC_POINTER_ASSIGN:
6729 gfc_resolve_assign_in_forall (c, nvar, var_expr);
6732 case EXEC_ASSIGN_CALL:
6736 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
6737 there is no need to handle it here. */
6741 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
6746 /* The next statement in the FORALL body. */
6752 /* Counts the number of iterators needed inside a forall construct, including
6753 nested forall constructs. This is used to allocate the needed memory
6754 in gfc_resolve_forall. */
6757 gfc_count_forall_iterators (gfc_code *code)
6759 int max_iters, sub_iters, current_iters;
6760 gfc_forall_iterator *fa;
6762 gcc_assert(code->op == EXEC_FORALL);
6766 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
6769 code = code->block->next;
6773 if (code->op == EXEC_FORALL)
6775 sub_iters = gfc_count_forall_iterators (code);
6776 if (sub_iters > max_iters)
6777 max_iters = sub_iters;
6782 return current_iters + max_iters;
6786 /* Given a FORALL construct, first resolve the FORALL iterator, then call
6787 gfc_resolve_forall_body to resolve the FORALL body. */
6790 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
6792 static gfc_expr **var_expr;
6793 static int total_var = 0;
6794 static int nvar = 0;
6796 gfc_forall_iterator *fa;
6801 /* Start to resolve a FORALL construct */
6802 if (forall_save == 0)
6804 /* Count the total number of FORALL index in the nested FORALL
6805 construct in order to allocate the VAR_EXPR with proper size. */
6806 total_var = gfc_count_forall_iterators (code);
6808 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
6809 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
6812 /* The information about FORALL iterator, including FORALL index start, end
6813 and stride. The FORALL index can not appear in start, end or stride. */
6814 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
6816 /* Check if any outer FORALL index name is the same as the current
6818 for (i = 0; i < nvar; i++)
6820 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
6822 gfc_error ("An outer FORALL construct already has an index "
6823 "with this name %L", &fa->var->where);
6827 /* Record the current FORALL index. */
6828 var_expr[nvar] = gfc_copy_expr (fa->var);
6832 /* No memory leak. */
6833 gcc_assert (nvar <= total_var);
6836 /* Resolve the FORALL body. */
6837 gfc_resolve_forall_body (code, nvar, var_expr);
6839 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
6840 gfc_resolve_blocks (code->block, ns);
6844 /* Free only the VAR_EXPRs allocated in this frame. */
6845 for (i = nvar; i < tmp; i++)
6846 gfc_free_expr (var_expr[i]);
6850 /* We are in the outermost FORALL construct. */
6851 gcc_assert (forall_save == 0);
6853 /* VAR_EXPR is not needed any more. */
6854 gfc_free (var_expr);
6860 /* Resolve a BLOCK construct statement. */
6863 resolve_block_construct (gfc_code* code)
6865 /* Eventually, we may want to do some checks here or handle special stuff.
6866 But so far the only thing we can do is resolving the local namespace. */
6868 gfc_resolve (code->ext.ns);
6872 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
6875 static void resolve_code (gfc_code *, gfc_namespace *);
6878 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
6882 for (; b; b = b->block)
6884 t = gfc_resolve_expr (b->expr1);
6885 if (gfc_resolve_expr (b->expr2) == FAILURE)
6891 if (t == SUCCESS && b->expr1 != NULL
6892 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
6893 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
6900 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
6901 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
6906 resolve_branch (b->label1, b);
6910 resolve_block_construct (b);
6923 case EXEC_OMP_ATOMIC:
6924 case EXEC_OMP_CRITICAL:
6926 case EXEC_OMP_MASTER:
6927 case EXEC_OMP_ORDERED:
6928 case EXEC_OMP_PARALLEL:
6929 case EXEC_OMP_PARALLEL_DO:
6930 case EXEC_OMP_PARALLEL_SECTIONS:
6931 case EXEC_OMP_PARALLEL_WORKSHARE:
6932 case EXEC_OMP_SECTIONS:
6933 case EXEC_OMP_SINGLE:
6935 case EXEC_OMP_TASKWAIT:
6936 case EXEC_OMP_WORKSHARE:
6940 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
6943 resolve_code (b->next, ns);
6948 /* Does everything to resolve an ordinary assignment. Returns true
6949 if this is an interface assignment. */
6951 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
6961 if (gfc_extend_assign (code, ns) == SUCCESS)
6963 gfc_symbol* assign_proc;
6966 if (code->op == EXEC_ASSIGN_CALL)
6968 lhs = code->ext.actual->expr;
6969 rhsptr = &code->ext.actual->next->expr;
6970 assign_proc = code->symtree->n.sym;
6974 gfc_actual_arglist* args;
6975 gfc_typebound_proc* tbp;
6977 gcc_assert (code->op == EXEC_COMPCALL);
6979 args = code->expr1->value.compcall.actual;
6981 rhsptr = &args->next->expr;
6983 tbp = code->expr1->value.compcall.tbp;
6984 gcc_assert (!tbp->is_generic);
6985 assign_proc = tbp->u.specific->n.sym;
6988 /* Make a temporary rhs when there is a default initializer
6989 and rhs is the same symbol as the lhs. */
6990 if ((*rhsptr)->expr_type == EXPR_VARIABLE
6991 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
6992 && has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
6993 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
6994 *rhsptr = gfc_get_parentheses (*rhsptr);
7003 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
7004 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
7005 &code->loc) == FAILURE)
7008 /* Handle the case of a BOZ literal on the RHS. */
7009 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
7012 if (gfc_option.warn_surprising)
7013 gfc_warning ("BOZ literal at %L is bitwise transferred "
7014 "non-integer symbol '%s'", &code->loc,
7015 lhs->symtree->n.sym->name);
7017 if (!gfc_convert_boz (rhs, &lhs->ts))
7019 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
7021 if (rc == ARITH_UNDERFLOW)
7022 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
7023 ". This check can be disabled with the option "
7024 "-fno-range-check", &rhs->where);
7025 else if (rc == ARITH_OVERFLOW)
7026 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
7027 ". This check can be disabled with the option "
7028 "-fno-range-check", &rhs->where);
7029 else if (rc == ARITH_NAN)
7030 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
7031 ". This check can be disabled with the option "
7032 "-fno-range-check", &rhs->where);
7038 if (lhs->ts.type == BT_CHARACTER
7039 && gfc_option.warn_character_truncation)
7041 if (lhs->ts.u.cl != NULL
7042 && lhs->ts.u.cl->length != NULL
7043 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7044 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
7046 if (rhs->expr_type == EXPR_CONSTANT)
7047 rlen = rhs->value.character.length;
7049 else if (rhs->ts.u.cl != NULL
7050 && rhs->ts.u.cl->length != NULL
7051 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7052 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
7054 if (rlen && llen && rlen > llen)
7055 gfc_warning_now ("CHARACTER expression will be truncated "
7056 "in assignment (%d/%d) at %L",
7057 llen, rlen, &code->loc);
7060 /* Ensure that a vector index expression for the lvalue is evaluated
7061 to a temporary if the lvalue symbol is referenced in it. */
7064 for (ref = lhs->ref; ref; ref= ref->next)
7065 if (ref->type == REF_ARRAY)
7067 for (n = 0; n < ref->u.ar.dimen; n++)
7068 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
7069 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
7070 ref->u.ar.start[n]))
7072 = gfc_get_parentheses (ref->u.ar.start[n]);
7076 if (gfc_pure (NULL))
7078 if (gfc_impure_variable (lhs->symtree->n.sym))
7080 gfc_error ("Cannot assign to variable '%s' in PURE "
7082 lhs->symtree->n.sym->name,
7087 if (lhs->ts.type == BT_DERIVED
7088 && lhs->expr_type == EXPR_VARIABLE
7089 && lhs->ts.u.derived->attr.pointer_comp
7090 && gfc_impure_variable (rhs->symtree->n.sym))
7092 gfc_error ("The impure variable at %L is assigned to "
7093 "a derived type variable with a POINTER "
7094 "component in a PURE procedure (12.6)",
7100 gfc_check_assign (lhs, rhs, 1);
7105 /* Given a block of code, recursively resolve everything pointed to by this
7109 resolve_code (gfc_code *code, gfc_namespace *ns)
7111 int omp_workshare_save;
7116 frame.prev = cs_base;
7120 find_reachable_labels (code);
7122 for (; code; code = code->next)
7124 frame.current = code;
7125 forall_save = forall_flag;
7127 if (code->op == EXEC_FORALL)
7130 gfc_resolve_forall (code, ns, forall_save);
7133 else if (code->block)
7135 omp_workshare_save = -1;
7138 case EXEC_OMP_PARALLEL_WORKSHARE:
7139 omp_workshare_save = omp_workshare_flag;
7140 omp_workshare_flag = 1;
7141 gfc_resolve_omp_parallel_blocks (code, ns);
7143 case EXEC_OMP_PARALLEL:
7144 case EXEC_OMP_PARALLEL_DO:
7145 case EXEC_OMP_PARALLEL_SECTIONS:
7147 omp_workshare_save = omp_workshare_flag;
7148 omp_workshare_flag = 0;
7149 gfc_resolve_omp_parallel_blocks (code, ns);
7152 gfc_resolve_omp_do_blocks (code, ns);
7154 case EXEC_OMP_WORKSHARE:
7155 omp_workshare_save = omp_workshare_flag;
7156 omp_workshare_flag = 1;
7159 gfc_resolve_blocks (code->block, ns);
7163 if (omp_workshare_save != -1)
7164 omp_workshare_flag = omp_workshare_save;
7168 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
7169 t = gfc_resolve_expr (code->expr1);
7170 forall_flag = forall_save;
7172 if (gfc_resolve_expr (code->expr2) == FAILURE)
7178 case EXEC_END_BLOCK:
7185 case EXEC_ASSIGN_CALL:
7189 /* Keep track of which entry we are up to. */
7190 current_entry_id = code->ext.entry->id;
7194 resolve_where (code, NULL);
7198 if (code->expr1 != NULL)
7200 if (code->expr1->ts.type != BT_INTEGER)
7201 gfc_error ("ASSIGNED GOTO statement at %L requires an "
7202 "INTEGER variable", &code->expr1->where);
7203 else if (code->expr1->symtree->n.sym->attr.assign != 1)
7204 gfc_error ("Variable '%s' has not been assigned a target "
7205 "label at %L", code->expr1->symtree->n.sym->name,
7206 &code->expr1->where);
7209 resolve_branch (code->label1, code);
7213 if (code->expr1 != NULL
7214 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
7215 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
7216 "INTEGER return specifier", &code->expr1->where);
7219 case EXEC_INIT_ASSIGN:
7220 case EXEC_END_PROCEDURE:
7227 if (resolve_ordinary_assign (code, ns))
7229 if (code->op == EXEC_COMPCALL)
7237 case EXEC_LABEL_ASSIGN:
7238 if (code->label1->defined == ST_LABEL_UNKNOWN)
7239 gfc_error ("Label %d referenced at %L is never defined",
7240 code->label1->value, &code->label1->where);
7242 && (code->expr1->expr_type != EXPR_VARIABLE
7243 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
7244 || code->expr1->symtree->n.sym->ts.kind
7245 != gfc_default_integer_kind
7246 || code->expr1->symtree->n.sym->as != NULL))
7247 gfc_error ("ASSIGN statement at %L requires a scalar "
7248 "default INTEGER variable", &code->expr1->where);
7251 case EXEC_POINTER_ASSIGN:
7255 gfc_check_pointer_assign (code->expr1, code->expr2);
7258 case EXEC_ARITHMETIC_IF:
7260 && code->expr1->ts.type != BT_INTEGER
7261 && code->expr1->ts.type != BT_REAL)
7262 gfc_error ("Arithmetic IF statement at %L requires a numeric "
7263 "expression", &code->expr1->where);
7265 resolve_branch (code->label1, code);
7266 resolve_branch (code->label2, code);
7267 resolve_branch (code->label3, code);
7271 if (t == SUCCESS && code->expr1 != NULL
7272 && (code->expr1->ts.type != BT_LOGICAL
7273 || code->expr1->rank != 0))
7274 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
7275 &code->expr1->where);
7280 resolve_call (code);
7285 resolve_typebound_call (code);
7289 resolve_ppc_call (code);
7293 /* Select is complicated. Also, a SELECT construct could be
7294 a transformed computed GOTO. */
7295 resolve_select (code);
7299 gfc_resolve (code->ext.ns);
7303 if (code->ext.iterator != NULL)
7305 gfc_iterator *iter = code->ext.iterator;
7306 if (gfc_resolve_iterator (iter, true) != FAILURE)
7307 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
7312 if (code->expr1 == NULL)
7313 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
7315 && (code->expr1->rank != 0
7316 || code->expr1->ts.type != BT_LOGICAL))
7317 gfc_error ("Exit condition of DO WHILE loop at %L must be "
7318 "a scalar LOGICAL expression", &code->expr1->where);
7323 resolve_allocate_deallocate (code, "ALLOCATE");
7327 case EXEC_DEALLOCATE:
7329 resolve_allocate_deallocate (code, "DEALLOCATE");
7334 if (gfc_resolve_open (code->ext.open) == FAILURE)
7337 resolve_branch (code->ext.open->err, code);
7341 if (gfc_resolve_close (code->ext.close) == FAILURE)
7344 resolve_branch (code->ext.close->err, code);
7347 case EXEC_BACKSPACE:
7351 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
7354 resolve_branch (code->ext.filepos->err, code);
7358 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
7361 resolve_branch (code->ext.inquire->err, code);
7365 gcc_assert (code->ext.inquire != NULL);
7366 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
7369 resolve_branch (code->ext.inquire->err, code);
7373 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
7376 resolve_branch (code->ext.wait->err, code);
7377 resolve_branch (code->ext.wait->end, code);
7378 resolve_branch (code->ext.wait->eor, code);
7383 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
7386 resolve_branch (code->ext.dt->err, code);
7387 resolve_branch (code->ext.dt->end, code);
7388 resolve_branch (code->ext.dt->eor, code);
7392 resolve_transfer (code);
7396 resolve_forall_iterators (code->ext.forall_iterator);
7398 if (code->expr1 != NULL && code->expr1->ts.type != BT_LOGICAL)
7399 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
7400 "expression", &code->expr1->where);
7403 case EXEC_OMP_ATOMIC:
7404 case EXEC_OMP_BARRIER:
7405 case EXEC_OMP_CRITICAL:
7406 case EXEC_OMP_FLUSH:
7408 case EXEC_OMP_MASTER:
7409 case EXEC_OMP_ORDERED:
7410 case EXEC_OMP_SECTIONS:
7411 case EXEC_OMP_SINGLE:
7412 case EXEC_OMP_TASKWAIT:
7413 case EXEC_OMP_WORKSHARE:
7414 gfc_resolve_omp_directive (code, ns);
7417 case EXEC_OMP_PARALLEL:
7418 case EXEC_OMP_PARALLEL_DO:
7419 case EXEC_OMP_PARALLEL_SECTIONS:
7420 case EXEC_OMP_PARALLEL_WORKSHARE:
7422 omp_workshare_save = omp_workshare_flag;
7423 omp_workshare_flag = 0;
7424 gfc_resolve_omp_directive (code, ns);
7425 omp_workshare_flag = omp_workshare_save;
7429 gfc_internal_error ("resolve_code(): Bad statement code");
7433 cs_base = frame.prev;
7437 /* Resolve initial values and make sure they are compatible with
7441 resolve_values (gfc_symbol *sym)
7443 if (sym->value == NULL)
7446 if (gfc_resolve_expr (sym->value) == FAILURE)
7449 gfc_check_assign_symbol (sym, sym->value);
7453 /* Verify the binding labels for common blocks that are BIND(C). The label
7454 for a BIND(C) common block must be identical in all scoping units in which
7455 the common block is declared. Further, the binding label can not collide
7456 with any other global entity in the program. */
7459 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
7461 if (comm_block_tree->n.common->is_bind_c == 1)
7463 gfc_gsymbol *binding_label_gsym;
7464 gfc_gsymbol *comm_name_gsym;
7466 /* See if a global symbol exists by the common block's name. It may
7467 be NULL if the common block is use-associated. */
7468 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
7469 comm_block_tree->n.common->name);
7470 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
7471 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
7472 "with the global entity '%s' at %L",
7473 comm_block_tree->n.common->binding_label,
7474 comm_block_tree->n.common->name,
7475 &(comm_block_tree->n.common->where),
7476 comm_name_gsym->name, &(comm_name_gsym->where));
7477 else if (comm_name_gsym != NULL
7478 && strcmp (comm_name_gsym->name,
7479 comm_block_tree->n.common->name) == 0)
7481 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
7483 if (comm_name_gsym->binding_label == NULL)
7484 /* No binding label for common block stored yet; save this one. */
7485 comm_name_gsym->binding_label =
7486 comm_block_tree->n.common->binding_label;
7488 if (strcmp (comm_name_gsym->binding_label,
7489 comm_block_tree->n.common->binding_label) != 0)
7491 /* Common block names match but binding labels do not. */
7492 gfc_error ("Binding label '%s' for common block '%s' at %L "
7493 "does not match the binding label '%s' for common "
7495 comm_block_tree->n.common->binding_label,
7496 comm_block_tree->n.common->name,
7497 &(comm_block_tree->n.common->where),
7498 comm_name_gsym->binding_label,
7499 comm_name_gsym->name,
7500 &(comm_name_gsym->where));
7505 /* There is no binding label (NAME="") so we have nothing further to
7506 check and nothing to add as a global symbol for the label. */
7507 if (comm_block_tree->n.common->binding_label[0] == '\0' )
7510 binding_label_gsym =
7511 gfc_find_gsymbol (gfc_gsym_root,
7512 comm_block_tree->n.common->binding_label);
7513 if (binding_label_gsym == NULL)
7515 /* Need to make a global symbol for the binding label to prevent
7516 it from colliding with another. */
7517 binding_label_gsym =
7518 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
7519 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
7520 binding_label_gsym->type = GSYM_COMMON;
7524 /* If comm_name_gsym is NULL, the name common block is use
7525 associated and the name could be colliding. */
7526 if (binding_label_gsym->type != GSYM_COMMON)
7527 gfc_error ("Binding label '%s' for common block '%s' at %L "
7528 "collides with the global entity '%s' at %L",
7529 comm_block_tree->n.common->binding_label,
7530 comm_block_tree->n.common->name,
7531 &(comm_block_tree->n.common->where),
7532 binding_label_gsym->name,
7533 &(binding_label_gsym->where));
7534 else if (comm_name_gsym != NULL
7535 && (strcmp (binding_label_gsym->name,
7536 comm_name_gsym->binding_label) != 0)
7537 && (strcmp (binding_label_gsym->sym_name,
7538 comm_name_gsym->name) != 0))
7539 gfc_error ("Binding label '%s' for common block '%s' at %L "
7540 "collides with global entity '%s' at %L",
7541 binding_label_gsym->name, binding_label_gsym->sym_name,
7542 &(comm_block_tree->n.common->where),
7543 comm_name_gsym->name, &(comm_name_gsym->where));
7551 /* Verify any BIND(C) derived types in the namespace so we can report errors
7552 for them once, rather than for each variable declared of that type. */
7555 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
7557 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
7558 && derived_sym->attr.is_bind_c == 1)
7559 verify_bind_c_derived_type (derived_sym);
7565 /* Verify that any binding labels used in a given namespace do not collide
7566 with the names or binding labels of any global symbols. */
7569 gfc_verify_binding_labels (gfc_symbol *sym)
7573 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
7574 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
7576 gfc_gsymbol *bind_c_sym;
7578 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
7579 if (bind_c_sym != NULL
7580 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
7582 if (sym->attr.if_source == IFSRC_DECL
7583 && (bind_c_sym->type != GSYM_SUBROUTINE
7584 && bind_c_sym->type != GSYM_FUNCTION)
7585 && ((sym->attr.contained == 1
7586 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
7587 || (sym->attr.use_assoc == 1
7588 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
7590 /* Make sure global procedures don't collide with anything. */
7591 gfc_error ("Binding label '%s' at %L collides with the global "
7592 "entity '%s' at %L", sym->binding_label,
7593 &(sym->declared_at), bind_c_sym->name,
7594 &(bind_c_sym->where));
7597 else if (sym->attr.contained == 0
7598 && (sym->attr.if_source == IFSRC_IFBODY
7599 && sym->attr.flavor == FL_PROCEDURE)
7600 && (bind_c_sym->sym_name != NULL
7601 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
7603 /* Make sure procedures in interface bodies don't collide. */
7604 gfc_error ("Binding label '%s' in interface body at %L collides "
7605 "with the global entity '%s' at %L",
7607 &(sym->declared_at), bind_c_sym->name,
7608 &(bind_c_sym->where));
7611 else if (sym->attr.contained == 0
7612 && sym->attr.if_source == IFSRC_UNKNOWN)
7613 if ((sym->attr.use_assoc && bind_c_sym->mod_name
7614 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
7615 || sym->attr.use_assoc == 0)
7617 gfc_error ("Binding label '%s' at %L collides with global "
7618 "entity '%s' at %L", sym->binding_label,
7619 &(sym->declared_at), bind_c_sym->name,
7620 &(bind_c_sym->where));
7625 /* Clear the binding label to prevent checking multiple times. */
7626 sym->binding_label[0] = '\0';
7628 else if (bind_c_sym == NULL)
7630 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
7631 bind_c_sym->where = sym->declared_at;
7632 bind_c_sym->sym_name = sym->name;
7634 if (sym->attr.use_assoc == 1)
7635 bind_c_sym->mod_name = sym->module;
7637 if (sym->ns->proc_name != NULL)
7638 bind_c_sym->mod_name = sym->ns->proc_name->name;
7640 if (sym->attr.contained == 0)
7642 if (sym->attr.subroutine)
7643 bind_c_sym->type = GSYM_SUBROUTINE;
7644 else if (sym->attr.function)
7645 bind_c_sym->type = GSYM_FUNCTION;
7653 /* Resolve an index expression. */
7656 resolve_index_expr (gfc_expr *e)
7658 if (gfc_resolve_expr (e) == FAILURE)
7661 if (gfc_simplify_expr (e, 0) == FAILURE)
7664 if (gfc_specification_expr (e) == FAILURE)
7670 /* Resolve a charlen structure. */
7673 resolve_charlen (gfc_charlen *cl)
7682 specification_expr = 1;
7684 if (resolve_index_expr (cl->length) == FAILURE)
7686 specification_expr = 0;
7690 /* "If the character length parameter value evaluates to a negative
7691 value, the length of character entities declared is zero." */
7692 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
7694 gfc_warning_now ("CHARACTER variable has zero length at %L",
7695 &cl->length->where);
7696 gfc_replace_expr (cl->length, gfc_int_expr (0));
7699 /* Check that the character length is not too large. */
7700 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
7701 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
7702 && cl->length->ts.type == BT_INTEGER
7703 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
7705 gfc_error ("String length at %L is too large", &cl->length->where);
7713 /* Test for non-constant shape arrays. */
7716 is_non_constant_shape_array (gfc_symbol *sym)
7722 not_constant = false;
7723 if (sym->as != NULL)
7725 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
7726 has not been simplified; parameter array references. Do the
7727 simplification now. */
7728 for (i = 0; i < sym->as->rank; i++)
7730 e = sym->as->lower[i];
7731 if (e && (resolve_index_expr (e) == FAILURE
7732 || !gfc_is_constant_expr (e)))
7733 not_constant = true;
7735 e = sym->as->upper[i];
7736 if (e && (resolve_index_expr (e) == FAILURE
7737 || !gfc_is_constant_expr (e)))
7738 not_constant = true;
7741 return not_constant;
7744 /* Given a symbol and an initialization expression, add code to initialize
7745 the symbol to the function entry. */
7747 build_init_assign (gfc_symbol *sym, gfc_expr *init)
7751 gfc_namespace *ns = sym->ns;
7753 /* Search for the function namespace if this is a contained
7754 function without an explicit result. */
7755 if (sym->attr.function && sym == sym->result
7756 && sym->name != sym->ns->proc_name->name)
7759 for (;ns; ns = ns->sibling)
7760 if (strcmp (ns->proc_name->name, sym->name) == 0)
7766 gfc_free_expr (init);
7770 /* Build an l-value expression for the result. */
7771 lval = gfc_lval_expr_from_sym (sym);
7773 /* Add the code at scope entry. */
7774 init_st = gfc_get_code ();
7775 init_st->next = ns->code;
7778 /* Assign the default initializer to the l-value. */
7779 init_st->loc = sym->declared_at;
7780 init_st->op = EXEC_INIT_ASSIGN;
7781 init_st->expr1 = lval;
7782 init_st->expr2 = init;
7785 /* Assign the default initializer to a derived type variable or result. */
7788 apply_default_init (gfc_symbol *sym)
7790 gfc_expr *init = NULL;
7792 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
7795 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
7796 init = gfc_default_initializer (&sym->ts);
7801 build_init_assign (sym, init);
7804 /* Build an initializer for a local integer, real, complex, logical, or
7805 character variable, based on the command line flags finit-local-zero,
7806 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
7807 null if the symbol should not have a default initialization. */
7809 build_default_init_expr (gfc_symbol *sym)
7812 gfc_expr *init_expr;
7815 /* These symbols should never have a default initialization. */
7816 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
7817 || sym->attr.external
7819 || sym->attr.pointer
7820 || sym->attr.in_equivalence
7821 || sym->attr.in_common
7824 || sym->attr.cray_pointee
7825 || sym->attr.cray_pointer)
7828 /* Now we'll try to build an initializer expression. */
7829 init_expr = gfc_get_expr ();
7830 init_expr->expr_type = EXPR_CONSTANT;
7831 init_expr->ts.type = sym->ts.type;
7832 init_expr->ts.kind = sym->ts.kind;
7833 init_expr->where = sym->declared_at;
7835 /* We will only initialize integers, reals, complex, logicals, and
7836 characters, and only if the corresponding command-line flags
7837 were set. Otherwise, we free init_expr and return null. */
7838 switch (sym->ts.type)
7841 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
7842 mpz_init_set_si (init_expr->value.integer,
7843 gfc_option.flag_init_integer_value);
7846 gfc_free_expr (init_expr);
7852 mpfr_init (init_expr->value.real);
7853 switch (gfc_option.flag_init_real)
7855 case GFC_INIT_REAL_SNAN:
7856 init_expr->is_snan = 1;
7858 case GFC_INIT_REAL_NAN:
7859 mpfr_set_nan (init_expr->value.real);
7862 case GFC_INIT_REAL_INF:
7863 mpfr_set_inf (init_expr->value.real, 1);
7866 case GFC_INIT_REAL_NEG_INF:
7867 mpfr_set_inf (init_expr->value.real, -1);
7870 case GFC_INIT_REAL_ZERO:
7871 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
7875 gfc_free_expr (init_expr);
7883 mpc_init2 (init_expr->value.complex, mpfr_get_default_prec());
7885 mpfr_init (init_expr->value.complex.r);
7886 mpfr_init (init_expr->value.complex.i);
7888 switch (gfc_option.flag_init_real)
7890 case GFC_INIT_REAL_SNAN:
7891 init_expr->is_snan = 1;
7893 case GFC_INIT_REAL_NAN:
7894 mpfr_set_nan (mpc_realref (init_expr->value.complex));
7895 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
7898 case GFC_INIT_REAL_INF:
7899 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
7900 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
7903 case GFC_INIT_REAL_NEG_INF:
7904 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
7905 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
7908 case GFC_INIT_REAL_ZERO:
7910 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
7912 mpfr_set_ui (init_expr->value.complex.r, 0.0, GFC_RND_MODE);
7913 mpfr_set_ui (init_expr->value.complex.i, 0.0, GFC_RND_MODE);
7918 gfc_free_expr (init_expr);
7925 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
7926 init_expr->value.logical = 0;
7927 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
7928 init_expr->value.logical = 1;
7931 gfc_free_expr (init_expr);
7937 /* For characters, the length must be constant in order to
7938 create a default initializer. */
7939 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
7940 && sym->ts.u.cl->length
7941 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7943 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
7944 init_expr->value.character.length = char_len;
7945 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
7946 for (i = 0; i < char_len; i++)
7947 init_expr->value.character.string[i]
7948 = (unsigned char) gfc_option.flag_init_character_value;
7952 gfc_free_expr (init_expr);
7958 gfc_free_expr (init_expr);
7964 /* Add an initialization expression to a local variable. */
7966 apply_default_init_local (gfc_symbol *sym)
7968 gfc_expr *init = NULL;
7970 /* The symbol should be a variable or a function return value. */
7971 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
7972 || (sym->attr.function && sym->result != sym))
7975 /* Try to build the initializer expression. If we can't initialize
7976 this symbol, then init will be NULL. */
7977 init = build_default_init_expr (sym);
7981 /* For saved variables, we don't want to add an initializer at
7982 function entry, so we just add a static initializer. */
7983 if (sym->attr.save || sym->ns->save_all)
7985 /* Don't clobber an existing initializer! */
7986 gcc_assert (sym->value == NULL);
7991 build_init_assign (sym, init);
7994 /* Resolution of common features of flavors variable and procedure. */
7997 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
7999 /* Constraints on deferred shape variable. */
8000 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
8002 if (sym->attr.allocatable)
8004 if (sym->attr.dimension)
8006 gfc_error ("Allocatable array '%s' at %L must have "
8007 "a deferred shape", sym->name, &sym->declared_at);
8010 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
8011 "may not be ALLOCATABLE", sym->name,
8012 &sym->declared_at) == FAILURE)
8016 if (sym->attr.pointer && sym->attr.dimension)
8018 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
8019 sym->name, &sym->declared_at);
8026 if (!mp_flag && !sym->attr.allocatable
8027 && !sym->attr.pointer && !sym->attr.dummy)
8029 gfc_error ("Array '%s' at %L cannot have a deferred shape",
8030 sym->name, &sym->declared_at);
8038 /* Check if a derived type is extensible. */
8041 type_is_extensible (gfc_symbol *sym)
8043 return !(sym->attr.is_bind_c || sym->attr.sequence);
8047 /* Additional checks for symbols with flavor variable and derived
8048 type. To be called from resolve_fl_variable. */
8051 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
8053 gcc_assert (sym->ts.type == BT_DERIVED);
8055 /* Check to see if a derived type is blocked from being host
8056 associated by the presence of another class I symbol in the same
8057 namespace. 14.6.1.3 of the standard and the discussion on
8058 comp.lang.fortran. */
8059 if (sym->ns != sym->ts.u.derived->ns
8060 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
8063 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
8064 if (s && s->attr.flavor != FL_DERIVED)
8066 gfc_error ("The type '%s' cannot be host associated at %L "
8067 "because it is blocked by an incompatible object "
8068 "of the same name declared at %L",
8069 sym->ts.u.derived->name, &sym->declared_at,
8075 /* 4th constraint in section 11.3: "If an object of a type for which
8076 component-initialization is specified (R429) appears in the
8077 specification-part of a module and does not have the ALLOCATABLE
8078 or POINTER attribute, the object shall have the SAVE attribute."
8080 The check for initializers is performed with
8081 has_default_initializer because gfc_default_initializer generates
8082 a hidden default for allocatable components. */
8083 if (!(sym->value || no_init_flag) && sym->ns->proc_name
8084 && sym->ns->proc_name->attr.flavor == FL_MODULE
8085 && !sym->ns->save_all && !sym->attr.save
8086 && !sym->attr.pointer && !sym->attr.allocatable
8087 && has_default_initializer (sym->ts.u.derived))
8089 gfc_error("Object '%s' at %L must have the SAVE attribute for "
8090 "default initialization of a component",
8091 sym->name, &sym->declared_at);
8095 if (sym->ts.is_class)
8098 if (!type_is_extensible (sym->ts.u.derived))
8100 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
8101 sym->ts.u.derived->name, sym->name, &sym->declared_at);
8106 if (!(sym->attr.dummy || sym->attr.allocatable || sym->attr.pointer))
8108 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
8109 "or pointer", sym->name, &sym->declared_at);
8114 /* Assign default initializer. */
8115 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
8116 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
8118 sym->value = gfc_default_initializer (&sym->ts);
8125 /* Resolve symbols with flavor variable. */
8128 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
8130 int no_init_flag, automatic_flag;
8132 const char *auto_save_msg;
8134 auto_save_msg = "Automatic object '%s' at %L cannot have the "
8137 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
8140 /* Set this flag to check that variables are parameters of all entries.
8141 This check is effected by the call to gfc_resolve_expr through
8142 is_non_constant_shape_array. */
8143 specification_expr = 1;
8145 if (sym->ns->proc_name
8146 && (sym->ns->proc_name->attr.flavor == FL_MODULE
8147 || sym->ns->proc_name->attr.is_main_program)
8148 && !sym->attr.use_assoc
8149 && !sym->attr.allocatable
8150 && !sym->attr.pointer
8151 && is_non_constant_shape_array (sym))
8153 /* The shape of a main program or module array needs to be
8155 gfc_error ("The module or main program array '%s' at %L must "
8156 "have constant shape", sym->name, &sym->declared_at);
8157 specification_expr = 0;
8161 if (sym->ts.type == BT_CHARACTER)
8163 /* Make sure that character string variables with assumed length are
8165 e = sym->ts.u.cl->length;
8166 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
8168 gfc_error ("Entity with assumed character length at %L must be a "
8169 "dummy argument or a PARAMETER", &sym->declared_at);
8173 if (e && sym->attr.save && !gfc_is_constant_expr (e))
8175 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
8179 if (!gfc_is_constant_expr (e)
8180 && !(e->expr_type == EXPR_VARIABLE
8181 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
8182 && sym->ns->proc_name
8183 && (sym->ns->proc_name->attr.flavor == FL_MODULE
8184 || sym->ns->proc_name->attr.is_main_program)
8185 && !sym->attr.use_assoc)
8187 gfc_error ("'%s' at %L must have constant character length "
8188 "in this context", sym->name, &sym->declared_at);
8193 if (sym->value == NULL && sym->attr.referenced)
8194 apply_default_init_local (sym); /* Try to apply a default initialization. */
8196 /* Determine if the symbol may not have an initializer. */
8197 no_init_flag = automatic_flag = 0;
8198 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
8199 || sym->attr.intrinsic || sym->attr.result)
8201 else if (sym->attr.dimension && !sym->attr.pointer
8202 && is_non_constant_shape_array (sym))
8204 no_init_flag = automatic_flag = 1;
8206 /* Also, they must not have the SAVE attribute.
8207 SAVE_IMPLICIT is checked below. */
8208 if (sym->attr.save == SAVE_EXPLICIT)
8210 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
8215 /* Ensure that any initializer is simplified. */
8217 gfc_simplify_expr (sym->value, 1);
8219 /* Reject illegal initializers. */
8220 if (!sym->mark && sym->value)
8222 if (sym->attr.allocatable)
8223 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
8224 sym->name, &sym->declared_at);
8225 else if (sym->attr.external)
8226 gfc_error ("External '%s' at %L cannot have an initializer",
8227 sym->name, &sym->declared_at);
8228 else if (sym->attr.dummy
8229 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
8230 gfc_error ("Dummy '%s' at %L cannot have an initializer",
8231 sym->name, &sym->declared_at);
8232 else if (sym->attr.intrinsic)
8233 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
8234 sym->name, &sym->declared_at);
8235 else if (sym->attr.result)
8236 gfc_error ("Function result '%s' at %L cannot have an initializer",
8237 sym->name, &sym->declared_at);
8238 else if (automatic_flag)
8239 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
8240 sym->name, &sym->declared_at);
8247 if (sym->ts.type == BT_DERIVED)
8248 return resolve_fl_variable_derived (sym, no_init_flag);
8254 /* Resolve a procedure. */
8257 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
8259 gfc_formal_arglist *arg;
8261 if (sym->attr.ambiguous_interfaces && !sym->attr.referenced)
8262 gfc_warning ("Although not referenced, '%s' at %L has ambiguous "
8263 "interfaces", sym->name, &sym->declared_at);
8265 if (sym->attr.function
8266 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
8269 if (sym->ts.type == BT_CHARACTER)
8271 gfc_charlen *cl = sym->ts.u.cl;
8273 if (cl && cl->length && gfc_is_constant_expr (cl->length)
8274 && resolve_charlen (cl) == FAILURE)
8277 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
8279 if (sym->attr.proc == PROC_ST_FUNCTION)
8281 gfc_error ("Character-valued statement function '%s' at %L must "
8282 "have constant length", sym->name, &sym->declared_at);
8286 if (sym->attr.external && sym->formal == NULL
8287 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
8289 gfc_error ("Automatic character length function '%s' at %L must "
8290 "have an explicit interface", sym->name,
8297 /* Ensure that derived type for are not of a private type. Internal
8298 module procedures are excluded by 2.2.3.3 - i.e., they are not
8299 externally accessible and can access all the objects accessible in
8301 if (!(sym->ns->parent
8302 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
8303 && gfc_check_access(sym->attr.access, sym->ns->default_access))
8305 gfc_interface *iface;
8307 for (arg = sym->formal; arg; arg = arg->next)
8310 && arg->sym->ts.type == BT_DERIVED
8311 && !arg->sym->ts.u.derived->attr.use_assoc
8312 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
8313 arg->sym->ts.u.derived->ns->default_access)
8314 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
8315 "PRIVATE type and cannot be a dummy argument"
8316 " of '%s', which is PUBLIC at %L",
8317 arg->sym->name, sym->name, &sym->declared_at)
8320 /* Stop this message from recurring. */
8321 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
8326 /* PUBLIC interfaces may expose PRIVATE procedures that take types
8327 PRIVATE to the containing module. */
8328 for (iface = sym->generic; iface; iface = iface->next)
8330 for (arg = iface->sym->formal; arg; arg = arg->next)
8333 && arg->sym->ts.type == BT_DERIVED
8334 && !arg->sym->ts.u.derived->attr.use_assoc
8335 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
8336 arg->sym->ts.u.derived->ns->default_access)
8337 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
8338 "'%s' in PUBLIC interface '%s' at %L "
8339 "takes dummy arguments of '%s' which is "
8340 "PRIVATE", iface->sym->name, sym->name,
8341 &iface->sym->declared_at,
8342 gfc_typename (&arg->sym->ts)) == FAILURE)
8344 /* Stop this message from recurring. */
8345 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
8351 /* PUBLIC interfaces may expose PRIVATE procedures that take types
8352 PRIVATE to the containing module. */
8353 for (iface = sym->generic; iface; iface = iface->next)
8355 for (arg = iface->sym->formal; arg; arg = arg->next)
8358 && arg->sym->ts.type == BT_DERIVED
8359 && !arg->sym->ts.u.derived->attr.use_assoc
8360 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
8361 arg->sym->ts.u.derived->ns->default_access)
8362 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
8363 "'%s' in PUBLIC interface '%s' at %L "
8364 "takes dummy arguments of '%s' which is "
8365 "PRIVATE", iface->sym->name, sym->name,
8366 &iface->sym->declared_at,
8367 gfc_typename (&arg->sym->ts)) == FAILURE)
8369 /* Stop this message from recurring. */
8370 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
8377 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
8378 && !sym->attr.proc_pointer)
8380 gfc_error ("Function '%s' at %L cannot have an initializer",
8381 sym->name, &sym->declared_at);
8385 /* An external symbol may not have an initializer because it is taken to be
8386 a procedure. Exception: Procedure Pointers. */
8387 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
8389 gfc_error ("External object '%s' at %L may not have an initializer",
8390 sym->name, &sym->declared_at);
8394 /* An elemental function is required to return a scalar 12.7.1 */
8395 if (sym->attr.elemental && sym->attr.function && sym->as)
8397 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
8398 "result", sym->name, &sym->declared_at);
8399 /* Reset so that the error only occurs once. */
8400 sym->attr.elemental = 0;
8404 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
8405 char-len-param shall not be array-valued, pointer-valued, recursive
8406 or pure. ....snip... A character value of * may only be used in the
8407 following ways: (i) Dummy arg of procedure - dummy associates with
8408 actual length; (ii) To declare a named constant; or (iii) External
8409 function - but length must be declared in calling scoping unit. */
8410 if (sym->attr.function
8411 && sym->ts.type == BT_CHARACTER
8412 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
8414 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
8415 || (sym->attr.recursive) || (sym->attr.pure))
8417 if (sym->as && sym->as->rank)
8418 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8419 "array-valued", sym->name, &sym->declared_at);
8421 if (sym->attr.pointer)
8422 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8423 "pointer-valued", sym->name, &sym->declared_at);
8426 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8427 "pure", sym->name, &sym->declared_at);
8429 if (sym->attr.recursive)
8430 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8431 "recursive", sym->name, &sym->declared_at);
8436 /* Appendix B.2 of the standard. Contained functions give an
8437 error anyway. Fixed-form is likely to be F77/legacy. */
8438 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
8439 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
8440 "CHARACTER(*) function '%s' at %L",
8441 sym->name, &sym->declared_at);
8444 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
8446 gfc_formal_arglist *curr_arg;
8447 int has_non_interop_arg = 0;
8449 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
8450 sym->common_block) == FAILURE)
8452 /* Clear these to prevent looking at them again if there was an
8454 sym->attr.is_bind_c = 0;
8455 sym->attr.is_c_interop = 0;
8456 sym->ts.is_c_interop = 0;
8460 /* So far, no errors have been found. */
8461 sym->attr.is_c_interop = 1;
8462 sym->ts.is_c_interop = 1;
8465 curr_arg = sym->formal;
8466 while (curr_arg != NULL)
8468 /* Skip implicitly typed dummy args here. */
8469 if (curr_arg->sym->attr.implicit_type == 0)
8470 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
8471 /* If something is found to fail, record the fact so we
8472 can mark the symbol for the procedure as not being
8473 BIND(C) to try and prevent multiple errors being
8475 has_non_interop_arg = 1;
8477 curr_arg = curr_arg->next;
8480 /* See if any of the arguments were not interoperable and if so, clear
8481 the procedure symbol to prevent duplicate error messages. */
8482 if (has_non_interop_arg != 0)
8484 sym->attr.is_c_interop = 0;
8485 sym->ts.is_c_interop = 0;
8486 sym->attr.is_bind_c = 0;
8490 if (!sym->attr.proc_pointer)
8492 if (sym->attr.save == SAVE_EXPLICIT)
8494 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
8495 "in '%s' at %L", sym->name, &sym->declared_at);
8498 if (sym->attr.intent)
8500 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
8501 "in '%s' at %L", sym->name, &sym->declared_at);
8504 if (sym->attr.subroutine && sym->attr.result)
8506 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
8507 "in '%s' at %L", sym->name, &sym->declared_at);
8510 if (sym->attr.external && sym->attr.function
8511 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
8512 || sym->attr.contained))
8514 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
8515 "in '%s' at %L", sym->name, &sym->declared_at);
8518 if (strcmp ("ppr@", sym->name) == 0)
8520 gfc_error ("Procedure pointer result '%s' at %L "
8521 "is missing the pointer attribute",
8522 sym->ns->proc_name->name, &sym->declared_at);
8531 /* Resolve a list of finalizer procedures. That is, after they have hopefully
8532 been defined and we now know their defined arguments, check that they fulfill
8533 the requirements of the standard for procedures used as finalizers. */
8536 gfc_resolve_finalizers (gfc_symbol* derived)
8538 gfc_finalizer* list;
8539 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
8540 gfc_try result = SUCCESS;
8541 bool seen_scalar = false;
8543 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
8546 /* Walk over the list of finalizer-procedures, check them, and if any one
8547 does not fit in with the standard's definition, print an error and remove
8548 it from the list. */
8549 prev_link = &derived->f2k_derived->finalizers;
8550 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
8556 /* Skip this finalizer if we already resolved it. */
8557 if (list->proc_tree)
8559 prev_link = &(list->next);
8563 /* Check this exists and is a SUBROUTINE. */
8564 if (!list->proc_sym->attr.subroutine)
8566 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
8567 list->proc_sym->name, &list->where);
8571 /* We should have exactly one argument. */
8572 if (!list->proc_sym->formal || list->proc_sym->formal->next)
8574 gfc_error ("FINAL procedure at %L must have exactly one argument",
8578 arg = list->proc_sym->formal->sym;
8580 /* This argument must be of our type. */
8581 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
8583 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
8584 &arg->declared_at, derived->name);
8588 /* It must neither be a pointer nor allocatable nor optional. */
8589 if (arg->attr.pointer)
8591 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
8595 if (arg->attr.allocatable)
8597 gfc_error ("Argument of FINAL procedure at %L must not be"
8598 " ALLOCATABLE", &arg->declared_at);
8601 if (arg->attr.optional)
8603 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
8608 /* It must not be INTENT(OUT). */
8609 if (arg->attr.intent == INTENT_OUT)
8611 gfc_error ("Argument of FINAL procedure at %L must not be"
8612 " INTENT(OUT)", &arg->declared_at);
8616 /* Warn if the procedure is non-scalar and not assumed shape. */
8617 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
8618 && arg->as->type != AS_ASSUMED_SHAPE)
8619 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
8620 " shape argument", &arg->declared_at);
8622 /* Check that it does not match in kind and rank with a FINAL procedure
8623 defined earlier. To really loop over the *earlier* declarations,
8624 we need to walk the tail of the list as new ones were pushed at the
8626 /* TODO: Handle kind parameters once they are implemented. */
8627 my_rank = (arg->as ? arg->as->rank : 0);
8628 for (i = list->next; i; i = i->next)
8630 /* Argument list might be empty; that is an error signalled earlier,
8631 but we nevertheless continued resolving. */
8632 if (i->proc_sym->formal)
8634 gfc_symbol* i_arg = i->proc_sym->formal->sym;
8635 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
8636 if (i_rank == my_rank)
8638 gfc_error ("FINAL procedure '%s' declared at %L has the same"
8639 " rank (%d) as '%s'",
8640 list->proc_sym->name, &list->where, my_rank,
8647 /* Is this the/a scalar finalizer procedure? */
8648 if (!arg->as || arg->as->rank == 0)
8651 /* Find the symtree for this procedure. */
8652 gcc_assert (!list->proc_tree);
8653 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
8655 prev_link = &list->next;
8658 /* Remove wrong nodes immediately from the list so we don't risk any
8659 troubles in the future when they might fail later expectations. */
8663 *prev_link = list->next;
8664 gfc_free_finalizer (i);
8667 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
8668 were nodes in the list, must have been for arrays. It is surely a good
8669 idea to have a scalar version there if there's something to finalize. */
8670 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
8671 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
8672 " defined at %L, suggest also scalar one",
8673 derived->name, &derived->declared_at);
8675 /* TODO: Remove this error when finalization is finished. */
8676 gfc_error ("Finalization at %L is not yet implemented",
8677 &derived->declared_at);
8683 /* Check that it is ok for the typebound procedure proc to override the
8687 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
8690 const gfc_symbol* proc_target;
8691 const gfc_symbol* old_target;
8692 unsigned proc_pass_arg, old_pass_arg, argpos;
8693 gfc_formal_arglist* proc_formal;
8694 gfc_formal_arglist* old_formal;
8696 /* This procedure should only be called for non-GENERIC proc. */
8697 gcc_assert (!proc->n.tb->is_generic);
8699 /* If the overwritten procedure is GENERIC, this is an error. */
8700 if (old->n.tb->is_generic)
8702 gfc_error ("Can't overwrite GENERIC '%s' at %L",
8703 old->name, &proc->n.tb->where);
8707 where = proc->n.tb->where;
8708 proc_target = proc->n.tb->u.specific->n.sym;
8709 old_target = old->n.tb->u.specific->n.sym;
8711 /* Check that overridden binding is not NON_OVERRIDABLE. */
8712 if (old->n.tb->non_overridable)
8714 gfc_error ("'%s' at %L overrides a procedure binding declared"
8715 " NON_OVERRIDABLE", proc->name, &where);
8719 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
8720 if (!old->n.tb->deferred && proc->n.tb->deferred)
8722 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
8723 " non-DEFERRED binding", proc->name, &where);
8727 /* If the overridden binding is PURE, the overriding must be, too. */
8728 if (old_target->attr.pure && !proc_target->attr.pure)
8730 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
8731 proc->name, &where);
8735 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
8736 is not, the overriding must not be either. */
8737 if (old_target->attr.elemental && !proc_target->attr.elemental)
8739 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
8740 " ELEMENTAL", proc->name, &where);
8743 if (!old_target->attr.elemental && proc_target->attr.elemental)
8745 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
8746 " be ELEMENTAL, either", proc->name, &where);
8750 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
8752 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
8754 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
8755 " SUBROUTINE", proc->name, &where);
8759 /* If the overridden binding is a FUNCTION, the overriding must also be a
8760 FUNCTION and have the same characteristics. */
8761 if (old_target->attr.function)
8763 if (!proc_target->attr.function)
8765 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
8766 " FUNCTION", proc->name, &where);
8770 /* FIXME: Do more comprehensive checking (including, for instance, the
8771 rank and array-shape). */
8772 gcc_assert (proc_target->result && old_target->result);
8773 if (!gfc_compare_types (&proc_target->result->ts,
8774 &old_target->result->ts))
8776 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
8777 " matching result types", proc->name, &where);
8782 /* If the overridden binding is PUBLIC, the overriding one must not be
8784 if (old->n.tb->access == ACCESS_PUBLIC
8785 && proc->n.tb->access == ACCESS_PRIVATE)
8787 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
8788 " PRIVATE", proc->name, &where);
8792 /* Compare the formal argument lists of both procedures. This is also abused
8793 to find the position of the passed-object dummy arguments of both
8794 bindings as at least the overridden one might not yet be resolved and we
8795 need those positions in the check below. */
8796 proc_pass_arg = old_pass_arg = 0;
8797 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
8799 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
8802 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
8803 proc_formal && old_formal;
8804 proc_formal = proc_formal->next, old_formal = old_formal->next)
8806 if (proc->n.tb->pass_arg
8807 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
8808 proc_pass_arg = argpos;
8809 if (old->n.tb->pass_arg
8810 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
8811 old_pass_arg = argpos;
8813 /* Check that the names correspond. */
8814 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
8816 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
8817 " to match the corresponding argument of the overridden"
8818 " procedure", proc_formal->sym->name, proc->name, &where,
8819 old_formal->sym->name);
8823 /* Check that the types correspond if neither is the passed-object
8825 /* FIXME: Do more comprehensive testing here. */
8826 if (proc_pass_arg != argpos && old_pass_arg != argpos
8827 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
8829 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L in"
8830 " in respect to the overridden procedure",
8831 proc_formal->sym->name, proc->name, &where);
8837 if (proc_formal || old_formal)
8839 gfc_error ("'%s' at %L must have the same number of formal arguments as"
8840 " the overridden procedure", proc->name, &where);
8844 /* If the overridden binding is NOPASS, the overriding one must also be
8846 if (old->n.tb->nopass && !proc->n.tb->nopass)
8848 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
8849 " NOPASS", proc->name, &where);
8853 /* If the overridden binding is PASS(x), the overriding one must also be
8854 PASS and the passed-object dummy arguments must correspond. */
8855 if (!old->n.tb->nopass)
8857 if (proc->n.tb->nopass)
8859 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
8860 " PASS", proc->name, &where);
8864 if (proc_pass_arg != old_pass_arg)
8866 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
8867 " the same position as the passed-object dummy argument of"
8868 " the overridden procedure", proc->name, &where);
8877 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
8880 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
8881 const char* generic_name, locus where)
8886 gcc_assert (t1->specific && t2->specific);
8887 gcc_assert (!t1->specific->is_generic);
8888 gcc_assert (!t2->specific->is_generic);
8890 sym1 = t1->specific->u.specific->n.sym;
8891 sym2 = t2->specific->u.specific->n.sym;
8893 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
8894 if (sym1->attr.subroutine != sym2->attr.subroutine
8895 || sym1->attr.function != sym2->attr.function)
8897 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
8898 " GENERIC '%s' at %L",
8899 sym1->name, sym2->name, generic_name, &where);
8903 /* Compare the interfaces. */
8904 if (gfc_compare_interfaces (sym1, sym2, NULL, 1, 0, NULL, 0))
8906 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
8907 sym1->name, sym2->name, generic_name, &where);
8915 /* Worker function for resolving a generic procedure binding; this is used to
8916 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
8918 The difference between those cases is finding possible inherited bindings
8919 that are overridden, as one has to look for them in tb_sym_root,
8920 tb_uop_root or tb_op, respectively. Thus the caller must already find
8921 the super-type and set p->overridden correctly. */
8924 resolve_tb_generic_targets (gfc_symbol* super_type,
8925 gfc_typebound_proc* p, const char* name)
8927 gfc_tbp_generic* target;
8928 gfc_symtree* first_target;
8929 gfc_symtree* inherited;
8931 gcc_assert (p && p->is_generic);
8933 /* Try to find the specific bindings for the symtrees in our target-list. */
8934 gcc_assert (p->u.generic);
8935 for (target = p->u.generic; target; target = target->next)
8936 if (!target->specific)
8938 gfc_typebound_proc* overridden_tbp;
8940 const char* target_name;
8942 target_name = target->specific_st->name;
8944 /* Defined for this type directly. */
8945 if (target->specific_st->n.tb)
8947 target->specific = target->specific_st->n.tb;
8948 goto specific_found;
8951 /* Look for an inherited specific binding. */
8954 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
8959 gcc_assert (inherited->n.tb);
8960 target->specific = inherited->n.tb;
8961 goto specific_found;
8965 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
8966 " at %L", target_name, name, &p->where);
8969 /* Once we've found the specific binding, check it is not ambiguous with
8970 other specifics already found or inherited for the same GENERIC. */
8972 gcc_assert (target->specific);
8974 /* This must really be a specific binding! */
8975 if (target->specific->is_generic)
8977 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
8978 " '%s' is GENERIC, too", name, &p->where, target_name);
8982 /* Check those already resolved on this type directly. */
8983 for (g = p->u.generic; g; g = g->next)
8984 if (g != target && g->specific
8985 && check_generic_tbp_ambiguity (target, g, name, p->where)
8989 /* Check for ambiguity with inherited specific targets. */
8990 for (overridden_tbp = p->overridden; overridden_tbp;
8991 overridden_tbp = overridden_tbp->overridden)
8992 if (overridden_tbp->is_generic)
8994 for (g = overridden_tbp->u.generic; g; g = g->next)
8996 gcc_assert (g->specific);
8997 if (check_generic_tbp_ambiguity (target, g,
8998 name, p->where) == FAILURE)
9004 /* If we attempt to "overwrite" a specific binding, this is an error. */
9005 if (p->overridden && !p->overridden->is_generic)
9007 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
9008 " the same name", name, &p->where);
9012 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
9013 all must have the same attributes here. */
9014 first_target = p->u.generic->specific->u.specific;
9015 gcc_assert (first_target);
9016 p->subroutine = first_target->n.sym->attr.subroutine;
9017 p->function = first_target->n.sym->attr.function;
9023 /* Resolve a GENERIC procedure binding for a derived type. */
9026 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
9028 gfc_symbol* super_type;
9030 /* Find the overridden binding if any. */
9031 st->n.tb->overridden = NULL;
9032 super_type = gfc_get_derived_super_type (derived);
9035 gfc_symtree* overridden;
9036 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
9039 if (overridden && overridden->n.tb)
9040 st->n.tb->overridden = overridden->n.tb;
9043 /* Resolve using worker function. */
9044 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
9048 /* Retrieve the target-procedure of an operator binding and do some checks in
9049 common for intrinsic and user-defined type-bound operators. */
9052 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
9054 gfc_symbol* target_proc;
9056 gcc_assert (target->specific && !target->specific->is_generic);
9057 target_proc = target->specific->u.specific->n.sym;
9058 gcc_assert (target_proc);
9060 /* All operator bindings must have a passed-object dummy argument. */
9061 if (target->specific->nopass)
9063 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
9071 /* Resolve a type-bound intrinsic operator. */
9074 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
9075 gfc_typebound_proc* p)
9077 gfc_symbol* super_type;
9078 gfc_tbp_generic* target;
9080 /* If there's already an error here, do nothing (but don't fail again). */
9084 /* Operators should always be GENERIC bindings. */
9085 gcc_assert (p->is_generic);
9087 /* Look for an overridden binding. */
9088 super_type = gfc_get_derived_super_type (derived);
9089 if (super_type && super_type->f2k_derived)
9090 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
9093 p->overridden = NULL;
9095 /* Resolve general GENERIC properties using worker function. */
9096 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
9099 /* Check the targets to be procedures of correct interface. */
9100 for (target = p->u.generic; target; target = target->next)
9102 gfc_symbol* target_proc;
9104 target_proc = get_checked_tb_operator_target (target, p->where);
9108 if (!gfc_check_operator_interface (target_proc, op, p->where))
9120 /* Resolve a type-bound user operator (tree-walker callback). */
9122 static gfc_symbol* resolve_bindings_derived;
9123 static gfc_try resolve_bindings_result;
9125 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
9128 resolve_typebound_user_op (gfc_symtree* stree)
9130 gfc_symbol* super_type;
9131 gfc_tbp_generic* target;
9133 gcc_assert (stree && stree->n.tb);
9135 if (stree->n.tb->error)
9138 /* Operators should always be GENERIC bindings. */
9139 gcc_assert (stree->n.tb->is_generic);
9141 /* Find overridden procedure, if any. */
9142 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
9143 if (super_type && super_type->f2k_derived)
9145 gfc_symtree* overridden;
9146 overridden = gfc_find_typebound_user_op (super_type, NULL,
9147 stree->name, true, NULL);
9149 if (overridden && overridden->n.tb)
9150 stree->n.tb->overridden = overridden->n.tb;
9153 stree->n.tb->overridden = NULL;
9155 /* Resolve basically using worker function. */
9156 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
9160 /* Check the targets to be functions of correct interface. */
9161 for (target = stree->n.tb->u.generic; target; target = target->next)
9163 gfc_symbol* target_proc;
9165 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
9169 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
9176 resolve_bindings_result = FAILURE;
9177 stree->n.tb->error = 1;
9181 /* Resolve the type-bound procedures for a derived type. */
9184 resolve_typebound_procedure (gfc_symtree* stree)
9189 gfc_symbol* super_type;
9190 gfc_component* comp;
9194 /* Undefined specific symbol from GENERIC target definition. */
9198 if (stree->n.tb->error)
9201 /* If this is a GENERIC binding, use that routine. */
9202 if (stree->n.tb->is_generic)
9204 if (resolve_typebound_generic (resolve_bindings_derived, stree)
9210 /* Get the target-procedure to check it. */
9211 gcc_assert (!stree->n.tb->is_generic);
9212 gcc_assert (stree->n.tb->u.specific);
9213 proc = stree->n.tb->u.specific->n.sym;
9214 where = stree->n.tb->where;
9216 /* Default access should already be resolved from the parser. */
9217 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
9219 /* It should be a module procedure or an external procedure with explicit
9220 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
9221 if ((!proc->attr.subroutine && !proc->attr.function)
9222 || (proc->attr.proc != PROC_MODULE
9223 && proc->attr.if_source != IFSRC_IFBODY)
9224 || (proc->attr.abstract && !stree->n.tb->deferred))
9226 gfc_error ("'%s' must be a module procedure or an external procedure with"
9227 " an explicit interface at %L", proc->name, &where);
9230 stree->n.tb->subroutine = proc->attr.subroutine;
9231 stree->n.tb->function = proc->attr.function;
9233 /* Find the super-type of the current derived type. We could do this once and
9234 store in a global if speed is needed, but as long as not I believe this is
9235 more readable and clearer. */
9236 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
9238 /* If PASS, resolve and check arguments if not already resolved / loaded
9239 from a .mod file. */
9240 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
9242 if (stree->n.tb->pass_arg)
9244 gfc_formal_arglist* i;
9246 /* If an explicit passing argument name is given, walk the arg-list
9250 stree->n.tb->pass_arg_num = 1;
9251 for (i = proc->formal; i; i = i->next)
9253 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
9258 ++stree->n.tb->pass_arg_num;
9263 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
9265 proc->name, stree->n.tb->pass_arg, &where,
9266 stree->n.tb->pass_arg);
9272 /* Otherwise, take the first one; there should in fact be at least
9274 stree->n.tb->pass_arg_num = 1;
9277 gfc_error ("Procedure '%s' with PASS at %L must have at"
9278 " least one argument", proc->name, &where);
9281 me_arg = proc->formal->sym;
9284 /* Now check that the argument-type matches. */
9285 gcc_assert (me_arg);
9286 if (me_arg->ts.type != BT_DERIVED
9287 || me_arg->ts.u.derived != resolve_bindings_derived)
9289 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
9290 " the derived-type '%s'", me_arg->name, proc->name,
9291 me_arg->name, &where, resolve_bindings_derived->name);
9295 if (!me_arg->ts.is_class)
9297 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
9298 " at %L", proc->name, &where);
9303 /* If we are extending some type, check that we don't override a procedure
9304 flagged NON_OVERRIDABLE. */
9305 stree->n.tb->overridden = NULL;
9308 gfc_symtree* overridden;
9309 overridden = gfc_find_typebound_proc (super_type, NULL,
9310 stree->name, true, NULL);
9312 if (overridden && overridden->n.tb)
9313 stree->n.tb->overridden = overridden->n.tb;
9315 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
9319 /* See if there's a name collision with a component directly in this type. */
9320 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
9321 if (!strcmp (comp->name, stree->name))
9323 gfc_error ("Procedure '%s' at %L has the same name as a component of"
9325 stree->name, &where, resolve_bindings_derived->name);
9329 /* Try to find a name collision with an inherited component. */
9330 if (super_type && gfc_find_component (super_type, stree->name, true, true))
9332 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
9333 " component of '%s'",
9334 stree->name, &where, resolve_bindings_derived->name);
9338 stree->n.tb->error = 0;
9342 resolve_bindings_result = FAILURE;
9343 stree->n.tb->error = 1;
9347 resolve_typebound_procedures (gfc_symbol* derived)
9351 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
9354 resolve_bindings_derived = derived;
9355 resolve_bindings_result = SUCCESS;
9357 if (derived->f2k_derived->tb_sym_root)
9358 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
9359 &resolve_typebound_procedure);
9361 if (derived->f2k_derived->tb_uop_root)
9362 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
9363 &resolve_typebound_user_op);
9365 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
9367 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
9368 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
9370 resolve_bindings_result = FAILURE;
9373 return resolve_bindings_result;
9377 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
9378 to give all identical derived types the same backend_decl. */
9380 add_dt_to_dt_list (gfc_symbol *derived)
9382 gfc_dt_list *dt_list;
9384 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
9385 if (derived == dt_list->derived)
9388 if (dt_list == NULL)
9390 dt_list = gfc_get_dt_list ();
9391 dt_list->next = gfc_derived_types;
9392 dt_list->derived = derived;
9393 gfc_derived_types = dt_list;
9398 /* Ensure that a derived-type is really not abstract, meaning that every
9399 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
9402 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
9407 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
9409 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
9412 if (st->n.tb && st->n.tb->deferred)
9414 gfc_symtree* overriding;
9415 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
9416 gcc_assert (overriding && overriding->n.tb);
9417 if (overriding->n.tb->deferred)
9419 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
9420 " '%s' is DEFERRED and not overridden",
9421 sub->name, &sub->declared_at, st->name);
9430 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
9432 /* The algorithm used here is to recursively travel up the ancestry of sub
9433 and for each ancestor-type, check all bindings. If any of them is
9434 DEFERRED, look it up starting from sub and see if the found (overriding)
9435 binding is not DEFERRED.
9436 This is not the most efficient way to do this, but it should be ok and is
9437 clearer than something sophisticated. */
9439 gcc_assert (ancestor && ancestor->attr.abstract && !sub->attr.abstract);
9441 /* Walk bindings of this ancestor. */
9442 if (ancestor->f2k_derived)
9445 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
9450 /* Find next ancestor type and recurse on it. */
9451 ancestor = gfc_get_derived_super_type (ancestor);
9453 return ensure_not_abstract (sub, ancestor);
9459 static void resolve_symbol (gfc_symbol *sym);
9462 /* Resolve the components of a derived type. */
9465 resolve_fl_derived (gfc_symbol *sym)
9467 gfc_symbol* super_type;
9471 super_type = gfc_get_derived_super_type (sym);
9473 /* Ensure the extended type gets resolved before we do. */
9474 if (super_type && resolve_fl_derived (super_type) == FAILURE)
9477 /* An ABSTRACT type must be extensible. */
9478 if (sym->attr.abstract && !type_is_extensible (sym))
9480 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
9481 sym->name, &sym->declared_at);
9485 for (c = sym->components; c != NULL; c = c->next)
9487 if (c->attr.proc_pointer && c->ts.interface)
9489 if (c->ts.interface->attr.procedure)
9490 gfc_error ("Interface '%s', used by procedure pointer component "
9491 "'%s' at %L, is declared in a later PROCEDURE statement",
9492 c->ts.interface->name, c->name, &c->loc);
9494 /* Get the attributes from the interface (now resolved). */
9495 if (c->ts.interface->attr.if_source
9496 || c->ts.interface->attr.intrinsic)
9498 gfc_symbol *ifc = c->ts.interface;
9500 if (ifc->formal && !ifc->formal_ns)
9501 resolve_symbol (ifc);
9503 if (ifc->attr.intrinsic)
9504 resolve_intrinsic (ifc, &ifc->declared_at);
9508 c->ts = ifc->result->ts;
9509 c->attr.allocatable = ifc->result->attr.allocatable;
9510 c->attr.pointer = ifc->result->attr.pointer;
9511 c->attr.dimension = ifc->result->attr.dimension;
9512 c->as = gfc_copy_array_spec (ifc->result->as);
9517 c->attr.allocatable = ifc->attr.allocatable;
9518 c->attr.pointer = ifc->attr.pointer;
9519 c->attr.dimension = ifc->attr.dimension;
9520 c->as = gfc_copy_array_spec (ifc->as);
9522 c->ts.interface = ifc;
9523 c->attr.function = ifc->attr.function;
9524 c->attr.subroutine = ifc->attr.subroutine;
9525 gfc_copy_formal_args_ppc (c, ifc);
9527 c->attr.pure = ifc->attr.pure;
9528 c->attr.elemental = ifc->attr.elemental;
9529 c->attr.recursive = ifc->attr.recursive;
9530 c->attr.always_explicit = ifc->attr.always_explicit;
9531 c->attr.ext_attr |= ifc->attr.ext_attr;
9532 /* Replace symbols in array spec. */
9536 for (i = 0; i < c->as->rank; i++)
9538 gfc_expr_replace_comp (c->as->lower[i], c);
9539 gfc_expr_replace_comp (c->as->upper[i], c);
9542 /* Copy char length. */
9543 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
9545 c->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
9546 gfc_expr_replace_comp (c->ts.u.cl->length, c);
9549 else if (c->ts.interface->name[0] != '\0')
9551 gfc_error ("Interface '%s' of procedure pointer component "
9552 "'%s' at %L must be explicit", c->ts.interface->name,
9557 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
9559 c->ts = *gfc_get_default_type (c->name, NULL);
9560 c->attr.implicit_type = 1;
9563 /* Procedure pointer components: Check PASS arg. */
9564 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0)
9568 if (c->tb->pass_arg)
9570 gfc_formal_arglist* i;
9572 /* If an explicit passing argument name is given, walk the arg-list
9576 c->tb->pass_arg_num = 1;
9577 for (i = c->formal; i; i = i->next)
9579 if (!strcmp (i->sym->name, c->tb->pass_arg))
9584 c->tb->pass_arg_num++;
9589 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
9590 "at %L has no argument '%s'", c->name,
9591 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
9598 /* Otherwise, take the first one; there should in fact be at least
9600 c->tb->pass_arg_num = 1;
9603 gfc_error ("Procedure pointer component '%s' with PASS at %L "
9604 "must have at least one argument",
9609 me_arg = c->formal->sym;
9612 /* Now check that the argument-type matches. */
9613 gcc_assert (me_arg);
9614 if (me_arg->ts.type != BT_DERIVED
9615 || me_arg->ts.u.derived != sym)
9617 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
9618 " the derived type '%s'", me_arg->name, c->name,
9619 me_arg->name, &c->loc, sym->name);
9624 /* Check for C453. */
9625 if (me_arg->attr.dimension)
9627 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
9628 "must be scalar", me_arg->name, c->name, me_arg->name,
9634 if (me_arg->attr.pointer)
9636 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
9637 "may not have the POINTER attribute", me_arg->name,
9638 c->name, me_arg->name, &c->loc);
9643 if (me_arg->attr.allocatable)
9645 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
9646 "may not be ALLOCATABLE", me_arg->name, c->name,
9647 me_arg->name, &c->loc);
9652 if (type_is_extensible (sym) && !me_arg->ts.is_class)
9653 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
9654 " at %L", c->name, &c->loc);
9658 /* Check type-spec if this is not the parent-type component. */
9659 if ((!sym->attr.extension || c != sym->components)
9660 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
9663 /* If this type is an extension, see if this component has the same name
9664 as an inherited type-bound procedure. */
9666 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
9668 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
9669 " inherited type-bound procedure",
9670 c->name, sym->name, &c->loc);
9674 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
9676 if (c->ts.u.cl->length == NULL
9677 || (resolve_charlen (c->ts.u.cl) == FAILURE)
9678 || !gfc_is_constant_expr (c->ts.u.cl->length))
9680 gfc_error ("Character length of component '%s' needs to "
9681 "be a constant specification expression at %L",
9683 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
9688 if (c->ts.type == BT_DERIVED
9689 && sym->component_access != ACCESS_PRIVATE
9690 && gfc_check_access (sym->attr.access, sym->ns->default_access)
9691 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
9692 && !c->ts.u.derived->attr.use_assoc
9693 && !gfc_check_access (c->ts.u.derived->attr.access,
9694 c->ts.u.derived->ns->default_access)
9695 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
9696 "is a PRIVATE type and cannot be a component of "
9697 "'%s', which is PUBLIC at %L", c->name,
9698 sym->name, &sym->declared_at) == FAILURE)
9701 if (sym->attr.sequence)
9703 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
9705 gfc_error ("Component %s of SEQUENCE type declared at %L does "
9706 "not have the SEQUENCE attribute",
9707 c->ts.u.derived->name, &sym->declared_at);
9712 if (c->ts.type == BT_DERIVED && c->attr.pointer
9713 && c->ts.u.derived->components == NULL
9714 && !c->ts.u.derived->attr.zero_comp)
9716 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
9717 "that has not been declared", c->name, sym->name,
9723 if (c->ts.type == BT_DERIVED && c->ts.is_class
9724 && !(c->attr.pointer || c->attr.allocatable))
9726 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
9727 "or pointer", c->name, &c->loc);
9731 /* Ensure that all the derived type components are put on the
9732 derived type list; even in formal namespaces, where derived type
9733 pointer components might not have been declared. */
9734 if (c->ts.type == BT_DERIVED
9736 && c->ts.u.derived->components
9738 && sym != c->ts.u.derived)
9739 add_dt_to_dt_list (c->ts.u.derived);
9741 if (c->attr.pointer || c->attr.proc_pointer || c->attr.allocatable
9745 for (i = 0; i < c->as->rank; i++)
9747 if (c->as->lower[i] == NULL
9748 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
9749 || !gfc_is_constant_expr (c->as->lower[i])
9750 || c->as->upper[i] == NULL
9751 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
9752 || !gfc_is_constant_expr (c->as->upper[i]))
9754 gfc_error ("Component '%s' of '%s' at %L must have "
9755 "constant array bounds",
9756 c->name, sym->name, &c->loc);
9762 /* Resolve the type-bound procedures. */
9763 if (resolve_typebound_procedures (sym) == FAILURE)
9766 /* Resolve the finalizer procedures. */
9767 if (gfc_resolve_finalizers (sym) == FAILURE)
9770 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
9771 all DEFERRED bindings are overridden. */
9772 if (super_type && super_type->attr.abstract && !sym->attr.abstract
9773 && ensure_not_abstract (sym, super_type) == FAILURE)
9776 /* Add derived type to the derived type list. */
9777 add_dt_to_dt_list (sym);
9784 resolve_fl_namelist (gfc_symbol *sym)
9789 /* Reject PRIVATE objects in a PUBLIC namelist. */
9790 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
9792 for (nl = sym->namelist; nl; nl = nl->next)
9794 if (!nl->sym->attr.use_assoc
9795 && !is_sym_host_assoc (nl->sym, sym->ns)
9796 && !gfc_check_access(nl->sym->attr.access,
9797 nl->sym->ns->default_access))
9799 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
9800 "cannot be member of PUBLIC namelist '%s' at %L",
9801 nl->sym->name, sym->name, &sym->declared_at);
9805 /* Types with private components that came here by USE-association. */
9806 if (nl->sym->ts.type == BT_DERIVED
9807 && derived_inaccessible (nl->sym->ts.u.derived))
9809 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
9810 "components and cannot be member of namelist '%s' at %L",
9811 nl->sym->name, sym->name, &sym->declared_at);
9815 /* Types with private components that are defined in the same module. */
9816 if (nl->sym->ts.type == BT_DERIVED
9817 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
9818 && !gfc_check_access (nl->sym->ts.u.derived->attr.private_comp
9819 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
9820 nl->sym->ns->default_access))
9822 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
9823 "cannot be a member of PUBLIC namelist '%s' at %L",
9824 nl->sym->name, sym->name, &sym->declared_at);
9830 for (nl = sym->namelist; nl; nl = nl->next)
9832 /* Reject namelist arrays of assumed shape. */
9833 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
9834 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
9835 "must not have assumed shape in namelist "
9836 "'%s' at %L", nl->sym->name, sym->name,
9837 &sym->declared_at) == FAILURE)
9840 /* Reject namelist arrays that are not constant shape. */
9841 if (is_non_constant_shape_array (nl->sym))
9843 gfc_error ("NAMELIST array object '%s' must have constant "
9844 "shape in namelist '%s' at %L", nl->sym->name,
9845 sym->name, &sym->declared_at);
9849 /* Namelist objects cannot have allocatable or pointer components. */
9850 if (nl->sym->ts.type != BT_DERIVED)
9853 if (nl->sym->ts.u.derived->attr.alloc_comp)
9855 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
9856 "have ALLOCATABLE components",
9857 nl->sym->name, sym->name, &sym->declared_at);
9861 if (nl->sym->ts.u.derived->attr.pointer_comp)
9863 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
9864 "have POINTER components",
9865 nl->sym->name, sym->name, &sym->declared_at);
9871 /* 14.1.2 A module or internal procedure represent local entities
9872 of the same type as a namelist member and so are not allowed. */
9873 for (nl = sym->namelist; nl; nl = nl->next)
9875 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
9878 if (nl->sym->attr.function && nl->sym == nl->sym->result)
9879 if ((nl->sym == sym->ns->proc_name)
9881 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
9885 if (nl->sym && nl->sym->name)
9886 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
9887 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
9889 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
9890 "attribute in '%s' at %L", nlsym->name,
9901 resolve_fl_parameter (gfc_symbol *sym)
9903 /* A parameter array's shape needs to be constant. */
9905 && (sym->as->type == AS_DEFERRED
9906 || is_non_constant_shape_array (sym)))
9908 gfc_error ("Parameter array '%s' at %L cannot be automatic "
9909 "or of deferred shape", sym->name, &sym->declared_at);
9913 /* Make sure a parameter that has been implicitly typed still
9914 matches the implicit type, since PARAMETER statements can precede
9915 IMPLICIT statements. */
9916 if (sym->attr.implicit_type
9917 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
9920 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
9921 "later IMPLICIT type", sym->name, &sym->declared_at);
9925 /* Make sure the types of derived parameters are consistent. This
9926 type checking is deferred until resolution because the type may
9927 refer to a derived type from the host. */
9928 if (sym->ts.type == BT_DERIVED
9929 && !gfc_compare_types (&sym->ts, &sym->value->ts))
9931 gfc_error ("Incompatible derived type in PARAMETER at %L",
9932 &sym->value->where);
9939 /* Do anything necessary to resolve a symbol. Right now, we just
9940 assume that an otherwise unknown symbol is a variable. This sort
9941 of thing commonly happens for symbols in module. */
9944 resolve_symbol (gfc_symbol *sym)
9946 int check_constant, mp_flag;
9947 gfc_symtree *symtree;
9948 gfc_symtree *this_symtree;
9952 if (sym->attr.flavor == FL_UNKNOWN)
9955 /* If we find that a flavorless symbol is an interface in one of the
9956 parent namespaces, find its symtree in this namespace, free the
9957 symbol and set the symtree to point to the interface symbol. */
9958 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
9960 symtree = gfc_find_symtree (ns->sym_root, sym->name);
9961 if (symtree && symtree->n.sym->generic)
9963 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
9967 gfc_free_symbol (sym);
9968 symtree->n.sym->refs++;
9969 this_symtree->n.sym = symtree->n.sym;
9974 /* Otherwise give it a flavor according to such attributes as
9976 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
9977 sym->attr.flavor = FL_VARIABLE;
9980 sym->attr.flavor = FL_PROCEDURE;
9981 if (sym->attr.dimension)
9982 sym->attr.function = 1;
9986 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
9987 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
9989 if (sym->attr.procedure && sym->ts.interface
9990 && sym->attr.if_source != IFSRC_DECL)
9992 if (sym->ts.interface == sym)
9994 gfc_error ("PROCEDURE '%s' at %L may not be used as its own "
9995 "interface", sym->name, &sym->declared_at);
9998 if (sym->ts.interface->attr.procedure)
10000 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared"
10001 " in a later PROCEDURE statement", sym->ts.interface->name,
10002 sym->name,&sym->declared_at);
10006 /* Get the attributes from the interface (now resolved). */
10007 if (sym->ts.interface->attr.if_source
10008 || sym->ts.interface->attr.intrinsic)
10010 gfc_symbol *ifc = sym->ts.interface;
10011 resolve_symbol (ifc);
10013 if (ifc->attr.intrinsic)
10014 resolve_intrinsic (ifc, &ifc->declared_at);
10017 sym->ts = ifc->result->ts;
10020 sym->ts.interface = ifc;
10021 sym->attr.function = ifc->attr.function;
10022 sym->attr.subroutine = ifc->attr.subroutine;
10023 gfc_copy_formal_args (sym, ifc);
10025 sym->attr.allocatable = ifc->attr.allocatable;
10026 sym->attr.pointer = ifc->attr.pointer;
10027 sym->attr.pure = ifc->attr.pure;
10028 sym->attr.elemental = ifc->attr.elemental;
10029 sym->attr.dimension = ifc->attr.dimension;
10030 sym->attr.recursive = ifc->attr.recursive;
10031 sym->attr.always_explicit = ifc->attr.always_explicit;
10032 sym->attr.ext_attr |= ifc->attr.ext_attr;
10033 /* Copy array spec. */
10034 sym->as = gfc_copy_array_spec (ifc->as);
10038 for (i = 0; i < sym->as->rank; i++)
10040 gfc_expr_replace_symbols (sym->as->lower[i], sym);
10041 gfc_expr_replace_symbols (sym->as->upper[i], sym);
10044 /* Copy char length. */
10045 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
10047 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
10048 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
10051 else if (sym->ts.interface->name[0] != '\0')
10053 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
10054 sym->ts.interface->name, sym->name, &sym->declared_at);
10059 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
10062 /* Symbols that are module procedures with results (functions) have
10063 the types and array specification copied for type checking in
10064 procedures that call them, as well as for saving to a module
10065 file. These symbols can't stand the scrutiny that their results
10067 mp_flag = (sym->result != NULL && sym->result != sym);
10070 /* Make sure that the intrinsic is consistent with its internal
10071 representation. This needs to be done before assigning a default
10072 type to avoid spurious warnings. */
10073 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
10074 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
10077 /* Assign default type to symbols that need one and don't have one. */
10078 if (sym->ts.type == BT_UNKNOWN)
10080 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
10081 gfc_set_default_type (sym, 1, NULL);
10083 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
10084 && !sym->attr.function && !sym->attr.subroutine
10085 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
10086 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
10088 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
10090 /* The specific case of an external procedure should emit an error
10091 in the case that there is no implicit type. */
10093 gfc_set_default_type (sym, sym->attr.external, NULL);
10096 /* Result may be in another namespace. */
10097 resolve_symbol (sym->result);
10099 if (!sym->result->attr.proc_pointer)
10101 sym->ts = sym->result->ts;
10102 sym->as = gfc_copy_array_spec (sym->result->as);
10103 sym->attr.dimension = sym->result->attr.dimension;
10104 sym->attr.pointer = sym->result->attr.pointer;
10105 sym->attr.allocatable = sym->result->attr.allocatable;
10111 /* Assumed size arrays and assumed shape arrays must be dummy
10114 if (sym->as != NULL
10115 && (sym->as->type == AS_ASSUMED_SIZE
10116 || sym->as->type == AS_ASSUMED_SHAPE)
10117 && sym->attr.dummy == 0)
10119 if (sym->as->type == AS_ASSUMED_SIZE)
10120 gfc_error ("Assumed size array at %L must be a dummy argument",
10121 &sym->declared_at);
10123 gfc_error ("Assumed shape array at %L must be a dummy argument",
10124 &sym->declared_at);
10128 /* Make sure symbols with known intent or optional are really dummy
10129 variable. Because of ENTRY statement, this has to be deferred
10130 until resolution time. */
10132 if (!sym->attr.dummy
10133 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
10135 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
10139 if (sym->attr.value && !sym->attr.dummy)
10141 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
10142 "it is not a dummy argument", sym->name, &sym->declared_at);
10146 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
10148 gfc_charlen *cl = sym->ts.u.cl;
10149 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10151 gfc_error ("Character dummy variable '%s' at %L with VALUE "
10152 "attribute must have constant length",
10153 sym->name, &sym->declared_at);
10157 if (sym->ts.is_c_interop
10158 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
10160 gfc_error ("C interoperable character dummy variable '%s' at %L "
10161 "with VALUE attribute must have length one",
10162 sym->name, &sym->declared_at);
10167 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
10168 do this for something that was implicitly typed because that is handled
10169 in gfc_set_default_type. Handle dummy arguments and procedure
10170 definitions separately. Also, anything that is use associated is not
10171 handled here but instead is handled in the module it is declared in.
10172 Finally, derived type definitions are allowed to be BIND(C) since that
10173 only implies that they're interoperable, and they are checked fully for
10174 interoperability when a variable is declared of that type. */
10175 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
10176 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
10177 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
10179 gfc_try t = SUCCESS;
10181 /* First, make sure the variable is declared at the
10182 module-level scope (J3/04-007, Section 15.3). */
10183 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
10184 sym->attr.in_common == 0)
10186 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
10187 "is neither a COMMON block nor declared at the "
10188 "module level scope", sym->name, &(sym->declared_at));
10191 else if (sym->common_head != NULL)
10193 t = verify_com_block_vars_c_interop (sym->common_head);
10197 /* If type() declaration, we need to verify that the components
10198 of the given type are all C interoperable, etc. */
10199 if (sym->ts.type == BT_DERIVED &&
10200 sym->ts.u.derived->attr.is_c_interop != 1)
10202 /* Make sure the user marked the derived type as BIND(C). If
10203 not, call the verify routine. This could print an error
10204 for the derived type more than once if multiple variables
10205 of that type are declared. */
10206 if (sym->ts.u.derived->attr.is_bind_c != 1)
10207 verify_bind_c_derived_type (sym->ts.u.derived);
10211 /* Verify the variable itself as C interoperable if it
10212 is BIND(C). It is not possible for this to succeed if
10213 the verify_bind_c_derived_type failed, so don't have to handle
10214 any error returned by verify_bind_c_derived_type. */
10215 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
10216 sym->common_block);
10221 /* clear the is_bind_c flag to prevent reporting errors more than
10222 once if something failed. */
10223 sym->attr.is_bind_c = 0;
10228 /* If a derived type symbol has reached this point, without its
10229 type being declared, we have an error. Notice that most
10230 conditions that produce undefined derived types have already
10231 been dealt with. However, the likes of:
10232 implicit type(t) (t) ..... call foo (t) will get us here if
10233 the type is not declared in the scope of the implicit
10234 statement. Change the type to BT_UNKNOWN, both because it is so
10235 and to prevent an ICE. */
10236 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
10237 && !sym->ts.u.derived->attr.zero_comp)
10239 gfc_error ("The derived type '%s' at %L is of type '%s', "
10240 "which has not been defined", sym->name,
10241 &sym->declared_at, sym->ts.u.derived->name);
10242 sym->ts.type = BT_UNKNOWN;
10246 /* Make sure that the derived type has been resolved and that the
10247 derived type is visible in the symbol's namespace, if it is a
10248 module function and is not PRIVATE. */
10249 if (sym->ts.type == BT_DERIVED
10250 && sym->ts.u.derived->attr.use_assoc
10251 && sym->ns->proc_name
10252 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10256 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
10259 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
10260 if (!ds && sym->attr.function
10261 && gfc_check_access (sym->attr.access, sym->ns->default_access))
10263 symtree = gfc_new_symtree (&sym->ns->sym_root,
10264 sym->ts.u.derived->name);
10265 symtree->n.sym = sym->ts.u.derived;
10266 sym->ts.u.derived->refs++;
10270 /* Unless the derived-type declaration is use associated, Fortran 95
10271 does not allow public entries of private derived types.
10272 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
10273 161 in 95-006r3. */
10274 if (sym->ts.type == BT_DERIVED
10275 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
10276 && !sym->ts.u.derived->attr.use_assoc
10277 && gfc_check_access (sym->attr.access, sym->ns->default_access)
10278 && !gfc_check_access (sym->ts.u.derived->attr.access,
10279 sym->ts.u.derived->ns->default_access)
10280 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
10281 "of PRIVATE derived type '%s'",
10282 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
10283 : "variable", sym->name, &sym->declared_at,
10284 sym->ts.u.derived->name) == FAILURE)
10287 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
10288 default initialization is defined (5.1.2.4.4). */
10289 if (sym->ts.type == BT_DERIVED
10291 && sym->attr.intent == INTENT_OUT
10293 && sym->as->type == AS_ASSUMED_SIZE)
10295 for (c = sym->ts.u.derived->components; c; c = c->next)
10297 if (c->initializer)
10299 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
10300 "ASSUMED SIZE and so cannot have a default initializer",
10301 sym->name, &sym->declared_at);
10307 switch (sym->attr.flavor)
10310 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
10315 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
10320 if (resolve_fl_namelist (sym) == FAILURE)
10325 if (resolve_fl_parameter (sym) == FAILURE)
10333 /* Resolve array specifier. Check as well some constraints
10334 on COMMON blocks. */
10336 check_constant = sym->attr.in_common && !sym->attr.pointer;
10338 /* Set the formal_arg_flag so that check_conflict will not throw
10339 an error for host associated variables in the specification
10340 expression for an array_valued function. */
10341 if (sym->attr.function && sym->as)
10342 formal_arg_flag = 1;
10344 gfc_resolve_array_spec (sym->as, check_constant);
10346 formal_arg_flag = 0;
10348 /* Resolve formal namespaces. */
10349 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
10350 && !sym->attr.contained && !sym->attr.intrinsic)
10351 gfc_resolve (sym->formal_ns);
10353 /* Make sure the formal namespace is present. */
10354 if (sym->formal && !sym->formal_ns)
10356 gfc_formal_arglist *formal = sym->formal;
10357 while (formal && !formal->sym)
10358 formal = formal->next;
10362 sym->formal_ns = formal->sym->ns;
10363 sym->formal_ns->refs++;
10367 /* Check threadprivate restrictions. */
10368 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
10369 && (!sym->attr.in_common
10370 && sym->module == NULL
10371 && (sym->ns->proc_name == NULL
10372 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
10373 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
10375 /* If we have come this far we can apply default-initializers, as
10376 described in 14.7.5, to those variables that have not already
10377 been assigned one. */
10378 if (sym->ts.type == BT_DERIVED
10379 && sym->attr.referenced
10380 && sym->ns == gfc_current_ns
10382 && !sym->attr.allocatable
10383 && !sym->attr.alloc_comp)
10385 symbol_attribute *a = &sym->attr;
10387 if ((!a->save && !a->dummy && !a->pointer
10388 && !a->in_common && !a->use_assoc
10389 && !(a->function && sym != sym->result))
10390 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
10391 apply_default_init (sym);
10394 /* If this symbol has a type-spec, check it. */
10395 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
10396 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
10397 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
10403 /************* Resolve DATA statements *************/
10407 gfc_data_value *vnode;
10413 /* Advance the values structure to point to the next value in the data list. */
10416 next_data_value (void)
10418 while (mpz_cmp_ui (values.left, 0) == 0)
10420 if (!gfc_is_constant_expr (values.vnode->expr))
10421 gfc_error ("non-constant DATA value at %L",
10422 &values.vnode->expr->where);
10424 if (values.vnode->next == NULL)
10427 values.vnode = values.vnode->next;
10428 mpz_set (values.left, values.vnode->repeat);
10436 check_data_variable (gfc_data_variable *var, locus *where)
10442 ar_type mark = AR_UNKNOWN;
10444 mpz_t section_index[GFC_MAX_DIMENSIONS];
10450 if (gfc_resolve_expr (var->expr) == FAILURE)
10454 mpz_init_set_si (offset, 0);
10457 if (e->expr_type != EXPR_VARIABLE)
10458 gfc_internal_error ("check_data_variable(): Bad expression");
10460 sym = e->symtree->n.sym;
10462 if (sym->ns->is_block_data && !sym->attr.in_common)
10464 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
10465 sym->name, &sym->declared_at);
10468 if (e->ref == NULL && sym->as)
10470 gfc_error ("DATA array '%s' at %L must be specified in a previous"
10471 " declaration", sym->name, where);
10475 has_pointer = sym->attr.pointer;
10477 for (ref = e->ref; ref; ref = ref->next)
10479 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
10483 && ref->type == REF_ARRAY
10484 && ref->u.ar.type != AR_FULL)
10486 gfc_error ("DATA element '%s' at %L is a pointer and so must "
10487 "be a full array", sym->name, where);
10492 if (e->rank == 0 || has_pointer)
10494 mpz_init_set_ui (size, 1);
10501 /* Find the array section reference. */
10502 for (ref = e->ref; ref; ref = ref->next)
10504 if (ref->type != REF_ARRAY)
10506 if (ref->u.ar.type == AR_ELEMENT)
10512 /* Set marks according to the reference pattern. */
10513 switch (ref->u.ar.type)
10521 /* Get the start position of array section. */
10522 gfc_get_section_index (ar, section_index, &offset);
10527 gcc_unreachable ();
10530 if (gfc_array_size (e, &size) == FAILURE)
10532 gfc_error ("Nonconstant array section at %L in DATA statement",
10534 mpz_clear (offset);
10541 while (mpz_cmp_ui (size, 0) > 0)
10543 if (next_data_value () == FAILURE)
10545 gfc_error ("DATA statement at %L has more variables than values",
10551 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
10555 /* If we have more than one element left in the repeat count,
10556 and we have more than one element left in the target variable,
10557 then create a range assignment. */
10558 /* FIXME: Only done for full arrays for now, since array sections
10560 if (mark == AR_FULL && ref && ref->next == NULL
10561 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
10565 if (mpz_cmp (size, values.left) >= 0)
10567 mpz_init_set (range, values.left);
10568 mpz_sub (size, size, values.left);
10569 mpz_set_ui (values.left, 0);
10573 mpz_init_set (range, size);
10574 mpz_sub (values.left, values.left, size);
10575 mpz_set_ui (size, 0);
10578 gfc_assign_data_value_range (var->expr, values.vnode->expr,
10581 mpz_add (offset, offset, range);
10585 /* Assign initial value to symbol. */
10588 mpz_sub_ui (values.left, values.left, 1);
10589 mpz_sub_ui (size, size, 1);
10591 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
10595 if (mark == AR_FULL)
10596 mpz_add_ui (offset, offset, 1);
10598 /* Modify the array section indexes and recalculate the offset
10599 for next element. */
10600 else if (mark == AR_SECTION)
10601 gfc_advance_section (section_index, ar, &offset);
10605 if (mark == AR_SECTION)
10607 for (i = 0; i < ar->dimen; i++)
10608 mpz_clear (section_index[i]);
10612 mpz_clear (offset);
10618 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
10620 /* Iterate over a list of elements in a DATA statement. */
10623 traverse_data_list (gfc_data_variable *var, locus *where)
10626 iterator_stack frame;
10627 gfc_expr *e, *start, *end, *step;
10628 gfc_try retval = SUCCESS;
10630 mpz_init (frame.value);
10632 start = gfc_copy_expr (var->iter.start);
10633 end = gfc_copy_expr (var->iter.end);
10634 step = gfc_copy_expr (var->iter.step);
10636 if (gfc_simplify_expr (start, 1) == FAILURE
10637 || start->expr_type != EXPR_CONSTANT)
10639 gfc_error ("iterator start at %L does not simplify", &start->where);
10643 if (gfc_simplify_expr (end, 1) == FAILURE
10644 || end->expr_type != EXPR_CONSTANT)
10646 gfc_error ("iterator end at %L does not simplify", &end->where);
10650 if (gfc_simplify_expr (step, 1) == FAILURE
10651 || step->expr_type != EXPR_CONSTANT)
10653 gfc_error ("iterator step at %L does not simplify", &step->where);
10658 mpz_init_set (trip, end->value.integer);
10659 mpz_sub (trip, trip, start->value.integer);
10660 mpz_add (trip, trip, step->value.integer);
10662 mpz_div (trip, trip, step->value.integer);
10664 mpz_set (frame.value, start->value.integer);
10666 frame.prev = iter_stack;
10667 frame.variable = var->iter.var->symtree;
10668 iter_stack = &frame;
10670 while (mpz_cmp_ui (trip, 0) > 0)
10672 if (traverse_data_var (var->list, where) == FAILURE)
10679 e = gfc_copy_expr (var->expr);
10680 if (gfc_simplify_expr (e, 1) == FAILURE)
10688 mpz_add (frame.value, frame.value, step->value.integer);
10690 mpz_sub_ui (trip, trip, 1);
10695 mpz_clear (frame.value);
10697 gfc_free_expr (start);
10698 gfc_free_expr (end);
10699 gfc_free_expr (step);
10701 iter_stack = frame.prev;
10706 /* Type resolve variables in the variable list of a DATA statement. */
10709 traverse_data_var (gfc_data_variable *var, locus *where)
10713 for (; var; var = var->next)
10715 if (var->expr == NULL)
10716 t = traverse_data_list (var, where);
10718 t = check_data_variable (var, where);
10728 /* Resolve the expressions and iterators associated with a data statement.
10729 This is separate from the assignment checking because data lists should
10730 only be resolved once. */
10733 resolve_data_variables (gfc_data_variable *d)
10735 for (; d; d = d->next)
10737 if (d->list == NULL)
10739 if (gfc_resolve_expr (d->expr) == FAILURE)
10744 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
10747 if (resolve_data_variables (d->list) == FAILURE)
10756 /* Resolve a single DATA statement. We implement this by storing a pointer to
10757 the value list into static variables, and then recursively traversing the
10758 variables list, expanding iterators and such. */
10761 resolve_data (gfc_data *d)
10764 if (resolve_data_variables (d->var) == FAILURE)
10767 values.vnode = d->value;
10768 if (d->value == NULL)
10769 mpz_set_ui (values.left, 0);
10771 mpz_set (values.left, d->value->repeat);
10773 if (traverse_data_var (d->var, &d->where) == FAILURE)
10776 /* At this point, we better not have any values left. */
10778 if (next_data_value () == SUCCESS)
10779 gfc_error ("DATA statement at %L has more values than variables",
10784 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
10785 accessed by host or use association, is a dummy argument to a pure function,
10786 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
10787 is storage associated with any such variable, shall not be used in the
10788 following contexts: (clients of this function). */
10790 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
10791 procedure. Returns zero if assignment is OK, nonzero if there is a
10794 gfc_impure_variable (gfc_symbol *sym)
10798 if (sym->attr.use_assoc || sym->attr.in_common)
10801 if (sym->ns != gfc_current_ns)
10802 return !sym->attr.function;
10804 proc = sym->ns->proc_name;
10805 if (sym->attr.dummy && gfc_pure (proc)
10806 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
10808 proc->attr.function))
10811 /* TODO: Sort out what can be storage associated, if anything, and include
10812 it here. In principle equivalences should be scanned but it does not
10813 seem to be possible to storage associate an impure variable this way. */
10818 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
10819 symbol of the current procedure. */
10822 gfc_pure (gfc_symbol *sym)
10824 symbol_attribute attr;
10827 sym = gfc_current_ns->proc_name;
10833 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
10837 /* Test whether the current procedure is elemental or not. */
10840 gfc_elemental (gfc_symbol *sym)
10842 symbol_attribute attr;
10845 sym = gfc_current_ns->proc_name;
10850 return attr.flavor == FL_PROCEDURE && attr.elemental;
10854 /* Warn about unused labels. */
10857 warn_unused_fortran_label (gfc_st_label *label)
10862 warn_unused_fortran_label (label->left);
10864 if (label->defined == ST_LABEL_UNKNOWN)
10867 switch (label->referenced)
10869 case ST_LABEL_UNKNOWN:
10870 gfc_warning ("Label %d at %L defined but not used", label->value,
10874 case ST_LABEL_BAD_TARGET:
10875 gfc_warning ("Label %d at %L defined but cannot be used",
10876 label->value, &label->where);
10883 warn_unused_fortran_label (label->right);
10887 /* Returns the sequence type of a symbol or sequence. */
10890 sequence_type (gfc_typespec ts)
10899 if (ts.u.derived->components == NULL)
10900 return SEQ_NONDEFAULT;
10902 result = sequence_type (ts.u.derived->components->ts);
10903 for (c = ts.u.derived->components->next; c; c = c->next)
10904 if (sequence_type (c->ts) != result)
10910 if (ts.kind != gfc_default_character_kind)
10911 return SEQ_NONDEFAULT;
10913 return SEQ_CHARACTER;
10916 if (ts.kind != gfc_default_integer_kind)
10917 return SEQ_NONDEFAULT;
10919 return SEQ_NUMERIC;
10922 if (!(ts.kind == gfc_default_real_kind
10923 || ts.kind == gfc_default_double_kind))
10924 return SEQ_NONDEFAULT;
10926 return SEQ_NUMERIC;
10929 if (ts.kind != gfc_default_complex_kind)
10930 return SEQ_NONDEFAULT;
10932 return SEQ_NUMERIC;
10935 if (ts.kind != gfc_default_logical_kind)
10936 return SEQ_NONDEFAULT;
10938 return SEQ_NUMERIC;
10941 return SEQ_NONDEFAULT;
10946 /* Resolve derived type EQUIVALENCE object. */
10949 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
10951 gfc_component *c = derived->components;
10956 /* Shall not be an object of nonsequence derived type. */
10957 if (!derived->attr.sequence)
10959 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
10960 "attribute to be an EQUIVALENCE object", sym->name,
10965 /* Shall not have allocatable components. */
10966 if (derived->attr.alloc_comp)
10968 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
10969 "components to be an EQUIVALENCE object",sym->name,
10974 if (sym->attr.in_common && has_default_initializer (sym->ts.u.derived))
10976 gfc_error ("Derived type variable '%s' at %L with default "
10977 "initialization cannot be in EQUIVALENCE with a variable "
10978 "in COMMON", sym->name, &e->where);
10982 for (; c ; c = c->next)
10984 if (c->ts.type == BT_DERIVED
10985 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
10988 /* Shall not be an object of sequence derived type containing a pointer
10989 in the structure. */
10990 if (c->attr.pointer)
10992 gfc_error ("Derived type variable '%s' at %L with pointer "
10993 "component(s) cannot be an EQUIVALENCE object",
10994 sym->name, &e->where);
11002 /* Resolve equivalence object.
11003 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
11004 an allocatable array, an object of nonsequence derived type, an object of
11005 sequence derived type containing a pointer at any level of component
11006 selection, an automatic object, a function name, an entry name, a result
11007 name, a named constant, a structure component, or a subobject of any of
11008 the preceding objects. A substring shall not have length zero. A
11009 derived type shall not have components with default initialization nor
11010 shall two objects of an equivalence group be initialized.
11011 Either all or none of the objects shall have an protected attribute.
11012 The simple constraints are done in symbol.c(check_conflict) and the rest
11013 are implemented here. */
11016 resolve_equivalence (gfc_equiv *eq)
11019 gfc_symbol *first_sym;
11022 locus *last_where = NULL;
11023 seq_type eq_type, last_eq_type;
11024 gfc_typespec *last_ts;
11025 int object, cnt_protected;
11026 const char *value_name;
11030 last_ts = &eq->expr->symtree->n.sym->ts;
11032 first_sym = eq->expr->symtree->n.sym;
11036 for (object = 1; eq; eq = eq->eq, object++)
11040 e->ts = e->symtree->n.sym->ts;
11041 /* match_varspec might not know yet if it is seeing
11042 array reference or substring reference, as it doesn't
11044 if (e->ref && e->ref->type == REF_ARRAY)
11046 gfc_ref *ref = e->ref;
11047 sym = e->symtree->n.sym;
11049 if (sym->attr.dimension)
11051 ref->u.ar.as = sym->as;
11055 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
11056 if (e->ts.type == BT_CHARACTER
11058 && ref->type == REF_ARRAY
11059 && ref->u.ar.dimen == 1
11060 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
11061 && ref->u.ar.stride[0] == NULL)
11063 gfc_expr *start = ref->u.ar.start[0];
11064 gfc_expr *end = ref->u.ar.end[0];
11067 /* Optimize away the (:) reference. */
11068 if (start == NULL && end == NULL)
11071 e->ref = ref->next;
11073 e->ref->next = ref->next;
11078 ref->type = REF_SUBSTRING;
11080 start = gfc_int_expr (1);
11081 ref->u.ss.start = start;
11082 if (end == NULL && e->ts.u.cl)
11083 end = gfc_copy_expr (e->ts.u.cl->length);
11084 ref->u.ss.end = end;
11085 ref->u.ss.length = e->ts.u.cl;
11092 /* Any further ref is an error. */
11095 gcc_assert (ref->type == REF_ARRAY);
11096 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
11102 if (gfc_resolve_expr (e) == FAILURE)
11105 sym = e->symtree->n.sym;
11107 if (sym->attr.is_protected)
11109 if (cnt_protected > 0 && cnt_protected != object)
11111 gfc_error ("Either all or none of the objects in the "
11112 "EQUIVALENCE set at %L shall have the "
11113 "PROTECTED attribute",
11118 /* Shall not equivalence common block variables in a PURE procedure. */
11119 if (sym->ns->proc_name
11120 && sym->ns->proc_name->attr.pure
11121 && sym->attr.in_common)
11123 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
11124 "object in the pure procedure '%s'",
11125 sym->name, &e->where, sym->ns->proc_name->name);
11129 /* Shall not be a named constant. */
11130 if (e->expr_type == EXPR_CONSTANT)
11132 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
11133 "object", sym->name, &e->where);
11137 if (e->ts.type == BT_DERIVED
11138 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
11141 /* Check that the types correspond correctly:
11143 A numeric sequence structure may be equivalenced to another sequence
11144 structure, an object of default integer type, default real type, double
11145 precision real type, default logical type such that components of the
11146 structure ultimately only become associated to objects of the same
11147 kind. A character sequence structure may be equivalenced to an object
11148 of default character kind or another character sequence structure.
11149 Other objects may be equivalenced only to objects of the same type and
11150 kind parameters. */
11152 /* Identical types are unconditionally OK. */
11153 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
11154 goto identical_types;
11156 last_eq_type = sequence_type (*last_ts);
11157 eq_type = sequence_type (sym->ts);
11159 /* Since the pair of objects is not of the same type, mixed or
11160 non-default sequences can be rejected. */
11162 msg = "Sequence %s with mixed components in EQUIVALENCE "
11163 "statement at %L with different type objects";
11165 && last_eq_type == SEQ_MIXED
11166 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
11168 || (eq_type == SEQ_MIXED
11169 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11170 &e->where) == FAILURE))
11173 msg = "Non-default type object or sequence %s in EQUIVALENCE "
11174 "statement at %L with objects of different type";
11176 && last_eq_type == SEQ_NONDEFAULT
11177 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
11178 last_where) == FAILURE)
11179 || (eq_type == SEQ_NONDEFAULT
11180 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11181 &e->where) == FAILURE))
11184 msg ="Non-CHARACTER object '%s' in default CHARACTER "
11185 "EQUIVALENCE statement at %L";
11186 if (last_eq_type == SEQ_CHARACTER
11187 && eq_type != SEQ_CHARACTER
11188 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11189 &e->where) == FAILURE)
11192 msg ="Non-NUMERIC object '%s' in default NUMERIC "
11193 "EQUIVALENCE statement at %L";
11194 if (last_eq_type == SEQ_NUMERIC
11195 && eq_type != SEQ_NUMERIC
11196 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11197 &e->where) == FAILURE)
11202 last_where = &e->where;
11207 /* Shall not be an automatic array. */
11208 if (e->ref->type == REF_ARRAY
11209 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
11211 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
11212 "an EQUIVALENCE object", sym->name, &e->where);
11219 /* Shall not be a structure component. */
11220 if (r->type == REF_COMPONENT)
11222 gfc_error ("Structure component '%s' at %L cannot be an "
11223 "EQUIVALENCE object",
11224 r->u.c.component->name, &e->where);
11228 /* A substring shall not have length zero. */
11229 if (r->type == REF_SUBSTRING)
11231 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
11233 gfc_error ("Substring at %L has length zero",
11234 &r->u.ss.start->where);
11244 /* Resolve function and ENTRY types, issue diagnostics if needed. */
11247 resolve_fntype (gfc_namespace *ns)
11249 gfc_entry_list *el;
11252 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
11255 /* If there are any entries, ns->proc_name is the entry master
11256 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
11258 sym = ns->entries->sym;
11260 sym = ns->proc_name;
11261 if (sym->result == sym
11262 && sym->ts.type == BT_UNKNOWN
11263 && gfc_set_default_type (sym, 0, NULL) == FAILURE
11264 && !sym->attr.untyped)
11266 gfc_error ("Function '%s' at %L has no IMPLICIT type",
11267 sym->name, &sym->declared_at);
11268 sym->attr.untyped = 1;
11271 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
11272 && !sym->attr.contained
11273 && !gfc_check_access (sym->ts.u.derived->attr.access,
11274 sym->ts.u.derived->ns->default_access)
11275 && gfc_check_access (sym->attr.access, sym->ns->default_access))
11277 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
11278 "%L of PRIVATE type '%s'", sym->name,
11279 &sym->declared_at, sym->ts.u.derived->name);
11283 for (el = ns->entries->next; el; el = el->next)
11285 if (el->sym->result == el->sym
11286 && el->sym->ts.type == BT_UNKNOWN
11287 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
11288 && !el->sym->attr.untyped)
11290 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
11291 el->sym->name, &el->sym->declared_at);
11292 el->sym->attr.untyped = 1;
11298 /* 12.3.2.1.1 Defined operators. */
11301 check_uop_procedure (gfc_symbol *sym, locus where)
11303 gfc_formal_arglist *formal;
11305 if (!sym->attr.function)
11307 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
11308 sym->name, &where);
11312 if (sym->ts.type == BT_CHARACTER
11313 && !(sym->ts.u.cl && sym->ts.u.cl->length)
11314 && !(sym->result && sym->result->ts.u.cl
11315 && sym->result->ts.u.cl->length))
11317 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
11318 "character length", sym->name, &where);
11322 formal = sym->formal;
11323 if (!formal || !formal->sym)
11325 gfc_error ("User operator procedure '%s' at %L must have at least "
11326 "one argument", sym->name, &where);
11330 if (formal->sym->attr.intent != INTENT_IN)
11332 gfc_error ("First argument of operator interface at %L must be "
11333 "INTENT(IN)", &where);
11337 if (formal->sym->attr.optional)
11339 gfc_error ("First argument of operator interface at %L cannot be "
11340 "optional", &where);
11344 formal = formal->next;
11345 if (!formal || !formal->sym)
11348 if (formal->sym->attr.intent != INTENT_IN)
11350 gfc_error ("Second argument of operator interface at %L must be "
11351 "INTENT(IN)", &where);
11355 if (formal->sym->attr.optional)
11357 gfc_error ("Second argument of operator interface at %L cannot be "
11358 "optional", &where);
11364 gfc_error ("Operator interface at %L must have, at most, two "
11365 "arguments", &where);
11373 gfc_resolve_uops (gfc_symtree *symtree)
11375 gfc_interface *itr;
11377 if (symtree == NULL)
11380 gfc_resolve_uops (symtree->left);
11381 gfc_resolve_uops (symtree->right);
11383 for (itr = symtree->n.uop->op; itr; itr = itr->next)
11384 check_uop_procedure (itr->sym, itr->sym->declared_at);
11388 /* Examine all of the expressions associated with a program unit,
11389 assign types to all intermediate expressions, make sure that all
11390 assignments are to compatible types and figure out which names
11391 refer to which functions or subroutines. It doesn't check code
11392 block, which is handled by resolve_code. */
11395 resolve_types (gfc_namespace *ns)
11401 gfc_namespace* old_ns = gfc_current_ns;
11403 /* Check that all IMPLICIT types are ok. */
11404 if (!ns->seen_implicit_none)
11407 for (letter = 0; letter != GFC_LETTERS; ++letter)
11408 if (ns->set_flag[letter]
11409 && resolve_typespec_used (&ns->default_type[letter],
11410 &ns->implicit_loc[letter],
11415 gfc_current_ns = ns;
11417 resolve_entries (ns);
11419 resolve_common_vars (ns->blank_common.head, false);
11420 resolve_common_blocks (ns->common_root);
11422 resolve_contained_functions (ns);
11424 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
11426 for (cl = ns->cl_list; cl; cl = cl->next)
11427 resolve_charlen (cl);
11429 gfc_traverse_ns (ns, resolve_symbol);
11431 resolve_fntype (ns);
11433 for (n = ns->contained; n; n = n->sibling)
11435 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
11436 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
11437 "also be PURE", n->proc_name->name,
11438 &n->proc_name->declared_at);
11444 gfc_check_interfaces (ns);
11446 gfc_traverse_ns (ns, resolve_values);
11452 for (d = ns->data; d; d = d->next)
11456 gfc_traverse_ns (ns, gfc_formalize_init_value);
11458 gfc_traverse_ns (ns, gfc_verify_binding_labels);
11460 if (ns->common_root != NULL)
11461 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
11463 for (eq = ns->equiv; eq; eq = eq->next)
11464 resolve_equivalence (eq);
11466 /* Warn about unused labels. */
11467 if (warn_unused_label)
11468 warn_unused_fortran_label (ns->st_labels);
11470 gfc_resolve_uops (ns->uop_root);
11472 gfc_current_ns = old_ns;
11476 /* Call resolve_code recursively. */
11479 resolve_codes (gfc_namespace *ns)
11482 bitmap_obstack old_obstack;
11484 for (n = ns->contained; n; n = n->sibling)
11487 gfc_current_ns = ns;
11489 /* Set to an out of range value. */
11490 current_entry_id = -1;
11492 old_obstack = labels_obstack;
11493 bitmap_obstack_initialize (&labels_obstack);
11495 resolve_code (ns->code, ns);
11497 bitmap_obstack_release (&labels_obstack);
11498 labels_obstack = old_obstack;
11502 /* This function is called after a complete program unit has been compiled.
11503 Its purpose is to examine all of the expressions associated with a program
11504 unit, assign types to all intermediate expressions, make sure that all
11505 assignments are to compatible types and figure out which names refer to
11506 which functions or subroutines. */
11509 gfc_resolve (gfc_namespace *ns)
11511 gfc_namespace *old_ns;
11512 code_stack *old_cs_base;
11518 old_ns = gfc_current_ns;
11519 old_cs_base = cs_base;
11521 resolve_types (ns);
11522 resolve_codes (ns);
11524 gfc_current_ns = old_ns;
11525 cs_base = old_cs_base;