1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
31 #include "target-memory.h" /* for gfc_simplify_transfer */
33 /* Types used in equivalence statements. */
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
41 /* Stack to keep track of the nesting of blocks as we move through the
42 code. See resolve_branch() and resolve_code(). */
44 typedef struct code_stack
46 struct gfc_code *head, *current;
47 struct code_stack *prev;
49 /* This bitmap keeps track of the targets valid for a branch from
50 inside this block except for END {IF|SELECT}s of enclosing
52 bitmap reachable_labels;
56 static code_stack *cs_base = NULL;
59 /* Nonzero if we're inside a FORALL block. */
61 static int forall_flag;
63 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
65 static int omp_workshare_flag;
67 /* Nonzero if we are processing a formal arglist. The corresponding function
68 resets the flag each time that it is read. */
69 static int formal_arg_flag = 0;
71 /* True if we are resolving a specification expression. */
72 static int specification_expr = 0;
74 /* The id of the last entry seen. */
75 static int current_entry_id;
77 /* We use bitmaps to determine if a branch target is valid. */
78 static bitmap_obstack labels_obstack;
81 gfc_is_formal_arg (void)
83 return formal_arg_flag;
86 /* Is the symbol host associated? */
88 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
90 for (ns = ns->parent; ns; ns = ns->parent)
99 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
100 an ABSTRACT derived-type. If where is not NULL, an error message with that
101 locus is printed, optionally using name. */
104 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
106 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
111 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
112 name, where, ts->u.derived->name);
114 gfc_error ("ABSTRACT type '%s' used at %L",
115 ts->u.derived->name, where);
125 /* Resolve types of formal argument lists. These have to be done early so that
126 the formal argument lists of module procedures can be copied to the
127 containing module before the individual procedures are resolved
128 individually. We also resolve argument lists of procedures in interface
129 blocks because they are self-contained scoping units.
131 Since a dummy argument cannot be a non-dummy procedure, the only
132 resort left for untyped names are the IMPLICIT types. */
135 resolve_formal_arglist (gfc_symbol *proc)
137 gfc_formal_arglist *f;
141 if (proc->result != NULL)
146 if (gfc_elemental (proc)
147 || sym->attr.pointer || sym->attr.allocatable
148 || (sym->as && sym->as->rank > 0))
150 proc->attr.always_explicit = 1;
151 sym->attr.always_explicit = 1;
156 for (f = proc->formal; f; f = f->next)
162 /* Alternate return placeholder. */
163 if (gfc_elemental (proc))
164 gfc_error ("Alternate return specifier in elemental subroutine "
165 "'%s' at %L is not allowed", proc->name,
167 if (proc->attr.function)
168 gfc_error ("Alternate return specifier in function "
169 "'%s' at %L is not allowed", proc->name,
174 if (sym->attr.if_source != IFSRC_UNKNOWN)
175 resolve_formal_arglist (sym);
177 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
179 if (gfc_pure (proc) && !gfc_pure (sym))
181 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
182 "also be PURE", sym->name, &sym->declared_at);
186 if (gfc_elemental (proc))
188 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
189 "procedure", &sym->declared_at);
193 if (sym->attr.function
194 && sym->ts.type == BT_UNKNOWN
195 && sym->attr.intrinsic)
197 gfc_intrinsic_sym *isym;
198 isym = gfc_find_function (sym->name);
199 if (isym == NULL || !isym->specific)
201 gfc_error ("Unable to find a specific INTRINSIC procedure "
202 "for the reference '%s' at %L", sym->name,
211 if (sym->ts.type == BT_UNKNOWN)
213 if (!sym->attr.function || sym->result == sym)
214 gfc_set_default_type (sym, 1, sym->ns);
217 gfc_resolve_array_spec (sym->as, 0);
219 /* We can't tell if an array with dimension (:) is assumed or deferred
220 shape until we know if it has the pointer or allocatable attributes.
222 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
223 && !(sym->attr.pointer || sym->attr.allocatable))
225 sym->as->type = AS_ASSUMED_SHAPE;
226 for (i = 0; i < sym->as->rank; i++)
227 sym->as->lower[i] = gfc_int_expr (1);
230 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
231 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
232 || sym->attr.optional)
234 proc->attr.always_explicit = 1;
236 proc->result->attr.always_explicit = 1;
239 /* If the flavor is unknown at this point, it has to be a variable.
240 A procedure specification would have already set the type. */
242 if (sym->attr.flavor == FL_UNKNOWN)
243 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
245 if (gfc_pure (proc) && !sym->attr.pointer
246 && sym->attr.flavor != FL_PROCEDURE)
248 if (proc->attr.function && sym->attr.intent != INTENT_IN)
249 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
250 "INTENT(IN)", sym->name, proc->name,
253 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
254 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
255 "have its INTENT specified", sym->name, proc->name,
259 if (gfc_elemental (proc))
263 gfc_error ("Argument '%s' of elemental procedure at %L must "
264 "be scalar", sym->name, &sym->declared_at);
268 if (sym->attr.pointer)
270 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
271 "have the POINTER attribute", sym->name,
276 if (sym->attr.flavor == FL_PROCEDURE)
278 gfc_error ("Dummy procedure '%s' not allowed in elemental "
279 "procedure '%s' at %L", sym->name, proc->name,
285 /* Each dummy shall be specified to be scalar. */
286 if (proc->attr.proc == PROC_ST_FUNCTION)
290 gfc_error ("Argument '%s' of statement function at %L must "
291 "be scalar", sym->name, &sym->declared_at);
295 if (sym->ts.type == BT_CHARACTER)
297 gfc_charlen *cl = sym->ts.u.cl;
298 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
300 gfc_error ("Character-valued argument '%s' of statement "
301 "function at %L must have constant length",
302 sym->name, &sym->declared_at);
312 /* Work function called when searching for symbols that have argument lists
313 associated with them. */
316 find_arglists (gfc_symbol *sym)
318 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
321 resolve_formal_arglist (sym);
325 /* Given a namespace, resolve all formal argument lists within the namespace.
329 resolve_formal_arglists (gfc_namespace *ns)
334 gfc_traverse_ns (ns, find_arglists);
339 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
343 /* If this namespace is not a function or an entry master function,
345 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
346 || sym->attr.entry_master)
349 /* Try to find out of what the return type is. */
350 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
352 t = gfc_set_default_type (sym->result, 0, ns);
354 if (t == FAILURE && !sym->result->attr.untyped)
356 if (sym->result == sym)
357 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
358 sym->name, &sym->declared_at);
359 else if (!sym->result->attr.proc_pointer)
360 gfc_error ("Result '%s' of contained function '%s' at %L has "
361 "no IMPLICIT type", sym->result->name, sym->name,
362 &sym->result->declared_at);
363 sym->result->attr.untyped = 1;
367 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
368 type, lists the only ways a character length value of * can be used:
369 dummy arguments of procedures, named constants, and function results
370 in external functions. Internal function results 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 ||(sym->attr.function && gfc_current_ns->proc_name == sym))
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 && cons->expr != NULL)
847 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
848 expr->ts.u.derived->name, &(expr->where));
852 for (; comp; comp = comp->next, cons = cons->next)
859 if (gfc_resolve_expr (cons->expr) == FAILURE)
865 rank = comp->as ? comp->as->rank : 0;
866 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
867 && (comp->attr.allocatable || cons->expr->rank))
869 gfc_error ("The rank of the element in the derived type "
870 "constructor at %L does not match that of the "
871 "component (%d/%d)", &cons->expr->where,
872 cons->expr->rank, rank);
876 /* If we don't have the right type, try to convert it. */
878 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
881 if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
882 gfc_error ("The element in the derived type constructor at %L, "
883 "for pointer component '%s', is %s but should be %s",
884 &cons->expr->where, comp->name,
885 gfc_basic_typename (cons->expr->ts.type),
886 gfc_basic_typename (comp->ts.type));
888 t = gfc_convert_type (cons->expr, &comp->ts, 1);
891 if (cons->expr->expr_type == EXPR_NULL
892 && !(comp->attr.pointer || comp->attr.allocatable
893 || comp->attr.proc_pointer
894 || (comp->ts.type == BT_CLASS
895 && (comp->ts.u.derived->components->attr.pointer
896 || comp->ts.u.derived->components->attr.allocatable))))
899 gfc_error ("The NULL in the derived type constructor at %L is "
900 "being applied to component '%s', which is neither "
901 "a POINTER nor ALLOCATABLE", &cons->expr->where,
905 if (!comp->attr.pointer || cons->expr->expr_type == EXPR_NULL)
908 a = gfc_expr_attr (cons->expr);
910 if (!a.pointer && !a.target)
913 gfc_error ("The element in the derived type constructor at %L, "
914 "for pointer component '%s' should be a POINTER or "
915 "a TARGET", &cons->expr->where, comp->name);
923 /****************** Expression name resolution ******************/
925 /* Returns 0 if a symbol was not declared with a type or
926 attribute declaration statement, nonzero otherwise. */
929 was_declared (gfc_symbol *sym)
935 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
938 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
939 || a.optional || a.pointer || a.save || a.target || a.volatile_
940 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
947 /* Determine if a symbol is generic or not. */
950 generic_sym (gfc_symbol *sym)
954 if (sym->attr.generic ||
955 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
958 if (was_declared (sym) || sym->ns->parent == NULL)
961 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
968 return generic_sym (s);
975 /* Determine if a symbol is specific or not. */
978 specific_sym (gfc_symbol *sym)
982 if (sym->attr.if_source == IFSRC_IFBODY
983 || sym->attr.proc == PROC_MODULE
984 || sym->attr.proc == PROC_INTERNAL
985 || sym->attr.proc == PROC_ST_FUNCTION
986 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
987 || sym->attr.external)
990 if (was_declared (sym) || sym->ns->parent == NULL)
993 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
995 return (s == NULL) ? 0 : specific_sym (s);
999 /* Figure out if the procedure is specific, generic or unknown. */
1002 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1006 procedure_kind (gfc_symbol *sym)
1008 if (generic_sym (sym))
1009 return PTYPE_GENERIC;
1011 if (specific_sym (sym))
1012 return PTYPE_SPECIFIC;
1014 return PTYPE_UNKNOWN;
1017 /* Check references to assumed size arrays. The flag need_full_assumed_size
1018 is nonzero when matching actual arguments. */
1020 static int need_full_assumed_size = 0;
1023 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1025 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1028 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1029 What should it be? */
1030 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1031 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1032 && (e->ref->u.ar.type == AR_FULL))
1034 gfc_error ("The upper bound in the last dimension must "
1035 "appear in the reference to the assumed size "
1036 "array '%s' at %L", sym->name, &e->where);
1043 /* Look for bad assumed size array references in argument expressions
1044 of elemental and array valued intrinsic procedures. Since this is
1045 called from procedure resolution functions, it only recurses at
1049 resolve_assumed_size_actual (gfc_expr *e)
1054 switch (e->expr_type)
1057 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1062 if (resolve_assumed_size_actual (e->value.op.op1)
1063 || resolve_assumed_size_actual (e->value.op.op2))
1074 /* Check a generic procedure, passed as an actual argument, to see if
1075 there is a matching specific name. If none, it is an error, and if
1076 more than one, the reference is ambiguous. */
1078 count_specific_procs (gfc_expr *e)
1085 sym = e->symtree->n.sym;
1087 for (p = sym->generic; p; p = p->next)
1088 if (strcmp (sym->name, p->sym->name) == 0)
1090 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1096 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1100 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1101 "argument at %L", sym->name, &e->where);
1107 /* See if a call to sym could possibly be a not allowed RECURSION because of
1108 a missing RECURIVE declaration. This means that either sym is the current
1109 context itself, or sym is the parent of a contained procedure calling its
1110 non-RECURSIVE containing procedure.
1111 This also works if sym is an ENTRY. */
1114 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1116 gfc_symbol* proc_sym;
1117 gfc_symbol* context_proc;
1118 gfc_namespace* real_context;
1120 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1122 /* If we've got an ENTRY, find real procedure. */
1123 if (sym->attr.entry && sym->ns->entries)
1124 proc_sym = sym->ns->entries->sym;
1128 /* If sym is RECURSIVE, all is well of course. */
1129 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1132 /* Find the context procedure's "real" symbol if it has entries.
1133 We look for a procedure symbol, so recurse on the parents if we don't
1134 find one (like in case of a BLOCK construct). */
1135 for (real_context = context; ; real_context = real_context->parent)
1137 /* We should find something, eventually! */
1138 gcc_assert (real_context);
1140 context_proc = (real_context->entries ? real_context->entries->sym
1141 : real_context->proc_name);
1143 /* In some special cases, there may not be a proc_name, like for this
1145 real(bad_kind()) function foo () ...
1146 when checking the call to bad_kind ().
1147 In these cases, we simply return here and assume that the
1152 if (context_proc->attr.flavor != FL_LABEL)
1156 /* A call from sym's body to itself is recursion, of course. */
1157 if (context_proc == proc_sym)
1160 /* The same is true if context is a contained procedure and sym the
1162 if (context_proc->attr.contained)
1164 gfc_symbol* parent_proc;
1166 gcc_assert (context->parent);
1167 parent_proc = (context->parent->entries ? context->parent->entries->sym
1168 : context->parent->proc_name);
1170 if (parent_proc == proc_sym)
1178 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1179 its typespec and formal argument list. */
1182 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1184 gfc_intrinsic_sym* isym;
1190 /* We already know this one is an intrinsic, so we don't call
1191 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1192 gfc_find_subroutine directly to check whether it is a function or
1195 if ((isym = gfc_find_function (sym->name)))
1197 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1198 && !sym->attr.implicit_type)
1199 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1200 " ignored", sym->name, &sym->declared_at);
1202 if (!sym->attr.function &&
1203 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1208 else if ((isym = gfc_find_subroutine (sym->name)))
1210 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1212 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1213 " specifier", sym->name, &sym->declared_at);
1217 if (!sym->attr.subroutine &&
1218 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1223 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1228 gfc_copy_formal_args_intr (sym, isym);
1230 /* Check it is actually available in the standard settings. */
1231 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1234 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1235 " available in the current standard settings but %s. Use"
1236 " an appropriate -std=* option or enable -fall-intrinsics"
1237 " in order to use it.",
1238 sym->name, &sym->declared_at, symstd);
1246 /* Resolve a procedure expression, like passing it to a called procedure or as
1247 RHS for a procedure pointer assignment. */
1250 resolve_procedure_expression (gfc_expr* expr)
1254 if (expr->expr_type != EXPR_VARIABLE)
1256 gcc_assert (expr->symtree);
1258 sym = expr->symtree->n.sym;
1260 if (sym->attr.intrinsic)
1261 resolve_intrinsic (sym, &expr->where);
1263 if (sym->attr.flavor != FL_PROCEDURE
1264 || (sym->attr.function && sym->result == sym))
1267 /* A non-RECURSIVE procedure that is used as procedure expression within its
1268 own body is in danger of being called recursively. */
1269 if (is_illegal_recursion (sym, gfc_current_ns))
1270 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1271 " itself recursively. Declare it RECURSIVE or use"
1272 " -frecursive", sym->name, &expr->where);
1278 /* Resolve an actual argument list. Most of the time, this is just
1279 resolving the expressions in the list.
1280 The exception is that we sometimes have to decide whether arguments
1281 that look like procedure arguments are really simple variable
1285 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1286 bool no_formal_args)
1289 gfc_symtree *parent_st;
1291 int save_need_full_assumed_size;
1292 gfc_component *comp;
1294 for (; arg; arg = arg->next)
1299 /* Check the label is a valid branching target. */
1302 if (arg->label->defined == ST_LABEL_UNKNOWN)
1304 gfc_error ("Label %d referenced at %L is never defined",
1305 arg->label->value, &arg->label->where);
1312 if (gfc_is_proc_ptr_comp (e, &comp))
1315 if (e->expr_type == EXPR_PPC)
1317 if (comp->as != NULL)
1318 e->rank = comp->as->rank;
1319 e->expr_type = EXPR_FUNCTION;
1324 if (e->expr_type == EXPR_VARIABLE
1325 && e->symtree->n.sym->attr.generic
1327 && count_specific_procs (e) != 1)
1330 if (e->ts.type != BT_PROCEDURE)
1332 save_need_full_assumed_size = need_full_assumed_size;
1333 if (e->expr_type != EXPR_VARIABLE)
1334 need_full_assumed_size = 0;
1335 if (gfc_resolve_expr (e) != SUCCESS)
1337 need_full_assumed_size = save_need_full_assumed_size;
1341 /* See if the expression node should really be a variable reference. */
1343 sym = e->symtree->n.sym;
1345 if (sym->attr.flavor == FL_PROCEDURE
1346 || sym->attr.intrinsic
1347 || sym->attr.external)
1351 /* If a procedure is not already determined to be something else
1352 check if it is intrinsic. */
1353 if (!sym->attr.intrinsic
1354 && !(sym->attr.external || sym->attr.use_assoc
1355 || sym->attr.if_source == IFSRC_IFBODY)
1356 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1357 sym->attr.intrinsic = 1;
1359 if (sym->attr.proc == PROC_ST_FUNCTION)
1361 gfc_error ("Statement function '%s' at %L is not allowed as an "
1362 "actual argument", sym->name, &e->where);
1365 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1366 sym->attr.subroutine);
1367 if (sym->attr.intrinsic && actual_ok == 0)
1369 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1370 "actual argument", sym->name, &e->where);
1373 if (sym->attr.contained && !sym->attr.use_assoc
1374 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1376 gfc_error ("Internal procedure '%s' is not allowed as an "
1377 "actual argument at %L", sym->name, &e->where);
1380 if (sym->attr.elemental && !sym->attr.intrinsic)
1382 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1383 "allowed as an actual argument at %L", sym->name,
1387 /* Check if a generic interface has a specific procedure
1388 with the same name before emitting an error. */
1389 if (sym->attr.generic && count_specific_procs (e) != 1)
1392 /* Just in case a specific was found for the expression. */
1393 sym = e->symtree->n.sym;
1395 /* If the symbol is the function that names the current (or
1396 parent) scope, then we really have a variable reference. */
1398 if (sym->attr.function && sym->result == sym
1399 && (sym->ns->proc_name == sym
1400 || (sym->ns->parent != NULL
1401 && sym->ns->parent->proc_name == sym)))
1404 /* If all else fails, see if we have a specific intrinsic. */
1405 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1407 gfc_intrinsic_sym *isym;
1409 isym = gfc_find_function (sym->name);
1410 if (isym == NULL || !isym->specific)
1412 gfc_error ("Unable to find a specific INTRINSIC procedure "
1413 "for the reference '%s' at %L", sym->name,
1418 sym->attr.intrinsic = 1;
1419 sym->attr.function = 1;
1422 if (gfc_resolve_expr (e) == FAILURE)
1427 /* See if the name is a module procedure in a parent unit. */
1429 if (was_declared (sym) || sym->ns->parent == NULL)
1432 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1434 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1438 if (parent_st == NULL)
1441 sym = parent_st->n.sym;
1442 e->symtree = parent_st; /* Point to the right thing. */
1444 if (sym->attr.flavor == FL_PROCEDURE
1445 || sym->attr.intrinsic
1446 || sym->attr.external)
1448 if (gfc_resolve_expr (e) == FAILURE)
1454 e->expr_type = EXPR_VARIABLE;
1456 if (sym->as != NULL)
1458 e->rank = sym->as->rank;
1459 e->ref = gfc_get_ref ();
1460 e->ref->type = REF_ARRAY;
1461 e->ref->u.ar.type = AR_FULL;
1462 e->ref->u.ar.as = sym->as;
1465 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1466 primary.c (match_actual_arg). If above code determines that it
1467 is a variable instead, it needs to be resolved as it was not
1468 done at the beginning of this function. */
1469 save_need_full_assumed_size = need_full_assumed_size;
1470 if (e->expr_type != EXPR_VARIABLE)
1471 need_full_assumed_size = 0;
1472 if (gfc_resolve_expr (e) != SUCCESS)
1474 need_full_assumed_size = save_need_full_assumed_size;
1477 /* Check argument list functions %VAL, %LOC and %REF. There is
1478 nothing to do for %REF. */
1479 if (arg->name && arg->name[0] == '%')
1481 if (strncmp ("%VAL", arg->name, 4) == 0)
1483 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1485 gfc_error ("By-value argument at %L is not of numeric "
1492 gfc_error ("By-value argument at %L cannot be an array or "
1493 "an array section", &e->where);
1497 /* Intrinsics are still PROC_UNKNOWN here. However,
1498 since same file external procedures are not resolvable
1499 in gfortran, it is a good deal easier to leave them to
1501 if (ptype != PROC_UNKNOWN
1502 && ptype != PROC_DUMMY
1503 && ptype != PROC_EXTERNAL
1504 && ptype != PROC_MODULE)
1506 gfc_error ("By-value argument at %L is not allowed "
1507 "in this context", &e->where);
1512 /* Statement functions have already been excluded above. */
1513 else if (strncmp ("%LOC", arg->name, 4) == 0
1514 && e->ts.type == BT_PROCEDURE)
1516 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1518 gfc_error ("Passing internal procedure at %L by location "
1519 "not allowed", &e->where);
1530 /* Do the checks of the actual argument list that are specific to elemental
1531 procedures. If called with c == NULL, we have a function, otherwise if
1532 expr == NULL, we have a subroutine. */
1535 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1537 gfc_actual_arglist *arg0;
1538 gfc_actual_arglist *arg;
1539 gfc_symbol *esym = NULL;
1540 gfc_intrinsic_sym *isym = NULL;
1542 gfc_intrinsic_arg *iformal = NULL;
1543 gfc_formal_arglist *eformal = NULL;
1544 bool formal_optional = false;
1545 bool set_by_optional = false;
1549 /* Is this an elemental procedure? */
1550 if (expr && expr->value.function.actual != NULL)
1552 if (expr->value.function.esym != NULL
1553 && expr->value.function.esym->attr.elemental)
1555 arg0 = expr->value.function.actual;
1556 esym = expr->value.function.esym;
1558 else if (expr->value.function.isym != NULL
1559 && expr->value.function.isym->elemental)
1561 arg0 = expr->value.function.actual;
1562 isym = expr->value.function.isym;
1567 else if (c && c->ext.actual != NULL)
1569 arg0 = c->ext.actual;
1571 if (c->resolved_sym)
1572 esym = c->resolved_sym;
1574 esym = c->symtree->n.sym;
1577 if (!esym->attr.elemental)
1583 /* The rank of an elemental is the rank of its array argument(s). */
1584 for (arg = arg0; arg; arg = arg->next)
1586 if (arg->expr != NULL && arg->expr->rank > 0)
1588 rank = arg->expr->rank;
1589 if (arg->expr->expr_type == EXPR_VARIABLE
1590 && arg->expr->symtree->n.sym->attr.optional)
1591 set_by_optional = true;
1593 /* Function specific; set the result rank and shape. */
1597 if (!expr->shape && arg->expr->shape)
1599 expr->shape = gfc_get_shape (rank);
1600 for (i = 0; i < rank; i++)
1601 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1608 /* If it is an array, it shall not be supplied as an actual argument
1609 to an elemental procedure unless an array of the same rank is supplied
1610 as an actual argument corresponding to a nonoptional dummy argument of
1611 that elemental procedure(12.4.1.5). */
1612 formal_optional = false;
1614 iformal = isym->formal;
1616 eformal = esym->formal;
1618 for (arg = arg0; arg; arg = arg->next)
1622 if (eformal->sym && eformal->sym->attr.optional)
1623 formal_optional = true;
1624 eformal = eformal->next;
1626 else if (isym && iformal)
1628 if (iformal->optional)
1629 formal_optional = true;
1630 iformal = iformal->next;
1633 formal_optional = true;
1635 if (pedantic && arg->expr != NULL
1636 && arg->expr->expr_type == EXPR_VARIABLE
1637 && arg->expr->symtree->n.sym->attr.optional
1640 && (set_by_optional || arg->expr->rank != rank)
1641 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1643 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1644 "MISSING, it cannot be the actual argument of an "
1645 "ELEMENTAL procedure unless there is a non-optional "
1646 "argument with the same rank (12.4.1.5)",
1647 arg->expr->symtree->n.sym->name, &arg->expr->where);
1652 for (arg = arg0; arg; arg = arg->next)
1654 if (arg->expr == NULL || arg->expr->rank == 0)
1657 /* Being elemental, the last upper bound of an assumed size array
1658 argument must be present. */
1659 if (resolve_assumed_size_actual (arg->expr))
1662 /* Elemental procedure's array actual arguments must conform. */
1665 if (gfc_check_conformance (arg->expr, e,
1666 "elemental procedure") == FAILURE)
1673 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1674 is an array, the intent inout/out variable needs to be also an array. */
1675 if (rank > 0 && esym && expr == NULL)
1676 for (eformal = esym->formal, arg = arg0; arg && eformal;
1677 arg = arg->next, eformal = eformal->next)
1678 if ((eformal->sym->attr.intent == INTENT_OUT
1679 || eformal->sym->attr.intent == INTENT_INOUT)
1680 && arg->expr && arg->expr->rank == 0)
1682 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1683 "ELEMENTAL subroutine '%s' is a scalar, but another "
1684 "actual argument is an array", &arg->expr->where,
1685 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1686 : "INOUT", eformal->sym->name, esym->name);
1693 /* Go through each actual argument in ACTUAL and see if it can be
1694 implemented as an inlined, non-copying intrinsic. FNSYM is the
1695 function being called, or NULL if not known. */
1698 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1700 gfc_actual_arglist *ap;
1703 for (ap = actual; ap; ap = ap->next)
1705 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1706 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1708 ap->expr->inline_noncopying_intrinsic = 1;
1712 /* This function does the checking of references to global procedures
1713 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1714 77 and 95 standards. It checks for a gsymbol for the name, making
1715 one if it does not already exist. If it already exists, then the
1716 reference being resolved must correspond to the type of gsymbol.
1717 Otherwise, the new symbol is equipped with the attributes of the
1718 reference. The corresponding code that is called in creating
1719 global entities is parse.c.
1721 In addition, for all but -std=legacy, the gsymbols are used to
1722 check the interfaces of external procedures from the same file.
1723 The namespace of the gsymbol is resolved and then, once this is
1724 done the interface is checked. */
1728 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1730 if (!gsym_ns->proc_name->attr.recursive)
1733 if (sym->ns == gsym_ns)
1736 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1743 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1745 if (gsym_ns->entries)
1747 gfc_entry_list *entry = gsym_ns->entries;
1749 for (; entry; entry = entry->next)
1751 if (strcmp (sym->name, entry->sym->name) == 0)
1753 if (strcmp (gsym_ns->proc_name->name,
1754 sym->ns->proc_name->name) == 0)
1758 && strcmp (gsym_ns->proc_name->name,
1759 sym->ns->parent->proc_name->name) == 0)
1768 resolve_global_procedure (gfc_symbol *sym, locus *where,
1769 gfc_actual_arglist **actual, int sub)
1773 enum gfc_symbol_type type;
1775 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1777 gsym = gfc_get_gsymbol (sym->name);
1779 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1780 gfc_global_used (gsym, where);
1782 if (gfc_option.flag_whole_file
1783 && sym->attr.if_source == IFSRC_UNKNOWN
1784 && gsym->type != GSYM_UNKNOWN
1786 && gsym->ns->resolved != -1
1787 && gsym->ns->proc_name
1788 && not_in_recursive (sym, gsym->ns)
1789 && not_entry_self_reference (sym, gsym->ns))
1791 /* Make sure that translation for the gsymbol occurs before
1792 the procedure currently being resolved. */
1793 ns = gsym->ns->resolved ? NULL : gfc_global_ns_list;
1794 for (; ns && ns != gsym->ns; ns = ns->sibling)
1796 if (ns->sibling == gsym->ns)
1798 ns->sibling = gsym->ns->sibling;
1799 gsym->ns->sibling = gfc_global_ns_list;
1800 gfc_global_ns_list = gsym->ns;
1805 if (!gsym->ns->resolved)
1807 gfc_dt_list *old_dt_list;
1809 /* Stash away derived types so that the backend_decls do not
1811 old_dt_list = gfc_derived_types;
1812 gfc_derived_types = NULL;
1814 gfc_resolve (gsym->ns);
1816 /* Store the new derived types with the global namespace. */
1817 if (gfc_derived_types)
1818 gsym->ns->derived_types = gfc_derived_types;
1820 /* Restore the derived types of this namespace. */
1821 gfc_derived_types = old_dt_list;
1824 if (gsym->ns->proc_name->attr.function
1825 && gsym->ns->proc_name->as
1826 && gsym->ns->proc_name->as->rank
1827 && (!sym->as || sym->as->rank != gsym->ns->proc_name->as->rank))
1828 gfc_error ("The reference to function '%s' at %L either needs an "
1829 "explicit INTERFACE or the rank is incorrect", sym->name,
1832 if (gfc_option.flag_whole_file == 1
1833 || ((gfc_option.warn_std & GFC_STD_LEGACY)
1835 !(gfc_option.warn_std & GFC_STD_GNU)))
1836 gfc_errors_to_warnings (1);
1838 gfc_procedure_use (gsym->ns->proc_name, actual, where);
1840 gfc_errors_to_warnings (0);
1843 if (gsym->type == GSYM_UNKNOWN)
1846 gsym->where = *where;
1853 /************* Function resolution *************/
1855 /* Resolve a function call known to be generic.
1856 Section 14.1.2.4.1. */
1859 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1863 if (sym->attr.generic)
1865 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1868 expr->value.function.name = s->name;
1869 expr->value.function.esym = s;
1871 if (s->ts.type != BT_UNKNOWN)
1873 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1874 expr->ts = s->result->ts;
1877 expr->rank = s->as->rank;
1878 else if (s->result != NULL && s->result->as != NULL)
1879 expr->rank = s->result->as->rank;
1881 gfc_set_sym_referenced (expr->value.function.esym);
1886 /* TODO: Need to search for elemental references in generic
1890 if (sym->attr.intrinsic)
1891 return gfc_intrinsic_func_interface (expr, 0);
1898 resolve_generic_f (gfc_expr *expr)
1903 sym = expr->symtree->n.sym;
1907 m = resolve_generic_f0 (expr, sym);
1910 else if (m == MATCH_ERROR)
1914 if (sym->ns->parent == NULL)
1916 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1920 if (!generic_sym (sym))
1924 /* Last ditch attempt. See if the reference is to an intrinsic
1925 that possesses a matching interface. 14.1.2.4 */
1926 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
1928 gfc_error ("There is no specific function for the generic '%s' at %L",
1929 expr->symtree->n.sym->name, &expr->where);
1933 m = gfc_intrinsic_func_interface (expr, 0);
1937 gfc_error ("Generic function '%s' at %L is not consistent with a "
1938 "specific intrinsic interface", expr->symtree->n.sym->name,
1945 /* Resolve a function call known to be specific. */
1948 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
1952 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1954 if (sym->attr.dummy)
1956 sym->attr.proc = PROC_DUMMY;
1960 sym->attr.proc = PROC_EXTERNAL;
1964 if (sym->attr.proc == PROC_MODULE
1965 || sym->attr.proc == PROC_ST_FUNCTION
1966 || sym->attr.proc == PROC_INTERNAL)
1969 if (sym->attr.intrinsic)
1971 m = gfc_intrinsic_func_interface (expr, 1);
1975 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
1976 "with an intrinsic", sym->name, &expr->where);
1984 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1987 expr->ts = sym->result->ts;
1990 expr->value.function.name = sym->name;
1991 expr->value.function.esym = sym;
1992 if (sym->as != NULL)
1993 expr->rank = sym->as->rank;
2000 resolve_specific_f (gfc_expr *expr)
2005 sym = expr->symtree->n.sym;
2009 m = resolve_specific_f0 (sym, expr);
2012 if (m == MATCH_ERROR)
2015 if (sym->ns->parent == NULL)
2018 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2024 gfc_error ("Unable to resolve the specific function '%s' at %L",
2025 expr->symtree->n.sym->name, &expr->where);
2031 /* Resolve a procedure call not known to be generic nor specific. */
2034 resolve_unknown_f (gfc_expr *expr)
2039 sym = expr->symtree->n.sym;
2041 if (sym->attr.dummy)
2043 sym->attr.proc = PROC_DUMMY;
2044 expr->value.function.name = sym->name;
2048 /* See if we have an intrinsic function reference. */
2050 if (gfc_is_intrinsic (sym, 0, expr->where))
2052 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2057 /* The reference is to an external name. */
2059 sym->attr.proc = PROC_EXTERNAL;
2060 expr->value.function.name = sym->name;
2061 expr->value.function.esym = expr->symtree->n.sym;
2063 if (sym->as != NULL)
2064 expr->rank = sym->as->rank;
2066 /* Type of the expression is either the type of the symbol or the
2067 default type of the symbol. */
2070 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2072 if (sym->ts.type != BT_UNKNOWN)
2076 ts = gfc_get_default_type (sym->name, sym->ns);
2078 if (ts->type == BT_UNKNOWN)
2080 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2081 sym->name, &expr->where);
2092 /* Return true, if the symbol is an external procedure. */
2094 is_external_proc (gfc_symbol *sym)
2096 if (!sym->attr.dummy && !sym->attr.contained
2097 && !(sym->attr.intrinsic
2098 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2099 && sym->attr.proc != PROC_ST_FUNCTION
2100 && !sym->attr.use_assoc
2108 /* Figure out if a function reference is pure or not. Also set the name
2109 of the function for a potential error message. Return nonzero if the
2110 function is PURE, zero if not. */
2112 pure_stmt_function (gfc_expr *, gfc_symbol *);
2115 pure_function (gfc_expr *e, const char **name)
2121 if (e->symtree != NULL
2122 && e->symtree->n.sym != NULL
2123 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2124 return pure_stmt_function (e, e->symtree->n.sym);
2126 if (e->value.function.esym)
2128 pure = gfc_pure (e->value.function.esym);
2129 *name = e->value.function.esym->name;
2131 else if (e->value.function.isym)
2133 pure = e->value.function.isym->pure
2134 || e->value.function.isym->elemental;
2135 *name = e->value.function.isym->name;
2139 /* Implicit functions are not pure. */
2141 *name = e->value.function.name;
2149 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2150 int *f ATTRIBUTE_UNUSED)
2154 /* Don't bother recursing into other statement functions
2155 since they will be checked individually for purity. */
2156 if (e->expr_type != EXPR_FUNCTION
2158 || e->symtree->n.sym == sym
2159 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2162 return pure_function (e, &name) ? false : true;
2167 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2169 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2174 is_scalar_expr_ptr (gfc_expr *expr)
2176 gfc_try retval = SUCCESS;
2181 /* See if we have a gfc_ref, which means we have a substring, array
2182 reference, or a component. */
2183 if (expr->ref != NULL)
2186 while (ref->next != NULL)
2192 if (ref->u.ss.length != NULL
2193 && ref->u.ss.length->length != NULL
2195 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2197 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2199 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2200 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2201 if (end - start + 1 != 1)
2208 if (ref->u.ar.type == AR_ELEMENT)
2210 else if (ref->u.ar.type == AR_FULL)
2212 /* The user can give a full array if the array is of size 1. */
2213 if (ref->u.ar.as != NULL
2214 && ref->u.ar.as->rank == 1
2215 && ref->u.ar.as->type == AS_EXPLICIT
2216 && ref->u.ar.as->lower[0] != NULL
2217 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2218 && ref->u.ar.as->upper[0] != NULL
2219 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2221 /* If we have a character string, we need to check if
2222 its length is one. */
2223 if (expr->ts.type == BT_CHARACTER)
2225 if (expr->ts.u.cl == NULL
2226 || expr->ts.u.cl->length == NULL
2227 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2233 /* We have constant lower and upper bounds. If the
2234 difference between is 1, it can be considered a
2236 start = (int) mpz_get_si
2237 (ref->u.ar.as->lower[0]->value.integer);
2238 end = (int) mpz_get_si
2239 (ref->u.ar.as->upper[0]->value.integer);
2240 if (end - start + 1 != 1)
2255 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2257 /* Character string. Make sure it's of length 1. */
2258 if (expr->ts.u.cl == NULL
2259 || expr->ts.u.cl->length == NULL
2260 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2263 else if (expr->rank != 0)
2270 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2271 and, in the case of c_associated, set the binding label based on
2275 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2276 gfc_symbol **new_sym)
2278 char name[GFC_MAX_SYMBOL_LEN + 1];
2279 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2280 int optional_arg = 0, is_pointer = 0;
2281 gfc_try retval = SUCCESS;
2282 gfc_symbol *args_sym;
2283 gfc_typespec *arg_ts;
2285 if (args->expr->expr_type == EXPR_CONSTANT
2286 || args->expr->expr_type == EXPR_OP
2287 || args->expr->expr_type == EXPR_NULL)
2289 gfc_error ("Argument to '%s' at %L is not a variable",
2290 sym->name, &(args->expr->where));
2294 args_sym = args->expr->symtree->n.sym;
2296 /* The typespec for the actual arg should be that stored in the expr
2297 and not necessarily that of the expr symbol (args_sym), because
2298 the actual expression could be a part-ref of the expr symbol. */
2299 arg_ts = &(args->expr->ts);
2301 is_pointer = gfc_is_data_pointer (args->expr);
2303 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2305 /* If the user gave two args then they are providing something for
2306 the optional arg (the second cptr). Therefore, set the name and
2307 binding label to the c_associated for two cptrs. Otherwise,
2308 set c_associated to expect one cptr. */
2312 sprintf (name, "%s_2", sym->name);
2313 sprintf (binding_label, "%s_2", sym->binding_label);
2319 sprintf (name, "%s_1", sym->name);
2320 sprintf (binding_label, "%s_1", sym->binding_label);
2324 /* Get a new symbol for the version of c_associated that
2326 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2328 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2329 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2331 sprintf (name, "%s", sym->name);
2332 sprintf (binding_label, "%s", sym->binding_label);
2334 /* Error check the call. */
2335 if (args->next != NULL)
2337 gfc_error_now ("More actual than formal arguments in '%s' "
2338 "call at %L", name, &(args->expr->where));
2341 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2343 /* Make sure we have either the target or pointer attribute. */
2344 if (!args_sym->attr.target && !is_pointer)
2346 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2347 "a TARGET or an associated pointer",
2349 sym->name, &(args->expr->where));
2353 /* See if we have interoperable type and type param. */
2354 if (verify_c_interop (arg_ts) == SUCCESS
2355 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2357 if (args_sym->attr.target == 1)
2359 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2360 has the target attribute and is interoperable. */
2361 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2362 allocatable variable that has the TARGET attribute and
2363 is not an array of zero size. */
2364 if (args_sym->attr.allocatable == 1)
2366 if (args_sym->attr.dimension != 0
2367 && (args_sym->as && args_sym->as->rank == 0))
2369 gfc_error_now ("Allocatable variable '%s' used as a "
2370 "parameter to '%s' at %L must not be "
2371 "an array of zero size",
2372 args_sym->name, sym->name,
2373 &(args->expr->where));
2379 /* A non-allocatable target variable with C
2380 interoperable type and type parameters must be
2382 if (args_sym && args_sym->attr.dimension)
2384 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2386 gfc_error ("Assumed-shape array '%s' at %L "
2387 "cannot be an argument to the "
2388 "procedure '%s' because "
2389 "it is not C interoperable",
2391 &(args->expr->where), sym->name);
2394 else if (args_sym->as->type == AS_DEFERRED)
2396 gfc_error ("Deferred-shape array '%s' at %L "
2397 "cannot be an argument to the "
2398 "procedure '%s' because "
2399 "it is not C interoperable",
2401 &(args->expr->where), sym->name);
2406 /* Make sure it's not a character string. Arrays of
2407 any type should be ok if the variable is of a C
2408 interoperable type. */
2409 if (arg_ts->type == BT_CHARACTER)
2410 if (arg_ts->u.cl != NULL
2411 && (arg_ts->u.cl->length == NULL
2412 || arg_ts->u.cl->length->expr_type
2415 (arg_ts->u.cl->length->value.integer, 1)
2417 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2419 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2420 "at %L must have a length of 1",
2421 args_sym->name, sym->name,
2422 &(args->expr->where));
2428 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2430 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2432 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2433 "associated scalar POINTER", args_sym->name,
2434 sym->name, &(args->expr->where));
2440 /* The parameter is not required to be C interoperable. If it
2441 is not C interoperable, it must be a nonpolymorphic scalar
2442 with no length type parameters. It still must have either
2443 the pointer or target attribute, and it can be
2444 allocatable (but must be allocated when c_loc is called). */
2445 if (args->expr->rank != 0
2446 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2448 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2449 "scalar", args_sym->name, sym->name,
2450 &(args->expr->where));
2453 else if (arg_ts->type == BT_CHARACTER
2454 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2456 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2457 "%L must have a length of 1",
2458 args_sym->name, sym->name,
2459 &(args->expr->where));
2464 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2466 if (args_sym->attr.flavor != FL_PROCEDURE)
2468 /* TODO: Update this error message to allow for procedure
2469 pointers once they are implemented. */
2470 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2472 args_sym->name, sym->name,
2473 &(args->expr->where));
2476 else if (args_sym->attr.is_bind_c != 1)
2478 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2480 args_sym->name, sym->name,
2481 &(args->expr->where));
2486 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2491 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2492 "iso_c_binding function: '%s'!\n", sym->name);
2499 /* Resolve a function call, which means resolving the arguments, then figuring
2500 out which entity the name refers to. */
2501 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2502 to INTENT(OUT) or INTENT(INOUT). */
2505 resolve_function (gfc_expr *expr)
2507 gfc_actual_arglist *arg;
2512 procedure_type p = PROC_INTRINSIC;
2513 bool no_formal_args;
2517 sym = expr->symtree->n.sym;
2519 if (sym && sym->attr.intrinsic
2520 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2523 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2525 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2529 if (sym && sym->attr.abstract)
2531 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2532 sym->name, &expr->where);
2536 /* Switch off assumed size checking and do this again for certain kinds
2537 of procedure, once the procedure itself is resolved. */
2538 need_full_assumed_size++;
2540 if (expr->symtree && expr->symtree->n.sym)
2541 p = expr->symtree->n.sym->attr.proc;
2543 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2544 if (resolve_actual_arglist (expr->value.function.actual,
2545 p, no_formal_args) == FAILURE)
2548 /* Need to setup the call to the correct c_associated, depending on
2549 the number of cptrs to user gives to compare. */
2550 if (sym && sym->attr.is_iso_c == 1)
2552 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2556 /* Get the symtree for the new symbol (resolved func).
2557 the old one will be freed later, when it's no longer used. */
2558 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2561 /* Resume assumed_size checking. */
2562 need_full_assumed_size--;
2564 /* If the procedure is external, check for usage. */
2565 if (sym && is_external_proc (sym))
2566 resolve_global_procedure (sym, &expr->where,
2567 &expr->value.function.actual, 0);
2569 if (sym && sym->ts.type == BT_CHARACTER
2571 && sym->ts.u.cl->length == NULL
2573 && expr->value.function.esym == NULL
2574 && !sym->attr.contained)
2576 /* Internal procedures are taken care of in resolve_contained_fntype. */
2577 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2578 "be used at %L since it is not a dummy argument",
2579 sym->name, &expr->where);
2583 /* See if function is already resolved. */
2585 if (expr->value.function.name != NULL)
2587 if (expr->ts.type == BT_UNKNOWN)
2593 /* Apply the rules of section 14.1.2. */
2595 switch (procedure_kind (sym))
2598 t = resolve_generic_f (expr);
2601 case PTYPE_SPECIFIC:
2602 t = resolve_specific_f (expr);
2606 t = resolve_unknown_f (expr);
2610 gfc_internal_error ("resolve_function(): bad function type");
2614 /* If the expression is still a function (it might have simplified),
2615 then we check to see if we are calling an elemental function. */
2617 if (expr->expr_type != EXPR_FUNCTION)
2620 temp = need_full_assumed_size;
2621 need_full_assumed_size = 0;
2623 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2626 if (omp_workshare_flag
2627 && expr->value.function.esym
2628 && ! gfc_elemental (expr->value.function.esym))
2630 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2631 "in WORKSHARE construct", expr->value.function.esym->name,
2636 #define GENERIC_ID expr->value.function.isym->id
2637 else if (expr->value.function.actual != NULL
2638 && expr->value.function.isym != NULL
2639 && GENERIC_ID != GFC_ISYM_LBOUND
2640 && GENERIC_ID != GFC_ISYM_LEN
2641 && GENERIC_ID != GFC_ISYM_LOC
2642 && GENERIC_ID != GFC_ISYM_PRESENT)
2644 /* Array intrinsics must also have the last upper bound of an
2645 assumed size array argument. UBOUND and SIZE have to be
2646 excluded from the check if the second argument is anything
2649 for (arg = expr->value.function.actual; arg; arg = arg->next)
2651 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2652 && arg->next != NULL && arg->next->expr)
2654 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2657 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2660 if ((int)mpz_get_si (arg->next->expr->value.integer)
2665 if (arg->expr != NULL
2666 && arg->expr->rank > 0
2667 && resolve_assumed_size_actual (arg->expr))
2673 need_full_assumed_size = temp;
2676 if (!pure_function (expr, &name) && name)
2680 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2681 "FORALL %s", name, &expr->where,
2682 forall_flag == 2 ? "mask" : "block");
2685 else if (gfc_pure (NULL))
2687 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2688 "procedure within a PURE procedure", name, &expr->where);
2693 /* Functions without the RECURSIVE attribution are not allowed to
2694 * call themselves. */
2695 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2698 esym = expr->value.function.esym;
2700 if (is_illegal_recursion (esym, gfc_current_ns))
2702 if (esym->attr.entry && esym->ns->entries)
2703 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2704 " function '%s' is not RECURSIVE",
2705 esym->name, &expr->where, esym->ns->entries->sym->name);
2707 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2708 " is not RECURSIVE", esym->name, &expr->where);
2714 /* Character lengths of use associated functions may contains references to
2715 symbols not referenced from the current program unit otherwise. Make sure
2716 those symbols are marked as referenced. */
2718 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2719 && expr->value.function.esym->attr.use_assoc)
2721 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
2725 && !((expr->value.function.esym
2726 && expr->value.function.esym->attr.elemental)
2728 (expr->value.function.isym
2729 && expr->value.function.isym->elemental)))
2730 find_noncopying_intrinsics (expr->value.function.esym,
2731 expr->value.function.actual);
2733 /* Make sure that the expression has a typespec that works. */
2734 if (expr->ts.type == BT_UNKNOWN)
2736 if (expr->symtree->n.sym->result
2737 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
2738 && !expr->symtree->n.sym->result->attr.proc_pointer)
2739 expr->ts = expr->symtree->n.sym->result->ts;
2746 /************* Subroutine resolution *************/
2749 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2755 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2756 sym->name, &c->loc);
2757 else if (gfc_pure (NULL))
2758 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2764 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2768 if (sym->attr.generic)
2770 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2773 c->resolved_sym = s;
2774 pure_subroutine (c, s);
2778 /* TODO: Need to search for elemental references in generic interface. */
2781 if (sym->attr.intrinsic)
2782 return gfc_intrinsic_sub_interface (c, 0);
2789 resolve_generic_s (gfc_code *c)
2794 sym = c->symtree->n.sym;
2798 m = resolve_generic_s0 (c, sym);
2801 else if (m == MATCH_ERROR)
2805 if (sym->ns->parent == NULL)
2807 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2811 if (!generic_sym (sym))
2815 /* Last ditch attempt. See if the reference is to an intrinsic
2816 that possesses a matching interface. 14.1.2.4 */
2817 sym = c->symtree->n.sym;
2819 if (!gfc_is_intrinsic (sym, 1, c->loc))
2821 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2822 sym->name, &c->loc);
2826 m = gfc_intrinsic_sub_interface (c, 0);
2830 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2831 "intrinsic subroutine interface", sym->name, &c->loc);
2837 /* Set the name and binding label of the subroutine symbol in the call
2838 expression represented by 'c' to include the type and kind of the
2839 second parameter. This function is for resolving the appropriate
2840 version of c_f_pointer() and c_f_procpointer(). For example, a
2841 call to c_f_pointer() for a default integer pointer could have a
2842 name of c_f_pointer_i4. If no second arg exists, which is an error
2843 for these two functions, it defaults to the generic symbol's name
2844 and binding label. */
2847 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2848 char *name, char *binding_label)
2850 gfc_expr *arg = NULL;
2854 /* The second arg of c_f_pointer and c_f_procpointer determines
2855 the type and kind for the procedure name. */
2856 arg = c->ext.actual->next->expr;
2860 /* Set up the name to have the given symbol's name,
2861 plus the type and kind. */
2862 /* a derived type is marked with the type letter 'u' */
2863 if (arg->ts.type == BT_DERIVED)
2866 kind = 0; /* set the kind as 0 for now */
2870 type = gfc_type_letter (arg->ts.type);
2871 kind = arg->ts.kind;
2874 if (arg->ts.type == BT_CHARACTER)
2875 /* Kind info for character strings not needed. */
2878 sprintf (name, "%s_%c%d", sym->name, type, kind);
2879 /* Set up the binding label as the given symbol's label plus
2880 the type and kind. */
2881 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2885 /* If the second arg is missing, set the name and label as
2886 was, cause it should at least be found, and the missing
2887 arg error will be caught by compare_parameters(). */
2888 sprintf (name, "%s", sym->name);
2889 sprintf (binding_label, "%s", sym->binding_label);
2896 /* Resolve a generic version of the iso_c_binding procedure given
2897 (sym) to the specific one based on the type and kind of the
2898 argument(s). Currently, this function resolves c_f_pointer() and
2899 c_f_procpointer based on the type and kind of the second argument
2900 (FPTR). Other iso_c_binding procedures aren't specially handled.
2901 Upon successfully exiting, c->resolved_sym will hold the resolved
2902 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2906 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2908 gfc_symbol *new_sym;
2909 /* this is fine, since we know the names won't use the max */
2910 char name[GFC_MAX_SYMBOL_LEN + 1];
2911 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2912 /* default to success; will override if find error */
2913 match m = MATCH_YES;
2915 /* Make sure the actual arguments are in the necessary order (based on the
2916 formal args) before resolving. */
2917 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2919 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2920 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2922 set_name_and_label (c, sym, name, binding_label);
2924 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2926 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2928 /* Make sure we got a third arg if the second arg has non-zero
2929 rank. We must also check that the type and rank are
2930 correct since we short-circuit this check in
2931 gfc_procedure_use() (called above to sort actual args). */
2932 if (c->ext.actual->next->expr->rank != 0)
2934 if(c->ext.actual->next->next == NULL
2935 || c->ext.actual->next->next->expr == NULL)
2938 gfc_error ("Missing SHAPE parameter for call to %s "
2939 "at %L", sym->name, &(c->loc));
2941 else if (c->ext.actual->next->next->expr->ts.type
2943 || c->ext.actual->next->next->expr->rank != 1)
2946 gfc_error ("SHAPE parameter for call to %s at %L must "
2947 "be a rank 1 INTEGER array", sym->name,
2954 if (m != MATCH_ERROR)
2956 /* the 1 means to add the optional arg to formal list */
2957 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
2959 /* for error reporting, say it's declared where the original was */
2960 new_sym->declared_at = sym->declared_at;
2965 /* no differences for c_loc or c_funloc */
2969 /* set the resolved symbol */
2970 if (m != MATCH_ERROR)
2971 c->resolved_sym = new_sym;
2973 c->resolved_sym = sym;
2979 /* Resolve a subroutine call known to be specific. */
2982 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
2986 if(sym->attr.is_iso_c)
2988 m = gfc_iso_c_sub_interface (c,sym);
2992 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2994 if (sym->attr.dummy)
2996 sym->attr.proc = PROC_DUMMY;
3000 sym->attr.proc = PROC_EXTERNAL;
3004 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3007 if (sym->attr.intrinsic)
3009 m = gfc_intrinsic_sub_interface (c, 1);
3013 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3014 "with an intrinsic", sym->name, &c->loc);
3022 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3024 c->resolved_sym = sym;
3025 pure_subroutine (c, sym);
3032 resolve_specific_s (gfc_code *c)
3037 sym = c->symtree->n.sym;
3041 m = resolve_specific_s0 (c, sym);
3044 if (m == MATCH_ERROR)
3047 if (sym->ns->parent == NULL)
3050 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3056 sym = c->symtree->n.sym;
3057 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3058 sym->name, &c->loc);
3064 /* Resolve a subroutine call not known to be generic nor specific. */
3067 resolve_unknown_s (gfc_code *c)
3071 sym = c->symtree->n.sym;
3073 if (sym->attr.dummy)
3075 sym->attr.proc = PROC_DUMMY;
3079 /* See if we have an intrinsic function reference. */
3081 if (gfc_is_intrinsic (sym, 1, c->loc))
3083 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3088 /* The reference is to an external name. */
3091 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3093 c->resolved_sym = sym;
3095 pure_subroutine (c, sym);
3101 /* Resolve a subroutine call. Although it was tempting to use the same code
3102 for functions, subroutines and functions are stored differently and this
3103 makes things awkward. */
3106 resolve_call (gfc_code *c)
3109 procedure_type ptype = PROC_INTRINSIC;
3110 gfc_symbol *csym, *sym;
3111 bool no_formal_args;
3113 csym = c->symtree ? c->symtree->n.sym : NULL;
3115 if (csym && csym->ts.type != BT_UNKNOWN)
3117 gfc_error ("'%s' at %L has a type, which is not consistent with "
3118 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3122 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3125 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3126 sym = st ? st->n.sym : NULL;
3127 if (sym && csym != sym
3128 && sym->ns == gfc_current_ns
3129 && sym->attr.flavor == FL_PROCEDURE
3130 && sym->attr.contained)
3133 if (csym->attr.generic)
3134 c->symtree->n.sym = sym;
3137 csym = c->symtree->n.sym;
3141 /* Subroutines without the RECURSIVE attribution are not allowed to
3142 * call themselves. */
3143 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3145 if (csym->attr.entry && csym->ns->entries)
3146 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3147 " subroutine '%s' is not RECURSIVE",
3148 csym->name, &c->loc, csym->ns->entries->sym->name);
3150 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3151 " is not RECURSIVE", csym->name, &c->loc);
3156 /* Switch off assumed size checking and do this again for certain kinds
3157 of procedure, once the procedure itself is resolved. */
3158 need_full_assumed_size++;
3161 ptype = csym->attr.proc;
3163 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3164 if (resolve_actual_arglist (c->ext.actual, ptype,
3165 no_formal_args) == FAILURE)
3168 /* Resume assumed_size checking. */
3169 need_full_assumed_size--;
3171 /* If external, check for usage. */
3172 if (csym && is_external_proc (csym))
3173 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3176 if (c->resolved_sym == NULL)
3178 c->resolved_isym = NULL;
3179 switch (procedure_kind (csym))
3182 t = resolve_generic_s (c);
3185 case PTYPE_SPECIFIC:
3186 t = resolve_specific_s (c);
3190 t = resolve_unknown_s (c);
3194 gfc_internal_error ("resolve_subroutine(): bad function type");
3198 /* Some checks of elemental subroutine actual arguments. */
3199 if (resolve_elemental_actual (NULL, c) == FAILURE)
3202 if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
3203 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
3208 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3209 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3210 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3211 if their shapes do not match. If either op1->shape or op2->shape is
3212 NULL, return SUCCESS. */
3215 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3222 if (op1->shape != NULL && op2->shape != NULL)
3224 for (i = 0; i < op1->rank; i++)
3226 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3228 gfc_error ("Shapes for operands at %L and %L are not conformable",
3229 &op1->where, &op2->where);
3240 /* Resolve an operator expression node. This can involve replacing the
3241 operation with a user defined function call. */
3244 resolve_operator (gfc_expr *e)
3246 gfc_expr *op1, *op2;
3248 bool dual_locus_error;
3251 /* Resolve all subnodes-- give them types. */
3253 switch (e->value.op.op)
3256 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3259 /* Fall through... */
3262 case INTRINSIC_UPLUS:
3263 case INTRINSIC_UMINUS:
3264 case INTRINSIC_PARENTHESES:
3265 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3270 /* Typecheck the new node. */
3272 op1 = e->value.op.op1;
3273 op2 = e->value.op.op2;
3274 dual_locus_error = false;
3276 if ((op1 && op1->expr_type == EXPR_NULL)
3277 || (op2 && op2->expr_type == EXPR_NULL))
3279 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3283 switch (e->value.op.op)
3285 case INTRINSIC_UPLUS:
3286 case INTRINSIC_UMINUS:
3287 if (op1->ts.type == BT_INTEGER
3288 || op1->ts.type == BT_REAL
3289 || op1->ts.type == BT_COMPLEX)
3295 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3296 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3299 case INTRINSIC_PLUS:
3300 case INTRINSIC_MINUS:
3301 case INTRINSIC_TIMES:
3302 case INTRINSIC_DIVIDE:
3303 case INTRINSIC_POWER:
3304 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3306 gfc_type_convert_binary (e);
3311 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3312 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3313 gfc_typename (&op2->ts));
3316 case INTRINSIC_CONCAT:
3317 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3318 && op1->ts.kind == op2->ts.kind)
3320 e->ts.type = BT_CHARACTER;
3321 e->ts.kind = op1->ts.kind;
3326 _("Operands of string concatenation operator at %%L are %s/%s"),
3327 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3333 case INTRINSIC_NEQV:
3334 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3336 e->ts.type = BT_LOGICAL;
3337 e->ts.kind = gfc_kind_max (op1, op2);
3338 if (op1->ts.kind < e->ts.kind)
3339 gfc_convert_type (op1, &e->ts, 2);
3340 else if (op2->ts.kind < e->ts.kind)
3341 gfc_convert_type (op2, &e->ts, 2);
3345 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3346 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3347 gfc_typename (&op2->ts));
3352 if (op1->ts.type == BT_LOGICAL)
3354 e->ts.type = BT_LOGICAL;
3355 e->ts.kind = op1->ts.kind;
3359 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3360 gfc_typename (&op1->ts));
3364 case INTRINSIC_GT_OS:
3366 case INTRINSIC_GE_OS:
3368 case INTRINSIC_LT_OS:
3370 case INTRINSIC_LE_OS:
3371 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3373 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3377 /* Fall through... */
3380 case INTRINSIC_EQ_OS:
3382 case INTRINSIC_NE_OS:
3383 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3384 && op1->ts.kind == op2->ts.kind)
3386 e->ts.type = BT_LOGICAL;
3387 e->ts.kind = gfc_default_logical_kind;
3391 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3393 gfc_type_convert_binary (e);
3395 e->ts.type = BT_LOGICAL;
3396 e->ts.kind = gfc_default_logical_kind;
3400 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3402 _("Logicals at %%L must be compared with %s instead of %s"),
3403 (e->value.op.op == INTRINSIC_EQ
3404 || e->value.op.op == INTRINSIC_EQ_OS)
3405 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3408 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3409 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3410 gfc_typename (&op2->ts));
3414 case INTRINSIC_USER:
3415 if (e->value.op.uop->op == NULL)
3416 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3417 else if (op2 == NULL)
3418 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3419 e->value.op.uop->name, gfc_typename (&op1->ts));
3421 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3422 e->value.op.uop->name, gfc_typename (&op1->ts),
3423 gfc_typename (&op2->ts));
3427 case INTRINSIC_PARENTHESES:
3429 if (e->ts.type == BT_CHARACTER)
3430 e->ts.u.cl = op1->ts.u.cl;
3434 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3437 /* Deal with arrayness of an operand through an operator. */
3441 switch (e->value.op.op)
3443 case INTRINSIC_PLUS:
3444 case INTRINSIC_MINUS:
3445 case INTRINSIC_TIMES:
3446 case INTRINSIC_DIVIDE:
3447 case INTRINSIC_POWER:
3448 case INTRINSIC_CONCAT:
3452 case INTRINSIC_NEQV:
3454 case INTRINSIC_EQ_OS:
3456 case INTRINSIC_NE_OS:
3458 case INTRINSIC_GT_OS:
3460 case INTRINSIC_GE_OS:
3462 case INTRINSIC_LT_OS:
3464 case INTRINSIC_LE_OS:
3466 if (op1->rank == 0 && op2->rank == 0)
3469 if (op1->rank == 0 && op2->rank != 0)
3471 e->rank = op2->rank;
3473 if (e->shape == NULL)
3474 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3477 if (op1->rank != 0 && op2->rank == 0)
3479 e->rank = op1->rank;
3481 if (e->shape == NULL)
3482 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3485 if (op1->rank != 0 && op2->rank != 0)
3487 if (op1->rank == op2->rank)
3489 e->rank = op1->rank;
3490 if (e->shape == NULL)
3492 t = compare_shapes(op1, op2);
3496 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3501 /* Allow higher level expressions to work. */
3504 /* Try user-defined operators, and otherwise throw an error. */
3505 dual_locus_error = true;
3507 _("Inconsistent ranks for operator at %%L and %%L"));
3514 case INTRINSIC_PARENTHESES:
3516 case INTRINSIC_UPLUS:
3517 case INTRINSIC_UMINUS:
3518 /* Simply copy arrayness attribute */
3519 e->rank = op1->rank;
3521 if (e->shape == NULL)
3522 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3530 /* Attempt to simplify the expression. */
3533 t = gfc_simplify_expr (e, 0);
3534 /* Some calls do not succeed in simplification and return FAILURE
3535 even though there is no error; e.g. variable references to
3536 PARAMETER arrays. */
3537 if (!gfc_is_constant_expr (e))
3546 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3553 if (dual_locus_error)
3554 gfc_error (msg, &op1->where, &op2->where);
3556 gfc_error (msg, &e->where);
3562 /************** Array resolution subroutines **************/
3565 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3568 /* Compare two integer expressions. */
3571 compare_bound (gfc_expr *a, gfc_expr *b)
3575 if (a == NULL || a->expr_type != EXPR_CONSTANT
3576 || b == NULL || b->expr_type != EXPR_CONSTANT)
3579 /* If either of the types isn't INTEGER, we must have
3580 raised an error earlier. */
3582 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3585 i = mpz_cmp (a->value.integer, b->value.integer);
3595 /* Compare an integer expression with an integer. */
3598 compare_bound_int (gfc_expr *a, int b)
3602 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3605 if (a->ts.type != BT_INTEGER)
3606 gfc_internal_error ("compare_bound_int(): Bad expression");
3608 i = mpz_cmp_si (a->value.integer, b);
3618 /* Compare an integer expression with a mpz_t. */
3621 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3625 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3628 if (a->ts.type != BT_INTEGER)
3629 gfc_internal_error ("compare_bound_int(): Bad expression");
3631 i = mpz_cmp (a->value.integer, b);
3641 /* Compute the last value of a sequence given by a triplet.
3642 Return 0 if it wasn't able to compute the last value, or if the
3643 sequence if empty, and 1 otherwise. */
3646 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3647 gfc_expr *stride, mpz_t last)
3651 if (start == NULL || start->expr_type != EXPR_CONSTANT
3652 || end == NULL || end->expr_type != EXPR_CONSTANT
3653 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3656 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3657 || (stride != NULL && stride->ts.type != BT_INTEGER))
3660 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3662 if (compare_bound (start, end) == CMP_GT)
3664 mpz_set (last, end->value.integer);
3668 if (compare_bound_int (stride, 0) == CMP_GT)
3670 /* Stride is positive */
3671 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3676 /* Stride is negative */
3677 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3682 mpz_sub (rem, end->value.integer, start->value.integer);
3683 mpz_tdiv_r (rem, rem, stride->value.integer);
3684 mpz_sub (last, end->value.integer, rem);
3691 /* Compare a single dimension of an array reference to the array
3695 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3699 /* Given start, end and stride values, calculate the minimum and
3700 maximum referenced indexes. */
3702 switch (ar->dimen_type[i])
3708 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3710 gfc_warning ("Array reference at %L is out of bounds "
3711 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3712 mpz_get_si (ar->start[i]->value.integer),
3713 mpz_get_si (as->lower[i]->value.integer), i+1);
3716 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3718 gfc_warning ("Array reference at %L is out of bounds "
3719 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3720 mpz_get_si (ar->start[i]->value.integer),
3721 mpz_get_si (as->upper[i]->value.integer), i+1);
3729 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3730 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3732 comparison comp_start_end = compare_bound (AR_START, AR_END);
3734 /* Check for zero stride, which is not allowed. */
3735 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3737 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3741 /* if start == len || (stride > 0 && start < len)
3742 || (stride < 0 && start > len),
3743 then the array section contains at least one element. In this
3744 case, there is an out-of-bounds access if
3745 (start < lower || start > upper). */
3746 if (compare_bound (AR_START, AR_END) == CMP_EQ
3747 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3748 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3749 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3750 && comp_start_end == CMP_GT))
3752 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3754 gfc_warning ("Lower array reference at %L is out of bounds "
3755 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3756 mpz_get_si (AR_START->value.integer),
3757 mpz_get_si (as->lower[i]->value.integer), i+1);
3760 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3762 gfc_warning ("Lower array reference at %L is out of bounds "
3763 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3764 mpz_get_si (AR_START->value.integer),
3765 mpz_get_si (as->upper[i]->value.integer), i+1);
3770 /* If we can compute the highest index of the array section,
3771 then it also has to be between lower and upper. */
3772 mpz_init (last_value);
3773 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3776 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3778 gfc_warning ("Upper array reference at %L is out of bounds "
3779 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3780 mpz_get_si (last_value),
3781 mpz_get_si (as->lower[i]->value.integer), i+1);
3782 mpz_clear (last_value);
3785 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3787 gfc_warning ("Upper array reference at %L is out of bounds "
3788 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3789 mpz_get_si (last_value),
3790 mpz_get_si (as->upper[i]->value.integer), i+1);
3791 mpz_clear (last_value);
3795 mpz_clear (last_value);
3803 gfc_internal_error ("check_dimension(): Bad array reference");
3810 /* Compare an array reference with an array specification. */
3813 compare_spec_to_ref (gfc_array_ref *ar)
3820 /* TODO: Full array sections are only allowed as actual parameters. */
3821 if (as->type == AS_ASSUMED_SIZE
3822 && (/*ar->type == AR_FULL
3823 ||*/ (ar->type == AR_SECTION
3824 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3826 gfc_error ("Rightmost upper bound of assumed size array section "
3827 "not specified at %L", &ar->where);
3831 if (ar->type == AR_FULL)
3834 if (as->rank != ar->dimen)
3836 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3837 &ar->where, ar->dimen, as->rank);
3841 for (i = 0; i < as->rank; i++)
3842 if (check_dimension (i, ar, as) == FAILURE)
3849 /* Resolve one part of an array index. */
3852 gfc_resolve_index (gfc_expr *index, int check_scalar)
3859 if (gfc_resolve_expr (index) == FAILURE)
3862 if (check_scalar && index->rank != 0)
3864 gfc_error ("Array index at %L must be scalar", &index->where);
3868 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
3870 gfc_error ("Array index at %L must be of INTEGER type, found %s",
3871 &index->where, gfc_basic_typename (index->ts.type));
3875 if (index->ts.type == BT_REAL)
3876 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
3877 &index->where) == FAILURE)
3880 if (index->ts.kind != gfc_index_integer_kind
3881 || index->ts.type != BT_INTEGER)
3884 ts.type = BT_INTEGER;
3885 ts.kind = gfc_index_integer_kind;
3887 gfc_convert_type_warn (index, &ts, 2, 0);
3893 /* Resolve a dim argument to an intrinsic function. */
3896 gfc_resolve_dim_arg (gfc_expr *dim)
3901 if (gfc_resolve_expr (dim) == FAILURE)
3906 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3911 if (dim->ts.type != BT_INTEGER)
3913 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3917 if (dim->ts.kind != gfc_index_integer_kind)
3921 ts.type = BT_INTEGER;
3922 ts.kind = gfc_index_integer_kind;
3924 gfc_convert_type_warn (dim, &ts, 2, 0);
3930 /* Given an expression that contains array references, update those array
3931 references to point to the right array specifications. While this is
3932 filled in during matching, this information is difficult to save and load
3933 in a module, so we take care of it here.
3935 The idea here is that the original array reference comes from the
3936 base symbol. We traverse the list of reference structures, setting
3937 the stored reference to references. Component references can
3938 provide an additional array specification. */
3941 find_array_spec (gfc_expr *e)
3945 gfc_symbol *derived;
3948 if (e->symtree->n.sym->ts.type == BT_CLASS)
3949 as = e->symtree->n.sym->ts.u.derived->components->as;
3951 as = e->symtree->n.sym->as;
3954 for (ref = e->ref; ref; ref = ref->next)
3959 gfc_internal_error ("find_array_spec(): Missing spec");
3966 if (derived == NULL)
3967 derived = e->symtree->n.sym->ts.u.derived;
3969 c = derived->components;
3971 for (; c; c = c->next)
3972 if (c == ref->u.c.component)
3974 /* Track the sequence of component references. */
3975 if (c->ts.type == BT_DERIVED)
3976 derived = c->ts.u.derived;
3981 gfc_internal_error ("find_array_spec(): Component not found");
3983 if (c->attr.dimension)
3986 gfc_internal_error ("find_array_spec(): unused as(1)");
3997 gfc_internal_error ("find_array_spec(): unused as(2)");
4001 /* Resolve an array reference. */
4004 resolve_array_ref (gfc_array_ref *ar)
4006 int i, check_scalar;
4009 for (i = 0; i < ar->dimen; i++)
4011 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4013 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
4015 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4017 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4022 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4026 ar->dimen_type[i] = DIMEN_ELEMENT;
4030 ar->dimen_type[i] = DIMEN_VECTOR;
4031 if (e->expr_type == EXPR_VARIABLE
4032 && e->symtree->n.sym->ts.type == BT_DERIVED)
4033 ar->start[i] = gfc_get_parentheses (e);
4037 gfc_error ("Array index at %L is an array of rank %d",
4038 &ar->c_where[i], e->rank);
4043 /* If the reference type is unknown, figure out what kind it is. */
4045 if (ar->type == AR_UNKNOWN)
4047 ar->type = AR_ELEMENT;
4048 for (i = 0; i < ar->dimen; i++)
4049 if (ar->dimen_type[i] == DIMEN_RANGE
4050 || ar->dimen_type[i] == DIMEN_VECTOR)
4052 ar->type = AR_SECTION;
4057 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4065 resolve_substring (gfc_ref *ref)
4067 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4069 if (ref->u.ss.start != NULL)
4071 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4074 if (ref->u.ss.start->ts.type != BT_INTEGER)
4076 gfc_error ("Substring start index at %L must be of type INTEGER",
4077 &ref->u.ss.start->where);
4081 if (ref->u.ss.start->rank != 0)
4083 gfc_error ("Substring start index at %L must be scalar",
4084 &ref->u.ss.start->where);
4088 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4089 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4090 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4092 gfc_error ("Substring start index at %L is less than one",
4093 &ref->u.ss.start->where);
4098 if (ref->u.ss.end != NULL)
4100 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4103 if (ref->u.ss.end->ts.type != BT_INTEGER)
4105 gfc_error ("Substring end index at %L must be of type INTEGER",
4106 &ref->u.ss.end->where);
4110 if (ref->u.ss.end->rank != 0)
4112 gfc_error ("Substring end index at %L must be scalar",
4113 &ref->u.ss.end->where);
4117 if (ref->u.ss.length != NULL
4118 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4119 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4120 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4122 gfc_error ("Substring end index at %L exceeds the string length",
4123 &ref->u.ss.start->where);
4127 if (compare_bound_mpz_t (ref->u.ss.end,
4128 gfc_integer_kinds[k].huge) == CMP_GT
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 end index at %L is too large",
4133 &ref->u.ss.end->where);
4142 /* This function supplies missing substring charlens. */
4145 gfc_resolve_substring_charlen (gfc_expr *e)
4148 gfc_expr *start, *end;
4150 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4151 if (char_ref->type == REF_SUBSTRING)
4157 gcc_assert (char_ref->next == NULL);
4161 if (e->ts.u.cl->length)
4162 gfc_free_expr (e->ts.u.cl->length);
4163 else if (e->expr_type == EXPR_VARIABLE
4164 && e->symtree->n.sym->attr.dummy)
4168 e->ts.type = BT_CHARACTER;
4169 e->ts.kind = gfc_default_character_kind;
4172 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4174 if (char_ref->u.ss.start)
4175 start = gfc_copy_expr (char_ref->u.ss.start);
4177 start = gfc_int_expr (1);
4179 if (char_ref->u.ss.end)
4180 end = gfc_copy_expr (char_ref->u.ss.end);
4181 else if (e->expr_type == EXPR_VARIABLE)
4182 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4189 /* Length = (end - start +1). */
4190 e->ts.u.cl->length = gfc_subtract (end, start);
4191 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length, gfc_int_expr (1));
4193 e->ts.u.cl->length->ts.type = BT_INTEGER;
4194 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4196 /* Make sure that the length is simplified. */
4197 gfc_simplify_expr (e->ts.u.cl->length, 1);
4198 gfc_resolve_expr (e->ts.u.cl->length);
4202 /* Resolve subtype references. */
4205 resolve_ref (gfc_expr *expr)
4207 int current_part_dimension, n_components, seen_part_dimension;
4210 for (ref = expr->ref; ref; ref = ref->next)
4211 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4213 find_array_spec (expr);
4217 for (ref = expr->ref; ref; ref = ref->next)
4221 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4229 resolve_substring (ref);
4233 /* Check constraints on part references. */
4235 current_part_dimension = 0;
4236 seen_part_dimension = 0;
4239 for (ref = expr->ref; ref; ref = ref->next)
4244 switch (ref->u.ar.type)
4248 current_part_dimension = 1;
4252 current_part_dimension = 0;
4256 gfc_internal_error ("resolve_ref(): Bad array reference");
4262 if (current_part_dimension || seen_part_dimension)
4264 if (ref->u.c.component->attr.pointer)
4266 gfc_error ("Component to the right of a part reference "
4267 "with nonzero rank must not have the POINTER "
4268 "attribute at %L", &expr->where);
4271 else if (ref->u.c.component->attr.allocatable)
4273 gfc_error ("Component to the right of a part reference "
4274 "with nonzero rank must not have the ALLOCATABLE "
4275 "attribute at %L", &expr->where);
4287 if (((ref->type == REF_COMPONENT && n_components > 1)
4288 || ref->next == NULL)
4289 && current_part_dimension
4290 && seen_part_dimension)
4292 gfc_error ("Two or more part references with nonzero rank must "
4293 "not be specified at %L", &expr->where);
4297 if (ref->type == REF_COMPONENT)
4299 if (current_part_dimension)
4300 seen_part_dimension = 1;
4302 /* reset to make sure */
4303 current_part_dimension = 0;
4311 /* Given an expression, determine its shape. This is easier than it sounds.
4312 Leaves the shape array NULL if it is not possible to determine the shape. */
4315 expression_shape (gfc_expr *e)
4317 mpz_t array[GFC_MAX_DIMENSIONS];
4320 if (e->rank == 0 || e->shape != NULL)
4323 for (i = 0; i < e->rank; i++)
4324 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4327 e->shape = gfc_get_shape (e->rank);
4329 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4334 for (i--; i >= 0; i--)
4335 mpz_clear (array[i]);
4339 /* Given a variable expression node, compute the rank of the expression by
4340 examining the base symbol and any reference structures it may have. */
4343 expression_rank (gfc_expr *e)
4348 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4349 could lead to serious confusion... */
4350 gcc_assert (e->expr_type != EXPR_COMPCALL);
4354 if (e->expr_type == EXPR_ARRAY)
4356 /* Constructors can have a rank different from one via RESHAPE(). */
4358 if (e->symtree == NULL)
4364 e->rank = (e->symtree->n.sym->as == NULL)
4365 ? 0 : e->symtree->n.sym->as->rank;
4371 for (ref = e->ref; ref; ref = ref->next)
4373 if (ref->type != REF_ARRAY)
4376 if (ref->u.ar.type == AR_FULL)
4378 rank = ref->u.ar.as->rank;
4382 if (ref->u.ar.type == AR_SECTION)
4384 /* Figure out the rank of the section. */
4386 gfc_internal_error ("expression_rank(): Two array specs");
4388 for (i = 0; i < ref->u.ar.dimen; i++)
4389 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4390 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4400 expression_shape (e);
4404 /* Resolve a variable expression. */
4407 resolve_variable (gfc_expr *e)
4414 if (e->symtree == NULL)
4417 if (e->ref && resolve_ref (e) == FAILURE)
4420 sym = e->symtree->n.sym;
4421 if (sym->attr.flavor == FL_PROCEDURE
4422 && (!sym->attr.function
4423 || (sym->attr.function && sym->result
4424 && sym->result->attr.proc_pointer
4425 && !sym->result->attr.function)))
4427 e->ts.type = BT_PROCEDURE;
4428 goto resolve_procedure;
4431 if (sym->ts.type != BT_UNKNOWN)
4432 gfc_variable_attr (e, &e->ts);
4435 /* Must be a simple variable reference. */
4436 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4441 if (check_assumed_size_reference (sym, e))
4444 /* Deal with forward references to entries during resolve_code, to
4445 satisfy, at least partially, 12.5.2.5. */
4446 if (gfc_current_ns->entries
4447 && current_entry_id == sym->entry_id
4450 && cs_base->current->op != EXEC_ENTRY)
4452 gfc_entry_list *entry;
4453 gfc_formal_arglist *formal;
4457 /* If the symbol is a dummy... */
4458 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4460 entry = gfc_current_ns->entries;
4463 /* ...test if the symbol is a parameter of previous entries. */
4464 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4465 for (formal = entry->sym->formal; formal; formal = formal->next)
4467 if (formal->sym && sym->name == formal->sym->name)
4471 /* If it has not been seen as a dummy, this is an error. */
4474 if (specification_expr)
4475 gfc_error ("Variable '%s', used in a specification expression"
4476 ", is referenced at %L before the ENTRY statement "
4477 "in which it is a parameter",
4478 sym->name, &cs_base->current->loc);
4480 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4481 "statement in which it is a parameter",
4482 sym->name, &cs_base->current->loc);
4487 /* Now do the same check on the specification expressions. */
4488 specification_expr = 1;
4489 if (sym->ts.type == BT_CHARACTER
4490 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
4494 for (n = 0; n < sym->as->rank; n++)
4496 specification_expr = 1;
4497 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4499 specification_expr = 1;
4500 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4503 specification_expr = 0;
4506 /* Update the symbol's entry level. */
4507 sym->entry_id = current_entry_id + 1;
4511 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
4518 /* Checks to see that the correct symbol has been host associated.
4519 The only situation where this arises is that in which a twice
4520 contained function is parsed after the host association is made.
4521 Therefore, on detecting this, change the symbol in the expression
4522 and convert the array reference into an actual arglist if the old
4523 symbol is a variable. */
4525 check_host_association (gfc_expr *e)
4527 gfc_symbol *sym, *old_sym;
4531 gfc_actual_arglist *arg, *tail = NULL;
4532 bool retval = e->expr_type == EXPR_FUNCTION;
4534 /* If the expression is the result of substitution in
4535 interface.c(gfc_extend_expr) because there is no way in
4536 which the host association can be wrong. */
4537 if (e->symtree == NULL
4538 || e->symtree->n.sym == NULL
4539 || e->user_operator)
4542 old_sym = e->symtree->n.sym;
4544 if (gfc_current_ns->parent
4545 && old_sym->ns != gfc_current_ns)
4547 /* Use the 'USE' name so that renamed module symbols are
4548 correctly handled. */
4549 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
4551 if (sym && old_sym != sym
4552 && sym->ts.type == old_sym->ts.type
4553 && sym->attr.flavor == FL_PROCEDURE
4554 && sym->attr.contained)
4556 /* Clear the shape, since it might not be valid. */
4557 if (e->shape != NULL)
4559 for (n = 0; n < e->rank; n++)
4560 mpz_clear (e->shape[n]);
4562 gfc_free (e->shape);
4565 /* Give the expression the right symtree! */
4566 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
4567 gcc_assert (st != NULL);
4569 if (old_sym->attr.flavor == FL_PROCEDURE
4570 || e->expr_type == EXPR_FUNCTION)
4572 /* Original was function so point to the new symbol, since
4573 the actual argument list is already attached to the
4575 e->value.function.esym = NULL;
4580 /* Original was variable so convert array references into
4581 an actual arglist. This does not need any checking now
4582 since gfc_resolve_function will take care of it. */
4583 e->value.function.actual = NULL;
4584 e->expr_type = EXPR_FUNCTION;
4587 /* Ambiguity will not arise if the array reference is not
4588 the last reference. */
4589 for (ref = e->ref; ref; ref = ref->next)
4590 if (ref->type == REF_ARRAY && ref->next == NULL)
4593 gcc_assert (ref->type == REF_ARRAY);
4595 /* Grab the start expressions from the array ref and
4596 copy them into actual arguments. */
4597 for (n = 0; n < ref->u.ar.dimen; n++)
4599 arg = gfc_get_actual_arglist ();
4600 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4601 if (e->value.function.actual == NULL)
4602 tail = e->value.function.actual = arg;
4610 /* Dump the reference list and set the rank. */
4611 gfc_free_ref_list (e->ref);
4613 e->rank = sym->as ? sym->as->rank : 0;
4616 gfc_resolve_expr (e);
4620 /* This might have changed! */
4621 return e->expr_type == EXPR_FUNCTION;
4626 gfc_resolve_character_operator (gfc_expr *e)
4628 gfc_expr *op1 = e->value.op.op1;
4629 gfc_expr *op2 = e->value.op.op2;
4630 gfc_expr *e1 = NULL;
4631 gfc_expr *e2 = NULL;
4633 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
4635 if (op1->ts.u.cl && op1->ts.u.cl->length)
4636 e1 = gfc_copy_expr (op1->ts.u.cl->length);
4637 else if (op1->expr_type == EXPR_CONSTANT)
4638 e1 = gfc_int_expr (op1->value.character.length);
4640 if (op2->ts.u.cl && op2->ts.u.cl->length)
4641 e2 = gfc_copy_expr (op2->ts.u.cl->length);
4642 else if (op2->expr_type == EXPR_CONSTANT)
4643 e2 = gfc_int_expr (op2->value.character.length);
4645 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4650 e->ts.u.cl->length = gfc_add (e1, e2);
4651 e->ts.u.cl->length->ts.type = BT_INTEGER;
4652 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4653 gfc_simplify_expr (e->ts.u.cl->length, 0);
4654 gfc_resolve_expr (e->ts.u.cl->length);
4660 /* Ensure that an character expression has a charlen and, if possible, a
4661 length expression. */
4664 fixup_charlen (gfc_expr *e)
4666 /* The cases fall through so that changes in expression type and the need
4667 for multiple fixes are picked up. In all circumstances, a charlen should
4668 be available for the middle end to hang a backend_decl on. */
4669 switch (e->expr_type)
4672 gfc_resolve_character_operator (e);
4675 if (e->expr_type == EXPR_ARRAY)
4676 gfc_resolve_character_array_constructor (e);
4678 case EXPR_SUBSTRING:
4679 if (!e->ts.u.cl && e->ref)
4680 gfc_resolve_substring_charlen (e);
4684 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4691 /* Update an actual argument to include the passed-object for type-bound
4692 procedures at the right position. */
4694 static gfc_actual_arglist*
4695 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
4698 gcc_assert (argpos > 0);
4702 gfc_actual_arglist* result;
4704 result = gfc_get_actual_arglist ();
4708 result->name = name;
4714 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
4716 lst = update_arglist_pass (NULL, po, argpos - 1, name);
4721 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
4724 extract_compcall_passed_object (gfc_expr* e)
4728 gcc_assert (e->expr_type == EXPR_COMPCALL);
4730 if (e->value.compcall.base_object)
4731 po = gfc_copy_expr (e->value.compcall.base_object);
4734 po = gfc_get_expr ();
4735 po->expr_type = EXPR_VARIABLE;
4736 po->symtree = e->symtree;
4737 po->ref = gfc_copy_ref (e->ref);
4740 if (gfc_resolve_expr (po) == FAILURE)
4747 /* Update the arglist of an EXPR_COMPCALL expression to include the
4751 update_compcall_arglist (gfc_expr* e)
4754 gfc_typebound_proc* tbp;
4756 tbp = e->value.compcall.tbp;
4761 po = extract_compcall_passed_object (e);
4767 gfc_error ("Passed-object at %L must be scalar", &e->where);
4771 if (tbp->nopass || e->value.compcall.ignore_pass)
4777 gcc_assert (tbp->pass_arg_num > 0);
4778 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4786 /* Extract the passed object from a PPC call (a copy of it). */
4789 extract_ppc_passed_object (gfc_expr *e)
4794 po = gfc_get_expr ();
4795 po->expr_type = EXPR_VARIABLE;
4796 po->symtree = e->symtree;
4797 po->ref = gfc_copy_ref (e->ref);
4799 /* Remove PPC reference. */
4801 while ((*ref)->next)
4802 (*ref) = (*ref)->next;
4803 gfc_free_ref_list (*ref);
4806 if (gfc_resolve_expr (po) == FAILURE)
4813 /* Update the actual arglist of a procedure pointer component to include the
4817 update_ppc_arglist (gfc_expr* e)
4821 gfc_typebound_proc* tb;
4823 if (!gfc_is_proc_ptr_comp (e, &ppc))
4830 else if (tb->nopass)
4833 po = extract_ppc_passed_object (e);
4839 gfc_error ("Passed-object at %L must be scalar", &e->where);
4843 gcc_assert (tb->pass_arg_num > 0);
4844 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4852 /* Check that the object a TBP is called on is valid, i.e. it must not be
4853 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
4856 check_typebound_baseobject (gfc_expr* e)
4860 base = extract_compcall_passed_object (e);
4864 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
4866 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
4868 gfc_error ("Base object for type-bound procedure call at %L is of"
4869 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
4877 /* Resolve a call to a type-bound procedure, either function or subroutine,
4878 statically from the data in an EXPR_COMPCALL expression. The adapted
4879 arglist and the target-procedure symtree are returned. */
4882 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
4883 gfc_actual_arglist** actual)
4885 gcc_assert (e->expr_type == EXPR_COMPCALL);
4886 gcc_assert (!e->value.compcall.tbp->is_generic);
4888 /* Update the actual arglist for PASS. */
4889 if (update_compcall_arglist (e) == FAILURE)
4892 *actual = e->value.compcall.actual;
4893 *target = e->value.compcall.tbp->u.specific;
4895 gfc_free_ref_list (e->ref);
4897 e->value.compcall.actual = NULL;
4903 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
4904 which of the specific bindings (if any) matches the arglist and transform
4905 the expression into a call of that binding. */
4908 resolve_typebound_generic_call (gfc_expr* e)
4910 gfc_typebound_proc* genproc;
4911 const char* genname;
4913 gcc_assert (e->expr_type == EXPR_COMPCALL);
4914 genname = e->value.compcall.name;
4915 genproc = e->value.compcall.tbp;
4917 if (!genproc->is_generic)
4920 /* Try the bindings on this type and in the inheritance hierarchy. */
4921 for (; genproc; genproc = genproc->overridden)
4925 gcc_assert (genproc->is_generic);
4926 for (g = genproc->u.generic; g; g = g->next)
4929 gfc_actual_arglist* args;
4932 gcc_assert (g->specific);
4934 if (g->specific->error)
4937 target = g->specific->u.specific->n.sym;
4939 /* Get the right arglist by handling PASS/NOPASS. */
4940 args = gfc_copy_actual_arglist (e->value.compcall.actual);
4941 if (!g->specific->nopass)
4944 po = extract_compcall_passed_object (e);
4948 gcc_assert (g->specific->pass_arg_num > 0);
4949 gcc_assert (!g->specific->error);
4950 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
4951 g->specific->pass_arg);
4953 resolve_actual_arglist (args, target->attr.proc,
4954 is_external_proc (target) && !target->formal);
4956 /* Check if this arglist matches the formal. */
4957 matches = gfc_arglist_matches_symbol (&args, target);
4959 /* Clean up and break out of the loop if we've found it. */
4960 gfc_free_actual_arglist (args);
4963 e->value.compcall.tbp = g->specific;
4969 /* Nothing matching found! */
4970 gfc_error ("Found no matching specific binding for the call to the GENERIC"
4971 " '%s' at %L", genname, &e->where);
4979 /* Resolve a call to a type-bound subroutine. */
4982 resolve_typebound_call (gfc_code* c)
4984 gfc_actual_arglist* newactual;
4985 gfc_symtree* target;
4987 /* Check that's really a SUBROUTINE. */
4988 if (!c->expr1->value.compcall.tbp->subroutine)
4990 gfc_error ("'%s' at %L should be a SUBROUTINE",
4991 c->expr1->value.compcall.name, &c->loc);
4995 if (check_typebound_baseobject (c->expr1) == FAILURE)
4998 if (resolve_typebound_generic_call (c->expr1) == FAILURE)
5001 /* Transform into an ordinary EXEC_CALL for now. */
5003 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5006 c->ext.actual = newactual;
5007 c->symtree = target;
5008 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5010 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5012 gfc_free_expr (c->expr1);
5013 c->expr1 = gfc_get_expr ();
5014 c->expr1->expr_type = EXPR_FUNCTION;
5015 c->expr1->symtree = target;
5016 c->expr1->where = c->loc;
5018 return resolve_call (c);
5022 /* Resolve a component-call expression. This originally was intended
5023 only to see functions. However, it is convenient to use it in
5024 resolving subroutine class methods, since we do not have to add a
5025 gfc_code each time. */
5027 resolve_compcall (gfc_expr* e, bool fcn)
5029 gfc_actual_arglist* newactual;
5030 gfc_symtree* target;
5032 /* Check that's really a FUNCTION. */
5033 if (fcn && !e->value.compcall.tbp->function)
5035 gfc_error ("'%s' at %L should be a FUNCTION",
5036 e->value.compcall.name, &e->where);
5039 else if (!fcn && !e->value.compcall.tbp->subroutine)
5041 /* To resolve class member calls, we borrow this bit
5042 of code to select the specific procedures. */
5043 gfc_error ("'%s' at %L should be a SUBROUTINE",
5044 e->value.compcall.name, &e->where);
5048 /* These must not be assign-calls! */
5049 gcc_assert (!e->value.compcall.assign);
5051 if (check_typebound_baseobject (e) == FAILURE)
5054 if (resolve_typebound_generic_call (e) == FAILURE)
5056 gcc_assert (!e->value.compcall.tbp->is_generic);
5058 /* Take the rank from the function's symbol. */
5059 if (e->value.compcall.tbp->u.specific->n.sym->as)
5060 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5062 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5063 arglist to the TBP's binding target. */
5065 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5068 e->value.function.actual = newactual;
5069 e->value.function.name = e->value.compcall.name;
5070 e->value.function.esym = target->n.sym;
5071 e->value.function.class_esym = NULL;
5072 e->value.function.isym = NULL;
5073 e->symtree = target;
5074 e->ts = target->n.sym->ts;
5075 e->expr_type = EXPR_FUNCTION;
5077 /* Resolution is not necessary if this is a class subroutine; this
5078 function only has to identify the specific proc. Resolution of
5079 the call will be done next in resolve_typebound_call. */
5080 return fcn ? gfc_resolve_expr (e) : SUCCESS;
5084 /* Resolve a typebound call for the members in a class. This group of
5085 functions implements dynamic dispatch in the provisional version
5086 of f03 OOP. As soon as vtables are in place and contain pointers
5087 to methods, this will no longer be necessary. */
5088 static gfc_expr *list_e;
5089 static void check_class_members (gfc_symbol *);
5090 static gfc_try class_try;
5091 static bool fcn_flag;
5092 static gfc_symbol *class_object;
5096 check_members (gfc_symbol *derived)
5098 if (derived->attr.flavor == FL_DERIVED)
5099 check_class_members (derived);
5104 check_class_members (gfc_symbol *derived)
5106 gfc_symbol* tbp_sym;
5109 gfc_class_esym_list *etmp;
5111 e = gfc_copy_expr (list_e);
5113 tbp = gfc_find_typebound_proc (derived, &class_try,
5114 e->value.compcall.name,
5119 gfc_error ("no typebound available procedure named '%s' at %L",
5120 e->value.compcall.name, &e->where);
5124 if (tbp->n.tb->is_generic)
5128 /* If we have to match a passed class member, force the actual
5129 expression to have the correct type. */
5130 if (!tbp->n.tb->nopass)
5132 if (e->value.compcall.base_object == NULL)
5133 e->value.compcall.base_object =
5134 extract_compcall_passed_object (e);
5136 e->value.compcall.base_object->ts.type = BT_DERIVED;
5137 e->value.compcall.base_object->ts.u.derived = derived;
5141 tbp_sym = tbp->n.tb->u.specific->n.sym;
5143 e->value.compcall.tbp = tbp->n.tb;
5144 e->value.compcall.name = tbp->name;
5146 /* Let the original expresssion catch the assertion in
5147 resolve_compcall, since this flag does not appear to be reset or
5148 copied in some systems. */
5149 e->value.compcall.assign = 0;
5151 /* Do the renaming, PASSing, generic => specific and other
5152 good things for each class member. */
5153 class_try = (resolve_compcall (e, fcn_flag) == SUCCESS)
5154 ? class_try : FAILURE;
5156 /* Now transfer the found symbol to the esym list. */
5157 if (class_try == SUCCESS)
5159 etmp = list_e->value.function.class_esym;
5160 list_e->value.function.class_esym
5161 = gfc_get_class_esym_list();
5162 list_e->value.function.class_esym->next = etmp;
5163 list_e->value.function.class_esym->derived = derived;
5164 list_e->value.function.class_esym->esym
5165 = e->value.function.esym;
5170 /* Burrow down into grandchildren types. */
5171 if (derived->f2k_derived)
5172 gfc_traverse_ns (derived->f2k_derived, check_members);
5176 /* Eliminate esym_lists where all the members point to the
5177 typebound procedure of the declared type; ie. one where
5178 type selection has no effect.. */
5180 resolve_class_esym (gfc_expr *e)
5182 gfc_class_esym_list *p, *q;
5185 gcc_assert (e && e->expr_type == EXPR_FUNCTION);
5187 p = e->value.function.class_esym;
5191 for (; p; p = p->next)
5192 empty = empty && (e->value.function.esym == p->esym);
5196 p = e->value.function.class_esym;
5202 e->value.function.class_esym = NULL;
5207 /* Generate an expression for the vindex, given the reference to
5208 the class of the final expression (class_ref), the base of the
5209 full reference list (new_ref), the declared type and the class
5212 vindex_expr (gfc_ref *class_ref, gfc_ref *new_ref,
5213 gfc_symbol *declared, gfc_symtree *st)
5218 /* Build an expression for the correct vindex; ie. that of the last
5220 ref = gfc_get_ref();
5221 ref->type = REF_COMPONENT;
5222 ref->u.c.component = declared->components->next;
5223 ref->u.c.sym = declared;
5227 class_ref->next = ref;
5231 gfc_free_ref_list (new_ref);
5234 vindex = gfc_get_expr ();
5235 vindex->expr_type = EXPR_VARIABLE;
5236 vindex->symtree = st;
5237 vindex->symtree->n.sym->refs++;
5238 vindex->ts = ref->u.c.component->ts;
5239 vindex->ref = new_ref;
5245 /* Get the ultimate declared type from an expression. In addition,
5246 return the last class/derived type reference and the copy of the
5249 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5252 gfc_symbol *declared;
5257 *new_ref = gfc_copy_ref (e->ref);
5258 for (ref = *new_ref; ref; ref = ref->next)
5260 if (ref->type != REF_COMPONENT)
5263 if (ref->u.c.component->ts.type == BT_CLASS
5264 || ref->u.c.component->ts.type == BT_DERIVED)
5266 declared = ref->u.c.component->ts.u.derived;
5271 if (declared == NULL)
5272 declared = e->symtree->n.sym->ts.u.derived;
5278 /* Resolve the argument expressions so that any arguments expressions
5279 that include class methods are resolved before the current call.
5280 This is necessary because of the static variables used in CLASS
5281 method resolution. */
5283 resolve_arg_exprs (gfc_actual_arglist *arg)
5285 /* Resolve the actual arglist expressions. */
5286 for (; arg; arg = arg->next)
5289 gfc_resolve_expr (arg->expr);
5294 /* Resolve a CLASS typebound function, or 'method'. */
5296 resolve_class_compcall (gfc_expr* e)
5298 gfc_symbol *derived, *declared;
5304 class_object = st->n.sym;
5306 /* Get the CLASS declared type. */
5307 declared = get_declared_from_expr (&class_ref, &new_ref, e);
5309 /* Weed out cases of the ultimate component being a derived type. */
5310 if (class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5312 gfc_free_ref_list (new_ref);
5313 return resolve_compcall (e, true);
5316 /* Resolve the argument expressions, */
5317 resolve_arg_exprs (e->value.function.actual);
5319 /* Get the data component, which is of the declared type. */
5320 derived = declared->components->ts.u.derived;
5322 /* Resolve the function call for each member of the class. */
5323 class_try = SUCCESS;
5325 list_e = gfc_copy_expr (e);
5326 check_class_members (derived);
5328 class_try = (resolve_compcall (e, true) == SUCCESS)
5329 ? class_try : FAILURE;
5331 /* Transfer the class list to the original expression. Note that
5332 the class_esym list is cleaned up in trans-expr.c, as the calls
5334 e->value.function.class_esym = list_e->value.function.class_esym;
5335 list_e->value.function.class_esym = NULL;
5336 gfc_free_expr (list_e);
5338 resolve_class_esym (e);
5340 /* More than one typebound procedure so transmit an expression for
5341 the vindex as the selector. */
5342 if (e->value.function.class_esym != NULL)
5343 e->value.function.class_esym->vindex
5344 = vindex_expr (class_ref, new_ref, declared, st);
5349 /* Resolve a CLASS typebound subroutine, or 'method'. */
5351 resolve_class_typebound_call (gfc_code *code)
5353 gfc_symbol *derived, *declared;
5358 st = code->expr1->symtree;
5359 class_object = st->n.sym;
5361 /* Get the CLASS declared type. */
5362 declared = get_declared_from_expr (&class_ref, &new_ref, code->expr1);
5364 /* Weed out cases of the ultimate component being a derived type. */
5365 if (class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5367 gfc_free_ref_list (new_ref);
5368 return resolve_typebound_call (code);
5371 /* Resolve the argument expressions, */
5372 resolve_arg_exprs (code->expr1->value.compcall.actual);
5374 /* Get the data component, which is of the declared type. */
5375 derived = declared->components->ts.u.derived;
5377 class_try = SUCCESS;
5379 list_e = gfc_copy_expr (code->expr1);
5380 check_class_members (derived);
5382 class_try = (resolve_typebound_call (code) == SUCCESS)
5383 ? class_try : FAILURE;
5385 /* Transfer the class list to the original expression. Note that
5386 the class_esym list is cleaned up in trans-expr.c, as the calls
5388 code->expr1->value.function.class_esym
5389 = list_e->value.function.class_esym;
5390 list_e->value.function.class_esym = NULL;
5391 gfc_free_expr (list_e);
5393 resolve_class_esym (code->expr1);
5395 /* More than one typebound procedure so transmit an expression for
5396 the vindex as the selector. */
5397 if (code->expr1->value.function.class_esym != NULL)
5398 code->expr1->value.function.class_esym->vindex
5399 = vindex_expr (class_ref, new_ref, declared, st);
5405 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5408 resolve_ppc_call (gfc_code* c)
5410 gfc_component *comp;
5413 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
5416 c->resolved_sym = c->expr1->symtree->n.sym;
5417 c->expr1->expr_type = EXPR_VARIABLE;
5419 if (!comp->attr.subroutine)
5420 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5422 if (resolve_ref (c->expr1) == FAILURE)
5425 if (update_ppc_arglist (c->expr1) == FAILURE)
5428 c->ext.actual = c->expr1->value.compcall.actual;
5430 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5431 comp->formal == NULL) == FAILURE)
5434 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5440 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5443 resolve_expr_ppc (gfc_expr* e)
5445 gfc_component *comp;
5448 b = gfc_is_proc_ptr_comp (e, &comp);
5451 /* Convert to EXPR_FUNCTION. */
5452 e->expr_type = EXPR_FUNCTION;
5453 e->value.function.isym = NULL;
5454 e->value.function.actual = e->value.compcall.actual;
5456 if (comp->as != NULL)
5457 e->rank = comp->as->rank;
5459 if (!comp->attr.function)
5460 gfc_add_function (&comp->attr, comp->name, &e->where);
5462 if (resolve_ref (e) == FAILURE)
5465 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5466 comp->formal == NULL) == FAILURE)
5469 if (update_ppc_arglist (e) == FAILURE)
5472 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
5478 /* Resolve an expression. That is, make sure that types of operands agree
5479 with their operators, intrinsic operators are converted to function calls
5480 for overloaded types and unresolved function references are resolved. */
5483 gfc_resolve_expr (gfc_expr *e)
5490 switch (e->expr_type)
5493 t = resolve_operator (e);
5499 if (check_host_association (e))
5500 t = resolve_function (e);
5503 t = resolve_variable (e);
5505 expression_rank (e);
5508 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
5509 && e->ref->type != REF_SUBSTRING)
5510 gfc_resolve_substring_charlen (e);
5515 if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
5516 t = resolve_class_compcall (e);
5518 t = resolve_compcall (e, true);
5521 case EXPR_SUBSTRING:
5522 t = resolve_ref (e);
5531 t = resolve_expr_ppc (e);
5536 if (resolve_ref (e) == FAILURE)
5539 t = gfc_resolve_array_constructor (e);
5540 /* Also try to expand a constructor. */
5543 expression_rank (e);
5544 gfc_expand_constructor (e);
5547 /* This provides the opportunity for the length of constructors with
5548 character valued function elements to propagate the string length
5549 to the expression. */
5550 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
5551 t = gfc_resolve_character_array_constructor (e);
5555 case EXPR_STRUCTURE:
5556 t = resolve_ref (e);
5560 t = resolve_structure_cons (e);
5564 t = gfc_simplify_expr (e, 0);
5568 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
5571 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
5578 /* Resolve an expression from an iterator. They must be scalar and have
5579 INTEGER or (optionally) REAL type. */
5582 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
5583 const char *name_msgid)
5585 if (gfc_resolve_expr (expr) == FAILURE)
5588 if (expr->rank != 0)
5590 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
5594 if (expr->ts.type != BT_INTEGER)
5596 if (expr->ts.type == BT_REAL)
5599 return gfc_notify_std (GFC_STD_F95_DEL,
5600 "Deleted feature: %s at %L must be integer",
5601 _(name_msgid), &expr->where);
5604 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
5611 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
5619 /* Resolve the expressions in an iterator structure. If REAL_OK is
5620 false allow only INTEGER type iterators, otherwise allow REAL types. */
5623 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
5625 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
5629 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
5631 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5636 if (gfc_resolve_iterator_expr (iter->start, real_ok,
5637 "Start expression in DO loop") == FAILURE)
5640 if (gfc_resolve_iterator_expr (iter->end, real_ok,
5641 "End expression in DO loop") == FAILURE)
5644 if (gfc_resolve_iterator_expr (iter->step, real_ok,
5645 "Step expression in DO loop") == FAILURE)
5648 if (iter->step->expr_type == EXPR_CONSTANT)
5650 if ((iter->step->ts.type == BT_INTEGER
5651 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
5652 || (iter->step->ts.type == BT_REAL
5653 && mpfr_sgn (iter->step->value.real) == 0))
5655 gfc_error ("Step expression in DO loop at %L cannot be zero",
5656 &iter->step->where);
5661 /* Convert start, end, and step to the same type as var. */
5662 if (iter->start->ts.kind != iter->var->ts.kind
5663 || iter->start->ts.type != iter->var->ts.type)
5664 gfc_convert_type (iter->start, &iter->var->ts, 2);
5666 if (iter->end->ts.kind != iter->var->ts.kind
5667 || iter->end->ts.type != iter->var->ts.type)
5668 gfc_convert_type (iter->end, &iter->var->ts, 2);
5670 if (iter->step->ts.kind != iter->var->ts.kind
5671 || iter->step->ts.type != iter->var->ts.type)
5672 gfc_convert_type (iter->step, &iter->var->ts, 2);
5674 if (iter->start->expr_type == EXPR_CONSTANT
5675 && iter->end->expr_type == EXPR_CONSTANT
5676 && iter->step->expr_type == EXPR_CONSTANT)
5679 if (iter->start->ts.type == BT_INTEGER)
5681 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
5682 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
5686 sgn = mpfr_sgn (iter->step->value.real);
5687 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
5689 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
5690 gfc_warning ("DO loop at %L will be executed zero times",
5691 &iter->step->where);
5698 /* Traversal function for find_forall_index. f == 2 signals that
5699 that variable itself is not to be checked - only the references. */
5702 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
5704 if (expr->expr_type != EXPR_VARIABLE)
5707 /* A scalar assignment */
5708 if (!expr->ref || *f == 1)
5710 if (expr->symtree->n.sym == sym)
5722 /* Check whether the FORALL index appears in the expression or not.
5723 Returns SUCCESS if SYM is found in EXPR. */
5726 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
5728 if (gfc_traverse_expr (expr, sym, forall_index, f))
5735 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5736 to be a scalar INTEGER variable. The subscripts and stride are scalar
5737 INTEGERs, and if stride is a constant it must be nonzero.
5738 Furthermore "A subscript or stride in a forall-triplet-spec shall
5739 not contain a reference to any index-name in the
5740 forall-triplet-spec-list in which it appears." (7.5.4.1) */
5743 resolve_forall_iterators (gfc_forall_iterator *it)
5745 gfc_forall_iterator *iter, *iter2;
5747 for (iter = it; iter; iter = iter->next)
5749 if (gfc_resolve_expr (iter->var) == SUCCESS
5750 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
5751 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
5754 if (gfc_resolve_expr (iter->start) == SUCCESS
5755 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
5756 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
5757 &iter->start->where);
5758 if (iter->var->ts.kind != iter->start->ts.kind)
5759 gfc_convert_type (iter->start, &iter->var->ts, 2);
5761 if (gfc_resolve_expr (iter->end) == SUCCESS
5762 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
5763 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
5765 if (iter->var->ts.kind != iter->end->ts.kind)
5766 gfc_convert_type (iter->end, &iter->var->ts, 2);
5768 if (gfc_resolve_expr (iter->stride) == SUCCESS)
5770 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
5771 gfc_error ("FORALL stride expression at %L must be a scalar %s",
5772 &iter->stride->where, "INTEGER");
5774 if (iter->stride->expr_type == EXPR_CONSTANT
5775 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
5776 gfc_error ("FORALL stride expression at %L cannot be zero",
5777 &iter->stride->where);
5779 if (iter->var->ts.kind != iter->stride->ts.kind)
5780 gfc_convert_type (iter->stride, &iter->var->ts, 2);
5783 for (iter = it; iter; iter = iter->next)
5784 for (iter2 = iter; iter2; iter2 = iter2->next)
5786 if (find_forall_index (iter2->start,
5787 iter->var->symtree->n.sym, 0) == SUCCESS
5788 || find_forall_index (iter2->end,
5789 iter->var->symtree->n.sym, 0) == SUCCESS
5790 || find_forall_index (iter2->stride,
5791 iter->var->symtree->n.sym, 0) == SUCCESS)
5792 gfc_error ("FORALL index '%s' may not appear in triplet "
5793 "specification at %L", iter->var->symtree->name,
5794 &iter2->start->where);
5799 /* Given a pointer to a symbol that is a derived type, see if it's
5800 inaccessible, i.e. if it's defined in another module and the components are
5801 PRIVATE. The search is recursive if necessary. Returns zero if no
5802 inaccessible components are found, nonzero otherwise. */
5805 derived_inaccessible (gfc_symbol *sym)
5809 if (sym->attr.use_assoc && sym->attr.private_comp)
5812 for (c = sym->components; c; c = c->next)
5814 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
5822 /* Resolve the argument of a deallocate expression. The expression must be
5823 a pointer or a full array. */
5826 resolve_deallocate_expr (gfc_expr *e)
5828 symbol_attribute attr;
5829 int allocatable, pointer, check_intent_in;
5834 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5835 check_intent_in = 1;
5837 if (gfc_resolve_expr (e) == FAILURE)
5840 if (e->expr_type != EXPR_VARIABLE)
5843 sym = e->symtree->n.sym;
5845 if (sym->ts.type == BT_CLASS)
5847 allocatable = sym->ts.u.derived->components->attr.allocatable;
5848 pointer = sym->ts.u.derived->components->attr.pointer;
5852 allocatable = sym->attr.allocatable;
5853 pointer = sym->attr.pointer;
5855 for (ref = e->ref; ref; ref = ref->next)
5858 check_intent_in = 0;
5863 if (ref->u.ar.type != AR_FULL)
5868 c = ref->u.c.component;
5869 if (c->ts.type == BT_CLASS)
5871 allocatable = c->ts.u.derived->components->attr.allocatable;
5872 pointer = c->ts.u.derived->components->attr.pointer;
5876 allocatable = c->attr.allocatable;
5877 pointer = c->attr.pointer;
5887 attr = gfc_expr_attr (e);
5889 if (allocatable == 0 && attr.pointer == 0)
5892 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5896 if (check_intent_in && sym->attr.intent == INTENT_IN)
5898 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
5899 sym->name, &e->where);
5903 if (e->ts.type == BT_CLASS)
5905 /* Only deallocate the DATA component. */
5906 gfc_add_component_ref (e, "$data");
5913 /* Returns true if the expression e contains a reference to the symbol sym. */
5915 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5917 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
5924 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
5926 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
5930 /* Given the expression node e for an allocatable/pointer of derived type to be
5931 allocated, get the expression node to be initialized afterwards (needed for
5932 derived types with default initializers, and derived types with allocatable
5933 components that need nullification.) */
5936 gfc_expr_to_initialize (gfc_expr *e)
5942 result = gfc_copy_expr (e);
5944 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
5945 for (ref = result->ref; ref; ref = ref->next)
5946 if (ref->type == REF_ARRAY && ref->next == NULL)
5948 ref->u.ar.type = AR_FULL;
5950 for (i = 0; i < ref->u.ar.dimen; i++)
5951 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
5953 result->rank = ref->u.ar.dimen;
5961 /* Resolve the expression in an ALLOCATE statement, doing the additional
5962 checks to see whether the expression is OK or not. The expression must
5963 have a trailing array reference that gives the size of the array. */
5966 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
5968 int i, pointer, allocatable, dimension, check_intent_in, is_abstract;
5969 symbol_attribute attr;
5970 gfc_ref *ref, *ref2;
5976 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5977 check_intent_in = 1;
5979 if (gfc_resolve_expr (e) == FAILURE)
5982 /* Make sure the expression is allocatable or a pointer. If it is
5983 pointer, the next-to-last reference must be a pointer. */
5987 sym = e->symtree->n.sym;
5989 /* Check whether ultimate component is abstract and CLASS. */
5992 if (e->expr_type != EXPR_VARIABLE)
5995 attr = gfc_expr_attr (e);
5996 pointer = attr.pointer;
5997 dimension = attr.dimension;
6001 if (sym->ts.type == BT_CLASS)
6003 allocatable = sym->ts.u.derived->components->attr.allocatable;
6004 pointer = sym->ts.u.derived->components->attr.pointer;
6005 dimension = sym->ts.u.derived->components->attr.dimension;
6006 is_abstract = sym->ts.u.derived->components->attr.abstract;
6010 allocatable = sym->attr.allocatable;
6011 pointer = sym->attr.pointer;
6012 dimension = sym->attr.dimension;
6015 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6018 check_intent_in = 0;
6023 if (ref->next != NULL)
6028 c = ref->u.c.component;
6029 if (c->ts.type == BT_CLASS)
6031 allocatable = c->ts.u.derived->components->attr.allocatable;
6032 pointer = c->ts.u.derived->components->attr.pointer;
6033 dimension = c->ts.u.derived->components->attr.dimension;
6034 is_abstract = c->ts.u.derived->components->attr.abstract;
6038 allocatable = c->attr.allocatable;
6039 pointer = c->attr.pointer;
6040 dimension = c->attr.dimension;
6041 is_abstract = c->attr.abstract;
6053 if (allocatable == 0 && pointer == 0)
6055 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6060 if (is_abstract && !code->expr3 && code->ext.alloc.ts.type == BT_UNKNOWN)
6062 gcc_assert (e->ts.type == BT_CLASS);
6063 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6064 "type-spec or SOURCE=", sym->name, &e->where);
6068 if (check_intent_in && sym->attr.intent == INTENT_IN)
6070 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
6071 sym->name, &e->where);
6075 if (pointer || dimension == 0)
6078 /* Make sure the next-to-last reference node is an array specification. */
6080 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
6082 gfc_error ("Array specification required in ALLOCATE statement "
6083 "at %L", &e->where);
6087 /* Make sure that the array section reference makes sense in the
6088 context of an ALLOCATE specification. */
6092 for (i = 0; i < ar->dimen; i++)
6094 if (ref2->u.ar.type == AR_ELEMENT)
6097 switch (ar->dimen_type[i])
6103 if (ar->start[i] != NULL
6104 && ar->end[i] != NULL
6105 && ar->stride[i] == NULL)
6108 /* Fall Through... */
6112 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6119 for (a = code->ext.alloc.list; a; a = a->next)
6121 sym = a->expr->symtree->n.sym;
6123 /* TODO - check derived type components. */
6124 if (sym->ts.type == BT_DERIVED)
6127 if ((ar->start[i] != NULL
6128 && gfc_find_sym_in_expr (sym, ar->start[i]))
6129 || (ar->end[i] != NULL
6130 && gfc_find_sym_in_expr (sym, ar->end[i])))
6132 gfc_error ("'%s' must not appear in the array specification at "
6133 "%L in the same ALLOCATE statement where it is "
6134 "itself allocated", sym->name, &ar->where);
6144 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6146 gfc_expr *stat, *errmsg, *pe, *qe;
6147 gfc_alloc *a, *p, *q;
6149 stat = code->expr1 ? code->expr1 : NULL;
6151 errmsg = code->expr2 ? code->expr2 : NULL;
6153 /* Check the stat variable. */
6156 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
6157 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
6158 stat->symtree->n.sym->name, &stat->where);
6160 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
6161 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
6164 if ((stat->ts.type != BT_INTEGER
6165 && !(stat->ref && (stat->ref->type == REF_ARRAY
6166 || stat->ref->type == REF_COMPONENT)))
6168 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6169 "variable", &stat->where);
6171 for (p = code->ext.alloc.list; p; p = p->next)
6172 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
6173 gfc_error ("Stat-variable at %L shall not be %sd within "
6174 "the same %s statement", &stat->where, fcn, fcn);
6177 /* Check the errmsg variable. */
6181 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6184 if (errmsg->symtree->n.sym->attr.intent == INTENT_IN)
6185 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
6186 errmsg->symtree->n.sym->name, &errmsg->where);
6188 if (gfc_pure (NULL) && gfc_impure_variable (errmsg->symtree->n.sym))
6189 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
6192 if ((errmsg->ts.type != BT_CHARACTER
6194 && (errmsg->ref->type == REF_ARRAY
6195 || errmsg->ref->type == REF_COMPONENT)))
6196 || errmsg->rank > 0 )
6197 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
6198 "variable", &errmsg->where);
6200 for (p = code->ext.alloc.list; p; p = p->next)
6201 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
6202 gfc_error ("Errmsg-variable at %L shall not be %sd within "
6203 "the same %s statement", &errmsg->where, fcn, fcn);
6206 /* Check that an allocate-object appears only once in the statement.
6207 FIXME: Checking derived types is disabled. */
6208 for (p = code->ext.alloc.list; p; p = p->next)
6211 if ((pe->ref && pe->ref->type != REF_COMPONENT)
6212 && (pe->symtree->n.sym->ts.type != BT_DERIVED))
6214 for (q = p->next; q; q = q->next)
6217 if ((qe->ref && qe->ref->type != REF_COMPONENT)
6218 && (qe->symtree->n.sym->ts.type != BT_DERIVED)
6219 && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
6220 gfc_error ("Allocate-object at %L also appears at %L",
6221 &pe->where, &qe->where);
6226 if (strcmp (fcn, "ALLOCATE") == 0)
6228 for (a = code->ext.alloc.list; a; a = a->next)
6229 resolve_allocate_expr (a->expr, code);
6233 for (a = code->ext.alloc.list; a; a = a->next)
6234 resolve_deallocate_expr (a->expr);
6239 /************ SELECT CASE resolution subroutines ************/
6241 /* Callback function for our mergesort variant. Determines interval
6242 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
6243 op1 > op2. Assumes we're not dealing with the default case.
6244 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
6245 There are nine situations to check. */
6248 compare_cases (const gfc_case *op1, const gfc_case *op2)
6252 if (op1->low == NULL) /* op1 = (:L) */
6254 /* op2 = (:N), so overlap. */
6256 /* op2 = (M:) or (M:N), L < M */
6257 if (op2->low != NULL
6258 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6261 else if (op1->high == NULL) /* op1 = (K:) */
6263 /* op2 = (M:), so overlap. */
6265 /* op2 = (:N) or (M:N), K > N */
6266 if (op2->high != NULL
6267 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6270 else /* op1 = (K:L) */
6272 if (op2->low == NULL) /* op2 = (:N), K > N */
6273 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6275 else if (op2->high == NULL) /* op2 = (M:), L < M */
6276 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6278 else /* op2 = (M:N) */
6282 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6285 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6294 /* Merge-sort a double linked case list, detecting overlap in the
6295 process. LIST is the head of the double linked case list before it
6296 is sorted. Returns the head of the sorted list if we don't see any
6297 overlap, or NULL otherwise. */
6300 check_case_overlap (gfc_case *list)
6302 gfc_case *p, *q, *e, *tail;
6303 int insize, nmerges, psize, qsize, cmp, overlap_seen;
6305 /* If the passed list was empty, return immediately. */
6312 /* Loop unconditionally. The only exit from this loop is a return
6313 statement, when we've finished sorting the case list. */
6320 /* Count the number of merges we do in this pass. */
6323 /* Loop while there exists a merge to be done. */
6328 /* Count this merge. */
6331 /* Cut the list in two pieces by stepping INSIZE places
6332 forward in the list, starting from P. */
6335 for (i = 0; i < insize; i++)
6344 /* Now we have two lists. Merge them! */
6345 while (psize > 0 || (qsize > 0 && q != NULL))
6347 /* See from which the next case to merge comes from. */
6350 /* P is empty so the next case must come from Q. */
6355 else if (qsize == 0 || q == NULL)
6364 cmp = compare_cases (p, q);
6367 /* The whole case range for P is less than the
6375 /* The whole case range for Q is greater than
6376 the case range for P. */
6383 /* The cases overlap, or they are the same
6384 element in the list. Either way, we must
6385 issue an error and get the next case from P. */
6386 /* FIXME: Sort P and Q by line number. */
6387 gfc_error ("CASE label at %L overlaps with CASE "
6388 "label at %L", &p->where, &q->where);
6396 /* Add the next element to the merged list. */
6405 /* P has now stepped INSIZE places along, and so has Q. So
6406 they're the same. */
6411 /* If we have done only one merge or none at all, we've
6412 finished sorting the cases. */
6421 /* Otherwise repeat, merging lists twice the size. */
6427 /* Check to see if an expression is suitable for use in a CASE statement.
6428 Makes sure that all case expressions are scalar constants of the same
6429 type. Return FAILURE if anything is wrong. */
6432 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
6434 if (e == NULL) return SUCCESS;
6436 if (e->ts.type != case_expr->ts.type)
6438 gfc_error ("Expression in CASE statement at %L must be of type %s",
6439 &e->where, gfc_basic_typename (case_expr->ts.type));
6443 /* C805 (R808) For a given case-construct, each case-value shall be of
6444 the same type as case-expr. For character type, length differences
6445 are allowed, but the kind type parameters shall be the same. */
6447 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
6449 gfc_error ("Expression in CASE statement at %L must be of kind %d",
6450 &e->where, case_expr->ts.kind);
6454 /* Convert the case value kind to that of case expression kind, if needed.
6455 FIXME: Should a warning be issued? */
6456 if (e->ts.kind != case_expr->ts.kind)
6457 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
6461 gfc_error ("Expression in CASE statement at %L must be scalar",
6470 /* Given a completely parsed select statement, we:
6472 - Validate all expressions and code within the SELECT.
6473 - Make sure that the selection expression is not of the wrong type.
6474 - Make sure that no case ranges overlap.
6475 - Eliminate unreachable cases and unreachable code resulting from
6476 removing case labels.
6478 The standard does allow unreachable cases, e.g. CASE (5:3). But
6479 they are a hassle for code generation, and to prevent that, we just
6480 cut them out here. This is not necessary for overlapping cases
6481 because they are illegal and we never even try to generate code.
6483 We have the additional caveat that a SELECT construct could have
6484 been a computed GOTO in the source code. Fortunately we can fairly
6485 easily work around that here: The case_expr for a "real" SELECT CASE
6486 is in code->expr1, but for a computed GOTO it is in code->expr2. All
6487 we have to do is make sure that the case_expr is a scalar integer
6491 resolve_select (gfc_code *code)
6494 gfc_expr *case_expr;
6495 gfc_case *cp, *default_case, *tail, *head;
6496 int seen_unreachable;
6502 if (code->expr1 == NULL)
6504 /* This was actually a computed GOTO statement. */
6505 case_expr = code->expr2;
6506 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
6507 gfc_error ("Selection expression in computed GOTO statement "
6508 "at %L must be a scalar integer expression",
6511 /* Further checking is not necessary because this SELECT was built
6512 by the compiler, so it should always be OK. Just move the
6513 case_expr from expr2 to expr so that we can handle computed
6514 GOTOs as normal SELECTs from here on. */
6515 code->expr1 = code->expr2;
6520 case_expr = code->expr1;
6522 type = case_expr->ts.type;
6523 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
6525 gfc_error ("Argument of SELECT statement at %L cannot be %s",
6526 &case_expr->where, gfc_typename (&case_expr->ts));
6528 /* Punt. Going on here just produce more garbage error messages. */
6532 if (case_expr->rank != 0)
6534 gfc_error ("Argument of SELECT statement at %L must be a scalar "
6535 "expression", &case_expr->where);
6541 /* PR 19168 has a long discussion concerning a mismatch of the kinds
6542 of the SELECT CASE expression and its CASE values. Walk the lists
6543 of case values, and if we find a mismatch, promote case_expr to
6544 the appropriate kind. */
6546 if (type == BT_LOGICAL || type == BT_INTEGER)
6548 for (body = code->block; body; body = body->block)
6550 /* Walk the case label list. */
6551 for (cp = body->ext.case_list; cp; cp = cp->next)
6553 /* Intercept the DEFAULT case. It does not have a kind. */
6554 if (cp->low == NULL && cp->high == NULL)
6557 /* Unreachable case ranges are discarded, so ignore. */
6558 if (cp->low != NULL && cp->high != NULL
6559 && cp->low != cp->high
6560 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6563 /* FIXME: Should a warning be issued? */
6565 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
6566 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
6568 if (cp->high != NULL
6569 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
6570 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
6575 /* Assume there is no DEFAULT case. */
6576 default_case = NULL;
6581 for (body = code->block; body; body = body->block)
6583 /* Assume the CASE list is OK, and all CASE labels can be matched. */
6585 seen_unreachable = 0;
6587 /* Walk the case label list, making sure that all case labels
6589 for (cp = body->ext.case_list; cp; cp = cp->next)
6591 /* Count the number of cases in the whole construct. */
6594 /* Intercept the DEFAULT case. */
6595 if (cp->low == NULL && cp->high == NULL)
6597 if (default_case != NULL)
6599 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6600 "by a second DEFAULT CASE at %L",
6601 &default_case->where, &cp->where);
6612 /* Deal with single value cases and case ranges. Errors are
6613 issued from the validation function. */
6614 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
6615 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
6621 if (type == BT_LOGICAL
6622 && ((cp->low == NULL || cp->high == NULL)
6623 || cp->low != cp->high))
6625 gfc_error ("Logical range in CASE statement at %L is not "
6626 "allowed", &cp->low->where);
6631 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
6634 value = cp->low->value.logical == 0 ? 2 : 1;
6635 if (value & seen_logical)
6637 gfc_error ("constant logical value in CASE statement "
6638 "is repeated at %L",
6643 seen_logical |= value;
6646 if (cp->low != NULL && cp->high != NULL
6647 && cp->low != cp->high
6648 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6650 if (gfc_option.warn_surprising)
6651 gfc_warning ("Range specification at %L can never "
6652 "be matched", &cp->where);
6654 cp->unreachable = 1;
6655 seen_unreachable = 1;
6659 /* If the case range can be matched, it can also overlap with
6660 other cases. To make sure it does not, we put it in a
6661 double linked list here. We sort that with a merge sort
6662 later on to detect any overlapping cases. */
6666 head->right = head->left = NULL;
6671 tail->right->left = tail;
6678 /* It there was a failure in the previous case label, give up
6679 for this case label list. Continue with the next block. */
6683 /* See if any case labels that are unreachable have been seen.
6684 If so, we eliminate them. This is a bit of a kludge because
6685 the case lists for a single case statement (label) is a
6686 single forward linked lists. */
6687 if (seen_unreachable)
6689 /* Advance until the first case in the list is reachable. */
6690 while (body->ext.case_list != NULL
6691 && body->ext.case_list->unreachable)
6693 gfc_case *n = body->ext.case_list;
6694 body->ext.case_list = body->ext.case_list->next;
6696 gfc_free_case_list (n);
6699 /* Strip all other unreachable cases. */
6700 if (body->ext.case_list)
6702 for (cp = body->ext.case_list; cp->next; cp = cp->next)
6704 if (cp->next->unreachable)
6706 gfc_case *n = cp->next;
6707 cp->next = cp->next->next;
6709 gfc_free_case_list (n);
6716 /* See if there were overlapping cases. If the check returns NULL,
6717 there was overlap. In that case we don't do anything. If head
6718 is non-NULL, we prepend the DEFAULT case. The sorted list can
6719 then used during code generation for SELECT CASE constructs with
6720 a case expression of a CHARACTER type. */
6723 head = check_case_overlap (head);
6725 /* Prepend the default_case if it is there. */
6726 if (head != NULL && default_case)
6728 default_case->left = NULL;
6729 default_case->right = head;
6730 head->left = default_case;
6734 /* Eliminate dead blocks that may be the result if we've seen
6735 unreachable case labels for a block. */
6736 for (body = code; body && body->block; body = body->block)
6738 if (body->block->ext.case_list == NULL)
6740 /* Cut the unreachable block from the code chain. */
6741 gfc_code *c = body->block;
6742 body->block = c->block;
6744 /* Kill the dead block, but not the blocks below it. */
6746 gfc_free_statements (c);
6750 /* More than two cases is legal but insane for logical selects.
6751 Issue a warning for it. */
6752 if (gfc_option.warn_surprising && type == BT_LOGICAL
6754 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
6759 /* Check if a derived type is extensible. */
6762 gfc_type_is_extensible (gfc_symbol *sym)
6764 return !(sym->attr.is_bind_c || sym->attr.sequence);
6768 /* Resolve a SELECT TYPE statement. */
6771 resolve_select_type (gfc_code *code)
6773 gfc_symbol *selector_type;
6774 gfc_code *body, *new_st;
6775 gfc_case *c, *default_case;
6777 char name[GFC_MAX_SYMBOL_LEN];
6784 selector_type = code->expr2->ts.u.derived->components->ts.u.derived;
6786 selector_type = code->expr1->ts.u.derived->components->ts.u.derived;
6788 /* Assume there is no DEFAULT case. */
6789 default_case = NULL;
6791 /* Loop over TYPE IS / CLASS IS cases. */
6792 for (body = code->block; body; body = body->block)
6794 c = body->ext.case_list;
6796 /* Check F03:C815. */
6797 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
6798 && !gfc_type_is_extensible (c->ts.u.derived))
6800 gfc_error ("Derived type '%s' at %L must be extensible",
6801 c->ts.u.derived->name, &c->where);
6805 /* Check F03:C816. */
6806 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
6807 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
6809 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
6810 c->ts.u.derived->name, &c->where, selector_type->name);
6814 /* Intercept the DEFAULT case. */
6815 if (c->ts.type == BT_UNKNOWN)
6817 /* Check F03:C818. */
6818 if (default_case != NULL)
6819 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6820 "by a second DEFAULT CASE at %L",
6821 &default_case->where, &c->where);
6830 /* Insert assignment for selector variable. */
6831 new_st = gfc_get_code ();
6832 new_st->op = EXEC_ASSIGN;
6833 new_st->expr1 = gfc_copy_expr (code->expr1);
6834 new_st->expr2 = gfc_copy_expr (code->expr2);
6838 /* Put SELECT TYPE statement inside a BLOCK. */
6839 new_st = gfc_get_code ();
6840 new_st->op = code->op;
6841 new_st->expr1 = code->expr1;
6842 new_st->expr2 = code->expr2;
6843 new_st->block = code->block;
6847 ns->code->next = new_st;
6848 code->op = EXEC_BLOCK;
6849 code->expr1 = code->expr2 = NULL;
6854 /* Transform to EXEC_SELECT. */
6855 code->op = EXEC_SELECT;
6856 gfc_add_component_ref (code->expr1, "$vindex");
6858 /* Loop over TYPE IS / CLASS IS cases. */
6859 for (body = code->block; body; body = body->block)
6861 c = body->ext.case_list;
6862 if (c->ts.type == BT_DERIVED)
6863 c->low = c->high = gfc_int_expr (c->ts.u.derived->vindex);
6864 else if (c->ts.type == BT_CLASS)
6865 /* Currently IS CLASS blocks are simply ignored.
6866 TODO: Implement IS CLASS. */
6869 if (c->ts.type != BT_DERIVED)
6871 /* Assign temporary to selector. */
6872 sprintf (name, "tmp$%s", c->ts.u.derived->name);
6873 st = gfc_find_symtree (ns->sym_root, name);
6874 new_st = gfc_get_code ();
6875 new_st->op = EXEC_POINTER_ASSIGN;
6876 new_st->expr1 = gfc_get_variable_expr (st);
6877 new_st->expr2 = gfc_get_variable_expr (code->expr1->symtree);
6878 gfc_add_component_ref (new_st->expr2, "$data");
6879 new_st->next = body->next;
6880 body->next = new_st;
6883 /* Eliminate dead blocks. */
6884 for (body = code; body && body->block; body = body->block)
6886 if (body->block->ext.case_list->unreachable)
6888 /* Cut the unreachable block from the code chain. */
6889 gfc_code *cd = body->block;
6890 body->block = cd->block;
6891 /* Kill the dead block, but not the blocks below it. */
6893 gfc_free_statements (cd);
6897 resolve_select (code);
6902 /* Resolve a transfer statement. This is making sure that:
6903 -- a derived type being transferred has only non-pointer components
6904 -- a derived type being transferred doesn't have private components, unless
6905 it's being transferred from the module where the type was defined
6906 -- we're not trying to transfer a whole assumed size array. */
6909 resolve_transfer (gfc_code *code)
6918 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
6921 sym = exp->symtree->n.sym;
6924 /* Go to actual component transferred. */
6925 for (ref = code->expr1->ref; ref; ref = ref->next)
6926 if (ref->type == REF_COMPONENT)
6927 ts = &ref->u.c.component->ts;
6929 if (ts->type == BT_DERIVED)
6931 /* Check that transferred derived type doesn't contain POINTER
6933 if (ts->u.derived->attr.pointer_comp)
6935 gfc_error ("Data transfer element at %L cannot have "
6936 "POINTER components", &code->loc);
6940 if (ts->u.derived->attr.alloc_comp)
6942 gfc_error ("Data transfer element at %L cannot have "
6943 "ALLOCATABLE components", &code->loc);
6947 if (derived_inaccessible (ts->u.derived))
6949 gfc_error ("Data transfer element at %L cannot have "
6950 "PRIVATE components",&code->loc);
6955 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
6956 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
6958 gfc_error ("Data transfer element at %L cannot be a full reference to "
6959 "an assumed-size array", &code->loc);
6965 /*********** Toplevel code resolution subroutines ***********/
6967 /* Find the set of labels that are reachable from this block. We also
6968 record the last statement in each block. */
6971 find_reachable_labels (gfc_code *block)
6978 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
6980 /* Collect labels in this block. We don't keep those corresponding
6981 to END {IF|SELECT}, these are checked in resolve_branch by going
6982 up through the code_stack. */
6983 for (c = block; c; c = c->next)
6985 if (c->here && c->op != EXEC_END_BLOCK)
6986 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
6989 /* Merge with labels from parent block. */
6992 gcc_assert (cs_base->prev->reachable_labels);
6993 bitmap_ior_into (cs_base->reachable_labels,
6994 cs_base->prev->reachable_labels);
6998 /* Given a branch to a label, see if the branch is conforming.
6999 The code node describes where the branch is located. */
7002 resolve_branch (gfc_st_label *label, gfc_code *code)
7009 /* Step one: is this a valid branching target? */
7011 if (label->defined == ST_LABEL_UNKNOWN)
7013 gfc_error ("Label %d referenced at %L is never defined", label->value,
7018 if (label->defined != ST_LABEL_TARGET)
7020 gfc_error ("Statement at %L is not a valid branch target statement "
7021 "for the branch statement at %L", &label->where, &code->loc);
7025 /* Step two: make sure this branch is not a branch to itself ;-) */
7027 if (code->here == label)
7029 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
7033 /* Step three: See if the label is in the same block as the
7034 branching statement. The hard work has been done by setting up
7035 the bitmap reachable_labels. */
7037 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
7040 /* Step four: If we haven't found the label in the bitmap, it may
7041 still be the label of the END of the enclosing block, in which
7042 case we find it by going up the code_stack. */
7044 for (stack = cs_base; stack; stack = stack->prev)
7045 if (stack->current->next && stack->current->next->here == label)
7050 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
7054 /* The label is not in an enclosing block, so illegal. This was
7055 allowed in Fortran 66, so we allow it as extension. No
7056 further checks are necessary in this case. */
7057 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
7058 "as the GOTO statement at %L", &label->where,
7064 /* Check whether EXPR1 has the same shape as EXPR2. */
7067 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
7069 mpz_t shape[GFC_MAX_DIMENSIONS];
7070 mpz_t shape2[GFC_MAX_DIMENSIONS];
7071 gfc_try result = FAILURE;
7074 /* Compare the rank. */
7075 if (expr1->rank != expr2->rank)
7078 /* Compare the size of each dimension. */
7079 for (i=0; i<expr1->rank; i++)
7081 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
7084 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
7087 if (mpz_cmp (shape[i], shape2[i]))
7091 /* When either of the two expression is an assumed size array, we
7092 ignore the comparison of dimension sizes. */
7097 for (i--; i >= 0; i--)
7099 mpz_clear (shape[i]);
7100 mpz_clear (shape2[i]);
7106 /* Check whether a WHERE assignment target or a WHERE mask expression
7107 has the same shape as the outmost WHERE mask expression. */
7110 resolve_where (gfc_code *code, gfc_expr *mask)
7116 cblock = code->block;
7118 /* Store the first WHERE mask-expr of the WHERE statement or construct.
7119 In case of nested WHERE, only the outmost one is stored. */
7120 if (mask == NULL) /* outmost WHERE */
7122 else /* inner WHERE */
7129 /* Check if the mask-expr has a consistent shape with the
7130 outmost WHERE mask-expr. */
7131 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
7132 gfc_error ("WHERE mask at %L has inconsistent shape",
7133 &cblock->expr1->where);
7136 /* the assignment statement of a WHERE statement, or the first
7137 statement in where-body-construct of a WHERE construct */
7138 cnext = cblock->next;
7143 /* WHERE assignment statement */
7146 /* Check shape consistent for WHERE assignment target. */
7147 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
7148 gfc_error ("WHERE assignment target at %L has "
7149 "inconsistent shape", &cnext->expr1->where);
7153 case EXEC_ASSIGN_CALL:
7154 resolve_call (cnext);
7155 if (!cnext->resolved_sym->attr.elemental)
7156 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7157 &cnext->ext.actual->expr->where);
7160 /* WHERE or WHERE construct is part of a where-body-construct */
7162 resolve_where (cnext, e);
7166 gfc_error ("Unsupported statement inside WHERE at %L",
7169 /* the next statement within the same where-body-construct */
7170 cnext = cnext->next;
7172 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7173 cblock = cblock->block;
7178 /* Resolve assignment in FORALL construct.
7179 NVAR is the number of FORALL index variables, and VAR_EXPR records the
7180 FORALL index variables. */
7183 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
7187 for (n = 0; n < nvar; n++)
7189 gfc_symbol *forall_index;
7191 forall_index = var_expr[n]->symtree->n.sym;
7193 /* Check whether the assignment target is one of the FORALL index
7195 if ((code->expr1->expr_type == EXPR_VARIABLE)
7196 && (code->expr1->symtree->n.sym == forall_index))
7197 gfc_error ("Assignment to a FORALL index variable at %L",
7198 &code->expr1->where);
7201 /* If one of the FORALL index variables doesn't appear in the
7202 assignment variable, then there could be a many-to-one
7203 assignment. Emit a warning rather than an error because the
7204 mask could be resolving this problem. */
7205 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
7206 gfc_warning ("The FORALL with index '%s' is not used on the "
7207 "left side of the assignment at %L and so might "
7208 "cause multiple assignment to this object",
7209 var_expr[n]->symtree->name, &code->expr1->where);
7215 /* Resolve WHERE statement in FORALL construct. */
7218 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
7219 gfc_expr **var_expr)
7224 cblock = code->block;
7227 /* the assignment statement of a WHERE statement, or the first
7228 statement in where-body-construct of a WHERE construct */
7229 cnext = cblock->next;
7234 /* WHERE assignment statement */
7236 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
7239 /* WHERE operator assignment statement */
7240 case EXEC_ASSIGN_CALL:
7241 resolve_call (cnext);
7242 if (!cnext->resolved_sym->attr.elemental)
7243 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7244 &cnext->ext.actual->expr->where);
7247 /* WHERE or WHERE construct is part of a where-body-construct */
7249 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
7253 gfc_error ("Unsupported statement inside WHERE at %L",
7256 /* the next statement within the same where-body-construct */
7257 cnext = cnext->next;
7259 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7260 cblock = cblock->block;
7265 /* Traverse the FORALL body to check whether the following errors exist:
7266 1. For assignment, check if a many-to-one assignment happens.
7267 2. For WHERE statement, check the WHERE body to see if there is any
7268 many-to-one assignment. */
7271 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
7275 c = code->block->next;
7281 case EXEC_POINTER_ASSIGN:
7282 gfc_resolve_assign_in_forall (c, nvar, var_expr);
7285 case EXEC_ASSIGN_CALL:
7289 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
7290 there is no need to handle it here. */
7294 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
7299 /* The next statement in the FORALL body. */
7305 /* Counts the number of iterators needed inside a forall construct, including
7306 nested forall constructs. This is used to allocate the needed memory
7307 in gfc_resolve_forall. */
7310 gfc_count_forall_iterators (gfc_code *code)
7312 int max_iters, sub_iters, current_iters;
7313 gfc_forall_iterator *fa;
7315 gcc_assert(code->op == EXEC_FORALL);
7319 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7322 code = code->block->next;
7326 if (code->op == EXEC_FORALL)
7328 sub_iters = gfc_count_forall_iterators (code);
7329 if (sub_iters > max_iters)
7330 max_iters = sub_iters;
7335 return current_iters + max_iters;
7339 /* Given a FORALL construct, first resolve the FORALL iterator, then call
7340 gfc_resolve_forall_body to resolve the FORALL body. */
7343 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
7345 static gfc_expr **var_expr;
7346 static int total_var = 0;
7347 static int nvar = 0;
7349 gfc_forall_iterator *fa;
7354 /* Start to resolve a FORALL construct */
7355 if (forall_save == 0)
7357 /* Count the total number of FORALL index in the nested FORALL
7358 construct in order to allocate the VAR_EXPR with proper size. */
7359 total_var = gfc_count_forall_iterators (code);
7361 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
7362 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
7365 /* The information about FORALL iterator, including FORALL index start, end
7366 and stride. The FORALL index can not appear in start, end or stride. */
7367 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7369 /* Check if any outer FORALL index name is the same as the current
7371 for (i = 0; i < nvar; i++)
7373 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
7375 gfc_error ("An outer FORALL construct already has an index "
7376 "with this name %L", &fa->var->where);
7380 /* Record the current FORALL index. */
7381 var_expr[nvar] = gfc_copy_expr (fa->var);
7385 /* No memory leak. */
7386 gcc_assert (nvar <= total_var);
7389 /* Resolve the FORALL body. */
7390 gfc_resolve_forall_body (code, nvar, var_expr);
7392 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
7393 gfc_resolve_blocks (code->block, ns);
7397 /* Free only the VAR_EXPRs allocated in this frame. */
7398 for (i = nvar; i < tmp; i++)
7399 gfc_free_expr (var_expr[i]);
7403 /* We are in the outermost FORALL construct. */
7404 gcc_assert (forall_save == 0);
7406 /* VAR_EXPR is not needed any more. */
7407 gfc_free (var_expr);
7413 /* Resolve a BLOCK construct statement. */
7416 resolve_block_construct (gfc_code* code)
7418 /* Eventually, we may want to do some checks here or handle special stuff.
7419 But so far the only thing we can do is resolving the local namespace. */
7421 gfc_resolve (code->ext.ns);
7425 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
7428 static void resolve_code (gfc_code *, gfc_namespace *);
7431 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
7435 for (; b; b = b->block)
7437 t = gfc_resolve_expr (b->expr1);
7438 if (gfc_resolve_expr (b->expr2) == FAILURE)
7444 if (t == SUCCESS && b->expr1 != NULL
7445 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
7446 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
7453 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
7454 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
7459 resolve_branch (b->label1, b);
7463 resolve_block_construct (b);
7467 case EXEC_SELECT_TYPE:
7477 case EXEC_OMP_ATOMIC:
7478 case EXEC_OMP_CRITICAL:
7480 case EXEC_OMP_MASTER:
7481 case EXEC_OMP_ORDERED:
7482 case EXEC_OMP_PARALLEL:
7483 case EXEC_OMP_PARALLEL_DO:
7484 case EXEC_OMP_PARALLEL_SECTIONS:
7485 case EXEC_OMP_PARALLEL_WORKSHARE:
7486 case EXEC_OMP_SECTIONS:
7487 case EXEC_OMP_SINGLE:
7489 case EXEC_OMP_TASKWAIT:
7490 case EXEC_OMP_WORKSHARE:
7494 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
7497 resolve_code (b->next, ns);
7502 /* Does everything to resolve an ordinary assignment. Returns true
7503 if this is an interface assignment. */
7505 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
7515 if (gfc_extend_assign (code, ns) == SUCCESS)
7517 gfc_symbol* assign_proc;
7520 if (code->op == EXEC_ASSIGN_CALL)
7522 lhs = code->ext.actual->expr;
7523 rhsptr = &code->ext.actual->next->expr;
7524 assign_proc = code->symtree->n.sym;
7528 gfc_actual_arglist* args;
7529 gfc_typebound_proc* tbp;
7531 gcc_assert (code->op == EXEC_COMPCALL);
7533 args = code->expr1->value.compcall.actual;
7535 rhsptr = &args->next->expr;
7537 tbp = code->expr1->value.compcall.tbp;
7538 gcc_assert (!tbp->is_generic);
7539 assign_proc = tbp->u.specific->n.sym;
7542 /* Make a temporary rhs when there is a default initializer
7543 and rhs is the same symbol as the lhs. */
7544 if ((*rhsptr)->expr_type == EXPR_VARIABLE
7545 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
7546 && has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
7547 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
7548 *rhsptr = gfc_get_parentheses (*rhsptr);
7557 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
7558 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
7559 &code->loc) == FAILURE)
7562 /* Handle the case of a BOZ literal on the RHS. */
7563 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
7566 if (gfc_option.warn_surprising)
7567 gfc_warning ("BOZ literal at %L is bitwise transferred "
7568 "non-integer symbol '%s'", &code->loc,
7569 lhs->symtree->n.sym->name);
7571 if (!gfc_convert_boz (rhs, &lhs->ts))
7573 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
7575 if (rc == ARITH_UNDERFLOW)
7576 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
7577 ". This check can be disabled with the option "
7578 "-fno-range-check", &rhs->where);
7579 else if (rc == ARITH_OVERFLOW)
7580 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
7581 ". This check can be disabled with the option "
7582 "-fno-range-check", &rhs->where);
7583 else if (rc == ARITH_NAN)
7584 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
7585 ". This check can be disabled with the option "
7586 "-fno-range-check", &rhs->where);
7592 if (lhs->ts.type == BT_CHARACTER
7593 && gfc_option.warn_character_truncation)
7595 if (lhs->ts.u.cl != NULL
7596 && lhs->ts.u.cl->length != NULL
7597 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7598 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
7600 if (rhs->expr_type == EXPR_CONSTANT)
7601 rlen = rhs->value.character.length;
7603 else if (rhs->ts.u.cl != NULL
7604 && rhs->ts.u.cl->length != NULL
7605 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7606 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
7608 if (rlen && llen && rlen > llen)
7609 gfc_warning_now ("CHARACTER expression will be truncated "
7610 "in assignment (%d/%d) at %L",
7611 llen, rlen, &code->loc);
7614 /* Ensure that a vector index expression for the lvalue is evaluated
7615 to a temporary if the lvalue symbol is referenced in it. */
7618 for (ref = lhs->ref; ref; ref= ref->next)
7619 if (ref->type == REF_ARRAY)
7621 for (n = 0; n < ref->u.ar.dimen; n++)
7622 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
7623 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
7624 ref->u.ar.start[n]))
7626 = gfc_get_parentheses (ref->u.ar.start[n]);
7630 if (gfc_pure (NULL))
7632 if (gfc_impure_variable (lhs->symtree->n.sym))
7634 gfc_error ("Cannot assign to variable '%s' in PURE "
7636 lhs->symtree->n.sym->name,
7641 if (lhs->ts.type == BT_DERIVED
7642 && lhs->expr_type == EXPR_VARIABLE
7643 && lhs->ts.u.derived->attr.pointer_comp
7644 && gfc_impure_variable (rhs->symtree->n.sym))
7646 gfc_error ("The impure variable at %L is assigned to "
7647 "a derived type variable with a POINTER "
7648 "component in a PURE procedure (12.6)",
7655 if (lhs->ts.type == BT_CLASS)
7657 gfc_error ("Variable must not be polymorphic in assignment at %L",
7662 gfc_check_assign (lhs, rhs, 1);
7667 /* Given a block of code, recursively resolve everything pointed to by this
7671 resolve_code (gfc_code *code, gfc_namespace *ns)
7673 int omp_workshare_save;
7678 frame.prev = cs_base;
7682 find_reachable_labels (code);
7684 for (; code; code = code->next)
7686 frame.current = code;
7687 forall_save = forall_flag;
7689 if (code->op == EXEC_FORALL)
7692 gfc_resolve_forall (code, ns, forall_save);
7695 else if (code->block)
7697 omp_workshare_save = -1;
7700 case EXEC_OMP_PARALLEL_WORKSHARE:
7701 omp_workshare_save = omp_workshare_flag;
7702 omp_workshare_flag = 1;
7703 gfc_resolve_omp_parallel_blocks (code, ns);
7705 case EXEC_OMP_PARALLEL:
7706 case EXEC_OMP_PARALLEL_DO:
7707 case EXEC_OMP_PARALLEL_SECTIONS:
7709 omp_workshare_save = omp_workshare_flag;
7710 omp_workshare_flag = 0;
7711 gfc_resolve_omp_parallel_blocks (code, ns);
7714 gfc_resolve_omp_do_blocks (code, ns);
7716 case EXEC_OMP_WORKSHARE:
7717 omp_workshare_save = omp_workshare_flag;
7718 omp_workshare_flag = 1;
7721 gfc_resolve_blocks (code->block, ns);
7725 if (omp_workshare_save != -1)
7726 omp_workshare_flag = omp_workshare_save;
7730 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
7731 t = gfc_resolve_expr (code->expr1);
7732 forall_flag = forall_save;
7734 if (gfc_resolve_expr (code->expr2) == FAILURE)
7740 case EXEC_END_BLOCK:
7747 case EXEC_ASSIGN_CALL:
7751 /* Keep track of which entry we are up to. */
7752 current_entry_id = code->ext.entry->id;
7756 resolve_where (code, NULL);
7760 if (code->expr1 != NULL)
7762 if (code->expr1->ts.type != BT_INTEGER)
7763 gfc_error ("ASSIGNED GOTO statement at %L requires an "
7764 "INTEGER variable", &code->expr1->where);
7765 else if (code->expr1->symtree->n.sym->attr.assign != 1)
7766 gfc_error ("Variable '%s' has not been assigned a target "
7767 "label at %L", code->expr1->symtree->n.sym->name,
7768 &code->expr1->where);
7771 resolve_branch (code->label1, code);
7775 if (code->expr1 != NULL
7776 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
7777 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
7778 "INTEGER return specifier", &code->expr1->where);
7781 case EXEC_INIT_ASSIGN:
7782 case EXEC_END_PROCEDURE:
7789 if (resolve_ordinary_assign (code, ns))
7791 if (code->op == EXEC_COMPCALL)
7798 case EXEC_LABEL_ASSIGN:
7799 if (code->label1->defined == ST_LABEL_UNKNOWN)
7800 gfc_error ("Label %d referenced at %L is never defined",
7801 code->label1->value, &code->label1->where);
7803 && (code->expr1->expr_type != EXPR_VARIABLE
7804 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
7805 || code->expr1->symtree->n.sym->ts.kind
7806 != gfc_default_integer_kind
7807 || code->expr1->symtree->n.sym->as != NULL))
7808 gfc_error ("ASSIGN statement at %L requires a scalar "
7809 "default INTEGER variable", &code->expr1->where);
7812 case EXEC_POINTER_ASSIGN:
7816 gfc_check_pointer_assign (code->expr1, code->expr2);
7819 case EXEC_ARITHMETIC_IF:
7821 && code->expr1->ts.type != BT_INTEGER
7822 && code->expr1->ts.type != BT_REAL)
7823 gfc_error ("Arithmetic IF statement at %L requires a numeric "
7824 "expression", &code->expr1->where);
7826 resolve_branch (code->label1, code);
7827 resolve_branch (code->label2, code);
7828 resolve_branch (code->label3, code);
7832 if (t == SUCCESS && code->expr1 != NULL
7833 && (code->expr1->ts.type != BT_LOGICAL
7834 || code->expr1->rank != 0))
7835 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
7836 &code->expr1->where);
7841 resolve_call (code);
7846 if (code->expr1->symtree
7847 && code->expr1->symtree->n.sym->ts.type == BT_CLASS)
7848 resolve_class_typebound_call (code);
7850 resolve_typebound_call (code);
7854 resolve_ppc_call (code);
7858 /* Select is complicated. Also, a SELECT construct could be
7859 a transformed computed GOTO. */
7860 resolve_select (code);
7863 case EXEC_SELECT_TYPE:
7864 resolve_select_type (code);
7868 gfc_resolve (code->ext.ns);
7872 if (code->ext.iterator != NULL)
7874 gfc_iterator *iter = code->ext.iterator;
7875 if (gfc_resolve_iterator (iter, true) != FAILURE)
7876 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
7881 if (code->expr1 == NULL)
7882 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
7884 && (code->expr1->rank != 0
7885 || code->expr1->ts.type != BT_LOGICAL))
7886 gfc_error ("Exit condition of DO WHILE loop at %L must be "
7887 "a scalar LOGICAL expression", &code->expr1->where);
7892 resolve_allocate_deallocate (code, "ALLOCATE");
7896 case EXEC_DEALLOCATE:
7898 resolve_allocate_deallocate (code, "DEALLOCATE");
7903 if (gfc_resolve_open (code->ext.open) == FAILURE)
7906 resolve_branch (code->ext.open->err, code);
7910 if (gfc_resolve_close (code->ext.close) == FAILURE)
7913 resolve_branch (code->ext.close->err, code);
7916 case EXEC_BACKSPACE:
7920 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
7923 resolve_branch (code->ext.filepos->err, code);
7927 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
7930 resolve_branch (code->ext.inquire->err, code);
7934 gcc_assert (code->ext.inquire != NULL);
7935 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
7938 resolve_branch (code->ext.inquire->err, code);
7942 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
7945 resolve_branch (code->ext.wait->err, code);
7946 resolve_branch (code->ext.wait->end, code);
7947 resolve_branch (code->ext.wait->eor, code);
7952 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
7955 resolve_branch (code->ext.dt->err, code);
7956 resolve_branch (code->ext.dt->end, code);
7957 resolve_branch (code->ext.dt->eor, code);
7961 resolve_transfer (code);
7965 resolve_forall_iterators (code->ext.forall_iterator);
7967 if (code->expr1 != NULL && code->expr1->ts.type != BT_LOGICAL)
7968 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
7969 "expression", &code->expr1->where);
7972 case EXEC_OMP_ATOMIC:
7973 case EXEC_OMP_BARRIER:
7974 case EXEC_OMP_CRITICAL:
7975 case EXEC_OMP_FLUSH:
7977 case EXEC_OMP_MASTER:
7978 case EXEC_OMP_ORDERED:
7979 case EXEC_OMP_SECTIONS:
7980 case EXEC_OMP_SINGLE:
7981 case EXEC_OMP_TASKWAIT:
7982 case EXEC_OMP_WORKSHARE:
7983 gfc_resolve_omp_directive (code, ns);
7986 case EXEC_OMP_PARALLEL:
7987 case EXEC_OMP_PARALLEL_DO:
7988 case EXEC_OMP_PARALLEL_SECTIONS:
7989 case EXEC_OMP_PARALLEL_WORKSHARE:
7991 omp_workshare_save = omp_workshare_flag;
7992 omp_workshare_flag = 0;
7993 gfc_resolve_omp_directive (code, ns);
7994 omp_workshare_flag = omp_workshare_save;
7998 gfc_internal_error ("resolve_code(): Bad statement code");
8002 cs_base = frame.prev;
8006 /* Resolve initial values and make sure they are compatible with
8010 resolve_values (gfc_symbol *sym)
8012 if (sym->value == NULL)
8015 if (gfc_resolve_expr (sym->value) == FAILURE)
8018 gfc_check_assign_symbol (sym, sym->value);
8022 /* Verify the binding labels for common blocks that are BIND(C). The label
8023 for a BIND(C) common block must be identical in all scoping units in which
8024 the common block is declared. Further, the binding label can not collide
8025 with any other global entity in the program. */
8028 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
8030 if (comm_block_tree->n.common->is_bind_c == 1)
8032 gfc_gsymbol *binding_label_gsym;
8033 gfc_gsymbol *comm_name_gsym;
8035 /* See if a global symbol exists by the common block's name. It may
8036 be NULL if the common block is use-associated. */
8037 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
8038 comm_block_tree->n.common->name);
8039 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
8040 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
8041 "with the global entity '%s' at %L",
8042 comm_block_tree->n.common->binding_label,
8043 comm_block_tree->n.common->name,
8044 &(comm_block_tree->n.common->where),
8045 comm_name_gsym->name, &(comm_name_gsym->where));
8046 else if (comm_name_gsym != NULL
8047 && strcmp (comm_name_gsym->name,
8048 comm_block_tree->n.common->name) == 0)
8050 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
8052 if (comm_name_gsym->binding_label == NULL)
8053 /* No binding label for common block stored yet; save this one. */
8054 comm_name_gsym->binding_label =
8055 comm_block_tree->n.common->binding_label;
8057 if (strcmp (comm_name_gsym->binding_label,
8058 comm_block_tree->n.common->binding_label) != 0)
8060 /* Common block names match but binding labels do not. */
8061 gfc_error ("Binding label '%s' for common block '%s' at %L "
8062 "does not match the binding label '%s' for common "
8064 comm_block_tree->n.common->binding_label,
8065 comm_block_tree->n.common->name,
8066 &(comm_block_tree->n.common->where),
8067 comm_name_gsym->binding_label,
8068 comm_name_gsym->name,
8069 &(comm_name_gsym->where));
8074 /* There is no binding label (NAME="") so we have nothing further to
8075 check and nothing to add as a global symbol for the label. */
8076 if (comm_block_tree->n.common->binding_label[0] == '\0' )
8079 binding_label_gsym =
8080 gfc_find_gsymbol (gfc_gsym_root,
8081 comm_block_tree->n.common->binding_label);
8082 if (binding_label_gsym == NULL)
8084 /* Need to make a global symbol for the binding label to prevent
8085 it from colliding with another. */
8086 binding_label_gsym =
8087 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
8088 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
8089 binding_label_gsym->type = GSYM_COMMON;
8093 /* If comm_name_gsym is NULL, the name common block is use
8094 associated and the name could be colliding. */
8095 if (binding_label_gsym->type != GSYM_COMMON)
8096 gfc_error ("Binding label '%s' for common block '%s' at %L "
8097 "collides with the global entity '%s' at %L",
8098 comm_block_tree->n.common->binding_label,
8099 comm_block_tree->n.common->name,
8100 &(comm_block_tree->n.common->where),
8101 binding_label_gsym->name,
8102 &(binding_label_gsym->where));
8103 else if (comm_name_gsym != NULL
8104 && (strcmp (binding_label_gsym->name,
8105 comm_name_gsym->binding_label) != 0)
8106 && (strcmp (binding_label_gsym->sym_name,
8107 comm_name_gsym->name) != 0))
8108 gfc_error ("Binding label '%s' for common block '%s' at %L "
8109 "collides with global entity '%s' at %L",
8110 binding_label_gsym->name, binding_label_gsym->sym_name,
8111 &(comm_block_tree->n.common->where),
8112 comm_name_gsym->name, &(comm_name_gsym->where));
8120 /* Verify any BIND(C) derived types in the namespace so we can report errors
8121 for them once, rather than for each variable declared of that type. */
8124 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
8126 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
8127 && derived_sym->attr.is_bind_c == 1)
8128 verify_bind_c_derived_type (derived_sym);
8134 /* Verify that any binding labels used in a given namespace do not collide
8135 with the names or binding labels of any global symbols. */
8138 gfc_verify_binding_labels (gfc_symbol *sym)
8142 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
8143 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
8145 gfc_gsymbol *bind_c_sym;
8147 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
8148 if (bind_c_sym != NULL
8149 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
8151 if (sym->attr.if_source == IFSRC_DECL
8152 && (bind_c_sym->type != GSYM_SUBROUTINE
8153 && bind_c_sym->type != GSYM_FUNCTION)
8154 && ((sym->attr.contained == 1
8155 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
8156 || (sym->attr.use_assoc == 1
8157 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
8159 /* Make sure global procedures don't collide with anything. */
8160 gfc_error ("Binding label '%s' at %L collides with the global "
8161 "entity '%s' at %L", sym->binding_label,
8162 &(sym->declared_at), bind_c_sym->name,
8163 &(bind_c_sym->where));
8166 else if (sym->attr.contained == 0
8167 && (sym->attr.if_source == IFSRC_IFBODY
8168 && sym->attr.flavor == FL_PROCEDURE)
8169 && (bind_c_sym->sym_name != NULL
8170 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
8172 /* Make sure procedures in interface bodies don't collide. */
8173 gfc_error ("Binding label '%s' in interface body at %L collides "
8174 "with the global entity '%s' at %L",
8176 &(sym->declared_at), bind_c_sym->name,
8177 &(bind_c_sym->where));
8180 else if (sym->attr.contained == 0
8181 && sym->attr.if_source == IFSRC_UNKNOWN)
8182 if ((sym->attr.use_assoc && bind_c_sym->mod_name
8183 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
8184 || sym->attr.use_assoc == 0)
8186 gfc_error ("Binding label '%s' at %L collides with global "
8187 "entity '%s' at %L", sym->binding_label,
8188 &(sym->declared_at), bind_c_sym->name,
8189 &(bind_c_sym->where));
8194 /* Clear the binding label to prevent checking multiple times. */
8195 sym->binding_label[0] = '\0';
8197 else if (bind_c_sym == NULL)
8199 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
8200 bind_c_sym->where = sym->declared_at;
8201 bind_c_sym->sym_name = sym->name;
8203 if (sym->attr.use_assoc == 1)
8204 bind_c_sym->mod_name = sym->module;
8206 if (sym->ns->proc_name != NULL)
8207 bind_c_sym->mod_name = sym->ns->proc_name->name;
8209 if (sym->attr.contained == 0)
8211 if (sym->attr.subroutine)
8212 bind_c_sym->type = GSYM_SUBROUTINE;
8213 else if (sym->attr.function)
8214 bind_c_sym->type = GSYM_FUNCTION;
8222 /* Resolve an index expression. */
8225 resolve_index_expr (gfc_expr *e)
8227 if (gfc_resolve_expr (e) == FAILURE)
8230 if (gfc_simplify_expr (e, 0) == FAILURE)
8233 if (gfc_specification_expr (e) == FAILURE)
8239 /* Resolve a charlen structure. */
8242 resolve_charlen (gfc_charlen *cl)
8251 specification_expr = 1;
8253 if (resolve_index_expr (cl->length) == FAILURE)
8255 specification_expr = 0;
8259 /* "If the character length parameter value evaluates to a negative
8260 value, the length of character entities declared is zero." */
8261 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
8263 gfc_warning_now ("CHARACTER variable has zero length at %L",
8264 &cl->length->where);
8265 gfc_replace_expr (cl->length, gfc_int_expr (0));
8268 /* Check that the character length is not too large. */
8269 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
8270 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
8271 && cl->length->ts.type == BT_INTEGER
8272 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
8274 gfc_error ("String length at %L is too large", &cl->length->where);
8282 /* Test for non-constant shape arrays. */
8285 is_non_constant_shape_array (gfc_symbol *sym)
8291 not_constant = false;
8292 if (sym->as != NULL)
8294 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
8295 has not been simplified; parameter array references. Do the
8296 simplification now. */
8297 for (i = 0; i < sym->as->rank; i++)
8299 e = sym->as->lower[i];
8300 if (e && (resolve_index_expr (e) == FAILURE
8301 || !gfc_is_constant_expr (e)))
8302 not_constant = true;
8304 e = sym->as->upper[i];
8305 if (e && (resolve_index_expr (e) == FAILURE
8306 || !gfc_is_constant_expr (e)))
8307 not_constant = true;
8310 return not_constant;
8313 /* Given a symbol and an initialization expression, add code to initialize
8314 the symbol to the function entry. */
8316 build_init_assign (gfc_symbol *sym, gfc_expr *init)
8320 gfc_namespace *ns = sym->ns;
8322 /* Search for the function namespace if this is a contained
8323 function without an explicit result. */
8324 if (sym->attr.function && sym == sym->result
8325 && sym->name != sym->ns->proc_name->name)
8328 for (;ns; ns = ns->sibling)
8329 if (strcmp (ns->proc_name->name, sym->name) == 0)
8335 gfc_free_expr (init);
8339 /* Build an l-value expression for the result. */
8340 lval = gfc_lval_expr_from_sym (sym);
8342 /* Add the code at scope entry. */
8343 init_st = gfc_get_code ();
8344 init_st->next = ns->code;
8347 /* Assign the default initializer to the l-value. */
8348 init_st->loc = sym->declared_at;
8349 init_st->op = EXEC_INIT_ASSIGN;
8350 init_st->expr1 = lval;
8351 init_st->expr2 = init;
8354 /* Assign the default initializer to a derived type variable or result. */
8357 apply_default_init (gfc_symbol *sym)
8359 gfc_expr *init = NULL;
8361 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
8364 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
8365 init = gfc_default_initializer (&sym->ts);
8370 build_init_assign (sym, init);
8373 /* Build an initializer for a local integer, real, complex, logical, or
8374 character variable, based on the command line flags finit-local-zero,
8375 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
8376 null if the symbol should not have a default initialization. */
8378 build_default_init_expr (gfc_symbol *sym)
8381 gfc_expr *init_expr;
8384 /* These symbols should never have a default initialization. */
8385 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
8386 || sym->attr.external
8388 || sym->attr.pointer
8389 || sym->attr.in_equivalence
8390 || sym->attr.in_common
8393 || sym->attr.cray_pointee
8394 || sym->attr.cray_pointer)
8397 /* Now we'll try to build an initializer expression. */
8398 init_expr = gfc_get_expr ();
8399 init_expr->expr_type = EXPR_CONSTANT;
8400 init_expr->ts.type = sym->ts.type;
8401 init_expr->ts.kind = sym->ts.kind;
8402 init_expr->where = sym->declared_at;
8404 /* We will only initialize integers, reals, complex, logicals, and
8405 characters, and only if the corresponding command-line flags
8406 were set. Otherwise, we free init_expr and return null. */
8407 switch (sym->ts.type)
8410 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
8411 mpz_init_set_si (init_expr->value.integer,
8412 gfc_option.flag_init_integer_value);
8415 gfc_free_expr (init_expr);
8421 mpfr_init (init_expr->value.real);
8422 switch (gfc_option.flag_init_real)
8424 case GFC_INIT_REAL_SNAN:
8425 init_expr->is_snan = 1;
8427 case GFC_INIT_REAL_NAN:
8428 mpfr_set_nan (init_expr->value.real);
8431 case GFC_INIT_REAL_INF:
8432 mpfr_set_inf (init_expr->value.real, 1);
8435 case GFC_INIT_REAL_NEG_INF:
8436 mpfr_set_inf (init_expr->value.real, -1);
8439 case GFC_INIT_REAL_ZERO:
8440 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
8444 gfc_free_expr (init_expr);
8452 mpc_init2 (init_expr->value.complex, mpfr_get_default_prec());
8454 mpfr_init (init_expr->value.complex.r);
8455 mpfr_init (init_expr->value.complex.i);
8457 switch (gfc_option.flag_init_real)
8459 case GFC_INIT_REAL_SNAN:
8460 init_expr->is_snan = 1;
8462 case GFC_INIT_REAL_NAN:
8463 mpfr_set_nan (mpc_realref (init_expr->value.complex));
8464 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
8467 case GFC_INIT_REAL_INF:
8468 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
8469 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
8472 case GFC_INIT_REAL_NEG_INF:
8473 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
8474 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
8477 case GFC_INIT_REAL_ZERO:
8479 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
8481 mpfr_set_ui (init_expr->value.complex.r, 0.0, GFC_RND_MODE);
8482 mpfr_set_ui (init_expr->value.complex.i, 0.0, GFC_RND_MODE);
8487 gfc_free_expr (init_expr);
8494 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
8495 init_expr->value.logical = 0;
8496 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
8497 init_expr->value.logical = 1;
8500 gfc_free_expr (init_expr);
8506 /* For characters, the length must be constant in order to
8507 create a default initializer. */
8508 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
8509 && sym->ts.u.cl->length
8510 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8512 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
8513 init_expr->value.character.length = char_len;
8514 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
8515 for (i = 0; i < char_len; i++)
8516 init_expr->value.character.string[i]
8517 = (unsigned char) gfc_option.flag_init_character_value;
8521 gfc_free_expr (init_expr);
8527 gfc_free_expr (init_expr);
8533 /* Add an initialization expression to a local variable. */
8535 apply_default_init_local (gfc_symbol *sym)
8537 gfc_expr *init = NULL;
8539 /* The symbol should be a variable or a function return value. */
8540 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
8541 || (sym->attr.function && sym->result != sym))
8544 /* Try to build the initializer expression. If we can't initialize
8545 this symbol, then init will be NULL. */
8546 init = build_default_init_expr (sym);
8550 /* For saved variables, we don't want to add an initializer at
8551 function entry, so we just add a static initializer. */
8552 if (sym->attr.save || sym->ns->save_all)
8554 /* Don't clobber an existing initializer! */
8555 gcc_assert (sym->value == NULL);
8560 build_init_assign (sym, init);
8563 /* Resolution of common features of flavors variable and procedure. */
8566 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
8568 /* Constraints on deferred shape variable. */
8569 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
8571 if (sym->attr.allocatable)
8573 if (sym->attr.dimension)
8575 gfc_error ("Allocatable array '%s' at %L must have "
8576 "a deferred shape", sym->name, &sym->declared_at);
8579 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
8580 "may not be ALLOCATABLE", sym->name,
8581 &sym->declared_at) == FAILURE)
8585 if (sym->attr.pointer && sym->attr.dimension)
8587 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
8588 sym->name, &sym->declared_at);
8595 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
8596 && !sym->attr.dummy && sym->ts.type != BT_CLASS)
8598 gfc_error ("Array '%s' at %L cannot have a deferred shape",
8599 sym->name, &sym->declared_at);
8607 /* Additional checks for symbols with flavor variable and derived
8608 type. To be called from resolve_fl_variable. */
8611 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
8613 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
8615 /* Check to see if a derived type is blocked from being host
8616 associated by the presence of another class I symbol in the same
8617 namespace. 14.6.1.3 of the standard and the discussion on
8618 comp.lang.fortran. */
8619 if (sym->ns != sym->ts.u.derived->ns
8620 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
8623 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
8624 if (s && s->attr.flavor != FL_DERIVED)
8626 gfc_error ("The type '%s' cannot be host associated at %L "
8627 "because it is blocked by an incompatible object "
8628 "of the same name declared at %L",
8629 sym->ts.u.derived->name, &sym->declared_at,
8635 /* 4th constraint in section 11.3: "If an object of a type for which
8636 component-initialization is specified (R429) appears in the
8637 specification-part of a module and does not have the ALLOCATABLE
8638 or POINTER attribute, the object shall have the SAVE attribute."
8640 The check for initializers is performed with
8641 has_default_initializer because gfc_default_initializer generates
8642 a hidden default for allocatable components. */
8643 if (!(sym->value || no_init_flag) && sym->ns->proc_name
8644 && sym->ns->proc_name->attr.flavor == FL_MODULE
8645 && !sym->ns->save_all && !sym->attr.save
8646 && !sym->attr.pointer && !sym->attr.allocatable
8647 && has_default_initializer (sym->ts.u.derived))
8649 gfc_error("Object '%s' at %L must have the SAVE attribute for "
8650 "default initialization of a component",
8651 sym->name, &sym->declared_at);
8655 if (sym->ts.type == BT_CLASS)
8658 if (!gfc_type_is_extensible (sym->ts.u.derived->components->ts.u.derived))
8660 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
8661 sym->ts.u.derived->name, sym->name, &sym->declared_at);
8666 /* Assume that use associated symbols were checked in the module ns. */
8667 if (!sym->attr.class_ok && !sym->attr.use_assoc)
8669 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
8670 "or pointer", sym->name, &sym->declared_at);
8675 /* Assign default initializer. */
8676 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
8677 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
8679 sym->value = gfc_default_initializer (&sym->ts);
8686 /* Resolve symbols with flavor variable. */
8689 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
8691 int no_init_flag, automatic_flag;
8693 const char *auto_save_msg;
8695 auto_save_msg = "Automatic object '%s' at %L cannot have the "
8698 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
8701 /* Set this flag to check that variables are parameters of all entries.
8702 This check is effected by the call to gfc_resolve_expr through
8703 is_non_constant_shape_array. */
8704 specification_expr = 1;
8706 if (sym->ns->proc_name
8707 && (sym->ns->proc_name->attr.flavor == FL_MODULE
8708 || sym->ns->proc_name->attr.is_main_program)
8709 && !sym->attr.use_assoc
8710 && !sym->attr.allocatable
8711 && !sym->attr.pointer
8712 && is_non_constant_shape_array (sym))
8714 /* The shape of a main program or module array needs to be
8716 gfc_error ("The module or main program array '%s' at %L must "
8717 "have constant shape", sym->name, &sym->declared_at);
8718 specification_expr = 0;
8722 if (sym->ts.type == BT_CHARACTER)
8724 /* Make sure that character string variables with assumed length are
8726 e = sym->ts.u.cl->length;
8727 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
8729 gfc_error ("Entity with assumed character length at %L must be a "
8730 "dummy argument or a PARAMETER", &sym->declared_at);
8734 if (e && sym->attr.save && !gfc_is_constant_expr (e))
8736 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
8740 if (!gfc_is_constant_expr (e)
8741 && !(e->expr_type == EXPR_VARIABLE
8742 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
8743 && sym->ns->proc_name
8744 && (sym->ns->proc_name->attr.flavor == FL_MODULE
8745 || sym->ns->proc_name->attr.is_main_program)
8746 && !sym->attr.use_assoc)
8748 gfc_error ("'%s' at %L must have constant character length "
8749 "in this context", sym->name, &sym->declared_at);
8754 if (sym->value == NULL && sym->attr.referenced)
8755 apply_default_init_local (sym); /* Try to apply a default initialization. */
8757 /* Determine if the symbol may not have an initializer. */
8758 no_init_flag = automatic_flag = 0;
8759 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
8760 || sym->attr.intrinsic || sym->attr.result)
8762 else if (sym->attr.dimension && !sym->attr.pointer
8763 && is_non_constant_shape_array (sym))
8765 no_init_flag = automatic_flag = 1;
8767 /* Also, they must not have the SAVE attribute.
8768 SAVE_IMPLICIT is checked below. */
8769 if (sym->attr.save == SAVE_EXPLICIT)
8771 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
8776 /* Ensure that any initializer is simplified. */
8778 gfc_simplify_expr (sym->value, 1);
8780 /* Reject illegal initializers. */
8781 if (!sym->mark && sym->value)
8783 if (sym->attr.allocatable)
8784 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
8785 sym->name, &sym->declared_at);
8786 else if (sym->attr.external)
8787 gfc_error ("External '%s' at %L cannot have an initializer",
8788 sym->name, &sym->declared_at);
8789 else if (sym->attr.dummy
8790 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
8791 gfc_error ("Dummy '%s' at %L cannot have an initializer",
8792 sym->name, &sym->declared_at);
8793 else if (sym->attr.intrinsic)
8794 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
8795 sym->name, &sym->declared_at);
8796 else if (sym->attr.result)
8797 gfc_error ("Function result '%s' at %L cannot have an initializer",
8798 sym->name, &sym->declared_at);
8799 else if (automatic_flag)
8800 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
8801 sym->name, &sym->declared_at);
8808 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
8809 return resolve_fl_variable_derived (sym, no_init_flag);
8815 /* Resolve a procedure. */
8818 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
8820 gfc_formal_arglist *arg;
8822 if (sym->attr.ambiguous_interfaces && !sym->attr.referenced)
8823 gfc_warning ("Although not referenced, '%s' at %L has ambiguous "
8824 "interfaces", sym->name, &sym->declared_at);
8826 if (sym->attr.function
8827 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
8830 if (sym->ts.type == BT_CHARACTER)
8832 gfc_charlen *cl = sym->ts.u.cl;
8834 if (cl && cl->length && gfc_is_constant_expr (cl->length)
8835 && resolve_charlen (cl) == FAILURE)
8838 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
8840 if (sym->attr.proc == PROC_ST_FUNCTION)
8842 gfc_error ("Character-valued statement function '%s' at %L must "
8843 "have constant length", sym->name, &sym->declared_at);
8847 if (sym->attr.external && sym->formal == NULL
8848 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
8850 gfc_error ("Automatic character length function '%s' at %L must "
8851 "have an explicit interface", sym->name,
8858 /* Ensure that derived type for are not of a private type. Internal
8859 module procedures are excluded by 2.2.3.3 - i.e., they are not
8860 externally accessible and can access all the objects accessible in
8862 if (!(sym->ns->parent
8863 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
8864 && gfc_check_access(sym->attr.access, sym->ns->default_access))
8866 gfc_interface *iface;
8868 for (arg = sym->formal; arg; arg = arg->next)
8871 && arg->sym->ts.type == BT_DERIVED
8872 && !arg->sym->ts.u.derived->attr.use_assoc
8873 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
8874 arg->sym->ts.u.derived->ns->default_access)
8875 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
8876 "PRIVATE type and cannot be a dummy argument"
8877 " of '%s', which is PUBLIC at %L",
8878 arg->sym->name, sym->name, &sym->declared_at)
8881 /* Stop this message from recurring. */
8882 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
8887 /* PUBLIC interfaces may expose PRIVATE procedures that take types
8888 PRIVATE to the containing module. */
8889 for (iface = sym->generic; iface; iface = iface->next)
8891 for (arg = iface->sym->formal; arg; arg = arg->next)
8894 && arg->sym->ts.type == BT_DERIVED
8895 && !arg->sym->ts.u.derived->attr.use_assoc
8896 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
8897 arg->sym->ts.u.derived->ns->default_access)
8898 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
8899 "'%s' in PUBLIC interface '%s' at %L "
8900 "takes dummy arguments of '%s' which is "
8901 "PRIVATE", iface->sym->name, sym->name,
8902 &iface->sym->declared_at,
8903 gfc_typename (&arg->sym->ts)) == FAILURE)
8905 /* Stop this message from recurring. */
8906 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
8912 /* PUBLIC interfaces may expose PRIVATE procedures that take types
8913 PRIVATE to the containing module. */
8914 for (iface = sym->generic; iface; iface = iface->next)
8916 for (arg = iface->sym->formal; arg; arg = arg->next)
8919 && arg->sym->ts.type == BT_DERIVED
8920 && !arg->sym->ts.u.derived->attr.use_assoc
8921 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
8922 arg->sym->ts.u.derived->ns->default_access)
8923 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
8924 "'%s' in PUBLIC interface '%s' at %L "
8925 "takes dummy arguments of '%s' which is "
8926 "PRIVATE", iface->sym->name, sym->name,
8927 &iface->sym->declared_at,
8928 gfc_typename (&arg->sym->ts)) == FAILURE)
8930 /* Stop this message from recurring. */
8931 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
8938 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
8939 && !sym->attr.proc_pointer)
8941 gfc_error ("Function '%s' at %L cannot have an initializer",
8942 sym->name, &sym->declared_at);
8946 /* An external symbol may not have an initializer because it is taken to be
8947 a procedure. Exception: Procedure Pointers. */
8948 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
8950 gfc_error ("External object '%s' at %L may not have an initializer",
8951 sym->name, &sym->declared_at);
8955 /* An elemental function is required to return a scalar 12.7.1 */
8956 if (sym->attr.elemental && sym->attr.function && sym->as)
8958 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
8959 "result", sym->name, &sym->declared_at);
8960 /* Reset so that the error only occurs once. */
8961 sym->attr.elemental = 0;
8965 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
8966 char-len-param shall not be array-valued, pointer-valued, recursive
8967 or pure. ....snip... A character value of * may only be used in the
8968 following ways: (i) Dummy arg of procedure - dummy associates with
8969 actual length; (ii) To declare a named constant; or (iii) External
8970 function - but length must be declared in calling scoping unit. */
8971 if (sym->attr.function
8972 && sym->ts.type == BT_CHARACTER
8973 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
8975 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
8976 || (sym->attr.recursive) || (sym->attr.pure))
8978 if (sym->as && sym->as->rank)
8979 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8980 "array-valued", sym->name, &sym->declared_at);
8982 if (sym->attr.pointer)
8983 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8984 "pointer-valued", sym->name, &sym->declared_at);
8987 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8988 "pure", sym->name, &sym->declared_at);
8990 if (sym->attr.recursive)
8991 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8992 "recursive", sym->name, &sym->declared_at);
8997 /* Appendix B.2 of the standard. Contained functions give an
8998 error anyway. Fixed-form is likely to be F77/legacy. */
8999 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
9000 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
9001 "CHARACTER(*) function '%s' at %L",
9002 sym->name, &sym->declared_at);
9005 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
9007 gfc_formal_arglist *curr_arg;
9008 int has_non_interop_arg = 0;
9010 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
9011 sym->common_block) == FAILURE)
9013 /* Clear these to prevent looking at them again if there was an
9015 sym->attr.is_bind_c = 0;
9016 sym->attr.is_c_interop = 0;
9017 sym->ts.is_c_interop = 0;
9021 /* So far, no errors have been found. */
9022 sym->attr.is_c_interop = 1;
9023 sym->ts.is_c_interop = 1;
9026 curr_arg = sym->formal;
9027 while (curr_arg != NULL)
9029 /* Skip implicitly typed dummy args here. */
9030 if (curr_arg->sym->attr.implicit_type == 0)
9031 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
9032 /* If something is found to fail, record the fact so we
9033 can mark the symbol for the procedure as not being
9034 BIND(C) to try and prevent multiple errors being
9036 has_non_interop_arg = 1;
9038 curr_arg = curr_arg->next;
9041 /* See if any of the arguments were not interoperable and if so, clear
9042 the procedure symbol to prevent duplicate error messages. */
9043 if (has_non_interop_arg != 0)
9045 sym->attr.is_c_interop = 0;
9046 sym->ts.is_c_interop = 0;
9047 sym->attr.is_bind_c = 0;
9051 if (!sym->attr.proc_pointer)
9053 if (sym->attr.save == SAVE_EXPLICIT)
9055 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
9056 "in '%s' at %L", sym->name, &sym->declared_at);
9059 if (sym->attr.intent)
9061 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
9062 "in '%s' at %L", sym->name, &sym->declared_at);
9065 if (sym->attr.subroutine && sym->attr.result)
9067 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
9068 "in '%s' at %L", sym->name, &sym->declared_at);
9071 if (sym->attr.external && sym->attr.function
9072 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
9073 || sym->attr.contained))
9075 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
9076 "in '%s' at %L", sym->name, &sym->declared_at);
9079 if (strcmp ("ppr@", sym->name) == 0)
9081 gfc_error ("Procedure pointer result '%s' at %L "
9082 "is missing the pointer attribute",
9083 sym->ns->proc_name->name, &sym->declared_at);
9092 /* Resolve a list of finalizer procedures. That is, after they have hopefully
9093 been defined and we now know their defined arguments, check that they fulfill
9094 the requirements of the standard for procedures used as finalizers. */
9097 gfc_resolve_finalizers (gfc_symbol* derived)
9099 gfc_finalizer* list;
9100 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
9101 gfc_try result = SUCCESS;
9102 bool seen_scalar = false;
9104 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
9107 /* Walk over the list of finalizer-procedures, check them, and if any one
9108 does not fit in with the standard's definition, print an error and remove
9109 it from the list. */
9110 prev_link = &derived->f2k_derived->finalizers;
9111 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
9117 /* Skip this finalizer if we already resolved it. */
9118 if (list->proc_tree)
9120 prev_link = &(list->next);
9124 /* Check this exists and is a SUBROUTINE. */
9125 if (!list->proc_sym->attr.subroutine)
9127 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
9128 list->proc_sym->name, &list->where);
9132 /* We should have exactly one argument. */
9133 if (!list->proc_sym->formal || list->proc_sym->formal->next)
9135 gfc_error ("FINAL procedure at %L must have exactly one argument",
9139 arg = list->proc_sym->formal->sym;
9141 /* This argument must be of our type. */
9142 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
9144 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
9145 &arg->declared_at, derived->name);
9149 /* It must neither be a pointer nor allocatable nor optional. */
9150 if (arg->attr.pointer)
9152 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
9156 if (arg->attr.allocatable)
9158 gfc_error ("Argument of FINAL procedure at %L must not be"
9159 " ALLOCATABLE", &arg->declared_at);
9162 if (arg->attr.optional)
9164 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
9169 /* It must not be INTENT(OUT). */
9170 if (arg->attr.intent == INTENT_OUT)
9172 gfc_error ("Argument of FINAL procedure at %L must not be"
9173 " INTENT(OUT)", &arg->declared_at);
9177 /* Warn if the procedure is non-scalar and not assumed shape. */
9178 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
9179 && arg->as->type != AS_ASSUMED_SHAPE)
9180 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
9181 " shape argument", &arg->declared_at);
9183 /* Check that it does not match in kind and rank with a FINAL procedure
9184 defined earlier. To really loop over the *earlier* declarations,
9185 we need to walk the tail of the list as new ones were pushed at the
9187 /* TODO: Handle kind parameters once they are implemented. */
9188 my_rank = (arg->as ? arg->as->rank : 0);
9189 for (i = list->next; i; i = i->next)
9191 /* Argument list might be empty; that is an error signalled earlier,
9192 but we nevertheless continued resolving. */
9193 if (i->proc_sym->formal)
9195 gfc_symbol* i_arg = i->proc_sym->formal->sym;
9196 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
9197 if (i_rank == my_rank)
9199 gfc_error ("FINAL procedure '%s' declared at %L has the same"
9200 " rank (%d) as '%s'",
9201 list->proc_sym->name, &list->where, my_rank,
9208 /* Is this the/a scalar finalizer procedure? */
9209 if (!arg->as || arg->as->rank == 0)
9212 /* Find the symtree for this procedure. */
9213 gcc_assert (!list->proc_tree);
9214 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
9216 prev_link = &list->next;
9219 /* Remove wrong nodes immediately from the list so we don't risk any
9220 troubles in the future when they might fail later expectations. */
9224 *prev_link = list->next;
9225 gfc_free_finalizer (i);
9228 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
9229 were nodes in the list, must have been for arrays. It is surely a good
9230 idea to have a scalar version there if there's something to finalize. */
9231 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
9232 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
9233 " defined at %L, suggest also scalar one",
9234 derived->name, &derived->declared_at);
9236 /* TODO: Remove this error when finalization is finished. */
9237 gfc_error ("Finalization at %L is not yet implemented",
9238 &derived->declared_at);
9244 /* Check that it is ok for the typebound procedure proc to override the
9248 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
9251 const gfc_symbol* proc_target;
9252 const gfc_symbol* old_target;
9253 unsigned proc_pass_arg, old_pass_arg, argpos;
9254 gfc_formal_arglist* proc_formal;
9255 gfc_formal_arglist* old_formal;
9257 /* This procedure should only be called for non-GENERIC proc. */
9258 gcc_assert (!proc->n.tb->is_generic);
9260 /* If the overwritten procedure is GENERIC, this is an error. */
9261 if (old->n.tb->is_generic)
9263 gfc_error ("Can't overwrite GENERIC '%s' at %L",
9264 old->name, &proc->n.tb->where);
9268 where = proc->n.tb->where;
9269 proc_target = proc->n.tb->u.specific->n.sym;
9270 old_target = old->n.tb->u.specific->n.sym;
9272 /* Check that overridden binding is not NON_OVERRIDABLE. */
9273 if (old->n.tb->non_overridable)
9275 gfc_error ("'%s' at %L overrides a procedure binding declared"
9276 " NON_OVERRIDABLE", proc->name, &where);
9280 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
9281 if (!old->n.tb->deferred && proc->n.tb->deferred)
9283 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
9284 " non-DEFERRED binding", proc->name, &where);
9288 /* If the overridden binding is PURE, the overriding must be, too. */
9289 if (old_target->attr.pure && !proc_target->attr.pure)
9291 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
9292 proc->name, &where);
9296 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
9297 is not, the overriding must not be either. */
9298 if (old_target->attr.elemental && !proc_target->attr.elemental)
9300 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
9301 " ELEMENTAL", proc->name, &where);
9304 if (!old_target->attr.elemental && proc_target->attr.elemental)
9306 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
9307 " be ELEMENTAL, either", proc->name, &where);
9311 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
9313 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
9315 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
9316 " SUBROUTINE", proc->name, &where);
9320 /* If the overridden binding is a FUNCTION, the overriding must also be a
9321 FUNCTION and have the same characteristics. */
9322 if (old_target->attr.function)
9324 if (!proc_target->attr.function)
9326 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
9327 " FUNCTION", proc->name, &where);
9331 /* FIXME: Do more comprehensive checking (including, for instance, the
9332 rank and array-shape). */
9333 gcc_assert (proc_target->result && old_target->result);
9334 if (!gfc_compare_types (&proc_target->result->ts,
9335 &old_target->result->ts))
9337 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
9338 " matching result types", proc->name, &where);
9343 /* If the overridden binding is PUBLIC, the overriding one must not be
9345 if (old->n.tb->access == ACCESS_PUBLIC
9346 && proc->n.tb->access == ACCESS_PRIVATE)
9348 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
9349 " PRIVATE", proc->name, &where);
9353 /* Compare the formal argument lists of both procedures. This is also abused
9354 to find the position of the passed-object dummy arguments of both
9355 bindings as at least the overridden one might not yet be resolved and we
9356 need those positions in the check below. */
9357 proc_pass_arg = old_pass_arg = 0;
9358 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
9360 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
9363 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
9364 proc_formal && old_formal;
9365 proc_formal = proc_formal->next, old_formal = old_formal->next)
9367 if (proc->n.tb->pass_arg
9368 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
9369 proc_pass_arg = argpos;
9370 if (old->n.tb->pass_arg
9371 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
9372 old_pass_arg = argpos;
9374 /* Check that the names correspond. */
9375 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
9377 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
9378 " to match the corresponding argument of the overridden"
9379 " procedure", proc_formal->sym->name, proc->name, &where,
9380 old_formal->sym->name);
9384 /* Check that the types correspond if neither is the passed-object
9386 /* FIXME: Do more comprehensive testing here. */
9387 if (proc_pass_arg != argpos && old_pass_arg != argpos
9388 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
9390 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L in"
9391 " in respect to the overridden procedure",
9392 proc_formal->sym->name, proc->name, &where);
9398 if (proc_formal || old_formal)
9400 gfc_error ("'%s' at %L must have the same number of formal arguments as"
9401 " the overridden procedure", proc->name, &where);
9405 /* If the overridden binding is NOPASS, the overriding one must also be
9407 if (old->n.tb->nopass && !proc->n.tb->nopass)
9409 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
9410 " NOPASS", proc->name, &where);
9414 /* If the overridden binding is PASS(x), the overriding one must also be
9415 PASS and the passed-object dummy arguments must correspond. */
9416 if (!old->n.tb->nopass)
9418 if (proc->n.tb->nopass)
9420 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
9421 " PASS", proc->name, &where);
9425 if (proc_pass_arg != old_pass_arg)
9427 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
9428 " the same position as the passed-object dummy argument of"
9429 " the overridden procedure", proc->name, &where);
9438 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
9441 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
9442 const char* generic_name, locus where)
9447 gcc_assert (t1->specific && t2->specific);
9448 gcc_assert (!t1->specific->is_generic);
9449 gcc_assert (!t2->specific->is_generic);
9451 sym1 = t1->specific->u.specific->n.sym;
9452 sym2 = t2->specific->u.specific->n.sym;
9457 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
9458 if (sym1->attr.subroutine != sym2->attr.subroutine
9459 || sym1->attr.function != sym2->attr.function)
9461 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
9462 " GENERIC '%s' at %L",
9463 sym1->name, sym2->name, generic_name, &where);
9467 /* Compare the interfaces. */
9468 if (gfc_compare_interfaces (sym1, sym2, NULL, 1, 0, NULL, 0))
9470 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
9471 sym1->name, sym2->name, generic_name, &where);
9479 /* Worker function for resolving a generic procedure binding; this is used to
9480 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
9482 The difference between those cases is finding possible inherited bindings
9483 that are overridden, as one has to look for them in tb_sym_root,
9484 tb_uop_root or tb_op, respectively. Thus the caller must already find
9485 the super-type and set p->overridden correctly. */
9488 resolve_tb_generic_targets (gfc_symbol* super_type,
9489 gfc_typebound_proc* p, const char* name)
9491 gfc_tbp_generic* target;
9492 gfc_symtree* first_target;
9493 gfc_symtree* inherited;
9495 gcc_assert (p && p->is_generic);
9497 /* Try to find the specific bindings for the symtrees in our target-list. */
9498 gcc_assert (p->u.generic);
9499 for (target = p->u.generic; target; target = target->next)
9500 if (!target->specific)
9502 gfc_typebound_proc* overridden_tbp;
9504 const char* target_name;
9506 target_name = target->specific_st->name;
9508 /* Defined for this type directly. */
9509 if (target->specific_st->n.tb)
9511 target->specific = target->specific_st->n.tb;
9512 goto specific_found;
9515 /* Look for an inherited specific binding. */
9518 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
9523 gcc_assert (inherited->n.tb);
9524 target->specific = inherited->n.tb;
9525 goto specific_found;
9529 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
9530 " at %L", target_name, name, &p->where);
9533 /* Once we've found the specific binding, check it is not ambiguous with
9534 other specifics already found or inherited for the same GENERIC. */
9536 gcc_assert (target->specific);
9538 /* This must really be a specific binding! */
9539 if (target->specific->is_generic)
9541 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
9542 " '%s' is GENERIC, too", name, &p->where, target_name);
9546 /* Check those already resolved on this type directly. */
9547 for (g = p->u.generic; g; g = g->next)
9548 if (g != target && g->specific
9549 && check_generic_tbp_ambiguity (target, g, name, p->where)
9553 /* Check for ambiguity with inherited specific targets. */
9554 for (overridden_tbp = p->overridden; overridden_tbp;
9555 overridden_tbp = overridden_tbp->overridden)
9556 if (overridden_tbp->is_generic)
9558 for (g = overridden_tbp->u.generic; g; g = g->next)
9560 gcc_assert (g->specific);
9561 if (check_generic_tbp_ambiguity (target, g,
9562 name, p->where) == FAILURE)
9568 /* If we attempt to "overwrite" a specific binding, this is an error. */
9569 if (p->overridden && !p->overridden->is_generic)
9571 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
9572 " the same name", name, &p->where);
9576 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
9577 all must have the same attributes here. */
9578 first_target = p->u.generic->specific->u.specific;
9579 gcc_assert (first_target);
9580 p->subroutine = first_target->n.sym->attr.subroutine;
9581 p->function = first_target->n.sym->attr.function;
9587 /* Resolve a GENERIC procedure binding for a derived type. */
9590 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
9592 gfc_symbol* super_type;
9594 /* Find the overridden binding if any. */
9595 st->n.tb->overridden = NULL;
9596 super_type = gfc_get_derived_super_type (derived);
9599 gfc_symtree* overridden;
9600 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
9603 if (overridden && overridden->n.tb)
9604 st->n.tb->overridden = overridden->n.tb;
9607 /* Resolve using worker function. */
9608 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
9612 /* Retrieve the target-procedure of an operator binding and do some checks in
9613 common for intrinsic and user-defined type-bound operators. */
9616 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
9618 gfc_symbol* target_proc;
9620 gcc_assert (target->specific && !target->specific->is_generic);
9621 target_proc = target->specific->u.specific->n.sym;
9622 gcc_assert (target_proc);
9624 /* All operator bindings must have a passed-object dummy argument. */
9625 if (target->specific->nopass)
9627 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
9635 /* Resolve a type-bound intrinsic operator. */
9638 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
9639 gfc_typebound_proc* p)
9641 gfc_symbol* super_type;
9642 gfc_tbp_generic* target;
9644 /* If there's already an error here, do nothing (but don't fail again). */
9648 /* Operators should always be GENERIC bindings. */
9649 gcc_assert (p->is_generic);
9651 /* Look for an overridden binding. */
9652 super_type = gfc_get_derived_super_type (derived);
9653 if (super_type && super_type->f2k_derived)
9654 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
9657 p->overridden = NULL;
9659 /* Resolve general GENERIC properties using worker function. */
9660 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
9663 /* Check the targets to be procedures of correct interface. */
9664 for (target = p->u.generic; target; target = target->next)
9666 gfc_symbol* target_proc;
9668 target_proc = get_checked_tb_operator_target (target, p->where);
9672 if (!gfc_check_operator_interface (target_proc, op, p->where))
9684 /* Resolve a type-bound user operator (tree-walker callback). */
9686 static gfc_symbol* resolve_bindings_derived;
9687 static gfc_try resolve_bindings_result;
9689 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
9692 resolve_typebound_user_op (gfc_symtree* stree)
9694 gfc_symbol* super_type;
9695 gfc_tbp_generic* target;
9697 gcc_assert (stree && stree->n.tb);
9699 if (stree->n.tb->error)
9702 /* Operators should always be GENERIC bindings. */
9703 gcc_assert (stree->n.tb->is_generic);
9705 /* Find overridden procedure, if any. */
9706 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
9707 if (super_type && super_type->f2k_derived)
9709 gfc_symtree* overridden;
9710 overridden = gfc_find_typebound_user_op (super_type, NULL,
9711 stree->name, true, NULL);
9713 if (overridden && overridden->n.tb)
9714 stree->n.tb->overridden = overridden->n.tb;
9717 stree->n.tb->overridden = NULL;
9719 /* Resolve basically using worker function. */
9720 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
9724 /* Check the targets to be functions of correct interface. */
9725 for (target = stree->n.tb->u.generic; target; target = target->next)
9727 gfc_symbol* target_proc;
9729 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
9733 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
9740 resolve_bindings_result = FAILURE;
9741 stree->n.tb->error = 1;
9745 /* Resolve the type-bound procedures for a derived type. */
9748 resolve_typebound_procedure (gfc_symtree* stree)
9753 gfc_symbol* super_type;
9754 gfc_component* comp;
9758 /* Undefined specific symbol from GENERIC target definition. */
9762 if (stree->n.tb->error)
9765 /* If this is a GENERIC binding, use that routine. */
9766 if (stree->n.tb->is_generic)
9768 if (resolve_typebound_generic (resolve_bindings_derived, stree)
9774 /* Get the target-procedure to check it. */
9775 gcc_assert (!stree->n.tb->is_generic);
9776 gcc_assert (stree->n.tb->u.specific);
9777 proc = stree->n.tb->u.specific->n.sym;
9778 where = stree->n.tb->where;
9780 /* Default access should already be resolved from the parser. */
9781 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
9783 /* It should be a module procedure or an external procedure with explicit
9784 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
9785 if ((!proc->attr.subroutine && !proc->attr.function)
9786 || (proc->attr.proc != PROC_MODULE
9787 && proc->attr.if_source != IFSRC_IFBODY)
9788 || (proc->attr.abstract && !stree->n.tb->deferred))
9790 gfc_error ("'%s' must be a module procedure or an external procedure with"
9791 " an explicit interface at %L", proc->name, &where);
9794 stree->n.tb->subroutine = proc->attr.subroutine;
9795 stree->n.tb->function = proc->attr.function;
9797 /* Find the super-type of the current derived type. We could do this once and
9798 store in a global if speed is needed, but as long as not I believe this is
9799 more readable and clearer. */
9800 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
9802 /* If PASS, resolve and check arguments if not already resolved / loaded
9803 from a .mod file. */
9804 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
9806 if (stree->n.tb->pass_arg)
9808 gfc_formal_arglist* i;
9810 /* If an explicit passing argument name is given, walk the arg-list
9814 stree->n.tb->pass_arg_num = 1;
9815 for (i = proc->formal; i; i = i->next)
9817 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
9822 ++stree->n.tb->pass_arg_num;
9827 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
9829 proc->name, stree->n.tb->pass_arg, &where,
9830 stree->n.tb->pass_arg);
9836 /* Otherwise, take the first one; there should in fact be at least
9838 stree->n.tb->pass_arg_num = 1;
9841 gfc_error ("Procedure '%s' with PASS at %L must have at"
9842 " least one argument", proc->name, &where);
9845 me_arg = proc->formal->sym;
9848 /* Now check that the argument-type matches. */
9849 gcc_assert (me_arg);
9850 if (me_arg->ts.type != BT_CLASS)
9852 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
9853 " at %L", proc->name, &where);
9857 if (me_arg->ts.u.derived->components->ts.u.derived
9858 != resolve_bindings_derived)
9860 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
9861 " the derived-type '%s'", me_arg->name, proc->name,
9862 me_arg->name, &where, resolve_bindings_derived->name);
9868 /* If we are extending some type, check that we don't override a procedure
9869 flagged NON_OVERRIDABLE. */
9870 stree->n.tb->overridden = NULL;
9873 gfc_symtree* overridden;
9874 overridden = gfc_find_typebound_proc (super_type, NULL,
9875 stree->name, true, NULL);
9877 if (overridden && overridden->n.tb)
9878 stree->n.tb->overridden = overridden->n.tb;
9880 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
9884 /* See if there's a name collision with a component directly in this type. */
9885 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
9886 if (!strcmp (comp->name, stree->name))
9888 gfc_error ("Procedure '%s' at %L has the same name as a component of"
9890 stree->name, &where, resolve_bindings_derived->name);
9894 /* Try to find a name collision with an inherited component. */
9895 if (super_type && gfc_find_component (super_type, stree->name, true, true))
9897 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
9898 " component of '%s'",
9899 stree->name, &where, resolve_bindings_derived->name);
9903 stree->n.tb->error = 0;
9907 resolve_bindings_result = FAILURE;
9908 stree->n.tb->error = 1;
9912 resolve_typebound_procedures (gfc_symbol* derived)
9916 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
9919 resolve_bindings_derived = derived;
9920 resolve_bindings_result = SUCCESS;
9922 if (derived->f2k_derived->tb_sym_root)
9923 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
9924 &resolve_typebound_procedure);
9926 if (derived->f2k_derived->tb_uop_root)
9927 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
9928 &resolve_typebound_user_op);
9930 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
9932 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
9933 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
9935 resolve_bindings_result = FAILURE;
9938 return resolve_bindings_result;
9942 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
9943 to give all identical derived types the same backend_decl. */
9945 add_dt_to_dt_list (gfc_symbol *derived)
9947 gfc_dt_list *dt_list;
9949 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
9950 if (derived == dt_list->derived)
9953 if (dt_list == NULL)
9955 dt_list = gfc_get_dt_list ();
9956 dt_list->next = gfc_derived_types;
9957 dt_list->derived = derived;
9958 gfc_derived_types = dt_list;
9963 /* Ensure that a derived-type is really not abstract, meaning that every
9964 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
9967 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
9972 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
9974 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
9977 if (st->n.tb && st->n.tb->deferred)
9979 gfc_symtree* overriding;
9980 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
9981 gcc_assert (overriding && overriding->n.tb);
9982 if (overriding->n.tb->deferred)
9984 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
9985 " '%s' is DEFERRED and not overridden",
9986 sub->name, &sub->declared_at, st->name);
9995 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
9997 /* The algorithm used here is to recursively travel up the ancestry of sub
9998 and for each ancestor-type, check all bindings. If any of them is
9999 DEFERRED, look it up starting from sub and see if the found (overriding)
10000 binding is not DEFERRED.
10001 This is not the most efficient way to do this, but it should be ok and is
10002 clearer than something sophisticated. */
10004 gcc_assert (ancestor && ancestor->attr.abstract && !sub->attr.abstract);
10006 /* Walk bindings of this ancestor. */
10007 if (ancestor->f2k_derived)
10010 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
10015 /* Find next ancestor type and recurse on it. */
10016 ancestor = gfc_get_derived_super_type (ancestor);
10018 return ensure_not_abstract (sub, ancestor);
10024 static void resolve_symbol (gfc_symbol *sym);
10027 /* Resolve the components of a derived type. */
10030 resolve_fl_derived (gfc_symbol *sym)
10032 gfc_symbol* super_type;
10036 super_type = gfc_get_derived_super_type (sym);
10038 /* Ensure the extended type gets resolved before we do. */
10039 if (super_type && resolve_fl_derived (super_type) == FAILURE)
10042 /* An ABSTRACT type must be extensible. */
10043 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
10045 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
10046 sym->name, &sym->declared_at);
10050 for (c = sym->components; c != NULL; c = c->next)
10052 if (c->attr.proc_pointer && c->ts.interface)
10054 if (c->ts.interface->attr.procedure)
10055 gfc_error ("Interface '%s', used by procedure pointer component "
10056 "'%s' at %L, is declared in a later PROCEDURE statement",
10057 c->ts.interface->name, c->name, &c->loc);
10059 /* Get the attributes from the interface (now resolved). */
10060 if (c->ts.interface->attr.if_source
10061 || c->ts.interface->attr.intrinsic)
10063 gfc_symbol *ifc = c->ts.interface;
10065 if (ifc->formal && !ifc->formal_ns)
10066 resolve_symbol (ifc);
10068 if (ifc->attr.intrinsic)
10069 resolve_intrinsic (ifc, &ifc->declared_at);
10073 c->ts = ifc->result->ts;
10074 c->attr.allocatable = ifc->result->attr.allocatable;
10075 c->attr.pointer = ifc->result->attr.pointer;
10076 c->attr.dimension = ifc->result->attr.dimension;
10077 c->as = gfc_copy_array_spec (ifc->result->as);
10082 c->attr.allocatable = ifc->attr.allocatable;
10083 c->attr.pointer = ifc->attr.pointer;
10084 c->attr.dimension = ifc->attr.dimension;
10085 c->as = gfc_copy_array_spec (ifc->as);
10087 c->ts.interface = ifc;
10088 c->attr.function = ifc->attr.function;
10089 c->attr.subroutine = ifc->attr.subroutine;
10090 gfc_copy_formal_args_ppc (c, ifc);
10092 c->attr.pure = ifc->attr.pure;
10093 c->attr.elemental = ifc->attr.elemental;
10094 c->attr.recursive = ifc->attr.recursive;
10095 c->attr.always_explicit = ifc->attr.always_explicit;
10096 c->attr.ext_attr |= ifc->attr.ext_attr;
10097 /* Replace symbols in array spec. */
10101 for (i = 0; i < c->as->rank; i++)
10103 gfc_expr_replace_comp (c->as->lower[i], c);
10104 gfc_expr_replace_comp (c->as->upper[i], c);
10107 /* Copy char length. */
10108 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
10110 c->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
10111 gfc_expr_replace_comp (c->ts.u.cl->length, c);
10114 else if (c->ts.interface->name[0] != '\0')
10116 gfc_error ("Interface '%s' of procedure pointer component "
10117 "'%s' at %L must be explicit", c->ts.interface->name,
10122 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
10124 c->ts = *gfc_get_default_type (c->name, NULL);
10125 c->attr.implicit_type = 1;
10128 /* Procedure pointer components: Check PASS arg. */
10129 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0)
10131 gfc_symbol* me_arg;
10133 if (c->tb->pass_arg)
10135 gfc_formal_arglist* i;
10137 /* If an explicit passing argument name is given, walk the arg-list
10138 and look for it. */
10141 c->tb->pass_arg_num = 1;
10142 for (i = c->formal; i; i = i->next)
10144 if (!strcmp (i->sym->name, c->tb->pass_arg))
10149 c->tb->pass_arg_num++;
10154 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
10155 "at %L has no argument '%s'", c->name,
10156 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
10163 /* Otherwise, take the first one; there should in fact be at least
10165 c->tb->pass_arg_num = 1;
10168 gfc_error ("Procedure pointer component '%s' with PASS at %L "
10169 "must have at least one argument",
10174 me_arg = c->formal->sym;
10177 /* Now check that the argument-type matches. */
10178 gcc_assert (me_arg);
10179 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
10180 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
10181 || (me_arg->ts.type == BT_CLASS
10182 && me_arg->ts.u.derived->components->ts.u.derived != sym))
10184 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10185 " the derived type '%s'", me_arg->name, c->name,
10186 me_arg->name, &c->loc, sym->name);
10191 /* Check for C453. */
10192 if (me_arg->attr.dimension)
10194 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10195 "must be scalar", me_arg->name, c->name, me_arg->name,
10201 if (me_arg->attr.pointer)
10203 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10204 "may not have the POINTER attribute", me_arg->name,
10205 c->name, me_arg->name, &c->loc);
10210 if (me_arg->attr.allocatable)
10212 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10213 "may not be ALLOCATABLE", me_arg->name, c->name,
10214 me_arg->name, &c->loc);
10219 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
10220 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10221 " at %L", c->name, &c->loc);
10225 /* Check type-spec if this is not the parent-type component. */
10226 if ((!sym->attr.extension || c != sym->components)
10227 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
10230 /* If this type is an extension, see if this component has the same name
10231 as an inherited type-bound procedure. */
10233 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
10235 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
10236 " inherited type-bound procedure",
10237 c->name, sym->name, &c->loc);
10241 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
10243 if (c->ts.u.cl->length == NULL
10244 || (resolve_charlen (c->ts.u.cl) == FAILURE)
10245 || !gfc_is_constant_expr (c->ts.u.cl->length))
10247 gfc_error ("Character length of component '%s' needs to "
10248 "be a constant specification expression at %L",
10250 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
10255 if (c->ts.type == BT_DERIVED
10256 && sym->component_access != ACCESS_PRIVATE
10257 && gfc_check_access (sym->attr.access, sym->ns->default_access)
10258 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
10259 && !c->ts.u.derived->attr.use_assoc
10260 && !gfc_check_access (c->ts.u.derived->attr.access,
10261 c->ts.u.derived->ns->default_access)
10262 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
10263 "is a PRIVATE type and cannot be a component of "
10264 "'%s', which is PUBLIC at %L", c->name,
10265 sym->name, &sym->declared_at) == FAILURE)
10268 if (sym->attr.sequence)
10270 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
10272 gfc_error ("Component %s of SEQUENCE type declared at %L does "
10273 "not have the SEQUENCE attribute",
10274 c->ts.u.derived->name, &sym->declared_at);
10279 if (c->ts.type == BT_DERIVED && c->attr.pointer
10280 && c->ts.u.derived->components == NULL
10281 && !c->ts.u.derived->attr.zero_comp)
10283 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
10284 "that has not been declared", c->name, sym->name,
10290 if (c->ts.type == BT_CLASS
10291 && !(c->ts.u.derived->components->attr.pointer
10292 || c->ts.u.derived->components->attr.allocatable))
10294 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
10295 "or pointer", c->name, &c->loc);
10299 /* Ensure that all the derived type components are put on the
10300 derived type list; even in formal namespaces, where derived type
10301 pointer components might not have been declared. */
10302 if (c->ts.type == BT_DERIVED
10304 && c->ts.u.derived->components
10306 && sym != c->ts.u.derived)
10307 add_dt_to_dt_list (c->ts.u.derived);
10309 if (c->attr.pointer || c->attr.proc_pointer || c->attr.allocatable
10313 for (i = 0; i < c->as->rank; i++)
10315 if (c->as->lower[i] == NULL
10316 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
10317 || !gfc_is_constant_expr (c->as->lower[i])
10318 || c->as->upper[i] == NULL
10319 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
10320 || !gfc_is_constant_expr (c->as->upper[i]))
10322 gfc_error ("Component '%s' of '%s' at %L must have "
10323 "constant array bounds",
10324 c->name, sym->name, &c->loc);
10330 /* Resolve the type-bound procedures. */
10331 if (resolve_typebound_procedures (sym) == FAILURE)
10334 /* Resolve the finalizer procedures. */
10335 if (gfc_resolve_finalizers (sym) == FAILURE)
10338 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
10339 all DEFERRED bindings are overridden. */
10340 if (super_type && super_type->attr.abstract && !sym->attr.abstract
10341 && ensure_not_abstract (sym, super_type) == FAILURE)
10344 /* Add derived type to the derived type list. */
10345 add_dt_to_dt_list (sym);
10352 resolve_fl_namelist (gfc_symbol *sym)
10357 /* Reject PRIVATE objects in a PUBLIC namelist. */
10358 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
10360 for (nl = sym->namelist; nl; nl = nl->next)
10362 if (!nl->sym->attr.use_assoc
10363 && !is_sym_host_assoc (nl->sym, sym->ns)
10364 && !gfc_check_access(nl->sym->attr.access,
10365 nl->sym->ns->default_access))
10367 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
10368 "cannot be member of PUBLIC namelist '%s' at %L",
10369 nl->sym->name, sym->name, &sym->declared_at);
10373 /* Types with private components that came here by USE-association. */
10374 if (nl->sym->ts.type == BT_DERIVED
10375 && derived_inaccessible (nl->sym->ts.u.derived))
10377 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
10378 "components and cannot be member of namelist '%s' at %L",
10379 nl->sym->name, sym->name, &sym->declared_at);
10383 /* Types with private components that are defined in the same module. */
10384 if (nl->sym->ts.type == BT_DERIVED
10385 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
10386 && !gfc_check_access (nl->sym->ts.u.derived->attr.private_comp
10387 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
10388 nl->sym->ns->default_access))
10390 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
10391 "cannot be a member of PUBLIC namelist '%s' at %L",
10392 nl->sym->name, sym->name, &sym->declared_at);
10398 for (nl = sym->namelist; nl; nl = nl->next)
10400 /* Reject namelist arrays of assumed shape. */
10401 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
10402 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
10403 "must not have assumed shape in namelist "
10404 "'%s' at %L", nl->sym->name, sym->name,
10405 &sym->declared_at) == FAILURE)
10408 /* Reject namelist arrays that are not constant shape. */
10409 if (is_non_constant_shape_array (nl->sym))
10411 gfc_error ("NAMELIST array object '%s' must have constant "
10412 "shape in namelist '%s' at %L", nl->sym->name,
10413 sym->name, &sym->declared_at);
10417 /* Namelist objects cannot have allocatable or pointer components. */
10418 if (nl->sym->ts.type != BT_DERIVED)
10421 if (nl->sym->ts.u.derived->attr.alloc_comp)
10423 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10424 "have ALLOCATABLE components",
10425 nl->sym->name, sym->name, &sym->declared_at);
10429 if (nl->sym->ts.u.derived->attr.pointer_comp)
10431 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10432 "have POINTER components",
10433 nl->sym->name, sym->name, &sym->declared_at);
10439 /* 14.1.2 A module or internal procedure represent local entities
10440 of the same type as a namelist member and so are not allowed. */
10441 for (nl = sym->namelist; nl; nl = nl->next)
10443 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
10446 if (nl->sym->attr.function && nl->sym == nl->sym->result)
10447 if ((nl->sym == sym->ns->proc_name)
10449 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
10453 if (nl->sym && nl->sym->name)
10454 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
10455 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
10457 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
10458 "attribute in '%s' at %L", nlsym->name,
10459 &sym->declared_at);
10469 resolve_fl_parameter (gfc_symbol *sym)
10471 /* A parameter array's shape needs to be constant. */
10472 if (sym->as != NULL
10473 && (sym->as->type == AS_DEFERRED
10474 || is_non_constant_shape_array (sym)))
10476 gfc_error ("Parameter array '%s' at %L cannot be automatic "
10477 "or of deferred shape", sym->name, &sym->declared_at);
10481 /* Make sure a parameter that has been implicitly typed still
10482 matches the implicit type, since PARAMETER statements can precede
10483 IMPLICIT statements. */
10484 if (sym->attr.implicit_type
10485 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
10488 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
10489 "later IMPLICIT type", sym->name, &sym->declared_at);
10493 /* Make sure the types of derived parameters are consistent. This
10494 type checking is deferred until resolution because the type may
10495 refer to a derived type from the host. */
10496 if (sym->ts.type == BT_DERIVED
10497 && !gfc_compare_types (&sym->ts, &sym->value->ts))
10499 gfc_error ("Incompatible derived type in PARAMETER at %L",
10500 &sym->value->where);
10507 /* Do anything necessary to resolve a symbol. Right now, we just
10508 assume that an otherwise unknown symbol is a variable. This sort
10509 of thing commonly happens for symbols in module. */
10512 resolve_symbol (gfc_symbol *sym)
10514 int check_constant, mp_flag;
10515 gfc_symtree *symtree;
10516 gfc_symtree *this_symtree;
10520 if (sym->attr.flavor == FL_UNKNOWN)
10523 /* If we find that a flavorless symbol is an interface in one of the
10524 parent namespaces, find its symtree in this namespace, free the
10525 symbol and set the symtree to point to the interface symbol. */
10526 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
10528 symtree = gfc_find_symtree (ns->sym_root, sym->name);
10529 if (symtree && symtree->n.sym->generic)
10531 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10535 gfc_free_symbol (sym);
10536 symtree->n.sym->refs++;
10537 this_symtree->n.sym = symtree->n.sym;
10542 /* Otherwise give it a flavor according to such attributes as
10544 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
10545 sym->attr.flavor = FL_VARIABLE;
10548 sym->attr.flavor = FL_PROCEDURE;
10549 if (sym->attr.dimension)
10550 sym->attr.function = 1;
10554 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
10555 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
10557 if (sym->attr.procedure && sym->ts.interface
10558 && sym->attr.if_source != IFSRC_DECL)
10560 if (sym->ts.interface == sym)
10562 gfc_error ("PROCEDURE '%s' at %L may not be used as its own "
10563 "interface", sym->name, &sym->declared_at);
10566 if (sym->ts.interface->attr.procedure)
10568 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared"
10569 " in a later PROCEDURE statement", sym->ts.interface->name,
10570 sym->name,&sym->declared_at);
10574 /* Get the attributes from the interface (now resolved). */
10575 if (sym->ts.interface->attr.if_source
10576 || sym->ts.interface->attr.intrinsic)
10578 gfc_symbol *ifc = sym->ts.interface;
10579 resolve_symbol (ifc);
10581 if (ifc->attr.intrinsic)
10582 resolve_intrinsic (ifc, &ifc->declared_at);
10585 sym->ts = ifc->result->ts;
10588 sym->ts.interface = ifc;
10589 sym->attr.function = ifc->attr.function;
10590 sym->attr.subroutine = ifc->attr.subroutine;
10591 gfc_copy_formal_args (sym, ifc);
10593 sym->attr.allocatable = ifc->attr.allocatable;
10594 sym->attr.pointer = ifc->attr.pointer;
10595 sym->attr.pure = ifc->attr.pure;
10596 sym->attr.elemental = ifc->attr.elemental;
10597 sym->attr.dimension = ifc->attr.dimension;
10598 sym->attr.recursive = ifc->attr.recursive;
10599 sym->attr.always_explicit = ifc->attr.always_explicit;
10600 sym->attr.ext_attr |= ifc->attr.ext_attr;
10601 /* Copy array spec. */
10602 sym->as = gfc_copy_array_spec (ifc->as);
10606 for (i = 0; i < sym->as->rank; i++)
10608 gfc_expr_replace_symbols (sym->as->lower[i], sym);
10609 gfc_expr_replace_symbols (sym->as->upper[i], sym);
10612 /* Copy char length. */
10613 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
10615 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
10616 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
10619 else if (sym->ts.interface->name[0] != '\0')
10621 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
10622 sym->ts.interface->name, sym->name, &sym->declared_at);
10627 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
10630 /* Symbols that are module procedures with results (functions) have
10631 the types and array specification copied for type checking in
10632 procedures that call them, as well as for saving to a module
10633 file. These symbols can't stand the scrutiny that their results
10635 mp_flag = (sym->result != NULL && sym->result != sym);
10638 /* Make sure that the intrinsic is consistent with its internal
10639 representation. This needs to be done before assigning a default
10640 type to avoid spurious warnings. */
10641 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
10642 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
10645 /* Assign default type to symbols that need one and don't have one. */
10646 if (sym->ts.type == BT_UNKNOWN)
10648 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
10649 gfc_set_default_type (sym, 1, NULL);
10651 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
10652 && !sym->attr.function && !sym->attr.subroutine
10653 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
10654 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
10656 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
10658 /* The specific case of an external procedure should emit an error
10659 in the case that there is no implicit type. */
10661 gfc_set_default_type (sym, sym->attr.external, NULL);
10664 /* Result may be in another namespace. */
10665 resolve_symbol (sym->result);
10667 if (!sym->result->attr.proc_pointer)
10669 sym->ts = sym->result->ts;
10670 sym->as = gfc_copy_array_spec (sym->result->as);
10671 sym->attr.dimension = sym->result->attr.dimension;
10672 sym->attr.pointer = sym->result->attr.pointer;
10673 sym->attr.allocatable = sym->result->attr.allocatable;
10679 /* Assumed size arrays and assumed shape arrays must be dummy
10682 if (sym->as != NULL
10683 && (sym->as->type == AS_ASSUMED_SIZE
10684 || sym->as->type == AS_ASSUMED_SHAPE)
10685 && sym->attr.dummy == 0)
10687 if (sym->as->type == AS_ASSUMED_SIZE)
10688 gfc_error ("Assumed size array at %L must be a dummy argument",
10689 &sym->declared_at);
10691 gfc_error ("Assumed shape array at %L must be a dummy argument",
10692 &sym->declared_at);
10696 /* Make sure symbols with known intent or optional are really dummy
10697 variable. Because of ENTRY statement, this has to be deferred
10698 until resolution time. */
10700 if (!sym->attr.dummy
10701 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
10703 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
10707 if (sym->attr.value && !sym->attr.dummy)
10709 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
10710 "it is not a dummy argument", sym->name, &sym->declared_at);
10714 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
10716 gfc_charlen *cl = sym->ts.u.cl;
10717 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10719 gfc_error ("Character dummy variable '%s' at %L with VALUE "
10720 "attribute must have constant length",
10721 sym->name, &sym->declared_at);
10725 if (sym->ts.is_c_interop
10726 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
10728 gfc_error ("C interoperable character dummy variable '%s' at %L "
10729 "with VALUE attribute must have length one",
10730 sym->name, &sym->declared_at);
10735 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
10736 do this for something that was implicitly typed because that is handled
10737 in gfc_set_default_type. Handle dummy arguments and procedure
10738 definitions separately. Also, anything that is use associated is not
10739 handled here but instead is handled in the module it is declared in.
10740 Finally, derived type definitions are allowed to be BIND(C) since that
10741 only implies that they're interoperable, and they are checked fully for
10742 interoperability when a variable is declared of that type. */
10743 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
10744 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
10745 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
10747 gfc_try t = SUCCESS;
10749 /* First, make sure the variable is declared at the
10750 module-level scope (J3/04-007, Section 15.3). */
10751 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
10752 sym->attr.in_common == 0)
10754 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
10755 "is neither a COMMON block nor declared at the "
10756 "module level scope", sym->name, &(sym->declared_at));
10759 else if (sym->common_head != NULL)
10761 t = verify_com_block_vars_c_interop (sym->common_head);
10765 /* If type() declaration, we need to verify that the components
10766 of the given type are all C interoperable, etc. */
10767 if (sym->ts.type == BT_DERIVED &&
10768 sym->ts.u.derived->attr.is_c_interop != 1)
10770 /* Make sure the user marked the derived type as BIND(C). If
10771 not, call the verify routine. This could print an error
10772 for the derived type more than once if multiple variables
10773 of that type are declared. */
10774 if (sym->ts.u.derived->attr.is_bind_c != 1)
10775 verify_bind_c_derived_type (sym->ts.u.derived);
10779 /* Verify the variable itself as C interoperable if it
10780 is BIND(C). It is not possible for this to succeed if
10781 the verify_bind_c_derived_type failed, so don't have to handle
10782 any error returned by verify_bind_c_derived_type. */
10783 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
10784 sym->common_block);
10789 /* clear the is_bind_c flag to prevent reporting errors more than
10790 once if something failed. */
10791 sym->attr.is_bind_c = 0;
10796 /* If a derived type symbol has reached this point, without its
10797 type being declared, we have an error. Notice that most
10798 conditions that produce undefined derived types have already
10799 been dealt with. However, the likes of:
10800 implicit type(t) (t) ..... call foo (t) will get us here if
10801 the type is not declared in the scope of the implicit
10802 statement. Change the type to BT_UNKNOWN, both because it is so
10803 and to prevent an ICE. */
10804 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
10805 && !sym->ts.u.derived->attr.zero_comp)
10807 gfc_error ("The derived type '%s' at %L is of type '%s', "
10808 "which has not been defined", sym->name,
10809 &sym->declared_at, sym->ts.u.derived->name);
10810 sym->ts.type = BT_UNKNOWN;
10814 /* Make sure that the derived type has been resolved and that the
10815 derived type is visible in the symbol's namespace, if it is a
10816 module function and is not PRIVATE. */
10817 if (sym->ts.type == BT_DERIVED
10818 && sym->ts.u.derived->attr.use_assoc
10819 && sym->ns->proc_name
10820 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10824 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
10827 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
10828 if (!ds && sym->attr.function
10829 && gfc_check_access (sym->attr.access, sym->ns->default_access))
10831 symtree = gfc_new_symtree (&sym->ns->sym_root,
10832 sym->ts.u.derived->name);
10833 symtree->n.sym = sym->ts.u.derived;
10834 sym->ts.u.derived->refs++;
10838 /* Unless the derived-type declaration is use associated, Fortran 95
10839 does not allow public entries of private derived types.
10840 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
10841 161 in 95-006r3. */
10842 if (sym->ts.type == BT_DERIVED
10843 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
10844 && !sym->ts.u.derived->attr.use_assoc
10845 && gfc_check_access (sym->attr.access, sym->ns->default_access)
10846 && !gfc_check_access (sym->ts.u.derived->attr.access,
10847 sym->ts.u.derived->ns->default_access)
10848 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
10849 "of PRIVATE derived type '%s'",
10850 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
10851 : "variable", sym->name, &sym->declared_at,
10852 sym->ts.u.derived->name) == FAILURE)
10855 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
10856 default initialization is defined (5.1.2.4.4). */
10857 if (sym->ts.type == BT_DERIVED
10859 && sym->attr.intent == INTENT_OUT
10861 && sym->as->type == AS_ASSUMED_SIZE)
10863 for (c = sym->ts.u.derived->components; c; c = c->next)
10865 if (c->initializer)
10867 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
10868 "ASSUMED SIZE and so cannot have a default initializer",
10869 sym->name, &sym->declared_at);
10875 switch (sym->attr.flavor)
10878 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
10883 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
10888 if (resolve_fl_namelist (sym) == FAILURE)
10893 if (resolve_fl_parameter (sym) == FAILURE)
10901 /* Resolve array specifier. Check as well some constraints
10902 on COMMON blocks. */
10904 check_constant = sym->attr.in_common && !sym->attr.pointer;
10906 /* Set the formal_arg_flag so that check_conflict will not throw
10907 an error for host associated variables in the specification
10908 expression for an array_valued function. */
10909 if (sym->attr.function && sym->as)
10910 formal_arg_flag = 1;
10912 gfc_resolve_array_spec (sym->as, check_constant);
10914 formal_arg_flag = 0;
10916 /* Resolve formal namespaces. */
10917 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
10918 && !sym->attr.contained && !sym->attr.intrinsic)
10919 gfc_resolve (sym->formal_ns);
10921 /* Make sure the formal namespace is present. */
10922 if (sym->formal && !sym->formal_ns)
10924 gfc_formal_arglist *formal = sym->formal;
10925 while (formal && !formal->sym)
10926 formal = formal->next;
10930 sym->formal_ns = formal->sym->ns;
10931 sym->formal_ns->refs++;
10935 /* Check threadprivate restrictions. */
10936 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
10937 && (!sym->attr.in_common
10938 && sym->module == NULL
10939 && (sym->ns->proc_name == NULL
10940 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
10941 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
10943 /* If we have come this far we can apply default-initializers, as
10944 described in 14.7.5, to those variables that have not already
10945 been assigned one. */
10946 if (sym->ts.type == BT_DERIVED
10947 && sym->attr.referenced
10948 && sym->ns == gfc_current_ns
10950 && !sym->attr.allocatable
10951 && !sym->attr.alloc_comp)
10953 symbol_attribute *a = &sym->attr;
10955 if ((!a->save && !a->dummy && !a->pointer
10956 && !a->in_common && !a->use_assoc
10957 && !(a->function && sym != sym->result))
10958 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
10959 apply_default_init (sym);
10962 /* If this symbol has a type-spec, check it. */
10963 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
10964 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
10965 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
10971 /************* Resolve DATA statements *************/
10975 gfc_data_value *vnode;
10981 /* Advance the values structure to point to the next value in the data list. */
10984 next_data_value (void)
10986 while (mpz_cmp_ui (values.left, 0) == 0)
10988 if (!gfc_is_constant_expr (values.vnode->expr))
10989 gfc_error ("non-constant DATA value at %L",
10990 &values.vnode->expr->where);
10992 if (values.vnode->next == NULL)
10995 values.vnode = values.vnode->next;
10996 mpz_set (values.left, values.vnode->repeat);
11004 check_data_variable (gfc_data_variable *var, locus *where)
11010 ar_type mark = AR_UNKNOWN;
11012 mpz_t section_index[GFC_MAX_DIMENSIONS];
11018 if (gfc_resolve_expr (var->expr) == FAILURE)
11022 mpz_init_set_si (offset, 0);
11025 if (e->expr_type != EXPR_VARIABLE)
11026 gfc_internal_error ("check_data_variable(): Bad expression");
11028 sym = e->symtree->n.sym;
11030 if (sym->ns->is_block_data && !sym->attr.in_common)
11032 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
11033 sym->name, &sym->declared_at);
11036 if (e->ref == NULL && sym->as)
11038 gfc_error ("DATA array '%s' at %L must be specified in a previous"
11039 " declaration", sym->name, where);
11043 has_pointer = sym->attr.pointer;
11045 for (ref = e->ref; ref; ref = ref->next)
11047 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
11051 && ref->type == REF_ARRAY
11052 && ref->u.ar.type != AR_FULL)
11054 gfc_error ("DATA element '%s' at %L is a pointer and so must "
11055 "be a full array", sym->name, where);
11060 if (e->rank == 0 || has_pointer)
11062 mpz_init_set_ui (size, 1);
11069 /* Find the array section reference. */
11070 for (ref = e->ref; ref; ref = ref->next)
11072 if (ref->type != REF_ARRAY)
11074 if (ref->u.ar.type == AR_ELEMENT)
11080 /* Set marks according to the reference pattern. */
11081 switch (ref->u.ar.type)
11089 /* Get the start position of array section. */
11090 gfc_get_section_index (ar, section_index, &offset);
11095 gcc_unreachable ();
11098 if (gfc_array_size (e, &size) == FAILURE)
11100 gfc_error ("Nonconstant array section at %L in DATA statement",
11102 mpz_clear (offset);
11109 while (mpz_cmp_ui (size, 0) > 0)
11111 if (next_data_value () == FAILURE)
11113 gfc_error ("DATA statement at %L has more variables than values",
11119 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
11123 /* If we have more than one element left in the repeat count,
11124 and we have more than one element left in the target variable,
11125 then create a range assignment. */
11126 /* FIXME: Only done for full arrays for now, since array sections
11128 if (mark == AR_FULL && ref && ref->next == NULL
11129 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
11133 if (mpz_cmp (size, values.left) >= 0)
11135 mpz_init_set (range, values.left);
11136 mpz_sub (size, size, values.left);
11137 mpz_set_ui (values.left, 0);
11141 mpz_init_set (range, size);
11142 mpz_sub (values.left, values.left, size);
11143 mpz_set_ui (size, 0);
11146 gfc_assign_data_value_range (var->expr, values.vnode->expr,
11149 mpz_add (offset, offset, range);
11153 /* Assign initial value to symbol. */
11156 mpz_sub_ui (values.left, values.left, 1);
11157 mpz_sub_ui (size, size, 1);
11159 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
11163 if (mark == AR_FULL)
11164 mpz_add_ui (offset, offset, 1);
11166 /* Modify the array section indexes and recalculate the offset
11167 for next element. */
11168 else if (mark == AR_SECTION)
11169 gfc_advance_section (section_index, ar, &offset);
11173 if (mark == AR_SECTION)
11175 for (i = 0; i < ar->dimen; i++)
11176 mpz_clear (section_index[i]);
11180 mpz_clear (offset);
11186 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
11188 /* Iterate over a list of elements in a DATA statement. */
11191 traverse_data_list (gfc_data_variable *var, locus *where)
11194 iterator_stack frame;
11195 gfc_expr *e, *start, *end, *step;
11196 gfc_try retval = SUCCESS;
11198 mpz_init (frame.value);
11200 start = gfc_copy_expr (var->iter.start);
11201 end = gfc_copy_expr (var->iter.end);
11202 step = gfc_copy_expr (var->iter.step);
11204 if (gfc_simplify_expr (start, 1) == FAILURE
11205 || start->expr_type != EXPR_CONSTANT)
11207 gfc_error ("iterator start at %L does not simplify", &start->where);
11211 if (gfc_simplify_expr (end, 1) == FAILURE
11212 || end->expr_type != EXPR_CONSTANT)
11214 gfc_error ("iterator end at %L does not simplify", &end->where);
11218 if (gfc_simplify_expr (step, 1) == FAILURE
11219 || step->expr_type != EXPR_CONSTANT)
11221 gfc_error ("iterator step at %L does not simplify", &step->where);
11226 mpz_init_set (trip, end->value.integer);
11227 mpz_sub (trip, trip, start->value.integer);
11228 mpz_add (trip, trip, step->value.integer);
11230 mpz_div (trip, trip, step->value.integer);
11232 mpz_set (frame.value, start->value.integer);
11234 frame.prev = iter_stack;
11235 frame.variable = var->iter.var->symtree;
11236 iter_stack = &frame;
11238 while (mpz_cmp_ui (trip, 0) > 0)
11240 if (traverse_data_var (var->list, where) == FAILURE)
11247 e = gfc_copy_expr (var->expr);
11248 if (gfc_simplify_expr (e, 1) == FAILURE)
11256 mpz_add (frame.value, frame.value, step->value.integer);
11258 mpz_sub_ui (trip, trip, 1);
11263 mpz_clear (frame.value);
11265 gfc_free_expr (start);
11266 gfc_free_expr (end);
11267 gfc_free_expr (step);
11269 iter_stack = frame.prev;
11274 /* Type resolve variables in the variable list of a DATA statement. */
11277 traverse_data_var (gfc_data_variable *var, locus *where)
11281 for (; var; var = var->next)
11283 if (var->expr == NULL)
11284 t = traverse_data_list (var, where);
11286 t = check_data_variable (var, where);
11296 /* Resolve the expressions and iterators associated with a data statement.
11297 This is separate from the assignment checking because data lists should
11298 only be resolved once. */
11301 resolve_data_variables (gfc_data_variable *d)
11303 for (; d; d = d->next)
11305 if (d->list == NULL)
11307 if (gfc_resolve_expr (d->expr) == FAILURE)
11312 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
11315 if (resolve_data_variables (d->list) == FAILURE)
11324 /* Resolve a single DATA statement. We implement this by storing a pointer to
11325 the value list into static variables, and then recursively traversing the
11326 variables list, expanding iterators and such. */
11329 resolve_data (gfc_data *d)
11332 if (resolve_data_variables (d->var) == FAILURE)
11335 values.vnode = d->value;
11336 if (d->value == NULL)
11337 mpz_set_ui (values.left, 0);
11339 mpz_set (values.left, d->value->repeat);
11341 if (traverse_data_var (d->var, &d->where) == FAILURE)
11344 /* At this point, we better not have any values left. */
11346 if (next_data_value () == SUCCESS)
11347 gfc_error ("DATA statement at %L has more values than variables",
11352 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
11353 accessed by host or use association, is a dummy argument to a pure function,
11354 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
11355 is storage associated with any such variable, shall not be used in the
11356 following contexts: (clients of this function). */
11358 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
11359 procedure. Returns zero if assignment is OK, nonzero if there is a
11362 gfc_impure_variable (gfc_symbol *sym)
11366 if (sym->attr.use_assoc || sym->attr.in_common)
11369 if (sym->ns != gfc_current_ns)
11370 return !sym->attr.function;
11372 proc = sym->ns->proc_name;
11373 if (sym->attr.dummy && gfc_pure (proc)
11374 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
11376 proc->attr.function))
11379 /* TODO: Sort out what can be storage associated, if anything, and include
11380 it here. In principle equivalences should be scanned but it does not
11381 seem to be possible to storage associate an impure variable this way. */
11386 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
11387 symbol of the current procedure. */
11390 gfc_pure (gfc_symbol *sym)
11392 symbol_attribute attr;
11395 sym = gfc_current_ns->proc_name;
11401 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
11405 /* Test whether the current procedure is elemental or not. */
11408 gfc_elemental (gfc_symbol *sym)
11410 symbol_attribute attr;
11413 sym = gfc_current_ns->proc_name;
11418 return attr.flavor == FL_PROCEDURE && attr.elemental;
11422 /* Warn about unused labels. */
11425 warn_unused_fortran_label (gfc_st_label *label)
11430 warn_unused_fortran_label (label->left);
11432 if (label->defined == ST_LABEL_UNKNOWN)
11435 switch (label->referenced)
11437 case ST_LABEL_UNKNOWN:
11438 gfc_warning ("Label %d at %L defined but not used", label->value,
11442 case ST_LABEL_BAD_TARGET:
11443 gfc_warning ("Label %d at %L defined but cannot be used",
11444 label->value, &label->where);
11451 warn_unused_fortran_label (label->right);
11455 /* Returns the sequence type of a symbol or sequence. */
11458 sequence_type (gfc_typespec ts)
11467 if (ts.u.derived->components == NULL)
11468 return SEQ_NONDEFAULT;
11470 result = sequence_type (ts.u.derived->components->ts);
11471 for (c = ts.u.derived->components->next; c; c = c->next)
11472 if (sequence_type (c->ts) != result)
11478 if (ts.kind != gfc_default_character_kind)
11479 return SEQ_NONDEFAULT;
11481 return SEQ_CHARACTER;
11484 if (ts.kind != gfc_default_integer_kind)
11485 return SEQ_NONDEFAULT;
11487 return SEQ_NUMERIC;
11490 if (!(ts.kind == gfc_default_real_kind
11491 || ts.kind == gfc_default_double_kind))
11492 return SEQ_NONDEFAULT;
11494 return SEQ_NUMERIC;
11497 if (ts.kind != gfc_default_complex_kind)
11498 return SEQ_NONDEFAULT;
11500 return SEQ_NUMERIC;
11503 if (ts.kind != gfc_default_logical_kind)
11504 return SEQ_NONDEFAULT;
11506 return SEQ_NUMERIC;
11509 return SEQ_NONDEFAULT;
11514 /* Resolve derived type EQUIVALENCE object. */
11517 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
11519 gfc_component *c = derived->components;
11524 /* Shall not be an object of nonsequence derived type. */
11525 if (!derived->attr.sequence)
11527 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
11528 "attribute to be an EQUIVALENCE object", sym->name,
11533 /* Shall not have allocatable components. */
11534 if (derived->attr.alloc_comp)
11536 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
11537 "components to be an EQUIVALENCE object",sym->name,
11542 if (sym->attr.in_common && has_default_initializer (sym->ts.u.derived))
11544 gfc_error ("Derived type variable '%s' at %L with default "
11545 "initialization cannot be in EQUIVALENCE with a variable "
11546 "in COMMON", sym->name, &e->where);
11550 for (; c ; c = c->next)
11552 if (c->ts.type == BT_DERIVED
11553 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
11556 /* Shall not be an object of sequence derived type containing a pointer
11557 in the structure. */
11558 if (c->attr.pointer)
11560 gfc_error ("Derived type variable '%s' at %L with pointer "
11561 "component(s) cannot be an EQUIVALENCE object",
11562 sym->name, &e->where);
11570 /* Resolve equivalence object.
11571 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
11572 an allocatable array, an object of nonsequence derived type, an object of
11573 sequence derived type containing a pointer at any level of component
11574 selection, an automatic object, a function name, an entry name, a result
11575 name, a named constant, a structure component, or a subobject of any of
11576 the preceding objects. A substring shall not have length zero. A
11577 derived type shall not have components with default initialization nor
11578 shall two objects of an equivalence group be initialized.
11579 Either all or none of the objects shall have an protected attribute.
11580 The simple constraints are done in symbol.c(check_conflict) and the rest
11581 are implemented here. */
11584 resolve_equivalence (gfc_equiv *eq)
11587 gfc_symbol *first_sym;
11590 locus *last_where = NULL;
11591 seq_type eq_type, last_eq_type;
11592 gfc_typespec *last_ts;
11593 int object, cnt_protected;
11594 const char *value_name;
11598 last_ts = &eq->expr->symtree->n.sym->ts;
11600 first_sym = eq->expr->symtree->n.sym;
11604 for (object = 1; eq; eq = eq->eq, object++)
11608 e->ts = e->symtree->n.sym->ts;
11609 /* match_varspec might not know yet if it is seeing
11610 array reference or substring reference, as it doesn't
11612 if (e->ref && e->ref->type == REF_ARRAY)
11614 gfc_ref *ref = e->ref;
11615 sym = e->symtree->n.sym;
11617 if (sym->attr.dimension)
11619 ref->u.ar.as = sym->as;
11623 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
11624 if (e->ts.type == BT_CHARACTER
11626 && ref->type == REF_ARRAY
11627 && ref->u.ar.dimen == 1
11628 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
11629 && ref->u.ar.stride[0] == NULL)
11631 gfc_expr *start = ref->u.ar.start[0];
11632 gfc_expr *end = ref->u.ar.end[0];
11635 /* Optimize away the (:) reference. */
11636 if (start == NULL && end == NULL)
11639 e->ref = ref->next;
11641 e->ref->next = ref->next;
11646 ref->type = REF_SUBSTRING;
11648 start = gfc_int_expr (1);
11649 ref->u.ss.start = start;
11650 if (end == NULL && e->ts.u.cl)
11651 end = gfc_copy_expr (e->ts.u.cl->length);
11652 ref->u.ss.end = end;
11653 ref->u.ss.length = e->ts.u.cl;
11660 /* Any further ref is an error. */
11663 gcc_assert (ref->type == REF_ARRAY);
11664 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
11670 if (gfc_resolve_expr (e) == FAILURE)
11673 sym = e->symtree->n.sym;
11675 if (sym->attr.is_protected)
11677 if (cnt_protected > 0 && cnt_protected != object)
11679 gfc_error ("Either all or none of the objects in the "
11680 "EQUIVALENCE set at %L shall have the "
11681 "PROTECTED attribute",
11686 /* Shall not equivalence common block variables in a PURE procedure. */
11687 if (sym->ns->proc_name
11688 && sym->ns->proc_name->attr.pure
11689 && sym->attr.in_common)
11691 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
11692 "object in the pure procedure '%s'",
11693 sym->name, &e->where, sym->ns->proc_name->name);
11697 /* Shall not be a named constant. */
11698 if (e->expr_type == EXPR_CONSTANT)
11700 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
11701 "object", sym->name, &e->where);
11705 if (e->ts.type == BT_DERIVED
11706 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
11709 /* Check that the types correspond correctly:
11711 A numeric sequence structure may be equivalenced to another sequence
11712 structure, an object of default integer type, default real type, double
11713 precision real type, default logical type such that components of the
11714 structure ultimately only become associated to objects of the same
11715 kind. A character sequence structure may be equivalenced to an object
11716 of default character kind or another character sequence structure.
11717 Other objects may be equivalenced only to objects of the same type and
11718 kind parameters. */
11720 /* Identical types are unconditionally OK. */
11721 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
11722 goto identical_types;
11724 last_eq_type = sequence_type (*last_ts);
11725 eq_type = sequence_type (sym->ts);
11727 /* Since the pair of objects is not of the same type, mixed or
11728 non-default sequences can be rejected. */
11730 msg = "Sequence %s with mixed components in EQUIVALENCE "
11731 "statement at %L with different type objects";
11733 && last_eq_type == SEQ_MIXED
11734 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
11736 || (eq_type == SEQ_MIXED
11737 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11738 &e->where) == FAILURE))
11741 msg = "Non-default type object or sequence %s in EQUIVALENCE "
11742 "statement at %L with objects of different type";
11744 && last_eq_type == SEQ_NONDEFAULT
11745 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
11746 last_where) == FAILURE)
11747 || (eq_type == SEQ_NONDEFAULT
11748 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11749 &e->where) == FAILURE))
11752 msg ="Non-CHARACTER object '%s' in default CHARACTER "
11753 "EQUIVALENCE statement at %L";
11754 if (last_eq_type == SEQ_CHARACTER
11755 && eq_type != SEQ_CHARACTER
11756 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11757 &e->where) == FAILURE)
11760 msg ="Non-NUMERIC object '%s' in default NUMERIC "
11761 "EQUIVALENCE statement at %L";
11762 if (last_eq_type == SEQ_NUMERIC
11763 && eq_type != SEQ_NUMERIC
11764 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11765 &e->where) == FAILURE)
11770 last_where = &e->where;
11775 /* Shall not be an automatic array. */
11776 if (e->ref->type == REF_ARRAY
11777 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
11779 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
11780 "an EQUIVALENCE object", sym->name, &e->where);
11787 /* Shall not be a structure component. */
11788 if (r->type == REF_COMPONENT)
11790 gfc_error ("Structure component '%s' at %L cannot be an "
11791 "EQUIVALENCE object",
11792 r->u.c.component->name, &e->where);
11796 /* A substring shall not have length zero. */
11797 if (r->type == REF_SUBSTRING)
11799 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
11801 gfc_error ("Substring at %L has length zero",
11802 &r->u.ss.start->where);
11812 /* Resolve function and ENTRY types, issue diagnostics if needed. */
11815 resolve_fntype (gfc_namespace *ns)
11817 gfc_entry_list *el;
11820 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
11823 /* If there are any entries, ns->proc_name is the entry master
11824 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
11826 sym = ns->entries->sym;
11828 sym = ns->proc_name;
11829 if (sym->result == sym
11830 && sym->ts.type == BT_UNKNOWN
11831 && gfc_set_default_type (sym, 0, NULL) == FAILURE
11832 && !sym->attr.untyped)
11834 gfc_error ("Function '%s' at %L has no IMPLICIT type",
11835 sym->name, &sym->declared_at);
11836 sym->attr.untyped = 1;
11839 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
11840 && !sym->attr.contained
11841 && !gfc_check_access (sym->ts.u.derived->attr.access,
11842 sym->ts.u.derived->ns->default_access)
11843 && gfc_check_access (sym->attr.access, sym->ns->default_access))
11845 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
11846 "%L of PRIVATE type '%s'", sym->name,
11847 &sym->declared_at, sym->ts.u.derived->name);
11851 for (el = ns->entries->next; el; el = el->next)
11853 if (el->sym->result == el->sym
11854 && el->sym->ts.type == BT_UNKNOWN
11855 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
11856 && !el->sym->attr.untyped)
11858 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
11859 el->sym->name, &el->sym->declared_at);
11860 el->sym->attr.untyped = 1;
11866 /* 12.3.2.1.1 Defined operators. */
11869 check_uop_procedure (gfc_symbol *sym, locus where)
11871 gfc_formal_arglist *formal;
11873 if (!sym->attr.function)
11875 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
11876 sym->name, &where);
11880 if (sym->ts.type == BT_CHARACTER
11881 && !(sym->ts.u.cl && sym->ts.u.cl->length)
11882 && !(sym->result && sym->result->ts.u.cl
11883 && sym->result->ts.u.cl->length))
11885 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
11886 "character length", sym->name, &where);
11890 formal = sym->formal;
11891 if (!formal || !formal->sym)
11893 gfc_error ("User operator procedure '%s' at %L must have at least "
11894 "one argument", sym->name, &where);
11898 if (formal->sym->attr.intent != INTENT_IN)
11900 gfc_error ("First argument of operator interface at %L must be "
11901 "INTENT(IN)", &where);
11905 if (formal->sym->attr.optional)
11907 gfc_error ("First argument of operator interface at %L cannot be "
11908 "optional", &where);
11912 formal = formal->next;
11913 if (!formal || !formal->sym)
11916 if (formal->sym->attr.intent != INTENT_IN)
11918 gfc_error ("Second argument of operator interface at %L must be "
11919 "INTENT(IN)", &where);
11923 if (formal->sym->attr.optional)
11925 gfc_error ("Second argument of operator interface at %L cannot be "
11926 "optional", &where);
11932 gfc_error ("Operator interface at %L must have, at most, two "
11933 "arguments", &where);
11941 gfc_resolve_uops (gfc_symtree *symtree)
11943 gfc_interface *itr;
11945 if (symtree == NULL)
11948 gfc_resolve_uops (symtree->left);
11949 gfc_resolve_uops (symtree->right);
11951 for (itr = symtree->n.uop->op; itr; itr = itr->next)
11952 check_uop_procedure (itr->sym, itr->sym->declared_at);
11956 /* Examine all of the expressions associated with a program unit,
11957 assign types to all intermediate expressions, make sure that all
11958 assignments are to compatible types and figure out which names
11959 refer to which functions or subroutines. It doesn't check code
11960 block, which is handled by resolve_code. */
11963 resolve_types (gfc_namespace *ns)
11969 gfc_namespace* old_ns = gfc_current_ns;
11971 /* Check that all IMPLICIT types are ok. */
11972 if (!ns->seen_implicit_none)
11975 for (letter = 0; letter != GFC_LETTERS; ++letter)
11976 if (ns->set_flag[letter]
11977 && resolve_typespec_used (&ns->default_type[letter],
11978 &ns->implicit_loc[letter],
11983 gfc_current_ns = ns;
11985 resolve_entries (ns);
11987 resolve_common_vars (ns->blank_common.head, false);
11988 resolve_common_blocks (ns->common_root);
11990 resolve_contained_functions (ns);
11992 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
11994 for (cl = ns->cl_list; cl; cl = cl->next)
11995 resolve_charlen (cl);
11997 gfc_traverse_ns (ns, resolve_symbol);
11999 resolve_fntype (ns);
12001 for (n = ns->contained; n; n = n->sibling)
12003 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
12004 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
12005 "also be PURE", n->proc_name->name,
12006 &n->proc_name->declared_at);
12012 gfc_check_interfaces (ns);
12014 gfc_traverse_ns (ns, resolve_values);
12020 for (d = ns->data; d; d = d->next)
12024 gfc_traverse_ns (ns, gfc_formalize_init_value);
12026 gfc_traverse_ns (ns, gfc_verify_binding_labels);
12028 if (ns->common_root != NULL)
12029 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
12031 for (eq = ns->equiv; eq; eq = eq->next)
12032 resolve_equivalence (eq);
12034 /* Warn about unused labels. */
12035 if (warn_unused_label)
12036 warn_unused_fortran_label (ns->st_labels);
12038 gfc_resolve_uops (ns->uop_root);
12040 gfc_current_ns = old_ns;
12044 /* Call resolve_code recursively. */
12047 resolve_codes (gfc_namespace *ns)
12050 bitmap_obstack old_obstack;
12052 for (n = ns->contained; n; n = n->sibling)
12055 gfc_current_ns = ns;
12057 /* Set to an out of range value. */
12058 current_entry_id = -1;
12060 old_obstack = labels_obstack;
12061 bitmap_obstack_initialize (&labels_obstack);
12063 resolve_code (ns->code, ns);
12065 bitmap_obstack_release (&labels_obstack);
12066 labels_obstack = old_obstack;
12070 /* This function is called after a complete program unit has been compiled.
12071 Its purpose is to examine all of the expressions associated with a program
12072 unit, assign types to all intermediate expressions, make sure that all
12073 assignments are to compatible types and figure out which names refer to
12074 which functions or subroutines. */
12077 gfc_resolve (gfc_namespace *ns)
12079 gfc_namespace *old_ns;
12080 code_stack *old_cs_base;
12086 old_ns = gfc_current_ns;
12087 old_cs_base = cs_base;
12089 resolve_types (ns);
12090 resolve_codes (ns);
12092 gfc_current_ns = old_ns;
12093 cs_base = old_cs_base;