Merged revision 156805 into branch.
[official-gcc.git] / gcc / fortran / resolve.c
blobbcc8eaeddb552f842abf25eafa30f47f8d0daf3b
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "flags.h"
25 #include "gfortran.h"
26 #include "obstack.h"
27 #include "bitmap.h"
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
30 #include "data.h"
31 #include "target-memory.h" /* for gfc_simplify_transfer */
33 /* Types used in equivalence statements. */
35 typedef enum seq_type
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
39 seq_type;
41 /* Stack to keep track of the nesting of blocks as we move through the
42 code. See resolve_branch() and resolve_code(). */
44 typedef struct code_stack
46 struct gfc_code *head, *current;
47 struct code_stack *prev;
49 /* This bitmap keeps track of the targets valid for a branch from
50 inside this block except for END {IF|SELECT}s of enclosing
51 blocks. */
52 bitmap reachable_labels;
54 code_stack;
56 static code_stack *cs_base = NULL;
59 /* Nonzero if we're inside a FORALL block. */
61 static int forall_flag;
63 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
65 static int omp_workshare_flag;
67 /* Nonzero if we are processing a formal arglist. The corresponding function
68 resets the flag each time that it is read. */
69 static int formal_arg_flag = 0;
71 /* True if we are resolving a specification expression. */
72 static int specification_expr = 0;
74 /* The id of the last entry seen. */
75 static int current_entry_id;
77 /* We use bitmaps to determine if a branch target is valid. */
78 static bitmap_obstack labels_obstack;
80 int
81 gfc_is_formal_arg (void)
83 return formal_arg_flag;
86 /* Is the symbol host associated? */
87 static bool
88 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
90 for (ns = ns->parent; ns; ns = ns->parent)
92 if (sym->ns == ns)
93 return true;
96 return false;
99 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
100 an ABSTRACT derived-type. If where is not NULL, an error message with that
101 locus is printed, optionally using name. */
103 static gfc_try
104 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
106 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
108 if (where)
110 if (name)
111 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
112 name, where, ts->u.derived->name);
113 else
114 gfc_error ("ABSTRACT type '%s' used at %L",
115 ts->u.derived->name, where);
118 return FAILURE;
121 return SUCCESS;
125 /* Resolve types of formal argument lists. These have to be done early so that
126 the formal argument lists of module procedures can be copied to the
127 containing module before the individual procedures are resolved
128 individually. We also resolve argument lists of procedures in interface
129 blocks because they are self-contained scoping units.
131 Since a dummy argument cannot be a non-dummy procedure, the only
132 resort left for untyped names are the IMPLICIT types. */
134 static void
135 resolve_formal_arglist (gfc_symbol *proc)
137 gfc_formal_arglist *f;
138 gfc_symbol *sym;
139 int i;
141 if (proc->result != NULL)
142 sym = proc->result;
143 else
144 sym = proc;
146 if (gfc_elemental (proc)
147 || sym->attr.pointer || sym->attr.allocatable
148 || (sym->as && sym->as->rank > 0))
150 proc->attr.always_explicit = 1;
151 sym->attr.always_explicit = 1;
154 formal_arg_flag = 1;
156 for (f = proc->formal; f; f = f->next)
158 sym = f->sym;
160 if (sym == NULL)
162 /* Alternate return placeholder. */
163 if (gfc_elemental (proc))
164 gfc_error ("Alternate return specifier in elemental subroutine "
165 "'%s' at %L is not allowed", proc->name,
166 &proc->declared_at);
167 if (proc->attr.function)
168 gfc_error ("Alternate return specifier in function "
169 "'%s' at %L is not allowed", proc->name,
170 &proc->declared_at);
171 continue;
174 if (sym->attr.if_source != IFSRC_UNKNOWN)
175 resolve_formal_arglist (sym);
177 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
179 if (gfc_pure (proc) && !gfc_pure (sym))
181 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
182 "also be PURE", sym->name, &sym->declared_at);
183 continue;
186 if (gfc_elemental (proc))
188 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
189 "procedure", &sym->declared_at);
190 continue;
193 if (sym->attr.function
194 && sym->ts.type == BT_UNKNOWN
195 && sym->attr.intrinsic)
197 gfc_intrinsic_sym *isym;
198 isym = gfc_find_function (sym->name);
199 if (isym == NULL || !isym->specific)
201 gfc_error ("Unable to find a specific INTRINSIC procedure "
202 "for the reference '%s' at %L", sym->name,
203 &sym->declared_at);
205 sym->ts = isym->ts;
208 continue;
211 if (sym->ts.type == BT_UNKNOWN)
213 if (!sym->attr.function || sym->result == sym)
214 gfc_set_default_type (sym, 1, sym->ns);
217 gfc_resolve_array_spec (sym->as, 0);
219 /* We can't tell if an array with dimension (:) is assumed or deferred
220 shape until we know if it has the pointer or allocatable attributes.
222 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
223 && !(sym->attr.pointer || sym->attr.allocatable))
225 sym->as->type = AS_ASSUMED_SHAPE;
226 for (i = 0; i < sym->as->rank; i++)
227 sym->as->lower[i] = gfc_int_expr (1);
230 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
231 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
232 || sym->attr.optional)
234 proc->attr.always_explicit = 1;
235 if (proc->result)
236 proc->result->attr.always_explicit = 1;
239 /* If the flavor is unknown at this point, it has to be a variable.
240 A procedure specification would have already set the type. */
242 if (sym->attr.flavor == FL_UNKNOWN)
243 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
245 if (gfc_pure (proc) && !sym->attr.pointer
246 && sym->attr.flavor != FL_PROCEDURE)
248 if (proc->attr.function && sym->attr.intent != INTENT_IN)
249 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
250 "INTENT(IN)", sym->name, proc->name,
251 &sym->declared_at);
253 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
254 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
255 "have its INTENT specified", sym->name, proc->name,
256 &sym->declared_at);
259 if (gfc_elemental (proc))
261 if (sym->as != NULL)
263 gfc_error ("Argument '%s' of elemental procedure at %L must "
264 "be scalar", sym->name, &sym->declared_at);
265 continue;
268 if (sym->attr.pointer)
270 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
271 "have the POINTER attribute", sym->name,
272 &sym->declared_at);
273 continue;
276 if (sym->attr.flavor == FL_PROCEDURE)
278 gfc_error ("Dummy procedure '%s' not allowed in elemental "
279 "procedure '%s' at %L", sym->name, proc->name,
280 &sym->declared_at);
281 continue;
285 /* Each dummy shall be specified to be scalar. */
286 if (proc->attr.proc == PROC_ST_FUNCTION)
288 if (sym->as != NULL)
290 gfc_error ("Argument '%s' of statement function at %L must "
291 "be scalar", sym->name, &sym->declared_at);
292 continue;
295 if (sym->ts.type == BT_CHARACTER)
297 gfc_charlen *cl = sym->ts.u.cl;
298 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
300 gfc_error ("Character-valued argument '%s' of statement "
301 "function at %L must have constant length",
302 sym->name, &sym->declared_at);
303 continue;
308 formal_arg_flag = 0;
312 /* Work function called when searching for symbols that have argument lists
313 associated with them. */
315 static void
316 find_arglists (gfc_symbol *sym)
318 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
319 return;
321 resolve_formal_arglist (sym);
325 /* Given a namespace, resolve all formal argument lists within the namespace.
328 static void
329 resolve_formal_arglists (gfc_namespace *ns)
331 if (ns == NULL)
332 return;
334 gfc_traverse_ns (ns, find_arglists);
338 static void
339 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
341 gfc_try t;
343 /* If this namespace is not a function or an entry master function,
344 ignore it. */
345 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
346 || sym->attr.entry_master)
347 return;
349 /* Try to find out of what the return type is. */
350 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
352 t = gfc_set_default_type (sym->result, 0, ns);
354 if (t == FAILURE && !sym->result->attr.untyped)
356 if (sym->result == sym)
357 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
358 sym->name, &sym->declared_at);
359 else if (!sym->result->attr.proc_pointer)
360 gfc_error ("Result '%s' of contained function '%s' at %L has "
361 "no IMPLICIT type", sym->result->name, sym->name,
362 &sym->result->declared_at);
363 sym->result->attr.untyped = 1;
367 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
368 type, lists the only ways a character length value of * can be used:
369 dummy arguments of procedures, named constants, and function results
370 in external functions. Internal function results and results of module
371 procedures are not on this list, ergo, not permitted. */
373 if (sym->result->ts.type == BT_CHARACTER)
375 gfc_charlen *cl = sym->result->ts.u.cl;
376 if (!cl || !cl->length)
378 /* See if this is a module-procedure and adapt error message
379 accordingly. */
380 bool module_proc;
381 gcc_assert (ns->parent && ns->parent->proc_name);
382 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
384 gfc_error ("Character-valued %s '%s' at %L must not be"
385 " assumed length",
386 module_proc ? _("module procedure")
387 : _("internal function"),
388 sym->name, &sym->declared_at);
394 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
395 introduce duplicates. */
397 static void
398 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
400 gfc_formal_arglist *f, *new_arglist;
401 gfc_symbol *new_sym;
403 for (; new_args != NULL; new_args = new_args->next)
405 new_sym = new_args->sym;
406 /* See if this arg is already in the formal argument list. */
407 for (f = proc->formal; f; f = f->next)
409 if (new_sym == f->sym)
410 break;
413 if (f)
414 continue;
416 /* Add a new argument. Argument order is not important. */
417 new_arglist = gfc_get_formal_arglist ();
418 new_arglist->sym = new_sym;
419 new_arglist->next = proc->formal;
420 proc->formal = new_arglist;
425 /* Flag the arguments that are not present in all entries. */
427 static void
428 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
430 gfc_formal_arglist *f, *head;
431 head = new_args;
433 for (f = proc->formal; f; f = f->next)
435 if (f->sym == NULL)
436 continue;
438 for (new_args = head; new_args; new_args = new_args->next)
440 if (new_args->sym == f->sym)
441 break;
444 if (new_args)
445 continue;
447 f->sym->attr.not_always_present = 1;
452 /* Resolve alternate entry points. If a symbol has multiple entry points we
453 create a new master symbol for the main routine, and turn the existing
454 symbol into an entry point. */
456 static void
457 resolve_entries (gfc_namespace *ns)
459 gfc_namespace *old_ns;
460 gfc_code *c;
461 gfc_symbol *proc;
462 gfc_entry_list *el;
463 char name[GFC_MAX_SYMBOL_LEN + 1];
464 static int master_count = 0;
466 if (ns->proc_name == NULL)
467 return;
469 /* No need to do anything if this procedure doesn't have alternate entry
470 points. */
471 if (!ns->entries)
472 return;
474 /* We may already have resolved alternate entry points. */
475 if (ns->proc_name->attr.entry_master)
476 return;
478 /* If this isn't a procedure something has gone horribly wrong. */
479 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
481 /* Remember the current namespace. */
482 old_ns = gfc_current_ns;
484 gfc_current_ns = ns;
486 /* Add the main entry point to the list of entry points. */
487 el = gfc_get_entry_list ();
488 el->sym = ns->proc_name;
489 el->id = 0;
490 el->next = ns->entries;
491 ns->entries = el;
492 ns->proc_name->attr.entry = 1;
494 /* If it is a module function, it needs to be in the right namespace
495 so that gfc_get_fake_result_decl can gather up the results. The
496 need for this arose in get_proc_name, where these beasts were
497 left in their own namespace, to keep prior references linked to
498 the entry declaration.*/
499 if (ns->proc_name->attr.function
500 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
501 el->sym->ns = ns;
503 /* Do the same for entries where the master is not a module
504 procedure. These are retained in the module namespace because
505 of the module procedure declaration. */
506 for (el = el->next; el; el = el->next)
507 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
508 && el->sym->attr.mod_proc)
509 el->sym->ns = ns;
510 el = ns->entries;
512 /* Add an entry statement for it. */
513 c = gfc_get_code ();
514 c->op = EXEC_ENTRY;
515 c->ext.entry = el;
516 c->next = ns->code;
517 ns->code = c;
519 /* Create a new symbol for the master function. */
520 /* Give the internal function a unique name (within this file).
521 Also include the function name so the user has some hope of figuring
522 out what is going on. */
523 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
524 master_count++, ns->proc_name->name);
525 gfc_get_ha_symbol (name, &proc);
526 gcc_assert (proc != NULL);
528 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
529 if (ns->proc_name->attr.subroutine)
530 gfc_add_subroutine (&proc->attr, proc->name, NULL);
531 else
533 gfc_symbol *sym;
534 gfc_typespec *ts, *fts;
535 gfc_array_spec *as, *fas;
536 gfc_add_function (&proc->attr, proc->name, NULL);
537 proc->result = proc;
538 fas = ns->entries->sym->as;
539 fas = fas ? fas : ns->entries->sym->result->as;
540 fts = &ns->entries->sym->result->ts;
541 if (fts->type == BT_UNKNOWN)
542 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
543 for (el = ns->entries->next; el; el = el->next)
545 ts = &el->sym->result->ts;
546 as = el->sym->as;
547 as = as ? as : el->sym->result->as;
548 if (ts->type == BT_UNKNOWN)
549 ts = gfc_get_default_type (el->sym->result->name, NULL);
551 if (! gfc_compare_types (ts, fts)
552 || (el->sym->result->attr.dimension
553 != ns->entries->sym->result->attr.dimension)
554 || (el->sym->result->attr.pointer
555 != ns->entries->sym->result->attr.pointer))
556 break;
557 else if (as && fas && ns->entries->sym->result != el->sym->result
558 && gfc_compare_array_spec (as, fas) == 0)
559 gfc_error ("Function %s at %L has entries with mismatched "
560 "array specifications", ns->entries->sym->name,
561 &ns->entries->sym->declared_at);
562 /* The characteristics need to match and thus both need to have
563 the same string length, i.e. both len=*, or both len=4.
564 Having both len=<variable> is also possible, but difficult to
565 check at compile time. */
566 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
567 && (((ts->u.cl->length && !fts->u.cl->length)
568 ||(!ts->u.cl->length && fts->u.cl->length))
569 || (ts->u.cl->length
570 && ts->u.cl->length->expr_type
571 != fts->u.cl->length->expr_type)
572 || (ts->u.cl->length
573 && ts->u.cl->length->expr_type == EXPR_CONSTANT
574 && mpz_cmp (ts->u.cl->length->value.integer,
575 fts->u.cl->length->value.integer) != 0)))
576 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
577 "entries returning variables of different "
578 "string lengths", ns->entries->sym->name,
579 &ns->entries->sym->declared_at);
582 if (el == NULL)
584 sym = ns->entries->sym->result;
585 /* All result types the same. */
586 proc->ts = *fts;
587 if (sym->attr.dimension)
588 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
589 if (sym->attr.pointer)
590 gfc_add_pointer (&proc->attr, NULL);
592 else
594 /* Otherwise the result will be passed through a union by
595 reference. */
596 proc->attr.mixed_entry_master = 1;
597 for (el = ns->entries; el; el = el->next)
599 sym = el->sym->result;
600 if (sym->attr.dimension)
602 if (el == ns->entries)
603 gfc_error ("FUNCTION result %s can't be an array in "
604 "FUNCTION %s at %L", sym->name,
605 ns->entries->sym->name, &sym->declared_at);
606 else
607 gfc_error ("ENTRY result %s can't be an array in "
608 "FUNCTION %s at %L", sym->name,
609 ns->entries->sym->name, &sym->declared_at);
611 else if (sym->attr.pointer)
613 if (el == ns->entries)
614 gfc_error ("FUNCTION result %s can't be a POINTER in "
615 "FUNCTION %s at %L", sym->name,
616 ns->entries->sym->name, &sym->declared_at);
617 else
618 gfc_error ("ENTRY result %s can't be a POINTER in "
619 "FUNCTION %s at %L", sym->name,
620 ns->entries->sym->name, &sym->declared_at);
622 else
624 ts = &sym->ts;
625 if (ts->type == BT_UNKNOWN)
626 ts = gfc_get_default_type (sym->name, NULL);
627 switch (ts->type)
629 case BT_INTEGER:
630 if (ts->kind == gfc_default_integer_kind)
631 sym = NULL;
632 break;
633 case BT_REAL:
634 if (ts->kind == gfc_default_real_kind
635 || ts->kind == gfc_default_double_kind)
636 sym = NULL;
637 break;
638 case BT_COMPLEX:
639 if (ts->kind == gfc_default_complex_kind)
640 sym = NULL;
641 break;
642 case BT_LOGICAL:
643 if (ts->kind == gfc_default_logical_kind)
644 sym = NULL;
645 break;
646 case BT_UNKNOWN:
647 /* We will issue error elsewhere. */
648 sym = NULL;
649 break;
650 default:
651 break;
653 if (sym)
655 if (el == ns->entries)
656 gfc_error ("FUNCTION result %s can't be of type %s "
657 "in FUNCTION %s at %L", sym->name,
658 gfc_typename (ts), ns->entries->sym->name,
659 &sym->declared_at);
660 else
661 gfc_error ("ENTRY result %s can't be of type %s "
662 "in FUNCTION %s at %L", sym->name,
663 gfc_typename (ts), ns->entries->sym->name,
664 &sym->declared_at);
670 proc->attr.access = ACCESS_PRIVATE;
671 proc->attr.entry_master = 1;
673 /* Merge all the entry point arguments. */
674 for (el = ns->entries; el; el = el->next)
675 merge_argument_lists (proc, el->sym->formal);
677 /* Check the master formal arguments for any that are not
678 present in all entry points. */
679 for (el = ns->entries; el; el = el->next)
680 check_argument_lists (proc, el->sym->formal);
682 /* Use the master function for the function body. */
683 ns->proc_name = proc;
685 /* Finalize the new symbols. */
686 gfc_commit_symbols ();
688 /* Restore the original namespace. */
689 gfc_current_ns = old_ns;
693 static bool
694 has_default_initializer (gfc_symbol *der)
696 gfc_component *c;
698 gcc_assert (der->attr.flavor == FL_DERIVED);
699 for (c = der->components; c; c = c->next)
700 if ((c->ts.type != BT_DERIVED && c->initializer)
701 || (c->ts.type == BT_DERIVED
702 && (!c->attr.pointer && has_default_initializer (c->ts.u.derived))))
703 break;
705 return c != NULL;
708 /* Resolve common variables. */
709 static void
710 resolve_common_vars (gfc_symbol *sym, bool named_common)
712 gfc_symbol *csym = sym;
714 for (; csym; csym = csym->common_next)
716 if (csym->value || csym->attr.data)
718 if (!csym->ns->is_block_data)
719 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
720 "but only in BLOCK DATA initialization is "
721 "allowed", csym->name, &csym->declared_at);
722 else if (!named_common)
723 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
724 "in a blank COMMON but initialization is only "
725 "allowed in named common blocks", csym->name,
726 &csym->declared_at);
729 if (csym->ts.type != BT_DERIVED)
730 continue;
732 if (!(csym->ts.u.derived->attr.sequence
733 || csym->ts.u.derived->attr.is_bind_c))
734 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
735 "has neither the SEQUENCE nor the BIND(C) "
736 "attribute", csym->name, &csym->declared_at);
737 if (csym->ts.u.derived->attr.alloc_comp)
738 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
739 "has an ultimate component that is "
740 "allocatable", csym->name, &csym->declared_at);
741 if (has_default_initializer (csym->ts.u.derived))
742 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
743 "may not have default initializer", csym->name,
744 &csym->declared_at);
746 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
747 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
751 /* Resolve common blocks. */
752 static void
753 resolve_common_blocks (gfc_symtree *common_root)
755 gfc_symbol *sym;
757 if (common_root == NULL)
758 return;
760 if (common_root->left)
761 resolve_common_blocks (common_root->left);
762 if (common_root->right)
763 resolve_common_blocks (common_root->right);
765 resolve_common_vars (common_root->n.common->head, true);
767 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
768 if (sym == NULL)
769 return;
771 if (sym->attr.flavor == FL_PARAMETER)
772 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
773 sym->name, &common_root->n.common->where, &sym->declared_at);
775 if (sym->attr.intrinsic)
776 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
777 sym->name, &common_root->n.common->where);
778 else if (sym->attr.result
779 || gfc_is_function_return_value (sym, gfc_current_ns))
780 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
781 "that is also a function result", sym->name,
782 &common_root->n.common->where);
783 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
784 && sym->attr.proc != PROC_ST_FUNCTION)
785 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
786 "that is also a global procedure", sym->name,
787 &common_root->n.common->where);
791 /* Resolve contained function types. Because contained functions can call one
792 another, they have to be worked out before any of the contained procedures
793 can be resolved.
795 The good news is that if a function doesn't already have a type, the only
796 way it can get one is through an IMPLICIT type or a RESULT variable, because
797 by definition contained functions are contained namespace they're contained
798 in, not in a sibling or parent namespace. */
800 static void
801 resolve_contained_functions (gfc_namespace *ns)
803 gfc_namespace *child;
804 gfc_entry_list *el;
806 resolve_formal_arglists (ns);
808 for (child = ns->contained; child; child = child->sibling)
810 /* Resolve alternate entry points first. */
811 resolve_entries (child);
813 /* Then check function return types. */
814 resolve_contained_fntype (child->proc_name, child);
815 for (el = child->entries; el; el = el->next)
816 resolve_contained_fntype (el->sym, child);
821 /* Resolve all of the elements of a structure constructor and make sure that
822 the types are correct. */
824 static gfc_try
825 resolve_structure_cons (gfc_expr *expr)
827 gfc_constructor *cons;
828 gfc_component *comp;
829 gfc_try t;
830 symbol_attribute a;
832 t = SUCCESS;
833 cons = expr->value.constructor;
834 /* A constructor may have references if it is the result of substituting a
835 parameter variable. In this case we just pull out the component we
836 want. */
837 if (expr->ref)
838 comp = expr->ref->u.c.sym->components;
839 else
840 comp = expr->ts.u.derived->components;
842 /* See if the user is trying to invoke a structure constructor for one of
843 the iso_c_binding derived types. */
844 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
845 && expr->ts.u.derived->ts.is_iso_c && cons
846 && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
848 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
849 expr->ts.u.derived->name, &(expr->where));
850 return FAILURE;
853 /* Return if structure constructor is c_null_(fun)prt. */
854 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
855 && expr->ts.u.derived->ts.is_iso_c && cons
856 && cons->expr && cons->expr->expr_type == EXPR_NULL)
857 return SUCCESS;
859 for (; comp; comp = comp->next, cons = cons->next)
861 int rank;
863 if (!cons->expr)
864 continue;
866 if (gfc_resolve_expr (cons->expr) == FAILURE)
868 t = FAILURE;
869 continue;
872 rank = comp->as ? comp->as->rank : 0;
873 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
874 && (comp->attr.allocatable || cons->expr->rank))
876 gfc_error ("The rank of the element in the derived type "
877 "constructor at %L does not match that of the "
878 "component (%d/%d)", &cons->expr->where,
879 cons->expr->rank, rank);
880 t = FAILURE;
883 /* If we don't have the right type, try to convert it. */
885 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
887 t = FAILURE;
888 if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
889 gfc_error ("The element in the derived type constructor at %L, "
890 "for pointer component '%s', is %s but should be %s",
891 &cons->expr->where, comp->name,
892 gfc_basic_typename (cons->expr->ts.type),
893 gfc_basic_typename (comp->ts.type));
894 else
895 t = gfc_convert_type (cons->expr, &comp->ts, 1);
898 if (cons->expr->expr_type == EXPR_NULL
899 && !(comp->attr.pointer || comp->attr.allocatable
900 || comp->attr.proc_pointer
901 || (comp->ts.type == BT_CLASS
902 && (comp->ts.u.derived->components->attr.pointer
903 || comp->ts.u.derived->components->attr.allocatable))))
905 t = FAILURE;
906 gfc_error ("The NULL in the derived type constructor at %L is "
907 "being applied to component '%s', which is neither "
908 "a POINTER nor ALLOCATABLE", &cons->expr->where,
909 comp->name);
912 if (!comp->attr.pointer || cons->expr->expr_type == EXPR_NULL)
913 continue;
915 a = gfc_expr_attr (cons->expr);
917 if (!a.pointer && !a.target)
919 t = FAILURE;
920 gfc_error ("The element in the derived type constructor at %L, "
921 "for pointer component '%s' should be a POINTER or "
922 "a TARGET", &cons->expr->where, comp->name);
926 return t;
930 /****************** Expression name resolution ******************/
932 /* Returns 0 if a symbol was not declared with a type or
933 attribute declaration statement, nonzero otherwise. */
935 static int
936 was_declared (gfc_symbol *sym)
938 symbol_attribute a;
940 a = sym->attr;
942 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
943 return 1;
945 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
946 || a.optional || a.pointer || a.save || a.target || a.volatile_
947 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
948 || a.asynchronous)
949 return 1;
951 return 0;
955 /* Determine if a symbol is generic or not. */
957 static int
958 generic_sym (gfc_symbol *sym)
960 gfc_symbol *s;
962 if (sym->attr.generic ||
963 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
964 return 1;
966 if (was_declared (sym) || sym->ns->parent == NULL)
967 return 0;
969 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
971 if (s != NULL)
973 if (s == sym)
974 return 0;
975 else
976 return generic_sym (s);
979 return 0;
983 /* Determine if a symbol is specific or not. */
985 static int
986 specific_sym (gfc_symbol *sym)
988 gfc_symbol *s;
990 if (sym->attr.if_source == IFSRC_IFBODY
991 || sym->attr.proc == PROC_MODULE
992 || sym->attr.proc == PROC_INTERNAL
993 || sym->attr.proc == PROC_ST_FUNCTION
994 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
995 || sym->attr.external)
996 return 1;
998 if (was_declared (sym) || sym->ns->parent == NULL)
999 return 0;
1001 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1003 return (s == NULL) ? 0 : specific_sym (s);
1007 /* Figure out if the procedure is specific, generic or unknown. */
1009 typedef enum
1010 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1011 proc_type;
1013 static proc_type
1014 procedure_kind (gfc_symbol *sym)
1016 if (generic_sym (sym))
1017 return PTYPE_GENERIC;
1019 if (specific_sym (sym))
1020 return PTYPE_SPECIFIC;
1022 return PTYPE_UNKNOWN;
1025 /* Check references to assumed size arrays. The flag need_full_assumed_size
1026 is nonzero when matching actual arguments. */
1028 static int need_full_assumed_size = 0;
1030 static bool
1031 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1033 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1034 return false;
1036 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1037 What should it be? */
1038 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1039 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1040 && (e->ref->u.ar.type == AR_FULL))
1042 gfc_error ("The upper bound in the last dimension must "
1043 "appear in the reference to the assumed size "
1044 "array '%s' at %L", sym->name, &e->where);
1045 return true;
1047 return false;
1051 /* Look for bad assumed size array references in argument expressions
1052 of elemental and array valued intrinsic procedures. Since this is
1053 called from procedure resolution functions, it only recurses at
1054 operators. */
1056 static bool
1057 resolve_assumed_size_actual (gfc_expr *e)
1059 if (e == NULL)
1060 return false;
1062 switch (e->expr_type)
1064 case EXPR_VARIABLE:
1065 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1066 return true;
1067 break;
1069 case EXPR_OP:
1070 if (resolve_assumed_size_actual (e->value.op.op1)
1071 || resolve_assumed_size_actual (e->value.op.op2))
1072 return true;
1073 break;
1075 default:
1076 break;
1078 return false;
1082 /* Check a generic procedure, passed as an actual argument, to see if
1083 there is a matching specific name. If none, it is an error, and if
1084 more than one, the reference is ambiguous. */
1085 static int
1086 count_specific_procs (gfc_expr *e)
1088 int n;
1089 gfc_interface *p;
1090 gfc_symbol *sym;
1092 n = 0;
1093 sym = e->symtree->n.sym;
1095 for (p = sym->generic; p; p = p->next)
1096 if (strcmp (sym->name, p->sym->name) == 0)
1098 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1099 sym->name);
1100 n++;
1103 if (n > 1)
1104 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1105 &e->where);
1107 if (n == 0)
1108 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1109 "argument at %L", sym->name, &e->where);
1111 return n;
1115 /* See if a call to sym could possibly be a not allowed RECURSION because of
1116 a missing RECURIVE declaration. This means that either sym is the current
1117 context itself, or sym is the parent of a contained procedure calling its
1118 non-RECURSIVE containing procedure.
1119 This also works if sym is an ENTRY. */
1121 static bool
1122 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1124 gfc_symbol* proc_sym;
1125 gfc_symbol* context_proc;
1126 gfc_namespace* real_context;
1128 if (sym->attr.flavor == FL_PROGRAM)
1129 return false;
1131 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1133 /* If we've got an ENTRY, find real procedure. */
1134 if (sym->attr.entry && sym->ns->entries)
1135 proc_sym = sym->ns->entries->sym;
1136 else
1137 proc_sym = sym;
1139 /* If sym is RECURSIVE, all is well of course. */
1140 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1141 return false;
1143 /* Find the context procedure's "real" symbol if it has entries.
1144 We look for a procedure symbol, so recurse on the parents if we don't
1145 find one (like in case of a BLOCK construct). */
1146 for (real_context = context; ; real_context = real_context->parent)
1148 /* We should find something, eventually! */
1149 gcc_assert (real_context);
1151 context_proc = (real_context->entries ? real_context->entries->sym
1152 : real_context->proc_name);
1154 /* In some special cases, there may not be a proc_name, like for this
1155 invalid code:
1156 real(bad_kind()) function foo () ...
1157 when checking the call to bad_kind ().
1158 In these cases, we simply return here and assume that the
1159 call is ok. */
1160 if (!context_proc)
1161 return false;
1163 if (context_proc->attr.flavor != FL_LABEL)
1164 break;
1167 /* A call from sym's body to itself is recursion, of course. */
1168 if (context_proc == proc_sym)
1169 return true;
1171 /* The same is true if context is a contained procedure and sym the
1172 containing one. */
1173 if (context_proc->attr.contained)
1175 gfc_symbol* parent_proc;
1177 gcc_assert (context->parent);
1178 parent_proc = (context->parent->entries ? context->parent->entries->sym
1179 : context->parent->proc_name);
1181 if (parent_proc == proc_sym)
1182 return true;
1185 return false;
1189 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1190 its typespec and formal argument list. */
1192 static gfc_try
1193 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1195 gfc_intrinsic_sym* isym;
1196 const char* symstd;
1198 if (sym->formal)
1199 return SUCCESS;
1201 /* We already know this one is an intrinsic, so we don't call
1202 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1203 gfc_find_subroutine directly to check whether it is a function or
1204 subroutine. */
1206 if ((isym = gfc_find_function (sym->name)))
1208 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1209 && !sym->attr.implicit_type)
1210 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1211 " ignored", sym->name, &sym->declared_at);
1213 if (!sym->attr.function &&
1214 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1215 return FAILURE;
1217 sym->ts = isym->ts;
1219 else if ((isym = gfc_find_subroutine (sym->name)))
1221 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1223 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1224 " specifier", sym->name, &sym->declared_at);
1225 return FAILURE;
1228 if (!sym->attr.subroutine &&
1229 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1230 return FAILURE;
1232 else
1234 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1235 &sym->declared_at);
1236 return FAILURE;
1239 gfc_copy_formal_args_intr (sym, isym);
1241 /* Check it is actually available in the standard settings. */
1242 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1243 == FAILURE)
1245 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1246 " available in the current standard settings but %s. Use"
1247 " an appropriate -std=* option or enable -fall-intrinsics"
1248 " in order to use it.",
1249 sym->name, &sym->declared_at, symstd);
1250 return FAILURE;
1253 return SUCCESS;
1257 /* Resolve a procedure expression, like passing it to a called procedure or as
1258 RHS for a procedure pointer assignment. */
1260 static gfc_try
1261 resolve_procedure_expression (gfc_expr* expr)
1263 gfc_symbol* sym;
1265 if (expr->expr_type != EXPR_VARIABLE)
1266 return SUCCESS;
1267 gcc_assert (expr->symtree);
1269 sym = expr->symtree->n.sym;
1271 if (sym->attr.intrinsic)
1272 resolve_intrinsic (sym, &expr->where);
1274 if (sym->attr.flavor != FL_PROCEDURE
1275 || (sym->attr.function && sym->result == sym))
1276 return SUCCESS;
1278 /* A non-RECURSIVE procedure that is used as procedure expression within its
1279 own body is in danger of being called recursively. */
1280 if (is_illegal_recursion (sym, gfc_current_ns))
1281 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1282 " itself recursively. Declare it RECURSIVE or use"
1283 " -frecursive", sym->name, &expr->where);
1285 return SUCCESS;
1289 /* Resolve an actual argument list. Most of the time, this is just
1290 resolving the expressions in the list.
1291 The exception is that we sometimes have to decide whether arguments
1292 that look like procedure arguments are really simple variable
1293 references. */
1295 static gfc_try
1296 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1297 bool no_formal_args)
1299 gfc_symbol *sym;
1300 gfc_symtree *parent_st;
1301 gfc_expr *e;
1302 int save_need_full_assumed_size;
1303 gfc_component *comp;
1305 for (; arg; arg = arg->next)
1307 e = arg->expr;
1308 if (e == NULL)
1310 /* Check the label is a valid branching target. */
1311 if (arg->label)
1313 if (arg->label->defined == ST_LABEL_UNKNOWN)
1315 gfc_error ("Label %d referenced at %L is never defined",
1316 arg->label->value, &arg->label->where);
1317 return FAILURE;
1320 continue;
1323 if (gfc_is_proc_ptr_comp (e, &comp))
1325 e->ts = comp->ts;
1326 if (e->expr_type == EXPR_PPC)
1328 if (comp->as != NULL)
1329 e->rank = comp->as->rank;
1330 e->expr_type = EXPR_FUNCTION;
1332 if (gfc_resolve_expr (e) == FAILURE)
1333 return FAILURE;
1334 goto argument_list;
1337 if (e->expr_type == EXPR_VARIABLE
1338 && e->symtree->n.sym->attr.generic
1339 && no_formal_args
1340 && count_specific_procs (e) != 1)
1341 return FAILURE;
1343 if (e->ts.type != BT_PROCEDURE)
1345 save_need_full_assumed_size = need_full_assumed_size;
1346 if (e->expr_type != EXPR_VARIABLE)
1347 need_full_assumed_size = 0;
1348 if (gfc_resolve_expr (e) != SUCCESS)
1349 return FAILURE;
1350 need_full_assumed_size = save_need_full_assumed_size;
1351 goto argument_list;
1354 /* See if the expression node should really be a variable reference. */
1356 sym = e->symtree->n.sym;
1358 if (sym->attr.flavor == FL_PROCEDURE
1359 || sym->attr.intrinsic
1360 || sym->attr.external)
1362 int actual_ok;
1364 /* If a procedure is not already determined to be something else
1365 check if it is intrinsic. */
1366 if (!sym->attr.intrinsic
1367 && !(sym->attr.external || sym->attr.use_assoc
1368 || sym->attr.if_source == IFSRC_IFBODY)
1369 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1370 sym->attr.intrinsic = 1;
1372 if (sym->attr.proc == PROC_ST_FUNCTION)
1374 gfc_error ("Statement function '%s' at %L is not allowed as an "
1375 "actual argument", sym->name, &e->where);
1378 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1379 sym->attr.subroutine);
1380 if (sym->attr.intrinsic && actual_ok == 0)
1382 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1383 "actual argument", sym->name, &e->where);
1386 if (sym->attr.contained && !sym->attr.use_assoc
1387 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1389 gfc_error ("Internal procedure '%s' is not allowed as an "
1390 "actual argument at %L", sym->name, &e->where);
1393 if (sym->attr.elemental && !sym->attr.intrinsic)
1395 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1396 "allowed as an actual argument at %L", sym->name,
1397 &e->where);
1400 /* Check if a generic interface has a specific procedure
1401 with the same name before emitting an error. */
1402 if (sym->attr.generic && count_specific_procs (e) != 1)
1403 return FAILURE;
1405 /* Just in case a specific was found for the expression. */
1406 sym = e->symtree->n.sym;
1408 /* If the symbol is the function that names the current (or
1409 parent) scope, then we really have a variable reference. */
1411 if (gfc_is_function_return_value (sym, sym->ns))
1412 goto got_variable;
1414 /* If all else fails, see if we have a specific intrinsic. */
1415 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1417 gfc_intrinsic_sym *isym;
1419 isym = gfc_find_function (sym->name);
1420 if (isym == NULL || !isym->specific)
1422 gfc_error ("Unable to find a specific INTRINSIC procedure "
1423 "for the reference '%s' at %L", sym->name,
1424 &e->where);
1425 return FAILURE;
1427 sym->ts = isym->ts;
1428 sym->attr.intrinsic = 1;
1429 sym->attr.function = 1;
1432 if (gfc_resolve_expr (e) == FAILURE)
1433 return FAILURE;
1434 goto argument_list;
1437 /* See if the name is a module procedure in a parent unit. */
1439 if (was_declared (sym) || sym->ns->parent == NULL)
1440 goto got_variable;
1442 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1444 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1445 return FAILURE;
1448 if (parent_st == NULL)
1449 goto got_variable;
1451 sym = parent_st->n.sym;
1452 e->symtree = parent_st; /* Point to the right thing. */
1454 if (sym->attr.flavor == FL_PROCEDURE
1455 || sym->attr.intrinsic
1456 || sym->attr.external)
1458 if (gfc_resolve_expr (e) == FAILURE)
1459 return FAILURE;
1460 goto argument_list;
1463 got_variable:
1464 e->expr_type = EXPR_VARIABLE;
1465 e->ts = sym->ts;
1466 if (sym->as != NULL)
1468 e->rank = sym->as->rank;
1469 e->ref = gfc_get_ref ();
1470 e->ref->type = REF_ARRAY;
1471 e->ref->u.ar.type = AR_FULL;
1472 e->ref->u.ar.as = sym->as;
1475 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1476 primary.c (match_actual_arg). If above code determines that it
1477 is a variable instead, it needs to be resolved as it was not
1478 done at the beginning of this function. */
1479 save_need_full_assumed_size = need_full_assumed_size;
1480 if (e->expr_type != EXPR_VARIABLE)
1481 need_full_assumed_size = 0;
1482 if (gfc_resolve_expr (e) != SUCCESS)
1483 return FAILURE;
1484 need_full_assumed_size = save_need_full_assumed_size;
1486 argument_list:
1487 /* Check argument list functions %VAL, %LOC and %REF. There is
1488 nothing to do for %REF. */
1489 if (arg->name && arg->name[0] == '%')
1491 if (strncmp ("%VAL", arg->name, 4) == 0)
1493 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1495 gfc_error ("By-value argument at %L is not of numeric "
1496 "type", &e->where);
1497 return FAILURE;
1500 if (e->rank)
1502 gfc_error ("By-value argument at %L cannot be an array or "
1503 "an array section", &e->where);
1504 return FAILURE;
1507 /* Intrinsics are still PROC_UNKNOWN here. However,
1508 since same file external procedures are not resolvable
1509 in gfortran, it is a good deal easier to leave them to
1510 intrinsic.c. */
1511 if (ptype != PROC_UNKNOWN
1512 && ptype != PROC_DUMMY
1513 && ptype != PROC_EXTERNAL
1514 && ptype != PROC_MODULE)
1516 gfc_error ("By-value argument at %L is not allowed "
1517 "in this context", &e->where);
1518 return FAILURE;
1522 /* Statement functions have already been excluded above. */
1523 else if (strncmp ("%LOC", arg->name, 4) == 0
1524 && e->ts.type == BT_PROCEDURE)
1526 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1528 gfc_error ("Passing internal procedure at %L by location "
1529 "not allowed", &e->where);
1530 return FAILURE;
1536 return SUCCESS;
1540 /* Do the checks of the actual argument list that are specific to elemental
1541 procedures. If called with c == NULL, we have a function, otherwise if
1542 expr == NULL, we have a subroutine. */
1544 static gfc_try
1545 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1547 gfc_actual_arglist *arg0;
1548 gfc_actual_arglist *arg;
1549 gfc_symbol *esym = NULL;
1550 gfc_intrinsic_sym *isym = NULL;
1551 gfc_expr *e = NULL;
1552 gfc_intrinsic_arg *iformal = NULL;
1553 gfc_formal_arglist *eformal = NULL;
1554 bool formal_optional = false;
1555 bool set_by_optional = false;
1556 int i;
1557 int rank = 0;
1559 /* Is this an elemental procedure? */
1560 if (expr && expr->value.function.actual != NULL)
1562 if (expr->value.function.esym != NULL
1563 && expr->value.function.esym->attr.elemental)
1565 arg0 = expr->value.function.actual;
1566 esym = expr->value.function.esym;
1568 else if (expr->value.function.isym != NULL
1569 && expr->value.function.isym->elemental)
1571 arg0 = expr->value.function.actual;
1572 isym = expr->value.function.isym;
1574 else
1575 return SUCCESS;
1577 else if (c && c->ext.actual != NULL)
1579 arg0 = c->ext.actual;
1581 if (c->resolved_sym)
1582 esym = c->resolved_sym;
1583 else
1584 esym = c->symtree->n.sym;
1585 gcc_assert (esym);
1587 if (!esym->attr.elemental)
1588 return SUCCESS;
1590 else
1591 return SUCCESS;
1593 /* The rank of an elemental is the rank of its array argument(s). */
1594 for (arg = arg0; arg; arg = arg->next)
1596 if (arg->expr != NULL && arg->expr->rank > 0)
1598 rank = arg->expr->rank;
1599 if (arg->expr->expr_type == EXPR_VARIABLE
1600 && arg->expr->symtree->n.sym->attr.optional)
1601 set_by_optional = true;
1603 /* Function specific; set the result rank and shape. */
1604 if (expr)
1606 expr->rank = rank;
1607 if (!expr->shape && arg->expr->shape)
1609 expr->shape = gfc_get_shape (rank);
1610 for (i = 0; i < rank; i++)
1611 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1614 break;
1618 /* If it is an array, it shall not be supplied as an actual argument
1619 to an elemental procedure unless an array of the same rank is supplied
1620 as an actual argument corresponding to a nonoptional dummy argument of
1621 that elemental procedure(12.4.1.5). */
1622 formal_optional = false;
1623 if (isym)
1624 iformal = isym->formal;
1625 else
1626 eformal = esym->formal;
1628 for (arg = arg0; arg; arg = arg->next)
1630 if (eformal)
1632 if (eformal->sym && eformal->sym->attr.optional)
1633 formal_optional = true;
1634 eformal = eformal->next;
1636 else if (isym && iformal)
1638 if (iformal->optional)
1639 formal_optional = true;
1640 iformal = iformal->next;
1642 else if (isym)
1643 formal_optional = true;
1645 if (pedantic && arg->expr != NULL
1646 && arg->expr->expr_type == EXPR_VARIABLE
1647 && arg->expr->symtree->n.sym->attr.optional
1648 && formal_optional
1649 && arg->expr->rank
1650 && (set_by_optional || arg->expr->rank != rank)
1651 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1653 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1654 "MISSING, it cannot be the actual argument of an "
1655 "ELEMENTAL procedure unless there is a non-optional "
1656 "argument with the same rank (12.4.1.5)",
1657 arg->expr->symtree->n.sym->name, &arg->expr->where);
1658 return FAILURE;
1662 for (arg = arg0; arg; arg = arg->next)
1664 if (arg->expr == NULL || arg->expr->rank == 0)
1665 continue;
1667 /* Being elemental, the last upper bound of an assumed size array
1668 argument must be present. */
1669 if (resolve_assumed_size_actual (arg->expr))
1670 return FAILURE;
1672 /* Elemental procedure's array actual arguments must conform. */
1673 if (e != NULL)
1675 if (gfc_check_conformance (arg->expr, e,
1676 "elemental procedure") == FAILURE)
1677 return FAILURE;
1679 else
1680 e = arg->expr;
1683 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1684 is an array, the intent inout/out variable needs to be also an array. */
1685 if (rank > 0 && esym && expr == NULL)
1686 for (eformal = esym->formal, arg = arg0; arg && eformal;
1687 arg = arg->next, eformal = eformal->next)
1688 if ((eformal->sym->attr.intent == INTENT_OUT
1689 || eformal->sym->attr.intent == INTENT_INOUT)
1690 && arg->expr && arg->expr->rank == 0)
1692 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1693 "ELEMENTAL subroutine '%s' is a scalar, but another "
1694 "actual argument is an array", &arg->expr->where,
1695 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1696 : "INOUT", eformal->sym->name, esym->name);
1697 return FAILURE;
1699 return SUCCESS;
1703 /* Go through each actual argument in ACTUAL and see if it can be
1704 implemented as an inlined, non-copying intrinsic. FNSYM is the
1705 function being called, or NULL if not known. */
1707 static void
1708 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1710 gfc_actual_arglist *ap;
1711 gfc_expr *expr;
1713 for (ap = actual; ap; ap = ap->next)
1714 if (ap->expr
1715 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1716 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1717 NOT_ELEMENTAL))
1718 ap->expr->inline_noncopying_intrinsic = 1;
1722 /* This function does the checking of references to global procedures
1723 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1724 77 and 95 standards. It checks for a gsymbol for the name, making
1725 one if it does not already exist. If it already exists, then the
1726 reference being resolved must correspond to the type of gsymbol.
1727 Otherwise, the new symbol is equipped with the attributes of the
1728 reference. The corresponding code that is called in creating
1729 global entities is parse.c.
1731 In addition, for all but -std=legacy, the gsymbols are used to
1732 check the interfaces of external procedures from the same file.
1733 The namespace of the gsymbol is resolved and then, once this is
1734 done the interface is checked. */
1737 static bool
1738 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1740 if (!gsym_ns->proc_name->attr.recursive)
1741 return true;
1743 if (sym->ns == gsym_ns)
1744 return false;
1746 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1747 return false;
1749 return true;
1752 static bool
1753 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1755 if (gsym_ns->entries)
1757 gfc_entry_list *entry = gsym_ns->entries;
1759 for (; entry; entry = entry->next)
1761 if (strcmp (sym->name, entry->sym->name) == 0)
1763 if (strcmp (gsym_ns->proc_name->name,
1764 sym->ns->proc_name->name) == 0)
1765 return false;
1767 if (sym->ns->parent
1768 && strcmp (gsym_ns->proc_name->name,
1769 sym->ns->parent->proc_name->name) == 0)
1770 return false;
1774 return true;
1777 static void
1778 resolve_global_procedure (gfc_symbol *sym, locus *where,
1779 gfc_actual_arglist **actual, int sub)
1781 gfc_gsymbol * gsym;
1782 gfc_namespace *ns;
1783 enum gfc_symbol_type type;
1785 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1787 gsym = gfc_get_gsymbol (sym->name);
1789 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1790 gfc_global_used (gsym, where);
1792 if (gfc_option.flag_whole_file
1793 && sym->attr.if_source == IFSRC_UNKNOWN
1794 && gsym->type != GSYM_UNKNOWN
1795 && gsym->ns
1796 && gsym->ns->resolved != -1
1797 && gsym->ns->proc_name
1798 && not_in_recursive (sym, gsym->ns)
1799 && not_entry_self_reference (sym, gsym->ns))
1801 /* Make sure that translation for the gsymbol occurs before
1802 the procedure currently being resolved. */
1803 ns = gsym->ns->resolved ? NULL : gfc_global_ns_list;
1804 for (; ns && ns != gsym->ns; ns = ns->sibling)
1806 if (ns->sibling == gsym->ns)
1808 ns->sibling = gsym->ns->sibling;
1809 gsym->ns->sibling = gfc_global_ns_list;
1810 gfc_global_ns_list = gsym->ns;
1811 break;
1815 if (!gsym->ns->resolved)
1817 gfc_dt_list *old_dt_list;
1819 /* Stash away derived types so that the backend_decls do not
1820 get mixed up. */
1821 old_dt_list = gfc_derived_types;
1822 gfc_derived_types = NULL;
1824 gfc_resolve (gsym->ns);
1826 /* Store the new derived types with the global namespace. */
1827 if (gfc_derived_types)
1828 gsym->ns->derived_types = gfc_derived_types;
1830 /* Restore the derived types of this namespace. */
1831 gfc_derived_types = old_dt_list;
1834 if (gsym->ns->proc_name->attr.function
1835 && gsym->ns->proc_name->as
1836 && gsym->ns->proc_name->as->rank
1837 && (!sym->as || sym->as->rank != gsym->ns->proc_name->as->rank))
1838 gfc_error ("The reference to function '%s' at %L either needs an "
1839 "explicit INTERFACE or the rank is incorrect", sym->name,
1840 where);
1842 /* Non-assumed length character functions. */
1843 if (sym->attr.function && sym->ts.type == BT_CHARACTER
1844 && gsym->ns->proc_name->ts.u.cl->length != NULL)
1846 gfc_charlen *cl = sym->ts.u.cl;
1848 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
1849 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
1851 gfc_error ("Nonconstant character-length function '%s' at %L "
1852 "must have an explicit interface", sym->name,
1853 &sym->declared_at);
1857 if (gfc_option.flag_whole_file == 1
1858 || ((gfc_option.warn_std & GFC_STD_LEGACY)
1860 !(gfc_option.warn_std & GFC_STD_GNU)))
1861 gfc_errors_to_warnings (1);
1863 gfc_procedure_use (gsym->ns->proc_name, actual, where);
1865 gfc_errors_to_warnings (0);
1868 if (gsym->type == GSYM_UNKNOWN)
1870 gsym->type = type;
1871 gsym->where = *where;
1874 gsym->used = 1;
1878 /************* Function resolution *************/
1880 /* Resolve a function call known to be generic.
1881 Section 14.1.2.4.1. */
1883 static match
1884 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1886 gfc_symbol *s;
1888 if (sym->attr.generic)
1890 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1891 if (s != NULL)
1893 expr->value.function.name = s->name;
1894 expr->value.function.esym = s;
1896 if (s->ts.type != BT_UNKNOWN)
1897 expr->ts = s->ts;
1898 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1899 expr->ts = s->result->ts;
1901 if (s->as != NULL)
1902 expr->rank = s->as->rank;
1903 else if (s->result != NULL && s->result->as != NULL)
1904 expr->rank = s->result->as->rank;
1906 gfc_set_sym_referenced (expr->value.function.esym);
1908 return MATCH_YES;
1911 /* TODO: Need to search for elemental references in generic
1912 interface. */
1915 if (sym->attr.intrinsic)
1916 return gfc_intrinsic_func_interface (expr, 0);
1918 return MATCH_NO;
1922 static gfc_try
1923 resolve_generic_f (gfc_expr *expr)
1925 gfc_symbol *sym;
1926 match m;
1928 sym = expr->symtree->n.sym;
1930 for (;;)
1932 m = resolve_generic_f0 (expr, sym);
1933 if (m == MATCH_YES)
1934 return SUCCESS;
1935 else if (m == MATCH_ERROR)
1936 return FAILURE;
1938 generic:
1939 if (sym->ns->parent == NULL)
1940 break;
1941 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1943 if (sym == NULL)
1944 break;
1945 if (!generic_sym (sym))
1946 goto generic;
1949 /* Last ditch attempt. See if the reference is to an intrinsic
1950 that possesses a matching interface. 14.1.2.4 */
1951 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
1953 gfc_error ("There is no specific function for the generic '%s' at %L",
1954 expr->symtree->n.sym->name, &expr->where);
1955 return FAILURE;
1958 m = gfc_intrinsic_func_interface (expr, 0);
1959 if (m == MATCH_YES)
1960 return SUCCESS;
1961 if (m == MATCH_NO)
1962 gfc_error ("Generic function '%s' at %L is not consistent with a "
1963 "specific intrinsic interface", expr->symtree->n.sym->name,
1964 &expr->where);
1966 return FAILURE;
1970 /* Resolve a function call known to be specific. */
1972 static match
1973 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
1975 match m;
1977 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1979 if (sym->attr.dummy)
1981 sym->attr.proc = PROC_DUMMY;
1982 goto found;
1985 sym->attr.proc = PROC_EXTERNAL;
1986 goto found;
1989 if (sym->attr.proc == PROC_MODULE
1990 || sym->attr.proc == PROC_ST_FUNCTION
1991 || sym->attr.proc == PROC_INTERNAL)
1992 goto found;
1994 if (sym->attr.intrinsic)
1996 m = gfc_intrinsic_func_interface (expr, 1);
1997 if (m == MATCH_YES)
1998 return MATCH_YES;
1999 if (m == MATCH_NO)
2000 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2001 "with an intrinsic", sym->name, &expr->where);
2003 return MATCH_ERROR;
2006 return MATCH_NO;
2008 found:
2009 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2011 if (sym->result)
2012 expr->ts = sym->result->ts;
2013 else
2014 expr->ts = sym->ts;
2015 expr->value.function.name = sym->name;
2016 expr->value.function.esym = sym;
2017 if (sym->as != NULL)
2018 expr->rank = sym->as->rank;
2020 return MATCH_YES;
2024 static gfc_try
2025 resolve_specific_f (gfc_expr *expr)
2027 gfc_symbol *sym;
2028 match m;
2030 sym = expr->symtree->n.sym;
2032 for (;;)
2034 m = resolve_specific_f0 (sym, expr);
2035 if (m == MATCH_YES)
2036 return SUCCESS;
2037 if (m == MATCH_ERROR)
2038 return FAILURE;
2040 if (sym->ns->parent == NULL)
2041 break;
2043 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2045 if (sym == NULL)
2046 break;
2049 gfc_error ("Unable to resolve the specific function '%s' at %L",
2050 expr->symtree->n.sym->name, &expr->where);
2052 return SUCCESS;
2056 /* Resolve a procedure call not known to be generic nor specific. */
2058 static gfc_try
2059 resolve_unknown_f (gfc_expr *expr)
2061 gfc_symbol *sym;
2062 gfc_typespec *ts;
2064 sym = expr->symtree->n.sym;
2066 if (sym->attr.dummy)
2068 sym->attr.proc = PROC_DUMMY;
2069 expr->value.function.name = sym->name;
2070 goto set_type;
2073 /* See if we have an intrinsic function reference. */
2075 if (gfc_is_intrinsic (sym, 0, expr->where))
2077 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2078 return SUCCESS;
2079 return FAILURE;
2082 /* The reference is to an external name. */
2084 sym->attr.proc = PROC_EXTERNAL;
2085 expr->value.function.name = sym->name;
2086 expr->value.function.esym = expr->symtree->n.sym;
2088 if (sym->as != NULL)
2089 expr->rank = sym->as->rank;
2091 /* Type of the expression is either the type of the symbol or the
2092 default type of the symbol. */
2094 set_type:
2095 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2097 if (sym->ts.type != BT_UNKNOWN)
2098 expr->ts = sym->ts;
2099 else
2101 ts = gfc_get_default_type (sym->name, sym->ns);
2103 if (ts->type == BT_UNKNOWN)
2105 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2106 sym->name, &expr->where);
2107 return FAILURE;
2109 else
2110 expr->ts = *ts;
2113 return SUCCESS;
2117 /* Return true, if the symbol is an external procedure. */
2118 static bool
2119 is_external_proc (gfc_symbol *sym)
2121 if (!sym->attr.dummy && !sym->attr.contained
2122 && !(sym->attr.intrinsic
2123 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2124 && sym->attr.proc != PROC_ST_FUNCTION
2125 && !sym->attr.use_assoc
2126 && sym->name)
2127 return true;
2129 return false;
2133 /* Figure out if a function reference is pure or not. Also set the name
2134 of the function for a potential error message. Return nonzero if the
2135 function is PURE, zero if not. */
2136 static int
2137 pure_stmt_function (gfc_expr *, gfc_symbol *);
2139 static int
2140 pure_function (gfc_expr *e, const char **name)
2142 int pure;
2144 *name = NULL;
2146 if (e->symtree != NULL
2147 && e->symtree->n.sym != NULL
2148 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2149 return pure_stmt_function (e, e->symtree->n.sym);
2151 if (e->value.function.esym)
2153 pure = gfc_pure (e->value.function.esym);
2154 *name = e->value.function.esym->name;
2156 else if (e->value.function.isym)
2158 pure = e->value.function.isym->pure
2159 || e->value.function.isym->elemental;
2160 *name = e->value.function.isym->name;
2162 else
2164 /* Implicit functions are not pure. */
2165 pure = 0;
2166 *name = e->value.function.name;
2169 return pure;
2173 static bool
2174 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2175 int *f ATTRIBUTE_UNUSED)
2177 const char *name;
2179 /* Don't bother recursing into other statement functions
2180 since they will be checked individually for purity. */
2181 if (e->expr_type != EXPR_FUNCTION
2182 || !e->symtree
2183 || e->symtree->n.sym == sym
2184 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2185 return false;
2187 return pure_function (e, &name) ? false : true;
2191 static int
2192 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2194 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2198 static gfc_try
2199 is_scalar_expr_ptr (gfc_expr *expr)
2201 gfc_try retval = SUCCESS;
2202 gfc_ref *ref;
2203 int start;
2204 int end;
2206 /* See if we have a gfc_ref, which means we have a substring, array
2207 reference, or a component. */
2208 if (expr->ref != NULL)
2210 ref = expr->ref;
2211 while (ref->next != NULL)
2212 ref = ref->next;
2214 switch (ref->type)
2216 case REF_SUBSTRING:
2217 if (ref->u.ss.length != NULL
2218 && ref->u.ss.length->length != NULL
2219 && ref->u.ss.start
2220 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2221 && ref->u.ss.end
2222 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2224 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2225 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2226 if (end - start + 1 != 1)
2227 retval = FAILURE;
2229 else
2230 retval = FAILURE;
2231 break;
2232 case REF_ARRAY:
2233 if (ref->u.ar.type == AR_ELEMENT)
2234 retval = SUCCESS;
2235 else if (ref->u.ar.type == AR_FULL)
2237 /* The user can give a full array if the array is of size 1. */
2238 if (ref->u.ar.as != NULL
2239 && ref->u.ar.as->rank == 1
2240 && ref->u.ar.as->type == AS_EXPLICIT
2241 && ref->u.ar.as->lower[0] != NULL
2242 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2243 && ref->u.ar.as->upper[0] != NULL
2244 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2246 /* If we have a character string, we need to check if
2247 its length is one. */
2248 if (expr->ts.type == BT_CHARACTER)
2250 if (expr->ts.u.cl == NULL
2251 || expr->ts.u.cl->length == NULL
2252 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2253 != 0)
2254 retval = FAILURE;
2256 else
2258 /* We have constant lower and upper bounds. If the
2259 difference between is 1, it can be considered a
2260 scalar. */
2261 start = (int) mpz_get_si
2262 (ref->u.ar.as->lower[0]->value.integer);
2263 end = (int) mpz_get_si
2264 (ref->u.ar.as->upper[0]->value.integer);
2265 if (end - start + 1 != 1)
2266 retval = FAILURE;
2269 else
2270 retval = FAILURE;
2272 else
2273 retval = FAILURE;
2274 break;
2275 default:
2276 retval = SUCCESS;
2277 break;
2280 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2282 /* Character string. Make sure it's of length 1. */
2283 if (expr->ts.u.cl == NULL
2284 || expr->ts.u.cl->length == NULL
2285 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2286 retval = FAILURE;
2288 else if (expr->rank != 0)
2289 retval = FAILURE;
2291 return retval;
2295 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2296 and, in the case of c_associated, set the binding label based on
2297 the arguments. */
2299 static gfc_try
2300 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2301 gfc_symbol **new_sym)
2303 char name[GFC_MAX_SYMBOL_LEN + 1];
2304 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2305 int optional_arg = 0, is_pointer = 0;
2306 gfc_try retval = SUCCESS;
2307 gfc_symbol *args_sym;
2308 gfc_typespec *arg_ts;
2310 if (args->expr->expr_type == EXPR_CONSTANT
2311 || args->expr->expr_type == EXPR_OP
2312 || args->expr->expr_type == EXPR_NULL)
2314 gfc_error ("Argument to '%s' at %L is not a variable",
2315 sym->name, &(args->expr->where));
2316 return FAILURE;
2319 args_sym = args->expr->symtree->n.sym;
2321 /* The typespec for the actual arg should be that stored in the expr
2322 and not necessarily that of the expr symbol (args_sym), because
2323 the actual expression could be a part-ref of the expr symbol. */
2324 arg_ts = &(args->expr->ts);
2326 is_pointer = gfc_is_data_pointer (args->expr);
2328 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2330 /* If the user gave two args then they are providing something for
2331 the optional arg (the second cptr). Therefore, set the name and
2332 binding label to the c_associated for two cptrs. Otherwise,
2333 set c_associated to expect one cptr. */
2334 if (args->next)
2336 /* two args. */
2337 sprintf (name, "%s_2", sym->name);
2338 sprintf (binding_label, "%s_2", sym->binding_label);
2339 optional_arg = 1;
2341 else
2343 /* one arg. */
2344 sprintf (name, "%s_1", sym->name);
2345 sprintf (binding_label, "%s_1", sym->binding_label);
2346 optional_arg = 0;
2349 /* Get a new symbol for the version of c_associated that
2350 will get called. */
2351 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2353 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2354 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2356 sprintf (name, "%s", sym->name);
2357 sprintf (binding_label, "%s", sym->binding_label);
2359 /* Error check the call. */
2360 if (args->next != NULL)
2362 gfc_error_now ("More actual than formal arguments in '%s' "
2363 "call at %L", name, &(args->expr->where));
2364 retval = FAILURE;
2366 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2368 /* Make sure we have either the target or pointer attribute. */
2369 if (!args_sym->attr.target && !is_pointer)
2371 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2372 "a TARGET or an associated pointer",
2373 args_sym->name,
2374 sym->name, &(args->expr->where));
2375 retval = FAILURE;
2378 /* See if we have interoperable type and type param. */
2379 if (verify_c_interop (arg_ts) == SUCCESS
2380 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2382 if (args_sym->attr.target == 1)
2384 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2385 has the target attribute and is interoperable. */
2386 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2387 allocatable variable that has the TARGET attribute and
2388 is not an array of zero size. */
2389 if (args_sym->attr.allocatable == 1)
2391 if (args_sym->attr.dimension != 0
2392 && (args_sym->as && args_sym->as->rank == 0))
2394 gfc_error_now ("Allocatable variable '%s' used as a "
2395 "parameter to '%s' at %L must not be "
2396 "an array of zero size",
2397 args_sym->name, sym->name,
2398 &(args->expr->where));
2399 retval = FAILURE;
2402 else
2404 /* A non-allocatable target variable with C
2405 interoperable type and type parameters must be
2406 interoperable. */
2407 if (args_sym && args_sym->attr.dimension)
2409 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2411 gfc_error ("Assumed-shape array '%s' at %L "
2412 "cannot be an argument to the "
2413 "procedure '%s' because "
2414 "it is not C interoperable",
2415 args_sym->name,
2416 &(args->expr->where), sym->name);
2417 retval = FAILURE;
2419 else if (args_sym->as->type == AS_DEFERRED)
2421 gfc_error ("Deferred-shape array '%s' at %L "
2422 "cannot be an argument to the "
2423 "procedure '%s' because "
2424 "it is not C interoperable",
2425 args_sym->name,
2426 &(args->expr->where), sym->name);
2427 retval = FAILURE;
2431 /* Make sure it's not a character string. Arrays of
2432 any type should be ok if the variable is of a C
2433 interoperable type. */
2434 if (arg_ts->type == BT_CHARACTER)
2435 if (arg_ts->u.cl != NULL
2436 && (arg_ts->u.cl->length == NULL
2437 || arg_ts->u.cl->length->expr_type
2438 != EXPR_CONSTANT
2439 || mpz_cmp_si
2440 (arg_ts->u.cl->length->value.integer, 1)
2441 != 0)
2442 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2444 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2445 "at %L must have a length of 1",
2446 args_sym->name, sym->name,
2447 &(args->expr->where));
2448 retval = FAILURE;
2452 else if (is_pointer
2453 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2455 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2456 scalar pointer. */
2457 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2458 "associated scalar POINTER", args_sym->name,
2459 sym->name, &(args->expr->where));
2460 retval = FAILURE;
2463 else
2465 /* The parameter is not required to be C interoperable. If it
2466 is not C interoperable, it must be a nonpolymorphic scalar
2467 with no length type parameters. It still must have either
2468 the pointer or target attribute, and it can be
2469 allocatable (but must be allocated when c_loc is called). */
2470 if (args->expr->rank != 0
2471 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2473 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2474 "scalar", args_sym->name, sym->name,
2475 &(args->expr->where));
2476 retval = FAILURE;
2478 else if (arg_ts->type == BT_CHARACTER
2479 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2481 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2482 "%L must have a length of 1",
2483 args_sym->name, sym->name,
2484 &(args->expr->where));
2485 retval = FAILURE;
2489 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2491 if (args_sym->attr.flavor != FL_PROCEDURE)
2493 /* TODO: Update this error message to allow for procedure
2494 pointers once they are implemented. */
2495 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2496 "procedure",
2497 args_sym->name, sym->name,
2498 &(args->expr->where));
2499 retval = FAILURE;
2501 else if (args_sym->attr.is_bind_c != 1)
2503 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2504 "BIND(C)",
2505 args_sym->name, sym->name,
2506 &(args->expr->where));
2507 retval = FAILURE;
2511 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2512 *new_sym = sym;
2514 else
2516 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2517 "iso_c_binding function: '%s'!\n", sym->name);
2520 return retval;
2524 /* Resolve a function call, which means resolving the arguments, then figuring
2525 out which entity the name refers to. */
2526 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2527 to INTENT(OUT) or INTENT(INOUT). */
2529 static gfc_try
2530 resolve_function (gfc_expr *expr)
2532 gfc_actual_arglist *arg;
2533 gfc_symbol *sym;
2534 const char *name;
2535 gfc_try t;
2536 int temp;
2537 procedure_type p = PROC_INTRINSIC;
2538 bool no_formal_args;
2540 sym = NULL;
2541 if (expr->symtree)
2542 sym = expr->symtree->n.sym;
2544 /* If this is a procedure pointer component, it has already been resolved. */
2545 if (gfc_is_proc_ptr_comp (expr, NULL))
2546 return SUCCESS;
2548 if (sym && sym->attr.intrinsic
2549 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2550 return FAILURE;
2552 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2554 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2555 return FAILURE;
2558 /* If this ia a deferred TBP with an abstract interface (which may
2559 of course be referenced), expr->value.function.name will be set. */
2560 if (sym && sym->attr.abstract && !expr->value.function.name)
2562 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2563 sym->name, &expr->where);
2564 return FAILURE;
2567 /* Switch off assumed size checking and do this again for certain kinds
2568 of procedure, once the procedure itself is resolved. */
2569 need_full_assumed_size++;
2571 if (expr->symtree && expr->symtree->n.sym)
2572 p = expr->symtree->n.sym->attr.proc;
2574 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2575 if (resolve_actual_arglist (expr->value.function.actual,
2576 p, no_formal_args) == FAILURE)
2577 return FAILURE;
2579 /* Need to setup the call to the correct c_associated, depending on
2580 the number of cptrs to user gives to compare. */
2581 if (sym && sym->attr.is_iso_c == 1)
2583 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2584 == FAILURE)
2585 return FAILURE;
2587 /* Get the symtree for the new symbol (resolved func).
2588 the old one will be freed later, when it's no longer used. */
2589 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2592 /* Resume assumed_size checking. */
2593 need_full_assumed_size--;
2595 /* If the procedure is external, check for usage. */
2596 if (sym && is_external_proc (sym))
2597 resolve_global_procedure (sym, &expr->where,
2598 &expr->value.function.actual, 0);
2600 if (sym && sym->ts.type == BT_CHARACTER
2601 && sym->ts.u.cl
2602 && sym->ts.u.cl->length == NULL
2603 && !sym->attr.dummy
2604 && expr->value.function.esym == NULL
2605 && !sym->attr.contained)
2607 /* Internal procedures are taken care of in resolve_contained_fntype. */
2608 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2609 "be used at %L since it is not a dummy argument",
2610 sym->name, &expr->where);
2611 return FAILURE;
2614 /* See if function is already resolved. */
2616 if (expr->value.function.name != NULL)
2618 if (expr->ts.type == BT_UNKNOWN)
2619 expr->ts = sym->ts;
2620 t = SUCCESS;
2622 else
2624 /* Apply the rules of section 14.1.2. */
2626 switch (procedure_kind (sym))
2628 case PTYPE_GENERIC:
2629 t = resolve_generic_f (expr);
2630 break;
2632 case PTYPE_SPECIFIC:
2633 t = resolve_specific_f (expr);
2634 break;
2636 case PTYPE_UNKNOWN:
2637 t = resolve_unknown_f (expr);
2638 break;
2640 default:
2641 gfc_internal_error ("resolve_function(): bad function type");
2645 /* If the expression is still a function (it might have simplified),
2646 then we check to see if we are calling an elemental function. */
2648 if (expr->expr_type != EXPR_FUNCTION)
2649 return t;
2651 temp = need_full_assumed_size;
2652 need_full_assumed_size = 0;
2654 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2655 return FAILURE;
2657 if (omp_workshare_flag
2658 && expr->value.function.esym
2659 && ! gfc_elemental (expr->value.function.esym))
2661 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2662 "in WORKSHARE construct", expr->value.function.esym->name,
2663 &expr->where);
2664 t = FAILURE;
2667 #define GENERIC_ID expr->value.function.isym->id
2668 else if (expr->value.function.actual != NULL
2669 && expr->value.function.isym != NULL
2670 && GENERIC_ID != GFC_ISYM_LBOUND
2671 && GENERIC_ID != GFC_ISYM_LEN
2672 && GENERIC_ID != GFC_ISYM_LOC
2673 && GENERIC_ID != GFC_ISYM_PRESENT)
2675 /* Array intrinsics must also have the last upper bound of an
2676 assumed size array argument. UBOUND and SIZE have to be
2677 excluded from the check if the second argument is anything
2678 than a constant. */
2680 for (arg = expr->value.function.actual; arg; arg = arg->next)
2682 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2683 && arg->next != NULL && arg->next->expr)
2685 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2686 break;
2688 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2689 break;
2691 if ((int)mpz_get_si (arg->next->expr->value.integer)
2692 < arg->expr->rank)
2693 break;
2696 if (arg->expr != NULL
2697 && arg->expr->rank > 0
2698 && resolve_assumed_size_actual (arg->expr))
2699 return FAILURE;
2702 #undef GENERIC_ID
2704 need_full_assumed_size = temp;
2705 name = NULL;
2707 if (!pure_function (expr, &name) && name)
2709 if (forall_flag)
2711 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2712 "FORALL %s", name, &expr->where,
2713 forall_flag == 2 ? "mask" : "block");
2714 t = FAILURE;
2716 else if (gfc_pure (NULL))
2718 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2719 "procedure within a PURE procedure", name, &expr->where);
2720 t = FAILURE;
2724 /* Functions without the RECURSIVE attribution are not allowed to
2725 * call themselves. */
2726 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2728 gfc_symbol *esym;
2729 esym = expr->value.function.esym;
2731 if (is_illegal_recursion (esym, gfc_current_ns))
2733 if (esym->attr.entry && esym->ns->entries)
2734 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2735 " function '%s' is not RECURSIVE",
2736 esym->name, &expr->where, esym->ns->entries->sym->name);
2737 else
2738 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2739 " is not RECURSIVE", esym->name, &expr->where);
2741 t = FAILURE;
2745 /* Character lengths of use associated functions may contains references to
2746 symbols not referenced from the current program unit otherwise. Make sure
2747 those symbols are marked as referenced. */
2749 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2750 && expr->value.function.esym->attr.use_assoc)
2752 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
2755 if (t == SUCCESS
2756 && !((expr->value.function.esym
2757 && expr->value.function.esym->attr.elemental)
2759 (expr->value.function.isym
2760 && expr->value.function.isym->elemental)))
2761 find_noncopying_intrinsics (expr->value.function.esym,
2762 expr->value.function.actual);
2764 /* Make sure that the expression has a typespec that works. */
2765 if (expr->ts.type == BT_UNKNOWN)
2767 if (expr->symtree->n.sym->result
2768 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
2769 && !expr->symtree->n.sym->result->attr.proc_pointer)
2770 expr->ts = expr->symtree->n.sym->result->ts;
2773 return t;
2777 /************* Subroutine resolution *************/
2779 static void
2780 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2782 if (gfc_pure (sym))
2783 return;
2785 if (forall_flag)
2786 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2787 sym->name, &c->loc);
2788 else if (gfc_pure (NULL))
2789 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2790 &c->loc);
2794 static match
2795 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2797 gfc_symbol *s;
2799 if (sym->attr.generic)
2801 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2802 if (s != NULL)
2804 c->resolved_sym = s;
2805 pure_subroutine (c, s);
2806 return MATCH_YES;
2809 /* TODO: Need to search for elemental references in generic interface. */
2812 if (sym->attr.intrinsic)
2813 return gfc_intrinsic_sub_interface (c, 0);
2815 return MATCH_NO;
2819 static gfc_try
2820 resolve_generic_s (gfc_code *c)
2822 gfc_symbol *sym;
2823 match m;
2825 sym = c->symtree->n.sym;
2827 for (;;)
2829 m = resolve_generic_s0 (c, sym);
2830 if (m == MATCH_YES)
2831 return SUCCESS;
2832 else if (m == MATCH_ERROR)
2833 return FAILURE;
2835 generic:
2836 if (sym->ns->parent == NULL)
2837 break;
2838 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2840 if (sym == NULL)
2841 break;
2842 if (!generic_sym (sym))
2843 goto generic;
2846 /* Last ditch attempt. See if the reference is to an intrinsic
2847 that possesses a matching interface. 14.1.2.4 */
2848 sym = c->symtree->n.sym;
2850 if (!gfc_is_intrinsic (sym, 1, c->loc))
2852 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2853 sym->name, &c->loc);
2854 return FAILURE;
2857 m = gfc_intrinsic_sub_interface (c, 0);
2858 if (m == MATCH_YES)
2859 return SUCCESS;
2860 if (m == MATCH_NO)
2861 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2862 "intrinsic subroutine interface", sym->name, &c->loc);
2864 return FAILURE;
2868 /* Set the name and binding label of the subroutine symbol in the call
2869 expression represented by 'c' to include the type and kind of the
2870 second parameter. This function is for resolving the appropriate
2871 version of c_f_pointer() and c_f_procpointer(). For example, a
2872 call to c_f_pointer() for a default integer pointer could have a
2873 name of c_f_pointer_i4. If no second arg exists, which is an error
2874 for these two functions, it defaults to the generic symbol's name
2875 and binding label. */
2877 static void
2878 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2879 char *name, char *binding_label)
2881 gfc_expr *arg = NULL;
2882 char type;
2883 int kind;
2885 /* The second arg of c_f_pointer and c_f_procpointer determines
2886 the type and kind for the procedure name. */
2887 arg = c->ext.actual->next->expr;
2889 if (arg != NULL)
2891 /* Set up the name to have the given symbol's name,
2892 plus the type and kind. */
2893 /* a derived type is marked with the type letter 'u' */
2894 if (arg->ts.type == BT_DERIVED)
2896 type = 'd';
2897 kind = 0; /* set the kind as 0 for now */
2899 else
2901 type = gfc_type_letter (arg->ts.type);
2902 kind = arg->ts.kind;
2905 if (arg->ts.type == BT_CHARACTER)
2906 /* Kind info for character strings not needed. */
2907 kind = 0;
2909 sprintf (name, "%s_%c%d", sym->name, type, kind);
2910 /* Set up the binding label as the given symbol's label plus
2911 the type and kind. */
2912 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2914 else
2916 /* If the second arg is missing, set the name and label as
2917 was, cause it should at least be found, and the missing
2918 arg error will be caught by compare_parameters(). */
2919 sprintf (name, "%s", sym->name);
2920 sprintf (binding_label, "%s", sym->binding_label);
2923 return;
2927 /* Resolve a generic version of the iso_c_binding procedure given
2928 (sym) to the specific one based on the type and kind of the
2929 argument(s). Currently, this function resolves c_f_pointer() and
2930 c_f_procpointer based on the type and kind of the second argument
2931 (FPTR). Other iso_c_binding procedures aren't specially handled.
2932 Upon successfully exiting, c->resolved_sym will hold the resolved
2933 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2934 otherwise. */
2936 match
2937 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2939 gfc_symbol *new_sym;
2940 /* this is fine, since we know the names won't use the max */
2941 char name[GFC_MAX_SYMBOL_LEN + 1];
2942 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2943 /* default to success; will override if find error */
2944 match m = MATCH_YES;
2946 /* Make sure the actual arguments are in the necessary order (based on the
2947 formal args) before resolving. */
2948 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2950 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2951 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2953 set_name_and_label (c, sym, name, binding_label);
2955 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2957 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2959 /* Make sure we got a third arg if the second arg has non-zero
2960 rank. We must also check that the type and rank are
2961 correct since we short-circuit this check in
2962 gfc_procedure_use() (called above to sort actual args). */
2963 if (c->ext.actual->next->expr->rank != 0)
2965 if(c->ext.actual->next->next == NULL
2966 || c->ext.actual->next->next->expr == NULL)
2968 m = MATCH_ERROR;
2969 gfc_error ("Missing SHAPE parameter for call to %s "
2970 "at %L", sym->name, &(c->loc));
2972 else if (c->ext.actual->next->next->expr->ts.type
2973 != BT_INTEGER
2974 || c->ext.actual->next->next->expr->rank != 1)
2976 m = MATCH_ERROR;
2977 gfc_error ("SHAPE parameter for call to %s at %L must "
2978 "be a rank 1 INTEGER array", sym->name,
2979 &(c->loc));
2985 if (m != MATCH_ERROR)
2987 /* the 1 means to add the optional arg to formal list */
2988 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
2990 /* for error reporting, say it's declared where the original was */
2991 new_sym->declared_at = sym->declared_at;
2994 else
2996 /* no differences for c_loc or c_funloc */
2997 new_sym = sym;
3000 /* set the resolved symbol */
3001 if (m != MATCH_ERROR)
3002 c->resolved_sym = new_sym;
3003 else
3004 c->resolved_sym = sym;
3006 return m;
3010 /* Resolve a subroutine call known to be specific. */
3012 static match
3013 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3015 match m;
3017 if(sym->attr.is_iso_c)
3019 m = gfc_iso_c_sub_interface (c,sym);
3020 return m;
3023 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3025 if (sym->attr.dummy)
3027 sym->attr.proc = PROC_DUMMY;
3028 goto found;
3031 sym->attr.proc = PROC_EXTERNAL;
3032 goto found;
3035 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3036 goto found;
3038 if (sym->attr.intrinsic)
3040 m = gfc_intrinsic_sub_interface (c, 1);
3041 if (m == MATCH_YES)
3042 return MATCH_YES;
3043 if (m == MATCH_NO)
3044 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3045 "with an intrinsic", sym->name, &c->loc);
3047 return MATCH_ERROR;
3050 return MATCH_NO;
3052 found:
3053 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3055 c->resolved_sym = sym;
3056 pure_subroutine (c, sym);
3058 return MATCH_YES;
3062 static gfc_try
3063 resolve_specific_s (gfc_code *c)
3065 gfc_symbol *sym;
3066 match m;
3068 sym = c->symtree->n.sym;
3070 for (;;)
3072 m = resolve_specific_s0 (c, sym);
3073 if (m == MATCH_YES)
3074 return SUCCESS;
3075 if (m == MATCH_ERROR)
3076 return FAILURE;
3078 if (sym->ns->parent == NULL)
3079 break;
3081 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3083 if (sym == NULL)
3084 break;
3087 sym = c->symtree->n.sym;
3088 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3089 sym->name, &c->loc);
3091 return FAILURE;
3095 /* Resolve a subroutine call not known to be generic nor specific. */
3097 static gfc_try
3098 resolve_unknown_s (gfc_code *c)
3100 gfc_symbol *sym;
3102 sym = c->symtree->n.sym;
3104 if (sym->attr.dummy)
3106 sym->attr.proc = PROC_DUMMY;
3107 goto found;
3110 /* See if we have an intrinsic function reference. */
3112 if (gfc_is_intrinsic (sym, 1, c->loc))
3114 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3115 return SUCCESS;
3116 return FAILURE;
3119 /* The reference is to an external name. */
3121 found:
3122 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3124 c->resolved_sym = sym;
3126 pure_subroutine (c, sym);
3128 return SUCCESS;
3132 /* Resolve a subroutine call. Although it was tempting to use the same code
3133 for functions, subroutines and functions are stored differently and this
3134 makes things awkward. */
3136 static gfc_try
3137 resolve_call (gfc_code *c)
3139 gfc_try t;
3140 procedure_type ptype = PROC_INTRINSIC;
3141 gfc_symbol *csym, *sym;
3142 bool no_formal_args;
3144 csym = c->symtree ? c->symtree->n.sym : NULL;
3146 if (csym && csym->ts.type != BT_UNKNOWN)
3148 gfc_error ("'%s' at %L has a type, which is not consistent with "
3149 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3150 return FAILURE;
3153 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3155 gfc_symtree *st;
3156 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3157 sym = st ? st->n.sym : NULL;
3158 if (sym && csym != sym
3159 && sym->ns == gfc_current_ns
3160 && sym->attr.flavor == FL_PROCEDURE
3161 && sym->attr.contained)
3163 sym->refs++;
3164 if (csym->attr.generic)
3165 c->symtree->n.sym = sym;
3166 else
3167 c->symtree = st;
3168 csym = c->symtree->n.sym;
3172 /* If this ia a deferred TBP with an abstract interface
3173 (which may of course be referenced), c->expr1 will be set. */
3174 if (csym && csym->attr.abstract && !c->expr1)
3176 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3177 csym->name, &c->loc);
3178 return FAILURE;
3181 /* Subroutines without the RECURSIVE attribution are not allowed to
3182 * call themselves. */
3183 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3185 if (csym->attr.entry && csym->ns->entries)
3186 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3187 " subroutine '%s' is not RECURSIVE",
3188 csym->name, &c->loc, csym->ns->entries->sym->name);
3189 else
3190 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3191 " is not RECURSIVE", csym->name, &c->loc);
3193 t = FAILURE;
3196 /* Switch off assumed size checking and do this again for certain kinds
3197 of procedure, once the procedure itself is resolved. */
3198 need_full_assumed_size++;
3200 if (csym)
3201 ptype = csym->attr.proc;
3203 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3204 if (resolve_actual_arglist (c->ext.actual, ptype,
3205 no_formal_args) == FAILURE)
3206 return FAILURE;
3208 /* Resume assumed_size checking. */
3209 need_full_assumed_size--;
3211 /* If external, check for usage. */
3212 if (csym && is_external_proc (csym))
3213 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3215 t = SUCCESS;
3216 if (c->resolved_sym == NULL)
3218 c->resolved_isym = NULL;
3219 switch (procedure_kind (csym))
3221 case PTYPE_GENERIC:
3222 t = resolve_generic_s (c);
3223 break;
3225 case PTYPE_SPECIFIC:
3226 t = resolve_specific_s (c);
3227 break;
3229 case PTYPE_UNKNOWN:
3230 t = resolve_unknown_s (c);
3231 break;
3233 default:
3234 gfc_internal_error ("resolve_subroutine(): bad function type");
3238 /* Some checks of elemental subroutine actual arguments. */
3239 if (resolve_elemental_actual (NULL, c) == FAILURE)
3240 return FAILURE;
3242 if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
3243 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
3244 return t;
3248 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3249 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3250 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3251 if their shapes do not match. If either op1->shape or op2->shape is
3252 NULL, return SUCCESS. */
3254 static gfc_try
3255 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3257 gfc_try t;
3258 int i;
3260 t = SUCCESS;
3262 if (op1->shape != NULL && op2->shape != NULL)
3264 for (i = 0; i < op1->rank; i++)
3266 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3268 gfc_error ("Shapes for operands at %L and %L are not conformable",
3269 &op1->where, &op2->where);
3270 t = FAILURE;
3271 break;
3276 return t;
3280 /* Resolve an operator expression node. This can involve replacing the
3281 operation with a user defined function call. */
3283 static gfc_try
3284 resolve_operator (gfc_expr *e)
3286 gfc_expr *op1, *op2;
3287 char msg[200];
3288 bool dual_locus_error;
3289 gfc_try t;
3291 /* Resolve all subnodes-- give them types. */
3293 switch (e->value.op.op)
3295 default:
3296 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3297 return FAILURE;
3299 /* Fall through... */
3301 case INTRINSIC_NOT:
3302 case INTRINSIC_UPLUS:
3303 case INTRINSIC_UMINUS:
3304 case INTRINSIC_PARENTHESES:
3305 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3306 return FAILURE;
3307 break;
3310 /* Typecheck the new node. */
3312 op1 = e->value.op.op1;
3313 op2 = e->value.op.op2;
3314 dual_locus_error = false;
3316 if ((op1 && op1->expr_type == EXPR_NULL)
3317 || (op2 && op2->expr_type == EXPR_NULL))
3319 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3320 goto bad_op;
3323 switch (e->value.op.op)
3325 case INTRINSIC_UPLUS:
3326 case INTRINSIC_UMINUS:
3327 if (op1->ts.type == BT_INTEGER
3328 || op1->ts.type == BT_REAL
3329 || op1->ts.type == BT_COMPLEX)
3331 e->ts = op1->ts;
3332 break;
3335 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3336 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3337 goto bad_op;
3339 case INTRINSIC_PLUS:
3340 case INTRINSIC_MINUS:
3341 case INTRINSIC_TIMES:
3342 case INTRINSIC_DIVIDE:
3343 case INTRINSIC_POWER:
3344 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3346 gfc_type_convert_binary (e, 1);
3347 break;
3350 sprintf (msg,
3351 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3352 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3353 gfc_typename (&op2->ts));
3354 goto bad_op;
3356 case INTRINSIC_CONCAT:
3357 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3358 && op1->ts.kind == op2->ts.kind)
3360 e->ts.type = BT_CHARACTER;
3361 e->ts.kind = op1->ts.kind;
3362 break;
3365 sprintf (msg,
3366 _("Operands of string concatenation operator at %%L are %s/%s"),
3367 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3368 goto bad_op;
3370 case INTRINSIC_AND:
3371 case INTRINSIC_OR:
3372 case INTRINSIC_EQV:
3373 case INTRINSIC_NEQV:
3374 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3376 e->ts.type = BT_LOGICAL;
3377 e->ts.kind = gfc_kind_max (op1, op2);
3378 if (op1->ts.kind < e->ts.kind)
3379 gfc_convert_type (op1, &e->ts, 2);
3380 else if (op2->ts.kind < e->ts.kind)
3381 gfc_convert_type (op2, &e->ts, 2);
3382 break;
3385 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3386 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3387 gfc_typename (&op2->ts));
3389 goto bad_op;
3391 case INTRINSIC_NOT:
3392 if (op1->ts.type == BT_LOGICAL)
3394 e->ts.type = BT_LOGICAL;
3395 e->ts.kind = op1->ts.kind;
3396 break;
3399 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3400 gfc_typename (&op1->ts));
3401 goto bad_op;
3403 case INTRINSIC_GT:
3404 case INTRINSIC_GT_OS:
3405 case INTRINSIC_GE:
3406 case INTRINSIC_GE_OS:
3407 case INTRINSIC_LT:
3408 case INTRINSIC_LT_OS:
3409 case INTRINSIC_LE:
3410 case INTRINSIC_LE_OS:
3411 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3413 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3414 goto bad_op;
3417 /* Fall through... */
3419 case INTRINSIC_EQ:
3420 case INTRINSIC_EQ_OS:
3421 case INTRINSIC_NE:
3422 case INTRINSIC_NE_OS:
3423 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3424 && op1->ts.kind == op2->ts.kind)
3426 e->ts.type = BT_LOGICAL;
3427 e->ts.kind = gfc_default_logical_kind;
3428 break;
3431 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3433 gfc_type_convert_binary (e, 1);
3435 e->ts.type = BT_LOGICAL;
3436 e->ts.kind = gfc_default_logical_kind;
3437 break;
3440 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3441 sprintf (msg,
3442 _("Logicals at %%L must be compared with %s instead of %s"),
3443 (e->value.op.op == INTRINSIC_EQ
3444 || e->value.op.op == INTRINSIC_EQ_OS)
3445 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3446 else
3447 sprintf (msg,
3448 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3449 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3450 gfc_typename (&op2->ts));
3452 goto bad_op;
3454 case INTRINSIC_USER:
3455 if (e->value.op.uop->op == NULL)
3456 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3457 else if (op2 == NULL)
3458 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3459 e->value.op.uop->name, gfc_typename (&op1->ts));
3460 else
3461 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3462 e->value.op.uop->name, gfc_typename (&op1->ts),
3463 gfc_typename (&op2->ts));
3465 goto bad_op;
3467 case INTRINSIC_PARENTHESES:
3468 e->ts = op1->ts;
3469 if (e->ts.type == BT_CHARACTER)
3470 e->ts.u.cl = op1->ts.u.cl;
3471 break;
3473 default:
3474 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3477 /* Deal with arrayness of an operand through an operator. */
3479 t = SUCCESS;
3481 switch (e->value.op.op)
3483 case INTRINSIC_PLUS:
3484 case INTRINSIC_MINUS:
3485 case INTRINSIC_TIMES:
3486 case INTRINSIC_DIVIDE:
3487 case INTRINSIC_POWER:
3488 case INTRINSIC_CONCAT:
3489 case INTRINSIC_AND:
3490 case INTRINSIC_OR:
3491 case INTRINSIC_EQV:
3492 case INTRINSIC_NEQV:
3493 case INTRINSIC_EQ:
3494 case INTRINSIC_EQ_OS:
3495 case INTRINSIC_NE:
3496 case INTRINSIC_NE_OS:
3497 case INTRINSIC_GT:
3498 case INTRINSIC_GT_OS:
3499 case INTRINSIC_GE:
3500 case INTRINSIC_GE_OS:
3501 case INTRINSIC_LT:
3502 case INTRINSIC_LT_OS:
3503 case INTRINSIC_LE:
3504 case INTRINSIC_LE_OS:
3506 if (op1->rank == 0 && op2->rank == 0)
3507 e->rank = 0;
3509 if (op1->rank == 0 && op2->rank != 0)
3511 e->rank = op2->rank;
3513 if (e->shape == NULL)
3514 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3517 if (op1->rank != 0 && op2->rank == 0)
3519 e->rank = op1->rank;
3521 if (e->shape == NULL)
3522 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3525 if (op1->rank != 0 && op2->rank != 0)
3527 if (op1->rank == op2->rank)
3529 e->rank = op1->rank;
3530 if (e->shape == NULL)
3532 t = compare_shapes(op1, op2);
3533 if (t == FAILURE)
3534 e->shape = NULL;
3535 else
3536 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3539 else
3541 /* Allow higher level expressions to work. */
3542 e->rank = 0;
3544 /* Try user-defined operators, and otherwise throw an error. */
3545 dual_locus_error = true;
3546 sprintf (msg,
3547 _("Inconsistent ranks for operator at %%L and %%L"));
3548 goto bad_op;
3552 break;
3554 case INTRINSIC_PARENTHESES:
3555 case INTRINSIC_NOT:
3556 case INTRINSIC_UPLUS:
3557 case INTRINSIC_UMINUS:
3558 /* Simply copy arrayness attribute */
3559 e->rank = op1->rank;
3561 if (e->shape == NULL)
3562 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3564 break;
3566 default:
3567 break;
3570 /* Attempt to simplify the expression. */
3571 if (t == SUCCESS)
3573 t = gfc_simplify_expr (e, 0);
3574 /* Some calls do not succeed in simplification and return FAILURE
3575 even though there is no error; e.g. variable references to
3576 PARAMETER arrays. */
3577 if (!gfc_is_constant_expr (e))
3578 t = SUCCESS;
3580 return t;
3582 bad_op:
3585 bool real_error;
3586 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3587 return SUCCESS;
3589 if (real_error)
3590 return FAILURE;
3593 if (dual_locus_error)
3594 gfc_error (msg, &op1->where, &op2->where);
3595 else
3596 gfc_error (msg, &e->where);
3598 return FAILURE;
3602 /************** Array resolution subroutines **************/
3604 typedef enum
3605 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3606 comparison;
3608 /* Compare two integer expressions. */
3610 static comparison
3611 compare_bound (gfc_expr *a, gfc_expr *b)
3613 int i;
3615 if (a == NULL || a->expr_type != EXPR_CONSTANT
3616 || b == NULL || b->expr_type != EXPR_CONSTANT)
3617 return CMP_UNKNOWN;
3619 /* If either of the types isn't INTEGER, we must have
3620 raised an error earlier. */
3622 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3623 return CMP_UNKNOWN;
3625 i = mpz_cmp (a->value.integer, b->value.integer);
3627 if (i < 0)
3628 return CMP_LT;
3629 if (i > 0)
3630 return CMP_GT;
3631 return CMP_EQ;
3635 /* Compare an integer expression with an integer. */
3637 static comparison
3638 compare_bound_int (gfc_expr *a, int b)
3640 int i;
3642 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3643 return CMP_UNKNOWN;
3645 if (a->ts.type != BT_INTEGER)
3646 gfc_internal_error ("compare_bound_int(): Bad expression");
3648 i = mpz_cmp_si (a->value.integer, b);
3650 if (i < 0)
3651 return CMP_LT;
3652 if (i > 0)
3653 return CMP_GT;
3654 return CMP_EQ;
3658 /* Compare an integer expression with a mpz_t. */
3660 static comparison
3661 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3663 int i;
3665 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3666 return CMP_UNKNOWN;
3668 if (a->ts.type != BT_INTEGER)
3669 gfc_internal_error ("compare_bound_int(): Bad expression");
3671 i = mpz_cmp (a->value.integer, b);
3673 if (i < 0)
3674 return CMP_LT;
3675 if (i > 0)
3676 return CMP_GT;
3677 return CMP_EQ;
3681 /* Compute the last value of a sequence given by a triplet.
3682 Return 0 if it wasn't able to compute the last value, or if the
3683 sequence if empty, and 1 otherwise. */
3685 static int
3686 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3687 gfc_expr *stride, mpz_t last)
3689 mpz_t rem;
3691 if (start == NULL || start->expr_type != EXPR_CONSTANT
3692 || end == NULL || end->expr_type != EXPR_CONSTANT
3693 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3694 return 0;
3696 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3697 || (stride != NULL && stride->ts.type != BT_INTEGER))
3698 return 0;
3700 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3702 if (compare_bound (start, end) == CMP_GT)
3703 return 0;
3704 mpz_set (last, end->value.integer);
3705 return 1;
3708 if (compare_bound_int (stride, 0) == CMP_GT)
3710 /* Stride is positive */
3711 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3712 return 0;
3714 else
3716 /* Stride is negative */
3717 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3718 return 0;
3721 mpz_init (rem);
3722 mpz_sub (rem, end->value.integer, start->value.integer);
3723 mpz_tdiv_r (rem, rem, stride->value.integer);
3724 mpz_sub (last, end->value.integer, rem);
3725 mpz_clear (rem);
3727 return 1;
3731 /* Compare a single dimension of an array reference to the array
3732 specification. */
3734 static gfc_try
3735 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3737 mpz_t last_value;
3739 /* Given start, end and stride values, calculate the minimum and
3740 maximum referenced indexes. */
3742 switch (ar->dimen_type[i])
3744 case DIMEN_VECTOR:
3745 break;
3747 case DIMEN_ELEMENT:
3748 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3750 gfc_warning ("Array reference at %L is out of bounds "
3751 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3752 mpz_get_si (ar->start[i]->value.integer),
3753 mpz_get_si (as->lower[i]->value.integer), i+1);
3754 return SUCCESS;
3756 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3758 gfc_warning ("Array reference at %L is out of bounds "
3759 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3760 mpz_get_si (ar->start[i]->value.integer),
3761 mpz_get_si (as->upper[i]->value.integer), i+1);
3762 return SUCCESS;
3765 break;
3767 case DIMEN_RANGE:
3769 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3770 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3772 comparison comp_start_end = compare_bound (AR_START, AR_END);
3774 /* Check for zero stride, which is not allowed. */
3775 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3777 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3778 return FAILURE;
3781 /* if start == len || (stride > 0 && start < len)
3782 || (stride < 0 && start > len),
3783 then the array section contains at least one element. In this
3784 case, there is an out-of-bounds access if
3785 (start < lower || start > upper). */
3786 if (compare_bound (AR_START, AR_END) == CMP_EQ
3787 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3788 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3789 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3790 && comp_start_end == CMP_GT))
3792 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3794 gfc_warning ("Lower array reference at %L is out of bounds "
3795 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3796 mpz_get_si (AR_START->value.integer),
3797 mpz_get_si (as->lower[i]->value.integer), i+1);
3798 return SUCCESS;
3800 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3802 gfc_warning ("Lower array reference at %L is out of bounds "
3803 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3804 mpz_get_si (AR_START->value.integer),
3805 mpz_get_si (as->upper[i]->value.integer), i+1);
3806 return SUCCESS;
3810 /* If we can compute the highest index of the array section,
3811 then it also has to be between lower and upper. */
3812 mpz_init (last_value);
3813 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3814 last_value))
3816 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3818 gfc_warning ("Upper array reference at %L is out of bounds "
3819 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3820 mpz_get_si (last_value),
3821 mpz_get_si (as->lower[i]->value.integer), i+1);
3822 mpz_clear (last_value);
3823 return SUCCESS;
3825 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3827 gfc_warning ("Upper array reference at %L is out of bounds "
3828 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3829 mpz_get_si (last_value),
3830 mpz_get_si (as->upper[i]->value.integer), i+1);
3831 mpz_clear (last_value);
3832 return SUCCESS;
3835 mpz_clear (last_value);
3837 #undef AR_START
3838 #undef AR_END
3840 break;
3842 default:
3843 gfc_internal_error ("check_dimension(): Bad array reference");
3846 return SUCCESS;
3850 /* Compare an array reference with an array specification. */
3852 static gfc_try
3853 compare_spec_to_ref (gfc_array_ref *ar)
3855 gfc_array_spec *as;
3856 int i;
3858 as = ar->as;
3859 i = as->rank - 1;
3860 /* TODO: Full array sections are only allowed as actual parameters. */
3861 if (as->type == AS_ASSUMED_SIZE
3862 && (/*ar->type == AR_FULL
3863 ||*/ (ar->type == AR_SECTION
3864 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3866 gfc_error ("Rightmost upper bound of assumed size array section "
3867 "not specified at %L", &ar->where);
3868 return FAILURE;
3871 if (ar->type == AR_FULL)
3872 return SUCCESS;
3874 if (as->rank != ar->dimen)
3876 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3877 &ar->where, ar->dimen, as->rank);
3878 return FAILURE;
3881 for (i = 0; i < as->rank; i++)
3882 if (check_dimension (i, ar, as) == FAILURE)
3883 return FAILURE;
3885 return SUCCESS;
3889 /* Resolve one part of an array index. */
3891 gfc_try
3892 gfc_resolve_index (gfc_expr *index, int check_scalar)
3894 gfc_typespec ts;
3896 if (index == NULL)
3897 return SUCCESS;
3899 if (gfc_resolve_expr (index) == FAILURE)
3900 return FAILURE;
3902 if (check_scalar && index->rank != 0)
3904 gfc_error ("Array index at %L must be scalar", &index->where);
3905 return FAILURE;
3908 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
3910 gfc_error ("Array index at %L must be of INTEGER type, found %s",
3911 &index->where, gfc_basic_typename (index->ts.type));
3912 return FAILURE;
3915 if (index->ts.type == BT_REAL)
3916 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
3917 &index->where) == FAILURE)
3918 return FAILURE;
3920 if (index->ts.kind != gfc_index_integer_kind
3921 || index->ts.type != BT_INTEGER)
3923 gfc_clear_ts (&ts);
3924 ts.type = BT_INTEGER;
3925 ts.kind = gfc_index_integer_kind;
3927 gfc_convert_type_warn (index, &ts, 2, 0);
3930 return SUCCESS;
3933 /* Resolve a dim argument to an intrinsic function. */
3935 gfc_try
3936 gfc_resolve_dim_arg (gfc_expr *dim)
3938 if (dim == NULL)
3939 return SUCCESS;
3941 if (gfc_resolve_expr (dim) == FAILURE)
3942 return FAILURE;
3944 if (dim->rank != 0)
3946 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3947 return FAILURE;
3951 if (dim->ts.type != BT_INTEGER)
3953 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3954 return FAILURE;
3957 if (dim->ts.kind != gfc_index_integer_kind)
3959 gfc_typespec ts;
3961 gfc_clear_ts (&ts);
3962 ts.type = BT_INTEGER;
3963 ts.kind = gfc_index_integer_kind;
3965 gfc_convert_type_warn (dim, &ts, 2, 0);
3968 return SUCCESS;
3971 /* Given an expression that contains array references, update those array
3972 references to point to the right array specifications. While this is
3973 filled in during matching, this information is difficult to save and load
3974 in a module, so we take care of it here.
3976 The idea here is that the original array reference comes from the
3977 base symbol. We traverse the list of reference structures, setting
3978 the stored reference to references. Component references can
3979 provide an additional array specification. */
3981 static void
3982 find_array_spec (gfc_expr *e)
3984 gfc_array_spec *as;
3985 gfc_component *c;
3986 gfc_symbol *derived;
3987 gfc_ref *ref;
3989 if (e->symtree->n.sym->ts.type == BT_CLASS)
3990 as = e->symtree->n.sym->ts.u.derived->components->as;
3991 else
3992 as = e->symtree->n.sym->as;
3993 derived = NULL;
3995 for (ref = e->ref; ref; ref = ref->next)
3996 switch (ref->type)
3998 case REF_ARRAY:
3999 if (as == NULL)
4000 gfc_internal_error ("find_array_spec(): Missing spec");
4002 ref->u.ar.as = as;
4003 as = NULL;
4004 break;
4006 case REF_COMPONENT:
4007 if (derived == NULL)
4008 derived = e->symtree->n.sym->ts.u.derived;
4010 c = derived->components;
4012 for (; c; c = c->next)
4013 if (c == ref->u.c.component)
4015 /* Track the sequence of component references. */
4016 if (c->ts.type == BT_DERIVED)
4017 derived = c->ts.u.derived;
4018 break;
4021 if (c == NULL)
4022 gfc_internal_error ("find_array_spec(): Component not found");
4024 if (c->attr.dimension)
4026 if (as != NULL)
4027 gfc_internal_error ("find_array_spec(): unused as(1)");
4028 as = c->as;
4031 break;
4033 case REF_SUBSTRING:
4034 break;
4037 if (as != NULL)
4038 gfc_internal_error ("find_array_spec(): unused as(2)");
4042 /* Resolve an array reference. */
4044 static gfc_try
4045 resolve_array_ref (gfc_array_ref *ar)
4047 int i, check_scalar;
4048 gfc_expr *e;
4050 for (i = 0; i < ar->dimen; i++)
4052 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4054 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
4055 return FAILURE;
4056 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4057 return FAILURE;
4058 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4059 return FAILURE;
4061 e = ar->start[i];
4063 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4064 switch (e->rank)
4066 case 0:
4067 ar->dimen_type[i] = DIMEN_ELEMENT;
4068 break;
4070 case 1:
4071 ar->dimen_type[i] = DIMEN_VECTOR;
4072 if (e->expr_type == EXPR_VARIABLE
4073 && e->symtree->n.sym->ts.type == BT_DERIVED)
4074 ar->start[i] = gfc_get_parentheses (e);
4075 break;
4077 default:
4078 gfc_error ("Array index at %L is an array of rank %d",
4079 &ar->c_where[i], e->rank);
4080 return FAILURE;
4084 /* If the reference type is unknown, figure out what kind it is. */
4086 if (ar->type == AR_UNKNOWN)
4088 ar->type = AR_ELEMENT;
4089 for (i = 0; i < ar->dimen; i++)
4090 if (ar->dimen_type[i] == DIMEN_RANGE
4091 || ar->dimen_type[i] == DIMEN_VECTOR)
4093 ar->type = AR_SECTION;
4094 break;
4098 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4099 return FAILURE;
4101 return SUCCESS;
4105 static gfc_try
4106 resolve_substring (gfc_ref *ref)
4108 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4110 if (ref->u.ss.start != NULL)
4112 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4113 return FAILURE;
4115 if (ref->u.ss.start->ts.type != BT_INTEGER)
4117 gfc_error ("Substring start index at %L must be of type INTEGER",
4118 &ref->u.ss.start->where);
4119 return FAILURE;
4122 if (ref->u.ss.start->rank != 0)
4124 gfc_error ("Substring start index at %L must be scalar",
4125 &ref->u.ss.start->where);
4126 return FAILURE;
4129 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4130 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4131 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4133 gfc_error ("Substring start index at %L is less than one",
4134 &ref->u.ss.start->where);
4135 return FAILURE;
4139 if (ref->u.ss.end != NULL)
4141 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4142 return FAILURE;
4144 if (ref->u.ss.end->ts.type != BT_INTEGER)
4146 gfc_error ("Substring end index at %L must be of type INTEGER",
4147 &ref->u.ss.end->where);
4148 return FAILURE;
4151 if (ref->u.ss.end->rank != 0)
4153 gfc_error ("Substring end index at %L must be scalar",
4154 &ref->u.ss.end->where);
4155 return FAILURE;
4158 if (ref->u.ss.length != NULL
4159 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4160 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4161 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4163 gfc_error ("Substring end index at %L exceeds the string length",
4164 &ref->u.ss.start->where);
4165 return FAILURE;
4168 if (compare_bound_mpz_t (ref->u.ss.end,
4169 gfc_integer_kinds[k].huge) == CMP_GT
4170 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4171 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4173 gfc_error ("Substring end index at %L is too large",
4174 &ref->u.ss.end->where);
4175 return FAILURE;
4179 return SUCCESS;
4183 /* This function supplies missing substring charlens. */
4185 void
4186 gfc_resolve_substring_charlen (gfc_expr *e)
4188 gfc_ref *char_ref;
4189 gfc_expr *start, *end;
4191 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4192 if (char_ref->type == REF_SUBSTRING)
4193 break;
4195 if (!char_ref)
4196 return;
4198 gcc_assert (char_ref->next == NULL);
4200 if (e->ts.u.cl)
4202 if (e->ts.u.cl->length)
4203 gfc_free_expr (e->ts.u.cl->length);
4204 else if (e->expr_type == EXPR_VARIABLE
4205 && e->symtree->n.sym->attr.dummy)
4206 return;
4209 e->ts.type = BT_CHARACTER;
4210 e->ts.kind = gfc_default_character_kind;
4212 if (!e->ts.u.cl)
4213 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4215 if (char_ref->u.ss.start)
4216 start = gfc_copy_expr (char_ref->u.ss.start);
4217 else
4218 start = gfc_int_expr (1);
4220 if (char_ref->u.ss.end)
4221 end = gfc_copy_expr (char_ref->u.ss.end);
4222 else if (e->expr_type == EXPR_VARIABLE)
4223 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4224 else
4225 end = NULL;
4227 if (!start || !end)
4228 return;
4230 /* Length = (end - start +1). */
4231 e->ts.u.cl->length = gfc_subtract (end, start);
4232 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length, gfc_int_expr (1));
4234 e->ts.u.cl->length->ts.type = BT_INTEGER;
4235 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4237 /* Make sure that the length is simplified. */
4238 gfc_simplify_expr (e->ts.u.cl->length, 1);
4239 gfc_resolve_expr (e->ts.u.cl->length);
4243 /* Resolve subtype references. */
4245 static gfc_try
4246 resolve_ref (gfc_expr *expr)
4248 int current_part_dimension, n_components, seen_part_dimension;
4249 gfc_ref *ref;
4251 for (ref = expr->ref; ref; ref = ref->next)
4252 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4254 find_array_spec (expr);
4255 break;
4258 for (ref = expr->ref; ref; ref = ref->next)
4259 switch (ref->type)
4261 case REF_ARRAY:
4262 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4263 return FAILURE;
4264 break;
4266 case REF_COMPONENT:
4267 break;
4269 case REF_SUBSTRING:
4270 resolve_substring (ref);
4271 break;
4274 /* Check constraints on part references. */
4276 current_part_dimension = 0;
4277 seen_part_dimension = 0;
4278 n_components = 0;
4280 for (ref = expr->ref; ref; ref = ref->next)
4282 switch (ref->type)
4284 case REF_ARRAY:
4285 switch (ref->u.ar.type)
4287 case AR_FULL:
4288 case AR_SECTION:
4289 current_part_dimension = 1;
4290 break;
4292 case AR_ELEMENT:
4293 current_part_dimension = 0;
4294 break;
4296 case AR_UNKNOWN:
4297 gfc_internal_error ("resolve_ref(): Bad array reference");
4300 break;
4302 case REF_COMPONENT:
4303 if (current_part_dimension || seen_part_dimension)
4305 /* F03:C614. */
4306 if (ref->u.c.component->attr.pointer
4307 || ref->u.c.component->attr.proc_pointer)
4309 gfc_error ("Component to the right of a part reference "
4310 "with nonzero rank must not have the POINTER "
4311 "attribute at %L", &expr->where);
4312 return FAILURE;
4314 else if (ref->u.c.component->attr.allocatable)
4316 gfc_error ("Component to the right of a part reference "
4317 "with nonzero rank must not have the ALLOCATABLE "
4318 "attribute at %L", &expr->where);
4319 return FAILURE;
4323 n_components++;
4324 break;
4326 case REF_SUBSTRING:
4327 break;
4330 if (((ref->type == REF_COMPONENT && n_components > 1)
4331 || ref->next == NULL)
4332 && current_part_dimension
4333 && seen_part_dimension)
4335 gfc_error ("Two or more part references with nonzero rank must "
4336 "not be specified at %L", &expr->where);
4337 return FAILURE;
4340 if (ref->type == REF_COMPONENT)
4342 if (current_part_dimension)
4343 seen_part_dimension = 1;
4345 /* reset to make sure */
4346 current_part_dimension = 0;
4350 return SUCCESS;
4354 /* Given an expression, determine its shape. This is easier than it sounds.
4355 Leaves the shape array NULL if it is not possible to determine the shape. */
4357 static void
4358 expression_shape (gfc_expr *e)
4360 mpz_t array[GFC_MAX_DIMENSIONS];
4361 int i;
4363 if (e->rank == 0 || e->shape != NULL)
4364 return;
4366 for (i = 0; i < e->rank; i++)
4367 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4368 goto fail;
4370 e->shape = gfc_get_shape (e->rank);
4372 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4374 return;
4376 fail:
4377 for (i--; i >= 0; i--)
4378 mpz_clear (array[i]);
4382 /* Given a variable expression node, compute the rank of the expression by
4383 examining the base symbol and any reference structures it may have. */
4385 static void
4386 expression_rank (gfc_expr *e)
4388 gfc_ref *ref;
4389 int i, rank;
4391 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4392 could lead to serious confusion... */
4393 gcc_assert (e->expr_type != EXPR_COMPCALL);
4395 if (e->ref == NULL)
4397 if (e->expr_type == EXPR_ARRAY)
4398 goto done;
4399 /* Constructors can have a rank different from one via RESHAPE(). */
4401 if (e->symtree == NULL)
4403 e->rank = 0;
4404 goto done;
4407 e->rank = (e->symtree->n.sym->as == NULL)
4408 ? 0 : e->symtree->n.sym->as->rank;
4409 goto done;
4412 rank = 0;
4414 for (ref = e->ref; ref; ref = ref->next)
4416 if (ref->type != REF_ARRAY)
4417 continue;
4419 if (ref->u.ar.type == AR_FULL)
4421 rank = ref->u.ar.as->rank;
4422 break;
4425 if (ref->u.ar.type == AR_SECTION)
4427 /* Figure out the rank of the section. */
4428 if (rank != 0)
4429 gfc_internal_error ("expression_rank(): Two array specs");
4431 for (i = 0; i < ref->u.ar.dimen; i++)
4432 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4433 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4434 rank++;
4436 break;
4440 e->rank = rank;
4442 done:
4443 expression_shape (e);
4447 /* Resolve a variable expression. */
4449 static gfc_try
4450 resolve_variable (gfc_expr *e)
4452 gfc_symbol *sym;
4453 gfc_try t;
4455 t = SUCCESS;
4457 if (e->symtree == NULL)
4458 return FAILURE;
4460 if (e->ref && resolve_ref (e) == FAILURE)
4461 return FAILURE;
4463 sym = e->symtree->n.sym;
4464 if (sym->attr.flavor == FL_PROCEDURE
4465 && (!sym->attr.function
4466 || (sym->attr.function && sym->result
4467 && sym->result->attr.proc_pointer
4468 && !sym->result->attr.function)))
4470 e->ts.type = BT_PROCEDURE;
4471 goto resolve_procedure;
4474 if (sym->ts.type != BT_UNKNOWN)
4475 gfc_variable_attr (e, &e->ts);
4476 else
4478 /* Must be a simple variable reference. */
4479 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4480 return FAILURE;
4481 e->ts = sym->ts;
4484 if (check_assumed_size_reference (sym, e))
4485 return FAILURE;
4487 /* Deal with forward references to entries during resolve_code, to
4488 satisfy, at least partially, 12.5.2.5. */
4489 if (gfc_current_ns->entries
4490 && current_entry_id == sym->entry_id
4491 && cs_base
4492 && cs_base->current
4493 && cs_base->current->op != EXEC_ENTRY)
4495 gfc_entry_list *entry;
4496 gfc_formal_arglist *formal;
4497 int n;
4498 bool seen;
4500 /* If the symbol is a dummy... */
4501 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4503 entry = gfc_current_ns->entries;
4504 seen = false;
4506 /* ...test if the symbol is a parameter of previous entries. */
4507 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4508 for (formal = entry->sym->formal; formal; formal = formal->next)
4510 if (formal->sym && sym->name == formal->sym->name)
4511 seen = true;
4514 /* If it has not been seen as a dummy, this is an error. */
4515 if (!seen)
4517 if (specification_expr)
4518 gfc_error ("Variable '%s', used in a specification expression"
4519 ", is referenced at %L before the ENTRY statement "
4520 "in which it is a parameter",
4521 sym->name, &cs_base->current->loc);
4522 else
4523 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4524 "statement in which it is a parameter",
4525 sym->name, &cs_base->current->loc);
4526 t = FAILURE;
4530 /* Now do the same check on the specification expressions. */
4531 specification_expr = 1;
4532 if (sym->ts.type == BT_CHARACTER
4533 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
4534 t = FAILURE;
4536 if (sym->as)
4537 for (n = 0; n < sym->as->rank; n++)
4539 specification_expr = 1;
4540 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4541 t = FAILURE;
4542 specification_expr = 1;
4543 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4544 t = FAILURE;
4546 specification_expr = 0;
4548 if (t == SUCCESS)
4549 /* Update the symbol's entry level. */
4550 sym->entry_id = current_entry_id + 1;
4553 resolve_procedure:
4554 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
4555 t = FAILURE;
4557 return t;
4561 /* Checks to see that the correct symbol has been host associated.
4562 The only situation where this arises is that in which a twice
4563 contained function is parsed after the host association is made.
4564 Therefore, on detecting this, change the symbol in the expression
4565 and convert the array reference into an actual arglist if the old
4566 symbol is a variable. */
4567 static bool
4568 check_host_association (gfc_expr *e)
4570 gfc_symbol *sym, *old_sym;
4571 gfc_symtree *st;
4572 int n;
4573 gfc_ref *ref;
4574 gfc_actual_arglist *arg, *tail = NULL;
4575 bool retval = e->expr_type == EXPR_FUNCTION;
4577 /* If the expression is the result of substitution in
4578 interface.c(gfc_extend_expr) because there is no way in
4579 which the host association can be wrong. */
4580 if (e->symtree == NULL
4581 || e->symtree->n.sym == NULL
4582 || e->user_operator)
4583 return retval;
4585 old_sym = e->symtree->n.sym;
4587 if (gfc_current_ns->parent
4588 && old_sym->ns != gfc_current_ns)
4590 /* Use the 'USE' name so that renamed module symbols are
4591 correctly handled. */
4592 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
4594 if (sym && old_sym != sym
4595 && sym->ts.type == old_sym->ts.type
4596 && sym->attr.flavor == FL_PROCEDURE
4597 && sym->attr.contained)
4599 /* Clear the shape, since it might not be valid. */
4600 if (e->shape != NULL)
4602 for (n = 0; n < e->rank; n++)
4603 mpz_clear (e->shape[n]);
4605 gfc_free (e->shape);
4608 /* Give the expression the right symtree! */
4609 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
4610 gcc_assert (st != NULL);
4612 if (old_sym->attr.flavor == FL_PROCEDURE
4613 || e->expr_type == EXPR_FUNCTION)
4615 /* Original was function so point to the new symbol, since
4616 the actual argument list is already attached to the
4617 expression. */
4618 e->value.function.esym = NULL;
4619 e->symtree = st;
4621 else
4623 /* Original was variable so convert array references into
4624 an actual arglist. This does not need any checking now
4625 since gfc_resolve_function will take care of it. */
4626 e->value.function.actual = NULL;
4627 e->expr_type = EXPR_FUNCTION;
4628 e->symtree = st;
4630 /* Ambiguity will not arise if the array reference is not
4631 the last reference. */
4632 for (ref = e->ref; ref; ref = ref->next)
4633 if (ref->type == REF_ARRAY && ref->next == NULL)
4634 break;
4636 gcc_assert (ref->type == REF_ARRAY);
4638 /* Grab the start expressions from the array ref and
4639 copy them into actual arguments. */
4640 for (n = 0; n < ref->u.ar.dimen; n++)
4642 arg = gfc_get_actual_arglist ();
4643 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4644 if (e->value.function.actual == NULL)
4645 tail = e->value.function.actual = arg;
4646 else
4648 tail->next = arg;
4649 tail = arg;
4653 /* Dump the reference list and set the rank. */
4654 gfc_free_ref_list (e->ref);
4655 e->ref = NULL;
4656 e->rank = sym->as ? sym->as->rank : 0;
4659 gfc_resolve_expr (e);
4660 sym->refs++;
4663 /* This might have changed! */
4664 return e->expr_type == EXPR_FUNCTION;
4668 static void
4669 gfc_resolve_character_operator (gfc_expr *e)
4671 gfc_expr *op1 = e->value.op.op1;
4672 gfc_expr *op2 = e->value.op.op2;
4673 gfc_expr *e1 = NULL;
4674 gfc_expr *e2 = NULL;
4676 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
4678 if (op1->ts.u.cl && op1->ts.u.cl->length)
4679 e1 = gfc_copy_expr (op1->ts.u.cl->length);
4680 else if (op1->expr_type == EXPR_CONSTANT)
4681 e1 = gfc_int_expr (op1->value.character.length);
4683 if (op2->ts.u.cl && op2->ts.u.cl->length)
4684 e2 = gfc_copy_expr (op2->ts.u.cl->length);
4685 else if (op2->expr_type == EXPR_CONSTANT)
4686 e2 = gfc_int_expr (op2->value.character.length);
4688 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4690 if (!e1 || !e2)
4691 return;
4693 e->ts.u.cl->length = gfc_add (e1, e2);
4694 e->ts.u.cl->length->ts.type = BT_INTEGER;
4695 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4696 gfc_simplify_expr (e->ts.u.cl->length, 0);
4697 gfc_resolve_expr (e->ts.u.cl->length);
4699 return;
4703 /* Ensure that an character expression has a charlen and, if possible, a
4704 length expression. */
4706 static void
4707 fixup_charlen (gfc_expr *e)
4709 /* The cases fall through so that changes in expression type and the need
4710 for multiple fixes are picked up. In all circumstances, a charlen should
4711 be available for the middle end to hang a backend_decl on. */
4712 switch (e->expr_type)
4714 case EXPR_OP:
4715 gfc_resolve_character_operator (e);
4717 case EXPR_ARRAY:
4718 if (e->expr_type == EXPR_ARRAY)
4719 gfc_resolve_character_array_constructor (e);
4721 case EXPR_SUBSTRING:
4722 if (!e->ts.u.cl && e->ref)
4723 gfc_resolve_substring_charlen (e);
4725 default:
4726 if (!e->ts.u.cl)
4727 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4729 break;
4734 /* Update an actual argument to include the passed-object for type-bound
4735 procedures at the right position. */
4737 static gfc_actual_arglist*
4738 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
4739 const char *name)
4741 gcc_assert (argpos > 0);
4743 if (argpos == 1)
4745 gfc_actual_arglist* result;
4747 result = gfc_get_actual_arglist ();
4748 result->expr = po;
4749 result->next = lst;
4750 if (name)
4751 result->name = name;
4753 return result;
4756 if (lst)
4757 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
4758 else
4759 lst = update_arglist_pass (NULL, po, argpos - 1, name);
4760 return lst;
4764 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
4766 static gfc_expr*
4767 extract_compcall_passed_object (gfc_expr* e)
4769 gfc_expr* po;
4771 gcc_assert (e->expr_type == EXPR_COMPCALL);
4773 if (e->value.compcall.base_object)
4774 po = gfc_copy_expr (e->value.compcall.base_object);
4775 else
4777 po = gfc_get_expr ();
4778 po->expr_type = EXPR_VARIABLE;
4779 po->symtree = e->symtree;
4780 po->ref = gfc_copy_ref (e->ref);
4781 po->where = e->where;
4784 if (gfc_resolve_expr (po) == FAILURE)
4785 return NULL;
4787 return po;
4791 /* Update the arglist of an EXPR_COMPCALL expression to include the
4792 passed-object. */
4794 static gfc_try
4795 update_compcall_arglist (gfc_expr* e)
4797 gfc_expr* po;
4798 gfc_typebound_proc* tbp;
4800 tbp = e->value.compcall.tbp;
4802 if (tbp->error)
4803 return FAILURE;
4805 po = extract_compcall_passed_object (e);
4806 if (!po)
4807 return FAILURE;
4809 if (tbp->nopass || e->value.compcall.ignore_pass)
4811 gfc_free_expr (po);
4812 return SUCCESS;
4815 gcc_assert (tbp->pass_arg_num > 0);
4816 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4817 tbp->pass_arg_num,
4818 tbp->pass_arg);
4820 return SUCCESS;
4824 /* Extract the passed object from a PPC call (a copy of it). */
4826 static gfc_expr*
4827 extract_ppc_passed_object (gfc_expr *e)
4829 gfc_expr *po;
4830 gfc_ref **ref;
4832 po = gfc_get_expr ();
4833 po->expr_type = EXPR_VARIABLE;
4834 po->symtree = e->symtree;
4835 po->ref = gfc_copy_ref (e->ref);
4836 po->where = e->where;
4838 /* Remove PPC reference. */
4839 ref = &po->ref;
4840 while ((*ref)->next)
4841 ref = &(*ref)->next;
4842 gfc_free_ref_list (*ref);
4843 *ref = NULL;
4845 if (gfc_resolve_expr (po) == FAILURE)
4846 return NULL;
4848 return po;
4852 /* Update the actual arglist of a procedure pointer component to include the
4853 passed-object. */
4855 static gfc_try
4856 update_ppc_arglist (gfc_expr* e)
4858 gfc_expr* po;
4859 gfc_component *ppc;
4860 gfc_typebound_proc* tb;
4862 if (!gfc_is_proc_ptr_comp (e, &ppc))
4863 return FAILURE;
4865 tb = ppc->tb;
4867 if (tb->error)
4868 return FAILURE;
4869 else if (tb->nopass)
4870 return SUCCESS;
4872 po = extract_ppc_passed_object (e);
4873 if (!po)
4874 return FAILURE;
4876 if (po->rank > 0)
4878 gfc_error ("Passed-object at %L must be scalar", &e->where);
4879 return FAILURE;
4882 gcc_assert (tb->pass_arg_num > 0);
4883 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4884 tb->pass_arg_num,
4885 tb->pass_arg);
4887 return SUCCESS;
4891 /* Check that the object a TBP is called on is valid, i.e. it must not be
4892 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
4894 static gfc_try
4895 check_typebound_baseobject (gfc_expr* e)
4897 gfc_expr* base;
4899 base = extract_compcall_passed_object (e);
4900 if (!base)
4901 return FAILURE;
4903 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
4905 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
4907 gfc_error ("Base object for type-bound procedure call at %L is of"
4908 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
4909 return FAILURE;
4912 /* If the procedure called is NOPASS, the base object must be scalar. */
4913 if (e->value.compcall.tbp->nopass && base->rank > 0)
4915 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
4916 " be scalar", &e->where);
4917 return FAILURE;
4920 /* FIXME: Remove once PR 41177 (this problem) is fixed completely. */
4921 if (base->rank > 0)
4923 gfc_error ("Non-scalar base object at %L currently not implemented",
4924 &e->where);
4925 return FAILURE;
4928 return SUCCESS;
4932 /* Resolve a call to a type-bound procedure, either function or subroutine,
4933 statically from the data in an EXPR_COMPCALL expression. The adapted
4934 arglist and the target-procedure symtree are returned. */
4936 static gfc_try
4937 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
4938 gfc_actual_arglist** actual)
4940 gcc_assert (e->expr_type == EXPR_COMPCALL);
4941 gcc_assert (!e->value.compcall.tbp->is_generic);
4943 /* Update the actual arglist for PASS. */
4944 if (update_compcall_arglist (e) == FAILURE)
4945 return FAILURE;
4947 *actual = e->value.compcall.actual;
4948 *target = e->value.compcall.tbp->u.specific;
4950 gfc_free_ref_list (e->ref);
4951 e->ref = NULL;
4952 e->value.compcall.actual = NULL;
4954 return SUCCESS;
4958 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
4959 which of the specific bindings (if any) matches the arglist and transform
4960 the expression into a call of that binding. */
4962 static gfc_try
4963 resolve_typebound_generic_call (gfc_expr* e)
4965 gfc_typebound_proc* genproc;
4966 const char* genname;
4968 gcc_assert (e->expr_type == EXPR_COMPCALL);
4969 genname = e->value.compcall.name;
4970 genproc = e->value.compcall.tbp;
4972 if (!genproc->is_generic)
4973 return SUCCESS;
4975 /* Try the bindings on this type and in the inheritance hierarchy. */
4976 for (; genproc; genproc = genproc->overridden)
4978 gfc_tbp_generic* g;
4980 gcc_assert (genproc->is_generic);
4981 for (g = genproc->u.generic; g; g = g->next)
4983 gfc_symbol* target;
4984 gfc_actual_arglist* args;
4985 bool matches;
4987 gcc_assert (g->specific);
4989 if (g->specific->error)
4990 continue;
4992 target = g->specific->u.specific->n.sym;
4994 /* Get the right arglist by handling PASS/NOPASS. */
4995 args = gfc_copy_actual_arglist (e->value.compcall.actual);
4996 if (!g->specific->nopass)
4998 gfc_expr* po;
4999 po = extract_compcall_passed_object (e);
5000 if (!po)
5001 return FAILURE;
5003 gcc_assert (g->specific->pass_arg_num > 0);
5004 gcc_assert (!g->specific->error);
5005 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5006 g->specific->pass_arg);
5008 resolve_actual_arglist (args, target->attr.proc,
5009 is_external_proc (target) && !target->formal);
5011 /* Check if this arglist matches the formal. */
5012 matches = gfc_arglist_matches_symbol (&args, target);
5014 /* Clean up and break out of the loop if we've found it. */
5015 gfc_free_actual_arglist (args);
5016 if (matches)
5018 e->value.compcall.tbp = g->specific;
5019 goto success;
5024 /* Nothing matching found! */
5025 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5026 " '%s' at %L", genname, &e->where);
5027 return FAILURE;
5029 success:
5030 return SUCCESS;
5034 /* Resolve a call to a type-bound subroutine. */
5036 static gfc_try
5037 resolve_typebound_call (gfc_code* c)
5039 gfc_actual_arglist* newactual;
5040 gfc_symtree* target;
5042 /* Check that's really a SUBROUTINE. */
5043 if (!c->expr1->value.compcall.tbp->subroutine)
5045 gfc_error ("'%s' at %L should be a SUBROUTINE",
5046 c->expr1->value.compcall.name, &c->loc);
5047 return FAILURE;
5050 if (check_typebound_baseobject (c->expr1) == FAILURE)
5051 return FAILURE;
5053 if (resolve_typebound_generic_call (c->expr1) == FAILURE)
5054 return FAILURE;
5056 /* Transform into an ordinary EXEC_CALL for now. */
5058 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5059 return FAILURE;
5061 c->ext.actual = newactual;
5062 c->symtree = target;
5063 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5065 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5067 gfc_free_expr (c->expr1);
5068 c->expr1 = gfc_get_expr ();
5069 c->expr1->expr_type = EXPR_FUNCTION;
5070 c->expr1->symtree = target;
5071 c->expr1->where = c->loc;
5073 return resolve_call (c);
5077 /* Resolve a component-call expression. This originally was intended
5078 only to see functions. However, it is convenient to use it in
5079 resolving subroutine class methods, since we do not have to add a
5080 gfc_code each time. */
5081 static gfc_try
5082 resolve_compcall (gfc_expr* e, bool fcn)
5084 gfc_actual_arglist* newactual;
5085 gfc_symtree* target;
5087 /* Check that's really a FUNCTION. */
5088 if (fcn && !e->value.compcall.tbp->function)
5090 gfc_error ("'%s' at %L should be a FUNCTION",
5091 e->value.compcall.name, &e->where);
5092 return FAILURE;
5094 else if (!fcn && !e->value.compcall.tbp->subroutine)
5096 /* To resolve class member calls, we borrow this bit
5097 of code to select the specific procedures. */
5098 gfc_error ("'%s' at %L should be a SUBROUTINE",
5099 e->value.compcall.name, &e->where);
5100 return FAILURE;
5103 /* These must not be assign-calls! */
5104 gcc_assert (!e->value.compcall.assign);
5106 if (check_typebound_baseobject (e) == FAILURE)
5107 return FAILURE;
5109 if (resolve_typebound_generic_call (e) == FAILURE)
5110 return FAILURE;
5111 gcc_assert (!e->value.compcall.tbp->is_generic);
5113 /* Take the rank from the function's symbol. */
5114 if (e->value.compcall.tbp->u.specific->n.sym->as)
5115 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5117 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5118 arglist to the TBP's binding target. */
5120 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5121 return FAILURE;
5123 e->value.function.actual = newactual;
5124 e->value.function.name = e->value.compcall.name;
5125 e->value.function.esym = target->n.sym;
5126 e->value.function.class_esym = NULL;
5127 e->value.function.isym = NULL;
5128 e->symtree = target;
5129 e->ts = target->n.sym->ts;
5130 e->expr_type = EXPR_FUNCTION;
5132 /* Resolution is not necessary if this is a class subroutine; this
5133 function only has to identify the specific proc. Resolution of
5134 the call will be done next in resolve_typebound_call. */
5135 return fcn ? gfc_resolve_expr (e) : SUCCESS;
5139 /* Resolve a typebound call for the members in a class. This group of
5140 functions implements dynamic dispatch in the provisional version
5141 of f03 OOP. As soon as vtables are in place and contain pointers
5142 to methods, this will no longer be necessary. */
5143 static gfc_expr *list_e;
5144 static void check_class_members (gfc_symbol *);
5145 static gfc_try class_try;
5146 static bool fcn_flag;
5147 static gfc_symbol *class_object;
5150 static void
5151 check_members (gfc_symbol *derived)
5153 if (derived->attr.flavor == FL_DERIVED)
5154 check_class_members (derived);
5158 static void
5159 check_class_members (gfc_symbol *derived)
5161 gfc_expr *e;
5162 gfc_symtree *tbp;
5163 gfc_class_esym_list *etmp;
5165 e = gfc_copy_expr (list_e);
5167 tbp = gfc_find_typebound_proc (derived, &class_try,
5168 e->value.compcall.name,
5169 false, &e->where);
5171 if (tbp == NULL)
5173 gfc_error ("no typebound available procedure named '%s' at %L",
5174 e->value.compcall.name, &e->where);
5175 return;
5178 if (tbp->n.tb->is_generic)
5180 /* If we have to match a passed class member, force the actual
5181 expression to have the correct type. */
5182 if (!tbp->n.tb->nopass)
5184 if (e->value.compcall.base_object == NULL)
5185 e->value.compcall.base_object =
5186 extract_compcall_passed_object (e);
5188 e->value.compcall.base_object->ts.type = BT_DERIVED;
5189 e->value.compcall.base_object->ts.u.derived = derived;
5193 e->value.compcall.tbp = tbp->n.tb;
5194 e->value.compcall.name = tbp->name;
5196 /* Let the original expresssion catch the assertion in
5197 resolve_compcall, since this flag does not appear to be reset or
5198 copied in some systems. */
5199 e->value.compcall.assign = 0;
5201 /* Do the renaming, PASSing, generic => specific and other
5202 good things for each class member. */
5203 class_try = (resolve_compcall (e, fcn_flag) == SUCCESS)
5204 ? class_try : FAILURE;
5206 /* Now transfer the found symbol to the esym list. */
5207 if (class_try == SUCCESS)
5209 etmp = list_e->value.function.class_esym;
5210 list_e->value.function.class_esym
5211 = gfc_get_class_esym_list();
5212 list_e->value.function.class_esym->next = etmp;
5213 list_e->value.function.class_esym->derived = derived;
5214 list_e->value.function.class_esym->esym
5215 = e->value.function.esym;
5218 gfc_free_expr (e);
5220 /* Burrow down into grandchildren types. */
5221 if (derived->f2k_derived)
5222 gfc_traverse_ns (derived->f2k_derived, check_members);
5226 /* Eliminate esym_lists where all the members point to the
5227 typebound procedure of the declared type; ie. one where
5228 type selection has no effect.. */
5229 static void
5230 resolve_class_esym (gfc_expr *e)
5232 gfc_class_esym_list *p, *q;
5233 bool empty = true;
5235 gcc_assert (e && e->expr_type == EXPR_FUNCTION);
5237 p = e->value.function.class_esym;
5238 if (p == NULL)
5239 return;
5241 for (; p; p = p->next)
5242 empty = empty && (e->value.function.esym == p->esym);
5244 if (empty)
5246 p = e->value.function.class_esym;
5247 for (; p; p = q)
5249 q = p->next;
5250 gfc_free (p);
5252 e->value.function.class_esym = NULL;
5257 /* Generate an expression for the hash value, given the reference to
5258 the class of the final expression (class_ref), the base of the
5259 full reference list (new_ref), the declared type and the class
5260 object (st). */
5261 static gfc_expr*
5262 hash_value_expr (gfc_ref *class_ref, gfc_ref *new_ref, gfc_symtree *st)
5264 gfc_expr *hash_value;
5266 /* Build an expression for the correct hash_value; ie. that of the last
5267 CLASS reference. */
5268 if (class_ref)
5270 class_ref->next = NULL;
5272 else
5274 gfc_free_ref_list (new_ref);
5275 new_ref = NULL;
5277 hash_value = gfc_get_expr ();
5278 hash_value->expr_type = EXPR_VARIABLE;
5279 hash_value->symtree = st;
5280 hash_value->symtree->n.sym->refs++;
5281 hash_value->ref = new_ref;
5282 gfc_add_component_ref (hash_value, "$vptr");
5283 gfc_add_component_ref (hash_value, "$hash");
5285 return hash_value;
5289 /* Get the ultimate declared type from an expression. In addition,
5290 return the last class/derived type reference and the copy of the
5291 reference list. */
5292 static gfc_symbol*
5293 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5294 gfc_expr *e)
5296 gfc_symbol *declared;
5297 gfc_ref *ref;
5299 declared = NULL;
5300 *class_ref = NULL;
5301 *new_ref = gfc_copy_ref (e->ref);
5302 for (ref = *new_ref; ref; ref = ref->next)
5304 if (ref->type != REF_COMPONENT)
5305 continue;
5307 if (ref->u.c.component->ts.type == BT_CLASS
5308 || ref->u.c.component->ts.type == BT_DERIVED)
5310 declared = ref->u.c.component->ts.u.derived;
5311 *class_ref = ref;
5315 if (declared == NULL)
5316 declared = e->symtree->n.sym->ts.u.derived;
5318 return declared;
5322 /* Resolve the argument expressions so that any arguments expressions
5323 that include class methods are resolved before the current call.
5324 This is necessary because of the static variables used in CLASS
5325 method resolution. */
5326 static void
5327 resolve_arg_exprs (gfc_actual_arglist *arg)
5329 /* Resolve the actual arglist expressions. */
5330 for (; arg; arg = arg->next)
5332 if (arg->expr)
5333 gfc_resolve_expr (arg->expr);
5338 /* Resolve a CLASS typebound function, or 'method'. */
5339 static gfc_try
5340 resolve_class_compcall (gfc_expr* e)
5342 gfc_symbol *derived, *declared;
5343 gfc_ref *new_ref;
5344 gfc_ref *class_ref;
5345 gfc_symtree *st;
5347 st = e->symtree;
5348 class_object = st->n.sym;
5350 /* Get the CLASS declared type. */
5351 declared = get_declared_from_expr (&class_ref, &new_ref, e);
5353 /* Weed out cases of the ultimate component being a derived type. */
5354 if (class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5356 gfc_free_ref_list (new_ref);
5357 return resolve_compcall (e, true);
5360 /* Resolve the argument expressions, */
5361 resolve_arg_exprs (e->value.function.actual);
5363 /* Get the data component, which is of the declared type. */
5364 derived = declared->components->ts.u.derived;
5366 /* Resolve the function call for each member of the class. */
5367 class_try = SUCCESS;
5368 fcn_flag = true;
5369 list_e = gfc_copy_expr (e);
5370 check_class_members (derived);
5372 class_try = (resolve_compcall (e, true) == SUCCESS)
5373 ? class_try : FAILURE;
5375 /* Transfer the class list to the original expression. Note that
5376 the class_esym list is cleaned up in trans-expr.c, as the calls
5377 are translated. */
5378 e->value.function.class_esym = list_e->value.function.class_esym;
5379 list_e->value.function.class_esym = NULL;
5380 gfc_free_expr (list_e);
5382 resolve_class_esym (e);
5384 /* More than one typebound procedure so transmit an expression for
5385 the hash_value as the selector. */
5386 if (e->value.function.class_esym != NULL)
5387 e->value.function.class_esym->hash_value
5388 = hash_value_expr (class_ref, new_ref, st);
5390 return class_try;
5393 /* Resolve a CLASS typebound subroutine, or 'method'. */
5394 static gfc_try
5395 resolve_class_typebound_call (gfc_code *code)
5397 gfc_symbol *derived, *declared;
5398 gfc_ref *new_ref;
5399 gfc_ref *class_ref;
5400 gfc_symtree *st;
5402 st = code->expr1->symtree;
5403 class_object = st->n.sym;
5405 /* Get the CLASS declared type. */
5406 declared = get_declared_from_expr (&class_ref, &new_ref, code->expr1);
5408 /* Weed out cases of the ultimate component being a derived type. */
5409 if (class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5411 gfc_free_ref_list (new_ref);
5412 return resolve_typebound_call (code);
5415 /* Resolve the argument expressions, */
5416 resolve_arg_exprs (code->expr1->value.compcall.actual);
5418 /* Get the data component, which is of the declared type. */
5419 derived = declared->components->ts.u.derived;
5421 class_try = SUCCESS;
5422 fcn_flag = false;
5423 list_e = gfc_copy_expr (code->expr1);
5424 check_class_members (derived);
5426 class_try = (resolve_typebound_call (code) == SUCCESS)
5427 ? class_try : FAILURE;
5429 /* Transfer the class list to the original expression. Note that
5430 the class_esym list is cleaned up in trans-expr.c, as the calls
5431 are translated. */
5432 code->expr1->value.function.class_esym
5433 = list_e->value.function.class_esym;
5434 list_e->value.function.class_esym = NULL;
5435 gfc_free_expr (list_e);
5437 resolve_class_esym (code->expr1);
5439 /* More than one typebound procedure so transmit an expression for
5440 the hash_value as the selector. */
5441 if (code->expr1->value.function.class_esym != NULL)
5442 code->expr1->value.function.class_esym->hash_value
5443 = hash_value_expr (class_ref, new_ref, st);
5445 return class_try;
5449 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5451 static gfc_try
5452 resolve_ppc_call (gfc_code* c)
5454 gfc_component *comp;
5455 bool b;
5457 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
5458 gcc_assert (b);
5460 c->resolved_sym = c->expr1->symtree->n.sym;
5461 c->expr1->expr_type = EXPR_VARIABLE;
5463 if (!comp->attr.subroutine)
5464 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5466 if (resolve_ref (c->expr1) == FAILURE)
5467 return FAILURE;
5469 if (update_ppc_arglist (c->expr1) == FAILURE)
5470 return FAILURE;
5472 c->ext.actual = c->expr1->value.compcall.actual;
5474 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5475 comp->formal == NULL) == FAILURE)
5476 return FAILURE;
5478 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5480 return SUCCESS;
5484 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5486 static gfc_try
5487 resolve_expr_ppc (gfc_expr* e)
5489 gfc_component *comp;
5490 bool b;
5492 b = gfc_is_proc_ptr_comp (e, &comp);
5493 gcc_assert (b);
5495 /* Convert to EXPR_FUNCTION. */
5496 e->expr_type = EXPR_FUNCTION;
5497 e->value.function.isym = NULL;
5498 e->value.function.actual = e->value.compcall.actual;
5499 e->ts = comp->ts;
5500 if (comp->as != NULL)
5501 e->rank = comp->as->rank;
5503 if (!comp->attr.function)
5504 gfc_add_function (&comp->attr, comp->name, &e->where);
5506 if (resolve_ref (e) == FAILURE)
5507 return FAILURE;
5509 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5510 comp->formal == NULL) == FAILURE)
5511 return FAILURE;
5513 if (update_ppc_arglist (e) == FAILURE)
5514 return FAILURE;
5516 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
5518 return SUCCESS;
5522 static bool
5523 gfc_is_expandable_expr (gfc_expr *e)
5525 gfc_constructor *con;
5527 if (e->expr_type == EXPR_ARRAY)
5529 /* Traverse the constructor looking for variables that are flavor
5530 parameter. Parameters must be expanded since they are fully used at
5531 compile time. */
5532 for (con = e->value.constructor; con; con = con->next)
5534 if (con->expr->expr_type == EXPR_VARIABLE
5535 && con->expr->symtree
5536 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
5537 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
5538 return true;
5539 if (con->expr->expr_type == EXPR_ARRAY
5540 && gfc_is_expandable_expr (con->expr))
5541 return true;
5545 return false;
5548 /* Resolve an expression. That is, make sure that types of operands agree
5549 with their operators, intrinsic operators are converted to function calls
5550 for overloaded types and unresolved function references are resolved. */
5552 gfc_try
5553 gfc_resolve_expr (gfc_expr *e)
5555 gfc_try t;
5557 if (e == NULL)
5558 return SUCCESS;
5560 switch (e->expr_type)
5562 case EXPR_OP:
5563 t = resolve_operator (e);
5564 break;
5566 case EXPR_FUNCTION:
5567 case EXPR_VARIABLE:
5569 if (check_host_association (e))
5570 t = resolve_function (e);
5571 else
5573 t = resolve_variable (e);
5574 if (t == SUCCESS)
5575 expression_rank (e);
5578 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
5579 && e->ref->type != REF_SUBSTRING)
5580 gfc_resolve_substring_charlen (e);
5582 break;
5584 case EXPR_COMPCALL:
5585 if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
5586 t = resolve_class_compcall (e);
5587 else
5588 t = resolve_compcall (e, true);
5589 break;
5591 case EXPR_SUBSTRING:
5592 t = resolve_ref (e);
5593 break;
5595 case EXPR_CONSTANT:
5596 case EXPR_NULL:
5597 t = SUCCESS;
5598 break;
5600 case EXPR_PPC:
5601 t = resolve_expr_ppc (e);
5602 break;
5604 case EXPR_ARRAY:
5605 t = FAILURE;
5606 if (resolve_ref (e) == FAILURE)
5607 break;
5609 t = gfc_resolve_array_constructor (e);
5610 /* Also try to expand a constructor. */
5611 if (t == SUCCESS)
5613 expression_rank (e);
5614 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
5615 gfc_expand_constructor (e);
5618 /* This provides the opportunity for the length of constructors with
5619 character valued function elements to propagate the string length
5620 to the expression. */
5621 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
5623 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
5624 here rather then add a duplicate test for it above. */
5625 gfc_expand_constructor (e);
5626 t = gfc_resolve_character_array_constructor (e);
5629 break;
5631 case EXPR_STRUCTURE:
5632 t = resolve_ref (e);
5633 if (t == FAILURE)
5634 break;
5636 t = resolve_structure_cons (e);
5637 if (t == FAILURE)
5638 break;
5640 t = gfc_simplify_expr (e, 0);
5641 break;
5643 default:
5644 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
5647 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
5648 fixup_charlen (e);
5650 return t;
5654 /* Resolve an expression from an iterator. They must be scalar and have
5655 INTEGER or (optionally) REAL type. */
5657 static gfc_try
5658 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
5659 const char *name_msgid)
5661 if (gfc_resolve_expr (expr) == FAILURE)
5662 return FAILURE;
5664 if (expr->rank != 0)
5666 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
5667 return FAILURE;
5670 if (expr->ts.type != BT_INTEGER)
5672 if (expr->ts.type == BT_REAL)
5674 if (real_ok)
5675 return gfc_notify_std (GFC_STD_F95_DEL,
5676 "Deleted feature: %s at %L must be integer",
5677 _(name_msgid), &expr->where);
5678 else
5680 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
5681 &expr->where);
5682 return FAILURE;
5685 else
5687 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
5688 return FAILURE;
5691 return SUCCESS;
5695 /* Resolve the expressions in an iterator structure. If REAL_OK is
5696 false allow only INTEGER type iterators, otherwise allow REAL types. */
5698 gfc_try
5699 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
5701 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
5702 == FAILURE)
5703 return FAILURE;
5705 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
5707 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5708 &iter->var->where);
5709 return FAILURE;
5712 if (gfc_resolve_iterator_expr (iter->start, real_ok,
5713 "Start expression in DO loop") == FAILURE)
5714 return FAILURE;
5716 if (gfc_resolve_iterator_expr (iter->end, real_ok,
5717 "End expression in DO loop") == FAILURE)
5718 return FAILURE;
5720 if (gfc_resolve_iterator_expr (iter->step, real_ok,
5721 "Step expression in DO loop") == FAILURE)
5722 return FAILURE;
5724 if (iter->step->expr_type == EXPR_CONSTANT)
5726 if ((iter->step->ts.type == BT_INTEGER
5727 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
5728 || (iter->step->ts.type == BT_REAL
5729 && mpfr_sgn (iter->step->value.real) == 0))
5731 gfc_error ("Step expression in DO loop at %L cannot be zero",
5732 &iter->step->where);
5733 return FAILURE;
5737 /* Convert start, end, and step to the same type as var. */
5738 if (iter->start->ts.kind != iter->var->ts.kind
5739 || iter->start->ts.type != iter->var->ts.type)
5740 gfc_convert_type (iter->start, &iter->var->ts, 2);
5742 if (iter->end->ts.kind != iter->var->ts.kind
5743 || iter->end->ts.type != iter->var->ts.type)
5744 gfc_convert_type (iter->end, &iter->var->ts, 2);
5746 if (iter->step->ts.kind != iter->var->ts.kind
5747 || iter->step->ts.type != iter->var->ts.type)
5748 gfc_convert_type (iter->step, &iter->var->ts, 2);
5750 if (iter->start->expr_type == EXPR_CONSTANT
5751 && iter->end->expr_type == EXPR_CONSTANT
5752 && iter->step->expr_type == EXPR_CONSTANT)
5754 int sgn, cmp;
5755 if (iter->start->ts.type == BT_INTEGER)
5757 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
5758 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
5760 else
5762 sgn = mpfr_sgn (iter->step->value.real);
5763 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
5765 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
5766 gfc_warning ("DO loop at %L will be executed zero times",
5767 &iter->step->where);
5770 return SUCCESS;
5774 /* Traversal function for find_forall_index. f == 2 signals that
5775 that variable itself is not to be checked - only the references. */
5777 static bool
5778 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
5780 if (expr->expr_type != EXPR_VARIABLE)
5781 return false;
5783 /* A scalar assignment */
5784 if (!expr->ref || *f == 1)
5786 if (expr->symtree->n.sym == sym)
5787 return true;
5788 else
5789 return false;
5792 if (*f == 2)
5793 *f = 1;
5794 return false;
5798 /* Check whether the FORALL index appears in the expression or not.
5799 Returns SUCCESS if SYM is found in EXPR. */
5801 gfc_try
5802 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
5804 if (gfc_traverse_expr (expr, sym, forall_index, f))
5805 return SUCCESS;
5806 else
5807 return FAILURE;
5811 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5812 to be a scalar INTEGER variable. The subscripts and stride are scalar
5813 INTEGERs, and if stride is a constant it must be nonzero.
5814 Furthermore "A subscript or stride in a forall-triplet-spec shall
5815 not contain a reference to any index-name in the
5816 forall-triplet-spec-list in which it appears." (7.5.4.1) */
5818 static void
5819 resolve_forall_iterators (gfc_forall_iterator *it)
5821 gfc_forall_iterator *iter, *iter2;
5823 for (iter = it; iter; iter = iter->next)
5825 if (gfc_resolve_expr (iter->var) == SUCCESS
5826 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
5827 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
5828 &iter->var->where);
5830 if (gfc_resolve_expr (iter->start) == SUCCESS
5831 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
5832 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
5833 &iter->start->where);
5834 if (iter->var->ts.kind != iter->start->ts.kind)
5835 gfc_convert_type (iter->start, &iter->var->ts, 2);
5837 if (gfc_resolve_expr (iter->end) == SUCCESS
5838 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
5839 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
5840 &iter->end->where);
5841 if (iter->var->ts.kind != iter->end->ts.kind)
5842 gfc_convert_type (iter->end, &iter->var->ts, 2);
5844 if (gfc_resolve_expr (iter->stride) == SUCCESS)
5846 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
5847 gfc_error ("FORALL stride expression at %L must be a scalar %s",
5848 &iter->stride->where, "INTEGER");
5850 if (iter->stride->expr_type == EXPR_CONSTANT
5851 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
5852 gfc_error ("FORALL stride expression at %L cannot be zero",
5853 &iter->stride->where);
5855 if (iter->var->ts.kind != iter->stride->ts.kind)
5856 gfc_convert_type (iter->stride, &iter->var->ts, 2);
5859 for (iter = it; iter; iter = iter->next)
5860 for (iter2 = iter; iter2; iter2 = iter2->next)
5862 if (find_forall_index (iter2->start,
5863 iter->var->symtree->n.sym, 0) == SUCCESS
5864 || find_forall_index (iter2->end,
5865 iter->var->symtree->n.sym, 0) == SUCCESS
5866 || find_forall_index (iter2->stride,
5867 iter->var->symtree->n.sym, 0) == SUCCESS)
5868 gfc_error ("FORALL index '%s' may not appear in triplet "
5869 "specification at %L", iter->var->symtree->name,
5870 &iter2->start->where);
5875 /* Given a pointer to a symbol that is a derived type, see if it's
5876 inaccessible, i.e. if it's defined in another module and the components are
5877 PRIVATE. The search is recursive if necessary. Returns zero if no
5878 inaccessible components are found, nonzero otherwise. */
5880 static int
5881 derived_inaccessible (gfc_symbol *sym)
5883 gfc_component *c;
5885 if (sym->attr.use_assoc && sym->attr.private_comp)
5886 return 1;
5888 for (c = sym->components; c; c = c->next)
5890 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
5891 return 1;
5894 return 0;
5898 /* Resolve the argument of a deallocate expression. The expression must be
5899 a pointer or a full array. */
5901 static gfc_try
5902 resolve_deallocate_expr (gfc_expr *e)
5904 symbol_attribute attr;
5905 int allocatable, pointer, check_intent_in;
5906 gfc_ref *ref;
5907 gfc_symbol *sym;
5908 gfc_component *c;
5910 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5911 check_intent_in = 1;
5913 if (gfc_resolve_expr (e) == FAILURE)
5914 return FAILURE;
5916 if (e->expr_type != EXPR_VARIABLE)
5917 goto bad;
5919 sym = e->symtree->n.sym;
5921 if (sym->ts.type == BT_CLASS)
5923 allocatable = sym->ts.u.derived->components->attr.allocatable;
5924 pointer = sym->ts.u.derived->components->attr.pointer;
5926 else
5928 allocatable = sym->attr.allocatable;
5929 pointer = sym->attr.pointer;
5931 for (ref = e->ref; ref; ref = ref->next)
5933 if (pointer)
5934 check_intent_in = 0;
5936 switch (ref->type)
5938 case REF_ARRAY:
5939 if (ref->u.ar.type != AR_FULL)
5940 allocatable = 0;
5941 break;
5943 case REF_COMPONENT:
5944 c = ref->u.c.component;
5945 if (c->ts.type == BT_CLASS)
5947 allocatable = c->ts.u.derived->components->attr.allocatable;
5948 pointer = c->ts.u.derived->components->attr.pointer;
5950 else
5952 allocatable = c->attr.allocatable;
5953 pointer = c->attr.pointer;
5955 break;
5957 case REF_SUBSTRING:
5958 allocatable = 0;
5959 break;
5963 attr = gfc_expr_attr (e);
5965 if (allocatable == 0 && attr.pointer == 0)
5967 bad:
5968 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5969 &e->where);
5972 if (check_intent_in && sym->attr.intent == INTENT_IN)
5974 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
5975 sym->name, &e->where);
5976 return FAILURE;
5979 if (e->ts.type == BT_CLASS)
5981 /* Only deallocate the DATA component. */
5982 gfc_add_component_ref (e, "$data");
5985 return SUCCESS;
5989 /* Returns true if the expression e contains a reference to the symbol sym. */
5990 static bool
5991 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5993 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
5994 return true;
5996 return false;
5999 bool
6000 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6002 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6006 /* Given the expression node e for an allocatable/pointer of derived type to be
6007 allocated, get the expression node to be initialized afterwards (needed for
6008 derived types with default initializers, and derived types with allocatable
6009 components that need nullification.) */
6011 gfc_expr *
6012 gfc_expr_to_initialize (gfc_expr *e)
6014 gfc_expr *result;
6015 gfc_ref *ref;
6016 int i;
6018 result = gfc_copy_expr (e);
6020 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6021 for (ref = result->ref; ref; ref = ref->next)
6022 if (ref->type == REF_ARRAY && ref->next == NULL)
6024 ref->u.ar.type = AR_FULL;
6026 for (i = 0; i < ref->u.ar.dimen; i++)
6027 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6029 result->rank = ref->u.ar.dimen;
6030 break;
6033 return result;
6037 /* Used in resolve_allocate_expr to check that a allocation-object and
6038 a source-expr are conformable. This does not catch all possible
6039 cases; in particular a runtime checking is needed. */
6041 static gfc_try
6042 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6044 /* First compare rank. */
6045 if (e2->ref && e1->rank != e2->ref->u.ar.as->rank)
6047 gfc_error ("Source-expr at %L must be scalar or have the "
6048 "same rank as the allocate-object at %L",
6049 &e1->where, &e2->where);
6050 return FAILURE;
6053 if (e1->shape)
6055 int i;
6056 mpz_t s;
6058 mpz_init (s);
6060 for (i = 0; i < e1->rank; i++)
6062 if (e2->ref->u.ar.end[i])
6064 mpz_set (s, e2->ref->u.ar.end[i]->value.integer);
6065 mpz_sub (s, s, e2->ref->u.ar.start[i]->value.integer);
6066 mpz_add_ui (s, s, 1);
6068 else
6070 mpz_set (s, e2->ref->u.ar.start[i]->value.integer);
6073 if (mpz_cmp (e1->shape[i], s) != 0)
6075 gfc_error ("Source-expr at %L and allocate-object at %L must "
6076 "have the same shape", &e1->where, &e2->where);
6077 mpz_clear (s);
6078 return FAILURE;
6082 mpz_clear (s);
6085 return SUCCESS;
6089 /* Resolve the expression in an ALLOCATE statement, doing the additional
6090 checks to see whether the expression is OK or not. The expression must
6091 have a trailing array reference that gives the size of the array. */
6093 static gfc_try
6094 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6096 int i, pointer, allocatable, dimension, check_intent_in, is_abstract;
6097 symbol_attribute attr;
6098 gfc_ref *ref, *ref2;
6099 gfc_array_ref *ar;
6100 gfc_symbol *sym;
6101 gfc_alloc *a;
6102 gfc_component *c;
6103 gfc_expr *init_e;
6105 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
6106 check_intent_in = 1;
6108 if (gfc_resolve_expr (e) == FAILURE)
6109 return FAILURE;
6111 /* Make sure the expression is allocatable or a pointer. If it is
6112 pointer, the next-to-last reference must be a pointer. */
6114 ref2 = NULL;
6115 if (e->symtree)
6116 sym = e->symtree->n.sym;
6118 /* Check whether ultimate component is abstract and CLASS. */
6119 is_abstract = 0;
6121 if (e->expr_type != EXPR_VARIABLE)
6123 allocatable = 0;
6124 attr = gfc_expr_attr (e);
6125 pointer = attr.pointer;
6126 dimension = attr.dimension;
6128 else
6130 if (sym->ts.type == BT_CLASS)
6132 allocatable = sym->ts.u.derived->components->attr.allocatable;
6133 pointer = sym->ts.u.derived->components->attr.pointer;
6134 dimension = sym->ts.u.derived->components->attr.dimension;
6135 is_abstract = sym->ts.u.derived->components->attr.abstract;
6137 else
6139 allocatable = sym->attr.allocatable;
6140 pointer = sym->attr.pointer;
6141 dimension = sym->attr.dimension;
6144 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6146 if (pointer)
6147 check_intent_in = 0;
6149 switch (ref->type)
6151 case REF_ARRAY:
6152 if (ref->next != NULL)
6153 pointer = 0;
6154 break;
6156 case REF_COMPONENT:
6157 c = ref->u.c.component;
6158 if (c->ts.type == BT_CLASS)
6160 allocatable = c->ts.u.derived->components->attr.allocatable;
6161 pointer = c->ts.u.derived->components->attr.pointer;
6162 dimension = c->ts.u.derived->components->attr.dimension;
6163 is_abstract = c->ts.u.derived->components->attr.abstract;
6165 else
6167 allocatable = c->attr.allocatable;
6168 pointer = c->attr.pointer;
6169 dimension = c->attr.dimension;
6170 is_abstract = c->attr.abstract;
6172 break;
6174 case REF_SUBSTRING:
6175 allocatable = 0;
6176 pointer = 0;
6177 break;
6182 if (allocatable == 0 && pointer == 0)
6184 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6185 &e->where);
6186 return FAILURE;
6189 /* Some checks for the SOURCE tag. */
6190 if (code->expr3)
6192 /* Check F03:C631. */
6193 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6195 gfc_error ("Type of entity at %L is type incompatible with "
6196 "source-expr at %L", &e->where, &code->expr3->where);
6197 return FAILURE;
6200 /* Check F03:C632 and restriction following Note 6.18. */
6201 if (code->expr3->rank > 0
6202 && conformable_arrays (code->expr3, e) == FAILURE)
6203 return FAILURE;
6205 /* Check F03:C633. */
6206 if (code->expr3->ts.kind != e->ts.kind)
6208 gfc_error ("The allocate-object at %L and the source-expr at %L "
6209 "shall have the same kind type parameter",
6210 &e->where, &code->expr3->where);
6211 return FAILURE;
6214 else if (is_abstract&& code->ext.alloc.ts.type == BT_UNKNOWN)
6216 gcc_assert (e->ts.type == BT_CLASS);
6217 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6218 "type-spec or SOURCE=", sym->name, &e->where);
6219 return FAILURE;
6222 if (check_intent_in && sym->attr.intent == INTENT_IN)
6224 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
6225 sym->name, &e->where);
6226 return FAILURE;
6229 if (!code->expr3)
6231 /* Add default initializer for those derived types that need them. */
6232 if (e->ts.type == BT_DERIVED
6233 && (init_e = gfc_default_initializer (&e->ts)))
6235 gfc_code *init_st = gfc_get_code ();
6236 init_st->loc = code->loc;
6237 init_st->op = EXEC_INIT_ASSIGN;
6238 init_st->expr1 = gfc_expr_to_initialize (e);
6239 init_st->expr2 = init_e;
6240 init_st->next = code->next;
6241 code->next = init_st;
6243 else if (e->ts.type == BT_CLASS
6244 && ((code->ext.alloc.ts.type == BT_UNKNOWN
6245 && (init_e = gfc_default_initializer (&e->ts.u.derived->components->ts)))
6246 || (code->ext.alloc.ts.type == BT_DERIVED
6247 && (init_e = gfc_default_initializer (&code->ext.alloc.ts)))))
6249 gfc_code *init_st = gfc_get_code ();
6250 init_st->loc = code->loc;
6251 init_st->op = EXEC_INIT_ASSIGN;
6252 init_st->expr1 = gfc_expr_to_initialize (e);
6253 init_st->expr2 = init_e;
6254 init_st->next = code->next;
6255 code->next = init_st;
6259 if (pointer || dimension == 0)
6260 return SUCCESS;
6262 /* Make sure the next-to-last reference node is an array specification. */
6264 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
6266 gfc_error ("Array specification required in ALLOCATE statement "
6267 "at %L", &e->where);
6268 return FAILURE;
6271 /* Make sure that the array section reference makes sense in the
6272 context of an ALLOCATE specification. */
6274 ar = &ref2->u.ar;
6276 for (i = 0; i < ar->dimen; i++)
6278 if (ref2->u.ar.type == AR_ELEMENT)
6279 goto check_symbols;
6281 switch (ar->dimen_type[i])
6283 case DIMEN_ELEMENT:
6284 break;
6286 case DIMEN_RANGE:
6287 if (ar->start[i] != NULL
6288 && ar->end[i] != NULL
6289 && ar->stride[i] == NULL)
6290 break;
6292 /* Fall Through... */
6294 case DIMEN_UNKNOWN:
6295 case DIMEN_VECTOR:
6296 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6297 &e->where);
6298 return FAILURE;
6301 check_symbols:
6303 for (a = code->ext.alloc.list; a; a = a->next)
6305 sym = a->expr->symtree->n.sym;
6307 /* TODO - check derived type components. */
6308 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6309 continue;
6311 if ((ar->start[i] != NULL
6312 && gfc_find_sym_in_expr (sym, ar->start[i]))
6313 || (ar->end[i] != NULL
6314 && gfc_find_sym_in_expr (sym, ar->end[i])))
6316 gfc_error ("'%s' must not appear in the array specification at "
6317 "%L in the same ALLOCATE statement where it is "
6318 "itself allocated", sym->name, &ar->where);
6319 return FAILURE;
6324 return SUCCESS;
6327 static void
6328 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6330 gfc_expr *stat, *errmsg, *pe, *qe;
6331 gfc_alloc *a, *p, *q;
6333 stat = code->expr1 ? code->expr1 : NULL;
6335 errmsg = code->expr2 ? code->expr2 : NULL;
6337 /* Check the stat variable. */
6338 if (stat)
6340 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
6341 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
6342 stat->symtree->n.sym->name, &stat->where);
6344 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
6345 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
6346 &stat->where);
6348 if ((stat->ts.type != BT_INTEGER
6349 && !(stat->ref && (stat->ref->type == REF_ARRAY
6350 || stat->ref->type == REF_COMPONENT)))
6351 || stat->rank > 0)
6352 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6353 "variable", &stat->where);
6355 for (p = code->ext.alloc.list; p; p = p->next)
6356 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
6357 gfc_error ("Stat-variable at %L shall not be %sd within "
6358 "the same %s statement", &stat->where, fcn, fcn);
6361 /* Check the errmsg variable. */
6362 if (errmsg)
6364 if (!stat)
6365 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6366 &errmsg->where);
6368 if (errmsg->symtree->n.sym->attr.intent == INTENT_IN)
6369 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
6370 errmsg->symtree->n.sym->name, &errmsg->where);
6372 if (gfc_pure (NULL) && gfc_impure_variable (errmsg->symtree->n.sym))
6373 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
6374 &errmsg->where);
6376 if ((errmsg->ts.type != BT_CHARACTER
6377 && !(errmsg->ref
6378 && (errmsg->ref->type == REF_ARRAY
6379 || errmsg->ref->type == REF_COMPONENT)))
6380 || errmsg->rank > 0 )
6381 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
6382 "variable", &errmsg->where);
6384 for (p = code->ext.alloc.list; p; p = p->next)
6385 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
6386 gfc_error ("Errmsg-variable at %L shall not be %sd within "
6387 "the same %s statement", &errmsg->where, fcn, fcn);
6390 /* Check that an allocate-object appears only once in the statement.
6391 FIXME: Checking derived types is disabled. */
6392 for (p = code->ext.alloc.list; p; p = p->next)
6394 pe = p->expr;
6395 if ((pe->ref && pe->ref->type != REF_COMPONENT)
6396 && (pe->symtree->n.sym->ts.type != BT_DERIVED))
6398 for (q = p->next; q; q = q->next)
6400 qe = q->expr;
6401 if ((qe->ref && qe->ref->type != REF_COMPONENT)
6402 && (qe->symtree->n.sym->ts.type != BT_DERIVED)
6403 && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
6404 gfc_error ("Allocate-object at %L also appears at %L",
6405 &pe->where, &qe->where);
6410 if (strcmp (fcn, "ALLOCATE") == 0)
6412 for (a = code->ext.alloc.list; a; a = a->next)
6413 resolve_allocate_expr (a->expr, code);
6415 else
6417 for (a = code->ext.alloc.list; a; a = a->next)
6418 resolve_deallocate_expr (a->expr);
6423 /************ SELECT CASE resolution subroutines ************/
6425 /* Callback function for our mergesort variant. Determines interval
6426 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
6427 op1 > op2. Assumes we're not dealing with the default case.
6428 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
6429 There are nine situations to check. */
6431 static int
6432 compare_cases (const gfc_case *op1, const gfc_case *op2)
6434 int retval;
6436 if (op1->low == NULL) /* op1 = (:L) */
6438 /* op2 = (:N), so overlap. */
6439 retval = 0;
6440 /* op2 = (M:) or (M:N), L < M */
6441 if (op2->low != NULL
6442 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6443 retval = -1;
6445 else if (op1->high == NULL) /* op1 = (K:) */
6447 /* op2 = (M:), so overlap. */
6448 retval = 0;
6449 /* op2 = (:N) or (M:N), K > N */
6450 if (op2->high != NULL
6451 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6452 retval = 1;
6454 else /* op1 = (K:L) */
6456 if (op2->low == NULL) /* op2 = (:N), K > N */
6457 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6458 ? 1 : 0;
6459 else if (op2->high == NULL) /* op2 = (M:), L < M */
6460 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6461 ? -1 : 0;
6462 else /* op2 = (M:N) */
6464 retval = 0;
6465 /* L < M */
6466 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6467 retval = -1;
6468 /* K > N */
6469 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6470 retval = 1;
6474 return retval;
6478 /* Merge-sort a double linked case list, detecting overlap in the
6479 process. LIST is the head of the double linked case list before it
6480 is sorted. Returns the head of the sorted list if we don't see any
6481 overlap, or NULL otherwise. */
6483 static gfc_case *
6484 check_case_overlap (gfc_case *list)
6486 gfc_case *p, *q, *e, *tail;
6487 int insize, nmerges, psize, qsize, cmp, overlap_seen;
6489 /* If the passed list was empty, return immediately. */
6490 if (!list)
6491 return NULL;
6493 overlap_seen = 0;
6494 insize = 1;
6496 /* Loop unconditionally. The only exit from this loop is a return
6497 statement, when we've finished sorting the case list. */
6498 for (;;)
6500 p = list;
6501 list = NULL;
6502 tail = NULL;
6504 /* Count the number of merges we do in this pass. */
6505 nmerges = 0;
6507 /* Loop while there exists a merge to be done. */
6508 while (p)
6510 int i;
6512 /* Count this merge. */
6513 nmerges++;
6515 /* Cut the list in two pieces by stepping INSIZE places
6516 forward in the list, starting from P. */
6517 psize = 0;
6518 q = p;
6519 for (i = 0; i < insize; i++)
6521 psize++;
6522 q = q->right;
6523 if (!q)
6524 break;
6526 qsize = insize;
6528 /* Now we have two lists. Merge them! */
6529 while (psize > 0 || (qsize > 0 && q != NULL))
6531 /* See from which the next case to merge comes from. */
6532 if (psize == 0)
6534 /* P is empty so the next case must come from Q. */
6535 e = q;
6536 q = q->right;
6537 qsize--;
6539 else if (qsize == 0 || q == NULL)
6541 /* Q is empty. */
6542 e = p;
6543 p = p->right;
6544 psize--;
6546 else
6548 cmp = compare_cases (p, q);
6549 if (cmp < 0)
6551 /* The whole case range for P is less than the
6552 one for Q. */
6553 e = p;
6554 p = p->right;
6555 psize--;
6557 else if (cmp > 0)
6559 /* The whole case range for Q is greater than
6560 the case range for P. */
6561 e = q;
6562 q = q->right;
6563 qsize--;
6565 else
6567 /* The cases overlap, or they are the same
6568 element in the list. Either way, we must
6569 issue an error and get the next case from P. */
6570 /* FIXME: Sort P and Q by line number. */
6571 gfc_error ("CASE label at %L overlaps with CASE "
6572 "label at %L", &p->where, &q->where);
6573 overlap_seen = 1;
6574 e = p;
6575 p = p->right;
6576 psize--;
6580 /* Add the next element to the merged list. */
6581 if (tail)
6582 tail->right = e;
6583 else
6584 list = e;
6585 e->left = tail;
6586 tail = e;
6589 /* P has now stepped INSIZE places along, and so has Q. So
6590 they're the same. */
6591 p = q;
6593 tail->right = NULL;
6595 /* If we have done only one merge or none at all, we've
6596 finished sorting the cases. */
6597 if (nmerges <= 1)
6599 if (!overlap_seen)
6600 return list;
6601 else
6602 return NULL;
6605 /* Otherwise repeat, merging lists twice the size. */
6606 insize *= 2;
6611 /* Check to see if an expression is suitable for use in a CASE statement.
6612 Makes sure that all case expressions are scalar constants of the same
6613 type. Return FAILURE if anything is wrong. */
6615 static gfc_try
6616 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
6618 if (e == NULL) return SUCCESS;
6620 if (e->ts.type != case_expr->ts.type)
6622 gfc_error ("Expression in CASE statement at %L must be of type %s",
6623 &e->where, gfc_basic_typename (case_expr->ts.type));
6624 return FAILURE;
6627 /* C805 (R808) For a given case-construct, each case-value shall be of
6628 the same type as case-expr. For character type, length differences
6629 are allowed, but the kind type parameters shall be the same. */
6631 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
6633 gfc_error ("Expression in CASE statement at %L must be of kind %d",
6634 &e->where, case_expr->ts.kind);
6635 return FAILURE;
6638 /* Convert the case value kind to that of case expression kind, if needed.
6639 FIXME: Should a warning be issued? */
6640 if (e->ts.kind != case_expr->ts.kind)
6641 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
6643 if (e->rank != 0)
6645 gfc_error ("Expression in CASE statement at %L must be scalar",
6646 &e->where);
6647 return FAILURE;
6650 return SUCCESS;
6654 /* Given a completely parsed select statement, we:
6656 - Validate all expressions and code within the SELECT.
6657 - Make sure that the selection expression is not of the wrong type.
6658 - Make sure that no case ranges overlap.
6659 - Eliminate unreachable cases and unreachable code resulting from
6660 removing case labels.
6662 The standard does allow unreachable cases, e.g. CASE (5:3). But
6663 they are a hassle for code generation, and to prevent that, we just
6664 cut them out here. This is not necessary for overlapping cases
6665 because they are illegal and we never even try to generate code.
6667 We have the additional caveat that a SELECT construct could have
6668 been a computed GOTO in the source code. Fortunately we can fairly
6669 easily work around that here: The case_expr for a "real" SELECT CASE
6670 is in code->expr1, but for a computed GOTO it is in code->expr2. All
6671 we have to do is make sure that the case_expr is a scalar integer
6672 expression. */
6674 static void
6675 resolve_select (gfc_code *code)
6677 gfc_code *body;
6678 gfc_expr *case_expr;
6679 gfc_case *cp, *default_case, *tail, *head;
6680 int seen_unreachable;
6681 int seen_logical;
6682 int ncases;
6683 bt type;
6684 gfc_try t;
6686 if (code->expr1 == NULL)
6688 /* This was actually a computed GOTO statement. */
6689 case_expr = code->expr2;
6690 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
6691 gfc_error ("Selection expression in computed GOTO statement "
6692 "at %L must be a scalar integer expression",
6693 &case_expr->where);
6695 /* Further checking is not necessary because this SELECT was built
6696 by the compiler, so it should always be OK. Just move the
6697 case_expr from expr2 to expr so that we can handle computed
6698 GOTOs as normal SELECTs from here on. */
6699 code->expr1 = code->expr2;
6700 code->expr2 = NULL;
6701 return;
6704 case_expr = code->expr1;
6706 type = case_expr->ts.type;
6707 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
6709 gfc_error ("Argument of SELECT statement at %L cannot be %s",
6710 &case_expr->where, gfc_typename (&case_expr->ts));
6712 /* Punt. Going on here just produce more garbage error messages. */
6713 return;
6716 if (case_expr->rank != 0)
6718 gfc_error ("Argument of SELECT statement at %L must be a scalar "
6719 "expression", &case_expr->where);
6721 /* Punt. */
6722 return;
6725 /* PR 19168 has a long discussion concerning a mismatch of the kinds
6726 of the SELECT CASE expression and its CASE values. Walk the lists
6727 of case values, and if we find a mismatch, promote case_expr to
6728 the appropriate kind. */
6730 if (type == BT_LOGICAL || type == BT_INTEGER)
6732 for (body = code->block; body; body = body->block)
6734 /* Walk the case label list. */
6735 for (cp = body->ext.case_list; cp; cp = cp->next)
6737 /* Intercept the DEFAULT case. It does not have a kind. */
6738 if (cp->low == NULL && cp->high == NULL)
6739 continue;
6741 /* Unreachable case ranges are discarded, so ignore. */
6742 if (cp->low != NULL && cp->high != NULL
6743 && cp->low != cp->high
6744 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6745 continue;
6747 /* FIXME: Should a warning be issued? */
6748 if (cp->low != NULL
6749 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
6750 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
6752 if (cp->high != NULL
6753 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
6754 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
6759 /* Assume there is no DEFAULT case. */
6760 default_case = NULL;
6761 head = tail = NULL;
6762 ncases = 0;
6763 seen_logical = 0;
6765 for (body = code->block; body; body = body->block)
6767 /* Assume the CASE list is OK, and all CASE labels can be matched. */
6768 t = SUCCESS;
6769 seen_unreachable = 0;
6771 /* Walk the case label list, making sure that all case labels
6772 are legal. */
6773 for (cp = body->ext.case_list; cp; cp = cp->next)
6775 /* Count the number of cases in the whole construct. */
6776 ncases++;
6778 /* Intercept the DEFAULT case. */
6779 if (cp->low == NULL && cp->high == NULL)
6781 if (default_case != NULL)
6783 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6784 "by a second DEFAULT CASE at %L",
6785 &default_case->where, &cp->where);
6786 t = FAILURE;
6787 break;
6789 else
6791 default_case = cp;
6792 continue;
6796 /* Deal with single value cases and case ranges. Errors are
6797 issued from the validation function. */
6798 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
6799 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
6801 t = FAILURE;
6802 break;
6805 if (type == BT_LOGICAL
6806 && ((cp->low == NULL || cp->high == NULL)
6807 || cp->low != cp->high))
6809 gfc_error ("Logical range in CASE statement at %L is not "
6810 "allowed", &cp->low->where);
6811 t = FAILURE;
6812 break;
6815 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
6817 int value;
6818 value = cp->low->value.logical == 0 ? 2 : 1;
6819 if (value & seen_logical)
6821 gfc_error ("constant logical value in CASE statement "
6822 "is repeated at %L",
6823 &cp->low->where);
6824 t = FAILURE;
6825 break;
6827 seen_logical |= value;
6830 if (cp->low != NULL && cp->high != NULL
6831 && cp->low != cp->high
6832 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6834 if (gfc_option.warn_surprising)
6835 gfc_warning ("Range specification at %L can never "
6836 "be matched", &cp->where);
6838 cp->unreachable = 1;
6839 seen_unreachable = 1;
6841 else
6843 /* If the case range can be matched, it can also overlap with
6844 other cases. To make sure it does not, we put it in a
6845 double linked list here. We sort that with a merge sort
6846 later on to detect any overlapping cases. */
6847 if (!head)
6849 head = tail = cp;
6850 head->right = head->left = NULL;
6852 else
6854 tail->right = cp;
6855 tail->right->left = tail;
6856 tail = tail->right;
6857 tail->right = NULL;
6862 /* It there was a failure in the previous case label, give up
6863 for this case label list. Continue with the next block. */
6864 if (t == FAILURE)
6865 continue;
6867 /* See if any case labels that are unreachable have been seen.
6868 If so, we eliminate them. This is a bit of a kludge because
6869 the case lists for a single case statement (label) is a
6870 single forward linked lists. */
6871 if (seen_unreachable)
6873 /* Advance until the first case in the list is reachable. */
6874 while (body->ext.case_list != NULL
6875 && body->ext.case_list->unreachable)
6877 gfc_case *n = body->ext.case_list;
6878 body->ext.case_list = body->ext.case_list->next;
6879 n->next = NULL;
6880 gfc_free_case_list (n);
6883 /* Strip all other unreachable cases. */
6884 if (body->ext.case_list)
6886 for (cp = body->ext.case_list; cp->next; cp = cp->next)
6888 if (cp->next->unreachable)
6890 gfc_case *n = cp->next;
6891 cp->next = cp->next->next;
6892 n->next = NULL;
6893 gfc_free_case_list (n);
6900 /* See if there were overlapping cases. If the check returns NULL,
6901 there was overlap. In that case we don't do anything. If head
6902 is non-NULL, we prepend the DEFAULT case. The sorted list can
6903 then used during code generation for SELECT CASE constructs with
6904 a case expression of a CHARACTER type. */
6905 if (head)
6907 head = check_case_overlap (head);
6909 /* Prepend the default_case if it is there. */
6910 if (head != NULL && default_case)
6912 default_case->left = NULL;
6913 default_case->right = head;
6914 head->left = default_case;
6918 /* Eliminate dead blocks that may be the result if we've seen
6919 unreachable case labels for a block. */
6920 for (body = code; body && body->block; body = body->block)
6922 if (body->block->ext.case_list == NULL)
6924 /* Cut the unreachable block from the code chain. */
6925 gfc_code *c = body->block;
6926 body->block = c->block;
6928 /* Kill the dead block, but not the blocks below it. */
6929 c->block = NULL;
6930 gfc_free_statements (c);
6934 /* More than two cases is legal but insane for logical selects.
6935 Issue a warning for it. */
6936 if (gfc_option.warn_surprising && type == BT_LOGICAL
6937 && ncases > 2)
6938 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
6939 &code->loc);
6943 /* Check if a derived type is extensible. */
6945 bool
6946 gfc_type_is_extensible (gfc_symbol *sym)
6948 return !(sym->attr.is_bind_c || sym->attr.sequence);
6952 /* Resolve a SELECT TYPE statement. */
6954 static void
6955 resolve_select_type (gfc_code *code)
6957 gfc_symbol *selector_type;
6958 gfc_code *body, *new_st, *if_st, *tail;
6959 gfc_code *class_is = NULL, *default_case = NULL;
6960 gfc_case *c;
6961 gfc_symtree *st;
6962 char name[GFC_MAX_SYMBOL_LEN];
6963 gfc_namespace *ns;
6964 int error = 0;
6966 ns = code->ext.ns;
6967 gfc_resolve (ns);
6969 if (code->expr2)
6970 selector_type = code->expr2->ts.u.derived->components->ts.u.derived;
6971 else
6972 selector_type = code->expr1->ts.u.derived->components->ts.u.derived;
6974 /* Loop over TYPE IS / CLASS IS cases. */
6975 for (body = code->block; body; body = body->block)
6977 c = body->ext.case_list;
6979 /* Check F03:C815. */
6980 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
6981 && !gfc_type_is_extensible (c->ts.u.derived))
6983 gfc_error ("Derived type '%s' at %L must be extensible",
6984 c->ts.u.derived->name, &c->where);
6985 error++;
6986 continue;
6989 /* Check F03:C816. */
6990 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
6991 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
6993 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
6994 c->ts.u.derived->name, &c->where, selector_type->name);
6995 error++;
6996 continue;
6999 /* Intercept the DEFAULT case. */
7000 if (c->ts.type == BT_UNKNOWN)
7002 /* Check F03:C818. */
7003 if (default_case)
7005 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7006 "by a second DEFAULT CASE at %L",
7007 &default_case->ext.case_list->where, &c->where);
7008 error++;
7009 continue;
7011 else
7012 default_case = body;
7016 if (error>0)
7017 return;
7019 if (code->expr2)
7021 /* Insert assignment for selector variable. */
7022 new_st = gfc_get_code ();
7023 new_st->op = EXEC_ASSIGN;
7024 new_st->expr1 = gfc_copy_expr (code->expr1);
7025 new_st->expr2 = gfc_copy_expr (code->expr2);
7026 ns->code = new_st;
7029 /* Put SELECT TYPE statement inside a BLOCK. */
7030 new_st = gfc_get_code ();
7031 new_st->op = code->op;
7032 new_st->expr1 = code->expr1;
7033 new_st->expr2 = code->expr2;
7034 new_st->block = code->block;
7035 if (!ns->code)
7036 ns->code = new_st;
7037 else
7038 ns->code->next = new_st;
7039 code->op = EXEC_BLOCK;
7040 code->expr1 = code->expr2 = NULL;
7041 code->block = NULL;
7043 code = new_st;
7045 /* Transform to EXEC_SELECT. */
7046 code->op = EXEC_SELECT;
7047 gfc_add_component_ref (code->expr1, "$vptr");
7048 gfc_add_component_ref (code->expr1, "$hash");
7050 /* Loop over TYPE IS / CLASS IS cases. */
7051 for (body = code->block; body; body = body->block)
7053 c = body->ext.case_list;
7055 if (c->ts.type == BT_DERIVED)
7056 c->low = c->high = gfc_int_expr (c->ts.u.derived->hash_value);
7057 else if (c->ts.type == BT_UNKNOWN)
7058 continue;
7060 /* Assign temporary to selector. */
7061 if (c->ts.type == BT_CLASS)
7062 sprintf (name, "tmp$class$%s", c->ts.u.derived->name);
7063 else
7064 sprintf (name, "tmp$type$%s", c->ts.u.derived->name);
7065 st = gfc_find_symtree (ns->sym_root, name);
7066 new_st = gfc_get_code ();
7067 new_st->expr1 = gfc_get_variable_expr (st);
7068 new_st->expr2 = gfc_get_variable_expr (code->expr1->symtree);
7069 if (c->ts.type == BT_DERIVED)
7071 new_st->op = EXEC_POINTER_ASSIGN;
7072 gfc_add_component_ref (new_st->expr2, "$data");
7074 else
7075 new_st->op = EXEC_POINTER_ASSIGN;
7076 new_st->next = body->next;
7077 body->next = new_st;
7080 /* Take out CLASS IS cases for separate treatment. */
7081 body = code;
7082 while (body && body->block)
7084 if (body->block->ext.case_list->ts.type == BT_CLASS)
7086 /* Add to class_is list. */
7087 if (class_is == NULL)
7089 class_is = body->block;
7090 tail = class_is;
7092 else
7094 for (tail = class_is; tail->block; tail = tail->block) ;
7095 tail->block = body->block;
7096 tail = tail->block;
7098 /* Remove from EXEC_SELECT list. */
7099 body->block = body->block->block;
7100 tail->block = NULL;
7102 else
7103 body = body->block;
7106 if (class_is)
7108 gfc_symbol *vtab;
7110 if (!default_case)
7112 /* Add a default case to hold the CLASS IS cases. */
7113 for (tail = code; tail->block; tail = tail->block) ;
7114 tail->block = gfc_get_code ();
7115 tail = tail->block;
7116 tail->op = EXEC_SELECT_TYPE;
7117 tail->ext.case_list = gfc_get_case ();
7118 tail->ext.case_list->ts.type = BT_UNKNOWN;
7119 tail->next = NULL;
7120 default_case = tail;
7123 /* More than one CLASS IS block? */
7124 if (class_is->block)
7126 gfc_code **c1,*c2;
7127 bool swapped;
7128 /* Sort CLASS IS blocks by extension level. */
7131 swapped = false;
7132 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
7134 c2 = (*c1)->block;
7135 /* F03:C817 (check for doubles). */
7136 if ((*c1)->ext.case_list->ts.u.derived->hash_value
7137 == c2->ext.case_list->ts.u.derived->hash_value)
7139 gfc_error ("Double CLASS IS block in SELECT TYPE "
7140 "statement at %L", &c2->ext.case_list->where);
7141 return;
7143 if ((*c1)->ext.case_list->ts.u.derived->attr.extension
7144 < c2->ext.case_list->ts.u.derived->attr.extension)
7146 /* Swap. */
7147 (*c1)->block = c2->block;
7148 c2->block = *c1;
7149 *c1 = c2;
7150 swapped = true;
7154 while (swapped);
7157 /* Generate IF chain. */
7158 if_st = gfc_get_code ();
7159 if_st->op = EXEC_IF;
7160 new_st = if_st;
7161 for (body = class_is; body; body = body->block)
7163 new_st->block = gfc_get_code ();
7164 new_st = new_st->block;
7165 new_st->op = EXEC_IF;
7166 /* Set up IF condition: Call _gfortran_is_extension_of. */
7167 new_st->expr1 = gfc_get_expr ();
7168 new_st->expr1->expr_type = EXPR_FUNCTION;
7169 new_st->expr1->ts.type = BT_LOGICAL;
7170 new_st->expr1->ts.kind = 4;
7171 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
7172 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
7173 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
7174 /* Set up arguments. */
7175 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
7176 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
7177 gfc_add_component_ref (new_st->expr1->value.function.actual->expr, "$vptr");
7178 vtab = gfc_find_derived_vtab (body->ext.case_list->ts.u.derived);
7179 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
7180 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
7181 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
7182 new_st->next = body->next;
7184 if (default_case->next)
7186 new_st->block = gfc_get_code ();
7187 new_st = new_st->block;
7188 new_st->op = EXEC_IF;
7189 new_st->next = default_case->next;
7192 /* Replace CLASS DEFAULT code by the IF chain. */
7193 default_case->next = if_st;
7196 resolve_select (code);
7201 /* Resolve a transfer statement. This is making sure that:
7202 -- a derived type being transferred has only non-pointer components
7203 -- a derived type being transferred doesn't have private components, unless
7204 it's being transferred from the module where the type was defined
7205 -- we're not trying to transfer a whole assumed size array. */
7207 static void
7208 resolve_transfer (gfc_code *code)
7210 gfc_typespec *ts;
7211 gfc_symbol *sym;
7212 gfc_ref *ref;
7213 gfc_expr *exp;
7215 exp = code->expr1;
7217 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
7218 return;
7220 sym = exp->symtree->n.sym;
7221 ts = &sym->ts;
7223 /* Go to actual component transferred. */
7224 for (ref = code->expr1->ref; ref; ref = ref->next)
7225 if (ref->type == REF_COMPONENT)
7226 ts = &ref->u.c.component->ts;
7228 if (ts->type == BT_DERIVED)
7230 /* Check that transferred derived type doesn't contain POINTER
7231 components. */
7232 if (ts->u.derived->attr.pointer_comp)
7234 gfc_error ("Data transfer element at %L cannot have "
7235 "POINTER components", &code->loc);
7236 return;
7239 if (ts->u.derived->attr.alloc_comp)
7241 gfc_error ("Data transfer element at %L cannot have "
7242 "ALLOCATABLE components", &code->loc);
7243 return;
7246 if (derived_inaccessible (ts->u.derived))
7248 gfc_error ("Data transfer element at %L cannot have "
7249 "PRIVATE components",&code->loc);
7250 return;
7254 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
7255 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
7257 gfc_error ("Data transfer element at %L cannot be a full reference to "
7258 "an assumed-size array", &code->loc);
7259 return;
7264 /*********** Toplevel code resolution subroutines ***********/
7266 /* Find the set of labels that are reachable from this block. We also
7267 record the last statement in each block. */
7269 static void
7270 find_reachable_labels (gfc_code *block)
7272 gfc_code *c;
7274 if (!block)
7275 return;
7277 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
7279 /* Collect labels in this block. We don't keep those corresponding
7280 to END {IF|SELECT}, these are checked in resolve_branch by going
7281 up through the code_stack. */
7282 for (c = block; c; c = c->next)
7284 if (c->here && c->op != EXEC_END_BLOCK)
7285 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
7288 /* Merge with labels from parent block. */
7289 if (cs_base->prev)
7291 gcc_assert (cs_base->prev->reachable_labels);
7292 bitmap_ior_into (cs_base->reachable_labels,
7293 cs_base->prev->reachable_labels);
7297 /* Given a branch to a label, see if the branch is conforming.
7298 The code node describes where the branch is located. */
7300 static void
7301 resolve_branch (gfc_st_label *label, gfc_code *code)
7303 code_stack *stack;
7305 if (label == NULL)
7306 return;
7308 /* Step one: is this a valid branching target? */
7310 if (label->defined == ST_LABEL_UNKNOWN)
7312 gfc_error ("Label %d referenced at %L is never defined", label->value,
7313 &label->where);
7314 return;
7317 if (label->defined != ST_LABEL_TARGET)
7319 gfc_error ("Statement at %L is not a valid branch target statement "
7320 "for the branch statement at %L", &label->where, &code->loc);
7321 return;
7324 /* Step two: make sure this branch is not a branch to itself ;-) */
7326 if (code->here == label)
7328 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
7329 return;
7332 /* Step three: See if the label is in the same block as the
7333 branching statement. The hard work has been done by setting up
7334 the bitmap reachable_labels. */
7336 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
7337 return;
7339 /* Step four: If we haven't found the label in the bitmap, it may
7340 still be the label of the END of the enclosing block, in which
7341 case we find it by going up the code_stack. */
7343 for (stack = cs_base; stack; stack = stack->prev)
7344 if (stack->current->next && stack->current->next->here == label)
7345 break;
7347 if (stack)
7349 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
7350 return;
7353 /* The label is not in an enclosing block, so illegal. This was
7354 allowed in Fortran 66, so we allow it as extension. No
7355 further checks are necessary in this case. */
7356 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
7357 "as the GOTO statement at %L", &label->where,
7358 &code->loc);
7359 return;
7363 /* Check whether EXPR1 has the same shape as EXPR2. */
7365 static gfc_try
7366 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
7368 mpz_t shape[GFC_MAX_DIMENSIONS];
7369 mpz_t shape2[GFC_MAX_DIMENSIONS];
7370 gfc_try result = FAILURE;
7371 int i;
7373 /* Compare the rank. */
7374 if (expr1->rank != expr2->rank)
7375 return result;
7377 /* Compare the size of each dimension. */
7378 for (i=0; i<expr1->rank; i++)
7380 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
7381 goto ignore;
7383 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
7384 goto ignore;
7386 if (mpz_cmp (shape[i], shape2[i]))
7387 goto over;
7390 /* When either of the two expression is an assumed size array, we
7391 ignore the comparison of dimension sizes. */
7392 ignore:
7393 result = SUCCESS;
7395 over:
7396 for (i--; i >= 0; i--)
7398 mpz_clear (shape[i]);
7399 mpz_clear (shape2[i]);
7401 return result;
7405 /* Check whether a WHERE assignment target or a WHERE mask expression
7406 has the same shape as the outmost WHERE mask expression. */
7408 static void
7409 resolve_where (gfc_code *code, gfc_expr *mask)
7411 gfc_code *cblock;
7412 gfc_code *cnext;
7413 gfc_expr *e = NULL;
7415 cblock = code->block;
7417 /* Store the first WHERE mask-expr of the WHERE statement or construct.
7418 In case of nested WHERE, only the outmost one is stored. */
7419 if (mask == NULL) /* outmost WHERE */
7420 e = cblock->expr1;
7421 else /* inner WHERE */
7422 e = mask;
7424 while (cblock)
7426 if (cblock->expr1)
7428 /* Check if the mask-expr has a consistent shape with the
7429 outmost WHERE mask-expr. */
7430 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
7431 gfc_error ("WHERE mask at %L has inconsistent shape",
7432 &cblock->expr1->where);
7435 /* the assignment statement of a WHERE statement, or the first
7436 statement in where-body-construct of a WHERE construct */
7437 cnext = cblock->next;
7438 while (cnext)
7440 switch (cnext->op)
7442 /* WHERE assignment statement */
7443 case EXEC_ASSIGN:
7445 /* Check shape consistent for WHERE assignment target. */
7446 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
7447 gfc_error ("WHERE assignment target at %L has "
7448 "inconsistent shape", &cnext->expr1->where);
7449 break;
7452 case EXEC_ASSIGN_CALL:
7453 resolve_call (cnext);
7454 if (!cnext->resolved_sym->attr.elemental)
7455 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7456 &cnext->ext.actual->expr->where);
7457 break;
7459 /* WHERE or WHERE construct is part of a where-body-construct */
7460 case EXEC_WHERE:
7461 resolve_where (cnext, e);
7462 break;
7464 default:
7465 gfc_error ("Unsupported statement inside WHERE at %L",
7466 &cnext->loc);
7468 /* the next statement within the same where-body-construct */
7469 cnext = cnext->next;
7471 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7472 cblock = cblock->block;
7477 /* Resolve assignment in FORALL construct.
7478 NVAR is the number of FORALL index variables, and VAR_EXPR records the
7479 FORALL index variables. */
7481 static void
7482 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
7484 int n;
7486 for (n = 0; n < nvar; n++)
7488 gfc_symbol *forall_index;
7490 forall_index = var_expr[n]->symtree->n.sym;
7492 /* Check whether the assignment target is one of the FORALL index
7493 variable. */
7494 if ((code->expr1->expr_type == EXPR_VARIABLE)
7495 && (code->expr1->symtree->n.sym == forall_index))
7496 gfc_error ("Assignment to a FORALL index variable at %L",
7497 &code->expr1->where);
7498 else
7500 /* If one of the FORALL index variables doesn't appear in the
7501 assignment variable, then there could be a many-to-one
7502 assignment. Emit a warning rather than an error because the
7503 mask could be resolving this problem. */
7504 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
7505 gfc_warning ("The FORALL with index '%s' is not used on the "
7506 "left side of the assignment at %L and so might "
7507 "cause multiple assignment to this object",
7508 var_expr[n]->symtree->name, &code->expr1->where);
7514 /* Resolve WHERE statement in FORALL construct. */
7516 static void
7517 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
7518 gfc_expr **var_expr)
7520 gfc_code *cblock;
7521 gfc_code *cnext;
7523 cblock = code->block;
7524 while (cblock)
7526 /* the assignment statement of a WHERE statement, or the first
7527 statement in where-body-construct of a WHERE construct */
7528 cnext = cblock->next;
7529 while (cnext)
7531 switch (cnext->op)
7533 /* WHERE assignment statement */
7534 case EXEC_ASSIGN:
7535 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
7536 break;
7538 /* WHERE operator assignment statement */
7539 case EXEC_ASSIGN_CALL:
7540 resolve_call (cnext);
7541 if (!cnext->resolved_sym->attr.elemental)
7542 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7543 &cnext->ext.actual->expr->where);
7544 break;
7546 /* WHERE or WHERE construct is part of a where-body-construct */
7547 case EXEC_WHERE:
7548 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
7549 break;
7551 default:
7552 gfc_error ("Unsupported statement inside WHERE at %L",
7553 &cnext->loc);
7555 /* the next statement within the same where-body-construct */
7556 cnext = cnext->next;
7558 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7559 cblock = cblock->block;
7564 /* Traverse the FORALL body to check whether the following errors exist:
7565 1. For assignment, check if a many-to-one assignment happens.
7566 2. For WHERE statement, check the WHERE body to see if there is any
7567 many-to-one assignment. */
7569 static void
7570 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
7572 gfc_code *c;
7574 c = code->block->next;
7575 while (c)
7577 switch (c->op)
7579 case EXEC_ASSIGN:
7580 case EXEC_POINTER_ASSIGN:
7581 gfc_resolve_assign_in_forall (c, nvar, var_expr);
7582 break;
7584 case EXEC_ASSIGN_CALL:
7585 resolve_call (c);
7586 break;
7588 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
7589 there is no need to handle it here. */
7590 case EXEC_FORALL:
7591 break;
7592 case EXEC_WHERE:
7593 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
7594 break;
7595 default:
7596 break;
7598 /* The next statement in the FORALL body. */
7599 c = c->next;
7604 /* Counts the number of iterators needed inside a forall construct, including
7605 nested forall constructs. This is used to allocate the needed memory
7606 in gfc_resolve_forall. */
7608 static int
7609 gfc_count_forall_iterators (gfc_code *code)
7611 int max_iters, sub_iters, current_iters;
7612 gfc_forall_iterator *fa;
7614 gcc_assert(code->op == EXEC_FORALL);
7615 max_iters = 0;
7616 current_iters = 0;
7618 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7619 current_iters ++;
7621 code = code->block->next;
7623 while (code)
7625 if (code->op == EXEC_FORALL)
7627 sub_iters = gfc_count_forall_iterators (code);
7628 if (sub_iters > max_iters)
7629 max_iters = sub_iters;
7631 code = code->next;
7634 return current_iters + max_iters;
7638 /* Given a FORALL construct, first resolve the FORALL iterator, then call
7639 gfc_resolve_forall_body to resolve the FORALL body. */
7641 static void
7642 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
7644 static gfc_expr **var_expr;
7645 static int total_var = 0;
7646 static int nvar = 0;
7647 int old_nvar, tmp;
7648 gfc_forall_iterator *fa;
7649 int i;
7651 old_nvar = nvar;
7653 /* Start to resolve a FORALL construct */
7654 if (forall_save == 0)
7656 /* Count the total number of FORALL index in the nested FORALL
7657 construct in order to allocate the VAR_EXPR with proper size. */
7658 total_var = gfc_count_forall_iterators (code);
7660 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
7661 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
7664 /* The information about FORALL iterator, including FORALL index start, end
7665 and stride. The FORALL index can not appear in start, end or stride. */
7666 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7668 /* Check if any outer FORALL index name is the same as the current
7669 one. */
7670 for (i = 0; i < nvar; i++)
7672 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
7674 gfc_error ("An outer FORALL construct already has an index "
7675 "with this name %L", &fa->var->where);
7679 /* Record the current FORALL index. */
7680 var_expr[nvar] = gfc_copy_expr (fa->var);
7682 nvar++;
7684 /* No memory leak. */
7685 gcc_assert (nvar <= total_var);
7688 /* Resolve the FORALL body. */
7689 gfc_resolve_forall_body (code, nvar, var_expr);
7691 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
7692 gfc_resolve_blocks (code->block, ns);
7694 tmp = nvar;
7695 nvar = old_nvar;
7696 /* Free only the VAR_EXPRs allocated in this frame. */
7697 for (i = nvar; i < tmp; i++)
7698 gfc_free_expr (var_expr[i]);
7700 if (nvar == 0)
7702 /* We are in the outermost FORALL construct. */
7703 gcc_assert (forall_save == 0);
7705 /* VAR_EXPR is not needed any more. */
7706 gfc_free (var_expr);
7707 total_var = 0;
7712 /* Resolve a BLOCK construct statement. */
7714 static void
7715 resolve_block_construct (gfc_code* code)
7717 /* Eventually, we may want to do some checks here or handle special stuff.
7718 But so far the only thing we can do is resolving the local namespace. */
7720 gfc_resolve (code->ext.ns);
7724 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
7725 DO code nodes. */
7727 static void resolve_code (gfc_code *, gfc_namespace *);
7729 void
7730 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
7732 gfc_try t;
7734 for (; b; b = b->block)
7736 t = gfc_resolve_expr (b->expr1);
7737 if (gfc_resolve_expr (b->expr2) == FAILURE)
7738 t = FAILURE;
7740 switch (b->op)
7742 case EXEC_IF:
7743 if (t == SUCCESS && b->expr1 != NULL
7744 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
7745 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
7746 &b->expr1->where);
7747 break;
7749 case EXEC_WHERE:
7750 if (t == SUCCESS
7751 && b->expr1 != NULL
7752 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
7753 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
7754 &b->expr1->where);
7755 break;
7757 case EXEC_GOTO:
7758 resolve_branch (b->label1, b);
7759 break;
7761 case EXEC_BLOCK:
7762 resolve_block_construct (b);
7763 break;
7765 case EXEC_SELECT:
7766 case EXEC_SELECT_TYPE:
7767 case EXEC_FORALL:
7768 case EXEC_DO:
7769 case EXEC_DO_WHILE:
7770 case EXEC_READ:
7771 case EXEC_WRITE:
7772 case EXEC_IOLENGTH:
7773 case EXEC_WAIT:
7774 break;
7776 case EXEC_OMP_ATOMIC:
7777 case EXEC_OMP_CRITICAL:
7778 case EXEC_OMP_DO:
7779 case EXEC_OMP_MASTER:
7780 case EXEC_OMP_ORDERED:
7781 case EXEC_OMP_PARALLEL:
7782 case EXEC_OMP_PARALLEL_DO:
7783 case EXEC_OMP_PARALLEL_SECTIONS:
7784 case EXEC_OMP_PARALLEL_WORKSHARE:
7785 case EXEC_OMP_SECTIONS:
7786 case EXEC_OMP_SINGLE:
7787 case EXEC_OMP_TASK:
7788 case EXEC_OMP_TASKWAIT:
7789 case EXEC_OMP_WORKSHARE:
7790 break;
7792 default:
7793 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
7796 resolve_code (b->next, ns);
7801 /* Does everything to resolve an ordinary assignment. Returns true
7802 if this is an interface assignment. */
7803 static bool
7804 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
7806 bool rval = false;
7807 gfc_expr *lhs;
7808 gfc_expr *rhs;
7809 int llen = 0;
7810 int rlen = 0;
7811 int n;
7812 gfc_ref *ref;
7814 if (gfc_extend_assign (code, ns) == SUCCESS)
7816 gfc_expr** rhsptr;
7818 if (code->op == EXEC_ASSIGN_CALL)
7820 lhs = code->ext.actual->expr;
7821 rhsptr = &code->ext.actual->next->expr;
7823 else
7825 gfc_actual_arglist* args;
7826 gfc_typebound_proc* tbp;
7828 gcc_assert (code->op == EXEC_COMPCALL);
7830 args = code->expr1->value.compcall.actual;
7831 lhs = args->expr;
7832 rhsptr = &args->next->expr;
7834 tbp = code->expr1->value.compcall.tbp;
7835 gcc_assert (!tbp->is_generic);
7838 /* Make a temporary rhs when there is a default initializer
7839 and rhs is the same symbol as the lhs. */
7840 if ((*rhsptr)->expr_type == EXPR_VARIABLE
7841 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
7842 && has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
7843 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
7844 *rhsptr = gfc_get_parentheses (*rhsptr);
7846 return true;
7849 lhs = code->expr1;
7850 rhs = code->expr2;
7852 if (rhs->is_boz
7853 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
7854 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
7855 &code->loc) == FAILURE)
7856 return false;
7858 /* Handle the case of a BOZ literal on the RHS. */
7859 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
7861 int rc;
7862 if (gfc_option.warn_surprising)
7863 gfc_warning ("BOZ literal at %L is bitwise transferred "
7864 "non-integer symbol '%s'", &code->loc,
7865 lhs->symtree->n.sym->name);
7867 if (!gfc_convert_boz (rhs, &lhs->ts))
7868 return false;
7869 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
7871 if (rc == ARITH_UNDERFLOW)
7872 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
7873 ". This check can be disabled with the option "
7874 "-fno-range-check", &rhs->where);
7875 else if (rc == ARITH_OVERFLOW)
7876 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
7877 ". This check can be disabled with the option "
7878 "-fno-range-check", &rhs->where);
7879 else if (rc == ARITH_NAN)
7880 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
7881 ". This check can be disabled with the option "
7882 "-fno-range-check", &rhs->where);
7883 return false;
7888 if (lhs->ts.type == BT_CHARACTER
7889 && gfc_option.warn_character_truncation)
7891 if (lhs->ts.u.cl != NULL
7892 && lhs->ts.u.cl->length != NULL
7893 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7894 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
7896 if (rhs->expr_type == EXPR_CONSTANT)
7897 rlen = rhs->value.character.length;
7899 else if (rhs->ts.u.cl != NULL
7900 && rhs->ts.u.cl->length != NULL
7901 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7902 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
7904 if (rlen && llen && rlen > llen)
7905 gfc_warning_now ("CHARACTER expression will be truncated "
7906 "in assignment (%d/%d) at %L",
7907 llen, rlen, &code->loc);
7910 /* Ensure that a vector index expression for the lvalue is evaluated
7911 to a temporary if the lvalue symbol is referenced in it. */
7912 if (lhs->rank)
7914 for (ref = lhs->ref; ref; ref= ref->next)
7915 if (ref->type == REF_ARRAY)
7917 for (n = 0; n < ref->u.ar.dimen; n++)
7918 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
7919 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
7920 ref->u.ar.start[n]))
7921 ref->u.ar.start[n]
7922 = gfc_get_parentheses (ref->u.ar.start[n]);
7926 if (gfc_pure (NULL))
7928 if (gfc_impure_variable (lhs->symtree->n.sym))
7930 gfc_error ("Cannot assign to variable '%s' in PURE "
7931 "procedure at %L",
7932 lhs->symtree->n.sym->name,
7933 &lhs->where);
7934 return rval;
7937 if (lhs->ts.type == BT_DERIVED
7938 && lhs->expr_type == EXPR_VARIABLE
7939 && lhs->ts.u.derived->attr.pointer_comp
7940 && gfc_impure_variable (rhs->symtree->n.sym))
7942 gfc_error ("The impure variable at %L is assigned to "
7943 "a derived type variable with a POINTER "
7944 "component in a PURE procedure (12.6)",
7945 &rhs->where);
7946 return rval;
7950 /* F03:7.4.1.2. */
7951 if (lhs->ts.type == BT_CLASS)
7953 gfc_error ("Variable must not be polymorphic in assignment at %L",
7954 &lhs->where);
7955 return false;
7958 gfc_check_assign (lhs, rhs, 1);
7959 return false;
7963 /* Given a block of code, recursively resolve everything pointed to by this
7964 code block. */
7966 static void
7967 resolve_code (gfc_code *code, gfc_namespace *ns)
7969 int omp_workshare_save;
7970 int forall_save;
7971 code_stack frame;
7972 gfc_try t;
7974 frame.prev = cs_base;
7975 frame.head = code;
7976 cs_base = &frame;
7978 find_reachable_labels (code);
7980 for (; code; code = code->next)
7982 frame.current = code;
7983 forall_save = forall_flag;
7985 if (code->op == EXEC_FORALL)
7987 forall_flag = 1;
7988 gfc_resolve_forall (code, ns, forall_save);
7989 forall_flag = 2;
7991 else if (code->block)
7993 omp_workshare_save = -1;
7994 switch (code->op)
7996 case EXEC_OMP_PARALLEL_WORKSHARE:
7997 omp_workshare_save = omp_workshare_flag;
7998 omp_workshare_flag = 1;
7999 gfc_resolve_omp_parallel_blocks (code, ns);
8000 break;
8001 case EXEC_OMP_PARALLEL:
8002 case EXEC_OMP_PARALLEL_DO:
8003 case EXEC_OMP_PARALLEL_SECTIONS:
8004 case EXEC_OMP_TASK:
8005 omp_workshare_save = omp_workshare_flag;
8006 omp_workshare_flag = 0;
8007 gfc_resolve_omp_parallel_blocks (code, ns);
8008 break;
8009 case EXEC_OMP_DO:
8010 gfc_resolve_omp_do_blocks (code, ns);
8011 break;
8012 case EXEC_OMP_WORKSHARE:
8013 omp_workshare_save = omp_workshare_flag;
8014 omp_workshare_flag = 1;
8015 /* FALLTHROUGH */
8016 default:
8017 gfc_resolve_blocks (code->block, ns);
8018 break;
8021 if (omp_workshare_save != -1)
8022 omp_workshare_flag = omp_workshare_save;
8025 t = SUCCESS;
8026 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
8027 t = gfc_resolve_expr (code->expr1);
8028 forall_flag = forall_save;
8030 if (gfc_resolve_expr (code->expr2) == FAILURE)
8031 t = FAILURE;
8033 if (code->op == EXEC_ALLOCATE
8034 && gfc_resolve_expr (code->expr3) == FAILURE)
8035 t = FAILURE;
8037 switch (code->op)
8039 case EXEC_NOP:
8040 case EXEC_END_BLOCK:
8041 case EXEC_CYCLE:
8042 case EXEC_PAUSE:
8043 case EXEC_STOP:
8044 case EXEC_EXIT:
8045 case EXEC_CONTINUE:
8046 case EXEC_DT_END:
8047 case EXEC_ASSIGN_CALL:
8048 break;
8050 case EXEC_ENTRY:
8051 /* Keep track of which entry we are up to. */
8052 current_entry_id = code->ext.entry->id;
8053 break;
8055 case EXEC_WHERE:
8056 resolve_where (code, NULL);
8057 break;
8059 case EXEC_GOTO:
8060 if (code->expr1 != NULL)
8062 if (code->expr1->ts.type != BT_INTEGER)
8063 gfc_error ("ASSIGNED GOTO statement at %L requires an "
8064 "INTEGER variable", &code->expr1->where);
8065 else if (code->expr1->symtree->n.sym->attr.assign != 1)
8066 gfc_error ("Variable '%s' has not been assigned a target "
8067 "label at %L", code->expr1->symtree->n.sym->name,
8068 &code->expr1->where);
8070 else
8071 resolve_branch (code->label1, code);
8072 break;
8074 case EXEC_RETURN:
8075 if (code->expr1 != NULL
8076 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
8077 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
8078 "INTEGER return specifier", &code->expr1->where);
8079 break;
8081 case EXEC_INIT_ASSIGN:
8082 case EXEC_END_PROCEDURE:
8083 break;
8085 case EXEC_ASSIGN:
8086 if (t == FAILURE)
8087 break;
8089 if (resolve_ordinary_assign (code, ns))
8091 if (code->op == EXEC_COMPCALL)
8092 goto compcall;
8093 else
8094 goto call;
8096 break;
8098 case EXEC_LABEL_ASSIGN:
8099 if (code->label1->defined == ST_LABEL_UNKNOWN)
8100 gfc_error ("Label %d referenced at %L is never defined",
8101 code->label1->value, &code->label1->where);
8102 if (t == SUCCESS
8103 && (code->expr1->expr_type != EXPR_VARIABLE
8104 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
8105 || code->expr1->symtree->n.sym->ts.kind
8106 != gfc_default_integer_kind
8107 || code->expr1->symtree->n.sym->as != NULL))
8108 gfc_error ("ASSIGN statement at %L requires a scalar "
8109 "default INTEGER variable", &code->expr1->where);
8110 break;
8112 case EXEC_POINTER_ASSIGN:
8113 if (t == FAILURE)
8114 break;
8116 gfc_check_pointer_assign (code->expr1, code->expr2);
8117 break;
8119 case EXEC_ARITHMETIC_IF:
8120 if (t == SUCCESS
8121 && code->expr1->ts.type != BT_INTEGER
8122 && code->expr1->ts.type != BT_REAL)
8123 gfc_error ("Arithmetic IF statement at %L requires a numeric "
8124 "expression", &code->expr1->where);
8126 resolve_branch (code->label1, code);
8127 resolve_branch (code->label2, code);
8128 resolve_branch (code->label3, code);
8129 break;
8131 case EXEC_IF:
8132 if (t == SUCCESS && code->expr1 != NULL
8133 && (code->expr1->ts.type != BT_LOGICAL
8134 || code->expr1->rank != 0))
8135 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8136 &code->expr1->where);
8137 break;
8139 case EXEC_CALL:
8140 call:
8141 resolve_call (code);
8142 break;
8144 case EXEC_COMPCALL:
8145 compcall:
8146 if (code->expr1->symtree
8147 && code->expr1->symtree->n.sym->ts.type == BT_CLASS)
8148 resolve_class_typebound_call (code);
8149 else
8150 resolve_typebound_call (code);
8151 break;
8153 case EXEC_CALL_PPC:
8154 resolve_ppc_call (code);
8155 break;
8157 case EXEC_SELECT:
8158 /* Select is complicated. Also, a SELECT construct could be
8159 a transformed computed GOTO. */
8160 resolve_select (code);
8161 break;
8163 case EXEC_SELECT_TYPE:
8164 resolve_select_type (code);
8165 break;
8167 case EXEC_BLOCK:
8168 gfc_resolve (code->ext.ns);
8169 break;
8171 case EXEC_DO:
8172 if (code->ext.iterator != NULL)
8174 gfc_iterator *iter = code->ext.iterator;
8175 if (gfc_resolve_iterator (iter, true) != FAILURE)
8176 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
8178 break;
8180 case EXEC_DO_WHILE:
8181 if (code->expr1 == NULL)
8182 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
8183 if (t == SUCCESS
8184 && (code->expr1->rank != 0
8185 || code->expr1->ts.type != BT_LOGICAL))
8186 gfc_error ("Exit condition of DO WHILE loop at %L must be "
8187 "a scalar LOGICAL expression", &code->expr1->where);
8188 break;
8190 case EXEC_ALLOCATE:
8191 if (t == SUCCESS)
8192 resolve_allocate_deallocate (code, "ALLOCATE");
8194 break;
8196 case EXEC_DEALLOCATE:
8197 if (t == SUCCESS)
8198 resolve_allocate_deallocate (code, "DEALLOCATE");
8200 break;
8202 case EXEC_OPEN:
8203 if (gfc_resolve_open (code->ext.open) == FAILURE)
8204 break;
8206 resolve_branch (code->ext.open->err, code);
8207 break;
8209 case EXEC_CLOSE:
8210 if (gfc_resolve_close (code->ext.close) == FAILURE)
8211 break;
8213 resolve_branch (code->ext.close->err, code);
8214 break;
8216 case EXEC_BACKSPACE:
8217 case EXEC_ENDFILE:
8218 case EXEC_REWIND:
8219 case EXEC_FLUSH:
8220 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
8221 break;
8223 resolve_branch (code->ext.filepos->err, code);
8224 break;
8226 case EXEC_INQUIRE:
8227 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8228 break;
8230 resolve_branch (code->ext.inquire->err, code);
8231 break;
8233 case EXEC_IOLENGTH:
8234 gcc_assert (code->ext.inquire != NULL);
8235 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8236 break;
8238 resolve_branch (code->ext.inquire->err, code);
8239 break;
8241 case EXEC_WAIT:
8242 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
8243 break;
8245 resolve_branch (code->ext.wait->err, code);
8246 resolve_branch (code->ext.wait->end, code);
8247 resolve_branch (code->ext.wait->eor, code);
8248 break;
8250 case EXEC_READ:
8251 case EXEC_WRITE:
8252 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
8253 break;
8255 resolve_branch (code->ext.dt->err, code);
8256 resolve_branch (code->ext.dt->end, code);
8257 resolve_branch (code->ext.dt->eor, code);
8258 break;
8260 case EXEC_TRANSFER:
8261 resolve_transfer (code);
8262 break;
8264 case EXEC_FORALL:
8265 resolve_forall_iterators (code->ext.forall_iterator);
8267 if (code->expr1 != NULL && code->expr1->ts.type != BT_LOGICAL)
8268 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
8269 "expression", &code->expr1->where);
8270 break;
8272 case EXEC_OMP_ATOMIC:
8273 case EXEC_OMP_BARRIER:
8274 case EXEC_OMP_CRITICAL:
8275 case EXEC_OMP_FLUSH:
8276 case EXEC_OMP_DO:
8277 case EXEC_OMP_MASTER:
8278 case EXEC_OMP_ORDERED:
8279 case EXEC_OMP_SECTIONS:
8280 case EXEC_OMP_SINGLE:
8281 case EXEC_OMP_TASKWAIT:
8282 case EXEC_OMP_WORKSHARE:
8283 gfc_resolve_omp_directive (code, ns);
8284 break;
8286 case EXEC_OMP_PARALLEL:
8287 case EXEC_OMP_PARALLEL_DO:
8288 case EXEC_OMP_PARALLEL_SECTIONS:
8289 case EXEC_OMP_PARALLEL_WORKSHARE:
8290 case EXEC_OMP_TASK:
8291 omp_workshare_save = omp_workshare_flag;
8292 omp_workshare_flag = 0;
8293 gfc_resolve_omp_directive (code, ns);
8294 omp_workshare_flag = omp_workshare_save;
8295 break;
8297 default:
8298 gfc_internal_error ("resolve_code(): Bad statement code");
8302 cs_base = frame.prev;
8306 /* Resolve initial values and make sure they are compatible with
8307 the variable. */
8309 static void
8310 resolve_values (gfc_symbol *sym)
8312 if (sym->value == NULL)
8313 return;
8315 if (gfc_resolve_expr (sym->value) == FAILURE)
8316 return;
8318 gfc_check_assign_symbol (sym, sym->value);
8322 /* Verify the binding labels for common blocks that are BIND(C). The label
8323 for a BIND(C) common block must be identical in all scoping units in which
8324 the common block is declared. Further, the binding label can not collide
8325 with any other global entity in the program. */
8327 static void
8328 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
8330 if (comm_block_tree->n.common->is_bind_c == 1)
8332 gfc_gsymbol *binding_label_gsym;
8333 gfc_gsymbol *comm_name_gsym;
8335 /* See if a global symbol exists by the common block's name. It may
8336 be NULL if the common block is use-associated. */
8337 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
8338 comm_block_tree->n.common->name);
8339 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
8340 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
8341 "with the global entity '%s' at %L",
8342 comm_block_tree->n.common->binding_label,
8343 comm_block_tree->n.common->name,
8344 &(comm_block_tree->n.common->where),
8345 comm_name_gsym->name, &(comm_name_gsym->where));
8346 else if (comm_name_gsym != NULL
8347 && strcmp (comm_name_gsym->name,
8348 comm_block_tree->n.common->name) == 0)
8350 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
8351 as expected. */
8352 if (comm_name_gsym->binding_label == NULL)
8353 /* No binding label for common block stored yet; save this one. */
8354 comm_name_gsym->binding_label =
8355 comm_block_tree->n.common->binding_label;
8356 else
8357 if (strcmp (comm_name_gsym->binding_label,
8358 comm_block_tree->n.common->binding_label) != 0)
8360 /* Common block names match but binding labels do not. */
8361 gfc_error ("Binding label '%s' for common block '%s' at %L "
8362 "does not match the binding label '%s' for common "
8363 "block '%s' at %L",
8364 comm_block_tree->n.common->binding_label,
8365 comm_block_tree->n.common->name,
8366 &(comm_block_tree->n.common->where),
8367 comm_name_gsym->binding_label,
8368 comm_name_gsym->name,
8369 &(comm_name_gsym->where));
8370 return;
8374 /* There is no binding label (NAME="") so we have nothing further to
8375 check and nothing to add as a global symbol for the label. */
8376 if (comm_block_tree->n.common->binding_label[0] == '\0' )
8377 return;
8379 binding_label_gsym =
8380 gfc_find_gsymbol (gfc_gsym_root,
8381 comm_block_tree->n.common->binding_label);
8382 if (binding_label_gsym == NULL)
8384 /* Need to make a global symbol for the binding label to prevent
8385 it from colliding with another. */
8386 binding_label_gsym =
8387 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
8388 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
8389 binding_label_gsym->type = GSYM_COMMON;
8391 else
8393 /* If comm_name_gsym is NULL, the name common block is use
8394 associated and the name could be colliding. */
8395 if (binding_label_gsym->type != GSYM_COMMON)
8396 gfc_error ("Binding label '%s' for common block '%s' at %L "
8397 "collides with the global entity '%s' at %L",
8398 comm_block_tree->n.common->binding_label,
8399 comm_block_tree->n.common->name,
8400 &(comm_block_tree->n.common->where),
8401 binding_label_gsym->name,
8402 &(binding_label_gsym->where));
8403 else if (comm_name_gsym != NULL
8404 && (strcmp (binding_label_gsym->name,
8405 comm_name_gsym->binding_label) != 0)
8406 && (strcmp (binding_label_gsym->sym_name,
8407 comm_name_gsym->name) != 0))
8408 gfc_error ("Binding label '%s' for common block '%s' at %L "
8409 "collides with global entity '%s' at %L",
8410 binding_label_gsym->name, binding_label_gsym->sym_name,
8411 &(comm_block_tree->n.common->where),
8412 comm_name_gsym->name, &(comm_name_gsym->where));
8416 return;
8420 /* Verify any BIND(C) derived types in the namespace so we can report errors
8421 for them once, rather than for each variable declared of that type. */
8423 static void
8424 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
8426 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
8427 && derived_sym->attr.is_bind_c == 1)
8428 verify_bind_c_derived_type (derived_sym);
8430 return;
8434 /* Verify that any binding labels used in a given namespace do not collide
8435 with the names or binding labels of any global symbols. */
8437 static void
8438 gfc_verify_binding_labels (gfc_symbol *sym)
8440 int has_error = 0;
8442 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
8443 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
8445 gfc_gsymbol *bind_c_sym;
8447 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
8448 if (bind_c_sym != NULL
8449 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
8451 if (sym->attr.if_source == IFSRC_DECL
8452 && (bind_c_sym->type != GSYM_SUBROUTINE
8453 && bind_c_sym->type != GSYM_FUNCTION)
8454 && ((sym->attr.contained == 1
8455 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
8456 || (sym->attr.use_assoc == 1
8457 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
8459 /* Make sure global procedures don't collide with anything. */
8460 gfc_error ("Binding label '%s' at %L collides with the global "
8461 "entity '%s' at %L", sym->binding_label,
8462 &(sym->declared_at), bind_c_sym->name,
8463 &(bind_c_sym->where));
8464 has_error = 1;
8466 else if (sym->attr.contained == 0
8467 && (sym->attr.if_source == IFSRC_IFBODY
8468 && sym->attr.flavor == FL_PROCEDURE)
8469 && (bind_c_sym->sym_name != NULL
8470 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
8472 /* Make sure procedures in interface bodies don't collide. */
8473 gfc_error ("Binding label '%s' in interface body at %L collides "
8474 "with the global entity '%s' at %L",
8475 sym->binding_label,
8476 &(sym->declared_at), bind_c_sym->name,
8477 &(bind_c_sym->where));
8478 has_error = 1;
8480 else if (sym->attr.contained == 0
8481 && sym->attr.if_source == IFSRC_UNKNOWN)
8482 if ((sym->attr.use_assoc && bind_c_sym->mod_name
8483 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
8484 || sym->attr.use_assoc == 0)
8486 gfc_error ("Binding label '%s' at %L collides with global "
8487 "entity '%s' at %L", sym->binding_label,
8488 &(sym->declared_at), bind_c_sym->name,
8489 &(bind_c_sym->where));
8490 has_error = 1;
8493 if (has_error != 0)
8494 /* Clear the binding label to prevent checking multiple times. */
8495 sym->binding_label[0] = '\0';
8497 else if (bind_c_sym == NULL)
8499 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
8500 bind_c_sym->where = sym->declared_at;
8501 bind_c_sym->sym_name = sym->name;
8503 if (sym->attr.use_assoc == 1)
8504 bind_c_sym->mod_name = sym->module;
8505 else
8506 if (sym->ns->proc_name != NULL)
8507 bind_c_sym->mod_name = sym->ns->proc_name->name;
8509 if (sym->attr.contained == 0)
8511 if (sym->attr.subroutine)
8512 bind_c_sym->type = GSYM_SUBROUTINE;
8513 else if (sym->attr.function)
8514 bind_c_sym->type = GSYM_FUNCTION;
8518 return;
8522 /* Resolve an index expression. */
8524 static gfc_try
8525 resolve_index_expr (gfc_expr *e)
8527 if (gfc_resolve_expr (e) == FAILURE)
8528 return FAILURE;
8530 if (gfc_simplify_expr (e, 0) == FAILURE)
8531 return FAILURE;
8533 if (gfc_specification_expr (e) == FAILURE)
8534 return FAILURE;
8536 return SUCCESS;
8539 /* Resolve a charlen structure. */
8541 static gfc_try
8542 resolve_charlen (gfc_charlen *cl)
8544 int i, k;
8546 if (cl->resolved)
8547 return SUCCESS;
8549 cl->resolved = 1;
8551 specification_expr = 1;
8553 if (resolve_index_expr (cl->length) == FAILURE)
8555 specification_expr = 0;
8556 return FAILURE;
8559 /* "If the character length parameter value evaluates to a negative
8560 value, the length of character entities declared is zero." */
8561 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
8563 if (gfc_option.warn_surprising)
8564 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
8565 " the length has been set to zero",
8566 &cl->length->where, i);
8567 gfc_replace_expr (cl->length, gfc_int_expr (0));
8570 /* Check that the character length is not too large. */
8571 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
8572 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
8573 && cl->length->ts.type == BT_INTEGER
8574 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
8576 gfc_error ("String length at %L is too large", &cl->length->where);
8577 return FAILURE;
8580 return SUCCESS;
8584 /* Test for non-constant shape arrays. */
8586 static bool
8587 is_non_constant_shape_array (gfc_symbol *sym)
8589 gfc_expr *e;
8590 int i;
8591 bool not_constant;
8593 not_constant = false;
8594 if (sym->as != NULL)
8596 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
8597 has not been simplified; parameter array references. Do the
8598 simplification now. */
8599 for (i = 0; i < sym->as->rank; i++)
8601 e = sym->as->lower[i];
8602 if (e && (resolve_index_expr (e) == FAILURE
8603 || !gfc_is_constant_expr (e)))
8604 not_constant = true;
8606 e = sym->as->upper[i];
8607 if (e && (resolve_index_expr (e) == FAILURE
8608 || !gfc_is_constant_expr (e)))
8609 not_constant = true;
8612 return not_constant;
8615 /* Given a symbol and an initialization expression, add code to initialize
8616 the symbol to the function entry. */
8617 static void
8618 build_init_assign (gfc_symbol *sym, gfc_expr *init)
8620 gfc_expr *lval;
8621 gfc_code *init_st;
8622 gfc_namespace *ns = sym->ns;
8624 /* Search for the function namespace if this is a contained
8625 function without an explicit result. */
8626 if (sym->attr.function && sym == sym->result
8627 && sym->name != sym->ns->proc_name->name)
8629 ns = ns->contained;
8630 for (;ns; ns = ns->sibling)
8631 if (strcmp (ns->proc_name->name, sym->name) == 0)
8632 break;
8635 if (ns == NULL)
8637 gfc_free_expr (init);
8638 return;
8641 /* Build an l-value expression for the result. */
8642 lval = gfc_lval_expr_from_sym (sym);
8644 /* Add the code at scope entry. */
8645 init_st = gfc_get_code ();
8646 init_st->next = ns->code;
8647 ns->code = init_st;
8649 /* Assign the default initializer to the l-value. */
8650 init_st->loc = sym->declared_at;
8651 init_st->op = EXEC_INIT_ASSIGN;
8652 init_st->expr1 = lval;
8653 init_st->expr2 = init;
8656 /* Assign the default initializer to a derived type variable or result. */
8658 static void
8659 apply_default_init (gfc_symbol *sym)
8661 gfc_expr *init = NULL;
8663 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
8664 return;
8666 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
8667 init = gfc_default_initializer (&sym->ts);
8669 if (init == NULL)
8670 return;
8672 build_init_assign (sym, init);
8675 /* Build an initializer for a local integer, real, complex, logical, or
8676 character variable, based on the command line flags finit-local-zero,
8677 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
8678 null if the symbol should not have a default initialization. */
8679 static gfc_expr *
8680 build_default_init_expr (gfc_symbol *sym)
8682 int char_len;
8683 gfc_expr *init_expr;
8684 int i;
8686 /* These symbols should never have a default initialization. */
8687 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
8688 || sym->attr.external
8689 || sym->attr.dummy
8690 || sym->attr.pointer
8691 || sym->attr.in_equivalence
8692 || sym->attr.in_common
8693 || sym->attr.data
8694 || sym->module
8695 || sym->attr.cray_pointee
8696 || sym->attr.cray_pointer)
8697 return NULL;
8699 /* Now we'll try to build an initializer expression. */
8700 init_expr = gfc_get_expr ();
8701 init_expr->expr_type = EXPR_CONSTANT;
8702 init_expr->ts.type = sym->ts.type;
8703 init_expr->ts.kind = sym->ts.kind;
8704 init_expr->where = sym->declared_at;
8706 /* We will only initialize integers, reals, complex, logicals, and
8707 characters, and only if the corresponding command-line flags
8708 were set. Otherwise, we free init_expr and return null. */
8709 switch (sym->ts.type)
8711 case BT_INTEGER:
8712 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
8713 mpz_init_set_si (init_expr->value.integer,
8714 gfc_option.flag_init_integer_value);
8715 else
8717 gfc_free_expr (init_expr);
8718 init_expr = NULL;
8720 break;
8722 case BT_REAL:
8723 mpfr_init (init_expr->value.real);
8724 switch (gfc_option.flag_init_real)
8726 case GFC_INIT_REAL_SNAN:
8727 init_expr->is_snan = 1;
8728 /* Fall through. */
8729 case GFC_INIT_REAL_NAN:
8730 mpfr_set_nan (init_expr->value.real);
8731 break;
8733 case GFC_INIT_REAL_INF:
8734 mpfr_set_inf (init_expr->value.real, 1);
8735 break;
8737 case GFC_INIT_REAL_NEG_INF:
8738 mpfr_set_inf (init_expr->value.real, -1);
8739 break;
8741 case GFC_INIT_REAL_ZERO:
8742 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
8743 break;
8745 default:
8746 gfc_free_expr (init_expr);
8747 init_expr = NULL;
8748 break;
8750 break;
8752 case BT_COMPLEX:
8753 mpc_init2 (init_expr->value.complex, mpfr_get_default_prec());
8754 switch (gfc_option.flag_init_real)
8756 case GFC_INIT_REAL_SNAN:
8757 init_expr->is_snan = 1;
8758 /* Fall through. */
8759 case GFC_INIT_REAL_NAN:
8760 mpfr_set_nan (mpc_realref (init_expr->value.complex));
8761 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
8762 break;
8764 case GFC_INIT_REAL_INF:
8765 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
8766 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
8767 break;
8769 case GFC_INIT_REAL_NEG_INF:
8770 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
8771 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
8772 break;
8774 case GFC_INIT_REAL_ZERO:
8775 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
8776 break;
8778 default:
8779 gfc_free_expr (init_expr);
8780 init_expr = NULL;
8781 break;
8783 break;
8785 case BT_LOGICAL:
8786 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
8787 init_expr->value.logical = 0;
8788 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
8789 init_expr->value.logical = 1;
8790 else
8792 gfc_free_expr (init_expr);
8793 init_expr = NULL;
8795 break;
8797 case BT_CHARACTER:
8798 /* For characters, the length must be constant in order to
8799 create a default initializer. */
8800 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
8801 && sym->ts.u.cl->length
8802 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8804 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
8805 init_expr->value.character.length = char_len;
8806 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
8807 for (i = 0; i < char_len; i++)
8808 init_expr->value.character.string[i]
8809 = (unsigned char) gfc_option.flag_init_character_value;
8811 else
8813 gfc_free_expr (init_expr);
8814 init_expr = NULL;
8816 break;
8818 default:
8819 gfc_free_expr (init_expr);
8820 init_expr = NULL;
8822 return init_expr;
8825 /* Add an initialization expression to a local variable. */
8826 static void
8827 apply_default_init_local (gfc_symbol *sym)
8829 gfc_expr *init = NULL;
8831 /* The symbol should be a variable or a function return value. */
8832 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
8833 || (sym->attr.function && sym->result != sym))
8834 return;
8836 /* Try to build the initializer expression. If we can't initialize
8837 this symbol, then init will be NULL. */
8838 init = build_default_init_expr (sym);
8839 if (init == NULL)
8840 return;
8842 /* For saved variables, we don't want to add an initializer at
8843 function entry, so we just add a static initializer. */
8844 if (sym->attr.save || sym->ns->save_all
8845 || gfc_option.flag_max_stack_var_size == 0)
8847 /* Don't clobber an existing initializer! */
8848 gcc_assert (sym->value == NULL);
8849 sym->value = init;
8850 return;
8853 build_init_assign (sym, init);
8856 /* Resolution of common features of flavors variable and procedure. */
8858 static gfc_try
8859 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
8861 /* Constraints on deferred shape variable. */
8862 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
8864 if (sym->attr.allocatable)
8866 if (sym->attr.dimension)
8868 gfc_error ("Allocatable array '%s' at %L must have "
8869 "a deferred shape", sym->name, &sym->declared_at);
8870 return FAILURE;
8872 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
8873 "may not be ALLOCATABLE", sym->name,
8874 &sym->declared_at) == FAILURE)
8875 return FAILURE;
8878 if (sym->attr.pointer && sym->attr.dimension)
8880 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
8881 sym->name, &sym->declared_at);
8882 return FAILURE;
8886 else
8888 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
8889 && !sym->attr.dummy && sym->ts.type != BT_CLASS)
8891 gfc_error ("Array '%s' at %L cannot have a deferred shape",
8892 sym->name, &sym->declared_at);
8893 return FAILURE;
8896 return SUCCESS;
8900 /* Additional checks for symbols with flavor variable and derived
8901 type. To be called from resolve_fl_variable. */
8903 static gfc_try
8904 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
8906 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
8908 /* Check to see if a derived type is blocked from being host
8909 associated by the presence of another class I symbol in the same
8910 namespace. 14.6.1.3 of the standard and the discussion on
8911 comp.lang.fortran. */
8912 if (sym->ns != sym->ts.u.derived->ns
8913 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
8915 gfc_symbol *s;
8916 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
8917 if (s && s->attr.flavor != FL_DERIVED)
8919 gfc_error ("The type '%s' cannot be host associated at %L "
8920 "because it is blocked by an incompatible object "
8921 "of the same name declared at %L",
8922 sym->ts.u.derived->name, &sym->declared_at,
8923 &s->declared_at);
8924 return FAILURE;
8928 /* 4th constraint in section 11.3: "If an object of a type for which
8929 component-initialization is specified (R429) appears in the
8930 specification-part of a module and does not have the ALLOCATABLE
8931 or POINTER attribute, the object shall have the SAVE attribute."
8933 The check for initializers is performed with
8934 has_default_initializer because gfc_default_initializer generates
8935 a hidden default for allocatable components. */
8936 if (!(sym->value || no_init_flag) && sym->ns->proc_name
8937 && sym->ns->proc_name->attr.flavor == FL_MODULE
8938 && !sym->ns->save_all && !sym->attr.save
8939 && !sym->attr.pointer && !sym->attr.allocatable
8940 && has_default_initializer (sym->ts.u.derived))
8942 gfc_error("Object '%s' at %L must have the SAVE attribute for "
8943 "default initialization of a component",
8944 sym->name, &sym->declared_at);
8945 return FAILURE;
8948 if (sym->ts.type == BT_CLASS)
8950 /* C502. */
8951 if (!gfc_type_is_extensible (sym->ts.u.derived->components->ts.u.derived))
8953 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
8954 sym->ts.u.derived->components->ts.u.derived->name,
8955 sym->name, &sym->declared_at);
8956 return FAILURE;
8959 /* C509. */
8960 /* Assume that use associated symbols were checked in the module ns. */
8961 if (!sym->attr.class_ok && !sym->attr.use_assoc)
8963 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
8964 "or pointer", sym->name, &sym->declared_at);
8965 return FAILURE;
8969 /* Assign default initializer. */
8970 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
8971 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
8973 sym->value = gfc_default_initializer (&sym->ts);
8976 return SUCCESS;
8980 /* Resolve symbols with flavor variable. */
8982 static gfc_try
8983 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
8985 int no_init_flag, automatic_flag;
8986 gfc_expr *e;
8987 const char *auto_save_msg;
8989 auto_save_msg = "Automatic object '%s' at %L cannot have the "
8990 "SAVE attribute";
8992 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
8993 return FAILURE;
8995 /* Set this flag to check that variables are parameters of all entries.
8996 This check is effected by the call to gfc_resolve_expr through
8997 is_non_constant_shape_array. */
8998 specification_expr = 1;
9000 if (sym->ns->proc_name
9001 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9002 || sym->ns->proc_name->attr.is_main_program)
9003 && !sym->attr.use_assoc
9004 && !sym->attr.allocatable
9005 && !sym->attr.pointer
9006 && is_non_constant_shape_array (sym))
9008 /* The shape of a main program or module array needs to be
9009 constant. */
9010 gfc_error ("The module or main program array '%s' at %L must "
9011 "have constant shape", sym->name, &sym->declared_at);
9012 specification_expr = 0;
9013 return FAILURE;
9016 if (sym->ts.type == BT_CHARACTER)
9018 /* Make sure that character string variables with assumed length are
9019 dummy arguments. */
9020 e = sym->ts.u.cl->length;
9021 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
9023 gfc_error ("Entity with assumed character length at %L must be a "
9024 "dummy argument or a PARAMETER", &sym->declared_at);
9025 return FAILURE;
9028 if (e && sym->attr.save && !gfc_is_constant_expr (e))
9030 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9031 return FAILURE;
9034 if (!gfc_is_constant_expr (e)
9035 && !(e->expr_type == EXPR_VARIABLE
9036 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
9037 && sym->ns->proc_name
9038 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9039 || sym->ns->proc_name->attr.is_main_program)
9040 && !sym->attr.use_assoc)
9042 gfc_error ("'%s' at %L must have constant character length "
9043 "in this context", sym->name, &sym->declared_at);
9044 return FAILURE;
9048 if (sym->value == NULL && sym->attr.referenced)
9049 apply_default_init_local (sym); /* Try to apply a default initialization. */
9051 /* Determine if the symbol may not have an initializer. */
9052 no_init_flag = automatic_flag = 0;
9053 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
9054 || sym->attr.intrinsic || sym->attr.result)
9055 no_init_flag = 1;
9056 else if (sym->attr.dimension && !sym->attr.pointer
9057 && is_non_constant_shape_array (sym))
9059 no_init_flag = automatic_flag = 1;
9061 /* Also, they must not have the SAVE attribute.
9062 SAVE_IMPLICIT is checked below. */
9063 if (sym->attr.save == SAVE_EXPLICIT)
9065 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9066 return FAILURE;
9070 /* Ensure that any initializer is simplified. */
9071 if (sym->value)
9072 gfc_simplify_expr (sym->value, 1);
9074 /* Reject illegal initializers. */
9075 if (!sym->mark && sym->value)
9077 if (sym->attr.allocatable)
9078 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
9079 sym->name, &sym->declared_at);
9080 else if (sym->attr.external)
9081 gfc_error ("External '%s' at %L cannot have an initializer",
9082 sym->name, &sym->declared_at);
9083 else if (sym->attr.dummy
9084 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
9085 gfc_error ("Dummy '%s' at %L cannot have an initializer",
9086 sym->name, &sym->declared_at);
9087 else if (sym->attr.intrinsic)
9088 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
9089 sym->name, &sym->declared_at);
9090 else if (sym->attr.result)
9091 gfc_error ("Function result '%s' at %L cannot have an initializer",
9092 sym->name, &sym->declared_at);
9093 else if (automatic_flag)
9094 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
9095 sym->name, &sym->declared_at);
9096 else
9097 goto no_init_error;
9098 return FAILURE;
9101 no_init_error:
9102 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
9103 return resolve_fl_variable_derived (sym, no_init_flag);
9105 return SUCCESS;
9109 /* Resolve a procedure. */
9111 static gfc_try
9112 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
9114 gfc_formal_arglist *arg;
9116 if (sym->attr.function
9117 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9118 return FAILURE;
9120 if (sym->ts.type == BT_CHARACTER)
9122 gfc_charlen *cl = sym->ts.u.cl;
9124 if (cl && cl->length && gfc_is_constant_expr (cl->length)
9125 && resolve_charlen (cl) == FAILURE)
9126 return FAILURE;
9128 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
9129 && sym->attr.proc == PROC_ST_FUNCTION)
9131 gfc_error ("Character-valued statement function '%s' at %L must "
9132 "have constant length", sym->name, &sym->declared_at);
9133 return FAILURE;
9137 /* Ensure that derived type for are not of a private type. Internal
9138 module procedures are excluded by 2.2.3.3 - i.e., they are not
9139 externally accessible and can access all the objects accessible in
9140 the host. */
9141 if (!(sym->ns->parent
9142 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
9143 && gfc_check_access(sym->attr.access, sym->ns->default_access))
9145 gfc_interface *iface;
9147 for (arg = sym->formal; arg; arg = arg->next)
9149 if (arg->sym
9150 && arg->sym->ts.type == BT_DERIVED
9151 && !arg->sym->ts.u.derived->attr.use_assoc
9152 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9153 arg->sym->ts.u.derived->ns->default_access)
9154 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
9155 "PRIVATE type and cannot be a dummy argument"
9156 " of '%s', which is PUBLIC at %L",
9157 arg->sym->name, sym->name, &sym->declared_at)
9158 == FAILURE)
9160 /* Stop this message from recurring. */
9161 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9162 return FAILURE;
9166 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9167 PRIVATE to the containing module. */
9168 for (iface = sym->generic; iface; iface = iface->next)
9170 for (arg = iface->sym->formal; arg; arg = arg->next)
9172 if (arg->sym
9173 && arg->sym->ts.type == BT_DERIVED
9174 && !arg->sym->ts.u.derived->attr.use_assoc
9175 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9176 arg->sym->ts.u.derived->ns->default_access)
9177 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9178 "'%s' in PUBLIC interface '%s' at %L "
9179 "takes dummy arguments of '%s' which is "
9180 "PRIVATE", iface->sym->name, sym->name,
9181 &iface->sym->declared_at,
9182 gfc_typename (&arg->sym->ts)) == FAILURE)
9184 /* Stop this message from recurring. */
9185 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9186 return FAILURE;
9191 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9192 PRIVATE to the containing module. */
9193 for (iface = sym->generic; iface; iface = iface->next)
9195 for (arg = iface->sym->formal; arg; arg = arg->next)
9197 if (arg->sym
9198 && arg->sym->ts.type == BT_DERIVED
9199 && !arg->sym->ts.u.derived->attr.use_assoc
9200 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9201 arg->sym->ts.u.derived->ns->default_access)
9202 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9203 "'%s' in PUBLIC interface '%s' at %L "
9204 "takes dummy arguments of '%s' which is "
9205 "PRIVATE", iface->sym->name, sym->name,
9206 &iface->sym->declared_at,
9207 gfc_typename (&arg->sym->ts)) == FAILURE)
9209 /* Stop this message from recurring. */
9210 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9211 return FAILURE;
9217 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
9218 && !sym->attr.proc_pointer)
9220 gfc_error ("Function '%s' at %L cannot have an initializer",
9221 sym->name, &sym->declared_at);
9222 return FAILURE;
9225 /* An external symbol may not have an initializer because it is taken to be
9226 a procedure. Exception: Procedure Pointers. */
9227 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
9229 gfc_error ("External object '%s' at %L may not have an initializer",
9230 sym->name, &sym->declared_at);
9231 return FAILURE;
9234 /* An elemental function is required to return a scalar 12.7.1 */
9235 if (sym->attr.elemental && sym->attr.function && sym->as)
9237 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
9238 "result", sym->name, &sym->declared_at);
9239 /* Reset so that the error only occurs once. */
9240 sym->attr.elemental = 0;
9241 return FAILURE;
9244 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
9245 char-len-param shall not be array-valued, pointer-valued, recursive
9246 or pure. ....snip... A character value of * may only be used in the
9247 following ways: (i) Dummy arg of procedure - dummy associates with
9248 actual length; (ii) To declare a named constant; or (iii) External
9249 function - but length must be declared in calling scoping unit. */
9250 if (sym->attr.function
9251 && sym->ts.type == BT_CHARACTER
9252 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
9254 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
9255 || (sym->attr.recursive) || (sym->attr.pure))
9257 if (sym->as && sym->as->rank)
9258 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9259 "array-valued", sym->name, &sym->declared_at);
9261 if (sym->attr.pointer)
9262 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9263 "pointer-valued", sym->name, &sym->declared_at);
9265 if (sym->attr.pure)
9266 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9267 "pure", sym->name, &sym->declared_at);
9269 if (sym->attr.recursive)
9270 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9271 "recursive", sym->name, &sym->declared_at);
9273 return FAILURE;
9276 /* Appendix B.2 of the standard. Contained functions give an
9277 error anyway. Fixed-form is likely to be F77/legacy. */
9278 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
9279 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
9280 "CHARACTER(*) function '%s' at %L",
9281 sym->name, &sym->declared_at);
9284 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
9286 gfc_formal_arglist *curr_arg;
9287 int has_non_interop_arg = 0;
9289 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
9290 sym->common_block) == FAILURE)
9292 /* Clear these to prevent looking at them again if there was an
9293 error. */
9294 sym->attr.is_bind_c = 0;
9295 sym->attr.is_c_interop = 0;
9296 sym->ts.is_c_interop = 0;
9298 else
9300 /* So far, no errors have been found. */
9301 sym->attr.is_c_interop = 1;
9302 sym->ts.is_c_interop = 1;
9305 curr_arg = sym->formal;
9306 while (curr_arg != NULL)
9308 /* Skip implicitly typed dummy args here. */
9309 if (curr_arg->sym->attr.implicit_type == 0)
9310 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
9311 /* If something is found to fail, record the fact so we
9312 can mark the symbol for the procedure as not being
9313 BIND(C) to try and prevent multiple errors being
9314 reported. */
9315 has_non_interop_arg = 1;
9317 curr_arg = curr_arg->next;
9320 /* See if any of the arguments were not interoperable and if so, clear
9321 the procedure symbol to prevent duplicate error messages. */
9322 if (has_non_interop_arg != 0)
9324 sym->attr.is_c_interop = 0;
9325 sym->ts.is_c_interop = 0;
9326 sym->attr.is_bind_c = 0;
9330 if (!sym->attr.proc_pointer)
9332 if (sym->attr.save == SAVE_EXPLICIT)
9334 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
9335 "in '%s' at %L", sym->name, &sym->declared_at);
9336 return FAILURE;
9338 if (sym->attr.intent)
9340 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
9341 "in '%s' at %L", sym->name, &sym->declared_at);
9342 return FAILURE;
9344 if (sym->attr.subroutine && sym->attr.result)
9346 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
9347 "in '%s' at %L", sym->name, &sym->declared_at);
9348 return FAILURE;
9350 if (sym->attr.external && sym->attr.function
9351 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
9352 || sym->attr.contained))
9354 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
9355 "in '%s' at %L", sym->name, &sym->declared_at);
9356 return FAILURE;
9358 if (strcmp ("ppr@", sym->name) == 0)
9360 gfc_error ("Procedure pointer result '%s' at %L "
9361 "is missing the pointer attribute",
9362 sym->ns->proc_name->name, &sym->declared_at);
9363 return FAILURE;
9367 return SUCCESS;
9371 /* Resolve a list of finalizer procedures. That is, after they have hopefully
9372 been defined and we now know their defined arguments, check that they fulfill
9373 the requirements of the standard for procedures used as finalizers. */
9375 static gfc_try
9376 gfc_resolve_finalizers (gfc_symbol* derived)
9378 gfc_finalizer* list;
9379 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
9380 gfc_try result = SUCCESS;
9381 bool seen_scalar = false;
9383 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
9384 return SUCCESS;
9386 /* Walk over the list of finalizer-procedures, check them, and if any one
9387 does not fit in with the standard's definition, print an error and remove
9388 it from the list. */
9389 prev_link = &derived->f2k_derived->finalizers;
9390 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
9392 gfc_symbol* arg;
9393 gfc_finalizer* i;
9394 int my_rank;
9396 /* Skip this finalizer if we already resolved it. */
9397 if (list->proc_tree)
9399 prev_link = &(list->next);
9400 continue;
9403 /* Check this exists and is a SUBROUTINE. */
9404 if (!list->proc_sym->attr.subroutine)
9406 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
9407 list->proc_sym->name, &list->where);
9408 goto error;
9411 /* We should have exactly one argument. */
9412 if (!list->proc_sym->formal || list->proc_sym->formal->next)
9414 gfc_error ("FINAL procedure at %L must have exactly one argument",
9415 &list->where);
9416 goto error;
9418 arg = list->proc_sym->formal->sym;
9420 /* This argument must be of our type. */
9421 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
9423 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
9424 &arg->declared_at, derived->name);
9425 goto error;
9428 /* It must neither be a pointer nor allocatable nor optional. */
9429 if (arg->attr.pointer)
9431 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
9432 &arg->declared_at);
9433 goto error;
9435 if (arg->attr.allocatable)
9437 gfc_error ("Argument of FINAL procedure at %L must not be"
9438 " ALLOCATABLE", &arg->declared_at);
9439 goto error;
9441 if (arg->attr.optional)
9443 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
9444 &arg->declared_at);
9445 goto error;
9448 /* It must not be INTENT(OUT). */
9449 if (arg->attr.intent == INTENT_OUT)
9451 gfc_error ("Argument of FINAL procedure at %L must not be"
9452 " INTENT(OUT)", &arg->declared_at);
9453 goto error;
9456 /* Warn if the procedure is non-scalar and not assumed shape. */
9457 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
9458 && arg->as->type != AS_ASSUMED_SHAPE)
9459 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
9460 " shape argument", &arg->declared_at);
9462 /* Check that it does not match in kind and rank with a FINAL procedure
9463 defined earlier. To really loop over the *earlier* declarations,
9464 we need to walk the tail of the list as new ones were pushed at the
9465 front. */
9466 /* TODO: Handle kind parameters once they are implemented. */
9467 my_rank = (arg->as ? arg->as->rank : 0);
9468 for (i = list->next; i; i = i->next)
9470 /* Argument list might be empty; that is an error signalled earlier,
9471 but we nevertheless continued resolving. */
9472 if (i->proc_sym->formal)
9474 gfc_symbol* i_arg = i->proc_sym->formal->sym;
9475 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
9476 if (i_rank == my_rank)
9478 gfc_error ("FINAL procedure '%s' declared at %L has the same"
9479 " rank (%d) as '%s'",
9480 list->proc_sym->name, &list->where, my_rank,
9481 i->proc_sym->name);
9482 goto error;
9487 /* Is this the/a scalar finalizer procedure? */
9488 if (!arg->as || arg->as->rank == 0)
9489 seen_scalar = true;
9491 /* Find the symtree for this procedure. */
9492 gcc_assert (!list->proc_tree);
9493 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
9495 prev_link = &list->next;
9496 continue;
9498 /* Remove wrong nodes immediately from the list so we don't risk any
9499 troubles in the future when they might fail later expectations. */
9500 error:
9501 result = FAILURE;
9502 i = list;
9503 *prev_link = list->next;
9504 gfc_free_finalizer (i);
9507 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
9508 were nodes in the list, must have been for arrays. It is surely a good
9509 idea to have a scalar version there if there's something to finalize. */
9510 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
9511 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
9512 " defined at %L, suggest also scalar one",
9513 derived->name, &derived->declared_at);
9515 /* TODO: Remove this error when finalization is finished. */
9516 gfc_error ("Finalization at %L is not yet implemented",
9517 &derived->declared_at);
9519 return result;
9523 /* Check that it is ok for the typebound procedure proc to override the
9524 procedure old. */
9526 static gfc_try
9527 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
9529 locus where;
9530 const gfc_symbol* proc_target;
9531 const gfc_symbol* old_target;
9532 unsigned proc_pass_arg, old_pass_arg, argpos;
9533 gfc_formal_arglist* proc_formal;
9534 gfc_formal_arglist* old_formal;
9536 /* This procedure should only be called for non-GENERIC proc. */
9537 gcc_assert (!proc->n.tb->is_generic);
9539 /* If the overwritten procedure is GENERIC, this is an error. */
9540 if (old->n.tb->is_generic)
9542 gfc_error ("Can't overwrite GENERIC '%s' at %L",
9543 old->name, &proc->n.tb->where);
9544 return FAILURE;
9547 where = proc->n.tb->where;
9548 proc_target = proc->n.tb->u.specific->n.sym;
9549 old_target = old->n.tb->u.specific->n.sym;
9551 /* Check that overridden binding is not NON_OVERRIDABLE. */
9552 if (old->n.tb->non_overridable)
9554 gfc_error ("'%s' at %L overrides a procedure binding declared"
9555 " NON_OVERRIDABLE", proc->name, &where);
9556 return FAILURE;
9559 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
9560 if (!old->n.tb->deferred && proc->n.tb->deferred)
9562 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
9563 " non-DEFERRED binding", proc->name, &where);
9564 return FAILURE;
9567 /* If the overridden binding is PURE, the overriding must be, too. */
9568 if (old_target->attr.pure && !proc_target->attr.pure)
9570 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
9571 proc->name, &where);
9572 return FAILURE;
9575 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
9576 is not, the overriding must not be either. */
9577 if (old_target->attr.elemental && !proc_target->attr.elemental)
9579 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
9580 " ELEMENTAL", proc->name, &where);
9581 return FAILURE;
9583 if (!old_target->attr.elemental && proc_target->attr.elemental)
9585 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
9586 " be ELEMENTAL, either", proc->name, &where);
9587 return FAILURE;
9590 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
9591 SUBROUTINE. */
9592 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
9594 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
9595 " SUBROUTINE", proc->name, &where);
9596 return FAILURE;
9599 /* If the overridden binding is a FUNCTION, the overriding must also be a
9600 FUNCTION and have the same characteristics. */
9601 if (old_target->attr.function)
9603 if (!proc_target->attr.function)
9605 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
9606 " FUNCTION", proc->name, &where);
9607 return FAILURE;
9610 /* FIXME: Do more comprehensive checking (including, for instance, the
9611 rank and array-shape). */
9612 gcc_assert (proc_target->result && old_target->result);
9613 if (!gfc_compare_types (&proc_target->result->ts,
9614 &old_target->result->ts))
9616 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
9617 " matching result types", proc->name, &where);
9618 return FAILURE;
9622 /* If the overridden binding is PUBLIC, the overriding one must not be
9623 PRIVATE. */
9624 if (old->n.tb->access == ACCESS_PUBLIC
9625 && proc->n.tb->access == ACCESS_PRIVATE)
9627 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
9628 " PRIVATE", proc->name, &where);
9629 return FAILURE;
9632 /* Compare the formal argument lists of both procedures. This is also abused
9633 to find the position of the passed-object dummy arguments of both
9634 bindings as at least the overridden one might not yet be resolved and we
9635 need those positions in the check below. */
9636 proc_pass_arg = old_pass_arg = 0;
9637 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
9638 proc_pass_arg = 1;
9639 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
9640 old_pass_arg = 1;
9641 argpos = 1;
9642 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
9643 proc_formal && old_formal;
9644 proc_formal = proc_formal->next, old_formal = old_formal->next)
9646 if (proc->n.tb->pass_arg
9647 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
9648 proc_pass_arg = argpos;
9649 if (old->n.tb->pass_arg
9650 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
9651 old_pass_arg = argpos;
9653 /* Check that the names correspond. */
9654 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
9656 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
9657 " to match the corresponding argument of the overridden"
9658 " procedure", proc_formal->sym->name, proc->name, &where,
9659 old_formal->sym->name);
9660 return FAILURE;
9663 /* Check that the types correspond if neither is the passed-object
9664 argument. */
9665 /* FIXME: Do more comprehensive testing here. */
9666 if (proc_pass_arg != argpos && old_pass_arg != argpos
9667 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
9669 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
9670 "in respect to the overridden procedure",
9671 proc_formal->sym->name, proc->name, &where);
9672 return FAILURE;
9675 ++argpos;
9677 if (proc_formal || old_formal)
9679 gfc_error ("'%s' at %L must have the same number of formal arguments as"
9680 " the overridden procedure", proc->name, &where);
9681 return FAILURE;
9684 /* If the overridden binding is NOPASS, the overriding one must also be
9685 NOPASS. */
9686 if (old->n.tb->nopass && !proc->n.tb->nopass)
9688 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
9689 " NOPASS", proc->name, &where);
9690 return FAILURE;
9693 /* If the overridden binding is PASS(x), the overriding one must also be
9694 PASS and the passed-object dummy arguments must correspond. */
9695 if (!old->n.tb->nopass)
9697 if (proc->n.tb->nopass)
9699 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
9700 " PASS", proc->name, &where);
9701 return FAILURE;
9704 if (proc_pass_arg != old_pass_arg)
9706 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
9707 " the same position as the passed-object dummy argument of"
9708 " the overridden procedure", proc->name, &where);
9709 return FAILURE;
9713 return SUCCESS;
9717 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
9719 static gfc_try
9720 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
9721 const char* generic_name, locus where)
9723 gfc_symbol* sym1;
9724 gfc_symbol* sym2;
9726 gcc_assert (t1->specific && t2->specific);
9727 gcc_assert (!t1->specific->is_generic);
9728 gcc_assert (!t2->specific->is_generic);
9730 sym1 = t1->specific->u.specific->n.sym;
9731 sym2 = t2->specific->u.specific->n.sym;
9733 if (sym1 == sym2)
9734 return SUCCESS;
9736 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
9737 if (sym1->attr.subroutine != sym2->attr.subroutine
9738 || sym1->attr.function != sym2->attr.function)
9740 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
9741 " GENERIC '%s' at %L",
9742 sym1->name, sym2->name, generic_name, &where);
9743 return FAILURE;
9746 /* Compare the interfaces. */
9747 if (gfc_compare_interfaces (sym1, sym2, sym2->name, 1, 0, NULL, 0))
9749 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
9750 sym1->name, sym2->name, generic_name, &where);
9751 return FAILURE;
9754 return SUCCESS;
9758 /* Worker function for resolving a generic procedure binding; this is used to
9759 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
9761 The difference between those cases is finding possible inherited bindings
9762 that are overridden, as one has to look for them in tb_sym_root,
9763 tb_uop_root or tb_op, respectively. Thus the caller must already find
9764 the super-type and set p->overridden correctly. */
9766 static gfc_try
9767 resolve_tb_generic_targets (gfc_symbol* super_type,
9768 gfc_typebound_proc* p, const char* name)
9770 gfc_tbp_generic* target;
9771 gfc_symtree* first_target;
9772 gfc_symtree* inherited;
9774 gcc_assert (p && p->is_generic);
9776 /* Try to find the specific bindings for the symtrees in our target-list. */
9777 gcc_assert (p->u.generic);
9778 for (target = p->u.generic; target; target = target->next)
9779 if (!target->specific)
9781 gfc_typebound_proc* overridden_tbp;
9782 gfc_tbp_generic* g;
9783 const char* target_name;
9785 target_name = target->specific_st->name;
9787 /* Defined for this type directly. */
9788 if (target->specific_st->n.tb)
9790 target->specific = target->specific_st->n.tb;
9791 goto specific_found;
9794 /* Look for an inherited specific binding. */
9795 if (super_type)
9797 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
9798 true, NULL);
9800 if (inherited)
9802 gcc_assert (inherited->n.tb);
9803 target->specific = inherited->n.tb;
9804 goto specific_found;
9808 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
9809 " at %L", target_name, name, &p->where);
9810 return FAILURE;
9812 /* Once we've found the specific binding, check it is not ambiguous with
9813 other specifics already found or inherited for the same GENERIC. */
9814 specific_found:
9815 gcc_assert (target->specific);
9817 /* This must really be a specific binding! */
9818 if (target->specific->is_generic)
9820 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
9821 " '%s' is GENERIC, too", name, &p->where, target_name);
9822 return FAILURE;
9825 /* Check those already resolved on this type directly. */
9826 for (g = p->u.generic; g; g = g->next)
9827 if (g != target && g->specific
9828 && check_generic_tbp_ambiguity (target, g, name, p->where)
9829 == FAILURE)
9830 return FAILURE;
9832 /* Check for ambiguity with inherited specific targets. */
9833 for (overridden_tbp = p->overridden; overridden_tbp;
9834 overridden_tbp = overridden_tbp->overridden)
9835 if (overridden_tbp->is_generic)
9837 for (g = overridden_tbp->u.generic; g; g = g->next)
9839 gcc_assert (g->specific);
9840 if (check_generic_tbp_ambiguity (target, g,
9841 name, p->where) == FAILURE)
9842 return FAILURE;
9847 /* If we attempt to "overwrite" a specific binding, this is an error. */
9848 if (p->overridden && !p->overridden->is_generic)
9850 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
9851 " the same name", name, &p->where);
9852 return FAILURE;
9855 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
9856 all must have the same attributes here. */
9857 first_target = p->u.generic->specific->u.specific;
9858 gcc_assert (first_target);
9859 p->subroutine = first_target->n.sym->attr.subroutine;
9860 p->function = first_target->n.sym->attr.function;
9862 return SUCCESS;
9866 /* Resolve a GENERIC procedure binding for a derived type. */
9868 static gfc_try
9869 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
9871 gfc_symbol* super_type;
9873 /* Find the overridden binding if any. */
9874 st->n.tb->overridden = NULL;
9875 super_type = gfc_get_derived_super_type (derived);
9876 if (super_type)
9878 gfc_symtree* overridden;
9879 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
9880 true, NULL);
9882 if (overridden && overridden->n.tb)
9883 st->n.tb->overridden = overridden->n.tb;
9886 /* Resolve using worker function. */
9887 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
9891 /* Retrieve the target-procedure of an operator binding and do some checks in
9892 common for intrinsic and user-defined type-bound operators. */
9894 static gfc_symbol*
9895 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
9897 gfc_symbol* target_proc;
9899 gcc_assert (target->specific && !target->specific->is_generic);
9900 target_proc = target->specific->u.specific->n.sym;
9901 gcc_assert (target_proc);
9903 /* All operator bindings must have a passed-object dummy argument. */
9904 if (target->specific->nopass)
9906 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
9907 return NULL;
9910 return target_proc;
9914 /* Resolve a type-bound intrinsic operator. */
9916 static gfc_try
9917 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
9918 gfc_typebound_proc* p)
9920 gfc_symbol* super_type;
9921 gfc_tbp_generic* target;
9923 /* If there's already an error here, do nothing (but don't fail again). */
9924 if (p->error)
9925 return SUCCESS;
9927 /* Operators should always be GENERIC bindings. */
9928 gcc_assert (p->is_generic);
9930 /* Look for an overridden binding. */
9931 super_type = gfc_get_derived_super_type (derived);
9932 if (super_type && super_type->f2k_derived)
9933 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
9934 op, true, NULL);
9935 else
9936 p->overridden = NULL;
9938 /* Resolve general GENERIC properties using worker function. */
9939 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
9940 goto error;
9942 /* Check the targets to be procedures of correct interface. */
9943 for (target = p->u.generic; target; target = target->next)
9945 gfc_symbol* target_proc;
9947 target_proc = get_checked_tb_operator_target (target, p->where);
9948 if (!target_proc)
9949 goto error;
9951 if (!gfc_check_operator_interface (target_proc, op, p->where))
9952 goto error;
9955 return SUCCESS;
9957 error:
9958 p->error = 1;
9959 return FAILURE;
9963 /* Resolve a type-bound user operator (tree-walker callback). */
9965 static gfc_symbol* resolve_bindings_derived;
9966 static gfc_try resolve_bindings_result;
9968 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
9970 static void
9971 resolve_typebound_user_op (gfc_symtree* stree)
9973 gfc_symbol* super_type;
9974 gfc_tbp_generic* target;
9976 gcc_assert (stree && stree->n.tb);
9978 if (stree->n.tb->error)
9979 return;
9981 /* Operators should always be GENERIC bindings. */
9982 gcc_assert (stree->n.tb->is_generic);
9984 /* Find overridden procedure, if any. */
9985 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
9986 if (super_type && super_type->f2k_derived)
9988 gfc_symtree* overridden;
9989 overridden = gfc_find_typebound_user_op (super_type, NULL,
9990 stree->name, true, NULL);
9992 if (overridden && overridden->n.tb)
9993 stree->n.tb->overridden = overridden->n.tb;
9995 else
9996 stree->n.tb->overridden = NULL;
9998 /* Resolve basically using worker function. */
9999 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
10000 == FAILURE)
10001 goto error;
10003 /* Check the targets to be functions of correct interface. */
10004 for (target = stree->n.tb->u.generic; target; target = target->next)
10006 gfc_symbol* target_proc;
10008 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
10009 if (!target_proc)
10010 goto error;
10012 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
10013 goto error;
10016 return;
10018 error:
10019 resolve_bindings_result = FAILURE;
10020 stree->n.tb->error = 1;
10024 /* Resolve the type-bound procedures for a derived type. */
10026 static void
10027 resolve_typebound_procedure (gfc_symtree* stree)
10029 gfc_symbol* proc;
10030 locus where;
10031 gfc_symbol* me_arg;
10032 gfc_symbol* super_type;
10033 gfc_component* comp;
10035 gcc_assert (stree);
10037 /* Undefined specific symbol from GENERIC target definition. */
10038 if (!stree->n.tb)
10039 return;
10041 if (stree->n.tb->error)
10042 return;
10044 /* If this is a GENERIC binding, use that routine. */
10045 if (stree->n.tb->is_generic)
10047 if (resolve_typebound_generic (resolve_bindings_derived, stree)
10048 == FAILURE)
10049 goto error;
10050 return;
10053 /* Get the target-procedure to check it. */
10054 gcc_assert (!stree->n.tb->is_generic);
10055 gcc_assert (stree->n.tb->u.specific);
10056 proc = stree->n.tb->u.specific->n.sym;
10057 where = stree->n.tb->where;
10059 /* Default access should already be resolved from the parser. */
10060 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
10062 /* It should be a module procedure or an external procedure with explicit
10063 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
10064 if ((!proc->attr.subroutine && !proc->attr.function)
10065 || (proc->attr.proc != PROC_MODULE
10066 && proc->attr.if_source != IFSRC_IFBODY)
10067 || (proc->attr.abstract && !stree->n.tb->deferred))
10069 gfc_error ("'%s' must be a module procedure or an external procedure with"
10070 " an explicit interface at %L", proc->name, &where);
10071 goto error;
10073 stree->n.tb->subroutine = proc->attr.subroutine;
10074 stree->n.tb->function = proc->attr.function;
10076 /* Find the super-type of the current derived type. We could do this once and
10077 store in a global if speed is needed, but as long as not I believe this is
10078 more readable and clearer. */
10079 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10081 /* If PASS, resolve and check arguments if not already resolved / loaded
10082 from a .mod file. */
10083 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
10085 if (stree->n.tb->pass_arg)
10087 gfc_formal_arglist* i;
10089 /* If an explicit passing argument name is given, walk the arg-list
10090 and look for it. */
10092 me_arg = NULL;
10093 stree->n.tb->pass_arg_num = 1;
10094 for (i = proc->formal; i; i = i->next)
10096 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
10098 me_arg = i->sym;
10099 break;
10101 ++stree->n.tb->pass_arg_num;
10104 if (!me_arg)
10106 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
10107 " argument '%s'",
10108 proc->name, stree->n.tb->pass_arg, &where,
10109 stree->n.tb->pass_arg);
10110 goto error;
10113 else
10115 /* Otherwise, take the first one; there should in fact be at least
10116 one. */
10117 stree->n.tb->pass_arg_num = 1;
10118 if (!proc->formal)
10120 gfc_error ("Procedure '%s' with PASS at %L must have at"
10121 " least one argument", proc->name, &where);
10122 goto error;
10124 me_arg = proc->formal->sym;
10127 /* Now check that the argument-type matches and the passed-object
10128 dummy argument is generally fine. */
10130 gcc_assert (me_arg);
10132 if (me_arg->ts.type != BT_CLASS)
10134 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10135 " at %L", proc->name, &where);
10136 goto error;
10139 if (me_arg->ts.u.derived->components->ts.u.derived
10140 != resolve_bindings_derived)
10142 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10143 " the derived-type '%s'", me_arg->name, proc->name,
10144 me_arg->name, &where, resolve_bindings_derived->name);
10145 goto error;
10148 gcc_assert (me_arg->ts.type == BT_CLASS);
10149 if (me_arg->ts.u.derived->components->as
10150 && me_arg->ts.u.derived->components->as->rank > 0)
10152 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
10153 " scalar", proc->name, &where);
10154 goto error;
10156 if (me_arg->ts.u.derived->components->attr.allocatable)
10158 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10159 " be ALLOCATABLE", proc->name, &where);
10160 goto error;
10162 if (me_arg->ts.u.derived->components->attr.class_pointer)
10164 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10165 " be POINTER", proc->name, &where);
10166 goto error;
10170 /* If we are extending some type, check that we don't override a procedure
10171 flagged NON_OVERRIDABLE. */
10172 stree->n.tb->overridden = NULL;
10173 if (super_type)
10175 gfc_symtree* overridden;
10176 overridden = gfc_find_typebound_proc (super_type, NULL,
10177 stree->name, true, NULL);
10179 if (overridden && overridden->n.tb)
10180 stree->n.tb->overridden = overridden->n.tb;
10182 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
10183 goto error;
10186 /* See if there's a name collision with a component directly in this type. */
10187 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
10188 if (!strcmp (comp->name, stree->name))
10190 gfc_error ("Procedure '%s' at %L has the same name as a component of"
10191 " '%s'",
10192 stree->name, &where, resolve_bindings_derived->name);
10193 goto error;
10196 /* Try to find a name collision with an inherited component. */
10197 if (super_type && gfc_find_component (super_type, stree->name, true, true))
10199 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
10200 " component of '%s'",
10201 stree->name, &where, resolve_bindings_derived->name);
10202 goto error;
10205 stree->n.tb->error = 0;
10206 return;
10208 error:
10209 resolve_bindings_result = FAILURE;
10210 stree->n.tb->error = 1;
10213 static gfc_try
10214 resolve_typebound_procedures (gfc_symbol* derived)
10216 int op;
10218 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
10219 return SUCCESS;
10221 resolve_bindings_derived = derived;
10222 resolve_bindings_result = SUCCESS;
10224 if (derived->f2k_derived->tb_sym_root)
10225 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
10226 &resolve_typebound_procedure);
10228 if (derived->f2k_derived->tb_uop_root)
10229 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
10230 &resolve_typebound_user_op);
10232 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
10234 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
10235 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
10236 p) == FAILURE)
10237 resolve_bindings_result = FAILURE;
10240 return resolve_bindings_result;
10244 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
10245 to give all identical derived types the same backend_decl. */
10246 static void
10247 add_dt_to_dt_list (gfc_symbol *derived)
10249 gfc_dt_list *dt_list;
10251 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
10252 if (derived == dt_list->derived)
10253 break;
10255 if (dt_list == NULL)
10257 dt_list = gfc_get_dt_list ();
10258 dt_list->next = gfc_derived_types;
10259 dt_list->derived = derived;
10260 gfc_derived_types = dt_list;
10265 /* Ensure that a derived-type is really not abstract, meaning that every
10266 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
10268 static gfc_try
10269 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
10271 if (!st)
10272 return SUCCESS;
10274 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
10275 return FAILURE;
10276 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
10277 return FAILURE;
10279 if (st->n.tb && st->n.tb->deferred)
10281 gfc_symtree* overriding;
10282 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
10283 gcc_assert (overriding && overriding->n.tb);
10284 if (overriding->n.tb->deferred)
10286 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
10287 " '%s' is DEFERRED and not overridden",
10288 sub->name, &sub->declared_at, st->name);
10289 return FAILURE;
10293 return SUCCESS;
10296 static gfc_try
10297 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
10299 /* The algorithm used here is to recursively travel up the ancestry of sub
10300 and for each ancestor-type, check all bindings. If any of them is
10301 DEFERRED, look it up starting from sub and see if the found (overriding)
10302 binding is not DEFERRED.
10303 This is not the most efficient way to do this, but it should be ok and is
10304 clearer than something sophisticated. */
10306 gcc_assert (ancestor && ancestor->attr.abstract && !sub->attr.abstract);
10308 /* Walk bindings of this ancestor. */
10309 if (ancestor->f2k_derived)
10311 gfc_try t;
10312 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
10313 if (t == FAILURE)
10314 return FAILURE;
10317 /* Find next ancestor type and recurse on it. */
10318 ancestor = gfc_get_derived_super_type (ancestor);
10319 if (ancestor)
10320 return ensure_not_abstract (sub, ancestor);
10322 return SUCCESS;
10326 static void resolve_symbol (gfc_symbol *sym);
10329 /* Resolve the components of a derived type. */
10331 static gfc_try
10332 resolve_fl_derived (gfc_symbol *sym)
10334 gfc_symbol* super_type;
10335 gfc_component *c;
10336 int i;
10338 super_type = gfc_get_derived_super_type (sym);
10340 /* Ensure the extended type gets resolved before we do. */
10341 if (super_type && resolve_fl_derived (super_type) == FAILURE)
10342 return FAILURE;
10344 /* An ABSTRACT type must be extensible. */
10345 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
10347 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
10348 sym->name, &sym->declared_at);
10349 return FAILURE;
10352 for (c = sym->components; c != NULL; c = c->next)
10354 if (c->attr.proc_pointer && c->ts.interface)
10356 if (c->ts.interface->attr.procedure)
10357 gfc_error ("Interface '%s', used by procedure pointer component "
10358 "'%s' at %L, is declared in a later PROCEDURE statement",
10359 c->ts.interface->name, c->name, &c->loc);
10361 /* Get the attributes from the interface (now resolved). */
10362 if (c->ts.interface->attr.if_source
10363 || c->ts.interface->attr.intrinsic)
10365 gfc_symbol *ifc = c->ts.interface;
10367 if (ifc->formal && !ifc->formal_ns)
10368 resolve_symbol (ifc);
10370 if (ifc->attr.intrinsic)
10371 resolve_intrinsic (ifc, &ifc->declared_at);
10373 if (ifc->result)
10375 c->ts = ifc->result->ts;
10376 c->attr.allocatable = ifc->result->attr.allocatable;
10377 c->attr.pointer = ifc->result->attr.pointer;
10378 c->attr.dimension = ifc->result->attr.dimension;
10379 c->as = gfc_copy_array_spec (ifc->result->as);
10381 else
10383 c->ts = ifc->ts;
10384 c->attr.allocatable = ifc->attr.allocatable;
10385 c->attr.pointer = ifc->attr.pointer;
10386 c->attr.dimension = ifc->attr.dimension;
10387 c->as = gfc_copy_array_spec (ifc->as);
10389 c->ts.interface = ifc;
10390 c->attr.function = ifc->attr.function;
10391 c->attr.subroutine = ifc->attr.subroutine;
10392 gfc_copy_formal_args_ppc (c, ifc);
10394 c->attr.pure = ifc->attr.pure;
10395 c->attr.elemental = ifc->attr.elemental;
10396 c->attr.recursive = ifc->attr.recursive;
10397 c->attr.always_explicit = ifc->attr.always_explicit;
10398 c->attr.ext_attr |= ifc->attr.ext_attr;
10399 /* Replace symbols in array spec. */
10400 if (c->as)
10402 int i;
10403 for (i = 0; i < c->as->rank; i++)
10405 gfc_expr_replace_comp (c->as->lower[i], c);
10406 gfc_expr_replace_comp (c->as->upper[i], c);
10409 /* Copy char length. */
10410 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
10412 c->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
10413 gfc_expr_replace_comp (c->ts.u.cl->length, c);
10416 else if (c->ts.interface->name[0] != '\0')
10418 gfc_error ("Interface '%s' of procedure pointer component "
10419 "'%s' at %L must be explicit", c->ts.interface->name,
10420 c->name, &c->loc);
10421 return FAILURE;
10424 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
10426 /* Since PPCs are not implicitly typed, a PPC without an explicit
10427 interface must be a subroutine. */
10428 gfc_add_subroutine (&c->attr, c->name, &c->loc);
10431 /* Procedure pointer components: Check PASS arg. */
10432 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0)
10434 gfc_symbol* me_arg;
10436 if (c->tb->pass_arg)
10438 gfc_formal_arglist* i;
10440 /* If an explicit passing argument name is given, walk the arg-list
10441 and look for it. */
10443 me_arg = NULL;
10444 c->tb->pass_arg_num = 1;
10445 for (i = c->formal; i; i = i->next)
10447 if (!strcmp (i->sym->name, c->tb->pass_arg))
10449 me_arg = i->sym;
10450 break;
10452 c->tb->pass_arg_num++;
10455 if (!me_arg)
10457 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
10458 "at %L has no argument '%s'", c->name,
10459 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
10460 c->tb->error = 1;
10461 return FAILURE;
10464 else
10466 /* Otherwise, take the first one; there should in fact be at least
10467 one. */
10468 c->tb->pass_arg_num = 1;
10469 if (!c->formal)
10471 gfc_error ("Procedure pointer component '%s' with PASS at %L "
10472 "must have at least one argument",
10473 c->name, &c->loc);
10474 c->tb->error = 1;
10475 return FAILURE;
10477 me_arg = c->formal->sym;
10480 /* Now check that the argument-type matches. */
10481 gcc_assert (me_arg);
10482 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
10483 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
10484 || (me_arg->ts.type == BT_CLASS
10485 && me_arg->ts.u.derived->components->ts.u.derived != sym))
10487 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10488 " the derived type '%s'", me_arg->name, c->name,
10489 me_arg->name, &c->loc, sym->name);
10490 c->tb->error = 1;
10491 return FAILURE;
10494 /* Check for C453. */
10495 if (me_arg->attr.dimension)
10497 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10498 "must be scalar", me_arg->name, c->name, me_arg->name,
10499 &c->loc);
10500 c->tb->error = 1;
10501 return FAILURE;
10504 if (me_arg->attr.pointer)
10506 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10507 "may not have the POINTER attribute", me_arg->name,
10508 c->name, me_arg->name, &c->loc);
10509 c->tb->error = 1;
10510 return FAILURE;
10513 if (me_arg->attr.allocatable)
10515 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10516 "may not be ALLOCATABLE", me_arg->name, c->name,
10517 me_arg->name, &c->loc);
10518 c->tb->error = 1;
10519 return FAILURE;
10522 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
10523 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10524 " at %L", c->name, &c->loc);
10528 /* Check type-spec if this is not the parent-type component. */
10529 if ((!sym->attr.extension || c != sym->components)
10530 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
10531 return FAILURE;
10533 /* If this type is an extension, set the accessibility of the parent
10534 component. */
10535 if (super_type && c == sym->components
10536 && strcmp (super_type->name, c->name) == 0)
10537 c->attr.access = super_type->attr.access;
10539 /* If this type is an extension, see if this component has the same name
10540 as an inherited type-bound procedure. */
10541 if (super_type
10542 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
10544 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
10545 " inherited type-bound procedure",
10546 c->name, sym->name, &c->loc);
10547 return FAILURE;
10550 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
10552 if (c->ts.u.cl->length == NULL
10553 || (resolve_charlen (c->ts.u.cl) == FAILURE)
10554 || !gfc_is_constant_expr (c->ts.u.cl->length))
10556 gfc_error ("Character length of component '%s' needs to "
10557 "be a constant specification expression at %L",
10558 c->name,
10559 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
10560 return FAILURE;
10564 if (c->ts.type == BT_DERIVED
10565 && sym->component_access != ACCESS_PRIVATE
10566 && gfc_check_access (sym->attr.access, sym->ns->default_access)
10567 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
10568 && !c->ts.u.derived->attr.use_assoc
10569 && !gfc_check_access (c->ts.u.derived->attr.access,
10570 c->ts.u.derived->ns->default_access)
10571 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
10572 "is a PRIVATE type and cannot be a component of "
10573 "'%s', which is PUBLIC at %L", c->name,
10574 sym->name, &sym->declared_at) == FAILURE)
10575 return FAILURE;
10577 if (sym->attr.sequence)
10579 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
10581 gfc_error ("Component %s of SEQUENCE type declared at %L does "
10582 "not have the SEQUENCE attribute",
10583 c->ts.u.derived->name, &sym->declared_at);
10584 return FAILURE;
10588 if (c->ts.type == BT_DERIVED && c->attr.pointer
10589 && c->ts.u.derived->components == NULL
10590 && !c->ts.u.derived->attr.zero_comp)
10592 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
10593 "that has not been declared", c->name, sym->name,
10594 &c->loc);
10595 return FAILURE;
10598 /* C437. */
10599 if (c->ts.type == BT_CLASS
10600 && !(c->ts.u.derived->components->attr.pointer
10601 || c->ts.u.derived->components->attr.allocatable))
10603 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
10604 "or pointer", c->name, &c->loc);
10605 return FAILURE;
10608 /* Ensure that all the derived type components are put on the
10609 derived type list; even in formal namespaces, where derived type
10610 pointer components might not have been declared. */
10611 if (c->ts.type == BT_DERIVED
10612 && c->ts.u.derived
10613 && c->ts.u.derived->components
10614 && c->attr.pointer
10615 && sym != c->ts.u.derived)
10616 add_dt_to_dt_list (c->ts.u.derived);
10618 if (c->attr.pointer || c->attr.proc_pointer || c->attr.allocatable
10619 || c->as == NULL)
10620 continue;
10622 for (i = 0; i < c->as->rank; i++)
10624 if (c->as->lower[i] == NULL
10625 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
10626 || !gfc_is_constant_expr (c->as->lower[i])
10627 || c->as->upper[i] == NULL
10628 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
10629 || !gfc_is_constant_expr (c->as->upper[i]))
10631 gfc_error ("Component '%s' of '%s' at %L must have "
10632 "constant array bounds",
10633 c->name, sym->name, &c->loc);
10634 return FAILURE;
10639 /* Resolve the type-bound procedures. */
10640 if (resolve_typebound_procedures (sym) == FAILURE)
10641 return FAILURE;
10643 /* Resolve the finalizer procedures. */
10644 if (gfc_resolve_finalizers (sym) == FAILURE)
10645 return FAILURE;
10647 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
10648 all DEFERRED bindings are overridden. */
10649 if (super_type && super_type->attr.abstract && !sym->attr.abstract
10650 && ensure_not_abstract (sym, super_type) == FAILURE)
10651 return FAILURE;
10653 /* Add derived type to the derived type list. */
10654 add_dt_to_dt_list (sym);
10656 return SUCCESS;
10660 static gfc_try
10661 resolve_fl_namelist (gfc_symbol *sym)
10663 gfc_namelist *nl;
10664 gfc_symbol *nlsym;
10666 /* Reject PRIVATE objects in a PUBLIC namelist. */
10667 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
10669 for (nl = sym->namelist; nl; nl = nl->next)
10671 if (!nl->sym->attr.use_assoc
10672 && !is_sym_host_assoc (nl->sym, sym->ns)
10673 && !gfc_check_access(nl->sym->attr.access,
10674 nl->sym->ns->default_access))
10676 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
10677 "cannot be member of PUBLIC namelist '%s' at %L",
10678 nl->sym->name, sym->name, &sym->declared_at);
10679 return FAILURE;
10682 /* Types with private components that came here by USE-association. */
10683 if (nl->sym->ts.type == BT_DERIVED
10684 && derived_inaccessible (nl->sym->ts.u.derived))
10686 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
10687 "components and cannot be member of namelist '%s' at %L",
10688 nl->sym->name, sym->name, &sym->declared_at);
10689 return FAILURE;
10692 /* Types with private components that are defined in the same module. */
10693 if (nl->sym->ts.type == BT_DERIVED
10694 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
10695 && !gfc_check_access (nl->sym->ts.u.derived->attr.private_comp
10696 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
10697 nl->sym->ns->default_access))
10699 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
10700 "cannot be a member of PUBLIC namelist '%s' at %L",
10701 nl->sym->name, sym->name, &sym->declared_at);
10702 return FAILURE;
10707 for (nl = sym->namelist; nl; nl = nl->next)
10709 /* Reject namelist arrays of assumed shape. */
10710 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
10711 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
10712 "must not have assumed shape in namelist "
10713 "'%s' at %L", nl->sym->name, sym->name,
10714 &sym->declared_at) == FAILURE)
10715 return FAILURE;
10717 /* Reject namelist arrays that are not constant shape. */
10718 if (is_non_constant_shape_array (nl->sym))
10720 gfc_error ("NAMELIST array object '%s' must have constant "
10721 "shape in namelist '%s' at %L", nl->sym->name,
10722 sym->name, &sym->declared_at);
10723 return FAILURE;
10726 /* Namelist objects cannot have allocatable or pointer components. */
10727 if (nl->sym->ts.type != BT_DERIVED)
10728 continue;
10730 if (nl->sym->ts.u.derived->attr.alloc_comp)
10732 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10733 "have ALLOCATABLE components",
10734 nl->sym->name, sym->name, &sym->declared_at);
10735 return FAILURE;
10738 if (nl->sym->ts.u.derived->attr.pointer_comp)
10740 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10741 "have POINTER components",
10742 nl->sym->name, sym->name, &sym->declared_at);
10743 return FAILURE;
10748 /* 14.1.2 A module or internal procedure represent local entities
10749 of the same type as a namelist member and so are not allowed. */
10750 for (nl = sym->namelist; nl; nl = nl->next)
10752 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
10753 continue;
10755 if (nl->sym->attr.function && nl->sym == nl->sym->result)
10756 if ((nl->sym == sym->ns->proc_name)
10758 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
10759 continue;
10761 nlsym = NULL;
10762 if (nl->sym && nl->sym->name)
10763 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
10764 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
10766 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
10767 "attribute in '%s' at %L", nlsym->name,
10768 &sym->declared_at);
10769 return FAILURE;
10773 return SUCCESS;
10777 static gfc_try
10778 resolve_fl_parameter (gfc_symbol *sym)
10780 /* A parameter array's shape needs to be constant. */
10781 if (sym->as != NULL
10782 && (sym->as->type == AS_DEFERRED
10783 || is_non_constant_shape_array (sym)))
10785 gfc_error ("Parameter array '%s' at %L cannot be automatic "
10786 "or of deferred shape", sym->name, &sym->declared_at);
10787 return FAILURE;
10790 /* Make sure a parameter that has been implicitly typed still
10791 matches the implicit type, since PARAMETER statements can precede
10792 IMPLICIT statements. */
10793 if (sym->attr.implicit_type
10794 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
10795 sym->ns)))
10797 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
10798 "later IMPLICIT type", sym->name, &sym->declared_at);
10799 return FAILURE;
10802 /* Make sure the types of derived parameters are consistent. This
10803 type checking is deferred until resolution because the type may
10804 refer to a derived type from the host. */
10805 if (sym->ts.type == BT_DERIVED
10806 && !gfc_compare_types (&sym->ts, &sym->value->ts))
10808 gfc_error ("Incompatible derived type in PARAMETER at %L",
10809 &sym->value->where);
10810 return FAILURE;
10812 return SUCCESS;
10816 /* Do anything necessary to resolve a symbol. Right now, we just
10817 assume that an otherwise unknown symbol is a variable. This sort
10818 of thing commonly happens for symbols in module. */
10820 static void
10821 resolve_symbol (gfc_symbol *sym)
10823 int check_constant, mp_flag;
10824 gfc_symtree *symtree;
10825 gfc_symtree *this_symtree;
10826 gfc_namespace *ns;
10827 gfc_component *c;
10829 if (sym->attr.flavor == FL_UNKNOWN)
10832 /* If we find that a flavorless symbol is an interface in one of the
10833 parent namespaces, find its symtree in this namespace, free the
10834 symbol and set the symtree to point to the interface symbol. */
10835 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
10837 symtree = gfc_find_symtree (ns->sym_root, sym->name);
10838 if (symtree && symtree->n.sym->generic)
10840 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10841 sym->name);
10842 sym->refs--;
10843 if (!sym->refs)
10844 gfc_free_symbol (sym);
10845 symtree->n.sym->refs++;
10846 this_symtree->n.sym = symtree->n.sym;
10847 return;
10851 /* Otherwise give it a flavor according to such attributes as
10852 it has. */
10853 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
10854 sym->attr.flavor = FL_VARIABLE;
10855 else
10857 sym->attr.flavor = FL_PROCEDURE;
10858 if (sym->attr.dimension)
10859 sym->attr.function = 1;
10863 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
10864 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
10866 if (sym->attr.procedure && sym->ts.interface
10867 && sym->attr.if_source != IFSRC_DECL)
10869 if (sym->ts.interface == sym)
10871 gfc_error ("PROCEDURE '%s' at %L may not be used as its own "
10872 "interface", sym->name, &sym->declared_at);
10873 return;
10875 if (sym->ts.interface->attr.procedure)
10877 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared"
10878 " in a later PROCEDURE statement", sym->ts.interface->name,
10879 sym->name,&sym->declared_at);
10880 return;
10883 /* Get the attributes from the interface (now resolved). */
10884 if (sym->ts.interface->attr.if_source
10885 || sym->ts.interface->attr.intrinsic)
10887 gfc_symbol *ifc = sym->ts.interface;
10888 resolve_symbol (ifc);
10890 if (ifc->attr.intrinsic)
10891 resolve_intrinsic (ifc, &ifc->declared_at);
10893 if (ifc->result)
10894 sym->ts = ifc->result->ts;
10895 else
10896 sym->ts = ifc->ts;
10897 sym->ts.interface = ifc;
10898 sym->attr.function = ifc->attr.function;
10899 sym->attr.subroutine = ifc->attr.subroutine;
10900 gfc_copy_formal_args (sym, ifc);
10902 sym->attr.allocatable = ifc->attr.allocatable;
10903 sym->attr.pointer = ifc->attr.pointer;
10904 sym->attr.pure = ifc->attr.pure;
10905 sym->attr.elemental = ifc->attr.elemental;
10906 sym->attr.dimension = ifc->attr.dimension;
10907 sym->attr.recursive = ifc->attr.recursive;
10908 sym->attr.always_explicit = ifc->attr.always_explicit;
10909 sym->attr.ext_attr |= ifc->attr.ext_attr;
10910 /* Copy array spec. */
10911 sym->as = gfc_copy_array_spec (ifc->as);
10912 if (sym->as)
10914 int i;
10915 for (i = 0; i < sym->as->rank; i++)
10917 gfc_expr_replace_symbols (sym->as->lower[i], sym);
10918 gfc_expr_replace_symbols (sym->as->upper[i], sym);
10921 /* Copy char length. */
10922 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
10924 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
10925 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
10928 else if (sym->ts.interface->name[0] != '\0')
10930 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
10931 sym->ts.interface->name, sym->name, &sym->declared_at);
10932 return;
10936 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
10937 return;
10939 /* Symbols that are module procedures with results (functions) have
10940 the types and array specification copied for type checking in
10941 procedures that call them, as well as for saving to a module
10942 file. These symbols can't stand the scrutiny that their results
10943 can. */
10944 mp_flag = (sym->result != NULL && sym->result != sym);
10947 /* Make sure that the intrinsic is consistent with its internal
10948 representation. This needs to be done before assigning a default
10949 type to avoid spurious warnings. */
10950 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
10951 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
10952 return;
10954 /* Assign default type to symbols that need one and don't have one. */
10955 if (sym->ts.type == BT_UNKNOWN)
10957 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
10958 gfc_set_default_type (sym, 1, NULL);
10960 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
10961 && !sym->attr.function && !sym->attr.subroutine
10962 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
10963 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
10965 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
10967 /* The specific case of an external procedure should emit an error
10968 in the case that there is no implicit type. */
10969 if (!mp_flag)
10970 gfc_set_default_type (sym, sym->attr.external, NULL);
10971 else
10973 /* Result may be in another namespace. */
10974 resolve_symbol (sym->result);
10976 if (!sym->result->attr.proc_pointer)
10978 sym->ts = sym->result->ts;
10979 sym->as = gfc_copy_array_spec (sym->result->as);
10980 sym->attr.dimension = sym->result->attr.dimension;
10981 sym->attr.pointer = sym->result->attr.pointer;
10982 sym->attr.allocatable = sym->result->attr.allocatable;
10988 /* Assumed size arrays and assumed shape arrays must be dummy
10989 arguments. */
10991 if (sym->as != NULL
10992 && (sym->as->type == AS_ASSUMED_SIZE
10993 || sym->as->type == AS_ASSUMED_SHAPE)
10994 && sym->attr.dummy == 0)
10996 if (sym->as->type == AS_ASSUMED_SIZE)
10997 gfc_error ("Assumed size array at %L must be a dummy argument",
10998 &sym->declared_at);
10999 else
11000 gfc_error ("Assumed shape array at %L must be a dummy argument",
11001 &sym->declared_at);
11002 return;
11005 /* Make sure symbols with known intent or optional are really dummy
11006 variable. Because of ENTRY statement, this has to be deferred
11007 until resolution time. */
11009 if (!sym->attr.dummy
11010 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
11012 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
11013 return;
11016 if (sym->attr.value && !sym->attr.dummy)
11018 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
11019 "it is not a dummy argument", sym->name, &sym->declared_at);
11020 return;
11023 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
11025 gfc_charlen *cl = sym->ts.u.cl;
11026 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11028 gfc_error ("Character dummy variable '%s' at %L with VALUE "
11029 "attribute must have constant length",
11030 sym->name, &sym->declared_at);
11031 return;
11034 if (sym->ts.is_c_interop
11035 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
11037 gfc_error ("C interoperable character dummy variable '%s' at %L "
11038 "with VALUE attribute must have length one",
11039 sym->name, &sym->declared_at);
11040 return;
11044 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
11045 do this for something that was implicitly typed because that is handled
11046 in gfc_set_default_type. Handle dummy arguments and procedure
11047 definitions separately. Also, anything that is use associated is not
11048 handled here but instead is handled in the module it is declared in.
11049 Finally, derived type definitions are allowed to be BIND(C) since that
11050 only implies that they're interoperable, and they are checked fully for
11051 interoperability when a variable is declared of that type. */
11052 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
11053 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
11054 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
11056 gfc_try t = SUCCESS;
11058 /* First, make sure the variable is declared at the
11059 module-level scope (J3/04-007, Section 15.3). */
11060 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
11061 sym->attr.in_common == 0)
11063 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
11064 "is neither a COMMON block nor declared at the "
11065 "module level scope", sym->name, &(sym->declared_at));
11066 t = FAILURE;
11068 else if (sym->common_head != NULL)
11070 t = verify_com_block_vars_c_interop (sym->common_head);
11072 else
11074 /* If type() declaration, we need to verify that the components
11075 of the given type are all C interoperable, etc. */
11076 if (sym->ts.type == BT_DERIVED &&
11077 sym->ts.u.derived->attr.is_c_interop != 1)
11079 /* Make sure the user marked the derived type as BIND(C). If
11080 not, call the verify routine. This could print an error
11081 for the derived type more than once if multiple variables
11082 of that type are declared. */
11083 if (sym->ts.u.derived->attr.is_bind_c != 1)
11084 verify_bind_c_derived_type (sym->ts.u.derived);
11085 t = FAILURE;
11088 /* Verify the variable itself as C interoperable if it
11089 is BIND(C). It is not possible for this to succeed if
11090 the verify_bind_c_derived_type failed, so don't have to handle
11091 any error returned by verify_bind_c_derived_type. */
11092 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11093 sym->common_block);
11096 if (t == FAILURE)
11098 /* clear the is_bind_c flag to prevent reporting errors more than
11099 once if something failed. */
11100 sym->attr.is_bind_c = 0;
11101 return;
11105 /* If a derived type symbol has reached this point, without its
11106 type being declared, we have an error. Notice that most
11107 conditions that produce undefined derived types have already
11108 been dealt with. However, the likes of:
11109 implicit type(t) (t) ..... call foo (t) will get us here if
11110 the type is not declared in the scope of the implicit
11111 statement. Change the type to BT_UNKNOWN, both because it is so
11112 and to prevent an ICE. */
11113 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
11114 && !sym->ts.u.derived->attr.zero_comp)
11116 gfc_error ("The derived type '%s' at %L is of type '%s', "
11117 "which has not been defined", sym->name,
11118 &sym->declared_at, sym->ts.u.derived->name);
11119 sym->ts.type = BT_UNKNOWN;
11120 return;
11123 /* Make sure that the derived type has been resolved and that the
11124 derived type is visible in the symbol's namespace, if it is a
11125 module function and is not PRIVATE. */
11126 if (sym->ts.type == BT_DERIVED
11127 && sym->ts.u.derived->attr.use_assoc
11128 && sym->ns->proc_name
11129 && sym->ns->proc_name->attr.flavor == FL_MODULE)
11131 gfc_symbol *ds;
11133 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
11134 return;
11136 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
11137 if (!ds && sym->attr.function
11138 && gfc_check_access (sym->attr.access, sym->ns->default_access))
11140 symtree = gfc_new_symtree (&sym->ns->sym_root,
11141 sym->ts.u.derived->name);
11142 symtree->n.sym = sym->ts.u.derived;
11143 sym->ts.u.derived->refs++;
11147 /* Unless the derived-type declaration is use associated, Fortran 95
11148 does not allow public entries of private derived types.
11149 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
11150 161 in 95-006r3. */
11151 if (sym->ts.type == BT_DERIVED
11152 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
11153 && !sym->ts.u.derived->attr.use_assoc
11154 && gfc_check_access (sym->attr.access, sym->ns->default_access)
11155 && !gfc_check_access (sym->ts.u.derived->attr.access,
11156 sym->ts.u.derived->ns->default_access)
11157 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
11158 "of PRIVATE derived type '%s'",
11159 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
11160 : "variable", sym->name, &sym->declared_at,
11161 sym->ts.u.derived->name) == FAILURE)
11162 return;
11164 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
11165 default initialization is defined (5.1.2.4.4). */
11166 if (sym->ts.type == BT_DERIVED
11167 && sym->attr.dummy
11168 && sym->attr.intent == INTENT_OUT
11169 && sym->as
11170 && sym->as->type == AS_ASSUMED_SIZE)
11172 for (c = sym->ts.u.derived->components; c; c = c->next)
11174 if (c->initializer)
11176 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
11177 "ASSUMED SIZE and so cannot have a default initializer",
11178 sym->name, &sym->declared_at);
11179 return;
11184 switch (sym->attr.flavor)
11186 case FL_VARIABLE:
11187 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
11188 return;
11189 break;
11191 case FL_PROCEDURE:
11192 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
11193 return;
11194 break;
11196 case FL_NAMELIST:
11197 if (resolve_fl_namelist (sym) == FAILURE)
11198 return;
11199 break;
11201 case FL_PARAMETER:
11202 if (resolve_fl_parameter (sym) == FAILURE)
11203 return;
11204 break;
11206 default:
11207 break;
11210 /* Resolve array specifier. Check as well some constraints
11211 on COMMON blocks. */
11213 check_constant = sym->attr.in_common && !sym->attr.pointer;
11215 /* Set the formal_arg_flag so that check_conflict will not throw
11216 an error for host associated variables in the specification
11217 expression for an array_valued function. */
11218 if (sym->attr.function && sym->as)
11219 formal_arg_flag = 1;
11221 gfc_resolve_array_spec (sym->as, check_constant);
11223 formal_arg_flag = 0;
11225 /* Resolve formal namespaces. */
11226 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
11227 && !sym->attr.contained && !sym->attr.intrinsic)
11228 gfc_resolve (sym->formal_ns);
11230 /* Make sure the formal namespace is present. */
11231 if (sym->formal && !sym->formal_ns)
11233 gfc_formal_arglist *formal = sym->formal;
11234 while (formal && !formal->sym)
11235 formal = formal->next;
11237 if (formal)
11239 sym->formal_ns = formal->sym->ns;
11240 sym->formal_ns->refs++;
11244 /* Check threadprivate restrictions. */
11245 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
11246 && (!sym->attr.in_common
11247 && sym->module == NULL
11248 && (sym->ns->proc_name == NULL
11249 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
11250 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
11252 /* If we have come this far we can apply default-initializers, as
11253 described in 14.7.5, to those variables that have not already
11254 been assigned one. */
11255 if (sym->ts.type == BT_DERIVED
11256 && sym->attr.referenced
11257 && sym->ns == gfc_current_ns
11258 && !sym->value
11259 && !sym->attr.allocatable
11260 && !sym->attr.alloc_comp)
11262 symbol_attribute *a = &sym->attr;
11264 if ((!a->save && !a->dummy && !a->pointer
11265 && !a->in_common && !a->use_assoc
11266 && !(a->function && sym != sym->result))
11267 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
11268 apply_default_init (sym);
11271 /* If this symbol has a type-spec, check it. */
11272 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
11273 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
11274 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
11275 == FAILURE)
11276 return;
11280 /************* Resolve DATA statements *************/
11282 static struct
11284 gfc_data_value *vnode;
11285 mpz_t left;
11287 values;
11290 /* Advance the values structure to point to the next value in the data list. */
11292 static gfc_try
11293 next_data_value (void)
11295 while (mpz_cmp_ui (values.left, 0) == 0)
11298 if (values.vnode->next == NULL)
11299 return FAILURE;
11301 values.vnode = values.vnode->next;
11302 mpz_set (values.left, values.vnode->repeat);
11305 return SUCCESS;
11309 static gfc_try
11310 check_data_variable (gfc_data_variable *var, locus *where)
11312 gfc_expr *e;
11313 mpz_t size;
11314 mpz_t offset;
11315 gfc_try t;
11316 ar_type mark = AR_UNKNOWN;
11317 int i;
11318 mpz_t section_index[GFC_MAX_DIMENSIONS];
11319 gfc_ref *ref;
11320 gfc_array_ref *ar;
11321 gfc_symbol *sym;
11322 int has_pointer;
11324 if (gfc_resolve_expr (var->expr) == FAILURE)
11325 return FAILURE;
11327 ar = NULL;
11328 mpz_init_set_si (offset, 0);
11329 e = var->expr;
11331 if (e->expr_type != EXPR_VARIABLE)
11332 gfc_internal_error ("check_data_variable(): Bad expression");
11334 sym = e->symtree->n.sym;
11336 if (sym->ns->is_block_data && !sym->attr.in_common)
11338 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
11339 sym->name, &sym->declared_at);
11342 if (e->ref == NULL && sym->as)
11344 gfc_error ("DATA array '%s' at %L must be specified in a previous"
11345 " declaration", sym->name, where);
11346 return FAILURE;
11349 has_pointer = sym->attr.pointer;
11351 for (ref = e->ref; ref; ref = ref->next)
11353 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
11354 has_pointer = 1;
11356 if (has_pointer
11357 && ref->type == REF_ARRAY
11358 && ref->u.ar.type != AR_FULL)
11360 gfc_error ("DATA element '%s' at %L is a pointer and so must "
11361 "be a full array", sym->name, where);
11362 return FAILURE;
11366 if (e->rank == 0 || has_pointer)
11368 mpz_init_set_ui (size, 1);
11369 ref = NULL;
11371 else
11373 ref = e->ref;
11375 /* Find the array section reference. */
11376 for (ref = e->ref; ref; ref = ref->next)
11378 if (ref->type != REF_ARRAY)
11379 continue;
11380 if (ref->u.ar.type == AR_ELEMENT)
11381 continue;
11382 break;
11384 gcc_assert (ref);
11386 /* Set marks according to the reference pattern. */
11387 switch (ref->u.ar.type)
11389 case AR_FULL:
11390 mark = AR_FULL;
11391 break;
11393 case AR_SECTION:
11394 ar = &ref->u.ar;
11395 /* Get the start position of array section. */
11396 gfc_get_section_index (ar, section_index, &offset);
11397 mark = AR_SECTION;
11398 break;
11400 default:
11401 gcc_unreachable ();
11404 if (gfc_array_size (e, &size) == FAILURE)
11406 gfc_error ("Nonconstant array section at %L in DATA statement",
11407 &e->where);
11408 mpz_clear (offset);
11409 return FAILURE;
11413 t = SUCCESS;
11415 while (mpz_cmp_ui (size, 0) > 0)
11417 if (next_data_value () == FAILURE)
11419 gfc_error ("DATA statement at %L has more variables than values",
11420 where);
11421 t = FAILURE;
11422 break;
11425 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
11426 if (t == FAILURE)
11427 break;
11429 /* If we have more than one element left in the repeat count,
11430 and we have more than one element left in the target variable,
11431 then create a range assignment. */
11432 /* FIXME: Only done for full arrays for now, since array sections
11433 seem tricky. */
11434 if (mark == AR_FULL && ref && ref->next == NULL
11435 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
11437 mpz_t range;
11439 if (mpz_cmp (size, values.left) >= 0)
11441 mpz_init_set (range, values.left);
11442 mpz_sub (size, size, values.left);
11443 mpz_set_ui (values.left, 0);
11445 else
11447 mpz_init_set (range, size);
11448 mpz_sub (values.left, values.left, size);
11449 mpz_set_ui (size, 0);
11452 gfc_assign_data_value_range (var->expr, values.vnode->expr,
11453 offset, range);
11455 mpz_add (offset, offset, range);
11456 mpz_clear (range);
11459 /* Assign initial value to symbol. */
11460 else
11462 mpz_sub_ui (values.left, values.left, 1);
11463 mpz_sub_ui (size, size, 1);
11465 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
11466 if (t == FAILURE)
11467 break;
11469 if (mark == AR_FULL)
11470 mpz_add_ui (offset, offset, 1);
11472 /* Modify the array section indexes and recalculate the offset
11473 for next element. */
11474 else if (mark == AR_SECTION)
11475 gfc_advance_section (section_index, ar, &offset);
11479 if (mark == AR_SECTION)
11481 for (i = 0; i < ar->dimen; i++)
11482 mpz_clear (section_index[i]);
11485 mpz_clear (size);
11486 mpz_clear (offset);
11488 return t;
11492 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
11494 /* Iterate over a list of elements in a DATA statement. */
11496 static gfc_try
11497 traverse_data_list (gfc_data_variable *var, locus *where)
11499 mpz_t trip;
11500 iterator_stack frame;
11501 gfc_expr *e, *start, *end, *step;
11502 gfc_try retval = SUCCESS;
11504 mpz_init (frame.value);
11506 start = gfc_copy_expr (var->iter.start);
11507 end = gfc_copy_expr (var->iter.end);
11508 step = gfc_copy_expr (var->iter.step);
11510 if (gfc_simplify_expr (start, 1) == FAILURE
11511 || start->expr_type != EXPR_CONSTANT)
11513 gfc_error ("iterator start at %L does not simplify", &start->where);
11514 retval = FAILURE;
11515 goto cleanup;
11517 if (gfc_simplify_expr (end, 1) == FAILURE
11518 || end->expr_type != EXPR_CONSTANT)
11520 gfc_error ("iterator end at %L does not simplify", &end->where);
11521 retval = FAILURE;
11522 goto cleanup;
11524 if (gfc_simplify_expr (step, 1) == FAILURE
11525 || step->expr_type != EXPR_CONSTANT)
11527 gfc_error ("iterator step at %L does not simplify", &step->where);
11528 retval = FAILURE;
11529 goto cleanup;
11532 mpz_init_set (trip, end->value.integer);
11533 mpz_sub (trip, trip, start->value.integer);
11534 mpz_add (trip, trip, step->value.integer);
11536 mpz_div (trip, trip, step->value.integer);
11538 mpz_set (frame.value, start->value.integer);
11540 frame.prev = iter_stack;
11541 frame.variable = var->iter.var->symtree;
11542 iter_stack = &frame;
11544 while (mpz_cmp_ui (trip, 0) > 0)
11546 if (traverse_data_var (var->list, where) == FAILURE)
11548 mpz_clear (trip);
11549 retval = FAILURE;
11550 goto cleanup;
11553 e = gfc_copy_expr (var->expr);
11554 if (gfc_simplify_expr (e, 1) == FAILURE)
11556 gfc_free_expr (e);
11557 mpz_clear (trip);
11558 retval = FAILURE;
11559 goto cleanup;
11562 mpz_add (frame.value, frame.value, step->value.integer);
11564 mpz_sub_ui (trip, trip, 1);
11567 mpz_clear (trip);
11568 cleanup:
11569 mpz_clear (frame.value);
11571 gfc_free_expr (start);
11572 gfc_free_expr (end);
11573 gfc_free_expr (step);
11575 iter_stack = frame.prev;
11576 return retval;
11580 /* Type resolve variables in the variable list of a DATA statement. */
11582 static gfc_try
11583 traverse_data_var (gfc_data_variable *var, locus *where)
11585 gfc_try t;
11587 for (; var; var = var->next)
11589 if (var->expr == NULL)
11590 t = traverse_data_list (var, where);
11591 else
11592 t = check_data_variable (var, where);
11594 if (t == FAILURE)
11595 return FAILURE;
11598 return SUCCESS;
11602 /* Resolve the expressions and iterators associated with a data statement.
11603 This is separate from the assignment checking because data lists should
11604 only be resolved once. */
11606 static gfc_try
11607 resolve_data_variables (gfc_data_variable *d)
11609 for (; d; d = d->next)
11611 if (d->list == NULL)
11613 if (gfc_resolve_expr (d->expr) == FAILURE)
11614 return FAILURE;
11616 else
11618 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
11619 return FAILURE;
11621 if (resolve_data_variables (d->list) == FAILURE)
11622 return FAILURE;
11626 return SUCCESS;
11630 /* Resolve a single DATA statement. We implement this by storing a pointer to
11631 the value list into static variables, and then recursively traversing the
11632 variables list, expanding iterators and such. */
11634 static void
11635 resolve_data (gfc_data *d)
11638 if (resolve_data_variables (d->var) == FAILURE)
11639 return;
11641 values.vnode = d->value;
11642 if (d->value == NULL)
11643 mpz_set_ui (values.left, 0);
11644 else
11645 mpz_set (values.left, d->value->repeat);
11647 if (traverse_data_var (d->var, &d->where) == FAILURE)
11648 return;
11650 /* At this point, we better not have any values left. */
11652 if (next_data_value () == SUCCESS)
11653 gfc_error ("DATA statement at %L has more values than variables",
11654 &d->where);
11658 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
11659 accessed by host or use association, is a dummy argument to a pure function,
11660 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
11661 is storage associated with any such variable, shall not be used in the
11662 following contexts: (clients of this function). */
11664 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
11665 procedure. Returns zero if assignment is OK, nonzero if there is a
11666 problem. */
11668 gfc_impure_variable (gfc_symbol *sym)
11670 gfc_symbol *proc;
11672 if (sym->attr.use_assoc || sym->attr.in_common)
11673 return 1;
11675 if (sym->ns != gfc_current_ns)
11676 return !sym->attr.function;
11678 proc = sym->ns->proc_name;
11679 if (sym->attr.dummy && gfc_pure (proc)
11680 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
11682 proc->attr.function))
11683 return 1;
11685 /* TODO: Sort out what can be storage associated, if anything, and include
11686 it here. In principle equivalences should be scanned but it does not
11687 seem to be possible to storage associate an impure variable this way. */
11688 return 0;
11692 /* Test whether a symbol is pure or not. For a NULL pointer, checks the
11693 symbol of the current procedure. */
11696 gfc_pure (gfc_symbol *sym)
11698 symbol_attribute attr;
11700 if (sym == NULL)
11701 sym = gfc_current_ns->proc_name;
11702 if (sym == NULL)
11703 return 0;
11705 attr = sym->attr;
11707 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
11711 /* Test whether the current procedure is elemental or not. */
11714 gfc_elemental (gfc_symbol *sym)
11716 symbol_attribute attr;
11718 if (sym == NULL)
11719 sym = gfc_current_ns->proc_name;
11720 if (sym == NULL)
11721 return 0;
11722 attr = sym->attr;
11724 return attr.flavor == FL_PROCEDURE && attr.elemental;
11728 /* Warn about unused labels. */
11730 static void
11731 warn_unused_fortran_label (gfc_st_label *label)
11733 if (label == NULL)
11734 return;
11736 warn_unused_fortran_label (label->left);
11738 if (label->defined == ST_LABEL_UNKNOWN)
11739 return;
11741 switch (label->referenced)
11743 case ST_LABEL_UNKNOWN:
11744 gfc_warning ("Label %d at %L defined but not used", label->value,
11745 &label->where);
11746 break;
11748 case ST_LABEL_BAD_TARGET:
11749 gfc_warning ("Label %d at %L defined but cannot be used",
11750 label->value, &label->where);
11751 break;
11753 default:
11754 break;
11757 warn_unused_fortran_label (label->right);
11761 /* Returns the sequence type of a symbol or sequence. */
11763 static seq_type
11764 sequence_type (gfc_typespec ts)
11766 seq_type result;
11767 gfc_component *c;
11769 switch (ts.type)
11771 case BT_DERIVED:
11773 if (ts.u.derived->components == NULL)
11774 return SEQ_NONDEFAULT;
11776 result = sequence_type (ts.u.derived->components->ts);
11777 for (c = ts.u.derived->components->next; c; c = c->next)
11778 if (sequence_type (c->ts) != result)
11779 return SEQ_MIXED;
11781 return result;
11783 case BT_CHARACTER:
11784 if (ts.kind != gfc_default_character_kind)
11785 return SEQ_NONDEFAULT;
11787 return SEQ_CHARACTER;
11789 case BT_INTEGER:
11790 if (ts.kind != gfc_default_integer_kind)
11791 return SEQ_NONDEFAULT;
11793 return SEQ_NUMERIC;
11795 case BT_REAL:
11796 if (!(ts.kind == gfc_default_real_kind
11797 || ts.kind == gfc_default_double_kind))
11798 return SEQ_NONDEFAULT;
11800 return SEQ_NUMERIC;
11802 case BT_COMPLEX:
11803 if (ts.kind != gfc_default_complex_kind)
11804 return SEQ_NONDEFAULT;
11806 return SEQ_NUMERIC;
11808 case BT_LOGICAL:
11809 if (ts.kind != gfc_default_logical_kind)
11810 return SEQ_NONDEFAULT;
11812 return SEQ_NUMERIC;
11814 default:
11815 return SEQ_NONDEFAULT;
11820 /* Resolve derived type EQUIVALENCE object. */
11822 static gfc_try
11823 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
11825 gfc_component *c = derived->components;
11827 if (!derived)
11828 return SUCCESS;
11830 /* Shall not be an object of nonsequence derived type. */
11831 if (!derived->attr.sequence)
11833 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
11834 "attribute to be an EQUIVALENCE object", sym->name,
11835 &e->where);
11836 return FAILURE;
11839 /* Shall not have allocatable components. */
11840 if (derived->attr.alloc_comp)
11842 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
11843 "components to be an EQUIVALENCE object",sym->name,
11844 &e->where);
11845 return FAILURE;
11848 if (sym->attr.in_common && has_default_initializer (sym->ts.u.derived))
11850 gfc_error ("Derived type variable '%s' at %L with default "
11851 "initialization cannot be in EQUIVALENCE with a variable "
11852 "in COMMON", sym->name, &e->where);
11853 return FAILURE;
11856 for (; c ; c = c->next)
11858 if (c->ts.type == BT_DERIVED
11859 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
11860 return FAILURE;
11862 /* Shall not be an object of sequence derived type containing a pointer
11863 in the structure. */
11864 if (c->attr.pointer)
11866 gfc_error ("Derived type variable '%s' at %L with pointer "
11867 "component(s) cannot be an EQUIVALENCE object",
11868 sym->name, &e->where);
11869 return FAILURE;
11872 return SUCCESS;
11876 /* Resolve equivalence object.
11877 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
11878 an allocatable array, an object of nonsequence derived type, an object of
11879 sequence derived type containing a pointer at any level of component
11880 selection, an automatic object, a function name, an entry name, a result
11881 name, a named constant, a structure component, or a subobject of any of
11882 the preceding objects. A substring shall not have length zero. A
11883 derived type shall not have components with default initialization nor
11884 shall two objects of an equivalence group be initialized.
11885 Either all or none of the objects shall have an protected attribute.
11886 The simple constraints are done in symbol.c(check_conflict) and the rest
11887 are implemented here. */
11889 static void
11890 resolve_equivalence (gfc_equiv *eq)
11892 gfc_symbol *sym;
11893 gfc_symbol *first_sym;
11894 gfc_expr *e;
11895 gfc_ref *r;
11896 locus *last_where = NULL;
11897 seq_type eq_type, last_eq_type;
11898 gfc_typespec *last_ts;
11899 int object, cnt_protected;
11900 const char *msg;
11902 last_ts = &eq->expr->symtree->n.sym->ts;
11904 first_sym = eq->expr->symtree->n.sym;
11906 cnt_protected = 0;
11908 for (object = 1; eq; eq = eq->eq, object++)
11910 e = eq->expr;
11912 e->ts = e->symtree->n.sym->ts;
11913 /* match_varspec might not know yet if it is seeing
11914 array reference or substring reference, as it doesn't
11915 know the types. */
11916 if (e->ref && e->ref->type == REF_ARRAY)
11918 gfc_ref *ref = e->ref;
11919 sym = e->symtree->n.sym;
11921 if (sym->attr.dimension)
11923 ref->u.ar.as = sym->as;
11924 ref = ref->next;
11927 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
11928 if (e->ts.type == BT_CHARACTER
11929 && ref
11930 && ref->type == REF_ARRAY
11931 && ref->u.ar.dimen == 1
11932 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
11933 && ref->u.ar.stride[0] == NULL)
11935 gfc_expr *start = ref->u.ar.start[0];
11936 gfc_expr *end = ref->u.ar.end[0];
11937 void *mem = NULL;
11939 /* Optimize away the (:) reference. */
11940 if (start == NULL && end == NULL)
11942 if (e->ref == ref)
11943 e->ref = ref->next;
11944 else
11945 e->ref->next = ref->next;
11946 mem = ref;
11948 else
11950 ref->type = REF_SUBSTRING;
11951 if (start == NULL)
11952 start = gfc_int_expr (1);
11953 ref->u.ss.start = start;
11954 if (end == NULL && e->ts.u.cl)
11955 end = gfc_copy_expr (e->ts.u.cl->length);
11956 ref->u.ss.end = end;
11957 ref->u.ss.length = e->ts.u.cl;
11958 e->ts.u.cl = NULL;
11960 ref = ref->next;
11961 gfc_free (mem);
11964 /* Any further ref is an error. */
11965 if (ref)
11967 gcc_assert (ref->type == REF_ARRAY);
11968 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
11969 &ref->u.ar.where);
11970 continue;
11974 if (gfc_resolve_expr (e) == FAILURE)
11975 continue;
11977 sym = e->symtree->n.sym;
11979 if (sym->attr.is_protected)
11980 cnt_protected++;
11981 if (cnt_protected > 0 && cnt_protected != object)
11983 gfc_error ("Either all or none of the objects in the "
11984 "EQUIVALENCE set at %L shall have the "
11985 "PROTECTED attribute",
11986 &e->where);
11987 break;
11990 /* Shall not equivalence common block variables in a PURE procedure. */
11991 if (sym->ns->proc_name
11992 && sym->ns->proc_name->attr.pure
11993 && sym->attr.in_common)
11995 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
11996 "object in the pure procedure '%s'",
11997 sym->name, &e->where, sym->ns->proc_name->name);
11998 break;
12001 /* Shall not be a named constant. */
12002 if (e->expr_type == EXPR_CONSTANT)
12004 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
12005 "object", sym->name, &e->where);
12006 continue;
12009 if (e->ts.type == BT_DERIVED
12010 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
12011 continue;
12013 /* Check that the types correspond correctly:
12014 Note 5.28:
12015 A numeric sequence structure may be equivalenced to another sequence
12016 structure, an object of default integer type, default real type, double
12017 precision real type, default logical type such that components of the
12018 structure ultimately only become associated to objects of the same
12019 kind. A character sequence structure may be equivalenced to an object
12020 of default character kind or another character sequence structure.
12021 Other objects may be equivalenced only to objects of the same type and
12022 kind parameters. */
12024 /* Identical types are unconditionally OK. */
12025 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
12026 goto identical_types;
12028 last_eq_type = sequence_type (*last_ts);
12029 eq_type = sequence_type (sym->ts);
12031 /* Since the pair of objects is not of the same type, mixed or
12032 non-default sequences can be rejected. */
12034 msg = "Sequence %s with mixed components in EQUIVALENCE "
12035 "statement at %L with different type objects";
12036 if ((object ==2
12037 && last_eq_type == SEQ_MIXED
12038 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
12039 == FAILURE)
12040 || (eq_type == SEQ_MIXED
12041 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12042 &e->where) == FAILURE))
12043 continue;
12045 msg = "Non-default type object or sequence %s in EQUIVALENCE "
12046 "statement at %L with objects of different type";
12047 if ((object ==2
12048 && last_eq_type == SEQ_NONDEFAULT
12049 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
12050 last_where) == FAILURE)
12051 || (eq_type == SEQ_NONDEFAULT
12052 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12053 &e->where) == FAILURE))
12054 continue;
12056 msg ="Non-CHARACTER object '%s' in default CHARACTER "
12057 "EQUIVALENCE statement at %L";
12058 if (last_eq_type == SEQ_CHARACTER
12059 && eq_type != SEQ_CHARACTER
12060 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12061 &e->where) == FAILURE)
12062 continue;
12064 msg ="Non-NUMERIC object '%s' in default NUMERIC "
12065 "EQUIVALENCE statement at %L";
12066 if (last_eq_type == SEQ_NUMERIC
12067 && eq_type != SEQ_NUMERIC
12068 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12069 &e->where) == FAILURE)
12070 continue;
12072 identical_types:
12073 last_ts =&sym->ts;
12074 last_where = &e->where;
12076 if (!e->ref)
12077 continue;
12079 /* Shall not be an automatic array. */
12080 if (e->ref->type == REF_ARRAY
12081 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
12083 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
12084 "an EQUIVALENCE object", sym->name, &e->where);
12085 continue;
12088 r = e->ref;
12089 while (r)
12091 /* Shall not be a structure component. */
12092 if (r->type == REF_COMPONENT)
12094 gfc_error ("Structure component '%s' at %L cannot be an "
12095 "EQUIVALENCE object",
12096 r->u.c.component->name, &e->where);
12097 break;
12100 /* A substring shall not have length zero. */
12101 if (r->type == REF_SUBSTRING)
12103 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
12105 gfc_error ("Substring at %L has length zero",
12106 &r->u.ss.start->where);
12107 break;
12110 r = r->next;
12116 /* Resolve function and ENTRY types, issue diagnostics if needed. */
12118 static void
12119 resolve_fntype (gfc_namespace *ns)
12121 gfc_entry_list *el;
12122 gfc_symbol *sym;
12124 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
12125 return;
12127 /* If there are any entries, ns->proc_name is the entry master
12128 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
12129 if (ns->entries)
12130 sym = ns->entries->sym;
12131 else
12132 sym = ns->proc_name;
12133 if (sym->result == sym
12134 && sym->ts.type == BT_UNKNOWN
12135 && gfc_set_default_type (sym, 0, NULL) == FAILURE
12136 && !sym->attr.untyped)
12138 gfc_error ("Function '%s' at %L has no IMPLICIT type",
12139 sym->name, &sym->declared_at);
12140 sym->attr.untyped = 1;
12143 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
12144 && !sym->attr.contained
12145 && !gfc_check_access (sym->ts.u.derived->attr.access,
12146 sym->ts.u.derived->ns->default_access)
12147 && gfc_check_access (sym->attr.access, sym->ns->default_access))
12149 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
12150 "%L of PRIVATE type '%s'", sym->name,
12151 &sym->declared_at, sym->ts.u.derived->name);
12154 if (ns->entries)
12155 for (el = ns->entries->next; el; el = el->next)
12157 if (el->sym->result == el->sym
12158 && el->sym->ts.type == BT_UNKNOWN
12159 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
12160 && !el->sym->attr.untyped)
12162 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
12163 el->sym->name, &el->sym->declared_at);
12164 el->sym->attr.untyped = 1;
12170 /* 12.3.2.1.1 Defined operators. */
12172 static gfc_try
12173 check_uop_procedure (gfc_symbol *sym, locus where)
12175 gfc_formal_arglist *formal;
12177 if (!sym->attr.function)
12179 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
12180 sym->name, &where);
12181 return FAILURE;
12184 if (sym->ts.type == BT_CHARACTER
12185 && !(sym->ts.u.cl && sym->ts.u.cl->length)
12186 && !(sym->result && sym->result->ts.u.cl
12187 && sym->result->ts.u.cl->length))
12189 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
12190 "character length", sym->name, &where);
12191 return FAILURE;
12194 formal = sym->formal;
12195 if (!formal || !formal->sym)
12197 gfc_error ("User operator procedure '%s' at %L must have at least "
12198 "one argument", sym->name, &where);
12199 return FAILURE;
12202 if (formal->sym->attr.intent != INTENT_IN)
12204 gfc_error ("First argument of operator interface at %L must be "
12205 "INTENT(IN)", &where);
12206 return FAILURE;
12209 if (formal->sym->attr.optional)
12211 gfc_error ("First argument of operator interface at %L cannot be "
12212 "optional", &where);
12213 return FAILURE;
12216 formal = formal->next;
12217 if (!formal || !formal->sym)
12218 return SUCCESS;
12220 if (formal->sym->attr.intent != INTENT_IN)
12222 gfc_error ("Second argument of operator interface at %L must be "
12223 "INTENT(IN)", &where);
12224 return FAILURE;
12227 if (formal->sym->attr.optional)
12229 gfc_error ("Second argument of operator interface at %L cannot be "
12230 "optional", &where);
12231 return FAILURE;
12234 if (formal->next)
12236 gfc_error ("Operator interface at %L must have, at most, two "
12237 "arguments", &where);
12238 return FAILURE;
12241 return SUCCESS;
12244 static void
12245 gfc_resolve_uops (gfc_symtree *symtree)
12247 gfc_interface *itr;
12249 if (symtree == NULL)
12250 return;
12252 gfc_resolve_uops (symtree->left);
12253 gfc_resolve_uops (symtree->right);
12255 for (itr = symtree->n.uop->op; itr; itr = itr->next)
12256 check_uop_procedure (itr->sym, itr->sym->declared_at);
12260 /* Examine all of the expressions associated with a program unit,
12261 assign types to all intermediate expressions, make sure that all
12262 assignments are to compatible types and figure out which names
12263 refer to which functions or subroutines. It doesn't check code
12264 block, which is handled by resolve_code. */
12266 static void
12267 resolve_types (gfc_namespace *ns)
12269 gfc_namespace *n;
12270 gfc_charlen *cl;
12271 gfc_data *d;
12272 gfc_equiv *eq;
12273 gfc_namespace* old_ns = gfc_current_ns;
12275 /* Check that all IMPLICIT types are ok. */
12276 if (!ns->seen_implicit_none)
12278 unsigned letter;
12279 for (letter = 0; letter != GFC_LETTERS; ++letter)
12280 if (ns->set_flag[letter]
12281 && resolve_typespec_used (&ns->default_type[letter],
12282 &ns->implicit_loc[letter],
12283 NULL) == FAILURE)
12284 return;
12287 gfc_current_ns = ns;
12289 resolve_entries (ns);
12291 resolve_common_vars (ns->blank_common.head, false);
12292 resolve_common_blocks (ns->common_root);
12294 resolve_contained_functions (ns);
12296 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
12298 for (cl = ns->cl_list; cl; cl = cl->next)
12299 resolve_charlen (cl);
12301 gfc_traverse_ns (ns, resolve_symbol);
12303 resolve_fntype (ns);
12305 for (n = ns->contained; n; n = n->sibling)
12307 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
12308 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
12309 "also be PURE", n->proc_name->name,
12310 &n->proc_name->declared_at);
12312 resolve_types (n);
12315 forall_flag = 0;
12316 gfc_check_interfaces (ns);
12318 gfc_traverse_ns (ns, resolve_values);
12320 if (ns->save_all)
12321 gfc_save_all (ns);
12323 iter_stack = NULL;
12324 for (d = ns->data; d; d = d->next)
12325 resolve_data (d);
12327 iter_stack = NULL;
12328 gfc_traverse_ns (ns, gfc_formalize_init_value);
12330 gfc_traverse_ns (ns, gfc_verify_binding_labels);
12332 if (ns->common_root != NULL)
12333 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
12335 for (eq = ns->equiv; eq; eq = eq->next)
12336 resolve_equivalence (eq);
12338 /* Warn about unused labels. */
12339 if (warn_unused_label)
12340 warn_unused_fortran_label (ns->st_labels);
12342 gfc_resolve_uops (ns->uop_root);
12344 gfc_current_ns = old_ns;
12348 /* Call resolve_code recursively. */
12350 static void
12351 resolve_codes (gfc_namespace *ns)
12353 gfc_namespace *n;
12354 bitmap_obstack old_obstack;
12356 for (n = ns->contained; n; n = n->sibling)
12357 resolve_codes (n);
12359 gfc_current_ns = ns;
12361 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
12362 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
12363 cs_base = NULL;
12365 /* Set to an out of range value. */
12366 current_entry_id = -1;
12368 old_obstack = labels_obstack;
12369 bitmap_obstack_initialize (&labels_obstack);
12371 resolve_code (ns->code, ns);
12373 bitmap_obstack_release (&labels_obstack);
12374 labels_obstack = old_obstack;
12378 /* This function is called after a complete program unit has been compiled.
12379 Its purpose is to examine all of the expressions associated with a program
12380 unit, assign types to all intermediate expressions, make sure that all
12381 assignments are to compatible types and figure out which names refer to
12382 which functions or subroutines. */
12384 void
12385 gfc_resolve (gfc_namespace *ns)
12387 gfc_namespace *old_ns;
12388 code_stack *old_cs_base;
12390 if (ns->resolved)
12391 return;
12393 ns->resolved = -1;
12394 old_ns = gfc_current_ns;
12395 old_cs_base = cs_base;
12397 resolve_types (ns);
12398 resolve_codes (ns);
12400 gfc_current_ns = old_ns;
12401 cs_base = old_cs_base;
12402 ns->resolved = 1;