1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
31 #include "target-memory.h" /* for gfc_simplify_transfer */
33 /* Types used in equivalence statements. */
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
41 /* Stack to keep track of the nesting of blocks as we move through the
42 code. See resolve_branch() and resolve_code(). */
44 typedef struct code_stack
46 struct gfc_code *head, *current;
47 struct code_stack *prev;
49 /* This bitmap keeps track of the targets valid for a branch from
50 inside this block except for END {IF|SELECT}s of enclosing
52 bitmap reachable_labels;
56 static code_stack *cs_base = NULL;
59 /* Nonzero if we're inside a FORALL block. */
61 static int forall_flag;
63 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
65 static int omp_workshare_flag;
67 /* Nonzero if we are processing a formal arglist. The corresponding function
68 resets the flag each time that it is read. */
69 static int formal_arg_flag = 0;
71 /* True if we are resolving a specification expression. */
72 static int specification_expr = 0;
74 /* The id of the last entry seen. */
75 static int current_entry_id;
77 /* We use bitmaps to determine if a branch target is valid. */
78 static bitmap_obstack labels_obstack;
81 gfc_is_formal_arg (void)
83 return formal_arg_flag;
86 /* Is the symbol host associated? */
88 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
90 for (ns = ns->parent; ns; ns = ns->parent)
99 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
100 an ABSTRACT derived-type. If where is not NULL, an error message with that
101 locus is printed, optionally using name. */
104 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
106 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
111 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
112 name, where, ts->u.derived->name);
114 gfc_error ("ABSTRACT type '%s' used at %L",
115 ts->u.derived->name, where);
125 /* Resolve types of formal argument lists. These have to be done early so that
126 the formal argument lists of module procedures can be copied to the
127 containing module before the individual procedures are resolved
128 individually. We also resolve argument lists of procedures in interface
129 blocks because they are self-contained scoping units.
131 Since a dummy argument cannot be a non-dummy procedure, the only
132 resort left for untyped names are the IMPLICIT types. */
135 resolve_formal_arglist (gfc_symbol *proc)
137 gfc_formal_arglist *f;
141 if (proc->result != NULL)
146 if (gfc_elemental (proc)
147 || sym->attr.pointer || sym->attr.allocatable
148 || (sym->as && sym->as->rank > 0))
150 proc->attr.always_explicit = 1;
151 sym->attr.always_explicit = 1;
156 for (f = proc->formal; f; f = f->next)
162 /* Alternate return placeholder. */
163 if (gfc_elemental (proc))
164 gfc_error ("Alternate return specifier in elemental subroutine "
165 "'%s' at %L is not allowed", proc->name,
167 if (proc->attr.function)
168 gfc_error ("Alternate return specifier in function "
169 "'%s' at %L is not allowed", proc->name,
174 if (sym->attr.if_source != IFSRC_UNKNOWN)
175 resolve_formal_arglist (sym);
177 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
179 if (gfc_pure (proc) && !gfc_pure (sym))
181 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
182 "also be PURE", sym->name, &sym->declared_at);
186 if (gfc_elemental (proc))
188 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
189 "procedure", &sym->declared_at);
193 if (sym->attr.function
194 && sym->ts.type == BT_UNKNOWN
195 && sym->attr.intrinsic)
197 gfc_intrinsic_sym *isym;
198 isym = gfc_find_function (sym->name);
199 if (isym == NULL || !isym->specific)
201 gfc_error ("Unable to find a specific INTRINSIC procedure "
202 "for the reference '%s' at %L", sym->name,
211 if (sym->ts.type == BT_UNKNOWN)
213 if (!sym->attr.function || sym->result == sym)
214 gfc_set_default_type (sym, 1, sym->ns);
217 gfc_resolve_array_spec (sym->as, 0);
219 /* We can't tell if an array with dimension (:) is assumed or deferred
220 shape until we know if it has the pointer or allocatable attributes.
222 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
223 && !(sym->attr.pointer || sym->attr.allocatable))
225 sym->as->type = AS_ASSUMED_SHAPE;
226 for (i = 0; i < sym->as->rank; i++)
227 sym->as->lower[i] = gfc_int_expr (1);
230 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
231 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
232 || sym->attr.optional)
234 proc->attr.always_explicit = 1;
236 proc->result->attr.always_explicit = 1;
239 /* If the flavor is unknown at this point, it has to be a variable.
240 A procedure specification would have already set the type. */
242 if (sym->attr.flavor == FL_UNKNOWN)
243 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
245 if (gfc_pure (proc) && !sym->attr.pointer
246 && sym->attr.flavor != FL_PROCEDURE)
248 if (proc->attr.function && sym->attr.intent != INTENT_IN)
249 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
250 "INTENT(IN)", sym->name, proc->name,
253 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
254 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
255 "have its INTENT specified", sym->name, proc->name,
259 if (gfc_elemental (proc))
263 gfc_error ("Argument '%s' of elemental procedure at %L must "
264 "be scalar", sym->name, &sym->declared_at);
268 if (sym->attr.pointer)
270 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
271 "have the POINTER attribute", sym->name,
276 if (sym->attr.flavor == FL_PROCEDURE)
278 gfc_error ("Dummy procedure '%s' not allowed in elemental "
279 "procedure '%s' at %L", sym->name, proc->name,
285 /* Each dummy shall be specified to be scalar. */
286 if (proc->attr.proc == PROC_ST_FUNCTION)
290 gfc_error ("Argument '%s' of statement function at %L must "
291 "be scalar", sym->name, &sym->declared_at);
295 if (sym->ts.type == BT_CHARACTER)
297 gfc_charlen *cl = sym->ts.u.cl;
298 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
300 gfc_error ("Character-valued argument '%s' of statement "
301 "function at %L must have constant length",
302 sym->name, &sym->declared_at);
312 /* Work function called when searching for symbols that have argument lists
313 associated with them. */
316 find_arglists (gfc_symbol *sym)
318 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
321 resolve_formal_arglist (sym);
325 /* Given a namespace, resolve all formal argument lists within the namespace.
329 resolve_formal_arglists (gfc_namespace *ns)
334 gfc_traverse_ns (ns, find_arglists);
339 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
343 /* If this namespace is not a function or an entry master function,
345 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
346 || sym->attr.entry_master)
349 /* Try to find out of what the return type is. */
350 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
352 t = gfc_set_default_type (sym->result, 0, ns);
354 if (t == FAILURE && !sym->result->attr.untyped)
356 if (sym->result == sym)
357 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
358 sym->name, &sym->declared_at);
359 else if (!sym->result->attr.proc_pointer)
360 gfc_error ("Result '%s' of contained function '%s' at %L has "
361 "no IMPLICIT type", sym->result->name, sym->name,
362 &sym->result->declared_at);
363 sym->result->attr.untyped = 1;
367 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
368 type, lists the only ways a character length value of * can be used:
369 dummy arguments of procedures, named constants, and function results
370 in external functions. Internal function results and results of module
371 procedures are not on this list, ergo, not permitted. */
373 if (sym->result->ts.type == BT_CHARACTER)
375 gfc_charlen *cl = sym->result->ts.u.cl;
376 if (!cl || !cl->length)
378 /* See if this is a module-procedure and adapt error message
381 gcc_assert (ns->parent && ns->parent->proc_name);
382 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
384 gfc_error ("Character-valued %s '%s' at %L must not be"
386 module_proc ? _("module procedure")
387 : _("internal function"),
388 sym->name, &sym->declared_at);
394 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
395 introduce duplicates. */
398 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
400 gfc_formal_arglist *f, *new_arglist;
403 for (; new_args != NULL; new_args = new_args->next)
405 new_sym = new_args->sym;
406 /* See if this arg is already in the formal argument list. */
407 for (f = proc->formal; f; f = f->next)
409 if (new_sym == f->sym)
416 /* Add a new argument. Argument order is not important. */
417 new_arglist = gfc_get_formal_arglist ();
418 new_arglist->sym = new_sym;
419 new_arglist->next = proc->formal;
420 proc->formal = new_arglist;
425 /* Flag the arguments that are not present in all entries. */
428 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
430 gfc_formal_arglist *f, *head;
433 for (f = proc->formal; f; f = f->next)
438 for (new_args = head; new_args; new_args = new_args->next)
440 if (new_args->sym == f->sym)
447 f->sym->attr.not_always_present = 1;
452 /* Resolve alternate entry points. If a symbol has multiple entry points we
453 create a new master symbol for the main routine, and turn the existing
454 symbol into an entry point. */
457 resolve_entries (gfc_namespace *ns)
459 gfc_namespace *old_ns;
463 char name[GFC_MAX_SYMBOL_LEN + 1];
464 static int master_count = 0;
466 if (ns->proc_name == NULL)
469 /* No need to do anything if this procedure doesn't have alternate entry
474 /* We may already have resolved alternate entry points. */
475 if (ns->proc_name->attr.entry_master)
478 /* If this isn't a procedure something has gone horribly wrong. */
479 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
481 /* Remember the current namespace. */
482 old_ns = gfc_current_ns;
486 /* Add the main entry point to the list of entry points. */
487 el = gfc_get_entry_list ();
488 el->sym = ns->proc_name;
490 el->next = ns->entries;
492 ns->proc_name->attr.entry = 1;
494 /* If it is a module function, it needs to be in the right namespace
495 so that gfc_get_fake_result_decl can gather up the results. The
496 need for this arose in get_proc_name, where these beasts were
497 left in their own namespace, to keep prior references linked to
498 the entry declaration.*/
499 if (ns->proc_name->attr.function
500 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
503 /* Do the same for entries where the master is not a module
504 procedure. These are retained in the module namespace because
505 of the module procedure declaration. */
506 for (el = el->next; el; el = el->next)
507 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
508 && el->sym->attr.mod_proc)
512 /* Add an entry statement for it. */
519 /* Create a new symbol for the master function. */
520 /* Give the internal function a unique name (within this file).
521 Also include the function name so the user has some hope of figuring
522 out what is going on. */
523 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
524 master_count++, ns->proc_name->name);
525 gfc_get_ha_symbol (name, &proc);
526 gcc_assert (proc != NULL);
528 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
529 if (ns->proc_name->attr.subroutine)
530 gfc_add_subroutine (&proc->attr, proc->name, NULL);
534 gfc_typespec *ts, *fts;
535 gfc_array_spec *as, *fas;
536 gfc_add_function (&proc->attr, proc->name, NULL);
538 fas = ns->entries->sym->as;
539 fas = fas ? fas : ns->entries->sym->result->as;
540 fts = &ns->entries->sym->result->ts;
541 if (fts->type == BT_UNKNOWN)
542 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
543 for (el = ns->entries->next; el; el = el->next)
545 ts = &el->sym->result->ts;
547 as = as ? as : el->sym->result->as;
548 if (ts->type == BT_UNKNOWN)
549 ts = gfc_get_default_type (el->sym->result->name, NULL);
551 if (! gfc_compare_types (ts, fts)
552 || (el->sym->result->attr.dimension
553 != ns->entries->sym->result->attr.dimension)
554 || (el->sym->result->attr.pointer
555 != ns->entries->sym->result->attr.pointer))
557 else if (as && fas && ns->entries->sym->result != el->sym->result
558 && gfc_compare_array_spec (as, fas) == 0)
559 gfc_error ("Function %s at %L has entries with mismatched "
560 "array specifications", ns->entries->sym->name,
561 &ns->entries->sym->declared_at);
562 /* The characteristics need to match and thus both need to have
563 the same string length, i.e. both len=*, or both len=4.
564 Having both len=<variable> is also possible, but difficult to
565 check at compile time. */
566 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
567 && (((ts->u.cl->length && !fts->u.cl->length)
568 ||(!ts->u.cl->length && fts->u.cl->length))
570 && ts->u.cl->length->expr_type
571 != fts->u.cl->length->expr_type)
573 && ts->u.cl->length->expr_type == EXPR_CONSTANT
574 && mpz_cmp (ts->u.cl->length->value.integer,
575 fts->u.cl->length->value.integer) != 0)))
576 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
577 "entries returning variables of different "
578 "string lengths", ns->entries->sym->name,
579 &ns->entries->sym->declared_at);
584 sym = ns->entries->sym->result;
585 /* All result types the same. */
587 if (sym->attr.dimension)
588 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
589 if (sym->attr.pointer)
590 gfc_add_pointer (&proc->attr, NULL);
594 /* Otherwise the result will be passed through a union by
596 proc->attr.mixed_entry_master = 1;
597 for (el = ns->entries; el; el = el->next)
599 sym = el->sym->result;
600 if (sym->attr.dimension)
602 if (el == ns->entries)
603 gfc_error ("FUNCTION result %s can't be an array 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 an array in "
608 "FUNCTION %s at %L", sym->name,
609 ns->entries->sym->name, &sym->declared_at);
611 else if (sym->attr.pointer)
613 if (el == ns->entries)
614 gfc_error ("FUNCTION result %s can't be a POINTER in "
615 "FUNCTION %s at %L", sym->name,
616 ns->entries->sym->name, &sym->declared_at);
618 gfc_error ("ENTRY result %s can't be a POINTER in "
619 "FUNCTION %s at %L", sym->name,
620 ns->entries->sym->name, &sym->declared_at);
625 if (ts->type == BT_UNKNOWN)
626 ts = gfc_get_default_type (sym->name, NULL);
630 if (ts->kind == gfc_default_integer_kind)
634 if (ts->kind == gfc_default_real_kind
635 || ts->kind == gfc_default_double_kind)
639 if (ts->kind == gfc_default_complex_kind)
643 if (ts->kind == gfc_default_logical_kind)
647 /* We will issue error elsewhere. */
655 if (el == ns->entries)
656 gfc_error ("FUNCTION result %s can't be of type %s "
657 "in FUNCTION %s at %L", sym->name,
658 gfc_typename (ts), ns->entries->sym->name,
661 gfc_error ("ENTRY result %s can't be of type %s "
662 "in FUNCTION %s at %L", sym->name,
663 gfc_typename (ts), ns->entries->sym->name,
670 proc->attr.access = ACCESS_PRIVATE;
671 proc->attr.entry_master = 1;
673 /* Merge all the entry point arguments. */
674 for (el = ns->entries; el; el = el->next)
675 merge_argument_lists (proc, el->sym->formal);
677 /* Check the master formal arguments for any that are not
678 present in all entry points. */
679 for (el = ns->entries; el; el = el->next)
680 check_argument_lists (proc, el->sym->formal);
682 /* Use the master function for the function body. */
683 ns->proc_name = proc;
685 /* Finalize the new symbols. */
686 gfc_commit_symbols ();
688 /* Restore the original namespace. */
689 gfc_current_ns = old_ns;
694 has_default_initializer (gfc_symbol *der)
698 gcc_assert (der->attr.flavor == FL_DERIVED);
699 for (c = der->components; c; c = c->next)
700 if ((c->ts.type != BT_DERIVED && c->initializer)
701 || (c->ts.type == BT_DERIVED
702 && (!c->attr.pointer && has_default_initializer (c->ts.u.derived))))
708 /* Resolve common variables. */
710 resolve_common_vars (gfc_symbol *sym, bool named_common)
712 gfc_symbol *csym = sym;
714 for (; csym; csym = csym->common_next)
716 if (csym->value || csym->attr.data)
718 if (!csym->ns->is_block_data)
719 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
720 "but only in BLOCK DATA initialization is "
721 "allowed", csym->name, &csym->declared_at);
722 else if (!named_common)
723 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
724 "in a blank COMMON but initialization is only "
725 "allowed in named common blocks", csym->name,
729 if (csym->ts.type != BT_DERIVED)
732 if (!(csym->ts.u.derived->attr.sequence
733 || csym->ts.u.derived->attr.is_bind_c))
734 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
735 "has neither the SEQUENCE nor the BIND(C) "
736 "attribute", csym->name, &csym->declared_at);
737 if (csym->ts.u.derived->attr.alloc_comp)
738 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
739 "has an ultimate component that is "
740 "allocatable", csym->name, &csym->declared_at);
741 if (has_default_initializer (csym->ts.u.derived))
742 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
743 "may not have default initializer", csym->name,
746 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
747 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
751 /* Resolve common blocks. */
753 resolve_common_blocks (gfc_symtree *common_root)
757 if (common_root == NULL)
760 if (common_root->left)
761 resolve_common_blocks (common_root->left);
762 if (common_root->right)
763 resolve_common_blocks (common_root->right);
765 resolve_common_vars (common_root->n.common->head, true);
767 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
771 if (sym->attr.flavor == FL_PARAMETER)
772 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
773 sym->name, &common_root->n.common->where, &sym->declared_at);
775 if (sym->attr.intrinsic)
776 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
777 sym->name, &common_root->n.common->where);
778 else if (sym->attr.result
779 || gfc_is_function_return_value (sym, gfc_current_ns))
780 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
781 "that is also a function result", sym->name,
782 &common_root->n.common->where);
783 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
784 && sym->attr.proc != PROC_ST_FUNCTION)
785 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
786 "that is also a global procedure", sym->name,
787 &common_root->n.common->where);
791 /* Resolve contained function types. Because contained functions can call one
792 another, they have to be worked out before any of the contained procedures
795 The good news is that if a function doesn't already have a type, the only
796 way it can get one is through an IMPLICIT type or a RESULT variable, because
797 by definition contained functions are contained namespace they're contained
798 in, not in a sibling or parent namespace. */
801 resolve_contained_functions (gfc_namespace *ns)
803 gfc_namespace *child;
806 resolve_formal_arglists (ns);
808 for (child = ns->contained; child; child = child->sibling)
810 /* Resolve alternate entry points first. */
811 resolve_entries (child);
813 /* Then check function return types. */
814 resolve_contained_fntype (child->proc_name, child);
815 for (el = child->entries; el; el = el->next)
816 resolve_contained_fntype (el->sym, child);
821 /* Resolve all of the elements of a structure constructor and make sure that
822 the types are correct. */
825 resolve_structure_cons (gfc_expr *expr)
827 gfc_constructor *cons;
833 cons = expr->value.constructor;
834 /* A constructor may have references if it is the result of substituting a
835 parameter variable. In this case we just pull out the component we
838 comp = expr->ref->u.c.sym->components;
840 comp = expr->ts.u.derived->components;
842 /* See if the user is trying to invoke a structure constructor for one of
843 the iso_c_binding derived types. */
844 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
845 && expr->ts.u.derived->ts.is_iso_c && cons
846 && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
848 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
849 expr->ts.u.derived->name, &(expr->where));
853 /* Return if structure constructor is c_null_(fun)prt. */
854 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
855 && expr->ts.u.derived->ts.is_iso_c && cons
856 && cons->expr && cons->expr->expr_type == EXPR_NULL)
859 for (; comp; comp = comp->next, cons = cons->next)
866 if (gfc_resolve_expr (cons->expr) == FAILURE)
872 rank = comp->as ? comp->as->rank : 0;
873 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
874 && (comp->attr.allocatable || cons->expr->rank))
876 gfc_error ("The rank of the element in the derived type "
877 "constructor at %L does not match that of the "
878 "component (%d/%d)", &cons->expr->where,
879 cons->expr->rank, rank);
883 /* If we don't have the right type, try to convert it. */
885 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
888 if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
889 gfc_error ("The element in the derived type constructor at %L, "
890 "for pointer component '%s', is %s but should be %s",
891 &cons->expr->where, comp->name,
892 gfc_basic_typename (cons->expr->ts.type),
893 gfc_basic_typename (comp->ts.type));
895 t = gfc_convert_type (cons->expr, &comp->ts, 1);
898 if (cons->expr->expr_type == EXPR_NULL
899 && !(comp->attr.pointer || comp->attr.allocatable
900 || comp->attr.proc_pointer
901 || (comp->ts.type == BT_CLASS
902 && (comp->ts.u.derived->components->attr.pointer
903 || comp->ts.u.derived->components->attr.allocatable))))
906 gfc_error ("The NULL in the derived type constructor at %L is "
907 "being applied to component '%s', which is neither "
908 "a POINTER nor ALLOCATABLE", &cons->expr->where,
912 if (!comp->attr.pointer || cons->expr->expr_type == EXPR_NULL)
915 a = gfc_expr_attr (cons->expr);
917 if (!a.pointer && !a.target)
920 gfc_error ("The element in the derived type constructor at %L, "
921 "for pointer component '%s' should be a POINTER or "
922 "a TARGET", &cons->expr->where, comp->name);
925 /* F2003, C1272 (3). */
926 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
927 && gfc_impure_variable (cons->expr->symtree->n.sym))
930 gfc_error ("Invalid expression in the derived type constructor for pointer "
931 "component '%s' at %L in PURE procedure", comp->name,
940 /****************** Expression name resolution ******************/
942 /* Returns 0 if a symbol was not declared with a type or
943 attribute declaration statement, nonzero otherwise. */
946 was_declared (gfc_symbol *sym)
952 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
955 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
956 || a.optional || a.pointer || a.save || a.target || a.volatile_
957 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
965 /* Determine if a symbol is generic or not. */
968 generic_sym (gfc_symbol *sym)
972 if (sym->attr.generic ||
973 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
976 if (was_declared (sym) || sym->ns->parent == NULL)
979 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
986 return generic_sym (s);
993 /* Determine if a symbol is specific or not. */
996 specific_sym (gfc_symbol *sym)
1000 if (sym->attr.if_source == IFSRC_IFBODY
1001 || sym->attr.proc == PROC_MODULE
1002 || sym->attr.proc == PROC_INTERNAL
1003 || sym->attr.proc == PROC_ST_FUNCTION
1004 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1005 || sym->attr.external)
1008 if (was_declared (sym) || sym->ns->parent == NULL)
1011 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1013 return (s == NULL) ? 0 : specific_sym (s);
1017 /* Figure out if the procedure is specific, generic or unknown. */
1020 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1024 procedure_kind (gfc_symbol *sym)
1026 if (generic_sym (sym))
1027 return PTYPE_GENERIC;
1029 if (specific_sym (sym))
1030 return PTYPE_SPECIFIC;
1032 return PTYPE_UNKNOWN;
1035 /* Check references to assumed size arrays. The flag need_full_assumed_size
1036 is nonzero when matching actual arguments. */
1038 static int need_full_assumed_size = 0;
1041 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1043 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1046 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1047 What should it be? */
1048 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1049 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1050 && (e->ref->u.ar.type == AR_FULL))
1052 gfc_error ("The upper bound in the last dimension must "
1053 "appear in the reference to the assumed size "
1054 "array '%s' at %L", sym->name, &e->where);
1061 /* Look for bad assumed size array references in argument expressions
1062 of elemental and array valued intrinsic procedures. Since this is
1063 called from procedure resolution functions, it only recurses at
1067 resolve_assumed_size_actual (gfc_expr *e)
1072 switch (e->expr_type)
1075 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1080 if (resolve_assumed_size_actual (e->value.op.op1)
1081 || resolve_assumed_size_actual (e->value.op.op2))
1092 /* Check a generic procedure, passed as an actual argument, to see if
1093 there is a matching specific name. If none, it is an error, and if
1094 more than one, the reference is ambiguous. */
1096 count_specific_procs (gfc_expr *e)
1103 sym = e->symtree->n.sym;
1105 for (p = sym->generic; p; p = p->next)
1106 if (strcmp (sym->name, p->sym->name) == 0)
1108 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1114 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1118 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1119 "argument at %L", sym->name, &e->where);
1125 /* See if a call to sym could possibly be a not allowed RECURSION because of
1126 a missing RECURIVE declaration. This means that either sym is the current
1127 context itself, or sym is the parent of a contained procedure calling its
1128 non-RECURSIVE containing procedure.
1129 This also works if sym is an ENTRY. */
1132 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1134 gfc_symbol* proc_sym;
1135 gfc_symbol* context_proc;
1136 gfc_namespace* real_context;
1138 if (sym->attr.flavor == FL_PROGRAM)
1141 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1143 /* If we've got an ENTRY, find real procedure. */
1144 if (sym->attr.entry && sym->ns->entries)
1145 proc_sym = sym->ns->entries->sym;
1149 /* If sym is RECURSIVE, all is well of course. */
1150 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1153 /* Find the context procedure's "real" symbol if it has entries.
1154 We look for a procedure symbol, so recurse on the parents if we don't
1155 find one (like in case of a BLOCK construct). */
1156 for (real_context = context; ; real_context = real_context->parent)
1158 /* We should find something, eventually! */
1159 gcc_assert (real_context);
1161 context_proc = (real_context->entries ? real_context->entries->sym
1162 : real_context->proc_name);
1164 /* In some special cases, there may not be a proc_name, like for this
1166 real(bad_kind()) function foo () ...
1167 when checking the call to bad_kind ().
1168 In these cases, we simply return here and assume that the
1173 if (context_proc->attr.flavor != FL_LABEL)
1177 /* A call from sym's body to itself is recursion, of course. */
1178 if (context_proc == proc_sym)
1181 /* The same is true if context is a contained procedure and sym the
1183 if (context_proc->attr.contained)
1185 gfc_symbol* parent_proc;
1187 gcc_assert (context->parent);
1188 parent_proc = (context->parent->entries ? context->parent->entries->sym
1189 : context->parent->proc_name);
1191 if (parent_proc == proc_sym)
1199 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1200 its typespec and formal argument list. */
1203 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1205 gfc_intrinsic_sym* isym;
1211 /* We already know this one is an intrinsic, so we don't call
1212 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1213 gfc_find_subroutine directly to check whether it is a function or
1216 if ((isym = gfc_find_function (sym->name)))
1218 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1219 && !sym->attr.implicit_type)
1220 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1221 " ignored", sym->name, &sym->declared_at);
1223 if (!sym->attr.function &&
1224 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1229 else if ((isym = gfc_find_subroutine (sym->name)))
1231 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1233 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1234 " specifier", sym->name, &sym->declared_at);
1238 if (!sym->attr.subroutine &&
1239 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1244 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1249 gfc_copy_formal_args_intr (sym, isym);
1251 /* Check it is actually available in the standard settings. */
1252 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1255 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1256 " available in the current standard settings but %s. Use"
1257 " an appropriate -std=* option or enable -fall-intrinsics"
1258 " in order to use it.",
1259 sym->name, &sym->declared_at, symstd);
1267 /* Resolve a procedure expression, like passing it to a called procedure or as
1268 RHS for a procedure pointer assignment. */
1271 resolve_procedure_expression (gfc_expr* expr)
1275 if (expr->expr_type != EXPR_VARIABLE)
1277 gcc_assert (expr->symtree);
1279 sym = expr->symtree->n.sym;
1281 if (sym->attr.intrinsic)
1282 resolve_intrinsic (sym, &expr->where);
1284 if (sym->attr.flavor != FL_PROCEDURE
1285 || (sym->attr.function && sym->result == sym))
1288 /* A non-RECURSIVE procedure that is used as procedure expression within its
1289 own body is in danger of being called recursively. */
1290 if (is_illegal_recursion (sym, gfc_current_ns))
1291 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1292 " itself recursively. Declare it RECURSIVE or use"
1293 " -frecursive", sym->name, &expr->where);
1299 /* Resolve an actual argument list. Most of the time, this is just
1300 resolving the expressions in the list.
1301 The exception is that we sometimes have to decide whether arguments
1302 that look like procedure arguments are really simple variable
1306 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1307 bool no_formal_args)
1310 gfc_symtree *parent_st;
1312 int save_need_full_assumed_size;
1313 gfc_component *comp;
1315 for (; arg; arg = arg->next)
1320 /* Check the label is a valid branching target. */
1323 if (arg->label->defined == ST_LABEL_UNKNOWN)
1325 gfc_error ("Label %d referenced at %L is never defined",
1326 arg->label->value, &arg->label->where);
1333 if (gfc_is_proc_ptr_comp (e, &comp))
1336 if (e->expr_type == EXPR_PPC)
1338 if (comp->as != NULL)
1339 e->rank = comp->as->rank;
1340 e->expr_type = EXPR_FUNCTION;
1342 if (gfc_resolve_expr (e) == FAILURE)
1347 if (e->expr_type == EXPR_VARIABLE
1348 && e->symtree->n.sym->attr.generic
1350 && count_specific_procs (e) != 1)
1353 if (e->ts.type != BT_PROCEDURE)
1355 save_need_full_assumed_size = need_full_assumed_size;
1356 if (e->expr_type != EXPR_VARIABLE)
1357 need_full_assumed_size = 0;
1358 if (gfc_resolve_expr (e) != SUCCESS)
1360 need_full_assumed_size = save_need_full_assumed_size;
1364 /* See if the expression node should really be a variable reference. */
1366 sym = e->symtree->n.sym;
1368 if (sym->attr.flavor == FL_PROCEDURE
1369 || sym->attr.intrinsic
1370 || sym->attr.external)
1374 /* If a procedure is not already determined to be something else
1375 check if it is intrinsic. */
1376 if (!sym->attr.intrinsic
1377 && !(sym->attr.external || sym->attr.use_assoc
1378 || sym->attr.if_source == IFSRC_IFBODY)
1379 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1380 sym->attr.intrinsic = 1;
1382 if (sym->attr.proc == PROC_ST_FUNCTION)
1384 gfc_error ("Statement function '%s' at %L is not allowed as an "
1385 "actual argument", sym->name, &e->where);
1388 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1389 sym->attr.subroutine);
1390 if (sym->attr.intrinsic && actual_ok == 0)
1392 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1393 "actual argument", sym->name, &e->where);
1396 if (sym->attr.contained && !sym->attr.use_assoc
1397 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1399 gfc_error ("Internal procedure '%s' is not allowed as an "
1400 "actual argument at %L", sym->name, &e->where);
1403 if (sym->attr.elemental && !sym->attr.intrinsic)
1405 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1406 "allowed as an actual argument at %L", sym->name,
1410 /* Check if a generic interface has a specific procedure
1411 with the same name before emitting an error. */
1412 if (sym->attr.generic && count_specific_procs (e) != 1)
1415 /* Just in case a specific was found for the expression. */
1416 sym = e->symtree->n.sym;
1418 /* If the symbol is the function that names the current (or
1419 parent) scope, then we really have a variable reference. */
1421 if (gfc_is_function_return_value (sym, sym->ns))
1424 /* If all else fails, see if we have a specific intrinsic. */
1425 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1427 gfc_intrinsic_sym *isym;
1429 isym = gfc_find_function (sym->name);
1430 if (isym == NULL || !isym->specific)
1432 gfc_error ("Unable to find a specific INTRINSIC procedure "
1433 "for the reference '%s' at %L", sym->name,
1438 sym->attr.intrinsic = 1;
1439 sym->attr.function = 1;
1442 if (gfc_resolve_expr (e) == FAILURE)
1447 /* See if the name is a module procedure in a parent unit. */
1449 if (was_declared (sym) || sym->ns->parent == NULL)
1452 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1454 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1458 if (parent_st == NULL)
1461 sym = parent_st->n.sym;
1462 e->symtree = parent_st; /* Point to the right thing. */
1464 if (sym->attr.flavor == FL_PROCEDURE
1465 || sym->attr.intrinsic
1466 || sym->attr.external)
1468 if (gfc_resolve_expr (e) == FAILURE)
1474 e->expr_type = EXPR_VARIABLE;
1476 if (sym->as != NULL)
1478 e->rank = sym->as->rank;
1479 e->ref = gfc_get_ref ();
1480 e->ref->type = REF_ARRAY;
1481 e->ref->u.ar.type = AR_FULL;
1482 e->ref->u.ar.as = sym->as;
1485 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1486 primary.c (match_actual_arg). If above code determines that it
1487 is a variable instead, it needs to be resolved as it was not
1488 done at the beginning of this function. */
1489 save_need_full_assumed_size = need_full_assumed_size;
1490 if (e->expr_type != EXPR_VARIABLE)
1491 need_full_assumed_size = 0;
1492 if (gfc_resolve_expr (e) != SUCCESS)
1494 need_full_assumed_size = save_need_full_assumed_size;
1497 /* Check argument list functions %VAL, %LOC and %REF. There is
1498 nothing to do for %REF. */
1499 if (arg->name && arg->name[0] == '%')
1501 if (strncmp ("%VAL", arg->name, 4) == 0)
1503 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1505 gfc_error ("By-value argument at %L is not of numeric "
1512 gfc_error ("By-value argument at %L cannot be an array or "
1513 "an array section", &e->where);
1517 /* Intrinsics are still PROC_UNKNOWN here. However,
1518 since same file external procedures are not resolvable
1519 in gfortran, it is a good deal easier to leave them to
1521 if (ptype != PROC_UNKNOWN
1522 && ptype != PROC_DUMMY
1523 && ptype != PROC_EXTERNAL
1524 && ptype != PROC_MODULE)
1526 gfc_error ("By-value argument at %L is not allowed "
1527 "in this context", &e->where);
1532 /* Statement functions have already been excluded above. */
1533 else if (strncmp ("%LOC", arg->name, 4) == 0
1534 && e->ts.type == BT_PROCEDURE)
1536 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1538 gfc_error ("Passing internal procedure at %L by location "
1539 "not allowed", &e->where);
1550 /* Do the checks of the actual argument list that are specific to elemental
1551 procedures. If called with c == NULL, we have a function, otherwise if
1552 expr == NULL, we have a subroutine. */
1555 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1557 gfc_actual_arglist *arg0;
1558 gfc_actual_arglist *arg;
1559 gfc_symbol *esym = NULL;
1560 gfc_intrinsic_sym *isym = NULL;
1562 gfc_intrinsic_arg *iformal = NULL;
1563 gfc_formal_arglist *eformal = NULL;
1564 bool formal_optional = false;
1565 bool set_by_optional = false;
1569 /* Is this an elemental procedure? */
1570 if (expr && expr->value.function.actual != NULL)
1572 if (expr->value.function.esym != NULL
1573 && expr->value.function.esym->attr.elemental)
1575 arg0 = expr->value.function.actual;
1576 esym = expr->value.function.esym;
1578 else if (expr->value.function.isym != NULL
1579 && expr->value.function.isym->elemental)
1581 arg0 = expr->value.function.actual;
1582 isym = expr->value.function.isym;
1587 else if (c && c->ext.actual != NULL)
1589 arg0 = c->ext.actual;
1591 if (c->resolved_sym)
1592 esym = c->resolved_sym;
1594 esym = c->symtree->n.sym;
1597 if (!esym->attr.elemental)
1603 /* The rank of an elemental is the rank of its array argument(s). */
1604 for (arg = arg0; arg; arg = arg->next)
1606 if (arg->expr != NULL && arg->expr->rank > 0)
1608 rank = arg->expr->rank;
1609 if (arg->expr->expr_type == EXPR_VARIABLE
1610 && arg->expr->symtree->n.sym->attr.optional)
1611 set_by_optional = true;
1613 /* Function specific; set the result rank and shape. */
1617 if (!expr->shape && arg->expr->shape)
1619 expr->shape = gfc_get_shape (rank);
1620 for (i = 0; i < rank; i++)
1621 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1628 /* If it is an array, it shall not be supplied as an actual argument
1629 to an elemental procedure unless an array of the same rank is supplied
1630 as an actual argument corresponding to a nonoptional dummy argument of
1631 that elemental procedure(12.4.1.5). */
1632 formal_optional = false;
1634 iformal = isym->formal;
1636 eformal = esym->formal;
1638 for (arg = arg0; arg; arg = arg->next)
1642 if (eformal->sym && eformal->sym->attr.optional)
1643 formal_optional = true;
1644 eformal = eformal->next;
1646 else if (isym && iformal)
1648 if (iformal->optional)
1649 formal_optional = true;
1650 iformal = iformal->next;
1653 formal_optional = true;
1655 if (pedantic && arg->expr != NULL
1656 && arg->expr->expr_type == EXPR_VARIABLE
1657 && arg->expr->symtree->n.sym->attr.optional
1660 && (set_by_optional || arg->expr->rank != rank)
1661 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1663 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1664 "MISSING, it cannot be the actual argument of an "
1665 "ELEMENTAL procedure unless there is a non-optional "
1666 "argument with the same rank (12.4.1.5)",
1667 arg->expr->symtree->n.sym->name, &arg->expr->where);
1672 for (arg = arg0; arg; arg = arg->next)
1674 if (arg->expr == NULL || arg->expr->rank == 0)
1677 /* Being elemental, the last upper bound of an assumed size array
1678 argument must be present. */
1679 if (resolve_assumed_size_actual (arg->expr))
1682 /* Elemental procedure's array actual arguments must conform. */
1685 if (gfc_check_conformance (arg->expr, e,
1686 "elemental procedure") == FAILURE)
1693 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1694 is an array, the intent inout/out variable needs to be also an array. */
1695 if (rank > 0 && esym && expr == NULL)
1696 for (eformal = esym->formal, arg = arg0; arg && eformal;
1697 arg = arg->next, eformal = eformal->next)
1698 if ((eformal->sym->attr.intent == INTENT_OUT
1699 || eformal->sym->attr.intent == INTENT_INOUT)
1700 && arg->expr && arg->expr->rank == 0)
1702 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1703 "ELEMENTAL subroutine '%s' is a scalar, but another "
1704 "actual argument is an array", &arg->expr->where,
1705 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1706 : "INOUT", eformal->sym->name, esym->name);
1713 /* Go through each actual argument in ACTUAL and see if it can be
1714 implemented as an inlined, non-copying intrinsic. FNSYM is the
1715 function being called, or NULL if not known. */
1718 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1720 gfc_actual_arglist *ap;
1723 for (ap = actual; ap; ap = ap->next)
1725 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1726 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1728 ap->expr->inline_noncopying_intrinsic = 1;
1732 /* This function does the checking of references to global procedures
1733 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1734 77 and 95 standards. It checks for a gsymbol for the name, making
1735 one if it does not already exist. If it already exists, then the
1736 reference being resolved must correspond to the type of gsymbol.
1737 Otherwise, the new symbol is equipped with the attributes of the
1738 reference. The corresponding code that is called in creating
1739 global entities is parse.c.
1741 In addition, for all but -std=legacy, the gsymbols are used to
1742 check the interfaces of external procedures from the same file.
1743 The namespace of the gsymbol is resolved and then, once this is
1744 done the interface is checked. */
1748 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1750 if (!gsym_ns->proc_name->attr.recursive)
1753 if (sym->ns == gsym_ns)
1756 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1763 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1765 if (gsym_ns->entries)
1767 gfc_entry_list *entry = gsym_ns->entries;
1769 for (; entry; entry = entry->next)
1771 if (strcmp (sym->name, entry->sym->name) == 0)
1773 if (strcmp (gsym_ns->proc_name->name,
1774 sym->ns->proc_name->name) == 0)
1778 && strcmp (gsym_ns->proc_name->name,
1779 sym->ns->parent->proc_name->name) == 0)
1788 resolve_global_procedure (gfc_symbol *sym, locus *where,
1789 gfc_actual_arglist **actual, int sub)
1793 enum gfc_symbol_type type;
1795 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1797 gsym = gfc_get_gsymbol (sym->name);
1799 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1800 gfc_global_used (gsym, where);
1802 if (gfc_option.flag_whole_file
1803 && sym->attr.if_source == IFSRC_UNKNOWN
1804 && gsym->type != GSYM_UNKNOWN
1806 && gsym->ns->resolved != -1
1807 && gsym->ns->proc_name
1808 && not_in_recursive (sym, gsym->ns)
1809 && not_entry_self_reference (sym, gsym->ns))
1811 /* Make sure that translation for the gsymbol occurs before
1812 the procedure currently being resolved. */
1813 ns = gsym->ns->resolved ? NULL : gfc_global_ns_list;
1814 for (; ns && ns != gsym->ns; ns = ns->sibling)
1816 if (ns->sibling == gsym->ns)
1818 ns->sibling = gsym->ns->sibling;
1819 gsym->ns->sibling = gfc_global_ns_list;
1820 gfc_global_ns_list = gsym->ns;
1825 if (!gsym->ns->resolved)
1827 gfc_dt_list *old_dt_list;
1829 /* Stash away derived types so that the backend_decls do not
1831 old_dt_list = gfc_derived_types;
1832 gfc_derived_types = NULL;
1834 gfc_resolve (gsym->ns);
1836 /* Store the new derived types with the global namespace. */
1837 if (gfc_derived_types)
1838 gsym->ns->derived_types = gfc_derived_types;
1840 /* Restore the derived types of this namespace. */
1841 gfc_derived_types = old_dt_list;
1844 if (gsym->ns->proc_name->attr.function
1845 && gsym->ns->proc_name->as
1846 && gsym->ns->proc_name->as->rank
1847 && (!sym->as || sym->as->rank != gsym->ns->proc_name->as->rank))
1848 gfc_error ("The reference to function '%s' at %L either needs an "
1849 "explicit INTERFACE or the rank is incorrect", sym->name,
1852 /* Non-assumed length character functions. */
1853 if (sym->attr.function && sym->ts.type == BT_CHARACTER
1854 && gsym->ns->proc_name->ts.u.cl != NULL
1855 && gsym->ns->proc_name->ts.u.cl->length != NULL)
1857 gfc_charlen *cl = sym->ts.u.cl;
1859 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
1860 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
1862 gfc_error ("Nonconstant character-length function '%s' at %L "
1863 "must have an explicit interface", sym->name,
1868 if (gfc_option.flag_whole_file == 1
1869 || ((gfc_option.warn_std & GFC_STD_LEGACY)
1871 !(gfc_option.warn_std & GFC_STD_GNU)))
1872 gfc_errors_to_warnings (1);
1874 gfc_procedure_use (gsym->ns->proc_name, actual, where);
1876 gfc_errors_to_warnings (0);
1879 if (gsym->type == GSYM_UNKNOWN)
1882 gsym->where = *where;
1889 /************* Function resolution *************/
1891 /* Resolve a function call known to be generic.
1892 Section 14.1.2.4.1. */
1895 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1899 if (sym->attr.generic)
1901 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1904 expr->value.function.name = s->name;
1905 expr->value.function.esym = s;
1907 if (s->ts.type != BT_UNKNOWN)
1909 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1910 expr->ts = s->result->ts;
1913 expr->rank = s->as->rank;
1914 else if (s->result != NULL && s->result->as != NULL)
1915 expr->rank = s->result->as->rank;
1917 gfc_set_sym_referenced (expr->value.function.esym);
1922 /* TODO: Need to search for elemental references in generic
1926 if (sym->attr.intrinsic)
1927 return gfc_intrinsic_func_interface (expr, 0);
1934 resolve_generic_f (gfc_expr *expr)
1939 sym = expr->symtree->n.sym;
1943 m = resolve_generic_f0 (expr, sym);
1946 else if (m == MATCH_ERROR)
1950 if (sym->ns->parent == NULL)
1952 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1956 if (!generic_sym (sym))
1960 /* Last ditch attempt. See if the reference is to an intrinsic
1961 that possesses a matching interface. 14.1.2.4 */
1962 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
1964 gfc_error ("There is no specific function for the generic '%s' at %L",
1965 expr->symtree->n.sym->name, &expr->where);
1969 m = gfc_intrinsic_func_interface (expr, 0);
1973 gfc_error ("Generic function '%s' at %L is not consistent with a "
1974 "specific intrinsic interface", expr->symtree->n.sym->name,
1981 /* Resolve a function call known to be specific. */
1984 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
1988 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1990 if (sym->attr.dummy)
1992 sym->attr.proc = PROC_DUMMY;
1996 sym->attr.proc = PROC_EXTERNAL;
2000 if (sym->attr.proc == PROC_MODULE
2001 || sym->attr.proc == PROC_ST_FUNCTION
2002 || sym->attr.proc == PROC_INTERNAL)
2005 if (sym->attr.intrinsic)
2007 m = gfc_intrinsic_func_interface (expr, 1);
2011 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2012 "with an intrinsic", sym->name, &expr->where);
2020 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2023 expr->ts = sym->result->ts;
2026 expr->value.function.name = sym->name;
2027 expr->value.function.esym = sym;
2028 if (sym->as != NULL)
2029 expr->rank = sym->as->rank;
2036 resolve_specific_f (gfc_expr *expr)
2041 sym = expr->symtree->n.sym;
2045 m = resolve_specific_f0 (sym, expr);
2048 if (m == MATCH_ERROR)
2051 if (sym->ns->parent == NULL)
2054 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2060 gfc_error ("Unable to resolve the specific function '%s' at %L",
2061 expr->symtree->n.sym->name, &expr->where);
2067 /* Resolve a procedure call not known to be generic nor specific. */
2070 resolve_unknown_f (gfc_expr *expr)
2075 sym = expr->symtree->n.sym;
2077 if (sym->attr.dummy)
2079 sym->attr.proc = PROC_DUMMY;
2080 expr->value.function.name = sym->name;
2084 /* See if we have an intrinsic function reference. */
2086 if (gfc_is_intrinsic (sym, 0, expr->where))
2088 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2093 /* The reference is to an external name. */
2095 sym->attr.proc = PROC_EXTERNAL;
2096 expr->value.function.name = sym->name;
2097 expr->value.function.esym = expr->symtree->n.sym;
2099 if (sym->as != NULL)
2100 expr->rank = sym->as->rank;
2102 /* Type of the expression is either the type of the symbol or the
2103 default type of the symbol. */
2106 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2108 if (sym->ts.type != BT_UNKNOWN)
2112 ts = gfc_get_default_type (sym->name, sym->ns);
2114 if (ts->type == BT_UNKNOWN)
2116 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2117 sym->name, &expr->where);
2128 /* Return true, if the symbol is an external procedure. */
2130 is_external_proc (gfc_symbol *sym)
2132 if (!sym->attr.dummy && !sym->attr.contained
2133 && !(sym->attr.intrinsic
2134 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2135 && sym->attr.proc != PROC_ST_FUNCTION
2136 && !sym->attr.use_assoc
2144 /* Figure out if a function reference is pure or not. Also set the name
2145 of the function for a potential error message. Return nonzero if the
2146 function is PURE, zero if not. */
2148 pure_stmt_function (gfc_expr *, gfc_symbol *);
2151 pure_function (gfc_expr *e, const char **name)
2157 if (e->symtree != NULL
2158 && e->symtree->n.sym != NULL
2159 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2160 return pure_stmt_function (e, e->symtree->n.sym);
2162 if (e->value.function.esym)
2164 pure = gfc_pure (e->value.function.esym);
2165 *name = e->value.function.esym->name;
2167 else if (e->value.function.isym)
2169 pure = e->value.function.isym->pure
2170 || e->value.function.isym->elemental;
2171 *name = e->value.function.isym->name;
2175 /* Implicit functions are not pure. */
2177 *name = e->value.function.name;
2185 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2186 int *f ATTRIBUTE_UNUSED)
2190 /* Don't bother recursing into other statement functions
2191 since they will be checked individually for purity. */
2192 if (e->expr_type != EXPR_FUNCTION
2194 || e->symtree->n.sym == sym
2195 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2198 return pure_function (e, &name) ? false : true;
2203 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2205 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2210 is_scalar_expr_ptr (gfc_expr *expr)
2212 gfc_try retval = SUCCESS;
2217 /* See if we have a gfc_ref, which means we have a substring, array
2218 reference, or a component. */
2219 if (expr->ref != NULL)
2222 while (ref->next != NULL)
2228 if (ref->u.ss.length != NULL
2229 && ref->u.ss.length->length != NULL
2231 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2233 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2235 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2236 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2237 if (end - start + 1 != 1)
2244 if (ref->u.ar.type == AR_ELEMENT)
2246 else if (ref->u.ar.type == AR_FULL)
2248 /* The user can give a full array if the array is of size 1. */
2249 if (ref->u.ar.as != NULL
2250 && ref->u.ar.as->rank == 1
2251 && ref->u.ar.as->type == AS_EXPLICIT
2252 && ref->u.ar.as->lower[0] != NULL
2253 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2254 && ref->u.ar.as->upper[0] != NULL
2255 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2257 /* If we have a character string, we need to check if
2258 its length is one. */
2259 if (expr->ts.type == BT_CHARACTER)
2261 if (expr->ts.u.cl == NULL
2262 || expr->ts.u.cl->length == NULL
2263 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2269 /* We have constant lower and upper bounds. If the
2270 difference between is 1, it can be considered a
2272 start = (int) mpz_get_si
2273 (ref->u.ar.as->lower[0]->value.integer);
2274 end = (int) mpz_get_si
2275 (ref->u.ar.as->upper[0]->value.integer);
2276 if (end - start + 1 != 1)
2291 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2293 /* Character string. Make sure it's of length 1. */
2294 if (expr->ts.u.cl == NULL
2295 || expr->ts.u.cl->length == NULL
2296 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2299 else if (expr->rank != 0)
2306 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2307 and, in the case of c_associated, set the binding label based on
2311 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2312 gfc_symbol **new_sym)
2314 char name[GFC_MAX_SYMBOL_LEN + 1];
2315 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2316 int optional_arg = 0, is_pointer = 0;
2317 gfc_try retval = SUCCESS;
2318 gfc_symbol *args_sym;
2319 gfc_typespec *arg_ts;
2321 if (args->expr->expr_type == EXPR_CONSTANT
2322 || args->expr->expr_type == EXPR_OP
2323 || args->expr->expr_type == EXPR_NULL)
2325 gfc_error ("Argument to '%s' at %L is not a variable",
2326 sym->name, &(args->expr->where));
2330 args_sym = args->expr->symtree->n.sym;
2332 /* The typespec for the actual arg should be that stored in the expr
2333 and not necessarily that of the expr symbol (args_sym), because
2334 the actual expression could be a part-ref of the expr symbol. */
2335 arg_ts = &(args->expr->ts);
2337 is_pointer = gfc_is_data_pointer (args->expr);
2339 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2341 /* If the user gave two args then they are providing something for
2342 the optional arg (the second cptr). Therefore, set the name and
2343 binding label to the c_associated for two cptrs. Otherwise,
2344 set c_associated to expect one cptr. */
2348 sprintf (name, "%s_2", sym->name);
2349 sprintf (binding_label, "%s_2", sym->binding_label);
2355 sprintf (name, "%s_1", sym->name);
2356 sprintf (binding_label, "%s_1", sym->binding_label);
2360 /* Get a new symbol for the version of c_associated that
2362 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2364 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2365 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2367 sprintf (name, "%s", sym->name);
2368 sprintf (binding_label, "%s", sym->binding_label);
2370 /* Error check the call. */
2371 if (args->next != NULL)
2373 gfc_error_now ("More actual than formal arguments in '%s' "
2374 "call at %L", name, &(args->expr->where));
2377 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2379 /* Make sure we have either the target or pointer attribute. */
2380 if (!args_sym->attr.target && !is_pointer)
2382 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2383 "a TARGET or an associated pointer",
2385 sym->name, &(args->expr->where));
2389 /* See if we have interoperable type and type param. */
2390 if (verify_c_interop (arg_ts) == SUCCESS
2391 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2393 if (args_sym->attr.target == 1)
2395 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2396 has the target attribute and is interoperable. */
2397 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2398 allocatable variable that has the TARGET attribute and
2399 is not an array of zero size. */
2400 if (args_sym->attr.allocatable == 1)
2402 if (args_sym->attr.dimension != 0
2403 && (args_sym->as && args_sym->as->rank == 0))
2405 gfc_error_now ("Allocatable variable '%s' used as a "
2406 "parameter to '%s' at %L must not be "
2407 "an array of zero size",
2408 args_sym->name, sym->name,
2409 &(args->expr->where));
2415 /* A non-allocatable target variable with C
2416 interoperable type and type parameters must be
2418 if (args_sym && args_sym->attr.dimension)
2420 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2422 gfc_error ("Assumed-shape array '%s' at %L "
2423 "cannot be an argument to the "
2424 "procedure '%s' because "
2425 "it is not C interoperable",
2427 &(args->expr->where), sym->name);
2430 else if (args_sym->as->type == AS_DEFERRED)
2432 gfc_error ("Deferred-shape array '%s' at %L "
2433 "cannot be an argument to the "
2434 "procedure '%s' because "
2435 "it is not C interoperable",
2437 &(args->expr->where), sym->name);
2442 /* Make sure it's not a character string. Arrays of
2443 any type should be ok if the variable is of a C
2444 interoperable type. */
2445 if (arg_ts->type == BT_CHARACTER)
2446 if (arg_ts->u.cl != NULL
2447 && (arg_ts->u.cl->length == NULL
2448 || arg_ts->u.cl->length->expr_type
2451 (arg_ts->u.cl->length->value.integer, 1)
2453 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2455 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2456 "at %L must have a length of 1",
2457 args_sym->name, sym->name,
2458 &(args->expr->where));
2464 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2466 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2468 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2469 "associated scalar POINTER", args_sym->name,
2470 sym->name, &(args->expr->where));
2476 /* The parameter is not required to be C interoperable. If it
2477 is not C interoperable, it must be a nonpolymorphic scalar
2478 with no length type parameters. It still must have either
2479 the pointer or target attribute, and it can be
2480 allocatable (but must be allocated when c_loc is called). */
2481 if (args->expr->rank != 0
2482 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2484 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2485 "scalar", args_sym->name, sym->name,
2486 &(args->expr->where));
2489 else if (arg_ts->type == BT_CHARACTER
2490 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2492 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2493 "%L must have a length of 1",
2494 args_sym->name, sym->name,
2495 &(args->expr->where));
2500 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2502 if (args_sym->attr.flavor != FL_PROCEDURE)
2504 /* TODO: Update this error message to allow for procedure
2505 pointers once they are implemented. */
2506 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2508 args_sym->name, sym->name,
2509 &(args->expr->where));
2512 else if (args_sym->attr.is_bind_c != 1)
2514 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2516 args_sym->name, sym->name,
2517 &(args->expr->where));
2522 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2527 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2528 "iso_c_binding function: '%s'!\n", sym->name);
2535 /* Resolve a function call, which means resolving the arguments, then figuring
2536 out which entity the name refers to. */
2537 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2538 to INTENT(OUT) or INTENT(INOUT). */
2541 resolve_function (gfc_expr *expr)
2543 gfc_actual_arglist *arg;
2548 procedure_type p = PROC_INTRINSIC;
2549 bool no_formal_args;
2553 sym = expr->symtree->n.sym;
2555 /* If this is a procedure pointer component, it has already been resolved. */
2556 if (gfc_is_proc_ptr_comp (expr, NULL))
2559 if (sym && sym->attr.intrinsic
2560 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2563 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2565 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2569 /* If this ia a deferred TBP with an abstract interface (which may
2570 of course be referenced), expr->value.function.esym will be set. */
2571 if (sym && sym->attr.abstract && !expr->value.function.esym)
2573 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2574 sym->name, &expr->where);
2578 /* Switch off assumed size checking and do this again for certain kinds
2579 of procedure, once the procedure itself is resolved. */
2580 need_full_assumed_size++;
2582 if (expr->symtree && expr->symtree->n.sym)
2583 p = expr->symtree->n.sym->attr.proc;
2585 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2586 if (resolve_actual_arglist (expr->value.function.actual,
2587 p, no_formal_args) == FAILURE)
2590 /* Need to setup the call to the correct c_associated, depending on
2591 the number of cptrs to user gives to compare. */
2592 if (sym && sym->attr.is_iso_c == 1)
2594 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2598 /* Get the symtree for the new symbol (resolved func).
2599 the old one will be freed later, when it's no longer used. */
2600 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2603 /* Resume assumed_size checking. */
2604 need_full_assumed_size--;
2606 /* If the procedure is external, check for usage. */
2607 if (sym && is_external_proc (sym))
2608 resolve_global_procedure (sym, &expr->where,
2609 &expr->value.function.actual, 0);
2611 if (sym && sym->ts.type == BT_CHARACTER
2613 && sym->ts.u.cl->length == NULL
2615 && expr->value.function.esym == NULL
2616 && !sym->attr.contained)
2618 /* Internal procedures are taken care of in resolve_contained_fntype. */
2619 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2620 "be used at %L since it is not a dummy argument",
2621 sym->name, &expr->where);
2625 /* See if function is already resolved. */
2627 if (expr->value.function.name != NULL)
2629 if (expr->ts.type == BT_UNKNOWN)
2635 /* Apply the rules of section 14.1.2. */
2637 switch (procedure_kind (sym))
2640 t = resolve_generic_f (expr);
2643 case PTYPE_SPECIFIC:
2644 t = resolve_specific_f (expr);
2648 t = resolve_unknown_f (expr);
2652 gfc_internal_error ("resolve_function(): bad function type");
2656 /* If the expression is still a function (it might have simplified),
2657 then we check to see if we are calling an elemental function. */
2659 if (expr->expr_type != EXPR_FUNCTION)
2662 temp = need_full_assumed_size;
2663 need_full_assumed_size = 0;
2665 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2668 if (omp_workshare_flag
2669 && expr->value.function.esym
2670 && ! gfc_elemental (expr->value.function.esym))
2672 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2673 "in WORKSHARE construct", expr->value.function.esym->name,
2678 #define GENERIC_ID expr->value.function.isym->id
2679 else if (expr->value.function.actual != NULL
2680 && expr->value.function.isym != NULL
2681 && GENERIC_ID != GFC_ISYM_LBOUND
2682 && GENERIC_ID != GFC_ISYM_LEN
2683 && GENERIC_ID != GFC_ISYM_LOC
2684 && GENERIC_ID != GFC_ISYM_PRESENT)
2686 /* Array intrinsics must also have the last upper bound of an
2687 assumed size array argument. UBOUND and SIZE have to be
2688 excluded from the check if the second argument is anything
2691 for (arg = expr->value.function.actual; arg; arg = arg->next)
2693 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2694 && arg->next != NULL && arg->next->expr)
2696 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2699 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2702 if ((int)mpz_get_si (arg->next->expr->value.integer)
2707 if (arg->expr != NULL
2708 && arg->expr->rank > 0
2709 && resolve_assumed_size_actual (arg->expr))
2715 need_full_assumed_size = temp;
2718 if (!pure_function (expr, &name) && name)
2722 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2723 "FORALL %s", name, &expr->where,
2724 forall_flag == 2 ? "mask" : "block");
2727 else if (gfc_pure (NULL))
2729 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2730 "procedure within a PURE procedure", name, &expr->where);
2735 /* Functions without the RECURSIVE attribution are not allowed to
2736 * call themselves. */
2737 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2740 esym = expr->value.function.esym;
2742 if (is_illegal_recursion (esym, gfc_current_ns))
2744 if (esym->attr.entry && esym->ns->entries)
2745 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2746 " function '%s' is not RECURSIVE",
2747 esym->name, &expr->where, esym->ns->entries->sym->name);
2749 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2750 " is not RECURSIVE", esym->name, &expr->where);
2756 /* Character lengths of use associated functions may contains references to
2757 symbols not referenced from the current program unit otherwise. Make sure
2758 those symbols are marked as referenced. */
2760 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2761 && expr->value.function.esym->attr.use_assoc)
2763 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
2767 && !((expr->value.function.esym
2768 && expr->value.function.esym->attr.elemental)
2770 (expr->value.function.isym
2771 && expr->value.function.isym->elemental)))
2772 find_noncopying_intrinsics (expr->value.function.esym,
2773 expr->value.function.actual);
2775 /* Make sure that the expression has a typespec that works. */
2776 if (expr->ts.type == BT_UNKNOWN)
2778 if (expr->symtree->n.sym->result
2779 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
2780 && !expr->symtree->n.sym->result->attr.proc_pointer)
2781 expr->ts = expr->symtree->n.sym->result->ts;
2788 /************* Subroutine resolution *************/
2791 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2797 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2798 sym->name, &c->loc);
2799 else if (gfc_pure (NULL))
2800 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2806 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2810 if (sym->attr.generic)
2812 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2815 c->resolved_sym = s;
2816 pure_subroutine (c, s);
2820 /* TODO: Need to search for elemental references in generic interface. */
2823 if (sym->attr.intrinsic)
2824 return gfc_intrinsic_sub_interface (c, 0);
2831 resolve_generic_s (gfc_code *c)
2836 sym = c->symtree->n.sym;
2840 m = resolve_generic_s0 (c, sym);
2843 else if (m == MATCH_ERROR)
2847 if (sym->ns->parent == NULL)
2849 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2853 if (!generic_sym (sym))
2857 /* Last ditch attempt. See if the reference is to an intrinsic
2858 that possesses a matching interface. 14.1.2.4 */
2859 sym = c->symtree->n.sym;
2861 if (!gfc_is_intrinsic (sym, 1, c->loc))
2863 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2864 sym->name, &c->loc);
2868 m = gfc_intrinsic_sub_interface (c, 0);
2872 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2873 "intrinsic subroutine interface", sym->name, &c->loc);
2879 /* Set the name and binding label of the subroutine symbol in the call
2880 expression represented by 'c' to include the type and kind of the
2881 second parameter. This function is for resolving the appropriate
2882 version of c_f_pointer() and c_f_procpointer(). For example, a
2883 call to c_f_pointer() for a default integer pointer could have a
2884 name of c_f_pointer_i4. If no second arg exists, which is an error
2885 for these two functions, it defaults to the generic symbol's name
2886 and binding label. */
2889 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2890 char *name, char *binding_label)
2892 gfc_expr *arg = NULL;
2896 /* The second arg of c_f_pointer and c_f_procpointer determines
2897 the type and kind for the procedure name. */
2898 arg = c->ext.actual->next->expr;
2902 /* Set up the name to have the given symbol's name,
2903 plus the type and kind. */
2904 /* a derived type is marked with the type letter 'u' */
2905 if (arg->ts.type == BT_DERIVED)
2908 kind = 0; /* set the kind as 0 for now */
2912 type = gfc_type_letter (arg->ts.type);
2913 kind = arg->ts.kind;
2916 if (arg->ts.type == BT_CHARACTER)
2917 /* Kind info for character strings not needed. */
2920 sprintf (name, "%s_%c%d", sym->name, type, kind);
2921 /* Set up the binding label as the given symbol's label plus
2922 the type and kind. */
2923 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2927 /* If the second arg is missing, set the name and label as
2928 was, cause it should at least be found, and the missing
2929 arg error will be caught by compare_parameters(). */
2930 sprintf (name, "%s", sym->name);
2931 sprintf (binding_label, "%s", sym->binding_label);
2938 /* Resolve a generic version of the iso_c_binding procedure given
2939 (sym) to the specific one based on the type and kind of the
2940 argument(s). Currently, this function resolves c_f_pointer() and
2941 c_f_procpointer based on the type and kind of the second argument
2942 (FPTR). Other iso_c_binding procedures aren't specially handled.
2943 Upon successfully exiting, c->resolved_sym will hold the resolved
2944 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2948 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2950 gfc_symbol *new_sym;
2951 /* this is fine, since we know the names won't use the max */
2952 char name[GFC_MAX_SYMBOL_LEN + 1];
2953 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2954 /* default to success; will override if find error */
2955 match m = MATCH_YES;
2957 /* Make sure the actual arguments are in the necessary order (based on the
2958 formal args) before resolving. */
2959 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2961 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2962 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2964 set_name_and_label (c, sym, name, binding_label);
2966 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2968 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2970 /* Make sure we got a third arg if the second arg has non-zero
2971 rank. We must also check that the type and rank are
2972 correct since we short-circuit this check in
2973 gfc_procedure_use() (called above to sort actual args). */
2974 if (c->ext.actual->next->expr->rank != 0)
2976 if(c->ext.actual->next->next == NULL
2977 || c->ext.actual->next->next->expr == NULL)
2980 gfc_error ("Missing SHAPE parameter for call to %s "
2981 "at %L", sym->name, &(c->loc));
2983 else if (c->ext.actual->next->next->expr->ts.type
2985 || c->ext.actual->next->next->expr->rank != 1)
2988 gfc_error ("SHAPE parameter for call to %s at %L must "
2989 "be a rank 1 INTEGER array", sym->name,
2996 if (m != MATCH_ERROR)
2998 /* the 1 means to add the optional arg to formal list */
2999 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
3001 /* for error reporting, say it's declared where the original was */
3002 new_sym->declared_at = sym->declared_at;
3007 /* no differences for c_loc or c_funloc */
3011 /* set the resolved symbol */
3012 if (m != MATCH_ERROR)
3013 c->resolved_sym = new_sym;
3015 c->resolved_sym = sym;
3021 /* Resolve a subroutine call known to be specific. */
3024 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3028 if(sym->attr.is_iso_c)
3030 m = gfc_iso_c_sub_interface (c,sym);
3034 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3036 if (sym->attr.dummy)
3038 sym->attr.proc = PROC_DUMMY;
3042 sym->attr.proc = PROC_EXTERNAL;
3046 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3049 if (sym->attr.intrinsic)
3051 m = gfc_intrinsic_sub_interface (c, 1);
3055 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3056 "with an intrinsic", sym->name, &c->loc);
3064 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3066 c->resolved_sym = sym;
3067 pure_subroutine (c, sym);
3074 resolve_specific_s (gfc_code *c)
3079 sym = c->symtree->n.sym;
3083 m = resolve_specific_s0 (c, sym);
3086 if (m == MATCH_ERROR)
3089 if (sym->ns->parent == NULL)
3092 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3098 sym = c->symtree->n.sym;
3099 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3100 sym->name, &c->loc);
3106 /* Resolve a subroutine call not known to be generic nor specific. */
3109 resolve_unknown_s (gfc_code *c)
3113 sym = c->symtree->n.sym;
3115 if (sym->attr.dummy)
3117 sym->attr.proc = PROC_DUMMY;
3121 /* See if we have an intrinsic function reference. */
3123 if (gfc_is_intrinsic (sym, 1, c->loc))
3125 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3130 /* The reference is to an external name. */
3133 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3135 c->resolved_sym = sym;
3137 pure_subroutine (c, sym);
3143 /* Resolve a subroutine call. Although it was tempting to use the same code
3144 for functions, subroutines and functions are stored differently and this
3145 makes things awkward. */
3148 resolve_call (gfc_code *c)
3151 procedure_type ptype = PROC_INTRINSIC;
3152 gfc_symbol *csym, *sym;
3153 bool no_formal_args;
3155 csym = c->symtree ? c->symtree->n.sym : NULL;
3157 if (csym && csym->ts.type != BT_UNKNOWN)
3159 gfc_error ("'%s' at %L has a type, which is not consistent with "
3160 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3164 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3167 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3168 sym = st ? st->n.sym : NULL;
3169 if (sym && csym != sym
3170 && sym->ns == gfc_current_ns
3171 && sym->attr.flavor == FL_PROCEDURE
3172 && sym->attr.contained)
3175 if (csym->attr.generic)
3176 c->symtree->n.sym = sym;
3179 csym = c->symtree->n.sym;
3183 /* If this ia a deferred TBP with an abstract interface
3184 (which may of course be referenced), c->expr1 will be set. */
3185 if (csym && csym->attr.abstract && !c->expr1)
3187 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3188 csym->name, &c->loc);
3192 /* Subroutines without the RECURSIVE attribution are not allowed to
3193 * call themselves. */
3194 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3196 if (csym->attr.entry && csym->ns->entries)
3197 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3198 " subroutine '%s' is not RECURSIVE",
3199 csym->name, &c->loc, csym->ns->entries->sym->name);
3201 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3202 " is not RECURSIVE", csym->name, &c->loc);
3207 /* Switch off assumed size checking and do this again for certain kinds
3208 of procedure, once the procedure itself is resolved. */
3209 need_full_assumed_size++;
3212 ptype = csym->attr.proc;
3214 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3215 if (resolve_actual_arglist (c->ext.actual, ptype,
3216 no_formal_args) == FAILURE)
3219 /* Resume assumed_size checking. */
3220 need_full_assumed_size--;
3222 /* If external, check for usage. */
3223 if (csym && is_external_proc (csym))
3224 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3227 if (c->resolved_sym == NULL)
3229 c->resolved_isym = NULL;
3230 switch (procedure_kind (csym))
3233 t = resolve_generic_s (c);
3236 case PTYPE_SPECIFIC:
3237 t = resolve_specific_s (c);
3241 t = resolve_unknown_s (c);
3245 gfc_internal_error ("resolve_subroutine(): bad function type");
3249 /* Some checks of elemental subroutine actual arguments. */
3250 if (resolve_elemental_actual (NULL, c) == FAILURE)
3253 if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
3254 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
3259 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3260 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3261 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3262 if their shapes do not match. If either op1->shape or op2->shape is
3263 NULL, return SUCCESS. */
3266 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3273 if (op1->shape != NULL && op2->shape != NULL)
3275 for (i = 0; i < op1->rank; i++)
3277 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3279 gfc_error ("Shapes for operands at %L and %L are not conformable",
3280 &op1->where, &op2->where);
3291 /* Resolve an operator expression node. This can involve replacing the
3292 operation with a user defined function call. */
3295 resolve_operator (gfc_expr *e)
3297 gfc_expr *op1, *op2;
3299 bool dual_locus_error;
3302 /* Resolve all subnodes-- give them types. */
3304 switch (e->value.op.op)
3307 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3310 /* Fall through... */
3313 case INTRINSIC_UPLUS:
3314 case INTRINSIC_UMINUS:
3315 case INTRINSIC_PARENTHESES:
3316 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3321 /* Typecheck the new node. */
3323 op1 = e->value.op.op1;
3324 op2 = e->value.op.op2;
3325 dual_locus_error = false;
3327 if ((op1 && op1->expr_type == EXPR_NULL)
3328 || (op2 && op2->expr_type == EXPR_NULL))
3330 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3334 switch (e->value.op.op)
3336 case INTRINSIC_UPLUS:
3337 case INTRINSIC_UMINUS:
3338 if (op1->ts.type == BT_INTEGER
3339 || op1->ts.type == BT_REAL
3340 || op1->ts.type == BT_COMPLEX)
3346 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3347 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3350 case INTRINSIC_PLUS:
3351 case INTRINSIC_MINUS:
3352 case INTRINSIC_TIMES:
3353 case INTRINSIC_DIVIDE:
3354 case INTRINSIC_POWER:
3355 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3357 gfc_type_convert_binary (e, 1);
3362 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3363 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3364 gfc_typename (&op2->ts));
3367 case INTRINSIC_CONCAT:
3368 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3369 && op1->ts.kind == op2->ts.kind)
3371 e->ts.type = BT_CHARACTER;
3372 e->ts.kind = op1->ts.kind;
3377 _("Operands of string concatenation operator at %%L are %s/%s"),
3378 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3384 case INTRINSIC_NEQV:
3385 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3387 e->ts.type = BT_LOGICAL;
3388 e->ts.kind = gfc_kind_max (op1, op2);
3389 if (op1->ts.kind < e->ts.kind)
3390 gfc_convert_type (op1, &e->ts, 2);
3391 else if (op2->ts.kind < e->ts.kind)
3392 gfc_convert_type (op2, &e->ts, 2);
3396 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3397 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3398 gfc_typename (&op2->ts));
3403 if (op1->ts.type == BT_LOGICAL)
3405 e->ts.type = BT_LOGICAL;
3406 e->ts.kind = op1->ts.kind;
3410 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3411 gfc_typename (&op1->ts));
3415 case INTRINSIC_GT_OS:
3417 case INTRINSIC_GE_OS:
3419 case INTRINSIC_LT_OS:
3421 case INTRINSIC_LE_OS:
3422 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3424 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3428 /* Fall through... */
3431 case INTRINSIC_EQ_OS:
3433 case INTRINSIC_NE_OS:
3434 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3435 && op1->ts.kind == op2->ts.kind)
3437 e->ts.type = BT_LOGICAL;
3438 e->ts.kind = gfc_default_logical_kind;
3442 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3444 gfc_type_convert_binary (e, 1);
3446 e->ts.type = BT_LOGICAL;
3447 e->ts.kind = gfc_default_logical_kind;
3451 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3453 _("Logicals at %%L must be compared with %s instead of %s"),
3454 (e->value.op.op == INTRINSIC_EQ
3455 || e->value.op.op == INTRINSIC_EQ_OS)
3456 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3459 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3460 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3461 gfc_typename (&op2->ts));
3465 case INTRINSIC_USER:
3466 if (e->value.op.uop->op == NULL)
3467 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3468 else if (op2 == NULL)
3469 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3470 e->value.op.uop->name, gfc_typename (&op1->ts));
3472 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3473 e->value.op.uop->name, gfc_typename (&op1->ts),
3474 gfc_typename (&op2->ts));
3478 case INTRINSIC_PARENTHESES:
3480 if (e->ts.type == BT_CHARACTER)
3481 e->ts.u.cl = op1->ts.u.cl;
3485 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3488 /* Deal with arrayness of an operand through an operator. */
3492 switch (e->value.op.op)
3494 case INTRINSIC_PLUS:
3495 case INTRINSIC_MINUS:
3496 case INTRINSIC_TIMES:
3497 case INTRINSIC_DIVIDE:
3498 case INTRINSIC_POWER:
3499 case INTRINSIC_CONCAT:
3503 case INTRINSIC_NEQV:
3505 case INTRINSIC_EQ_OS:
3507 case INTRINSIC_NE_OS:
3509 case INTRINSIC_GT_OS:
3511 case INTRINSIC_GE_OS:
3513 case INTRINSIC_LT_OS:
3515 case INTRINSIC_LE_OS:
3517 if (op1->rank == 0 && op2->rank == 0)
3520 if (op1->rank == 0 && op2->rank != 0)
3522 e->rank = op2->rank;
3524 if (e->shape == NULL)
3525 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3528 if (op1->rank != 0 && op2->rank == 0)
3530 e->rank = op1->rank;
3532 if (e->shape == NULL)
3533 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3536 if (op1->rank != 0 && op2->rank != 0)
3538 if (op1->rank == op2->rank)
3540 e->rank = op1->rank;
3541 if (e->shape == NULL)
3543 t = compare_shapes(op1, op2);
3547 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3552 /* Allow higher level expressions to work. */
3555 /* Try user-defined operators, and otherwise throw an error. */
3556 dual_locus_error = true;
3558 _("Inconsistent ranks for operator at %%L and %%L"));
3565 case INTRINSIC_PARENTHESES:
3567 case INTRINSIC_UPLUS:
3568 case INTRINSIC_UMINUS:
3569 /* Simply copy arrayness attribute */
3570 e->rank = op1->rank;
3572 if (e->shape == NULL)
3573 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3581 /* Attempt to simplify the expression. */
3584 t = gfc_simplify_expr (e, 0);
3585 /* Some calls do not succeed in simplification and return FAILURE
3586 even though there is no error; e.g. variable references to
3587 PARAMETER arrays. */
3588 if (!gfc_is_constant_expr (e))
3597 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3604 if (dual_locus_error)
3605 gfc_error (msg, &op1->where, &op2->where);
3607 gfc_error (msg, &e->where);
3613 /************** Array resolution subroutines **************/
3616 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3619 /* Compare two integer expressions. */
3622 compare_bound (gfc_expr *a, gfc_expr *b)
3626 if (a == NULL || a->expr_type != EXPR_CONSTANT
3627 || b == NULL || b->expr_type != EXPR_CONSTANT)
3630 /* If either of the types isn't INTEGER, we must have
3631 raised an error earlier. */
3633 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3636 i = mpz_cmp (a->value.integer, b->value.integer);
3646 /* Compare an integer expression with an integer. */
3649 compare_bound_int (gfc_expr *a, int b)
3653 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3656 if (a->ts.type != BT_INTEGER)
3657 gfc_internal_error ("compare_bound_int(): Bad expression");
3659 i = mpz_cmp_si (a->value.integer, b);
3669 /* Compare an integer expression with a mpz_t. */
3672 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3676 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3679 if (a->ts.type != BT_INTEGER)
3680 gfc_internal_error ("compare_bound_int(): Bad expression");
3682 i = mpz_cmp (a->value.integer, b);
3692 /* Compute the last value of a sequence given by a triplet.
3693 Return 0 if it wasn't able to compute the last value, or if the
3694 sequence if empty, and 1 otherwise. */
3697 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3698 gfc_expr *stride, mpz_t last)
3702 if (start == NULL || start->expr_type != EXPR_CONSTANT
3703 || end == NULL || end->expr_type != EXPR_CONSTANT
3704 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3707 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3708 || (stride != NULL && stride->ts.type != BT_INTEGER))
3711 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3713 if (compare_bound (start, end) == CMP_GT)
3715 mpz_set (last, end->value.integer);
3719 if (compare_bound_int (stride, 0) == CMP_GT)
3721 /* Stride is positive */
3722 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3727 /* Stride is negative */
3728 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3733 mpz_sub (rem, end->value.integer, start->value.integer);
3734 mpz_tdiv_r (rem, rem, stride->value.integer);
3735 mpz_sub (last, end->value.integer, rem);
3742 /* Compare a single dimension of an array reference to the array
3746 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3750 /* Given start, end and stride values, calculate the minimum and
3751 maximum referenced indexes. */
3753 switch (ar->dimen_type[i])
3759 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3761 gfc_warning ("Array reference at %L is out of bounds "
3762 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3763 mpz_get_si (ar->start[i]->value.integer),
3764 mpz_get_si (as->lower[i]->value.integer), i+1);
3767 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3769 gfc_warning ("Array reference at %L is out of bounds "
3770 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3771 mpz_get_si (ar->start[i]->value.integer),
3772 mpz_get_si (as->upper[i]->value.integer), i+1);
3780 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3781 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3783 comparison comp_start_end = compare_bound (AR_START, AR_END);
3785 /* Check for zero stride, which is not allowed. */
3786 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3788 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3792 /* if start == len || (stride > 0 && start < len)
3793 || (stride < 0 && start > len),
3794 then the array section contains at least one element. In this
3795 case, there is an out-of-bounds access if
3796 (start < lower || start > upper). */
3797 if (compare_bound (AR_START, AR_END) == CMP_EQ
3798 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3799 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3800 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3801 && comp_start_end == CMP_GT))
3803 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3805 gfc_warning ("Lower array reference at %L is out of bounds "
3806 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3807 mpz_get_si (AR_START->value.integer),
3808 mpz_get_si (as->lower[i]->value.integer), i+1);
3811 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3813 gfc_warning ("Lower array reference at %L is out of bounds "
3814 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3815 mpz_get_si (AR_START->value.integer),
3816 mpz_get_si (as->upper[i]->value.integer), i+1);
3821 /* If we can compute the highest index of the array section,
3822 then it also has to be between lower and upper. */
3823 mpz_init (last_value);
3824 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3827 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3829 gfc_warning ("Upper array reference at %L is out of bounds "
3830 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3831 mpz_get_si (last_value),
3832 mpz_get_si (as->lower[i]->value.integer), i+1);
3833 mpz_clear (last_value);
3836 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3838 gfc_warning ("Upper array reference at %L is out of bounds "
3839 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3840 mpz_get_si (last_value),
3841 mpz_get_si (as->upper[i]->value.integer), i+1);
3842 mpz_clear (last_value);
3846 mpz_clear (last_value);
3854 gfc_internal_error ("check_dimension(): Bad array reference");
3861 /* Compare an array reference with an array specification. */
3864 compare_spec_to_ref (gfc_array_ref *ar)
3871 /* TODO: Full array sections are only allowed as actual parameters. */
3872 if (as->type == AS_ASSUMED_SIZE
3873 && (/*ar->type == AR_FULL
3874 ||*/ (ar->type == AR_SECTION
3875 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3877 gfc_error ("Rightmost upper bound of assumed size array section "
3878 "not specified at %L", &ar->where);
3882 if (ar->type == AR_FULL)
3885 if (as->rank != ar->dimen)
3887 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3888 &ar->where, ar->dimen, as->rank);
3892 for (i = 0; i < as->rank; i++)
3893 if (check_dimension (i, ar, as) == FAILURE)
3900 /* Resolve one part of an array index. */
3903 gfc_resolve_index (gfc_expr *index, int check_scalar)
3910 if (gfc_resolve_expr (index) == FAILURE)
3913 if (check_scalar && index->rank != 0)
3915 gfc_error ("Array index at %L must be scalar", &index->where);
3919 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
3921 gfc_error ("Array index at %L must be of INTEGER type, found %s",
3922 &index->where, gfc_basic_typename (index->ts.type));
3926 if (index->ts.type == BT_REAL)
3927 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
3928 &index->where) == FAILURE)
3931 if (index->ts.kind != gfc_index_integer_kind
3932 || index->ts.type != BT_INTEGER)
3935 ts.type = BT_INTEGER;
3936 ts.kind = gfc_index_integer_kind;
3938 gfc_convert_type_warn (index, &ts, 2, 0);
3944 /* Resolve a dim argument to an intrinsic function. */
3947 gfc_resolve_dim_arg (gfc_expr *dim)
3952 if (gfc_resolve_expr (dim) == FAILURE)
3957 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3962 if (dim->ts.type != BT_INTEGER)
3964 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3968 if (dim->ts.kind != gfc_index_integer_kind)
3973 ts.type = BT_INTEGER;
3974 ts.kind = gfc_index_integer_kind;
3976 gfc_convert_type_warn (dim, &ts, 2, 0);
3982 /* Given an expression that contains array references, update those array
3983 references to point to the right array specifications. While this is
3984 filled in during matching, this information is difficult to save and load
3985 in a module, so we take care of it here.
3987 The idea here is that the original array reference comes from the
3988 base symbol. We traverse the list of reference structures, setting
3989 the stored reference to references. Component references can
3990 provide an additional array specification. */
3993 find_array_spec (gfc_expr *e)
3997 gfc_symbol *derived;
4000 if (e->symtree->n.sym->ts.type == BT_CLASS)
4001 as = e->symtree->n.sym->ts.u.derived->components->as;
4003 as = e->symtree->n.sym->as;
4006 for (ref = e->ref; ref; ref = ref->next)
4011 gfc_internal_error ("find_array_spec(): Missing spec");
4018 if (derived == NULL)
4019 derived = e->symtree->n.sym->ts.u.derived;
4021 if (derived->attr.is_class)
4022 derived = derived->components->ts.u.derived;
4024 c = derived->components;
4026 for (; c; c = c->next)
4027 if (c == ref->u.c.component)
4029 /* Track the sequence of component references. */
4030 if (c->ts.type == BT_DERIVED)
4031 derived = c->ts.u.derived;
4036 gfc_internal_error ("find_array_spec(): Component not found");
4038 if (c->attr.dimension)
4041 gfc_internal_error ("find_array_spec(): unused as(1)");
4052 gfc_internal_error ("find_array_spec(): unused as(2)");
4056 /* Resolve an array reference. */
4059 resolve_array_ref (gfc_array_ref *ar)
4061 int i, check_scalar;
4064 for (i = 0; i < ar->dimen; i++)
4066 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4068 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
4070 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4072 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4077 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4081 ar->dimen_type[i] = DIMEN_ELEMENT;
4085 ar->dimen_type[i] = DIMEN_VECTOR;
4086 if (e->expr_type == EXPR_VARIABLE
4087 && e->symtree->n.sym->ts.type == BT_DERIVED)
4088 ar->start[i] = gfc_get_parentheses (e);
4092 gfc_error ("Array index at %L is an array of rank %d",
4093 &ar->c_where[i], e->rank);
4098 /* If the reference type is unknown, figure out what kind it is. */
4100 if (ar->type == AR_UNKNOWN)
4102 ar->type = AR_ELEMENT;
4103 for (i = 0; i < ar->dimen; i++)
4104 if (ar->dimen_type[i] == DIMEN_RANGE
4105 || ar->dimen_type[i] == DIMEN_VECTOR)
4107 ar->type = AR_SECTION;
4112 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4120 resolve_substring (gfc_ref *ref)
4122 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4124 if (ref->u.ss.start != NULL)
4126 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4129 if (ref->u.ss.start->ts.type != BT_INTEGER)
4131 gfc_error ("Substring start index at %L must be of type INTEGER",
4132 &ref->u.ss.start->where);
4136 if (ref->u.ss.start->rank != 0)
4138 gfc_error ("Substring start index at %L must be scalar",
4139 &ref->u.ss.start->where);
4143 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4144 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4145 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4147 gfc_error ("Substring start index at %L is less than one",
4148 &ref->u.ss.start->where);
4153 if (ref->u.ss.end != NULL)
4155 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4158 if (ref->u.ss.end->ts.type != BT_INTEGER)
4160 gfc_error ("Substring end index at %L must be of type INTEGER",
4161 &ref->u.ss.end->where);
4165 if (ref->u.ss.end->rank != 0)
4167 gfc_error ("Substring end index at %L must be scalar",
4168 &ref->u.ss.end->where);
4172 if (ref->u.ss.length != NULL
4173 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4174 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4175 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4177 gfc_error ("Substring end index at %L exceeds the string length",
4178 &ref->u.ss.start->where);
4182 if (compare_bound_mpz_t (ref->u.ss.end,
4183 gfc_integer_kinds[k].huge) == CMP_GT
4184 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4185 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4187 gfc_error ("Substring end index at %L is too large",
4188 &ref->u.ss.end->where);
4197 /* This function supplies missing substring charlens. */
4200 gfc_resolve_substring_charlen (gfc_expr *e)
4203 gfc_expr *start, *end;
4205 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4206 if (char_ref->type == REF_SUBSTRING)
4212 gcc_assert (char_ref->next == NULL);
4216 if (e->ts.u.cl->length)
4217 gfc_free_expr (e->ts.u.cl->length);
4218 else if (e->expr_type == EXPR_VARIABLE
4219 && e->symtree->n.sym->attr.dummy)
4223 e->ts.type = BT_CHARACTER;
4224 e->ts.kind = gfc_default_character_kind;
4227 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4229 if (char_ref->u.ss.start)
4230 start = gfc_copy_expr (char_ref->u.ss.start);
4232 start = gfc_int_expr (1);
4234 if (char_ref->u.ss.end)
4235 end = gfc_copy_expr (char_ref->u.ss.end);
4236 else if (e->expr_type == EXPR_VARIABLE)
4237 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4244 /* Length = (end - start +1). */
4245 e->ts.u.cl->length = gfc_subtract (end, start);
4246 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length, gfc_int_expr (1));
4248 e->ts.u.cl->length->ts.type = BT_INTEGER;
4249 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4251 /* Make sure that the length is simplified. */
4252 gfc_simplify_expr (e->ts.u.cl->length, 1);
4253 gfc_resolve_expr (e->ts.u.cl->length);
4257 /* Resolve subtype references. */
4260 resolve_ref (gfc_expr *expr)
4262 int current_part_dimension, n_components, seen_part_dimension;
4265 for (ref = expr->ref; ref; ref = ref->next)
4266 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4268 find_array_spec (expr);
4272 for (ref = expr->ref; ref; ref = ref->next)
4276 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4284 resolve_substring (ref);
4288 /* Check constraints on part references. */
4290 current_part_dimension = 0;
4291 seen_part_dimension = 0;
4294 for (ref = expr->ref; ref; ref = ref->next)
4299 switch (ref->u.ar.type)
4303 current_part_dimension = 1;
4307 current_part_dimension = 0;
4311 gfc_internal_error ("resolve_ref(): Bad array reference");
4317 if (current_part_dimension || seen_part_dimension)
4320 if (ref->u.c.component->attr.pointer
4321 || ref->u.c.component->attr.proc_pointer)
4323 gfc_error ("Component to the right of a part reference "
4324 "with nonzero rank must not have the POINTER "
4325 "attribute at %L", &expr->where);
4328 else if (ref->u.c.component->attr.allocatable)
4330 gfc_error ("Component to the right of a part reference "
4331 "with nonzero rank must not have the ALLOCATABLE "
4332 "attribute at %L", &expr->where);
4344 if (((ref->type == REF_COMPONENT && n_components > 1)
4345 || ref->next == NULL)
4346 && current_part_dimension
4347 && seen_part_dimension)
4349 gfc_error ("Two or more part references with nonzero rank must "
4350 "not be specified at %L", &expr->where);
4354 if (ref->type == REF_COMPONENT)
4356 if (current_part_dimension)
4357 seen_part_dimension = 1;
4359 /* reset to make sure */
4360 current_part_dimension = 0;
4368 /* Given an expression, determine its shape. This is easier than it sounds.
4369 Leaves the shape array NULL if it is not possible to determine the shape. */
4372 expression_shape (gfc_expr *e)
4374 mpz_t array[GFC_MAX_DIMENSIONS];
4377 if (e->rank == 0 || e->shape != NULL)
4380 for (i = 0; i < e->rank; i++)
4381 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4384 e->shape = gfc_get_shape (e->rank);
4386 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4391 for (i--; i >= 0; i--)
4392 mpz_clear (array[i]);
4396 /* Given a variable expression node, compute the rank of the expression by
4397 examining the base symbol and any reference structures it may have. */
4400 expression_rank (gfc_expr *e)
4405 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4406 could lead to serious confusion... */
4407 gcc_assert (e->expr_type != EXPR_COMPCALL);
4411 if (e->expr_type == EXPR_ARRAY)
4413 /* Constructors can have a rank different from one via RESHAPE(). */
4415 if (e->symtree == NULL)
4421 e->rank = (e->symtree->n.sym->as == NULL)
4422 ? 0 : e->symtree->n.sym->as->rank;
4428 for (ref = e->ref; ref; ref = ref->next)
4430 if (ref->type != REF_ARRAY)
4433 if (ref->u.ar.type == AR_FULL)
4435 rank = ref->u.ar.as->rank;
4439 if (ref->u.ar.type == AR_SECTION)
4441 /* Figure out the rank of the section. */
4443 gfc_internal_error ("expression_rank(): Two array specs");
4445 for (i = 0; i < ref->u.ar.dimen; i++)
4446 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4447 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4457 expression_shape (e);
4461 /* Resolve a variable expression. */
4464 resolve_variable (gfc_expr *e)
4471 if (e->symtree == NULL)
4474 if (e->ref && resolve_ref (e) == FAILURE)
4477 sym = e->symtree->n.sym;
4478 if (sym->attr.flavor == FL_PROCEDURE
4479 && (!sym->attr.function
4480 || (sym->attr.function && sym->result
4481 && sym->result->attr.proc_pointer
4482 && !sym->result->attr.function)))
4484 e->ts.type = BT_PROCEDURE;
4485 goto resolve_procedure;
4488 if (sym->ts.type != BT_UNKNOWN)
4489 gfc_variable_attr (e, &e->ts);
4492 /* Must be a simple variable reference. */
4493 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4498 if (check_assumed_size_reference (sym, e))
4501 /* Deal with forward references to entries during resolve_code, to
4502 satisfy, at least partially, 12.5.2.5. */
4503 if (gfc_current_ns->entries
4504 && current_entry_id == sym->entry_id
4507 && cs_base->current->op != EXEC_ENTRY)
4509 gfc_entry_list *entry;
4510 gfc_formal_arglist *formal;
4514 /* If the symbol is a dummy... */
4515 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4517 entry = gfc_current_ns->entries;
4520 /* ...test if the symbol is a parameter of previous entries. */
4521 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4522 for (formal = entry->sym->formal; formal; formal = formal->next)
4524 if (formal->sym && sym->name == formal->sym->name)
4528 /* If it has not been seen as a dummy, this is an error. */
4531 if (specification_expr)
4532 gfc_error ("Variable '%s', used in a specification expression"
4533 ", is referenced at %L before the ENTRY statement "
4534 "in which it is a parameter",
4535 sym->name, &cs_base->current->loc);
4537 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4538 "statement in which it is a parameter",
4539 sym->name, &cs_base->current->loc);
4544 /* Now do the same check on the specification expressions. */
4545 specification_expr = 1;
4546 if (sym->ts.type == BT_CHARACTER
4547 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
4551 for (n = 0; n < sym->as->rank; n++)
4553 specification_expr = 1;
4554 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4556 specification_expr = 1;
4557 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4560 specification_expr = 0;
4563 /* Update the symbol's entry level. */
4564 sym->entry_id = current_entry_id + 1;
4568 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
4575 /* Checks to see that the correct symbol has been host associated.
4576 The only situation where this arises is that in which a twice
4577 contained function is parsed after the host association is made.
4578 Therefore, on detecting this, change the symbol in the expression
4579 and convert the array reference into an actual arglist if the old
4580 symbol is a variable. */
4582 check_host_association (gfc_expr *e)
4584 gfc_symbol *sym, *old_sym;
4588 gfc_actual_arglist *arg, *tail = NULL;
4589 bool retval = e->expr_type == EXPR_FUNCTION;
4591 /* If the expression is the result of substitution in
4592 interface.c(gfc_extend_expr) because there is no way in
4593 which the host association can be wrong. */
4594 if (e->symtree == NULL
4595 || e->symtree->n.sym == NULL
4596 || e->user_operator)
4599 old_sym = e->symtree->n.sym;
4601 if (gfc_current_ns->parent
4602 && old_sym->ns != gfc_current_ns)
4604 /* Use the 'USE' name so that renamed module symbols are
4605 correctly handled. */
4606 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
4608 if (sym && old_sym != sym
4609 && sym->ts.type == old_sym->ts.type
4610 && sym->attr.flavor == FL_PROCEDURE
4611 && sym->attr.contained)
4613 /* Clear the shape, since it might not be valid. */
4614 if (e->shape != NULL)
4616 for (n = 0; n < e->rank; n++)
4617 mpz_clear (e->shape[n]);
4619 gfc_free (e->shape);
4622 /* Give the expression the right symtree! */
4623 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
4624 gcc_assert (st != NULL);
4626 if (old_sym->attr.flavor == FL_PROCEDURE
4627 || e->expr_type == EXPR_FUNCTION)
4629 /* Original was function so point to the new symbol, since
4630 the actual argument list is already attached to the
4632 e->value.function.esym = NULL;
4637 /* Original was variable so convert array references into
4638 an actual arglist. This does not need any checking now
4639 since gfc_resolve_function will take care of it. */
4640 e->value.function.actual = NULL;
4641 e->expr_type = EXPR_FUNCTION;
4644 /* Ambiguity will not arise if the array reference is not
4645 the last reference. */
4646 for (ref = e->ref; ref; ref = ref->next)
4647 if (ref->type == REF_ARRAY && ref->next == NULL)
4650 gcc_assert (ref->type == REF_ARRAY);
4652 /* Grab the start expressions from the array ref and
4653 copy them into actual arguments. */
4654 for (n = 0; n < ref->u.ar.dimen; n++)
4656 arg = gfc_get_actual_arglist ();
4657 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4658 if (e->value.function.actual == NULL)
4659 tail = e->value.function.actual = arg;
4667 /* Dump the reference list and set the rank. */
4668 gfc_free_ref_list (e->ref);
4670 e->rank = sym->as ? sym->as->rank : 0;
4673 gfc_resolve_expr (e);
4677 /* This might have changed! */
4678 return e->expr_type == EXPR_FUNCTION;
4683 gfc_resolve_character_operator (gfc_expr *e)
4685 gfc_expr *op1 = e->value.op.op1;
4686 gfc_expr *op2 = e->value.op.op2;
4687 gfc_expr *e1 = NULL;
4688 gfc_expr *e2 = NULL;
4690 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
4692 if (op1->ts.u.cl && op1->ts.u.cl->length)
4693 e1 = gfc_copy_expr (op1->ts.u.cl->length);
4694 else if (op1->expr_type == EXPR_CONSTANT)
4695 e1 = gfc_int_expr (op1->value.character.length);
4697 if (op2->ts.u.cl && op2->ts.u.cl->length)
4698 e2 = gfc_copy_expr (op2->ts.u.cl->length);
4699 else if (op2->expr_type == EXPR_CONSTANT)
4700 e2 = gfc_int_expr (op2->value.character.length);
4702 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4707 e->ts.u.cl->length = gfc_add (e1, e2);
4708 e->ts.u.cl->length->ts.type = BT_INTEGER;
4709 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4710 gfc_simplify_expr (e->ts.u.cl->length, 0);
4711 gfc_resolve_expr (e->ts.u.cl->length);
4717 /* Ensure that an character expression has a charlen and, if possible, a
4718 length expression. */
4721 fixup_charlen (gfc_expr *e)
4723 /* The cases fall through so that changes in expression type and the need
4724 for multiple fixes are picked up. In all circumstances, a charlen should
4725 be available for the middle end to hang a backend_decl on. */
4726 switch (e->expr_type)
4729 gfc_resolve_character_operator (e);
4732 if (e->expr_type == EXPR_ARRAY)
4733 gfc_resolve_character_array_constructor (e);
4735 case EXPR_SUBSTRING:
4736 if (!e->ts.u.cl && e->ref)
4737 gfc_resolve_substring_charlen (e);
4741 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4748 /* Update an actual argument to include the passed-object for type-bound
4749 procedures at the right position. */
4751 static gfc_actual_arglist*
4752 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
4755 gcc_assert (argpos > 0);
4759 gfc_actual_arglist* result;
4761 result = gfc_get_actual_arglist ();
4765 result->name = name;
4771 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
4773 lst = update_arglist_pass (NULL, po, argpos - 1, name);
4778 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
4781 extract_compcall_passed_object (gfc_expr* e)
4785 gcc_assert (e->expr_type == EXPR_COMPCALL);
4787 if (e->value.compcall.base_object)
4788 po = gfc_copy_expr (e->value.compcall.base_object);
4791 po = gfc_get_expr ();
4792 po->expr_type = EXPR_VARIABLE;
4793 po->symtree = e->symtree;
4794 po->ref = gfc_copy_ref (e->ref);
4795 po->where = e->where;
4798 if (gfc_resolve_expr (po) == FAILURE)
4805 /* Update the arglist of an EXPR_COMPCALL expression to include the
4809 update_compcall_arglist (gfc_expr* e)
4812 gfc_typebound_proc* tbp;
4814 tbp = e->value.compcall.tbp;
4819 po = extract_compcall_passed_object (e);
4823 if (tbp->nopass || e->value.compcall.ignore_pass)
4829 gcc_assert (tbp->pass_arg_num > 0);
4830 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4838 /* Extract the passed object from a PPC call (a copy of it). */
4841 extract_ppc_passed_object (gfc_expr *e)
4846 po = gfc_get_expr ();
4847 po->expr_type = EXPR_VARIABLE;
4848 po->symtree = e->symtree;
4849 po->ref = gfc_copy_ref (e->ref);
4850 po->where = e->where;
4852 /* Remove PPC reference. */
4854 while ((*ref)->next)
4855 ref = &(*ref)->next;
4856 gfc_free_ref_list (*ref);
4859 if (gfc_resolve_expr (po) == FAILURE)
4866 /* Update the actual arglist of a procedure pointer component to include the
4870 update_ppc_arglist (gfc_expr* e)
4874 gfc_typebound_proc* tb;
4876 if (!gfc_is_proc_ptr_comp (e, &ppc))
4883 else if (tb->nopass)
4886 po = extract_ppc_passed_object (e);
4892 gfc_error ("Passed-object at %L must be scalar", &e->where);
4896 gcc_assert (tb->pass_arg_num > 0);
4897 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4905 /* Check that the object a TBP is called on is valid, i.e. it must not be
4906 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
4909 check_typebound_baseobject (gfc_expr* e)
4913 base = extract_compcall_passed_object (e);
4917 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
4919 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
4921 gfc_error ("Base object for type-bound procedure call at %L is of"
4922 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
4926 /* If the procedure called is NOPASS, the base object must be scalar. */
4927 if (e->value.compcall.tbp->nopass && base->rank > 0)
4929 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
4930 " be scalar", &e->where);
4934 /* FIXME: Remove once PR 41177 (this problem) is fixed completely. */
4937 gfc_error ("Non-scalar base object at %L currently not implemented",
4946 /* Resolve a call to a type-bound procedure, either function or subroutine,
4947 statically from the data in an EXPR_COMPCALL expression. The adapted
4948 arglist and the target-procedure symtree are returned. */
4951 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
4952 gfc_actual_arglist** actual)
4954 gcc_assert (e->expr_type == EXPR_COMPCALL);
4955 gcc_assert (!e->value.compcall.tbp->is_generic);
4957 /* Update the actual arglist for PASS. */
4958 if (update_compcall_arglist (e) == FAILURE)
4961 *actual = e->value.compcall.actual;
4962 *target = e->value.compcall.tbp->u.specific;
4964 gfc_free_ref_list (e->ref);
4966 e->value.compcall.actual = NULL;
4972 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
4973 which of the specific bindings (if any) matches the arglist and transform
4974 the expression into a call of that binding. */
4977 resolve_typebound_generic_call (gfc_expr* e)
4979 gfc_typebound_proc* genproc;
4980 const char* genname;
4982 gcc_assert (e->expr_type == EXPR_COMPCALL);
4983 genname = e->value.compcall.name;
4984 genproc = e->value.compcall.tbp;
4986 if (!genproc->is_generic)
4989 /* Try the bindings on this type and in the inheritance hierarchy. */
4990 for (; genproc; genproc = genproc->overridden)
4994 gcc_assert (genproc->is_generic);
4995 for (g = genproc->u.generic; g; g = g->next)
4998 gfc_actual_arglist* args;
5001 gcc_assert (g->specific);
5003 if (g->specific->error)
5006 target = g->specific->u.specific->n.sym;
5008 /* Get the right arglist by handling PASS/NOPASS. */
5009 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5010 if (!g->specific->nopass)
5013 po = extract_compcall_passed_object (e);
5017 gcc_assert (g->specific->pass_arg_num > 0);
5018 gcc_assert (!g->specific->error);
5019 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5020 g->specific->pass_arg);
5022 resolve_actual_arglist (args, target->attr.proc,
5023 is_external_proc (target) && !target->formal);
5025 /* Check if this arglist matches the formal. */
5026 matches = gfc_arglist_matches_symbol (&args, target);
5028 /* Clean up and break out of the loop if we've found it. */
5029 gfc_free_actual_arglist (args);
5032 e->value.compcall.tbp = g->specific;
5038 /* Nothing matching found! */
5039 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5040 " '%s' at %L", genname, &e->where);
5048 /* Resolve a call to a type-bound subroutine. */
5051 resolve_typebound_call (gfc_code* c)
5053 gfc_actual_arglist* newactual;
5054 gfc_symtree* target;
5056 /* Check that's really a SUBROUTINE. */
5057 if (!c->expr1->value.compcall.tbp->subroutine)
5059 gfc_error ("'%s' at %L should be a SUBROUTINE",
5060 c->expr1->value.compcall.name, &c->loc);
5064 if (check_typebound_baseobject (c->expr1) == FAILURE)
5067 if (resolve_typebound_generic_call (c->expr1) == FAILURE)
5070 /* Transform into an ordinary EXEC_CALL for now. */
5072 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5075 c->ext.actual = newactual;
5076 c->symtree = target;
5077 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5079 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5081 gfc_free_expr (c->expr1);
5082 c->expr1 = gfc_get_expr ();
5083 c->expr1->expr_type = EXPR_FUNCTION;
5084 c->expr1->symtree = target;
5085 c->expr1->where = c->loc;
5087 return resolve_call (c);
5091 /* Resolve a component-call expression. This originally was intended
5092 only to see functions. However, it is convenient to use it in
5093 resolving subroutine class methods, since we do not have to add a
5094 gfc_code each time. */
5096 resolve_compcall (gfc_expr* e, bool fcn, bool class_members)
5098 gfc_actual_arglist* newactual;
5099 gfc_symtree* target;
5101 /* Check that's really a FUNCTION. */
5102 if (fcn && !e->value.compcall.tbp->function)
5104 gfc_error ("'%s' at %L should be a FUNCTION",
5105 e->value.compcall.name, &e->where);
5108 else if (!fcn && !e->value.compcall.tbp->subroutine)
5110 /* To resolve class member calls, we borrow this bit
5111 of code to select the specific procedures. */
5112 gfc_error ("'%s' at %L should be a SUBROUTINE",
5113 e->value.compcall.name, &e->where);
5117 /* These must not be assign-calls! */
5118 gcc_assert (!e->value.compcall.assign);
5120 if (check_typebound_baseobject (e) == FAILURE)
5123 if (resolve_typebound_generic_call (e) == FAILURE)
5125 gcc_assert (!e->value.compcall.tbp->is_generic);
5127 /* Take the rank from the function's symbol. */
5128 if (e->value.compcall.tbp->u.specific->n.sym->as)
5129 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5131 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5132 arglist to the TBP's binding target. */
5134 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5137 e->value.function.actual = newactual;
5138 e->value.function.name = NULL;
5139 e->value.function.esym = target->n.sym;
5140 e->value.function.class_esym = NULL;
5141 e->value.function.isym = NULL;
5142 e->symtree = target;
5143 e->ts = target->n.sym->ts;
5144 e->expr_type = EXPR_FUNCTION;
5146 /* Resolution is not necessary when constructing component calls
5147 for class members, since this must only be done for the
5148 declared type, which is done afterwards. */
5149 return !class_members ? gfc_resolve_expr (e) : SUCCESS;
5153 /* Resolve a typebound call for the members in a class. This group of
5154 functions implements dynamic dispatch in the provisional version
5155 of f03 OOP. As soon as vtables are in place and contain pointers
5156 to methods, this will no longer be necessary. */
5157 static gfc_expr *list_e;
5158 static void check_class_members (gfc_symbol *);
5159 static gfc_try class_try;
5160 static bool fcn_flag;
5164 check_members (gfc_symbol *derived)
5166 if (derived->attr.flavor == FL_DERIVED)
5167 check_class_members (derived);
5172 check_class_members (gfc_symbol *derived)
5176 gfc_class_esym_list *etmp;
5178 e = gfc_copy_expr (list_e);
5180 tbp = gfc_find_typebound_proc (derived, &class_try,
5181 e->value.compcall.name,
5186 gfc_error ("no typebound available procedure named '%s' at %L",
5187 e->value.compcall.name, &e->where);
5191 /* If we have to match a passed class member, force the actual
5192 expression to have the correct type. */
5193 if (!tbp->n.tb->nopass)
5195 if (e->value.compcall.base_object == NULL)
5196 e->value.compcall.base_object = extract_compcall_passed_object (e);
5198 if (!derived->attr.abstract)
5200 e->value.compcall.base_object->ts.type = BT_DERIVED;
5201 e->value.compcall.base_object->ts.u.derived = derived;
5205 e->value.compcall.tbp = tbp->n.tb;
5206 e->value.compcall.name = tbp->name;
5208 /* Let the original expresssion catch the assertion in
5209 resolve_compcall, since this flag does not appear to be reset or
5210 copied in some systems. */
5211 e->value.compcall.assign = 0;
5213 /* Do the renaming, PASSing, generic => specific and other
5214 good things for each class member. */
5215 class_try = (resolve_compcall (e, fcn_flag, true) == SUCCESS)
5216 ? class_try : FAILURE;
5218 /* Now transfer the found symbol to the esym list. */
5219 if (class_try == SUCCESS)
5221 etmp = list_e->value.function.class_esym;
5222 list_e->value.function.class_esym
5223 = gfc_get_class_esym_list();
5224 list_e->value.function.class_esym->next = etmp;
5225 list_e->value.function.class_esym->derived = derived;
5226 list_e->value.function.class_esym->esym
5227 = e->value.function.esym;
5232 /* Burrow down into grandchildren types. */
5233 if (derived->f2k_derived)
5234 gfc_traverse_ns (derived->f2k_derived, check_members);
5238 /* Eliminate esym_lists where all the members point to the
5239 typebound procedure of the declared type; ie. one where
5240 type selection has no effect.. */
5242 resolve_class_esym (gfc_expr *e)
5244 gfc_class_esym_list *p, *q;
5247 gcc_assert (e && e->expr_type == EXPR_FUNCTION);
5249 p = e->value.function.class_esym;
5253 for (; p; p = p->next)
5254 empty = empty && (e->value.function.esym == p->esym);
5258 p = e->value.function.class_esym;
5264 e->value.function.class_esym = NULL;
5269 /* Generate an expression for the hash value, given the reference to
5270 the class of the final expression (class_ref), the base of the
5271 full reference list (new_ref), the declared type and the class
5274 hash_value_expr (gfc_ref *class_ref, gfc_ref *new_ref, gfc_symtree *st)
5276 gfc_expr *hash_value;
5278 /* Build an expression for the correct hash_value; ie. that of the last
5282 class_ref->next = NULL;
5286 gfc_free_ref_list (new_ref);
5289 hash_value = gfc_get_expr ();
5290 hash_value->expr_type = EXPR_VARIABLE;
5291 hash_value->symtree = st;
5292 hash_value->symtree->n.sym->refs++;
5293 hash_value->ref = new_ref;
5294 gfc_add_component_ref (hash_value, "$vptr");
5295 gfc_add_component_ref (hash_value, "$hash");
5301 /* Get the ultimate declared type from an expression. In addition,
5302 return the last class/derived type reference and the copy of the
5305 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5308 gfc_symbol *declared;
5313 *new_ref = gfc_copy_ref (e->ref);
5314 for (ref = *new_ref; ref; ref = ref->next)
5316 if (ref->type != REF_COMPONENT)
5319 if (ref->u.c.component->ts.type == BT_CLASS
5320 || ref->u.c.component->ts.type == BT_DERIVED)
5322 declared = ref->u.c.component->ts.u.derived;
5327 if (declared == NULL)
5328 declared = e->symtree->n.sym->ts.u.derived;
5334 /* Resolve the argument expressions so that any arguments expressions
5335 that include class methods are resolved before the current call.
5336 This is necessary because of the static variables used in CLASS
5337 method resolution. */
5339 resolve_arg_exprs (gfc_actual_arglist *arg)
5341 /* Resolve the actual arglist expressions. */
5342 for (; arg; arg = arg->next)
5345 gfc_resolve_expr (arg->expr);
5350 /* Resolve a typebound function, or 'method'. First separate all
5351 the non-CLASS references by calling resolve_compcall directly.
5352 Then treat the CLASS references by resolving for each of the class
5356 resolve_typebound_function (gfc_expr* e)
5358 gfc_symbol *derived, *declared;
5365 return resolve_compcall (e, true, false);
5367 /* Get the CLASS declared type. */
5368 declared = get_declared_from_expr (&class_ref, &new_ref, e);
5370 /* Weed out cases of the ultimate component being a derived type. */
5371 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5372 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5374 gfc_free_ref_list (new_ref);
5375 return resolve_compcall (e, true, false);
5378 /* Resolve the argument expressions, */
5379 resolve_arg_exprs (e->value.function.actual);
5381 /* Get the data component, which is of the declared type. */
5382 derived = declared->components->ts.u.derived;
5384 /* Resolve the function call for each member of the class. */
5385 class_try = SUCCESS;
5387 list_e = gfc_copy_expr (e);
5388 check_class_members (derived);
5390 class_try = (resolve_compcall (e, true, false) == SUCCESS)
5391 ? class_try : FAILURE;
5393 /* Transfer the class list to the original expression. Note that
5394 the class_esym list is cleaned up in trans-expr.c, as the calls
5396 e->value.function.class_esym = list_e->value.function.class_esym;
5397 list_e->value.function.class_esym = NULL;
5398 gfc_free_expr (list_e);
5400 resolve_class_esym (e);
5402 /* More than one typebound procedure so transmit an expression for
5403 the hash_value as the selector. */
5404 if (e->value.function.class_esym != NULL)
5405 e->value.function.class_esym->hash_value
5406 = hash_value_expr (class_ref, new_ref, st);
5411 /* Resolve a typebound subroutine, or 'method'. First separate all
5412 the non-CLASS references by calling resolve_typebound_call directly.
5413 Then treat the CLASS references by resolving for each of the class
5417 resolve_typebound_subroutine (gfc_code *code)
5419 gfc_symbol *derived, *declared;
5424 st = code->expr1->symtree;
5426 return resolve_typebound_call (code);
5428 /* Get the CLASS declared type. */
5429 declared = get_declared_from_expr (&class_ref, &new_ref, code->expr1);
5431 /* Weed out cases of the ultimate component being a derived type. */
5432 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5433 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5435 gfc_free_ref_list (new_ref);
5436 return resolve_typebound_call (code);
5439 /* Resolve the argument expressions, */
5440 resolve_arg_exprs (code->expr1->value.compcall.actual);
5442 /* Get the data component, which is of the declared type. */
5443 derived = declared->components->ts.u.derived;
5445 class_try = SUCCESS;
5447 list_e = gfc_copy_expr (code->expr1);
5448 check_class_members (derived);
5450 class_try = (resolve_typebound_call (code) == SUCCESS)
5451 ? class_try : FAILURE;
5453 /* Transfer the class list to the original expression. Note that
5454 the class_esym list is cleaned up in trans-expr.c, as the calls
5456 code->expr1->value.function.class_esym
5457 = list_e->value.function.class_esym;
5458 list_e->value.function.class_esym = NULL;
5459 gfc_free_expr (list_e);
5461 resolve_class_esym (code->expr1);
5463 /* More than one typebound procedure so transmit an expression for
5464 the hash_value as the selector. */
5465 if (code->expr1->value.function.class_esym != NULL)
5466 code->expr1->value.function.class_esym->hash_value
5467 = hash_value_expr (class_ref, new_ref, st);
5473 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5476 resolve_ppc_call (gfc_code* c)
5478 gfc_component *comp;
5481 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
5484 c->resolved_sym = c->expr1->symtree->n.sym;
5485 c->expr1->expr_type = EXPR_VARIABLE;
5487 if (!comp->attr.subroutine)
5488 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5490 if (resolve_ref (c->expr1) == FAILURE)
5493 if (update_ppc_arglist (c->expr1) == FAILURE)
5496 c->ext.actual = c->expr1->value.compcall.actual;
5498 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5499 comp->formal == NULL) == FAILURE)
5502 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5508 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5511 resolve_expr_ppc (gfc_expr* e)
5513 gfc_component *comp;
5516 b = gfc_is_proc_ptr_comp (e, &comp);
5519 /* Convert to EXPR_FUNCTION. */
5520 e->expr_type = EXPR_FUNCTION;
5521 e->value.function.isym = NULL;
5522 e->value.function.actual = e->value.compcall.actual;
5524 if (comp->as != NULL)
5525 e->rank = comp->as->rank;
5527 if (!comp->attr.function)
5528 gfc_add_function (&comp->attr, comp->name, &e->where);
5530 if (resolve_ref (e) == FAILURE)
5533 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5534 comp->formal == NULL) == FAILURE)
5537 if (update_ppc_arglist (e) == FAILURE)
5540 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
5547 gfc_is_expandable_expr (gfc_expr *e)
5549 gfc_constructor *con;
5551 if (e->expr_type == EXPR_ARRAY)
5553 /* Traverse the constructor looking for variables that are flavor
5554 parameter. Parameters must be expanded since they are fully used at
5556 for (con = e->value.constructor; con; con = con->next)
5558 if (con->expr->expr_type == EXPR_VARIABLE
5559 && con->expr->symtree
5560 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
5561 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
5563 if (con->expr->expr_type == EXPR_ARRAY
5564 && gfc_is_expandable_expr (con->expr))
5572 /* Resolve an expression. That is, make sure that types of operands agree
5573 with their operators, intrinsic operators are converted to function calls
5574 for overloaded types and unresolved function references are resolved. */
5577 gfc_resolve_expr (gfc_expr *e)
5584 switch (e->expr_type)
5587 t = resolve_operator (e);
5593 if (check_host_association (e))
5594 t = resolve_function (e);
5597 t = resolve_variable (e);
5599 expression_rank (e);
5602 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
5603 && e->ref->type != REF_SUBSTRING)
5604 gfc_resolve_substring_charlen (e);
5609 t = resolve_typebound_function (e);
5612 case EXPR_SUBSTRING:
5613 t = resolve_ref (e);
5622 t = resolve_expr_ppc (e);
5627 if (resolve_ref (e) == FAILURE)
5630 t = gfc_resolve_array_constructor (e);
5631 /* Also try to expand a constructor. */
5634 expression_rank (e);
5635 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
5636 gfc_expand_constructor (e);
5639 /* This provides the opportunity for the length of constructors with
5640 character valued function elements to propagate the string length
5641 to the expression. */
5642 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
5644 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
5645 here rather then add a duplicate test for it above. */
5646 gfc_expand_constructor (e);
5647 t = gfc_resolve_character_array_constructor (e);
5652 case EXPR_STRUCTURE:
5653 t = resolve_ref (e);
5657 t = resolve_structure_cons (e);
5661 t = gfc_simplify_expr (e, 0);
5665 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
5668 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
5675 /* Resolve an expression from an iterator. They must be scalar and have
5676 INTEGER or (optionally) REAL type. */
5679 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
5680 const char *name_msgid)
5682 if (gfc_resolve_expr (expr) == FAILURE)
5685 if (expr->rank != 0)
5687 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
5691 if (expr->ts.type != BT_INTEGER)
5693 if (expr->ts.type == BT_REAL)
5696 return gfc_notify_std (GFC_STD_F95_DEL,
5697 "Deleted feature: %s at %L must be integer",
5698 _(name_msgid), &expr->where);
5701 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
5708 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
5716 /* Resolve the expressions in an iterator structure. If REAL_OK is
5717 false allow only INTEGER type iterators, otherwise allow REAL types. */
5720 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
5722 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
5726 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
5728 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5733 if (gfc_resolve_iterator_expr (iter->start, real_ok,
5734 "Start expression in DO loop") == FAILURE)
5737 if (gfc_resolve_iterator_expr (iter->end, real_ok,
5738 "End expression in DO loop") == FAILURE)
5741 if (gfc_resolve_iterator_expr (iter->step, real_ok,
5742 "Step expression in DO loop") == FAILURE)
5745 if (iter->step->expr_type == EXPR_CONSTANT)
5747 if ((iter->step->ts.type == BT_INTEGER
5748 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
5749 || (iter->step->ts.type == BT_REAL
5750 && mpfr_sgn (iter->step->value.real) == 0))
5752 gfc_error ("Step expression in DO loop at %L cannot be zero",
5753 &iter->step->where);
5758 /* Convert start, end, and step to the same type as var. */
5759 if (iter->start->ts.kind != iter->var->ts.kind
5760 || iter->start->ts.type != iter->var->ts.type)
5761 gfc_convert_type (iter->start, &iter->var->ts, 2);
5763 if (iter->end->ts.kind != iter->var->ts.kind
5764 || iter->end->ts.type != iter->var->ts.type)
5765 gfc_convert_type (iter->end, &iter->var->ts, 2);
5767 if (iter->step->ts.kind != iter->var->ts.kind
5768 || iter->step->ts.type != iter->var->ts.type)
5769 gfc_convert_type (iter->step, &iter->var->ts, 2);
5771 if (iter->start->expr_type == EXPR_CONSTANT
5772 && iter->end->expr_type == EXPR_CONSTANT
5773 && iter->step->expr_type == EXPR_CONSTANT)
5776 if (iter->start->ts.type == BT_INTEGER)
5778 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
5779 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
5783 sgn = mpfr_sgn (iter->step->value.real);
5784 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
5786 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
5787 gfc_warning ("DO loop at %L will be executed zero times",
5788 &iter->step->where);
5795 /* Traversal function for find_forall_index. f == 2 signals that
5796 that variable itself is not to be checked - only the references. */
5799 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
5801 if (expr->expr_type != EXPR_VARIABLE)
5804 /* A scalar assignment */
5805 if (!expr->ref || *f == 1)
5807 if (expr->symtree->n.sym == sym)
5819 /* Check whether the FORALL index appears in the expression or not.
5820 Returns SUCCESS if SYM is found in EXPR. */
5823 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
5825 if (gfc_traverse_expr (expr, sym, forall_index, f))
5832 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5833 to be a scalar INTEGER variable. The subscripts and stride are scalar
5834 INTEGERs, and if stride is a constant it must be nonzero.
5835 Furthermore "A subscript or stride in a forall-triplet-spec shall
5836 not contain a reference to any index-name in the
5837 forall-triplet-spec-list in which it appears." (7.5.4.1) */
5840 resolve_forall_iterators (gfc_forall_iterator *it)
5842 gfc_forall_iterator *iter, *iter2;
5844 for (iter = it; iter; iter = iter->next)
5846 if (gfc_resolve_expr (iter->var) == SUCCESS
5847 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
5848 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
5851 if (gfc_resolve_expr (iter->start) == SUCCESS
5852 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
5853 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
5854 &iter->start->where);
5855 if (iter->var->ts.kind != iter->start->ts.kind)
5856 gfc_convert_type (iter->start, &iter->var->ts, 2);
5858 if (gfc_resolve_expr (iter->end) == SUCCESS
5859 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
5860 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
5862 if (iter->var->ts.kind != iter->end->ts.kind)
5863 gfc_convert_type (iter->end, &iter->var->ts, 2);
5865 if (gfc_resolve_expr (iter->stride) == SUCCESS)
5867 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
5868 gfc_error ("FORALL stride expression at %L must be a scalar %s",
5869 &iter->stride->where, "INTEGER");
5871 if (iter->stride->expr_type == EXPR_CONSTANT
5872 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
5873 gfc_error ("FORALL stride expression at %L cannot be zero",
5874 &iter->stride->where);
5876 if (iter->var->ts.kind != iter->stride->ts.kind)
5877 gfc_convert_type (iter->stride, &iter->var->ts, 2);
5880 for (iter = it; iter; iter = iter->next)
5881 for (iter2 = iter; iter2; iter2 = iter2->next)
5883 if (find_forall_index (iter2->start,
5884 iter->var->symtree->n.sym, 0) == SUCCESS
5885 || find_forall_index (iter2->end,
5886 iter->var->symtree->n.sym, 0) == SUCCESS
5887 || find_forall_index (iter2->stride,
5888 iter->var->symtree->n.sym, 0) == SUCCESS)
5889 gfc_error ("FORALL index '%s' may not appear in triplet "
5890 "specification at %L", iter->var->symtree->name,
5891 &iter2->start->where);
5896 /* Given a pointer to a symbol that is a derived type, see if it's
5897 inaccessible, i.e. if it's defined in another module and the components are
5898 PRIVATE. The search is recursive if necessary. Returns zero if no
5899 inaccessible components are found, nonzero otherwise. */
5902 derived_inaccessible (gfc_symbol *sym)
5906 if (sym->attr.use_assoc && sym->attr.private_comp)
5909 for (c = sym->components; c; c = c->next)
5911 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
5919 /* Resolve the argument of a deallocate expression. The expression must be
5920 a pointer or a full array. */
5923 resolve_deallocate_expr (gfc_expr *e)
5925 symbol_attribute attr;
5926 int allocatable, pointer, check_intent_in;
5931 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5932 check_intent_in = 1;
5934 if (gfc_resolve_expr (e) == FAILURE)
5937 if (e->expr_type != EXPR_VARIABLE)
5940 sym = e->symtree->n.sym;
5942 if (sym->ts.type == BT_CLASS)
5944 allocatable = sym->ts.u.derived->components->attr.allocatable;
5945 pointer = sym->ts.u.derived->components->attr.pointer;
5949 allocatable = sym->attr.allocatable;
5950 pointer = sym->attr.pointer;
5952 for (ref = e->ref; ref; ref = ref->next)
5955 check_intent_in = 0;
5960 if (ref->u.ar.type != AR_FULL)
5965 c = ref->u.c.component;
5966 if (c->ts.type == BT_CLASS)
5968 allocatable = c->ts.u.derived->components->attr.allocatable;
5969 pointer = c->ts.u.derived->components->attr.pointer;
5973 allocatable = c->attr.allocatable;
5974 pointer = c->attr.pointer;
5984 attr = gfc_expr_attr (e);
5986 if (allocatable == 0 && attr.pointer == 0)
5989 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5993 if (check_intent_in && sym->attr.intent == INTENT_IN)
5995 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
5996 sym->name, &e->where);
6000 if (e->ts.type == BT_CLASS)
6002 /* Only deallocate the DATA component. */
6003 gfc_add_component_ref (e, "$data");
6010 /* Returns true if the expression e contains a reference to the symbol sym. */
6012 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6014 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6021 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6023 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6027 /* Given the expression node e for an allocatable/pointer of derived type to be
6028 allocated, get the expression node to be initialized afterwards (needed for
6029 derived types with default initializers, and derived types with allocatable
6030 components that need nullification.) */
6033 gfc_expr_to_initialize (gfc_expr *e)
6039 result = gfc_copy_expr (e);
6041 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6042 for (ref = result->ref; ref; ref = ref->next)
6043 if (ref->type == REF_ARRAY && ref->next == NULL)
6045 ref->u.ar.type = AR_FULL;
6047 for (i = 0; i < ref->u.ar.dimen; i++)
6048 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6050 result->rank = ref->u.ar.dimen;
6058 /* Used in resolve_allocate_expr to check that a allocation-object and
6059 a source-expr are conformable. This does not catch all possible
6060 cases; in particular a runtime checking is needed. */
6063 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6065 /* First compare rank. */
6066 if (e2->ref && e1->rank != e2->ref->u.ar.as->rank)
6068 gfc_error ("Source-expr at %L must be scalar or have the "
6069 "same rank as the allocate-object at %L",
6070 &e1->where, &e2->where);
6081 for (i = 0; i < e1->rank; i++)
6083 if (e2->ref->u.ar.end[i])
6085 mpz_set (s, e2->ref->u.ar.end[i]->value.integer);
6086 mpz_sub (s, s, e2->ref->u.ar.start[i]->value.integer);
6087 mpz_add_ui (s, s, 1);
6091 mpz_set (s, e2->ref->u.ar.start[i]->value.integer);
6094 if (mpz_cmp (e1->shape[i], s) != 0)
6096 gfc_error ("Source-expr at %L and allocate-object at %L must "
6097 "have the same shape", &e1->where, &e2->where);
6110 /* Resolve the expression in an ALLOCATE statement, doing the additional
6111 checks to see whether the expression is OK or not. The expression must
6112 have a trailing array reference that gives the size of the array. */
6115 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6117 int i, pointer, allocatable, dimension, check_intent_in, is_abstract;
6118 symbol_attribute attr;
6119 gfc_ref *ref, *ref2;
6126 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
6127 check_intent_in = 1;
6129 if (gfc_resolve_expr (e) == FAILURE)
6132 /* Make sure the expression is allocatable or a pointer. If it is
6133 pointer, the next-to-last reference must be a pointer. */
6137 sym = e->symtree->n.sym;
6139 /* Check whether ultimate component is abstract and CLASS. */
6142 if (e->expr_type != EXPR_VARIABLE)
6145 attr = gfc_expr_attr (e);
6146 pointer = attr.pointer;
6147 dimension = attr.dimension;
6151 if (sym->ts.type == BT_CLASS)
6153 allocatable = sym->ts.u.derived->components->attr.allocatable;
6154 pointer = sym->ts.u.derived->components->attr.pointer;
6155 dimension = sym->ts.u.derived->components->attr.dimension;
6156 is_abstract = sym->ts.u.derived->components->attr.abstract;
6160 allocatable = sym->attr.allocatable;
6161 pointer = sym->attr.pointer;
6162 dimension = sym->attr.dimension;
6165 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6168 check_intent_in = 0;
6173 if (ref->next != NULL)
6178 c = ref->u.c.component;
6179 if (c->ts.type == BT_CLASS)
6181 allocatable = c->ts.u.derived->components->attr.allocatable;
6182 pointer = c->ts.u.derived->components->attr.pointer;
6183 dimension = c->ts.u.derived->components->attr.dimension;
6184 is_abstract = c->ts.u.derived->components->attr.abstract;
6188 allocatable = c->attr.allocatable;
6189 pointer = c->attr.pointer;
6190 dimension = c->attr.dimension;
6191 is_abstract = c->attr.abstract;
6203 if (allocatable == 0 && pointer == 0)
6205 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6210 /* Some checks for the SOURCE tag. */
6213 /* Check F03:C631. */
6214 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6216 gfc_error ("Type of entity at %L is type incompatible with "
6217 "source-expr at %L", &e->where, &code->expr3->where);
6221 /* Check F03:C632 and restriction following Note 6.18. */
6222 if (code->expr3->rank > 0
6223 && conformable_arrays (code->expr3, e) == FAILURE)
6226 /* Check F03:C633. */
6227 if (code->expr3->ts.kind != e->ts.kind)
6229 gfc_error ("The allocate-object at %L and the source-expr at %L "
6230 "shall have the same kind type parameter",
6231 &e->where, &code->expr3->where);
6235 else if (is_abstract&& code->ext.alloc.ts.type == BT_UNKNOWN)
6237 gcc_assert (e->ts.type == BT_CLASS);
6238 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6239 "type-spec or SOURCE=", sym->name, &e->where);
6243 if (check_intent_in && sym->attr.intent == INTENT_IN)
6245 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
6246 sym->name, &e->where);
6252 /* Add default initializer for those derived types that need them. */
6253 if (e->ts.type == BT_DERIVED
6254 && (init_e = gfc_default_initializer (&e->ts)))
6256 gfc_code *init_st = gfc_get_code ();
6257 init_st->loc = code->loc;
6258 init_st->op = EXEC_INIT_ASSIGN;
6259 init_st->expr1 = gfc_expr_to_initialize (e);
6260 init_st->expr2 = init_e;
6261 init_st->next = code->next;
6262 code->next = init_st;
6264 else if (e->ts.type == BT_CLASS
6265 && ((code->ext.alloc.ts.type == BT_UNKNOWN
6266 && (init_e = gfc_default_initializer (&e->ts.u.derived->components->ts)))
6267 || (code->ext.alloc.ts.type == BT_DERIVED
6268 && (init_e = gfc_default_initializer (&code->ext.alloc.ts)))))
6270 gfc_code *init_st = gfc_get_code ();
6271 init_st->loc = code->loc;
6272 init_st->op = EXEC_INIT_ASSIGN;
6273 init_st->expr1 = gfc_expr_to_initialize (e);
6274 init_st->expr2 = init_e;
6275 init_st->next = code->next;
6276 code->next = init_st;
6280 if (pointer || dimension == 0)
6283 /* Make sure the next-to-last reference node is an array specification. */
6285 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
6287 gfc_error ("Array specification required in ALLOCATE statement "
6288 "at %L", &e->where);
6292 /* Make sure that the array section reference makes sense in the
6293 context of an ALLOCATE specification. */
6297 for (i = 0; i < ar->dimen; i++)
6299 if (ref2->u.ar.type == AR_ELEMENT)
6302 switch (ar->dimen_type[i])
6308 if (ar->start[i] != NULL
6309 && ar->end[i] != NULL
6310 && ar->stride[i] == NULL)
6313 /* Fall Through... */
6317 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6324 for (a = code->ext.alloc.list; a; a = a->next)
6326 sym = a->expr->symtree->n.sym;
6328 /* TODO - check derived type components. */
6329 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6332 if ((ar->start[i] != NULL
6333 && gfc_find_sym_in_expr (sym, ar->start[i]))
6334 || (ar->end[i] != NULL
6335 && gfc_find_sym_in_expr (sym, ar->end[i])))
6337 gfc_error ("'%s' must not appear in the array specification at "
6338 "%L in the same ALLOCATE statement where it is "
6339 "itself allocated", sym->name, &ar->where);
6349 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6351 gfc_expr *stat, *errmsg, *pe, *qe;
6352 gfc_alloc *a, *p, *q;
6354 stat = code->expr1 ? code->expr1 : NULL;
6356 errmsg = code->expr2 ? code->expr2 : NULL;
6358 /* Check the stat variable. */
6361 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
6362 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
6363 stat->symtree->n.sym->name, &stat->where);
6365 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
6366 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
6369 if ((stat->ts.type != BT_INTEGER
6370 && !(stat->ref && (stat->ref->type == REF_ARRAY
6371 || stat->ref->type == REF_COMPONENT)))
6373 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6374 "variable", &stat->where);
6376 for (p = code->ext.alloc.list; p; p = p->next)
6377 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
6378 gfc_error ("Stat-variable at %L shall not be %sd within "
6379 "the same %s statement", &stat->where, fcn, fcn);
6382 /* Check the errmsg variable. */
6386 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6389 if (errmsg->symtree->n.sym->attr.intent == INTENT_IN)
6390 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
6391 errmsg->symtree->n.sym->name, &errmsg->where);
6393 if (gfc_pure (NULL) && gfc_impure_variable (errmsg->symtree->n.sym))
6394 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
6397 if ((errmsg->ts.type != BT_CHARACTER
6399 && (errmsg->ref->type == REF_ARRAY
6400 || errmsg->ref->type == REF_COMPONENT)))
6401 || errmsg->rank > 0 )
6402 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
6403 "variable", &errmsg->where);
6405 for (p = code->ext.alloc.list; p; p = p->next)
6406 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
6407 gfc_error ("Errmsg-variable at %L shall not be %sd within "
6408 "the same %s statement", &errmsg->where, fcn, fcn);
6411 /* Check that an allocate-object appears only once in the statement.
6412 FIXME: Checking derived types is disabled. */
6413 for (p = code->ext.alloc.list; p; p = p->next)
6416 if ((pe->ref && pe->ref->type != REF_COMPONENT)
6417 && (pe->symtree->n.sym->ts.type != BT_DERIVED))
6419 for (q = p->next; q; q = q->next)
6422 if ((qe->ref && qe->ref->type != REF_COMPONENT)
6423 && (qe->symtree->n.sym->ts.type != BT_DERIVED)
6424 && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
6425 gfc_error ("Allocate-object at %L also appears at %L",
6426 &pe->where, &qe->where);
6431 if (strcmp (fcn, "ALLOCATE") == 0)
6433 for (a = code->ext.alloc.list; a; a = a->next)
6434 resolve_allocate_expr (a->expr, code);
6438 for (a = code->ext.alloc.list; a; a = a->next)
6439 resolve_deallocate_expr (a->expr);
6444 /************ SELECT CASE resolution subroutines ************/
6446 /* Callback function for our mergesort variant. Determines interval
6447 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
6448 op1 > op2. Assumes we're not dealing with the default case.
6449 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
6450 There are nine situations to check. */
6453 compare_cases (const gfc_case *op1, const gfc_case *op2)
6457 if (op1->low == NULL) /* op1 = (:L) */
6459 /* op2 = (:N), so overlap. */
6461 /* op2 = (M:) or (M:N), L < M */
6462 if (op2->low != NULL
6463 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6466 else if (op1->high == NULL) /* op1 = (K:) */
6468 /* op2 = (M:), so overlap. */
6470 /* op2 = (:N) or (M:N), K > N */
6471 if (op2->high != NULL
6472 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6475 else /* op1 = (K:L) */
6477 if (op2->low == NULL) /* op2 = (:N), K > N */
6478 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6480 else if (op2->high == NULL) /* op2 = (M:), L < M */
6481 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6483 else /* op2 = (M:N) */
6487 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6490 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6499 /* Merge-sort a double linked case list, detecting overlap in the
6500 process. LIST is the head of the double linked case list before it
6501 is sorted. Returns the head of the sorted list if we don't see any
6502 overlap, or NULL otherwise. */
6505 check_case_overlap (gfc_case *list)
6507 gfc_case *p, *q, *e, *tail;
6508 int insize, nmerges, psize, qsize, cmp, overlap_seen;
6510 /* If the passed list was empty, return immediately. */
6517 /* Loop unconditionally. The only exit from this loop is a return
6518 statement, when we've finished sorting the case list. */
6525 /* Count the number of merges we do in this pass. */
6528 /* Loop while there exists a merge to be done. */
6533 /* Count this merge. */
6536 /* Cut the list in two pieces by stepping INSIZE places
6537 forward in the list, starting from P. */
6540 for (i = 0; i < insize; i++)
6549 /* Now we have two lists. Merge them! */
6550 while (psize > 0 || (qsize > 0 && q != NULL))
6552 /* See from which the next case to merge comes from. */
6555 /* P is empty so the next case must come from Q. */
6560 else if (qsize == 0 || q == NULL)
6569 cmp = compare_cases (p, q);
6572 /* The whole case range for P is less than the
6580 /* The whole case range for Q is greater than
6581 the case range for P. */
6588 /* The cases overlap, or they are the same
6589 element in the list. Either way, we must
6590 issue an error and get the next case from P. */
6591 /* FIXME: Sort P and Q by line number. */
6592 gfc_error ("CASE label at %L overlaps with CASE "
6593 "label at %L", &p->where, &q->where);
6601 /* Add the next element to the merged list. */
6610 /* P has now stepped INSIZE places along, and so has Q. So
6611 they're the same. */
6616 /* If we have done only one merge or none at all, we've
6617 finished sorting the cases. */
6626 /* Otherwise repeat, merging lists twice the size. */
6632 /* Check to see if an expression is suitable for use in a CASE statement.
6633 Makes sure that all case expressions are scalar constants of the same
6634 type. Return FAILURE if anything is wrong. */
6637 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
6639 if (e == NULL) return SUCCESS;
6641 if (e->ts.type != case_expr->ts.type)
6643 gfc_error ("Expression in CASE statement at %L must be of type %s",
6644 &e->where, gfc_basic_typename (case_expr->ts.type));
6648 /* C805 (R808) For a given case-construct, each case-value shall be of
6649 the same type as case-expr. For character type, length differences
6650 are allowed, but the kind type parameters shall be the same. */
6652 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
6654 gfc_error ("Expression in CASE statement at %L must be of kind %d",
6655 &e->where, case_expr->ts.kind);
6659 /* Convert the case value kind to that of case expression kind, if needed.
6660 FIXME: Should a warning be issued? */
6661 if (e->ts.kind != case_expr->ts.kind)
6662 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
6666 gfc_error ("Expression in CASE statement at %L must be scalar",
6675 /* Given a completely parsed select statement, we:
6677 - Validate all expressions and code within the SELECT.
6678 - Make sure that the selection expression is not of the wrong type.
6679 - Make sure that no case ranges overlap.
6680 - Eliminate unreachable cases and unreachable code resulting from
6681 removing case labels.
6683 The standard does allow unreachable cases, e.g. CASE (5:3). But
6684 they are a hassle for code generation, and to prevent that, we just
6685 cut them out here. This is not necessary for overlapping cases
6686 because they are illegal and we never even try to generate code.
6688 We have the additional caveat that a SELECT construct could have
6689 been a computed GOTO in the source code. Fortunately we can fairly
6690 easily work around that here: The case_expr for a "real" SELECT CASE
6691 is in code->expr1, but for a computed GOTO it is in code->expr2. All
6692 we have to do is make sure that the case_expr is a scalar integer
6696 resolve_select (gfc_code *code)
6699 gfc_expr *case_expr;
6700 gfc_case *cp, *default_case, *tail, *head;
6701 int seen_unreachable;
6707 if (code->expr1 == NULL)
6709 /* This was actually a computed GOTO statement. */
6710 case_expr = code->expr2;
6711 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
6712 gfc_error ("Selection expression in computed GOTO statement "
6713 "at %L must be a scalar integer expression",
6716 /* Further checking is not necessary because this SELECT was built
6717 by the compiler, so it should always be OK. Just move the
6718 case_expr from expr2 to expr so that we can handle computed
6719 GOTOs as normal SELECTs from here on. */
6720 code->expr1 = code->expr2;
6725 case_expr = code->expr1;
6727 type = case_expr->ts.type;
6728 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
6730 gfc_error ("Argument of SELECT statement at %L cannot be %s",
6731 &case_expr->where, gfc_typename (&case_expr->ts));
6733 /* Punt. Going on here just produce more garbage error messages. */
6737 if (case_expr->rank != 0)
6739 gfc_error ("Argument of SELECT statement at %L must be a scalar "
6740 "expression", &case_expr->where);
6746 /* PR 19168 has a long discussion concerning a mismatch of the kinds
6747 of the SELECT CASE expression and its CASE values. Walk the lists
6748 of case values, and if we find a mismatch, promote case_expr to
6749 the appropriate kind. */
6751 if (type == BT_LOGICAL || type == BT_INTEGER)
6753 for (body = code->block; body; body = body->block)
6755 /* Walk the case label list. */
6756 for (cp = body->ext.case_list; cp; cp = cp->next)
6758 /* Intercept the DEFAULT case. It does not have a kind. */
6759 if (cp->low == NULL && cp->high == NULL)
6762 /* Unreachable case ranges are discarded, so ignore. */
6763 if (cp->low != NULL && cp->high != NULL
6764 && cp->low != cp->high
6765 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6768 /* FIXME: Should a warning be issued? */
6770 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
6771 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
6773 if (cp->high != NULL
6774 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
6775 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
6780 /* Assume there is no DEFAULT case. */
6781 default_case = NULL;
6786 for (body = code->block; body; body = body->block)
6788 /* Assume the CASE list is OK, and all CASE labels can be matched. */
6790 seen_unreachable = 0;
6792 /* Walk the case label list, making sure that all case labels
6794 for (cp = body->ext.case_list; cp; cp = cp->next)
6796 /* Count the number of cases in the whole construct. */
6799 /* Intercept the DEFAULT case. */
6800 if (cp->low == NULL && cp->high == NULL)
6802 if (default_case != NULL)
6804 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6805 "by a second DEFAULT CASE at %L",
6806 &default_case->where, &cp->where);
6817 /* Deal with single value cases and case ranges. Errors are
6818 issued from the validation function. */
6819 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
6820 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
6826 if (type == BT_LOGICAL
6827 && ((cp->low == NULL || cp->high == NULL)
6828 || cp->low != cp->high))
6830 gfc_error ("Logical range in CASE statement at %L is not "
6831 "allowed", &cp->low->where);
6836 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
6839 value = cp->low->value.logical == 0 ? 2 : 1;
6840 if (value & seen_logical)
6842 gfc_error ("constant logical value in CASE statement "
6843 "is repeated at %L",
6848 seen_logical |= value;
6851 if (cp->low != NULL && cp->high != NULL
6852 && cp->low != cp->high
6853 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6855 if (gfc_option.warn_surprising)
6856 gfc_warning ("Range specification at %L can never "
6857 "be matched", &cp->where);
6859 cp->unreachable = 1;
6860 seen_unreachable = 1;
6864 /* If the case range can be matched, it can also overlap with
6865 other cases. To make sure it does not, we put it in a
6866 double linked list here. We sort that with a merge sort
6867 later on to detect any overlapping cases. */
6871 head->right = head->left = NULL;
6876 tail->right->left = tail;
6883 /* It there was a failure in the previous case label, give up
6884 for this case label list. Continue with the next block. */
6888 /* See if any case labels that are unreachable have been seen.
6889 If so, we eliminate them. This is a bit of a kludge because
6890 the case lists for a single case statement (label) is a
6891 single forward linked lists. */
6892 if (seen_unreachable)
6894 /* Advance until the first case in the list is reachable. */
6895 while (body->ext.case_list != NULL
6896 && body->ext.case_list->unreachable)
6898 gfc_case *n = body->ext.case_list;
6899 body->ext.case_list = body->ext.case_list->next;
6901 gfc_free_case_list (n);
6904 /* Strip all other unreachable cases. */
6905 if (body->ext.case_list)
6907 for (cp = body->ext.case_list; cp->next; cp = cp->next)
6909 if (cp->next->unreachable)
6911 gfc_case *n = cp->next;
6912 cp->next = cp->next->next;
6914 gfc_free_case_list (n);
6921 /* See if there were overlapping cases. If the check returns NULL,
6922 there was overlap. In that case we don't do anything. If head
6923 is non-NULL, we prepend the DEFAULT case. The sorted list can
6924 then used during code generation for SELECT CASE constructs with
6925 a case expression of a CHARACTER type. */
6928 head = check_case_overlap (head);
6930 /* Prepend the default_case if it is there. */
6931 if (head != NULL && default_case)
6933 default_case->left = NULL;
6934 default_case->right = head;
6935 head->left = default_case;
6939 /* Eliminate dead blocks that may be the result if we've seen
6940 unreachable case labels for a block. */
6941 for (body = code; body && body->block; body = body->block)
6943 if (body->block->ext.case_list == NULL)
6945 /* Cut the unreachable block from the code chain. */
6946 gfc_code *c = body->block;
6947 body->block = c->block;
6949 /* Kill the dead block, but not the blocks below it. */
6951 gfc_free_statements (c);
6955 /* More than two cases is legal but insane for logical selects.
6956 Issue a warning for it. */
6957 if (gfc_option.warn_surprising && type == BT_LOGICAL
6959 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
6964 /* Check if a derived type is extensible. */
6967 gfc_type_is_extensible (gfc_symbol *sym)
6969 return !(sym->attr.is_bind_c || sym->attr.sequence);
6973 /* Resolve a SELECT TYPE statement. */
6976 resolve_select_type (gfc_code *code)
6978 gfc_symbol *selector_type;
6979 gfc_code *body, *new_st, *if_st, *tail;
6980 gfc_code *class_is = NULL, *default_case = NULL;
6983 char name[GFC_MAX_SYMBOL_LEN];
6991 selector_type = code->expr2->ts.u.derived->components->ts.u.derived;
6993 selector_type = code->expr1->ts.u.derived->components->ts.u.derived;
6995 /* Loop over TYPE IS / CLASS IS cases. */
6996 for (body = code->block; body; body = body->block)
6998 c = body->ext.case_list;
7000 /* Check F03:C815. */
7001 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7002 && !gfc_type_is_extensible (c->ts.u.derived))
7004 gfc_error ("Derived type '%s' at %L must be extensible",
7005 c->ts.u.derived->name, &c->where);
7010 /* Check F03:C816. */
7011 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7012 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
7014 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7015 c->ts.u.derived->name, &c->where, selector_type->name);
7020 /* Intercept the DEFAULT case. */
7021 if (c->ts.type == BT_UNKNOWN)
7023 /* Check F03:C818. */
7026 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7027 "by a second DEFAULT CASE at %L",
7028 &default_case->ext.case_list->where, &c->where);
7033 default_case = body;
7042 /* Insert assignment for selector variable. */
7043 new_st = gfc_get_code ();
7044 new_st->op = EXEC_ASSIGN;
7045 new_st->expr1 = gfc_copy_expr (code->expr1);
7046 new_st->expr2 = gfc_copy_expr (code->expr2);
7050 /* Put SELECT TYPE statement inside a BLOCK. */
7051 new_st = gfc_get_code ();
7052 new_st->op = code->op;
7053 new_st->expr1 = code->expr1;
7054 new_st->expr2 = code->expr2;
7055 new_st->block = code->block;
7059 ns->code->next = new_st;
7060 code->op = EXEC_BLOCK;
7061 code->expr1 = code->expr2 = NULL;
7066 /* Transform to EXEC_SELECT. */
7067 code->op = EXEC_SELECT;
7068 gfc_add_component_ref (code->expr1, "$vptr");
7069 gfc_add_component_ref (code->expr1, "$hash");
7071 /* Loop over TYPE IS / CLASS IS cases. */
7072 for (body = code->block; body; body = body->block)
7074 c = body->ext.case_list;
7076 if (c->ts.type == BT_DERIVED)
7077 c->low = c->high = gfc_int_expr (c->ts.u.derived->hash_value);
7078 else if (c->ts.type == BT_UNKNOWN)
7081 /* Assign temporary to selector. */
7082 if (c->ts.type == BT_CLASS)
7083 sprintf (name, "tmp$class$%s", c->ts.u.derived->name);
7085 sprintf (name, "tmp$type$%s", c->ts.u.derived->name);
7086 st = gfc_find_symtree (ns->sym_root, name);
7087 new_st = gfc_get_code ();
7088 new_st->expr1 = gfc_get_variable_expr (st);
7089 new_st->expr2 = gfc_get_variable_expr (code->expr1->symtree);
7090 if (c->ts.type == BT_DERIVED)
7092 new_st->op = EXEC_POINTER_ASSIGN;
7093 gfc_add_component_ref (new_st->expr2, "$data");
7096 new_st->op = EXEC_POINTER_ASSIGN;
7097 new_st->next = body->next;
7098 body->next = new_st;
7101 /* Take out CLASS IS cases for separate treatment. */
7103 while (body && body->block)
7105 if (body->block->ext.case_list->ts.type == BT_CLASS)
7107 /* Add to class_is list. */
7108 if (class_is == NULL)
7110 class_is = body->block;
7115 for (tail = class_is; tail->block; tail = tail->block) ;
7116 tail->block = body->block;
7119 /* Remove from EXEC_SELECT list. */
7120 body->block = body->block->block;
7133 /* Add a default case to hold the CLASS IS cases. */
7134 for (tail = code; tail->block; tail = tail->block) ;
7135 tail->block = gfc_get_code ();
7137 tail->op = EXEC_SELECT_TYPE;
7138 tail->ext.case_list = gfc_get_case ();
7139 tail->ext.case_list->ts.type = BT_UNKNOWN;
7141 default_case = tail;
7144 /* More than one CLASS IS block? */
7145 if (class_is->block)
7149 /* Sort CLASS IS blocks by extension level. */
7153 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
7156 /* F03:C817 (check for doubles). */
7157 if ((*c1)->ext.case_list->ts.u.derived->hash_value
7158 == c2->ext.case_list->ts.u.derived->hash_value)
7160 gfc_error ("Double CLASS IS block in SELECT TYPE "
7161 "statement at %L", &c2->ext.case_list->where);
7164 if ((*c1)->ext.case_list->ts.u.derived->attr.extension
7165 < c2->ext.case_list->ts.u.derived->attr.extension)
7168 (*c1)->block = c2->block;
7178 /* Generate IF chain. */
7179 if_st = gfc_get_code ();
7180 if_st->op = EXEC_IF;
7182 for (body = class_is; body; body = body->block)
7184 new_st->block = gfc_get_code ();
7185 new_st = new_st->block;
7186 new_st->op = EXEC_IF;
7187 /* Set up IF condition: Call _gfortran_is_extension_of. */
7188 new_st->expr1 = gfc_get_expr ();
7189 new_st->expr1->expr_type = EXPR_FUNCTION;
7190 new_st->expr1->ts.type = BT_LOGICAL;
7191 new_st->expr1->ts.kind = 4;
7192 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
7193 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
7194 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
7195 /* Set up arguments. */
7196 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
7197 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
7198 gfc_add_component_ref (new_st->expr1->value.function.actual->expr, "$vptr");
7199 vtab = gfc_find_derived_vtab (body->ext.case_list->ts.u.derived);
7200 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
7201 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
7202 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
7203 new_st->next = body->next;
7205 if (default_case->next)
7207 new_st->block = gfc_get_code ();
7208 new_st = new_st->block;
7209 new_st->op = EXEC_IF;
7210 new_st->next = default_case->next;
7213 /* Replace CLASS DEFAULT code by the IF chain. */
7214 default_case->next = if_st;
7217 resolve_select (code);
7222 /* Resolve a transfer statement. This is making sure that:
7223 -- a derived type being transferred has only non-pointer components
7224 -- a derived type being transferred doesn't have private components, unless
7225 it's being transferred from the module where the type was defined
7226 -- we're not trying to transfer a whole assumed size array. */
7229 resolve_transfer (gfc_code *code)
7238 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
7241 sym = exp->symtree->n.sym;
7244 /* Go to actual component transferred. */
7245 for (ref = code->expr1->ref; ref; ref = ref->next)
7246 if (ref->type == REF_COMPONENT)
7247 ts = &ref->u.c.component->ts;
7249 if (ts->type == BT_DERIVED)
7251 /* Check that transferred derived type doesn't contain POINTER
7253 if (ts->u.derived->attr.pointer_comp)
7255 gfc_error ("Data transfer element at %L cannot have "
7256 "POINTER components", &code->loc);
7260 if (ts->u.derived->attr.alloc_comp)
7262 gfc_error ("Data transfer element at %L cannot have "
7263 "ALLOCATABLE components", &code->loc);
7267 if (derived_inaccessible (ts->u.derived))
7269 gfc_error ("Data transfer element at %L cannot have "
7270 "PRIVATE components",&code->loc);
7275 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
7276 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
7278 gfc_error ("Data transfer element at %L cannot be a full reference to "
7279 "an assumed-size array", &code->loc);
7285 /*********** Toplevel code resolution subroutines ***********/
7287 /* Find the set of labels that are reachable from this block. We also
7288 record the last statement in each block. */
7291 find_reachable_labels (gfc_code *block)
7298 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
7300 /* Collect labels in this block. We don't keep those corresponding
7301 to END {IF|SELECT}, these are checked in resolve_branch by going
7302 up through the code_stack. */
7303 for (c = block; c; c = c->next)
7305 if (c->here && c->op != EXEC_END_BLOCK)
7306 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
7309 /* Merge with labels from parent block. */
7312 gcc_assert (cs_base->prev->reachable_labels);
7313 bitmap_ior_into (cs_base->reachable_labels,
7314 cs_base->prev->reachable_labels);
7320 resolve_sync (gfc_code *code)
7322 /* Check imageset. The * case matches expr1 == NULL. */
7325 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
7326 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
7327 "INTEGER expression", &code->expr1->where);
7328 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
7329 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
7330 gfc_error ("Imageset argument at %L must between 1 and num_images()",
7331 &code->expr1->where);
7332 else if (code->expr1->expr_type == EXPR_ARRAY
7333 && gfc_simplify_expr (code->expr1, 0) == SUCCESS)
7335 gfc_constructor *cons;
7336 for (cons = code->expr1->value.constructor; cons; cons = cons->next)
7337 if (cons->expr->expr_type == EXPR_CONSTANT
7338 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
7339 gfc_error ("Imageset argument at %L must between 1 and "
7340 "num_images()", &cons->expr->where);
7346 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
7347 || code->expr2->expr_type != EXPR_VARIABLE))
7348 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
7349 &code->expr2->where);
7353 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
7354 || code->expr3->expr_type != EXPR_VARIABLE))
7355 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
7356 &code->expr3->where);
7360 /* Given a branch to a label, see if the branch is conforming.
7361 The code node describes where the branch is located. */
7364 resolve_branch (gfc_st_label *label, gfc_code *code)
7371 /* Step one: is this a valid branching target? */
7373 if (label->defined == ST_LABEL_UNKNOWN)
7375 gfc_error ("Label %d referenced at %L is never defined", label->value,
7380 if (label->defined != ST_LABEL_TARGET)
7382 gfc_error ("Statement at %L is not a valid branch target statement "
7383 "for the branch statement at %L", &label->where, &code->loc);
7387 /* Step two: make sure this branch is not a branch to itself ;-) */
7389 if (code->here == label)
7391 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
7395 /* Step three: See if the label is in the same block as the
7396 branching statement. The hard work has been done by setting up
7397 the bitmap reachable_labels. */
7399 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
7401 /* Check now whether there is a CRITICAL construct; if so, check
7402 whether the label is still visible outside of the CRITICAL block,
7403 which is invalid. */
7404 for (stack = cs_base; stack; stack = stack->prev)
7405 if (stack->current->op == EXEC_CRITICAL
7406 && bitmap_bit_p (stack->reachable_labels, label->value))
7407 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
7408 " at %L", &code->loc, &label->where);
7413 /* Step four: If we haven't found the label in the bitmap, it may
7414 still be the label of the END of the enclosing block, in which
7415 case we find it by going up the code_stack. */
7417 for (stack = cs_base; stack; stack = stack->prev)
7419 if (stack->current->next && stack->current->next->here == label)
7421 if (stack->current->op == EXEC_CRITICAL)
7423 /* Note: A label at END CRITICAL does not leave the CRITICAL
7424 construct as END CRITICAL is still part of it. */
7425 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
7426 " at %L", &code->loc, &label->where);
7433 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
7437 /* The label is not in an enclosing block, so illegal. This was
7438 allowed in Fortran 66, so we allow it as extension. No
7439 further checks are necessary in this case. */
7440 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
7441 "as the GOTO statement at %L", &label->where,
7447 /* Check whether EXPR1 has the same shape as EXPR2. */
7450 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
7452 mpz_t shape[GFC_MAX_DIMENSIONS];
7453 mpz_t shape2[GFC_MAX_DIMENSIONS];
7454 gfc_try result = FAILURE;
7457 /* Compare the rank. */
7458 if (expr1->rank != expr2->rank)
7461 /* Compare the size of each dimension. */
7462 for (i=0; i<expr1->rank; i++)
7464 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
7467 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
7470 if (mpz_cmp (shape[i], shape2[i]))
7474 /* When either of the two expression is an assumed size array, we
7475 ignore the comparison of dimension sizes. */
7480 for (i--; i >= 0; i--)
7482 mpz_clear (shape[i]);
7483 mpz_clear (shape2[i]);
7489 /* Check whether a WHERE assignment target or a WHERE mask expression
7490 has the same shape as the outmost WHERE mask expression. */
7493 resolve_where (gfc_code *code, gfc_expr *mask)
7499 cblock = code->block;
7501 /* Store the first WHERE mask-expr of the WHERE statement or construct.
7502 In case of nested WHERE, only the outmost one is stored. */
7503 if (mask == NULL) /* outmost WHERE */
7505 else /* inner WHERE */
7512 /* Check if the mask-expr has a consistent shape with the
7513 outmost WHERE mask-expr. */
7514 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
7515 gfc_error ("WHERE mask at %L has inconsistent shape",
7516 &cblock->expr1->where);
7519 /* the assignment statement of a WHERE statement, or the first
7520 statement in where-body-construct of a WHERE construct */
7521 cnext = cblock->next;
7526 /* WHERE assignment statement */
7529 /* Check shape consistent for WHERE assignment target. */
7530 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
7531 gfc_error ("WHERE assignment target at %L has "
7532 "inconsistent shape", &cnext->expr1->where);
7536 case EXEC_ASSIGN_CALL:
7537 resolve_call (cnext);
7538 if (!cnext->resolved_sym->attr.elemental)
7539 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7540 &cnext->ext.actual->expr->where);
7543 /* WHERE or WHERE construct is part of a where-body-construct */
7545 resolve_where (cnext, e);
7549 gfc_error ("Unsupported statement inside WHERE at %L",
7552 /* the next statement within the same where-body-construct */
7553 cnext = cnext->next;
7555 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7556 cblock = cblock->block;
7561 /* Resolve assignment in FORALL construct.
7562 NVAR is the number of FORALL index variables, and VAR_EXPR records the
7563 FORALL index variables. */
7566 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
7570 for (n = 0; n < nvar; n++)
7572 gfc_symbol *forall_index;
7574 forall_index = var_expr[n]->symtree->n.sym;
7576 /* Check whether the assignment target is one of the FORALL index
7578 if ((code->expr1->expr_type == EXPR_VARIABLE)
7579 && (code->expr1->symtree->n.sym == forall_index))
7580 gfc_error ("Assignment to a FORALL index variable at %L",
7581 &code->expr1->where);
7584 /* If one of the FORALL index variables doesn't appear in the
7585 assignment variable, then there could be a many-to-one
7586 assignment. Emit a warning rather than an error because the
7587 mask could be resolving this problem. */
7588 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
7589 gfc_warning ("The FORALL with index '%s' is not used on the "
7590 "left side of the assignment at %L and so might "
7591 "cause multiple assignment to this object",
7592 var_expr[n]->symtree->name, &code->expr1->where);
7598 /* Resolve WHERE statement in FORALL construct. */
7601 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
7602 gfc_expr **var_expr)
7607 cblock = code->block;
7610 /* the assignment statement of a WHERE statement, or the first
7611 statement in where-body-construct of a WHERE construct */
7612 cnext = cblock->next;
7617 /* WHERE assignment statement */
7619 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
7622 /* WHERE operator assignment statement */
7623 case EXEC_ASSIGN_CALL:
7624 resolve_call (cnext);
7625 if (!cnext->resolved_sym->attr.elemental)
7626 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7627 &cnext->ext.actual->expr->where);
7630 /* WHERE or WHERE construct is part of a where-body-construct */
7632 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
7636 gfc_error ("Unsupported statement inside WHERE at %L",
7639 /* the next statement within the same where-body-construct */
7640 cnext = cnext->next;
7642 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7643 cblock = cblock->block;
7648 /* Traverse the FORALL body to check whether the following errors exist:
7649 1. For assignment, check if a many-to-one assignment happens.
7650 2. For WHERE statement, check the WHERE body to see if there is any
7651 many-to-one assignment. */
7654 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
7658 c = code->block->next;
7664 case EXEC_POINTER_ASSIGN:
7665 gfc_resolve_assign_in_forall (c, nvar, var_expr);
7668 case EXEC_ASSIGN_CALL:
7672 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
7673 there is no need to handle it here. */
7677 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
7682 /* The next statement in the FORALL body. */
7688 /* Counts the number of iterators needed inside a forall construct, including
7689 nested forall constructs. This is used to allocate the needed memory
7690 in gfc_resolve_forall. */
7693 gfc_count_forall_iterators (gfc_code *code)
7695 int max_iters, sub_iters, current_iters;
7696 gfc_forall_iterator *fa;
7698 gcc_assert(code->op == EXEC_FORALL);
7702 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7705 code = code->block->next;
7709 if (code->op == EXEC_FORALL)
7711 sub_iters = gfc_count_forall_iterators (code);
7712 if (sub_iters > max_iters)
7713 max_iters = sub_iters;
7718 return current_iters + max_iters;
7722 /* Given a FORALL construct, first resolve the FORALL iterator, then call
7723 gfc_resolve_forall_body to resolve the FORALL body. */
7726 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
7728 static gfc_expr **var_expr;
7729 static int total_var = 0;
7730 static int nvar = 0;
7732 gfc_forall_iterator *fa;
7737 /* Start to resolve a FORALL construct */
7738 if (forall_save == 0)
7740 /* Count the total number of FORALL index in the nested FORALL
7741 construct in order to allocate the VAR_EXPR with proper size. */
7742 total_var = gfc_count_forall_iterators (code);
7744 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
7745 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
7748 /* The information about FORALL iterator, including FORALL index start, end
7749 and stride. The FORALL index can not appear in start, end or stride. */
7750 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7752 /* Check if any outer FORALL index name is the same as the current
7754 for (i = 0; i < nvar; i++)
7756 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
7758 gfc_error ("An outer FORALL construct already has an index "
7759 "with this name %L", &fa->var->where);
7763 /* Record the current FORALL index. */
7764 var_expr[nvar] = gfc_copy_expr (fa->var);
7768 /* No memory leak. */
7769 gcc_assert (nvar <= total_var);
7772 /* Resolve the FORALL body. */
7773 gfc_resolve_forall_body (code, nvar, var_expr);
7775 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
7776 gfc_resolve_blocks (code->block, ns);
7780 /* Free only the VAR_EXPRs allocated in this frame. */
7781 for (i = nvar; i < tmp; i++)
7782 gfc_free_expr (var_expr[i]);
7786 /* We are in the outermost FORALL construct. */
7787 gcc_assert (forall_save == 0);
7789 /* VAR_EXPR is not needed any more. */
7790 gfc_free (var_expr);
7796 /* Resolve a BLOCK construct statement. */
7799 resolve_block_construct (gfc_code* code)
7801 /* Eventually, we may want to do some checks here or handle special stuff.
7802 But so far the only thing we can do is resolving the local namespace. */
7804 gfc_resolve (code->ext.ns);
7808 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
7811 static void resolve_code (gfc_code *, gfc_namespace *);
7814 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
7818 for (; b; b = b->block)
7820 t = gfc_resolve_expr (b->expr1);
7821 if (gfc_resolve_expr (b->expr2) == FAILURE)
7827 if (t == SUCCESS && b->expr1 != NULL
7828 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
7829 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
7836 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
7837 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
7842 resolve_branch (b->label1, b);
7846 resolve_block_construct (b);
7850 case EXEC_SELECT_TYPE:
7861 case EXEC_OMP_ATOMIC:
7862 case EXEC_OMP_CRITICAL:
7864 case EXEC_OMP_MASTER:
7865 case EXEC_OMP_ORDERED:
7866 case EXEC_OMP_PARALLEL:
7867 case EXEC_OMP_PARALLEL_DO:
7868 case EXEC_OMP_PARALLEL_SECTIONS:
7869 case EXEC_OMP_PARALLEL_WORKSHARE:
7870 case EXEC_OMP_SECTIONS:
7871 case EXEC_OMP_SINGLE:
7873 case EXEC_OMP_TASKWAIT:
7874 case EXEC_OMP_WORKSHARE:
7878 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
7881 resolve_code (b->next, ns);
7886 /* Does everything to resolve an ordinary assignment. Returns true
7887 if this is an interface assignment. */
7889 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
7899 if (gfc_extend_assign (code, ns) == SUCCESS)
7903 if (code->op == EXEC_ASSIGN_CALL)
7905 lhs = code->ext.actual->expr;
7906 rhsptr = &code->ext.actual->next->expr;
7910 gfc_actual_arglist* args;
7911 gfc_typebound_proc* tbp;
7913 gcc_assert (code->op == EXEC_COMPCALL);
7915 args = code->expr1->value.compcall.actual;
7917 rhsptr = &args->next->expr;
7919 tbp = code->expr1->value.compcall.tbp;
7920 gcc_assert (!tbp->is_generic);
7923 /* Make a temporary rhs when there is a default initializer
7924 and rhs is the same symbol as the lhs. */
7925 if ((*rhsptr)->expr_type == EXPR_VARIABLE
7926 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
7927 && has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
7928 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
7929 *rhsptr = gfc_get_parentheses (*rhsptr);
7938 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
7939 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
7940 &code->loc) == FAILURE)
7943 /* Handle the case of a BOZ literal on the RHS. */
7944 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
7947 if (gfc_option.warn_surprising)
7948 gfc_warning ("BOZ literal at %L is bitwise transferred "
7949 "non-integer symbol '%s'", &code->loc,
7950 lhs->symtree->n.sym->name);
7952 if (!gfc_convert_boz (rhs, &lhs->ts))
7954 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
7956 if (rc == ARITH_UNDERFLOW)
7957 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
7958 ". This check can be disabled with the option "
7959 "-fno-range-check", &rhs->where);
7960 else if (rc == ARITH_OVERFLOW)
7961 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
7962 ". This check can be disabled with the option "
7963 "-fno-range-check", &rhs->where);
7964 else if (rc == ARITH_NAN)
7965 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
7966 ". This check can be disabled with the option "
7967 "-fno-range-check", &rhs->where);
7973 if (lhs->ts.type == BT_CHARACTER
7974 && gfc_option.warn_character_truncation)
7976 if (lhs->ts.u.cl != NULL
7977 && lhs->ts.u.cl->length != NULL
7978 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7979 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
7981 if (rhs->expr_type == EXPR_CONSTANT)
7982 rlen = rhs->value.character.length;
7984 else if (rhs->ts.u.cl != NULL
7985 && rhs->ts.u.cl->length != NULL
7986 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7987 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
7989 if (rlen && llen && rlen > llen)
7990 gfc_warning_now ("CHARACTER expression will be truncated "
7991 "in assignment (%d/%d) at %L",
7992 llen, rlen, &code->loc);
7995 /* Ensure that a vector index expression for the lvalue is evaluated
7996 to a temporary if the lvalue symbol is referenced in it. */
7999 for (ref = lhs->ref; ref; ref= ref->next)
8000 if (ref->type == REF_ARRAY)
8002 for (n = 0; n < ref->u.ar.dimen; n++)
8003 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
8004 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
8005 ref->u.ar.start[n]))
8007 = gfc_get_parentheses (ref->u.ar.start[n]);
8011 if (gfc_pure (NULL))
8013 if (gfc_impure_variable (lhs->symtree->n.sym))
8015 gfc_error ("Cannot assign to variable '%s' in PURE "
8017 lhs->symtree->n.sym->name,
8022 if (lhs->ts.type == BT_DERIVED
8023 && lhs->expr_type == EXPR_VARIABLE
8024 && lhs->ts.u.derived->attr.pointer_comp
8025 && rhs->expr_type == EXPR_VARIABLE
8026 && gfc_impure_variable (rhs->symtree->n.sym))
8028 gfc_error ("The impure variable at %L is assigned to "
8029 "a derived type variable with a POINTER "
8030 "component in a PURE procedure (12.6)",
8037 if (lhs->ts.type == BT_CLASS)
8039 gfc_error ("Variable must not be polymorphic in assignment at %L",
8044 gfc_check_assign (lhs, rhs, 1);
8049 /* Given a block of code, recursively resolve everything pointed to by this
8053 resolve_code (gfc_code *code, gfc_namespace *ns)
8055 int omp_workshare_save;
8060 frame.prev = cs_base;
8064 find_reachable_labels (code);
8066 for (; code; code = code->next)
8068 frame.current = code;
8069 forall_save = forall_flag;
8071 if (code->op == EXEC_FORALL)
8074 gfc_resolve_forall (code, ns, forall_save);
8077 else if (code->block)
8079 omp_workshare_save = -1;
8082 case EXEC_OMP_PARALLEL_WORKSHARE:
8083 omp_workshare_save = omp_workshare_flag;
8084 omp_workshare_flag = 1;
8085 gfc_resolve_omp_parallel_blocks (code, ns);
8087 case EXEC_OMP_PARALLEL:
8088 case EXEC_OMP_PARALLEL_DO:
8089 case EXEC_OMP_PARALLEL_SECTIONS:
8091 omp_workshare_save = omp_workshare_flag;
8092 omp_workshare_flag = 0;
8093 gfc_resolve_omp_parallel_blocks (code, ns);
8096 gfc_resolve_omp_do_blocks (code, ns);
8098 case EXEC_SELECT_TYPE:
8099 gfc_current_ns = code->ext.ns;
8100 gfc_resolve_blocks (code->block, gfc_current_ns);
8101 gfc_current_ns = ns;
8103 case EXEC_OMP_WORKSHARE:
8104 omp_workshare_save = omp_workshare_flag;
8105 omp_workshare_flag = 1;
8108 gfc_resolve_blocks (code->block, ns);
8112 if (omp_workshare_save != -1)
8113 omp_workshare_flag = omp_workshare_save;
8117 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
8118 t = gfc_resolve_expr (code->expr1);
8119 forall_flag = forall_save;
8121 if (gfc_resolve_expr (code->expr2) == FAILURE)
8124 if (code->op == EXEC_ALLOCATE
8125 && gfc_resolve_expr (code->expr3) == FAILURE)
8131 case EXEC_END_BLOCK:
8135 case EXEC_ERROR_STOP:
8139 case EXEC_ASSIGN_CALL:
8144 case EXEC_SYNC_IMAGES:
8145 case EXEC_SYNC_MEMORY:
8146 resolve_sync (code);
8150 /* Keep track of which entry we are up to. */
8151 current_entry_id = code->ext.entry->id;
8155 resolve_where (code, NULL);
8159 if (code->expr1 != NULL)
8161 if (code->expr1->ts.type != BT_INTEGER)
8162 gfc_error ("ASSIGNED GOTO statement at %L requires an "
8163 "INTEGER variable", &code->expr1->where);
8164 else if (code->expr1->symtree->n.sym->attr.assign != 1)
8165 gfc_error ("Variable '%s' has not been assigned a target "
8166 "label at %L", code->expr1->symtree->n.sym->name,
8167 &code->expr1->where);
8170 resolve_branch (code->label1, code);
8174 if (code->expr1 != NULL
8175 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
8176 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
8177 "INTEGER return specifier", &code->expr1->where);
8180 case EXEC_INIT_ASSIGN:
8181 case EXEC_END_PROCEDURE:
8188 if (resolve_ordinary_assign (code, ns))
8190 if (code->op == EXEC_COMPCALL)
8197 case EXEC_LABEL_ASSIGN:
8198 if (code->label1->defined == ST_LABEL_UNKNOWN)
8199 gfc_error ("Label %d referenced at %L is never defined",
8200 code->label1->value, &code->label1->where);
8202 && (code->expr1->expr_type != EXPR_VARIABLE
8203 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
8204 || code->expr1->symtree->n.sym->ts.kind
8205 != gfc_default_integer_kind
8206 || code->expr1->symtree->n.sym->as != NULL))
8207 gfc_error ("ASSIGN statement at %L requires a scalar "
8208 "default INTEGER variable", &code->expr1->where);
8211 case EXEC_POINTER_ASSIGN:
8215 gfc_check_pointer_assign (code->expr1, code->expr2);
8218 case EXEC_ARITHMETIC_IF:
8220 && code->expr1->ts.type != BT_INTEGER
8221 && code->expr1->ts.type != BT_REAL)
8222 gfc_error ("Arithmetic IF statement at %L requires a numeric "
8223 "expression", &code->expr1->where);
8225 resolve_branch (code->label1, code);
8226 resolve_branch (code->label2, code);
8227 resolve_branch (code->label3, code);
8231 if (t == SUCCESS && code->expr1 != NULL
8232 && (code->expr1->ts.type != BT_LOGICAL
8233 || code->expr1->rank != 0))
8234 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8235 &code->expr1->where);
8240 resolve_call (code);
8245 resolve_typebound_subroutine (code);
8249 resolve_ppc_call (code);
8253 /* Select is complicated. Also, a SELECT construct could be
8254 a transformed computed GOTO. */
8255 resolve_select (code);
8258 case EXEC_SELECT_TYPE:
8259 resolve_select_type (code);
8263 gfc_resolve (code->ext.ns);
8267 if (code->ext.iterator != NULL)
8269 gfc_iterator *iter = code->ext.iterator;
8270 if (gfc_resolve_iterator (iter, true) != FAILURE)
8271 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
8276 if (code->expr1 == NULL)
8277 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
8279 && (code->expr1->rank != 0
8280 || code->expr1->ts.type != BT_LOGICAL))
8281 gfc_error ("Exit condition of DO WHILE loop at %L must be "
8282 "a scalar LOGICAL expression", &code->expr1->where);
8287 resolve_allocate_deallocate (code, "ALLOCATE");
8291 case EXEC_DEALLOCATE:
8293 resolve_allocate_deallocate (code, "DEALLOCATE");
8298 if (gfc_resolve_open (code->ext.open) == FAILURE)
8301 resolve_branch (code->ext.open->err, code);
8305 if (gfc_resolve_close (code->ext.close) == FAILURE)
8308 resolve_branch (code->ext.close->err, code);
8311 case EXEC_BACKSPACE:
8315 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
8318 resolve_branch (code->ext.filepos->err, code);
8322 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8325 resolve_branch (code->ext.inquire->err, code);
8329 gcc_assert (code->ext.inquire != NULL);
8330 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8333 resolve_branch (code->ext.inquire->err, code);
8337 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
8340 resolve_branch (code->ext.wait->err, code);
8341 resolve_branch (code->ext.wait->end, code);
8342 resolve_branch (code->ext.wait->eor, code);
8347 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
8350 resolve_branch (code->ext.dt->err, code);
8351 resolve_branch (code->ext.dt->end, code);
8352 resolve_branch (code->ext.dt->eor, code);
8356 resolve_transfer (code);
8360 resolve_forall_iterators (code->ext.forall_iterator);
8362 if (code->expr1 != NULL && code->expr1->ts.type != BT_LOGICAL)
8363 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
8364 "expression", &code->expr1->where);
8367 case EXEC_OMP_ATOMIC:
8368 case EXEC_OMP_BARRIER:
8369 case EXEC_OMP_CRITICAL:
8370 case EXEC_OMP_FLUSH:
8372 case EXEC_OMP_MASTER:
8373 case EXEC_OMP_ORDERED:
8374 case EXEC_OMP_SECTIONS:
8375 case EXEC_OMP_SINGLE:
8376 case EXEC_OMP_TASKWAIT:
8377 case EXEC_OMP_WORKSHARE:
8378 gfc_resolve_omp_directive (code, ns);
8381 case EXEC_OMP_PARALLEL:
8382 case EXEC_OMP_PARALLEL_DO:
8383 case EXEC_OMP_PARALLEL_SECTIONS:
8384 case EXEC_OMP_PARALLEL_WORKSHARE:
8386 omp_workshare_save = omp_workshare_flag;
8387 omp_workshare_flag = 0;
8388 gfc_resolve_omp_directive (code, ns);
8389 omp_workshare_flag = omp_workshare_save;
8393 gfc_internal_error ("resolve_code(): Bad statement code");
8397 cs_base = frame.prev;
8401 /* Resolve initial values and make sure they are compatible with
8405 resolve_values (gfc_symbol *sym)
8407 if (sym->value == NULL)
8410 if (gfc_resolve_expr (sym->value) == FAILURE)
8413 gfc_check_assign_symbol (sym, sym->value);
8417 /* Verify the binding labels for common blocks that are BIND(C). The label
8418 for a BIND(C) common block must be identical in all scoping units in which
8419 the common block is declared. Further, the binding label can not collide
8420 with any other global entity in the program. */
8423 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
8425 if (comm_block_tree->n.common->is_bind_c == 1)
8427 gfc_gsymbol *binding_label_gsym;
8428 gfc_gsymbol *comm_name_gsym;
8430 /* See if a global symbol exists by the common block's name. It may
8431 be NULL if the common block is use-associated. */
8432 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
8433 comm_block_tree->n.common->name);
8434 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
8435 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
8436 "with the global entity '%s' at %L",
8437 comm_block_tree->n.common->binding_label,
8438 comm_block_tree->n.common->name,
8439 &(comm_block_tree->n.common->where),
8440 comm_name_gsym->name, &(comm_name_gsym->where));
8441 else if (comm_name_gsym != NULL
8442 && strcmp (comm_name_gsym->name,
8443 comm_block_tree->n.common->name) == 0)
8445 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
8447 if (comm_name_gsym->binding_label == NULL)
8448 /* No binding label for common block stored yet; save this one. */
8449 comm_name_gsym->binding_label =
8450 comm_block_tree->n.common->binding_label;
8452 if (strcmp (comm_name_gsym->binding_label,
8453 comm_block_tree->n.common->binding_label) != 0)
8455 /* Common block names match but binding labels do not. */
8456 gfc_error ("Binding label '%s' for common block '%s' at %L "
8457 "does not match the binding label '%s' for common "
8459 comm_block_tree->n.common->binding_label,
8460 comm_block_tree->n.common->name,
8461 &(comm_block_tree->n.common->where),
8462 comm_name_gsym->binding_label,
8463 comm_name_gsym->name,
8464 &(comm_name_gsym->where));
8469 /* There is no binding label (NAME="") so we have nothing further to
8470 check and nothing to add as a global symbol for the label. */
8471 if (comm_block_tree->n.common->binding_label[0] == '\0' )
8474 binding_label_gsym =
8475 gfc_find_gsymbol (gfc_gsym_root,
8476 comm_block_tree->n.common->binding_label);
8477 if (binding_label_gsym == NULL)
8479 /* Need to make a global symbol for the binding label to prevent
8480 it from colliding with another. */
8481 binding_label_gsym =
8482 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
8483 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
8484 binding_label_gsym->type = GSYM_COMMON;
8488 /* If comm_name_gsym is NULL, the name common block is use
8489 associated and the name could be colliding. */
8490 if (binding_label_gsym->type != GSYM_COMMON)
8491 gfc_error ("Binding label '%s' for common block '%s' at %L "
8492 "collides with the global entity '%s' at %L",
8493 comm_block_tree->n.common->binding_label,
8494 comm_block_tree->n.common->name,
8495 &(comm_block_tree->n.common->where),
8496 binding_label_gsym->name,
8497 &(binding_label_gsym->where));
8498 else if (comm_name_gsym != NULL
8499 && (strcmp (binding_label_gsym->name,
8500 comm_name_gsym->binding_label) != 0)
8501 && (strcmp (binding_label_gsym->sym_name,
8502 comm_name_gsym->name) != 0))
8503 gfc_error ("Binding label '%s' for common block '%s' at %L "
8504 "collides with global entity '%s' at %L",
8505 binding_label_gsym->name, binding_label_gsym->sym_name,
8506 &(comm_block_tree->n.common->where),
8507 comm_name_gsym->name, &(comm_name_gsym->where));
8515 /* Verify any BIND(C) derived types in the namespace so we can report errors
8516 for them once, rather than for each variable declared of that type. */
8519 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
8521 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
8522 && derived_sym->attr.is_bind_c == 1)
8523 verify_bind_c_derived_type (derived_sym);
8529 /* Verify that any binding labels used in a given namespace do not collide
8530 with the names or binding labels of any global symbols. */
8533 gfc_verify_binding_labels (gfc_symbol *sym)
8537 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
8538 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
8540 gfc_gsymbol *bind_c_sym;
8542 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
8543 if (bind_c_sym != NULL
8544 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
8546 if (sym->attr.if_source == IFSRC_DECL
8547 && (bind_c_sym->type != GSYM_SUBROUTINE
8548 && bind_c_sym->type != GSYM_FUNCTION)
8549 && ((sym->attr.contained == 1
8550 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
8551 || (sym->attr.use_assoc == 1
8552 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
8554 /* Make sure global procedures don't collide with anything. */
8555 gfc_error ("Binding label '%s' at %L collides with the global "
8556 "entity '%s' at %L", sym->binding_label,
8557 &(sym->declared_at), bind_c_sym->name,
8558 &(bind_c_sym->where));
8561 else if (sym->attr.contained == 0
8562 && (sym->attr.if_source == IFSRC_IFBODY
8563 && sym->attr.flavor == FL_PROCEDURE)
8564 && (bind_c_sym->sym_name != NULL
8565 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
8567 /* Make sure procedures in interface bodies don't collide. */
8568 gfc_error ("Binding label '%s' in interface body at %L collides "
8569 "with the global entity '%s' at %L",
8571 &(sym->declared_at), bind_c_sym->name,
8572 &(bind_c_sym->where));
8575 else if (sym->attr.contained == 0
8576 && sym->attr.if_source == IFSRC_UNKNOWN)
8577 if ((sym->attr.use_assoc && bind_c_sym->mod_name
8578 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
8579 || sym->attr.use_assoc == 0)
8581 gfc_error ("Binding label '%s' at %L collides with global "
8582 "entity '%s' at %L", sym->binding_label,
8583 &(sym->declared_at), bind_c_sym->name,
8584 &(bind_c_sym->where));
8589 /* Clear the binding label to prevent checking multiple times. */
8590 sym->binding_label[0] = '\0';
8592 else if (bind_c_sym == NULL)
8594 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
8595 bind_c_sym->where = sym->declared_at;
8596 bind_c_sym->sym_name = sym->name;
8598 if (sym->attr.use_assoc == 1)
8599 bind_c_sym->mod_name = sym->module;
8601 if (sym->ns->proc_name != NULL)
8602 bind_c_sym->mod_name = sym->ns->proc_name->name;
8604 if (sym->attr.contained == 0)
8606 if (sym->attr.subroutine)
8607 bind_c_sym->type = GSYM_SUBROUTINE;
8608 else if (sym->attr.function)
8609 bind_c_sym->type = GSYM_FUNCTION;
8617 /* Resolve an index expression. */
8620 resolve_index_expr (gfc_expr *e)
8622 if (gfc_resolve_expr (e) == FAILURE)
8625 if (gfc_simplify_expr (e, 0) == FAILURE)
8628 if (gfc_specification_expr (e) == FAILURE)
8634 /* Resolve a charlen structure. */
8637 resolve_charlen (gfc_charlen *cl)
8646 specification_expr = 1;
8648 if (resolve_index_expr (cl->length) == FAILURE)
8650 specification_expr = 0;
8654 /* "If the character length parameter value evaluates to a negative
8655 value, the length of character entities declared is zero." */
8656 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
8658 if (gfc_option.warn_surprising)
8659 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
8660 " the length has been set to zero",
8661 &cl->length->where, i);
8662 gfc_replace_expr (cl->length, gfc_int_expr (0));
8665 /* Check that the character length is not too large. */
8666 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
8667 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
8668 && cl->length->ts.type == BT_INTEGER
8669 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
8671 gfc_error ("String length at %L is too large", &cl->length->where);
8679 /* Test for non-constant shape arrays. */
8682 is_non_constant_shape_array (gfc_symbol *sym)
8688 not_constant = false;
8689 if (sym->as != NULL)
8691 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
8692 has not been simplified; parameter array references. Do the
8693 simplification now. */
8694 for (i = 0; i < sym->as->rank; i++)
8696 e = sym->as->lower[i];
8697 if (e && (resolve_index_expr (e) == FAILURE
8698 || !gfc_is_constant_expr (e)))
8699 not_constant = true;
8701 e = sym->as->upper[i];
8702 if (e && (resolve_index_expr (e) == FAILURE
8703 || !gfc_is_constant_expr (e)))
8704 not_constant = true;
8707 return not_constant;
8710 /* Given a symbol and an initialization expression, add code to initialize
8711 the symbol to the function entry. */
8713 build_init_assign (gfc_symbol *sym, gfc_expr *init)
8717 gfc_namespace *ns = sym->ns;
8719 /* Search for the function namespace if this is a contained
8720 function without an explicit result. */
8721 if (sym->attr.function && sym == sym->result
8722 && sym->name != sym->ns->proc_name->name)
8725 for (;ns; ns = ns->sibling)
8726 if (strcmp (ns->proc_name->name, sym->name) == 0)
8732 gfc_free_expr (init);
8736 /* Build an l-value expression for the result. */
8737 lval = gfc_lval_expr_from_sym (sym);
8739 /* Add the code at scope entry. */
8740 init_st = gfc_get_code ();
8741 init_st->next = ns->code;
8744 /* Assign the default initializer to the l-value. */
8745 init_st->loc = sym->declared_at;
8746 init_st->op = EXEC_INIT_ASSIGN;
8747 init_st->expr1 = lval;
8748 init_st->expr2 = init;
8751 /* Assign the default initializer to a derived type variable or result. */
8754 apply_default_init (gfc_symbol *sym)
8756 gfc_expr *init = NULL;
8758 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
8761 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
8762 init = gfc_default_initializer (&sym->ts);
8767 build_init_assign (sym, init);
8770 /* Build an initializer for a local integer, real, complex, logical, or
8771 character variable, based on the command line flags finit-local-zero,
8772 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
8773 null if the symbol should not have a default initialization. */
8775 build_default_init_expr (gfc_symbol *sym)
8778 gfc_expr *init_expr;
8781 /* These symbols should never have a default initialization. */
8782 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
8783 || sym->attr.external
8785 || sym->attr.pointer
8786 || sym->attr.in_equivalence
8787 || sym->attr.in_common
8790 || sym->attr.cray_pointee
8791 || sym->attr.cray_pointer)
8794 /* Now we'll try to build an initializer expression. */
8795 init_expr = gfc_get_expr ();
8796 init_expr->expr_type = EXPR_CONSTANT;
8797 init_expr->ts.type = sym->ts.type;
8798 init_expr->ts.kind = sym->ts.kind;
8799 init_expr->where = sym->declared_at;
8801 /* We will only initialize integers, reals, complex, logicals, and
8802 characters, and only if the corresponding command-line flags
8803 were set. Otherwise, we free init_expr and return null. */
8804 switch (sym->ts.type)
8807 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
8808 mpz_init_set_si (init_expr->value.integer,
8809 gfc_option.flag_init_integer_value);
8812 gfc_free_expr (init_expr);
8818 mpfr_init (init_expr->value.real);
8819 switch (gfc_option.flag_init_real)
8821 case GFC_INIT_REAL_SNAN:
8822 init_expr->is_snan = 1;
8824 case GFC_INIT_REAL_NAN:
8825 mpfr_set_nan (init_expr->value.real);
8828 case GFC_INIT_REAL_INF:
8829 mpfr_set_inf (init_expr->value.real, 1);
8832 case GFC_INIT_REAL_NEG_INF:
8833 mpfr_set_inf (init_expr->value.real, -1);
8836 case GFC_INIT_REAL_ZERO:
8837 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
8841 gfc_free_expr (init_expr);
8848 mpc_init2 (init_expr->value.complex, mpfr_get_default_prec());
8849 switch (gfc_option.flag_init_real)
8851 case GFC_INIT_REAL_SNAN:
8852 init_expr->is_snan = 1;
8854 case GFC_INIT_REAL_NAN:
8855 mpfr_set_nan (mpc_realref (init_expr->value.complex));
8856 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
8859 case GFC_INIT_REAL_INF:
8860 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
8861 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
8864 case GFC_INIT_REAL_NEG_INF:
8865 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
8866 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
8869 case GFC_INIT_REAL_ZERO:
8870 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
8874 gfc_free_expr (init_expr);
8881 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
8882 init_expr->value.logical = 0;
8883 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
8884 init_expr->value.logical = 1;
8887 gfc_free_expr (init_expr);
8893 /* For characters, the length must be constant in order to
8894 create a default initializer. */
8895 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
8896 && sym->ts.u.cl->length
8897 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8899 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
8900 init_expr->value.character.length = char_len;
8901 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
8902 for (i = 0; i < char_len; i++)
8903 init_expr->value.character.string[i]
8904 = (unsigned char) gfc_option.flag_init_character_value;
8908 gfc_free_expr (init_expr);
8914 gfc_free_expr (init_expr);
8920 /* Add an initialization expression to a local variable. */
8922 apply_default_init_local (gfc_symbol *sym)
8924 gfc_expr *init = NULL;
8926 /* The symbol should be a variable or a function return value. */
8927 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
8928 || (sym->attr.function && sym->result != sym))
8931 /* Try to build the initializer expression. If we can't initialize
8932 this symbol, then init will be NULL. */
8933 init = build_default_init_expr (sym);
8937 /* For saved variables, we don't want to add an initializer at
8938 function entry, so we just add a static initializer. */
8939 if (sym->attr.save || sym->ns->save_all
8940 || gfc_option.flag_max_stack_var_size == 0)
8942 /* Don't clobber an existing initializer! */
8943 gcc_assert (sym->value == NULL);
8948 build_init_assign (sym, init);
8951 /* Resolution of common features of flavors variable and procedure. */
8954 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
8956 /* Constraints on deferred shape variable. */
8957 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
8959 if (sym->attr.allocatable)
8961 if (sym->attr.dimension)
8963 gfc_error ("Allocatable array '%s' at %L must have "
8964 "a deferred shape", sym->name, &sym->declared_at);
8967 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
8968 "may not be ALLOCATABLE", sym->name,
8969 &sym->declared_at) == FAILURE)
8973 if (sym->attr.pointer && sym->attr.dimension)
8975 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
8976 sym->name, &sym->declared_at);
8983 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
8984 && !sym->attr.dummy && sym->ts.type != BT_CLASS)
8986 gfc_error ("Array '%s' at %L cannot have a deferred shape",
8987 sym->name, &sym->declared_at);
8995 /* Additional checks for symbols with flavor variable and derived
8996 type. To be called from resolve_fl_variable. */
8999 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
9001 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
9003 /* Check to see if a derived type is blocked from being host
9004 associated by the presence of another class I symbol in the same
9005 namespace. 14.6.1.3 of the standard and the discussion on
9006 comp.lang.fortran. */
9007 if (sym->ns != sym->ts.u.derived->ns
9008 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
9011 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
9012 if (s && s->attr.flavor != FL_DERIVED)
9014 gfc_error ("The type '%s' cannot be host associated at %L "
9015 "because it is blocked by an incompatible object "
9016 "of the same name declared at %L",
9017 sym->ts.u.derived->name, &sym->declared_at,
9023 /* 4th constraint in section 11.3: "If an object of a type for which
9024 component-initialization is specified (R429) appears in the
9025 specification-part of a module and does not have the ALLOCATABLE
9026 or POINTER attribute, the object shall have the SAVE attribute."
9028 The check for initializers is performed with
9029 has_default_initializer because gfc_default_initializer generates
9030 a hidden default for allocatable components. */
9031 if (!(sym->value || no_init_flag) && sym->ns->proc_name
9032 && sym->ns->proc_name->attr.flavor == FL_MODULE
9033 && !sym->ns->save_all && !sym->attr.save
9034 && !sym->attr.pointer && !sym->attr.allocatable
9035 && has_default_initializer (sym->ts.u.derived)
9036 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Implied SAVE for "
9037 "module variable '%s' at %L, needed due to "
9038 "the default initialization", sym->name,
9039 &sym->declared_at) == FAILURE)
9042 if (sym->ts.type == BT_CLASS)
9045 if (!gfc_type_is_extensible (sym->ts.u.derived->components->ts.u.derived))
9047 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
9048 sym->ts.u.derived->components->ts.u.derived->name,
9049 sym->name, &sym->declared_at);
9054 /* Assume that use associated symbols were checked in the module ns. */
9055 if (!sym->attr.class_ok && !sym->attr.use_assoc)
9057 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
9058 "or pointer", sym->name, &sym->declared_at);
9063 /* Assign default initializer. */
9064 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
9065 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
9067 sym->value = gfc_default_initializer (&sym->ts);
9074 /* Resolve symbols with flavor variable. */
9077 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
9079 int no_init_flag, automatic_flag;
9081 const char *auto_save_msg;
9083 auto_save_msg = "Automatic object '%s' at %L cannot have the "
9086 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9089 /* Set this flag to check that variables are parameters of all entries.
9090 This check is effected by the call to gfc_resolve_expr through
9091 is_non_constant_shape_array. */
9092 specification_expr = 1;
9094 if (sym->ns->proc_name
9095 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9096 || sym->ns->proc_name->attr.is_main_program)
9097 && !sym->attr.use_assoc
9098 && !sym->attr.allocatable
9099 && !sym->attr.pointer
9100 && is_non_constant_shape_array (sym))
9102 /* The shape of a main program or module array needs to be
9104 gfc_error ("The module or main program array '%s' at %L must "
9105 "have constant shape", sym->name, &sym->declared_at);
9106 specification_expr = 0;
9110 if (sym->ts.type == BT_CHARACTER)
9112 /* Make sure that character string variables with assumed length are
9114 e = sym->ts.u.cl->length;
9115 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
9117 gfc_error ("Entity with assumed character length at %L must be a "
9118 "dummy argument or a PARAMETER", &sym->declared_at);
9122 if (e && sym->attr.save && !gfc_is_constant_expr (e))
9124 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9128 if (!gfc_is_constant_expr (e)
9129 && !(e->expr_type == EXPR_VARIABLE
9130 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
9131 && sym->ns->proc_name
9132 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9133 || sym->ns->proc_name->attr.is_main_program)
9134 && !sym->attr.use_assoc)
9136 gfc_error ("'%s' at %L must have constant character length "
9137 "in this context", sym->name, &sym->declared_at);
9142 if (sym->value == NULL && sym->attr.referenced)
9143 apply_default_init_local (sym); /* Try to apply a default initialization. */
9145 /* Determine if the symbol may not have an initializer. */
9146 no_init_flag = automatic_flag = 0;
9147 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
9148 || sym->attr.intrinsic || sym->attr.result)
9150 else if (sym->attr.dimension && !sym->attr.pointer
9151 && is_non_constant_shape_array (sym))
9153 no_init_flag = automatic_flag = 1;
9155 /* Also, they must not have the SAVE attribute.
9156 SAVE_IMPLICIT is checked below. */
9157 if (sym->attr.save == SAVE_EXPLICIT)
9159 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9164 /* Ensure that any initializer is simplified. */
9166 gfc_simplify_expr (sym->value, 1);
9168 /* Reject illegal initializers. */
9169 if (!sym->mark && sym->value)
9171 if (sym->attr.allocatable)
9172 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
9173 sym->name, &sym->declared_at);
9174 else if (sym->attr.external)
9175 gfc_error ("External '%s' at %L cannot have an initializer",
9176 sym->name, &sym->declared_at);
9177 else if (sym->attr.dummy
9178 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
9179 gfc_error ("Dummy '%s' at %L cannot have an initializer",
9180 sym->name, &sym->declared_at);
9181 else if (sym->attr.intrinsic)
9182 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
9183 sym->name, &sym->declared_at);
9184 else if (sym->attr.result)
9185 gfc_error ("Function result '%s' at %L cannot have an initializer",
9186 sym->name, &sym->declared_at);
9187 else if (automatic_flag)
9188 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
9189 sym->name, &sym->declared_at);
9196 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
9197 return resolve_fl_variable_derived (sym, no_init_flag);
9203 /* Resolve a procedure. */
9206 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
9208 gfc_formal_arglist *arg;
9210 if (sym->attr.function
9211 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9214 if (sym->ts.type == BT_CHARACTER)
9216 gfc_charlen *cl = sym->ts.u.cl;
9218 if (cl && cl->length && gfc_is_constant_expr (cl->length)
9219 && resolve_charlen (cl) == FAILURE)
9222 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
9223 && sym->attr.proc == PROC_ST_FUNCTION)
9225 gfc_error ("Character-valued statement function '%s' at %L must "
9226 "have constant length", sym->name, &sym->declared_at);
9231 /* Ensure that derived type for are not of a private type. Internal
9232 module procedures are excluded by 2.2.3.3 - i.e., they are not
9233 externally accessible and can access all the objects accessible in
9235 if (!(sym->ns->parent
9236 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
9237 && gfc_check_access(sym->attr.access, sym->ns->default_access))
9239 gfc_interface *iface;
9241 for (arg = sym->formal; arg; arg = arg->next)
9244 && arg->sym->ts.type == BT_DERIVED
9245 && !arg->sym->ts.u.derived->attr.use_assoc
9246 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9247 arg->sym->ts.u.derived->ns->default_access)
9248 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
9249 "PRIVATE type and cannot be a dummy argument"
9250 " of '%s', which is PUBLIC at %L",
9251 arg->sym->name, sym->name, &sym->declared_at)
9254 /* Stop this message from recurring. */
9255 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9260 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9261 PRIVATE to the containing module. */
9262 for (iface = sym->generic; iface; iface = iface->next)
9264 for (arg = iface->sym->formal; arg; arg = arg->next)
9267 && arg->sym->ts.type == BT_DERIVED
9268 && !arg->sym->ts.u.derived->attr.use_assoc
9269 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9270 arg->sym->ts.u.derived->ns->default_access)
9271 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9272 "'%s' in PUBLIC interface '%s' at %L "
9273 "takes dummy arguments of '%s' which is "
9274 "PRIVATE", iface->sym->name, sym->name,
9275 &iface->sym->declared_at,
9276 gfc_typename (&arg->sym->ts)) == FAILURE)
9278 /* Stop this message from recurring. */
9279 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9285 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9286 PRIVATE to the containing module. */
9287 for (iface = sym->generic; iface; iface = iface->next)
9289 for (arg = iface->sym->formal; arg; arg = arg->next)
9292 && arg->sym->ts.type == BT_DERIVED
9293 && !arg->sym->ts.u.derived->attr.use_assoc
9294 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9295 arg->sym->ts.u.derived->ns->default_access)
9296 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9297 "'%s' in PUBLIC interface '%s' at %L "
9298 "takes dummy arguments of '%s' which is "
9299 "PRIVATE", iface->sym->name, sym->name,
9300 &iface->sym->declared_at,
9301 gfc_typename (&arg->sym->ts)) == FAILURE)
9303 /* Stop this message from recurring. */
9304 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9311 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
9312 && !sym->attr.proc_pointer)
9314 gfc_error ("Function '%s' at %L cannot have an initializer",
9315 sym->name, &sym->declared_at);
9319 /* An external symbol may not have an initializer because it is taken to be
9320 a procedure. Exception: Procedure Pointers. */
9321 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
9323 gfc_error ("External object '%s' at %L may not have an initializer",
9324 sym->name, &sym->declared_at);
9328 /* An elemental function is required to return a scalar 12.7.1 */
9329 if (sym->attr.elemental && sym->attr.function && sym->as)
9331 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
9332 "result", sym->name, &sym->declared_at);
9333 /* Reset so that the error only occurs once. */
9334 sym->attr.elemental = 0;
9338 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
9339 char-len-param shall not be array-valued, pointer-valued, recursive
9340 or pure. ....snip... A character value of * may only be used in the
9341 following ways: (i) Dummy arg of procedure - dummy associates with
9342 actual length; (ii) To declare a named constant; or (iii) External
9343 function - but length must be declared in calling scoping unit. */
9344 if (sym->attr.function
9345 && sym->ts.type == BT_CHARACTER
9346 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
9348 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
9349 || (sym->attr.recursive) || (sym->attr.pure))
9351 if (sym->as && sym->as->rank)
9352 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9353 "array-valued", sym->name, &sym->declared_at);
9355 if (sym->attr.pointer)
9356 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9357 "pointer-valued", sym->name, &sym->declared_at);
9360 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9361 "pure", sym->name, &sym->declared_at);
9363 if (sym->attr.recursive)
9364 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9365 "recursive", sym->name, &sym->declared_at);
9370 /* Appendix B.2 of the standard. Contained functions give an
9371 error anyway. Fixed-form is likely to be F77/legacy. */
9372 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
9373 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
9374 "CHARACTER(*) function '%s' at %L",
9375 sym->name, &sym->declared_at);
9378 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
9380 gfc_formal_arglist *curr_arg;
9381 int has_non_interop_arg = 0;
9383 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
9384 sym->common_block) == FAILURE)
9386 /* Clear these to prevent looking at them again if there was an
9388 sym->attr.is_bind_c = 0;
9389 sym->attr.is_c_interop = 0;
9390 sym->ts.is_c_interop = 0;
9394 /* So far, no errors have been found. */
9395 sym->attr.is_c_interop = 1;
9396 sym->ts.is_c_interop = 1;
9399 curr_arg = sym->formal;
9400 while (curr_arg != NULL)
9402 /* Skip implicitly typed dummy args here. */
9403 if (curr_arg->sym->attr.implicit_type == 0)
9404 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
9405 /* If something is found to fail, record the fact so we
9406 can mark the symbol for the procedure as not being
9407 BIND(C) to try and prevent multiple errors being
9409 has_non_interop_arg = 1;
9411 curr_arg = curr_arg->next;
9414 /* See if any of the arguments were not interoperable and if so, clear
9415 the procedure symbol to prevent duplicate error messages. */
9416 if (has_non_interop_arg != 0)
9418 sym->attr.is_c_interop = 0;
9419 sym->ts.is_c_interop = 0;
9420 sym->attr.is_bind_c = 0;
9424 if (!sym->attr.proc_pointer)
9426 if (sym->attr.save == SAVE_EXPLICIT)
9428 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
9429 "in '%s' at %L", sym->name, &sym->declared_at);
9432 if (sym->attr.intent)
9434 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
9435 "in '%s' at %L", sym->name, &sym->declared_at);
9438 if (sym->attr.subroutine && sym->attr.result)
9440 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
9441 "in '%s' at %L", sym->name, &sym->declared_at);
9444 if (sym->attr.external && sym->attr.function
9445 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
9446 || sym->attr.contained))
9448 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
9449 "in '%s' at %L", sym->name, &sym->declared_at);
9452 if (strcmp ("ppr@", sym->name) == 0)
9454 gfc_error ("Procedure pointer result '%s' at %L "
9455 "is missing the pointer attribute",
9456 sym->ns->proc_name->name, &sym->declared_at);
9465 /* Resolve a list of finalizer procedures. That is, after they have hopefully
9466 been defined and we now know their defined arguments, check that they fulfill
9467 the requirements of the standard for procedures used as finalizers. */
9470 gfc_resolve_finalizers (gfc_symbol* derived)
9472 gfc_finalizer* list;
9473 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
9474 gfc_try result = SUCCESS;
9475 bool seen_scalar = false;
9477 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
9480 /* Walk over the list of finalizer-procedures, check them, and if any one
9481 does not fit in with the standard's definition, print an error and remove
9482 it from the list. */
9483 prev_link = &derived->f2k_derived->finalizers;
9484 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
9490 /* Skip this finalizer if we already resolved it. */
9491 if (list->proc_tree)
9493 prev_link = &(list->next);
9497 /* Check this exists and is a SUBROUTINE. */
9498 if (!list->proc_sym->attr.subroutine)
9500 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
9501 list->proc_sym->name, &list->where);
9505 /* We should have exactly one argument. */
9506 if (!list->proc_sym->formal || list->proc_sym->formal->next)
9508 gfc_error ("FINAL procedure at %L must have exactly one argument",
9512 arg = list->proc_sym->formal->sym;
9514 /* This argument must be of our type. */
9515 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
9517 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
9518 &arg->declared_at, derived->name);
9522 /* It must neither be a pointer nor allocatable nor optional. */
9523 if (arg->attr.pointer)
9525 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
9529 if (arg->attr.allocatable)
9531 gfc_error ("Argument of FINAL procedure at %L must not be"
9532 " ALLOCATABLE", &arg->declared_at);
9535 if (arg->attr.optional)
9537 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
9542 /* It must not be INTENT(OUT). */
9543 if (arg->attr.intent == INTENT_OUT)
9545 gfc_error ("Argument of FINAL procedure at %L must not be"
9546 " INTENT(OUT)", &arg->declared_at);
9550 /* Warn if the procedure is non-scalar and not assumed shape. */
9551 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
9552 && arg->as->type != AS_ASSUMED_SHAPE)
9553 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
9554 " shape argument", &arg->declared_at);
9556 /* Check that it does not match in kind and rank with a FINAL procedure
9557 defined earlier. To really loop over the *earlier* declarations,
9558 we need to walk the tail of the list as new ones were pushed at the
9560 /* TODO: Handle kind parameters once they are implemented. */
9561 my_rank = (arg->as ? arg->as->rank : 0);
9562 for (i = list->next; i; i = i->next)
9564 /* Argument list might be empty; that is an error signalled earlier,
9565 but we nevertheless continued resolving. */
9566 if (i->proc_sym->formal)
9568 gfc_symbol* i_arg = i->proc_sym->formal->sym;
9569 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
9570 if (i_rank == my_rank)
9572 gfc_error ("FINAL procedure '%s' declared at %L has the same"
9573 " rank (%d) as '%s'",
9574 list->proc_sym->name, &list->where, my_rank,
9581 /* Is this the/a scalar finalizer procedure? */
9582 if (!arg->as || arg->as->rank == 0)
9585 /* Find the symtree for this procedure. */
9586 gcc_assert (!list->proc_tree);
9587 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
9589 prev_link = &list->next;
9592 /* Remove wrong nodes immediately from the list so we don't risk any
9593 troubles in the future when they might fail later expectations. */
9597 *prev_link = list->next;
9598 gfc_free_finalizer (i);
9601 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
9602 were nodes in the list, must have been for arrays. It is surely a good
9603 idea to have a scalar version there if there's something to finalize. */
9604 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
9605 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
9606 " defined at %L, suggest also scalar one",
9607 derived->name, &derived->declared_at);
9609 /* TODO: Remove this error when finalization is finished. */
9610 gfc_error ("Finalization at %L is not yet implemented",
9611 &derived->declared_at);
9617 /* Check that it is ok for the typebound procedure proc to override the
9621 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
9624 const gfc_symbol* proc_target;
9625 const gfc_symbol* old_target;
9626 unsigned proc_pass_arg, old_pass_arg, argpos;
9627 gfc_formal_arglist* proc_formal;
9628 gfc_formal_arglist* old_formal;
9630 /* This procedure should only be called for non-GENERIC proc. */
9631 gcc_assert (!proc->n.tb->is_generic);
9633 /* If the overwritten procedure is GENERIC, this is an error. */
9634 if (old->n.tb->is_generic)
9636 gfc_error ("Can't overwrite GENERIC '%s' at %L",
9637 old->name, &proc->n.tb->where);
9641 where = proc->n.tb->where;
9642 proc_target = proc->n.tb->u.specific->n.sym;
9643 old_target = old->n.tb->u.specific->n.sym;
9645 /* Check that overridden binding is not NON_OVERRIDABLE. */
9646 if (old->n.tb->non_overridable)
9648 gfc_error ("'%s' at %L overrides a procedure binding declared"
9649 " NON_OVERRIDABLE", proc->name, &where);
9653 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
9654 if (!old->n.tb->deferred && proc->n.tb->deferred)
9656 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
9657 " non-DEFERRED binding", proc->name, &where);
9661 /* If the overridden binding is PURE, the overriding must be, too. */
9662 if (old_target->attr.pure && !proc_target->attr.pure)
9664 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
9665 proc->name, &where);
9669 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
9670 is not, the overriding must not be either. */
9671 if (old_target->attr.elemental && !proc_target->attr.elemental)
9673 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
9674 " ELEMENTAL", proc->name, &where);
9677 if (!old_target->attr.elemental && proc_target->attr.elemental)
9679 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
9680 " be ELEMENTAL, either", proc->name, &where);
9684 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
9686 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
9688 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
9689 " SUBROUTINE", proc->name, &where);
9693 /* If the overridden binding is a FUNCTION, the overriding must also be a
9694 FUNCTION and have the same characteristics. */
9695 if (old_target->attr.function)
9697 if (!proc_target->attr.function)
9699 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
9700 " FUNCTION", proc->name, &where);
9704 /* FIXME: Do more comprehensive checking (including, for instance, the
9705 rank and array-shape). */
9706 gcc_assert (proc_target->result && old_target->result);
9707 if (!gfc_compare_types (&proc_target->result->ts,
9708 &old_target->result->ts))
9710 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
9711 " matching result types", proc->name, &where);
9716 /* If the overridden binding is PUBLIC, the overriding one must not be
9718 if (old->n.tb->access == ACCESS_PUBLIC
9719 && proc->n.tb->access == ACCESS_PRIVATE)
9721 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
9722 " PRIVATE", proc->name, &where);
9726 /* Compare the formal argument lists of both procedures. This is also abused
9727 to find the position of the passed-object dummy arguments of both
9728 bindings as at least the overridden one might not yet be resolved and we
9729 need those positions in the check below. */
9730 proc_pass_arg = old_pass_arg = 0;
9731 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
9733 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
9736 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
9737 proc_formal && old_formal;
9738 proc_formal = proc_formal->next, old_formal = old_formal->next)
9740 if (proc->n.tb->pass_arg
9741 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
9742 proc_pass_arg = argpos;
9743 if (old->n.tb->pass_arg
9744 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
9745 old_pass_arg = argpos;
9747 /* Check that the names correspond. */
9748 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
9750 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
9751 " to match the corresponding argument of the overridden"
9752 " procedure", proc_formal->sym->name, proc->name, &where,
9753 old_formal->sym->name);
9757 /* Check that the types correspond if neither is the passed-object
9759 /* FIXME: Do more comprehensive testing here. */
9760 if (proc_pass_arg != argpos && old_pass_arg != argpos
9761 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
9763 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
9764 "in respect to the overridden procedure",
9765 proc_formal->sym->name, proc->name, &where);
9771 if (proc_formal || old_formal)
9773 gfc_error ("'%s' at %L must have the same number of formal arguments as"
9774 " the overridden procedure", proc->name, &where);
9778 /* If the overridden binding is NOPASS, the overriding one must also be
9780 if (old->n.tb->nopass && !proc->n.tb->nopass)
9782 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
9783 " NOPASS", proc->name, &where);
9787 /* If the overridden binding is PASS(x), the overriding one must also be
9788 PASS and the passed-object dummy arguments must correspond. */
9789 if (!old->n.tb->nopass)
9791 if (proc->n.tb->nopass)
9793 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
9794 " PASS", proc->name, &where);
9798 if (proc_pass_arg != old_pass_arg)
9800 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
9801 " the same position as the passed-object dummy argument of"
9802 " the overridden procedure", proc->name, &where);
9811 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
9814 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
9815 const char* generic_name, locus where)
9820 gcc_assert (t1->specific && t2->specific);
9821 gcc_assert (!t1->specific->is_generic);
9822 gcc_assert (!t2->specific->is_generic);
9824 sym1 = t1->specific->u.specific->n.sym;
9825 sym2 = t2->specific->u.specific->n.sym;
9830 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
9831 if (sym1->attr.subroutine != sym2->attr.subroutine
9832 || sym1->attr.function != sym2->attr.function)
9834 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
9835 " GENERIC '%s' at %L",
9836 sym1->name, sym2->name, generic_name, &where);
9840 /* Compare the interfaces. */
9841 if (gfc_compare_interfaces (sym1, sym2, sym2->name, 1, 0, NULL, 0))
9843 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
9844 sym1->name, sym2->name, generic_name, &where);
9852 /* Worker function for resolving a generic procedure binding; this is used to
9853 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
9855 The difference between those cases is finding possible inherited bindings
9856 that are overridden, as one has to look for them in tb_sym_root,
9857 tb_uop_root or tb_op, respectively. Thus the caller must already find
9858 the super-type and set p->overridden correctly. */
9861 resolve_tb_generic_targets (gfc_symbol* super_type,
9862 gfc_typebound_proc* p, const char* name)
9864 gfc_tbp_generic* target;
9865 gfc_symtree* first_target;
9866 gfc_symtree* inherited;
9868 gcc_assert (p && p->is_generic);
9870 /* Try to find the specific bindings for the symtrees in our target-list. */
9871 gcc_assert (p->u.generic);
9872 for (target = p->u.generic; target; target = target->next)
9873 if (!target->specific)
9875 gfc_typebound_proc* overridden_tbp;
9877 const char* target_name;
9879 target_name = target->specific_st->name;
9881 /* Defined for this type directly. */
9882 if (target->specific_st->n.tb)
9884 target->specific = target->specific_st->n.tb;
9885 goto specific_found;
9888 /* Look for an inherited specific binding. */
9891 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
9896 gcc_assert (inherited->n.tb);
9897 target->specific = inherited->n.tb;
9898 goto specific_found;
9902 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
9903 " at %L", target_name, name, &p->where);
9906 /* Once we've found the specific binding, check it is not ambiguous with
9907 other specifics already found or inherited for the same GENERIC. */
9909 gcc_assert (target->specific);
9911 /* This must really be a specific binding! */
9912 if (target->specific->is_generic)
9914 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
9915 " '%s' is GENERIC, too", name, &p->where, target_name);
9919 /* Check those already resolved on this type directly. */
9920 for (g = p->u.generic; g; g = g->next)
9921 if (g != target && g->specific
9922 && check_generic_tbp_ambiguity (target, g, name, p->where)
9926 /* Check for ambiguity with inherited specific targets. */
9927 for (overridden_tbp = p->overridden; overridden_tbp;
9928 overridden_tbp = overridden_tbp->overridden)
9929 if (overridden_tbp->is_generic)
9931 for (g = overridden_tbp->u.generic; g; g = g->next)
9933 gcc_assert (g->specific);
9934 if (check_generic_tbp_ambiguity (target, g,
9935 name, p->where) == FAILURE)
9941 /* If we attempt to "overwrite" a specific binding, this is an error. */
9942 if (p->overridden && !p->overridden->is_generic)
9944 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
9945 " the same name", name, &p->where);
9949 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
9950 all must have the same attributes here. */
9951 first_target = p->u.generic->specific->u.specific;
9952 gcc_assert (first_target);
9953 p->subroutine = first_target->n.sym->attr.subroutine;
9954 p->function = first_target->n.sym->attr.function;
9960 /* Resolve a GENERIC procedure binding for a derived type. */
9963 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
9965 gfc_symbol* super_type;
9967 /* Find the overridden binding if any. */
9968 st->n.tb->overridden = NULL;
9969 super_type = gfc_get_derived_super_type (derived);
9972 gfc_symtree* overridden;
9973 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
9976 if (overridden && overridden->n.tb)
9977 st->n.tb->overridden = overridden->n.tb;
9980 /* Resolve using worker function. */
9981 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
9985 /* Retrieve the target-procedure of an operator binding and do some checks in
9986 common for intrinsic and user-defined type-bound operators. */
9989 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
9991 gfc_symbol* target_proc;
9993 gcc_assert (target->specific && !target->specific->is_generic);
9994 target_proc = target->specific->u.specific->n.sym;
9995 gcc_assert (target_proc);
9997 /* All operator bindings must have a passed-object dummy argument. */
9998 if (target->specific->nopass)
10000 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
10004 return target_proc;
10008 /* Resolve a type-bound intrinsic operator. */
10011 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
10012 gfc_typebound_proc* p)
10014 gfc_symbol* super_type;
10015 gfc_tbp_generic* target;
10017 /* If there's already an error here, do nothing (but don't fail again). */
10021 /* Operators should always be GENERIC bindings. */
10022 gcc_assert (p->is_generic);
10024 /* Look for an overridden binding. */
10025 super_type = gfc_get_derived_super_type (derived);
10026 if (super_type && super_type->f2k_derived)
10027 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
10030 p->overridden = NULL;
10032 /* Resolve general GENERIC properties using worker function. */
10033 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
10036 /* Check the targets to be procedures of correct interface. */
10037 for (target = p->u.generic; target; target = target->next)
10039 gfc_symbol* target_proc;
10041 target_proc = get_checked_tb_operator_target (target, p->where);
10045 if (!gfc_check_operator_interface (target_proc, op, p->where))
10057 /* Resolve a type-bound user operator (tree-walker callback). */
10059 static gfc_symbol* resolve_bindings_derived;
10060 static gfc_try resolve_bindings_result;
10062 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
10065 resolve_typebound_user_op (gfc_symtree* stree)
10067 gfc_symbol* super_type;
10068 gfc_tbp_generic* target;
10070 gcc_assert (stree && stree->n.tb);
10072 if (stree->n.tb->error)
10075 /* Operators should always be GENERIC bindings. */
10076 gcc_assert (stree->n.tb->is_generic);
10078 /* Find overridden procedure, if any. */
10079 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10080 if (super_type && super_type->f2k_derived)
10082 gfc_symtree* overridden;
10083 overridden = gfc_find_typebound_user_op (super_type, NULL,
10084 stree->name, true, NULL);
10086 if (overridden && overridden->n.tb)
10087 stree->n.tb->overridden = overridden->n.tb;
10090 stree->n.tb->overridden = NULL;
10092 /* Resolve basically using worker function. */
10093 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
10097 /* Check the targets to be functions of correct interface. */
10098 for (target = stree->n.tb->u.generic; target; target = target->next)
10100 gfc_symbol* target_proc;
10102 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
10106 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
10113 resolve_bindings_result = FAILURE;
10114 stree->n.tb->error = 1;
10118 /* Resolve the type-bound procedures for a derived type. */
10121 resolve_typebound_procedure (gfc_symtree* stree)
10125 gfc_symbol* me_arg;
10126 gfc_symbol* super_type;
10127 gfc_component* comp;
10129 gcc_assert (stree);
10131 /* Undefined specific symbol from GENERIC target definition. */
10135 if (stree->n.tb->error)
10138 /* If this is a GENERIC binding, use that routine. */
10139 if (stree->n.tb->is_generic)
10141 if (resolve_typebound_generic (resolve_bindings_derived, stree)
10147 /* Get the target-procedure to check it. */
10148 gcc_assert (!stree->n.tb->is_generic);
10149 gcc_assert (stree->n.tb->u.specific);
10150 proc = stree->n.tb->u.specific->n.sym;
10151 where = stree->n.tb->where;
10153 /* Default access should already be resolved from the parser. */
10154 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
10156 /* It should be a module procedure or an external procedure with explicit
10157 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
10158 if ((!proc->attr.subroutine && !proc->attr.function)
10159 || (proc->attr.proc != PROC_MODULE
10160 && proc->attr.if_source != IFSRC_IFBODY)
10161 || (proc->attr.abstract && !stree->n.tb->deferred))
10163 gfc_error ("'%s' must be a module procedure or an external procedure with"
10164 " an explicit interface at %L", proc->name, &where);
10167 stree->n.tb->subroutine = proc->attr.subroutine;
10168 stree->n.tb->function = proc->attr.function;
10170 /* Find the super-type of the current derived type. We could do this once and
10171 store in a global if speed is needed, but as long as not I believe this is
10172 more readable and clearer. */
10173 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10175 /* If PASS, resolve and check arguments if not already resolved / loaded
10176 from a .mod file. */
10177 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
10179 if (stree->n.tb->pass_arg)
10181 gfc_formal_arglist* i;
10183 /* If an explicit passing argument name is given, walk the arg-list
10184 and look for it. */
10187 stree->n.tb->pass_arg_num = 1;
10188 for (i = proc->formal; i; i = i->next)
10190 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
10195 ++stree->n.tb->pass_arg_num;
10200 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
10202 proc->name, stree->n.tb->pass_arg, &where,
10203 stree->n.tb->pass_arg);
10209 /* Otherwise, take the first one; there should in fact be at least
10211 stree->n.tb->pass_arg_num = 1;
10214 gfc_error ("Procedure '%s' with PASS at %L must have at"
10215 " least one argument", proc->name, &where);
10218 me_arg = proc->formal->sym;
10221 /* Now check that the argument-type matches and the passed-object
10222 dummy argument is generally fine. */
10224 gcc_assert (me_arg);
10226 if (me_arg->ts.type != BT_CLASS)
10228 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10229 " at %L", proc->name, &where);
10233 if (me_arg->ts.u.derived->components->ts.u.derived
10234 != resolve_bindings_derived)
10236 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10237 " the derived-type '%s'", me_arg->name, proc->name,
10238 me_arg->name, &where, resolve_bindings_derived->name);
10242 gcc_assert (me_arg->ts.type == BT_CLASS);
10243 if (me_arg->ts.u.derived->components->as
10244 && me_arg->ts.u.derived->components->as->rank > 0)
10246 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
10247 " scalar", proc->name, &where);
10250 if (me_arg->ts.u.derived->components->attr.allocatable)
10252 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10253 " be ALLOCATABLE", proc->name, &where);
10256 if (me_arg->ts.u.derived->components->attr.class_pointer)
10258 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10259 " be POINTER", proc->name, &where);
10264 /* If we are extending some type, check that we don't override a procedure
10265 flagged NON_OVERRIDABLE. */
10266 stree->n.tb->overridden = NULL;
10269 gfc_symtree* overridden;
10270 overridden = gfc_find_typebound_proc (super_type, NULL,
10271 stree->name, true, NULL);
10273 if (overridden && overridden->n.tb)
10274 stree->n.tb->overridden = overridden->n.tb;
10276 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
10280 /* See if there's a name collision with a component directly in this type. */
10281 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
10282 if (!strcmp (comp->name, stree->name))
10284 gfc_error ("Procedure '%s' at %L has the same name as a component of"
10286 stree->name, &where, resolve_bindings_derived->name);
10290 /* Try to find a name collision with an inherited component. */
10291 if (super_type && gfc_find_component (super_type, stree->name, true, true))
10293 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
10294 " component of '%s'",
10295 stree->name, &where, resolve_bindings_derived->name);
10299 stree->n.tb->error = 0;
10303 resolve_bindings_result = FAILURE;
10304 stree->n.tb->error = 1;
10308 resolve_typebound_procedures (gfc_symbol* derived)
10312 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
10315 resolve_bindings_derived = derived;
10316 resolve_bindings_result = SUCCESS;
10318 if (derived->f2k_derived->tb_sym_root)
10319 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
10320 &resolve_typebound_procedure);
10322 if (derived->f2k_derived->tb_uop_root)
10323 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
10324 &resolve_typebound_user_op);
10326 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
10328 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
10329 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
10331 resolve_bindings_result = FAILURE;
10334 return resolve_bindings_result;
10338 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
10339 to give all identical derived types the same backend_decl. */
10341 add_dt_to_dt_list (gfc_symbol *derived)
10343 gfc_dt_list *dt_list;
10345 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
10346 if (derived == dt_list->derived)
10349 if (dt_list == NULL)
10351 dt_list = gfc_get_dt_list ();
10352 dt_list->next = gfc_derived_types;
10353 dt_list->derived = derived;
10354 gfc_derived_types = dt_list;
10359 /* Ensure that a derived-type is really not abstract, meaning that every
10360 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
10363 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
10368 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
10370 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
10373 if (st->n.tb && st->n.tb->deferred)
10375 gfc_symtree* overriding;
10376 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
10377 gcc_assert (overriding && overriding->n.tb);
10378 if (overriding->n.tb->deferred)
10380 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
10381 " '%s' is DEFERRED and not overridden",
10382 sub->name, &sub->declared_at, st->name);
10391 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
10393 /* The algorithm used here is to recursively travel up the ancestry of sub
10394 and for each ancestor-type, check all bindings. If any of them is
10395 DEFERRED, look it up starting from sub and see if the found (overriding)
10396 binding is not DEFERRED.
10397 This is not the most efficient way to do this, but it should be ok and is
10398 clearer than something sophisticated. */
10400 gcc_assert (ancestor && ancestor->attr.abstract && !sub->attr.abstract);
10402 /* Walk bindings of this ancestor. */
10403 if (ancestor->f2k_derived)
10406 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
10411 /* Find next ancestor type and recurse on it. */
10412 ancestor = gfc_get_derived_super_type (ancestor);
10414 return ensure_not_abstract (sub, ancestor);
10420 static void resolve_symbol (gfc_symbol *sym);
10423 /* Resolve the components of a derived type. */
10426 resolve_fl_derived (gfc_symbol *sym)
10428 gfc_symbol* super_type;
10432 super_type = gfc_get_derived_super_type (sym);
10434 /* Ensure the extended type gets resolved before we do. */
10435 if (super_type && resolve_fl_derived (super_type) == FAILURE)
10438 /* An ABSTRACT type must be extensible. */
10439 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
10441 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
10442 sym->name, &sym->declared_at);
10446 for (c = sym->components; c != NULL; c = c->next)
10448 if (c->attr.proc_pointer && c->ts.interface)
10450 if (c->ts.interface->attr.procedure)
10451 gfc_error ("Interface '%s', used by procedure pointer component "
10452 "'%s' at %L, is declared in a later PROCEDURE statement",
10453 c->ts.interface->name, c->name, &c->loc);
10455 /* Get the attributes from the interface (now resolved). */
10456 if (c->ts.interface->attr.if_source
10457 || c->ts.interface->attr.intrinsic)
10459 gfc_symbol *ifc = c->ts.interface;
10461 if (ifc->formal && !ifc->formal_ns)
10462 resolve_symbol (ifc);
10464 if (ifc->attr.intrinsic)
10465 resolve_intrinsic (ifc, &ifc->declared_at);
10469 c->ts = ifc->result->ts;
10470 c->attr.allocatable = ifc->result->attr.allocatable;
10471 c->attr.pointer = ifc->result->attr.pointer;
10472 c->attr.dimension = ifc->result->attr.dimension;
10473 c->as = gfc_copy_array_spec (ifc->result->as);
10478 c->attr.allocatable = ifc->attr.allocatable;
10479 c->attr.pointer = ifc->attr.pointer;
10480 c->attr.dimension = ifc->attr.dimension;
10481 c->as = gfc_copy_array_spec (ifc->as);
10483 c->ts.interface = ifc;
10484 c->attr.function = ifc->attr.function;
10485 c->attr.subroutine = ifc->attr.subroutine;
10486 gfc_copy_formal_args_ppc (c, ifc);
10488 c->attr.pure = ifc->attr.pure;
10489 c->attr.elemental = ifc->attr.elemental;
10490 c->attr.recursive = ifc->attr.recursive;
10491 c->attr.always_explicit = ifc->attr.always_explicit;
10492 c->attr.ext_attr |= ifc->attr.ext_attr;
10493 /* Replace symbols in array spec. */
10497 for (i = 0; i < c->as->rank; i++)
10499 gfc_expr_replace_comp (c->as->lower[i], c);
10500 gfc_expr_replace_comp (c->as->upper[i], c);
10503 /* Copy char length. */
10504 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
10506 c->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
10507 gfc_expr_replace_comp (c->ts.u.cl->length, c);
10510 else if (c->ts.interface->name[0] != '\0')
10512 gfc_error ("Interface '%s' of procedure pointer component "
10513 "'%s' at %L must be explicit", c->ts.interface->name,
10518 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
10520 /* Since PPCs are not implicitly typed, a PPC without an explicit
10521 interface must be a subroutine. */
10522 gfc_add_subroutine (&c->attr, c->name, &c->loc);
10525 /* Procedure pointer components: Check PASS arg. */
10526 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0)
10528 gfc_symbol* me_arg;
10530 if (c->tb->pass_arg)
10532 gfc_formal_arglist* i;
10534 /* If an explicit passing argument name is given, walk the arg-list
10535 and look for it. */
10538 c->tb->pass_arg_num = 1;
10539 for (i = c->formal; i; i = i->next)
10541 if (!strcmp (i->sym->name, c->tb->pass_arg))
10546 c->tb->pass_arg_num++;
10551 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
10552 "at %L has no argument '%s'", c->name,
10553 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
10560 /* Otherwise, take the first one; there should in fact be at least
10562 c->tb->pass_arg_num = 1;
10565 gfc_error ("Procedure pointer component '%s' with PASS at %L "
10566 "must have at least one argument",
10571 me_arg = c->formal->sym;
10574 /* Now check that the argument-type matches. */
10575 gcc_assert (me_arg);
10576 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
10577 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
10578 || (me_arg->ts.type == BT_CLASS
10579 && me_arg->ts.u.derived->components->ts.u.derived != sym))
10581 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10582 " the derived type '%s'", me_arg->name, c->name,
10583 me_arg->name, &c->loc, sym->name);
10588 /* Check for C453. */
10589 if (me_arg->attr.dimension)
10591 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10592 "must be scalar", me_arg->name, c->name, me_arg->name,
10598 if (me_arg->attr.pointer)
10600 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10601 "may not have the POINTER attribute", me_arg->name,
10602 c->name, me_arg->name, &c->loc);
10607 if (me_arg->attr.allocatable)
10609 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10610 "may not be ALLOCATABLE", me_arg->name, c->name,
10611 me_arg->name, &c->loc);
10616 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
10617 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10618 " at %L", c->name, &c->loc);
10622 /* Check type-spec if this is not the parent-type component. */
10623 if ((!sym->attr.extension || c != sym->components)
10624 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
10627 /* If this type is an extension, set the accessibility of the parent
10629 if (super_type && c == sym->components
10630 && strcmp (super_type->name, c->name) == 0)
10631 c->attr.access = super_type->attr.access;
10633 /* If this type is an extension, see if this component has the same name
10634 as an inherited type-bound procedure. */
10636 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
10638 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
10639 " inherited type-bound procedure",
10640 c->name, sym->name, &c->loc);
10644 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
10646 if (c->ts.u.cl->length == NULL
10647 || (resolve_charlen (c->ts.u.cl) == FAILURE)
10648 || !gfc_is_constant_expr (c->ts.u.cl->length))
10650 gfc_error ("Character length of component '%s' needs to "
10651 "be a constant specification expression at %L",
10653 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
10658 if (c->ts.type == BT_DERIVED
10659 && sym->component_access != ACCESS_PRIVATE
10660 && gfc_check_access (sym->attr.access, sym->ns->default_access)
10661 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
10662 && !c->ts.u.derived->attr.use_assoc
10663 && !gfc_check_access (c->ts.u.derived->attr.access,
10664 c->ts.u.derived->ns->default_access)
10665 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
10666 "is a PRIVATE type and cannot be a component of "
10667 "'%s', which is PUBLIC at %L", c->name,
10668 sym->name, &sym->declared_at) == FAILURE)
10671 if (sym->attr.sequence)
10673 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
10675 gfc_error ("Component %s of SEQUENCE type declared at %L does "
10676 "not have the SEQUENCE attribute",
10677 c->ts.u.derived->name, &sym->declared_at);
10682 if (c->ts.type == BT_DERIVED && c->attr.pointer
10683 && c->ts.u.derived->components == NULL
10684 && !c->ts.u.derived->attr.zero_comp)
10686 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
10687 "that has not been declared", c->name, sym->name,
10693 if (c->ts.type == BT_CLASS
10694 && !(c->ts.u.derived->components->attr.pointer
10695 || c->ts.u.derived->components->attr.allocatable))
10697 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
10698 "or pointer", c->name, &c->loc);
10702 /* Ensure that all the derived type components are put on the
10703 derived type list; even in formal namespaces, where derived type
10704 pointer components might not have been declared. */
10705 if (c->ts.type == BT_DERIVED
10707 && c->ts.u.derived->components
10709 && sym != c->ts.u.derived)
10710 add_dt_to_dt_list (c->ts.u.derived);
10712 if (c->attr.pointer || c->attr.proc_pointer || c->attr.allocatable
10716 for (i = 0; i < c->as->rank; i++)
10718 if (c->as->lower[i] == NULL
10719 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
10720 || !gfc_is_constant_expr (c->as->lower[i])
10721 || c->as->upper[i] == NULL
10722 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
10723 || !gfc_is_constant_expr (c->as->upper[i]))
10725 gfc_error ("Component '%s' of '%s' at %L must have "
10726 "constant array bounds",
10727 c->name, sym->name, &c->loc);
10733 /* Resolve the type-bound procedures. */
10734 if (resolve_typebound_procedures (sym) == FAILURE)
10737 /* Resolve the finalizer procedures. */
10738 if (gfc_resolve_finalizers (sym) == FAILURE)
10741 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
10742 all DEFERRED bindings are overridden. */
10743 if (super_type && super_type->attr.abstract && !sym->attr.abstract
10744 && ensure_not_abstract (sym, super_type) == FAILURE)
10747 /* Add derived type to the derived type list. */
10748 add_dt_to_dt_list (sym);
10755 resolve_fl_namelist (gfc_symbol *sym)
10760 /* Reject PRIVATE objects in a PUBLIC namelist. */
10761 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
10763 for (nl = sym->namelist; nl; nl = nl->next)
10765 if (!nl->sym->attr.use_assoc
10766 && !is_sym_host_assoc (nl->sym, sym->ns)
10767 && !gfc_check_access(nl->sym->attr.access,
10768 nl->sym->ns->default_access))
10770 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
10771 "cannot be member of PUBLIC namelist '%s' at %L",
10772 nl->sym->name, sym->name, &sym->declared_at);
10776 /* Types with private components that came here by USE-association. */
10777 if (nl->sym->ts.type == BT_DERIVED
10778 && derived_inaccessible (nl->sym->ts.u.derived))
10780 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
10781 "components and cannot be member of namelist '%s' at %L",
10782 nl->sym->name, sym->name, &sym->declared_at);
10786 /* Types with private components that are defined in the same module. */
10787 if (nl->sym->ts.type == BT_DERIVED
10788 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
10789 && !gfc_check_access (nl->sym->ts.u.derived->attr.private_comp
10790 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
10791 nl->sym->ns->default_access))
10793 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
10794 "cannot be a member of PUBLIC namelist '%s' at %L",
10795 nl->sym->name, sym->name, &sym->declared_at);
10801 for (nl = sym->namelist; nl; nl = nl->next)
10803 /* Reject namelist arrays of assumed shape. */
10804 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
10805 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
10806 "must not have assumed shape in namelist "
10807 "'%s' at %L", nl->sym->name, sym->name,
10808 &sym->declared_at) == FAILURE)
10811 /* Reject namelist arrays that are not constant shape. */
10812 if (is_non_constant_shape_array (nl->sym))
10814 gfc_error ("NAMELIST array object '%s' must have constant "
10815 "shape in namelist '%s' at %L", nl->sym->name,
10816 sym->name, &sym->declared_at);
10820 /* Namelist objects cannot have allocatable or pointer components. */
10821 if (nl->sym->ts.type != BT_DERIVED)
10824 if (nl->sym->ts.u.derived->attr.alloc_comp)
10826 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10827 "have ALLOCATABLE components",
10828 nl->sym->name, sym->name, &sym->declared_at);
10832 if (nl->sym->ts.u.derived->attr.pointer_comp)
10834 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10835 "have POINTER components",
10836 nl->sym->name, sym->name, &sym->declared_at);
10842 /* 14.1.2 A module or internal procedure represent local entities
10843 of the same type as a namelist member and so are not allowed. */
10844 for (nl = sym->namelist; nl; nl = nl->next)
10846 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
10849 if (nl->sym->attr.function && nl->sym == nl->sym->result)
10850 if ((nl->sym == sym->ns->proc_name)
10852 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
10856 if (nl->sym && nl->sym->name)
10857 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
10858 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
10860 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
10861 "attribute in '%s' at %L", nlsym->name,
10862 &sym->declared_at);
10872 resolve_fl_parameter (gfc_symbol *sym)
10874 /* A parameter array's shape needs to be constant. */
10875 if (sym->as != NULL
10876 && (sym->as->type == AS_DEFERRED
10877 || is_non_constant_shape_array (sym)))
10879 gfc_error ("Parameter array '%s' at %L cannot be automatic "
10880 "or of deferred shape", sym->name, &sym->declared_at);
10884 /* Make sure a parameter that has been implicitly typed still
10885 matches the implicit type, since PARAMETER statements can precede
10886 IMPLICIT statements. */
10887 if (sym->attr.implicit_type
10888 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
10891 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
10892 "later IMPLICIT type", sym->name, &sym->declared_at);
10896 /* Make sure the types of derived parameters are consistent. This
10897 type checking is deferred until resolution because the type may
10898 refer to a derived type from the host. */
10899 if (sym->ts.type == BT_DERIVED
10900 && !gfc_compare_types (&sym->ts, &sym->value->ts))
10902 gfc_error ("Incompatible derived type in PARAMETER at %L",
10903 &sym->value->where);
10910 /* Do anything necessary to resolve a symbol. Right now, we just
10911 assume that an otherwise unknown symbol is a variable. This sort
10912 of thing commonly happens for symbols in module. */
10915 resolve_symbol (gfc_symbol *sym)
10917 int check_constant, mp_flag;
10918 gfc_symtree *symtree;
10919 gfc_symtree *this_symtree;
10923 if (sym->attr.flavor == FL_UNKNOWN)
10926 /* If we find that a flavorless symbol is an interface in one of the
10927 parent namespaces, find its symtree in this namespace, free the
10928 symbol and set the symtree to point to the interface symbol. */
10929 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
10931 symtree = gfc_find_symtree (ns->sym_root, sym->name);
10932 if (symtree && symtree->n.sym->generic)
10934 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10938 gfc_free_symbol (sym);
10939 symtree->n.sym->refs++;
10940 this_symtree->n.sym = symtree->n.sym;
10945 /* Otherwise give it a flavor according to such attributes as
10947 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
10948 sym->attr.flavor = FL_VARIABLE;
10951 sym->attr.flavor = FL_PROCEDURE;
10952 if (sym->attr.dimension)
10953 sym->attr.function = 1;
10957 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
10958 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
10960 if (sym->attr.procedure && sym->ts.interface
10961 && sym->attr.if_source != IFSRC_DECL)
10963 if (sym->ts.interface == sym)
10965 gfc_error ("PROCEDURE '%s' at %L may not be used as its own "
10966 "interface", sym->name, &sym->declared_at);
10969 if (sym->ts.interface->attr.procedure)
10971 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared"
10972 " in a later PROCEDURE statement", sym->ts.interface->name,
10973 sym->name,&sym->declared_at);
10977 /* Get the attributes from the interface (now resolved). */
10978 if (sym->ts.interface->attr.if_source
10979 || sym->ts.interface->attr.intrinsic)
10981 gfc_symbol *ifc = sym->ts.interface;
10982 resolve_symbol (ifc);
10984 if (ifc->attr.intrinsic)
10985 resolve_intrinsic (ifc, &ifc->declared_at);
10988 sym->ts = ifc->result->ts;
10991 sym->ts.interface = ifc;
10992 sym->attr.function = ifc->attr.function;
10993 sym->attr.subroutine = ifc->attr.subroutine;
10994 gfc_copy_formal_args (sym, ifc);
10996 sym->attr.allocatable = ifc->attr.allocatable;
10997 sym->attr.pointer = ifc->attr.pointer;
10998 sym->attr.pure = ifc->attr.pure;
10999 sym->attr.elemental = ifc->attr.elemental;
11000 sym->attr.dimension = ifc->attr.dimension;
11001 sym->attr.recursive = ifc->attr.recursive;
11002 sym->attr.always_explicit = ifc->attr.always_explicit;
11003 sym->attr.ext_attr |= ifc->attr.ext_attr;
11004 /* Copy array spec. */
11005 sym->as = gfc_copy_array_spec (ifc->as);
11009 for (i = 0; i < sym->as->rank; i++)
11011 gfc_expr_replace_symbols (sym->as->lower[i], sym);
11012 gfc_expr_replace_symbols (sym->as->upper[i], sym);
11015 /* Copy char length. */
11016 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
11018 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
11019 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
11022 else if (sym->ts.interface->name[0] != '\0')
11024 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
11025 sym->ts.interface->name, sym->name, &sym->declared_at);
11030 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
11033 /* Symbols that are module procedures with results (functions) have
11034 the types and array specification copied for type checking in
11035 procedures that call them, as well as for saving to a module
11036 file. These symbols can't stand the scrutiny that their results
11038 mp_flag = (sym->result != NULL && sym->result != sym);
11041 /* Make sure that the intrinsic is consistent with its internal
11042 representation. This needs to be done before assigning a default
11043 type to avoid spurious warnings. */
11044 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
11045 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
11048 /* Assign default type to symbols that need one and don't have one. */
11049 if (sym->ts.type == BT_UNKNOWN)
11051 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
11052 gfc_set_default_type (sym, 1, NULL);
11054 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
11055 && !sym->attr.function && !sym->attr.subroutine
11056 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
11057 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
11059 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
11061 /* The specific case of an external procedure should emit an error
11062 in the case that there is no implicit type. */
11064 gfc_set_default_type (sym, sym->attr.external, NULL);
11067 /* Result may be in another namespace. */
11068 resolve_symbol (sym->result);
11070 if (!sym->result->attr.proc_pointer)
11072 sym->ts = sym->result->ts;
11073 sym->as = gfc_copy_array_spec (sym->result->as);
11074 sym->attr.dimension = sym->result->attr.dimension;
11075 sym->attr.pointer = sym->result->attr.pointer;
11076 sym->attr.allocatable = sym->result->attr.allocatable;
11082 /* Assumed size arrays and assumed shape arrays must be dummy
11085 if (sym->as != NULL
11086 && ((sym->as->type == AS_ASSUMED_SIZE && !sym->as->cp_was_assumed)
11087 || sym->as->type == AS_ASSUMED_SHAPE)
11088 && sym->attr.dummy == 0)
11090 if (sym->as->type == AS_ASSUMED_SIZE)
11091 gfc_error ("Assumed size array at %L must be a dummy argument",
11092 &sym->declared_at);
11094 gfc_error ("Assumed shape array at %L must be a dummy argument",
11095 &sym->declared_at);
11099 /* Make sure symbols with known intent or optional are really dummy
11100 variable. Because of ENTRY statement, this has to be deferred
11101 until resolution time. */
11103 if (!sym->attr.dummy
11104 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
11106 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
11110 if (sym->attr.value && !sym->attr.dummy)
11112 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
11113 "it is not a dummy argument", sym->name, &sym->declared_at);
11117 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
11119 gfc_charlen *cl = sym->ts.u.cl;
11120 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11122 gfc_error ("Character dummy variable '%s' at %L with VALUE "
11123 "attribute must have constant length",
11124 sym->name, &sym->declared_at);
11128 if (sym->ts.is_c_interop
11129 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
11131 gfc_error ("C interoperable character dummy variable '%s' at %L "
11132 "with VALUE attribute must have length one",
11133 sym->name, &sym->declared_at);
11138 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
11139 do this for something that was implicitly typed because that is handled
11140 in gfc_set_default_type. Handle dummy arguments and procedure
11141 definitions separately. Also, anything that is use associated is not
11142 handled here but instead is handled in the module it is declared in.
11143 Finally, derived type definitions are allowed to be BIND(C) since that
11144 only implies that they're interoperable, and they are checked fully for
11145 interoperability when a variable is declared of that type. */
11146 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
11147 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
11148 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
11150 gfc_try t = SUCCESS;
11152 /* First, make sure the variable is declared at the
11153 module-level scope (J3/04-007, Section 15.3). */
11154 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
11155 sym->attr.in_common == 0)
11157 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
11158 "is neither a COMMON block nor declared at the "
11159 "module level scope", sym->name, &(sym->declared_at));
11162 else if (sym->common_head != NULL)
11164 t = verify_com_block_vars_c_interop (sym->common_head);
11168 /* If type() declaration, we need to verify that the components
11169 of the given type are all C interoperable, etc. */
11170 if (sym->ts.type == BT_DERIVED &&
11171 sym->ts.u.derived->attr.is_c_interop != 1)
11173 /* Make sure the user marked the derived type as BIND(C). If
11174 not, call the verify routine. This could print an error
11175 for the derived type more than once if multiple variables
11176 of that type are declared. */
11177 if (sym->ts.u.derived->attr.is_bind_c != 1)
11178 verify_bind_c_derived_type (sym->ts.u.derived);
11182 /* Verify the variable itself as C interoperable if it
11183 is BIND(C). It is not possible for this to succeed if
11184 the verify_bind_c_derived_type failed, so don't have to handle
11185 any error returned by verify_bind_c_derived_type. */
11186 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11187 sym->common_block);
11192 /* clear the is_bind_c flag to prevent reporting errors more than
11193 once if something failed. */
11194 sym->attr.is_bind_c = 0;
11199 /* If a derived type symbol has reached this point, without its
11200 type being declared, we have an error. Notice that most
11201 conditions that produce undefined derived types have already
11202 been dealt with. However, the likes of:
11203 implicit type(t) (t) ..... call foo (t) will get us here if
11204 the type is not declared in the scope of the implicit
11205 statement. Change the type to BT_UNKNOWN, both because it is so
11206 and to prevent an ICE. */
11207 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
11208 && !sym->ts.u.derived->attr.zero_comp)
11210 gfc_error ("The derived type '%s' at %L is of type '%s', "
11211 "which has not been defined", sym->name,
11212 &sym->declared_at, sym->ts.u.derived->name);
11213 sym->ts.type = BT_UNKNOWN;
11217 /* Make sure that the derived type has been resolved and that the
11218 derived type is visible in the symbol's namespace, if it is a
11219 module function and is not PRIVATE. */
11220 if (sym->ts.type == BT_DERIVED
11221 && sym->ts.u.derived->attr.use_assoc
11222 && sym->ns->proc_name
11223 && sym->ns->proc_name->attr.flavor == FL_MODULE)
11227 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
11230 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
11231 if (!ds && sym->attr.function
11232 && gfc_check_access (sym->attr.access, sym->ns->default_access))
11234 symtree = gfc_new_symtree (&sym->ns->sym_root,
11235 sym->ts.u.derived->name);
11236 symtree->n.sym = sym->ts.u.derived;
11237 sym->ts.u.derived->refs++;
11241 /* Unless the derived-type declaration is use associated, Fortran 95
11242 does not allow public entries of private derived types.
11243 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
11244 161 in 95-006r3. */
11245 if (sym->ts.type == BT_DERIVED
11246 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
11247 && !sym->ts.u.derived->attr.use_assoc
11248 && gfc_check_access (sym->attr.access, sym->ns->default_access)
11249 && !gfc_check_access (sym->ts.u.derived->attr.access,
11250 sym->ts.u.derived->ns->default_access)
11251 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
11252 "of PRIVATE derived type '%s'",
11253 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
11254 : "variable", sym->name, &sym->declared_at,
11255 sym->ts.u.derived->name) == FAILURE)
11258 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
11259 default initialization is defined (5.1.2.4.4). */
11260 if (sym->ts.type == BT_DERIVED
11262 && sym->attr.intent == INTENT_OUT
11264 && sym->as->type == AS_ASSUMED_SIZE)
11266 for (c = sym->ts.u.derived->components; c; c = c->next)
11268 if (c->initializer)
11270 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
11271 "ASSUMED SIZE and so cannot have a default initializer",
11272 sym->name, &sym->declared_at);
11278 switch (sym->attr.flavor)
11281 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
11286 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
11291 if (resolve_fl_namelist (sym) == FAILURE)
11296 if (resolve_fl_parameter (sym) == FAILURE)
11304 /* Resolve array specifier. Check as well some constraints
11305 on COMMON blocks. */
11307 check_constant = sym->attr.in_common && !sym->attr.pointer;
11309 /* Set the formal_arg_flag so that check_conflict will not throw
11310 an error for host associated variables in the specification
11311 expression for an array_valued function. */
11312 if (sym->attr.function && sym->as)
11313 formal_arg_flag = 1;
11315 gfc_resolve_array_spec (sym->as, check_constant);
11317 formal_arg_flag = 0;
11319 /* Resolve formal namespaces. */
11320 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
11321 && !sym->attr.contained && !sym->attr.intrinsic)
11322 gfc_resolve (sym->formal_ns);
11324 /* Make sure the formal namespace is present. */
11325 if (sym->formal && !sym->formal_ns)
11327 gfc_formal_arglist *formal = sym->formal;
11328 while (formal && !formal->sym)
11329 formal = formal->next;
11333 sym->formal_ns = formal->sym->ns;
11334 sym->formal_ns->refs++;
11338 /* Check threadprivate restrictions. */
11339 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
11340 && (!sym->attr.in_common
11341 && sym->module == NULL
11342 && (sym->ns->proc_name == NULL
11343 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
11344 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
11346 /* If we have come this far we can apply default-initializers, as
11347 described in 14.7.5, to those variables that have not already
11348 been assigned one. */
11349 if (sym->ts.type == BT_DERIVED
11350 && sym->attr.referenced
11351 && sym->ns == gfc_current_ns
11353 && !sym->attr.allocatable
11354 && !sym->attr.alloc_comp)
11356 symbol_attribute *a = &sym->attr;
11358 if ((!a->save && !a->dummy && !a->pointer
11359 && !a->in_common && !a->use_assoc
11360 && !(a->function && sym != sym->result))
11361 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
11362 apply_default_init (sym);
11365 /* If this symbol has a type-spec, check it. */
11366 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
11367 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
11368 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
11374 /************* Resolve DATA statements *************/
11378 gfc_data_value *vnode;
11384 /* Advance the values structure to point to the next value in the data list. */
11387 next_data_value (void)
11389 while (mpz_cmp_ui (values.left, 0) == 0)
11392 if (values.vnode->next == NULL)
11395 values.vnode = values.vnode->next;
11396 mpz_set (values.left, values.vnode->repeat);
11404 check_data_variable (gfc_data_variable *var, locus *where)
11410 ar_type mark = AR_UNKNOWN;
11412 mpz_t section_index[GFC_MAX_DIMENSIONS];
11418 if (gfc_resolve_expr (var->expr) == FAILURE)
11422 mpz_init_set_si (offset, 0);
11425 if (e->expr_type != EXPR_VARIABLE)
11426 gfc_internal_error ("check_data_variable(): Bad expression");
11428 sym = e->symtree->n.sym;
11430 if (sym->ns->is_block_data && !sym->attr.in_common)
11432 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
11433 sym->name, &sym->declared_at);
11436 if (e->ref == NULL && sym->as)
11438 gfc_error ("DATA array '%s' at %L must be specified in a previous"
11439 " declaration", sym->name, where);
11443 has_pointer = sym->attr.pointer;
11445 for (ref = e->ref; ref; ref = ref->next)
11447 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
11451 && ref->type == REF_ARRAY
11452 && ref->u.ar.type != AR_FULL)
11454 gfc_error ("DATA element '%s' at %L is a pointer and so must "
11455 "be a full array", sym->name, where);
11460 if (e->rank == 0 || has_pointer)
11462 mpz_init_set_ui (size, 1);
11469 /* Find the array section reference. */
11470 for (ref = e->ref; ref; ref = ref->next)
11472 if (ref->type != REF_ARRAY)
11474 if (ref->u.ar.type == AR_ELEMENT)
11480 /* Set marks according to the reference pattern. */
11481 switch (ref->u.ar.type)
11489 /* Get the start position of array section. */
11490 gfc_get_section_index (ar, section_index, &offset);
11495 gcc_unreachable ();
11498 if (gfc_array_size (e, &size) == FAILURE)
11500 gfc_error ("Nonconstant array section at %L in DATA statement",
11502 mpz_clear (offset);
11509 while (mpz_cmp_ui (size, 0) > 0)
11511 if (next_data_value () == FAILURE)
11513 gfc_error ("DATA statement at %L has more variables than values",
11519 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
11523 /* If we have more than one element left in the repeat count,
11524 and we have more than one element left in the target variable,
11525 then create a range assignment. */
11526 /* FIXME: Only done for full arrays for now, since array sections
11528 if (mark == AR_FULL && ref && ref->next == NULL
11529 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
11533 if (mpz_cmp (size, values.left) >= 0)
11535 mpz_init_set (range, values.left);
11536 mpz_sub (size, size, values.left);
11537 mpz_set_ui (values.left, 0);
11541 mpz_init_set (range, size);
11542 mpz_sub (values.left, values.left, size);
11543 mpz_set_ui (size, 0);
11546 gfc_assign_data_value_range (var->expr, values.vnode->expr,
11549 mpz_add (offset, offset, range);
11553 /* Assign initial value to symbol. */
11556 mpz_sub_ui (values.left, values.left, 1);
11557 mpz_sub_ui (size, size, 1);
11559 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
11563 if (mark == AR_FULL)
11564 mpz_add_ui (offset, offset, 1);
11566 /* Modify the array section indexes and recalculate the offset
11567 for next element. */
11568 else if (mark == AR_SECTION)
11569 gfc_advance_section (section_index, ar, &offset);
11573 if (mark == AR_SECTION)
11575 for (i = 0; i < ar->dimen; i++)
11576 mpz_clear (section_index[i]);
11580 mpz_clear (offset);
11586 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
11588 /* Iterate over a list of elements in a DATA statement. */
11591 traverse_data_list (gfc_data_variable *var, locus *where)
11594 iterator_stack frame;
11595 gfc_expr *e, *start, *end, *step;
11596 gfc_try retval = SUCCESS;
11598 mpz_init (frame.value);
11600 start = gfc_copy_expr (var->iter.start);
11601 end = gfc_copy_expr (var->iter.end);
11602 step = gfc_copy_expr (var->iter.step);
11604 if (gfc_simplify_expr (start, 1) == FAILURE
11605 || start->expr_type != EXPR_CONSTANT)
11607 gfc_error ("iterator start at %L does not simplify", &start->where);
11611 if (gfc_simplify_expr (end, 1) == FAILURE
11612 || end->expr_type != EXPR_CONSTANT)
11614 gfc_error ("iterator end at %L does not simplify", &end->where);
11618 if (gfc_simplify_expr (step, 1) == FAILURE
11619 || step->expr_type != EXPR_CONSTANT)
11621 gfc_error ("iterator step at %L does not simplify", &step->where);
11626 mpz_init_set (trip, end->value.integer);
11627 mpz_sub (trip, trip, start->value.integer);
11628 mpz_add (trip, trip, step->value.integer);
11630 mpz_div (trip, trip, step->value.integer);
11632 mpz_set (frame.value, start->value.integer);
11634 frame.prev = iter_stack;
11635 frame.variable = var->iter.var->symtree;
11636 iter_stack = &frame;
11638 while (mpz_cmp_ui (trip, 0) > 0)
11640 if (traverse_data_var (var->list, where) == FAILURE)
11647 e = gfc_copy_expr (var->expr);
11648 if (gfc_simplify_expr (e, 1) == FAILURE)
11656 mpz_add (frame.value, frame.value, step->value.integer);
11658 mpz_sub_ui (trip, trip, 1);
11663 mpz_clear (frame.value);
11665 gfc_free_expr (start);
11666 gfc_free_expr (end);
11667 gfc_free_expr (step);
11669 iter_stack = frame.prev;
11674 /* Type resolve variables in the variable list of a DATA statement. */
11677 traverse_data_var (gfc_data_variable *var, locus *where)
11681 for (; var; var = var->next)
11683 if (var->expr == NULL)
11684 t = traverse_data_list (var, where);
11686 t = check_data_variable (var, where);
11696 /* Resolve the expressions and iterators associated with a data statement.
11697 This is separate from the assignment checking because data lists should
11698 only be resolved once. */
11701 resolve_data_variables (gfc_data_variable *d)
11703 for (; d; d = d->next)
11705 if (d->list == NULL)
11707 if (gfc_resolve_expr (d->expr) == FAILURE)
11712 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
11715 if (resolve_data_variables (d->list) == FAILURE)
11724 /* Resolve a single DATA statement. We implement this by storing a pointer to
11725 the value list into static variables, and then recursively traversing the
11726 variables list, expanding iterators and such. */
11729 resolve_data (gfc_data *d)
11732 if (resolve_data_variables (d->var) == FAILURE)
11735 values.vnode = d->value;
11736 if (d->value == NULL)
11737 mpz_set_ui (values.left, 0);
11739 mpz_set (values.left, d->value->repeat);
11741 if (traverse_data_var (d->var, &d->where) == FAILURE)
11744 /* At this point, we better not have any values left. */
11746 if (next_data_value () == SUCCESS)
11747 gfc_error ("DATA statement at %L has more values than variables",
11752 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
11753 accessed by host or use association, is a dummy argument to a pure function,
11754 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
11755 is storage associated with any such variable, shall not be used in the
11756 following contexts: (clients of this function). */
11758 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
11759 procedure. Returns zero if assignment is OK, nonzero if there is a
11762 gfc_impure_variable (gfc_symbol *sym)
11767 if (sym->attr.use_assoc || sym->attr.in_common)
11770 /* Check if the symbol's ns is inside the pure procedure. */
11771 for (ns = gfc_current_ns; ns; ns = ns->parent)
11775 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
11779 proc = sym->ns->proc_name;
11780 if (sym->attr.dummy && gfc_pure (proc)
11781 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
11783 proc->attr.function))
11786 /* TODO: Sort out what can be storage associated, if anything, and include
11787 it here. In principle equivalences should be scanned but it does not
11788 seem to be possible to storage associate an impure variable this way. */
11793 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
11794 current namespace is inside a pure procedure. */
11797 gfc_pure (gfc_symbol *sym)
11799 symbol_attribute attr;
11804 /* Check if the current namespace or one of its parents
11805 belongs to a pure procedure. */
11806 for (ns = gfc_current_ns; ns; ns = ns->parent)
11808 sym = ns->proc_name;
11812 if (attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental))
11820 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
11824 /* Test whether the current procedure is elemental or not. */
11827 gfc_elemental (gfc_symbol *sym)
11829 symbol_attribute attr;
11832 sym = gfc_current_ns->proc_name;
11837 return attr.flavor == FL_PROCEDURE && attr.elemental;
11841 /* Warn about unused labels. */
11844 warn_unused_fortran_label (gfc_st_label *label)
11849 warn_unused_fortran_label (label->left);
11851 if (label->defined == ST_LABEL_UNKNOWN)
11854 switch (label->referenced)
11856 case ST_LABEL_UNKNOWN:
11857 gfc_warning ("Label %d at %L defined but not used", label->value,
11861 case ST_LABEL_BAD_TARGET:
11862 gfc_warning ("Label %d at %L defined but cannot be used",
11863 label->value, &label->where);
11870 warn_unused_fortran_label (label->right);
11874 /* Returns the sequence type of a symbol or sequence. */
11877 sequence_type (gfc_typespec ts)
11886 if (ts.u.derived->components == NULL)
11887 return SEQ_NONDEFAULT;
11889 result = sequence_type (ts.u.derived->components->ts);
11890 for (c = ts.u.derived->components->next; c; c = c->next)
11891 if (sequence_type (c->ts) != result)
11897 if (ts.kind != gfc_default_character_kind)
11898 return SEQ_NONDEFAULT;
11900 return SEQ_CHARACTER;
11903 if (ts.kind != gfc_default_integer_kind)
11904 return SEQ_NONDEFAULT;
11906 return SEQ_NUMERIC;
11909 if (!(ts.kind == gfc_default_real_kind
11910 || ts.kind == gfc_default_double_kind))
11911 return SEQ_NONDEFAULT;
11913 return SEQ_NUMERIC;
11916 if (ts.kind != gfc_default_complex_kind)
11917 return SEQ_NONDEFAULT;
11919 return SEQ_NUMERIC;
11922 if (ts.kind != gfc_default_logical_kind)
11923 return SEQ_NONDEFAULT;
11925 return SEQ_NUMERIC;
11928 return SEQ_NONDEFAULT;
11933 /* Resolve derived type EQUIVALENCE object. */
11936 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
11938 gfc_component *c = derived->components;
11943 /* Shall not be an object of nonsequence derived type. */
11944 if (!derived->attr.sequence)
11946 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
11947 "attribute to be an EQUIVALENCE object", sym->name,
11952 /* Shall not have allocatable components. */
11953 if (derived->attr.alloc_comp)
11955 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
11956 "components to be an EQUIVALENCE object",sym->name,
11961 if (sym->attr.in_common && has_default_initializer (sym->ts.u.derived))
11963 gfc_error ("Derived type variable '%s' at %L with default "
11964 "initialization cannot be in EQUIVALENCE with a variable "
11965 "in COMMON", sym->name, &e->where);
11969 for (; c ; c = c->next)
11971 if (c->ts.type == BT_DERIVED
11972 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
11975 /* Shall not be an object of sequence derived type containing a pointer
11976 in the structure. */
11977 if (c->attr.pointer)
11979 gfc_error ("Derived type variable '%s' at %L with pointer "
11980 "component(s) cannot be an EQUIVALENCE object",
11981 sym->name, &e->where);
11989 /* Resolve equivalence object.
11990 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
11991 an allocatable array, an object of nonsequence derived type, an object of
11992 sequence derived type containing a pointer at any level of component
11993 selection, an automatic object, a function name, an entry name, a result
11994 name, a named constant, a structure component, or a subobject of any of
11995 the preceding objects. A substring shall not have length zero. A
11996 derived type shall not have components with default initialization nor
11997 shall two objects of an equivalence group be initialized.
11998 Either all or none of the objects shall have an protected attribute.
11999 The simple constraints are done in symbol.c(check_conflict) and the rest
12000 are implemented here. */
12003 resolve_equivalence (gfc_equiv *eq)
12006 gfc_symbol *first_sym;
12009 locus *last_where = NULL;
12010 seq_type eq_type, last_eq_type;
12011 gfc_typespec *last_ts;
12012 int object, cnt_protected;
12015 last_ts = &eq->expr->symtree->n.sym->ts;
12017 first_sym = eq->expr->symtree->n.sym;
12021 for (object = 1; eq; eq = eq->eq, object++)
12025 e->ts = e->symtree->n.sym->ts;
12026 /* match_varspec might not know yet if it is seeing
12027 array reference or substring reference, as it doesn't
12029 if (e->ref && e->ref->type == REF_ARRAY)
12031 gfc_ref *ref = e->ref;
12032 sym = e->symtree->n.sym;
12034 if (sym->attr.dimension)
12036 ref->u.ar.as = sym->as;
12040 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
12041 if (e->ts.type == BT_CHARACTER
12043 && ref->type == REF_ARRAY
12044 && ref->u.ar.dimen == 1
12045 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
12046 && ref->u.ar.stride[0] == NULL)
12048 gfc_expr *start = ref->u.ar.start[0];
12049 gfc_expr *end = ref->u.ar.end[0];
12052 /* Optimize away the (:) reference. */
12053 if (start == NULL && end == NULL)
12056 e->ref = ref->next;
12058 e->ref->next = ref->next;
12063 ref->type = REF_SUBSTRING;
12065 start = gfc_int_expr (1);
12066 ref->u.ss.start = start;
12067 if (end == NULL && e->ts.u.cl)
12068 end = gfc_copy_expr (e->ts.u.cl->length);
12069 ref->u.ss.end = end;
12070 ref->u.ss.length = e->ts.u.cl;
12077 /* Any further ref is an error. */
12080 gcc_assert (ref->type == REF_ARRAY);
12081 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
12087 if (gfc_resolve_expr (e) == FAILURE)
12090 sym = e->symtree->n.sym;
12092 if (sym->attr.is_protected)
12094 if (cnt_protected > 0 && cnt_protected != object)
12096 gfc_error ("Either all or none of the objects in the "
12097 "EQUIVALENCE set at %L shall have the "
12098 "PROTECTED attribute",
12103 /* Shall not equivalence common block variables in a PURE procedure. */
12104 if (sym->ns->proc_name
12105 && sym->ns->proc_name->attr.pure
12106 && sym->attr.in_common)
12108 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
12109 "object in the pure procedure '%s'",
12110 sym->name, &e->where, sym->ns->proc_name->name);
12114 /* Shall not be a named constant. */
12115 if (e->expr_type == EXPR_CONSTANT)
12117 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
12118 "object", sym->name, &e->where);
12122 if (e->ts.type == BT_DERIVED
12123 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
12126 /* Check that the types correspond correctly:
12128 A numeric sequence structure may be equivalenced to another sequence
12129 structure, an object of default integer type, default real type, double
12130 precision real type, default logical type such that components of the
12131 structure ultimately only become associated to objects of the same
12132 kind. A character sequence structure may be equivalenced to an object
12133 of default character kind or another character sequence structure.
12134 Other objects may be equivalenced only to objects of the same type and
12135 kind parameters. */
12137 /* Identical types are unconditionally OK. */
12138 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
12139 goto identical_types;
12141 last_eq_type = sequence_type (*last_ts);
12142 eq_type = sequence_type (sym->ts);
12144 /* Since the pair of objects is not of the same type, mixed or
12145 non-default sequences can be rejected. */
12147 msg = "Sequence %s with mixed components in EQUIVALENCE "
12148 "statement at %L with different type objects";
12150 && last_eq_type == SEQ_MIXED
12151 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
12153 || (eq_type == SEQ_MIXED
12154 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12155 &e->where) == FAILURE))
12158 msg = "Non-default type object or sequence %s in EQUIVALENCE "
12159 "statement at %L with objects of different type";
12161 && last_eq_type == SEQ_NONDEFAULT
12162 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
12163 last_where) == FAILURE)
12164 || (eq_type == SEQ_NONDEFAULT
12165 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12166 &e->where) == FAILURE))
12169 msg ="Non-CHARACTER object '%s' in default CHARACTER "
12170 "EQUIVALENCE statement at %L";
12171 if (last_eq_type == SEQ_CHARACTER
12172 && eq_type != SEQ_CHARACTER
12173 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12174 &e->where) == FAILURE)
12177 msg ="Non-NUMERIC object '%s' in default NUMERIC "
12178 "EQUIVALENCE statement at %L";
12179 if (last_eq_type == SEQ_NUMERIC
12180 && eq_type != SEQ_NUMERIC
12181 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12182 &e->where) == FAILURE)
12187 last_where = &e->where;
12192 /* Shall not be an automatic array. */
12193 if (e->ref->type == REF_ARRAY
12194 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
12196 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
12197 "an EQUIVALENCE object", sym->name, &e->where);
12204 /* Shall not be a structure component. */
12205 if (r->type == REF_COMPONENT)
12207 gfc_error ("Structure component '%s' at %L cannot be an "
12208 "EQUIVALENCE object",
12209 r->u.c.component->name, &e->where);
12213 /* A substring shall not have length zero. */
12214 if (r->type == REF_SUBSTRING)
12216 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
12218 gfc_error ("Substring at %L has length zero",
12219 &r->u.ss.start->where);
12229 /* Resolve function and ENTRY types, issue diagnostics if needed. */
12232 resolve_fntype (gfc_namespace *ns)
12234 gfc_entry_list *el;
12237 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
12240 /* If there are any entries, ns->proc_name is the entry master
12241 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
12243 sym = ns->entries->sym;
12245 sym = ns->proc_name;
12246 if (sym->result == sym
12247 && sym->ts.type == BT_UNKNOWN
12248 && gfc_set_default_type (sym, 0, NULL) == FAILURE
12249 && !sym->attr.untyped)
12251 gfc_error ("Function '%s' at %L has no IMPLICIT type",
12252 sym->name, &sym->declared_at);
12253 sym->attr.untyped = 1;
12256 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
12257 && !sym->attr.contained
12258 && !gfc_check_access (sym->ts.u.derived->attr.access,
12259 sym->ts.u.derived->ns->default_access)
12260 && gfc_check_access (sym->attr.access, sym->ns->default_access))
12262 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
12263 "%L of PRIVATE type '%s'", sym->name,
12264 &sym->declared_at, sym->ts.u.derived->name);
12268 for (el = ns->entries->next; el; el = el->next)
12270 if (el->sym->result == el->sym
12271 && el->sym->ts.type == BT_UNKNOWN
12272 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
12273 && !el->sym->attr.untyped)
12275 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
12276 el->sym->name, &el->sym->declared_at);
12277 el->sym->attr.untyped = 1;
12283 /* 12.3.2.1.1 Defined operators. */
12286 check_uop_procedure (gfc_symbol *sym, locus where)
12288 gfc_formal_arglist *formal;
12290 if (!sym->attr.function)
12292 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
12293 sym->name, &where);
12297 if (sym->ts.type == BT_CHARACTER
12298 && !(sym->ts.u.cl && sym->ts.u.cl->length)
12299 && !(sym->result && sym->result->ts.u.cl
12300 && sym->result->ts.u.cl->length))
12302 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
12303 "character length", sym->name, &where);
12307 formal = sym->formal;
12308 if (!formal || !formal->sym)
12310 gfc_error ("User operator procedure '%s' at %L must have at least "
12311 "one argument", sym->name, &where);
12315 if (formal->sym->attr.intent != INTENT_IN)
12317 gfc_error ("First argument of operator interface at %L must be "
12318 "INTENT(IN)", &where);
12322 if (formal->sym->attr.optional)
12324 gfc_error ("First argument of operator interface at %L cannot be "
12325 "optional", &where);
12329 formal = formal->next;
12330 if (!formal || !formal->sym)
12333 if (formal->sym->attr.intent != INTENT_IN)
12335 gfc_error ("Second argument of operator interface at %L must be "
12336 "INTENT(IN)", &where);
12340 if (formal->sym->attr.optional)
12342 gfc_error ("Second argument of operator interface at %L cannot be "
12343 "optional", &where);
12349 gfc_error ("Operator interface at %L must have, at most, two "
12350 "arguments", &where);
12358 gfc_resolve_uops (gfc_symtree *symtree)
12360 gfc_interface *itr;
12362 if (symtree == NULL)
12365 gfc_resolve_uops (symtree->left);
12366 gfc_resolve_uops (symtree->right);
12368 for (itr = symtree->n.uop->op; itr; itr = itr->next)
12369 check_uop_procedure (itr->sym, itr->sym->declared_at);
12373 /* Examine all of the expressions associated with a program unit,
12374 assign types to all intermediate expressions, make sure that all
12375 assignments are to compatible types and figure out which names
12376 refer to which functions or subroutines. It doesn't check code
12377 block, which is handled by resolve_code. */
12380 resolve_types (gfc_namespace *ns)
12386 gfc_namespace* old_ns = gfc_current_ns;
12388 /* Check that all IMPLICIT types are ok. */
12389 if (!ns->seen_implicit_none)
12392 for (letter = 0; letter != GFC_LETTERS; ++letter)
12393 if (ns->set_flag[letter]
12394 && resolve_typespec_used (&ns->default_type[letter],
12395 &ns->implicit_loc[letter],
12400 gfc_current_ns = ns;
12402 resolve_entries (ns);
12404 resolve_common_vars (ns->blank_common.head, false);
12405 resolve_common_blocks (ns->common_root);
12407 resolve_contained_functions (ns);
12409 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
12411 for (cl = ns->cl_list; cl; cl = cl->next)
12412 resolve_charlen (cl);
12414 gfc_traverse_ns (ns, resolve_symbol);
12416 resolve_fntype (ns);
12418 for (n = ns->contained; n; n = n->sibling)
12420 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
12421 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
12422 "also be PURE", n->proc_name->name,
12423 &n->proc_name->declared_at);
12429 gfc_check_interfaces (ns);
12431 gfc_traverse_ns (ns, resolve_values);
12437 for (d = ns->data; d; d = d->next)
12441 gfc_traverse_ns (ns, gfc_formalize_init_value);
12443 gfc_traverse_ns (ns, gfc_verify_binding_labels);
12445 if (ns->common_root != NULL)
12446 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
12448 for (eq = ns->equiv; eq; eq = eq->next)
12449 resolve_equivalence (eq);
12451 /* Warn about unused labels. */
12452 if (warn_unused_label)
12453 warn_unused_fortran_label (ns->st_labels);
12455 gfc_resolve_uops (ns->uop_root);
12457 gfc_current_ns = old_ns;
12461 /* Call resolve_code recursively. */
12464 resolve_codes (gfc_namespace *ns)
12467 bitmap_obstack old_obstack;
12469 for (n = ns->contained; n; n = n->sibling)
12472 gfc_current_ns = ns;
12474 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
12475 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
12478 /* Set to an out of range value. */
12479 current_entry_id = -1;
12481 old_obstack = labels_obstack;
12482 bitmap_obstack_initialize (&labels_obstack);
12484 resolve_code (ns->code, ns);
12486 bitmap_obstack_release (&labels_obstack);
12487 labels_obstack = old_obstack;
12491 /* This function is called after a complete program unit has been compiled.
12492 Its purpose is to examine all of the expressions associated with a program
12493 unit, assign types to all intermediate expressions, make sure that all
12494 assignments are to compatible types and figure out which names refer to
12495 which functions or subroutines. */
12498 gfc_resolve (gfc_namespace *ns)
12500 gfc_namespace *old_ns;
12501 code_stack *old_cs_base;
12507 old_ns = gfc_current_ns;
12508 old_cs_base = cs_base;
12510 resolve_types (ns);
12511 resolve_codes (ns);
12513 gfc_current_ns = old_ns;
12514 cs_base = old_cs_base;