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->derived->attr.abstract)
111 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
112 name, where, ts->derived->name);
114 gfc_error ("ABSTRACT type '%s' used at %L",
115 ts->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.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)
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.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->cl && fts->cl
556 && (((ts->cl->length && !fts->cl->length)
557 ||(!ts->cl->length && fts->cl->length))
559 && ts->cl->length->expr_type
560 != fts->cl->length->expr_type)
562 && ts->cl->length->expr_type == EXPR_CONSTANT
563 && mpz_cmp (ts->cl->length->value.integer,
564 fts->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.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.derived->attr.sequence
722 || csym->ts.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.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.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.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.derived && expr->ts.derived->ts.is_iso_c && cons
834 && cons->expr != NULL)
836 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
837 expr->ts.derived->name, &(expr->where));
841 for (; comp; comp = comp->next, cons = cons->next)
848 if (gfc_resolve_expr (cons->expr) == FAILURE)
854 rank = comp->as ? comp->as->rank : 0;
855 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
856 && (comp->attr.allocatable || cons->expr->rank))
858 gfc_error ("The rank of the element in the derived type "
859 "constructor at %L does not match that of the "
860 "component (%d/%d)", &cons->expr->where,
861 cons->expr->rank, rank);
865 /* If we don't have the right type, try to convert it. */
867 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
870 if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
871 gfc_error ("The element in the derived type constructor at %L, "
872 "for pointer component '%s', is %s but should be %s",
873 &cons->expr->where, comp->name,
874 gfc_basic_typename (cons->expr->ts.type),
875 gfc_basic_typename (comp->ts.type));
877 t = gfc_convert_type (cons->expr, &comp->ts, 1);
880 if (cons->expr->expr_type == EXPR_NULL
881 && !(comp->attr.pointer || comp->attr.allocatable
882 || comp->attr.proc_pointer))
885 gfc_error ("The NULL in the derived type constructor at %L is "
886 "being applied to component '%s', which is neither "
887 "a POINTER nor ALLOCATABLE", &cons->expr->where,
891 if (!comp->attr.pointer || cons->expr->expr_type == EXPR_NULL)
894 a = gfc_expr_attr (cons->expr);
896 if (!a.pointer && !a.target)
899 gfc_error ("The element in the derived type constructor at %L, "
900 "for pointer component '%s' should be a POINTER or "
901 "a TARGET", &cons->expr->where, comp->name);
909 /****************** Expression name resolution ******************/
911 /* Returns 0 if a symbol was not declared with a type or
912 attribute declaration statement, nonzero otherwise. */
915 was_declared (gfc_symbol *sym)
921 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
924 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
925 || a.optional || a.pointer || a.save || a.target || a.volatile_
926 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
933 /* Determine if a symbol is generic or not. */
936 generic_sym (gfc_symbol *sym)
940 if (sym->attr.generic ||
941 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
944 if (was_declared (sym) || sym->ns->parent == NULL)
947 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
954 return generic_sym (s);
961 /* Determine if a symbol is specific or not. */
964 specific_sym (gfc_symbol *sym)
968 if (sym->attr.if_source == IFSRC_IFBODY
969 || sym->attr.proc == PROC_MODULE
970 || sym->attr.proc == PROC_INTERNAL
971 || sym->attr.proc == PROC_ST_FUNCTION
972 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
973 || sym->attr.external)
976 if (was_declared (sym) || sym->ns->parent == NULL)
979 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
981 return (s == NULL) ? 0 : specific_sym (s);
985 /* Figure out if the procedure is specific, generic or unknown. */
988 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
992 procedure_kind (gfc_symbol *sym)
994 if (generic_sym (sym))
995 return PTYPE_GENERIC;
997 if (specific_sym (sym))
998 return PTYPE_SPECIFIC;
1000 return PTYPE_UNKNOWN;
1003 /* Check references to assumed size arrays. The flag need_full_assumed_size
1004 is nonzero when matching actual arguments. */
1006 static int need_full_assumed_size = 0;
1009 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1011 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1014 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1015 What should it be? */
1016 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1017 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1018 && (e->ref->u.ar.type == AR_FULL))
1020 gfc_error ("The upper bound in the last dimension must "
1021 "appear in the reference to the assumed size "
1022 "array '%s' at %L", sym->name, &e->where);
1029 /* Look for bad assumed size array references in argument expressions
1030 of elemental and array valued intrinsic procedures. Since this is
1031 called from procedure resolution functions, it only recurses at
1035 resolve_assumed_size_actual (gfc_expr *e)
1040 switch (e->expr_type)
1043 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1048 if (resolve_assumed_size_actual (e->value.op.op1)
1049 || resolve_assumed_size_actual (e->value.op.op2))
1060 /* Check a generic procedure, passed as an actual argument, to see if
1061 there is a matching specific name. If none, it is an error, and if
1062 more than one, the reference is ambiguous. */
1064 count_specific_procs (gfc_expr *e)
1071 sym = e->symtree->n.sym;
1073 for (p = sym->generic; p; p = p->next)
1074 if (strcmp (sym->name, p->sym->name) == 0)
1076 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1082 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1086 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1087 "argument at %L", sym->name, &e->where);
1093 /* See if a call to sym could possibly be a not allowed RECURSION because of
1094 a missing RECURIVE declaration. This means that either sym is the current
1095 context itself, or sym is the parent of a contained procedure calling its
1096 non-RECURSIVE containing procedure.
1097 This also works if sym is an ENTRY. */
1100 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1102 gfc_symbol* proc_sym;
1103 gfc_symbol* context_proc;
1105 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1107 /* If we've got an ENTRY, find real procedure. */
1108 if (sym->attr.entry && sym->ns->entries)
1109 proc_sym = sym->ns->entries->sym;
1113 /* If sym is RECURSIVE, all is well of course. */
1114 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1117 /* Find the context procdure's "real" symbol if it has entries. */
1118 context_proc = (context->entries ? context->entries->sym
1119 : context->proc_name);
1123 /* A call from sym's body to itself is recursion, of course. */
1124 if (context_proc == proc_sym)
1127 /* The same is true if context is a contained procedure and sym the
1129 if (context_proc->attr.contained)
1131 gfc_symbol* parent_proc;
1133 gcc_assert (context->parent);
1134 parent_proc = (context->parent->entries ? context->parent->entries->sym
1135 : context->parent->proc_name);
1137 if (parent_proc == proc_sym)
1145 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1146 its typespec and formal argument list. */
1149 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1151 gfc_intrinsic_sym *isym = gfc_find_function (sym->name);
1154 if (!sym->attr.function &&
1155 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1161 isym = gfc_find_subroutine (sym->name);
1163 if (!sym->attr.subroutine &&
1164 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1168 gfc_copy_formal_args_intr (sym, isym);
1173 /* Resolve a procedure expression, like passing it to a called procedure or as
1174 RHS for a procedure pointer assignment. */
1177 resolve_procedure_expression (gfc_expr* expr)
1181 if (expr->expr_type != EXPR_VARIABLE)
1183 gcc_assert (expr->symtree);
1185 sym = expr->symtree->n.sym;
1187 if (sym->attr.intrinsic)
1188 resolve_intrinsic (sym, &expr->where);
1190 if (sym->attr.flavor != FL_PROCEDURE
1191 || (sym->attr.function && sym->result == sym))
1194 /* A non-RECURSIVE procedure that is used as procedure expression within its
1195 own body is in danger of being called recursively. */
1196 if (is_illegal_recursion (sym, gfc_current_ns))
1197 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1198 " itself recursively. Declare it RECURSIVE or use"
1199 " -frecursive", sym->name, &expr->where);
1205 /* Resolve an actual argument list. Most of the time, this is just
1206 resolving the expressions in the list.
1207 The exception is that we sometimes have to decide whether arguments
1208 that look like procedure arguments are really simple variable
1212 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1213 bool no_formal_args)
1216 gfc_symtree *parent_st;
1218 int save_need_full_assumed_size;
1219 gfc_component *comp;
1221 for (; arg; arg = arg->next)
1226 /* Check the label is a valid branching target. */
1229 if (arg->label->defined == ST_LABEL_UNKNOWN)
1231 gfc_error ("Label %d referenced at %L is never defined",
1232 arg->label->value, &arg->label->where);
1239 if (is_proc_ptr_comp (e, &comp))
1242 e->expr_type = EXPR_VARIABLE;
1246 if (e->expr_type == EXPR_VARIABLE
1247 && e->symtree->n.sym->attr.generic
1249 && count_specific_procs (e) != 1)
1252 if (e->ts.type != BT_PROCEDURE)
1254 save_need_full_assumed_size = need_full_assumed_size;
1255 if (e->expr_type != EXPR_VARIABLE)
1256 need_full_assumed_size = 0;
1257 if (gfc_resolve_expr (e) != SUCCESS)
1259 need_full_assumed_size = save_need_full_assumed_size;
1263 /* See if the expression node should really be a variable reference. */
1265 sym = e->symtree->n.sym;
1267 if (sym->attr.flavor == FL_PROCEDURE
1268 || sym->attr.intrinsic
1269 || sym->attr.external)
1273 /* If a procedure is not already determined to be something else
1274 check if it is intrinsic. */
1275 if (!sym->attr.intrinsic
1276 && !(sym->attr.external || sym->attr.use_assoc
1277 || sym->attr.if_source == IFSRC_IFBODY)
1278 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1279 sym->attr.intrinsic = 1;
1281 if (sym->attr.proc == PROC_ST_FUNCTION)
1283 gfc_error ("Statement function '%s' at %L is not allowed as an "
1284 "actual argument", sym->name, &e->where);
1287 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1288 sym->attr.subroutine);
1289 if (sym->attr.intrinsic && actual_ok == 0)
1291 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1292 "actual argument", sym->name, &e->where);
1295 if (sym->attr.contained && !sym->attr.use_assoc
1296 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1298 gfc_error ("Internal procedure '%s' is not allowed as an "
1299 "actual argument at %L", sym->name, &e->where);
1302 if (sym->attr.elemental && !sym->attr.intrinsic)
1304 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1305 "allowed as an actual argument at %L", sym->name,
1309 /* Check if a generic interface has a specific procedure
1310 with the same name before emitting an error. */
1311 if (sym->attr.generic && count_specific_procs (e) != 1)
1314 /* Just in case a specific was found for the expression. */
1315 sym = e->symtree->n.sym;
1317 /* If the symbol is the function that names the current (or
1318 parent) scope, then we really have a variable reference. */
1320 if (sym->attr.function && sym->result == sym
1321 && (sym->ns->proc_name == sym
1322 || (sym->ns->parent != NULL
1323 && sym->ns->parent->proc_name == sym)))
1326 /* If all else fails, see if we have a specific intrinsic. */
1327 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1329 gfc_intrinsic_sym *isym;
1331 isym = gfc_find_function (sym->name);
1332 if (isym == NULL || !isym->specific)
1334 gfc_error ("Unable to find a specific INTRINSIC procedure "
1335 "for the reference '%s' at %L", sym->name,
1340 sym->attr.intrinsic = 1;
1341 sym->attr.function = 1;
1344 if (gfc_resolve_expr (e) == FAILURE)
1349 /* See if the name is a module procedure in a parent unit. */
1351 if (was_declared (sym) || sym->ns->parent == NULL)
1354 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1356 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1360 if (parent_st == NULL)
1363 sym = parent_st->n.sym;
1364 e->symtree = parent_st; /* Point to the right thing. */
1366 if (sym->attr.flavor == FL_PROCEDURE
1367 || sym->attr.intrinsic
1368 || sym->attr.external)
1370 if (gfc_resolve_expr (e) == FAILURE)
1376 e->expr_type = EXPR_VARIABLE;
1378 if (sym->as != NULL)
1380 e->rank = sym->as->rank;
1381 e->ref = gfc_get_ref ();
1382 e->ref->type = REF_ARRAY;
1383 e->ref->u.ar.type = AR_FULL;
1384 e->ref->u.ar.as = sym->as;
1387 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1388 primary.c (match_actual_arg). If above code determines that it
1389 is a variable instead, it needs to be resolved as it was not
1390 done at the beginning of this function. */
1391 save_need_full_assumed_size = need_full_assumed_size;
1392 if (e->expr_type != EXPR_VARIABLE)
1393 need_full_assumed_size = 0;
1394 if (gfc_resolve_expr (e) != SUCCESS)
1396 need_full_assumed_size = save_need_full_assumed_size;
1399 /* Check argument list functions %VAL, %LOC and %REF. There is
1400 nothing to do for %REF. */
1401 if (arg->name && arg->name[0] == '%')
1403 if (strncmp ("%VAL", arg->name, 4) == 0)
1405 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1407 gfc_error ("By-value argument at %L is not of numeric "
1414 gfc_error ("By-value argument at %L cannot be an array or "
1415 "an array section", &e->where);
1419 /* Intrinsics are still PROC_UNKNOWN here. However,
1420 since same file external procedures are not resolvable
1421 in gfortran, it is a good deal easier to leave them to
1423 if (ptype != PROC_UNKNOWN
1424 && ptype != PROC_DUMMY
1425 && ptype != PROC_EXTERNAL
1426 && ptype != PROC_MODULE)
1428 gfc_error ("By-value argument at %L is not allowed "
1429 "in this context", &e->where);
1434 /* Statement functions have already been excluded above. */
1435 else if (strncmp ("%LOC", arg->name, 4) == 0
1436 && e->ts.type == BT_PROCEDURE)
1438 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1440 gfc_error ("Passing internal procedure at %L by location "
1441 "not allowed", &e->where);
1452 /* Do the checks of the actual argument list that are specific to elemental
1453 procedures. If called with c == NULL, we have a function, otherwise if
1454 expr == NULL, we have a subroutine. */
1457 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1459 gfc_actual_arglist *arg0;
1460 gfc_actual_arglist *arg;
1461 gfc_symbol *esym = NULL;
1462 gfc_intrinsic_sym *isym = NULL;
1464 gfc_intrinsic_arg *iformal = NULL;
1465 gfc_formal_arglist *eformal = NULL;
1466 bool formal_optional = false;
1467 bool set_by_optional = false;
1471 /* Is this an elemental procedure? */
1472 if (expr && expr->value.function.actual != NULL)
1474 if (expr->value.function.esym != NULL
1475 && expr->value.function.esym->attr.elemental)
1477 arg0 = expr->value.function.actual;
1478 esym = expr->value.function.esym;
1480 else if (expr->value.function.isym != NULL
1481 && expr->value.function.isym->elemental)
1483 arg0 = expr->value.function.actual;
1484 isym = expr->value.function.isym;
1489 else if (c && c->ext.actual != NULL)
1491 arg0 = c->ext.actual;
1493 if (c->resolved_sym)
1494 esym = c->resolved_sym;
1496 esym = c->symtree->n.sym;
1499 if (!esym->attr.elemental)
1505 /* The rank of an elemental is the rank of its array argument(s). */
1506 for (arg = arg0; arg; arg = arg->next)
1508 if (arg->expr != NULL && arg->expr->rank > 0)
1510 rank = arg->expr->rank;
1511 if (arg->expr->expr_type == EXPR_VARIABLE
1512 && arg->expr->symtree->n.sym->attr.optional)
1513 set_by_optional = true;
1515 /* Function specific; set the result rank and shape. */
1519 if (!expr->shape && arg->expr->shape)
1521 expr->shape = gfc_get_shape (rank);
1522 for (i = 0; i < rank; i++)
1523 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1530 /* If it is an array, it shall not be supplied as an actual argument
1531 to an elemental procedure unless an array of the same rank is supplied
1532 as an actual argument corresponding to a nonoptional dummy argument of
1533 that elemental procedure(12.4.1.5). */
1534 formal_optional = false;
1536 iformal = isym->formal;
1538 eformal = esym->formal;
1540 for (arg = arg0; arg; arg = arg->next)
1544 if (eformal->sym && eformal->sym->attr.optional)
1545 formal_optional = true;
1546 eformal = eformal->next;
1548 else if (isym && iformal)
1550 if (iformal->optional)
1551 formal_optional = true;
1552 iformal = iformal->next;
1555 formal_optional = true;
1557 if (pedantic && arg->expr != NULL
1558 && arg->expr->expr_type == EXPR_VARIABLE
1559 && arg->expr->symtree->n.sym->attr.optional
1562 && (set_by_optional || arg->expr->rank != rank)
1563 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1565 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1566 "MISSING, it cannot be the actual argument of an "
1567 "ELEMENTAL procedure unless there is a non-optional "
1568 "argument with the same rank (12.4.1.5)",
1569 arg->expr->symtree->n.sym->name, &arg->expr->where);
1574 for (arg = arg0; arg; arg = arg->next)
1576 if (arg->expr == NULL || arg->expr->rank == 0)
1579 /* Being elemental, the last upper bound of an assumed size array
1580 argument must be present. */
1581 if (resolve_assumed_size_actual (arg->expr))
1584 /* Elemental procedure's array actual arguments must conform. */
1587 if (gfc_check_conformance ("elemental procedure", arg->expr, e)
1595 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1596 is an array, the intent inout/out variable needs to be also an array. */
1597 if (rank > 0 && esym && expr == NULL)
1598 for (eformal = esym->formal, arg = arg0; arg && eformal;
1599 arg = arg->next, eformal = eformal->next)
1600 if ((eformal->sym->attr.intent == INTENT_OUT
1601 || eformal->sym->attr.intent == INTENT_INOUT)
1602 && arg->expr && arg->expr->rank == 0)
1604 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1605 "ELEMENTAL subroutine '%s' is a scalar, but another "
1606 "actual argument is an array", &arg->expr->where,
1607 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1608 : "INOUT", eformal->sym->name, esym->name);
1615 /* Go through each actual argument in ACTUAL and see if it can be
1616 implemented as an inlined, non-copying intrinsic. FNSYM is the
1617 function being called, or NULL if not known. */
1620 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1622 gfc_actual_arglist *ap;
1625 for (ap = actual; ap; ap = ap->next)
1627 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1628 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1630 ap->expr->inline_noncopying_intrinsic = 1;
1634 /* This function does the checking of references to global procedures
1635 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1636 77 and 95 standards. It checks for a gsymbol for the name, making
1637 one if it does not already exist. If it already exists, then the
1638 reference being resolved must correspond to the type of gsymbol.
1639 Otherwise, the new symbol is equipped with the attributes of the
1640 reference. The corresponding code that is called in creating
1641 global entities is parse.c.
1643 In addition, for all but -std=legacy, the gsymbols are used to
1644 check the interfaces of external procedures from the same file.
1645 The namespace of the gsymbol is resolved and then, once this is
1646 done the interface is checked. */
1649 resolve_global_procedure (gfc_symbol *sym, locus *where,
1650 gfc_actual_arglist **actual, int sub)
1654 enum gfc_symbol_type type;
1656 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1658 gsym = gfc_get_gsymbol (sym->name);
1660 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1661 gfc_global_used (gsym, where);
1663 if (gfc_option.flag_whole_file
1664 && gsym->type != GSYM_UNKNOWN
1666 && gsym->ns->proc_name)
1668 /* Make sure that translation for the gsymbol occurs before
1669 the procedure currently being resolved. */
1670 ns = gsym->ns->resolved ? NULL : gfc_global_ns_list;
1671 for (; ns && ns != gsym->ns; ns = ns->sibling)
1673 if (ns->sibling == gsym->ns)
1675 ns->sibling = gsym->ns->sibling;
1676 gsym->ns->sibling = gfc_global_ns_list;
1677 gfc_global_ns_list = gsym->ns;
1682 if (!gsym->ns->resolved)
1683 gfc_resolve (gsym->ns);
1685 gfc_procedure_use (gsym->ns->proc_name, actual, where);
1688 if (gsym->type == GSYM_UNKNOWN)
1691 gsym->where = *where;
1698 /************* Function resolution *************/
1700 /* Resolve a function call known to be generic.
1701 Section 14.1.2.4.1. */
1704 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1708 if (sym->attr.generic)
1710 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1713 expr->value.function.name = s->name;
1714 expr->value.function.esym = s;
1716 if (s->ts.type != BT_UNKNOWN)
1718 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1719 expr->ts = s->result->ts;
1722 expr->rank = s->as->rank;
1723 else if (s->result != NULL && s->result->as != NULL)
1724 expr->rank = s->result->as->rank;
1726 gfc_set_sym_referenced (expr->value.function.esym);
1731 /* TODO: Need to search for elemental references in generic
1735 if (sym->attr.intrinsic)
1736 return gfc_intrinsic_func_interface (expr, 0);
1743 resolve_generic_f (gfc_expr *expr)
1748 sym = expr->symtree->n.sym;
1752 m = resolve_generic_f0 (expr, sym);
1755 else if (m == MATCH_ERROR)
1759 if (sym->ns->parent == NULL)
1761 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1765 if (!generic_sym (sym))
1769 /* Last ditch attempt. See if the reference is to an intrinsic
1770 that possesses a matching interface. 14.1.2.4 */
1771 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
1773 gfc_error ("There is no specific function for the generic '%s' at %L",
1774 expr->symtree->n.sym->name, &expr->where);
1778 m = gfc_intrinsic_func_interface (expr, 0);
1782 gfc_error ("Generic function '%s' at %L is not consistent with a "
1783 "specific intrinsic interface", expr->symtree->n.sym->name,
1790 /* Resolve a function call known to be specific. */
1793 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
1797 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1799 if (sym->attr.dummy)
1801 sym->attr.proc = PROC_DUMMY;
1805 sym->attr.proc = PROC_EXTERNAL;
1809 if (sym->attr.proc == PROC_MODULE
1810 || sym->attr.proc == PROC_ST_FUNCTION
1811 || sym->attr.proc == PROC_INTERNAL)
1814 if (sym->attr.intrinsic)
1816 m = gfc_intrinsic_func_interface (expr, 1);
1820 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
1821 "with an intrinsic", sym->name, &expr->where);
1829 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1832 expr->value.function.name = sym->name;
1833 expr->value.function.esym = sym;
1834 if (sym->as != NULL)
1835 expr->rank = sym->as->rank;
1842 resolve_specific_f (gfc_expr *expr)
1847 sym = expr->symtree->n.sym;
1851 m = resolve_specific_f0 (sym, expr);
1854 if (m == MATCH_ERROR)
1857 if (sym->ns->parent == NULL)
1860 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1866 gfc_error ("Unable to resolve the specific function '%s' at %L",
1867 expr->symtree->n.sym->name, &expr->where);
1873 /* Resolve a procedure call not known to be generic nor specific. */
1876 resolve_unknown_f (gfc_expr *expr)
1881 sym = expr->symtree->n.sym;
1883 if (sym->attr.dummy)
1885 sym->attr.proc = PROC_DUMMY;
1886 expr->value.function.name = sym->name;
1890 /* See if we have an intrinsic function reference. */
1892 if (gfc_is_intrinsic (sym, 0, expr->where))
1894 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
1899 /* The reference is to an external name. */
1901 sym->attr.proc = PROC_EXTERNAL;
1902 expr->value.function.name = sym->name;
1903 expr->value.function.esym = expr->symtree->n.sym;
1905 if (sym->as != NULL)
1906 expr->rank = sym->as->rank;
1908 /* Type of the expression is either the type of the symbol or the
1909 default type of the symbol. */
1912 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1914 if (sym->ts.type != BT_UNKNOWN)
1918 ts = gfc_get_default_type (sym->name, sym->ns);
1920 if (ts->type == BT_UNKNOWN)
1922 gfc_error ("Function '%s' at %L has no IMPLICIT type",
1923 sym->name, &expr->where);
1934 /* Return true, if the symbol is an external procedure. */
1936 is_external_proc (gfc_symbol *sym)
1938 if (!sym->attr.dummy && !sym->attr.contained
1939 && !(sym->attr.intrinsic
1940 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
1941 && sym->attr.proc != PROC_ST_FUNCTION
1942 && !sym->attr.use_assoc
1950 /* Figure out if a function reference is pure or not. Also set the name
1951 of the function for a potential error message. Return nonzero if the
1952 function is PURE, zero if not. */
1954 pure_stmt_function (gfc_expr *, gfc_symbol *);
1957 pure_function (gfc_expr *e, const char **name)
1963 if (e->symtree != NULL
1964 && e->symtree->n.sym != NULL
1965 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
1966 return pure_stmt_function (e, e->symtree->n.sym);
1968 if (e->value.function.esym)
1970 pure = gfc_pure (e->value.function.esym);
1971 *name = e->value.function.esym->name;
1973 else if (e->value.function.isym)
1975 pure = e->value.function.isym->pure
1976 || e->value.function.isym->elemental;
1977 *name = e->value.function.isym->name;
1981 /* Implicit functions are not pure. */
1983 *name = e->value.function.name;
1991 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
1992 int *f ATTRIBUTE_UNUSED)
1996 /* Don't bother recursing into other statement functions
1997 since they will be checked individually for purity. */
1998 if (e->expr_type != EXPR_FUNCTION
2000 || e->symtree->n.sym == sym
2001 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2004 return pure_function (e, &name) ? false : true;
2009 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2011 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2016 is_scalar_expr_ptr (gfc_expr *expr)
2018 gfc_try retval = SUCCESS;
2023 /* See if we have a gfc_ref, which means we have a substring, array
2024 reference, or a component. */
2025 if (expr->ref != NULL)
2028 while (ref->next != NULL)
2034 if (ref->u.ss.length != NULL
2035 && ref->u.ss.length->length != NULL
2037 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2039 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2041 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2042 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2043 if (end - start + 1 != 1)
2050 if (ref->u.ar.type == AR_ELEMENT)
2052 else if (ref->u.ar.type == AR_FULL)
2054 /* The user can give a full array if the array is of size 1. */
2055 if (ref->u.ar.as != NULL
2056 && ref->u.ar.as->rank == 1
2057 && ref->u.ar.as->type == AS_EXPLICIT
2058 && ref->u.ar.as->lower[0] != NULL
2059 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2060 && ref->u.ar.as->upper[0] != NULL
2061 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2063 /* If we have a character string, we need to check if
2064 its length is one. */
2065 if (expr->ts.type == BT_CHARACTER)
2067 if (expr->ts.cl == NULL
2068 || expr->ts.cl->length == NULL
2069 || mpz_cmp_si (expr->ts.cl->length->value.integer, 1)
2075 /* We have constant lower and upper bounds. If the
2076 difference between is 1, it can be considered a
2078 start = (int) mpz_get_si
2079 (ref->u.ar.as->lower[0]->value.integer);
2080 end = (int) mpz_get_si
2081 (ref->u.ar.as->upper[0]->value.integer);
2082 if (end - start + 1 != 1)
2097 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2099 /* Character string. Make sure it's of length 1. */
2100 if (expr->ts.cl == NULL
2101 || expr->ts.cl->length == NULL
2102 || mpz_cmp_si (expr->ts.cl->length->value.integer, 1) != 0)
2105 else if (expr->rank != 0)
2112 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2113 and, in the case of c_associated, set the binding label based on
2117 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2118 gfc_symbol **new_sym)
2120 char name[GFC_MAX_SYMBOL_LEN + 1];
2121 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2122 int optional_arg = 0, is_pointer = 0;
2123 gfc_try retval = SUCCESS;
2124 gfc_symbol *args_sym;
2125 gfc_typespec *arg_ts;
2127 if (args->expr->expr_type == EXPR_CONSTANT
2128 || args->expr->expr_type == EXPR_OP
2129 || args->expr->expr_type == EXPR_NULL)
2131 gfc_error ("Argument to '%s' at %L is not a variable",
2132 sym->name, &(args->expr->where));
2136 args_sym = args->expr->symtree->n.sym;
2138 /* The typespec for the actual arg should be that stored in the expr
2139 and not necessarily that of the expr symbol (args_sym), because
2140 the actual expression could be a part-ref of the expr symbol. */
2141 arg_ts = &(args->expr->ts);
2143 is_pointer = gfc_is_data_pointer (args->expr);
2145 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2147 /* If the user gave two args then they are providing something for
2148 the optional arg (the second cptr). Therefore, set the name and
2149 binding label to the c_associated for two cptrs. Otherwise,
2150 set c_associated to expect one cptr. */
2154 sprintf (name, "%s_2", sym->name);
2155 sprintf (binding_label, "%s_2", sym->binding_label);
2161 sprintf (name, "%s_1", sym->name);
2162 sprintf (binding_label, "%s_1", sym->binding_label);
2166 /* Get a new symbol for the version of c_associated that
2168 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2170 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2171 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2173 sprintf (name, "%s", sym->name);
2174 sprintf (binding_label, "%s", sym->binding_label);
2176 /* Error check the call. */
2177 if (args->next != NULL)
2179 gfc_error_now ("More actual than formal arguments in '%s' "
2180 "call at %L", name, &(args->expr->where));
2183 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2185 /* Make sure we have either the target or pointer attribute. */
2186 if (!args_sym->attr.target && !is_pointer)
2188 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2189 "a TARGET or an associated pointer",
2191 sym->name, &(args->expr->where));
2195 /* See if we have interoperable type and type param. */
2196 if (verify_c_interop (arg_ts) == SUCCESS
2197 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2199 if (args_sym->attr.target == 1)
2201 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2202 has the target attribute and is interoperable. */
2203 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2204 allocatable variable that has the TARGET attribute and
2205 is not an array of zero size. */
2206 if (args_sym->attr.allocatable == 1)
2208 if (args_sym->attr.dimension != 0
2209 && (args_sym->as && args_sym->as->rank == 0))
2211 gfc_error_now ("Allocatable variable '%s' used as a "
2212 "parameter to '%s' at %L must not be "
2213 "an array of zero size",
2214 args_sym->name, sym->name,
2215 &(args->expr->where));
2221 /* A non-allocatable target variable with C
2222 interoperable type and type parameters must be
2224 if (args_sym && args_sym->attr.dimension)
2226 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2228 gfc_error ("Assumed-shape array '%s' at %L "
2229 "cannot be an argument to the "
2230 "procedure '%s' because "
2231 "it is not C interoperable",
2233 &(args->expr->where), sym->name);
2236 else if (args_sym->as->type == AS_DEFERRED)
2238 gfc_error ("Deferred-shape array '%s' at %L "
2239 "cannot be an argument to the "
2240 "procedure '%s' because "
2241 "it is not C interoperable",
2243 &(args->expr->where), sym->name);
2248 /* Make sure it's not a character string. Arrays of
2249 any type should be ok if the variable is of a C
2250 interoperable type. */
2251 if (arg_ts->type == BT_CHARACTER)
2252 if (arg_ts->cl != NULL
2253 && (arg_ts->cl->length == NULL
2254 || arg_ts->cl->length->expr_type
2257 (arg_ts->cl->length->value.integer, 1)
2259 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2261 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2262 "at %L must have a length of 1",
2263 args_sym->name, sym->name,
2264 &(args->expr->where));
2270 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2272 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2274 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2275 "associated scalar POINTER", args_sym->name,
2276 sym->name, &(args->expr->where));
2282 /* The parameter is not required to be C interoperable. If it
2283 is not C interoperable, it must be a nonpolymorphic scalar
2284 with no length type parameters. It still must have either
2285 the pointer or target attribute, and it can be
2286 allocatable (but must be allocated when c_loc is called). */
2287 if (args->expr->rank != 0
2288 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2290 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2291 "scalar", args_sym->name, sym->name,
2292 &(args->expr->where));
2295 else if (arg_ts->type == BT_CHARACTER
2296 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2298 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2299 "%L must have a length of 1",
2300 args_sym->name, sym->name,
2301 &(args->expr->where));
2306 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2308 if (args_sym->attr.flavor != FL_PROCEDURE)
2310 /* TODO: Update this error message to allow for procedure
2311 pointers once they are implemented. */
2312 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2314 args_sym->name, sym->name,
2315 &(args->expr->where));
2318 else if (args_sym->attr.is_bind_c != 1)
2320 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2322 args_sym->name, sym->name,
2323 &(args->expr->where));
2328 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2333 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2334 "iso_c_binding function: '%s'!\n", sym->name);
2341 /* Resolve a function call, which means resolving the arguments, then figuring
2342 out which entity the name refers to. */
2343 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2344 to INTENT(OUT) or INTENT(INOUT). */
2347 resolve_function (gfc_expr *expr)
2349 gfc_actual_arglist *arg;
2354 procedure_type p = PROC_INTRINSIC;
2355 bool no_formal_args;
2359 sym = expr->symtree->n.sym;
2361 if (sym && sym->attr.intrinsic
2362 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2365 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2367 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2371 if (sym && sym->attr.abstract)
2373 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2374 sym->name, &expr->where);
2378 /* Switch off assumed size checking and do this again for certain kinds
2379 of procedure, once the procedure itself is resolved. */
2380 need_full_assumed_size++;
2382 if (expr->symtree && expr->symtree->n.sym)
2383 p = expr->symtree->n.sym->attr.proc;
2385 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2386 if (resolve_actual_arglist (expr->value.function.actual,
2387 p, no_formal_args) == FAILURE)
2390 /* Need to setup the call to the correct c_associated, depending on
2391 the number of cptrs to user gives to compare. */
2392 if (sym && sym->attr.is_iso_c == 1)
2394 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2398 /* Get the symtree for the new symbol (resolved func).
2399 the old one will be freed later, when it's no longer used. */
2400 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2403 /* Resume assumed_size checking. */
2404 need_full_assumed_size--;
2406 /* If the procedure is external, check for usage. */
2407 if (sym && is_external_proc (sym))
2408 resolve_global_procedure (sym, &expr->where,
2409 &expr->value.function.actual, 0);
2411 if (sym && sym->ts.type == BT_CHARACTER
2413 && sym->ts.cl->length == NULL
2415 && expr->value.function.esym == NULL
2416 && !sym->attr.contained)
2418 /* Internal procedures are taken care of in resolve_contained_fntype. */
2419 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2420 "be used at %L since it is not a dummy argument",
2421 sym->name, &expr->where);
2425 /* See if function is already resolved. */
2427 if (expr->value.function.name != NULL)
2429 if (expr->ts.type == BT_UNKNOWN)
2435 /* Apply the rules of section 14.1.2. */
2437 switch (procedure_kind (sym))
2440 t = resolve_generic_f (expr);
2443 case PTYPE_SPECIFIC:
2444 t = resolve_specific_f (expr);
2448 t = resolve_unknown_f (expr);
2452 gfc_internal_error ("resolve_function(): bad function type");
2456 /* If the expression is still a function (it might have simplified),
2457 then we check to see if we are calling an elemental function. */
2459 if (expr->expr_type != EXPR_FUNCTION)
2462 temp = need_full_assumed_size;
2463 need_full_assumed_size = 0;
2465 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2468 if (omp_workshare_flag
2469 && expr->value.function.esym
2470 && ! gfc_elemental (expr->value.function.esym))
2472 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2473 "in WORKSHARE construct", expr->value.function.esym->name,
2478 #define GENERIC_ID expr->value.function.isym->id
2479 else if (expr->value.function.actual != NULL
2480 && expr->value.function.isym != NULL
2481 && GENERIC_ID != GFC_ISYM_LBOUND
2482 && GENERIC_ID != GFC_ISYM_LEN
2483 && GENERIC_ID != GFC_ISYM_LOC
2484 && GENERIC_ID != GFC_ISYM_PRESENT)
2486 /* Array intrinsics must also have the last upper bound of an
2487 assumed size array argument. UBOUND and SIZE have to be
2488 excluded from the check if the second argument is anything
2491 for (arg = expr->value.function.actual; arg; arg = arg->next)
2493 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2494 && arg->next != NULL && arg->next->expr)
2496 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2499 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2502 if ((int)mpz_get_si (arg->next->expr->value.integer)
2507 if (arg->expr != NULL
2508 && arg->expr->rank > 0
2509 && resolve_assumed_size_actual (arg->expr))
2515 need_full_assumed_size = temp;
2518 if (!pure_function (expr, &name) && name)
2522 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2523 "FORALL %s", name, &expr->where,
2524 forall_flag == 2 ? "mask" : "block");
2527 else if (gfc_pure (NULL))
2529 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2530 "procedure within a PURE procedure", name, &expr->where);
2535 /* Functions without the RECURSIVE attribution are not allowed to
2536 * call themselves. */
2537 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2540 esym = expr->value.function.esym;
2542 if (is_illegal_recursion (esym, gfc_current_ns))
2544 if (esym->attr.entry && esym->ns->entries)
2545 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2546 " function '%s' is not RECURSIVE",
2547 esym->name, &expr->where, esym->ns->entries->sym->name);
2549 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2550 " is not RECURSIVE", esym->name, &expr->where);
2556 /* Character lengths of use associated functions may contains references to
2557 symbols not referenced from the current program unit otherwise. Make sure
2558 those symbols are marked as referenced. */
2560 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2561 && expr->value.function.esym->attr.use_assoc)
2563 gfc_expr_set_symbols_referenced (expr->ts.cl->length);
2567 && !((expr->value.function.esym
2568 && expr->value.function.esym->attr.elemental)
2570 (expr->value.function.isym
2571 && expr->value.function.isym->elemental)))
2572 find_noncopying_intrinsics (expr->value.function.esym,
2573 expr->value.function.actual);
2575 /* Make sure that the expression has a typespec that works. */
2576 if (expr->ts.type == BT_UNKNOWN)
2578 if (expr->symtree->n.sym->result
2579 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
2580 && !expr->symtree->n.sym->result->attr.proc_pointer)
2581 expr->ts = expr->symtree->n.sym->result->ts;
2588 /************* Subroutine resolution *************/
2591 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2597 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2598 sym->name, &c->loc);
2599 else if (gfc_pure (NULL))
2600 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2606 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2610 if (sym->attr.generic)
2612 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2615 c->resolved_sym = s;
2616 pure_subroutine (c, s);
2620 /* TODO: Need to search for elemental references in generic interface. */
2623 if (sym->attr.intrinsic)
2624 return gfc_intrinsic_sub_interface (c, 0);
2631 resolve_generic_s (gfc_code *c)
2636 sym = c->symtree->n.sym;
2640 m = resolve_generic_s0 (c, sym);
2643 else if (m == MATCH_ERROR)
2647 if (sym->ns->parent == NULL)
2649 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2653 if (!generic_sym (sym))
2657 /* Last ditch attempt. See if the reference is to an intrinsic
2658 that possesses a matching interface. 14.1.2.4 */
2659 sym = c->symtree->n.sym;
2661 if (!gfc_is_intrinsic (sym, 1, c->loc))
2663 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2664 sym->name, &c->loc);
2668 m = gfc_intrinsic_sub_interface (c, 0);
2672 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2673 "intrinsic subroutine interface", sym->name, &c->loc);
2679 /* Set the name and binding label of the subroutine symbol in the call
2680 expression represented by 'c' to include the type and kind of the
2681 second parameter. This function is for resolving the appropriate
2682 version of c_f_pointer() and c_f_procpointer(). For example, a
2683 call to c_f_pointer() for a default integer pointer could have a
2684 name of c_f_pointer_i4. If no second arg exists, which is an error
2685 for these two functions, it defaults to the generic symbol's name
2686 and binding label. */
2689 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2690 char *name, char *binding_label)
2692 gfc_expr *arg = NULL;
2696 /* The second arg of c_f_pointer and c_f_procpointer determines
2697 the type and kind for the procedure name. */
2698 arg = c->ext.actual->next->expr;
2702 /* Set up the name to have the given symbol's name,
2703 plus the type and kind. */
2704 /* a derived type is marked with the type letter 'u' */
2705 if (arg->ts.type == BT_DERIVED)
2708 kind = 0; /* set the kind as 0 for now */
2712 type = gfc_type_letter (arg->ts.type);
2713 kind = arg->ts.kind;
2716 if (arg->ts.type == BT_CHARACTER)
2717 /* Kind info for character strings not needed. */
2720 sprintf (name, "%s_%c%d", sym->name, type, kind);
2721 /* Set up the binding label as the given symbol's label plus
2722 the type and kind. */
2723 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2727 /* If the second arg is missing, set the name and label as
2728 was, cause it should at least be found, and the missing
2729 arg error will be caught by compare_parameters(). */
2730 sprintf (name, "%s", sym->name);
2731 sprintf (binding_label, "%s", sym->binding_label);
2738 /* Resolve a generic version of the iso_c_binding procedure given
2739 (sym) to the specific one based on the type and kind of the
2740 argument(s). Currently, this function resolves c_f_pointer() and
2741 c_f_procpointer based on the type and kind of the second argument
2742 (FPTR). Other iso_c_binding procedures aren't specially handled.
2743 Upon successfully exiting, c->resolved_sym will hold the resolved
2744 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2748 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2750 gfc_symbol *new_sym;
2751 /* this is fine, since we know the names won't use the max */
2752 char name[GFC_MAX_SYMBOL_LEN + 1];
2753 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2754 /* default to success; will override if find error */
2755 match m = MATCH_YES;
2757 /* Make sure the actual arguments are in the necessary order (based on the
2758 formal args) before resolving. */
2759 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2761 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2762 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2764 set_name_and_label (c, sym, name, binding_label);
2766 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2768 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2770 /* Make sure we got a third arg if the second arg has non-zero
2771 rank. We must also check that the type and rank are
2772 correct since we short-circuit this check in
2773 gfc_procedure_use() (called above to sort actual args). */
2774 if (c->ext.actual->next->expr->rank != 0)
2776 if(c->ext.actual->next->next == NULL
2777 || c->ext.actual->next->next->expr == NULL)
2780 gfc_error ("Missing SHAPE parameter for call to %s "
2781 "at %L", sym->name, &(c->loc));
2783 else if (c->ext.actual->next->next->expr->ts.type
2785 || c->ext.actual->next->next->expr->rank != 1)
2788 gfc_error ("SHAPE parameter for call to %s at %L must "
2789 "be a rank 1 INTEGER array", sym->name,
2796 if (m != MATCH_ERROR)
2798 /* the 1 means to add the optional arg to formal list */
2799 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
2801 /* for error reporting, say it's declared where the original was */
2802 new_sym->declared_at = sym->declared_at;
2807 /* no differences for c_loc or c_funloc */
2811 /* set the resolved symbol */
2812 if (m != MATCH_ERROR)
2813 c->resolved_sym = new_sym;
2815 c->resolved_sym = sym;
2821 /* Resolve a subroutine call known to be specific. */
2824 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
2828 if(sym->attr.is_iso_c)
2830 m = gfc_iso_c_sub_interface (c,sym);
2834 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2836 if (sym->attr.dummy)
2838 sym->attr.proc = PROC_DUMMY;
2842 sym->attr.proc = PROC_EXTERNAL;
2846 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
2849 if (sym->attr.intrinsic)
2851 m = gfc_intrinsic_sub_interface (c, 1);
2855 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
2856 "with an intrinsic", sym->name, &c->loc);
2864 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2866 c->resolved_sym = sym;
2867 pure_subroutine (c, sym);
2874 resolve_specific_s (gfc_code *c)
2879 sym = c->symtree->n.sym;
2883 m = resolve_specific_s0 (c, sym);
2886 if (m == MATCH_ERROR)
2889 if (sym->ns->parent == NULL)
2892 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2898 sym = c->symtree->n.sym;
2899 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
2900 sym->name, &c->loc);
2906 /* Resolve a subroutine call not known to be generic nor specific. */
2909 resolve_unknown_s (gfc_code *c)
2913 sym = c->symtree->n.sym;
2915 if (sym->attr.dummy)
2917 sym->attr.proc = PROC_DUMMY;
2921 /* See if we have an intrinsic function reference. */
2923 if (gfc_is_intrinsic (sym, 1, c->loc))
2925 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
2930 /* The reference is to an external name. */
2933 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2935 c->resolved_sym = sym;
2937 pure_subroutine (c, sym);
2943 /* Resolve a subroutine call. Although it was tempting to use the same code
2944 for functions, subroutines and functions are stored differently and this
2945 makes things awkward. */
2948 resolve_call (gfc_code *c)
2951 procedure_type ptype = PROC_INTRINSIC;
2952 gfc_symbol *csym, *sym;
2953 bool no_formal_args;
2955 csym = c->symtree ? c->symtree->n.sym : NULL;
2957 if (csym && csym->ts.type != BT_UNKNOWN)
2959 gfc_error ("'%s' at %L has a type, which is not consistent with "
2960 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
2964 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
2967 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
2968 sym = st ? st->n.sym : NULL;
2969 if (sym && csym != sym
2970 && sym->ns == gfc_current_ns
2971 && sym->attr.flavor == FL_PROCEDURE
2972 && sym->attr.contained)
2975 if (csym->attr.generic)
2976 c->symtree->n.sym = sym;
2979 csym = c->symtree->n.sym;
2983 /* Subroutines without the RECURSIVE attribution are not allowed to
2984 * call themselves. */
2985 if (csym && is_illegal_recursion (csym, gfc_current_ns))
2987 if (csym->attr.entry && csym->ns->entries)
2988 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2989 " subroutine '%s' is not RECURSIVE",
2990 csym->name, &c->loc, csym->ns->entries->sym->name);
2992 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
2993 " is not RECURSIVE", csym->name, &c->loc);
2998 /* Switch off assumed size checking and do this again for certain kinds
2999 of procedure, once the procedure itself is resolved. */
3000 need_full_assumed_size++;
3003 ptype = csym->attr.proc;
3005 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3006 if (resolve_actual_arglist (c->ext.actual, ptype,
3007 no_formal_args) == FAILURE)
3010 /* Resume assumed_size checking. */
3011 need_full_assumed_size--;
3013 /* If external, check for usage. */
3014 if (csym && is_external_proc (csym))
3015 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3018 if (c->resolved_sym == NULL)
3020 c->resolved_isym = NULL;
3021 switch (procedure_kind (csym))
3024 t = resolve_generic_s (c);
3027 case PTYPE_SPECIFIC:
3028 t = resolve_specific_s (c);
3032 t = resolve_unknown_s (c);
3036 gfc_internal_error ("resolve_subroutine(): bad function type");
3040 /* Some checks of elemental subroutine actual arguments. */
3041 if (resolve_elemental_actual (NULL, c) == FAILURE)
3044 if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
3045 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
3050 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3051 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3052 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3053 if their shapes do not match. If either op1->shape or op2->shape is
3054 NULL, return SUCCESS. */
3057 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3064 if (op1->shape != NULL && op2->shape != NULL)
3066 for (i = 0; i < op1->rank; i++)
3068 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3070 gfc_error ("Shapes for operands at %L and %L are not conformable",
3071 &op1->where, &op2->where);
3082 /* Resolve an operator expression node. This can involve replacing the
3083 operation with a user defined function call. */
3086 resolve_operator (gfc_expr *e)
3088 gfc_expr *op1, *op2;
3090 bool dual_locus_error;
3093 /* Resolve all subnodes-- give them types. */
3095 switch (e->value.op.op)
3098 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3101 /* Fall through... */
3104 case INTRINSIC_UPLUS:
3105 case INTRINSIC_UMINUS:
3106 case INTRINSIC_PARENTHESES:
3107 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3112 /* Typecheck the new node. */
3114 op1 = e->value.op.op1;
3115 op2 = e->value.op.op2;
3116 dual_locus_error = false;
3118 if ((op1 && op1->expr_type == EXPR_NULL)
3119 || (op2 && op2->expr_type == EXPR_NULL))
3121 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3125 switch (e->value.op.op)
3127 case INTRINSIC_UPLUS:
3128 case INTRINSIC_UMINUS:
3129 if (op1->ts.type == BT_INTEGER
3130 || op1->ts.type == BT_REAL
3131 || op1->ts.type == BT_COMPLEX)
3137 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3138 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3141 case INTRINSIC_PLUS:
3142 case INTRINSIC_MINUS:
3143 case INTRINSIC_TIMES:
3144 case INTRINSIC_DIVIDE:
3145 case INTRINSIC_POWER:
3146 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3148 gfc_type_convert_binary (e);
3153 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3154 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3155 gfc_typename (&op2->ts));
3158 case INTRINSIC_CONCAT:
3159 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3160 && op1->ts.kind == op2->ts.kind)
3162 e->ts.type = BT_CHARACTER;
3163 e->ts.kind = op1->ts.kind;
3168 _("Operands of string concatenation operator at %%L are %s/%s"),
3169 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3175 case INTRINSIC_NEQV:
3176 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3178 e->ts.type = BT_LOGICAL;
3179 e->ts.kind = gfc_kind_max (op1, op2);
3180 if (op1->ts.kind < e->ts.kind)
3181 gfc_convert_type (op1, &e->ts, 2);
3182 else if (op2->ts.kind < e->ts.kind)
3183 gfc_convert_type (op2, &e->ts, 2);
3187 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3188 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3189 gfc_typename (&op2->ts));
3194 if (op1->ts.type == BT_LOGICAL)
3196 e->ts.type = BT_LOGICAL;
3197 e->ts.kind = op1->ts.kind;
3201 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3202 gfc_typename (&op1->ts));
3206 case INTRINSIC_GT_OS:
3208 case INTRINSIC_GE_OS:
3210 case INTRINSIC_LT_OS:
3212 case INTRINSIC_LE_OS:
3213 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3215 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3219 /* Fall through... */
3222 case INTRINSIC_EQ_OS:
3224 case INTRINSIC_NE_OS:
3225 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3226 && op1->ts.kind == op2->ts.kind)
3228 e->ts.type = BT_LOGICAL;
3229 e->ts.kind = gfc_default_logical_kind;
3233 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3235 gfc_type_convert_binary (e);
3237 e->ts.type = BT_LOGICAL;
3238 e->ts.kind = gfc_default_logical_kind;
3242 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3244 _("Logicals at %%L must be compared with %s instead of %s"),
3245 (e->value.op.op == INTRINSIC_EQ
3246 || e->value.op.op == INTRINSIC_EQ_OS)
3247 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3250 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3251 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3252 gfc_typename (&op2->ts));
3256 case INTRINSIC_USER:
3257 if (e->value.op.uop->op == NULL)
3258 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3259 else if (op2 == NULL)
3260 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3261 e->value.op.uop->name, gfc_typename (&op1->ts));
3263 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3264 e->value.op.uop->name, gfc_typename (&op1->ts),
3265 gfc_typename (&op2->ts));
3269 case INTRINSIC_PARENTHESES:
3271 if (e->ts.type == BT_CHARACTER)
3272 e->ts.cl = op1->ts.cl;
3276 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3279 /* Deal with arrayness of an operand through an operator. */
3283 switch (e->value.op.op)
3285 case INTRINSIC_PLUS:
3286 case INTRINSIC_MINUS:
3287 case INTRINSIC_TIMES:
3288 case INTRINSIC_DIVIDE:
3289 case INTRINSIC_POWER:
3290 case INTRINSIC_CONCAT:
3294 case INTRINSIC_NEQV:
3296 case INTRINSIC_EQ_OS:
3298 case INTRINSIC_NE_OS:
3300 case INTRINSIC_GT_OS:
3302 case INTRINSIC_GE_OS:
3304 case INTRINSIC_LT_OS:
3306 case INTRINSIC_LE_OS:
3308 if (op1->rank == 0 && op2->rank == 0)
3311 if (op1->rank == 0 && op2->rank != 0)
3313 e->rank = op2->rank;
3315 if (e->shape == NULL)
3316 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3319 if (op1->rank != 0 && op2->rank == 0)
3321 e->rank = op1->rank;
3323 if (e->shape == NULL)
3324 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3327 if (op1->rank != 0 && op2->rank != 0)
3329 if (op1->rank == op2->rank)
3331 e->rank = op1->rank;
3332 if (e->shape == NULL)
3334 t = compare_shapes(op1, op2);
3338 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3343 /* Allow higher level expressions to work. */
3346 /* Try user-defined operators, and otherwise throw an error. */
3347 dual_locus_error = true;
3349 _("Inconsistent ranks for operator at %%L and %%L"));
3356 case INTRINSIC_PARENTHESES:
3358 case INTRINSIC_UPLUS:
3359 case INTRINSIC_UMINUS:
3360 /* Simply copy arrayness attribute */
3361 e->rank = op1->rank;
3363 if (e->shape == NULL)
3364 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3372 /* Attempt to simplify the expression. */
3375 t = gfc_simplify_expr (e, 0);
3376 /* Some calls do not succeed in simplification and return FAILURE
3377 even though there is no error; e.g. variable references to
3378 PARAMETER arrays. */
3379 if (!gfc_is_constant_expr (e))
3386 if (gfc_extend_expr (e) == SUCCESS)
3389 if (dual_locus_error)
3390 gfc_error (msg, &op1->where, &op2->where);
3392 gfc_error (msg, &e->where);
3398 /************** Array resolution subroutines **************/
3401 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3404 /* Compare two integer expressions. */
3407 compare_bound (gfc_expr *a, gfc_expr *b)
3411 if (a == NULL || a->expr_type != EXPR_CONSTANT
3412 || b == NULL || b->expr_type != EXPR_CONSTANT)
3415 /* If either of the types isn't INTEGER, we must have
3416 raised an error earlier. */
3418 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3421 i = mpz_cmp (a->value.integer, b->value.integer);
3431 /* Compare an integer expression with an integer. */
3434 compare_bound_int (gfc_expr *a, int b)
3438 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3441 if (a->ts.type != BT_INTEGER)
3442 gfc_internal_error ("compare_bound_int(): Bad expression");
3444 i = mpz_cmp_si (a->value.integer, b);
3454 /* Compare an integer expression with a mpz_t. */
3457 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3461 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3464 if (a->ts.type != BT_INTEGER)
3465 gfc_internal_error ("compare_bound_int(): Bad expression");
3467 i = mpz_cmp (a->value.integer, b);
3477 /* Compute the last value of a sequence given by a triplet.
3478 Return 0 if it wasn't able to compute the last value, or if the
3479 sequence if empty, and 1 otherwise. */
3482 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3483 gfc_expr *stride, mpz_t last)
3487 if (start == NULL || start->expr_type != EXPR_CONSTANT
3488 || end == NULL || end->expr_type != EXPR_CONSTANT
3489 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3492 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3493 || (stride != NULL && stride->ts.type != BT_INTEGER))
3496 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3498 if (compare_bound (start, end) == CMP_GT)
3500 mpz_set (last, end->value.integer);
3504 if (compare_bound_int (stride, 0) == CMP_GT)
3506 /* Stride is positive */
3507 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3512 /* Stride is negative */
3513 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3518 mpz_sub (rem, end->value.integer, start->value.integer);
3519 mpz_tdiv_r (rem, rem, stride->value.integer);
3520 mpz_sub (last, end->value.integer, rem);
3527 /* Compare a single dimension of an array reference to the array
3531 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3535 /* Given start, end and stride values, calculate the minimum and
3536 maximum referenced indexes. */
3538 switch (ar->dimen_type[i])
3544 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3546 gfc_warning ("Array reference at %L is out of bounds "
3547 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3548 mpz_get_si (ar->start[i]->value.integer),
3549 mpz_get_si (as->lower[i]->value.integer), i+1);
3552 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3554 gfc_warning ("Array reference at %L is out of bounds "
3555 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3556 mpz_get_si (ar->start[i]->value.integer),
3557 mpz_get_si (as->upper[i]->value.integer), i+1);
3565 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3566 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3568 comparison comp_start_end = compare_bound (AR_START, AR_END);
3570 /* Check for zero stride, which is not allowed. */
3571 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3573 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3577 /* if start == len || (stride > 0 && start < len)
3578 || (stride < 0 && start > len),
3579 then the array section contains at least one element. In this
3580 case, there is an out-of-bounds access if
3581 (start < lower || start > upper). */
3582 if (compare_bound (AR_START, AR_END) == CMP_EQ
3583 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3584 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3585 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3586 && comp_start_end == CMP_GT))
3588 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3590 gfc_warning ("Lower array reference at %L is out of bounds "
3591 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3592 mpz_get_si (AR_START->value.integer),
3593 mpz_get_si (as->lower[i]->value.integer), i+1);
3596 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3598 gfc_warning ("Lower array reference at %L is out of bounds "
3599 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3600 mpz_get_si (AR_START->value.integer),
3601 mpz_get_si (as->upper[i]->value.integer), i+1);
3606 /* If we can compute the highest index of the array section,
3607 then it also has to be between lower and upper. */
3608 mpz_init (last_value);
3609 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3612 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3614 gfc_warning ("Upper array reference at %L is out of bounds "
3615 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3616 mpz_get_si (last_value),
3617 mpz_get_si (as->lower[i]->value.integer), i+1);
3618 mpz_clear (last_value);
3621 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3623 gfc_warning ("Upper array reference at %L is out of bounds "
3624 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3625 mpz_get_si (last_value),
3626 mpz_get_si (as->upper[i]->value.integer), i+1);
3627 mpz_clear (last_value);
3631 mpz_clear (last_value);
3639 gfc_internal_error ("check_dimension(): Bad array reference");
3646 /* Compare an array reference with an array specification. */
3649 compare_spec_to_ref (gfc_array_ref *ar)
3656 /* TODO: Full array sections are only allowed as actual parameters. */
3657 if (as->type == AS_ASSUMED_SIZE
3658 && (/*ar->type == AR_FULL
3659 ||*/ (ar->type == AR_SECTION
3660 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3662 gfc_error ("Rightmost upper bound of assumed size array section "
3663 "not specified at %L", &ar->where);
3667 if (ar->type == AR_FULL)
3670 if (as->rank != ar->dimen)
3672 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3673 &ar->where, ar->dimen, as->rank);
3677 for (i = 0; i < as->rank; i++)
3678 if (check_dimension (i, ar, as) == FAILURE)
3685 /* Resolve one part of an array index. */
3688 gfc_resolve_index (gfc_expr *index, int check_scalar)
3695 if (gfc_resolve_expr (index) == FAILURE)
3698 if (check_scalar && index->rank != 0)
3700 gfc_error ("Array index at %L must be scalar", &index->where);
3704 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
3706 gfc_error ("Array index at %L must be of INTEGER type, found %s",
3707 &index->where, gfc_basic_typename (index->ts.type));
3711 if (index->ts.type == BT_REAL)
3712 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
3713 &index->where) == FAILURE)
3716 if (index->ts.kind != gfc_index_integer_kind
3717 || index->ts.type != BT_INTEGER)
3720 ts.type = BT_INTEGER;
3721 ts.kind = gfc_index_integer_kind;
3723 gfc_convert_type_warn (index, &ts, 2, 0);
3729 /* Resolve a dim argument to an intrinsic function. */
3732 gfc_resolve_dim_arg (gfc_expr *dim)
3737 if (gfc_resolve_expr (dim) == FAILURE)
3742 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3747 if (dim->ts.type != BT_INTEGER)
3749 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3753 if (dim->ts.kind != gfc_index_integer_kind)
3757 ts.type = BT_INTEGER;
3758 ts.kind = gfc_index_integer_kind;
3760 gfc_convert_type_warn (dim, &ts, 2, 0);
3766 /* Given an expression that contains array references, update those array
3767 references to point to the right array specifications. While this is
3768 filled in during matching, this information is difficult to save and load
3769 in a module, so we take care of it here.
3771 The idea here is that the original array reference comes from the
3772 base symbol. We traverse the list of reference structures, setting
3773 the stored reference to references. Component references can
3774 provide an additional array specification. */
3777 find_array_spec (gfc_expr *e)
3781 gfc_symbol *derived;
3784 as = e->symtree->n.sym->as;
3787 for (ref = e->ref; ref; ref = ref->next)
3792 gfc_internal_error ("find_array_spec(): Missing spec");
3799 if (derived == NULL)
3800 derived = e->symtree->n.sym->ts.derived;
3802 c = derived->components;
3804 for (; c; c = c->next)
3805 if (c == ref->u.c.component)
3807 /* Track the sequence of component references. */
3808 if (c->ts.type == BT_DERIVED)
3809 derived = c->ts.derived;
3814 gfc_internal_error ("find_array_spec(): Component not found");
3816 if (c->attr.dimension)
3819 gfc_internal_error ("find_array_spec(): unused as(1)");
3830 gfc_internal_error ("find_array_spec(): unused as(2)");
3834 /* Resolve an array reference. */
3837 resolve_array_ref (gfc_array_ref *ar)
3839 int i, check_scalar;
3842 for (i = 0; i < ar->dimen; i++)
3844 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
3846 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
3848 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
3850 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
3855 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
3859 ar->dimen_type[i] = DIMEN_ELEMENT;
3863 ar->dimen_type[i] = DIMEN_VECTOR;
3864 if (e->expr_type == EXPR_VARIABLE
3865 && e->symtree->n.sym->ts.type == BT_DERIVED)
3866 ar->start[i] = gfc_get_parentheses (e);
3870 gfc_error ("Array index at %L is an array of rank %d",
3871 &ar->c_where[i], e->rank);
3876 /* If the reference type is unknown, figure out what kind it is. */
3878 if (ar->type == AR_UNKNOWN)
3880 ar->type = AR_ELEMENT;
3881 for (i = 0; i < ar->dimen; i++)
3882 if (ar->dimen_type[i] == DIMEN_RANGE
3883 || ar->dimen_type[i] == DIMEN_VECTOR)
3885 ar->type = AR_SECTION;
3890 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
3898 resolve_substring (gfc_ref *ref)
3900 if (ref->u.ss.start != NULL)
3902 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
3905 if (ref->u.ss.start->ts.type != BT_INTEGER)
3907 gfc_error ("Substring start index at %L must be of type INTEGER",
3908 &ref->u.ss.start->where);
3912 if (ref->u.ss.start->rank != 0)
3914 gfc_error ("Substring start index at %L must be scalar",
3915 &ref->u.ss.start->where);
3919 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
3920 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
3921 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
3923 gfc_error ("Substring start index at %L is less than one",
3924 &ref->u.ss.start->where);
3929 if (ref->u.ss.end != NULL)
3931 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
3934 if (ref->u.ss.end->ts.type != BT_INTEGER)
3936 gfc_error ("Substring end index at %L must be of type INTEGER",
3937 &ref->u.ss.end->where);
3941 if (ref->u.ss.end->rank != 0)
3943 gfc_error ("Substring end index at %L must be scalar",
3944 &ref->u.ss.end->where);
3948 if (ref->u.ss.length != NULL
3949 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
3950 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
3951 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
3953 gfc_error ("Substring end index at %L exceeds the string length",
3954 &ref->u.ss.start->where);
3963 /* This function supplies missing substring charlens. */
3966 gfc_resolve_substring_charlen (gfc_expr *e)
3969 gfc_expr *start, *end;
3971 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
3972 if (char_ref->type == REF_SUBSTRING)
3978 gcc_assert (char_ref->next == NULL);
3982 if (e->ts.cl->length)
3983 gfc_free_expr (e->ts.cl->length);
3984 else if (e->expr_type == EXPR_VARIABLE
3985 && e->symtree->n.sym->attr.dummy)
3989 e->ts.type = BT_CHARACTER;
3990 e->ts.kind = gfc_default_character_kind;
3994 e->ts.cl = gfc_get_charlen ();
3995 e->ts.cl->next = gfc_current_ns->cl_list;
3996 gfc_current_ns->cl_list = e->ts.cl;
3999 if (char_ref->u.ss.start)
4000 start = gfc_copy_expr (char_ref->u.ss.start);
4002 start = gfc_int_expr (1);
4004 if (char_ref->u.ss.end)
4005 end = gfc_copy_expr (char_ref->u.ss.end);
4006 else if (e->expr_type == EXPR_VARIABLE)
4007 end = gfc_copy_expr (e->symtree->n.sym->ts.cl->length);
4014 /* Length = (end - start +1). */
4015 e->ts.cl->length = gfc_subtract (end, start);
4016 e->ts.cl->length = gfc_add (e->ts.cl->length, gfc_int_expr (1));
4018 e->ts.cl->length->ts.type = BT_INTEGER;
4019 e->ts.cl->length->ts.kind = gfc_charlen_int_kind;;
4021 /* Make sure that the length is simplified. */
4022 gfc_simplify_expr (e->ts.cl->length, 1);
4023 gfc_resolve_expr (e->ts.cl->length);
4027 /* Resolve subtype references. */
4030 resolve_ref (gfc_expr *expr)
4032 int current_part_dimension, n_components, seen_part_dimension;
4035 for (ref = expr->ref; ref; ref = ref->next)
4036 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4038 find_array_spec (expr);
4042 for (ref = expr->ref; ref; ref = ref->next)
4046 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4054 resolve_substring (ref);
4058 /* Check constraints on part references. */
4060 current_part_dimension = 0;
4061 seen_part_dimension = 0;
4064 for (ref = expr->ref; ref; ref = ref->next)
4069 switch (ref->u.ar.type)
4073 current_part_dimension = 1;
4077 current_part_dimension = 0;
4081 gfc_internal_error ("resolve_ref(): Bad array reference");
4087 if (current_part_dimension || seen_part_dimension)
4089 if (ref->u.c.component->attr.pointer)
4091 gfc_error ("Component to the right of a part reference "
4092 "with nonzero rank must not have the POINTER "
4093 "attribute at %L", &expr->where);
4096 else if (ref->u.c.component->attr.allocatable)
4098 gfc_error ("Component to the right of a part reference "
4099 "with nonzero rank must not have the ALLOCATABLE "
4100 "attribute at %L", &expr->where);
4112 if (((ref->type == REF_COMPONENT && n_components > 1)
4113 || ref->next == NULL)
4114 && current_part_dimension
4115 && seen_part_dimension)
4117 gfc_error ("Two or more part references with nonzero rank must "
4118 "not be specified at %L", &expr->where);
4122 if (ref->type == REF_COMPONENT)
4124 if (current_part_dimension)
4125 seen_part_dimension = 1;
4127 /* reset to make sure */
4128 current_part_dimension = 0;
4136 /* Given an expression, determine its shape. This is easier than it sounds.
4137 Leaves the shape array NULL if it is not possible to determine the shape. */
4140 expression_shape (gfc_expr *e)
4142 mpz_t array[GFC_MAX_DIMENSIONS];
4145 if (e->rank == 0 || e->shape != NULL)
4148 for (i = 0; i < e->rank; i++)
4149 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4152 e->shape = gfc_get_shape (e->rank);
4154 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4159 for (i--; i >= 0; i--)
4160 mpz_clear (array[i]);
4164 /* Given a variable expression node, compute the rank of the expression by
4165 examining the base symbol and any reference structures it may have. */
4168 expression_rank (gfc_expr *e)
4173 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4174 could lead to serious confusion... */
4175 gcc_assert (e->expr_type != EXPR_COMPCALL);
4179 if (e->expr_type == EXPR_ARRAY)
4181 /* Constructors can have a rank different from one via RESHAPE(). */
4183 if (e->symtree == NULL)
4189 e->rank = (e->symtree->n.sym->as == NULL)
4190 ? 0 : e->symtree->n.sym->as->rank;
4196 for (ref = e->ref; ref; ref = ref->next)
4198 if (ref->type != REF_ARRAY)
4201 if (ref->u.ar.type == AR_FULL)
4203 rank = ref->u.ar.as->rank;
4207 if (ref->u.ar.type == AR_SECTION)
4209 /* Figure out the rank of the section. */
4211 gfc_internal_error ("expression_rank(): Two array specs");
4213 for (i = 0; i < ref->u.ar.dimen; i++)
4214 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4215 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4225 expression_shape (e);
4229 /* Resolve a variable expression. */
4232 resolve_variable (gfc_expr *e)
4239 if (e->symtree == NULL)
4242 if (e->ref && resolve_ref (e) == FAILURE)
4245 sym = e->symtree->n.sym;
4246 if (sym->attr.flavor == FL_PROCEDURE
4247 && (!sym->attr.function
4248 || (sym->attr.function && sym->result
4249 && sym->result->attr.proc_pointer
4250 && !sym->result->attr.function)))
4252 e->ts.type = BT_PROCEDURE;
4253 goto resolve_procedure;
4256 if (sym->ts.type != BT_UNKNOWN)
4257 gfc_variable_attr (e, &e->ts);
4260 /* Must be a simple variable reference. */
4261 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4266 if (check_assumed_size_reference (sym, e))
4269 /* Deal with forward references to entries during resolve_code, to
4270 satisfy, at least partially, 12.5.2.5. */
4271 if (gfc_current_ns->entries
4272 && current_entry_id == sym->entry_id
4275 && cs_base->current->op != EXEC_ENTRY)
4277 gfc_entry_list *entry;
4278 gfc_formal_arglist *formal;
4282 /* If the symbol is a dummy... */
4283 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4285 entry = gfc_current_ns->entries;
4288 /* ...test if the symbol is a parameter of previous entries. */
4289 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4290 for (formal = entry->sym->formal; formal; formal = formal->next)
4292 if (formal->sym && sym->name == formal->sym->name)
4296 /* If it has not been seen as a dummy, this is an error. */
4299 if (specification_expr)
4300 gfc_error ("Variable '%s', used in a specification expression"
4301 ", is referenced at %L before the ENTRY statement "
4302 "in which it is a parameter",
4303 sym->name, &cs_base->current->loc);
4305 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4306 "statement in which it is a parameter",
4307 sym->name, &cs_base->current->loc);
4312 /* Now do the same check on the specification expressions. */
4313 specification_expr = 1;
4314 if (sym->ts.type == BT_CHARACTER
4315 && gfc_resolve_expr (sym->ts.cl->length) == FAILURE)
4319 for (n = 0; n < sym->as->rank; n++)
4321 specification_expr = 1;
4322 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4324 specification_expr = 1;
4325 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4328 specification_expr = 0;
4331 /* Update the symbol's entry level. */
4332 sym->entry_id = current_entry_id + 1;
4336 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
4343 /* Checks to see that the correct symbol has been host associated.
4344 The only situation where this arises is that in which a twice
4345 contained function is parsed after the host association is made.
4346 Therefore, on detecting this, change the symbol in the expression
4347 and convert the array reference into an actual arglist if the old
4348 symbol is a variable. */
4350 check_host_association (gfc_expr *e)
4352 gfc_symbol *sym, *old_sym;
4356 gfc_actual_arglist *arg, *tail = NULL;
4357 bool retval = e->expr_type == EXPR_FUNCTION;
4359 /* If the expression is the result of substitution in
4360 interface.c(gfc_extend_expr) because there is no way in
4361 which the host association can be wrong. */
4362 if (e->symtree == NULL
4363 || e->symtree->n.sym == NULL
4364 || e->user_operator)
4367 old_sym = e->symtree->n.sym;
4369 if (gfc_current_ns->parent
4370 && old_sym->ns != gfc_current_ns)
4372 /* Use the 'USE' name so that renamed module symbols are
4373 correctly handled. */
4374 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
4376 if (sym && old_sym != sym
4377 && sym->ts.type == old_sym->ts.type
4378 && sym->attr.flavor == FL_PROCEDURE
4379 && sym->attr.contained)
4381 /* Clear the shape, since it might not be valid. */
4382 if (e->shape != NULL)
4384 for (n = 0; n < e->rank; n++)
4385 mpz_clear (e->shape[n]);
4387 gfc_free (e->shape);
4390 /* Give the symbol a symtree in the right place! */
4391 gfc_get_sym_tree (sym->name, gfc_current_ns, &st);
4394 if (old_sym->attr.flavor == FL_PROCEDURE)
4396 /* Original was function so point to the new symbol, since
4397 the actual argument list is already attached to the
4399 e->value.function.esym = NULL;
4404 /* Original was variable so convert array references into
4405 an actual arglist. This does not need any checking now
4406 since gfc_resolve_function will take care of it. */
4407 e->value.function.actual = NULL;
4408 e->expr_type = EXPR_FUNCTION;
4411 /* Ambiguity will not arise if the array reference is not
4412 the last reference. */
4413 for (ref = e->ref; ref; ref = ref->next)
4414 if (ref->type == REF_ARRAY && ref->next == NULL)
4417 gcc_assert (ref->type == REF_ARRAY);
4419 /* Grab the start expressions from the array ref and
4420 copy them into actual arguments. */
4421 for (n = 0; n < ref->u.ar.dimen; n++)
4423 arg = gfc_get_actual_arglist ();
4424 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4425 if (e->value.function.actual == NULL)
4426 tail = e->value.function.actual = arg;
4434 /* Dump the reference list and set the rank. */
4435 gfc_free_ref_list (e->ref);
4437 e->rank = sym->as ? sym->as->rank : 0;
4440 gfc_resolve_expr (e);
4444 /* This might have changed! */
4445 return e->expr_type == EXPR_FUNCTION;
4450 gfc_resolve_character_operator (gfc_expr *e)
4452 gfc_expr *op1 = e->value.op.op1;
4453 gfc_expr *op2 = e->value.op.op2;
4454 gfc_expr *e1 = NULL;
4455 gfc_expr *e2 = NULL;
4457 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
4459 if (op1->ts.cl && op1->ts.cl->length)
4460 e1 = gfc_copy_expr (op1->ts.cl->length);
4461 else if (op1->expr_type == EXPR_CONSTANT)
4462 e1 = gfc_int_expr (op1->value.character.length);
4464 if (op2->ts.cl && op2->ts.cl->length)
4465 e2 = gfc_copy_expr (op2->ts.cl->length);
4466 else if (op2->expr_type == EXPR_CONSTANT)
4467 e2 = gfc_int_expr (op2->value.character.length);
4469 e->ts.cl = gfc_get_charlen ();
4470 e->ts.cl->next = gfc_current_ns->cl_list;
4471 gfc_current_ns->cl_list = e->ts.cl;
4476 e->ts.cl->length = gfc_add (e1, e2);
4477 e->ts.cl->length->ts.type = BT_INTEGER;
4478 e->ts.cl->length->ts.kind = gfc_charlen_int_kind;;
4479 gfc_simplify_expr (e->ts.cl->length, 0);
4480 gfc_resolve_expr (e->ts.cl->length);
4486 /* Ensure that an character expression has a charlen and, if possible, a
4487 length expression. */
4490 fixup_charlen (gfc_expr *e)
4492 /* The cases fall through so that changes in expression type and the need
4493 for multiple fixes are picked up. In all circumstances, a charlen should
4494 be available for the middle end to hang a backend_decl on. */
4495 switch (e->expr_type)
4498 gfc_resolve_character_operator (e);
4501 if (e->expr_type == EXPR_ARRAY)
4502 gfc_resolve_character_array_constructor (e);
4504 case EXPR_SUBSTRING:
4505 if (!e->ts.cl && e->ref)
4506 gfc_resolve_substring_charlen (e);
4511 e->ts.cl = gfc_get_charlen ();
4512 e->ts.cl->next = gfc_current_ns->cl_list;
4513 gfc_current_ns->cl_list = e->ts.cl;
4521 /* Update an actual argument to include the passed-object for type-bound
4522 procedures at the right position. */
4524 static gfc_actual_arglist*
4525 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos)
4527 gcc_assert (argpos > 0);
4531 gfc_actual_arglist* result;
4533 result = gfc_get_actual_arglist ();
4541 gcc_assert (argpos > 1);
4543 lst->next = update_arglist_pass (lst->next, po, argpos - 1);
4548 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
4551 extract_compcall_passed_object (gfc_expr* e)
4555 gcc_assert (e->expr_type == EXPR_COMPCALL);
4557 po = gfc_get_expr ();
4558 po->expr_type = EXPR_VARIABLE;
4559 po->symtree = e->symtree;
4560 po->ref = gfc_copy_ref (e->ref);
4562 if (gfc_resolve_expr (po) == FAILURE)
4569 /* Update the arglist of an EXPR_COMPCALL expression to include the
4573 update_compcall_arglist (gfc_expr* e)
4576 gfc_typebound_proc* tbp;
4578 tbp = e->value.compcall.tbp;
4583 po = extract_compcall_passed_object (e);
4589 gfc_error ("Passed-object at %L must be scalar", &e->where);
4599 gcc_assert (tbp->pass_arg_num > 0);
4600 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4607 /* Check that the object a TBP is called on is valid, i.e. it must not be
4608 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
4611 check_typebound_baseobject (gfc_expr* e)
4615 base = extract_compcall_passed_object (e);
4619 gcc_assert (base->ts.type == BT_DERIVED);
4620 if (base->ts.derived->attr.abstract)
4622 gfc_error ("Base object for type-bound procedure call at %L is of"
4623 " ABSTRACT type '%s'", &e->where, base->ts.derived->name);
4631 /* Resolve a call to a type-bound procedure, either function or subroutine,
4632 statically from the data in an EXPR_COMPCALL expression. The adapted
4633 arglist and the target-procedure symtree are returned. */
4636 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
4637 gfc_actual_arglist** actual)
4639 gcc_assert (e->expr_type == EXPR_COMPCALL);
4640 gcc_assert (!e->value.compcall.tbp->is_generic);
4642 /* Update the actual arglist for PASS. */
4643 if (update_compcall_arglist (e) == FAILURE)
4646 *actual = e->value.compcall.actual;
4647 *target = e->value.compcall.tbp->u.specific;
4649 gfc_free_ref_list (e->ref);
4651 e->value.compcall.actual = NULL;
4657 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
4658 which of the specific bindings (if any) matches the arglist and transform
4659 the expression into a call of that binding. */
4662 resolve_typebound_generic_call (gfc_expr* e)
4664 gfc_typebound_proc* genproc;
4665 const char* genname;
4667 gcc_assert (e->expr_type == EXPR_COMPCALL);
4668 genname = e->value.compcall.name;
4669 genproc = e->value.compcall.tbp;
4671 if (!genproc->is_generic)
4674 /* Try the bindings on this type and in the inheritance hierarchy. */
4675 for (; genproc; genproc = genproc->overridden)
4679 gcc_assert (genproc->is_generic);
4680 for (g = genproc->u.generic; g; g = g->next)
4683 gfc_actual_arglist* args;
4686 gcc_assert (g->specific);
4688 if (g->specific->error)
4691 target = g->specific->u.specific->n.sym;
4693 /* Get the right arglist by handling PASS/NOPASS. */
4694 args = gfc_copy_actual_arglist (e->value.compcall.actual);
4695 if (!g->specific->nopass)
4698 po = extract_compcall_passed_object (e);
4702 gcc_assert (g->specific->pass_arg_num > 0);
4703 gcc_assert (!g->specific->error);
4704 args = update_arglist_pass (args, po, g->specific->pass_arg_num);
4706 resolve_actual_arglist (args, target->attr.proc,
4707 is_external_proc (target) && !target->formal);
4709 /* Check if this arglist matches the formal. */
4710 matches = gfc_arglist_matches_symbol (&args, target);
4712 /* Clean up and break out of the loop if we've found it. */
4713 gfc_free_actual_arglist (args);
4716 e->value.compcall.tbp = g->specific;
4722 /* Nothing matching found! */
4723 gfc_error ("Found no matching specific binding for the call to the GENERIC"
4724 " '%s' at %L", genname, &e->where);
4732 /* Resolve a call to a type-bound subroutine. */
4735 resolve_typebound_call (gfc_code* c)
4737 gfc_actual_arglist* newactual;
4738 gfc_symtree* target;
4740 /* Check that's really a SUBROUTINE. */
4741 if (!c->expr1->value.compcall.tbp->subroutine)
4743 gfc_error ("'%s' at %L should be a SUBROUTINE",
4744 c->expr1->value.compcall.name, &c->loc);
4748 if (check_typebound_baseobject (c->expr1) == FAILURE)
4751 if (resolve_typebound_generic_call (c->expr1) == FAILURE)
4754 /* Transform into an ordinary EXEC_CALL for now. */
4756 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
4759 c->ext.actual = newactual;
4760 c->symtree = target;
4763 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
4764 gfc_free_expr (c->expr1);
4767 return resolve_call (c);
4771 /* Resolve a component-call expression. */
4774 resolve_compcall (gfc_expr* e)
4776 gfc_actual_arglist* newactual;
4777 gfc_symtree* target;
4779 /* Check that's really a FUNCTION. */
4780 if (!e->value.compcall.tbp->function)
4782 gfc_error ("'%s' at %L should be a FUNCTION",
4783 e->value.compcall.name, &e->where);
4787 if (check_typebound_baseobject (e) == FAILURE)
4790 if (resolve_typebound_generic_call (e) == FAILURE)
4792 gcc_assert (!e->value.compcall.tbp->is_generic);
4794 /* Take the rank from the function's symbol. */
4795 if (e->value.compcall.tbp->u.specific->n.sym->as)
4796 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
4798 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
4799 arglist to the TBP's binding target. */
4801 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
4804 e->value.function.actual = newactual;
4805 e->value.function.name = e->value.compcall.name;
4806 e->value.function.isym = NULL;
4807 e->value.function.esym = NULL;
4808 e->symtree = target;
4809 e->ts = target->n.sym->ts;
4810 e->expr_type = EXPR_FUNCTION;
4812 return gfc_resolve_expr (e);
4816 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
4819 resolve_ppc_call (gfc_code* c)
4821 gfc_component *comp;
4822 gcc_assert (is_proc_ptr_comp (c->expr1, &comp));
4824 c->resolved_sym = c->expr1->symtree->n.sym;
4825 c->expr1->expr_type = EXPR_VARIABLE;
4826 c->ext.actual = c->expr1->value.compcall.actual;
4828 if (!comp->attr.subroutine)
4829 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
4831 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
4832 comp->formal == NULL) == FAILURE)
4835 /* TODO: Check actual arguments.
4836 gfc_procedure_use (stree->n.sym, &c->expr1->value.compcall.actual,
4837 &c->expr1->where);*/
4843 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
4846 resolve_expr_ppc (gfc_expr* e)
4848 gfc_component *comp;
4849 gcc_assert (is_proc_ptr_comp (e, &comp));
4851 /* Convert to EXPR_FUNCTION. */
4852 e->expr_type = EXPR_FUNCTION;
4853 e->value.function.isym = NULL;
4854 e->value.function.actual = e->value.compcall.actual;
4857 if (!comp->attr.function)
4858 gfc_add_function (&comp->attr, comp->name, &e->where);
4860 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
4861 comp->formal == NULL) == FAILURE)
4864 /* TODO: Check actual arguments.
4865 gfc_procedure_use (stree->n.sym, &e->value.compcall.actual, &e->where); */
4871 /* Resolve an expression. That is, make sure that types of operands agree
4872 with their operators, intrinsic operators are converted to function calls
4873 for overloaded types and unresolved function references are resolved. */
4876 gfc_resolve_expr (gfc_expr *e)
4883 switch (e->expr_type)
4886 t = resolve_operator (e);
4892 if (check_host_association (e))
4893 t = resolve_function (e);
4896 t = resolve_variable (e);
4898 expression_rank (e);
4901 if (e->ts.type == BT_CHARACTER && e->ts.cl == NULL && e->ref
4902 && e->ref->type != REF_SUBSTRING)
4903 gfc_resolve_substring_charlen (e);
4908 t = resolve_compcall (e);
4911 case EXPR_SUBSTRING:
4912 t = resolve_ref (e);
4921 t = resolve_expr_ppc (e);
4926 if (resolve_ref (e) == FAILURE)
4929 t = gfc_resolve_array_constructor (e);
4930 /* Also try to expand a constructor. */
4933 expression_rank (e);
4934 gfc_expand_constructor (e);
4937 /* This provides the opportunity for the length of constructors with
4938 character valued function elements to propagate the string length
4939 to the expression. */
4940 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
4941 t = gfc_resolve_character_array_constructor (e);
4945 case EXPR_STRUCTURE:
4946 t = resolve_ref (e);
4950 t = resolve_structure_cons (e);
4954 t = gfc_simplify_expr (e, 0);
4958 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
4961 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.cl)
4968 /* Resolve an expression from an iterator. They must be scalar and have
4969 INTEGER or (optionally) REAL type. */
4972 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
4973 const char *name_msgid)
4975 if (gfc_resolve_expr (expr) == FAILURE)
4978 if (expr->rank != 0)
4980 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
4984 if (expr->ts.type != BT_INTEGER)
4986 if (expr->ts.type == BT_REAL)
4989 return gfc_notify_std (GFC_STD_F95_DEL,
4990 "Deleted feature: %s at %L must be integer",
4991 _(name_msgid), &expr->where);
4994 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
5001 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
5009 /* Resolve the expressions in an iterator structure. If REAL_OK is
5010 false allow only INTEGER type iterators, otherwise allow REAL types. */
5013 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
5015 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
5019 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
5021 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5026 if (gfc_resolve_iterator_expr (iter->start, real_ok,
5027 "Start expression in DO loop") == FAILURE)
5030 if (gfc_resolve_iterator_expr (iter->end, real_ok,
5031 "End expression in DO loop") == FAILURE)
5034 if (gfc_resolve_iterator_expr (iter->step, real_ok,
5035 "Step expression in DO loop") == FAILURE)
5038 if (iter->step->expr_type == EXPR_CONSTANT)
5040 if ((iter->step->ts.type == BT_INTEGER
5041 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
5042 || (iter->step->ts.type == BT_REAL
5043 && mpfr_sgn (iter->step->value.real) == 0))
5045 gfc_error ("Step expression in DO loop at %L cannot be zero",
5046 &iter->step->where);
5051 /* Convert start, end, and step to the same type as var. */
5052 if (iter->start->ts.kind != iter->var->ts.kind
5053 || iter->start->ts.type != iter->var->ts.type)
5054 gfc_convert_type (iter->start, &iter->var->ts, 2);
5056 if (iter->end->ts.kind != iter->var->ts.kind
5057 || iter->end->ts.type != iter->var->ts.type)
5058 gfc_convert_type (iter->end, &iter->var->ts, 2);
5060 if (iter->step->ts.kind != iter->var->ts.kind
5061 || iter->step->ts.type != iter->var->ts.type)
5062 gfc_convert_type (iter->step, &iter->var->ts, 2);
5064 if (iter->start->expr_type == EXPR_CONSTANT
5065 && iter->end->expr_type == EXPR_CONSTANT
5066 && iter->step->expr_type == EXPR_CONSTANT)
5069 if (iter->start->ts.type == BT_INTEGER)
5071 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
5072 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
5076 sgn = mpfr_sgn (iter->step->value.real);
5077 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
5079 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
5080 gfc_warning ("DO loop at %L will be executed zero times",
5081 &iter->step->where);
5088 /* Traversal function for find_forall_index. f == 2 signals that
5089 that variable itself is not to be checked - only the references. */
5092 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
5094 if (expr->expr_type != EXPR_VARIABLE)
5097 /* A scalar assignment */
5098 if (!expr->ref || *f == 1)
5100 if (expr->symtree->n.sym == sym)
5112 /* Check whether the FORALL index appears in the expression or not.
5113 Returns SUCCESS if SYM is found in EXPR. */
5116 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
5118 if (gfc_traverse_expr (expr, sym, forall_index, f))
5125 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5126 to be a scalar INTEGER variable. The subscripts and stride are scalar
5127 INTEGERs, and if stride is a constant it must be nonzero.
5128 Furthermore "A subscript or stride in a forall-triplet-spec shall
5129 not contain a reference to any index-name in the
5130 forall-triplet-spec-list in which it appears." (7.5.4.1) */
5133 resolve_forall_iterators (gfc_forall_iterator *it)
5135 gfc_forall_iterator *iter, *iter2;
5137 for (iter = it; iter; iter = iter->next)
5139 if (gfc_resolve_expr (iter->var) == SUCCESS
5140 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
5141 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
5144 if (gfc_resolve_expr (iter->start) == SUCCESS
5145 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
5146 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
5147 &iter->start->where);
5148 if (iter->var->ts.kind != iter->start->ts.kind)
5149 gfc_convert_type (iter->start, &iter->var->ts, 2);
5151 if (gfc_resolve_expr (iter->end) == SUCCESS
5152 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
5153 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
5155 if (iter->var->ts.kind != iter->end->ts.kind)
5156 gfc_convert_type (iter->end, &iter->var->ts, 2);
5158 if (gfc_resolve_expr (iter->stride) == SUCCESS)
5160 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
5161 gfc_error ("FORALL stride expression at %L must be a scalar %s",
5162 &iter->stride->where, "INTEGER");
5164 if (iter->stride->expr_type == EXPR_CONSTANT
5165 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
5166 gfc_error ("FORALL stride expression at %L cannot be zero",
5167 &iter->stride->where);
5169 if (iter->var->ts.kind != iter->stride->ts.kind)
5170 gfc_convert_type (iter->stride, &iter->var->ts, 2);
5173 for (iter = it; iter; iter = iter->next)
5174 for (iter2 = iter; iter2; iter2 = iter2->next)
5176 if (find_forall_index (iter2->start,
5177 iter->var->symtree->n.sym, 0) == SUCCESS
5178 || find_forall_index (iter2->end,
5179 iter->var->symtree->n.sym, 0) == SUCCESS
5180 || find_forall_index (iter2->stride,
5181 iter->var->symtree->n.sym, 0) == SUCCESS)
5182 gfc_error ("FORALL index '%s' may not appear in triplet "
5183 "specification at %L", iter->var->symtree->name,
5184 &iter2->start->where);
5189 /* Given a pointer to a symbol that is a derived type, see if it's
5190 inaccessible, i.e. if it's defined in another module and the components are
5191 PRIVATE. The search is recursive if necessary. Returns zero if no
5192 inaccessible components are found, nonzero otherwise. */
5195 derived_inaccessible (gfc_symbol *sym)
5199 if (sym->attr.use_assoc && sym->attr.private_comp)
5202 for (c = sym->components; c; c = c->next)
5204 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.derived))
5212 /* Resolve the argument of a deallocate expression. The expression must be
5213 a pointer or a full array. */
5216 resolve_deallocate_expr (gfc_expr *e)
5218 symbol_attribute attr;
5219 int allocatable, pointer, check_intent_in;
5222 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5223 check_intent_in = 1;
5225 if (gfc_resolve_expr (e) == FAILURE)
5228 if (e->expr_type != EXPR_VARIABLE)
5231 allocatable = e->symtree->n.sym->attr.allocatable;
5232 pointer = e->symtree->n.sym->attr.pointer;
5233 for (ref = e->ref; ref; ref = ref->next)
5236 check_intent_in = 0;
5241 if (ref->u.ar.type != AR_FULL)
5246 allocatable = (ref->u.c.component->as != NULL
5247 && ref->u.c.component->as->type == AS_DEFERRED);
5248 pointer = ref->u.c.component->attr.pointer;
5257 attr = gfc_expr_attr (e);
5259 if (allocatable == 0 && attr.pointer == 0)
5262 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5267 && e->symtree->n.sym->attr.intent == INTENT_IN)
5269 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
5270 e->symtree->n.sym->name, &e->where);
5278 /* Returns true if the expression e contains a reference to the symbol sym. */
5280 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5282 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
5289 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
5291 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
5295 /* Given the expression node e for an allocatable/pointer of derived type to be
5296 allocated, get the expression node to be initialized afterwards (needed for
5297 derived types with default initializers, and derived types with allocatable
5298 components that need nullification.) */
5301 expr_to_initialize (gfc_expr *e)
5307 result = gfc_copy_expr (e);
5309 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
5310 for (ref = result->ref; ref; ref = ref->next)
5311 if (ref->type == REF_ARRAY && ref->next == NULL)
5313 ref->u.ar.type = AR_FULL;
5315 for (i = 0; i < ref->u.ar.dimen; i++)
5316 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
5318 result->rank = ref->u.ar.dimen;
5326 /* Resolve the expression in an ALLOCATE statement, doing the additional
5327 checks to see whether the expression is OK or not. The expression must
5328 have a trailing array reference that gives the size of the array. */
5331 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
5333 int i, pointer, allocatable, dimension, check_intent_in;
5334 symbol_attribute attr;
5335 gfc_ref *ref, *ref2;
5342 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5343 check_intent_in = 1;
5345 if (gfc_resolve_expr (e) == FAILURE)
5348 /* Make sure the expression is allocatable or a pointer. If it is
5349 pointer, the next-to-last reference must be a pointer. */
5353 if (e->expr_type != EXPR_VARIABLE)
5356 attr = gfc_expr_attr (e);
5357 pointer = attr.pointer;
5358 dimension = attr.dimension;
5362 allocatable = e->symtree->n.sym->attr.allocatable;
5363 pointer = e->symtree->n.sym->attr.pointer;
5364 dimension = e->symtree->n.sym->attr.dimension;
5366 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
5369 check_intent_in = 0;
5374 if (ref->next != NULL)
5379 allocatable = (ref->u.c.component->as != NULL
5380 && ref->u.c.component->as->type == AS_DEFERRED);
5382 pointer = ref->u.c.component->attr.pointer;
5383 dimension = ref->u.c.component->attr.dimension;
5394 if (allocatable == 0 && pointer == 0)
5396 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5402 && e->symtree->n.sym->attr.intent == INTENT_IN)
5404 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
5405 e->symtree->n.sym->name, &e->where);
5409 /* Add default initializer for those derived types that need them. */
5410 if (e->ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&e->ts)))
5412 init_st = gfc_get_code ();
5413 init_st->loc = code->loc;
5414 init_st->op = EXEC_INIT_ASSIGN;
5415 init_st->expr1 = expr_to_initialize (e);
5416 init_st->expr2 = init_e;
5417 init_st->next = code->next;
5418 code->next = init_st;
5421 if (pointer && dimension == 0)
5424 /* Make sure the next-to-last reference node is an array specification. */
5426 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
5428 gfc_error ("Array specification required in ALLOCATE statement "
5429 "at %L", &e->where);
5433 /* Make sure that the array section reference makes sense in the
5434 context of an ALLOCATE specification. */
5438 for (i = 0; i < ar->dimen; i++)
5440 if (ref2->u.ar.type == AR_ELEMENT)
5443 switch (ar->dimen_type[i])
5449 if (ar->start[i] != NULL
5450 && ar->end[i] != NULL
5451 && ar->stride[i] == NULL)
5454 /* Fall Through... */
5458 gfc_error ("Bad array specification in ALLOCATE statement at %L",
5465 for (a = code->ext.alloc_list; a; a = a->next)
5467 sym = a->expr->symtree->n.sym;
5469 /* TODO - check derived type components. */
5470 if (sym->ts.type == BT_DERIVED)
5473 if ((ar->start[i] != NULL
5474 && gfc_find_sym_in_expr (sym, ar->start[i]))
5475 || (ar->end[i] != NULL
5476 && gfc_find_sym_in_expr (sym, ar->end[i])))
5478 gfc_error ("'%s' must not appear in the array specification at "
5479 "%L in the same ALLOCATE statement where it is "
5480 "itself allocated", sym->name, &ar->where);
5490 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
5492 gfc_expr *stat, *errmsg, *pe, *qe;
5493 gfc_alloc *a, *p, *q;
5495 stat = code->expr1 ? code->expr1 : NULL;
5497 errmsg = code->expr2 ? code->expr2 : NULL;
5499 /* Check the stat variable. */
5502 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
5503 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
5504 stat->symtree->n.sym->name, &stat->where);
5506 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
5507 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
5510 if (stat->ts.type != BT_INTEGER
5511 && !(stat->ref && (stat->ref->type == REF_ARRAY
5512 || stat->ref->type == REF_COMPONENT)))
5513 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
5514 "variable", &stat->where);
5516 for (p = code->ext.alloc_list; p; p = p->next)
5517 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
5518 gfc_error ("Stat-variable at %L shall not be %sd within "
5519 "the same %s statement", &stat->where, fcn, fcn);
5522 /* Check the errmsg variable. */
5526 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
5529 if (errmsg->symtree->n.sym->attr.intent == INTENT_IN)
5530 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
5531 errmsg->symtree->n.sym->name, &errmsg->where);
5533 if (gfc_pure (NULL) && gfc_impure_variable (errmsg->symtree->n.sym))
5534 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
5537 if (errmsg->ts.type != BT_CHARACTER
5539 && (errmsg->ref->type == REF_ARRAY
5540 || errmsg->ref->type == REF_COMPONENT)))
5541 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
5542 "variable", &errmsg->where);
5544 for (p = code->ext.alloc_list; p; p = p->next)
5545 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
5546 gfc_error ("Errmsg-variable at %L shall not be %sd within "
5547 "the same %s statement", &errmsg->where, fcn, fcn);
5550 /* Check that an allocate-object appears only once in the statement.
5551 FIXME: Checking derived types is disabled. */
5552 for (p = code->ext.alloc_list; p; p = p->next)
5555 if ((pe->ref && pe->ref->type != REF_COMPONENT)
5556 && (pe->symtree->n.sym->ts.type != BT_DERIVED))
5558 for (q = p->next; q; q = q->next)
5561 if ((qe->ref && qe->ref->type != REF_COMPONENT)
5562 && (qe->symtree->n.sym->ts.type != BT_DERIVED)
5563 && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
5564 gfc_error ("Allocate-object at %L also appears at %L",
5565 &pe->where, &qe->where);
5570 if (strcmp (fcn, "ALLOCATE") == 0)
5572 for (a = code->ext.alloc_list; a; a = a->next)
5573 resolve_allocate_expr (a->expr, code);
5577 for (a = code->ext.alloc_list; a; a = a->next)
5578 resolve_deallocate_expr (a->expr);
5583 /************ SELECT CASE resolution subroutines ************/
5585 /* Callback function for our mergesort variant. Determines interval
5586 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
5587 op1 > op2. Assumes we're not dealing with the default case.
5588 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
5589 There are nine situations to check. */
5592 compare_cases (const gfc_case *op1, const gfc_case *op2)
5596 if (op1->low == NULL) /* op1 = (:L) */
5598 /* op2 = (:N), so overlap. */
5600 /* op2 = (M:) or (M:N), L < M */
5601 if (op2->low != NULL
5602 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
5605 else if (op1->high == NULL) /* op1 = (K:) */
5607 /* op2 = (M:), so overlap. */
5609 /* op2 = (:N) or (M:N), K > N */
5610 if (op2->high != NULL
5611 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
5614 else /* op1 = (K:L) */
5616 if (op2->low == NULL) /* op2 = (:N), K > N */
5617 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
5619 else if (op2->high == NULL) /* op2 = (M:), L < M */
5620 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
5622 else /* op2 = (M:N) */
5626 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
5629 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
5638 /* Merge-sort a double linked case list, detecting overlap in the
5639 process. LIST is the head of the double linked case list before it
5640 is sorted. Returns the head of the sorted list if we don't see any
5641 overlap, or NULL otherwise. */
5644 check_case_overlap (gfc_case *list)
5646 gfc_case *p, *q, *e, *tail;
5647 int insize, nmerges, psize, qsize, cmp, overlap_seen;
5649 /* If the passed list was empty, return immediately. */
5656 /* Loop unconditionally. The only exit from this loop is a return
5657 statement, when we've finished sorting the case list. */
5664 /* Count the number of merges we do in this pass. */
5667 /* Loop while there exists a merge to be done. */
5672 /* Count this merge. */
5675 /* Cut the list in two pieces by stepping INSIZE places
5676 forward in the list, starting from P. */
5679 for (i = 0; i < insize; i++)
5688 /* Now we have two lists. Merge them! */
5689 while (psize > 0 || (qsize > 0 && q != NULL))
5691 /* See from which the next case to merge comes from. */
5694 /* P is empty so the next case must come from Q. */
5699 else if (qsize == 0 || q == NULL)
5708 cmp = compare_cases (p, q);
5711 /* The whole case range for P is less than the
5719 /* The whole case range for Q is greater than
5720 the case range for P. */
5727 /* The cases overlap, or they are the same
5728 element in the list. Either way, we must
5729 issue an error and get the next case from P. */
5730 /* FIXME: Sort P and Q by line number. */
5731 gfc_error ("CASE label at %L overlaps with CASE "
5732 "label at %L", &p->where, &q->where);
5740 /* Add the next element to the merged list. */
5749 /* P has now stepped INSIZE places along, and so has Q. So
5750 they're the same. */
5755 /* If we have done only one merge or none at all, we've
5756 finished sorting the cases. */
5765 /* Otherwise repeat, merging lists twice the size. */
5771 /* Check to see if an expression is suitable for use in a CASE statement.
5772 Makes sure that all case expressions are scalar constants of the same
5773 type. Return FAILURE if anything is wrong. */
5776 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
5778 if (e == NULL) return SUCCESS;
5780 if (e->ts.type != case_expr->ts.type)
5782 gfc_error ("Expression in CASE statement at %L must be of type %s",
5783 &e->where, gfc_basic_typename (case_expr->ts.type));
5787 /* C805 (R808) For a given case-construct, each case-value shall be of
5788 the same type as case-expr. For character type, length differences
5789 are allowed, but the kind type parameters shall be the same. */
5791 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
5793 gfc_error ("Expression in CASE statement at %L must be of kind %d",
5794 &e->where, case_expr->ts.kind);
5798 /* Convert the case value kind to that of case expression kind, if needed.
5799 FIXME: Should a warning be issued? */
5800 if (e->ts.kind != case_expr->ts.kind)
5801 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
5805 gfc_error ("Expression in CASE statement at %L must be scalar",
5814 /* Given a completely parsed select statement, we:
5816 - Validate all expressions and code within the SELECT.
5817 - Make sure that the selection expression is not of the wrong type.
5818 - Make sure that no case ranges overlap.
5819 - Eliminate unreachable cases and unreachable code resulting from
5820 removing case labels.
5822 The standard does allow unreachable cases, e.g. CASE (5:3). But
5823 they are a hassle for code generation, and to prevent that, we just
5824 cut them out here. This is not necessary for overlapping cases
5825 because they are illegal and we never even try to generate code.
5827 We have the additional caveat that a SELECT construct could have
5828 been a computed GOTO in the source code. Fortunately we can fairly
5829 easily work around that here: The case_expr for a "real" SELECT CASE
5830 is in code->expr1, but for a computed GOTO it is in code->expr2. All
5831 we have to do is make sure that the case_expr is a scalar integer
5835 resolve_select (gfc_code *code)
5838 gfc_expr *case_expr;
5839 gfc_case *cp, *default_case, *tail, *head;
5840 int seen_unreachable;
5846 if (code->expr1 == NULL)
5848 /* This was actually a computed GOTO statement. */
5849 case_expr = code->expr2;
5850 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
5851 gfc_error ("Selection expression in computed GOTO statement "
5852 "at %L must be a scalar integer expression",
5855 /* Further checking is not necessary because this SELECT was built
5856 by the compiler, so it should always be OK. Just move the
5857 case_expr from expr2 to expr so that we can handle computed
5858 GOTOs as normal SELECTs from here on. */
5859 code->expr1 = code->expr2;
5864 case_expr = code->expr1;
5866 type = case_expr->ts.type;
5867 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
5869 gfc_error ("Argument of SELECT statement at %L cannot be %s",
5870 &case_expr->where, gfc_typename (&case_expr->ts));
5872 /* Punt. Going on here just produce more garbage error messages. */
5876 if (case_expr->rank != 0)
5878 gfc_error ("Argument of SELECT statement at %L must be a scalar "
5879 "expression", &case_expr->where);
5885 /* PR 19168 has a long discussion concerning a mismatch of the kinds
5886 of the SELECT CASE expression and its CASE values. Walk the lists
5887 of case values, and if we find a mismatch, promote case_expr to
5888 the appropriate kind. */
5890 if (type == BT_LOGICAL || type == BT_INTEGER)
5892 for (body = code->block; body; body = body->block)
5894 /* Walk the case label list. */
5895 for (cp = body->ext.case_list; cp; cp = cp->next)
5897 /* Intercept the DEFAULT case. It does not have a kind. */
5898 if (cp->low == NULL && cp->high == NULL)
5901 /* Unreachable case ranges are discarded, so ignore. */
5902 if (cp->low != NULL && cp->high != NULL
5903 && cp->low != cp->high
5904 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5907 /* FIXME: Should a warning be issued? */
5909 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
5910 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
5912 if (cp->high != NULL
5913 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
5914 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
5919 /* Assume there is no DEFAULT case. */
5920 default_case = NULL;
5925 for (body = code->block; body; body = body->block)
5927 /* Assume the CASE list is OK, and all CASE labels can be matched. */
5929 seen_unreachable = 0;
5931 /* Walk the case label list, making sure that all case labels
5933 for (cp = body->ext.case_list; cp; cp = cp->next)
5935 /* Count the number of cases in the whole construct. */
5938 /* Intercept the DEFAULT case. */
5939 if (cp->low == NULL && cp->high == NULL)
5941 if (default_case != NULL)
5943 gfc_error ("The DEFAULT CASE at %L cannot be followed "
5944 "by a second DEFAULT CASE at %L",
5945 &default_case->where, &cp->where);
5956 /* Deal with single value cases and case ranges. Errors are
5957 issued from the validation function. */
5958 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
5959 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
5965 if (type == BT_LOGICAL
5966 && ((cp->low == NULL || cp->high == NULL)
5967 || cp->low != cp->high))
5969 gfc_error ("Logical range in CASE statement at %L is not "
5970 "allowed", &cp->low->where);
5975 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
5978 value = cp->low->value.logical == 0 ? 2 : 1;
5979 if (value & seen_logical)
5981 gfc_error ("constant logical value in CASE statement "
5982 "is repeated at %L",
5987 seen_logical |= value;
5990 if (cp->low != NULL && cp->high != NULL
5991 && cp->low != cp->high
5992 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5994 if (gfc_option.warn_surprising)
5995 gfc_warning ("Range specification at %L can never "
5996 "be matched", &cp->where);
5998 cp->unreachable = 1;
5999 seen_unreachable = 1;
6003 /* If the case range can be matched, it can also overlap with
6004 other cases. To make sure it does not, we put it in a
6005 double linked list here. We sort that with a merge sort
6006 later on to detect any overlapping cases. */
6010 head->right = head->left = NULL;
6015 tail->right->left = tail;
6022 /* It there was a failure in the previous case label, give up
6023 for this case label list. Continue with the next block. */
6027 /* See if any case labels that are unreachable have been seen.
6028 If so, we eliminate them. This is a bit of a kludge because
6029 the case lists for a single case statement (label) is a
6030 single forward linked lists. */
6031 if (seen_unreachable)
6033 /* Advance until the first case in the list is reachable. */
6034 while (body->ext.case_list != NULL
6035 && body->ext.case_list->unreachable)
6037 gfc_case *n = body->ext.case_list;
6038 body->ext.case_list = body->ext.case_list->next;
6040 gfc_free_case_list (n);
6043 /* Strip all other unreachable cases. */
6044 if (body->ext.case_list)
6046 for (cp = body->ext.case_list; cp->next; cp = cp->next)
6048 if (cp->next->unreachable)
6050 gfc_case *n = cp->next;
6051 cp->next = cp->next->next;
6053 gfc_free_case_list (n);
6060 /* See if there were overlapping cases. If the check returns NULL,
6061 there was overlap. In that case we don't do anything. If head
6062 is non-NULL, we prepend the DEFAULT case. The sorted list can
6063 then used during code generation for SELECT CASE constructs with
6064 a case expression of a CHARACTER type. */
6067 head = check_case_overlap (head);
6069 /* Prepend the default_case if it is there. */
6070 if (head != NULL && default_case)
6072 default_case->left = NULL;
6073 default_case->right = head;
6074 head->left = default_case;
6078 /* Eliminate dead blocks that may be the result if we've seen
6079 unreachable case labels for a block. */
6080 for (body = code; body && body->block; body = body->block)
6082 if (body->block->ext.case_list == NULL)
6084 /* Cut the unreachable block from the code chain. */
6085 gfc_code *c = body->block;
6086 body->block = c->block;
6088 /* Kill the dead block, but not the blocks below it. */
6090 gfc_free_statements (c);
6094 /* More than two cases is legal but insane for logical selects.
6095 Issue a warning for it. */
6096 if (gfc_option.warn_surprising && type == BT_LOGICAL
6098 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
6103 /* Resolve a transfer statement. This is making sure that:
6104 -- a derived type being transferred has only non-pointer components
6105 -- a derived type being transferred doesn't have private components, unless
6106 it's being transferred from the module where the type was defined
6107 -- we're not trying to transfer a whole assumed size array. */
6110 resolve_transfer (gfc_code *code)
6119 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
6122 sym = exp->symtree->n.sym;
6125 /* Go to actual component transferred. */
6126 for (ref = code->expr1->ref; ref; ref = ref->next)
6127 if (ref->type == REF_COMPONENT)
6128 ts = &ref->u.c.component->ts;
6130 if (ts->type == BT_DERIVED)
6132 /* Check that transferred derived type doesn't contain POINTER
6134 if (ts->derived->attr.pointer_comp)
6136 gfc_error ("Data transfer element at %L cannot have "
6137 "POINTER components", &code->loc);
6141 if (ts->derived->attr.alloc_comp)
6143 gfc_error ("Data transfer element at %L cannot have "
6144 "ALLOCATABLE components", &code->loc);
6148 if (derived_inaccessible (ts->derived))
6150 gfc_error ("Data transfer element at %L cannot have "
6151 "PRIVATE components",&code->loc);
6156 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
6157 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
6159 gfc_error ("Data transfer element at %L cannot be a full reference to "
6160 "an assumed-size array", &code->loc);
6166 /*********** Toplevel code resolution subroutines ***********/
6168 /* Find the set of labels that are reachable from this block. We also
6169 record the last statement in each block. */
6172 find_reachable_labels (gfc_code *block)
6179 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
6181 /* Collect labels in this block. We don't keep those corresponding
6182 to END {IF|SELECT}, these are checked in resolve_branch by going
6183 up through the code_stack. */
6184 for (c = block; c; c = c->next)
6186 if (c->here && c->op != EXEC_END_BLOCK)
6187 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
6190 /* Merge with labels from parent block. */
6193 gcc_assert (cs_base->prev->reachable_labels);
6194 bitmap_ior_into (cs_base->reachable_labels,
6195 cs_base->prev->reachable_labels);
6199 /* Given a branch to a label, see if the branch is conforming.
6200 The code node describes where the branch is located. */
6203 resolve_branch (gfc_st_label *label, gfc_code *code)
6210 /* Step one: is this a valid branching target? */
6212 if (label->defined == ST_LABEL_UNKNOWN)
6214 gfc_error ("Label %d referenced at %L is never defined", label->value,
6219 if (label->defined != ST_LABEL_TARGET)
6221 gfc_error ("Statement at %L is not a valid branch target statement "
6222 "for the branch statement at %L", &label->where, &code->loc);
6226 /* Step two: make sure this branch is not a branch to itself ;-) */
6228 if (code->here == label)
6230 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
6234 /* Step three: See if the label is in the same block as the
6235 branching statement. The hard work has been done by setting up
6236 the bitmap reachable_labels. */
6238 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
6241 /* Step four: If we haven't found the label in the bitmap, it may
6242 still be the label of the END of the enclosing block, in which
6243 case we find it by going up the code_stack. */
6245 for (stack = cs_base; stack; stack = stack->prev)
6246 if (stack->current->next && stack->current->next->here == label)
6251 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
6255 /* The label is not in an enclosing block, so illegal. This was
6256 allowed in Fortran 66, so we allow it as extension. No
6257 further checks are necessary in this case. */
6258 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
6259 "as the GOTO statement at %L", &label->where,
6265 /* Check whether EXPR1 has the same shape as EXPR2. */
6268 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
6270 mpz_t shape[GFC_MAX_DIMENSIONS];
6271 mpz_t shape2[GFC_MAX_DIMENSIONS];
6272 gfc_try result = FAILURE;
6275 /* Compare the rank. */
6276 if (expr1->rank != expr2->rank)
6279 /* Compare the size of each dimension. */
6280 for (i=0; i<expr1->rank; i++)
6282 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
6285 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
6288 if (mpz_cmp (shape[i], shape2[i]))
6292 /* When either of the two expression is an assumed size array, we
6293 ignore the comparison of dimension sizes. */
6298 for (i--; i >= 0; i--)
6300 mpz_clear (shape[i]);
6301 mpz_clear (shape2[i]);
6307 /* Check whether a WHERE assignment target or a WHERE mask expression
6308 has the same shape as the outmost WHERE mask expression. */
6311 resolve_where (gfc_code *code, gfc_expr *mask)
6317 cblock = code->block;
6319 /* Store the first WHERE mask-expr of the WHERE statement or construct.
6320 In case of nested WHERE, only the outmost one is stored. */
6321 if (mask == NULL) /* outmost WHERE */
6323 else /* inner WHERE */
6330 /* Check if the mask-expr has a consistent shape with the
6331 outmost WHERE mask-expr. */
6332 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
6333 gfc_error ("WHERE mask at %L has inconsistent shape",
6334 &cblock->expr1->where);
6337 /* the assignment statement of a WHERE statement, or the first
6338 statement in where-body-construct of a WHERE construct */
6339 cnext = cblock->next;
6344 /* WHERE assignment statement */
6347 /* Check shape consistent for WHERE assignment target. */
6348 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
6349 gfc_error ("WHERE assignment target at %L has "
6350 "inconsistent shape", &cnext->expr1->where);
6354 case EXEC_ASSIGN_CALL:
6355 resolve_call (cnext);
6356 if (!cnext->resolved_sym->attr.elemental)
6357 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
6358 &cnext->ext.actual->expr->where);
6361 /* WHERE or WHERE construct is part of a where-body-construct */
6363 resolve_where (cnext, e);
6367 gfc_error ("Unsupported statement inside WHERE at %L",
6370 /* the next statement within the same where-body-construct */
6371 cnext = cnext->next;
6373 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
6374 cblock = cblock->block;
6379 /* Resolve assignment in FORALL construct.
6380 NVAR is the number of FORALL index variables, and VAR_EXPR records the
6381 FORALL index variables. */
6384 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
6388 for (n = 0; n < nvar; n++)
6390 gfc_symbol *forall_index;
6392 forall_index = var_expr[n]->symtree->n.sym;
6394 /* Check whether the assignment target is one of the FORALL index
6396 if ((code->expr1->expr_type == EXPR_VARIABLE)
6397 && (code->expr1->symtree->n.sym == forall_index))
6398 gfc_error ("Assignment to a FORALL index variable at %L",
6399 &code->expr1->where);
6402 /* If one of the FORALL index variables doesn't appear in the
6403 assignment variable, then there could be a many-to-one
6404 assignment. Emit a warning rather than an error because the
6405 mask could be resolving this problem. */
6406 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
6407 gfc_warning ("The FORALL with index '%s' is not used on the "
6408 "left side of the assignment at %L and so might "
6409 "cause multiple assignment to this object",
6410 var_expr[n]->symtree->name, &code->expr1->where);
6416 /* Resolve WHERE statement in FORALL construct. */
6419 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
6420 gfc_expr **var_expr)
6425 cblock = code->block;
6428 /* the assignment statement of a WHERE statement, or the first
6429 statement in where-body-construct of a WHERE construct */
6430 cnext = cblock->next;
6435 /* WHERE assignment statement */
6437 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
6440 /* WHERE operator assignment statement */
6441 case EXEC_ASSIGN_CALL:
6442 resolve_call (cnext);
6443 if (!cnext->resolved_sym->attr.elemental)
6444 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
6445 &cnext->ext.actual->expr->where);
6448 /* WHERE or WHERE construct is part of a where-body-construct */
6450 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
6454 gfc_error ("Unsupported statement inside WHERE at %L",
6457 /* the next statement within the same where-body-construct */
6458 cnext = cnext->next;
6460 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
6461 cblock = cblock->block;
6466 /* Traverse the FORALL body to check whether the following errors exist:
6467 1. For assignment, check if a many-to-one assignment happens.
6468 2. For WHERE statement, check the WHERE body to see if there is any
6469 many-to-one assignment. */
6472 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
6476 c = code->block->next;
6482 case EXEC_POINTER_ASSIGN:
6483 gfc_resolve_assign_in_forall (c, nvar, var_expr);
6486 case EXEC_ASSIGN_CALL:
6490 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
6491 there is no need to handle it here. */
6495 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
6500 /* The next statement in the FORALL body. */
6506 /* Counts the number of iterators needed inside a forall construct, including
6507 nested forall constructs. This is used to allocate the needed memory
6508 in gfc_resolve_forall. */
6511 gfc_count_forall_iterators (gfc_code *code)
6513 int max_iters, sub_iters, current_iters;
6514 gfc_forall_iterator *fa;
6516 gcc_assert(code->op == EXEC_FORALL);
6520 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
6523 code = code->block->next;
6527 if (code->op == EXEC_FORALL)
6529 sub_iters = gfc_count_forall_iterators (code);
6530 if (sub_iters > max_iters)
6531 max_iters = sub_iters;
6536 return current_iters + max_iters;
6540 /* Given a FORALL construct, first resolve the FORALL iterator, then call
6541 gfc_resolve_forall_body to resolve the FORALL body. */
6544 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
6546 static gfc_expr **var_expr;
6547 static int total_var = 0;
6548 static int nvar = 0;
6550 gfc_forall_iterator *fa;
6555 /* Start to resolve a FORALL construct */
6556 if (forall_save == 0)
6558 /* Count the total number of FORALL index in the nested FORALL
6559 construct in order to allocate the VAR_EXPR with proper size. */
6560 total_var = gfc_count_forall_iterators (code);
6562 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
6563 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
6566 /* The information about FORALL iterator, including FORALL index start, end
6567 and stride. The FORALL index can not appear in start, end or stride. */
6568 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
6570 /* Check if any outer FORALL index name is the same as the current
6572 for (i = 0; i < nvar; i++)
6574 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
6576 gfc_error ("An outer FORALL construct already has an index "
6577 "with this name %L", &fa->var->where);
6581 /* Record the current FORALL index. */
6582 var_expr[nvar] = gfc_copy_expr (fa->var);
6586 /* No memory leak. */
6587 gcc_assert (nvar <= total_var);
6590 /* Resolve the FORALL body. */
6591 gfc_resolve_forall_body (code, nvar, var_expr);
6593 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
6594 gfc_resolve_blocks (code->block, ns);
6598 /* Free only the VAR_EXPRs allocated in this frame. */
6599 for (i = nvar; i < tmp; i++)
6600 gfc_free_expr (var_expr[i]);
6604 /* We are in the outermost FORALL construct. */
6605 gcc_assert (forall_save == 0);
6607 /* VAR_EXPR is not needed any more. */
6608 gfc_free (var_expr);
6614 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL ,GOTO and
6617 static void resolve_code (gfc_code *, gfc_namespace *);
6620 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
6624 for (; b; b = b->block)
6626 t = gfc_resolve_expr (b->expr1);
6627 if (gfc_resolve_expr (b->expr2) == FAILURE)
6633 if (t == SUCCESS && b->expr1 != NULL
6634 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
6635 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
6642 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
6643 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
6648 resolve_branch (b->label1, b);
6661 case EXEC_OMP_ATOMIC:
6662 case EXEC_OMP_CRITICAL:
6664 case EXEC_OMP_MASTER:
6665 case EXEC_OMP_ORDERED:
6666 case EXEC_OMP_PARALLEL:
6667 case EXEC_OMP_PARALLEL_DO:
6668 case EXEC_OMP_PARALLEL_SECTIONS:
6669 case EXEC_OMP_PARALLEL_WORKSHARE:
6670 case EXEC_OMP_SECTIONS:
6671 case EXEC_OMP_SINGLE:
6673 case EXEC_OMP_TASKWAIT:
6674 case EXEC_OMP_WORKSHARE:
6678 gfc_internal_error ("resolve_block(): Bad block type");
6681 resolve_code (b->next, ns);
6686 /* Does everything to resolve an ordinary assignment. Returns true
6687 if this is an interface assignment. */
6689 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
6699 if (gfc_extend_assign (code, ns) == SUCCESS)
6701 lhs = code->ext.actual->expr;
6702 rhs = code->ext.actual->next->expr;
6703 if (gfc_pure (NULL) && !gfc_pure (code->symtree->n.sym))
6705 gfc_error ("Subroutine '%s' called instead of assignment at "
6706 "%L must be PURE", code->symtree->n.sym->name,
6711 /* Make a temporary rhs when there is a default initializer
6712 and rhs is the same symbol as the lhs. */
6713 if (rhs->expr_type == EXPR_VARIABLE
6714 && rhs->symtree->n.sym->ts.type == BT_DERIVED
6715 && has_default_initializer (rhs->symtree->n.sym->ts.derived)
6716 && (lhs->symtree->n.sym == rhs->symtree->n.sym))
6717 code->ext.actual->next->expr = gfc_get_parentheses (rhs);
6726 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
6727 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
6728 &code->loc) == FAILURE)
6731 /* Handle the case of a BOZ literal on the RHS. */
6732 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
6735 if (gfc_option.warn_surprising)
6736 gfc_warning ("BOZ literal at %L is bitwise transferred "
6737 "non-integer symbol '%s'", &code->loc,
6738 lhs->symtree->n.sym->name);
6740 if (!gfc_convert_boz (rhs, &lhs->ts))
6742 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
6744 if (rc == ARITH_UNDERFLOW)
6745 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
6746 ". This check can be disabled with the option "
6747 "-fno-range-check", &rhs->where);
6748 else if (rc == ARITH_OVERFLOW)
6749 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
6750 ". This check can be disabled with the option "
6751 "-fno-range-check", &rhs->where);
6752 else if (rc == ARITH_NAN)
6753 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
6754 ". This check can be disabled with the option "
6755 "-fno-range-check", &rhs->where);
6761 if (lhs->ts.type == BT_CHARACTER
6762 && gfc_option.warn_character_truncation)
6764 if (lhs->ts.cl != NULL
6765 && lhs->ts.cl->length != NULL
6766 && lhs->ts.cl->length->expr_type == EXPR_CONSTANT)
6767 llen = mpz_get_si (lhs->ts.cl->length->value.integer);
6769 if (rhs->expr_type == EXPR_CONSTANT)
6770 rlen = rhs->value.character.length;
6772 else if (rhs->ts.cl != NULL
6773 && rhs->ts.cl->length != NULL
6774 && rhs->ts.cl->length->expr_type == EXPR_CONSTANT)
6775 rlen = mpz_get_si (rhs->ts.cl->length->value.integer);
6777 if (rlen && llen && rlen > llen)
6778 gfc_warning_now ("CHARACTER expression will be truncated "
6779 "in assignment (%d/%d) at %L",
6780 llen, rlen, &code->loc);
6783 /* Ensure that a vector index expression for the lvalue is evaluated
6784 to a temporary if the lvalue symbol is referenced in it. */
6787 for (ref = lhs->ref; ref; ref= ref->next)
6788 if (ref->type == REF_ARRAY)
6790 for (n = 0; n < ref->u.ar.dimen; n++)
6791 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
6792 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
6793 ref->u.ar.start[n]))
6795 = gfc_get_parentheses (ref->u.ar.start[n]);
6799 if (gfc_pure (NULL))
6801 if (gfc_impure_variable (lhs->symtree->n.sym))
6803 gfc_error ("Cannot assign to variable '%s' in PURE "
6805 lhs->symtree->n.sym->name,
6810 if (lhs->ts.type == BT_DERIVED
6811 && lhs->expr_type == EXPR_VARIABLE
6812 && lhs->ts.derived->attr.pointer_comp
6813 && gfc_impure_variable (rhs->symtree->n.sym))
6815 gfc_error ("The impure variable at %L is assigned to "
6816 "a derived type variable with a POINTER "
6817 "component in a PURE procedure (12.6)",
6823 gfc_check_assign (lhs, rhs, 1);
6827 /* Given a block of code, recursively resolve everything pointed to by this
6831 resolve_code (gfc_code *code, gfc_namespace *ns)
6833 int omp_workshare_save;
6838 frame.prev = cs_base;
6842 find_reachable_labels (code);
6844 for (; code; code = code->next)
6846 frame.current = code;
6847 forall_save = forall_flag;
6849 if (code->op == EXEC_FORALL)
6852 gfc_resolve_forall (code, ns, forall_save);
6855 else if (code->block)
6857 omp_workshare_save = -1;
6860 case EXEC_OMP_PARALLEL_WORKSHARE:
6861 omp_workshare_save = omp_workshare_flag;
6862 omp_workshare_flag = 1;
6863 gfc_resolve_omp_parallel_blocks (code, ns);
6865 case EXEC_OMP_PARALLEL:
6866 case EXEC_OMP_PARALLEL_DO:
6867 case EXEC_OMP_PARALLEL_SECTIONS:
6869 omp_workshare_save = omp_workshare_flag;
6870 omp_workshare_flag = 0;
6871 gfc_resolve_omp_parallel_blocks (code, ns);
6874 gfc_resolve_omp_do_blocks (code, ns);
6876 case EXEC_OMP_WORKSHARE:
6877 omp_workshare_save = omp_workshare_flag;
6878 omp_workshare_flag = 1;
6881 gfc_resolve_blocks (code->block, ns);
6885 if (omp_workshare_save != -1)
6886 omp_workshare_flag = omp_workshare_save;
6890 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
6891 t = gfc_resolve_expr (code->expr1);
6892 forall_flag = forall_save;
6894 if (gfc_resolve_expr (code->expr2) == FAILURE)
6900 case EXEC_END_BLOCK:
6910 /* Keep track of which entry we are up to. */
6911 current_entry_id = code->ext.entry->id;
6915 resolve_where (code, NULL);
6919 if (code->expr1 != NULL)
6921 if (code->expr1->ts.type != BT_INTEGER)
6922 gfc_error ("ASSIGNED GOTO statement at %L requires an "
6923 "INTEGER variable", &code->expr1->where);
6924 else if (code->expr1->symtree->n.sym->attr.assign != 1)
6925 gfc_error ("Variable '%s' has not been assigned a target "
6926 "label at %L", code->expr1->symtree->n.sym->name,
6927 &code->expr1->where);
6930 resolve_branch (code->label1, code);
6934 if (code->expr1 != NULL
6935 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
6936 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
6937 "INTEGER return specifier", &code->expr1->where);
6940 case EXEC_INIT_ASSIGN:
6941 case EXEC_END_PROCEDURE:
6948 if (resolve_ordinary_assign (code, ns))
6953 case EXEC_LABEL_ASSIGN:
6954 if (code->label1->defined == ST_LABEL_UNKNOWN)
6955 gfc_error ("Label %d referenced at %L is never defined",
6956 code->label1->value, &code->label1->where);
6958 && (code->expr1->expr_type != EXPR_VARIABLE
6959 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
6960 || code->expr1->symtree->n.sym->ts.kind
6961 != gfc_default_integer_kind
6962 || code->expr1->symtree->n.sym->as != NULL))
6963 gfc_error ("ASSIGN statement at %L requires a scalar "
6964 "default INTEGER variable", &code->expr1->where);
6967 case EXEC_POINTER_ASSIGN:
6971 gfc_check_pointer_assign (code->expr1, code->expr2);
6974 case EXEC_ARITHMETIC_IF:
6976 && code->expr1->ts.type != BT_INTEGER
6977 && code->expr1->ts.type != BT_REAL)
6978 gfc_error ("Arithmetic IF statement at %L requires a numeric "
6979 "expression", &code->expr1->where);
6981 resolve_branch (code->label1, code);
6982 resolve_branch (code->label2, code);
6983 resolve_branch (code->label3, code);
6987 if (t == SUCCESS && code->expr1 != NULL
6988 && (code->expr1->ts.type != BT_LOGICAL
6989 || code->expr1->rank != 0))
6990 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
6991 &code->expr1->where);
6996 resolve_call (code);
7000 resolve_typebound_call (code);
7004 resolve_ppc_call (code);
7008 /* Select is complicated. Also, a SELECT construct could be
7009 a transformed computed GOTO. */
7010 resolve_select (code);
7014 if (code->ext.iterator != NULL)
7016 gfc_iterator *iter = code->ext.iterator;
7017 if (gfc_resolve_iterator (iter, true) != FAILURE)
7018 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
7023 if (code->expr1 == NULL)
7024 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
7026 && (code->expr1->rank != 0
7027 || code->expr1->ts.type != BT_LOGICAL))
7028 gfc_error ("Exit condition of DO WHILE loop at %L must be "
7029 "a scalar LOGICAL expression", &code->expr1->where);
7034 resolve_allocate_deallocate (code, "ALLOCATE");
7038 case EXEC_DEALLOCATE:
7040 resolve_allocate_deallocate (code, "DEALLOCATE");
7045 if (gfc_resolve_open (code->ext.open) == FAILURE)
7048 resolve_branch (code->ext.open->err, code);
7052 if (gfc_resolve_close (code->ext.close) == FAILURE)
7055 resolve_branch (code->ext.close->err, code);
7058 case EXEC_BACKSPACE:
7062 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
7065 resolve_branch (code->ext.filepos->err, code);
7069 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
7072 resolve_branch (code->ext.inquire->err, code);
7076 gcc_assert (code->ext.inquire != NULL);
7077 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
7080 resolve_branch (code->ext.inquire->err, code);
7084 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
7087 resolve_branch (code->ext.wait->err, code);
7088 resolve_branch (code->ext.wait->end, code);
7089 resolve_branch (code->ext.wait->eor, code);
7094 if (gfc_resolve_dt (code->ext.dt) == FAILURE)
7097 resolve_branch (code->ext.dt->err, code);
7098 resolve_branch (code->ext.dt->end, code);
7099 resolve_branch (code->ext.dt->eor, code);
7103 resolve_transfer (code);
7107 resolve_forall_iterators (code->ext.forall_iterator);
7109 if (code->expr1 != NULL && code->expr1->ts.type != BT_LOGICAL)
7110 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
7111 "expression", &code->expr1->where);
7114 case EXEC_OMP_ATOMIC:
7115 case EXEC_OMP_BARRIER:
7116 case EXEC_OMP_CRITICAL:
7117 case EXEC_OMP_FLUSH:
7119 case EXEC_OMP_MASTER:
7120 case EXEC_OMP_ORDERED:
7121 case EXEC_OMP_SECTIONS:
7122 case EXEC_OMP_SINGLE:
7123 case EXEC_OMP_TASKWAIT:
7124 case EXEC_OMP_WORKSHARE:
7125 gfc_resolve_omp_directive (code, ns);
7128 case EXEC_OMP_PARALLEL:
7129 case EXEC_OMP_PARALLEL_DO:
7130 case EXEC_OMP_PARALLEL_SECTIONS:
7131 case EXEC_OMP_PARALLEL_WORKSHARE:
7133 omp_workshare_save = omp_workshare_flag;
7134 omp_workshare_flag = 0;
7135 gfc_resolve_omp_directive (code, ns);
7136 omp_workshare_flag = omp_workshare_save;
7140 gfc_internal_error ("resolve_code(): Bad statement code");
7144 cs_base = frame.prev;
7148 /* Resolve initial values and make sure they are compatible with
7152 resolve_values (gfc_symbol *sym)
7154 if (sym->value == NULL)
7157 if (gfc_resolve_expr (sym->value) == FAILURE)
7160 gfc_check_assign_symbol (sym, sym->value);
7164 /* Verify the binding labels for common blocks that are BIND(C). The label
7165 for a BIND(C) common block must be identical in all scoping units in which
7166 the common block is declared. Further, the binding label can not collide
7167 with any other global entity in the program. */
7170 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
7172 if (comm_block_tree->n.common->is_bind_c == 1)
7174 gfc_gsymbol *binding_label_gsym;
7175 gfc_gsymbol *comm_name_gsym;
7177 /* See if a global symbol exists by the common block's name. It may
7178 be NULL if the common block is use-associated. */
7179 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
7180 comm_block_tree->n.common->name);
7181 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
7182 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
7183 "with the global entity '%s' at %L",
7184 comm_block_tree->n.common->binding_label,
7185 comm_block_tree->n.common->name,
7186 &(comm_block_tree->n.common->where),
7187 comm_name_gsym->name, &(comm_name_gsym->where));
7188 else if (comm_name_gsym != NULL
7189 && strcmp (comm_name_gsym->name,
7190 comm_block_tree->n.common->name) == 0)
7192 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
7194 if (comm_name_gsym->binding_label == NULL)
7195 /* No binding label for common block stored yet; save this one. */
7196 comm_name_gsym->binding_label =
7197 comm_block_tree->n.common->binding_label;
7199 if (strcmp (comm_name_gsym->binding_label,
7200 comm_block_tree->n.common->binding_label) != 0)
7202 /* Common block names match but binding labels do not. */
7203 gfc_error ("Binding label '%s' for common block '%s' at %L "
7204 "does not match the binding label '%s' for common "
7206 comm_block_tree->n.common->binding_label,
7207 comm_block_tree->n.common->name,
7208 &(comm_block_tree->n.common->where),
7209 comm_name_gsym->binding_label,
7210 comm_name_gsym->name,
7211 &(comm_name_gsym->where));
7216 /* There is no binding label (NAME="") so we have nothing further to
7217 check and nothing to add as a global symbol for the label. */
7218 if (comm_block_tree->n.common->binding_label[0] == '\0' )
7221 binding_label_gsym =
7222 gfc_find_gsymbol (gfc_gsym_root,
7223 comm_block_tree->n.common->binding_label);
7224 if (binding_label_gsym == NULL)
7226 /* Need to make a global symbol for the binding label to prevent
7227 it from colliding with another. */
7228 binding_label_gsym =
7229 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
7230 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
7231 binding_label_gsym->type = GSYM_COMMON;
7235 /* If comm_name_gsym is NULL, the name common block is use
7236 associated and the name could be colliding. */
7237 if (binding_label_gsym->type != GSYM_COMMON)
7238 gfc_error ("Binding label '%s' for common block '%s' at %L "
7239 "collides with the global entity '%s' at %L",
7240 comm_block_tree->n.common->binding_label,
7241 comm_block_tree->n.common->name,
7242 &(comm_block_tree->n.common->where),
7243 binding_label_gsym->name,
7244 &(binding_label_gsym->where));
7245 else if (comm_name_gsym != NULL
7246 && (strcmp (binding_label_gsym->name,
7247 comm_name_gsym->binding_label) != 0)
7248 && (strcmp (binding_label_gsym->sym_name,
7249 comm_name_gsym->name) != 0))
7250 gfc_error ("Binding label '%s' for common block '%s' at %L "
7251 "collides with global entity '%s' at %L",
7252 binding_label_gsym->name, binding_label_gsym->sym_name,
7253 &(comm_block_tree->n.common->where),
7254 comm_name_gsym->name, &(comm_name_gsym->where));
7262 /* Verify any BIND(C) derived types in the namespace so we can report errors
7263 for them once, rather than for each variable declared of that type. */
7266 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
7268 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
7269 && derived_sym->attr.is_bind_c == 1)
7270 verify_bind_c_derived_type (derived_sym);
7276 /* Verify that any binding labels used in a given namespace do not collide
7277 with the names or binding labels of any global symbols. */
7280 gfc_verify_binding_labels (gfc_symbol *sym)
7284 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
7285 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
7287 gfc_gsymbol *bind_c_sym;
7289 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
7290 if (bind_c_sym != NULL
7291 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
7293 if (sym->attr.if_source == IFSRC_DECL
7294 && (bind_c_sym->type != GSYM_SUBROUTINE
7295 && bind_c_sym->type != GSYM_FUNCTION)
7296 && ((sym->attr.contained == 1
7297 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
7298 || (sym->attr.use_assoc == 1
7299 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
7301 /* Make sure global procedures don't collide with anything. */
7302 gfc_error ("Binding label '%s' at %L collides with the global "
7303 "entity '%s' at %L", sym->binding_label,
7304 &(sym->declared_at), bind_c_sym->name,
7305 &(bind_c_sym->where));
7308 else if (sym->attr.contained == 0
7309 && (sym->attr.if_source == IFSRC_IFBODY
7310 && sym->attr.flavor == FL_PROCEDURE)
7311 && (bind_c_sym->sym_name != NULL
7312 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
7314 /* Make sure procedures in interface bodies don't collide. */
7315 gfc_error ("Binding label '%s' in interface body at %L collides "
7316 "with the global entity '%s' at %L",
7318 &(sym->declared_at), bind_c_sym->name,
7319 &(bind_c_sym->where));
7322 else if (sym->attr.contained == 0
7323 && sym->attr.if_source == IFSRC_UNKNOWN)
7324 if ((sym->attr.use_assoc && bind_c_sym->mod_name
7325 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
7326 || sym->attr.use_assoc == 0)
7328 gfc_error ("Binding label '%s' at %L collides with global "
7329 "entity '%s' at %L", sym->binding_label,
7330 &(sym->declared_at), bind_c_sym->name,
7331 &(bind_c_sym->where));
7336 /* Clear the binding label to prevent checking multiple times. */
7337 sym->binding_label[0] = '\0';
7339 else if (bind_c_sym == NULL)
7341 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
7342 bind_c_sym->where = sym->declared_at;
7343 bind_c_sym->sym_name = sym->name;
7345 if (sym->attr.use_assoc == 1)
7346 bind_c_sym->mod_name = sym->module;
7348 if (sym->ns->proc_name != NULL)
7349 bind_c_sym->mod_name = sym->ns->proc_name->name;
7351 if (sym->attr.contained == 0)
7353 if (sym->attr.subroutine)
7354 bind_c_sym->type = GSYM_SUBROUTINE;
7355 else if (sym->attr.function)
7356 bind_c_sym->type = GSYM_FUNCTION;
7364 /* Resolve an index expression. */
7367 resolve_index_expr (gfc_expr *e)
7369 if (gfc_resolve_expr (e) == FAILURE)
7372 if (gfc_simplify_expr (e, 0) == FAILURE)
7375 if (gfc_specification_expr (e) == FAILURE)
7381 /* Resolve a charlen structure. */
7384 resolve_charlen (gfc_charlen *cl)
7393 specification_expr = 1;
7395 if (resolve_index_expr (cl->length) == FAILURE)
7397 specification_expr = 0;
7401 /* "If the character length parameter value evaluates to a negative
7402 value, the length of character entities declared is zero." */
7403 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
7405 gfc_warning_now ("CHARACTER variable has zero length at %L",
7406 &cl->length->where);
7407 gfc_replace_expr (cl->length, gfc_int_expr (0));
7414 /* Test for non-constant shape arrays. */
7417 is_non_constant_shape_array (gfc_symbol *sym)
7423 not_constant = false;
7424 if (sym->as != NULL)
7426 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
7427 has not been simplified; parameter array references. Do the
7428 simplification now. */
7429 for (i = 0; i < sym->as->rank; i++)
7431 e = sym->as->lower[i];
7432 if (e && (resolve_index_expr (e) == FAILURE
7433 || !gfc_is_constant_expr (e)))
7434 not_constant = true;
7436 e = sym->as->upper[i];
7437 if (e && (resolve_index_expr (e) == FAILURE
7438 || !gfc_is_constant_expr (e)))
7439 not_constant = true;
7442 return not_constant;
7445 /* Given a symbol and an initialization expression, add code to initialize
7446 the symbol to the function entry. */
7448 build_init_assign (gfc_symbol *sym, gfc_expr *init)
7452 gfc_namespace *ns = sym->ns;
7454 /* Search for the function namespace if this is a contained
7455 function without an explicit result. */
7456 if (sym->attr.function && sym == sym->result
7457 && sym->name != sym->ns->proc_name->name)
7460 for (;ns; ns = ns->sibling)
7461 if (strcmp (ns->proc_name->name, sym->name) == 0)
7467 gfc_free_expr (init);
7471 /* Build an l-value expression for the result. */
7472 lval = gfc_lval_expr_from_sym (sym);
7474 /* Add the code at scope entry. */
7475 init_st = gfc_get_code ();
7476 init_st->next = ns->code;
7479 /* Assign the default initializer to the l-value. */
7480 init_st->loc = sym->declared_at;
7481 init_st->op = EXEC_INIT_ASSIGN;
7482 init_st->expr1 = lval;
7483 init_st->expr2 = init;
7486 /* Assign the default initializer to a derived type variable or result. */
7489 apply_default_init (gfc_symbol *sym)
7491 gfc_expr *init = NULL;
7493 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
7496 if (sym->ts.type == BT_DERIVED && sym->ts.derived)
7497 init = gfc_default_initializer (&sym->ts);
7502 build_init_assign (sym, init);
7505 /* Build an initializer for a local integer, real, complex, logical, or
7506 character variable, based on the command line flags finit-local-zero,
7507 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
7508 null if the symbol should not have a default initialization. */
7510 build_default_init_expr (gfc_symbol *sym)
7513 gfc_expr *init_expr;
7516 /* These symbols should never have a default initialization. */
7517 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
7518 || sym->attr.external
7520 || sym->attr.pointer
7521 || sym->attr.in_equivalence
7522 || sym->attr.in_common
7525 || sym->attr.cray_pointee
7526 || sym->attr.cray_pointer)
7529 /* Now we'll try to build an initializer expression. */
7530 init_expr = gfc_get_expr ();
7531 init_expr->expr_type = EXPR_CONSTANT;
7532 init_expr->ts.type = sym->ts.type;
7533 init_expr->ts.kind = sym->ts.kind;
7534 init_expr->where = sym->declared_at;
7536 /* We will only initialize integers, reals, complex, logicals, and
7537 characters, and only if the corresponding command-line flags
7538 were set. Otherwise, we free init_expr and return null. */
7539 switch (sym->ts.type)
7542 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
7543 mpz_init_set_si (init_expr->value.integer,
7544 gfc_option.flag_init_integer_value);
7547 gfc_free_expr (init_expr);
7553 mpfr_init (init_expr->value.real);
7554 switch (gfc_option.flag_init_real)
7556 case GFC_INIT_REAL_SNAN:
7557 init_expr->is_snan = 1;
7559 case GFC_INIT_REAL_NAN:
7560 mpfr_set_nan (init_expr->value.real);
7563 case GFC_INIT_REAL_INF:
7564 mpfr_set_inf (init_expr->value.real, 1);
7567 case GFC_INIT_REAL_NEG_INF:
7568 mpfr_set_inf (init_expr->value.real, -1);
7571 case GFC_INIT_REAL_ZERO:
7572 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
7576 gfc_free_expr (init_expr);
7583 mpfr_init (init_expr->value.complex.r);
7584 mpfr_init (init_expr->value.complex.i);
7585 switch (gfc_option.flag_init_real)
7587 case GFC_INIT_REAL_SNAN:
7588 init_expr->is_snan = 1;
7590 case GFC_INIT_REAL_NAN:
7591 mpfr_set_nan (init_expr->value.complex.r);
7592 mpfr_set_nan (init_expr->value.complex.i);
7595 case GFC_INIT_REAL_INF:
7596 mpfr_set_inf (init_expr->value.complex.r, 1);
7597 mpfr_set_inf (init_expr->value.complex.i, 1);
7600 case GFC_INIT_REAL_NEG_INF:
7601 mpfr_set_inf (init_expr->value.complex.r, -1);
7602 mpfr_set_inf (init_expr->value.complex.i, -1);
7605 case GFC_INIT_REAL_ZERO:
7606 mpfr_set_ui (init_expr->value.complex.r, 0.0, GFC_RND_MODE);
7607 mpfr_set_ui (init_expr->value.complex.i, 0.0, GFC_RND_MODE);
7611 gfc_free_expr (init_expr);
7618 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
7619 init_expr->value.logical = 0;
7620 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
7621 init_expr->value.logical = 1;
7624 gfc_free_expr (init_expr);
7630 /* For characters, the length must be constant in order to
7631 create a default initializer. */
7632 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
7633 && sym->ts.cl->length
7634 && sym->ts.cl->length->expr_type == EXPR_CONSTANT)
7636 char_len = mpz_get_si (sym->ts.cl->length->value.integer);
7637 init_expr->value.character.length = char_len;
7638 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
7639 for (i = 0; i < char_len; i++)
7640 init_expr->value.character.string[i]
7641 = (unsigned char) gfc_option.flag_init_character_value;
7645 gfc_free_expr (init_expr);
7651 gfc_free_expr (init_expr);
7657 /* Add an initialization expression to a local variable. */
7659 apply_default_init_local (gfc_symbol *sym)
7661 gfc_expr *init = NULL;
7663 /* The symbol should be a variable or a function return value. */
7664 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
7665 || (sym->attr.function && sym->result != sym))
7668 /* Try to build the initializer expression. If we can't initialize
7669 this symbol, then init will be NULL. */
7670 init = build_default_init_expr (sym);
7674 /* For saved variables, we don't want to add an initializer at
7675 function entry, so we just add a static initializer. */
7676 if (sym->attr.save || sym->ns->save_all)
7678 /* Don't clobber an existing initializer! */
7679 gcc_assert (sym->value == NULL);
7684 build_init_assign (sym, init);
7687 /* Resolution of common features of flavors variable and procedure. */
7690 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
7692 /* Constraints on deferred shape variable. */
7693 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
7695 if (sym->attr.allocatable)
7697 if (sym->attr.dimension)
7698 gfc_error ("Allocatable array '%s' at %L must have "
7699 "a deferred shape", sym->name, &sym->declared_at);
7701 gfc_error ("Scalar object '%s' at %L may not be ALLOCATABLE",
7702 sym->name, &sym->declared_at);
7706 if (sym->attr.pointer && sym->attr.dimension)
7708 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
7709 sym->name, &sym->declared_at);
7716 if (!mp_flag && !sym->attr.allocatable
7717 && !sym->attr.pointer && !sym->attr.dummy)
7719 gfc_error ("Array '%s' at %L cannot have a deferred shape",
7720 sym->name, &sym->declared_at);
7728 /* Additional checks for symbols with flavor variable and derived
7729 type. To be called from resolve_fl_variable. */
7732 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
7734 gcc_assert (sym->ts.type == BT_DERIVED);
7736 /* Check to see if a derived type is blocked from being host
7737 associated by the presence of another class I symbol in the same
7738 namespace. 14.6.1.3 of the standard and the discussion on
7739 comp.lang.fortran. */
7740 if (sym->ns != sym->ts.derived->ns
7741 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
7744 gfc_find_symbol (sym->ts.derived->name, sym->ns, 0, &s);
7745 if (s && s->attr.flavor != FL_DERIVED)
7747 gfc_error ("The type '%s' cannot be host associated at %L "
7748 "because it is blocked by an incompatible object "
7749 "of the same name declared at %L",
7750 sym->ts.derived->name, &sym->declared_at,
7756 /* 4th constraint in section 11.3: "If an object of a type for which
7757 component-initialization is specified (R429) appears in the
7758 specification-part of a module and does not have the ALLOCATABLE
7759 or POINTER attribute, the object shall have the SAVE attribute."
7761 The check for initializers is performed with
7762 has_default_initializer because gfc_default_initializer generates
7763 a hidden default for allocatable components. */
7764 if (!(sym->value || no_init_flag) && sym->ns->proc_name
7765 && sym->ns->proc_name->attr.flavor == FL_MODULE
7766 && !sym->ns->save_all && !sym->attr.save
7767 && !sym->attr.pointer && !sym->attr.allocatable
7768 && has_default_initializer (sym->ts.derived))
7770 gfc_error("Object '%s' at %L must have the SAVE attribute for "
7771 "default initialization of a component",
7772 sym->name, &sym->declared_at);
7776 /* Assign default initializer. */
7777 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
7778 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
7780 sym->value = gfc_default_initializer (&sym->ts);
7787 /* Resolve symbols with flavor variable. */
7790 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
7792 int no_init_flag, automatic_flag;
7794 const char *auto_save_msg;
7796 auto_save_msg = "Automatic object '%s' at %L cannot have the "
7799 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
7802 /* Set this flag to check that variables are parameters of all entries.
7803 This check is effected by the call to gfc_resolve_expr through
7804 is_non_constant_shape_array. */
7805 specification_expr = 1;
7807 if (sym->ns->proc_name
7808 && (sym->ns->proc_name->attr.flavor == FL_MODULE
7809 || sym->ns->proc_name->attr.is_main_program)
7810 && !sym->attr.use_assoc
7811 && !sym->attr.allocatable
7812 && !sym->attr.pointer
7813 && is_non_constant_shape_array (sym))
7815 /* The shape of a main program or module array needs to be
7817 gfc_error ("The module or main program array '%s' at %L must "
7818 "have constant shape", sym->name, &sym->declared_at);
7819 specification_expr = 0;
7823 if (sym->ts.type == BT_CHARACTER)
7825 /* Make sure that character string variables with assumed length are
7827 e = sym->ts.cl->length;
7828 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
7830 gfc_error ("Entity with assumed character length at %L must be a "
7831 "dummy argument or a PARAMETER", &sym->declared_at);
7835 if (e && sym->attr.save && !gfc_is_constant_expr (e))
7837 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
7841 if (!gfc_is_constant_expr (e)
7842 && !(e->expr_type == EXPR_VARIABLE
7843 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
7844 && sym->ns->proc_name
7845 && (sym->ns->proc_name->attr.flavor == FL_MODULE
7846 || sym->ns->proc_name->attr.is_main_program)
7847 && !sym->attr.use_assoc)
7849 gfc_error ("'%s' at %L must have constant character length "
7850 "in this context", sym->name, &sym->declared_at);
7855 if (sym->value == NULL && sym->attr.referenced)
7856 apply_default_init_local (sym); /* Try to apply a default initialization. */
7858 /* Determine if the symbol may not have an initializer. */
7859 no_init_flag = automatic_flag = 0;
7860 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
7861 || sym->attr.intrinsic || sym->attr.result)
7863 else if (sym->attr.dimension && !sym->attr.pointer
7864 && is_non_constant_shape_array (sym))
7866 no_init_flag = automatic_flag = 1;
7868 /* Also, they must not have the SAVE attribute.
7869 SAVE_IMPLICIT is checked below. */
7870 if (sym->attr.save == SAVE_EXPLICIT)
7872 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
7877 /* Ensure that any initializer is simplified. */
7879 gfc_simplify_expr (sym->value, 1);
7881 /* Reject illegal initializers. */
7882 if (!sym->mark && sym->value)
7884 if (sym->attr.allocatable)
7885 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
7886 sym->name, &sym->declared_at);
7887 else if (sym->attr.external)
7888 gfc_error ("External '%s' at %L cannot have an initializer",
7889 sym->name, &sym->declared_at);
7890 else if (sym->attr.dummy
7891 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
7892 gfc_error ("Dummy '%s' at %L cannot have an initializer",
7893 sym->name, &sym->declared_at);
7894 else if (sym->attr.intrinsic)
7895 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
7896 sym->name, &sym->declared_at);
7897 else if (sym->attr.result)
7898 gfc_error ("Function result '%s' at %L cannot have an initializer",
7899 sym->name, &sym->declared_at);
7900 else if (automatic_flag)
7901 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
7902 sym->name, &sym->declared_at);
7909 if (sym->ts.type == BT_DERIVED)
7910 return resolve_fl_variable_derived (sym, no_init_flag);
7916 /* Resolve a procedure. */
7919 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
7921 gfc_formal_arglist *arg;
7923 if (sym->attr.ambiguous_interfaces && !sym->attr.referenced)
7924 gfc_warning ("Although not referenced, '%s' at %L has ambiguous "
7925 "interfaces", sym->name, &sym->declared_at);
7927 if (sym->attr.function
7928 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
7931 if (sym->ts.type == BT_CHARACTER)
7933 gfc_charlen *cl = sym->ts.cl;
7935 if (cl && cl->length && gfc_is_constant_expr (cl->length)
7936 && resolve_charlen (cl) == FAILURE)
7939 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
7941 if (sym->attr.proc == PROC_ST_FUNCTION)
7943 gfc_error ("Character-valued statement function '%s' at %L must "
7944 "have constant length", sym->name, &sym->declared_at);
7948 if (sym->attr.external && sym->formal == NULL
7949 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
7951 gfc_error ("Automatic character length function '%s' at %L must "
7952 "have an explicit interface", sym->name,
7959 /* Ensure that derived type for are not of a private type. Internal
7960 module procedures are excluded by 2.2.3.3 - i.e., they are not
7961 externally accessible and can access all the objects accessible in
7963 if (!(sym->ns->parent
7964 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
7965 && gfc_check_access(sym->attr.access, sym->ns->default_access))
7967 gfc_interface *iface;
7969 for (arg = sym->formal; arg; arg = arg->next)
7972 && arg->sym->ts.type == BT_DERIVED
7973 && !arg->sym->ts.derived->attr.use_assoc
7974 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7975 arg->sym->ts.derived->ns->default_access)
7976 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
7977 "PRIVATE type and cannot be a dummy argument"
7978 " of '%s', which is PUBLIC at %L",
7979 arg->sym->name, sym->name, &sym->declared_at)
7982 /* Stop this message from recurring. */
7983 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7988 /* PUBLIC interfaces may expose PRIVATE procedures that take types
7989 PRIVATE to the containing module. */
7990 for (iface = sym->generic; iface; iface = iface->next)
7992 for (arg = iface->sym->formal; arg; arg = arg->next)
7995 && arg->sym->ts.type == BT_DERIVED
7996 && !arg->sym->ts.derived->attr.use_assoc
7997 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7998 arg->sym->ts.derived->ns->default_access)
7999 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
8000 "'%s' in PUBLIC interface '%s' at %L "
8001 "takes dummy arguments of '%s' which is "
8002 "PRIVATE", iface->sym->name, sym->name,
8003 &iface->sym->declared_at,
8004 gfc_typename (&arg->sym->ts)) == FAILURE)
8006 /* Stop this message from recurring. */
8007 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
8013 /* PUBLIC interfaces may expose PRIVATE procedures that take types
8014 PRIVATE to the containing module. */
8015 for (iface = sym->generic; iface; iface = iface->next)
8017 for (arg = iface->sym->formal; arg; arg = arg->next)
8020 && arg->sym->ts.type == BT_DERIVED
8021 && !arg->sym->ts.derived->attr.use_assoc
8022 && !gfc_check_access (arg->sym->ts.derived->attr.access,
8023 arg->sym->ts.derived->ns->default_access)
8024 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
8025 "'%s' in PUBLIC interface '%s' at %L "
8026 "takes dummy arguments of '%s' which is "
8027 "PRIVATE", iface->sym->name, sym->name,
8028 &iface->sym->declared_at,
8029 gfc_typename (&arg->sym->ts)) == FAILURE)
8031 /* Stop this message from recurring. */
8032 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
8039 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
8040 && !sym->attr.proc_pointer)
8042 gfc_error ("Function '%s' at %L cannot have an initializer",
8043 sym->name, &sym->declared_at);
8047 /* An external symbol may not have an initializer because it is taken to be
8048 a procedure. Exception: Procedure Pointers. */
8049 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
8051 gfc_error ("External object '%s' at %L may not have an initializer",
8052 sym->name, &sym->declared_at);
8056 /* An elemental function is required to return a scalar 12.7.1 */
8057 if (sym->attr.elemental && sym->attr.function && sym->as)
8059 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
8060 "result", sym->name, &sym->declared_at);
8061 /* Reset so that the error only occurs once. */
8062 sym->attr.elemental = 0;
8066 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
8067 char-len-param shall not be array-valued, pointer-valued, recursive
8068 or pure. ....snip... A character value of * may only be used in the
8069 following ways: (i) Dummy arg of procedure - dummy associates with
8070 actual length; (ii) To declare a named constant; or (iii) External
8071 function - but length must be declared in calling scoping unit. */
8072 if (sym->attr.function
8073 && sym->ts.type == BT_CHARACTER
8074 && sym->ts.cl && sym->ts.cl->length == NULL)
8076 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
8077 || (sym->attr.recursive) || (sym->attr.pure))
8079 if (sym->as && sym->as->rank)
8080 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8081 "array-valued", sym->name, &sym->declared_at);
8083 if (sym->attr.pointer)
8084 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8085 "pointer-valued", sym->name, &sym->declared_at);
8088 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8089 "pure", sym->name, &sym->declared_at);
8091 if (sym->attr.recursive)
8092 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8093 "recursive", sym->name, &sym->declared_at);
8098 /* Appendix B.2 of the standard. Contained functions give an
8099 error anyway. Fixed-form is likely to be F77/legacy. */
8100 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
8101 gfc_notify_std (GFC_STD_F95_OBS, "CHARACTER(*) function "
8102 "'%s' at %L is obsolescent in fortran 95",
8103 sym->name, &sym->declared_at);
8106 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
8108 gfc_formal_arglist *curr_arg;
8109 int has_non_interop_arg = 0;
8111 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
8112 sym->common_block) == FAILURE)
8114 /* Clear these to prevent looking at them again if there was an
8116 sym->attr.is_bind_c = 0;
8117 sym->attr.is_c_interop = 0;
8118 sym->ts.is_c_interop = 0;
8122 /* So far, no errors have been found. */
8123 sym->attr.is_c_interop = 1;
8124 sym->ts.is_c_interop = 1;
8127 curr_arg = sym->formal;
8128 while (curr_arg != NULL)
8130 /* Skip implicitly typed dummy args here. */
8131 if (curr_arg->sym->attr.implicit_type == 0)
8132 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
8133 /* If something is found to fail, record the fact so we
8134 can mark the symbol for the procedure as not being
8135 BIND(C) to try and prevent multiple errors being
8137 has_non_interop_arg = 1;
8139 curr_arg = curr_arg->next;
8142 /* See if any of the arguments were not interoperable and if so, clear
8143 the procedure symbol to prevent duplicate error messages. */
8144 if (has_non_interop_arg != 0)
8146 sym->attr.is_c_interop = 0;
8147 sym->ts.is_c_interop = 0;
8148 sym->attr.is_bind_c = 0;
8152 if (!sym->attr.proc_pointer)
8154 if (sym->attr.save == SAVE_EXPLICIT)
8156 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
8157 "in '%s' at %L", sym->name, &sym->declared_at);
8160 if (sym->attr.intent)
8162 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
8163 "in '%s' at %L", sym->name, &sym->declared_at);
8166 if (sym->attr.subroutine && sym->attr.result)
8168 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
8169 "in '%s' at %L", sym->name, &sym->declared_at);
8172 if (sym->attr.external && sym->attr.function
8173 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
8174 || sym->attr.contained))
8176 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
8177 "in '%s' at %L", sym->name, &sym->declared_at);
8180 if (strcmp ("ppr@", sym->name) == 0)
8182 gfc_error ("Procedure pointer result '%s' at %L "
8183 "is missing the pointer attribute",
8184 sym->ns->proc_name->name, &sym->declared_at);
8193 /* Resolve a list of finalizer procedures. That is, after they have hopefully
8194 been defined and we now know their defined arguments, check that they fulfill
8195 the requirements of the standard for procedures used as finalizers. */
8198 gfc_resolve_finalizers (gfc_symbol* derived)
8200 gfc_finalizer* list;
8201 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
8202 gfc_try result = SUCCESS;
8203 bool seen_scalar = false;
8205 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
8208 /* Walk over the list of finalizer-procedures, check them, and if any one
8209 does not fit in with the standard's definition, print an error and remove
8210 it from the list. */
8211 prev_link = &derived->f2k_derived->finalizers;
8212 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
8218 /* Skip this finalizer if we already resolved it. */
8219 if (list->proc_tree)
8221 prev_link = &(list->next);
8225 /* Check this exists and is a SUBROUTINE. */
8226 if (!list->proc_sym->attr.subroutine)
8228 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
8229 list->proc_sym->name, &list->where);
8233 /* We should have exactly one argument. */
8234 if (!list->proc_sym->formal || list->proc_sym->formal->next)
8236 gfc_error ("FINAL procedure at %L must have exactly one argument",
8240 arg = list->proc_sym->formal->sym;
8242 /* This argument must be of our type. */
8243 if (arg->ts.type != BT_DERIVED || arg->ts.derived != derived)
8245 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
8246 &arg->declared_at, derived->name);
8250 /* It must neither be a pointer nor allocatable nor optional. */
8251 if (arg->attr.pointer)
8253 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
8257 if (arg->attr.allocatable)
8259 gfc_error ("Argument of FINAL procedure at %L must not be"
8260 " ALLOCATABLE", &arg->declared_at);
8263 if (arg->attr.optional)
8265 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
8270 /* It must not be INTENT(OUT). */
8271 if (arg->attr.intent == INTENT_OUT)
8273 gfc_error ("Argument of FINAL procedure at %L must not be"
8274 " INTENT(OUT)", &arg->declared_at);
8278 /* Warn if the procedure is non-scalar and not assumed shape. */
8279 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
8280 && arg->as->type != AS_ASSUMED_SHAPE)
8281 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
8282 " shape argument", &arg->declared_at);
8284 /* Check that it does not match in kind and rank with a FINAL procedure
8285 defined earlier. To really loop over the *earlier* declarations,
8286 we need to walk the tail of the list as new ones were pushed at the
8288 /* TODO: Handle kind parameters once they are implemented. */
8289 my_rank = (arg->as ? arg->as->rank : 0);
8290 for (i = list->next; i; i = i->next)
8292 /* Argument list might be empty; that is an error signalled earlier,
8293 but we nevertheless continued resolving. */
8294 if (i->proc_sym->formal)
8296 gfc_symbol* i_arg = i->proc_sym->formal->sym;
8297 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
8298 if (i_rank == my_rank)
8300 gfc_error ("FINAL procedure '%s' declared at %L has the same"
8301 " rank (%d) as '%s'",
8302 list->proc_sym->name, &list->where, my_rank,
8309 /* Is this the/a scalar finalizer procedure? */
8310 if (!arg->as || arg->as->rank == 0)
8313 /* Find the symtree for this procedure. */
8314 gcc_assert (!list->proc_tree);
8315 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
8317 prev_link = &list->next;
8320 /* Remove wrong nodes immediately from the list so we don't risk any
8321 troubles in the future when they might fail later expectations. */
8325 *prev_link = list->next;
8326 gfc_free_finalizer (i);
8329 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
8330 were nodes in the list, must have been for arrays. It is surely a good
8331 idea to have a scalar version there if there's something to finalize. */
8332 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
8333 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
8334 " defined at %L, suggest also scalar one",
8335 derived->name, &derived->declared_at);
8337 /* TODO: Remove this error when finalization is finished. */
8338 gfc_error ("Finalization at %L is not yet implemented",
8339 &derived->declared_at);
8345 /* Check that it is ok for the typebound procedure proc to override the
8349 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
8352 const gfc_symbol* proc_target;
8353 const gfc_symbol* old_target;
8354 unsigned proc_pass_arg, old_pass_arg, argpos;
8355 gfc_formal_arglist* proc_formal;
8356 gfc_formal_arglist* old_formal;
8358 /* This procedure should only be called for non-GENERIC proc. */
8359 gcc_assert (!proc->n.tb->is_generic);
8361 /* If the overwritten procedure is GENERIC, this is an error. */
8362 if (old->n.tb->is_generic)
8364 gfc_error ("Can't overwrite GENERIC '%s' at %L",
8365 old->name, &proc->n.tb->where);
8369 where = proc->n.tb->where;
8370 proc_target = proc->n.tb->u.specific->n.sym;
8371 old_target = old->n.tb->u.specific->n.sym;
8373 /* Check that overridden binding is not NON_OVERRIDABLE. */
8374 if (old->n.tb->non_overridable)
8376 gfc_error ("'%s' at %L overrides a procedure binding declared"
8377 " NON_OVERRIDABLE", proc->name, &where);
8381 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
8382 if (!old->n.tb->deferred && proc->n.tb->deferred)
8384 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
8385 " non-DEFERRED binding", proc->name, &where);
8389 /* If the overridden binding is PURE, the overriding must be, too. */
8390 if (old_target->attr.pure && !proc_target->attr.pure)
8392 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
8393 proc->name, &where);
8397 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
8398 is not, the overriding must not be either. */
8399 if (old_target->attr.elemental && !proc_target->attr.elemental)
8401 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
8402 " ELEMENTAL", proc->name, &where);
8405 if (!old_target->attr.elemental && proc_target->attr.elemental)
8407 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
8408 " be ELEMENTAL, either", proc->name, &where);
8412 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
8414 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
8416 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
8417 " SUBROUTINE", proc->name, &where);
8421 /* If the overridden binding is a FUNCTION, the overriding must also be a
8422 FUNCTION and have the same characteristics. */
8423 if (old_target->attr.function)
8425 if (!proc_target->attr.function)
8427 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
8428 " FUNCTION", proc->name, &where);
8432 /* FIXME: Do more comprehensive checking (including, for instance, the
8433 rank and array-shape). */
8434 gcc_assert (proc_target->result && old_target->result);
8435 if (!gfc_compare_types (&proc_target->result->ts,
8436 &old_target->result->ts))
8438 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
8439 " matching result types", proc->name, &where);
8444 /* If the overridden binding is PUBLIC, the overriding one must not be
8446 if (old->n.tb->access == ACCESS_PUBLIC
8447 && proc->n.tb->access == ACCESS_PRIVATE)
8449 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
8450 " PRIVATE", proc->name, &where);
8454 /* Compare the formal argument lists of both procedures. This is also abused
8455 to find the position of the passed-object dummy arguments of both
8456 bindings as at least the overridden one might not yet be resolved and we
8457 need those positions in the check below. */
8458 proc_pass_arg = old_pass_arg = 0;
8459 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
8461 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
8464 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
8465 proc_formal && old_formal;
8466 proc_formal = proc_formal->next, old_formal = old_formal->next)
8468 if (proc->n.tb->pass_arg
8469 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
8470 proc_pass_arg = argpos;
8471 if (old->n.tb->pass_arg
8472 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
8473 old_pass_arg = argpos;
8475 /* Check that the names correspond. */
8476 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
8478 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
8479 " to match the corresponding argument of the overridden"
8480 " procedure", proc_formal->sym->name, proc->name, &where,
8481 old_formal->sym->name);
8485 /* Check that the types correspond if neither is the passed-object
8487 /* FIXME: Do more comprehensive testing here. */
8488 if (proc_pass_arg != argpos && old_pass_arg != argpos
8489 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
8491 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L in"
8492 " in respect to the overridden procedure",
8493 proc_formal->sym->name, proc->name, &where);
8499 if (proc_formal || old_formal)
8501 gfc_error ("'%s' at %L must have the same number of formal arguments as"
8502 " the overridden procedure", proc->name, &where);
8506 /* If the overridden binding is NOPASS, the overriding one must also be
8508 if (old->n.tb->nopass && !proc->n.tb->nopass)
8510 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
8511 " NOPASS", proc->name, &where);
8515 /* If the overridden binding is PASS(x), the overriding one must also be
8516 PASS and the passed-object dummy arguments must correspond. */
8517 if (!old->n.tb->nopass)
8519 if (proc->n.tb->nopass)
8521 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
8522 " PASS", proc->name, &where);
8526 if (proc_pass_arg != old_pass_arg)
8528 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
8529 " the same position as the passed-object dummy argument of"
8530 " the overridden procedure", proc->name, &where);
8539 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
8542 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
8543 const char* generic_name, locus where)
8548 gcc_assert (t1->specific && t2->specific);
8549 gcc_assert (!t1->specific->is_generic);
8550 gcc_assert (!t2->specific->is_generic);
8552 sym1 = t1->specific->u.specific->n.sym;
8553 sym2 = t2->specific->u.specific->n.sym;
8555 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
8556 if (sym1->attr.subroutine != sym2->attr.subroutine
8557 || sym1->attr.function != sym2->attr.function)
8559 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
8560 " GENERIC '%s' at %L",
8561 sym1->name, sym2->name, generic_name, &where);
8565 /* Compare the interfaces. */
8566 if (gfc_compare_interfaces (sym1, sym2, 1))
8568 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
8569 sym1->name, sym2->name, generic_name, &where);
8577 /* Resolve a GENERIC procedure binding for a derived type. */
8580 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
8582 gfc_tbp_generic* target;
8583 gfc_symtree* first_target;
8584 gfc_symbol* super_type;
8585 gfc_symtree* inherited;
8588 gcc_assert (st->n.tb);
8589 gcc_assert (st->n.tb->is_generic);
8591 where = st->n.tb->where;
8592 super_type = gfc_get_derived_super_type (derived);
8594 /* Find the overridden binding if any. */
8595 st->n.tb->overridden = NULL;
8598 gfc_symtree* overridden;
8599 overridden = gfc_find_typebound_proc (super_type, NULL, st->name, true);
8601 if (overridden && overridden->n.tb)
8602 st->n.tb->overridden = overridden->n.tb;
8605 /* Try to find the specific bindings for the symtrees in our target-list. */
8606 gcc_assert (st->n.tb->u.generic);
8607 for (target = st->n.tb->u.generic; target; target = target->next)
8608 if (!target->specific)
8610 gfc_typebound_proc* overridden_tbp;
8612 const char* target_name;
8614 target_name = target->specific_st->name;
8616 /* Defined for this type directly. */
8617 if (target->specific_st->n.tb)
8619 target->specific = target->specific_st->n.tb;
8620 goto specific_found;
8623 /* Look for an inherited specific binding. */
8626 inherited = gfc_find_typebound_proc (super_type, NULL,
8631 gcc_assert (inherited->n.tb);
8632 target->specific = inherited->n.tb;
8633 goto specific_found;
8637 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
8638 " at %L", target_name, st->name, &where);
8641 /* Once we've found the specific binding, check it is not ambiguous with
8642 other specifics already found or inherited for the same GENERIC. */
8644 gcc_assert (target->specific);
8646 /* This must really be a specific binding! */
8647 if (target->specific->is_generic)
8649 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
8650 " '%s' is GENERIC, too", st->name, &where, target_name);
8654 /* Check those already resolved on this type directly. */
8655 for (g = st->n.tb->u.generic; g; g = g->next)
8656 if (g != target && g->specific
8657 && check_generic_tbp_ambiguity (target, g, st->name, where)
8661 /* Check for ambiguity with inherited specific targets. */
8662 for (overridden_tbp = st->n.tb->overridden; overridden_tbp;
8663 overridden_tbp = overridden_tbp->overridden)
8664 if (overridden_tbp->is_generic)
8666 for (g = overridden_tbp->u.generic; g; g = g->next)
8668 gcc_assert (g->specific);
8669 if (check_generic_tbp_ambiguity (target, g,
8670 st->name, where) == FAILURE)
8676 /* If we attempt to "overwrite" a specific binding, this is an error. */
8677 if (st->n.tb->overridden && !st->n.tb->overridden->is_generic)
8679 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
8680 " the same name", st->name, &where);
8684 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
8685 all must have the same attributes here. */
8686 first_target = st->n.tb->u.generic->specific->u.specific;
8687 gcc_assert (first_target);
8688 st->n.tb->subroutine = first_target->n.sym->attr.subroutine;
8689 st->n.tb->function = first_target->n.sym->attr.function;
8695 /* Resolve the type-bound procedures for a derived type. */
8697 static gfc_symbol* resolve_bindings_derived;
8698 static gfc_try resolve_bindings_result;
8701 resolve_typebound_procedure (gfc_symtree* stree)
8706 gfc_symbol* super_type;
8707 gfc_component* comp;
8711 /* Undefined specific symbol from GENERIC target definition. */
8715 if (stree->n.tb->error)
8718 /* If this is a GENERIC binding, use that routine. */
8719 if (stree->n.tb->is_generic)
8721 if (resolve_typebound_generic (resolve_bindings_derived, stree)
8727 /* Get the target-procedure to check it. */
8728 gcc_assert (!stree->n.tb->is_generic);
8729 gcc_assert (stree->n.tb->u.specific);
8730 proc = stree->n.tb->u.specific->n.sym;
8731 where = stree->n.tb->where;
8733 /* Default access should already be resolved from the parser. */
8734 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
8736 /* It should be a module procedure or an external procedure with explicit
8737 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
8738 if ((!proc->attr.subroutine && !proc->attr.function)
8739 || (proc->attr.proc != PROC_MODULE
8740 && proc->attr.if_source != IFSRC_IFBODY)
8741 || (proc->attr.abstract && !stree->n.tb->deferred))
8743 gfc_error ("'%s' must be a module procedure or an external procedure with"
8744 " an explicit interface at %L", proc->name, &where);
8747 stree->n.tb->subroutine = proc->attr.subroutine;
8748 stree->n.tb->function = proc->attr.function;
8750 /* Find the super-type of the current derived type. We could do this once and
8751 store in a global if speed is needed, but as long as not I believe this is
8752 more readable and clearer. */
8753 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
8755 /* If PASS, resolve and check arguments if not already resolved / loaded
8756 from a .mod file. */
8757 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
8759 if (stree->n.tb->pass_arg)
8761 gfc_formal_arglist* i;
8763 /* If an explicit passing argument name is given, walk the arg-list
8767 stree->n.tb->pass_arg_num = 1;
8768 for (i = proc->formal; i; i = i->next)
8770 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
8775 ++stree->n.tb->pass_arg_num;
8780 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
8782 proc->name, stree->n.tb->pass_arg, &where,
8783 stree->n.tb->pass_arg);
8789 /* Otherwise, take the first one; there should in fact be at least
8791 stree->n.tb->pass_arg_num = 1;
8794 gfc_error ("Procedure '%s' with PASS at %L must have at"
8795 " least one argument", proc->name, &where);
8798 me_arg = proc->formal->sym;
8801 /* Now check that the argument-type matches. */
8802 gcc_assert (me_arg);
8803 if (me_arg->ts.type != BT_DERIVED
8804 || me_arg->ts.derived != resolve_bindings_derived)
8806 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
8807 " the derived-type '%s'", me_arg->name, proc->name,
8808 me_arg->name, &where, resolve_bindings_derived->name);
8812 gfc_warning ("Polymorphic entities are not yet implemented,"
8813 " non-polymorphic passed-object dummy argument of '%s'"
8814 " at %L accepted", proc->name, &where);
8817 /* If we are extending some type, check that we don't override a procedure
8818 flagged NON_OVERRIDABLE. */
8819 stree->n.tb->overridden = NULL;
8822 gfc_symtree* overridden;
8823 overridden = gfc_find_typebound_proc (super_type, NULL,
8826 if (overridden && overridden->n.tb)
8827 stree->n.tb->overridden = overridden->n.tb;
8829 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
8833 /* See if there's a name collision with a component directly in this type. */
8834 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
8835 if (!strcmp (comp->name, stree->name))
8837 gfc_error ("Procedure '%s' at %L has the same name as a component of"
8839 stree->name, &where, resolve_bindings_derived->name);
8843 /* Try to find a name collision with an inherited component. */
8844 if (super_type && gfc_find_component (super_type, stree->name, true, true))
8846 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
8847 " component of '%s'",
8848 stree->name, &where, resolve_bindings_derived->name);
8852 stree->n.tb->error = 0;
8856 resolve_bindings_result = FAILURE;
8857 stree->n.tb->error = 1;
8861 resolve_typebound_procedures (gfc_symbol* derived)
8863 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
8866 resolve_bindings_derived = derived;
8867 resolve_bindings_result = SUCCESS;
8868 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
8869 &resolve_typebound_procedure);
8871 return resolve_bindings_result;
8875 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
8876 to give all identical derived types the same backend_decl. */
8878 add_dt_to_dt_list (gfc_symbol *derived)
8880 gfc_dt_list *dt_list;
8882 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
8883 if (derived == dt_list->derived)
8886 if (dt_list == NULL)
8888 dt_list = gfc_get_dt_list ();
8889 dt_list->next = gfc_derived_types;
8890 dt_list->derived = derived;
8891 gfc_derived_types = dt_list;
8896 /* Ensure that a derived-type is really not abstract, meaning that every
8897 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
8900 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
8905 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
8907 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
8910 if (st->n.tb && st->n.tb->deferred)
8912 gfc_symtree* overriding;
8913 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true);
8914 gcc_assert (overriding && overriding->n.tb);
8915 if (overriding->n.tb->deferred)
8917 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
8918 " '%s' is DEFERRED and not overridden",
8919 sub->name, &sub->declared_at, st->name);
8928 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
8930 /* The algorithm used here is to recursively travel up the ancestry of sub
8931 and for each ancestor-type, check all bindings. If any of them is
8932 DEFERRED, look it up starting from sub and see if the found (overriding)
8933 binding is not DEFERRED.
8934 This is not the most efficient way to do this, but it should be ok and is
8935 clearer than something sophisticated. */
8937 gcc_assert (ancestor && ancestor->attr.abstract && !sub->attr.abstract);
8939 /* Walk bindings of this ancestor. */
8940 if (ancestor->f2k_derived)
8943 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
8948 /* Find next ancestor type and recurse on it. */
8949 ancestor = gfc_get_derived_super_type (ancestor);
8951 return ensure_not_abstract (sub, ancestor);
8957 /* Resolve the components of a derived type. */
8960 resolve_fl_derived (gfc_symbol *sym)
8962 gfc_symbol* super_type;
8966 super_type = gfc_get_derived_super_type (sym);
8968 /* Ensure the extended type gets resolved before we do. */
8969 if (super_type && resolve_fl_derived (super_type) == FAILURE)
8972 /* An ABSTRACT type must be extensible. */
8973 if (sym->attr.abstract && (sym->attr.is_bind_c || sym->attr.sequence))
8975 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
8976 sym->name, &sym->declared_at);
8980 for (c = sym->components; c != NULL; c = c->next)
8982 if (c->attr.proc_pointer && c->ts.interface)
8984 if (c->ts.interface->attr.procedure)
8985 gfc_error ("Interface '%s', used by procedure pointer component "
8986 "'%s' at %L, is declared in a later PROCEDURE statement",
8987 c->ts.interface->name, c->name, &c->loc);
8989 /* Get the attributes from the interface (now resolved). */
8990 if (c->ts.interface->attr.if_source
8991 || c->ts.interface->attr.intrinsic)
8993 gfc_symbol *ifc = c->ts.interface;
8995 if (ifc->attr.intrinsic)
8996 resolve_intrinsic (ifc, &ifc->declared_at);
8999 c->ts = ifc->result->ts;
9002 c->ts.interface = ifc;
9003 c->attr.function = ifc->attr.function;
9004 c->attr.subroutine = ifc->attr.subroutine;
9005 /* TODO: gfc_copy_formal_args (c, ifc); */
9007 c->attr.allocatable = ifc->attr.allocatable;
9008 c->attr.pointer = ifc->attr.pointer;
9009 c->attr.pure = ifc->attr.pure;
9010 c->attr.elemental = ifc->attr.elemental;
9011 c->attr.dimension = ifc->attr.dimension;
9012 c->attr.recursive = ifc->attr.recursive;
9013 c->attr.always_explicit = ifc->attr.always_explicit;
9014 /* Copy array spec. */
9015 c->as = gfc_copy_array_spec (ifc->as);
9019 for (i = 0; i < c->as->rank; i++)
9021 gfc_expr_replace_symbols (c->as->lower[i], c);
9022 gfc_expr_replace_symbols (c->as->upper[i], c);
9025 /* Copy char length. */
9028 c->ts.cl = gfc_get_charlen();
9029 c->ts.cl->resolved = ifc->ts.cl->resolved;
9030 c->ts.cl->length = gfc_copy_expr (ifc->ts.cl->length);
9031 /*gfc_expr_replace_symbols (c->ts.cl->length, c);*/
9032 /* Add charlen to namespace. */
9035 c->ts.cl->next = c->formal_ns->cl_list;
9036 c->formal_ns->cl_list = c->ts.cl;
9040 else if (c->ts.interface->name[0] != '\0')
9042 gfc_error ("Interface '%s' of procedure pointer component "
9043 "'%s' at %L must be explicit", c->ts.interface->name,
9048 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
9050 c->ts = *gfc_get_default_type (c->name, NULL);
9051 c->attr.implicit_type = 1;
9054 /* Check type-spec if this is not the parent-type component. */
9055 if ((!sym->attr.extension || c != sym->components)
9056 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
9059 /* If this type is an extension, see if this component has the same name
9060 as an inherited type-bound procedure. */
9062 && gfc_find_typebound_proc (super_type, NULL, c->name, true))
9064 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
9065 " inherited type-bound procedure",
9066 c->name, sym->name, &c->loc);
9070 if (c->ts.type == BT_CHARACTER)
9072 if (c->ts.cl->length == NULL
9073 || (resolve_charlen (c->ts.cl) == FAILURE)
9074 || !gfc_is_constant_expr (c->ts.cl->length))
9076 gfc_error ("Character length of component '%s' needs to "
9077 "be a constant specification expression at %L",
9079 c->ts.cl->length ? &c->ts.cl->length->where : &c->loc);
9084 if (c->ts.type == BT_DERIVED
9085 && sym->component_access != ACCESS_PRIVATE
9086 && gfc_check_access (sym->attr.access, sym->ns->default_access)
9087 && !is_sym_host_assoc (c->ts.derived, sym->ns)
9088 && !c->ts.derived->attr.use_assoc
9089 && !gfc_check_access (c->ts.derived->attr.access,
9090 c->ts.derived->ns->default_access)
9091 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
9092 "is a PRIVATE type and cannot be a component of "
9093 "'%s', which is PUBLIC at %L", c->name,
9094 sym->name, &sym->declared_at) == FAILURE)
9097 if (sym->attr.sequence)
9099 if (c->ts.type == BT_DERIVED && c->ts.derived->attr.sequence == 0)
9101 gfc_error ("Component %s of SEQUENCE type declared at %L does "
9102 "not have the SEQUENCE attribute",
9103 c->ts.derived->name, &sym->declared_at);
9108 if (c->ts.type == BT_DERIVED && c->attr.pointer
9109 && c->ts.derived->components == NULL
9110 && !c->ts.derived->attr.zero_comp)
9112 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
9113 "that has not been declared", c->name, sym->name,
9118 /* Ensure that all the derived type components are put on the
9119 derived type list; even in formal namespaces, where derived type
9120 pointer components might not have been declared. */
9121 if (c->ts.type == BT_DERIVED
9123 && c->ts.derived->components
9125 && sym != c->ts.derived)
9126 add_dt_to_dt_list (c->ts.derived);
9128 if (c->attr.pointer || c->attr.allocatable || c->as == NULL)
9131 for (i = 0; i < c->as->rank; i++)
9133 if (c->as->lower[i] == NULL
9134 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
9135 || !gfc_is_constant_expr (c->as->lower[i])
9136 || c->as->upper[i] == NULL
9137 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
9138 || !gfc_is_constant_expr (c->as->upper[i]))
9140 gfc_error ("Component '%s' of '%s' at %L must have "
9141 "constant array bounds",
9142 c->name, sym->name, &c->loc);
9148 /* Resolve the type-bound procedures. */
9149 if (resolve_typebound_procedures (sym) == FAILURE)
9152 /* Resolve the finalizer procedures. */
9153 if (gfc_resolve_finalizers (sym) == FAILURE)
9156 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
9157 all DEFERRED bindings are overridden. */
9158 if (super_type && super_type->attr.abstract && !sym->attr.abstract
9159 && ensure_not_abstract (sym, super_type) == FAILURE)
9162 /* Add derived type to the derived type list. */
9163 add_dt_to_dt_list (sym);
9170 resolve_fl_namelist (gfc_symbol *sym)
9175 /* Reject PRIVATE objects in a PUBLIC namelist. */
9176 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
9178 for (nl = sym->namelist; nl; nl = nl->next)
9180 if (!nl->sym->attr.use_assoc
9181 && !is_sym_host_assoc (nl->sym, sym->ns)
9182 && !gfc_check_access(nl->sym->attr.access,
9183 nl->sym->ns->default_access))
9185 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
9186 "cannot be member of PUBLIC namelist '%s' at %L",
9187 nl->sym->name, sym->name, &sym->declared_at);
9191 /* Types with private components that came here by USE-association. */
9192 if (nl->sym->ts.type == BT_DERIVED
9193 && derived_inaccessible (nl->sym->ts.derived))
9195 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
9196 "components and cannot be member of namelist '%s' at %L",
9197 nl->sym->name, sym->name, &sym->declared_at);
9201 /* Types with private components that are defined in the same module. */
9202 if (nl->sym->ts.type == BT_DERIVED
9203 && !is_sym_host_assoc (nl->sym->ts.derived, sym->ns)
9204 && !gfc_check_access (nl->sym->ts.derived->attr.private_comp
9205 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
9206 nl->sym->ns->default_access))
9208 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
9209 "cannot be a member of PUBLIC namelist '%s' at %L",
9210 nl->sym->name, sym->name, &sym->declared_at);
9216 for (nl = sym->namelist; nl; nl = nl->next)
9218 /* Reject namelist arrays of assumed shape. */
9219 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
9220 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
9221 "must not have assumed shape in namelist "
9222 "'%s' at %L", nl->sym->name, sym->name,
9223 &sym->declared_at) == FAILURE)
9226 /* Reject namelist arrays that are not constant shape. */
9227 if (is_non_constant_shape_array (nl->sym))
9229 gfc_error ("NAMELIST array object '%s' must have constant "
9230 "shape in namelist '%s' at %L", nl->sym->name,
9231 sym->name, &sym->declared_at);
9235 /* Namelist objects cannot have allocatable or pointer components. */
9236 if (nl->sym->ts.type != BT_DERIVED)
9239 if (nl->sym->ts.derived->attr.alloc_comp)
9241 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
9242 "have ALLOCATABLE components",
9243 nl->sym->name, sym->name, &sym->declared_at);
9247 if (nl->sym->ts.derived->attr.pointer_comp)
9249 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
9250 "have POINTER components",
9251 nl->sym->name, sym->name, &sym->declared_at);
9257 /* 14.1.2 A module or internal procedure represent local entities
9258 of the same type as a namelist member and so are not allowed. */
9259 for (nl = sym->namelist; nl; nl = nl->next)
9261 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
9264 if (nl->sym->attr.function && nl->sym == nl->sym->result)
9265 if ((nl->sym == sym->ns->proc_name)
9267 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
9271 if (nl->sym && nl->sym->name)
9272 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
9273 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
9275 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
9276 "attribute in '%s' at %L", nlsym->name,
9287 resolve_fl_parameter (gfc_symbol *sym)
9289 /* A parameter array's shape needs to be constant. */
9291 && (sym->as->type == AS_DEFERRED
9292 || is_non_constant_shape_array (sym)))
9294 gfc_error ("Parameter array '%s' at %L cannot be automatic "
9295 "or of deferred shape", sym->name, &sym->declared_at);
9299 /* Make sure a parameter that has been implicitly typed still
9300 matches the implicit type, since PARAMETER statements can precede
9301 IMPLICIT statements. */
9302 if (sym->attr.implicit_type
9303 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
9306 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
9307 "later IMPLICIT type", sym->name, &sym->declared_at);
9311 /* Make sure the types of derived parameters are consistent. This
9312 type checking is deferred until resolution because the type may
9313 refer to a derived type from the host. */
9314 if (sym->ts.type == BT_DERIVED
9315 && !gfc_compare_types (&sym->ts, &sym->value->ts))
9317 gfc_error ("Incompatible derived type in PARAMETER at %L",
9318 &sym->value->where);
9325 /* Do anything necessary to resolve a symbol. Right now, we just
9326 assume that an otherwise unknown symbol is a variable. This sort
9327 of thing commonly happens for symbols in module. */
9330 resolve_symbol (gfc_symbol *sym)
9332 int check_constant, mp_flag;
9333 gfc_symtree *symtree;
9334 gfc_symtree *this_symtree;
9338 if (sym->attr.flavor == FL_UNKNOWN)
9341 /* If we find that a flavorless symbol is an interface in one of the
9342 parent namespaces, find its symtree in this namespace, free the
9343 symbol and set the symtree to point to the interface symbol. */
9344 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
9346 symtree = gfc_find_symtree (ns->sym_root, sym->name);
9347 if (symtree && symtree->n.sym->generic)
9349 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
9353 gfc_free_symbol (sym);
9354 symtree->n.sym->refs++;
9355 this_symtree->n.sym = symtree->n.sym;
9360 /* Otherwise give it a flavor according to such attributes as
9362 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
9363 sym->attr.flavor = FL_VARIABLE;
9366 sym->attr.flavor = FL_PROCEDURE;
9367 if (sym->attr.dimension)
9368 sym->attr.function = 1;
9372 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
9373 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
9375 if (sym->attr.procedure && sym->ts.interface
9376 && sym->attr.if_source != IFSRC_DECL)
9378 if (sym->ts.interface->attr.procedure)
9379 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared "
9380 "in a later PROCEDURE statement", sym->ts.interface->name,
9381 sym->name,&sym->declared_at);
9383 /* Get the attributes from the interface (now resolved). */
9384 if (sym->ts.interface->attr.if_source
9385 || sym->ts.interface->attr.intrinsic)
9387 gfc_symbol *ifc = sym->ts.interface;
9389 if (ifc->attr.intrinsic)
9390 resolve_intrinsic (ifc, &ifc->declared_at);
9393 sym->ts = ifc->result->ts;
9396 sym->ts.interface = ifc;
9397 sym->attr.function = ifc->attr.function;
9398 sym->attr.subroutine = ifc->attr.subroutine;
9399 gfc_copy_formal_args (sym, ifc);
9401 sym->attr.allocatable = ifc->attr.allocatable;
9402 sym->attr.pointer = ifc->attr.pointer;
9403 sym->attr.pure = ifc->attr.pure;
9404 sym->attr.elemental = ifc->attr.elemental;
9405 sym->attr.dimension = ifc->attr.dimension;
9406 sym->attr.recursive = ifc->attr.recursive;
9407 sym->attr.always_explicit = ifc->attr.always_explicit;
9408 /* Copy array spec. */
9409 sym->as = gfc_copy_array_spec (ifc->as);
9413 for (i = 0; i < sym->as->rank; i++)
9415 gfc_expr_replace_symbols (sym->as->lower[i], sym);
9416 gfc_expr_replace_symbols (sym->as->upper[i], sym);
9419 /* Copy char length. */
9422 sym->ts.cl = gfc_get_charlen();
9423 sym->ts.cl->resolved = ifc->ts.cl->resolved;
9424 sym->ts.cl->length = gfc_copy_expr (ifc->ts.cl->length);
9425 gfc_expr_replace_symbols (sym->ts.cl->length, sym);
9426 /* Add charlen to namespace. */
9429 sym->ts.cl->next = sym->formal_ns->cl_list;
9430 sym->formal_ns->cl_list = sym->ts.cl;
9434 else if (sym->ts.interface->name[0] != '\0')
9436 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
9437 sym->ts.interface->name, sym->name, &sym->declared_at);
9442 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
9445 /* Symbols that are module procedures with results (functions) have
9446 the types and array specification copied for type checking in
9447 procedures that call them, as well as for saving to a module
9448 file. These symbols can't stand the scrutiny that their results
9450 mp_flag = (sym->result != NULL && sym->result != sym);
9453 /* Make sure that the intrinsic is consistent with its internal
9454 representation. This needs to be done before assigning a default
9455 type to avoid spurious warnings. */
9456 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic)
9458 gfc_intrinsic_sym* isym;
9461 /* We already know this one is an intrinsic, so we don't call
9462 gfc_is_intrinsic for full checking but rather use gfc_find_function and
9463 gfc_find_subroutine directly to check whether it is a function or
9466 if ((isym = gfc_find_function (sym->name)))
9468 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
9469 && !sym->attr.implicit_type)
9470 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
9471 " ignored", sym->name, &sym->declared_at);
9473 else if ((isym = gfc_find_subroutine (sym->name)))
9475 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
9477 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
9478 " specifier", sym->name, &sym->declared_at);
9484 gfc_error ("'%s' declared INTRINSIC at %L does not exist",
9485 sym->name, &sym->declared_at);
9489 /* Check it is actually available in the standard settings. */
9490 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
9493 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
9494 " available in the current standard settings but %s. Use"
9495 " an appropriate -std=* option or enable -fall-intrinsics"
9496 " in order to use it.",
9497 sym->name, &sym->declared_at, symstd);
9502 /* Assign default type to symbols that need one and don't have one. */
9503 if (sym->ts.type == BT_UNKNOWN)
9505 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
9506 gfc_set_default_type (sym, 1, NULL);
9508 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
9510 /* The specific case of an external procedure should emit an error
9511 in the case that there is no implicit type. */
9513 gfc_set_default_type (sym, sym->attr.external, NULL);
9516 /* Result may be in another namespace. */
9517 resolve_symbol (sym->result);
9519 if (!sym->result->attr.proc_pointer)
9521 sym->ts = sym->result->ts;
9522 sym->as = gfc_copy_array_spec (sym->result->as);
9523 sym->attr.dimension = sym->result->attr.dimension;
9524 sym->attr.pointer = sym->result->attr.pointer;
9525 sym->attr.allocatable = sym->result->attr.allocatable;
9531 /* Assumed size arrays and assumed shape arrays must be dummy
9535 && (sym->as->type == AS_ASSUMED_SIZE
9536 || sym->as->type == AS_ASSUMED_SHAPE)
9537 && sym->attr.dummy == 0)
9539 if (sym->as->type == AS_ASSUMED_SIZE)
9540 gfc_error ("Assumed size array at %L must be a dummy argument",
9543 gfc_error ("Assumed shape array at %L must be a dummy argument",
9548 /* Make sure symbols with known intent or optional are really dummy
9549 variable. Because of ENTRY statement, this has to be deferred
9550 until resolution time. */
9552 if (!sym->attr.dummy
9553 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
9555 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
9559 if (sym->attr.value && !sym->attr.dummy)
9561 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
9562 "it is not a dummy argument", sym->name, &sym->declared_at);
9566 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
9568 gfc_charlen *cl = sym->ts.cl;
9569 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
9571 gfc_error ("Character dummy variable '%s' at %L with VALUE "
9572 "attribute must have constant length",
9573 sym->name, &sym->declared_at);
9577 if (sym->ts.is_c_interop
9578 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
9580 gfc_error ("C interoperable character dummy variable '%s' at %L "
9581 "with VALUE attribute must have length one",
9582 sym->name, &sym->declared_at);
9587 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
9588 do this for something that was implicitly typed because that is handled
9589 in gfc_set_default_type. Handle dummy arguments and procedure
9590 definitions separately. Also, anything that is use associated is not
9591 handled here but instead is handled in the module it is declared in.
9592 Finally, derived type definitions are allowed to be BIND(C) since that
9593 only implies that they're interoperable, and they are checked fully for
9594 interoperability when a variable is declared of that type. */
9595 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
9596 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
9597 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
9599 gfc_try t = SUCCESS;
9601 /* First, make sure the variable is declared at the
9602 module-level scope (J3/04-007, Section 15.3). */
9603 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
9604 sym->attr.in_common == 0)
9606 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
9607 "is neither a COMMON block nor declared at the "
9608 "module level scope", sym->name, &(sym->declared_at));
9611 else if (sym->common_head != NULL)
9613 t = verify_com_block_vars_c_interop (sym->common_head);
9617 /* If type() declaration, we need to verify that the components
9618 of the given type are all C interoperable, etc. */
9619 if (sym->ts.type == BT_DERIVED &&
9620 sym->ts.derived->attr.is_c_interop != 1)
9622 /* Make sure the user marked the derived type as BIND(C). If
9623 not, call the verify routine. This could print an error
9624 for the derived type more than once if multiple variables
9625 of that type are declared. */
9626 if (sym->ts.derived->attr.is_bind_c != 1)
9627 verify_bind_c_derived_type (sym->ts.derived);
9631 /* Verify the variable itself as C interoperable if it
9632 is BIND(C). It is not possible for this to succeed if
9633 the verify_bind_c_derived_type failed, so don't have to handle
9634 any error returned by verify_bind_c_derived_type. */
9635 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
9641 /* clear the is_bind_c flag to prevent reporting errors more than
9642 once if something failed. */
9643 sym->attr.is_bind_c = 0;
9648 /* If a derived type symbol has reached this point, without its
9649 type being declared, we have an error. Notice that most
9650 conditions that produce undefined derived types have already
9651 been dealt with. However, the likes of:
9652 implicit type(t) (t) ..... call foo (t) will get us here if
9653 the type is not declared in the scope of the implicit
9654 statement. Change the type to BT_UNKNOWN, both because it is so
9655 and to prevent an ICE. */
9656 if (sym->ts.type == BT_DERIVED && sym->ts.derived->components == NULL
9657 && !sym->ts.derived->attr.zero_comp)
9659 gfc_error ("The derived type '%s' at %L is of type '%s', "
9660 "which has not been defined", sym->name,
9661 &sym->declared_at, sym->ts.derived->name);
9662 sym->ts.type = BT_UNKNOWN;
9666 /* Make sure that the derived type has been resolved and that the
9667 derived type is visible in the symbol's namespace, if it is a
9668 module function and is not PRIVATE. */
9669 if (sym->ts.type == BT_DERIVED
9670 && sym->ts.derived->attr.use_assoc
9671 && sym->ns->proc_name
9672 && sym->ns->proc_name->attr.flavor == FL_MODULE)
9676 if (resolve_fl_derived (sym->ts.derived) == FAILURE)
9679 gfc_find_symbol (sym->ts.derived->name, sym->ns, 1, &ds);
9680 if (!ds && sym->attr.function
9681 && gfc_check_access (sym->attr.access, sym->ns->default_access))
9683 symtree = gfc_new_symtree (&sym->ns->sym_root,
9684 sym->ts.derived->name);
9685 symtree->n.sym = sym->ts.derived;
9686 sym->ts.derived->refs++;
9690 /* Unless the derived-type declaration is use associated, Fortran 95
9691 does not allow public entries of private derived types.
9692 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
9694 if (sym->ts.type == BT_DERIVED
9695 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
9696 && !sym->ts.derived->attr.use_assoc
9697 && gfc_check_access (sym->attr.access, sym->ns->default_access)
9698 && !gfc_check_access (sym->ts.derived->attr.access,
9699 sym->ts.derived->ns->default_access)
9700 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
9701 "of PRIVATE derived type '%s'",
9702 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
9703 : "variable", sym->name, &sym->declared_at,
9704 sym->ts.derived->name) == FAILURE)
9707 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
9708 default initialization is defined (5.1.2.4.4). */
9709 if (sym->ts.type == BT_DERIVED
9711 && sym->attr.intent == INTENT_OUT
9713 && sym->as->type == AS_ASSUMED_SIZE)
9715 for (c = sym->ts.derived->components; c; c = c->next)
9719 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
9720 "ASSUMED SIZE and so cannot have a default initializer",
9721 sym->name, &sym->declared_at);
9727 switch (sym->attr.flavor)
9730 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
9735 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
9740 if (resolve_fl_namelist (sym) == FAILURE)
9745 if (resolve_fl_parameter (sym) == FAILURE)
9753 /* Resolve array specifier. Check as well some constraints
9754 on COMMON blocks. */
9756 check_constant = sym->attr.in_common && !sym->attr.pointer;
9758 /* Set the formal_arg_flag so that check_conflict will not throw
9759 an error for host associated variables in the specification
9760 expression for an array_valued function. */
9761 if (sym->attr.function && sym->as)
9762 formal_arg_flag = 1;
9764 gfc_resolve_array_spec (sym->as, check_constant);
9766 formal_arg_flag = 0;
9768 /* Resolve formal namespaces. */
9769 if (sym->formal_ns && sym->formal_ns != gfc_current_ns)
9770 gfc_resolve (sym->formal_ns);
9772 /* Check threadprivate restrictions. */
9773 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
9774 && (!sym->attr.in_common
9775 && sym->module == NULL
9776 && (sym->ns->proc_name == NULL
9777 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
9778 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
9780 /* If we have come this far we can apply default-initializers, as
9781 described in 14.7.5, to those variables that have not already
9782 been assigned one. */
9783 if (sym->ts.type == BT_DERIVED
9784 && sym->attr.referenced
9785 && sym->ns == gfc_current_ns
9787 && !sym->attr.allocatable
9788 && !sym->attr.alloc_comp)
9790 symbol_attribute *a = &sym->attr;
9792 if ((!a->save && !a->dummy && !a->pointer
9793 && !a->in_common && !a->use_assoc
9794 && !(a->function && sym != sym->result))
9795 || (a->dummy && a->intent == INTENT_OUT))
9796 apply_default_init (sym);
9799 /* If this symbol has a type-spec, check it. */
9800 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
9801 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
9802 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
9808 /************* Resolve DATA statements *************/
9812 gfc_data_value *vnode;
9818 /* Advance the values structure to point to the next value in the data list. */
9821 next_data_value (void)
9824 while (mpz_cmp_ui (values.left, 0) == 0)
9826 if (values.vnode->next == NULL)
9829 values.vnode = values.vnode->next;
9830 mpz_set (values.left, values.vnode->repeat);
9838 check_data_variable (gfc_data_variable *var, locus *where)
9844 ar_type mark = AR_UNKNOWN;
9846 mpz_t section_index[GFC_MAX_DIMENSIONS];
9852 if (gfc_resolve_expr (var->expr) == FAILURE)
9856 mpz_init_set_si (offset, 0);
9859 if (e->expr_type != EXPR_VARIABLE)
9860 gfc_internal_error ("check_data_variable(): Bad expression");
9862 sym = e->symtree->n.sym;
9864 if (sym->ns->is_block_data && !sym->attr.in_common)
9866 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
9867 sym->name, &sym->declared_at);
9870 if (e->ref == NULL && sym->as)
9872 gfc_error ("DATA array '%s' at %L must be specified in a previous"
9873 " declaration", sym->name, where);
9877 has_pointer = sym->attr.pointer;
9879 for (ref = e->ref; ref; ref = ref->next)
9881 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
9885 && ref->type == REF_ARRAY
9886 && ref->u.ar.type != AR_FULL)
9888 gfc_error ("DATA element '%s' at %L is a pointer and so must "
9889 "be a full array", sym->name, where);
9894 if (e->rank == 0 || has_pointer)
9896 mpz_init_set_ui (size, 1);
9903 /* Find the array section reference. */
9904 for (ref = e->ref; ref; ref = ref->next)
9906 if (ref->type != REF_ARRAY)
9908 if (ref->u.ar.type == AR_ELEMENT)
9914 /* Set marks according to the reference pattern. */
9915 switch (ref->u.ar.type)
9923 /* Get the start position of array section. */
9924 gfc_get_section_index (ar, section_index, &offset);
9932 if (gfc_array_size (e, &size) == FAILURE)
9934 gfc_error ("Nonconstant array section at %L in DATA statement",
9943 while (mpz_cmp_ui (size, 0) > 0)
9945 if (next_data_value () == FAILURE)
9947 gfc_error ("DATA statement at %L has more variables than values",
9953 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
9957 /* If we have more than one element left in the repeat count,
9958 and we have more than one element left in the target variable,
9959 then create a range assignment. */
9960 /* FIXME: Only done for full arrays for now, since array sections
9962 if (mark == AR_FULL && ref && ref->next == NULL
9963 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
9967 if (mpz_cmp (size, values.left) >= 0)
9969 mpz_init_set (range, values.left);
9970 mpz_sub (size, size, values.left);
9971 mpz_set_ui (values.left, 0);
9975 mpz_init_set (range, size);
9976 mpz_sub (values.left, values.left, size);
9977 mpz_set_ui (size, 0);
9980 gfc_assign_data_value_range (var->expr, values.vnode->expr,
9983 mpz_add (offset, offset, range);
9987 /* Assign initial value to symbol. */
9990 mpz_sub_ui (values.left, values.left, 1);
9991 mpz_sub_ui (size, size, 1);
9993 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
9997 if (mark == AR_FULL)
9998 mpz_add_ui (offset, offset, 1);
10000 /* Modify the array section indexes and recalculate the offset
10001 for next element. */
10002 else if (mark == AR_SECTION)
10003 gfc_advance_section (section_index, ar, &offset);
10007 if (mark == AR_SECTION)
10009 for (i = 0; i < ar->dimen; i++)
10010 mpz_clear (section_index[i]);
10014 mpz_clear (offset);
10020 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
10022 /* Iterate over a list of elements in a DATA statement. */
10025 traverse_data_list (gfc_data_variable *var, locus *where)
10028 iterator_stack frame;
10029 gfc_expr *e, *start, *end, *step;
10030 gfc_try retval = SUCCESS;
10032 mpz_init (frame.value);
10034 start = gfc_copy_expr (var->iter.start);
10035 end = gfc_copy_expr (var->iter.end);
10036 step = gfc_copy_expr (var->iter.step);
10038 if (gfc_simplify_expr (start, 1) == FAILURE
10039 || start->expr_type != EXPR_CONSTANT)
10041 gfc_error ("iterator start at %L does not simplify", &start->where);
10045 if (gfc_simplify_expr (end, 1) == FAILURE
10046 || end->expr_type != EXPR_CONSTANT)
10048 gfc_error ("iterator end at %L does not simplify", &end->where);
10052 if (gfc_simplify_expr (step, 1) == FAILURE
10053 || step->expr_type != EXPR_CONSTANT)
10055 gfc_error ("iterator step at %L does not simplify", &step->where);
10060 mpz_init_set (trip, end->value.integer);
10061 mpz_sub (trip, trip, start->value.integer);
10062 mpz_add (trip, trip, step->value.integer);
10064 mpz_div (trip, trip, step->value.integer);
10066 mpz_set (frame.value, start->value.integer);
10068 frame.prev = iter_stack;
10069 frame.variable = var->iter.var->symtree;
10070 iter_stack = &frame;
10072 while (mpz_cmp_ui (trip, 0) > 0)
10074 if (traverse_data_var (var->list, where) == FAILURE)
10081 e = gfc_copy_expr (var->expr);
10082 if (gfc_simplify_expr (e, 1) == FAILURE)
10090 mpz_add (frame.value, frame.value, step->value.integer);
10092 mpz_sub_ui (trip, trip, 1);
10097 mpz_clear (frame.value);
10099 gfc_free_expr (start);
10100 gfc_free_expr (end);
10101 gfc_free_expr (step);
10103 iter_stack = frame.prev;
10108 /* Type resolve variables in the variable list of a DATA statement. */
10111 traverse_data_var (gfc_data_variable *var, locus *where)
10115 for (; var; var = var->next)
10117 if (var->expr == NULL)
10118 t = traverse_data_list (var, where);
10120 t = check_data_variable (var, where);
10130 /* Resolve the expressions and iterators associated with a data statement.
10131 This is separate from the assignment checking because data lists should
10132 only be resolved once. */
10135 resolve_data_variables (gfc_data_variable *d)
10137 for (; d; d = d->next)
10139 if (d->list == NULL)
10141 if (gfc_resolve_expr (d->expr) == FAILURE)
10146 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
10149 if (resolve_data_variables (d->list) == FAILURE)
10158 /* Resolve a single DATA statement. We implement this by storing a pointer to
10159 the value list into static variables, and then recursively traversing the
10160 variables list, expanding iterators and such. */
10163 resolve_data (gfc_data *d)
10166 if (resolve_data_variables (d->var) == FAILURE)
10169 values.vnode = d->value;
10170 if (d->value == NULL)
10171 mpz_set_ui (values.left, 0);
10173 mpz_set (values.left, d->value->repeat);
10175 if (traverse_data_var (d->var, &d->where) == FAILURE)
10178 /* At this point, we better not have any values left. */
10180 if (next_data_value () == SUCCESS)
10181 gfc_error ("DATA statement at %L has more values than variables",
10186 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
10187 accessed by host or use association, is a dummy argument to a pure function,
10188 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
10189 is storage associated with any such variable, shall not be used in the
10190 following contexts: (clients of this function). */
10192 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
10193 procedure. Returns zero if assignment is OK, nonzero if there is a
10196 gfc_impure_variable (gfc_symbol *sym)
10200 if (sym->attr.use_assoc || sym->attr.in_common)
10203 if (sym->ns != gfc_current_ns)
10204 return !sym->attr.function;
10206 proc = sym->ns->proc_name;
10207 if (sym->attr.dummy && gfc_pure (proc)
10208 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
10210 proc->attr.function))
10213 /* TODO: Sort out what can be storage associated, if anything, and include
10214 it here. In principle equivalences should be scanned but it does not
10215 seem to be possible to storage associate an impure variable this way. */
10220 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
10221 symbol of the current procedure. */
10224 gfc_pure (gfc_symbol *sym)
10226 symbol_attribute attr;
10229 sym = gfc_current_ns->proc_name;
10235 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
10239 /* Test whether the current procedure is elemental or not. */
10242 gfc_elemental (gfc_symbol *sym)
10244 symbol_attribute attr;
10247 sym = gfc_current_ns->proc_name;
10252 return attr.flavor == FL_PROCEDURE && attr.elemental;
10256 /* Warn about unused labels. */
10259 warn_unused_fortran_label (gfc_st_label *label)
10264 warn_unused_fortran_label (label->left);
10266 if (label->defined == ST_LABEL_UNKNOWN)
10269 switch (label->referenced)
10271 case ST_LABEL_UNKNOWN:
10272 gfc_warning ("Label %d at %L defined but not used", label->value,
10276 case ST_LABEL_BAD_TARGET:
10277 gfc_warning ("Label %d at %L defined but cannot be used",
10278 label->value, &label->where);
10285 warn_unused_fortran_label (label->right);
10289 /* Returns the sequence type of a symbol or sequence. */
10292 sequence_type (gfc_typespec ts)
10301 if (ts.derived->components == NULL)
10302 return SEQ_NONDEFAULT;
10304 result = sequence_type (ts.derived->components->ts);
10305 for (c = ts.derived->components->next; c; c = c->next)
10306 if (sequence_type (c->ts) != result)
10312 if (ts.kind != gfc_default_character_kind)
10313 return SEQ_NONDEFAULT;
10315 return SEQ_CHARACTER;
10318 if (ts.kind != gfc_default_integer_kind)
10319 return SEQ_NONDEFAULT;
10321 return SEQ_NUMERIC;
10324 if (!(ts.kind == gfc_default_real_kind
10325 || ts.kind == gfc_default_double_kind))
10326 return SEQ_NONDEFAULT;
10328 return SEQ_NUMERIC;
10331 if (ts.kind != gfc_default_complex_kind)
10332 return SEQ_NONDEFAULT;
10334 return SEQ_NUMERIC;
10337 if (ts.kind != gfc_default_logical_kind)
10338 return SEQ_NONDEFAULT;
10340 return SEQ_NUMERIC;
10343 return SEQ_NONDEFAULT;
10348 /* Resolve derived type EQUIVALENCE object. */
10351 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
10354 gfc_component *c = derived->components;
10359 /* Shall not be an object of nonsequence derived type. */
10360 if (!derived->attr.sequence)
10362 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
10363 "attribute to be an EQUIVALENCE object", sym->name,
10368 /* Shall not have allocatable components. */
10369 if (derived->attr.alloc_comp)
10371 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
10372 "components to be an EQUIVALENCE object",sym->name,
10377 if (sym->attr.in_common && has_default_initializer (sym->ts.derived))
10379 gfc_error ("Derived type variable '%s' at %L with default "
10380 "initialization cannot be in EQUIVALENCE with a variable "
10381 "in COMMON", sym->name, &e->where);
10385 for (; c ; c = c->next)
10389 && (resolve_equivalence_derived (c->ts.derived, sym, e) == FAILURE))
10392 /* Shall not be an object of sequence derived type containing a pointer
10393 in the structure. */
10394 if (c->attr.pointer)
10396 gfc_error ("Derived type variable '%s' at %L with pointer "
10397 "component(s) cannot be an EQUIVALENCE object",
10398 sym->name, &e->where);
10406 /* Resolve equivalence object.
10407 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
10408 an allocatable array, an object of nonsequence derived type, an object of
10409 sequence derived type containing a pointer at any level of component
10410 selection, an automatic object, a function name, an entry name, a result
10411 name, a named constant, a structure component, or a subobject of any of
10412 the preceding objects. A substring shall not have length zero. A
10413 derived type shall not have components with default initialization nor
10414 shall two objects of an equivalence group be initialized.
10415 Either all or none of the objects shall have an protected attribute.
10416 The simple constraints are done in symbol.c(check_conflict) and the rest
10417 are implemented here. */
10420 resolve_equivalence (gfc_equiv *eq)
10423 gfc_symbol *derived;
10424 gfc_symbol *first_sym;
10427 locus *last_where = NULL;
10428 seq_type eq_type, last_eq_type;
10429 gfc_typespec *last_ts;
10430 int object, cnt_protected;
10431 const char *value_name;
10435 last_ts = &eq->expr->symtree->n.sym->ts;
10437 first_sym = eq->expr->symtree->n.sym;
10441 for (object = 1; eq; eq = eq->eq, object++)
10445 e->ts = e->symtree->n.sym->ts;
10446 /* match_varspec might not know yet if it is seeing
10447 array reference or substring reference, as it doesn't
10449 if (e->ref && e->ref->type == REF_ARRAY)
10451 gfc_ref *ref = e->ref;
10452 sym = e->symtree->n.sym;
10454 if (sym->attr.dimension)
10456 ref->u.ar.as = sym->as;
10460 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
10461 if (e->ts.type == BT_CHARACTER
10463 && ref->type == REF_ARRAY
10464 && ref->u.ar.dimen == 1
10465 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
10466 && ref->u.ar.stride[0] == NULL)
10468 gfc_expr *start = ref->u.ar.start[0];
10469 gfc_expr *end = ref->u.ar.end[0];
10472 /* Optimize away the (:) reference. */
10473 if (start == NULL && end == NULL)
10476 e->ref = ref->next;
10478 e->ref->next = ref->next;
10483 ref->type = REF_SUBSTRING;
10485 start = gfc_int_expr (1);
10486 ref->u.ss.start = start;
10487 if (end == NULL && e->ts.cl)
10488 end = gfc_copy_expr (e->ts.cl->length);
10489 ref->u.ss.end = end;
10490 ref->u.ss.length = e->ts.cl;
10497 /* Any further ref is an error. */
10500 gcc_assert (ref->type == REF_ARRAY);
10501 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
10507 if (gfc_resolve_expr (e) == FAILURE)
10510 sym = e->symtree->n.sym;
10512 if (sym->attr.is_protected)
10514 if (cnt_protected > 0 && cnt_protected != object)
10516 gfc_error ("Either all or none of the objects in the "
10517 "EQUIVALENCE set at %L shall have the "
10518 "PROTECTED attribute",
10523 /* Shall not equivalence common block variables in a PURE procedure. */
10524 if (sym->ns->proc_name
10525 && sym->ns->proc_name->attr.pure
10526 && sym->attr.in_common)
10528 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
10529 "object in the pure procedure '%s'",
10530 sym->name, &e->where, sym->ns->proc_name->name);
10534 /* Shall not be a named constant. */
10535 if (e->expr_type == EXPR_CONSTANT)
10537 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
10538 "object", sym->name, &e->where);
10542 derived = e->ts.derived;
10543 if (derived && resolve_equivalence_derived (derived, sym, e) == FAILURE)
10546 /* Check that the types correspond correctly:
10548 A numeric sequence structure may be equivalenced to another sequence
10549 structure, an object of default integer type, default real type, double
10550 precision real type, default logical type such that components of the
10551 structure ultimately only become associated to objects of the same
10552 kind. A character sequence structure may be equivalenced to an object
10553 of default character kind or another character sequence structure.
10554 Other objects may be equivalenced only to objects of the same type and
10555 kind parameters. */
10557 /* Identical types are unconditionally OK. */
10558 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
10559 goto identical_types;
10561 last_eq_type = sequence_type (*last_ts);
10562 eq_type = sequence_type (sym->ts);
10564 /* Since the pair of objects is not of the same type, mixed or
10565 non-default sequences can be rejected. */
10567 msg = "Sequence %s with mixed components in EQUIVALENCE "
10568 "statement at %L with different type objects";
10570 && last_eq_type == SEQ_MIXED
10571 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
10573 || (eq_type == SEQ_MIXED
10574 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
10575 &e->where) == FAILURE))
10578 msg = "Non-default type object or sequence %s in EQUIVALENCE "
10579 "statement at %L with objects of different type";
10581 && last_eq_type == SEQ_NONDEFAULT
10582 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
10583 last_where) == FAILURE)
10584 || (eq_type == SEQ_NONDEFAULT
10585 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
10586 &e->where) == FAILURE))
10589 msg ="Non-CHARACTER object '%s' in default CHARACTER "
10590 "EQUIVALENCE statement at %L";
10591 if (last_eq_type == SEQ_CHARACTER
10592 && eq_type != SEQ_CHARACTER
10593 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
10594 &e->where) == FAILURE)
10597 msg ="Non-NUMERIC object '%s' in default NUMERIC "
10598 "EQUIVALENCE statement at %L";
10599 if (last_eq_type == SEQ_NUMERIC
10600 && eq_type != SEQ_NUMERIC
10601 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
10602 &e->where) == FAILURE)
10607 last_where = &e->where;
10612 /* Shall not be an automatic array. */
10613 if (e->ref->type == REF_ARRAY
10614 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
10616 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
10617 "an EQUIVALENCE object", sym->name, &e->where);
10624 /* Shall not be a structure component. */
10625 if (r->type == REF_COMPONENT)
10627 gfc_error ("Structure component '%s' at %L cannot be an "
10628 "EQUIVALENCE object",
10629 r->u.c.component->name, &e->where);
10633 /* A substring shall not have length zero. */
10634 if (r->type == REF_SUBSTRING)
10636 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
10638 gfc_error ("Substring at %L has length zero",
10639 &r->u.ss.start->where);
10649 /* Resolve function and ENTRY types, issue diagnostics if needed. */
10652 resolve_fntype (gfc_namespace *ns)
10654 gfc_entry_list *el;
10657 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
10660 /* If there are any entries, ns->proc_name is the entry master
10661 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
10663 sym = ns->entries->sym;
10665 sym = ns->proc_name;
10666 if (sym->result == sym
10667 && sym->ts.type == BT_UNKNOWN
10668 && gfc_set_default_type (sym, 0, NULL) == FAILURE
10669 && !sym->attr.untyped)
10671 gfc_error ("Function '%s' at %L has no IMPLICIT type",
10672 sym->name, &sym->declared_at);
10673 sym->attr.untyped = 1;
10676 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.use_assoc
10677 && !sym->attr.contained
10678 && !gfc_check_access (sym->ts.derived->attr.access,
10679 sym->ts.derived->ns->default_access)
10680 && gfc_check_access (sym->attr.access, sym->ns->default_access))
10682 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
10683 "%L of PRIVATE type '%s'", sym->name,
10684 &sym->declared_at, sym->ts.derived->name);
10688 for (el = ns->entries->next; el; el = el->next)
10690 if (el->sym->result == el->sym
10691 && el->sym->ts.type == BT_UNKNOWN
10692 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
10693 && !el->sym->attr.untyped)
10695 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
10696 el->sym->name, &el->sym->declared_at);
10697 el->sym->attr.untyped = 1;
10702 /* 12.3.2.1.1 Defined operators. */
10705 gfc_resolve_uops (gfc_symtree *symtree)
10707 gfc_interface *itr;
10709 gfc_formal_arglist *formal;
10711 if (symtree == NULL)
10714 gfc_resolve_uops (symtree->left);
10715 gfc_resolve_uops (symtree->right);
10717 for (itr = symtree->n.uop->op; itr; itr = itr->next)
10720 if (!sym->attr.function)
10721 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
10722 sym->name, &sym->declared_at);
10724 if (sym->ts.type == BT_CHARACTER
10725 && !(sym->ts.cl && sym->ts.cl->length)
10726 && !(sym->result && sym->result->ts.cl
10727 && sym->result->ts.cl->length))
10728 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
10729 "character length", sym->name, &sym->declared_at);
10731 formal = sym->formal;
10732 if (!formal || !formal->sym)
10734 gfc_error ("User operator procedure '%s' at %L must have at least "
10735 "one argument", sym->name, &sym->declared_at);
10739 if (formal->sym->attr.intent != INTENT_IN)
10740 gfc_error ("First argument of operator interface at %L must be "
10741 "INTENT(IN)", &sym->declared_at);
10743 if (formal->sym->attr.optional)
10744 gfc_error ("First argument of operator interface at %L cannot be "
10745 "optional", &sym->declared_at);
10747 formal = formal->next;
10748 if (!formal || !formal->sym)
10751 if (formal->sym->attr.intent != INTENT_IN)
10752 gfc_error ("Second argument of operator interface at %L must be "
10753 "INTENT(IN)", &sym->declared_at);
10755 if (formal->sym->attr.optional)
10756 gfc_error ("Second argument of operator interface at %L cannot be "
10757 "optional", &sym->declared_at);
10760 gfc_error ("Operator interface at %L must have, at most, two "
10761 "arguments", &sym->declared_at);
10766 /* Examine all of the expressions associated with a program unit,
10767 assign types to all intermediate expressions, make sure that all
10768 assignments are to compatible types and figure out which names
10769 refer to which functions or subroutines. It doesn't check code
10770 block, which is handled by resolve_code. */
10773 resolve_types (gfc_namespace *ns)
10779 gfc_namespace* old_ns = gfc_current_ns;
10781 /* Check that all IMPLICIT types are ok. */
10782 if (!ns->seen_implicit_none)
10785 for (letter = 0; letter != GFC_LETTERS; ++letter)
10786 if (ns->set_flag[letter]
10787 && resolve_typespec_used (&ns->default_type[letter],
10788 &ns->implicit_loc[letter],
10793 gfc_current_ns = ns;
10795 resolve_entries (ns);
10797 resolve_common_vars (ns->blank_common.head, false);
10798 resolve_common_blocks (ns->common_root);
10800 resolve_contained_functions (ns);
10802 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
10804 for (cl = ns->cl_list; cl; cl = cl->next)
10805 resolve_charlen (cl);
10807 gfc_traverse_ns (ns, resolve_symbol);
10809 resolve_fntype (ns);
10811 for (n = ns->contained; n; n = n->sibling)
10813 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
10814 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
10815 "also be PURE", n->proc_name->name,
10816 &n->proc_name->declared_at);
10822 gfc_check_interfaces (ns);
10824 gfc_traverse_ns (ns, resolve_values);
10830 for (d = ns->data; d; d = d->next)
10834 gfc_traverse_ns (ns, gfc_formalize_init_value);
10836 gfc_traverse_ns (ns, gfc_verify_binding_labels);
10838 if (ns->common_root != NULL)
10839 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
10841 for (eq = ns->equiv; eq; eq = eq->next)
10842 resolve_equivalence (eq);
10844 /* Warn about unused labels. */
10845 if (warn_unused_label)
10846 warn_unused_fortran_label (ns->st_labels);
10848 gfc_resolve_uops (ns->uop_root);
10850 gfc_current_ns = old_ns;
10854 /* Call resolve_code recursively. */
10857 resolve_codes (gfc_namespace *ns)
10860 bitmap_obstack old_obstack;
10862 for (n = ns->contained; n; n = n->sibling)
10865 gfc_current_ns = ns;
10867 /* Set to an out of range value. */
10868 current_entry_id = -1;
10870 old_obstack = labels_obstack;
10871 bitmap_obstack_initialize (&labels_obstack);
10873 resolve_code (ns->code, ns);
10875 bitmap_obstack_release (&labels_obstack);
10876 labels_obstack = old_obstack;
10880 /* This function is called after a complete program unit has been compiled.
10881 Its purpose is to examine all of the expressions associated with a program
10882 unit, assign types to all intermediate expressions, make sure that all
10883 assignments are to compatible types and figure out which names refer to
10884 which functions or subroutines. */
10887 gfc_resolve (gfc_namespace *ns)
10889 gfc_namespace *old_ns;
10894 old_ns = gfc_current_ns;
10896 resolve_types (ns);
10897 resolve_codes (ns);
10899 gfc_current_ns = old_ns;