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);
930 /****************** Expression name resolution ******************/
932 /* Returns 0 if a symbol was not declared with a type or
933 attribute declaration statement, nonzero otherwise. */
936 was_declared (gfc_symbol *sym)
942 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
945 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
946 || a.optional || a.pointer || a.save || a.target || a.volatile_
947 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
955 /* Determine if a symbol is generic or not. */
958 generic_sym (gfc_symbol *sym)
962 if (sym->attr.generic ||
963 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
966 if (was_declared (sym) || sym->ns->parent == NULL)
969 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
976 return generic_sym (s);
983 /* Determine if a symbol is specific or not. */
986 specific_sym (gfc_symbol *sym)
990 if (sym->attr.if_source == IFSRC_IFBODY
991 || sym->attr.proc == PROC_MODULE
992 || sym->attr.proc == PROC_INTERNAL
993 || sym->attr.proc == PROC_ST_FUNCTION
994 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
995 || sym->attr.external)
998 if (was_declared (sym) || sym->ns->parent == NULL)
1001 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1003 return (s == NULL) ? 0 : specific_sym (s);
1007 /* Figure out if the procedure is specific, generic or unknown. */
1010 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1014 procedure_kind (gfc_symbol *sym)
1016 if (generic_sym (sym))
1017 return PTYPE_GENERIC;
1019 if (specific_sym (sym))
1020 return PTYPE_SPECIFIC;
1022 return PTYPE_UNKNOWN;
1025 /* Check references to assumed size arrays. The flag need_full_assumed_size
1026 is nonzero when matching actual arguments. */
1028 static int need_full_assumed_size = 0;
1031 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1033 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1036 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1037 What should it be? */
1038 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1039 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1040 && (e->ref->u.ar.type == AR_FULL))
1042 gfc_error ("The upper bound in the last dimension must "
1043 "appear in the reference to the assumed size "
1044 "array '%s' at %L", sym->name, &e->where);
1051 /* Look for bad assumed size array references in argument expressions
1052 of elemental and array valued intrinsic procedures. Since this is
1053 called from procedure resolution functions, it only recurses at
1057 resolve_assumed_size_actual (gfc_expr *e)
1062 switch (e->expr_type)
1065 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1070 if (resolve_assumed_size_actual (e->value.op.op1)
1071 || resolve_assumed_size_actual (e->value.op.op2))
1082 /* Check a generic procedure, passed as an actual argument, to see if
1083 there is a matching specific name. If none, it is an error, and if
1084 more than one, the reference is ambiguous. */
1086 count_specific_procs (gfc_expr *e)
1093 sym = e->symtree->n.sym;
1095 for (p = sym->generic; p; p = p->next)
1096 if (strcmp (sym->name, p->sym->name) == 0)
1098 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1104 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1108 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1109 "argument at %L", sym->name, &e->where);
1115 /* See if a call to sym could possibly be a not allowed RECURSION because of
1116 a missing RECURIVE declaration. This means that either sym is the current
1117 context itself, or sym is the parent of a contained procedure calling its
1118 non-RECURSIVE containing procedure.
1119 This also works if sym is an ENTRY. */
1122 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1124 gfc_symbol* proc_sym;
1125 gfc_symbol* context_proc;
1126 gfc_namespace* real_context;
1128 if (sym->attr.flavor == FL_PROGRAM)
1131 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1133 /* If we've got an ENTRY, find real procedure. */
1134 if (sym->attr.entry && sym->ns->entries)
1135 proc_sym = sym->ns->entries->sym;
1139 /* If sym is RECURSIVE, all is well of course. */
1140 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1143 /* Find the context procedure's "real" symbol if it has entries.
1144 We look for a procedure symbol, so recurse on the parents if we don't
1145 find one (like in case of a BLOCK construct). */
1146 for (real_context = context; ; real_context = real_context->parent)
1148 /* We should find something, eventually! */
1149 gcc_assert (real_context);
1151 context_proc = (real_context->entries ? real_context->entries->sym
1152 : real_context->proc_name);
1154 /* In some special cases, there may not be a proc_name, like for this
1156 real(bad_kind()) function foo () ...
1157 when checking the call to bad_kind ().
1158 In these cases, we simply return here and assume that the
1163 if (context_proc->attr.flavor != FL_LABEL)
1167 /* A call from sym's body to itself is recursion, of course. */
1168 if (context_proc == proc_sym)
1171 /* The same is true if context is a contained procedure and sym the
1173 if (context_proc->attr.contained)
1175 gfc_symbol* parent_proc;
1177 gcc_assert (context->parent);
1178 parent_proc = (context->parent->entries ? context->parent->entries->sym
1179 : context->parent->proc_name);
1181 if (parent_proc == proc_sym)
1189 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1190 its typespec and formal argument list. */
1193 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1195 gfc_intrinsic_sym* isym;
1201 /* We already know this one is an intrinsic, so we don't call
1202 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1203 gfc_find_subroutine directly to check whether it is a function or
1206 if ((isym = gfc_find_function (sym->name)))
1208 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1209 && !sym->attr.implicit_type)
1210 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1211 " ignored", sym->name, &sym->declared_at);
1213 if (!sym->attr.function &&
1214 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1219 else if ((isym = gfc_find_subroutine (sym->name)))
1221 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1223 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1224 " specifier", sym->name, &sym->declared_at);
1228 if (!sym->attr.subroutine &&
1229 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1234 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1239 gfc_copy_formal_args_intr (sym, isym);
1241 /* Check it is actually available in the standard settings. */
1242 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1245 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1246 " available in the current standard settings but %s. Use"
1247 " an appropriate -std=* option or enable -fall-intrinsics"
1248 " in order to use it.",
1249 sym->name, &sym->declared_at, symstd);
1257 /* Resolve a procedure expression, like passing it to a called procedure or as
1258 RHS for a procedure pointer assignment. */
1261 resolve_procedure_expression (gfc_expr* expr)
1265 if (expr->expr_type != EXPR_VARIABLE)
1267 gcc_assert (expr->symtree);
1269 sym = expr->symtree->n.sym;
1271 if (sym->attr.intrinsic)
1272 resolve_intrinsic (sym, &expr->where);
1274 if (sym->attr.flavor != FL_PROCEDURE
1275 || (sym->attr.function && sym->result == sym))
1278 /* A non-RECURSIVE procedure that is used as procedure expression within its
1279 own body is in danger of being called recursively. */
1280 if (is_illegal_recursion (sym, gfc_current_ns))
1281 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1282 " itself recursively. Declare it RECURSIVE or use"
1283 " -frecursive", sym->name, &expr->where);
1289 /* Resolve an actual argument list. Most of the time, this is just
1290 resolving the expressions in the list.
1291 The exception is that we sometimes have to decide whether arguments
1292 that look like procedure arguments are really simple variable
1296 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1297 bool no_formal_args)
1300 gfc_symtree *parent_st;
1302 int save_need_full_assumed_size;
1303 gfc_component *comp;
1305 for (; arg; arg = arg->next)
1310 /* Check the label is a valid branching target. */
1313 if (arg->label->defined == ST_LABEL_UNKNOWN)
1315 gfc_error ("Label %d referenced at %L is never defined",
1316 arg->label->value, &arg->label->where);
1323 if (gfc_is_proc_ptr_comp (e, &comp))
1326 if (e->expr_type == EXPR_PPC)
1328 if (comp->as != NULL)
1329 e->rank = comp->as->rank;
1330 e->expr_type = EXPR_FUNCTION;
1332 if (gfc_resolve_expr (e) == FAILURE)
1337 if (e->expr_type == EXPR_VARIABLE
1338 && e->symtree->n.sym->attr.generic
1340 && count_specific_procs (e) != 1)
1343 if (e->ts.type != BT_PROCEDURE)
1345 save_need_full_assumed_size = need_full_assumed_size;
1346 if (e->expr_type != EXPR_VARIABLE)
1347 need_full_assumed_size = 0;
1348 if (gfc_resolve_expr (e) != SUCCESS)
1350 need_full_assumed_size = save_need_full_assumed_size;
1354 /* See if the expression node should really be a variable reference. */
1356 sym = e->symtree->n.sym;
1358 if (sym->attr.flavor == FL_PROCEDURE
1359 || sym->attr.intrinsic
1360 || sym->attr.external)
1364 /* If a procedure is not already determined to be something else
1365 check if it is intrinsic. */
1366 if (!sym->attr.intrinsic
1367 && !(sym->attr.external || sym->attr.use_assoc
1368 || sym->attr.if_source == IFSRC_IFBODY)
1369 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1370 sym->attr.intrinsic = 1;
1372 if (sym->attr.proc == PROC_ST_FUNCTION)
1374 gfc_error ("Statement function '%s' at %L is not allowed as an "
1375 "actual argument", sym->name, &e->where);
1378 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1379 sym->attr.subroutine);
1380 if (sym->attr.intrinsic && actual_ok == 0)
1382 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1383 "actual argument", sym->name, &e->where);
1386 if (sym->attr.contained && !sym->attr.use_assoc
1387 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1389 gfc_error ("Internal procedure '%s' is not allowed as an "
1390 "actual argument at %L", sym->name, &e->where);
1393 if (sym->attr.elemental && !sym->attr.intrinsic)
1395 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1396 "allowed as an actual argument at %L", sym->name,
1400 /* Check if a generic interface has a specific procedure
1401 with the same name before emitting an error. */
1402 if (sym->attr.generic && count_specific_procs (e) != 1)
1405 /* Just in case a specific was found for the expression. */
1406 sym = e->symtree->n.sym;
1408 /* If the symbol is the function that names the current (or
1409 parent) scope, then we really have a variable reference. */
1411 if (gfc_is_function_return_value (sym, sym->ns))
1414 /* If all else fails, see if we have a specific intrinsic. */
1415 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1417 gfc_intrinsic_sym *isym;
1419 isym = gfc_find_function (sym->name);
1420 if (isym == NULL || !isym->specific)
1422 gfc_error ("Unable to find a specific INTRINSIC procedure "
1423 "for the reference '%s' at %L", sym->name,
1428 sym->attr.intrinsic = 1;
1429 sym->attr.function = 1;
1432 if (gfc_resolve_expr (e) == FAILURE)
1437 /* See if the name is a module procedure in a parent unit. */
1439 if (was_declared (sym) || sym->ns->parent == NULL)
1442 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1444 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1448 if (parent_st == NULL)
1451 sym = parent_st->n.sym;
1452 e->symtree = parent_st; /* Point to the right thing. */
1454 if (sym->attr.flavor == FL_PROCEDURE
1455 || sym->attr.intrinsic
1456 || sym->attr.external)
1458 if (gfc_resolve_expr (e) == FAILURE)
1464 e->expr_type = EXPR_VARIABLE;
1466 if (sym->as != NULL)
1468 e->rank = sym->as->rank;
1469 e->ref = gfc_get_ref ();
1470 e->ref->type = REF_ARRAY;
1471 e->ref->u.ar.type = AR_FULL;
1472 e->ref->u.ar.as = sym->as;
1475 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1476 primary.c (match_actual_arg). If above code determines that it
1477 is a variable instead, it needs to be resolved as it was not
1478 done at the beginning of this function. */
1479 save_need_full_assumed_size = need_full_assumed_size;
1480 if (e->expr_type != EXPR_VARIABLE)
1481 need_full_assumed_size = 0;
1482 if (gfc_resolve_expr (e) != SUCCESS)
1484 need_full_assumed_size = save_need_full_assumed_size;
1487 /* Check argument list functions %VAL, %LOC and %REF. There is
1488 nothing to do for %REF. */
1489 if (arg->name && arg->name[0] == '%')
1491 if (strncmp ("%VAL", arg->name, 4) == 0)
1493 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1495 gfc_error ("By-value argument at %L is not of numeric "
1502 gfc_error ("By-value argument at %L cannot be an array or "
1503 "an array section", &e->where);
1507 /* Intrinsics are still PROC_UNKNOWN here. However,
1508 since same file external procedures are not resolvable
1509 in gfortran, it is a good deal easier to leave them to
1511 if (ptype != PROC_UNKNOWN
1512 && ptype != PROC_DUMMY
1513 && ptype != PROC_EXTERNAL
1514 && ptype != PROC_MODULE)
1516 gfc_error ("By-value argument at %L is not allowed "
1517 "in this context", &e->where);
1522 /* Statement functions have already been excluded above. */
1523 else if (strncmp ("%LOC", arg->name, 4) == 0
1524 && e->ts.type == BT_PROCEDURE)
1526 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1528 gfc_error ("Passing internal procedure at %L by location "
1529 "not allowed", &e->where);
1540 /* Do the checks of the actual argument list that are specific to elemental
1541 procedures. If called with c == NULL, we have a function, otherwise if
1542 expr == NULL, we have a subroutine. */
1545 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1547 gfc_actual_arglist *arg0;
1548 gfc_actual_arglist *arg;
1549 gfc_symbol *esym = NULL;
1550 gfc_intrinsic_sym *isym = NULL;
1552 gfc_intrinsic_arg *iformal = NULL;
1553 gfc_formal_arglist *eformal = NULL;
1554 bool formal_optional = false;
1555 bool set_by_optional = false;
1559 /* Is this an elemental procedure? */
1560 if (expr && expr->value.function.actual != NULL)
1562 if (expr->value.function.esym != NULL
1563 && expr->value.function.esym->attr.elemental)
1565 arg0 = expr->value.function.actual;
1566 esym = expr->value.function.esym;
1568 else if (expr->value.function.isym != NULL
1569 && expr->value.function.isym->elemental)
1571 arg0 = expr->value.function.actual;
1572 isym = expr->value.function.isym;
1577 else if (c && c->ext.actual != NULL)
1579 arg0 = c->ext.actual;
1581 if (c->resolved_sym)
1582 esym = c->resolved_sym;
1584 esym = c->symtree->n.sym;
1587 if (!esym->attr.elemental)
1593 /* The rank of an elemental is the rank of its array argument(s). */
1594 for (arg = arg0; arg; arg = arg->next)
1596 if (arg->expr != NULL && arg->expr->rank > 0)
1598 rank = arg->expr->rank;
1599 if (arg->expr->expr_type == EXPR_VARIABLE
1600 && arg->expr->symtree->n.sym->attr.optional)
1601 set_by_optional = true;
1603 /* Function specific; set the result rank and shape. */
1607 if (!expr->shape && arg->expr->shape)
1609 expr->shape = gfc_get_shape (rank);
1610 for (i = 0; i < rank; i++)
1611 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1618 /* If it is an array, it shall not be supplied as an actual argument
1619 to an elemental procedure unless an array of the same rank is supplied
1620 as an actual argument corresponding to a nonoptional dummy argument of
1621 that elemental procedure(12.4.1.5). */
1622 formal_optional = false;
1624 iformal = isym->formal;
1626 eformal = esym->formal;
1628 for (arg = arg0; arg; arg = arg->next)
1632 if (eformal->sym && eformal->sym->attr.optional)
1633 formal_optional = true;
1634 eformal = eformal->next;
1636 else if (isym && iformal)
1638 if (iformal->optional)
1639 formal_optional = true;
1640 iformal = iformal->next;
1643 formal_optional = true;
1645 if (pedantic && arg->expr != NULL
1646 && arg->expr->expr_type == EXPR_VARIABLE
1647 && arg->expr->symtree->n.sym->attr.optional
1650 && (set_by_optional || arg->expr->rank != rank)
1651 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1653 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1654 "MISSING, it cannot be the actual argument of an "
1655 "ELEMENTAL procedure unless there is a non-optional "
1656 "argument with the same rank (12.4.1.5)",
1657 arg->expr->symtree->n.sym->name, &arg->expr->where);
1662 for (arg = arg0; arg; arg = arg->next)
1664 if (arg->expr == NULL || arg->expr->rank == 0)
1667 /* Being elemental, the last upper bound of an assumed size array
1668 argument must be present. */
1669 if (resolve_assumed_size_actual (arg->expr))
1672 /* Elemental procedure's array actual arguments must conform. */
1675 if (gfc_check_conformance (arg->expr, e,
1676 "elemental procedure") == FAILURE)
1683 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1684 is an array, the intent inout/out variable needs to be also an array. */
1685 if (rank > 0 && esym && expr == NULL)
1686 for (eformal = esym->formal, arg = arg0; arg && eformal;
1687 arg = arg->next, eformal = eformal->next)
1688 if ((eformal->sym->attr.intent == INTENT_OUT
1689 || eformal->sym->attr.intent == INTENT_INOUT)
1690 && arg->expr && arg->expr->rank == 0)
1692 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1693 "ELEMENTAL subroutine '%s' is a scalar, but another "
1694 "actual argument is an array", &arg->expr->where,
1695 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1696 : "INOUT", eformal->sym->name, esym->name);
1703 /* Go through each actual argument in ACTUAL and see if it can be
1704 implemented as an inlined, non-copying intrinsic. FNSYM is the
1705 function being called, or NULL if not known. */
1708 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1710 gfc_actual_arglist *ap;
1713 for (ap = actual; ap; ap = ap->next)
1715 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1716 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1718 ap->expr->inline_noncopying_intrinsic = 1;
1722 /* This function does the checking of references to global procedures
1723 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1724 77 and 95 standards. It checks for a gsymbol for the name, making
1725 one if it does not already exist. If it already exists, then the
1726 reference being resolved must correspond to the type of gsymbol.
1727 Otherwise, the new symbol is equipped with the attributes of the
1728 reference. The corresponding code that is called in creating
1729 global entities is parse.c.
1731 In addition, for all but -std=legacy, the gsymbols are used to
1732 check the interfaces of external procedures from the same file.
1733 The namespace of the gsymbol is resolved and then, once this is
1734 done the interface is checked. */
1738 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1740 if (!gsym_ns->proc_name->attr.recursive)
1743 if (sym->ns == gsym_ns)
1746 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1753 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1755 if (gsym_ns->entries)
1757 gfc_entry_list *entry = gsym_ns->entries;
1759 for (; entry; entry = entry->next)
1761 if (strcmp (sym->name, entry->sym->name) == 0)
1763 if (strcmp (gsym_ns->proc_name->name,
1764 sym->ns->proc_name->name) == 0)
1768 && strcmp (gsym_ns->proc_name->name,
1769 sym->ns->parent->proc_name->name) == 0)
1778 resolve_global_procedure (gfc_symbol *sym, locus *where,
1779 gfc_actual_arglist **actual, int sub)
1783 enum gfc_symbol_type type;
1785 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1787 gsym = gfc_get_gsymbol (sym->name);
1789 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1790 gfc_global_used (gsym, where);
1792 if (gfc_option.flag_whole_file
1793 && sym->attr.if_source == IFSRC_UNKNOWN
1794 && gsym->type != GSYM_UNKNOWN
1796 && gsym->ns->resolved != -1
1797 && gsym->ns->proc_name
1798 && not_in_recursive (sym, gsym->ns)
1799 && not_entry_self_reference (sym, gsym->ns))
1801 /* Make sure that translation for the gsymbol occurs before
1802 the procedure currently being resolved. */
1803 ns = gsym->ns->resolved ? NULL : gfc_global_ns_list;
1804 for (; ns && ns != gsym->ns; ns = ns->sibling)
1806 if (ns->sibling == gsym->ns)
1808 ns->sibling = gsym->ns->sibling;
1809 gsym->ns->sibling = gfc_global_ns_list;
1810 gfc_global_ns_list = gsym->ns;
1815 if (!gsym->ns->resolved)
1817 gfc_dt_list *old_dt_list;
1819 /* Stash away derived types so that the backend_decls do not
1821 old_dt_list = gfc_derived_types;
1822 gfc_derived_types = NULL;
1824 gfc_resolve (gsym->ns);
1826 /* Store the new derived types with the global namespace. */
1827 if (gfc_derived_types)
1828 gsym->ns->derived_types = gfc_derived_types;
1830 /* Restore the derived types of this namespace. */
1831 gfc_derived_types = old_dt_list;
1834 if (gsym->ns->proc_name->attr.function
1835 && gsym->ns->proc_name->as
1836 && gsym->ns->proc_name->as->rank
1837 && (!sym->as || sym->as->rank != gsym->ns->proc_name->as->rank))
1838 gfc_error ("The reference to function '%s' at %L either needs an "
1839 "explicit INTERFACE or the rank is incorrect", sym->name,
1842 /* Non-assumed length character functions. */
1843 if (sym->attr.function && sym->ts.type == BT_CHARACTER
1844 && gsym->ns->proc_name->ts.u.cl->length != NULL)
1846 gfc_charlen *cl = sym->ts.u.cl;
1848 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
1849 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
1851 gfc_error ("Nonconstant character-length function '%s' at %L "
1852 "must have an explicit interface", sym->name,
1857 if (gfc_option.flag_whole_file == 1
1858 || ((gfc_option.warn_std & GFC_STD_LEGACY)
1860 !(gfc_option.warn_std & GFC_STD_GNU)))
1861 gfc_errors_to_warnings (1);
1863 gfc_procedure_use (gsym->ns->proc_name, actual, where);
1865 gfc_errors_to_warnings (0);
1868 if (gsym->type == GSYM_UNKNOWN)
1871 gsym->where = *where;
1878 /************* Function resolution *************/
1880 /* Resolve a function call known to be generic.
1881 Section 14.1.2.4.1. */
1884 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1888 if (sym->attr.generic)
1890 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1893 expr->value.function.name = s->name;
1894 expr->value.function.esym = s;
1896 if (s->ts.type != BT_UNKNOWN)
1898 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1899 expr->ts = s->result->ts;
1902 expr->rank = s->as->rank;
1903 else if (s->result != NULL && s->result->as != NULL)
1904 expr->rank = s->result->as->rank;
1906 gfc_set_sym_referenced (expr->value.function.esym);
1911 /* TODO: Need to search for elemental references in generic
1915 if (sym->attr.intrinsic)
1916 return gfc_intrinsic_func_interface (expr, 0);
1923 resolve_generic_f (gfc_expr *expr)
1928 sym = expr->symtree->n.sym;
1932 m = resolve_generic_f0 (expr, sym);
1935 else if (m == MATCH_ERROR)
1939 if (sym->ns->parent == NULL)
1941 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1945 if (!generic_sym (sym))
1949 /* Last ditch attempt. See if the reference is to an intrinsic
1950 that possesses a matching interface. 14.1.2.4 */
1951 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
1953 gfc_error ("There is no specific function for the generic '%s' at %L",
1954 expr->symtree->n.sym->name, &expr->where);
1958 m = gfc_intrinsic_func_interface (expr, 0);
1962 gfc_error ("Generic function '%s' at %L is not consistent with a "
1963 "specific intrinsic interface", expr->symtree->n.sym->name,
1970 /* Resolve a function call known to be specific. */
1973 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
1977 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1979 if (sym->attr.dummy)
1981 sym->attr.proc = PROC_DUMMY;
1985 sym->attr.proc = PROC_EXTERNAL;
1989 if (sym->attr.proc == PROC_MODULE
1990 || sym->attr.proc == PROC_ST_FUNCTION
1991 || sym->attr.proc == PROC_INTERNAL)
1994 if (sym->attr.intrinsic)
1996 m = gfc_intrinsic_func_interface (expr, 1);
2000 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2001 "with an intrinsic", sym->name, &expr->where);
2009 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2012 expr->ts = sym->result->ts;
2015 expr->value.function.name = sym->name;
2016 expr->value.function.esym = sym;
2017 if (sym->as != NULL)
2018 expr->rank = sym->as->rank;
2025 resolve_specific_f (gfc_expr *expr)
2030 sym = expr->symtree->n.sym;
2034 m = resolve_specific_f0 (sym, expr);
2037 if (m == MATCH_ERROR)
2040 if (sym->ns->parent == NULL)
2043 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2049 gfc_error ("Unable to resolve the specific function '%s' at %L",
2050 expr->symtree->n.sym->name, &expr->where);
2056 /* Resolve a procedure call not known to be generic nor specific. */
2059 resolve_unknown_f (gfc_expr *expr)
2064 sym = expr->symtree->n.sym;
2066 if (sym->attr.dummy)
2068 sym->attr.proc = PROC_DUMMY;
2069 expr->value.function.name = sym->name;
2073 /* See if we have an intrinsic function reference. */
2075 if (gfc_is_intrinsic (sym, 0, expr->where))
2077 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2082 /* The reference is to an external name. */
2084 sym->attr.proc = PROC_EXTERNAL;
2085 expr->value.function.name = sym->name;
2086 expr->value.function.esym = expr->symtree->n.sym;
2088 if (sym->as != NULL)
2089 expr->rank = sym->as->rank;
2091 /* Type of the expression is either the type of the symbol or the
2092 default type of the symbol. */
2095 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2097 if (sym->ts.type != BT_UNKNOWN)
2101 ts = gfc_get_default_type (sym->name, sym->ns);
2103 if (ts->type == BT_UNKNOWN)
2105 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2106 sym->name, &expr->where);
2117 /* Return true, if the symbol is an external procedure. */
2119 is_external_proc (gfc_symbol *sym)
2121 if (!sym->attr.dummy && !sym->attr.contained
2122 && !(sym->attr.intrinsic
2123 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2124 && sym->attr.proc != PROC_ST_FUNCTION
2125 && !sym->attr.use_assoc
2133 /* Figure out if a function reference is pure or not. Also set the name
2134 of the function for a potential error message. Return nonzero if the
2135 function is PURE, zero if not. */
2137 pure_stmt_function (gfc_expr *, gfc_symbol *);
2140 pure_function (gfc_expr *e, const char **name)
2146 if (e->symtree != NULL
2147 && e->symtree->n.sym != NULL
2148 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2149 return pure_stmt_function (e, e->symtree->n.sym);
2151 if (e->value.function.esym)
2153 pure = gfc_pure (e->value.function.esym);
2154 *name = e->value.function.esym->name;
2156 else if (e->value.function.isym)
2158 pure = e->value.function.isym->pure
2159 || e->value.function.isym->elemental;
2160 *name = e->value.function.isym->name;
2164 /* Implicit functions are not pure. */
2166 *name = e->value.function.name;
2174 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2175 int *f ATTRIBUTE_UNUSED)
2179 /* Don't bother recursing into other statement functions
2180 since they will be checked individually for purity. */
2181 if (e->expr_type != EXPR_FUNCTION
2183 || e->symtree->n.sym == sym
2184 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2187 return pure_function (e, &name) ? false : true;
2192 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2194 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2199 is_scalar_expr_ptr (gfc_expr *expr)
2201 gfc_try retval = SUCCESS;
2206 /* See if we have a gfc_ref, which means we have a substring, array
2207 reference, or a component. */
2208 if (expr->ref != NULL)
2211 while (ref->next != NULL)
2217 if (ref->u.ss.length != NULL
2218 && ref->u.ss.length->length != NULL
2220 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2222 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2224 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2225 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2226 if (end - start + 1 != 1)
2233 if (ref->u.ar.type == AR_ELEMENT)
2235 else if (ref->u.ar.type == AR_FULL)
2237 /* The user can give a full array if the array is of size 1. */
2238 if (ref->u.ar.as != NULL
2239 && ref->u.ar.as->rank == 1
2240 && ref->u.ar.as->type == AS_EXPLICIT
2241 && ref->u.ar.as->lower[0] != NULL
2242 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2243 && ref->u.ar.as->upper[0] != NULL
2244 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2246 /* If we have a character string, we need to check if
2247 its length is one. */
2248 if (expr->ts.type == BT_CHARACTER)
2250 if (expr->ts.u.cl == NULL
2251 || expr->ts.u.cl->length == NULL
2252 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2258 /* We have constant lower and upper bounds. If the
2259 difference between is 1, it can be considered a
2261 start = (int) mpz_get_si
2262 (ref->u.ar.as->lower[0]->value.integer);
2263 end = (int) mpz_get_si
2264 (ref->u.ar.as->upper[0]->value.integer);
2265 if (end - start + 1 != 1)
2280 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2282 /* Character string. Make sure it's of length 1. */
2283 if (expr->ts.u.cl == NULL
2284 || expr->ts.u.cl->length == NULL
2285 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2288 else if (expr->rank != 0)
2295 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2296 and, in the case of c_associated, set the binding label based on
2300 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2301 gfc_symbol **new_sym)
2303 char name[GFC_MAX_SYMBOL_LEN + 1];
2304 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2305 int optional_arg = 0, is_pointer = 0;
2306 gfc_try retval = SUCCESS;
2307 gfc_symbol *args_sym;
2308 gfc_typespec *arg_ts;
2310 if (args->expr->expr_type == EXPR_CONSTANT
2311 || args->expr->expr_type == EXPR_OP
2312 || args->expr->expr_type == EXPR_NULL)
2314 gfc_error ("Argument to '%s' at %L is not a variable",
2315 sym->name, &(args->expr->where));
2319 args_sym = args->expr->symtree->n.sym;
2321 /* The typespec for the actual arg should be that stored in the expr
2322 and not necessarily that of the expr symbol (args_sym), because
2323 the actual expression could be a part-ref of the expr symbol. */
2324 arg_ts = &(args->expr->ts);
2326 is_pointer = gfc_is_data_pointer (args->expr);
2328 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2330 /* If the user gave two args then they are providing something for
2331 the optional arg (the second cptr). Therefore, set the name and
2332 binding label to the c_associated for two cptrs. Otherwise,
2333 set c_associated to expect one cptr. */
2337 sprintf (name, "%s_2", sym->name);
2338 sprintf (binding_label, "%s_2", sym->binding_label);
2344 sprintf (name, "%s_1", sym->name);
2345 sprintf (binding_label, "%s_1", sym->binding_label);
2349 /* Get a new symbol for the version of c_associated that
2351 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2353 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2354 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2356 sprintf (name, "%s", sym->name);
2357 sprintf (binding_label, "%s", sym->binding_label);
2359 /* Error check the call. */
2360 if (args->next != NULL)
2362 gfc_error_now ("More actual than formal arguments in '%s' "
2363 "call at %L", name, &(args->expr->where));
2366 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2368 /* Make sure we have either the target or pointer attribute. */
2369 if (!args_sym->attr.target && !is_pointer)
2371 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2372 "a TARGET or an associated pointer",
2374 sym->name, &(args->expr->where));
2378 /* See if we have interoperable type and type param. */
2379 if (verify_c_interop (arg_ts) == SUCCESS
2380 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2382 if (args_sym->attr.target == 1)
2384 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2385 has the target attribute and is interoperable. */
2386 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2387 allocatable variable that has the TARGET attribute and
2388 is not an array of zero size. */
2389 if (args_sym->attr.allocatable == 1)
2391 if (args_sym->attr.dimension != 0
2392 && (args_sym->as && args_sym->as->rank == 0))
2394 gfc_error_now ("Allocatable variable '%s' used as a "
2395 "parameter to '%s' at %L must not be "
2396 "an array of zero size",
2397 args_sym->name, sym->name,
2398 &(args->expr->where));
2404 /* A non-allocatable target variable with C
2405 interoperable type and type parameters must be
2407 if (args_sym && args_sym->attr.dimension)
2409 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2411 gfc_error ("Assumed-shape array '%s' at %L "
2412 "cannot be an argument to the "
2413 "procedure '%s' because "
2414 "it is not C interoperable",
2416 &(args->expr->where), sym->name);
2419 else if (args_sym->as->type == AS_DEFERRED)
2421 gfc_error ("Deferred-shape array '%s' at %L "
2422 "cannot be an argument to the "
2423 "procedure '%s' because "
2424 "it is not C interoperable",
2426 &(args->expr->where), sym->name);
2431 /* Make sure it's not a character string. Arrays of
2432 any type should be ok if the variable is of a C
2433 interoperable type. */
2434 if (arg_ts->type == BT_CHARACTER)
2435 if (arg_ts->u.cl != NULL
2436 && (arg_ts->u.cl->length == NULL
2437 || arg_ts->u.cl->length->expr_type
2440 (arg_ts->u.cl->length->value.integer, 1)
2442 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2444 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2445 "at %L must have a length of 1",
2446 args_sym->name, sym->name,
2447 &(args->expr->where));
2453 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2455 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2457 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2458 "associated scalar POINTER", args_sym->name,
2459 sym->name, &(args->expr->where));
2465 /* The parameter is not required to be C interoperable. If it
2466 is not C interoperable, it must be a nonpolymorphic scalar
2467 with no length type parameters. It still must have either
2468 the pointer or target attribute, and it can be
2469 allocatable (but must be allocated when c_loc is called). */
2470 if (args->expr->rank != 0
2471 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2473 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2474 "scalar", args_sym->name, sym->name,
2475 &(args->expr->where));
2478 else if (arg_ts->type == BT_CHARACTER
2479 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2481 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2482 "%L must have a length of 1",
2483 args_sym->name, sym->name,
2484 &(args->expr->where));
2489 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2491 if (args_sym->attr.flavor != FL_PROCEDURE)
2493 /* TODO: Update this error message to allow for procedure
2494 pointers once they are implemented. */
2495 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2497 args_sym->name, sym->name,
2498 &(args->expr->where));
2501 else if (args_sym->attr.is_bind_c != 1)
2503 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2505 args_sym->name, sym->name,
2506 &(args->expr->where));
2511 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2516 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2517 "iso_c_binding function: '%s'!\n", sym->name);
2524 /* Resolve a function call, which means resolving the arguments, then figuring
2525 out which entity the name refers to. */
2526 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2527 to INTENT(OUT) or INTENT(INOUT). */
2530 resolve_function (gfc_expr *expr)
2532 gfc_actual_arglist *arg;
2537 procedure_type p = PROC_INTRINSIC;
2538 bool no_formal_args;
2542 sym = expr->symtree->n.sym;
2544 /* If this is a procedure pointer component, it has already been resolved. */
2545 if (gfc_is_proc_ptr_comp (expr, NULL))
2548 if (sym && sym->attr.intrinsic
2549 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2552 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2554 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2558 /* If this ia a deferred TBP with an abstract interface (which may
2559 of course be referenced), expr->value.function.name will be set. */
2560 if (sym && sym->attr.abstract && !expr->value.function.name)
2562 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2563 sym->name, &expr->where);
2567 /* Switch off assumed size checking and do this again for certain kinds
2568 of procedure, once the procedure itself is resolved. */
2569 need_full_assumed_size++;
2571 if (expr->symtree && expr->symtree->n.sym)
2572 p = expr->symtree->n.sym->attr.proc;
2574 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2575 if (resolve_actual_arglist (expr->value.function.actual,
2576 p, no_formal_args) == FAILURE)
2579 /* Need to setup the call to the correct c_associated, depending on
2580 the number of cptrs to user gives to compare. */
2581 if (sym && sym->attr.is_iso_c == 1)
2583 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2587 /* Get the symtree for the new symbol (resolved func).
2588 the old one will be freed later, when it's no longer used. */
2589 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2592 /* Resume assumed_size checking. */
2593 need_full_assumed_size--;
2595 /* If the procedure is external, check for usage. */
2596 if (sym && is_external_proc (sym))
2597 resolve_global_procedure (sym, &expr->where,
2598 &expr->value.function.actual, 0);
2600 if (sym && sym->ts.type == BT_CHARACTER
2602 && sym->ts.u.cl->length == NULL
2604 && expr->value.function.esym == NULL
2605 && !sym->attr.contained)
2607 /* Internal procedures are taken care of in resolve_contained_fntype. */
2608 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2609 "be used at %L since it is not a dummy argument",
2610 sym->name, &expr->where);
2614 /* See if function is already resolved. */
2616 if (expr->value.function.name != NULL)
2618 if (expr->ts.type == BT_UNKNOWN)
2624 /* Apply the rules of section 14.1.2. */
2626 switch (procedure_kind (sym))
2629 t = resolve_generic_f (expr);
2632 case PTYPE_SPECIFIC:
2633 t = resolve_specific_f (expr);
2637 t = resolve_unknown_f (expr);
2641 gfc_internal_error ("resolve_function(): bad function type");
2645 /* If the expression is still a function (it might have simplified),
2646 then we check to see if we are calling an elemental function. */
2648 if (expr->expr_type != EXPR_FUNCTION)
2651 temp = need_full_assumed_size;
2652 need_full_assumed_size = 0;
2654 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2657 if (omp_workshare_flag
2658 && expr->value.function.esym
2659 && ! gfc_elemental (expr->value.function.esym))
2661 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2662 "in WORKSHARE construct", expr->value.function.esym->name,
2667 #define GENERIC_ID expr->value.function.isym->id
2668 else if (expr->value.function.actual != NULL
2669 && expr->value.function.isym != NULL
2670 && GENERIC_ID != GFC_ISYM_LBOUND
2671 && GENERIC_ID != GFC_ISYM_LEN
2672 && GENERIC_ID != GFC_ISYM_LOC
2673 && GENERIC_ID != GFC_ISYM_PRESENT)
2675 /* Array intrinsics must also have the last upper bound of an
2676 assumed size array argument. UBOUND and SIZE have to be
2677 excluded from the check if the second argument is anything
2680 for (arg = expr->value.function.actual; arg; arg = arg->next)
2682 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2683 && arg->next != NULL && arg->next->expr)
2685 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2688 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2691 if ((int)mpz_get_si (arg->next->expr->value.integer)
2696 if (arg->expr != NULL
2697 && arg->expr->rank > 0
2698 && resolve_assumed_size_actual (arg->expr))
2704 need_full_assumed_size = temp;
2707 if (!pure_function (expr, &name) && name)
2711 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2712 "FORALL %s", name, &expr->where,
2713 forall_flag == 2 ? "mask" : "block");
2716 else if (gfc_pure (NULL))
2718 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2719 "procedure within a PURE procedure", name, &expr->where);
2724 /* Functions without the RECURSIVE attribution are not allowed to
2725 * call themselves. */
2726 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2729 esym = expr->value.function.esym;
2731 if (is_illegal_recursion (esym, gfc_current_ns))
2733 if (esym->attr.entry && esym->ns->entries)
2734 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2735 " function '%s' is not RECURSIVE",
2736 esym->name, &expr->where, esym->ns->entries->sym->name);
2738 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2739 " is not RECURSIVE", esym->name, &expr->where);
2745 /* Character lengths of use associated functions may contains references to
2746 symbols not referenced from the current program unit otherwise. Make sure
2747 those symbols are marked as referenced. */
2749 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2750 && expr->value.function.esym->attr.use_assoc)
2752 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
2756 && !((expr->value.function.esym
2757 && expr->value.function.esym->attr.elemental)
2759 (expr->value.function.isym
2760 && expr->value.function.isym->elemental)))
2761 find_noncopying_intrinsics (expr->value.function.esym,
2762 expr->value.function.actual);
2764 /* Make sure that the expression has a typespec that works. */
2765 if (expr->ts.type == BT_UNKNOWN)
2767 if (expr->symtree->n.sym->result
2768 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
2769 && !expr->symtree->n.sym->result->attr.proc_pointer)
2770 expr->ts = expr->symtree->n.sym->result->ts;
2777 /************* Subroutine resolution *************/
2780 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2786 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2787 sym->name, &c->loc);
2788 else if (gfc_pure (NULL))
2789 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2795 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2799 if (sym->attr.generic)
2801 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2804 c->resolved_sym = s;
2805 pure_subroutine (c, s);
2809 /* TODO: Need to search for elemental references in generic interface. */
2812 if (sym->attr.intrinsic)
2813 return gfc_intrinsic_sub_interface (c, 0);
2820 resolve_generic_s (gfc_code *c)
2825 sym = c->symtree->n.sym;
2829 m = resolve_generic_s0 (c, sym);
2832 else if (m == MATCH_ERROR)
2836 if (sym->ns->parent == NULL)
2838 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2842 if (!generic_sym (sym))
2846 /* Last ditch attempt. See if the reference is to an intrinsic
2847 that possesses a matching interface. 14.1.2.4 */
2848 sym = c->symtree->n.sym;
2850 if (!gfc_is_intrinsic (sym, 1, c->loc))
2852 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2853 sym->name, &c->loc);
2857 m = gfc_intrinsic_sub_interface (c, 0);
2861 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2862 "intrinsic subroutine interface", sym->name, &c->loc);
2868 /* Set the name and binding label of the subroutine symbol in the call
2869 expression represented by 'c' to include the type and kind of the
2870 second parameter. This function is for resolving the appropriate
2871 version of c_f_pointer() and c_f_procpointer(). For example, a
2872 call to c_f_pointer() for a default integer pointer could have a
2873 name of c_f_pointer_i4. If no second arg exists, which is an error
2874 for these two functions, it defaults to the generic symbol's name
2875 and binding label. */
2878 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2879 char *name, char *binding_label)
2881 gfc_expr *arg = NULL;
2885 /* The second arg of c_f_pointer and c_f_procpointer determines
2886 the type and kind for the procedure name. */
2887 arg = c->ext.actual->next->expr;
2891 /* Set up the name to have the given symbol's name,
2892 plus the type and kind. */
2893 /* a derived type is marked with the type letter 'u' */
2894 if (arg->ts.type == BT_DERIVED)
2897 kind = 0; /* set the kind as 0 for now */
2901 type = gfc_type_letter (arg->ts.type);
2902 kind = arg->ts.kind;
2905 if (arg->ts.type == BT_CHARACTER)
2906 /* Kind info for character strings not needed. */
2909 sprintf (name, "%s_%c%d", sym->name, type, kind);
2910 /* Set up the binding label as the given symbol's label plus
2911 the type and kind. */
2912 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2916 /* If the second arg is missing, set the name and label as
2917 was, cause it should at least be found, and the missing
2918 arg error will be caught by compare_parameters(). */
2919 sprintf (name, "%s", sym->name);
2920 sprintf (binding_label, "%s", sym->binding_label);
2927 /* Resolve a generic version of the iso_c_binding procedure given
2928 (sym) to the specific one based on the type and kind of the
2929 argument(s). Currently, this function resolves c_f_pointer() and
2930 c_f_procpointer based on the type and kind of the second argument
2931 (FPTR). Other iso_c_binding procedures aren't specially handled.
2932 Upon successfully exiting, c->resolved_sym will hold the resolved
2933 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2937 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2939 gfc_symbol *new_sym;
2940 /* this is fine, since we know the names won't use the max */
2941 char name[GFC_MAX_SYMBOL_LEN + 1];
2942 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2943 /* default to success; will override if find error */
2944 match m = MATCH_YES;
2946 /* Make sure the actual arguments are in the necessary order (based on the
2947 formal args) before resolving. */
2948 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2950 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2951 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2953 set_name_and_label (c, sym, name, binding_label);
2955 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2957 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2959 /* Make sure we got a third arg if the second arg has non-zero
2960 rank. We must also check that the type and rank are
2961 correct since we short-circuit this check in
2962 gfc_procedure_use() (called above to sort actual args). */
2963 if (c->ext.actual->next->expr->rank != 0)
2965 if(c->ext.actual->next->next == NULL
2966 || c->ext.actual->next->next->expr == NULL)
2969 gfc_error ("Missing SHAPE parameter for call to %s "
2970 "at %L", sym->name, &(c->loc));
2972 else if (c->ext.actual->next->next->expr->ts.type
2974 || c->ext.actual->next->next->expr->rank != 1)
2977 gfc_error ("SHAPE parameter for call to %s at %L must "
2978 "be a rank 1 INTEGER array", sym->name,
2985 if (m != MATCH_ERROR)
2987 /* the 1 means to add the optional arg to formal list */
2988 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
2990 /* for error reporting, say it's declared where the original was */
2991 new_sym->declared_at = sym->declared_at;
2996 /* no differences for c_loc or c_funloc */
3000 /* set the resolved symbol */
3001 if (m != MATCH_ERROR)
3002 c->resolved_sym = new_sym;
3004 c->resolved_sym = sym;
3010 /* Resolve a subroutine call known to be specific. */
3013 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3017 if(sym->attr.is_iso_c)
3019 m = gfc_iso_c_sub_interface (c,sym);
3023 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3025 if (sym->attr.dummy)
3027 sym->attr.proc = PROC_DUMMY;
3031 sym->attr.proc = PROC_EXTERNAL;
3035 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3038 if (sym->attr.intrinsic)
3040 m = gfc_intrinsic_sub_interface (c, 1);
3044 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3045 "with an intrinsic", sym->name, &c->loc);
3053 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3055 c->resolved_sym = sym;
3056 pure_subroutine (c, sym);
3063 resolve_specific_s (gfc_code *c)
3068 sym = c->symtree->n.sym;
3072 m = resolve_specific_s0 (c, sym);
3075 if (m == MATCH_ERROR)
3078 if (sym->ns->parent == NULL)
3081 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3087 sym = c->symtree->n.sym;
3088 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3089 sym->name, &c->loc);
3095 /* Resolve a subroutine call not known to be generic nor specific. */
3098 resolve_unknown_s (gfc_code *c)
3102 sym = c->symtree->n.sym;
3104 if (sym->attr.dummy)
3106 sym->attr.proc = PROC_DUMMY;
3110 /* See if we have an intrinsic function reference. */
3112 if (gfc_is_intrinsic (sym, 1, c->loc))
3114 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3119 /* The reference is to an external name. */
3122 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3124 c->resolved_sym = sym;
3126 pure_subroutine (c, sym);
3132 /* Resolve a subroutine call. Although it was tempting to use the same code
3133 for functions, subroutines and functions are stored differently and this
3134 makes things awkward. */
3137 resolve_call (gfc_code *c)
3140 procedure_type ptype = PROC_INTRINSIC;
3141 gfc_symbol *csym, *sym;
3142 bool no_formal_args;
3144 csym = c->symtree ? c->symtree->n.sym : NULL;
3146 if (csym && csym->ts.type != BT_UNKNOWN)
3148 gfc_error ("'%s' at %L has a type, which is not consistent with "
3149 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3153 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3156 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3157 sym = st ? st->n.sym : NULL;
3158 if (sym && csym != sym
3159 && sym->ns == gfc_current_ns
3160 && sym->attr.flavor == FL_PROCEDURE
3161 && sym->attr.contained)
3164 if (csym->attr.generic)
3165 c->symtree->n.sym = sym;
3168 csym = c->symtree->n.sym;
3172 /* If this ia a deferred TBP with an abstract interface
3173 (which may of course be referenced), c->expr1 will be set. */
3174 if (csym && csym->attr.abstract && !c->expr1)
3176 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3177 csym->name, &c->loc);
3181 /* Subroutines without the RECURSIVE attribution are not allowed to
3182 * call themselves. */
3183 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3185 if (csym->attr.entry && csym->ns->entries)
3186 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3187 " subroutine '%s' is not RECURSIVE",
3188 csym->name, &c->loc, csym->ns->entries->sym->name);
3190 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3191 " is not RECURSIVE", csym->name, &c->loc);
3196 /* Switch off assumed size checking and do this again for certain kinds
3197 of procedure, once the procedure itself is resolved. */
3198 need_full_assumed_size++;
3201 ptype = csym->attr.proc;
3203 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3204 if (resolve_actual_arglist (c->ext.actual, ptype,
3205 no_formal_args) == FAILURE)
3208 /* Resume assumed_size checking. */
3209 need_full_assumed_size--;
3211 /* If external, check for usage. */
3212 if (csym && is_external_proc (csym))
3213 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3216 if (c->resolved_sym == NULL)
3218 c->resolved_isym = NULL;
3219 switch (procedure_kind (csym))
3222 t = resolve_generic_s (c);
3225 case PTYPE_SPECIFIC:
3226 t = resolve_specific_s (c);
3230 t = resolve_unknown_s (c);
3234 gfc_internal_error ("resolve_subroutine(): bad function type");
3238 /* Some checks of elemental subroutine actual arguments. */
3239 if (resolve_elemental_actual (NULL, c) == FAILURE)
3242 if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
3243 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
3248 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3249 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3250 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3251 if their shapes do not match. If either op1->shape or op2->shape is
3252 NULL, return SUCCESS. */
3255 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3262 if (op1->shape != NULL && op2->shape != NULL)
3264 for (i = 0; i < op1->rank; i++)
3266 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3268 gfc_error ("Shapes for operands at %L and %L are not conformable",
3269 &op1->where, &op2->where);
3280 /* Resolve an operator expression node. This can involve replacing the
3281 operation with a user defined function call. */
3284 resolve_operator (gfc_expr *e)
3286 gfc_expr *op1, *op2;
3288 bool dual_locus_error;
3291 /* Resolve all subnodes-- give them types. */
3293 switch (e->value.op.op)
3296 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3299 /* Fall through... */
3302 case INTRINSIC_UPLUS:
3303 case INTRINSIC_UMINUS:
3304 case INTRINSIC_PARENTHESES:
3305 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3310 /* Typecheck the new node. */
3312 op1 = e->value.op.op1;
3313 op2 = e->value.op.op2;
3314 dual_locus_error = false;
3316 if ((op1 && op1->expr_type == EXPR_NULL)
3317 || (op2 && op2->expr_type == EXPR_NULL))
3319 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3323 switch (e->value.op.op)
3325 case INTRINSIC_UPLUS:
3326 case INTRINSIC_UMINUS:
3327 if (op1->ts.type == BT_INTEGER
3328 || op1->ts.type == BT_REAL
3329 || op1->ts.type == BT_COMPLEX)
3335 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3336 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3339 case INTRINSIC_PLUS:
3340 case INTRINSIC_MINUS:
3341 case INTRINSIC_TIMES:
3342 case INTRINSIC_DIVIDE:
3343 case INTRINSIC_POWER:
3344 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3346 gfc_type_convert_binary (e, 1);
3351 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3352 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3353 gfc_typename (&op2->ts));
3356 case INTRINSIC_CONCAT:
3357 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3358 && op1->ts.kind == op2->ts.kind)
3360 e->ts.type = BT_CHARACTER;
3361 e->ts.kind = op1->ts.kind;
3366 _("Operands of string concatenation operator at %%L are %s/%s"),
3367 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3373 case INTRINSIC_NEQV:
3374 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3376 e->ts.type = BT_LOGICAL;
3377 e->ts.kind = gfc_kind_max (op1, op2);
3378 if (op1->ts.kind < e->ts.kind)
3379 gfc_convert_type (op1, &e->ts, 2);
3380 else if (op2->ts.kind < e->ts.kind)
3381 gfc_convert_type (op2, &e->ts, 2);
3385 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3386 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3387 gfc_typename (&op2->ts));
3392 if (op1->ts.type == BT_LOGICAL)
3394 e->ts.type = BT_LOGICAL;
3395 e->ts.kind = op1->ts.kind;
3399 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3400 gfc_typename (&op1->ts));
3404 case INTRINSIC_GT_OS:
3406 case INTRINSIC_GE_OS:
3408 case INTRINSIC_LT_OS:
3410 case INTRINSIC_LE_OS:
3411 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3413 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3417 /* Fall through... */
3420 case INTRINSIC_EQ_OS:
3422 case INTRINSIC_NE_OS:
3423 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3424 && op1->ts.kind == op2->ts.kind)
3426 e->ts.type = BT_LOGICAL;
3427 e->ts.kind = gfc_default_logical_kind;
3431 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3433 gfc_type_convert_binary (e, 1);
3435 e->ts.type = BT_LOGICAL;
3436 e->ts.kind = gfc_default_logical_kind;
3440 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3442 _("Logicals at %%L must be compared with %s instead of %s"),
3443 (e->value.op.op == INTRINSIC_EQ
3444 || e->value.op.op == INTRINSIC_EQ_OS)
3445 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3448 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3449 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3450 gfc_typename (&op2->ts));
3454 case INTRINSIC_USER:
3455 if (e->value.op.uop->op == NULL)
3456 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3457 else if (op2 == NULL)
3458 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3459 e->value.op.uop->name, gfc_typename (&op1->ts));
3461 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3462 e->value.op.uop->name, gfc_typename (&op1->ts),
3463 gfc_typename (&op2->ts));
3467 case INTRINSIC_PARENTHESES:
3469 if (e->ts.type == BT_CHARACTER)
3470 e->ts.u.cl = op1->ts.u.cl;
3474 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3477 /* Deal with arrayness of an operand through an operator. */
3481 switch (e->value.op.op)
3483 case INTRINSIC_PLUS:
3484 case INTRINSIC_MINUS:
3485 case INTRINSIC_TIMES:
3486 case INTRINSIC_DIVIDE:
3487 case INTRINSIC_POWER:
3488 case INTRINSIC_CONCAT:
3492 case INTRINSIC_NEQV:
3494 case INTRINSIC_EQ_OS:
3496 case INTRINSIC_NE_OS:
3498 case INTRINSIC_GT_OS:
3500 case INTRINSIC_GE_OS:
3502 case INTRINSIC_LT_OS:
3504 case INTRINSIC_LE_OS:
3506 if (op1->rank == 0 && op2->rank == 0)
3509 if (op1->rank == 0 && op2->rank != 0)
3511 e->rank = op2->rank;
3513 if (e->shape == NULL)
3514 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3517 if (op1->rank != 0 && op2->rank == 0)
3519 e->rank = op1->rank;
3521 if (e->shape == NULL)
3522 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3525 if (op1->rank != 0 && op2->rank != 0)
3527 if (op1->rank == op2->rank)
3529 e->rank = op1->rank;
3530 if (e->shape == NULL)
3532 t = compare_shapes(op1, op2);
3536 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3541 /* Allow higher level expressions to work. */
3544 /* Try user-defined operators, and otherwise throw an error. */
3545 dual_locus_error = true;
3547 _("Inconsistent ranks for operator at %%L and %%L"));
3554 case INTRINSIC_PARENTHESES:
3556 case INTRINSIC_UPLUS:
3557 case INTRINSIC_UMINUS:
3558 /* Simply copy arrayness attribute */
3559 e->rank = op1->rank;
3561 if (e->shape == NULL)
3562 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3570 /* Attempt to simplify the expression. */
3573 t = gfc_simplify_expr (e, 0);
3574 /* Some calls do not succeed in simplification and return FAILURE
3575 even though there is no error; e.g. variable references to
3576 PARAMETER arrays. */
3577 if (!gfc_is_constant_expr (e))
3586 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3593 if (dual_locus_error)
3594 gfc_error (msg, &op1->where, &op2->where);
3596 gfc_error (msg, &e->where);
3602 /************** Array resolution subroutines **************/
3605 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3608 /* Compare two integer expressions. */
3611 compare_bound (gfc_expr *a, gfc_expr *b)
3615 if (a == NULL || a->expr_type != EXPR_CONSTANT
3616 || b == NULL || b->expr_type != EXPR_CONSTANT)
3619 /* If either of the types isn't INTEGER, we must have
3620 raised an error earlier. */
3622 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3625 i = mpz_cmp (a->value.integer, b->value.integer);
3635 /* Compare an integer expression with an integer. */
3638 compare_bound_int (gfc_expr *a, int b)
3642 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3645 if (a->ts.type != BT_INTEGER)
3646 gfc_internal_error ("compare_bound_int(): Bad expression");
3648 i = mpz_cmp_si (a->value.integer, b);
3658 /* Compare an integer expression with a mpz_t. */
3661 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3665 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3668 if (a->ts.type != BT_INTEGER)
3669 gfc_internal_error ("compare_bound_int(): Bad expression");
3671 i = mpz_cmp (a->value.integer, b);
3681 /* Compute the last value of a sequence given by a triplet.
3682 Return 0 if it wasn't able to compute the last value, or if the
3683 sequence if empty, and 1 otherwise. */
3686 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3687 gfc_expr *stride, mpz_t last)
3691 if (start == NULL || start->expr_type != EXPR_CONSTANT
3692 || end == NULL || end->expr_type != EXPR_CONSTANT
3693 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3696 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3697 || (stride != NULL && stride->ts.type != BT_INTEGER))
3700 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3702 if (compare_bound (start, end) == CMP_GT)
3704 mpz_set (last, end->value.integer);
3708 if (compare_bound_int (stride, 0) == CMP_GT)
3710 /* Stride is positive */
3711 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3716 /* Stride is negative */
3717 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3722 mpz_sub (rem, end->value.integer, start->value.integer);
3723 mpz_tdiv_r (rem, rem, stride->value.integer);
3724 mpz_sub (last, end->value.integer, rem);
3731 /* Compare a single dimension of an array reference to the array
3735 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3739 /* Given start, end and stride values, calculate the minimum and
3740 maximum referenced indexes. */
3742 switch (ar->dimen_type[i])
3748 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3750 gfc_warning ("Array reference at %L is out of bounds "
3751 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3752 mpz_get_si (ar->start[i]->value.integer),
3753 mpz_get_si (as->lower[i]->value.integer), i+1);
3756 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3758 gfc_warning ("Array reference at %L is out of bounds "
3759 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3760 mpz_get_si (ar->start[i]->value.integer),
3761 mpz_get_si (as->upper[i]->value.integer), i+1);
3769 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3770 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3772 comparison comp_start_end = compare_bound (AR_START, AR_END);
3774 /* Check for zero stride, which is not allowed. */
3775 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3777 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3781 /* if start == len || (stride > 0 && start < len)
3782 || (stride < 0 && start > len),
3783 then the array section contains at least one element. In this
3784 case, there is an out-of-bounds access if
3785 (start < lower || start > upper). */
3786 if (compare_bound (AR_START, AR_END) == CMP_EQ
3787 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3788 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3789 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3790 && comp_start_end == CMP_GT))
3792 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3794 gfc_warning ("Lower array reference at %L is out of bounds "
3795 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3796 mpz_get_si (AR_START->value.integer),
3797 mpz_get_si (as->lower[i]->value.integer), i+1);
3800 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3802 gfc_warning ("Lower array reference at %L is out of bounds "
3803 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3804 mpz_get_si (AR_START->value.integer),
3805 mpz_get_si (as->upper[i]->value.integer), i+1);
3810 /* If we can compute the highest index of the array section,
3811 then it also has to be between lower and upper. */
3812 mpz_init (last_value);
3813 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3816 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3818 gfc_warning ("Upper array reference at %L is out of bounds "
3819 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3820 mpz_get_si (last_value),
3821 mpz_get_si (as->lower[i]->value.integer), i+1);
3822 mpz_clear (last_value);
3825 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3827 gfc_warning ("Upper array reference at %L is out of bounds "
3828 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3829 mpz_get_si (last_value),
3830 mpz_get_si (as->upper[i]->value.integer), i+1);
3831 mpz_clear (last_value);
3835 mpz_clear (last_value);
3843 gfc_internal_error ("check_dimension(): Bad array reference");
3850 /* Compare an array reference with an array specification. */
3853 compare_spec_to_ref (gfc_array_ref *ar)
3860 /* TODO: Full array sections are only allowed as actual parameters. */
3861 if (as->type == AS_ASSUMED_SIZE
3862 && (/*ar->type == AR_FULL
3863 ||*/ (ar->type == AR_SECTION
3864 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3866 gfc_error ("Rightmost upper bound of assumed size array section "
3867 "not specified at %L", &ar->where);
3871 if (ar->type == AR_FULL)
3874 if (as->rank != ar->dimen)
3876 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3877 &ar->where, ar->dimen, as->rank);
3881 for (i = 0; i < as->rank; i++)
3882 if (check_dimension (i, ar, as) == FAILURE)
3889 /* Resolve one part of an array index. */
3892 gfc_resolve_index (gfc_expr *index, int check_scalar)
3899 if (gfc_resolve_expr (index) == FAILURE)
3902 if (check_scalar && index->rank != 0)
3904 gfc_error ("Array index at %L must be scalar", &index->where);
3908 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
3910 gfc_error ("Array index at %L must be of INTEGER type, found %s",
3911 &index->where, gfc_basic_typename (index->ts.type));
3915 if (index->ts.type == BT_REAL)
3916 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
3917 &index->where) == FAILURE)
3920 if (index->ts.kind != gfc_index_integer_kind
3921 || index->ts.type != BT_INTEGER)
3924 ts.type = BT_INTEGER;
3925 ts.kind = gfc_index_integer_kind;
3927 gfc_convert_type_warn (index, &ts, 2, 0);
3933 /* Resolve a dim argument to an intrinsic function. */
3936 gfc_resolve_dim_arg (gfc_expr *dim)
3941 if (gfc_resolve_expr (dim) == FAILURE)
3946 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3951 if (dim->ts.type != BT_INTEGER)
3953 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3957 if (dim->ts.kind != gfc_index_integer_kind)
3961 ts.type = BT_INTEGER;
3962 ts.kind = gfc_index_integer_kind;
3964 gfc_convert_type_warn (dim, &ts, 2, 0);
3970 /* Given an expression that contains array references, update those array
3971 references to point to the right array specifications. While this is
3972 filled in during matching, this information is difficult to save and load
3973 in a module, so we take care of it here.
3975 The idea here is that the original array reference comes from the
3976 base symbol. We traverse the list of reference structures, setting
3977 the stored reference to references. Component references can
3978 provide an additional array specification. */
3981 find_array_spec (gfc_expr *e)
3985 gfc_symbol *derived;
3988 if (e->symtree->n.sym->ts.type == BT_CLASS)
3989 as = e->symtree->n.sym->ts.u.derived->components->as;
3991 as = e->symtree->n.sym->as;
3994 for (ref = e->ref; ref; ref = ref->next)
3999 gfc_internal_error ("find_array_spec(): Missing spec");
4006 if (derived == NULL)
4007 derived = e->symtree->n.sym->ts.u.derived;
4009 c = derived->components;
4011 for (; c; c = c->next)
4012 if (c == ref->u.c.component)
4014 /* Track the sequence of component references. */
4015 if (c->ts.type == BT_DERIVED)
4016 derived = c->ts.u.derived;
4021 gfc_internal_error ("find_array_spec(): Component not found");
4023 if (c->attr.dimension)
4026 gfc_internal_error ("find_array_spec(): unused as(1)");
4037 gfc_internal_error ("find_array_spec(): unused as(2)");
4041 /* Resolve an array reference. */
4044 resolve_array_ref (gfc_array_ref *ar)
4046 int i, check_scalar;
4049 for (i = 0; i < ar->dimen; i++)
4051 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4053 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
4055 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4057 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4062 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4066 ar->dimen_type[i] = DIMEN_ELEMENT;
4070 ar->dimen_type[i] = DIMEN_VECTOR;
4071 if (e->expr_type == EXPR_VARIABLE
4072 && e->symtree->n.sym->ts.type == BT_DERIVED)
4073 ar->start[i] = gfc_get_parentheses (e);
4077 gfc_error ("Array index at %L is an array of rank %d",
4078 &ar->c_where[i], e->rank);
4083 /* If the reference type is unknown, figure out what kind it is. */
4085 if (ar->type == AR_UNKNOWN)
4087 ar->type = AR_ELEMENT;
4088 for (i = 0; i < ar->dimen; i++)
4089 if (ar->dimen_type[i] == DIMEN_RANGE
4090 || ar->dimen_type[i] == DIMEN_VECTOR)
4092 ar->type = AR_SECTION;
4097 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4105 resolve_substring (gfc_ref *ref)
4107 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4109 if (ref->u.ss.start != NULL)
4111 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4114 if (ref->u.ss.start->ts.type != BT_INTEGER)
4116 gfc_error ("Substring start index at %L must be of type INTEGER",
4117 &ref->u.ss.start->where);
4121 if (ref->u.ss.start->rank != 0)
4123 gfc_error ("Substring start index at %L must be scalar",
4124 &ref->u.ss.start->where);
4128 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4129 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4130 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4132 gfc_error ("Substring start index at %L is less than one",
4133 &ref->u.ss.start->where);
4138 if (ref->u.ss.end != NULL)
4140 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4143 if (ref->u.ss.end->ts.type != BT_INTEGER)
4145 gfc_error ("Substring end index at %L must be of type INTEGER",
4146 &ref->u.ss.end->where);
4150 if (ref->u.ss.end->rank != 0)
4152 gfc_error ("Substring end index at %L must be scalar",
4153 &ref->u.ss.end->where);
4157 if (ref->u.ss.length != NULL
4158 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4159 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4160 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4162 gfc_error ("Substring end index at %L exceeds the string length",
4163 &ref->u.ss.start->where);
4167 if (compare_bound_mpz_t (ref->u.ss.end,
4168 gfc_integer_kinds[k].huge) == CMP_GT
4169 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4170 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4172 gfc_error ("Substring end index at %L is too large",
4173 &ref->u.ss.end->where);
4182 /* This function supplies missing substring charlens. */
4185 gfc_resolve_substring_charlen (gfc_expr *e)
4188 gfc_expr *start, *end;
4190 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4191 if (char_ref->type == REF_SUBSTRING)
4197 gcc_assert (char_ref->next == NULL);
4201 if (e->ts.u.cl->length)
4202 gfc_free_expr (e->ts.u.cl->length);
4203 else if (e->expr_type == EXPR_VARIABLE
4204 && e->symtree->n.sym->attr.dummy)
4208 e->ts.type = BT_CHARACTER;
4209 e->ts.kind = gfc_default_character_kind;
4212 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4214 if (char_ref->u.ss.start)
4215 start = gfc_copy_expr (char_ref->u.ss.start);
4217 start = gfc_int_expr (1);
4219 if (char_ref->u.ss.end)
4220 end = gfc_copy_expr (char_ref->u.ss.end);
4221 else if (e->expr_type == EXPR_VARIABLE)
4222 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4229 /* Length = (end - start +1). */
4230 e->ts.u.cl->length = gfc_subtract (end, start);
4231 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length, gfc_int_expr (1));
4233 e->ts.u.cl->length->ts.type = BT_INTEGER;
4234 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4236 /* Make sure that the length is simplified. */
4237 gfc_simplify_expr (e->ts.u.cl->length, 1);
4238 gfc_resolve_expr (e->ts.u.cl->length);
4242 /* Resolve subtype references. */
4245 resolve_ref (gfc_expr *expr)
4247 int current_part_dimension, n_components, seen_part_dimension;
4250 for (ref = expr->ref; ref; ref = ref->next)
4251 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4253 find_array_spec (expr);
4257 for (ref = expr->ref; ref; ref = ref->next)
4261 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4269 resolve_substring (ref);
4273 /* Check constraints on part references. */
4275 current_part_dimension = 0;
4276 seen_part_dimension = 0;
4279 for (ref = expr->ref; ref; ref = ref->next)
4284 switch (ref->u.ar.type)
4288 current_part_dimension = 1;
4292 current_part_dimension = 0;
4296 gfc_internal_error ("resolve_ref(): Bad array reference");
4302 if (current_part_dimension || seen_part_dimension)
4305 if (ref->u.c.component->attr.pointer
4306 || ref->u.c.component->attr.proc_pointer)
4308 gfc_error ("Component to the right of a part reference "
4309 "with nonzero rank must not have the POINTER "
4310 "attribute at %L", &expr->where);
4313 else if (ref->u.c.component->attr.allocatable)
4315 gfc_error ("Component to the right of a part reference "
4316 "with nonzero rank must not have the ALLOCATABLE "
4317 "attribute at %L", &expr->where);
4329 if (((ref->type == REF_COMPONENT && n_components > 1)
4330 || ref->next == NULL)
4331 && current_part_dimension
4332 && seen_part_dimension)
4334 gfc_error ("Two or more part references with nonzero rank must "
4335 "not be specified at %L", &expr->where);
4339 if (ref->type == REF_COMPONENT)
4341 if (current_part_dimension)
4342 seen_part_dimension = 1;
4344 /* reset to make sure */
4345 current_part_dimension = 0;
4353 /* Given an expression, determine its shape. This is easier than it sounds.
4354 Leaves the shape array NULL if it is not possible to determine the shape. */
4357 expression_shape (gfc_expr *e)
4359 mpz_t array[GFC_MAX_DIMENSIONS];
4362 if (e->rank == 0 || e->shape != NULL)
4365 for (i = 0; i < e->rank; i++)
4366 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4369 e->shape = gfc_get_shape (e->rank);
4371 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4376 for (i--; i >= 0; i--)
4377 mpz_clear (array[i]);
4381 /* Given a variable expression node, compute the rank of the expression by
4382 examining the base symbol and any reference structures it may have. */
4385 expression_rank (gfc_expr *e)
4390 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4391 could lead to serious confusion... */
4392 gcc_assert (e->expr_type != EXPR_COMPCALL);
4396 if (e->expr_type == EXPR_ARRAY)
4398 /* Constructors can have a rank different from one via RESHAPE(). */
4400 if (e->symtree == NULL)
4406 e->rank = (e->symtree->n.sym->as == NULL)
4407 ? 0 : e->symtree->n.sym->as->rank;
4413 for (ref = e->ref; ref; ref = ref->next)
4415 if (ref->type != REF_ARRAY)
4418 if (ref->u.ar.type == AR_FULL)
4420 rank = ref->u.ar.as->rank;
4424 if (ref->u.ar.type == AR_SECTION)
4426 /* Figure out the rank of the section. */
4428 gfc_internal_error ("expression_rank(): Two array specs");
4430 for (i = 0; i < ref->u.ar.dimen; i++)
4431 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4432 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4442 expression_shape (e);
4446 /* Resolve a variable expression. */
4449 resolve_variable (gfc_expr *e)
4456 if (e->symtree == NULL)
4459 if (e->ref && resolve_ref (e) == FAILURE)
4462 sym = e->symtree->n.sym;
4463 if (sym->attr.flavor == FL_PROCEDURE
4464 && (!sym->attr.function
4465 || (sym->attr.function && sym->result
4466 && sym->result->attr.proc_pointer
4467 && !sym->result->attr.function)))
4469 e->ts.type = BT_PROCEDURE;
4470 goto resolve_procedure;
4473 if (sym->ts.type != BT_UNKNOWN)
4474 gfc_variable_attr (e, &e->ts);
4477 /* Must be a simple variable reference. */
4478 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4483 if (check_assumed_size_reference (sym, e))
4486 /* Deal with forward references to entries during resolve_code, to
4487 satisfy, at least partially, 12.5.2.5. */
4488 if (gfc_current_ns->entries
4489 && current_entry_id == sym->entry_id
4492 && cs_base->current->op != EXEC_ENTRY)
4494 gfc_entry_list *entry;
4495 gfc_formal_arglist *formal;
4499 /* If the symbol is a dummy... */
4500 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4502 entry = gfc_current_ns->entries;
4505 /* ...test if the symbol is a parameter of previous entries. */
4506 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4507 for (formal = entry->sym->formal; formal; formal = formal->next)
4509 if (formal->sym && sym->name == formal->sym->name)
4513 /* If it has not been seen as a dummy, this is an error. */
4516 if (specification_expr)
4517 gfc_error ("Variable '%s', used in a specification expression"
4518 ", is referenced at %L before the ENTRY statement "
4519 "in which it is a parameter",
4520 sym->name, &cs_base->current->loc);
4522 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4523 "statement in which it is a parameter",
4524 sym->name, &cs_base->current->loc);
4529 /* Now do the same check on the specification expressions. */
4530 specification_expr = 1;
4531 if (sym->ts.type == BT_CHARACTER
4532 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
4536 for (n = 0; n < sym->as->rank; n++)
4538 specification_expr = 1;
4539 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4541 specification_expr = 1;
4542 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4545 specification_expr = 0;
4548 /* Update the symbol's entry level. */
4549 sym->entry_id = current_entry_id + 1;
4553 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
4560 /* Checks to see that the correct symbol has been host associated.
4561 The only situation where this arises is that in which a twice
4562 contained function is parsed after the host association is made.
4563 Therefore, on detecting this, change the symbol in the expression
4564 and convert the array reference into an actual arglist if the old
4565 symbol is a variable. */
4567 check_host_association (gfc_expr *e)
4569 gfc_symbol *sym, *old_sym;
4573 gfc_actual_arglist *arg, *tail = NULL;
4574 bool retval = e->expr_type == EXPR_FUNCTION;
4576 /* If the expression is the result of substitution in
4577 interface.c(gfc_extend_expr) because there is no way in
4578 which the host association can be wrong. */
4579 if (e->symtree == NULL
4580 || e->symtree->n.sym == NULL
4581 || e->user_operator)
4584 old_sym = e->symtree->n.sym;
4586 if (gfc_current_ns->parent
4587 && old_sym->ns != gfc_current_ns)
4589 /* Use the 'USE' name so that renamed module symbols are
4590 correctly handled. */
4591 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
4593 if (sym && old_sym != sym
4594 && sym->ts.type == old_sym->ts.type
4595 && sym->attr.flavor == FL_PROCEDURE
4596 && sym->attr.contained)
4598 /* Clear the shape, since it might not be valid. */
4599 if (e->shape != NULL)
4601 for (n = 0; n < e->rank; n++)
4602 mpz_clear (e->shape[n]);
4604 gfc_free (e->shape);
4607 /* Give the expression the right symtree! */
4608 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
4609 gcc_assert (st != NULL);
4611 if (old_sym->attr.flavor == FL_PROCEDURE
4612 || e->expr_type == EXPR_FUNCTION)
4614 /* Original was function so point to the new symbol, since
4615 the actual argument list is already attached to the
4617 e->value.function.esym = NULL;
4622 /* Original was variable so convert array references into
4623 an actual arglist. This does not need any checking now
4624 since gfc_resolve_function will take care of it. */
4625 e->value.function.actual = NULL;
4626 e->expr_type = EXPR_FUNCTION;
4629 /* Ambiguity will not arise if the array reference is not
4630 the last reference. */
4631 for (ref = e->ref; ref; ref = ref->next)
4632 if (ref->type == REF_ARRAY && ref->next == NULL)
4635 gcc_assert (ref->type == REF_ARRAY);
4637 /* Grab the start expressions from the array ref and
4638 copy them into actual arguments. */
4639 for (n = 0; n < ref->u.ar.dimen; n++)
4641 arg = gfc_get_actual_arglist ();
4642 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4643 if (e->value.function.actual == NULL)
4644 tail = e->value.function.actual = arg;
4652 /* Dump the reference list and set the rank. */
4653 gfc_free_ref_list (e->ref);
4655 e->rank = sym->as ? sym->as->rank : 0;
4658 gfc_resolve_expr (e);
4662 /* This might have changed! */
4663 return e->expr_type == EXPR_FUNCTION;
4668 gfc_resolve_character_operator (gfc_expr *e)
4670 gfc_expr *op1 = e->value.op.op1;
4671 gfc_expr *op2 = e->value.op.op2;
4672 gfc_expr *e1 = NULL;
4673 gfc_expr *e2 = NULL;
4675 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
4677 if (op1->ts.u.cl && op1->ts.u.cl->length)
4678 e1 = gfc_copy_expr (op1->ts.u.cl->length);
4679 else if (op1->expr_type == EXPR_CONSTANT)
4680 e1 = gfc_int_expr (op1->value.character.length);
4682 if (op2->ts.u.cl && op2->ts.u.cl->length)
4683 e2 = gfc_copy_expr (op2->ts.u.cl->length);
4684 else if (op2->expr_type == EXPR_CONSTANT)
4685 e2 = gfc_int_expr (op2->value.character.length);
4687 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4692 e->ts.u.cl->length = gfc_add (e1, e2);
4693 e->ts.u.cl->length->ts.type = BT_INTEGER;
4694 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4695 gfc_simplify_expr (e->ts.u.cl->length, 0);
4696 gfc_resolve_expr (e->ts.u.cl->length);
4702 /* Ensure that an character expression has a charlen and, if possible, a
4703 length expression. */
4706 fixup_charlen (gfc_expr *e)
4708 /* The cases fall through so that changes in expression type and the need
4709 for multiple fixes are picked up. In all circumstances, a charlen should
4710 be available for the middle end to hang a backend_decl on. */
4711 switch (e->expr_type)
4714 gfc_resolve_character_operator (e);
4717 if (e->expr_type == EXPR_ARRAY)
4718 gfc_resolve_character_array_constructor (e);
4720 case EXPR_SUBSTRING:
4721 if (!e->ts.u.cl && e->ref)
4722 gfc_resolve_substring_charlen (e);
4726 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4733 /* Update an actual argument to include the passed-object for type-bound
4734 procedures at the right position. */
4736 static gfc_actual_arglist*
4737 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
4740 gcc_assert (argpos > 0);
4744 gfc_actual_arglist* result;
4746 result = gfc_get_actual_arglist ();
4750 result->name = name;
4756 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
4758 lst = update_arglist_pass (NULL, po, argpos - 1, name);
4763 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
4766 extract_compcall_passed_object (gfc_expr* e)
4770 gcc_assert (e->expr_type == EXPR_COMPCALL);
4772 if (e->value.compcall.base_object)
4773 po = gfc_copy_expr (e->value.compcall.base_object);
4776 po = gfc_get_expr ();
4777 po->expr_type = EXPR_VARIABLE;
4778 po->symtree = e->symtree;
4779 po->ref = gfc_copy_ref (e->ref);
4782 if (gfc_resolve_expr (po) == FAILURE)
4789 /* Update the arglist of an EXPR_COMPCALL expression to include the
4793 update_compcall_arglist (gfc_expr* e)
4796 gfc_typebound_proc* tbp;
4798 tbp = e->value.compcall.tbp;
4803 po = extract_compcall_passed_object (e);
4807 if (tbp->nopass || e->value.compcall.ignore_pass)
4813 gcc_assert (tbp->pass_arg_num > 0);
4814 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4822 /* Extract the passed object from a PPC call (a copy of it). */
4825 extract_ppc_passed_object (gfc_expr *e)
4830 po = gfc_get_expr ();
4831 po->expr_type = EXPR_VARIABLE;
4832 po->symtree = e->symtree;
4833 po->ref = gfc_copy_ref (e->ref);
4835 /* Remove PPC reference. */
4837 while ((*ref)->next)
4838 (*ref) = (*ref)->next;
4839 gfc_free_ref_list (*ref);
4842 if (gfc_resolve_expr (po) == FAILURE)
4849 /* Update the actual arglist of a procedure pointer component to include the
4853 update_ppc_arglist (gfc_expr* e)
4857 gfc_typebound_proc* tb;
4859 if (!gfc_is_proc_ptr_comp (e, &ppc))
4866 else if (tb->nopass)
4869 po = extract_ppc_passed_object (e);
4875 gfc_error ("Passed-object at %L must be scalar", &e->where);
4879 gcc_assert (tb->pass_arg_num > 0);
4880 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4888 /* Check that the object a TBP is called on is valid, i.e. it must not be
4889 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
4892 check_typebound_baseobject (gfc_expr* e)
4896 base = extract_compcall_passed_object (e);
4900 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
4902 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
4904 gfc_error ("Base object for type-bound procedure call at %L is of"
4905 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
4909 /* If the procedure called is NOPASS, the base object must be scalar. */
4910 if (e->value.compcall.tbp->nopass && base->rank > 0)
4912 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
4913 " be scalar", &e->where);
4917 /* FIXME: Remove once PR 41177 (this problem) is fixed completely. */
4920 gfc_error ("Non-scalar base object at %L currently not implemented",
4929 /* Resolve a call to a type-bound procedure, either function or subroutine,
4930 statically from the data in an EXPR_COMPCALL expression. The adapted
4931 arglist and the target-procedure symtree are returned. */
4934 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
4935 gfc_actual_arglist** actual)
4937 gcc_assert (e->expr_type == EXPR_COMPCALL);
4938 gcc_assert (!e->value.compcall.tbp->is_generic);
4940 /* Update the actual arglist for PASS. */
4941 if (update_compcall_arglist (e) == FAILURE)
4944 *actual = e->value.compcall.actual;
4945 *target = e->value.compcall.tbp->u.specific;
4947 gfc_free_ref_list (e->ref);
4949 e->value.compcall.actual = NULL;
4955 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
4956 which of the specific bindings (if any) matches the arglist and transform
4957 the expression into a call of that binding. */
4960 resolve_typebound_generic_call (gfc_expr* e)
4962 gfc_typebound_proc* genproc;
4963 const char* genname;
4965 gcc_assert (e->expr_type == EXPR_COMPCALL);
4966 genname = e->value.compcall.name;
4967 genproc = e->value.compcall.tbp;
4969 if (!genproc->is_generic)
4972 /* Try the bindings on this type and in the inheritance hierarchy. */
4973 for (; genproc; genproc = genproc->overridden)
4977 gcc_assert (genproc->is_generic);
4978 for (g = genproc->u.generic; g; g = g->next)
4981 gfc_actual_arglist* args;
4984 gcc_assert (g->specific);
4986 if (g->specific->error)
4989 target = g->specific->u.specific->n.sym;
4991 /* Get the right arglist by handling PASS/NOPASS. */
4992 args = gfc_copy_actual_arglist (e->value.compcall.actual);
4993 if (!g->specific->nopass)
4996 po = extract_compcall_passed_object (e);
5000 gcc_assert (g->specific->pass_arg_num > 0);
5001 gcc_assert (!g->specific->error);
5002 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5003 g->specific->pass_arg);
5005 resolve_actual_arglist (args, target->attr.proc,
5006 is_external_proc (target) && !target->formal);
5008 /* Check if this arglist matches the formal. */
5009 matches = gfc_arglist_matches_symbol (&args, target);
5011 /* Clean up and break out of the loop if we've found it. */
5012 gfc_free_actual_arglist (args);
5015 e->value.compcall.tbp = g->specific;
5021 /* Nothing matching found! */
5022 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5023 " '%s' at %L", genname, &e->where);
5031 /* Resolve a call to a type-bound subroutine. */
5034 resolve_typebound_call (gfc_code* c)
5036 gfc_actual_arglist* newactual;
5037 gfc_symtree* target;
5039 /* Check that's really a SUBROUTINE. */
5040 if (!c->expr1->value.compcall.tbp->subroutine)
5042 gfc_error ("'%s' at %L should be a SUBROUTINE",
5043 c->expr1->value.compcall.name, &c->loc);
5047 if (check_typebound_baseobject (c->expr1) == FAILURE)
5050 if (resolve_typebound_generic_call (c->expr1) == FAILURE)
5053 /* Transform into an ordinary EXEC_CALL for now. */
5055 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5058 c->ext.actual = newactual;
5059 c->symtree = target;
5060 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5062 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5064 gfc_free_expr (c->expr1);
5065 c->expr1 = gfc_get_expr ();
5066 c->expr1->expr_type = EXPR_FUNCTION;
5067 c->expr1->symtree = target;
5068 c->expr1->where = c->loc;
5070 return resolve_call (c);
5074 /* Resolve a component-call expression. This originally was intended
5075 only to see functions. However, it is convenient to use it in
5076 resolving subroutine class methods, since we do not have to add a
5077 gfc_code each time. */
5079 resolve_compcall (gfc_expr* e, bool fcn)
5081 gfc_actual_arglist* newactual;
5082 gfc_symtree* target;
5084 /* Check that's really a FUNCTION. */
5085 if (fcn && !e->value.compcall.tbp->function)
5087 gfc_error ("'%s' at %L should be a FUNCTION",
5088 e->value.compcall.name, &e->where);
5091 else if (!fcn && !e->value.compcall.tbp->subroutine)
5093 /* To resolve class member calls, we borrow this bit
5094 of code to select the specific procedures. */
5095 gfc_error ("'%s' at %L should be a SUBROUTINE",
5096 e->value.compcall.name, &e->where);
5100 /* These must not be assign-calls! */
5101 gcc_assert (!e->value.compcall.assign);
5103 if (check_typebound_baseobject (e) == FAILURE)
5106 if (resolve_typebound_generic_call (e) == FAILURE)
5108 gcc_assert (!e->value.compcall.tbp->is_generic);
5110 /* Take the rank from the function's symbol. */
5111 if (e->value.compcall.tbp->u.specific->n.sym->as)
5112 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5114 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5115 arglist to the TBP's binding target. */
5117 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5120 e->value.function.actual = newactual;
5121 e->value.function.name = e->value.compcall.name;
5122 e->value.function.esym = target->n.sym;
5123 e->value.function.class_esym = NULL;
5124 e->value.function.isym = NULL;
5125 e->symtree = target;
5126 e->ts = target->n.sym->ts;
5127 e->expr_type = EXPR_FUNCTION;
5129 /* Resolution is not necessary if this is a class subroutine; this
5130 function only has to identify the specific proc. Resolution of
5131 the call will be done next in resolve_typebound_call. */
5132 return fcn ? gfc_resolve_expr (e) : SUCCESS;
5136 /* Resolve a typebound call for the members in a class. This group of
5137 functions implements dynamic dispatch in the provisional version
5138 of f03 OOP. As soon as vtables are in place and contain pointers
5139 to methods, this will no longer be necessary. */
5140 static gfc_expr *list_e;
5141 static void check_class_members (gfc_symbol *);
5142 static gfc_try class_try;
5143 static bool fcn_flag;
5144 static gfc_symbol *class_object;
5148 check_members (gfc_symbol *derived)
5150 if (derived->attr.flavor == FL_DERIVED)
5151 check_class_members (derived);
5156 check_class_members (gfc_symbol *derived)
5160 gfc_class_esym_list *etmp;
5162 e = gfc_copy_expr (list_e);
5164 tbp = gfc_find_typebound_proc (derived, &class_try,
5165 e->value.compcall.name,
5170 gfc_error ("no typebound available procedure named '%s' at %L",
5171 e->value.compcall.name, &e->where);
5175 if (tbp->n.tb->is_generic)
5177 /* If we have to match a passed class member, force the actual
5178 expression to have the correct type. */
5179 if (!tbp->n.tb->nopass)
5181 if (e->value.compcall.base_object == NULL)
5182 e->value.compcall.base_object =
5183 extract_compcall_passed_object (e);
5185 e->value.compcall.base_object->ts.type = BT_DERIVED;
5186 e->value.compcall.base_object->ts.u.derived = derived;
5190 e->value.compcall.tbp = tbp->n.tb;
5191 e->value.compcall.name = tbp->name;
5193 /* Let the original expresssion catch the assertion in
5194 resolve_compcall, since this flag does not appear to be reset or
5195 copied in some systems. */
5196 e->value.compcall.assign = 0;
5198 /* Do the renaming, PASSing, generic => specific and other
5199 good things for each class member. */
5200 class_try = (resolve_compcall (e, fcn_flag) == SUCCESS)
5201 ? class_try : FAILURE;
5203 /* Now transfer the found symbol to the esym list. */
5204 if (class_try == SUCCESS)
5206 etmp = list_e->value.function.class_esym;
5207 list_e->value.function.class_esym
5208 = gfc_get_class_esym_list();
5209 list_e->value.function.class_esym->next = etmp;
5210 list_e->value.function.class_esym->derived = derived;
5211 list_e->value.function.class_esym->esym
5212 = e->value.function.esym;
5217 /* Burrow down into grandchildren types. */
5218 if (derived->f2k_derived)
5219 gfc_traverse_ns (derived->f2k_derived, check_members);
5223 /* Eliminate esym_lists where all the members point to the
5224 typebound procedure of the declared type; ie. one where
5225 type selection has no effect.. */
5227 resolve_class_esym (gfc_expr *e)
5229 gfc_class_esym_list *p, *q;
5232 gcc_assert (e && e->expr_type == EXPR_FUNCTION);
5234 p = e->value.function.class_esym;
5238 for (; p; p = p->next)
5239 empty = empty && (e->value.function.esym == p->esym);
5243 p = e->value.function.class_esym;
5249 e->value.function.class_esym = NULL;
5254 /* Generate an expression for the hash value, given the reference to
5255 the class of the final expression (class_ref), the base of the
5256 full reference list (new_ref), the declared type and the class
5259 hash_value_expr (gfc_ref *class_ref, gfc_ref *new_ref, gfc_symtree *st)
5261 gfc_expr *hash_value;
5263 /* Build an expression for the correct hash_value; ie. that of the last
5267 class_ref->next = NULL;
5271 gfc_free_ref_list (new_ref);
5274 hash_value = gfc_get_expr ();
5275 hash_value->expr_type = EXPR_VARIABLE;
5276 hash_value->symtree = st;
5277 hash_value->symtree->n.sym->refs++;
5278 hash_value->ref = new_ref;
5279 gfc_add_component_ref (hash_value, "$vptr");
5280 gfc_add_component_ref (hash_value, "$hash");
5286 /* Get the ultimate declared type from an expression. In addition,
5287 return the last class/derived type reference and the copy of the
5290 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5293 gfc_symbol *declared;
5298 *new_ref = gfc_copy_ref (e->ref);
5299 for (ref = *new_ref; ref; ref = ref->next)
5301 if (ref->type != REF_COMPONENT)
5304 if (ref->u.c.component->ts.type == BT_CLASS
5305 || ref->u.c.component->ts.type == BT_DERIVED)
5307 declared = ref->u.c.component->ts.u.derived;
5312 if (declared == NULL)
5313 declared = e->symtree->n.sym->ts.u.derived;
5319 /* Resolve the argument expressions so that any arguments expressions
5320 that include class methods are resolved before the current call.
5321 This is necessary because of the static variables used in CLASS
5322 method resolution. */
5324 resolve_arg_exprs (gfc_actual_arglist *arg)
5326 /* Resolve the actual arglist expressions. */
5327 for (; arg; arg = arg->next)
5330 gfc_resolve_expr (arg->expr);
5335 /* Resolve a CLASS typebound function, or 'method'. */
5337 resolve_class_compcall (gfc_expr* e)
5339 gfc_symbol *derived, *declared;
5345 class_object = st->n.sym;
5347 /* Get the CLASS declared type. */
5348 declared = get_declared_from_expr (&class_ref, &new_ref, e);
5350 /* Weed out cases of the ultimate component being a derived type. */
5351 if (class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5353 gfc_free_ref_list (new_ref);
5354 return resolve_compcall (e, true);
5357 /* Resolve the argument expressions, */
5358 resolve_arg_exprs (e->value.function.actual);
5360 /* Get the data component, which is of the declared type. */
5361 derived = declared->components->ts.u.derived;
5363 /* Resolve the function call for each member of the class. */
5364 class_try = SUCCESS;
5366 list_e = gfc_copy_expr (e);
5367 check_class_members (derived);
5369 class_try = (resolve_compcall (e, true) == SUCCESS)
5370 ? class_try : FAILURE;
5372 /* Transfer the class list to the original expression. Note that
5373 the class_esym list is cleaned up in trans-expr.c, as the calls
5375 e->value.function.class_esym = list_e->value.function.class_esym;
5376 list_e->value.function.class_esym = NULL;
5377 gfc_free_expr (list_e);
5379 resolve_class_esym (e);
5381 /* More than one typebound procedure so transmit an expression for
5382 the hash_value as the selector. */
5383 if (e->value.function.class_esym != NULL)
5384 e->value.function.class_esym->hash_value
5385 = hash_value_expr (class_ref, new_ref, st);
5390 /* Resolve a CLASS typebound subroutine, or 'method'. */
5392 resolve_class_typebound_call (gfc_code *code)
5394 gfc_symbol *derived, *declared;
5399 st = code->expr1->symtree;
5400 class_object = st->n.sym;
5402 /* Get the CLASS declared type. */
5403 declared = get_declared_from_expr (&class_ref, &new_ref, code->expr1);
5405 /* Weed out cases of the ultimate component being a derived type. */
5406 if (class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5408 gfc_free_ref_list (new_ref);
5409 return resolve_typebound_call (code);
5412 /* Resolve the argument expressions, */
5413 resolve_arg_exprs (code->expr1->value.compcall.actual);
5415 /* Get the data component, which is of the declared type. */
5416 derived = declared->components->ts.u.derived;
5418 class_try = SUCCESS;
5420 list_e = gfc_copy_expr (code->expr1);
5421 check_class_members (derived);
5423 class_try = (resolve_typebound_call (code) == SUCCESS)
5424 ? class_try : FAILURE;
5426 /* Transfer the class list to the original expression. Note that
5427 the class_esym list is cleaned up in trans-expr.c, as the calls
5429 code->expr1->value.function.class_esym
5430 = list_e->value.function.class_esym;
5431 list_e->value.function.class_esym = NULL;
5432 gfc_free_expr (list_e);
5434 resolve_class_esym (code->expr1);
5436 /* More than one typebound procedure so transmit an expression for
5437 the hash_value as the selector. */
5438 if (code->expr1->value.function.class_esym != NULL)
5439 code->expr1->value.function.class_esym->hash_value
5440 = hash_value_expr (class_ref, new_ref, st);
5446 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5449 resolve_ppc_call (gfc_code* c)
5451 gfc_component *comp;
5454 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
5457 c->resolved_sym = c->expr1->symtree->n.sym;
5458 c->expr1->expr_type = EXPR_VARIABLE;
5460 if (!comp->attr.subroutine)
5461 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5463 if (resolve_ref (c->expr1) == FAILURE)
5466 if (update_ppc_arglist (c->expr1) == FAILURE)
5469 c->ext.actual = c->expr1->value.compcall.actual;
5471 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5472 comp->formal == NULL) == FAILURE)
5475 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5481 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5484 resolve_expr_ppc (gfc_expr* e)
5486 gfc_component *comp;
5489 b = gfc_is_proc_ptr_comp (e, &comp);
5492 /* Convert to EXPR_FUNCTION. */
5493 e->expr_type = EXPR_FUNCTION;
5494 e->value.function.isym = NULL;
5495 e->value.function.actual = e->value.compcall.actual;
5497 if (comp->as != NULL)
5498 e->rank = comp->as->rank;
5500 if (!comp->attr.function)
5501 gfc_add_function (&comp->attr, comp->name, &e->where);
5503 if (resolve_ref (e) == FAILURE)
5506 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5507 comp->formal == NULL) == FAILURE)
5510 if (update_ppc_arglist (e) == FAILURE)
5513 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
5520 gfc_is_expandable_expr (gfc_expr *e)
5522 gfc_constructor *con;
5524 if (e->expr_type == EXPR_ARRAY)
5526 /* Traverse the constructor looking for variables that are flavor
5527 parameter. Parameters must be expanded since they are fully used at
5529 for (con = e->value.constructor; con; con = con->next)
5531 if (con->expr->expr_type == EXPR_VARIABLE
5532 && con->expr->symtree
5533 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
5534 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
5536 if (con->expr->expr_type == EXPR_ARRAY
5537 && gfc_is_expandable_expr (con->expr))
5545 /* Resolve an expression. That is, make sure that types of operands agree
5546 with their operators, intrinsic operators are converted to function calls
5547 for overloaded types and unresolved function references are resolved. */
5550 gfc_resolve_expr (gfc_expr *e)
5557 switch (e->expr_type)
5560 t = resolve_operator (e);
5566 if (check_host_association (e))
5567 t = resolve_function (e);
5570 t = resolve_variable (e);
5572 expression_rank (e);
5575 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
5576 && e->ref->type != REF_SUBSTRING)
5577 gfc_resolve_substring_charlen (e);
5582 if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
5583 t = resolve_class_compcall (e);
5585 t = resolve_compcall (e, true);
5588 case EXPR_SUBSTRING:
5589 t = resolve_ref (e);
5598 t = resolve_expr_ppc (e);
5603 if (resolve_ref (e) == FAILURE)
5606 t = gfc_resolve_array_constructor (e);
5607 /* Also try to expand a constructor. */
5610 expression_rank (e);
5611 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
5612 gfc_expand_constructor (e);
5615 /* This provides the opportunity for the length of constructors with
5616 character valued function elements to propagate the string length
5617 to the expression. */
5618 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
5620 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
5621 here rather then add a duplicate test for it above. */
5622 gfc_expand_constructor (e);
5623 t = gfc_resolve_character_array_constructor (e);
5628 case EXPR_STRUCTURE:
5629 t = resolve_ref (e);
5633 t = resolve_structure_cons (e);
5637 t = gfc_simplify_expr (e, 0);
5641 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
5644 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
5651 /* Resolve an expression from an iterator. They must be scalar and have
5652 INTEGER or (optionally) REAL type. */
5655 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
5656 const char *name_msgid)
5658 if (gfc_resolve_expr (expr) == FAILURE)
5661 if (expr->rank != 0)
5663 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
5667 if (expr->ts.type != BT_INTEGER)
5669 if (expr->ts.type == BT_REAL)
5672 return gfc_notify_std (GFC_STD_F95_DEL,
5673 "Deleted feature: %s at %L must be integer",
5674 _(name_msgid), &expr->where);
5677 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
5684 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
5692 /* Resolve the expressions in an iterator structure. If REAL_OK is
5693 false allow only INTEGER type iterators, otherwise allow REAL types. */
5696 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
5698 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
5702 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
5704 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5709 if (gfc_resolve_iterator_expr (iter->start, real_ok,
5710 "Start expression in DO loop") == FAILURE)
5713 if (gfc_resolve_iterator_expr (iter->end, real_ok,
5714 "End expression in DO loop") == FAILURE)
5717 if (gfc_resolve_iterator_expr (iter->step, real_ok,
5718 "Step expression in DO loop") == FAILURE)
5721 if (iter->step->expr_type == EXPR_CONSTANT)
5723 if ((iter->step->ts.type == BT_INTEGER
5724 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
5725 || (iter->step->ts.type == BT_REAL
5726 && mpfr_sgn (iter->step->value.real) == 0))
5728 gfc_error ("Step expression in DO loop at %L cannot be zero",
5729 &iter->step->where);
5734 /* Convert start, end, and step to the same type as var. */
5735 if (iter->start->ts.kind != iter->var->ts.kind
5736 || iter->start->ts.type != iter->var->ts.type)
5737 gfc_convert_type (iter->start, &iter->var->ts, 2);
5739 if (iter->end->ts.kind != iter->var->ts.kind
5740 || iter->end->ts.type != iter->var->ts.type)
5741 gfc_convert_type (iter->end, &iter->var->ts, 2);
5743 if (iter->step->ts.kind != iter->var->ts.kind
5744 || iter->step->ts.type != iter->var->ts.type)
5745 gfc_convert_type (iter->step, &iter->var->ts, 2);
5747 if (iter->start->expr_type == EXPR_CONSTANT
5748 && iter->end->expr_type == EXPR_CONSTANT
5749 && iter->step->expr_type == EXPR_CONSTANT)
5752 if (iter->start->ts.type == BT_INTEGER)
5754 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
5755 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
5759 sgn = mpfr_sgn (iter->step->value.real);
5760 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
5762 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
5763 gfc_warning ("DO loop at %L will be executed zero times",
5764 &iter->step->where);
5771 /* Traversal function for find_forall_index. f == 2 signals that
5772 that variable itself is not to be checked - only the references. */
5775 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
5777 if (expr->expr_type != EXPR_VARIABLE)
5780 /* A scalar assignment */
5781 if (!expr->ref || *f == 1)
5783 if (expr->symtree->n.sym == sym)
5795 /* Check whether the FORALL index appears in the expression or not.
5796 Returns SUCCESS if SYM is found in EXPR. */
5799 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
5801 if (gfc_traverse_expr (expr, sym, forall_index, f))
5808 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5809 to be a scalar INTEGER variable. The subscripts and stride are scalar
5810 INTEGERs, and if stride is a constant it must be nonzero.
5811 Furthermore "A subscript or stride in a forall-triplet-spec shall
5812 not contain a reference to any index-name in the
5813 forall-triplet-spec-list in which it appears." (7.5.4.1) */
5816 resolve_forall_iterators (gfc_forall_iterator *it)
5818 gfc_forall_iterator *iter, *iter2;
5820 for (iter = it; iter; iter = iter->next)
5822 if (gfc_resolve_expr (iter->var) == SUCCESS
5823 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
5824 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
5827 if (gfc_resolve_expr (iter->start) == SUCCESS
5828 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
5829 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
5830 &iter->start->where);
5831 if (iter->var->ts.kind != iter->start->ts.kind)
5832 gfc_convert_type (iter->start, &iter->var->ts, 2);
5834 if (gfc_resolve_expr (iter->end) == SUCCESS
5835 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
5836 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
5838 if (iter->var->ts.kind != iter->end->ts.kind)
5839 gfc_convert_type (iter->end, &iter->var->ts, 2);
5841 if (gfc_resolve_expr (iter->stride) == SUCCESS)
5843 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
5844 gfc_error ("FORALL stride expression at %L must be a scalar %s",
5845 &iter->stride->where, "INTEGER");
5847 if (iter->stride->expr_type == EXPR_CONSTANT
5848 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
5849 gfc_error ("FORALL stride expression at %L cannot be zero",
5850 &iter->stride->where);
5852 if (iter->var->ts.kind != iter->stride->ts.kind)
5853 gfc_convert_type (iter->stride, &iter->var->ts, 2);
5856 for (iter = it; iter; iter = iter->next)
5857 for (iter2 = iter; iter2; iter2 = iter2->next)
5859 if (find_forall_index (iter2->start,
5860 iter->var->symtree->n.sym, 0) == SUCCESS
5861 || find_forall_index (iter2->end,
5862 iter->var->symtree->n.sym, 0) == SUCCESS
5863 || find_forall_index (iter2->stride,
5864 iter->var->symtree->n.sym, 0) == SUCCESS)
5865 gfc_error ("FORALL index '%s' may not appear in triplet "
5866 "specification at %L", iter->var->symtree->name,
5867 &iter2->start->where);
5872 /* Given a pointer to a symbol that is a derived type, see if it's
5873 inaccessible, i.e. if it's defined in another module and the components are
5874 PRIVATE. The search is recursive if necessary. Returns zero if no
5875 inaccessible components are found, nonzero otherwise. */
5878 derived_inaccessible (gfc_symbol *sym)
5882 if (sym->attr.use_assoc && sym->attr.private_comp)
5885 for (c = sym->components; c; c = c->next)
5887 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
5895 /* Resolve the argument of a deallocate expression. The expression must be
5896 a pointer or a full array. */
5899 resolve_deallocate_expr (gfc_expr *e)
5901 symbol_attribute attr;
5902 int allocatable, pointer, check_intent_in;
5907 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5908 check_intent_in = 1;
5910 if (gfc_resolve_expr (e) == FAILURE)
5913 if (e->expr_type != EXPR_VARIABLE)
5916 sym = e->symtree->n.sym;
5918 if (sym->ts.type == BT_CLASS)
5920 allocatable = sym->ts.u.derived->components->attr.allocatable;
5921 pointer = sym->ts.u.derived->components->attr.pointer;
5925 allocatable = sym->attr.allocatable;
5926 pointer = sym->attr.pointer;
5928 for (ref = e->ref; ref; ref = ref->next)
5931 check_intent_in = 0;
5936 if (ref->u.ar.type != AR_FULL)
5941 c = ref->u.c.component;
5942 if (c->ts.type == BT_CLASS)
5944 allocatable = c->ts.u.derived->components->attr.allocatable;
5945 pointer = c->ts.u.derived->components->attr.pointer;
5949 allocatable = c->attr.allocatable;
5950 pointer = c->attr.pointer;
5960 attr = gfc_expr_attr (e);
5962 if (allocatable == 0 && attr.pointer == 0)
5965 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5969 if (check_intent_in && sym->attr.intent == INTENT_IN)
5971 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
5972 sym->name, &e->where);
5976 if (e->ts.type == BT_CLASS)
5978 /* Only deallocate the DATA component. */
5979 gfc_add_component_ref (e, "$data");
5986 /* Returns true if the expression e contains a reference to the symbol sym. */
5988 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5990 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
5997 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
5999 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6003 /* Given the expression node e for an allocatable/pointer of derived type to be
6004 allocated, get the expression node to be initialized afterwards (needed for
6005 derived types with default initializers, and derived types with allocatable
6006 components that need nullification.) */
6009 gfc_expr_to_initialize (gfc_expr *e)
6015 result = gfc_copy_expr (e);
6017 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6018 for (ref = result->ref; ref; ref = ref->next)
6019 if (ref->type == REF_ARRAY && ref->next == NULL)
6021 ref->u.ar.type = AR_FULL;
6023 for (i = 0; i < ref->u.ar.dimen; i++)
6024 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6026 result->rank = ref->u.ar.dimen;
6034 /* Used in resolve_allocate_expr to check that a allocation-object and
6035 a source-expr are conformable. This does not catch all possible
6036 cases; in particular a runtime checking is needed. */
6039 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6041 /* First compare rank. */
6042 if (e2->ref && e1->rank != e2->ref->u.ar.as->rank)
6044 gfc_error ("Source-expr at %L must be scalar or have the "
6045 "same rank as the allocate-object at %L",
6046 &e1->where, &e2->where);
6057 for (i = 0; i < e1->rank; i++)
6059 if (e2->ref->u.ar.end[i])
6061 mpz_set (s, e2->ref->u.ar.end[i]->value.integer);
6062 mpz_sub (s, s, e2->ref->u.ar.start[i]->value.integer);
6063 mpz_add_ui (s, s, 1);
6067 mpz_set (s, e2->ref->u.ar.start[i]->value.integer);
6070 if (mpz_cmp (e1->shape[i], s) != 0)
6072 gfc_error ("Source-expr at %L and allocate-object at %L must "
6073 "have the same shape", &e1->where, &e2->where);
6086 /* Resolve the expression in an ALLOCATE statement, doing the additional
6087 checks to see whether the expression is OK or not. The expression must
6088 have a trailing array reference that gives the size of the array. */
6091 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6093 int i, pointer, allocatable, dimension, check_intent_in, is_abstract;
6094 symbol_attribute attr;
6095 gfc_ref *ref, *ref2;
6101 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
6102 check_intent_in = 1;
6104 if (gfc_resolve_expr (e) == FAILURE)
6107 /* Make sure the expression is allocatable or a pointer. If it is
6108 pointer, the next-to-last reference must be a pointer. */
6112 sym = e->symtree->n.sym;
6114 /* Check whether ultimate component is abstract and CLASS. */
6117 if (e->expr_type != EXPR_VARIABLE)
6120 attr = gfc_expr_attr (e);
6121 pointer = attr.pointer;
6122 dimension = attr.dimension;
6126 if (sym->ts.type == BT_CLASS)
6128 allocatable = sym->ts.u.derived->components->attr.allocatable;
6129 pointer = sym->ts.u.derived->components->attr.pointer;
6130 dimension = sym->ts.u.derived->components->attr.dimension;
6131 is_abstract = sym->ts.u.derived->components->attr.abstract;
6135 allocatable = sym->attr.allocatable;
6136 pointer = sym->attr.pointer;
6137 dimension = sym->attr.dimension;
6140 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6143 check_intent_in = 0;
6148 if (ref->next != NULL)
6153 c = ref->u.c.component;
6154 if (c->ts.type == BT_CLASS)
6156 allocatable = c->ts.u.derived->components->attr.allocatable;
6157 pointer = c->ts.u.derived->components->attr.pointer;
6158 dimension = c->ts.u.derived->components->attr.dimension;
6159 is_abstract = c->ts.u.derived->components->attr.abstract;
6163 allocatable = c->attr.allocatable;
6164 pointer = c->attr.pointer;
6165 dimension = c->attr.dimension;
6166 is_abstract = c->attr.abstract;
6178 if (allocatable == 0 && pointer == 0)
6180 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6185 /* Some checks for the SOURCE tag. */
6188 /* Check F03:C631. */
6189 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6191 gfc_error ("Type of entity at %L is type incompatible with "
6192 "source-expr at %L", &e->where, &code->expr3->where);
6196 /* Check F03:C632 and restriction following Note 6.18. */
6197 if (code->expr3->rank > 0
6198 && conformable_arrays (code->expr3, e) == FAILURE)
6201 /* Check F03:C633. */
6202 if (code->expr3->ts.kind != e->ts.kind)
6204 gfc_error ("The allocate-object at %L and the source-expr at %L "
6205 "shall have the same kind type parameter",
6206 &e->where, &code->expr3->where);
6210 else if (is_abstract&& code->ext.alloc.ts.type == BT_UNKNOWN)
6212 gcc_assert (e->ts.type == BT_CLASS);
6213 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6214 "type-spec or SOURCE=", sym->name, &e->where);
6218 if (check_intent_in && sym->attr.intent == INTENT_IN)
6220 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
6221 sym->name, &e->where);
6225 if (pointer || dimension == 0)
6228 /* Make sure the next-to-last reference node is an array specification. */
6230 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
6232 gfc_error ("Array specification required in ALLOCATE statement "
6233 "at %L", &e->where);
6237 /* Make sure that the array section reference makes sense in the
6238 context of an ALLOCATE specification. */
6242 for (i = 0; i < ar->dimen; i++)
6244 if (ref2->u.ar.type == AR_ELEMENT)
6247 switch (ar->dimen_type[i])
6253 if (ar->start[i] != NULL
6254 && ar->end[i] != NULL
6255 && ar->stride[i] == NULL)
6258 /* Fall Through... */
6262 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6269 for (a = code->ext.alloc.list; a; a = a->next)
6271 sym = a->expr->symtree->n.sym;
6273 /* TODO - check derived type components. */
6274 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6277 if ((ar->start[i] != NULL
6278 && gfc_find_sym_in_expr (sym, ar->start[i]))
6279 || (ar->end[i] != NULL
6280 && gfc_find_sym_in_expr (sym, ar->end[i])))
6282 gfc_error ("'%s' must not appear in the array specification at "
6283 "%L in the same ALLOCATE statement where it is "
6284 "itself allocated", sym->name, &ar->where);
6294 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6296 gfc_expr *stat, *errmsg, *pe, *qe;
6297 gfc_alloc *a, *p, *q;
6299 stat = code->expr1 ? code->expr1 : NULL;
6301 errmsg = code->expr2 ? code->expr2 : NULL;
6303 /* Check the stat variable. */
6306 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
6307 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
6308 stat->symtree->n.sym->name, &stat->where);
6310 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
6311 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
6314 if ((stat->ts.type != BT_INTEGER
6315 && !(stat->ref && (stat->ref->type == REF_ARRAY
6316 || stat->ref->type == REF_COMPONENT)))
6318 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6319 "variable", &stat->where);
6321 for (p = code->ext.alloc.list; p; p = p->next)
6322 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
6323 gfc_error ("Stat-variable at %L shall not be %sd within "
6324 "the same %s statement", &stat->where, fcn, fcn);
6327 /* Check the errmsg variable. */
6331 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6334 if (errmsg->symtree->n.sym->attr.intent == INTENT_IN)
6335 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
6336 errmsg->symtree->n.sym->name, &errmsg->where);
6338 if (gfc_pure (NULL) && gfc_impure_variable (errmsg->symtree->n.sym))
6339 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
6342 if ((errmsg->ts.type != BT_CHARACTER
6344 && (errmsg->ref->type == REF_ARRAY
6345 || errmsg->ref->type == REF_COMPONENT)))
6346 || errmsg->rank > 0 )
6347 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
6348 "variable", &errmsg->where);
6350 for (p = code->ext.alloc.list; p; p = p->next)
6351 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
6352 gfc_error ("Errmsg-variable at %L shall not be %sd within "
6353 "the same %s statement", &errmsg->where, fcn, fcn);
6356 /* Check that an allocate-object appears only once in the statement.
6357 FIXME: Checking derived types is disabled. */
6358 for (p = code->ext.alloc.list; p; p = p->next)
6361 if ((pe->ref && pe->ref->type != REF_COMPONENT)
6362 && (pe->symtree->n.sym->ts.type != BT_DERIVED))
6364 for (q = p->next; q; q = q->next)
6367 if ((qe->ref && qe->ref->type != REF_COMPONENT)
6368 && (qe->symtree->n.sym->ts.type != BT_DERIVED)
6369 && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
6370 gfc_error ("Allocate-object at %L also appears at %L",
6371 &pe->where, &qe->where);
6376 if (strcmp (fcn, "ALLOCATE") == 0)
6378 for (a = code->ext.alloc.list; a; a = a->next)
6379 resolve_allocate_expr (a->expr, code);
6383 for (a = code->ext.alloc.list; a; a = a->next)
6384 resolve_deallocate_expr (a->expr);
6389 /************ SELECT CASE resolution subroutines ************/
6391 /* Callback function for our mergesort variant. Determines interval
6392 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
6393 op1 > op2. Assumes we're not dealing with the default case.
6394 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
6395 There are nine situations to check. */
6398 compare_cases (const gfc_case *op1, const gfc_case *op2)
6402 if (op1->low == NULL) /* op1 = (:L) */
6404 /* op2 = (:N), so overlap. */
6406 /* op2 = (M:) or (M:N), L < M */
6407 if (op2->low != NULL
6408 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6411 else if (op1->high == NULL) /* op1 = (K:) */
6413 /* op2 = (M:), so overlap. */
6415 /* op2 = (:N) or (M:N), K > N */
6416 if (op2->high != NULL
6417 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6420 else /* op1 = (K:L) */
6422 if (op2->low == NULL) /* op2 = (:N), K > N */
6423 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6425 else if (op2->high == NULL) /* op2 = (M:), L < M */
6426 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6428 else /* op2 = (M:N) */
6432 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6435 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6444 /* Merge-sort a double linked case list, detecting overlap in the
6445 process. LIST is the head of the double linked case list before it
6446 is sorted. Returns the head of the sorted list if we don't see any
6447 overlap, or NULL otherwise. */
6450 check_case_overlap (gfc_case *list)
6452 gfc_case *p, *q, *e, *tail;
6453 int insize, nmerges, psize, qsize, cmp, overlap_seen;
6455 /* If the passed list was empty, return immediately. */
6462 /* Loop unconditionally. The only exit from this loop is a return
6463 statement, when we've finished sorting the case list. */
6470 /* Count the number of merges we do in this pass. */
6473 /* Loop while there exists a merge to be done. */
6478 /* Count this merge. */
6481 /* Cut the list in two pieces by stepping INSIZE places
6482 forward in the list, starting from P. */
6485 for (i = 0; i < insize; i++)
6494 /* Now we have two lists. Merge them! */
6495 while (psize > 0 || (qsize > 0 && q != NULL))
6497 /* See from which the next case to merge comes from. */
6500 /* P is empty so the next case must come from Q. */
6505 else if (qsize == 0 || q == NULL)
6514 cmp = compare_cases (p, q);
6517 /* The whole case range for P is less than the
6525 /* The whole case range for Q is greater than
6526 the case range for P. */
6533 /* The cases overlap, or they are the same
6534 element in the list. Either way, we must
6535 issue an error and get the next case from P. */
6536 /* FIXME: Sort P and Q by line number. */
6537 gfc_error ("CASE label at %L overlaps with CASE "
6538 "label at %L", &p->where, &q->where);
6546 /* Add the next element to the merged list. */
6555 /* P has now stepped INSIZE places along, and so has Q. So
6556 they're the same. */
6561 /* If we have done only one merge or none at all, we've
6562 finished sorting the cases. */
6571 /* Otherwise repeat, merging lists twice the size. */
6577 /* Check to see if an expression is suitable for use in a CASE statement.
6578 Makes sure that all case expressions are scalar constants of the same
6579 type. Return FAILURE if anything is wrong. */
6582 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
6584 if (e == NULL) return SUCCESS;
6586 if (e->ts.type != case_expr->ts.type)
6588 gfc_error ("Expression in CASE statement at %L must be of type %s",
6589 &e->where, gfc_basic_typename (case_expr->ts.type));
6593 /* C805 (R808) For a given case-construct, each case-value shall be of
6594 the same type as case-expr. For character type, length differences
6595 are allowed, but the kind type parameters shall be the same. */
6597 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
6599 gfc_error ("Expression in CASE statement at %L must be of kind %d",
6600 &e->where, case_expr->ts.kind);
6604 /* Convert the case value kind to that of case expression kind, if needed.
6605 FIXME: Should a warning be issued? */
6606 if (e->ts.kind != case_expr->ts.kind)
6607 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
6611 gfc_error ("Expression in CASE statement at %L must be scalar",
6620 /* Given a completely parsed select statement, we:
6622 - Validate all expressions and code within the SELECT.
6623 - Make sure that the selection expression is not of the wrong type.
6624 - Make sure that no case ranges overlap.
6625 - Eliminate unreachable cases and unreachable code resulting from
6626 removing case labels.
6628 The standard does allow unreachable cases, e.g. CASE (5:3). But
6629 they are a hassle for code generation, and to prevent that, we just
6630 cut them out here. This is not necessary for overlapping cases
6631 because they are illegal and we never even try to generate code.
6633 We have the additional caveat that a SELECT construct could have
6634 been a computed GOTO in the source code. Fortunately we can fairly
6635 easily work around that here: The case_expr for a "real" SELECT CASE
6636 is in code->expr1, but for a computed GOTO it is in code->expr2. All
6637 we have to do is make sure that the case_expr is a scalar integer
6641 resolve_select (gfc_code *code)
6644 gfc_expr *case_expr;
6645 gfc_case *cp, *default_case, *tail, *head;
6646 int seen_unreachable;
6652 if (code->expr1 == NULL)
6654 /* This was actually a computed GOTO statement. */
6655 case_expr = code->expr2;
6656 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
6657 gfc_error ("Selection expression in computed GOTO statement "
6658 "at %L must be a scalar integer expression",
6661 /* Further checking is not necessary because this SELECT was built
6662 by the compiler, so it should always be OK. Just move the
6663 case_expr from expr2 to expr so that we can handle computed
6664 GOTOs as normal SELECTs from here on. */
6665 code->expr1 = code->expr2;
6670 case_expr = code->expr1;
6672 type = case_expr->ts.type;
6673 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
6675 gfc_error ("Argument of SELECT statement at %L cannot be %s",
6676 &case_expr->where, gfc_typename (&case_expr->ts));
6678 /* Punt. Going on here just produce more garbage error messages. */
6682 if (case_expr->rank != 0)
6684 gfc_error ("Argument of SELECT statement at %L must be a scalar "
6685 "expression", &case_expr->where);
6691 /* PR 19168 has a long discussion concerning a mismatch of the kinds
6692 of the SELECT CASE expression and its CASE values. Walk the lists
6693 of case values, and if we find a mismatch, promote case_expr to
6694 the appropriate kind. */
6696 if (type == BT_LOGICAL || type == BT_INTEGER)
6698 for (body = code->block; body; body = body->block)
6700 /* Walk the case label list. */
6701 for (cp = body->ext.case_list; cp; cp = cp->next)
6703 /* Intercept the DEFAULT case. It does not have a kind. */
6704 if (cp->low == NULL && cp->high == NULL)
6707 /* Unreachable case ranges are discarded, so ignore. */
6708 if (cp->low != NULL && cp->high != NULL
6709 && cp->low != cp->high
6710 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6713 /* FIXME: Should a warning be issued? */
6715 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
6716 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
6718 if (cp->high != NULL
6719 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
6720 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
6725 /* Assume there is no DEFAULT case. */
6726 default_case = NULL;
6731 for (body = code->block; body; body = body->block)
6733 /* Assume the CASE list is OK, and all CASE labels can be matched. */
6735 seen_unreachable = 0;
6737 /* Walk the case label list, making sure that all case labels
6739 for (cp = body->ext.case_list; cp; cp = cp->next)
6741 /* Count the number of cases in the whole construct. */
6744 /* Intercept the DEFAULT case. */
6745 if (cp->low == NULL && cp->high == NULL)
6747 if (default_case != NULL)
6749 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6750 "by a second DEFAULT CASE at %L",
6751 &default_case->where, &cp->where);
6762 /* Deal with single value cases and case ranges. Errors are
6763 issued from the validation function. */
6764 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
6765 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
6771 if (type == BT_LOGICAL
6772 && ((cp->low == NULL || cp->high == NULL)
6773 || cp->low != cp->high))
6775 gfc_error ("Logical range in CASE statement at %L is not "
6776 "allowed", &cp->low->where);
6781 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
6784 value = cp->low->value.logical == 0 ? 2 : 1;
6785 if (value & seen_logical)
6787 gfc_error ("constant logical value in CASE statement "
6788 "is repeated at %L",
6793 seen_logical |= value;
6796 if (cp->low != NULL && cp->high != NULL
6797 && cp->low != cp->high
6798 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6800 if (gfc_option.warn_surprising)
6801 gfc_warning ("Range specification at %L can never "
6802 "be matched", &cp->where);
6804 cp->unreachable = 1;
6805 seen_unreachable = 1;
6809 /* If the case range can be matched, it can also overlap with
6810 other cases. To make sure it does not, we put it in a
6811 double linked list here. We sort that with a merge sort
6812 later on to detect any overlapping cases. */
6816 head->right = head->left = NULL;
6821 tail->right->left = tail;
6828 /* It there was a failure in the previous case label, give up
6829 for this case label list. Continue with the next block. */
6833 /* See if any case labels that are unreachable have been seen.
6834 If so, we eliminate them. This is a bit of a kludge because
6835 the case lists for a single case statement (label) is a
6836 single forward linked lists. */
6837 if (seen_unreachable)
6839 /* Advance until the first case in the list is reachable. */
6840 while (body->ext.case_list != NULL
6841 && body->ext.case_list->unreachable)
6843 gfc_case *n = body->ext.case_list;
6844 body->ext.case_list = body->ext.case_list->next;
6846 gfc_free_case_list (n);
6849 /* Strip all other unreachable cases. */
6850 if (body->ext.case_list)
6852 for (cp = body->ext.case_list; cp->next; cp = cp->next)
6854 if (cp->next->unreachable)
6856 gfc_case *n = cp->next;
6857 cp->next = cp->next->next;
6859 gfc_free_case_list (n);
6866 /* See if there were overlapping cases. If the check returns NULL,
6867 there was overlap. In that case we don't do anything. If head
6868 is non-NULL, we prepend the DEFAULT case. The sorted list can
6869 then used during code generation for SELECT CASE constructs with
6870 a case expression of a CHARACTER type. */
6873 head = check_case_overlap (head);
6875 /* Prepend the default_case if it is there. */
6876 if (head != NULL && default_case)
6878 default_case->left = NULL;
6879 default_case->right = head;
6880 head->left = default_case;
6884 /* Eliminate dead blocks that may be the result if we've seen
6885 unreachable case labels for a block. */
6886 for (body = code; body && body->block; body = body->block)
6888 if (body->block->ext.case_list == NULL)
6890 /* Cut the unreachable block from the code chain. */
6891 gfc_code *c = body->block;
6892 body->block = c->block;
6894 /* Kill the dead block, but not the blocks below it. */
6896 gfc_free_statements (c);
6900 /* More than two cases is legal but insane for logical selects.
6901 Issue a warning for it. */
6902 if (gfc_option.warn_surprising && type == BT_LOGICAL
6904 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
6909 /* Check if a derived type is extensible. */
6912 gfc_type_is_extensible (gfc_symbol *sym)
6914 return !(sym->attr.is_bind_c || sym->attr.sequence);
6918 /* Resolve a SELECT TYPE statement. */
6921 resolve_select_type (gfc_code *code)
6923 gfc_symbol *selector_type;
6924 gfc_code *body, *new_st, *if_st, *tail;
6925 gfc_code *class_is = NULL, *default_case = NULL;
6928 char name[GFC_MAX_SYMBOL_LEN];
6936 selector_type = code->expr2->ts.u.derived->components->ts.u.derived;
6938 selector_type = code->expr1->ts.u.derived->components->ts.u.derived;
6940 /* Loop over TYPE IS / CLASS IS cases. */
6941 for (body = code->block; body; body = body->block)
6943 c = body->ext.case_list;
6945 /* Check F03:C815. */
6946 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
6947 && !gfc_type_is_extensible (c->ts.u.derived))
6949 gfc_error ("Derived type '%s' at %L must be extensible",
6950 c->ts.u.derived->name, &c->where);
6955 /* Check F03:C816. */
6956 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
6957 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
6959 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
6960 c->ts.u.derived->name, &c->where, selector_type->name);
6965 /* Intercept the DEFAULT case. */
6966 if (c->ts.type == BT_UNKNOWN)
6968 /* Check F03:C818. */
6971 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6972 "by a second DEFAULT CASE at %L",
6973 &default_case->ext.case_list->where, &c->where);
6978 default_case = body;
6987 /* Insert assignment for selector variable. */
6988 new_st = gfc_get_code ();
6989 new_st->op = EXEC_ASSIGN;
6990 new_st->expr1 = gfc_copy_expr (code->expr1);
6991 new_st->expr2 = gfc_copy_expr (code->expr2);
6995 /* Put SELECT TYPE statement inside a BLOCK. */
6996 new_st = gfc_get_code ();
6997 new_st->op = code->op;
6998 new_st->expr1 = code->expr1;
6999 new_st->expr2 = code->expr2;
7000 new_st->block = code->block;
7004 ns->code->next = new_st;
7005 code->op = EXEC_BLOCK;
7006 code->expr1 = code->expr2 = NULL;
7011 /* Transform to EXEC_SELECT. */
7012 code->op = EXEC_SELECT;
7013 gfc_add_component_ref (code->expr1, "$vptr");
7014 gfc_add_component_ref (code->expr1, "$hash");
7016 /* Loop over TYPE IS / CLASS IS cases. */
7017 for (body = code->block; body; body = body->block)
7019 c = body->ext.case_list;
7021 if (c->ts.type == BT_DERIVED)
7022 c->low = c->high = gfc_int_expr (c->ts.u.derived->hash_value);
7023 else if (c->ts.type == BT_UNKNOWN)
7026 /* Assign temporary to selector. */
7027 if (c->ts.type == BT_CLASS)
7028 sprintf (name, "tmp$class$%s", c->ts.u.derived->name);
7030 sprintf (name, "tmp$type$%s", c->ts.u.derived->name);
7031 st = gfc_find_symtree (ns->sym_root, name);
7032 new_st = gfc_get_code ();
7033 new_st->expr1 = gfc_get_variable_expr (st);
7034 new_st->expr2 = gfc_get_variable_expr (code->expr1->symtree);
7035 if (c->ts.type == BT_DERIVED)
7037 new_st->op = EXEC_POINTER_ASSIGN;
7038 gfc_add_component_ref (new_st->expr2, "$data");
7041 new_st->op = EXEC_POINTER_ASSIGN;
7042 new_st->next = body->next;
7043 body->next = new_st;
7046 /* Take out CLASS IS cases for separate treatment. */
7048 while (body && body->block)
7050 if (body->block->ext.case_list->ts.type == BT_CLASS)
7052 /* Add to class_is list. */
7053 if (class_is == NULL)
7055 class_is = body->block;
7060 for (tail = class_is; tail->block; tail = tail->block) ;
7061 tail->block = body->block;
7064 /* Remove from EXEC_SELECT list. */
7065 body->block = body->block->block;
7078 /* Add a default case to hold the CLASS IS cases. */
7079 for (tail = code; tail->block; tail = tail->block) ;
7080 tail->block = gfc_get_code ();
7082 tail->op = EXEC_SELECT_TYPE;
7083 tail->ext.case_list = gfc_get_case ();
7084 tail->ext.case_list->ts.type = BT_UNKNOWN;
7086 default_case = tail;
7089 /* More than one CLASS IS block? */
7090 if (class_is->block)
7094 /* Sort CLASS IS blocks by extension level. */
7098 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
7101 /* F03:C817 (check for doubles). */
7102 if ((*c1)->ext.case_list->ts.u.derived->hash_value
7103 == c2->ext.case_list->ts.u.derived->hash_value)
7105 gfc_error ("Double CLASS IS block in SELECT TYPE "
7106 "statement at %L", &c2->ext.case_list->where);
7109 if ((*c1)->ext.case_list->ts.u.derived->attr.extension
7110 < c2->ext.case_list->ts.u.derived->attr.extension)
7113 (*c1)->block = c2->block;
7123 /* Generate IF chain. */
7124 if_st = gfc_get_code ();
7125 if_st->op = EXEC_IF;
7127 for (body = class_is; body; body = body->block)
7129 new_st->block = gfc_get_code ();
7130 new_st = new_st->block;
7131 new_st->op = EXEC_IF;
7132 /* Set up IF condition: Call _gfortran_is_extension_of. */
7133 new_st->expr1 = gfc_get_expr ();
7134 new_st->expr1->expr_type = EXPR_FUNCTION;
7135 new_st->expr1->ts.type = BT_LOGICAL;
7136 new_st->expr1->ts.kind = 4;
7137 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
7138 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
7139 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
7140 /* Set up arguments. */
7141 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
7142 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
7143 gfc_add_component_ref (new_st->expr1->value.function.actual->expr, "$vptr");
7144 vtab = gfc_find_derived_vtab (body->ext.case_list->ts.u.derived);
7145 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
7146 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
7147 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
7148 new_st->next = body->next;
7150 if (default_case->next)
7152 new_st->block = gfc_get_code ();
7153 new_st = new_st->block;
7154 new_st->op = EXEC_IF;
7155 new_st->next = default_case->next;
7158 /* Replace CLASS DEFAULT code by the IF chain. */
7159 default_case->next = if_st;
7162 resolve_select (code);
7167 /* Resolve a transfer statement. This is making sure that:
7168 -- a derived type being transferred has only non-pointer components
7169 -- a derived type being transferred doesn't have private components, unless
7170 it's being transferred from the module where the type was defined
7171 -- we're not trying to transfer a whole assumed size array. */
7174 resolve_transfer (gfc_code *code)
7183 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
7186 sym = exp->symtree->n.sym;
7189 /* Go to actual component transferred. */
7190 for (ref = code->expr1->ref; ref; ref = ref->next)
7191 if (ref->type == REF_COMPONENT)
7192 ts = &ref->u.c.component->ts;
7194 if (ts->type == BT_DERIVED)
7196 /* Check that transferred derived type doesn't contain POINTER
7198 if (ts->u.derived->attr.pointer_comp)
7200 gfc_error ("Data transfer element at %L cannot have "
7201 "POINTER components", &code->loc);
7205 if (ts->u.derived->attr.alloc_comp)
7207 gfc_error ("Data transfer element at %L cannot have "
7208 "ALLOCATABLE components", &code->loc);
7212 if (derived_inaccessible (ts->u.derived))
7214 gfc_error ("Data transfer element at %L cannot have "
7215 "PRIVATE components",&code->loc);
7220 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
7221 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
7223 gfc_error ("Data transfer element at %L cannot be a full reference to "
7224 "an assumed-size array", &code->loc);
7230 /*********** Toplevel code resolution subroutines ***********/
7232 /* Find the set of labels that are reachable from this block. We also
7233 record the last statement in each block. */
7236 find_reachable_labels (gfc_code *block)
7243 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
7245 /* Collect labels in this block. We don't keep those corresponding
7246 to END {IF|SELECT}, these are checked in resolve_branch by going
7247 up through the code_stack. */
7248 for (c = block; c; c = c->next)
7250 if (c->here && c->op != EXEC_END_BLOCK)
7251 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
7254 /* Merge with labels from parent block. */
7257 gcc_assert (cs_base->prev->reachable_labels);
7258 bitmap_ior_into (cs_base->reachable_labels,
7259 cs_base->prev->reachable_labels);
7263 /* Given a branch to a label, see if the branch is conforming.
7264 The code node describes where the branch is located. */
7267 resolve_branch (gfc_st_label *label, gfc_code *code)
7274 /* Step one: is this a valid branching target? */
7276 if (label->defined == ST_LABEL_UNKNOWN)
7278 gfc_error ("Label %d referenced at %L is never defined", label->value,
7283 if (label->defined != ST_LABEL_TARGET)
7285 gfc_error ("Statement at %L is not a valid branch target statement "
7286 "for the branch statement at %L", &label->where, &code->loc);
7290 /* Step two: make sure this branch is not a branch to itself ;-) */
7292 if (code->here == label)
7294 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
7298 /* Step three: See if the label is in the same block as the
7299 branching statement. The hard work has been done by setting up
7300 the bitmap reachable_labels. */
7302 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
7305 /* Step four: If we haven't found the label in the bitmap, it may
7306 still be the label of the END of the enclosing block, in which
7307 case we find it by going up the code_stack. */
7309 for (stack = cs_base; stack; stack = stack->prev)
7310 if (stack->current->next && stack->current->next->here == label)
7315 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
7319 /* The label is not in an enclosing block, so illegal. This was
7320 allowed in Fortran 66, so we allow it as extension. No
7321 further checks are necessary in this case. */
7322 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
7323 "as the GOTO statement at %L", &label->where,
7329 /* Check whether EXPR1 has the same shape as EXPR2. */
7332 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
7334 mpz_t shape[GFC_MAX_DIMENSIONS];
7335 mpz_t shape2[GFC_MAX_DIMENSIONS];
7336 gfc_try result = FAILURE;
7339 /* Compare the rank. */
7340 if (expr1->rank != expr2->rank)
7343 /* Compare the size of each dimension. */
7344 for (i=0; i<expr1->rank; i++)
7346 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
7349 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
7352 if (mpz_cmp (shape[i], shape2[i]))
7356 /* When either of the two expression is an assumed size array, we
7357 ignore the comparison of dimension sizes. */
7362 for (i--; i >= 0; i--)
7364 mpz_clear (shape[i]);
7365 mpz_clear (shape2[i]);
7371 /* Check whether a WHERE assignment target or a WHERE mask expression
7372 has the same shape as the outmost WHERE mask expression. */
7375 resolve_where (gfc_code *code, gfc_expr *mask)
7381 cblock = code->block;
7383 /* Store the first WHERE mask-expr of the WHERE statement or construct.
7384 In case of nested WHERE, only the outmost one is stored. */
7385 if (mask == NULL) /* outmost WHERE */
7387 else /* inner WHERE */
7394 /* Check if the mask-expr has a consistent shape with the
7395 outmost WHERE mask-expr. */
7396 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
7397 gfc_error ("WHERE mask at %L has inconsistent shape",
7398 &cblock->expr1->where);
7401 /* the assignment statement of a WHERE statement, or the first
7402 statement in where-body-construct of a WHERE construct */
7403 cnext = cblock->next;
7408 /* WHERE assignment statement */
7411 /* Check shape consistent for WHERE assignment target. */
7412 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
7413 gfc_error ("WHERE assignment target at %L has "
7414 "inconsistent shape", &cnext->expr1->where);
7418 case EXEC_ASSIGN_CALL:
7419 resolve_call (cnext);
7420 if (!cnext->resolved_sym->attr.elemental)
7421 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7422 &cnext->ext.actual->expr->where);
7425 /* WHERE or WHERE construct is part of a where-body-construct */
7427 resolve_where (cnext, e);
7431 gfc_error ("Unsupported statement inside WHERE at %L",
7434 /* the next statement within the same where-body-construct */
7435 cnext = cnext->next;
7437 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7438 cblock = cblock->block;
7443 /* Resolve assignment in FORALL construct.
7444 NVAR is the number of FORALL index variables, and VAR_EXPR records the
7445 FORALL index variables. */
7448 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
7452 for (n = 0; n < nvar; n++)
7454 gfc_symbol *forall_index;
7456 forall_index = var_expr[n]->symtree->n.sym;
7458 /* Check whether the assignment target is one of the FORALL index
7460 if ((code->expr1->expr_type == EXPR_VARIABLE)
7461 && (code->expr1->symtree->n.sym == forall_index))
7462 gfc_error ("Assignment to a FORALL index variable at %L",
7463 &code->expr1->where);
7466 /* If one of the FORALL index variables doesn't appear in the
7467 assignment variable, then there could be a many-to-one
7468 assignment. Emit a warning rather than an error because the
7469 mask could be resolving this problem. */
7470 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
7471 gfc_warning ("The FORALL with index '%s' is not used on the "
7472 "left side of the assignment at %L and so might "
7473 "cause multiple assignment to this object",
7474 var_expr[n]->symtree->name, &code->expr1->where);
7480 /* Resolve WHERE statement in FORALL construct. */
7483 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
7484 gfc_expr **var_expr)
7489 cblock = code->block;
7492 /* the assignment statement of a WHERE statement, or the first
7493 statement in where-body-construct of a WHERE construct */
7494 cnext = cblock->next;
7499 /* WHERE assignment statement */
7501 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
7504 /* WHERE operator assignment statement */
7505 case EXEC_ASSIGN_CALL:
7506 resolve_call (cnext);
7507 if (!cnext->resolved_sym->attr.elemental)
7508 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7509 &cnext->ext.actual->expr->where);
7512 /* WHERE or WHERE construct is part of a where-body-construct */
7514 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
7518 gfc_error ("Unsupported statement inside WHERE at %L",
7521 /* the next statement within the same where-body-construct */
7522 cnext = cnext->next;
7524 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7525 cblock = cblock->block;
7530 /* Traverse the FORALL body to check whether the following errors exist:
7531 1. For assignment, check if a many-to-one assignment happens.
7532 2. For WHERE statement, check the WHERE body to see if there is any
7533 many-to-one assignment. */
7536 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
7540 c = code->block->next;
7546 case EXEC_POINTER_ASSIGN:
7547 gfc_resolve_assign_in_forall (c, nvar, var_expr);
7550 case EXEC_ASSIGN_CALL:
7554 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
7555 there is no need to handle it here. */
7559 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
7564 /* The next statement in the FORALL body. */
7570 /* Counts the number of iterators needed inside a forall construct, including
7571 nested forall constructs. This is used to allocate the needed memory
7572 in gfc_resolve_forall. */
7575 gfc_count_forall_iterators (gfc_code *code)
7577 int max_iters, sub_iters, current_iters;
7578 gfc_forall_iterator *fa;
7580 gcc_assert(code->op == EXEC_FORALL);
7584 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7587 code = code->block->next;
7591 if (code->op == EXEC_FORALL)
7593 sub_iters = gfc_count_forall_iterators (code);
7594 if (sub_iters > max_iters)
7595 max_iters = sub_iters;
7600 return current_iters + max_iters;
7604 /* Given a FORALL construct, first resolve the FORALL iterator, then call
7605 gfc_resolve_forall_body to resolve the FORALL body. */
7608 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
7610 static gfc_expr **var_expr;
7611 static int total_var = 0;
7612 static int nvar = 0;
7614 gfc_forall_iterator *fa;
7619 /* Start to resolve a FORALL construct */
7620 if (forall_save == 0)
7622 /* Count the total number of FORALL index in the nested FORALL
7623 construct in order to allocate the VAR_EXPR with proper size. */
7624 total_var = gfc_count_forall_iterators (code);
7626 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
7627 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
7630 /* The information about FORALL iterator, including FORALL index start, end
7631 and stride. The FORALL index can not appear in start, end or stride. */
7632 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7634 /* Check if any outer FORALL index name is the same as the current
7636 for (i = 0; i < nvar; i++)
7638 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
7640 gfc_error ("An outer FORALL construct already has an index "
7641 "with this name %L", &fa->var->where);
7645 /* Record the current FORALL index. */
7646 var_expr[nvar] = gfc_copy_expr (fa->var);
7650 /* No memory leak. */
7651 gcc_assert (nvar <= total_var);
7654 /* Resolve the FORALL body. */
7655 gfc_resolve_forall_body (code, nvar, var_expr);
7657 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
7658 gfc_resolve_blocks (code->block, ns);
7662 /* Free only the VAR_EXPRs allocated in this frame. */
7663 for (i = nvar; i < tmp; i++)
7664 gfc_free_expr (var_expr[i]);
7668 /* We are in the outermost FORALL construct. */
7669 gcc_assert (forall_save == 0);
7671 /* VAR_EXPR is not needed any more. */
7672 gfc_free (var_expr);
7678 /* Resolve a BLOCK construct statement. */
7681 resolve_block_construct (gfc_code* code)
7683 /* Eventually, we may want to do some checks here or handle special stuff.
7684 But so far the only thing we can do is resolving the local namespace. */
7686 gfc_resolve (code->ext.ns);
7690 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
7693 static void resolve_code (gfc_code *, gfc_namespace *);
7696 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
7700 for (; b; b = b->block)
7702 t = gfc_resolve_expr (b->expr1);
7703 if (gfc_resolve_expr (b->expr2) == FAILURE)
7709 if (t == SUCCESS && b->expr1 != NULL
7710 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
7711 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
7718 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
7719 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
7724 resolve_branch (b->label1, b);
7728 resolve_block_construct (b);
7732 case EXEC_SELECT_TYPE:
7742 case EXEC_OMP_ATOMIC:
7743 case EXEC_OMP_CRITICAL:
7745 case EXEC_OMP_MASTER:
7746 case EXEC_OMP_ORDERED:
7747 case EXEC_OMP_PARALLEL:
7748 case EXEC_OMP_PARALLEL_DO:
7749 case EXEC_OMP_PARALLEL_SECTIONS:
7750 case EXEC_OMP_PARALLEL_WORKSHARE:
7751 case EXEC_OMP_SECTIONS:
7752 case EXEC_OMP_SINGLE:
7754 case EXEC_OMP_TASKWAIT:
7755 case EXEC_OMP_WORKSHARE:
7759 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
7762 resolve_code (b->next, ns);
7767 /* Does everything to resolve an ordinary assignment. Returns true
7768 if this is an interface assignment. */
7770 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
7780 if (gfc_extend_assign (code, ns) == SUCCESS)
7784 if (code->op == EXEC_ASSIGN_CALL)
7786 lhs = code->ext.actual->expr;
7787 rhsptr = &code->ext.actual->next->expr;
7791 gfc_actual_arglist* args;
7792 gfc_typebound_proc* tbp;
7794 gcc_assert (code->op == EXEC_COMPCALL);
7796 args = code->expr1->value.compcall.actual;
7798 rhsptr = &args->next->expr;
7800 tbp = code->expr1->value.compcall.tbp;
7801 gcc_assert (!tbp->is_generic);
7804 /* Make a temporary rhs when there is a default initializer
7805 and rhs is the same symbol as the lhs. */
7806 if ((*rhsptr)->expr_type == EXPR_VARIABLE
7807 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
7808 && has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
7809 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
7810 *rhsptr = gfc_get_parentheses (*rhsptr);
7819 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
7820 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
7821 &code->loc) == FAILURE)
7824 /* Handle the case of a BOZ literal on the RHS. */
7825 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
7828 if (gfc_option.warn_surprising)
7829 gfc_warning ("BOZ literal at %L is bitwise transferred "
7830 "non-integer symbol '%s'", &code->loc,
7831 lhs->symtree->n.sym->name);
7833 if (!gfc_convert_boz (rhs, &lhs->ts))
7835 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
7837 if (rc == ARITH_UNDERFLOW)
7838 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
7839 ". This check can be disabled with the option "
7840 "-fno-range-check", &rhs->where);
7841 else if (rc == ARITH_OVERFLOW)
7842 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
7843 ". This check can be disabled with the option "
7844 "-fno-range-check", &rhs->where);
7845 else if (rc == ARITH_NAN)
7846 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
7847 ". This check can be disabled with the option "
7848 "-fno-range-check", &rhs->where);
7854 if (lhs->ts.type == BT_CHARACTER
7855 && gfc_option.warn_character_truncation)
7857 if (lhs->ts.u.cl != NULL
7858 && lhs->ts.u.cl->length != NULL
7859 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7860 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
7862 if (rhs->expr_type == EXPR_CONSTANT)
7863 rlen = rhs->value.character.length;
7865 else if (rhs->ts.u.cl != NULL
7866 && rhs->ts.u.cl->length != NULL
7867 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7868 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
7870 if (rlen && llen && rlen > llen)
7871 gfc_warning_now ("CHARACTER expression will be truncated "
7872 "in assignment (%d/%d) at %L",
7873 llen, rlen, &code->loc);
7876 /* Ensure that a vector index expression for the lvalue is evaluated
7877 to a temporary if the lvalue symbol is referenced in it. */
7880 for (ref = lhs->ref; ref; ref= ref->next)
7881 if (ref->type == REF_ARRAY)
7883 for (n = 0; n < ref->u.ar.dimen; n++)
7884 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
7885 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
7886 ref->u.ar.start[n]))
7888 = gfc_get_parentheses (ref->u.ar.start[n]);
7892 if (gfc_pure (NULL))
7894 if (gfc_impure_variable (lhs->symtree->n.sym))
7896 gfc_error ("Cannot assign to variable '%s' in PURE "
7898 lhs->symtree->n.sym->name,
7903 if (lhs->ts.type == BT_DERIVED
7904 && lhs->expr_type == EXPR_VARIABLE
7905 && lhs->ts.u.derived->attr.pointer_comp
7906 && gfc_impure_variable (rhs->symtree->n.sym))
7908 gfc_error ("The impure variable at %L is assigned to "
7909 "a derived type variable with a POINTER "
7910 "component in a PURE procedure (12.6)",
7917 if (lhs->ts.type == BT_CLASS)
7919 gfc_error ("Variable must not be polymorphic in assignment at %L",
7924 gfc_check_assign (lhs, rhs, 1);
7929 /* Given a block of code, recursively resolve everything pointed to by this
7933 resolve_code (gfc_code *code, gfc_namespace *ns)
7935 int omp_workshare_save;
7940 frame.prev = cs_base;
7944 find_reachable_labels (code);
7946 for (; code; code = code->next)
7948 frame.current = code;
7949 forall_save = forall_flag;
7951 if (code->op == EXEC_FORALL)
7954 gfc_resolve_forall (code, ns, forall_save);
7957 else if (code->block)
7959 omp_workshare_save = -1;
7962 case EXEC_OMP_PARALLEL_WORKSHARE:
7963 omp_workshare_save = omp_workshare_flag;
7964 omp_workshare_flag = 1;
7965 gfc_resolve_omp_parallel_blocks (code, ns);
7967 case EXEC_OMP_PARALLEL:
7968 case EXEC_OMP_PARALLEL_DO:
7969 case EXEC_OMP_PARALLEL_SECTIONS:
7971 omp_workshare_save = omp_workshare_flag;
7972 omp_workshare_flag = 0;
7973 gfc_resolve_omp_parallel_blocks (code, ns);
7976 gfc_resolve_omp_do_blocks (code, ns);
7978 case EXEC_OMP_WORKSHARE:
7979 omp_workshare_save = omp_workshare_flag;
7980 omp_workshare_flag = 1;
7983 gfc_resolve_blocks (code->block, ns);
7987 if (omp_workshare_save != -1)
7988 omp_workshare_flag = omp_workshare_save;
7992 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
7993 t = gfc_resolve_expr (code->expr1);
7994 forall_flag = forall_save;
7996 if (gfc_resolve_expr (code->expr2) == FAILURE)
7999 if (code->op == EXEC_ALLOCATE
8000 && gfc_resolve_expr (code->expr3) == FAILURE)
8006 case EXEC_END_BLOCK:
8013 case EXEC_ASSIGN_CALL:
8017 /* Keep track of which entry we are up to. */
8018 current_entry_id = code->ext.entry->id;
8022 resolve_where (code, NULL);
8026 if (code->expr1 != NULL)
8028 if (code->expr1->ts.type != BT_INTEGER)
8029 gfc_error ("ASSIGNED GOTO statement at %L requires an "
8030 "INTEGER variable", &code->expr1->where);
8031 else if (code->expr1->symtree->n.sym->attr.assign != 1)
8032 gfc_error ("Variable '%s' has not been assigned a target "
8033 "label at %L", code->expr1->symtree->n.sym->name,
8034 &code->expr1->where);
8037 resolve_branch (code->label1, code);
8041 if (code->expr1 != NULL
8042 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
8043 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
8044 "INTEGER return specifier", &code->expr1->where);
8047 case EXEC_INIT_ASSIGN:
8048 case EXEC_END_PROCEDURE:
8055 if (resolve_ordinary_assign (code, ns))
8057 if (code->op == EXEC_COMPCALL)
8064 case EXEC_LABEL_ASSIGN:
8065 if (code->label1->defined == ST_LABEL_UNKNOWN)
8066 gfc_error ("Label %d referenced at %L is never defined",
8067 code->label1->value, &code->label1->where);
8069 && (code->expr1->expr_type != EXPR_VARIABLE
8070 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
8071 || code->expr1->symtree->n.sym->ts.kind
8072 != gfc_default_integer_kind
8073 || code->expr1->symtree->n.sym->as != NULL))
8074 gfc_error ("ASSIGN statement at %L requires a scalar "
8075 "default INTEGER variable", &code->expr1->where);
8078 case EXEC_POINTER_ASSIGN:
8082 gfc_check_pointer_assign (code->expr1, code->expr2);
8085 case EXEC_ARITHMETIC_IF:
8087 && code->expr1->ts.type != BT_INTEGER
8088 && code->expr1->ts.type != BT_REAL)
8089 gfc_error ("Arithmetic IF statement at %L requires a numeric "
8090 "expression", &code->expr1->where);
8092 resolve_branch (code->label1, code);
8093 resolve_branch (code->label2, code);
8094 resolve_branch (code->label3, code);
8098 if (t == SUCCESS && code->expr1 != NULL
8099 && (code->expr1->ts.type != BT_LOGICAL
8100 || code->expr1->rank != 0))
8101 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8102 &code->expr1->where);
8107 resolve_call (code);
8112 if (code->expr1->symtree
8113 && code->expr1->symtree->n.sym->ts.type == BT_CLASS)
8114 resolve_class_typebound_call (code);
8116 resolve_typebound_call (code);
8120 resolve_ppc_call (code);
8124 /* Select is complicated. Also, a SELECT construct could be
8125 a transformed computed GOTO. */
8126 resolve_select (code);
8129 case EXEC_SELECT_TYPE:
8130 resolve_select_type (code);
8134 gfc_resolve (code->ext.ns);
8138 if (code->ext.iterator != NULL)
8140 gfc_iterator *iter = code->ext.iterator;
8141 if (gfc_resolve_iterator (iter, true) != FAILURE)
8142 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
8147 if (code->expr1 == NULL)
8148 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
8150 && (code->expr1->rank != 0
8151 || code->expr1->ts.type != BT_LOGICAL))
8152 gfc_error ("Exit condition of DO WHILE loop at %L must be "
8153 "a scalar LOGICAL expression", &code->expr1->where);
8158 resolve_allocate_deallocate (code, "ALLOCATE");
8162 case EXEC_DEALLOCATE:
8164 resolve_allocate_deallocate (code, "DEALLOCATE");
8169 if (gfc_resolve_open (code->ext.open) == FAILURE)
8172 resolve_branch (code->ext.open->err, code);
8176 if (gfc_resolve_close (code->ext.close) == FAILURE)
8179 resolve_branch (code->ext.close->err, code);
8182 case EXEC_BACKSPACE:
8186 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
8189 resolve_branch (code->ext.filepos->err, code);
8193 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8196 resolve_branch (code->ext.inquire->err, code);
8200 gcc_assert (code->ext.inquire != NULL);
8201 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8204 resolve_branch (code->ext.inquire->err, code);
8208 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
8211 resolve_branch (code->ext.wait->err, code);
8212 resolve_branch (code->ext.wait->end, code);
8213 resolve_branch (code->ext.wait->eor, code);
8218 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
8221 resolve_branch (code->ext.dt->err, code);
8222 resolve_branch (code->ext.dt->end, code);
8223 resolve_branch (code->ext.dt->eor, code);
8227 resolve_transfer (code);
8231 resolve_forall_iterators (code->ext.forall_iterator);
8233 if (code->expr1 != NULL && code->expr1->ts.type != BT_LOGICAL)
8234 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
8235 "expression", &code->expr1->where);
8238 case EXEC_OMP_ATOMIC:
8239 case EXEC_OMP_BARRIER:
8240 case EXEC_OMP_CRITICAL:
8241 case EXEC_OMP_FLUSH:
8243 case EXEC_OMP_MASTER:
8244 case EXEC_OMP_ORDERED:
8245 case EXEC_OMP_SECTIONS:
8246 case EXEC_OMP_SINGLE:
8247 case EXEC_OMP_TASKWAIT:
8248 case EXEC_OMP_WORKSHARE:
8249 gfc_resolve_omp_directive (code, ns);
8252 case EXEC_OMP_PARALLEL:
8253 case EXEC_OMP_PARALLEL_DO:
8254 case EXEC_OMP_PARALLEL_SECTIONS:
8255 case EXEC_OMP_PARALLEL_WORKSHARE:
8257 omp_workshare_save = omp_workshare_flag;
8258 omp_workshare_flag = 0;
8259 gfc_resolve_omp_directive (code, ns);
8260 omp_workshare_flag = omp_workshare_save;
8264 gfc_internal_error ("resolve_code(): Bad statement code");
8268 cs_base = frame.prev;
8272 /* Resolve initial values and make sure they are compatible with
8276 resolve_values (gfc_symbol *sym)
8278 if (sym->value == NULL)
8281 if (gfc_resolve_expr (sym->value) == FAILURE)
8284 gfc_check_assign_symbol (sym, sym->value);
8288 /* Verify the binding labels for common blocks that are BIND(C). The label
8289 for a BIND(C) common block must be identical in all scoping units in which
8290 the common block is declared. Further, the binding label can not collide
8291 with any other global entity in the program. */
8294 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
8296 if (comm_block_tree->n.common->is_bind_c == 1)
8298 gfc_gsymbol *binding_label_gsym;
8299 gfc_gsymbol *comm_name_gsym;
8301 /* See if a global symbol exists by the common block's name. It may
8302 be NULL if the common block is use-associated. */
8303 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
8304 comm_block_tree->n.common->name);
8305 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
8306 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
8307 "with the global entity '%s' at %L",
8308 comm_block_tree->n.common->binding_label,
8309 comm_block_tree->n.common->name,
8310 &(comm_block_tree->n.common->where),
8311 comm_name_gsym->name, &(comm_name_gsym->where));
8312 else if (comm_name_gsym != NULL
8313 && strcmp (comm_name_gsym->name,
8314 comm_block_tree->n.common->name) == 0)
8316 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
8318 if (comm_name_gsym->binding_label == NULL)
8319 /* No binding label for common block stored yet; save this one. */
8320 comm_name_gsym->binding_label =
8321 comm_block_tree->n.common->binding_label;
8323 if (strcmp (comm_name_gsym->binding_label,
8324 comm_block_tree->n.common->binding_label) != 0)
8326 /* Common block names match but binding labels do not. */
8327 gfc_error ("Binding label '%s' for common block '%s' at %L "
8328 "does not match the binding label '%s' for common "
8330 comm_block_tree->n.common->binding_label,
8331 comm_block_tree->n.common->name,
8332 &(comm_block_tree->n.common->where),
8333 comm_name_gsym->binding_label,
8334 comm_name_gsym->name,
8335 &(comm_name_gsym->where));
8340 /* There is no binding label (NAME="") so we have nothing further to
8341 check and nothing to add as a global symbol for the label. */
8342 if (comm_block_tree->n.common->binding_label[0] == '\0' )
8345 binding_label_gsym =
8346 gfc_find_gsymbol (gfc_gsym_root,
8347 comm_block_tree->n.common->binding_label);
8348 if (binding_label_gsym == NULL)
8350 /* Need to make a global symbol for the binding label to prevent
8351 it from colliding with another. */
8352 binding_label_gsym =
8353 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
8354 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
8355 binding_label_gsym->type = GSYM_COMMON;
8359 /* If comm_name_gsym is NULL, the name common block is use
8360 associated and the name could be colliding. */
8361 if (binding_label_gsym->type != GSYM_COMMON)
8362 gfc_error ("Binding label '%s' for common block '%s' at %L "
8363 "collides with the global entity '%s' at %L",
8364 comm_block_tree->n.common->binding_label,
8365 comm_block_tree->n.common->name,
8366 &(comm_block_tree->n.common->where),
8367 binding_label_gsym->name,
8368 &(binding_label_gsym->where));
8369 else if (comm_name_gsym != NULL
8370 && (strcmp (binding_label_gsym->name,
8371 comm_name_gsym->binding_label) != 0)
8372 && (strcmp (binding_label_gsym->sym_name,
8373 comm_name_gsym->name) != 0))
8374 gfc_error ("Binding label '%s' for common block '%s' at %L "
8375 "collides with global entity '%s' at %L",
8376 binding_label_gsym->name, binding_label_gsym->sym_name,
8377 &(comm_block_tree->n.common->where),
8378 comm_name_gsym->name, &(comm_name_gsym->where));
8386 /* Verify any BIND(C) derived types in the namespace so we can report errors
8387 for them once, rather than for each variable declared of that type. */
8390 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
8392 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
8393 && derived_sym->attr.is_bind_c == 1)
8394 verify_bind_c_derived_type (derived_sym);
8400 /* Verify that any binding labels used in a given namespace do not collide
8401 with the names or binding labels of any global symbols. */
8404 gfc_verify_binding_labels (gfc_symbol *sym)
8408 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
8409 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
8411 gfc_gsymbol *bind_c_sym;
8413 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
8414 if (bind_c_sym != NULL
8415 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
8417 if (sym->attr.if_source == IFSRC_DECL
8418 && (bind_c_sym->type != GSYM_SUBROUTINE
8419 && bind_c_sym->type != GSYM_FUNCTION)
8420 && ((sym->attr.contained == 1
8421 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
8422 || (sym->attr.use_assoc == 1
8423 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
8425 /* Make sure global procedures don't collide with anything. */
8426 gfc_error ("Binding label '%s' at %L collides with the global "
8427 "entity '%s' at %L", sym->binding_label,
8428 &(sym->declared_at), bind_c_sym->name,
8429 &(bind_c_sym->where));
8432 else if (sym->attr.contained == 0
8433 && (sym->attr.if_source == IFSRC_IFBODY
8434 && sym->attr.flavor == FL_PROCEDURE)
8435 && (bind_c_sym->sym_name != NULL
8436 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
8438 /* Make sure procedures in interface bodies don't collide. */
8439 gfc_error ("Binding label '%s' in interface body at %L collides "
8440 "with the global entity '%s' at %L",
8442 &(sym->declared_at), bind_c_sym->name,
8443 &(bind_c_sym->where));
8446 else if (sym->attr.contained == 0
8447 && sym->attr.if_source == IFSRC_UNKNOWN)
8448 if ((sym->attr.use_assoc && bind_c_sym->mod_name
8449 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
8450 || sym->attr.use_assoc == 0)
8452 gfc_error ("Binding label '%s' at %L collides with global "
8453 "entity '%s' at %L", sym->binding_label,
8454 &(sym->declared_at), bind_c_sym->name,
8455 &(bind_c_sym->where));
8460 /* Clear the binding label to prevent checking multiple times. */
8461 sym->binding_label[0] = '\0';
8463 else if (bind_c_sym == NULL)
8465 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
8466 bind_c_sym->where = sym->declared_at;
8467 bind_c_sym->sym_name = sym->name;
8469 if (sym->attr.use_assoc == 1)
8470 bind_c_sym->mod_name = sym->module;
8472 if (sym->ns->proc_name != NULL)
8473 bind_c_sym->mod_name = sym->ns->proc_name->name;
8475 if (sym->attr.contained == 0)
8477 if (sym->attr.subroutine)
8478 bind_c_sym->type = GSYM_SUBROUTINE;
8479 else if (sym->attr.function)
8480 bind_c_sym->type = GSYM_FUNCTION;
8488 /* Resolve an index expression. */
8491 resolve_index_expr (gfc_expr *e)
8493 if (gfc_resolve_expr (e) == FAILURE)
8496 if (gfc_simplify_expr (e, 0) == FAILURE)
8499 if (gfc_specification_expr (e) == FAILURE)
8505 /* Resolve a charlen structure. */
8508 resolve_charlen (gfc_charlen *cl)
8517 specification_expr = 1;
8519 if (resolve_index_expr (cl->length) == FAILURE)
8521 specification_expr = 0;
8525 /* "If the character length parameter value evaluates to a negative
8526 value, the length of character entities declared is zero." */
8527 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
8529 gfc_warning_now ("CHARACTER variable has zero length at %L",
8530 &cl->length->where);
8531 gfc_replace_expr (cl->length, gfc_int_expr (0));
8534 /* Check that the character length is not too large. */
8535 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
8536 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
8537 && cl->length->ts.type == BT_INTEGER
8538 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
8540 gfc_error ("String length at %L is too large", &cl->length->where);
8548 /* Test for non-constant shape arrays. */
8551 is_non_constant_shape_array (gfc_symbol *sym)
8557 not_constant = false;
8558 if (sym->as != NULL)
8560 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
8561 has not been simplified; parameter array references. Do the
8562 simplification now. */
8563 for (i = 0; i < sym->as->rank; i++)
8565 e = sym->as->lower[i];
8566 if (e && (resolve_index_expr (e) == FAILURE
8567 || !gfc_is_constant_expr (e)))
8568 not_constant = true;
8570 e = sym->as->upper[i];
8571 if (e && (resolve_index_expr (e) == FAILURE
8572 || !gfc_is_constant_expr (e)))
8573 not_constant = true;
8576 return not_constant;
8579 /* Given a symbol and an initialization expression, add code to initialize
8580 the symbol to the function entry. */
8582 build_init_assign (gfc_symbol *sym, gfc_expr *init)
8586 gfc_namespace *ns = sym->ns;
8588 /* Search for the function namespace if this is a contained
8589 function without an explicit result. */
8590 if (sym->attr.function && sym == sym->result
8591 && sym->name != sym->ns->proc_name->name)
8594 for (;ns; ns = ns->sibling)
8595 if (strcmp (ns->proc_name->name, sym->name) == 0)
8601 gfc_free_expr (init);
8605 /* Build an l-value expression for the result. */
8606 lval = gfc_lval_expr_from_sym (sym);
8608 /* Add the code at scope entry. */
8609 init_st = gfc_get_code ();
8610 init_st->next = ns->code;
8613 /* Assign the default initializer to the l-value. */
8614 init_st->loc = sym->declared_at;
8615 init_st->op = EXEC_INIT_ASSIGN;
8616 init_st->expr1 = lval;
8617 init_st->expr2 = init;
8620 /* Assign the default initializer to a derived type variable or result. */
8623 apply_default_init (gfc_symbol *sym)
8625 gfc_expr *init = NULL;
8627 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
8630 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
8631 init = gfc_default_initializer (&sym->ts);
8636 build_init_assign (sym, init);
8639 /* Build an initializer for a local integer, real, complex, logical, or
8640 character variable, based on the command line flags finit-local-zero,
8641 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
8642 null if the symbol should not have a default initialization. */
8644 build_default_init_expr (gfc_symbol *sym)
8647 gfc_expr *init_expr;
8650 /* These symbols should never have a default initialization. */
8651 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
8652 || sym->attr.external
8654 || sym->attr.pointer
8655 || sym->attr.in_equivalence
8656 || sym->attr.in_common
8659 || sym->attr.cray_pointee
8660 || sym->attr.cray_pointer)
8663 /* Now we'll try to build an initializer expression. */
8664 init_expr = gfc_get_expr ();
8665 init_expr->expr_type = EXPR_CONSTANT;
8666 init_expr->ts.type = sym->ts.type;
8667 init_expr->ts.kind = sym->ts.kind;
8668 init_expr->where = sym->declared_at;
8670 /* We will only initialize integers, reals, complex, logicals, and
8671 characters, and only if the corresponding command-line flags
8672 were set. Otherwise, we free init_expr and return null. */
8673 switch (sym->ts.type)
8676 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
8677 mpz_init_set_si (init_expr->value.integer,
8678 gfc_option.flag_init_integer_value);
8681 gfc_free_expr (init_expr);
8687 mpfr_init (init_expr->value.real);
8688 switch (gfc_option.flag_init_real)
8690 case GFC_INIT_REAL_SNAN:
8691 init_expr->is_snan = 1;
8693 case GFC_INIT_REAL_NAN:
8694 mpfr_set_nan (init_expr->value.real);
8697 case GFC_INIT_REAL_INF:
8698 mpfr_set_inf (init_expr->value.real, 1);
8701 case GFC_INIT_REAL_NEG_INF:
8702 mpfr_set_inf (init_expr->value.real, -1);
8705 case GFC_INIT_REAL_ZERO:
8706 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
8710 gfc_free_expr (init_expr);
8717 mpc_init2 (init_expr->value.complex, mpfr_get_default_prec());
8718 switch (gfc_option.flag_init_real)
8720 case GFC_INIT_REAL_SNAN:
8721 init_expr->is_snan = 1;
8723 case GFC_INIT_REAL_NAN:
8724 mpfr_set_nan (mpc_realref (init_expr->value.complex));
8725 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
8728 case GFC_INIT_REAL_INF:
8729 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
8730 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
8733 case GFC_INIT_REAL_NEG_INF:
8734 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
8735 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
8738 case GFC_INIT_REAL_ZERO:
8739 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
8743 gfc_free_expr (init_expr);
8750 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
8751 init_expr->value.logical = 0;
8752 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
8753 init_expr->value.logical = 1;
8756 gfc_free_expr (init_expr);
8762 /* For characters, the length must be constant in order to
8763 create a default initializer. */
8764 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
8765 && sym->ts.u.cl->length
8766 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8768 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
8769 init_expr->value.character.length = char_len;
8770 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
8771 for (i = 0; i < char_len; i++)
8772 init_expr->value.character.string[i]
8773 = (unsigned char) gfc_option.flag_init_character_value;
8777 gfc_free_expr (init_expr);
8783 gfc_free_expr (init_expr);
8789 /* Add an initialization expression to a local variable. */
8791 apply_default_init_local (gfc_symbol *sym)
8793 gfc_expr *init = NULL;
8795 /* The symbol should be a variable or a function return value. */
8796 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
8797 || (sym->attr.function && sym->result != sym))
8800 /* Try to build the initializer expression. If we can't initialize
8801 this symbol, then init will be NULL. */
8802 init = build_default_init_expr (sym);
8806 /* For saved variables, we don't want to add an initializer at
8807 function entry, so we just add a static initializer. */
8808 if (sym->attr.save || sym->ns->save_all
8809 || gfc_option.flag_max_stack_var_size == 0)
8811 /* Don't clobber an existing initializer! */
8812 gcc_assert (sym->value == NULL);
8817 build_init_assign (sym, init);
8820 /* Resolution of common features of flavors variable and procedure. */
8823 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
8825 /* Constraints on deferred shape variable. */
8826 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
8828 if (sym->attr.allocatable)
8830 if (sym->attr.dimension)
8832 gfc_error ("Allocatable array '%s' at %L must have "
8833 "a deferred shape", sym->name, &sym->declared_at);
8836 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
8837 "may not be ALLOCATABLE", sym->name,
8838 &sym->declared_at) == FAILURE)
8842 if (sym->attr.pointer && sym->attr.dimension)
8844 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
8845 sym->name, &sym->declared_at);
8852 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
8853 && !sym->attr.dummy && sym->ts.type != BT_CLASS)
8855 gfc_error ("Array '%s' at %L cannot have a deferred shape",
8856 sym->name, &sym->declared_at);
8864 /* Additional checks for symbols with flavor variable and derived
8865 type. To be called from resolve_fl_variable. */
8868 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
8870 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
8872 /* Check to see if a derived type is blocked from being host
8873 associated by the presence of another class I symbol in the same
8874 namespace. 14.6.1.3 of the standard and the discussion on
8875 comp.lang.fortran. */
8876 if (sym->ns != sym->ts.u.derived->ns
8877 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
8880 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
8881 if (s && s->attr.flavor != FL_DERIVED)
8883 gfc_error ("The type '%s' cannot be host associated at %L "
8884 "because it is blocked by an incompatible object "
8885 "of the same name declared at %L",
8886 sym->ts.u.derived->name, &sym->declared_at,
8892 /* 4th constraint in section 11.3: "If an object of a type for which
8893 component-initialization is specified (R429) appears in the
8894 specification-part of a module and does not have the ALLOCATABLE
8895 or POINTER attribute, the object shall have the SAVE attribute."
8897 The check for initializers is performed with
8898 has_default_initializer because gfc_default_initializer generates
8899 a hidden default for allocatable components. */
8900 if (!(sym->value || no_init_flag) && sym->ns->proc_name
8901 && sym->ns->proc_name->attr.flavor == FL_MODULE
8902 && !sym->ns->save_all && !sym->attr.save
8903 && !sym->attr.pointer && !sym->attr.allocatable
8904 && has_default_initializer (sym->ts.u.derived))
8906 gfc_error("Object '%s' at %L must have the SAVE attribute for "
8907 "default initialization of a component",
8908 sym->name, &sym->declared_at);
8912 if (sym->ts.type == BT_CLASS)
8915 if (!gfc_type_is_extensible (sym->ts.u.derived->components->ts.u.derived))
8917 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
8918 sym->ts.u.derived->components->ts.u.derived->name,
8919 sym->name, &sym->declared_at);
8924 /* Assume that use associated symbols were checked in the module ns. */
8925 if (!sym->attr.class_ok && !sym->attr.use_assoc)
8927 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
8928 "or pointer", sym->name, &sym->declared_at);
8933 /* Assign default initializer. */
8934 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
8935 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
8937 sym->value = gfc_default_initializer (&sym->ts);
8944 /* Resolve symbols with flavor variable. */
8947 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
8949 int no_init_flag, automatic_flag;
8951 const char *auto_save_msg;
8953 auto_save_msg = "Automatic object '%s' at %L cannot have the "
8956 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
8959 /* Set this flag to check that variables are parameters of all entries.
8960 This check is effected by the call to gfc_resolve_expr through
8961 is_non_constant_shape_array. */
8962 specification_expr = 1;
8964 if (sym->ns->proc_name
8965 && (sym->ns->proc_name->attr.flavor == FL_MODULE
8966 || sym->ns->proc_name->attr.is_main_program)
8967 && !sym->attr.use_assoc
8968 && !sym->attr.allocatable
8969 && !sym->attr.pointer
8970 && is_non_constant_shape_array (sym))
8972 /* The shape of a main program or module array needs to be
8974 gfc_error ("The module or main program array '%s' at %L must "
8975 "have constant shape", sym->name, &sym->declared_at);
8976 specification_expr = 0;
8980 if (sym->ts.type == BT_CHARACTER)
8982 /* Make sure that character string variables with assumed length are
8984 e = sym->ts.u.cl->length;
8985 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
8987 gfc_error ("Entity with assumed character length at %L must be a "
8988 "dummy argument or a PARAMETER", &sym->declared_at);
8992 if (e && sym->attr.save && !gfc_is_constant_expr (e))
8994 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
8998 if (!gfc_is_constant_expr (e)
8999 && !(e->expr_type == EXPR_VARIABLE
9000 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
9001 && sym->ns->proc_name
9002 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9003 || sym->ns->proc_name->attr.is_main_program)
9004 && !sym->attr.use_assoc)
9006 gfc_error ("'%s' at %L must have constant character length "
9007 "in this context", sym->name, &sym->declared_at);
9012 if (sym->value == NULL && sym->attr.referenced)
9013 apply_default_init_local (sym); /* Try to apply a default initialization. */
9015 /* Determine if the symbol may not have an initializer. */
9016 no_init_flag = automatic_flag = 0;
9017 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
9018 || sym->attr.intrinsic || sym->attr.result)
9020 else if (sym->attr.dimension && !sym->attr.pointer
9021 && is_non_constant_shape_array (sym))
9023 no_init_flag = automatic_flag = 1;
9025 /* Also, they must not have the SAVE attribute.
9026 SAVE_IMPLICIT is checked below. */
9027 if (sym->attr.save == SAVE_EXPLICIT)
9029 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9034 /* Ensure that any initializer is simplified. */
9036 gfc_simplify_expr (sym->value, 1);
9038 /* Reject illegal initializers. */
9039 if (!sym->mark && sym->value)
9041 if (sym->attr.allocatable)
9042 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
9043 sym->name, &sym->declared_at);
9044 else if (sym->attr.external)
9045 gfc_error ("External '%s' at %L cannot have an initializer",
9046 sym->name, &sym->declared_at);
9047 else if (sym->attr.dummy
9048 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
9049 gfc_error ("Dummy '%s' at %L cannot have an initializer",
9050 sym->name, &sym->declared_at);
9051 else if (sym->attr.intrinsic)
9052 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
9053 sym->name, &sym->declared_at);
9054 else if (sym->attr.result)
9055 gfc_error ("Function result '%s' at %L cannot have an initializer",
9056 sym->name, &sym->declared_at);
9057 else if (automatic_flag)
9058 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
9059 sym->name, &sym->declared_at);
9066 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
9067 return resolve_fl_variable_derived (sym, no_init_flag);
9073 /* Resolve a procedure. */
9076 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
9078 gfc_formal_arglist *arg;
9080 if (sym->attr.ambiguous_interfaces && !sym->attr.referenced)
9081 gfc_warning ("Although not referenced, '%s' at %L has ambiguous "
9082 "interfaces", sym->name, &sym->declared_at);
9084 if (sym->attr.function
9085 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9088 if (sym->ts.type == BT_CHARACTER)
9090 gfc_charlen *cl = sym->ts.u.cl;
9092 if (cl && cl->length && gfc_is_constant_expr (cl->length)
9093 && resolve_charlen (cl) == FAILURE)
9096 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
9097 && sym->attr.proc == PROC_ST_FUNCTION)
9099 gfc_error ("Character-valued statement function '%s' at %L must "
9100 "have constant length", sym->name, &sym->declared_at);
9105 /* Ensure that derived type for are not of a private type. Internal
9106 module procedures are excluded by 2.2.3.3 - i.e., they are not
9107 externally accessible and can access all the objects accessible in
9109 if (!(sym->ns->parent
9110 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
9111 && gfc_check_access(sym->attr.access, sym->ns->default_access))
9113 gfc_interface *iface;
9115 for (arg = sym->formal; arg; arg = arg->next)
9118 && arg->sym->ts.type == BT_DERIVED
9119 && !arg->sym->ts.u.derived->attr.use_assoc
9120 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9121 arg->sym->ts.u.derived->ns->default_access)
9122 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
9123 "PRIVATE type and cannot be a dummy argument"
9124 " of '%s', which is PUBLIC at %L",
9125 arg->sym->name, sym->name, &sym->declared_at)
9128 /* Stop this message from recurring. */
9129 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9134 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9135 PRIVATE to the containing module. */
9136 for (iface = sym->generic; iface; iface = iface->next)
9138 for (arg = iface->sym->formal; arg; arg = arg->next)
9141 && arg->sym->ts.type == BT_DERIVED
9142 && !arg->sym->ts.u.derived->attr.use_assoc
9143 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9144 arg->sym->ts.u.derived->ns->default_access)
9145 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9146 "'%s' in PUBLIC interface '%s' at %L "
9147 "takes dummy arguments of '%s' which is "
9148 "PRIVATE", iface->sym->name, sym->name,
9149 &iface->sym->declared_at,
9150 gfc_typename (&arg->sym->ts)) == FAILURE)
9152 /* Stop this message from recurring. */
9153 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9159 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9160 PRIVATE to the containing module. */
9161 for (iface = sym->generic; iface; iface = iface->next)
9163 for (arg = iface->sym->formal; arg; arg = arg->next)
9166 && arg->sym->ts.type == BT_DERIVED
9167 && !arg->sym->ts.u.derived->attr.use_assoc
9168 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9169 arg->sym->ts.u.derived->ns->default_access)
9170 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9171 "'%s' in PUBLIC interface '%s' at %L "
9172 "takes dummy arguments of '%s' which is "
9173 "PRIVATE", iface->sym->name, sym->name,
9174 &iface->sym->declared_at,
9175 gfc_typename (&arg->sym->ts)) == FAILURE)
9177 /* Stop this message from recurring. */
9178 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9185 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
9186 && !sym->attr.proc_pointer)
9188 gfc_error ("Function '%s' at %L cannot have an initializer",
9189 sym->name, &sym->declared_at);
9193 /* An external symbol may not have an initializer because it is taken to be
9194 a procedure. Exception: Procedure Pointers. */
9195 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
9197 gfc_error ("External object '%s' at %L may not have an initializer",
9198 sym->name, &sym->declared_at);
9202 /* An elemental function is required to return a scalar 12.7.1 */
9203 if (sym->attr.elemental && sym->attr.function && sym->as)
9205 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
9206 "result", sym->name, &sym->declared_at);
9207 /* Reset so that the error only occurs once. */
9208 sym->attr.elemental = 0;
9212 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
9213 char-len-param shall not be array-valued, pointer-valued, recursive
9214 or pure. ....snip... A character value of * may only be used in the
9215 following ways: (i) Dummy arg of procedure - dummy associates with
9216 actual length; (ii) To declare a named constant; or (iii) External
9217 function - but length must be declared in calling scoping unit. */
9218 if (sym->attr.function
9219 && sym->ts.type == BT_CHARACTER
9220 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
9222 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
9223 || (sym->attr.recursive) || (sym->attr.pure))
9225 if (sym->as && sym->as->rank)
9226 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9227 "array-valued", sym->name, &sym->declared_at);
9229 if (sym->attr.pointer)
9230 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9231 "pointer-valued", sym->name, &sym->declared_at);
9234 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9235 "pure", sym->name, &sym->declared_at);
9237 if (sym->attr.recursive)
9238 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9239 "recursive", sym->name, &sym->declared_at);
9244 /* Appendix B.2 of the standard. Contained functions give an
9245 error anyway. Fixed-form is likely to be F77/legacy. */
9246 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
9247 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
9248 "CHARACTER(*) function '%s' at %L",
9249 sym->name, &sym->declared_at);
9252 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
9254 gfc_formal_arglist *curr_arg;
9255 int has_non_interop_arg = 0;
9257 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
9258 sym->common_block) == FAILURE)
9260 /* Clear these to prevent looking at them again if there was an
9262 sym->attr.is_bind_c = 0;
9263 sym->attr.is_c_interop = 0;
9264 sym->ts.is_c_interop = 0;
9268 /* So far, no errors have been found. */
9269 sym->attr.is_c_interop = 1;
9270 sym->ts.is_c_interop = 1;
9273 curr_arg = sym->formal;
9274 while (curr_arg != NULL)
9276 /* Skip implicitly typed dummy args here. */
9277 if (curr_arg->sym->attr.implicit_type == 0)
9278 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
9279 /* If something is found to fail, record the fact so we
9280 can mark the symbol for the procedure as not being
9281 BIND(C) to try and prevent multiple errors being
9283 has_non_interop_arg = 1;
9285 curr_arg = curr_arg->next;
9288 /* See if any of the arguments were not interoperable and if so, clear
9289 the procedure symbol to prevent duplicate error messages. */
9290 if (has_non_interop_arg != 0)
9292 sym->attr.is_c_interop = 0;
9293 sym->ts.is_c_interop = 0;
9294 sym->attr.is_bind_c = 0;
9298 if (!sym->attr.proc_pointer)
9300 if (sym->attr.save == SAVE_EXPLICIT)
9302 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
9303 "in '%s' at %L", sym->name, &sym->declared_at);
9306 if (sym->attr.intent)
9308 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
9309 "in '%s' at %L", sym->name, &sym->declared_at);
9312 if (sym->attr.subroutine && sym->attr.result)
9314 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
9315 "in '%s' at %L", sym->name, &sym->declared_at);
9318 if (sym->attr.external && sym->attr.function
9319 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
9320 || sym->attr.contained))
9322 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
9323 "in '%s' at %L", sym->name, &sym->declared_at);
9326 if (strcmp ("ppr@", sym->name) == 0)
9328 gfc_error ("Procedure pointer result '%s' at %L "
9329 "is missing the pointer attribute",
9330 sym->ns->proc_name->name, &sym->declared_at);
9339 /* Resolve a list of finalizer procedures. That is, after they have hopefully
9340 been defined and we now know their defined arguments, check that they fulfill
9341 the requirements of the standard for procedures used as finalizers. */
9344 gfc_resolve_finalizers (gfc_symbol* derived)
9346 gfc_finalizer* list;
9347 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
9348 gfc_try result = SUCCESS;
9349 bool seen_scalar = false;
9351 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
9354 /* Walk over the list of finalizer-procedures, check them, and if any one
9355 does not fit in with the standard's definition, print an error and remove
9356 it from the list. */
9357 prev_link = &derived->f2k_derived->finalizers;
9358 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
9364 /* Skip this finalizer if we already resolved it. */
9365 if (list->proc_tree)
9367 prev_link = &(list->next);
9371 /* Check this exists and is a SUBROUTINE. */
9372 if (!list->proc_sym->attr.subroutine)
9374 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
9375 list->proc_sym->name, &list->where);
9379 /* We should have exactly one argument. */
9380 if (!list->proc_sym->formal || list->proc_sym->formal->next)
9382 gfc_error ("FINAL procedure at %L must have exactly one argument",
9386 arg = list->proc_sym->formal->sym;
9388 /* This argument must be of our type. */
9389 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
9391 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
9392 &arg->declared_at, derived->name);
9396 /* It must neither be a pointer nor allocatable nor optional. */
9397 if (arg->attr.pointer)
9399 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
9403 if (arg->attr.allocatable)
9405 gfc_error ("Argument of FINAL procedure at %L must not be"
9406 " ALLOCATABLE", &arg->declared_at);
9409 if (arg->attr.optional)
9411 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
9416 /* It must not be INTENT(OUT). */
9417 if (arg->attr.intent == INTENT_OUT)
9419 gfc_error ("Argument of FINAL procedure at %L must not be"
9420 " INTENT(OUT)", &arg->declared_at);
9424 /* Warn if the procedure is non-scalar and not assumed shape. */
9425 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
9426 && arg->as->type != AS_ASSUMED_SHAPE)
9427 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
9428 " shape argument", &arg->declared_at);
9430 /* Check that it does not match in kind and rank with a FINAL procedure
9431 defined earlier. To really loop over the *earlier* declarations,
9432 we need to walk the tail of the list as new ones were pushed at the
9434 /* TODO: Handle kind parameters once they are implemented. */
9435 my_rank = (arg->as ? arg->as->rank : 0);
9436 for (i = list->next; i; i = i->next)
9438 /* Argument list might be empty; that is an error signalled earlier,
9439 but we nevertheless continued resolving. */
9440 if (i->proc_sym->formal)
9442 gfc_symbol* i_arg = i->proc_sym->formal->sym;
9443 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
9444 if (i_rank == my_rank)
9446 gfc_error ("FINAL procedure '%s' declared at %L has the same"
9447 " rank (%d) as '%s'",
9448 list->proc_sym->name, &list->where, my_rank,
9455 /* Is this the/a scalar finalizer procedure? */
9456 if (!arg->as || arg->as->rank == 0)
9459 /* Find the symtree for this procedure. */
9460 gcc_assert (!list->proc_tree);
9461 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
9463 prev_link = &list->next;
9466 /* Remove wrong nodes immediately from the list so we don't risk any
9467 troubles in the future when they might fail later expectations. */
9471 *prev_link = list->next;
9472 gfc_free_finalizer (i);
9475 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
9476 were nodes in the list, must have been for arrays. It is surely a good
9477 idea to have a scalar version there if there's something to finalize. */
9478 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
9479 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
9480 " defined at %L, suggest also scalar one",
9481 derived->name, &derived->declared_at);
9483 /* TODO: Remove this error when finalization is finished. */
9484 gfc_error ("Finalization at %L is not yet implemented",
9485 &derived->declared_at);
9491 /* Check that it is ok for the typebound procedure proc to override the
9495 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
9498 const gfc_symbol* proc_target;
9499 const gfc_symbol* old_target;
9500 unsigned proc_pass_arg, old_pass_arg, argpos;
9501 gfc_formal_arglist* proc_formal;
9502 gfc_formal_arglist* old_formal;
9504 /* This procedure should only be called for non-GENERIC proc. */
9505 gcc_assert (!proc->n.tb->is_generic);
9507 /* If the overwritten procedure is GENERIC, this is an error. */
9508 if (old->n.tb->is_generic)
9510 gfc_error ("Can't overwrite GENERIC '%s' at %L",
9511 old->name, &proc->n.tb->where);
9515 where = proc->n.tb->where;
9516 proc_target = proc->n.tb->u.specific->n.sym;
9517 old_target = old->n.tb->u.specific->n.sym;
9519 /* Check that overridden binding is not NON_OVERRIDABLE. */
9520 if (old->n.tb->non_overridable)
9522 gfc_error ("'%s' at %L overrides a procedure binding declared"
9523 " NON_OVERRIDABLE", proc->name, &where);
9527 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
9528 if (!old->n.tb->deferred && proc->n.tb->deferred)
9530 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
9531 " non-DEFERRED binding", proc->name, &where);
9535 /* If the overridden binding is PURE, the overriding must be, too. */
9536 if (old_target->attr.pure && !proc_target->attr.pure)
9538 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
9539 proc->name, &where);
9543 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
9544 is not, the overriding must not be either. */
9545 if (old_target->attr.elemental && !proc_target->attr.elemental)
9547 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
9548 " ELEMENTAL", proc->name, &where);
9551 if (!old_target->attr.elemental && proc_target->attr.elemental)
9553 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
9554 " be ELEMENTAL, either", proc->name, &where);
9558 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
9560 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
9562 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
9563 " SUBROUTINE", proc->name, &where);
9567 /* If the overridden binding is a FUNCTION, the overriding must also be a
9568 FUNCTION and have the same characteristics. */
9569 if (old_target->attr.function)
9571 if (!proc_target->attr.function)
9573 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
9574 " FUNCTION", proc->name, &where);
9578 /* FIXME: Do more comprehensive checking (including, for instance, the
9579 rank and array-shape). */
9580 gcc_assert (proc_target->result && old_target->result);
9581 if (!gfc_compare_types (&proc_target->result->ts,
9582 &old_target->result->ts))
9584 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
9585 " matching result types", proc->name, &where);
9590 /* If the overridden binding is PUBLIC, the overriding one must not be
9592 if (old->n.tb->access == ACCESS_PUBLIC
9593 && proc->n.tb->access == ACCESS_PRIVATE)
9595 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
9596 " PRIVATE", proc->name, &where);
9600 /* Compare the formal argument lists of both procedures. This is also abused
9601 to find the position of the passed-object dummy arguments of both
9602 bindings as at least the overridden one might not yet be resolved and we
9603 need those positions in the check below. */
9604 proc_pass_arg = old_pass_arg = 0;
9605 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
9607 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
9610 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
9611 proc_formal && old_formal;
9612 proc_formal = proc_formal->next, old_formal = old_formal->next)
9614 if (proc->n.tb->pass_arg
9615 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
9616 proc_pass_arg = argpos;
9617 if (old->n.tb->pass_arg
9618 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
9619 old_pass_arg = argpos;
9621 /* Check that the names correspond. */
9622 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
9624 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
9625 " to match the corresponding argument of the overridden"
9626 " procedure", proc_formal->sym->name, proc->name, &where,
9627 old_formal->sym->name);
9631 /* Check that the types correspond if neither is the passed-object
9633 /* FIXME: Do more comprehensive testing here. */
9634 if (proc_pass_arg != argpos && old_pass_arg != argpos
9635 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
9637 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
9638 "in respect to the overridden procedure",
9639 proc_formal->sym->name, proc->name, &where);
9645 if (proc_formal || old_formal)
9647 gfc_error ("'%s' at %L must have the same number of formal arguments as"
9648 " the overridden procedure", proc->name, &where);
9652 /* If the overridden binding is NOPASS, the overriding one must also be
9654 if (old->n.tb->nopass && !proc->n.tb->nopass)
9656 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
9657 " NOPASS", proc->name, &where);
9661 /* If the overridden binding is PASS(x), the overriding one must also be
9662 PASS and the passed-object dummy arguments must correspond. */
9663 if (!old->n.tb->nopass)
9665 if (proc->n.tb->nopass)
9667 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
9668 " PASS", proc->name, &where);
9672 if (proc_pass_arg != old_pass_arg)
9674 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
9675 " the same position as the passed-object dummy argument of"
9676 " the overridden procedure", proc->name, &where);
9685 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
9688 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
9689 const char* generic_name, locus where)
9694 gcc_assert (t1->specific && t2->specific);
9695 gcc_assert (!t1->specific->is_generic);
9696 gcc_assert (!t2->specific->is_generic);
9698 sym1 = t1->specific->u.specific->n.sym;
9699 sym2 = t2->specific->u.specific->n.sym;
9704 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
9705 if (sym1->attr.subroutine != sym2->attr.subroutine
9706 || sym1->attr.function != sym2->attr.function)
9708 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
9709 " GENERIC '%s' at %L",
9710 sym1->name, sym2->name, generic_name, &where);
9714 /* Compare the interfaces. */
9715 if (gfc_compare_interfaces (sym1, sym2, sym2->name, 1, 0, NULL, 0))
9717 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
9718 sym1->name, sym2->name, generic_name, &where);
9726 /* Worker function for resolving a generic procedure binding; this is used to
9727 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
9729 The difference between those cases is finding possible inherited bindings
9730 that are overridden, as one has to look for them in tb_sym_root,
9731 tb_uop_root or tb_op, respectively. Thus the caller must already find
9732 the super-type and set p->overridden correctly. */
9735 resolve_tb_generic_targets (gfc_symbol* super_type,
9736 gfc_typebound_proc* p, const char* name)
9738 gfc_tbp_generic* target;
9739 gfc_symtree* first_target;
9740 gfc_symtree* inherited;
9742 gcc_assert (p && p->is_generic);
9744 /* Try to find the specific bindings for the symtrees in our target-list. */
9745 gcc_assert (p->u.generic);
9746 for (target = p->u.generic; target; target = target->next)
9747 if (!target->specific)
9749 gfc_typebound_proc* overridden_tbp;
9751 const char* target_name;
9753 target_name = target->specific_st->name;
9755 /* Defined for this type directly. */
9756 if (target->specific_st->n.tb)
9758 target->specific = target->specific_st->n.tb;
9759 goto specific_found;
9762 /* Look for an inherited specific binding. */
9765 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
9770 gcc_assert (inherited->n.tb);
9771 target->specific = inherited->n.tb;
9772 goto specific_found;
9776 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
9777 " at %L", target_name, name, &p->where);
9780 /* Once we've found the specific binding, check it is not ambiguous with
9781 other specifics already found or inherited for the same GENERIC. */
9783 gcc_assert (target->specific);
9785 /* This must really be a specific binding! */
9786 if (target->specific->is_generic)
9788 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
9789 " '%s' is GENERIC, too", name, &p->where, target_name);
9793 /* Check those already resolved on this type directly. */
9794 for (g = p->u.generic; g; g = g->next)
9795 if (g != target && g->specific
9796 && check_generic_tbp_ambiguity (target, g, name, p->where)
9800 /* Check for ambiguity with inherited specific targets. */
9801 for (overridden_tbp = p->overridden; overridden_tbp;
9802 overridden_tbp = overridden_tbp->overridden)
9803 if (overridden_tbp->is_generic)
9805 for (g = overridden_tbp->u.generic; g; g = g->next)
9807 gcc_assert (g->specific);
9808 if (check_generic_tbp_ambiguity (target, g,
9809 name, p->where) == FAILURE)
9815 /* If we attempt to "overwrite" a specific binding, this is an error. */
9816 if (p->overridden && !p->overridden->is_generic)
9818 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
9819 " the same name", name, &p->where);
9823 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
9824 all must have the same attributes here. */
9825 first_target = p->u.generic->specific->u.specific;
9826 gcc_assert (first_target);
9827 p->subroutine = first_target->n.sym->attr.subroutine;
9828 p->function = first_target->n.sym->attr.function;
9834 /* Resolve a GENERIC procedure binding for a derived type. */
9837 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
9839 gfc_symbol* super_type;
9841 /* Find the overridden binding if any. */
9842 st->n.tb->overridden = NULL;
9843 super_type = gfc_get_derived_super_type (derived);
9846 gfc_symtree* overridden;
9847 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
9850 if (overridden && overridden->n.tb)
9851 st->n.tb->overridden = overridden->n.tb;
9854 /* Resolve using worker function. */
9855 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
9859 /* Retrieve the target-procedure of an operator binding and do some checks in
9860 common for intrinsic and user-defined type-bound operators. */
9863 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
9865 gfc_symbol* target_proc;
9867 gcc_assert (target->specific && !target->specific->is_generic);
9868 target_proc = target->specific->u.specific->n.sym;
9869 gcc_assert (target_proc);
9871 /* All operator bindings must have a passed-object dummy argument. */
9872 if (target->specific->nopass)
9874 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
9882 /* Resolve a type-bound intrinsic operator. */
9885 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
9886 gfc_typebound_proc* p)
9888 gfc_symbol* super_type;
9889 gfc_tbp_generic* target;
9891 /* If there's already an error here, do nothing (but don't fail again). */
9895 /* Operators should always be GENERIC bindings. */
9896 gcc_assert (p->is_generic);
9898 /* Look for an overridden binding. */
9899 super_type = gfc_get_derived_super_type (derived);
9900 if (super_type && super_type->f2k_derived)
9901 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
9904 p->overridden = NULL;
9906 /* Resolve general GENERIC properties using worker function. */
9907 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
9910 /* Check the targets to be procedures of correct interface. */
9911 for (target = p->u.generic; target; target = target->next)
9913 gfc_symbol* target_proc;
9915 target_proc = get_checked_tb_operator_target (target, p->where);
9919 if (!gfc_check_operator_interface (target_proc, op, p->where))
9931 /* Resolve a type-bound user operator (tree-walker callback). */
9933 static gfc_symbol* resolve_bindings_derived;
9934 static gfc_try resolve_bindings_result;
9936 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
9939 resolve_typebound_user_op (gfc_symtree* stree)
9941 gfc_symbol* super_type;
9942 gfc_tbp_generic* target;
9944 gcc_assert (stree && stree->n.tb);
9946 if (stree->n.tb->error)
9949 /* Operators should always be GENERIC bindings. */
9950 gcc_assert (stree->n.tb->is_generic);
9952 /* Find overridden procedure, if any. */
9953 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
9954 if (super_type && super_type->f2k_derived)
9956 gfc_symtree* overridden;
9957 overridden = gfc_find_typebound_user_op (super_type, NULL,
9958 stree->name, true, NULL);
9960 if (overridden && overridden->n.tb)
9961 stree->n.tb->overridden = overridden->n.tb;
9964 stree->n.tb->overridden = NULL;
9966 /* Resolve basically using worker function. */
9967 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
9971 /* Check the targets to be functions of correct interface. */
9972 for (target = stree->n.tb->u.generic; target; target = target->next)
9974 gfc_symbol* target_proc;
9976 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
9980 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
9987 resolve_bindings_result = FAILURE;
9988 stree->n.tb->error = 1;
9992 /* Resolve the type-bound procedures for a derived type. */
9995 resolve_typebound_procedure (gfc_symtree* stree)
10000 gfc_symbol* super_type;
10001 gfc_component* comp;
10003 gcc_assert (stree);
10005 /* Undefined specific symbol from GENERIC target definition. */
10009 if (stree->n.tb->error)
10012 /* If this is a GENERIC binding, use that routine. */
10013 if (stree->n.tb->is_generic)
10015 if (resolve_typebound_generic (resolve_bindings_derived, stree)
10021 /* Get the target-procedure to check it. */
10022 gcc_assert (!stree->n.tb->is_generic);
10023 gcc_assert (stree->n.tb->u.specific);
10024 proc = stree->n.tb->u.specific->n.sym;
10025 where = stree->n.tb->where;
10027 /* Default access should already be resolved from the parser. */
10028 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
10030 /* It should be a module procedure or an external procedure with explicit
10031 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
10032 if ((!proc->attr.subroutine && !proc->attr.function)
10033 || (proc->attr.proc != PROC_MODULE
10034 && proc->attr.if_source != IFSRC_IFBODY)
10035 || (proc->attr.abstract && !stree->n.tb->deferred))
10037 gfc_error ("'%s' must be a module procedure or an external procedure with"
10038 " an explicit interface at %L", proc->name, &where);
10041 stree->n.tb->subroutine = proc->attr.subroutine;
10042 stree->n.tb->function = proc->attr.function;
10044 /* Find the super-type of the current derived type. We could do this once and
10045 store in a global if speed is needed, but as long as not I believe this is
10046 more readable and clearer. */
10047 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10049 /* If PASS, resolve and check arguments if not already resolved / loaded
10050 from a .mod file. */
10051 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
10053 if (stree->n.tb->pass_arg)
10055 gfc_formal_arglist* i;
10057 /* If an explicit passing argument name is given, walk the arg-list
10058 and look for it. */
10061 stree->n.tb->pass_arg_num = 1;
10062 for (i = proc->formal; i; i = i->next)
10064 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
10069 ++stree->n.tb->pass_arg_num;
10074 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
10076 proc->name, stree->n.tb->pass_arg, &where,
10077 stree->n.tb->pass_arg);
10083 /* Otherwise, take the first one; there should in fact be at least
10085 stree->n.tb->pass_arg_num = 1;
10088 gfc_error ("Procedure '%s' with PASS at %L must have at"
10089 " least one argument", proc->name, &where);
10092 me_arg = proc->formal->sym;
10095 /* Now check that the argument-type matches and the passed-object
10096 dummy argument is generally fine. */
10098 gcc_assert (me_arg);
10100 if (me_arg->ts.type != BT_CLASS)
10102 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10103 " at %L", proc->name, &where);
10107 if (me_arg->ts.u.derived->components->ts.u.derived
10108 != resolve_bindings_derived)
10110 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10111 " the derived-type '%s'", me_arg->name, proc->name,
10112 me_arg->name, &where, resolve_bindings_derived->name);
10116 gcc_assert (me_arg->ts.type == BT_CLASS);
10117 if (me_arg->ts.u.derived->components->as
10118 && me_arg->ts.u.derived->components->as->rank > 0)
10120 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
10121 " scalar", proc->name, &where);
10124 if (me_arg->ts.u.derived->components->attr.allocatable)
10126 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10127 " be ALLOCATABLE", proc->name, &where);
10130 if (me_arg->ts.u.derived->components->attr.class_pointer)
10132 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10133 " be POINTER", proc->name, &where);
10138 /* If we are extending some type, check that we don't override a procedure
10139 flagged NON_OVERRIDABLE. */
10140 stree->n.tb->overridden = NULL;
10143 gfc_symtree* overridden;
10144 overridden = gfc_find_typebound_proc (super_type, NULL,
10145 stree->name, true, NULL);
10147 if (overridden && overridden->n.tb)
10148 stree->n.tb->overridden = overridden->n.tb;
10150 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
10154 /* See if there's a name collision with a component directly in this type. */
10155 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
10156 if (!strcmp (comp->name, stree->name))
10158 gfc_error ("Procedure '%s' at %L has the same name as a component of"
10160 stree->name, &where, resolve_bindings_derived->name);
10164 /* Try to find a name collision with an inherited component. */
10165 if (super_type && gfc_find_component (super_type, stree->name, true, true))
10167 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
10168 " component of '%s'",
10169 stree->name, &where, resolve_bindings_derived->name);
10173 stree->n.tb->error = 0;
10177 resolve_bindings_result = FAILURE;
10178 stree->n.tb->error = 1;
10182 resolve_typebound_procedures (gfc_symbol* derived)
10186 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
10189 resolve_bindings_derived = derived;
10190 resolve_bindings_result = SUCCESS;
10192 if (derived->f2k_derived->tb_sym_root)
10193 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
10194 &resolve_typebound_procedure);
10196 if (derived->f2k_derived->tb_uop_root)
10197 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
10198 &resolve_typebound_user_op);
10200 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
10202 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
10203 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
10205 resolve_bindings_result = FAILURE;
10208 return resolve_bindings_result;
10212 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
10213 to give all identical derived types the same backend_decl. */
10215 add_dt_to_dt_list (gfc_symbol *derived)
10217 gfc_dt_list *dt_list;
10219 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
10220 if (derived == dt_list->derived)
10223 if (dt_list == NULL)
10225 dt_list = gfc_get_dt_list ();
10226 dt_list->next = gfc_derived_types;
10227 dt_list->derived = derived;
10228 gfc_derived_types = dt_list;
10233 /* Ensure that a derived-type is really not abstract, meaning that every
10234 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
10237 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
10242 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
10244 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
10247 if (st->n.tb && st->n.tb->deferred)
10249 gfc_symtree* overriding;
10250 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
10251 gcc_assert (overriding && overriding->n.tb);
10252 if (overriding->n.tb->deferred)
10254 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
10255 " '%s' is DEFERRED and not overridden",
10256 sub->name, &sub->declared_at, st->name);
10265 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
10267 /* The algorithm used here is to recursively travel up the ancestry of sub
10268 and for each ancestor-type, check all bindings. If any of them is
10269 DEFERRED, look it up starting from sub and see if the found (overriding)
10270 binding is not DEFERRED.
10271 This is not the most efficient way to do this, but it should be ok and is
10272 clearer than something sophisticated. */
10274 gcc_assert (ancestor && ancestor->attr.abstract && !sub->attr.abstract);
10276 /* Walk bindings of this ancestor. */
10277 if (ancestor->f2k_derived)
10280 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
10285 /* Find next ancestor type and recurse on it. */
10286 ancestor = gfc_get_derived_super_type (ancestor);
10288 return ensure_not_abstract (sub, ancestor);
10294 static void resolve_symbol (gfc_symbol *sym);
10297 /* Resolve the components of a derived type. */
10300 resolve_fl_derived (gfc_symbol *sym)
10302 gfc_symbol* super_type;
10306 super_type = gfc_get_derived_super_type (sym);
10308 /* Ensure the extended type gets resolved before we do. */
10309 if (super_type && resolve_fl_derived (super_type) == FAILURE)
10312 /* An ABSTRACT type must be extensible. */
10313 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
10315 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
10316 sym->name, &sym->declared_at);
10320 for (c = sym->components; c != NULL; c = c->next)
10322 if (c->attr.proc_pointer && c->ts.interface)
10324 if (c->ts.interface->attr.procedure)
10325 gfc_error ("Interface '%s', used by procedure pointer component "
10326 "'%s' at %L, is declared in a later PROCEDURE statement",
10327 c->ts.interface->name, c->name, &c->loc);
10329 /* Get the attributes from the interface (now resolved). */
10330 if (c->ts.interface->attr.if_source
10331 || c->ts.interface->attr.intrinsic)
10333 gfc_symbol *ifc = c->ts.interface;
10335 if (ifc->formal && !ifc->formal_ns)
10336 resolve_symbol (ifc);
10338 if (ifc->attr.intrinsic)
10339 resolve_intrinsic (ifc, &ifc->declared_at);
10343 c->ts = ifc->result->ts;
10344 c->attr.allocatable = ifc->result->attr.allocatable;
10345 c->attr.pointer = ifc->result->attr.pointer;
10346 c->attr.dimension = ifc->result->attr.dimension;
10347 c->as = gfc_copy_array_spec (ifc->result->as);
10352 c->attr.allocatable = ifc->attr.allocatable;
10353 c->attr.pointer = ifc->attr.pointer;
10354 c->attr.dimension = ifc->attr.dimension;
10355 c->as = gfc_copy_array_spec (ifc->as);
10357 c->ts.interface = ifc;
10358 c->attr.function = ifc->attr.function;
10359 c->attr.subroutine = ifc->attr.subroutine;
10360 gfc_copy_formal_args_ppc (c, ifc);
10362 c->attr.pure = ifc->attr.pure;
10363 c->attr.elemental = ifc->attr.elemental;
10364 c->attr.recursive = ifc->attr.recursive;
10365 c->attr.always_explicit = ifc->attr.always_explicit;
10366 c->attr.ext_attr |= ifc->attr.ext_attr;
10367 /* Replace symbols in array spec. */
10371 for (i = 0; i < c->as->rank; i++)
10373 gfc_expr_replace_comp (c->as->lower[i], c);
10374 gfc_expr_replace_comp (c->as->upper[i], c);
10377 /* Copy char length. */
10378 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
10380 c->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
10381 gfc_expr_replace_comp (c->ts.u.cl->length, c);
10384 else if (c->ts.interface->name[0] != '\0')
10386 gfc_error ("Interface '%s' of procedure pointer component "
10387 "'%s' at %L must be explicit", c->ts.interface->name,
10392 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
10394 /* Since PPCs are not implicitly typed, a PPC without an explicit
10395 interface must be a subroutine. */
10396 gfc_add_subroutine (&c->attr, c->name, &c->loc);
10399 /* Procedure pointer components: Check PASS arg. */
10400 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0)
10402 gfc_symbol* me_arg;
10404 if (c->tb->pass_arg)
10406 gfc_formal_arglist* i;
10408 /* If an explicit passing argument name is given, walk the arg-list
10409 and look for it. */
10412 c->tb->pass_arg_num = 1;
10413 for (i = c->formal; i; i = i->next)
10415 if (!strcmp (i->sym->name, c->tb->pass_arg))
10420 c->tb->pass_arg_num++;
10425 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
10426 "at %L has no argument '%s'", c->name,
10427 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
10434 /* Otherwise, take the first one; there should in fact be at least
10436 c->tb->pass_arg_num = 1;
10439 gfc_error ("Procedure pointer component '%s' with PASS at %L "
10440 "must have at least one argument",
10445 me_arg = c->formal->sym;
10448 /* Now check that the argument-type matches. */
10449 gcc_assert (me_arg);
10450 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
10451 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
10452 || (me_arg->ts.type == BT_CLASS
10453 && me_arg->ts.u.derived->components->ts.u.derived != sym))
10455 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10456 " the derived type '%s'", me_arg->name, c->name,
10457 me_arg->name, &c->loc, sym->name);
10462 /* Check for C453. */
10463 if (me_arg->attr.dimension)
10465 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10466 "must be scalar", me_arg->name, c->name, me_arg->name,
10472 if (me_arg->attr.pointer)
10474 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10475 "may not have the POINTER attribute", me_arg->name,
10476 c->name, me_arg->name, &c->loc);
10481 if (me_arg->attr.allocatable)
10483 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10484 "may not be ALLOCATABLE", me_arg->name, c->name,
10485 me_arg->name, &c->loc);
10490 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
10491 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10492 " at %L", c->name, &c->loc);
10496 /* Check type-spec if this is not the parent-type component. */
10497 if ((!sym->attr.extension || c != sym->components)
10498 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
10501 /* If this type is an extension, see if this component has the same name
10502 as an inherited type-bound procedure. */
10504 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
10506 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
10507 " inherited type-bound procedure",
10508 c->name, sym->name, &c->loc);
10512 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
10514 if (c->ts.u.cl->length == NULL
10515 || (resolve_charlen (c->ts.u.cl) == FAILURE)
10516 || !gfc_is_constant_expr (c->ts.u.cl->length))
10518 gfc_error ("Character length of component '%s' needs to "
10519 "be a constant specification expression at %L",
10521 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
10526 if (c->ts.type == BT_DERIVED
10527 && sym->component_access != ACCESS_PRIVATE
10528 && gfc_check_access (sym->attr.access, sym->ns->default_access)
10529 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
10530 && !c->ts.u.derived->attr.use_assoc
10531 && !gfc_check_access (c->ts.u.derived->attr.access,
10532 c->ts.u.derived->ns->default_access)
10533 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
10534 "is a PRIVATE type and cannot be a component of "
10535 "'%s', which is PUBLIC at %L", c->name,
10536 sym->name, &sym->declared_at) == FAILURE)
10539 if (sym->attr.sequence)
10541 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
10543 gfc_error ("Component %s of SEQUENCE type declared at %L does "
10544 "not have the SEQUENCE attribute",
10545 c->ts.u.derived->name, &sym->declared_at);
10550 if (c->ts.type == BT_DERIVED && c->attr.pointer
10551 && c->ts.u.derived->components == NULL
10552 && !c->ts.u.derived->attr.zero_comp)
10554 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
10555 "that has not been declared", c->name, sym->name,
10561 if (c->ts.type == BT_CLASS
10562 && !(c->ts.u.derived->components->attr.pointer
10563 || c->ts.u.derived->components->attr.allocatable))
10565 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
10566 "or pointer", c->name, &c->loc);
10570 /* Ensure that all the derived type components are put on the
10571 derived type list; even in formal namespaces, where derived type
10572 pointer components might not have been declared. */
10573 if (c->ts.type == BT_DERIVED
10575 && c->ts.u.derived->components
10577 && sym != c->ts.u.derived)
10578 add_dt_to_dt_list (c->ts.u.derived);
10580 if (c->attr.pointer || c->attr.proc_pointer || c->attr.allocatable
10584 for (i = 0; i < c->as->rank; i++)
10586 if (c->as->lower[i] == NULL
10587 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
10588 || !gfc_is_constant_expr (c->as->lower[i])
10589 || c->as->upper[i] == NULL
10590 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
10591 || !gfc_is_constant_expr (c->as->upper[i]))
10593 gfc_error ("Component '%s' of '%s' at %L must have "
10594 "constant array bounds",
10595 c->name, sym->name, &c->loc);
10601 /* Resolve the type-bound procedures. */
10602 if (resolve_typebound_procedures (sym) == FAILURE)
10605 /* Resolve the finalizer procedures. */
10606 if (gfc_resolve_finalizers (sym) == FAILURE)
10609 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
10610 all DEFERRED bindings are overridden. */
10611 if (super_type && super_type->attr.abstract && !sym->attr.abstract
10612 && ensure_not_abstract (sym, super_type) == FAILURE)
10615 /* Add derived type to the derived type list. */
10616 add_dt_to_dt_list (sym);
10623 resolve_fl_namelist (gfc_symbol *sym)
10628 /* Reject PRIVATE objects in a PUBLIC namelist. */
10629 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
10631 for (nl = sym->namelist; nl; nl = nl->next)
10633 if (!nl->sym->attr.use_assoc
10634 && !is_sym_host_assoc (nl->sym, sym->ns)
10635 && !gfc_check_access(nl->sym->attr.access,
10636 nl->sym->ns->default_access))
10638 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
10639 "cannot be member of PUBLIC namelist '%s' at %L",
10640 nl->sym->name, sym->name, &sym->declared_at);
10644 /* Types with private components that came here by USE-association. */
10645 if (nl->sym->ts.type == BT_DERIVED
10646 && derived_inaccessible (nl->sym->ts.u.derived))
10648 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
10649 "components and cannot be member of namelist '%s' at %L",
10650 nl->sym->name, sym->name, &sym->declared_at);
10654 /* Types with private components that are defined in the same module. */
10655 if (nl->sym->ts.type == BT_DERIVED
10656 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
10657 && !gfc_check_access (nl->sym->ts.u.derived->attr.private_comp
10658 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
10659 nl->sym->ns->default_access))
10661 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
10662 "cannot be a member of PUBLIC namelist '%s' at %L",
10663 nl->sym->name, sym->name, &sym->declared_at);
10669 for (nl = sym->namelist; nl; nl = nl->next)
10671 /* Reject namelist arrays of assumed shape. */
10672 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
10673 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
10674 "must not have assumed shape in namelist "
10675 "'%s' at %L", nl->sym->name, sym->name,
10676 &sym->declared_at) == FAILURE)
10679 /* Reject namelist arrays that are not constant shape. */
10680 if (is_non_constant_shape_array (nl->sym))
10682 gfc_error ("NAMELIST array object '%s' must have constant "
10683 "shape in namelist '%s' at %L", nl->sym->name,
10684 sym->name, &sym->declared_at);
10688 /* Namelist objects cannot have allocatable or pointer components. */
10689 if (nl->sym->ts.type != BT_DERIVED)
10692 if (nl->sym->ts.u.derived->attr.alloc_comp)
10694 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10695 "have ALLOCATABLE components",
10696 nl->sym->name, sym->name, &sym->declared_at);
10700 if (nl->sym->ts.u.derived->attr.pointer_comp)
10702 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10703 "have POINTER components",
10704 nl->sym->name, sym->name, &sym->declared_at);
10710 /* 14.1.2 A module or internal procedure represent local entities
10711 of the same type as a namelist member and so are not allowed. */
10712 for (nl = sym->namelist; nl; nl = nl->next)
10714 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
10717 if (nl->sym->attr.function && nl->sym == nl->sym->result)
10718 if ((nl->sym == sym->ns->proc_name)
10720 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
10724 if (nl->sym && nl->sym->name)
10725 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
10726 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
10728 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
10729 "attribute in '%s' at %L", nlsym->name,
10730 &sym->declared_at);
10740 resolve_fl_parameter (gfc_symbol *sym)
10742 /* A parameter array's shape needs to be constant. */
10743 if (sym->as != NULL
10744 && (sym->as->type == AS_DEFERRED
10745 || is_non_constant_shape_array (sym)))
10747 gfc_error ("Parameter array '%s' at %L cannot be automatic "
10748 "or of deferred shape", sym->name, &sym->declared_at);
10752 /* Make sure a parameter that has been implicitly typed still
10753 matches the implicit type, since PARAMETER statements can precede
10754 IMPLICIT statements. */
10755 if (sym->attr.implicit_type
10756 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
10759 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
10760 "later IMPLICIT type", sym->name, &sym->declared_at);
10764 /* Make sure the types of derived parameters are consistent. This
10765 type checking is deferred until resolution because the type may
10766 refer to a derived type from the host. */
10767 if (sym->ts.type == BT_DERIVED
10768 && !gfc_compare_types (&sym->ts, &sym->value->ts))
10770 gfc_error ("Incompatible derived type in PARAMETER at %L",
10771 &sym->value->where);
10778 /* Do anything necessary to resolve a symbol. Right now, we just
10779 assume that an otherwise unknown symbol is a variable. This sort
10780 of thing commonly happens for symbols in module. */
10783 resolve_symbol (gfc_symbol *sym)
10785 int check_constant, mp_flag;
10786 gfc_symtree *symtree;
10787 gfc_symtree *this_symtree;
10791 if (sym->attr.flavor == FL_UNKNOWN)
10794 /* If we find that a flavorless symbol is an interface in one of the
10795 parent namespaces, find its symtree in this namespace, free the
10796 symbol and set the symtree to point to the interface symbol. */
10797 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
10799 symtree = gfc_find_symtree (ns->sym_root, sym->name);
10800 if (symtree && symtree->n.sym->generic)
10802 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10806 gfc_free_symbol (sym);
10807 symtree->n.sym->refs++;
10808 this_symtree->n.sym = symtree->n.sym;
10813 /* Otherwise give it a flavor according to such attributes as
10815 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
10816 sym->attr.flavor = FL_VARIABLE;
10819 sym->attr.flavor = FL_PROCEDURE;
10820 if (sym->attr.dimension)
10821 sym->attr.function = 1;
10825 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
10826 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
10828 if (sym->attr.procedure && sym->ts.interface
10829 && sym->attr.if_source != IFSRC_DECL)
10831 if (sym->ts.interface == sym)
10833 gfc_error ("PROCEDURE '%s' at %L may not be used as its own "
10834 "interface", sym->name, &sym->declared_at);
10837 if (sym->ts.interface->attr.procedure)
10839 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared"
10840 " in a later PROCEDURE statement", sym->ts.interface->name,
10841 sym->name,&sym->declared_at);
10845 /* Get the attributes from the interface (now resolved). */
10846 if (sym->ts.interface->attr.if_source
10847 || sym->ts.interface->attr.intrinsic)
10849 gfc_symbol *ifc = sym->ts.interface;
10850 resolve_symbol (ifc);
10852 if (ifc->attr.intrinsic)
10853 resolve_intrinsic (ifc, &ifc->declared_at);
10856 sym->ts = ifc->result->ts;
10859 sym->ts.interface = ifc;
10860 sym->attr.function = ifc->attr.function;
10861 sym->attr.subroutine = ifc->attr.subroutine;
10862 gfc_copy_formal_args (sym, ifc);
10864 sym->attr.allocatable = ifc->attr.allocatable;
10865 sym->attr.pointer = ifc->attr.pointer;
10866 sym->attr.pure = ifc->attr.pure;
10867 sym->attr.elemental = ifc->attr.elemental;
10868 sym->attr.dimension = ifc->attr.dimension;
10869 sym->attr.recursive = ifc->attr.recursive;
10870 sym->attr.always_explicit = ifc->attr.always_explicit;
10871 sym->attr.ext_attr |= ifc->attr.ext_attr;
10872 /* Copy array spec. */
10873 sym->as = gfc_copy_array_spec (ifc->as);
10877 for (i = 0; i < sym->as->rank; i++)
10879 gfc_expr_replace_symbols (sym->as->lower[i], sym);
10880 gfc_expr_replace_symbols (sym->as->upper[i], sym);
10883 /* Copy char length. */
10884 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
10886 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
10887 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
10890 else if (sym->ts.interface->name[0] != '\0')
10892 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
10893 sym->ts.interface->name, sym->name, &sym->declared_at);
10898 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
10901 /* Symbols that are module procedures with results (functions) have
10902 the types and array specification copied for type checking in
10903 procedures that call them, as well as for saving to a module
10904 file. These symbols can't stand the scrutiny that their results
10906 mp_flag = (sym->result != NULL && sym->result != sym);
10909 /* Make sure that the intrinsic is consistent with its internal
10910 representation. This needs to be done before assigning a default
10911 type to avoid spurious warnings. */
10912 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
10913 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
10916 /* Assign default type to symbols that need one and don't have one. */
10917 if (sym->ts.type == BT_UNKNOWN)
10919 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
10920 gfc_set_default_type (sym, 1, NULL);
10922 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
10923 && !sym->attr.function && !sym->attr.subroutine
10924 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
10925 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
10927 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
10929 /* The specific case of an external procedure should emit an error
10930 in the case that there is no implicit type. */
10932 gfc_set_default_type (sym, sym->attr.external, NULL);
10935 /* Result may be in another namespace. */
10936 resolve_symbol (sym->result);
10938 if (!sym->result->attr.proc_pointer)
10940 sym->ts = sym->result->ts;
10941 sym->as = gfc_copy_array_spec (sym->result->as);
10942 sym->attr.dimension = sym->result->attr.dimension;
10943 sym->attr.pointer = sym->result->attr.pointer;
10944 sym->attr.allocatable = sym->result->attr.allocatable;
10950 /* Assumed size arrays and assumed shape arrays must be dummy
10953 if (sym->as != NULL
10954 && (sym->as->type == AS_ASSUMED_SIZE
10955 || sym->as->type == AS_ASSUMED_SHAPE)
10956 && sym->attr.dummy == 0)
10958 if (sym->as->type == AS_ASSUMED_SIZE)
10959 gfc_error ("Assumed size array at %L must be a dummy argument",
10960 &sym->declared_at);
10962 gfc_error ("Assumed shape array at %L must be a dummy argument",
10963 &sym->declared_at);
10967 /* Make sure symbols with known intent or optional are really dummy
10968 variable. Because of ENTRY statement, this has to be deferred
10969 until resolution time. */
10971 if (!sym->attr.dummy
10972 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
10974 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
10978 if (sym->attr.value && !sym->attr.dummy)
10980 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
10981 "it is not a dummy argument", sym->name, &sym->declared_at);
10985 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
10987 gfc_charlen *cl = sym->ts.u.cl;
10988 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10990 gfc_error ("Character dummy variable '%s' at %L with VALUE "
10991 "attribute must have constant length",
10992 sym->name, &sym->declared_at);
10996 if (sym->ts.is_c_interop
10997 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
10999 gfc_error ("C interoperable character dummy variable '%s' at %L "
11000 "with VALUE attribute must have length one",
11001 sym->name, &sym->declared_at);
11006 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
11007 do this for something that was implicitly typed because that is handled
11008 in gfc_set_default_type. Handle dummy arguments and procedure
11009 definitions separately. Also, anything that is use associated is not
11010 handled here but instead is handled in the module it is declared in.
11011 Finally, derived type definitions are allowed to be BIND(C) since that
11012 only implies that they're interoperable, and they are checked fully for
11013 interoperability when a variable is declared of that type. */
11014 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
11015 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
11016 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
11018 gfc_try t = SUCCESS;
11020 /* First, make sure the variable is declared at the
11021 module-level scope (J3/04-007, Section 15.3). */
11022 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
11023 sym->attr.in_common == 0)
11025 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
11026 "is neither a COMMON block nor declared at the "
11027 "module level scope", sym->name, &(sym->declared_at));
11030 else if (sym->common_head != NULL)
11032 t = verify_com_block_vars_c_interop (sym->common_head);
11036 /* If type() declaration, we need to verify that the components
11037 of the given type are all C interoperable, etc. */
11038 if (sym->ts.type == BT_DERIVED &&
11039 sym->ts.u.derived->attr.is_c_interop != 1)
11041 /* Make sure the user marked the derived type as BIND(C). If
11042 not, call the verify routine. This could print an error
11043 for the derived type more than once if multiple variables
11044 of that type are declared. */
11045 if (sym->ts.u.derived->attr.is_bind_c != 1)
11046 verify_bind_c_derived_type (sym->ts.u.derived);
11050 /* Verify the variable itself as C interoperable if it
11051 is BIND(C). It is not possible for this to succeed if
11052 the verify_bind_c_derived_type failed, so don't have to handle
11053 any error returned by verify_bind_c_derived_type. */
11054 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11055 sym->common_block);
11060 /* clear the is_bind_c flag to prevent reporting errors more than
11061 once if something failed. */
11062 sym->attr.is_bind_c = 0;
11067 /* If a derived type symbol has reached this point, without its
11068 type being declared, we have an error. Notice that most
11069 conditions that produce undefined derived types have already
11070 been dealt with. However, the likes of:
11071 implicit type(t) (t) ..... call foo (t) will get us here if
11072 the type is not declared in the scope of the implicit
11073 statement. Change the type to BT_UNKNOWN, both because it is so
11074 and to prevent an ICE. */
11075 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
11076 && !sym->ts.u.derived->attr.zero_comp)
11078 gfc_error ("The derived type '%s' at %L is of type '%s', "
11079 "which has not been defined", sym->name,
11080 &sym->declared_at, sym->ts.u.derived->name);
11081 sym->ts.type = BT_UNKNOWN;
11085 /* Make sure that the derived type has been resolved and that the
11086 derived type is visible in the symbol's namespace, if it is a
11087 module function and is not PRIVATE. */
11088 if (sym->ts.type == BT_DERIVED
11089 && sym->ts.u.derived->attr.use_assoc
11090 && sym->ns->proc_name
11091 && sym->ns->proc_name->attr.flavor == FL_MODULE)
11095 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
11098 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
11099 if (!ds && sym->attr.function
11100 && gfc_check_access (sym->attr.access, sym->ns->default_access))
11102 symtree = gfc_new_symtree (&sym->ns->sym_root,
11103 sym->ts.u.derived->name);
11104 symtree->n.sym = sym->ts.u.derived;
11105 sym->ts.u.derived->refs++;
11109 /* Unless the derived-type declaration is use associated, Fortran 95
11110 does not allow public entries of private derived types.
11111 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
11112 161 in 95-006r3. */
11113 if (sym->ts.type == BT_DERIVED
11114 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
11115 && !sym->ts.u.derived->attr.use_assoc
11116 && gfc_check_access (sym->attr.access, sym->ns->default_access)
11117 && !gfc_check_access (sym->ts.u.derived->attr.access,
11118 sym->ts.u.derived->ns->default_access)
11119 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
11120 "of PRIVATE derived type '%s'",
11121 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
11122 : "variable", sym->name, &sym->declared_at,
11123 sym->ts.u.derived->name) == FAILURE)
11126 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
11127 default initialization is defined (5.1.2.4.4). */
11128 if (sym->ts.type == BT_DERIVED
11130 && sym->attr.intent == INTENT_OUT
11132 && sym->as->type == AS_ASSUMED_SIZE)
11134 for (c = sym->ts.u.derived->components; c; c = c->next)
11136 if (c->initializer)
11138 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
11139 "ASSUMED SIZE and so cannot have a default initializer",
11140 sym->name, &sym->declared_at);
11146 switch (sym->attr.flavor)
11149 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
11154 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
11159 if (resolve_fl_namelist (sym) == FAILURE)
11164 if (resolve_fl_parameter (sym) == FAILURE)
11172 /* Resolve array specifier. Check as well some constraints
11173 on COMMON blocks. */
11175 check_constant = sym->attr.in_common && !sym->attr.pointer;
11177 /* Set the formal_arg_flag so that check_conflict will not throw
11178 an error for host associated variables in the specification
11179 expression for an array_valued function. */
11180 if (sym->attr.function && sym->as)
11181 formal_arg_flag = 1;
11183 gfc_resolve_array_spec (sym->as, check_constant);
11185 formal_arg_flag = 0;
11187 /* Resolve formal namespaces. */
11188 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
11189 && !sym->attr.contained && !sym->attr.intrinsic)
11190 gfc_resolve (sym->formal_ns);
11192 /* Make sure the formal namespace is present. */
11193 if (sym->formal && !sym->formal_ns)
11195 gfc_formal_arglist *formal = sym->formal;
11196 while (formal && !formal->sym)
11197 formal = formal->next;
11201 sym->formal_ns = formal->sym->ns;
11202 sym->formal_ns->refs++;
11206 /* Check threadprivate restrictions. */
11207 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
11208 && (!sym->attr.in_common
11209 && sym->module == NULL
11210 && (sym->ns->proc_name == NULL
11211 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
11212 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
11214 /* If we have come this far we can apply default-initializers, as
11215 described in 14.7.5, to those variables that have not already
11216 been assigned one. */
11217 if (sym->ts.type == BT_DERIVED
11218 && sym->attr.referenced
11219 && sym->ns == gfc_current_ns
11221 && !sym->attr.allocatable
11222 && !sym->attr.alloc_comp)
11224 symbol_attribute *a = &sym->attr;
11226 if ((!a->save && !a->dummy && !a->pointer
11227 && !a->in_common && !a->use_assoc
11228 && !(a->function && sym != sym->result))
11229 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
11230 apply_default_init (sym);
11233 /* If this symbol has a type-spec, check it. */
11234 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
11235 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
11236 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
11242 /************* Resolve DATA statements *************/
11246 gfc_data_value *vnode;
11252 /* Advance the values structure to point to the next value in the data list. */
11255 next_data_value (void)
11257 while (mpz_cmp_ui (values.left, 0) == 0)
11260 if (values.vnode->next == NULL)
11263 values.vnode = values.vnode->next;
11264 mpz_set (values.left, values.vnode->repeat);
11272 check_data_variable (gfc_data_variable *var, locus *where)
11278 ar_type mark = AR_UNKNOWN;
11280 mpz_t section_index[GFC_MAX_DIMENSIONS];
11286 if (gfc_resolve_expr (var->expr) == FAILURE)
11290 mpz_init_set_si (offset, 0);
11293 if (e->expr_type != EXPR_VARIABLE)
11294 gfc_internal_error ("check_data_variable(): Bad expression");
11296 sym = e->symtree->n.sym;
11298 if (sym->ns->is_block_data && !sym->attr.in_common)
11300 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
11301 sym->name, &sym->declared_at);
11304 if (e->ref == NULL && sym->as)
11306 gfc_error ("DATA array '%s' at %L must be specified in a previous"
11307 " declaration", sym->name, where);
11311 has_pointer = sym->attr.pointer;
11313 for (ref = e->ref; ref; ref = ref->next)
11315 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
11319 && ref->type == REF_ARRAY
11320 && ref->u.ar.type != AR_FULL)
11322 gfc_error ("DATA element '%s' at %L is a pointer and so must "
11323 "be a full array", sym->name, where);
11328 if (e->rank == 0 || has_pointer)
11330 mpz_init_set_ui (size, 1);
11337 /* Find the array section reference. */
11338 for (ref = e->ref; ref; ref = ref->next)
11340 if (ref->type != REF_ARRAY)
11342 if (ref->u.ar.type == AR_ELEMENT)
11348 /* Set marks according to the reference pattern. */
11349 switch (ref->u.ar.type)
11357 /* Get the start position of array section. */
11358 gfc_get_section_index (ar, section_index, &offset);
11363 gcc_unreachable ();
11366 if (gfc_array_size (e, &size) == FAILURE)
11368 gfc_error ("Nonconstant array section at %L in DATA statement",
11370 mpz_clear (offset);
11377 while (mpz_cmp_ui (size, 0) > 0)
11379 if (next_data_value () == FAILURE)
11381 gfc_error ("DATA statement at %L has more variables than values",
11387 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
11391 /* If we have more than one element left in the repeat count,
11392 and we have more than one element left in the target variable,
11393 then create a range assignment. */
11394 /* FIXME: Only done for full arrays for now, since array sections
11396 if (mark == AR_FULL && ref && ref->next == NULL
11397 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
11401 if (mpz_cmp (size, values.left) >= 0)
11403 mpz_init_set (range, values.left);
11404 mpz_sub (size, size, values.left);
11405 mpz_set_ui (values.left, 0);
11409 mpz_init_set (range, size);
11410 mpz_sub (values.left, values.left, size);
11411 mpz_set_ui (size, 0);
11414 gfc_assign_data_value_range (var->expr, values.vnode->expr,
11417 mpz_add (offset, offset, range);
11421 /* Assign initial value to symbol. */
11424 mpz_sub_ui (values.left, values.left, 1);
11425 mpz_sub_ui (size, size, 1);
11427 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
11431 if (mark == AR_FULL)
11432 mpz_add_ui (offset, offset, 1);
11434 /* Modify the array section indexes and recalculate the offset
11435 for next element. */
11436 else if (mark == AR_SECTION)
11437 gfc_advance_section (section_index, ar, &offset);
11441 if (mark == AR_SECTION)
11443 for (i = 0; i < ar->dimen; i++)
11444 mpz_clear (section_index[i]);
11448 mpz_clear (offset);
11454 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
11456 /* Iterate over a list of elements in a DATA statement. */
11459 traverse_data_list (gfc_data_variable *var, locus *where)
11462 iterator_stack frame;
11463 gfc_expr *e, *start, *end, *step;
11464 gfc_try retval = SUCCESS;
11466 mpz_init (frame.value);
11468 start = gfc_copy_expr (var->iter.start);
11469 end = gfc_copy_expr (var->iter.end);
11470 step = gfc_copy_expr (var->iter.step);
11472 if (gfc_simplify_expr (start, 1) == FAILURE
11473 || start->expr_type != EXPR_CONSTANT)
11475 gfc_error ("iterator start at %L does not simplify", &start->where);
11479 if (gfc_simplify_expr (end, 1) == FAILURE
11480 || end->expr_type != EXPR_CONSTANT)
11482 gfc_error ("iterator end at %L does not simplify", &end->where);
11486 if (gfc_simplify_expr (step, 1) == FAILURE
11487 || step->expr_type != EXPR_CONSTANT)
11489 gfc_error ("iterator step at %L does not simplify", &step->where);
11494 mpz_init_set (trip, end->value.integer);
11495 mpz_sub (trip, trip, start->value.integer);
11496 mpz_add (trip, trip, step->value.integer);
11498 mpz_div (trip, trip, step->value.integer);
11500 mpz_set (frame.value, start->value.integer);
11502 frame.prev = iter_stack;
11503 frame.variable = var->iter.var->symtree;
11504 iter_stack = &frame;
11506 while (mpz_cmp_ui (trip, 0) > 0)
11508 if (traverse_data_var (var->list, where) == FAILURE)
11515 e = gfc_copy_expr (var->expr);
11516 if (gfc_simplify_expr (e, 1) == FAILURE)
11524 mpz_add (frame.value, frame.value, step->value.integer);
11526 mpz_sub_ui (trip, trip, 1);
11531 mpz_clear (frame.value);
11533 gfc_free_expr (start);
11534 gfc_free_expr (end);
11535 gfc_free_expr (step);
11537 iter_stack = frame.prev;
11542 /* Type resolve variables in the variable list of a DATA statement. */
11545 traverse_data_var (gfc_data_variable *var, locus *where)
11549 for (; var; var = var->next)
11551 if (var->expr == NULL)
11552 t = traverse_data_list (var, where);
11554 t = check_data_variable (var, where);
11564 /* Resolve the expressions and iterators associated with a data statement.
11565 This is separate from the assignment checking because data lists should
11566 only be resolved once. */
11569 resolve_data_variables (gfc_data_variable *d)
11571 for (; d; d = d->next)
11573 if (d->list == NULL)
11575 if (gfc_resolve_expr (d->expr) == FAILURE)
11580 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
11583 if (resolve_data_variables (d->list) == FAILURE)
11592 /* Resolve a single DATA statement. We implement this by storing a pointer to
11593 the value list into static variables, and then recursively traversing the
11594 variables list, expanding iterators and such. */
11597 resolve_data (gfc_data *d)
11600 if (resolve_data_variables (d->var) == FAILURE)
11603 values.vnode = d->value;
11604 if (d->value == NULL)
11605 mpz_set_ui (values.left, 0);
11607 mpz_set (values.left, d->value->repeat);
11609 if (traverse_data_var (d->var, &d->where) == FAILURE)
11612 /* At this point, we better not have any values left. */
11614 if (next_data_value () == SUCCESS)
11615 gfc_error ("DATA statement at %L has more values than variables",
11620 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
11621 accessed by host or use association, is a dummy argument to a pure function,
11622 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
11623 is storage associated with any such variable, shall not be used in the
11624 following contexts: (clients of this function). */
11626 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
11627 procedure. Returns zero if assignment is OK, nonzero if there is a
11630 gfc_impure_variable (gfc_symbol *sym)
11634 if (sym->attr.use_assoc || sym->attr.in_common)
11637 if (sym->ns != gfc_current_ns)
11638 return !sym->attr.function;
11640 proc = sym->ns->proc_name;
11641 if (sym->attr.dummy && gfc_pure (proc)
11642 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
11644 proc->attr.function))
11647 /* TODO: Sort out what can be storage associated, if anything, and include
11648 it here. In principle equivalences should be scanned but it does not
11649 seem to be possible to storage associate an impure variable this way. */
11654 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
11655 symbol of the current procedure. */
11658 gfc_pure (gfc_symbol *sym)
11660 symbol_attribute attr;
11663 sym = gfc_current_ns->proc_name;
11669 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
11673 /* Test whether the current procedure is elemental or not. */
11676 gfc_elemental (gfc_symbol *sym)
11678 symbol_attribute attr;
11681 sym = gfc_current_ns->proc_name;
11686 return attr.flavor == FL_PROCEDURE && attr.elemental;
11690 /* Warn about unused labels. */
11693 warn_unused_fortran_label (gfc_st_label *label)
11698 warn_unused_fortran_label (label->left);
11700 if (label->defined == ST_LABEL_UNKNOWN)
11703 switch (label->referenced)
11705 case ST_LABEL_UNKNOWN:
11706 gfc_warning ("Label %d at %L defined but not used", label->value,
11710 case ST_LABEL_BAD_TARGET:
11711 gfc_warning ("Label %d at %L defined but cannot be used",
11712 label->value, &label->where);
11719 warn_unused_fortran_label (label->right);
11723 /* Returns the sequence type of a symbol or sequence. */
11726 sequence_type (gfc_typespec ts)
11735 if (ts.u.derived->components == NULL)
11736 return SEQ_NONDEFAULT;
11738 result = sequence_type (ts.u.derived->components->ts);
11739 for (c = ts.u.derived->components->next; c; c = c->next)
11740 if (sequence_type (c->ts) != result)
11746 if (ts.kind != gfc_default_character_kind)
11747 return SEQ_NONDEFAULT;
11749 return SEQ_CHARACTER;
11752 if (ts.kind != gfc_default_integer_kind)
11753 return SEQ_NONDEFAULT;
11755 return SEQ_NUMERIC;
11758 if (!(ts.kind == gfc_default_real_kind
11759 || ts.kind == gfc_default_double_kind))
11760 return SEQ_NONDEFAULT;
11762 return SEQ_NUMERIC;
11765 if (ts.kind != gfc_default_complex_kind)
11766 return SEQ_NONDEFAULT;
11768 return SEQ_NUMERIC;
11771 if (ts.kind != gfc_default_logical_kind)
11772 return SEQ_NONDEFAULT;
11774 return SEQ_NUMERIC;
11777 return SEQ_NONDEFAULT;
11782 /* Resolve derived type EQUIVALENCE object. */
11785 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
11787 gfc_component *c = derived->components;
11792 /* Shall not be an object of nonsequence derived type. */
11793 if (!derived->attr.sequence)
11795 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
11796 "attribute to be an EQUIVALENCE object", sym->name,
11801 /* Shall not have allocatable components. */
11802 if (derived->attr.alloc_comp)
11804 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
11805 "components to be an EQUIVALENCE object",sym->name,
11810 if (sym->attr.in_common && has_default_initializer (sym->ts.u.derived))
11812 gfc_error ("Derived type variable '%s' at %L with default "
11813 "initialization cannot be in EQUIVALENCE with a variable "
11814 "in COMMON", sym->name, &e->where);
11818 for (; c ; c = c->next)
11820 if (c->ts.type == BT_DERIVED
11821 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
11824 /* Shall not be an object of sequence derived type containing a pointer
11825 in the structure. */
11826 if (c->attr.pointer)
11828 gfc_error ("Derived type variable '%s' at %L with pointer "
11829 "component(s) cannot be an EQUIVALENCE object",
11830 sym->name, &e->where);
11838 /* Resolve equivalence object.
11839 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
11840 an allocatable array, an object of nonsequence derived type, an object of
11841 sequence derived type containing a pointer at any level of component
11842 selection, an automatic object, a function name, an entry name, a result
11843 name, a named constant, a structure component, or a subobject of any of
11844 the preceding objects. A substring shall not have length zero. A
11845 derived type shall not have components with default initialization nor
11846 shall two objects of an equivalence group be initialized.
11847 Either all or none of the objects shall have an protected attribute.
11848 The simple constraints are done in symbol.c(check_conflict) and the rest
11849 are implemented here. */
11852 resolve_equivalence (gfc_equiv *eq)
11855 gfc_symbol *first_sym;
11858 locus *last_where = NULL;
11859 seq_type eq_type, last_eq_type;
11860 gfc_typespec *last_ts;
11861 int object, cnt_protected;
11864 last_ts = &eq->expr->symtree->n.sym->ts;
11866 first_sym = eq->expr->symtree->n.sym;
11870 for (object = 1; eq; eq = eq->eq, object++)
11874 e->ts = e->symtree->n.sym->ts;
11875 /* match_varspec might not know yet if it is seeing
11876 array reference or substring reference, as it doesn't
11878 if (e->ref && e->ref->type == REF_ARRAY)
11880 gfc_ref *ref = e->ref;
11881 sym = e->symtree->n.sym;
11883 if (sym->attr.dimension)
11885 ref->u.ar.as = sym->as;
11889 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
11890 if (e->ts.type == BT_CHARACTER
11892 && ref->type == REF_ARRAY
11893 && ref->u.ar.dimen == 1
11894 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
11895 && ref->u.ar.stride[0] == NULL)
11897 gfc_expr *start = ref->u.ar.start[0];
11898 gfc_expr *end = ref->u.ar.end[0];
11901 /* Optimize away the (:) reference. */
11902 if (start == NULL && end == NULL)
11905 e->ref = ref->next;
11907 e->ref->next = ref->next;
11912 ref->type = REF_SUBSTRING;
11914 start = gfc_int_expr (1);
11915 ref->u.ss.start = start;
11916 if (end == NULL && e->ts.u.cl)
11917 end = gfc_copy_expr (e->ts.u.cl->length);
11918 ref->u.ss.end = end;
11919 ref->u.ss.length = e->ts.u.cl;
11926 /* Any further ref is an error. */
11929 gcc_assert (ref->type == REF_ARRAY);
11930 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
11936 if (gfc_resolve_expr (e) == FAILURE)
11939 sym = e->symtree->n.sym;
11941 if (sym->attr.is_protected)
11943 if (cnt_protected > 0 && cnt_protected != object)
11945 gfc_error ("Either all or none of the objects in the "
11946 "EQUIVALENCE set at %L shall have the "
11947 "PROTECTED attribute",
11952 /* Shall not equivalence common block variables in a PURE procedure. */
11953 if (sym->ns->proc_name
11954 && sym->ns->proc_name->attr.pure
11955 && sym->attr.in_common)
11957 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
11958 "object in the pure procedure '%s'",
11959 sym->name, &e->where, sym->ns->proc_name->name);
11963 /* Shall not be a named constant. */
11964 if (e->expr_type == EXPR_CONSTANT)
11966 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
11967 "object", sym->name, &e->where);
11971 if (e->ts.type == BT_DERIVED
11972 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
11975 /* Check that the types correspond correctly:
11977 A numeric sequence structure may be equivalenced to another sequence
11978 structure, an object of default integer type, default real type, double
11979 precision real type, default logical type such that components of the
11980 structure ultimately only become associated to objects of the same
11981 kind. A character sequence structure may be equivalenced to an object
11982 of default character kind or another character sequence structure.
11983 Other objects may be equivalenced only to objects of the same type and
11984 kind parameters. */
11986 /* Identical types are unconditionally OK. */
11987 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
11988 goto identical_types;
11990 last_eq_type = sequence_type (*last_ts);
11991 eq_type = sequence_type (sym->ts);
11993 /* Since the pair of objects is not of the same type, mixed or
11994 non-default sequences can be rejected. */
11996 msg = "Sequence %s with mixed components in EQUIVALENCE "
11997 "statement at %L with different type objects";
11999 && last_eq_type == SEQ_MIXED
12000 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
12002 || (eq_type == SEQ_MIXED
12003 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12004 &e->where) == FAILURE))
12007 msg = "Non-default type object or sequence %s in EQUIVALENCE "
12008 "statement at %L with objects of different type";
12010 && last_eq_type == SEQ_NONDEFAULT
12011 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
12012 last_where) == FAILURE)
12013 || (eq_type == SEQ_NONDEFAULT
12014 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12015 &e->where) == FAILURE))
12018 msg ="Non-CHARACTER object '%s' in default CHARACTER "
12019 "EQUIVALENCE statement at %L";
12020 if (last_eq_type == SEQ_CHARACTER
12021 && eq_type != SEQ_CHARACTER
12022 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12023 &e->where) == FAILURE)
12026 msg ="Non-NUMERIC object '%s' in default NUMERIC "
12027 "EQUIVALENCE statement at %L";
12028 if (last_eq_type == SEQ_NUMERIC
12029 && eq_type != SEQ_NUMERIC
12030 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12031 &e->where) == FAILURE)
12036 last_where = &e->where;
12041 /* Shall not be an automatic array. */
12042 if (e->ref->type == REF_ARRAY
12043 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
12045 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
12046 "an EQUIVALENCE object", sym->name, &e->where);
12053 /* Shall not be a structure component. */
12054 if (r->type == REF_COMPONENT)
12056 gfc_error ("Structure component '%s' at %L cannot be an "
12057 "EQUIVALENCE object",
12058 r->u.c.component->name, &e->where);
12062 /* A substring shall not have length zero. */
12063 if (r->type == REF_SUBSTRING)
12065 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
12067 gfc_error ("Substring at %L has length zero",
12068 &r->u.ss.start->where);
12078 /* Resolve function and ENTRY types, issue diagnostics if needed. */
12081 resolve_fntype (gfc_namespace *ns)
12083 gfc_entry_list *el;
12086 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
12089 /* If there are any entries, ns->proc_name is the entry master
12090 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
12092 sym = ns->entries->sym;
12094 sym = ns->proc_name;
12095 if (sym->result == sym
12096 && sym->ts.type == BT_UNKNOWN
12097 && gfc_set_default_type (sym, 0, NULL) == FAILURE
12098 && !sym->attr.untyped)
12100 gfc_error ("Function '%s' at %L has no IMPLICIT type",
12101 sym->name, &sym->declared_at);
12102 sym->attr.untyped = 1;
12105 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
12106 && !sym->attr.contained
12107 && !gfc_check_access (sym->ts.u.derived->attr.access,
12108 sym->ts.u.derived->ns->default_access)
12109 && gfc_check_access (sym->attr.access, sym->ns->default_access))
12111 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
12112 "%L of PRIVATE type '%s'", sym->name,
12113 &sym->declared_at, sym->ts.u.derived->name);
12117 for (el = ns->entries->next; el; el = el->next)
12119 if (el->sym->result == el->sym
12120 && el->sym->ts.type == BT_UNKNOWN
12121 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
12122 && !el->sym->attr.untyped)
12124 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
12125 el->sym->name, &el->sym->declared_at);
12126 el->sym->attr.untyped = 1;
12132 /* 12.3.2.1.1 Defined operators. */
12135 check_uop_procedure (gfc_symbol *sym, locus where)
12137 gfc_formal_arglist *formal;
12139 if (!sym->attr.function)
12141 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
12142 sym->name, &where);
12146 if (sym->ts.type == BT_CHARACTER
12147 && !(sym->ts.u.cl && sym->ts.u.cl->length)
12148 && !(sym->result && sym->result->ts.u.cl
12149 && sym->result->ts.u.cl->length))
12151 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
12152 "character length", sym->name, &where);
12156 formal = sym->formal;
12157 if (!formal || !formal->sym)
12159 gfc_error ("User operator procedure '%s' at %L must have at least "
12160 "one argument", sym->name, &where);
12164 if (formal->sym->attr.intent != INTENT_IN)
12166 gfc_error ("First argument of operator interface at %L must be "
12167 "INTENT(IN)", &where);
12171 if (formal->sym->attr.optional)
12173 gfc_error ("First argument of operator interface at %L cannot be "
12174 "optional", &where);
12178 formal = formal->next;
12179 if (!formal || !formal->sym)
12182 if (formal->sym->attr.intent != INTENT_IN)
12184 gfc_error ("Second argument of operator interface at %L must be "
12185 "INTENT(IN)", &where);
12189 if (formal->sym->attr.optional)
12191 gfc_error ("Second argument of operator interface at %L cannot be "
12192 "optional", &where);
12198 gfc_error ("Operator interface at %L must have, at most, two "
12199 "arguments", &where);
12207 gfc_resolve_uops (gfc_symtree *symtree)
12209 gfc_interface *itr;
12211 if (symtree == NULL)
12214 gfc_resolve_uops (symtree->left);
12215 gfc_resolve_uops (symtree->right);
12217 for (itr = symtree->n.uop->op; itr; itr = itr->next)
12218 check_uop_procedure (itr->sym, itr->sym->declared_at);
12222 /* Examine all of the expressions associated with a program unit,
12223 assign types to all intermediate expressions, make sure that all
12224 assignments are to compatible types and figure out which names
12225 refer to which functions or subroutines. It doesn't check code
12226 block, which is handled by resolve_code. */
12229 resolve_types (gfc_namespace *ns)
12235 gfc_namespace* old_ns = gfc_current_ns;
12237 /* Check that all IMPLICIT types are ok. */
12238 if (!ns->seen_implicit_none)
12241 for (letter = 0; letter != GFC_LETTERS; ++letter)
12242 if (ns->set_flag[letter]
12243 && resolve_typespec_used (&ns->default_type[letter],
12244 &ns->implicit_loc[letter],
12249 gfc_current_ns = ns;
12251 resolve_entries (ns);
12253 resolve_common_vars (ns->blank_common.head, false);
12254 resolve_common_blocks (ns->common_root);
12256 resolve_contained_functions (ns);
12258 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
12260 for (cl = ns->cl_list; cl; cl = cl->next)
12261 resolve_charlen (cl);
12263 gfc_traverse_ns (ns, resolve_symbol);
12265 resolve_fntype (ns);
12267 for (n = ns->contained; n; n = n->sibling)
12269 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
12270 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
12271 "also be PURE", n->proc_name->name,
12272 &n->proc_name->declared_at);
12278 gfc_check_interfaces (ns);
12280 gfc_traverse_ns (ns, resolve_values);
12286 for (d = ns->data; d; d = d->next)
12290 gfc_traverse_ns (ns, gfc_formalize_init_value);
12292 gfc_traverse_ns (ns, gfc_verify_binding_labels);
12294 if (ns->common_root != NULL)
12295 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
12297 for (eq = ns->equiv; eq; eq = eq->next)
12298 resolve_equivalence (eq);
12300 /* Warn about unused labels. */
12301 if (warn_unused_label)
12302 warn_unused_fortran_label (ns->st_labels);
12304 gfc_resolve_uops (ns->uop_root);
12306 gfc_current_ns = old_ns;
12310 /* Call resolve_code recursively. */
12313 resolve_codes (gfc_namespace *ns)
12316 bitmap_obstack old_obstack;
12318 for (n = ns->contained; n; n = n->sibling)
12321 gfc_current_ns = ns;
12323 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
12324 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
12327 /* Set to an out of range value. */
12328 current_entry_id = -1;
12330 old_obstack = labels_obstack;
12331 bitmap_obstack_initialize (&labels_obstack);
12333 resolve_code (ns->code, ns);
12335 bitmap_obstack_release (&labels_obstack);
12336 labels_obstack = old_obstack;
12340 /* This function is called after a complete program unit has been compiled.
12341 Its purpose is to examine all of the expressions associated with a program
12342 unit, assign types to all intermediate expressions, make sure that all
12343 assignments are to compatible types and figure out which names refer to
12344 which functions or subroutines. */
12347 gfc_resolve (gfc_namespace *ns)
12349 gfc_namespace *old_ns;
12350 code_stack *old_cs_base;
12356 old_ns = gfc_current_ns;
12357 old_cs_base = cs_base;
12359 resolve_types (ns);
12360 resolve_codes (ns);
12362 gfc_current_ns = old_ns;
12363 cs_base = old_cs_base;