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;
87 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
88 an ABSTRACT derived-type. If where is not NULL, an error message with that
89 locus is printed, optionally using name. */
92 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
94 if (ts->type == BT_DERIVED && ts->derived->attr.abstract)
99 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
100 name, where, ts->derived->name);
102 gfc_error ("ABSTRACT type '%s' used at %L",
103 ts->derived->name, where);
113 /* Resolve types of formal argument lists. These have to be done early so that
114 the formal argument lists of module procedures can be copied to the
115 containing module before the individual procedures are resolved
116 individually. We also resolve argument lists of procedures in interface
117 blocks because they are self-contained scoping units.
119 Since a dummy argument cannot be a non-dummy procedure, the only
120 resort left for untyped names are the IMPLICIT types. */
123 resolve_formal_arglist (gfc_symbol *proc)
125 gfc_formal_arglist *f;
129 if (proc->result != NULL)
134 if (gfc_elemental (proc)
135 || sym->attr.pointer || sym->attr.allocatable
136 || (sym->as && sym->as->rank > 0))
138 proc->attr.always_explicit = 1;
139 sym->attr.always_explicit = 1;
144 for (f = proc->formal; f; f = f->next)
150 /* Alternate return placeholder. */
151 if (gfc_elemental (proc))
152 gfc_error ("Alternate return specifier in elemental subroutine "
153 "'%s' at %L is not allowed", proc->name,
155 if (proc->attr.function)
156 gfc_error ("Alternate return specifier in function "
157 "'%s' at %L is not allowed", proc->name,
162 if (sym->attr.if_source != IFSRC_UNKNOWN)
163 resolve_formal_arglist (sym);
165 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
167 if (gfc_pure (proc) && !gfc_pure (sym))
169 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
170 "also be PURE", sym->name, &sym->declared_at);
174 if (gfc_elemental (proc))
176 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
177 "procedure", &sym->declared_at);
181 if (sym->attr.function
182 && sym->ts.type == BT_UNKNOWN
183 && sym->attr.intrinsic)
185 gfc_intrinsic_sym *isym;
186 isym = gfc_find_function (sym->name);
187 if (isym == NULL || !isym->specific)
189 gfc_error ("Unable to find a specific INTRINSIC procedure "
190 "for the reference '%s' at %L", sym->name,
199 if (sym->ts.type == BT_UNKNOWN)
201 if (!sym->attr.function || sym->result == sym)
202 gfc_set_default_type (sym, 1, sym->ns);
205 gfc_resolve_array_spec (sym->as, 0);
207 /* We can't tell if an array with dimension (:) is assumed or deferred
208 shape until we know if it has the pointer or allocatable attributes.
210 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
211 && !(sym->attr.pointer || sym->attr.allocatable))
213 sym->as->type = AS_ASSUMED_SHAPE;
214 for (i = 0; i < sym->as->rank; i++)
215 sym->as->lower[i] = gfc_int_expr (1);
218 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
219 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
220 || sym->attr.optional)
222 proc->attr.always_explicit = 1;
224 proc->result->attr.always_explicit = 1;
227 /* If the flavor is unknown at this point, it has to be a variable.
228 A procedure specification would have already set the type. */
230 if (sym->attr.flavor == FL_UNKNOWN)
231 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
233 if (gfc_pure (proc) && !sym->attr.pointer
234 && sym->attr.flavor != FL_PROCEDURE)
236 if (proc->attr.function && sym->attr.intent != INTENT_IN)
237 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
238 "INTENT(IN)", sym->name, proc->name,
241 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
242 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
243 "have its INTENT specified", sym->name, proc->name,
247 if (gfc_elemental (proc))
251 gfc_error ("Argument '%s' of elemental procedure at %L must "
252 "be scalar", sym->name, &sym->declared_at);
256 if (sym->attr.pointer)
258 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
259 "have the POINTER attribute", sym->name,
264 if (sym->attr.flavor == FL_PROCEDURE)
266 gfc_error ("Dummy procedure '%s' not allowed in elemental "
267 "procedure '%s' at %L", sym->name, proc->name,
273 /* Each dummy shall be specified to be scalar. */
274 if (proc->attr.proc == PROC_ST_FUNCTION)
278 gfc_error ("Argument '%s' of statement function at %L must "
279 "be scalar", sym->name, &sym->declared_at);
283 if (sym->ts.type == BT_CHARACTER)
285 gfc_charlen *cl = sym->ts.cl;
286 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
288 gfc_error ("Character-valued argument '%s' of statement "
289 "function at %L must have constant length",
290 sym->name, &sym->declared_at);
300 /* Work function called when searching for symbols that have argument lists
301 associated with them. */
304 find_arglists (gfc_symbol *sym)
306 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
309 resolve_formal_arglist (sym);
313 /* Given a namespace, resolve all formal argument lists within the namespace.
317 resolve_formal_arglists (gfc_namespace *ns)
322 gfc_traverse_ns (ns, find_arglists);
327 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
331 /* If this namespace is not a function or an entry master function,
333 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
334 || sym->attr.entry_master)
337 /* Try to find out of what the return type is. */
338 if (sym->result->ts.type == BT_UNKNOWN)
340 t = gfc_set_default_type (sym->result, 0, ns);
342 if (t == FAILURE && !sym->result->attr.untyped)
344 if (sym->result == sym)
345 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
346 sym->name, &sym->declared_at);
348 gfc_error ("Result '%s' of contained function '%s' at %L has "
349 "no IMPLICIT type", sym->result->name, sym->name,
350 &sym->result->declared_at);
351 sym->result->attr.untyped = 1;
355 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
356 type, lists the only ways a character length value of * can be used:
357 dummy arguments of procedures, named constants, and function results
358 in external functions. Internal function results are not on that list;
359 ergo, not permitted. */
361 if (sym->result->ts.type == BT_CHARACTER)
363 gfc_charlen *cl = sym->result->ts.cl;
364 if (!cl || !cl->length)
365 gfc_error ("Character-valued internal function '%s' at %L must "
366 "not be assumed length", sym->name, &sym->declared_at);
371 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
372 introduce duplicates. */
375 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
377 gfc_formal_arglist *f, *new_arglist;
380 for (; new_args != NULL; new_args = new_args->next)
382 new_sym = new_args->sym;
383 /* See if this arg is already in the formal argument list. */
384 for (f = proc->formal; f; f = f->next)
386 if (new_sym == f->sym)
393 /* Add a new argument. Argument order is not important. */
394 new_arglist = gfc_get_formal_arglist ();
395 new_arglist->sym = new_sym;
396 new_arglist->next = proc->formal;
397 proc->formal = new_arglist;
402 /* Flag the arguments that are not present in all entries. */
405 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
407 gfc_formal_arglist *f, *head;
410 for (f = proc->formal; f; f = f->next)
415 for (new_args = head; new_args; new_args = new_args->next)
417 if (new_args->sym == f->sym)
424 f->sym->attr.not_always_present = 1;
429 /* Resolve alternate entry points. If a symbol has multiple entry points we
430 create a new master symbol for the main routine, and turn the existing
431 symbol into an entry point. */
434 resolve_entries (gfc_namespace *ns)
436 gfc_namespace *old_ns;
440 char name[GFC_MAX_SYMBOL_LEN + 1];
441 static int master_count = 0;
443 if (ns->proc_name == NULL)
446 /* No need to do anything if this procedure doesn't have alternate entry
451 /* We may already have resolved alternate entry points. */
452 if (ns->proc_name->attr.entry_master)
455 /* If this isn't a procedure something has gone horribly wrong. */
456 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
458 /* Remember the current namespace. */
459 old_ns = gfc_current_ns;
463 /* Add the main entry point to the list of entry points. */
464 el = gfc_get_entry_list ();
465 el->sym = ns->proc_name;
467 el->next = ns->entries;
469 ns->proc_name->attr.entry = 1;
471 /* If it is a module function, it needs to be in the right namespace
472 so that gfc_get_fake_result_decl can gather up the results. The
473 need for this arose in get_proc_name, where these beasts were
474 left in their own namespace, to keep prior references linked to
475 the entry declaration.*/
476 if (ns->proc_name->attr.function
477 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
480 /* Do the same for entries where the master is not a module
481 procedure. These are retained in the module namespace because
482 of the module procedure declaration. */
483 for (el = el->next; el; el = el->next)
484 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
485 && el->sym->attr.mod_proc)
489 /* Add an entry statement for it. */
496 /* Create a new symbol for the master function. */
497 /* Give the internal function a unique name (within this file).
498 Also include the function name so the user has some hope of figuring
499 out what is going on. */
500 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
501 master_count++, ns->proc_name->name);
502 gfc_get_ha_symbol (name, &proc);
503 gcc_assert (proc != NULL);
505 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
506 if (ns->proc_name->attr.subroutine)
507 gfc_add_subroutine (&proc->attr, proc->name, NULL);
511 gfc_typespec *ts, *fts;
512 gfc_array_spec *as, *fas;
513 gfc_add_function (&proc->attr, proc->name, NULL);
515 fas = ns->entries->sym->as;
516 fas = fas ? fas : ns->entries->sym->result->as;
517 fts = &ns->entries->sym->result->ts;
518 if (fts->type == BT_UNKNOWN)
519 fts = gfc_get_default_type (ns->entries->sym->result, NULL);
520 for (el = ns->entries->next; el; el = el->next)
522 ts = &el->sym->result->ts;
524 as = as ? as : el->sym->result->as;
525 if (ts->type == BT_UNKNOWN)
526 ts = gfc_get_default_type (el->sym->result, NULL);
528 if (! gfc_compare_types (ts, fts)
529 || (el->sym->result->attr.dimension
530 != ns->entries->sym->result->attr.dimension)
531 || (el->sym->result->attr.pointer
532 != ns->entries->sym->result->attr.pointer))
534 else if (as && fas && ns->entries->sym->result != el->sym->result
535 && gfc_compare_array_spec (as, fas) == 0)
536 gfc_error ("Function %s at %L has entries with mismatched "
537 "array specifications", ns->entries->sym->name,
538 &ns->entries->sym->declared_at);
539 /* The characteristics need to match and thus both need to have
540 the same string length, i.e. both len=*, or both len=4.
541 Having both len=<variable> is also possible, but difficult to
542 check at compile time. */
543 else if (ts->type == BT_CHARACTER && ts->cl && fts->cl
544 && (((ts->cl->length && !fts->cl->length)
545 ||(!ts->cl->length && fts->cl->length))
547 && ts->cl->length->expr_type
548 != fts->cl->length->expr_type)
550 && ts->cl->length->expr_type == EXPR_CONSTANT
551 && mpz_cmp (ts->cl->length->value.integer,
552 fts->cl->length->value.integer) != 0)))
553 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
554 "entries returning variables of different "
555 "string lengths", ns->entries->sym->name,
556 &ns->entries->sym->declared_at);
561 sym = ns->entries->sym->result;
562 /* All result types the same. */
564 if (sym->attr.dimension)
565 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
566 if (sym->attr.pointer)
567 gfc_add_pointer (&proc->attr, NULL);
571 /* Otherwise the result will be passed through a union by
573 proc->attr.mixed_entry_master = 1;
574 for (el = ns->entries; el; el = el->next)
576 sym = el->sym->result;
577 if (sym->attr.dimension)
579 if (el == ns->entries)
580 gfc_error ("FUNCTION result %s can't be an array in "
581 "FUNCTION %s at %L", sym->name,
582 ns->entries->sym->name, &sym->declared_at);
584 gfc_error ("ENTRY result %s can't be an array in "
585 "FUNCTION %s at %L", sym->name,
586 ns->entries->sym->name, &sym->declared_at);
588 else if (sym->attr.pointer)
590 if (el == ns->entries)
591 gfc_error ("FUNCTION result %s can't be a POINTER in "
592 "FUNCTION %s at %L", sym->name,
593 ns->entries->sym->name, &sym->declared_at);
595 gfc_error ("ENTRY result %s can't be a POINTER in "
596 "FUNCTION %s at %L", sym->name,
597 ns->entries->sym->name, &sym->declared_at);
602 if (ts->type == BT_UNKNOWN)
603 ts = gfc_get_default_type (sym, NULL);
607 if (ts->kind == gfc_default_integer_kind)
611 if (ts->kind == gfc_default_real_kind
612 || ts->kind == gfc_default_double_kind)
616 if (ts->kind == gfc_default_complex_kind)
620 if (ts->kind == gfc_default_logical_kind)
624 /* We will issue error elsewhere. */
632 if (el == ns->entries)
633 gfc_error ("FUNCTION result %s can't be of type %s "
634 "in FUNCTION %s at %L", sym->name,
635 gfc_typename (ts), ns->entries->sym->name,
638 gfc_error ("ENTRY result %s can't be of type %s "
639 "in FUNCTION %s at %L", sym->name,
640 gfc_typename (ts), ns->entries->sym->name,
647 proc->attr.access = ACCESS_PRIVATE;
648 proc->attr.entry_master = 1;
650 /* Merge all the entry point arguments. */
651 for (el = ns->entries; el; el = el->next)
652 merge_argument_lists (proc, el->sym->formal);
654 /* Check the master formal arguments for any that are not
655 present in all entry points. */
656 for (el = ns->entries; el; el = el->next)
657 check_argument_lists (proc, el->sym->formal);
659 /* Use the master function for the function body. */
660 ns->proc_name = proc;
662 /* Finalize the new symbols. */
663 gfc_commit_symbols ();
665 /* Restore the original namespace. */
666 gfc_current_ns = old_ns;
671 has_default_initializer (gfc_symbol *der)
675 gcc_assert (der->attr.flavor == FL_DERIVED);
676 for (c = der->components; c; c = c->next)
677 if ((c->ts.type != BT_DERIVED && c->initializer)
678 || (c->ts.type == BT_DERIVED
679 && (!c->attr.pointer && has_default_initializer (c->ts.derived))))
685 /* Resolve common variables. */
687 resolve_common_vars (gfc_symbol *sym, bool named_common)
689 gfc_symbol *csym = sym;
691 for (; csym; csym = csym->common_next)
693 if (csym->value || csym->attr.data)
695 if (!csym->ns->is_block_data)
696 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
697 "but only in BLOCK DATA initialization is "
698 "allowed", csym->name, &csym->declared_at);
699 else if (!named_common)
700 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
701 "in a blank COMMON but initialization is only "
702 "allowed in named common blocks", csym->name,
706 if (csym->ts.type != BT_DERIVED)
709 if (!(csym->ts.derived->attr.sequence
710 || csym->ts.derived->attr.is_bind_c))
711 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
712 "has neither the SEQUENCE nor the BIND(C) "
713 "attribute", csym->name, &csym->declared_at);
714 if (csym->ts.derived->attr.alloc_comp)
715 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
716 "has an ultimate component that is "
717 "allocatable", csym->name, &csym->declared_at);
718 if (has_default_initializer (csym->ts.derived))
719 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
720 "may not have default initializer", csym->name,
723 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
724 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
728 /* Resolve common blocks. */
730 resolve_common_blocks (gfc_symtree *common_root)
734 if (common_root == NULL)
737 if (common_root->left)
738 resolve_common_blocks (common_root->left);
739 if (common_root->right)
740 resolve_common_blocks (common_root->right);
742 resolve_common_vars (common_root->n.common->head, true);
744 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
748 if (sym->attr.flavor == FL_PARAMETER)
749 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
750 sym->name, &common_root->n.common->where, &sym->declared_at);
752 if (sym->attr.intrinsic)
753 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
754 sym->name, &common_root->n.common->where);
755 else if (sym->attr.result
756 ||(sym->attr.function && gfc_current_ns->proc_name == sym))
757 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
758 "that is also a function result", sym->name,
759 &common_root->n.common->where);
760 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
761 && sym->attr.proc != PROC_ST_FUNCTION)
762 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
763 "that is also a global procedure", sym->name,
764 &common_root->n.common->where);
768 /* Resolve contained function types. Because contained functions can call one
769 another, they have to be worked out before any of the contained procedures
772 The good news is that if a function doesn't already have a type, the only
773 way it can get one is through an IMPLICIT type or a RESULT variable, because
774 by definition contained functions are contained namespace they're contained
775 in, not in a sibling or parent namespace. */
778 resolve_contained_functions (gfc_namespace *ns)
780 gfc_namespace *child;
783 resolve_formal_arglists (ns);
785 for (child = ns->contained; child; child = child->sibling)
787 /* Resolve alternate entry points first. */
788 resolve_entries (child);
790 /* Then check function return types. */
791 resolve_contained_fntype (child->proc_name, child);
792 for (el = child->entries; el; el = el->next)
793 resolve_contained_fntype (el->sym, child);
798 /* Resolve all of the elements of a structure constructor and make sure that
799 the types are correct. */
802 resolve_structure_cons (gfc_expr *expr)
804 gfc_constructor *cons;
810 cons = expr->value.constructor;
811 /* A constructor may have references if it is the result of substituting a
812 parameter variable. In this case we just pull out the component we
815 comp = expr->ref->u.c.sym->components;
817 comp = expr->ts.derived->components;
819 /* See if the user is trying to invoke a structure constructor for one of
820 the iso_c_binding derived types. */
821 if (expr->ts.derived && expr->ts.derived->ts.is_iso_c && cons
822 && cons->expr != NULL)
824 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
825 expr->ts.derived->name, &(expr->where));
829 for (; comp; comp = comp->next, cons = cons->next)
836 if (gfc_resolve_expr (cons->expr) == FAILURE)
842 rank = comp->as ? comp->as->rank : 0;
843 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
844 && (comp->attr.allocatable || cons->expr->rank))
846 gfc_error ("The rank of the element in the derived type "
847 "constructor at %L does not match that of the "
848 "component (%d/%d)", &cons->expr->where,
849 cons->expr->rank, rank);
853 /* If we don't have the right type, try to convert it. */
855 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
858 if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
859 gfc_error ("The element in the derived type constructor at %L, "
860 "for pointer component '%s', is %s but should be %s",
861 &cons->expr->where, comp->name,
862 gfc_basic_typename (cons->expr->ts.type),
863 gfc_basic_typename (comp->ts.type));
865 t = gfc_convert_type (cons->expr, &comp->ts, 1);
868 if (cons->expr->expr_type == EXPR_NULL
869 && !(comp->attr.pointer || comp->attr.allocatable))
872 gfc_error ("The NULL in the derived type constructor at %L is "
873 "being applied to component '%s', which is neither "
874 "a POINTER nor ALLOCATABLE", &cons->expr->where,
878 if (!comp->attr.pointer || cons->expr->expr_type == EXPR_NULL)
881 a = gfc_expr_attr (cons->expr);
883 if (!a.pointer && !a.target)
886 gfc_error ("The element in the derived type constructor at %L, "
887 "for pointer component '%s' should be a POINTER or "
888 "a TARGET", &cons->expr->where, comp->name);
896 /****************** Expression name resolution ******************/
898 /* Returns 0 if a symbol was not declared with a type or
899 attribute declaration statement, nonzero otherwise. */
902 was_declared (gfc_symbol *sym)
908 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
911 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
912 || a.optional || a.pointer || a.save || a.target || a.volatile_
913 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
920 /* Determine if a symbol is generic or not. */
923 generic_sym (gfc_symbol *sym)
927 if (sym->attr.generic ||
928 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
931 if (was_declared (sym) || sym->ns->parent == NULL)
934 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
941 return generic_sym (s);
948 /* Determine if a symbol is specific or not. */
951 specific_sym (gfc_symbol *sym)
955 if (sym->attr.if_source == IFSRC_IFBODY
956 || sym->attr.proc == PROC_MODULE
957 || sym->attr.proc == PROC_INTERNAL
958 || sym->attr.proc == PROC_ST_FUNCTION
959 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
960 || sym->attr.external)
963 if (was_declared (sym) || sym->ns->parent == NULL)
966 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
968 return (s == NULL) ? 0 : specific_sym (s);
972 /* Figure out if the procedure is specific, generic or unknown. */
975 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
979 procedure_kind (gfc_symbol *sym)
981 if (generic_sym (sym))
982 return PTYPE_GENERIC;
984 if (specific_sym (sym))
985 return PTYPE_SPECIFIC;
987 return PTYPE_UNKNOWN;
990 /* Check references to assumed size arrays. The flag need_full_assumed_size
991 is nonzero when matching actual arguments. */
993 static int need_full_assumed_size = 0;
996 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
998 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1001 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1002 What should it be? */
1003 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1004 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1005 && (e->ref->u.ar.type == AR_FULL))
1007 gfc_error ("The upper bound in the last dimension must "
1008 "appear in the reference to the assumed size "
1009 "array '%s' at %L", sym->name, &e->where);
1016 /* Look for bad assumed size array references in argument expressions
1017 of elemental and array valued intrinsic procedures. Since this is
1018 called from procedure resolution functions, it only recurses at
1022 resolve_assumed_size_actual (gfc_expr *e)
1027 switch (e->expr_type)
1030 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1035 if (resolve_assumed_size_actual (e->value.op.op1)
1036 || resolve_assumed_size_actual (e->value.op.op2))
1047 /* Check a generic procedure, passed as an actual argument, to see if
1048 there is a matching specific name. If none, it is an error, and if
1049 more than one, the reference is ambiguous. */
1051 count_specific_procs (gfc_expr *e)
1058 sym = e->symtree->n.sym;
1060 for (p = sym->generic; p; p = p->next)
1061 if (strcmp (sym->name, p->sym->name) == 0)
1063 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1069 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1073 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1074 "argument at %L", sym->name, &e->where);
1080 /* See if a call to sym could possibly be a not allowed RECURSION because of
1081 a missing RECURIVE declaration. This means that either sym is the current
1082 context itself, or sym is the parent of a contained procedure calling its
1083 non-RECURSIVE containing procedure.
1084 This also works if sym is an ENTRY. */
1087 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1089 gfc_symbol* proc_sym;
1090 gfc_symbol* context_proc;
1092 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1094 /* If we've got an ENTRY, find real procedure. */
1095 if (sym->attr.entry && sym->ns->entries)
1096 proc_sym = sym->ns->entries->sym;
1100 /* If sym is RECURSIVE, all is well of course. */
1101 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1104 /* Find the context procdure's "real" symbol if it has entries. */
1105 context_proc = (context->entries ? context->entries->sym
1106 : context->proc_name);
1110 /* A call from sym's body to itself is recursion, of course. */
1111 if (context_proc == proc_sym)
1114 /* The same is true if context is a contained procedure and sym the
1116 if (context_proc->attr.contained)
1118 gfc_symbol* parent_proc;
1120 gcc_assert (context->parent);
1121 parent_proc = (context->parent->entries ? context->parent->entries->sym
1122 : context->parent->proc_name);
1124 if (parent_proc == proc_sym)
1132 /* Resolve a procedure expression, like passing it to a called procedure or as
1133 RHS for a procedure pointer assignment. */
1136 resolve_procedure_expression (gfc_expr* expr)
1140 if (expr->expr_type != EXPR_VARIABLE)
1142 gcc_assert (expr->symtree);
1144 sym = expr->symtree->n.sym;
1145 if (sym->attr.flavor != FL_PROCEDURE
1146 || (sym->attr.function && sym->result == sym))
1149 /* A non-RECURSIVE procedure that is used as procedure expression within its
1150 own body is in danger of being called recursively. */
1151 if (is_illegal_recursion (sym, gfc_current_ns))
1152 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1153 " itself recursively. Declare it RECURSIVE or use"
1154 " -frecursive", sym->name, &expr->where);
1160 /* Resolve an actual argument list. Most of the time, this is just
1161 resolving the expressions in the list.
1162 The exception is that we sometimes have to decide whether arguments
1163 that look like procedure arguments are really simple variable
1167 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1168 bool no_formal_args)
1171 gfc_symtree *parent_st;
1173 int save_need_full_assumed_size;
1175 for (; arg; arg = arg->next)
1180 /* Check the label is a valid branching target. */
1183 if (arg->label->defined == ST_LABEL_UNKNOWN)
1185 gfc_error ("Label %d referenced at %L is never defined",
1186 arg->label->value, &arg->label->where);
1193 if (e->expr_type == EXPR_VARIABLE
1194 && e->symtree->n.sym->attr.generic
1196 && count_specific_procs (e) != 1)
1199 if (e->ts.type != BT_PROCEDURE)
1201 save_need_full_assumed_size = need_full_assumed_size;
1202 if (e->expr_type != EXPR_VARIABLE)
1203 need_full_assumed_size = 0;
1204 if (gfc_resolve_expr (e) != SUCCESS)
1206 need_full_assumed_size = save_need_full_assumed_size;
1210 /* See if the expression node should really be a variable reference. */
1212 sym = e->symtree->n.sym;
1214 if (sym->attr.flavor == FL_PROCEDURE
1215 || sym->attr.intrinsic
1216 || sym->attr.external)
1220 /* If a procedure is not already determined to be something else
1221 check if it is intrinsic. */
1222 if (!sym->attr.intrinsic
1223 && !(sym->attr.external || sym->attr.use_assoc
1224 || sym->attr.if_source == IFSRC_IFBODY)
1225 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1226 sym->attr.intrinsic = 1;
1228 if (sym->attr.proc == PROC_ST_FUNCTION)
1230 gfc_error ("Statement function '%s' at %L is not allowed as an "
1231 "actual argument", sym->name, &e->where);
1234 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1235 sym->attr.subroutine);
1236 if (sym->attr.intrinsic && actual_ok == 0)
1238 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1239 "actual argument", sym->name, &e->where);
1242 if (sym->attr.contained && !sym->attr.use_assoc
1243 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1245 gfc_error ("Internal procedure '%s' is not allowed as an "
1246 "actual argument at %L", sym->name, &e->where);
1249 if (sym->attr.elemental && !sym->attr.intrinsic)
1251 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1252 "allowed as an actual argument at %L", sym->name,
1256 /* Check if a generic interface has a specific procedure
1257 with the same name before emitting an error. */
1258 if (sym->attr.generic && count_specific_procs (e) != 1)
1261 /* Just in case a specific was found for the expression. */
1262 sym = e->symtree->n.sym;
1264 /* If the symbol is the function that names the current (or
1265 parent) scope, then we really have a variable reference. */
1267 if (sym->attr.function && sym->result == sym
1268 && (sym->ns->proc_name == sym
1269 || (sym->ns->parent != NULL
1270 && sym->ns->parent->proc_name == sym)))
1273 /* If all else fails, see if we have a specific intrinsic. */
1274 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1276 gfc_intrinsic_sym *isym;
1278 isym = gfc_find_function (sym->name);
1279 if (isym == NULL || !isym->specific)
1281 gfc_error ("Unable to find a specific INTRINSIC procedure "
1282 "for the reference '%s' at %L", sym->name,
1287 sym->attr.intrinsic = 1;
1288 sym->attr.function = 1;
1291 if (gfc_resolve_expr (e) == FAILURE)
1296 /* See if the name is a module procedure in a parent unit. */
1298 if (was_declared (sym) || sym->ns->parent == NULL)
1301 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1303 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1307 if (parent_st == NULL)
1310 sym = parent_st->n.sym;
1311 e->symtree = parent_st; /* Point to the right thing. */
1313 if (sym->attr.flavor == FL_PROCEDURE
1314 || sym->attr.intrinsic
1315 || sym->attr.external)
1317 if (gfc_resolve_expr (e) == FAILURE)
1323 e->expr_type = EXPR_VARIABLE;
1325 if (sym->as != NULL)
1327 e->rank = sym->as->rank;
1328 e->ref = gfc_get_ref ();
1329 e->ref->type = REF_ARRAY;
1330 e->ref->u.ar.type = AR_FULL;
1331 e->ref->u.ar.as = sym->as;
1334 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1335 primary.c (match_actual_arg). If above code determines that it
1336 is a variable instead, it needs to be resolved as it was not
1337 done at the beginning of this function. */
1338 save_need_full_assumed_size = need_full_assumed_size;
1339 if (e->expr_type != EXPR_VARIABLE)
1340 need_full_assumed_size = 0;
1341 if (gfc_resolve_expr (e) != SUCCESS)
1343 need_full_assumed_size = save_need_full_assumed_size;
1346 /* Check argument list functions %VAL, %LOC and %REF. There is
1347 nothing to do for %REF. */
1348 if (arg->name && arg->name[0] == '%')
1350 if (strncmp ("%VAL", arg->name, 4) == 0)
1352 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1354 gfc_error ("By-value argument at %L is not of numeric "
1361 gfc_error ("By-value argument at %L cannot be an array or "
1362 "an array section", &e->where);
1366 /* Intrinsics are still PROC_UNKNOWN here. However,
1367 since same file external procedures are not resolvable
1368 in gfortran, it is a good deal easier to leave them to
1370 if (ptype != PROC_UNKNOWN
1371 && ptype != PROC_DUMMY
1372 && ptype != PROC_EXTERNAL
1373 && ptype != PROC_MODULE)
1375 gfc_error ("By-value argument at %L is not allowed "
1376 "in this context", &e->where);
1381 /* Statement functions have already been excluded above. */
1382 else if (strncmp ("%LOC", arg->name, 4) == 0
1383 && e->ts.type == BT_PROCEDURE)
1385 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1387 gfc_error ("Passing internal procedure at %L by location "
1388 "not allowed", &e->where);
1399 /* Do the checks of the actual argument list that are specific to elemental
1400 procedures. If called with c == NULL, we have a function, otherwise if
1401 expr == NULL, we have a subroutine. */
1404 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1406 gfc_actual_arglist *arg0;
1407 gfc_actual_arglist *arg;
1408 gfc_symbol *esym = NULL;
1409 gfc_intrinsic_sym *isym = NULL;
1411 gfc_intrinsic_arg *iformal = NULL;
1412 gfc_formal_arglist *eformal = NULL;
1413 bool formal_optional = false;
1414 bool set_by_optional = false;
1418 /* Is this an elemental procedure? */
1419 if (expr && expr->value.function.actual != NULL)
1421 if (expr->value.function.esym != NULL
1422 && expr->value.function.esym->attr.elemental)
1424 arg0 = expr->value.function.actual;
1425 esym = expr->value.function.esym;
1427 else if (expr->value.function.isym != NULL
1428 && expr->value.function.isym->elemental)
1430 arg0 = expr->value.function.actual;
1431 isym = expr->value.function.isym;
1436 else if (c && c->ext.actual != NULL)
1438 arg0 = c->ext.actual;
1440 if (c->resolved_sym)
1441 esym = c->resolved_sym;
1443 esym = c->symtree->n.sym;
1446 if (!esym->attr.elemental)
1452 /* The rank of an elemental is the rank of its array argument(s). */
1453 for (arg = arg0; arg; arg = arg->next)
1455 if (arg->expr != NULL && arg->expr->rank > 0)
1457 rank = arg->expr->rank;
1458 if (arg->expr->expr_type == EXPR_VARIABLE
1459 && arg->expr->symtree->n.sym->attr.optional)
1460 set_by_optional = true;
1462 /* Function specific; set the result rank and shape. */
1466 if (!expr->shape && arg->expr->shape)
1468 expr->shape = gfc_get_shape (rank);
1469 for (i = 0; i < rank; i++)
1470 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1477 /* If it is an array, it shall not be supplied as an actual argument
1478 to an elemental procedure unless an array of the same rank is supplied
1479 as an actual argument corresponding to a nonoptional dummy argument of
1480 that elemental procedure(12.4.1.5). */
1481 formal_optional = false;
1483 iformal = isym->formal;
1485 eformal = esym->formal;
1487 for (arg = arg0; arg; arg = arg->next)
1491 if (eformal->sym && eformal->sym->attr.optional)
1492 formal_optional = true;
1493 eformal = eformal->next;
1495 else if (isym && iformal)
1497 if (iformal->optional)
1498 formal_optional = true;
1499 iformal = iformal->next;
1502 formal_optional = true;
1504 if (pedantic && arg->expr != NULL
1505 && arg->expr->expr_type == EXPR_VARIABLE
1506 && arg->expr->symtree->n.sym->attr.optional
1509 && (set_by_optional || arg->expr->rank != rank)
1510 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1512 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1513 "MISSING, it cannot be the actual argument of an "
1514 "ELEMENTAL procedure unless there is a non-optional "
1515 "argument with the same rank (12.4.1.5)",
1516 arg->expr->symtree->n.sym->name, &arg->expr->where);
1521 for (arg = arg0; arg; arg = arg->next)
1523 if (arg->expr == NULL || arg->expr->rank == 0)
1526 /* Being elemental, the last upper bound of an assumed size array
1527 argument must be present. */
1528 if (resolve_assumed_size_actual (arg->expr))
1531 /* Elemental procedure's array actual arguments must conform. */
1534 if (gfc_check_conformance ("elemental procedure", arg->expr, e)
1542 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1543 is an array, the intent inout/out variable needs to be also an array. */
1544 if (rank > 0 && esym && expr == NULL)
1545 for (eformal = esym->formal, arg = arg0; arg && eformal;
1546 arg = arg->next, eformal = eformal->next)
1547 if ((eformal->sym->attr.intent == INTENT_OUT
1548 || eformal->sym->attr.intent == INTENT_INOUT)
1549 && arg->expr && arg->expr->rank == 0)
1551 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1552 "ELEMENTAL subroutine '%s' is a scalar, but another "
1553 "actual argument is an array", &arg->expr->where,
1554 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1555 : "INOUT", eformal->sym->name, esym->name);
1562 /* Go through each actual argument in ACTUAL and see if it can be
1563 implemented as an inlined, non-copying intrinsic. FNSYM is the
1564 function being called, or NULL if not known. */
1567 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1569 gfc_actual_arglist *ap;
1572 for (ap = actual; ap; ap = ap->next)
1574 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1575 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1577 ap->expr->inline_noncopying_intrinsic = 1;
1581 /* This function does the checking of references to global procedures
1582 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1583 77 and 95 standards. It checks for a gsymbol for the name, making
1584 one if it does not already exist. If it already exists, then the
1585 reference being resolved must correspond to the type of gsymbol.
1586 Otherwise, the new symbol is equipped with the attributes of the
1587 reference. The corresponding code that is called in creating
1588 global entities is parse.c.
1590 In addition, for all but -std=legacy, the gsymbols are used to
1591 check the interfaces of external procedures from the same file.
1592 The namespace of the gsymbol is resolved and then, once this is
1593 done the interface is checked. */
1596 resolve_global_procedure (gfc_symbol *sym, locus *where,
1597 gfc_actual_arglist **actual, int sub)
1603 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1605 gsym = gfc_get_gsymbol (sym->name);
1607 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1608 gfc_global_used (gsym, where);
1610 if (gfc_option.flag_whole_file
1611 && gsym->type != GSYM_UNKNOWN
1613 && gsym->ns->proc_name
1614 && gsym->ns->proc_name->formal)
1616 /* Make sure that translation for the gsymbol occurs before
1617 the procedure currently being resolved. */
1618 ns = gsym->ns->resolved ? NULL : gfc_global_ns_list;
1619 for (; ns && ns != gsym->ns; ns = ns->sibling)
1621 if (ns->sibling == gsym->ns)
1623 ns->sibling = gsym->ns->sibling;
1624 gsym->ns->sibling = gfc_global_ns_list;
1625 gfc_global_ns_list = gsym->ns;
1630 if (!gsym->ns->resolved)
1631 gfc_resolve (gsym->ns);
1633 gfc_procedure_use (gsym->ns->proc_name, actual, where);
1636 if (gsym->type == GSYM_UNKNOWN)
1639 gsym->where = *where;
1646 /************* Function resolution *************/
1648 /* Resolve a function call known to be generic.
1649 Section 14.1.2.4.1. */
1652 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1656 if (sym->attr.generic)
1658 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1661 expr->value.function.name = s->name;
1662 expr->value.function.esym = s;
1664 if (s->ts.type != BT_UNKNOWN)
1666 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1667 expr->ts = s->result->ts;
1670 expr->rank = s->as->rank;
1671 else if (s->result != NULL && s->result->as != NULL)
1672 expr->rank = s->result->as->rank;
1674 gfc_set_sym_referenced (expr->value.function.esym);
1679 /* TODO: Need to search for elemental references in generic
1683 if (sym->attr.intrinsic)
1684 return gfc_intrinsic_func_interface (expr, 0);
1691 resolve_generic_f (gfc_expr *expr)
1696 sym = expr->symtree->n.sym;
1700 m = resolve_generic_f0 (expr, sym);
1703 else if (m == MATCH_ERROR)
1707 if (sym->ns->parent == NULL)
1709 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1713 if (!generic_sym (sym))
1717 /* Last ditch attempt. See if the reference is to an intrinsic
1718 that possesses a matching interface. 14.1.2.4 */
1719 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
1721 gfc_error ("There is no specific function for the generic '%s' at %L",
1722 expr->symtree->n.sym->name, &expr->where);
1726 m = gfc_intrinsic_func_interface (expr, 0);
1730 gfc_error ("Generic function '%s' at %L is not consistent with a "
1731 "specific intrinsic interface", expr->symtree->n.sym->name,
1738 /* Resolve a function call known to be specific. */
1741 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
1745 /* See if we have an intrinsic interface. */
1747 if (sym->ts.interface != NULL && sym->ts.interface->attr.intrinsic)
1749 gfc_intrinsic_sym *isym;
1750 isym = gfc_find_function (sym->ts.interface->name);
1752 /* Existence of isym should be checked already. */
1755 sym->ts.type = isym->ts.type;
1756 sym->ts.kind = isym->ts.kind;
1757 sym->attr.function = 1;
1758 sym->attr.proc = PROC_EXTERNAL;
1762 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1764 if (sym->attr.dummy)
1766 sym->attr.proc = PROC_DUMMY;
1770 sym->attr.proc = PROC_EXTERNAL;
1774 if (sym->attr.proc == PROC_MODULE
1775 || sym->attr.proc == PROC_ST_FUNCTION
1776 || sym->attr.proc == PROC_INTERNAL)
1779 if (sym->attr.intrinsic)
1781 m = gfc_intrinsic_func_interface (expr, 1);
1785 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
1786 "with an intrinsic", sym->name, &expr->where);
1794 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1797 expr->value.function.name = sym->name;
1798 expr->value.function.esym = sym;
1799 if (sym->as != NULL)
1800 expr->rank = sym->as->rank;
1807 resolve_specific_f (gfc_expr *expr)
1812 sym = expr->symtree->n.sym;
1816 m = resolve_specific_f0 (sym, expr);
1819 if (m == MATCH_ERROR)
1822 if (sym->ns->parent == NULL)
1825 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1831 gfc_error ("Unable to resolve the specific function '%s' at %L",
1832 expr->symtree->n.sym->name, &expr->where);
1838 /* Resolve a procedure call not known to be generic nor specific. */
1841 resolve_unknown_f (gfc_expr *expr)
1846 sym = expr->symtree->n.sym;
1848 if (sym->attr.dummy)
1850 sym->attr.proc = PROC_DUMMY;
1851 expr->value.function.name = sym->name;
1855 /* See if we have an intrinsic function reference. */
1857 if (gfc_is_intrinsic (sym, 0, expr->where))
1859 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
1864 /* The reference is to an external name. */
1866 sym->attr.proc = PROC_EXTERNAL;
1867 expr->value.function.name = sym->name;
1868 expr->value.function.esym = expr->symtree->n.sym;
1870 if (sym->as != NULL)
1871 expr->rank = sym->as->rank;
1873 /* Type of the expression is either the type of the symbol or the
1874 default type of the symbol. */
1877 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1879 if (sym->ts.type != BT_UNKNOWN)
1883 ts = gfc_get_default_type (sym, sym->ns);
1885 if (ts->type == BT_UNKNOWN)
1887 gfc_error ("Function '%s' at %L has no IMPLICIT type",
1888 sym->name, &expr->where);
1899 /* Return true, if the symbol is an external procedure. */
1901 is_external_proc (gfc_symbol *sym)
1903 if (!sym->attr.dummy && !sym->attr.contained
1904 && !(sym->attr.intrinsic
1905 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
1906 && sym->attr.proc != PROC_ST_FUNCTION
1907 && !sym->attr.use_assoc
1915 /* Figure out if a function reference is pure or not. Also set the name
1916 of the function for a potential error message. Return nonzero if the
1917 function is PURE, zero if not. */
1919 pure_stmt_function (gfc_expr *, gfc_symbol *);
1922 pure_function (gfc_expr *e, const char **name)
1928 if (e->symtree != NULL
1929 && e->symtree->n.sym != NULL
1930 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
1931 return pure_stmt_function (e, e->symtree->n.sym);
1933 if (e->value.function.esym)
1935 pure = gfc_pure (e->value.function.esym);
1936 *name = e->value.function.esym->name;
1938 else if (e->value.function.isym)
1940 pure = e->value.function.isym->pure
1941 || e->value.function.isym->elemental;
1942 *name = e->value.function.isym->name;
1946 /* Implicit functions are not pure. */
1948 *name = e->value.function.name;
1956 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
1957 int *f ATTRIBUTE_UNUSED)
1961 /* Don't bother recursing into other statement functions
1962 since they will be checked individually for purity. */
1963 if (e->expr_type != EXPR_FUNCTION
1965 || e->symtree->n.sym == sym
1966 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
1969 return pure_function (e, &name) ? false : true;
1974 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
1976 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
1981 is_scalar_expr_ptr (gfc_expr *expr)
1983 gfc_try retval = SUCCESS;
1988 /* See if we have a gfc_ref, which means we have a substring, array
1989 reference, or a component. */
1990 if (expr->ref != NULL)
1993 while (ref->next != NULL)
1999 if (ref->u.ss.length != NULL
2000 && ref->u.ss.length->length != NULL
2002 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2004 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2006 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2007 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2008 if (end - start + 1 != 1)
2015 if (ref->u.ar.type == AR_ELEMENT)
2017 else if (ref->u.ar.type == AR_FULL)
2019 /* The user can give a full array if the array is of size 1. */
2020 if (ref->u.ar.as != NULL
2021 && ref->u.ar.as->rank == 1
2022 && ref->u.ar.as->type == AS_EXPLICIT
2023 && ref->u.ar.as->lower[0] != NULL
2024 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2025 && ref->u.ar.as->upper[0] != NULL
2026 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2028 /* If we have a character string, we need to check if
2029 its length is one. */
2030 if (expr->ts.type == BT_CHARACTER)
2032 if (expr->ts.cl == NULL
2033 || expr->ts.cl->length == NULL
2034 || mpz_cmp_si (expr->ts.cl->length->value.integer, 1)
2040 /* We have constant lower and upper bounds. If the
2041 difference between is 1, it can be considered a
2043 start = (int) mpz_get_si
2044 (ref->u.ar.as->lower[0]->value.integer);
2045 end = (int) mpz_get_si
2046 (ref->u.ar.as->upper[0]->value.integer);
2047 if (end - start + 1 != 1)
2062 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2064 /* Character string. Make sure it's of length 1. */
2065 if (expr->ts.cl == NULL
2066 || expr->ts.cl->length == NULL
2067 || mpz_cmp_si (expr->ts.cl->length->value.integer, 1) != 0)
2070 else if (expr->rank != 0)
2077 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2078 and, in the case of c_associated, set the binding label based on
2082 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2083 gfc_symbol **new_sym)
2085 char name[GFC_MAX_SYMBOL_LEN + 1];
2086 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2087 int optional_arg = 0, is_pointer = 0;
2088 gfc_try retval = SUCCESS;
2089 gfc_symbol *args_sym;
2090 gfc_typespec *arg_ts;
2092 if (args->expr->expr_type == EXPR_CONSTANT
2093 || args->expr->expr_type == EXPR_OP
2094 || args->expr->expr_type == EXPR_NULL)
2096 gfc_error ("Argument to '%s' at %L is not a variable",
2097 sym->name, &(args->expr->where));
2101 args_sym = args->expr->symtree->n.sym;
2103 /* The typespec for the actual arg should be that stored in the expr
2104 and not necessarily that of the expr symbol (args_sym), because
2105 the actual expression could be a part-ref of the expr symbol. */
2106 arg_ts = &(args->expr->ts);
2108 is_pointer = gfc_is_data_pointer (args->expr);
2110 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2112 /* If the user gave two args then they are providing something for
2113 the optional arg (the second cptr). Therefore, set the name and
2114 binding label to the c_associated for two cptrs. Otherwise,
2115 set c_associated to expect one cptr. */
2119 sprintf (name, "%s_2", sym->name);
2120 sprintf (binding_label, "%s_2", sym->binding_label);
2126 sprintf (name, "%s_1", sym->name);
2127 sprintf (binding_label, "%s_1", sym->binding_label);
2131 /* Get a new symbol for the version of c_associated that
2133 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2135 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2136 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2138 sprintf (name, "%s", sym->name);
2139 sprintf (binding_label, "%s", sym->binding_label);
2141 /* Error check the call. */
2142 if (args->next != NULL)
2144 gfc_error_now ("More actual than formal arguments in '%s' "
2145 "call at %L", name, &(args->expr->where));
2148 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2150 /* Make sure we have either the target or pointer attribute. */
2151 if (!args_sym->attr.target && !is_pointer)
2153 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2154 "a TARGET or an associated pointer",
2156 sym->name, &(args->expr->where));
2160 /* See if we have interoperable type and type param. */
2161 if (verify_c_interop (arg_ts) == SUCCESS
2162 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2164 if (args_sym->attr.target == 1)
2166 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2167 has the target attribute and is interoperable. */
2168 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2169 allocatable variable that has the TARGET attribute and
2170 is not an array of zero size. */
2171 if (args_sym->attr.allocatable == 1)
2173 if (args_sym->attr.dimension != 0
2174 && (args_sym->as && args_sym->as->rank == 0))
2176 gfc_error_now ("Allocatable variable '%s' used as a "
2177 "parameter to '%s' at %L must not be "
2178 "an array of zero size",
2179 args_sym->name, sym->name,
2180 &(args->expr->where));
2186 /* A non-allocatable target variable with C
2187 interoperable type and type parameters must be
2189 if (args_sym && args_sym->attr.dimension)
2191 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2193 gfc_error ("Assumed-shape array '%s' at %L "
2194 "cannot be an argument to the "
2195 "procedure '%s' because "
2196 "it is not C interoperable",
2198 &(args->expr->where), sym->name);
2201 else if (args_sym->as->type == AS_DEFERRED)
2203 gfc_error ("Deferred-shape array '%s' at %L "
2204 "cannot be an argument to the "
2205 "procedure '%s' because "
2206 "it is not C interoperable",
2208 &(args->expr->where), sym->name);
2213 /* Make sure it's not a character string. Arrays of
2214 any type should be ok if the variable is of a C
2215 interoperable type. */
2216 if (arg_ts->type == BT_CHARACTER)
2217 if (arg_ts->cl != NULL
2218 && (arg_ts->cl->length == NULL
2219 || arg_ts->cl->length->expr_type
2222 (arg_ts->cl->length->value.integer, 1)
2224 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2226 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2227 "at %L must have a length of 1",
2228 args_sym->name, sym->name,
2229 &(args->expr->where));
2235 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2237 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2239 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2240 "associated scalar POINTER", args_sym->name,
2241 sym->name, &(args->expr->where));
2247 /* The parameter is not required to be C interoperable. If it
2248 is not C interoperable, it must be a nonpolymorphic scalar
2249 with no length type parameters. It still must have either
2250 the pointer or target attribute, and it can be
2251 allocatable (but must be allocated when c_loc is called). */
2252 if (args->expr->rank != 0
2253 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2255 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2256 "scalar", args_sym->name, sym->name,
2257 &(args->expr->where));
2260 else if (arg_ts->type == BT_CHARACTER
2261 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2263 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2264 "%L must have a length of 1",
2265 args_sym->name, sym->name,
2266 &(args->expr->where));
2271 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2273 if (args_sym->attr.flavor != FL_PROCEDURE)
2275 /* TODO: Update this error message to allow for procedure
2276 pointers once they are implemented. */
2277 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2279 args_sym->name, sym->name,
2280 &(args->expr->where));
2283 else if (args_sym->attr.is_bind_c != 1)
2285 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2287 args_sym->name, sym->name,
2288 &(args->expr->where));
2293 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2298 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2299 "iso_c_binding function: '%s'!\n", sym->name);
2306 /* Resolve a function call, which means resolving the arguments, then figuring
2307 out which entity the name refers to. */
2308 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2309 to INTENT(OUT) or INTENT(INOUT). */
2312 resolve_function (gfc_expr *expr)
2314 gfc_actual_arglist *arg;
2319 procedure_type p = PROC_INTRINSIC;
2320 bool no_formal_args;
2324 sym = expr->symtree->n.sym;
2326 if (sym && sym->attr.intrinsic
2327 && !gfc_find_function (sym->name)
2328 && gfc_find_subroutine (sym->name)
2329 && sym->attr.function)
2331 gfc_error ("Intrinsic subroutine '%s' used as "
2332 "a function at %L", sym->name, &expr->where);
2336 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2338 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2342 if (sym && sym->attr.abstract)
2344 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2345 sym->name, &expr->where);
2349 /* Switch off assumed size checking and do this again for certain kinds
2350 of procedure, once the procedure itself is resolved. */
2351 need_full_assumed_size++;
2353 if (expr->symtree && expr->symtree->n.sym)
2354 p = expr->symtree->n.sym->attr.proc;
2356 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2357 if (resolve_actual_arglist (expr->value.function.actual,
2358 p, no_formal_args) == FAILURE)
2361 /* Need to setup the call to the correct c_associated, depending on
2362 the number of cptrs to user gives to compare. */
2363 if (sym && sym->attr.is_iso_c == 1)
2365 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2369 /* Get the symtree for the new symbol (resolved func).
2370 the old one will be freed later, when it's no longer used. */
2371 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2374 /* Resume assumed_size checking. */
2375 need_full_assumed_size--;
2377 /* If the procedure is external, check for usage. */
2378 if (sym && is_external_proc (sym))
2379 resolve_global_procedure (sym, &expr->where,
2380 &expr->value.function.actual, 0);
2382 if (sym && sym->ts.type == BT_CHARACTER
2384 && sym->ts.cl->length == NULL
2386 && expr->value.function.esym == NULL
2387 && !sym->attr.contained)
2389 /* Internal procedures are taken care of in resolve_contained_fntype. */
2390 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2391 "be used at %L since it is not a dummy argument",
2392 sym->name, &expr->where);
2396 /* See if function is already resolved. */
2398 if (expr->value.function.name != NULL)
2400 if (expr->ts.type == BT_UNKNOWN)
2406 /* Apply the rules of section 14.1.2. */
2408 switch (procedure_kind (sym))
2411 t = resolve_generic_f (expr);
2414 case PTYPE_SPECIFIC:
2415 t = resolve_specific_f (expr);
2419 t = resolve_unknown_f (expr);
2423 gfc_internal_error ("resolve_function(): bad function type");
2427 /* If the expression is still a function (it might have simplified),
2428 then we check to see if we are calling an elemental function. */
2430 if (expr->expr_type != EXPR_FUNCTION)
2433 temp = need_full_assumed_size;
2434 need_full_assumed_size = 0;
2436 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2439 if (omp_workshare_flag
2440 && expr->value.function.esym
2441 && ! gfc_elemental (expr->value.function.esym))
2443 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2444 "in WORKSHARE construct", expr->value.function.esym->name,
2449 #define GENERIC_ID expr->value.function.isym->id
2450 else if (expr->value.function.actual != NULL
2451 && expr->value.function.isym != NULL
2452 && GENERIC_ID != GFC_ISYM_LBOUND
2453 && GENERIC_ID != GFC_ISYM_LEN
2454 && GENERIC_ID != GFC_ISYM_LOC
2455 && GENERIC_ID != GFC_ISYM_PRESENT)
2457 /* Array intrinsics must also have the last upper bound of an
2458 assumed size array argument. UBOUND and SIZE have to be
2459 excluded from the check if the second argument is anything
2462 for (arg = expr->value.function.actual; arg; arg = arg->next)
2464 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2465 && arg->next != NULL && arg->next->expr)
2467 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2470 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2473 if ((int)mpz_get_si (arg->next->expr->value.integer)
2478 if (arg->expr != NULL
2479 && arg->expr->rank > 0
2480 && resolve_assumed_size_actual (arg->expr))
2486 need_full_assumed_size = temp;
2489 if (!pure_function (expr, &name) && name)
2493 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2494 "FORALL %s", name, &expr->where,
2495 forall_flag == 2 ? "mask" : "block");
2498 else if (gfc_pure (NULL))
2500 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2501 "procedure within a PURE procedure", name, &expr->where);
2506 /* Functions without the RECURSIVE attribution are not allowed to
2507 * call themselves. */
2508 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2511 esym = expr->value.function.esym;
2513 if (is_illegal_recursion (esym, gfc_current_ns))
2515 if (esym->attr.entry && esym->ns->entries)
2516 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2517 " function '%s' is not RECURSIVE",
2518 esym->name, &expr->where, esym->ns->entries->sym->name);
2520 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2521 " is not RECURSIVE", esym->name, &expr->where);
2527 /* Character lengths of use associated functions may contains references to
2528 symbols not referenced from the current program unit otherwise. Make sure
2529 those symbols are marked as referenced. */
2531 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2532 && expr->value.function.esym->attr.use_assoc)
2534 gfc_expr_set_symbols_referenced (expr->ts.cl->length);
2538 && !((expr->value.function.esym
2539 && expr->value.function.esym->attr.elemental)
2541 (expr->value.function.isym
2542 && expr->value.function.isym->elemental)))
2543 find_noncopying_intrinsics (expr->value.function.esym,
2544 expr->value.function.actual);
2546 /* Make sure that the expression has a typespec that works. */
2547 if (expr->ts.type == BT_UNKNOWN)
2549 if (expr->symtree->n.sym->result
2550 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN)
2551 expr->ts = expr->symtree->n.sym->result->ts;
2558 /************* Subroutine resolution *************/
2561 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2567 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2568 sym->name, &c->loc);
2569 else if (gfc_pure (NULL))
2570 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2576 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2580 if (sym->attr.generic)
2582 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2585 c->resolved_sym = s;
2586 pure_subroutine (c, s);
2590 /* TODO: Need to search for elemental references in generic interface. */
2593 if (sym->attr.intrinsic)
2594 return gfc_intrinsic_sub_interface (c, 0);
2601 resolve_generic_s (gfc_code *c)
2606 sym = c->symtree->n.sym;
2610 m = resolve_generic_s0 (c, sym);
2613 else if (m == MATCH_ERROR)
2617 if (sym->ns->parent == NULL)
2619 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2623 if (!generic_sym (sym))
2627 /* Last ditch attempt. See if the reference is to an intrinsic
2628 that possesses a matching interface. 14.1.2.4 */
2629 sym = c->symtree->n.sym;
2631 if (!gfc_is_intrinsic (sym, 1, c->loc))
2633 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2634 sym->name, &c->loc);
2638 m = gfc_intrinsic_sub_interface (c, 0);
2642 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2643 "intrinsic subroutine interface", sym->name, &c->loc);
2649 /* Set the name and binding label of the subroutine symbol in the call
2650 expression represented by 'c' to include the type and kind of the
2651 second parameter. This function is for resolving the appropriate
2652 version of c_f_pointer() and c_f_procpointer(). For example, a
2653 call to c_f_pointer() for a default integer pointer could have a
2654 name of c_f_pointer_i4. If no second arg exists, which is an error
2655 for these two functions, it defaults to the generic symbol's name
2656 and binding label. */
2659 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2660 char *name, char *binding_label)
2662 gfc_expr *arg = NULL;
2666 /* The second arg of c_f_pointer and c_f_procpointer determines
2667 the type and kind for the procedure name. */
2668 arg = c->ext.actual->next->expr;
2672 /* Set up the name to have the given symbol's name,
2673 plus the type and kind. */
2674 /* a derived type is marked with the type letter 'u' */
2675 if (arg->ts.type == BT_DERIVED)
2678 kind = 0; /* set the kind as 0 for now */
2682 type = gfc_type_letter (arg->ts.type);
2683 kind = arg->ts.kind;
2686 if (arg->ts.type == BT_CHARACTER)
2687 /* Kind info for character strings not needed. */
2690 sprintf (name, "%s_%c%d", sym->name, type, kind);
2691 /* Set up the binding label as the given symbol's label plus
2692 the type and kind. */
2693 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2697 /* If the second arg is missing, set the name and label as
2698 was, cause it should at least be found, and the missing
2699 arg error will be caught by compare_parameters(). */
2700 sprintf (name, "%s", sym->name);
2701 sprintf (binding_label, "%s", sym->binding_label);
2708 /* Resolve a generic version of the iso_c_binding procedure given
2709 (sym) to the specific one based on the type and kind of the
2710 argument(s). Currently, this function resolves c_f_pointer() and
2711 c_f_procpointer based on the type and kind of the second argument
2712 (FPTR). Other iso_c_binding procedures aren't specially handled.
2713 Upon successfully exiting, c->resolved_sym will hold the resolved
2714 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2718 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2720 gfc_symbol *new_sym;
2721 /* this is fine, since we know the names won't use the max */
2722 char name[GFC_MAX_SYMBOL_LEN + 1];
2723 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2724 /* default to success; will override if find error */
2725 match m = MATCH_YES;
2727 /* Make sure the actual arguments are in the necessary order (based on the
2728 formal args) before resolving. */
2729 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2731 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2732 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2734 set_name_and_label (c, sym, name, binding_label);
2736 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2738 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2740 /* Make sure we got a third arg if the second arg has non-zero
2741 rank. We must also check that the type and rank are
2742 correct since we short-circuit this check in
2743 gfc_procedure_use() (called above to sort actual args). */
2744 if (c->ext.actual->next->expr->rank != 0)
2746 if(c->ext.actual->next->next == NULL
2747 || c->ext.actual->next->next->expr == NULL)
2750 gfc_error ("Missing SHAPE parameter for call to %s "
2751 "at %L", sym->name, &(c->loc));
2753 else if (c->ext.actual->next->next->expr->ts.type
2755 || c->ext.actual->next->next->expr->rank != 1)
2758 gfc_error ("SHAPE parameter for call to %s at %L must "
2759 "be a rank 1 INTEGER array", sym->name,
2766 if (m != MATCH_ERROR)
2768 /* the 1 means to add the optional arg to formal list */
2769 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
2771 /* for error reporting, say it's declared where the original was */
2772 new_sym->declared_at = sym->declared_at;
2777 /* no differences for c_loc or c_funloc */
2781 /* set the resolved symbol */
2782 if (m != MATCH_ERROR)
2783 c->resolved_sym = new_sym;
2785 c->resolved_sym = sym;
2791 /* Resolve a subroutine call known to be specific. */
2794 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
2798 /* See if we have an intrinsic interface. */
2799 if (sym->ts.interface != NULL && !sym->ts.interface->attr.abstract
2800 && !sym->ts.interface->attr.subroutine
2801 && sym->ts.interface->attr.intrinsic)
2803 gfc_intrinsic_sym *isym;
2805 isym = gfc_find_function (sym->ts.interface->name);
2807 /* Existence of isym should be checked already. */
2810 sym->ts.type = isym->ts.type;
2811 sym->ts.kind = isym->ts.kind;
2812 sym->attr.subroutine = 1;
2816 if(sym->attr.is_iso_c)
2818 m = gfc_iso_c_sub_interface (c,sym);
2822 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2824 if (sym->attr.dummy)
2826 sym->attr.proc = PROC_DUMMY;
2830 sym->attr.proc = PROC_EXTERNAL;
2834 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
2837 if (sym->attr.intrinsic)
2839 m = gfc_intrinsic_sub_interface (c, 1);
2843 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
2844 "with an intrinsic", sym->name, &c->loc);
2852 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2854 c->resolved_sym = sym;
2855 pure_subroutine (c, sym);
2862 resolve_specific_s (gfc_code *c)
2867 sym = c->symtree->n.sym;
2871 m = resolve_specific_s0 (c, sym);
2874 if (m == MATCH_ERROR)
2877 if (sym->ns->parent == NULL)
2880 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2886 sym = c->symtree->n.sym;
2887 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
2888 sym->name, &c->loc);
2894 /* Resolve a subroutine call not known to be generic nor specific. */
2897 resolve_unknown_s (gfc_code *c)
2901 sym = c->symtree->n.sym;
2903 if (sym->attr.dummy)
2905 sym->attr.proc = PROC_DUMMY;
2909 /* See if we have an intrinsic function reference. */
2911 if (gfc_is_intrinsic (sym, 1, c->loc))
2913 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
2918 /* The reference is to an external name. */
2921 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2923 c->resolved_sym = sym;
2925 pure_subroutine (c, sym);
2931 /* Resolve a subroutine call. Although it was tempting to use the same code
2932 for functions, subroutines and functions are stored differently and this
2933 makes things awkward. */
2936 resolve_call (gfc_code *c)
2939 procedure_type ptype = PROC_INTRINSIC;
2940 gfc_symbol *csym, *sym;
2941 bool no_formal_args;
2943 csym = c->symtree ? c->symtree->n.sym : NULL;
2945 if (csym && csym->ts.type != BT_UNKNOWN)
2947 gfc_error ("'%s' at %L has a type, which is not consistent with "
2948 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
2952 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
2955 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
2956 sym = st ? st->n.sym : NULL;
2957 if (sym && csym != sym
2958 && sym->ns == gfc_current_ns
2959 && sym->attr.flavor == FL_PROCEDURE
2960 && sym->attr.contained)
2963 if (csym->attr.generic)
2964 c->symtree->n.sym = sym;
2967 csym = c->symtree->n.sym;
2971 /* Subroutines without the RECURSIVE attribution are not allowed to
2972 * call themselves. */
2973 if (csym && is_illegal_recursion (csym, gfc_current_ns))
2975 if (csym->attr.entry && csym->ns->entries)
2976 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2977 " subroutine '%s' is not RECURSIVE",
2978 csym->name, &c->loc, csym->ns->entries->sym->name);
2980 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
2981 " is not RECURSIVE", csym->name, &c->loc);
2986 /* Switch off assumed size checking and do this again for certain kinds
2987 of procedure, once the procedure itself is resolved. */
2988 need_full_assumed_size++;
2991 ptype = csym->attr.proc;
2993 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
2994 if (resolve_actual_arglist (c->ext.actual, ptype,
2995 no_formal_args) == FAILURE)
2998 /* Resume assumed_size checking. */
2999 need_full_assumed_size--;
3001 /* If external, check for usage. */
3002 if (csym && is_external_proc (csym))
3003 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3006 if (c->resolved_sym == NULL)
3008 c->resolved_isym = NULL;
3009 switch (procedure_kind (csym))
3012 t = resolve_generic_s (c);
3015 case PTYPE_SPECIFIC:
3016 t = resolve_specific_s (c);
3020 t = resolve_unknown_s (c);
3024 gfc_internal_error ("resolve_subroutine(): bad function type");
3028 /* Some checks of elemental subroutine actual arguments. */
3029 if (resolve_elemental_actual (NULL, c) == FAILURE)
3032 if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
3033 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
3038 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3039 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3040 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3041 if their shapes do not match. If either op1->shape or op2->shape is
3042 NULL, return SUCCESS. */
3045 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3052 if (op1->shape != NULL && op2->shape != NULL)
3054 for (i = 0; i < op1->rank; i++)
3056 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3058 gfc_error ("Shapes for operands at %L and %L are not conformable",
3059 &op1->where, &op2->where);
3070 /* Resolve an operator expression node. This can involve replacing the
3071 operation with a user defined function call. */
3074 resolve_operator (gfc_expr *e)
3076 gfc_expr *op1, *op2;
3078 bool dual_locus_error;
3081 /* Resolve all subnodes-- give them types. */
3083 switch (e->value.op.op)
3086 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3089 /* Fall through... */
3092 case INTRINSIC_UPLUS:
3093 case INTRINSIC_UMINUS:
3094 case INTRINSIC_PARENTHESES:
3095 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3100 /* Typecheck the new node. */
3102 op1 = e->value.op.op1;
3103 op2 = e->value.op.op2;
3104 dual_locus_error = false;
3106 if ((op1 && op1->expr_type == EXPR_NULL)
3107 || (op2 && op2->expr_type == EXPR_NULL))
3109 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3113 switch (e->value.op.op)
3115 case INTRINSIC_UPLUS:
3116 case INTRINSIC_UMINUS:
3117 if (op1->ts.type == BT_INTEGER
3118 || op1->ts.type == BT_REAL
3119 || op1->ts.type == BT_COMPLEX)
3125 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3126 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3129 case INTRINSIC_PLUS:
3130 case INTRINSIC_MINUS:
3131 case INTRINSIC_TIMES:
3132 case INTRINSIC_DIVIDE:
3133 case INTRINSIC_POWER:
3134 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3136 gfc_type_convert_binary (e);
3141 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3142 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3143 gfc_typename (&op2->ts));
3146 case INTRINSIC_CONCAT:
3147 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3148 && op1->ts.kind == op2->ts.kind)
3150 e->ts.type = BT_CHARACTER;
3151 e->ts.kind = op1->ts.kind;
3156 _("Operands of string concatenation operator at %%L are %s/%s"),
3157 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3163 case INTRINSIC_NEQV:
3164 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3166 e->ts.type = BT_LOGICAL;
3167 e->ts.kind = gfc_kind_max (op1, op2);
3168 if (op1->ts.kind < e->ts.kind)
3169 gfc_convert_type (op1, &e->ts, 2);
3170 else if (op2->ts.kind < e->ts.kind)
3171 gfc_convert_type (op2, &e->ts, 2);
3175 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3176 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3177 gfc_typename (&op2->ts));
3182 if (op1->ts.type == BT_LOGICAL)
3184 e->ts.type = BT_LOGICAL;
3185 e->ts.kind = op1->ts.kind;
3189 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3190 gfc_typename (&op1->ts));
3194 case INTRINSIC_GT_OS:
3196 case INTRINSIC_GE_OS:
3198 case INTRINSIC_LT_OS:
3200 case INTRINSIC_LE_OS:
3201 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3203 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3207 /* Fall through... */
3210 case INTRINSIC_EQ_OS:
3212 case INTRINSIC_NE_OS:
3213 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3214 && op1->ts.kind == op2->ts.kind)
3216 e->ts.type = BT_LOGICAL;
3217 e->ts.kind = gfc_default_logical_kind;
3221 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3223 gfc_type_convert_binary (e);
3225 e->ts.type = BT_LOGICAL;
3226 e->ts.kind = gfc_default_logical_kind;
3230 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3232 _("Logicals at %%L must be compared with %s instead of %s"),
3233 (e->value.op.op == INTRINSIC_EQ
3234 || e->value.op.op == INTRINSIC_EQ_OS)
3235 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3238 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3239 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3240 gfc_typename (&op2->ts));
3244 case INTRINSIC_USER:
3245 if (e->value.op.uop->op == NULL)
3246 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3247 else if (op2 == NULL)
3248 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3249 e->value.op.uop->name, gfc_typename (&op1->ts));
3251 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3252 e->value.op.uop->name, gfc_typename (&op1->ts),
3253 gfc_typename (&op2->ts));
3257 case INTRINSIC_PARENTHESES:
3259 if (e->ts.type == BT_CHARACTER)
3260 e->ts.cl = op1->ts.cl;
3264 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3267 /* Deal with arrayness of an operand through an operator. */
3271 switch (e->value.op.op)
3273 case INTRINSIC_PLUS:
3274 case INTRINSIC_MINUS:
3275 case INTRINSIC_TIMES:
3276 case INTRINSIC_DIVIDE:
3277 case INTRINSIC_POWER:
3278 case INTRINSIC_CONCAT:
3282 case INTRINSIC_NEQV:
3284 case INTRINSIC_EQ_OS:
3286 case INTRINSIC_NE_OS:
3288 case INTRINSIC_GT_OS:
3290 case INTRINSIC_GE_OS:
3292 case INTRINSIC_LT_OS:
3294 case INTRINSIC_LE_OS:
3296 if (op1->rank == 0 && op2->rank == 0)
3299 if (op1->rank == 0 && op2->rank != 0)
3301 e->rank = op2->rank;
3303 if (e->shape == NULL)
3304 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3307 if (op1->rank != 0 && op2->rank == 0)
3309 e->rank = op1->rank;
3311 if (e->shape == NULL)
3312 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3315 if (op1->rank != 0 && op2->rank != 0)
3317 if (op1->rank == op2->rank)
3319 e->rank = op1->rank;
3320 if (e->shape == NULL)
3322 t = compare_shapes(op1, op2);
3326 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3331 /* Allow higher level expressions to work. */
3334 /* Try user-defined operators, and otherwise throw an error. */
3335 dual_locus_error = true;
3337 _("Inconsistent ranks for operator at %%L and %%L"));
3344 case INTRINSIC_PARENTHESES:
3346 case INTRINSIC_UPLUS:
3347 case INTRINSIC_UMINUS:
3348 /* Simply copy arrayness attribute */
3349 e->rank = op1->rank;
3351 if (e->shape == NULL)
3352 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3360 /* Attempt to simplify the expression. */
3363 t = gfc_simplify_expr (e, 0);
3364 /* Some calls do not succeed in simplification and return FAILURE
3365 even though there is no error; e.g. variable references to
3366 PARAMETER arrays. */
3367 if (!gfc_is_constant_expr (e))
3374 if (gfc_extend_expr (e) == SUCCESS)
3377 if (dual_locus_error)
3378 gfc_error (msg, &op1->where, &op2->where);
3380 gfc_error (msg, &e->where);
3386 /************** Array resolution subroutines **************/
3389 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3392 /* Compare two integer expressions. */
3395 compare_bound (gfc_expr *a, gfc_expr *b)
3399 if (a == NULL || a->expr_type != EXPR_CONSTANT
3400 || b == NULL || b->expr_type != EXPR_CONSTANT)
3403 /* If either of the types isn't INTEGER, we must have
3404 raised an error earlier. */
3406 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3409 i = mpz_cmp (a->value.integer, b->value.integer);
3419 /* Compare an integer expression with an integer. */
3422 compare_bound_int (gfc_expr *a, int b)
3426 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3429 if (a->ts.type != BT_INTEGER)
3430 gfc_internal_error ("compare_bound_int(): Bad expression");
3432 i = mpz_cmp_si (a->value.integer, b);
3442 /* Compare an integer expression with a mpz_t. */
3445 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3449 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3452 if (a->ts.type != BT_INTEGER)
3453 gfc_internal_error ("compare_bound_int(): Bad expression");
3455 i = mpz_cmp (a->value.integer, b);
3465 /* Compute the last value of a sequence given by a triplet.
3466 Return 0 if it wasn't able to compute the last value, or if the
3467 sequence if empty, and 1 otherwise. */
3470 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3471 gfc_expr *stride, mpz_t last)
3475 if (start == NULL || start->expr_type != EXPR_CONSTANT
3476 || end == NULL || end->expr_type != EXPR_CONSTANT
3477 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3480 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3481 || (stride != NULL && stride->ts.type != BT_INTEGER))
3484 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3486 if (compare_bound (start, end) == CMP_GT)
3488 mpz_set (last, end->value.integer);
3492 if (compare_bound_int (stride, 0) == CMP_GT)
3494 /* Stride is positive */
3495 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3500 /* Stride is negative */
3501 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3506 mpz_sub (rem, end->value.integer, start->value.integer);
3507 mpz_tdiv_r (rem, rem, stride->value.integer);
3508 mpz_sub (last, end->value.integer, rem);
3515 /* Compare a single dimension of an array reference to the array
3519 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3523 /* Given start, end and stride values, calculate the minimum and
3524 maximum referenced indexes. */
3526 switch (ar->dimen_type[i])
3532 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3534 gfc_warning ("Array reference at %L is out of bounds "
3535 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3536 mpz_get_si (ar->start[i]->value.integer),
3537 mpz_get_si (as->lower[i]->value.integer), i+1);
3540 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3542 gfc_warning ("Array reference at %L is out of bounds "
3543 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3544 mpz_get_si (ar->start[i]->value.integer),
3545 mpz_get_si (as->upper[i]->value.integer), i+1);
3553 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3554 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3556 comparison comp_start_end = compare_bound (AR_START, AR_END);
3558 /* Check for zero stride, which is not allowed. */
3559 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3561 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3565 /* if start == len || (stride > 0 && start < len)
3566 || (stride < 0 && start > len),
3567 then the array section contains at least one element. In this
3568 case, there is an out-of-bounds access if
3569 (start < lower || start > upper). */
3570 if (compare_bound (AR_START, AR_END) == CMP_EQ
3571 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3572 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3573 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3574 && comp_start_end == CMP_GT))
3576 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3578 gfc_warning ("Lower array reference at %L is out of bounds "
3579 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3580 mpz_get_si (AR_START->value.integer),
3581 mpz_get_si (as->lower[i]->value.integer), i+1);
3584 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3586 gfc_warning ("Lower array reference at %L is out of bounds "
3587 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3588 mpz_get_si (AR_START->value.integer),
3589 mpz_get_si (as->upper[i]->value.integer), i+1);
3594 /* If we can compute the highest index of the array section,
3595 then it also has to be between lower and upper. */
3596 mpz_init (last_value);
3597 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3600 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3602 gfc_warning ("Upper array reference at %L is out of bounds "
3603 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3604 mpz_get_si (last_value),
3605 mpz_get_si (as->lower[i]->value.integer), i+1);
3606 mpz_clear (last_value);
3609 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3611 gfc_warning ("Upper array reference at %L is out of bounds "
3612 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3613 mpz_get_si (last_value),
3614 mpz_get_si (as->upper[i]->value.integer), i+1);
3615 mpz_clear (last_value);
3619 mpz_clear (last_value);
3627 gfc_internal_error ("check_dimension(): Bad array reference");
3634 /* Compare an array reference with an array specification. */
3637 compare_spec_to_ref (gfc_array_ref *ar)
3644 /* TODO: Full array sections are only allowed as actual parameters. */
3645 if (as->type == AS_ASSUMED_SIZE
3646 && (/*ar->type == AR_FULL
3647 ||*/ (ar->type == AR_SECTION
3648 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3650 gfc_error ("Rightmost upper bound of assumed size array section "
3651 "not specified at %L", &ar->where);
3655 if (ar->type == AR_FULL)
3658 if (as->rank != ar->dimen)
3660 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3661 &ar->where, ar->dimen, as->rank);
3665 for (i = 0; i < as->rank; i++)
3666 if (check_dimension (i, ar, as) == FAILURE)
3673 /* Resolve one part of an array index. */
3676 gfc_resolve_index (gfc_expr *index, int check_scalar)
3683 if (gfc_resolve_expr (index) == FAILURE)
3686 if (check_scalar && index->rank != 0)
3688 gfc_error ("Array index at %L must be scalar", &index->where);
3692 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
3694 gfc_error ("Array index at %L must be of INTEGER type, found %s",
3695 &index->where, gfc_basic_typename (index->ts.type));
3699 if (index->ts.type == BT_REAL)
3700 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
3701 &index->where) == FAILURE)
3704 if (index->ts.kind != gfc_index_integer_kind
3705 || index->ts.type != BT_INTEGER)
3708 ts.type = BT_INTEGER;
3709 ts.kind = gfc_index_integer_kind;
3711 gfc_convert_type_warn (index, &ts, 2, 0);
3717 /* Resolve a dim argument to an intrinsic function. */
3720 gfc_resolve_dim_arg (gfc_expr *dim)
3725 if (gfc_resolve_expr (dim) == FAILURE)
3730 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3735 if (dim->ts.type != BT_INTEGER)
3737 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3741 if (dim->ts.kind != gfc_index_integer_kind)
3745 ts.type = BT_INTEGER;
3746 ts.kind = gfc_index_integer_kind;
3748 gfc_convert_type_warn (dim, &ts, 2, 0);
3754 /* Given an expression that contains array references, update those array
3755 references to point to the right array specifications. While this is
3756 filled in during matching, this information is difficult to save and load
3757 in a module, so we take care of it here.
3759 The idea here is that the original array reference comes from the
3760 base symbol. We traverse the list of reference structures, setting
3761 the stored reference to references. Component references can
3762 provide an additional array specification. */
3765 find_array_spec (gfc_expr *e)
3769 gfc_symbol *derived;
3772 as = e->symtree->n.sym->as;
3775 for (ref = e->ref; ref; ref = ref->next)
3780 gfc_internal_error ("find_array_spec(): Missing spec");
3787 if (derived == NULL)
3788 derived = e->symtree->n.sym->ts.derived;
3790 c = derived->components;
3792 for (; c; c = c->next)
3793 if (c == ref->u.c.component)
3795 /* Track the sequence of component references. */
3796 if (c->ts.type == BT_DERIVED)
3797 derived = c->ts.derived;
3802 gfc_internal_error ("find_array_spec(): Component not found");
3804 if (c->attr.dimension)
3807 gfc_internal_error ("find_array_spec(): unused as(1)");
3818 gfc_internal_error ("find_array_spec(): unused as(2)");
3822 /* Resolve an array reference. */
3825 resolve_array_ref (gfc_array_ref *ar)
3827 int i, check_scalar;
3830 for (i = 0; i < ar->dimen; i++)
3832 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
3834 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
3836 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
3838 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
3843 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
3847 ar->dimen_type[i] = DIMEN_ELEMENT;
3851 ar->dimen_type[i] = DIMEN_VECTOR;
3852 if (e->expr_type == EXPR_VARIABLE
3853 && e->symtree->n.sym->ts.type == BT_DERIVED)
3854 ar->start[i] = gfc_get_parentheses (e);
3858 gfc_error ("Array index at %L is an array of rank %d",
3859 &ar->c_where[i], e->rank);
3864 /* If the reference type is unknown, figure out what kind it is. */
3866 if (ar->type == AR_UNKNOWN)
3868 ar->type = AR_ELEMENT;
3869 for (i = 0; i < ar->dimen; i++)
3870 if (ar->dimen_type[i] == DIMEN_RANGE
3871 || ar->dimen_type[i] == DIMEN_VECTOR)
3873 ar->type = AR_SECTION;
3878 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
3886 resolve_substring (gfc_ref *ref)
3888 if (ref->u.ss.start != NULL)
3890 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
3893 if (ref->u.ss.start->ts.type != BT_INTEGER)
3895 gfc_error ("Substring start index at %L must be of type INTEGER",
3896 &ref->u.ss.start->where);
3900 if (ref->u.ss.start->rank != 0)
3902 gfc_error ("Substring start index at %L must be scalar",
3903 &ref->u.ss.start->where);
3907 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
3908 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
3909 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
3911 gfc_error ("Substring start index at %L is less than one",
3912 &ref->u.ss.start->where);
3917 if (ref->u.ss.end != NULL)
3919 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
3922 if (ref->u.ss.end->ts.type != BT_INTEGER)
3924 gfc_error ("Substring end index at %L must be of type INTEGER",
3925 &ref->u.ss.end->where);
3929 if (ref->u.ss.end->rank != 0)
3931 gfc_error ("Substring end index at %L must be scalar",
3932 &ref->u.ss.end->where);
3936 if (ref->u.ss.length != NULL
3937 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
3938 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
3939 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
3941 gfc_error ("Substring end index at %L exceeds the string length",
3942 &ref->u.ss.start->where);
3951 /* This function supplies missing substring charlens. */
3954 gfc_resolve_substring_charlen (gfc_expr *e)
3957 gfc_expr *start, *end;
3959 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
3960 if (char_ref->type == REF_SUBSTRING)
3966 gcc_assert (char_ref->next == NULL);
3970 if (e->ts.cl->length)
3971 gfc_free_expr (e->ts.cl->length);
3972 else if (e->expr_type == EXPR_VARIABLE
3973 && e->symtree->n.sym->attr.dummy)
3977 e->ts.type = BT_CHARACTER;
3978 e->ts.kind = gfc_default_character_kind;
3982 e->ts.cl = gfc_get_charlen ();
3983 e->ts.cl->next = gfc_current_ns->cl_list;
3984 gfc_current_ns->cl_list = e->ts.cl;
3987 if (char_ref->u.ss.start)
3988 start = gfc_copy_expr (char_ref->u.ss.start);
3990 start = gfc_int_expr (1);
3992 if (char_ref->u.ss.end)
3993 end = gfc_copy_expr (char_ref->u.ss.end);
3994 else if (e->expr_type == EXPR_VARIABLE)
3995 end = gfc_copy_expr (e->symtree->n.sym->ts.cl->length);
4002 /* Length = (end - start +1). */
4003 e->ts.cl->length = gfc_subtract (end, start);
4004 e->ts.cl->length = gfc_add (e->ts.cl->length, gfc_int_expr (1));
4006 e->ts.cl->length->ts.type = BT_INTEGER;
4007 e->ts.cl->length->ts.kind = gfc_charlen_int_kind;;
4009 /* Make sure that the length is simplified. */
4010 gfc_simplify_expr (e->ts.cl->length, 1);
4011 gfc_resolve_expr (e->ts.cl->length);
4015 /* Resolve subtype references. */
4018 resolve_ref (gfc_expr *expr)
4020 int current_part_dimension, n_components, seen_part_dimension;
4023 for (ref = expr->ref; ref; ref = ref->next)
4024 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4026 find_array_spec (expr);
4030 for (ref = expr->ref; ref; ref = ref->next)
4034 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4042 resolve_substring (ref);
4046 /* Check constraints on part references. */
4048 current_part_dimension = 0;
4049 seen_part_dimension = 0;
4052 for (ref = expr->ref; ref; ref = ref->next)
4057 switch (ref->u.ar.type)
4061 current_part_dimension = 1;
4065 current_part_dimension = 0;
4069 gfc_internal_error ("resolve_ref(): Bad array reference");
4075 if (current_part_dimension || seen_part_dimension)
4077 if (ref->u.c.component->attr.pointer)
4079 gfc_error ("Component to the right of a part reference "
4080 "with nonzero rank must not have the POINTER "
4081 "attribute at %L", &expr->where);
4084 else if (ref->u.c.component->attr.allocatable)
4086 gfc_error ("Component to the right of a part reference "
4087 "with nonzero rank must not have the ALLOCATABLE "
4088 "attribute at %L", &expr->where);
4100 if (((ref->type == REF_COMPONENT && n_components > 1)
4101 || ref->next == NULL)
4102 && current_part_dimension
4103 && seen_part_dimension)
4105 gfc_error ("Two or more part references with nonzero rank must "
4106 "not be specified at %L", &expr->where);
4110 if (ref->type == REF_COMPONENT)
4112 if (current_part_dimension)
4113 seen_part_dimension = 1;
4115 /* reset to make sure */
4116 current_part_dimension = 0;
4124 /* Given an expression, determine its shape. This is easier than it sounds.
4125 Leaves the shape array NULL if it is not possible to determine the shape. */
4128 expression_shape (gfc_expr *e)
4130 mpz_t array[GFC_MAX_DIMENSIONS];
4133 if (e->rank == 0 || e->shape != NULL)
4136 for (i = 0; i < e->rank; i++)
4137 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4140 e->shape = gfc_get_shape (e->rank);
4142 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4147 for (i--; i >= 0; i--)
4148 mpz_clear (array[i]);
4152 /* Given a variable expression node, compute the rank of the expression by
4153 examining the base symbol and any reference structures it may have. */
4156 expression_rank (gfc_expr *e)
4161 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4162 could lead to serious confusion... */
4163 gcc_assert (e->expr_type != EXPR_COMPCALL);
4167 if (e->expr_type == EXPR_ARRAY)
4169 /* Constructors can have a rank different from one via RESHAPE(). */
4171 if (e->symtree == NULL)
4177 e->rank = (e->symtree->n.sym->as == NULL)
4178 ? 0 : e->symtree->n.sym->as->rank;
4184 for (ref = e->ref; ref; ref = ref->next)
4186 if (ref->type != REF_ARRAY)
4189 if (ref->u.ar.type == AR_FULL)
4191 rank = ref->u.ar.as->rank;
4195 if (ref->u.ar.type == AR_SECTION)
4197 /* Figure out the rank of the section. */
4199 gfc_internal_error ("expression_rank(): Two array specs");
4201 for (i = 0; i < ref->u.ar.dimen; i++)
4202 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4203 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4213 expression_shape (e);
4217 /* Resolve a variable expression. */
4220 resolve_variable (gfc_expr *e)
4227 if (e->symtree == NULL)
4230 if (e->ref && resolve_ref (e) == FAILURE)
4233 sym = e->symtree->n.sym;
4234 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
4236 e->ts.type = BT_PROCEDURE;
4237 goto resolve_procedure;
4240 if (sym->ts.type != BT_UNKNOWN)
4241 gfc_variable_attr (e, &e->ts);
4244 /* Must be a simple variable reference. */
4245 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4250 if (check_assumed_size_reference (sym, e))
4253 /* Deal with forward references to entries during resolve_code, to
4254 satisfy, at least partially, 12.5.2.5. */
4255 if (gfc_current_ns->entries
4256 && current_entry_id == sym->entry_id
4259 && cs_base->current->op != EXEC_ENTRY)
4261 gfc_entry_list *entry;
4262 gfc_formal_arglist *formal;
4266 /* If the symbol is a dummy... */
4267 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4269 entry = gfc_current_ns->entries;
4272 /* ...test if the symbol is a parameter of previous entries. */
4273 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4274 for (formal = entry->sym->formal; formal; formal = formal->next)
4276 if (formal->sym && sym->name == formal->sym->name)
4280 /* If it has not been seen as a dummy, this is an error. */
4283 if (specification_expr)
4284 gfc_error ("Variable '%s', used in a specification expression"
4285 ", is referenced at %L before the ENTRY statement "
4286 "in which it is a parameter",
4287 sym->name, &cs_base->current->loc);
4289 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4290 "statement in which it is a parameter",
4291 sym->name, &cs_base->current->loc);
4296 /* Now do the same check on the specification expressions. */
4297 specification_expr = 1;
4298 if (sym->ts.type == BT_CHARACTER
4299 && gfc_resolve_expr (sym->ts.cl->length) == FAILURE)
4303 for (n = 0; n < sym->as->rank; n++)
4305 specification_expr = 1;
4306 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4308 specification_expr = 1;
4309 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4312 specification_expr = 0;
4315 /* Update the symbol's entry level. */
4316 sym->entry_id = current_entry_id + 1;
4320 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
4327 /* Checks to see that the correct symbol has been host associated.
4328 The only situation where this arises is that in which a twice
4329 contained function is parsed after the host association is made.
4330 Therefore, on detecting this, change the symbol in the expression
4331 and convert the array reference into an actual arglist if the old
4332 symbol is a variable. */
4334 check_host_association (gfc_expr *e)
4336 gfc_symbol *sym, *old_sym;
4340 gfc_actual_arglist *arg, *tail;
4341 bool retval = e->expr_type == EXPR_FUNCTION;
4343 /* If the expression is the result of substitution in
4344 interface.c(gfc_extend_expr) because there is no way in
4345 which the host association can be wrong. */
4346 if (e->symtree == NULL
4347 || e->symtree->n.sym == NULL
4348 || e->user_operator)
4351 old_sym = e->symtree->n.sym;
4353 if (gfc_current_ns->parent
4354 && old_sym->ns != gfc_current_ns)
4356 /* Use the 'USE' name so that renamed module symbols are
4357 correctly handled. */
4358 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
4360 if (sym && old_sym != sym
4361 && sym->ts.type == old_sym->ts.type
4362 && sym->attr.flavor == FL_PROCEDURE
4363 && sym->attr.contained)
4365 /* Clear the shape, since it might not be valid. */
4366 if (e->shape != NULL)
4368 for (n = 0; n < e->rank; n++)
4369 mpz_clear (e->shape[n]);
4371 gfc_free (e->shape);
4374 /* Give the symbol a symtree in the right place! */
4375 gfc_get_sym_tree (sym->name, gfc_current_ns, &st);
4378 if (old_sym->attr.flavor == FL_PROCEDURE)
4380 /* Original was function so point to the new symbol, since
4381 the actual argument list is already attached to the
4383 e->value.function.esym = NULL;
4388 /* Original was variable so convert array references into
4389 an actual arglist. This does not need any checking now
4390 since gfc_resolve_function will take care of it. */
4391 e->value.function.actual = NULL;
4392 e->expr_type = EXPR_FUNCTION;
4395 /* Ambiguity will not arise if the array reference is not
4396 the last reference. */
4397 for (ref = e->ref; ref; ref = ref->next)
4398 if (ref->type == REF_ARRAY && ref->next == NULL)
4401 gcc_assert (ref->type == REF_ARRAY);
4403 /* Grab the start expressions from the array ref and
4404 copy them into actual arguments. */
4405 for (n = 0; n < ref->u.ar.dimen; n++)
4407 arg = gfc_get_actual_arglist ();
4408 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4409 if (e->value.function.actual == NULL)
4410 tail = e->value.function.actual = arg;
4418 /* Dump the reference list and set the rank. */
4419 gfc_free_ref_list (e->ref);
4421 e->rank = sym->as ? sym->as->rank : 0;
4424 gfc_resolve_expr (e);
4428 /* This might have changed! */
4429 return e->expr_type == EXPR_FUNCTION;
4434 gfc_resolve_character_operator (gfc_expr *e)
4436 gfc_expr *op1 = e->value.op.op1;
4437 gfc_expr *op2 = e->value.op.op2;
4438 gfc_expr *e1 = NULL;
4439 gfc_expr *e2 = NULL;
4441 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
4443 if (op1->ts.cl && op1->ts.cl->length)
4444 e1 = gfc_copy_expr (op1->ts.cl->length);
4445 else if (op1->expr_type == EXPR_CONSTANT)
4446 e1 = gfc_int_expr (op1->value.character.length);
4448 if (op2->ts.cl && op2->ts.cl->length)
4449 e2 = gfc_copy_expr (op2->ts.cl->length);
4450 else if (op2->expr_type == EXPR_CONSTANT)
4451 e2 = gfc_int_expr (op2->value.character.length);
4453 e->ts.cl = gfc_get_charlen ();
4454 e->ts.cl->next = gfc_current_ns->cl_list;
4455 gfc_current_ns->cl_list = e->ts.cl;
4460 e->ts.cl->length = gfc_add (e1, e2);
4461 e->ts.cl->length->ts.type = BT_INTEGER;
4462 e->ts.cl->length->ts.kind = gfc_charlen_int_kind;;
4463 gfc_simplify_expr (e->ts.cl->length, 0);
4464 gfc_resolve_expr (e->ts.cl->length);
4470 /* Ensure that an character expression has a charlen and, if possible, a
4471 length expression. */
4474 fixup_charlen (gfc_expr *e)
4476 /* The cases fall through so that changes in expression type and the need
4477 for multiple fixes are picked up. In all circumstances, a charlen should
4478 be available for the middle end to hang a backend_decl on. */
4479 switch (e->expr_type)
4482 gfc_resolve_character_operator (e);
4485 if (e->expr_type == EXPR_ARRAY)
4486 gfc_resolve_character_array_constructor (e);
4488 case EXPR_SUBSTRING:
4489 if (!e->ts.cl && e->ref)
4490 gfc_resolve_substring_charlen (e);
4495 e->ts.cl = gfc_get_charlen ();
4496 e->ts.cl->next = gfc_current_ns->cl_list;
4497 gfc_current_ns->cl_list = e->ts.cl;
4505 /* Update an actual argument to include the passed-object for type-bound
4506 procedures at the right position. */
4508 static gfc_actual_arglist*
4509 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos)
4511 gcc_assert (argpos > 0);
4515 gfc_actual_arglist* result;
4517 result = gfc_get_actual_arglist ();
4525 gcc_assert (argpos > 1);
4527 lst->next = update_arglist_pass (lst->next, po, argpos - 1);
4532 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
4535 extract_compcall_passed_object (gfc_expr* e)
4539 gcc_assert (e->expr_type == EXPR_COMPCALL);
4541 po = gfc_get_expr ();
4542 po->expr_type = EXPR_VARIABLE;
4543 po->symtree = e->symtree;
4544 po->ref = gfc_copy_ref (e->ref);
4546 if (gfc_resolve_expr (po) == FAILURE)
4553 /* Update the arglist of an EXPR_COMPCALL expression to include the
4557 update_compcall_arglist (gfc_expr* e)
4560 gfc_typebound_proc* tbp;
4562 tbp = e->value.compcall.tbp;
4567 po = extract_compcall_passed_object (e);
4573 gfc_error ("Passed-object at %L must be scalar", &e->where);
4583 gcc_assert (tbp->pass_arg_num > 0);
4584 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4591 /* Check that the object a TBP is called on is valid, i.e. it must not be
4592 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
4595 check_typebound_baseobject (gfc_expr* e)
4599 base = extract_compcall_passed_object (e);
4603 gcc_assert (base->ts.type == BT_DERIVED);
4604 if (base->ts.derived->attr.abstract)
4606 gfc_error ("Base object for type-bound procedure call at %L is of"
4607 " ABSTRACT type '%s'", &e->where, base->ts.derived->name);
4615 /* Resolve a call to a type-bound procedure, either function or subroutine,
4616 statically from the data in an EXPR_COMPCALL expression. The adapted
4617 arglist and the target-procedure symtree are returned. */
4620 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
4621 gfc_actual_arglist** actual)
4623 gcc_assert (e->expr_type == EXPR_COMPCALL);
4624 gcc_assert (!e->value.compcall.tbp->is_generic);
4626 /* Update the actual arglist for PASS. */
4627 if (update_compcall_arglist (e) == FAILURE)
4630 *actual = e->value.compcall.actual;
4631 *target = e->value.compcall.tbp->u.specific;
4633 gfc_free_ref_list (e->ref);
4635 e->value.compcall.actual = NULL;
4641 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
4642 which of the specific bindings (if any) matches the arglist and transform
4643 the expression into a call of that binding. */
4646 resolve_typebound_generic_call (gfc_expr* e)
4648 gfc_typebound_proc* genproc;
4649 const char* genname;
4651 gcc_assert (e->expr_type == EXPR_COMPCALL);
4652 genname = e->value.compcall.name;
4653 genproc = e->value.compcall.tbp;
4655 if (!genproc->is_generic)
4658 /* Try the bindings on this type and in the inheritance hierarchy. */
4659 for (; genproc; genproc = genproc->overridden)
4663 gcc_assert (genproc->is_generic);
4664 for (g = genproc->u.generic; g; g = g->next)
4667 gfc_actual_arglist* args;
4670 gcc_assert (g->specific);
4672 if (g->specific->error)
4675 target = g->specific->u.specific->n.sym;
4677 /* Get the right arglist by handling PASS/NOPASS. */
4678 args = gfc_copy_actual_arglist (e->value.compcall.actual);
4679 if (!g->specific->nopass)
4682 po = extract_compcall_passed_object (e);
4686 gcc_assert (g->specific->pass_arg_num > 0);
4687 gcc_assert (!g->specific->error);
4688 args = update_arglist_pass (args, po, g->specific->pass_arg_num);
4690 resolve_actual_arglist (args, target->attr.proc,
4691 is_external_proc (target) && !target->formal);
4693 /* Check if this arglist matches the formal. */
4694 matches = gfc_arglist_matches_symbol (&args, target);
4696 /* Clean up and break out of the loop if we've found it. */
4697 gfc_free_actual_arglist (args);
4700 e->value.compcall.tbp = g->specific;
4706 /* Nothing matching found! */
4707 gfc_error ("Found no matching specific binding for the call to the GENERIC"
4708 " '%s' at %L", genname, &e->where);
4716 /* Resolve a call to a type-bound subroutine. */
4719 resolve_typebound_call (gfc_code* c)
4721 gfc_actual_arglist* newactual;
4722 gfc_symtree* target;
4724 /* Check that's really a SUBROUTINE. */
4725 if (!c->expr->value.compcall.tbp->subroutine)
4727 gfc_error ("'%s' at %L should be a SUBROUTINE",
4728 c->expr->value.compcall.name, &c->loc);
4732 if (check_typebound_baseobject (c->expr) == FAILURE)
4735 if (resolve_typebound_generic_call (c->expr) == FAILURE)
4738 /* Transform into an ordinary EXEC_CALL for now. */
4740 if (resolve_typebound_static (c->expr, &target, &newactual) == FAILURE)
4743 c->ext.actual = newactual;
4744 c->symtree = target;
4747 gcc_assert (!c->expr->ref && !c->expr->value.compcall.actual);
4748 gfc_free_expr (c->expr);
4751 return resolve_call (c);
4755 /* Resolve a component-call expression. */
4758 resolve_compcall (gfc_expr* e)
4760 gfc_actual_arglist* newactual;
4761 gfc_symtree* target;
4763 /* Check that's really a FUNCTION. */
4764 if (!e->value.compcall.tbp->function)
4766 gfc_error ("'%s' at %L should be a FUNCTION",
4767 e->value.compcall.name, &e->where);
4771 if (check_typebound_baseobject (e) == FAILURE)
4774 if (resolve_typebound_generic_call (e) == FAILURE)
4776 gcc_assert (!e->value.compcall.tbp->is_generic);
4778 /* Take the rank from the function's symbol. */
4779 if (e->value.compcall.tbp->u.specific->n.sym->as)
4780 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
4782 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
4783 arglist to the TBP's binding target. */
4785 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
4788 e->value.function.actual = newactual;
4789 e->value.function.name = e->value.compcall.name;
4790 e->value.function.isym = NULL;
4791 e->value.function.esym = NULL;
4792 e->symtree = target;
4793 e->ts = target->n.sym->ts;
4794 e->expr_type = EXPR_FUNCTION;
4796 return gfc_resolve_expr (e);
4800 /* Resolve an expression. That is, make sure that types of operands agree
4801 with their operators, intrinsic operators are converted to function calls
4802 for overloaded types and unresolved function references are resolved. */
4805 gfc_resolve_expr (gfc_expr *e)
4812 switch (e->expr_type)
4815 t = resolve_operator (e);
4821 if (check_host_association (e))
4822 t = resolve_function (e);
4825 t = resolve_variable (e);
4827 expression_rank (e);
4830 if (e->ts.type == BT_CHARACTER && e->ts.cl == NULL && e->ref
4831 && e->ref->type != REF_SUBSTRING)
4832 gfc_resolve_substring_charlen (e);
4837 t = resolve_compcall (e);
4840 case EXPR_SUBSTRING:
4841 t = resolve_ref (e);
4851 if (resolve_ref (e) == FAILURE)
4854 t = gfc_resolve_array_constructor (e);
4855 /* Also try to expand a constructor. */
4858 expression_rank (e);
4859 gfc_expand_constructor (e);
4862 /* This provides the opportunity for the length of constructors with
4863 character valued function elements to propagate the string length
4864 to the expression. */
4865 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
4866 t = gfc_resolve_character_array_constructor (e);
4870 case EXPR_STRUCTURE:
4871 t = resolve_ref (e);
4875 t = resolve_structure_cons (e);
4879 t = gfc_simplify_expr (e, 0);
4883 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
4886 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.cl)
4893 /* Resolve an expression from an iterator. They must be scalar and have
4894 INTEGER or (optionally) REAL type. */
4897 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
4898 const char *name_msgid)
4900 if (gfc_resolve_expr (expr) == FAILURE)
4903 if (expr->rank != 0)
4905 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
4909 if (expr->ts.type != BT_INTEGER)
4911 if (expr->ts.type == BT_REAL)
4914 return gfc_notify_std (GFC_STD_F95_DEL,
4915 "Deleted feature: %s at %L must be integer",
4916 _(name_msgid), &expr->where);
4919 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
4926 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
4934 /* Resolve the expressions in an iterator structure. If REAL_OK is
4935 false allow only INTEGER type iterators, otherwise allow REAL types. */
4938 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
4940 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
4944 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
4946 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
4951 if (gfc_resolve_iterator_expr (iter->start, real_ok,
4952 "Start expression in DO loop") == FAILURE)
4955 if (gfc_resolve_iterator_expr (iter->end, real_ok,
4956 "End expression in DO loop") == FAILURE)
4959 if (gfc_resolve_iterator_expr (iter->step, real_ok,
4960 "Step expression in DO loop") == FAILURE)
4963 if (iter->step->expr_type == EXPR_CONSTANT)
4965 if ((iter->step->ts.type == BT_INTEGER
4966 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
4967 || (iter->step->ts.type == BT_REAL
4968 && mpfr_sgn (iter->step->value.real) == 0))
4970 gfc_error ("Step expression in DO loop at %L cannot be zero",
4971 &iter->step->where);
4976 /* Convert start, end, and step to the same type as var. */
4977 if (iter->start->ts.kind != iter->var->ts.kind
4978 || iter->start->ts.type != iter->var->ts.type)
4979 gfc_convert_type (iter->start, &iter->var->ts, 2);
4981 if (iter->end->ts.kind != iter->var->ts.kind
4982 || iter->end->ts.type != iter->var->ts.type)
4983 gfc_convert_type (iter->end, &iter->var->ts, 2);
4985 if (iter->step->ts.kind != iter->var->ts.kind
4986 || iter->step->ts.type != iter->var->ts.type)
4987 gfc_convert_type (iter->step, &iter->var->ts, 2);
4989 if (iter->start->expr_type == EXPR_CONSTANT
4990 && iter->end->expr_type == EXPR_CONSTANT
4991 && iter->step->expr_type == EXPR_CONSTANT)
4994 if (iter->start->ts.type == BT_INTEGER)
4996 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
4997 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
5001 sgn = mpfr_sgn (iter->step->value.real);
5002 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
5004 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
5005 gfc_warning ("DO loop at %L will be executed zero times",
5006 &iter->step->where);
5013 /* Traversal function for find_forall_index. f == 2 signals that
5014 that variable itself is not to be checked - only the references. */
5017 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
5019 if (expr->expr_type != EXPR_VARIABLE)
5022 /* A scalar assignment */
5023 if (!expr->ref || *f == 1)
5025 if (expr->symtree->n.sym == sym)
5037 /* Check whether the FORALL index appears in the expression or not.
5038 Returns SUCCESS if SYM is found in EXPR. */
5041 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
5043 if (gfc_traverse_expr (expr, sym, forall_index, f))
5050 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5051 to be a scalar INTEGER variable. The subscripts and stride are scalar
5052 INTEGERs, and if stride is a constant it must be nonzero.
5053 Furthermore "A subscript or stride in a forall-triplet-spec shall
5054 not contain a reference to any index-name in the
5055 forall-triplet-spec-list in which it appears." (7.5.4.1) */
5058 resolve_forall_iterators (gfc_forall_iterator *it)
5060 gfc_forall_iterator *iter, *iter2;
5062 for (iter = it; iter; iter = iter->next)
5064 if (gfc_resolve_expr (iter->var) == SUCCESS
5065 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
5066 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
5069 if (gfc_resolve_expr (iter->start) == SUCCESS
5070 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
5071 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
5072 &iter->start->where);
5073 if (iter->var->ts.kind != iter->start->ts.kind)
5074 gfc_convert_type (iter->start, &iter->var->ts, 2);
5076 if (gfc_resolve_expr (iter->end) == SUCCESS
5077 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
5078 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
5080 if (iter->var->ts.kind != iter->end->ts.kind)
5081 gfc_convert_type (iter->end, &iter->var->ts, 2);
5083 if (gfc_resolve_expr (iter->stride) == SUCCESS)
5085 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
5086 gfc_error ("FORALL stride expression at %L must be a scalar %s",
5087 &iter->stride->where, "INTEGER");
5089 if (iter->stride->expr_type == EXPR_CONSTANT
5090 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
5091 gfc_error ("FORALL stride expression at %L cannot be zero",
5092 &iter->stride->where);
5094 if (iter->var->ts.kind != iter->stride->ts.kind)
5095 gfc_convert_type (iter->stride, &iter->var->ts, 2);
5098 for (iter = it; iter; iter = iter->next)
5099 for (iter2 = iter; iter2; iter2 = iter2->next)
5101 if (find_forall_index (iter2->start,
5102 iter->var->symtree->n.sym, 0) == SUCCESS
5103 || find_forall_index (iter2->end,
5104 iter->var->symtree->n.sym, 0) == SUCCESS
5105 || find_forall_index (iter2->stride,
5106 iter->var->symtree->n.sym, 0) == SUCCESS)
5107 gfc_error ("FORALL index '%s' may not appear in triplet "
5108 "specification at %L", iter->var->symtree->name,
5109 &iter2->start->where);
5114 /* Given a pointer to a symbol that is a derived type, see if it's
5115 inaccessible, i.e. if it's defined in another module and the components are
5116 PRIVATE. The search is recursive if necessary. Returns zero if no
5117 inaccessible components are found, nonzero otherwise. */
5120 derived_inaccessible (gfc_symbol *sym)
5124 if (sym->attr.use_assoc && sym->attr.private_comp)
5127 for (c = sym->components; c; c = c->next)
5129 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.derived))
5137 /* Resolve the argument of a deallocate expression. The expression must be
5138 a pointer or a full array. */
5141 resolve_deallocate_expr (gfc_expr *e)
5143 symbol_attribute attr;
5144 int allocatable, pointer, check_intent_in;
5147 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5148 check_intent_in = 1;
5150 if (gfc_resolve_expr (e) == FAILURE)
5153 if (e->expr_type != EXPR_VARIABLE)
5156 allocatable = e->symtree->n.sym->attr.allocatable;
5157 pointer = e->symtree->n.sym->attr.pointer;
5158 for (ref = e->ref; ref; ref = ref->next)
5161 check_intent_in = 0;
5166 if (ref->u.ar.type != AR_FULL)
5171 allocatable = (ref->u.c.component->as != NULL
5172 && ref->u.c.component->as->type == AS_DEFERRED);
5173 pointer = ref->u.c.component->attr.pointer;
5182 attr = gfc_expr_attr (e);
5184 if (allocatable == 0 && attr.pointer == 0)
5187 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5192 && e->symtree->n.sym->attr.intent == INTENT_IN)
5194 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
5195 e->symtree->n.sym->name, &e->where);
5203 /* Returns true if the expression e contains a reference to the symbol sym. */
5205 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5207 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
5214 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
5216 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
5220 /* Given the expression node e for an allocatable/pointer of derived type to be
5221 allocated, get the expression node to be initialized afterwards (needed for
5222 derived types with default initializers, and derived types with allocatable
5223 components that need nullification.) */
5226 expr_to_initialize (gfc_expr *e)
5232 result = gfc_copy_expr (e);
5234 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
5235 for (ref = result->ref; ref; ref = ref->next)
5236 if (ref->type == REF_ARRAY && ref->next == NULL)
5238 ref->u.ar.type = AR_FULL;
5240 for (i = 0; i < ref->u.ar.dimen; i++)
5241 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
5243 result->rank = ref->u.ar.dimen;
5251 /* Resolve the expression in an ALLOCATE statement, doing the additional
5252 checks to see whether the expression is OK or not. The expression must
5253 have a trailing array reference that gives the size of the array. */
5256 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
5258 int i, pointer, allocatable, dimension, check_intent_in;
5259 symbol_attribute attr;
5260 gfc_ref *ref, *ref2;
5267 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5268 check_intent_in = 1;
5270 if (gfc_resolve_expr (e) == FAILURE)
5273 /* Make sure the expression is allocatable or a pointer. If it is
5274 pointer, the next-to-last reference must be a pointer. */
5278 if (e->expr_type != EXPR_VARIABLE)
5281 attr = gfc_expr_attr (e);
5282 pointer = attr.pointer;
5283 dimension = attr.dimension;
5287 allocatable = e->symtree->n.sym->attr.allocatable;
5288 pointer = e->symtree->n.sym->attr.pointer;
5289 dimension = e->symtree->n.sym->attr.dimension;
5291 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
5294 check_intent_in = 0;
5299 if (ref->next != NULL)
5304 allocatable = (ref->u.c.component->as != NULL
5305 && ref->u.c.component->as->type == AS_DEFERRED);
5307 pointer = ref->u.c.component->attr.pointer;
5308 dimension = ref->u.c.component->attr.dimension;
5319 if (allocatable == 0 && pointer == 0)
5321 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5327 && e->symtree->n.sym->attr.intent == INTENT_IN)
5329 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
5330 e->symtree->n.sym->name, &e->where);
5334 /* Add default initializer for those derived types that need them. */
5335 if (e->ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&e->ts)))
5337 init_st = gfc_get_code ();
5338 init_st->loc = code->loc;
5339 init_st->op = EXEC_INIT_ASSIGN;
5340 init_st->expr = expr_to_initialize (e);
5341 init_st->expr2 = init_e;
5342 init_st->next = code->next;
5343 code->next = init_st;
5346 if (pointer && dimension == 0)
5349 /* Make sure the next-to-last reference node is an array specification. */
5351 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
5353 gfc_error ("Array specification required in ALLOCATE statement "
5354 "at %L", &e->where);
5358 /* Make sure that the array section reference makes sense in the
5359 context of an ALLOCATE specification. */
5363 for (i = 0; i < ar->dimen; i++)
5365 if (ref2->u.ar.type == AR_ELEMENT)
5368 switch (ar->dimen_type[i])
5374 if (ar->start[i] != NULL
5375 && ar->end[i] != NULL
5376 && ar->stride[i] == NULL)
5379 /* Fall Through... */
5383 gfc_error ("Bad array specification in ALLOCATE statement at %L",
5390 for (a = code->ext.alloc_list; a; a = a->next)
5392 sym = a->expr->symtree->n.sym;
5394 /* TODO - check derived type components. */
5395 if (sym->ts.type == BT_DERIVED)
5398 if ((ar->start[i] != NULL
5399 && gfc_find_sym_in_expr (sym, ar->start[i]))
5400 || (ar->end[i] != NULL
5401 && gfc_find_sym_in_expr (sym, ar->end[i])))
5403 gfc_error ("'%s' must not appear in the array specification at "
5404 "%L in the same ALLOCATE statement where it is "
5405 "itself allocated", sym->name, &ar->where);
5415 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
5417 gfc_expr *stat, *errmsg, *pe, *qe;
5418 gfc_alloc *a, *p, *q;
5420 stat = code->expr ? code->expr : NULL;
5422 errmsg = code->expr2 ? code->expr2 : NULL;
5424 /* Check the stat variable. */
5427 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
5428 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
5429 stat->symtree->n.sym->name, &stat->where);
5431 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
5432 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
5435 if (stat->ts.type != BT_INTEGER
5436 && !(stat->ref && (stat->ref->type == REF_ARRAY
5437 || stat->ref->type == REF_COMPONENT)))
5438 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
5439 "variable", &stat->where);
5441 for (p = code->ext.alloc_list; p; p = p->next)
5442 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
5443 gfc_error ("Stat-variable at %L shall not be %sd within "
5444 "the same %s statement", &stat->where, fcn, fcn);
5447 /* Check the errmsg variable. */
5451 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
5454 if (errmsg->symtree->n.sym->attr.intent == INTENT_IN)
5455 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
5456 errmsg->symtree->n.sym->name, &errmsg->where);
5458 if (gfc_pure (NULL) && gfc_impure_variable (errmsg->symtree->n.sym))
5459 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
5462 if (errmsg->ts.type != BT_CHARACTER
5464 && (errmsg->ref->type == REF_ARRAY
5465 || errmsg->ref->type == REF_COMPONENT)))
5466 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
5467 "variable", &errmsg->where);
5469 for (p = code->ext.alloc_list; p; p = p->next)
5470 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
5471 gfc_error ("Errmsg-variable at %L shall not be %sd within "
5472 "the same %s statement", &errmsg->where, fcn, fcn);
5475 /* Check that an allocate-object appears only once in the statement.
5476 FIXME: Checking derived types is disabled. */
5477 for (p = code->ext.alloc_list; p; p = p->next)
5480 if ((pe->ref && pe->ref->type != REF_COMPONENT)
5481 && (pe->symtree->n.sym->ts.type != BT_DERIVED))
5483 for (q = p->next; q; q = q->next)
5486 if ((qe->ref && qe->ref->type != REF_COMPONENT)
5487 && (qe->symtree->n.sym->ts.type != BT_DERIVED)
5488 && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
5489 gfc_error ("Allocate-object at %L also appears at %L",
5490 &pe->where, &qe->where);
5495 if (strcmp (fcn, "ALLOCATE") == 0)
5497 for (a = code->ext.alloc_list; a; a = a->next)
5498 resolve_allocate_expr (a->expr, code);
5502 for (a = code->ext.alloc_list; a; a = a->next)
5503 resolve_deallocate_expr (a->expr);
5508 /************ SELECT CASE resolution subroutines ************/
5510 /* Callback function for our mergesort variant. Determines interval
5511 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
5512 op1 > op2. Assumes we're not dealing with the default case.
5513 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
5514 There are nine situations to check. */
5517 compare_cases (const gfc_case *op1, const gfc_case *op2)
5521 if (op1->low == NULL) /* op1 = (:L) */
5523 /* op2 = (:N), so overlap. */
5525 /* op2 = (M:) or (M:N), L < M */
5526 if (op2->low != NULL
5527 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
5530 else if (op1->high == NULL) /* op1 = (K:) */
5532 /* op2 = (M:), so overlap. */
5534 /* op2 = (:N) or (M:N), K > N */
5535 if (op2->high != NULL
5536 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
5539 else /* op1 = (K:L) */
5541 if (op2->low == NULL) /* op2 = (:N), K > N */
5542 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
5544 else if (op2->high == NULL) /* op2 = (M:), L < M */
5545 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
5547 else /* op2 = (M:N) */
5551 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
5554 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
5563 /* Merge-sort a double linked case list, detecting overlap in the
5564 process. LIST is the head of the double linked case list before it
5565 is sorted. Returns the head of the sorted list if we don't see any
5566 overlap, or NULL otherwise. */
5569 check_case_overlap (gfc_case *list)
5571 gfc_case *p, *q, *e, *tail;
5572 int insize, nmerges, psize, qsize, cmp, overlap_seen;
5574 /* If the passed list was empty, return immediately. */
5581 /* Loop unconditionally. The only exit from this loop is a return
5582 statement, when we've finished sorting the case list. */
5589 /* Count the number of merges we do in this pass. */
5592 /* Loop while there exists a merge to be done. */
5597 /* Count this merge. */
5600 /* Cut the list in two pieces by stepping INSIZE places
5601 forward in the list, starting from P. */
5604 for (i = 0; i < insize; i++)
5613 /* Now we have two lists. Merge them! */
5614 while (psize > 0 || (qsize > 0 && q != NULL))
5616 /* See from which the next case to merge comes from. */
5619 /* P is empty so the next case must come from Q. */
5624 else if (qsize == 0 || q == NULL)
5633 cmp = compare_cases (p, q);
5636 /* The whole case range for P is less than the
5644 /* The whole case range for Q is greater than
5645 the case range for P. */
5652 /* The cases overlap, or they are the same
5653 element in the list. Either way, we must
5654 issue an error and get the next case from P. */
5655 /* FIXME: Sort P and Q by line number. */
5656 gfc_error ("CASE label at %L overlaps with CASE "
5657 "label at %L", &p->where, &q->where);
5665 /* Add the next element to the merged list. */
5674 /* P has now stepped INSIZE places along, and so has Q. So
5675 they're the same. */
5680 /* If we have done only one merge or none at all, we've
5681 finished sorting the cases. */
5690 /* Otherwise repeat, merging lists twice the size. */
5696 /* Check to see if an expression is suitable for use in a CASE statement.
5697 Makes sure that all case expressions are scalar constants of the same
5698 type. Return FAILURE if anything is wrong. */
5701 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
5703 if (e == NULL) return SUCCESS;
5705 if (e->ts.type != case_expr->ts.type)
5707 gfc_error ("Expression in CASE statement at %L must be of type %s",
5708 &e->where, gfc_basic_typename (case_expr->ts.type));
5712 /* C805 (R808) For a given case-construct, each case-value shall be of
5713 the same type as case-expr. For character type, length differences
5714 are allowed, but the kind type parameters shall be the same. */
5716 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
5718 gfc_error ("Expression in CASE statement at %L must be of kind %d",
5719 &e->where, case_expr->ts.kind);
5723 /* Convert the case value kind to that of case expression kind, if needed.
5724 FIXME: Should a warning be issued? */
5725 if (e->ts.kind != case_expr->ts.kind)
5726 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
5730 gfc_error ("Expression in CASE statement at %L must be scalar",
5739 /* Given a completely parsed select statement, we:
5741 - Validate all expressions and code within the SELECT.
5742 - Make sure that the selection expression is not of the wrong type.
5743 - Make sure that no case ranges overlap.
5744 - Eliminate unreachable cases and unreachable code resulting from
5745 removing case labels.
5747 The standard does allow unreachable cases, e.g. CASE (5:3). But
5748 they are a hassle for code generation, and to prevent that, we just
5749 cut them out here. This is not necessary for overlapping cases
5750 because they are illegal and we never even try to generate code.
5752 We have the additional caveat that a SELECT construct could have
5753 been a computed GOTO in the source code. Fortunately we can fairly
5754 easily work around that here: The case_expr for a "real" SELECT CASE
5755 is in code->expr1, but for a computed GOTO it is in code->expr2. All
5756 we have to do is make sure that the case_expr is a scalar integer
5760 resolve_select (gfc_code *code)
5763 gfc_expr *case_expr;
5764 gfc_case *cp, *default_case, *tail, *head;
5765 int seen_unreachable;
5771 if (code->expr == NULL)
5773 /* This was actually a computed GOTO statement. */
5774 case_expr = code->expr2;
5775 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
5776 gfc_error ("Selection expression in computed GOTO statement "
5777 "at %L must be a scalar integer expression",
5780 /* Further checking is not necessary because this SELECT was built
5781 by the compiler, so it should always be OK. Just move the
5782 case_expr from expr2 to expr so that we can handle computed
5783 GOTOs as normal SELECTs from here on. */
5784 code->expr = code->expr2;
5789 case_expr = code->expr;
5791 type = case_expr->ts.type;
5792 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
5794 gfc_error ("Argument of SELECT statement at %L cannot be %s",
5795 &case_expr->where, gfc_typename (&case_expr->ts));
5797 /* Punt. Going on here just produce more garbage error messages. */
5801 if (case_expr->rank != 0)
5803 gfc_error ("Argument of SELECT statement at %L must be a scalar "
5804 "expression", &case_expr->where);
5810 /* PR 19168 has a long discussion concerning a mismatch of the kinds
5811 of the SELECT CASE expression and its CASE values. Walk the lists
5812 of case values, and if we find a mismatch, promote case_expr to
5813 the appropriate kind. */
5815 if (type == BT_LOGICAL || type == BT_INTEGER)
5817 for (body = code->block; body; body = body->block)
5819 /* Walk the case label list. */
5820 for (cp = body->ext.case_list; cp; cp = cp->next)
5822 /* Intercept the DEFAULT case. It does not have a kind. */
5823 if (cp->low == NULL && cp->high == NULL)
5826 /* Unreachable case ranges are discarded, so ignore. */
5827 if (cp->low != NULL && cp->high != NULL
5828 && cp->low != cp->high
5829 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5832 /* FIXME: Should a warning be issued? */
5834 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
5835 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
5837 if (cp->high != NULL
5838 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
5839 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
5844 /* Assume there is no DEFAULT case. */
5845 default_case = NULL;
5850 for (body = code->block; body; body = body->block)
5852 /* Assume the CASE list is OK, and all CASE labels can be matched. */
5854 seen_unreachable = 0;
5856 /* Walk the case label list, making sure that all case labels
5858 for (cp = body->ext.case_list; cp; cp = cp->next)
5860 /* Count the number of cases in the whole construct. */
5863 /* Intercept the DEFAULT case. */
5864 if (cp->low == NULL && cp->high == NULL)
5866 if (default_case != NULL)
5868 gfc_error ("The DEFAULT CASE at %L cannot be followed "
5869 "by a second DEFAULT CASE at %L",
5870 &default_case->where, &cp->where);
5881 /* Deal with single value cases and case ranges. Errors are
5882 issued from the validation function. */
5883 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
5884 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
5890 if (type == BT_LOGICAL
5891 && ((cp->low == NULL || cp->high == NULL)
5892 || cp->low != cp->high))
5894 gfc_error ("Logical range in CASE statement at %L is not "
5895 "allowed", &cp->low->where);
5900 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
5903 value = cp->low->value.logical == 0 ? 2 : 1;
5904 if (value & seen_logical)
5906 gfc_error ("constant logical value in CASE statement "
5907 "is repeated at %L",
5912 seen_logical |= value;
5915 if (cp->low != NULL && cp->high != NULL
5916 && cp->low != cp->high
5917 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5919 if (gfc_option.warn_surprising)
5920 gfc_warning ("Range specification at %L can never "
5921 "be matched", &cp->where);
5923 cp->unreachable = 1;
5924 seen_unreachable = 1;
5928 /* If the case range can be matched, it can also overlap with
5929 other cases. To make sure it does not, we put it in a
5930 double linked list here. We sort that with a merge sort
5931 later on to detect any overlapping cases. */
5935 head->right = head->left = NULL;
5940 tail->right->left = tail;
5947 /* It there was a failure in the previous case label, give up
5948 for this case label list. Continue with the next block. */
5952 /* See if any case labels that are unreachable have been seen.
5953 If so, we eliminate them. This is a bit of a kludge because
5954 the case lists for a single case statement (label) is a
5955 single forward linked lists. */
5956 if (seen_unreachable)
5958 /* Advance until the first case in the list is reachable. */
5959 while (body->ext.case_list != NULL
5960 && body->ext.case_list->unreachable)
5962 gfc_case *n = body->ext.case_list;
5963 body->ext.case_list = body->ext.case_list->next;
5965 gfc_free_case_list (n);
5968 /* Strip all other unreachable cases. */
5969 if (body->ext.case_list)
5971 for (cp = body->ext.case_list; cp->next; cp = cp->next)
5973 if (cp->next->unreachable)
5975 gfc_case *n = cp->next;
5976 cp->next = cp->next->next;
5978 gfc_free_case_list (n);
5985 /* See if there were overlapping cases. If the check returns NULL,
5986 there was overlap. In that case we don't do anything. If head
5987 is non-NULL, we prepend the DEFAULT case. The sorted list can
5988 then used during code generation for SELECT CASE constructs with
5989 a case expression of a CHARACTER type. */
5992 head = check_case_overlap (head);
5994 /* Prepend the default_case if it is there. */
5995 if (head != NULL && default_case)
5997 default_case->left = NULL;
5998 default_case->right = head;
5999 head->left = default_case;
6003 /* Eliminate dead blocks that may be the result if we've seen
6004 unreachable case labels for a block. */
6005 for (body = code; body && body->block; body = body->block)
6007 if (body->block->ext.case_list == NULL)
6009 /* Cut the unreachable block from the code chain. */
6010 gfc_code *c = body->block;
6011 body->block = c->block;
6013 /* Kill the dead block, but not the blocks below it. */
6015 gfc_free_statements (c);
6019 /* More than two cases is legal but insane for logical selects.
6020 Issue a warning for it. */
6021 if (gfc_option.warn_surprising && type == BT_LOGICAL
6023 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
6028 /* Resolve a transfer statement. This is making sure that:
6029 -- a derived type being transferred has only non-pointer components
6030 -- a derived type being transferred doesn't have private components, unless
6031 it's being transferred from the module where the type was defined
6032 -- we're not trying to transfer a whole assumed size array. */
6035 resolve_transfer (gfc_code *code)
6044 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
6047 sym = exp->symtree->n.sym;
6050 /* Go to actual component transferred. */
6051 for (ref = code->expr->ref; ref; ref = ref->next)
6052 if (ref->type == REF_COMPONENT)
6053 ts = &ref->u.c.component->ts;
6055 if (ts->type == BT_DERIVED)
6057 /* Check that transferred derived type doesn't contain POINTER
6059 if (ts->derived->attr.pointer_comp)
6061 gfc_error ("Data transfer element at %L cannot have "
6062 "POINTER components", &code->loc);
6066 if (ts->derived->attr.alloc_comp)
6068 gfc_error ("Data transfer element at %L cannot have "
6069 "ALLOCATABLE components", &code->loc);
6073 if (derived_inaccessible (ts->derived))
6075 gfc_error ("Data transfer element at %L cannot have "
6076 "PRIVATE components",&code->loc);
6081 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
6082 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
6084 gfc_error ("Data transfer element at %L cannot be a full reference to "
6085 "an assumed-size array", &code->loc);
6091 /*********** Toplevel code resolution subroutines ***********/
6093 /* Find the set of labels that are reachable from this block. We also
6094 record the last statement in each block. */
6097 find_reachable_labels (gfc_code *block)
6104 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
6106 /* Collect labels in this block. We don't keep those corresponding
6107 to END {IF|SELECT}, these are checked in resolve_branch by going
6108 up through the code_stack. */
6109 for (c = block; c; c = c->next)
6111 if (c->here && c->op != EXEC_END_BLOCK)
6112 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
6115 /* Merge with labels from parent block. */
6118 gcc_assert (cs_base->prev->reachable_labels);
6119 bitmap_ior_into (cs_base->reachable_labels,
6120 cs_base->prev->reachable_labels);
6124 /* Given a branch to a label, see if the branch is conforming.
6125 The code node describes where the branch is located. */
6128 resolve_branch (gfc_st_label *label, gfc_code *code)
6135 /* Step one: is this a valid branching target? */
6137 if (label->defined == ST_LABEL_UNKNOWN)
6139 gfc_error ("Label %d referenced at %L is never defined", label->value,
6144 if (label->defined != ST_LABEL_TARGET)
6146 gfc_error ("Statement at %L is not a valid branch target statement "
6147 "for the branch statement at %L", &label->where, &code->loc);
6151 /* Step two: make sure this branch is not a branch to itself ;-) */
6153 if (code->here == label)
6155 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
6159 /* Step three: See if the label is in the same block as the
6160 branching statement. The hard work has been done by setting up
6161 the bitmap reachable_labels. */
6163 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
6166 /* Step four: If we haven't found the label in the bitmap, it may
6167 still be the label of the END of the enclosing block, in which
6168 case we find it by going up the code_stack. */
6170 for (stack = cs_base; stack; stack = stack->prev)
6171 if (stack->current->next && stack->current->next->here == label)
6176 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
6180 /* The label is not in an enclosing block, so illegal. This was
6181 allowed in Fortran 66, so we allow it as extension. No
6182 further checks are necessary in this case. */
6183 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
6184 "as the GOTO statement at %L", &label->where,
6190 /* Check whether EXPR1 has the same shape as EXPR2. */
6193 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
6195 mpz_t shape[GFC_MAX_DIMENSIONS];
6196 mpz_t shape2[GFC_MAX_DIMENSIONS];
6197 gfc_try result = FAILURE;
6200 /* Compare the rank. */
6201 if (expr1->rank != expr2->rank)
6204 /* Compare the size of each dimension. */
6205 for (i=0; i<expr1->rank; i++)
6207 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
6210 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
6213 if (mpz_cmp (shape[i], shape2[i]))
6217 /* When either of the two expression is an assumed size array, we
6218 ignore the comparison of dimension sizes. */
6223 for (i--; i >= 0; i--)
6225 mpz_clear (shape[i]);
6226 mpz_clear (shape2[i]);
6232 /* Check whether a WHERE assignment target or a WHERE mask expression
6233 has the same shape as the outmost WHERE mask expression. */
6236 resolve_where (gfc_code *code, gfc_expr *mask)
6242 cblock = code->block;
6244 /* Store the first WHERE mask-expr of the WHERE statement or construct.
6245 In case of nested WHERE, only the outmost one is stored. */
6246 if (mask == NULL) /* outmost WHERE */
6248 else /* inner WHERE */
6255 /* Check if the mask-expr has a consistent shape with the
6256 outmost WHERE mask-expr. */
6257 if (resolve_where_shape (cblock->expr, e) == FAILURE)
6258 gfc_error ("WHERE mask at %L has inconsistent shape",
6259 &cblock->expr->where);
6262 /* the assignment statement of a WHERE statement, or the first
6263 statement in where-body-construct of a WHERE construct */
6264 cnext = cblock->next;
6269 /* WHERE assignment statement */
6272 /* Check shape consistent for WHERE assignment target. */
6273 if (e && resolve_where_shape (cnext->expr, e) == FAILURE)
6274 gfc_error ("WHERE assignment target at %L has "
6275 "inconsistent shape", &cnext->expr->where);
6279 case EXEC_ASSIGN_CALL:
6280 resolve_call (cnext);
6281 if (!cnext->resolved_sym->attr.elemental)
6282 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
6283 &cnext->ext.actual->expr->where);
6286 /* WHERE or WHERE construct is part of a where-body-construct */
6288 resolve_where (cnext, e);
6292 gfc_error ("Unsupported statement inside WHERE at %L",
6295 /* the next statement within the same where-body-construct */
6296 cnext = cnext->next;
6298 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
6299 cblock = cblock->block;
6304 /* Resolve assignment in FORALL construct.
6305 NVAR is the number of FORALL index variables, and VAR_EXPR records the
6306 FORALL index variables. */
6309 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
6313 for (n = 0; n < nvar; n++)
6315 gfc_symbol *forall_index;
6317 forall_index = var_expr[n]->symtree->n.sym;
6319 /* Check whether the assignment target is one of the FORALL index
6321 if ((code->expr->expr_type == EXPR_VARIABLE)
6322 && (code->expr->symtree->n.sym == forall_index))
6323 gfc_error ("Assignment to a FORALL index variable at %L",
6324 &code->expr->where);
6327 /* If one of the FORALL index variables doesn't appear in the
6328 assignment variable, then there could be a many-to-one
6329 assignment. Emit a warning rather than an error because the
6330 mask could be resolving this problem. */
6331 if (find_forall_index (code->expr, forall_index, 0) == FAILURE)
6332 gfc_warning ("The FORALL with index '%s' is not used on the "
6333 "left side of the assignment at %L and so might "
6334 "cause multiple assignment to this object",
6335 var_expr[n]->symtree->name, &code->expr->where);
6341 /* Resolve WHERE statement in FORALL construct. */
6344 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
6345 gfc_expr **var_expr)
6350 cblock = code->block;
6353 /* the assignment statement of a WHERE statement, or the first
6354 statement in where-body-construct of a WHERE construct */
6355 cnext = cblock->next;
6360 /* WHERE assignment statement */
6362 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
6365 /* WHERE operator assignment statement */
6366 case EXEC_ASSIGN_CALL:
6367 resolve_call (cnext);
6368 if (!cnext->resolved_sym->attr.elemental)
6369 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
6370 &cnext->ext.actual->expr->where);
6373 /* WHERE or WHERE construct is part of a where-body-construct */
6375 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
6379 gfc_error ("Unsupported statement inside WHERE at %L",
6382 /* the next statement within the same where-body-construct */
6383 cnext = cnext->next;
6385 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
6386 cblock = cblock->block;
6391 /* Traverse the FORALL body to check whether the following errors exist:
6392 1. For assignment, check if a many-to-one assignment happens.
6393 2. For WHERE statement, check the WHERE body to see if there is any
6394 many-to-one assignment. */
6397 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
6401 c = code->block->next;
6407 case EXEC_POINTER_ASSIGN:
6408 gfc_resolve_assign_in_forall (c, nvar, var_expr);
6411 case EXEC_ASSIGN_CALL:
6415 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
6416 there is no need to handle it here. */
6420 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
6425 /* The next statement in the FORALL body. */
6431 /* Counts the number of iterators needed inside a forall construct, including
6432 nested forall constructs. This is used to allocate the needed memory
6433 in gfc_resolve_forall. */
6436 gfc_count_forall_iterators (gfc_code *code)
6438 int max_iters, sub_iters, current_iters;
6439 gfc_forall_iterator *fa;
6441 gcc_assert(code->op == EXEC_FORALL);
6445 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
6448 code = code->block->next;
6452 if (code->op == EXEC_FORALL)
6454 sub_iters = gfc_count_forall_iterators (code);
6455 if (sub_iters > max_iters)
6456 max_iters = sub_iters;
6461 return current_iters + max_iters;
6465 /* Given a FORALL construct, first resolve the FORALL iterator, then call
6466 gfc_resolve_forall_body to resolve the FORALL body. */
6469 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
6471 static gfc_expr **var_expr;
6472 static int total_var = 0;
6473 static int nvar = 0;
6475 gfc_forall_iterator *fa;
6480 /* Start to resolve a FORALL construct */
6481 if (forall_save == 0)
6483 /* Count the total number of FORALL index in the nested FORALL
6484 construct in order to allocate the VAR_EXPR with proper size. */
6485 total_var = gfc_count_forall_iterators (code);
6487 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
6488 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
6491 /* The information about FORALL iterator, including FORALL index start, end
6492 and stride. The FORALL index can not appear in start, end or stride. */
6493 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
6495 /* Check if any outer FORALL index name is the same as the current
6497 for (i = 0; i < nvar; i++)
6499 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
6501 gfc_error ("An outer FORALL construct already has an index "
6502 "with this name %L", &fa->var->where);
6506 /* Record the current FORALL index. */
6507 var_expr[nvar] = gfc_copy_expr (fa->var);
6511 /* No memory leak. */
6512 gcc_assert (nvar <= total_var);
6515 /* Resolve the FORALL body. */
6516 gfc_resolve_forall_body (code, nvar, var_expr);
6518 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
6519 gfc_resolve_blocks (code->block, ns);
6523 /* Free only the VAR_EXPRs allocated in this frame. */
6524 for (i = nvar; i < tmp; i++)
6525 gfc_free_expr (var_expr[i]);
6529 /* We are in the outermost FORALL construct. */
6530 gcc_assert (forall_save == 0);
6532 /* VAR_EXPR is not needed any more. */
6533 gfc_free (var_expr);
6539 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL ,GOTO and
6542 static void resolve_code (gfc_code *, gfc_namespace *);
6545 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
6549 for (; b; b = b->block)
6551 t = gfc_resolve_expr (b->expr);
6552 if (gfc_resolve_expr (b->expr2) == FAILURE)
6558 if (t == SUCCESS && b->expr != NULL
6559 && (b->expr->ts.type != BT_LOGICAL || b->expr->rank != 0))
6560 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
6567 && (b->expr->ts.type != BT_LOGICAL || b->expr->rank == 0))
6568 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
6573 resolve_branch (b->label, b);
6586 case EXEC_OMP_ATOMIC:
6587 case EXEC_OMP_CRITICAL:
6589 case EXEC_OMP_MASTER:
6590 case EXEC_OMP_ORDERED:
6591 case EXEC_OMP_PARALLEL:
6592 case EXEC_OMP_PARALLEL_DO:
6593 case EXEC_OMP_PARALLEL_SECTIONS:
6594 case EXEC_OMP_PARALLEL_WORKSHARE:
6595 case EXEC_OMP_SECTIONS:
6596 case EXEC_OMP_SINGLE:
6598 case EXEC_OMP_TASKWAIT:
6599 case EXEC_OMP_WORKSHARE:
6603 gfc_internal_error ("resolve_block(): Bad block type");
6606 resolve_code (b->next, ns);
6611 /* Does everything to resolve an ordinary assignment. Returns true
6612 if this is an interface assignment. */
6614 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
6624 if (gfc_extend_assign (code, ns) == SUCCESS)
6626 lhs = code->ext.actual->expr;
6627 rhs = code->ext.actual->next->expr;
6628 if (gfc_pure (NULL) && !gfc_pure (code->symtree->n.sym))
6630 gfc_error ("Subroutine '%s' called instead of assignment at "
6631 "%L must be PURE", code->symtree->n.sym->name,
6636 /* Make a temporary rhs when there is a default initializer
6637 and rhs is the same symbol as the lhs. */
6638 if (rhs->expr_type == EXPR_VARIABLE
6639 && rhs->symtree->n.sym->ts.type == BT_DERIVED
6640 && has_default_initializer (rhs->symtree->n.sym->ts.derived)
6641 && (lhs->symtree->n.sym == rhs->symtree->n.sym))
6642 code->ext.actual->next->expr = gfc_get_parentheses (rhs);
6651 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
6652 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
6653 &code->loc) == FAILURE)
6656 /* Handle the case of a BOZ literal on the RHS. */
6657 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
6660 if (gfc_option.warn_surprising)
6661 gfc_warning ("BOZ literal at %L is bitwise transferred "
6662 "non-integer symbol '%s'", &code->loc,
6663 lhs->symtree->n.sym->name);
6665 if (!gfc_convert_boz (rhs, &lhs->ts))
6667 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
6669 if (rc == ARITH_UNDERFLOW)
6670 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
6671 ". This check can be disabled with the option "
6672 "-fno-range-check", &rhs->where);
6673 else if (rc == ARITH_OVERFLOW)
6674 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
6675 ". This check can be disabled with the option "
6676 "-fno-range-check", &rhs->where);
6677 else if (rc == ARITH_NAN)
6678 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
6679 ". This check can be disabled with the option "
6680 "-fno-range-check", &rhs->where);
6686 if (lhs->ts.type == BT_CHARACTER
6687 && gfc_option.warn_character_truncation)
6689 if (lhs->ts.cl != NULL
6690 && lhs->ts.cl->length != NULL
6691 && lhs->ts.cl->length->expr_type == EXPR_CONSTANT)
6692 llen = mpz_get_si (lhs->ts.cl->length->value.integer);
6694 if (rhs->expr_type == EXPR_CONSTANT)
6695 rlen = rhs->value.character.length;
6697 else if (rhs->ts.cl != NULL
6698 && rhs->ts.cl->length != NULL
6699 && rhs->ts.cl->length->expr_type == EXPR_CONSTANT)
6700 rlen = mpz_get_si (rhs->ts.cl->length->value.integer);
6702 if (rlen && llen && rlen > llen)
6703 gfc_warning_now ("CHARACTER expression will be truncated "
6704 "in assignment (%d/%d) at %L",
6705 llen, rlen, &code->loc);
6708 /* Ensure that a vector index expression for the lvalue is evaluated
6709 to a temporary if the lvalue symbol is referenced in it. */
6712 for (ref = lhs->ref; ref; ref= ref->next)
6713 if (ref->type == REF_ARRAY)
6715 for (n = 0; n < ref->u.ar.dimen; n++)
6716 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
6717 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
6718 ref->u.ar.start[n]))
6720 = gfc_get_parentheses (ref->u.ar.start[n]);
6724 if (gfc_pure (NULL))
6726 if (gfc_impure_variable (lhs->symtree->n.sym))
6728 gfc_error ("Cannot assign to variable '%s' in PURE "
6730 lhs->symtree->n.sym->name,
6735 if (lhs->ts.type == BT_DERIVED
6736 && lhs->expr_type == EXPR_VARIABLE
6737 && lhs->ts.derived->attr.pointer_comp
6738 && gfc_impure_variable (rhs->symtree->n.sym))
6740 gfc_error ("The impure variable at %L is assigned to "
6741 "a derived type variable with a POINTER "
6742 "component in a PURE procedure (12.6)",
6748 gfc_check_assign (lhs, rhs, 1);
6752 /* Given a block of code, recursively resolve everything pointed to by this
6756 resolve_code (gfc_code *code, gfc_namespace *ns)
6758 int omp_workshare_save;
6763 frame.prev = cs_base;
6767 find_reachable_labels (code);
6769 for (; code; code = code->next)
6771 frame.current = code;
6772 forall_save = forall_flag;
6774 if (code->op == EXEC_FORALL)
6777 gfc_resolve_forall (code, ns, forall_save);
6780 else if (code->block)
6782 omp_workshare_save = -1;
6785 case EXEC_OMP_PARALLEL_WORKSHARE:
6786 omp_workshare_save = omp_workshare_flag;
6787 omp_workshare_flag = 1;
6788 gfc_resolve_omp_parallel_blocks (code, ns);
6790 case EXEC_OMP_PARALLEL:
6791 case EXEC_OMP_PARALLEL_DO:
6792 case EXEC_OMP_PARALLEL_SECTIONS:
6794 omp_workshare_save = omp_workshare_flag;
6795 omp_workshare_flag = 0;
6796 gfc_resolve_omp_parallel_blocks (code, ns);
6799 gfc_resolve_omp_do_blocks (code, ns);
6801 case EXEC_OMP_WORKSHARE:
6802 omp_workshare_save = omp_workshare_flag;
6803 omp_workshare_flag = 1;
6806 gfc_resolve_blocks (code->block, ns);
6810 if (omp_workshare_save != -1)
6811 omp_workshare_flag = omp_workshare_save;
6815 if (code->op != EXEC_COMPCALL)
6816 t = gfc_resolve_expr (code->expr);
6817 forall_flag = forall_save;
6819 if (gfc_resolve_expr (code->expr2) == FAILURE)
6825 case EXEC_END_BLOCK:
6835 /* Keep track of which entry we are up to. */
6836 current_entry_id = code->ext.entry->id;
6840 resolve_where (code, NULL);
6844 if (code->expr != NULL)
6846 if (code->expr->ts.type != BT_INTEGER)
6847 gfc_error ("ASSIGNED GOTO statement at %L requires an "
6848 "INTEGER variable", &code->expr->where);
6849 else if (code->expr->symtree->n.sym->attr.assign != 1)
6850 gfc_error ("Variable '%s' has not been assigned a target "
6851 "label at %L", code->expr->symtree->n.sym->name,
6852 &code->expr->where);
6855 resolve_branch (code->label, code);
6859 if (code->expr != NULL
6860 && (code->expr->ts.type != BT_INTEGER || code->expr->rank))
6861 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
6862 "INTEGER return specifier", &code->expr->where);
6865 case EXEC_INIT_ASSIGN:
6872 if (resolve_ordinary_assign (code, ns))
6877 case EXEC_LABEL_ASSIGN:
6878 if (code->label->defined == ST_LABEL_UNKNOWN)
6879 gfc_error ("Label %d referenced at %L is never defined",
6880 code->label->value, &code->label->where);
6882 && (code->expr->expr_type != EXPR_VARIABLE
6883 || code->expr->symtree->n.sym->ts.type != BT_INTEGER
6884 || code->expr->symtree->n.sym->ts.kind
6885 != gfc_default_integer_kind
6886 || code->expr->symtree->n.sym->as != NULL))
6887 gfc_error ("ASSIGN statement at %L requires a scalar "
6888 "default INTEGER variable", &code->expr->where);
6891 case EXEC_POINTER_ASSIGN:
6895 gfc_check_pointer_assign (code->expr, code->expr2);
6898 case EXEC_ARITHMETIC_IF:
6900 && code->expr->ts.type != BT_INTEGER
6901 && code->expr->ts.type != BT_REAL)
6902 gfc_error ("Arithmetic IF statement at %L requires a numeric "
6903 "expression", &code->expr->where);
6905 resolve_branch (code->label, code);
6906 resolve_branch (code->label2, code);
6907 resolve_branch (code->label3, code);
6911 if (t == SUCCESS && code->expr != NULL
6912 && (code->expr->ts.type != BT_LOGICAL
6913 || code->expr->rank != 0))
6914 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
6915 &code->expr->where);
6920 resolve_call (code);
6924 resolve_typebound_call (code);
6928 /* Select is complicated. Also, a SELECT construct could be
6929 a transformed computed GOTO. */
6930 resolve_select (code);
6934 if (code->ext.iterator != NULL)
6936 gfc_iterator *iter = code->ext.iterator;
6937 if (gfc_resolve_iterator (iter, true) != FAILURE)
6938 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
6943 if (code->expr == NULL)
6944 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
6946 && (code->expr->rank != 0
6947 || code->expr->ts.type != BT_LOGICAL))
6948 gfc_error ("Exit condition of DO WHILE loop at %L must be "
6949 "a scalar LOGICAL expression", &code->expr->where);
6954 resolve_allocate_deallocate (code, "ALLOCATE");
6958 case EXEC_DEALLOCATE:
6960 resolve_allocate_deallocate (code, "DEALLOCATE");
6965 if (gfc_resolve_open (code->ext.open) == FAILURE)
6968 resolve_branch (code->ext.open->err, code);
6972 if (gfc_resolve_close (code->ext.close) == FAILURE)
6975 resolve_branch (code->ext.close->err, code);
6978 case EXEC_BACKSPACE:
6982 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
6985 resolve_branch (code->ext.filepos->err, code);
6989 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
6992 resolve_branch (code->ext.inquire->err, code);
6996 gcc_assert (code->ext.inquire != NULL);
6997 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
7000 resolve_branch (code->ext.inquire->err, code);
7004 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
7007 resolve_branch (code->ext.wait->err, code);
7008 resolve_branch (code->ext.wait->end, code);
7009 resolve_branch (code->ext.wait->eor, code);
7014 if (gfc_resolve_dt (code->ext.dt) == FAILURE)
7017 resolve_branch (code->ext.dt->err, code);
7018 resolve_branch (code->ext.dt->end, code);
7019 resolve_branch (code->ext.dt->eor, code);
7023 resolve_transfer (code);
7027 resolve_forall_iterators (code->ext.forall_iterator);
7029 if (code->expr != NULL && code->expr->ts.type != BT_LOGICAL)
7030 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
7031 "expression", &code->expr->where);
7034 case EXEC_OMP_ATOMIC:
7035 case EXEC_OMP_BARRIER:
7036 case EXEC_OMP_CRITICAL:
7037 case EXEC_OMP_FLUSH:
7039 case EXEC_OMP_MASTER:
7040 case EXEC_OMP_ORDERED:
7041 case EXEC_OMP_SECTIONS:
7042 case EXEC_OMP_SINGLE:
7043 case EXEC_OMP_TASKWAIT:
7044 case EXEC_OMP_WORKSHARE:
7045 gfc_resolve_omp_directive (code, ns);
7048 case EXEC_OMP_PARALLEL:
7049 case EXEC_OMP_PARALLEL_DO:
7050 case EXEC_OMP_PARALLEL_SECTIONS:
7051 case EXEC_OMP_PARALLEL_WORKSHARE:
7053 omp_workshare_save = omp_workshare_flag;
7054 omp_workshare_flag = 0;
7055 gfc_resolve_omp_directive (code, ns);
7056 omp_workshare_flag = omp_workshare_save;
7060 gfc_internal_error ("resolve_code(): Bad statement code");
7064 cs_base = frame.prev;
7068 /* Resolve initial values and make sure they are compatible with
7072 resolve_values (gfc_symbol *sym)
7074 if (sym->value == NULL)
7077 if (gfc_resolve_expr (sym->value) == FAILURE)
7080 gfc_check_assign_symbol (sym, sym->value);
7084 /* Verify the binding labels for common blocks that are BIND(C). The label
7085 for a BIND(C) common block must be identical in all scoping units in which
7086 the common block is declared. Further, the binding label can not collide
7087 with any other global entity in the program. */
7090 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
7092 if (comm_block_tree->n.common->is_bind_c == 1)
7094 gfc_gsymbol *binding_label_gsym;
7095 gfc_gsymbol *comm_name_gsym;
7097 /* See if a global symbol exists by the common block's name. It may
7098 be NULL if the common block is use-associated. */
7099 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
7100 comm_block_tree->n.common->name);
7101 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
7102 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
7103 "with the global entity '%s' at %L",
7104 comm_block_tree->n.common->binding_label,
7105 comm_block_tree->n.common->name,
7106 &(comm_block_tree->n.common->where),
7107 comm_name_gsym->name, &(comm_name_gsym->where));
7108 else if (comm_name_gsym != NULL
7109 && strcmp (comm_name_gsym->name,
7110 comm_block_tree->n.common->name) == 0)
7112 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
7114 if (comm_name_gsym->binding_label == NULL)
7115 /* No binding label for common block stored yet; save this one. */
7116 comm_name_gsym->binding_label =
7117 comm_block_tree->n.common->binding_label;
7119 if (strcmp (comm_name_gsym->binding_label,
7120 comm_block_tree->n.common->binding_label) != 0)
7122 /* Common block names match but binding labels do not. */
7123 gfc_error ("Binding label '%s' for common block '%s' at %L "
7124 "does not match the binding label '%s' for common "
7126 comm_block_tree->n.common->binding_label,
7127 comm_block_tree->n.common->name,
7128 &(comm_block_tree->n.common->where),
7129 comm_name_gsym->binding_label,
7130 comm_name_gsym->name,
7131 &(comm_name_gsym->where));
7136 /* There is no binding label (NAME="") so we have nothing further to
7137 check and nothing to add as a global symbol for the label. */
7138 if (comm_block_tree->n.common->binding_label[0] == '\0' )
7141 binding_label_gsym =
7142 gfc_find_gsymbol (gfc_gsym_root,
7143 comm_block_tree->n.common->binding_label);
7144 if (binding_label_gsym == NULL)
7146 /* Need to make a global symbol for the binding label to prevent
7147 it from colliding with another. */
7148 binding_label_gsym =
7149 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
7150 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
7151 binding_label_gsym->type = GSYM_COMMON;
7155 /* If comm_name_gsym is NULL, the name common block is use
7156 associated and the name could be colliding. */
7157 if (binding_label_gsym->type != GSYM_COMMON)
7158 gfc_error ("Binding label '%s' for common block '%s' at %L "
7159 "collides with the global entity '%s' at %L",
7160 comm_block_tree->n.common->binding_label,
7161 comm_block_tree->n.common->name,
7162 &(comm_block_tree->n.common->where),
7163 binding_label_gsym->name,
7164 &(binding_label_gsym->where));
7165 else if (comm_name_gsym != NULL
7166 && (strcmp (binding_label_gsym->name,
7167 comm_name_gsym->binding_label) != 0)
7168 && (strcmp (binding_label_gsym->sym_name,
7169 comm_name_gsym->name) != 0))
7170 gfc_error ("Binding label '%s' for common block '%s' at %L "
7171 "collides with global entity '%s' at %L",
7172 binding_label_gsym->name, binding_label_gsym->sym_name,
7173 &(comm_block_tree->n.common->where),
7174 comm_name_gsym->name, &(comm_name_gsym->where));
7182 /* Verify any BIND(C) derived types in the namespace so we can report errors
7183 for them once, rather than for each variable declared of that type. */
7186 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
7188 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
7189 && derived_sym->attr.is_bind_c == 1)
7190 verify_bind_c_derived_type (derived_sym);
7196 /* Verify that any binding labels used in a given namespace do not collide
7197 with the names or binding labels of any global symbols. */
7200 gfc_verify_binding_labels (gfc_symbol *sym)
7204 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
7205 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
7207 gfc_gsymbol *bind_c_sym;
7209 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
7210 if (bind_c_sym != NULL
7211 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
7213 if (sym->attr.if_source == IFSRC_DECL
7214 && (bind_c_sym->type != GSYM_SUBROUTINE
7215 && bind_c_sym->type != GSYM_FUNCTION)
7216 && ((sym->attr.contained == 1
7217 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
7218 || (sym->attr.use_assoc == 1
7219 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
7221 /* Make sure global procedures don't collide with anything. */
7222 gfc_error ("Binding label '%s' at %L collides with the global "
7223 "entity '%s' at %L", sym->binding_label,
7224 &(sym->declared_at), bind_c_sym->name,
7225 &(bind_c_sym->where));
7228 else if (sym->attr.contained == 0
7229 && (sym->attr.if_source == IFSRC_IFBODY
7230 && sym->attr.flavor == FL_PROCEDURE)
7231 && (bind_c_sym->sym_name != NULL
7232 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
7234 /* Make sure procedures in interface bodies don't collide. */
7235 gfc_error ("Binding label '%s' in interface body at %L collides "
7236 "with the global entity '%s' at %L",
7238 &(sym->declared_at), bind_c_sym->name,
7239 &(bind_c_sym->where));
7242 else if (sym->attr.contained == 0
7243 && sym->attr.if_source == IFSRC_UNKNOWN)
7244 if ((sym->attr.use_assoc && bind_c_sym->mod_name
7245 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
7246 || sym->attr.use_assoc == 0)
7248 gfc_error ("Binding label '%s' at %L collides with global "
7249 "entity '%s' at %L", sym->binding_label,
7250 &(sym->declared_at), bind_c_sym->name,
7251 &(bind_c_sym->where));
7256 /* Clear the binding label to prevent checking multiple times. */
7257 sym->binding_label[0] = '\0';
7259 else if (bind_c_sym == NULL)
7261 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
7262 bind_c_sym->where = sym->declared_at;
7263 bind_c_sym->sym_name = sym->name;
7265 if (sym->attr.use_assoc == 1)
7266 bind_c_sym->mod_name = sym->module;
7268 if (sym->ns->proc_name != NULL)
7269 bind_c_sym->mod_name = sym->ns->proc_name->name;
7271 if (sym->attr.contained == 0)
7273 if (sym->attr.subroutine)
7274 bind_c_sym->type = GSYM_SUBROUTINE;
7275 else if (sym->attr.function)
7276 bind_c_sym->type = GSYM_FUNCTION;
7284 /* Resolve an index expression. */
7287 resolve_index_expr (gfc_expr *e)
7289 if (gfc_resolve_expr (e) == FAILURE)
7292 if (gfc_simplify_expr (e, 0) == FAILURE)
7295 if (gfc_specification_expr (e) == FAILURE)
7301 /* Resolve a charlen structure. */
7304 resolve_charlen (gfc_charlen *cl)
7313 specification_expr = 1;
7315 if (resolve_index_expr (cl->length) == FAILURE)
7317 specification_expr = 0;
7321 /* "If the character length parameter value evaluates to a negative
7322 value, the length of character entities declared is zero." */
7323 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
7325 gfc_warning_now ("CHARACTER variable has zero length at %L",
7326 &cl->length->where);
7327 gfc_replace_expr (cl->length, gfc_int_expr (0));
7334 /* Test for non-constant shape arrays. */
7337 is_non_constant_shape_array (gfc_symbol *sym)
7343 not_constant = false;
7344 if (sym->as != NULL)
7346 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
7347 has not been simplified; parameter array references. Do the
7348 simplification now. */
7349 for (i = 0; i < sym->as->rank; i++)
7351 e = sym->as->lower[i];
7352 if (e && (resolve_index_expr (e) == FAILURE
7353 || !gfc_is_constant_expr (e)))
7354 not_constant = true;
7356 e = sym->as->upper[i];
7357 if (e && (resolve_index_expr (e) == FAILURE
7358 || !gfc_is_constant_expr (e)))
7359 not_constant = true;
7362 return not_constant;
7365 /* Given a symbol and an initialization expression, add code to initialize
7366 the symbol to the function entry. */
7368 build_init_assign (gfc_symbol *sym, gfc_expr *init)
7372 gfc_namespace *ns = sym->ns;
7374 /* Search for the function namespace if this is a contained
7375 function without an explicit result. */
7376 if (sym->attr.function && sym == sym->result
7377 && sym->name != sym->ns->proc_name->name)
7380 for (;ns; ns = ns->sibling)
7381 if (strcmp (ns->proc_name->name, sym->name) == 0)
7387 gfc_free_expr (init);
7391 /* Build an l-value expression for the result. */
7392 lval = gfc_lval_expr_from_sym (sym);
7394 /* Add the code at scope entry. */
7395 init_st = gfc_get_code ();
7396 init_st->next = ns->code;
7399 /* Assign the default initializer to the l-value. */
7400 init_st->loc = sym->declared_at;
7401 init_st->op = EXEC_INIT_ASSIGN;
7402 init_st->expr = lval;
7403 init_st->expr2 = init;
7406 /* Assign the default initializer to a derived type variable or result. */
7409 apply_default_init (gfc_symbol *sym)
7411 gfc_expr *init = NULL;
7413 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
7416 if (sym->ts.type == BT_DERIVED && sym->ts.derived)
7417 init = gfc_default_initializer (&sym->ts);
7422 build_init_assign (sym, init);
7425 /* Build an initializer for a local integer, real, complex, logical, or
7426 character variable, based on the command line flags finit-local-zero,
7427 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
7428 null if the symbol should not have a default initialization. */
7430 build_default_init_expr (gfc_symbol *sym)
7433 gfc_expr *init_expr;
7436 /* These symbols should never have a default initialization. */
7437 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
7438 || sym->attr.external
7440 || sym->attr.pointer
7441 || sym->attr.in_equivalence
7442 || sym->attr.in_common
7445 || sym->attr.cray_pointee
7446 || sym->attr.cray_pointer)
7449 /* Now we'll try to build an initializer expression. */
7450 init_expr = gfc_get_expr ();
7451 init_expr->expr_type = EXPR_CONSTANT;
7452 init_expr->ts.type = sym->ts.type;
7453 init_expr->ts.kind = sym->ts.kind;
7454 init_expr->where = sym->declared_at;
7456 /* We will only initialize integers, reals, complex, logicals, and
7457 characters, and only if the corresponding command-line flags
7458 were set. Otherwise, we free init_expr and return null. */
7459 switch (sym->ts.type)
7462 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
7463 mpz_init_set_si (init_expr->value.integer,
7464 gfc_option.flag_init_integer_value);
7467 gfc_free_expr (init_expr);
7473 mpfr_init (init_expr->value.real);
7474 switch (gfc_option.flag_init_real)
7476 case GFC_INIT_REAL_SNAN:
7477 init_expr->is_snan = 1;
7479 case GFC_INIT_REAL_NAN:
7480 mpfr_set_nan (init_expr->value.real);
7483 case GFC_INIT_REAL_INF:
7484 mpfr_set_inf (init_expr->value.real, 1);
7487 case GFC_INIT_REAL_NEG_INF:
7488 mpfr_set_inf (init_expr->value.real, -1);
7491 case GFC_INIT_REAL_ZERO:
7492 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
7496 gfc_free_expr (init_expr);
7503 mpfr_init (init_expr->value.complex.r);
7504 mpfr_init (init_expr->value.complex.i);
7505 switch (gfc_option.flag_init_real)
7507 case GFC_INIT_REAL_SNAN:
7508 init_expr->is_snan = 1;
7510 case GFC_INIT_REAL_NAN:
7511 mpfr_set_nan (init_expr->value.complex.r);
7512 mpfr_set_nan (init_expr->value.complex.i);
7515 case GFC_INIT_REAL_INF:
7516 mpfr_set_inf (init_expr->value.complex.r, 1);
7517 mpfr_set_inf (init_expr->value.complex.i, 1);
7520 case GFC_INIT_REAL_NEG_INF:
7521 mpfr_set_inf (init_expr->value.complex.r, -1);
7522 mpfr_set_inf (init_expr->value.complex.i, -1);
7525 case GFC_INIT_REAL_ZERO:
7526 mpfr_set_ui (init_expr->value.complex.r, 0.0, GFC_RND_MODE);
7527 mpfr_set_ui (init_expr->value.complex.i, 0.0, GFC_RND_MODE);
7531 gfc_free_expr (init_expr);
7538 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
7539 init_expr->value.logical = 0;
7540 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
7541 init_expr->value.logical = 1;
7544 gfc_free_expr (init_expr);
7550 /* For characters, the length must be constant in order to
7551 create a default initializer. */
7552 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
7553 && sym->ts.cl->length
7554 && sym->ts.cl->length->expr_type == EXPR_CONSTANT)
7556 char_len = mpz_get_si (sym->ts.cl->length->value.integer);
7557 init_expr->value.character.length = char_len;
7558 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
7559 for (i = 0; i < char_len; i++)
7560 init_expr->value.character.string[i]
7561 = (unsigned char) gfc_option.flag_init_character_value;
7565 gfc_free_expr (init_expr);
7571 gfc_free_expr (init_expr);
7577 /* Add an initialization expression to a local variable. */
7579 apply_default_init_local (gfc_symbol *sym)
7581 gfc_expr *init = NULL;
7583 /* The symbol should be a variable or a function return value. */
7584 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
7585 || (sym->attr.function && sym->result != sym))
7588 /* Try to build the initializer expression. If we can't initialize
7589 this symbol, then init will be NULL. */
7590 init = build_default_init_expr (sym);
7594 /* For saved variables, we don't want to add an initializer at
7595 function entry, so we just add a static initializer. */
7596 if (sym->attr.save || sym->ns->save_all)
7598 /* Don't clobber an existing initializer! */
7599 gcc_assert (sym->value == NULL);
7604 build_init_assign (sym, init);
7607 /* Resolution of common features of flavors variable and procedure. */
7610 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
7612 /* Constraints on deferred shape variable. */
7613 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
7615 if (sym->attr.allocatable)
7617 if (sym->attr.dimension)
7618 gfc_error ("Allocatable array '%s' at %L must have "
7619 "a deferred shape", sym->name, &sym->declared_at);
7621 gfc_error ("Scalar object '%s' at %L may not be ALLOCATABLE",
7622 sym->name, &sym->declared_at);
7626 if (sym->attr.pointer && sym->attr.dimension)
7628 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
7629 sym->name, &sym->declared_at);
7636 if (!mp_flag && !sym->attr.allocatable
7637 && !sym->attr.pointer && !sym->attr.dummy)
7639 gfc_error ("Array '%s' at %L cannot have a deferred shape",
7640 sym->name, &sym->declared_at);
7648 /* Additional checks for symbols with flavor variable and derived
7649 type. To be called from resolve_fl_variable. */
7652 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
7654 gcc_assert (sym->ts.type == BT_DERIVED);
7656 /* Check to see if a derived type is blocked from being host
7657 associated by the presence of another class I symbol in the same
7658 namespace. 14.6.1.3 of the standard and the discussion on
7659 comp.lang.fortran. */
7660 if (sym->ns != sym->ts.derived->ns
7661 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
7664 gfc_find_symbol (sym->ts.derived->name, sym->ns, 0, &s);
7665 if (s && s->attr.flavor != FL_DERIVED)
7667 gfc_error ("The type '%s' cannot be host associated at %L "
7668 "because it is blocked by an incompatible object "
7669 "of the same name declared at %L",
7670 sym->ts.derived->name, &sym->declared_at,
7676 /* 4th constraint in section 11.3: "If an object of a type for which
7677 component-initialization is specified (R429) appears in the
7678 specification-part of a module and does not have the ALLOCATABLE
7679 or POINTER attribute, the object shall have the SAVE attribute."
7681 The check for initializers is performed with
7682 has_default_initializer because gfc_default_initializer generates
7683 a hidden default for allocatable components. */
7684 if (!(sym->value || no_init_flag) && sym->ns->proc_name
7685 && sym->ns->proc_name->attr.flavor == FL_MODULE
7686 && !sym->ns->save_all && !sym->attr.save
7687 && !sym->attr.pointer && !sym->attr.allocatable
7688 && has_default_initializer (sym->ts.derived))
7690 gfc_error("Object '%s' at %L must have the SAVE attribute for "
7691 "default initialization of a component",
7692 sym->name, &sym->declared_at);
7696 /* Assign default initializer. */
7697 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
7698 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
7700 sym->value = gfc_default_initializer (&sym->ts);
7707 /* Resolve symbols with flavor variable. */
7710 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
7712 int no_init_flag, automatic_flag;
7714 const char *auto_save_msg;
7716 auto_save_msg = "Automatic object '%s' at %L cannot have the "
7719 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
7722 /* Set this flag to check that variables are parameters of all entries.
7723 This check is effected by the call to gfc_resolve_expr through
7724 is_non_constant_shape_array. */
7725 specification_expr = 1;
7727 if (sym->ns->proc_name
7728 && (sym->ns->proc_name->attr.flavor == FL_MODULE
7729 || sym->ns->proc_name->attr.is_main_program)
7730 && !sym->attr.use_assoc
7731 && !sym->attr.allocatable
7732 && !sym->attr.pointer
7733 && is_non_constant_shape_array (sym))
7735 /* The shape of a main program or module array needs to be
7737 gfc_error ("The module or main program array '%s' at %L must "
7738 "have constant shape", sym->name, &sym->declared_at);
7739 specification_expr = 0;
7743 if (sym->ts.type == BT_CHARACTER)
7745 /* Make sure that character string variables with assumed length are
7747 e = sym->ts.cl->length;
7748 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
7750 gfc_error ("Entity with assumed character length at %L must be a "
7751 "dummy argument or a PARAMETER", &sym->declared_at);
7755 if (e && sym->attr.save && !gfc_is_constant_expr (e))
7757 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
7761 if (!gfc_is_constant_expr (e)
7762 && !(e->expr_type == EXPR_VARIABLE
7763 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
7764 && sym->ns->proc_name
7765 && (sym->ns->proc_name->attr.flavor == FL_MODULE
7766 || sym->ns->proc_name->attr.is_main_program)
7767 && !sym->attr.use_assoc)
7769 gfc_error ("'%s' at %L must have constant character length "
7770 "in this context", sym->name, &sym->declared_at);
7775 if (sym->value == NULL && sym->attr.referenced)
7776 apply_default_init_local (sym); /* Try to apply a default initialization. */
7778 /* Determine if the symbol may not have an initializer. */
7779 no_init_flag = automatic_flag = 0;
7780 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
7781 || sym->attr.intrinsic || sym->attr.result)
7783 else if (sym->attr.dimension && !sym->attr.pointer
7784 && is_non_constant_shape_array (sym))
7786 no_init_flag = automatic_flag = 1;
7788 /* Also, they must not have the SAVE attribute.
7789 SAVE_IMPLICIT is checked below. */
7790 if (sym->attr.save == SAVE_EXPLICIT)
7792 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
7797 /* Ensure that any initializer is simplified. */
7799 gfc_simplify_expr (sym->value, 1);
7801 /* Reject illegal initializers. */
7802 if (!sym->mark && sym->value)
7804 if (sym->attr.allocatable)
7805 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
7806 sym->name, &sym->declared_at);
7807 else if (sym->attr.external)
7808 gfc_error ("External '%s' at %L cannot have an initializer",
7809 sym->name, &sym->declared_at);
7810 else if (sym->attr.dummy
7811 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
7812 gfc_error ("Dummy '%s' at %L cannot have an initializer",
7813 sym->name, &sym->declared_at);
7814 else if (sym->attr.intrinsic)
7815 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
7816 sym->name, &sym->declared_at);
7817 else if (sym->attr.result)
7818 gfc_error ("Function result '%s' at %L cannot have an initializer",
7819 sym->name, &sym->declared_at);
7820 else if (automatic_flag)
7821 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
7822 sym->name, &sym->declared_at);
7829 if (sym->ts.type == BT_DERIVED)
7830 return resolve_fl_variable_derived (sym, no_init_flag);
7836 /* Resolve a procedure. */
7839 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
7841 gfc_formal_arglist *arg;
7843 if (sym->attr.ambiguous_interfaces && !sym->attr.referenced)
7844 gfc_warning ("Although not referenced, '%s' at %L has ambiguous "
7845 "interfaces", sym->name, &sym->declared_at);
7847 if (sym->attr.function
7848 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
7851 if (sym->ts.type == BT_CHARACTER)
7853 gfc_charlen *cl = sym->ts.cl;
7855 if (cl && cl->length && gfc_is_constant_expr (cl->length)
7856 && resolve_charlen (cl) == FAILURE)
7859 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
7861 if (sym->attr.proc == PROC_ST_FUNCTION)
7863 gfc_error ("Character-valued statement function '%s' at %L must "
7864 "have constant length", sym->name, &sym->declared_at);
7868 if (sym->attr.external && sym->formal == NULL
7869 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
7871 gfc_error ("Automatic character length function '%s' at %L must "
7872 "have an explicit interface", sym->name,
7879 /* Ensure that derived type for are not of a private type. Internal
7880 module procedures are excluded by 2.2.3.3 - i.e., they are not
7881 externally accessible and can access all the objects accessible in
7883 if (!(sym->ns->parent
7884 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
7885 && gfc_check_access(sym->attr.access, sym->ns->default_access))
7887 gfc_interface *iface;
7889 for (arg = sym->formal; arg; arg = arg->next)
7892 && arg->sym->ts.type == BT_DERIVED
7893 && !arg->sym->ts.derived->attr.use_assoc
7894 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7895 arg->sym->ts.derived->ns->default_access)
7896 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
7897 "PRIVATE type and cannot be a dummy argument"
7898 " of '%s', which is PUBLIC at %L",
7899 arg->sym->name, sym->name, &sym->declared_at)
7902 /* Stop this message from recurring. */
7903 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7908 /* PUBLIC interfaces may expose PRIVATE procedures that take types
7909 PRIVATE to the containing module. */
7910 for (iface = sym->generic; iface; iface = iface->next)
7912 for (arg = iface->sym->formal; arg; arg = arg->next)
7915 && arg->sym->ts.type == BT_DERIVED
7916 && !arg->sym->ts.derived->attr.use_assoc
7917 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7918 arg->sym->ts.derived->ns->default_access)
7919 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
7920 "'%s' in PUBLIC interface '%s' at %L "
7921 "takes dummy arguments of '%s' which is "
7922 "PRIVATE", iface->sym->name, sym->name,
7923 &iface->sym->declared_at,
7924 gfc_typename (&arg->sym->ts)) == FAILURE)
7926 /* Stop this message from recurring. */
7927 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7933 /* PUBLIC interfaces may expose PRIVATE procedures that take types
7934 PRIVATE to the containing module. */
7935 for (iface = sym->generic; iface; iface = iface->next)
7937 for (arg = iface->sym->formal; arg; arg = arg->next)
7940 && arg->sym->ts.type == BT_DERIVED
7941 && !arg->sym->ts.derived->attr.use_assoc
7942 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7943 arg->sym->ts.derived->ns->default_access)
7944 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
7945 "'%s' in PUBLIC interface '%s' at %L "
7946 "takes dummy arguments of '%s' which is "
7947 "PRIVATE", iface->sym->name, sym->name,
7948 &iface->sym->declared_at,
7949 gfc_typename (&arg->sym->ts)) == FAILURE)
7951 /* Stop this message from recurring. */
7952 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7959 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
7960 && !sym->attr.proc_pointer)
7962 gfc_error ("Function '%s' at %L cannot have an initializer",
7963 sym->name, &sym->declared_at);
7967 /* An external symbol may not have an initializer because it is taken to be
7968 a procedure. Exception: Procedure Pointers. */
7969 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
7971 gfc_error ("External object '%s' at %L may not have an initializer",
7972 sym->name, &sym->declared_at);
7976 /* An elemental function is required to return a scalar 12.7.1 */
7977 if (sym->attr.elemental && sym->attr.function && sym->as)
7979 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
7980 "result", sym->name, &sym->declared_at);
7981 /* Reset so that the error only occurs once. */
7982 sym->attr.elemental = 0;
7986 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
7987 char-len-param shall not be array-valued, pointer-valued, recursive
7988 or pure. ....snip... A character value of * may only be used in the
7989 following ways: (i) Dummy arg of procedure - dummy associates with
7990 actual length; (ii) To declare a named constant; or (iii) External
7991 function - but length must be declared in calling scoping unit. */
7992 if (sym->attr.function
7993 && sym->ts.type == BT_CHARACTER
7994 && sym->ts.cl && sym->ts.cl->length == NULL)
7996 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
7997 || (sym->attr.recursive) || (sym->attr.pure))
7999 if (sym->as && sym->as->rank)
8000 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8001 "array-valued", sym->name, &sym->declared_at);
8003 if (sym->attr.pointer)
8004 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8005 "pointer-valued", sym->name, &sym->declared_at);
8008 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8009 "pure", sym->name, &sym->declared_at);
8011 if (sym->attr.recursive)
8012 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
8013 "recursive", sym->name, &sym->declared_at);
8018 /* Appendix B.2 of the standard. Contained functions give an
8019 error anyway. Fixed-form is likely to be F77/legacy. */
8020 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
8021 gfc_notify_std (GFC_STD_F95_OBS, "CHARACTER(*) function "
8022 "'%s' at %L is obsolescent in fortran 95",
8023 sym->name, &sym->declared_at);
8026 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
8028 gfc_formal_arglist *curr_arg;
8029 int has_non_interop_arg = 0;
8031 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
8032 sym->common_block) == FAILURE)
8034 /* Clear these to prevent looking at them again if there was an
8036 sym->attr.is_bind_c = 0;
8037 sym->attr.is_c_interop = 0;
8038 sym->ts.is_c_interop = 0;
8042 /* So far, no errors have been found. */
8043 sym->attr.is_c_interop = 1;
8044 sym->ts.is_c_interop = 1;
8047 curr_arg = sym->formal;
8048 while (curr_arg != NULL)
8050 /* Skip implicitly typed dummy args here. */
8051 if (curr_arg->sym->attr.implicit_type == 0)
8052 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
8053 /* If something is found to fail, record the fact so we
8054 can mark the symbol for the procedure as not being
8055 BIND(C) to try and prevent multiple errors being
8057 has_non_interop_arg = 1;
8059 curr_arg = curr_arg->next;
8062 /* See if any of the arguments were not interoperable and if so, clear
8063 the procedure symbol to prevent duplicate error messages. */
8064 if (has_non_interop_arg != 0)
8066 sym->attr.is_c_interop = 0;
8067 sym->ts.is_c_interop = 0;
8068 sym->attr.is_bind_c = 0;
8072 if (sym->attr.save == SAVE_EXPLICIT && !sym->attr.proc_pointer)
8074 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
8075 "in '%s' at %L", sym->name, &sym->declared_at);
8079 if (sym->attr.intent && !sym->attr.proc_pointer)
8081 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
8082 "in '%s' at %L", sym->name, &sym->declared_at);
8090 /* Resolve a list of finalizer procedures. That is, after they have hopefully
8091 been defined and we now know their defined arguments, check that they fulfill
8092 the requirements of the standard for procedures used as finalizers. */
8095 gfc_resolve_finalizers (gfc_symbol* derived)
8097 gfc_finalizer* list;
8098 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
8099 gfc_try result = SUCCESS;
8100 bool seen_scalar = false;
8102 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
8105 /* Walk over the list of finalizer-procedures, check them, and if any one
8106 does not fit in with the standard's definition, print an error and remove
8107 it from the list. */
8108 prev_link = &derived->f2k_derived->finalizers;
8109 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
8115 /* Skip this finalizer if we already resolved it. */
8116 if (list->proc_tree)
8118 prev_link = &(list->next);
8122 /* Check this exists and is a SUBROUTINE. */
8123 if (!list->proc_sym->attr.subroutine)
8125 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
8126 list->proc_sym->name, &list->where);
8130 /* We should have exactly one argument. */
8131 if (!list->proc_sym->formal || list->proc_sym->formal->next)
8133 gfc_error ("FINAL procedure at %L must have exactly one argument",
8137 arg = list->proc_sym->formal->sym;
8139 /* This argument must be of our type. */
8140 if (arg->ts.type != BT_DERIVED || arg->ts.derived != derived)
8142 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
8143 &arg->declared_at, derived->name);
8147 /* It must neither be a pointer nor allocatable nor optional. */
8148 if (arg->attr.pointer)
8150 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
8154 if (arg->attr.allocatable)
8156 gfc_error ("Argument of FINAL procedure at %L must not be"
8157 " ALLOCATABLE", &arg->declared_at);
8160 if (arg->attr.optional)
8162 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
8167 /* It must not be INTENT(OUT). */
8168 if (arg->attr.intent == INTENT_OUT)
8170 gfc_error ("Argument of FINAL procedure at %L must not be"
8171 " INTENT(OUT)", &arg->declared_at);
8175 /* Warn if the procedure is non-scalar and not assumed shape. */
8176 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
8177 && arg->as->type != AS_ASSUMED_SHAPE)
8178 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
8179 " shape argument", &arg->declared_at);
8181 /* Check that it does not match in kind and rank with a FINAL procedure
8182 defined earlier. To really loop over the *earlier* declarations,
8183 we need to walk the tail of the list as new ones were pushed at the
8185 /* TODO: Handle kind parameters once they are implemented. */
8186 my_rank = (arg->as ? arg->as->rank : 0);
8187 for (i = list->next; i; i = i->next)
8189 /* Argument list might be empty; that is an error signalled earlier,
8190 but we nevertheless continued resolving. */
8191 if (i->proc_sym->formal)
8193 gfc_symbol* i_arg = i->proc_sym->formal->sym;
8194 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
8195 if (i_rank == my_rank)
8197 gfc_error ("FINAL procedure '%s' declared at %L has the same"
8198 " rank (%d) as '%s'",
8199 list->proc_sym->name, &list->where, my_rank,
8206 /* Is this the/a scalar finalizer procedure? */
8207 if (!arg->as || arg->as->rank == 0)
8210 /* Find the symtree for this procedure. */
8211 gcc_assert (!list->proc_tree);
8212 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
8214 prev_link = &list->next;
8217 /* Remove wrong nodes immediately from the list so we don't risk any
8218 troubles in the future when they might fail later expectations. */
8222 *prev_link = list->next;
8223 gfc_free_finalizer (i);
8226 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
8227 were nodes in the list, must have been for arrays. It is surely a good
8228 idea to have a scalar version there if there's something to finalize. */
8229 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
8230 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
8231 " defined at %L, suggest also scalar one",
8232 derived->name, &derived->declared_at);
8234 /* TODO: Remove this error when finalization is finished. */
8235 gfc_error ("Finalization at %L is not yet implemented",
8236 &derived->declared_at);
8242 /* Check that it is ok for the typebound procedure proc to override the
8246 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
8249 const gfc_symbol* proc_target;
8250 const gfc_symbol* old_target;
8251 unsigned proc_pass_arg, old_pass_arg, argpos;
8252 gfc_formal_arglist* proc_formal;
8253 gfc_formal_arglist* old_formal;
8255 /* This procedure should only be called for non-GENERIC proc. */
8256 gcc_assert (!proc->typebound->is_generic);
8258 /* If the overwritten procedure is GENERIC, this is an error. */
8259 if (old->typebound->is_generic)
8261 gfc_error ("Can't overwrite GENERIC '%s' at %L",
8262 old->name, &proc->typebound->where);
8266 where = proc->typebound->where;
8267 proc_target = proc->typebound->u.specific->n.sym;
8268 old_target = old->typebound->u.specific->n.sym;
8270 /* Check that overridden binding is not NON_OVERRIDABLE. */
8271 if (old->typebound->non_overridable)
8273 gfc_error ("'%s' at %L overrides a procedure binding declared"
8274 " NON_OVERRIDABLE", proc->name, &where);
8278 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
8279 if (!old->typebound->deferred && proc->typebound->deferred)
8281 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
8282 " non-DEFERRED binding", proc->name, &where);
8286 /* If the overridden binding is PURE, the overriding must be, too. */
8287 if (old_target->attr.pure && !proc_target->attr.pure)
8289 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
8290 proc->name, &where);
8294 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
8295 is not, the overriding must not be either. */
8296 if (old_target->attr.elemental && !proc_target->attr.elemental)
8298 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
8299 " ELEMENTAL", proc->name, &where);
8302 if (!old_target->attr.elemental && proc_target->attr.elemental)
8304 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
8305 " be ELEMENTAL, either", proc->name, &where);
8309 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
8311 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
8313 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
8314 " SUBROUTINE", proc->name, &where);
8318 /* If the overridden binding is a FUNCTION, the overriding must also be a
8319 FUNCTION and have the same characteristics. */
8320 if (old_target->attr.function)
8322 if (!proc_target->attr.function)
8324 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
8325 " FUNCTION", proc->name, &where);
8329 /* FIXME: Do more comprehensive checking (including, for instance, the
8330 rank and array-shape). */
8331 gcc_assert (proc_target->result && old_target->result);
8332 if (!gfc_compare_types (&proc_target->result->ts,
8333 &old_target->result->ts))
8335 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
8336 " matching result types", proc->name, &where);
8341 /* If the overridden binding is PUBLIC, the overriding one must not be
8343 if (old->typebound->access == ACCESS_PUBLIC
8344 && proc->typebound->access == ACCESS_PRIVATE)
8346 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
8347 " PRIVATE", proc->name, &where);
8351 /* Compare the formal argument lists of both procedures. This is also abused
8352 to find the position of the passed-object dummy arguments of both
8353 bindings as at least the overridden one might not yet be resolved and we
8354 need those positions in the check below. */
8355 proc_pass_arg = old_pass_arg = 0;
8356 if (!proc->typebound->nopass && !proc->typebound->pass_arg)
8358 if (!old->typebound->nopass && !old->typebound->pass_arg)
8361 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
8362 proc_formal && old_formal;
8363 proc_formal = proc_formal->next, old_formal = old_formal->next)
8365 if (proc->typebound->pass_arg
8366 && !strcmp (proc->typebound->pass_arg, proc_formal->sym->name))
8367 proc_pass_arg = argpos;
8368 if (old->typebound->pass_arg
8369 && !strcmp (old->typebound->pass_arg, old_formal->sym->name))
8370 old_pass_arg = argpos;
8372 /* Check that the names correspond. */
8373 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
8375 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
8376 " to match the corresponding argument of the overridden"
8377 " procedure", proc_formal->sym->name, proc->name, &where,
8378 old_formal->sym->name);
8382 /* Check that the types correspond if neither is the passed-object
8384 /* FIXME: Do more comprehensive testing here. */
8385 if (proc_pass_arg != argpos && old_pass_arg != argpos
8386 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
8388 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L in"
8389 " in respect to the overridden procedure",
8390 proc_formal->sym->name, proc->name, &where);
8396 if (proc_formal || old_formal)
8398 gfc_error ("'%s' at %L must have the same number of formal arguments as"
8399 " the overridden procedure", proc->name, &where);
8403 /* If the overridden binding is NOPASS, the overriding one must also be
8405 if (old->typebound->nopass && !proc->typebound->nopass)
8407 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
8408 " NOPASS", proc->name, &where);
8412 /* If the overridden binding is PASS(x), the overriding one must also be
8413 PASS and the passed-object dummy arguments must correspond. */
8414 if (!old->typebound->nopass)
8416 if (proc->typebound->nopass)
8418 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
8419 " PASS", proc->name, &where);
8423 if (proc_pass_arg != old_pass_arg)
8425 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
8426 " the same position as the passed-object dummy argument of"
8427 " the overridden procedure", proc->name, &where);
8436 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
8439 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
8440 const char* generic_name, locus where)
8445 gcc_assert (t1->specific && t2->specific);
8446 gcc_assert (!t1->specific->is_generic);
8447 gcc_assert (!t2->specific->is_generic);
8449 sym1 = t1->specific->u.specific->n.sym;
8450 sym2 = t2->specific->u.specific->n.sym;
8452 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
8453 if (sym1->attr.subroutine != sym2->attr.subroutine
8454 || sym1->attr.function != sym2->attr.function)
8456 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
8457 " GENERIC '%s' at %L",
8458 sym1->name, sym2->name, generic_name, &where);
8462 /* Compare the interfaces. */
8463 if (gfc_compare_interfaces (sym1, sym2, 1))
8465 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
8466 sym1->name, sym2->name, generic_name, &where);
8474 /* Resolve a GENERIC procedure binding for a derived type. */
8477 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
8479 gfc_tbp_generic* target;
8480 gfc_symtree* first_target;
8481 gfc_symbol* super_type;
8482 gfc_symtree* inherited;
8485 gcc_assert (st->typebound);
8486 gcc_assert (st->typebound->is_generic);
8488 where = st->typebound->where;
8489 super_type = gfc_get_derived_super_type (derived);
8491 /* Find the overridden binding if any. */
8492 st->typebound->overridden = NULL;
8495 gfc_symtree* overridden;
8496 overridden = gfc_find_typebound_proc (super_type, NULL, st->name, true);
8498 if (overridden && overridden->typebound)
8499 st->typebound->overridden = overridden->typebound;
8502 /* Try to find the specific bindings for the symtrees in our target-list. */
8503 gcc_assert (st->typebound->u.generic);
8504 for (target = st->typebound->u.generic; target; target = target->next)
8505 if (!target->specific)
8507 gfc_typebound_proc* overridden_tbp;
8509 const char* target_name;
8511 target_name = target->specific_st->name;
8513 /* Defined for this type directly. */
8514 if (target->specific_st->typebound)
8516 target->specific = target->specific_st->typebound;
8517 goto specific_found;
8520 /* Look for an inherited specific binding. */
8523 inherited = gfc_find_typebound_proc (super_type, NULL,
8528 gcc_assert (inherited->typebound);
8529 target->specific = inherited->typebound;
8530 goto specific_found;
8534 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
8535 " at %L", target_name, st->name, &where);
8538 /* Once we've found the specific binding, check it is not ambiguous with
8539 other specifics already found or inherited for the same GENERIC. */
8541 gcc_assert (target->specific);
8543 /* This must really be a specific binding! */
8544 if (target->specific->is_generic)
8546 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
8547 " '%s' is GENERIC, too", st->name, &where, target_name);
8551 /* Check those already resolved on this type directly. */
8552 for (g = st->typebound->u.generic; g; g = g->next)
8553 if (g != target && g->specific
8554 && check_generic_tbp_ambiguity (target, g, st->name, where)
8558 /* Check for ambiguity with inherited specific targets. */
8559 for (overridden_tbp = st->typebound->overridden; overridden_tbp;
8560 overridden_tbp = overridden_tbp->overridden)
8561 if (overridden_tbp->is_generic)
8563 for (g = overridden_tbp->u.generic; g; g = g->next)
8565 gcc_assert (g->specific);
8566 if (check_generic_tbp_ambiguity (target, g,
8567 st->name, where) == FAILURE)
8573 /* If we attempt to "overwrite" a specific binding, this is an error. */
8574 if (st->typebound->overridden && !st->typebound->overridden->is_generic)
8576 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
8577 " the same name", st->name, &where);
8581 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
8582 all must have the same attributes here. */
8583 first_target = st->typebound->u.generic->specific->u.specific;
8584 st->typebound->subroutine = first_target->n.sym->attr.subroutine;
8585 st->typebound->function = first_target->n.sym->attr.function;
8591 /* Resolve the type-bound procedures for a derived type. */
8593 static gfc_symbol* resolve_bindings_derived;
8594 static gfc_try resolve_bindings_result;
8597 resolve_typebound_procedure (gfc_symtree* stree)
8602 gfc_symbol* super_type;
8603 gfc_component* comp;
8605 /* If this is no type-bound procedure, just return. */
8606 if (!stree->typebound)
8609 /* If this is a GENERIC binding, use that routine. */
8610 if (stree->typebound->is_generic)
8612 if (resolve_typebound_generic (resolve_bindings_derived, stree)
8618 /* Get the target-procedure to check it. */
8619 gcc_assert (!stree->typebound->is_generic);
8620 gcc_assert (stree->typebound->u.specific);
8621 proc = stree->typebound->u.specific->n.sym;
8622 where = stree->typebound->where;
8624 /* Default access should already be resolved from the parser. */
8625 gcc_assert (stree->typebound->access != ACCESS_UNKNOWN);
8627 /* It should be a module procedure or an external procedure with explicit
8628 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
8629 if ((!proc->attr.subroutine && !proc->attr.function)
8630 || (proc->attr.proc != PROC_MODULE
8631 && proc->attr.if_source != IFSRC_IFBODY)
8632 || (proc->attr.abstract && !stree->typebound->deferred))
8634 gfc_error ("'%s' must be a module procedure or an external procedure with"
8635 " an explicit interface at %L", proc->name, &where);
8638 stree->typebound->subroutine = proc->attr.subroutine;
8639 stree->typebound->function = proc->attr.function;
8641 /* Find the super-type of the current derived type. We could do this once and
8642 store in a global if speed is needed, but as long as not I believe this is
8643 more readable and clearer. */
8644 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
8646 /* If PASS, resolve and check arguments if not already resolved / loaded
8647 from a .mod file. */
8648 if (!stree->typebound->nopass && stree->typebound->pass_arg_num == 0)
8650 if (stree->typebound->pass_arg)
8652 gfc_formal_arglist* i;
8654 /* If an explicit passing argument name is given, walk the arg-list
8658 stree->typebound->pass_arg_num = 1;
8659 for (i = proc->formal; i; i = i->next)
8661 if (!strcmp (i->sym->name, stree->typebound->pass_arg))
8666 ++stree->typebound->pass_arg_num;
8671 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
8673 proc->name, stree->typebound->pass_arg, &where,
8674 stree->typebound->pass_arg);
8680 /* Otherwise, take the first one; there should in fact be at least
8682 stree->typebound->pass_arg_num = 1;
8685 gfc_error ("Procedure '%s' with PASS at %L must have at"
8686 " least one argument", proc->name, &where);
8689 me_arg = proc->formal->sym;
8692 /* Now check that the argument-type matches. */
8693 gcc_assert (me_arg);
8694 if (me_arg->ts.type != BT_DERIVED
8695 || me_arg->ts.derived != resolve_bindings_derived)
8697 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
8698 " the derived-type '%s'", me_arg->name, proc->name,
8699 me_arg->name, &where, resolve_bindings_derived->name);
8703 gfc_warning ("Polymorphic entities are not yet implemented,"
8704 " non-polymorphic passed-object dummy argument of '%s'"
8705 " at %L accepted", proc->name, &where);
8708 /* If we are extending some type, check that we don't override a procedure
8709 flagged NON_OVERRIDABLE. */
8710 stree->typebound->overridden = NULL;
8713 gfc_symtree* overridden;
8714 overridden = gfc_find_typebound_proc (super_type, NULL,
8717 if (overridden && overridden->typebound)
8718 stree->typebound->overridden = overridden->typebound;
8720 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
8724 /* See if there's a name collision with a component directly in this type. */
8725 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
8726 if (!strcmp (comp->name, stree->name))
8728 gfc_error ("Procedure '%s' at %L has the same name as a component of"
8730 stree->name, &where, resolve_bindings_derived->name);
8734 /* Try to find a name collision with an inherited component. */
8735 if (super_type && gfc_find_component (super_type, stree->name, true, true))
8737 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
8738 " component of '%s'",
8739 stree->name, &where, resolve_bindings_derived->name);
8743 stree->typebound->error = 0;
8747 resolve_bindings_result = FAILURE;
8748 stree->typebound->error = 1;
8752 resolve_typebound_procedures (gfc_symbol* derived)
8754 if (!derived->f2k_derived || !derived->f2k_derived->sym_root)
8757 resolve_bindings_derived = derived;
8758 resolve_bindings_result = SUCCESS;
8759 gfc_traverse_symtree (derived->f2k_derived->sym_root,
8760 &resolve_typebound_procedure);
8762 return resolve_bindings_result;
8766 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
8767 to give all identical derived types the same backend_decl. */
8769 add_dt_to_dt_list (gfc_symbol *derived)
8771 gfc_dt_list *dt_list;
8773 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
8774 if (derived == dt_list->derived)
8777 if (dt_list == NULL)
8779 dt_list = gfc_get_dt_list ();
8780 dt_list->next = gfc_derived_types;
8781 dt_list->derived = derived;
8782 gfc_derived_types = dt_list;
8787 /* Ensure that a derived-type is really not abstract, meaning that every
8788 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
8791 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
8796 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
8798 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
8801 if (st->typebound && st->typebound->deferred)
8803 gfc_symtree* overriding;
8804 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true);
8805 gcc_assert (overriding && overriding->typebound);
8806 if (overriding->typebound->deferred)
8808 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
8809 " '%s' is DEFERRED and not overridden",
8810 sub->name, &sub->declared_at, st->name);
8819 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
8821 /* The algorithm used here is to recursively travel up the ancestry of sub
8822 and for each ancestor-type, check all bindings. If any of them is
8823 DEFERRED, look it up starting from sub and see if the found (overriding)
8824 binding is not DEFERRED.
8825 This is not the most efficient way to do this, but it should be ok and is
8826 clearer than something sophisticated. */
8828 gcc_assert (ancestor && ancestor->attr.abstract && !sub->attr.abstract);
8830 /* Walk bindings of this ancestor. */
8831 if (ancestor->f2k_derived)
8834 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->sym_root);
8839 /* Find next ancestor type and recurse on it. */
8840 ancestor = gfc_get_derived_super_type (ancestor);
8842 return ensure_not_abstract (sub, ancestor);
8848 /* Resolve the components of a derived type. */
8851 resolve_fl_derived (gfc_symbol *sym)
8853 gfc_symbol* super_type;
8857 super_type = gfc_get_derived_super_type (sym);
8859 /* Ensure the extended type gets resolved before we do. */
8860 if (super_type && resolve_fl_derived (super_type) == FAILURE)
8863 /* An ABSTRACT type must be extensible. */
8864 if (sym->attr.abstract && (sym->attr.is_bind_c || sym->attr.sequence))
8866 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
8867 sym->name, &sym->declared_at);
8871 for (c = sym->components; c != NULL; c = c->next)
8873 /* Check type-spec if this is not the parent-type component. */
8874 if ((!sym->attr.extension || c != sym->components)
8875 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
8878 /* If this type is an extension, see if this component has the same name
8879 as an inherited type-bound procedure. */
8881 && gfc_find_typebound_proc (super_type, NULL, c->name, true))
8883 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
8884 " inherited type-bound procedure",
8885 c->name, sym->name, &c->loc);
8889 if (c->ts.type == BT_CHARACTER)
8891 if (c->ts.cl->length == NULL
8892 || (resolve_charlen (c->ts.cl) == FAILURE)
8893 || !gfc_is_constant_expr (c->ts.cl->length))
8895 gfc_error ("Character length of component '%s' needs to "
8896 "be a constant specification expression at %L",
8898 c->ts.cl->length ? &c->ts.cl->length->where : &c->loc);
8903 if (c->ts.type == BT_DERIVED
8904 && sym->component_access != ACCESS_PRIVATE
8905 && gfc_check_access (sym->attr.access, sym->ns->default_access)
8906 && !c->ts.derived->attr.use_assoc
8907 && !gfc_check_access (c->ts.derived->attr.access,
8908 c->ts.derived->ns->default_access))
8910 gfc_error ("The component '%s' is a PRIVATE type and cannot be "
8911 "a component of '%s', which is PUBLIC at %L",
8912 c->name, sym->name, &sym->declared_at);
8916 if (sym->attr.sequence)
8918 if (c->ts.type == BT_DERIVED && c->ts.derived->attr.sequence == 0)
8920 gfc_error ("Component %s of SEQUENCE type declared at %L does "
8921 "not have the SEQUENCE attribute",
8922 c->ts.derived->name, &sym->declared_at);
8927 if (c->ts.type == BT_DERIVED && c->attr.pointer
8928 && c->ts.derived->components == NULL
8929 && !c->ts.derived->attr.zero_comp)
8931 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
8932 "that has not been declared", c->name, sym->name,
8937 /* Ensure that all the derived type components are put on the
8938 derived type list; even in formal namespaces, where derived type
8939 pointer components might not have been declared. */
8940 if (c->ts.type == BT_DERIVED
8942 && c->ts.derived->components
8944 && sym != c->ts.derived)
8945 add_dt_to_dt_list (c->ts.derived);
8947 if (c->attr.pointer || c->attr.allocatable || c->as == NULL)
8950 for (i = 0; i < c->as->rank; i++)
8952 if (c->as->lower[i] == NULL
8953 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
8954 || !gfc_is_constant_expr (c->as->lower[i])
8955 || c->as->upper[i] == NULL
8956 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
8957 || !gfc_is_constant_expr (c->as->upper[i]))
8959 gfc_error ("Component '%s' of '%s' at %L must have "
8960 "constant array bounds",
8961 c->name, sym->name, &c->loc);
8967 /* Resolve the type-bound procedures. */
8968 if (resolve_typebound_procedures (sym) == FAILURE)
8971 /* Resolve the finalizer procedures. */
8972 if (gfc_resolve_finalizers (sym) == FAILURE)
8975 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
8976 all DEFERRED bindings are overridden. */
8977 if (super_type && super_type->attr.abstract && !sym->attr.abstract
8978 && ensure_not_abstract (sym, super_type) == FAILURE)
8981 /* Add derived type to the derived type list. */
8982 add_dt_to_dt_list (sym);
8989 resolve_fl_namelist (gfc_symbol *sym)
8994 /* Reject PRIVATE objects in a PUBLIC namelist. */
8995 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
8997 for (nl = sym->namelist; nl; nl = nl->next)
8999 if (!nl->sym->attr.use_assoc
9000 && !(sym->ns->parent == nl->sym->ns)
9001 && !(sym->ns->parent
9002 && sym->ns->parent->parent == nl->sym->ns)
9003 && !gfc_check_access(nl->sym->attr.access,
9004 nl->sym->ns->default_access))
9006 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
9007 "cannot be member of PUBLIC namelist '%s' at %L",
9008 nl->sym->name, sym->name, &sym->declared_at);
9012 /* Types with private components that came here by USE-association. */
9013 if (nl->sym->ts.type == BT_DERIVED
9014 && derived_inaccessible (nl->sym->ts.derived))
9016 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
9017 "components and cannot be member of namelist '%s' at %L",
9018 nl->sym->name, sym->name, &sym->declared_at);
9022 /* Types with private components that are defined in the same module. */
9023 if (nl->sym->ts.type == BT_DERIVED
9024 && !(sym->ns->parent == nl->sym->ts.derived->ns)
9025 && !gfc_check_access (nl->sym->ts.derived->attr.private_comp
9026 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
9027 nl->sym->ns->default_access))
9029 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
9030 "cannot be a member of PUBLIC namelist '%s' at %L",
9031 nl->sym->name, sym->name, &sym->declared_at);
9037 for (nl = sym->namelist; nl; nl = nl->next)
9039 /* Reject namelist arrays of assumed shape. */
9040 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
9041 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
9042 "must not have assumed shape in namelist "
9043 "'%s' at %L", nl->sym->name, sym->name,
9044 &sym->declared_at) == FAILURE)
9047 /* Reject namelist arrays that are not constant shape. */
9048 if (is_non_constant_shape_array (nl->sym))
9050 gfc_error ("NAMELIST array object '%s' must have constant "
9051 "shape in namelist '%s' at %L", nl->sym->name,
9052 sym->name, &sym->declared_at);
9056 /* Namelist objects cannot have allocatable or pointer components. */
9057 if (nl->sym->ts.type != BT_DERIVED)
9060 if (nl->sym->ts.derived->attr.alloc_comp)
9062 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
9063 "have ALLOCATABLE components",
9064 nl->sym->name, sym->name, &sym->declared_at);
9068 if (nl->sym->ts.derived->attr.pointer_comp)
9070 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
9071 "have POINTER components",
9072 nl->sym->name, sym->name, &sym->declared_at);
9078 /* 14.1.2 A module or internal procedure represent local entities
9079 of the same type as a namelist member and so are not allowed. */
9080 for (nl = sym->namelist; nl; nl = nl->next)
9082 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
9085 if (nl->sym->attr.function && nl->sym == nl->sym->result)
9086 if ((nl->sym == sym->ns->proc_name)
9088 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
9092 if (nl->sym && nl->sym->name)
9093 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
9094 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
9096 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
9097 "attribute in '%s' at %L", nlsym->name,
9108 resolve_fl_parameter (gfc_symbol *sym)
9110 /* A parameter array's shape needs to be constant. */
9112 && (sym->as->type == AS_DEFERRED
9113 || is_non_constant_shape_array (sym)))
9115 gfc_error ("Parameter array '%s' at %L cannot be automatic "
9116 "or of deferred shape", sym->name, &sym->declared_at);
9120 /* Make sure a parameter that has been implicitly typed still
9121 matches the implicit type, since PARAMETER statements can precede
9122 IMPLICIT statements. */
9123 if (sym->attr.implicit_type
9124 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym, sym->ns)))
9126 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
9127 "later IMPLICIT type", sym->name, &sym->declared_at);
9131 /* Make sure the types of derived parameters are consistent. This
9132 type checking is deferred until resolution because the type may
9133 refer to a derived type from the host. */
9134 if (sym->ts.type == BT_DERIVED
9135 && !gfc_compare_types (&sym->ts, &sym->value->ts))
9137 gfc_error ("Incompatible derived type in PARAMETER at %L",
9138 &sym->value->where);
9145 /* Do anything necessary to resolve a symbol. Right now, we just
9146 assume that an otherwise unknown symbol is a variable. This sort
9147 of thing commonly happens for symbols in module. */
9150 resolve_symbol (gfc_symbol *sym)
9152 int check_constant, mp_flag;
9153 gfc_symtree *symtree;
9154 gfc_symtree *this_symtree;
9158 if (sym->attr.flavor == FL_UNKNOWN)
9161 /* If we find that a flavorless symbol is an interface in one of the
9162 parent namespaces, find its symtree in this namespace, free the
9163 symbol and set the symtree to point to the interface symbol. */
9164 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
9166 symtree = gfc_find_symtree (ns->sym_root, sym->name);
9167 if (symtree && symtree->n.sym->generic)
9169 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
9173 gfc_free_symbol (sym);
9174 symtree->n.sym->refs++;
9175 this_symtree->n.sym = symtree->n.sym;
9180 /* Otherwise give it a flavor according to such attributes as
9182 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
9183 sym->attr.flavor = FL_VARIABLE;
9186 sym->attr.flavor = FL_PROCEDURE;
9187 if (sym->attr.dimension)
9188 sym->attr.function = 1;
9192 if (sym->attr.procedure && sym->ts.interface
9193 && sym->attr.if_source != IFSRC_DECL)
9195 if (sym->ts.interface->attr.procedure)
9196 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared "
9197 "in a later PROCEDURE statement", sym->ts.interface->name,
9198 sym->name,&sym->declared_at);
9200 /* Get the attributes from the interface (now resolved). */
9201 if (sym->ts.interface->attr.if_source || sym->ts.interface->attr.intrinsic)
9203 gfc_symbol *ifc = sym->ts.interface;
9205 sym->ts.interface = ifc;
9206 sym->attr.function = ifc->attr.function;
9207 sym->attr.subroutine = ifc->attr.subroutine;
9208 sym->attr.allocatable = ifc->attr.allocatable;
9209 sym->attr.pointer = ifc->attr.pointer;
9210 sym->attr.pure = ifc->attr.pure;
9211 sym->attr.elemental = ifc->attr.elemental;
9212 sym->attr.dimension = ifc->attr.dimension;
9213 sym->attr.recursive = ifc->attr.recursive;
9214 sym->attr.always_explicit = ifc->attr.always_explicit;
9215 copy_formal_args (sym, ifc);
9216 /* Copy array spec. */
9217 sym->as = gfc_copy_array_spec (ifc->as);
9221 for (i = 0; i < sym->as->rank; i++)
9223 gfc_expr_replace_symbols (sym->as->lower[i], sym);
9224 gfc_expr_replace_symbols (sym->as->upper[i], sym);
9227 /* Copy char length. */
9230 sym->ts.cl = gfc_get_charlen();
9231 sym->ts.cl->resolved = ifc->ts.cl->resolved;
9232 sym->ts.cl->length = gfc_copy_expr (ifc->ts.cl->length);
9233 gfc_expr_replace_symbols (sym->ts.cl->length, sym);
9234 /* Add charlen to namespace. */
9237 sym->ts.cl->next = sym->formal_ns->cl_list;
9238 sym->formal_ns->cl_list = sym->ts.cl;
9242 else if (sym->ts.interface->name[0] != '\0')
9244 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
9245 sym->ts.interface->name, sym->name, &sym->declared_at);
9250 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
9253 /* Symbols that are module procedures with results (functions) have
9254 the types and array specification copied for type checking in
9255 procedures that call them, as well as for saving to a module
9256 file. These symbols can't stand the scrutiny that their results
9258 mp_flag = (sym->result != NULL && sym->result != sym);
9261 /* Make sure that the intrinsic is consistent with its internal
9262 representation. This needs to be done before assigning a default
9263 type to avoid spurious warnings. */
9264 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic)
9266 gfc_intrinsic_sym* isym;
9269 /* We already know this one is an intrinsic, so we don't call
9270 gfc_is_intrinsic for full checking but rather use gfc_find_function and
9271 gfc_find_subroutine directly to check whether it is a function or
9274 if ((isym = gfc_find_function (sym->name)))
9276 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising)
9277 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
9278 " ignored", sym->name, &sym->declared_at);
9280 else if ((isym = gfc_find_subroutine (sym->name)))
9282 if (sym->ts.type != BT_UNKNOWN)
9284 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
9285 " specifier", sym->name, &sym->declared_at);
9291 gfc_error ("'%s' declared INTRINSIC at %L does not exist",
9292 sym->name, &sym->declared_at);
9296 /* Check it is actually available in the standard settings. */
9297 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
9300 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
9301 " available in the current standard settings but %s. Use"
9302 " an appropriate -std=* option or enable -fall-intrinsics"
9303 " in order to use it.",
9304 sym->name, &sym->declared_at, symstd);
9309 /* Assign default type to symbols that need one and don't have one. */
9310 if (sym->ts.type == BT_UNKNOWN)
9312 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
9313 gfc_set_default_type (sym, 1, NULL);
9315 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
9317 /* The specific case of an external procedure should emit an error
9318 in the case that there is no implicit type. */
9320 gfc_set_default_type (sym, sym->attr.external, NULL);
9323 /* Result may be in another namespace. */
9324 resolve_symbol (sym->result);
9326 sym->ts = sym->result->ts;
9327 sym->as = gfc_copy_array_spec (sym->result->as);
9328 sym->attr.dimension = sym->result->attr.dimension;
9329 sym->attr.pointer = sym->result->attr.pointer;
9330 sym->attr.allocatable = sym->result->attr.allocatable;
9335 /* Assumed size arrays and assumed shape arrays must be dummy
9339 && (sym->as->type == AS_ASSUMED_SIZE
9340 || sym->as->type == AS_ASSUMED_SHAPE)
9341 && sym->attr.dummy == 0)
9343 if (sym->as->type == AS_ASSUMED_SIZE)
9344 gfc_error ("Assumed size array at %L must be a dummy argument",
9347 gfc_error ("Assumed shape array at %L must be a dummy argument",
9352 /* Make sure symbols with known intent or optional are really dummy
9353 variable. Because of ENTRY statement, this has to be deferred
9354 until resolution time. */
9356 if (!sym->attr.dummy
9357 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
9359 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
9363 if (sym->attr.value && !sym->attr.dummy)
9365 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
9366 "it is not a dummy argument", sym->name, &sym->declared_at);
9370 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
9372 gfc_charlen *cl = sym->ts.cl;
9373 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
9375 gfc_error ("Character dummy variable '%s' at %L with VALUE "
9376 "attribute must have constant length",
9377 sym->name, &sym->declared_at);
9381 if (sym->ts.is_c_interop
9382 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
9384 gfc_error ("C interoperable character dummy variable '%s' at %L "
9385 "with VALUE attribute must have length one",
9386 sym->name, &sym->declared_at);
9391 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
9392 do this for something that was implicitly typed because that is handled
9393 in gfc_set_default_type. Handle dummy arguments and procedure
9394 definitions separately. Also, anything that is use associated is not
9395 handled here but instead is handled in the module it is declared in.
9396 Finally, derived type definitions are allowed to be BIND(C) since that
9397 only implies that they're interoperable, and they are checked fully for
9398 interoperability when a variable is declared of that type. */
9399 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
9400 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
9401 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
9403 gfc_try t = SUCCESS;
9405 /* First, make sure the variable is declared at the
9406 module-level scope (J3/04-007, Section 15.3). */
9407 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
9408 sym->attr.in_common == 0)
9410 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
9411 "is neither a COMMON block nor declared at the "
9412 "module level scope", sym->name, &(sym->declared_at));
9415 else if (sym->common_head != NULL)
9417 t = verify_com_block_vars_c_interop (sym->common_head);
9421 /* If type() declaration, we need to verify that the components
9422 of the given type are all C interoperable, etc. */
9423 if (sym->ts.type == BT_DERIVED &&
9424 sym->ts.derived->attr.is_c_interop != 1)
9426 /* Make sure the user marked the derived type as BIND(C). If
9427 not, call the verify routine. This could print an error
9428 for the derived type more than once if multiple variables
9429 of that type are declared. */
9430 if (sym->ts.derived->attr.is_bind_c != 1)
9431 verify_bind_c_derived_type (sym->ts.derived);
9435 /* Verify the variable itself as C interoperable if it
9436 is BIND(C). It is not possible for this to succeed if
9437 the verify_bind_c_derived_type failed, so don't have to handle
9438 any error returned by verify_bind_c_derived_type. */
9439 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
9445 /* clear the is_bind_c flag to prevent reporting errors more than
9446 once if something failed. */
9447 sym->attr.is_bind_c = 0;
9452 /* If a derived type symbol has reached this point, without its
9453 type being declared, we have an error. Notice that most
9454 conditions that produce undefined derived types have already
9455 been dealt with. However, the likes of:
9456 implicit type(t) (t) ..... call foo (t) will get us here if
9457 the type is not declared in the scope of the implicit
9458 statement. Change the type to BT_UNKNOWN, both because it is so
9459 and to prevent an ICE. */
9460 if (sym->ts.type == BT_DERIVED && sym->ts.derived->components == NULL
9461 && !sym->ts.derived->attr.zero_comp)
9463 gfc_error ("The derived type '%s' at %L is of type '%s', "
9464 "which has not been defined", sym->name,
9465 &sym->declared_at, sym->ts.derived->name);
9466 sym->ts.type = BT_UNKNOWN;
9470 /* Make sure that the derived type has been resolved and that the
9471 derived type is visible in the symbol's namespace, if it is a
9472 module function and is not PRIVATE. */
9473 if (sym->ts.type == BT_DERIVED
9474 && sym->ts.derived->attr.use_assoc
9475 && sym->ns->proc_name
9476 && sym->ns->proc_name->attr.flavor == FL_MODULE)
9480 if (resolve_fl_derived (sym->ts.derived) == FAILURE)
9483 gfc_find_symbol (sym->ts.derived->name, sym->ns, 1, &ds);
9484 if (!ds && sym->attr.function
9485 && gfc_check_access (sym->attr.access, sym->ns->default_access))
9487 symtree = gfc_new_symtree (&sym->ns->sym_root,
9488 sym->ts.derived->name);
9489 symtree->n.sym = sym->ts.derived;
9490 sym->ts.derived->refs++;
9494 /* Unless the derived-type declaration is use associated, Fortran 95
9495 does not allow public entries of private derived types.
9496 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
9498 if (sym->ts.type == BT_DERIVED
9499 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
9500 && !sym->ts.derived->attr.use_assoc
9501 && gfc_check_access (sym->attr.access, sym->ns->default_access)
9502 && !gfc_check_access (sym->ts.derived->attr.access,
9503 sym->ts.derived->ns->default_access)
9504 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
9505 "of PRIVATE derived type '%s'",
9506 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
9507 : "variable", sym->name, &sym->declared_at,
9508 sym->ts.derived->name) == FAILURE)
9511 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
9512 default initialization is defined (5.1.2.4.4). */
9513 if (sym->ts.type == BT_DERIVED
9515 && sym->attr.intent == INTENT_OUT
9517 && sym->as->type == AS_ASSUMED_SIZE)
9519 for (c = sym->ts.derived->components; c; c = c->next)
9523 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
9524 "ASSUMED SIZE and so cannot have a default initializer",
9525 sym->name, &sym->declared_at);
9531 switch (sym->attr.flavor)
9534 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
9539 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
9544 if (resolve_fl_namelist (sym) == FAILURE)
9549 if (resolve_fl_parameter (sym) == FAILURE)
9557 /* Resolve array specifier. Check as well some constraints
9558 on COMMON blocks. */
9560 check_constant = sym->attr.in_common && !sym->attr.pointer;
9562 /* Set the formal_arg_flag so that check_conflict will not throw
9563 an error for host associated variables in the specification
9564 expression for an array_valued function. */
9565 if (sym->attr.function && sym->as)
9566 formal_arg_flag = 1;
9568 gfc_resolve_array_spec (sym->as, check_constant);
9570 formal_arg_flag = 0;
9572 /* Resolve formal namespaces. */
9573 if (sym->formal_ns && sym->formal_ns != gfc_current_ns)
9574 gfc_resolve (sym->formal_ns);
9576 /* Check threadprivate restrictions. */
9577 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
9578 && (!sym->attr.in_common
9579 && sym->module == NULL
9580 && (sym->ns->proc_name == NULL
9581 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
9582 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
9584 /* If we have come this far we can apply default-initializers, as
9585 described in 14.7.5, to those variables that have not already
9586 been assigned one. */
9587 if (sym->ts.type == BT_DERIVED
9588 && sym->attr.referenced
9589 && sym->ns == gfc_current_ns
9591 && !sym->attr.allocatable
9592 && !sym->attr.alloc_comp)
9594 symbol_attribute *a = &sym->attr;
9596 if ((!a->save && !a->dummy && !a->pointer
9597 && !a->in_common && !a->use_assoc
9598 && !(a->function && sym != sym->result))
9599 || (a->dummy && a->intent == INTENT_OUT))
9600 apply_default_init (sym);
9603 /* If this symbol has a type-spec, check it. */
9604 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
9605 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
9606 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
9612 /************* Resolve DATA statements *************/
9616 gfc_data_value *vnode;
9622 /* Advance the values structure to point to the next value in the data list. */
9625 next_data_value (void)
9628 while (mpz_cmp_ui (values.left, 0) == 0)
9630 if (values.vnode->next == NULL)
9633 values.vnode = values.vnode->next;
9634 mpz_set (values.left, values.vnode->repeat);
9642 check_data_variable (gfc_data_variable *var, locus *where)
9648 ar_type mark = AR_UNKNOWN;
9650 mpz_t section_index[GFC_MAX_DIMENSIONS];
9656 if (gfc_resolve_expr (var->expr) == FAILURE)
9660 mpz_init_set_si (offset, 0);
9663 if (e->expr_type != EXPR_VARIABLE)
9664 gfc_internal_error ("check_data_variable(): Bad expression");
9666 sym = e->symtree->n.sym;
9668 if (sym->ns->is_block_data && !sym->attr.in_common)
9670 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
9671 sym->name, &sym->declared_at);
9674 if (e->ref == NULL && sym->as)
9676 gfc_error ("DATA array '%s' at %L must be specified in a previous"
9677 " declaration", sym->name, where);
9681 has_pointer = sym->attr.pointer;
9683 for (ref = e->ref; ref; ref = ref->next)
9685 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
9689 && ref->type == REF_ARRAY
9690 && ref->u.ar.type != AR_FULL)
9692 gfc_error ("DATA element '%s' at %L is a pointer and so must "
9693 "be a full array", sym->name, where);
9698 if (e->rank == 0 || has_pointer)
9700 mpz_init_set_ui (size, 1);
9707 /* Find the array section reference. */
9708 for (ref = e->ref; ref; ref = ref->next)
9710 if (ref->type != REF_ARRAY)
9712 if (ref->u.ar.type == AR_ELEMENT)
9718 /* Set marks according to the reference pattern. */
9719 switch (ref->u.ar.type)
9727 /* Get the start position of array section. */
9728 gfc_get_section_index (ar, section_index, &offset);
9736 if (gfc_array_size (e, &size) == FAILURE)
9738 gfc_error ("Nonconstant array section at %L in DATA statement",
9747 while (mpz_cmp_ui (size, 0) > 0)
9749 if (next_data_value () == FAILURE)
9751 gfc_error ("DATA statement at %L has more variables than values",
9757 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
9761 /* If we have more than one element left in the repeat count,
9762 and we have more than one element left in the target variable,
9763 then create a range assignment. */
9764 /* FIXME: Only done for full arrays for now, since array sections
9766 if (mark == AR_FULL && ref && ref->next == NULL
9767 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
9771 if (mpz_cmp (size, values.left) >= 0)
9773 mpz_init_set (range, values.left);
9774 mpz_sub (size, size, values.left);
9775 mpz_set_ui (values.left, 0);
9779 mpz_init_set (range, size);
9780 mpz_sub (values.left, values.left, size);
9781 mpz_set_ui (size, 0);
9784 gfc_assign_data_value_range (var->expr, values.vnode->expr,
9787 mpz_add (offset, offset, range);
9791 /* Assign initial value to symbol. */
9794 mpz_sub_ui (values.left, values.left, 1);
9795 mpz_sub_ui (size, size, 1);
9797 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
9801 if (mark == AR_FULL)
9802 mpz_add_ui (offset, offset, 1);
9804 /* Modify the array section indexes and recalculate the offset
9805 for next element. */
9806 else if (mark == AR_SECTION)
9807 gfc_advance_section (section_index, ar, &offset);
9811 if (mark == AR_SECTION)
9813 for (i = 0; i < ar->dimen; i++)
9814 mpz_clear (section_index[i]);
9824 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
9826 /* Iterate over a list of elements in a DATA statement. */
9829 traverse_data_list (gfc_data_variable *var, locus *where)
9832 iterator_stack frame;
9833 gfc_expr *e, *start, *end, *step;
9834 gfc_try retval = SUCCESS;
9836 mpz_init (frame.value);
9838 start = gfc_copy_expr (var->iter.start);
9839 end = gfc_copy_expr (var->iter.end);
9840 step = gfc_copy_expr (var->iter.step);
9842 if (gfc_simplify_expr (start, 1) == FAILURE
9843 || start->expr_type != EXPR_CONSTANT)
9845 gfc_error ("iterator start at %L does not simplify", &start->where);
9849 if (gfc_simplify_expr (end, 1) == FAILURE
9850 || end->expr_type != EXPR_CONSTANT)
9852 gfc_error ("iterator end at %L does not simplify", &end->where);
9856 if (gfc_simplify_expr (step, 1) == FAILURE
9857 || step->expr_type != EXPR_CONSTANT)
9859 gfc_error ("iterator step at %L does not simplify", &step->where);
9864 mpz_init_set (trip, end->value.integer);
9865 mpz_sub (trip, trip, start->value.integer);
9866 mpz_add (trip, trip, step->value.integer);
9868 mpz_div (trip, trip, step->value.integer);
9870 mpz_set (frame.value, start->value.integer);
9872 frame.prev = iter_stack;
9873 frame.variable = var->iter.var->symtree;
9874 iter_stack = &frame;
9876 while (mpz_cmp_ui (trip, 0) > 0)
9878 if (traverse_data_var (var->list, where) == FAILURE)
9885 e = gfc_copy_expr (var->expr);
9886 if (gfc_simplify_expr (e, 1) == FAILURE)
9894 mpz_add (frame.value, frame.value, step->value.integer);
9896 mpz_sub_ui (trip, trip, 1);
9901 mpz_clear (frame.value);
9903 gfc_free_expr (start);
9904 gfc_free_expr (end);
9905 gfc_free_expr (step);
9907 iter_stack = frame.prev;
9912 /* Type resolve variables in the variable list of a DATA statement. */
9915 traverse_data_var (gfc_data_variable *var, locus *where)
9919 for (; var; var = var->next)
9921 if (var->expr == NULL)
9922 t = traverse_data_list (var, where);
9924 t = check_data_variable (var, where);
9934 /* Resolve the expressions and iterators associated with a data statement.
9935 This is separate from the assignment checking because data lists should
9936 only be resolved once. */
9939 resolve_data_variables (gfc_data_variable *d)
9941 for (; d; d = d->next)
9943 if (d->list == NULL)
9945 if (gfc_resolve_expr (d->expr) == FAILURE)
9950 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
9953 if (resolve_data_variables (d->list) == FAILURE)
9962 /* Resolve a single DATA statement. We implement this by storing a pointer to
9963 the value list into static variables, and then recursively traversing the
9964 variables list, expanding iterators and such. */
9967 resolve_data (gfc_data *d)
9970 if (resolve_data_variables (d->var) == FAILURE)
9973 values.vnode = d->value;
9974 if (d->value == NULL)
9975 mpz_set_ui (values.left, 0);
9977 mpz_set (values.left, d->value->repeat);
9979 if (traverse_data_var (d->var, &d->where) == FAILURE)
9982 /* At this point, we better not have any values left. */
9984 if (next_data_value () == SUCCESS)
9985 gfc_error ("DATA statement at %L has more values than variables",
9990 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
9991 accessed by host or use association, is a dummy argument to a pure function,
9992 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
9993 is storage associated with any such variable, shall not be used in the
9994 following contexts: (clients of this function). */
9996 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
9997 procedure. Returns zero if assignment is OK, nonzero if there is a
10000 gfc_impure_variable (gfc_symbol *sym)
10004 if (sym->attr.use_assoc || sym->attr.in_common)
10007 if (sym->ns != gfc_current_ns)
10008 return !sym->attr.function;
10010 proc = sym->ns->proc_name;
10011 if (sym->attr.dummy && gfc_pure (proc)
10012 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
10014 proc->attr.function))
10017 /* TODO: Sort out what can be storage associated, if anything, and include
10018 it here. In principle equivalences should be scanned but it does not
10019 seem to be possible to storage associate an impure variable this way. */
10024 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
10025 symbol of the current procedure. */
10028 gfc_pure (gfc_symbol *sym)
10030 symbol_attribute attr;
10033 sym = gfc_current_ns->proc_name;
10039 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
10043 /* Test whether the current procedure is elemental or not. */
10046 gfc_elemental (gfc_symbol *sym)
10048 symbol_attribute attr;
10051 sym = gfc_current_ns->proc_name;
10056 return attr.flavor == FL_PROCEDURE && attr.elemental;
10060 /* Warn about unused labels. */
10063 warn_unused_fortran_label (gfc_st_label *label)
10068 warn_unused_fortran_label (label->left);
10070 if (label->defined == ST_LABEL_UNKNOWN)
10073 switch (label->referenced)
10075 case ST_LABEL_UNKNOWN:
10076 gfc_warning ("Label %d at %L defined but not used", label->value,
10080 case ST_LABEL_BAD_TARGET:
10081 gfc_warning ("Label %d at %L defined but cannot be used",
10082 label->value, &label->where);
10089 warn_unused_fortran_label (label->right);
10093 /* Returns the sequence type of a symbol or sequence. */
10096 sequence_type (gfc_typespec ts)
10105 if (ts.derived->components == NULL)
10106 return SEQ_NONDEFAULT;
10108 result = sequence_type (ts.derived->components->ts);
10109 for (c = ts.derived->components->next; c; c = c->next)
10110 if (sequence_type (c->ts) != result)
10116 if (ts.kind != gfc_default_character_kind)
10117 return SEQ_NONDEFAULT;
10119 return SEQ_CHARACTER;
10122 if (ts.kind != gfc_default_integer_kind)
10123 return SEQ_NONDEFAULT;
10125 return SEQ_NUMERIC;
10128 if (!(ts.kind == gfc_default_real_kind
10129 || ts.kind == gfc_default_double_kind))
10130 return SEQ_NONDEFAULT;
10132 return SEQ_NUMERIC;
10135 if (ts.kind != gfc_default_complex_kind)
10136 return SEQ_NONDEFAULT;
10138 return SEQ_NUMERIC;
10141 if (ts.kind != gfc_default_logical_kind)
10142 return SEQ_NONDEFAULT;
10144 return SEQ_NUMERIC;
10147 return SEQ_NONDEFAULT;
10152 /* Resolve derived type EQUIVALENCE object. */
10155 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
10158 gfc_component *c = derived->components;
10163 /* Shall not be an object of nonsequence derived type. */
10164 if (!derived->attr.sequence)
10166 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
10167 "attribute to be an EQUIVALENCE object", sym->name,
10172 /* Shall not have allocatable components. */
10173 if (derived->attr.alloc_comp)
10175 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
10176 "components to be an EQUIVALENCE object",sym->name,
10181 if (sym->attr.in_common && has_default_initializer (sym->ts.derived))
10183 gfc_error ("Derived type variable '%s' at %L with default "
10184 "initialization cannot be in EQUIVALENCE with a variable "
10185 "in COMMON", sym->name, &e->where);
10189 for (; c ; c = c->next)
10193 && (resolve_equivalence_derived (c->ts.derived, sym, e) == FAILURE))
10196 /* Shall not be an object of sequence derived type containing a pointer
10197 in the structure. */
10198 if (c->attr.pointer)
10200 gfc_error ("Derived type variable '%s' at %L with pointer "
10201 "component(s) cannot be an EQUIVALENCE object",
10202 sym->name, &e->where);
10210 /* Resolve equivalence object.
10211 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
10212 an allocatable array, an object of nonsequence derived type, an object of
10213 sequence derived type containing a pointer at any level of component
10214 selection, an automatic object, a function name, an entry name, a result
10215 name, a named constant, a structure component, or a subobject of any of
10216 the preceding objects. A substring shall not have length zero. A
10217 derived type shall not have components with default initialization nor
10218 shall two objects of an equivalence group be initialized.
10219 Either all or none of the objects shall have an protected attribute.
10220 The simple constraints are done in symbol.c(check_conflict) and the rest
10221 are implemented here. */
10224 resolve_equivalence (gfc_equiv *eq)
10227 gfc_symbol *derived;
10228 gfc_symbol *first_sym;
10231 locus *last_where = NULL;
10232 seq_type eq_type, last_eq_type;
10233 gfc_typespec *last_ts;
10234 int object, cnt_protected;
10235 const char *value_name;
10239 last_ts = &eq->expr->symtree->n.sym->ts;
10241 first_sym = eq->expr->symtree->n.sym;
10245 for (object = 1; eq; eq = eq->eq, object++)
10249 e->ts = e->symtree->n.sym->ts;
10250 /* match_varspec might not know yet if it is seeing
10251 array reference or substring reference, as it doesn't
10253 if (e->ref && e->ref->type == REF_ARRAY)
10255 gfc_ref *ref = e->ref;
10256 sym = e->symtree->n.sym;
10258 if (sym->attr.dimension)
10260 ref->u.ar.as = sym->as;
10264 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
10265 if (e->ts.type == BT_CHARACTER
10267 && ref->type == REF_ARRAY
10268 && ref->u.ar.dimen == 1
10269 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
10270 && ref->u.ar.stride[0] == NULL)
10272 gfc_expr *start = ref->u.ar.start[0];
10273 gfc_expr *end = ref->u.ar.end[0];
10276 /* Optimize away the (:) reference. */
10277 if (start == NULL && end == NULL)
10280 e->ref = ref->next;
10282 e->ref->next = ref->next;
10287 ref->type = REF_SUBSTRING;
10289 start = gfc_int_expr (1);
10290 ref->u.ss.start = start;
10291 if (end == NULL && e->ts.cl)
10292 end = gfc_copy_expr (e->ts.cl->length);
10293 ref->u.ss.end = end;
10294 ref->u.ss.length = e->ts.cl;
10301 /* Any further ref is an error. */
10304 gcc_assert (ref->type == REF_ARRAY);
10305 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
10311 if (gfc_resolve_expr (e) == FAILURE)
10314 sym = e->symtree->n.sym;
10316 if (sym->attr.is_protected)
10318 if (cnt_protected > 0 && cnt_protected != object)
10320 gfc_error ("Either all or none of the objects in the "
10321 "EQUIVALENCE set at %L shall have the "
10322 "PROTECTED attribute",
10327 /* Shall not equivalence common block variables in a PURE procedure. */
10328 if (sym->ns->proc_name
10329 && sym->ns->proc_name->attr.pure
10330 && sym->attr.in_common)
10332 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
10333 "object in the pure procedure '%s'",
10334 sym->name, &e->where, sym->ns->proc_name->name);
10338 /* Shall not be a named constant. */
10339 if (e->expr_type == EXPR_CONSTANT)
10341 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
10342 "object", sym->name, &e->where);
10346 derived = e->ts.derived;
10347 if (derived && resolve_equivalence_derived (derived, sym, e) == FAILURE)
10350 /* Check that the types correspond correctly:
10352 A numeric sequence structure may be equivalenced to another sequence
10353 structure, an object of default integer type, default real type, double
10354 precision real type, default logical type such that components of the
10355 structure ultimately only become associated to objects of the same
10356 kind. A character sequence structure may be equivalenced to an object
10357 of default character kind or another character sequence structure.
10358 Other objects may be equivalenced only to objects of the same type and
10359 kind parameters. */
10361 /* Identical types are unconditionally OK. */
10362 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
10363 goto identical_types;
10365 last_eq_type = sequence_type (*last_ts);
10366 eq_type = sequence_type (sym->ts);
10368 /* Since the pair of objects is not of the same type, mixed or
10369 non-default sequences can be rejected. */
10371 msg = "Sequence %s with mixed components in EQUIVALENCE "
10372 "statement at %L with different type objects";
10374 && last_eq_type == SEQ_MIXED
10375 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
10377 || (eq_type == SEQ_MIXED
10378 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
10379 &e->where) == FAILURE))
10382 msg = "Non-default type object or sequence %s in EQUIVALENCE "
10383 "statement at %L with objects of different type";
10385 && last_eq_type == SEQ_NONDEFAULT
10386 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
10387 last_where) == FAILURE)
10388 || (eq_type == SEQ_NONDEFAULT
10389 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
10390 &e->where) == FAILURE))
10393 msg ="Non-CHARACTER object '%s' in default CHARACTER "
10394 "EQUIVALENCE statement at %L";
10395 if (last_eq_type == SEQ_CHARACTER
10396 && eq_type != SEQ_CHARACTER
10397 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
10398 &e->where) == FAILURE)
10401 msg ="Non-NUMERIC object '%s' in default NUMERIC "
10402 "EQUIVALENCE statement at %L";
10403 if (last_eq_type == SEQ_NUMERIC
10404 && eq_type != SEQ_NUMERIC
10405 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
10406 &e->where) == FAILURE)
10411 last_where = &e->where;
10416 /* Shall not be an automatic array. */
10417 if (e->ref->type == REF_ARRAY
10418 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
10420 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
10421 "an EQUIVALENCE object", sym->name, &e->where);
10428 /* Shall not be a structure component. */
10429 if (r->type == REF_COMPONENT)
10431 gfc_error ("Structure component '%s' at %L cannot be an "
10432 "EQUIVALENCE object",
10433 r->u.c.component->name, &e->where);
10437 /* A substring shall not have length zero. */
10438 if (r->type == REF_SUBSTRING)
10440 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
10442 gfc_error ("Substring at %L has length zero",
10443 &r->u.ss.start->where);
10453 /* Resolve function and ENTRY types, issue diagnostics if needed. */
10456 resolve_fntype (gfc_namespace *ns)
10458 gfc_entry_list *el;
10461 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
10464 /* If there are any entries, ns->proc_name is the entry master
10465 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
10467 sym = ns->entries->sym;
10469 sym = ns->proc_name;
10470 if (sym->result == sym
10471 && sym->ts.type == BT_UNKNOWN
10472 && gfc_set_default_type (sym, 0, NULL) == FAILURE
10473 && !sym->attr.untyped)
10475 gfc_error ("Function '%s' at %L has no IMPLICIT type",
10476 sym->name, &sym->declared_at);
10477 sym->attr.untyped = 1;
10480 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.use_assoc
10481 && !sym->attr.contained
10482 && !gfc_check_access (sym->ts.derived->attr.access,
10483 sym->ts.derived->ns->default_access)
10484 && gfc_check_access (sym->attr.access, sym->ns->default_access))
10486 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
10487 "%L of PRIVATE type '%s'", sym->name,
10488 &sym->declared_at, sym->ts.derived->name);
10492 for (el = ns->entries->next; el; el = el->next)
10494 if (el->sym->result == el->sym
10495 && el->sym->ts.type == BT_UNKNOWN
10496 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
10497 && !el->sym->attr.untyped)
10499 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
10500 el->sym->name, &el->sym->declared_at);
10501 el->sym->attr.untyped = 1;
10506 /* 12.3.2.1.1 Defined operators. */
10509 gfc_resolve_uops (gfc_symtree *symtree)
10511 gfc_interface *itr;
10513 gfc_formal_arglist *formal;
10515 if (symtree == NULL)
10518 gfc_resolve_uops (symtree->left);
10519 gfc_resolve_uops (symtree->right);
10521 for (itr = symtree->n.uop->op; itr; itr = itr->next)
10524 if (!sym->attr.function)
10525 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
10526 sym->name, &sym->declared_at);
10528 if (sym->ts.type == BT_CHARACTER
10529 && !(sym->ts.cl && sym->ts.cl->length)
10530 && !(sym->result && sym->result->ts.cl
10531 && sym->result->ts.cl->length))
10532 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
10533 "character length", sym->name, &sym->declared_at);
10535 formal = sym->formal;
10536 if (!formal || !formal->sym)
10538 gfc_error ("User operator procedure '%s' at %L must have at least "
10539 "one argument", sym->name, &sym->declared_at);
10543 if (formal->sym->attr.intent != INTENT_IN)
10544 gfc_error ("First argument of operator interface at %L must be "
10545 "INTENT(IN)", &sym->declared_at);
10547 if (formal->sym->attr.optional)
10548 gfc_error ("First argument of operator interface at %L cannot be "
10549 "optional", &sym->declared_at);
10551 formal = formal->next;
10552 if (!formal || !formal->sym)
10555 if (formal->sym->attr.intent != INTENT_IN)
10556 gfc_error ("Second argument of operator interface at %L must be "
10557 "INTENT(IN)", &sym->declared_at);
10559 if (formal->sym->attr.optional)
10560 gfc_error ("Second argument of operator interface at %L cannot be "
10561 "optional", &sym->declared_at);
10564 gfc_error ("Operator interface at %L must have, at most, two "
10565 "arguments", &sym->declared_at);
10570 /* Examine all of the expressions associated with a program unit,
10571 assign types to all intermediate expressions, make sure that all
10572 assignments are to compatible types and figure out which names
10573 refer to which functions or subroutines. It doesn't check code
10574 block, which is handled by resolve_code. */
10577 resolve_types (gfc_namespace *ns)
10583 gfc_namespace* old_ns = gfc_current_ns;
10585 /* Check that all IMPLICIT types are ok. */
10586 if (!ns->seen_implicit_none)
10589 for (letter = 0; letter != GFC_LETTERS; ++letter)
10590 if (ns->set_flag[letter]
10591 && resolve_typespec_used (&ns->default_type[letter],
10592 &ns->implicit_loc[letter],
10597 gfc_current_ns = ns;
10599 resolve_entries (ns);
10601 resolve_common_vars (ns->blank_common.head, false);
10602 resolve_common_blocks (ns->common_root);
10604 resolve_contained_functions (ns);
10606 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
10608 for (cl = ns->cl_list; cl; cl = cl->next)
10609 resolve_charlen (cl);
10611 gfc_traverse_ns (ns, resolve_symbol);
10613 resolve_fntype (ns);
10615 for (n = ns->contained; n; n = n->sibling)
10617 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
10618 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
10619 "also be PURE", n->proc_name->name,
10620 &n->proc_name->declared_at);
10626 gfc_check_interfaces (ns);
10628 gfc_traverse_ns (ns, resolve_values);
10634 for (d = ns->data; d; d = d->next)
10638 gfc_traverse_ns (ns, gfc_formalize_init_value);
10640 gfc_traverse_ns (ns, gfc_verify_binding_labels);
10642 if (ns->common_root != NULL)
10643 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
10645 for (eq = ns->equiv; eq; eq = eq->next)
10646 resolve_equivalence (eq);
10648 /* Warn about unused labels. */
10649 if (warn_unused_label)
10650 warn_unused_fortran_label (ns->st_labels);
10652 gfc_resolve_uops (ns->uop_root);
10654 gfc_current_ns = old_ns;
10658 /* Call resolve_code recursively. */
10661 resolve_codes (gfc_namespace *ns)
10664 bitmap_obstack old_obstack;
10666 for (n = ns->contained; n; n = n->sibling)
10669 gfc_current_ns = ns;
10671 /* Set to an out of range value. */
10672 current_entry_id = -1;
10674 old_obstack = labels_obstack;
10675 bitmap_obstack_initialize (&labels_obstack);
10677 resolve_code (ns->code, ns);
10679 bitmap_obstack_release (&labels_obstack);
10680 labels_obstack = old_obstack;
10684 /* This function is called after a complete program unit has been compiled.
10685 Its purpose is to examine all of the expressions associated with a program
10686 unit, assign types to all intermediate expressions, make sure that all
10687 assignments are to compatible types and figure out which names refer to
10688 which functions or subroutines. */
10691 gfc_resolve (gfc_namespace *ns)
10693 gfc_namespace *old_ns;
10698 old_ns = gfc_current_ns;
10700 resolve_types (ns);
10701 resolve_codes (ns);
10703 gfc_current_ns = old_ns;