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 (!comp->pointer || cons->expr->expr_type == EXPR_NULL)
840 a = gfc_expr_attr (cons->expr);
842 if (!a.pointer && !a.target)
845 gfc_error ("The element in the derived type constructor at %L, "
846 "for pointer component '%s' should be a POINTER or "
847 "a TARGET", &cons->expr->where, comp->name);
855 /****************** Expression name resolution ******************/
857 /* Returns 0 if a symbol was not declared with a type or
858 attribute declaration statement, nonzero otherwise. */
861 was_declared (gfc_symbol *sym)
867 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
870 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
871 || a.optional || a.pointer || a.save || a.target || a.volatile_
872 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
879 /* Determine if a symbol is generic or not. */
882 generic_sym (gfc_symbol *sym)
886 if (sym->attr.generic ||
887 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
890 if (was_declared (sym) || sym->ns->parent == NULL)
893 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
900 return generic_sym (s);
907 /* Determine if a symbol is specific or not. */
910 specific_sym (gfc_symbol *sym)
914 if (sym->attr.if_source == IFSRC_IFBODY
915 || sym->attr.proc == PROC_MODULE
916 || sym->attr.proc == PROC_INTERNAL
917 || sym->attr.proc == PROC_ST_FUNCTION
918 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
919 || sym->attr.external)
922 if (was_declared (sym) || sym->ns->parent == NULL)
925 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
927 return (s == NULL) ? 0 : specific_sym (s);
931 /* Figure out if the procedure is specific, generic or unknown. */
934 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
938 procedure_kind (gfc_symbol *sym)
940 if (generic_sym (sym))
941 return PTYPE_GENERIC;
943 if (specific_sym (sym))
944 return PTYPE_SPECIFIC;
946 return PTYPE_UNKNOWN;
949 /* Check references to assumed size arrays. The flag need_full_assumed_size
950 is nonzero when matching actual arguments. */
952 static int need_full_assumed_size = 0;
955 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
961 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
964 for (ref = e->ref; ref; ref = ref->next)
965 if (ref->type == REF_ARRAY)
966 for (dim = 0; dim < ref->u.ar.as->rank; dim++)
967 last = (ref->u.ar.end[dim] == NULL)
968 && (ref->u.ar.type == DIMEN_ELEMENT);
972 gfc_error ("The upper bound in the last dimension must "
973 "appear in the reference to the assumed size "
974 "array '%s' at %L", sym->name, &e->where);
981 /* Look for bad assumed size array references in argument expressions
982 of elemental and array valued intrinsic procedures. Since this is
983 called from procedure resolution functions, it only recurses at
987 resolve_assumed_size_actual (gfc_expr *e)
992 switch (e->expr_type)
995 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1000 if (resolve_assumed_size_actual (e->value.op.op1)
1001 || resolve_assumed_size_actual (e->value.op.op2))
1012 /* Resolve an actual argument list. Most of the time, this is just
1013 resolving the expressions in the list.
1014 The exception is that we sometimes have to decide whether arguments
1015 that look like procedure arguments are really simple variable
1019 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype)
1022 gfc_symtree *parent_st;
1024 int save_need_full_assumed_size;
1026 for (; arg; arg = arg->next)
1031 /* Check the label is a valid branching target. */
1034 if (arg->label->defined == ST_LABEL_UNKNOWN)
1036 gfc_error ("Label %d referenced at %L is never defined",
1037 arg->label->value, &arg->label->where);
1044 if (e->expr_type == FL_VARIABLE && e->symtree->ambiguous)
1046 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1051 if (e->ts.type != BT_PROCEDURE)
1053 save_need_full_assumed_size = need_full_assumed_size;
1054 if (e->expr_type != FL_VARIABLE)
1055 need_full_assumed_size = 0;
1056 if (gfc_resolve_expr (e) != SUCCESS)
1058 need_full_assumed_size = save_need_full_assumed_size;
1062 /* See if the expression node should really be a variable reference. */
1064 sym = e->symtree->n.sym;
1066 if (sym->attr.flavor == FL_PROCEDURE
1067 || sym->attr.intrinsic
1068 || sym->attr.external)
1072 /* If a procedure is not already determined to be something else
1073 check if it is intrinsic. */
1074 if (!sym->attr.intrinsic
1075 && !(sym->attr.external || sym->attr.use_assoc
1076 || sym->attr.if_source == IFSRC_IFBODY)
1077 && gfc_intrinsic_name (sym->name, sym->attr.subroutine))
1078 sym->attr.intrinsic = 1;
1080 if (sym->attr.proc == PROC_ST_FUNCTION)
1082 gfc_error ("Statement function '%s' at %L is not allowed as an "
1083 "actual argument", sym->name, &e->where);
1086 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1087 sym->attr.subroutine);
1088 if (sym->attr.intrinsic && actual_ok == 0)
1090 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1091 "actual argument", sym->name, &e->where);
1094 if (sym->attr.contained && !sym->attr.use_assoc
1095 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1097 gfc_error ("Internal procedure '%s' is not allowed as an "
1098 "actual argument at %L", sym->name, &e->where);
1101 if (sym->attr.elemental && !sym->attr.intrinsic)
1103 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1104 "allowed as an actual argument at %L", sym->name,
1108 /* Check if a generic interface has a specific procedure
1109 with the same name before emitting an error. */
1110 if (sym->attr.generic)
1113 for (p = sym->generic; p; p = p->next)
1114 if (strcmp (sym->name, p->sym->name) == 0)
1116 e->symtree = gfc_find_symtree
1117 (p->sym->ns->sym_root, sym->name);
1122 if (p == NULL || e->symtree == NULL)
1123 gfc_error ("GENERIC procedure '%s' is not "
1124 "allowed as an actual argument at %L", sym->name,
1128 /* If the symbol is the function that names the current (or
1129 parent) scope, then we really have a variable reference. */
1131 if (sym->attr.function && sym->result == sym
1132 && (sym->ns->proc_name == sym
1133 || (sym->ns->parent != NULL
1134 && sym->ns->parent->proc_name == sym)))
1137 /* If all else fails, see if we have a specific intrinsic. */
1138 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1140 gfc_intrinsic_sym *isym;
1142 isym = gfc_find_function (sym->name);
1143 if (isym == NULL || !isym->specific)
1145 gfc_error ("Unable to find a specific INTRINSIC procedure "
1146 "for the reference '%s' at %L", sym->name,
1151 sym->attr.intrinsic = 1;
1152 sym->attr.function = 1;
1157 /* See if the name is a module procedure in a parent unit. */
1159 if (was_declared (sym) || sym->ns->parent == NULL)
1162 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1164 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1168 if (parent_st == NULL)
1171 sym = parent_st->n.sym;
1172 e->symtree = parent_st; /* Point to the right thing. */
1174 if (sym->attr.flavor == FL_PROCEDURE
1175 || sym->attr.intrinsic
1176 || sym->attr.external)
1182 e->expr_type = EXPR_VARIABLE;
1184 if (sym->as != NULL)
1186 e->rank = sym->as->rank;
1187 e->ref = gfc_get_ref ();
1188 e->ref->type = REF_ARRAY;
1189 e->ref->u.ar.type = AR_FULL;
1190 e->ref->u.ar.as = sym->as;
1193 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1194 primary.c (match_actual_arg). If above code determines that it
1195 is a variable instead, it needs to be resolved as it was not
1196 done at the beginning of this function. */
1197 save_need_full_assumed_size = need_full_assumed_size;
1198 if (e->expr_type != FL_VARIABLE)
1199 need_full_assumed_size = 0;
1200 if (gfc_resolve_expr (e) != SUCCESS)
1202 need_full_assumed_size = save_need_full_assumed_size;
1205 /* Check argument list functions %VAL, %LOC and %REF. There is
1206 nothing to do for %REF. */
1207 if (arg->name && arg->name[0] == '%')
1209 if (strncmp ("%VAL", arg->name, 4) == 0)
1211 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1213 gfc_error ("By-value argument at %L is not of numeric "
1220 gfc_error ("By-value argument at %L cannot be an array or "
1221 "an array section", &e->where);
1225 /* Intrinsics are still PROC_UNKNOWN here. However,
1226 since same file external procedures are not resolvable
1227 in gfortran, it is a good deal easier to leave them to
1229 if (ptype != PROC_UNKNOWN
1230 && ptype != PROC_DUMMY
1231 && ptype != PROC_EXTERNAL
1232 && ptype != PROC_MODULE)
1234 gfc_error ("By-value argument at %L is not allowed "
1235 "in this context", &e->where);
1240 /* Statement functions have already been excluded above. */
1241 else if (strncmp ("%LOC", arg->name, 4) == 0
1242 && e->ts.type == BT_PROCEDURE)
1244 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1246 gfc_error ("Passing internal procedure at %L by location "
1247 "not allowed", &e->where);
1258 /* Do the checks of the actual argument list that are specific to elemental
1259 procedures. If called with c == NULL, we have a function, otherwise if
1260 expr == NULL, we have a subroutine. */
1263 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1265 gfc_actual_arglist *arg0;
1266 gfc_actual_arglist *arg;
1267 gfc_symbol *esym = NULL;
1268 gfc_intrinsic_sym *isym = NULL;
1270 gfc_intrinsic_arg *iformal = NULL;
1271 gfc_formal_arglist *eformal = NULL;
1272 bool formal_optional = false;
1273 bool set_by_optional = false;
1277 /* Is this an elemental procedure? */
1278 if (expr && expr->value.function.actual != NULL)
1280 if (expr->value.function.esym != NULL
1281 && expr->value.function.esym->attr.elemental)
1283 arg0 = expr->value.function.actual;
1284 esym = expr->value.function.esym;
1286 else if (expr->value.function.isym != NULL
1287 && expr->value.function.isym->elemental)
1289 arg0 = expr->value.function.actual;
1290 isym = expr->value.function.isym;
1295 else if (c && c->ext.actual != NULL && c->symtree->n.sym->attr.elemental)
1297 arg0 = c->ext.actual;
1298 esym = c->symtree->n.sym;
1303 /* The rank of an elemental is the rank of its array argument(s). */
1304 for (arg = arg0; arg; arg = arg->next)
1306 if (arg->expr != NULL && arg->expr->rank > 0)
1308 rank = arg->expr->rank;
1309 if (arg->expr->expr_type == EXPR_VARIABLE
1310 && arg->expr->symtree->n.sym->attr.optional)
1311 set_by_optional = true;
1313 /* Function specific; set the result rank and shape. */
1317 if (!expr->shape && arg->expr->shape)
1319 expr->shape = gfc_get_shape (rank);
1320 for (i = 0; i < rank; i++)
1321 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1328 /* If it is an array, it shall not be supplied as an actual argument
1329 to an elemental procedure unless an array of the same rank is supplied
1330 as an actual argument corresponding to a nonoptional dummy argument of
1331 that elemental procedure(12.4.1.5). */
1332 formal_optional = false;
1334 iformal = isym->formal;
1336 eformal = esym->formal;
1338 for (arg = arg0; arg; arg = arg->next)
1342 if (eformal->sym && eformal->sym->attr.optional)
1343 formal_optional = true;
1344 eformal = eformal->next;
1346 else if (isym && iformal)
1348 if (iformal->optional)
1349 formal_optional = true;
1350 iformal = iformal->next;
1353 formal_optional = true;
1355 if (pedantic && arg->expr != NULL
1356 && arg->expr->expr_type == EXPR_VARIABLE
1357 && arg->expr->symtree->n.sym->attr.optional
1360 && (set_by_optional || arg->expr->rank != rank)
1361 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1363 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1364 "MISSING, it cannot be the actual argument of an "
1365 "ELEMENTAL procedure unless there is a non-optional "
1366 "argument with the same rank (12.4.1.5)",
1367 arg->expr->symtree->n.sym->name, &arg->expr->where);
1372 for (arg = arg0; arg; arg = arg->next)
1374 if (arg->expr == NULL || arg->expr->rank == 0)
1377 /* Being elemental, the last upper bound of an assumed size array
1378 argument must be present. */
1379 if (resolve_assumed_size_actual (arg->expr))
1382 /* Elemental procedure's array actual arguments must conform. */
1385 if (gfc_check_conformance ("elemental procedure", arg->expr, e)
1393 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1394 is an array, the intent inout/out variable needs to be also an array. */
1395 if (rank > 0 && esym && expr == NULL)
1396 for (eformal = esym->formal, arg = arg0; arg && eformal;
1397 arg = arg->next, eformal = eformal->next)
1398 if ((eformal->sym->attr.intent == INTENT_OUT
1399 || eformal->sym->attr.intent == INTENT_INOUT)
1400 && arg->expr && arg->expr->rank == 0)
1402 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1403 "ELEMENTAL subroutine '%s' is a scalar, but another "
1404 "actual argument is an array", &arg->expr->where,
1405 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1406 : "INOUT", eformal->sym->name, esym->name);
1413 /* Go through each actual argument in ACTUAL and see if it can be
1414 implemented as an inlined, non-copying intrinsic. FNSYM is the
1415 function being called, or NULL if not known. */
1418 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1420 gfc_actual_arglist *ap;
1423 for (ap = actual; ap; ap = ap->next)
1425 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1426 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual))
1427 ap->expr->inline_noncopying_intrinsic = 1;
1431 /* This function does the checking of references to global procedures
1432 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1433 77 and 95 standards. It checks for a gsymbol for the name, making
1434 one if it does not already exist. If it already exists, then the
1435 reference being resolved must correspond to the type of gsymbol.
1436 Otherwise, the new symbol is equipped with the attributes of the
1437 reference. The corresponding code that is called in creating
1438 global entities is parse.c. */
1441 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
1446 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1448 gsym = gfc_get_gsymbol (sym->name);
1450 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1451 gfc_global_used (gsym, where);
1453 if (gsym->type == GSYM_UNKNOWN)
1456 gsym->where = *where;
1463 /************* Function resolution *************/
1465 /* Resolve a function call known to be generic.
1466 Section 14.1.2.4.1. */
1469 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1473 if (sym->attr.generic)
1475 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1478 expr->value.function.name = s->name;
1479 expr->value.function.esym = s;
1481 if (s->ts.type != BT_UNKNOWN)
1483 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1484 expr->ts = s->result->ts;
1487 expr->rank = s->as->rank;
1488 else if (s->result != NULL && s->result->as != NULL)
1489 expr->rank = s->result->as->rank;
1491 gfc_set_sym_referenced (expr->value.function.esym);
1496 /* TODO: Need to search for elemental references in generic
1500 if (sym->attr.intrinsic)
1501 return gfc_intrinsic_func_interface (expr, 0);
1508 resolve_generic_f (gfc_expr *expr)
1513 sym = expr->symtree->n.sym;
1517 m = resolve_generic_f0 (expr, sym);
1520 else if (m == MATCH_ERROR)
1524 if (sym->ns->parent == NULL)
1526 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1530 if (!generic_sym (sym))
1534 /* Last ditch attempt. See if the reference is to an intrinsic
1535 that possesses a matching interface. 14.1.2.4 */
1536 if (sym && !gfc_intrinsic_name (sym->name, 0))
1538 gfc_error ("There is no specific function for the generic '%s' at %L",
1539 expr->symtree->n.sym->name, &expr->where);
1543 m = gfc_intrinsic_func_interface (expr, 0);
1547 gfc_error ("Generic function '%s' at %L is not consistent with a "
1548 "specific intrinsic interface", expr->symtree->n.sym->name,
1555 /* Resolve a function call known to be specific. */
1558 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
1562 /* See if we have an intrinsic interface. */
1564 if (sym->interface != NULL && sym->interface->attr.intrinsic)
1566 gfc_intrinsic_sym *isym;
1567 isym = gfc_find_function (sym->interface->name);
1569 /* Existance of isym should be checked already. */
1573 sym->attr.function = 1;
1574 sym->attr.proc = PROC_EXTERNAL;
1578 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1580 if (sym->attr.dummy)
1582 sym->attr.proc = PROC_DUMMY;
1586 sym->attr.proc = PROC_EXTERNAL;
1590 if (sym->attr.proc == PROC_MODULE
1591 || sym->attr.proc == PROC_ST_FUNCTION
1592 || sym->attr.proc == PROC_INTERNAL)
1595 if (sym->attr.intrinsic)
1597 m = gfc_intrinsic_func_interface (expr, 1);
1601 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
1602 "with an intrinsic", sym->name, &expr->where);
1610 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1613 expr->value.function.name = sym->name;
1614 expr->value.function.esym = sym;
1615 if (sym->as != NULL)
1616 expr->rank = sym->as->rank;
1623 resolve_specific_f (gfc_expr *expr)
1628 sym = expr->symtree->n.sym;
1632 m = resolve_specific_f0 (sym, expr);
1635 if (m == MATCH_ERROR)
1638 if (sym->ns->parent == NULL)
1641 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1647 gfc_error ("Unable to resolve the specific function '%s' at %L",
1648 expr->symtree->n.sym->name, &expr->where);
1654 /* Resolve a procedure call not known to be generic nor specific. */
1657 resolve_unknown_f (gfc_expr *expr)
1662 sym = expr->symtree->n.sym;
1664 if (sym->attr.dummy)
1666 sym->attr.proc = PROC_DUMMY;
1667 expr->value.function.name = sym->name;
1671 /* See if we have an intrinsic function reference. */
1673 if (gfc_intrinsic_name (sym->name, 0))
1675 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
1680 /* The reference is to an external name. */
1682 sym->attr.proc = PROC_EXTERNAL;
1683 expr->value.function.name = sym->name;
1684 expr->value.function.esym = expr->symtree->n.sym;
1686 if (sym->as != NULL)
1687 expr->rank = sym->as->rank;
1689 /* Type of the expression is either the type of the symbol or the
1690 default type of the symbol. */
1693 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1695 if (sym->ts.type != BT_UNKNOWN)
1699 ts = gfc_get_default_type (sym, sym->ns);
1701 if (ts->type == BT_UNKNOWN)
1703 gfc_error ("Function '%s' at %L has no IMPLICIT type",
1704 sym->name, &expr->where);
1715 /* Return true, if the symbol is an external procedure. */
1717 is_external_proc (gfc_symbol *sym)
1719 if (!sym->attr.dummy && !sym->attr.contained
1720 && !(sym->attr.intrinsic
1721 || gfc_intrinsic_name (sym->name, sym->attr.subroutine))
1722 && sym->attr.proc != PROC_ST_FUNCTION
1723 && !sym->attr.use_assoc
1731 /* Figure out if a function reference is pure or not. Also set the name
1732 of the function for a potential error message. Return nonzero if the
1733 function is PURE, zero if not. */
1735 pure_stmt_function (gfc_expr *, gfc_symbol *);
1738 pure_function (gfc_expr *e, const char **name)
1744 if (e->symtree != NULL
1745 && e->symtree->n.sym != NULL
1746 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
1747 return pure_stmt_function (e, e->symtree->n.sym);
1749 if (e->value.function.esym)
1751 pure = gfc_pure (e->value.function.esym);
1752 *name = e->value.function.esym->name;
1754 else if (e->value.function.isym)
1756 pure = e->value.function.isym->pure
1757 || e->value.function.isym->elemental;
1758 *name = e->value.function.isym->name;
1762 /* Implicit functions are not pure. */
1764 *name = e->value.function.name;
1772 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
1773 int *f ATTRIBUTE_UNUSED)
1777 /* Don't bother recursing into other statement functions
1778 since they will be checked individually for purity. */
1779 if (e->expr_type != EXPR_FUNCTION
1781 || e->symtree->n.sym == sym
1782 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
1785 return pure_function (e, &name) ? false : true;
1790 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
1792 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
1797 is_scalar_expr_ptr (gfc_expr *expr)
1799 try retval = SUCCESS;
1804 /* See if we have a gfc_ref, which means we have a substring, array
1805 reference, or a component. */
1806 if (expr->ref != NULL)
1809 while (ref->next != NULL)
1815 if (ref->u.ss.length != NULL
1816 && ref->u.ss.length->length != NULL
1818 && ref->u.ss.start->expr_type == EXPR_CONSTANT
1820 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
1822 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
1823 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
1824 if (end - start + 1 != 1)
1831 if (ref->u.ar.type == AR_ELEMENT)
1833 else if (ref->u.ar.type == AR_FULL)
1835 /* The user can give a full array if the array is of size 1. */
1836 if (ref->u.ar.as != NULL
1837 && ref->u.ar.as->rank == 1
1838 && ref->u.ar.as->type == AS_EXPLICIT
1839 && ref->u.ar.as->lower[0] != NULL
1840 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
1841 && ref->u.ar.as->upper[0] != NULL
1842 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
1844 /* If we have a character string, we need to check if
1845 its length is one. */
1846 if (expr->ts.type == BT_CHARACTER)
1848 if (expr->ts.cl == NULL
1849 || expr->ts.cl->length == NULL
1850 || mpz_cmp_si (expr->ts.cl->length->value.integer, 1)
1856 /* We have constant lower and upper bounds. If the
1857 difference between is 1, it can be considered a
1859 start = (int) mpz_get_si
1860 (ref->u.ar.as->lower[0]->value.integer);
1861 end = (int) mpz_get_si
1862 (ref->u.ar.as->upper[0]->value.integer);
1863 if (end - start + 1 != 1)
1878 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
1880 /* Character string. Make sure it's of length 1. */
1881 if (expr->ts.cl == NULL
1882 || expr->ts.cl->length == NULL
1883 || mpz_cmp_si (expr->ts.cl->length->value.integer, 1) != 0)
1886 else if (expr->rank != 0)
1893 /* Match one of the iso_c_binding functions (c_associated or c_loc)
1894 and, in the case of c_associated, set the binding label based on
1898 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
1899 gfc_symbol **new_sym)
1901 char name[GFC_MAX_SYMBOL_LEN + 1];
1902 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
1903 int optional_arg = 0;
1904 try retval = SUCCESS;
1905 gfc_symbol *args_sym;
1906 gfc_typespec *arg_ts;
1907 gfc_ref *parent_ref;
1910 if (args->expr->expr_type == EXPR_CONSTANT
1911 || args->expr->expr_type == EXPR_OP
1912 || args->expr->expr_type == EXPR_NULL)
1914 gfc_error ("Argument to '%s' at %L is not a variable",
1915 sym->name, &(args->expr->where));
1919 args_sym = args->expr->symtree->n.sym;
1921 /* The typespec for the actual arg should be that stored in the expr
1922 and not necessarily that of the expr symbol (args_sym), because
1923 the actual expression could be a part-ref of the expr symbol. */
1924 arg_ts = &(args->expr->ts);
1926 /* Get the parent reference (if any) for the expression. This happens for
1927 cases such as a%b%c. */
1928 parent_ref = args->expr->ref;
1930 if (parent_ref != NULL)
1932 curr_ref = parent_ref->next;
1933 while (curr_ref != NULL && curr_ref->next != NULL)
1935 parent_ref = curr_ref;
1936 curr_ref = curr_ref->next;
1940 /* If curr_ref is non-NULL, we had a part-ref expression. If the curr_ref
1941 is for a REF_COMPONENT, then we need to use it as the parent_ref for
1942 the name, etc. Otherwise, the current parent_ref should be correct. */
1943 if (curr_ref != NULL && curr_ref->type == REF_COMPONENT)
1944 parent_ref = curr_ref;
1946 if (parent_ref == args->expr->ref)
1948 else if (parent_ref != NULL && parent_ref->type != REF_COMPONENT)
1949 gfc_internal_error ("Unexpected expression reference type in "
1950 "gfc_iso_c_func_interface");
1952 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
1954 /* If the user gave two args then they are providing something for
1955 the optional arg (the second cptr). Therefore, set the name and
1956 binding label to the c_associated for two cptrs. Otherwise,
1957 set c_associated to expect one cptr. */
1961 sprintf (name, "%s_2", sym->name);
1962 sprintf (binding_label, "%s_2", sym->binding_label);
1968 sprintf (name, "%s_1", sym->name);
1969 sprintf (binding_label, "%s_1", sym->binding_label);
1973 /* Get a new symbol for the version of c_associated that
1975 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
1977 else if (sym->intmod_sym_id == ISOCBINDING_LOC
1978 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
1980 sprintf (name, "%s", sym->name);
1981 sprintf (binding_label, "%s", sym->binding_label);
1983 /* Error check the call. */
1984 if (args->next != NULL)
1986 gfc_error_now ("More actual than formal arguments in '%s' "
1987 "call at %L", name, &(args->expr->where));
1990 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
1992 /* Make sure we have either the target or pointer attribute. */
1993 if (!(args_sym->attr.target)
1994 && !(args_sym->attr.pointer)
1995 && (parent_ref == NULL ||
1996 !parent_ref->u.c.component->pointer))
1998 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
1999 "a TARGET or an associated pointer",
2001 sym->name, &(args->expr->where));
2005 /* See if we have interoperable type and type param. */
2006 if (verify_c_interop (arg_ts,
2007 (parent_ref ? parent_ref->u.c.component->name
2009 &(args->expr->where)) == SUCCESS
2010 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2012 if (args_sym->attr.target == 1)
2014 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2015 has the target attribute and is interoperable. */
2016 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2017 allocatable variable that has the TARGET attribute and
2018 is not an array of zero size. */
2019 if (args_sym->attr.allocatable == 1)
2021 if (args_sym->attr.dimension != 0
2022 && (args_sym->as && args_sym->as->rank == 0))
2024 gfc_error_now ("Allocatable variable '%s' used as a "
2025 "parameter to '%s' at %L must not be "
2026 "an array of zero size",
2027 args_sym->name, sym->name,
2028 &(args->expr->where));
2034 /* A non-allocatable target variable with C
2035 interoperable type and type parameters must be
2037 if (args_sym && args_sym->attr.dimension)
2039 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2041 gfc_error ("Assumed-shape array '%s' at %L "
2042 "cannot be an argument to the "
2043 "procedure '%s' because "
2044 "it is not C interoperable",
2046 &(args->expr->where), sym->name);
2049 else if (args_sym->as->type == AS_DEFERRED)
2051 gfc_error ("Deferred-shape array '%s' at %L "
2052 "cannot be an argument to the "
2053 "procedure '%s' because "
2054 "it is not C interoperable",
2056 &(args->expr->where), sym->name);
2061 /* Make sure it's not a character string. Arrays of
2062 any type should be ok if the variable is of a C
2063 interoperable type. */
2064 if (arg_ts->type == BT_CHARACTER)
2065 if (arg_ts->cl != NULL
2066 && (arg_ts->cl->length == NULL
2067 || arg_ts->cl->length->expr_type
2070 (arg_ts->cl->length->value.integer, 1)
2072 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2074 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2075 "at %L must have a length of 1",
2076 args_sym->name, sym->name,
2077 &(args->expr->where));
2082 else if ((args_sym->attr.pointer == 1 ||
2084 && parent_ref->u.c.component->pointer))
2085 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2087 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2089 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2090 "associated scalar POINTER", args_sym->name,
2091 sym->name, &(args->expr->where));
2097 /* The parameter is not required to be C interoperable. If it
2098 is not C interoperable, it must be a nonpolymorphic scalar
2099 with no length type parameters. It still must have either
2100 the pointer or target attribute, and it can be
2101 allocatable (but must be allocated when c_loc is called). */
2102 if (args->expr->rank != 0
2103 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2105 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2106 "scalar", args_sym->name, sym->name,
2107 &(args->expr->where));
2110 else if (arg_ts->type == BT_CHARACTER
2111 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2113 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2114 "%L must have a length of 1",
2115 args_sym->name, sym->name,
2116 &(args->expr->where));
2121 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2123 if (args_sym->attr.flavor != FL_PROCEDURE)
2125 /* TODO: Update this error message to allow for procedure
2126 pointers once they are implemented. */
2127 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2129 args_sym->name, sym->name,
2130 &(args->expr->where));
2133 else if (args_sym->attr.is_bind_c != 1)
2135 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2137 args_sym->name, sym->name,
2138 &(args->expr->where));
2143 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2148 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2149 "iso_c_binding function: '%s'!\n", sym->name);
2156 /* Resolve a function call, which means resolving the arguments, then figuring
2157 out which entity the name refers to. */
2158 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2159 to INTENT(OUT) or INTENT(INOUT). */
2162 resolve_function (gfc_expr *expr)
2164 gfc_actual_arglist *arg;
2169 procedure_type p = PROC_INTRINSIC;
2173 sym = expr->symtree->n.sym;
2175 if (sym && sym->attr.flavor == FL_VARIABLE)
2177 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2181 if (sym && sym->attr.abstract)
2183 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2184 sym->name, &expr->where);
2188 /* If the procedure is external, check for usage. */
2189 if (sym && is_external_proc (sym))
2190 resolve_global_procedure (sym, &expr->where, 0);
2192 /* Switch off assumed size checking and do this again for certain kinds
2193 of procedure, once the procedure itself is resolved. */
2194 need_full_assumed_size++;
2196 if (expr->symtree && expr->symtree->n.sym)
2197 p = expr->symtree->n.sym->attr.proc;
2199 if (resolve_actual_arglist (expr->value.function.actual, p) == FAILURE)
2202 /* Need to setup the call to the correct c_associated, depending on
2203 the number of cptrs to user gives to compare. */
2204 if (sym && sym->attr.is_iso_c == 1)
2206 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2210 /* Get the symtree for the new symbol (resolved func).
2211 the old one will be freed later, when it's no longer used. */
2212 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2215 /* Resume assumed_size checking. */
2216 need_full_assumed_size--;
2218 if (sym && sym->ts.type == BT_CHARACTER
2220 && sym->ts.cl->length == NULL
2222 && expr->value.function.esym == NULL
2223 && !sym->attr.contained)
2225 /* Internal procedures are taken care of in resolve_contained_fntype. */
2226 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2227 "be used at %L since it is not a dummy argument",
2228 sym->name, &expr->where);
2232 /* See if function is already resolved. */
2234 if (expr->value.function.name != NULL)
2236 if (expr->ts.type == BT_UNKNOWN)
2242 /* Apply the rules of section 14.1.2. */
2244 switch (procedure_kind (sym))
2247 t = resolve_generic_f (expr);
2250 case PTYPE_SPECIFIC:
2251 t = resolve_specific_f (expr);
2255 t = resolve_unknown_f (expr);
2259 gfc_internal_error ("resolve_function(): bad function type");
2263 /* If the expression is still a function (it might have simplified),
2264 then we check to see if we are calling an elemental function. */
2266 if (expr->expr_type != EXPR_FUNCTION)
2269 temp = need_full_assumed_size;
2270 need_full_assumed_size = 0;
2272 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2275 if (omp_workshare_flag
2276 && expr->value.function.esym
2277 && ! gfc_elemental (expr->value.function.esym))
2279 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2280 "in WORKSHARE construct", expr->value.function.esym->name,
2285 #define GENERIC_ID expr->value.function.isym->id
2286 else if (expr->value.function.actual != NULL
2287 && expr->value.function.isym != NULL
2288 && GENERIC_ID != GFC_ISYM_LBOUND
2289 && GENERIC_ID != GFC_ISYM_LEN
2290 && GENERIC_ID != GFC_ISYM_LOC
2291 && GENERIC_ID != GFC_ISYM_PRESENT)
2293 /* Array intrinsics must also have the last upper bound of an
2294 assumed size array argument. UBOUND and SIZE have to be
2295 excluded from the check if the second argument is anything
2298 inquiry = GENERIC_ID == GFC_ISYM_UBOUND
2299 || GENERIC_ID == GFC_ISYM_SIZE;
2301 for (arg = expr->value.function.actual; arg; arg = arg->next)
2303 if (inquiry && arg->next != NULL && arg->next->expr)
2305 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2308 if ((int)mpz_get_si (arg->next->expr->value.integer)
2313 if (arg->expr != NULL
2314 && arg->expr->rank > 0
2315 && resolve_assumed_size_actual (arg->expr))
2321 need_full_assumed_size = temp;
2324 if (!pure_function (expr, &name) && name)
2328 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2329 "FORALL %s", name, &expr->where,
2330 forall_flag == 2 ? "mask" : "block");
2333 else if (gfc_pure (NULL))
2335 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2336 "procedure within a PURE procedure", name, &expr->where);
2341 /* Functions without the RECURSIVE attribution are not allowed to
2342 * call themselves. */
2343 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2345 gfc_symbol *esym, *proc;
2346 esym = expr->value.function.esym;
2347 proc = gfc_current_ns->proc_name;
2350 gfc_error ("Function '%s' at %L cannot call itself, as it is not "
2351 "RECURSIVE", name, &expr->where);
2355 if (esym->attr.entry && esym->ns->entries && proc->ns->entries
2356 && esym->ns->entries->sym == proc->ns->entries->sym)
2358 gfc_error ("Call to ENTRY '%s' at %L is recursive, but function "
2359 "'%s' is not declared as RECURSIVE",
2360 esym->name, &expr->where, esym->ns->entries->sym->name);
2365 /* Character lengths of use associated functions may contains references to
2366 symbols not referenced from the current program unit otherwise. Make sure
2367 those symbols are marked as referenced. */
2369 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2370 && expr->value.function.esym->attr.use_assoc)
2372 gfc_expr_set_symbols_referenced (expr->ts.cl->length);
2376 find_noncopying_intrinsics (expr->value.function.esym,
2377 expr->value.function.actual);
2379 /* Make sure that the expression has a typespec that works. */
2380 if (expr->ts.type == BT_UNKNOWN)
2382 if (expr->symtree->n.sym->result
2383 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN)
2384 expr->ts = expr->symtree->n.sym->result->ts;
2391 /************* Subroutine resolution *************/
2394 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2400 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2401 sym->name, &c->loc);
2402 else if (gfc_pure (NULL))
2403 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2409 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2413 if (sym->attr.generic)
2415 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2418 c->resolved_sym = s;
2419 pure_subroutine (c, s);
2423 /* TODO: Need to search for elemental references in generic interface. */
2426 if (sym->attr.intrinsic)
2427 return gfc_intrinsic_sub_interface (c, 0);
2434 resolve_generic_s (gfc_code *c)
2439 sym = c->symtree->n.sym;
2443 m = resolve_generic_s0 (c, sym);
2446 else if (m == MATCH_ERROR)
2450 if (sym->ns->parent == NULL)
2452 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2456 if (!generic_sym (sym))
2460 /* Last ditch attempt. See if the reference is to an intrinsic
2461 that possesses a matching interface. 14.1.2.4 */
2462 sym = c->symtree->n.sym;
2464 if (!gfc_intrinsic_name (sym->name, 1))
2466 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2467 sym->name, &c->loc);
2471 m = gfc_intrinsic_sub_interface (c, 0);
2475 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2476 "intrinsic subroutine interface", sym->name, &c->loc);
2482 /* Set the name and binding label of the subroutine symbol in the call
2483 expression represented by 'c' to include the type and kind of the
2484 second parameter. This function is for resolving the appropriate
2485 version of c_f_pointer() and c_f_procpointer(). For example, a
2486 call to c_f_pointer() for a default integer pointer could have a
2487 name of c_f_pointer_i4. If no second arg exists, which is an error
2488 for these two functions, it defaults to the generic symbol's name
2489 and binding label. */
2492 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2493 char *name, char *binding_label)
2495 gfc_expr *arg = NULL;
2499 /* The second arg of c_f_pointer and c_f_procpointer determines
2500 the type and kind for the procedure name. */
2501 arg = c->ext.actual->next->expr;
2505 /* Set up the name to have the given symbol's name,
2506 plus the type and kind. */
2507 /* a derived type is marked with the type letter 'u' */
2508 if (arg->ts.type == BT_DERIVED)
2511 kind = 0; /* set the kind as 0 for now */
2515 type = gfc_type_letter (arg->ts.type);
2516 kind = arg->ts.kind;
2519 if (arg->ts.type == BT_CHARACTER)
2520 /* Kind info for character strings not needed. */
2523 sprintf (name, "%s_%c%d", sym->name, type, kind);
2524 /* Set up the binding label as the given symbol's label plus
2525 the type and kind. */
2526 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2530 /* If the second arg is missing, set the name and label as
2531 was, cause it should at least be found, and the missing
2532 arg error will be caught by compare_parameters(). */
2533 sprintf (name, "%s", sym->name);
2534 sprintf (binding_label, "%s", sym->binding_label);
2541 /* Resolve a generic version of the iso_c_binding procedure given
2542 (sym) to the specific one based on the type and kind of the
2543 argument(s). Currently, this function resolves c_f_pointer() and
2544 c_f_procpointer based on the type and kind of the second argument
2545 (FPTR). Other iso_c_binding procedures aren't specially handled.
2546 Upon successfully exiting, c->resolved_sym will hold the resolved
2547 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2551 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2553 gfc_symbol *new_sym;
2554 /* this is fine, since we know the names won't use the max */
2555 char name[GFC_MAX_SYMBOL_LEN + 1];
2556 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2557 /* default to success; will override if find error */
2558 match m = MATCH_YES;
2560 /* Make sure the actual arguments are in the necessary order (based on the
2561 formal args) before resolving. */
2562 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2564 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2565 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2567 set_name_and_label (c, sym, name, binding_label);
2569 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2571 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2573 /* Make sure we got a third arg if the second arg has non-zero
2574 rank. We must also check that the type and rank are
2575 correct since we short-circuit this check in
2576 gfc_procedure_use() (called above to sort actual args). */
2577 if (c->ext.actual->next->expr->rank != 0)
2579 if(c->ext.actual->next->next == NULL
2580 || c->ext.actual->next->next->expr == NULL)
2583 gfc_error ("Missing SHAPE parameter for call to %s "
2584 "at %L", sym->name, &(c->loc));
2586 else if (c->ext.actual->next->next->expr->ts.type
2588 || c->ext.actual->next->next->expr->rank != 1)
2591 gfc_error ("SHAPE parameter for call to %s at %L must "
2592 "be a rank 1 INTEGER array", sym->name,
2599 if (m != MATCH_ERROR)
2601 /* the 1 means to add the optional arg to formal list */
2602 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
2604 /* for error reporting, say it's declared where the original was */
2605 new_sym->declared_at = sym->declared_at;
2610 /* no differences for c_loc or c_funloc */
2614 /* set the resolved symbol */
2615 if (m != MATCH_ERROR)
2616 c->resolved_sym = new_sym;
2618 c->resolved_sym = sym;
2624 /* Resolve a subroutine call known to be specific. */
2627 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
2631 /* See if we have an intrinsic interface. */
2632 if (sym->interface != NULL && !sym->interface->attr.abstract
2633 && !sym->interface->attr.subroutine)
2635 gfc_intrinsic_sym *isym;
2637 isym = gfc_find_function (sym->interface->name);
2639 /* Existance of isym should be checked already. */
2643 sym->attr.function = 1;
2647 if(sym->attr.is_iso_c)
2649 m = gfc_iso_c_sub_interface (c,sym);
2653 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2655 if (sym->attr.dummy)
2657 sym->attr.proc = PROC_DUMMY;
2661 sym->attr.proc = PROC_EXTERNAL;
2665 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
2668 if (sym->attr.intrinsic)
2670 m = gfc_intrinsic_sub_interface (c, 1);
2674 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
2675 "with an intrinsic", sym->name, &c->loc);
2683 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2685 c->resolved_sym = sym;
2686 pure_subroutine (c, sym);
2693 resolve_specific_s (gfc_code *c)
2698 sym = c->symtree->n.sym;
2702 m = resolve_specific_s0 (c, sym);
2705 if (m == MATCH_ERROR)
2708 if (sym->ns->parent == NULL)
2711 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2717 sym = c->symtree->n.sym;
2718 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
2719 sym->name, &c->loc);
2725 /* Resolve a subroutine call not known to be generic nor specific. */
2728 resolve_unknown_s (gfc_code *c)
2732 sym = c->symtree->n.sym;
2734 if (sym->attr.dummy)
2736 sym->attr.proc = PROC_DUMMY;
2740 /* See if we have an intrinsic function reference. */
2742 if (gfc_intrinsic_name (sym->name, 1))
2744 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
2749 /* The reference is to an external name. */
2752 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2754 c->resolved_sym = sym;
2756 pure_subroutine (c, sym);
2762 /* Resolve a subroutine call. Although it was tempting to use the same code
2763 for functions, subroutines and functions are stored differently and this
2764 makes things awkward. */
2767 resolve_call (gfc_code *c)
2770 procedure_type ptype = PROC_INTRINSIC;
2772 if (c->symtree && c->symtree->n.sym
2773 && c->symtree->n.sym->ts.type != BT_UNKNOWN)
2775 gfc_error ("'%s' at %L has a type, which is not consistent with "
2776 "the CALL at %L", c->symtree->n.sym->name,
2777 &c->symtree->n.sym->declared_at, &c->loc);
2781 /* If external, check for usage. */
2782 if (c->symtree && is_external_proc (c->symtree->n.sym))
2783 resolve_global_procedure (c->symtree->n.sym, &c->loc, 1);
2785 /* Subroutines without the RECURSIVE attribution are not allowed to
2786 * call themselves. */
2787 if (c->symtree && c->symtree->n.sym && !c->symtree->n.sym->attr.recursive)
2789 gfc_symbol *csym, *proc;
2790 csym = c->symtree->n.sym;
2791 proc = gfc_current_ns->proc_name;
2794 gfc_error ("SUBROUTINE '%s' at %L cannot call itself, as it is not "
2795 "RECURSIVE", csym->name, &c->loc);
2799 if (csym->attr.entry && csym->ns->entries && proc->ns->entries
2800 && csym->ns->entries->sym == proc->ns->entries->sym)
2802 gfc_error ("Call to ENTRY '%s' at %L is recursive, but subroutine "
2803 "'%s' is not declared as RECURSIVE",
2804 csym->name, &c->loc, csym->ns->entries->sym->name);
2809 /* Switch off assumed size checking and do this again for certain kinds
2810 of procedure, once the procedure itself is resolved. */
2811 need_full_assumed_size++;
2813 if (c->symtree && c->symtree->n.sym)
2814 ptype = c->symtree->n.sym->attr.proc;
2816 if (resolve_actual_arglist (c->ext.actual, ptype) == FAILURE)
2819 /* Resume assumed_size checking. */
2820 need_full_assumed_size--;
2823 if (c->resolved_sym == NULL)
2824 switch (procedure_kind (c->symtree->n.sym))
2827 t = resolve_generic_s (c);
2830 case PTYPE_SPECIFIC:
2831 t = resolve_specific_s (c);
2835 t = resolve_unknown_s (c);
2839 gfc_internal_error ("resolve_subroutine(): bad function type");
2842 /* Some checks of elemental subroutine actual arguments. */
2843 if (resolve_elemental_actual (NULL, c) == FAILURE)
2847 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
2852 /* Compare the shapes of two arrays that have non-NULL shapes. If both
2853 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
2854 match. If both op1->shape and op2->shape are non-NULL return FAILURE
2855 if their shapes do not match. If either op1->shape or op2->shape is
2856 NULL, return SUCCESS. */
2859 compare_shapes (gfc_expr *op1, gfc_expr *op2)
2866 if (op1->shape != NULL && op2->shape != NULL)
2868 for (i = 0; i < op1->rank; i++)
2870 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
2872 gfc_error ("Shapes for operands at %L and %L are not conformable",
2873 &op1->where, &op2->where);
2884 /* Resolve an operator expression node. This can involve replacing the
2885 operation with a user defined function call. */
2888 resolve_operator (gfc_expr *e)
2890 gfc_expr *op1, *op2;
2892 bool dual_locus_error;
2895 /* Resolve all subnodes-- give them types. */
2897 switch (e->value.op.operator)
2900 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
2903 /* Fall through... */
2906 case INTRINSIC_UPLUS:
2907 case INTRINSIC_UMINUS:
2908 case INTRINSIC_PARENTHESES:
2909 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
2914 /* Typecheck the new node. */
2916 op1 = e->value.op.op1;
2917 op2 = e->value.op.op2;
2918 dual_locus_error = false;
2920 if ((op1 && op1->expr_type == EXPR_NULL)
2921 || (op2 && op2->expr_type == EXPR_NULL))
2923 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
2927 switch (e->value.op.operator)
2929 case INTRINSIC_UPLUS:
2930 case INTRINSIC_UMINUS:
2931 if (op1->ts.type == BT_INTEGER
2932 || op1->ts.type == BT_REAL
2933 || op1->ts.type == BT_COMPLEX)
2939 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
2940 gfc_op2string (e->value.op.operator), gfc_typename (&e->ts));
2943 case INTRINSIC_PLUS:
2944 case INTRINSIC_MINUS:
2945 case INTRINSIC_TIMES:
2946 case INTRINSIC_DIVIDE:
2947 case INTRINSIC_POWER:
2948 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
2950 gfc_type_convert_binary (e);
2955 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
2956 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2957 gfc_typename (&op2->ts));
2960 case INTRINSIC_CONCAT:
2961 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
2963 e->ts.type = BT_CHARACTER;
2964 e->ts.kind = op1->ts.kind;
2969 _("Operands of string concatenation operator at %%L are %s/%s"),
2970 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
2976 case INTRINSIC_NEQV:
2977 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
2979 e->ts.type = BT_LOGICAL;
2980 e->ts.kind = gfc_kind_max (op1, op2);
2981 if (op1->ts.kind < e->ts.kind)
2982 gfc_convert_type (op1, &e->ts, 2);
2983 else if (op2->ts.kind < e->ts.kind)
2984 gfc_convert_type (op2, &e->ts, 2);
2988 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
2989 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2990 gfc_typename (&op2->ts));
2995 if (op1->ts.type == BT_LOGICAL)
2997 e->ts.type = BT_LOGICAL;
2998 e->ts.kind = op1->ts.kind;
3002 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3003 gfc_typename (&op1->ts));
3007 case INTRINSIC_GT_OS:
3009 case INTRINSIC_GE_OS:
3011 case INTRINSIC_LT_OS:
3013 case INTRINSIC_LE_OS:
3014 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3016 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3020 /* Fall through... */
3023 case INTRINSIC_EQ_OS:
3025 case INTRINSIC_NE_OS:
3026 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
3028 e->ts.type = BT_LOGICAL;
3029 e->ts.kind = gfc_default_logical_kind;
3033 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3035 gfc_type_convert_binary (e);
3037 e->ts.type = BT_LOGICAL;
3038 e->ts.kind = gfc_default_logical_kind;
3042 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3044 _("Logicals at %%L must be compared with %s instead of %s"),
3045 (e->value.op.operator == INTRINSIC_EQ
3046 || e->value.op.operator == INTRINSIC_EQ_OS)
3047 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.operator));
3050 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3051 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
3052 gfc_typename (&op2->ts));
3056 case INTRINSIC_USER:
3057 if (e->value.op.uop->operator == NULL)
3058 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3059 else if (op2 == NULL)
3060 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3061 e->value.op.uop->name, gfc_typename (&op1->ts));
3063 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3064 e->value.op.uop->name, gfc_typename (&op1->ts),
3065 gfc_typename (&op2->ts));
3069 case INTRINSIC_PARENTHESES:
3071 if (e->ts.type == BT_CHARACTER)
3072 e->ts.cl = op1->ts.cl;
3076 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3079 /* Deal with arrayness of an operand through an operator. */
3083 switch (e->value.op.operator)
3085 case INTRINSIC_PLUS:
3086 case INTRINSIC_MINUS:
3087 case INTRINSIC_TIMES:
3088 case INTRINSIC_DIVIDE:
3089 case INTRINSIC_POWER:
3090 case INTRINSIC_CONCAT:
3094 case INTRINSIC_NEQV:
3096 case INTRINSIC_EQ_OS:
3098 case INTRINSIC_NE_OS:
3100 case INTRINSIC_GT_OS:
3102 case INTRINSIC_GE_OS:
3104 case INTRINSIC_LT_OS:
3106 case INTRINSIC_LE_OS:
3108 if (op1->rank == 0 && op2->rank == 0)
3111 if (op1->rank == 0 && op2->rank != 0)
3113 e->rank = op2->rank;
3115 if (e->shape == NULL)
3116 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3119 if (op1->rank != 0 && op2->rank == 0)
3121 e->rank = op1->rank;
3123 if (e->shape == NULL)
3124 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3127 if (op1->rank != 0 && op2->rank != 0)
3129 if (op1->rank == op2->rank)
3131 e->rank = op1->rank;
3132 if (e->shape == NULL)
3134 t = compare_shapes(op1, op2);
3138 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3143 /* Allow higher level expressions to work. */
3146 /* Try user-defined operators, and otherwise throw an error. */
3147 dual_locus_error = true;
3149 _("Inconsistent ranks for operator at %%L and %%L"));
3156 case INTRINSIC_PARENTHESES:
3158 case INTRINSIC_UPLUS:
3159 case INTRINSIC_UMINUS:
3160 /* Simply copy arrayness attribute */
3161 e->rank = op1->rank;
3163 if (e->shape == NULL)
3164 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3172 /* Attempt to simplify the expression. */
3175 t = gfc_simplify_expr (e, 0);
3176 /* Some calls do not succeed in simplification and return FAILURE
3177 even though there is no error; eg. variable references to
3178 PARAMETER arrays. */
3179 if (!gfc_is_constant_expr (e))
3186 if (gfc_extend_expr (e) == SUCCESS)
3189 if (dual_locus_error)
3190 gfc_error (msg, &op1->where, &op2->where);
3192 gfc_error (msg, &e->where);
3198 /************** Array resolution subroutines **************/
3201 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3204 /* Compare two integer expressions. */
3207 compare_bound (gfc_expr *a, gfc_expr *b)
3211 if (a == NULL || a->expr_type != EXPR_CONSTANT
3212 || b == NULL || b->expr_type != EXPR_CONSTANT)
3215 /* If either of the types isn't INTEGER, we must have
3216 raised an error earlier. */
3218 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3221 i = mpz_cmp (a->value.integer, b->value.integer);
3231 /* Compare an integer expression with an integer. */
3234 compare_bound_int (gfc_expr *a, int b)
3238 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3241 if (a->ts.type != BT_INTEGER)
3242 gfc_internal_error ("compare_bound_int(): Bad expression");
3244 i = mpz_cmp_si (a->value.integer, b);
3254 /* Compare an integer expression with a mpz_t. */
3257 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3261 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3264 if (a->ts.type != BT_INTEGER)
3265 gfc_internal_error ("compare_bound_int(): Bad expression");
3267 i = mpz_cmp (a->value.integer, b);
3277 /* Compute the last value of a sequence given by a triplet.
3278 Return 0 if it wasn't able to compute the last value, or if the
3279 sequence if empty, and 1 otherwise. */
3282 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3283 gfc_expr *stride, mpz_t last)
3287 if (start == NULL || start->expr_type != EXPR_CONSTANT
3288 || end == NULL || end->expr_type != EXPR_CONSTANT
3289 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3292 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3293 || (stride != NULL && stride->ts.type != BT_INTEGER))
3296 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3298 if (compare_bound (start, end) == CMP_GT)
3300 mpz_set (last, end->value.integer);
3304 if (compare_bound_int (stride, 0) == CMP_GT)
3306 /* Stride is positive */
3307 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3312 /* Stride is negative */
3313 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3318 mpz_sub (rem, end->value.integer, start->value.integer);
3319 mpz_tdiv_r (rem, rem, stride->value.integer);
3320 mpz_sub (last, end->value.integer, rem);
3327 /* Compare a single dimension of an array reference to the array
3331 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3335 /* Given start, end and stride values, calculate the minimum and
3336 maximum referenced indexes. */
3338 switch (ar->dimen_type[i])
3344 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3346 gfc_warning ("Array reference at %L is out of bounds "
3347 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3348 mpz_get_si (ar->start[i]->value.integer),
3349 mpz_get_si (as->lower[i]->value.integer), i+1);
3352 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3354 gfc_warning ("Array reference at %L is out of bounds "
3355 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3356 mpz_get_si (ar->start[i]->value.integer),
3357 mpz_get_si (as->upper[i]->value.integer), i+1);
3365 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3366 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3368 comparison comp_start_end = compare_bound (AR_START, AR_END);
3370 /* Check for zero stride, which is not allowed. */
3371 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3373 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3377 /* if start == len || (stride > 0 && start < len)
3378 || (stride < 0 && start > len),
3379 then the array section contains at least one element. In this
3380 case, there is an out-of-bounds access if
3381 (start < lower || start > upper). */
3382 if (compare_bound (AR_START, AR_END) == CMP_EQ
3383 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3384 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3385 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3386 && comp_start_end == CMP_GT))
3388 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3390 gfc_warning ("Lower array reference at %L is out of bounds "
3391 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3392 mpz_get_si (AR_START->value.integer),
3393 mpz_get_si (as->lower[i]->value.integer), i+1);
3396 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3398 gfc_warning ("Lower array reference at %L is out of bounds "
3399 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3400 mpz_get_si (AR_START->value.integer),
3401 mpz_get_si (as->upper[i]->value.integer), i+1);
3406 /* If we can compute the highest index of the array section,
3407 then it also has to be between lower and upper. */
3408 mpz_init (last_value);
3409 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3412 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3414 gfc_warning ("Upper array reference at %L is out of bounds "
3415 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3416 mpz_get_si (last_value),
3417 mpz_get_si (as->lower[i]->value.integer), i+1);
3418 mpz_clear (last_value);
3421 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3423 gfc_warning ("Upper array reference at %L is out of bounds "
3424 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3425 mpz_get_si (last_value),
3426 mpz_get_si (as->upper[i]->value.integer), i+1);
3427 mpz_clear (last_value);
3431 mpz_clear (last_value);
3439 gfc_internal_error ("check_dimension(): Bad array reference");
3446 /* Compare an array reference with an array specification. */
3449 compare_spec_to_ref (gfc_array_ref *ar)
3456 /* TODO: Full array sections are only allowed as actual parameters. */
3457 if (as->type == AS_ASSUMED_SIZE
3458 && (/*ar->type == AR_FULL
3459 ||*/ (ar->type == AR_SECTION
3460 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3462 gfc_error ("Rightmost upper bound of assumed size array section "
3463 "not specified at %L", &ar->where);
3467 if (ar->type == AR_FULL)
3470 if (as->rank != ar->dimen)
3472 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3473 &ar->where, ar->dimen, as->rank);
3477 for (i = 0; i < as->rank; i++)
3478 if (check_dimension (i, ar, as) == FAILURE)
3485 /* Resolve one part of an array index. */
3488 gfc_resolve_index (gfc_expr *index, int check_scalar)
3495 if (gfc_resolve_expr (index) == FAILURE)
3498 if (check_scalar && index->rank != 0)
3500 gfc_error ("Array index at %L must be scalar", &index->where);
3504 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
3506 gfc_error ("Array index at %L must be of INTEGER type",
3511 if (index->ts.type == BT_REAL)
3512 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
3513 &index->where) == FAILURE)
3516 if (index->ts.kind != gfc_index_integer_kind
3517 || index->ts.type != BT_INTEGER)
3520 ts.type = BT_INTEGER;
3521 ts.kind = gfc_index_integer_kind;
3523 gfc_convert_type_warn (index, &ts, 2, 0);
3529 /* Resolve a dim argument to an intrinsic function. */
3532 gfc_resolve_dim_arg (gfc_expr *dim)
3537 if (gfc_resolve_expr (dim) == FAILURE)
3542 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3547 if (dim->ts.type != BT_INTEGER)
3549 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3553 if (dim->ts.kind != gfc_index_integer_kind)
3557 ts.type = BT_INTEGER;
3558 ts.kind = gfc_index_integer_kind;
3560 gfc_convert_type_warn (dim, &ts, 2, 0);
3566 /* Given an expression that contains array references, update those array
3567 references to point to the right array specifications. While this is
3568 filled in during matching, this information is difficult to save and load
3569 in a module, so we take care of it here.
3571 The idea here is that the original array reference comes from the
3572 base symbol. We traverse the list of reference structures, setting
3573 the stored reference to references. Component references can
3574 provide an additional array specification. */
3577 find_array_spec (gfc_expr *e)
3581 gfc_symbol *derived;
3584 as = e->symtree->n.sym->as;
3587 for (ref = e->ref; ref; ref = ref->next)
3592 gfc_internal_error ("find_array_spec(): Missing spec");
3599 if (derived == NULL)
3600 derived = e->symtree->n.sym->ts.derived;
3602 c = derived->components;
3604 for (; c; c = c->next)
3605 if (c == ref->u.c.component)
3607 /* Track the sequence of component references. */
3608 if (c->ts.type == BT_DERIVED)
3609 derived = c->ts.derived;
3614 gfc_internal_error ("find_array_spec(): Component not found");
3619 gfc_internal_error ("find_array_spec(): unused as(1)");
3630 gfc_internal_error ("find_array_spec(): unused as(2)");
3634 /* Resolve an array reference. */
3637 resolve_array_ref (gfc_array_ref *ar)
3639 int i, check_scalar;
3642 for (i = 0; i < ar->dimen; i++)
3644 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
3646 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
3648 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
3650 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
3655 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
3659 ar->dimen_type[i] = DIMEN_ELEMENT;
3663 ar->dimen_type[i] = DIMEN_VECTOR;
3664 if (e->expr_type == EXPR_VARIABLE
3665 && e->symtree->n.sym->ts.type == BT_DERIVED)
3666 ar->start[i] = gfc_get_parentheses (e);
3670 gfc_error ("Array index at %L is an array of rank %d",
3671 &ar->c_where[i], e->rank);
3676 /* If the reference type is unknown, figure out what kind it is. */
3678 if (ar->type == AR_UNKNOWN)
3680 ar->type = AR_ELEMENT;
3681 for (i = 0; i < ar->dimen; i++)
3682 if (ar->dimen_type[i] == DIMEN_RANGE
3683 || ar->dimen_type[i] == DIMEN_VECTOR)
3685 ar->type = AR_SECTION;
3690 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
3698 resolve_substring (gfc_ref *ref)
3700 if (ref->u.ss.start != NULL)
3702 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
3705 if (ref->u.ss.start->ts.type != BT_INTEGER)
3707 gfc_error ("Substring start index at %L must be of type INTEGER",
3708 &ref->u.ss.start->where);
3712 if (ref->u.ss.start->rank != 0)
3714 gfc_error ("Substring start index at %L must be scalar",
3715 &ref->u.ss.start->where);
3719 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
3720 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
3721 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
3723 gfc_error ("Substring start index at %L is less than one",
3724 &ref->u.ss.start->where);
3729 if (ref->u.ss.end != NULL)
3731 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
3734 if (ref->u.ss.end->ts.type != BT_INTEGER)
3736 gfc_error ("Substring end index at %L must be of type INTEGER",
3737 &ref->u.ss.end->where);
3741 if (ref->u.ss.end->rank != 0)
3743 gfc_error ("Substring end index at %L must be scalar",
3744 &ref->u.ss.end->where);
3748 if (ref->u.ss.length != NULL
3749 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
3750 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
3751 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
3753 gfc_error ("Substring end index at %L exceeds the string length",
3754 &ref->u.ss.start->where);
3763 /* This function supplies missing substring charlens. */
3766 gfc_resolve_substring_charlen (gfc_expr *e)
3769 gfc_expr *start, *end;
3771 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
3772 if (char_ref->type == REF_SUBSTRING)
3778 gcc_assert (char_ref->next == NULL);
3782 if (e->ts.cl->length)
3783 gfc_free_expr (e->ts.cl->length);
3784 else if (e->expr_type == EXPR_VARIABLE
3785 && e->symtree->n.sym->attr.dummy)
3789 e->ts.type = BT_CHARACTER;
3790 e->ts.kind = gfc_default_character_kind;
3794 e->ts.cl = gfc_get_charlen ();
3795 e->ts.cl->next = gfc_current_ns->cl_list;
3796 gfc_current_ns->cl_list = e->ts.cl;
3799 if (char_ref->u.ss.start)
3800 start = gfc_copy_expr (char_ref->u.ss.start);
3802 start = gfc_int_expr (1);
3804 if (char_ref->u.ss.end)
3805 end = gfc_copy_expr (char_ref->u.ss.end);
3806 else if (e->expr_type == EXPR_VARIABLE)
3807 end = gfc_copy_expr (e->symtree->n.sym->ts.cl->length);
3814 /* Length = (end - start +1). */
3815 e->ts.cl->length = gfc_subtract (end, start);
3816 e->ts.cl->length = gfc_add (e->ts.cl->length, gfc_int_expr (1));
3818 e->ts.cl->length->ts.type = BT_INTEGER;
3819 e->ts.cl->length->ts.kind = gfc_charlen_int_kind;;
3821 /* Make sure that the length is simplified. */
3822 gfc_simplify_expr (e->ts.cl->length, 1);
3823 gfc_resolve_expr (e->ts.cl->length);
3827 /* Resolve subtype references. */
3830 resolve_ref (gfc_expr *expr)
3832 int current_part_dimension, n_components, seen_part_dimension;
3835 for (ref = expr->ref; ref; ref = ref->next)
3836 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
3838 find_array_spec (expr);
3842 for (ref = expr->ref; ref; ref = ref->next)
3846 if (resolve_array_ref (&ref->u.ar) == FAILURE)
3854 resolve_substring (ref);
3858 /* Check constraints on part references. */
3860 current_part_dimension = 0;
3861 seen_part_dimension = 0;
3864 for (ref = expr->ref; ref; ref = ref->next)
3869 switch (ref->u.ar.type)
3873 current_part_dimension = 1;
3877 current_part_dimension = 0;
3881 gfc_internal_error ("resolve_ref(): Bad array reference");
3887 if (current_part_dimension || seen_part_dimension)
3889 if (ref->u.c.component->pointer)
3891 gfc_error ("Component to the right of a part reference "
3892 "with nonzero rank must not have the POINTER "
3893 "attribute at %L", &expr->where);
3896 else if (ref->u.c.component->allocatable)
3898 gfc_error ("Component to the right of a part reference "
3899 "with nonzero rank must not have the ALLOCATABLE "
3900 "attribute at %L", &expr->where);
3912 if (((ref->type == REF_COMPONENT && n_components > 1)
3913 || ref->next == NULL)
3914 && current_part_dimension
3915 && seen_part_dimension)
3917 gfc_error ("Two or more part references with nonzero rank must "
3918 "not be specified at %L", &expr->where);
3922 if (ref->type == REF_COMPONENT)
3924 if (current_part_dimension)
3925 seen_part_dimension = 1;
3927 /* reset to make sure */
3928 current_part_dimension = 0;
3936 /* Given an expression, determine its shape. This is easier than it sounds.
3937 Leaves the shape array NULL if it is not possible to determine the shape. */
3940 expression_shape (gfc_expr *e)
3942 mpz_t array[GFC_MAX_DIMENSIONS];
3945 if (e->rank == 0 || e->shape != NULL)
3948 for (i = 0; i < e->rank; i++)
3949 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
3952 e->shape = gfc_get_shape (e->rank);
3954 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
3959 for (i--; i >= 0; i--)
3960 mpz_clear (array[i]);
3964 /* Given a variable expression node, compute the rank of the expression by
3965 examining the base symbol and any reference structures it may have. */
3968 expression_rank (gfc_expr *e)
3975 if (e->expr_type == EXPR_ARRAY)
3977 /* Constructors can have a rank different from one via RESHAPE(). */
3979 if (e->symtree == NULL)
3985 e->rank = (e->symtree->n.sym->as == NULL)
3986 ? 0 : e->symtree->n.sym->as->rank;
3992 for (ref = e->ref; ref; ref = ref->next)
3994 if (ref->type != REF_ARRAY)
3997 if (ref->u.ar.type == AR_FULL)
3999 rank = ref->u.ar.as->rank;
4003 if (ref->u.ar.type == AR_SECTION)
4005 /* Figure out the rank of the section. */
4007 gfc_internal_error ("expression_rank(): Two array specs");
4009 for (i = 0; i < ref->u.ar.dimen; i++)
4010 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4011 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4021 expression_shape (e);
4025 /* Resolve a variable expression. */
4028 resolve_variable (gfc_expr *e)
4035 if (e->symtree == NULL)
4038 if (e->ref && resolve_ref (e) == FAILURE)
4041 sym = e->symtree->n.sym;
4042 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
4044 e->ts.type = BT_PROCEDURE;
4048 if (sym->ts.type != BT_UNKNOWN)
4049 gfc_variable_attr (e, &e->ts);
4052 /* Must be a simple variable reference. */
4053 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4058 if (check_assumed_size_reference (sym, e))
4061 /* Deal with forward references to entries during resolve_code, to
4062 satisfy, at least partially, 12.5.2.5. */
4063 if (gfc_current_ns->entries
4064 && current_entry_id == sym->entry_id
4067 && cs_base->current->op != EXEC_ENTRY)
4069 gfc_entry_list *entry;
4070 gfc_formal_arglist *formal;
4074 /* If the symbol is a dummy... */
4075 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4077 entry = gfc_current_ns->entries;
4080 /* ...test if the symbol is a parameter of previous entries. */
4081 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4082 for (formal = entry->sym->formal; formal; formal = formal->next)
4084 if (formal->sym && sym->name == formal->sym->name)
4088 /* If it has not been seen as a dummy, this is an error. */
4091 if (specification_expr)
4092 gfc_error ("Variable '%s', used in a specification expression"
4093 ", is referenced at %L before the ENTRY statement "
4094 "in which it is a parameter",
4095 sym->name, &cs_base->current->loc);
4097 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4098 "statement in which it is a parameter",
4099 sym->name, &cs_base->current->loc);
4104 /* Now do the same check on the specification expressions. */
4105 specification_expr = 1;
4106 if (sym->ts.type == BT_CHARACTER
4107 && gfc_resolve_expr (sym->ts.cl->length) == FAILURE)
4111 for (n = 0; n < sym->as->rank; n++)
4113 specification_expr = 1;
4114 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4116 specification_expr = 1;
4117 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4120 specification_expr = 0;
4123 /* Update the symbol's entry level. */
4124 sym->entry_id = current_entry_id + 1;
4131 /* Checks to see that the correct symbol has been host associated.
4132 The only situation where this arises is that in which a twice
4133 contained function is parsed after the host association is made.
4134 Therefore, on detecting this, the line is rematched, having got
4135 rid of the existing references and actual_arg_list. */
4137 check_host_association (gfc_expr *e)
4139 gfc_symbol *sym, *old_sym;
4143 bool retval = e->expr_type == EXPR_FUNCTION;
4145 if (e->symtree == NULL || e->symtree->n.sym == NULL)
4148 old_sym = e->symtree->n.sym;
4150 if (old_sym->attr.use_assoc)
4153 if (gfc_current_ns->parent
4154 && old_sym->ns != gfc_current_ns)
4156 gfc_find_symbol (old_sym->name, gfc_current_ns, 1, &sym);
4157 if (sym && old_sym != sym
4158 && sym->attr.flavor == FL_PROCEDURE
4159 && sym->attr.contained)
4161 temp_locus = gfc_current_locus;
4162 gfc_current_locus = e->where;
4164 gfc_buffer_error (1);
4166 gfc_free_ref_list (e->ref);
4171 gfc_free_actual_arglist (e->value.function.actual);
4172 e->value.function.actual = NULL;
4175 if (e->shape != NULL)
4177 for (n = 0; n < e->rank; n++)
4178 mpz_clear (e->shape[n]);
4180 gfc_free (e->shape);
4183 gfc_match_rvalue (&expr);
4185 gfc_buffer_error (0);
4187 gcc_assert (expr && sym == expr->symtree->n.sym);
4193 gfc_current_locus = temp_locus;
4196 /* This might have changed! */
4197 return e->expr_type == EXPR_FUNCTION;
4202 gfc_resolve_character_operator (gfc_expr *e)
4204 gfc_expr *op1 = e->value.op.op1;
4205 gfc_expr *op2 = e->value.op.op2;
4206 gfc_expr *e1 = NULL;
4207 gfc_expr *e2 = NULL;
4209 gcc_assert (e->value.op.operator == INTRINSIC_CONCAT);
4211 if (op1->ts.cl && op1->ts.cl->length)
4212 e1 = gfc_copy_expr (op1->ts.cl->length);
4213 else if (op1->expr_type == EXPR_CONSTANT)
4214 e1 = gfc_int_expr (op1->value.character.length);
4216 if (op2->ts.cl && op2->ts.cl->length)
4217 e2 = gfc_copy_expr (op2->ts.cl->length);
4218 else if (op2->expr_type == EXPR_CONSTANT)
4219 e2 = gfc_int_expr (op2->value.character.length);
4221 e->ts.cl = gfc_get_charlen ();
4222 e->ts.cl->next = gfc_current_ns->cl_list;
4223 gfc_current_ns->cl_list = e->ts.cl;
4228 e->ts.cl->length = gfc_add (e1, e2);
4229 e->ts.cl->length->ts.type = BT_INTEGER;
4230 e->ts.cl->length->ts.kind = gfc_charlen_int_kind;;
4231 gfc_simplify_expr (e->ts.cl->length, 0);
4232 gfc_resolve_expr (e->ts.cl->length);
4238 /* Ensure that an character expression has a charlen and, if possible, a
4239 length expression. */
4242 fixup_charlen (gfc_expr *e)
4244 /* The cases fall through so that changes in expression type and the need
4245 for multiple fixes are picked up. In all circumstances, a charlen should
4246 be available for the middle end to hang a backend_decl on. */
4247 switch (e->expr_type)
4250 gfc_resolve_character_operator (e);
4253 if (e->expr_type == EXPR_ARRAY)
4254 gfc_resolve_character_array_constructor (e);
4256 case EXPR_SUBSTRING:
4257 if (!e->ts.cl && e->ref)
4258 gfc_resolve_substring_charlen (e);
4263 e->ts.cl = gfc_get_charlen ();
4264 e->ts.cl->next = gfc_current_ns->cl_list;
4265 gfc_current_ns->cl_list = e->ts.cl;
4273 /* Resolve an expression. That is, make sure that types of operands agree
4274 with their operators, intrinsic operators are converted to function calls
4275 for overloaded types and unresolved function references are resolved. */
4278 gfc_resolve_expr (gfc_expr *e)
4285 switch (e->expr_type)
4288 t = resolve_operator (e);
4294 if (check_host_association (e))
4295 t = resolve_function (e);
4298 t = resolve_variable (e);
4300 expression_rank (e);
4303 if (e->ts.type == BT_CHARACTER && e->ts.cl == NULL && e->ref
4304 && e->ref->type != REF_SUBSTRING)
4305 gfc_resolve_substring_charlen (e);
4309 case EXPR_SUBSTRING:
4310 t = resolve_ref (e);
4320 if (resolve_ref (e) == FAILURE)
4323 t = gfc_resolve_array_constructor (e);
4324 /* Also try to expand a constructor. */
4327 expression_rank (e);
4328 gfc_expand_constructor (e);
4331 /* This provides the opportunity for the length of constructors with
4332 character valued function elements to propagate the string length
4333 to the expression. */
4334 if (e->ts.type == BT_CHARACTER)
4335 gfc_resolve_character_array_constructor (e);
4339 case EXPR_STRUCTURE:
4340 t = resolve_ref (e);
4344 t = resolve_structure_cons (e);
4348 t = gfc_simplify_expr (e, 0);
4352 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
4355 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.cl)
4362 /* Resolve an expression from an iterator. They must be scalar and have
4363 INTEGER or (optionally) REAL type. */
4366 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
4367 const char *name_msgid)
4369 if (gfc_resolve_expr (expr) == FAILURE)
4372 if (expr->rank != 0)
4374 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
4378 if (expr->ts.type != BT_INTEGER)
4380 if (expr->ts.type == BT_REAL)
4383 return gfc_notify_std (GFC_STD_F95_DEL,
4384 "Deleted feature: %s at %L must be integer",
4385 _(name_msgid), &expr->where);
4388 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
4395 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
4403 /* Resolve the expressions in an iterator structure. If REAL_OK is
4404 false allow only INTEGER type iterators, otherwise allow REAL types. */
4407 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
4409 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
4413 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
4415 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
4420 if (gfc_resolve_iterator_expr (iter->start, real_ok,
4421 "Start expression in DO loop") == FAILURE)
4424 if (gfc_resolve_iterator_expr (iter->end, real_ok,
4425 "End expression in DO loop") == FAILURE)
4428 if (gfc_resolve_iterator_expr (iter->step, real_ok,
4429 "Step expression in DO loop") == FAILURE)
4432 if (iter->step->expr_type == EXPR_CONSTANT)
4434 if ((iter->step->ts.type == BT_INTEGER
4435 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
4436 || (iter->step->ts.type == BT_REAL
4437 && mpfr_sgn (iter->step->value.real) == 0))
4439 gfc_error ("Step expression in DO loop at %L cannot be zero",
4440 &iter->step->where);
4445 /* Convert start, end, and step to the same type as var. */
4446 if (iter->start->ts.kind != iter->var->ts.kind
4447 || iter->start->ts.type != iter->var->ts.type)
4448 gfc_convert_type (iter->start, &iter->var->ts, 2);
4450 if (iter->end->ts.kind != iter->var->ts.kind
4451 || iter->end->ts.type != iter->var->ts.type)
4452 gfc_convert_type (iter->end, &iter->var->ts, 2);
4454 if (iter->step->ts.kind != iter->var->ts.kind
4455 || iter->step->ts.type != iter->var->ts.type)
4456 gfc_convert_type (iter->step, &iter->var->ts, 2);
4462 /* Traversal function for find_forall_index. f == 2 signals that
4463 that variable itself is not to be checked - only the references. */
4466 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
4468 if (expr->expr_type != EXPR_VARIABLE)
4471 /* A scalar assignment */
4472 if (!expr->ref || *f == 1)
4474 if (expr->symtree->n.sym == sym)
4486 /* Check whether the FORALL index appears in the expression or not.
4487 Returns SUCCESS if SYM is found in EXPR. */
4490 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
4492 if (gfc_traverse_expr (expr, sym, forall_index, f))
4499 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
4500 to be a scalar INTEGER variable. The subscripts and stride are scalar
4501 INTEGERs, and if stride is a constant it must be nonzero.
4502 Furthermore "A subscript or stride in a forall-triplet-spec shall
4503 not contain a reference to any index-name in the
4504 forall-triplet-spec-list in which it appears." (7.5.4.1) */
4507 resolve_forall_iterators (gfc_forall_iterator *it)
4509 gfc_forall_iterator *iter, *iter2;
4511 for (iter = it; iter; iter = iter->next)
4513 if (gfc_resolve_expr (iter->var) == SUCCESS
4514 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
4515 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
4518 if (gfc_resolve_expr (iter->start) == SUCCESS
4519 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
4520 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
4521 &iter->start->where);
4522 if (iter->var->ts.kind != iter->start->ts.kind)
4523 gfc_convert_type (iter->start, &iter->var->ts, 2);
4525 if (gfc_resolve_expr (iter->end) == SUCCESS
4526 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
4527 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
4529 if (iter->var->ts.kind != iter->end->ts.kind)
4530 gfc_convert_type (iter->end, &iter->var->ts, 2);
4532 if (gfc_resolve_expr (iter->stride) == SUCCESS)
4534 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
4535 gfc_error ("FORALL stride expression at %L must be a scalar %s",
4536 &iter->stride->where, "INTEGER");
4538 if (iter->stride->expr_type == EXPR_CONSTANT
4539 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
4540 gfc_error ("FORALL stride expression at %L cannot be zero",
4541 &iter->stride->where);
4543 if (iter->var->ts.kind != iter->stride->ts.kind)
4544 gfc_convert_type (iter->stride, &iter->var->ts, 2);
4547 for (iter = it; iter; iter = iter->next)
4548 for (iter2 = iter; iter2; iter2 = iter2->next)
4550 if (find_forall_index (iter2->start,
4551 iter->var->symtree->n.sym, 0) == SUCCESS
4552 || find_forall_index (iter2->end,
4553 iter->var->symtree->n.sym, 0) == SUCCESS
4554 || find_forall_index (iter2->stride,
4555 iter->var->symtree->n.sym, 0) == SUCCESS)
4556 gfc_error ("FORALL index '%s' may not appear in triplet "
4557 "specification at %L", iter->var->symtree->name,
4558 &iter2->start->where);
4563 /* Given a pointer to a symbol that is a derived type, see if it's
4564 inaccessible, i.e. if it's defined in another module and the components are
4565 PRIVATE. The search is recursive if necessary. Returns zero if no
4566 inaccessible components are found, nonzero otherwise. */
4569 derived_inaccessible (gfc_symbol *sym)
4573 if (sym->attr.use_assoc && sym->attr.private_comp)
4576 for (c = sym->components; c; c = c->next)
4578 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.derived))
4586 /* Resolve the argument of a deallocate expression. The expression must be
4587 a pointer or a full array. */
4590 resolve_deallocate_expr (gfc_expr *e)
4592 symbol_attribute attr;
4593 int allocatable, pointer, check_intent_in;
4596 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
4597 check_intent_in = 1;
4599 if (gfc_resolve_expr (e) == FAILURE)
4602 if (e->expr_type != EXPR_VARIABLE)
4605 allocatable = e->symtree->n.sym->attr.allocatable;
4606 pointer = e->symtree->n.sym->attr.pointer;
4607 for (ref = e->ref; ref; ref = ref->next)
4610 check_intent_in = 0;
4615 if (ref->u.ar.type != AR_FULL)
4620 allocatable = (ref->u.c.component->as != NULL
4621 && ref->u.c.component->as->type == AS_DEFERRED);
4622 pointer = ref->u.c.component->pointer;
4631 attr = gfc_expr_attr (e);
4633 if (allocatable == 0 && attr.pointer == 0)
4636 gfc_error ("Expression in DEALLOCATE statement at %L must be "
4637 "ALLOCATABLE or a POINTER", &e->where);
4641 && e->symtree->n.sym->attr.intent == INTENT_IN)
4643 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
4644 e->symtree->n.sym->name, &e->where);
4652 /* Returns true if the expression e contains a reference to the symbol sym. */
4654 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
4656 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
4663 find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
4665 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
4669 /* Given the expression node e for an allocatable/pointer of derived type to be
4670 allocated, get the expression node to be initialized afterwards (needed for
4671 derived types with default initializers, and derived types with allocatable
4672 components that need nullification.) */
4675 expr_to_initialize (gfc_expr *e)
4681 result = gfc_copy_expr (e);
4683 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
4684 for (ref = result->ref; ref; ref = ref->next)
4685 if (ref->type == REF_ARRAY && ref->next == NULL)
4687 ref->u.ar.type = AR_FULL;
4689 for (i = 0; i < ref->u.ar.dimen; i++)
4690 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
4692 result->rank = ref->u.ar.dimen;
4700 /* Resolve the expression in an ALLOCATE statement, doing the additional
4701 checks to see whether the expression is OK or not. The expression must
4702 have a trailing array reference that gives the size of the array. */
4705 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
4707 int i, pointer, allocatable, dimension, check_intent_in;
4708 symbol_attribute attr;
4709 gfc_ref *ref, *ref2;
4716 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
4717 check_intent_in = 1;
4719 if (gfc_resolve_expr (e) == FAILURE)
4722 if (code->expr && code->expr->expr_type == EXPR_VARIABLE)
4723 sym = code->expr->symtree->n.sym;
4727 /* Make sure the expression is allocatable or a pointer. If it is
4728 pointer, the next-to-last reference must be a pointer. */
4732 if (e->expr_type != EXPR_VARIABLE)
4735 attr = gfc_expr_attr (e);
4736 pointer = attr.pointer;
4737 dimension = attr.dimension;
4741 allocatable = e->symtree->n.sym->attr.allocatable;
4742 pointer = e->symtree->n.sym->attr.pointer;
4743 dimension = e->symtree->n.sym->attr.dimension;
4745 if (sym == e->symtree->n.sym && sym->ts.type != BT_DERIVED)
4747 gfc_error ("The STAT variable '%s' in an ALLOCATE statement must "
4748 "not be allocated in the same statement at %L",
4749 sym->name, &e->where);
4753 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
4756 check_intent_in = 0;
4761 if (ref->next != NULL)
4766 allocatable = (ref->u.c.component->as != NULL
4767 && ref->u.c.component->as->type == AS_DEFERRED);
4769 pointer = ref->u.c.component->pointer;
4770 dimension = ref->u.c.component->dimension;
4781 if (allocatable == 0 && pointer == 0)
4783 gfc_error ("Expression in ALLOCATE statement at %L must be "
4784 "ALLOCATABLE or a POINTER", &e->where);
4789 && e->symtree->n.sym->attr.intent == INTENT_IN)
4791 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
4792 e->symtree->n.sym->name, &e->where);
4796 /* Add default initializer for those derived types that need them. */
4797 if (e->ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&e->ts)))
4799 init_st = gfc_get_code ();
4800 init_st->loc = code->loc;
4801 init_st->op = EXEC_INIT_ASSIGN;
4802 init_st->expr = expr_to_initialize (e);
4803 init_st->expr2 = init_e;
4804 init_st->next = code->next;
4805 code->next = init_st;
4808 if (pointer && dimension == 0)
4811 /* Make sure the next-to-last reference node is an array specification. */
4813 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
4815 gfc_error ("Array specification required in ALLOCATE statement "
4816 "at %L", &e->where);
4820 /* Make sure that the array section reference makes sense in the
4821 context of an ALLOCATE specification. */
4825 for (i = 0; i < ar->dimen; i++)
4827 if (ref2->u.ar.type == AR_ELEMENT)
4830 switch (ar->dimen_type[i])
4836 if (ar->start[i] != NULL
4837 && ar->end[i] != NULL
4838 && ar->stride[i] == NULL)
4841 /* Fall Through... */
4845 gfc_error ("Bad array specification in ALLOCATE statement at %L",
4852 for (a = code->ext.alloc_list; a; a = a->next)
4854 sym = a->expr->symtree->n.sym;
4856 /* TODO - check derived type components. */
4857 if (sym->ts.type == BT_DERIVED)
4860 if ((ar->start[i] != NULL && find_sym_in_expr (sym, ar->start[i]))
4861 || (ar->end[i] != NULL && find_sym_in_expr (sym, ar->end[i])))
4863 gfc_error ("'%s' must not appear an the array specification at "
4864 "%L in the same ALLOCATE statement where it is "
4865 "itself allocated", sym->name, &ar->where);
4875 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
4877 gfc_symbol *s = NULL;
4882 s = code->expr->symtree->n.sym;
4886 if (s->attr.intent == INTENT_IN)
4887 gfc_error ("STAT variable '%s' of %s statement at %C cannot "
4888 "be INTENT(IN)", s->name, fcn);
4890 if (gfc_pure (NULL) && gfc_impure_variable (s))
4891 gfc_error ("Illegal STAT variable in %s statement at %C "
4892 "for a PURE procedure", fcn);
4894 is_variable = false;
4895 if (s->attr.flavor == FL_VARIABLE)
4897 else if (s->attr.function && s->result == s
4898 && (gfc_current_ns->proc_name == s
4900 (gfc_current_ns->parent
4901 && gfc_current_ns->parent->proc_name == s)))
4903 else if (gfc_current_ns->entries && s->result == s)
4906 for (el = gfc_current_ns->entries; el; el = el->next)
4912 else if (gfc_current_ns->parent && gfc_current_ns->parent->entries
4916 for (el = gfc_current_ns->parent->entries; el; el = el->next)
4923 if (s->attr.flavor == FL_UNKNOWN
4924 && gfc_add_flavor (&s->attr, FL_VARIABLE,
4925 s->name, NULL) == SUCCESS)
4929 gfc_error ("STAT tag in %s statement at %L must be "
4930 "a variable", fcn, &code->expr->where);
4934 if (s && code->expr->ts.type != BT_INTEGER)
4935 gfc_error ("STAT tag in %s statement at %L must be "
4936 "of type INTEGER", fcn, &code->expr->where);
4938 if (strcmp (fcn, "ALLOCATE") == 0)
4940 for (a = code->ext.alloc_list; a; a = a->next)
4941 resolve_allocate_expr (a->expr, code);
4945 for (a = code->ext.alloc_list; a; a = a->next)
4946 resolve_deallocate_expr (a->expr);
4950 /************ SELECT CASE resolution subroutines ************/
4952 /* Callback function for our mergesort variant. Determines interval
4953 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
4954 op1 > op2. Assumes we're not dealing with the default case.
4955 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
4956 There are nine situations to check. */
4959 compare_cases (const gfc_case *op1, const gfc_case *op2)
4963 if (op1->low == NULL) /* op1 = (:L) */
4965 /* op2 = (:N), so overlap. */
4967 /* op2 = (M:) or (M:N), L < M */
4968 if (op2->low != NULL
4969 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
4972 else if (op1->high == NULL) /* op1 = (K:) */
4974 /* op2 = (M:), so overlap. */
4976 /* op2 = (:N) or (M:N), K > N */
4977 if (op2->high != NULL
4978 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
4981 else /* op1 = (K:L) */
4983 if (op2->low == NULL) /* op2 = (:N), K > N */
4984 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
4986 else if (op2->high == NULL) /* op2 = (M:), L < M */
4987 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
4989 else /* op2 = (M:N) */
4993 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
4996 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
5005 /* Merge-sort a double linked case list, detecting overlap in the
5006 process. LIST is the head of the double linked case list before it
5007 is sorted. Returns the head of the sorted list if we don't see any
5008 overlap, or NULL otherwise. */
5011 check_case_overlap (gfc_case *list)
5013 gfc_case *p, *q, *e, *tail;
5014 int insize, nmerges, psize, qsize, cmp, overlap_seen;
5016 /* If the passed list was empty, return immediately. */
5023 /* Loop unconditionally. The only exit from this loop is a return
5024 statement, when we've finished sorting the case list. */
5031 /* Count the number of merges we do in this pass. */
5034 /* Loop while there exists a merge to be done. */
5039 /* Count this merge. */
5042 /* Cut the list in two pieces by stepping INSIZE places
5043 forward in the list, starting from P. */
5046 for (i = 0; i < insize; i++)
5055 /* Now we have two lists. Merge them! */
5056 while (psize > 0 || (qsize > 0 && q != NULL))
5058 /* See from which the next case to merge comes from. */
5061 /* P is empty so the next case must come from Q. */
5066 else if (qsize == 0 || q == NULL)
5075 cmp = compare_cases (p, q);
5078 /* The whole case range for P is less than the
5086 /* The whole case range for Q is greater than
5087 the case range for P. */
5094 /* The cases overlap, or they are the same
5095 element in the list. Either way, we must
5096 issue an error and get the next case from P. */
5097 /* FIXME: Sort P and Q by line number. */
5098 gfc_error ("CASE label at %L overlaps with CASE "
5099 "label at %L", &p->where, &q->where);
5107 /* Add the next element to the merged list. */
5116 /* P has now stepped INSIZE places along, and so has Q. So
5117 they're the same. */
5122 /* If we have done only one merge or none at all, we've
5123 finished sorting the cases. */
5132 /* Otherwise repeat, merging lists twice the size. */
5138 /* Check to see if an expression is suitable for use in a CASE statement.
5139 Makes sure that all case expressions are scalar constants of the same
5140 type. Return FAILURE if anything is wrong. */
5143 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
5145 if (e == NULL) return SUCCESS;
5147 if (e->ts.type != case_expr->ts.type)
5149 gfc_error ("Expression in CASE statement at %L must be of type %s",
5150 &e->where, gfc_basic_typename (case_expr->ts.type));
5154 /* C805 (R808) For a given case-construct, each case-value shall be of
5155 the same type as case-expr. For character type, length differences
5156 are allowed, but the kind type parameters shall be the same. */
5158 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
5160 gfc_error("Expression in CASE statement at %L must be kind %d",
5161 &e->where, case_expr->ts.kind);
5165 /* Convert the case value kind to that of case expression kind, if needed.
5166 FIXME: Should a warning be issued? */
5167 if (e->ts.kind != case_expr->ts.kind)
5168 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
5172 gfc_error ("Expression in CASE statement at %L must be scalar",
5181 /* Given a completely parsed select statement, we:
5183 - Validate all expressions and code within the SELECT.
5184 - Make sure that the selection expression is not of the wrong type.
5185 - Make sure that no case ranges overlap.
5186 - Eliminate unreachable cases and unreachable code resulting from
5187 removing case labels.
5189 The standard does allow unreachable cases, e.g. CASE (5:3). But
5190 they are a hassle for code generation, and to prevent that, we just
5191 cut them out here. This is not necessary for overlapping cases
5192 because they are illegal and we never even try to generate code.
5194 We have the additional caveat that a SELECT construct could have
5195 been a computed GOTO in the source code. Fortunately we can fairly
5196 easily work around that here: The case_expr for a "real" SELECT CASE
5197 is in code->expr1, but for a computed GOTO it is in code->expr2. All
5198 we have to do is make sure that the case_expr is a scalar integer
5202 resolve_select (gfc_code *code)
5205 gfc_expr *case_expr;
5206 gfc_case *cp, *default_case, *tail, *head;
5207 int seen_unreachable;
5213 if (code->expr == NULL)
5215 /* This was actually a computed GOTO statement. */
5216 case_expr = code->expr2;
5217 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
5218 gfc_error ("Selection expression in computed GOTO statement "
5219 "at %L must be a scalar integer expression",
5222 /* Further checking is not necessary because this SELECT was built
5223 by the compiler, so it should always be OK. Just move the
5224 case_expr from expr2 to expr so that we can handle computed
5225 GOTOs as normal SELECTs from here on. */
5226 code->expr = code->expr2;
5231 case_expr = code->expr;
5233 type = case_expr->ts.type;
5234 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
5236 gfc_error ("Argument of SELECT statement at %L cannot be %s",
5237 &case_expr->where, gfc_typename (&case_expr->ts));
5239 /* Punt. Going on here just produce more garbage error messages. */
5243 if (case_expr->rank != 0)
5245 gfc_error ("Argument of SELECT statement at %L must be a scalar "
5246 "expression", &case_expr->where);
5252 /* PR 19168 has a long discussion concerning a mismatch of the kinds
5253 of the SELECT CASE expression and its CASE values. Walk the lists
5254 of case values, and if we find a mismatch, promote case_expr to
5255 the appropriate kind. */
5257 if (type == BT_LOGICAL || type == BT_INTEGER)
5259 for (body = code->block; body; body = body->block)
5261 /* Walk the case label list. */
5262 for (cp = body->ext.case_list; cp; cp = cp->next)
5264 /* Intercept the DEFAULT case. It does not have a kind. */
5265 if (cp->low == NULL && cp->high == NULL)
5268 /* Unreachable case ranges are discarded, so ignore. */
5269 if (cp->low != NULL && cp->high != NULL
5270 && cp->low != cp->high
5271 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5274 /* FIXME: Should a warning be issued? */
5276 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
5277 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
5279 if (cp->high != NULL
5280 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
5281 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
5286 /* Assume there is no DEFAULT case. */
5287 default_case = NULL;
5292 for (body = code->block; body; body = body->block)
5294 /* Assume the CASE list is OK, and all CASE labels can be matched. */
5296 seen_unreachable = 0;
5298 /* Walk the case label list, making sure that all case labels
5300 for (cp = body->ext.case_list; cp; cp = cp->next)
5302 /* Count the number of cases in the whole construct. */
5305 /* Intercept the DEFAULT case. */
5306 if (cp->low == NULL && cp->high == NULL)
5308 if (default_case != NULL)
5310 gfc_error ("The DEFAULT CASE at %L cannot be followed "
5311 "by a second DEFAULT CASE at %L",
5312 &default_case->where, &cp->where);
5323 /* Deal with single value cases and case ranges. Errors are
5324 issued from the validation function. */
5325 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
5326 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
5332 if (type == BT_LOGICAL
5333 && ((cp->low == NULL || cp->high == NULL)
5334 || cp->low != cp->high))
5336 gfc_error ("Logical range in CASE statement at %L is not "
5337 "allowed", &cp->low->where);
5342 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
5345 value = cp->low->value.logical == 0 ? 2 : 1;
5346 if (value & seen_logical)
5348 gfc_error ("constant logical value in CASE statement "
5349 "is repeated at %L",
5354 seen_logical |= value;
5357 if (cp->low != NULL && cp->high != NULL
5358 && cp->low != cp->high
5359 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5361 if (gfc_option.warn_surprising)
5362 gfc_warning ("Range specification at %L can never "
5363 "be matched", &cp->where);
5365 cp->unreachable = 1;
5366 seen_unreachable = 1;
5370 /* If the case range can be matched, it can also overlap with
5371 other cases. To make sure it does not, we put it in a
5372 double linked list here. We sort that with a merge sort
5373 later on to detect any overlapping cases. */
5377 head->right = head->left = NULL;
5382 tail->right->left = tail;
5389 /* It there was a failure in the previous case label, give up
5390 for this case label list. Continue with the next block. */
5394 /* See if any case labels that are unreachable have been seen.
5395 If so, we eliminate them. This is a bit of a kludge because
5396 the case lists for a single case statement (label) is a
5397 single forward linked lists. */
5398 if (seen_unreachable)
5400 /* Advance until the first case in the list is reachable. */
5401 while (body->ext.case_list != NULL
5402 && body->ext.case_list->unreachable)
5404 gfc_case *n = body->ext.case_list;
5405 body->ext.case_list = body->ext.case_list->next;
5407 gfc_free_case_list (n);
5410 /* Strip all other unreachable cases. */
5411 if (body->ext.case_list)
5413 for (cp = body->ext.case_list; cp->next; cp = cp->next)
5415 if (cp->next->unreachable)
5417 gfc_case *n = cp->next;
5418 cp->next = cp->next->next;
5420 gfc_free_case_list (n);
5427 /* See if there were overlapping cases. If the check returns NULL,
5428 there was overlap. In that case we don't do anything. If head
5429 is non-NULL, we prepend the DEFAULT case. The sorted list can
5430 then used during code generation for SELECT CASE constructs with
5431 a case expression of a CHARACTER type. */
5434 head = check_case_overlap (head);
5436 /* Prepend the default_case if it is there. */
5437 if (head != NULL && default_case)
5439 default_case->left = NULL;
5440 default_case->right = head;
5441 head->left = default_case;
5445 /* Eliminate dead blocks that may be the result if we've seen
5446 unreachable case labels for a block. */
5447 for (body = code; body && body->block; body = body->block)
5449 if (body->block->ext.case_list == NULL)
5451 /* Cut the unreachable block from the code chain. */
5452 gfc_code *c = body->block;
5453 body->block = c->block;
5455 /* Kill the dead block, but not the blocks below it. */
5457 gfc_free_statements (c);
5461 /* More than two cases is legal but insane for logical selects.
5462 Issue a warning for it. */
5463 if (gfc_option.warn_surprising && type == BT_LOGICAL
5465 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
5470 /* Resolve a transfer statement. This is making sure that:
5471 -- a derived type being transferred has only non-pointer components
5472 -- a derived type being transferred doesn't have private components, unless
5473 it's being transferred from the module where the type was defined
5474 -- we're not trying to transfer a whole assumed size array. */
5477 resolve_transfer (gfc_code *code)
5486 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
5489 sym = exp->symtree->n.sym;
5492 /* Go to actual component transferred. */
5493 for (ref = code->expr->ref; ref; ref = ref->next)
5494 if (ref->type == REF_COMPONENT)
5495 ts = &ref->u.c.component->ts;
5497 if (ts->type == BT_DERIVED)
5499 /* Check that transferred derived type doesn't contain POINTER
5501 if (ts->derived->attr.pointer_comp)
5503 gfc_error ("Data transfer element at %L cannot have "
5504 "POINTER components", &code->loc);
5508 if (ts->derived->attr.alloc_comp)
5510 gfc_error ("Data transfer element at %L cannot have "
5511 "ALLOCATABLE components", &code->loc);
5515 if (derived_inaccessible (ts->derived))
5517 gfc_error ("Data transfer element at %L cannot have "
5518 "PRIVATE components",&code->loc);
5523 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
5524 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
5526 gfc_error ("Data transfer element at %L cannot be a full reference to "
5527 "an assumed-size array", &code->loc);
5533 /*********** Toplevel code resolution subroutines ***********/
5535 /* Find the set of labels that are reachable from this block. We also
5536 record the last statement in each block so that we don't have to do
5537 a linear search to find the END DO statements of the blocks. */
5540 reachable_labels (gfc_code *block)
5547 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
5549 /* Collect labels in this block. */
5550 for (c = block; c; c = c->next)
5553 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
5555 if (!c->next && cs_base->prev)
5556 cs_base->prev->tail = c;
5559 /* Merge with labels from parent block. */
5562 gcc_assert (cs_base->prev->reachable_labels);
5563 bitmap_ior_into (cs_base->reachable_labels,
5564 cs_base->prev->reachable_labels);
5568 /* Given a branch to a label and a namespace, if the branch is conforming.
5569 The code node describes where the branch is located. */
5572 resolve_branch (gfc_st_label *label, gfc_code *code)
5579 /* Step one: is this a valid branching target? */
5581 if (label->defined == ST_LABEL_UNKNOWN)
5583 gfc_error ("Label %d referenced at %L is never defined", label->value,
5588 if (label->defined != ST_LABEL_TARGET)
5590 gfc_error ("Statement at %L is not a valid branch target statement "
5591 "for the branch statement at %L", &label->where, &code->loc);
5595 /* Step two: make sure this branch is not a branch to itself ;-) */
5597 if (code->here == label)
5599 gfc_warning ("Branch at %L causes an infinite loop", &code->loc);
5603 /* Step three: See if the label is in the same block as the
5604 branching statement. The hard work has been done by setting up
5605 the bitmap reachable_labels. */
5607 if (!bitmap_bit_p (cs_base->reachable_labels, label->value))
5609 /* The label is not in an enclosing block, so illegal. This was
5610 allowed in Fortran 66, so we allow it as extension. No
5611 further checks are necessary in this case. */
5612 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
5613 "as the GOTO statement at %L", &label->where,
5618 /* Step four: Make sure that the branching target is legal if
5619 the statement is an END {SELECT,IF}. */
5621 for (stack = cs_base; stack; stack = stack->prev)
5622 if (stack->current->next && stack->current->next->here == label)
5625 if (stack && stack->current->next->op == EXEC_NOP)
5627 gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: GOTO at %L jumps to "
5628 "END of construct at %L", &code->loc,
5629 &stack->current->next->loc);
5630 return; /* We know this is not an END DO. */
5633 /* Step five: Make sure that we're not jumping to the end of a DO
5634 loop from within the loop. */
5636 for (stack = cs_base; stack; stack = stack->prev)
5637 if ((stack->current->op == EXEC_DO
5638 || stack->current->op == EXEC_DO_WHILE)
5639 && stack->tail->here == label && stack->tail->op == EXEC_NOP)
5641 gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: GOTO at %L jumps "
5642 "to END of construct at %L", &code->loc,
5650 /* Check whether EXPR1 has the same shape as EXPR2. */
5653 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
5655 mpz_t shape[GFC_MAX_DIMENSIONS];
5656 mpz_t shape2[GFC_MAX_DIMENSIONS];
5657 try result = FAILURE;
5660 /* Compare the rank. */
5661 if (expr1->rank != expr2->rank)
5664 /* Compare the size of each dimension. */
5665 for (i=0; i<expr1->rank; i++)
5667 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
5670 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
5673 if (mpz_cmp (shape[i], shape2[i]))
5677 /* When either of the two expression is an assumed size array, we
5678 ignore the comparison of dimension sizes. */
5683 for (i--; i >= 0; i--)
5685 mpz_clear (shape[i]);
5686 mpz_clear (shape2[i]);
5692 /* Check whether a WHERE assignment target or a WHERE mask expression
5693 has the same shape as the outmost WHERE mask expression. */
5696 resolve_where (gfc_code *code, gfc_expr *mask)
5702 cblock = code->block;
5704 /* Store the first WHERE mask-expr of the WHERE statement or construct.
5705 In case of nested WHERE, only the outmost one is stored. */
5706 if (mask == NULL) /* outmost WHERE */
5708 else /* inner WHERE */
5715 /* Check if the mask-expr has a consistent shape with the
5716 outmost WHERE mask-expr. */
5717 if (resolve_where_shape (cblock->expr, e) == FAILURE)
5718 gfc_error ("WHERE mask at %L has inconsistent shape",
5719 &cblock->expr->where);
5722 /* the assignment statement of a WHERE statement, or the first
5723 statement in where-body-construct of a WHERE construct */
5724 cnext = cblock->next;
5729 /* WHERE assignment statement */
5732 /* Check shape consistent for WHERE assignment target. */
5733 if (e && resolve_where_shape (cnext->expr, e) == FAILURE)
5734 gfc_error ("WHERE assignment target at %L has "
5735 "inconsistent shape", &cnext->expr->where);
5739 case EXEC_ASSIGN_CALL:
5740 resolve_call (cnext);
5741 if (!cnext->resolved_sym->attr.elemental)
5742 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
5743 &cnext->ext.actual->expr->where);
5746 /* WHERE or WHERE construct is part of a where-body-construct */
5748 resolve_where (cnext, e);
5752 gfc_error ("Unsupported statement inside WHERE at %L",
5755 /* the next statement within the same where-body-construct */
5756 cnext = cnext->next;
5758 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
5759 cblock = cblock->block;
5764 /* Resolve assignment in FORALL construct.
5765 NVAR is the number of FORALL index variables, and VAR_EXPR records the
5766 FORALL index variables. */
5769 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
5773 for (n = 0; n < nvar; n++)
5775 gfc_symbol *forall_index;
5777 forall_index = var_expr[n]->symtree->n.sym;
5779 /* Check whether the assignment target is one of the FORALL index
5781 if ((code->expr->expr_type == EXPR_VARIABLE)
5782 && (code->expr->symtree->n.sym == forall_index))
5783 gfc_error ("Assignment to a FORALL index variable at %L",
5784 &code->expr->where);
5787 /* If one of the FORALL index variables doesn't appear in the
5788 assignment target, then there will be a many-to-one
5790 if (find_forall_index (code->expr, forall_index, 0) == FAILURE)
5791 gfc_error ("The FORALL with index '%s' cause more than one "
5792 "assignment to this object at %L",
5793 var_expr[n]->symtree->name, &code->expr->where);
5799 /* Resolve WHERE statement in FORALL construct. */
5802 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
5803 gfc_expr **var_expr)
5808 cblock = code->block;
5811 /* the assignment statement of a WHERE statement, or the first
5812 statement in where-body-construct of a WHERE construct */
5813 cnext = cblock->next;
5818 /* WHERE assignment statement */
5820 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
5823 /* WHERE operator assignment statement */
5824 case EXEC_ASSIGN_CALL:
5825 resolve_call (cnext);
5826 if (!cnext->resolved_sym->attr.elemental)
5827 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
5828 &cnext->ext.actual->expr->where);
5831 /* WHERE or WHERE construct is part of a where-body-construct */
5833 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
5837 gfc_error ("Unsupported statement inside WHERE at %L",
5840 /* the next statement within the same where-body-construct */
5841 cnext = cnext->next;
5843 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
5844 cblock = cblock->block;
5849 /* Traverse the FORALL body to check whether the following errors exist:
5850 1. For assignment, check if a many-to-one assignment happens.
5851 2. For WHERE statement, check the WHERE body to see if there is any
5852 many-to-one assignment. */
5855 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
5859 c = code->block->next;
5865 case EXEC_POINTER_ASSIGN:
5866 gfc_resolve_assign_in_forall (c, nvar, var_expr);
5869 case EXEC_ASSIGN_CALL:
5873 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
5874 there is no need to handle it here. */
5878 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
5883 /* The next statement in the FORALL body. */
5889 /* Given a FORALL construct, first resolve the FORALL iterator, then call
5890 gfc_resolve_forall_body to resolve the FORALL body. */
5893 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
5895 static gfc_expr **var_expr;
5896 static int total_var = 0;
5897 static int nvar = 0;
5898 gfc_forall_iterator *fa;
5902 /* Start to resolve a FORALL construct */
5903 if (forall_save == 0)
5905 /* Count the total number of FORALL index in the nested FORALL
5906 construct in order to allocate the VAR_EXPR with proper size. */
5908 while ((next != NULL) && (next->op == EXEC_FORALL))
5910 for (fa = next->ext.forall_iterator; fa; fa = fa->next)
5912 next = next->block->next;
5915 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
5916 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
5919 /* The information about FORALL iterator, including FORALL index start, end
5920 and stride. The FORALL index can not appear in start, end or stride. */
5921 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
5923 /* Check if any outer FORALL index name is the same as the current
5925 for (i = 0; i < nvar; i++)
5927 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
5929 gfc_error ("An outer FORALL construct already has an index "
5930 "with this name %L", &fa->var->where);
5934 /* Record the current FORALL index. */
5935 var_expr[nvar] = gfc_copy_expr (fa->var);
5940 /* Resolve the FORALL body. */
5941 gfc_resolve_forall_body (code, nvar, var_expr);
5943 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
5944 gfc_resolve_blocks (code->block, ns);
5946 /* Free VAR_EXPR after the whole FORALL construct resolved. */
5947 for (i = 0; i < total_var; i++)
5948 gfc_free_expr (var_expr[i]);
5950 /* Reset the counters. */
5956 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL ,GOTO and
5959 static void resolve_code (gfc_code *, gfc_namespace *);
5962 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
5966 for (; b; b = b->block)
5968 t = gfc_resolve_expr (b->expr);
5969 if (gfc_resolve_expr (b->expr2) == FAILURE)
5975 if (t == SUCCESS && b->expr != NULL
5976 && (b->expr->ts.type != BT_LOGICAL || b->expr->rank != 0))
5977 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
5984 && (b->expr->ts.type != BT_LOGICAL || b->expr->rank == 0))
5985 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
5990 resolve_branch (b->label, b);
6002 case EXEC_OMP_ATOMIC:
6003 case EXEC_OMP_CRITICAL:
6005 case EXEC_OMP_MASTER:
6006 case EXEC_OMP_ORDERED:
6007 case EXEC_OMP_PARALLEL:
6008 case EXEC_OMP_PARALLEL_DO:
6009 case EXEC_OMP_PARALLEL_SECTIONS:
6010 case EXEC_OMP_PARALLEL_WORKSHARE:
6011 case EXEC_OMP_SECTIONS:
6012 case EXEC_OMP_SINGLE:
6013 case EXEC_OMP_WORKSHARE:
6017 gfc_internal_error ("resolve_block(): Bad block type");
6020 resolve_code (b->next, ns);
6025 /* Does everything to resolve an ordinary assignment. Returns true
6026 if this is an interface asignment. */
6028 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
6038 if (gfc_extend_assign (code, ns) == SUCCESS)
6040 lhs = code->ext.actual->expr;
6041 rhs = code->ext.actual->next->expr;
6042 if (gfc_pure (NULL) && !gfc_pure (code->symtree->n.sym))
6044 gfc_error ("Subroutine '%s' called instead of assignment at "
6045 "%L must be PURE", code->symtree->n.sym->name,
6050 /* Make a temporary rhs when there is a default initializer
6051 and rhs is the same symbol as the lhs. */
6052 if (rhs->expr_type == EXPR_VARIABLE
6053 && rhs->symtree->n.sym->ts.type == BT_DERIVED
6054 && has_default_initializer (rhs->symtree->n.sym->ts.derived)
6055 && (lhs->symtree->n.sym == rhs->symtree->n.sym))
6056 code->ext.actual->next->expr = gfc_get_parentheses (rhs);
6065 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
6066 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
6067 &code->loc) == FAILURE)
6070 /* Handle the case of a BOZ literal on the RHS. */
6071 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
6074 if (gfc_option.warn_surprising)
6075 gfc_warning ("BOZ literal at %L is bitwise transferred "
6076 "non-integer symbol '%s'", &code->loc,
6077 lhs->symtree->n.sym->name);
6079 if (!gfc_convert_boz (rhs, &lhs->ts))
6081 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
6083 if (rc == ARITH_UNDERFLOW)
6084 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
6085 ". This check can be disabled with the option "
6086 "-fno-range-check", &rhs->where);
6087 else if (rc == ARITH_OVERFLOW)
6088 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
6089 ". This check can be disabled with the option "
6090 "-fno-range-check", &rhs->where);
6091 else if (rc == ARITH_NAN)
6092 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
6093 ". This check can be disabled with the option "
6094 "-fno-range-check", &rhs->where);
6100 if (lhs->ts.type == BT_CHARACTER
6101 && gfc_option.warn_character_truncation)
6103 if (lhs->ts.cl != NULL
6104 && lhs->ts.cl->length != NULL
6105 && lhs->ts.cl->length->expr_type == EXPR_CONSTANT)
6106 llen = mpz_get_si (lhs->ts.cl->length->value.integer);
6108 if (rhs->expr_type == EXPR_CONSTANT)
6109 rlen = rhs->value.character.length;
6111 else if (rhs->ts.cl != NULL
6112 && rhs->ts.cl->length != NULL
6113 && rhs->ts.cl->length->expr_type == EXPR_CONSTANT)
6114 rlen = mpz_get_si (rhs->ts.cl->length->value.integer);
6116 if (rlen && llen && rlen > llen)
6117 gfc_warning_now ("CHARACTER expression will be truncated "
6118 "in assignment (%d/%d) at %L",
6119 llen, rlen, &code->loc);
6122 /* Ensure that a vector index expression for the lvalue is evaluated
6123 to a temporary if the lvalue symbol is referenced in it. */
6126 for (ref = lhs->ref; ref; ref= ref->next)
6127 if (ref->type == REF_ARRAY)
6129 for (n = 0; n < ref->u.ar.dimen; n++)
6130 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
6131 && find_sym_in_expr (lhs->symtree->n.sym,
6132 ref->u.ar.start[n]))
6134 = gfc_get_parentheses (ref->u.ar.start[n]);
6138 if (gfc_pure (NULL))
6140 if (gfc_impure_variable (lhs->symtree->n.sym))
6142 gfc_error ("Cannot assign to variable '%s' in PURE "
6144 lhs->symtree->n.sym->name,
6149 if (lhs->ts.type == BT_DERIVED
6150 && lhs->expr_type == EXPR_VARIABLE
6151 && lhs->ts.derived->attr.pointer_comp
6152 && gfc_impure_variable (rhs->symtree->n.sym))
6154 gfc_error ("The impure variable at %L is assigned to "
6155 "a derived type variable with a POINTER "
6156 "component in a PURE procedure (12.6)",
6162 gfc_check_assign (lhs, rhs, 1);
6166 /* Given a block of code, recursively resolve everything pointed to by this
6170 resolve_code (gfc_code *code, gfc_namespace *ns)
6172 int omp_workshare_save;
6177 frame.prev = cs_base;
6181 reachable_labels (code);
6183 for (; code; code = code->next)
6185 frame.current = code;
6186 forall_save = forall_flag;
6188 if (code->op == EXEC_FORALL)
6191 gfc_resolve_forall (code, ns, forall_save);
6194 else if (code->block)
6196 omp_workshare_save = -1;
6199 case EXEC_OMP_PARALLEL_WORKSHARE:
6200 omp_workshare_save = omp_workshare_flag;
6201 omp_workshare_flag = 1;
6202 gfc_resolve_omp_parallel_blocks (code, ns);
6204 case EXEC_OMP_PARALLEL:
6205 case EXEC_OMP_PARALLEL_DO:
6206 case EXEC_OMP_PARALLEL_SECTIONS:
6207 omp_workshare_save = omp_workshare_flag;
6208 omp_workshare_flag = 0;
6209 gfc_resolve_omp_parallel_blocks (code, ns);
6212 gfc_resolve_omp_do_blocks (code, ns);
6214 case EXEC_OMP_WORKSHARE:
6215 omp_workshare_save = omp_workshare_flag;
6216 omp_workshare_flag = 1;
6219 gfc_resolve_blocks (code->block, ns);
6223 if (omp_workshare_save != -1)
6224 omp_workshare_flag = omp_workshare_save;
6227 t = gfc_resolve_expr (code->expr);
6228 forall_flag = forall_save;
6230 if (gfc_resolve_expr (code->expr2) == FAILURE)
6245 /* Keep track of which entry we are up to. */
6246 current_entry_id = code->ext.entry->id;
6250 resolve_where (code, NULL);
6254 if (code->expr != NULL)
6256 if (code->expr->ts.type != BT_INTEGER)
6257 gfc_error ("ASSIGNED GOTO statement at %L requires an "
6258 "INTEGER variable", &code->expr->where);
6259 else if (code->expr->symtree->n.sym->attr.assign != 1)
6260 gfc_error ("Variable '%s' has not been assigned a target "
6261 "label at %L", code->expr->symtree->n.sym->name,
6262 &code->expr->where);
6265 resolve_branch (code->label, code);
6269 if (code->expr != NULL
6270 && (code->expr->ts.type != BT_INTEGER || code->expr->rank))
6271 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
6272 "INTEGER return specifier", &code->expr->where);
6275 case EXEC_INIT_ASSIGN:
6282 if (resolve_ordinary_assign (code, ns))
6287 case EXEC_LABEL_ASSIGN:
6288 if (code->label->defined == ST_LABEL_UNKNOWN)
6289 gfc_error ("Label %d referenced at %L is never defined",
6290 code->label->value, &code->label->where);
6292 && (code->expr->expr_type != EXPR_VARIABLE
6293 || code->expr->symtree->n.sym->ts.type != BT_INTEGER
6294 || code->expr->symtree->n.sym->ts.kind
6295 != gfc_default_integer_kind
6296 || code->expr->symtree->n.sym->as != NULL))
6297 gfc_error ("ASSIGN statement at %L requires a scalar "
6298 "default INTEGER variable", &code->expr->where);
6301 case EXEC_POINTER_ASSIGN:
6305 gfc_check_pointer_assign (code->expr, code->expr2);
6308 case EXEC_ARITHMETIC_IF:
6310 && code->expr->ts.type != BT_INTEGER
6311 && code->expr->ts.type != BT_REAL)
6312 gfc_error ("Arithmetic IF statement at %L requires a numeric "
6313 "expression", &code->expr->where);
6315 resolve_branch (code->label, code);
6316 resolve_branch (code->label2, code);
6317 resolve_branch (code->label3, code);
6321 if (t == SUCCESS && code->expr != NULL
6322 && (code->expr->ts.type != BT_LOGICAL
6323 || code->expr->rank != 0))
6324 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
6325 &code->expr->where);
6330 resolve_call (code);
6334 /* Select is complicated. Also, a SELECT construct could be
6335 a transformed computed GOTO. */
6336 resolve_select (code);
6340 if (code->ext.iterator != NULL)
6342 gfc_iterator *iter = code->ext.iterator;
6343 if (gfc_resolve_iterator (iter, true) != FAILURE)
6344 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
6349 if (code->expr == NULL)
6350 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
6352 && (code->expr->rank != 0
6353 || code->expr->ts.type != BT_LOGICAL))
6354 gfc_error ("Exit condition of DO WHILE loop at %L must be "
6355 "a scalar LOGICAL expression", &code->expr->where);
6360 resolve_allocate_deallocate (code, "ALLOCATE");
6364 case EXEC_DEALLOCATE:
6366 resolve_allocate_deallocate (code, "DEALLOCATE");
6371 if (gfc_resolve_open (code->ext.open) == FAILURE)
6374 resolve_branch (code->ext.open->err, code);
6378 if (gfc_resolve_close (code->ext.close) == FAILURE)
6381 resolve_branch (code->ext.close->err, code);
6384 case EXEC_BACKSPACE:
6388 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
6391 resolve_branch (code->ext.filepos->err, code);
6395 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
6398 resolve_branch (code->ext.inquire->err, code);
6402 gcc_assert (code->ext.inquire != NULL);
6403 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
6406 resolve_branch (code->ext.inquire->err, code);
6411 if (gfc_resolve_dt (code->ext.dt) == FAILURE)
6414 resolve_branch (code->ext.dt->err, code);
6415 resolve_branch (code->ext.dt->end, code);
6416 resolve_branch (code->ext.dt->eor, code);
6420 resolve_transfer (code);
6424 resolve_forall_iterators (code->ext.forall_iterator);
6426 if (code->expr != NULL && code->expr->ts.type != BT_LOGICAL)
6427 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
6428 "expression", &code->expr->where);
6431 case EXEC_OMP_ATOMIC:
6432 case EXEC_OMP_BARRIER:
6433 case EXEC_OMP_CRITICAL:
6434 case EXEC_OMP_FLUSH:
6436 case EXEC_OMP_MASTER:
6437 case EXEC_OMP_ORDERED:
6438 case EXEC_OMP_SECTIONS:
6439 case EXEC_OMP_SINGLE:
6440 case EXEC_OMP_WORKSHARE:
6441 gfc_resolve_omp_directive (code, ns);
6444 case EXEC_OMP_PARALLEL:
6445 case EXEC_OMP_PARALLEL_DO:
6446 case EXEC_OMP_PARALLEL_SECTIONS:
6447 case EXEC_OMP_PARALLEL_WORKSHARE:
6448 omp_workshare_save = omp_workshare_flag;
6449 omp_workshare_flag = 0;
6450 gfc_resolve_omp_directive (code, ns);
6451 omp_workshare_flag = omp_workshare_save;
6455 gfc_internal_error ("resolve_code(): Bad statement code");
6459 cs_base = frame.prev;
6463 /* Resolve initial values and make sure they are compatible with
6467 resolve_values (gfc_symbol *sym)
6469 if (sym->value == NULL)
6472 if (gfc_resolve_expr (sym->value) == FAILURE)
6475 gfc_check_assign_symbol (sym, sym->value);
6479 /* Verify the binding labels for common blocks that are BIND(C). The label
6480 for a BIND(C) common block must be identical in all scoping units in which
6481 the common block is declared. Further, the binding label can not collide
6482 with any other global entity in the program. */
6485 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
6487 if (comm_block_tree->n.common->is_bind_c == 1)
6489 gfc_gsymbol *binding_label_gsym;
6490 gfc_gsymbol *comm_name_gsym;
6492 /* See if a global symbol exists by the common block's name. It may
6493 be NULL if the common block is use-associated. */
6494 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
6495 comm_block_tree->n.common->name);
6496 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
6497 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
6498 "with the global entity '%s' at %L",
6499 comm_block_tree->n.common->binding_label,
6500 comm_block_tree->n.common->name,
6501 &(comm_block_tree->n.common->where),
6502 comm_name_gsym->name, &(comm_name_gsym->where));
6503 else if (comm_name_gsym != NULL
6504 && strcmp (comm_name_gsym->name,
6505 comm_block_tree->n.common->name) == 0)
6507 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
6509 if (comm_name_gsym->binding_label == NULL)
6510 /* No binding label for common block stored yet; save this one. */
6511 comm_name_gsym->binding_label =
6512 comm_block_tree->n.common->binding_label;
6514 if (strcmp (comm_name_gsym->binding_label,
6515 comm_block_tree->n.common->binding_label) != 0)
6517 /* Common block names match but binding labels do not. */
6518 gfc_error ("Binding label '%s' for common block '%s' at %L "
6519 "does not match the binding label '%s' for common "
6521 comm_block_tree->n.common->binding_label,
6522 comm_block_tree->n.common->name,
6523 &(comm_block_tree->n.common->where),
6524 comm_name_gsym->binding_label,
6525 comm_name_gsym->name,
6526 &(comm_name_gsym->where));
6531 /* There is no binding label (NAME="") so we have nothing further to
6532 check and nothing to add as a global symbol for the label. */
6533 if (comm_block_tree->n.common->binding_label[0] == '\0' )
6536 binding_label_gsym =
6537 gfc_find_gsymbol (gfc_gsym_root,
6538 comm_block_tree->n.common->binding_label);
6539 if (binding_label_gsym == NULL)
6541 /* Need to make a global symbol for the binding label to prevent
6542 it from colliding with another. */
6543 binding_label_gsym =
6544 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
6545 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
6546 binding_label_gsym->type = GSYM_COMMON;
6550 /* If comm_name_gsym is NULL, the name common block is use
6551 associated and the name could be colliding. */
6552 if (binding_label_gsym->type != GSYM_COMMON)
6553 gfc_error ("Binding label '%s' for common block '%s' at %L "
6554 "collides with the global entity '%s' at %L",
6555 comm_block_tree->n.common->binding_label,
6556 comm_block_tree->n.common->name,
6557 &(comm_block_tree->n.common->where),
6558 binding_label_gsym->name,
6559 &(binding_label_gsym->where));
6560 else if (comm_name_gsym != NULL
6561 && (strcmp (binding_label_gsym->name,
6562 comm_name_gsym->binding_label) != 0)
6563 && (strcmp (binding_label_gsym->sym_name,
6564 comm_name_gsym->name) != 0))
6565 gfc_error ("Binding label '%s' for common block '%s' at %L "
6566 "collides with global entity '%s' at %L",
6567 binding_label_gsym->name, binding_label_gsym->sym_name,
6568 &(comm_block_tree->n.common->where),
6569 comm_name_gsym->name, &(comm_name_gsym->where));
6577 /* Verify any BIND(C) derived types in the namespace so we can report errors
6578 for them once, rather than for each variable declared of that type. */
6581 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
6583 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
6584 && derived_sym->attr.is_bind_c == 1)
6585 verify_bind_c_derived_type (derived_sym);
6591 /* Verify that any binding labels used in a given namespace do not collide
6592 with the names or binding labels of any global symbols. */
6595 gfc_verify_binding_labels (gfc_symbol *sym)
6599 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
6600 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
6602 gfc_gsymbol *bind_c_sym;
6604 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
6605 if (bind_c_sym != NULL
6606 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
6608 if (sym->attr.if_source == IFSRC_DECL
6609 && (bind_c_sym->type != GSYM_SUBROUTINE
6610 && bind_c_sym->type != GSYM_FUNCTION)
6611 && ((sym->attr.contained == 1
6612 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
6613 || (sym->attr.use_assoc == 1
6614 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
6616 /* Make sure global procedures don't collide with anything. */
6617 gfc_error ("Binding label '%s' at %L collides with the global "
6618 "entity '%s' at %L", sym->binding_label,
6619 &(sym->declared_at), bind_c_sym->name,
6620 &(bind_c_sym->where));
6623 else if (sym->attr.contained == 0
6624 && (sym->attr.if_source == IFSRC_IFBODY
6625 && sym->attr.flavor == FL_PROCEDURE)
6626 && (bind_c_sym->sym_name != NULL
6627 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
6629 /* Make sure procedures in interface bodies don't collide. */
6630 gfc_error ("Binding label '%s' in interface body at %L collides "
6631 "with the global entity '%s' at %L",
6633 &(sym->declared_at), bind_c_sym->name,
6634 &(bind_c_sym->where));
6637 else if (sym->attr.contained == 0
6638 && (sym->attr.if_source == IFSRC_UNKNOWN))
6639 if ((sym->attr.use_assoc
6640 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))
6641 || sym->attr.use_assoc == 0)
6643 gfc_error ("Binding label '%s' at %L collides with global "
6644 "entity '%s' at %L", sym->binding_label,
6645 &(sym->declared_at), bind_c_sym->name,
6646 &(bind_c_sym->where));
6651 /* Clear the binding label to prevent checking multiple times. */
6652 sym->binding_label[0] = '\0';
6654 else if (bind_c_sym == NULL)
6656 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
6657 bind_c_sym->where = sym->declared_at;
6658 bind_c_sym->sym_name = sym->name;
6660 if (sym->attr.use_assoc == 1)
6661 bind_c_sym->mod_name = sym->module;
6663 if (sym->ns->proc_name != NULL)
6664 bind_c_sym->mod_name = sym->ns->proc_name->name;
6666 if (sym->attr.contained == 0)
6668 if (sym->attr.subroutine)
6669 bind_c_sym->type = GSYM_SUBROUTINE;
6670 else if (sym->attr.function)
6671 bind_c_sym->type = GSYM_FUNCTION;
6679 /* Resolve an index expression. */
6682 resolve_index_expr (gfc_expr *e)
6684 if (gfc_resolve_expr (e) == FAILURE)
6687 if (gfc_simplify_expr (e, 0) == FAILURE)
6690 if (gfc_specification_expr (e) == FAILURE)
6696 /* Resolve a charlen structure. */
6699 resolve_charlen (gfc_charlen *cl)
6708 specification_expr = 1;
6710 if (resolve_index_expr (cl->length) == FAILURE)
6712 specification_expr = 0;
6716 /* "If the character length parameter value evaluates to a negative
6717 value, the length of character entities declared is zero." */
6718 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
6720 gfc_warning_now ("CHARACTER variable has zero length at %L",
6721 &cl->length->where);
6722 gfc_replace_expr (cl->length, gfc_int_expr (0));
6729 /* Test for non-constant shape arrays. */
6732 is_non_constant_shape_array (gfc_symbol *sym)
6738 not_constant = false;
6739 if (sym->as != NULL)
6741 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
6742 has not been simplified; parameter array references. Do the
6743 simplification now. */
6744 for (i = 0; i < sym->as->rank; i++)
6746 e = sym->as->lower[i];
6747 if (e && (resolve_index_expr (e) == FAILURE
6748 || !gfc_is_constant_expr (e)))
6749 not_constant = true;
6751 e = sym->as->upper[i];
6752 if (e && (resolve_index_expr (e) == FAILURE
6753 || !gfc_is_constant_expr (e)))
6754 not_constant = true;
6757 return not_constant;
6760 /* Given a symbol and an initialization expression, add code to initialize
6761 the symbol to the function entry. */
6763 build_init_assign (gfc_symbol *sym, gfc_expr *init)
6767 gfc_namespace *ns = sym->ns;
6769 /* Search for the function namespace if this is a contained
6770 function without an explicit result. */
6771 if (sym->attr.function && sym == sym->result
6772 && sym->name != sym->ns->proc_name->name)
6775 for (;ns; ns = ns->sibling)
6776 if (strcmp (ns->proc_name->name, sym->name) == 0)
6782 gfc_free_expr (init);
6786 /* Build an l-value expression for the result. */
6787 lval = gfc_lval_expr_from_sym (sym);
6789 /* Add the code at scope entry. */
6790 init_st = gfc_get_code ();
6791 init_st->next = ns->code;
6794 /* Assign the default initializer to the l-value. */
6795 init_st->loc = sym->declared_at;
6796 init_st->op = EXEC_INIT_ASSIGN;
6797 init_st->expr = lval;
6798 init_st->expr2 = init;
6801 /* Assign the default initializer to a derived type variable or result. */
6804 apply_default_init (gfc_symbol *sym)
6806 gfc_expr *init = NULL;
6808 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
6811 if (sym->ts.type == BT_DERIVED && sym->ts.derived)
6812 init = gfc_default_initializer (&sym->ts);
6817 build_init_assign (sym, init);
6820 /* Build an initializer for a local integer, real, complex, logical, or
6821 character variable, based on the command line flags finit-local-zero,
6822 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
6823 null if the symbol should not have a default initialization. */
6825 build_default_init_expr (gfc_symbol *sym)
6828 gfc_expr *init_expr;
6832 /* These symbols should never have a default initialization. */
6833 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
6834 || sym->attr.external
6836 || sym->attr.pointer
6837 || sym->attr.in_equivalence
6838 || sym->attr.in_common
6841 || sym->attr.cray_pointee
6842 || sym->attr.cray_pointer)
6845 /* Now we'll try to build an initializer expression. */
6846 init_expr = gfc_get_expr ();
6847 init_expr->expr_type = EXPR_CONSTANT;
6848 init_expr->ts.type = sym->ts.type;
6849 init_expr->ts.kind = sym->ts.kind;
6850 init_expr->where = sym->declared_at;
6852 /* We will only initialize integers, reals, complex, logicals, and
6853 characters, and only if the corresponding command-line flags
6854 were set. Otherwise, we free init_expr and return null. */
6855 switch (sym->ts.type)
6858 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
6859 mpz_init_set_si (init_expr->value.integer,
6860 gfc_option.flag_init_integer_value);
6863 gfc_free_expr (init_expr);
6869 mpfr_init (init_expr->value.real);
6870 switch (gfc_option.flag_init_real)
6872 case GFC_INIT_REAL_NAN:
6873 mpfr_set_nan (init_expr->value.real);
6876 case GFC_INIT_REAL_INF:
6877 mpfr_set_inf (init_expr->value.real, 1);
6880 case GFC_INIT_REAL_NEG_INF:
6881 mpfr_set_inf (init_expr->value.real, -1);
6884 case GFC_INIT_REAL_ZERO:
6885 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
6889 gfc_free_expr (init_expr);
6896 mpfr_init (init_expr->value.complex.r);
6897 mpfr_init (init_expr->value.complex.i);
6898 switch (gfc_option.flag_init_real)
6900 case GFC_INIT_REAL_NAN:
6901 mpfr_set_nan (init_expr->value.complex.r);
6902 mpfr_set_nan (init_expr->value.complex.i);
6905 case GFC_INIT_REAL_INF:
6906 mpfr_set_inf (init_expr->value.complex.r, 1);
6907 mpfr_set_inf (init_expr->value.complex.i, 1);
6910 case GFC_INIT_REAL_NEG_INF:
6911 mpfr_set_inf (init_expr->value.complex.r, -1);
6912 mpfr_set_inf (init_expr->value.complex.i, -1);
6915 case GFC_INIT_REAL_ZERO:
6916 mpfr_set_ui (init_expr->value.complex.r, 0.0, GFC_RND_MODE);
6917 mpfr_set_ui (init_expr->value.complex.i, 0.0, GFC_RND_MODE);
6921 gfc_free_expr (init_expr);
6928 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
6929 init_expr->value.logical = 0;
6930 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
6931 init_expr->value.logical = 1;
6934 gfc_free_expr (init_expr);
6940 /* For characters, the length must be constant in order to
6941 create a default initializer. */
6942 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
6943 && sym->ts.cl->length
6944 && sym->ts.cl->length->expr_type == EXPR_CONSTANT)
6946 char_len = mpz_get_si (sym->ts.cl->length->value.integer);
6947 init_expr->value.character.length = char_len;
6948 init_expr->value.character.string = gfc_getmem (char_len+1);
6949 ch = init_expr->value.character.string;
6950 for (i = 0; i < char_len; i++)
6951 *(ch++) = gfc_option.flag_init_character_value;
6955 gfc_free_expr (init_expr);
6961 gfc_free_expr (init_expr);
6967 /* Add an initialization expression to a local variable. */
6969 apply_default_init_local (gfc_symbol *sym)
6971 gfc_expr *init = NULL;
6973 /* The symbol should be a variable or a function return value. */
6974 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
6975 || (sym->attr.function && sym->result != sym))
6978 /* Try to build the initializer expression. If we can't initialize
6979 this symbol, then init will be NULL. */
6980 init = build_default_init_expr (sym);
6984 /* For saved variables, we don't want to add an initializer at
6985 function entry, so we just add a static initializer. */
6986 if (sym->attr.save || sym->ns->save_all)
6988 /* Don't clobber an existing initializer! */
6989 gcc_assert (sym->value == NULL);
6994 build_init_assign (sym, init);
6997 /* Resolution of common features of flavors variable and procedure. */
7000 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
7002 /* Constraints on deferred shape variable. */
7003 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
7005 if (sym->attr.allocatable)
7007 if (sym->attr.dimension)
7008 gfc_error ("Allocatable array '%s' at %L must have "
7009 "a deferred shape", sym->name, &sym->declared_at);
7011 gfc_error ("Scalar object '%s' at %L may not be ALLOCATABLE",
7012 sym->name, &sym->declared_at);
7016 if (sym->attr.pointer && sym->attr.dimension)
7018 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
7019 sym->name, &sym->declared_at);
7026 if (!mp_flag && !sym->attr.allocatable
7027 && !sym->attr.pointer && !sym->attr.dummy)
7029 gfc_error ("Array '%s' at %L cannot have a deferred shape",
7030 sym->name, &sym->declared_at);
7038 /* Additional checks for symbols with flavor variable and derived
7039 type. To be called from resolve_fl_variable. */
7042 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
7044 gcc_assert (sym->ts.type == BT_DERIVED);
7046 /* Check to see if a derived type is blocked from being host
7047 associated by the presence of another class I symbol in the same
7048 namespace. 14.6.1.3 of the standard and the discussion on
7049 comp.lang.fortran. */
7050 if (sym->ns != sym->ts.derived->ns
7051 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
7054 gfc_find_symbol (sym->ts.derived->name, sym->ns, 0, &s);
7055 if (s && (s->attr.flavor != FL_DERIVED
7056 || !gfc_compare_derived_types (s, sym->ts.derived)))
7058 gfc_error ("The type '%s' cannot be host associated at %L "
7059 "because it is blocked by an incompatible object "
7060 "of the same name declared at %L",
7061 sym->ts.derived->name, &sym->declared_at,
7067 /* 4th constraint in section 11.3: "If an object of a type for which
7068 component-initialization is specified (R429) appears in the
7069 specification-part of a module and does not have the ALLOCATABLE
7070 or POINTER attribute, the object shall have the SAVE attribute."
7072 The check for initializers is performed with
7073 has_default_initializer because gfc_default_initializer generates
7074 a hidden default for allocatable components. */
7075 if (!(sym->value || no_init_flag) && sym->ns->proc_name
7076 && sym->ns->proc_name->attr.flavor == FL_MODULE
7077 && !sym->ns->save_all && !sym->attr.save
7078 && !sym->attr.pointer && !sym->attr.allocatable
7079 && has_default_initializer (sym->ts.derived))
7081 gfc_error("Object '%s' at %L must have the SAVE attribute for "
7082 "default initialization of a component",
7083 sym->name, &sym->declared_at);
7087 /* Assign default initializer. */
7088 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
7089 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
7091 sym->value = gfc_default_initializer (&sym->ts);
7098 /* Resolve symbols with flavor variable. */
7101 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
7103 int no_init_flag, automatic_flag;
7105 const char *auto_save_msg;
7107 auto_save_msg = "Automatic object '%s' at %L cannot have the "
7110 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
7113 /* Set this flag to check that variables are parameters of all entries.
7114 This check is effected by the call to gfc_resolve_expr through
7115 is_non_constant_shape_array. */
7116 specification_expr = 1;
7118 if (sym->ns->proc_name
7119 && (sym->ns->proc_name->attr.flavor == FL_MODULE
7120 || sym->ns->proc_name->attr.is_main_program)
7121 && !sym->attr.use_assoc
7122 && !sym->attr.allocatable
7123 && !sym->attr.pointer
7124 && is_non_constant_shape_array (sym))
7126 /* The shape of a main program or module array needs to be
7128 gfc_error ("The module or main program array '%s' at %L must "
7129 "have constant shape", sym->name, &sym->declared_at);
7130 specification_expr = 0;
7134 if (sym->ts.type == BT_CHARACTER)
7136 /* Make sure that character string variables with assumed length are
7138 e = sym->ts.cl->length;
7139 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
7141 gfc_error ("Entity with assumed character length at %L must be a "
7142 "dummy argument or a PARAMETER", &sym->declared_at);
7146 if (e && sym->attr.save && !gfc_is_constant_expr (e))
7148 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
7152 if (!gfc_is_constant_expr (e)
7153 && !(e->expr_type == EXPR_VARIABLE
7154 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
7155 && sym->ns->proc_name
7156 && (sym->ns->proc_name->attr.flavor == FL_MODULE
7157 || sym->ns->proc_name->attr.is_main_program)
7158 && !sym->attr.use_assoc)
7160 gfc_error ("'%s' at %L must have constant character length "
7161 "in this context", sym->name, &sym->declared_at);
7166 if (sym->value == NULL && sym->attr.referenced)
7167 apply_default_init_local (sym); /* Try to apply a default initialization. */
7169 /* Determine if the symbol may not have an initializer. */
7170 no_init_flag = automatic_flag = 0;
7171 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
7172 || sym->attr.intrinsic || sym->attr.result)
7174 else if (sym->attr.dimension && !sym->attr.pointer
7175 && is_non_constant_shape_array (sym))
7177 no_init_flag = automatic_flag = 1;
7179 /* Also, they must not have the SAVE attribute.
7180 SAVE_IMPLICIT is checked below. */
7181 if (sym->attr.save == SAVE_EXPLICIT)
7183 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
7188 /* Reject illegal initializers. */
7189 if (!sym->mark && sym->value)
7191 if (sym->attr.allocatable)
7192 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
7193 sym->name, &sym->declared_at);
7194 else if (sym->attr.external)
7195 gfc_error ("External '%s' at %L cannot have an initializer",
7196 sym->name, &sym->declared_at);
7197 else if (sym->attr.dummy
7198 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
7199 gfc_error ("Dummy '%s' at %L cannot have an initializer",
7200 sym->name, &sym->declared_at);
7201 else if (sym->attr.intrinsic)
7202 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
7203 sym->name, &sym->declared_at);
7204 else if (sym->attr.result)
7205 gfc_error ("Function result '%s' at %L cannot have an initializer",
7206 sym->name, &sym->declared_at);
7207 else if (automatic_flag)
7208 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
7209 sym->name, &sym->declared_at);
7216 if (sym->ts.type == BT_DERIVED)
7217 return resolve_fl_variable_derived (sym, no_init_flag);
7223 /* Resolve a procedure. */
7226 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
7228 gfc_formal_arglist *arg;
7230 if (sym->attr.ambiguous_interfaces && !sym->attr.referenced)
7231 gfc_warning ("Although not referenced, '%s' at %L has ambiguous "
7232 "interfaces", sym->name, &sym->declared_at);
7234 if (sym->attr.function
7235 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
7238 if (sym->ts.type == BT_CHARACTER)
7240 gfc_charlen *cl = sym->ts.cl;
7242 if (cl && cl->length && gfc_is_constant_expr (cl->length)
7243 && resolve_charlen (cl) == FAILURE)
7246 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
7248 if (sym->attr.proc == PROC_ST_FUNCTION)
7250 gfc_error ("Character-valued statement function '%s' at %L must "
7251 "have constant length", sym->name, &sym->declared_at);
7255 if (sym->attr.external && sym->formal == NULL
7256 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
7258 gfc_error ("Automatic character length function '%s' at %L must "
7259 "have an explicit interface", sym->name,
7266 /* Ensure that derived type for are not of a private type. Internal
7267 module procedures are excluded by 2.2.3.3 - ie. they are not
7268 externally accessible and can access all the objects accessible in
7270 if (!(sym->ns->parent
7271 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
7272 && gfc_check_access(sym->attr.access, sym->ns->default_access))
7274 gfc_interface *iface;
7276 for (arg = sym->formal; arg; arg = arg->next)
7279 && arg->sym->ts.type == BT_DERIVED
7280 && !arg->sym->ts.derived->attr.use_assoc
7281 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7282 arg->sym->ts.derived->ns->default_access)
7283 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
7284 "PRIVATE type and cannot be a dummy argument"
7285 " of '%s', which is PUBLIC at %L",
7286 arg->sym->name, sym->name, &sym->declared_at)
7289 /* Stop this message from recurring. */
7290 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7295 /* PUBLIC interfaces may expose PRIVATE procedures that take types
7296 PRIVATE to the containing module. */
7297 for (iface = sym->generic; iface; iface = iface->next)
7299 for (arg = iface->sym->formal; arg; arg = arg->next)
7302 && arg->sym->ts.type == BT_DERIVED
7303 && !arg->sym->ts.derived->attr.use_assoc
7304 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7305 arg->sym->ts.derived->ns->default_access)
7306 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
7307 "'%s' in PUBLIC interface '%s' at %L "
7308 "takes dummy arguments of '%s' which is "
7309 "PRIVATE", iface->sym->name, sym->name,
7310 &iface->sym->declared_at,
7311 gfc_typename (&arg->sym->ts)) == FAILURE)
7313 /* Stop this message from recurring. */
7314 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7320 /* PUBLIC interfaces may expose PRIVATE procedures that take types
7321 PRIVATE to the containing module. */
7322 for (iface = sym->generic; iface; iface = iface->next)
7324 for (arg = iface->sym->formal; arg; arg = arg->next)
7327 && arg->sym->ts.type == BT_DERIVED
7328 && !arg->sym->ts.derived->attr.use_assoc
7329 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7330 arg->sym->ts.derived->ns->default_access)
7331 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
7332 "'%s' in PUBLIC interface '%s' at %L "
7333 "takes dummy arguments of '%s' which is "
7334 "PRIVATE", iface->sym->name, sym->name,
7335 &iface->sym->declared_at,
7336 gfc_typename (&arg->sym->ts)) == FAILURE)
7338 /* Stop this message from recurring. */
7339 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7346 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION)
7348 gfc_error ("Function '%s' at %L cannot have an initializer",
7349 sym->name, &sym->declared_at);
7353 /* An external symbol may not have an initializer because it is taken to be
7355 if (sym->attr.external && sym->value)
7357 gfc_error ("External object '%s' at %L may not have an initializer",
7358 sym->name, &sym->declared_at);
7362 /* An elemental function is required to return a scalar 12.7.1 */
7363 if (sym->attr.elemental && sym->attr.function && sym->as)
7365 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
7366 "result", sym->name, &sym->declared_at);
7367 /* Reset so that the error only occurs once. */
7368 sym->attr.elemental = 0;
7372 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
7373 char-len-param shall not be array-valued, pointer-valued, recursive
7374 or pure. ....snip... A character value of * may only be used in the
7375 following ways: (i) Dummy arg of procedure - dummy associates with
7376 actual length; (ii) To declare a named constant; or (iii) External
7377 function - but length must be declared in calling scoping unit. */
7378 if (sym->attr.function
7379 && sym->ts.type == BT_CHARACTER
7380 && sym->ts.cl && sym->ts.cl->length == NULL)
7382 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
7383 || (sym->attr.recursive) || (sym->attr.pure))
7385 if (sym->as && sym->as->rank)
7386 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7387 "array-valued", sym->name, &sym->declared_at);
7389 if (sym->attr.pointer)
7390 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7391 "pointer-valued", sym->name, &sym->declared_at);
7394 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7395 "pure", sym->name, &sym->declared_at);
7397 if (sym->attr.recursive)
7398 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7399 "recursive", sym->name, &sym->declared_at);
7404 /* Appendix B.2 of the standard. Contained functions give an
7405 error anyway. Fixed-form is likely to be F77/legacy. */
7406 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
7407 gfc_notify_std (GFC_STD_F95_OBS, "CHARACTER(*) function "
7408 "'%s' at %L is obsolescent in fortran 95",
7409 sym->name, &sym->declared_at);
7412 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
7414 gfc_formal_arglist *curr_arg;
7415 int has_non_interop_arg = 0;
7417 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
7418 sym->common_block) == FAILURE)
7420 /* Clear these to prevent looking at them again if there was an
7422 sym->attr.is_bind_c = 0;
7423 sym->attr.is_c_interop = 0;
7424 sym->ts.is_c_interop = 0;
7428 /* So far, no errors have been found. */
7429 sym->attr.is_c_interop = 1;
7430 sym->ts.is_c_interop = 1;
7433 curr_arg = sym->formal;
7434 while (curr_arg != NULL)
7436 /* Skip implicitly typed dummy args here. */
7437 if (curr_arg->sym->attr.implicit_type == 0)
7438 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
7439 /* If something is found to fail, record the fact so we
7440 can mark the symbol for the procedure as not being
7441 BIND(C) to try and prevent multiple errors being
7443 has_non_interop_arg = 1;
7445 curr_arg = curr_arg->next;
7448 /* See if any of the arguments were not interoperable and if so, clear
7449 the procedure symbol to prevent duplicate error messages. */
7450 if (has_non_interop_arg != 0)
7452 sym->attr.is_c_interop = 0;
7453 sym->ts.is_c_interop = 0;
7454 sym->attr.is_bind_c = 0;
7462 /* Resolve the components of a derived type. */
7465 resolve_fl_derived (gfc_symbol *sym)
7468 gfc_dt_list * dt_list;
7471 for (c = sym->components; c != NULL; c = c->next)
7473 if (c->ts.type == BT_CHARACTER)
7475 if (c->ts.cl->length == NULL
7476 || (resolve_charlen (c->ts.cl) == FAILURE)
7477 || !gfc_is_constant_expr (c->ts.cl->length))
7479 gfc_error ("Character length of component '%s' needs to "
7480 "be a constant specification expression at %L",
7482 c->ts.cl->length ? &c->ts.cl->length->where : &c->loc);
7487 if (c->ts.type == BT_DERIVED
7488 && sym->component_access != ACCESS_PRIVATE
7489 && gfc_check_access (sym->attr.access, sym->ns->default_access)
7490 && !c->ts.derived->attr.use_assoc
7491 && !gfc_check_access (c->ts.derived->attr.access,
7492 c->ts.derived->ns->default_access))
7494 gfc_error ("The component '%s' is a PRIVATE type and cannot be "
7495 "a component of '%s', which is PUBLIC at %L",
7496 c->name, sym->name, &sym->declared_at);
7500 if (sym->attr.sequence)
7502 if (c->ts.type == BT_DERIVED && c->ts.derived->attr.sequence == 0)
7504 gfc_error ("Component %s of SEQUENCE type declared at %L does "
7505 "not have the SEQUENCE attribute",
7506 c->ts.derived->name, &sym->declared_at);
7511 if (c->ts.type == BT_DERIVED && c->pointer
7512 && c->ts.derived->components == NULL)
7514 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
7515 "that has not been declared", c->name, sym->name,
7520 if (c->pointer || c->allocatable || c->as == NULL)
7523 for (i = 0; i < c->as->rank; i++)
7525 if (c->as->lower[i] == NULL
7526 || !gfc_is_constant_expr (c->as->lower[i])
7527 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
7528 || c->as->upper[i] == NULL
7529 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
7530 || !gfc_is_constant_expr (c->as->upper[i]))
7532 gfc_error ("Component '%s' of '%s' at %L must have "
7533 "constant array bounds",
7534 c->name, sym->name, &c->loc);
7540 /* Add derived type to the derived type list. */
7541 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
7542 if (sym == dt_list->derived)
7545 if (dt_list == NULL)
7547 dt_list = gfc_get_dt_list ();
7548 dt_list->next = gfc_derived_types;
7549 dt_list->derived = sym;
7550 gfc_derived_types = dt_list;
7558 resolve_fl_namelist (gfc_symbol *sym)
7563 /* Reject PRIVATE objects in a PUBLIC namelist. */
7564 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
7566 for (nl = sym->namelist; nl; nl = nl->next)
7568 if (!nl->sym->attr.use_assoc
7569 && !(sym->ns->parent == nl->sym->ns)
7570 && !(sym->ns->parent
7571 && sym->ns->parent->parent == nl->sym->ns)
7572 && !gfc_check_access(nl->sym->attr.access,
7573 nl->sym->ns->default_access))
7575 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
7576 "cannot be member of PUBLIC namelist '%s' at %L",
7577 nl->sym->name, sym->name, &sym->declared_at);
7581 /* Types with private components that came here by USE-association. */
7582 if (nl->sym->ts.type == BT_DERIVED
7583 && derived_inaccessible (nl->sym->ts.derived))
7585 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
7586 "components and cannot be member of namelist '%s' at %L",
7587 nl->sym->name, sym->name, &sym->declared_at);
7591 /* Types with private components that are defined in the same module. */
7592 if (nl->sym->ts.type == BT_DERIVED
7593 && !(sym->ns->parent == nl->sym->ts.derived->ns)
7594 && !gfc_check_access (nl->sym->ts.derived->attr.private_comp
7595 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
7596 nl->sym->ns->default_access))
7598 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
7599 "cannot be a member of PUBLIC namelist '%s' at %L",
7600 nl->sym->name, sym->name, &sym->declared_at);
7606 for (nl = sym->namelist; nl; nl = nl->next)
7608 /* Reject namelist arrays of assumed shape. */
7609 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
7610 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
7611 "must not have assumed shape in namelist "
7612 "'%s' at %L", nl->sym->name, sym->name,
7613 &sym->declared_at) == FAILURE)
7616 /* Reject namelist arrays that are not constant shape. */
7617 if (is_non_constant_shape_array (nl->sym))
7619 gfc_error ("NAMELIST array object '%s' must have constant "
7620 "shape in namelist '%s' at %L", nl->sym->name,
7621 sym->name, &sym->declared_at);
7625 /* Namelist objects cannot have allocatable or pointer components. */
7626 if (nl->sym->ts.type != BT_DERIVED)
7629 if (nl->sym->ts.derived->attr.alloc_comp)
7631 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
7632 "have ALLOCATABLE components",
7633 nl->sym->name, sym->name, &sym->declared_at);
7637 if (nl->sym->ts.derived->attr.pointer_comp)
7639 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
7640 "have POINTER components",
7641 nl->sym->name, sym->name, &sym->declared_at);
7647 /* 14.1.2 A module or internal procedure represent local entities
7648 of the same type as a namelist member and so are not allowed. */
7649 for (nl = sym->namelist; nl; nl = nl->next)
7651 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
7654 if (nl->sym->attr.function && nl->sym == nl->sym->result)
7655 if ((nl->sym == sym->ns->proc_name)
7657 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
7661 if (nl->sym && nl->sym->name)
7662 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
7663 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
7665 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
7666 "attribute in '%s' at %L", nlsym->name,
7677 resolve_fl_parameter (gfc_symbol *sym)
7679 /* A parameter array's shape needs to be constant. */
7681 && (sym->as->type == AS_DEFERRED
7682 || is_non_constant_shape_array (sym)))
7684 gfc_error ("Parameter array '%s' at %L cannot be automatic "
7685 "or of deferred shape", sym->name, &sym->declared_at);
7689 /* Make sure a parameter that has been implicitly typed still
7690 matches the implicit type, since PARAMETER statements can precede
7691 IMPLICIT statements. */
7692 if (sym->attr.implicit_type
7693 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym, sym->ns)))
7695 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
7696 "later IMPLICIT type", sym->name, &sym->declared_at);
7700 /* Make sure the types of derived parameters are consistent. This
7701 type checking is deferred until resolution because the type may
7702 refer to a derived type from the host. */
7703 if (sym->ts.type == BT_DERIVED
7704 && !gfc_compare_types (&sym->ts, &sym->value->ts))
7706 gfc_error ("Incompatible derived type in PARAMETER at %L",
7707 &sym->value->where);
7714 /* Do anything necessary to resolve a symbol. Right now, we just
7715 assume that an otherwise unknown symbol is a variable. This sort
7716 of thing commonly happens for symbols in module. */
7719 resolve_symbol (gfc_symbol *sym)
7721 int check_constant, mp_flag;
7722 gfc_symtree *symtree;
7723 gfc_symtree *this_symtree;
7727 if (sym->attr.flavor == FL_UNKNOWN)
7730 /* If we find that a flavorless symbol is an interface in one of the
7731 parent namespaces, find its symtree in this namespace, free the
7732 symbol and set the symtree to point to the interface symbol. */
7733 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
7735 symtree = gfc_find_symtree (ns->sym_root, sym->name);
7736 if (symtree && symtree->n.sym->generic)
7738 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
7742 gfc_free_symbol (sym);
7743 symtree->n.sym->refs++;
7744 this_symtree->n.sym = symtree->n.sym;
7749 /* Otherwise give it a flavor according to such attributes as
7751 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
7752 sym->attr.flavor = FL_VARIABLE;
7755 sym->attr.flavor = FL_PROCEDURE;
7756 if (sym->attr.dimension)
7757 sym->attr.function = 1;
7761 if (sym->attr.procedure && sym->interface
7762 && sym->attr.if_source != IFSRC_DECL)
7764 if (sym->interface->attr.procedure)
7765 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared "
7766 "in a later PROCEDURE statement", sym->interface->name,
7767 sym->name,&sym->declared_at);
7769 /* Get the attributes from the interface (now resolved). */
7770 if (sym->interface->attr.if_source || sym->interface->attr.intrinsic)
7772 sym->ts = sym->interface->ts;
7773 sym->attr.function = sym->interface->attr.function;
7774 sym->attr.subroutine = sym->interface->attr.subroutine;
7775 copy_formal_args (sym, sym->interface);
7777 else if (sym->interface->name[0] != '\0')
7779 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
7780 sym->interface->name, sym->name, &sym->declared_at);
7785 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
7788 /* Symbols that are module procedures with results (functions) have
7789 the types and array specification copied for type checking in
7790 procedures that call them, as well as for saving to a module
7791 file. These symbols can't stand the scrutiny that their results
7793 mp_flag = (sym->result != NULL && sym->result != sym);
7796 /* Make sure that the intrinsic is consistent with its internal
7797 representation. This needs to be done before assigning a default
7798 type to avoid spurious warnings. */
7799 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic)
7801 if (gfc_intrinsic_name (sym->name, 0))
7803 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising)
7804 gfc_warning ("Type specified for intrinsic function '%s' at %L is ignored",
7805 sym->name, &sym->declared_at);
7807 else if (gfc_intrinsic_name (sym->name, 1))
7809 if (sym->ts.type != BT_UNKNOWN)
7811 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type specifier",
7812 sym->name, &sym->declared_at);
7818 gfc_error ("Intrinsic '%s' at %L does not exist", sym->name, &sym->declared_at);
7823 /* Assign default type to symbols that need one and don't have one. */
7824 if (sym->ts.type == BT_UNKNOWN)
7826 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
7827 gfc_set_default_type (sym, 1, NULL);
7829 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
7831 /* The specific case of an external procedure should emit an error
7832 in the case that there is no implicit type. */
7834 gfc_set_default_type (sym, sym->attr.external, NULL);
7837 /* Result may be in another namespace. */
7838 resolve_symbol (sym->result);
7840 sym->ts = sym->result->ts;
7841 sym->as = gfc_copy_array_spec (sym->result->as);
7842 sym->attr.dimension = sym->result->attr.dimension;
7843 sym->attr.pointer = sym->result->attr.pointer;
7844 sym->attr.allocatable = sym->result->attr.allocatable;
7849 /* Assumed size arrays and assumed shape arrays must be dummy
7853 && (sym->as->type == AS_ASSUMED_SIZE
7854 || sym->as->type == AS_ASSUMED_SHAPE)
7855 && sym->attr.dummy == 0)
7857 if (sym->as->type == AS_ASSUMED_SIZE)
7858 gfc_error ("Assumed size array at %L must be a dummy argument",
7861 gfc_error ("Assumed shape array at %L must be a dummy argument",
7866 /* Make sure symbols with known intent or optional are really dummy
7867 variable. Because of ENTRY statement, this has to be deferred
7868 until resolution time. */
7870 if (!sym->attr.dummy
7871 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
7873 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
7877 if (sym->attr.value && !sym->attr.dummy)
7879 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
7880 "it is not a dummy argument", sym->name, &sym->declared_at);
7884 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
7886 gfc_charlen *cl = sym->ts.cl;
7887 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
7889 gfc_error ("Character dummy variable '%s' at %L with VALUE "
7890 "attribute must have constant length",
7891 sym->name, &sym->declared_at);
7895 if (sym->ts.is_c_interop
7896 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
7898 gfc_error ("C interoperable character dummy variable '%s' at %L "
7899 "with VALUE attribute must have length one",
7900 sym->name, &sym->declared_at);
7905 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
7906 do this for something that was implicitly typed because that is handled
7907 in gfc_set_default_type. Handle dummy arguments and procedure
7908 definitions separately. Also, anything that is use associated is not
7909 handled here but instead is handled in the module it is declared in.
7910 Finally, derived type definitions are allowed to be BIND(C) since that
7911 only implies that they're interoperable, and they are checked fully for
7912 interoperability when a variable is declared of that type. */
7913 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
7914 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
7915 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
7919 /* First, make sure the variable is declared at the
7920 module-level scope (J3/04-007, Section 15.3). */
7921 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
7922 sym->attr.in_common == 0)
7924 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
7925 "is neither a COMMON block nor declared at the "
7926 "module level scope", sym->name, &(sym->declared_at));
7929 else if (sym->common_head != NULL)
7931 t = verify_com_block_vars_c_interop (sym->common_head);
7935 /* If type() declaration, we need to verify that the components
7936 of the given type are all C interoperable, etc. */
7937 if (sym->ts.type == BT_DERIVED &&
7938 sym->ts.derived->attr.is_c_interop != 1)
7940 /* Make sure the user marked the derived type as BIND(C). If
7941 not, call the verify routine. This could print an error
7942 for the derived type more than once if multiple variables
7943 of that type are declared. */
7944 if (sym->ts.derived->attr.is_bind_c != 1)
7945 verify_bind_c_derived_type (sym->ts.derived);
7949 /* Verify the variable itself as C interoperable if it
7950 is BIND(C). It is not possible for this to succeed if
7951 the verify_bind_c_derived_type failed, so don't have to handle
7952 any error returned by verify_bind_c_derived_type. */
7953 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
7959 /* clear the is_bind_c flag to prevent reporting errors more than
7960 once if something failed. */
7961 sym->attr.is_bind_c = 0;
7966 /* If a derived type symbol has reached this point, without its
7967 type being declared, we have an error. Notice that most
7968 conditions that produce undefined derived types have already
7969 been dealt with. However, the likes of:
7970 implicit type(t) (t) ..... call foo (t) will get us here if
7971 the type is not declared in the scope of the implicit
7972 statement. Change the type to BT_UNKNOWN, both because it is so
7973 and to prevent an ICE. */
7974 if (sym->ts.type == BT_DERIVED && sym->ts.derived->components == NULL
7975 && !sym->ts.derived->attr.zero_comp)
7977 gfc_error ("The derived type '%s' at %L is of type '%s', "
7978 "which has not been defined", sym->name,
7979 &sym->declared_at, sym->ts.derived->name);
7980 sym->ts.type = BT_UNKNOWN;
7984 /* Unless the derived-type declaration is use associated, Fortran 95
7985 does not allow public entries of private derived types.
7986 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
7988 if (sym->ts.type == BT_DERIVED
7989 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
7990 && !sym->ts.derived->attr.use_assoc
7991 && gfc_check_access (sym->attr.access, sym->ns->default_access)
7992 && !gfc_check_access (sym->ts.derived->attr.access,
7993 sym->ts.derived->ns->default_access)
7994 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
7995 "of PRIVATE derived type '%s'",
7996 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
7997 : "variable", sym->name, &sym->declared_at,
7998 sym->ts.derived->name) == FAILURE)
8001 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
8002 default initialization is defined (5.1.2.4.4). */
8003 if (sym->ts.type == BT_DERIVED
8005 && sym->attr.intent == INTENT_OUT
8007 && sym->as->type == AS_ASSUMED_SIZE)
8009 for (c = sym->ts.derived->components; c; c = c->next)
8013 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
8014 "ASSUMED SIZE and so cannot have a default initializer",
8015 sym->name, &sym->declared_at);
8021 switch (sym->attr.flavor)
8024 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
8029 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
8034 if (resolve_fl_namelist (sym) == FAILURE)
8039 if (resolve_fl_parameter (sym) == FAILURE)
8047 /* Resolve array specifier. Check as well some constraints
8048 on COMMON blocks. */
8050 check_constant = sym->attr.in_common && !sym->attr.pointer;
8052 /* Set the formal_arg_flag so that check_conflict will not throw
8053 an error for host associated variables in the specification
8054 expression for an array_valued function. */
8055 if (sym->attr.function && sym->as)
8056 formal_arg_flag = 1;
8058 gfc_resolve_array_spec (sym->as, check_constant);
8060 formal_arg_flag = 0;
8062 /* Resolve formal namespaces. */
8063 if (sym->formal_ns && sym->formal_ns != gfc_current_ns)
8064 gfc_resolve (sym->formal_ns);
8066 /* Check threadprivate restrictions. */
8067 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
8068 && (!sym->attr.in_common
8069 && sym->module == NULL
8070 && (sym->ns->proc_name == NULL
8071 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
8072 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
8074 /* If we have come this far we can apply default-initializers, as
8075 described in 14.7.5, to those variables that have not already
8076 been assigned one. */
8077 if (sym->ts.type == BT_DERIVED
8078 && sym->attr.referenced
8079 && sym->ns == gfc_current_ns
8081 && !sym->attr.allocatable
8082 && !sym->attr.alloc_comp)
8084 symbol_attribute *a = &sym->attr;
8086 if ((!a->save && !a->dummy && !a->pointer
8087 && !a->in_common && !a->use_assoc
8088 && !(a->function && sym != sym->result))
8089 || (a->dummy && a->intent == INTENT_OUT))
8090 apply_default_init (sym);
8095 /************* Resolve DATA statements *************/
8099 gfc_data_value *vnode;
8105 /* Advance the values structure to point to the next value in the data list. */
8108 next_data_value (void)
8111 while (mpz_cmp_ui (values.left, 0) == 0)
8113 if (values.vnode->next == NULL)
8116 values.vnode = values.vnode->next;
8117 mpz_set (values.left, values.vnode->repeat);
8125 check_data_variable (gfc_data_variable *var, locus *where)
8131 ar_type mark = AR_UNKNOWN;
8133 mpz_t section_index[GFC_MAX_DIMENSIONS];
8137 if (gfc_resolve_expr (var->expr) == FAILURE)
8141 mpz_init_set_si (offset, 0);
8144 if (e->expr_type != EXPR_VARIABLE)
8145 gfc_internal_error ("check_data_variable(): Bad expression");
8147 if (e->symtree->n.sym->ns->is_block_data
8148 && !e->symtree->n.sym->attr.in_common)
8150 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
8151 e->symtree->n.sym->name, &e->symtree->n.sym->declared_at);
8154 if (e->ref == NULL && e->symtree->n.sym->as)
8156 gfc_error ("DATA array '%s' at %L must be specified in a previous"
8157 " declaration", e->symtree->n.sym->name, where);
8163 mpz_init_set_ui (size, 1);
8170 /* Find the array section reference. */
8171 for (ref = e->ref; ref; ref = ref->next)
8173 if (ref->type != REF_ARRAY)
8175 if (ref->u.ar.type == AR_ELEMENT)
8181 /* Set marks according to the reference pattern. */
8182 switch (ref->u.ar.type)
8190 /* Get the start position of array section. */
8191 gfc_get_section_index (ar, section_index, &offset);
8199 if (gfc_array_size (e, &size) == FAILURE)
8201 gfc_error ("Nonconstant array section at %L in DATA statement",
8210 while (mpz_cmp_ui (size, 0) > 0)
8212 if (next_data_value () == FAILURE)
8214 gfc_error ("DATA statement at %L has more variables than values",
8220 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
8224 /* If we have more than one element left in the repeat count,
8225 and we have more than one element left in the target variable,
8226 then create a range assignment. */
8227 /* FIXME: Only done for full arrays for now, since array sections
8229 if (mark == AR_FULL && ref && ref->next == NULL
8230 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
8234 if (mpz_cmp (size, values.left) >= 0)
8236 mpz_init_set (range, values.left);
8237 mpz_sub (size, size, values.left);
8238 mpz_set_ui (values.left, 0);
8242 mpz_init_set (range, size);
8243 mpz_sub (values.left, values.left, size);
8244 mpz_set_ui (size, 0);
8247 gfc_assign_data_value_range (var->expr, values.vnode->expr,
8250 mpz_add (offset, offset, range);
8254 /* Assign initial value to symbol. */
8257 mpz_sub_ui (values.left, values.left, 1);
8258 mpz_sub_ui (size, size, 1);
8260 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
8264 if (mark == AR_FULL)
8265 mpz_add_ui (offset, offset, 1);
8267 /* Modify the array section indexes and recalculate the offset
8268 for next element. */
8269 else if (mark == AR_SECTION)
8270 gfc_advance_section (section_index, ar, &offset);
8274 if (mark == AR_SECTION)
8276 for (i = 0; i < ar->dimen; i++)
8277 mpz_clear (section_index[i]);
8287 static try traverse_data_var (gfc_data_variable *, locus *);
8289 /* Iterate over a list of elements in a DATA statement. */
8292 traverse_data_list (gfc_data_variable *var, locus *where)
8295 iterator_stack frame;
8296 gfc_expr *e, *start, *end, *step;
8297 try retval = SUCCESS;
8299 mpz_init (frame.value);
8301 start = gfc_copy_expr (var->iter.start);
8302 end = gfc_copy_expr (var->iter.end);
8303 step = gfc_copy_expr (var->iter.step);
8305 if (gfc_simplify_expr (start, 1) == FAILURE
8306 || start->expr_type != EXPR_CONSTANT)
8308 gfc_error ("iterator start at %L does not simplify", &start->where);
8312 if (gfc_simplify_expr (end, 1) == FAILURE
8313 || end->expr_type != EXPR_CONSTANT)
8315 gfc_error ("iterator end at %L does not simplify", &end->where);
8319 if (gfc_simplify_expr (step, 1) == FAILURE
8320 || step->expr_type != EXPR_CONSTANT)
8322 gfc_error ("iterator step at %L does not simplify", &step->where);
8327 mpz_init_set (trip, end->value.integer);
8328 mpz_sub (trip, trip, start->value.integer);
8329 mpz_add (trip, trip, step->value.integer);
8331 mpz_div (trip, trip, step->value.integer);
8333 mpz_set (frame.value, start->value.integer);
8335 frame.prev = iter_stack;
8336 frame.variable = var->iter.var->symtree;
8337 iter_stack = &frame;
8339 while (mpz_cmp_ui (trip, 0) > 0)
8341 if (traverse_data_var (var->list, where) == FAILURE)
8348 e = gfc_copy_expr (var->expr);
8349 if (gfc_simplify_expr (e, 1) == FAILURE)
8357 mpz_add (frame.value, frame.value, step->value.integer);
8359 mpz_sub_ui (trip, trip, 1);
8364 mpz_clear (frame.value);
8366 gfc_free_expr (start);
8367 gfc_free_expr (end);
8368 gfc_free_expr (step);
8370 iter_stack = frame.prev;
8375 /* Type resolve variables in the variable list of a DATA statement. */
8378 traverse_data_var (gfc_data_variable *var, locus *where)
8382 for (; var; var = var->next)
8384 if (var->expr == NULL)
8385 t = traverse_data_list (var, where);
8387 t = check_data_variable (var, where);
8397 /* Resolve the expressions and iterators associated with a data statement.
8398 This is separate from the assignment checking because data lists should
8399 only be resolved once. */
8402 resolve_data_variables (gfc_data_variable *d)
8404 for (; d; d = d->next)
8406 if (d->list == NULL)
8408 if (gfc_resolve_expr (d->expr) == FAILURE)
8413 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
8416 if (resolve_data_variables (d->list) == FAILURE)
8425 /* Resolve a single DATA statement. We implement this by storing a pointer to
8426 the value list into static variables, and then recursively traversing the
8427 variables list, expanding iterators and such. */
8430 resolve_data (gfc_data *d)
8433 if (resolve_data_variables (d->var) == FAILURE)
8436 values.vnode = d->value;
8437 if (d->value == NULL)
8438 mpz_set_ui (values.left, 0);
8440 mpz_set (values.left, d->value->repeat);
8442 if (traverse_data_var (d->var, &d->where) == FAILURE)
8445 /* At this point, we better not have any values left. */
8447 if (next_data_value () == SUCCESS)
8448 gfc_error ("DATA statement at %L has more values than variables",
8453 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
8454 accessed by host or use association, is a dummy argument to a pure function,
8455 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
8456 is storage associated with any such variable, shall not be used in the
8457 following contexts: (clients of this function). */
8459 /* Determines if a variable is not 'pure', ie not assignable within a pure
8460 procedure. Returns zero if assignment is OK, nonzero if there is a
8463 gfc_impure_variable (gfc_symbol *sym)
8467 if (sym->attr.use_assoc || sym->attr.in_common)
8470 if (sym->ns != gfc_current_ns)
8471 return !sym->attr.function;
8473 proc = sym->ns->proc_name;
8474 if (sym->attr.dummy && gfc_pure (proc)
8475 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
8477 proc->attr.function))
8480 /* TODO: Sort out what can be storage associated, if anything, and include
8481 it here. In principle equivalences should be scanned but it does not
8482 seem to be possible to storage associate an impure variable this way. */
8487 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
8488 symbol of the current procedure. */
8491 gfc_pure (gfc_symbol *sym)
8493 symbol_attribute attr;
8496 sym = gfc_current_ns->proc_name;
8502 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
8506 /* Test whether the current procedure is elemental or not. */
8509 gfc_elemental (gfc_symbol *sym)
8511 symbol_attribute attr;
8514 sym = gfc_current_ns->proc_name;
8519 return attr.flavor == FL_PROCEDURE && attr.elemental;
8523 /* Warn about unused labels. */
8526 warn_unused_fortran_label (gfc_st_label *label)
8531 warn_unused_fortran_label (label->left);
8533 if (label->defined == ST_LABEL_UNKNOWN)
8536 switch (label->referenced)
8538 case ST_LABEL_UNKNOWN:
8539 gfc_warning ("Label %d at %L defined but not used", label->value,
8543 case ST_LABEL_BAD_TARGET:
8544 gfc_warning ("Label %d at %L defined but cannot be used",
8545 label->value, &label->where);
8552 warn_unused_fortran_label (label->right);
8556 /* Returns the sequence type of a symbol or sequence. */
8559 sequence_type (gfc_typespec ts)
8568 if (ts.derived->components == NULL)
8569 return SEQ_NONDEFAULT;
8571 result = sequence_type (ts.derived->components->ts);
8572 for (c = ts.derived->components->next; c; c = c->next)
8573 if (sequence_type (c->ts) != result)
8579 if (ts.kind != gfc_default_character_kind)
8580 return SEQ_NONDEFAULT;
8582 return SEQ_CHARACTER;
8585 if (ts.kind != gfc_default_integer_kind)
8586 return SEQ_NONDEFAULT;
8591 if (!(ts.kind == gfc_default_real_kind
8592 || ts.kind == gfc_default_double_kind))
8593 return SEQ_NONDEFAULT;
8598 if (ts.kind != gfc_default_complex_kind)
8599 return SEQ_NONDEFAULT;
8604 if (ts.kind != gfc_default_logical_kind)
8605 return SEQ_NONDEFAULT;
8610 return SEQ_NONDEFAULT;
8615 /* Resolve derived type EQUIVALENCE object. */
8618 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
8621 gfc_component *c = derived->components;
8626 /* Shall not be an object of nonsequence derived type. */
8627 if (!derived->attr.sequence)
8629 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
8630 "attribute to be an EQUIVALENCE object", sym->name,
8635 /* Shall not have allocatable components. */
8636 if (derived->attr.alloc_comp)
8638 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
8639 "components to be an EQUIVALENCE object",sym->name,
8644 if (sym->attr.in_common && has_default_initializer (sym->ts.derived))
8646 gfc_error ("Derived type variable '%s' at %L with default "
8647 "initialization cannot be in EQUIVALENCE with a variable "
8648 "in COMMON", sym->name, &e->where);
8652 for (; c ; c = c->next)
8656 && (resolve_equivalence_derived (c->ts.derived, sym, e) == FAILURE))
8659 /* Shall not be an object of sequence derived type containing a pointer
8660 in the structure. */
8663 gfc_error ("Derived type variable '%s' at %L with pointer "
8664 "component(s) cannot be an EQUIVALENCE object",
8665 sym->name, &e->where);
8673 /* Resolve equivalence object.
8674 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
8675 an allocatable array, an object of nonsequence derived type, an object of
8676 sequence derived type containing a pointer at any level of component
8677 selection, an automatic object, a function name, an entry name, a result
8678 name, a named constant, a structure component, or a subobject of any of
8679 the preceding objects. A substring shall not have length zero. A
8680 derived type shall not have components with default initialization nor
8681 shall two objects of an equivalence group be initialized.
8682 Either all or none of the objects shall have an protected attribute.
8683 The simple constraints are done in symbol.c(check_conflict) and the rest
8684 are implemented here. */
8687 resolve_equivalence (gfc_equiv *eq)
8690 gfc_symbol *derived;
8691 gfc_symbol *first_sym;
8694 locus *last_where = NULL;
8695 seq_type eq_type, last_eq_type;
8696 gfc_typespec *last_ts;
8697 int object, cnt_protected;
8698 const char *value_name;
8702 last_ts = &eq->expr->symtree->n.sym->ts;
8704 first_sym = eq->expr->symtree->n.sym;
8708 for (object = 1; eq; eq = eq->eq, object++)
8712 e->ts = e->symtree->n.sym->ts;
8713 /* match_varspec might not know yet if it is seeing
8714 array reference or substring reference, as it doesn't
8716 if (e->ref && e->ref->type == REF_ARRAY)
8718 gfc_ref *ref = e->ref;
8719 sym = e->symtree->n.sym;
8721 if (sym->attr.dimension)
8723 ref->u.ar.as = sym->as;
8727 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
8728 if (e->ts.type == BT_CHARACTER
8730 && ref->type == REF_ARRAY
8731 && ref->u.ar.dimen == 1
8732 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
8733 && ref->u.ar.stride[0] == NULL)
8735 gfc_expr *start = ref->u.ar.start[0];
8736 gfc_expr *end = ref->u.ar.end[0];
8739 /* Optimize away the (:) reference. */
8740 if (start == NULL && end == NULL)
8745 e->ref->next = ref->next;
8750 ref->type = REF_SUBSTRING;
8752 start = gfc_int_expr (1);
8753 ref->u.ss.start = start;
8754 if (end == NULL && e->ts.cl)
8755 end = gfc_copy_expr (e->ts.cl->length);
8756 ref->u.ss.end = end;
8757 ref->u.ss.length = e->ts.cl;
8764 /* Any further ref is an error. */
8767 gcc_assert (ref->type == REF_ARRAY);
8768 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
8774 if (gfc_resolve_expr (e) == FAILURE)
8777 sym = e->symtree->n.sym;
8779 if (sym->attr.protected)
8781 if (cnt_protected > 0 && cnt_protected != object)
8783 gfc_error ("Either all or none of the objects in the "
8784 "EQUIVALENCE set at %L shall have the "
8785 "PROTECTED attribute",
8790 /* Shall not equivalence common block variables in a PURE procedure. */
8791 if (sym->ns->proc_name
8792 && sym->ns->proc_name->attr.pure
8793 && sym->attr.in_common)
8795 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
8796 "object in the pure procedure '%s'",
8797 sym->name, &e->where, sym->ns->proc_name->name);
8801 /* Shall not be a named constant. */
8802 if (e->expr_type == EXPR_CONSTANT)
8804 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
8805 "object", sym->name, &e->where);
8809 derived = e->ts.derived;
8810 if (derived && resolve_equivalence_derived (derived, sym, e) == FAILURE)
8813 /* Check that the types correspond correctly:
8815 A numeric sequence structure may be equivalenced to another sequence
8816 structure, an object of default integer type, default real type, double
8817 precision real type, default logical type such that components of the
8818 structure ultimately only become associated to objects of the same
8819 kind. A character sequence structure may be equivalenced to an object
8820 of default character kind or another character sequence structure.
8821 Other objects may be equivalenced only to objects of the same type and
8824 /* Identical types are unconditionally OK. */
8825 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
8826 goto identical_types;
8828 last_eq_type = sequence_type (*last_ts);
8829 eq_type = sequence_type (sym->ts);
8831 /* Since the pair of objects is not of the same type, mixed or
8832 non-default sequences can be rejected. */
8834 msg = "Sequence %s with mixed components in EQUIVALENCE "
8835 "statement at %L with different type objects";
8837 && last_eq_type == SEQ_MIXED
8838 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
8840 || (eq_type == SEQ_MIXED
8841 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
8842 &e->where) == FAILURE))
8845 msg = "Non-default type object or sequence %s in EQUIVALENCE "
8846 "statement at %L with objects of different type";
8848 && last_eq_type == SEQ_NONDEFAULT
8849 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
8850 last_where) == FAILURE)
8851 || (eq_type == SEQ_NONDEFAULT
8852 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
8853 &e->where) == FAILURE))
8856 msg ="Non-CHARACTER object '%s' in default CHARACTER "
8857 "EQUIVALENCE statement at %L";
8858 if (last_eq_type == SEQ_CHARACTER
8859 && eq_type != SEQ_CHARACTER
8860 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
8861 &e->where) == FAILURE)
8864 msg ="Non-NUMERIC object '%s' in default NUMERIC "
8865 "EQUIVALENCE statement at %L";
8866 if (last_eq_type == SEQ_NUMERIC
8867 && eq_type != SEQ_NUMERIC
8868 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
8869 &e->where) == FAILURE)
8874 last_where = &e->where;
8879 /* Shall not be an automatic array. */
8880 if (e->ref->type == REF_ARRAY
8881 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
8883 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
8884 "an EQUIVALENCE object", sym->name, &e->where);
8891 /* Shall not be a structure component. */
8892 if (r->type == REF_COMPONENT)
8894 gfc_error ("Structure component '%s' at %L cannot be an "
8895 "EQUIVALENCE object",
8896 r->u.c.component->name, &e->where);
8900 /* A substring shall not have length zero. */
8901 if (r->type == REF_SUBSTRING)
8903 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
8905 gfc_error ("Substring at %L has length zero",
8906 &r->u.ss.start->where);
8916 /* Resolve function and ENTRY types, issue diagnostics if needed. */
8919 resolve_fntype (gfc_namespace *ns)
8924 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
8927 /* If there are any entries, ns->proc_name is the entry master
8928 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
8930 sym = ns->entries->sym;
8932 sym = ns->proc_name;
8933 if (sym->result == sym
8934 && sym->ts.type == BT_UNKNOWN
8935 && gfc_set_default_type (sym, 0, NULL) == FAILURE
8936 && !sym->attr.untyped)
8938 gfc_error ("Function '%s' at %L has no IMPLICIT type",
8939 sym->name, &sym->declared_at);
8940 sym->attr.untyped = 1;
8943 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.use_assoc
8944 && !gfc_check_access (sym->ts.derived->attr.access,
8945 sym->ts.derived->ns->default_access)
8946 && gfc_check_access (sym->attr.access, sym->ns->default_access))
8948 gfc_error ("PUBLIC function '%s' at %L cannot be of PRIVATE type '%s'",
8949 sym->name, &sym->declared_at, sym->ts.derived->name);
8953 for (el = ns->entries->next; el; el = el->next)
8955 if (el->sym->result == el->sym
8956 && el->sym->ts.type == BT_UNKNOWN
8957 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
8958 && !el->sym->attr.untyped)
8960 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
8961 el->sym->name, &el->sym->declared_at);
8962 el->sym->attr.untyped = 1;
8967 /* 12.3.2.1.1 Defined operators. */
8970 gfc_resolve_uops (gfc_symtree *symtree)
8974 gfc_formal_arglist *formal;
8976 if (symtree == NULL)
8979 gfc_resolve_uops (symtree->left);
8980 gfc_resolve_uops (symtree->right);
8982 for (itr = symtree->n.uop->operator; itr; itr = itr->next)
8985 if (!sym->attr.function)
8986 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
8987 sym->name, &sym->declared_at);
8989 if (sym->ts.type == BT_CHARACTER
8990 && !(sym->ts.cl && sym->ts.cl->length)
8991 && !(sym->result && sym->result->ts.cl
8992 && sym->result->ts.cl->length))
8993 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
8994 "character length", sym->name, &sym->declared_at);
8996 formal = sym->formal;
8997 if (!formal || !formal->sym)
8999 gfc_error ("User operator procedure '%s' at %L must have at least "
9000 "one argument", sym->name, &sym->declared_at);
9004 if (formal->sym->attr.intent != INTENT_IN)
9005 gfc_error ("First argument of operator interface at %L must be "
9006 "INTENT(IN)", &sym->declared_at);
9008 if (formal->sym->attr.optional)
9009 gfc_error ("First argument of operator interface at %L cannot be "
9010 "optional", &sym->declared_at);
9012 formal = formal->next;
9013 if (!formal || !formal->sym)
9016 if (formal->sym->attr.intent != INTENT_IN)
9017 gfc_error ("Second argument of operator interface at %L must be "
9018 "INTENT(IN)", &sym->declared_at);
9020 if (formal->sym->attr.optional)
9021 gfc_error ("Second argument of operator interface at %L cannot be "
9022 "optional", &sym->declared_at);
9025 gfc_error ("Operator interface at %L must have, at most, two "
9026 "arguments", &sym->declared_at);
9031 /* Examine all of the expressions associated with a program unit,
9032 assign types to all intermediate expressions, make sure that all
9033 assignments are to compatible types and figure out which names
9034 refer to which functions or subroutines. It doesn't check code
9035 block, which is handled by resolve_code. */
9038 resolve_types (gfc_namespace *ns)
9045 gfc_current_ns = ns;
9047 resolve_entries (ns);
9049 resolve_common_vars (ns->blank_common.head, false);
9050 resolve_common_blocks (ns->common_root);
9052 resolve_contained_functions (ns);
9054 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
9056 for (cl = ns->cl_list; cl; cl = cl->next)
9057 resolve_charlen (cl);
9059 gfc_traverse_ns (ns, resolve_symbol);
9061 resolve_fntype (ns);
9063 for (n = ns->contained; n; n = n->sibling)
9065 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
9066 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
9067 "also be PURE", n->proc_name->name,
9068 &n->proc_name->declared_at);
9074 gfc_check_interfaces (ns);
9076 gfc_traverse_ns (ns, resolve_values);
9082 for (d = ns->data; d; d = d->next)
9086 gfc_traverse_ns (ns, gfc_formalize_init_value);
9088 gfc_traverse_ns (ns, gfc_verify_binding_labels);
9090 if (ns->common_root != NULL)
9091 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
9093 for (eq = ns->equiv; eq; eq = eq->next)
9094 resolve_equivalence (eq);
9096 /* Warn about unused labels. */
9097 if (warn_unused_label)
9098 warn_unused_fortran_label (ns->st_labels);
9100 gfc_resolve_uops (ns->uop_root);
9104 /* Call resolve_code recursively. */
9107 resolve_codes (gfc_namespace *ns)
9111 for (n = ns->contained; n; n = n->sibling)
9114 gfc_current_ns = ns;
9116 /* Set to an out of range value. */
9117 current_entry_id = -1;
9119 bitmap_obstack_initialize (&labels_obstack);
9120 resolve_code (ns->code, ns);
9121 bitmap_obstack_release (&labels_obstack);
9125 /* This function is called after a complete program unit has been compiled.
9126 Its purpose is to examine all of the expressions associated with a program
9127 unit, assign types to all intermediate expressions, make sure that all
9128 assignments are to compatible types and figure out which names refer to
9129 which functions or subroutines. */
9132 gfc_resolve (gfc_namespace *ns)
9134 gfc_namespace *old_ns;
9136 old_ns = gfc_current_ns;
9141 gfc_current_ns = old_ns;