1 /* Perform type resolution on the various stuctures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor,Boston, MA
26 #include "arith.h" /* For gfc_compare_expr(). */
29 /* Stack to push the current if we descend into a block during
30 resolution. See resolve_branch() and resolve_code(). */
32 typedef struct code_stack
34 struct gfc_code *head, *current;
35 struct code_stack *prev;
39 static code_stack *cs_base = NULL;
42 /* Nonzero if we're inside a FORALL block */
44 static int forall_flag;
46 /* Resolve types of formal argument lists. These have to be done early so that
47 the formal argument lists of module procedures can be copied to the
48 containing module before the individual procedures are resolved
49 individually. We also resolve argument lists of procedures in interface
50 blocks because they are self-contained scoping units.
52 Since a dummy argument cannot be a non-dummy procedure, the only
53 resort left for untyped names are the IMPLICIT types. */
56 resolve_formal_arglist (gfc_symbol * proc)
58 gfc_formal_arglist *f;
62 /* TODO: Procedures whose return character length parameter is not constant
63 or assumed must also have explicit interfaces. */
64 if (proc->result != NULL)
69 if (gfc_elemental (proc)
70 || sym->attr.pointer || sym->attr.allocatable
71 || (sym->as && sym->as->rank > 0))
72 proc->attr.always_explicit = 1;
74 for (f = proc->formal; f; f = f->next)
80 /* Alternate return placeholder. */
81 if (gfc_elemental (proc))
82 gfc_error ("Alternate return specifier in elemental subroutine "
83 "'%s' at %L is not allowed", proc->name,
85 if (proc->attr.function)
86 gfc_error ("Alternate return specifier in function "
87 "'%s' at %L is not allowed", proc->name,
92 if (sym->attr.if_source != IFSRC_UNKNOWN)
93 resolve_formal_arglist (sym);
95 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
97 if (gfc_pure (proc) && !gfc_pure (sym))
100 ("Dummy procedure '%s' of PURE procedure at %L must also "
101 "be PURE", sym->name, &sym->declared_at);
105 if (gfc_elemental (proc))
108 ("Dummy procedure at %L not allowed in ELEMENTAL procedure",
116 if (sym->ts.type == BT_UNKNOWN)
118 if (!sym->attr.function || sym->result == sym)
119 gfc_set_default_type (sym, 1, sym->ns);
122 /* Set the type of the RESULT, then copy. */
123 if (sym->result->ts.type == BT_UNKNOWN)
124 gfc_set_default_type (sym->result, 1, sym->result->ns);
126 sym->ts = sym->result->ts;
128 sym->as = gfc_copy_array_spec (sym->result->as);
132 gfc_resolve_array_spec (sym->as, 0);
134 /* We can't tell if an array with dimension (:) is assumed or deferred
135 shape until we know if it has the pointer or allocatable attributes.
137 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
138 && !(sym->attr.pointer || sym->attr.allocatable))
140 sym->as->type = AS_ASSUMED_SHAPE;
141 for (i = 0; i < sym->as->rank; i++)
142 sym->as->lower[i] = gfc_int_expr (1);
145 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
146 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
147 || sym->attr.optional)
148 proc->attr.always_explicit = 1;
150 /* If the flavor is unknown at this point, it has to be a variable.
151 A procedure specification would have already set the type. */
153 if (sym->attr.flavor == FL_UNKNOWN)
154 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
158 if (proc->attr.function && !sym->attr.pointer
159 && sym->attr.flavor != FL_PROCEDURE
160 && sym->attr.intent != INTENT_IN)
162 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
163 "INTENT(IN)", sym->name, proc->name,
166 if (proc->attr.subroutine && !sym->attr.pointer
167 && sym->attr.intent == INTENT_UNKNOWN)
170 ("Argument '%s' of pure subroutine '%s' at %L must have "
171 "its INTENT specified", sym->name, proc->name,
176 if (gfc_elemental (proc))
181 ("Argument '%s' of elemental procedure at %L must be scalar",
182 sym->name, &sym->declared_at);
186 if (sym->attr.pointer)
189 ("Argument '%s' of elemental procedure at %L cannot have "
190 "the POINTER attribute", sym->name, &sym->declared_at);
195 /* Each dummy shall be specified to be scalar. */
196 if (proc->attr.proc == PROC_ST_FUNCTION)
201 ("Argument '%s' of statement function at %L must be scalar",
202 sym->name, &sym->declared_at);
206 if (sym->ts.type == BT_CHARACTER)
208 gfc_charlen *cl = sym->ts.cl;
209 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
212 ("Character-valued argument '%s' of statement function at "
213 "%L must has constant length",
214 sym->name, &sym->declared_at);
223 /* Work function called when searching for symbols that have argument lists
224 associated with them. */
227 find_arglists (gfc_symbol * sym)
230 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
233 resolve_formal_arglist (sym);
237 /* Given a namespace, resolve all formal argument lists within the namespace.
241 resolve_formal_arglists (gfc_namespace * ns)
247 gfc_traverse_ns (ns, find_arglists);
252 resolve_contained_fntype (gfc_symbol * sym, gfc_namespace * ns)
256 /* If this namespace is not a function, ignore it. */
258 || !(sym->attr.function
259 || sym->attr.flavor == FL_VARIABLE))
262 /* Try to find out of what the return type is. */
263 if (sym->result != NULL)
266 if (sym->ts.type == BT_UNKNOWN)
268 t = gfc_set_default_type (sym, 0, ns);
270 if (t == FAILURE && !sym->attr.untyped)
272 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
273 sym->name, &sym->declared_at); /* FIXME */
274 sym->attr.untyped = 1;
280 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
281 introduce duplicates. */
284 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
286 gfc_formal_arglist *f, *new_arglist;
289 for (; new_args != NULL; new_args = new_args->next)
291 new_sym = new_args->sym;
292 /* See if ths arg is already in the formal argument list. */
293 for (f = proc->formal; f; f = f->next)
295 if (new_sym == f->sym)
302 /* Add a new argument. Argument order is not important. */
303 new_arglist = gfc_get_formal_arglist ();
304 new_arglist->sym = new_sym;
305 new_arglist->next = proc->formal;
306 proc->formal = new_arglist;
311 /* Resolve alternate entry points. If a symbol has multiple entry points we
312 create a new master symbol for the main routine, and turn the existing
313 symbol into an entry point. */
316 resolve_entries (gfc_namespace * ns)
318 gfc_namespace *old_ns;
322 char name[GFC_MAX_SYMBOL_LEN + 1];
323 static int master_count = 0;
325 if (ns->proc_name == NULL)
328 /* No need to do anything if this procedure doesn't have alternate entry
333 /* We may already have resolved alternate entry points. */
334 if (ns->proc_name->attr.entry_master)
337 /* If this isn't a procedure something has gone horribly wrong. */
338 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
340 /* Remember the current namespace. */
341 old_ns = gfc_current_ns;
345 /* Add the main entry point to the list of entry points. */
346 el = gfc_get_entry_list ();
347 el->sym = ns->proc_name;
349 el->next = ns->entries;
351 ns->proc_name->attr.entry = 1;
353 /* Add an entry statement for it. */
360 /* Create a new symbol for the master function. */
361 /* Give the internal function a unique name (within this file).
362 Also include the function name so the user has some hope of figuring
363 out what is going on. */
364 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
365 master_count++, ns->proc_name->name);
366 gfc_get_ha_symbol (name, &proc);
367 gcc_assert (proc != NULL);
369 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
370 if (ns->proc_name->attr.subroutine)
371 gfc_add_subroutine (&proc->attr, proc->name, NULL);
375 gfc_typespec *ts, *fts;
377 gfc_add_function (&proc->attr, proc->name, NULL);
379 fts = &ns->entries->sym->result->ts;
380 if (fts->type == BT_UNKNOWN)
381 fts = gfc_get_default_type (ns->entries->sym->result, NULL);
382 for (el = ns->entries->next; el; el = el->next)
384 ts = &el->sym->result->ts;
385 if (ts->type == BT_UNKNOWN)
386 ts = gfc_get_default_type (el->sym->result, NULL);
387 if (! gfc_compare_types (ts, fts)
388 || (el->sym->result->attr.dimension
389 != ns->entries->sym->result->attr.dimension)
390 || (el->sym->result->attr.pointer
391 != ns->entries->sym->result->attr.pointer))
397 sym = ns->entries->sym->result;
398 /* All result types the same. */
400 if (sym->attr.dimension)
401 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
402 if (sym->attr.pointer)
403 gfc_add_pointer (&proc->attr, NULL);
407 /* Otherwise the result will be passed through a union by
409 proc->attr.mixed_entry_master = 1;
410 for (el = ns->entries; el; el = el->next)
412 sym = el->sym->result;
413 if (sym->attr.dimension)
415 if (el == ns->entries)
417 ("FUNCTION result %s can't be an array in FUNCTION %s at %L",
418 sym->name, ns->entries->sym->name, &sym->declared_at);
421 ("ENTRY result %s can't be an array in FUNCTION %s at %L",
422 sym->name, ns->entries->sym->name, &sym->declared_at);
424 else if (sym->attr.pointer)
426 if (el == ns->entries)
428 ("FUNCTION result %s can't be a POINTER in FUNCTION %s at %L",
429 sym->name, ns->entries->sym->name, &sym->declared_at);
432 ("ENTRY result %s can't be a POINTER in FUNCTION %s at %L",
433 sym->name, ns->entries->sym->name, &sym->declared_at);
438 if (ts->type == BT_UNKNOWN)
439 ts = gfc_get_default_type (sym, NULL);
443 if (ts->kind == gfc_default_integer_kind)
447 if (ts->kind == gfc_default_real_kind
448 || ts->kind == gfc_default_double_kind)
452 if (ts->kind == gfc_default_complex_kind)
456 if (ts->kind == gfc_default_logical_kind)
460 /* We will issue error elsewhere. */
468 if (el == ns->entries)
470 ("FUNCTION result %s can't be of type %s in FUNCTION %s at %L",
471 sym->name, gfc_typename (ts), ns->entries->sym->name,
475 ("ENTRY result %s can't be of type %s in FUNCTION %s at %L",
476 sym->name, gfc_typename (ts), ns->entries->sym->name,
483 proc->attr.access = ACCESS_PRIVATE;
484 proc->attr.entry_master = 1;
486 /* Merge all the entry point arguments. */
487 for (el = ns->entries; el; el = el->next)
488 merge_argument_lists (proc, el->sym->formal);
490 /* Use the master function for the function body. */
491 ns->proc_name = proc;
493 /* Finalize the new symbols. */
494 gfc_commit_symbols ();
496 /* Restore the original namespace. */
497 gfc_current_ns = old_ns;
501 /* Resolve contained function types. Because contained functions can call one
502 another, they have to be worked out before any of the contained procedures
505 The good news is that if a function doesn't already have a type, the only
506 way it can get one is through an IMPLICIT type or a RESULT variable, because
507 by definition contained functions are contained namespace they're contained
508 in, not in a sibling or parent namespace. */
511 resolve_contained_functions (gfc_namespace * ns)
513 gfc_namespace *child;
516 resolve_formal_arglists (ns);
518 for (child = ns->contained; child; child = child->sibling)
520 /* Resolve alternate entry points first. */
521 resolve_entries (child);
523 /* Then check function return types. */
524 resolve_contained_fntype (child->proc_name, child);
525 for (el = child->entries; el; el = el->next)
526 resolve_contained_fntype (el->sym, child);
531 /* Resolve all of the elements of a structure constructor and make sure that
532 the types are correct. */
535 resolve_structure_cons (gfc_expr * expr)
537 gfc_constructor *cons;
542 cons = expr->value.constructor;
543 /* A constructor may have references if it is the result of substituting a
544 parameter variable. In this case we just pull out the component we
547 comp = expr->ref->u.c.sym->components;
549 comp = expr->ts.derived->components;
551 for (; comp; comp = comp->next, cons = cons->next)
559 if (gfc_resolve_expr (cons->expr) == FAILURE)
565 /* If we don't have the right type, try to convert it. */
567 if (!gfc_compare_types (&cons->expr->ts, &comp->ts)
568 && gfc_convert_type (cons->expr, &comp->ts, 1) == FAILURE)
577 /****************** Expression name resolution ******************/
579 /* Returns 0 if a symbol was not declared with a type or
580 attribute declaration statement, nonzero otherwise. */
583 was_declared (gfc_symbol * sym)
589 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
592 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
593 || a.optional || a.pointer || a.save || a.target
594 || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
601 /* Determine if a symbol is generic or not. */
604 generic_sym (gfc_symbol * sym)
608 if (sym->attr.generic ||
609 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
612 if (was_declared (sym) || sym->ns->parent == NULL)
615 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
617 return (s == NULL) ? 0 : generic_sym (s);
621 /* Determine if a symbol is specific or not. */
624 specific_sym (gfc_symbol * sym)
628 if (sym->attr.if_source == IFSRC_IFBODY
629 || sym->attr.proc == PROC_MODULE
630 || sym->attr.proc == PROC_INTERNAL
631 || sym->attr.proc == PROC_ST_FUNCTION
632 || (sym->attr.intrinsic &&
633 gfc_specific_intrinsic (sym->name))
634 || sym->attr.external)
637 if (was_declared (sym) || sym->ns->parent == NULL)
640 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
642 return (s == NULL) ? 0 : specific_sym (s);
646 /* Figure out if the procedure is specific, generic or unknown. */
649 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
653 procedure_kind (gfc_symbol * sym)
656 if (generic_sym (sym))
657 return PTYPE_GENERIC;
659 if (specific_sym (sym))
660 return PTYPE_SPECIFIC;
662 return PTYPE_UNKNOWN;
666 /* Resolve an actual argument list. Most of the time, this is just
667 resolving the expressions in the list.
668 The exception is that we sometimes have to decide whether arguments
669 that look like procedure arguments are really simple variable
673 resolve_actual_arglist (gfc_actual_arglist * arg)
676 gfc_symtree *parent_st;
679 for (; arg; arg = arg->next)
685 /* Check the label is a valid branching target. */
688 if (arg->label->defined == ST_LABEL_UNKNOWN)
690 gfc_error ("Label %d referenced at %L is never defined",
691 arg->label->value, &arg->label->where);
698 if (e->ts.type != BT_PROCEDURE)
700 if (gfc_resolve_expr (e) != SUCCESS)
705 /* See if the expression node should really be a variable
708 sym = e->symtree->n.sym;
710 if (sym->attr.flavor == FL_PROCEDURE
711 || sym->attr.intrinsic
712 || sym->attr.external)
715 if (sym->attr.proc == PROC_ST_FUNCTION)
717 gfc_error ("Statement function '%s' at %L is not allowed as an "
718 "actual argument", sym->name, &e->where);
721 /* If the symbol is the function that names the current (or
722 parent) scope, then we really have a variable reference. */
724 if (sym->attr.function && sym->result == sym
725 && (sym->ns->proc_name == sym
726 || (sym->ns->parent != NULL
727 && sym->ns->parent->proc_name == sym)))
733 /* See if the name is a module procedure in a parent unit. */
735 if (was_declared (sym) || sym->ns->parent == NULL)
738 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
740 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
744 if (parent_st == NULL)
747 sym = parent_st->n.sym;
748 e->symtree = parent_st; /* Point to the right thing. */
750 if (sym->attr.flavor == FL_PROCEDURE
751 || sym->attr.intrinsic
752 || sym->attr.external)
758 e->expr_type = EXPR_VARIABLE;
762 e->rank = sym->as->rank;
763 e->ref = gfc_get_ref ();
764 e->ref->type = REF_ARRAY;
765 e->ref->u.ar.type = AR_FULL;
766 e->ref->u.ar.as = sym->as;
774 /************* Function resolution *************/
776 /* Resolve a function call known to be generic.
777 Section 14.1.2.4.1. */
780 resolve_generic_f0 (gfc_expr * expr, gfc_symbol * sym)
784 if (sym->attr.generic)
787 gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
790 expr->value.function.name = s->name;
791 expr->value.function.esym = s;
794 expr->rank = s->as->rank;
798 /* TODO: Need to search for elemental references in generic interface */
801 if (sym->attr.intrinsic)
802 return gfc_intrinsic_func_interface (expr, 0);
809 resolve_generic_f (gfc_expr * expr)
814 sym = expr->symtree->n.sym;
818 m = resolve_generic_f0 (expr, sym);
821 else if (m == MATCH_ERROR)
825 if (sym->ns->parent == NULL)
827 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
831 if (!generic_sym (sym))
835 /* Last ditch attempt. */
837 if (!gfc_generic_intrinsic (expr->symtree->n.sym->name))
839 gfc_error ("Generic function '%s' at %L is not an intrinsic function",
840 expr->symtree->n.sym->name, &expr->where);
844 m = gfc_intrinsic_func_interface (expr, 0);
849 ("Generic function '%s' at %L is not consistent with a specific "
850 "intrinsic interface", expr->symtree->n.sym->name, &expr->where);
856 /* Resolve a function call known to be specific. */
859 resolve_specific_f0 (gfc_symbol * sym, gfc_expr * expr)
863 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
867 sym->attr.proc = PROC_DUMMY;
871 sym->attr.proc = PROC_EXTERNAL;
875 if (sym->attr.proc == PROC_MODULE
876 || sym->attr.proc == PROC_ST_FUNCTION
877 || sym->attr.proc == PROC_INTERNAL)
880 if (sym->attr.intrinsic)
882 m = gfc_intrinsic_func_interface (expr, 1);
887 ("Function '%s' at %L is INTRINSIC but is not compatible with "
888 "an intrinsic", sym->name, &expr->where);
896 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
899 expr->value.function.name = sym->name;
900 expr->value.function.esym = sym;
902 expr->rank = sym->as->rank;
909 resolve_specific_f (gfc_expr * expr)
914 sym = expr->symtree->n.sym;
918 m = resolve_specific_f0 (sym, expr);
921 if (m == MATCH_ERROR)
924 if (sym->ns->parent == NULL)
927 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
933 gfc_error ("Unable to resolve the specific function '%s' at %L",
934 expr->symtree->n.sym->name, &expr->where);
940 /* Resolve a procedure call not known to be generic nor specific. */
943 resolve_unknown_f (gfc_expr * expr)
948 sym = expr->symtree->n.sym;
952 sym->attr.proc = PROC_DUMMY;
953 expr->value.function.name = sym->name;
957 /* See if we have an intrinsic function reference. */
959 if (gfc_intrinsic_name (sym->name, 0))
961 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
966 /* The reference is to an external name. */
968 sym->attr.proc = PROC_EXTERNAL;
969 expr->value.function.name = sym->name;
970 expr->value.function.esym = expr->symtree->n.sym;
973 expr->rank = sym->as->rank;
975 /* Type of the expression is either the type of the symbol or the
976 default type of the symbol. */
979 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
981 if (sym->ts.type != BT_UNKNOWN)
985 ts = gfc_get_default_type (sym, sym->ns);
987 if (ts->type == BT_UNKNOWN)
989 gfc_error ("Function '%s' at %L has no IMPLICIT type",
990 sym->name, &expr->where);
1001 /* Figure out if a function reference is pure or not. Also set the name
1002 of the function for a potential error message. Return nonzero if the
1003 function is PURE, zero if not. */
1006 pure_function (gfc_expr * e, const char **name)
1010 if (e->value.function.esym)
1012 pure = gfc_pure (e->value.function.esym);
1013 *name = e->value.function.esym->name;
1015 else if (e->value.function.isym)
1017 pure = e->value.function.isym->pure
1018 || e->value.function.isym->elemental;
1019 *name = e->value.function.isym->name;
1023 /* Implicit functions are not pure. */
1025 *name = e->value.function.name;
1032 /* Resolve a function call, which means resolving the arguments, then figuring
1033 out which entity the name refers to. */
1034 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
1035 to INTENT(OUT) or INTENT(INOUT). */
1038 resolve_function (gfc_expr * expr)
1040 gfc_actual_arglist *arg;
1044 if (resolve_actual_arglist (expr->value.function.actual) == FAILURE)
1047 /* See if function is already resolved. */
1049 if (expr->value.function.name != NULL)
1051 if (expr->ts.type == BT_UNKNOWN)
1052 expr->ts = expr->symtree->n.sym->ts;
1057 /* Apply the rules of section 14.1.2. */
1059 switch (procedure_kind (expr->symtree->n.sym))
1062 t = resolve_generic_f (expr);
1065 case PTYPE_SPECIFIC:
1066 t = resolve_specific_f (expr);
1070 t = resolve_unknown_f (expr);
1074 gfc_internal_error ("resolve_function(): bad function type");
1078 /* If the expression is still a function (it might have simplified),
1079 then we check to see if we are calling an elemental function. */
1081 if (expr->expr_type != EXPR_FUNCTION)
1084 if (expr->value.function.actual != NULL
1085 && ((expr->value.function.esym != NULL
1086 && expr->value.function.esym->attr.elemental)
1087 || (expr->value.function.isym != NULL
1088 && expr->value.function.isym->elemental)))
1091 /* The rank of an elemental is the rank of its array argument(s). */
1093 for (arg = expr->value.function.actual; arg; arg = arg->next)
1095 if (arg->expr != NULL && arg->expr->rank > 0)
1097 expr->rank = arg->expr->rank;
1103 if (!pure_function (expr, &name))
1108 ("Function reference to '%s' at %L is inside a FORALL block",
1109 name, &expr->where);
1112 else if (gfc_pure (NULL))
1114 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
1115 "procedure within a PURE procedure", name, &expr->where);
1124 /************* Subroutine resolution *************/
1127 pure_subroutine (gfc_code * c, gfc_symbol * sym)
1134 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
1135 sym->name, &c->loc);
1136 else if (gfc_pure (NULL))
1137 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
1143 resolve_generic_s0 (gfc_code * c, gfc_symbol * sym)
1147 if (sym->attr.generic)
1149 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
1152 c->resolved_sym = s;
1153 pure_subroutine (c, s);
1157 /* TODO: Need to search for elemental references in generic interface. */
1160 if (sym->attr.intrinsic)
1161 return gfc_intrinsic_sub_interface (c, 0);
1168 resolve_generic_s (gfc_code * c)
1173 sym = c->symtree->n.sym;
1175 m = resolve_generic_s0 (c, sym);
1178 if (m == MATCH_ERROR)
1181 if (sym->ns->parent != NULL)
1183 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1186 m = resolve_generic_s0 (c, sym);
1189 if (m == MATCH_ERROR)
1194 /* Last ditch attempt. */
1196 if (!gfc_generic_intrinsic (sym->name))
1199 ("Generic subroutine '%s' at %L is not an intrinsic subroutine",
1200 sym->name, &c->loc);
1204 m = gfc_intrinsic_sub_interface (c, 0);
1208 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
1209 "intrinsic subroutine interface", sym->name, &c->loc);
1215 /* Resolve a subroutine call known to be specific. */
1218 resolve_specific_s0 (gfc_code * c, gfc_symbol * sym)
1222 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1224 if (sym->attr.dummy)
1226 sym->attr.proc = PROC_DUMMY;
1230 sym->attr.proc = PROC_EXTERNAL;
1234 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
1237 if (sym->attr.intrinsic)
1239 m = gfc_intrinsic_sub_interface (c, 1);
1243 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
1244 "with an intrinsic", sym->name, &c->loc);
1252 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1254 c->resolved_sym = sym;
1255 pure_subroutine (c, sym);
1262 resolve_specific_s (gfc_code * c)
1267 sym = c->symtree->n.sym;
1269 m = resolve_specific_s0 (c, sym);
1272 if (m == MATCH_ERROR)
1275 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1279 m = resolve_specific_s0 (c, sym);
1282 if (m == MATCH_ERROR)
1286 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
1287 sym->name, &c->loc);
1293 /* Resolve a subroutine call not known to be generic nor specific. */
1296 resolve_unknown_s (gfc_code * c)
1300 sym = c->symtree->n.sym;
1302 if (sym->attr.dummy)
1304 sym->attr.proc = PROC_DUMMY;
1308 /* See if we have an intrinsic function reference. */
1310 if (gfc_intrinsic_name (sym->name, 1))
1312 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
1317 /* The reference is to an external name. */
1320 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1322 c->resolved_sym = sym;
1324 pure_subroutine (c, sym);
1330 /* Resolve a subroutine call. Although it was tempting to use the same code
1331 for functions, subroutines and functions are stored differently and this
1332 makes things awkward. */
1335 resolve_call (gfc_code * c)
1339 if (resolve_actual_arglist (c->ext.actual) == FAILURE)
1342 if (c->resolved_sym != NULL)
1345 switch (procedure_kind (c->symtree->n.sym))
1348 t = resolve_generic_s (c);
1351 case PTYPE_SPECIFIC:
1352 t = resolve_specific_s (c);
1356 t = resolve_unknown_s (c);
1360 gfc_internal_error ("resolve_subroutine(): bad function type");
1366 /* Compare the shapes of two arrays that have non-NULL shapes. If both
1367 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
1368 match. If both op1->shape and op2->shape are non-NULL return FAILURE
1369 if their shapes do not match. If either op1->shape or op2->shape is
1370 NULL, return SUCCESS. */
1373 compare_shapes (gfc_expr * op1, gfc_expr * op2)
1380 if (op1->shape != NULL && op2->shape != NULL)
1382 for (i = 0; i < op1->rank; i++)
1384 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
1386 gfc_error ("Shapes for operands at %L and %L are not conformable",
1387 &op1->where, &op2->where);
1397 /* Resolve an operator expression node. This can involve replacing the
1398 operation with a user defined function call. */
1401 resolve_operator (gfc_expr * e)
1403 gfc_expr *op1, *op2;
1407 /* Resolve all subnodes-- give them types. */
1409 switch (e->value.op.operator)
1412 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
1415 /* Fall through... */
1418 case INTRINSIC_UPLUS:
1419 case INTRINSIC_UMINUS:
1420 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
1425 /* Typecheck the new node. */
1427 op1 = e->value.op.op1;
1428 op2 = e->value.op.op2;
1430 switch (e->value.op.operator)
1432 case INTRINSIC_UPLUS:
1433 case INTRINSIC_UMINUS:
1434 if (op1->ts.type == BT_INTEGER
1435 || op1->ts.type == BT_REAL
1436 || op1->ts.type == BT_COMPLEX)
1442 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
1443 gfc_op2string (e->value.op.operator), gfc_typename (&e->ts));
1446 case INTRINSIC_PLUS:
1447 case INTRINSIC_MINUS:
1448 case INTRINSIC_TIMES:
1449 case INTRINSIC_DIVIDE:
1450 case INTRINSIC_POWER:
1451 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
1453 gfc_type_convert_binary (e);
1458 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
1459 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1460 gfc_typename (&op2->ts));
1463 case INTRINSIC_CONCAT:
1464 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1466 e->ts.type = BT_CHARACTER;
1467 e->ts.kind = op1->ts.kind;
1472 _("Operands of string concatenation operator at %%L are %s/%s"),
1473 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
1479 case INTRINSIC_NEQV:
1480 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
1482 e->ts.type = BT_LOGICAL;
1483 e->ts.kind = gfc_kind_max (op1, op2);
1484 if (op1->ts.kind < e->ts.kind)
1485 gfc_convert_type (op1, &e->ts, 2);
1486 else if (op2->ts.kind < e->ts.kind)
1487 gfc_convert_type (op2, &e->ts, 2);
1491 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
1492 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1493 gfc_typename (&op2->ts));
1498 if (op1->ts.type == BT_LOGICAL)
1500 e->ts.type = BT_LOGICAL;
1501 e->ts.kind = op1->ts.kind;
1505 sprintf (msg, _("Operand of .NOT. operator at %%L is %s"),
1506 gfc_typename (&op1->ts));
1513 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1515 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
1519 /* Fall through... */
1523 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1525 e->ts.type = BT_LOGICAL;
1526 e->ts.kind = gfc_default_logical_kind;
1530 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
1532 gfc_type_convert_binary (e);
1534 e->ts.type = BT_LOGICAL;
1535 e->ts.kind = gfc_default_logical_kind;
1539 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
1541 _("Logicals at %%L must be compared with %s instead of %s"),
1542 e->value.op.operator == INTRINSIC_EQ ? ".EQV." : ".NEQV.",
1543 gfc_op2string (e->value.op.operator));
1546 _("Operands of comparison operator '%s' at %%L are %s/%s"),
1547 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1548 gfc_typename (&op2->ts));
1552 case INTRINSIC_USER:
1554 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
1555 e->value.op.uop->name, gfc_typename (&op1->ts));
1557 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
1558 e->value.op.uop->name, gfc_typename (&op1->ts),
1559 gfc_typename (&op2->ts));
1564 gfc_internal_error ("resolve_operator(): Bad intrinsic");
1567 /* Deal with arrayness of an operand through an operator. */
1571 switch (e->value.op.operator)
1573 case INTRINSIC_PLUS:
1574 case INTRINSIC_MINUS:
1575 case INTRINSIC_TIMES:
1576 case INTRINSIC_DIVIDE:
1577 case INTRINSIC_POWER:
1578 case INTRINSIC_CONCAT:
1582 case INTRINSIC_NEQV:
1590 if (op1->rank == 0 && op2->rank == 0)
1593 if (op1->rank == 0 && op2->rank != 0)
1595 e->rank = op2->rank;
1597 if (e->shape == NULL)
1598 e->shape = gfc_copy_shape (op2->shape, op2->rank);
1601 if (op1->rank != 0 && op2->rank == 0)
1603 e->rank = op1->rank;
1605 if (e->shape == NULL)
1606 e->shape = gfc_copy_shape (op1->shape, op1->rank);
1609 if (op1->rank != 0 && op2->rank != 0)
1611 if (op1->rank == op2->rank)
1613 e->rank = op1->rank;
1614 if (e->shape == NULL)
1616 t = compare_shapes(op1, op2);
1620 e->shape = gfc_copy_shape (op1->shape, op1->rank);
1625 gfc_error ("Inconsistent ranks for operator at %L and %L",
1626 &op1->where, &op2->where);
1629 /* Allow higher level expressions to work. */
1637 case INTRINSIC_UPLUS:
1638 case INTRINSIC_UMINUS:
1639 e->rank = op1->rank;
1641 if (e->shape == NULL)
1642 e->shape = gfc_copy_shape (op1->shape, op1->rank);
1644 /* Simply copy arrayness attribute */
1651 /* Attempt to simplify the expression. */
1653 t = gfc_simplify_expr (e, 0);
1658 if (gfc_extend_expr (e) == SUCCESS)
1661 gfc_error (msg, &e->where);
1667 /************** Array resolution subroutines **************/
1671 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
1674 /* Compare two integer expressions. */
1677 compare_bound (gfc_expr * a, gfc_expr * b)
1681 if (a == NULL || a->expr_type != EXPR_CONSTANT
1682 || b == NULL || b->expr_type != EXPR_CONSTANT)
1685 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
1686 gfc_internal_error ("compare_bound(): Bad expression");
1688 i = mpz_cmp (a->value.integer, b->value.integer);
1698 /* Compare an integer expression with an integer. */
1701 compare_bound_int (gfc_expr * a, int b)
1705 if (a == NULL || a->expr_type != EXPR_CONSTANT)
1708 if (a->ts.type != BT_INTEGER)
1709 gfc_internal_error ("compare_bound_int(): Bad expression");
1711 i = mpz_cmp_si (a->value.integer, b);
1721 /* Compare a single dimension of an array reference to the array
1725 check_dimension (int i, gfc_array_ref * ar, gfc_array_spec * as)
1728 /* Given start, end and stride values, calculate the minimum and
1729 maximum referenced indexes. */
1737 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
1739 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
1745 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
1747 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
1751 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
1753 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
1756 /* TODO: Possibly, we could warn about end[i] being out-of-bound although
1757 it is legal (see 6.2.2.3.1). */
1762 gfc_internal_error ("check_dimension(): Bad array reference");
1768 gfc_warning ("Array reference at %L is out of bounds", &ar->c_where[i]);
1773 /* Compare an array reference with an array specification. */
1776 compare_spec_to_ref (gfc_array_ref * ar)
1783 /* TODO: Full array sections are only allowed as actual parameters. */
1784 if (as->type == AS_ASSUMED_SIZE
1785 && (/*ar->type == AR_FULL
1786 ||*/ (ar->type == AR_SECTION
1787 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
1789 gfc_error ("Rightmost upper bound of assumed size array section"
1790 " not specified at %L", &ar->where);
1794 if (ar->type == AR_FULL)
1797 if (as->rank != ar->dimen)
1799 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
1800 &ar->where, ar->dimen, as->rank);
1804 for (i = 0; i < as->rank; i++)
1805 if (check_dimension (i, ar, as) == FAILURE)
1812 /* Resolve one part of an array index. */
1815 gfc_resolve_index (gfc_expr * index, int check_scalar)
1822 if (gfc_resolve_expr (index) == FAILURE)
1825 if (check_scalar && index->rank != 0)
1827 gfc_error ("Array index at %L must be scalar", &index->where);
1831 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
1833 gfc_error ("Array index at %L must be of INTEGER type",
1838 if (index->ts.type == BT_REAL)
1839 if (gfc_notify_std (GFC_STD_GNU, "Extension: REAL array index at %L",
1840 &index->where) == FAILURE)
1843 if (index->ts.kind != gfc_index_integer_kind
1844 || index->ts.type != BT_INTEGER)
1846 ts.type = BT_INTEGER;
1847 ts.kind = gfc_index_integer_kind;
1849 gfc_convert_type_warn (index, &ts, 2, 0);
1855 /* Resolve a dim argument to an intrinsic function. */
1858 gfc_resolve_dim_arg (gfc_expr *dim)
1863 if (gfc_resolve_expr (dim) == FAILURE)
1868 gfc_error ("Argument dim at %L must be scalar", &dim->where);
1872 if (dim->ts.type != BT_INTEGER)
1874 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
1877 if (dim->ts.kind != gfc_index_integer_kind)
1881 ts.type = BT_INTEGER;
1882 ts.kind = gfc_index_integer_kind;
1884 gfc_convert_type_warn (dim, &ts, 2, 0);
1890 /* Given an expression that contains array references, update those array
1891 references to point to the right array specifications. While this is
1892 filled in during matching, this information is difficult to save and load
1893 in a module, so we take care of it here.
1895 The idea here is that the original array reference comes from the
1896 base symbol. We traverse the list of reference structures, setting
1897 the stored reference to references. Component references can
1898 provide an additional array specification. */
1901 find_array_spec (gfc_expr * e)
1907 as = e->symtree->n.sym->as;
1908 c = e->symtree->n.sym->components;
1910 for (ref = e->ref; ref; ref = ref->next)
1915 gfc_internal_error ("find_array_spec(): Missing spec");
1922 for (; c; c = c->next)
1923 if (c == ref->u.c.component)
1927 gfc_internal_error ("find_array_spec(): Component not found");
1932 gfc_internal_error ("find_array_spec(): unused as(1)");
1936 c = c->ts.derived->components;
1944 gfc_internal_error ("find_array_spec(): unused as(2)");
1948 /* Resolve an array reference. */
1951 resolve_array_ref (gfc_array_ref * ar)
1953 int i, check_scalar;
1955 for (i = 0; i < ar->dimen; i++)
1957 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
1959 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
1961 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
1963 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
1966 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
1967 switch (ar->start[i]->rank)
1970 ar->dimen_type[i] = DIMEN_ELEMENT;
1974 ar->dimen_type[i] = DIMEN_VECTOR;
1978 gfc_error ("Array index at %L is an array of rank %d",
1979 &ar->c_where[i], ar->start[i]->rank);
1984 /* If the reference type is unknown, figure out what kind it is. */
1986 if (ar->type == AR_UNKNOWN)
1988 ar->type = AR_ELEMENT;
1989 for (i = 0; i < ar->dimen; i++)
1990 if (ar->dimen_type[i] == DIMEN_RANGE
1991 || ar->dimen_type[i] == DIMEN_VECTOR)
1993 ar->type = AR_SECTION;
1998 if (compare_spec_to_ref (ar) == FAILURE)
2006 resolve_substring (gfc_ref * ref)
2009 if (ref->u.ss.start != NULL)
2011 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
2014 if (ref->u.ss.start->ts.type != BT_INTEGER)
2016 gfc_error ("Substring start index at %L must be of type INTEGER",
2017 &ref->u.ss.start->where);
2021 if (ref->u.ss.start->rank != 0)
2023 gfc_error ("Substring start index at %L must be scalar",
2024 &ref->u.ss.start->where);
2028 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT)
2030 gfc_error ("Substring start index at %L is less than one",
2031 &ref->u.ss.start->where);
2036 if (ref->u.ss.end != NULL)
2038 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
2041 if (ref->u.ss.end->ts.type != BT_INTEGER)
2043 gfc_error ("Substring end index at %L must be of type INTEGER",
2044 &ref->u.ss.end->where);
2048 if (ref->u.ss.end->rank != 0)
2050 gfc_error ("Substring end index at %L must be scalar",
2051 &ref->u.ss.end->where);
2055 if (ref->u.ss.length != NULL
2056 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT)
2058 gfc_error ("Substring end index at %L is out of bounds",
2059 &ref->u.ss.start->where);
2068 /* Resolve subtype references. */
2071 resolve_ref (gfc_expr * expr)
2073 int current_part_dimension, n_components, seen_part_dimension;
2076 for (ref = expr->ref; ref; ref = ref->next)
2077 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
2079 find_array_spec (expr);
2083 for (ref = expr->ref; ref; ref = ref->next)
2087 if (resolve_array_ref (&ref->u.ar) == FAILURE)
2095 resolve_substring (ref);
2099 /* Check constraints on part references. */
2101 current_part_dimension = 0;
2102 seen_part_dimension = 0;
2105 for (ref = expr->ref; ref; ref = ref->next)
2110 switch (ref->u.ar.type)
2114 current_part_dimension = 1;
2118 current_part_dimension = 0;
2122 gfc_internal_error ("resolve_ref(): Bad array reference");
2128 if ((current_part_dimension || seen_part_dimension)
2129 && ref->u.c.component->pointer)
2132 ("Component to the right of a part reference with nonzero "
2133 "rank must not have the POINTER attribute at %L",
2145 if (((ref->type == REF_COMPONENT && n_components > 1)
2146 || ref->next == NULL)
2147 && current_part_dimension
2148 && seen_part_dimension)
2151 gfc_error ("Two or more part references with nonzero rank must "
2152 "not be specified at %L", &expr->where);
2156 if (ref->type == REF_COMPONENT)
2158 if (current_part_dimension)
2159 seen_part_dimension = 1;
2161 /* reset to make sure */
2162 current_part_dimension = 0;
2170 /* Given an expression, determine its shape. This is easier than it sounds.
2171 Leaves the shape array NULL if it is not possible to determine the shape. */
2174 expression_shape (gfc_expr * e)
2176 mpz_t array[GFC_MAX_DIMENSIONS];
2179 if (e->rank == 0 || e->shape != NULL)
2182 for (i = 0; i < e->rank; i++)
2183 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
2186 e->shape = gfc_get_shape (e->rank);
2188 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
2193 for (i--; i >= 0; i--)
2194 mpz_clear (array[i]);
2198 /* Given a variable expression node, compute the rank of the expression by
2199 examining the base symbol and any reference structures it may have. */
2202 expression_rank (gfc_expr * e)
2209 if (e->expr_type == EXPR_ARRAY)
2211 /* Constructors can have a rank different from one via RESHAPE(). */
2213 if (e->symtree == NULL)
2219 e->rank = (e->symtree->n.sym->as == NULL)
2220 ? 0 : e->symtree->n.sym->as->rank;
2226 for (ref = e->ref; ref; ref = ref->next)
2228 if (ref->type != REF_ARRAY)
2231 if (ref->u.ar.type == AR_FULL)
2233 rank = ref->u.ar.as->rank;
2237 if (ref->u.ar.type == AR_SECTION)
2239 /* Figure out the rank of the section. */
2241 gfc_internal_error ("expression_rank(): Two array specs");
2243 for (i = 0; i < ref->u.ar.dimen; i++)
2244 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
2245 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
2255 expression_shape (e);
2259 /* Resolve a variable expression. */
2262 resolve_variable (gfc_expr * e)
2266 if (e->ref && resolve_ref (e) == FAILURE)
2269 if (e->symtree == NULL)
2272 sym = e->symtree->n.sym;
2273 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2275 e->ts.type = BT_PROCEDURE;
2279 if (sym->ts.type != BT_UNKNOWN)
2280 gfc_variable_attr (e, &e->ts);
2283 /* Must be a simple variable reference. */
2284 if (gfc_set_default_type (sym, 1, NULL) == FAILURE)
2293 /* Resolve an expression. That is, make sure that types of operands agree
2294 with their operators, intrinsic operators are converted to function calls
2295 for overloaded types and unresolved function references are resolved. */
2298 gfc_resolve_expr (gfc_expr * e)
2305 switch (e->expr_type)
2308 t = resolve_operator (e);
2312 t = resolve_function (e);
2316 t = resolve_variable (e);
2318 expression_rank (e);
2321 case EXPR_SUBSTRING:
2322 t = resolve_ref (e);
2332 if (resolve_ref (e) == FAILURE)
2335 t = gfc_resolve_array_constructor (e);
2336 /* Also try to expand a constructor. */
2339 expression_rank (e);
2340 gfc_expand_constructor (e);
2345 case EXPR_STRUCTURE:
2346 t = resolve_ref (e);
2350 t = resolve_structure_cons (e);
2354 t = gfc_simplify_expr (e, 0);
2358 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
2365 /* Resolve an expression from an iterator. They must be scalar and have
2366 INTEGER or (optionally) REAL type. */
2369 gfc_resolve_iterator_expr (gfc_expr * expr, bool real_ok,
2370 const char * name_msgid)
2372 if (gfc_resolve_expr (expr) == FAILURE)
2375 if (expr->rank != 0)
2377 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
2381 if (!(expr->ts.type == BT_INTEGER
2382 || (expr->ts.type == BT_REAL && real_ok)))
2385 gfc_error ("%s at %L must be INTEGER or REAL", _(name_msgid),
2388 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
2395 /* Resolve the expressions in an iterator structure. If REAL_OK is
2396 false allow only INTEGER type iterators, otherwise allow REAL types. */
2399 gfc_resolve_iterator (gfc_iterator * iter, bool real_ok)
2402 if (iter->var->ts.type == BT_REAL)
2403 gfc_notify_std (GFC_STD_F95_DEL,
2404 "Obsolete: REAL DO loop iterator at %L",
2407 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
2411 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
2413 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
2418 if (gfc_resolve_iterator_expr (iter->start, real_ok,
2419 "Start expression in DO loop") == FAILURE)
2422 if (gfc_resolve_iterator_expr (iter->end, real_ok,
2423 "End expression in DO loop") == FAILURE)
2426 if (gfc_resolve_iterator_expr (iter->step, real_ok,
2427 "Step expression in DO loop") == FAILURE)
2430 if (iter->step->expr_type == EXPR_CONSTANT)
2432 if ((iter->step->ts.type == BT_INTEGER
2433 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
2434 || (iter->step->ts.type == BT_REAL
2435 && mpfr_sgn (iter->step->value.real) == 0))
2437 gfc_error ("Step expression in DO loop at %L cannot be zero",
2438 &iter->step->where);
2443 /* Convert start, end, and step to the same type as var. */
2444 if (iter->start->ts.kind != iter->var->ts.kind
2445 || iter->start->ts.type != iter->var->ts.type)
2446 gfc_convert_type (iter->start, &iter->var->ts, 2);
2448 if (iter->end->ts.kind != iter->var->ts.kind
2449 || iter->end->ts.type != iter->var->ts.type)
2450 gfc_convert_type (iter->end, &iter->var->ts, 2);
2452 if (iter->step->ts.kind != iter->var->ts.kind
2453 || iter->step->ts.type != iter->var->ts.type)
2454 gfc_convert_type (iter->step, &iter->var->ts, 2);
2460 /* Resolve a list of FORALL iterators. */
2463 resolve_forall_iterators (gfc_forall_iterator * iter)
2468 if (gfc_resolve_expr (iter->var) == SUCCESS
2469 && iter->var->ts.type != BT_INTEGER)
2470 gfc_error ("FORALL Iteration variable at %L must be INTEGER",
2473 if (gfc_resolve_expr (iter->start) == SUCCESS
2474 && iter->start->ts.type != BT_INTEGER)
2475 gfc_error ("FORALL start expression at %L must be INTEGER",
2476 &iter->start->where);
2477 if (iter->var->ts.kind != iter->start->ts.kind)
2478 gfc_convert_type (iter->start, &iter->var->ts, 2);
2480 if (gfc_resolve_expr (iter->end) == SUCCESS
2481 && iter->end->ts.type != BT_INTEGER)
2482 gfc_error ("FORALL end expression at %L must be INTEGER",
2484 if (iter->var->ts.kind != iter->end->ts.kind)
2485 gfc_convert_type (iter->end, &iter->var->ts, 2);
2487 if (gfc_resolve_expr (iter->stride) == SUCCESS
2488 && iter->stride->ts.type != BT_INTEGER)
2489 gfc_error ("FORALL Stride expression at %L must be INTEGER",
2490 &iter->stride->where);
2491 if (iter->var->ts.kind != iter->stride->ts.kind)
2492 gfc_convert_type (iter->stride, &iter->var->ts, 2);
2499 /* Given a pointer to a symbol that is a derived type, see if any components
2500 have the POINTER attribute. The search is recursive if necessary.
2501 Returns zero if no pointer components are found, nonzero otherwise. */
2504 derived_pointer (gfc_symbol * sym)
2508 for (c = sym->components; c; c = c->next)
2513 if (c->ts.type == BT_DERIVED && derived_pointer (c->ts.derived))
2521 /* Given a pointer to a symbol that is a derived type, see if it's
2522 inaccessible, i.e. if it's defined in another module and the components are
2523 PRIVATE. The search is recursive if necessary. Returns zero if no
2524 inaccessible components are found, nonzero otherwise. */
2527 derived_inaccessible (gfc_symbol *sym)
2531 if (sym->attr.use_assoc && sym->component_access == ACCESS_PRIVATE)
2534 for (c = sym->components; c; c = c->next)
2536 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.derived))
2544 /* Resolve the argument of a deallocate expression. The expression must be
2545 a pointer or a full array. */
2548 resolve_deallocate_expr (gfc_expr * e)
2550 symbol_attribute attr;
2554 if (gfc_resolve_expr (e) == FAILURE)
2557 attr = gfc_expr_attr (e);
2561 if (e->expr_type != EXPR_VARIABLE)
2564 allocatable = e->symtree->n.sym->attr.allocatable;
2565 for (ref = e->ref; ref; ref = ref->next)
2569 if (ref->u.ar.type != AR_FULL)
2574 allocatable = (ref->u.c.component->as != NULL
2575 && ref->u.c.component->as->type == AS_DEFERRED);
2583 if (allocatable == 0)
2586 gfc_error ("Expression in DEALLOCATE statement at %L must be "
2587 "ALLOCATABLE or a POINTER", &e->where);
2594 /* Resolve the expression in an ALLOCATE statement, doing the additional
2595 checks to see whether the expression is OK or not. The expression must
2596 have a trailing array reference that gives the size of the array. */
2599 resolve_allocate_expr (gfc_expr * e)
2601 int i, pointer, allocatable, dimension;
2602 symbol_attribute attr;
2603 gfc_ref *ref, *ref2;
2606 if (gfc_resolve_expr (e) == FAILURE)
2609 /* Make sure the expression is allocatable or a pointer. If it is
2610 pointer, the next-to-last reference must be a pointer. */
2614 if (e->expr_type != EXPR_VARIABLE)
2618 attr = gfc_expr_attr (e);
2619 pointer = attr.pointer;
2620 dimension = attr.dimension;
2625 allocatable = e->symtree->n.sym->attr.allocatable;
2626 pointer = e->symtree->n.sym->attr.pointer;
2627 dimension = e->symtree->n.sym->attr.dimension;
2629 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
2633 if (ref->next != NULL)
2638 allocatable = (ref->u.c.component->as != NULL
2639 && ref->u.c.component->as->type == AS_DEFERRED);
2641 pointer = ref->u.c.component->pointer;
2642 dimension = ref->u.c.component->dimension;
2652 if (allocatable == 0 && pointer == 0)
2654 gfc_error ("Expression in ALLOCATE statement at %L must be "
2655 "ALLOCATABLE or a POINTER", &e->where);
2659 if (pointer && dimension == 0)
2662 /* Make sure the next-to-last reference node is an array specification. */
2664 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
2666 gfc_error ("Array specification required in ALLOCATE statement "
2667 "at %L", &e->where);
2671 if (ref2->u.ar.type == AR_ELEMENT)
2674 /* Make sure that the array section reference makes sense in the
2675 context of an ALLOCATE specification. */
2679 for (i = 0; i < ar->dimen; i++)
2680 switch (ar->dimen_type[i])
2686 if (ar->start[i] != NULL
2687 && ar->end[i] != NULL
2688 && ar->stride[i] == NULL)
2691 /* Fall Through... */
2695 gfc_error ("Bad array specification in ALLOCATE statement at %L",
2704 /************ SELECT CASE resolution subroutines ************/
2706 /* Callback function for our mergesort variant. Determines interval
2707 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
2708 op1 > op2. Assumes we're not dealing with the default case.
2709 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
2710 There are nine situations to check. */
2713 compare_cases (const gfc_case * op1, const gfc_case * op2)
2717 if (op1->low == NULL) /* op1 = (:L) */
2719 /* op2 = (:N), so overlap. */
2721 /* op2 = (M:) or (M:N), L < M */
2722 if (op2->low != NULL
2723 && gfc_compare_expr (op1->high, op2->low) < 0)
2726 else if (op1->high == NULL) /* op1 = (K:) */
2728 /* op2 = (M:), so overlap. */
2730 /* op2 = (:N) or (M:N), K > N */
2731 if (op2->high != NULL
2732 && gfc_compare_expr (op1->low, op2->high) > 0)
2735 else /* op1 = (K:L) */
2737 if (op2->low == NULL) /* op2 = (:N), K > N */
2738 retval = (gfc_compare_expr (op1->low, op2->high) > 0) ? 1 : 0;
2739 else if (op2->high == NULL) /* op2 = (M:), L < M */
2740 retval = (gfc_compare_expr (op1->high, op2->low) < 0) ? -1 : 0;
2741 else /* op2 = (M:N) */
2745 if (gfc_compare_expr (op1->high, op2->low) < 0)
2748 else if (gfc_compare_expr (op1->low, op2->high) > 0)
2757 /* Merge-sort a double linked case list, detecting overlap in the
2758 process. LIST is the head of the double linked case list before it
2759 is sorted. Returns the head of the sorted list if we don't see any
2760 overlap, or NULL otherwise. */
2763 check_case_overlap (gfc_case * list)
2765 gfc_case *p, *q, *e, *tail;
2766 int insize, nmerges, psize, qsize, cmp, overlap_seen;
2768 /* If the passed list was empty, return immediately. */
2775 /* Loop unconditionally. The only exit from this loop is a return
2776 statement, when we've finished sorting the case list. */
2783 /* Count the number of merges we do in this pass. */
2786 /* Loop while there exists a merge to be done. */
2791 /* Count this merge. */
2794 /* Cut the list in two pieces by stepping INSIZE places
2795 forward in the list, starting from P. */
2798 for (i = 0; i < insize; i++)
2807 /* Now we have two lists. Merge them! */
2808 while (psize > 0 || (qsize > 0 && q != NULL))
2811 /* See from which the next case to merge comes from. */
2814 /* P is empty so the next case must come from Q. */
2819 else if (qsize == 0 || q == NULL)
2828 cmp = compare_cases (p, q);
2831 /* The whole case range for P is less than the
2839 /* The whole case range for Q is greater than
2840 the case range for P. */
2847 /* The cases overlap, or they are the same
2848 element in the list. Either way, we must
2849 issue an error and get the next case from P. */
2850 /* FIXME: Sort P and Q by line number. */
2851 gfc_error ("CASE label at %L overlaps with CASE "
2852 "label at %L", &p->where, &q->where);
2860 /* Add the next element to the merged list. */
2869 /* P has now stepped INSIZE places along, and so has Q. So
2870 they're the same. */
2875 /* If we have done only one merge or none at all, we've
2876 finished sorting the cases. */
2885 /* Otherwise repeat, merging lists twice the size. */
2891 /* Check to see if an expression is suitable for use in a CASE statement.
2892 Makes sure that all case expressions are scalar constants of the same
2893 type. Return FAILURE if anything is wrong. */
2896 validate_case_label_expr (gfc_expr * e, gfc_expr * case_expr)
2898 if (e == NULL) return SUCCESS;
2900 if (e->ts.type != case_expr->ts.type)
2902 gfc_error ("Expression in CASE statement at %L must be of type %s",
2903 &e->where, gfc_basic_typename (case_expr->ts.type));
2907 /* C805 (R808) For a given case-construct, each case-value shall be of
2908 the same type as case-expr. For character type, length differences
2909 are allowed, but the kind type parameters shall be the same. */
2911 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
2913 gfc_error("Expression in CASE statement at %L must be kind %d",
2914 &e->where, case_expr->ts.kind);
2918 /* Convert the case value kind to that of case expression kind, if needed.
2919 FIXME: Should a warning be issued? */
2920 if (e->ts.kind != case_expr->ts.kind)
2921 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
2925 gfc_error ("Expression in CASE statement at %L must be scalar",
2934 /* Given a completely parsed select statement, we:
2936 - Validate all expressions and code within the SELECT.
2937 - Make sure that the selection expression is not of the wrong type.
2938 - Make sure that no case ranges overlap.
2939 - Eliminate unreachable cases and unreachable code resulting from
2940 removing case labels.
2942 The standard does allow unreachable cases, e.g. CASE (5:3). But
2943 they are a hassle for code generation, and to prevent that, we just
2944 cut them out here. This is not necessary for overlapping cases
2945 because they are illegal and we never even try to generate code.
2947 We have the additional caveat that a SELECT construct could have
2948 been a computed GOTO in the source code. Fortunately we can fairly
2949 easily work around that here: The case_expr for a "real" SELECT CASE
2950 is in code->expr1, but for a computed GOTO it is in code->expr2. All
2951 we have to do is make sure that the case_expr is a scalar integer
2955 resolve_select (gfc_code * code)
2958 gfc_expr *case_expr;
2959 gfc_case *cp, *default_case, *tail, *head;
2960 int seen_unreachable;
2965 if (code->expr == NULL)
2967 /* This was actually a computed GOTO statement. */
2968 case_expr = code->expr2;
2969 if (case_expr->ts.type != BT_INTEGER
2970 || case_expr->rank != 0)
2971 gfc_error ("Selection expression in computed GOTO statement "
2972 "at %L must be a scalar integer expression",
2975 /* Further checking is not necessary because this SELECT was built
2976 by the compiler, so it should always be OK. Just move the
2977 case_expr from expr2 to expr so that we can handle computed
2978 GOTOs as normal SELECTs from here on. */
2979 code->expr = code->expr2;
2984 case_expr = code->expr;
2986 type = case_expr->ts.type;
2987 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
2989 gfc_error ("Argument of SELECT statement at %L cannot be %s",
2990 &case_expr->where, gfc_typename (&case_expr->ts));
2992 /* Punt. Going on here just produce more garbage error messages. */
2996 if (case_expr->rank != 0)
2998 gfc_error ("Argument of SELECT statement at %L must be a scalar "
2999 "expression", &case_expr->where);
3005 /* PR 19168 has a long discussion concerning a mismatch of the kinds
3006 of the SELECT CASE expression and its CASE values. Walk the lists
3007 of case values, and if we find a mismatch, promote case_expr to
3008 the appropriate kind. */
3010 if (type == BT_LOGICAL || type == BT_INTEGER)
3012 for (body = code->block; body; body = body->block)
3014 /* Walk the case label list. */
3015 for (cp = body->ext.case_list; cp; cp = cp->next)
3017 /* Intercept the DEFAULT case. It does not have a kind. */
3018 if (cp->low == NULL && cp->high == NULL)
3021 /* Unreachable case ranges are discarded, so ignore. */
3022 if (cp->low != NULL && cp->high != NULL
3023 && cp->low != cp->high
3024 && gfc_compare_expr (cp->low, cp->high) > 0)
3027 /* FIXME: Should a warning be issued? */
3029 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
3030 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
3032 if (cp->high != NULL
3033 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
3034 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
3039 /* Assume there is no DEFAULT case. */
3040 default_case = NULL;
3044 for (body = code->block; body; body = body->block)
3046 /* Assume the CASE list is OK, and all CASE labels can be matched. */
3048 seen_unreachable = 0;
3050 /* Walk the case label list, making sure that all case labels
3052 for (cp = body->ext.case_list; cp; cp = cp->next)
3054 /* Count the number of cases in the whole construct. */
3057 /* Intercept the DEFAULT case. */
3058 if (cp->low == NULL && cp->high == NULL)
3060 if (default_case != NULL)
3062 gfc_error ("The DEFAULT CASE at %L cannot be followed "
3063 "by a second DEFAULT CASE at %L",
3064 &default_case->where, &cp->where);
3075 /* Deal with single value cases and case ranges. Errors are
3076 issued from the validation function. */
3077 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
3078 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
3084 if (type == BT_LOGICAL
3085 && ((cp->low == NULL || cp->high == NULL)
3086 || cp->low != cp->high))
3089 ("Logical range in CASE statement at %L is not allowed",
3095 if (cp->low != NULL && cp->high != NULL
3096 && cp->low != cp->high
3097 && gfc_compare_expr (cp->low, cp->high) > 0)
3099 if (gfc_option.warn_surprising)
3100 gfc_warning ("Range specification at %L can never "
3101 "be matched", &cp->where);
3103 cp->unreachable = 1;
3104 seen_unreachable = 1;
3108 /* If the case range can be matched, it can also overlap with
3109 other cases. To make sure it does not, we put it in a
3110 double linked list here. We sort that with a merge sort
3111 later on to detect any overlapping cases. */
3115 head->right = head->left = NULL;
3120 tail->right->left = tail;
3127 /* It there was a failure in the previous case label, give up
3128 for this case label list. Continue with the next block. */
3132 /* See if any case labels that are unreachable have been seen.
3133 If so, we eliminate them. This is a bit of a kludge because
3134 the case lists for a single case statement (label) is a
3135 single forward linked lists. */
3136 if (seen_unreachable)
3138 /* Advance until the first case in the list is reachable. */
3139 while (body->ext.case_list != NULL
3140 && body->ext.case_list->unreachable)
3142 gfc_case *n = body->ext.case_list;
3143 body->ext.case_list = body->ext.case_list->next;
3145 gfc_free_case_list (n);
3148 /* Strip all other unreachable cases. */
3149 if (body->ext.case_list)
3151 for (cp = body->ext.case_list; cp->next; cp = cp->next)
3153 if (cp->next->unreachable)
3155 gfc_case *n = cp->next;
3156 cp->next = cp->next->next;
3158 gfc_free_case_list (n);
3165 /* See if there were overlapping cases. If the check returns NULL,
3166 there was overlap. In that case we don't do anything. If head
3167 is non-NULL, we prepend the DEFAULT case. The sorted list can
3168 then used during code generation for SELECT CASE constructs with
3169 a case expression of a CHARACTER type. */
3172 head = check_case_overlap (head);
3174 /* Prepend the default_case if it is there. */
3175 if (head != NULL && default_case)
3177 default_case->left = NULL;
3178 default_case->right = head;
3179 head->left = default_case;
3183 /* Eliminate dead blocks that may be the result if we've seen
3184 unreachable case labels for a block. */
3185 for (body = code; body && body->block; body = body->block)
3187 if (body->block->ext.case_list == NULL)
3189 /* Cut the unreachable block from the code chain. */
3190 gfc_code *c = body->block;
3191 body->block = c->block;
3193 /* Kill the dead block, but not the blocks below it. */
3195 gfc_free_statements (c);
3199 /* More than two cases is legal but insane for logical selects.
3200 Issue a warning for it. */
3201 if (gfc_option.warn_surprising && type == BT_LOGICAL
3203 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
3208 /* Resolve a transfer statement. This is making sure that:
3209 -- a derived type being transferred has only non-pointer components
3210 -- a derived type being transferred doesn't have private components, unless
3211 it's being transferred from the module where the type was defined
3212 -- we're not trying to transfer a whole assumed size array. */
3215 resolve_transfer (gfc_code * code)
3224 if (exp->expr_type != EXPR_VARIABLE)
3227 sym = exp->symtree->n.sym;
3230 /* Go to actual component transferred. */
3231 for (ref = code->expr->ref; ref; ref = ref->next)
3232 if (ref->type == REF_COMPONENT)
3233 ts = &ref->u.c.component->ts;
3235 if (ts->type == BT_DERIVED)
3237 /* Check that transferred derived type doesn't contain POINTER
3239 if (derived_pointer (ts->derived))
3241 gfc_error ("Data transfer element at %L cannot have "
3242 "POINTER components", &code->loc);
3246 if (derived_inaccessible (ts->derived))
3248 gfc_error ("Data transfer element at %L cannot have "
3249 "PRIVATE components",&code->loc);
3254 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
3255 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
3257 gfc_error ("Data transfer element at %L cannot be a full reference to "
3258 "an assumed-size array", &code->loc);
3264 /*********** Toplevel code resolution subroutines ***********/
3266 /* Given a branch to a label and a namespace, if the branch is conforming.
3267 The code node described where the branch is located. */
3270 resolve_branch (gfc_st_label * label, gfc_code * code)
3272 gfc_code *block, *found;
3280 /* Step one: is this a valid branching target? */
3282 if (lp->defined == ST_LABEL_UNKNOWN)
3284 gfc_error ("Label %d referenced at %L is never defined", lp->value,
3289 if (lp->defined != ST_LABEL_TARGET)
3291 gfc_error ("Statement at %L is not a valid branch target statement "
3292 "for the branch statement at %L", &lp->where, &code->loc);
3296 /* Step two: make sure this branch is not a branch to itself ;-) */
3298 if (code->here == label)
3300 gfc_warning ("Branch at %L causes an infinite loop", &code->loc);
3304 /* Step three: Try to find the label in the parse tree. To do this,
3305 we traverse the tree block-by-block: first the block that
3306 contains this GOTO, then the block that it is nested in, etc. We
3307 can ignore other blocks because branching into another block is
3312 for (stack = cs_base; stack; stack = stack->prev)
3314 for (block = stack->head; block; block = block->next)
3316 if (block->here == label)
3329 /* still nothing, so illegal. */
3330 gfc_error_now ("Label at %L is not in the same block as the "
3331 "GOTO statement at %L", &lp->where, &code->loc);
3335 /* Step four: Make sure that the branching target is legal if
3336 the statement is an END {SELECT,DO,IF}. */
3338 if (found->op == EXEC_NOP)
3340 for (stack = cs_base; stack; stack = stack->prev)
3341 if (stack->current->next == found)
3345 gfc_notify_std (GFC_STD_F95_DEL,
3346 "Obsolete: GOTO at %L jumps to END of construct at %L",
3347 &code->loc, &found->loc);
3352 /* Check whether EXPR1 has the same shape as EXPR2. */
3355 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
3357 mpz_t shape[GFC_MAX_DIMENSIONS];
3358 mpz_t shape2[GFC_MAX_DIMENSIONS];
3359 try result = FAILURE;
3362 /* Compare the rank. */
3363 if (expr1->rank != expr2->rank)
3366 /* Compare the size of each dimension. */
3367 for (i=0; i<expr1->rank; i++)
3369 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
3372 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
3375 if (mpz_cmp (shape[i], shape2[i]))
3379 /* When either of the two expression is an assumed size array, we
3380 ignore the comparison of dimension sizes. */
3385 for (i--; i>=0; i--)
3387 mpz_clear (shape[i]);
3388 mpz_clear (shape2[i]);
3394 /* Check whether a WHERE assignment target or a WHERE mask expression
3395 has the same shape as the outmost WHERE mask expression. */
3398 resolve_where (gfc_code *code, gfc_expr *mask)
3404 cblock = code->block;
3406 /* Store the first WHERE mask-expr of the WHERE statement or construct.
3407 In case of nested WHERE, only the outmost one is stored. */
3408 if (mask == NULL) /* outmost WHERE */
3410 else /* inner WHERE */
3417 /* Check if the mask-expr has a consistent shape with the
3418 outmost WHERE mask-expr. */
3419 if (resolve_where_shape (cblock->expr, e) == FAILURE)
3420 gfc_error ("WHERE mask at %L has inconsistent shape",
3421 &cblock->expr->where);
3424 /* the assignment statement of a WHERE statement, or the first
3425 statement in where-body-construct of a WHERE construct */
3426 cnext = cblock->next;
3431 /* WHERE assignment statement */
3434 /* Check shape consistent for WHERE assignment target. */
3435 if (e && resolve_where_shape (cnext->expr, e) == FAILURE)
3436 gfc_error ("WHERE assignment target at %L has "
3437 "inconsistent shape", &cnext->expr->where);
3440 /* WHERE or WHERE construct is part of a where-body-construct */
3442 resolve_where (cnext, e);
3446 gfc_error ("Unsupported statement inside WHERE at %L",
3449 /* the next statement within the same where-body-construct */
3450 cnext = cnext->next;
3452 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
3453 cblock = cblock->block;
3458 /* Check whether the FORALL index appears in the expression or not. */
3461 gfc_find_forall_index (gfc_expr *expr, gfc_symbol *symbol)
3465 gfc_actual_arglist *args;
3468 switch (expr->expr_type)
3471 gcc_assert (expr->symtree->n.sym);
3473 /* A scalar assignment */
3476 if (expr->symtree->n.sym == symbol)
3482 /* the expr is array ref, substring or struct component. */
3489 /* Check if the symbol appears in the array subscript. */
3491 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
3494 if (gfc_find_forall_index (ar.start[i], symbol) == SUCCESS)
3498 if (gfc_find_forall_index (ar.end[i], symbol) == SUCCESS)
3502 if (gfc_find_forall_index (ar.stride[i], symbol) == SUCCESS)
3508 if (expr->symtree->n.sym == symbol)
3511 /* Check if the symbol appears in the substring section. */
3512 if (gfc_find_forall_index (tmp->u.ss.start, symbol) == SUCCESS)
3514 if (gfc_find_forall_index (tmp->u.ss.end, symbol) == SUCCESS)
3522 gfc_error("expresion reference type error at %L", &expr->where);
3528 /* If the expression is a function call, then check if the symbol
3529 appears in the actual arglist of the function. */
3531 for (args = expr->value.function.actual; args; args = args->next)
3533 if (gfc_find_forall_index(args->expr,symbol) == SUCCESS)
3538 /* It seems not to happen. */
3539 case EXPR_SUBSTRING:
3543 gcc_assert (expr->ref->type == REF_SUBSTRING);
3544 if (gfc_find_forall_index (tmp->u.ss.start, symbol) == SUCCESS)
3546 if (gfc_find_forall_index (tmp->u.ss.end, symbol) == SUCCESS)
3551 /* It seems not to happen. */
3552 case EXPR_STRUCTURE:
3554 gfc_error ("Unsupported statement while finding forall index in "
3559 /* Find the FORALL index in the first operand. */
3560 if (expr->value.op.op1)
3562 if (gfc_find_forall_index (expr->value.op.op1, symbol) == SUCCESS)
3566 /* Find the FORALL index in the second operand. */
3567 if (expr->value.op.op2)
3569 if (gfc_find_forall_index (expr->value.op.op2, symbol) == SUCCESS)
3582 /* Resolve assignment in FORALL construct.
3583 NVAR is the number of FORALL index variables, and VAR_EXPR records the
3584 FORALL index variables. */
3587 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
3591 for (n = 0; n < nvar; n++)
3593 gfc_symbol *forall_index;
3595 forall_index = var_expr[n]->symtree->n.sym;
3597 /* Check whether the assignment target is one of the FORALL index
3599 if ((code->expr->expr_type == EXPR_VARIABLE)
3600 && (code->expr->symtree->n.sym == forall_index))
3601 gfc_error ("Assignment to a FORALL index variable at %L",
3602 &code->expr->where);
3605 /* If one of the FORALL index variables doesn't appear in the
3606 assignment target, then there will be a many-to-one
3608 if (gfc_find_forall_index (code->expr, forall_index) == FAILURE)
3609 gfc_error ("The FORALL with index '%s' cause more than one "
3610 "assignment to this object at %L",
3611 var_expr[n]->symtree->name, &code->expr->where);
3617 /* Resolve WHERE statement in FORALL construct. */
3620 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr){
3624 cblock = code->block;
3627 /* the assignment statement of a WHERE statement, or the first
3628 statement in where-body-construct of a WHERE construct */
3629 cnext = cblock->next;
3634 /* WHERE assignment statement */
3636 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
3639 /* WHERE or WHERE construct is part of a where-body-construct */
3641 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
3645 gfc_error ("Unsupported statement inside WHERE at %L",
3648 /* the next statement within the same where-body-construct */
3649 cnext = cnext->next;
3651 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
3652 cblock = cblock->block;
3657 /* Traverse the FORALL body to check whether the following errors exist:
3658 1. For assignment, check if a many-to-one assignment happens.
3659 2. For WHERE statement, check the WHERE body to see if there is any
3660 many-to-one assignment. */
3663 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
3667 c = code->block->next;
3673 case EXEC_POINTER_ASSIGN:
3674 gfc_resolve_assign_in_forall (c, nvar, var_expr);
3677 /* Because the resolve_blocks() will handle the nested FORALL,
3678 there is no need to handle it here. */
3682 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
3687 /* The next statement in the FORALL body. */
3693 /* Given a FORALL construct, first resolve the FORALL iterator, then call
3694 gfc_resolve_forall_body to resolve the FORALL body. */
3696 static void resolve_blocks (gfc_code *, gfc_namespace *);
3699 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
3701 static gfc_expr **var_expr;
3702 static int total_var = 0;
3703 static int nvar = 0;
3704 gfc_forall_iterator *fa;
3705 gfc_symbol *forall_index;
3709 /* Start to resolve a FORALL construct */
3710 if (forall_save == 0)
3712 /* Count the total number of FORALL index in the nested FORALL
3713 construct in order to allocate the VAR_EXPR with proper size. */
3715 while ((next != NULL) && (next->op == EXEC_FORALL))
3717 for (fa = next->ext.forall_iterator; fa; fa = fa->next)
3719 next = next->block->next;
3722 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
3723 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
3726 /* The information about FORALL iterator, including FORALL index start, end
3727 and stride. The FORALL index can not appear in start, end or stride. */
3728 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
3730 /* Check if any outer FORALL index name is the same as the current
3732 for (i = 0; i < nvar; i++)
3734 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
3736 gfc_error ("An outer FORALL construct already has an index "
3737 "with this name %L", &fa->var->where);
3741 /* Record the current FORALL index. */
3742 var_expr[nvar] = gfc_copy_expr (fa->var);
3744 forall_index = fa->var->symtree->n.sym;
3746 /* Check if the FORALL index appears in start, end or stride. */
3747 if (gfc_find_forall_index (fa->start, forall_index) == SUCCESS)
3748 gfc_error ("A FORALL index must not appear in a limit or stride "
3749 "expression in the same FORALL at %L", &fa->start->where);
3750 if (gfc_find_forall_index (fa->end, forall_index) == SUCCESS)
3751 gfc_error ("A FORALL index must not appear in a limit or stride "
3752 "expression in the same FORALL at %L", &fa->end->where);
3753 if (gfc_find_forall_index (fa->stride, forall_index) == SUCCESS)
3754 gfc_error ("A FORALL index must not appear in a limit or stride "
3755 "expression in the same FORALL at %L", &fa->stride->where);
3759 /* Resolve the FORALL body. */
3760 gfc_resolve_forall_body (code, nvar, var_expr);
3762 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
3763 resolve_blocks (code->block, ns);
3765 /* Free VAR_EXPR after the whole FORALL construct resolved. */
3766 for (i = 0; i < total_var; i++)
3767 gfc_free_expr (var_expr[i]);
3769 /* Reset the counters. */
3775 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL ,GOTO and
3778 static void resolve_code (gfc_code *, gfc_namespace *);
3781 resolve_blocks (gfc_code * b, gfc_namespace * ns)
3785 for (; b; b = b->block)
3787 t = gfc_resolve_expr (b->expr);
3788 if (gfc_resolve_expr (b->expr2) == FAILURE)
3794 if (t == SUCCESS && b->expr != NULL
3795 && (b->expr->ts.type != BT_LOGICAL || b->expr->rank != 0))
3797 ("ELSE IF clause at %L requires a scalar LOGICAL expression",
3804 && (b->expr->ts.type != BT_LOGICAL
3805 || b->expr->rank == 0))
3807 ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
3812 resolve_branch (b->label, b);
3822 gfc_internal_error ("resolve_block(): Bad block type");
3825 resolve_code (b->next, ns);
3830 /* Given a block of code, recursively resolve everything pointed to by this
3834 resolve_code (gfc_code * code, gfc_namespace * ns)
3836 int forall_save = 0;
3841 frame.prev = cs_base;
3845 for (; code; code = code->next)
3847 frame.current = code;
3849 if (code->op == EXEC_FORALL)
3851 forall_save = forall_flag;
3853 gfc_resolve_forall (code, ns, forall_save);
3856 resolve_blocks (code->block, ns);
3858 if (code->op == EXEC_FORALL)
3859 forall_flag = forall_save;
3861 t = gfc_resolve_expr (code->expr);
3862 if (gfc_resolve_expr (code->expr2) == FAILURE)
3878 resolve_where (code, NULL);
3882 if (code->expr != NULL)
3884 if (code->expr->ts.type != BT_INTEGER)
3885 gfc_error ("ASSIGNED GOTO statement at %L requires an INTEGER "
3886 "variable", &code->expr->where);
3887 else if (code->expr->symtree->n.sym->attr.assign != 1)
3888 gfc_error ("Variable '%s' has not been assigned a target label "
3889 "at %L", code->expr->symtree->n.sym->name,
3890 &code->expr->where);
3893 resolve_branch (code->label, code);
3897 if (code->expr != NULL && code->expr->ts.type != BT_INTEGER)
3898 gfc_error ("Alternate RETURN statement at %L requires an INTEGER "
3899 "return specifier", &code->expr->where);
3906 if (gfc_extend_assign (code, ns) == SUCCESS)
3909 if (gfc_pure (NULL))
3911 if (gfc_impure_variable (code->expr->symtree->n.sym))
3914 ("Cannot assign to variable '%s' in PURE procedure at %L",
3915 code->expr->symtree->n.sym->name, &code->expr->where);
3919 if (code->expr2->ts.type == BT_DERIVED
3920 && derived_pointer (code->expr2->ts.derived))
3923 ("Right side of assignment at %L is a derived type "
3924 "containing a POINTER in a PURE procedure",
3925 &code->expr2->where);
3930 gfc_check_assign (code->expr, code->expr2, 1);
3933 case EXEC_LABEL_ASSIGN:
3934 if (code->label->defined == ST_LABEL_UNKNOWN)
3935 gfc_error ("Label %d referenced at %L is never defined",
3936 code->label->value, &code->label->where);
3938 && (code->expr->expr_type != EXPR_VARIABLE
3939 || code->expr->symtree->n.sym->ts.type != BT_INTEGER
3940 || code->expr->symtree->n.sym->ts.kind
3941 != gfc_default_integer_kind
3942 || code->expr->symtree->n.sym->as != NULL))
3943 gfc_error ("ASSIGN statement at %L requires a scalar "
3944 "default INTEGER variable", &code->expr->where);
3947 case EXEC_POINTER_ASSIGN:
3951 gfc_check_pointer_assign (code->expr, code->expr2);
3954 case EXEC_ARITHMETIC_IF:
3956 && code->expr->ts.type != BT_INTEGER
3957 && code->expr->ts.type != BT_REAL)
3958 gfc_error ("Arithmetic IF statement at %L requires a numeric "
3959 "expression", &code->expr->where);
3961 resolve_branch (code->label, code);
3962 resolve_branch (code->label2, code);
3963 resolve_branch (code->label3, code);
3967 if (t == SUCCESS && code->expr != NULL
3968 && (code->expr->ts.type != BT_LOGICAL
3969 || code->expr->rank != 0))
3970 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
3971 &code->expr->where);
3976 resolve_call (code);
3980 /* Select is complicated. Also, a SELECT construct could be
3981 a transformed computed GOTO. */
3982 resolve_select (code);
3986 if (code->ext.iterator != NULL)
3987 gfc_resolve_iterator (code->ext.iterator, true);
3991 if (code->expr == NULL)
3992 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
3994 && (code->expr->rank != 0
3995 || code->expr->ts.type != BT_LOGICAL))
3996 gfc_error ("Exit condition of DO WHILE loop at %L must be "
3997 "a scalar LOGICAL expression", &code->expr->where);
4001 if (t == SUCCESS && code->expr != NULL
4002 && code->expr->ts.type != BT_INTEGER)
4003 gfc_error ("STAT tag in ALLOCATE statement at %L must be "
4004 "of type INTEGER", &code->expr->where);
4006 for (a = code->ext.alloc_list; a; a = a->next)
4007 resolve_allocate_expr (a->expr);
4011 case EXEC_DEALLOCATE:
4012 if (t == SUCCESS && code->expr != NULL
4013 && code->expr->ts.type != BT_INTEGER)
4015 ("STAT tag in DEALLOCATE statement at %L must be of type "
4016 "INTEGER", &code->expr->where);
4018 for (a = code->ext.alloc_list; a; a = a->next)
4019 resolve_deallocate_expr (a->expr);
4024 if (gfc_resolve_open (code->ext.open) == FAILURE)
4027 resolve_branch (code->ext.open->err, code);
4031 if (gfc_resolve_close (code->ext.close) == FAILURE)
4034 resolve_branch (code->ext.close->err, code);
4037 case EXEC_BACKSPACE:
4041 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
4044 resolve_branch (code->ext.filepos->err, code);
4048 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
4051 resolve_branch (code->ext.inquire->err, code);
4055 gcc_assert (code->ext.inquire != NULL);
4056 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
4059 resolve_branch (code->ext.inquire->err, code);
4064 if (gfc_resolve_dt (code->ext.dt) == FAILURE)
4067 resolve_branch (code->ext.dt->err, code);
4068 resolve_branch (code->ext.dt->end, code);
4069 resolve_branch (code->ext.dt->eor, code);
4073 resolve_transfer (code);
4077 resolve_forall_iterators (code->ext.forall_iterator);
4079 if (code->expr != NULL && code->expr->ts.type != BT_LOGICAL)
4081 ("FORALL mask clause at %L requires a LOGICAL expression",
4082 &code->expr->where);
4086 gfc_internal_error ("resolve_code(): Bad statement code");
4090 cs_base = frame.prev;
4094 /* Resolve initial values and make sure they are compatible with
4098 resolve_values (gfc_symbol * sym)
4101 if (sym->value == NULL)
4104 if (gfc_resolve_expr (sym->value) == FAILURE)
4107 gfc_check_assign_symbol (sym, sym->value);
4111 /* Do anything necessary to resolve a symbol. Right now, we just
4112 assume that an otherwise unknown symbol is a variable. This sort
4113 of thing commonly happens for symbols in module. */
4116 resolve_symbol (gfc_symbol * sym)
4118 /* Zero if we are checking a formal namespace. */
4119 static int formal_ns_flag = 1;
4120 int formal_ns_save, check_constant, mp_flag;
4124 gfc_symtree * symtree;
4125 gfc_symtree * this_symtree;
4128 if (sym->attr.flavor == FL_UNKNOWN)
4131 /* If we find that a flavorless symbol is an interface in one of the
4132 parent namespaces, find its symtree in this namespace, free the
4133 symbol and set the symtree to point to the interface symbol. */
4134 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
4136 symtree = gfc_find_symtree (ns->sym_root, sym->name);
4137 if (symtree && symtree->n.sym->generic)
4139 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
4143 gfc_free_symbol (sym);
4144 symtree->n.sym->refs++;
4145 this_symtree->n.sym = symtree->n.sym;
4150 /* Otherwise give it a flavor according to such attributes as
4152 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
4153 sym->attr.flavor = FL_VARIABLE;
4156 sym->attr.flavor = FL_PROCEDURE;
4157 if (sym->attr.dimension)
4158 sym->attr.function = 1;
4162 /* Symbols that are module procedures with results (functions) have
4163 the types and array specification copied for type checking in
4164 procedures that call them, as well as for saving to a module