1 /* Perform type resolution on the various stuctures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation,
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor,Boston, MA
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
31 /* Types used in equivalence statements. */
35 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
39 /* Stack to push the current if we descend into a block during
40 resolution. See resolve_branch() and resolve_code(). */
42 typedef struct code_stack
44 struct gfc_code *head, *current;
45 struct code_stack *prev;
49 static code_stack *cs_base = NULL;
52 /* Nonzero if we're inside a FORALL block. */
54 static int forall_flag;
56 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
58 static int omp_workshare_flag;
60 /* Nonzero if we are processing a formal arglist. The corresponding function
61 resets the flag each time that it is read. */
62 static int formal_arg_flag = 0;
64 /* True if we are resolving a specification expression. */
65 static int specification_expr = 0;
67 /* The id of the last entry seen. */
68 static int current_entry_id;
71 gfc_is_formal_arg (void)
73 return formal_arg_flag;
76 /* Resolve types of formal argument lists. These have to be done early so that
77 the formal argument lists of module procedures can be copied to the
78 containing module before the individual procedures are resolved
79 individually. We also resolve argument lists of procedures in interface
80 blocks because they are self-contained scoping units.
82 Since a dummy argument cannot be a non-dummy procedure, the only
83 resort left for untyped names are the IMPLICIT types. */
86 resolve_formal_arglist (gfc_symbol * proc)
88 gfc_formal_arglist *f;
92 if (proc->result != NULL)
97 if (gfc_elemental (proc)
98 || sym->attr.pointer || sym->attr.allocatable
99 || (sym->as && sym->as->rank > 0))
100 proc->attr.always_explicit = 1;
104 for (f = proc->formal; f; f = f->next)
110 /* Alternate return placeholder. */
111 if (gfc_elemental (proc))
112 gfc_error ("Alternate return specifier in elemental subroutine "
113 "'%s' at %L is not allowed", proc->name,
115 if (proc->attr.function)
116 gfc_error ("Alternate return specifier in function "
117 "'%s' at %L is not allowed", proc->name,
122 if (sym->attr.if_source != IFSRC_UNKNOWN)
123 resolve_formal_arglist (sym);
125 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
127 if (gfc_pure (proc) && !gfc_pure (sym))
130 ("Dummy procedure '%s' of PURE procedure at %L must also "
131 "be PURE", sym->name, &sym->declared_at);
135 if (gfc_elemental (proc))
138 ("Dummy procedure at %L not allowed in ELEMENTAL procedure",
146 if (sym->ts.type == BT_UNKNOWN)
148 if (!sym->attr.function || sym->result == sym)
149 gfc_set_default_type (sym, 1, sym->ns);
152 gfc_resolve_array_spec (sym->as, 0);
154 /* We can't tell if an array with dimension (:) is assumed or deferred
155 shape until we know if it has the pointer or allocatable attributes.
157 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
158 && !(sym->attr.pointer || sym->attr.allocatable))
160 sym->as->type = AS_ASSUMED_SHAPE;
161 for (i = 0; i < sym->as->rank; i++)
162 sym->as->lower[i] = gfc_int_expr (1);
165 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
166 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
167 || sym->attr.optional)
168 proc->attr.always_explicit = 1;
170 /* If the flavor is unknown at this point, it has to be a variable.
171 A procedure specification would have already set the type. */
173 if (sym->attr.flavor == FL_UNKNOWN)
174 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
178 if (proc->attr.function && !sym->attr.pointer
179 && sym->attr.flavor != FL_PROCEDURE
180 && sym->attr.intent != INTENT_IN)
182 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
183 "INTENT(IN)", sym->name, proc->name,
186 if (proc->attr.subroutine && !sym->attr.pointer
187 && sym->attr.intent == INTENT_UNKNOWN)
190 ("Argument '%s' of pure subroutine '%s' at %L must have "
191 "its INTENT specified", sym->name, proc->name,
196 if (gfc_elemental (proc))
201 ("Argument '%s' of elemental procedure at %L must be scalar",
202 sym->name, &sym->declared_at);
206 if (sym->attr.pointer)
209 ("Argument '%s' of elemental procedure at %L cannot have "
210 "the POINTER attribute", sym->name, &sym->declared_at);
215 /* Each dummy shall be specified to be scalar. */
216 if (proc->attr.proc == PROC_ST_FUNCTION)
221 ("Argument '%s' of statement function at %L must be scalar",
222 sym->name, &sym->declared_at);
226 if (sym->ts.type == BT_CHARACTER)
228 gfc_charlen *cl = sym->ts.cl;
229 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
232 ("Character-valued argument '%s' of statement function at "
233 "%L must have constant length",
234 sym->name, &sym->declared_at);
244 /* Work function called when searching for symbols that have argument lists
245 associated with them. */
248 find_arglists (gfc_symbol * sym)
251 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
254 resolve_formal_arglist (sym);
258 /* Given a namespace, resolve all formal argument lists within the namespace.
262 resolve_formal_arglists (gfc_namespace * ns)
268 gfc_traverse_ns (ns, find_arglists);
273 resolve_contained_fntype (gfc_symbol * sym, gfc_namespace * ns)
277 /* If this namespace is not a function, ignore it. */
279 || !(sym->attr.function
280 || sym->attr.flavor == FL_VARIABLE))
283 /* Try to find out of what the return type is. */
284 if (sym->result != NULL)
287 if (sym->ts.type == BT_UNKNOWN)
289 t = gfc_set_default_type (sym, 0, ns);
291 if (t == FAILURE && !sym->attr.untyped)
293 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
294 sym->name, &sym->declared_at); /* FIXME */
295 sym->attr.untyped = 1;
299 /*Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character type,
300 lists the only ways a character length value of * can be used: dummy arguments
301 of procedures, named constants, and function results in external functions.
302 Internal function results are not on that list; ergo, not permitted. */
304 if (sym->ts.type == BT_CHARACTER)
306 gfc_charlen *cl = sym->ts.cl;
307 if (!cl || !cl->length)
308 gfc_error ("Character-valued internal function '%s' at %L must "
309 "not be assumed length", sym->name, &sym->declared_at);
314 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
315 introduce duplicates. */
318 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
320 gfc_formal_arglist *f, *new_arglist;
323 for (; new_args != NULL; new_args = new_args->next)
325 new_sym = new_args->sym;
326 /* See if this arg is already in the formal argument list. */
327 for (f = proc->formal; f; f = f->next)
329 if (new_sym == f->sym)
336 /* Add a new argument. Argument order is not important. */
337 new_arglist = gfc_get_formal_arglist ();
338 new_arglist->sym = new_sym;
339 new_arglist->next = proc->formal;
340 proc->formal = new_arglist;
345 /* Resolve alternate entry points. If a symbol has multiple entry points we
346 create a new master symbol for the main routine, and turn the existing
347 symbol into an entry point. */
350 resolve_entries (gfc_namespace * ns)
352 gfc_namespace *old_ns;
356 char name[GFC_MAX_SYMBOL_LEN + 1];
357 static int master_count = 0;
359 if (ns->proc_name == NULL)
362 /* No need to do anything if this procedure doesn't have alternate entry
367 /* We may already have resolved alternate entry points. */
368 if (ns->proc_name->attr.entry_master)
371 /* If this isn't a procedure something has gone horribly wrong. */
372 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
374 /* Remember the current namespace. */
375 old_ns = gfc_current_ns;
379 /* Add the main entry point to the list of entry points. */
380 el = gfc_get_entry_list ();
381 el->sym = ns->proc_name;
383 el->next = ns->entries;
385 ns->proc_name->attr.entry = 1;
387 /* If it is a module function, it needs to be in the right namespace
388 so that gfc_get_fake_result_decl can gather up the results. The
389 need for this arose in get_proc_name, where these beasts were
390 left in their own namespace, to keep prior references linked to
391 the entry declaration.*/
392 if (ns->proc_name->attr.function
394 && ns->parent->proc_name->attr.flavor == FL_MODULE)
397 /* Add an entry statement for it. */
404 /* Create a new symbol for the master function. */
405 /* Give the internal function a unique name (within this file).
406 Also include the function name so the user has some hope of figuring
407 out what is going on. */
408 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
409 master_count++, ns->proc_name->name);
410 gfc_get_ha_symbol (name, &proc);
411 gcc_assert (proc != NULL);
413 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
414 if (ns->proc_name->attr.subroutine)
415 gfc_add_subroutine (&proc->attr, proc->name, NULL);
419 gfc_typespec *ts, *fts;
420 gfc_array_spec *as, *fas;
421 gfc_add_function (&proc->attr, proc->name, NULL);
423 fas = ns->entries->sym->as;
424 fas = fas ? fas : ns->entries->sym->result->as;
425 fts = &ns->entries->sym->result->ts;
426 if (fts->type == BT_UNKNOWN)
427 fts = gfc_get_default_type (ns->entries->sym->result, NULL);
428 for (el = ns->entries->next; el; el = el->next)
430 ts = &el->sym->result->ts;
432 as = as ? as : el->sym->result->as;
433 if (ts->type == BT_UNKNOWN)
434 ts = gfc_get_default_type (el->sym->result, NULL);
436 if (! gfc_compare_types (ts, fts)
437 || (el->sym->result->attr.dimension
438 != ns->entries->sym->result->attr.dimension)
439 || (el->sym->result->attr.pointer
440 != ns->entries->sym->result->attr.pointer))
443 else if (as && fas && gfc_compare_array_spec (as, fas) == 0)
444 gfc_error ("Procedure %s at %L has entries with mismatched "
445 "array specifications", ns->entries->sym->name,
446 &ns->entries->sym->declared_at);
451 sym = ns->entries->sym->result;
452 /* All result types the same. */
454 if (sym->attr.dimension)
455 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
456 if (sym->attr.pointer)
457 gfc_add_pointer (&proc->attr, NULL);
461 /* Otherwise the result will be passed through a union by
463 proc->attr.mixed_entry_master = 1;
464 for (el = ns->entries; el; el = el->next)
466 sym = el->sym->result;
467 if (sym->attr.dimension)
469 if (el == ns->entries)
471 ("FUNCTION result %s can't be an array in FUNCTION %s at %L",
472 sym->name, ns->entries->sym->name, &sym->declared_at);
475 ("ENTRY result %s can't be an array in FUNCTION %s at %L",
476 sym->name, ns->entries->sym->name, &sym->declared_at);
478 else if (sym->attr.pointer)
480 if (el == ns->entries)
482 ("FUNCTION result %s can't be a POINTER in FUNCTION %s at %L",
483 sym->name, ns->entries->sym->name, &sym->declared_at);
486 ("ENTRY result %s can't be a POINTER in FUNCTION %s at %L",
487 sym->name, ns->entries->sym->name, &sym->declared_at);
492 if (ts->type == BT_UNKNOWN)
493 ts = gfc_get_default_type (sym, NULL);
497 if (ts->kind == gfc_default_integer_kind)
501 if (ts->kind == gfc_default_real_kind
502 || ts->kind == gfc_default_double_kind)
506 if (ts->kind == gfc_default_complex_kind)
510 if (ts->kind == gfc_default_logical_kind)
514 /* We will issue error elsewhere. */
522 if (el == ns->entries)
524 ("FUNCTION result %s can't be of type %s in FUNCTION %s at %L",
525 sym->name, gfc_typename (ts), ns->entries->sym->name,
529 ("ENTRY result %s can't be of type %s in FUNCTION %s at %L",
530 sym->name, gfc_typename (ts), ns->entries->sym->name,
537 proc->attr.access = ACCESS_PRIVATE;
538 proc->attr.entry_master = 1;
540 /* Merge all the entry point arguments. */
541 for (el = ns->entries; el; el = el->next)
542 merge_argument_lists (proc, el->sym->formal);
544 /* Use the master function for the function body. */
545 ns->proc_name = proc;
547 /* Finalize the new symbols. */
548 gfc_commit_symbols ();
550 /* Restore the original namespace. */
551 gfc_current_ns = old_ns;
555 /* Resolve contained function types. Because contained functions can call one
556 another, they have to be worked out before any of the contained procedures
559 The good news is that if a function doesn't already have a type, the only
560 way it can get one is through an IMPLICIT type or a RESULT variable, because
561 by definition contained functions are contained namespace they're contained
562 in, not in a sibling or parent namespace. */
565 resolve_contained_functions (gfc_namespace * ns)
567 gfc_namespace *child;
570 resolve_formal_arglists (ns);
572 for (child = ns->contained; child; child = child->sibling)
574 /* Resolve alternate entry points first. */
575 resolve_entries (child);
577 /* Then check function return types. */
578 resolve_contained_fntype (child->proc_name, child);
579 for (el = child->entries; el; el = el->next)
580 resolve_contained_fntype (el->sym, child);
585 /* Resolve all of the elements of a structure constructor and make sure that
586 the types are correct. */
589 resolve_structure_cons (gfc_expr * expr)
591 gfc_constructor *cons;
597 cons = expr->value.constructor;
598 /* A constructor may have references if it is the result of substituting a
599 parameter variable. In this case we just pull out the component we
602 comp = expr->ref->u.c.sym->components;
604 comp = expr->ts.derived->components;
606 for (; comp; comp = comp->next, cons = cons->next)
611 if (gfc_resolve_expr (cons->expr) == FAILURE)
617 if (cons->expr->expr_type != EXPR_NULL
618 && comp->as && comp->as->rank != cons->expr->rank
619 && (comp->allocatable || cons->expr->rank))
621 gfc_error ("The rank of the element in the derived type "
622 "constructor at %L does not match that of the "
623 "component (%d/%d)", &cons->expr->where,
624 cons->expr->rank, comp->as ? comp->as->rank : 0);
628 /* If we don't have the right type, try to convert it. */
630 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
633 if (comp->pointer && cons->expr->ts.type != BT_UNKNOWN)
634 gfc_error ("The element in the derived type constructor at %L, "
635 "for pointer component '%s', is %s but should be %s",
636 &cons->expr->where, comp->name,
637 gfc_basic_typename (cons->expr->ts.type),
638 gfc_basic_typename (comp->ts.type));
640 t = gfc_convert_type (cons->expr, &comp->ts, 1);
643 if (!comp->pointer || cons->expr->expr_type == EXPR_NULL)
646 a = gfc_expr_attr (cons->expr);
648 if (!a.pointer && !a.target)
651 gfc_error ("The element in the derived type constructor at %L, "
652 "for pointer component '%s' should be a POINTER or "
653 "a TARGET", &cons->expr->where, comp->name);
662 /****************** Expression name resolution ******************/
664 /* Returns 0 if a symbol was not declared with a type or
665 attribute declaration statement, nonzero otherwise. */
668 was_declared (gfc_symbol * sym)
674 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
677 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
678 || a.optional || a.pointer || a.save || a.target || a.volatile_ || a.value
679 || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
686 /* Determine if a symbol is generic or not. */
689 generic_sym (gfc_symbol * sym)
693 if (sym->attr.generic ||
694 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
697 if (was_declared (sym) || sym->ns->parent == NULL)
700 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
702 return (s == NULL) ? 0 : generic_sym (s);
706 /* Determine if a symbol is specific or not. */
709 specific_sym (gfc_symbol * sym)
713 if (sym->attr.if_source == IFSRC_IFBODY
714 || sym->attr.proc == PROC_MODULE
715 || sym->attr.proc == PROC_INTERNAL
716 || sym->attr.proc == PROC_ST_FUNCTION
717 || (sym->attr.intrinsic &&
718 gfc_specific_intrinsic (sym->name))
719 || sym->attr.external)
722 if (was_declared (sym) || sym->ns->parent == NULL)
725 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
727 return (s == NULL) ? 0 : specific_sym (s);
731 /* Figure out if the procedure is specific, generic or unknown. */
734 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
738 procedure_kind (gfc_symbol * sym)
741 if (generic_sym (sym))
742 return PTYPE_GENERIC;
744 if (specific_sym (sym))
745 return PTYPE_SPECIFIC;
747 return PTYPE_UNKNOWN;
750 /* Check references to assumed size arrays. The flag need_full_assumed_size
751 is nonzero when matching actual arguments. */
753 static int need_full_assumed_size = 0;
756 check_assumed_size_reference (gfc_symbol * sym, gfc_expr * e)
762 if (need_full_assumed_size
763 || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
766 for (ref = e->ref; ref; ref = ref->next)
767 if (ref->type == REF_ARRAY)
768 for (dim = 0; dim < ref->u.ar.as->rank; dim++)
769 last = (ref->u.ar.end[dim] == NULL) && (ref->u.ar.type == DIMEN_ELEMENT);
773 gfc_error ("The upper bound in the last dimension must "
774 "appear in the reference to the assumed size "
775 "array '%s' at %L", sym->name, &e->where);
782 /* Look for bad assumed size array references in argument expressions
783 of elemental and array valued intrinsic procedures. Since this is
784 called from procedure resolution functions, it only recurses at
788 resolve_assumed_size_actual (gfc_expr *e)
793 switch (e->expr_type)
797 && check_assumed_size_reference (e->symtree->n.sym, e))
802 if (resolve_assumed_size_actual (e->value.op.op1)
803 || resolve_assumed_size_actual (e->value.op.op2))
814 /* Resolve an actual argument list. Most of the time, this is just
815 resolving the expressions in the list.
816 The exception is that we sometimes have to decide whether arguments
817 that look like procedure arguments are really simple variable
821 resolve_actual_arglist (gfc_actual_arglist * arg)
824 gfc_symtree *parent_st;
827 for (; arg; arg = arg->next)
833 /* Check the label is a valid branching target. */
836 if (arg->label->defined == ST_LABEL_UNKNOWN)
838 gfc_error ("Label %d referenced at %L is never defined",
839 arg->label->value, &arg->label->where);
846 if (e->ts.type != BT_PROCEDURE)
848 if (gfc_resolve_expr (e) != SUCCESS)
853 /* See if the expression node should really be a variable
856 sym = e->symtree->n.sym;
858 if (sym->attr.flavor == FL_PROCEDURE
859 || sym->attr.intrinsic
860 || sym->attr.external)
864 /* If a procedure is not already determined to be something else
865 check if it is intrinsic. */
866 if (!sym->attr.intrinsic
867 && !(sym->attr.external || sym->attr.use_assoc
868 || sym->attr.if_source == IFSRC_IFBODY)
869 && gfc_intrinsic_name (sym->name, sym->attr.subroutine))
870 sym->attr.intrinsic = 1;
872 if (sym->attr.proc == PROC_ST_FUNCTION)
874 gfc_error ("Statement function '%s' at %L is not allowed as an "
875 "actual argument", sym->name, &e->where);
878 actual_ok = gfc_intrinsic_actual_ok (sym->name, sym->attr.subroutine);
879 if (sym->attr.intrinsic && actual_ok == 0)
881 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
882 "actual argument", sym->name, &e->where);
885 if (sym->attr.contained && !sym->attr.use_assoc
886 && sym->ns->proc_name->attr.flavor != FL_MODULE)
888 gfc_error ("Internal procedure '%s' is not allowed as an "
889 "actual argument at %L", sym->name, &e->where);
892 if (sym->attr.elemental && !sym->attr.intrinsic)
894 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
895 "allowed as an actual argument at %L", sym->name,
899 if (sym->attr.generic)
901 gfc_error ("GENERIC non-INTRINSIC procedure '%s' is not "
902 "allowed as an actual argument at %L", sym->name,
906 /* If the symbol is the function that names the current (or
907 parent) scope, then we really have a variable reference. */
909 if (sym->attr.function && sym->result == sym
910 && (sym->ns->proc_name == sym
911 || (sym->ns->parent != NULL
912 && sym->ns->parent->proc_name == sym)))
918 /* See if the name is a module procedure in a parent unit. */
920 if (was_declared (sym) || sym->ns->parent == NULL)
923 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
925 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
929 if (parent_st == NULL)
932 sym = parent_st->n.sym;
933 e->symtree = parent_st; /* Point to the right thing. */
935 if (sym->attr.flavor == FL_PROCEDURE
936 || sym->attr.intrinsic
937 || sym->attr.external)
943 e->expr_type = EXPR_VARIABLE;
947 e->rank = sym->as->rank;
948 e->ref = gfc_get_ref ();
949 e->ref->type = REF_ARRAY;
950 e->ref->u.ar.type = AR_FULL;
951 e->ref->u.ar.as = sym->as;
959 /* Do the checks of the actual argument list that are specific to elemental
960 procedures. If called with c == NULL, we have a function, otherwise if
961 expr == NULL, we have a subroutine. */
963 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
965 gfc_actual_arglist *arg0;
966 gfc_actual_arglist *arg;
967 gfc_symbol *esym = NULL;
968 gfc_intrinsic_sym *isym = NULL;
970 gfc_intrinsic_arg *iformal = NULL;
971 gfc_formal_arglist *eformal = NULL;
972 bool formal_optional = false;
973 bool set_by_optional = false;
977 /* Is this an elemental procedure? */
978 if (expr && expr->value.function.actual != NULL)
980 if (expr->value.function.esym != NULL
981 && expr->value.function.esym->attr.elemental)
983 arg0 = expr->value.function.actual;
984 esym = expr->value.function.esym;
986 else if (expr->value.function.isym != NULL
987 && expr->value.function.isym->elemental)
989 arg0 = expr->value.function.actual;
990 isym = expr->value.function.isym;
995 else if (c && c->ext.actual != NULL
996 && c->symtree->n.sym->attr.elemental)
998 arg0 = c->ext.actual;
999 esym = c->symtree->n.sym;
1004 /* The rank of an elemental is the rank of its array argument(s). */
1005 for (arg = arg0; arg; arg = arg->next)
1007 if (arg->expr != NULL && arg->expr->rank > 0)
1009 rank = arg->expr->rank;
1010 if (arg->expr->expr_type == EXPR_VARIABLE
1011 && arg->expr->symtree->n.sym->attr.optional)
1012 set_by_optional = true;
1014 /* Function specific; set the result rank and shape. */
1018 if (!expr->shape && arg->expr->shape)
1020 expr->shape = gfc_get_shape (rank);
1021 for (i = 0; i < rank; i++)
1022 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1029 /* If it is an array, it shall not be supplied as an actual argument
1030 to an elemental procedure unless an array of the same rank is supplied
1031 as an actual argument corresponding to a nonoptional dummy argument of
1032 that elemental procedure(12.4.1.5). */
1033 formal_optional = false;
1035 iformal = isym->formal;
1037 eformal = esym->formal;
1039 for (arg = arg0; arg; arg = arg->next)
1043 if (eformal->sym && eformal->sym->attr.optional)
1044 formal_optional = true;
1045 eformal = eformal->next;
1047 else if (isym && iformal)
1049 if (iformal->optional)
1050 formal_optional = true;
1051 iformal = iformal->next;
1054 formal_optional = true;
1056 if (pedantic && arg->expr != NULL
1057 && arg->expr->expr_type == EXPR_VARIABLE
1058 && arg->expr->symtree->n.sym->attr.optional
1061 && (set_by_optional || arg->expr->rank != rank)
1062 && !(isym && isym->generic_id == GFC_ISYM_CONVERSION))
1064 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1065 "MISSING, it cannot be the actual argument of an "
1066 "ELEMENTAL procedure unless there is a non-optional"
1067 "argument with the same rank (12.4.1.5)",
1068 arg->expr->symtree->n.sym->name, &arg->expr->where);
1073 for (arg = arg0; arg; arg = arg->next)
1075 if (arg->expr == NULL || arg->expr->rank == 0)
1078 /* Being elemental, the last upper bound of an assumed size array
1079 argument must be present. */
1080 if (resolve_assumed_size_actual (arg->expr))
1086 /* Elemental subroutine array actual arguments must conform. */
1089 if (gfc_check_conformance ("elemental subroutine", arg->expr, e)
1101 /* Go through each actual argument in ACTUAL and see if it can be
1102 implemented as an inlined, non-copying intrinsic. FNSYM is the
1103 function being called, or NULL if not known. */
1106 find_noncopying_intrinsics (gfc_symbol * fnsym, gfc_actual_arglist * actual)
1108 gfc_actual_arglist *ap;
1111 for (ap = actual; ap; ap = ap->next)
1113 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1114 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual))
1115 ap->expr->inline_noncopying_intrinsic = 1;
1118 /* This function does the checking of references to global procedures
1119 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1120 77 and 95 standards. It checks for a gsymbol for the name, making
1121 one if it does not already exist. If it already exists, then the
1122 reference being resolved must correspond to the type of gsymbol.
1123 Otherwise, the new symbol is equipped with the attributes of the
1124 reference. The corresponding code that is called in creating
1125 global entities is parse.c. */
1128 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
1133 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1135 gsym = gfc_get_gsymbol (sym->name);
1137 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1138 global_used (gsym, where);
1140 if (gsym->type == GSYM_UNKNOWN)
1143 gsym->where = *where;
1149 /************* Function resolution *************/
1151 /* Resolve a function call known to be generic.
1152 Section 14.1.2.4.1. */
1155 resolve_generic_f0 (gfc_expr * expr, gfc_symbol * sym)
1159 if (sym->attr.generic)
1162 gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1165 expr->value.function.name = s->name;
1166 expr->value.function.esym = s;
1168 if (s->ts.type != BT_UNKNOWN)
1170 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1171 expr->ts = s->result->ts;
1174 expr->rank = s->as->rank;
1175 else if (s->result != NULL && s->result->as != NULL)
1176 expr->rank = s->result->as->rank;
1181 /* TODO: Need to search for elemental references in generic interface */
1184 if (sym->attr.intrinsic)
1185 return gfc_intrinsic_func_interface (expr, 0);
1192 resolve_generic_f (gfc_expr * expr)
1197 sym = expr->symtree->n.sym;
1201 m = resolve_generic_f0 (expr, sym);
1204 else if (m == MATCH_ERROR)
1208 if (sym->ns->parent == NULL)
1210 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1214 if (!generic_sym (sym))
1218 /* Last ditch attempt. */
1220 if (!gfc_generic_intrinsic (expr->symtree->n.sym->name))
1222 gfc_error ("There is no specific function for the generic '%s' at %L",
1223 expr->symtree->n.sym->name, &expr->where);
1227 m = gfc_intrinsic_func_interface (expr, 0);
1232 ("Generic function '%s' at %L is not consistent with a specific "
1233 "intrinsic interface", expr->symtree->n.sym->name, &expr->where);
1239 /* Resolve a function call known to be specific. */
1242 resolve_specific_f0 (gfc_symbol * sym, gfc_expr * expr)
1246 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1248 if (sym->attr.dummy)
1250 sym->attr.proc = PROC_DUMMY;
1254 sym->attr.proc = PROC_EXTERNAL;
1258 if (sym->attr.proc == PROC_MODULE
1259 || sym->attr.proc == PROC_ST_FUNCTION
1260 || sym->attr.proc == PROC_INTERNAL)
1263 if (sym->attr.intrinsic)
1265 m = gfc_intrinsic_func_interface (expr, 1);
1270 ("Function '%s' at %L is INTRINSIC but is not compatible with "
1271 "an intrinsic", sym->name, &expr->where);
1279 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1282 expr->value.function.name = sym->name;
1283 expr->value.function.esym = sym;
1284 if (sym->as != NULL)
1285 expr->rank = sym->as->rank;
1292 resolve_specific_f (gfc_expr * expr)
1297 sym = expr->symtree->n.sym;
1301 m = resolve_specific_f0 (sym, expr);
1304 if (m == MATCH_ERROR)
1307 if (sym->ns->parent == NULL)
1310 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1316 gfc_error ("Unable to resolve the specific function '%s' at %L",
1317 expr->symtree->n.sym->name, &expr->where);
1323 /* Resolve a procedure call not known to be generic nor specific. */
1326 resolve_unknown_f (gfc_expr * expr)
1331 sym = expr->symtree->n.sym;
1333 if (sym->attr.dummy)
1335 sym->attr.proc = PROC_DUMMY;
1336 expr->value.function.name = sym->name;
1340 /* See if we have an intrinsic function reference. */
1342 if (gfc_intrinsic_name (sym->name, 0))
1344 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
1349 /* The reference is to an external name. */
1351 sym->attr.proc = PROC_EXTERNAL;
1352 expr->value.function.name = sym->name;
1353 expr->value.function.esym = expr->symtree->n.sym;
1355 if (sym->as != NULL)
1356 expr->rank = sym->as->rank;
1358 /* Type of the expression is either the type of the symbol or the
1359 default type of the symbol. */
1362 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1364 if (sym->ts.type != BT_UNKNOWN)
1368 ts = gfc_get_default_type (sym, sym->ns);
1370 if (ts->type == BT_UNKNOWN)
1372 gfc_error ("Function '%s' at %L has no IMPLICIT type",
1373 sym->name, &expr->where);
1384 /* Figure out if a function reference is pure or not. Also set the name
1385 of the function for a potential error message. Return nonzero if the
1386 function is PURE, zero if not. */
1389 pure_function (gfc_expr * e, const char **name)
1393 if (e->value.function.esym)
1395 pure = gfc_pure (e->value.function.esym);
1396 *name = e->value.function.esym->name;
1398 else if (e->value.function.isym)
1400 pure = e->value.function.isym->pure
1401 || e->value.function.isym->elemental;
1402 *name = e->value.function.isym->name;
1406 /* Implicit functions are not pure. */
1408 *name = e->value.function.name;
1415 /* Resolve a function call, which means resolving the arguments, then figuring
1416 out which entity the name refers to. */
1417 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
1418 to INTENT(OUT) or INTENT(INOUT). */
1421 resolve_function (gfc_expr * expr)
1423 gfc_actual_arglist *arg;
1431 sym = expr->symtree->n.sym;
1433 /* If the procedure is not internal, a statement function or a module
1434 procedure,it must be external and should be checked for usage. */
1435 if (sym && !sym->attr.dummy && !sym->attr.contained
1436 && sym->attr.proc != PROC_ST_FUNCTION
1437 && !sym->attr.use_assoc)
1438 resolve_global_procedure (sym, &expr->where, 0);
1440 /* Switch off assumed size checking and do this again for certain kinds
1441 of procedure, once the procedure itself is resolved. */
1442 need_full_assumed_size++;
1444 if (resolve_actual_arglist (expr->value.function.actual) == FAILURE)
1447 /* Resume assumed_size checking. */
1448 need_full_assumed_size--;
1450 if (sym && sym->ts.type == BT_CHARACTER
1452 && sym->ts.cl->length == NULL
1454 && expr->value.function.esym == NULL
1455 && !sym->attr.contained)
1457 /* Internal procedures are taken care of in resolve_contained_fntype. */
1458 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
1459 "be used at %L since it is not a dummy argument",
1460 sym->name, &expr->where);
1464 /* See if function is already resolved. */
1466 if (expr->value.function.name != NULL)
1468 if (expr->ts.type == BT_UNKNOWN)
1474 /* Apply the rules of section 14.1.2. */
1476 switch (procedure_kind (sym))
1479 t = resolve_generic_f (expr);
1482 case PTYPE_SPECIFIC:
1483 t = resolve_specific_f (expr);
1487 t = resolve_unknown_f (expr);
1491 gfc_internal_error ("resolve_function(): bad function type");
1495 /* If the expression is still a function (it might have simplified),
1496 then we check to see if we are calling an elemental function. */
1498 if (expr->expr_type != EXPR_FUNCTION)
1501 temp = need_full_assumed_size;
1502 need_full_assumed_size = 0;
1504 if (resolve_elemental_actual (expr, NULL) == FAILURE)
1507 if (omp_workshare_flag
1508 && expr->value.function.esym
1509 && ! gfc_elemental (expr->value.function.esym))
1511 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed"
1512 " in WORKSHARE construct", expr->value.function.esym->name,
1517 else if (expr->value.function.actual != NULL
1518 && expr->value.function.isym != NULL
1519 && expr->value.function.isym->generic_id != GFC_ISYM_LBOUND
1520 && expr->value.function.isym->generic_id != GFC_ISYM_LOC
1521 && expr->value.function.isym->generic_id != GFC_ISYM_PRESENT)
1523 /* Array intrinsics must also have the last upper bound of an
1524 assumed size array argument. UBOUND and SIZE have to be
1525 excluded from the check if the second argument is anything
1528 inquiry = expr->value.function.isym->generic_id == GFC_ISYM_UBOUND
1529 || expr->value.function.isym->generic_id == GFC_ISYM_SIZE;
1531 for (arg = expr->value.function.actual; arg; arg = arg->next)
1533 if (inquiry && arg->next != NULL && arg->next->expr
1534 && arg->next->expr->expr_type != EXPR_CONSTANT)
1537 if (arg->expr != NULL
1538 && arg->expr->rank > 0
1539 && resolve_assumed_size_actual (arg->expr))
1544 need_full_assumed_size = temp;
1546 if (!pure_function (expr, &name) && name)
1551 ("reference to non-PURE function '%s' at %L inside a "
1552 "FORALL %s", name, &expr->where, forall_flag == 2 ?
1556 else if (gfc_pure (NULL))
1558 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
1559 "procedure within a PURE procedure", name, &expr->where);
1564 /* Functions without the RECURSIVE attribution are not allowed to
1565 * call themselves. */
1566 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
1568 gfc_symbol *esym, *proc;
1569 esym = expr->value.function.esym;
1570 proc = gfc_current_ns->proc_name;
1573 gfc_error ("Function '%s' at %L cannot call itself, as it is not "
1574 "RECURSIVE", name, &expr->where);
1578 if (esym->attr.entry && esym->ns->entries && proc->ns->entries
1579 && esym->ns->entries->sym == proc->ns->entries->sym)
1581 gfc_error ("Call to ENTRY '%s' at %L is recursive, but function "
1582 "'%s' is not declared as RECURSIVE",
1583 esym->name, &expr->where, esym->ns->entries->sym->name);
1588 /* Character lengths of use associated functions may contains references to
1589 symbols not referenced from the current program unit otherwise. Make sure
1590 those symbols are marked as referenced. */
1592 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
1593 && expr->value.function.esym->attr.use_assoc)
1595 gfc_expr_set_symbols_referenced (expr->ts.cl->length);
1599 find_noncopying_intrinsics (expr->value.function.esym,
1600 expr->value.function.actual);
1605 /************* Subroutine resolution *************/
1608 pure_subroutine (gfc_code * c, gfc_symbol * sym)
1615 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
1616 sym->name, &c->loc);
1617 else if (gfc_pure (NULL))
1618 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
1624 resolve_generic_s0 (gfc_code * c, gfc_symbol * sym)
1628 if (sym->attr.generic)
1630 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
1633 c->resolved_sym = s;
1634 pure_subroutine (c, s);
1638 /* TODO: Need to search for elemental references in generic interface. */
1641 if (sym->attr.intrinsic)
1642 return gfc_intrinsic_sub_interface (c, 0);
1649 resolve_generic_s (gfc_code * c)
1654 sym = c->symtree->n.sym;
1658 m = resolve_generic_s0 (c, sym);
1661 else if (m == MATCH_ERROR)
1665 if (sym->ns->parent == NULL)
1667 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1671 if (!generic_sym (sym))
1675 /* Last ditch attempt. */
1676 sym = c->symtree->n.sym;
1677 if (!gfc_generic_intrinsic (sym->name))
1680 ("There is no specific subroutine for the generic '%s' at %L",
1681 sym->name, &c->loc);
1685 m = gfc_intrinsic_sub_interface (c, 0);
1689 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
1690 "intrinsic subroutine interface", sym->name, &c->loc);
1696 /* Resolve a subroutine call known to be specific. */
1699 resolve_specific_s0 (gfc_code * c, gfc_symbol * sym)
1703 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1705 if (sym->attr.dummy)
1707 sym->attr.proc = PROC_DUMMY;
1711 sym->attr.proc = PROC_EXTERNAL;
1715 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
1718 if (sym->attr.intrinsic)
1720 m = gfc_intrinsic_sub_interface (c, 1);
1724 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
1725 "with an intrinsic", sym->name, &c->loc);
1733 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1735 c->resolved_sym = sym;
1736 pure_subroutine (c, sym);
1743 resolve_specific_s (gfc_code * c)
1748 sym = c->symtree->n.sym;
1752 m = resolve_specific_s0 (c, sym);
1755 if (m == MATCH_ERROR)
1758 if (sym->ns->parent == NULL)
1761 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1767 sym = c->symtree->n.sym;
1768 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
1769 sym->name, &c->loc);
1775 /* Resolve a subroutine call not known to be generic nor specific. */
1778 resolve_unknown_s (gfc_code * c)
1782 sym = c->symtree->n.sym;
1784 if (sym->attr.dummy)
1786 sym->attr.proc = PROC_DUMMY;
1790 /* See if we have an intrinsic function reference. */
1792 if (gfc_intrinsic_name (sym->name, 1))
1794 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
1799 /* The reference is to an external name. */
1802 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1804 c->resolved_sym = sym;
1806 pure_subroutine (c, sym);
1812 /* Resolve a subroutine call. Although it was tempting to use the same code
1813 for functions, subroutines and functions are stored differently and this
1814 makes things awkward. */
1817 resolve_call (gfc_code * c)
1821 if (c->symtree && c->symtree->n.sym
1822 && c->symtree->n.sym->ts.type != BT_UNKNOWN)
1824 gfc_error ("'%s' at %L has a type, which is not consistent with "
1825 "the CALL at %L", c->symtree->n.sym->name,
1826 &c->symtree->n.sym->declared_at, &c->loc);
1830 /* If the procedure is not internal or module, it must be external and
1831 should be checked for usage. */
1832 if (c->symtree && c->symtree->n.sym
1833 && !c->symtree->n.sym->attr.dummy
1834 && !c->symtree->n.sym->attr.contained
1835 && !c->symtree->n.sym->attr.use_assoc)
1836 resolve_global_procedure (c->symtree->n.sym, &c->loc, 1);
1838 /* Subroutines without the RECURSIVE attribution are not allowed to
1839 * call themselves. */
1840 if (c->symtree && c->symtree->n.sym && !c->symtree->n.sym->attr.recursive)
1842 gfc_symbol *csym, *proc;
1843 csym = c->symtree->n.sym;
1844 proc = gfc_current_ns->proc_name;
1847 gfc_error ("SUBROUTINE '%s' at %L cannot call itself, as it is not "
1848 "RECURSIVE", csym->name, &c->loc);
1852 if (csym->attr.entry && csym->ns->entries && proc->ns->entries
1853 && csym->ns->entries->sym == proc->ns->entries->sym)
1855 gfc_error ("Call to ENTRY '%s' at %L is recursive, but subroutine "
1856 "'%s' is not declared as RECURSIVE",
1857 csym->name, &c->loc, csym->ns->entries->sym->name);
1862 /* Switch off assumed size checking and do this again for certain kinds
1863 of procedure, once the procedure itself is resolved. */
1864 need_full_assumed_size++;
1866 if (resolve_actual_arglist (c->ext.actual) == FAILURE)
1869 /* Resume assumed_size checking. */
1870 need_full_assumed_size--;
1874 if (c->resolved_sym == NULL)
1875 switch (procedure_kind (c->symtree->n.sym))
1878 t = resolve_generic_s (c);
1881 case PTYPE_SPECIFIC:
1882 t = resolve_specific_s (c);
1886 t = resolve_unknown_s (c);
1890 gfc_internal_error ("resolve_subroutine(): bad function type");
1893 /* Some checks of elemental subroutine actual arguments. */
1894 if (resolve_elemental_actual (NULL, c) == FAILURE)
1898 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
1902 /* Compare the shapes of two arrays that have non-NULL shapes. If both
1903 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
1904 match. If both op1->shape and op2->shape are non-NULL return FAILURE
1905 if their shapes do not match. If either op1->shape or op2->shape is
1906 NULL, return SUCCESS. */
1909 compare_shapes (gfc_expr * op1, gfc_expr * op2)
1916 if (op1->shape != NULL && op2->shape != NULL)
1918 for (i = 0; i < op1->rank; i++)
1920 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
1922 gfc_error ("Shapes for operands at %L and %L are not conformable",
1923 &op1->where, &op2->where);
1933 /* Resolve an operator expression node. This can involve replacing the
1934 operation with a user defined function call. */
1937 resolve_operator (gfc_expr * e)
1939 gfc_expr *op1, *op2;
1943 /* Resolve all subnodes-- give them types. */
1945 switch (e->value.op.operator)
1948 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
1951 /* Fall through... */
1954 case INTRINSIC_UPLUS:
1955 case INTRINSIC_UMINUS:
1956 case INTRINSIC_PARENTHESES:
1957 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
1962 /* Typecheck the new node. */
1964 op1 = e->value.op.op1;
1965 op2 = e->value.op.op2;
1967 switch (e->value.op.operator)
1969 case INTRINSIC_UPLUS:
1970 case INTRINSIC_UMINUS:
1971 if (op1->ts.type == BT_INTEGER
1972 || op1->ts.type == BT_REAL
1973 || op1->ts.type == BT_COMPLEX)
1979 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
1980 gfc_op2string (e->value.op.operator), gfc_typename (&e->ts));
1983 case INTRINSIC_PLUS:
1984 case INTRINSIC_MINUS:
1985 case INTRINSIC_TIMES:
1986 case INTRINSIC_DIVIDE:
1987 case INTRINSIC_POWER:
1988 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
1990 gfc_type_convert_binary (e);
1995 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
1996 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1997 gfc_typename (&op2->ts));
2000 case INTRINSIC_CONCAT:
2001 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
2003 e->ts.type = BT_CHARACTER;
2004 e->ts.kind = op1->ts.kind;
2009 _("Operands of string concatenation operator at %%L are %s/%s"),
2010 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
2016 case INTRINSIC_NEQV:
2017 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
2019 e->ts.type = BT_LOGICAL;
2020 e->ts.kind = gfc_kind_max (op1, op2);
2021 if (op1->ts.kind < e->ts.kind)
2022 gfc_convert_type (op1, &e->ts, 2);
2023 else if (op2->ts.kind < e->ts.kind)
2024 gfc_convert_type (op2, &e->ts, 2);
2028 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
2029 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2030 gfc_typename (&op2->ts));
2035 if (op1->ts.type == BT_LOGICAL)
2037 e->ts.type = BT_LOGICAL;
2038 e->ts.kind = op1->ts.kind;
2042 sprintf (msg, _("Operand of .NOT. operator at %%L is %s"),
2043 gfc_typename (&op1->ts));
2050 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
2052 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
2056 /* Fall through... */
2060 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
2062 e->ts.type = BT_LOGICAL;
2063 e->ts.kind = gfc_default_logical_kind;
2067 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
2069 gfc_type_convert_binary (e);
2071 e->ts.type = BT_LOGICAL;
2072 e->ts.kind = gfc_default_logical_kind;
2076 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
2078 _("Logicals at %%L must be compared with %s instead of %s"),
2079 e->value.op.operator == INTRINSIC_EQ ? ".EQV." : ".NEQV.",
2080 gfc_op2string (e->value.op.operator));
2083 _("Operands of comparison operator '%s' at %%L are %s/%s"),
2084 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2085 gfc_typename (&op2->ts));
2089 case INTRINSIC_USER:
2091 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
2092 e->value.op.uop->name, gfc_typename (&op1->ts));
2094 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
2095 e->value.op.uop->name, gfc_typename (&op1->ts),
2096 gfc_typename (&op2->ts));
2100 case INTRINSIC_PARENTHESES:
2104 gfc_internal_error ("resolve_operator(): Bad intrinsic");
2107 /* Deal with arrayness of an operand through an operator. */
2111 switch (e->value.op.operator)
2113 case INTRINSIC_PLUS:
2114 case INTRINSIC_MINUS:
2115 case INTRINSIC_TIMES:
2116 case INTRINSIC_DIVIDE:
2117 case INTRINSIC_POWER:
2118 case INTRINSIC_CONCAT:
2122 case INTRINSIC_NEQV:
2130 if (op1->rank == 0 && op2->rank == 0)
2133 if (op1->rank == 0 && op2->rank != 0)
2135 e->rank = op2->rank;
2137 if (e->shape == NULL)
2138 e->shape = gfc_copy_shape (op2->shape, op2->rank);
2141 if (op1->rank != 0 && op2->rank == 0)
2143 e->rank = op1->rank;
2145 if (e->shape == NULL)
2146 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2149 if (op1->rank != 0 && op2->rank != 0)
2151 if (op1->rank == op2->rank)
2153 e->rank = op1->rank;
2154 if (e->shape == NULL)
2156 t = compare_shapes(op1, op2);
2160 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2165 gfc_error ("Inconsistent ranks for operator at %L and %L",
2166 &op1->where, &op2->where);
2169 /* Allow higher level expressions to work. */
2177 case INTRINSIC_UPLUS:
2178 case INTRINSIC_UMINUS:
2179 case INTRINSIC_PARENTHESES:
2180 e->rank = op1->rank;
2182 if (e->shape == NULL)
2183 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2185 /* Simply copy arrayness attribute */
2192 /* Attempt to simplify the expression. */
2194 t = gfc_simplify_expr (e, 0);
2199 if (gfc_extend_expr (e) == SUCCESS)
2202 gfc_error (msg, &e->where);
2208 /************** Array resolution subroutines **************/
2212 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
2215 /* Compare two integer expressions. */
2218 compare_bound (gfc_expr * a, gfc_expr * b)
2222 if (a == NULL || a->expr_type != EXPR_CONSTANT
2223 || b == NULL || b->expr_type != EXPR_CONSTANT)
2226 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
2227 gfc_internal_error ("compare_bound(): Bad expression");
2229 i = mpz_cmp (a->value.integer, b->value.integer);
2239 /* Compare an integer expression with an integer. */
2242 compare_bound_int (gfc_expr * a, int b)
2246 if (a == NULL || a->expr_type != EXPR_CONSTANT)
2249 if (a->ts.type != BT_INTEGER)
2250 gfc_internal_error ("compare_bound_int(): Bad expression");
2252 i = mpz_cmp_si (a->value.integer, b);
2262 /* Compare an integer expression with a mpz_t. */
2265 compare_bound_mpz_t (gfc_expr * a, mpz_t b)
2269 if (a == NULL || a->expr_type != EXPR_CONSTANT)
2272 if (a->ts.type != BT_INTEGER)
2273 gfc_internal_error ("compare_bound_int(): Bad expression");
2275 i = mpz_cmp (a->value.integer, b);
2285 /* Compute the last value of a sequence given by a triplet.
2286 Return 0 if it wasn't able to compute the last value, or if the
2287 sequence if empty, and 1 otherwise. */
2290 compute_last_value_for_triplet (gfc_expr * start, gfc_expr * end,
2291 gfc_expr * stride, mpz_t last)
2295 if (start == NULL || start->expr_type != EXPR_CONSTANT
2296 || end == NULL || end->expr_type != EXPR_CONSTANT
2297 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
2300 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
2301 || (stride != NULL && stride->ts.type != BT_INTEGER))
2304 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
2306 if (compare_bound (start, end) == CMP_GT)
2308 mpz_set (last, end->value.integer);
2312 if (compare_bound_int (stride, 0) == CMP_GT)
2314 /* Stride is positive */
2315 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
2320 /* Stride is negative */
2321 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
2326 mpz_sub (rem, end->value.integer, start->value.integer);
2327 mpz_tdiv_r (rem, rem, stride->value.integer);
2328 mpz_sub (last, end->value.integer, rem);
2335 /* Compare a single dimension of an array reference to the array
2339 check_dimension (int i, gfc_array_ref * ar, gfc_array_spec * as)
2343 /* Given start, end and stride values, calculate the minimum and
2344 maximum referenced indexes. */
2352 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
2354 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
2360 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
2362 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
2366 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
2367 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
2369 if (compare_bound (AR_START, AR_END) == CMP_EQ
2370 && (compare_bound (AR_START, as->lower[i]) == CMP_LT
2371 || compare_bound (AR_START, as->upper[i]) == CMP_GT))
2374 if (((compare_bound_int (ar->stride[i], 0) == CMP_GT
2375 || ar->stride[i] == NULL)
2376 && compare_bound (AR_START, AR_END) != CMP_GT)
2377 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
2378 && compare_bound (AR_START, AR_END) != CMP_LT))
2380 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
2382 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
2386 mpz_init (last_value);
2387 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
2390 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT
2391 || compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
2393 mpz_clear (last_value);
2397 mpz_clear (last_value);
2405 gfc_internal_error ("check_dimension(): Bad array reference");
2411 gfc_warning ("Array reference at %L is out of bounds", &ar->c_where[i]);
2416 /* Compare an array reference with an array specification. */
2419 compare_spec_to_ref (gfc_array_ref * ar)
2426 /* TODO: Full array sections are only allowed as actual parameters. */
2427 if (as->type == AS_ASSUMED_SIZE
2428 && (/*ar->type == AR_FULL
2429 ||*/ (ar->type == AR_SECTION
2430 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
2432 gfc_error ("Rightmost upper bound of assumed size array section"
2433 " not specified at %L", &ar->where);
2437 if (ar->type == AR_FULL)
2440 if (as->rank != ar->dimen)
2442 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
2443 &ar->where, ar->dimen, as->rank);
2447 for (i = 0; i < as->rank; i++)
2448 if (check_dimension (i, ar, as) == FAILURE)
2455 /* Resolve one part of an array index. */
2458 gfc_resolve_index (gfc_expr * index, int check_scalar)
2465 if (gfc_resolve_expr (index) == FAILURE)
2468 if (check_scalar && index->rank != 0)
2470 gfc_error ("Array index at %L must be scalar", &index->where);
2474 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
2476 gfc_error ("Array index at %L must be of INTEGER type",
2481 if (index->ts.type == BT_REAL)
2482 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
2483 &index->where) == FAILURE)
2486 if (index->ts.kind != gfc_index_integer_kind
2487 || index->ts.type != BT_INTEGER)
2490 ts.type = BT_INTEGER;
2491 ts.kind = gfc_index_integer_kind;
2493 gfc_convert_type_warn (index, &ts, 2, 0);
2499 /* Resolve a dim argument to an intrinsic function. */
2502 gfc_resolve_dim_arg (gfc_expr *dim)
2507 if (gfc_resolve_expr (dim) == FAILURE)
2512 gfc_error ("Argument dim at %L must be scalar", &dim->where);
2516 if (dim->ts.type != BT_INTEGER)
2518 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
2521 if (dim->ts.kind != gfc_index_integer_kind)
2525 ts.type = BT_INTEGER;
2526 ts.kind = gfc_index_integer_kind;
2528 gfc_convert_type_warn (dim, &ts, 2, 0);
2534 /* Given an expression that contains array references, update those array
2535 references to point to the right array specifications. While this is
2536 filled in during matching, this information is difficult to save and load
2537 in a module, so we take care of it here.
2539 The idea here is that the original array reference comes from the
2540 base symbol. We traverse the list of reference structures, setting
2541 the stored reference to references. Component references can
2542 provide an additional array specification. */
2545 find_array_spec (gfc_expr * e)
2549 gfc_symbol *derived;
2552 as = e->symtree->n.sym->as;
2555 for (ref = e->ref; ref; ref = ref->next)
2560 gfc_internal_error ("find_array_spec(): Missing spec");
2567 if (derived == NULL)
2568 derived = e->symtree->n.sym->ts.derived;
2570 c = derived->components;
2572 for (; c; c = c->next)
2573 if (c == ref->u.c.component)
2575 /* Track the sequence of component references. */
2576 if (c->ts.type == BT_DERIVED)
2577 derived = c->ts.derived;
2582 gfc_internal_error ("find_array_spec(): Component not found");
2587 gfc_internal_error ("find_array_spec(): unused as(1)");
2598 gfc_internal_error ("find_array_spec(): unused as(2)");
2602 /* Resolve an array reference. */
2605 resolve_array_ref (gfc_array_ref * ar)
2607 int i, check_scalar;
2610 for (i = 0; i < ar->dimen; i++)
2612 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
2614 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
2616 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
2618 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
2623 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
2627 ar->dimen_type[i] = DIMEN_ELEMENT;
2631 ar->dimen_type[i] = DIMEN_VECTOR;
2632 if (e->expr_type == EXPR_VARIABLE
2633 && e->symtree->n.sym->ts.type == BT_DERIVED)
2634 ar->start[i] = gfc_get_parentheses (e);
2638 gfc_error ("Array index at %L is an array of rank %d",
2639 &ar->c_where[i], e->rank);
2644 /* If the reference type is unknown, figure out what kind it is. */
2646 if (ar->type == AR_UNKNOWN)
2648 ar->type = AR_ELEMENT;
2649 for (i = 0; i < ar->dimen; i++)
2650 if (ar->dimen_type[i] == DIMEN_RANGE
2651 || ar->dimen_type[i] == DIMEN_VECTOR)
2653 ar->type = AR_SECTION;
2658 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
2666 resolve_substring (gfc_ref * ref)
2669 if (ref->u.ss.start != NULL)
2671 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
2674 if (ref->u.ss.start->ts.type != BT_INTEGER)
2676 gfc_error ("Substring start index at %L must be of type INTEGER",
2677 &ref->u.ss.start->where);
2681 if (ref->u.ss.start->rank != 0)
2683 gfc_error ("Substring start index at %L must be scalar",
2684 &ref->u.ss.start->where);
2688 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
2689 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
2690 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
2692 gfc_error ("Substring start index at %L is less than one",
2693 &ref->u.ss.start->where);
2698 if (ref->u.ss.end != NULL)
2700 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
2703 if (ref->u.ss.end->ts.type != BT_INTEGER)
2705 gfc_error ("Substring end index at %L must be of type INTEGER",
2706 &ref->u.ss.end->where);
2710 if (ref->u.ss.end->rank != 0)
2712 gfc_error ("Substring end index at %L must be scalar",
2713 &ref->u.ss.end->where);
2717 if (ref->u.ss.length != NULL
2718 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
2719 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
2720 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
2722 gfc_error ("Substring end index at %L exceeds the string length",
2723 &ref->u.ss.start->where);
2732 /* Resolve subtype references. */
2735 resolve_ref (gfc_expr * expr)
2737 int current_part_dimension, n_components, seen_part_dimension;
2740 for (ref = expr->ref; ref; ref = ref->next)
2741 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
2743 find_array_spec (expr);
2747 for (ref = expr->ref; ref; ref = ref->next)
2751 if (resolve_array_ref (&ref->u.ar) == FAILURE)
2759 resolve_substring (ref);
2763 /* Check constraints on part references. */
2765 current_part_dimension = 0;
2766 seen_part_dimension = 0;
2769 for (ref = expr->ref; ref; ref = ref->next)
2774 switch (ref->u.ar.type)
2778 current_part_dimension = 1;
2782 current_part_dimension = 0;
2786 gfc_internal_error ("resolve_ref(): Bad array reference");
2792 if (current_part_dimension || seen_part_dimension)
2794 if (ref->u.c.component->pointer)
2797 ("Component to the right of a part reference with nonzero "
2798 "rank must not have the POINTER attribute at %L",
2802 else if (ref->u.c.component->allocatable)
2805 ("Component to the right of a part reference with nonzero "
2806 "rank must not have the ALLOCATABLE attribute at %L",
2819 if (((ref->type == REF_COMPONENT && n_components > 1)
2820 || ref->next == NULL)
2821 && current_part_dimension
2822 && seen_part_dimension)
2825 gfc_error ("Two or more part references with nonzero rank must "
2826 "not be specified at %L", &expr->where);
2830 if (ref->type == REF_COMPONENT)
2832 if (current_part_dimension)
2833 seen_part_dimension = 1;
2835 /* reset to make sure */
2836 current_part_dimension = 0;
2844 /* Given an expression, determine its shape. This is easier than it sounds.
2845 Leaves the shape array NULL if it is not possible to determine the shape. */
2848 expression_shape (gfc_expr * e)
2850 mpz_t array[GFC_MAX_DIMENSIONS];
2853 if (e->rank == 0 || e->shape != NULL)
2856 for (i = 0; i < e->rank; i++)
2857 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
2860 e->shape = gfc_get_shape (e->rank);
2862 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
2867 for (i--; i >= 0; i--)
2868 mpz_clear (array[i]);
2872 /* Given a variable expression node, compute the rank of the expression by
2873 examining the base symbol and any reference structures it may have. */
2876 expression_rank (gfc_expr * e)
2883 if (e->expr_type == EXPR_ARRAY)
2885 /* Constructors can have a rank different from one via RESHAPE(). */
2887 if (e->symtree == NULL)
2893 e->rank = (e->symtree->n.sym->as == NULL)
2894 ? 0 : e->symtree->n.sym->as->rank;
2900 for (ref = e->ref; ref; ref = ref->next)
2902 if (ref->type != REF_ARRAY)
2905 if (ref->u.ar.type == AR_FULL)
2907 rank = ref->u.ar.as->rank;
2911 if (ref->u.ar.type == AR_SECTION)
2913 /* Figure out the rank of the section. */
2915 gfc_internal_error ("expression_rank(): Two array specs");
2917 for (i = 0; i < ref->u.ar.dimen; i++)
2918 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
2919 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
2929 expression_shape (e);
2933 /* Resolve a variable expression. */
2936 resolve_variable (gfc_expr * e)
2943 if (e->symtree == NULL)
2946 if (e->ref && resolve_ref (e) == FAILURE)
2949 sym = e->symtree->n.sym;
2950 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2952 e->ts.type = BT_PROCEDURE;
2956 if (sym->ts.type != BT_UNKNOWN)
2957 gfc_variable_attr (e, &e->ts);
2960 /* Must be a simple variable reference. */
2961 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
2966 if (check_assumed_size_reference (sym, e))
2969 /* Deal with forward references to entries during resolve_code, to
2970 satisfy, at least partially, 12.5.2.5. */
2971 if (gfc_current_ns->entries
2972 && current_entry_id == sym->entry_id
2975 && cs_base->current->op != EXEC_ENTRY)
2977 gfc_entry_list *entry;
2978 gfc_formal_arglist *formal;
2982 /* If the symbol is a dummy... */
2983 if (sym->attr.dummy)
2985 entry = gfc_current_ns->entries;
2988 /* ...test if the symbol is a parameter of previous entries. */
2989 for (; entry && entry->id <= current_entry_id; entry = entry->next)
2990 for (formal = entry->sym->formal; formal; formal = formal->next)
2992 if (formal->sym && sym->name == formal->sym->name)
2996 /* If it has not been seen as a dummy, this is an error. */
2999 if (specification_expr)
3000 gfc_error ("Variable '%s',used in a specification expression, "
3001 "is referenced at %L before the ENTRY statement "
3002 "in which it is a parameter",
3003 sym->name, &cs_base->current->loc);
3005 gfc_error ("Variable '%s' is used at %L before the ENTRY "
3006 "statement in which it is a parameter",
3007 sym->name, &cs_base->current->loc);
3012 /* Now do the same check on the specification expressions. */
3013 specification_expr = 1;
3014 if (sym->ts.type == BT_CHARACTER
3015 && gfc_resolve_expr (sym->ts.cl->length) == FAILURE)
3019 for (n = 0; n < sym->as->rank; n++)
3021 specification_expr = 1;
3022 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
3024 specification_expr = 1;
3025 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
3028 specification_expr = 0;
3031 /* Update the symbol's entry level. */
3032 sym->entry_id = current_entry_id + 1;
3039 /* Resolve an expression. That is, make sure that types of operands agree
3040 with their operators, intrinsic operators are converted to function calls
3041 for overloaded types and unresolved function references are resolved. */
3044 gfc_resolve_expr (gfc_expr * e)
3051 switch (e->expr_type)
3054 t = resolve_operator (e);
3058 t = resolve_function (e);
3062 t = resolve_variable (e);
3064 expression_rank (e);
3067 case EXPR_SUBSTRING:
3068 t = resolve_ref (e);
3078 if (resolve_ref (e) == FAILURE)
3081 t = gfc_resolve_array_constructor (e);
3082 /* Also try to expand a constructor. */
3085 expression_rank (e);
3086 gfc_expand_constructor (e);
3089 /* This provides the opportunity for the length of constructors with character
3090 valued function elements to propogate the string length to the expression. */
3091 if (e->ts.type == BT_CHARACTER)
3092 gfc_resolve_character_array_constructor (e);
3096 case EXPR_STRUCTURE:
3097 t = resolve_ref (e);
3101 t = resolve_structure_cons (e);
3105 t = gfc_simplify_expr (e, 0);
3109 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
3116 /* Resolve an expression from an iterator. They must be scalar and have
3117 INTEGER or (optionally) REAL type. */
3120 gfc_resolve_iterator_expr (gfc_expr * expr, bool real_ok,
3121 const char * name_msgid)
3123 if (gfc_resolve_expr (expr) == FAILURE)
3126 if (expr->rank != 0)
3128 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
3132 if (!(expr->ts.type == BT_INTEGER
3133 || (expr->ts.type == BT_REAL && real_ok)))
3136 gfc_error ("%s at %L must be INTEGER or REAL", _(name_msgid),
3139 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
3146 /* Resolve the expressions in an iterator structure. If REAL_OK is
3147 false allow only INTEGER type iterators, otherwise allow REAL types. */
3150 gfc_resolve_iterator (gfc_iterator * iter, bool real_ok)
3153 if (iter->var->ts.type == BT_REAL)
3154 gfc_notify_std (GFC_STD_F95_DEL,
3155 "Obsolete: REAL DO loop iterator at %L",
3158 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
3162 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
3164 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
3169 if (gfc_resolve_iterator_expr (iter->start, real_ok,
3170 "Start expression in DO loop") == FAILURE)
3173 if (gfc_resolve_iterator_expr (iter->end, real_ok,
3174 "End expression in DO loop") == FAILURE)
3177 if (gfc_resolve_iterator_expr (iter->step, real_ok,
3178 "Step expression in DO loop") == FAILURE)
3181 if (iter->step->expr_type == EXPR_CONSTANT)
3183 if ((iter->step->ts.type == BT_INTEGER
3184 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
3185 || (iter->step->ts.type == BT_REAL
3186 && mpfr_sgn (iter->step->value.real) == 0))
3188 gfc_error ("Step expression in DO loop at %L cannot be zero",
3189 &iter->step->where);
3194 /* Convert start, end, and step to the same type as var. */
3195 if (iter->start->ts.kind != iter->var->ts.kind
3196 || iter->start->ts.type != iter->var->ts.type)
3197 gfc_convert_type (iter->start, &iter->var->ts, 2);
3199 if (iter->end->ts.kind != iter->var->ts.kind
3200 || iter->end->ts.type != iter->var->ts.type)
3201 gfc_convert_type (iter->end, &iter->var->ts, 2);
3203 if (iter->step->ts.kind != iter->var->ts.kind
3204 || iter->step->ts.type != iter->var->ts.type)
3205 gfc_convert_type (iter->step, &iter->var->ts, 2);
3211 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
3212 to be a scalar INTEGER variable. The subscripts and stride are scalar
3213 INTEGERs, and if stride is a constant it must be nonzero. */
3216 resolve_forall_iterators (gfc_forall_iterator * iter)
3221 if (gfc_resolve_expr (iter->var) == SUCCESS
3222 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
3223 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
3226 if (gfc_resolve_expr (iter->start) == SUCCESS
3227 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
3228 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
3229 &iter->start->where);
3230 if (iter->var->ts.kind != iter->start->ts.kind)
3231 gfc_convert_type (iter->start, &iter->var->ts, 2);
3233 if (gfc_resolve_expr (iter->end) == SUCCESS
3234 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
3235 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
3237 if (iter->var->ts.kind != iter->end->ts.kind)
3238 gfc_convert_type (iter->end, &iter->var->ts, 2);
3240 if (gfc_resolve_expr (iter->stride) == SUCCESS)
3242 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
3243 gfc_error ("FORALL stride expression at %L must be a scalar %s",
3244 &iter->stride->where, "INTEGER");
3246 if (iter->stride->expr_type == EXPR_CONSTANT
3247 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
3248 gfc_error ("FORALL stride expression at %L cannot be zero",
3249 &iter->stride->where);
3251 if (iter->var->ts.kind != iter->stride->ts.kind)
3252 gfc_convert_type (iter->stride, &iter->var->ts, 2);
3259 /* Given a pointer to a symbol that is a derived type, see if any components
3260 have the POINTER attribute. The search is recursive if necessary.
3261 Returns zero if no pointer components are found, nonzero otherwise. */
3264 derived_pointer (gfc_symbol * sym)
3268 for (c = sym->components; c; c = c->next)
3273 if (c->ts.type == BT_DERIVED && derived_pointer (c->ts.derived))
3281 /* Given a pointer to a symbol that is a derived type, see if it's
3282 inaccessible, i.e. if it's defined in another module and the components are
3283 PRIVATE. The search is recursive if necessary. Returns zero if no
3284 inaccessible components are found, nonzero otherwise. */
3287 derived_inaccessible (gfc_symbol *sym)
3291 if (sym->attr.use_assoc && sym->component_access == ACCESS_PRIVATE)
3294 for (c = sym->components; c; c = c->next)
3296 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.derived))
3304 /* Resolve the argument of a deallocate expression. The expression must be
3305 a pointer or a full array. */
3308 resolve_deallocate_expr (gfc_expr * e)
3310 symbol_attribute attr;
3314 if (gfc_resolve_expr (e) == FAILURE)
3317 attr = gfc_expr_attr (e);
3321 if (e->expr_type != EXPR_VARIABLE)
3324 allocatable = e->symtree->n.sym->attr.allocatable;
3325 for (ref = e->ref; ref; ref = ref->next)
3329 if (ref->u.ar.type != AR_FULL)
3334 allocatable = (ref->u.c.component->as != NULL
3335 && ref->u.c.component->as->type == AS_DEFERRED);
3343 if (allocatable == 0)
3346 gfc_error ("Expression in DEALLOCATE statement at %L must be "
3347 "ALLOCATABLE or a POINTER", &e->where);
3350 if (e->symtree->n.sym->attr.intent == INTENT_IN)
3352 gfc_error ("Can't deallocate INTENT(IN) variable '%s' at %L",
3353 e->symtree->n.sym->name, &e->where);
3360 /* Returns true if the expression e contains a reference the symbol sym. */
3362 find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
3364 gfc_actual_arglist *arg;
3372 switch (e->expr_type)
3375 for (arg = e->value.function.actual; arg; arg = arg->next)
3376 rv = rv || find_sym_in_expr (sym, arg->expr);
3379 /* If the variable is not the same as the dependent, 'sym', and
3380 it is not marked as being declared and it is in the same
3381 namespace as 'sym', add it to the local declarations. */
3383 if (sym == e->symtree->n.sym)
3388 rv = rv || find_sym_in_expr (sym, e->value.op.op1);
3389 rv = rv || find_sym_in_expr (sym, e->value.op.op2);
3398 for (ref = e->ref; ref; ref = ref->next)
3403 for (i = 0; i < ref->u.ar.dimen; i++)
3405 rv = rv || find_sym_in_expr (sym, ref->u.ar.start[i]);
3406 rv = rv || find_sym_in_expr (sym, ref->u.ar.end[i]);
3407 rv = rv || find_sym_in_expr (sym, ref->u.ar.stride[i]);
3412 rv = rv || find_sym_in_expr (sym, ref->u.ss.start);
3413 rv = rv || find_sym_in_expr (sym, ref->u.ss.end);
3417 if (ref->u.c.component->ts.type == BT_CHARACTER
3418 && ref->u.c.component->ts.cl->length->expr_type
3420 rv = rv || find_sym_in_expr (sym, ref->u.c.component->ts.cl->length);
3422 if (ref->u.c.component->as)
3423 for (i = 0; i < ref->u.c.component->as->rank; i++)
3425 rv = rv || find_sym_in_expr (sym, ref->u.c.component->as->lower[i]);
3426 rv = rv || find_sym_in_expr (sym, ref->u.c.component->as->upper[i]);
3436 /* Given the expression node e for an allocatable/pointer of derived type to be
3437 allocated, get the expression node to be initialized afterwards (needed for
3438 derived types with default initializers, and derived types with allocatable
3439 components that need nullification.) */
3442 expr_to_initialize (gfc_expr * e)
3448 result = gfc_copy_expr (e);
3450 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
3451 for (ref = result->ref; ref; ref = ref->next)
3452 if (ref->type == REF_ARRAY && ref->next == NULL)
3454 ref->u.ar.type = AR_FULL;
3456 for (i = 0; i < ref->u.ar.dimen; i++)
3457 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
3459 result->rank = ref->u.ar.dimen;
3467 /* Resolve the expression in an ALLOCATE statement, doing the additional
3468 checks to see whether the expression is OK or not. The expression must
3469 have a trailing array reference that gives the size of the array. */
3472 resolve_allocate_expr (gfc_expr * e, gfc_code * code)
3474 int i, pointer, allocatable, dimension;
3475 symbol_attribute attr;
3476 gfc_ref *ref, *ref2;
3483 if (gfc_resolve_expr (e) == FAILURE)
3486 if (code->expr && code->expr->expr_type == EXPR_VARIABLE)
3487 sym = code->expr->symtree->n.sym;
3491 /* Make sure the expression is allocatable or a pointer. If it is
3492 pointer, the next-to-last reference must be a pointer. */
3496 if (e->expr_type != EXPR_VARIABLE)
3500 attr = gfc_expr_attr (e);
3501 pointer = attr.pointer;
3502 dimension = attr.dimension;
3507 allocatable = e->symtree->n.sym->attr.allocatable;
3508 pointer = e->symtree->n.sym->attr.pointer;
3509 dimension = e->symtree->n.sym->attr.dimension;
3511 if (sym == e->symtree->n.sym && sym->ts.type != BT_DERIVED)
3513 gfc_error ("The STAT variable '%s' in an ALLOCATE statement must "
3514 "not be allocated in the same statement at %L",
3515 sym->name, &e->where);
3519 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
3523 if (ref->next != NULL)
3528 allocatable = (ref->u.c.component->as != NULL
3529 && ref->u.c.component->as->type == AS_DEFERRED);
3531 pointer = ref->u.c.component->pointer;
3532 dimension = ref->u.c.component->dimension;
3542 if (allocatable == 0 && pointer == 0)
3544 gfc_error ("Expression in ALLOCATE statement at %L must be "
3545 "ALLOCATABLE or a POINTER", &e->where);
3549 if (e->symtree->n.sym->attr.intent == INTENT_IN)
3551 gfc_error ("Can't allocate INTENT(IN) variable '%s' at %L",
3552 e->symtree->n.sym->name, &e->where);
3556 /* Add default initializer for those derived types that need them. */
3557 if (e->ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&e->ts)))
3559 init_st = gfc_get_code ();
3560 init_st->loc = code->loc;
3561 init_st->op = EXEC_INIT_ASSIGN;
3562 init_st->expr = expr_to_initialize (e);
3563 init_st->expr2 = init_e;
3564 init_st->next = code->next;
3565 code->next = init_st;