1 /* Perform type resolution on the various stuctures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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, *tail;
47 struct code_stack *prev;
49 /* This bitmap keeps track of the targets valid for a branch from
51 bitmap reachable_labels;
55 static code_stack *cs_base = NULL;
58 /* Nonzero if we're inside a FORALL block. */
60 static int forall_flag;
62 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
64 static int omp_workshare_flag;
66 /* Nonzero if we are processing a formal arglist. The corresponding function
67 resets the flag each time that it is read. */
68 static int formal_arg_flag = 0;
70 /* True if we are resolving a specification expression. */
71 static int specification_expr = 0;
73 /* The id of the last entry seen. */
74 static int current_entry_id;
76 /* We use bitmaps to determine if a branch target is valid. */
77 static bitmap_obstack labels_obstack;
80 gfc_is_formal_arg (void)
82 return formal_arg_flag;
85 /* Resolve types of formal argument lists. These have to be done early so that
86 the formal argument lists of module procedures can be copied to the
87 containing module before the individual procedures are resolved
88 individually. We also resolve argument lists of procedures in interface
89 blocks because they are self-contained scoping units.
91 Since a dummy argument cannot be a non-dummy procedure, the only
92 resort left for untyped names are the IMPLICIT types. */
95 resolve_formal_arglist (gfc_symbol *proc)
97 gfc_formal_arglist *f;
101 if (proc->result != NULL)
106 if (gfc_elemental (proc)
107 || sym->attr.pointer || sym->attr.allocatable
108 || (sym->as && sym->as->rank > 0))
110 proc->attr.always_explicit = 1;
111 sym->attr.always_explicit = 1;
116 for (f = proc->formal; f; f = f->next)
122 /* Alternate return placeholder. */
123 if (gfc_elemental (proc))
124 gfc_error ("Alternate return specifier in elemental subroutine "
125 "'%s' at %L is not allowed", proc->name,
127 if (proc->attr.function)
128 gfc_error ("Alternate return specifier in function "
129 "'%s' at %L is not allowed", proc->name,
134 if (sym->attr.if_source != IFSRC_UNKNOWN)
135 resolve_formal_arglist (sym);
137 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
139 if (gfc_pure (proc) && !gfc_pure (sym))
141 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
142 "also be PURE", sym->name, &sym->declared_at);
146 if (gfc_elemental (proc))
148 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
149 "procedure", &sym->declared_at);
153 if (sym->attr.function
154 && sym->ts.type == BT_UNKNOWN
155 && sym->attr.intrinsic)
157 gfc_intrinsic_sym *isym;
158 isym = gfc_find_function (sym->name);
159 if (isym == NULL || !isym->specific)
161 gfc_error ("Unable to find a specific INTRINSIC procedure "
162 "for the reference '%s' at %L", sym->name,
171 if (sym->ts.type == BT_UNKNOWN)
173 if (!sym->attr.function || sym->result == sym)
174 gfc_set_default_type (sym, 1, sym->ns);
177 gfc_resolve_array_spec (sym->as, 0);
179 /* We can't tell if an array with dimension (:) is assumed or deferred
180 shape until we know if it has the pointer or allocatable attributes.
182 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
183 && !(sym->attr.pointer || sym->attr.allocatable))
185 sym->as->type = AS_ASSUMED_SHAPE;
186 for (i = 0; i < sym->as->rank; i++)
187 sym->as->lower[i] = gfc_int_expr (1);
190 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
191 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
192 || sym->attr.optional)
194 proc->attr.always_explicit = 1;
196 proc->result->attr.always_explicit = 1;
199 /* If the flavor is unknown at this point, it has to be a variable.
200 A procedure specification would have already set the type. */
202 if (sym->attr.flavor == FL_UNKNOWN)
203 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
205 if (gfc_pure (proc) && !sym->attr.pointer
206 && sym->attr.flavor != FL_PROCEDURE)
208 if (proc->attr.function && sym->attr.intent != INTENT_IN)
209 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
210 "INTENT(IN)", sym->name, proc->name,
213 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
214 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
215 "have its INTENT specified", sym->name, proc->name,
219 if (gfc_elemental (proc))
223 gfc_error ("Argument '%s' of elemental procedure at %L must "
224 "be scalar", sym->name, &sym->declared_at);
228 if (sym->attr.pointer)
230 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
231 "have the POINTER attribute", sym->name,
236 if (sym->attr.flavor == FL_PROCEDURE)
238 gfc_error ("Dummy procedure '%s' not allowed in elemental "
239 "procedure '%s' at %L", sym->name, proc->name,
245 /* Each dummy shall be specified to be scalar. */
246 if (proc->attr.proc == PROC_ST_FUNCTION)
250 gfc_error ("Argument '%s' of statement function at %L must "
251 "be scalar", sym->name, &sym->declared_at);
255 if (sym->ts.type == BT_CHARACTER)
257 gfc_charlen *cl = sym->ts.cl;
258 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
260 gfc_error ("Character-valued argument '%s' of statement "
261 "function at %L must have constant length",
262 sym->name, &sym->declared_at);
272 /* Work function called when searching for symbols that have argument lists
273 associated with them. */
276 find_arglists (gfc_symbol *sym)
278 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
281 resolve_formal_arglist (sym);
285 /* Given a namespace, resolve all formal argument lists within the namespace.
289 resolve_formal_arglists (gfc_namespace *ns)
294 gfc_traverse_ns (ns, find_arglists);
299 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
303 /* If this namespace is not a function or an entry master function,
305 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
306 || sym->attr.entry_master)
309 /* Try to find out of what the return type is. */
310 if (sym->result->ts.type == BT_UNKNOWN)
312 t = gfc_set_default_type (sym->result, 0, ns);
314 if (t == FAILURE && !sym->result->attr.untyped)
316 if (sym->result == sym)
317 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
318 sym->name, &sym->declared_at);
320 gfc_error ("Result '%s' of contained function '%s' at %L has "
321 "no IMPLICIT type", sym->result->name, sym->name,
322 &sym->result->declared_at);
323 sym->result->attr.untyped = 1;
327 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
328 type, lists the only ways a character length value of * can be used:
329 dummy arguments of procedures, named constants, and function results
330 in external functions. Internal function results are not on that list;
331 ergo, not permitted. */
333 if (sym->result->ts.type == BT_CHARACTER)
335 gfc_charlen *cl = sym->result->ts.cl;
336 if (!cl || !cl->length)
337 gfc_error ("Character-valued internal function '%s' at %L must "
338 "not be assumed length", sym->name, &sym->declared_at);
343 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
344 introduce duplicates. */
347 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
349 gfc_formal_arglist *f, *new_arglist;
352 for (; new_args != NULL; new_args = new_args->next)
354 new_sym = new_args->sym;
355 /* See if this arg is already in the formal argument list. */
356 for (f = proc->formal; f; f = f->next)
358 if (new_sym == f->sym)
365 /* Add a new argument. Argument order is not important. */
366 new_arglist = gfc_get_formal_arglist ();
367 new_arglist->sym = new_sym;
368 new_arglist->next = proc->formal;
369 proc->formal = new_arglist;
374 /* Flag the arguments that are not present in all entries. */
377 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
379 gfc_formal_arglist *f, *head;
382 for (f = proc->formal; f; f = f->next)
387 for (new_args = head; new_args; new_args = new_args->next)
389 if (new_args->sym == f->sym)
396 f->sym->attr.not_always_present = 1;
401 /* Resolve alternate entry points. If a symbol has multiple entry points we
402 create a new master symbol for the main routine, and turn the existing
403 symbol into an entry point. */
406 resolve_entries (gfc_namespace *ns)
408 gfc_namespace *old_ns;
412 char name[GFC_MAX_SYMBOL_LEN + 1];
413 static int master_count = 0;
415 if (ns->proc_name == NULL)
418 /* No need to do anything if this procedure doesn't have alternate entry
423 /* We may already have resolved alternate entry points. */
424 if (ns->proc_name->attr.entry_master)
427 /* If this isn't a procedure something has gone horribly wrong. */
428 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
430 /* Remember the current namespace. */
431 old_ns = gfc_current_ns;
435 /* Add the main entry point to the list of entry points. */
436 el = gfc_get_entry_list ();
437 el->sym = ns->proc_name;
439 el->next = ns->entries;
441 ns->proc_name->attr.entry = 1;
443 /* If it is a module function, it needs to be in the right namespace
444 so that gfc_get_fake_result_decl can gather up the results. The
445 need for this arose in get_proc_name, where these beasts were
446 left in their own namespace, to keep prior references linked to
447 the entry declaration.*/
448 if (ns->proc_name->attr.function
449 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
452 /* Do the same for entries where the master is not a module
453 procedure. These are retained in the module namespace because
454 of the module procedure declaration. */
455 for (el = el->next; el; el = el->next)
456 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
457 && el->sym->attr.mod_proc)
461 /* Add an entry statement for it. */
468 /* Create a new symbol for the master function. */
469 /* Give the internal function a unique name (within this file).
470 Also include the function name so the user has some hope of figuring
471 out what is going on. */
472 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
473 master_count++, ns->proc_name->name);
474 gfc_get_ha_symbol (name, &proc);
475 gcc_assert (proc != NULL);
477 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
478 if (ns->proc_name->attr.subroutine)
479 gfc_add_subroutine (&proc->attr, proc->name, NULL);
483 gfc_typespec *ts, *fts;
484 gfc_array_spec *as, *fas;
485 gfc_add_function (&proc->attr, proc->name, NULL);
487 fas = ns->entries->sym->as;
488 fas = fas ? fas : ns->entries->sym->result->as;
489 fts = &ns->entries->sym->result->ts;
490 if (fts->type == BT_UNKNOWN)
491 fts = gfc_get_default_type (ns->entries->sym->result, NULL);
492 for (el = ns->entries->next; el; el = el->next)
494 ts = &el->sym->result->ts;
496 as = as ? as : el->sym->result->as;
497 if (ts->type == BT_UNKNOWN)
498 ts = gfc_get_default_type (el->sym->result, NULL);
500 if (! gfc_compare_types (ts, fts)
501 || (el->sym->result->attr.dimension
502 != ns->entries->sym->result->attr.dimension)
503 || (el->sym->result->attr.pointer
504 != ns->entries->sym->result->attr.pointer))
506 else if (as && fas && ns->entries->sym->result != el->sym->result
507 && gfc_compare_array_spec (as, fas) == 0)
508 gfc_error ("Function %s at %L has entries with mismatched "
509 "array specifications", ns->entries->sym->name,
510 &ns->entries->sym->declared_at);
511 /* The characteristics need to match and thus both need to have
512 the same string length, i.e. both len=*, or both len=4.
513 Having both len=<variable> is also possible, but difficult to
514 check at compile time. */
515 else if (ts->type == BT_CHARACTER && ts->cl && fts->cl
516 && (((ts->cl->length && !fts->cl->length)
517 ||(!ts->cl->length && fts->cl->length))
519 && ts->cl->length->expr_type
520 != fts->cl->length->expr_type)
522 && ts->cl->length->expr_type == EXPR_CONSTANT
523 && mpz_cmp (ts->cl->length->value.integer,
524 fts->cl->length->value.integer) != 0)))
525 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
526 "entries returning variables of different "
527 "string lengths", ns->entries->sym->name,
528 &ns->entries->sym->declared_at);
533 sym = ns->entries->sym->result;
534 /* All result types the same. */
536 if (sym->attr.dimension)
537 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
538 if (sym->attr.pointer)
539 gfc_add_pointer (&proc->attr, NULL);
543 /* Otherwise the result will be passed through a union by
545 proc->attr.mixed_entry_master = 1;
546 for (el = ns->entries; el; el = el->next)
548 sym = el->sym->result;
549 if (sym->attr.dimension)
551 if (el == ns->entries)
552 gfc_error ("FUNCTION result %s can't be an array in "
553 "FUNCTION %s at %L", sym->name,
554 ns->entries->sym->name, &sym->declared_at);
556 gfc_error ("ENTRY result %s can't be an array in "
557 "FUNCTION %s at %L", sym->name,
558 ns->entries->sym->name, &sym->declared_at);
560 else if (sym->attr.pointer)
562 if (el == ns->entries)
563 gfc_error ("FUNCTION result %s can't be a POINTER in "
564 "FUNCTION %s at %L", sym->name,
565 ns->entries->sym->name, &sym->declared_at);
567 gfc_error ("ENTRY result %s can't be a POINTER in "
568 "FUNCTION %s at %L", sym->name,
569 ns->entries->sym->name, &sym->declared_at);
574 if (ts->type == BT_UNKNOWN)
575 ts = gfc_get_default_type (sym, NULL);
579 if (ts->kind == gfc_default_integer_kind)
583 if (ts->kind == gfc_default_real_kind
584 || ts->kind == gfc_default_double_kind)
588 if (ts->kind == gfc_default_complex_kind)
592 if (ts->kind == gfc_default_logical_kind)
596 /* We will issue error elsewhere. */
604 if (el == ns->entries)
605 gfc_error ("FUNCTION result %s can't be of type %s "
606 "in FUNCTION %s at %L", sym->name,
607 gfc_typename (ts), ns->entries->sym->name,
610 gfc_error ("ENTRY result %s can't be of type %s "
611 "in FUNCTION %s at %L", sym->name,
612 gfc_typename (ts), ns->entries->sym->name,
619 proc->attr.access = ACCESS_PRIVATE;
620 proc->attr.entry_master = 1;
622 /* Merge all the entry point arguments. */
623 for (el = ns->entries; el; el = el->next)
624 merge_argument_lists (proc, el->sym->formal);
626 /* Check the master formal arguments for any that are not
627 present in all entry points. */
628 for (el = ns->entries; el; el = el->next)
629 check_argument_lists (proc, el->sym->formal);
631 /* Use the master function for the function body. */
632 ns->proc_name = proc;
634 /* Finalize the new symbols. */
635 gfc_commit_symbols ();
637 /* Restore the original namespace. */
638 gfc_current_ns = old_ns;
643 has_default_initializer (gfc_symbol *der)
647 gcc_assert (der->attr.flavor == FL_DERIVED);
648 for (c = der->components; c; c = c->next)
649 if ((c->ts.type != BT_DERIVED && c->initializer)
650 || (c->ts.type == BT_DERIVED
651 && (!c->pointer && has_default_initializer (c->ts.derived))))
657 /* Resolve common variables. */
659 resolve_common_vars (gfc_symbol *sym, bool named_common)
661 gfc_symbol *csym = sym;
663 for (; csym; csym = csym->common_next)
665 if (csym->value || csym->attr.data)
667 if (!csym->ns->is_block_data)
668 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
669 "but only in BLOCK DATA initialization is "
670 "allowed", csym->name, &csym->declared_at);
671 else if (!named_common)
672 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
673 "in a blank COMMON but initialization is only "
674 "allowed in named common blocks", csym->name,
678 if (csym->ts.type != BT_DERIVED)
681 if (!(csym->ts.derived->attr.sequence
682 || csym->ts.derived->attr.is_bind_c))
683 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
684 "has neither the SEQUENCE nor the BIND(C) "
685 "attribute", csym->name, &csym->declared_at);
686 if (csym->ts.derived->attr.alloc_comp)
687 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
688 "has an ultimate component that is "
689 "allocatable", csym->name, &csym->declared_at);
690 if (has_default_initializer (csym->ts.derived))
691 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
692 "may not have default initializer", csym->name,
697 /* Resolve common blocks. */
699 resolve_common_blocks (gfc_symtree *common_root)
703 if (common_root == NULL)
706 if (common_root->left)
707 resolve_common_blocks (common_root->left);
708 if (common_root->right)
709 resolve_common_blocks (common_root->right);
711 resolve_common_vars (common_root->n.common->head, true);
713 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
717 if (sym->attr.flavor == FL_PARAMETER)
718 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
719 sym->name, &common_root->n.common->where, &sym->declared_at);
721 if (sym->attr.intrinsic)
722 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
723 sym->name, &common_root->n.common->where);
724 else if (sym->attr.result
725 ||(sym->attr.function && gfc_current_ns->proc_name == sym))
726 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
727 "that is also a function result", sym->name,
728 &common_root->n.common->where);
729 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
730 && sym->attr.proc != PROC_ST_FUNCTION)
731 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
732 "that is also a global procedure", sym->name,
733 &common_root->n.common->where);
737 /* Resolve contained function types. Because contained functions can call one
738 another, they have to be worked out before any of the contained procedures
741 The good news is that if a function doesn't already have a type, the only
742 way it can get one is through an IMPLICIT type or a RESULT variable, because
743 by definition contained functions are contained namespace they're contained
744 in, not in a sibling or parent namespace. */
747 resolve_contained_functions (gfc_namespace *ns)
749 gfc_namespace *child;
752 resolve_formal_arglists (ns);
754 for (child = ns->contained; child; child = child->sibling)
756 /* Resolve alternate entry points first. */
757 resolve_entries (child);
759 /* Then check function return types. */
760 resolve_contained_fntype (child->proc_name, child);
761 for (el = child->entries; el; el = el->next)
762 resolve_contained_fntype (el->sym, child);
767 /* Resolve all of the elements of a structure constructor and make sure that
768 the types are correct. */
771 resolve_structure_cons (gfc_expr *expr)
773 gfc_constructor *cons;
779 cons = expr->value.constructor;
780 /* A constructor may have references if it is the result of substituting a
781 parameter variable. In this case we just pull out the component we
784 comp = expr->ref->u.c.sym->components;
786 comp = expr->ts.derived->components;
788 /* See if the user is trying to invoke a structure constructor for one of
789 the iso_c_binding derived types. */
790 if (expr->ts.derived && expr->ts.derived->ts.is_iso_c && cons
791 && cons->expr != NULL)
793 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
794 expr->ts.derived->name, &(expr->where));
798 for (; comp; comp = comp->next, cons = cons->next)
805 if (gfc_resolve_expr (cons->expr) == FAILURE)
811 rank = comp->as ? comp->as->rank : 0;
812 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
813 && (comp->allocatable || cons->expr->rank))
815 gfc_error ("The rank of the element in the derived type "
816 "constructor at %L does not match that of the "
817 "component (%d/%d)", &cons->expr->where,
818 cons->expr->rank, rank);
822 /* If we don't have the right type, try to convert it. */
824 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
827 if (comp->pointer && cons->expr->ts.type != BT_UNKNOWN)
828 gfc_error ("The element in the derived type constructor at %L, "
829 "for pointer component '%s', is %s but should be %s",
830 &cons->expr->where, comp->name,
831 gfc_basic_typename (cons->expr->ts.type),
832 gfc_basic_typename (comp->ts.type));
834 t = gfc_convert_type (cons->expr, &comp->ts, 1);
837 if (cons->expr->expr_type == EXPR_NULL
838 && !(comp->pointer || comp->allocatable))
841 gfc_error ("The NULL in the derived type constructor at %L is "
842 "being applied to component '%s', which is neither "
843 "a POINTER nor ALLOCATABLE", &cons->expr->where,
847 if (!comp->pointer || cons->expr->expr_type == EXPR_NULL)
850 a = gfc_expr_attr (cons->expr);
852 if (!a.pointer && !a.target)
855 gfc_error ("The element in the derived type constructor at %L, "
856 "for pointer component '%s' should be a POINTER or "
857 "a TARGET", &cons->expr->where, comp->name);
865 /****************** Expression name resolution ******************/
867 /* Returns 0 if a symbol was not declared with a type or
868 attribute declaration statement, nonzero otherwise. */
871 was_declared (gfc_symbol *sym)
877 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
880 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
881 || a.optional || a.pointer || a.save || a.target || a.volatile_
882 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
889 /* Determine if a symbol is generic or not. */
892 generic_sym (gfc_symbol *sym)
896 if (sym->attr.generic ||
897 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
900 if (was_declared (sym) || sym->ns->parent == NULL)
903 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
910 return generic_sym (s);
917 /* Determine if a symbol is specific or not. */
920 specific_sym (gfc_symbol *sym)
924 if (sym->attr.if_source == IFSRC_IFBODY
925 || sym->attr.proc == PROC_MODULE
926 || sym->attr.proc == PROC_INTERNAL
927 || sym->attr.proc == PROC_ST_FUNCTION
928 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
929 || sym->attr.external)
932 if (was_declared (sym) || sym->ns->parent == NULL)
935 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
937 return (s == NULL) ? 0 : specific_sym (s);
941 /* Figure out if the procedure is specific, generic or unknown. */
944 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
948 procedure_kind (gfc_symbol *sym)
950 if (generic_sym (sym))
951 return PTYPE_GENERIC;
953 if (specific_sym (sym))
954 return PTYPE_SPECIFIC;
956 return PTYPE_UNKNOWN;
959 /* Check references to assumed size arrays. The flag need_full_assumed_size
960 is nonzero when matching actual arguments. */
962 static int need_full_assumed_size = 0;
965 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
967 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
970 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
971 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
972 && (e->ref->u.ar.type == DIMEN_ELEMENT))
974 gfc_error ("The upper bound in the last dimension must "
975 "appear in the reference to the assumed size "
976 "array '%s' at %L", sym->name, &e->where);
983 /* Look for bad assumed size array references in argument expressions
984 of elemental and array valued intrinsic procedures. Since this is
985 called from procedure resolution functions, it only recurses at
989 resolve_assumed_size_actual (gfc_expr *e)
994 switch (e->expr_type)
997 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1002 if (resolve_assumed_size_actual (e->value.op.op1)
1003 || resolve_assumed_size_actual (e->value.op.op2))
1014 /* Resolve an actual argument list. Most of the time, this is just
1015 resolving the expressions in the list.
1016 The exception is that we sometimes have to decide whether arguments
1017 that look like procedure arguments are really simple variable
1021 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype)
1024 gfc_symtree *parent_st;
1026 int save_need_full_assumed_size;
1028 for (; arg; arg = arg->next)
1033 /* Check the label is a valid branching target. */
1036 if (arg->label->defined == ST_LABEL_UNKNOWN)
1038 gfc_error ("Label %d referenced at %L is never defined",
1039 arg->label->value, &arg->label->where);
1046 if (e->expr_type == FL_VARIABLE && e->symtree->ambiguous)
1048 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1053 if (e->ts.type != BT_PROCEDURE)
1055 save_need_full_assumed_size = need_full_assumed_size;
1056 if (e->expr_type != FL_VARIABLE)
1057 need_full_assumed_size = 0;
1058 if (gfc_resolve_expr (e) != SUCCESS)
1060 need_full_assumed_size = save_need_full_assumed_size;
1064 /* See if the expression node should really be a variable reference. */
1066 sym = e->symtree->n.sym;
1068 if (sym->attr.flavor == FL_PROCEDURE
1069 || sym->attr.intrinsic
1070 || sym->attr.external)
1074 /* If a procedure is not already determined to be something else
1075 check if it is intrinsic. */
1076 if (!sym->attr.intrinsic
1077 && !(sym->attr.external || sym->attr.use_assoc
1078 || sym->attr.if_source == IFSRC_IFBODY)
1079 && gfc_intrinsic_name (sym->name, sym->attr.subroutine))
1080 sym->attr.intrinsic = 1;
1082 if (sym->attr.proc == PROC_ST_FUNCTION)
1084 gfc_error ("Statement function '%s' at %L is not allowed as an "
1085 "actual argument", sym->name, &e->where);
1088 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1089 sym->attr.subroutine);
1090 if (sym->attr.intrinsic && actual_ok == 0)
1092 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1093 "actual argument", sym->name, &e->where);
1096 if (sym->attr.contained && !sym->attr.use_assoc
1097 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1099 gfc_error ("Internal procedure '%s' is not allowed as an "
1100 "actual argument at %L", sym->name, &e->where);
1103 if (sym->attr.elemental && !sym->attr.intrinsic)
1105 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1106 "allowed as an actual argument at %L", sym->name,
1110 /* Check if a generic interface has a specific procedure
1111 with the same name before emitting an error. */
1112 if (sym->attr.generic)
1115 for (p = sym->generic; p; p = p->next)
1116 if (strcmp (sym->name, p->sym->name) == 0)
1118 e->symtree = gfc_find_symtree
1119 (p->sym->ns->sym_root, sym->name);
1124 if (p == NULL || e->symtree == NULL)
1125 gfc_error ("GENERIC procedure '%s' is not "
1126 "allowed as an actual argument at %L", sym->name,
1130 /* If the symbol is the function that names the current (or
1131 parent) scope, then we really have a variable reference. */
1133 if (sym->attr.function && sym->result == sym
1134 && (sym->ns->proc_name == sym
1135 || (sym->ns->parent != NULL
1136 && sym->ns->parent->proc_name == sym)))
1139 /* If all else fails, see if we have a specific intrinsic. */
1140 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1142 gfc_intrinsic_sym *isym;
1144 isym = gfc_find_function (sym->name);
1145 if (isym == NULL || !isym->specific)
1147 gfc_error ("Unable to find a specific INTRINSIC procedure "
1148 "for the reference '%s' at %L", sym->name,
1153 sym->attr.intrinsic = 1;
1154 sym->attr.function = 1;
1159 /* See if the name is a module procedure in a parent unit. */
1161 if (was_declared (sym) || sym->ns->parent == NULL)
1164 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1166 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1170 if (parent_st == NULL)
1173 sym = parent_st->n.sym;
1174 e->symtree = parent_st; /* Point to the right thing. */
1176 if (sym->attr.flavor == FL_PROCEDURE
1177 || sym->attr.intrinsic
1178 || sym->attr.external)
1184 e->expr_type = EXPR_VARIABLE;
1186 if (sym->as != NULL)
1188 e->rank = sym->as->rank;
1189 e->ref = gfc_get_ref ();
1190 e->ref->type = REF_ARRAY;
1191 e->ref->u.ar.type = AR_FULL;
1192 e->ref->u.ar.as = sym->as;
1195 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1196 primary.c (match_actual_arg). If above code determines that it
1197 is a variable instead, it needs to be resolved as it was not
1198 done at the beginning of this function. */
1199 save_need_full_assumed_size = need_full_assumed_size;
1200 if (e->expr_type != FL_VARIABLE)
1201 need_full_assumed_size = 0;
1202 if (gfc_resolve_expr (e) != SUCCESS)
1204 need_full_assumed_size = save_need_full_assumed_size;
1207 /* Check argument list functions %VAL, %LOC and %REF. There is
1208 nothing to do for %REF. */
1209 if (arg->name && arg->name[0] == '%')
1211 if (strncmp ("%VAL", arg->name, 4) == 0)
1213 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1215 gfc_error ("By-value argument at %L is not of numeric "
1222 gfc_error ("By-value argument at %L cannot be an array or "
1223 "an array section", &e->where);
1227 /* Intrinsics are still PROC_UNKNOWN here. However,
1228 since same file external procedures are not resolvable
1229 in gfortran, it is a good deal easier to leave them to
1231 if (ptype != PROC_UNKNOWN
1232 && ptype != PROC_DUMMY
1233 && ptype != PROC_EXTERNAL
1234 && ptype != PROC_MODULE)
1236 gfc_error ("By-value argument at %L is not allowed "
1237 "in this context", &e->where);
1242 /* Statement functions have already been excluded above. */
1243 else if (strncmp ("%LOC", arg->name, 4) == 0
1244 && e->ts.type == BT_PROCEDURE)
1246 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1248 gfc_error ("Passing internal procedure at %L by location "
1249 "not allowed", &e->where);
1260 /* Do the checks of the actual argument list that are specific to elemental
1261 procedures. If called with c == NULL, we have a function, otherwise if
1262 expr == NULL, we have a subroutine. */
1265 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1267 gfc_actual_arglist *arg0;
1268 gfc_actual_arglist *arg;
1269 gfc_symbol *esym = NULL;
1270 gfc_intrinsic_sym *isym = NULL;
1272 gfc_intrinsic_arg *iformal = NULL;
1273 gfc_formal_arglist *eformal = NULL;
1274 bool formal_optional = false;
1275 bool set_by_optional = false;
1279 /* Is this an elemental procedure? */
1280 if (expr && expr->value.function.actual != NULL)
1282 if (expr->value.function.esym != NULL
1283 && expr->value.function.esym->attr.elemental)
1285 arg0 = expr->value.function.actual;
1286 esym = expr->value.function.esym;
1288 else if (expr->value.function.isym != NULL
1289 && expr->value.function.isym->elemental)
1291 arg0 = expr->value.function.actual;
1292 isym = expr->value.function.isym;
1297 else if (c && c->ext.actual != NULL && c->symtree->n.sym->attr.elemental)
1299 arg0 = c->ext.actual;
1300 esym = c->symtree->n.sym;
1305 /* The rank of an elemental is the rank of its array argument(s). */
1306 for (arg = arg0; arg; arg = arg->next)
1308 if (arg->expr != NULL && arg->expr->rank > 0)
1310 rank = arg->expr->rank;
1311 if (arg->expr->expr_type == EXPR_VARIABLE
1312 && arg->expr->symtree->n.sym->attr.optional)
1313 set_by_optional = true;
1315 /* Function specific; set the result rank and shape. */
1319 if (!expr->shape && arg->expr->shape)
1321 expr->shape = gfc_get_shape (rank);
1322 for (i = 0; i < rank; i++)
1323 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1330 /* If it is an array, it shall not be supplied as an actual argument
1331 to an elemental procedure unless an array of the same rank is supplied
1332 as an actual argument corresponding to a nonoptional dummy argument of
1333 that elemental procedure(12.4.1.5). */
1334 formal_optional = false;
1336 iformal = isym->formal;
1338 eformal = esym->formal;
1340 for (arg = arg0; arg; arg = arg->next)
1344 if (eformal->sym && eformal->sym->attr.optional)
1345 formal_optional = true;
1346 eformal = eformal->next;
1348 else if (isym && iformal)
1350 if (iformal->optional)
1351 formal_optional = true;
1352 iformal = iformal->next;
1355 formal_optional = true;
1357 if (pedantic && arg->expr != NULL
1358 && arg->expr->expr_type == EXPR_VARIABLE
1359 && arg->expr->symtree->n.sym->attr.optional
1362 && (set_by_optional || arg->expr->rank != rank)
1363 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1365 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1366 "MISSING, it cannot be the actual argument of an "
1367 "ELEMENTAL procedure unless there is a non-optional "
1368 "argument with the same rank (12.4.1.5)",
1369 arg->expr->symtree->n.sym->name, &arg->expr->where);
1374 for (arg = arg0; arg; arg = arg->next)
1376 if (arg->expr == NULL || arg->expr->rank == 0)
1379 /* Being elemental, the last upper bound of an assumed size array
1380 argument must be present. */
1381 if (resolve_assumed_size_actual (arg->expr))
1384 /* Elemental procedure's array actual arguments must conform. */
1387 if (gfc_check_conformance ("elemental procedure", arg->expr, e)
1395 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1396 is an array, the intent inout/out variable needs to be also an array. */
1397 if (rank > 0 && esym && expr == NULL)
1398 for (eformal = esym->formal, arg = arg0; arg && eformal;
1399 arg = arg->next, eformal = eformal->next)
1400 if ((eformal->sym->attr.intent == INTENT_OUT
1401 || eformal->sym->attr.intent == INTENT_INOUT)
1402 && arg->expr && arg->expr->rank == 0)
1404 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1405 "ELEMENTAL subroutine '%s' is a scalar, but another "
1406 "actual argument is an array", &arg->expr->where,
1407 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1408 : "INOUT", eformal->sym->name, esym->name);
1415 /* Go through each actual argument in ACTUAL and see if it can be
1416 implemented as an inlined, non-copying intrinsic. FNSYM is the
1417 function being called, or NULL if not known. */
1420 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1422 gfc_actual_arglist *ap;
1425 for (ap = actual; ap; ap = ap->next)
1427 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1428 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual))
1429 ap->expr->inline_noncopying_intrinsic = 1;
1433 /* This function does the checking of references to global procedures
1434 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1435 77 and 95 standards. It checks for a gsymbol for the name, making
1436 one if it does not already exist. If it already exists, then the
1437 reference being resolved must correspond to the type of gsymbol.
1438 Otherwise, the new symbol is equipped with the attributes of the
1439 reference. The corresponding code that is called in creating
1440 global entities is parse.c. */
1443 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
1448 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1450 gsym = gfc_get_gsymbol (sym->name);
1452 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1453 gfc_global_used (gsym, where);
1455 if (gsym->type == GSYM_UNKNOWN)
1458 gsym->where = *where;
1465 /************* Function resolution *************/
1467 /* Resolve a function call known to be generic.
1468 Section 14.1.2.4.1. */
1471 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1475 if (sym->attr.generic)
1477 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1480 expr->value.function.name = s->name;
1481 expr->value.function.esym = s;
1483 if (s->ts.type != BT_UNKNOWN)
1485 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1486 expr->ts = s->result->ts;
1489 expr->rank = s->as->rank;
1490 else if (s->result != NULL && s->result->as != NULL)
1491 expr->rank = s->result->as->rank;
1493 gfc_set_sym_referenced (expr->value.function.esym);
1498 /* TODO: Need to search for elemental references in generic
1502 if (sym->attr.intrinsic)
1503 return gfc_intrinsic_func_interface (expr, 0);
1510 resolve_generic_f (gfc_expr *expr)
1515 sym = expr->symtree->n.sym;
1519 m = resolve_generic_f0 (expr, sym);
1522 else if (m == MATCH_ERROR)
1526 if (sym->ns->parent == NULL)
1528 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1532 if (!generic_sym (sym))
1536 /* Last ditch attempt. See if the reference is to an intrinsic
1537 that possesses a matching interface. 14.1.2.4 */
1538 if (sym && !gfc_intrinsic_name (sym->name, 0))
1540 gfc_error ("There is no specific function for the generic '%s' at %L",
1541 expr->symtree->n.sym->name, &expr->where);
1545 m = gfc_intrinsic_func_interface (expr, 0);
1549 gfc_error ("Generic function '%s' at %L is not consistent with a "
1550 "specific intrinsic interface", expr->symtree->n.sym->name,
1557 /* Resolve a function call known to be specific. */
1560 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
1564 /* See if we have an intrinsic interface. */
1566 if (sym->ts.interface != NULL && sym->ts.interface->attr.intrinsic)
1568 gfc_intrinsic_sym *isym;
1569 isym = gfc_find_function (sym->ts.interface->name);
1571 /* Existance of isym should be checked already. */
1574 sym->ts.type = isym->ts.type;
1575 sym->ts.kind = isym->ts.kind;
1576 sym->attr.function = 1;
1577 sym->attr.proc = PROC_EXTERNAL;
1581 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1583 if (sym->attr.dummy)
1585 sym->attr.proc = PROC_DUMMY;
1589 sym->attr.proc = PROC_EXTERNAL;
1593 if (sym->attr.proc == PROC_MODULE
1594 || sym->attr.proc == PROC_ST_FUNCTION
1595 || sym->attr.proc == PROC_INTERNAL)
1598 if (sym->attr.intrinsic)
1600 m = gfc_intrinsic_func_interface (expr, 1);
1604 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
1605 "with an intrinsic", sym->name, &expr->where);
1613 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1616 expr->value.function.name = sym->name;
1617 expr->value.function.esym = sym;
1618 if (sym->as != NULL)
1619 expr->rank = sym->as->rank;
1626 resolve_specific_f (gfc_expr *expr)
1631 sym = expr->symtree->n.sym;
1635 m = resolve_specific_f0 (sym, expr);
1638 if (m == MATCH_ERROR)
1641 if (sym->ns->parent == NULL)
1644 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1650 gfc_error ("Unable to resolve the specific function '%s' at %L",
1651 expr->symtree->n.sym->name, &expr->where);
1657 /* Resolve a procedure call not known to be generic nor specific. */
1660 resolve_unknown_f (gfc_expr *expr)
1665 sym = expr->symtree->n.sym;
1667 if (sym->attr.dummy)
1669 sym->attr.proc = PROC_DUMMY;
1670 expr->value.function.name = sym->name;
1674 /* See if we have an intrinsic function reference. */
1676 if (gfc_intrinsic_name (sym->name, 0))
1678 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
1683 /* The reference is to an external name. */
1685 sym->attr.proc = PROC_EXTERNAL;
1686 expr->value.function.name = sym->name;
1687 expr->value.function.esym = expr->symtree->n.sym;
1689 if (sym->as != NULL)
1690 expr->rank = sym->as->rank;
1692 /* Type of the expression is either the type of the symbol or the
1693 default type of the symbol. */
1696 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1698 if (sym->ts.type != BT_UNKNOWN)
1702 ts = gfc_get_default_type (sym, sym->ns);
1704 if (ts->type == BT_UNKNOWN)
1706 gfc_error ("Function '%s' at %L has no IMPLICIT type",
1707 sym->name, &expr->where);
1718 /* Return true, if the symbol is an external procedure. */
1720 is_external_proc (gfc_symbol *sym)
1722 if (!sym->attr.dummy && !sym->attr.contained
1723 && !(sym->attr.intrinsic
1724 || gfc_intrinsic_name (sym->name, sym->attr.subroutine))
1725 && sym->attr.proc != PROC_ST_FUNCTION
1726 && !sym->attr.use_assoc
1734 /* Figure out if a function reference is pure or not. Also set the name
1735 of the function for a potential error message. Return nonzero if the
1736 function is PURE, zero if not. */
1738 pure_stmt_function (gfc_expr *, gfc_symbol *);
1741 pure_function (gfc_expr *e, const char **name)
1747 if (e->symtree != NULL
1748 && e->symtree->n.sym != NULL
1749 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
1750 return pure_stmt_function (e, e->symtree->n.sym);
1752 if (e->value.function.esym)
1754 pure = gfc_pure (e->value.function.esym);
1755 *name = e->value.function.esym->name;
1757 else if (e->value.function.isym)
1759 pure = e->value.function.isym->pure
1760 || e->value.function.isym->elemental;
1761 *name = e->value.function.isym->name;
1765 /* Implicit functions are not pure. */
1767 *name = e->value.function.name;
1775 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
1776 int *f ATTRIBUTE_UNUSED)
1780 /* Don't bother recursing into other statement functions
1781 since they will be checked individually for purity. */
1782 if (e->expr_type != EXPR_FUNCTION
1784 || e->symtree->n.sym == sym
1785 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
1788 return pure_function (e, &name) ? false : true;
1793 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
1795 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
1800 is_scalar_expr_ptr (gfc_expr *expr)
1802 try retval = SUCCESS;
1807 /* See if we have a gfc_ref, which means we have a substring, array
1808 reference, or a component. */
1809 if (expr->ref != NULL)
1812 while (ref->next != NULL)
1818 if (ref->u.ss.length != NULL
1819 && ref->u.ss.length->length != NULL
1821 && ref->u.ss.start->expr_type == EXPR_CONSTANT
1823 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
1825 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
1826 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
1827 if (end - start + 1 != 1)
1834 if (ref->u.ar.type == AR_ELEMENT)
1836 else if (ref->u.ar.type == AR_FULL)
1838 /* The user can give a full array if the array is of size 1. */
1839 if (ref->u.ar.as != NULL
1840 && ref->u.ar.as->rank == 1
1841 && ref->u.ar.as->type == AS_EXPLICIT
1842 && ref->u.ar.as->lower[0] != NULL
1843 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
1844 && ref->u.ar.as->upper[0] != NULL
1845 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
1847 /* If we have a character string, we need to check if
1848 its length is one. */
1849 if (expr->ts.type == BT_CHARACTER)
1851 if (expr->ts.cl == NULL
1852 || expr->ts.cl->length == NULL
1853 || mpz_cmp_si (expr->ts.cl->length->value.integer, 1)
1859 /* We have constant lower and upper bounds. If the
1860 difference between is 1, it can be considered a
1862 start = (int) mpz_get_si
1863 (ref->u.ar.as->lower[0]->value.integer);
1864 end = (int) mpz_get_si
1865 (ref->u.ar.as->upper[0]->value.integer);
1866 if (end - start + 1 != 1)
1881 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
1883 /* Character string. Make sure it's of length 1. */
1884 if (expr->ts.cl == NULL
1885 || expr->ts.cl->length == NULL
1886 || mpz_cmp_si (expr->ts.cl->length->value.integer, 1) != 0)
1889 else if (expr->rank != 0)
1896 /* Match one of the iso_c_binding functions (c_associated or c_loc)
1897 and, in the case of c_associated, set the binding label based on
1901 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
1902 gfc_symbol **new_sym)
1904 char name[GFC_MAX_SYMBOL_LEN + 1];
1905 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
1906 int optional_arg = 0;
1907 try retval = SUCCESS;
1908 gfc_symbol *args_sym;
1909 gfc_typespec *arg_ts;
1910 gfc_ref *parent_ref;
1913 if (args->expr->expr_type == EXPR_CONSTANT
1914 || args->expr->expr_type == EXPR_OP
1915 || args->expr->expr_type == EXPR_NULL)
1917 gfc_error ("Argument to '%s' at %L is not a variable",
1918 sym->name, &(args->expr->where));
1922 args_sym = args->expr->symtree->n.sym;
1924 /* The typespec for the actual arg should be that stored in the expr
1925 and not necessarily that of the expr symbol (args_sym), because
1926 the actual expression could be a part-ref of the expr symbol. */
1927 arg_ts = &(args->expr->ts);
1929 /* Get the parent reference (if any) for the expression. This happens for
1930 cases such as a%b%c. */
1931 parent_ref = args->expr->ref;
1933 if (parent_ref != NULL)
1935 curr_ref = parent_ref->next;
1936 while (curr_ref != NULL && curr_ref->next != NULL)
1938 parent_ref = curr_ref;
1939 curr_ref = curr_ref->next;
1943 /* If curr_ref is non-NULL, we had a part-ref expression. If the curr_ref
1944 is for a REF_COMPONENT, then we need to use it as the parent_ref for
1945 the name, etc. Otherwise, the current parent_ref should be correct. */
1946 if (curr_ref != NULL && curr_ref->type == REF_COMPONENT)
1947 parent_ref = curr_ref;
1949 if (parent_ref == args->expr->ref)
1951 else if (parent_ref != NULL && parent_ref->type != REF_COMPONENT)
1952 gfc_internal_error ("Unexpected expression reference type in "
1953 "gfc_iso_c_func_interface");
1955 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
1957 /* If the user gave two args then they are providing something for
1958 the optional arg (the second cptr). Therefore, set the name and
1959 binding label to the c_associated for two cptrs. Otherwise,
1960 set c_associated to expect one cptr. */
1964 sprintf (name, "%s_2", sym->name);
1965 sprintf (binding_label, "%s_2", sym->binding_label);
1971 sprintf (name, "%s_1", sym->name);
1972 sprintf (binding_label, "%s_1", sym->binding_label);
1976 /* Get a new symbol for the version of c_associated that
1978 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
1980 else if (sym->intmod_sym_id == ISOCBINDING_LOC
1981 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
1983 sprintf (name, "%s", sym->name);
1984 sprintf (binding_label, "%s", sym->binding_label);
1986 /* Error check the call. */
1987 if (args->next != NULL)
1989 gfc_error_now ("More actual than formal arguments in '%s' "
1990 "call at %L", name, &(args->expr->where));
1993 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
1995 /* Make sure we have either the target or pointer attribute. */
1996 if (!(args_sym->attr.target)
1997 && !(args_sym->attr.pointer)
1998 && (parent_ref == NULL ||
1999 !parent_ref->u.c.component->pointer))
2001 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2002 "a TARGET or an associated pointer",
2004 sym->name, &(args->expr->where));
2008 /* See if we have interoperable type and type param. */
2009 if (verify_c_interop (arg_ts,
2010 (parent_ref ? parent_ref->u.c.component->name
2012 &(args->expr->where)) == SUCCESS
2013 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2015 if (args_sym->attr.target == 1)
2017 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2018 has the target attribute and is interoperable. */
2019 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2020 allocatable variable that has the TARGET attribute and
2021 is not an array of zero size. */
2022 if (args_sym->attr.allocatable == 1)
2024 if (args_sym->attr.dimension != 0
2025 && (args_sym->as && args_sym->as->rank == 0))
2027 gfc_error_now ("Allocatable variable '%s' used as a "
2028 "parameter to '%s' at %L must not be "
2029 "an array of zero size",
2030 args_sym->name, sym->name,
2031 &(args->expr->where));
2037 /* A non-allocatable target variable with C
2038 interoperable type and type parameters must be
2040 if (args_sym && args_sym->attr.dimension)
2042 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2044 gfc_error ("Assumed-shape array '%s' at %L "
2045 "cannot be an argument to the "
2046 "procedure '%s' because "
2047 "it is not C interoperable",
2049 &(args->expr->where), sym->name);
2052 else if (args_sym->as->type == AS_DEFERRED)
2054 gfc_error ("Deferred-shape array '%s' at %L "
2055 "cannot be an argument to the "
2056 "procedure '%s' because "
2057 "it is not C interoperable",
2059 &(args->expr->where), sym->name);
2064 /* Make sure it's not a character string. Arrays of
2065 any type should be ok if the variable is of a C
2066 interoperable type. */
2067 if (arg_ts->type == BT_CHARACTER)
2068 if (arg_ts->cl != NULL
2069 && (arg_ts->cl->length == NULL
2070 || arg_ts->cl->length->expr_type
2073 (arg_ts->cl->length->value.integer, 1)
2075 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2077 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2078 "at %L must have a length of 1",
2079 args_sym->name, sym->name,
2080 &(args->expr->where));
2085 else if ((args_sym->attr.pointer == 1 ||
2087 && parent_ref->u.c.component->pointer))
2088 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2090 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2092 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2093 "associated scalar POINTER", args_sym->name,
2094 sym->name, &(args->expr->where));
2100 /* The parameter is not required to be C interoperable. If it
2101 is not C interoperable, it must be a nonpolymorphic scalar
2102 with no length type parameters. It still must have either
2103 the pointer or target attribute, and it can be
2104 allocatable (but must be allocated when c_loc is called). */
2105 if (args->expr->rank != 0
2106 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2108 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2109 "scalar", args_sym->name, sym->name,
2110 &(args->expr->where));
2113 else if (arg_ts->type == BT_CHARACTER
2114 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2116 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2117 "%L must have a length of 1",
2118 args_sym->name, sym->name,
2119 &(args->expr->where));
2124 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2126 if (args_sym->attr.flavor != FL_PROCEDURE)
2128 /* TODO: Update this error message to allow for procedure
2129 pointers once they are implemented. */
2130 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2132 args_sym->name, sym->name,
2133 &(args->expr->where));
2136 else if (args_sym->attr.is_bind_c != 1)
2138 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2140 args_sym->name, sym->name,
2141 &(args->expr->where));
2146 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2151 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2152 "iso_c_binding function: '%s'!\n", sym->name);
2159 /* Resolve a function call, which means resolving the arguments, then figuring
2160 out which entity the name refers to. */
2161 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2162 to INTENT(OUT) or INTENT(INOUT). */
2165 resolve_function (gfc_expr *expr)
2167 gfc_actual_arglist *arg;
2172 procedure_type p = PROC_INTRINSIC;
2176 sym = expr->symtree->n.sym;
2178 if (sym && sym->attr.flavor == FL_VARIABLE)
2180 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2184 if (sym && sym->attr.abstract)
2186 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2187 sym->name, &expr->where);
2191 /* If the procedure is external, check for usage. */
2192 if (sym && is_external_proc (sym))
2193 resolve_global_procedure (sym, &expr->where, 0);
2195 /* Switch off assumed size checking and do this again for certain kinds
2196 of procedure, once the procedure itself is resolved. */
2197 need_full_assumed_size++;
2199 if (expr->symtree && expr->symtree->n.sym)
2200 p = expr->symtree->n.sym->attr.proc;
2202 if (resolve_actual_arglist (expr->value.function.actual, p) == FAILURE)
2205 /* Need to setup the call to the correct c_associated, depending on
2206 the number of cptrs to user gives to compare. */
2207 if (sym && sym->attr.is_iso_c == 1)
2209 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2213 /* Get the symtree for the new symbol (resolved func).
2214 the old one will be freed later, when it's no longer used. */
2215 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2218 /* Resume assumed_size checking. */
2219 need_full_assumed_size--;
2221 if (sym && sym->ts.type == BT_CHARACTER
2223 && sym->ts.cl->length == NULL
2225 && expr->value.function.esym == NULL
2226 && !sym->attr.contained)
2228 /* Internal procedures are taken care of in resolve_contained_fntype. */
2229 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2230 "be used at %L since it is not a dummy argument",
2231 sym->name, &expr->where);
2235 /* See if function is already resolved. */
2237 if (expr->value.function.name != NULL)
2239 if (expr->ts.type == BT_UNKNOWN)
2245 /* Apply the rules of section 14.1.2. */
2247 switch (procedure_kind (sym))
2250 t = resolve_generic_f (expr);
2253 case PTYPE_SPECIFIC:
2254 t = resolve_specific_f (expr);
2258 t = resolve_unknown_f (expr);
2262 gfc_internal_error ("resolve_function(): bad function type");
2266 /* If the expression is still a function (it might have simplified),
2267 then we check to see if we are calling an elemental function. */
2269 if (expr->expr_type != EXPR_FUNCTION)
2272 temp = need_full_assumed_size;
2273 need_full_assumed_size = 0;
2275 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2278 if (omp_workshare_flag
2279 && expr->value.function.esym
2280 && ! gfc_elemental (expr->value.function.esym))
2282 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2283 "in WORKSHARE construct", expr->value.function.esym->name,
2288 #define GENERIC_ID expr->value.function.isym->id
2289 else if (expr->value.function.actual != NULL
2290 && expr->value.function.isym != NULL
2291 && GENERIC_ID != GFC_ISYM_LBOUND
2292 && GENERIC_ID != GFC_ISYM_LEN
2293 && GENERIC_ID != GFC_ISYM_LOC
2294 && GENERIC_ID != GFC_ISYM_PRESENT)
2296 /* Array intrinsics must also have the last upper bound of an
2297 assumed size array argument. UBOUND and SIZE have to be
2298 excluded from the check if the second argument is anything
2301 inquiry = GENERIC_ID == GFC_ISYM_UBOUND
2302 || GENERIC_ID == GFC_ISYM_SIZE;
2304 for (arg = expr->value.function.actual; arg; arg = arg->next)
2306 if (inquiry && arg->next != NULL && arg->next->expr)
2308 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2311 if ((int)mpz_get_si (arg->next->expr->value.integer)
2316 if (arg->expr != NULL
2317 && arg->expr->rank > 0
2318 && resolve_assumed_size_actual (arg->expr))
2324 need_full_assumed_size = temp;
2327 if (!pure_function (expr, &name) && name)
2331 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2332 "FORALL %s", name, &expr->where,
2333 forall_flag == 2 ? "mask" : "block");
2336 else if (gfc_pure (NULL))
2338 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2339 "procedure within a PURE procedure", name, &expr->where);
2344 /* Functions without the RECURSIVE attribution are not allowed to
2345 * call themselves. */
2346 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2348 gfc_symbol *esym, *proc;
2349 esym = expr->value.function.esym;
2350 proc = gfc_current_ns->proc_name;
2353 gfc_error ("Function '%s' at %L cannot call itself, as it is not "
2354 "RECURSIVE", name, &expr->where);
2358 if (esym->attr.entry && esym->ns->entries && proc->ns->entries
2359 && esym->ns->entries->sym == proc->ns->entries->sym)
2361 gfc_error ("Call to ENTRY '%s' at %L is recursive, but function "
2362 "'%s' is not declared as RECURSIVE",
2363 esym->name, &expr->where, esym->ns->entries->sym->name);
2368 /* Character lengths of use associated functions may contains references to
2369 symbols not referenced from the current program unit otherwise. Make sure
2370 those symbols are marked as referenced. */
2372 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2373 && expr->value.function.esym->attr.use_assoc)
2375 gfc_expr_set_symbols_referenced (expr->ts.cl->length);
2379 && !((expr->value.function.esym
2380 && expr->value.function.esym->attr.elemental)
2382 (expr->value.function.isym
2383 && expr->value.function.isym->elemental)))
2384 find_noncopying_intrinsics (expr->value.function.esym,
2385 expr->value.function.actual);
2387 /* Make sure that the expression has a typespec that works. */
2388 if (expr->ts.type == BT_UNKNOWN)
2390 if (expr->symtree->n.sym->result
2391 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN)
2392 expr->ts = expr->symtree->n.sym->result->ts;
2399 /************* Subroutine resolution *************/
2402 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2408 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2409 sym->name, &c->loc);
2410 else if (gfc_pure (NULL))
2411 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2417 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2421 if (sym->attr.generic)
2423 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2426 c->resolved_sym = s;
2427 pure_subroutine (c, s);
2431 /* TODO: Need to search for elemental references in generic interface. */
2434 if (sym->attr.intrinsic)
2435 return gfc_intrinsic_sub_interface (c, 0);
2442 resolve_generic_s (gfc_code *c)
2447 sym = c->symtree->n.sym;
2451 m = resolve_generic_s0 (c, sym);
2454 else if (m == MATCH_ERROR)
2458 if (sym->ns->parent == NULL)
2460 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2464 if (!generic_sym (sym))
2468 /* Last ditch attempt. See if the reference is to an intrinsic
2469 that possesses a matching interface. 14.1.2.4 */
2470 sym = c->symtree->n.sym;
2472 if (!gfc_intrinsic_name (sym->name, 1))
2474 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2475 sym->name, &c->loc);
2479 m = gfc_intrinsic_sub_interface (c, 0);
2483 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2484 "intrinsic subroutine interface", sym->name, &c->loc);
2490 /* Set the name and binding label of the subroutine symbol in the call
2491 expression represented by 'c' to include the type and kind of the
2492 second parameter. This function is for resolving the appropriate
2493 version of c_f_pointer() and c_f_procpointer(). For example, a
2494 call to c_f_pointer() for a default integer pointer could have a
2495 name of c_f_pointer_i4. If no second arg exists, which is an error
2496 for these two functions, it defaults to the generic symbol's name
2497 and binding label. */
2500 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2501 char *name, char *binding_label)
2503 gfc_expr *arg = NULL;
2507 /* The second arg of c_f_pointer and c_f_procpointer determines
2508 the type and kind for the procedure name. */
2509 arg = c->ext.actual->next->expr;
2513 /* Set up the name to have the given symbol's name,
2514 plus the type and kind. */
2515 /* a derived type is marked with the type letter 'u' */
2516 if (arg->ts.type == BT_DERIVED)
2519 kind = 0; /* set the kind as 0 for now */
2523 type = gfc_type_letter (arg->ts.type);
2524 kind = arg->ts.kind;
2527 if (arg->ts.type == BT_CHARACTER)
2528 /* Kind info for character strings not needed. */
2531 sprintf (name, "%s_%c%d", sym->name, type, kind);
2532 /* Set up the binding label as the given symbol's label plus
2533 the type and kind. */
2534 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2538 /* If the second arg is missing, set the name and label as
2539 was, cause it should at least be found, and the missing
2540 arg error will be caught by compare_parameters(). */
2541 sprintf (name, "%s", sym->name);
2542 sprintf (binding_label, "%s", sym->binding_label);
2549 /* Resolve a generic version of the iso_c_binding procedure given
2550 (sym) to the specific one based on the type and kind of the
2551 argument(s). Currently, this function resolves c_f_pointer() and
2552 c_f_procpointer based on the type and kind of the second argument
2553 (FPTR). Other iso_c_binding procedures aren't specially handled.
2554 Upon successfully exiting, c->resolved_sym will hold the resolved
2555 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2559 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2561 gfc_symbol *new_sym;
2562 /* this is fine, since we know the names won't use the max */
2563 char name[GFC_MAX_SYMBOL_LEN + 1];
2564 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2565 /* default to success; will override if find error */
2566 match m = MATCH_YES;
2568 /* Make sure the actual arguments are in the necessary order (based on the
2569 formal args) before resolving. */
2570 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2572 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2573 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2575 set_name_and_label (c, sym, name, binding_label);
2577 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2579 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2581 /* Make sure we got a third arg if the second arg has non-zero
2582 rank. We must also check that the type and rank are
2583 correct since we short-circuit this check in
2584 gfc_procedure_use() (called above to sort actual args). */
2585 if (c->ext.actual->next->expr->rank != 0)
2587 if(c->ext.actual->next->next == NULL
2588 || c->ext.actual->next->next->expr == NULL)
2591 gfc_error ("Missing SHAPE parameter for call to %s "
2592 "at %L", sym->name, &(c->loc));
2594 else if (c->ext.actual->next->next->expr->ts.type
2596 || c->ext.actual->next->next->expr->rank != 1)
2599 gfc_error ("SHAPE parameter for call to %s at %L must "
2600 "be a rank 1 INTEGER array", sym->name,
2607 if (m != MATCH_ERROR)
2609 /* the 1 means to add the optional arg to formal list */
2610 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
2612 /* for error reporting, say it's declared where the original was */
2613 new_sym->declared_at = sym->declared_at;
2618 /* no differences for c_loc or c_funloc */
2622 /* set the resolved symbol */
2623 if (m != MATCH_ERROR)
2624 c->resolved_sym = new_sym;
2626 c->resolved_sym = sym;
2632 /* Resolve a subroutine call known to be specific. */
2635 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
2639 /* See if we have an intrinsic interface. */
2640 if (sym->ts.interface != NULL && !sym->ts.interface->attr.abstract
2641 && !sym->ts.interface->attr.subroutine)
2643 gfc_intrinsic_sym *isym;
2645 isym = gfc_find_function (sym->ts.interface->name);
2647 /* Existance of isym should be checked already. */
2650 sym->ts.type = isym->ts.type;
2651 sym->ts.kind = isym->ts.kind;
2652 sym->attr.subroutine = 1;
2656 if(sym->attr.is_iso_c)
2658 m = gfc_iso_c_sub_interface (c,sym);
2662 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2664 if (sym->attr.dummy)
2666 sym->attr.proc = PROC_DUMMY;
2670 sym->attr.proc = PROC_EXTERNAL;
2674 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
2677 if (sym->attr.intrinsic)
2679 m = gfc_intrinsic_sub_interface (c, 1);
2683 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
2684 "with an intrinsic", sym->name, &c->loc);
2692 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2694 c->resolved_sym = sym;
2695 pure_subroutine (c, sym);
2702 resolve_specific_s (gfc_code *c)
2707 sym = c->symtree->n.sym;
2711 m = resolve_specific_s0 (c, sym);
2714 if (m == MATCH_ERROR)
2717 if (sym->ns->parent == NULL)
2720 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2726 sym = c->symtree->n.sym;
2727 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
2728 sym->name, &c->loc);
2734 /* Resolve a subroutine call not known to be generic nor specific. */
2737 resolve_unknown_s (gfc_code *c)
2741 sym = c->symtree->n.sym;
2743 if (sym->attr.dummy)
2745 sym->attr.proc = PROC_DUMMY;
2749 /* See if we have an intrinsic function reference. */
2751 if (gfc_intrinsic_name (sym->name, 1))
2753 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
2758 /* The reference is to an external name. */
2761 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2763 c->resolved_sym = sym;
2765 pure_subroutine (c, sym);
2771 /* Resolve a subroutine call. Although it was tempting to use the same code
2772 for functions, subroutines and functions are stored differently and this
2773 makes things awkward. */
2776 resolve_call (gfc_code *c)
2779 procedure_type ptype = PROC_INTRINSIC;
2781 if (c->symtree && c->symtree->n.sym
2782 && c->symtree->n.sym->ts.type != BT_UNKNOWN)
2784 gfc_error ("'%s' at %L has a type, which is not consistent with "
2785 "the CALL at %L", c->symtree->n.sym->name,
2786 &c->symtree->n.sym->declared_at, &c->loc);
2790 /* If external, check for usage. */
2791 if (c->symtree && is_external_proc (c->symtree->n.sym))
2792 resolve_global_procedure (c->symtree->n.sym, &c->loc, 1);
2794 /* Subroutines without the RECURSIVE attribution are not allowed to
2795 * call themselves. */
2796 if (c->symtree && c->symtree->n.sym && !c->symtree->n.sym->attr.recursive)
2798 gfc_symbol *csym, *proc;
2799 csym = c->symtree->n.sym;
2800 proc = gfc_current_ns->proc_name;
2803 gfc_error ("SUBROUTINE '%s' at %L cannot call itself, as it is not "
2804 "RECURSIVE", csym->name, &c->loc);
2808 if (csym->attr.entry && csym->ns->entries && proc->ns->entries
2809 && csym->ns->entries->sym == proc->ns->entries->sym)
2811 gfc_error ("Call to ENTRY '%s' at %L is recursive, but subroutine "
2812 "'%s' is not declared as RECURSIVE",
2813 csym->name, &c->loc, csym->ns->entries->sym->name);
2818 /* Switch off assumed size checking and do this again for certain kinds
2819 of procedure, once the procedure itself is resolved. */
2820 need_full_assumed_size++;
2822 if (c->symtree && c->symtree->n.sym)
2823 ptype = c->symtree->n.sym->attr.proc;
2825 if (resolve_actual_arglist (c->ext.actual, ptype) == FAILURE)
2828 /* Resume assumed_size checking. */
2829 need_full_assumed_size--;
2832 if (c->resolved_sym == NULL)
2833 switch (procedure_kind (c->symtree->n.sym))
2836 t = resolve_generic_s (c);
2839 case PTYPE_SPECIFIC:
2840 t = resolve_specific_s (c);
2844 t = resolve_unknown_s (c);
2848 gfc_internal_error ("resolve_subroutine(): bad function type");
2851 /* Some checks of elemental subroutine actual arguments. */
2852 if (resolve_elemental_actual (NULL, c) == FAILURE)
2855 if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
2856 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
2861 /* Compare the shapes of two arrays that have non-NULL shapes. If both
2862 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
2863 match. If both op1->shape and op2->shape are non-NULL return FAILURE
2864 if their shapes do not match. If either op1->shape or op2->shape is
2865 NULL, return SUCCESS. */
2868 compare_shapes (gfc_expr *op1, gfc_expr *op2)
2875 if (op1->shape != NULL && op2->shape != NULL)
2877 for (i = 0; i < op1->rank; i++)
2879 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
2881 gfc_error ("Shapes for operands at %L and %L are not conformable",
2882 &op1->where, &op2->where);
2893 /* Resolve an operator expression node. This can involve replacing the
2894 operation with a user defined function call. */
2897 resolve_operator (gfc_expr *e)
2899 gfc_expr *op1, *op2;
2901 bool dual_locus_error;
2904 /* Resolve all subnodes-- give them types. */
2906 switch (e->value.op.operator)
2909 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
2912 /* Fall through... */
2915 case INTRINSIC_UPLUS:
2916 case INTRINSIC_UMINUS:
2917 case INTRINSIC_PARENTHESES:
2918 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
2923 /* Typecheck the new node. */
2925 op1 = e->value.op.op1;
2926 op2 = e->value.op.op2;
2927 dual_locus_error = false;
2929 if ((op1 && op1->expr_type == EXPR_NULL)
2930 || (op2 && op2->expr_type == EXPR_NULL))
2932 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
2936 switch (e->value.op.operator)
2938 case INTRINSIC_UPLUS:
2939 case INTRINSIC_UMINUS:
2940 if (op1->ts.type == BT_INTEGER
2941 || op1->ts.type == BT_REAL
2942 || op1->ts.type == BT_COMPLEX)
2948 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
2949 gfc_op2string (e->value.op.operator), gfc_typename (&e->ts));
2952 case INTRINSIC_PLUS:
2953 case INTRINSIC_MINUS:
2954 case INTRINSIC_TIMES:
2955 case INTRINSIC_DIVIDE:
2956 case INTRINSIC_POWER:
2957 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
2959 gfc_type_convert_binary (e);
2964 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
2965 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2966 gfc_typename (&op2->ts));
2969 case INTRINSIC_CONCAT:
2970 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
2971 && op1->ts.kind == op2->ts.kind)
2973 e->ts.type = BT_CHARACTER;
2974 e->ts.kind = op1->ts.kind;
2979 _("Operands of string concatenation operator at %%L are %s/%s"),
2980 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
2986 case INTRINSIC_NEQV:
2987 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
2989 e->ts.type = BT_LOGICAL;
2990 e->ts.kind = gfc_kind_max (op1, op2);
2991 if (op1->ts.kind < e->ts.kind)
2992 gfc_convert_type (op1, &e->ts, 2);
2993 else if (op2->ts.kind < e->ts.kind)
2994 gfc_convert_type (op2, &e->ts, 2);
2998 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
2999 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
3000 gfc_typename (&op2->ts));
3005 if (op1->ts.type == BT_LOGICAL)
3007 e->ts.type = BT_LOGICAL;
3008 e->ts.kind = op1->ts.kind;
3012 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3013 gfc_typename (&op1->ts));
3017 case INTRINSIC_GT_OS:
3019 case INTRINSIC_GE_OS:
3021 case INTRINSIC_LT_OS:
3023 case INTRINSIC_LE_OS:
3024 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3026 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3030 /* Fall through... */
3033 case INTRINSIC_EQ_OS:
3035 case INTRINSIC_NE_OS:
3036 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3037 && op1->ts.kind == op2->ts.kind)
3039 e->ts.type = BT_LOGICAL;
3040 e->ts.kind = gfc_default_logical_kind;
3044 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3046 gfc_type_convert_binary (e);
3048 e->ts.type = BT_LOGICAL;
3049 e->ts.kind = gfc_default_logical_kind;
3053 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3055 _("Logicals at %%L must be compared with %s instead of %s"),
3056 (e->value.op.operator == INTRINSIC_EQ
3057 || e->value.op.operator == INTRINSIC_EQ_OS)
3058 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.operator));
3061 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3062 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
3063 gfc_typename (&op2->ts));
3067 case INTRINSIC_USER:
3068 if (e->value.op.uop->operator == NULL)
3069 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3070 else if (op2 == NULL)
3071 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3072 e->value.op.uop->name, gfc_typename (&op1->ts));
3074 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3075 e->value.op.uop->name, gfc_typename (&op1->ts),
3076 gfc_typename (&op2->ts));
3080 case INTRINSIC_PARENTHESES:
3082 if (e->ts.type == BT_CHARACTER)
3083 e->ts.cl = op1->ts.cl;
3087 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3090 /* Deal with arrayness of an operand through an operator. */
3094 switch (e->value.op.operator)
3096 case INTRINSIC_PLUS:
3097 case INTRINSIC_MINUS:
3098 case INTRINSIC_TIMES:
3099 case INTRINSIC_DIVIDE:
3100 case INTRINSIC_POWER:
3101 case INTRINSIC_CONCAT:
3105 case INTRINSIC_NEQV:
3107 case INTRINSIC_EQ_OS:
3109 case INTRINSIC_NE_OS:
3111 case INTRINSIC_GT_OS:
3113 case INTRINSIC_GE_OS:
3115 case INTRINSIC_LT_OS:
3117 case INTRINSIC_LE_OS:
3119 if (op1->rank == 0 && op2->rank == 0)
3122 if (op1->rank == 0 && op2->rank != 0)
3124 e->rank = op2->rank;
3126 if (e->shape == NULL)
3127 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3130 if (op1->rank != 0 && op2->rank == 0)
3132 e->rank = op1->rank;
3134 if (e->shape == NULL)
3135 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3138 if (op1->rank != 0 && op2->rank != 0)
3140 if (op1->rank == op2->rank)
3142 e->rank = op1->rank;
3143 if (e->shape == NULL)
3145 t = compare_shapes(op1, op2);
3149 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3154 /* Allow higher level expressions to work. */
3157 /* Try user-defined operators, and otherwise throw an error. */
3158 dual_locus_error = true;
3160 _("Inconsistent ranks for operator at %%L and %%L"));
3167 case INTRINSIC_PARENTHESES:
3169 case INTRINSIC_UPLUS:
3170 case INTRINSIC_UMINUS:
3171 /* Simply copy arrayness attribute */
3172 e->rank = op1->rank;
3174 if (e->shape == NULL)
3175 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3183 /* Attempt to simplify the expression. */
3186 t = gfc_simplify_expr (e, 0);
3187 /* Some calls do not succeed in simplification and return FAILURE
3188 even though there is no error; eg. variable references to
3189 PARAMETER arrays. */
3190 if (!gfc_is_constant_expr (e))
3197 if (gfc_extend_expr (e) == SUCCESS)
3200 if (dual_locus_error)
3201 gfc_error (msg, &op1->where, &op2->where);
3203 gfc_error (msg, &e->where);
3209 /************** Array resolution subroutines **************/
3212 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3215 /* Compare two integer expressions. */
3218 compare_bound (gfc_expr *a, gfc_expr *b)
3222 if (a == NULL || a->expr_type != EXPR_CONSTANT
3223 || b == NULL || b->expr_type != EXPR_CONSTANT)
3226 /* If either of the types isn't INTEGER, we must have
3227 raised an error earlier. */
3229 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3232 i = mpz_cmp (a->value.integer, b->value.integer);
3242 /* Compare an integer expression with an integer. */
3245 compare_bound_int (gfc_expr *a, int b)
3249 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3252 if (a->ts.type != BT_INTEGER)
3253 gfc_internal_error ("compare_bound_int(): Bad expression");
3255 i = mpz_cmp_si (a->value.integer, b);
3265 /* Compare an integer expression with a mpz_t. */
3268 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3272 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3275 if (a->ts.type != BT_INTEGER)
3276 gfc_internal_error ("compare_bound_int(): Bad expression");
3278 i = mpz_cmp (a->value.integer, b);
3288 /* Compute the last value of a sequence given by a triplet.
3289 Return 0 if it wasn't able to compute the last value, or if the
3290 sequence if empty, and 1 otherwise. */
3293 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3294 gfc_expr *stride, mpz_t last)
3298 if (start == NULL || start->expr_type != EXPR_CONSTANT
3299 || end == NULL || end->expr_type != EXPR_CONSTANT
3300 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3303 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3304 || (stride != NULL && stride->ts.type != BT_INTEGER))
3307 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3309 if (compare_bound (start, end) == CMP_GT)
3311 mpz_set (last, end->value.integer);
3315 if (compare_bound_int (stride, 0) == CMP_GT)
3317 /* Stride is positive */
3318 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3323 /* Stride is negative */
3324 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3329 mpz_sub (rem, end->value.integer, start->value.integer);
3330 mpz_tdiv_r (rem, rem, stride->value.integer);
3331 mpz_sub (last, end->value.integer, rem);
3338 /* Compare a single dimension of an array reference to the array
3342 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3346 /* Given start, end and stride values, calculate the minimum and
3347 maximum referenced indexes. */
3349 switch (ar->dimen_type[i])
3355 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3357 gfc_warning ("Array reference at %L is out of bounds "
3358 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3359 mpz_get_si (ar->start[i]->value.integer),
3360 mpz_get_si (as->lower[i]->value.integer), i+1);
3363 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3365 gfc_warning ("Array reference at %L is out of bounds "
3366 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3367 mpz_get_si (ar->start[i]->value.integer),
3368 mpz_get_si (as->upper[i]->value.integer), i+1);
3376 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3377 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3379 comparison comp_start_end = compare_bound (AR_START, AR_END);
3381 /* Check for zero stride, which is not allowed. */
3382 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3384 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3388 /* if start == len || (stride > 0 && start < len)
3389 || (stride < 0 && start > len),
3390 then the array section contains at least one element. In this
3391 case, there is an out-of-bounds access if
3392 (start < lower || start > upper). */
3393 if (compare_bound (AR_START, AR_END) == CMP_EQ
3394 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3395 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3396 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3397 && comp_start_end == CMP_GT))
3399 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3401 gfc_warning ("Lower array reference at %L is out of bounds "
3402 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3403 mpz_get_si (AR_START->value.integer),
3404 mpz_get_si (as->lower[i]->value.integer), i+1);
3407 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3409 gfc_warning ("Lower array reference at %L is out of bounds "
3410 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3411 mpz_get_si (AR_START->value.integer),
3412 mpz_get_si (as->upper[i]->value.integer), i+1);
3417 /* If we can compute the highest index of the array section,
3418 then it also has to be between lower and upper. */
3419 mpz_init (last_value);
3420 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3423 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3425 gfc_warning ("Upper array reference at %L is out of bounds "
3426 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3427 mpz_get_si (last_value),
3428 mpz_get_si (as->lower[i]->value.integer), i+1);
3429 mpz_clear (last_value);
3432 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3434 gfc_warning ("Upper array reference at %L is out of bounds "
3435 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3436 mpz_get_si (last_value),
3437 mpz_get_si (as->upper[i]->value.integer), i+1);
3438 mpz_clear (last_value);
3442 mpz_clear (last_value);
3450 gfc_internal_error ("check_dimension(): Bad array reference");
3457 /* Compare an array reference with an array specification. */
3460 compare_spec_to_ref (gfc_array_ref *ar)
3467 /* TODO: Full array sections are only allowed as actual parameters. */
3468 if (as->type == AS_ASSUMED_SIZE
3469 && (/*ar->type == AR_FULL
3470 ||*/ (ar->type == AR_SECTION
3471 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3473 gfc_error ("Rightmost upper bound of assumed size array section "
3474 "not specified at %L", &ar->where);
3478 if (ar->type == AR_FULL)
3481 if (as->rank != ar->dimen)
3483 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3484 &ar->where, ar->dimen, as->rank);
3488 for (i = 0; i < as->rank; i++)
3489 if (check_dimension (i, ar, as) == FAILURE)
3496 /* Resolve one part of an array index. */
3499 gfc_resolve_index (gfc_expr *index, int check_scalar)
3506 if (gfc_resolve_expr (index) == FAILURE)
3509 if (check_scalar && index->rank != 0)
3511 gfc_error ("Array index at %L must be scalar", &index->where);
3515 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
3517 gfc_error ("Array index at %L must be of INTEGER type, found %s",
3518 &index->where, gfc_basic_typename (index->ts.type));
3522 if (index->ts.type == BT_REAL)
3523 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
3524 &index->where) == FAILURE)
3527 if (index->ts.kind != gfc_index_integer_kind
3528 || index->ts.type != BT_INTEGER)
3531 ts.type = BT_INTEGER;
3532 ts.kind = gfc_index_integer_kind;
3534 gfc_convert_type_warn (index, &ts, 2, 0);
3540 /* Resolve a dim argument to an intrinsic function. */
3543 gfc_resolve_dim_arg (gfc_expr *dim)
3548 if (gfc_resolve_expr (dim) == FAILURE)
3553 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3558 if (dim->ts.type != BT_INTEGER)
3560 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3564 if (dim->ts.kind != gfc_index_integer_kind)
3568 ts.type = BT_INTEGER;
3569 ts.kind = gfc_index_integer_kind;
3571 gfc_convert_type_warn (dim, &ts, 2, 0);
3577 /* Given an expression that contains array references, update those array
3578 references to point to the right array specifications. While this is
3579 filled in during matching, this information is difficult to save and load
3580 in a module, so we take care of it here.
3582 The idea here is that the original array reference comes from the
3583 base symbol. We traverse the list of reference structures, setting
3584 the stored reference to references. Component references can
3585 provide an additional array specification. */
3588 find_array_spec (gfc_expr *e)
3592 gfc_symbol *derived;
3595 as = e->symtree->n.sym->as;
3598 for (ref = e->ref; ref; ref = ref->next)
3603 gfc_internal_error ("find_array_spec(): Missing spec");
3610 if (derived == NULL)
3611 derived = e->symtree->n.sym->ts.derived;
3613 c = derived->components;
3615 for (; c; c = c->next)
3616 if (c == ref->u.c.component)
3618 /* Track the sequence of component references. */
3619 if (c->ts.type == BT_DERIVED)
3620 derived = c->ts.derived;
3625 gfc_internal_error ("find_array_spec(): Component not found");
3630 gfc_internal_error ("find_array_spec(): unused as(1)");
3641 gfc_internal_error ("find_array_spec(): unused as(2)");
3645 /* Resolve an array reference. */
3648 resolve_array_ref (gfc_array_ref *ar)
3650 int i, check_scalar;
3653 for (i = 0; i < ar->dimen; i++)
3655 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
3657 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
3659 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
3661 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
3666 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
3670 ar->dimen_type[i] = DIMEN_ELEMENT;
3674 ar->dimen_type[i] = DIMEN_VECTOR;
3675 if (e->expr_type == EXPR_VARIABLE
3676 && e->symtree->n.sym->ts.type == BT_DERIVED)
3677 ar->start[i] = gfc_get_parentheses (e);
3681 gfc_error ("Array index at %L is an array of rank %d",
3682 &ar->c_where[i], e->rank);
3687 /* If the reference type is unknown, figure out what kind it is. */
3689 if (ar->type == AR_UNKNOWN)
3691 ar->type = AR_ELEMENT;
3692 for (i = 0; i < ar->dimen; i++)
3693 if (ar->dimen_type[i] == DIMEN_RANGE
3694 || ar->dimen_type[i] == DIMEN_VECTOR)
3696 ar->type = AR_SECTION;
3701 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
3709 resolve_substring (gfc_ref *ref)
3711 if (ref->u.ss.start != NULL)
3713 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
3716 if (ref->u.ss.start->ts.type != BT_INTEGER)
3718 gfc_error ("Substring start index at %L must be of type INTEGER",
3719 &ref->u.ss.start->where);
3723 if (ref->u.ss.start->rank != 0)
3725 gfc_error ("Substring start index at %L must be scalar",
3726 &ref->u.ss.start->where);
3730 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
3731 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
3732 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
3734 gfc_error ("Substring start index at %L is less than one",
3735 &ref->u.ss.start->where);
3740 if (ref->u.ss.end != NULL)
3742 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
3745 if (ref->u.ss.end->ts.type != BT_INTEGER)
3747 gfc_error ("Substring end index at %L must be of type INTEGER",
3748 &ref->u.ss.end->where);
3752 if (ref->u.ss.end->rank != 0)
3754 gfc_error ("Substring end index at %L must be scalar",
3755 &ref->u.ss.end->where);
3759 if (ref->u.ss.length != NULL
3760 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
3761 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
3762 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
3764 gfc_error ("Substring end index at %L exceeds the string length",
3765 &ref->u.ss.start->where);
3774 /* This function supplies missing substring charlens. */
3777 gfc_resolve_substring_charlen (gfc_expr *e)
3780 gfc_expr *start, *end;
3782 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
3783 if (char_ref->type == REF_SUBSTRING)
3789 gcc_assert (char_ref->next == NULL);
3793 if (e->ts.cl->length)
3794 gfc_free_expr (e->ts.cl->length);
3795 else if (e->expr_type == EXPR_VARIABLE
3796 && e->symtree->n.sym->attr.dummy)
3800 e->ts.type = BT_CHARACTER;
3801 e->ts.kind = gfc_default_character_kind;
3805 e->ts.cl = gfc_get_charlen ();
3806 e->ts.cl->next = gfc_current_ns->cl_list;
3807 gfc_current_ns->cl_list = e->ts.cl;
3810 if (char_ref->u.ss.start)
3811 start = gfc_copy_expr (char_ref->u.ss.start);
3813 start = gfc_int_expr (1);
3815 if (char_ref->u.ss.end)
3816 end = gfc_copy_expr (char_ref->u.ss.end);
3817 else if (e->expr_type == EXPR_VARIABLE)
3818 end = gfc_copy_expr (e->symtree->n.sym->ts.cl->length);
3825 /* Length = (end - start +1). */
3826 e->ts.cl->length = gfc_subtract (end, start);
3827 e->ts.cl->length = gfc_add (e->ts.cl->length, gfc_int_expr (1));
3829 e->ts.cl->length->ts.type = BT_INTEGER;
3830 e->ts.cl->length->ts.kind = gfc_charlen_int_kind;;
3832 /* Make sure that the length is simplified. */
3833 gfc_simplify_expr (e->ts.cl->length, 1);
3834 gfc_resolve_expr (e->ts.cl->length);
3838 /* Resolve subtype references. */
3841 resolve_ref (gfc_expr *expr)
3843 int current_part_dimension, n_components, seen_part_dimension;
3846 for (ref = expr->ref; ref; ref = ref->next)
3847 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
3849 find_array_spec (expr);
3853 for (ref = expr->ref; ref; ref = ref->next)
3857 if (resolve_array_ref (&ref->u.ar) == FAILURE)
3865 resolve_substring (ref);
3869 /* Check constraints on part references. */
3871 current_part_dimension = 0;
3872 seen_part_dimension = 0;
3875 for (ref = expr->ref; ref; ref = ref->next)
3880 switch (ref->u.ar.type)
3884 current_part_dimension = 1;
3888 current_part_dimension = 0;
3892 gfc_internal_error ("resolve_ref(): Bad array reference");
3898 if (current_part_dimension || seen_part_dimension)
3900 if (ref->u.c.component->pointer)
3902 gfc_error ("Component to the right of a part reference "
3903 "with nonzero rank must not have the POINTER "
3904 "attribute at %L", &expr->where);
3907 else if (ref->u.c.component->allocatable)
3909 gfc_error ("Component to the right of a part reference "
3910 "with nonzero rank must not have the ALLOCATABLE "
3911 "attribute at %L", &expr->where);
3923 if (((ref->type == REF_COMPONENT && n_components > 1)
3924 || ref->next == NULL)
3925 && current_part_dimension
3926 && seen_part_dimension)
3928 gfc_error ("Two or more part references with nonzero rank must "
3929 "not be specified at %L", &expr->where);
3933 if (ref->type == REF_COMPONENT)
3935 if (current_part_dimension)
3936 seen_part_dimension = 1;
3938 /* reset to make sure */
3939 current_part_dimension = 0;
3947 /* Given an expression, determine its shape. This is easier than it sounds.
3948 Leaves the shape array NULL if it is not possible to determine the shape. */
3951 expression_shape (gfc_expr *e)
3953 mpz_t array[GFC_MAX_DIMENSIONS];
3956 if (e->rank == 0 || e->shape != NULL)
3959 for (i = 0; i < e->rank; i++)
3960 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
3963 e->shape = gfc_get_shape (e->rank);
3965 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
3970 for (i--; i >= 0; i--)
3971 mpz_clear (array[i]);
3975 /* Given a variable expression node, compute the rank of the expression by
3976 examining the base symbol and any reference structures it may have. */
3979 expression_rank (gfc_expr *e)
3986 if (e->expr_type == EXPR_ARRAY)
3988 /* Constructors can have a rank different from one via RESHAPE(). */
3990 if (e->symtree == NULL)
3996 e->rank = (e->symtree->n.sym->as == NULL)
3997 ? 0 : e->symtree->n.sym->as->rank;
4003 for (ref = e->ref; ref; ref = ref->next)
4005 if (ref->type != REF_ARRAY)
4008 if (ref->u.ar.type == AR_FULL)
4010 rank = ref->u.ar.as->rank;
4014 if (ref->u.ar.type == AR_SECTION)
4016 /* Figure out the rank of the section. */
4018 gfc_internal_error ("expression_rank(): Two array specs");
4020 for (i = 0; i < ref->u.ar.dimen; i++)
4021 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4022 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4032 expression_shape (e);
4036 /* Resolve a variable expression. */
4039 resolve_variable (gfc_expr *e)
4046 if (e->symtree == NULL)
4049 if (e->ref && resolve_ref (e) == FAILURE)
4052 sym = e->symtree->n.sym;
4053 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
4055 e->ts.type = BT_PROCEDURE;
4059 if (sym->ts.type != BT_UNKNOWN)
4060 gfc_variable_attr (e, &e->ts);
4063 /* Must be a simple variable reference. */
4064 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4069 if (check_assumed_size_reference (sym, e))
4072 /* Deal with forward references to entries during resolve_code, to
4073 satisfy, at least partially, 12.5.2.5. */
4074 if (gfc_current_ns->entries
4075 && current_entry_id == sym->entry_id
4078 && cs_base->current->op != EXEC_ENTRY)
4080 gfc_entry_list *entry;
4081 gfc_formal_arglist *formal;
4085 /* If the symbol is a dummy... */
4086 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4088 entry = gfc_current_ns->entries;
4091 /* ...test if the symbol is a parameter of previous entries. */
4092 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4093 for (formal = entry->sym->formal; formal; formal = formal->next)
4095 if (formal->sym && sym->name == formal->sym->name)
4099 /* If it has not been seen as a dummy, this is an error. */
4102 if (specification_expr)
4103 gfc_error ("Variable '%s', used in a specification expression"
4104 ", is referenced at %L before the ENTRY statement "
4105 "in which it is a parameter",
4106 sym->name, &cs_base->current->loc);
4108 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4109 "statement in which it is a parameter",
4110 sym->name, &cs_base->current->loc);
4115 /* Now do the same check on the specification expressions. */
4116 specification_expr = 1;
4117 if (sym->ts.type == BT_CHARACTER
4118 && gfc_resolve_expr (sym->ts.cl->length) == FAILURE)
4122 for (n = 0; n < sym->as->rank; n++)
4124 specification_expr = 1;
4125 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4127 specification_expr = 1;
4128 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4131 specification_expr = 0;
4134 /* Update the symbol's entry level. */
4135 sym->entry_id = current_entry_id + 1;
4142 /* Checks to see that the correct symbol has been host associated.
4143 The only situation where this arises is that in which a twice
4144 contained function is parsed after the host association is made.
4145 Therefore, on detecting this, the line is rematched, having got
4146 rid of the existing references and actual_arg_list. */
4148 check_host_association (gfc_expr *e)
4150 gfc_symbol *sym, *old_sym;
4154 bool retval = e->expr_type == EXPR_FUNCTION;
4156 if (e->symtree == NULL || e->symtree->n.sym == NULL)
4159 old_sym = e->symtree->n.sym;
4161 if (old_sym->attr.use_assoc)
4164 if (gfc_current_ns->parent
4165 && old_sym->ns != gfc_current_ns)
4167 gfc_find_symbol (old_sym->name, gfc_current_ns, 1, &sym);
4168 if (sym && old_sym != sym
4169 && sym->attr.flavor == FL_PROCEDURE
4170 && sym->attr.contained)
4172 temp_locus = gfc_current_locus;
4173 gfc_current_locus = e->where;
4175 gfc_buffer_error (1);
4177 gfc_free_ref_list (e->ref);
4182 gfc_free_actual_arglist (e->value.function.actual);
4183 e->value.function.actual = NULL;
4186 if (e->shape != NULL)
4188 for (n = 0; n < e->rank; n++)
4189 mpz_clear (e->shape[n]);
4191 gfc_free (e->shape);
4194 gfc_match_rvalue (&expr);
4196 gfc_buffer_error (0);
4198 gcc_assert (expr && sym == expr->symtree->n.sym);
4204 gfc_current_locus = temp_locus;
4207 /* This might have changed! */
4208 return e->expr_type == EXPR_FUNCTION;
4213 gfc_resolve_character_operator (gfc_expr *e)
4215 gfc_expr *op1 = e->value.op.op1;
4216 gfc_expr *op2 = e->value.op.op2;
4217 gfc_expr *e1 = NULL;
4218 gfc_expr *e2 = NULL;
4220 gcc_assert (e->value.op.operator == INTRINSIC_CONCAT);
4222 if (op1->ts.cl && op1->ts.cl->length)
4223 e1 = gfc_copy_expr (op1->ts.cl->length);
4224 else if (op1->expr_type == EXPR_CONSTANT)
4225 e1 = gfc_int_expr (op1->value.character.length);
4227 if (op2->ts.cl && op2->ts.cl->length)
4228 e2 = gfc_copy_expr (op2->ts.cl->length);
4229 else if (op2->expr_type == EXPR_CONSTANT)
4230 e2 = gfc_int_expr (op2->value.character.length);
4232 e->ts.cl = gfc_get_charlen ();
4233 e->ts.cl->next = gfc_current_ns->cl_list;
4234 gfc_current_ns->cl_list = e->ts.cl;
4239 e->ts.cl->length = gfc_add (e1, e2);
4240 e->ts.cl->length->ts.type = BT_INTEGER;
4241 e->ts.cl->length->ts.kind = gfc_charlen_int_kind;;
4242 gfc_simplify_expr (e->ts.cl->length, 0);
4243 gfc_resolve_expr (e->ts.cl->length);
4249 /* Ensure that an character expression has a charlen and, if possible, a
4250 length expression. */
4253 fixup_charlen (gfc_expr *e)
4255 /* The cases fall through so that changes in expression type and the need
4256 for multiple fixes are picked up. In all circumstances, a charlen should
4257 be available for the middle end to hang a backend_decl on. */
4258 switch (e->expr_type)
4261 gfc_resolve_character_operator (e);
4264 if (e->expr_type == EXPR_ARRAY)
4265 gfc_resolve_character_array_constructor (e);
4267 case EXPR_SUBSTRING:
4268 if (!e->ts.cl && e->ref)
4269 gfc_resolve_substring_charlen (e);
4274 e->ts.cl = gfc_get_charlen ();
4275 e->ts.cl->next = gfc_current_ns->cl_list;
4276 gfc_current_ns->cl_list = e->ts.cl;
4284 /* Resolve an expression. That is, make sure that types of operands agree
4285 with their operators, intrinsic operators are converted to function calls
4286 for overloaded types and unresolved function references are resolved. */
4289 gfc_resolve_expr (gfc_expr *e)
4296 switch (e->expr_type)
4299 t = resolve_operator (e);
4305 if (check_host_association (e))
4306 t = resolve_function (e);
4309 t = resolve_variable (e);
4311 expression_rank (e);
4314 if (e->ts.type == BT_CHARACTER && e->ts.cl == NULL && e->ref
4315 && e->ref->type != REF_SUBSTRING)
4316 gfc_resolve_substring_charlen (e);
4320 case EXPR_SUBSTRING:
4321 t = resolve_ref (e);
4331 if (resolve_ref (e) == FAILURE)
4334 t = gfc_resolve_array_constructor (e);
4335 /* Also try to expand a constructor. */
4338 expression_rank (e);
4339 gfc_expand_constructor (e);
4342 /* This provides the opportunity for the length of constructors with
4343 character valued function elements to propagate the string length
4344 to the expression. */
4345 if (e->ts.type == BT_CHARACTER)
4346 gfc_resolve_character_array_constructor (e);
4350 case EXPR_STRUCTURE:
4351 t = resolve_ref (e);
4355 t = resolve_structure_cons (e);
4359 t = gfc_simplify_expr (e, 0);
4363 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
4366 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.cl)
4373 /* Resolve an expression from an iterator. They must be scalar and have
4374 INTEGER or (optionally) REAL type. */
4377 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
4378 const char *name_msgid)
4380 if (gfc_resolve_expr (expr) == FAILURE)
4383 if (expr->rank != 0)
4385 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
4389 if (expr->ts.type != BT_INTEGER)
4391 if (expr->ts.type == BT_REAL)
4394 return gfc_notify_std (GFC_STD_F95_DEL,
4395 "Deleted feature: %s at %L must be integer",
4396 _(name_msgid), &expr->where);
4399 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
4406 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
4414 /* Resolve the expressions in an iterator structure. If REAL_OK is
4415 false allow only INTEGER type iterators, otherwise allow REAL types. */
4418 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
4420 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
4424 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
4426 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
4431 if (gfc_resolve_iterator_expr (iter->start, real_ok,
4432 "Start expression in DO loop") == FAILURE)
4435 if (gfc_resolve_iterator_expr (iter->end, real_ok,
4436 "End expression in DO loop") == FAILURE)
4439 if (gfc_resolve_iterator_expr (iter->step, real_ok,
4440 "Step expression in DO loop") == FAILURE)
4443 if (iter->step->expr_type == EXPR_CONSTANT)
4445 if ((iter->step->ts.type == BT_INTEGER
4446 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
4447 || (iter->step->ts.type == BT_REAL
4448 && mpfr_sgn (iter->step->value.real) == 0))
4450 gfc_error ("Step expression in DO loop at %L cannot be zero",
4451 &iter->step->where);
4456 /* Convert start, end, and step to the same type as var. */
4457 if (iter->start->ts.kind != iter->var->ts.kind
4458 || iter->start->ts.type != iter->var->ts.type)
4459 gfc_convert_type (iter->start, &iter->var->ts, 2);
4461 if (iter->end->ts.kind != iter->var->ts.kind
4462 || iter->end->ts.type != iter->var->ts.type)
4463 gfc_convert_type (iter->end, &iter->var->ts, 2);
4465 if (iter->step->ts.kind != iter->var->ts.kind
4466 || iter->step->ts.type != iter->var->ts.type)
4467 gfc_convert_type (iter->step, &iter->var->ts, 2);
4473 /* Traversal function for find_forall_index. f == 2 signals that
4474 that variable itself is not to be checked - only the references. */
4477 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
4479 if (expr->expr_type != EXPR_VARIABLE)
4482 /* A scalar assignment */
4483 if (!expr->ref || *f == 1)
4485 if (expr->symtree->n.sym == sym)
4497 /* Check whether the FORALL index appears in the expression or not.
4498 Returns SUCCESS if SYM is found in EXPR. */
4501 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
4503 if (gfc_traverse_expr (expr, sym, forall_index, f))
4510 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
4511 to be a scalar INTEGER variable. The subscripts and stride are scalar
4512 INTEGERs, and if stride is a constant it must be nonzero.
4513 Furthermore "A subscript or stride in a forall-triplet-spec shall
4514 not contain a reference to any index-name in the
4515 forall-triplet-spec-list in which it appears." (7.5.4.1) */
4518 resolve_forall_iterators (gfc_forall_iterator *it)
4520 gfc_forall_iterator *iter, *iter2;
4522 for (iter = it; iter; iter = iter->next)
4524 if (gfc_resolve_expr (iter->var) == SUCCESS
4525 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
4526 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
4529 if (gfc_resolve_expr (iter->start) == SUCCESS
4530 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
4531 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
4532 &iter->start->where);
4533 if (iter->var->ts.kind != iter->start->ts.kind)
4534 gfc_convert_type (iter->start, &iter->var->ts, 2);
4536 if (gfc_resolve_expr (iter->end) == SUCCESS
4537 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
4538 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
4540 if (iter->var->ts.kind != iter->end->ts.kind)
4541 gfc_convert_type (iter->end, &iter->var->ts, 2);
4543 if (gfc_resolve_expr (iter->stride) == SUCCESS)
4545 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
4546 gfc_error ("FORALL stride expression at %L must be a scalar %s",
4547 &iter->stride->where, "INTEGER");
4549 if (iter->stride->expr_type == EXPR_CONSTANT
4550 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
4551 gfc_error ("FORALL stride expression at %L cannot be zero",
4552 &iter->stride->where);
4554 if (iter->var->ts.kind != iter->stride->ts.kind)
4555 gfc_convert_type (iter->stride, &iter->var->ts, 2);
4558 for (iter = it; iter; iter = iter->next)
4559 for (iter2 = iter; iter2; iter2 = iter2->next)
4561 if (find_forall_index (iter2->start,
4562 iter->var->symtree->n.sym, 0) == SUCCESS
4563 || find_forall_index (iter2->end,
4564 iter->var->symtree->n.sym, 0) == SUCCESS
4565 || find_forall_index (iter2->stride,
4566 iter->var->symtree->n.sym, 0) == SUCCESS)
4567 gfc_error ("FORALL index '%s' may not appear in triplet "
4568 "specification at %L", iter->var->symtree->name,
4569 &iter2->start->where);
4574 /* Given a pointer to a symbol that is a derived type, see if it's
4575 inaccessible, i.e. if it's defined in another module and the components are
4576 PRIVATE. The search is recursive if necessary. Returns zero if no
4577 inaccessible components are found, nonzero otherwise. */
4580 derived_inaccessible (gfc_symbol *sym)
4584 if (sym->attr.use_assoc && sym->attr.private_comp)
4587 for (c = sym->components; c; c = c->next)
4589 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.derived))
4597 /* Resolve the argument of a deallocate expression. The expression must be
4598 a pointer or a full array. */
4601 resolve_deallocate_expr (gfc_expr *e)
4603 symbol_attribute attr;
4604 int allocatable, pointer, check_intent_in;
4607 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
4608 check_intent_in = 1;
4610 if (gfc_resolve_expr (e) == FAILURE)
4613 if (e->expr_type != EXPR_VARIABLE)
4616 allocatable = e->symtree->n.sym->attr.allocatable;
4617 pointer = e->symtree->n.sym->attr.pointer;
4618 for (ref = e->ref; ref; ref = ref->next)
4621 check_intent_in = 0;
4626 if (ref->u.ar.type != AR_FULL)
4631 allocatable = (ref->u.c.component->as != NULL
4632 && ref->u.c.component->as->type == AS_DEFERRED);
4633 pointer = ref->u.c.component->pointer;
4642 attr = gfc_expr_attr (e);
4644 if (allocatable == 0 && attr.pointer == 0)
4647 gfc_error ("Expression in DEALLOCATE statement at %L must be "
4648 "ALLOCATABLE or a POINTER", &e->where);
4652 && e->symtree->n.sym->attr.intent == INTENT_IN)
4654 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
4655 e->symtree->n.sym->name, &e->where);
4663 /* Returns true if the expression e contains a reference to the symbol sym. */
4665 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
4667 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
4674 find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
4676 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
4680 /* Given the expression node e for an allocatable/pointer of derived type to be
4681 allocated, get the expression node to be initialized afterwards (needed for
4682 derived types with default initializers, and derived types with allocatable
4683 components that need nullification.) */
4686 expr_to_initialize (gfc_expr *e)
4692 result = gfc_copy_expr (e);
4694 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
4695 for (ref = result->ref; ref; ref = ref->next)
4696 if (ref->type == REF_ARRAY && ref->next == NULL)
4698 ref->u.ar.type = AR_FULL;
4700 for (i = 0; i < ref->u.ar.dimen; i++)
4701 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
4703 result->rank = ref->u.ar.dimen;
4711 /* Resolve the expression in an ALLOCATE statement, doing the additional
4712 checks to see whether the expression is OK or not. The expression must
4713 have a trailing array reference that gives the size of the array. */
4716 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
4718 int i, pointer, allocatable, dimension, check_intent_in;
4719 symbol_attribute attr;
4720 gfc_ref *ref, *ref2;
4727 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
4728 check_intent_in = 1;
4730 if (gfc_resolve_expr (e) == FAILURE)
4733 if (code->expr && code->expr->expr_type == EXPR_VARIABLE)
4734 sym = code->expr->symtree->n.sym;
4738 /* Make sure the expression is allocatable or a pointer. If it is
4739 pointer, the next-to-last reference must be a pointer. */
4743 if (e->expr_type != EXPR_VARIABLE)
4746 attr = gfc_expr_attr (e);
4747 pointer = attr.pointer;
4748 dimension = attr.dimension;
4752 allocatable = e->symtree->n.sym->attr.allocatable;
4753 pointer = e->symtree->n.sym->attr.pointer;
4754 dimension = e->symtree->n.sym->attr.dimension;
4756 if (sym == e->symtree->n.sym && sym->ts.type != BT_DERIVED)
4758 gfc_error ("The STAT variable '%s' in an ALLOCATE statement must "
4759 "not be allocated in the same statement at %L",
4760 sym->name, &e->where);
4764 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
4767 check_intent_in = 0;
4772 if (ref->next != NULL)
4777 allocatable = (ref->u.c.component->as != NULL
4778 && ref->u.c.component->as->type == AS_DEFERRED);
4780 pointer = ref->u.c.component->pointer;
4781 dimension = ref->u.c.component->dimension;
4792 if (allocatable == 0 && pointer == 0)
4794 gfc_error ("Expression in ALLOCATE statement at %L must be "
4795 "ALLOCATABLE or a POINTER", &e->where);
4800 && e->symtree->n.sym->attr.intent == INTENT_IN)
4802 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
4803 e->symtree->n.sym->name, &e->where);
4807 /* Add default initializer for those derived types that need them. */
4808 if (e->ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&e->ts)))
4810 init_st = gfc_get_code ();
4811 init_st->loc = code->loc;
4812 init_st->op = EXEC_INIT_ASSIGN;
4813 init_st->expr = expr_to_initialize (e);
4814 init_st->expr2 = init_e;
4815 init_st->next = code->next;
4816 code->next = init_st;
4819 if (pointer && dimension == 0)
4822 /* Make sure the next-to-last reference node is an array specification. */
4824 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
4826 gfc_error ("Array specification required in ALLOCATE statement "
4827 "at %L", &e->where);
4831 /* Make sure that the array section reference makes sense in the
4832 context of an ALLOCATE specification. */
4836 for (i = 0; i < ar->dimen; i++)
4838 if (ref2->u.ar.type == AR_ELEMENT)
4841 switch (ar->dimen_type[i])
4847 if (ar->start[i] != NULL
4848 && ar->end[i] != NULL
4849 && ar->stride[i] == NULL)
4852 /* Fall Through... */
4856 gfc_error ("Bad array specification in ALLOCATE statement at %L",
4863 for (a = code->ext.alloc_list; a; a = a->next)
4865 sym = a->expr->symtree->n.sym;
4867 /* TODO - check derived type components. */
4868 if (sym->ts.type == BT_DERIVED)
4871 if ((ar->start[i] != NULL && find_sym_in_expr (sym, ar->start[i]))
4872 || (ar->end[i] != NULL && find_sym_in_expr (sym, ar->end[i])))
4874 gfc_error ("'%s' must not appear an the array specification at "
4875 "%L in the same ALLOCATE statement where it is "
4876 "itself allocated", sym->name, &ar->where);
4886 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
4888 gfc_symbol *s = NULL;
4892 s = code->expr->symtree->n.sym;
4896 if (s->attr.intent == INTENT_IN)
4897 gfc_error ("STAT variable '%s' of %s statement at %C cannot "
4898 "be INTENT(IN)", s->name, fcn);
4900 if (gfc_pure (NULL) && gfc_impure_variable (s))
4901 gfc_error ("Illegal STAT variable in %s statement at %C "
4902 "for a PURE procedure", fcn);
4905 if (s && code->expr->ts.type != BT_INTEGER)
4906 gfc_error ("STAT tag in %s statement at %L must be "
4907 "of type INTEGER", fcn, &code->expr->where);
4909 if (strcmp (fcn, "ALLOCATE") == 0)
4911 for (a = code->ext.alloc_list; a; a = a->next)
4912 resolve_allocate_expr (a->expr, code);
4916 for (a = code->ext.alloc_list; a; a = a->next)
4917 resolve_deallocate_expr (a->expr);
4921 /************ SELECT CASE resolution subroutines ************/
4923 /* Callback function for our mergesort variant. Determines interval
4924 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
4925 op1 > op2. Assumes we're not dealing with the default case.
4926 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
4927 There are nine situations to check. */
4930 compare_cases (const gfc_case *op1, const gfc_case *op2)
4934 if (op1->low == NULL) /* op1 = (:L) */
4936 /* op2 = (:N), so overlap. */
4938 /* op2 = (M:) or (M:N), L < M */
4939 if (op2->low != NULL
4940 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
4943 else if (op1->high == NULL) /* op1 = (K:) */
4945 /* op2 = (M:), so overlap. */
4947 /* op2 = (:N) or (M:N), K > N */
4948 if (op2->high != NULL
4949 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
4952 else /* op1 = (K:L) */
4954 if (op2->low == NULL) /* op2 = (:N), K > N */
4955 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
4957 else if (op2->high == NULL) /* op2 = (M:), L < M */
4958 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
4960 else /* op2 = (M:N) */
4964 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
4967 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
4976 /* Merge-sort a double linked case list, detecting overlap in the
4977 process. LIST is the head of the double linked case list before it
4978 is sorted. Returns the head of the sorted list if we don't see any
4979 overlap, or NULL otherwise. */
4982 check_case_overlap (gfc_case *list)
4984 gfc_case *p, *q, *e, *tail;
4985 int insize, nmerges, psize, qsize, cmp, overlap_seen;
4987 /* If the passed list was empty, return immediately. */
4994 /* Loop unconditionally. The only exit from this loop is a return
4995 statement, when we've finished sorting the case list. */
5002 /* Count the number of merges we do in this pass. */
5005 /* Loop while there exists a merge to be done. */
5010 /* Count this merge. */
5013 /* Cut the list in two pieces by stepping INSIZE places
5014 forward in the list, starting from P. */
5017 for (i = 0; i < insize; i++)
5026 /* Now we have two lists. Merge them! */
5027 while (psize > 0 || (qsize > 0 && q != NULL))
5029 /* See from which the next case to merge comes from. */
5032 /* P is empty so the next case must come from Q. */
5037 else if (qsize == 0 || q == NULL)
5046 cmp = compare_cases (p, q);
5049 /* The whole case range for P is less than the
5057 /* The whole case range for Q is greater than
5058 the case range for P. */
5065 /* The cases overlap, or they are the same
5066 element in the list. Either way, we must
5067 issue an error and get the next case from P. */
5068 /* FIXME: Sort P and Q by line number. */
5069 gfc_error ("CASE label at %L overlaps with CASE "
5070 "label at %L", &p->where, &q->where);
5078 /* Add the next element to the merged list. */
5087 /* P has now stepped INSIZE places along, and so has Q. So
5088 they're the same. */
5093 /* If we have done only one merge or none at all, we've
5094 finished sorting the cases. */
5103 /* Otherwise repeat, merging lists twice the size. */
5109 /* Check to see if an expression is suitable for use in a CASE statement.
5110 Makes sure that all case expressions are scalar constants of the same
5111 type. Return FAILURE if anything is wrong. */
5114 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
5116 if (e == NULL) return SUCCESS;
5118 if (e->ts.type != case_expr->ts.type)
5120 gfc_error ("Expression in CASE statement at %L must be of type %s",
5121 &e->where, gfc_basic_typename (case_expr->ts.type));
5125 /* C805 (R808) For a given case-construct, each case-value shall be of
5126 the same type as case-expr. For character type, length differences
5127 are allowed, but the kind type parameters shall be the same. */
5129 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
5131 gfc_error ("Expression in CASE statement at %L must be of kind %d",
5132 &e->where, case_expr->ts.kind);
5136 /* Convert the case value kind to that of case expression kind, if needed.
5137 FIXME: Should a warning be issued? */
5138 if (e->ts.kind != case_expr->ts.kind)
5139 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
5143 gfc_error ("Expression in CASE statement at %L must be scalar",
5152 /* Given a completely parsed select statement, we:
5154 - Validate all expressions and code within the SELECT.
5155 - Make sure that the selection expression is not of the wrong type.
5156 - Make sure that no case ranges overlap.
5157 - Eliminate unreachable cases and unreachable code resulting from
5158 removing case labels.
5160 The standard does allow unreachable cases, e.g. CASE (5:3). But
5161 they are a hassle for code generation, and to prevent that, we just
5162 cut them out here. This is not necessary for overlapping cases
5163 because they are illegal and we never even try to generate code.
5165 We have the additional caveat that a SELECT construct could have
5166 been a computed GOTO in the source code. Fortunately we can fairly
5167 easily work around that here: The case_expr for a "real" SELECT CASE
5168 is in code->expr1, but for a computed GOTO it is in code->expr2. All
5169 we have to do is make sure that the case_expr is a scalar integer
5173 resolve_select (gfc_code *code)
5176 gfc_expr *case_expr;
5177 gfc_case *cp, *default_case, *tail, *head;
5178 int seen_unreachable;
5184 if (code->expr == NULL)
5186 /* This was actually a computed GOTO statement. */
5187 case_expr = code->expr2;
5188 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
5189 gfc_error ("Selection expression in computed GOTO statement "
5190 "at %L must be a scalar integer expression",
5193 /* Further checking is not necessary because this SELECT was built
5194 by the compiler, so it should always be OK. Just move the
5195 case_expr from expr2 to expr so that we can handle computed
5196 GOTOs as normal SELECTs from here on. */
5197 code->expr = code->expr2;
5202 case_expr = code->expr;
5204 type = case_expr->ts.type;
5205 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
5207 gfc_error ("Argument of SELECT statement at %L cannot be %s",
5208 &case_expr->where, gfc_typename (&case_expr->ts));
5210 /* Punt. Going on here just produce more garbage error messages. */
5214 if (case_expr->rank != 0)
5216 gfc_error ("Argument of SELECT statement at %L must be a scalar "
5217 "expression", &case_expr->where);
5223 /* PR 19168 has a long discussion concerning a mismatch of the kinds
5224 of the SELECT CASE expression and its CASE values. Walk the lists
5225 of case values, and if we find a mismatch, promote case_expr to
5226 the appropriate kind. */
5228 if (type == BT_LOGICAL || type == BT_INTEGER)
5230 for (body = code->block; body; body = body->block)
5232 /* Walk the case label list. */
5233 for (cp = body->ext.case_list; cp; cp = cp->next)
5235 /* Intercept the DEFAULT case. It does not have a kind. */
5236 if (cp->low == NULL && cp->high == NULL)
5239 /* Unreachable case ranges are discarded, so ignore. */
5240 if (cp->low != NULL && cp->high != NULL
5241 && cp->low != cp->high
5242 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5245 /* FIXME: Should a warning be issued? */
5247 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
5248 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
5250 if (cp->high != NULL
5251 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
5252 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
5257 /* Assume there is no DEFAULT case. */
5258 default_case = NULL;
5263 for (body = code->block; body; body = body->block)
5265 /* Assume the CASE list is OK, and all CASE labels can be matched. */
5267 seen_unreachable = 0;
5269 /* Walk the case label list, making sure that all case labels
5271 for (cp = body->ext.case_list; cp; cp = cp->next)
5273 /* Count the number of cases in the whole construct. */
5276 /* Intercept the DEFAULT case. */
5277 if (cp->low == NULL && cp->high == NULL)
5279 if (default_case != NULL)
5281 gfc_error ("The DEFAULT CASE at %L cannot be followed "
5282 "by a second DEFAULT CASE at %L",
5283 &default_case->where, &cp->where);
5294 /* Deal with single value cases and case ranges. Errors are
5295 issued from the validation function. */
5296 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
5297 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
5303 if (type == BT_LOGICAL
5304 && ((cp->low == NULL || cp->high == NULL)
5305 || cp->low != cp->high))
5307 gfc_error ("Logical range in CASE statement at %L is not "
5308 "allowed", &cp->low->where);
5313 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
5316 value = cp->low->value.logical == 0 ? 2 : 1;
5317 if (value & seen_logical)
5319 gfc_error ("constant logical value in CASE statement "
5320 "is repeated at %L",
5325 seen_logical |= value;
5328 if (cp->low != NULL && cp->high != NULL
5329 && cp->low != cp->high
5330 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5332 if (gfc_option.warn_surprising)
5333 gfc_warning ("Range specification at %L can never "
5334 "be matched", &cp->where);
5336 cp->unreachable = 1;
5337 seen_unreachable = 1;
5341 /* If the case range can be matched, it can also overlap with
5342 other cases. To make sure it does not, we put it in a
5343 double linked list here. We sort that with a merge sort
5344 later on to detect any overlapping cases. */
5348 head->right = head->left = NULL;
5353 tail->right->left = tail;
5360 /* It there was a failure in the previous case label, give up
5361 for this case label list. Continue with the next block. */
5365 /* See if any case labels that are unreachable have been seen.
5366 If so, we eliminate them. This is a bit of a kludge because
5367 the case lists for a single case statement (label) is a
5368 single forward linked lists. */
5369 if (seen_unreachable)
5371 /* Advance until the first case in the list is reachable. */
5372 while (body->ext.case_list != NULL
5373 && body->ext.case_list->unreachable)
5375 gfc_case *n = body->ext.case_list;
5376 body->ext.case_list = body->ext.case_list->next;
5378 gfc_free_case_list (n);
5381 /* Strip all other unreachable cases. */
5382 if (body->ext.case_list)
5384 for (cp = body->ext.case_list; cp->next; cp = cp->next)
5386 if (cp->next->unreachable)
5388 gfc_case *n = cp->next;
5389 cp->next = cp->next->next;
5391 gfc_free_case_list (n);
5398 /* See if there were overlapping cases. If the check returns NULL,
5399 there was overlap. In that case we don't do anything. If head
5400 is non-NULL, we prepend the DEFAULT case. The sorted list can
5401 then used during code generation for SELECT CASE constructs with
5402 a case expression of a CHARACTER type. */
5405 head = check_case_overlap (head);
5407 /* Prepend the default_case if it is there. */
5408 if (head != NULL && default_case)
5410 default_case->left = NULL;
5411 default_case->right = head;
5412 head->left = default_case;
5416 /* Eliminate dead blocks that may be the result if we've seen
5417 unreachable case labels for a block. */
5418 for (body = code; body && body->block; body = body->block)
5420 if (body->block->ext.case_list == NULL)
5422 /* Cut the unreachable block from the code chain. */
5423 gfc_code *c = body->block;
5424 body->block = c->block;
5426 /* Kill the dead block, but not the blocks below it. */
5428 gfc_free_statements (c);
5432 /* More than two cases is legal but insane for logical selects.
5433 Issue a warning for it. */
5434 if (gfc_option.warn_surprising && type == BT_LOGICAL
5436 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
5441 /* Resolve a transfer statement. This is making sure that:
5442 -- a derived type being transferred has only non-pointer components
5443 -- a derived type being transferred doesn't have private components, unless
5444 it's being transferred from the module where the type was defined
5445 -- we're not trying to transfer a whole assumed size array. */
5448 resolve_transfer (gfc_code *code)
5457 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
5460 sym = exp->symtree->n.sym;
5463 /* Go to actual component transferred. */
5464 for (ref = code->expr->ref; ref; ref = ref->next)
5465 if (ref->type == REF_COMPONENT)
5466 ts = &ref->u.c.component->ts;
5468 if (ts->type == BT_DERIVED)
5470 /* Check that transferred derived type doesn't contain POINTER
5472 if (ts->derived->attr.pointer_comp)
5474 gfc_error ("Data transfer element at %L cannot have "
5475 "POINTER components", &code->loc);
5479 if (ts->derived->attr.alloc_comp)
5481 gfc_error ("Data transfer element at %L cannot have "
5482 "ALLOCATABLE components", &code->loc);
5486 if (derived_inaccessible (ts->derived))
5488 gfc_error ("Data transfer element at %L cannot have "
5489 "PRIVATE components",&code->loc);
5494 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
5495 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
5497 gfc_error ("Data transfer element at %L cannot be a full reference to "
5498 "an assumed-size array", &code->loc);
5504 /*********** Toplevel code resolution subroutines ***********/
5506 /* Find the set of labels that are reachable from this block. We also
5507 record the last statement in each block so that we don't have to do
5508 a linear search to find the END DO statements of the blocks. */
5511 reachable_labels (gfc_code *block)
5518 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
5520 /* Collect labels in this block. */
5521 for (c = block; c; c = c->next)
5524 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
5526 if (!c->next && cs_base->prev)
5527 cs_base->prev->tail = c;
5530 /* Merge with labels from parent block. */
5533 gcc_assert (cs_base->prev->reachable_labels);
5534 bitmap_ior_into (cs_base->reachable_labels,
5535 cs_base->prev->reachable_labels);
5539 /* Given a branch to a label and a namespace, if the branch is conforming.
5540 The code node describes where the branch is located. */
5543 resolve_branch (gfc_st_label *label, gfc_code *code)
5550 /* Step one: is this a valid branching target? */
5552 if (label->defined == ST_LABEL_UNKNOWN)
5554 gfc_error ("Label %d referenced at %L is never defined", label->value,
5559 if (label->defined != ST_LABEL_TARGET)
5561 gfc_error ("Statement at %L is not a valid branch target statement "
5562 "for the branch statement at %L", &label->where, &code->loc);
5566 /* Step two: make sure this branch is not a branch to itself ;-) */
5568 if (code->here == label)
5570 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
5574 /* Step three: See if the label is in the same block as the
5575 branching statement. The hard work has been done by setting up
5576 the bitmap reachable_labels. */
5578 if (!bitmap_bit_p (cs_base->reachable_labels, label->value))
5580 /* The label is not in an enclosing block, so illegal. This was
5581 allowed in Fortran 66, so we allow it as extension. No
5582 further checks are necessary in this case. */
5583 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
5584 "as the GOTO statement at %L", &label->where,
5589 /* Step four: Make sure that the branching target is legal if
5590 the statement is an END {SELECT,IF}. */
5592 for (stack = cs_base; stack; stack = stack->prev)
5593 if (stack->current->next && stack->current->next->here == label)
5596 if (stack && stack->current->next->op == EXEC_NOP)
5598 gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: GOTO at %L jumps to "
5599 "END of construct at %L", &code->loc,
5600 &stack->current->next->loc);
5601 return; /* We know this is not an END DO. */
5604 /* Step five: Make sure that we're not jumping to the end of a DO
5605 loop from within the loop. */
5607 for (stack = cs_base; stack; stack = stack->prev)
5608 if ((stack->current->op == EXEC_DO
5609 || stack->current->op == EXEC_DO_WHILE)
5610 && stack->tail->here == label && stack->tail->op == EXEC_NOP)
5612 gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: GOTO at %L jumps "
5613 "to END of construct at %L", &code->loc,
5621 /* Check whether EXPR1 has the same shape as EXPR2. */
5624 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
5626 mpz_t shape[GFC_MAX_DIMENSIONS];
5627 mpz_t shape2[GFC_MAX_DIMENSIONS];
5628 try result = FAILURE;
5631 /* Compare the rank. */
5632 if (expr1->rank != expr2->rank)
5635 /* Compare the size of each dimension. */
5636 for (i=0; i<expr1->rank; i++)
5638 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
5641 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
5644 if (mpz_cmp (shape[i], shape2[i]))
5648 /* When either of the two expression is an assumed size array, we
5649 ignore the comparison of dimension sizes. */
5654 for (i--; i >= 0; i--)
5656 mpz_clear (shape[i]);
5657 mpz_clear (shape2[i]);
5663 /* Check whether a WHERE assignment target or a WHERE mask expression
5664 has the same shape as the outmost WHERE mask expression. */
5667 resolve_where (gfc_code *code, gfc_expr *mask)
5673 cblock = code->block;
5675 /* Store the first WHERE mask-expr of the WHERE statement or construct.
5676 In case of nested WHERE, only the outmost one is stored. */
5677 if (mask == NULL) /* outmost WHERE */
5679 else /* inner WHERE */
5686 /* Check if the mask-expr has a consistent shape with the
5687 outmost WHERE mask-expr. */
5688 if (resolve_where_shape (cblock->expr, e) == FAILURE)
5689 gfc_error ("WHERE mask at %L has inconsistent shape",
5690 &cblock->expr->where);
5693 /* the assignment statement of a WHERE statement, or the first
5694 statement in where-body-construct of a WHERE construct */
5695 cnext = cblock->next;
5700 /* WHERE assignment statement */
5703 /* Check shape consistent for WHERE assignment target. */
5704 if (e && resolve_where_shape (cnext->expr, e) == FAILURE)
5705 gfc_error ("WHERE assignment target at %L has "
5706 "inconsistent shape", &cnext->expr->where);
5710 case EXEC_ASSIGN_CALL:
5711 resolve_call (cnext);
5712 if (!cnext->resolved_sym->attr.elemental)
5713 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
5714 &cnext->ext.actual->expr->where);
5717 /* WHERE or WHERE construct is part of a where-body-construct */
5719 resolve_where (cnext, e);
5723 gfc_error ("Unsupported statement inside WHERE at %L",
5726 /* the next statement within the same where-body-construct */
5727 cnext = cnext->next;
5729 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
5730 cblock = cblock->block;
5735 /* Resolve assignment in FORALL construct.
5736 NVAR is the number of FORALL index variables, and VAR_EXPR records the
5737 FORALL index variables. */
5740 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
5744 for (n = 0; n < nvar; n++)
5746 gfc_symbol *forall_index;
5748 forall_index = var_expr[n]->symtree->n.sym;
5750 /* Check whether the assignment target is one of the FORALL index
5752 if ((code->expr->expr_type == EXPR_VARIABLE)
5753 && (code->expr->symtree->n.sym == forall_index))
5754 gfc_error ("Assignment to a FORALL index variable at %L",
5755 &code->expr->where);
5758 /* If one of the FORALL index variables doesn't appear in the
5759 assignment target, then there will be a many-to-one
5761 if (find_forall_index (code->expr, forall_index, 0) == FAILURE)
5762 gfc_error ("The FORALL with index '%s' cause more than one "
5763 "assignment to this object at %L",
5764 var_expr[n]->symtree->name, &code->expr->where);
5770 /* Resolve WHERE statement in FORALL construct. */
5773 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
5774 gfc_expr **var_expr)
5779 cblock = code->block;
5782 /* the assignment statement of a WHERE statement, or the first
5783 statement in where-body-construct of a WHERE construct */
5784 cnext = cblock->next;
5789 /* WHERE assignment statement */
5791 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
5794 /* WHERE operator assignment statement */
5795 case EXEC_ASSIGN_CALL:
5796 resolve_call (cnext);
5797 if (!cnext->resolved_sym->attr.elemental)
5798 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
5799 &cnext->ext.actual->expr->where);
5802 /* WHERE or WHERE construct is part of a where-body-construct */
5804 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
5808 gfc_error ("Unsupported statement inside WHERE at %L",
5811 /* the next statement within the same where-body-construct */
5812 cnext = cnext->next;
5814 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
5815 cblock = cblock->block;
5820 /* Traverse the FORALL body to check whether the following errors exist:
5821 1. For assignment, check if a many-to-one assignment happens.
5822 2. For WHERE statement, check the WHERE body to see if there is any
5823 many-to-one assignment. */
5826 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
5830 c = code->block->next;
5836 case EXEC_POINTER_ASSIGN:
5837 gfc_resolve_assign_in_forall (c, nvar, var_expr);
5840 case EXEC_ASSIGN_CALL:
5844 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
5845 there is no need to handle it here. */
5849 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
5854 /* The next statement in the FORALL body. */
5860 /* Given a FORALL construct, first resolve the FORALL iterator, then call
5861 gfc_resolve_forall_body to resolve the FORALL body. */
5864 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
5866 static gfc_expr **var_expr;
5867 static int total_var = 0;
5868 static int nvar = 0;
5869 gfc_forall_iterator *fa;
5873 /* Start to resolve a FORALL construct */
5874 if (forall_save == 0)
5876 /* Count the total number of FORALL index in the nested FORALL
5877 construct in order to allocate the VAR_EXPR with proper size. */
5879 while ((next != NULL) && (next->op == EXEC_FORALL))
5881 for (fa = next->ext.forall_iterator; fa; fa = fa->next)
5883 next = next->block->next;
5886 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
5887 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
5890 /* The information about FORALL iterator, including FORALL index start, end
5891 and stride. The FORALL index can not appear in start, end or stride. */
5892 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
5894 /* Check if any outer FORALL index name is the same as the current
5896 for (i = 0; i < nvar; i++)
5898 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
5900 gfc_error ("An outer FORALL construct already has an index "
5901 "with this name %L", &fa->var->where);
5905 /* Record the current FORALL index. */
5906 var_expr[nvar] = gfc_copy_expr (fa->var);
5911 /* Resolve the FORALL body. */
5912 gfc_resolve_forall_body (code, nvar, var_expr);
5914 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
5915 gfc_resolve_blocks (code->block, ns);
5917 /* Free VAR_EXPR after the whole FORALL construct resolved. */
5918 for (i = 0; i < total_var; i++)
5919 gfc_free_expr (var_expr[i]);
5921 /* Reset the counters. */
5927 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL ,GOTO and
5930 static void resolve_code (gfc_code *, gfc_namespace *);
5933 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
5937 for (; b; b = b->block)
5939 t = gfc_resolve_expr (b->expr);
5940 if (gfc_resolve_expr (b->expr2) == FAILURE)
5946 if (t == SUCCESS && b->expr != NULL
5947 && (b->expr->ts.type != BT_LOGICAL || b->expr->rank != 0))
5948 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
5955 && (b->expr->ts.type != BT_LOGICAL || b->expr->rank == 0))
5956 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
5961 resolve_branch (b->label, b);
5974 case EXEC_OMP_ATOMIC:
5975 case EXEC_OMP_CRITICAL:
5977 case EXEC_OMP_MASTER:
5978 case EXEC_OMP_ORDERED:
5979 case EXEC_OMP_PARALLEL:
5980 case EXEC_OMP_PARALLEL_DO:
5981 case EXEC_OMP_PARALLEL_SECTIONS:
5982 case EXEC_OMP_PARALLEL_WORKSHARE:
5983 case EXEC_OMP_SECTIONS:
5984 case EXEC_OMP_SINGLE:
5985 case EXEC_OMP_WORKSHARE:
5989 gfc_internal_error ("resolve_block(): Bad block type");
5992 resolve_code (b->next, ns);
5997 /* Does everything to resolve an ordinary assignment. Returns true
5998 if this is an interface asignment. */
6000 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
6010 if (gfc_extend_assign (code, ns) == SUCCESS)
6012 lhs = code->ext.actual->expr;
6013 rhs = code->ext.actual->next->expr;
6014 if (gfc_pure (NULL) && !gfc_pure (code->symtree->n.sym))
6016 gfc_error ("Subroutine '%s' called instead of assignment at "
6017 "%L must be PURE", code->symtree->n.sym->name,
6022 /* Make a temporary rhs when there is a default initializer
6023 and rhs is the same symbol as the lhs. */
6024 if (rhs->expr_type == EXPR_VARIABLE
6025 && rhs->symtree->n.sym->ts.type == BT_DERIVED
6026 && has_default_initializer (rhs->symtree->n.sym->ts.derived)
6027 && (lhs->symtree->n.sym == rhs->symtree->n.sym))
6028 code->ext.actual->next->expr = gfc_get_parentheses (rhs);
6037 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
6038 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
6039 &code->loc) == FAILURE)
6042 /* Handle the case of a BOZ literal on the RHS. */
6043 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
6046 if (gfc_option.warn_surprising)
6047 gfc_warning ("BOZ literal at %L is bitwise transferred "
6048 "non-integer symbol '%s'", &code->loc,
6049 lhs->symtree->n.sym->name);
6051 if (!gfc_convert_boz (rhs, &lhs->ts))
6053 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
6055 if (rc == ARITH_UNDERFLOW)
6056 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
6057 ". This check can be disabled with the option "
6058 "-fno-range-check", &rhs->where);
6059 else if (rc == ARITH_OVERFLOW)
6060 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
6061 ". This check can be disabled with the option "
6062 "-fno-range-check", &rhs->where);
6063 else if (rc == ARITH_NAN)
6064 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
6065 ". This check can be disabled with the option "
6066 "-fno-range-check", &rhs->where);
6072 if (lhs->ts.type == BT_CHARACTER
6073 && gfc_option.warn_character_truncation)
6075 if (lhs->ts.cl != NULL
6076 && lhs->ts.cl->length != NULL
6077 && lhs->ts.cl->length->expr_type == EXPR_CONSTANT)
6078 llen = mpz_get_si (lhs->ts.cl->length->value.integer);
6080 if (rhs->expr_type == EXPR_CONSTANT)
6081 rlen = rhs->value.character.length;
6083 else if (rhs->ts.cl != NULL
6084 && rhs->ts.cl->length != NULL
6085 && rhs->ts.cl->length->expr_type == EXPR_CONSTANT)
6086 rlen = mpz_get_si (rhs->ts.cl->length->value.integer);
6088 if (rlen && llen && rlen > llen)
6089 gfc_warning_now ("CHARACTER expression will be truncated "
6090 "in assignment (%d/%d) at %L",
6091 llen, rlen, &code->loc);
6094 /* Ensure that a vector index expression for the lvalue is evaluated
6095 to a temporary if the lvalue symbol is referenced in it. */
6098 for (ref = lhs->ref; ref; ref= ref->next)
6099 if (ref->type == REF_ARRAY)
6101 for (n = 0; n < ref->u.ar.dimen; n++)
6102 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
6103 && find_sym_in_expr (lhs->symtree->n.sym,
6104 ref->u.ar.start[n]))
6106 = gfc_get_parentheses (ref->u.ar.start[n]);
6110 if (gfc_pure (NULL))
6112 if (gfc_impure_variable (lhs->symtree->n.sym))
6114 gfc_error ("Cannot assign to variable '%s' in PURE "
6116 lhs->symtree->n.sym->name,
6121 if (lhs->ts.type == BT_DERIVED
6122 && lhs->expr_type == EXPR_VARIABLE
6123 && lhs->ts.derived->attr.pointer_comp
6124 && gfc_impure_variable (rhs->symtree->n.sym))
6126 gfc_error ("The impure variable at %L is assigned to "
6127 "a derived type variable with a POINTER "
6128 "component in a PURE procedure (12.6)",
6134 gfc_check_assign (lhs, rhs, 1);
6138 /* Given a block of code, recursively resolve everything pointed to by this
6142 resolve_code (gfc_code *code, gfc_namespace *ns)
6144 int omp_workshare_save;
6149 frame.prev = cs_base;
6153 reachable_labels (code);
6155 for (; code; code = code->next)
6157 frame.current = code;
6158 forall_save = forall_flag;
6160 if (code->op == EXEC_FORALL)
6163 gfc_resolve_forall (code, ns, forall_save);
6166 else if (code->block)
6168 omp_workshare_save = -1;
6171 case EXEC_OMP_PARALLEL_WORKSHARE:
6172 omp_workshare_save = omp_workshare_flag;
6173 omp_workshare_flag = 1;
6174 gfc_resolve_omp_parallel_blocks (code, ns);
6176 case EXEC_OMP_PARALLEL:
6177 case EXEC_OMP_PARALLEL_DO:
6178 case EXEC_OMP_PARALLEL_SECTIONS:
6179 omp_workshare_save = omp_workshare_flag;
6180 omp_workshare_flag = 0;
6181 gfc_resolve_omp_parallel_blocks (code, ns);
6184 gfc_resolve_omp_do_blocks (code, ns);
6186 case EXEC_OMP_WORKSHARE:
6187 omp_workshare_save = omp_workshare_flag;
6188 omp_workshare_flag = 1;
6191 gfc_resolve_blocks (code->block, ns);
6195 if (omp_workshare_save != -1)
6196 omp_workshare_flag = omp_workshare_save;
6199 t = gfc_resolve_expr (code->expr);
6200 forall_flag = forall_save;
6202 if (gfc_resolve_expr (code->expr2) == FAILURE)
6217 /* Keep track of which entry we are up to. */
6218 current_entry_id = code->ext.entry->id;
6222 resolve_where (code, NULL);
6226 if (code->expr != NULL)
6228 if (code->expr->ts.type != BT_INTEGER)
6229 gfc_error ("ASSIGNED GOTO statement at %L requires an "
6230 "INTEGER variable", &code->expr->where);
6231 else if (code->expr->symtree->n.sym->attr.assign != 1)
6232 gfc_error ("Variable '%s' has not been assigned a target "
6233 "label at %L", code->expr->symtree->n.sym->name,
6234 &code->expr->where);
6237 resolve_branch (code->label, code);
6241 if (code->expr != NULL
6242 && (code->expr->ts.type != BT_INTEGER || code->expr->rank))
6243 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
6244 "INTEGER return specifier", &code->expr->where);
6247 case EXEC_INIT_ASSIGN:
6254 if (resolve_ordinary_assign (code, ns))
6259 case EXEC_LABEL_ASSIGN:
6260 if (code->label->defined == ST_LABEL_UNKNOWN)
6261 gfc_error ("Label %d referenced at %L is never defined",
6262 code->label->value, &code->label->where);
6264 && (code->expr->expr_type != EXPR_VARIABLE
6265 || code->expr->symtree->n.sym->ts.type != BT_INTEGER
6266 || code->expr->symtree->n.sym->ts.kind
6267 != gfc_default_integer_kind
6268 || code->expr->symtree->n.sym->as != NULL))
6269 gfc_error ("ASSIGN statement at %L requires a scalar "
6270 "default INTEGER variable", &code->expr->where);
6273 case EXEC_POINTER_ASSIGN:
6277 gfc_check_pointer_assign (code->expr, code->expr2);
6280 case EXEC_ARITHMETIC_IF:
6282 && code->expr->ts.type != BT_INTEGER
6283 && code->expr->ts.type != BT_REAL)
6284 gfc_error ("Arithmetic IF statement at %L requires a numeric "
6285 "expression", &code->expr->where);
6287 resolve_branch (code->label, code);
6288 resolve_branch (code->label2, code);
6289 resolve_branch (code->label3, code);
6293 if (t == SUCCESS && code->expr != NULL
6294 && (code->expr->ts.type != BT_LOGICAL
6295 || code->expr->rank != 0))
6296 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
6297 &code->expr->where);
6302 resolve_call (code);
6306 /* Select is complicated. Also, a SELECT construct could be
6307 a transformed computed GOTO. */
6308 resolve_select (code);
6312 if (code->ext.iterator != NULL)
6314 gfc_iterator *iter = code->ext.iterator;
6315 if (gfc_resolve_iterator (iter, true) != FAILURE)
6316 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
6321 if (code->expr == NULL)
6322 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
6324 && (code->expr->rank != 0
6325 || code->expr->ts.type != BT_LOGICAL))
6326 gfc_error ("Exit condition of DO WHILE loop at %L must be "
6327 "a scalar LOGICAL expression", &code->expr->where);
6332 resolve_allocate_deallocate (code, "ALLOCATE");
6336 case EXEC_DEALLOCATE:
6338 resolve_allocate_deallocate (code, "DEALLOCATE");
6343 if (gfc_resolve_open (code->ext.open) == FAILURE)
6346 resolve_branch (code->ext.open->err, code);
6350 if (gfc_resolve_close (code->ext.close) == FAILURE)
6353 resolve_branch (code->ext.close->err, code);
6356 case EXEC_BACKSPACE:
6360 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
6363 resolve_branch (code->ext.filepos->err, code);
6367 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
6370 resolve_branch (code->ext.inquire->err, code);
6374 gcc_assert (code->ext.inquire != NULL);
6375 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
6378 resolve_branch (code->ext.inquire->err, code);
6382 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
6385 resolve_branch (code->ext.wait->err, code);
6386 resolve_branch (code->ext.wait->end, code);
6387 resolve_branch (code->ext.wait->eor, code);
6392 if (gfc_resolve_dt (code->ext.dt) == FAILURE)
6395 resolve_branch (code->ext.dt->err, code);
6396 resolve_branch (code->ext.dt->end, code);
6397 resolve_branch (code->ext.dt->eor, code);
6401 resolve_transfer (code);
6405 resolve_forall_iterators (code->ext.forall_iterator);
6407 if (code->expr != NULL && code->expr->ts.type != BT_LOGICAL)
6408 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
6409 "expression", &code->expr->where);
6412 case EXEC_OMP_ATOMIC:
6413 case EXEC_OMP_BARRIER:
6414 case EXEC_OMP_CRITICAL:
6415 case EXEC_OMP_FLUSH:
6417 case EXEC_OMP_MASTER:
6418 case EXEC_OMP_ORDERED:
6419 case EXEC_OMP_SECTIONS:
6420 case EXEC_OMP_SINGLE:
6421 case EXEC_OMP_WORKSHARE:
6422 gfc_resolve_omp_directive (code, ns);
6425 case EXEC_OMP_PARALLEL:
6426 case EXEC_OMP_PARALLEL_DO:
6427 case EXEC_OMP_PARALLEL_SECTIONS:
6428 case EXEC_OMP_PARALLEL_WORKSHARE:
6429 omp_workshare_save = omp_workshare_flag;
6430 omp_workshare_flag = 0;
6431 gfc_resolve_omp_directive (code, ns);
6432 omp_workshare_flag = omp_workshare_save;
6436 gfc_internal_error ("resolve_code(): Bad statement code");
6440 cs_base = frame.prev;
6444 /* Resolve initial values and make sure they are compatible with
6448 resolve_values (gfc_symbol *sym)
6450 if (sym->value == NULL)
6453 if (gfc_resolve_expr (sym->value) == FAILURE)
6456 gfc_check_assign_symbol (sym, sym->value);
6460 /* Verify the binding labels for common blocks that are BIND(C). The label
6461 for a BIND(C) common block must be identical in all scoping units in which
6462 the common block is declared. Further, the binding label can not collide
6463 with any other global entity in the program. */
6466 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
6468 if (comm_block_tree->n.common->is_bind_c == 1)
6470 gfc_gsymbol *binding_label_gsym;
6471 gfc_gsymbol *comm_name_gsym;
6473 /* See if a global symbol exists by the common block's name. It may
6474 be NULL if the common block is use-associated. */
6475 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
6476 comm_block_tree->n.common->name);
6477 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
6478 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
6479 "with the global entity '%s' at %L",
6480 comm_block_tree->n.common->binding_label,
6481 comm_block_tree->n.common->name,
6482 &(comm_block_tree->n.common->where),
6483 comm_name_gsym->name, &(comm_name_gsym->where));
6484 else if (comm_name_gsym != NULL
6485 && strcmp (comm_name_gsym->name,
6486 comm_block_tree->n.common->name) == 0)
6488 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
6490 if (comm_name_gsym->binding_label == NULL)
6491 /* No binding label for common block stored yet; save this one. */
6492 comm_name_gsym->binding_label =
6493 comm_block_tree->n.common->binding_label;
6495 if (strcmp (comm_name_gsym->binding_label,
6496 comm_block_tree->n.common->binding_label) != 0)
6498 /* Common block names match but binding labels do not. */
6499 gfc_error ("Binding label '%s' for common block '%s' at %L "
6500 "does not match the binding label '%s' for common "
6502 comm_block_tree->n.common->binding_label,
6503 comm_block_tree->n.common->name,
6504 &(comm_block_tree->n.common->where),
6505 comm_name_gsym->binding_label,
6506 comm_name_gsym->name,
6507 &(comm_name_gsym->where));
6512 /* There is no binding label (NAME="") so we have nothing further to
6513 check and nothing to add as a global symbol for the label. */
6514 if (comm_block_tree->n.common->binding_label[0] == '\0' )
6517 binding_label_gsym =
6518 gfc_find_gsymbol (gfc_gsym_root,
6519 comm_block_tree->n.common->binding_label);
6520 if (binding_label_gsym == NULL)
6522 /* Need to make a global symbol for the binding label to prevent
6523 it from colliding with another. */
6524 binding_label_gsym =
6525 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
6526 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
6527 binding_label_gsym->type = GSYM_COMMON;
6531 /* If comm_name_gsym is NULL, the name common block is use
6532 associated and the name could be colliding. */
6533 if (binding_label_gsym->type != GSYM_COMMON)
6534 gfc_error ("Binding label '%s' for common block '%s' at %L "
6535 "collides with the global entity '%s' at %L",
6536 comm_block_tree->n.common->binding_label,
6537 comm_block_tree->n.common->name,
6538 &(comm_block_tree->n.common->where),
6539 binding_label_gsym->name,
6540 &(binding_label_gsym->where));
6541 else if (comm_name_gsym != NULL
6542 && (strcmp (binding_label_gsym->name,
6543 comm_name_gsym->binding_label) != 0)
6544 && (strcmp (binding_label_gsym->sym_name,
6545 comm_name_gsym->name) != 0))
6546 gfc_error ("Binding label '%s' for common block '%s' at %L "
6547 "collides with global entity '%s' at %L",
6548 binding_label_gsym->name, binding_label_gsym->sym_name,
6549 &(comm_block_tree->n.common->where),
6550 comm_name_gsym->name, &(comm_name_gsym->where));
6558 /* Verify any BIND(C) derived types in the namespace so we can report errors
6559 for them once, rather than for each variable declared of that type. */
6562 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
6564 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
6565 && derived_sym->attr.is_bind_c == 1)
6566 verify_bind_c_derived_type (derived_sym);
6572 /* Verify that any binding labels used in a given namespace do not collide
6573 with the names or binding labels of any global symbols. */
6576 gfc_verify_binding_labels (gfc_symbol *sym)
6580 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
6581 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
6583 gfc_gsymbol *bind_c_sym;
6585 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
6586 if (bind_c_sym != NULL
6587 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
6589 if (sym->attr.if_source == IFSRC_DECL
6590 && (bind_c_sym->type != GSYM_SUBROUTINE
6591 && bind_c_sym->type != GSYM_FUNCTION)
6592 && ((sym->attr.contained == 1
6593 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
6594 || (sym->attr.use_assoc == 1
6595 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
6597 /* Make sure global procedures don't collide with anything. */
6598 gfc_error ("Binding label '%s' at %L collides with the global "
6599 "entity '%s' at %L", sym->binding_label,
6600 &(sym->declared_at), bind_c_sym->name,
6601 &(bind_c_sym->where));
6604 else if (sym->attr.contained == 0
6605 && (sym->attr.if_source == IFSRC_IFBODY
6606 && sym->attr.flavor == FL_PROCEDURE)
6607 && (bind_c_sym->sym_name != NULL
6608 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
6610 /* Make sure procedures in interface bodies don't collide. */
6611 gfc_error ("Binding label '%s' in interface body at %L collides "
6612 "with the global entity '%s' at %L",
6614 &(sym->declared_at), bind_c_sym->name,
6615 &(bind_c_sym->where));
6618 else if (sym->attr.contained == 0
6619 && sym->attr.if_source == IFSRC_UNKNOWN)
6620 if ((sym->attr.use_assoc && bind_c_sym->mod_name
6621 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
6622 || sym->attr.use_assoc == 0)
6624 gfc_error ("Binding label '%s' at %L collides with global "
6625 "entity '%s' at %L", sym->binding_label,
6626 &(sym->declared_at), bind_c_sym->name,
6627 &(bind_c_sym->where));
6632 /* Clear the binding label to prevent checking multiple times. */
6633 sym->binding_label[0] = '\0';
6635 else if (bind_c_sym == NULL)
6637 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
6638 bind_c_sym->where = sym->declared_at;
6639 bind_c_sym->sym_name = sym->name;
6641 if (sym->attr.use_assoc == 1)
6642 bind_c_sym->mod_name = sym->module;
6644 if (sym->ns->proc_name != NULL)
6645 bind_c_sym->mod_name = sym->ns->proc_name->name;
6647 if (sym->attr.contained == 0)
6649 if (sym->attr.subroutine)
6650 bind_c_sym->type = GSYM_SUBROUTINE;
6651 else if (sym->attr.function)
6652 bind_c_sym->type = GSYM_FUNCTION;
6660 /* Resolve an index expression. */
6663 resolve_index_expr (gfc_expr *e)
6665 if (gfc_resolve_expr (e) == FAILURE)
6668 if (gfc_simplify_expr (e, 0) == FAILURE)
6671 if (gfc_specification_expr (e) == FAILURE)
6677 /* Resolve a charlen structure. */
6680 resolve_charlen (gfc_charlen *cl)
6689 specification_expr = 1;
6691 if (resolve_index_expr (cl->length) == FAILURE)
6693 specification_expr = 0;
6697 /* "If the character length parameter value evaluates to a negative
6698 value, the length of character entities declared is zero." */
6699 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
6701 gfc_warning_now ("CHARACTER variable has zero length at %L",
6702 &cl->length->where);
6703 gfc_replace_expr (cl->length, gfc_int_expr (0));
6710 /* Test for non-constant shape arrays. */
6713 is_non_constant_shape_array (gfc_symbol *sym)
6719 not_constant = false;
6720 if (sym->as != NULL)
6722 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
6723 has not been simplified; parameter array references. Do the
6724 simplification now. */
6725 for (i = 0; i < sym->as->rank; i++)
6727 e = sym->as->lower[i];
6728 if (e && (resolve_index_expr (e) == FAILURE
6729 || !gfc_is_constant_expr (e)))
6730 not_constant = true;
6732 e = sym->as->upper[i];
6733 if (e && (resolve_index_expr (e) == FAILURE
6734 || !gfc_is_constant_expr (e)))
6735 not_constant = true;
6738 return not_constant;
6741 /* Given a symbol and an initialization expression, add code to initialize
6742 the symbol to the function entry. */
6744 build_init_assign (gfc_symbol *sym, gfc_expr *init)
6748 gfc_namespace *ns = sym->ns;
6750 /* Search for the function namespace if this is a contained
6751 function without an explicit result. */
6752 if (sym->attr.function && sym == sym->result
6753 && sym->name != sym->ns->proc_name->name)
6756 for (;ns; ns = ns->sibling)
6757 if (strcmp (ns->proc_name->name, sym->name) == 0)
6763 gfc_free_expr (init);
6767 /* Build an l-value expression for the result. */
6768 lval = gfc_lval_expr_from_sym (sym);
6770 /* Add the code at scope entry. */
6771 init_st = gfc_get_code ();
6772 init_st->next = ns->code;
6775 /* Assign the default initializer to the l-value. */
6776 init_st->loc = sym->declared_at;
6777 init_st->op = EXEC_INIT_ASSIGN;
6778 init_st->expr = lval;
6779 init_st->expr2 = init;
6782 /* Assign the default initializer to a derived type variable or result. */
6785 apply_default_init (gfc_symbol *sym)
6787 gfc_expr *init = NULL;
6789 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
6792 if (sym->ts.type == BT_DERIVED && sym->ts.derived)
6793 init = gfc_default_initializer (&sym->ts);
6798 build_init_assign (sym, init);
6801 /* Build an initializer for a local integer, real, complex, logical, or
6802 character variable, based on the command line flags finit-local-zero,
6803 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
6804 null if the symbol should not have a default initialization. */
6806 build_default_init_expr (gfc_symbol *sym)
6809 gfc_expr *init_expr;
6812 /* These symbols should never have a default initialization. */
6813 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
6814 || sym->attr.external
6816 || sym->attr.pointer
6817 || sym->attr.in_equivalence
6818 || sym->attr.in_common
6821 || sym->attr.cray_pointee
6822 || sym->attr.cray_pointer)
6825 /* Now we'll try to build an initializer expression. */
6826 init_expr = gfc_get_expr ();
6827 init_expr->expr_type = EXPR_CONSTANT;
6828 init_expr->ts.type = sym->ts.type;
6829 init_expr->ts.kind = sym->ts.kind;
6830 init_expr->where = sym->declared_at;
6832 /* We will only initialize integers, reals, complex, logicals, and
6833 characters, and only if the corresponding command-line flags
6834 were set. Otherwise, we free init_expr and return null. */
6835 switch (sym->ts.type)
6838 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
6839 mpz_init_set_si (init_expr->value.integer,
6840 gfc_option.flag_init_integer_value);
6843 gfc_free_expr (init_expr);
6849 mpfr_init (init_expr->value.real);
6850 switch (gfc_option.flag_init_real)
6852 case GFC_INIT_REAL_NAN:
6853 mpfr_set_nan (init_expr->value.real);
6856 case GFC_INIT_REAL_INF:
6857 mpfr_set_inf (init_expr->value.real, 1);
6860 case GFC_INIT_REAL_NEG_INF:
6861 mpfr_set_inf (init_expr->value.real, -1);
6864 case GFC_INIT_REAL_ZERO:
6865 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
6869 gfc_free_expr (init_expr);
6876 mpfr_init (init_expr->value.complex.r);
6877 mpfr_init (init_expr->value.complex.i);
6878 switch (gfc_option.flag_init_real)
6880 case GFC_INIT_REAL_NAN:
6881 mpfr_set_nan (init_expr->value.complex.r);
6882 mpfr_set_nan (init_expr->value.complex.i);
6885 case GFC_INIT_REAL_INF:
6886 mpfr_set_inf (init_expr->value.complex.r, 1);
6887 mpfr_set_inf (init_expr->value.complex.i, 1);
6890 case GFC_INIT_REAL_NEG_INF:
6891 mpfr_set_inf (init_expr->value.complex.r, -1);
6892 mpfr_set_inf (init_expr->value.complex.i, -1);
6895 case GFC_INIT_REAL_ZERO:
6896 mpfr_set_ui (init_expr->value.complex.r, 0.0, GFC_RND_MODE);
6897 mpfr_set_ui (init_expr->value.complex.i, 0.0, GFC_RND_MODE);
6901 gfc_free_expr (init_expr);
6908 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
6909 init_expr->value.logical = 0;
6910 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
6911 init_expr->value.logical = 1;
6914 gfc_free_expr (init_expr);
6920 /* For characters, the length must be constant in order to
6921 create a default initializer. */
6922 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
6923 && sym->ts.cl->length
6924 && sym->ts.cl->length->expr_type == EXPR_CONSTANT)
6926 char_len = mpz_get_si (sym->ts.cl->length->value.integer);
6927 init_expr->value.character.length = char_len;
6928 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
6929 for (i = 0; i < char_len; i++)
6930 init_expr->value.character.string[i]
6931 = (unsigned char) gfc_option.flag_init_character_value;
6935 gfc_free_expr (init_expr);
6941 gfc_free_expr (init_expr);
6947 /* Add an initialization expression to a local variable. */
6949 apply_default_init_local (gfc_symbol *sym)
6951 gfc_expr *init = NULL;
6953 /* The symbol should be a variable or a function return value. */
6954 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
6955 || (sym->attr.function && sym->result != sym))
6958 /* Try to build the initializer expression. If we can't initialize
6959 this symbol, then init will be NULL. */
6960 init = build_default_init_expr (sym);
6964 /* For saved variables, we don't want to add an initializer at
6965 function entry, so we just add a static initializer. */
6966 if (sym->attr.save || sym->ns->save_all)
6968 /* Don't clobber an existing initializer! */
6969 gcc_assert (sym->value == NULL);
6974 build_init_assign (sym, init);
6977 /* Resolution of common features of flavors variable and procedure. */
6980 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
6982 /* Constraints on deferred shape variable. */
6983 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
6985 if (sym->attr.allocatable)
6987 if (sym->attr.dimension)
6988 gfc_error ("Allocatable array '%s' at %L must have "
6989 "a deferred shape", sym->name, &sym->declared_at);
6991 gfc_error ("Scalar object '%s' at %L may not be ALLOCATABLE",
6992 sym->name, &sym->declared_at);
6996 if (sym->attr.pointer && sym->attr.dimension)
6998 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
6999 sym->name, &sym->declared_at);
7006 if (!mp_flag && !sym->attr.allocatable
7007 && !sym->attr.pointer && !sym->attr.dummy)
7009 gfc_error ("Array '%s' at %L cannot have a deferred shape",
7010 sym->name, &sym->declared_at);
7018 /* Additional checks for symbols with flavor variable and derived
7019 type. To be called from resolve_fl_variable. */
7022 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
7024 gcc_assert (sym->ts.type == BT_DERIVED);
7026 /* Check to see if a derived type is blocked from being host
7027 associated by the presence of another class I symbol in the same
7028 namespace. 14.6.1.3 of the standard and the discussion on
7029 comp.lang.fortran. */
7030 if (sym->ns != sym->ts.derived->ns
7031 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
7034 gfc_find_symbol (sym->ts.derived->name, sym->ns, 0, &s);
7035 if (s && (s->attr.flavor != FL_DERIVED
7036 || !gfc_compare_derived_types (s, sym->ts.derived)))
7038 gfc_error ("The type '%s' cannot be host associated at %L "
7039 "because it is blocked by an incompatible object "
7040 "of the same name declared at %L",
7041 sym->ts.derived->name, &sym->declared_at,
7047 /* 4th constraint in section 11.3: "If an object of a type for which
7048 component-initialization is specified (R429) appears in the
7049 specification-part of a module and does not have the ALLOCATABLE
7050 or POINTER attribute, the object shall have the SAVE attribute."
7052 The check for initializers is performed with
7053 has_default_initializer because gfc_default_initializer generates
7054 a hidden default for allocatable components. */
7055 if (!(sym->value || no_init_flag) && sym->ns->proc_name
7056 && sym->ns->proc_name->attr.flavor == FL_MODULE
7057 && !sym->ns->save_all && !sym->attr.save
7058 && !sym->attr.pointer && !sym->attr.allocatable
7059 && has_default_initializer (sym->ts.derived))
7061 gfc_error("Object '%s' at %L must have the SAVE attribute for "
7062 "default initialization of a component",
7063 sym->name, &sym->declared_at);
7067 /* Assign default initializer. */
7068 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
7069 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
7071 sym->value = gfc_default_initializer (&sym->ts);
7078 /* Resolve symbols with flavor variable. */
7081 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
7083 int no_init_flag, automatic_flag;
7085 const char *auto_save_msg;
7087 auto_save_msg = "Automatic object '%s' at %L cannot have the "
7090 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
7093 /* Set this flag to check that variables are parameters of all entries.
7094 This check is effected by the call to gfc_resolve_expr through
7095 is_non_constant_shape_array. */
7096 specification_expr = 1;
7098 if (sym->ns->proc_name
7099 && (sym->ns->proc_name->attr.flavor == FL_MODULE
7100 || sym->ns->proc_name->attr.is_main_program)
7101 && !sym->attr.use_assoc
7102 && !sym->attr.allocatable
7103 && !sym->attr.pointer
7104 && is_non_constant_shape_array (sym))
7106 /* The shape of a main program or module array needs to be
7108 gfc_error ("The module or main program array '%s' at %L must "
7109 "have constant shape", sym->name, &sym->declared_at);
7110 specification_expr = 0;
7114 if (sym->ts.type == BT_CHARACTER)
7116 /* Make sure that character string variables with assumed length are
7118 e = sym->ts.cl->length;
7119 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
7121 gfc_error ("Entity with assumed character length at %L must be a "
7122 "dummy argument or a PARAMETER", &sym->declared_at);
7126 if (e && sym->attr.save && !gfc_is_constant_expr (e))
7128 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
7132 if (!gfc_is_constant_expr (e)
7133 && !(e->expr_type == EXPR_VARIABLE
7134 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
7135 && sym->ns->proc_name
7136 && (sym->ns->proc_name->attr.flavor == FL_MODULE
7137 || sym->ns->proc_name->attr.is_main_program)
7138 && !sym->attr.use_assoc)
7140 gfc_error ("'%s' at %L must have constant character length "
7141 "in this context", sym->name, &sym->declared_at);
7146 if (sym->value == NULL && sym->attr.referenced)
7147 apply_default_init_local (sym); /* Try to apply a default initialization. */
7149 /* Determine if the symbol may not have an initializer. */
7150 no_init_flag = automatic_flag = 0;
7151 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
7152 || sym->attr.intrinsic || sym->attr.result)
7154 else if (sym->attr.dimension && !sym->attr.pointer
7155 && is_non_constant_shape_array (sym))
7157 no_init_flag = automatic_flag = 1;
7159 /* Also, they must not have the SAVE attribute.
7160 SAVE_IMPLICIT is checked below. */
7161 if (sym->attr.save == SAVE_EXPLICIT)
7163 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
7168 /* Reject illegal initializers. */
7169 if (!sym->mark && sym->value)
7171 if (sym->attr.allocatable)
7172 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
7173 sym->name, &sym->declared_at);
7174 else if (sym->attr.external)
7175 gfc_error ("External '%s' at %L cannot have an initializer",
7176 sym->name, &sym->declared_at);
7177 else if (sym->attr.dummy
7178 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
7179 gfc_error ("Dummy '%s' at %L cannot have an initializer",
7180 sym->name, &sym->declared_at);
7181 else if (sym->attr.intrinsic)
7182 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
7183 sym->name, &sym->declared_at);
7184 else if (sym->attr.result)
7185 gfc_error ("Function result '%s' at %L cannot have an initializer",
7186 sym->name, &sym->declared_at);
7187 else if (automatic_flag)
7188 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
7189 sym->name, &sym->declared_at);
7196 if (sym->ts.type == BT_DERIVED)
7197 return resolve_fl_variable_derived (sym, no_init_flag);
7203 /* Resolve a procedure. */
7206 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
7208 gfc_formal_arglist *arg;
7210 if (sym->attr.ambiguous_interfaces && !sym->attr.referenced)
7211 gfc_warning ("Although not referenced, '%s' at %L has ambiguous "
7212 "interfaces", sym->name, &sym->declared_at);
7214 if (sym->attr.function
7215 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
7218 if (sym->ts.type == BT_CHARACTER)
7220 gfc_charlen *cl = sym->ts.cl;
7222 if (cl && cl->length && gfc_is_constant_expr (cl->length)
7223 && resolve_charlen (cl) == FAILURE)
7226 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
7228 if (sym->attr.proc == PROC_ST_FUNCTION)
7230 gfc_error ("Character-valued statement function '%s' at %L must "
7231 "have constant length", sym->name, &sym->declared_at);
7235 if (sym->attr.external && sym->formal == NULL
7236 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
7238 gfc_error ("Automatic character length function '%s' at %L must "
7239 "have an explicit interface", sym->name,
7246 /* Ensure that derived type for are not of a private type. Internal
7247 module procedures are excluded by 2.2.3.3 - ie. they are not
7248 externally accessible and can access all the objects accessible in
7250 if (!(sym->ns->parent
7251 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
7252 && gfc_check_access(sym->attr.access, sym->ns->default_access))
7254 gfc_interface *iface;
7256 for (arg = sym->formal; arg; arg = arg->next)
7259 && arg->sym->ts.type == BT_DERIVED
7260 && !arg->sym->ts.derived->attr.use_assoc
7261 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7262 arg->sym->ts.derived->ns->default_access)
7263 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
7264 "PRIVATE type and cannot be a dummy argument"
7265 " of '%s', which is PUBLIC at %L",
7266 arg->sym->name, sym->name, &sym->declared_at)
7269 /* Stop this message from recurring. */
7270 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7275 /* PUBLIC interfaces may expose PRIVATE procedures that take types
7276 PRIVATE to the containing module. */
7277 for (iface = sym->generic; iface; iface = iface->next)
7279 for (arg = iface->sym->formal; arg; arg = arg->next)
7282 && arg->sym->ts.type == BT_DERIVED
7283 && !arg->sym->ts.derived->attr.use_assoc
7284 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7285 arg->sym->ts.derived->ns->default_access)
7286 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
7287 "'%s' in PUBLIC interface '%s' at %L "
7288 "takes dummy arguments of '%s' which is "
7289 "PRIVATE", iface->sym->name, sym->name,
7290 &iface->sym->declared_at,
7291 gfc_typename (&arg->sym->ts)) == FAILURE)
7293 /* Stop this message from recurring. */
7294 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7300 /* PUBLIC interfaces may expose PRIVATE procedures that take types
7301 PRIVATE to the containing module. */
7302 for (iface = sym->generic; iface; iface = iface->next)
7304 for (arg = iface->sym->formal; arg; arg = arg->next)
7307 && arg->sym->ts.type == BT_DERIVED
7308 && !arg->sym->ts.derived->attr.use_assoc
7309 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7310 arg->sym->ts.derived->ns->default_access)
7311 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
7312 "'%s' in PUBLIC interface '%s' at %L "
7313 "takes dummy arguments of '%s' which is "
7314 "PRIVATE", iface->sym->name, sym->name,
7315 &iface->sym->declared_at,
7316 gfc_typename (&arg->sym->ts)) == FAILURE)
7318 /* Stop this message from recurring. */
7319 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7326 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION)
7328 gfc_error ("Function '%s' at %L cannot have an initializer",
7329 sym->name, &sym->declared_at);
7333 /* An external symbol may not have an initializer because it is taken to be
7335 if (sym->attr.external && sym->value)
7337 gfc_error ("External object '%s' at %L may not have an initializer",
7338 sym->name, &sym->declared_at);
7342 /* An elemental function is required to return a scalar 12.7.1 */
7343 if (sym->attr.elemental && sym->attr.function && sym->as)
7345 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
7346 "result", sym->name, &sym->declared_at);
7347 /* Reset so that the error only occurs once. */
7348 sym->attr.elemental = 0;
7352 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
7353 char-len-param shall not be array-valued, pointer-valued, recursive
7354 or pure. ....snip... A character value of * may only be used in the
7355 following ways: (i) Dummy arg of procedure - dummy associates with
7356 actual length; (ii) To declare a named constant; or (iii) External
7357 function - but length must be declared in calling scoping unit. */
7358 if (sym->attr.function
7359 && sym->ts.type == BT_CHARACTER
7360 && sym->ts.cl && sym->ts.cl->length == NULL)
7362 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
7363 || (sym->attr.recursive) || (sym->attr.pure))
7365 if (sym->as && sym->as->rank)
7366 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7367 "array-valued", sym->name, &sym->declared_at);
7369 if (sym->attr.pointer)
7370 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7371 "pointer-valued", sym->name, &sym->declared_at);
7374 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7375 "pure", sym->name, &sym->declared_at);
7377 if (sym->attr.recursive)
7378 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7379 "recursive", sym->name, &sym->declared_at);
7384 /* Appendix B.2 of the standard. Contained functions give an
7385 error anyway. Fixed-form is likely to be F77/legacy. */
7386 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
7387 gfc_notify_std (GFC_STD_F95_OBS, "CHARACTER(*) function "
7388 "'%s' at %L is obsolescent in fortran 95",
7389 sym->name, &sym->declared_at);
7392 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
7394 gfc_formal_arglist *curr_arg;
7395 int has_non_interop_arg = 0;
7397 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
7398 sym->common_block) == FAILURE)
7400 /* Clear these to prevent looking at them again if there was an
7402 sym->attr.is_bind_c = 0;
7403 sym->attr.is_c_interop = 0;
7404 sym->ts.is_c_interop = 0;
7408 /* So far, no errors have been found. */
7409 sym->attr.is_c_interop = 1;
7410 sym->ts.is_c_interop = 1;
7413 curr_arg = sym->formal;
7414 while (curr_arg != NULL)
7416 /* Skip implicitly typed dummy args here. */
7417 if (curr_arg->sym->attr.implicit_type == 0)
7418 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
7419 /* If something is found to fail, record the fact so we
7420 can mark the symbol for the procedure as not being
7421 BIND(C) to try and prevent multiple errors being
7423 has_non_interop_arg = 1;
7425 curr_arg = curr_arg->next;
7428 /* See if any of the arguments were not interoperable and if so, clear
7429 the procedure symbol to prevent duplicate error messages. */
7430 if (has_non_interop_arg != 0)
7432 sym->attr.is_c_interop = 0;
7433 sym->ts.is_c_interop = 0;
7434 sym->attr.is_bind_c = 0;
7442 /* Resolve a list of finalizer procedures. That is, after they have hopefully
7443 been defined and we now know their defined arguments, check that they fulfill
7444 the requirements of the standard for procedures used as finalizers. */
7447 gfc_resolve_finalizers (gfc_symbol* derived)
7449 gfc_finalizer* list;
7450 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
7451 try result = SUCCESS;
7452 bool seen_scalar = false;
7454 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
7457 /* Walk over the list of finalizer-procedures, check them, and if any one
7458 does not fit in with the standard's definition, print an error and remove
7459 it from the list. */
7460 prev_link = &derived->f2k_derived->finalizers;
7461 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
7467 /* Check this exists and is a SUBROUTINE. */
7468 if (!list->procedure->attr.subroutine)
7470 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
7471 list->procedure->name, &list->where);
7475 /* We should have exactly one argument. */
7476 if (!list->procedure->formal || list->procedure->formal->next)
7478 gfc_error ("FINAL procedure at %L must have exactly one argument",
7482 arg = list->procedure->formal->sym;
7484 /* This argument must be of our type. */
7485 if (arg->ts.type != BT_DERIVED || arg->ts.derived != derived)
7487 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
7488 &arg->declared_at, derived->name);
7492 /* It must neither be a pointer nor allocatable nor optional. */
7493 if (arg->attr.pointer)
7495 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
7499 if (arg->attr.allocatable)
7501 gfc_error ("Argument of FINAL procedure at %L must not be"
7502 " ALLOCATABLE", &arg->declared_at);
7505 if (arg->attr.optional)
7507 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
7512 /* It must not be INTENT(OUT). */
7513 if (arg->attr.intent == INTENT_OUT)
7515 gfc_error ("Argument of FINAL procedure at %L must not be"
7516 " INTENT(OUT)", &arg->declared_at);
7520 /* Warn if the procedure is non-scalar and not assumed shape. */
7521 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
7522 && arg->as->type != AS_ASSUMED_SHAPE)
7523 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
7524 " shape argument", &arg->declared_at);
7526 /* Check that it does not match in kind and rank with a FINAL procedure
7527 defined earlier. To really loop over the *earlier* declarations,
7528 we need to walk the tail of the list as new ones were pushed at the
7530 /* TODO: Handle kind parameters once they are implemented. */
7531 my_rank = (arg->as ? arg->as->rank : 0);
7532 for (i = list->next; i; i = i->next)
7534 /* Argument list might be empty; that is an error signalled earlier,
7535 but we nevertheless continued resolving. */
7536 if (i->procedure->formal)
7538 gfc_symbol* i_arg = i->procedure->formal->sym;
7539 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
7540 if (i_rank == my_rank)
7542 gfc_error ("FINAL procedure '%s' declared at %L has the same"
7543 " rank (%d) as '%s'",
7544 list->procedure->name, &list->where, my_rank,
7545 i->procedure->name);
7551 /* Is this the/a scalar finalizer procedure? */
7552 if (!arg->as || arg->as->rank == 0)
7555 prev_link = &list->next;
7558 /* Remove wrong nodes immediatelly from the list so we don't risk any
7559 troubles in the future when they might fail later expectations. */
7563 *prev_link = list->next;
7564 gfc_free_finalizer (i);
7567 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
7568 were nodes in the list, must have been for arrays. It is surely a good
7569 idea to have a scalar version there if there's something to finalize. */
7570 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
7571 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
7572 " defined at %L, suggest also scalar one",
7573 derived->name, &derived->declared_at);
7575 /* TODO: Remove this error when finalization is finished. */
7576 gfc_error ("Finalization at %L is not yet implemented", &derived->declared_at);
7582 /* Resolve the components of a derived type. */
7585 resolve_fl_derived (gfc_symbol *sym)
7588 gfc_dt_list * dt_list;
7591 for (c = sym->components; c != NULL; c = c->next)
7593 if (c->ts.type == BT_CHARACTER)
7595 if (c->ts.cl->length == NULL
7596 || (resolve_charlen (c->ts.cl) == FAILURE)
7597 || !gfc_is_constant_expr (c->ts.cl->length))
7599 gfc_error ("Character length of component '%s' needs to "
7600 "be a constant specification expression at %L",
7602 c->ts.cl->length ? &c->ts.cl->length->where : &c->loc);
7607 if (c->ts.type == BT_DERIVED
7608 && sym->component_access != ACCESS_PRIVATE
7609 && gfc_check_access (sym->attr.access, sym->ns->default_access)
7610 && !c->ts.derived->attr.use_assoc
7611 && !gfc_check_access (c->ts.derived->attr.access,
7612 c->ts.derived->ns->default_access))
7614 gfc_error ("The component '%s' is a PRIVATE type and cannot be "
7615 "a component of '%s', which is PUBLIC at %L",
7616 c->name, sym->name, &sym->declared_at);
7620 if (sym->attr.sequence)
7622 if (c->ts.type == BT_DERIVED && c->ts.derived->attr.sequence == 0)
7624 gfc_error ("Component %s of SEQUENCE type declared at %L does "
7625 "not have the SEQUENCE attribute",
7626 c->ts.derived->name, &sym->declared_at);
7631 if (c->ts.type == BT_DERIVED && c->pointer
7632 && c->ts.derived->components == NULL)
7634 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
7635 "that has not been declared", c->name, sym->name,
7640 if (c->pointer || c->allocatable || c->as == NULL)
7643 for (i = 0; i < c->as->rank; i++)
7645 if (c->as->lower[i] == NULL
7646 || !gfc_is_constant_expr (c->as->lower[i])
7647 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
7648 || c->as->upper[i] == NULL
7649 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
7650 || !gfc_is_constant_expr (c->as->upper[i]))
7652 gfc_error ("Component '%s' of '%s' at %L must have "
7653 "constant array bounds",
7654 c->name, sym->name, &c->loc);
7660 /* Resolve the finalizer procedures. */
7661 if (gfc_resolve_finalizers (sym) == FAILURE)
7664 /* Add derived type to the derived type list. */
7665 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
7666 if (sym == dt_list->derived)
7669 if (dt_list == NULL)
7671 dt_list = gfc_get_dt_list ();
7672 dt_list->next = gfc_derived_types;
7673 dt_list->derived = sym;
7674 gfc_derived_types = dt_list;
7682 resolve_fl_namelist (gfc_symbol *sym)
7687 /* Reject PRIVATE objects in a PUBLIC namelist. */
7688 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
7690 for (nl = sym->namelist; nl; nl = nl->next)
7692 if (!nl->sym->attr.use_assoc
7693 && !(sym->ns->parent == nl->sym->ns)
7694 && !(sym->ns->parent
7695 && sym->ns->parent->parent == nl->sym->ns)
7696 && !gfc_check_access(nl->sym->attr.access,
7697 nl->sym->ns->default_access))
7699 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
7700 "cannot be member of PUBLIC namelist '%s' at %L",
7701 nl->sym->name, sym->name, &sym->declared_at);
7705 /* Types with private components that came here by USE-association. */
7706 if (nl->sym->ts.type == BT_DERIVED
7707 && derived_inaccessible (nl->sym->ts.derived))
7709 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
7710 "components and cannot be member of namelist '%s' at %L",
7711 nl->sym->name, sym->name, &sym->declared_at);
7715 /* Types with private components that are defined in the same module. */
7716 if (nl->sym->ts.type == BT_DERIVED
7717 && !(sym->ns->parent == nl->sym->ts.derived->ns)
7718 && !gfc_check_access (nl->sym->ts.derived->attr.private_comp
7719 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
7720 nl->sym->ns->default_access))
7722 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
7723 "cannot be a member of PUBLIC namelist '%s' at %L",
7724 nl->sym->name, sym->name, &sym->declared_at);
7730 for (nl = sym->namelist; nl; nl = nl->next)
7732 /* Reject namelist arrays of assumed shape. */
7733 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
7734 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
7735 "must not have assumed shape in namelist "
7736 "'%s' at %L", nl->sym->name, sym->name,
7737 &sym->declared_at) == FAILURE)
7740 /* Reject namelist arrays that are not constant shape. */
7741 if (is_non_constant_shape_array (nl->sym))
7743 gfc_error ("NAMELIST array object '%s' must have constant "
7744 "shape in namelist '%s' at %L", nl->sym->name,
7745 sym->name, &sym->declared_at);
7749 /* Namelist objects cannot have allocatable or pointer components. */
7750 if (nl->sym->ts.type != BT_DERIVED)
7753 if (nl->sym->ts.derived->attr.alloc_comp)
7755 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
7756 "have ALLOCATABLE components",
7757 nl->sym->name, sym->name, &sym->declared_at);
7761 if (nl->sym->ts.derived->attr.pointer_comp)
7763 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
7764 "have POINTER components",
7765 nl->sym->name, sym->name, &sym->declared_at);
7771 /* 14.1.2 A module or internal procedure represent local entities
7772 of the same type as a namelist member and so are not allowed. */
7773 for (nl = sym->namelist; nl; nl = nl->next)
7775 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
7778 if (nl->sym->attr.function && nl->sym == nl->sym->result)
7779 if ((nl->sym == sym->ns->proc_name)
7781 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
7785 if (nl->sym && nl->sym->name)
7786 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
7787 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
7789 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
7790 "attribute in '%s' at %L", nlsym->name,
7801 resolve_fl_parameter (gfc_symbol *sym)
7803 /* A parameter array's shape needs to be constant. */
7805 && (sym->as->type == AS_DEFERRED
7806 || is_non_constant_shape_array (sym)))
7808 gfc_error ("Parameter array '%s' at %L cannot be automatic "
7809 "or of deferred shape", sym->name, &sym->declared_at);
7813 /* Make sure a parameter that has been implicitly typed still
7814 matches the implicit type, since PARAMETER statements can precede
7815 IMPLICIT statements. */
7816 if (sym->attr.implicit_type
7817 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym, sym->ns)))
7819 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
7820 "later IMPLICIT type", sym->name, &sym->declared_at);
7824 /* Make sure the types of derived parameters are consistent. This
7825 type checking is deferred until resolution because the type may
7826 refer to a derived type from the host. */
7827 if (sym->ts.type == BT_DERIVED
7828 && !gfc_compare_types (&sym->ts, &sym->value->ts))
7830 gfc_error ("Incompatible derived type in PARAMETER at %L",
7831 &sym->value->where);
7838 /* Do anything necessary to resolve a symbol. Right now, we just
7839 assume that an otherwise unknown symbol is a variable. This sort
7840 of thing commonly happens for symbols in module. */
7843 resolve_symbol (gfc_symbol *sym)
7845 int check_constant, mp_flag;
7846 gfc_symtree *symtree;
7847 gfc_symtree *this_symtree;
7851 if (sym->attr.flavor == FL_UNKNOWN)
7854 /* If we find that a flavorless symbol is an interface in one of the
7855 parent namespaces, find its symtree in this namespace, free the
7856 symbol and set the symtree to point to the interface symbol. */
7857 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
7859 symtree = gfc_find_symtree (ns->sym_root, sym->name);
7860 if (symtree && symtree->n.sym->generic)
7862 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
7866 gfc_free_symbol (sym);
7867 symtree->n.sym->refs++;
7868 this_symtree->n.sym = symtree->n.sym;
7873 /* Otherwise give it a flavor according to such attributes as
7875 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
7876 sym->attr.flavor = FL_VARIABLE;
7879 sym->attr.flavor = FL_PROCEDURE;
7880 if (sym->attr.dimension)
7881 sym->attr.function = 1;
7885 if (sym->attr.procedure && sym->ts.interface
7886 && sym->attr.if_source != IFSRC_DECL)
7888 if (sym->ts.interface->attr.procedure)
7889 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared "
7890 "in a later PROCEDURE statement", sym->ts.interface->name,
7891 sym->name,&sym->declared_at);
7893 /* Get the attributes from the interface (now resolved). */
7894 if (sym->ts.interface->attr.if_source || sym->ts.interface->attr.intrinsic)
7896 sym->ts.type = sym->ts.interface->ts.type;
7897 sym->ts.kind = sym->ts.interface->ts.kind;
7898 sym->attr.function = sym->ts.interface->attr.function;
7899 sym->attr.subroutine = sym->ts.interface->attr.subroutine;
7900 copy_formal_args (sym, sym->ts.interface);
7902 else if (sym->ts.interface->name[0] != '\0')
7904 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
7905 sym->ts.interface->name, sym->name, &sym->declared_at);
7910 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
7913 /* Symbols that are module procedures with results (functions) have
7914 the types and array specification copied for type checking in
7915 procedures that call them, as well as for saving to a module
7916 file. These symbols can't stand the scrutiny that their results
7918 mp_flag = (sym->result != NULL && sym->result != sym);
7921 /* Make sure that the intrinsic is consistent with its internal
7922 representation. This needs to be done before assigning a default
7923 type to avoid spurious warnings. */
7924 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic)
7926 if (gfc_intrinsic_name (sym->name, 0))
7928 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising)
7929 gfc_warning ("Type specified for intrinsic function '%s' at %L is ignored",
7930 sym->name, &sym->declared_at);
7932 else if (gfc_intrinsic_name (sym->name, 1))
7934 if (sym->ts.type != BT_UNKNOWN)
7936 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type specifier",
7937 sym->name, &sym->declared_at);
7943 gfc_error ("Intrinsic '%s' at %L does not exist", sym->name, &sym->declared_at);
7948 /* Assign default type to symbols that need one and don't have one. */
7949 if (sym->ts.type == BT_UNKNOWN)
7951 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
7952 gfc_set_default_type (sym, 1, NULL);
7954 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
7956 /* The specific case of an external procedure should emit an error
7957 in the case that there is no implicit type. */
7959 gfc_set_default_type (sym, sym->attr.external, NULL);
7962 /* Result may be in another namespace. */
7963 resolve_symbol (sym->result);
7965 sym->ts = sym->result->ts;
7966 sym->as = gfc_copy_array_spec (sym->result->as);
7967 sym->attr.dimension = sym->result->attr.dimension;
7968 sym->attr.pointer = sym->result->attr.pointer;
7969 sym->attr.allocatable = sym->result->attr.allocatable;
7974 /* Assumed size arrays and assumed shape arrays must be dummy
7978 && (sym->as->type == AS_ASSUMED_SIZE
7979 || sym->as->type == AS_ASSUMED_SHAPE)
7980 && sym->attr.dummy == 0)
7982 if (sym->as->type == AS_ASSUMED_SIZE)
7983 gfc_error ("Assumed size array at %L must be a dummy argument",
7986 gfc_error ("Assumed shape array at %L must be a dummy argument",
7991 /* Make sure symbols with known intent or optional are really dummy
7992 variable. Because of ENTRY statement, this has to be deferred
7993 until resolution time. */
7995 if (!sym->attr.dummy
7996 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
7998 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
8002 if (sym->attr.value && !sym->attr.dummy)
8004 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
8005 "it is not a dummy argument", sym->name, &sym->declared_at);
8009 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
8011 gfc_charlen *cl = sym->ts.cl;
8012 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
8014 gfc_error ("Character dummy variable '%s' at %L with VALUE "
8015 "attribute must have constant length",
8016 sym->name, &sym->declared_at);
8020 if (sym->ts.is_c_interop
8021 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
8023 gfc_error ("C interoperable character dummy variable '%s' at %L "
8024 "with VALUE attribute must have length one",
8025 sym->name, &sym->declared_at);
8030 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
8031 do this for something that was implicitly typed because that is handled
8032 in gfc_set_default_type. Handle dummy arguments and procedure
8033 definitions separately. Also, anything that is use associated is not
8034 handled here but instead is handled in the module it is declared in.
8035 Finally, derived type definitions are allowed to be BIND(C) since that
8036 only implies that they're interoperable, and they are checked fully for
8037 interoperability when a variable is declared of that type. */
8038 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
8039 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
8040 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
8044 /* First, make sure the variable is declared at the
8045 module-level scope (J3/04-007, Section 15.3). */
8046 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
8047 sym->attr.in_common == 0)
8049 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
8050 "is neither a COMMON block nor declared at the "
8051 "module level scope", sym->name, &(sym->declared_at));
8054 else if (sym->common_head != NULL)
8056 t = verify_com_block_vars_c_interop (sym->common_head);
8060 /* If type() declaration, we need to verify that the components
8061 of the given type are all C interoperable, etc. */
8062 if (sym->ts.type == BT_DERIVED &&
8063 sym->ts.derived->attr.is_c_interop != 1)
8065 /* Make sure the user marked the derived type as BIND(C). If
8066 not, call the verify routine. This could print an error
8067 for the derived type more than once if multiple variables
8068 of that type are declared. */
8069 if (sym->ts.derived->attr.is_bind_c != 1)
8070 verify_bind_c_derived_type (sym->ts.derived);
8074 /* Verify the variable itself as C interoperable if it
8075 is BIND(C). It is not possible for this to succeed if
8076 the verify_bind_c_derived_type failed, so don't have to handle
8077 any error returned by verify_bind_c_derived_type. */
8078 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
8084 /* clear the is_bind_c flag to prevent reporting errors more than
8085 once if something failed. */
8086 sym->attr.is_bind_c = 0;
8091 /* If a derived type symbol has reached this point, without its
8092 type being declared, we have an error. Notice that most
8093 conditions that produce undefined derived types have already
8094 been dealt with. However, the likes of:
8095 implicit type(t) (t) ..... call foo (t) will get us here if
8096 the type is not declared in the scope of the implicit
8097 statement. Change the type to BT_UNKNOWN, both because it is so
8098 and to prevent an ICE. */
8099 if (sym->ts.type == BT_DERIVED && sym->ts.derived->components == NULL
8100 && !sym->ts.derived->attr.zero_comp)
8102 gfc_error ("The derived type '%s' at %L is of type '%s', "
8103 "which has not been defined", sym->name,
8104 &sym->declared_at, sym->ts.derived->name);
8105 sym->ts.type = BT_UNKNOWN;
8109 /* Make sure that the derived type has been resolved and that the
8110 derived type is visible in the symbol's namespace, if it is a
8111 module function and is not PRIVATE. */
8112 if (sym->ts.type == BT_DERIVED
8113 && sym->ts.derived->attr.use_assoc
8114 && sym->ns->proc_name->attr.flavor == FL_MODULE)
8118 if (resolve_fl_derived (sym->ts.derived) == FAILURE)
8121 gfc_find_symbol (sym->ts.derived->name, sym->ns, 1, &ds);
8122 if (!ds && sym->attr.function
8123 && gfc_check_access (sym->attr.access, sym->ns->default_access))
8125 symtree = gfc_new_symtree (&sym->ns->sym_root,
8126 sym->ts.derived->name);
8127 symtree->n.sym = sym->ts.derived;
8128 sym->ts.derived->refs++;
8132 /* Unless the derived-type declaration is use associated, Fortran 95
8133 does not allow public entries of private derived types.
8134 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
8136 if (sym->ts.type == BT_DERIVED
8137 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
8138 && !sym->ts.derived->attr.use_assoc
8139 && gfc_check_access (sym->attr.access, sym->ns->default_access)
8140 && !gfc_check_access (sym->ts.derived->attr.access,
8141 sym->ts.derived->ns->default_access)
8142 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
8143 "of PRIVATE derived type '%s'",
8144 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
8145 : "variable", sym->name, &sym->declared_at,
8146 sym->ts.derived->name) == FAILURE)
8149 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
8150 default initialization is defined (5.1.2.4.4). */
8151 if (sym->ts.type == BT_DERIVED
8153 && sym->attr.intent == INTENT_OUT
8155 && sym->as->type == AS_ASSUMED_SIZE)
8157 for (c = sym->ts.derived->components; c; c = c->next)
8161 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
8162 "ASSUMED SIZE and so cannot have a default initializer",
8163 sym->name, &sym->declared_at);
8169 switch (sym->attr.flavor)
8172 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
8177 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
8182 if (resolve_fl_namelist (sym) == FAILURE)
8187 if (resolve_fl_parameter (sym) == FAILURE)
8195 /* Resolve array specifier. Check as well some constraints
8196 on COMMON blocks. */
8198 check_constant = sym->attr.in_common && !sym->attr.pointer;
8200 /* Set the formal_arg_flag so that check_conflict will not throw
8201 an error for host associated variables in the specification
8202 expression for an array_valued function. */
8203 if (sym->attr.function && sym->as)
8204 formal_arg_flag = 1;
8206 gfc_resolve_array_spec (sym->as, check_constant);
8208 formal_arg_flag = 0;
8210 /* Resolve formal namespaces. */
8211 if (sym->formal_ns && sym->formal_ns != gfc_current_ns)
8212 gfc_resolve (sym->formal_ns);
8214 /* Check threadprivate restrictions. */
8215 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
8216 && (!sym->attr.in_common
8217 && sym->module == NULL
8218 && (sym->ns->proc_name == NULL
8219 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
8220 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
8222 /* If we have come this far we can apply default-initializers, as
8223 described in 14.7.5, to those variables that have not already
8224 been assigned one. */
8225 if (sym->ts.type == BT_DERIVED
8226 && sym->attr.referenced
8227 && sym->ns == gfc_current_ns
8229 && !sym->attr.allocatable
8230 && !sym->attr.alloc_comp)
8232 symbol_attribute *a = &sym->attr;
8234 if ((!a->save && !a->dummy && !a->pointer
8235 && !a->in_common && !a->use_assoc
8236 && !(a->function && sym != sym->result))
8237 || (a->dummy && a->intent == INTENT_OUT))
8238 apply_default_init (sym);
8243 /************* Resolve DATA statements *************/
8247 gfc_data_value *vnode;
8253 /* Advance the values structure to point to the next value in the data list. */
8256 next_data_value (void)
8259 while (mpz_cmp_ui (values.left, 0) == 0)
8261 if (values.vnode->next == NULL)
8264 values.vnode = values.vnode->next;
8265 mpz_set (values.left, values.vnode->repeat);
8273 check_data_variable (gfc_data_variable *var, locus *where)
8279 ar_type mark = AR_UNKNOWN;
8281 mpz_t section_index[GFC_MAX_DIMENSIONS];
8285 if (gfc_resolve_expr (var->expr) == FAILURE)
8289 mpz_init_set_si (offset, 0);
8292 if (e->expr_type != EXPR_VARIABLE)
8293 gfc_internal_error ("check_data_variable(): Bad expression");
8295 if (e->symtree->n.sym->ns->is_block_data
8296 && !e->symtree->n.sym->attr.in_common)
8298 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
8299 e->symtree->n.sym->name, &e->symtree->n.sym->declared_at);
8302 if (e->ref == NULL && e->symtree->n.sym->as)
8304 gfc_error ("DATA array '%s' at %L must be specified in a previous"
8305 " declaration", e->symtree->n.sym->name, where);
8311 mpz_init_set_ui (size, 1);
8318 /* Find the array section reference. */
8319 for (ref = e->ref; ref; ref = ref->next)
8321 if (ref->type != REF_ARRAY)
8323 if (ref->u.ar.type == AR_ELEMENT)
8329 /* Set marks according to the reference pattern. */
8330 switch (ref->u.ar.type)
8338 /* Get the start position of array section. */
8339 gfc_get_section_index (ar, section_index, &offset);
8347 if (gfc_array_size (e, &size) == FAILURE)
8349 gfc_error ("Nonconstant array section at %L in DATA statement",
8358 while (mpz_cmp_ui (size, 0) > 0)
8360 if (next_data_value () == FAILURE)
8362 gfc_error ("DATA statement at %L has more variables than values",
8368 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
8372 /* If we have more than one element left in the repeat count,
8373 and we have more than one element left in the target variable,
8374 then create a range assignment. */
8375 /* FIXME: Only done for full arrays for now, since array sections
8377 if (mark == AR_FULL && ref && ref->next == NULL
8378 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
8382 if (mpz_cmp (size, values.left) >= 0)
8384 mpz_init_set (range, values.left);
8385 mpz_sub (size, size, values.left);
8386 mpz_set_ui (values.left, 0);
8390 mpz_init_set (range, size);
8391 mpz_sub (values.left, values.left, size);
8392 mpz_set_ui (size, 0);
8395 gfc_assign_data_value_range (var->expr, values.vnode->expr,
8398 mpz_add (offset, offset, range);
8402 /* Assign initial value to symbol. */
8405 mpz_sub_ui (values.left, values.left, 1);
8406 mpz_sub_ui (size, size, 1);
8408 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
8412 if (mark == AR_FULL)
8413 mpz_add_ui (offset, offset, 1);
8415 /* Modify the array section indexes and recalculate the offset
8416 for next element. */
8417 else if (mark == AR_SECTION)
8418 gfc_advance_section (section_index, ar, &offset);
8422 if (mark == AR_SECTION)
8424 for (i = 0; i < ar->dimen; i++)
8425 mpz_clear (section_index[i]);
8435 static try traverse_data_var (gfc_data_variable *, locus *);
8437 /* Iterate over a list of elements in a DATA statement. */
8440 traverse_data_list (gfc_data_variable *var, locus *where)
8443 iterator_stack frame;
8444 gfc_expr *e, *start, *end, *step;
8445 try retval = SUCCESS;
8447 mpz_init (frame.value);
8449 start = gfc_copy_expr (var->iter.start);
8450 end = gfc_copy_expr (var->iter.end);
8451 step = gfc_copy_expr (var->iter.step);
8453 if (gfc_simplify_expr (start, 1) == FAILURE
8454 || start->expr_type != EXPR_CONSTANT)
8456 gfc_error ("iterator start at %L does not simplify", &start->where);
8460 if (gfc_simplify_expr (end, 1) == FAILURE
8461 || end->expr_type != EXPR_CONSTANT)
8463 gfc_error ("iterator end at %L does not simplify", &end->where);
8467 if (gfc_simplify_expr (step, 1) == FAILURE
8468 || step->expr_type != EXPR_CONSTANT)
8470 gfc_error ("iterator step at %L does not simplify", &step->where);
8475 mpz_init_set (trip, end->value.integer);
8476 mpz_sub (trip, trip, start->value.integer);
8477 mpz_add (trip, trip, step->value.integer);
8479 mpz_div (trip, trip, step->value.integer);
8481 mpz_set (frame.value, start->value.integer);
8483 frame.prev = iter_stack;
8484 frame.variable = var->iter.var->symtree;
8485 iter_stack = &frame;
8487 while (mpz_cmp_ui (trip, 0) > 0)
8489 if (traverse_data_var (var->list, where) == FAILURE)
8496 e = gfc_copy_expr (var->expr);
8497 if (gfc_simplify_expr (e, 1) == FAILURE)
8505 mpz_add (frame.value, frame.value, step->value.integer);
8507 mpz_sub_ui (trip, trip, 1);
8512 mpz_clear (frame.value);
8514 gfc_free_expr (start);
8515 gfc_free_expr (end);
8516 gfc_free_expr (step);
8518 iter_stack = frame.prev;
8523 /* Type resolve variables in the variable list of a DATA statement. */
8526 traverse_data_var (gfc_data_variable *var, locus *where)
8530 for (; var; var = var->next)
8532 if (var->expr == NULL)
8533 t = traverse_data_list (var, where);
8535 t = check_data_variable (var, where);
8545 /* Resolve the expressions and iterators associated with a data statement.
8546 This is separate from the assignment checking because data lists should
8547 only be resolved once. */
8550 resolve_data_variables (gfc_data_variable *d)
8552 for (; d; d = d->next)
8554 if (d->list == NULL)
8556 if (gfc_resolve_expr (d->expr) == FAILURE)
8561 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
8564 if (resolve_data_variables (d->list) == FAILURE)
8573 /* Resolve a single DATA statement. We implement this by storing a pointer to
8574 the value list into static variables, and then recursively traversing the
8575 variables list, expanding iterators and such. */
8578 resolve_data (gfc_data *d)
8581 if (resolve_data_variables (d->var) == FAILURE)
8584 values.vnode = d->value;
8585 if (d->value == NULL)
8586 mpz_set_ui (values.left, 0);
8588 mpz_set (values.left, d->value->repeat);
8590 if (traverse_data_var (d->var, &d->where) == FAILURE)
8593 /* At this point, we better not have any values left. */
8595 if (next_data_value () == SUCCESS)
8596 gfc_error ("DATA statement at %L has more values than variables",
8601 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
8602 accessed by host or use association, is a dummy argument to a pure function,
8603 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
8604 is storage associated with any such variable, shall not be used in the
8605 following contexts: (clients of this function). */
8607 /* Determines if a variable is not 'pure', ie not assignable within a pure
8608 procedure. Returns zero if assignment is OK, nonzero if there is a
8611 gfc_impure_variable (gfc_symbol *sym)
8615 if (sym->attr.use_assoc || sym->attr.in_common)
8618 if (sym->ns != gfc_current_ns)
8619 return !sym->attr.function;
8621 proc = sym->ns->proc_name;
8622 if (sym->attr.dummy && gfc_pure (proc)
8623 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
8625 proc->attr.function))
8628 /* TODO: Sort out what can be storage associated, if anything, and include
8629 it here. In principle equivalences should be scanned but it does not
8630 seem to be possible to storage associate an impure variable this way. */
8635 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
8636 symbol of the current procedure. */
8639 gfc_pure (gfc_symbol *sym)
8641 symbol_attribute attr;
8644 sym = gfc_current_ns->proc_name;
8650 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
8654 /* Test whether the current procedure is elemental or not. */
8657 gfc_elemental (gfc_symbol *sym)
8659 symbol_attribute attr;
8662 sym = gfc_current_ns->proc_name;
8667 return attr.flavor == FL_PROCEDURE && attr.elemental;
8671 /* Warn about unused labels. */
8674 warn_unused_fortran_label (gfc_st_label *label)
8679 warn_unused_fortran_label (label->left);
8681 if (label->defined == ST_LABEL_UNKNOWN)
8684 switch (label->referenced)
8686 case ST_LABEL_UNKNOWN:
8687 gfc_warning ("Label %d at %L defined but not used", label->value,
8691 case ST_LABEL_BAD_TARGET:
8692 gfc_warning ("Label %d at %L defined but cannot be used",
8693 label->value, &label->where);
8700 warn_unused_fortran_label (label->right);
8704 /* Returns the sequence type of a symbol or sequence. */
8707 sequence_type (gfc_typespec ts)
8716 if (ts.derived->components == NULL)
8717 return SEQ_NONDEFAULT;
8719 result = sequence_type (ts.derived->components->ts);
8720 for (c = ts.derived->components->next; c; c = c->next)
8721 if (sequence_type (c->ts) != result)
8727 if (ts.kind != gfc_default_character_kind)
8728 return SEQ_NONDEFAULT;
8730 return SEQ_CHARACTER;
8733 if (ts.kind != gfc_default_integer_kind)
8734 return SEQ_NONDEFAULT;
8739 if (!(ts.kind == gfc_default_real_kind
8740 || ts.kind == gfc_default_double_kind))
8741 return SEQ_NONDEFAULT;
8746 if (ts.kind != gfc_default_complex_kind)
8747 return SEQ_NONDEFAULT;
8752 if (ts.kind != gfc_default_logical_kind)
8753 return SEQ_NONDEFAULT;
8758 return SEQ_NONDEFAULT;
8763 /* Resolve derived type EQUIVALENCE object. */
8766 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
8769 gfc_component *c = derived->components;
8774 /* Shall not be an object of nonsequence derived type. */
8775 if (!derived->attr.sequence)
8777 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
8778 "attribute to be an EQUIVALENCE object", sym->name,
8783 /* Shall not have allocatable components. */
8784 if (derived->attr.alloc_comp)
8786 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
8787 "components to be an EQUIVALENCE object",sym->name,
8792 if (sym->attr.in_common && has_default_initializer (sym->ts.derived))
8794 gfc_error ("Derived type variable '%s' at %L with default "
8795 "initialization cannot be in EQUIVALENCE with a variable "
8796 "in COMMON", sym->name, &e->where);
8800 for (; c ; c = c->next)
8804 && (resolve_equivalence_derived (c->ts.derived, sym, e) == FAILURE))
8807 /* Shall not be an object of sequence derived type containing a pointer
8808 in the structure. */
8811 gfc_error ("Derived type variable '%s' at %L with pointer "
8812 "component(s) cannot be an EQUIVALENCE object",
8813 sym->name, &e->where);
8821 /* Resolve equivalence object.
8822 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
8823 an allocatable array, an object of nonsequence derived type, an object of
8824 sequence derived type containing a pointer at any level of component
8825 selection, an automatic object, a function name, an entry name, a result
8826 name, a named constant, a structure component, or a subobject of any of
8827 the preceding objects. A substring shall not have length zero. A
8828 derived type shall not have components with default initialization nor
8829 shall two objects of an equivalence group be initialized.
8830 Either all or none of the objects shall have an protected attribute.
8831 The simple constraints are done in symbol.c(check_conflict) and the rest
8832 are implemented here. */
8835 resolve_equivalence (gfc_equiv *eq)
8838 gfc_symbol *derived;
8839 gfc_symbol *first_sym;
8842 locus *last_where = NULL;
8843 seq_type eq_type, last_eq_type;
8844 gfc_typespec *last_ts;
8845 int object, cnt_protected;
8846 const char *value_name;
8850 last_ts = &eq->expr->symtree->n.sym->ts;
8852 first_sym = eq->expr->symtree->n.sym;
8856 for (object = 1; eq; eq = eq->eq, object++)
8860 e->ts = e->symtree->n.sym->ts;
8861 /* match_varspec might not know yet if it is seeing
8862 array reference or substring reference, as it doesn't
8864 if (e->ref && e->ref->type == REF_ARRAY)
8866 gfc_ref *ref = e->ref;
8867 sym = e->symtree->n.sym;
8869 if (sym->attr.dimension)
8871 ref->u.ar.as = sym->as;
8875 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
8876 if (e->ts.type == BT_CHARACTER
8878 && ref->type == REF_ARRAY
8879 && ref->u.ar.dimen == 1
8880 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
8881 && ref->u.ar.stride[0] == NULL)
8883 gfc_expr *start = ref->u.ar.start[0];
8884 gfc_expr *end = ref->u.ar.end[0];
8887 /* Optimize away the (:) reference. */
8888 if (start == NULL && end == NULL)
8893 e->ref->next = ref->next;
8898 ref->type = REF_SUBSTRING;
8900 start = gfc_int_expr (1);
8901 ref->u.ss.start = start;
8902 if (end == NULL && e->ts.cl)
8903 end = gfc_copy_expr (e->ts.cl->length);
8904 ref->u.ss.end = end;
8905 ref->u.ss.length = e->ts.cl;
8912 /* Any further ref is an error. */
8915 gcc_assert (ref->type == REF_ARRAY);
8916 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
8922 if (gfc_resolve_expr (e) == FAILURE)
8925 sym = e->symtree->n.sym;
8927 if (sym->attr.protected)
8929 if (cnt_protected > 0 && cnt_protected != object)
8931 gfc_error ("Either all or none of the objects in the "
8932 "EQUIVALENCE set at %L shall have the "
8933 "PROTECTED attribute",
8938 /* Shall not equivalence common block variables in a PURE procedure. */
8939 if (sym->ns->proc_name
8940 && sym->ns->proc_name->attr.pure
8941 && sym->attr.in_common)
8943 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
8944 "object in the pure procedure '%s'",
8945 sym->name, &e->where, sym->ns->proc_name->name);
8949 /* Shall not be a named constant. */
8950 if (e->expr_type == EXPR_CONSTANT)
8952 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
8953 "object", sym->name, &e->where);
8957 derived = e->ts.derived;
8958 if (derived && resolve_equivalence_derived (derived, sym, e) == FAILURE)
8961 /* Check that the types correspond correctly:
8963 A numeric sequence structure may be equivalenced to another sequence
8964 structure, an object of default integer type, default real type, double
8965 precision real type, default logical type such that components of the
8966 structure ultimately only become associated to objects of the same
8967 kind. A character sequence structure may be equivalenced to an object
8968 of default character kind or another character sequence structure.
8969 Other objects may be equivalenced only to objects of the same type and
8972 /* Identical types are unconditionally OK. */
8973 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
8974 goto identical_types;
8976 last_eq_type = sequence_type (*last_ts);
8977 eq_type = sequence_type (sym->ts);
8979 /* Since the pair of objects is not of the same type, mixed or
8980 non-default sequences can be rejected. */
8982 msg = "Sequence %s with mixed components in EQUIVALENCE "
8983 "statement at %L with different type objects";
8985 && last_eq_type == SEQ_MIXED
8986 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
8988 || (eq_type == SEQ_MIXED
8989 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
8990 &e->where) == FAILURE))
8993 msg = "Non-default type object or sequence %s in EQUIVALENCE "
8994 "statement at %L with objects of different type";
8996 && last_eq_type == SEQ_NONDEFAULT
8997 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
8998 last_where) == FAILURE)
8999 || (eq_type == SEQ_NONDEFAULT
9000 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
9001 &e->where) == FAILURE))
9004 msg ="Non-CHARACTER object '%s' in default CHARACTER "
9005 "EQUIVALENCE statement at %L";
9006 if (last_eq_type == SEQ_CHARACTER
9007 && eq_type != SEQ_CHARACTER
9008 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
9009 &e->where) == FAILURE)
9012 msg ="Non-NUMERIC object '%s' in default NUMERIC "
9013 "EQUIVALENCE statement at %L";
9014 if (last_eq_type == SEQ_NUMERIC
9015 && eq_type != SEQ_NUMERIC
9016 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
9017 &e->where) == FAILURE)
9022 last_where = &e->where;
9027 /* Shall not be an automatic array. */
9028 if (e->ref->type == REF_ARRAY
9029 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
9031 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
9032 "an EQUIVALENCE object", sym->name, &e->where);
9039 /* Shall not be a structure component. */
9040 if (r->type == REF_COMPONENT)
9042 gfc_error ("Structure component '%s' at %L cannot be an "
9043 "EQUIVALENCE object",
9044 r->u.c.component->name, &e->where);
9048 /* A substring shall not have length zero. */
9049 if (r->type == REF_SUBSTRING)
9051 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
9053 gfc_error ("Substring at %L has length zero",
9054 &r->u.ss.start->where);
9064 /* Resolve function and ENTRY types, issue diagnostics if needed. */
9067 resolve_fntype (gfc_namespace *ns)
9072 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
9075 /* If there are any entries, ns->proc_name is the entry master
9076 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
9078 sym = ns->entries->sym;
9080 sym = ns->proc_name;
9081 if (sym->result == sym
9082 && sym->ts.type == BT_UNKNOWN
9083 && gfc_set_default_type (sym, 0, NULL) == FAILURE
9084 && !sym->attr.untyped)
9086 gfc_error ("Function '%s' at %L has no IMPLICIT type",
9087 sym->name, &sym->declared_at);
9088 sym->attr.untyped = 1;
9091 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.use_assoc
9092 && !gfc_check_access (sym->ts.derived->attr.access,
9093 sym->ts.derived->ns->default_access)
9094 && gfc_check_access (sym->attr.access, sym->ns->default_access))
9096 gfc_error ("PUBLIC function '%s' at %L cannot be of PRIVATE type '%s'",
9097 sym->name, &sym->declared_at, sym->ts.derived->name);
9101 for (el = ns->entries->next; el; el = el->next)
9103 if (el->sym->result == el->sym
9104 && el->sym->ts.type == BT_UNKNOWN
9105 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
9106 && !el->sym->attr.untyped)
9108 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
9109 el->sym->name, &el->sym->declared_at);
9110 el->sym->attr.untyped = 1;
9115 /* 12.3.2.1.1 Defined operators. */
9118 gfc_resolve_uops (gfc_symtree *symtree)
9122 gfc_formal_arglist *formal;
9124 if (symtree == NULL)
9127 gfc_resolve_uops (symtree->left);
9128 gfc_resolve_uops (symtree->right);
9130 for (itr = symtree->n.uop->operator; itr; itr = itr->next)
9133 if (!sym->attr.function)
9134 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
9135 sym->name, &sym->declared_at);
9137 if (sym->ts.type == BT_CHARACTER
9138 && !(sym->ts.cl && sym->ts.cl->length)
9139 && !(sym->result && sym->result->ts.cl
9140 && sym->result->ts.cl->length))
9141 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
9142 "character length", sym->name, &sym->declared_at);
9144 formal = sym->formal;
9145 if (!formal || !formal->sym)
9147 gfc_error ("User operator procedure '%s' at %L must have at least "
9148 "one argument", sym->name, &sym->declared_at);
9152 if (formal->sym->attr.intent != INTENT_IN)
9153 gfc_error ("First argument of operator interface at %L must be "
9154 "INTENT(IN)", &sym->declared_at);
9156 if (formal->sym->attr.optional)
9157 gfc_error ("First argument of operator interface at %L cannot be "
9158 "optional", &sym->declared_at);
9160 formal = formal->next;
9161 if (!formal || !formal->sym)
9164 if (formal->sym->attr.intent != INTENT_IN)
9165 gfc_error ("Second argument of operator interface at %L must be "
9166 "INTENT(IN)", &sym->declared_at);
9168 if (formal->sym->attr.optional)
9169 gfc_error ("Second argument of operator interface at %L cannot be "
9170 "optional", &sym->declared_at);
9173 gfc_error ("Operator interface at %L must have, at most, two "
9174 "arguments", &sym->declared_at);
9179 /* Examine all of the expressions associated with a program unit,
9180 assign types to all intermediate expressions, make sure that all
9181 assignments are to compatible types and figure out which names
9182 refer to which functions or subroutines. It doesn't check code
9183 block, which is handled by resolve_code. */
9186 resolve_types (gfc_namespace *ns)
9193 gfc_current_ns = ns;
9195 resolve_entries (ns);
9197 resolve_common_vars (ns->blank_common.head, false);
9198 resolve_common_blocks (ns->common_root);
9200 resolve_contained_functions (ns);
9202 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
9204 for (cl = ns->cl_list; cl; cl = cl->next)
9205 resolve_charlen (cl);
9207 gfc_traverse_ns (ns, resolve_symbol);
9209 resolve_fntype (ns);
9211 for (n = ns->contained; n; n = n->sibling)
9213 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
9214 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
9215 "also be PURE", n->proc_name->name,
9216 &n->proc_name->declared_at);
9222 gfc_check_interfaces (ns);
9224 gfc_traverse_ns (ns, resolve_values);
9230 for (d = ns->data; d; d = d->next)
9234 gfc_traverse_ns (ns, gfc_formalize_init_value);
9236 gfc_traverse_ns (ns, gfc_verify_binding_labels);
9238 if (ns->common_root != NULL)
9239 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
9241 for (eq = ns->equiv; eq; eq = eq->next)
9242 resolve_equivalence (eq);
9244 /* Warn about unused labels. */
9245 if (warn_unused_label)
9246 warn_unused_fortran_label (ns->st_labels);
9248 gfc_resolve_uops (ns->uop_root);
9252 /* Call resolve_code recursively. */
9255 resolve_codes (gfc_namespace *ns)
9259 for (n = ns->contained; n; n = n->sibling)
9262 gfc_current_ns = ns;
9264 /* Set to an out of range value. */
9265 current_entry_id = -1;
9267 bitmap_obstack_initialize (&labels_obstack);
9268 resolve_code (ns->code, ns);
9269 bitmap_obstack_release (&labels_obstack);
9273 /* This function is called after a complete program unit has been compiled.
9274 Its purpose is to examine all of the expressions associated with a program
9275 unit, assign types to all intermediate expressions, make sure that all
9276 assignments are to compatible types and figure out which names refer to
9277 which functions or subroutines. */
9280 gfc_resolve (gfc_namespace *ns)
9282 gfc_namespace *old_ns;
9284 old_ns = gfc_current_ns;
9289 gfc_current_ns = old_ns;