Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / gcc / fortran / resolve.c
blobde2da6355ecfe3cb8579e87d81870e4108c923ef
1 /* Perform type resolution on the various stuctures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor,Boston, MA
20 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "gfortran.h"
26 #include "arith.h" /* For gfc_compare_expr(). */
27 #include "dependency.h"
29 /* Types used in equivalence statements. */
31 typedef enum seq_type
33 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
35 seq_type;
37 /* Stack to push the current if we descend into a block during
38 resolution. See resolve_branch() and resolve_code(). */
40 typedef struct code_stack
42 struct gfc_code *head, *current;
43 struct code_stack *prev;
45 code_stack;
47 static code_stack *cs_base = NULL;
50 /* Nonzero if we're inside a FORALL block */
52 static int forall_flag;
54 /* Nonzero if we are processing a formal arglist. The corresponding function
55 resets the flag each time that it is read. */
56 static int formal_arg_flag = 0;
58 int
59 gfc_is_formal_arg (void)
61 return formal_arg_flag;
64 /* Resolve types of formal argument lists. These have to be done early so that
65 the formal argument lists of module procedures can be copied to the
66 containing module before the individual procedures are resolved
67 individually. We also resolve argument lists of procedures in interface
68 blocks because they are self-contained scoping units.
70 Since a dummy argument cannot be a non-dummy procedure, the only
71 resort left for untyped names are the IMPLICIT types. */
73 static void
74 resolve_formal_arglist (gfc_symbol * proc)
76 gfc_formal_arglist *f;
77 gfc_symbol *sym;
78 int i;
80 /* TODO: Procedures whose return character length parameter is not constant
81 or assumed must also have explicit interfaces. */
82 if (proc->result != NULL)
83 sym = proc->result;
84 else
85 sym = proc;
87 if (gfc_elemental (proc)
88 || sym->attr.pointer || sym->attr.allocatable
89 || (sym->as && sym->as->rank > 0))
90 proc->attr.always_explicit = 1;
92 formal_arg_flag = 1;
94 for (f = proc->formal; f; f = f->next)
96 sym = f->sym;
98 if (sym == NULL)
100 /* Alternate return placeholder. */
101 if (gfc_elemental (proc))
102 gfc_error ("Alternate return specifier in elemental subroutine "
103 "'%s' at %L is not allowed", proc->name,
104 &proc->declared_at);
105 if (proc->attr.function)
106 gfc_error ("Alternate return specifier in function "
107 "'%s' at %L is not allowed", proc->name,
108 &proc->declared_at);
109 continue;
112 if (sym->attr.if_source != IFSRC_UNKNOWN)
113 resolve_formal_arglist (sym);
115 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
117 if (gfc_pure (proc) && !gfc_pure (sym))
119 gfc_error
120 ("Dummy procedure '%s' of PURE procedure at %L must also "
121 "be PURE", sym->name, &sym->declared_at);
122 continue;
125 if (gfc_elemental (proc))
127 gfc_error
128 ("Dummy procedure at %L not allowed in ELEMENTAL procedure",
129 &sym->declared_at);
130 continue;
133 continue;
136 if (sym->ts.type == BT_UNKNOWN)
138 if (!sym->attr.function || sym->result == sym)
139 gfc_set_default_type (sym, 1, sym->ns);
142 gfc_resolve_array_spec (sym->as, 0);
144 /* We can't tell if an array with dimension (:) is assumed or deferred
145 shape until we know if it has the pointer or allocatable attributes.
147 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
148 && !(sym->attr.pointer || sym->attr.allocatable))
150 sym->as->type = AS_ASSUMED_SHAPE;
151 for (i = 0; i < sym->as->rank; i++)
152 sym->as->lower[i] = gfc_int_expr (1);
155 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
156 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
157 || sym->attr.optional)
158 proc->attr.always_explicit = 1;
160 /* If the flavor is unknown at this point, it has to be a variable.
161 A procedure specification would have already set the type. */
163 if (sym->attr.flavor == FL_UNKNOWN)
164 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
166 if (gfc_pure (proc))
168 if (proc->attr.function && !sym->attr.pointer
169 && sym->attr.flavor != FL_PROCEDURE
170 && sym->attr.intent != INTENT_IN)
172 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
173 "INTENT(IN)", sym->name, proc->name,
174 &sym->declared_at);
176 if (proc->attr.subroutine && !sym->attr.pointer
177 && sym->attr.intent == INTENT_UNKNOWN)
179 gfc_error
180 ("Argument '%s' of pure subroutine '%s' at %L must have "
181 "its INTENT specified", sym->name, proc->name,
182 &sym->declared_at);
186 if (gfc_elemental (proc))
188 if (sym->as != NULL)
190 gfc_error
191 ("Argument '%s' of elemental procedure at %L must be scalar",
192 sym->name, &sym->declared_at);
193 continue;
196 if (sym->attr.pointer)
198 gfc_error
199 ("Argument '%s' of elemental procedure at %L cannot have "
200 "the POINTER attribute", sym->name, &sym->declared_at);
201 continue;
205 /* Each dummy shall be specified to be scalar. */
206 if (proc->attr.proc == PROC_ST_FUNCTION)
208 if (sym->as != NULL)
210 gfc_error
211 ("Argument '%s' of statement function at %L must be scalar",
212 sym->name, &sym->declared_at);
213 continue;
216 if (sym->ts.type == BT_CHARACTER)
218 gfc_charlen *cl = sym->ts.cl;
219 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
221 gfc_error
222 ("Character-valued argument '%s' of statement function at "
223 "%L must has constant length",
224 sym->name, &sym->declared_at);
225 continue;
230 formal_arg_flag = 0;
234 /* Work function called when searching for symbols that have argument lists
235 associated with them. */
237 static void
238 find_arglists (gfc_symbol * sym)
241 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
242 return;
244 resolve_formal_arglist (sym);
248 /* Given a namespace, resolve all formal argument lists within the namespace.
251 static void
252 resolve_formal_arglists (gfc_namespace * ns)
255 if (ns == NULL)
256 return;
258 gfc_traverse_ns (ns, find_arglists);
262 static void
263 resolve_contained_fntype (gfc_symbol * sym, gfc_namespace * ns)
265 try t;
267 /* If this namespace is not a function, ignore it. */
268 if (! sym
269 || !(sym->attr.function
270 || sym->attr.flavor == FL_VARIABLE))
271 return;
273 /* Try to find out of what the return type is. */
274 if (sym->result != NULL)
275 sym = sym->result;
277 if (sym->ts.type == BT_UNKNOWN)
279 t = gfc_set_default_type (sym, 0, ns);
281 if (t == FAILURE && !sym->attr.untyped)
283 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
284 sym->name, &sym->declared_at); /* FIXME */
285 sym->attr.untyped = 1;
289 /*Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character type,
290 lists the only ways a character length value of * can be used: dummy arguments
291 of procedures, named constants, and function results in external functions.
292 Internal function results are not on that list; ergo, not permitted. */
294 if (sym->ts.type == BT_CHARACTER)
296 gfc_charlen *cl = sym->ts.cl;
297 if (!cl || !cl->length)
298 gfc_error ("Character-valued internal function '%s' at %L must "
299 "not be assumed length", sym->name, &sym->declared_at);
304 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
305 introduce duplicates. */
307 static void
308 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
310 gfc_formal_arglist *f, *new_arglist;
311 gfc_symbol *new_sym;
313 for (; new_args != NULL; new_args = new_args->next)
315 new_sym = new_args->sym;
316 /* See if ths arg is already in the formal argument list. */
317 for (f = proc->formal; f; f = f->next)
319 if (new_sym == f->sym)
320 break;
323 if (f)
324 continue;
326 /* Add a new argument. Argument order is not important. */
327 new_arglist = gfc_get_formal_arglist ();
328 new_arglist->sym = new_sym;
329 new_arglist->next = proc->formal;
330 proc->formal = new_arglist;
335 /* Resolve alternate entry points. If a symbol has multiple entry points we
336 create a new master symbol for the main routine, and turn the existing
337 symbol into an entry point. */
339 static void
340 resolve_entries (gfc_namespace * ns)
342 gfc_namespace *old_ns;
343 gfc_code *c;
344 gfc_symbol *proc;
345 gfc_entry_list *el;
346 char name[GFC_MAX_SYMBOL_LEN + 1];
347 static int master_count = 0;
349 if (ns->proc_name == NULL)
350 return;
352 /* No need to do anything if this procedure doesn't have alternate entry
353 points. */
354 if (!ns->entries)
355 return;
357 /* We may already have resolved alternate entry points. */
358 if (ns->proc_name->attr.entry_master)
359 return;
361 /* If this isn't a procedure something has gone horribly wrong. */
362 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
364 /* Remember the current namespace. */
365 old_ns = gfc_current_ns;
367 gfc_current_ns = ns;
369 /* Add the main entry point to the list of entry points. */
370 el = gfc_get_entry_list ();
371 el->sym = ns->proc_name;
372 el->id = 0;
373 el->next = ns->entries;
374 ns->entries = el;
375 ns->proc_name->attr.entry = 1;
377 /* Add an entry statement for it. */
378 c = gfc_get_code ();
379 c->op = EXEC_ENTRY;
380 c->ext.entry = el;
381 c->next = ns->code;
382 ns->code = c;
384 /* Create a new symbol for the master function. */
385 /* Give the internal function a unique name (within this file).
386 Also include the function name so the user has some hope of figuring
387 out what is going on. */
388 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
389 master_count++, ns->proc_name->name);
390 gfc_get_ha_symbol (name, &proc);
391 gcc_assert (proc != NULL);
393 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
394 if (ns->proc_name->attr.subroutine)
395 gfc_add_subroutine (&proc->attr, proc->name, NULL);
396 else
398 gfc_symbol *sym;
399 gfc_typespec *ts, *fts;
401 gfc_add_function (&proc->attr, proc->name, NULL);
402 proc->result = proc;
403 fts = &ns->entries->sym->result->ts;
404 if (fts->type == BT_UNKNOWN)
405 fts = gfc_get_default_type (ns->entries->sym->result, NULL);
406 for (el = ns->entries->next; el; el = el->next)
408 ts = &el->sym->result->ts;
409 if (ts->type == BT_UNKNOWN)
410 ts = gfc_get_default_type (el->sym->result, NULL);
411 if (! gfc_compare_types (ts, fts)
412 || (el->sym->result->attr.dimension
413 != ns->entries->sym->result->attr.dimension)
414 || (el->sym->result->attr.pointer
415 != ns->entries->sym->result->attr.pointer))
416 break;
419 if (el == NULL)
421 sym = ns->entries->sym->result;
422 /* All result types the same. */
423 proc->ts = *fts;
424 if (sym->attr.dimension)
425 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
426 if (sym->attr.pointer)
427 gfc_add_pointer (&proc->attr, NULL);
429 else
431 /* Otherwise the result will be passed through a union by
432 reference. */
433 proc->attr.mixed_entry_master = 1;
434 for (el = ns->entries; el; el = el->next)
436 sym = el->sym->result;
437 if (sym->attr.dimension)
439 if (el == ns->entries)
440 gfc_error
441 ("FUNCTION result %s can't be an array in FUNCTION %s at %L",
442 sym->name, ns->entries->sym->name, &sym->declared_at);
443 else
444 gfc_error
445 ("ENTRY result %s can't be an array in FUNCTION %s at %L",
446 sym->name, ns->entries->sym->name, &sym->declared_at);
448 else if (sym->attr.pointer)
450 if (el == ns->entries)
451 gfc_error
452 ("FUNCTION result %s can't be a POINTER in FUNCTION %s at %L",
453 sym->name, ns->entries->sym->name, &sym->declared_at);
454 else
455 gfc_error
456 ("ENTRY result %s can't be a POINTER in FUNCTION %s at %L",
457 sym->name, ns->entries->sym->name, &sym->declared_at);
459 else
461 ts = &sym->ts;
462 if (ts->type == BT_UNKNOWN)
463 ts = gfc_get_default_type (sym, NULL);
464 switch (ts->type)
466 case BT_INTEGER:
467 if (ts->kind == gfc_default_integer_kind)
468 sym = NULL;
469 break;
470 case BT_REAL:
471 if (ts->kind == gfc_default_real_kind
472 || ts->kind == gfc_default_double_kind)
473 sym = NULL;
474 break;
475 case BT_COMPLEX:
476 if (ts->kind == gfc_default_complex_kind)
477 sym = NULL;
478 break;
479 case BT_LOGICAL:
480 if (ts->kind == gfc_default_logical_kind)
481 sym = NULL;
482 break;
483 case BT_UNKNOWN:
484 /* We will issue error elsewhere. */
485 sym = NULL;
486 break;
487 default:
488 break;
490 if (sym)
492 if (el == ns->entries)
493 gfc_error
494 ("FUNCTION result %s can't be of type %s in FUNCTION %s at %L",
495 sym->name, gfc_typename (ts), ns->entries->sym->name,
496 &sym->declared_at);
497 else
498 gfc_error
499 ("ENTRY result %s can't be of type %s in FUNCTION %s at %L",
500 sym->name, gfc_typename (ts), ns->entries->sym->name,
501 &sym->declared_at);
507 proc->attr.access = ACCESS_PRIVATE;
508 proc->attr.entry_master = 1;
510 /* Merge all the entry point arguments. */
511 for (el = ns->entries; el; el = el->next)
512 merge_argument_lists (proc, el->sym->formal);
514 /* Use the master function for the function body. */
515 ns->proc_name = proc;
517 /* Finalize the new symbols. */
518 gfc_commit_symbols ();
520 /* Restore the original namespace. */
521 gfc_current_ns = old_ns;
525 /* Resolve contained function types. Because contained functions can call one
526 another, they have to be worked out before any of the contained procedures
527 can be resolved.
529 The good news is that if a function doesn't already have a type, the only
530 way it can get one is through an IMPLICIT type or a RESULT variable, because
531 by definition contained functions are contained namespace they're contained
532 in, not in a sibling or parent namespace. */
534 static void
535 resolve_contained_functions (gfc_namespace * ns)
537 gfc_namespace *child;
538 gfc_entry_list *el;
540 resolve_formal_arglists (ns);
542 for (child = ns->contained; child; child = child->sibling)
544 /* Resolve alternate entry points first. */
545 resolve_entries (child);
547 /* Then check function return types. */
548 resolve_contained_fntype (child->proc_name, child);
549 for (el = child->entries; el; el = el->next)
550 resolve_contained_fntype (el->sym, child);
555 /* Resolve all of the elements of a structure constructor and make sure that
556 the types are correct. */
558 static try
559 resolve_structure_cons (gfc_expr * expr)
561 gfc_constructor *cons;
562 gfc_component *comp;
563 try t;
565 t = SUCCESS;
566 cons = expr->value.constructor;
567 /* A constructor may have references if it is the result of substituting a
568 parameter variable. In this case we just pull out the component we
569 want. */
570 if (expr->ref)
571 comp = expr->ref->u.c.sym->components;
572 else
573 comp = expr->ts.derived->components;
575 for (; comp; comp = comp->next, cons = cons->next)
577 if (! cons->expr)
579 t = FAILURE;
580 continue;
583 if (gfc_resolve_expr (cons->expr) == FAILURE)
585 t = FAILURE;
586 continue;
589 /* If we don't have the right type, try to convert it. */
591 if (!gfc_compare_types (&cons->expr->ts, &comp->ts)
592 && gfc_convert_type (cons->expr, &comp->ts, 1) == FAILURE)
593 t = FAILURE;
596 return t;
601 /****************** Expression name resolution ******************/
603 /* Returns 0 if a symbol was not declared with a type or
604 attribute declaration statement, nonzero otherwise. */
606 static int
607 was_declared (gfc_symbol * sym)
609 symbol_attribute a;
611 a = sym->attr;
613 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
614 return 1;
616 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
617 || a.optional || a.pointer || a.save || a.target
618 || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
619 return 1;
621 return 0;
625 /* Determine if a symbol is generic or not. */
627 static int
628 generic_sym (gfc_symbol * sym)
630 gfc_symbol *s;
632 if (sym->attr.generic ||
633 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
634 return 1;
636 if (was_declared (sym) || sym->ns->parent == NULL)
637 return 0;
639 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
641 return (s == NULL) ? 0 : generic_sym (s);
645 /* Determine if a symbol is specific or not. */
647 static int
648 specific_sym (gfc_symbol * sym)
650 gfc_symbol *s;
652 if (sym->attr.if_source == IFSRC_IFBODY
653 || sym->attr.proc == PROC_MODULE
654 || sym->attr.proc == PROC_INTERNAL
655 || sym->attr.proc == PROC_ST_FUNCTION
656 || (sym->attr.intrinsic &&
657 gfc_specific_intrinsic (sym->name))
658 || sym->attr.external)
659 return 1;
661 if (was_declared (sym) || sym->ns->parent == NULL)
662 return 0;
664 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
666 return (s == NULL) ? 0 : specific_sym (s);
670 /* Figure out if the procedure is specific, generic or unknown. */
672 typedef enum
673 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
674 proc_type;
676 static proc_type
677 procedure_kind (gfc_symbol * sym)
680 if (generic_sym (sym))
681 return PTYPE_GENERIC;
683 if (specific_sym (sym))
684 return PTYPE_SPECIFIC;
686 return PTYPE_UNKNOWN;
690 /* Resolve an actual argument list. Most of the time, this is just
691 resolving the expressions in the list.
692 The exception is that we sometimes have to decide whether arguments
693 that look like procedure arguments are really simple variable
694 references. */
696 static try
697 resolve_actual_arglist (gfc_actual_arglist * arg)
699 gfc_symbol *sym;
700 gfc_symtree *parent_st;
701 gfc_expr *e;
703 for (; arg; arg = arg->next)
706 e = arg->expr;
707 if (e == NULL)
709 /* Check the label is a valid branching target. */
710 if (arg->label)
712 if (arg->label->defined == ST_LABEL_UNKNOWN)
714 gfc_error ("Label %d referenced at %L is never defined",
715 arg->label->value, &arg->label->where);
716 return FAILURE;
719 continue;
722 if (e->ts.type != BT_PROCEDURE)
724 if (gfc_resolve_expr (e) != SUCCESS)
725 return FAILURE;
726 continue;
729 /* See if the expression node should really be a variable
730 reference. */
732 sym = e->symtree->n.sym;
734 if (sym->attr.flavor == FL_PROCEDURE
735 || sym->attr.intrinsic
736 || sym->attr.external)
739 if (sym->attr.proc == PROC_ST_FUNCTION)
741 gfc_error ("Statement function '%s' at %L is not allowed as an "
742 "actual argument", sym->name, &e->where);
745 /* If the symbol is the function that names the current (or
746 parent) scope, then we really have a variable reference. */
748 if (sym->attr.function && sym->result == sym
749 && (sym->ns->proc_name == sym
750 || (sym->ns->parent != NULL
751 && sym->ns->parent->proc_name == sym)))
752 goto got_variable;
754 continue;
757 /* See if the name is a module procedure in a parent unit. */
759 if (was_declared (sym) || sym->ns->parent == NULL)
760 goto got_variable;
762 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
764 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
765 return FAILURE;
768 if (parent_st == NULL)
769 goto got_variable;
771 sym = parent_st->n.sym;
772 e->symtree = parent_st; /* Point to the right thing. */
774 if (sym->attr.flavor == FL_PROCEDURE
775 || sym->attr.intrinsic
776 || sym->attr.external)
778 continue;
781 got_variable:
782 e->expr_type = EXPR_VARIABLE;
783 e->ts = sym->ts;
784 if (sym->as != NULL)
786 e->rank = sym->as->rank;
787 e->ref = gfc_get_ref ();
788 e->ref->type = REF_ARRAY;
789 e->ref->u.ar.type = AR_FULL;
790 e->ref->u.ar.as = sym->as;
794 return SUCCESS;
798 /* Go through each actual argument in ACTUAL and see if it can be
799 implemented as an inlined, non-copying intrinsic. FNSYM is the
800 function being called, or NULL if not known. */
802 static void
803 find_noncopying_intrinsics (gfc_symbol * fnsym, gfc_actual_arglist * actual)
805 gfc_actual_arglist *ap;
806 gfc_expr *expr;
808 for (ap = actual; ap; ap = ap->next)
809 if (ap->expr
810 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
811 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual))
812 ap->expr->inline_noncopying_intrinsic = 1;
816 /************* Function resolution *************/
818 /* Resolve a function call known to be generic.
819 Section 14.1.2.4.1. */
821 static match
822 resolve_generic_f0 (gfc_expr * expr, gfc_symbol * sym)
824 gfc_symbol *s;
826 if (sym->attr.generic)
829 gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
830 if (s != NULL)
832 expr->value.function.name = s->name;
833 expr->value.function.esym = s;
834 expr->ts = s->ts;
835 if (s->as != NULL)
836 expr->rank = s->as->rank;
837 return MATCH_YES;
840 /* TODO: Need to search for elemental references in generic interface */
843 if (sym->attr.intrinsic)
844 return gfc_intrinsic_func_interface (expr, 0);
846 return MATCH_NO;
850 static try
851 resolve_generic_f (gfc_expr * expr)
853 gfc_symbol *sym;
854 match m;
856 sym = expr->symtree->n.sym;
858 for (;;)
860 m = resolve_generic_f0 (expr, sym);
861 if (m == MATCH_YES)
862 return SUCCESS;
863 else if (m == MATCH_ERROR)
864 return FAILURE;
866 generic:
867 if (sym->ns->parent == NULL)
868 break;
869 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
871 if (sym == NULL)
872 break;
873 if (!generic_sym (sym))
874 goto generic;
877 /* Last ditch attempt. */
879 if (!gfc_generic_intrinsic (expr->symtree->n.sym->name))
881 gfc_error ("Generic function '%s' at %L is not an intrinsic function",
882 expr->symtree->n.sym->name, &expr->where);
883 return FAILURE;
886 m = gfc_intrinsic_func_interface (expr, 0);
887 if (m == MATCH_YES)
888 return SUCCESS;
889 if (m == MATCH_NO)
890 gfc_error
891 ("Generic function '%s' at %L is not consistent with a specific "
892 "intrinsic interface", expr->symtree->n.sym->name, &expr->where);
894 return FAILURE;
898 /* Resolve a function call known to be specific. */
900 static match
901 resolve_specific_f0 (gfc_symbol * sym, gfc_expr * expr)
903 match m;
905 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
907 if (sym->attr.dummy)
909 sym->attr.proc = PROC_DUMMY;
910 goto found;
913 sym->attr.proc = PROC_EXTERNAL;
914 goto found;
917 if (sym->attr.proc == PROC_MODULE
918 || sym->attr.proc == PROC_ST_FUNCTION
919 || sym->attr.proc == PROC_INTERNAL)
920 goto found;
922 if (sym->attr.intrinsic)
924 m = gfc_intrinsic_func_interface (expr, 1);
925 if (m == MATCH_YES)
926 return MATCH_YES;
927 if (m == MATCH_NO)
928 gfc_error
929 ("Function '%s' at %L is INTRINSIC but is not compatible with "
930 "an intrinsic", sym->name, &expr->where);
932 return MATCH_ERROR;
935 return MATCH_NO;
937 found:
938 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
940 expr->ts = sym->ts;
941 expr->value.function.name = sym->name;
942 expr->value.function.esym = sym;
943 if (sym->as != NULL)
944 expr->rank = sym->as->rank;
946 return MATCH_YES;
950 static try
951 resolve_specific_f (gfc_expr * expr)
953 gfc_symbol *sym;
954 match m;
956 sym = expr->symtree->n.sym;
958 for (;;)
960 m = resolve_specific_f0 (sym, expr);
961 if (m == MATCH_YES)
962 return SUCCESS;
963 if (m == MATCH_ERROR)
964 return FAILURE;
966 if (sym->ns->parent == NULL)
967 break;
969 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
971 if (sym == NULL)
972 break;
975 gfc_error ("Unable to resolve the specific function '%s' at %L",
976 expr->symtree->n.sym->name, &expr->where);
978 return SUCCESS;
982 /* Resolve a procedure call not known to be generic nor specific. */
984 static try
985 resolve_unknown_f (gfc_expr * expr)
987 gfc_symbol *sym;
988 gfc_typespec *ts;
990 sym = expr->symtree->n.sym;
992 if (sym->attr.dummy)
994 sym->attr.proc = PROC_DUMMY;
995 expr->value.function.name = sym->name;
996 goto set_type;
999 /* See if we have an intrinsic function reference. */
1001 if (gfc_intrinsic_name (sym->name, 0))
1003 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
1004 return SUCCESS;
1005 return FAILURE;
1008 /* The reference is to an external name. */
1010 sym->attr.proc = PROC_EXTERNAL;
1011 expr->value.function.name = sym->name;
1012 expr->value.function.esym = expr->symtree->n.sym;
1014 if (sym->as != NULL)
1015 expr->rank = sym->as->rank;
1017 /* Type of the expression is either the type of the symbol or the
1018 default type of the symbol. */
1020 set_type:
1021 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1023 if (sym->ts.type != BT_UNKNOWN)
1024 expr->ts = sym->ts;
1025 else
1027 ts = gfc_get_default_type (sym, sym->ns);
1029 if (ts->type == BT_UNKNOWN)
1031 gfc_error ("Function '%s' at %L has no IMPLICIT type",
1032 sym->name, &expr->where);
1033 return FAILURE;
1035 else
1036 expr->ts = *ts;
1039 return SUCCESS;
1043 /* Figure out if a function reference is pure or not. Also set the name
1044 of the function for a potential error message. Return nonzero if the
1045 function is PURE, zero if not. */
1047 static int
1048 pure_function (gfc_expr * e, const char **name)
1050 int pure;
1052 if (e->value.function.esym)
1054 pure = gfc_pure (e->value.function.esym);
1055 *name = e->value.function.esym->name;
1057 else if (e->value.function.isym)
1059 pure = e->value.function.isym->pure
1060 || e->value.function.isym->elemental;
1061 *name = e->value.function.isym->name;
1063 else
1065 /* Implicit functions are not pure. */
1066 pure = 0;
1067 *name = e->value.function.name;
1070 return pure;
1074 /* Resolve a function call, which means resolving the arguments, then figuring
1075 out which entity the name refers to. */
1076 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
1077 to INTENT(OUT) or INTENT(INOUT). */
1079 static try
1080 resolve_function (gfc_expr * expr)
1082 gfc_actual_arglist *arg;
1083 const char *name;
1084 try t;
1086 if (resolve_actual_arglist (expr->value.function.actual) == FAILURE)
1087 return FAILURE;
1089 /* See if function is already resolved. */
1091 if (expr->value.function.name != NULL)
1093 if (expr->ts.type == BT_UNKNOWN)
1094 expr->ts = expr->symtree->n.sym->ts;
1095 t = SUCCESS;
1097 else
1099 /* Apply the rules of section 14.1.2. */
1101 switch (procedure_kind (expr->symtree->n.sym))
1103 case PTYPE_GENERIC:
1104 t = resolve_generic_f (expr);
1105 break;
1107 case PTYPE_SPECIFIC:
1108 t = resolve_specific_f (expr);
1109 break;
1111 case PTYPE_UNKNOWN:
1112 t = resolve_unknown_f (expr);
1113 break;
1115 default:
1116 gfc_internal_error ("resolve_function(): bad function type");
1120 /* If the expression is still a function (it might have simplified),
1121 then we check to see if we are calling an elemental function. */
1123 if (expr->expr_type != EXPR_FUNCTION)
1124 return t;
1126 if (expr->value.function.actual != NULL
1127 && ((expr->value.function.esym != NULL
1128 && expr->value.function.esym->attr.elemental)
1129 || (expr->value.function.isym != NULL
1130 && expr->value.function.isym->elemental)))
1133 /* The rank of an elemental is the rank of its array argument(s). */
1135 for (arg = expr->value.function.actual; arg; arg = arg->next)
1137 if (arg->expr != NULL && arg->expr->rank > 0)
1139 expr->rank = arg->expr->rank;
1140 break;
1145 if (!pure_function (expr, &name))
1147 if (forall_flag)
1149 gfc_error
1150 ("Function reference to '%s' at %L is inside a FORALL block",
1151 name, &expr->where);
1152 t = FAILURE;
1154 else if (gfc_pure (NULL))
1156 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
1157 "procedure within a PURE procedure", name, &expr->where);
1158 t = FAILURE;
1162 if (t == SUCCESS)
1163 find_noncopying_intrinsics (expr->value.function.esym,
1164 expr->value.function.actual);
1165 return t;
1169 /************* Subroutine resolution *************/
1171 static void
1172 pure_subroutine (gfc_code * c, gfc_symbol * sym)
1175 if (gfc_pure (sym))
1176 return;
1178 if (forall_flag)
1179 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
1180 sym->name, &c->loc);
1181 else if (gfc_pure (NULL))
1182 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
1183 &c->loc);
1187 static match
1188 resolve_generic_s0 (gfc_code * c, gfc_symbol * sym)
1190 gfc_symbol *s;
1192 if (sym->attr.generic)
1194 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
1195 if (s != NULL)
1197 c->resolved_sym = s;
1198 pure_subroutine (c, s);
1199 return MATCH_YES;
1202 /* TODO: Need to search for elemental references in generic interface. */
1205 if (sym->attr.intrinsic)
1206 return gfc_intrinsic_sub_interface (c, 0);
1208 return MATCH_NO;
1212 static try
1213 resolve_generic_s (gfc_code * c)
1215 gfc_symbol *sym;
1216 match m;
1218 sym = c->symtree->n.sym;
1220 m = resolve_generic_s0 (c, sym);
1221 if (m == MATCH_YES)
1222 return SUCCESS;
1223 if (m == MATCH_ERROR)
1224 return FAILURE;
1226 if (sym->ns->parent != NULL)
1228 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1229 if (sym != NULL)
1231 m = resolve_generic_s0 (c, sym);
1232 if (m == MATCH_YES)
1233 return SUCCESS;
1234 if (m == MATCH_ERROR)
1235 return FAILURE;
1239 /* Last ditch attempt. */
1241 if (!gfc_generic_intrinsic (sym->name))
1243 gfc_error
1244 ("Generic subroutine '%s' at %L is not an intrinsic subroutine",
1245 sym->name, &c->loc);
1246 return FAILURE;
1249 m = gfc_intrinsic_sub_interface (c, 0);
1250 if (m == MATCH_YES)
1251 return SUCCESS;
1252 if (m == MATCH_NO)
1253 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
1254 "intrinsic subroutine interface", sym->name, &c->loc);
1256 return FAILURE;
1260 /* Resolve a subroutine call known to be specific. */
1262 static match
1263 resolve_specific_s0 (gfc_code * c, gfc_symbol * sym)
1265 match m;
1267 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1269 if (sym->attr.dummy)
1271 sym->attr.proc = PROC_DUMMY;
1272 goto found;
1275 sym->attr.proc = PROC_EXTERNAL;
1276 goto found;
1279 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
1280 goto found;
1282 if (sym->attr.intrinsic)
1284 m = gfc_intrinsic_sub_interface (c, 1);
1285 if (m == MATCH_YES)
1286 return MATCH_YES;
1287 if (m == MATCH_NO)
1288 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
1289 "with an intrinsic", sym->name, &c->loc);
1291 return MATCH_ERROR;
1294 return MATCH_NO;
1296 found:
1297 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1299 c->resolved_sym = sym;
1300 pure_subroutine (c, sym);
1302 return MATCH_YES;
1306 static try
1307 resolve_specific_s (gfc_code * c)
1309 gfc_symbol *sym;
1310 match m;
1312 sym = c->symtree->n.sym;
1314 m = resolve_specific_s0 (c, sym);
1315 if (m == MATCH_YES)
1316 return SUCCESS;
1317 if (m == MATCH_ERROR)
1318 return FAILURE;
1320 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1322 if (sym != NULL)
1324 m = resolve_specific_s0 (c, sym);
1325 if (m == MATCH_YES)
1326 return SUCCESS;
1327 if (m == MATCH_ERROR)
1328 return FAILURE;
1331 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
1332 sym->name, &c->loc);
1334 return FAILURE;
1338 /* Resolve a subroutine call not known to be generic nor specific. */
1340 static try
1341 resolve_unknown_s (gfc_code * c)
1343 gfc_symbol *sym;
1345 sym = c->symtree->n.sym;
1347 if (sym->attr.dummy)
1349 sym->attr.proc = PROC_DUMMY;
1350 goto found;
1353 /* See if we have an intrinsic function reference. */
1355 if (gfc_intrinsic_name (sym->name, 1))
1357 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
1358 return SUCCESS;
1359 return FAILURE;
1362 /* The reference is to an external name. */
1364 found:
1365 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1367 c->resolved_sym = sym;
1369 pure_subroutine (c, sym);
1371 return SUCCESS;
1375 /* Resolve a subroutine call. Although it was tempting to use the same code
1376 for functions, subroutines and functions are stored differently and this
1377 makes things awkward. */
1379 static try
1380 resolve_call (gfc_code * c)
1382 try t;
1384 if (resolve_actual_arglist (c->ext.actual) == FAILURE)
1385 return FAILURE;
1387 t = SUCCESS;
1388 if (c->resolved_sym == NULL)
1389 switch (procedure_kind (c->symtree->n.sym))
1391 case PTYPE_GENERIC:
1392 t = resolve_generic_s (c);
1393 break;
1395 case PTYPE_SPECIFIC:
1396 t = resolve_specific_s (c);
1397 break;
1399 case PTYPE_UNKNOWN:
1400 t = resolve_unknown_s (c);
1401 break;
1403 default:
1404 gfc_internal_error ("resolve_subroutine(): bad function type");
1407 if (t == SUCCESS)
1408 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
1409 return t;
1412 /* Compare the shapes of two arrays that have non-NULL shapes. If both
1413 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
1414 match. If both op1->shape and op2->shape are non-NULL return FAILURE
1415 if their shapes do not match. If either op1->shape or op2->shape is
1416 NULL, return SUCCESS. */
1418 static try
1419 compare_shapes (gfc_expr * op1, gfc_expr * op2)
1421 try t;
1422 int i;
1424 t = SUCCESS;
1426 if (op1->shape != NULL && op2->shape != NULL)
1428 for (i = 0; i < op1->rank; i++)
1430 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
1432 gfc_error ("Shapes for operands at %L and %L are not conformable",
1433 &op1->where, &op2->where);
1434 t = FAILURE;
1435 break;
1440 return t;
1443 /* Resolve an operator expression node. This can involve replacing the
1444 operation with a user defined function call. */
1446 static try
1447 resolve_operator (gfc_expr * e)
1449 gfc_expr *op1, *op2;
1450 char msg[200];
1451 try t;
1453 /* Resolve all subnodes-- give them types. */
1455 switch (e->value.op.operator)
1457 default:
1458 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
1459 return FAILURE;
1461 /* Fall through... */
1463 case INTRINSIC_NOT:
1464 case INTRINSIC_UPLUS:
1465 case INTRINSIC_UMINUS:
1466 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
1467 return FAILURE;
1468 break;
1471 /* Typecheck the new node. */
1473 op1 = e->value.op.op1;
1474 op2 = e->value.op.op2;
1476 switch (e->value.op.operator)
1478 case INTRINSIC_UPLUS:
1479 case INTRINSIC_UMINUS:
1480 if (op1->ts.type == BT_INTEGER
1481 || op1->ts.type == BT_REAL
1482 || op1->ts.type == BT_COMPLEX)
1484 e->ts = op1->ts;
1485 break;
1488 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
1489 gfc_op2string (e->value.op.operator), gfc_typename (&e->ts));
1490 goto bad_op;
1492 case INTRINSIC_PLUS:
1493 case INTRINSIC_MINUS:
1494 case INTRINSIC_TIMES:
1495 case INTRINSIC_DIVIDE:
1496 case INTRINSIC_POWER:
1497 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
1499 gfc_type_convert_binary (e);
1500 break;
1503 sprintf (msg,
1504 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
1505 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1506 gfc_typename (&op2->ts));
1507 goto bad_op;
1509 case INTRINSIC_CONCAT:
1510 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1512 e->ts.type = BT_CHARACTER;
1513 e->ts.kind = op1->ts.kind;
1514 break;
1517 sprintf (msg,
1518 _("Operands of string concatenation operator at %%L are %s/%s"),
1519 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
1520 goto bad_op;
1522 case INTRINSIC_AND:
1523 case INTRINSIC_OR:
1524 case INTRINSIC_EQV:
1525 case INTRINSIC_NEQV:
1526 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
1528 e->ts.type = BT_LOGICAL;
1529 e->ts.kind = gfc_kind_max (op1, op2);
1530 if (op1->ts.kind < e->ts.kind)
1531 gfc_convert_type (op1, &e->ts, 2);
1532 else if (op2->ts.kind < e->ts.kind)
1533 gfc_convert_type (op2, &e->ts, 2);
1534 break;
1537 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
1538 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1539 gfc_typename (&op2->ts));
1541 goto bad_op;
1543 case INTRINSIC_NOT:
1544 if (op1->ts.type == BT_LOGICAL)
1546 e->ts.type = BT_LOGICAL;
1547 e->ts.kind = op1->ts.kind;
1548 break;
1551 sprintf (msg, _("Operand of .NOT. operator at %%L is %s"),
1552 gfc_typename (&op1->ts));
1553 goto bad_op;
1555 case INTRINSIC_GT:
1556 case INTRINSIC_GE:
1557 case INTRINSIC_LT:
1558 case INTRINSIC_LE:
1559 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1561 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
1562 goto bad_op;
1565 /* Fall through... */
1567 case INTRINSIC_EQ:
1568 case INTRINSIC_NE:
1569 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1571 e->ts.type = BT_LOGICAL;
1572 e->ts.kind = gfc_default_logical_kind;
1573 break;
1576 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
1578 gfc_type_convert_binary (e);
1580 e->ts.type = BT_LOGICAL;
1581 e->ts.kind = gfc_default_logical_kind;
1582 break;
1585 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
1586 sprintf (msg,
1587 _("Logicals at %%L must be compared with %s instead of %s"),
1588 e->value.op.operator == INTRINSIC_EQ ? ".EQV." : ".NEQV.",
1589 gfc_op2string (e->value.op.operator));
1590 else
1591 sprintf (msg,
1592 _("Operands of comparison operator '%s' at %%L are %s/%s"),
1593 gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1594 gfc_typename (&op2->ts));
1596 goto bad_op;
1598 case INTRINSIC_USER:
1599 if (op2 == NULL)
1600 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
1601 e->value.op.uop->name, gfc_typename (&op1->ts));
1602 else
1603 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
1604 e->value.op.uop->name, gfc_typename (&op1->ts),
1605 gfc_typename (&op2->ts));
1607 goto bad_op;
1609 default:
1610 gfc_internal_error ("resolve_operator(): Bad intrinsic");
1613 /* Deal with arrayness of an operand through an operator. */
1615 t = SUCCESS;
1617 switch (e->value.op.operator)
1619 case INTRINSIC_PLUS:
1620 case INTRINSIC_MINUS:
1621 case INTRINSIC_TIMES:
1622 case INTRINSIC_DIVIDE:
1623 case INTRINSIC_POWER:
1624 case INTRINSIC_CONCAT:
1625 case INTRINSIC_AND:
1626 case INTRINSIC_OR:
1627 case INTRINSIC_EQV:
1628 case INTRINSIC_NEQV:
1629 case INTRINSIC_EQ:
1630 case INTRINSIC_NE:
1631 case INTRINSIC_GT:
1632 case INTRINSIC_GE:
1633 case INTRINSIC_LT:
1634 case INTRINSIC_LE:
1636 if (op1->rank == 0 && op2->rank == 0)
1637 e->rank = 0;
1639 if (op1->rank == 0 && op2->rank != 0)
1641 e->rank = op2->rank;
1643 if (e->shape == NULL)
1644 e->shape = gfc_copy_shape (op2->shape, op2->rank);
1647 if (op1->rank != 0 && op2->rank == 0)
1649 e->rank = op1->rank;
1651 if (e->shape == NULL)
1652 e->shape = gfc_copy_shape (op1->shape, op1->rank);
1655 if (op1->rank != 0 && op2->rank != 0)
1657 if (op1->rank == op2->rank)
1659 e->rank = op1->rank;
1660 if (e->shape == NULL)
1662 t = compare_shapes(op1, op2);
1663 if (t == FAILURE)
1664 e->shape = NULL;
1665 else
1666 e->shape = gfc_copy_shape (op1->shape, op1->rank);
1669 else
1671 gfc_error ("Inconsistent ranks for operator at %L and %L",
1672 &op1->where, &op2->where);
1673 t = FAILURE;
1675 /* Allow higher level expressions to work. */
1676 e->rank = 0;
1680 break;
1682 case INTRINSIC_NOT:
1683 case INTRINSIC_UPLUS:
1684 case INTRINSIC_UMINUS:
1685 e->rank = op1->rank;
1687 if (e->shape == NULL)
1688 e->shape = gfc_copy_shape (op1->shape, op1->rank);
1690 /* Simply copy arrayness attribute */
1691 break;
1693 default:
1694 break;
1697 /* Attempt to simplify the expression. */
1698 if (t == SUCCESS)
1699 t = gfc_simplify_expr (e, 0);
1700 return t;
1702 bad_op:
1704 if (gfc_extend_expr (e) == SUCCESS)
1705 return SUCCESS;
1707 gfc_error (msg, &e->where);
1709 return FAILURE;
1713 /************** Array resolution subroutines **************/
1716 typedef enum
1717 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
1718 comparison;
1720 /* Compare two integer expressions. */
1722 static comparison
1723 compare_bound (gfc_expr * a, gfc_expr * b)
1725 int i;
1727 if (a == NULL || a->expr_type != EXPR_CONSTANT
1728 || b == NULL || b->expr_type != EXPR_CONSTANT)
1729 return CMP_UNKNOWN;
1731 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
1732 gfc_internal_error ("compare_bound(): Bad expression");
1734 i = mpz_cmp (a->value.integer, b->value.integer);
1736 if (i < 0)
1737 return CMP_LT;
1738 if (i > 0)
1739 return CMP_GT;
1740 return CMP_EQ;
1744 /* Compare an integer expression with an integer. */
1746 static comparison
1747 compare_bound_int (gfc_expr * a, int b)
1749 int i;
1751 if (a == NULL || a->expr_type != EXPR_CONSTANT)
1752 return CMP_UNKNOWN;
1754 if (a->ts.type != BT_INTEGER)
1755 gfc_internal_error ("compare_bound_int(): Bad expression");
1757 i = mpz_cmp_si (a->value.integer, b);
1759 if (i < 0)
1760 return CMP_LT;
1761 if (i > 0)
1762 return CMP_GT;
1763 return CMP_EQ;
1767 /* Compare a single dimension of an array reference to the array
1768 specification. */
1770 static try
1771 check_dimension (int i, gfc_array_ref * ar, gfc_array_spec * as)
1774 /* Given start, end and stride values, calculate the minimum and
1775 maximum referenced indexes. */
1777 switch (ar->type)
1779 case AR_FULL:
1780 break;
1782 case AR_ELEMENT:
1783 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
1784 goto bound;
1785 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
1786 goto bound;
1788 break;
1790 case AR_SECTION:
1791 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
1793 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
1794 return FAILURE;
1797 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
1798 goto bound;
1799 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
1800 goto bound;
1802 /* TODO: Possibly, we could warn about end[i] being out-of-bound although
1803 it is legal (see 6.2.2.3.1). */
1805 break;
1807 default:
1808 gfc_internal_error ("check_dimension(): Bad array reference");
1811 return SUCCESS;
1813 bound:
1814 gfc_warning ("Array reference at %L is out of bounds", &ar->c_where[i]);
1815 return SUCCESS;
1819 /* Compare an array reference with an array specification. */
1821 static try
1822 compare_spec_to_ref (gfc_array_ref * ar)
1824 gfc_array_spec *as;
1825 int i;
1827 as = ar->as;
1828 i = as->rank - 1;
1829 /* TODO: Full array sections are only allowed as actual parameters. */
1830 if (as->type == AS_ASSUMED_SIZE
1831 && (/*ar->type == AR_FULL
1832 ||*/ (ar->type == AR_SECTION
1833 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
1835 gfc_error ("Rightmost upper bound of assumed size array section"
1836 " not specified at %L", &ar->where);
1837 return FAILURE;
1840 if (ar->type == AR_FULL)
1841 return SUCCESS;
1843 if (as->rank != ar->dimen)
1845 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
1846 &ar->where, ar->dimen, as->rank);
1847 return FAILURE;
1850 for (i = 0; i < as->rank; i++)
1851 if (check_dimension (i, ar, as) == FAILURE)
1852 return FAILURE;
1854 return SUCCESS;
1858 /* Resolve one part of an array index. */
1861 gfc_resolve_index (gfc_expr * index, int check_scalar)
1863 gfc_typespec ts;
1865 if (index == NULL)
1866 return SUCCESS;
1868 if (gfc_resolve_expr (index) == FAILURE)
1869 return FAILURE;
1871 if (check_scalar && index->rank != 0)
1873 gfc_error ("Array index at %L must be scalar", &index->where);
1874 return FAILURE;
1877 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
1879 gfc_error ("Array index at %L must be of INTEGER type",
1880 &index->where);
1881 return FAILURE;
1884 if (index->ts.type == BT_REAL)
1885 if (gfc_notify_std (GFC_STD_GNU, "Extension: REAL array index at %L",
1886 &index->where) == FAILURE)
1887 return FAILURE;
1889 if (index->ts.kind != gfc_index_integer_kind
1890 || index->ts.type != BT_INTEGER)
1892 ts.type = BT_INTEGER;
1893 ts.kind = gfc_index_integer_kind;
1895 gfc_convert_type_warn (index, &ts, 2, 0);
1898 return SUCCESS;
1901 /* Resolve a dim argument to an intrinsic function. */
1904 gfc_resolve_dim_arg (gfc_expr *dim)
1906 if (dim == NULL)
1907 return SUCCESS;
1909 if (gfc_resolve_expr (dim) == FAILURE)
1910 return FAILURE;
1912 if (dim->rank != 0)
1914 gfc_error ("Argument dim at %L must be scalar", &dim->where);
1915 return FAILURE;
1918 if (dim->ts.type != BT_INTEGER)
1920 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
1921 return FAILURE;
1923 if (dim->ts.kind != gfc_index_integer_kind)
1925 gfc_typespec ts;
1927 ts.type = BT_INTEGER;
1928 ts.kind = gfc_index_integer_kind;
1930 gfc_convert_type_warn (dim, &ts, 2, 0);
1933 return SUCCESS;
1936 /* Given an expression that contains array references, update those array
1937 references to point to the right array specifications. While this is
1938 filled in during matching, this information is difficult to save and load
1939 in a module, so we take care of it here.
1941 The idea here is that the original array reference comes from the
1942 base symbol. We traverse the list of reference structures, setting
1943 the stored reference to references. Component references can
1944 provide an additional array specification. */
1946 static void
1947 find_array_spec (gfc_expr * e)
1949 gfc_array_spec *as;
1950 gfc_component *c;
1951 gfc_ref *ref;
1953 as = e->symtree->n.sym->as;
1955 for (ref = e->ref; ref; ref = ref->next)
1956 switch (ref->type)
1958 case REF_ARRAY:
1959 if (as == NULL)
1960 gfc_internal_error ("find_array_spec(): Missing spec");
1962 ref->u.ar.as = as;
1963 as = NULL;
1964 break;
1966 case REF_COMPONENT:
1967 for (c = e->symtree->n.sym->ts.derived->components; c; c = c->next)
1968 if (c == ref->u.c.component)
1969 break;
1971 if (c == NULL)
1972 gfc_internal_error ("find_array_spec(): Component not found");
1974 if (c->dimension)
1976 if (as != NULL)
1977 gfc_internal_error ("find_array_spec(): unused as(1)");
1978 as = c->as;
1981 break;
1983 case REF_SUBSTRING:
1984 break;
1987 if (as != NULL)
1988 gfc_internal_error ("find_array_spec(): unused as(2)");
1992 /* Resolve an array reference. */
1994 static try
1995 resolve_array_ref (gfc_array_ref * ar)
1997 int i, check_scalar;
1999 for (i = 0; i < ar->dimen; i++)
2001 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
2003 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
2004 return FAILURE;
2005 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
2006 return FAILURE;
2007 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
2008 return FAILURE;
2010 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
2011 switch (ar->start[i]->rank)
2013 case 0:
2014 ar->dimen_type[i] = DIMEN_ELEMENT;
2015 break;
2017 case 1:
2018 ar->dimen_type[i] = DIMEN_VECTOR;
2019 break;
2021 default:
2022 gfc_error ("Array index at %L is an array of rank %d",
2023 &ar->c_where[i], ar->start[i]->rank);
2024 return FAILURE;
2028 /* If the reference type is unknown, figure out what kind it is. */
2030 if (ar->type == AR_UNKNOWN)
2032 ar->type = AR_ELEMENT;
2033 for (i = 0; i < ar->dimen; i++)
2034 if (ar->dimen_type[i] == DIMEN_RANGE
2035 || ar->dimen_type[i] == DIMEN_VECTOR)
2037 ar->type = AR_SECTION;
2038 break;
2042 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
2043 return FAILURE;
2045 return SUCCESS;
2049 static try
2050 resolve_substring (gfc_ref * ref)
2053 if (ref->u.ss.start != NULL)
2055 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
2056 return FAILURE;
2058 if (ref->u.ss.start->ts.type != BT_INTEGER)
2060 gfc_error ("Substring start index at %L must be of type INTEGER",
2061 &ref->u.ss.start->where);
2062 return FAILURE;
2065 if (ref->u.ss.start->rank != 0)
2067 gfc_error ("Substring start index at %L must be scalar",
2068 &ref->u.ss.start->where);
2069 return FAILURE;
2072 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT)
2074 gfc_error ("Substring start index at %L is less than one",
2075 &ref->u.ss.start->where);
2076 return FAILURE;
2080 if (ref->u.ss.end != NULL)
2082 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
2083 return FAILURE;
2085 if (ref->u.ss.end->ts.type != BT_INTEGER)
2087 gfc_error ("Substring end index at %L must be of type INTEGER",
2088 &ref->u.ss.end->where);
2089 return FAILURE;
2092 if (ref->u.ss.end->rank != 0)
2094 gfc_error ("Substring end index at %L must be scalar",
2095 &ref->u.ss.end->where);
2096 return FAILURE;
2099 if (ref->u.ss.length != NULL
2100 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT)
2102 gfc_error ("Substring end index at %L is out of bounds",
2103 &ref->u.ss.start->where);
2104 return FAILURE;
2108 return SUCCESS;
2112 /* Resolve subtype references. */
2114 static try
2115 resolve_ref (gfc_expr * expr)
2117 int current_part_dimension, n_components, seen_part_dimension;
2118 gfc_ref *ref;
2120 for (ref = expr->ref; ref; ref = ref->next)
2121 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
2123 find_array_spec (expr);
2124 break;
2127 for (ref = expr->ref; ref; ref = ref->next)
2128 switch (ref->type)
2130 case REF_ARRAY:
2131 if (resolve_array_ref (&ref->u.ar) == FAILURE)
2132 return FAILURE;
2133 break;
2135 case REF_COMPONENT:
2136 break;
2138 case REF_SUBSTRING:
2139 resolve_substring (ref);
2140 break;
2143 /* Check constraints on part references. */
2145 current_part_dimension = 0;
2146 seen_part_dimension = 0;
2147 n_components = 0;
2149 for (ref = expr->ref; ref; ref = ref->next)
2151 switch (ref->type)
2153 case REF_ARRAY:
2154 switch (ref->u.ar.type)
2156 case AR_FULL:
2157 case AR_SECTION:
2158 current_part_dimension = 1;
2159 break;
2161 case AR_ELEMENT:
2162 current_part_dimension = 0;
2163 break;
2165 case AR_UNKNOWN:
2166 gfc_internal_error ("resolve_ref(): Bad array reference");
2169 break;
2171 case REF_COMPONENT:
2172 if ((current_part_dimension || seen_part_dimension)
2173 && ref->u.c.component->pointer)
2175 gfc_error
2176 ("Component to the right of a part reference with nonzero "
2177 "rank must not have the POINTER attribute at %L",
2178 &expr->where);
2179 return FAILURE;
2182 n_components++;
2183 break;
2185 case REF_SUBSTRING:
2186 break;
2189 if (((ref->type == REF_COMPONENT && n_components > 1)
2190 || ref->next == NULL)
2191 && current_part_dimension
2192 && seen_part_dimension)
2195 gfc_error ("Two or more part references with nonzero rank must "
2196 "not be specified at %L", &expr->where);
2197 return FAILURE;
2200 if (ref->type == REF_COMPONENT)
2202 if (current_part_dimension)
2203 seen_part_dimension = 1;
2205 /* reset to make sure */
2206 current_part_dimension = 0;
2210 return SUCCESS;
2214 /* Given an expression, determine its shape. This is easier than it sounds.
2215 Leaves the shape array NULL if it is not possible to determine the shape. */
2217 static void
2218 expression_shape (gfc_expr * e)
2220 mpz_t array[GFC_MAX_DIMENSIONS];
2221 int i;
2223 if (e->rank == 0 || e->shape != NULL)
2224 return;
2226 for (i = 0; i < e->rank; i++)
2227 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
2228 goto fail;
2230 e->shape = gfc_get_shape (e->rank);
2232 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
2234 return;
2236 fail:
2237 for (i--; i >= 0; i--)
2238 mpz_clear (array[i]);
2242 /* Given a variable expression node, compute the rank of the expression by
2243 examining the base symbol and any reference structures it may have. */
2245 static void
2246 expression_rank (gfc_expr * e)
2248 gfc_ref *ref;
2249 int i, rank;
2251 if (e->ref == NULL)
2253 if (e->expr_type == EXPR_ARRAY)
2254 goto done;
2255 /* Constructors can have a rank different from one via RESHAPE(). */
2257 if (e->symtree == NULL)
2259 e->rank = 0;
2260 goto done;
2263 e->rank = (e->symtree->n.sym->as == NULL)
2264 ? 0 : e->symtree->n.sym->as->rank;
2265 goto done;
2268 rank = 0;
2270 for (ref = e->ref; ref; ref = ref->next)
2272 if (ref->type != REF_ARRAY)
2273 continue;
2275 if (ref->u.ar.type == AR_FULL)
2277 rank = ref->u.ar.as->rank;
2278 break;
2281 if (ref->u.ar.type == AR_SECTION)
2283 /* Figure out the rank of the section. */
2284 if (rank != 0)
2285 gfc_internal_error ("expression_rank(): Two array specs");
2287 for (i = 0; i < ref->u.ar.dimen; i++)
2288 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
2289 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
2290 rank++;
2292 break;
2296 e->rank = rank;
2298 done:
2299 expression_shape (e);
2303 /* Resolve a variable expression. */
2305 static try
2306 resolve_variable (gfc_expr * e)
2308 gfc_symbol *sym;
2310 if (e->ref && resolve_ref (e) == FAILURE)
2311 return FAILURE;
2313 if (e->symtree == NULL)
2314 return FAILURE;
2316 sym = e->symtree->n.sym;
2317 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2319 e->ts.type = BT_PROCEDURE;
2320 return SUCCESS;
2323 if (sym->ts.type != BT_UNKNOWN)
2324 gfc_variable_attr (e, &e->ts);
2325 else
2327 /* Must be a simple variable reference. */
2328 if (gfc_set_default_type (sym, 1, NULL) == FAILURE)
2329 return FAILURE;
2330 e->ts = sym->ts;
2333 return SUCCESS;
2337 /* Resolve an expression. That is, make sure that types of operands agree
2338 with their operators, intrinsic operators are converted to function calls
2339 for overloaded types and unresolved function references are resolved. */
2342 gfc_resolve_expr (gfc_expr * e)
2344 try t;
2346 if (e == NULL)
2347 return SUCCESS;
2349 switch (e->expr_type)
2351 case EXPR_OP:
2352 t = resolve_operator (e);
2353 break;
2355 case EXPR_FUNCTION:
2356 t = resolve_function (e);
2357 break;
2359 case EXPR_VARIABLE:
2360 t = resolve_variable (e);
2361 if (t == SUCCESS)
2362 expression_rank (e);
2363 break;
2365 case EXPR_SUBSTRING:
2366 t = resolve_ref (e);
2367 break;
2369 case EXPR_CONSTANT:
2370 case EXPR_NULL:
2371 t = SUCCESS;
2372 break;
2374 case EXPR_ARRAY:
2375 t = FAILURE;
2376 if (resolve_ref (e) == FAILURE)
2377 break;
2379 t = gfc_resolve_array_constructor (e);
2380 /* Also try to expand a constructor. */
2381 if (t == SUCCESS)
2383 expression_rank (e);
2384 gfc_expand_constructor (e);
2387 break;
2389 case EXPR_STRUCTURE:
2390 t = resolve_ref (e);
2391 if (t == FAILURE)
2392 break;
2394 t = resolve_structure_cons (e);
2395 if (t == FAILURE)
2396 break;
2398 t = gfc_simplify_expr (e, 0);
2399 break;
2401 default:
2402 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
2405 return t;
2409 /* Resolve an expression from an iterator. They must be scalar and have
2410 INTEGER or (optionally) REAL type. */
2412 static try
2413 gfc_resolve_iterator_expr (gfc_expr * expr, bool real_ok,
2414 const char * name_msgid)
2416 if (gfc_resolve_expr (expr) == FAILURE)
2417 return FAILURE;
2419 if (expr->rank != 0)
2421 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
2422 return FAILURE;
2425 if (!(expr->ts.type == BT_INTEGER
2426 || (expr->ts.type == BT_REAL && real_ok)))
2428 if (real_ok)
2429 gfc_error ("%s at %L must be INTEGER or REAL", _(name_msgid),
2430 &expr->where);
2431 else
2432 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
2433 return FAILURE;
2435 return SUCCESS;
2439 /* Resolve the expressions in an iterator structure. If REAL_OK is
2440 false allow only INTEGER type iterators, otherwise allow REAL types. */
2443 gfc_resolve_iterator (gfc_iterator * iter, bool real_ok)
2446 if (iter->var->ts.type == BT_REAL)
2447 gfc_notify_std (GFC_STD_F95_DEL,
2448 "Obsolete: REAL DO loop iterator at %L",
2449 &iter->var->where);
2451 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
2452 == FAILURE)
2453 return FAILURE;
2455 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
2457 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
2458 &iter->var->where);
2459 return FAILURE;
2462 if (gfc_resolve_iterator_expr (iter->start, real_ok,
2463 "Start expression in DO loop") == FAILURE)
2464 return FAILURE;
2466 if (gfc_resolve_iterator_expr (iter->end, real_ok,
2467 "End expression in DO loop") == FAILURE)
2468 return FAILURE;
2470 if (gfc_resolve_iterator_expr (iter->step, real_ok,
2471 "Step expression in DO loop") == FAILURE)
2472 return FAILURE;
2474 if (iter->step->expr_type == EXPR_CONSTANT)
2476 if ((iter->step->ts.type == BT_INTEGER
2477 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
2478 || (iter->step->ts.type == BT_REAL
2479 && mpfr_sgn (iter->step->value.real) == 0))
2481 gfc_error ("Step expression in DO loop at %L cannot be zero",
2482 &iter->step->where);
2483 return FAILURE;
2487 /* Convert start, end, and step to the same type as var. */
2488 if (iter->start->ts.kind != iter->var->ts.kind
2489 || iter->start->ts.type != iter->var->ts.type)
2490 gfc_convert_type (iter->start, &iter->var->ts, 2);
2492 if (iter->end->ts.kind != iter->var->ts.kind
2493 || iter->end->ts.type != iter->var->ts.type)
2494 gfc_convert_type (iter->end, &iter->var->ts, 2);
2496 if (iter->step->ts.kind != iter->var->ts.kind
2497 || iter->step->ts.type != iter->var->ts.type)
2498 gfc_convert_type (iter->step, &iter->var->ts, 2);
2500 return SUCCESS;
2504 /* Resolve a list of FORALL iterators. */
2506 static void
2507 resolve_forall_iterators (gfc_forall_iterator * iter)
2510 while (iter)
2512 if (gfc_resolve_expr (iter->var) == SUCCESS
2513 && iter->var->ts.type != BT_INTEGER)
2514 gfc_error ("FORALL Iteration variable at %L must be INTEGER",
2515 &iter->var->where);
2517 if (gfc_resolve_expr (iter->start) == SUCCESS
2518 && iter->start->ts.type != BT_INTEGER)
2519 gfc_error ("FORALL start expression at %L must be INTEGER",
2520 &iter->start->where);
2521 if (iter->var->ts.kind != iter->start->ts.kind)
2522 gfc_convert_type (iter->start, &iter->var->ts, 2);
2524 if (gfc_resolve_expr (iter->end) == SUCCESS
2525 && iter->end->ts.type != BT_INTEGER)
2526 gfc_error ("FORALL end expression at %L must be INTEGER",
2527 &iter->end->where);
2528 if (iter->var->ts.kind != iter->end->ts.kind)
2529 gfc_convert_type (iter->end, &iter->var->ts, 2);
2531 if (gfc_resolve_expr (iter->stride) == SUCCESS
2532 && iter->stride->ts.type != BT_INTEGER)
2533 gfc_error ("FORALL Stride expression at %L must be INTEGER",
2534 &iter->stride->where);
2535 if (iter->var->ts.kind != iter->stride->ts.kind)
2536 gfc_convert_type (iter->stride, &iter->var->ts, 2);
2538 iter = iter->next;
2543 /* Given a pointer to a symbol that is a derived type, see if any components
2544 have the POINTER attribute. The search is recursive if necessary.
2545 Returns zero if no pointer components are found, nonzero otherwise. */
2547 static int
2548 derived_pointer (gfc_symbol * sym)
2550 gfc_component *c;
2552 for (c = sym->components; c; c = c->next)
2554 if (c->pointer)
2555 return 1;
2557 if (c->ts.type == BT_DERIVED && derived_pointer (c->ts.derived))
2558 return 1;
2561 return 0;
2565 /* Given a pointer to a symbol that is a derived type, see if it's
2566 inaccessible, i.e. if it's defined in another module and the components are
2567 PRIVATE. The search is recursive if necessary. Returns zero if no
2568 inaccessible components are found, nonzero otherwise. */
2570 static int
2571 derived_inaccessible (gfc_symbol *sym)
2573 gfc_component *c;
2575 if (sym->attr.use_assoc && sym->component_access == ACCESS_PRIVATE)
2576 return 1;
2578 for (c = sym->components; c; c = c->next)
2580 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.derived))
2581 return 1;
2584 return 0;
2588 /* Resolve the argument of a deallocate expression. The expression must be
2589 a pointer or a full array. */
2591 static try
2592 resolve_deallocate_expr (gfc_expr * e)
2594 symbol_attribute attr;
2595 int allocatable;
2596 gfc_ref *ref;
2598 if (gfc_resolve_expr (e) == FAILURE)
2599 return FAILURE;
2601 attr = gfc_expr_attr (e);
2602 if (attr.pointer)
2603 return SUCCESS;
2605 if (e->expr_type != EXPR_VARIABLE)
2606 goto bad;
2608 allocatable = e->symtree->n.sym->attr.allocatable;
2609 for (ref = e->ref; ref; ref = ref->next)
2610 switch (ref->type)
2612 case REF_ARRAY:
2613 if (ref->u.ar.type != AR_FULL)
2614 allocatable = 0;
2615 break;
2617 case REF_COMPONENT:
2618 allocatable = (ref->u.c.component->as != NULL
2619 && ref->u.c.component->as->type == AS_DEFERRED);
2620 break;
2622 case REF_SUBSTRING:
2623 allocatable = 0;
2624 break;
2627 if (allocatable == 0)
2629 bad:
2630 gfc_error ("Expression in DEALLOCATE statement at %L must be "
2631 "ALLOCATABLE or a POINTER", &e->where);
2634 return SUCCESS;
2638 /* Given the expression node e for an allocatable/pointer of derived type to be
2639 allocated, get the expression node to be initialized afterwards (needed for
2640 derived types with default initializers). */
2642 static gfc_expr *
2643 expr_to_initialize (gfc_expr * e)
2645 gfc_expr *result;
2646 gfc_ref *ref;
2647 int i;
2649 result = gfc_copy_expr (e);
2651 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
2652 for (ref = result->ref; ref; ref = ref->next)
2653 if (ref->type == REF_ARRAY && ref->next == NULL)
2655 ref->u.ar.type = AR_FULL;
2657 for (i = 0; i < ref->u.ar.dimen; i++)
2658 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
2660 result->rank = ref->u.ar.dimen;
2661 break;
2664 return result;
2668 /* Resolve the expression in an ALLOCATE statement, doing the additional
2669 checks to see whether the expression is OK or not. The expression must
2670 have a trailing array reference that gives the size of the array. */
2672 static try
2673 resolve_allocate_expr (gfc_expr * e, gfc_code * code)
2675 int i, pointer, allocatable, dimension;
2676 symbol_attribute attr;
2677 gfc_ref *ref, *ref2;
2678 gfc_array_ref *ar;
2679 gfc_code *init_st;
2680 gfc_expr *init_e;
2682 if (gfc_resolve_expr (e) == FAILURE)
2683 return FAILURE;
2685 /* Make sure the expression is allocatable or a pointer. If it is
2686 pointer, the next-to-last reference must be a pointer. */
2688 ref2 = NULL;
2690 if (e->expr_type != EXPR_VARIABLE)
2692 allocatable = 0;
2694 attr = gfc_expr_attr (e);
2695 pointer = attr.pointer;
2696 dimension = attr.dimension;
2699 else
2701 allocatable = e->symtree->n.sym->attr.allocatable;
2702 pointer = e->symtree->n.sym->attr.pointer;
2703 dimension = e->symtree->n.sym->attr.dimension;
2705 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
2706 switch (ref->type)
2708 case REF_ARRAY:
2709 if (ref->next != NULL)
2710 pointer = 0;
2711 break;
2713 case REF_COMPONENT:
2714 allocatable = (ref->u.c.component->as != NULL
2715 && ref->u.c.component->as->type == AS_DEFERRED);
2717 pointer = ref->u.c.component->pointer;
2718 dimension = ref->u.c.component->dimension;
2719 break;
2721 case REF_SUBSTRING:
2722 allocatable = 0;
2723 pointer = 0;
2724 break;
2728 if (allocatable == 0 && pointer == 0)
2730 gfc_error ("Expression in ALLOCATE statement at %L must be "
2731 "ALLOCATABLE or a POINTER", &e->where);
2732 return FAILURE;
2735 /* Add default initializer for those derived types that need them. */
2736 if (e->ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&e->ts)))
2738 init_st = gfc_get_code ();
2739 init_st->loc = code->loc;
2740 init_st->op = EXEC_ASSIGN;
2741 init_st->expr = expr_to_initialize (e);
2742 init_st->expr2 = init_e;
2744 init_st->next = code->next;
2745 code->next = init_st;
2748 if (pointer && dimension == 0)
2749 return SUCCESS;
2751 /* Make sure the next-to-last reference node is an array specification. */
2753 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
2755 gfc_error ("Array specification required in ALLOCATE statement "
2756 "at %L", &e->where);
2757 return FAILURE;
2760 if (ref2->u.ar.type == AR_ELEMENT)
2761 return SUCCESS;
2763 /* Make sure that the array section reference makes sense in the
2764 context of an ALLOCATE specification. */
2766 ar = &ref2->u.ar;
2768 for (i = 0; i < ar->dimen; i++)
2769 switch (ar->dimen_type[i])
2771 case DIMEN_ELEMENT:
2772 break;
2774 case DIMEN_RANGE:
2775 if (ar->start[i] != NULL
2776 && ar->end[i] != NULL
2777 && ar->stride[i] == NULL)
2778 break;
2780 /* Fall Through... */
2782 case DIMEN_UNKNOWN:
2783 case DIMEN_VECTOR:
2784 gfc_error ("Bad array specification in ALLOCATE statement at %L",
2785 &e->where);
2786 return FAILURE;
2789 return SUCCESS;
2793 /************ SELECT CASE resolution subroutines ************/
2795 /* Callback function for our mergesort variant. Determines interval
2796 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
2797 op1 > op2. Assumes we're not dealing with the default case.
2798 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
2799 There are nine situations to check. */
2801 static int
2802 compare_cases (const gfc_case * op1, const gfc_case * op2)
2804 int retval;
2806 if (op1->low == NULL) /* op1 = (:L) */
2808 /* op2 = (:N), so overlap. */
2809 retval = 0;
2810 /* op2 = (M:) or (M:N), L < M */
2811 if (op2->low != NULL
2812 && gfc_compare_expr (op1->high, op2->low) < 0)
2813 retval = -1;
2815 else if (op1->high == NULL) /* op1 = (K:) */
2817 /* op2 = (M:), so overlap. */
2818 retval = 0;
2819 /* op2 = (:N) or (M:N), K > N */
2820 if (op2->high != NULL
2821 && gfc_compare_expr (op1->low, op2->high) > 0)
2822 retval = 1;
2824 else /* op1 = (K:L) */
2826 if (op2->low == NULL) /* op2 = (:N), K > N */
2827 retval = (gfc_compare_expr (op1->low, op2->high) > 0) ? 1 : 0;
2828 else if (op2->high == NULL) /* op2 = (M:), L < M */
2829 retval = (gfc_compare_expr (op1->high, op2->low) < 0) ? -1 : 0;
2830 else /* op2 = (M:N) */
2832 retval = 0;
2833 /* L < M */
2834 if (gfc_compare_expr (op1->high, op2->low) < 0)
2835 retval = -1;
2836 /* K > N */
2837 else if (gfc_compare_expr (op1->low, op2->high) > 0)
2838 retval = 1;
2842 return retval;
2846 /* Merge-sort a double linked case list, detecting overlap in the
2847 process. LIST is the head of the double linked case list before it
2848 is sorted. Returns the head of the sorted list if we don't see any
2849 overlap, or NULL otherwise. */
2851 static gfc_case *
2852 check_case_overlap (gfc_case * list)
2854 gfc_case *p, *q, *e, *tail;
2855 int insize, nmerges, psize, qsize, cmp, overlap_seen;
2857 /* If the passed list was empty, return immediately. */
2858 if (!list)
2859 return NULL;
2861 overlap_seen = 0;
2862 insize = 1;
2864 /* Loop unconditionally. The only exit from this loop is a return
2865 statement, when we've finished sorting the case list. */
2866 for (;;)
2868 p = list;
2869 list = NULL;
2870 tail = NULL;
2872 /* Count the number of merges we do in this pass. */
2873 nmerges = 0;
2875 /* Loop while there exists a merge to be done. */
2876 while (p)
2878 int i;
2880 /* Count this merge. */
2881 nmerges++;
2883 /* Cut the list in two pieces by stepping INSIZE places
2884 forward in the list, starting from P. */
2885 psize = 0;
2886 q = p;
2887 for (i = 0; i < insize; i++)
2889 psize++;
2890 q = q->right;
2891 if (!q)
2892 break;
2894 qsize = insize;
2896 /* Now we have two lists. Merge them! */
2897 while (psize > 0 || (qsize > 0 && q != NULL))
2900 /* See from which the next case to merge comes from. */
2901 if (psize == 0)
2903 /* P is empty so the next case must come from Q. */
2904 e = q;
2905 q = q->right;
2906 qsize--;
2908 else if (qsize == 0 || q == NULL)
2910 /* Q is empty. */
2911 e = p;
2912 p = p->right;
2913 psize--;
2915 else
2917 cmp = compare_cases (p, q);
2918 if (cmp < 0)
2920 /* The whole case range for P is less than the
2921 one for Q. */
2922 e = p;
2923 p = p->right;
2924 psize--;
2926 else if (cmp > 0)
2928 /* The whole case range for Q is greater than
2929 the case range for P. */
2930 e = q;
2931 q = q->right;
2932 qsize--;
2934 else
2936 /* The cases overlap, or they are the same
2937 element in the list. Either way, we must
2938 issue an error and get the next case from P. */
2939 /* FIXME: Sort P and Q by line number. */
2940 gfc_error ("CASE label at %L overlaps with CASE "
2941 "label at %L", &p->where, &q->where);
2942 overlap_seen = 1;
2943 e = p;
2944 p = p->right;
2945 psize--;
2949 /* Add the next element to the merged list. */
2950 if (tail)
2951 tail->right = e;
2952 else
2953 list = e;
2954 e->left = tail;
2955 tail = e;
2958 /* P has now stepped INSIZE places along, and so has Q. So
2959 they're the same. */
2960 p = q;
2962 tail->right = NULL;
2964 /* If we have done only one merge or none at all, we've
2965 finished sorting the cases. */
2966 if (nmerges <= 1)
2968 if (!overlap_seen)
2969 return list;
2970 else
2971 return NULL;
2974 /* Otherwise repeat, merging lists twice the size. */
2975 insize *= 2;
2980 /* Check to see if an expression is suitable for use in a CASE statement.
2981 Makes sure that all case expressions are scalar constants of the same
2982 type. Return FAILURE if anything is wrong. */
2984 static try
2985 validate_case_label_expr (gfc_expr * e, gfc_expr * case_expr)
2987 if (e == NULL) return SUCCESS;
2989 if (e->ts.type != case_expr->ts.type)
2991 gfc_error ("Expression in CASE statement at %L must be of type %s",
2992 &e->where, gfc_basic_typename (case_expr->ts.type));
2993 return FAILURE;
2996 /* C805 (R808) For a given case-construct, each case-value shall be of
2997 the same type as case-expr. For character type, length differences
2998 are allowed, but the kind type parameters shall be the same. */
3000 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
3002 gfc_error("Expression in CASE statement at %L must be kind %d",
3003 &e->where, case_expr->ts.kind);
3004 return FAILURE;
3007 /* Convert the case value kind to that of case expression kind, if needed.
3008 FIXME: Should a warning be issued? */
3009 if (e->ts.kind != case_expr->ts.kind)
3010 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
3012 if (e->rank != 0)
3014 gfc_error ("Expression in CASE statement at %L must be scalar",
3015 &e->where);
3016 return FAILURE;
3019 return SUCCESS;
3023 /* Given a completely parsed select statement, we:
3025 - Validate all expressions and code within the SELECT.
3026 - Make sure that the selection expression is not of the wrong type.
3027 - Make sure that no case ranges overlap.
3028 - Eliminate unreachable cases and unreachable code resulting from
3029 removing case labels.
3031 The standard does allow unreachable cases, e.g. CASE (5:3). But
3032 they are a hassle for code generation, and to prevent that, we just
3033 cut them out here. This is not necessary for overlapping cases
3034 because they are illegal and we never even try to generate code.
3036 We have the additional caveat that a SELECT construct could have
3037 been a computed GOTO in the source code. Fortunately we can fairly
3038 easily work around that here: The case_expr for a "real" SELECT CASE
3039 is in code->expr1, but for a computed GOTO it is in code->expr2. All
3040 we have to do is make sure that the case_expr is a scalar integer
3041 expression. */
3043 static void
3044 resolve_select (gfc_code * code)
3046 gfc_code *body;
3047 gfc_expr *case_expr;
3048 gfc_case *cp, *default_case, *tail, *head;
3049 int seen_unreachable;
3050 int ncases;
3051 bt type;
3052 try t;
3054 if (code->expr == NULL)
3056 /* This was actually a computed GOTO statement. */
3057 case_expr = code->expr2;
3058 if (case_expr->ts.type != BT_INTEGER
3059 || case_expr->rank != 0)
3060 gfc_error ("Selection expression in computed GOTO statement "
3061 "at %L must be a scalar integer expression",
3062 &case_expr->where);
3064 /* Further checking is not necessary because this SELECT was built
3065 by the compiler, so it should always be OK. Just move the
3066 case_expr from expr2 to expr so that we can handle computed
3067 GOTOs as normal SELECTs from here on. */
3068 code->expr = code->expr2;
3069 code->expr2 = NULL;
3070 return;
3073 case_expr = code->expr;
3075 type = case_expr->ts.type;
3076 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
3078 gfc_error ("Argument of SELECT statement at %L cannot be %s",
3079 &case_expr->where, gfc_typename (&case_expr->ts));
3081 /* Punt. Going on here just produce more garbage error messages. */
3082 return;
3085 if (case_expr->rank != 0)
3087 gfc_error ("Argument of SELECT statement at %L must be a scalar "
3088 "expression", &case_expr->where);
3090 /* Punt. */
3091 return;
3094 /* PR 19168 has a long discussion concerning a mismatch of the kinds
3095 of the SELECT CASE expression and its CASE values. Walk the lists
3096 of case values, and if we find a mismatch, promote case_expr to
3097 the appropriate kind. */
3099 if (type == BT_LOGICAL || type == BT_INTEGER)
3101 for (body = code->block; body; body = body->block)
3103 /* Walk the case label list. */
3104 for (cp = body->ext.case_list; cp; cp = cp->next)
3106 /* Intercept the DEFAULT case. It does not have a kind. */
3107 if (cp->low == NULL && cp->high == NULL)
3108 continue;
3110 /* Unreachable case ranges are discarded, so ignore. */
3111 if (cp->low != NULL && cp->high != NULL
3112 && cp->low != cp->high
3113 && gfc_compare_expr (cp->low, cp->high) > 0)
3114 continue;
3116 /* FIXME: Should a warning be issued? */
3117 if (cp->low != NULL
3118 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
3119 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
3121 if (cp->high != NULL
3122 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
3123 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
3128 /* Assume there is no DEFAULT case. */
3129 default_case = NULL;
3130 head = tail = NULL;
3131 ncases = 0;
3133 for (body = code->block; body; body = body->block)
3135 /* Assume the CASE list is OK, and all CASE labels can be matched. */
3136 t = SUCCESS;
3137 seen_unreachable = 0;
3139 /* Walk the case label list, making sure that all case labels
3140 are legal. */
3141 for (cp = body->ext.case_list; cp; cp = cp->next)
3143 /* Count the number of cases in the whole construct. */
3144 ncases++;
3146 /* Intercept the DEFAULT case. */
3147 if (cp->low == NULL && cp->high == NULL)
3149 if (default_case != NULL)
3151 gfc_error ("The DEFAULT CASE at %L cannot be followed "
3152 "by a second DEFAULT CASE at %L",
3153 &default_case->where, &cp->where);
3154 t = FAILURE;
3155 break;
3157 else
3159 default_case = cp;
3160 continue;
3164 /* Deal with single value cases and case ranges. Errors are
3165 issued from the validation function. */
3166 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
3167 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
3169 t = FAILURE;
3170 break;
3173 if (type == BT_LOGICAL
3174 && ((cp->low == NULL || cp->high == NULL)
3175 || cp->low != cp->high))
3177 gfc_error
3178 ("Logical range in CASE statement at %L is not allowed",
3179 &cp->low->where);
3180 t = FAILURE;
3181 break;
3184 if (cp->low != NULL && cp->high != NULL
3185 && cp->low != cp->high
3186 && gfc_compare_expr (cp->low, cp->high) > 0)
3188 if (gfc_option.warn_surprising)
3189 gfc_warning ("Range specification at %L can never "
3190 "be matched", &cp->where);
3192 cp->unreachable = 1;
3193 seen_unreachable = 1;
3195 else
3197 /* If the case range can be matched, it can also overlap with
3198 other cases. To make sure it does not, we put it in a
3199 double linked list here. We sort that with a merge sort
3200 later on to detect any overlapping cases. */
3201 if (!head)
3203 head = tail = cp;
3204 head->right = head->left = NULL;
3206 else
3208 tail->right = cp;
3209 tail->right->left = tail;
3210 tail = tail->right;
3211 tail->right = NULL;
3216 /* It there was a failure in the previous case label, give up
3217 for this case label list. Continue with the next block. */
3218 if (t == FAILURE)
3219 continue;
3221 /* See if any case labels that are unreachable have been seen.
3222 If so, we eliminate them. This is a bit of a kludge because
3223 the case lists for a single case statement (label) is a
3224 single forward linked lists. */
3225 if (seen_unreachable)
3227 /* Advance until the first case in the list is reachable. */
3228 while (body->ext.case_list != NULL
3229 && body->ext.case_list->unreachable)
3231 gfc_case *n = body->ext.case_list;
3232 body->ext.case_list = body->ext.case_list->next;
3233 n->next = NULL;
3234 gfc_free_case_list (n);
3237 /* Strip all other unreachable cases. */
3238 if (body->ext.case_list)
3240 for (cp = body->ext.case_list; cp->next; cp = cp->next)
3242 if (cp->next->unreachable)
3244 gfc_case *n = cp->next;
3245 cp->next = cp->next->next;
3246 n->next = NULL;
3247 gfc_free_case_list (n);
3254 /* See if there were overlapping cases. If the check returns NULL,
3255 there was overlap. In that case we don't do anything. If head
3256 is non-NULL, we prepend the DEFAULT case. The sorted list can
3257 then used during code generation for SELECT CASE constructs with
3258 a case expression of a CHARACTER type. */
3259 if (head)
3261 head = check_case_overlap (head);
3263 /* Prepend the default_case if it is there. */
3264 if (head != NULL && default_case)
3266 default_case->left = NULL;
3267 default_case->right = head;
3268 head->left = default_case;
3272 /* Eliminate dead blocks that may be the result if we've seen
3273 unreachable case labels for a block. */
3274 for (body = code; body && body->block; body = body->block)
3276 if (body->block->ext.case_list == NULL)
3278 /* Cut the unreachable block from the code chain. */
3279 gfc_code *c = body->block;
3280 body->block = c->block;
3282 /* Kill the dead block, but not the blocks below it. */
3283 c->block = NULL;
3284 gfc_free_statements (c);
3288 /* More than two cases is legal but insane for logical selects.
3289 Issue a warning for it. */
3290 if (gfc_option.warn_surprising && type == BT_LOGICAL
3291 && ncases > 2)
3292 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
3293 &code->loc);
3297 /* Resolve a transfer statement. This is making sure that:
3298 -- a derived type being transferred has only non-pointer components
3299 -- a derived type being transferred doesn't have private components, unless
3300 it's being transferred from the module where the type was defined
3301 -- we're not trying to transfer a whole assumed size array. */
3303 static void
3304 resolve_transfer (gfc_code * code)
3306 gfc_typespec *ts;
3307 gfc_symbol *sym;
3308 gfc_ref *ref;
3309 gfc_expr *exp;
3311 exp = code->expr;
3313 if (exp->expr_type != EXPR_VARIABLE)
3314 return;
3316 sym = exp->symtree->n.sym;
3317 ts = &sym->ts;
3319 /* Go to actual component transferred. */
3320 for (ref = code->expr->ref; ref; ref = ref->next)
3321 if (ref->type == REF_COMPONENT)
3322 ts = &ref->u.c.component->ts;
3324 if (ts->type == BT_DERIVED)
3326 /* Check that transferred derived type doesn't contain POINTER
3327 components. */
3328 if (derived_pointer (ts->derived))
3330 gfc_error ("Data transfer element at %L cannot have "
3331 "POINTER components", &code->loc);
3332 return;
3335 if (derived_inaccessible (ts->derived))
3337 gfc_error ("Data transfer element at %L cannot have "
3338 "PRIVATE components",&code->loc);
3339 return;
3343 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
3344 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
3346 gfc_error ("Data transfer element at %L cannot be a full reference to "
3347 "an assumed-size array", &code->loc);
3348 return;
3353 /*********** Toplevel code resolution subroutines ***********/
3355 /* Given a branch to a label and a namespace, if the branch is conforming.
3356 The code node described where the branch is located. */
3358 static void
3359 resolve_branch (gfc_st_label * label, gfc_code * code)
3361 gfc_code *block, *found;
3362 code_stack *stack;
3363 gfc_st_label *lp;
3365 if (label == NULL)
3366 return;
3367 lp = label;
3369 /* Step one: is this a valid branching target? */
3371 if (lp->defined == ST_LABEL_UNKNOWN)
3373 gfc_error ("Label %d referenced at %L is never defined", lp->value,
3374 &lp->where);
3375 return;
3378 if (lp->defined != ST_LABEL_TARGET)
3380 gfc_error ("Statement at %L is not a valid branch target statement "
3381 "for the branch statement at %L", &lp->where, &code->loc);
3382 return;
3385 /* Step two: make sure this branch is not a branch to itself ;-) */
3387 if (code->here == label)
3389 gfc_warning ("Branch at %L causes an infinite loop", &code->loc);
3390 return;
3393 /* Step three: Try to find the label in the parse tree. To do this,
3394 we traverse the tree block-by-block: first the block that
3395 contains this GOTO, then the block that it is nested in, etc. We
3396 can ignore other blocks because branching into another block is
3397 not allowed. */
3399 found = NULL;
3401 for (stack = cs_base; stack; stack = stack->prev)
3403 for (block = stack->head; block; block = block->next)
3405 if (block->here == label)
3407 found = block;
3408 break;
3412 if (found)
3413 break;
3416 if (found == NULL)
3418 /* still nothing, so illegal. */
3419 gfc_error_now ("Label at %L is not in the same block as the "
3420 "GOTO statement at %L", &lp->where, &code->loc);
3421 return;
3424 /* Step four: Make sure that the branching target is legal if
3425 the statement is an END {SELECT,DO,IF}. */
3427 if (found->op == EXEC_NOP)
3429 for (stack = cs_base; stack; stack = stack->prev)
3430 if (stack->current->next == found)
3431 break;
3433 if (stack == NULL)
3434 gfc_notify_std (GFC_STD_F95_DEL,
3435 "Obsolete: GOTO at %L jumps to END of construct at %L",
3436 &code->loc, &found->loc);
3441 /* Check whether EXPR1 has the same shape as EXPR2. */
3443 static try
3444 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
3446 mpz_t shape[GFC_MAX_DIMENSIONS];
3447 mpz_t shape2[GFC_MAX_DIMENSIONS];
3448 try result = FAILURE;
3449 int i;
3451 /* Compare the rank. */
3452 if (expr1->rank != expr2->rank)
3453 return result;
3455 /* Compare the size of each dimension. */
3456 for (i=0; i<expr1->rank; i++)
3458 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
3459 goto ignore;
3461 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
3462 goto ignore;
3464 if (mpz_cmp (shape[i], shape2[i]))
3465 goto over;
3468 /* When either of the two expression is an assumed size array, we
3469 ignore the comparison of dimension sizes. */
3470 ignore:
3471 result = SUCCESS;
3473 over:
3474 for (i--; i>=0; i--)
3476 mpz_clear (shape[i]);
3477 mpz_clear (shape2[i]);
3479 return result;
3483 /* Check whether a WHERE assignment target or a WHERE mask expression
3484 has the same shape as the outmost WHERE mask expression. */
3486 static void
3487 resolve_where (gfc_code *code, gfc_expr *mask)
3489 gfc_code *cblock;
3490 gfc_code *cnext;
3491 gfc_expr *e = NULL;
3493 cblock = code->block;
3495 /* Store the first WHERE mask-expr of the WHERE statement or construct.
3496 In case of nested WHERE, only the outmost one is stored. */
3497 if (mask == NULL) /* outmost WHERE */
3498 e = cblock->expr;
3499 else /* inner WHERE */
3500 e = mask;
3502 while (cblock)
3504 if (cblock->expr)
3506 /* Check if the mask-expr has a consistent shape with the
3507 outmost WHERE mask-expr. */
3508 if (resolve_where_shape (cblock->expr, e) == FAILURE)
3509 gfc_error ("WHERE mask at %L has inconsistent shape",
3510 &cblock->expr->where);
3513 /* the assignment statement of a WHERE statement, or the first
3514 statement in where-body-construct of a WHERE construct */
3515 cnext = cblock->next;
3516 while (cnext)
3518 switch (cnext->op)
3520 /* WHERE assignment statement */
3521 case EXEC_ASSIGN:
3523 /* Check shape consistent for WHERE assignment target. */
3524 if (e && resolve_where_shape (cnext->expr, e) == FAILURE)
3525 gfc_error ("WHERE assignment target at %L has "
3526 "inconsistent shape", &cnext->expr->where);
3527 break;
3529 /* WHERE or WHERE construct is part of a where-body-construct */
3530 case EXEC_WHERE:
3531 resolve_where (cnext, e);
3532 break;
3534 default:
3535 gfc_error ("Unsupported statement inside WHERE at %L",
3536 &cnext->loc);
3538 /* the next statement within the same where-body-construct */
3539 cnext = cnext->next;
3541 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
3542 cblock = cblock->block;
3547 /* Check whether the FORALL index appears in the expression or not. */
3549 static try
3550 gfc_find_forall_index (gfc_expr *expr, gfc_symbol *symbol)
3552 gfc_array_ref ar;
3553 gfc_ref *tmp;
3554 gfc_actual_arglist *args;
3555 int i;
3557 switch (expr->expr_type)
3559 case EXPR_VARIABLE:
3560 gcc_assert (expr->symtree->n.sym);
3562 /* A scalar assignment */
3563 if (!expr->ref)
3565 if (expr->symtree->n.sym == symbol)
3566 return SUCCESS;
3567 else
3568 return FAILURE;
3571 /* the expr is array ref, substring or struct component. */
3572 tmp = expr->ref;
3573 while (tmp != NULL)
3575 switch (tmp->type)
3577 case REF_ARRAY:
3578 /* Check if the symbol appears in the array subscript. */
3579 ar = tmp->u.ar;
3580 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
3582 if (ar.start[i])
3583 if (gfc_find_forall_index (ar.start[i], symbol) == SUCCESS)
3584 return SUCCESS;
3586 if (ar.end[i])
3587 if (gfc_find_forall_index (ar.end[i], symbol) == SUCCESS)
3588 return SUCCESS;
3590 if (ar.stride[i])
3591 if (gfc_find_forall_index (ar.stride[i], symbol) == SUCCESS)
3592 return SUCCESS;
3593 } /* end for */
3594 break;
3596 case REF_SUBSTRING:
3597 if (expr->symtree->n.sym == symbol)
3598 return SUCCESS;
3599 tmp = expr->ref;
3600 /* Check if the symbol appears in the substring section. */
3601 if (gfc_find_forall_index (tmp->u.ss.start, symbol) == SUCCESS)
3602 return SUCCESS;
3603 if (gfc_find_forall_index (tmp->u.ss.end, symbol) == SUCCESS)
3604 return SUCCESS;
3605 break;
3607 case REF_COMPONENT:
3608 break;
3610 default:
3611 gfc_error("expresion reference type error at %L", &expr->where);
3613 tmp = tmp->next;
3615 break;
3617 /* If the expression is a function call, then check if the symbol
3618 appears in the actual arglist of the function. */
3619 case EXPR_FUNCTION:
3620 for (args = expr->value.function.actual; args; args = args->next)
3622 if (gfc_find_forall_index(args->expr,symbol) == SUCCESS)
3623 return SUCCESS;
3625 break;
3627 /* It seems not to happen. */
3628 case EXPR_SUBSTRING:
3629 if (expr->ref)
3631 tmp = expr->ref;
3632 gcc_assert (expr->ref->type == REF_SUBSTRING);
3633 if (gfc_find_forall_index (tmp->u.ss.start, symbol) == SUCCESS)
3634 return SUCCESS;
3635 if (gfc_find_forall_index (tmp->u.ss.end, symbol) == SUCCESS)
3636 return SUCCESS;
3638 break;
3640 /* It seems not to happen. */
3641 case EXPR_STRUCTURE:
3642 case EXPR_ARRAY:
3643 gfc_error ("Unsupported statement while finding forall index in "
3644 "expression");
3645 break;
3647 case EXPR_OP:
3648 /* Find the FORALL index in the first operand. */
3649 if (expr->value.op.op1)
3651 if (gfc_find_forall_index (expr->value.op.op1, symbol) == SUCCESS)
3652 return SUCCESS;
3655 /* Find the FORALL index in the second operand. */
3656 if (expr->value.op.op2)
3658 if (gfc_find_forall_index (expr->value.op.op2, symbol) == SUCCESS)
3659 return SUCCESS;
3661 break;
3663 default:
3664 break;
3667 return FAILURE;
3671 /* Resolve assignment in FORALL construct.
3672 NVAR is the number of FORALL index variables, and VAR_EXPR records the
3673 FORALL index variables. */
3675 static void
3676 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
3678 int n;
3680 for (n = 0; n < nvar; n++)
3682 gfc_symbol *forall_index;
3684 forall_index = var_expr[n]->symtree->n.sym;
3686 /* Check whether the assignment target is one of the FORALL index
3687 variable. */
3688 if ((code->expr->expr_type == EXPR_VARIABLE)
3689 && (code->expr->symtree->n.sym == forall_index))
3690 gfc_error ("Assignment to a FORALL index variable at %L",
3691 &code->expr->where);
3692 else
3694 /* If one of the FORALL index variables doesn't appear in the
3695 assignment target, then there will be a many-to-one
3696 assignment. */
3697 if (gfc_find_forall_index (code->expr, forall_index) == FAILURE)
3698 gfc_error ("The FORALL with index '%s' cause more than one "
3699 "assignment to this object at %L",
3700 var_expr[n]->symtree->name, &code->expr->where);
3706 /* Resolve WHERE statement in FORALL construct. */
3708 static void
3709 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr){
3710 gfc_code *cblock;
3711 gfc_code *cnext;
3713 cblock = code->block;
3714 while (cblock)
3716 /* the assignment statement of a WHERE statement, or the first
3717 statement in where-body-construct of a WHERE construct */
3718 cnext = cblock->next;
3719 while (cnext)
3721 switch (cnext->op)
3723 /* WHERE assignment statement */
3724 case EXEC_ASSIGN:
3725 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
3726 break;
3728 /* WHERE or WHERE construct is part of a where-body-construct */
3729 case EXEC_WHERE:
3730 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
3731 break;
3733 default:
3734 gfc_error ("Unsupported statement inside WHERE at %L",
3735 &cnext->loc);
3737 /* the next statement within the same where-body-construct */
3738 cnext = cnext->next;
3740 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
3741 cblock = cblock->block;
3746 /* Traverse the FORALL body to check whether the following errors exist:
3747 1. For assignment, check if a many-to-one assignment happens.
3748 2. For WHERE statement, check the WHERE body to see if there is any
3749 many-to-one assignment. */
3751 static void
3752 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
3754 gfc_code *c;
3756 c = code->block->next;
3757 while (c)
3759 switch (c->op)
3761 case EXEC_ASSIGN:
3762 case EXEC_POINTER_ASSIGN:
3763 gfc_resolve_assign_in_forall (c, nvar, var_expr);
3764 break;
3766 /* Because the resolve_blocks() will handle the nested FORALL,
3767 there is no need to handle it here. */
3768 case EXEC_FORALL:
3769 break;
3770 case EXEC_WHERE:
3771 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
3772 break;
3773 default:
3774 break;
3776 /* The next statement in the FORALL body. */
3777 c = c->next;
3782 /* Given a FORALL construct, first resolve the FORALL iterator, then call
3783 gfc_resolve_forall_body to resolve the FORALL body. */
3785 static void resolve_blocks (gfc_code *, gfc_namespace *);
3787 static void
3788 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
3790 static gfc_expr **var_expr;
3791 static int total_var = 0;
3792 static int nvar = 0;
3793 gfc_forall_iterator *fa;
3794 gfc_symbol *forall_index;
3795 gfc_code *next;
3796 int i;
3798 /* Start to resolve a FORALL construct */
3799 if (forall_save == 0)
3801 /* Count the total number of FORALL index in the nested FORALL
3802 construct in order to allocate the VAR_EXPR with proper size. */
3803 next = code;
3804 while ((next != NULL) && (next->op == EXEC_FORALL))
3806 for (fa = next->ext.forall_iterator; fa; fa = fa->next)
3807 total_var ++;
3808 next = next->block->next;
3811 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
3812 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
3815 /* The information about FORALL iterator, including FORALL index start, end
3816 and stride. The FORALL index can not appear in start, end or stride. */
3817 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
3819 /* Check if any outer FORALL index name is the same as the current
3820 one. */
3821 for (i = 0; i < nvar; i++)
3823 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
3825 gfc_error ("An outer FORALL construct already has an index "
3826 "with this name %L", &fa->var->where);
3830 /* Record the current FORALL index. */
3831 var_expr[nvar] = gfc_copy_expr (fa->var);
3833 forall_index = fa->var->symtree->n.sym;
3835 /* Check if the FORALL index appears in start, end or stride. */
3836 if (gfc_find_forall_index (fa->start, forall_index) == SUCCESS)
3837 gfc_error ("A FORALL index must not appear in a limit or stride "
3838 "expression in the same FORALL at %L", &fa->start->where);
3839 if (gfc_find_forall_index (fa->end, forall_index) == SUCCESS)
3840 gfc_error ("A FORALL index must not appear in a limit or stride "
3841 "expression in the same FORALL at %L", &fa->end->where);
3842 if (gfc_find_forall_index (fa->stride, forall_index) == SUCCESS)
3843 gfc_error ("A FORALL index must not appear in a limit or stride "
3844 "expression in the same FORALL at %L", &fa->stride->where);
3845 nvar++;
3848 /* Resolve the FORALL body. */
3849 gfc_resolve_forall_body (code, nvar, var_expr);
3851 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
3852 resolve_blocks (code->block, ns);
3854 /* Free VAR_EXPR after the whole FORALL construct resolved. */
3855 for (i = 0; i < total_var; i++)
3856 gfc_free_expr (var_expr[i]);
3858 /* Reset the counters. */
3859 total_var = 0;
3860 nvar = 0;
3864 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL ,GOTO and
3865 DO code nodes. */
3867 static void resolve_code (gfc_code *, gfc_namespace *);
3869 static void
3870 resolve_blocks (gfc_code * b, gfc_namespace * ns)
3872 try t;
3874 for (; b; b = b->block)
3876 t = gfc_resolve_expr (b->expr);
3877 if (gfc_resolve_expr (b->expr2) == FAILURE)
3878 t = FAILURE;
3880 switch (b->op)
3882 case EXEC_IF:
3883 if (t == SUCCESS && b->expr != NULL
3884 && (b->expr->ts.type != BT_LOGICAL || b->expr->rank != 0))
3885 gfc_error
3886 ("ELSE IF clause at %L requires a scalar LOGICAL expression",
3887 &b->expr->where);
3888 break;
3890 case EXEC_WHERE:
3891 if (t == SUCCESS
3892 && b->expr != NULL
3893 && (b->expr->ts.type != BT_LOGICAL
3894 || b->expr->rank == 0))
3895 gfc_error
3896 ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
3897 &b->expr->where);
3898 break;
3900 case EXEC_GOTO:
3901 resolve_branch (b->label, b);
3902 break;
3904 case EXEC_SELECT:
3905 case EXEC_FORALL:
3906 case EXEC_DO:
3907 case EXEC_DO_WHILE:
3908 case EXEC_READ:
3909 case EXEC_WRITE:
3910 case EXEC_IOLENGTH:
3911 break;
3913 default:
3914 gfc_internal_error ("resolve_block(): Bad block type");
3917 resolve_code (b->next, ns);
3922 /* Given a block of code, recursively resolve everything pointed to by this
3923 code block. */
3925 static void
3926 resolve_code (gfc_code * code, gfc_namespace * ns)
3928 int forall_save = 0;
3929 code_stack frame;
3930 gfc_alloc *a;
3931 try t;
3933 frame.prev = cs_base;
3934 frame.head = code;
3935 cs_base = &frame;
3937 for (; code; code = code->next)
3939 frame.current = code;
3941 if (code->op == EXEC_FORALL)
3943 forall_save = forall_flag;
3944 forall_flag = 1;
3945 gfc_resolve_forall (code, ns, forall_save);
3947 else
3948 resolve_blocks (code->block, ns);
3950 if (code->op == EXEC_FORALL)
3951 forall_flag = forall_save;
3953 t = gfc_resolve_expr (code->expr);
3954 if (gfc_resolve_expr (code->expr2) == FAILURE)
3955 t = FAILURE;
3957 switch (code->op)
3959 case EXEC_NOP:
3960 case EXEC_CYCLE:
3961 case EXEC_PAUSE:
3962 case EXEC_STOP:
3963 case EXEC_EXIT:
3964 case EXEC_CONTINUE:
3965 case EXEC_DT_END:
3966 case EXEC_ENTRY:
3967 break;
3969 case EXEC_WHERE:
3970 resolve_where (code, NULL);
3971 break;
3973 case EXEC_GOTO:
3974 if (code->expr != NULL)
3976 if (code->expr->ts.type != BT_INTEGER)
3977 gfc_error ("ASSIGNED GOTO statement at %L requires an INTEGER "
3978 "variable", &code->expr->where);
3979 else if (code->expr->symtree->n.sym->attr.assign != 1)
3980 gfc_error ("Variable '%s' has not been assigned a target label "
3981 "at %L", code->expr->symtree->n.sym->name,
3982 &code->expr->where);
3984 else
3985 resolve_branch (code->label, code);
3986 break;
3988 case EXEC_RETURN:
3989 if (code->expr != NULL && code->expr->ts.type != BT_INTEGER)
3990 gfc_error ("Alternate RETURN statement at %L requires an INTEGER "
3991 "return specifier", &code->expr->where);
3992 break;
3994 case EXEC_ASSIGN:
3995 if (t == FAILURE)
3996 break;
3998 if (gfc_extend_assign (code, ns) == SUCCESS)
3999 goto call;
4001 if (gfc_pure (NULL))
4003 if (gfc_impure_variable (code->expr->symtree->n.sym))
4005 gfc_error
4006 ("Cannot assign to variable '%s' in PURE procedure at %L",
4007 code->expr->symtree->n.sym->name, &code->expr->where);
4008 break;
4011 if (code->expr2->ts.type == BT_DERIVED
4012 && derived_pointer (code->expr2->ts.derived))
4014 gfc_error
4015 ("Right side of assignment at %L is a derived type "
4016 "containing a POINTER in a PURE procedure",
4017 &code->expr2->where);
4018 break;
4022 gfc_check_assign (code->expr, code->expr2, 1);
4023 break;
4025 case EXEC_LABEL_ASSIGN:
4026 if (code->label->defined == ST_LABEL_UNKNOWN)
4027 gfc_error ("Label %d referenced at %L is never defined",
4028 code->label->value, &code->label->where);
4029 if (t == SUCCESS
4030 && (code->expr->expr_type != EXPR_VARIABLE
4031 || code->expr->symtree->n.sym->ts.type != BT_INTEGER
4032 || code->expr->symtree->n.sym->ts.kind
4033 != gfc_default_integer_kind
4034 || code->expr->symtree->n.sym->as != NULL))
4035 gfc_error ("ASSIGN statement at %L requires a scalar "
4036 "default INTEGER variable", &code->expr->where);
4037 break;
4039 case EXEC_POINTER_ASSIGN:
4040 if (t == FAILURE)
4041 break;
4043 gfc_check_pointer_assign (code->expr, code->expr2);
4044 break;
4046 case EXEC_ARITHMETIC_IF:
4047 if (t == SUCCESS
4048 && code->expr->ts.type != BT_INTEGER
4049 && code->expr->ts.type != BT_REAL)
4050 gfc_error ("Arithmetic IF statement at %L requires a numeric "
4051 "expression", &code->expr->where);
4053 resolve_branch (code->label, code);
4054 resolve_branch (code->label2, code);
4055 resolve_branch (code->label3, code);
4056 break;
4058 case EXEC_IF:
4059 if (t == SUCCESS && code->expr != NULL
4060 && (code->expr->ts.type != BT_LOGICAL
4061 || code->expr->rank != 0))
4062 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
4063 &code->expr->where);
4064 break;
4066 case EXEC_CALL:
4067 call:
4068 resolve_call (code);
4069 break;
4071 case EXEC_SELECT:
4072 /* Select is complicated. Also, a SELECT construct could be
4073 a transformed computed GOTO. */
4074 resolve_select (code);
4075 break;
4077 case EXEC_DO:
4078 if (code->ext.iterator != NULL)
4079 gfc_resolve_iterator (code->ext.iterator, true);
4080 break;
4082 case EXEC_DO_WHILE:
4083 if (code->expr == NULL)
4084 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
4085 if (t == SUCCESS
4086 && (code->expr->rank != 0
4087 || code->expr->ts.type != BT_LOGICAL))
4088 gfc_error ("Exit condition of DO WHILE loop at %L must be "
4089 "a scalar LOGICAL expression", &code->expr->where);
4090 break;
4092 case EXEC_ALLOCATE:
4093 if (t == SUCCESS && code->expr != NULL
4094 && code->expr->ts.type != BT_INTEGER)
4095 gfc_error ("STAT tag in ALLOCATE statement at %L must be "
4096 "of type INTEGER", &code->expr->where);
4098 for (a = code->ext.alloc_list; a; a = a->next)
4099 resolve_allocate_expr (a->expr, code);
4101 break;
4103 case EXEC_DEALLOCATE:
4104 if (t == SUCCESS && code->expr != NULL
4105 && code->expr->ts.type != BT_INTEGER)
4106 gfc_error
4107 ("STAT tag in DEALLOCATE statement at %L must be of type "
4108 "INTEGER", &code->expr->where);
4110 for (a = code->ext.alloc_list; a; a = a->next)
4111 resolve_deallocate_expr (a->expr);
4113 break;
4115 case EXEC_OPEN:
4116 if (gfc_resolve_open (code->ext.open) == FAILURE)
4117 break;
4119 resolve_branch (code->ext.open->err, code);
4120 break;
4122 case EXEC_CLOSE:
4123 if (gfc_resolve_close (code->ext.close) == FAILURE)
4124 break;
4126 resolve_branch (code->ext.close->err, code);
4127 break;
4129 case EXEC_BACKSPACE:
4130 case EXEC_ENDFILE:
4131 case EXEC_REWIND:
4132 case EXEC_FLUSH:
4133 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
4134 break;
4136 resolve_branch (code->ext.filepos->err, code);
4137 break;
4139 case EXEC_INQUIRE:
4140 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
4141 break;
4143 resolve_branch (code->ext.inquire->err, code);
4144 break;
4146 case EXEC_IOLENGTH:
4147 gcc_assert (code->ext.inquire != NULL);
4148 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
4149 break;
4151 resolve_branch (code->ext.inquire->err, code);
4152 break;
4154 case EXEC_READ:
4155 case EXEC_WRITE:
4156 if (gfc_resolve_dt (code->ext.dt) == FAILURE)
4157 break;
4159 resolve_branch (code->ext.dt->err, code);
4160 resolve_branch (code->ext.dt->end, code);
4161 resolve_branch (code->ext.dt->eor, code);
4162 break;
4164 case EXEC_TRANSFER:
4165 resolve_transfer (code);
4166 break;
4168 case EXEC_FORALL:
4169 resolve_forall_iterators (code->ext.forall_iterator);
4171 if (code->expr != NULL && code->expr->ts.type != BT_LOGICAL)
4172 gfc_error
4173 ("FORALL mask clause at %L requires a LOGICAL expression",
4174 &code->expr->where);
4175 break;
4177 default:
4178 gfc_internal_error ("resolve_code(): Bad statement code");
4182 cs_base = frame.prev;
4186 /* Resolve initial values and make sure they are compatible with
4187 the variable. */
4189 static void
4190 resolve_values (gfc_symbol * sym)
4193 if (sym->value == NULL)
4194 return;
4196 if (gfc_resolve_expr (sym->value) == FAILURE)
4197 return;
4199 gfc_check_assign_symbol (sym, sym->value);
4203 /* Do anything necessary to resolve a symbol. Right now, we just
4204 assume that an otherwise unknown symbol is a variable. This sort
4205 of thing commonly happens for symbols in module. */
4207 static void
4208 resolve_symbol (gfc_symbol * sym)
4210 /* Zero if we are checking a formal namespace. */
4211 static int formal_ns_flag = 1;
4212 int formal_ns_save, check_constant, mp_flag;
4213 int i, flag;
4214 gfc_namelist *nl;
4215 gfc_symtree * symtree;
4216 gfc_symtree * this_symtree;
4217 gfc_namespace * ns;
4218 gfc_component * c;
4219 gfc_formal_arglist * arg;
4221 if (sym->attr.flavor == FL_UNKNOWN)
4224 /* If we find that a flavorless symbol is an interface in one of the
4225 parent namespaces, find its symtree in this namespace, free the
4226 symbol and set the symtree to point to the interface symbol. */
4227 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
4229 symtree = gfc_find_symtree (ns->sym_root, sym->name);
4230 if (symtree && symtree->n.sym->generic)
4232 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
4233 sym->name);
4234 sym->refs--;
4235 if (!sym->refs)
4236 gfc_free_symbol (sym);
4237 symtree->n.sym->refs++;
4238 this_symtree->n.sym = symtree->n.sym;
4239 return;
4243 /* Otherwise give it a flavor according to such attributes as
4244 it has. */
4245 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
4246 sym->attr.flavor = FL_VARIABLE;
4247 else
4249 sym->attr.flavor = FL_PROCEDURE;
4250 if (sym->attr.dimension)
4251 sym->attr.function = 1;
4255 /* Symbols that are module procedures with results (functions) have
4256 the types and array specification copied for type checking in
4257 procedures that call them, as well as for saving to a module
4258 file. These symbols can't stand the scrutiny that their results
4259 can. */
4260 mp_flag = (sym->result != NULL && sym->result != sym);
4262 /* Assign default type to symbols that need one and don't have one. */
4263 if (sym->ts.type == BT_UNKNOWN)
4265 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
4266 gfc_set_default_type (sym, 1, NULL);
4268 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
4270 /* The specific case of an external procedure should emit an error
4271 in the case that there is no implicit type. */
4272 if (!mp_flag)
4273 gfc_set_default_type (sym, sym->attr.external, NULL);
4274 else
4276 /* Result may be in another namespace. */
4277 resolve_symbol (sym->result);
4279 sym->ts = sym->result->ts;
4280 sym->as = gfc_copy_array_spec (sym->result->as);
4281 sym->attr.dimension = sym->result->attr.dimension;
4282 sym->attr.pointer = sym->result->attr.pointer;
4287 /* Assumed size arrays and assumed shape arrays must be dummy
4288 arguments. */
4290 if (sym->as != NULL
4291 && (sym->as->type == AS_ASSUMED_SIZE
4292 || sym->as->type == AS_ASSUMED_SHAPE)
4293 && sym->attr.dummy == 0)
4295 if (sym->as->type == AS_ASSUMED_SIZE)
4296 gfc_error ("Assumed size array at %L must be a dummy argument",
4297 &sym->declared_at);
4298 else
4299 gfc_error ("Assumed shape array at %L must be a dummy argument",
4300 &sym->declared_at);
4301 return;
4304 /* A parameter array's shape needs to be constant. */
4306 if (sym->attr.flavor == FL_PARAMETER && sym->as != NULL
4307 && !gfc_is_compile_time_shape (sym->as))
4309 gfc_error ("Parameter array '%s' at %L cannot be automatic "
4310 "or assumed shape", sym->name, &sym->declared_at);
4311 return;
4314 /* A module array's shape needs to be constant. */
4316 if (sym->ns->proc_name
4317 && sym->attr.flavor == FL_VARIABLE
4318 && sym->ns->proc_name->attr.flavor == FL_MODULE
4319 && !sym->attr.use_assoc
4320 && !sym->attr.allocatable
4321 && !sym->attr.pointer
4322 && sym->as != NULL
4323 && !gfc_is_compile_time_shape (sym->as))
4325 gfc_error ("Module array '%s' at %L cannot be automatic "
4326 "or assumed shape", sym->name, &sym->declared_at);
4327 return;
4330 /* Make sure that character string variables with assumed length are
4331 dummy arguments. */
4333 if (sym->attr.flavor == FL_VARIABLE && !sym->attr.result
4334 && sym->ts.type == BT_CHARACTER
4335 && sym->ts.cl->length == NULL && sym->attr.dummy == 0)
4337 gfc_error ("Entity with assumed character length at %L must be a "
4338 "dummy argument or a PARAMETER", &sym->declared_at);
4339 return;
4342 /* Make sure a parameter that has been implicitly typed still
4343 matches the implicit type, since PARAMETER statements can precede
4344 IMPLICIT statements. */
4346 if (sym->attr.flavor == FL_PARAMETER
4347 && sym->attr.implicit_type
4348 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym, sym->ns)))
4349 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
4350 "later IMPLICIT type", sym->name, &sym->declared_at);
4352 /* Make sure the types of derived parameters are consistent. This
4353 type checking is deferred until resolution because the type may
4354 refer to a derived type from the host. */
4356 if (sym->attr.flavor == FL_PARAMETER
4357 && sym->ts.type == BT_DERIVED
4358 && !gfc_compare_types (&sym->ts, &sym->value->ts))
4359 gfc_error ("Incompatible derived type in PARAMETER at %L",
4360 &sym->value->where);
4362 /* Make sure symbols with known intent or optional are really dummy
4363 variable. Because of ENTRY statement, this has to be deferred
4364 until resolution time. */
4366 if (! sym->attr.dummy
4367 && (sym->attr.optional
4368 || sym->attr.intent != INTENT_UNKNOWN))
4370 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
4371 return;
4374 if (sym->attr.proc == PROC_ST_FUNCTION)
4376 if (sym->ts.type == BT_CHARACTER)
4378 gfc_charlen *cl = sym->ts.cl;
4379 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
4381 gfc_error ("Character-valued statement function '%s' at %L must "
4382 "have constant length", sym->name, &sym->declared_at);
4383 return;
4388 /* If a derived type symbol has reached this point, without its
4389 type being declared, we have an error. Notice that most
4390 conditions that produce undefined derived types have already
4391 been dealt with. However, the likes of:
4392 implicit type(t) (t) ..... call foo (t) will get us here if
4393 the type is not declared in the scope of the implicit
4394 statement. Change the type to BT_UNKNOWN, both because it is so
4395 and to prevent an ICE. */
4396 if (sym->ts.type == BT_DERIVED
4397 && sym->ts.derived->components == NULL)
4399 gfc_error ("The derived type '%s' at %L is of type '%s', "
4400 "which has not been defined.", sym->name,
4401 &sym->declared_at, sym->ts.derived->name);
4402 sym->ts.type = BT_UNKNOWN;
4403 return;
4406 /* If a component of a derived type is of a type declared to be private,
4407 either the derived type definition must contain the PRIVATE statement,
4408 or the derived type must be private. (4.4.1 just after R427) */
4409 if (sym->attr.flavor == FL_DERIVED
4410 && sym->component_access != ACCESS_PRIVATE
4411 && gfc_check_access(sym->attr.access, sym->ns->default_access))
4413 for (c = sym->components; c; c = c->next)
4415 if (c->ts.type == BT_DERIVED
4416 && !c->ts.derived->attr.use_assoc
4417 && !gfc_check_access(c->ts.derived->attr.access,
4418 c->ts.derived->ns->default_access))
4420 gfc_error ("The component '%s' is a PRIVATE type and cannot be "
4421 "a component of '%s', which is PUBLIC at %L",
4422 c->name, sym->name, &sym->declared_at);
4423 return;
4428 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
4429 default initialization is defined (5.1.2.4.4). */
4430 if (sym->ts.type == BT_DERIVED
4431 && sym->attr.dummy
4432 && sym->attr.intent == INTENT_OUT
4433 && sym->as
4434 && sym->as->type == AS_ASSUMED_SIZE)
4436 for (c = sym->ts.derived->components; c; c = c->next)
4438 if (c->initializer)
4440 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
4441 "ASSUMED SIZE and so cannot have a default initializer",
4442 sym->name, &sym->declared_at);
4443 return;
4449 /* Ensure that derived type formal arguments of a public procedure
4450 are not of a private type. */
4451 if (sym->attr.flavor == FL_PROCEDURE
4452 && gfc_check_access(sym->attr.access, sym->ns->default_access))
4454 for (arg = sym->formal; arg; arg = arg->next)
4456 if (arg->sym
4457 && arg->sym->ts.type == BT_DERIVED
4458 && !arg->sym->ts.derived->attr.use_assoc
4459 && !gfc_check_access(arg->sym->ts.derived->attr.access,
4460 arg->sym->ts.derived->ns->default_access))
4462 gfc_error_now ("'%s' is a PRIVATE type and cannot be "
4463 "a dummy argument of '%s', which is PUBLIC at %L",
4464 arg->sym->name, sym->name, &sym->declared_at);
4465 /* Stop this message from recurring. */
4466 arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
4467 return;
4472 /* Constraints on deferred shape variable. */
4473 if (sym->attr.flavor == FL_VARIABLE
4474 || (sym->attr.flavor == FL_PROCEDURE
4475 && sym->attr.function))
4477 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
4479 if (sym->attr.allocatable)
4481 if (sym->attr.dimension)
4482 gfc_error ("Allocatable array '%s' at %L must have "
4483 "a deferred shape", sym->name, &sym->declared_at);
4484 else
4485 gfc_error ("Scalar object '%s' at %L may not be ALLOCATABLE",
4486 sym->name, &sym->declared_at);
4487 return;
4490 if (sym->attr.pointer && sym->attr.dimension)
4492 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
4493 sym->name, &sym->declared_at);
4494 return;
4498 else
4500 if (!mp_flag && !sym->attr.allocatable
4501 && !sym->attr.pointer && !sym->attr.dummy)
4503 gfc_error ("Array '%s' at %L cannot have a deferred shape",
4504 sym->name, &sym->declared_at);
4505 return;
4510 switch (sym->attr.flavor)
4512 case FL_VARIABLE:
4513 /* Can the symbol have an initializer? */
4514 flag = 0;
4515 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
4516 || sym->attr.intrinsic || sym->attr.result)
4517 flag = 1;
4518 else if (sym->attr.dimension && !sym->attr.pointer)
4520 /* Don't allow initialization of automatic arrays. */
4521 for (i = 0; i < sym->as->rank; i++)
4523 if (sym->as->lower[i] == NULL
4524 || sym->as->lower[i]->expr_type != EXPR_CONSTANT
4525 || sym->as->upper[i] == NULL
4526 || sym->as->upper[i]->expr_type != EXPR_CONSTANT)
4528 flag = 1;
4529 break;
4534 /* Reject illegal initializers. */
4535 if (sym->value && flag)
4537 if (sym->attr.allocatable)
4538 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
4539 sym->name, &sym->declared_at);
4540 else if (sym->attr.external)
4541 gfc_error ("External '%s' at %L cannot have an initializer",
4542 sym->name, &sym->declared_at);
4543 else if (sym->attr.dummy)
4544 gfc_error ("Dummy '%s' at %L cannot have an initializer",
4545 sym->name, &sym->declared_at);
4546 else if (sym->attr.intrinsic)
4547 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
4548 sym->name, &sym->declared_at);
4549 else if (sym->attr.result)
4550 gfc_error ("Function result '%s' at %L cannot have an initializer",
4551 sym->name, &sym->declared_at);
4552 else
4553 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
4554 sym->name, &sym->declared_at);
4555 return;
4558 /* Assign default initializer. */
4559 if (sym->ts.type == BT_DERIVED && !(sym->value || flag)
4560 && !sym->attr.pointer)
4561 sym->value = gfc_default_initializer (&sym->ts);
4562 break;
4564 case FL_NAMELIST:
4565 /* Reject PRIVATE objects in a PUBLIC namelist. */
4566 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
4568 for (nl = sym->namelist; nl; nl = nl->next)
4570 if (!nl->sym->attr.use_assoc
4572 !(sym->ns->parent == nl->sym->ns)
4574 !gfc_check_access(nl->sym->attr.access,
4575 nl->sym->ns->default_access))
4576 gfc_error ("PRIVATE symbol '%s' cannot be member of "
4577 "PUBLIC namelist at %L", nl->sym->name,
4578 &sym->declared_at);
4581 break;
4583 default:
4585 /* An external symbol falls through to here if it is not referenced. */
4586 if (sym->attr.external && sym->value)
4588 gfc_error ("External object '%s' at %L may not have an initializer",
4589 sym->name, &sym->declared_at);
4590 return;
4593 break;
4597 /* Make sure that intrinsic exist */
4598 if (sym->attr.intrinsic
4599 && ! gfc_intrinsic_name(sym->name, 0)
4600 && ! gfc_intrinsic_name(sym->name, 1))
4601 gfc_error("Intrinsic at %L does not exist", &sym->declared_at);
4603 /* Resolve array specifier. Check as well some constraints
4604 on COMMON blocks. */
4606 check_constant = sym->attr.in_common && !sym->attr.pointer;
4607 gfc_resolve_array_spec (sym->as, check_constant);
4609 /* Resolve formal namespaces. */
4611 if (formal_ns_flag && sym != NULL && sym->formal_ns != NULL)
4613 formal_ns_save = formal_ns_flag;
4614 formal_ns_flag = 0;
4615 gfc_resolve (sym->formal_ns);
4616 formal_ns_flag = formal_ns_save;
4622 /************* Resolve DATA statements *************/
4624 static struct
4626 gfc_data_value *vnode;
4627 unsigned int left;
4629 values;
4632 /* Advance the values structure to point to the next value in the data list. */
4634 static try
4635 next_data_value (void)
4637 while (values.left == 0)
4639 if (values.vnode->next == NULL)
4640 return FAILURE;
4642 values.vnode = values.vnode->next;
4643 values.left = values.vnode->repeat;
4646 return SUCCESS;
4650 static try
4651 check_data_variable (gfc_data_variable * var, locus * where)
4653 gfc_expr *e;
4654 mpz_t size;
4655 mpz_t offset;
4656 try t;
4657 ar_type mark = AR_UNKNOWN;
4658 int i;
4659 mpz_t section_index[GFC_MAX_DIMENSIONS];
4660 gfc_ref *ref;
4661 gfc_array_ref *ar;
4663 if (gfc_resolve_expr (var->expr) == FAILURE)
4664 return FAILURE;
4666 ar = NULL;
4667 mpz_init_set_si (offset, 0);
4668 e = var->expr;
4670 if (e->expr_type != EXPR_VARIABLE)
4671 gfc_internal_error ("check_data_variable(): Bad expression");
4673 if (e->rank == 0)
4675 mpz_init_set_ui (size, 1);
4676 ref = NULL;
4678 else
4680 ref = e->ref;
4682 /* Find the array section reference. */
4683 for (ref = e->ref; ref; ref = ref->next)
4685 if (ref->type != REF_ARRAY)
4686 continue;
4687 if (ref->u.ar.type == AR_ELEMENT)
4688 continue;
4689 break;
4691 gcc_assert (ref);
4693 /* Set marks according to the reference pattern. */
4694 switch (ref->u.ar.type)
4696 case AR_FULL:
4697 mark = AR_FULL;
4698 break;
4700 case AR_SECTION:
4701 ar = &ref->u.ar;
4702 /* Get the start position of array section. */
4703 gfc_get_section_index (ar, section_index, &offset);
4704 mark = AR_SECTION;
4705 break;
4707 default:
4708 gcc_unreachable ();
4711 if (gfc_array_size (e, &size) == FAILURE)
4713 gfc_error ("Nonconstant array section at %L in DATA statement",
4714 &e->where);
4715 mpz_clear (offset);
4716 return FAILURE;
4720 t = SUCCESS;
4722 while (mpz_cmp_ui (size, 0) > 0)
4724 if (next_data_value () == FAILURE)
4726 gfc_error ("DATA statement at %L has more variables than values",
4727 where);
4728 t = FAILURE;
4729 break;
4732 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
4733 if (t == FAILURE)
4734 break;
4736 /* If we have more than one element left in the repeat count,
4737 and we have more than one element left in the target variable,
4738 then create a range assignment. */
4739 /* ??? Only done for full arrays for now, since array sections
4740 seem tricky. */
4741 if (mark == AR_FULL && ref && ref->next == NULL
4742 && values.left > 1 && mpz_cmp_ui (size, 1) > 0)
4744 mpz_t range;
4746 if (mpz_cmp_ui (size, values.left) >= 0)
4748 mpz_init_set_ui (range, values.left);
4749 mpz_sub_ui (size, size, values.left);
4750 values.left = 0;
4752 else
4754 mpz_init_set (range, size);
4755 values.left -= mpz_get_ui (size);
4756 mpz_set_ui (size, 0);
4759 gfc_assign_data_value_range (var->expr, values.vnode->expr,
4760 offset, range);
4762 mpz_add (offset, offset, range);
4763 mpz_clear (range);
4766 /* Assign initial value to symbol. */
4767 else
4769 values.left -= 1;
4770 mpz_sub_ui (size, size, 1);
4772 gfc_assign_data_value (var->expr, values.vnode->expr, offset);
4774 if (mark == AR_FULL)
4775 mpz_add_ui (offset, offset, 1);
4777 /* Modify the array section indexes and recalculate the offset
4778 for next element. */
4779 else if (mark == AR_SECTION)
4780 gfc_advance_section (section_index, ar, &offset);
4784 if (mark == AR_SECTION)
4786 for (i = 0; i < ar->dimen; i++)
4787 mpz_clear (section_index[i]);
4790 mpz_clear (size);
4791 mpz_clear (offset);
4793 return t;
4797 static try traverse_data_var (gfc_data_variable *, locus *);
4799 /* Iterate over a list of elements in a DATA statement. */
4801 static try
4802 traverse_data_list (gfc_data_variable * var, locus * where)
4804 mpz_t trip;
4805 iterator_stack frame;
4806 gfc_expr *e;
4808 mpz_init (frame.value);
4810 mpz_init_set (trip, var->iter.end->value.integer);
4811 mpz_sub (trip, trip, var->iter.start->value.integer);
4812 mpz_add (trip, trip, var->iter.step->value.integer);
4814 mpz_div (trip, trip, var->iter.step->value.integer);
4816 mpz_set (frame.value, var->iter.start->value.integer);
4818 frame.prev = iter_stack;
4819 frame.variable = var->iter.var->symtree;
4820 iter_stack = &frame;
4822 while (mpz_cmp_ui (trip, 0) > 0)
4824 if (traverse_data_var (var->list, where) == FAILURE)
4826 mpz_clear (trip);
4827 return FAILURE;
4830 e = gfc_copy_expr (var->expr);
4831 if (gfc_simplify_expr (e, 1) == FAILURE)
4833 gfc_free_expr (e);
4834 return FAILURE;
4837 mpz_add (frame.value, frame.value, var->iter.step->value.integer);
4839 mpz_sub_ui (trip, trip, 1);
4842 mpz_clear (trip);
4843 mpz_clear (frame.value);
4845 iter_stack = frame.prev;
4846 return SUCCESS;
4850 /* Type resolve variables in the variable list of a DATA statement. */
4852 static try
4853 traverse_data_var (gfc_data_variable * var, locus * where)
4855 try t;
4857 for (; var; var = var->next)
4859 if (var->expr == NULL)
4860 t = traverse_data_list (var, where);
4861 else
4862 t = check_data_variable (var, where);
4864 if (t == FAILURE)
4865 return FAILURE;
4868 return SUCCESS;
4872 /* Resolve the expressions and iterators associated with a data statement.
4873 This is separate from the assignment checking because data lists should
4874 only be resolved once. */
4876 static try
4877 resolve_data_variables (gfc_data_variable * d)
4879 for (; d; d = d->next)
4881 if (d->list == NULL)
4883 if (gfc_resolve_expr (d->expr) == FAILURE)
4884 return FAILURE;
4886 else
4888 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
4889 return FAILURE;
4891 if (d->iter.start->expr_type != EXPR_CONSTANT
4892 || d->iter.end->expr_type != EXPR_CONSTANT
4893 || d->iter.step->expr_type != EXPR_CONSTANT)
4894 gfc_internal_error ("resolve_data_variables(): Bad iterator");
4896 if (resolve_data_variables (d->list) == FAILURE)
4897 return FAILURE;
4901 return SUCCESS;
4905 /* Resolve a single DATA statement. We implement this by storing a pointer to
4906 the value list into static variables, and then recursively traversing the
4907 variables list, expanding iterators and such. */
4909 static void
4910 resolve_data (gfc_data * d)
4912 if (resolve_data_variables (d->var) == FAILURE)
4913 return;
4915 values.vnode = d->value;
4916 values.left = (d->value == NULL) ? 0 : d->value->repeat;
4918 if (traverse_data_var (d->var, &d->where) == FAILURE)
4919 return;
4921 /* At this point, we better not have any values left. */
4923 if (next_data_value () == SUCCESS)
4924 gfc_error ("DATA statement at %L has more values than variables",
4925 &d->where);
4929 /* Determines if a variable is not 'pure', ie not assignable within a pure
4930 procedure. Returns zero if assignment is OK, nonzero if there is a problem.
4934 gfc_impure_variable (gfc_symbol * sym)
4936 if (sym->attr.use_assoc || sym->attr.in_common)
4937 return 1;
4939 if (sym->ns != gfc_current_ns)
4940 return !sym->attr.function;
4942 /* TODO: Check storage association through EQUIVALENCE statements */
4944 return 0;
4948 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
4949 symbol of the current procedure. */
4952 gfc_pure (gfc_symbol * sym)
4954 symbol_attribute attr;
4956 if (sym == NULL)
4957 sym = gfc_current_ns->proc_name;
4958 if (sym == NULL)
4959 return 0;
4961 attr = sym->attr;
4963 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
4967 /* Test whether the current procedure is elemental or not. */
4970 gfc_elemental (gfc_symbol * sym)
4972 symbol_attribute attr;
4974 if (sym == NULL)
4975 sym = gfc_current_ns->proc_name;
4976 if (sym == NULL)
4977 return 0;
4978 attr = sym->attr;
4980 return attr.flavor == FL_PROCEDURE && attr.elemental;
4984 /* Warn about unused labels. */
4986 static void
4987 warn_unused_label (gfc_namespace * ns)
4989 gfc_st_label *l;
4991 l = ns->st_labels;
4992 if (l == NULL)
4993 return;
4995 while (l->next)
4996 l = l->next;
4998 for (; l; l = l->prev)
5000 if (l->defined == ST_LABEL_UNKNOWN)
5001 continue;
5003 switch (l->referenced)
5005 case ST_LABEL_UNKNOWN:
5006 gfc_warning ("Label %d at %L defined but not used", l->value,
5007 &l->where);
5008 break;
5010 case ST_LABEL_BAD_TARGET:
5011 gfc_warning ("Label %d at %L defined but cannot be used", l->value,
5012 &l->where);
5013 break;
5015 default:
5016 break;
5022 /* Returns the sequence type of a symbol or sequence. */
5024 static seq_type
5025 sequence_type (gfc_typespec ts)
5027 seq_type result;
5028 gfc_component *c;
5030 switch (ts.type)
5032 case BT_DERIVED:
5034 if (ts.derived->components == NULL)
5035 return SEQ_NONDEFAULT;
5037 result = sequence_type (ts.derived->components->ts);
5038 for (c = ts.derived->components->next; c; c = c->next)
5039 if (sequence_type (c->ts) != result)
5040 return SEQ_MIXED;
5042 return result;
5044 case BT_CHARACTER:
5045 if (ts.kind != gfc_default_character_kind)
5046 return SEQ_NONDEFAULT;
5048 return SEQ_CHARACTER;
5050 case BT_INTEGER:
5051 if (ts.kind != gfc_default_integer_kind)
5052 return SEQ_NONDEFAULT;
5054 return SEQ_NUMERIC;
5056 case BT_REAL:
5057 if (!(ts.kind == gfc_default_real_kind
5058 || ts.kind == gfc_default_double_kind))
5059 return SEQ_NONDEFAULT;
5061 return SEQ_NUMERIC;
5063 case BT_COMPLEX:
5064 if (ts.kind != gfc_default_complex_kind)
5065 return SEQ_NONDEFAULT;
5067 return SEQ_NUMERIC;
5069 case BT_LOGICAL:
5070 if (ts.kind != gfc_default_logical_kind)
5071 return SEQ_NONDEFAULT;
5073 return SEQ_NUMERIC;
5075 default:
5076 return SEQ_NONDEFAULT;
5081 /* Resolve derived type EQUIVALENCE object. */
5083 static try
5084 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
5086 gfc_symbol *d;
5087 gfc_component *c = derived->components;
5089 if (!derived)
5090 return SUCCESS;
5092 /* Shall not be an object of nonsequence derived type. */
5093 if (!derived->attr.sequence)
5095 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
5096 "attribute to be an EQUIVALENCE object", sym->name, &e->where);
5097 return FAILURE;
5100 for (; c ; c = c->next)
5102 d = c->ts.derived;
5103 if (d && (resolve_equivalence_derived (c->ts.derived, sym, e) == FAILURE))
5104 return FAILURE;
5106 /* Shall not be an object of sequence derived type containing a pointer
5107 in the structure. */
5108 if (c->pointer)
5110 gfc_error ("Derived type variable '%s' at %L with pointer component(s) "
5111 "cannot be an EQUIVALENCE object", sym->name, &e->where);
5112 return FAILURE;
5115 if (c->initializer)
5117 gfc_error ("Derived type variable '%s' at %L with default initializer "
5118 "cannot be an EQUIVALENCE object", sym->name, &e->where);
5119 return FAILURE;
5122 return SUCCESS;
5126 /* Resolve equivalence object.
5127 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
5128 an allocatable array, an object of nonsequence derived type, an object of
5129 sequence derived type containing a pointer at any level of component
5130 selection, an automatic object, a function name, an entry name, a result
5131 name, a named constant, a structure component, or a subobject of any of
5132 the preceding objects. A substring shall not have length zero. A
5133 derived type shall not have components with default initialization nor
5134 shall two objects of an equivalence group be initialized.
5135 The simple constraints are done in symbol.c(check_conflict) and the rest
5136 are implemented here. */
5138 static void
5139 resolve_equivalence (gfc_equiv *eq)
5141 gfc_symbol *sym;
5142 gfc_symbol *derived;
5143 gfc_symbol *first_sym;
5144 gfc_expr *e;
5145 gfc_ref *r;
5146 locus *last_where = NULL;
5147 seq_type eq_type, last_eq_type;
5148 gfc_typespec *last_ts;
5149 int object;
5150 const char *value_name;
5151 const char *msg;
5153 value_name = NULL;
5154 last_ts = &eq->expr->symtree->n.sym->ts;
5156 first_sym = eq->expr->symtree->n.sym;
5158 for (object = 1; eq; eq = eq->eq, object++)
5160 e = eq->expr;
5162 e->ts = e->symtree->n.sym->ts;
5163 /* match_varspec might not know yet if it is seeing
5164 array reference or substring reference, as it doesn't
5165 know the types. */
5166 if (e->ref && e->ref->type == REF_ARRAY)
5168 gfc_ref *ref = e->ref;
5169 sym = e->symtree->n.sym;
5171 if (sym->attr.dimension)
5173 ref->u.ar.as = sym->as;
5174 ref = ref->next;
5177 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
5178 if (e->ts.type == BT_CHARACTER
5179 && ref
5180 && ref->type == REF_ARRAY
5181 && ref->u.ar.dimen == 1
5182 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
5183 && ref->u.ar.stride[0] == NULL)
5185 gfc_expr *start = ref->u.ar.start[0];
5186 gfc_expr *end = ref->u.ar.end[0];
5187 void *mem = NULL;
5189 /* Optimize away the (:) reference. */
5190 if (start == NULL && end == NULL)
5192 if (e->ref == ref)
5193 e->ref = ref->next;
5194 else
5195 e->ref->next = ref->next;
5196 mem = ref;
5198 else
5200 ref->type = REF_SUBSTRING;
5201 if (start == NULL)
5202 start = gfc_int_expr (1);
5203 ref->u.ss.start = start;
5204 if (end == NULL && e->ts.cl)
5205 end = gfc_copy_expr (e->ts.cl->length);
5206 ref->u.ss.end = end;
5207 ref->u.ss.length = e->ts.cl;
5208 e->ts.cl = NULL;
5210 ref = ref->next;
5211 gfc_free (mem);
5214 /* Any further ref is an error. */
5215 if (ref)
5217 gcc_assert (ref->type == REF_ARRAY);
5218 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
5219 &ref->u.ar.where);
5220 continue;
5224 if (gfc_resolve_expr (e) == FAILURE)
5225 continue;
5227 sym = e->symtree->n.sym;
5229 /* An equivalence statement cannot have more than one initialized
5230 object. */
5231 if (sym->value)
5233 if (value_name != NULL)
5235 gfc_error ("Initialized objects '%s' and '%s' cannot both "
5236 "be in the EQUIVALENCE statement at %L",
5237 value_name, sym->name, &e->where);
5238 continue;
5240 else
5241 value_name = sym->name;
5244 /* Shall not equivalence common block variables in a PURE procedure. */
5245 if (sym->ns->proc_name
5246 && sym->ns->proc_name->attr.pure
5247 && sym->attr.in_common)
5249 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
5250 "object in the pure procedure '%s'",
5251 sym->name, &e->where, sym->ns->proc_name->name);
5252 break;
5255 /* Shall not be a named constant. */
5256 if (e->expr_type == EXPR_CONSTANT)
5258 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
5259 "object", sym->name, &e->where);
5260 continue;
5263 derived = e->ts.derived;
5264 if (derived && resolve_equivalence_derived (derived, sym, e) == FAILURE)
5265 continue;
5267 /* Check that the types correspond correctly:
5268 Note 5.28:
5269 A numeric sequence structure may be equivalenced to another sequence
5270 structure, an object of default integer type, default real type, double
5271 precision real type, default logical type such that components of the
5272 structure ultimately only become associated to objects of the same
5273 kind. A character sequence structure may be equivalenced to an object
5274 of default character kind or another character sequence structure.
5275 Other objects may be equivalenced only to objects of the same type and
5276 kind parameters. */
5278 /* Identical types are unconditionally OK. */
5279 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
5280 goto identical_types;
5282 last_eq_type = sequence_type (*last_ts);
5283 eq_type = sequence_type (sym->ts);
5285 /* Since the pair of objects is not of the same type, mixed or
5286 non-default sequences can be rejected. */
5288 msg = "Sequence %s with mixed components in EQUIVALENCE "
5289 "statement at %L with different type objects";
5290 if ((object ==2
5291 && last_eq_type == SEQ_MIXED
5292 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
5293 last_where) == FAILURE)
5294 || (eq_type == SEQ_MIXED
5295 && gfc_notify_std (GFC_STD_GNU, msg,sym->name,
5296 &e->where) == FAILURE))
5297 continue;
5299 msg = "Non-default type object or sequence %s in EQUIVALENCE "
5300 "statement at %L with objects of different type";
5301 if ((object ==2
5302 && last_eq_type == SEQ_NONDEFAULT
5303 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
5304 last_where) == FAILURE)
5305 || (eq_type == SEQ_NONDEFAULT
5306 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
5307 &e->where) == FAILURE))
5308 continue;
5310 msg ="Non-CHARACTER object '%s' in default CHARACTER "
5311 "EQUIVALENCE statement at %L";
5312 if (last_eq_type == SEQ_CHARACTER
5313 && eq_type != SEQ_CHARACTER
5314 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
5315 &e->where) == FAILURE)
5316 continue;
5318 msg ="Non-NUMERIC object '%s' in default NUMERIC "
5319 "EQUIVALENCE statement at %L";
5320 if (last_eq_type == SEQ_NUMERIC
5321 && eq_type != SEQ_NUMERIC
5322 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
5323 &e->where) == FAILURE)
5324 continue;
5326 identical_types:
5327 last_ts =&sym->ts;
5328 last_where = &e->where;
5330 if (!e->ref)
5331 continue;
5333 /* Shall not be an automatic array. */
5334 if (e->ref->type == REF_ARRAY
5335 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
5337 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
5338 "an EQUIVALENCE object", sym->name, &e->where);
5339 continue;
5342 r = e->ref;
5343 while (r)
5345 /* Shall not be a structure component. */
5346 if (r->type == REF_COMPONENT)
5348 gfc_error ("Structure component '%s' at %L cannot be an "
5349 "EQUIVALENCE object",
5350 r->u.c.component->name, &e->where);
5351 break;
5354 /* A substring shall not have length zero. */
5355 if (r->type == REF_SUBSTRING)
5357 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
5359 gfc_error ("Substring at %L has length zero",
5360 &r->u.ss.start->where);
5361 break;
5364 r = r->next;
5370 /* Resolve function and ENTRY types, issue diagnostics if needed. */
5372 static void
5373 resolve_fntype (gfc_namespace * ns)
5375 gfc_entry_list *el;
5376 gfc_symbol *sym;
5378 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
5379 return;
5381 /* If there are any entries, ns->proc_name is the entry master
5382 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
5383 if (ns->entries)
5384 sym = ns->entries->sym;
5385 else
5386 sym = ns->proc_name;
5387 if (sym->result == sym
5388 && sym->ts.type == BT_UNKNOWN
5389 && gfc_set_default_type (sym, 0, NULL) == FAILURE
5390 && !sym->attr.untyped)
5392 gfc_error ("Function '%s' at %L has no IMPLICIT type",
5393 sym->name, &sym->declared_at);
5394 sym->attr.untyped = 1;
5397 if (ns->entries)
5398 for (el = ns->entries->next; el; el = el->next)
5400 if (el->sym->result == el->sym
5401 && el->sym->ts.type == BT_UNKNOWN
5402 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
5403 && !el->sym->attr.untyped)
5405 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
5406 el->sym->name, &el->sym->declared_at);
5407 el->sym->attr.untyped = 1;
5413 /* This function is called after a complete program unit has been compiled.
5414 Its purpose is to examine all of the expressions associated with a program
5415 unit, assign types to all intermediate expressions, make sure that all
5416 assignments are to compatible types and figure out which names refer to
5417 which functions or subroutines. */
5419 void
5420 gfc_resolve (gfc_namespace * ns)
5422 gfc_namespace *old_ns, *n;
5423 gfc_charlen *cl;
5424 gfc_data *d;
5425 gfc_equiv *eq;
5427 old_ns = gfc_current_ns;
5428 gfc_current_ns = ns;
5430 resolve_entries (ns);
5432 resolve_contained_functions (ns);
5434 gfc_traverse_ns (ns, resolve_symbol);
5436 resolve_fntype (ns);
5438 for (n = ns->contained; n; n = n->sibling)
5440 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
5441 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
5442 "also be PURE", n->proc_name->name,
5443 &n->proc_name->declared_at);
5445 gfc_resolve (n);
5448 forall_flag = 0;
5449 gfc_check_interfaces (ns);
5451 for (cl = ns->cl_list; cl; cl = cl->next)
5453 if (cl->length == NULL || gfc_resolve_expr (cl->length) == FAILURE)
5454 continue;
5456 if (gfc_simplify_expr (cl->length, 0) == FAILURE)
5457 continue;
5459 if (gfc_specification_expr (cl->length) == FAILURE)
5460 continue;
5463 gfc_traverse_ns (ns, resolve_values);
5465 if (ns->save_all)
5466 gfc_save_all (ns);
5468 iter_stack = NULL;
5469 for (d = ns->data; d; d = d->next)
5470 resolve_data (d);
5472 iter_stack = NULL;
5473 gfc_traverse_ns (ns, gfc_formalize_init_value);
5475 for (eq = ns->equiv; eq; eq = eq->next)
5476 resolve_equivalence (eq);
5478 cs_base = NULL;
5479 resolve_code (ns->code, ns);
5481 /* Warn about unused labels. */
5482 if (gfc_option.warn_unused_labels)
5483 warn_unused_label (ns);
5485 gfc_current_ns = old_ns;