1 /* Perform type resolution on the various stuctures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
32 /* Types used in equivalence statements. */
36 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
40 /* Stack to keep track of the nesting of blocks as we move through the
41 code. See resolve_branch() and resolve_code(). */
43 typedef struct code_stack
45 struct gfc_code *head, *current, *tail;
46 struct code_stack *prev;
48 /* This bitmap keeps track of the targets valid for a branch from
50 bitmap reachable_labels;
54 static code_stack *cs_base = NULL;
57 /* Nonzero if we're inside a FORALL block. */
59 static int forall_flag;
61 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
63 static int omp_workshare_flag;
65 /* Nonzero if we are processing a formal arglist. The corresponding function
66 resets the flag each time that it is read. */
67 static int formal_arg_flag = 0;
69 /* True if we are resolving a specification expression. */
70 static int specification_expr = 0;
72 /* The id of the last entry seen. */
73 static int current_entry_id;
75 /* We use bitmaps to determine if a branch target is valid. */
76 static bitmap_obstack labels_obstack;
79 gfc_is_formal_arg (void)
81 return formal_arg_flag;
84 /* Resolve types of formal argument lists. These have to be done early so that
85 the formal argument lists of module procedures can be copied to the
86 containing module before the individual procedures are resolved
87 individually. We also resolve argument lists of procedures in interface
88 blocks because they are self-contained scoping units.
90 Since a dummy argument cannot be a non-dummy procedure, the only
91 resort left for untyped names are the IMPLICIT types. */
94 resolve_formal_arglist (gfc_symbol *proc)
96 gfc_formal_arglist *f;
100 if (proc->result != NULL)
105 if (gfc_elemental (proc)
106 || sym->attr.pointer || sym->attr.allocatable
107 || (sym->as && sym->as->rank > 0))
108 proc->attr.always_explicit = 1;
112 for (f = proc->formal; f; f = f->next)
118 /* Alternate return placeholder. */
119 if (gfc_elemental (proc))
120 gfc_error ("Alternate return specifier in elemental subroutine "
121 "'%s' at %L is not allowed", proc->name,
123 if (proc->attr.function)
124 gfc_error ("Alternate return specifier in function "
125 "'%s' at %L is not allowed", proc->name,
130 if (sym->attr.if_source != IFSRC_UNKNOWN)
131 resolve_formal_arglist (sym);
133 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
135 if (gfc_pure (proc) && !gfc_pure (sym))
137 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
138 "also be PURE", sym->name, &sym->declared_at);
142 if (gfc_elemental (proc))
144 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
145 "procedure", &sym->declared_at);
149 if (sym->attr.function
150 && sym->ts.type == BT_UNKNOWN
151 && sym->attr.intrinsic)
153 gfc_intrinsic_sym *isym;
154 isym = gfc_find_function (sym->name);
155 if (isym == NULL || !isym->specific)
157 gfc_error ("Unable to find a specific INTRINSIC procedure "
158 "for the reference '%s' at %L", sym->name,
167 if (sym->ts.type == BT_UNKNOWN)
169 if (!sym->attr.function || sym->result == sym)
170 gfc_set_default_type (sym, 1, sym->ns);
173 gfc_resolve_array_spec (sym->as, 0);
175 /* We can't tell if an array with dimension (:) is assumed or deferred
176 shape until we know if it has the pointer or allocatable attributes.
178 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
179 && !(sym->attr.pointer || sym->attr.allocatable))
181 sym->as->type = AS_ASSUMED_SHAPE;
182 for (i = 0; i < sym->as->rank; i++)
183 sym->as->lower[i] = gfc_int_expr (1);
186 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
187 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
188 || sym->attr.optional)
189 proc->attr.always_explicit = 1;
191 /* If the flavor is unknown at this point, it has to be a variable.
192 A procedure specification would have already set the type. */
194 if (sym->attr.flavor == FL_UNKNOWN)
195 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
197 if (gfc_pure (proc) && !sym->attr.pointer
198 && sym->attr.flavor != FL_PROCEDURE)
200 if (proc->attr.function && sym->attr.intent != INTENT_IN)
201 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
202 "INTENT(IN)", sym->name, proc->name,
205 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
206 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
207 "have its INTENT specified", sym->name, proc->name,
211 if (gfc_elemental (proc))
215 gfc_error ("Argument '%s' of elemental procedure at %L must "
216 "be scalar", sym->name, &sym->declared_at);
220 if (sym->attr.pointer)
222 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
223 "have the POINTER attribute", sym->name,
229 /* Each dummy shall be specified to be scalar. */
230 if (proc->attr.proc == PROC_ST_FUNCTION)
234 gfc_error ("Argument '%s' of statement function at %L must "
235 "be scalar", sym->name, &sym->declared_at);
239 if (sym->ts.type == BT_CHARACTER)
241 gfc_charlen *cl = sym->ts.cl;
242 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
244 gfc_error ("Character-valued argument '%s' of statement "
245 "function at %L must have constant length",
246 sym->name, &sym->declared_at);
256 /* Work function called when searching for symbols that have argument lists
257 associated with them. */
260 find_arglists (gfc_symbol *sym)
262 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
265 resolve_formal_arglist (sym);
269 /* Given a namespace, resolve all formal argument lists within the namespace.
273 resolve_formal_arglists (gfc_namespace *ns)
278 gfc_traverse_ns (ns, find_arglists);
283 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
287 /* If this namespace is not a function or an entry master function,
289 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
290 || sym->attr.entry_master)
293 /* Try to find out of what the return type is. */
294 if (sym->result->ts.type == BT_UNKNOWN)
296 t = gfc_set_default_type (sym->result, 0, ns);
298 if (t == FAILURE && !sym->result->attr.untyped)
300 if (sym->result == sym)
301 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
302 sym->name, &sym->declared_at);
304 gfc_error ("Result '%s' of contained function '%s' at %L has "
305 "no IMPLICIT type", sym->result->name, sym->name,
306 &sym->result->declared_at);
307 sym->result->attr.untyped = 1;
311 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
312 type, lists the only ways a character length value of * can be used:
313 dummy arguments of procedures, named constants, and function results
314 in external functions. Internal function results are not on that list;
315 ergo, not permitted. */
317 if (sym->result->ts.type == BT_CHARACTER)
319 gfc_charlen *cl = sym->result->ts.cl;
320 if (!cl || !cl->length)
321 gfc_error ("Character-valued internal function '%s' at %L must "
322 "not be assumed length", sym->name, &sym->declared_at);
327 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
328 introduce duplicates. */
331 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
333 gfc_formal_arglist *f, *new_arglist;
336 for (; new_args != NULL; new_args = new_args->next)
338 new_sym = new_args->sym;
339 /* See if this arg is already in the formal argument list. */
340 for (f = proc->formal; f; f = f->next)
342 if (new_sym == f->sym)
349 /* Add a new argument. Argument order is not important. */
350 new_arglist = gfc_get_formal_arglist ();
351 new_arglist->sym = new_sym;
352 new_arglist->next = proc->formal;
353 proc->formal = new_arglist;
358 /* Flag the arguments that are not present in all entries. */
361 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
363 gfc_formal_arglist *f, *head;
366 for (f = proc->formal; f; f = f->next)
371 for (new_args = head; new_args; new_args = new_args->next)
373 if (new_args->sym == f->sym)
380 f->sym->attr.not_always_present = 1;
385 /* Resolve alternate entry points. If a symbol has multiple entry points we
386 create a new master symbol for the main routine, and turn the existing
387 symbol into an entry point. */
390 resolve_entries (gfc_namespace *ns)
392 gfc_namespace *old_ns;
396 char name[GFC_MAX_SYMBOL_LEN + 1];
397 static int master_count = 0;
399 if (ns->proc_name == NULL)
402 /* No need to do anything if this procedure doesn't have alternate entry
407 /* We may already have resolved alternate entry points. */
408 if (ns->proc_name->attr.entry_master)
411 /* If this isn't a procedure something has gone horribly wrong. */
412 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
414 /* Remember the current namespace. */
415 old_ns = gfc_current_ns;
419 /* Add the main entry point to the list of entry points. */
420 el = gfc_get_entry_list ();
421 el->sym = ns->proc_name;
423 el->next = ns->entries;
425 ns->proc_name->attr.entry = 1;
427 /* If it is a module function, it needs to be in the right namespace
428 so that gfc_get_fake_result_decl can gather up the results. The
429 need for this arose in get_proc_name, where these beasts were
430 left in their own namespace, to keep prior references linked to
431 the entry declaration.*/
432 if (ns->proc_name->attr.function
433 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
436 /* Do the same for entries where the master is not a module
437 procedure. These are retained in the module namespace because
438 of the module procedure declaration. */
439 for (el = el->next; el; el = el->next)
440 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
441 && el->sym->attr.mod_proc)
445 /* Add an entry statement for it. */
452 /* Create a new symbol for the master function. */
453 /* Give the internal function a unique name (within this file).
454 Also include the function name so the user has some hope of figuring
455 out what is going on. */
456 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
457 master_count++, ns->proc_name->name);
458 gfc_get_ha_symbol (name, &proc);
459 gcc_assert (proc != NULL);
461 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
462 if (ns->proc_name->attr.subroutine)
463 gfc_add_subroutine (&proc->attr, proc->name, NULL);
467 gfc_typespec *ts, *fts;
468 gfc_array_spec *as, *fas;
469 gfc_add_function (&proc->attr, proc->name, NULL);
471 fas = ns->entries->sym->as;
472 fas = fas ? fas : ns->entries->sym->result->as;
473 fts = &ns->entries->sym->result->ts;
474 if (fts->type == BT_UNKNOWN)
475 fts = gfc_get_default_type (ns->entries->sym->result, NULL);
476 for (el = ns->entries->next; el; el = el->next)
478 ts = &el->sym->result->ts;
480 as = as ? as : el->sym->result->as;
481 if (ts->type == BT_UNKNOWN)
482 ts = gfc_get_default_type (el->sym->result, NULL);
484 if (! gfc_compare_types (ts, fts)
485 || (el->sym->result->attr.dimension
486 != ns->entries->sym->result->attr.dimension)
487 || (el->sym->result->attr.pointer
488 != ns->entries->sym->result->attr.pointer))
491 else if (as && fas && gfc_compare_array_spec (as, fas) == 0)
492 gfc_error ("Procedure %s at %L has entries with mismatched "
493 "array specifications", ns->entries->sym->name,
494 &ns->entries->sym->declared_at);
499 sym = ns->entries->sym->result;
500 /* All result types the same. */
502 if (sym->attr.dimension)
503 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
504 if (sym->attr.pointer)
505 gfc_add_pointer (&proc->attr, NULL);
509 /* Otherwise the result will be passed through a union by
511 proc->attr.mixed_entry_master = 1;
512 for (el = ns->entries; el; el = el->next)
514 sym = el->sym->result;
515 if (sym->attr.dimension)
517 if (el == ns->entries)
518 gfc_error ("FUNCTION result %s can't be an array in "
519 "FUNCTION %s at %L", sym->name,
520 ns->entries->sym->name, &sym->declared_at);
522 gfc_error ("ENTRY result %s can't be an array in "
523 "FUNCTION %s at %L", sym->name,
524 ns->entries->sym->name, &sym->declared_at);
526 else if (sym->attr.pointer)
528 if (el == ns->entries)
529 gfc_error ("FUNCTION result %s can't be a POINTER in "
530 "FUNCTION %s at %L", sym->name,
531 ns->entries->sym->name, &sym->declared_at);
533 gfc_error ("ENTRY result %s can't be a POINTER in "
534 "FUNCTION %s at %L", sym->name,
535 ns->entries->sym->name, &sym->declared_at);
540 if (ts->type == BT_UNKNOWN)
541 ts = gfc_get_default_type (sym, NULL);
545 if (ts->kind == gfc_default_integer_kind)
549 if (ts->kind == gfc_default_real_kind
550 || ts->kind == gfc_default_double_kind)
554 if (ts->kind == gfc_default_complex_kind)
558 if (ts->kind == gfc_default_logical_kind)
562 /* We will issue error elsewhere. */
570 if (el == ns->entries)
571 gfc_error ("FUNCTION result %s can't be of type %s "
572 "in FUNCTION %s at %L", sym->name,
573 gfc_typename (ts), ns->entries->sym->name,
576 gfc_error ("ENTRY result %s can't be of type %s "
577 "in FUNCTION %s at %L", sym->name,
578 gfc_typename (ts), ns->entries->sym->name,
585 proc->attr.access = ACCESS_PRIVATE;
586 proc->attr.entry_master = 1;
588 /* Merge all the entry point arguments. */
589 for (el = ns->entries; el; el = el->next)
590 merge_argument_lists (proc, el->sym->formal);
592 /* Check the master formal arguments for any that are not
593 present in all entry points. */
594 for (el = ns->entries; el; el = el->next)
595 check_argument_lists (proc, el->sym->formal);
597 /* Use the master function for the function body. */
598 ns->proc_name = proc;
600 /* Finalize the new symbols. */
601 gfc_commit_symbols ();
603 /* Restore the original namespace. */
604 gfc_current_ns = old_ns;
609 has_default_initializer (gfc_symbol *der)
613 gcc_assert (der->attr.flavor == FL_DERIVED);
614 for (c = der->components; c; c = c->next)
615 if ((c->ts.type != BT_DERIVED && c->initializer)
616 || (c->ts.type == BT_DERIVED
617 && (!c->pointer && has_default_initializer (c->ts.derived))))
624 /* Resolve common blocks. */
626 resolve_common_blocks (gfc_symtree *common_root)
628 gfc_symbol *sym, *csym;
630 if (common_root == NULL)
633 if (common_root->left)
634 resolve_common_blocks (common_root->left);
635 if (common_root->right)
636 resolve_common_blocks (common_root->right);
638 for (csym = common_root->n.common->head; csym; csym = csym->common_next)
640 if (csym->ts.type != BT_DERIVED)
643 if (!(csym->ts.derived->attr.sequence
644 || csym->ts.derived->attr.is_bind_c))
645 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
646 "has neither the SEQUENCE nor the BIND(C) "
647 "attribute", csym->name, &csym->declared_at);
648 if (csym->ts.derived->attr.alloc_comp)
649 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
650 "has an ultimate component that is "
651 "allocatable", csym->name, &csym->declared_at);
652 if (has_default_initializer (csym->ts.derived))
653 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
654 "may not have default initializer", csym->name,
658 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
662 if (sym->attr.flavor == FL_PARAMETER)
663 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
664 sym->name, &common_root->n.common->where, &sym->declared_at);
666 if (sym->attr.intrinsic)
667 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
668 sym->name, &common_root->n.common->where);
669 else if (sym->attr.result
670 ||(sym->attr.function && gfc_current_ns->proc_name == sym))
671 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
672 "that is also a function result", sym->name,
673 &common_root->n.common->where);
674 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
675 && sym->attr.proc != PROC_ST_FUNCTION)
676 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
677 "that is also a global procedure", sym->name,
678 &common_root->n.common->where);
682 /* Resolve contained function types. Because contained functions can call one
683 another, they have to be worked out before any of the contained procedures
686 The good news is that if a function doesn't already have a type, the only
687 way it can get one is through an IMPLICIT type or a RESULT variable, because
688 by definition contained functions are contained namespace they're contained
689 in, not in a sibling or parent namespace. */
692 resolve_contained_functions (gfc_namespace *ns)
694 gfc_namespace *child;
697 resolve_formal_arglists (ns);
699 for (child = ns->contained; child; child = child->sibling)
701 /* Resolve alternate entry points first. */
702 resolve_entries (child);
704 /* Then check function return types. */
705 resolve_contained_fntype (child->proc_name, child);
706 for (el = child->entries; el; el = el->next)
707 resolve_contained_fntype (el->sym, child);
712 /* Resolve all of the elements of a structure constructor and make sure that
713 the types are correct. */
716 resolve_structure_cons (gfc_expr *expr)
718 gfc_constructor *cons;
724 cons = expr->value.constructor;
725 /* A constructor may have references if it is the result of substituting a
726 parameter variable. In this case we just pull out the component we
729 comp = expr->ref->u.c.sym->components;
731 comp = expr->ts.derived->components;
733 /* See if the user is trying to invoke a structure constructor for one of
734 the iso_c_binding derived types. */
735 if (expr->ts.derived && expr->ts.derived->ts.is_iso_c && cons
736 && cons->expr != NULL)
738 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
739 expr->ts.derived->name, &(expr->where));
743 for (; comp; comp = comp->next, cons = cons->next)
750 if (gfc_resolve_expr (cons->expr) == FAILURE)
756 rank = comp->as ? comp->as->rank : 0;
757 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
758 && (comp->allocatable || cons->expr->rank))
760 gfc_error ("The rank of the element in the derived type "
761 "constructor at %L does not match that of the "
762 "component (%d/%d)", &cons->expr->where,
763 cons->expr->rank, rank);
767 /* If we don't have the right type, try to convert it. */
769 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
772 if (comp->pointer && cons->expr->ts.type != BT_UNKNOWN)
773 gfc_error ("The element in the derived type constructor at %L, "
774 "for pointer component '%s', is %s but should be %s",
775 &cons->expr->where, comp->name,
776 gfc_basic_typename (cons->expr->ts.type),
777 gfc_basic_typename (comp->ts.type));
779 t = gfc_convert_type (cons->expr, &comp->ts, 1);
782 if (!comp->pointer || cons->expr->expr_type == EXPR_NULL)
785 a = gfc_expr_attr (cons->expr);
787 if (!a.pointer && !a.target)
790 gfc_error ("The element in the derived type constructor at %L, "
791 "for pointer component '%s' should be a POINTER or "
792 "a TARGET", &cons->expr->where, comp->name);
800 /****************** Expression name resolution ******************/
802 /* Returns 0 if a symbol was not declared with a type or
803 attribute declaration statement, nonzero otherwise. */
806 was_declared (gfc_symbol *sym)
812 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
815 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
816 || a.optional || a.pointer || a.save || a.target || a.volatile_
817 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
824 /* Determine if a symbol is generic or not. */
827 generic_sym (gfc_symbol *sym)
831 if (sym->attr.generic ||
832 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
835 if (was_declared (sym) || sym->ns->parent == NULL)
838 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
845 return generic_sym (s);
852 /* Determine if a symbol is specific or not. */
855 specific_sym (gfc_symbol *sym)
859 if (sym->attr.if_source == IFSRC_IFBODY
860 || sym->attr.proc == PROC_MODULE
861 || sym->attr.proc == PROC_INTERNAL
862 || sym->attr.proc == PROC_ST_FUNCTION
863 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
864 || sym->attr.external)
867 if (was_declared (sym) || sym->ns->parent == NULL)
870 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
872 return (s == NULL) ? 0 : specific_sym (s);
876 /* Figure out if the procedure is specific, generic or unknown. */
879 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
883 procedure_kind (gfc_symbol *sym)
885 if (generic_sym (sym))
886 return PTYPE_GENERIC;
888 if (specific_sym (sym))
889 return PTYPE_SPECIFIC;
891 return PTYPE_UNKNOWN;
894 /* Check references to assumed size arrays. The flag need_full_assumed_size
895 is nonzero when matching actual arguments. */
897 static int need_full_assumed_size = 0;
900 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
906 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
909 for (ref = e->ref; ref; ref = ref->next)
910 if (ref->type == REF_ARRAY)
911 for (dim = 0; dim < ref->u.ar.as->rank; dim++)
912 last = (ref->u.ar.end[dim] == NULL)
913 && (ref->u.ar.type == DIMEN_ELEMENT);
917 gfc_error ("The upper bound in the last dimension must "
918 "appear in the reference to the assumed size "
919 "array '%s' at %L", sym->name, &e->where);
926 /* Look for bad assumed size array references in argument expressions
927 of elemental and array valued intrinsic procedures. Since this is
928 called from procedure resolution functions, it only recurses at
932 resolve_assumed_size_actual (gfc_expr *e)
937 switch (e->expr_type)
940 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
945 if (resolve_assumed_size_actual (e->value.op.op1)
946 || resolve_assumed_size_actual (e->value.op.op2))
957 /* Resolve an actual argument list. Most of the time, this is just
958 resolving the expressions in the list.
959 The exception is that we sometimes have to decide whether arguments
960 that look like procedure arguments are really simple variable
964 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype)
967 gfc_symtree *parent_st;
970 for (; arg; arg = arg->next)
975 /* Check the label is a valid branching target. */
978 if (arg->label->defined == ST_LABEL_UNKNOWN)
980 gfc_error ("Label %d referenced at %L is never defined",
981 arg->label->value, &arg->label->where);
988 if (e->expr_type == FL_VARIABLE && e->symtree->ambiguous)
990 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
995 if (e->ts.type != BT_PROCEDURE)
997 if (gfc_resolve_expr (e) != SUCCESS)
1002 /* See if the expression node should really be a variable reference. */
1004 sym = e->symtree->n.sym;
1006 if (sym->attr.flavor == FL_PROCEDURE
1007 || sym->attr.intrinsic
1008 || sym->attr.external)
1012 /* If a procedure is not already determined to be something else
1013 check if it is intrinsic. */
1014 if (!sym->attr.intrinsic
1015 && !(sym->attr.external || sym->attr.use_assoc
1016 || sym->attr.if_source == IFSRC_IFBODY)
1017 && gfc_intrinsic_name (sym->name, sym->attr.subroutine))
1018 sym->attr.intrinsic = 1;
1020 if (sym->attr.proc == PROC_ST_FUNCTION)
1022 gfc_error ("Statement function '%s' at %L is not allowed as an "
1023 "actual argument", sym->name, &e->where);
1026 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1027 sym->attr.subroutine);
1028 if (sym->attr.intrinsic && actual_ok == 0)
1030 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1031 "actual argument", sym->name, &e->where);
1034 if (sym->attr.contained && !sym->attr.use_assoc
1035 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1037 gfc_error ("Internal procedure '%s' is not allowed as an "
1038 "actual argument at %L", sym->name, &e->where);
1041 if (sym->attr.elemental && !sym->attr.intrinsic)
1043 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1044 "allowed as an actual argument at %L", sym->name,
1048 /* Check if a generic interface has a specific procedure
1049 with the same name before emitting an error. */
1050 if (sym->attr.generic)
1053 for (p = sym->generic; p; p = p->next)
1054 if (strcmp (sym->name, p->sym->name) == 0)
1056 e->symtree = gfc_find_symtree
1057 (p->sym->ns->sym_root, sym->name);
1062 if (p == NULL || e->symtree == NULL)
1063 gfc_error ("GENERIC procedure '%s' is not "
1064 "allowed as an actual argument at %L", sym->name,
1068 /* If the symbol is the function that names the current (or
1069 parent) scope, then we really have a variable reference. */
1071 if (sym->attr.function && sym->result == sym
1072 && (sym->ns->proc_name == sym
1073 || (sym->ns->parent != NULL
1074 && sym->ns->parent->proc_name == sym)))
1077 /* If all else fails, see if we have a specific intrinsic. */
1078 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1080 gfc_intrinsic_sym *isym;
1082 isym = gfc_find_function (sym->name);
1083 if (isym == NULL || !isym->specific)
1085 gfc_error ("Unable to find a specific INTRINSIC procedure "
1086 "for the reference '%s' at %L", sym->name,
1091 sym->attr.intrinsic = 1;
1092 sym->attr.function = 1;
1097 /* See if the name is a module procedure in a parent unit. */
1099 if (was_declared (sym) || sym->ns->parent == NULL)
1102 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1104 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1108 if (parent_st == NULL)
1111 sym = parent_st->n.sym;
1112 e->symtree = parent_st; /* Point to the right thing. */
1114 if (sym->attr.flavor == FL_PROCEDURE
1115 || sym->attr.intrinsic
1116 || sym->attr.external)
1122 e->expr_type = EXPR_VARIABLE;
1124 if (sym->as != NULL)
1126 e->rank = sym->as->rank;
1127 e->ref = gfc_get_ref ();
1128 e->ref->type = REF_ARRAY;
1129 e->ref->u.ar.type = AR_FULL;
1130 e->ref->u.ar.as = sym->as;
1133 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1134 primary.c (match_actual_arg). If above code determines that it
1135 is a variable instead, it needs to be resolved as it was not
1136 done at the beginning of this function. */
1137 if (gfc_resolve_expr (e) != SUCCESS)
1141 /* Check argument list functions %VAL, %LOC and %REF. There is
1142 nothing to do for %REF. */
1143 if (arg->name && arg->name[0] == '%')
1145 if (strncmp ("%VAL", arg->name, 4) == 0)
1147 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1149 gfc_error ("By-value argument at %L is not of numeric "
1156 gfc_error ("By-value argument at %L cannot be an array or "
1157 "an array section", &e->where);
1161 /* Intrinsics are still PROC_UNKNOWN here. However,
1162 since same file external procedures are not resolvable
1163 in gfortran, it is a good deal easier to leave them to
1165 if (ptype != PROC_UNKNOWN
1166 && ptype != PROC_DUMMY
1167 && ptype != PROC_EXTERNAL
1168 && ptype != PROC_MODULE)
1170 gfc_error ("By-value argument at %L is not allowed "
1171 "in this context", &e->where);
1176 /* Statement functions have already been excluded above. */
1177 else if (strncmp ("%LOC", arg->name, 4) == 0
1178 && e->ts.type == BT_PROCEDURE)
1180 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1182 gfc_error ("Passing internal procedure at %L by location "
1183 "not allowed", &e->where);
1194 /* Do the checks of the actual argument list that are specific to elemental
1195 procedures. If called with c == NULL, we have a function, otherwise if
1196 expr == NULL, we have a subroutine. */
1199 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1201 gfc_actual_arglist *arg0;
1202 gfc_actual_arglist *arg;
1203 gfc_symbol *esym = NULL;
1204 gfc_intrinsic_sym *isym = NULL;
1206 gfc_intrinsic_arg *iformal = NULL;
1207 gfc_formal_arglist *eformal = NULL;
1208 bool formal_optional = false;
1209 bool set_by_optional = false;
1213 /* Is this an elemental procedure? */
1214 if (expr && expr->value.function.actual != NULL)
1216 if (expr->value.function.esym != NULL
1217 && expr->value.function.esym->attr.elemental)
1219 arg0 = expr->value.function.actual;
1220 esym = expr->value.function.esym;
1222 else if (expr->value.function.isym != NULL
1223 && expr->value.function.isym->elemental)
1225 arg0 = expr->value.function.actual;
1226 isym = expr->value.function.isym;
1231 else if (c && c->ext.actual != NULL && c->symtree->n.sym->attr.elemental)
1233 arg0 = c->ext.actual;
1234 esym = c->symtree->n.sym;
1239 /* The rank of an elemental is the rank of its array argument(s). */
1240 for (arg = arg0; arg; arg = arg->next)
1242 if (arg->expr != NULL && arg->expr->rank > 0)
1244 rank = arg->expr->rank;
1245 if (arg->expr->expr_type == EXPR_VARIABLE
1246 && arg->expr->symtree->n.sym->attr.optional)
1247 set_by_optional = true;
1249 /* Function specific; set the result rank and shape. */
1253 if (!expr->shape && arg->expr->shape)
1255 expr->shape = gfc_get_shape (rank);
1256 for (i = 0; i < rank; i++)
1257 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1264 /* If it is an array, it shall not be supplied as an actual argument
1265 to an elemental procedure unless an array of the same rank is supplied
1266 as an actual argument corresponding to a nonoptional dummy argument of
1267 that elemental procedure(12.4.1.5). */
1268 formal_optional = false;
1270 iformal = isym->formal;
1272 eformal = esym->formal;
1274 for (arg = arg0; arg; arg = arg->next)
1278 if (eformal->sym && eformal->sym->attr.optional)
1279 formal_optional = true;
1280 eformal = eformal->next;
1282 else if (isym && iformal)
1284 if (iformal->optional)
1285 formal_optional = true;
1286 iformal = iformal->next;
1289 formal_optional = true;
1291 if (pedantic && arg->expr != NULL
1292 && arg->expr->expr_type == EXPR_VARIABLE
1293 && arg->expr->symtree->n.sym->attr.optional
1296 && (set_by_optional || arg->expr->rank != rank)
1297 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1299 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1300 "MISSING, it cannot be the actual argument of an "
1301 "ELEMENTAL procedure unless there is a non-optional "
1302 "argument with the same rank (12.4.1.5)",
1303 arg->expr->symtree->n.sym->name, &arg->expr->where);
1308 for (arg = arg0; arg; arg = arg->next)
1310 if (arg->expr == NULL || arg->expr->rank == 0)
1313 /* Being elemental, the last upper bound of an assumed size array
1314 argument must be present. */
1315 if (resolve_assumed_size_actual (arg->expr))
1318 /* Elemental procedure's array actual arguments must conform. */
1321 if (gfc_check_conformance ("elemental procedure", arg->expr, e)
1329 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1330 is an array, the intent inout/out variable needs to be also an array. */
1331 if (rank > 0 && esym && expr == NULL)
1332 for (eformal = esym->formal, arg = arg0; arg && eformal;
1333 arg = arg->next, eformal = eformal->next)
1334 if ((eformal->sym->attr.intent == INTENT_OUT
1335 || eformal->sym->attr.intent == INTENT_INOUT)
1336 && arg->expr && arg->expr->rank == 0)
1338 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1339 "ELEMENTAL subroutine '%s' is a scalar, but another "
1340 "actual argument is an array", &arg->expr->where,
1341 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1342 : "INOUT", eformal->sym->name, esym->name);
1349 /* Go through each actual argument in ACTUAL and see if it can be
1350 implemented as an inlined, non-copying intrinsic. FNSYM is the
1351 function being called, or NULL if not known. */
1354 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1356 gfc_actual_arglist *ap;
1359 for (ap = actual; ap; ap = ap->next)
1361 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1362 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual))
1363 ap->expr->inline_noncopying_intrinsic = 1;
1367 /* This function does the checking of references to global procedures
1368 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1369 77 and 95 standards. It checks for a gsymbol for the name, making
1370 one if it does not already exist. If it already exists, then the
1371 reference being resolved must correspond to the type of gsymbol.
1372 Otherwise, the new symbol is equipped with the attributes of the
1373 reference. The corresponding code that is called in creating
1374 global entities is parse.c. */
1377 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
1382 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1384 gsym = gfc_get_gsymbol (sym->name);
1386 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1387 gfc_global_used (gsym, where);
1389 if (gsym->type == GSYM_UNKNOWN)
1392 gsym->where = *where;
1399 /************* Function resolution *************/
1401 /* Resolve a function call known to be generic.
1402 Section 14.1.2.4.1. */
1405 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1409 if (sym->attr.generic)
1411 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1414 expr->value.function.name = s->name;
1415 expr->value.function.esym = s;
1417 if (s->ts.type != BT_UNKNOWN)
1419 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1420 expr->ts = s->result->ts;
1423 expr->rank = s->as->rank;
1424 else if (s->result != NULL && s->result->as != NULL)
1425 expr->rank = s->result->as->rank;
1430 /* TODO: Need to search for elemental references in generic
1434 if (sym->attr.intrinsic)
1435 return gfc_intrinsic_func_interface (expr, 0);
1442 resolve_generic_f (gfc_expr *expr)
1447 sym = expr->symtree->n.sym;
1451 m = resolve_generic_f0 (expr, sym);
1454 else if (m == MATCH_ERROR)
1458 if (sym->ns->parent == NULL)
1460 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1464 if (!generic_sym (sym))
1468 /* Last ditch attempt. See if the reference is to an intrinsic
1469 that possesses a matching interface. 14.1.2.4 */
1470 if (sym && !gfc_intrinsic_name (sym->name, 0))
1472 gfc_error ("There is no specific function for the generic '%s' at %L",
1473 expr->symtree->n.sym->name, &expr->where);
1477 m = gfc_intrinsic_func_interface (expr, 0);
1481 gfc_error ("Generic function '%s' at %L is not consistent with a "
1482 "specific intrinsic interface", expr->symtree->n.sym->name,
1489 /* Resolve a function call known to be specific. */
1492 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
1496 /* See if we have an intrinsic interface. */
1498 if (sym->interface != NULL && sym->interface->attr.intrinsic)
1500 gfc_intrinsic_sym *isym;
1501 isym = gfc_find_function (sym->interface->name);
1503 /* Existance of isym should be checked already. */
1507 sym->attr.function = 1;
1508 sym->attr.proc = PROC_EXTERNAL;
1512 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1514 if (sym->attr.dummy)
1516 sym->attr.proc = PROC_DUMMY;
1520 sym->attr.proc = PROC_EXTERNAL;
1524 if (sym->attr.proc == PROC_MODULE
1525 || sym->attr.proc == PROC_ST_FUNCTION
1526 || sym->attr.proc == PROC_INTERNAL)
1529 if (sym->attr.intrinsic)
1531 m = gfc_intrinsic_func_interface (expr, 1);
1535 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
1536 "with an intrinsic", sym->name, &expr->where);
1544 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1547 expr->value.function.name = sym->name;
1548 expr->value.function.esym = sym;
1549 if (sym->as != NULL)
1550 expr->rank = sym->as->rank;
1557 resolve_specific_f (gfc_expr *expr)
1562 sym = expr->symtree->n.sym;
1566 m = resolve_specific_f0 (sym, expr);
1569 if (m == MATCH_ERROR)
1572 if (sym->ns->parent == NULL)
1575 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1581 gfc_error ("Unable to resolve the specific function '%s' at %L",
1582 expr->symtree->n.sym->name, &expr->where);
1588 /* Resolve a procedure call not known to be generic nor specific. */
1591 resolve_unknown_f (gfc_expr *expr)
1596 sym = expr->symtree->n.sym;
1598 if (sym->attr.dummy)
1600 sym->attr.proc = PROC_DUMMY;
1601 expr->value.function.name = sym->name;
1605 /* See if we have an intrinsic function reference. */
1607 if (gfc_intrinsic_name (sym->name, 0))
1609 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
1614 /* The reference is to an external name. */
1616 sym->attr.proc = PROC_EXTERNAL;
1617 expr->value.function.name = sym->name;
1618 expr->value.function.esym = expr->symtree->n.sym;
1620 if (sym->as != NULL)
1621 expr->rank = sym->as->rank;
1623 /* Type of the expression is either the type of the symbol or the
1624 default type of the symbol. */
1627 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1629 if (sym->ts.type != BT_UNKNOWN)
1633 ts = gfc_get_default_type (sym, sym->ns);
1635 if (ts->type == BT_UNKNOWN)
1637 gfc_error ("Function '%s' at %L has no IMPLICIT type",
1638 sym->name, &expr->where);
1649 /* Return true, if the symbol is an external procedure. */
1651 is_external_proc (gfc_symbol *sym)
1653 if (!sym->attr.dummy && !sym->attr.contained
1654 && !(sym->attr.intrinsic
1655 || gfc_intrinsic_name (sym->name, sym->attr.subroutine))
1656 && sym->attr.proc != PROC_ST_FUNCTION
1657 && !sym->attr.use_assoc
1665 /* Figure out if a function reference is pure or not. Also set the name
1666 of the function for a potential error message. Return nonzero if the
1667 function is PURE, zero if not. */
1669 pure_stmt_function (gfc_expr *, gfc_symbol *);
1672 pure_function (gfc_expr *e, const char **name)
1678 if (e->symtree != NULL
1679 && e->symtree->n.sym != NULL
1680 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
1681 return pure_stmt_function (e, e->symtree->n.sym);
1683 if (e->value.function.esym)
1685 pure = gfc_pure (e->value.function.esym);
1686 *name = e->value.function.esym->name;
1688 else if (e->value.function.isym)
1690 pure = e->value.function.isym->pure
1691 || e->value.function.isym->elemental;
1692 *name = e->value.function.isym->name;
1696 /* Implicit functions are not pure. */
1698 *name = e->value.function.name;
1706 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
1707 int *f ATTRIBUTE_UNUSED)
1711 /* Don't bother recursing into other statement functions
1712 since they will be checked individually for purity. */
1713 if (e->expr_type != EXPR_FUNCTION
1715 || e->symtree->n.sym == sym
1716 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
1719 return pure_function (e, &name) ? false : true;
1724 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
1726 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
1731 is_scalar_expr_ptr (gfc_expr *expr)
1733 try retval = SUCCESS;
1738 /* See if we have a gfc_ref, which means we have a substring, array
1739 reference, or a component. */
1740 if (expr->ref != NULL)
1743 while (ref->next != NULL)
1749 if (ref->u.ss.length != NULL
1750 && ref->u.ss.length->length != NULL
1752 && ref->u.ss.start->expr_type == EXPR_CONSTANT
1754 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
1756 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
1757 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
1758 if (end - start + 1 != 1)
1765 if (ref->u.ar.type == AR_ELEMENT)
1767 else if (ref->u.ar.type == AR_FULL)
1769 /* The user can give a full array if the array is of size 1. */
1770 if (ref->u.ar.as != NULL
1771 && ref->u.ar.as->rank == 1
1772 && ref->u.ar.as->type == AS_EXPLICIT
1773 && ref->u.ar.as->lower[0] != NULL
1774 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
1775 && ref->u.ar.as->upper[0] != NULL
1776 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
1778 /* If we have a character string, we need to check if
1779 its length is one. */
1780 if (expr->ts.type == BT_CHARACTER)
1782 if (expr->ts.cl == NULL
1783 || expr->ts.cl->length == NULL
1784 || mpz_cmp_si (expr->ts.cl->length->value.integer, 1)
1790 /* We have constant lower and upper bounds. If the
1791 difference between is 1, it can be considered a
1793 start = (int) mpz_get_si
1794 (ref->u.ar.as->lower[0]->value.integer);
1795 end = (int) mpz_get_si
1796 (ref->u.ar.as->upper[0]->value.integer);
1797 if (end - start + 1 != 1)
1812 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
1814 /* Character string. Make sure it's of length 1. */
1815 if (expr->ts.cl == NULL
1816 || expr->ts.cl->length == NULL
1817 || mpz_cmp_si (expr->ts.cl->length->value.integer, 1) != 0)
1820 else if (expr->rank != 0)
1827 /* Match one of the iso_c_binding functions (c_associated or c_loc)
1828 and, in the case of c_associated, set the binding label based on
1832 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
1833 gfc_symbol **new_sym)
1835 char name[GFC_MAX_SYMBOL_LEN + 1];
1836 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
1837 int optional_arg = 0;
1838 try retval = SUCCESS;
1839 gfc_symbol *args_sym;
1840 gfc_typespec *arg_ts;
1841 gfc_ref *parent_ref;
1844 if (args->expr->expr_type == EXPR_CONSTANT
1845 || args->expr->expr_type == EXPR_OP
1846 || args->expr->expr_type == EXPR_NULL)
1848 gfc_error ("Argument to '%s' at %L is not a variable",
1849 sym->name, &(args->expr->where));
1853 args_sym = args->expr->symtree->n.sym;
1855 /* The typespec for the actual arg should be that stored in the expr
1856 and not necessarily that of the expr symbol (args_sym), because
1857 the actual expression could be a part-ref of the expr symbol. */
1858 arg_ts = &(args->expr->ts);
1860 /* Get the parent reference (if any) for the expression. This happens for
1861 cases such as a%b%c. */
1862 parent_ref = args->expr->ref;
1864 if (parent_ref != NULL)
1866 curr_ref = parent_ref->next;
1867 while (curr_ref != NULL && curr_ref->next != NULL)
1869 parent_ref = curr_ref;
1870 curr_ref = curr_ref->next;
1874 /* If curr_ref is non-NULL, we had a part-ref expression. If the curr_ref
1875 is for a REF_COMPONENT, then we need to use it as the parent_ref for
1876 the name, etc. Otherwise, the current parent_ref should be correct. */
1877 if (curr_ref != NULL && curr_ref->type == REF_COMPONENT)
1878 parent_ref = curr_ref;
1880 if (parent_ref == args->expr->ref)
1882 else if (parent_ref != NULL && parent_ref->type != REF_COMPONENT)
1883 gfc_internal_error ("Unexpected expression reference type in "
1884 "gfc_iso_c_func_interface");
1886 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
1888 /* If the user gave two args then they are providing something for
1889 the optional arg (the second cptr). Therefore, set the name and
1890 binding label to the c_associated for two cptrs. Otherwise,
1891 set c_associated to expect one cptr. */
1895 sprintf (name, "%s_2", sym->name);
1896 sprintf (binding_label, "%s_2", sym->binding_label);
1902 sprintf (name, "%s_1", sym->name);
1903 sprintf (binding_label, "%s_1", sym->binding_label);
1907 /* Get a new symbol for the version of c_associated that
1909 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
1911 else if (sym->intmod_sym_id == ISOCBINDING_LOC
1912 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
1914 sprintf (name, "%s", sym->name);
1915 sprintf (binding_label, "%s", sym->binding_label);
1917 /* Error check the call. */
1918 if (args->next != NULL)
1920 gfc_error_now ("More actual than formal arguments in '%s' "
1921 "call at %L", name, &(args->expr->where));
1924 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
1926 /* Make sure we have either the target or pointer attribute. */
1927 if (!(args_sym->attr.target)
1928 && !(args_sym->attr.pointer)
1929 && (parent_ref == NULL ||
1930 !parent_ref->u.c.component->pointer))
1932 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
1933 "a TARGET or an associated pointer",
1935 sym->name, &(args->expr->where));
1939 /* See if we have interoperable type and type param. */
1940 if (verify_c_interop (arg_ts,
1941 (parent_ref ? parent_ref->u.c.component->name
1943 &(args->expr->where)) == SUCCESS
1944 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
1946 if (args_sym->attr.target == 1)
1948 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
1949 has the target attribute and is interoperable. */
1950 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
1951 allocatable variable that has the TARGET attribute and
1952 is not an array of zero size. */
1953 if (args_sym->attr.allocatable == 1)
1955 if (args_sym->attr.dimension != 0
1956 && (args_sym->as && args_sym->as->rank == 0))
1958 gfc_error_now ("Allocatable variable '%s' used as a "
1959 "parameter to '%s' at %L must not be "
1960 "an array of zero size",
1961 args_sym->name, sym->name,
1962 &(args->expr->where));
1968 /* A non-allocatable target variable with C
1969 interoperable type and type parameters must be
1971 if (args_sym && args_sym->attr.dimension)
1973 if (args_sym->as->type == AS_ASSUMED_SHAPE)
1975 gfc_error ("Assumed-shape array '%s' at %L "
1976 "cannot be an argument to the "
1977 "procedure '%s' because "
1978 "it is not C interoperable",
1980 &(args->expr->where), sym->name);
1983 else if (args_sym->as->type == AS_DEFERRED)
1985 gfc_error ("Deferred-shape array '%s' at %L "
1986 "cannot be an argument to the "
1987 "procedure '%s' because "
1988 "it is not C interoperable",
1990 &(args->expr->where), sym->name);
1995 /* Make sure it's not a character string. Arrays of
1996 any type should be ok if the variable is of a C
1997 interoperable type. */
1998 if (arg_ts->type == BT_CHARACTER)
1999 if (arg_ts->cl != NULL
2000 && (arg_ts->cl->length == NULL
2001 || arg_ts->cl->length->expr_type
2004 (arg_ts->cl->length->value.integer, 1)
2006 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2008 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2009 "at %L must have a length of 1",
2010 args_sym->name, sym->name,
2011 &(args->expr->where));
2016 else if ((args_sym->attr.pointer == 1 ||
2018 && parent_ref->u.c.component->pointer))
2019 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2021 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2023 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2024 "associated scalar POINTER", args_sym->name,
2025 sym->name, &(args->expr->where));
2031 /* The parameter is not required to be C interoperable. If it
2032 is not C interoperable, it must be a nonpolymorphic scalar
2033 with no length type parameters. It still must have either
2034 the pointer or target attribute, and it can be
2035 allocatable (but must be allocated when c_loc is called). */
2036 if (args->expr->rank != 0
2037 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2039 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2040 "scalar", args_sym->name, sym->name,
2041 &(args->expr->where));
2044 else if (arg_ts->type == BT_CHARACTER
2045 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2047 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2048 "%L must have a length of 1",
2049 args_sym->name, sym->name,
2050 &(args->expr->where));
2055 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2057 if (args_sym->attr.flavor != FL_PROCEDURE)
2059 /* TODO: Update this error message to allow for procedure
2060 pointers once they are implemented. */
2061 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2063 args_sym->name, sym->name,
2064 &(args->expr->where));
2067 else if (args_sym->attr.is_bind_c != 1)
2069 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2071 args_sym->name, sym->name,
2072 &(args->expr->where));
2077 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2082 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2083 "iso_c_binding function: '%s'!\n", sym->name);
2090 /* Resolve a function call, which means resolving the arguments, then figuring
2091 out which entity the name refers to. */
2092 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2093 to INTENT(OUT) or INTENT(INOUT). */
2096 resolve_function (gfc_expr *expr)
2098 gfc_actual_arglist *arg;
2103 procedure_type p = PROC_INTRINSIC;
2107 sym = expr->symtree->n.sym;
2109 if (sym && sym->attr.flavor == FL_VARIABLE)
2111 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2115 if (sym && sym->attr.abstract)
2117 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2118 sym->name, &expr->where);
2122 /* If the procedure is external, check for usage. */
2123 if (sym && is_external_proc (sym))
2124 resolve_global_procedure (sym, &expr->where, 0);
2126 /* Switch off assumed size checking and do this again for certain kinds
2127 of procedure, once the procedure itself is resolved. */
2128 need_full_assumed_size++;
2130 if (expr->symtree && expr->symtree->n.sym)
2131 p = expr->symtree->n.sym->attr.proc;
2133 if (resolve_actual_arglist (expr->value.function.actual, p) == FAILURE)
2136 /* Need to setup the call to the correct c_associated, depending on
2137 the number of cptrs to user gives to compare. */
2138 if (sym && sym->attr.is_iso_c == 1)
2140 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2144 /* Get the symtree for the new symbol (resolved func).
2145 the old one will be freed later, when it's no longer used. */
2146 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2149 /* Resume assumed_size checking. */
2150 need_full_assumed_size--;
2152 if (sym && sym->ts.type == BT_CHARACTER
2154 && sym->ts.cl->length == NULL
2156 && expr->value.function.esym == NULL
2157 && !sym->attr.contained)
2159 /* Internal procedures are taken care of in resolve_contained_fntype. */
2160 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2161 "be used at %L since it is not a dummy argument",
2162 sym->name, &expr->where);
2166 /* See if function is already resolved. */
2168 if (expr->value.function.name != NULL)
2170 if (expr->ts.type == BT_UNKNOWN)
2176 /* Apply the rules of section 14.1.2. */
2178 switch (procedure_kind (sym))
2181 t = resolve_generic_f (expr);
2184 case PTYPE_SPECIFIC:
2185 t = resolve_specific_f (expr);
2189 t = resolve_unknown_f (expr);
2193 gfc_internal_error ("resolve_function(): bad function type");
2197 /* If the expression is still a function (it might have simplified),
2198 then we check to see if we are calling an elemental function. */
2200 if (expr->expr_type != EXPR_FUNCTION)
2203 temp = need_full_assumed_size;
2204 need_full_assumed_size = 0;
2206 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2209 if (omp_workshare_flag
2210 && expr->value.function.esym
2211 && ! gfc_elemental (expr->value.function.esym))
2213 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2214 "in WORKSHARE construct", expr->value.function.esym->name,
2219 #define GENERIC_ID expr->value.function.isym->id
2220 else if (expr->value.function.actual != NULL
2221 && expr->value.function.isym != NULL
2222 && GENERIC_ID != GFC_ISYM_LBOUND
2223 && GENERIC_ID != GFC_ISYM_LEN
2224 && GENERIC_ID != GFC_ISYM_LOC
2225 && GENERIC_ID != GFC_ISYM_PRESENT)
2227 /* Array intrinsics must also have the last upper bound of an
2228 assumed size array argument. UBOUND and SIZE have to be
2229 excluded from the check if the second argument is anything
2232 inquiry = GENERIC_ID == GFC_ISYM_UBOUND
2233 || GENERIC_ID == GFC_ISYM_SIZE;
2235 for (arg = expr->value.function.actual; arg; arg = arg->next)
2237 if (inquiry && arg->next != NULL && arg->next->expr)
2239 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2242 if ((int)mpz_get_si (arg->next->expr->value.integer)
2247 if (arg->expr != NULL
2248 && arg->expr->rank > 0
2249 && resolve_assumed_size_actual (arg->expr))
2255 need_full_assumed_size = temp;
2258 if (!pure_function (expr, &name) && name)
2262 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2263 "FORALL %s", name, &expr->where,
2264 forall_flag == 2 ? "mask" : "block");
2267 else if (gfc_pure (NULL))
2269 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2270 "procedure within a PURE procedure", name, &expr->where);
2275 /* Functions without the RECURSIVE attribution are not allowed to
2276 * call themselves. */
2277 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2279 gfc_symbol *esym, *proc;
2280 esym = expr->value.function.esym;
2281 proc = gfc_current_ns->proc_name;
2284 gfc_error ("Function '%s' at %L cannot call itself, as it is not "
2285 "RECURSIVE", name, &expr->where);
2289 if (esym->attr.entry && esym->ns->entries && proc->ns->entries
2290 && esym->ns->entries->sym == proc->ns->entries->sym)
2292 gfc_error ("Call to ENTRY '%s' at %L is recursive, but function "
2293 "'%s' is not declared as RECURSIVE",
2294 esym->name, &expr->where, esym->ns->entries->sym->name);
2299 /* Character lengths of use associated functions may contains references to
2300 symbols not referenced from the current program unit otherwise. Make sure
2301 those symbols are marked as referenced. */
2303 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2304 && expr->value.function.esym->attr.use_assoc)
2306 gfc_expr_set_symbols_referenced (expr->ts.cl->length);
2310 find_noncopying_intrinsics (expr->value.function.esym,
2311 expr->value.function.actual);
2313 /* Make sure that the expression has a typespec that works. */
2314 if (expr->ts.type == BT_UNKNOWN)
2316 if (expr->symtree->n.sym->result
2317 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN)
2318 expr->ts = expr->symtree->n.sym->result->ts;
2325 /************* Subroutine resolution *************/
2328 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2334 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2335 sym->name, &c->loc);
2336 else if (gfc_pure (NULL))
2337 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2343 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2347 if (sym->attr.generic)
2349 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2352 c->resolved_sym = s;
2353 pure_subroutine (c, s);
2357 /* TODO: Need to search for elemental references in generic interface. */
2360 if (sym->attr.intrinsic)
2361 return gfc_intrinsic_sub_interface (c, 0);
2368 resolve_generic_s (gfc_code *c)
2373 sym = c->symtree->n.sym;
2377 m = resolve_generic_s0 (c, sym);
2380 else if (m == MATCH_ERROR)
2384 if (sym->ns->parent == NULL)
2386 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2390 if (!generic_sym (sym))
2394 /* Last ditch attempt. See if the reference is to an intrinsic
2395 that possesses a matching interface. 14.1.2.4 */
2396 sym = c->symtree->n.sym;
2398 if (!gfc_intrinsic_name (sym->name, 1))
2400 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2401 sym->name, &c->loc);
2405 m = gfc_intrinsic_sub_interface (c, 0);
2409 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2410 "intrinsic subroutine interface", sym->name, &c->loc);
2416 /* Set the name and binding label of the subroutine symbol in the call
2417 expression represented by 'c' to include the type and kind of the
2418 second parameter. This function is for resolving the appropriate
2419 version of c_f_pointer() and c_f_procpointer(). For example, a
2420 call to c_f_pointer() for a default integer pointer could have a
2421 name of c_f_pointer_i4. If no second arg exists, which is an error
2422 for these two functions, it defaults to the generic symbol's name
2423 and binding label. */
2426 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2427 char *name, char *binding_label)
2429 gfc_expr *arg = NULL;
2433 /* The second arg of c_f_pointer and c_f_procpointer determines
2434 the type and kind for the procedure name. */
2435 arg = c->ext.actual->next->expr;
2439 /* Set up the name to have the given symbol's name,
2440 plus the type and kind. */
2441 /* a derived type is marked with the type letter 'u' */
2442 if (arg->ts.type == BT_DERIVED)
2445 kind = 0; /* set the kind as 0 for now */
2449 type = gfc_type_letter (arg->ts.type);
2450 kind = arg->ts.kind;
2453 if (arg->ts.type == BT_CHARACTER)
2454 /* Kind info for character strings not needed. */
2457 sprintf (name, "%s_%c%d", sym->name, type, kind);
2458 /* Set up the binding label as the given symbol's label plus
2459 the type and kind. */
2460 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2464 /* If the second arg is missing, set the name and label as
2465 was, cause it should at least be found, and the missing
2466 arg error will be caught by compare_parameters(). */
2467 sprintf (name, "%s", sym->name);
2468 sprintf (binding_label, "%s", sym->binding_label);
2475 /* Resolve a generic version of the iso_c_binding procedure given
2476 (sym) to the specific one based on the type and kind of the
2477 argument(s). Currently, this function resolves c_f_pointer() and
2478 c_f_procpointer based on the type and kind of the second argument
2479 (FPTR). Other iso_c_binding procedures aren't specially handled.
2480 Upon successfully exiting, c->resolved_sym will hold the resolved
2481 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2485 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2487 gfc_symbol *new_sym;
2488 /* this is fine, since we know the names won't use the max */
2489 char name[GFC_MAX_SYMBOL_LEN + 1];
2490 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2491 /* default to success; will override if find error */
2492 match m = MATCH_YES;
2494 /* Make sure the actual arguments are in the necessary order (based on the
2495 formal args) before resolving. */
2496 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2498 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2499 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2501 set_name_and_label (c, sym, name, binding_label);
2503 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2505 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2507 /* Make sure we got a third arg if the second arg has non-zero
2508 rank. We must also check that the type and rank are
2509 correct since we short-circuit this check in
2510 gfc_procedure_use() (called above to sort actual args). */
2511 if (c->ext.actual->next->expr->rank != 0)
2513 if(c->ext.actual->next->next == NULL
2514 || c->ext.actual->next->next->expr == NULL)
2517 gfc_error ("Missing SHAPE parameter for call to %s "
2518 "at %L", sym->name, &(c->loc));
2520 else if (c->ext.actual->next->next->expr->ts.type
2522 || c->ext.actual->next->next->expr->rank != 1)
2525 gfc_error ("SHAPE parameter for call to %s at %L must "
2526 "be a rank 1 INTEGER array", sym->name,
2533 if (m != MATCH_ERROR)
2535 /* the 1 means to add the optional arg to formal list */
2536 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
2538 /* for error reporting, say it's declared where the original was */
2539 new_sym->declared_at = sym->declared_at;
2544 /* no differences for c_loc or c_funloc */
2548 /* set the resolved symbol */
2549 if (m != MATCH_ERROR)
2550 c->resolved_sym = new_sym;
2552 c->resolved_sym = sym;
2558 /* Resolve a subroutine call known to be specific. */
2561 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
2565 /* See if we have an intrinsic interface. */
2566 if (sym->interface != NULL && !sym->interface->attr.abstract
2567 && !sym->interface->attr.subroutine)
2569 gfc_intrinsic_sym *isym;
2571 isym = gfc_find_function (sym->interface->name);
2573 /* Existance of isym should be checked already. */
2577 sym->attr.function = 1;
2581 if(sym->attr.is_iso_c)
2583 m = gfc_iso_c_sub_interface (c,sym);
2587 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2589 if (sym->attr.dummy)
2591 sym->attr.proc = PROC_DUMMY;
2595 sym->attr.proc = PROC_EXTERNAL;
2599 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
2602 if (sym->attr.intrinsic)
2604 m = gfc_intrinsic_sub_interface (c, 1);
2608 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
2609 "with an intrinsic", sym->name, &c->loc);
2617 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2619 c->resolved_sym = sym;
2620 pure_subroutine (c, sym);
2627 resolve_specific_s (gfc_code *c)
2632 sym = c->symtree->n.sym;
2636 m = resolve_specific_s0 (c, sym);
2639 if (m == MATCH_ERROR)
2642 if (sym->ns->parent == NULL)
2645 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2651 sym = c->symtree->n.sym;
2652 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
2653 sym->name, &c->loc);
2659 /* Resolve a subroutine call not known to be generic nor specific. */
2662 resolve_unknown_s (gfc_code *c)
2666 sym = c->symtree->n.sym;
2668 if (sym->attr.dummy)
2670 sym->attr.proc = PROC_DUMMY;
2674 /* See if we have an intrinsic function reference. */
2676 if (gfc_intrinsic_name (sym->name, 1))
2678 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
2683 /* The reference is to an external name. */
2686 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
2688 c->resolved_sym = sym;
2690 pure_subroutine (c, sym);
2696 /* Resolve a subroutine call. Although it was tempting to use the same code
2697 for functions, subroutines and functions are stored differently and this
2698 makes things awkward. */
2701 resolve_call (gfc_code *c)
2704 procedure_type ptype = PROC_INTRINSIC;
2706 if (c->symtree && c->symtree->n.sym
2707 && c->symtree->n.sym->ts.type != BT_UNKNOWN)
2709 gfc_error ("'%s' at %L has a type, which is not consistent with "
2710 "the CALL at %L", c->symtree->n.sym->name,
2711 &c->symtree->n.sym->declared_at, &c->loc);
2715 /* If external, check for usage. */
2716 if (c->symtree && is_external_proc (c->symtree->n.sym))
2717 resolve_global_procedure (c->symtree->n.sym, &c->loc, 1);
2719 /* Subroutines without the RECURSIVE attribution are not allowed to
2720 * call themselves. */
2721 if (c->symtree && c->symtree->n.sym && !c->symtree->n.sym->attr.recursive)
2723 gfc_symbol *csym, *proc;
2724 csym = c->symtree->n.sym;
2725 proc = gfc_current_ns->proc_name;
2728 gfc_error ("SUBROUTINE '%s' at %L cannot call itself, as it is not "
2729 "RECURSIVE", csym->name, &c->loc);
2733 if (csym->attr.entry && csym->ns->entries && proc->ns->entries
2734 && csym->ns->entries->sym == proc->ns->entries->sym)
2736 gfc_error ("Call to ENTRY '%s' at %L is recursive, but subroutine "
2737 "'%s' is not declared as RECURSIVE",
2738 csym->name, &c->loc, csym->ns->entries->sym->name);
2743 /* Switch off assumed size checking and do this again for certain kinds
2744 of procedure, once the procedure itself is resolved. */
2745 need_full_assumed_size++;
2747 if (c->symtree && c->symtree->n.sym)
2748 ptype = c->symtree->n.sym->attr.proc;
2750 if (resolve_actual_arglist (c->ext.actual, ptype) == FAILURE)
2753 /* Resume assumed_size checking. */
2754 need_full_assumed_size--;
2757 if (c->resolved_sym == NULL)
2758 switch (procedure_kind (c->symtree->n.sym))
2761 t = resolve_generic_s (c);
2764 case PTYPE_SPECIFIC:
2765 t = resolve_specific_s (c);
2769 t = resolve_unknown_s (c);
2773 gfc_internal_error ("resolve_subroutine(): bad function type");
2776 /* Some checks of elemental subroutine actual arguments. */
2777 if (resolve_elemental_actual (NULL, c) == FAILURE)
2781 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
2786 /* Compare the shapes of two arrays that have non-NULL shapes. If both
2787 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
2788 match. If both op1->shape and op2->shape are non-NULL return FAILURE
2789 if their shapes do not match. If either op1->shape or op2->shape is
2790 NULL, return SUCCESS. */
2793 compare_shapes (gfc_expr *op1, gfc_expr *op2)
2800 if (op1->shape != NULL && op2->shape != NULL)
2802 for (i = 0; i < op1->rank; i++)
2804 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
2806 gfc_error ("Shapes for operands at %L and %L are not conformable",
2807 &op1->where, &op2->where);
2818 /* Resolve an operator expression node. This can involve replacing the
2819 operation with a user defined function call. */
2822 resolve_operator (gfc_expr *e)
2824 gfc_expr *op1, *op2;
2826 bool dual_locus_error;
2829 /* Resolve all subnodes-- give them types. */
2831 switch (e->value.op.operator)
2834 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
2837 /* Fall through... */
2840 case INTRINSIC_UPLUS:
2841 case INTRINSIC_UMINUS:
2842 case INTRINSIC_PARENTHESES:
2843 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
2848 /* Typecheck the new node. */
2850 op1 = e->value.op.op1;
2851 op2 = e->value.op.op2;
2852 dual_locus_error = false;
2854 if ((op1 && op1->expr_type == EXPR_NULL)
2855 || (op2 && op2->expr_type == EXPR_NULL))
2857 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
2861 switch (e->value.op.operator)
2863 case INTRINSIC_UPLUS:
2864 case INTRINSIC_UMINUS:
2865 if (op1->ts.type == BT_INTEGER
2866 || op1->ts.type == BT_REAL
2867 || op1->ts.type == BT_COMPLEX)
2873 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
2874 gfc_op2string (e->value.op.operator), gfc_typename (&e->ts));
2877 case INTRINSIC_PLUS:
2878 case INTRINSIC_MINUS:
2879 case INTRINSIC_TIMES:
2880 case INTRINSIC_DIVIDE:
2881 case INTRINSIC_POWER:
2882 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
2884 gfc_type_convert_binary (e);
2889 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
2890 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2891 gfc_typename (&op2->ts));
2894 case INTRINSIC_CONCAT:
2895 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
2897 e->ts.type = BT_CHARACTER;
2898 e->ts.kind = op1->ts.kind;
2903 _("Operands of string concatenation operator at %%L are %s/%s"),
2904 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
2910 case INTRINSIC_NEQV:
2911 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
2913 e->ts.type = BT_LOGICAL;
2914 e->ts.kind = gfc_kind_max (op1, op2);
2915 if (op1->ts.kind < e->ts.kind)
2916 gfc_convert_type (op1, &e->ts, 2);
2917 else if (op2->ts.kind < e->ts.kind)
2918 gfc_convert_type (op2, &e->ts, 2);
2922 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
2923 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2924 gfc_typename (&op2->ts));
2929 if (op1->ts.type == BT_LOGICAL)
2931 e->ts.type = BT_LOGICAL;
2932 e->ts.kind = op1->ts.kind;
2936 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
2937 gfc_typename (&op1->ts));
2941 case INTRINSIC_GT_OS:
2943 case INTRINSIC_GE_OS:
2945 case INTRINSIC_LT_OS:
2947 case INTRINSIC_LE_OS:
2948 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
2950 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
2954 /* Fall through... */
2957 case INTRINSIC_EQ_OS:
2959 case INTRINSIC_NE_OS:
2960 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
2962 e->ts.type = BT_LOGICAL;
2963 e->ts.kind = gfc_default_logical_kind;
2967 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
2969 gfc_type_convert_binary (e);
2971 e->ts.type = BT_LOGICAL;
2972 e->ts.kind = gfc_default_logical_kind;
2976 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
2978 _("Logicals at %%L must be compared with %s instead of %s"),
2979 (e->value.op.operator == INTRINSIC_EQ
2980 || e->value.op.operator == INTRINSIC_EQ_OS)
2981 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.operator));
2984 _("Operands of comparison operator '%s' at %%L are %s/%s"),
2985 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2986 gfc_typename (&op2->ts));
2990 case INTRINSIC_USER:
2991 if (e->value.op.uop->operator == NULL)
2992 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
2993 else if (op2 == NULL)
2994 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
2995 e->value.op.uop->name, gfc_typename (&op1->ts));
2997 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
2998 e->value.op.uop->name, gfc_typename (&op1->ts),
2999 gfc_typename (&op2->ts));
3003 case INTRINSIC_PARENTHESES:
3005 if (e->ts.type == BT_CHARACTER)
3006 e->ts.cl = op1->ts.cl;
3010 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3013 /* Deal with arrayness of an operand through an operator. */
3017 switch (e->value.op.operator)
3019 case INTRINSIC_PLUS:
3020 case INTRINSIC_MINUS:
3021 case INTRINSIC_TIMES:
3022 case INTRINSIC_DIVIDE:
3023 case INTRINSIC_POWER:
3024 case INTRINSIC_CONCAT:
3028 case INTRINSIC_NEQV:
3030 case INTRINSIC_EQ_OS:
3032 case INTRINSIC_NE_OS:
3034 case INTRINSIC_GT_OS:
3036 case INTRINSIC_GE_OS:
3038 case INTRINSIC_LT_OS:
3040 case INTRINSIC_LE_OS:
3042 if (op1->rank == 0 && op2->rank == 0)
3045 if (op1->rank == 0 && op2->rank != 0)
3047 e->rank = op2->rank;
3049 if (e->shape == NULL)
3050 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3053 if (op1->rank != 0 && op2->rank == 0)
3055 e->rank = op1->rank;
3057 if (e->shape == NULL)
3058 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3061 if (op1->rank != 0 && op2->rank != 0)
3063 if (op1->rank == op2->rank)
3065 e->rank = op1->rank;
3066 if (e->shape == NULL)
3068 t = compare_shapes(op1, op2);
3072 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3077 /* Allow higher level expressions to work. */
3080 /* Try user-defined operators, and otherwise throw an error. */
3081 dual_locus_error = true;
3083 _("Inconsistent ranks for operator at %%L and %%L"));
3090 case INTRINSIC_PARENTHESES:
3092 case INTRINSIC_UPLUS:
3093 case INTRINSIC_UMINUS:
3094 /* Simply copy arrayness attribute */
3095 e->rank = op1->rank;
3097 if (e->shape == NULL)
3098 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3106 /* Attempt to simplify the expression. */
3109 t = gfc_simplify_expr (e, 0);
3110 /* Some calls do not succeed in simplification and return FAILURE
3111 even though there is no error; eg. variable references to
3112 PARAMETER arrays. */
3113 if (!gfc_is_constant_expr (e))
3120 if (gfc_extend_expr (e) == SUCCESS)
3123 if (dual_locus_error)
3124 gfc_error (msg, &op1->where, &op2->where);
3126 gfc_error (msg, &e->where);
3132 /************** Array resolution subroutines **************/
3135 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3138 /* Compare two integer expressions. */
3141 compare_bound (gfc_expr *a, gfc_expr *b)
3145 if (a == NULL || a->expr_type != EXPR_CONSTANT
3146 || b == NULL || b->expr_type != EXPR_CONSTANT)
3149 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3150 gfc_internal_error ("compare_bound(): Bad expression");
3152 i = mpz_cmp (a->value.integer, b->value.integer);
3162 /* Compare an integer expression with an integer. */
3165 compare_bound_int (gfc_expr *a, int b)
3169 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3172 if (a->ts.type != BT_INTEGER)
3173 gfc_internal_error ("compare_bound_int(): Bad expression");
3175 i = mpz_cmp_si (a->value.integer, b);
3185 /* Compare an integer expression with a mpz_t. */
3188 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3192 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3195 if (a->ts.type != BT_INTEGER)
3196 gfc_internal_error ("compare_bound_int(): Bad expression");
3198 i = mpz_cmp (a->value.integer, b);
3208 /* Compute the last value of a sequence given by a triplet.
3209 Return 0 if it wasn't able to compute the last value, or if the
3210 sequence if empty, and 1 otherwise. */
3213 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3214 gfc_expr *stride, mpz_t last)
3218 if (start == NULL || start->expr_type != EXPR_CONSTANT
3219 || end == NULL || end->expr_type != EXPR_CONSTANT
3220 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3223 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3224 || (stride != NULL && stride->ts.type != BT_INTEGER))
3227 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3229 if (compare_bound (start, end) == CMP_GT)
3231 mpz_set (last, end->value.integer);
3235 if (compare_bound_int (stride, 0) == CMP_GT)
3237 /* Stride is positive */
3238 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3243 /* Stride is negative */
3244 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3249 mpz_sub (rem, end->value.integer, start->value.integer);
3250 mpz_tdiv_r (rem, rem, stride->value.integer);
3251 mpz_sub (last, end->value.integer, rem);
3258 /* Compare a single dimension of an array reference to the array
3262 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3266 /* Given start, end and stride values, calculate the minimum and
3267 maximum referenced indexes. */
3269 switch (ar->dimen_type[i])
3275 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3277 gfc_warning ("Array reference at %L is out of bounds "
3278 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3279 mpz_get_si (ar->start[i]->value.integer),
3280 mpz_get_si (as->lower[i]->value.integer), i+1);
3283 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3285 gfc_warning ("Array reference at %L is out of bounds "
3286 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3287 mpz_get_si (ar->start[i]->value.integer),
3288 mpz_get_si (as->upper[i]->value.integer), i+1);
3296 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3297 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3299 comparison comp_start_end = compare_bound (AR_START, AR_END);
3301 /* Check for zero stride, which is not allowed. */
3302 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3304 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3308 /* if start == len || (stride > 0 && start < len)
3309 || (stride < 0 && start > len),
3310 then the array section contains at least one element. In this
3311 case, there is an out-of-bounds access if
3312 (start < lower || start > upper). */
3313 if (compare_bound (AR_START, AR_END) == CMP_EQ
3314 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3315 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3316 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3317 && comp_start_end == CMP_GT))
3319 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3321 gfc_warning ("Lower array reference at %L is out of bounds "
3322 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3323 mpz_get_si (AR_START->value.integer),
3324 mpz_get_si (as->lower[i]->value.integer), i+1);
3327 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3329 gfc_warning ("Lower array reference at %L is out of bounds "
3330 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3331 mpz_get_si (AR_START->value.integer),
3332 mpz_get_si (as->upper[i]->value.integer), i+1);
3337 /* If we can compute the highest index of the array section,
3338 then it also has to be between lower and upper. */
3339 mpz_init (last_value);
3340 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3343 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3345 gfc_warning ("Upper array reference at %L is out of bounds "
3346 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3347 mpz_get_si (last_value),
3348 mpz_get_si (as->lower[i]->value.integer), i+1);
3349 mpz_clear (last_value);
3352 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3354 gfc_warning ("Upper array reference at %L is out of bounds "
3355 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3356 mpz_get_si (last_value),
3357 mpz_get_si (as->upper[i]->value.integer), i+1);
3358 mpz_clear (last_value);
3362 mpz_clear (last_value);
3370 gfc_internal_error ("check_dimension(): Bad array reference");
3377 /* Compare an array reference with an array specification. */
3380 compare_spec_to_ref (gfc_array_ref *ar)
3387 /* TODO: Full array sections are only allowed as actual parameters. */
3388 if (as->type == AS_ASSUMED_SIZE
3389 && (/*ar->type == AR_FULL
3390 ||*/ (ar->type == AR_SECTION
3391 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3393 gfc_error ("Rightmost upper bound of assumed size array section "
3394 "not specified at %L", &ar->where);
3398 if (ar->type == AR_FULL)
3401 if (as->rank != ar->dimen)
3403 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3404 &ar->where, ar->dimen, as->rank);
3408 for (i = 0; i < as->rank; i++)
3409 if (check_dimension (i, ar, as) == FAILURE)
3416 /* Resolve one part of an array index. */
3419 gfc_resolve_index (gfc_expr *index, int check_scalar)
3426 if (gfc_resolve_expr (index) == FAILURE)
3429 if (check_scalar && index->rank != 0)
3431 gfc_error ("Array index at %L must be scalar", &index->where);
3435 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
3437 gfc_error ("Array index at %L must be of INTEGER type",
3442 if (index->ts.type == BT_REAL)
3443 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
3444 &index->where) == FAILURE)
3447 if (index->ts.kind != gfc_index_integer_kind
3448 || index->ts.type != BT_INTEGER)
3451 ts.type = BT_INTEGER;
3452 ts.kind = gfc_index_integer_kind;
3454 gfc_convert_type_warn (index, &ts, 2, 0);
3460 /* Resolve a dim argument to an intrinsic function. */
3463 gfc_resolve_dim_arg (gfc_expr *dim)
3468 if (gfc_resolve_expr (dim) == FAILURE)
3473 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3478 if (dim->ts.type != BT_INTEGER)
3480 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3484 if (dim->ts.kind != gfc_index_integer_kind)
3488 ts.type = BT_INTEGER;
3489 ts.kind = gfc_index_integer_kind;
3491 gfc_convert_type_warn (dim, &ts, 2, 0);
3497 /* Given an expression that contains array references, update those array
3498 references to point to the right array specifications. While this is
3499 filled in during matching, this information is difficult to save and load
3500 in a module, so we take care of it here.
3502 The idea here is that the original array reference comes from the
3503 base symbol. We traverse the list of reference structures, setting
3504 the stored reference to references. Component references can
3505 provide an additional array specification. */
3508 find_array_spec (gfc_expr *e)
3512 gfc_symbol *derived;
3515 as = e->symtree->n.sym->as;
3518 for (ref = e->ref; ref; ref = ref->next)
3523 gfc_internal_error ("find_array_spec(): Missing spec");
3530 if (derived == NULL)
3531 derived = e->symtree->n.sym->ts.derived;
3533 c = derived->components;
3535 for (; c; c = c->next)
3536 if (c == ref->u.c.component)
3538 /* Track the sequence of component references. */
3539 if (c->ts.type == BT_DERIVED)
3540 derived = c->ts.derived;
3545 gfc_internal_error ("find_array_spec(): Component not found");
3550 gfc_internal_error ("find_array_spec(): unused as(1)");
3561 gfc_internal_error ("find_array_spec(): unused as(2)");
3565 /* Resolve an array reference. */
3568 resolve_array_ref (gfc_array_ref *ar)
3570 int i, check_scalar;
3573 for (i = 0; i < ar->dimen; i++)
3575 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
3577 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
3579 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
3581 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
3586 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
3590 ar->dimen_type[i] = DIMEN_ELEMENT;
3594 ar->dimen_type[i] = DIMEN_VECTOR;
3595 if (e->expr_type == EXPR_VARIABLE
3596 && e->symtree->n.sym->ts.type == BT_DERIVED)
3597 ar->start[i] = gfc_get_parentheses (e);
3601 gfc_error ("Array index at %L is an array of rank %d",
3602 &ar->c_where[i], e->rank);
3607 /* If the reference type is unknown, figure out what kind it is. */
3609 if (ar->type == AR_UNKNOWN)
3611 ar->type = AR_ELEMENT;
3612 for (i = 0; i < ar->dimen; i++)
3613 if (ar->dimen_type[i] == DIMEN_RANGE
3614 || ar->dimen_type[i] == DIMEN_VECTOR)
3616 ar->type = AR_SECTION;
3621 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
3629 resolve_substring (gfc_ref *ref)
3631 if (ref->u.ss.start != NULL)
3633 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
3636 if (ref->u.ss.start->ts.type != BT_INTEGER)
3638 gfc_error ("Substring start index at %L must be of type INTEGER",
3639 &ref->u.ss.start->where);
3643 if (ref->u.ss.start->rank != 0)
3645 gfc_error ("Substring start index at %L must be scalar",
3646 &ref->u.ss.start->where);
3650 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
3651 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
3652 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
3654 gfc_error ("Substring start index at %L is less than one",
3655 &ref->u.ss.start->where);
3660 if (ref->u.ss.end != NULL)
3662 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
3665 if (ref->u.ss.end->ts.type != BT_INTEGER)
3667 gfc_error ("Substring end index at %L must be of type INTEGER",
3668 &ref->u.ss.end->where);
3672 if (ref->u.ss.end->rank != 0)
3674 gfc_error ("Substring end index at %L must be scalar",
3675 &ref->u.ss.end->where);
3679 if (ref->u.ss.length != NULL
3680 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
3681 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
3682 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
3684 gfc_error ("Substring end index at %L exceeds the string length",
3685 &ref->u.ss.start->where);
3694 /* This function supplies missing substring charlens. */
3697 gfc_resolve_substring_charlen (gfc_expr *e)
3700 gfc_expr *start, *end;
3702 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
3703 if (char_ref->type == REF_SUBSTRING)
3709 gcc_assert (char_ref->next == NULL);
3713 if (e->ts.cl->length)
3714 gfc_free_expr (e->ts.cl->length);
3715 else if (e->expr_type == EXPR_VARIABLE
3716 && e->symtree->n.sym->attr.dummy)
3720 e->ts.type = BT_CHARACTER;
3721 e->ts.kind = gfc_default_character_kind;
3725 e->ts.cl = gfc_get_charlen ();
3726 e->ts.cl->next = gfc_current_ns->cl_list;
3727 gfc_current_ns->cl_list = e->ts.cl;
3730 if (char_ref->u.ss.start)
3731 start = gfc_copy_expr (char_ref->u.ss.start);
3733 start = gfc_int_expr (1);
3735 if (char_ref->u.ss.end)
3736 end = gfc_copy_expr (char_ref->u.ss.end);
3737 else if (e->expr_type == EXPR_VARIABLE)
3738 end = gfc_copy_expr (e->symtree->n.sym->ts.cl->length);
3745 /* Length = (end - start +1). */
3746 e->ts.cl->length = gfc_subtract (end, start);
3747 e->ts.cl->length = gfc_add (e->ts.cl->length, gfc_int_expr (1));
3749 e->ts.cl->length->ts.type = BT_INTEGER;
3750 e->ts.cl->length->ts.kind = gfc_charlen_int_kind;;
3752 /* Make sure that the length is simplified. */
3753 gfc_simplify_expr (e->ts.cl->length, 1);
3754 gfc_resolve_expr (e->ts.cl->length);
3758 /* Resolve subtype references. */
3761 resolve_ref (gfc_expr *expr)
3763 int current_part_dimension, n_components, seen_part_dimension;
3766 for (ref = expr->ref; ref; ref = ref->next)
3767 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
3769 find_array_spec (expr);
3773 for (ref = expr->ref; ref; ref = ref->next)
3777 if (resolve_array_ref (&ref->u.ar) == FAILURE)
3785 resolve_substring (ref);
3789 /* Check constraints on part references. */
3791 current_part_dimension = 0;
3792 seen_part_dimension = 0;
3795 for (ref = expr->ref; ref; ref = ref->next)
3800 switch (ref->u.ar.type)
3804 current_part_dimension = 1;
3808 current_part_dimension = 0;
3812 gfc_internal_error ("resolve_ref(): Bad array reference");
3818 if (current_part_dimension || seen_part_dimension)
3820 if (ref->u.c.component->pointer)
3822 gfc_error ("Component to the right of a part reference "
3823 "with nonzero rank must not have the POINTER "
3824 "attribute at %L", &expr->where);
3827 else if (ref->u.c.component->allocatable)
3829 gfc_error ("Component to the right of a part reference "
3830 "with nonzero rank must not have the ALLOCATABLE "
3831 "attribute at %L", &expr->where);
3843 if (((ref->type == REF_COMPONENT && n_components > 1)
3844 || ref->next == NULL)
3845 && current_part_dimension
3846 && seen_part_dimension)
3848 gfc_error ("Two or more part references with nonzero rank must "
3849 "not be specified at %L", &expr->where);
3853 if (ref->type == REF_COMPONENT)
3855 if (current_part_dimension)
3856 seen_part_dimension = 1;
3858 /* reset to make sure */
3859 current_part_dimension = 0;
3867 /* Given an expression, determine its shape. This is easier than it sounds.
3868 Leaves the shape array NULL if it is not possible to determine the shape. */
3871 expression_shape (gfc_expr *e)
3873 mpz_t array[GFC_MAX_DIMENSIONS];
3876 if (e->rank == 0 || e->shape != NULL)
3879 for (i = 0; i < e->rank; i++)
3880 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
3883 e->shape = gfc_get_shape (e->rank);
3885 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
3890 for (i--; i >= 0; i--)
3891 mpz_clear (array[i]);
3895 /* Given a variable expression node, compute the rank of the expression by
3896 examining the base symbol and any reference structures it may have. */
3899 expression_rank (gfc_expr *e)
3906 if (e->expr_type == EXPR_ARRAY)
3908 /* Constructors can have a rank different from one via RESHAPE(). */
3910 if (e->symtree == NULL)
3916 e->rank = (e->symtree->n.sym->as == NULL)
3917 ? 0 : e->symtree->n.sym->as->rank;
3923 for (ref = e->ref; ref; ref = ref->next)
3925 if (ref->type != REF_ARRAY)
3928 if (ref->u.ar.type == AR_FULL)
3930 rank = ref->u.ar.as->rank;
3934 if (ref->u.ar.type == AR_SECTION)
3936 /* Figure out the rank of the section. */
3938 gfc_internal_error ("expression_rank(): Two array specs");
3940 for (i = 0; i < ref->u.ar.dimen; i++)
3941 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
3942 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
3952 expression_shape (e);
3956 /* Resolve a variable expression. */
3959 resolve_variable (gfc_expr *e)
3966 if (e->symtree == NULL)
3969 if (e->ref && resolve_ref (e) == FAILURE)
3972 sym = e->symtree->n.sym;
3973 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
3975 e->ts.type = BT_PROCEDURE;
3979 if (sym->ts.type != BT_UNKNOWN)
3980 gfc_variable_attr (e, &e->ts);
3983 /* Must be a simple variable reference. */
3984 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
3989 if (check_assumed_size_reference (sym, e))
3992 /* Deal with forward references to entries during resolve_code, to
3993 satisfy, at least partially, 12.5.2.5. */
3994 if (gfc_current_ns->entries
3995 && current_entry_id == sym->entry_id
3998 && cs_base->current->op != EXEC_ENTRY)
4000 gfc_entry_list *entry;
4001 gfc_formal_arglist *formal;
4005 /* If the symbol is a dummy... */
4006 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4008 entry = gfc_current_ns->entries;
4011 /* ...test if the symbol is a parameter of previous entries. */
4012 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4013 for (formal = entry->sym->formal; formal; formal = formal->next)
4015 if (formal->sym && sym->name == formal->sym->name)
4019 /* If it has not been seen as a dummy, this is an error. */
4022 if (specification_expr)
4023 gfc_error ("Variable '%s', used in a specification expression"
4024 ", is referenced at %L before the ENTRY statement "
4025 "in which it is a parameter",
4026 sym->name, &cs_base->current->loc);
4028 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4029 "statement in which it is a parameter",
4030 sym->name, &cs_base->current->loc);
4035 /* Now do the same check on the specification expressions. */
4036 specification_expr = 1;
4037 if (sym->ts.type == BT_CHARACTER
4038 && gfc_resolve_expr (sym->ts.cl->length) == FAILURE)
4042 for (n = 0; n < sym->as->rank; n++)
4044 specification_expr = 1;
4045 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4047 specification_expr = 1;
4048 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4051 specification_expr = 0;
4054 /* Update the symbol's entry level. */
4055 sym->entry_id = current_entry_id + 1;
4062 /* Checks to see that the correct symbol has been host associated.
4063 The only situation where this arises is that in which a twice
4064 contained function is parsed after the host association is made.
4065 Therefore, on detecting this, the line is rematched, having got
4066 rid of the existing references and actual_arg_list. */
4068 check_host_association (gfc_expr *e)
4070 gfc_symbol *sym, *old_sym;
4074 bool retval = e->expr_type == EXPR_FUNCTION;
4076 if (e->symtree == NULL || e->symtree->n.sym == NULL)
4079 old_sym = e->symtree->n.sym;
4081 if (old_sym->attr.use_assoc)
4084 if (gfc_current_ns->parent
4085 && old_sym->ns != gfc_current_ns)
4087 gfc_find_symbol (old_sym->name, gfc_current_ns, 1, &sym);
4088 if (sym && old_sym != sym
4089 && sym->attr.flavor == FL_PROCEDURE
4090 && sym->attr.contained)
4092 temp_locus = gfc_current_locus;
4093 gfc_current_locus = e->where;
4095 gfc_buffer_error (1);
4097 gfc_free_ref_list (e->ref);
4102 gfc_free_actual_arglist (e->value.function.actual);
4103 e->value.function.actual = NULL;
4106 if (e->shape != NULL)
4108 for (n = 0; n < e->rank; n++)
4109 mpz_clear (e->shape[n]);
4111 gfc_free (e->shape);
4114 gfc_match_rvalue (&expr);
4116 gfc_buffer_error (0);
4118 gcc_assert (expr && sym == expr->symtree->n.sym);
4124 gfc_current_locus = temp_locus;
4127 /* This might have changed! */
4128 return e->expr_type == EXPR_FUNCTION;
4133 gfc_resolve_character_operator (gfc_expr *e)
4135 gfc_expr *op1 = e->value.op.op1;
4136 gfc_expr *op2 = e->value.op.op2;
4137 gfc_expr *e1 = NULL;
4138 gfc_expr *e2 = NULL;
4140 gcc_assert (e->value.op.operator == INTRINSIC_CONCAT);
4142 if (op1->ts.cl && op1->ts.cl->length)
4143 e1 = gfc_copy_expr (op1->ts.cl->length);
4144 else if (op1->expr_type == EXPR_CONSTANT)
4145 e1 = gfc_int_expr (op1->value.character.length);
4147 if (op2->ts.cl && op2->ts.cl->length)
4148 e2 = gfc_copy_expr (op2->ts.cl->length);
4149 else if (op2->expr_type == EXPR_CONSTANT)
4150 e2 = gfc_int_expr (op2->value.character.length);
4152 e->ts.cl = gfc_get_charlen ();
4153 e->ts.cl->next = gfc_current_ns->cl_list;
4154 gfc_current_ns->cl_list = e->ts.cl;
4159 e->ts.cl->length = gfc_add (e1, e2);
4160 e->ts.cl->length->ts.type = BT_INTEGER;
4161 e->ts.cl->length->ts.kind = gfc_charlen_int_kind;;
4162 gfc_simplify_expr (e->ts.cl->length, 0);
4163 gfc_resolve_expr (e->ts.cl->length);
4169 /* Ensure that an character expression has a charlen and, if possible, a
4170 length expression. */
4173 fixup_charlen (gfc_expr *e)
4175 /* The cases fall through so that changes in expression type and the need
4176 for multiple fixes are picked up. In all circumstances, a charlen should
4177 be available for the middle end to hang a backend_decl on. */
4178 switch (e->expr_type)
4181 gfc_resolve_character_operator (e);
4184 if (e->expr_type == EXPR_ARRAY)
4185 gfc_resolve_character_array_constructor (e);
4187 case EXPR_SUBSTRING:
4188 if (!e->ts.cl && e->ref)
4189 gfc_resolve_substring_charlen (e);
4194 e->ts.cl = gfc_get_charlen ();
4195 e->ts.cl->next = gfc_current_ns->cl_list;
4196 gfc_current_ns->cl_list = e->ts.cl;
4204 /* Resolve an expression. That is, make sure that types of operands agree
4205 with their operators, intrinsic operators are converted to function calls
4206 for overloaded types and unresolved function references are resolved. */
4209 gfc_resolve_expr (gfc_expr *e)
4216 switch (e->expr_type)
4219 t = resolve_operator (e);
4225 if (check_host_association (e))
4226 t = resolve_function (e);
4229 t = resolve_variable (e);
4231 expression_rank (e);
4234 if (e->ts.type == BT_CHARACTER && e->ts.cl == NULL && e->ref
4235 && e->ref->type != REF_SUBSTRING)
4236 gfc_resolve_substring_charlen (e);
4240 case EXPR_SUBSTRING:
4241 t = resolve_ref (e);
4251 if (resolve_ref (e) == FAILURE)
4254 t = gfc_resolve_array_constructor (e);
4255 /* Also try to expand a constructor. */
4258 expression_rank (e);
4259 gfc_expand_constructor (e);
4262 /* This provides the opportunity for the length of constructors with
4263 character valued function elements to propagate the string length
4264 to the expression. */
4265 if (e->ts.type == BT_CHARACTER)
4266 gfc_resolve_character_array_constructor (e);
4270 case EXPR_STRUCTURE:
4271 t = resolve_ref (e);
4275 t = resolve_structure_cons (e);
4279 t = gfc_simplify_expr (e, 0);
4283 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
4286 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.cl)
4293 /* Resolve an expression from an iterator. They must be scalar and have
4294 INTEGER or (optionally) REAL type. */
4297 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
4298 const char *name_msgid)
4300 if (gfc_resolve_expr (expr) == FAILURE)
4303 if (expr->rank != 0)
4305 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
4309 if (expr->ts.type != BT_INTEGER)
4311 if (expr->ts.type == BT_REAL)
4314 return gfc_notify_std (GFC_STD_F95_DEL,
4315 "Deleted feature: %s at %L must be integer",
4316 _(name_msgid), &expr->where);
4319 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
4326 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
4334 /* Resolve the expressions in an iterator structure. If REAL_OK is
4335 false allow only INTEGER type iterators, otherwise allow REAL types. */
4338 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
4340 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
4344 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
4346 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
4351 if (gfc_resolve_iterator_expr (iter->start, real_ok,
4352 "Start expression in DO loop") == FAILURE)
4355 if (gfc_resolve_iterator_expr (iter->end, real_ok,
4356 "End expression in DO loop") == FAILURE)
4359 if (gfc_resolve_iterator_expr (iter->step, real_ok,
4360 "Step expression in DO loop") == FAILURE)
4363 if (iter->step->expr_type == EXPR_CONSTANT)
4365 if ((iter->step->ts.type == BT_INTEGER
4366 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
4367 || (iter->step->ts.type == BT_REAL
4368 && mpfr_sgn (iter->step->value.real) == 0))
4370 gfc_error ("Step expression in DO loop at %L cannot be zero",
4371 &iter->step->where);
4376 /* Convert start, end, and step to the same type as var. */
4377 if (iter->start->ts.kind != iter->var->ts.kind
4378 || iter->start->ts.type != iter->var->ts.type)
4379 gfc_convert_type (iter->start, &iter->var->ts, 2);
4381 if (iter->end->ts.kind != iter->var->ts.kind
4382 || iter->end->ts.type != iter->var->ts.type)
4383 gfc_convert_type (iter->end, &iter->var->ts, 2);
4385 if (iter->step->ts.kind != iter->var->ts.kind
4386 || iter->step->ts.type != iter->var->ts.type)
4387 gfc_convert_type (iter->step, &iter->var->ts, 2);
4393 /* Traversal function for find_forall_index. f == 2 signals that
4394 that variable itself is not to be checked - only the references. */
4397 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
4399 if (expr->expr_type != EXPR_VARIABLE)
4402 /* A scalar assignment */
4403 if (!expr->ref || *f == 1)
4405 if (expr->symtree->n.sym == sym)
4417 /* Check whether the FORALL index appears in the expression or not.
4418 Returns SUCCESS if SYM is found in EXPR. */
4421 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
4423 if (gfc_traverse_expr (expr, sym, forall_index, f))
4430 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
4431 to be a scalar INTEGER variable. The subscripts and stride are scalar
4432 INTEGERs, and if stride is a constant it must be nonzero.
4433 Furthermore "A subscript or stride in a forall-triplet-spec shall
4434 not contain a reference to any index-name in the
4435 forall-triplet-spec-list in which it appears." (7.5.4.1) */
4438 resolve_forall_iterators (gfc_forall_iterator *it)
4440 gfc_forall_iterator *iter, *iter2;
4442 for (iter = it; iter; iter = iter->next)
4444 if (gfc_resolve_expr (iter->var) == SUCCESS
4445 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
4446 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
4449 if (gfc_resolve_expr (iter->start) == SUCCESS
4450 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
4451 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
4452 &iter->start->where);
4453 if (iter->var->ts.kind != iter->start->ts.kind)
4454 gfc_convert_type (iter->start, &iter->var->ts, 2);
4456 if (gfc_resolve_expr (iter->end) == SUCCESS
4457 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
4458 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
4460 if (iter->var->ts.kind != iter->end->ts.kind)
4461 gfc_convert_type (iter->end, &iter->var->ts, 2);
4463 if (gfc_resolve_expr (iter->stride) == SUCCESS)
4465 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
4466 gfc_error ("FORALL stride expression at %L must be a scalar %s",
4467 &iter->stride->where, "INTEGER");
4469 if (iter->stride->expr_type == EXPR_CONSTANT
4470 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
4471 gfc_error ("FORALL stride expression at %L cannot be zero",
4472 &iter->stride->where);
4474 if (iter->var->ts.kind != iter->stride->ts.kind)
4475 gfc_convert_type (iter->stride, &iter->var->ts, 2);
4478 for (iter = it; iter; iter = iter->next)
4479 for (iter2 = iter; iter2; iter2 = iter2->next)
4481 if (find_forall_index (iter2->start,
4482 iter->var->symtree->n.sym, 0) == SUCCESS
4483 || find_forall_index (iter2->end,
4484 iter->var->symtree->n.sym, 0) == SUCCESS
4485 || find_forall_index (iter2->stride,
4486 iter->var->symtree->n.sym, 0) == SUCCESS)
4487 gfc_error ("FORALL index '%s' may not appear in triplet "
4488 "specification at %L", iter->var->symtree->name,
4489 &iter2->start->where);
4494 /* Given a pointer to a symbol that is a derived type, see if it's
4495 inaccessible, i.e. if it's defined in another module and the components are
4496 PRIVATE. The search is recursive if necessary. Returns zero if no
4497 inaccessible components are found, nonzero otherwise. */
4500 derived_inaccessible (gfc_symbol *sym)
4504 if (sym->attr.use_assoc && sym->attr.private_comp)
4507 for (c = sym->components; c; c = c->next)
4509 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.derived))
4517 /* Resolve the argument of a deallocate expression. The expression must be
4518 a pointer or a full array. */
4521 resolve_deallocate_expr (gfc_expr *e)
4523 symbol_attribute attr;
4524 int allocatable, pointer, check_intent_in;
4527 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
4528 check_intent_in = 1;
4530 if (gfc_resolve_expr (e) == FAILURE)
4533 if (e->expr_type != EXPR_VARIABLE)
4536 allocatable = e->symtree->n.sym->attr.allocatable;
4537 pointer = e->symtree->n.sym->attr.pointer;
4538 for (ref = e->ref; ref; ref = ref->next)
4541 check_intent_in = 0;
4546 if (ref->u.ar.type != AR_FULL)
4551 allocatable = (ref->u.c.component->as != NULL
4552 && ref->u.c.component->as->type == AS_DEFERRED);
4553 pointer = ref->u.c.component->pointer;
4562 attr = gfc_expr_attr (e);
4564 if (allocatable == 0 && attr.pointer == 0)
4567 gfc_error ("Expression in DEALLOCATE statement at %L must be "
4568 "ALLOCATABLE or a POINTER", &e->where);
4572 && e->symtree->n.sym->attr.intent == INTENT_IN)
4574 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
4575 e->symtree->n.sym->name, &e->where);
4583 /* Returns true if the expression e contains a reference to the symbol sym. */
4585 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
4587 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
4594 find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
4596 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
4600 /* Given the expression node e for an allocatable/pointer of derived type to be
4601 allocated, get the expression node to be initialized afterwards (needed for
4602 derived types with default initializers, and derived types with allocatable
4603 components that need nullification.) */
4606 expr_to_initialize (gfc_expr *e)
4612 result = gfc_copy_expr (e);
4614 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
4615 for (ref = result->ref; ref; ref = ref->next)
4616 if (ref->type == REF_ARRAY && ref->next == NULL)
4618 ref->u.ar.type = AR_FULL;
4620 for (i = 0; i < ref->u.ar.dimen; i++)
4621 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
4623 result->rank = ref->u.ar.dimen;
4631 /* Resolve the expression in an ALLOCATE statement, doing the additional
4632 checks to see whether the expression is OK or not. The expression must
4633 have a trailing array reference that gives the size of the array. */
4636 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
4638 int i, pointer, allocatable, dimension, check_intent_in;
4639 symbol_attribute attr;
4640 gfc_ref *ref, *ref2;
4647 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
4648 check_intent_in = 1;
4650 if (gfc_resolve_expr (e) == FAILURE)
4653 if (code->expr && code->expr->expr_type == EXPR_VARIABLE)
4654 sym = code->expr->symtree->n.sym;
4658 /* Make sure the expression is allocatable or a pointer. If it is
4659 pointer, the next-to-last reference must be a pointer. */
4663 if (e->expr_type != EXPR_VARIABLE)
4666 attr = gfc_expr_attr (e);
4667 pointer = attr.pointer;
4668 dimension = attr.dimension;
4672 allocatable = e->symtree->n.sym->attr.allocatable;
4673 pointer = e->symtree->n.sym->attr.pointer;
4674 dimension = e->symtree->n.sym->attr.dimension;
4676 if (sym == e->symtree->n.sym && sym->ts.type != BT_DERIVED)
4678 gfc_error ("The STAT variable '%s' in an ALLOCATE statement must "
4679 "not be allocated in the same statement at %L",
4680 sym->name, &e->where);
4684 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
4687 check_intent_in = 0;
4692 if (ref->next != NULL)
4697 allocatable = (ref->u.c.component->as != NULL
4698 && ref->u.c.component->as->type == AS_DEFERRED);
4700 pointer = ref->u.c.component->pointer;
4701 dimension = ref->u.c.component->dimension;
4712 if (allocatable == 0 && pointer == 0)
4714 gfc_error ("Expression in ALLOCATE statement at %L must be "
4715 "ALLOCATABLE or a POINTER", &e->where);
4720 && e->symtree->n.sym->attr.intent == INTENT_IN)
4722 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
4723 e->symtree->n.sym->name, &e->where);
4727 /* Add default initializer for those derived types that need them. */
4728 if (e->ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&e->ts)))
4730 init_st = gfc_get_code ();
4731 init_st->loc = code->loc;
4732 init_st->op = EXEC_INIT_ASSIGN;
4733 init_st->expr = expr_to_initialize (e);
4734 init_st->expr2 = init_e;
4735 init_st->next = code->next;
4736 code->next = init_st;
4739 if (pointer && dimension == 0)
4742 /* Make sure the next-to-last reference node is an array specification. */
4744 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
4746 gfc_error ("Array specification required in ALLOCATE statement "
4747 "at %L", &e->where);
4751 /* Make sure that the array section reference makes sense in the
4752 context of an ALLOCATE specification. */
4756 for (i = 0; i < ar->dimen; i++)
4758 if (ref2->u.ar.type == AR_ELEMENT)
4761 switch (ar->dimen_type[i])
4767 if (ar->start[i] != NULL
4768 && ar->end[i] != NULL
4769 && ar->stride[i] == NULL)
4772 /* Fall Through... */
4776 gfc_error ("Bad array specification in ALLOCATE statement at %L",
4783 for (a = code->ext.alloc_list; a; a = a->next)
4785 sym = a->expr->symtree->n.sym;
4787 /* TODO - check derived type components. */
4788 if (sym->ts.type == BT_DERIVED)
4791 if ((ar->start[i] != NULL && find_sym_in_expr (sym, ar->start[i]))
4792 || (ar->end[i] != NULL && find_sym_in_expr (sym, ar->end[i])))
4794 gfc_error ("'%s' must not appear an the array specification at "
4795 "%L in the same ALLOCATE statement where it is "
4796 "itself allocated", sym->name, &ar->where);
4806 /************ SELECT CASE resolution subroutines ************/
4808 /* Callback function for our mergesort variant. Determines interval
4809 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
4810 op1 > op2. Assumes we're not dealing with the default case.
4811 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
4812 There are nine situations to check. */
4815 compare_cases (const gfc_case *op1, const gfc_case *op2)
4819 if (op1->low == NULL) /* op1 = (:L) */
4821 /* op2 = (:N), so overlap. */
4823 /* op2 = (M:) or (M:N), L < M */
4824 if (op2->low != NULL
4825 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
4828 else if (op1->high == NULL) /* op1 = (K:) */
4830 /* op2 = (M:), so overlap. */
4832 /* op2 = (:N) or (M:N), K > N */
4833 if (op2->high != NULL
4834 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
4837 else /* op1 = (K:L) */
4839 if (op2->low == NULL) /* op2 = (:N), K > N */
4840 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
4842 else if (op2->high == NULL) /* op2 = (M:), L < M */
4843 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
4845 else /* op2 = (M:N) */
4849 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
4852 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
4861 /* Merge-sort a double linked case list, detecting overlap in the
4862 process. LIST is the head of the double linked case list before it
4863 is sorted. Returns the head of the sorted list if we don't see any
4864 overlap, or NULL otherwise. */
4867 check_case_overlap (gfc_case *list)
4869 gfc_case *p, *q, *e, *tail;
4870 int insize, nmerges, psize, qsize, cmp, overlap_seen;
4872 /* If the passed list was empty, return immediately. */
4879 /* Loop unconditionally. The only exit from this loop is a return
4880 statement, when we've finished sorting the case list. */
4887 /* Count the number of merges we do in this pass. */
4890 /* Loop while there exists a merge to be done. */
4895 /* Count this merge. */
4898 /* Cut the list in two pieces by stepping INSIZE places
4899 forward in the list, starting from P. */
4902 for (i = 0; i < insize; i++)
4911 /* Now we have two lists. Merge them! */
4912 while (psize > 0 || (qsize > 0 && q != NULL))
4914 /* See from which the next case to merge comes from. */
4917 /* P is empty so the next case must come from Q. */
4922 else if (qsize == 0 || q == NULL)
4931 cmp = compare_cases (p, q);
4934 /* The whole case range for P is less than the
4942 /* The whole case range for Q is greater than
4943 the case range for P. */
4950 /* The cases overlap, or they are the same
4951 element in the list. Either way, we must
4952 issue an error and get the next case from P. */
4953 /* FIXME: Sort P and Q by line number. */
4954 gfc_error ("CASE label at %L overlaps with CASE "
4955 "label at %L", &p->where, &q->where);
4963 /* Add the next element to the merged list. */
4972 /* P has now stepped INSIZE places along, and so has Q. So
4973 they're the same. */
4978 /* If we have done only one merge or none at all, we've
4979 finished sorting the cases. */
4988 /* Otherwise repeat, merging lists twice the size. */
4994 /* Check to see if an expression is suitable for use in a CASE statement.
4995 Makes sure that all case expressions are scalar constants of the same
4996 type. Return FAILURE if anything is wrong. */
4999 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
5001 if (e == NULL) return SUCCESS;
5003 if (e->ts.type != case_expr->ts.type)
5005 gfc_error ("Expression in CASE statement at %L must be of type %s",
5006 &e->where, gfc_basic_typename (case_expr->ts.type));
5010 /* C805 (R808) For a given case-construct, each case-value shall be of
5011 the same type as case-expr. For character type, length differences
5012 are allowed, but the kind type parameters shall be the same. */
5014 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
5016 gfc_error("Expression in CASE statement at %L must be kind %d",
5017 &e->where, case_expr->ts.kind);
5021 /* Convert the case value kind to that of case expression kind, if needed.
5022 FIXME: Should a warning be issued? */
5023 if (e->ts.kind != case_expr->ts.kind)
5024 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
5028 gfc_error ("Expression in CASE statement at %L must be scalar",
5037 /* Given a completely parsed select statement, we:
5039 - Validate all expressions and code within the SELECT.
5040 - Make sure that the selection expression is not of the wrong type.
5041 - Make sure that no case ranges overlap.
5042 - Eliminate unreachable cases and unreachable code resulting from
5043 removing case labels.
5045 The standard does allow unreachable cases, e.g. CASE (5:3). But
5046 they are a hassle for code generation, and to prevent that, we just
5047 cut them out here. This is not necessary for overlapping cases
5048 because they are illegal and we never even try to generate code.
5050 We have the additional caveat that a SELECT construct could have
5051 been a computed GOTO in the source code. Fortunately we can fairly
5052 easily work around that here: The case_expr for a "real" SELECT CASE
5053 is in code->expr1, but for a computed GOTO it is in code->expr2. All
5054 we have to do is make sure that the case_expr is a scalar integer
5058 resolve_select (gfc_code *code)
5061 gfc_expr *case_expr;
5062 gfc_case *cp, *default_case, *tail, *head;
5063 int seen_unreachable;
5069 if (code->expr == NULL)
5071 /* This was actually a computed GOTO statement. */
5072 case_expr = code->expr2;
5073 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
5074 gfc_error ("Selection expression in computed GOTO statement "
5075 "at %L must be a scalar integer expression",
5078 /* Further checking is not necessary because this SELECT was built
5079 by the compiler, so it should always be OK. Just move the
5080 case_expr from expr2 to expr so that we can handle computed
5081 GOTOs as normal SELECTs from here on. */
5082 code->expr = code->expr2;
5087 case_expr = code->expr;
5089 type = case_expr->ts.type;
5090 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
5092 gfc_error ("Argument of SELECT statement at %L cannot be %s",
5093 &case_expr->where, gfc_typename (&case_expr->ts));
5095 /* Punt. Going on here just produce more garbage error messages. */
5099 if (case_expr->rank != 0)
5101 gfc_error ("Argument of SELECT statement at %L must be a scalar "
5102 "expression", &case_expr->where);
5108 /* PR 19168 has a long discussion concerning a mismatch of the kinds
5109 of the SELECT CASE expression and its CASE values. Walk the lists
5110 of case values, and if we find a mismatch, promote case_expr to
5111 the appropriate kind. */
5113 if (type == BT_LOGICAL || type == BT_INTEGER)
5115 for (body = code->block; body; body = body->block)
5117 /* Walk the case label list. */
5118 for (cp = body->ext.case_list; cp; cp = cp->next)
5120 /* Intercept the DEFAULT case. It does not have a kind. */
5121 if (cp->low == NULL && cp->high == NULL)
5124 /* Unreachable case ranges are discarded, so ignore. */
5125 if (cp->low != NULL && cp->high != NULL
5126 && cp->low != cp->high
5127 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5130 /* FIXME: Should a warning be issued? */
5132 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
5133 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
5135 if (cp->high != NULL
5136 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
5137 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
5142 /* Assume there is no DEFAULT case. */
5143 default_case = NULL;
5148 for (body = code->block; body; body = body->block)
5150 /* Assume the CASE list is OK, and all CASE labels can be matched. */
5152 seen_unreachable = 0;
5154 /* Walk the case label list, making sure that all case labels
5156 for (cp = body->ext.case_list; cp; cp = cp->next)
5158 /* Count the number of cases in the whole construct. */
5161 /* Intercept the DEFAULT case. */
5162 if (cp->low == NULL && cp->high == NULL)
5164 if (default_case != NULL)
5166 gfc_error ("The DEFAULT CASE at %L cannot be followed "
5167 "by a second DEFAULT CASE at %L",
5168 &default_case->where, &cp->where);
5179 /* Deal with single value cases and case ranges. Errors are
5180 issued from the validation function. */
5181 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
5182 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
5188 if (type == BT_LOGICAL
5189 && ((cp->low == NULL || cp->high == NULL)
5190 || cp->low != cp->high))
5192 gfc_error ("Logical range in CASE statement at %L is not "
5193 "allowed", &cp->low->where);
5198 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
5201 value = cp->low->value.logical == 0 ? 2 : 1;
5202 if (value & seen_logical)
5204 gfc_error ("constant logical value in CASE statement "
5205 "is repeated at %L",
5210 seen_logical |= value;
5213 if (cp->low != NULL && cp->high != NULL
5214 && cp->low != cp->high
5215 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5217 if (gfc_option.warn_surprising)
5218 gfc_warning ("Range specification at %L can never "
5219 "be matched", &cp->where);
5221 cp->unreachable = 1;
5222 seen_unreachable = 1;
5226 /* If the case range can be matched, it can also overlap with
5227 other cases. To make sure it does not, we put it in a
5228 double linked list here. We sort that with a merge sort
5229 later on to detect any overlapping cases. */
5233 head->right = head->left = NULL;
5238 tail->right->left = tail;
5245 /* It there was a failure in the previous case label, give up
5246 for this case label list. Continue with the next block. */
5250 /* See if any case labels that are unreachable have been seen.
5251 If so, we eliminate them. This is a bit of a kludge because
5252 the case lists for a single case statement (label) is a
5253 single forward linked lists. */
5254 if (seen_unreachable)
5256 /* Advance until the first case in the list is reachable. */
5257 while (body->ext.case_list != NULL
5258 && body->ext.case_list->unreachable)
5260 gfc_case *n = body->ext.case_list;
5261 body->ext.case_list = body->ext.case_list->next;
5263 gfc_free_case_list (n);
5266 /* Strip all other unreachable cases. */
5267 if (body->ext.case_list)
5269 for (cp = body->ext.case_list; cp->next; cp = cp->next)
5271 if (cp->next->unreachable)
5273 gfc_case *n = cp->next;
5274 cp->next = cp->next->next;
5276 gfc_free_case_list (n);
5283 /* See if there were overlapping cases. If the check returns NULL,
5284 there was overlap. In that case we don't do anything. If head
5285 is non-NULL, we prepend the DEFAULT case. The sorted list can
5286 then used during code generation for SELECT CASE constructs with
5287 a case expression of a CHARACTER type. */
5290 head = check_case_overlap (head);
5292 /* Prepend the default_case if it is there. */
5293 if (head != NULL && default_case)
5295 default_case->left = NULL;
5296 default_case->right = head;
5297 head->left = default_case;
5301 /* Eliminate dead blocks that may be the result if we've seen
5302 unreachable case labels for a block. */
5303 for (body = code; body && body->block; body = body->block)
5305 if (body->block->ext.case_list == NULL)
5307 /* Cut the unreachable block from the code chain. */
5308 gfc_code *c = body->block;
5309 body->block = c->block;
5311 /* Kill the dead block, but not the blocks below it. */
5313 gfc_free_statements (c);
5317 /* More than two cases is legal but insane for logical selects.
5318 Issue a warning for it. */
5319 if (gfc_option.warn_surprising && type == BT_LOGICAL
5321 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
5326 /* Resolve a transfer statement. This is making sure that:
5327 -- a derived type being transferred has only non-pointer components
5328 -- a derived type being transferred doesn't have private components, unless
5329 it's being transferred from the module where the type was defined
5330 -- we're not trying to transfer a whole assumed size array. */
5333 resolve_transfer (gfc_code *code)
5342 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
5345 sym = exp->symtree->n.sym;
5348 /* Go to actual component transferred. */
5349 for (ref = code->expr->ref; ref; ref = ref->next)
5350 if (ref->type == REF_COMPONENT)
5351 ts = &ref->u.c.component->ts;
5353 if (ts->type == BT_DERIVED)
5355 /* Check that transferred derived type doesn't contain POINTER
5357 if (ts->derived->attr.pointer_comp)
5359 gfc_error ("Data transfer element at %L cannot have "
5360 "POINTER components", &code->loc);
5364 if (ts->derived->attr.alloc_comp)
5366 gfc_error ("Data transfer element at %L cannot have "
5367 "ALLOCATABLE components", &code->loc);
5371 if (derived_inaccessible (ts->derived))
5373 gfc_error ("Data transfer element at %L cannot have "
5374 "PRIVATE components",&code->loc);
5379 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
5380 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
5382 gfc_error ("Data transfer element at %L cannot be a full reference to "
5383 "an assumed-size array", &code->loc);
5389 /*********** Toplevel code resolution subroutines ***********/
5391 /* Find the set of labels that are reachable from this block. We also
5392 record the last statement in each block so that we don't have to do
5393 a linear search to find the END DO statements of the blocks. */
5396 reachable_labels (gfc_code *block)
5403 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
5405 /* Collect labels in this block. */
5406 for (c = block; c; c = c->next)
5409 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
5411 if (!c->next && cs_base->prev)
5412 cs_base->prev->tail = c;
5415 /* Merge with labels from parent block. */
5418 gcc_assert (cs_base->prev->reachable_labels);
5419 bitmap_ior_into (cs_base->reachable_labels,
5420 cs_base->prev->reachable_labels);
5424 /* Given a branch to a label and a namespace, if the branch is conforming.
5425 The code node describes where the branch is located. */
5428 resolve_branch (gfc_st_label *label, gfc_code *code)
5435 /* Step one: is this a valid branching target? */
5437 if (label->defined == ST_LABEL_UNKNOWN)
5439 gfc_error ("Label %d referenced at %L is never defined", label->value,
5444 if (label->defined != ST_LABEL_TARGET)
5446 gfc_error ("Statement at %L is not a valid branch target statement "
5447 "for the branch statement at %L", &label->where, &code->loc);
5451 /* Step two: make sure this branch is not a branch to itself ;-) */
5453 if (code->here == label)
5455 gfc_warning ("Branch at %L causes an infinite loop", &code->loc);
5459 /* Step three: See if the label is in the same block as the
5460 branching statement. The hard work has been done by setting up
5461 the bitmap reachable_labels. */
5463 if (!bitmap_bit_p (cs_base->reachable_labels, label->value))
5465 /* The label is not in an enclosing block, so illegal. This was
5466 allowed in Fortran 66, so we allow it as extension. No
5467 further checks are necessary in this case. */
5468 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
5469 "as the GOTO statement at %L", &label->where,
5474 /* Step four: Make sure that the branching target is legal if
5475 the statement is an END {SELECT,IF}. */
5477 for (stack = cs_base; stack; stack = stack->prev)
5478 if (stack->current->next && stack->current->next->here == label)
5481 if (stack && stack->current->next->op == EXEC_NOP)
5483 gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: GOTO at %L jumps to "
5484 "END of construct at %L", &code->loc,
5485 &stack->current->next->loc);
5486 return; /* We know this is not an END DO. */
5489 /* Step five: Make sure that we're not jumping to the end of a DO
5490 loop from within the loop. */
5492 for (stack = cs_base; stack; stack = stack->prev)
5493 if ((stack->current->op == EXEC_DO
5494 || stack->current->op == EXEC_DO_WHILE)
5495 && stack->tail->here == label && stack->tail->op == EXEC_NOP)
5497 gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: GOTO at %L jumps "
5498 "to END of construct at %L", &code->loc,
5506 /* Check whether EXPR1 has the same shape as EXPR2. */
5509 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
5511 mpz_t shape[GFC_MAX_DIMENSIONS];
5512 mpz_t shape2[GFC_MAX_DIMENSIONS];
5513 try result = FAILURE;
5516 /* Compare the rank. */
5517 if (expr1->rank != expr2->rank)
5520 /* Compare the size of each dimension. */
5521 for (i=0; i<expr1->rank; i++)
5523 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
5526 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
5529 if (mpz_cmp (shape[i], shape2[i]))
5533 /* When either of the two expression is an assumed size array, we
5534 ignore the comparison of dimension sizes. */
5539 for (i--; i >= 0; i--)
5541 mpz_clear (shape[i]);
5542 mpz_clear (shape2[i]);
5548 /* Check whether a WHERE assignment target or a WHERE mask expression
5549 has the same shape as the outmost WHERE mask expression. */
5552 resolve_where (gfc_code *code, gfc_expr *mask)
5558 cblock = code->block;
5560 /* Store the first WHERE mask-expr of the WHERE statement or construct.
5561 In case of nested WHERE, only the outmost one is stored. */
5562 if (mask == NULL) /* outmost WHERE */
5564 else /* inner WHERE */
5571 /* Check if the mask-expr has a consistent shape with the
5572 outmost WHERE mask-expr. */
5573 if (resolve_where_shape (cblock->expr, e) == FAILURE)
5574 gfc_error ("WHERE mask at %L has inconsistent shape",
5575 &cblock->expr->where);
5578 /* the assignment statement of a WHERE statement, or the first
5579 statement in where-body-construct of a WHERE construct */
5580 cnext = cblock->next;
5585 /* WHERE assignment statement */
5588 /* Check shape consistent for WHERE assignment target. */
5589 if (e && resolve_where_shape (cnext->expr, e) == FAILURE)
5590 gfc_error ("WHERE assignment target at %L has "
5591 "inconsistent shape", &cnext->expr->where);
5595 case EXEC_ASSIGN_CALL:
5596 resolve_call (cnext);
5599 /* WHERE or WHERE construct is part of a where-body-construct */
5601 resolve_where (cnext, e);
5605 gfc_error ("Unsupported statement inside WHERE at %L",
5608 /* the next statement within the same where-body-construct */
5609 cnext = cnext->next;
5611 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
5612 cblock = cblock->block;
5617 /* Resolve assignment in FORALL construct.
5618 NVAR is the number of FORALL index variables, and VAR_EXPR records the
5619 FORALL index variables. */
5622 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
5626 for (n = 0; n < nvar; n++)
5628 gfc_symbol *forall_index;
5630 forall_index = var_expr[n]->symtree->n.sym;
5632 /* Check whether the assignment target is one of the FORALL index
5634 if ((code->expr->expr_type == EXPR_VARIABLE)
5635 && (code->expr->symtree->n.sym == forall_index))
5636 gfc_error ("Assignment to a FORALL index variable at %L",
5637 &code->expr->where);
5640 /* If one of the FORALL index variables doesn't appear in the
5641 assignment target, then there will be a many-to-one
5643 if (find_forall_index (code->expr, forall_index, 0) == FAILURE)
5644 gfc_error ("The FORALL with index '%s' cause more than one "
5645 "assignment to this object at %L",
5646 var_expr[n]->symtree->name, &code->expr->where);
5652 /* Resolve WHERE statement in FORALL construct. */
5655 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
5656 gfc_expr **var_expr)
5661 cblock = code->block;
5664 /* the assignment statement of a WHERE statement, or the first
5665 statement in where-body-construct of a WHERE construct */
5666 cnext = cblock->next;
5671 /* WHERE assignment statement */
5673 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
5676 /* WHERE operator assignment statement */
5677 case EXEC_ASSIGN_CALL:
5678 resolve_call (cnext);
5681 /* WHERE or WHERE construct is part of a where-body-construct */
5683 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
5687 gfc_error ("Unsupported statement inside WHERE at %L",
5690 /* the next statement within the same where-body-construct */
5691 cnext = cnext->next;
5693 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
5694 cblock = cblock->block;
5699 /* Traverse the FORALL body to check whether the following errors exist:
5700 1. For assignment, check if a many-to-one assignment happens.
5701 2. For WHERE statement, check the WHERE body to see if there is any
5702 many-to-one assignment. */
5705 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
5709 c = code->block->next;
5715 case EXEC_POINTER_ASSIGN:
5716 gfc_resolve_assign_in_forall (c, nvar, var_expr);
5719 case EXEC_ASSIGN_CALL:
5723 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
5724 there is no need to handle it here. */
5728 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
5733 /* The next statement in the FORALL body. */
5739 /* Given a FORALL construct, first resolve the FORALL iterator, then call
5740 gfc_resolve_forall_body to resolve the FORALL body. */
5743 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
5745 static gfc_expr **var_expr;
5746 static int total_var = 0;
5747 static int nvar = 0;
5748 gfc_forall_iterator *fa;
5752 /* Start to resolve a FORALL construct */
5753 if (forall_save == 0)
5755 /* Count the total number of FORALL index in the nested FORALL
5756 construct in order to allocate the VAR_EXPR with proper size. */
5758 while ((next != NULL) && (next->op == EXEC_FORALL))
5760 for (fa = next->ext.forall_iterator; fa; fa = fa->next)
5762 next = next->block->next;
5765 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
5766 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
5769 /* The information about FORALL iterator, including FORALL index start, end
5770 and stride. The FORALL index can not appear in start, end or stride. */
5771 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
5773 /* Check if any outer FORALL index name is the same as the current
5775 for (i = 0; i < nvar; i++)
5777 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
5779 gfc_error ("An outer FORALL construct already has an index "
5780 "with this name %L", &fa->var->where);
5784 /* Record the current FORALL index. */
5785 var_expr[nvar] = gfc_copy_expr (fa->var);
5790 /* Resolve the FORALL body. */
5791 gfc_resolve_forall_body (code, nvar, var_expr);
5793 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
5794 gfc_resolve_blocks (code->block, ns);
5796 /* Free VAR_EXPR after the whole FORALL construct resolved. */
5797 for (i = 0; i < total_var; i++)
5798 gfc_free_expr (var_expr[i]);
5800 /* Reset the counters. */
5806 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL ,GOTO and
5809 static void resolve_code (gfc_code *, gfc_namespace *);
5812 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
5816 for (; b; b = b->block)
5818 t = gfc_resolve_expr (b->expr);
5819 if (gfc_resolve_expr (b->expr2) == FAILURE)
5825 if (t == SUCCESS && b->expr != NULL
5826 && (b->expr->ts.type != BT_LOGICAL || b->expr->rank != 0))
5827 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
5834 && (b->expr->ts.type != BT_LOGICAL || b->expr->rank == 0))
5835 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
5840 resolve_branch (b->label, b);
5852 case EXEC_OMP_ATOMIC:
5853 case EXEC_OMP_CRITICAL:
5855 case EXEC_OMP_MASTER:
5856 case EXEC_OMP_ORDERED:
5857 case EXEC_OMP_PARALLEL:
5858 case EXEC_OMP_PARALLEL_DO:
5859 case EXEC_OMP_PARALLEL_SECTIONS:
5860 case EXEC_OMP_PARALLEL_WORKSHARE:
5861 case EXEC_OMP_SECTIONS:
5862 case EXEC_OMP_SINGLE:
5863 case EXEC_OMP_WORKSHARE:
5867 gfc_internal_error ("resolve_block(): Bad block type");
5870 resolve_code (b->next, ns);
5875 /* Does everything to resolve an ordinary assignment. Returns true
5876 if this is an interface asignment. */
5878 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
5889 if (gfc_extend_assign (code, ns) == SUCCESS)
5891 lhs = code->ext.actual->expr;
5892 rhs = code->ext.actual->next->expr;
5893 if (gfc_pure (NULL) && !gfc_pure (code->symtree->n.sym))
5895 gfc_error ("Subroutine '%s' called instead of assignment at "
5896 "%L must be PURE", code->symtree->n.sym->name,
5901 /* Make a temporary rhs when there is a default initializer
5902 and rhs is the same symbol as the lhs. */
5903 if (rhs->expr_type == EXPR_VARIABLE
5904 && rhs->symtree->n.sym->ts.type == BT_DERIVED
5905 && has_default_initializer (rhs->symtree->n.sym->ts.derived)
5906 && (lhs->symtree->n.sym == rhs->symtree->n.sym))
5907 code->ext.actual->next->expr = gfc_get_parentheses (rhs);
5915 if (lhs->ts.type == BT_CHARACTER
5916 && gfc_option.warn_character_truncation)
5918 if (lhs->ts.cl != NULL
5919 && lhs->ts.cl->length != NULL
5920 && lhs->ts.cl->length->expr_type == EXPR_CONSTANT)
5921 llen = mpz_get_si (lhs->ts.cl->length->value.integer);
5923 if (rhs->expr_type == EXPR_CONSTANT)
5924 rlen = rhs->value.character.length;
5926 else if (rhs->ts.cl != NULL
5927 && rhs->ts.cl->length != NULL
5928 && rhs->ts.cl->length->expr_type == EXPR_CONSTANT)
5929 rlen = mpz_get_si (rhs->ts.cl->length->value.integer);
5931 if (rlen && llen && rlen > llen)
5932 gfc_warning_now ("CHARACTER expression will be truncated "
5933 "in assignment (%d/%d) at %L",
5934 llen, rlen, &code->loc);
5937 /* Ensure that a vector index expression for the lvalue is evaluated
5938 to a temporary if the lvalue symbol is referenced in it. */
5941 for (ref = lhs->ref; ref; ref= ref->next)
5942 if (ref->type == REF_ARRAY)
5944 for (n = 0; n < ref->u.ar.dimen; n++)
5945 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
5946 && find_sym_in_expr (lhs->symtree->n.sym,
5947 ref->u.ar.start[n]))
5949 = gfc_get_parentheses (ref->u.ar.start[n]);
5953 if (gfc_pure (NULL))
5955 if (gfc_impure_variable (lhs->symtree->n.sym))
5957 gfc_error ("Cannot assign to variable '%s' in PURE "
5959 lhs->symtree->n.sym->name,
5964 if (lhs->ts.type == BT_DERIVED
5965 && lhs->expr_type == EXPR_VARIABLE
5966 && lhs->ts.derived->attr.pointer_comp
5967 && gfc_impure_variable (rhs->symtree->n.sym))
5969 gfc_error ("The impure variable at %L is assigned to "
5970 "a derived type variable with a POINTER "
5971 "component in a PURE procedure (12.6)",
5977 gfc_check_assign (lhs, rhs, 1);
5981 /* Given a block of code, recursively resolve everything pointed to by this
5985 resolve_code (gfc_code *code, gfc_namespace *ns)
5987 int omp_workshare_save;
5993 frame.prev = cs_base;
5997 reachable_labels (code);
5999 for (; code; code = code->next)
6001 frame.current = code;
6002 forall_save = forall_flag;
6004 if (code->op == EXEC_FORALL)
6007 gfc_resolve_forall (code, ns, forall_save);
6010 else if (code->block)
6012 omp_workshare_save = -1;
6015 case EXEC_OMP_PARALLEL_WORKSHARE:
6016 omp_workshare_save = omp_workshare_flag;
6017 omp_workshare_flag = 1;
6018 gfc_resolve_omp_parallel_blocks (code, ns);
6020 case EXEC_OMP_PARALLEL:
6021 case EXEC_OMP_PARALLEL_DO:
6022 case EXEC_OMP_PARALLEL_SECTIONS:
6023 omp_workshare_save = omp_workshare_flag;
6024 omp_workshare_flag = 0;
6025 gfc_resolve_omp_parallel_blocks (code, ns);
6028 gfc_resolve_omp_do_blocks (code, ns);
6030 case EXEC_OMP_WORKSHARE:
6031 omp_workshare_save = omp_workshare_flag;
6032 omp_workshare_flag = 1;
6035 gfc_resolve_blocks (code->block, ns);
6039 if (omp_workshare_save != -1)
6040 omp_workshare_flag = omp_workshare_save;
6043 t = gfc_resolve_expr (code->expr);
6044 forall_flag = forall_save;
6046 if (gfc_resolve_expr (code->expr2) == FAILURE)
6061 /* Keep track of which entry we are up to. */
6062 current_entry_id = code->ext.entry->id;
6066 resolve_where (code, NULL);
6070 if (code->expr != NULL)
6072 if (code->expr->ts.type != BT_INTEGER)
6073 gfc_error ("ASSIGNED GOTO statement at %L requires an "
6074 "INTEGER variable", &code->expr->where);
6075 else if (code->expr->symtree->n.sym->attr.assign != 1)
6076 gfc_error ("Variable '%s' has not been assigned a target "
6077 "label at %L", code->expr->symtree->n.sym->name,
6078 &code->expr->where);
6081 resolve_branch (code->label, code);
6085 if (code->expr != NULL
6086 && (code->expr->ts.type != BT_INTEGER || code->expr->rank))
6087 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
6088 "INTEGER return specifier", &code->expr->where);
6091 case EXEC_INIT_ASSIGN:
6098 if (resolve_ordinary_assign (code, ns))
6103 case EXEC_LABEL_ASSIGN:
6104 if (code->label->defined == ST_LABEL_UNKNOWN)
6105 gfc_error ("Label %d referenced at %L is never defined",
6106 code->label->value, &code->label->where);
6108 && (code->expr->expr_type != EXPR_VARIABLE
6109 || code->expr->symtree->n.sym->ts.type != BT_INTEGER
6110 || code->expr->symtree->n.sym->ts.kind
6111 != gfc_default_integer_kind
6112 || code->expr->symtree->n.sym->as != NULL))
6113 gfc_error ("ASSIGN statement at %L requires a scalar "
6114 "default INTEGER variable", &code->expr->where);
6117 case EXEC_POINTER_ASSIGN:
6121 gfc_check_pointer_assign (code->expr, code->expr2);
6124 case EXEC_ARITHMETIC_IF:
6126 && code->expr->ts.type != BT_INTEGER
6127 && code->expr->ts.type != BT_REAL)
6128 gfc_error ("Arithmetic IF statement at %L requires a numeric "
6129 "expression", &code->expr->where);
6131 resolve_branch (code->label, code);
6132 resolve_branch (code->label2, code);
6133 resolve_branch (code->label3, code);
6137 if (t == SUCCESS && code->expr != NULL
6138 && (code->expr->ts.type != BT_LOGICAL
6139 || code->expr->rank != 0))
6140 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
6141 &code->expr->where);
6146 resolve_call (code);
6150 /* Select is complicated. Also, a SELECT construct could be
6151 a transformed computed GOTO. */
6152 resolve_select (code);
6156 if (code->ext.iterator != NULL)
6158 gfc_iterator *iter = code->ext.iterator;
6159 if (gfc_resolve_iterator (iter, true) != FAILURE)
6160 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
6165 if (code->expr == NULL)
6166 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
6168 && (code->expr->rank != 0
6169 || code->expr->ts.type != BT_LOGICAL))
6170 gfc_error ("Exit condition of DO WHILE loop at %L must be "
6171 "a scalar LOGICAL expression", &code->expr->where);
6175 if (t == SUCCESS && code->expr != NULL
6176 && code->expr->ts.type != BT_INTEGER)
6177 gfc_error ("STAT tag in ALLOCATE statement at %L must be "
6178 "of type INTEGER", &code->expr->where);
6180 for (a = code->ext.alloc_list; a; a = a->next)
6181 resolve_allocate_expr (a->expr, code);
6185 case EXEC_DEALLOCATE:
6186 if (t == SUCCESS && code->expr != NULL
6187 && code->expr->ts.type != BT_INTEGER)
6189 ("STAT tag in DEALLOCATE statement at %L must be of type "
6190 "INTEGER", &code->expr->where);
6192 for (a = code->ext.alloc_list; a; a = a->next)
6193 resolve_deallocate_expr (a->expr);
6198 if (gfc_resolve_open (code->ext.open) == FAILURE)
6201 resolve_branch (code->ext.open->err, code);
6205 if (gfc_resolve_close (code->ext.close) == FAILURE)
6208 resolve_branch (code->ext.close->err, code);
6211 case EXEC_BACKSPACE:
6215 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
6218 resolve_branch (code->ext.filepos->err, code);
6222 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
6225 resolve_branch (code->ext.inquire->err, code);
6229 gcc_assert (code->ext.inquire != NULL);
6230 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
6233 resolve_branch (code->ext.inquire->err, code);
6238 if (gfc_resolve_dt (code->ext.dt) == FAILURE)
6241 resolve_branch (code->ext.dt->err, code);
6242 resolve_branch (code->ext.dt->end, code);
6243 resolve_branch (code->ext.dt->eor, code);
6247 resolve_transfer (code);
6251 resolve_forall_iterators (code->ext.forall_iterator);
6253 if (code->expr != NULL && code->expr->ts.type != BT_LOGICAL)
6254 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
6255 "expression", &code->expr->where);
6258 case EXEC_OMP_ATOMIC:
6259 case EXEC_OMP_BARRIER:
6260 case EXEC_OMP_CRITICAL:
6261 case EXEC_OMP_FLUSH:
6263 case EXEC_OMP_MASTER:
6264 case EXEC_OMP_ORDERED:
6265 case EXEC_OMP_SECTIONS:
6266 case EXEC_OMP_SINGLE:
6267 case EXEC_OMP_WORKSHARE:
6268 gfc_resolve_omp_directive (code, ns);
6271 case EXEC_OMP_PARALLEL:
6272 case EXEC_OMP_PARALLEL_DO:
6273 case EXEC_OMP_PARALLEL_SECTIONS:
6274 case EXEC_OMP_PARALLEL_WORKSHARE:
6275 omp_workshare_save = omp_workshare_flag;
6276 omp_workshare_flag = 0;
6277 gfc_resolve_omp_directive (code, ns);
6278 omp_workshare_flag = omp_workshare_save;
6282 gfc_internal_error ("resolve_code(): Bad statement code");
6286 cs_base = frame.prev;
6290 /* Resolve initial values and make sure they are compatible with
6294 resolve_values (gfc_symbol *sym)
6296 if (sym->value == NULL)
6299 if (gfc_resolve_expr (sym->value) == FAILURE)
6302 gfc_check_assign_symbol (sym, sym->value);
6306 /* Verify the binding labels for common blocks that are BIND(C). The label
6307 for a BIND(C) common block must be identical in all scoping units in which
6308 the common block is declared. Further, the binding label can not collide
6309 with any other global entity in the program. */
6312 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
6314 if (comm_block_tree->n.common->is_bind_c == 1)
6316 gfc_gsymbol *binding_label_gsym;
6317 gfc_gsymbol *comm_name_gsym;
6319 /* See if a global symbol exists by the common block's name. It may
6320 be NULL if the common block is use-associated. */
6321 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
6322 comm_block_tree->n.common->name);
6323 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
6324 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
6325 "with the global entity '%s' at %L",
6326 comm_block_tree->n.common->binding_label,
6327 comm_block_tree->n.common->name,
6328 &(comm_block_tree->n.common->where),
6329 comm_name_gsym->name, &(comm_name_gsym->where));
6330 else if (comm_name_gsym != NULL
6331 && strcmp (comm_name_gsym->name,
6332 comm_block_tree->n.common->name) == 0)
6334 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
6336 if (comm_name_gsym->binding_label == NULL)
6337 /* No binding label for common block stored yet; save this one. */
6338 comm_name_gsym->binding_label =
6339 comm_block_tree->n.common->binding_label;
6341 if (strcmp (comm_name_gsym->binding_label,
6342 comm_block_tree->n.common->binding_label) != 0)
6344 /* Common block names match but binding labels do not. */
6345 gfc_error ("Binding label '%s' for common block '%s' at %L "
6346 "does not match the binding label '%s' for common "
6348 comm_block_tree->n.common->binding_label,
6349 comm_block_tree->n.common->name,
6350 &(comm_block_tree->n.common->where),
6351 comm_name_gsym->binding_label,
6352 comm_name_gsym->name,
6353 &(comm_name_gsym->where));
6358 /* There is no binding label (NAME="") so we have nothing further to
6359 check and nothing to add as a global symbol for the label. */
6360 if (comm_block_tree->n.common->binding_label[0] == '\0' )
6363 binding_label_gsym =
6364 gfc_find_gsymbol (gfc_gsym_root,
6365 comm_block_tree->n.common->binding_label);
6366 if (binding_label_gsym == NULL)
6368 /* Need to make a global symbol for the binding label to prevent
6369 it from colliding with another. */
6370 binding_label_gsym =
6371 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
6372 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
6373 binding_label_gsym->type = GSYM_COMMON;
6377 /* If comm_name_gsym is NULL, the name common block is use
6378 associated and the name could be colliding. */
6379 if (binding_label_gsym->type != GSYM_COMMON)
6380 gfc_error ("Binding label '%s' for common block '%s' at %L "
6381 "collides with the global entity '%s' at %L",
6382 comm_block_tree->n.common->binding_label,
6383 comm_block_tree->n.common->name,
6384 &(comm_block_tree->n.common->where),
6385 binding_label_gsym->name,
6386 &(binding_label_gsym->where));
6387 else if (comm_name_gsym != NULL
6388 && (strcmp (binding_label_gsym->name,
6389 comm_name_gsym->binding_label) != 0)
6390 && (strcmp (binding_label_gsym->sym_name,
6391 comm_name_gsym->name) != 0))
6392 gfc_error ("Binding label '%s' for common block '%s' at %L "
6393 "collides with global entity '%s' at %L",
6394 binding_label_gsym->name, binding_label_gsym->sym_name,
6395 &(comm_block_tree->n.common->where),
6396 comm_name_gsym->name, &(comm_name_gsym->where));
6404 /* Verify any BIND(C) derived types in the namespace so we can report errors
6405 for them once, rather than for each variable declared of that type. */
6408 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
6410 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
6411 && derived_sym->attr.is_bind_c == 1)
6412 verify_bind_c_derived_type (derived_sym);
6418 /* Verify that any binding labels used in a given namespace do not collide
6419 with the names or binding labels of any global symbols. */
6422 gfc_verify_binding_labels (gfc_symbol *sym)
6426 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
6427 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
6429 gfc_gsymbol *bind_c_sym;
6431 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
6432 if (bind_c_sym != NULL
6433 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
6435 if (sym->attr.if_source == IFSRC_DECL
6436 && (bind_c_sym->type != GSYM_SUBROUTINE
6437 && bind_c_sym->type != GSYM_FUNCTION)
6438 && ((sym->attr.contained == 1
6439 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
6440 || (sym->attr.use_assoc == 1
6441 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
6443 /* Make sure global procedures don't collide with anything. */
6444 gfc_error ("Binding label '%s' at %L collides with the global "
6445 "entity '%s' at %L", sym->binding_label,
6446 &(sym->declared_at), bind_c_sym->name,
6447 &(bind_c_sym->where));
6450 else if (sym->attr.contained == 0
6451 && (sym->attr.if_source == IFSRC_IFBODY
6452 && sym->attr.flavor == FL_PROCEDURE)
6453 && (bind_c_sym->sym_name != NULL
6454 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
6456 /* Make sure procedures in interface bodies don't collide. */
6457 gfc_error ("Binding label '%s' in interface body at %L collides "
6458 "with the global entity '%s' at %L",
6460 &(sym->declared_at), bind_c_sym->name,
6461 &(bind_c_sym->where));
6464 else if (sym->attr.contained == 0
6465 && (sym->attr.if_source == IFSRC_UNKNOWN))
6466 if ((sym->attr.use_assoc
6467 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))
6468 || sym->attr.use_assoc == 0)
6470 gfc_error ("Binding label '%s' at %L collides with global "
6471 "entity '%s' at %L", sym->binding_label,
6472 &(sym->declared_at), bind_c_sym->name,
6473 &(bind_c_sym->where));
6478 /* Clear the binding label to prevent checking multiple times. */
6479 sym->binding_label[0] = '\0';
6481 else if (bind_c_sym == NULL)
6483 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
6484 bind_c_sym->where = sym->declared_at;
6485 bind_c_sym->sym_name = sym->name;
6487 if (sym->attr.use_assoc == 1)
6488 bind_c_sym->mod_name = sym->module;
6490 if (sym->ns->proc_name != NULL)
6491 bind_c_sym->mod_name = sym->ns->proc_name->name;
6493 if (sym->attr.contained == 0)
6495 if (sym->attr.subroutine)
6496 bind_c_sym->type = GSYM_SUBROUTINE;
6497 else if (sym->attr.function)
6498 bind_c_sym->type = GSYM_FUNCTION;
6506 /* Resolve an index expression. */
6509 resolve_index_expr (gfc_expr *e)
6511 if (gfc_resolve_expr (e) == FAILURE)
6514 if (gfc_simplify_expr (e, 0) == FAILURE)
6517 if (gfc_specification_expr (e) == FAILURE)
6523 /* Resolve a charlen structure. */
6526 resolve_charlen (gfc_charlen *cl)
6535 specification_expr = 1;
6537 if (resolve_index_expr (cl->length) == FAILURE)
6539 specification_expr = 0;
6543 /* "If the character length parameter value evaluates to a negative
6544 value, the length of character entities declared is zero." */
6545 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
6547 gfc_warning_now ("CHARACTER variable has zero length at %L",
6548 &cl->length->where);
6549 gfc_replace_expr (cl->length, gfc_int_expr (0));
6556 /* Test for non-constant shape arrays. */
6559 is_non_constant_shape_array (gfc_symbol *sym)
6565 not_constant = false;
6566 if (sym->as != NULL)
6568 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
6569 has not been simplified; parameter array references. Do the
6570 simplification now. */
6571 for (i = 0; i < sym->as->rank; i++)
6573 e = sym->as->lower[i];
6574 if (e && (resolve_index_expr (e) == FAILURE
6575 || !gfc_is_constant_expr (e)))
6576 not_constant = true;
6578 e = sym->as->upper[i];
6579 if (e && (resolve_index_expr (e) == FAILURE
6580 || !gfc_is_constant_expr (e)))
6581 not_constant = true;
6584 return not_constant;
6587 /* Given a symbol and an initialization expression, add code to initialize
6588 the symbol to the function entry. */
6590 build_init_assign (gfc_symbol *sym, gfc_expr *init)
6594 gfc_namespace *ns = sym->ns;
6596 /* Search for the function namespace if this is a contained
6597 function without an explicit result. */
6598 if (sym->attr.function && sym == sym->result
6599 && sym->name != sym->ns->proc_name->name)
6602 for (;ns; ns = ns->sibling)
6603 if (strcmp (ns->proc_name->name, sym->name) == 0)
6609 gfc_free_expr (init);
6613 /* Build an l-value expression for the result. */
6614 lval = gfc_lval_expr_from_sym (sym);
6616 /* Add the code at scope entry. */
6617 init_st = gfc_get_code ();
6618 init_st->next = ns->code;
6621 /* Assign the default initializer to the l-value. */
6622 init_st->loc = sym->declared_at;
6623 init_st->op = EXEC_INIT_ASSIGN;
6624 init_st->expr = lval;
6625 init_st->expr2 = init;
6628 /* Assign the default initializer to a derived type variable or result. */
6631 apply_default_init (gfc_symbol *sym)
6633 gfc_expr *init = NULL;
6635 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
6638 if (sym->ts.type == BT_DERIVED && sym->ts.derived)
6639 init = gfc_default_initializer (&sym->ts);
6644 build_init_assign (sym, init);
6647 /* Build an initializer for a local integer, real, complex, logical, or
6648 character variable, based on the command line flags finit-local-zero,
6649 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
6650 null if the symbol should not have a default initialization. */
6652 build_default_init_expr (gfc_symbol *sym)
6655 gfc_expr *init_expr;
6659 /* These symbols should never have a default initialization. */
6660 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
6661 || sym->attr.external
6663 || sym->attr.pointer
6664 || sym->attr.in_equivalence
6665 || sym->attr.in_common
6668 || sym->attr.cray_pointee
6669 || sym->attr.cray_pointer)
6672 /* Now we'll try to build an initializer expression. */
6673 init_expr = gfc_get_expr ();
6674 init_expr->expr_type = EXPR_CONSTANT;
6675 init_expr->ts.type = sym->ts.type;
6676 init_expr->ts.kind = sym->ts.kind;
6677 init_expr->where = sym->declared_at;
6679 /* We will only initialize integers, reals, complex, logicals, and
6680 characters, and only if the corresponding command-line flags
6681 were set. Otherwise, we free init_expr and return null. */
6682 switch (sym->ts.type)
6685 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
6686 mpz_init_set_si (init_expr->value.integer,
6687 gfc_option.flag_init_integer_value);
6690 gfc_free_expr (init_expr);
6696 mpfr_init (init_expr->value.real);
6697 switch (gfc_option.flag_init_real)
6699 case GFC_INIT_REAL_NAN:
6700 mpfr_set_nan (init_expr->value.real);
6703 case GFC_INIT_REAL_INF:
6704 mpfr_set_inf (init_expr->value.real, 1);
6707 case GFC_INIT_REAL_NEG_INF:
6708 mpfr_set_inf (init_expr->value.real, -1);
6711 case GFC_INIT_REAL_ZERO:
6712 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
6716 gfc_free_expr (init_expr);
6723 mpfr_init (init_expr->value.complex.r);
6724 mpfr_init (init_expr->value.complex.i);
6725 switch (gfc_option.flag_init_real)
6727 case GFC_INIT_REAL_NAN:
6728 mpfr_set_nan (init_expr->value.complex.r);
6729 mpfr_set_nan (init_expr->value.complex.i);
6732 case GFC_INIT_REAL_INF:
6733 mpfr_set_inf (init_expr->value.complex.r, 1);
6734 mpfr_set_inf (init_expr->value.complex.i, 1);
6737 case GFC_INIT_REAL_NEG_INF:
6738 mpfr_set_inf (init_expr->value.complex.r, -1);
6739 mpfr_set_inf (init_expr->value.complex.i, -1);
6742 case GFC_INIT_REAL_ZERO:
6743 mpfr_set_ui (init_expr->value.complex.r, 0.0, GFC_RND_MODE);
6744 mpfr_set_ui (init_expr->value.complex.i, 0.0, GFC_RND_MODE);
6748 gfc_free_expr (init_expr);
6755 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
6756 init_expr->value.logical = 0;
6757 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
6758 init_expr->value.logical = 1;
6761 gfc_free_expr (init_expr);
6767 /* For characters, the length must be constant in order to
6768 create a default initializer. */
6769 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
6770 && sym->ts.cl->length
6771 && sym->ts.cl->length->expr_type == EXPR_CONSTANT)
6773 char_len = mpz_get_si (sym->ts.cl->length->value.integer);
6774 init_expr->value.character.length = char_len;
6775 init_expr->value.character.string = gfc_getmem (char_len+1);
6776 ch = init_expr->value.character.string;
6777 for (i = 0; i < char_len; i++)
6778 *(ch++) = gfc_option.flag_init_character_value;
6782 gfc_free_expr (init_expr);
6788 gfc_free_expr (init_expr);
6794 /* Add an initialization expression to a local variable. */
6796 apply_default_init_local (gfc_symbol *sym)
6798 gfc_expr *init = NULL;
6800 /* The symbol should be a variable or a function return value. */
6801 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
6802 || (sym->attr.function && sym->result != sym))
6805 /* Try to build the initializer expression. If we can't initialize
6806 this symbol, then init will be NULL. */
6807 init = build_default_init_expr (sym);
6811 /* For saved variables, we don't want to add an initializer at
6812 function entry, so we just add a static initializer. */
6813 if (sym->attr.save || sym->ns->save_all)
6815 /* Don't clobber an existing initializer! */
6816 gcc_assert (sym->value == NULL);
6821 build_init_assign (sym, init);
6824 /* Resolution of common features of flavors variable and procedure. */
6827 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
6829 /* Constraints on deferred shape variable. */
6830 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
6832 if (sym->attr.allocatable)
6834 if (sym->attr.dimension)
6835 gfc_error ("Allocatable array '%s' at %L must have "
6836 "a deferred shape", sym->name, &sym->declared_at);
6838 gfc_error ("Scalar object '%s' at %L may not be ALLOCATABLE",
6839 sym->name, &sym->declared_at);
6843 if (sym->attr.pointer && sym->attr.dimension)
6845 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
6846 sym->name, &sym->declared_at);
6853 if (!mp_flag && !sym->attr.allocatable
6854 && !sym->attr.pointer && !sym->attr.dummy)
6856 gfc_error ("Array '%s' at %L cannot have a deferred shape",
6857 sym->name, &sym->declared_at);
6865 /* Additional checks for symbols with flavor variable and derived
6866 type. To be called from resolve_fl_variable. */
6869 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
6871 gcc_assert (sym->ts.type == BT_DERIVED);
6873 /* Check to see if a derived type is blocked from being host
6874 associated by the presence of another class I symbol in the same
6875 namespace. 14.6.1.3 of the standard and the discussion on
6876 comp.lang.fortran. */
6877 if (sym->ns != sym->ts.derived->ns
6878 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
6881 gfc_find_symbol (sym->ts.derived->name, sym->ns, 0, &s);
6882 if (s && (s->attr.flavor != FL_DERIVED
6883 || !gfc_compare_derived_types (s, sym->ts.derived)))
6885 gfc_error ("The type '%s' cannot be host associated at %L "
6886 "because it is blocked by an incompatible object "
6887 "of the same name declared at %L",
6888 sym->ts.derived->name, &sym->declared_at,
6894 /* 4th constraint in section 11.3: "If an object of a type for which
6895 component-initialization is specified (R429) appears in the
6896 specification-part of a module and does not have the ALLOCATABLE
6897 or POINTER attribute, the object shall have the SAVE attribute."
6899 The check for initializers is performed with
6900 has_default_initializer because gfc_default_initializer generates
6901 a hidden default for allocatable components. */
6902 if (!(sym->value || no_init_flag) && sym->ns->proc_name
6903 && sym->ns->proc_name->attr.flavor == FL_MODULE
6904 && !sym->ns->save_all && !sym->attr.save
6905 && !sym->attr.pointer && !sym->attr.allocatable
6906 && has_default_initializer (sym->ts.derived))
6908 gfc_error("Object '%s' at %L must have the SAVE attribute for "
6909 "default initialization of a component",
6910 sym->name, &sym->declared_at);
6914 /* Assign default initializer. */
6915 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
6916 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
6918 sym->value = gfc_default_initializer (&sym->ts);
6925 /* Resolve symbols with flavor variable. */
6928 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
6930 int no_init_flag, automatic_flag;
6932 const char *auto_save_msg;
6934 auto_save_msg = "Automatic object '%s' at %L cannot have the "
6937 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
6940 /* Set this flag to check that variables are parameters of all entries.
6941 This check is effected by the call to gfc_resolve_expr through
6942 is_non_constant_shape_array. */
6943 specification_expr = 1;
6945 if (sym->ns->proc_name
6946 && (sym->ns->proc_name->attr.flavor == FL_MODULE
6947 || sym->ns->proc_name->attr.is_main_program)
6948 && !sym->attr.use_assoc
6949 && !sym->attr.allocatable
6950 && !sym->attr.pointer
6951 && is_non_constant_shape_array (sym))
6953 /* The shape of a main program or module array needs to be
6955 gfc_error ("The module or main program array '%s' at %L must "
6956 "have constant shape", sym->name, &sym->declared_at);
6957 specification_expr = 0;
6961 if (sym->ts.type == BT_CHARACTER)
6963 /* Make sure that character string variables with assumed length are
6965 e = sym->ts.cl->length;
6966 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
6968 gfc_error ("Entity with assumed character length at %L must be a "
6969 "dummy argument or a PARAMETER", &sym->declared_at);
6973 if (e && sym->attr.save && !gfc_is_constant_expr (e))
6975 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
6979 if (!gfc_is_constant_expr (e)
6980 && !(e->expr_type == EXPR_VARIABLE
6981 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
6982 && sym->ns->proc_name
6983 && (sym->ns->proc_name->attr.flavor == FL_MODULE
6984 || sym->ns->proc_name->attr.is_main_program)
6985 && !sym->attr.use_assoc)
6987 gfc_error ("'%s' at %L must have constant character length "
6988 "in this context", sym->name, &sym->declared_at);
6993 if (sym->value == NULL && sym->attr.referenced)
6994 apply_default_init_local (sym); /* Try to apply a default initialization. */
6996 /* Determine if the symbol may not have an initializer. */
6997 no_init_flag = automatic_flag = 0;
6998 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
6999 || sym->attr.intrinsic || sym->attr.result)
7001 else if (sym->attr.dimension && !sym->attr.pointer
7002 && is_non_constant_shape_array (sym))
7004 no_init_flag = automatic_flag = 1;
7006 /* Also, they must not have the SAVE attribute.
7007 SAVE_IMPLICIT is checked below. */
7008 if (sym->attr.save == SAVE_EXPLICIT)
7010 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
7015 /* Reject illegal initializers. */
7016 if (!sym->mark && sym->value)
7018 if (sym->attr.allocatable)
7019 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
7020 sym->name, &sym->declared_at);
7021 else if (sym->attr.external)
7022 gfc_error ("External '%s' at %L cannot have an initializer",
7023 sym->name, &sym->declared_at);
7024 else if (sym->attr.dummy
7025 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
7026 gfc_error ("Dummy '%s' at %L cannot have an initializer",
7027 sym->name, &sym->declared_at);
7028 else if (sym->attr.intrinsic)
7029 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
7030 sym->name, &sym->declared_at);
7031 else if (sym->attr.result)
7032 gfc_error ("Function result '%s' at %L cannot have an initializer",
7033 sym->name, &sym->declared_at);
7034 else if (automatic_flag)
7035 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
7036 sym->name, &sym->declared_at);
7043 if (sym->ts.type == BT_DERIVED)
7044 return resolve_fl_variable_derived (sym, no_init_flag);
7050 /* Resolve a procedure. */
7053 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
7055 gfc_formal_arglist *arg;
7057 if (sym->attr.ambiguous_interfaces && !sym->attr.referenced)
7058 gfc_warning ("Although not referenced, '%s' at %L has ambiguous "
7059 "interfaces", sym->name, &sym->declared_at);
7061 if (sym->attr.function
7062 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
7065 if (sym->ts.type == BT_CHARACTER)
7067 gfc_charlen *cl = sym->ts.cl;
7069 if (cl && cl->length && gfc_is_constant_expr (cl->length)
7070 && resolve_charlen (cl) == FAILURE)
7073 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
7075 if (sym->attr.proc == PROC_ST_FUNCTION)
7077 gfc_error ("Character-valued statement function '%s' at %L must "
7078 "have constant length", sym->name, &sym->declared_at);
7082 if (sym->attr.external && sym->formal == NULL
7083 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
7085 gfc_error ("Automatic character length function '%s' at %L must "
7086 "have an explicit interface", sym->name,
7093 /* Ensure that derived type for are not of a private type. Internal
7094 module procedures are excluded by 2.2.3.3 - ie. they are not
7095 externally accessible and can access all the objects accessible in
7097 if (!(sym->ns->parent
7098 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
7099 && gfc_check_access(sym->attr.access, sym->ns->default_access))
7101 gfc_interface *iface;
7103 for (arg = sym->formal; arg; arg = arg->next)
7106 && arg->sym->ts.type == BT_DERIVED
7107 && !arg->sym->ts.derived->attr.use_assoc
7108 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7109 arg->sym->ts.derived->ns->default_access)
7110 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
7111 "PRIVATE type and cannot be a dummy argument"
7112 " of '%s', which is PUBLIC at %L",
7113 arg->sym->name, sym->name, &sym->declared_at)
7116 /* Stop this message from recurring. */
7117 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7122 /* PUBLIC interfaces may expose PRIVATE procedures that take types
7123 PRIVATE to the containing module. */
7124 for (iface = sym->generic; iface; iface = iface->next)
7126 for (arg = iface->sym->formal; arg; arg = arg->next)
7129 && arg->sym->ts.type == BT_DERIVED
7130 && !arg->sym->ts.derived->attr.use_assoc
7131 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7132 arg->sym->ts.derived->ns->default_access)
7133 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
7134 "'%s' in PUBLIC interface '%s' at %L "
7135 "takes dummy arguments of '%s' which is "
7136 "PRIVATE", iface->sym->name, sym->name,
7137 &iface->sym->declared_at,
7138 gfc_typename (&arg->sym->ts)) == FAILURE)
7140 /* Stop this message from recurring. */
7141 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7147 /* PUBLIC interfaces may expose PRIVATE procedures that take types
7148 PRIVATE to the containing module. */
7149 for (iface = sym->generic; iface; iface = iface->next)
7151 for (arg = iface->sym->formal; arg; arg = arg->next)
7154 && arg->sym->ts.type == BT_DERIVED
7155 && !arg->sym->ts.derived->attr.use_assoc
7156 && !gfc_check_access (arg->sym->ts.derived->attr.access,
7157 arg->sym->ts.derived->ns->default_access)
7158 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
7159 "'%s' in PUBLIC interface '%s' at %L "
7160 "takes dummy arguments of '%s' which is "
7161 "PRIVATE", iface->sym->name, sym->name,
7162 &iface->sym->declared_at,
7163 gfc_typename (&arg->sym->ts)) == FAILURE)
7165 /* Stop this message from recurring. */
7166 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
7173 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION)
7175 gfc_error ("Function '%s' at %L cannot have an initializer",
7176 sym->name, &sym->declared_at);
7180 /* An external symbol may not have an initializer because it is taken to be
7182 if (sym->attr.external && sym->value)
7184 gfc_error ("External object '%s' at %L may not have an initializer",
7185 sym->name, &sym->declared_at);
7189 /* An elemental function is required to return a scalar 12.7.1 */
7190 if (sym->attr.elemental && sym->attr.function && sym->as)
7192 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
7193 "result", sym->name, &sym->declared_at);
7194 /* Reset so that the error only occurs once. */
7195 sym->attr.elemental = 0;
7199 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
7200 char-len-param shall not be array-valued, pointer-valued, recursive
7201 or pure. ....snip... A character value of * may only be used in the
7202 following ways: (i) Dummy arg of procedure - dummy associates with
7203 actual length; (ii) To declare a named constant; or (iii) External
7204 function - but length must be declared in calling scoping unit. */
7205 if (sym->attr.function
7206 && sym->ts.type == BT_CHARACTER
7207 && sym->ts.cl && sym->ts.cl->length == NULL)
7209 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
7210 || (sym->attr.recursive) || (sym->attr.pure))
7212 if (sym->as && sym->as->rank)
7213 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7214 "array-valued", sym->name, &sym->declared_at);
7216 if (sym->attr.pointer)
7217 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7218 "pointer-valued", sym->name, &sym->declared_at);
7221 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7222 "pure", sym->name, &sym->declared_at);
7224 if (sym->attr.recursive)
7225 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
7226 "recursive", sym->name, &sym->declared_at);
7231 /* Appendix B.2 of the standard. Contained functions give an
7232 error anyway. Fixed-form is likely to be F77/legacy. */
7233 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
7234 gfc_notify_std (GFC_STD_F95_OBS, "CHARACTER(*) function "
7235 "'%s' at %L is obsolescent in fortran 95",
7236 sym->name, &sym->declared_at);
7239 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
7241 gfc_formal_arglist *curr_arg;
7242 int has_non_interop_arg = 0;
7244 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
7245 sym->common_block) == FAILURE)
7247 /* Clear these to prevent looking at them again if there was an
7249 sym->attr.is_bind_c = 0;
7250 sym->attr.is_c_interop = 0;
7251 sym->ts.is_c_interop = 0;
7255 /* So far, no errors have been found. */
7256 sym->attr.is_c_interop = 1;
7257 sym->ts.is_c_interop = 1;
7260 curr_arg = sym->formal;
7261 while (curr_arg != NULL)
7263 /* Skip implicitly typed dummy args here. */
7264 if (curr_arg->sym->attr.implicit_type == 0)
7265 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
7266 /* If something is found to fail, record the fact so we
7267 can mark the symbol for the procedure as not being
7268 BIND(C) to try and prevent multiple errors being
7270 has_non_interop_arg = 1;
7272 curr_arg = curr_arg->next;
7275 /* See if any of the arguments were not interoperable and if so, clear
7276 the procedure symbol to prevent duplicate error messages. */
7277 if (has_non_interop_arg != 0)
7279 sym->attr.is_c_interop = 0;
7280 sym->ts.is_c_interop = 0;
7281 sym->attr.is_bind_c = 0;
7289 /* Resolve the components of a derived type. */
7292 resolve_fl_derived (gfc_symbol *sym)
7295 gfc_dt_list * dt_list;
7298 for (c = sym->components; c != NULL; c = c->next)
7300 if (c->ts.type == BT_CHARACTER)
7302 if (c->ts.cl->length == NULL
7303 || (resolve_charlen (c->ts.cl) == FAILURE)
7304 || !gfc_is_constant_expr (c->ts.cl->length))
7306 gfc_error ("Character length of component '%s' needs to "
7307 "be a constant specification expression at %L",
7309 c->ts.cl->length ? &c->ts.cl->length->where : &c->loc);
7314 if (c->ts.type == BT_DERIVED
7315 && sym->component_access != ACCESS_PRIVATE
7316 && gfc_check_access (sym->attr.access, sym->ns->default_access)
7317 && !c->ts.derived->attr.use_assoc
7318 && !gfc_check_access (c->ts.derived->attr.access,
7319 c->ts.derived->ns->default_access))
7321 gfc_error ("The component '%s' is a PRIVATE type and cannot be "
7322 "a component of '%s', which is PUBLIC at %L",
7323 c->name, sym->name, &sym->declared_at);
7327 if (sym->attr.sequence)
7329 if (c->ts.type == BT_DERIVED && c->ts.derived->attr.sequence == 0)
7331 gfc_error ("Component %s of SEQUENCE type declared at %L does "
7332 "not have the SEQUENCE attribute",
7333 c->ts.derived->name, &sym->declared_at);
7338 if (c->ts.type == BT_DERIVED && c->pointer
7339 && c->ts.derived->components == NULL)
7341 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
7342 "that has not been declared", c->name, sym->name,
7347 if (c->pointer || c->allocatable || c->as == NULL)
7350 for (i = 0; i < c->as->rank; i++)
7352 if (c->as->lower[i] == NULL
7353 || !gfc_is_constant_expr (c->as->lower[i])
7354 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
7355 || c->as->upper[i] == NULL
7356 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
7357 || !gfc_is_constant_expr (c->as->upper[i]))
7359 gfc_error ("Component '%s' of '%s' at %L must have "
7360 "constant array bounds",
7361 c->name, sym->name, &c->loc);
7367 /* Add derived type to the derived type list. */
7368 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
7369 if (sym == dt_list->derived)
7372 if (dt_list == NULL)
7374 dt_list = gfc_get_dt_list ();
7375 dt_list->next = gfc_derived_types;
7376 dt_list->derived = sym;
7377 gfc_derived_types = dt_list;
7385 resolve_fl_namelist (gfc_symbol *sym)
7390 /* Reject PRIVATE objects in a PUBLIC namelist. */
7391 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
7393 for (nl = sym->namelist; nl; nl = nl->next)
7395 if (!nl->sym->attr.use_assoc
7396 && !(sym->ns->parent == nl->sym->ns)
7397 && !(sym->ns->parent
7398 && sym->ns->parent->parent == nl->sym->ns)
7399 && !gfc_check_access(nl->sym->attr.access,
7400 nl->sym->ns->default_access))
7402 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
7403 "cannot be member of PUBLIC namelist '%s' at %L",
7404 nl->sym->name, sym->name, &sym->declared_at);
7408 /* Types with private components that came here by USE-association. */
7409 if (nl->sym->ts.type == BT_DERIVED
7410 && derived_inaccessible (nl->sym->ts.derived))
7412 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
7413 "components and cannot be member of namelist '%s' at %L",
7414 nl->sym->name, sym->name, &sym->declared_at);
7418 /* Types with private components that are defined in the same module. */
7419 if (nl->sym->ts.type == BT_DERIVED
7420 && !(sym->ns->parent == nl->sym->ts.derived->ns)
7421 && !gfc_check_access (nl->sym->ts.derived->attr.private_comp
7422 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
7423 nl->sym->ns->default_access))
7425 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
7426 "cannot be a member of PUBLIC namelist '%s' at %L",
7427 nl->sym->name, sym->name, &sym->declared_at);
7433 for (nl = sym->namelist; nl; nl = nl->next)
7435 /* Reject namelist arrays of assumed shape. */
7436 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
7437 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
7438 "must not have assumed shape in namelist "
7439 "'%s' at %L", nl->sym->name, sym->name,
7440 &sym->declared_at) == FAILURE)
7443 /* Reject namelist arrays that are not constant shape. */
7444 if (is_non_constant_shape_array (nl->sym))
7446 gfc_error ("NAMELIST array object '%s' must have constant "
7447 "shape in namelist '%s' at %L", nl->sym->name,
7448 sym->name, &sym->declared_at);
7452 /* Namelist objects cannot have allocatable or pointer components. */
7453 if (nl->sym->ts.type != BT_DERIVED)
7456 if (nl->sym->ts.derived->attr.alloc_comp)
7458 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
7459 "have ALLOCATABLE components",
7460 nl->sym->name, sym->name, &sym->declared_at);
7464 if (nl->sym->ts.derived->attr.pointer_comp)
7466 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
7467 "have POINTER components",
7468 nl->sym->name, sym->name, &sym->declared_at);
7474 /* 14.1.2 A module or internal procedure represent local entities
7475 of the same type as a namelist member and so are not allowed. */
7476 for (nl = sym->namelist; nl; nl = nl->next)
7478 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
7481 if (nl->sym->attr.function && nl->sym == nl->sym->result)
7482 if ((nl->sym == sym->ns->proc_name)
7484 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
7488 if (nl->sym && nl->sym->name)
7489 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
7490 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
7492 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
7493 "attribute in '%s' at %L", nlsym->name,
7504 resolve_fl_parameter (gfc_symbol *sym)
7506 /* A parameter array's shape needs to be constant. */
7508 && (sym->as->type == AS_DEFERRED
7509 || is_non_constant_shape_array (sym)))
7511 gfc_error ("Parameter array '%s' at %L cannot be automatic "
7512 "or of deferred shape", sym->name, &sym->declared_at);
7516 /* Make sure a parameter that has been implicitly typed still
7517 matches the implicit type, since PARAMETER statements can precede
7518 IMPLICIT statements. */
7519 if (sym->attr.implicit_type
7520 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym, sym->ns)))
7522 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
7523 "later IMPLICIT type", sym->name, &sym->declared_at);
7527 /* Make sure the types of derived parameters are consistent. This
7528 type checking is deferred until resolution because the type may
7529 refer to a derived type from the host. */
7530 if (sym->ts.type == BT_DERIVED
7531 && !gfc_compare_types (&sym->ts, &sym->value->ts))
7533 gfc_error ("Incompatible derived type in PARAMETER at %L",
7534 &sym->value->where);
7541 /* Do anything necessary to resolve a symbol. Right now, we just
7542 assume that an otherwise unknown symbol is a variable. This sort
7543 of thing commonly happens for symbols in module. */
7546 resolve_symbol (gfc_symbol *sym)
7548 int check_constant, mp_flag;
7549 gfc_symtree *symtree;
7550 gfc_symtree *this_symtree;
7554 if (sym->attr.flavor == FL_UNKNOWN)
7557 /* If we find that a flavorless symbol is an interface in one of the
7558 parent namespaces, find its symtree in this namespace, free the
7559 symbol and set the symtree to point to the interface symbol. */
7560 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
7562 symtree = gfc_find_symtree (ns->sym_root, sym->name);
7563 if (symtree && symtree->n.sym->generic)
7565 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
7569 gfc_free_symbol (sym);
7570 symtree->n.sym->refs++;
7571 this_symtree->n.sym = symtree->n.sym;
7576 /* Otherwise give it a flavor according to such attributes as
7578 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
7579 sym->attr.flavor = FL_VARIABLE;
7582 sym->attr.flavor = FL_PROCEDURE;
7583 if (sym->attr.dimension)
7584 sym->attr.function = 1;
7588 if (sym->attr.procedure && sym->interface
7589 && sym->attr.if_source != IFSRC_DECL)
7591 if (sym->interface->attr.procedure)
7592 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared "
7593 "in a later PROCEDURE statement", sym->interface->name,
7594 sym->name,&sym->declared_at);
7596 /* Get the attributes from the interface (now resolved). */
7597 if (sym->interface->attr.if_source || sym->interface->attr.intrinsic)
7599 sym->ts = sym->interface->ts;
7600 sym->attr.function = sym->interface->attr.function;
7601 sym->attr.subroutine = sym->interface->attr.subroutine;
7602 copy_formal_args (sym, sym->interface);
7604 else if (sym->interface->name[0] != '\0')
7606 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
7607 sym->interface->name, sym->name, &sym->declared_at);
7612 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
7615 /* Symbols that are module procedures with results (functions) have
7616 the types and array specification copied for type checking in
7617 procedures that call them, as well as for saving to a module
7618 file. These symbols can't stand the scrutiny that their results
7620 mp_flag = (sym->result != NULL && sym->result != sym);
7623 /* Make sure that the intrinsic is consistent with its internal
7624 representation. This needs to be done before assigning a default
7625 type to avoid spurious warnings. */
7626 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic)
7628 if (gfc_intrinsic_name (sym->name, 0))
7630 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising)
7631 gfc_warning ("Type specified for intrinsic function '%s' at %L is ignored",
7632 sym->name, &sym->declared_at);
7634 else if (gfc_intrinsic_name (sym->name, 1))
7636 if (sym->ts.type != BT_UNKNOWN)
7638 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type specifier",
7639 sym->name, &sym->declared_at);
7645 gfc_error ("Intrinsic '%s' at %L does not exist", sym->name, &sym->declared_at);
7650 /* Assign default type to symbols that need one and don't have one. */
7651 if (sym->ts.type == BT_UNKNOWN)
7653 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
7654 gfc_set_default_type (sym, 1, NULL);
7656 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
7658 /* The specific case of an external procedure should emit an error
7659 in the case that there is no implicit type. */
7661 gfc_set_default_type (sym, sym->attr.external, NULL);
7664 /* Result may be in another namespace. */
7665 resolve_symbol (sym->result);
7667 sym->ts = sym->result->ts;
7668 sym->as = gfc_copy_array_spec (sym->result->as);
7669 sym->attr.dimension = sym->result->attr.dimension;
7670 sym->attr.pointer = sym->result->attr.pointer;
7671 sym->attr.allocatable = sym->result->attr.allocatable;
7676 /* Assumed size arrays and assumed shape arrays must be dummy
7680 && (sym->as->type == AS_ASSUMED_SIZE
7681 || sym->as->type == AS_ASSUMED_SHAPE)
7682 && sym->attr.dummy == 0)
7684 if (sym->as->type == AS_ASSUMED_SIZE)
7685 gfc_error ("Assumed size array at %L must be a dummy argument",
7688 gfc_error ("Assumed shape array at %L must be a dummy argument",
7693 /* Make sure symbols with known intent or optional are really dummy
7694 variable. Because of ENTRY statement, this has to be deferred
7695 until resolution time. */
7697 if (!sym->attr.dummy
7698 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
7700 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
7704 if (sym->attr.value && !sym->attr.dummy)
7706 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
7707 "it is not a dummy argument", sym->name, &sym->declared_at);
7711 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
7713 gfc_charlen *cl = sym->ts.cl;
7714 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
7716 gfc_error ("Character dummy variable '%s' at %L with VALUE "
7717 "attribute must have constant length",
7718 sym->name, &sym->declared_at);
7722 if (sym->ts.is_c_interop
7723 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
7725 gfc_error ("C interoperable character dummy variable '%s' at %L "
7726 "with VALUE attribute must have length one",
7727 sym->name, &sym->declared_at);
7732 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
7733 do this for something that was implicitly typed because that is handled
7734 in gfc_set_default_type. Handle dummy arguments and procedure
7735 definitions separately. Also, anything that is use associated is not
7736 handled here but instead is handled in the module it is declared in.
7737 Finally, derived type definitions are allowed to be BIND(C) since that
7738 only implies that they're interoperable, and they are checked fully for
7739 interoperability when a variable is declared of that type. */
7740 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
7741 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
7742 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
7746 /* First, make sure the variable is declared at the
7747 module-level scope (J3/04-007, Section 15.3). */
7748 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
7749 sym->attr.in_common == 0)
7751 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
7752 "is neither a COMMON block nor declared at the "
7753 "module level scope", sym->name, &(sym->declared_at));
7756 else if (sym->common_head != NULL)
7758 t = verify_com_block_vars_c_interop (sym->common_head);
7762 /* If type() declaration, we need to verify that the components
7763 of the given type are all C interoperable, etc. */
7764 if (sym->ts.type == BT_DERIVED &&
7765 sym->ts.derived->attr.is_c_interop != 1)
7767 /* Make sure the user marked the derived type as BIND(C). If
7768 not, call the verify routine. This could print an error
7769 for the derived type more than once if multiple variables
7770 of that type are declared. */
7771 if (sym->ts.derived->attr.is_bind_c != 1)
7772 verify_bind_c_derived_type (sym->ts.derived);
7776 /* Verify the variable itself as C interoperable if it
7777 is BIND(C). It is not possible for this to succeed if
7778 the verify_bind_c_derived_type failed, so don't have to handle
7779 any error returned by verify_bind_c_derived_type. */
7780 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
7786 /* clear the is_bind_c flag to prevent reporting errors more than
7787 once if something failed. */
7788 sym->attr.is_bind_c = 0;
7793 /* If a derived type symbol has reached this point, without its
7794 type being declared, we have an error. Notice that most
7795 conditions that produce undefined derived types have already
7796 been dealt with. However, the likes of:
7797 implicit type(t) (t) ..... call foo (t) will get us here if
7798 the type is not declared in the scope of the implicit
7799 statement. Change the type to BT_UNKNOWN, both because it is so
7800 and to prevent an ICE. */
7801 if (sym->ts.type == BT_DERIVED && sym->ts.derived->components == NULL
7802 && !sym->ts.derived->attr.zero_comp)
7804 gfc_error ("The derived type '%s' at %L is of type '%s', "
7805 "which has not been defined", sym->name,
7806 &sym->declared_at, sym->ts.derived->name);
7807 sym->ts.type = BT_UNKNOWN;
7811 /* Unless the derived-type declaration is use associated, Fortran 95
7812 does not allow public entries of private derived types.
7813 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
7815 if (sym->ts.type == BT_DERIVED
7816 && gfc_check_access (sym->attr.access, sym->ns->default_access)
7817 && !gfc_check_access (sym->ts.derived->attr.access,
7818 sym->ts.derived->ns->default_access)
7819 && !sym->ts.derived->attr.use_assoc
7820 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
7821 "of PRIVATE derived type '%s'",
7822 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
7823 : "variable", sym->name, &sym->declared_at,
7824 sym->ts.derived->name) == FAILURE)
7827 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
7828 default initialization is defined (5.1.2.4.4). */
7829 if (sym->ts.type == BT_DERIVED
7831 && sym->attr.intent == INTENT_OUT
7833 && sym->as->type == AS_ASSUMED_SIZE)
7835 for (c = sym->ts.derived->components; c; c = c->next)
7839 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
7840 "ASSUMED SIZE and so cannot have a default initializer",
7841 sym->name, &sym->declared_at);
7847 switch (sym->attr.flavor)
7850 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
7855 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
7860 if (resolve_fl_namelist (sym) == FAILURE)
7865 if (resolve_fl_parameter (sym) == FAILURE)
7873 /* Resolve array specifier. Check as well some constraints
7874 on COMMON blocks. */
7876 check_constant = sym->attr.in_common && !sym->attr.pointer;
7878 /* Set the formal_arg_flag so that check_conflict will not throw
7879 an error for host associated variables in the specification
7880 expression for an array_valued function. */
7881 if (sym->attr.function && sym->as)
7882 formal_arg_flag = 1;
7884 gfc_resolve_array_spec (sym->as, check_constant);
7886 formal_arg_flag = 0;
7888 /* Resolve formal namespaces. */
7889 if (sym->formal_ns && sym->formal_ns != gfc_current_ns)
7890 gfc_resolve (sym->formal_ns);
7892 /* Check threadprivate restrictions. */
7893 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
7894 && (!sym->attr.in_common
7895 && sym->module == NULL
7896 && (sym->ns->proc_name == NULL
7897 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
7898 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
7900 /* If we have come this far we can apply default-initializers, as
7901 described in 14.7.5, to those variables that have not already
7902 been assigned one. */
7903 if (sym->ts.type == BT_DERIVED
7904 && sym->attr.referenced
7905 && sym->ns == gfc_current_ns
7907 && !sym->attr.allocatable
7908 && !sym->attr.alloc_comp)
7910 symbol_attribute *a = &sym->attr;
7912 if ((!a->save && !a->dummy && !a->pointer
7913 && !a->in_common && !a->use_assoc
7914 && !(a->function && sym != sym->result))
7915 || (a->dummy && a->intent == INTENT_OUT))
7916 apply_default_init (sym);
7921 /************* Resolve DATA statements *************/
7925 gfc_data_value *vnode;
7931 /* Advance the values structure to point to the next value in the data list. */
7934 next_data_value (void)
7937 while (mpz_cmp_ui (values.left, 0) == 0)
7939 if (values.vnode->next == NULL)
7942 values.vnode = values.vnode->next;
7943 mpz_set (values.left, values.vnode->repeat);
7951 check_data_variable (gfc_data_variable *var, locus *where)
7957 ar_type mark = AR_UNKNOWN;
7959 mpz_t section_index[GFC_MAX_DIMENSIONS];
7963 if (gfc_resolve_expr (var->expr) == FAILURE)
7967 mpz_init_set_si (offset, 0);
7970 if (e->expr_type != EXPR_VARIABLE)
7971 gfc_internal_error ("check_data_variable(): Bad expression");
7973 if (e->symtree->n.sym->ns->is_block_data
7974 && !e->symtree->n.sym->attr.in_common)
7976 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
7977 e->symtree->n.sym->name, &e->symtree->n.sym->declared_at);
7980 if (e->ref == NULL && e->symtree->n.sym->as)
7982 gfc_error ("DATA array '%s' at %L must be specified in a previous"
7983 " declaration", e->symtree->n.sym->name, where);
7989 mpz_init_set_ui (size, 1);
7996 /* Find the array section reference. */
7997 for (ref = e->ref; ref; ref = ref->next)
7999 if (ref->type != REF_ARRAY)
8001 if (ref->u.ar.type == AR_ELEMENT)
8007 /* Set marks according to the reference pattern. */
8008 switch (ref->u.ar.type)
8016 /* Get the start position of array section. */
8017 gfc_get_section_index (ar, section_index, &offset);
8025 if (gfc_array_size (e, &size) == FAILURE)
8027 gfc_error ("Nonconstant array section at %L in DATA statement",
8036 while (mpz_cmp_ui (size, 0) > 0)
8038 if (next_data_value () == FAILURE)
8040 gfc_error ("DATA statement at %L has more variables than values",
8046 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
8050 /* If we have more than one element left in the repeat count,
8051 and we have more than one element left in the target variable,
8052 then create a range assignment. */
8053 /* FIXME: Only done for full arrays for now, since array sections
8055 if (mark == AR_FULL && ref && ref->next == NULL
8056 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
8060 if (mpz_cmp (size, values.left) >= 0)
8062 mpz_init_set (range, values.left);
8063 mpz_sub (size, size, values.left);
8064 mpz_set_ui (values.left, 0);
8068 mpz_init_set (range, size);
8069 mpz_sub (values.left, values.left, size);
8070 mpz_set_ui (size, 0);
8073 gfc_assign_data_value_range (var->expr, values.vnode->expr,
8076 mpz_add (offset, offset, range);
8080 /* Assign initial value to symbol. */
8083 mpz_sub_ui (values.left, values.left, 1);
8084 mpz_sub_ui (size, size, 1);
8086 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
8090 if (mark == AR_FULL)
8091 mpz_add_ui (offset, offset, 1);
8093 /* Modify the array section indexes and recalculate the offset
8094 for next element. */
8095 else if (mark == AR_SECTION)
8096 gfc_advance_section (section_index, ar, &offset);
8100 if (mark == AR_SECTION)
8102 for (i = 0; i < ar->dimen; i++)
8103 mpz_clear (section_index[i]);
8113 static try traverse_data_var (gfc_data_variable *, locus *);
8115 /* Iterate over a list of elements in a DATA statement. */
8118 traverse_data_list (gfc_data_variable *var, locus *where)
8121 iterator_stack frame;
8122 gfc_expr *e, *start, *end, *step;
8123 try retval = SUCCESS;
8125 mpz_init (frame.value);
8127 start = gfc_copy_expr (var->iter.start);
8128 end = gfc_copy_expr (var->iter.end);
8129 step = gfc_copy_expr (var->iter.step);
8131 if (gfc_simplify_expr (start, 1) == FAILURE
8132 || start->expr_type != EXPR_CONSTANT)
8134 gfc_error ("iterator start at %L does not simplify", &start->where);
8138 if (gfc_simplify_expr (end, 1) == FAILURE
8139 || end->expr_type != EXPR_CONSTANT)
8141 gfc_error ("iterator end at %L does not simplify", &end->where);
8145 if (gfc_simplify_expr (step, 1) == FAILURE
8146 || step->expr_type != EXPR_CONSTANT)
8148 gfc_error ("iterator step at %L does not simplify", &step->where);
8153 mpz_init_set (trip, end->value.integer);
8154 mpz_sub (trip, trip, start->value.integer);
8155 mpz_add (trip, trip, step->value.integer);
8157 mpz_div (trip, trip, step->value.integer);
8159 mpz_set (frame.value, start->value.integer);
8161 frame.prev = iter_stack;
8162 frame.variable = var->iter.var->symtree;
8163 iter_stack = &frame;
8165 while (mpz_cmp_ui (trip, 0) > 0)
8167 if (traverse_data_var (var->list, where) == FAILURE)
8174 e = gfc_copy_expr (var->expr);
8175 if (gfc_simplify_expr (e, 1) == FAILURE)
8183 mpz_add (frame.value, frame.value, step->value.integer);
8185 mpz_sub_ui (trip, trip, 1);
8190 mpz_clear (frame.value);
8192 gfc_free_expr (start);
8193 gfc_free_expr (end);
8194 gfc_free_expr (step);
8196 iter_stack = frame.prev;
8201 /* Type resolve variables in the variable list of a DATA statement. */
8204 traverse_data_var (gfc_data_variable *var, locus *where)
8208 for (; var; var = var->next)
8210 if (var->expr == NULL)
8211 t = traverse_data_list (var, where);
8213 t = check_data_variable (var, where);
8223 /* Resolve the expressions and iterators associated with a data statement.
8224 This is separate from the assignment checking because data lists should
8225 only be resolved once. */
8228 resolve_data_variables (gfc_data_variable *d)
8230 for (; d; d = d->next)
8232 if (d->list == NULL)
8234 if (gfc_resolve_expr (d->expr) == FAILURE)
8239 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
8242 if (resolve_data_variables (d->list) == FAILURE)
8251 /* Resolve a single DATA statement. We implement this by storing a pointer to
8252 the value list into static variables, and then recursively traversing the
8253 variables list, expanding iterators and such. */
8256 resolve_data (gfc_data *d)
8259 if (resolve_data_variables (d->var) == FAILURE)
8262 values.vnode = d->value;
8263 if (d->value == NULL)
8264 mpz_set_ui (values.left, 0);
8266 mpz_set (values.left, d->value->repeat);
8268 if (traverse_data_var (d->var, &d->where) == FAILURE)
8271 /* At this point, we better not have any values left. */
8273 if (next_data_value () == SUCCESS)
8274 gfc_error ("DATA statement at %L has more values than variables",
8279 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
8280 accessed by host or use association, is a dummy argument to a pure function,
8281 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
8282 is storage associated with any such variable, shall not be used in the
8283 following contexts: (clients of this function). */
8285 /* Determines if a variable is not 'pure', ie not assignable within a pure
8286 procedure. Returns zero if assignment is OK, nonzero if there is a
8289 gfc_impure_variable (gfc_symbol *sym)
8293 if (sym->attr.use_assoc || sym->attr.in_common)
8296 if (sym->ns != gfc_current_ns)
8297 return !sym->attr.function;
8299 proc = sym->ns->proc_name;
8300 if (sym->attr.dummy && gfc_pure (proc)
8301 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
8303 proc->attr.function))
8306 /* TODO: Sort out what can be storage associated, if anything, and include
8307 it here. In principle equivalences should be scanned but it does not
8308 seem to be possible to storage associate an impure variable this way. */
8313 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
8314 symbol of the current procedure. */
8317 gfc_pure (gfc_symbol *sym)
8319 symbol_attribute attr;
8322 sym = gfc_current_ns->proc_name;
8328 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
8332 /* Test whether the current procedure is elemental or not. */
8335 gfc_elemental (gfc_symbol *sym)
8337 symbol_attribute attr;
8340 sym = gfc_current_ns->proc_name;
8345 return attr.flavor == FL_PROCEDURE && attr.elemental;
8349 /* Warn about unused labels. */
8352 warn_unused_fortran_label (gfc_st_label *label)
8357 warn_unused_fortran_label (label->left);
8359 if (label->defined == ST_LABEL_UNKNOWN)
8362 switch (label->referenced)
8364 case ST_LABEL_UNKNOWN:
8365 gfc_warning ("Label %d at %L defined but not used", label->value,
8369 case ST_LABEL_BAD_TARGET:
8370 gfc_warning ("Label %d at %L defined but cannot be used",
8371 label->value, &label->where);
8378 warn_unused_fortran_label (label->right);
8382 /* Returns the sequence type of a symbol or sequence. */
8385 sequence_type (gfc_typespec ts)
8394 if (ts.derived->components == NULL)
8395 return SEQ_NONDEFAULT;
8397 result = sequence_type (ts.derived->components->ts);
8398 for (c = ts.derived->components->next; c; c = c->next)
8399 if (sequence_type (c->ts) != result)
8405 if (ts.kind != gfc_default_character_kind)
8406 return SEQ_NONDEFAULT;
8408 return SEQ_CHARACTER;
8411 if (ts.kind != gfc_default_integer_kind)
8412 return SEQ_NONDEFAULT;
8417 if (!(ts.kind == gfc_default_real_kind
8418 || ts.kind == gfc_default_double_kind))
8419 return SEQ_NONDEFAULT;
8424 if (ts.kind != gfc_default_complex_kind)
8425 return SEQ_NONDEFAULT;
8430 if (ts.kind != gfc_default_logical_kind)
8431 return SEQ_NONDEFAULT;
8436 return SEQ_NONDEFAULT;
8441 /* Resolve derived type EQUIVALENCE object. */
8444 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
8447 gfc_component *c = derived->components;
8452 /* Shall not be an object of nonsequence derived type. */
8453 if (!derived->attr.sequence)
8455 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
8456 "attribute to be an EQUIVALENCE object", sym->name,
8461 /* Shall not have allocatable components. */
8462 if (derived->attr.alloc_comp)
8464 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
8465 "components to be an EQUIVALENCE object",sym->name,
8470 for (; c ; c = c->next)
8474 && (resolve_equivalence_derived (c->ts.derived, sym, e) == FAILURE))
8477 /* Shall not be an object of sequence derived type containing a pointer
8478 in the structure. */
8481 gfc_error ("Derived type variable '%s' at %L with pointer "
8482 "component(s) cannot be an EQUIVALENCE object",
8483 sym->name, &e->where);
8491 /* Resolve equivalence object.
8492 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
8493 an allocatable array, an object of nonsequence derived type, an object of
8494 sequence derived type containing a pointer at any level of component
8495 selection, an automatic object, a function name, an entry name, a result
8496 name, a named constant, a structure component, or a subobject of any of
8497 the preceding objects. A substring shall not have length zero. A
8498 derived type shall not have components with default initialization nor
8499 shall two objects of an equivalence group be initialized.
8500 Either all or none of the objects shall have an protected attribute.
8501 The simple constraints are done in symbol.c(check_conflict) and the rest
8502 are implemented here. */
8505 resolve_equivalence (gfc_equiv *eq)
8508 gfc_symbol *derived;
8509 gfc_symbol *first_sym;
8512 locus *last_where = NULL;
8513 seq_type eq_type, last_eq_type;
8514 gfc_typespec *last_ts;
8515 int object, cnt_protected;
8516 const char *value_name;
8520 last_ts = &eq->expr->symtree->n.sym->ts;
8522 first_sym = eq->expr->symtree->n.sym;
8526 for (object = 1; eq; eq = eq->eq, object++)
8530 e->ts = e->symtree->n.sym->ts;
8531 /* match_varspec might not know yet if it is seeing
8532 array reference or substring reference, as it doesn't
8534 if (e->ref && e->ref->type == REF_ARRAY)
8536 gfc_ref *ref = e->ref;
8537 sym = e->symtree->n.sym;
8539 if (sym->attr.dimension)
8541 ref->u.ar.as = sym->as;
8545 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
8546 if (e->ts.type == BT_CHARACTER
8548 && ref->type == REF_ARRAY
8549 && ref->u.ar.dimen == 1
8550 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
8551 && ref->u.ar.stride[0] == NULL)
8553 gfc_expr *start = ref->u.ar.start[0];
8554 gfc_expr *end = ref->u.ar.end[0];
8557 /* Optimize away the (:) reference. */
8558 if (start == NULL && end == NULL)
8563 e->ref->next = ref->next;
8568 ref->type = REF_SUBSTRING;
8570 start = gfc_int_expr (1);
8571 ref->u.ss.start = start;
8572 if (end == NULL && e->ts.cl)
8573 end = gfc_copy_expr (e->ts.cl->length);
8574 ref->u.ss.end = end;
8575 ref->u.ss.length = e->ts.cl;
8582 /* Any further ref is an error. */
8585 gcc_assert (ref->type == REF_ARRAY);
8586 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
8592 if (gfc_resolve_expr (e) == FAILURE)
8595 sym = e->symtree->n.sym;
8597 if (sym->attr.protected)
8599 if (cnt_protected > 0 && cnt_protected != object)
8601 gfc_error ("Either all or none of the objects in the "
8602 "EQUIVALENCE set at %L shall have the "
8603 "PROTECTED attribute",
8608 /* Shall not equivalence common block variables in a PURE procedure. */
8609 if (sym->ns->proc_name
8610 && sym->ns->proc_name->attr.pure
8611 && sym->attr.in_common)
8613 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
8614 "object in the pure procedure '%s'",
8615 sym->name, &e->where, sym->ns->proc_name->name);
8619 /* Shall not be a named constant. */
8620 if (e->expr_type == EXPR_CONSTANT)
8622 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
8623 "object", sym->name, &e->where);
8627 derived = e->ts.derived;
8628 if (derived && resolve_equivalence_derived (derived, sym, e) == FAILURE)
8631 /* Check that the types correspond correctly:
8633 A numeric sequence structure may be equivalenced to another sequence
8634 structure, an object of default integer type, default real type, double
8635 precision real type, default logical type such that components of the
8636 structure ultimately only become associated to objects of the same
8637 kind. A character sequence structure may be equivalenced to an object
8638 of default character kind or another character sequence structure.
8639 Other objects may be equivalenced only to objects of the same type and
8642 /* Identical types are unconditionally OK. */
8643 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
8644 goto identical_types;
8646 last_eq_type = sequence_type (*last_ts);
8647 eq_type = sequence_type (sym->ts);
8649 /* Since the pair of objects is not of the same type, mixed or
8650 non-default sequences can be rejected. */
8652 msg = "Sequence %s with mixed components in EQUIVALENCE "
8653 "statement at %L with different type objects";
8655 && last_eq_type == SEQ_MIXED
8656 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
8658 || (eq_type == SEQ_MIXED
8659 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
8660 &e->where) == FAILURE))
8663 msg = "Non-default type object or sequence %s in EQUIVALENCE "
8664 "statement at %L with objects of different type";
8666 && last_eq_type == SEQ_NONDEFAULT
8667 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
8668 last_where) == FAILURE)
8669 || (eq_type == SEQ_NONDEFAULT
8670 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
8671 &e->where) == FAILURE))
8674 msg ="Non-CHARACTER object '%s' in default CHARACTER "
8675 "EQUIVALENCE statement at %L";
8676 if (last_eq_type == SEQ_CHARACTER
8677 && eq_type != SEQ_CHARACTER
8678 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
8679 &e->where) == FAILURE)
8682 msg ="Non-NUMERIC object '%s' in default NUMERIC "
8683 "EQUIVALENCE statement at %L";
8684 if (last_eq_type == SEQ_NUMERIC
8685 && eq_type != SEQ_NUMERIC
8686 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
8687 &e->where) == FAILURE)
8692 last_where = &e->where;
8697 /* Shall not be an automatic array. */
8698 if (e->ref->type == REF_ARRAY
8699 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
8701 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
8702 "an EQUIVALENCE object", sym->name, &e->where);
8709 /* Shall not be a structure component. */
8710 if (r->type == REF_COMPONENT)
8712 gfc_error ("Structure component '%s' at %L cannot be an "
8713 "EQUIVALENCE object",
8714 r->u.c.component->name, &e->where);
8718 /* A substring shall not have length zero. */
8719 if (r->type == REF_SUBSTRING)
8721 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
8723 gfc_error ("Substring at %L has length zero",
8724 &r->u.ss.start->where);
8734 /* Resolve function and ENTRY types, issue diagnostics if needed. */
8737 resolve_fntype (gfc_namespace *ns)
8742 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
8745 /* If there are any entries, ns->proc_name is the entry master
8746 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
8748 sym = ns->entries->sym;
8750 sym = ns->proc_name;
8751 if (sym->result == sym
8752 && sym->ts.type == BT_UNKNOWN
8753 && gfc_set_default_type (sym, 0, NULL) == FAILURE
8754 && !sym->attr.untyped)
8756 gfc_error ("Function '%s' at %L has no IMPLICIT type",
8757 sym->name, &sym->declared_at);
8758 sym->attr.untyped = 1;
8761 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.use_assoc
8762 && !gfc_check_access (sym->ts.derived->attr.access,
8763 sym->ts.derived->ns->default_access)
8764 && gfc_check_access (sym->attr.access, sym->ns->default_access))
8766 gfc_error ("PUBLIC function '%s' at %L cannot be of PRIVATE type '%s'",
8767 sym->name, &sym->declared_at, sym->ts.derived->name);
8771 for (el = ns->entries->next; el; el = el->next)
8773 if (el->sym->result == el->sym
8774 && el->sym->ts.type == BT_UNKNOWN
8775 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
8776 && !el->sym->attr.untyped)
8778 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
8779 el->sym->name, &el->sym->declared_at);
8780 el->sym->attr.untyped = 1;
8785 /* 12.3.2.1.1 Defined operators. */
8788 gfc_resolve_uops (gfc_symtree *symtree)
8792 gfc_formal_arglist *formal;
8794 if (symtree == NULL)
8797 gfc_resolve_uops (symtree->left);
8798 gfc_resolve_uops (symtree->right);
8800 for (itr = symtree->n.uop->operator; itr; itr = itr->next)
8803 if (!sym->attr.function)
8804 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
8805 sym->name, &sym->declared_at);
8807 if (sym->ts.type == BT_CHARACTER
8808 && !(sym->ts.cl && sym->ts.cl->length)
8809 && !(sym->result && sym->result->ts.cl
8810 && sym->result->ts.cl->length))
8811 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
8812 "character length", sym->name, &sym->declared_at);
8814 formal = sym->formal;
8815 if (!formal || !formal->sym)
8817 gfc_error ("User operator procedure '%s' at %L must have at least "
8818 "one argument", sym->name, &sym->declared_at);
8822 if (formal->sym->attr.intent != INTENT_IN)
8823 gfc_error ("First argument of operator interface at %L must be "
8824 "INTENT(IN)", &sym->declared_at);
8826 if (formal->sym->attr.optional)
8827 gfc_error ("First argument of operator interface at %L cannot be "
8828 "optional", &sym->declared_at);
8830 formal = formal->next;
8831 if (!formal || !formal->sym)
8834 if (formal->sym->attr.intent != INTENT_IN)
8835 gfc_error ("Second argument of operator interface at %L must be "
8836 "INTENT(IN)", &sym->declared_at);
8838 if (formal->sym->attr.optional)
8839 gfc_error ("Second argument of operator interface at %L cannot be "
8840 "optional", &sym->declared_at);
8843 gfc_error ("Operator interface at %L must have, at most, two "
8844 "arguments", &sym->declared_at);
8849 /* Examine all of the expressions associated with a program unit,
8850 assign types to all intermediate expressions, make sure that all
8851 assignments are to compatible types and figure out which names
8852 refer to which functions or subroutines. It doesn't check code
8853 block, which is handled by resolve_code. */
8856 resolve_types (gfc_namespace *ns)
8863 gfc_current_ns = ns;
8865 resolve_entries (ns);
8867 resolve_common_blocks (ns->common_root);
8869 resolve_contained_functions (ns);
8871 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
8873 for (cl = ns->cl_list; cl; cl = cl->next)
8874 resolve_charlen (cl);
8876 gfc_traverse_ns (ns, resolve_symbol);
8878 resolve_fntype (ns);
8880 for (n = ns->contained; n; n = n->sibling)
8882 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
8883 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
8884 "also be PURE", n->proc_name->name,
8885 &n->proc_name->declared_at);
8891 gfc_check_interfaces (ns);
8893 gfc_traverse_ns (ns, resolve_values);
8899 for (d = ns->data; d; d = d->next)
8903 gfc_traverse_ns (ns, gfc_formalize_init_value);
8905 gfc_traverse_ns (ns, gfc_verify_binding_labels);
8907 if (ns->common_root != NULL)
8908 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
8910 for (eq = ns->equiv; eq; eq = eq->next)
8911 resolve_equivalence (eq);
8913 /* Warn about unused labels. */
8914 if (warn_unused_label)
8915 warn_unused_fortran_label (ns->st_labels);
8917 gfc_resolve_uops (ns->uop_root);
8921 /* Call resolve_code recursively. */
8924 resolve_codes (gfc_namespace *ns)
8928 for (n = ns->contained; n; n = n->sibling)
8931 gfc_current_ns = ns;
8933 /* Set to an out of range value. */
8934 current_entry_id = -1;
8936 bitmap_obstack_initialize (&labels_obstack);
8937 resolve_code (ns->code, ns);
8938 bitmap_obstack_release (&labels_obstack);
8942 /* This function is called after a complete program unit has been compiled.
8943 Its purpose is to examine all of the expressions associated with a program
8944 unit, assign types to all intermediate expressions, make sure that all
8945 assignments are to compatible types and figure out which names refer to
8946 which functions or subroutines. */
8949 gfc_resolve (gfc_namespace *ns)
8951 gfc_namespace *old_ns;
8953 old_ns = gfc_current_ns;
8958 gfc_current_ns = old_ns;