1 /* Perform type resolution on the various stuctures.
2 Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GNU G95.
7 GNU G95 is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU G95 is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU G95; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "arith.h" /* For gfc_compare_expr(). */
28 /* Stack to push the current if we descend into a block during
29 resolution. See resolve_branch() and resolve_code(). */
31 typedef struct code_stack
33 struct gfc_code *head, *current;
34 struct code_stack *prev;
38 static code_stack *cs_base = NULL;
41 /* Nonzero if we're inside a FORALL block */
43 static int forall_flag;
45 /* Resolve types of formal argument lists. These have to be done early so that
46 the formal argument lists of module procedures can be copied to the
47 containing module before the individual procedures are resolved
48 individually. We also resolve argument lists of procedures in interface
49 blocks because they are self-contained scoping units.
51 Since a dummy argument cannot be a non-dummy procedure, the only
52 resort left for untyped names are the IMPLICIT types. */
55 resolve_formal_arglist (gfc_symbol * proc)
57 gfc_formal_arglist *f;
61 /* TODO: Procedures whose return character length parameter is not constant
62 or assumed must also have explicit interfaces. */
63 if (proc->result != NULL)
68 if (gfc_elemental (proc)
69 || sym->attr.pointer || sym->attr.allocatable
70 || (sym->as && sym->as->rank > 0))
71 proc->attr.always_explicit = 1;
73 for (f = proc->formal; f; f = f->next)
79 /* Alternate return placeholder. */
80 if (gfc_elemental (proc))
81 gfc_error ("Alternate return specifier in elemental subroutine "
82 "'%s' at %L is not allowed", proc->name,
84 if (proc->attr.function)
85 gfc_error ("Alternate return specifier in function "
86 "'%s' at %L is not allowed", proc->name,
91 if (sym->attr.if_source != IFSRC_UNKNOWN)
92 resolve_formal_arglist (sym);
94 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
96 if (gfc_pure (proc) && !gfc_pure (sym))
99 ("Dummy procedure '%s' of PURE procedure at %L must also "
100 "be PURE", sym->name, &sym->declared_at);
104 if (gfc_elemental (proc))
107 ("Dummy procedure at %L not allowed in ELEMENTAL procedure",
115 if (sym->ts.type == BT_UNKNOWN)
117 if (!sym->attr.function || sym->result == sym)
118 gfc_set_default_type (sym, 1, sym->ns);
121 /* Set the type of the RESULT, then copy. */
122 if (sym->result->ts.type == BT_UNKNOWN)
123 gfc_set_default_type (sym->result, 1, sym->result->ns);
125 sym->ts = sym->result->ts;
127 sym->as = gfc_copy_array_spec (sym->result->as);
131 gfc_resolve_array_spec (sym->as, 0);
133 /* We can't tell if an array with dimension (:) is assumed or deferred
134 shape until we know if it has the pointer or allocatable attributes.
136 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
137 && !(sym->attr.pointer || sym->attr.allocatable))
139 sym->as->type = AS_ASSUMED_SHAPE;
140 for (i = 0; i < sym->as->rank; i++)
141 sym->as->lower[i] = gfc_int_expr (1);
144 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
145 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
146 || sym->attr.optional)
147 proc->attr.always_explicit = 1;
149 /* If the flavor is unknown at this point, it has to be a variable.
150 A procedure specification would have already set the type. */
152 if (sym->attr.flavor == FL_UNKNOWN)
153 gfc_add_flavor (&sym->attr, FL_VARIABLE, &sym->declared_at);
157 if (proc->attr.function && !sym->attr.pointer
158 && sym->attr.flavor != FL_PROCEDURE
159 && sym->attr.intent != INTENT_IN)
161 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
162 "INTENT(IN)", sym->name, proc->name,
165 if (proc->attr.subroutine && !sym->attr.pointer
166 && sym->attr.intent == INTENT_UNKNOWN)
169 ("Argument '%s' of pure subroutine '%s' at %L must have "
170 "its INTENT specified", sym->name, proc->name,
175 if (gfc_elemental (proc))
180 ("Argument '%s' of elemental procedure at %L must be scalar",
181 sym->name, &sym->declared_at);
185 if (sym->attr.pointer)
188 ("Argument '%s' of elemental procedure at %L cannot have "
189 "the POINTER attribute", sym->name, &sym->declared_at);
194 /* Each dummy shall be specified to be scalar. */
195 if (proc->attr.proc == PROC_ST_FUNCTION)
200 ("Argument '%s' of statement function at %L must be scalar",
201 sym->name, &sym->declared_at);
205 if (sym->ts.type == BT_CHARACTER)
207 gfc_charlen *cl = sym->ts.cl;
208 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
211 ("Character-valued argument '%s' of statement function at "
212 "%L must has constant length",
213 sym->name, &sym->declared_at);
222 /* Work function called when searching for symbols that have argument lists
223 associated with them. */
226 find_arglists (gfc_symbol * sym)
229 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
232 resolve_formal_arglist (sym);
236 /* Given a namespace, resolve all formal argument lists within the namespace.
240 resolve_formal_arglists (gfc_namespace * ns)
246 gfc_traverse_ns (ns, find_arglists);
250 /* Resolve contained function types. Because contained functions can call one
251 another, they have to be worked out before any of the contained procedures
254 The good news is that if a function doesn't already have a type, the only
255 way it can get one is through an IMPLICIT type or a RESULT variable, because
256 by definition contained functions are contained namespace they're contained
257 in, not in a sibling or parent namespace. */
260 resolve_contained_functions (gfc_namespace * ns)
262 gfc_symbol *contained_sym, *sym_lower;
263 gfc_namespace *child;
266 resolve_formal_arglists (ns);
268 for (child = ns->contained; child; child = child->sibling)
270 sym_lower = child->proc_name;
272 /* If this namespace is not a function, ignore it. */
274 || !( sym_lower->attr.function
275 || sym_lower->attr.flavor == FL_VARIABLE))
278 /* Find the contained symbol in the current namespace. */
279 gfc_find_symbol (sym_lower->name, ns, 0, &contained_sym);
281 if (contained_sym == NULL)
282 gfc_internal_error ("resolve_contained_functions(): Contained "
283 "function not found in parent namespace");
285 /* Try to find out of what type the function is. If there was an
286 explicit RESULT clause, try to get the type from it. If the
287 function is never defined, set it to the implicit type. If
288 even that fails, give up. */
289 if (sym_lower->result != NULL)
290 sym_lower = sym_lower->result;
292 if (sym_lower->ts.type == BT_UNKNOWN)
294 /* Assume we can find an implicit type. */
297 if (sym_lower->result == NULL)
298 t = gfc_set_default_type (sym_lower, 0, child);
301 if (sym_lower->result->ts.type == BT_UNKNOWN)
302 t = gfc_set_default_type (sym_lower->result, 0, NULL);
304 sym_lower->ts = sym_lower->result->ts;
308 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
309 sym_lower->name, &sym_lower->declared_at); /* FIXME */
312 /* If the symbol in the parent of the contained namespace is not
313 the same as the one in contained namespace itself, copy over
314 the type information. */
315 /* ??? Shouldn't we replace the symbol with the parent symbol instead? */
316 if (contained_sym != sym_lower)
318 contained_sym->ts = sym_lower->ts;
319 contained_sym->as = gfc_copy_array_spec (sym_lower->as);
325 /* Resolve all of the elements of a structure constructor and make sure that
326 the types are correct. */
329 resolve_structure_cons (gfc_expr * expr)
331 gfc_constructor *cons;
336 cons = expr->value.constructor;
337 /* A constructor may have references if it is the result of substituting a
338 parameter variable. In this case we just pull out the component we
341 comp = expr->ref->u.c.sym->components;
343 comp = expr->ts.derived->components;
345 for (; comp; comp = comp->next, cons = cons->next)
353 if (gfc_resolve_expr (cons->expr) == FAILURE)
359 /* If we don't have the right type, try to convert it. */
361 if (!gfc_compare_types (&cons->expr->ts, &comp->ts)
362 && gfc_convert_type (cons->expr, &comp->ts, 1) == FAILURE)
371 /****************** Expression name resolution ******************/
373 /* Returns 0 if a symbol was not declared with a type or
374 or attribute declaration statement, nonzero otherwise. */
377 was_declared (gfc_symbol * sym)
383 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
386 if (a.allocatable || a.dimension || a.external || a.intrinsic
387 || a.optional || a.pointer || a.save || a.target
388 || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
395 /* Determine if a symbol is generic or not. */
398 generic_sym (gfc_symbol * sym)
402 if (sym->attr.generic ||
403 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
406 if (was_declared (sym) || sym->ns->parent == NULL)
409 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
411 return (s == NULL) ? 0 : generic_sym (s);
415 /* Determine if a symbol is specific or not. */
418 specific_sym (gfc_symbol * sym)
422 if (sym->attr.if_source == IFSRC_IFBODY
423 || sym->attr.proc == PROC_MODULE
424 || sym->attr.proc == PROC_INTERNAL
425 || sym->attr.proc == PROC_ST_FUNCTION
426 || (sym->attr.intrinsic &&
427 gfc_specific_intrinsic (sym->name))
428 || sym->attr.external)
431 if (was_declared (sym) || sym->ns->parent == NULL)
434 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
436 return (s == NULL) ? 0 : specific_sym (s);
440 /* Figure out if the procedure is specific, generic or unknown. */
443 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
447 procedure_kind (gfc_symbol * sym)
450 if (generic_sym (sym))
451 return PTYPE_GENERIC;
453 if (specific_sym (sym))
454 return PTYPE_SPECIFIC;
456 return PTYPE_UNKNOWN;
460 /* Resolve an actual argument list. Most of the time, this is just
461 resolving the expressions in the list.
462 The exception is that we sometimes have to decide whether arguments
463 that look like procedure arguments are really simple variable
467 resolve_actual_arglist (gfc_actual_arglist * arg)
470 gfc_symtree *parent_st;
473 for (; arg; arg = arg->next)
479 /* Check the label is a valid branching target. */
482 if (arg->label->defined == ST_LABEL_UNKNOWN)
484 gfc_error ("Label %d referenced at %L is never defined",
485 arg->label->value, &arg->label->where);
492 if (e->ts.type != BT_PROCEDURE)
494 if (gfc_resolve_expr (e) != SUCCESS)
499 /* See if the expression node should really be a variable
502 sym = e->symtree->n.sym;
504 if (sym->attr.flavor == FL_PROCEDURE
505 || sym->attr.intrinsic
506 || sym->attr.external)
509 /* If the symbol is the function that names the current (or
510 parent) scope, then we really have a variable reference. */
512 if (sym->attr.function && sym->result == sym
513 && (sym->ns->proc_name == sym
514 || (sym->ns->parent != NULL
515 && sym->ns->parent->proc_name == sym)))
521 /* See if the name is a module procedure in a parent unit. */
523 if (was_declared (sym) || sym->ns->parent == NULL)
526 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
528 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
532 if (parent_st == NULL)
535 sym = parent_st->n.sym;
536 e->symtree = parent_st; /* Point to the right thing. */
538 if (sym->attr.flavor == FL_PROCEDURE
539 || sym->attr.intrinsic
540 || sym->attr.external)
546 e->expr_type = EXPR_VARIABLE;
550 e->rank = sym->as->rank;
551 e->ref = gfc_get_ref ();
552 e->ref->type = REF_ARRAY;
553 e->ref->u.ar.type = AR_FULL;
554 e->ref->u.ar.as = sym->as;
562 /************* Function resolution *************/
564 /* Resolve a function call known to be generic.
565 Section 14.1.2.4.1. */
568 resolve_generic_f0 (gfc_expr * expr, gfc_symbol * sym)
572 if (sym->attr.generic)
575 gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
578 expr->value.function.name = s->name;
579 expr->value.function.esym = s;
582 expr->rank = s->as->rank;
586 /* TODO: Need to search for elemental references in generic interface */
589 if (sym->attr.intrinsic)
590 return gfc_intrinsic_func_interface (expr, 0);
597 resolve_generic_f (gfc_expr * expr)
602 sym = expr->symtree->n.sym;
606 m = resolve_generic_f0 (expr, sym);
609 else if (m == MATCH_ERROR)
613 if (sym->ns->parent == NULL)
615 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
619 if (!generic_sym (sym))
623 /* Last ditch attempt. */
625 if (!gfc_generic_intrinsic (expr->symtree->n.sym->name))
627 gfc_error ("Generic function '%s' at %L is not an intrinsic function",
628 expr->symtree->n.sym->name, &expr->where);
632 m = gfc_intrinsic_func_interface (expr, 0);
637 ("Generic function '%s' at %L is not consistent with a specific "
638 "intrinsic interface", expr->symtree->n.sym->name, &expr->where);
644 /* Resolve a function call known to be specific. */
647 resolve_specific_f0 (gfc_symbol * sym, gfc_expr * expr)
651 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
655 sym->attr.proc = PROC_DUMMY;
659 sym->attr.proc = PROC_EXTERNAL;
663 if (sym->attr.proc == PROC_MODULE
664 || sym->attr.proc == PROC_ST_FUNCTION
665 || sym->attr.proc == PROC_INTERNAL)
668 if (sym->attr.intrinsic)
670 m = gfc_intrinsic_func_interface (expr, 1);
675 ("Function '%s' at %L is INTRINSIC but is not compatible with "
676 "an intrinsic", sym->name, &expr->where);
684 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
687 expr->value.function.name = sym->name;
688 expr->value.function.esym = sym;
690 expr->rank = sym->as->rank;
697 resolve_specific_f (gfc_expr * expr)
702 sym = expr->symtree->n.sym;
706 m = resolve_specific_f0 (sym, expr);
709 if (m == MATCH_ERROR)
712 if (sym->ns->parent == NULL)
715 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
721 gfc_error ("Unable to resolve the specific function '%s' at %L",
722 expr->symtree->n.sym->name, &expr->where);
728 /* Resolve a procedure call not known to be generic nor specific. */
731 resolve_unknown_f (gfc_expr * expr)
736 sym = expr->symtree->n.sym;
740 sym->attr.proc = PROC_DUMMY;
741 expr->value.function.name = sym->name;
745 /* See if we have an intrinsic function reference. */
747 if (gfc_intrinsic_name (sym->name, 0))
749 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
754 /* The reference is to an external name. */
756 sym->attr.proc = PROC_EXTERNAL;
757 expr->value.function.name = sym->name;
758 expr->value.function.esym = expr->symtree->n.sym;
761 expr->rank = sym->as->rank;
763 /* Type of the expression is either the type of the symbol or the
764 default type of the symbol. */
767 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
769 if (sym->ts.type != BT_UNKNOWN)
773 ts = gfc_get_default_type (sym, sym->ns);
775 if (ts->type == BT_UNKNOWN)
777 gfc_error ("Function '%s' at %L has no implicit type",
778 sym->name, &expr->where);
789 /* Figure out if if a function reference is pure or not. Also sets the name
790 of the function for a potential error message. Returns nonzero if the
791 function is PURE, zero if not. */
794 pure_function (gfc_expr * e, char **name)
798 if (e->value.function.esym)
800 pure = gfc_pure (e->value.function.esym);
801 *name = e->value.function.esym->name;
803 else if (e->value.function.isym)
805 pure = e->value.function.isym->pure
806 || e->value.function.isym->elemental;
807 *name = e->value.function.isym->name;
811 /* Implicit functions are not pure. */
813 *name = e->value.function.name;
820 /* Resolve a function call, which means resolving the arguments, then figuring
821 out which entity the name refers to. */
822 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
823 to INTENT(OUT) or INTENT(INOUT). */
826 resolve_function (gfc_expr * expr)
828 gfc_actual_arglist *arg;
832 if (resolve_actual_arglist (expr->value.function.actual) == FAILURE)
835 /* See if function is already resolved. */
837 if (expr->value.function.name != NULL)
839 if (expr->ts.type == BT_UNKNOWN)
840 expr->ts = expr->symtree->n.sym->ts;
845 /* Apply the rules of section 14.1.2. */
847 switch (procedure_kind (expr->symtree->n.sym))
850 t = resolve_generic_f (expr);
854 t = resolve_specific_f (expr);
858 t = resolve_unknown_f (expr);
862 gfc_internal_error ("resolve_function(): bad function type");
866 /* If the expression is still a function (it might have simplified),
867 then we check to see if we are calling an elemental function. */
869 if (expr->expr_type != EXPR_FUNCTION)
872 if (expr->value.function.actual != NULL
873 && ((expr->value.function.esym != NULL
874 && expr->value.function.esym->attr.elemental)
875 || (expr->value.function.isym != NULL
876 && expr->value.function.isym->elemental)))
879 /* The rank of an elemental is the rank of its array argument(s). */
881 for (arg = expr->value.function.actual; arg; arg = arg->next)
883 if (arg->expr != NULL && arg->expr->rank > 0)
885 expr->rank = arg->expr->rank;
891 if (!pure_function (expr, &name))
896 ("Function reference to '%s' at %L is inside a FORALL block",
900 else if (gfc_pure (NULL))
902 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
903 "procedure within a PURE procedure", name, &expr->where);
912 /************* Subroutine resolution *************/
915 pure_subroutine (gfc_code * c, gfc_symbol * sym)
922 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
924 else if (gfc_pure (NULL))
925 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
931 resolve_generic_s0 (gfc_code * c, gfc_symbol * sym)
935 if (sym->attr.generic)
937 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
941 pure_subroutine (c, s);
945 /* TODO: Need to search for elemental references in generic interface. */
948 if (sym->attr.intrinsic)
949 return gfc_intrinsic_sub_interface (c, 0);
956 resolve_generic_s (gfc_code * c)
961 sym = c->symtree->n.sym;
963 m = resolve_generic_s0 (c, sym);
966 if (m == MATCH_ERROR)
969 if (sym->ns->parent != NULL)
971 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
974 m = resolve_generic_s0 (c, sym);
977 if (m == MATCH_ERROR)
982 /* Last ditch attempt. */
984 if (!gfc_generic_intrinsic (sym->name))
987 ("Generic subroutine '%s' at %L is not an intrinsic subroutine",
992 m = gfc_intrinsic_sub_interface (c, 0);
996 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
997 "intrinsic subroutine interface", sym->name, &c->loc);
1003 /* Resolve a subroutine call known to be specific. */
1006 resolve_specific_s0 (gfc_code * c, gfc_symbol * sym)
1010 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1012 if (sym->attr.dummy)
1014 sym->attr.proc = PROC_DUMMY;
1018 sym->attr.proc = PROC_EXTERNAL;
1022 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
1025 if (sym->attr.intrinsic)
1027 m = gfc_intrinsic_sub_interface (c, 1);
1031 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
1032 "with an intrinsic", sym->name, &c->loc);
1040 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1042 c->resolved_sym = sym;
1043 pure_subroutine (c, sym);
1050 resolve_specific_s (gfc_code * c)
1055 sym = c->symtree->n.sym;
1057 m = resolve_specific_s0 (c, sym);
1060 if (m == MATCH_ERROR)
1063 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1067 m = resolve_specific_s0 (c, sym);
1070 if (m == MATCH_ERROR)
1074 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
1075 sym->name, &c->loc);
1081 /* Resolve a subroutine call not known to be generic nor specific. */
1084 resolve_unknown_s (gfc_code * c)
1088 sym = c->symtree->n.sym;
1090 if (sym->attr.dummy)
1092 sym->attr.proc = PROC_DUMMY;
1096 /* See if we have an intrinsic function reference. */
1098 if (gfc_intrinsic_name (sym->name, 1))
1100 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
1105 /* The reference is to an external name. */
1108 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1110 c->resolved_sym = sym;
1112 pure_subroutine (c, sym);
1118 /* Resolve a subroutine call. Although it was tempting to use the same code
1119 for functions, subroutines and functions are stored differently and this
1120 makes things awkward. */
1123 resolve_call (gfc_code * c)
1127 if (resolve_actual_arglist (c->ext.actual) == FAILURE)
1130 if (c->resolved_sym != NULL)
1133 switch (procedure_kind (c->symtree->n.sym))
1136 t = resolve_generic_s (c);
1139 case PTYPE_SPECIFIC:
1140 t = resolve_specific_s (c);
1144 t = resolve_unknown_s (c);
1148 gfc_internal_error ("resolve_subroutine(): bad function type");
1155 /* Resolve an operator expression node. This can involve replacing the
1156 operation with a user defined function call. */
1159 resolve_operator (gfc_expr * e)
1161 gfc_expr *op1, *op2;
1165 /* Resolve all subnodes-- give them types. */
1167 switch (e->operator)
1170 if (gfc_resolve_expr (e->op2) == FAILURE)
1173 /* Fall through... */
1176 case INTRINSIC_UPLUS:
1177 case INTRINSIC_UMINUS:
1178 if (gfc_resolve_expr (e->op1) == FAILURE)
1183 /* Typecheck the new node. */
1188 switch (e->operator)
1190 case INTRINSIC_UPLUS:
1191 case INTRINSIC_UMINUS:
1192 if (op1->ts.type == BT_INTEGER
1193 || op1->ts.type == BT_REAL
1194 || op1->ts.type == BT_COMPLEX)
1200 sprintf (msg, "Operand of unary numeric operator '%s' at %%L is %s",
1201 gfc_op2string (e->operator), gfc_typename (&e->ts));
1204 case INTRINSIC_PLUS:
1205 case INTRINSIC_MINUS:
1206 case INTRINSIC_TIMES:
1207 case INTRINSIC_DIVIDE:
1208 case INTRINSIC_POWER:
1209 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
1211 gfc_type_convert_binary (e);
1216 "Operands of binary numeric operator '%s' at %%L are %s/%s",
1217 gfc_op2string (e->operator), gfc_typename (&op1->ts),
1218 gfc_typename (&op2->ts));
1221 case INTRINSIC_CONCAT:
1222 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1224 e->ts.type = BT_CHARACTER;
1225 e->ts.kind = op1->ts.kind;
1230 "Operands of string concatenation operator at %%L are %s/%s",
1231 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
1237 case INTRINSIC_NEQV:
1238 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
1240 e->ts.type = BT_LOGICAL;
1241 e->ts.kind = gfc_kind_max (op1, op2);
1242 if (op1->ts.kind < e->ts.kind)
1243 gfc_convert_type (op1, &e->ts, 2);
1244 else if (op2->ts.kind < e->ts.kind)
1245 gfc_convert_type (op2, &e->ts, 2);
1249 sprintf (msg, "Operands of logical operator '%s' at %%L are %s/%s",
1250 gfc_op2string (e->operator), gfc_typename (&op1->ts),
1251 gfc_typename (&op2->ts));
1256 if (op1->ts.type == BT_LOGICAL)
1258 e->ts.type = BT_LOGICAL;
1259 e->ts.kind = op1->ts.kind;
1263 sprintf (msg, "Operand of .NOT. operator at %%L is %s",
1264 gfc_typename (&op1->ts));
1271 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1273 strcpy (msg, "COMPLEX quantities cannot be compared at %L");
1277 /* Fall through... */
1281 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1283 e->ts.type = BT_LOGICAL;
1284 e->ts.kind = gfc_default_logical_kind ();
1288 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
1290 gfc_type_convert_binary (e);
1292 e->ts.type = BT_LOGICAL;
1293 e->ts.kind = gfc_default_logical_kind ();
1297 sprintf (msg, "Operands of comparison operator '%s' at %%L are %s/%s",
1298 gfc_op2string (e->operator), gfc_typename (&op1->ts),
1299 gfc_typename (&op2->ts));
1303 case INTRINSIC_USER:
1305 sprintf (msg, "Operand of user operator '%s' at %%L is %s",
1306 e->uop->ns->proc_name->name, gfc_typename (&op1->ts));
1308 sprintf (msg, "Operands of user operator '%s' at %%L are %s/%s",
1309 e->uop->ns->proc_name->name, gfc_typename (&op1->ts),
1310 gfc_typename (&op2->ts));
1315 gfc_internal_error ("resolve_operator(): Bad intrinsic");
1318 /* Deal with arrayness of an operand through an operator. */
1322 switch (e->operator)
1324 case INTRINSIC_PLUS:
1325 case INTRINSIC_MINUS:
1326 case INTRINSIC_TIMES:
1327 case INTRINSIC_DIVIDE:
1328 case INTRINSIC_POWER:
1329 case INTRINSIC_CONCAT:
1333 case INTRINSIC_NEQV:
1341 if (op1->rank == 0 && op2->rank == 0)
1344 if (op1->rank == 0 && op2->rank != 0)
1346 e->rank = op2->rank;
1348 if (e->shape == NULL)
1349 e->shape = gfc_copy_shape (op2->shape, op2->rank);
1352 if (op1->rank != 0 && op2->rank == 0)
1354 e->rank = op1->rank;
1356 if (e->shape == NULL)
1357 e->shape = gfc_copy_shape (op1->shape, op1->rank);
1360 if (op1->rank != 0 && op2->rank != 0)
1362 if (op1->rank == op2->rank)
1364 e->rank = op1->rank;
1366 if (e->shape == NULL)
1367 e->shape = gfc_copy_shape (op1->shape, op1->rank);
1372 gfc_error ("Inconsistent ranks for operator at %L and %L",
1373 &op1->where, &op2->where);
1376 /* Allow higher level expressions to work. */
1384 case INTRINSIC_UPLUS:
1385 case INTRINSIC_UMINUS:
1386 e->rank = op1->rank;
1388 if (e->shape == NULL)
1389 e->shape = gfc_copy_shape (op1->shape, op1->rank);
1391 /* Simply copy arrayness attribute */
1398 /* Attempt to simplify the expression. */
1400 t = gfc_simplify_expr (e, 0);
1404 if (gfc_extend_expr (e) == SUCCESS)
1407 gfc_error (msg, &e->where);
1412 /************** Array resolution subroutines **************/
1416 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
1419 /* Compare two integer expressions. */
1422 compare_bound (gfc_expr * a, gfc_expr * b)
1426 if (a == NULL || a->expr_type != EXPR_CONSTANT
1427 || b == NULL || b->expr_type != EXPR_CONSTANT)
1430 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
1431 gfc_internal_error ("compare_bound(): Bad expression");
1433 i = mpz_cmp (a->value.integer, b->value.integer);
1443 /* Compare an integer expression with an integer. */
1446 compare_bound_int (gfc_expr * a, int b)
1450 if (a == NULL || a->expr_type != EXPR_CONSTANT)
1453 if (a->ts.type != BT_INTEGER)
1454 gfc_internal_error ("compare_bound_int(): Bad expression");
1456 i = mpz_cmp_si (a->value.integer, b);
1466 /* Compare a single dimension of an array reference to the array
1470 check_dimension (int i, gfc_array_ref * ar, gfc_array_spec * as)
1473 /* Given start, end and stride values, calculate the minimum and
1474 maximum referenced indexes. */
1482 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
1484 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
1490 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
1492 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
1496 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
1498 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
1501 /* TODO: Possibly, we could warn about end[i] being out-of-bound although
1502 it is legal (see 6.2.2.3.1). */
1507 gfc_internal_error ("check_dimension(): Bad array reference");
1513 gfc_warning ("Array reference at %L is out of bounds", &ar->c_where[i]);
1518 /* Compare an array reference with an array specification. */
1521 compare_spec_to_ref (gfc_array_ref * ar)
1528 /* TODO: Full array sections are only allowed as actual parameters. */
1529 if (as->type == AS_ASSUMED_SIZE
1530 && (/*ar->type == AR_FULL
1531 ||*/ (ar->type == AR_SECTION
1532 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
1534 gfc_error ("Rightmost upper bound of assumed size array section"
1535 " not specified at %L", &ar->where);
1539 if (ar->type == AR_FULL)
1542 if (as->rank != ar->dimen)
1544 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
1545 &ar->where, ar->dimen, as->rank);
1549 for (i = 0; i < as->rank; i++)
1550 if (check_dimension (i, ar, as) == FAILURE)
1557 /* Resolve one part of an array index. */
1560 gfc_resolve_index (gfc_expr * index, int check_scalar)
1567 if (gfc_resolve_expr (index) == FAILURE)
1570 if (index->ts.type != BT_INTEGER)
1572 gfc_error ("Array index at %L must be of INTEGER type", &index->where);
1576 if (check_scalar && index->rank != 0)
1578 gfc_error ("Array index at %L must be scalar", &index->where);
1582 if (index->ts.kind != gfc_index_integer_kind)
1584 ts.type = BT_INTEGER;
1585 ts.kind = gfc_index_integer_kind;
1587 gfc_convert_type_warn (index, &ts, 2, 0);
1594 /* Given an expression that contains array references, update those array
1595 references to point to the right array specifications. While this is
1596 filled in during matching, this information is difficult to save and load
1597 in a module, so we take care of it here.
1599 The idea here is that the original array reference comes from the
1600 base symbol. We traverse the list of reference structures, setting
1601 the stored reference to references. Component references can
1602 provide an additional array specification. */
1605 find_array_spec (gfc_expr * e)
1611 as = e->symtree->n.sym->as;
1612 c = e->symtree->n.sym->components;
1614 for (ref = e->ref; ref; ref = ref->next)
1619 gfc_internal_error ("find_array_spec(): Missing spec");
1626 for (; c; c = c->next)
1627 if (c == ref->u.c.component)
1631 gfc_internal_error ("find_array_spec(): Component not found");
1636 gfc_internal_error ("find_array_spec(): unused as(1)");
1640 c = c->ts.derived->components;
1648 gfc_internal_error ("find_array_spec(): unused as(2)");
1652 /* Resolve an array reference. */
1655 resolve_array_ref (gfc_array_ref * ar)
1657 int i, check_scalar;
1659 for (i = 0; i < ar->dimen; i++)
1661 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
1663 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
1665 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
1667 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
1670 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
1671 switch (ar->start[i]->rank)
1674 ar->dimen_type[i] = DIMEN_ELEMENT;
1678 ar->dimen_type[i] = DIMEN_VECTOR;
1682 gfc_error ("Array index at %L is an array of rank %d",
1683 &ar->c_where[i], ar->start[i]->rank);
1688 /* If the reference type is unknown, figure out what kind it is. */
1690 if (ar->type == AR_UNKNOWN)
1692 ar->type = AR_ELEMENT;
1693 for (i = 0; i < ar->dimen; i++)
1694 if (ar->dimen_type[i] == DIMEN_RANGE
1695 || ar->dimen_type[i] == DIMEN_VECTOR)
1697 ar->type = AR_SECTION;
1702 if (compare_spec_to_ref (ar) == FAILURE)
1710 resolve_substring (gfc_ref * ref)
1713 if (ref->u.ss.start != NULL)
1715 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
1718 if (ref->u.ss.start->ts.type != BT_INTEGER)
1720 gfc_error ("Substring start index at %L must be of type INTEGER",
1721 &ref->u.ss.start->where);
1725 if (ref->u.ss.start->rank != 0)
1727 gfc_error ("Substring start index at %L must be scalar",
1728 &ref->u.ss.start->where);
1732 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT)
1734 gfc_error ("Substring start index at %L is less than one",
1735 &ref->u.ss.start->where);
1740 if (ref->u.ss.end != NULL)
1742 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
1745 if (ref->u.ss.end->ts.type != BT_INTEGER)
1747 gfc_error ("Substring end index at %L must be of type INTEGER",
1748 &ref->u.ss.end->where);
1752 if (ref->u.ss.end->rank != 0)
1754 gfc_error ("Substring end index at %L must be scalar",
1755 &ref->u.ss.end->where);
1759 if (ref->u.ss.length != NULL
1760 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT)
1762 gfc_error ("Substring end index at %L is out of bounds",
1763 &ref->u.ss.start->where);
1772 /* Resolve subtype references. */
1775 resolve_ref (gfc_expr * expr)
1777 int current_part_dimension, n_components, seen_part_dimension;
1780 for (ref = expr->ref; ref; ref = ref->next)
1781 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
1783 find_array_spec (expr);
1787 for (ref = expr->ref; ref; ref = ref->next)
1791 if (resolve_array_ref (&ref->u.ar) == FAILURE)
1799 resolve_substring (ref);
1803 /* Check constraints on part references. */
1805 current_part_dimension = 0;
1806 seen_part_dimension = 0;
1809 for (ref = expr->ref; ref; ref = ref->next)
1814 switch (ref->u.ar.type)
1818 current_part_dimension = 1;
1822 current_part_dimension = 0;
1826 gfc_internal_error ("resolve_ref(): Bad array reference");
1832 if ((current_part_dimension || seen_part_dimension)
1833 && ref->u.c.component->pointer)
1836 ("Component to the right of a part reference with nonzero "
1837 "rank must not have the POINTER attribute at %L",
1849 if (((ref->type == REF_COMPONENT && n_components > 1)
1850 || ref->next == NULL)
1851 && current_part_dimension
1852 && seen_part_dimension)
1855 gfc_error ("Two or more part references with nonzero rank must "
1856 "not be specified at %L", &expr->where);
1860 if (ref->type == REF_COMPONENT)
1862 if (current_part_dimension)
1863 seen_part_dimension = 1;
1865 /* reset to make sure */
1866 current_part_dimension = 0;
1874 /* Given an expression, determine its shape. This is easier than it sounds.
1875 Leaves the shape array NULL if it is not possible to determine the shape. */
1878 expression_shape (gfc_expr * e)
1880 mpz_t array[GFC_MAX_DIMENSIONS];
1883 if (e->rank == 0 || e->shape != NULL)
1886 for (i = 0; i < e->rank; i++)
1887 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
1890 e->shape = gfc_get_shape (e->rank);
1892 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
1897 for (i--; i >= 0; i--)
1898 mpz_clear (array[i]);
1902 /* Given a variable expression node, compute the rank of the expression by
1903 examining the base symbol and any reference structures it may have. */
1906 expression_rank (gfc_expr * e)
1913 if (e->expr_type == EXPR_ARRAY)
1915 /* Constructors can have a rank different from one via RESHAPE(). */
1917 if (e->symtree == NULL)
1923 e->rank = (e->symtree->n.sym->as == NULL)
1924 ? 0 : e->symtree->n.sym->as->rank;
1930 for (ref = e->ref; ref; ref = ref->next)
1932 if (ref->type != REF_ARRAY)
1935 if (ref->u.ar.type == AR_FULL)
1937 rank = ref->u.ar.as->rank;
1941 if (ref->u.ar.type == AR_SECTION)
1943 /* Figure out the rank of the section. */
1945 gfc_internal_error ("expression_rank(): Two array specs");
1947 for (i = 0; i < ref->u.ar.dimen; i++)
1948 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
1949 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
1959 expression_shape (e);
1963 /* Resolve a variable expression. */
1966 resolve_variable (gfc_expr * e)
1970 if (e->ref && resolve_ref (e) == FAILURE)
1973 sym = e->symtree->n.sym;
1974 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
1976 e->ts.type = BT_PROCEDURE;
1980 if (sym->ts.type != BT_UNKNOWN)
1981 gfc_variable_attr (e, &e->ts);
1984 /* Must be a simple variable reference. */
1985 if (gfc_set_default_type (sym, 1, NULL) == FAILURE)
1994 /* Resolve an expression. That is, make sure that types of operands agree
1995 with their operators, intrinsic operators are converted to function calls
1996 for overloaded types and unresolved function references are resolved. */
1999 gfc_resolve_expr (gfc_expr * e)
2006 switch (e->expr_type)
2009 t = resolve_operator (e);
2013 t = resolve_function (e);
2017 t = resolve_variable (e);
2019 expression_rank (e);
2022 case EXPR_SUBSTRING:
2023 t = resolve_ref (e);
2033 if (resolve_ref (e) == FAILURE)
2036 t = gfc_resolve_array_constructor (e);
2037 /* Also try to expand a constructor. */
2040 expression_rank (e);
2041 gfc_expand_constructor (e);
2046 case EXPR_STRUCTURE:
2047 t = resolve_ref (e);
2051 t = resolve_structure_cons (e);
2055 t = gfc_simplify_expr (e, 0);
2059 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
2066 /* Resolve the expressions in an iterator structure and require that they all
2067 be of integer type. */
2070 gfc_resolve_iterator (gfc_iterator * iter)
2073 if (gfc_resolve_expr (iter->var) == FAILURE)
2076 if (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0)
2078 gfc_error ("Loop variable at %L must be a scalar INTEGER",
2083 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
2085 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
2090 if (gfc_resolve_expr (iter->start) == FAILURE)
2093 if (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0)
2095 gfc_error ("Start expression in DO loop at %L must be a scalar INTEGER",
2096 &iter->start->where);
2100 if (gfc_resolve_expr (iter->end) == FAILURE)
2103 if (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0)
2105 gfc_error ("End expression in DO loop at %L must be a scalar INTEGER",
2110 if (gfc_resolve_expr (iter->step) == FAILURE)
2113 if (iter->step->ts.type != BT_INTEGER || iter->step->rank != 0)
2115 gfc_error ("Step expression in DO loop at %L must be a scalar INTEGER",
2116 &iter->step->where);
2120 if (iter->step->expr_type == EXPR_CONSTANT
2121 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
2123 gfc_error ("Step expression in DO loop at %L cannot be zero",
2124 &iter->step->where);
2132 /* Resolve a list of FORALL iterators. */
2135 resolve_forall_iterators (gfc_forall_iterator * iter)
2140 if (gfc_resolve_expr (iter->var) == SUCCESS
2141 && iter->var->ts.type != BT_INTEGER)
2142 gfc_error ("FORALL Iteration variable at %L must be INTEGER",
2145 if (gfc_resolve_expr (iter->start) == SUCCESS
2146 && iter->start->ts.type != BT_INTEGER)
2147 gfc_error ("FORALL start expression at %L must be INTEGER",
2148 &iter->start->where);
2149 if (iter->var->ts.kind != iter->start->ts.kind)
2150 gfc_convert_type (iter->start, &iter->var->ts, 2);
2152 if (gfc_resolve_expr (iter->end) == SUCCESS
2153 && iter->end->ts.type != BT_INTEGER)
2154 gfc_error ("FORALL end expression at %L must be INTEGER",
2156 if (iter->var->ts.kind != iter->end->ts.kind)
2157 gfc_convert_type (iter->end, &iter->var->ts, 2);
2159 if (gfc_resolve_expr (iter->stride) == SUCCESS
2160 && iter->stride->ts.type != BT_INTEGER)
2161 gfc_error ("FORALL Stride expression at %L must be INTEGER",
2162 &iter->stride->where);
2163 if (iter->var->ts.kind != iter->stride->ts.kind)
2164 gfc_convert_type (iter->stride, &iter->var->ts, 2);
2171 /* Given a pointer to a symbol that is a derived type, see if any components
2172 have the POINTER attribute. The search is recursive if necessary.
2173 Returns zero if no pointer components are found, nonzero otherwise. */
2176 derived_pointer (gfc_symbol * sym)
2180 for (c = sym->components; c; c = c->next)
2185 if (c->ts.type == BT_DERIVED && derived_pointer (c->ts.derived))
2193 /* Resolve the argument of a deallocate expression. The expression must be
2194 a pointer or a full array. */
2197 resolve_deallocate_expr (gfc_expr * e)
2199 symbol_attribute attr;
2203 if (gfc_resolve_expr (e) == FAILURE)
2206 attr = gfc_expr_attr (e);
2210 if (e->expr_type != EXPR_VARIABLE)
2213 allocatable = e->symtree->n.sym->attr.allocatable;
2214 for (ref = e->ref; ref; ref = ref->next)
2218 if (ref->u.ar.type != AR_FULL)
2223 allocatable = (ref->u.c.component->as != NULL
2224 && ref->u.c.component->as->type == AS_DEFERRED);
2232 if (allocatable == 0)
2235 gfc_error ("Expression in DEALLOCATE statement at %L must be "
2236 "ALLOCATABLE or a POINTER", &e->where);
2243 /* Resolve the expression in an ALLOCATE statement, doing the additional
2244 checks to see whether the expression is OK or not. The expression must
2245 have a trailing array reference that gives the size of the array. */
2248 resolve_allocate_expr (gfc_expr * e)
2250 int i, pointer, allocatable, dimension;
2251 symbol_attribute attr;
2252 gfc_ref *ref, *ref2;
2255 if (gfc_resolve_expr (e) == FAILURE)
2258 /* Make sure the expression is allocatable or a pointer. If it is
2259 pointer, the next-to-last reference must be a pointer. */
2263 if (e->expr_type != EXPR_VARIABLE)
2267 attr = gfc_expr_attr (e);
2268 pointer = attr.pointer;
2269 dimension = attr.dimension;
2274 allocatable = e->symtree->n.sym->attr.allocatable;
2275 pointer = e->symtree->n.sym->attr.pointer;
2276 dimension = e->symtree->n.sym->attr.dimension;
2278 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
2282 if (ref->next != NULL)
2287 allocatable = (ref->u.c.component->as != NULL
2288 && ref->u.c.component->as->type == AS_DEFERRED);
2290 pointer = ref->u.c.component->pointer;
2291 dimension = ref->u.c.component->dimension;
2301 if (allocatable == 0 && pointer == 0)
2303 gfc_error ("Expression in ALLOCATE statement at %L must be "
2304 "ALLOCATABLE or a POINTER", &e->where);
2308 if (pointer && dimension == 0)
2311 /* Make sure the next-to-last reference node is an array specification. */
2313 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
2315 gfc_error ("Array specification required in ALLOCATE statement "
2316 "at %L", &e->where);
2320 if (ref2->u.ar.type == AR_ELEMENT)
2323 /* Make sure that the array section reference makes sense in the
2324 context of an ALLOCATE specification. */
2328 for (i = 0; i < ar->dimen; i++)
2329 switch (ar->dimen_type[i])
2335 if (ar->start[i] != NULL
2336 && ar->end[i] != NULL
2337 && ar->stride[i] == NULL)
2340 /* Fall Through... */
2344 gfc_error ("Bad array specification in ALLOCATE statement at %L",
2353 /************ SELECT CASE resolution subroutines ************/
2355 /* Callback function for our mergesort variant. Determines interval
2356 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
2357 op1 > op2. Assumes we're not dealing with the default case. */
2360 compare_cases (const void * _op1, const void * _op2)
2362 const gfc_case *op1, *op2;
2364 op1 = (const gfc_case *) _op1;
2365 op2 = (const gfc_case *) _op2;
2367 if (op1->low == NULL) /* op1 = (:N) */
2369 if (op2->low == NULL) /* op2 = (:M), so overlap. */
2372 else if (op2->high == NULL) /* op2 = (M:) */
2374 if (gfc_compare_expr (op1->high, op2->low) < 0)
2375 return -1; /* N < M */
2380 else /* op2 = (L:M) */
2382 if (gfc_compare_expr (op1->high, op2->low) < 0)
2383 return -1; /* N < L */
2389 else if (op1->high == NULL) /* op1 = (N:) */
2391 if (op2->low == NULL) /* op2 = (:M) */
2393 if (gfc_compare_expr (op1->low, op2->high) > 0)
2394 return 1; /* N > M */
2399 else if (op2->high == NULL) /* op2 = (M:), so overlap. */
2402 else /* op2 = (L:M) */
2404 if (gfc_compare_expr (op1->low, op2->high) > 0)
2405 return 1; /* N > M */
2411 else /* op1 = (N:P) */
2413 if (op2->low == NULL) /* op2 = (:M) */
2415 if (gfc_compare_expr (op1->low, op2->high) > 0)
2416 return 1; /* N > M */
2421 else if (op2->high == NULL) /* op2 = (M:) */
2423 if (gfc_compare_expr (op1->high, op2->low) < 0)
2424 return -1; /* P < M */
2429 else /* op2 = (L:M) */
2431 if (gfc_compare_expr (op1->high, op2->low) < 0)
2432 return -1; /* P < L */
2434 if (gfc_compare_expr (op1->low, op2->high) > 0)
2435 return 1; /* N > M */
2443 /* Merge-sort a double linked case list, detecting overlap in the
2444 process. LIST is the head of the double linked case list before it
2445 is sorted. Returns the head of the sorted list if we don't see any
2446 overlap, or NULL otherwise. */
2449 check_case_overlap (gfc_case * list)
2451 gfc_case *p, *q, *e, *tail;
2452 int insize, nmerges, psize, qsize, cmp, overlap_seen;
2454 /* If the passed list was empty, return immediately. */
2461 /* Loop unconditionally. The only exit from this loop is a return
2462 statement, when we've finished sorting the case list. */
2469 /* Count the number of merges we do in this pass. */
2472 /* Loop while there exists a merge to be done. */
2477 /* Count this merge. */
2480 /* Cut the list in two pieces by steppin INSIZE places
2481 forward in the list, starting from P. */
2484 for (i = 0; i < insize; i++)
2493 /* Now we have two lists. Merge them! */
2494 while (psize > 0 || (qsize > 0 && q != NULL))
2497 /* See from which the next case to merge comes from. */
2500 /* P is empty so the next case must come from Q. */
2505 else if (qsize == 0 || q == NULL)
2514 cmp = compare_cases (p, q);
2517 /* The whole case range for P is less than the
2525 /* The whole case range for Q is greater than
2526 the case range for P. */
2533 /* The cases overlap, or they are the same
2534 element in the list. Either way, we must
2535 issue an error and get the next case from P. */
2536 /* FIXME: Sort P and Q by line number. */
2537 gfc_error ("CASE label at %L overlaps with CASE "
2538 "label at %L", &p->where, &q->where);
2546 /* Add the next element to the merged list. */
2555 /* P has now stepped INSIZE places along, and so has Q. So
2556 they're the same. */
2561 /* If we have done only one merge or none at all, we've
2562 finished sorting the cases. */
2571 /* Otherwise repeat, merging lists twice the size. */
2577 /* Check to see if an expression is suitable for use in a CASE
2578 statement. Makes sure that all case expressions are scalar
2579 constants of the same type/kind. Return FAILURE if anything
2583 validate_case_label_expr (gfc_expr * e, gfc_expr * case_expr)
2585 gfc_typespec case_ts = case_expr->ts;
2587 if (e == NULL) return SUCCESS;
2589 if (e->expr_type != EXPR_CONSTANT)
2591 gfc_error ("Expression in CASE statement at %L must be a constant",
2596 if (e->ts.type != case_ts.type)
2598 gfc_error ("Expression in CASE statement at %L must be of type %s",
2599 &e->where, gfc_basic_typename (case_ts.type));
2603 if (e->ts.kind != case_ts.kind)
2605 gfc_error("Expression in CASE statement at %L must be kind %d",
2606 &e->where, case_ts.kind);
2612 gfc_error ("Expression in CASE statement at %L must be scalar",
2621 /* Given a completely parsed select statement, we:
2623 - Validate all expressions and code within the SELECT.
2624 - Make sure that the selection expression is not of the wrong type.
2625 - Make sure that no case ranges overlap.
2626 - Eliminate unreachable cases and unreachable code resulting from
2627 removing case labels.
2629 The standard does allow unreachable cases, e.g. CASE (5:3). But
2630 they are a hassle for code generation, and to prevent that, we just
2631 cut them out here. This is not necessary for overlapping cases
2632 because they are illegal and we never even try to generate code.
2634 We have the additional caveat that a SELECT construct could have
2635 been a computed GOTO in the source code. Furtunately we can fairly
2636 easily work around that here: The case_expr for a "real" SELECT CASE
2637 is in code->expr1, but for a computed GOTO it is in code->expr2. All
2638 we have to do is make sure that the case_expr is a scalar integer
2642 resolve_select (gfc_code * code)
2645 gfc_expr *case_expr;
2646 gfc_case *cp, *default_case, *tail, *head;
2647 int seen_unreachable;
2652 if (code->expr == NULL)
2654 /* This was actually a computed GOTO statement. */
2655 case_expr = code->expr2;
2656 if (case_expr->ts.type != BT_INTEGER
2657 || case_expr->rank != 0)
2658 gfc_error ("Selection expression in computed GOTO statement "
2659 "at %L must be a scalar integer expression",
2662 /* Further checking is not necessary because this SELECT was built
2663 by the compiler, so it should always be OK. Just move the
2664 case_expr from expr2 to expr so that we can handle computed
2665 GOTOs as normal SELECTs from here on. */
2666 code->expr = code->expr2;
2671 case_expr = code->expr;
2673 type = case_expr->ts.type;
2674 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
2676 gfc_error ("Argument of SELECT statement at %L cannot be %s",
2677 &case_expr->where, gfc_typename (&case_expr->ts));
2679 /* Punt. Going on here just produce more garbage error messages. */
2683 if (case_expr->rank != 0)
2685 gfc_error ("Argument of SELECT statement at %L must be a scalar "
2686 "expression", &case_expr->where);
2692 /* Assume there is no DEFAULT case. */
2693 default_case = NULL;
2697 for (body = code->block; body; body = body->block)
2699 /* Assume the CASE list is OK, and all CASE labels can be matched. */
2701 seen_unreachable = 0;
2703 /* Walk the case label list, making sure that all case labels
2705 for (cp = body->ext.case_list; cp; cp = cp->next)
2707 /* Count the number of cases in the whole construct. */
2710 /* Intercept the DEFAULT case. */
2711 if (cp->low == NULL && cp->high == NULL)
2713 if (default_case != NULL)
2715 gfc_error ("The DEFAULT CASE at %L cannot be followed "
2716 "by a second DEFAULT CASE at %L",
2717 &default_case->where, &cp->where);
2728 /* Deal with single value cases and case ranges. Errors are
2729 issued from the validation function. */
2730 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
2731 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
2737 if (type == BT_LOGICAL
2738 && ((cp->low == NULL || cp->high == NULL)
2739 || cp->low != cp->high))
2742 ("Logical range in CASE statement at %L is not allowed",
2748 if (cp->low != NULL && cp->high != NULL
2749 && cp->low != cp->high
2750 && gfc_compare_expr (cp->low, cp->high) > 0)
2752 if (gfc_option.warn_surprising)
2753 gfc_warning ("Range specification at %L can never "
2754 "be matched", &cp->where);
2756 cp->unreachable = 1;
2757 seen_unreachable = 1;
2761 /* If the case range can be matched, it can also overlap with
2762 other cases. To make sure it does not, we put it in a
2763 double linked list here. We sort that with a merge sort
2764 later on to detect any overlapping cases. */
2768 head->right = head->left = NULL;
2773 tail->right->left = tail;
2780 /* It there was a failure in the previous case label, give up
2781 for this case label list. Continue with the next block. */
2785 /* See if any case labels that are unreachable have been seen.
2786 If so, we eliminate them. This is a bit of a kludge because
2787 the case lists for a single case statement (label) is a
2788 single forward linked lists. */
2789 if (seen_unreachable)
2791 /* Advance until the first case in the list is reachable. */
2792 while (body->ext.case_list != NULL
2793 && body->ext.case_list->unreachable)
2795 gfc_case *n = body->ext.case_list;
2796 body->ext.case_list = body->ext.case_list->next;
2798 gfc_free_case_list (n);
2801 /* Strip all other unreachable cases. */
2802 if (body->ext.case_list)
2804 for (cp = body->ext.case_list; cp->next; cp = cp->next)
2806 if (cp->next->unreachable)
2808 gfc_case *n = cp->next;
2809 cp->next = cp->next->next;
2811 gfc_free_case_list (n);
2818 /* See if there were overlapping cases. If the check returns NULL,
2819 there was overlap. In that case we don't do anything. If head
2820 is non-NULL, we prepend the DEFAULT case. The sorted list can
2821 then used during code generation for SELECT CASE constructs with
2822 a case expression of a CHARACTER type. */
2825 head = check_case_overlap (head);
2827 /* Prepend the default_case if it is there. */
2828 if (head != NULL && default_case)
2830 default_case->left = NULL;
2831 default_case->right = head;
2832 head->left = default_case;
2836 /* Eliminate dead blocks that may be the result if we've seen
2837 unreachable case labels for a block. */
2838 for (body = code; body && body->block; body = body->block)
2840 if (body->block->ext.case_list == NULL)
2842 /* Cut the unreachable block from the code chain. */
2843 gfc_code *c = body->block;
2844 body->block = c->block;
2846 /* Kill the dead block, but not the blocks below it. */
2848 gfc_free_statements (c);
2852 /* More than two cases is legal but insane for logical selects.
2853 Issue a warning for it. */
2854 if (gfc_option.warn_surprising && type == BT_LOGICAL
2856 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
2861 /*********** Toplevel code resolution subroutines ***********/
2863 /* Given a branch to a label and a namespace, if the branch is conforming.
2864 The code node described where the branch is located. */
2867 resolve_branch (gfc_st_label * label, gfc_code * code)
2869 gfc_code *block, *found;
2877 /* Step one: is this a valid branching target? */
2879 if (lp->defined == ST_LABEL_UNKNOWN)
2881 gfc_error ("Label %d referenced at %L is never defined", lp->value,
2886 if (lp->defined != ST_LABEL_TARGET)
2888 gfc_error ("Statement at %L is not a valid branch target statement "
2889 "for the branch statement at %L", &lp->where, &code->loc);
2893 /* Step two: make sure this branch is not a branch to itself ;-) */
2895 if (code->here == label)
2897 gfc_warning ("Branch at %L causes an infinite loop", &code->loc);
2901 /* Step three: Try to find the label in the parse tree. To do this,
2902 we traverse the tree block-by-block: first the block that
2903 contains this GOTO, then the block that it is nested in, etc. We
2904 can ignore other blocks because branching into another block is
2909 for (stack = cs_base; stack; stack = stack->prev)
2911 for (block = stack->head; block; block = block->next)
2913 if (block->here == label)
2926 /* still nothing, so illegal. */
2927 gfc_error_now ("Label at %L is not in the same block as the "
2928 "GOTO statement at %L", &lp->where, &code->loc);
2932 /* Step four: Make sure that the branching target is legal if
2933 the statement is an END {SELECT,DO,IF}. */
2935 if (found->op == EXEC_NOP)
2937 for (stack = cs_base; stack; stack = stack->prev)
2938 if (stack->current->next == found)
2942 gfc_notify_std (GFC_STD_F95_DEL,
2943 "Obsolete: GOTO at %L jumps to END of construct at %L",
2944 &code->loc, &found->loc);
2949 /* Check whether EXPR1 has the same shape as EXPR2. */
2952 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
2954 mpz_t shape[GFC_MAX_DIMENSIONS];
2955 mpz_t shape2[GFC_MAX_DIMENSIONS];
2956 try result = FAILURE;
2959 /* Compare the rank. */
2960 if (expr1->rank != expr2->rank)
2963 /* Compare the size of each dimension. */
2964 for (i=0; i<expr1->rank; i++)
2966 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
2969 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
2972 if (mpz_cmp (shape[i], shape2[i]))
2976 /* When either of the two expression is an assumed size array, we
2977 ignore the comparison of dimension sizes. */
2982 for (i--; i>=0; i--)
2984 mpz_clear (shape[i]);
2985 mpz_clear (shape2[i]);
2991 /* Check whether a WHERE assignment target or a WHERE mask expression
2992 has the same shape as the outmost WHERE mask expression. */
2995 resolve_where (gfc_code *code, gfc_expr *mask)
3001 cblock = code->block;
3003 /* Store the first WHERE mask-expr of the WHERE statement or construct.
3004 In case of nested WHERE, only the outmost one is stored. */
3005 if (mask == NULL) /* outmost WHERE */
3007 else /* inner WHERE */
3014 /* Check if the mask-expr has a consistent shape with the
3015 outmost WHERE mask-expr. */
3016 if (resolve_where_shape (cblock->expr, e) == FAILURE)
3017 gfc_error ("WHERE mask at %L has inconsistent shape",
3018 &cblock->expr->where);
3021 /* the assignment statement of a WHERE statement, or the first
3022 statement in where-body-construct of a WHERE construct */
3023 cnext = cblock->next;
3028 /* WHERE assignment statement */
3031 /* Check shape consistent for WHERE assignment target. */
3032 if (e && resolve_where_shape (cnext->expr, e) == FAILURE)
3033 gfc_error ("WHERE assignment target at %L has "
3034 "inconsistent shape", &cnext->expr->where);
3037 /* WHERE or WHERE construct is part of a where-body-construct */
3039 resolve_where (cnext, e);
3043 gfc_error ("Unsupported statement inside WHERE at %L",
3046 /* the next statement within the same where-body-construct */
3047 cnext = cnext->next;
3049 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
3050 cblock = cblock->block;
3055 /* Check whether the FORALL index appears in the expression or not. */
3058 gfc_find_forall_index (gfc_expr *expr, gfc_symbol *symbol)
3062 gfc_actual_arglist *args;
3065 switch (expr->expr_type)
3068 assert (expr->symtree->n.sym);
3070 /* A scalar assignment */
3073 if (expr->symtree->n.sym == symbol)
3079 /* the expr is array ref, substring or struct component. */
3086 /* Check if the symbol appears in the array subscript. */
3088 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
3091 if (gfc_find_forall_index (ar.start[i], symbol) == SUCCESS)
3095 if (gfc_find_forall_index (ar.end[i], symbol) == SUCCESS)
3099 if (gfc_find_forall_index (ar.stride[i], symbol) == SUCCESS)
3105 if (expr->symtree->n.sym == symbol)
3108 /* Check if the symbol appears in the substring section. */
3109 if (gfc_find_forall_index (tmp->u.ss.start, symbol) == SUCCESS)
3111 if (gfc_find_forall_index (tmp->u.ss.end, symbol) == SUCCESS)
3119 gfc_error("expresion reference type error at %L", &expr->where);
3125 /* If the expression is a function call, then check if the symbol
3126 appears in the actual arglist of the function. */
3128 for (args = expr->value.function.actual; args; args = args->next)
3130 if (gfc_find_forall_index(args->expr,symbol) == SUCCESS)
3135 /* It seems not to happen. */
3136 case EXPR_SUBSTRING:
3140 assert(expr->ref->type == REF_SUBSTRING);
3141 if (gfc_find_forall_index (tmp->u.ss.start, symbol) == SUCCESS)
3143 if (gfc_find_forall_index (tmp->u.ss.end, symbol) == SUCCESS)
3148 /* It seems not to happen. */
3149 case EXPR_STRUCTURE:
3151 gfc_error ("Unsupported statement while finding forall index in "
3158 /* Find the FORALL index in the first operand. */
3161 if (gfc_find_forall_index (expr->op1, symbol) == SUCCESS)
3165 /* Find the FORALL index in the second operand. */
3168 if (gfc_find_forall_index (expr->op2, symbol) == SUCCESS)
3175 /* Resolve assignment in FORALL construct.
3176 NVAR is the number of FORALL index variables, and VAR_EXPR records the
3177 FORALL index variables. */
3180 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
3184 for (n = 0; n < nvar; n++)
3186 gfc_symbol *forall_index;
3188 forall_index = var_expr[n]->symtree->n.sym;
3190 /* Check whether the assignment target is one of the FORALL index
3192 if ((code->expr->expr_type == EXPR_VARIABLE)
3193 && (code->expr->symtree->n.sym == forall_index))
3194 gfc_error ("Assignment to a FORALL index variable at %L",
3195 &code->expr->where);
3198 /* If one of the FORALL index variables doesn't appear in the
3199 assignment target, then there will be a many-to-one
3201 if (gfc_find_forall_index (code->expr, forall_index) == FAILURE)
3202 gfc_error ("The FORALL with index '%s' cause more than one "
3203 "assignment to this object at %L",
3204 var_expr[n]->symtree->name, &code->expr->where);
3210 /* Resolve WHERE statement in FORALL construct. */
3213 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr){
3217 cblock = code->block;
3220 /* the assignment statement of a WHERE statement, or the first
3221 statement in where-body-construct of a WHERE construct */
3222 cnext = cblock->next;
3227 /* WHERE assignment statement */
3229 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
3232 /* WHERE or WHERE construct is part of a where-body-construct */
3234 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
3238 gfc_error ("Unsupported statement inside WHERE at %L",
3241 /* the next statement within the same where-body-construct */
3242 cnext = cnext->next;
3244 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
3245 cblock = cblock->block;
3250 /* Traverse the FORALL body to check whether the following errors exist:
3251 1. For assignment, check if a many-to-one assignment happens.
3252 2. For WHERE statement, check the WHERE body to see if there is any
3253 many-to-one assignment. */
3256 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
3260 c = code->block->next;
3266 case EXEC_POINTER_ASSIGN:
3267 gfc_resolve_assign_in_forall (c, nvar, var_expr);
3270 /* Because the resolve_blocks() will handle the nested FORALL,
3271 there is no need to handle it here. */
3275 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
3280 /* The next statement in the FORALL body. */
3286 /* Given a FORALL construct, first resolve the FORALL iterator, then call
3287 gfc_resolve_forall_body to resolve the FORALL body. */
3289 static void resolve_blocks (gfc_code *, gfc_namespace *);
3292 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
3294 static gfc_expr **var_expr;
3295 static int total_var = 0;
3296 static int nvar = 0;
3297 gfc_forall_iterator *fa;
3298 gfc_symbol *forall_index;
3302 /* Start to resolve a FORALL construct */
3303 if (forall_save == 0)
3305 /* Count the total number of FORALL index in the nested FORALL
3306 construct in order to allocate the VAR_EXPR with proper size. */
3308 while ((next != NULL) && (next->op == EXEC_FORALL))
3310 for (fa = next->ext.forall_iterator; fa; fa = fa->next)
3312 next = next->block->next;
3315 /* allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
3316 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
3319 /* The information about FORALL iterator, including FORALL index start, end
3320 and stride. The FORALL index can not appear in start, end or stride. */
3321 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
3323 /* Check if any outer FORALL index name is the same as the current
3325 for (i = 0; i < nvar; i++)
3327 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
3329 gfc_error ("An outer FORALL construct already has an index "
3330 "with this name %L", &fa->var->where);
3334 /* Record the current FORALL index. */
3335 var_expr[nvar] = gfc_copy_expr (fa->var);
3337 forall_index = fa->var->symtree->n.sym;
3339 /* Check if the FORALL index appears in start, end or stride. */
3340 if (gfc_find_forall_index (fa->start, forall_index) == SUCCESS)
3341 gfc_error ("A FORALL index must not appear in a limit or stride "
3342 "expression in the same FORALL at %L", &fa->start->where);
3343 if (gfc_find_forall_index (fa->end, forall_index) == SUCCESS)
3344 gfc_error ("A FORALL index must not appear in a limit or stride "
3345 "expression in the same FORALL at %L", &fa->end->where);
3346 if (gfc_find_forall_index (fa->stride, forall_index) == SUCCESS)
3347 gfc_error ("A FORALL index must not appear in a limit or stride "
3348 "expression in the same FORALL at %L", &fa->stride->where);
3352 /* Resolve the FORALL body. */
3353 gfc_resolve_forall_body (code, nvar, var_expr);
3355 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
3356 resolve_blocks (code->block, ns);
3358 /* Free VAR_EXPR after the whole FORALL construct resolved. */
3359 for (i = 0; i < total_var; i++)
3360 gfc_free_expr (var_expr[i]);
3362 /* Reset the counters. */
3368 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL ,GOTO and
3371 static void resolve_code (gfc_code *, gfc_namespace *);
3374 resolve_blocks (gfc_code * b, gfc_namespace * ns)
3378 for (; b; b = b->block)
3380 t = gfc_resolve_expr (b->expr);
3381 if (gfc_resolve_expr (b->expr2) == FAILURE)
3387 if (t == SUCCESS && b->expr != NULL
3388 && (b->expr->ts.type != BT_LOGICAL || b->expr->rank != 0))
3390 ("ELSE IF clause at %L requires a scalar LOGICAL expression",
3397 && (b->expr->ts.type != BT_LOGICAL
3398 || b->expr->rank == 0))
3400 ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
3405 resolve_branch (b->label, b);
3415 gfc_internal_error ("resolve_block(): Bad block type");
3418 resolve_code (b->next, ns);
3423 /* Given a block of code, recursively resolve everything pointed to by this
3427 resolve_code (gfc_code * code, gfc_namespace * ns)
3429 int forall_save = 0;
3434 frame.prev = cs_base;
3438 for (; code; code = code->next)
3440 frame.current = code;
3442 if (code->op == EXEC_FORALL)
3444 forall_save = forall_flag;
3446 gfc_resolve_forall (code, ns, forall_save);
3449 resolve_blocks (code->block, ns);
3451 if (code->op == EXEC_FORALL)
3452 forall_flag = forall_save;
3454 t = gfc_resolve_expr (code->expr);
3455 if (gfc_resolve_expr (code->expr2) == FAILURE)
3472 resolve_where (code, NULL);
3476 if (code->expr != NULL && code->expr->ts.type != BT_INTEGER)
3477 gfc_error ("ASSIGNED GOTO statement at %L requires an INTEGER "
3478 "variable", &code->expr->where);
3480 resolve_branch (code->label, code);
3484 if (code->expr != NULL && code->expr->ts.type != BT_INTEGER)
3485 gfc_error ("Alternate RETURN statement at %L requires an INTEGER "
3486 "return specifier", &code->expr->where);
3493 if (gfc_extend_assign (code, ns) == SUCCESS)
3496 if (gfc_pure (NULL))
3498 if (gfc_impure_variable (code->expr->symtree->n.sym))
3501 ("Cannot assign to variable '%s' in PURE procedure at %L",
3502 code->expr->symtree->n.sym->name, &code->expr->where);
3506 if (code->expr2->ts.type == BT_DERIVED
3507 && derived_pointer (code->expr2->ts.derived))
3510 ("Right side of assignment at %L is a derived type "
3511 "containing a POINTER in a PURE procedure",
3512 &code->expr2->where);
3517 gfc_check_assign (code->expr, code->expr2, 1);
3520 case EXEC_LABEL_ASSIGN:
3521 if (code->label->defined == ST_LABEL_UNKNOWN)
3522 gfc_error ("Label %d referenced at %L is never defined",
3523 code->label->value, &code->label->where);
3524 if (t == SUCCESS && code->expr->ts.type != BT_INTEGER)
3525 gfc_error ("ASSIGN statement at %L requires an INTEGER "
3526 "variable", &code->expr->where);
3529 case EXEC_POINTER_ASSIGN:
3533 gfc_check_pointer_assign (code->expr, code->expr2);
3536 case EXEC_ARITHMETIC_IF:
3538 && code->expr->ts.type != BT_INTEGER
3539 && code->expr->ts.type != BT_REAL)
3540 gfc_error ("Arithmetic IF statement at %L requires a numeric "
3541 "expression", &code->expr->where);
3543 resolve_branch (code->label, code);
3544 resolve_branch (code->label2, code);
3545 resolve_branch (code->label3, code);
3549 if (t == SUCCESS && code->expr != NULL
3550 && (code->expr->ts.type != BT_LOGICAL
3551 || code->expr->rank != 0))
3552 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
3553 &code->expr->where);
3558 resolve_call (code);
3562 /* Select is complicated. Also, a SELECT construct could be
3563 a transformed computed GOTO. */
3564 resolve_select (code);
3568 if (code->ext.iterator != NULL)
3569 gfc_resolve_iterator (code->ext.iterator);
3573 if (code->expr == NULL)
3574 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
3576 && (code->expr->rank != 0
3577 || code->expr->ts.type != BT_LOGICAL))
3578 gfc_error ("Exit condition of DO WHILE loop at %L must be "
3579 "a scalar LOGICAL expression", &code->expr->where);
3583 if (t == SUCCESS && code->expr != NULL
3584 && code->expr->ts.type != BT_INTEGER)
3585 gfc_error ("STAT tag in ALLOCATE statement at %L must be "
3586 "of type INTEGER", &code->expr->where);
3588 for (a = code->ext.alloc_list; a; a = a->next)
3589 resolve_allocate_expr (a->expr);
3593 case EXEC_DEALLOCATE:
3594 if (t == SUCCESS && code->expr != NULL
3595 && code->expr->ts.type != BT_INTEGER)
3597 ("STAT tag in DEALLOCATE statement at %L must be of type "
3598 "INTEGER", &code->expr->where);
3600 for (a = code->ext.alloc_list; a; a = a->next)
3601 resolve_deallocate_expr (a->expr);
3606 if (gfc_resolve_open (code->ext.open) == FAILURE)
3609 resolve_branch (code->ext.open->err, code);
3613 if (gfc_resolve_close (code->ext.close) == FAILURE)
3616 resolve_branch (code->ext.close->err, code);
3619 case EXEC_BACKSPACE:
3622 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
3625 resolve_branch (code->ext.filepos->err, code);
3629 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
3632 resolve_branch (code->ext.inquire->err, code);
3637 if (gfc_resolve_dt (code->ext.dt) == FAILURE)
3640 resolve_branch (code->ext.dt->err, code);
3641 resolve_branch (code->ext.dt->end, code);
3642 resolve_branch (code->ext.dt->eor, code);
3646 resolve_forall_iterators (code->ext.forall_iterator);
3648 if (code->expr != NULL && code->expr->ts.type != BT_LOGICAL)
3650 ("FORALL mask clause at %L requires a LOGICAL expression",
3651 &code->expr->where);
3655 gfc_internal_error ("resolve_code(): Bad statement code");
3659 cs_base = frame.prev;
3663 /* Resolve initial values and make sure they are compatible with
3667 resolve_values (gfc_symbol * sym)
3670 if (sym->value == NULL)
3673 if (gfc_resolve_expr (sym->value) == FAILURE)
3676 gfc_check_assign_symbol (sym, sym->value);
3680 /* Do anything necessary to resolve a symbol. Right now, we just
3681 assume that an otherwise unknown symbol is a variable. This sort
3682 of thing commonly happens for symbols in module. */
3685 resolve_symbol (gfc_symbol * sym)
3687 /* Zero if we are checking a formal namespace. */
3688 static int formal_ns_flag = 1;
3689 int formal_ns_save, check_constant, mp_flag;
3691 if (sym->attr.flavor == FL_UNKNOWN)
3693 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
3694 sym->attr.flavor = FL_VARIABLE;
3697 sym->attr.flavor = FL_PROCEDURE;
3698 if (sym->attr.dimension)
3699 sym->attr.function = 1;
3703 /* Symbols that are module procedures with results (functions) have
3704 the types and array specification copied for type checking in
3705 procedures that call them, as well as for saving to a module
3706 file. These symbols can't stand the scrutiny that their results
3708 mp_flag = (sym->result != NULL && sym->result != sym);
3710 /* Assign default type to symbols that need one and don't have one. */
3711 if (sym->ts.type == BT_UNKNOWN)
3713 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
3714 gfc_set_default_type (sym, 0, NULL);
3716 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
3719 gfc_set_default_type (sym, 0, NULL);
3722 /* Result may be in another namespace. */
3723 resolve_symbol (sym->result);
3725 sym->ts = sym->result->ts;
3726 sym->as = gfc_copy_array_spec (sym->result->as);
3732 && (sym->as->type == AS_ASSUMED_SIZE
3733 || sym->as->type == AS_ASSUMED_SHAPE)
3734 && sym->attr.dummy == 0)
3736 gfc_error("Assumed %s array at %L must be a dummy argument",
3737 sym->as->type == AS_ASSUMED_SIZE ? "size" : "shape",
3742 /* Make sure that character string variables with assumed length are
3745 if (sym->attr.flavor == FL_VARIABLE && !sym->attr.result
3746 && sym->ts.type == BT_CHARACTER
3747 && sym->ts.cl->length == NULL && sym->attr.dummy == 0)
3749 gfc_error ("Entity with assumed character length at %L must be a "
3750 "dummy argument or a PARAMETER", &sym->declared_at);
3754 /* Make sure a parameter that has been implicitly typed still
3755 matches the implicit type, since PARAMETER statements can precede
3756 IMPLICIT statements. */
3758 if (sym->attr.flavor == FL_PARAMETER
3759 && sym->attr.implicit_type
3760 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym, sym->ns)))
3761 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
3762 "later IMPLICIT type", sym->name, &sym->declared_at);
3764 /* Make sure the types of derived parameters are consistent. This
3765 type checking is deferred until resolution because the type may
3766 refer to a derived type from the host. */
3768 if (sym->attr.flavor == FL_PARAMETER
3769 && sym->ts.type == BT_DERIVED
3770 && !gfc_compare_types (&sym->ts, &sym->value->ts))
3771 gfc_error ("Incompatible derived type in PARAMETER at %L",
3772 &sym->value->where);
3774 /* Make sure symbols with known intent or optional are really dummy
3775 variable. Because of ENTRY statement, this has to be deferred
3776 until resolution time. */
3778 if (! sym->attr.dummy
3779 && (sym->attr.optional
3780 || sym->attr.intent != INTENT_UNKNOWN))
3782 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
3786 if (sym->attr.proc == PROC_ST_FUNCTION)
3788 if (sym->ts.type == BT_CHARACTER)
3790 gfc_charlen *cl = sym->ts.cl;
3791 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
3793 gfc_error ("Character-valued statement function '%s' at %L must "
3794 "have constant length", sym->name, &sym->declared_at);
3800 /* Constraints on deferred shape variable. */
3801 if (sym->attr.flavor == FL_VARIABLE
3802 || (sym->attr.flavor == FL_PROCEDURE
3803 && sym->attr.function))
3805 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
3807 if (sym->attr.allocatable)
3809 if (sym->attr.dimension)
3810 gfc_error ("Allocatable array at %L must have a deferred shape",
3813 gfc_error ("Object at %L may not be ALLOCATABLE",
3818 if (sym->attr.pointer && sym->attr.dimension)
3820 gfc_error ("Pointer to array at %L must have a deferred shape",
3828 if (!mp_flag && !sym->attr.allocatable
3829 && !sym->attr.pointer && !sym->attr.dummy)
3831 gfc_error ("Array at %L cannot have a deferred shape",
3838 /* Make sure that intrinsic exist */
3839 if (sym->attr.intrinsic
3840 && ! gfc_intrinsic_name(sym->name, 0)
3841 && ! gfc_intrinsic_name(sym->name, 1))
3842 gfc_error("Intrinsic at %L does not exist", &sym->declared_at);
3844 /* Resolve array specifier. Check as well some constraints
3845 on COMMON blocks. */
3847 check_constant = sym->attr.in_common && !sym->attr.pointer;
3848 gfc_resolve_array_spec (sym->as, check_constant);
3850 /* Resolve formal namespaces. */
3852 if (formal_ns_flag && sym != NULL && sym->formal_ns != NULL)
3854 formal_ns_save = formal_ns_flag;
3856 gfc_resolve (sym->formal_ns);
3857 formal_ns_flag = formal_ns_save;
3863 /************* Resolve DATA statements *************/
3867 gfc_data_value *vnode;
3873 /* Advance the values structure to point to the next value in the data list. */
3876 next_data_value (void)
3879 while (values.left == 0)
3881 if (values.vnode->next == NULL)
3884 values.vnode = values.vnode->next;
3885 values.left = values.vnode->repeat;
3894 check_data_variable (gfc_data_variable * var, locus * where)
3902 mpz_t section_index[GFC_MAX_DIMENSIONS];
3906 if (gfc_resolve_expr (var->expr) == FAILURE)
3910 mpz_init_set_si (offset, 0);
3913 if (e->expr_type != EXPR_VARIABLE)
3914 gfc_internal_error ("check_data_variable(): Bad expression");
3917 mpz_init_set_ui (size, 1);
3922 /* Find the array section reference. */
3923 for (ref = e->ref; ref; ref = ref->next)
3925 if (ref->type != REF_ARRAY)
3927 if (ref->u.ar.type == AR_ELEMENT)
3933 /* Set marks asscording to the reference pattern. */
3934 switch (ref->u.ar.type)
3942 /* Get the start position of array section. */
3943 gfc_get_section_index (ar, section_index, &offset);
3951 if (gfc_array_size (e, &size) == FAILURE)
3953 gfc_error ("Nonconstant array section at %L in DATA statement",
3962 while (mpz_cmp_ui (size, 0) > 0)
3964 if (next_data_value () == FAILURE)
3966 gfc_error ("DATA statement at %L has more variables than values",
3972 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
3976 /* Assign initial value to symbol. */
3977 gfc_assign_data_value (var->expr, values.vnode->expr, offset);
3980 mpz_add_ui (offset, offset, 1);
3982 /* Modify the array section indexes and recalculate the offset for
3985 gfc_advance_section (section_index, ar, &offset);
3987 mpz_sub_ui (size, size, 1);
3991 for (i = 0; i < ar->dimen; i++)
3992 mpz_clear (section_index[i]);
4002 static try traverse_data_var (gfc_data_variable *, locus *);
4004 /* Iterate over a list of elements in a DATA statement. */
4007 traverse_data_list (gfc_data_variable * var, locus * where)
4010 iterator_stack frame;
4013 mpz_init (frame.value);
4015 mpz_init_set (trip, var->iter.end->value.integer);
4016 mpz_sub (trip, trip, var->iter.start->value.integer);
4017 mpz_add (trip, trip, var->iter.step->value.integer);
4019 mpz_div (trip, trip, var->iter.step->value.integer);
4021 mpz_set (frame.value, var->iter.start->value.integer);
4023 frame.prev = iter_stack;
4024 frame.variable = var->iter.var->symtree;
4025 iter_stack = &frame;
4027 while (mpz_cmp_ui (trip, 0) > 0)
4029 if (traverse_data_var (var->list, where) == FAILURE)
4035 e = gfc_copy_expr (var->expr);
4036 if (gfc_simplify_expr (e, 1) == FAILURE)
4042 mpz_add (frame.value, frame.value, var->iter.step->value.integer);
4044 mpz_sub_ui (trip, trip, 1);
4048 mpz_clear (frame.value);
4050 iter_stack = frame.prev;
4055 /* Type resolve variables in the variable list of a DATA statement. */
4058 traverse_data_var (gfc_data_variable * var, locus * where)
4062 for (; var; var = var->next)
4064 if (var->expr == NULL)
4065 t = traverse_data_list (var, where);
4067 t = check_data_variable (var, where);
4077 /* Resolve the expressions and iterators associated with a data statement.
4078 This is separate from the assignment checking because data lists should
4079 only be resolved once. */
4082 resolve_data_variables (gfc_data_variable * d)
4085 for (; d; d = d->next)
4087 if (d->list == NULL)
4089 if (gfc_resolve_expr (d->expr) == FAILURE)
4094 if (gfc_resolve_iterator (&d->iter) == FAILURE)
4097 if (d->iter.start->expr_type != EXPR_CONSTANT
4098 || d->iter.end->expr_type != EXPR_CONSTANT
4099 || d->iter.step->expr_type != EXPR_CONSTANT)
4100 gfc_internal_error ("resolve_data_variables(): Bad iterator");
4102 if (resolve_data_variables (d->list) == FAILURE)
4111 /* Resolve a single DATA statement. We implement this by storing a pointer to
4112 the value list into static variables, and then recursively traversing the
4113 variables list, expanding iterators and such. */
4116 resolve_data (gfc_data * d)
4119 if (resolve_data_variables (d->var) == FAILURE)
4122 values.vnode = d->value;
4123 values.left = (d->value == NULL) ? 0 : d->value->repeat;
4125 if (traverse_data_var (d->var, &d->where) == FAILURE)
4128 /* At this point, we better not have any values left. */
4130 if (next_data_value () == SUCCESS)
4131 gfc_error ("DATA statement at %L has more values than variables",
4136 /* Determines if a variable is not 'pure', ie not assignable within a pure
4137 procedure. Returns zero if assignment is OK, nonzero if there is a problem.
4141 gfc_impure_variable (gfc_symbol * sym)
4144 if (sym->attr.use_assoc || sym->attr.in_common)
4147 if (sym->ns != gfc_current_ns)
4148 return !sym->attr.function;
4150 /* TODO: Check storage association through EQUIVALENCE statements */
4156 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
4157 symbol of the current procedure. */
4160 gfc_pure (gfc_symbol * sym)
4162 symbol_attribute attr;
4165 sym = gfc_current_ns->proc_name;
4171 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
4175 /* Test whether the current procedure is elemental or not. */
4178 gfc_elemental (gfc_symbol * sym)
4180 symbol_attribute attr;
4183 sym = gfc_current_ns->proc_name;
4188 return attr.flavor == FL_PROCEDURE && attr.elemental;
4192 /* Warn about unused labels. */
4195 warn_unused_label (gfc_namespace * ns)
4206 for (; l; l = l->prev)
4208 if (l->defined == ST_LABEL_UNKNOWN)
4211 switch (l->referenced)
4213 case ST_LABEL_UNKNOWN:
4214 gfc_warning ("Label %d at %L defined but not used", l->value,
4218 case ST_LABEL_BAD_TARGET:
4219 gfc_warning ("Label %d at %L defined but cannot be used", l->value,
4230 /* Resolve derived type EQUIVALENCE object. */
4233 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
4236 gfc_component *c = derived->components;
4241 /* Shall not be an object of nonsequence derived type. */
4242 if (!derived->attr.sequence)
4244 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
4245 "attribute to be an EQUIVALENCE object", sym->name, &e->where);
4249 for (; c ; c = c->next)
4252 if (d && (resolve_equivalence_derived (c->ts.derived, sym, e) == FAILURE))
4255 /* Shall not be an object of sequence derived type containing a pointer
4256 in the structure. */
4259 gfc_error ("Derived type variable '%s' at %L has pointer componet(s) "
4260 "cannot be an EQUIVALENCE object", sym->name, &e->where);
4268 /* Resolve equivalence object.
4269 An EQUIVALENCE object shall not be a dummy argument, a pointer, an
4270 allocatable array, an object of nonsequence derived type, an object of
4271 sequence derived type containing a pointer at any level of component
4272 selection, an automatic object, a function name, an entry name, a result
4273 name, a named constant, a structure component, or a subobject of any of
4274 the preceding objects. */
4277 resolve_equivalence (gfc_equiv *eq)
4280 gfc_symbol *derived;
4284 for (; eq; eq = eq->eq)
4287 if (gfc_resolve_expr (e) == FAILURE)
4290 sym = e->symtree->n.sym;
4292 /* Shall not be a dummy argument. */
4293 if (sym->attr.dummy)
4295 gfc_error ("Dummy argument '%s' at %L cannot be an EQUIVALENCE "
4296 "object", sym->name, &e->where);
4300 /* Shall not be an allocatable array. */
4301 if (sym->attr.allocatable)
4303 gfc_error ("Allocatable array '%s' at %L cannot be an EQUIVALENCE "
4304 "object", sym->name, &e->where);
4308 /* Shall not be a pointer. */
4309 if (sym->attr.pointer)
4311 gfc_error ("Pointer '%s' at %L cannot be an EQUIVALENCE object",
4312 sym->name, &e->where);
4316 /* Shall not be a function name, ... */
4317 if (sym->attr.function || sym->attr.result || sym->attr.entry
4318 || sym->attr.subroutine)
4320 gfc_error ("Entity '%s' at %L cannot be an EQUIVALENCE object",
4321 sym->name, &e->where);
4325 /* Shall not be a named constant. */
4326 if (e->expr_type == EXPR_CONSTANT)
4328 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
4329 "object", sym->name, &e->where);
4333 derived = e->ts.derived;
4334 if (derived && resolve_equivalence_derived (derived, sym, e) == FAILURE)
4340 /* Shall not be an automatic array. */
4341 if (e->ref->type == REF_ARRAY
4342 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
4344 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
4345 "an EQUIVALENCE object", sym->name, &e->where);
4349 /* Shall not be a structure component. */
4353 if (r->type == REF_COMPONENT)
4355 gfc_error ("Structure component '%s' at %L cannot be an "
4356 "EQUIVALENCE object",
4357 r->u.c.component->name, &e->where);
4366 /* This function is called after a complete program unit has been compiled.
4367 Its purpose is to examine all of the expressions associated with a program
4368 unit, assign types to all intermediate expressions, make sure that all
4369 assignments are to compatible types and figure out which names refer to
4370 which functions or subroutines. */
4373 gfc_resolve (gfc_namespace * ns)
4375 gfc_namespace *old_ns, *n;
4380 old_ns = gfc_current_ns;
4381 gfc_current_ns = ns;
4383 resolve_contained_functions (ns);
4385 gfc_traverse_ns (ns, resolve_symbol);
4387 for (n = ns->contained; n; n = n->sibling)
4389 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
4390 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
4391 "also be PURE", n->proc_name->name,
4392 &n->proc_name->declared_at);
4398 gfc_check_interfaces (ns);
4400 for (cl = ns->cl_list; cl; cl = cl->next)
4402 if (cl->length == NULL || gfc_resolve_expr (cl->length) == FAILURE)
4405 if (cl->length->ts.type != BT_INTEGER)
4407 ("Character length specification at %L must be of type INTEGER",
4408 &cl->length->where);
4411 gfc_traverse_ns (ns, resolve_values);
4417 for (d = ns->data; d; d = d->next)
4421 gfc_traverse_ns (ns, gfc_formalize_init_value);
4423 for (eq = ns->equiv; eq; eq = eq->next)
4424 resolve_equivalence (eq);
4427 resolve_code (ns->code, ns);
4429 /* Warn about unused labels. */
4430 if (gfc_option.warn_unused_labels)
4431 warn_unused_label (ns);
4433 gfc_current_ns = old_ns;