Merged r158465 through r158660 into branch.
[official-gcc.git] / gcc / fortran / resolve.c
blobaeccffb60ca09935d53756e04089380650fbfeeb
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 */
32 #include "constructor.h"
34 /* Types used in equivalence statements. */
36 typedef enum seq_type
38 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
40 seq_type;
42 /* Stack to keep track of the nesting of blocks as we move through the
43 code. See resolve_branch() and resolve_code(). */
45 typedef struct code_stack
47 struct gfc_code *head, *current;
48 struct code_stack *prev;
50 /* This bitmap keeps track of the targets valid for a branch from
51 inside this block except for END {IF|SELECT}s of enclosing
52 blocks. */
53 bitmap reachable_labels;
55 code_stack;
57 static code_stack *cs_base = NULL;
60 /* Nonzero if we're inside a FORALL block. */
62 static int forall_flag;
64 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
66 static int omp_workshare_flag;
68 /* Nonzero if we are processing a formal arglist. The corresponding function
69 resets the flag each time that it is read. */
70 static int formal_arg_flag = 0;
72 /* True if we are resolving a specification expression. */
73 static int specification_expr = 0;
75 /* The id of the last entry seen. */
76 static int current_entry_id;
78 /* We use bitmaps to determine if a branch target is valid. */
79 static bitmap_obstack labels_obstack;
81 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
82 static bool inquiry_argument = false;
84 int
85 gfc_is_formal_arg (void)
87 return formal_arg_flag;
90 /* Is the symbol host associated? */
91 static bool
92 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
94 for (ns = ns->parent; ns; ns = ns->parent)
96 if (sym->ns == ns)
97 return true;
100 return false;
103 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
104 an ABSTRACT derived-type. If where is not NULL, an error message with that
105 locus is printed, optionally using name. */
107 static gfc_try
108 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
110 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
112 if (where)
114 if (name)
115 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
116 name, where, ts->u.derived->name);
117 else
118 gfc_error ("ABSTRACT type '%s' used at %L",
119 ts->u.derived->name, where);
122 return FAILURE;
125 return SUCCESS;
129 /* Resolve types of formal argument lists. These have to be done early so that
130 the formal argument lists of module procedures can be copied to the
131 containing module before the individual procedures are resolved
132 individually. We also resolve argument lists of procedures in interface
133 blocks because they are self-contained scoping units.
135 Since a dummy argument cannot be a non-dummy procedure, the only
136 resort left for untyped names are the IMPLICIT types. */
138 static void
139 resolve_formal_arglist (gfc_symbol *proc)
141 gfc_formal_arglist *f;
142 gfc_symbol *sym;
143 int i;
145 if (proc->result != NULL)
146 sym = proc->result;
147 else
148 sym = proc;
150 if (gfc_elemental (proc)
151 || sym->attr.pointer || sym->attr.allocatable
152 || (sym->as && sym->as->rank > 0))
154 proc->attr.always_explicit = 1;
155 sym->attr.always_explicit = 1;
158 formal_arg_flag = 1;
160 for (f = proc->formal; f; f = f->next)
162 sym = f->sym;
164 if (sym == NULL)
166 /* Alternate return placeholder. */
167 if (gfc_elemental (proc))
168 gfc_error ("Alternate return specifier in elemental subroutine "
169 "'%s' at %L is not allowed", proc->name,
170 &proc->declared_at);
171 if (proc->attr.function)
172 gfc_error ("Alternate return specifier in function "
173 "'%s' at %L is not allowed", proc->name,
174 &proc->declared_at);
175 continue;
178 if (sym->attr.if_source != IFSRC_UNKNOWN)
179 resolve_formal_arglist (sym);
181 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
183 if (gfc_pure (proc) && !gfc_pure (sym))
185 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
186 "also be PURE", sym->name, &sym->declared_at);
187 continue;
190 if (gfc_elemental (proc))
192 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
193 "procedure", &sym->declared_at);
194 continue;
197 if (sym->attr.function
198 && sym->ts.type == BT_UNKNOWN
199 && sym->attr.intrinsic)
201 gfc_intrinsic_sym *isym;
202 isym = gfc_find_function (sym->name);
203 if (isym == NULL || !isym->specific)
205 gfc_error ("Unable to find a specific INTRINSIC procedure "
206 "for the reference '%s' at %L", sym->name,
207 &sym->declared_at);
209 sym->ts = isym->ts;
212 continue;
215 if (sym->ts.type == BT_UNKNOWN)
217 if (!sym->attr.function || sym->result == sym)
218 gfc_set_default_type (sym, 1, sym->ns);
221 gfc_resolve_array_spec (sym->as, 0);
223 /* We can't tell if an array with dimension (:) is assumed or deferred
224 shape until we know if it has the pointer or allocatable attributes.
226 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
227 && !(sym->attr.pointer || sym->attr.allocatable))
229 sym->as->type = AS_ASSUMED_SHAPE;
230 for (i = 0; i < sym->as->rank; i++)
231 sym->as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind,
232 NULL, 1);
235 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
236 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
237 || sym->attr.optional)
239 proc->attr.always_explicit = 1;
240 if (proc->result)
241 proc->result->attr.always_explicit = 1;
244 /* If the flavor is unknown at this point, it has to be a variable.
245 A procedure specification would have already set the type. */
247 if (sym->attr.flavor == FL_UNKNOWN)
248 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
250 if (gfc_pure (proc) && !sym->attr.pointer
251 && sym->attr.flavor != FL_PROCEDURE)
253 if (proc->attr.function && sym->attr.intent != INTENT_IN)
254 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
255 "INTENT(IN)", sym->name, proc->name,
256 &sym->declared_at);
258 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
259 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
260 "have its INTENT specified", sym->name, proc->name,
261 &sym->declared_at);
264 if (gfc_elemental (proc))
266 /* F2008, C1289. */
267 if (sym->attr.codimension)
269 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
270 "procedure", sym->name, &sym->declared_at);
271 continue;
274 if (sym->as != NULL)
276 gfc_error ("Argument '%s' of elemental procedure at %L must "
277 "be scalar", sym->name, &sym->declared_at);
278 continue;
281 if (sym->attr.pointer)
283 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
284 "have the POINTER attribute", sym->name,
285 &sym->declared_at);
286 continue;
289 if (sym->attr.flavor == FL_PROCEDURE)
291 gfc_error ("Dummy procedure '%s' not allowed in elemental "
292 "procedure '%s' at %L", sym->name, proc->name,
293 &sym->declared_at);
294 continue;
298 /* Each dummy shall be specified to be scalar. */
299 if (proc->attr.proc == PROC_ST_FUNCTION)
301 if (sym->as != NULL)
303 gfc_error ("Argument '%s' of statement function at %L must "
304 "be scalar", sym->name, &sym->declared_at);
305 continue;
308 if (sym->ts.type == BT_CHARACTER)
310 gfc_charlen *cl = sym->ts.u.cl;
311 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
313 gfc_error ("Character-valued argument '%s' of statement "
314 "function at %L must have constant length",
315 sym->name, &sym->declared_at);
316 continue;
321 formal_arg_flag = 0;
325 /* Work function called when searching for symbols that have argument lists
326 associated with them. */
328 static void
329 find_arglists (gfc_symbol *sym)
331 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
332 return;
334 resolve_formal_arglist (sym);
338 /* Given a namespace, resolve all formal argument lists within the namespace.
341 static void
342 resolve_formal_arglists (gfc_namespace *ns)
344 if (ns == NULL)
345 return;
347 gfc_traverse_ns (ns, find_arglists);
351 static void
352 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
354 gfc_try t;
356 /* If this namespace is not a function or an entry master function,
357 ignore it. */
358 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
359 || sym->attr.entry_master)
360 return;
362 /* Try to find out of what the return type is. */
363 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
365 t = gfc_set_default_type (sym->result, 0, ns);
367 if (t == FAILURE && !sym->result->attr.untyped)
369 if (sym->result == sym)
370 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
371 sym->name, &sym->declared_at);
372 else if (!sym->result->attr.proc_pointer)
373 gfc_error ("Result '%s' of contained function '%s' at %L has "
374 "no IMPLICIT type", sym->result->name, sym->name,
375 &sym->result->declared_at);
376 sym->result->attr.untyped = 1;
380 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
381 type, lists the only ways a character length value of * can be used:
382 dummy arguments of procedures, named constants, and function results
383 in external functions. Internal function results and results of module
384 procedures are not on this list, ergo, not permitted. */
386 if (sym->result->ts.type == BT_CHARACTER)
388 gfc_charlen *cl = sym->result->ts.u.cl;
389 if (!cl || !cl->length)
391 /* See if this is a module-procedure and adapt error message
392 accordingly. */
393 bool module_proc;
394 gcc_assert (ns->parent && ns->parent->proc_name);
395 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
397 gfc_error ("Character-valued %s '%s' at %L must not be"
398 " assumed length",
399 module_proc ? _("module procedure")
400 : _("internal function"),
401 sym->name, &sym->declared_at);
407 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
408 introduce duplicates. */
410 static void
411 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
413 gfc_formal_arglist *f, *new_arglist;
414 gfc_symbol *new_sym;
416 for (; new_args != NULL; new_args = new_args->next)
418 new_sym = new_args->sym;
419 /* See if this arg is already in the formal argument list. */
420 for (f = proc->formal; f; f = f->next)
422 if (new_sym == f->sym)
423 break;
426 if (f)
427 continue;
429 /* Add a new argument. Argument order is not important. */
430 new_arglist = gfc_get_formal_arglist ();
431 new_arglist->sym = new_sym;
432 new_arglist->next = proc->formal;
433 proc->formal = new_arglist;
438 /* Flag the arguments that are not present in all entries. */
440 static void
441 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
443 gfc_formal_arglist *f, *head;
444 head = new_args;
446 for (f = proc->formal; f; f = f->next)
448 if (f->sym == NULL)
449 continue;
451 for (new_args = head; new_args; new_args = new_args->next)
453 if (new_args->sym == f->sym)
454 break;
457 if (new_args)
458 continue;
460 f->sym->attr.not_always_present = 1;
465 /* Resolve alternate entry points. If a symbol has multiple entry points we
466 create a new master symbol for the main routine, and turn the existing
467 symbol into an entry point. */
469 static void
470 resolve_entries (gfc_namespace *ns)
472 gfc_namespace *old_ns;
473 gfc_code *c;
474 gfc_symbol *proc;
475 gfc_entry_list *el;
476 char name[GFC_MAX_SYMBOL_LEN + 1];
477 static int master_count = 0;
479 if (ns->proc_name == NULL)
480 return;
482 /* No need to do anything if this procedure doesn't have alternate entry
483 points. */
484 if (!ns->entries)
485 return;
487 /* We may already have resolved alternate entry points. */
488 if (ns->proc_name->attr.entry_master)
489 return;
491 /* If this isn't a procedure something has gone horribly wrong. */
492 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
494 /* Remember the current namespace. */
495 old_ns = gfc_current_ns;
497 gfc_current_ns = ns;
499 /* Add the main entry point to the list of entry points. */
500 el = gfc_get_entry_list ();
501 el->sym = ns->proc_name;
502 el->id = 0;
503 el->next = ns->entries;
504 ns->entries = el;
505 ns->proc_name->attr.entry = 1;
507 /* If it is a module function, it needs to be in the right namespace
508 so that gfc_get_fake_result_decl can gather up the results. The
509 need for this arose in get_proc_name, where these beasts were
510 left in their own namespace, to keep prior references linked to
511 the entry declaration.*/
512 if (ns->proc_name->attr.function
513 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
514 el->sym->ns = ns;
516 /* Do the same for entries where the master is not a module
517 procedure. These are retained in the module namespace because
518 of the module procedure declaration. */
519 for (el = el->next; el; el = el->next)
520 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
521 && el->sym->attr.mod_proc)
522 el->sym->ns = ns;
523 el = ns->entries;
525 /* Add an entry statement for it. */
526 c = gfc_get_code ();
527 c->op = EXEC_ENTRY;
528 c->ext.entry = el;
529 c->next = ns->code;
530 ns->code = c;
532 /* Create a new symbol for the master function. */
533 /* Give the internal function a unique name (within this file).
534 Also include the function name so the user has some hope of figuring
535 out what is going on. */
536 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
537 master_count++, ns->proc_name->name);
538 gfc_get_ha_symbol (name, &proc);
539 gcc_assert (proc != NULL);
541 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
542 if (ns->proc_name->attr.subroutine)
543 gfc_add_subroutine (&proc->attr, proc->name, NULL);
544 else
546 gfc_symbol *sym;
547 gfc_typespec *ts, *fts;
548 gfc_array_spec *as, *fas;
549 gfc_add_function (&proc->attr, proc->name, NULL);
550 proc->result = proc;
551 fas = ns->entries->sym->as;
552 fas = fas ? fas : ns->entries->sym->result->as;
553 fts = &ns->entries->sym->result->ts;
554 if (fts->type == BT_UNKNOWN)
555 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
556 for (el = ns->entries->next; el; el = el->next)
558 ts = &el->sym->result->ts;
559 as = el->sym->as;
560 as = as ? as : el->sym->result->as;
561 if (ts->type == BT_UNKNOWN)
562 ts = gfc_get_default_type (el->sym->result->name, NULL);
564 if (! gfc_compare_types (ts, fts)
565 || (el->sym->result->attr.dimension
566 != ns->entries->sym->result->attr.dimension)
567 || (el->sym->result->attr.pointer
568 != ns->entries->sym->result->attr.pointer))
569 break;
570 else if (as && fas && ns->entries->sym->result != el->sym->result
571 && gfc_compare_array_spec (as, fas) == 0)
572 gfc_error ("Function %s at %L has entries with mismatched "
573 "array specifications", ns->entries->sym->name,
574 &ns->entries->sym->declared_at);
575 /* The characteristics need to match and thus both need to have
576 the same string length, i.e. both len=*, or both len=4.
577 Having both len=<variable> is also possible, but difficult to
578 check at compile time. */
579 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
580 && (((ts->u.cl->length && !fts->u.cl->length)
581 ||(!ts->u.cl->length && fts->u.cl->length))
582 || (ts->u.cl->length
583 && ts->u.cl->length->expr_type
584 != fts->u.cl->length->expr_type)
585 || (ts->u.cl->length
586 && ts->u.cl->length->expr_type == EXPR_CONSTANT
587 && mpz_cmp (ts->u.cl->length->value.integer,
588 fts->u.cl->length->value.integer) != 0)))
589 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
590 "entries returning variables of different "
591 "string lengths", ns->entries->sym->name,
592 &ns->entries->sym->declared_at);
595 if (el == NULL)
597 sym = ns->entries->sym->result;
598 /* All result types the same. */
599 proc->ts = *fts;
600 if (sym->attr.dimension)
601 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
602 if (sym->attr.pointer)
603 gfc_add_pointer (&proc->attr, NULL);
605 else
607 /* Otherwise the result will be passed through a union by
608 reference. */
609 proc->attr.mixed_entry_master = 1;
610 for (el = ns->entries; el; el = el->next)
612 sym = el->sym->result;
613 if (sym->attr.dimension)
615 if (el == ns->entries)
616 gfc_error ("FUNCTION result %s can't be an array in "
617 "FUNCTION %s at %L", sym->name,
618 ns->entries->sym->name, &sym->declared_at);
619 else
620 gfc_error ("ENTRY result %s can't be an array in "
621 "FUNCTION %s at %L", sym->name,
622 ns->entries->sym->name, &sym->declared_at);
624 else if (sym->attr.pointer)
626 if (el == ns->entries)
627 gfc_error ("FUNCTION result %s can't be a POINTER in "
628 "FUNCTION %s at %L", sym->name,
629 ns->entries->sym->name, &sym->declared_at);
630 else
631 gfc_error ("ENTRY result %s can't be a POINTER in "
632 "FUNCTION %s at %L", sym->name,
633 ns->entries->sym->name, &sym->declared_at);
635 else
637 ts = &sym->ts;
638 if (ts->type == BT_UNKNOWN)
639 ts = gfc_get_default_type (sym->name, NULL);
640 switch (ts->type)
642 case BT_INTEGER:
643 if (ts->kind == gfc_default_integer_kind)
644 sym = NULL;
645 break;
646 case BT_REAL:
647 if (ts->kind == gfc_default_real_kind
648 || ts->kind == gfc_default_double_kind)
649 sym = NULL;
650 break;
651 case BT_COMPLEX:
652 if (ts->kind == gfc_default_complex_kind)
653 sym = NULL;
654 break;
655 case BT_LOGICAL:
656 if (ts->kind == gfc_default_logical_kind)
657 sym = NULL;
658 break;
659 case BT_UNKNOWN:
660 /* We will issue error elsewhere. */
661 sym = NULL;
662 break;
663 default:
664 break;
666 if (sym)
668 if (el == ns->entries)
669 gfc_error ("FUNCTION result %s can't be of type %s "
670 "in FUNCTION %s at %L", sym->name,
671 gfc_typename (ts), ns->entries->sym->name,
672 &sym->declared_at);
673 else
674 gfc_error ("ENTRY result %s can't be of type %s "
675 "in FUNCTION %s at %L", sym->name,
676 gfc_typename (ts), ns->entries->sym->name,
677 &sym->declared_at);
683 proc->attr.access = ACCESS_PRIVATE;
684 proc->attr.entry_master = 1;
686 /* Merge all the entry point arguments. */
687 for (el = ns->entries; el; el = el->next)
688 merge_argument_lists (proc, el->sym->formal);
690 /* Check the master formal arguments for any that are not
691 present in all entry points. */
692 for (el = ns->entries; el; el = el->next)
693 check_argument_lists (proc, el->sym->formal);
695 /* Use the master function for the function body. */
696 ns->proc_name = proc;
698 /* Finalize the new symbols. */
699 gfc_commit_symbols ();
701 /* Restore the original namespace. */
702 gfc_current_ns = old_ns;
706 static bool
707 has_default_initializer (gfc_symbol *der)
709 gfc_component *c;
711 gcc_assert (der->attr.flavor == FL_DERIVED);
712 for (c = der->components; c; c = c->next)
713 if ((c->ts.type != BT_DERIVED && c->initializer)
714 || (c->ts.type == BT_DERIVED
715 && (!c->attr.pointer && has_default_initializer (c->ts.u.derived))))
716 break;
718 return c != NULL;
721 /* Resolve common variables. */
722 static void
723 resolve_common_vars (gfc_symbol *sym, bool named_common)
725 gfc_symbol *csym = sym;
727 for (; csym; csym = csym->common_next)
729 if (csym->value || csym->attr.data)
731 if (!csym->ns->is_block_data)
732 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
733 "but only in BLOCK DATA initialization is "
734 "allowed", csym->name, &csym->declared_at);
735 else if (!named_common)
736 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
737 "in a blank COMMON but initialization is only "
738 "allowed in named common blocks", csym->name,
739 &csym->declared_at);
742 if (csym->ts.type != BT_DERIVED)
743 continue;
745 if (!(csym->ts.u.derived->attr.sequence
746 || csym->ts.u.derived->attr.is_bind_c))
747 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
748 "has neither the SEQUENCE nor the BIND(C) "
749 "attribute", csym->name, &csym->declared_at);
750 if (csym->ts.u.derived->attr.alloc_comp)
751 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
752 "has an ultimate component that is "
753 "allocatable", csym->name, &csym->declared_at);
754 if (has_default_initializer (csym->ts.u.derived))
755 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
756 "may not have default initializer", csym->name,
757 &csym->declared_at);
759 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
760 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
764 /* Resolve common blocks. */
765 static void
766 resolve_common_blocks (gfc_symtree *common_root)
768 gfc_symbol *sym;
770 if (common_root == NULL)
771 return;
773 if (common_root->left)
774 resolve_common_blocks (common_root->left);
775 if (common_root->right)
776 resolve_common_blocks (common_root->right);
778 resolve_common_vars (common_root->n.common->head, true);
780 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
781 if (sym == NULL)
782 return;
784 if (sym->attr.flavor == FL_PARAMETER)
785 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
786 sym->name, &common_root->n.common->where, &sym->declared_at);
788 if (sym->attr.intrinsic)
789 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
790 sym->name, &common_root->n.common->where);
791 else if (sym->attr.result
792 || gfc_is_function_return_value (sym, gfc_current_ns))
793 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
794 "that is also a function result", sym->name,
795 &common_root->n.common->where);
796 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
797 && sym->attr.proc != PROC_ST_FUNCTION)
798 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
799 "that is also a global procedure", sym->name,
800 &common_root->n.common->where);
804 /* Resolve contained function types. Because contained functions can call one
805 another, they have to be worked out before any of the contained procedures
806 can be resolved.
808 The good news is that if a function doesn't already have a type, the only
809 way it can get one is through an IMPLICIT type or a RESULT variable, because
810 by definition contained functions are contained namespace they're contained
811 in, not in a sibling or parent namespace. */
813 static void
814 resolve_contained_functions (gfc_namespace *ns)
816 gfc_namespace *child;
817 gfc_entry_list *el;
819 resolve_formal_arglists (ns);
821 for (child = ns->contained; child; child = child->sibling)
823 /* Resolve alternate entry points first. */
824 resolve_entries (child);
826 /* Then check function return types. */
827 resolve_contained_fntype (child->proc_name, child);
828 for (el = child->entries; el; el = el->next)
829 resolve_contained_fntype (el->sym, child);
834 /* Resolve all of the elements of a structure constructor and make sure that
835 the types are correct. */
837 static gfc_try
838 resolve_structure_cons (gfc_expr *expr)
840 gfc_constructor *cons;
841 gfc_component *comp;
842 gfc_try t;
843 symbol_attribute a;
845 t = SUCCESS;
846 cons = gfc_constructor_first (expr->value.constructor);
847 /* A constructor may have references if it is the result of substituting a
848 parameter variable. In this case we just pull out the component we
849 want. */
850 if (expr->ref)
851 comp = expr->ref->u.c.sym->components;
852 else
853 comp = expr->ts.u.derived->components;
855 /* See if the user is trying to invoke a structure constructor for one of
856 the iso_c_binding derived types. */
857 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
858 && expr->ts.u.derived->ts.is_iso_c && cons
859 && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
861 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
862 expr->ts.u.derived->name, &(expr->where));
863 return FAILURE;
866 /* Return if structure constructor is c_null_(fun)prt. */
867 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
868 && expr->ts.u.derived->ts.is_iso_c && cons
869 && cons->expr && cons->expr->expr_type == EXPR_NULL)
870 return SUCCESS;
872 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
874 int rank;
876 if (!cons->expr)
877 continue;
879 if (gfc_resolve_expr (cons->expr) == FAILURE)
881 t = FAILURE;
882 continue;
885 rank = comp->as ? comp->as->rank : 0;
886 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
887 && (comp->attr.allocatable || cons->expr->rank))
889 gfc_error ("The rank of the element in the derived type "
890 "constructor at %L does not match that of the "
891 "component (%d/%d)", &cons->expr->where,
892 cons->expr->rank, rank);
893 t = FAILURE;
896 /* If we don't have the right type, try to convert it. */
898 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
900 t = FAILURE;
901 if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
902 gfc_error ("The element in the derived type constructor at %L, "
903 "for pointer component '%s', is %s but should be %s",
904 &cons->expr->where, comp->name,
905 gfc_basic_typename (cons->expr->ts.type),
906 gfc_basic_typename (comp->ts.type));
907 else
908 t = gfc_convert_type (cons->expr, &comp->ts, 1);
911 if (cons->expr->expr_type == EXPR_NULL
912 && !(comp->attr.pointer || comp->attr.allocatable
913 || comp->attr.proc_pointer
914 || (comp->ts.type == BT_CLASS
915 && (comp->ts.u.derived->components->attr.pointer
916 || comp->ts.u.derived->components->attr.allocatable))))
918 t = FAILURE;
919 gfc_error ("The NULL in the derived type constructor at %L is "
920 "being applied to component '%s', which is neither "
921 "a POINTER nor ALLOCATABLE", &cons->expr->where,
922 comp->name);
925 if (!comp->attr.pointer || cons->expr->expr_type == EXPR_NULL)
926 continue;
928 a = gfc_expr_attr (cons->expr);
930 if (!a.pointer && !a.target)
932 t = FAILURE;
933 gfc_error ("The element in the derived type constructor at %L, "
934 "for pointer component '%s' should be a POINTER or "
935 "a TARGET", &cons->expr->where, comp->name);
938 /* F2003, C1272 (3). */
939 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
940 && (gfc_impure_variable (cons->expr->symtree->n.sym)
941 || gfc_is_coindexed (cons->expr)))
943 t = FAILURE;
944 gfc_error ("Invalid expression in the derived type constructor for "
945 "pointer component '%s' at %L in PURE procedure",
946 comp->name, &cons->expr->where);
950 return t;
954 /****************** Expression name resolution ******************/
956 /* Returns 0 if a symbol was not declared with a type or
957 attribute declaration statement, nonzero otherwise. */
959 static int
960 was_declared (gfc_symbol *sym)
962 symbol_attribute a;
964 a = sym->attr;
966 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
967 return 1;
969 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
970 || a.optional || a.pointer || a.save || a.target || a.volatile_
971 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
972 || a.asynchronous || a.codimension)
973 return 1;
975 return 0;
979 /* Determine if a symbol is generic or not. */
981 static int
982 generic_sym (gfc_symbol *sym)
984 gfc_symbol *s;
986 if (sym->attr.generic ||
987 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
988 return 1;
990 if (was_declared (sym) || sym->ns->parent == NULL)
991 return 0;
993 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
995 if (s != NULL)
997 if (s == sym)
998 return 0;
999 else
1000 return generic_sym (s);
1003 return 0;
1007 /* Determine if a symbol is specific or not. */
1009 static int
1010 specific_sym (gfc_symbol *sym)
1012 gfc_symbol *s;
1014 if (sym->attr.if_source == IFSRC_IFBODY
1015 || sym->attr.proc == PROC_MODULE
1016 || sym->attr.proc == PROC_INTERNAL
1017 || sym->attr.proc == PROC_ST_FUNCTION
1018 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1019 || sym->attr.external)
1020 return 1;
1022 if (was_declared (sym) || sym->ns->parent == NULL)
1023 return 0;
1025 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1027 return (s == NULL) ? 0 : specific_sym (s);
1031 /* Figure out if the procedure is specific, generic or unknown. */
1033 typedef enum
1034 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1035 proc_type;
1037 static proc_type
1038 procedure_kind (gfc_symbol *sym)
1040 if (generic_sym (sym))
1041 return PTYPE_GENERIC;
1043 if (specific_sym (sym))
1044 return PTYPE_SPECIFIC;
1046 return PTYPE_UNKNOWN;
1049 /* Check references to assumed size arrays. The flag need_full_assumed_size
1050 is nonzero when matching actual arguments. */
1052 static int need_full_assumed_size = 0;
1054 static bool
1055 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1057 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1058 return false;
1060 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1061 What should it be? */
1062 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1063 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1064 && (e->ref->u.ar.type == AR_FULL))
1066 gfc_error ("The upper bound in the last dimension must "
1067 "appear in the reference to the assumed size "
1068 "array '%s' at %L", sym->name, &e->where);
1069 return true;
1071 return false;
1075 /* Look for bad assumed size array references in argument expressions
1076 of elemental and array valued intrinsic procedures. Since this is
1077 called from procedure resolution functions, it only recurses at
1078 operators. */
1080 static bool
1081 resolve_assumed_size_actual (gfc_expr *e)
1083 if (e == NULL)
1084 return false;
1086 switch (e->expr_type)
1088 case EXPR_VARIABLE:
1089 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1090 return true;
1091 break;
1093 case EXPR_OP:
1094 if (resolve_assumed_size_actual (e->value.op.op1)
1095 || resolve_assumed_size_actual (e->value.op.op2))
1096 return true;
1097 break;
1099 default:
1100 break;
1102 return false;
1106 /* Check a generic procedure, passed as an actual argument, to see if
1107 there is a matching specific name. If none, it is an error, and if
1108 more than one, the reference is ambiguous. */
1109 static int
1110 count_specific_procs (gfc_expr *e)
1112 int n;
1113 gfc_interface *p;
1114 gfc_symbol *sym;
1116 n = 0;
1117 sym = e->symtree->n.sym;
1119 for (p = sym->generic; p; p = p->next)
1120 if (strcmp (sym->name, p->sym->name) == 0)
1122 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1123 sym->name);
1124 n++;
1127 if (n > 1)
1128 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1129 &e->where);
1131 if (n == 0)
1132 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1133 "argument at %L", sym->name, &e->where);
1135 return n;
1139 /* See if a call to sym could possibly be a not allowed RECURSION because of
1140 a missing RECURIVE declaration. This means that either sym is the current
1141 context itself, or sym is the parent of a contained procedure calling its
1142 non-RECURSIVE containing procedure.
1143 This also works if sym is an ENTRY. */
1145 static bool
1146 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1148 gfc_symbol* proc_sym;
1149 gfc_symbol* context_proc;
1150 gfc_namespace* real_context;
1152 if (sym->attr.flavor == FL_PROGRAM)
1153 return false;
1155 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1157 /* If we've got an ENTRY, find real procedure. */
1158 if (sym->attr.entry && sym->ns->entries)
1159 proc_sym = sym->ns->entries->sym;
1160 else
1161 proc_sym = sym;
1163 /* If sym is RECURSIVE, all is well of course. */
1164 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1165 return false;
1167 /* Find the context procedure's "real" symbol if it has entries.
1168 We look for a procedure symbol, so recurse on the parents if we don't
1169 find one (like in case of a BLOCK construct). */
1170 for (real_context = context; ; real_context = real_context->parent)
1172 /* We should find something, eventually! */
1173 gcc_assert (real_context);
1175 context_proc = (real_context->entries ? real_context->entries->sym
1176 : real_context->proc_name);
1178 /* In some special cases, there may not be a proc_name, like for this
1179 invalid code:
1180 real(bad_kind()) function foo () ...
1181 when checking the call to bad_kind ().
1182 In these cases, we simply return here and assume that the
1183 call is ok. */
1184 if (!context_proc)
1185 return false;
1187 if (context_proc->attr.flavor != FL_LABEL)
1188 break;
1191 /* A call from sym's body to itself is recursion, of course. */
1192 if (context_proc == proc_sym)
1193 return true;
1195 /* The same is true if context is a contained procedure and sym the
1196 containing one. */
1197 if (context_proc->attr.contained)
1199 gfc_symbol* parent_proc;
1201 gcc_assert (context->parent);
1202 parent_proc = (context->parent->entries ? context->parent->entries->sym
1203 : context->parent->proc_name);
1205 if (parent_proc == proc_sym)
1206 return true;
1209 return false;
1213 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1214 its typespec and formal argument list. */
1216 static gfc_try
1217 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1219 gfc_intrinsic_sym* isym;
1220 const char* symstd;
1222 if (sym->formal)
1223 return SUCCESS;
1225 /* We already know this one is an intrinsic, so we don't call
1226 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1227 gfc_find_subroutine directly to check whether it is a function or
1228 subroutine. */
1230 if ((isym = gfc_find_function (sym->name)))
1232 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1233 && !sym->attr.implicit_type)
1234 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1235 " ignored", sym->name, &sym->declared_at);
1237 if (!sym->attr.function &&
1238 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1239 return FAILURE;
1241 sym->ts = isym->ts;
1243 else if ((isym = gfc_find_subroutine (sym->name)))
1245 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1247 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1248 " specifier", sym->name, &sym->declared_at);
1249 return FAILURE;
1252 if (!sym->attr.subroutine &&
1253 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1254 return FAILURE;
1256 else
1258 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1259 &sym->declared_at);
1260 return FAILURE;
1263 gfc_copy_formal_args_intr (sym, isym);
1265 /* Check it is actually available in the standard settings. */
1266 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1267 == FAILURE)
1269 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1270 " available in the current standard settings but %s. Use"
1271 " an appropriate -std=* option or enable -fall-intrinsics"
1272 " in order to use it.",
1273 sym->name, &sym->declared_at, symstd);
1274 return FAILURE;
1277 return SUCCESS;
1281 /* Resolve a procedure expression, like passing it to a called procedure or as
1282 RHS for a procedure pointer assignment. */
1284 static gfc_try
1285 resolve_procedure_expression (gfc_expr* expr)
1287 gfc_symbol* sym;
1289 if (expr->expr_type != EXPR_VARIABLE)
1290 return SUCCESS;
1291 gcc_assert (expr->symtree);
1293 sym = expr->symtree->n.sym;
1295 if (sym->attr.intrinsic)
1296 resolve_intrinsic (sym, &expr->where);
1298 if (sym->attr.flavor != FL_PROCEDURE
1299 || (sym->attr.function && sym->result == sym))
1300 return SUCCESS;
1302 /* A non-RECURSIVE procedure that is used as procedure expression within its
1303 own body is in danger of being called recursively. */
1304 if (is_illegal_recursion (sym, gfc_current_ns))
1305 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1306 " itself recursively. Declare it RECURSIVE or use"
1307 " -frecursive", sym->name, &expr->where);
1309 return SUCCESS;
1313 /* Resolve an actual argument list. Most of the time, this is just
1314 resolving the expressions in the list.
1315 The exception is that we sometimes have to decide whether arguments
1316 that look like procedure arguments are really simple variable
1317 references. */
1319 static gfc_try
1320 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1321 bool no_formal_args)
1323 gfc_symbol *sym;
1324 gfc_symtree *parent_st;
1325 gfc_expr *e;
1326 int save_need_full_assumed_size;
1327 gfc_component *comp;
1329 for (; arg; arg = arg->next)
1331 e = arg->expr;
1332 if (e == NULL)
1334 /* Check the label is a valid branching target. */
1335 if (arg->label)
1337 if (arg->label->defined == ST_LABEL_UNKNOWN)
1339 gfc_error ("Label %d referenced at %L is never defined",
1340 arg->label->value, &arg->label->where);
1341 return FAILURE;
1344 continue;
1347 if (gfc_is_proc_ptr_comp (e, &comp))
1349 e->ts = comp->ts;
1350 if (e->expr_type == EXPR_PPC)
1352 if (comp->as != NULL)
1353 e->rank = comp->as->rank;
1354 e->expr_type = EXPR_FUNCTION;
1356 if (gfc_resolve_expr (e) == FAILURE)
1357 return FAILURE;
1358 goto argument_list;
1361 if (e->expr_type == EXPR_VARIABLE
1362 && e->symtree->n.sym->attr.generic
1363 && no_formal_args
1364 && count_specific_procs (e) != 1)
1365 return FAILURE;
1367 if (e->ts.type != BT_PROCEDURE)
1369 save_need_full_assumed_size = need_full_assumed_size;
1370 if (e->expr_type != EXPR_VARIABLE)
1371 need_full_assumed_size = 0;
1372 if (gfc_resolve_expr (e) != SUCCESS)
1373 return FAILURE;
1374 need_full_assumed_size = save_need_full_assumed_size;
1375 goto argument_list;
1378 /* See if the expression node should really be a variable reference. */
1380 sym = e->symtree->n.sym;
1382 if (sym->attr.flavor == FL_PROCEDURE
1383 || sym->attr.intrinsic
1384 || sym->attr.external)
1386 int actual_ok;
1388 /* If a procedure is not already determined to be something else
1389 check if it is intrinsic. */
1390 if (!sym->attr.intrinsic
1391 && !(sym->attr.external || sym->attr.use_assoc
1392 || sym->attr.if_source == IFSRC_IFBODY)
1393 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1394 sym->attr.intrinsic = 1;
1396 if (sym->attr.proc == PROC_ST_FUNCTION)
1398 gfc_error ("Statement function '%s' at %L is not allowed as an "
1399 "actual argument", sym->name, &e->where);
1402 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1403 sym->attr.subroutine);
1404 if (sym->attr.intrinsic && actual_ok == 0)
1406 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1407 "actual argument", sym->name, &e->where);
1410 if (sym->attr.contained && !sym->attr.use_assoc
1411 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1413 gfc_error ("Internal procedure '%s' is not allowed as an "
1414 "actual argument at %L", sym->name, &e->where);
1417 if (sym->attr.elemental && !sym->attr.intrinsic)
1419 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1420 "allowed as an actual argument at %L", sym->name,
1421 &e->where);
1424 /* Check if a generic interface has a specific procedure
1425 with the same name before emitting an error. */
1426 if (sym->attr.generic && count_specific_procs (e) != 1)
1427 return FAILURE;
1429 /* Just in case a specific was found for the expression. */
1430 sym = e->symtree->n.sym;
1432 /* If the symbol is the function that names the current (or
1433 parent) scope, then we really have a variable reference. */
1435 if (gfc_is_function_return_value (sym, sym->ns))
1436 goto got_variable;
1438 /* If all else fails, see if we have a specific intrinsic. */
1439 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1441 gfc_intrinsic_sym *isym;
1443 isym = gfc_find_function (sym->name);
1444 if (isym == NULL || !isym->specific)
1446 gfc_error ("Unable to find a specific INTRINSIC procedure "
1447 "for the reference '%s' at %L", sym->name,
1448 &e->where);
1449 return FAILURE;
1451 sym->ts = isym->ts;
1452 sym->attr.intrinsic = 1;
1453 sym->attr.function = 1;
1456 if (gfc_resolve_expr (e) == FAILURE)
1457 return FAILURE;
1458 goto argument_list;
1461 /* See if the name is a module procedure in a parent unit. */
1463 if (was_declared (sym) || sym->ns->parent == NULL)
1464 goto got_variable;
1466 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1468 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1469 return FAILURE;
1472 if (parent_st == NULL)
1473 goto got_variable;
1475 sym = parent_st->n.sym;
1476 e->symtree = parent_st; /* Point to the right thing. */
1478 if (sym->attr.flavor == FL_PROCEDURE
1479 || sym->attr.intrinsic
1480 || sym->attr.external)
1482 if (gfc_resolve_expr (e) == FAILURE)
1483 return FAILURE;
1484 goto argument_list;
1487 got_variable:
1488 e->expr_type = EXPR_VARIABLE;
1489 e->ts = sym->ts;
1490 if (sym->as != NULL)
1492 e->rank = sym->as->rank;
1493 e->ref = gfc_get_ref ();
1494 e->ref->type = REF_ARRAY;
1495 e->ref->u.ar.type = AR_FULL;
1496 e->ref->u.ar.as = sym->as;
1499 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1500 primary.c (match_actual_arg). If above code determines that it
1501 is a variable instead, it needs to be resolved as it was not
1502 done at the beginning of this function. */
1503 save_need_full_assumed_size = need_full_assumed_size;
1504 if (e->expr_type != EXPR_VARIABLE)
1505 need_full_assumed_size = 0;
1506 if (gfc_resolve_expr (e) != SUCCESS)
1507 return FAILURE;
1508 need_full_assumed_size = save_need_full_assumed_size;
1510 argument_list:
1511 /* Check argument list functions %VAL, %LOC and %REF. There is
1512 nothing to do for %REF. */
1513 if (arg->name && arg->name[0] == '%')
1515 if (strncmp ("%VAL", arg->name, 4) == 0)
1517 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1519 gfc_error ("By-value argument at %L is not of numeric "
1520 "type", &e->where);
1521 return FAILURE;
1524 if (e->rank)
1526 gfc_error ("By-value argument at %L cannot be an array or "
1527 "an array section", &e->where);
1528 return FAILURE;
1531 /* Intrinsics are still PROC_UNKNOWN here. However,
1532 since same file external procedures are not resolvable
1533 in gfortran, it is a good deal easier to leave them to
1534 intrinsic.c. */
1535 if (ptype != PROC_UNKNOWN
1536 && ptype != PROC_DUMMY
1537 && ptype != PROC_EXTERNAL
1538 && ptype != PROC_MODULE)
1540 gfc_error ("By-value argument at %L is not allowed "
1541 "in this context", &e->where);
1542 return FAILURE;
1546 /* Statement functions have already been excluded above. */
1547 else if (strncmp ("%LOC", arg->name, 4) == 0
1548 && e->ts.type == BT_PROCEDURE)
1550 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1552 gfc_error ("Passing internal procedure at %L by location "
1553 "not allowed", &e->where);
1554 return FAILURE;
1559 /* Fortran 2008, C1237. */
1560 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1561 && gfc_has_ultimate_pointer (e))
1563 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1564 "component", &e->where);
1565 return FAILURE;
1569 return SUCCESS;
1573 /* Do the checks of the actual argument list that are specific to elemental
1574 procedures. If called with c == NULL, we have a function, otherwise if
1575 expr == NULL, we have a subroutine. */
1577 static gfc_try
1578 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1580 gfc_actual_arglist *arg0;
1581 gfc_actual_arglist *arg;
1582 gfc_symbol *esym = NULL;
1583 gfc_intrinsic_sym *isym = NULL;
1584 gfc_expr *e = NULL;
1585 gfc_intrinsic_arg *iformal = NULL;
1586 gfc_formal_arglist *eformal = NULL;
1587 bool formal_optional = false;
1588 bool set_by_optional = false;
1589 int i;
1590 int rank = 0;
1592 /* Is this an elemental procedure? */
1593 if (expr && expr->value.function.actual != NULL)
1595 if (expr->value.function.esym != NULL
1596 && expr->value.function.esym->attr.elemental)
1598 arg0 = expr->value.function.actual;
1599 esym = expr->value.function.esym;
1601 else if (expr->value.function.isym != NULL
1602 && expr->value.function.isym->elemental)
1604 arg0 = expr->value.function.actual;
1605 isym = expr->value.function.isym;
1607 else
1608 return SUCCESS;
1610 else if (c && c->ext.actual != NULL)
1612 arg0 = c->ext.actual;
1614 if (c->resolved_sym)
1615 esym = c->resolved_sym;
1616 else
1617 esym = c->symtree->n.sym;
1618 gcc_assert (esym);
1620 if (!esym->attr.elemental)
1621 return SUCCESS;
1623 else
1624 return SUCCESS;
1626 /* The rank of an elemental is the rank of its array argument(s). */
1627 for (arg = arg0; arg; arg = arg->next)
1629 if (arg->expr != NULL && arg->expr->rank > 0)
1631 rank = arg->expr->rank;
1632 if (arg->expr->expr_type == EXPR_VARIABLE
1633 && arg->expr->symtree->n.sym->attr.optional)
1634 set_by_optional = true;
1636 /* Function specific; set the result rank and shape. */
1637 if (expr)
1639 expr->rank = rank;
1640 if (!expr->shape && arg->expr->shape)
1642 expr->shape = gfc_get_shape (rank);
1643 for (i = 0; i < rank; i++)
1644 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1647 break;
1651 /* If it is an array, it shall not be supplied as an actual argument
1652 to an elemental procedure unless an array of the same rank is supplied
1653 as an actual argument corresponding to a nonoptional dummy argument of
1654 that elemental procedure(12.4.1.5). */
1655 formal_optional = false;
1656 if (isym)
1657 iformal = isym->formal;
1658 else
1659 eformal = esym->formal;
1661 for (arg = arg0; arg; arg = arg->next)
1663 if (eformal)
1665 if (eformal->sym && eformal->sym->attr.optional)
1666 formal_optional = true;
1667 eformal = eformal->next;
1669 else if (isym && iformal)
1671 if (iformal->optional)
1672 formal_optional = true;
1673 iformal = iformal->next;
1675 else if (isym)
1676 formal_optional = true;
1678 if (pedantic && arg->expr != NULL
1679 && arg->expr->expr_type == EXPR_VARIABLE
1680 && arg->expr->symtree->n.sym->attr.optional
1681 && formal_optional
1682 && arg->expr->rank
1683 && (set_by_optional || arg->expr->rank != rank)
1684 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1686 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1687 "MISSING, it cannot be the actual argument of an "
1688 "ELEMENTAL procedure unless there is a non-optional "
1689 "argument with the same rank (12.4.1.5)",
1690 arg->expr->symtree->n.sym->name, &arg->expr->where);
1691 return FAILURE;
1695 for (arg = arg0; arg; arg = arg->next)
1697 if (arg->expr == NULL || arg->expr->rank == 0)
1698 continue;
1700 /* Being elemental, the last upper bound of an assumed size array
1701 argument must be present. */
1702 if (resolve_assumed_size_actual (arg->expr))
1703 return FAILURE;
1705 /* Elemental procedure's array actual arguments must conform. */
1706 if (e != NULL)
1708 if (gfc_check_conformance (arg->expr, e,
1709 "elemental procedure") == FAILURE)
1710 return FAILURE;
1712 else
1713 e = arg->expr;
1716 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1717 is an array, the intent inout/out variable needs to be also an array. */
1718 if (rank > 0 && esym && expr == NULL)
1719 for (eformal = esym->formal, arg = arg0; arg && eformal;
1720 arg = arg->next, eformal = eformal->next)
1721 if ((eformal->sym->attr.intent == INTENT_OUT
1722 || eformal->sym->attr.intent == INTENT_INOUT)
1723 && arg->expr && arg->expr->rank == 0)
1725 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1726 "ELEMENTAL subroutine '%s' is a scalar, but another "
1727 "actual argument is an array", &arg->expr->where,
1728 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1729 : "INOUT", eformal->sym->name, esym->name);
1730 return FAILURE;
1732 return SUCCESS;
1736 /* Go through each actual argument in ACTUAL and see if it can be
1737 implemented as an inlined, non-copying intrinsic. FNSYM is the
1738 function being called, or NULL if not known. */
1740 static void
1741 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1743 gfc_actual_arglist *ap;
1744 gfc_expr *expr;
1746 for (ap = actual; ap; ap = ap->next)
1747 if (ap->expr
1748 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1749 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1750 NOT_ELEMENTAL))
1751 ap->expr->inline_noncopying_intrinsic = 1;
1755 /* This function does the checking of references to global procedures
1756 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1757 77 and 95 standards. It checks for a gsymbol for the name, making
1758 one if it does not already exist. If it already exists, then the
1759 reference being resolved must correspond to the type of gsymbol.
1760 Otherwise, the new symbol is equipped with the attributes of the
1761 reference. The corresponding code that is called in creating
1762 global entities is parse.c.
1764 In addition, for all but -std=legacy, the gsymbols are used to
1765 check the interfaces of external procedures from the same file.
1766 The namespace of the gsymbol is resolved and then, once this is
1767 done the interface is checked. */
1770 static bool
1771 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1773 if (!gsym_ns->proc_name->attr.recursive)
1774 return true;
1776 if (sym->ns == gsym_ns)
1777 return false;
1779 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1780 return false;
1782 return true;
1785 static bool
1786 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1788 if (gsym_ns->entries)
1790 gfc_entry_list *entry = gsym_ns->entries;
1792 for (; entry; entry = entry->next)
1794 if (strcmp (sym->name, entry->sym->name) == 0)
1796 if (strcmp (gsym_ns->proc_name->name,
1797 sym->ns->proc_name->name) == 0)
1798 return false;
1800 if (sym->ns->parent
1801 && strcmp (gsym_ns->proc_name->name,
1802 sym->ns->parent->proc_name->name) == 0)
1803 return false;
1807 return true;
1810 static void
1811 resolve_global_procedure (gfc_symbol *sym, locus *where,
1812 gfc_actual_arglist **actual, int sub)
1814 gfc_gsymbol * gsym;
1815 gfc_namespace *ns;
1816 enum gfc_symbol_type type;
1818 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1820 gsym = gfc_get_gsymbol (sym->name);
1822 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1823 gfc_global_used (gsym, where);
1825 if (gfc_option.flag_whole_file
1826 && sym->attr.if_source == IFSRC_UNKNOWN
1827 && gsym->type != GSYM_UNKNOWN
1828 && gsym->ns
1829 && gsym->ns->resolved != -1
1830 && gsym->ns->proc_name
1831 && not_in_recursive (sym, gsym->ns)
1832 && not_entry_self_reference (sym, gsym->ns))
1834 /* Make sure that translation for the gsymbol occurs before
1835 the procedure currently being resolved. */
1836 ns = gsym->ns->resolved ? NULL : gfc_global_ns_list;
1837 for (; ns && ns != gsym->ns; ns = ns->sibling)
1839 if (ns->sibling == gsym->ns)
1841 ns->sibling = gsym->ns->sibling;
1842 gsym->ns->sibling = gfc_global_ns_list;
1843 gfc_global_ns_list = gsym->ns;
1844 break;
1848 if (!gsym->ns->resolved)
1850 gfc_dt_list *old_dt_list;
1852 /* Stash away derived types so that the backend_decls do not
1853 get mixed up. */
1854 old_dt_list = gfc_derived_types;
1855 gfc_derived_types = NULL;
1857 gfc_resolve (gsym->ns);
1859 /* Store the new derived types with the global namespace. */
1860 if (gfc_derived_types)
1861 gsym->ns->derived_types = gfc_derived_types;
1863 /* Restore the derived types of this namespace. */
1864 gfc_derived_types = old_dt_list;
1867 if (gsym->ns->proc_name->attr.function
1868 && gsym->ns->proc_name->as
1869 && gsym->ns->proc_name->as->rank
1870 && (!sym->as || sym->as->rank != gsym->ns->proc_name->as->rank))
1871 gfc_error ("The reference to function '%s' at %L either needs an "
1872 "explicit INTERFACE or the rank is incorrect", sym->name,
1873 where);
1875 /* Non-assumed length character functions. */
1876 if (sym->attr.function && sym->ts.type == BT_CHARACTER
1877 && gsym->ns->proc_name->ts.u.cl != NULL
1878 && gsym->ns->proc_name->ts.u.cl->length != NULL)
1880 gfc_charlen *cl = sym->ts.u.cl;
1882 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
1883 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
1885 gfc_error ("Nonconstant character-length function '%s' at %L "
1886 "must have an explicit interface", sym->name,
1887 &sym->declared_at);
1891 if (gfc_option.flag_whole_file == 1
1892 || ((gfc_option.warn_std & GFC_STD_LEGACY)
1894 !(gfc_option.warn_std & GFC_STD_GNU)))
1895 gfc_errors_to_warnings (1);
1897 gfc_procedure_use (gsym->ns->proc_name, actual, where);
1899 gfc_errors_to_warnings (0);
1902 if (gsym->type == GSYM_UNKNOWN)
1904 gsym->type = type;
1905 gsym->where = *where;
1908 gsym->used = 1;
1912 /************* Function resolution *************/
1914 /* Resolve a function call known to be generic.
1915 Section 14.1.2.4.1. */
1917 static match
1918 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1920 gfc_symbol *s;
1922 if (sym->attr.generic)
1924 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1925 if (s != NULL)
1927 expr->value.function.name = s->name;
1928 expr->value.function.esym = s;
1930 if (s->ts.type != BT_UNKNOWN)
1931 expr->ts = s->ts;
1932 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1933 expr->ts = s->result->ts;
1935 if (s->as != NULL)
1936 expr->rank = s->as->rank;
1937 else if (s->result != NULL && s->result->as != NULL)
1938 expr->rank = s->result->as->rank;
1940 gfc_set_sym_referenced (expr->value.function.esym);
1942 return MATCH_YES;
1945 /* TODO: Need to search for elemental references in generic
1946 interface. */
1949 if (sym->attr.intrinsic)
1950 return gfc_intrinsic_func_interface (expr, 0);
1952 return MATCH_NO;
1956 static gfc_try
1957 resolve_generic_f (gfc_expr *expr)
1959 gfc_symbol *sym;
1960 match m;
1962 sym = expr->symtree->n.sym;
1964 for (;;)
1966 m = resolve_generic_f0 (expr, sym);
1967 if (m == MATCH_YES)
1968 return SUCCESS;
1969 else if (m == MATCH_ERROR)
1970 return FAILURE;
1972 generic:
1973 if (sym->ns->parent == NULL)
1974 break;
1975 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1977 if (sym == NULL)
1978 break;
1979 if (!generic_sym (sym))
1980 goto generic;
1983 /* Last ditch attempt. See if the reference is to an intrinsic
1984 that possesses a matching interface. 14.1.2.4 */
1985 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
1987 gfc_error ("There is no specific function for the generic '%s' at %L",
1988 expr->symtree->n.sym->name, &expr->where);
1989 return FAILURE;
1992 m = gfc_intrinsic_func_interface (expr, 0);
1993 if (m == MATCH_YES)
1994 return SUCCESS;
1995 if (m == MATCH_NO)
1996 gfc_error ("Generic function '%s' at %L is not consistent with a "
1997 "specific intrinsic interface", expr->symtree->n.sym->name,
1998 &expr->where);
2000 return FAILURE;
2004 /* Resolve a function call known to be specific. */
2006 static match
2007 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2009 match m;
2011 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2013 if (sym->attr.dummy)
2015 sym->attr.proc = PROC_DUMMY;
2016 goto found;
2019 sym->attr.proc = PROC_EXTERNAL;
2020 goto found;
2023 if (sym->attr.proc == PROC_MODULE
2024 || sym->attr.proc == PROC_ST_FUNCTION
2025 || sym->attr.proc == PROC_INTERNAL)
2026 goto found;
2028 if (sym->attr.intrinsic)
2030 m = gfc_intrinsic_func_interface (expr, 1);
2031 if (m == MATCH_YES)
2032 return MATCH_YES;
2033 if (m == MATCH_NO)
2034 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2035 "with an intrinsic", sym->name, &expr->where);
2037 return MATCH_ERROR;
2040 return MATCH_NO;
2042 found:
2043 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2045 if (sym->result)
2046 expr->ts = sym->result->ts;
2047 else
2048 expr->ts = sym->ts;
2049 expr->value.function.name = sym->name;
2050 expr->value.function.esym = sym;
2051 if (sym->as != NULL)
2052 expr->rank = sym->as->rank;
2054 return MATCH_YES;
2058 static gfc_try
2059 resolve_specific_f (gfc_expr *expr)
2061 gfc_symbol *sym;
2062 match m;
2064 sym = expr->symtree->n.sym;
2066 for (;;)
2068 m = resolve_specific_f0 (sym, expr);
2069 if (m == MATCH_YES)
2070 return SUCCESS;
2071 if (m == MATCH_ERROR)
2072 return FAILURE;
2074 if (sym->ns->parent == NULL)
2075 break;
2077 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2079 if (sym == NULL)
2080 break;
2083 gfc_error ("Unable to resolve the specific function '%s' at %L",
2084 expr->symtree->n.sym->name, &expr->where);
2086 return SUCCESS;
2090 /* Resolve a procedure call not known to be generic nor specific. */
2092 static gfc_try
2093 resolve_unknown_f (gfc_expr *expr)
2095 gfc_symbol *sym;
2096 gfc_typespec *ts;
2098 sym = expr->symtree->n.sym;
2100 if (sym->attr.dummy)
2102 sym->attr.proc = PROC_DUMMY;
2103 expr->value.function.name = sym->name;
2104 goto set_type;
2107 /* See if we have an intrinsic function reference. */
2109 if (gfc_is_intrinsic (sym, 0, expr->where))
2111 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2112 return SUCCESS;
2113 return FAILURE;
2116 /* The reference is to an external name. */
2118 sym->attr.proc = PROC_EXTERNAL;
2119 expr->value.function.name = sym->name;
2120 expr->value.function.esym = expr->symtree->n.sym;
2122 if (sym->as != NULL)
2123 expr->rank = sym->as->rank;
2125 /* Type of the expression is either the type of the symbol or the
2126 default type of the symbol. */
2128 set_type:
2129 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2131 if (sym->ts.type != BT_UNKNOWN)
2132 expr->ts = sym->ts;
2133 else
2135 ts = gfc_get_default_type (sym->name, sym->ns);
2137 if (ts->type == BT_UNKNOWN)
2139 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2140 sym->name, &expr->where);
2141 return FAILURE;
2143 else
2144 expr->ts = *ts;
2147 return SUCCESS;
2151 /* Return true, if the symbol is an external procedure. */
2152 static bool
2153 is_external_proc (gfc_symbol *sym)
2155 if (!sym->attr.dummy && !sym->attr.contained
2156 && !(sym->attr.intrinsic
2157 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2158 && sym->attr.proc != PROC_ST_FUNCTION
2159 && !sym->attr.use_assoc
2160 && sym->name)
2161 return true;
2163 return false;
2167 /* Figure out if a function reference is pure or not. Also set the name
2168 of the function for a potential error message. Return nonzero if the
2169 function is PURE, zero if not. */
2170 static int
2171 pure_stmt_function (gfc_expr *, gfc_symbol *);
2173 static int
2174 pure_function (gfc_expr *e, const char **name)
2176 int pure;
2178 *name = NULL;
2180 if (e->symtree != NULL
2181 && e->symtree->n.sym != NULL
2182 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2183 return pure_stmt_function (e, e->symtree->n.sym);
2185 if (e->value.function.esym)
2187 pure = gfc_pure (e->value.function.esym);
2188 *name = e->value.function.esym->name;
2190 else if (e->value.function.isym)
2192 pure = e->value.function.isym->pure
2193 || e->value.function.isym->elemental;
2194 *name = e->value.function.isym->name;
2196 else
2198 /* Implicit functions are not pure. */
2199 pure = 0;
2200 *name = e->value.function.name;
2203 return pure;
2207 static bool
2208 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2209 int *f ATTRIBUTE_UNUSED)
2211 const char *name;
2213 /* Don't bother recursing into other statement functions
2214 since they will be checked individually for purity. */
2215 if (e->expr_type != EXPR_FUNCTION
2216 || !e->symtree
2217 || e->symtree->n.sym == sym
2218 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2219 return false;
2221 return pure_function (e, &name) ? false : true;
2225 static int
2226 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2228 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2232 static gfc_try
2233 is_scalar_expr_ptr (gfc_expr *expr)
2235 gfc_try retval = SUCCESS;
2236 gfc_ref *ref;
2237 int start;
2238 int end;
2240 /* See if we have a gfc_ref, which means we have a substring, array
2241 reference, or a component. */
2242 if (expr->ref != NULL)
2244 ref = expr->ref;
2245 while (ref->next != NULL)
2246 ref = ref->next;
2248 switch (ref->type)
2250 case REF_SUBSTRING:
2251 if (ref->u.ss.length != NULL
2252 && ref->u.ss.length->length != NULL
2253 && ref->u.ss.start
2254 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2255 && ref->u.ss.end
2256 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2258 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2259 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2260 if (end - start + 1 != 1)
2261 retval = FAILURE;
2263 else
2264 retval = FAILURE;
2265 break;
2266 case REF_ARRAY:
2267 if (ref->u.ar.type == AR_ELEMENT)
2268 retval = SUCCESS;
2269 else if (ref->u.ar.type == AR_FULL)
2271 /* The user can give a full array if the array is of size 1. */
2272 if (ref->u.ar.as != NULL
2273 && ref->u.ar.as->rank == 1
2274 && ref->u.ar.as->type == AS_EXPLICIT
2275 && ref->u.ar.as->lower[0] != NULL
2276 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2277 && ref->u.ar.as->upper[0] != NULL
2278 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2280 /* If we have a character string, we need to check if
2281 its length is one. */
2282 if (expr->ts.type == BT_CHARACTER)
2284 if (expr->ts.u.cl == NULL
2285 || expr->ts.u.cl->length == NULL
2286 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2287 != 0)
2288 retval = FAILURE;
2290 else
2292 /* We have constant lower and upper bounds. If the
2293 difference between is 1, it can be considered a
2294 scalar. */
2295 start = (int) mpz_get_si
2296 (ref->u.ar.as->lower[0]->value.integer);
2297 end = (int) mpz_get_si
2298 (ref->u.ar.as->upper[0]->value.integer);
2299 if (end - start + 1 != 1)
2300 retval = FAILURE;
2303 else
2304 retval = FAILURE;
2306 else
2307 retval = FAILURE;
2308 break;
2309 default:
2310 retval = SUCCESS;
2311 break;
2314 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2316 /* Character string. Make sure it's of length 1. */
2317 if (expr->ts.u.cl == NULL
2318 || expr->ts.u.cl->length == NULL
2319 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2320 retval = FAILURE;
2322 else if (expr->rank != 0)
2323 retval = FAILURE;
2325 return retval;
2329 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2330 and, in the case of c_associated, set the binding label based on
2331 the arguments. */
2333 static gfc_try
2334 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2335 gfc_symbol **new_sym)
2337 char name[GFC_MAX_SYMBOL_LEN + 1];
2338 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2339 int optional_arg = 0, is_pointer = 0;
2340 gfc_try retval = SUCCESS;
2341 gfc_symbol *args_sym;
2342 gfc_typespec *arg_ts;
2344 if (args->expr->expr_type == EXPR_CONSTANT
2345 || args->expr->expr_type == EXPR_OP
2346 || args->expr->expr_type == EXPR_NULL)
2348 gfc_error ("Argument to '%s' at %L is not a variable",
2349 sym->name, &(args->expr->where));
2350 return FAILURE;
2353 args_sym = args->expr->symtree->n.sym;
2355 /* The typespec for the actual arg should be that stored in the expr
2356 and not necessarily that of the expr symbol (args_sym), because
2357 the actual expression could be a part-ref of the expr symbol. */
2358 arg_ts = &(args->expr->ts);
2360 is_pointer = gfc_is_data_pointer (args->expr);
2362 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2364 /* If the user gave two args then they are providing something for
2365 the optional arg (the second cptr). Therefore, set the name and
2366 binding label to the c_associated for two cptrs. Otherwise,
2367 set c_associated to expect one cptr. */
2368 if (args->next)
2370 /* two args. */
2371 sprintf (name, "%s_2", sym->name);
2372 sprintf (binding_label, "%s_2", sym->binding_label);
2373 optional_arg = 1;
2375 else
2377 /* one arg. */
2378 sprintf (name, "%s_1", sym->name);
2379 sprintf (binding_label, "%s_1", sym->binding_label);
2380 optional_arg = 0;
2383 /* Get a new symbol for the version of c_associated that
2384 will get called. */
2385 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2387 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2388 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2390 sprintf (name, "%s", sym->name);
2391 sprintf (binding_label, "%s", sym->binding_label);
2393 /* Error check the call. */
2394 if (args->next != NULL)
2396 gfc_error_now ("More actual than formal arguments in '%s' "
2397 "call at %L", name, &(args->expr->where));
2398 retval = FAILURE;
2400 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2402 /* Make sure we have either the target or pointer attribute. */
2403 if (!args_sym->attr.target && !is_pointer)
2405 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2406 "a TARGET or an associated pointer",
2407 args_sym->name,
2408 sym->name, &(args->expr->where));
2409 retval = FAILURE;
2412 /* See if we have interoperable type and type param. */
2413 if (verify_c_interop (arg_ts) == SUCCESS
2414 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2416 if (args_sym->attr.target == 1)
2418 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2419 has the target attribute and is interoperable. */
2420 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2421 allocatable variable that has the TARGET attribute and
2422 is not an array of zero size. */
2423 if (args_sym->attr.allocatable == 1)
2425 if (args_sym->attr.dimension != 0
2426 && (args_sym->as && args_sym->as->rank == 0))
2428 gfc_error_now ("Allocatable variable '%s' used as a "
2429 "parameter to '%s' at %L must not be "
2430 "an array of zero size",
2431 args_sym->name, sym->name,
2432 &(args->expr->where));
2433 retval = FAILURE;
2436 else
2438 /* A non-allocatable target variable with C
2439 interoperable type and type parameters must be
2440 interoperable. */
2441 if (args_sym && args_sym->attr.dimension)
2443 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2445 gfc_error ("Assumed-shape array '%s' at %L "
2446 "cannot be an argument to the "
2447 "procedure '%s' because "
2448 "it is not C interoperable",
2449 args_sym->name,
2450 &(args->expr->where), sym->name);
2451 retval = FAILURE;
2453 else if (args_sym->as->type == AS_DEFERRED)
2455 gfc_error ("Deferred-shape array '%s' at %L "
2456 "cannot be an argument to the "
2457 "procedure '%s' because "
2458 "it is not C interoperable",
2459 args_sym->name,
2460 &(args->expr->where), sym->name);
2461 retval = FAILURE;
2465 /* Make sure it's not a character string. Arrays of
2466 any type should be ok if the variable is of a C
2467 interoperable type. */
2468 if (arg_ts->type == BT_CHARACTER)
2469 if (arg_ts->u.cl != NULL
2470 && (arg_ts->u.cl->length == NULL
2471 || arg_ts->u.cl->length->expr_type
2472 != EXPR_CONSTANT
2473 || mpz_cmp_si
2474 (arg_ts->u.cl->length->value.integer, 1)
2475 != 0)
2476 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2478 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2479 "at %L must have a length of 1",
2480 args_sym->name, sym->name,
2481 &(args->expr->where));
2482 retval = FAILURE;
2486 else if (is_pointer
2487 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2489 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2490 scalar pointer. */
2491 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2492 "associated scalar POINTER", args_sym->name,
2493 sym->name, &(args->expr->where));
2494 retval = FAILURE;
2497 else
2499 /* The parameter is not required to be C interoperable. If it
2500 is not C interoperable, it must be a nonpolymorphic scalar
2501 with no length type parameters. It still must have either
2502 the pointer or target attribute, and it can be
2503 allocatable (but must be allocated when c_loc is called). */
2504 if (args->expr->rank != 0
2505 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2507 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2508 "scalar", args_sym->name, sym->name,
2509 &(args->expr->where));
2510 retval = FAILURE;
2512 else if (arg_ts->type == BT_CHARACTER
2513 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2515 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2516 "%L must have a length of 1",
2517 args_sym->name, sym->name,
2518 &(args->expr->where));
2519 retval = FAILURE;
2523 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2525 if (args_sym->attr.flavor != FL_PROCEDURE)
2527 /* TODO: Update this error message to allow for procedure
2528 pointers once they are implemented. */
2529 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2530 "procedure",
2531 args_sym->name, sym->name,
2532 &(args->expr->where));
2533 retval = FAILURE;
2535 else if (args_sym->attr.is_bind_c != 1)
2537 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2538 "BIND(C)",
2539 args_sym->name, sym->name,
2540 &(args->expr->where));
2541 retval = FAILURE;
2545 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2546 *new_sym = sym;
2548 else
2550 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2551 "iso_c_binding function: '%s'!\n", sym->name);
2554 return retval;
2558 /* Resolve a function call, which means resolving the arguments, then figuring
2559 out which entity the name refers to. */
2560 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2561 to INTENT(OUT) or INTENT(INOUT). */
2563 static gfc_try
2564 resolve_function (gfc_expr *expr)
2566 gfc_actual_arglist *arg;
2567 gfc_symbol *sym;
2568 const char *name;
2569 gfc_try t;
2570 int temp;
2571 procedure_type p = PROC_INTRINSIC;
2572 bool no_formal_args;
2574 sym = NULL;
2575 if (expr->symtree)
2576 sym = expr->symtree->n.sym;
2578 /* If this is a procedure pointer component, it has already been resolved. */
2579 if (gfc_is_proc_ptr_comp (expr, NULL))
2580 return SUCCESS;
2582 if (sym && sym->attr.intrinsic
2583 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2584 return FAILURE;
2586 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2588 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2589 return FAILURE;
2592 /* If this ia a deferred TBP with an abstract interface (which may
2593 of course be referenced), expr->value.function.esym will be set. */
2594 if (sym && sym->attr.abstract && !expr->value.function.esym)
2596 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2597 sym->name, &expr->where);
2598 return FAILURE;
2601 /* Switch off assumed size checking and do this again for certain kinds
2602 of procedure, once the procedure itself is resolved. */
2603 need_full_assumed_size++;
2605 if (expr->symtree && expr->symtree->n.sym)
2606 p = expr->symtree->n.sym->attr.proc;
2608 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2609 inquiry_argument = true;
2610 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2612 if (resolve_actual_arglist (expr->value.function.actual,
2613 p, no_formal_args) == FAILURE)
2615 inquiry_argument = false;
2616 return FAILURE;
2619 inquiry_argument = false;
2621 /* Need to setup the call to the correct c_associated, depending on
2622 the number of cptrs to user gives to compare. */
2623 if (sym && sym->attr.is_iso_c == 1)
2625 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2626 == FAILURE)
2627 return FAILURE;
2629 /* Get the symtree for the new symbol (resolved func).
2630 the old one will be freed later, when it's no longer used. */
2631 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2634 /* Resume assumed_size checking. */
2635 need_full_assumed_size--;
2637 /* If the procedure is external, check for usage. */
2638 if (sym && is_external_proc (sym))
2639 resolve_global_procedure (sym, &expr->where,
2640 &expr->value.function.actual, 0);
2642 if (sym && sym->ts.type == BT_CHARACTER
2643 && sym->ts.u.cl
2644 && sym->ts.u.cl->length == NULL
2645 && !sym->attr.dummy
2646 && expr->value.function.esym == NULL
2647 && !sym->attr.contained)
2649 /* Internal procedures are taken care of in resolve_contained_fntype. */
2650 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2651 "be used at %L since it is not a dummy argument",
2652 sym->name, &expr->where);
2653 return FAILURE;
2656 /* See if function is already resolved. */
2658 if (expr->value.function.name != NULL)
2660 if (expr->ts.type == BT_UNKNOWN)
2661 expr->ts = sym->ts;
2662 t = SUCCESS;
2664 else
2666 /* Apply the rules of section 14.1.2. */
2668 switch (procedure_kind (sym))
2670 case PTYPE_GENERIC:
2671 t = resolve_generic_f (expr);
2672 break;
2674 case PTYPE_SPECIFIC:
2675 t = resolve_specific_f (expr);
2676 break;
2678 case PTYPE_UNKNOWN:
2679 t = resolve_unknown_f (expr);
2680 break;
2682 default:
2683 gfc_internal_error ("resolve_function(): bad function type");
2687 /* If the expression is still a function (it might have simplified),
2688 then we check to see if we are calling an elemental function. */
2690 if (expr->expr_type != EXPR_FUNCTION)
2691 return t;
2693 temp = need_full_assumed_size;
2694 need_full_assumed_size = 0;
2696 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2697 return FAILURE;
2699 if (omp_workshare_flag
2700 && expr->value.function.esym
2701 && ! gfc_elemental (expr->value.function.esym))
2703 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2704 "in WORKSHARE construct", expr->value.function.esym->name,
2705 &expr->where);
2706 t = FAILURE;
2709 #define GENERIC_ID expr->value.function.isym->id
2710 else if (expr->value.function.actual != NULL
2711 && expr->value.function.isym != NULL
2712 && GENERIC_ID != GFC_ISYM_LBOUND
2713 && GENERIC_ID != GFC_ISYM_LEN
2714 && GENERIC_ID != GFC_ISYM_LOC
2715 && GENERIC_ID != GFC_ISYM_PRESENT)
2717 /* Array intrinsics must also have the last upper bound of an
2718 assumed size array argument. UBOUND and SIZE have to be
2719 excluded from the check if the second argument is anything
2720 than a constant. */
2722 for (arg = expr->value.function.actual; arg; arg = arg->next)
2724 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2725 && arg->next != NULL && arg->next->expr)
2727 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2728 break;
2730 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2731 break;
2733 if ((int)mpz_get_si (arg->next->expr->value.integer)
2734 < arg->expr->rank)
2735 break;
2738 if (arg->expr != NULL
2739 && arg->expr->rank > 0
2740 && resolve_assumed_size_actual (arg->expr))
2741 return FAILURE;
2744 #undef GENERIC_ID
2746 need_full_assumed_size = temp;
2747 name = NULL;
2749 if (!pure_function (expr, &name) && name)
2751 if (forall_flag)
2753 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2754 "FORALL %s", name, &expr->where,
2755 forall_flag == 2 ? "mask" : "block");
2756 t = FAILURE;
2758 else if (gfc_pure (NULL))
2760 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2761 "procedure within a PURE procedure", name, &expr->where);
2762 t = FAILURE;
2766 /* Functions without the RECURSIVE attribution are not allowed to
2767 * call themselves. */
2768 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2770 gfc_symbol *esym;
2771 esym = expr->value.function.esym;
2773 if (is_illegal_recursion (esym, gfc_current_ns))
2775 if (esym->attr.entry && esym->ns->entries)
2776 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2777 " function '%s' is not RECURSIVE",
2778 esym->name, &expr->where, esym->ns->entries->sym->name);
2779 else
2780 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2781 " is not RECURSIVE", esym->name, &expr->where);
2783 t = FAILURE;
2787 /* Character lengths of use associated functions may contains references to
2788 symbols not referenced from the current program unit otherwise. Make sure
2789 those symbols are marked as referenced. */
2791 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2792 && expr->value.function.esym->attr.use_assoc)
2794 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
2797 if (t == SUCCESS
2798 && !((expr->value.function.esym
2799 && expr->value.function.esym->attr.elemental)
2801 (expr->value.function.isym
2802 && expr->value.function.isym->elemental)))
2803 find_noncopying_intrinsics (expr->value.function.esym,
2804 expr->value.function.actual);
2806 /* Make sure that the expression has a typespec that works. */
2807 if (expr->ts.type == BT_UNKNOWN)
2809 if (expr->symtree->n.sym->result
2810 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
2811 && !expr->symtree->n.sym->result->attr.proc_pointer)
2812 expr->ts = expr->symtree->n.sym->result->ts;
2815 return t;
2819 /************* Subroutine resolution *************/
2821 static void
2822 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2824 if (gfc_pure (sym))
2825 return;
2827 if (forall_flag)
2828 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2829 sym->name, &c->loc);
2830 else if (gfc_pure (NULL))
2831 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2832 &c->loc);
2836 static match
2837 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2839 gfc_symbol *s;
2841 if (sym->attr.generic)
2843 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2844 if (s != NULL)
2846 c->resolved_sym = s;
2847 pure_subroutine (c, s);
2848 return MATCH_YES;
2851 /* TODO: Need to search for elemental references in generic interface. */
2854 if (sym->attr.intrinsic)
2855 return gfc_intrinsic_sub_interface (c, 0);
2857 return MATCH_NO;
2861 static gfc_try
2862 resolve_generic_s (gfc_code *c)
2864 gfc_symbol *sym;
2865 match m;
2867 sym = c->symtree->n.sym;
2869 for (;;)
2871 m = resolve_generic_s0 (c, sym);
2872 if (m == MATCH_YES)
2873 return SUCCESS;
2874 else if (m == MATCH_ERROR)
2875 return FAILURE;
2877 generic:
2878 if (sym->ns->parent == NULL)
2879 break;
2880 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2882 if (sym == NULL)
2883 break;
2884 if (!generic_sym (sym))
2885 goto generic;
2888 /* Last ditch attempt. See if the reference is to an intrinsic
2889 that possesses a matching interface. 14.1.2.4 */
2890 sym = c->symtree->n.sym;
2892 if (!gfc_is_intrinsic (sym, 1, c->loc))
2894 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2895 sym->name, &c->loc);
2896 return FAILURE;
2899 m = gfc_intrinsic_sub_interface (c, 0);
2900 if (m == MATCH_YES)
2901 return SUCCESS;
2902 if (m == MATCH_NO)
2903 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2904 "intrinsic subroutine interface", sym->name, &c->loc);
2906 return FAILURE;
2910 /* Set the name and binding label of the subroutine symbol in the call
2911 expression represented by 'c' to include the type and kind of the
2912 second parameter. This function is for resolving the appropriate
2913 version of c_f_pointer() and c_f_procpointer(). For example, a
2914 call to c_f_pointer() for a default integer pointer could have a
2915 name of c_f_pointer_i4. If no second arg exists, which is an error
2916 for these two functions, it defaults to the generic symbol's name
2917 and binding label. */
2919 static void
2920 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2921 char *name, char *binding_label)
2923 gfc_expr *arg = NULL;
2924 char type;
2925 int kind;
2927 /* The second arg of c_f_pointer and c_f_procpointer determines
2928 the type and kind for the procedure name. */
2929 arg = c->ext.actual->next->expr;
2931 if (arg != NULL)
2933 /* Set up the name to have the given symbol's name,
2934 plus the type and kind. */
2935 /* a derived type is marked with the type letter 'u' */
2936 if (arg->ts.type == BT_DERIVED)
2938 type = 'd';
2939 kind = 0; /* set the kind as 0 for now */
2941 else
2943 type = gfc_type_letter (arg->ts.type);
2944 kind = arg->ts.kind;
2947 if (arg->ts.type == BT_CHARACTER)
2948 /* Kind info for character strings not needed. */
2949 kind = 0;
2951 sprintf (name, "%s_%c%d", sym->name, type, kind);
2952 /* Set up the binding label as the given symbol's label plus
2953 the type and kind. */
2954 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2956 else
2958 /* If the second arg is missing, set the name and label as
2959 was, cause it should at least be found, and the missing
2960 arg error will be caught by compare_parameters(). */
2961 sprintf (name, "%s", sym->name);
2962 sprintf (binding_label, "%s", sym->binding_label);
2965 return;
2969 /* Resolve a generic version of the iso_c_binding procedure given
2970 (sym) to the specific one based on the type and kind of the
2971 argument(s). Currently, this function resolves c_f_pointer() and
2972 c_f_procpointer based on the type and kind of the second argument
2973 (FPTR). Other iso_c_binding procedures aren't specially handled.
2974 Upon successfully exiting, c->resolved_sym will hold the resolved
2975 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2976 otherwise. */
2978 match
2979 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2981 gfc_symbol *new_sym;
2982 /* this is fine, since we know the names won't use the max */
2983 char name[GFC_MAX_SYMBOL_LEN + 1];
2984 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2985 /* default to success; will override if find error */
2986 match m = MATCH_YES;
2988 /* Make sure the actual arguments are in the necessary order (based on the
2989 formal args) before resolving. */
2990 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2992 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2993 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2995 set_name_and_label (c, sym, name, binding_label);
2997 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2999 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
3001 /* Make sure we got a third arg if the second arg has non-zero
3002 rank. We must also check that the type and rank are
3003 correct since we short-circuit this check in
3004 gfc_procedure_use() (called above to sort actual args). */
3005 if (c->ext.actual->next->expr->rank != 0)
3007 if(c->ext.actual->next->next == NULL
3008 || c->ext.actual->next->next->expr == NULL)
3010 m = MATCH_ERROR;
3011 gfc_error ("Missing SHAPE parameter for call to %s "
3012 "at %L", sym->name, &(c->loc));
3014 else if (c->ext.actual->next->next->expr->ts.type
3015 != BT_INTEGER
3016 || c->ext.actual->next->next->expr->rank != 1)
3018 m = MATCH_ERROR;
3019 gfc_error ("SHAPE parameter for call to %s at %L must "
3020 "be a rank 1 INTEGER array", sym->name,
3021 &(c->loc));
3027 if (m != MATCH_ERROR)
3029 /* the 1 means to add the optional arg to formal list */
3030 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
3032 /* for error reporting, say it's declared where the original was */
3033 new_sym->declared_at = sym->declared_at;
3036 else
3038 /* no differences for c_loc or c_funloc */
3039 new_sym = sym;
3042 /* set the resolved symbol */
3043 if (m != MATCH_ERROR)
3044 c->resolved_sym = new_sym;
3045 else
3046 c->resolved_sym = sym;
3048 return m;
3052 /* Resolve a subroutine call known to be specific. */
3054 static match
3055 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3057 match m;
3059 if(sym->attr.is_iso_c)
3061 m = gfc_iso_c_sub_interface (c,sym);
3062 return m;
3065 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3067 if (sym->attr.dummy)
3069 sym->attr.proc = PROC_DUMMY;
3070 goto found;
3073 sym->attr.proc = PROC_EXTERNAL;
3074 goto found;
3077 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3078 goto found;
3080 if (sym->attr.intrinsic)
3082 m = gfc_intrinsic_sub_interface (c, 1);
3083 if (m == MATCH_YES)
3084 return MATCH_YES;
3085 if (m == MATCH_NO)
3086 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3087 "with an intrinsic", sym->name, &c->loc);
3089 return MATCH_ERROR;
3092 return MATCH_NO;
3094 found:
3095 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3097 c->resolved_sym = sym;
3098 pure_subroutine (c, sym);
3100 return MATCH_YES;
3104 static gfc_try
3105 resolve_specific_s (gfc_code *c)
3107 gfc_symbol *sym;
3108 match m;
3110 sym = c->symtree->n.sym;
3112 for (;;)
3114 m = resolve_specific_s0 (c, sym);
3115 if (m == MATCH_YES)
3116 return SUCCESS;
3117 if (m == MATCH_ERROR)
3118 return FAILURE;
3120 if (sym->ns->parent == NULL)
3121 break;
3123 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3125 if (sym == NULL)
3126 break;
3129 sym = c->symtree->n.sym;
3130 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3131 sym->name, &c->loc);
3133 return FAILURE;
3137 /* Resolve a subroutine call not known to be generic nor specific. */
3139 static gfc_try
3140 resolve_unknown_s (gfc_code *c)
3142 gfc_symbol *sym;
3144 sym = c->symtree->n.sym;
3146 if (sym->attr.dummy)
3148 sym->attr.proc = PROC_DUMMY;
3149 goto found;
3152 /* See if we have an intrinsic function reference. */
3154 if (gfc_is_intrinsic (sym, 1, c->loc))
3156 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3157 return SUCCESS;
3158 return FAILURE;
3161 /* The reference is to an external name. */
3163 found:
3164 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3166 c->resolved_sym = sym;
3168 pure_subroutine (c, sym);
3170 return SUCCESS;
3174 /* Resolve a subroutine call. Although it was tempting to use the same code
3175 for functions, subroutines and functions are stored differently and this
3176 makes things awkward. */
3178 static gfc_try
3179 resolve_call (gfc_code *c)
3181 gfc_try t;
3182 procedure_type ptype = PROC_INTRINSIC;
3183 gfc_symbol *csym, *sym;
3184 bool no_formal_args;
3186 csym = c->symtree ? c->symtree->n.sym : NULL;
3188 if (csym && csym->ts.type != BT_UNKNOWN)
3190 gfc_error ("'%s' at %L has a type, which is not consistent with "
3191 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3192 return FAILURE;
3195 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3197 gfc_symtree *st;
3198 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3199 sym = st ? st->n.sym : NULL;
3200 if (sym && csym != sym
3201 && sym->ns == gfc_current_ns
3202 && sym->attr.flavor == FL_PROCEDURE
3203 && sym->attr.contained)
3205 sym->refs++;
3206 if (csym->attr.generic)
3207 c->symtree->n.sym = sym;
3208 else
3209 c->symtree = st;
3210 csym = c->symtree->n.sym;
3214 /* If this ia a deferred TBP with an abstract interface
3215 (which may of course be referenced), c->expr1 will be set. */
3216 if (csym && csym->attr.abstract && !c->expr1)
3218 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3219 csym->name, &c->loc);
3220 return FAILURE;
3223 /* Subroutines without the RECURSIVE attribution are not allowed to
3224 * call themselves. */
3225 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3227 if (csym->attr.entry && csym->ns->entries)
3228 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3229 " subroutine '%s' is not RECURSIVE",
3230 csym->name, &c->loc, csym->ns->entries->sym->name);
3231 else
3232 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3233 " is not RECURSIVE", csym->name, &c->loc);
3235 t = FAILURE;
3238 /* Switch off assumed size checking and do this again for certain kinds
3239 of procedure, once the procedure itself is resolved. */
3240 need_full_assumed_size++;
3242 if (csym)
3243 ptype = csym->attr.proc;
3245 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3246 if (resolve_actual_arglist (c->ext.actual, ptype,
3247 no_formal_args) == FAILURE)
3248 return FAILURE;
3250 /* Resume assumed_size checking. */
3251 need_full_assumed_size--;
3253 /* If external, check for usage. */
3254 if (csym && is_external_proc (csym))
3255 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3257 t = SUCCESS;
3258 if (c->resolved_sym == NULL)
3260 c->resolved_isym = NULL;
3261 switch (procedure_kind (csym))
3263 case PTYPE_GENERIC:
3264 t = resolve_generic_s (c);
3265 break;
3267 case PTYPE_SPECIFIC:
3268 t = resolve_specific_s (c);
3269 break;
3271 case PTYPE_UNKNOWN:
3272 t = resolve_unknown_s (c);
3273 break;
3275 default:
3276 gfc_internal_error ("resolve_subroutine(): bad function type");
3280 /* Some checks of elemental subroutine actual arguments. */
3281 if (resolve_elemental_actual (NULL, c) == FAILURE)
3282 return FAILURE;
3284 if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
3285 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
3286 return t;
3290 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3291 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3292 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3293 if their shapes do not match. If either op1->shape or op2->shape is
3294 NULL, return SUCCESS. */
3296 static gfc_try
3297 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3299 gfc_try t;
3300 int i;
3302 t = SUCCESS;
3304 if (op1->shape != NULL && op2->shape != NULL)
3306 for (i = 0; i < op1->rank; i++)
3308 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3310 gfc_error ("Shapes for operands at %L and %L are not conformable",
3311 &op1->where, &op2->where);
3312 t = FAILURE;
3313 break;
3318 return t;
3322 /* Resolve an operator expression node. This can involve replacing the
3323 operation with a user defined function call. */
3325 static gfc_try
3326 resolve_operator (gfc_expr *e)
3328 gfc_expr *op1, *op2;
3329 char msg[200];
3330 bool dual_locus_error;
3331 gfc_try t;
3333 /* Resolve all subnodes-- give them types. */
3335 switch (e->value.op.op)
3337 default:
3338 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3339 return FAILURE;
3341 /* Fall through... */
3343 case INTRINSIC_NOT:
3344 case INTRINSIC_UPLUS:
3345 case INTRINSIC_UMINUS:
3346 case INTRINSIC_PARENTHESES:
3347 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3348 return FAILURE;
3349 break;
3352 /* Typecheck the new node. */
3354 op1 = e->value.op.op1;
3355 op2 = e->value.op.op2;
3356 dual_locus_error = false;
3358 if ((op1 && op1->expr_type == EXPR_NULL)
3359 || (op2 && op2->expr_type == EXPR_NULL))
3361 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3362 goto bad_op;
3365 switch (e->value.op.op)
3367 case INTRINSIC_UPLUS:
3368 case INTRINSIC_UMINUS:
3369 if (op1->ts.type == BT_INTEGER
3370 || op1->ts.type == BT_REAL
3371 || op1->ts.type == BT_COMPLEX)
3373 e->ts = op1->ts;
3374 break;
3377 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3378 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3379 goto bad_op;
3381 case INTRINSIC_PLUS:
3382 case INTRINSIC_MINUS:
3383 case INTRINSIC_TIMES:
3384 case INTRINSIC_DIVIDE:
3385 case INTRINSIC_POWER:
3386 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3388 gfc_type_convert_binary (e, 1);
3389 break;
3392 sprintf (msg,
3393 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3394 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3395 gfc_typename (&op2->ts));
3396 goto bad_op;
3398 case INTRINSIC_CONCAT:
3399 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3400 && op1->ts.kind == op2->ts.kind)
3402 e->ts.type = BT_CHARACTER;
3403 e->ts.kind = op1->ts.kind;
3404 break;
3407 sprintf (msg,
3408 _("Operands of string concatenation operator at %%L are %s/%s"),
3409 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3410 goto bad_op;
3412 case INTRINSIC_AND:
3413 case INTRINSIC_OR:
3414 case INTRINSIC_EQV:
3415 case INTRINSIC_NEQV:
3416 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3418 e->ts.type = BT_LOGICAL;
3419 e->ts.kind = gfc_kind_max (op1, op2);
3420 if (op1->ts.kind < e->ts.kind)
3421 gfc_convert_type (op1, &e->ts, 2);
3422 else if (op2->ts.kind < e->ts.kind)
3423 gfc_convert_type (op2, &e->ts, 2);
3424 break;
3427 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3428 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3429 gfc_typename (&op2->ts));
3431 goto bad_op;
3433 case INTRINSIC_NOT:
3434 if (op1->ts.type == BT_LOGICAL)
3436 e->ts.type = BT_LOGICAL;
3437 e->ts.kind = op1->ts.kind;
3438 break;
3441 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3442 gfc_typename (&op1->ts));
3443 goto bad_op;
3445 case INTRINSIC_GT:
3446 case INTRINSIC_GT_OS:
3447 case INTRINSIC_GE:
3448 case INTRINSIC_GE_OS:
3449 case INTRINSIC_LT:
3450 case INTRINSIC_LT_OS:
3451 case INTRINSIC_LE:
3452 case INTRINSIC_LE_OS:
3453 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3455 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3456 goto bad_op;
3459 /* Fall through... */
3461 case INTRINSIC_EQ:
3462 case INTRINSIC_EQ_OS:
3463 case INTRINSIC_NE:
3464 case INTRINSIC_NE_OS:
3465 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3466 && op1->ts.kind == op2->ts.kind)
3468 e->ts.type = BT_LOGICAL;
3469 e->ts.kind = gfc_default_logical_kind;
3470 break;
3473 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3475 gfc_type_convert_binary (e, 1);
3477 e->ts.type = BT_LOGICAL;
3478 e->ts.kind = gfc_default_logical_kind;
3479 break;
3482 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3483 sprintf (msg,
3484 _("Logicals at %%L must be compared with %s instead of %s"),
3485 (e->value.op.op == INTRINSIC_EQ
3486 || e->value.op.op == INTRINSIC_EQ_OS)
3487 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3488 else
3489 sprintf (msg,
3490 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3491 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3492 gfc_typename (&op2->ts));
3494 goto bad_op;
3496 case INTRINSIC_USER:
3497 if (e->value.op.uop->op == NULL)
3498 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3499 else if (op2 == NULL)
3500 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3501 e->value.op.uop->name, gfc_typename (&op1->ts));
3502 else
3503 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3504 e->value.op.uop->name, gfc_typename (&op1->ts),
3505 gfc_typename (&op2->ts));
3507 goto bad_op;
3509 case INTRINSIC_PARENTHESES:
3510 e->ts = op1->ts;
3511 if (e->ts.type == BT_CHARACTER)
3512 e->ts.u.cl = op1->ts.u.cl;
3513 break;
3515 default:
3516 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3519 /* Deal with arrayness of an operand through an operator. */
3521 t = SUCCESS;
3523 switch (e->value.op.op)
3525 case INTRINSIC_PLUS:
3526 case INTRINSIC_MINUS:
3527 case INTRINSIC_TIMES:
3528 case INTRINSIC_DIVIDE:
3529 case INTRINSIC_POWER:
3530 case INTRINSIC_CONCAT:
3531 case INTRINSIC_AND:
3532 case INTRINSIC_OR:
3533 case INTRINSIC_EQV:
3534 case INTRINSIC_NEQV:
3535 case INTRINSIC_EQ:
3536 case INTRINSIC_EQ_OS:
3537 case INTRINSIC_NE:
3538 case INTRINSIC_NE_OS:
3539 case INTRINSIC_GT:
3540 case INTRINSIC_GT_OS:
3541 case INTRINSIC_GE:
3542 case INTRINSIC_GE_OS:
3543 case INTRINSIC_LT:
3544 case INTRINSIC_LT_OS:
3545 case INTRINSIC_LE:
3546 case INTRINSIC_LE_OS:
3548 if (op1->rank == 0 && op2->rank == 0)
3549 e->rank = 0;
3551 if (op1->rank == 0 && op2->rank != 0)
3553 e->rank = op2->rank;
3555 if (e->shape == NULL)
3556 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3559 if (op1->rank != 0 && op2->rank == 0)
3561 e->rank = op1->rank;
3563 if (e->shape == NULL)
3564 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3567 if (op1->rank != 0 && op2->rank != 0)
3569 if (op1->rank == op2->rank)
3571 e->rank = op1->rank;
3572 if (e->shape == NULL)
3574 t = compare_shapes(op1, op2);
3575 if (t == FAILURE)
3576 e->shape = NULL;
3577 else
3578 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3581 else
3583 /* Allow higher level expressions to work. */
3584 e->rank = 0;
3586 /* Try user-defined operators, and otherwise throw an error. */
3587 dual_locus_error = true;
3588 sprintf (msg,
3589 _("Inconsistent ranks for operator at %%L and %%L"));
3590 goto bad_op;
3594 break;
3596 case INTRINSIC_PARENTHESES:
3597 case INTRINSIC_NOT:
3598 case INTRINSIC_UPLUS:
3599 case INTRINSIC_UMINUS:
3600 /* Simply copy arrayness attribute */
3601 e->rank = op1->rank;
3603 if (e->shape == NULL)
3604 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3606 break;
3608 default:
3609 break;
3612 /* Attempt to simplify the expression. */
3613 if (t == SUCCESS)
3615 t = gfc_simplify_expr (e, 0);
3616 /* Some calls do not succeed in simplification and return FAILURE
3617 even though there is no error; e.g. variable references to
3618 PARAMETER arrays. */
3619 if (!gfc_is_constant_expr (e))
3620 t = SUCCESS;
3622 return t;
3624 bad_op:
3627 bool real_error;
3628 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3629 return SUCCESS;
3631 if (real_error)
3632 return FAILURE;
3635 if (dual_locus_error)
3636 gfc_error (msg, &op1->where, &op2->where);
3637 else
3638 gfc_error (msg, &e->where);
3640 return FAILURE;
3644 /************** Array resolution subroutines **************/
3646 typedef enum
3647 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3648 comparison;
3650 /* Compare two integer expressions. */
3652 static comparison
3653 compare_bound (gfc_expr *a, gfc_expr *b)
3655 int i;
3657 if (a == NULL || a->expr_type != EXPR_CONSTANT
3658 || b == NULL || b->expr_type != EXPR_CONSTANT)
3659 return CMP_UNKNOWN;
3661 /* If either of the types isn't INTEGER, we must have
3662 raised an error earlier. */
3664 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3665 return CMP_UNKNOWN;
3667 i = mpz_cmp (a->value.integer, b->value.integer);
3669 if (i < 0)
3670 return CMP_LT;
3671 if (i > 0)
3672 return CMP_GT;
3673 return CMP_EQ;
3677 /* Compare an integer expression with an integer. */
3679 static comparison
3680 compare_bound_int (gfc_expr *a, int b)
3682 int i;
3684 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3685 return CMP_UNKNOWN;
3687 if (a->ts.type != BT_INTEGER)
3688 gfc_internal_error ("compare_bound_int(): Bad expression");
3690 i = mpz_cmp_si (a->value.integer, b);
3692 if (i < 0)
3693 return CMP_LT;
3694 if (i > 0)
3695 return CMP_GT;
3696 return CMP_EQ;
3700 /* Compare an integer expression with a mpz_t. */
3702 static comparison
3703 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3705 int i;
3707 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3708 return CMP_UNKNOWN;
3710 if (a->ts.type != BT_INTEGER)
3711 gfc_internal_error ("compare_bound_int(): Bad expression");
3713 i = mpz_cmp (a->value.integer, b);
3715 if (i < 0)
3716 return CMP_LT;
3717 if (i > 0)
3718 return CMP_GT;
3719 return CMP_EQ;
3723 /* Compute the last value of a sequence given by a triplet.
3724 Return 0 if it wasn't able to compute the last value, or if the
3725 sequence if empty, and 1 otherwise. */
3727 static int
3728 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3729 gfc_expr *stride, mpz_t last)
3731 mpz_t rem;
3733 if (start == NULL || start->expr_type != EXPR_CONSTANT
3734 || end == NULL || end->expr_type != EXPR_CONSTANT
3735 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3736 return 0;
3738 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3739 || (stride != NULL && stride->ts.type != BT_INTEGER))
3740 return 0;
3742 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3744 if (compare_bound (start, end) == CMP_GT)
3745 return 0;
3746 mpz_set (last, end->value.integer);
3747 return 1;
3750 if (compare_bound_int (stride, 0) == CMP_GT)
3752 /* Stride is positive */
3753 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3754 return 0;
3756 else
3758 /* Stride is negative */
3759 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3760 return 0;
3763 mpz_init (rem);
3764 mpz_sub (rem, end->value.integer, start->value.integer);
3765 mpz_tdiv_r (rem, rem, stride->value.integer);
3766 mpz_sub (last, end->value.integer, rem);
3767 mpz_clear (rem);
3769 return 1;
3773 /* Compare a single dimension of an array reference to the array
3774 specification. */
3776 static gfc_try
3777 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3779 mpz_t last_value;
3781 if (ar->dimen_type[i] == DIMEN_STAR)
3783 gcc_assert (ar->stride[i] == NULL);
3784 /* This implies [*] as [*:] and [*:3] are not possible. */
3785 if (ar->start[i] == NULL)
3787 gcc_assert (ar->end[i] == NULL);
3788 return SUCCESS;
3792 /* Given start, end and stride values, calculate the minimum and
3793 maximum referenced indexes. */
3795 switch (ar->dimen_type[i])
3797 case DIMEN_VECTOR:
3798 break;
3800 case DIMEN_STAR:
3801 case DIMEN_ELEMENT:
3802 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3804 if (i < as->rank)
3805 gfc_warning ("Array reference at %L is out of bounds "
3806 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3807 mpz_get_si (ar->start[i]->value.integer),
3808 mpz_get_si (as->lower[i]->value.integer), i+1);
3809 else
3810 gfc_warning ("Array reference at %L is out of bounds "
3811 "(%ld < %ld) in codimension %d", &ar->c_where[i],
3812 mpz_get_si (ar->start[i]->value.integer),
3813 mpz_get_si (as->lower[i]->value.integer),
3814 i + 1 - as->rank);
3815 return SUCCESS;
3817 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3819 if (i < as->rank)
3820 gfc_warning ("Array reference at %L is out of bounds "
3821 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3822 mpz_get_si (ar->start[i]->value.integer),
3823 mpz_get_si (as->upper[i]->value.integer), i+1);
3824 else
3825 gfc_warning ("Array reference at %L is out of bounds "
3826 "(%ld > %ld) in codimension %d", &ar->c_where[i],
3827 mpz_get_si (ar->start[i]->value.integer),
3828 mpz_get_si (as->upper[i]->value.integer),
3829 i + 1 - as->rank);
3830 return SUCCESS;
3833 break;
3835 case DIMEN_RANGE:
3837 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3838 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3840 comparison comp_start_end = compare_bound (AR_START, AR_END);
3842 /* Check for zero stride, which is not allowed. */
3843 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3845 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3846 return FAILURE;
3849 /* if start == len || (stride > 0 && start < len)
3850 || (stride < 0 && start > len),
3851 then the array section contains at least one element. In this
3852 case, there is an out-of-bounds access if
3853 (start < lower || start > upper). */
3854 if (compare_bound (AR_START, AR_END) == CMP_EQ
3855 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3856 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3857 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3858 && comp_start_end == CMP_GT))
3860 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3862 gfc_warning ("Lower array reference at %L is out of bounds "
3863 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3864 mpz_get_si (AR_START->value.integer),
3865 mpz_get_si (as->lower[i]->value.integer), i+1);
3866 return SUCCESS;
3868 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3870 gfc_warning ("Lower array reference at %L is out of bounds "
3871 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3872 mpz_get_si (AR_START->value.integer),
3873 mpz_get_si (as->upper[i]->value.integer), i+1);
3874 return SUCCESS;
3878 /* If we can compute the highest index of the array section,
3879 then it also has to be between lower and upper. */
3880 mpz_init (last_value);
3881 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3882 last_value))
3884 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3886 gfc_warning ("Upper array reference at %L is out of bounds "
3887 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3888 mpz_get_si (last_value),
3889 mpz_get_si (as->lower[i]->value.integer), i+1);
3890 mpz_clear (last_value);
3891 return SUCCESS;
3893 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3895 gfc_warning ("Upper array reference at %L is out of bounds "
3896 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3897 mpz_get_si (last_value),
3898 mpz_get_si (as->upper[i]->value.integer), i+1);
3899 mpz_clear (last_value);
3900 return SUCCESS;
3903 mpz_clear (last_value);
3905 #undef AR_START
3906 #undef AR_END
3908 break;
3910 default:
3911 gfc_internal_error ("check_dimension(): Bad array reference");
3914 return SUCCESS;
3918 /* Compare an array reference with an array specification. */
3920 static gfc_try
3921 compare_spec_to_ref (gfc_array_ref *ar)
3923 gfc_array_spec *as;
3924 int i;
3926 as = ar->as;
3927 i = as->rank - 1;
3928 /* TODO: Full array sections are only allowed as actual parameters. */
3929 if (as->type == AS_ASSUMED_SIZE
3930 && (/*ar->type == AR_FULL
3931 ||*/ (ar->type == AR_SECTION
3932 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3934 gfc_error ("Rightmost upper bound of assumed size array section "
3935 "not specified at %L", &ar->where);
3936 return FAILURE;
3939 if (ar->type == AR_FULL)
3940 return SUCCESS;
3942 if (as->rank != ar->dimen)
3944 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3945 &ar->where, ar->dimen, as->rank);
3946 return FAILURE;
3949 /* ar->codimen == 0 is a local array. */
3950 if (as->corank != ar->codimen && ar->codimen != 0)
3952 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
3953 &ar->where, ar->codimen, as->corank);
3954 return FAILURE;
3957 for (i = 0; i < as->rank; i++)
3958 if (check_dimension (i, ar, as) == FAILURE)
3959 return FAILURE;
3961 /* Local access has no coarray spec. */
3962 if (ar->codimen != 0)
3963 for (i = as->rank; i < as->rank + as->corank; i++)
3965 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate)
3967 gfc_error ("Coindex of codimension %d must be a scalar at %L",
3968 i + 1 - as->rank, &ar->where);
3969 return FAILURE;
3971 if (check_dimension (i, ar, as) == FAILURE)
3972 return FAILURE;
3975 return SUCCESS;
3979 /* Resolve one part of an array index. */
3981 static gfc_try
3982 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
3983 int force_index_integer_kind)
3985 gfc_typespec ts;
3987 if (index == NULL)
3988 return SUCCESS;
3990 if (gfc_resolve_expr (index) == FAILURE)
3991 return FAILURE;
3993 if (check_scalar && index->rank != 0)
3995 gfc_error ("Array index at %L must be scalar", &index->where);
3996 return FAILURE;
3999 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4001 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4002 &index->where, gfc_basic_typename (index->ts.type));
4003 return FAILURE;
4006 if (index->ts.type == BT_REAL)
4007 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
4008 &index->where) == FAILURE)
4009 return FAILURE;
4011 if ((index->ts.kind != gfc_index_integer_kind
4012 && force_index_integer_kind)
4013 || index->ts.type != BT_INTEGER)
4015 gfc_clear_ts (&ts);
4016 ts.type = BT_INTEGER;
4017 ts.kind = gfc_index_integer_kind;
4019 gfc_convert_type_warn (index, &ts, 2, 0);
4022 return SUCCESS;
4025 /* Resolve one part of an array index. */
4027 gfc_try
4028 gfc_resolve_index (gfc_expr *index, int check_scalar)
4030 return gfc_resolve_index_1 (index, check_scalar, 1);
4033 /* Resolve a dim argument to an intrinsic function. */
4035 gfc_try
4036 gfc_resolve_dim_arg (gfc_expr *dim)
4038 if (dim == NULL)
4039 return SUCCESS;
4041 if (gfc_resolve_expr (dim) == FAILURE)
4042 return FAILURE;
4044 if (dim->rank != 0)
4046 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4047 return FAILURE;
4051 if (dim->ts.type != BT_INTEGER)
4053 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4054 return FAILURE;
4057 if (dim->ts.kind != gfc_index_integer_kind)
4059 gfc_typespec ts;
4061 gfc_clear_ts (&ts);
4062 ts.type = BT_INTEGER;
4063 ts.kind = gfc_index_integer_kind;
4065 gfc_convert_type_warn (dim, &ts, 2, 0);
4068 return SUCCESS;
4071 /* Given an expression that contains array references, update those array
4072 references to point to the right array specifications. While this is
4073 filled in during matching, this information is difficult to save and load
4074 in a module, so we take care of it here.
4076 The idea here is that the original array reference comes from the
4077 base symbol. We traverse the list of reference structures, setting
4078 the stored reference to references. Component references can
4079 provide an additional array specification. */
4081 static void
4082 find_array_spec (gfc_expr *e)
4084 gfc_array_spec *as;
4085 gfc_component *c;
4086 gfc_symbol *derived;
4087 gfc_ref *ref;
4089 if (e->symtree->n.sym->ts.type == BT_CLASS)
4090 as = e->symtree->n.sym->ts.u.derived->components->as;
4091 else
4092 as = e->symtree->n.sym->as;
4093 derived = NULL;
4095 for (ref = e->ref; ref; ref = ref->next)
4096 switch (ref->type)
4098 case REF_ARRAY:
4099 if (as == NULL)
4100 gfc_internal_error ("find_array_spec(): Missing spec");
4102 ref->u.ar.as = as;
4103 as = NULL;
4104 break;
4106 case REF_COMPONENT:
4107 if (derived == NULL)
4108 derived = e->symtree->n.sym->ts.u.derived;
4110 if (derived->attr.is_class)
4111 derived = derived->components->ts.u.derived;
4113 c = derived->components;
4115 for (; c; c = c->next)
4116 if (c == ref->u.c.component)
4118 /* Track the sequence of component references. */
4119 if (c->ts.type == BT_DERIVED)
4120 derived = c->ts.u.derived;
4121 break;
4124 if (c == NULL)
4125 gfc_internal_error ("find_array_spec(): Component not found");
4127 if (c->attr.dimension)
4129 if (as != NULL)
4130 gfc_internal_error ("find_array_spec(): unused as(1)");
4131 as = c->as;
4134 break;
4136 case REF_SUBSTRING:
4137 break;
4140 if (as != NULL)
4141 gfc_internal_error ("find_array_spec(): unused as(2)");
4145 /* Resolve an array reference. */
4147 static gfc_try
4148 resolve_array_ref (gfc_array_ref *ar)
4150 int i, check_scalar;
4151 gfc_expr *e;
4153 for (i = 0; i < ar->dimen + ar->codimen; i++)
4155 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4157 /* Do not force gfc_index_integer_kind for the start. We can
4158 do fine with any integer kind. This avoids temporary arrays
4159 created for indexing with a vector. */
4160 if (gfc_resolve_index_1 (ar->start[i], check_scalar, 0) == FAILURE)
4161 return FAILURE;
4162 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4163 return FAILURE;
4164 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4165 return FAILURE;
4167 e = ar->start[i];
4169 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4170 switch (e->rank)
4172 case 0:
4173 ar->dimen_type[i] = DIMEN_ELEMENT;
4174 break;
4176 case 1:
4177 ar->dimen_type[i] = DIMEN_VECTOR;
4178 if (e->expr_type == EXPR_VARIABLE
4179 && e->symtree->n.sym->ts.type == BT_DERIVED)
4180 ar->start[i] = gfc_get_parentheses (e);
4181 break;
4183 default:
4184 gfc_error ("Array index at %L is an array of rank %d",
4185 &ar->c_where[i], e->rank);
4186 return FAILURE;
4190 if (ar->type == AR_FULL && ar->as->rank == 0)
4191 ar->type = AR_ELEMENT;
4193 /* If the reference type is unknown, figure out what kind it is. */
4195 if (ar->type == AR_UNKNOWN)
4197 ar->type = AR_ELEMENT;
4198 for (i = 0; i < ar->dimen; i++)
4199 if (ar->dimen_type[i] == DIMEN_RANGE
4200 || ar->dimen_type[i] == DIMEN_VECTOR)
4202 ar->type = AR_SECTION;
4203 break;
4207 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4208 return FAILURE;
4210 return SUCCESS;
4214 static gfc_try
4215 resolve_substring (gfc_ref *ref)
4217 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4219 if (ref->u.ss.start != NULL)
4221 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4222 return FAILURE;
4224 if (ref->u.ss.start->ts.type != BT_INTEGER)
4226 gfc_error ("Substring start index at %L must be of type INTEGER",
4227 &ref->u.ss.start->where);
4228 return FAILURE;
4231 if (ref->u.ss.start->rank != 0)
4233 gfc_error ("Substring start index at %L must be scalar",
4234 &ref->u.ss.start->where);
4235 return FAILURE;
4238 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4239 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4240 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4242 gfc_error ("Substring start index at %L is less than one",
4243 &ref->u.ss.start->where);
4244 return FAILURE;
4248 if (ref->u.ss.end != NULL)
4250 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4251 return FAILURE;
4253 if (ref->u.ss.end->ts.type != BT_INTEGER)
4255 gfc_error ("Substring end index at %L must be of type INTEGER",
4256 &ref->u.ss.end->where);
4257 return FAILURE;
4260 if (ref->u.ss.end->rank != 0)
4262 gfc_error ("Substring end index at %L must be scalar",
4263 &ref->u.ss.end->where);
4264 return FAILURE;
4267 if (ref->u.ss.length != NULL
4268 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4269 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4270 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4272 gfc_error ("Substring end index at %L exceeds the string length",
4273 &ref->u.ss.start->where);
4274 return FAILURE;
4277 if (compare_bound_mpz_t (ref->u.ss.end,
4278 gfc_integer_kinds[k].huge) == CMP_GT
4279 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4280 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4282 gfc_error ("Substring end index at %L is too large",
4283 &ref->u.ss.end->where);
4284 return FAILURE;
4288 return SUCCESS;
4292 /* This function supplies missing substring charlens. */
4294 void
4295 gfc_resolve_substring_charlen (gfc_expr *e)
4297 gfc_ref *char_ref;
4298 gfc_expr *start, *end;
4300 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4301 if (char_ref->type == REF_SUBSTRING)
4302 break;
4304 if (!char_ref)
4305 return;
4307 gcc_assert (char_ref->next == NULL);
4309 if (e->ts.u.cl)
4311 if (e->ts.u.cl->length)
4312 gfc_free_expr (e->ts.u.cl->length);
4313 else if (e->expr_type == EXPR_VARIABLE
4314 && e->symtree->n.sym->attr.dummy)
4315 return;
4318 e->ts.type = BT_CHARACTER;
4319 e->ts.kind = gfc_default_character_kind;
4321 if (!e->ts.u.cl)
4322 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4324 if (char_ref->u.ss.start)
4325 start = gfc_copy_expr (char_ref->u.ss.start);
4326 else
4327 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4329 if (char_ref->u.ss.end)
4330 end = gfc_copy_expr (char_ref->u.ss.end);
4331 else if (e->expr_type == EXPR_VARIABLE)
4332 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4333 else
4334 end = NULL;
4336 if (!start || !end)
4337 return;
4339 /* Length = (end - start +1). */
4340 e->ts.u.cl->length = gfc_subtract (end, start);
4341 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4342 gfc_get_int_expr (gfc_default_integer_kind,
4343 NULL, 1));
4345 e->ts.u.cl->length->ts.type = BT_INTEGER;
4346 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4348 /* Make sure that the length is simplified. */
4349 gfc_simplify_expr (e->ts.u.cl->length, 1);
4350 gfc_resolve_expr (e->ts.u.cl->length);
4354 /* Resolve subtype references. */
4356 static gfc_try
4357 resolve_ref (gfc_expr *expr)
4359 int current_part_dimension, n_components, seen_part_dimension;
4360 gfc_ref *ref;
4362 for (ref = expr->ref; ref; ref = ref->next)
4363 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4365 find_array_spec (expr);
4366 break;
4369 for (ref = expr->ref; ref; ref = ref->next)
4370 switch (ref->type)
4372 case REF_ARRAY:
4373 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4374 return FAILURE;
4375 break;
4377 case REF_COMPONENT:
4378 break;
4380 case REF_SUBSTRING:
4381 resolve_substring (ref);
4382 break;
4385 /* Check constraints on part references. */
4387 current_part_dimension = 0;
4388 seen_part_dimension = 0;
4389 n_components = 0;
4391 for (ref = expr->ref; ref; ref = ref->next)
4393 switch (ref->type)
4395 case REF_ARRAY:
4396 switch (ref->u.ar.type)
4398 case AR_FULL:
4399 /* Coarray scalar. */
4400 if (ref->u.ar.as->rank == 0)
4402 current_part_dimension = 0;
4403 break;
4405 /* Fall through. */
4406 case AR_SECTION:
4407 current_part_dimension = 1;
4408 break;
4410 case AR_ELEMENT:
4411 current_part_dimension = 0;
4412 break;
4414 case AR_UNKNOWN:
4415 gfc_internal_error ("resolve_ref(): Bad array reference");
4418 break;
4420 case REF_COMPONENT:
4421 if (current_part_dimension || seen_part_dimension)
4423 /* F03:C614. */
4424 if (ref->u.c.component->attr.pointer
4425 || ref->u.c.component->attr.proc_pointer)
4427 gfc_error ("Component to the right of a part reference "
4428 "with nonzero rank must not have the POINTER "
4429 "attribute at %L", &expr->where);
4430 return FAILURE;
4432 else if (ref->u.c.component->attr.allocatable)
4434 gfc_error ("Component to the right of a part reference "
4435 "with nonzero rank must not have the ALLOCATABLE "
4436 "attribute at %L", &expr->where);
4437 return FAILURE;
4441 n_components++;
4442 break;
4444 case REF_SUBSTRING:
4445 break;
4448 if (((ref->type == REF_COMPONENT && n_components > 1)
4449 || ref->next == NULL)
4450 && current_part_dimension
4451 && seen_part_dimension)
4453 gfc_error ("Two or more part references with nonzero rank must "
4454 "not be specified at %L", &expr->where);
4455 return FAILURE;
4458 if (ref->type == REF_COMPONENT)
4460 if (current_part_dimension)
4461 seen_part_dimension = 1;
4463 /* reset to make sure */
4464 current_part_dimension = 0;
4468 return SUCCESS;
4472 /* Given an expression, determine its shape. This is easier than it sounds.
4473 Leaves the shape array NULL if it is not possible to determine the shape. */
4475 static void
4476 expression_shape (gfc_expr *e)
4478 mpz_t array[GFC_MAX_DIMENSIONS];
4479 int i;
4481 if (e->rank == 0 || e->shape != NULL)
4482 return;
4484 for (i = 0; i < e->rank; i++)
4485 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4486 goto fail;
4488 e->shape = gfc_get_shape (e->rank);
4490 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4492 return;
4494 fail:
4495 for (i--; i >= 0; i--)
4496 mpz_clear (array[i]);
4500 /* Given a variable expression node, compute the rank of the expression by
4501 examining the base symbol and any reference structures it may have. */
4503 static void
4504 expression_rank (gfc_expr *e)
4506 gfc_ref *ref;
4507 int i, rank;
4509 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4510 could lead to serious confusion... */
4511 gcc_assert (e->expr_type != EXPR_COMPCALL);
4513 if (e->ref == NULL)
4515 if (e->expr_type == EXPR_ARRAY)
4516 goto done;
4517 /* Constructors can have a rank different from one via RESHAPE(). */
4519 if (e->symtree == NULL)
4521 e->rank = 0;
4522 goto done;
4525 e->rank = (e->symtree->n.sym->as == NULL)
4526 ? 0 : e->symtree->n.sym->as->rank;
4527 goto done;
4530 rank = 0;
4532 for (ref = e->ref; ref; ref = ref->next)
4534 if (ref->type != REF_ARRAY)
4535 continue;
4537 if (ref->u.ar.type == AR_FULL)
4539 rank = ref->u.ar.as->rank;
4540 break;
4543 if (ref->u.ar.type == AR_SECTION)
4545 /* Figure out the rank of the section. */
4546 if (rank != 0)
4547 gfc_internal_error ("expression_rank(): Two array specs");
4549 for (i = 0; i < ref->u.ar.dimen; i++)
4550 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4551 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4552 rank++;
4554 break;
4558 e->rank = rank;
4560 done:
4561 expression_shape (e);
4565 /* Resolve a variable expression. */
4567 static gfc_try
4568 resolve_variable (gfc_expr *e)
4570 gfc_symbol *sym;
4571 gfc_try t;
4573 t = SUCCESS;
4575 if (e->symtree == NULL)
4576 return FAILURE;
4578 if (e->ref && resolve_ref (e) == FAILURE)
4579 return FAILURE;
4581 sym = e->symtree->n.sym;
4582 if (sym->attr.flavor == FL_PROCEDURE
4583 && (!sym->attr.function
4584 || (sym->attr.function && sym->result
4585 && sym->result->attr.proc_pointer
4586 && !sym->result->attr.function)))
4588 e->ts.type = BT_PROCEDURE;
4589 goto resolve_procedure;
4592 if (sym->ts.type != BT_UNKNOWN)
4593 gfc_variable_attr (e, &e->ts);
4594 else
4596 /* Must be a simple variable reference. */
4597 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4598 return FAILURE;
4599 e->ts = sym->ts;
4602 if (check_assumed_size_reference (sym, e))
4603 return FAILURE;
4605 /* Deal with forward references to entries during resolve_code, to
4606 satisfy, at least partially, 12.5.2.5. */
4607 if (gfc_current_ns->entries
4608 && current_entry_id == sym->entry_id
4609 && cs_base
4610 && cs_base->current
4611 && cs_base->current->op != EXEC_ENTRY)
4613 gfc_entry_list *entry;
4614 gfc_formal_arglist *formal;
4615 int n;
4616 bool seen;
4618 /* If the symbol is a dummy... */
4619 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4621 entry = gfc_current_ns->entries;
4622 seen = false;
4624 /* ...test if the symbol is a parameter of previous entries. */
4625 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4626 for (formal = entry->sym->formal; formal; formal = formal->next)
4628 if (formal->sym && sym->name == formal->sym->name)
4629 seen = true;
4632 /* If it has not been seen as a dummy, this is an error. */
4633 if (!seen)
4635 if (specification_expr)
4636 gfc_error ("Variable '%s', used in a specification expression"
4637 ", is referenced at %L before the ENTRY statement "
4638 "in which it is a parameter",
4639 sym->name, &cs_base->current->loc);
4640 else
4641 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4642 "statement in which it is a parameter",
4643 sym->name, &cs_base->current->loc);
4644 t = FAILURE;
4648 /* Now do the same check on the specification expressions. */
4649 specification_expr = 1;
4650 if (sym->ts.type == BT_CHARACTER
4651 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
4652 t = FAILURE;
4654 if (sym->as)
4655 for (n = 0; n < sym->as->rank; n++)
4657 specification_expr = 1;
4658 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4659 t = FAILURE;
4660 specification_expr = 1;
4661 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4662 t = FAILURE;
4664 specification_expr = 0;
4666 if (t == SUCCESS)
4667 /* Update the symbol's entry level. */
4668 sym->entry_id = current_entry_id + 1;
4671 resolve_procedure:
4672 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
4673 t = FAILURE;
4675 /* F2008, C617 and C1229. */
4676 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
4677 && gfc_is_coindexed (e))
4679 gfc_ref *ref, *ref2 = NULL;
4681 if (e->ts.type == BT_CLASS)
4683 gfc_error ("Polymorphic subobject of coindexed object at %L",
4684 &e->where);
4685 t = FAILURE;
4688 for (ref = e->ref; ref; ref = ref->next)
4690 if (ref->type == REF_COMPONENT)
4691 ref2 = ref;
4692 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4693 break;
4696 for ( ; ref; ref = ref->next)
4697 if (ref->type == REF_COMPONENT)
4698 break;
4700 /* Expression itself is coindexed object. */
4701 if (ref == NULL)
4703 gfc_component *c;
4704 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
4705 for ( ; c; c = c->next)
4706 if (c->attr.allocatable && c->ts.type == BT_CLASS)
4708 gfc_error ("Coindexed object with polymorphic allocatable "
4709 "subcomponent at %L", &e->where);
4710 t = FAILURE;
4711 break;
4716 return t;
4720 /* Checks to see that the correct symbol has been host associated.
4721 The only situation where this arises is that in which a twice
4722 contained function is parsed after the host association is made.
4723 Therefore, on detecting this, change the symbol in the expression
4724 and convert the array reference into an actual arglist if the old
4725 symbol is a variable. */
4726 static bool
4727 check_host_association (gfc_expr *e)
4729 gfc_symbol *sym, *old_sym;
4730 gfc_symtree *st;
4731 int n;
4732 gfc_ref *ref;
4733 gfc_actual_arglist *arg, *tail = NULL;
4734 bool retval = e->expr_type == EXPR_FUNCTION;
4736 /* If the expression is the result of substitution in
4737 interface.c(gfc_extend_expr) because there is no way in
4738 which the host association can be wrong. */
4739 if (e->symtree == NULL
4740 || e->symtree->n.sym == NULL
4741 || e->user_operator)
4742 return retval;
4744 old_sym = e->symtree->n.sym;
4746 if (gfc_current_ns->parent
4747 && old_sym->ns != gfc_current_ns)
4749 /* Use the 'USE' name so that renamed module symbols are
4750 correctly handled. */
4751 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
4753 if (sym && old_sym != sym
4754 && sym->ts.type == old_sym->ts.type
4755 && sym->attr.flavor == FL_PROCEDURE
4756 && sym->attr.contained)
4758 /* Clear the shape, since it might not be valid. */
4759 if (e->shape != NULL)
4761 for (n = 0; n < e->rank; n++)
4762 mpz_clear (e->shape[n]);
4764 gfc_free (e->shape);
4767 /* Give the expression the right symtree! */
4768 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
4769 gcc_assert (st != NULL);
4771 if (old_sym->attr.flavor == FL_PROCEDURE
4772 || e->expr_type == EXPR_FUNCTION)
4774 /* Original was function so point to the new symbol, since
4775 the actual argument list is already attached to the
4776 expression. */
4777 e->value.function.esym = NULL;
4778 e->symtree = st;
4780 else
4782 /* Original was variable so convert array references into
4783 an actual arglist. This does not need any checking now
4784 since gfc_resolve_function will take care of it. */
4785 e->value.function.actual = NULL;
4786 e->expr_type = EXPR_FUNCTION;
4787 e->symtree = st;
4789 /* Ambiguity will not arise if the array reference is not
4790 the last reference. */
4791 for (ref = e->ref; ref; ref = ref->next)
4792 if (ref->type == REF_ARRAY && ref->next == NULL)
4793 break;
4795 gcc_assert (ref->type == REF_ARRAY);
4797 /* Grab the start expressions from the array ref and
4798 copy them into actual arguments. */
4799 for (n = 0; n < ref->u.ar.dimen; n++)
4801 arg = gfc_get_actual_arglist ();
4802 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4803 if (e->value.function.actual == NULL)
4804 tail = e->value.function.actual = arg;
4805 else
4807 tail->next = arg;
4808 tail = arg;
4812 /* Dump the reference list and set the rank. */
4813 gfc_free_ref_list (e->ref);
4814 e->ref = NULL;
4815 e->rank = sym->as ? sym->as->rank : 0;
4818 gfc_resolve_expr (e);
4819 sym->refs++;
4822 /* This might have changed! */
4823 return e->expr_type == EXPR_FUNCTION;
4827 static void
4828 gfc_resolve_character_operator (gfc_expr *e)
4830 gfc_expr *op1 = e->value.op.op1;
4831 gfc_expr *op2 = e->value.op.op2;
4832 gfc_expr *e1 = NULL;
4833 gfc_expr *e2 = NULL;
4835 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
4837 if (op1->ts.u.cl && op1->ts.u.cl->length)
4838 e1 = gfc_copy_expr (op1->ts.u.cl->length);
4839 else if (op1->expr_type == EXPR_CONSTANT)
4840 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
4841 op1->value.character.length);
4843 if (op2->ts.u.cl && op2->ts.u.cl->length)
4844 e2 = gfc_copy_expr (op2->ts.u.cl->length);
4845 else if (op2->expr_type == EXPR_CONSTANT)
4846 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
4847 op2->value.character.length);
4849 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4851 if (!e1 || !e2)
4852 return;
4854 e->ts.u.cl->length = gfc_add (e1, e2);
4855 e->ts.u.cl->length->ts.type = BT_INTEGER;
4856 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4857 gfc_simplify_expr (e->ts.u.cl->length, 0);
4858 gfc_resolve_expr (e->ts.u.cl->length);
4860 return;
4864 /* Ensure that an character expression has a charlen and, if possible, a
4865 length expression. */
4867 static void
4868 fixup_charlen (gfc_expr *e)
4870 /* The cases fall through so that changes in expression type and the need
4871 for multiple fixes are picked up. In all circumstances, a charlen should
4872 be available for the middle end to hang a backend_decl on. */
4873 switch (e->expr_type)
4875 case EXPR_OP:
4876 gfc_resolve_character_operator (e);
4878 case EXPR_ARRAY:
4879 if (e->expr_type == EXPR_ARRAY)
4880 gfc_resolve_character_array_constructor (e);
4882 case EXPR_SUBSTRING:
4883 if (!e->ts.u.cl && e->ref)
4884 gfc_resolve_substring_charlen (e);
4886 default:
4887 if (!e->ts.u.cl)
4888 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4890 break;
4895 /* Update an actual argument to include the passed-object for type-bound
4896 procedures at the right position. */
4898 static gfc_actual_arglist*
4899 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
4900 const char *name)
4902 gcc_assert (argpos > 0);
4904 if (argpos == 1)
4906 gfc_actual_arglist* result;
4908 result = gfc_get_actual_arglist ();
4909 result->expr = po;
4910 result->next = lst;
4911 if (name)
4912 result->name = name;
4914 return result;
4917 if (lst)
4918 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
4919 else
4920 lst = update_arglist_pass (NULL, po, argpos - 1, name);
4921 return lst;
4925 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
4927 static gfc_expr*
4928 extract_compcall_passed_object (gfc_expr* e)
4930 gfc_expr* po;
4932 gcc_assert (e->expr_type == EXPR_COMPCALL);
4934 if (e->value.compcall.base_object)
4935 po = gfc_copy_expr (e->value.compcall.base_object);
4936 else
4938 po = gfc_get_expr ();
4939 po->expr_type = EXPR_VARIABLE;
4940 po->symtree = e->symtree;
4941 po->ref = gfc_copy_ref (e->ref);
4942 po->where = e->where;
4945 if (gfc_resolve_expr (po) == FAILURE)
4946 return NULL;
4948 return po;
4952 /* Update the arglist of an EXPR_COMPCALL expression to include the
4953 passed-object. */
4955 static gfc_try
4956 update_compcall_arglist (gfc_expr* e)
4958 gfc_expr* po;
4959 gfc_typebound_proc* tbp;
4961 tbp = e->value.compcall.tbp;
4963 if (tbp->error)
4964 return FAILURE;
4966 po = extract_compcall_passed_object (e);
4967 if (!po)
4968 return FAILURE;
4970 if (tbp->nopass || e->value.compcall.ignore_pass)
4972 gfc_free_expr (po);
4973 return SUCCESS;
4976 gcc_assert (tbp->pass_arg_num > 0);
4977 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4978 tbp->pass_arg_num,
4979 tbp->pass_arg);
4981 return SUCCESS;
4985 /* Extract the passed object from a PPC call (a copy of it). */
4987 static gfc_expr*
4988 extract_ppc_passed_object (gfc_expr *e)
4990 gfc_expr *po;
4991 gfc_ref **ref;
4993 po = gfc_get_expr ();
4994 po->expr_type = EXPR_VARIABLE;
4995 po->symtree = e->symtree;
4996 po->ref = gfc_copy_ref (e->ref);
4997 po->where = e->where;
4999 /* Remove PPC reference. */
5000 ref = &po->ref;
5001 while ((*ref)->next)
5002 ref = &(*ref)->next;
5003 gfc_free_ref_list (*ref);
5004 *ref = NULL;
5006 if (gfc_resolve_expr (po) == FAILURE)
5007 return NULL;
5009 return po;
5013 /* Update the actual arglist of a procedure pointer component to include the
5014 passed-object. */
5016 static gfc_try
5017 update_ppc_arglist (gfc_expr* e)
5019 gfc_expr* po;
5020 gfc_component *ppc;
5021 gfc_typebound_proc* tb;
5023 if (!gfc_is_proc_ptr_comp (e, &ppc))
5024 return FAILURE;
5026 tb = ppc->tb;
5028 if (tb->error)
5029 return FAILURE;
5030 else if (tb->nopass)
5031 return SUCCESS;
5033 po = extract_ppc_passed_object (e);
5034 if (!po)
5035 return FAILURE;
5037 if (po->rank > 0)
5039 gfc_error ("Passed-object at %L must be scalar", &e->where);
5040 return FAILURE;
5043 gcc_assert (tb->pass_arg_num > 0);
5044 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5045 tb->pass_arg_num,
5046 tb->pass_arg);
5048 return SUCCESS;
5052 /* Check that the object a TBP is called on is valid, i.e. it must not be
5053 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5055 static gfc_try
5056 check_typebound_baseobject (gfc_expr* e)
5058 gfc_expr* base;
5060 base = extract_compcall_passed_object (e);
5061 if (!base)
5062 return FAILURE;
5064 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5066 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5068 gfc_error ("Base object for type-bound procedure call at %L is of"
5069 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5070 return FAILURE;
5073 /* If the procedure called is NOPASS, the base object must be scalar. */
5074 if (e->value.compcall.tbp->nopass && base->rank > 0)
5076 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5077 " be scalar", &e->where);
5078 return FAILURE;
5081 /* FIXME: Remove once PR 41177 (this problem) is fixed completely. */
5082 if (base->rank > 0)
5084 gfc_error ("Non-scalar base object at %L currently not implemented",
5085 &e->where);
5086 return FAILURE;
5089 return SUCCESS;
5093 /* Resolve a call to a type-bound procedure, either function or subroutine,
5094 statically from the data in an EXPR_COMPCALL expression. The adapted
5095 arglist and the target-procedure symtree are returned. */
5097 static gfc_try
5098 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5099 gfc_actual_arglist** actual)
5101 gcc_assert (e->expr_type == EXPR_COMPCALL);
5102 gcc_assert (!e->value.compcall.tbp->is_generic);
5104 /* Update the actual arglist for PASS. */
5105 if (update_compcall_arglist (e) == FAILURE)
5106 return FAILURE;
5108 *actual = e->value.compcall.actual;
5109 *target = e->value.compcall.tbp->u.specific;
5111 gfc_free_ref_list (e->ref);
5112 e->ref = NULL;
5113 e->value.compcall.actual = NULL;
5115 return SUCCESS;
5119 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5120 which of the specific bindings (if any) matches the arglist and transform
5121 the expression into a call of that binding. */
5123 static gfc_try
5124 resolve_typebound_generic_call (gfc_expr* e)
5126 gfc_typebound_proc* genproc;
5127 const char* genname;
5129 gcc_assert (e->expr_type == EXPR_COMPCALL);
5130 genname = e->value.compcall.name;
5131 genproc = e->value.compcall.tbp;
5133 if (!genproc->is_generic)
5134 return SUCCESS;
5136 /* Try the bindings on this type and in the inheritance hierarchy. */
5137 for (; genproc; genproc = genproc->overridden)
5139 gfc_tbp_generic* g;
5141 gcc_assert (genproc->is_generic);
5142 for (g = genproc->u.generic; g; g = g->next)
5144 gfc_symbol* target;
5145 gfc_actual_arglist* args;
5146 bool matches;
5148 gcc_assert (g->specific);
5150 if (g->specific->error)
5151 continue;
5153 target = g->specific->u.specific->n.sym;
5155 /* Get the right arglist by handling PASS/NOPASS. */
5156 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5157 if (!g->specific->nopass)
5159 gfc_expr* po;
5160 po = extract_compcall_passed_object (e);
5161 if (!po)
5162 return FAILURE;
5164 gcc_assert (g->specific->pass_arg_num > 0);
5165 gcc_assert (!g->specific->error);
5166 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5167 g->specific->pass_arg);
5169 resolve_actual_arglist (args, target->attr.proc,
5170 is_external_proc (target) && !target->formal);
5172 /* Check if this arglist matches the formal. */
5173 matches = gfc_arglist_matches_symbol (&args, target);
5175 /* Clean up and break out of the loop if we've found it. */
5176 gfc_free_actual_arglist (args);
5177 if (matches)
5179 e->value.compcall.tbp = g->specific;
5180 goto success;
5185 /* Nothing matching found! */
5186 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5187 " '%s' at %L", genname, &e->where);
5188 return FAILURE;
5190 success:
5191 return SUCCESS;
5195 /* Resolve a call to a type-bound subroutine. */
5197 static gfc_try
5198 resolve_typebound_call (gfc_code* c)
5200 gfc_actual_arglist* newactual;
5201 gfc_symtree* target;
5203 /* Check that's really a SUBROUTINE. */
5204 if (!c->expr1->value.compcall.tbp->subroutine)
5206 gfc_error ("'%s' at %L should be a SUBROUTINE",
5207 c->expr1->value.compcall.name, &c->loc);
5208 return FAILURE;
5211 if (check_typebound_baseobject (c->expr1) == FAILURE)
5212 return FAILURE;
5214 if (resolve_typebound_generic_call (c->expr1) == FAILURE)
5215 return FAILURE;
5217 /* Transform into an ordinary EXEC_CALL for now. */
5219 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5220 return FAILURE;
5222 c->ext.actual = newactual;
5223 c->symtree = target;
5224 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5226 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5228 gfc_free_expr (c->expr1);
5229 c->expr1 = gfc_get_expr ();
5230 c->expr1->expr_type = EXPR_FUNCTION;
5231 c->expr1->symtree = target;
5232 c->expr1->where = c->loc;
5234 return resolve_call (c);
5238 /* Resolve a component-call expression. This originally was intended
5239 only to see functions. However, it is convenient to use it in
5240 resolving subroutine class methods, since we do not have to add a
5241 gfc_code each time. */
5242 static gfc_try
5243 resolve_compcall (gfc_expr* e, bool fcn, bool class_members)
5245 gfc_actual_arglist* newactual;
5246 gfc_symtree* target;
5248 /* Check that's really a FUNCTION. */
5249 if (fcn && !e->value.compcall.tbp->function)
5251 gfc_error ("'%s' at %L should be a FUNCTION",
5252 e->value.compcall.name, &e->where);
5253 return FAILURE;
5255 else if (!fcn && !e->value.compcall.tbp->subroutine)
5257 /* To resolve class member calls, we borrow this bit
5258 of code to select the specific procedures. */
5259 gfc_error ("'%s' at %L should be a SUBROUTINE",
5260 e->value.compcall.name, &e->where);
5261 return FAILURE;
5264 /* These must not be assign-calls! */
5265 gcc_assert (!e->value.compcall.assign);
5267 if (check_typebound_baseobject (e) == FAILURE)
5268 return FAILURE;
5270 if (resolve_typebound_generic_call (e) == FAILURE)
5271 return FAILURE;
5272 gcc_assert (!e->value.compcall.tbp->is_generic);
5274 /* Take the rank from the function's symbol. */
5275 if (e->value.compcall.tbp->u.specific->n.sym->as)
5276 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5278 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5279 arglist to the TBP's binding target. */
5281 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5282 return FAILURE;
5284 e->value.function.actual = newactual;
5285 e->value.function.name = NULL;
5286 e->value.function.esym = target->n.sym;
5287 e->value.function.class_esym = NULL;
5288 e->value.function.isym = NULL;
5289 e->symtree = target;
5290 e->ts = target->n.sym->ts;
5291 e->expr_type = EXPR_FUNCTION;
5293 /* Resolution is not necessary when constructing component calls
5294 for class members, since this must only be done for the
5295 declared type, which is done afterwards. */
5296 return !class_members ? gfc_resolve_expr (e) : SUCCESS;
5300 /* Resolve a typebound call for the members in a class. This group of
5301 functions implements dynamic dispatch in the provisional version
5302 of f03 OOP. As soon as vtables are in place and contain pointers
5303 to methods, this will no longer be necessary. */
5304 static gfc_expr *list_e;
5305 static gfc_try check_class_members (gfc_symbol *);
5306 static gfc_try class_try;
5307 static bool fcn_flag;
5310 static void
5311 check_members (gfc_symbol *derived)
5313 if (derived->attr.flavor == FL_DERIVED)
5314 (void) check_class_members (derived);
5318 static gfc_try
5319 check_class_members (gfc_symbol *derived)
5321 gfc_expr *e;
5322 gfc_symtree *tbp;
5323 gfc_class_esym_list *etmp;
5325 e = gfc_copy_expr (list_e);
5327 tbp = gfc_find_typebound_proc (derived, &class_try,
5328 e->value.compcall.name,
5329 false, &e->where);
5331 if (tbp == NULL)
5333 gfc_error ("no typebound available procedure named '%s' at %L",
5334 e->value.compcall.name, &e->where);
5335 return FAILURE;
5338 /* If we have to match a passed class member, force the actual
5339 expression to have the correct type. */
5340 if (!tbp->n.tb->nopass)
5342 if (e->value.compcall.base_object == NULL)
5343 e->value.compcall.base_object = extract_compcall_passed_object (e);
5345 if (e->value.compcall.base_object == NULL)
5346 return FAILURE;
5348 if (!derived->attr.abstract)
5350 e->value.compcall.base_object->ts.type = BT_DERIVED;
5351 e->value.compcall.base_object->ts.u.derived = derived;
5355 e->value.compcall.tbp = tbp->n.tb;
5356 e->value.compcall.name = tbp->name;
5358 /* Let the original expresssion catch the assertion in
5359 resolve_compcall, since this flag does not appear to be reset or
5360 copied in some systems. */
5361 e->value.compcall.assign = 0;
5363 /* Do the renaming, PASSing, generic => specific and other
5364 good things for each class member. */
5365 class_try = (resolve_compcall (e, fcn_flag, true) == SUCCESS)
5366 ? class_try : FAILURE;
5368 /* Now transfer the found symbol to the esym list. */
5369 if (class_try == SUCCESS)
5371 etmp = list_e->value.function.class_esym;
5372 list_e->value.function.class_esym
5373 = gfc_get_class_esym_list();
5374 list_e->value.function.class_esym->next = etmp;
5375 list_e->value.function.class_esym->derived = derived;
5376 list_e->value.function.class_esym->esym
5377 = e->value.function.esym;
5380 gfc_free_expr (e);
5382 /* Burrow down into grandchildren types. */
5383 if (derived->f2k_derived)
5384 gfc_traverse_ns (derived->f2k_derived, check_members);
5386 return SUCCESS;
5390 /* Eliminate esym_lists where all the members point to the
5391 typebound procedure of the declared type; ie. one where
5392 type selection has no effect.. */
5393 static void
5394 resolve_class_esym (gfc_expr *e)
5396 gfc_class_esym_list *p, *q;
5397 bool empty = true;
5399 gcc_assert (e && e->expr_type == EXPR_FUNCTION);
5401 p = e->value.function.class_esym;
5402 if (p == NULL)
5403 return;
5405 for (; p; p = p->next)
5406 empty = empty && (e->value.function.esym == p->esym);
5408 if (empty)
5410 p = e->value.function.class_esym;
5411 for (; p; p = q)
5413 q = p->next;
5414 gfc_free (p);
5416 e->value.function.class_esym = NULL;
5421 /* Generate an expression for the hash value, given the reference to
5422 the class of the final expression (class_ref), the base of the
5423 full reference list (new_ref), the declared type and the class
5424 object (st). */
5425 static gfc_expr*
5426 hash_value_expr (gfc_ref *class_ref, gfc_ref *new_ref, gfc_symtree *st)
5428 gfc_expr *hash_value;
5430 /* Build an expression for the correct hash_value; ie. that of the last
5431 CLASS reference. */
5432 if (class_ref)
5434 class_ref->next = NULL;
5436 else
5438 gfc_free_ref_list (new_ref);
5439 new_ref = NULL;
5441 hash_value = gfc_get_expr ();
5442 hash_value->expr_type = EXPR_VARIABLE;
5443 hash_value->symtree = st;
5444 hash_value->symtree->n.sym->refs++;
5445 hash_value->ref = new_ref;
5446 gfc_add_component_ref (hash_value, "$vptr");
5447 gfc_add_component_ref (hash_value, "$hash");
5449 return hash_value;
5453 /* Get the ultimate declared type from an expression. In addition,
5454 return the last class/derived type reference and the copy of the
5455 reference list. */
5456 static gfc_symbol*
5457 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5458 gfc_expr *e)
5460 gfc_symbol *declared;
5461 gfc_ref *ref;
5463 declared = NULL;
5464 *class_ref = NULL;
5465 *new_ref = gfc_copy_ref (e->ref);
5466 for (ref = *new_ref; ref; ref = ref->next)
5468 if (ref->type != REF_COMPONENT)
5469 continue;
5471 if (ref->u.c.component->ts.type == BT_CLASS
5472 || ref->u.c.component->ts.type == BT_DERIVED)
5474 declared = ref->u.c.component->ts.u.derived;
5475 *class_ref = ref;
5479 if (declared == NULL)
5480 declared = e->symtree->n.sym->ts.u.derived;
5482 return declared;
5486 /* Resolve the argument expressions so that any arguments expressions
5487 that include class methods are resolved before the current call.
5488 This is necessary because of the static variables used in CLASS
5489 method resolution. */
5490 static void
5491 resolve_arg_exprs (gfc_actual_arglist *arg)
5493 /* Resolve the actual arglist expressions. */
5494 for (; arg; arg = arg->next)
5496 if (arg->expr)
5497 gfc_resolve_expr (arg->expr);
5502 /* Resolve a typebound function, or 'method'. First separate all
5503 the non-CLASS references by calling resolve_compcall directly.
5504 Then treat the CLASS references by resolving for each of the class
5505 members in turn. */
5507 static gfc_try
5508 resolve_typebound_function (gfc_expr* e)
5510 gfc_symbol *derived, *declared;
5511 gfc_ref *new_ref;
5512 gfc_ref *class_ref;
5513 gfc_symtree *st;
5515 st = e->symtree;
5516 if (st == NULL)
5517 return resolve_compcall (e, true, false);
5519 /* Get the CLASS declared type. */
5520 declared = get_declared_from_expr (&class_ref, &new_ref, e);
5522 /* Weed out cases of the ultimate component being a derived type. */
5523 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5524 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5526 gfc_free_ref_list (new_ref);
5527 return resolve_compcall (e, true, false);
5530 /* Resolve the argument expressions, */
5531 resolve_arg_exprs (e->value.function.actual);
5533 /* Get the data component, which is of the declared type. */
5534 derived = declared->components->ts.u.derived;
5536 /* Resolve the function call for each member of the class. */
5537 class_try = SUCCESS;
5538 fcn_flag = true;
5539 list_e = gfc_copy_expr (e);
5541 if (check_class_members (derived) == FAILURE)
5542 return FAILURE;
5544 class_try = (resolve_compcall (e, true, false) == SUCCESS)
5545 ? class_try : FAILURE;
5547 /* Transfer the class list to the original expression. Note that
5548 the class_esym list is cleaned up in trans-expr.c, as the calls
5549 are translated. */
5550 e->value.function.class_esym = list_e->value.function.class_esym;
5551 list_e->value.function.class_esym = NULL;
5552 gfc_free_expr (list_e);
5554 resolve_class_esym (e);
5556 /* More than one typebound procedure so transmit an expression for
5557 the hash_value as the selector. */
5558 if (e->value.function.class_esym != NULL)
5559 e->value.function.class_esym->hash_value
5560 = hash_value_expr (class_ref, new_ref, st);
5562 return class_try;
5565 /* Resolve a typebound subroutine, or 'method'. First separate all
5566 the non-CLASS references by calling resolve_typebound_call directly.
5567 Then treat the CLASS references by resolving for each of the class
5568 members in turn. */
5570 static gfc_try
5571 resolve_typebound_subroutine (gfc_code *code)
5573 gfc_symbol *derived, *declared;
5574 gfc_ref *new_ref;
5575 gfc_ref *class_ref;
5576 gfc_symtree *st;
5578 st = code->expr1->symtree;
5579 if (st == NULL)
5580 return resolve_typebound_call (code);
5582 /* Get the CLASS declared type. */
5583 declared = get_declared_from_expr (&class_ref, &new_ref, code->expr1);
5585 /* Weed out cases of the ultimate component being a derived type. */
5586 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5587 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5589 gfc_free_ref_list (new_ref);
5590 return resolve_typebound_call (code);
5593 /* Resolve the argument expressions, */
5594 resolve_arg_exprs (code->expr1->value.compcall.actual);
5596 /* Get the data component, which is of the declared type. */
5597 derived = declared->components->ts.u.derived;
5599 class_try = SUCCESS;
5600 fcn_flag = false;
5601 list_e = gfc_copy_expr (code->expr1);
5603 if (check_class_members (derived) == FAILURE)
5604 return FAILURE;
5606 class_try = (resolve_typebound_call (code) == SUCCESS)
5607 ? class_try : FAILURE;
5609 /* Transfer the class list to the original expression. Note that
5610 the class_esym list is cleaned up in trans-expr.c, as the calls
5611 are translated. */
5612 code->expr1->value.function.class_esym
5613 = list_e->value.function.class_esym;
5614 list_e->value.function.class_esym = NULL;
5615 gfc_free_expr (list_e);
5617 resolve_class_esym (code->expr1);
5619 /* More than one typebound procedure so transmit an expression for
5620 the hash_value as the selector. */
5621 if (code->expr1->value.function.class_esym != NULL)
5622 code->expr1->value.function.class_esym->hash_value
5623 = hash_value_expr (class_ref, new_ref, st);
5625 return class_try;
5629 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5631 static gfc_try
5632 resolve_ppc_call (gfc_code* c)
5634 gfc_component *comp;
5635 bool b;
5637 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
5638 gcc_assert (b);
5640 c->resolved_sym = c->expr1->symtree->n.sym;
5641 c->expr1->expr_type = EXPR_VARIABLE;
5643 if (!comp->attr.subroutine)
5644 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5646 if (resolve_ref (c->expr1) == FAILURE)
5647 return FAILURE;
5649 if (update_ppc_arglist (c->expr1) == FAILURE)
5650 return FAILURE;
5652 c->ext.actual = c->expr1->value.compcall.actual;
5654 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5655 comp->formal == NULL) == FAILURE)
5656 return FAILURE;
5658 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5660 return SUCCESS;
5664 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5666 static gfc_try
5667 resolve_expr_ppc (gfc_expr* e)
5669 gfc_component *comp;
5670 bool b;
5672 b = gfc_is_proc_ptr_comp (e, &comp);
5673 gcc_assert (b);
5675 /* Convert to EXPR_FUNCTION. */
5676 e->expr_type = EXPR_FUNCTION;
5677 e->value.function.isym = NULL;
5678 e->value.function.actual = e->value.compcall.actual;
5679 e->ts = comp->ts;
5680 if (comp->as != NULL)
5681 e->rank = comp->as->rank;
5683 if (!comp->attr.function)
5684 gfc_add_function (&comp->attr, comp->name, &e->where);
5686 if (resolve_ref (e) == FAILURE)
5687 return FAILURE;
5689 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5690 comp->formal == NULL) == FAILURE)
5691 return FAILURE;
5693 if (update_ppc_arglist (e) == FAILURE)
5694 return FAILURE;
5696 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
5698 return SUCCESS;
5702 static bool
5703 gfc_is_expandable_expr (gfc_expr *e)
5705 gfc_constructor *con;
5707 if (e->expr_type == EXPR_ARRAY)
5709 /* Traverse the constructor looking for variables that are flavor
5710 parameter. Parameters must be expanded since they are fully used at
5711 compile time. */
5712 con = gfc_constructor_first (e->value.constructor);
5713 for (; con; con = gfc_constructor_next (con))
5715 if (con->expr->expr_type == EXPR_VARIABLE
5716 && con->expr->symtree
5717 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
5718 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
5719 return true;
5720 if (con->expr->expr_type == EXPR_ARRAY
5721 && gfc_is_expandable_expr (con->expr))
5722 return true;
5726 return false;
5729 /* Resolve an expression. That is, make sure that types of operands agree
5730 with their operators, intrinsic operators are converted to function calls
5731 for overloaded types and unresolved function references are resolved. */
5733 gfc_try
5734 gfc_resolve_expr (gfc_expr *e)
5736 gfc_try t;
5737 bool inquiry_save;
5739 if (e == NULL)
5740 return SUCCESS;
5742 /* inquiry_argument only applies to variables. */
5743 inquiry_save = inquiry_argument;
5744 if (e->expr_type != EXPR_VARIABLE)
5745 inquiry_argument = false;
5747 switch (e->expr_type)
5749 case EXPR_OP:
5750 t = resolve_operator (e);
5751 break;
5753 case EXPR_FUNCTION:
5754 case EXPR_VARIABLE:
5756 if (check_host_association (e))
5757 t = resolve_function (e);
5758 else
5760 t = resolve_variable (e);
5761 if (t == SUCCESS)
5762 expression_rank (e);
5765 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
5766 && e->ref->type != REF_SUBSTRING)
5767 gfc_resolve_substring_charlen (e);
5769 break;
5771 case EXPR_COMPCALL:
5772 t = resolve_typebound_function (e);
5773 break;
5775 case EXPR_SUBSTRING:
5776 t = resolve_ref (e);
5777 break;
5779 case EXPR_CONSTANT:
5780 case EXPR_NULL:
5781 t = SUCCESS;
5782 break;
5784 case EXPR_PPC:
5785 t = resolve_expr_ppc (e);
5786 break;
5788 case EXPR_ARRAY:
5789 t = FAILURE;
5790 if (resolve_ref (e) == FAILURE)
5791 break;
5793 t = gfc_resolve_array_constructor (e);
5794 /* Also try to expand a constructor. */
5795 if (t == SUCCESS)
5797 expression_rank (e);
5798 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
5799 gfc_expand_constructor (e);
5802 /* This provides the opportunity for the length of constructors with
5803 character valued function elements to propagate the string length
5804 to the expression. */
5805 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
5807 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
5808 here rather then add a duplicate test for it above. */
5809 gfc_expand_constructor (e);
5810 t = gfc_resolve_character_array_constructor (e);
5813 break;
5815 case EXPR_STRUCTURE:
5816 t = resolve_ref (e);
5817 if (t == FAILURE)
5818 break;
5820 t = resolve_structure_cons (e);
5821 if (t == FAILURE)
5822 break;
5824 t = gfc_simplify_expr (e, 0);
5825 break;
5827 default:
5828 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
5831 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
5832 fixup_charlen (e);
5834 inquiry_argument = inquiry_save;
5836 return t;
5840 /* Resolve an expression from an iterator. They must be scalar and have
5841 INTEGER or (optionally) REAL type. */
5843 static gfc_try
5844 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
5845 const char *name_msgid)
5847 if (gfc_resolve_expr (expr) == FAILURE)
5848 return FAILURE;
5850 if (expr->rank != 0)
5852 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
5853 return FAILURE;
5856 if (expr->ts.type != BT_INTEGER)
5858 if (expr->ts.type == BT_REAL)
5860 if (real_ok)
5861 return gfc_notify_std (GFC_STD_F95_DEL,
5862 "Deleted feature: %s at %L must be integer",
5863 _(name_msgid), &expr->where);
5864 else
5866 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
5867 &expr->where);
5868 return FAILURE;
5871 else
5873 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
5874 return FAILURE;
5877 return SUCCESS;
5881 /* Resolve the expressions in an iterator structure. If REAL_OK is
5882 false allow only INTEGER type iterators, otherwise allow REAL types. */
5884 gfc_try
5885 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
5887 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
5888 == FAILURE)
5889 return FAILURE;
5891 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
5893 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5894 &iter->var->where);
5895 return FAILURE;
5898 if (gfc_resolve_iterator_expr (iter->start, real_ok,
5899 "Start expression in DO loop") == FAILURE)
5900 return FAILURE;
5902 if (gfc_resolve_iterator_expr (iter->end, real_ok,
5903 "End expression in DO loop") == FAILURE)
5904 return FAILURE;
5906 if (gfc_resolve_iterator_expr (iter->step, real_ok,
5907 "Step expression in DO loop") == FAILURE)
5908 return FAILURE;
5910 if (iter->step->expr_type == EXPR_CONSTANT)
5912 if ((iter->step->ts.type == BT_INTEGER
5913 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
5914 || (iter->step->ts.type == BT_REAL
5915 && mpfr_sgn (iter->step->value.real) == 0))
5917 gfc_error ("Step expression in DO loop at %L cannot be zero",
5918 &iter->step->where);
5919 return FAILURE;
5923 /* Convert start, end, and step to the same type as var. */
5924 if (iter->start->ts.kind != iter->var->ts.kind
5925 || iter->start->ts.type != iter->var->ts.type)
5926 gfc_convert_type (iter->start, &iter->var->ts, 2);
5928 if (iter->end->ts.kind != iter->var->ts.kind
5929 || iter->end->ts.type != iter->var->ts.type)
5930 gfc_convert_type (iter->end, &iter->var->ts, 2);
5932 if (iter->step->ts.kind != iter->var->ts.kind
5933 || iter->step->ts.type != iter->var->ts.type)
5934 gfc_convert_type (iter->step, &iter->var->ts, 2);
5936 if (iter->start->expr_type == EXPR_CONSTANT
5937 && iter->end->expr_type == EXPR_CONSTANT
5938 && iter->step->expr_type == EXPR_CONSTANT)
5940 int sgn, cmp;
5941 if (iter->start->ts.type == BT_INTEGER)
5943 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
5944 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
5946 else
5948 sgn = mpfr_sgn (iter->step->value.real);
5949 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
5951 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
5952 gfc_warning ("DO loop at %L will be executed zero times",
5953 &iter->step->where);
5956 return SUCCESS;
5960 /* Traversal function for find_forall_index. f == 2 signals that
5961 that variable itself is not to be checked - only the references. */
5963 static bool
5964 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
5966 if (expr->expr_type != EXPR_VARIABLE)
5967 return false;
5969 /* A scalar assignment */
5970 if (!expr->ref || *f == 1)
5972 if (expr->symtree->n.sym == sym)
5973 return true;
5974 else
5975 return false;
5978 if (*f == 2)
5979 *f = 1;
5980 return false;
5984 /* Check whether the FORALL index appears in the expression or not.
5985 Returns SUCCESS if SYM is found in EXPR. */
5987 gfc_try
5988 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
5990 if (gfc_traverse_expr (expr, sym, forall_index, f))
5991 return SUCCESS;
5992 else
5993 return FAILURE;
5997 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5998 to be a scalar INTEGER variable. The subscripts and stride are scalar
5999 INTEGERs, and if stride is a constant it must be nonzero.
6000 Furthermore "A subscript or stride in a forall-triplet-spec shall
6001 not contain a reference to any index-name in the
6002 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6004 static void
6005 resolve_forall_iterators (gfc_forall_iterator *it)
6007 gfc_forall_iterator *iter, *iter2;
6009 for (iter = it; iter; iter = iter->next)
6011 if (gfc_resolve_expr (iter->var) == SUCCESS
6012 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6013 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6014 &iter->var->where);
6016 if (gfc_resolve_expr (iter->start) == SUCCESS
6017 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6018 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6019 &iter->start->where);
6020 if (iter->var->ts.kind != iter->start->ts.kind)
6021 gfc_convert_type (iter->start, &iter->var->ts, 2);
6023 if (gfc_resolve_expr (iter->end) == SUCCESS
6024 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6025 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6026 &iter->end->where);
6027 if (iter->var->ts.kind != iter->end->ts.kind)
6028 gfc_convert_type (iter->end, &iter->var->ts, 2);
6030 if (gfc_resolve_expr (iter->stride) == SUCCESS)
6032 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6033 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6034 &iter->stride->where, "INTEGER");
6036 if (iter->stride->expr_type == EXPR_CONSTANT
6037 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
6038 gfc_error ("FORALL stride expression at %L cannot be zero",
6039 &iter->stride->where);
6041 if (iter->var->ts.kind != iter->stride->ts.kind)
6042 gfc_convert_type (iter->stride, &iter->var->ts, 2);
6045 for (iter = it; iter; iter = iter->next)
6046 for (iter2 = iter; iter2; iter2 = iter2->next)
6048 if (find_forall_index (iter2->start,
6049 iter->var->symtree->n.sym, 0) == SUCCESS
6050 || find_forall_index (iter2->end,
6051 iter->var->symtree->n.sym, 0) == SUCCESS
6052 || find_forall_index (iter2->stride,
6053 iter->var->symtree->n.sym, 0) == SUCCESS)
6054 gfc_error ("FORALL index '%s' may not appear in triplet "
6055 "specification at %L", iter->var->symtree->name,
6056 &iter2->start->where);
6061 /* Given a pointer to a symbol that is a derived type, see if it's
6062 inaccessible, i.e. if it's defined in another module and the components are
6063 PRIVATE. The search is recursive if necessary. Returns zero if no
6064 inaccessible components are found, nonzero otherwise. */
6066 static int
6067 derived_inaccessible (gfc_symbol *sym)
6069 gfc_component *c;
6071 if (sym->attr.use_assoc && sym->attr.private_comp)
6072 return 1;
6074 for (c = sym->components; c; c = c->next)
6076 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6077 return 1;
6080 return 0;
6084 /* Resolve the argument of a deallocate expression. The expression must be
6085 a pointer or a full array. */
6087 static gfc_try
6088 resolve_deallocate_expr (gfc_expr *e)
6090 symbol_attribute attr;
6091 int allocatable, pointer, check_intent_in;
6092 gfc_ref *ref;
6093 gfc_symbol *sym;
6094 gfc_component *c;
6096 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
6097 check_intent_in = 1;
6099 if (gfc_resolve_expr (e) == FAILURE)
6100 return FAILURE;
6102 if (e->expr_type != EXPR_VARIABLE)
6103 goto bad;
6105 sym = e->symtree->n.sym;
6107 if (sym->ts.type == BT_CLASS)
6109 allocatable = sym->ts.u.derived->components->attr.allocatable;
6110 pointer = sym->ts.u.derived->components->attr.pointer;
6112 else
6114 allocatable = sym->attr.allocatable;
6115 pointer = sym->attr.pointer;
6117 for (ref = e->ref; ref; ref = ref->next)
6119 if (pointer)
6120 check_intent_in = 0;
6122 switch (ref->type)
6124 case REF_ARRAY:
6125 if (ref->u.ar.type != AR_FULL)
6126 allocatable = 0;
6127 break;
6129 case REF_COMPONENT:
6130 c = ref->u.c.component;
6131 if (c->ts.type == BT_CLASS)
6133 allocatable = c->ts.u.derived->components->attr.allocatable;
6134 pointer = c->ts.u.derived->components->attr.pointer;
6136 else
6138 allocatable = c->attr.allocatable;
6139 pointer = c->attr.pointer;
6141 break;
6143 case REF_SUBSTRING:
6144 allocatable = 0;
6145 break;
6149 attr = gfc_expr_attr (e);
6151 if (allocatable == 0 && attr.pointer == 0)
6153 bad:
6154 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6155 &e->where);
6158 if (check_intent_in && sym->attr.intent == INTENT_IN)
6160 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
6161 sym->name, &e->where);
6162 return FAILURE;
6165 if (e->ts.type == BT_CLASS)
6167 /* Only deallocate the DATA component. */
6168 gfc_add_component_ref (e, "$data");
6171 return SUCCESS;
6175 /* Returns true if the expression e contains a reference to the symbol sym. */
6176 static bool
6177 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6179 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6180 return true;
6182 return false;
6185 bool
6186 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6188 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6192 /* Given the expression node e for an allocatable/pointer of derived type to be
6193 allocated, get the expression node to be initialized afterwards (needed for
6194 derived types with default initializers, and derived types with allocatable
6195 components that need nullification.) */
6197 gfc_expr *
6198 gfc_expr_to_initialize (gfc_expr *e)
6200 gfc_expr *result;
6201 gfc_ref *ref;
6202 int i;
6204 result = gfc_copy_expr (e);
6206 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6207 for (ref = result->ref; ref; ref = ref->next)
6208 if (ref->type == REF_ARRAY && ref->next == NULL)
6210 ref->u.ar.type = AR_FULL;
6212 for (i = 0; i < ref->u.ar.dimen; i++)
6213 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6215 result->rank = ref->u.ar.dimen;
6216 break;
6219 return result;
6223 /* Used in resolve_allocate_expr to check that a allocation-object and
6224 a source-expr are conformable. This does not catch all possible
6225 cases; in particular a runtime checking is needed. */
6227 static gfc_try
6228 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6230 /* First compare rank. */
6231 if (e2->ref && e1->rank != e2->ref->u.ar.as->rank)
6233 gfc_error ("Source-expr at %L must be scalar or have the "
6234 "same rank as the allocate-object at %L",
6235 &e1->where, &e2->where);
6236 return FAILURE;
6239 if (e1->shape)
6241 int i;
6242 mpz_t s;
6244 mpz_init (s);
6246 for (i = 0; i < e1->rank; i++)
6248 if (e2->ref->u.ar.end[i])
6250 mpz_set (s, e2->ref->u.ar.end[i]->value.integer);
6251 mpz_sub (s, s, e2->ref->u.ar.start[i]->value.integer);
6252 mpz_add_ui (s, s, 1);
6254 else
6256 mpz_set (s, e2->ref->u.ar.start[i]->value.integer);
6259 if (mpz_cmp (e1->shape[i], s) != 0)
6261 gfc_error ("Source-expr at %L and allocate-object at %L must "
6262 "have the same shape", &e1->where, &e2->where);
6263 mpz_clear (s);
6264 return FAILURE;
6268 mpz_clear (s);
6271 return SUCCESS;
6275 /* Resolve the expression in an ALLOCATE statement, doing the additional
6276 checks to see whether the expression is OK or not. The expression must
6277 have a trailing array reference that gives the size of the array. */
6279 static gfc_try
6280 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6282 int i, pointer, allocatable, dimension, check_intent_in, is_abstract;
6283 int codimension;
6284 symbol_attribute attr;
6285 gfc_ref *ref, *ref2;
6286 gfc_array_ref *ar;
6287 gfc_symbol *sym;
6288 gfc_alloc *a;
6289 gfc_component *c;
6290 gfc_expr *init_e;
6292 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
6293 check_intent_in = 1;
6295 /* Mark the ultimost array component as being in allocate to allow DIMEN_STAR
6296 checking of coarrays. */
6297 for (ref = e->ref; ref; ref = ref->next)
6298 if (ref->next == NULL)
6299 break;
6301 if (ref && ref->type == REF_ARRAY)
6302 ref->u.ar.in_allocate = true;
6304 if (gfc_resolve_expr (e) == FAILURE)
6305 goto failure;
6307 /* Make sure the expression is allocatable or a pointer. If it is
6308 pointer, the next-to-last reference must be a pointer. */
6310 ref2 = NULL;
6311 if (e->symtree)
6312 sym = e->symtree->n.sym;
6314 /* Check whether ultimate component is abstract and CLASS. */
6315 is_abstract = 0;
6317 if (e->expr_type != EXPR_VARIABLE)
6319 allocatable = 0;
6320 attr = gfc_expr_attr (e);
6321 pointer = attr.pointer;
6322 dimension = attr.dimension;
6323 codimension = attr.codimension;
6325 else
6327 if (sym->ts.type == BT_CLASS)
6329 allocatable = sym->ts.u.derived->components->attr.allocatable;
6330 pointer = sym->ts.u.derived->components->attr.pointer;
6331 dimension = sym->ts.u.derived->components->attr.dimension;
6332 codimension = sym->ts.u.derived->components->attr.codimension;
6333 is_abstract = sym->ts.u.derived->components->attr.abstract;
6335 else
6337 allocatable = sym->attr.allocatable;
6338 pointer = sym->attr.pointer;
6339 dimension = sym->attr.dimension;
6340 codimension = sym->attr.codimension;
6343 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6345 if (pointer)
6346 check_intent_in = 0;
6348 switch (ref->type)
6350 case REF_ARRAY:
6351 if (ref->next != NULL)
6352 pointer = 0;
6353 break;
6355 case REF_COMPONENT:
6356 /* F2008, C644. */
6357 if (gfc_is_coindexed (e))
6359 gfc_error ("Coindexed allocatable object at %L",
6360 &e->where);
6361 goto failure;
6364 c = ref->u.c.component;
6365 if (c->ts.type == BT_CLASS)
6367 allocatable = c->ts.u.derived->components->attr.allocatable;
6368 pointer = c->ts.u.derived->components->attr.pointer;
6369 dimension = c->ts.u.derived->components->attr.dimension;
6370 codimension = c->ts.u.derived->components->attr.codimension;
6371 is_abstract = c->ts.u.derived->components->attr.abstract;
6373 else
6375 allocatable = c->attr.allocatable;
6376 pointer = c->attr.pointer;
6377 dimension = c->attr.dimension;
6378 codimension = c->attr.codimension;
6379 is_abstract = c->attr.abstract;
6381 break;
6383 case REF_SUBSTRING:
6384 allocatable = 0;
6385 pointer = 0;
6386 break;
6391 if (allocatable == 0 && pointer == 0)
6393 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6394 &e->where);
6395 goto failure;
6398 /* Some checks for the SOURCE tag. */
6399 if (code->expr3)
6401 /* Check F03:C631. */
6402 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6404 gfc_error ("Type of entity at %L is type incompatible with "
6405 "source-expr at %L", &e->where, &code->expr3->where);
6406 goto failure;
6409 /* Check F03:C632 and restriction following Note 6.18. */
6410 if (code->expr3->rank > 0
6411 && conformable_arrays (code->expr3, e) == FAILURE)
6412 goto failure;
6414 /* Check F03:C633. */
6415 if (code->expr3->ts.kind != e->ts.kind)
6417 gfc_error ("The allocate-object at %L and the source-expr at %L "
6418 "shall have the same kind type parameter",
6419 &e->where, &code->expr3->where);
6420 goto failure;
6423 else if (is_abstract&& code->ext.alloc.ts.type == BT_UNKNOWN)
6425 gcc_assert (e->ts.type == BT_CLASS);
6426 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6427 "type-spec or SOURCE=", sym->name, &e->where);
6428 goto failure;
6431 if (check_intent_in && sym->attr.intent == INTENT_IN)
6433 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
6434 sym->name, &e->where);
6435 goto failure;
6438 if (!code->expr3)
6440 /* Add default initializer for those derived types that need them. */
6441 if (e->ts.type == BT_DERIVED
6442 && (init_e = gfc_default_initializer (&e->ts)))
6444 gfc_code *init_st = gfc_get_code ();
6445 init_st->loc = code->loc;
6446 init_st->op = EXEC_INIT_ASSIGN;
6447 init_st->expr1 = gfc_expr_to_initialize (e);
6448 init_st->expr2 = init_e;
6449 init_st->next = code->next;
6450 code->next = init_st;
6452 else if (e->ts.type == BT_CLASS
6453 && ((code->ext.alloc.ts.type == BT_UNKNOWN
6454 && (init_e = gfc_default_initializer (&e->ts.u.derived->components->ts)))
6455 || (code->ext.alloc.ts.type == BT_DERIVED
6456 && (init_e = gfc_default_initializer (&code->ext.alloc.ts)))))
6458 gfc_code *init_st = gfc_get_code ();
6459 init_st->loc = code->loc;
6460 init_st->op = EXEC_INIT_ASSIGN;
6461 init_st->expr1 = gfc_expr_to_initialize (e);
6462 init_st->expr2 = init_e;
6463 init_st->next = code->next;
6464 code->next = init_st;
6468 if (pointer || (dimension == 0 && codimension == 0))
6469 goto success;
6471 /* Make sure the next-to-last reference node is an array specification. */
6473 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6474 || (dimension && ref2->u.ar.dimen == 0))
6476 gfc_error ("Array specification required in ALLOCATE statement "
6477 "at %L", &e->where);
6478 goto failure;
6481 /* Make sure that the array section reference makes sense in the
6482 context of an ALLOCATE specification. */
6484 ar = &ref2->u.ar;
6486 if (codimension && ar->codimen == 0)
6488 gfc_error ("Coarray specification required in ALLOCATE statement "
6489 "at %L", &e->where);
6490 goto failure;
6493 for (i = 0; i < ar->dimen; i++)
6495 if (ref2->u.ar.type == AR_ELEMENT)
6496 goto check_symbols;
6498 switch (ar->dimen_type[i])
6500 case DIMEN_ELEMENT:
6501 break;
6503 case DIMEN_RANGE:
6504 if (ar->start[i] != NULL
6505 && ar->end[i] != NULL
6506 && ar->stride[i] == NULL)
6507 break;
6509 /* Fall Through... */
6511 case DIMEN_UNKNOWN:
6512 case DIMEN_VECTOR:
6513 case DIMEN_STAR:
6514 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6515 &e->where);
6516 goto failure;
6519 check_symbols:
6520 for (a = code->ext.alloc.list; a; a = a->next)
6522 sym = a->expr->symtree->n.sym;
6524 /* TODO - check derived type components. */
6525 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6526 continue;
6528 if ((ar->start[i] != NULL
6529 && gfc_find_sym_in_expr (sym, ar->start[i]))
6530 || (ar->end[i] != NULL
6531 && gfc_find_sym_in_expr (sym, ar->end[i])))
6533 gfc_error ("'%s' must not appear in the array specification at "
6534 "%L in the same ALLOCATE statement where it is "
6535 "itself allocated", sym->name, &ar->where);
6536 goto failure;
6541 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
6543 if (ar->dimen_type[i] == DIMEN_ELEMENT
6544 || ar->dimen_type[i] == DIMEN_RANGE)
6546 if (i == (ar->dimen + ar->codimen - 1))
6548 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
6549 "statement at %L", &e->where);
6550 goto failure;
6552 break;
6555 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
6556 && ar->stride[i] == NULL)
6557 break;
6559 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
6560 &e->where);
6561 goto failure;
6564 if (codimension)
6566 gfc_error ("Sorry, allocatable coarrays are no yet supported coarray "
6567 "at %L", &e->where);
6568 goto failure;
6571 success:
6572 return SUCCESS;
6574 failure:
6575 return FAILURE;
6578 static void
6579 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6581 gfc_expr *stat, *errmsg, *pe, *qe;
6582 gfc_alloc *a, *p, *q;
6584 stat = code->expr1 ? code->expr1 : NULL;
6586 errmsg = code->expr2 ? code->expr2 : NULL;
6588 /* Check the stat variable. */
6589 if (stat)
6591 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
6592 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
6593 stat->symtree->n.sym->name, &stat->where);
6595 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
6596 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
6597 &stat->where);
6599 if ((stat->ts.type != BT_INTEGER
6600 && !(stat->ref && (stat->ref->type == REF_ARRAY
6601 || stat->ref->type == REF_COMPONENT)))
6602 || stat->rank > 0)
6603 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6604 "variable", &stat->where);
6606 for (p = code->ext.alloc.list; p; p = p->next)
6607 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
6608 gfc_error ("Stat-variable at %L shall not be %sd within "
6609 "the same %s statement", &stat->where, fcn, fcn);
6612 /* Check the errmsg variable. */
6613 if (errmsg)
6615 if (!stat)
6616 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6617 &errmsg->where);
6619 if (errmsg->symtree->n.sym->attr.intent == INTENT_IN)
6620 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
6621 errmsg->symtree->n.sym->name, &errmsg->where);
6623 if (gfc_pure (NULL) && gfc_impure_variable (errmsg->symtree->n.sym))
6624 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
6625 &errmsg->where);
6627 if ((errmsg->ts.type != BT_CHARACTER
6628 && !(errmsg->ref
6629 && (errmsg->ref->type == REF_ARRAY
6630 || errmsg->ref->type == REF_COMPONENT)))
6631 || errmsg->rank > 0 )
6632 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
6633 "variable", &errmsg->where);
6635 for (p = code->ext.alloc.list; p; p = p->next)
6636 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
6637 gfc_error ("Errmsg-variable at %L shall not be %sd within "
6638 "the same %s statement", &errmsg->where, fcn, fcn);
6641 /* Check that an allocate-object appears only once in the statement.
6642 FIXME: Checking derived types is disabled. */
6643 for (p = code->ext.alloc.list; p; p = p->next)
6645 pe = p->expr;
6646 if ((pe->ref && pe->ref->type != REF_COMPONENT)
6647 && (pe->symtree->n.sym->ts.type != BT_DERIVED))
6649 for (q = p->next; q; q = q->next)
6651 qe = q->expr;
6652 if ((qe->ref && qe->ref->type != REF_COMPONENT)
6653 && (qe->symtree->n.sym->ts.type != BT_DERIVED)
6654 && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
6655 gfc_error ("Allocate-object at %L also appears at %L",
6656 &pe->where, &qe->where);
6661 if (strcmp (fcn, "ALLOCATE") == 0)
6663 for (a = code->ext.alloc.list; a; a = a->next)
6664 resolve_allocate_expr (a->expr, code);
6666 else
6668 for (a = code->ext.alloc.list; a; a = a->next)
6669 resolve_deallocate_expr (a->expr);
6674 /************ SELECT CASE resolution subroutines ************/
6676 /* Callback function for our mergesort variant. Determines interval
6677 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
6678 op1 > op2. Assumes we're not dealing with the default case.
6679 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
6680 There are nine situations to check. */
6682 static int
6683 compare_cases (const gfc_case *op1, const gfc_case *op2)
6685 int retval;
6687 if (op1->low == NULL) /* op1 = (:L) */
6689 /* op2 = (:N), so overlap. */
6690 retval = 0;
6691 /* op2 = (M:) or (M:N), L < M */
6692 if (op2->low != NULL
6693 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6694 retval = -1;
6696 else if (op1->high == NULL) /* op1 = (K:) */
6698 /* op2 = (M:), so overlap. */
6699 retval = 0;
6700 /* op2 = (:N) or (M:N), K > N */
6701 if (op2->high != NULL
6702 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6703 retval = 1;
6705 else /* op1 = (K:L) */
6707 if (op2->low == NULL) /* op2 = (:N), K > N */
6708 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6709 ? 1 : 0;
6710 else if (op2->high == NULL) /* op2 = (M:), L < M */
6711 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6712 ? -1 : 0;
6713 else /* op2 = (M:N) */
6715 retval = 0;
6716 /* L < M */
6717 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6718 retval = -1;
6719 /* K > N */
6720 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6721 retval = 1;
6725 return retval;
6729 /* Merge-sort a double linked case list, detecting overlap in the
6730 process. LIST is the head of the double linked case list before it
6731 is sorted. Returns the head of the sorted list if we don't see any
6732 overlap, or NULL otherwise. */
6734 static gfc_case *
6735 check_case_overlap (gfc_case *list)
6737 gfc_case *p, *q, *e, *tail;
6738 int insize, nmerges, psize, qsize, cmp, overlap_seen;
6740 /* If the passed list was empty, return immediately. */
6741 if (!list)
6742 return NULL;
6744 overlap_seen = 0;
6745 insize = 1;
6747 /* Loop unconditionally. The only exit from this loop is a return
6748 statement, when we've finished sorting the case list. */
6749 for (;;)
6751 p = list;
6752 list = NULL;
6753 tail = NULL;
6755 /* Count the number of merges we do in this pass. */
6756 nmerges = 0;
6758 /* Loop while there exists a merge to be done. */
6759 while (p)
6761 int i;
6763 /* Count this merge. */
6764 nmerges++;
6766 /* Cut the list in two pieces by stepping INSIZE places
6767 forward in the list, starting from P. */
6768 psize = 0;
6769 q = p;
6770 for (i = 0; i < insize; i++)
6772 psize++;
6773 q = q->right;
6774 if (!q)
6775 break;
6777 qsize = insize;
6779 /* Now we have two lists. Merge them! */
6780 while (psize > 0 || (qsize > 0 && q != NULL))
6782 /* See from which the next case to merge comes from. */
6783 if (psize == 0)
6785 /* P is empty so the next case must come from Q. */
6786 e = q;
6787 q = q->right;
6788 qsize--;
6790 else if (qsize == 0 || q == NULL)
6792 /* Q is empty. */
6793 e = p;
6794 p = p->right;
6795 psize--;
6797 else
6799 cmp = compare_cases (p, q);
6800 if (cmp < 0)
6802 /* The whole case range for P is less than the
6803 one for Q. */
6804 e = p;
6805 p = p->right;
6806 psize--;
6808 else if (cmp > 0)
6810 /* The whole case range for Q is greater than
6811 the case range for P. */
6812 e = q;
6813 q = q->right;
6814 qsize--;
6816 else
6818 /* The cases overlap, or they are the same
6819 element in the list. Either way, we must
6820 issue an error and get the next case from P. */
6821 /* FIXME: Sort P and Q by line number. */
6822 gfc_error ("CASE label at %L overlaps with CASE "
6823 "label at %L", &p->where, &q->where);
6824 overlap_seen = 1;
6825 e = p;
6826 p = p->right;
6827 psize--;
6831 /* Add the next element to the merged list. */
6832 if (tail)
6833 tail->right = e;
6834 else
6835 list = e;
6836 e->left = tail;
6837 tail = e;
6840 /* P has now stepped INSIZE places along, and so has Q. So
6841 they're the same. */
6842 p = q;
6844 tail->right = NULL;
6846 /* If we have done only one merge or none at all, we've
6847 finished sorting the cases. */
6848 if (nmerges <= 1)
6850 if (!overlap_seen)
6851 return list;
6852 else
6853 return NULL;
6856 /* Otherwise repeat, merging lists twice the size. */
6857 insize *= 2;
6862 /* Check to see if an expression is suitable for use in a CASE statement.
6863 Makes sure that all case expressions are scalar constants of the same
6864 type. Return FAILURE if anything is wrong. */
6866 static gfc_try
6867 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
6869 if (e == NULL) return SUCCESS;
6871 if (e->ts.type != case_expr->ts.type)
6873 gfc_error ("Expression in CASE statement at %L must be of type %s",
6874 &e->where, gfc_basic_typename (case_expr->ts.type));
6875 return FAILURE;
6878 /* C805 (R808) For a given case-construct, each case-value shall be of
6879 the same type as case-expr. For character type, length differences
6880 are allowed, but the kind type parameters shall be the same. */
6882 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
6884 gfc_error ("Expression in CASE statement at %L must be of kind %d",
6885 &e->where, case_expr->ts.kind);
6886 return FAILURE;
6889 /* Convert the case value kind to that of case expression kind, if needed.
6890 FIXME: Should a warning be issued? */
6891 if (e->ts.kind != case_expr->ts.kind)
6892 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
6894 if (e->rank != 0)
6896 gfc_error ("Expression in CASE statement at %L must be scalar",
6897 &e->where);
6898 return FAILURE;
6901 return SUCCESS;
6905 /* Given a completely parsed select statement, we:
6907 - Validate all expressions and code within the SELECT.
6908 - Make sure that the selection expression is not of the wrong type.
6909 - Make sure that no case ranges overlap.
6910 - Eliminate unreachable cases and unreachable code resulting from
6911 removing case labels.
6913 The standard does allow unreachable cases, e.g. CASE (5:3). But
6914 they are a hassle for code generation, and to prevent that, we just
6915 cut them out here. This is not necessary for overlapping cases
6916 because they are illegal and we never even try to generate code.
6918 We have the additional caveat that a SELECT construct could have
6919 been a computed GOTO in the source code. Fortunately we can fairly
6920 easily work around that here: The case_expr for a "real" SELECT CASE
6921 is in code->expr1, but for a computed GOTO it is in code->expr2. All
6922 we have to do is make sure that the case_expr is a scalar integer
6923 expression. */
6925 static void
6926 resolve_select (gfc_code *code)
6928 gfc_code *body;
6929 gfc_expr *case_expr;
6930 gfc_case *cp, *default_case, *tail, *head;
6931 int seen_unreachable;
6932 int seen_logical;
6933 int ncases;
6934 bt type;
6935 gfc_try t;
6937 if (code->expr1 == NULL)
6939 /* This was actually a computed GOTO statement. */
6940 case_expr = code->expr2;
6941 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
6942 gfc_error ("Selection expression in computed GOTO statement "
6943 "at %L must be a scalar integer expression",
6944 &case_expr->where);
6946 /* Further checking is not necessary because this SELECT was built
6947 by the compiler, so it should always be OK. Just move the
6948 case_expr from expr2 to expr so that we can handle computed
6949 GOTOs as normal SELECTs from here on. */
6950 code->expr1 = code->expr2;
6951 code->expr2 = NULL;
6952 return;
6955 case_expr = code->expr1;
6957 type = case_expr->ts.type;
6958 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
6960 gfc_error ("Argument of SELECT statement at %L cannot be %s",
6961 &case_expr->where, gfc_typename (&case_expr->ts));
6963 /* Punt. Going on here just produce more garbage error messages. */
6964 return;
6967 if (case_expr->rank != 0)
6969 gfc_error ("Argument of SELECT statement at %L must be a scalar "
6970 "expression", &case_expr->where);
6972 /* Punt. */
6973 return;
6976 /* PR 19168 has a long discussion concerning a mismatch of the kinds
6977 of the SELECT CASE expression and its CASE values. Walk the lists
6978 of case values, and if we find a mismatch, promote case_expr to
6979 the appropriate kind. */
6981 if (type == BT_LOGICAL || type == BT_INTEGER)
6983 for (body = code->block; body; body = body->block)
6985 /* Walk the case label list. */
6986 for (cp = body->ext.case_list; cp; cp = cp->next)
6988 /* Intercept the DEFAULT case. It does not have a kind. */
6989 if (cp->low == NULL && cp->high == NULL)
6990 continue;
6992 /* Unreachable case ranges are discarded, so ignore. */
6993 if (cp->low != NULL && cp->high != NULL
6994 && cp->low != cp->high
6995 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6996 continue;
6998 /* FIXME: Should a warning be issued? */
6999 if (cp->low != NULL
7000 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7001 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7003 if (cp->high != NULL
7004 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7005 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7010 /* Assume there is no DEFAULT case. */
7011 default_case = NULL;
7012 head = tail = NULL;
7013 ncases = 0;
7014 seen_logical = 0;
7016 for (body = code->block; body; body = body->block)
7018 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7019 t = SUCCESS;
7020 seen_unreachable = 0;
7022 /* Walk the case label list, making sure that all case labels
7023 are legal. */
7024 for (cp = body->ext.case_list; cp; cp = cp->next)
7026 /* Count the number of cases in the whole construct. */
7027 ncases++;
7029 /* Intercept the DEFAULT case. */
7030 if (cp->low == NULL && cp->high == NULL)
7032 if (default_case != NULL)
7034 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7035 "by a second DEFAULT CASE at %L",
7036 &default_case->where, &cp->where);
7037 t = FAILURE;
7038 break;
7040 else
7042 default_case = cp;
7043 continue;
7047 /* Deal with single value cases and case ranges. Errors are
7048 issued from the validation function. */
7049 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
7050 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
7052 t = FAILURE;
7053 break;
7056 if (type == BT_LOGICAL
7057 && ((cp->low == NULL || cp->high == NULL)
7058 || cp->low != cp->high))
7060 gfc_error ("Logical range in CASE statement at %L is not "
7061 "allowed", &cp->low->where);
7062 t = FAILURE;
7063 break;
7066 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7068 int value;
7069 value = cp->low->value.logical == 0 ? 2 : 1;
7070 if (value & seen_logical)
7072 gfc_error ("constant logical value in CASE statement "
7073 "is repeated at %L",
7074 &cp->low->where);
7075 t = FAILURE;
7076 break;
7078 seen_logical |= value;
7081 if (cp->low != NULL && cp->high != NULL
7082 && cp->low != cp->high
7083 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7085 if (gfc_option.warn_surprising)
7086 gfc_warning ("Range specification at %L can never "
7087 "be matched", &cp->where);
7089 cp->unreachable = 1;
7090 seen_unreachable = 1;
7092 else
7094 /* If the case range can be matched, it can also overlap with
7095 other cases. To make sure it does not, we put it in a
7096 double linked list here. We sort that with a merge sort
7097 later on to detect any overlapping cases. */
7098 if (!head)
7100 head = tail = cp;
7101 head->right = head->left = NULL;
7103 else
7105 tail->right = cp;
7106 tail->right->left = tail;
7107 tail = tail->right;
7108 tail->right = NULL;
7113 /* It there was a failure in the previous case label, give up
7114 for this case label list. Continue with the next block. */
7115 if (t == FAILURE)
7116 continue;
7118 /* See if any case labels that are unreachable have been seen.
7119 If so, we eliminate them. This is a bit of a kludge because
7120 the case lists for a single case statement (label) is a
7121 single forward linked lists. */
7122 if (seen_unreachable)
7124 /* Advance until the first case in the list is reachable. */
7125 while (body->ext.case_list != NULL
7126 && body->ext.case_list->unreachable)
7128 gfc_case *n = body->ext.case_list;
7129 body->ext.case_list = body->ext.case_list->next;
7130 n->next = NULL;
7131 gfc_free_case_list (n);
7134 /* Strip all other unreachable cases. */
7135 if (body->ext.case_list)
7137 for (cp = body->ext.case_list; cp->next; cp = cp->next)
7139 if (cp->next->unreachable)
7141 gfc_case *n = cp->next;
7142 cp->next = cp->next->next;
7143 n->next = NULL;
7144 gfc_free_case_list (n);
7151 /* See if there were overlapping cases. If the check returns NULL,
7152 there was overlap. In that case we don't do anything. If head
7153 is non-NULL, we prepend the DEFAULT case. The sorted list can
7154 then used during code generation for SELECT CASE constructs with
7155 a case expression of a CHARACTER type. */
7156 if (head)
7158 head = check_case_overlap (head);
7160 /* Prepend the default_case if it is there. */
7161 if (head != NULL && default_case)
7163 default_case->left = NULL;
7164 default_case->right = head;
7165 head->left = default_case;
7169 /* Eliminate dead blocks that may be the result if we've seen
7170 unreachable case labels for a block. */
7171 for (body = code; body && body->block; body = body->block)
7173 if (body->block->ext.case_list == NULL)
7175 /* Cut the unreachable block from the code chain. */
7176 gfc_code *c = body->block;
7177 body->block = c->block;
7179 /* Kill the dead block, but not the blocks below it. */
7180 c->block = NULL;
7181 gfc_free_statements (c);
7185 /* More than two cases is legal but insane for logical selects.
7186 Issue a warning for it. */
7187 if (gfc_option.warn_surprising && type == BT_LOGICAL
7188 && ncases > 2)
7189 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7190 &code->loc);
7194 /* Check if a derived type is extensible. */
7196 bool
7197 gfc_type_is_extensible (gfc_symbol *sym)
7199 return !(sym->attr.is_bind_c || sym->attr.sequence);
7203 /* Resolve a SELECT TYPE statement. */
7205 static void
7206 resolve_select_type (gfc_code *code)
7208 gfc_symbol *selector_type;
7209 gfc_code *body, *new_st, *if_st, *tail;
7210 gfc_code *class_is = NULL, *default_case = NULL;
7211 gfc_case *c;
7212 gfc_symtree *st;
7213 char name[GFC_MAX_SYMBOL_LEN];
7214 gfc_namespace *ns;
7215 int error = 0;
7217 ns = code->ext.ns;
7218 gfc_resolve (ns);
7220 if (code->expr2)
7221 selector_type = code->expr2->ts.u.derived->components->ts.u.derived;
7222 else
7223 selector_type = code->expr1->ts.u.derived->components->ts.u.derived;
7225 /* Loop over TYPE IS / CLASS IS cases. */
7226 for (body = code->block; body; body = body->block)
7228 c = body->ext.case_list;
7230 /* Check F03:C815. */
7231 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7232 && !gfc_type_is_extensible (c->ts.u.derived))
7234 gfc_error ("Derived type '%s' at %L must be extensible",
7235 c->ts.u.derived->name, &c->where);
7236 error++;
7237 continue;
7240 /* Check F03:C816. */
7241 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7242 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
7244 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7245 c->ts.u.derived->name, &c->where, selector_type->name);
7246 error++;
7247 continue;
7250 /* Intercept the DEFAULT case. */
7251 if (c->ts.type == BT_UNKNOWN)
7253 /* Check F03:C818. */
7254 if (default_case)
7256 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7257 "by a second DEFAULT CASE at %L",
7258 &default_case->ext.case_list->where, &c->where);
7259 error++;
7260 continue;
7262 else
7263 default_case = body;
7267 if (error>0)
7268 return;
7270 if (code->expr2)
7272 /* Insert assignment for selector variable. */
7273 new_st = gfc_get_code ();
7274 new_st->op = EXEC_ASSIGN;
7275 new_st->expr1 = gfc_copy_expr (code->expr1);
7276 new_st->expr2 = gfc_copy_expr (code->expr2);
7277 ns->code = new_st;
7280 /* Put SELECT TYPE statement inside a BLOCK. */
7281 new_st = gfc_get_code ();
7282 new_st->op = code->op;
7283 new_st->expr1 = code->expr1;
7284 new_st->expr2 = code->expr2;
7285 new_st->block = code->block;
7286 if (!ns->code)
7287 ns->code = new_st;
7288 else
7289 ns->code->next = new_st;
7290 code->op = EXEC_BLOCK;
7291 code->expr1 = code->expr2 = NULL;
7292 code->block = NULL;
7294 code = new_st;
7296 /* Transform to EXEC_SELECT. */
7297 code->op = EXEC_SELECT;
7298 gfc_add_component_ref (code->expr1, "$vptr");
7299 gfc_add_component_ref (code->expr1, "$hash");
7301 /* Loop over TYPE IS / CLASS IS cases. */
7302 for (body = code->block; body; body = body->block)
7304 c = body->ext.case_list;
7306 if (c->ts.type == BT_DERIVED)
7307 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
7308 c->ts.u.derived->hash_value);
7310 else if (c->ts.type == BT_UNKNOWN)
7311 continue;
7313 /* Assign temporary to selector. */
7314 if (c->ts.type == BT_CLASS)
7315 sprintf (name, "tmp$class$%s", c->ts.u.derived->name);
7316 else
7317 sprintf (name, "tmp$type$%s", c->ts.u.derived->name);
7318 st = gfc_find_symtree (ns->sym_root, name);
7319 new_st = gfc_get_code ();
7320 new_st->expr1 = gfc_get_variable_expr (st);
7321 new_st->expr2 = gfc_get_variable_expr (code->expr1->symtree);
7322 if (c->ts.type == BT_DERIVED)
7324 new_st->op = EXEC_POINTER_ASSIGN;
7325 gfc_add_component_ref (new_st->expr2, "$data");
7327 else
7328 new_st->op = EXEC_POINTER_ASSIGN;
7329 new_st->next = body->next;
7330 body->next = new_st;
7333 /* Take out CLASS IS cases for separate treatment. */
7334 body = code;
7335 while (body && body->block)
7337 if (body->block->ext.case_list->ts.type == BT_CLASS)
7339 /* Add to class_is list. */
7340 if (class_is == NULL)
7342 class_is = body->block;
7343 tail = class_is;
7345 else
7347 for (tail = class_is; tail->block; tail = tail->block) ;
7348 tail->block = body->block;
7349 tail = tail->block;
7351 /* Remove from EXEC_SELECT list. */
7352 body->block = body->block->block;
7353 tail->block = NULL;
7355 else
7356 body = body->block;
7359 if (class_is)
7361 gfc_symbol *vtab;
7363 if (!default_case)
7365 /* Add a default case to hold the CLASS IS cases. */
7366 for (tail = code; tail->block; tail = tail->block) ;
7367 tail->block = gfc_get_code ();
7368 tail = tail->block;
7369 tail->op = EXEC_SELECT_TYPE;
7370 tail->ext.case_list = gfc_get_case ();
7371 tail->ext.case_list->ts.type = BT_UNKNOWN;
7372 tail->next = NULL;
7373 default_case = tail;
7376 /* More than one CLASS IS block? */
7377 if (class_is->block)
7379 gfc_code **c1,*c2;
7380 bool swapped;
7381 /* Sort CLASS IS blocks by extension level. */
7384 swapped = false;
7385 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
7387 c2 = (*c1)->block;
7388 /* F03:C817 (check for doubles). */
7389 if ((*c1)->ext.case_list->ts.u.derived->hash_value
7390 == c2->ext.case_list->ts.u.derived->hash_value)
7392 gfc_error ("Double CLASS IS block in SELECT TYPE "
7393 "statement at %L", &c2->ext.case_list->where);
7394 return;
7396 if ((*c1)->ext.case_list->ts.u.derived->attr.extension
7397 < c2->ext.case_list->ts.u.derived->attr.extension)
7399 /* Swap. */
7400 (*c1)->block = c2->block;
7401 c2->block = *c1;
7402 *c1 = c2;
7403 swapped = true;
7407 while (swapped);
7410 /* Generate IF chain. */
7411 if_st = gfc_get_code ();
7412 if_st->op = EXEC_IF;
7413 new_st = if_st;
7414 for (body = class_is; body; body = body->block)
7416 new_st->block = gfc_get_code ();
7417 new_st = new_st->block;
7418 new_st->op = EXEC_IF;
7419 /* Set up IF condition: Call _gfortran_is_extension_of. */
7420 new_st->expr1 = gfc_get_expr ();
7421 new_st->expr1->expr_type = EXPR_FUNCTION;
7422 new_st->expr1->ts.type = BT_LOGICAL;
7423 new_st->expr1->ts.kind = 4;
7424 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
7425 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
7426 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
7427 /* Set up arguments. */
7428 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
7429 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
7430 gfc_add_component_ref (new_st->expr1->value.function.actual->expr, "$vptr");
7431 vtab = gfc_find_derived_vtab (body->ext.case_list->ts.u.derived);
7432 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
7433 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
7434 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
7435 new_st->next = body->next;
7437 if (default_case->next)
7439 new_st->block = gfc_get_code ();
7440 new_st = new_st->block;
7441 new_st->op = EXEC_IF;
7442 new_st->next = default_case->next;
7445 /* Replace CLASS DEFAULT code by the IF chain. */
7446 default_case->next = if_st;
7449 resolve_select (code);
7454 /* Resolve a transfer statement. This is making sure that:
7455 -- a derived type being transferred has only non-pointer components
7456 -- a derived type being transferred doesn't have private components, unless
7457 it's being transferred from the module where the type was defined
7458 -- we're not trying to transfer a whole assumed size array. */
7460 static void
7461 resolve_transfer (gfc_code *code)
7463 gfc_typespec *ts;
7464 gfc_symbol *sym;
7465 gfc_ref *ref;
7466 gfc_expr *exp;
7468 exp = code->expr1;
7470 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
7471 return;
7473 sym = exp->symtree->n.sym;
7474 ts = &sym->ts;
7476 /* Go to actual component transferred. */
7477 for (ref = code->expr1->ref; ref; ref = ref->next)
7478 if (ref->type == REF_COMPONENT)
7479 ts = &ref->u.c.component->ts;
7481 if (ts->type == BT_DERIVED)
7483 /* Check that transferred derived type doesn't contain POINTER
7484 components. */
7485 if (ts->u.derived->attr.pointer_comp)
7487 gfc_error ("Data transfer element at %L cannot have "
7488 "POINTER components", &code->loc);
7489 return;
7492 if (ts->u.derived->attr.alloc_comp)
7494 gfc_error ("Data transfer element at %L cannot have "
7495 "ALLOCATABLE components", &code->loc);
7496 return;
7499 if (derived_inaccessible (ts->u.derived))
7501 gfc_error ("Data transfer element at %L cannot have "
7502 "PRIVATE components",&code->loc);
7503 return;
7507 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
7508 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
7510 gfc_error ("Data transfer element at %L cannot be a full reference to "
7511 "an assumed-size array", &code->loc);
7512 return;
7517 /*********** Toplevel code resolution subroutines ***********/
7519 /* Find the set of labels that are reachable from this block. We also
7520 record the last statement in each block. */
7522 static void
7523 find_reachable_labels (gfc_code *block)
7525 gfc_code *c;
7527 if (!block)
7528 return;
7530 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
7532 /* Collect labels in this block. We don't keep those corresponding
7533 to END {IF|SELECT}, these are checked in resolve_branch by going
7534 up through the code_stack. */
7535 for (c = block; c; c = c->next)
7537 if (c->here && c->op != EXEC_END_BLOCK)
7538 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
7541 /* Merge with labels from parent block. */
7542 if (cs_base->prev)
7544 gcc_assert (cs_base->prev->reachable_labels);
7545 bitmap_ior_into (cs_base->reachable_labels,
7546 cs_base->prev->reachable_labels);
7551 static void
7552 resolve_sync (gfc_code *code)
7554 /* Check imageset. The * case matches expr1 == NULL. */
7555 if (code->expr1)
7557 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
7558 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
7559 "INTEGER expression", &code->expr1->where);
7560 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
7561 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
7562 gfc_error ("Imageset argument at %L must between 1 and num_images()",
7563 &code->expr1->where);
7564 else if (code->expr1->expr_type == EXPR_ARRAY
7565 && gfc_simplify_expr (code->expr1, 0) == SUCCESS)
7567 gfc_constructor *cons;
7568 cons = gfc_constructor_first (code->expr1->value.constructor);
7569 for (; cons; cons = gfc_constructor_next (cons))
7570 if (cons->expr->expr_type == EXPR_CONSTANT
7571 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
7572 gfc_error ("Imageset argument at %L must between 1 and "
7573 "num_images()", &cons->expr->where);
7577 /* Check STAT. */
7578 if (code->expr2
7579 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
7580 || code->expr2->expr_type != EXPR_VARIABLE))
7581 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
7582 &code->expr2->where);
7584 /* Check ERRMSG. */
7585 if (code->expr3
7586 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
7587 || code->expr3->expr_type != EXPR_VARIABLE))
7588 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
7589 &code->expr3->where);
7593 /* Given a branch to a label, see if the branch is conforming.
7594 The code node describes where the branch is located. */
7596 static void
7597 resolve_branch (gfc_st_label *label, gfc_code *code)
7599 code_stack *stack;
7601 if (label == NULL)
7602 return;
7604 /* Step one: is this a valid branching target? */
7606 if (label->defined == ST_LABEL_UNKNOWN)
7608 gfc_error ("Label %d referenced at %L is never defined", label->value,
7609 &label->where);
7610 return;
7613 if (label->defined != ST_LABEL_TARGET)
7615 gfc_error ("Statement at %L is not a valid branch target statement "
7616 "for the branch statement at %L", &label->where, &code->loc);
7617 return;
7620 /* Step two: make sure this branch is not a branch to itself ;-) */
7622 if (code->here == label)
7624 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
7625 return;
7628 /* Step three: See if the label is in the same block as the
7629 branching statement. The hard work has been done by setting up
7630 the bitmap reachable_labels. */
7632 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
7634 /* Check now whether there is a CRITICAL construct; if so, check
7635 whether the label is still visible outside of the CRITICAL block,
7636 which is invalid. */
7637 for (stack = cs_base; stack; stack = stack->prev)
7638 if (stack->current->op == EXEC_CRITICAL
7639 && bitmap_bit_p (stack->reachable_labels, label->value))
7640 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
7641 " at %L", &code->loc, &label->where);
7643 return;
7646 /* Step four: If we haven't found the label in the bitmap, it may
7647 still be the label of the END of the enclosing block, in which
7648 case we find it by going up the code_stack. */
7650 for (stack = cs_base; stack; stack = stack->prev)
7652 if (stack->current->next && stack->current->next->here == label)
7653 break;
7654 if (stack->current->op == EXEC_CRITICAL)
7656 /* Note: A label at END CRITICAL does not leave the CRITICAL
7657 construct as END CRITICAL is still part of it. */
7658 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
7659 " at %L", &code->loc, &label->where);
7660 return;
7664 if (stack)
7666 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
7667 return;
7670 /* The label is not in an enclosing block, so illegal. This was
7671 allowed in Fortran 66, so we allow it as extension. No
7672 further checks are necessary in this case. */
7673 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
7674 "as the GOTO statement at %L", &label->where,
7675 &code->loc);
7676 return;
7680 /* Check whether EXPR1 has the same shape as EXPR2. */
7682 static gfc_try
7683 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
7685 mpz_t shape[GFC_MAX_DIMENSIONS];
7686 mpz_t shape2[GFC_MAX_DIMENSIONS];
7687 gfc_try result = FAILURE;
7688 int i;
7690 /* Compare the rank. */
7691 if (expr1->rank != expr2->rank)
7692 return result;
7694 /* Compare the size of each dimension. */
7695 for (i=0; i<expr1->rank; i++)
7697 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
7698 goto ignore;
7700 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
7701 goto ignore;
7703 if (mpz_cmp (shape[i], shape2[i]))
7704 goto over;
7707 /* When either of the two expression is an assumed size array, we
7708 ignore the comparison of dimension sizes. */
7709 ignore:
7710 result = SUCCESS;
7712 over:
7713 for (i--; i >= 0; i--)
7715 mpz_clear (shape[i]);
7716 mpz_clear (shape2[i]);
7718 return result;
7722 /* Check whether a WHERE assignment target or a WHERE mask expression
7723 has the same shape as the outmost WHERE mask expression. */
7725 static void
7726 resolve_where (gfc_code *code, gfc_expr *mask)
7728 gfc_code *cblock;
7729 gfc_code *cnext;
7730 gfc_expr *e = NULL;
7732 cblock = code->block;
7734 /* Store the first WHERE mask-expr of the WHERE statement or construct.
7735 In case of nested WHERE, only the outmost one is stored. */
7736 if (mask == NULL) /* outmost WHERE */
7737 e = cblock->expr1;
7738 else /* inner WHERE */
7739 e = mask;
7741 while (cblock)
7743 if (cblock->expr1)
7745 /* Check if the mask-expr has a consistent shape with the
7746 outmost WHERE mask-expr. */
7747 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
7748 gfc_error ("WHERE mask at %L has inconsistent shape",
7749 &cblock->expr1->where);
7752 /* the assignment statement of a WHERE statement, or the first
7753 statement in where-body-construct of a WHERE construct */
7754 cnext = cblock->next;
7755 while (cnext)
7757 switch (cnext->op)
7759 /* WHERE assignment statement */
7760 case EXEC_ASSIGN:
7762 /* Check shape consistent for WHERE assignment target. */
7763 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
7764 gfc_error ("WHERE assignment target at %L has "
7765 "inconsistent shape", &cnext->expr1->where);
7766 break;
7769 case EXEC_ASSIGN_CALL:
7770 resolve_call (cnext);
7771 if (!cnext->resolved_sym->attr.elemental)
7772 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7773 &cnext->ext.actual->expr->where);
7774 break;
7776 /* WHERE or WHERE construct is part of a where-body-construct */
7777 case EXEC_WHERE:
7778 resolve_where (cnext, e);
7779 break;
7781 default:
7782 gfc_error ("Unsupported statement inside WHERE at %L",
7783 &cnext->loc);
7785 /* the next statement within the same where-body-construct */
7786 cnext = cnext->next;
7788 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7789 cblock = cblock->block;
7794 /* Resolve assignment in FORALL construct.
7795 NVAR is the number of FORALL index variables, and VAR_EXPR records the
7796 FORALL index variables. */
7798 static void
7799 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
7801 int n;
7803 for (n = 0; n < nvar; n++)
7805 gfc_symbol *forall_index;
7807 forall_index = var_expr[n]->symtree->n.sym;
7809 /* Check whether the assignment target is one of the FORALL index
7810 variable. */
7811 if ((code->expr1->expr_type == EXPR_VARIABLE)
7812 && (code->expr1->symtree->n.sym == forall_index))
7813 gfc_error ("Assignment to a FORALL index variable at %L",
7814 &code->expr1->where);
7815 else
7817 /* If one of the FORALL index variables doesn't appear in the
7818 assignment variable, then there could be a many-to-one
7819 assignment. Emit a warning rather than an error because the
7820 mask could be resolving this problem. */
7821 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
7822 gfc_warning ("The FORALL with index '%s' is not used on the "
7823 "left side of the assignment at %L and so might "
7824 "cause multiple assignment to this object",
7825 var_expr[n]->symtree->name, &code->expr1->where);
7831 /* Resolve WHERE statement in FORALL construct. */
7833 static void
7834 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
7835 gfc_expr **var_expr)
7837 gfc_code *cblock;
7838 gfc_code *cnext;
7840 cblock = code->block;
7841 while (cblock)
7843 /* the assignment statement of a WHERE statement, or the first
7844 statement in where-body-construct of a WHERE construct */
7845 cnext = cblock->next;
7846 while (cnext)
7848 switch (cnext->op)
7850 /* WHERE assignment statement */
7851 case EXEC_ASSIGN:
7852 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
7853 break;
7855 /* WHERE operator assignment statement */
7856 case EXEC_ASSIGN_CALL:
7857 resolve_call (cnext);
7858 if (!cnext->resolved_sym->attr.elemental)
7859 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7860 &cnext->ext.actual->expr->where);
7861 break;
7863 /* WHERE or WHERE construct is part of a where-body-construct */
7864 case EXEC_WHERE:
7865 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
7866 break;
7868 default:
7869 gfc_error ("Unsupported statement inside WHERE at %L",
7870 &cnext->loc);
7872 /* the next statement within the same where-body-construct */
7873 cnext = cnext->next;
7875 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7876 cblock = cblock->block;
7881 /* Traverse the FORALL body to check whether the following errors exist:
7882 1. For assignment, check if a many-to-one assignment happens.
7883 2. For WHERE statement, check the WHERE body to see if there is any
7884 many-to-one assignment. */
7886 static void
7887 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
7889 gfc_code *c;
7891 c = code->block->next;
7892 while (c)
7894 switch (c->op)
7896 case EXEC_ASSIGN:
7897 case EXEC_POINTER_ASSIGN:
7898 gfc_resolve_assign_in_forall (c, nvar, var_expr);
7899 break;
7901 case EXEC_ASSIGN_CALL:
7902 resolve_call (c);
7903 break;
7905 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
7906 there is no need to handle it here. */
7907 case EXEC_FORALL:
7908 break;
7909 case EXEC_WHERE:
7910 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
7911 break;
7912 default:
7913 break;
7915 /* The next statement in the FORALL body. */
7916 c = c->next;
7921 /* Counts the number of iterators needed inside a forall construct, including
7922 nested forall constructs. This is used to allocate the needed memory
7923 in gfc_resolve_forall. */
7925 static int
7926 gfc_count_forall_iterators (gfc_code *code)
7928 int max_iters, sub_iters, current_iters;
7929 gfc_forall_iterator *fa;
7931 gcc_assert(code->op == EXEC_FORALL);
7932 max_iters = 0;
7933 current_iters = 0;
7935 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7936 current_iters ++;
7938 code = code->block->next;
7940 while (code)
7942 if (code->op == EXEC_FORALL)
7944 sub_iters = gfc_count_forall_iterators (code);
7945 if (sub_iters > max_iters)
7946 max_iters = sub_iters;
7948 code = code->next;
7951 return current_iters + max_iters;
7955 /* Given a FORALL construct, first resolve the FORALL iterator, then call
7956 gfc_resolve_forall_body to resolve the FORALL body. */
7958 static void
7959 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
7961 static gfc_expr **var_expr;
7962 static int total_var = 0;
7963 static int nvar = 0;
7964 int old_nvar, tmp;
7965 gfc_forall_iterator *fa;
7966 int i;
7968 old_nvar = nvar;
7970 /* Start to resolve a FORALL construct */
7971 if (forall_save == 0)
7973 /* Count the total number of FORALL index in the nested FORALL
7974 construct in order to allocate the VAR_EXPR with proper size. */
7975 total_var = gfc_count_forall_iterators (code);
7977 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
7978 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
7981 /* The information about FORALL iterator, including FORALL index start, end
7982 and stride. The FORALL index can not appear in start, end or stride. */
7983 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7985 /* Check if any outer FORALL index name is the same as the current
7986 one. */
7987 for (i = 0; i < nvar; i++)
7989 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
7991 gfc_error ("An outer FORALL construct already has an index "
7992 "with this name %L", &fa->var->where);
7996 /* Record the current FORALL index. */
7997 var_expr[nvar] = gfc_copy_expr (fa->var);
7999 nvar++;
8001 /* No memory leak. */
8002 gcc_assert (nvar <= total_var);
8005 /* Resolve the FORALL body. */
8006 gfc_resolve_forall_body (code, nvar, var_expr);
8008 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8009 gfc_resolve_blocks (code->block, ns);
8011 tmp = nvar;
8012 nvar = old_nvar;
8013 /* Free only the VAR_EXPRs allocated in this frame. */
8014 for (i = nvar; i < tmp; i++)
8015 gfc_free_expr (var_expr[i]);
8017 if (nvar == 0)
8019 /* We are in the outermost FORALL construct. */
8020 gcc_assert (forall_save == 0);
8022 /* VAR_EXPR is not needed any more. */
8023 gfc_free (var_expr);
8024 total_var = 0;
8029 /* Resolve a BLOCK construct statement. */
8031 static void
8032 resolve_block_construct (gfc_code* code)
8034 /* Eventually, we may want to do some checks here or handle special stuff.
8035 But so far the only thing we can do is resolving the local namespace. */
8037 gfc_resolve (code->ext.ns);
8041 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8042 DO code nodes. */
8044 static void resolve_code (gfc_code *, gfc_namespace *);
8046 void
8047 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8049 gfc_try t;
8051 for (; b; b = b->block)
8053 t = gfc_resolve_expr (b->expr1);
8054 if (gfc_resolve_expr (b->expr2) == FAILURE)
8055 t = FAILURE;
8057 switch (b->op)
8059 case EXEC_IF:
8060 if (t == SUCCESS && b->expr1 != NULL
8061 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
8062 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8063 &b->expr1->where);
8064 break;
8066 case EXEC_WHERE:
8067 if (t == SUCCESS
8068 && b->expr1 != NULL
8069 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
8070 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8071 &b->expr1->where);
8072 break;
8074 case EXEC_GOTO:
8075 resolve_branch (b->label1, b);
8076 break;
8078 case EXEC_BLOCK:
8079 resolve_block_construct (b);
8080 break;
8082 case EXEC_SELECT:
8083 case EXEC_SELECT_TYPE:
8084 case EXEC_FORALL:
8085 case EXEC_DO:
8086 case EXEC_DO_WHILE:
8087 case EXEC_CRITICAL:
8088 case EXEC_READ:
8089 case EXEC_WRITE:
8090 case EXEC_IOLENGTH:
8091 case EXEC_WAIT:
8092 break;
8094 case EXEC_OMP_ATOMIC:
8095 case EXEC_OMP_CRITICAL:
8096 case EXEC_OMP_DO:
8097 case EXEC_OMP_MASTER:
8098 case EXEC_OMP_ORDERED:
8099 case EXEC_OMP_PARALLEL:
8100 case EXEC_OMP_PARALLEL_DO:
8101 case EXEC_OMP_PARALLEL_SECTIONS:
8102 case EXEC_OMP_PARALLEL_WORKSHARE:
8103 case EXEC_OMP_SECTIONS:
8104 case EXEC_OMP_SINGLE:
8105 case EXEC_OMP_TASK:
8106 case EXEC_OMP_TASKWAIT:
8107 case EXEC_OMP_WORKSHARE:
8108 break;
8110 default:
8111 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
8114 resolve_code (b->next, ns);
8119 /* Does everything to resolve an ordinary assignment. Returns true
8120 if this is an interface assignment. */
8121 static bool
8122 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
8124 bool rval = false;
8125 gfc_expr *lhs;
8126 gfc_expr *rhs;
8127 int llen = 0;
8128 int rlen = 0;
8129 int n;
8130 gfc_ref *ref;
8132 if (gfc_extend_assign (code, ns) == SUCCESS)
8134 gfc_expr** rhsptr;
8136 if (code->op == EXEC_ASSIGN_CALL)
8138 lhs = code->ext.actual->expr;
8139 rhsptr = &code->ext.actual->next->expr;
8141 else
8143 gfc_actual_arglist* args;
8144 gfc_typebound_proc* tbp;
8146 gcc_assert (code->op == EXEC_COMPCALL);
8148 args = code->expr1->value.compcall.actual;
8149 lhs = args->expr;
8150 rhsptr = &args->next->expr;
8152 tbp = code->expr1->value.compcall.tbp;
8153 gcc_assert (!tbp->is_generic);
8156 /* Make a temporary rhs when there is a default initializer
8157 and rhs is the same symbol as the lhs. */
8158 if ((*rhsptr)->expr_type == EXPR_VARIABLE
8159 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
8160 && has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
8161 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
8162 *rhsptr = gfc_get_parentheses (*rhsptr);
8164 return true;
8167 lhs = code->expr1;
8168 rhs = code->expr2;
8170 if (rhs->is_boz
8171 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
8172 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
8173 &code->loc) == FAILURE)
8174 return false;
8176 /* Handle the case of a BOZ literal on the RHS. */
8177 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
8179 int rc;
8180 if (gfc_option.warn_surprising)
8181 gfc_warning ("BOZ literal at %L is bitwise transferred "
8182 "non-integer symbol '%s'", &code->loc,
8183 lhs->symtree->n.sym->name);
8185 if (!gfc_convert_boz (rhs, &lhs->ts))
8186 return false;
8187 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
8189 if (rc == ARITH_UNDERFLOW)
8190 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
8191 ". This check can be disabled with the option "
8192 "-fno-range-check", &rhs->where);
8193 else if (rc == ARITH_OVERFLOW)
8194 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
8195 ". This check can be disabled with the option "
8196 "-fno-range-check", &rhs->where);
8197 else if (rc == ARITH_NAN)
8198 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
8199 ". This check can be disabled with the option "
8200 "-fno-range-check", &rhs->where);
8201 return false;
8206 if (lhs->ts.type == BT_CHARACTER
8207 && gfc_option.warn_character_truncation)
8209 if (lhs->ts.u.cl != NULL
8210 && lhs->ts.u.cl->length != NULL
8211 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8212 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
8214 if (rhs->expr_type == EXPR_CONSTANT)
8215 rlen = rhs->value.character.length;
8217 else if (rhs->ts.u.cl != NULL
8218 && rhs->ts.u.cl->length != NULL
8219 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8220 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
8222 if (rlen && llen && rlen > llen)
8223 gfc_warning_now ("CHARACTER expression will be truncated "
8224 "in assignment (%d/%d) at %L",
8225 llen, rlen, &code->loc);
8228 /* Ensure that a vector index expression for the lvalue is evaluated
8229 to a temporary if the lvalue symbol is referenced in it. */
8230 if (lhs->rank)
8232 for (ref = lhs->ref; ref; ref= ref->next)
8233 if (ref->type == REF_ARRAY)
8235 for (n = 0; n < ref->u.ar.dimen; n++)
8236 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
8237 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
8238 ref->u.ar.start[n]))
8239 ref->u.ar.start[n]
8240 = gfc_get_parentheses (ref->u.ar.start[n]);
8244 if (gfc_pure (NULL))
8246 if (gfc_impure_variable (lhs->symtree->n.sym))
8248 gfc_error ("Cannot assign to variable '%s' in PURE "
8249 "procedure at %L",
8250 lhs->symtree->n.sym->name,
8251 &lhs->where);
8252 return rval;
8255 if (lhs->ts.type == BT_DERIVED
8256 && lhs->expr_type == EXPR_VARIABLE
8257 && lhs->ts.u.derived->attr.pointer_comp
8258 && rhs->expr_type == EXPR_VARIABLE
8259 && (gfc_impure_variable (rhs->symtree->n.sym)
8260 || gfc_is_coindexed (rhs)))
8262 /* F2008, C1283. */
8263 if (gfc_is_coindexed (rhs))
8264 gfc_error ("Coindexed expression at %L is assigned to "
8265 "a derived type variable with a POINTER "
8266 "component in a PURE procedure",
8267 &rhs->where);
8268 else
8269 gfc_error ("The impure variable at %L is assigned to "
8270 "a derived type variable with a POINTER "
8271 "component in a PURE procedure (12.6)",
8272 &rhs->where);
8273 return rval;
8276 /* Fortran 2008, C1283. */
8277 if (gfc_is_coindexed (lhs))
8279 gfc_error ("Assignment to coindexed variable at %L in a PURE "
8280 "procedure", &rhs->where);
8281 return rval;
8285 /* F03:7.4.1.2. */
8286 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
8287 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
8288 if (lhs->ts.type == BT_CLASS)
8290 gfc_error ("Variable must not be polymorphic in assignment at %L",
8291 &lhs->where);
8292 return false;
8295 /* F2008, Section 7.2.1.2. */
8296 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
8298 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
8299 "component in assignment at %L", &lhs->where);
8300 return false;
8303 gfc_check_assign (lhs, rhs, 1);
8304 return false;
8308 /* Given a block of code, recursively resolve everything pointed to by this
8309 code block. */
8311 static void
8312 resolve_code (gfc_code *code, gfc_namespace *ns)
8314 int omp_workshare_save;
8315 int forall_save;
8316 code_stack frame;
8317 gfc_try t;
8319 frame.prev = cs_base;
8320 frame.head = code;
8321 cs_base = &frame;
8323 find_reachable_labels (code);
8325 for (; code; code = code->next)
8327 frame.current = code;
8328 forall_save = forall_flag;
8330 if (code->op == EXEC_FORALL)
8332 forall_flag = 1;
8333 gfc_resolve_forall (code, ns, forall_save);
8334 forall_flag = 2;
8336 else if (code->block)
8338 omp_workshare_save = -1;
8339 switch (code->op)
8341 case EXEC_OMP_PARALLEL_WORKSHARE:
8342 omp_workshare_save = omp_workshare_flag;
8343 omp_workshare_flag = 1;
8344 gfc_resolve_omp_parallel_blocks (code, ns);
8345 break;
8346 case EXEC_OMP_PARALLEL:
8347 case EXEC_OMP_PARALLEL_DO:
8348 case EXEC_OMP_PARALLEL_SECTIONS:
8349 case EXEC_OMP_TASK:
8350 omp_workshare_save = omp_workshare_flag;
8351 omp_workshare_flag = 0;
8352 gfc_resolve_omp_parallel_blocks (code, ns);
8353 break;
8354 case EXEC_OMP_DO:
8355 gfc_resolve_omp_do_blocks (code, ns);
8356 break;
8357 case EXEC_SELECT_TYPE:
8358 gfc_current_ns = code->ext.ns;
8359 gfc_resolve_blocks (code->block, gfc_current_ns);
8360 gfc_current_ns = ns;
8361 break;
8362 case EXEC_OMP_WORKSHARE:
8363 omp_workshare_save = omp_workshare_flag;
8364 omp_workshare_flag = 1;
8365 /* FALLTHROUGH */
8366 default:
8367 gfc_resolve_blocks (code->block, ns);
8368 break;
8371 if (omp_workshare_save != -1)
8372 omp_workshare_flag = omp_workshare_save;
8375 t = SUCCESS;
8376 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
8377 t = gfc_resolve_expr (code->expr1);
8378 forall_flag = forall_save;
8380 if (gfc_resolve_expr (code->expr2) == FAILURE)
8381 t = FAILURE;
8383 if (code->op == EXEC_ALLOCATE
8384 && gfc_resolve_expr (code->expr3) == FAILURE)
8385 t = FAILURE;
8387 switch (code->op)
8389 case EXEC_NOP:
8390 case EXEC_END_BLOCK:
8391 case EXEC_CYCLE:
8392 case EXEC_PAUSE:
8393 case EXEC_STOP:
8394 case EXEC_ERROR_STOP:
8395 case EXEC_EXIT:
8396 case EXEC_CONTINUE:
8397 case EXEC_DT_END:
8398 case EXEC_ASSIGN_CALL:
8399 case EXEC_CRITICAL:
8400 break;
8402 case EXEC_SYNC_ALL:
8403 case EXEC_SYNC_IMAGES:
8404 case EXEC_SYNC_MEMORY:
8405 resolve_sync (code);
8406 break;
8408 case EXEC_ENTRY:
8409 /* Keep track of which entry we are up to. */
8410 current_entry_id = code->ext.entry->id;
8411 break;
8413 case EXEC_WHERE:
8414 resolve_where (code, NULL);
8415 break;
8417 case EXEC_GOTO:
8418 if (code->expr1 != NULL)
8420 if (code->expr1->ts.type != BT_INTEGER)
8421 gfc_error ("ASSIGNED GOTO statement at %L requires an "
8422 "INTEGER variable", &code->expr1->where);
8423 else if (code->expr1->symtree->n.sym->attr.assign != 1)
8424 gfc_error ("Variable '%s' has not been assigned a target "
8425 "label at %L", code->expr1->symtree->n.sym->name,
8426 &code->expr1->where);
8428 else
8429 resolve_branch (code->label1, code);
8430 break;
8432 case EXEC_RETURN:
8433 if (code->expr1 != NULL
8434 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
8435 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
8436 "INTEGER return specifier", &code->expr1->where);
8437 break;
8439 case EXEC_INIT_ASSIGN:
8440 case EXEC_END_PROCEDURE:
8441 break;
8443 case EXEC_ASSIGN:
8444 if (t == FAILURE)
8445 break;
8447 if (resolve_ordinary_assign (code, ns))
8449 if (code->op == EXEC_COMPCALL)
8450 goto compcall;
8451 else
8452 goto call;
8454 break;
8456 case EXEC_LABEL_ASSIGN:
8457 if (code->label1->defined == ST_LABEL_UNKNOWN)
8458 gfc_error ("Label %d referenced at %L is never defined",
8459 code->label1->value, &code->label1->where);
8460 if (t == SUCCESS
8461 && (code->expr1->expr_type != EXPR_VARIABLE
8462 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
8463 || code->expr1->symtree->n.sym->ts.kind
8464 != gfc_default_integer_kind
8465 || code->expr1->symtree->n.sym->as != NULL))
8466 gfc_error ("ASSIGN statement at %L requires a scalar "
8467 "default INTEGER variable", &code->expr1->where);
8468 break;
8470 case EXEC_POINTER_ASSIGN:
8471 if (t == FAILURE)
8472 break;
8474 gfc_check_pointer_assign (code->expr1, code->expr2);
8475 break;
8477 case EXEC_ARITHMETIC_IF:
8478 if (t == SUCCESS
8479 && code->expr1->ts.type != BT_INTEGER
8480 && code->expr1->ts.type != BT_REAL)
8481 gfc_error ("Arithmetic IF statement at %L requires a numeric "
8482 "expression", &code->expr1->where);
8484 resolve_branch (code->label1, code);
8485 resolve_branch (code->label2, code);
8486 resolve_branch (code->label3, code);
8487 break;
8489 case EXEC_IF:
8490 if (t == SUCCESS && code->expr1 != NULL
8491 && (code->expr1->ts.type != BT_LOGICAL
8492 || code->expr1->rank != 0))
8493 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8494 &code->expr1->where);
8495 break;
8497 case EXEC_CALL:
8498 call:
8499 resolve_call (code);
8500 break;
8502 case EXEC_COMPCALL:
8503 compcall:
8504 resolve_typebound_subroutine (code);
8505 break;
8507 case EXEC_CALL_PPC:
8508 resolve_ppc_call (code);
8509 break;
8511 case EXEC_SELECT:
8512 /* Select is complicated. Also, a SELECT construct could be
8513 a transformed computed GOTO. */
8514 resolve_select (code);
8515 break;
8517 case EXEC_SELECT_TYPE:
8518 resolve_select_type (code);
8519 break;
8521 case EXEC_BLOCK:
8522 gfc_resolve (code->ext.ns);
8523 break;
8525 case EXEC_DO:
8526 if (code->ext.iterator != NULL)
8528 gfc_iterator *iter = code->ext.iterator;
8529 if (gfc_resolve_iterator (iter, true) != FAILURE)
8530 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
8532 break;
8534 case EXEC_DO_WHILE:
8535 if (code->expr1 == NULL)
8536 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
8537 if (t == SUCCESS
8538 && (code->expr1->rank != 0
8539 || code->expr1->ts.type != BT_LOGICAL))
8540 gfc_error ("Exit condition of DO WHILE loop at %L must be "
8541 "a scalar LOGICAL expression", &code->expr1->where);
8542 break;
8544 case EXEC_ALLOCATE:
8545 if (t == SUCCESS)
8546 resolve_allocate_deallocate (code, "ALLOCATE");
8548 break;
8550 case EXEC_DEALLOCATE:
8551 if (t == SUCCESS)
8552 resolve_allocate_deallocate (code, "DEALLOCATE");
8554 break;
8556 case EXEC_OPEN:
8557 if (gfc_resolve_open (code->ext.open) == FAILURE)
8558 break;
8560 resolve_branch (code->ext.open->err, code);
8561 break;
8563 case EXEC_CLOSE:
8564 if (gfc_resolve_close (code->ext.close) == FAILURE)
8565 break;
8567 resolve_branch (code->ext.close->err, code);
8568 break;
8570 case EXEC_BACKSPACE:
8571 case EXEC_ENDFILE:
8572 case EXEC_REWIND:
8573 case EXEC_FLUSH:
8574 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
8575 break;
8577 resolve_branch (code->ext.filepos->err, code);
8578 break;
8580 case EXEC_INQUIRE:
8581 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8582 break;
8584 resolve_branch (code->ext.inquire->err, code);
8585 break;
8587 case EXEC_IOLENGTH:
8588 gcc_assert (code->ext.inquire != NULL);
8589 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8590 break;
8592 resolve_branch (code->ext.inquire->err, code);
8593 break;
8595 case EXEC_WAIT:
8596 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
8597 break;
8599 resolve_branch (code->ext.wait->err, code);
8600 resolve_branch (code->ext.wait->end, code);
8601 resolve_branch (code->ext.wait->eor, code);
8602 break;
8604 case EXEC_READ:
8605 case EXEC_WRITE:
8606 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
8607 break;
8609 resolve_branch (code->ext.dt->err, code);
8610 resolve_branch (code->ext.dt->end, code);
8611 resolve_branch (code->ext.dt->eor, code);
8612 break;
8614 case EXEC_TRANSFER:
8615 resolve_transfer (code);
8616 break;
8618 case EXEC_FORALL:
8619 resolve_forall_iterators (code->ext.forall_iterator);
8621 if (code->expr1 != NULL && code->expr1->ts.type != BT_LOGICAL)
8622 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
8623 "expression", &code->expr1->where);
8624 break;
8626 case EXEC_OMP_ATOMIC:
8627 case EXEC_OMP_BARRIER:
8628 case EXEC_OMP_CRITICAL:
8629 case EXEC_OMP_FLUSH:
8630 case EXEC_OMP_DO:
8631 case EXEC_OMP_MASTER:
8632 case EXEC_OMP_ORDERED:
8633 case EXEC_OMP_SECTIONS:
8634 case EXEC_OMP_SINGLE:
8635 case EXEC_OMP_TASKWAIT:
8636 case EXEC_OMP_WORKSHARE:
8637 gfc_resolve_omp_directive (code, ns);
8638 break;
8640 case EXEC_OMP_PARALLEL:
8641 case EXEC_OMP_PARALLEL_DO:
8642 case EXEC_OMP_PARALLEL_SECTIONS:
8643 case EXEC_OMP_PARALLEL_WORKSHARE:
8644 case EXEC_OMP_TASK:
8645 omp_workshare_save = omp_workshare_flag;
8646 omp_workshare_flag = 0;
8647 gfc_resolve_omp_directive (code, ns);
8648 omp_workshare_flag = omp_workshare_save;
8649 break;
8651 default:
8652 gfc_internal_error ("resolve_code(): Bad statement code");
8656 cs_base = frame.prev;
8660 /* Resolve initial values and make sure they are compatible with
8661 the variable. */
8663 static void
8664 resolve_values (gfc_symbol *sym)
8666 if (sym->value == NULL)
8667 return;
8669 if (gfc_resolve_expr (sym->value) == FAILURE)
8670 return;
8672 gfc_check_assign_symbol (sym, sym->value);
8676 /* Verify the binding labels for common blocks that are BIND(C). The label
8677 for a BIND(C) common block must be identical in all scoping units in which
8678 the common block is declared. Further, the binding label can not collide
8679 with any other global entity in the program. */
8681 static void
8682 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
8684 if (comm_block_tree->n.common->is_bind_c == 1)
8686 gfc_gsymbol *binding_label_gsym;
8687 gfc_gsymbol *comm_name_gsym;
8689 /* See if a global symbol exists by the common block's name. It may
8690 be NULL if the common block is use-associated. */
8691 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
8692 comm_block_tree->n.common->name);
8693 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
8694 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
8695 "with the global entity '%s' at %L",
8696 comm_block_tree->n.common->binding_label,
8697 comm_block_tree->n.common->name,
8698 &(comm_block_tree->n.common->where),
8699 comm_name_gsym->name, &(comm_name_gsym->where));
8700 else if (comm_name_gsym != NULL
8701 && strcmp (comm_name_gsym->name,
8702 comm_block_tree->n.common->name) == 0)
8704 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
8705 as expected. */
8706 if (comm_name_gsym->binding_label == NULL)
8707 /* No binding label for common block stored yet; save this one. */
8708 comm_name_gsym->binding_label =
8709 comm_block_tree->n.common->binding_label;
8710 else
8711 if (strcmp (comm_name_gsym->binding_label,
8712 comm_block_tree->n.common->binding_label) != 0)
8714 /* Common block names match but binding labels do not. */
8715 gfc_error ("Binding label '%s' for common block '%s' at %L "
8716 "does not match the binding label '%s' for common "
8717 "block '%s' at %L",
8718 comm_block_tree->n.common->binding_label,
8719 comm_block_tree->n.common->name,
8720 &(comm_block_tree->n.common->where),
8721 comm_name_gsym->binding_label,
8722 comm_name_gsym->name,
8723 &(comm_name_gsym->where));
8724 return;
8728 /* There is no binding label (NAME="") so we have nothing further to
8729 check and nothing to add as a global symbol for the label. */
8730 if (comm_block_tree->n.common->binding_label[0] == '\0' )
8731 return;
8733 binding_label_gsym =
8734 gfc_find_gsymbol (gfc_gsym_root,
8735 comm_block_tree->n.common->binding_label);
8736 if (binding_label_gsym == NULL)
8738 /* Need to make a global symbol for the binding label to prevent
8739 it from colliding with another. */
8740 binding_label_gsym =
8741 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
8742 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
8743 binding_label_gsym->type = GSYM_COMMON;
8745 else
8747 /* If comm_name_gsym is NULL, the name common block is use
8748 associated and the name could be colliding. */
8749 if (binding_label_gsym->type != GSYM_COMMON)
8750 gfc_error ("Binding label '%s' for common block '%s' at %L "
8751 "collides with the global entity '%s' at %L",
8752 comm_block_tree->n.common->binding_label,
8753 comm_block_tree->n.common->name,
8754 &(comm_block_tree->n.common->where),
8755 binding_label_gsym->name,
8756 &(binding_label_gsym->where));
8757 else if (comm_name_gsym != NULL
8758 && (strcmp (binding_label_gsym->name,
8759 comm_name_gsym->binding_label) != 0)
8760 && (strcmp (binding_label_gsym->sym_name,
8761 comm_name_gsym->name) != 0))
8762 gfc_error ("Binding label '%s' for common block '%s' at %L "
8763 "collides with global entity '%s' at %L",
8764 binding_label_gsym->name, binding_label_gsym->sym_name,
8765 &(comm_block_tree->n.common->where),
8766 comm_name_gsym->name, &(comm_name_gsym->where));
8770 return;
8774 /* Verify any BIND(C) derived types in the namespace so we can report errors
8775 for them once, rather than for each variable declared of that type. */
8777 static void
8778 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
8780 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
8781 && derived_sym->attr.is_bind_c == 1)
8782 verify_bind_c_derived_type (derived_sym);
8784 return;
8788 /* Verify that any binding labels used in a given namespace do not collide
8789 with the names or binding labels of any global symbols. */
8791 static void
8792 gfc_verify_binding_labels (gfc_symbol *sym)
8794 int has_error = 0;
8796 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
8797 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
8799 gfc_gsymbol *bind_c_sym;
8801 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
8802 if (bind_c_sym != NULL
8803 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
8805 if (sym->attr.if_source == IFSRC_DECL
8806 && (bind_c_sym->type != GSYM_SUBROUTINE
8807 && bind_c_sym->type != GSYM_FUNCTION)
8808 && ((sym->attr.contained == 1
8809 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
8810 || (sym->attr.use_assoc == 1
8811 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
8813 /* Make sure global procedures don't collide with anything. */
8814 gfc_error ("Binding label '%s' at %L collides with the global "
8815 "entity '%s' at %L", sym->binding_label,
8816 &(sym->declared_at), bind_c_sym->name,
8817 &(bind_c_sym->where));
8818 has_error = 1;
8820 else if (sym->attr.contained == 0
8821 && (sym->attr.if_source == IFSRC_IFBODY
8822 && sym->attr.flavor == FL_PROCEDURE)
8823 && (bind_c_sym->sym_name != NULL
8824 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
8826 /* Make sure procedures in interface bodies don't collide. */
8827 gfc_error ("Binding label '%s' in interface body at %L collides "
8828 "with the global entity '%s' at %L",
8829 sym->binding_label,
8830 &(sym->declared_at), bind_c_sym->name,
8831 &(bind_c_sym->where));
8832 has_error = 1;
8834 else if (sym->attr.contained == 0
8835 && sym->attr.if_source == IFSRC_UNKNOWN)
8836 if ((sym->attr.use_assoc && bind_c_sym->mod_name
8837 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
8838 || sym->attr.use_assoc == 0)
8840 gfc_error ("Binding label '%s' at %L collides with global "
8841 "entity '%s' at %L", sym->binding_label,
8842 &(sym->declared_at), bind_c_sym->name,
8843 &(bind_c_sym->where));
8844 has_error = 1;
8847 if (has_error != 0)
8848 /* Clear the binding label to prevent checking multiple times. */
8849 sym->binding_label[0] = '\0';
8851 else if (bind_c_sym == NULL)
8853 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
8854 bind_c_sym->where = sym->declared_at;
8855 bind_c_sym->sym_name = sym->name;
8857 if (sym->attr.use_assoc == 1)
8858 bind_c_sym->mod_name = sym->module;
8859 else
8860 if (sym->ns->proc_name != NULL)
8861 bind_c_sym->mod_name = sym->ns->proc_name->name;
8863 if (sym->attr.contained == 0)
8865 if (sym->attr.subroutine)
8866 bind_c_sym->type = GSYM_SUBROUTINE;
8867 else if (sym->attr.function)
8868 bind_c_sym->type = GSYM_FUNCTION;
8872 return;
8876 /* Resolve an index expression. */
8878 static gfc_try
8879 resolve_index_expr (gfc_expr *e)
8881 if (gfc_resolve_expr (e) == FAILURE)
8882 return FAILURE;
8884 if (gfc_simplify_expr (e, 0) == FAILURE)
8885 return FAILURE;
8887 if (gfc_specification_expr (e) == FAILURE)
8888 return FAILURE;
8890 return SUCCESS;
8893 /* Resolve a charlen structure. */
8895 static gfc_try
8896 resolve_charlen (gfc_charlen *cl)
8898 int i, k;
8900 if (cl->resolved)
8901 return SUCCESS;
8903 cl->resolved = 1;
8905 specification_expr = 1;
8907 if (resolve_index_expr (cl->length) == FAILURE)
8909 specification_expr = 0;
8910 return FAILURE;
8913 /* "If the character length parameter value evaluates to a negative
8914 value, the length of character entities declared is zero." */
8915 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
8917 if (gfc_option.warn_surprising)
8918 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
8919 " the length has been set to zero",
8920 &cl->length->where, i);
8921 gfc_replace_expr (cl->length,
8922 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
8925 /* Check that the character length is not too large. */
8926 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
8927 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
8928 && cl->length->ts.type == BT_INTEGER
8929 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
8931 gfc_error ("String length at %L is too large", &cl->length->where);
8932 return FAILURE;
8935 return SUCCESS;
8939 /* Test for non-constant shape arrays. */
8941 static bool
8942 is_non_constant_shape_array (gfc_symbol *sym)
8944 gfc_expr *e;
8945 int i;
8946 bool not_constant;
8948 not_constant = false;
8949 if (sym->as != NULL)
8951 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
8952 has not been simplified; parameter array references. Do the
8953 simplification now. */
8954 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
8956 e = sym->as->lower[i];
8957 if (e && (resolve_index_expr (e) == FAILURE
8958 || !gfc_is_constant_expr (e)))
8959 not_constant = true;
8960 e = sym->as->upper[i];
8961 if (e && (resolve_index_expr (e) == FAILURE
8962 || !gfc_is_constant_expr (e)))
8963 not_constant = true;
8966 return not_constant;
8969 /* Given a symbol and an initialization expression, add code to initialize
8970 the symbol to the function entry. */
8971 static void
8972 build_init_assign (gfc_symbol *sym, gfc_expr *init)
8974 gfc_expr *lval;
8975 gfc_code *init_st;
8976 gfc_namespace *ns = sym->ns;
8978 /* Search for the function namespace if this is a contained
8979 function without an explicit result. */
8980 if (sym->attr.function && sym == sym->result
8981 && sym->name != sym->ns->proc_name->name)
8983 ns = ns->contained;
8984 for (;ns; ns = ns->sibling)
8985 if (strcmp (ns->proc_name->name, sym->name) == 0)
8986 break;
8989 if (ns == NULL)
8991 gfc_free_expr (init);
8992 return;
8995 /* Build an l-value expression for the result. */
8996 lval = gfc_lval_expr_from_sym (sym);
8998 /* Add the code at scope entry. */
8999 init_st = gfc_get_code ();
9000 init_st->next = ns->code;
9001 ns->code = init_st;
9003 /* Assign the default initializer to the l-value. */
9004 init_st->loc = sym->declared_at;
9005 init_st->op = EXEC_INIT_ASSIGN;
9006 init_st->expr1 = lval;
9007 init_st->expr2 = init;
9010 /* Assign the default initializer to a derived type variable or result. */
9012 static void
9013 apply_default_init (gfc_symbol *sym)
9015 gfc_expr *init = NULL;
9017 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9018 return;
9020 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
9021 init = gfc_default_initializer (&sym->ts);
9023 if (init == NULL)
9024 return;
9026 build_init_assign (sym, init);
9029 /* Build an initializer for a local integer, real, complex, logical, or
9030 character variable, based on the command line flags finit-local-zero,
9031 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
9032 null if the symbol should not have a default initialization. */
9033 static gfc_expr *
9034 build_default_init_expr (gfc_symbol *sym)
9036 int char_len;
9037 gfc_expr *init_expr;
9038 int i;
9040 /* These symbols should never have a default initialization. */
9041 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
9042 || sym->attr.external
9043 || sym->attr.dummy
9044 || sym->attr.pointer
9045 || sym->attr.in_equivalence
9046 || sym->attr.in_common
9047 || sym->attr.data
9048 || sym->module
9049 || sym->attr.cray_pointee
9050 || sym->attr.cray_pointer)
9051 return NULL;
9053 /* Now we'll try to build an initializer expression. */
9054 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
9055 &sym->declared_at);
9057 /* We will only initialize integers, reals, complex, logicals, and
9058 characters, and only if the corresponding command-line flags
9059 were set. Otherwise, we free init_expr and return null. */
9060 switch (sym->ts.type)
9062 case BT_INTEGER:
9063 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
9064 mpz_init_set_si (init_expr->value.integer,
9065 gfc_option.flag_init_integer_value);
9066 else
9068 gfc_free_expr (init_expr);
9069 init_expr = NULL;
9071 break;
9073 case BT_REAL:
9074 mpfr_init (init_expr->value.real);
9075 switch (gfc_option.flag_init_real)
9077 case GFC_INIT_REAL_SNAN:
9078 init_expr->is_snan = 1;
9079 /* Fall through. */
9080 case GFC_INIT_REAL_NAN:
9081 mpfr_set_nan (init_expr->value.real);
9082 break;
9084 case GFC_INIT_REAL_INF:
9085 mpfr_set_inf (init_expr->value.real, 1);
9086 break;
9088 case GFC_INIT_REAL_NEG_INF:
9089 mpfr_set_inf (init_expr->value.real, -1);
9090 break;
9092 case GFC_INIT_REAL_ZERO:
9093 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
9094 break;
9096 default:
9097 gfc_free_expr (init_expr);
9098 init_expr = NULL;
9099 break;
9101 break;
9103 case BT_COMPLEX:
9104 mpc_init2 (init_expr->value.complex, mpfr_get_default_prec());
9105 switch (gfc_option.flag_init_real)
9107 case GFC_INIT_REAL_SNAN:
9108 init_expr->is_snan = 1;
9109 /* Fall through. */
9110 case GFC_INIT_REAL_NAN:
9111 mpfr_set_nan (mpc_realref (init_expr->value.complex));
9112 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
9113 break;
9115 case GFC_INIT_REAL_INF:
9116 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
9117 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
9118 break;
9120 case GFC_INIT_REAL_NEG_INF:
9121 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
9122 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
9123 break;
9125 case GFC_INIT_REAL_ZERO:
9126 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
9127 break;
9129 default:
9130 gfc_free_expr (init_expr);
9131 init_expr = NULL;
9132 break;
9134 break;
9136 case BT_LOGICAL:
9137 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
9138 init_expr->value.logical = 0;
9139 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
9140 init_expr->value.logical = 1;
9141 else
9143 gfc_free_expr (init_expr);
9144 init_expr = NULL;
9146 break;
9148 case BT_CHARACTER:
9149 /* For characters, the length must be constant in order to
9150 create a default initializer. */
9151 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
9152 && sym->ts.u.cl->length
9153 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9155 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
9156 init_expr->value.character.length = char_len;
9157 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
9158 for (i = 0; i < char_len; i++)
9159 init_expr->value.character.string[i]
9160 = (unsigned char) gfc_option.flag_init_character_value;
9162 else
9164 gfc_free_expr (init_expr);
9165 init_expr = NULL;
9167 break;
9169 default:
9170 gfc_free_expr (init_expr);
9171 init_expr = NULL;
9173 return init_expr;
9176 /* Add an initialization expression to a local variable. */
9177 static void
9178 apply_default_init_local (gfc_symbol *sym)
9180 gfc_expr *init = NULL;
9182 /* The symbol should be a variable or a function return value. */
9183 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9184 || (sym->attr.function && sym->result != sym))
9185 return;
9187 /* Try to build the initializer expression. If we can't initialize
9188 this symbol, then init will be NULL. */
9189 init = build_default_init_expr (sym);
9190 if (init == NULL)
9191 return;
9193 /* For saved variables, we don't want to add an initializer at
9194 function entry, so we just add a static initializer. */
9195 if (sym->attr.save || sym->ns->save_all
9196 || gfc_option.flag_max_stack_var_size == 0)
9198 /* Don't clobber an existing initializer! */
9199 gcc_assert (sym->value == NULL);
9200 sym->value = init;
9201 return;
9204 build_init_assign (sym, init);
9207 /* Resolution of common features of flavors variable and procedure. */
9209 static gfc_try
9210 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
9212 /* Constraints on deferred shape variable. */
9213 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
9215 if (sym->attr.allocatable)
9217 if (sym->attr.dimension)
9219 gfc_error ("Allocatable array '%s' at %L must have "
9220 "a deferred shape", sym->name, &sym->declared_at);
9221 return FAILURE;
9223 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
9224 "may not be ALLOCATABLE", sym->name,
9225 &sym->declared_at) == FAILURE)
9226 return FAILURE;
9229 if (sym->attr.pointer && sym->attr.dimension)
9231 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
9232 sym->name, &sym->declared_at);
9233 return FAILURE;
9237 else
9239 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
9240 && !sym->attr.dummy && sym->ts.type != BT_CLASS)
9242 gfc_error ("Array '%s' at %L cannot have a deferred shape",
9243 sym->name, &sym->declared_at);
9244 return FAILURE;
9247 return SUCCESS;
9251 /* Additional checks for symbols with flavor variable and derived
9252 type. To be called from resolve_fl_variable. */
9254 static gfc_try
9255 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
9257 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
9259 /* Check to see if a derived type is blocked from being host
9260 associated by the presence of another class I symbol in the same
9261 namespace. 14.6.1.3 of the standard and the discussion on
9262 comp.lang.fortran. */
9263 if (sym->ns != sym->ts.u.derived->ns
9264 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
9266 gfc_symbol *s;
9267 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
9268 if (s && s->attr.flavor != FL_DERIVED)
9270 gfc_error ("The type '%s' cannot be host associated at %L "
9271 "because it is blocked by an incompatible object "
9272 "of the same name declared at %L",
9273 sym->ts.u.derived->name, &sym->declared_at,
9274 &s->declared_at);
9275 return FAILURE;
9279 /* 4th constraint in section 11.3: "If an object of a type for which
9280 component-initialization is specified (R429) appears in the
9281 specification-part of a module and does not have the ALLOCATABLE
9282 or POINTER attribute, the object shall have the SAVE attribute."
9284 The check for initializers is performed with
9285 has_default_initializer because gfc_default_initializer generates
9286 a hidden default for allocatable components. */
9287 if (!(sym->value || no_init_flag) && sym->ns->proc_name
9288 && sym->ns->proc_name->attr.flavor == FL_MODULE
9289 && !sym->ns->save_all && !sym->attr.save
9290 && !sym->attr.pointer && !sym->attr.allocatable
9291 && has_default_initializer (sym->ts.u.derived)
9292 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Implied SAVE for "
9293 "module variable '%s' at %L, needed due to "
9294 "the default initialization", sym->name,
9295 &sym->declared_at) == FAILURE)
9296 return FAILURE;
9298 if (sym->ts.type == BT_CLASS)
9300 /* C502. */
9301 if (!gfc_type_is_extensible (sym->ts.u.derived->components->ts.u.derived))
9303 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
9304 sym->ts.u.derived->components->ts.u.derived->name,
9305 sym->name, &sym->declared_at);
9306 return FAILURE;
9309 /* C509. */
9310 /* Assume that use associated symbols were checked in the module ns. */
9311 if (!sym->attr.class_ok && !sym->attr.use_assoc)
9313 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
9314 "or pointer", sym->name, &sym->declared_at);
9315 return FAILURE;
9319 /* Assign default initializer. */
9320 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
9321 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
9323 sym->value = gfc_default_initializer (&sym->ts);
9326 return SUCCESS;
9330 /* Resolve symbols with flavor variable. */
9332 static gfc_try
9333 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
9335 int no_init_flag, automatic_flag;
9336 gfc_expr *e;
9337 const char *auto_save_msg;
9339 auto_save_msg = "Automatic object '%s' at %L cannot have the "
9340 "SAVE attribute";
9342 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9343 return FAILURE;
9345 /* Set this flag to check that variables are parameters of all entries.
9346 This check is effected by the call to gfc_resolve_expr through
9347 is_non_constant_shape_array. */
9348 specification_expr = 1;
9350 if (sym->ns->proc_name
9351 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9352 || sym->ns->proc_name->attr.is_main_program)
9353 && !sym->attr.use_assoc
9354 && !sym->attr.allocatable
9355 && !sym->attr.pointer
9356 && is_non_constant_shape_array (sym))
9358 /* The shape of a main program or module array needs to be
9359 constant. */
9360 gfc_error ("The module or main program array '%s' at %L must "
9361 "have constant shape", sym->name, &sym->declared_at);
9362 specification_expr = 0;
9363 return FAILURE;
9366 if (sym->ts.type == BT_CHARACTER)
9368 /* Make sure that character string variables with assumed length are
9369 dummy arguments. */
9370 e = sym->ts.u.cl->length;
9371 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
9373 gfc_error ("Entity with assumed character length at %L must be a "
9374 "dummy argument or a PARAMETER", &sym->declared_at);
9375 return FAILURE;
9378 if (e && sym->attr.save && !gfc_is_constant_expr (e))
9380 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9381 return FAILURE;
9384 if (!gfc_is_constant_expr (e)
9385 && !(e->expr_type == EXPR_VARIABLE
9386 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
9387 && sym->ns->proc_name
9388 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9389 || sym->ns->proc_name->attr.is_main_program)
9390 && !sym->attr.use_assoc)
9392 gfc_error ("'%s' at %L must have constant character length "
9393 "in this context", sym->name, &sym->declared_at);
9394 return FAILURE;
9398 if (sym->value == NULL && sym->attr.referenced)
9399 apply_default_init_local (sym); /* Try to apply a default initialization. */
9401 /* Determine if the symbol may not have an initializer. */
9402 no_init_flag = automatic_flag = 0;
9403 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
9404 || sym->attr.intrinsic || sym->attr.result)
9405 no_init_flag = 1;
9406 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
9407 && is_non_constant_shape_array (sym))
9409 no_init_flag = automatic_flag = 1;
9411 /* Also, they must not have the SAVE attribute.
9412 SAVE_IMPLICIT is checked below. */
9413 if (sym->attr.save == SAVE_EXPLICIT)
9415 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9416 return FAILURE;
9420 /* Ensure that any initializer is simplified. */
9421 if (sym->value)
9422 gfc_simplify_expr (sym->value, 1);
9424 /* Reject illegal initializers. */
9425 if (!sym->mark && sym->value)
9427 if (sym->attr.allocatable)
9428 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
9429 sym->name, &sym->declared_at);
9430 else if (sym->attr.external)
9431 gfc_error ("External '%s' at %L cannot have an initializer",
9432 sym->name, &sym->declared_at);
9433 else if (sym->attr.dummy
9434 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
9435 gfc_error ("Dummy '%s' at %L cannot have an initializer",
9436 sym->name, &sym->declared_at);
9437 else if (sym->attr.intrinsic)
9438 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
9439 sym->name, &sym->declared_at);
9440 else if (sym->attr.result)
9441 gfc_error ("Function result '%s' at %L cannot have an initializer",
9442 sym->name, &sym->declared_at);
9443 else if (automatic_flag)
9444 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
9445 sym->name, &sym->declared_at);
9446 else
9447 goto no_init_error;
9448 return FAILURE;
9451 no_init_error:
9452 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
9453 return resolve_fl_variable_derived (sym, no_init_flag);
9455 return SUCCESS;
9459 /* Resolve a procedure. */
9461 static gfc_try
9462 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
9464 gfc_formal_arglist *arg;
9466 if (sym->attr.function
9467 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9468 return FAILURE;
9470 if (sym->ts.type == BT_CHARACTER)
9472 gfc_charlen *cl = sym->ts.u.cl;
9474 if (cl && cl->length && gfc_is_constant_expr (cl->length)
9475 && resolve_charlen (cl) == FAILURE)
9476 return FAILURE;
9478 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
9479 && sym->attr.proc == PROC_ST_FUNCTION)
9481 gfc_error ("Character-valued statement function '%s' at %L must "
9482 "have constant length", sym->name, &sym->declared_at);
9483 return FAILURE;
9487 /* Ensure that derived type for are not of a private type. Internal
9488 module procedures are excluded by 2.2.3.3 - i.e., they are not
9489 externally accessible and can access all the objects accessible in
9490 the host. */
9491 if (!(sym->ns->parent
9492 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
9493 && gfc_check_access(sym->attr.access, sym->ns->default_access))
9495 gfc_interface *iface;
9497 for (arg = sym->formal; arg; arg = arg->next)
9499 if (arg->sym
9500 && arg->sym->ts.type == BT_DERIVED
9501 && !arg->sym->ts.u.derived->attr.use_assoc
9502 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9503 arg->sym->ts.u.derived->ns->default_access)
9504 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
9505 "PRIVATE type and cannot be a dummy argument"
9506 " of '%s', which is PUBLIC at %L",
9507 arg->sym->name, sym->name, &sym->declared_at)
9508 == FAILURE)
9510 /* Stop this message from recurring. */
9511 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9512 return FAILURE;
9516 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9517 PRIVATE to the containing module. */
9518 for (iface = sym->generic; iface; iface = iface->next)
9520 for (arg = iface->sym->formal; arg; arg = arg->next)
9522 if (arg->sym
9523 && arg->sym->ts.type == BT_DERIVED
9524 && !arg->sym->ts.u.derived->attr.use_assoc
9525 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9526 arg->sym->ts.u.derived->ns->default_access)
9527 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9528 "'%s' in PUBLIC interface '%s' at %L "
9529 "takes dummy arguments of '%s' which is "
9530 "PRIVATE", iface->sym->name, sym->name,
9531 &iface->sym->declared_at,
9532 gfc_typename (&arg->sym->ts)) == FAILURE)
9534 /* Stop this message from recurring. */
9535 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9536 return FAILURE;
9541 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9542 PRIVATE to the containing module. */
9543 for (iface = sym->generic; iface; iface = iface->next)
9545 for (arg = iface->sym->formal; arg; arg = arg->next)
9547 if (arg->sym
9548 && arg->sym->ts.type == BT_DERIVED
9549 && !arg->sym->ts.u.derived->attr.use_assoc
9550 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9551 arg->sym->ts.u.derived->ns->default_access)
9552 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9553 "'%s' in PUBLIC interface '%s' at %L "
9554 "takes dummy arguments of '%s' which is "
9555 "PRIVATE", iface->sym->name, sym->name,
9556 &iface->sym->declared_at,
9557 gfc_typename (&arg->sym->ts)) == FAILURE)
9559 /* Stop this message from recurring. */
9560 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9561 return FAILURE;
9567 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
9568 && !sym->attr.proc_pointer)
9570 gfc_error ("Function '%s' at %L cannot have an initializer",
9571 sym->name, &sym->declared_at);
9572 return FAILURE;
9575 /* An external symbol may not have an initializer because it is taken to be
9576 a procedure. Exception: Procedure Pointers. */
9577 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
9579 gfc_error ("External object '%s' at %L may not have an initializer",
9580 sym->name, &sym->declared_at);
9581 return FAILURE;
9584 /* An elemental function is required to return a scalar 12.7.1 */
9585 if (sym->attr.elemental && sym->attr.function && sym->as)
9587 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
9588 "result", sym->name, &sym->declared_at);
9589 /* Reset so that the error only occurs once. */
9590 sym->attr.elemental = 0;
9591 return FAILURE;
9594 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
9595 char-len-param shall not be array-valued, pointer-valued, recursive
9596 or pure. ....snip... A character value of * may only be used in the
9597 following ways: (i) Dummy arg of procedure - dummy associates with
9598 actual length; (ii) To declare a named constant; or (iii) External
9599 function - but length must be declared in calling scoping unit. */
9600 if (sym->attr.function
9601 && sym->ts.type == BT_CHARACTER
9602 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
9604 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
9605 || (sym->attr.recursive) || (sym->attr.pure))
9607 if (sym->as && sym->as->rank)
9608 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9609 "array-valued", sym->name, &sym->declared_at);
9611 if (sym->attr.pointer)
9612 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9613 "pointer-valued", sym->name, &sym->declared_at);
9615 if (sym->attr.pure)
9616 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9617 "pure", sym->name, &sym->declared_at);
9619 if (sym->attr.recursive)
9620 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9621 "recursive", sym->name, &sym->declared_at);
9623 return FAILURE;
9626 /* Appendix B.2 of the standard. Contained functions give an
9627 error anyway. Fixed-form is likely to be F77/legacy. */
9628 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
9629 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
9630 "CHARACTER(*) function '%s' at %L",
9631 sym->name, &sym->declared_at);
9634 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
9636 gfc_formal_arglist *curr_arg;
9637 int has_non_interop_arg = 0;
9639 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
9640 sym->common_block) == FAILURE)
9642 /* Clear these to prevent looking at them again if there was an
9643 error. */
9644 sym->attr.is_bind_c = 0;
9645 sym->attr.is_c_interop = 0;
9646 sym->ts.is_c_interop = 0;
9648 else
9650 /* So far, no errors have been found. */
9651 sym->attr.is_c_interop = 1;
9652 sym->ts.is_c_interop = 1;
9655 curr_arg = sym->formal;
9656 while (curr_arg != NULL)
9658 /* Skip implicitly typed dummy args here. */
9659 if (curr_arg->sym->attr.implicit_type == 0)
9660 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
9661 /* If something is found to fail, record the fact so we
9662 can mark the symbol for the procedure as not being
9663 BIND(C) to try and prevent multiple errors being
9664 reported. */
9665 has_non_interop_arg = 1;
9667 curr_arg = curr_arg->next;
9670 /* See if any of the arguments were not interoperable and if so, clear
9671 the procedure symbol to prevent duplicate error messages. */
9672 if (has_non_interop_arg != 0)
9674 sym->attr.is_c_interop = 0;
9675 sym->ts.is_c_interop = 0;
9676 sym->attr.is_bind_c = 0;
9680 if (!sym->attr.proc_pointer)
9682 if (sym->attr.save == SAVE_EXPLICIT)
9684 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
9685 "in '%s' at %L", sym->name, &sym->declared_at);
9686 return FAILURE;
9688 if (sym->attr.intent)
9690 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
9691 "in '%s' at %L", sym->name, &sym->declared_at);
9692 return FAILURE;
9694 if (sym->attr.subroutine && sym->attr.result)
9696 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
9697 "in '%s' at %L", sym->name, &sym->declared_at);
9698 return FAILURE;
9700 if (sym->attr.external && sym->attr.function
9701 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
9702 || sym->attr.contained))
9704 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
9705 "in '%s' at %L", sym->name, &sym->declared_at);
9706 return FAILURE;
9708 if (strcmp ("ppr@", sym->name) == 0)
9710 gfc_error ("Procedure pointer result '%s' at %L "
9711 "is missing the pointer attribute",
9712 sym->ns->proc_name->name, &sym->declared_at);
9713 return FAILURE;
9717 return SUCCESS;
9721 /* Resolve a list of finalizer procedures. That is, after they have hopefully
9722 been defined and we now know their defined arguments, check that they fulfill
9723 the requirements of the standard for procedures used as finalizers. */
9725 static gfc_try
9726 gfc_resolve_finalizers (gfc_symbol* derived)
9728 gfc_finalizer* list;
9729 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
9730 gfc_try result = SUCCESS;
9731 bool seen_scalar = false;
9733 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
9734 return SUCCESS;
9736 /* Walk over the list of finalizer-procedures, check them, and if any one
9737 does not fit in with the standard's definition, print an error and remove
9738 it from the list. */
9739 prev_link = &derived->f2k_derived->finalizers;
9740 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
9742 gfc_symbol* arg;
9743 gfc_finalizer* i;
9744 int my_rank;
9746 /* Skip this finalizer if we already resolved it. */
9747 if (list->proc_tree)
9749 prev_link = &(list->next);
9750 continue;
9753 /* Check this exists and is a SUBROUTINE. */
9754 if (!list->proc_sym->attr.subroutine)
9756 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
9757 list->proc_sym->name, &list->where);
9758 goto error;
9761 /* We should have exactly one argument. */
9762 if (!list->proc_sym->formal || list->proc_sym->formal->next)
9764 gfc_error ("FINAL procedure at %L must have exactly one argument",
9765 &list->where);
9766 goto error;
9768 arg = list->proc_sym->formal->sym;
9770 /* This argument must be of our type. */
9771 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
9773 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
9774 &arg->declared_at, derived->name);
9775 goto error;
9778 /* It must neither be a pointer nor allocatable nor optional. */
9779 if (arg->attr.pointer)
9781 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
9782 &arg->declared_at);
9783 goto error;
9785 if (arg->attr.allocatable)
9787 gfc_error ("Argument of FINAL procedure at %L must not be"
9788 " ALLOCATABLE", &arg->declared_at);
9789 goto error;
9791 if (arg->attr.optional)
9793 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
9794 &arg->declared_at);
9795 goto error;
9798 /* It must not be INTENT(OUT). */
9799 if (arg->attr.intent == INTENT_OUT)
9801 gfc_error ("Argument of FINAL procedure at %L must not be"
9802 " INTENT(OUT)", &arg->declared_at);
9803 goto error;
9806 /* Warn if the procedure is non-scalar and not assumed shape. */
9807 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
9808 && arg->as->type != AS_ASSUMED_SHAPE)
9809 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
9810 " shape argument", &arg->declared_at);
9812 /* Check that it does not match in kind and rank with a FINAL procedure
9813 defined earlier. To really loop over the *earlier* declarations,
9814 we need to walk the tail of the list as new ones were pushed at the
9815 front. */
9816 /* TODO: Handle kind parameters once they are implemented. */
9817 my_rank = (arg->as ? arg->as->rank : 0);
9818 for (i = list->next; i; i = i->next)
9820 /* Argument list might be empty; that is an error signalled earlier,
9821 but we nevertheless continued resolving. */
9822 if (i->proc_sym->formal)
9824 gfc_symbol* i_arg = i->proc_sym->formal->sym;
9825 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
9826 if (i_rank == my_rank)
9828 gfc_error ("FINAL procedure '%s' declared at %L has the same"
9829 " rank (%d) as '%s'",
9830 list->proc_sym->name, &list->where, my_rank,
9831 i->proc_sym->name);
9832 goto error;
9837 /* Is this the/a scalar finalizer procedure? */
9838 if (!arg->as || arg->as->rank == 0)
9839 seen_scalar = true;
9841 /* Find the symtree for this procedure. */
9842 gcc_assert (!list->proc_tree);
9843 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
9845 prev_link = &list->next;
9846 continue;
9848 /* Remove wrong nodes immediately from the list so we don't risk any
9849 troubles in the future when they might fail later expectations. */
9850 error:
9851 result = FAILURE;
9852 i = list;
9853 *prev_link = list->next;
9854 gfc_free_finalizer (i);
9857 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
9858 were nodes in the list, must have been for arrays. It is surely a good
9859 idea to have a scalar version there if there's something to finalize. */
9860 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
9861 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
9862 " defined at %L, suggest also scalar one",
9863 derived->name, &derived->declared_at);
9865 /* TODO: Remove this error when finalization is finished. */
9866 gfc_error ("Finalization at %L is not yet implemented",
9867 &derived->declared_at);
9869 return result;
9873 /* Check that it is ok for the typebound procedure proc to override the
9874 procedure old. */
9876 static gfc_try
9877 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
9879 locus where;
9880 const gfc_symbol* proc_target;
9881 const gfc_symbol* old_target;
9882 unsigned proc_pass_arg, old_pass_arg, argpos;
9883 gfc_formal_arglist* proc_formal;
9884 gfc_formal_arglist* old_formal;
9886 /* This procedure should only be called for non-GENERIC proc. */
9887 gcc_assert (!proc->n.tb->is_generic);
9889 /* If the overwritten procedure is GENERIC, this is an error. */
9890 if (old->n.tb->is_generic)
9892 gfc_error ("Can't overwrite GENERIC '%s' at %L",
9893 old->name, &proc->n.tb->where);
9894 return FAILURE;
9897 where = proc->n.tb->where;
9898 proc_target = proc->n.tb->u.specific->n.sym;
9899 old_target = old->n.tb->u.specific->n.sym;
9901 /* Check that overridden binding is not NON_OVERRIDABLE. */
9902 if (old->n.tb->non_overridable)
9904 gfc_error ("'%s' at %L overrides a procedure binding declared"
9905 " NON_OVERRIDABLE", proc->name, &where);
9906 return FAILURE;
9909 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
9910 if (!old->n.tb->deferred && proc->n.tb->deferred)
9912 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
9913 " non-DEFERRED binding", proc->name, &where);
9914 return FAILURE;
9917 /* If the overridden binding is PURE, the overriding must be, too. */
9918 if (old_target->attr.pure && !proc_target->attr.pure)
9920 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
9921 proc->name, &where);
9922 return FAILURE;
9925 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
9926 is not, the overriding must not be either. */
9927 if (old_target->attr.elemental && !proc_target->attr.elemental)
9929 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
9930 " ELEMENTAL", proc->name, &where);
9931 return FAILURE;
9933 if (!old_target->attr.elemental && proc_target->attr.elemental)
9935 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
9936 " be ELEMENTAL, either", proc->name, &where);
9937 return FAILURE;
9940 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
9941 SUBROUTINE. */
9942 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
9944 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
9945 " SUBROUTINE", proc->name, &where);
9946 return FAILURE;
9949 /* If the overridden binding is a FUNCTION, the overriding must also be a
9950 FUNCTION and have the same characteristics. */
9951 if (old_target->attr.function)
9953 if (!proc_target->attr.function)
9955 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
9956 " FUNCTION", proc->name, &where);
9957 return FAILURE;
9960 /* FIXME: Do more comprehensive checking (including, for instance, the
9961 rank and array-shape). */
9962 gcc_assert (proc_target->result && old_target->result);
9963 if (!gfc_compare_types (&proc_target->result->ts,
9964 &old_target->result->ts))
9966 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
9967 " matching result types", proc->name, &where);
9968 return FAILURE;
9972 /* If the overridden binding is PUBLIC, the overriding one must not be
9973 PRIVATE. */
9974 if (old->n.tb->access == ACCESS_PUBLIC
9975 && proc->n.tb->access == ACCESS_PRIVATE)
9977 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
9978 " PRIVATE", proc->name, &where);
9979 return FAILURE;
9982 /* Compare the formal argument lists of both procedures. This is also abused
9983 to find the position of the passed-object dummy arguments of both
9984 bindings as at least the overridden one might not yet be resolved and we
9985 need those positions in the check below. */
9986 proc_pass_arg = old_pass_arg = 0;
9987 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
9988 proc_pass_arg = 1;
9989 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
9990 old_pass_arg = 1;
9991 argpos = 1;
9992 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
9993 proc_formal && old_formal;
9994 proc_formal = proc_formal->next, old_formal = old_formal->next)
9996 if (proc->n.tb->pass_arg
9997 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
9998 proc_pass_arg = argpos;
9999 if (old->n.tb->pass_arg
10000 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
10001 old_pass_arg = argpos;
10003 /* Check that the names correspond. */
10004 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
10006 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
10007 " to match the corresponding argument of the overridden"
10008 " procedure", proc_formal->sym->name, proc->name, &where,
10009 old_formal->sym->name);
10010 return FAILURE;
10013 /* Check that the types correspond if neither is the passed-object
10014 argument. */
10015 /* FIXME: Do more comprehensive testing here. */
10016 if (proc_pass_arg != argpos && old_pass_arg != argpos
10017 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
10019 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
10020 "in respect to the overridden procedure",
10021 proc_formal->sym->name, proc->name, &where);
10022 return FAILURE;
10025 ++argpos;
10027 if (proc_formal || old_formal)
10029 gfc_error ("'%s' at %L must have the same number of formal arguments as"
10030 " the overridden procedure", proc->name, &where);
10031 return FAILURE;
10034 /* If the overridden binding is NOPASS, the overriding one must also be
10035 NOPASS. */
10036 if (old->n.tb->nopass && !proc->n.tb->nopass)
10038 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
10039 " NOPASS", proc->name, &where);
10040 return FAILURE;
10043 /* If the overridden binding is PASS(x), the overriding one must also be
10044 PASS and the passed-object dummy arguments must correspond. */
10045 if (!old->n.tb->nopass)
10047 if (proc->n.tb->nopass)
10049 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
10050 " PASS", proc->name, &where);
10051 return FAILURE;
10054 if (proc_pass_arg != old_pass_arg)
10056 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
10057 " the same position as the passed-object dummy argument of"
10058 " the overridden procedure", proc->name, &where);
10059 return FAILURE;
10063 return SUCCESS;
10067 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
10069 static gfc_try
10070 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
10071 const char* generic_name, locus where)
10073 gfc_symbol* sym1;
10074 gfc_symbol* sym2;
10076 gcc_assert (t1->specific && t2->specific);
10077 gcc_assert (!t1->specific->is_generic);
10078 gcc_assert (!t2->specific->is_generic);
10080 sym1 = t1->specific->u.specific->n.sym;
10081 sym2 = t2->specific->u.specific->n.sym;
10083 if (sym1 == sym2)
10084 return SUCCESS;
10086 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
10087 if (sym1->attr.subroutine != sym2->attr.subroutine
10088 || sym1->attr.function != sym2->attr.function)
10090 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
10091 " GENERIC '%s' at %L",
10092 sym1->name, sym2->name, generic_name, &where);
10093 return FAILURE;
10096 /* Compare the interfaces. */
10097 if (gfc_compare_interfaces (sym1, sym2, sym2->name, 1, 0, NULL, 0))
10099 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
10100 sym1->name, sym2->name, generic_name, &where);
10101 return FAILURE;
10104 return SUCCESS;
10108 /* Worker function for resolving a generic procedure binding; this is used to
10109 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
10111 The difference between those cases is finding possible inherited bindings
10112 that are overridden, as one has to look for them in tb_sym_root,
10113 tb_uop_root or tb_op, respectively. Thus the caller must already find
10114 the super-type and set p->overridden correctly. */
10116 static gfc_try
10117 resolve_tb_generic_targets (gfc_symbol* super_type,
10118 gfc_typebound_proc* p, const char* name)
10120 gfc_tbp_generic* target;
10121 gfc_symtree* first_target;
10122 gfc_symtree* inherited;
10124 gcc_assert (p && p->is_generic);
10126 /* Try to find the specific bindings for the symtrees in our target-list. */
10127 gcc_assert (p->u.generic);
10128 for (target = p->u.generic; target; target = target->next)
10129 if (!target->specific)
10131 gfc_typebound_proc* overridden_tbp;
10132 gfc_tbp_generic* g;
10133 const char* target_name;
10135 target_name = target->specific_st->name;
10137 /* Defined for this type directly. */
10138 if (target->specific_st->n.tb)
10140 target->specific = target->specific_st->n.tb;
10141 goto specific_found;
10144 /* Look for an inherited specific binding. */
10145 if (super_type)
10147 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
10148 true, NULL);
10150 if (inherited)
10152 gcc_assert (inherited->n.tb);
10153 target->specific = inherited->n.tb;
10154 goto specific_found;
10158 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
10159 " at %L", target_name, name, &p->where);
10160 return FAILURE;
10162 /* Once we've found the specific binding, check it is not ambiguous with
10163 other specifics already found or inherited for the same GENERIC. */
10164 specific_found:
10165 gcc_assert (target->specific);
10167 /* This must really be a specific binding! */
10168 if (target->specific->is_generic)
10170 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
10171 " '%s' is GENERIC, too", name, &p->where, target_name);
10172 return FAILURE;
10175 /* Check those already resolved on this type directly. */
10176 for (g = p->u.generic; g; g = g->next)
10177 if (g != target && g->specific
10178 && check_generic_tbp_ambiguity (target, g, name, p->where)
10179 == FAILURE)
10180 return FAILURE;
10182 /* Check for ambiguity with inherited specific targets. */
10183 for (overridden_tbp = p->overridden; overridden_tbp;
10184 overridden_tbp = overridden_tbp->overridden)
10185 if (overridden_tbp->is_generic)
10187 for (g = overridden_tbp->u.generic; g; g = g->next)
10189 gcc_assert (g->specific);
10190 if (check_generic_tbp_ambiguity (target, g,
10191 name, p->where) == FAILURE)
10192 return FAILURE;
10197 /* If we attempt to "overwrite" a specific binding, this is an error. */
10198 if (p->overridden && !p->overridden->is_generic)
10200 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
10201 " the same name", name, &p->where);
10202 return FAILURE;
10205 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
10206 all must have the same attributes here. */
10207 first_target = p->u.generic->specific->u.specific;
10208 gcc_assert (first_target);
10209 p->subroutine = first_target->n.sym->attr.subroutine;
10210 p->function = first_target->n.sym->attr.function;
10212 return SUCCESS;
10216 /* Resolve a GENERIC procedure binding for a derived type. */
10218 static gfc_try
10219 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
10221 gfc_symbol* super_type;
10223 /* Find the overridden binding if any. */
10224 st->n.tb->overridden = NULL;
10225 super_type = gfc_get_derived_super_type (derived);
10226 if (super_type)
10228 gfc_symtree* overridden;
10229 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
10230 true, NULL);
10232 if (overridden && overridden->n.tb)
10233 st->n.tb->overridden = overridden->n.tb;
10236 /* Resolve using worker function. */
10237 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
10241 /* Retrieve the target-procedure of an operator binding and do some checks in
10242 common for intrinsic and user-defined type-bound operators. */
10244 static gfc_symbol*
10245 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
10247 gfc_symbol* target_proc;
10249 gcc_assert (target->specific && !target->specific->is_generic);
10250 target_proc = target->specific->u.specific->n.sym;
10251 gcc_assert (target_proc);
10253 /* All operator bindings must have a passed-object dummy argument. */
10254 if (target->specific->nopass)
10256 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
10257 return NULL;
10260 return target_proc;
10264 /* Resolve a type-bound intrinsic operator. */
10266 static gfc_try
10267 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
10268 gfc_typebound_proc* p)
10270 gfc_symbol* super_type;
10271 gfc_tbp_generic* target;
10273 /* If there's already an error here, do nothing (but don't fail again). */
10274 if (p->error)
10275 return SUCCESS;
10277 /* Operators should always be GENERIC bindings. */
10278 gcc_assert (p->is_generic);
10280 /* Look for an overridden binding. */
10281 super_type = gfc_get_derived_super_type (derived);
10282 if (super_type && super_type->f2k_derived)
10283 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
10284 op, true, NULL);
10285 else
10286 p->overridden = NULL;
10288 /* Resolve general GENERIC properties using worker function. */
10289 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
10290 goto error;
10292 /* Check the targets to be procedures of correct interface. */
10293 for (target = p->u.generic; target; target = target->next)
10295 gfc_symbol* target_proc;
10297 target_proc = get_checked_tb_operator_target (target, p->where);
10298 if (!target_proc)
10299 goto error;
10301 if (!gfc_check_operator_interface (target_proc, op, p->where))
10302 goto error;
10305 return SUCCESS;
10307 error:
10308 p->error = 1;
10309 return FAILURE;
10313 /* Resolve a type-bound user operator (tree-walker callback). */
10315 static gfc_symbol* resolve_bindings_derived;
10316 static gfc_try resolve_bindings_result;
10318 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
10320 static void
10321 resolve_typebound_user_op (gfc_symtree* stree)
10323 gfc_symbol* super_type;
10324 gfc_tbp_generic* target;
10326 gcc_assert (stree && stree->n.tb);
10328 if (stree->n.tb->error)
10329 return;
10331 /* Operators should always be GENERIC bindings. */
10332 gcc_assert (stree->n.tb->is_generic);
10334 /* Find overridden procedure, if any. */
10335 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10336 if (super_type && super_type->f2k_derived)
10338 gfc_symtree* overridden;
10339 overridden = gfc_find_typebound_user_op (super_type, NULL,
10340 stree->name, true, NULL);
10342 if (overridden && overridden->n.tb)
10343 stree->n.tb->overridden = overridden->n.tb;
10345 else
10346 stree->n.tb->overridden = NULL;
10348 /* Resolve basically using worker function. */
10349 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
10350 == FAILURE)
10351 goto error;
10353 /* Check the targets to be functions of correct interface. */
10354 for (target = stree->n.tb->u.generic; target; target = target->next)
10356 gfc_symbol* target_proc;
10358 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
10359 if (!target_proc)
10360 goto error;
10362 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
10363 goto error;
10366 return;
10368 error:
10369 resolve_bindings_result = FAILURE;
10370 stree->n.tb->error = 1;
10374 /* Resolve the type-bound procedures for a derived type. */
10376 static void
10377 resolve_typebound_procedure (gfc_symtree* stree)
10379 gfc_symbol* proc;
10380 locus where;
10381 gfc_symbol* me_arg;
10382 gfc_symbol* super_type;
10383 gfc_component* comp;
10385 gcc_assert (stree);
10387 /* Undefined specific symbol from GENERIC target definition. */
10388 if (!stree->n.tb)
10389 return;
10391 if (stree->n.tb->error)
10392 return;
10394 /* If this is a GENERIC binding, use that routine. */
10395 if (stree->n.tb->is_generic)
10397 if (resolve_typebound_generic (resolve_bindings_derived, stree)
10398 == FAILURE)
10399 goto error;
10400 return;
10403 /* Get the target-procedure to check it. */
10404 gcc_assert (!stree->n.tb->is_generic);
10405 gcc_assert (stree->n.tb->u.specific);
10406 proc = stree->n.tb->u.specific->n.sym;
10407 where = stree->n.tb->where;
10409 /* Default access should already be resolved from the parser. */
10410 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
10412 /* It should be a module procedure or an external procedure with explicit
10413 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
10414 if ((!proc->attr.subroutine && !proc->attr.function)
10415 || (proc->attr.proc != PROC_MODULE
10416 && proc->attr.if_source != IFSRC_IFBODY)
10417 || (proc->attr.abstract && !stree->n.tb->deferred))
10419 gfc_error ("'%s' must be a module procedure or an external procedure with"
10420 " an explicit interface at %L", proc->name, &where);
10421 goto error;
10423 stree->n.tb->subroutine = proc->attr.subroutine;
10424 stree->n.tb->function = proc->attr.function;
10426 /* Find the super-type of the current derived type. We could do this once and
10427 store in a global if speed is needed, but as long as not I believe this is
10428 more readable and clearer. */
10429 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10431 /* If PASS, resolve and check arguments if not already resolved / loaded
10432 from a .mod file. */
10433 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
10435 if (stree->n.tb->pass_arg)
10437 gfc_formal_arglist* i;
10439 /* If an explicit passing argument name is given, walk the arg-list
10440 and look for it. */
10442 me_arg = NULL;
10443 stree->n.tb->pass_arg_num = 1;
10444 for (i = proc->formal; i; i = i->next)
10446 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
10448 me_arg = i->sym;
10449 break;
10451 ++stree->n.tb->pass_arg_num;
10454 if (!me_arg)
10456 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
10457 " argument '%s'",
10458 proc->name, stree->n.tb->pass_arg, &where,
10459 stree->n.tb->pass_arg);
10460 goto error;
10463 else
10465 /* Otherwise, take the first one; there should in fact be at least
10466 one. */
10467 stree->n.tb->pass_arg_num = 1;
10468 if (!proc->formal)
10470 gfc_error ("Procedure '%s' with PASS at %L must have at"
10471 " least one argument", proc->name, &where);
10472 goto error;
10474 me_arg = proc->formal->sym;
10477 /* Now check that the argument-type matches and the passed-object
10478 dummy argument is generally fine. */
10480 gcc_assert (me_arg);
10482 if (me_arg->ts.type != BT_CLASS)
10484 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10485 " at %L", proc->name, &where);
10486 goto error;
10489 if (me_arg->ts.u.derived->components->ts.u.derived
10490 != resolve_bindings_derived)
10492 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10493 " the derived-type '%s'", me_arg->name, proc->name,
10494 me_arg->name, &where, resolve_bindings_derived->name);
10495 goto error;
10498 gcc_assert (me_arg->ts.type == BT_CLASS);
10499 if (me_arg->ts.u.derived->components->as
10500 && me_arg->ts.u.derived->components->as->rank > 0)
10502 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
10503 " scalar", proc->name, &where);
10504 goto error;
10506 if (me_arg->ts.u.derived->components->attr.allocatable)
10508 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10509 " be ALLOCATABLE", proc->name, &where);
10510 goto error;
10512 if (me_arg->ts.u.derived->components->attr.class_pointer)
10514 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10515 " be POINTER", proc->name, &where);
10516 goto error;
10520 /* If we are extending some type, check that we don't override a procedure
10521 flagged NON_OVERRIDABLE. */
10522 stree->n.tb->overridden = NULL;
10523 if (super_type)
10525 gfc_symtree* overridden;
10526 overridden = gfc_find_typebound_proc (super_type, NULL,
10527 stree->name, true, NULL);
10529 if (overridden && overridden->n.tb)
10530 stree->n.tb->overridden = overridden->n.tb;
10532 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
10533 goto error;
10536 /* See if there's a name collision with a component directly in this type. */
10537 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
10538 if (!strcmp (comp->name, stree->name))
10540 gfc_error ("Procedure '%s' at %L has the same name as a component of"
10541 " '%s'",
10542 stree->name, &where, resolve_bindings_derived->name);
10543 goto error;
10546 /* Try to find a name collision with an inherited component. */
10547 if (super_type && gfc_find_component (super_type, stree->name, true, true))
10549 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
10550 " component of '%s'",
10551 stree->name, &where, resolve_bindings_derived->name);
10552 goto error;
10555 stree->n.tb->error = 0;
10556 return;
10558 error:
10559 resolve_bindings_result = FAILURE;
10560 stree->n.tb->error = 1;
10563 static gfc_try
10564 resolve_typebound_procedures (gfc_symbol* derived)
10566 int op;
10568 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
10569 return SUCCESS;
10571 resolve_bindings_derived = derived;
10572 resolve_bindings_result = SUCCESS;
10574 if (derived->f2k_derived->tb_sym_root)
10575 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
10576 &resolve_typebound_procedure);
10578 if (derived->f2k_derived->tb_uop_root)
10579 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
10580 &resolve_typebound_user_op);
10582 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
10584 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
10585 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
10586 p) == FAILURE)
10587 resolve_bindings_result = FAILURE;
10590 return resolve_bindings_result;
10594 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
10595 to give all identical derived types the same backend_decl. */
10596 static void
10597 add_dt_to_dt_list (gfc_symbol *derived)
10599 gfc_dt_list *dt_list;
10601 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
10602 if (derived == dt_list->derived)
10603 break;
10605 if (dt_list == NULL)
10607 dt_list = gfc_get_dt_list ();
10608 dt_list->next = gfc_derived_types;
10609 dt_list->derived = derived;
10610 gfc_derived_types = dt_list;
10615 /* Ensure that a derived-type is really not abstract, meaning that every
10616 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
10618 static gfc_try
10619 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
10621 if (!st)
10622 return SUCCESS;
10624 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
10625 return FAILURE;
10626 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
10627 return FAILURE;
10629 if (st->n.tb && st->n.tb->deferred)
10631 gfc_symtree* overriding;
10632 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
10633 if (!overriding)
10634 return FAILURE;
10635 gcc_assert (overriding->n.tb);
10636 if (overriding->n.tb->deferred)
10638 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
10639 " '%s' is DEFERRED and not overridden",
10640 sub->name, &sub->declared_at, st->name);
10641 return FAILURE;
10645 return SUCCESS;
10648 static gfc_try
10649 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
10651 /* The algorithm used here is to recursively travel up the ancestry of sub
10652 and for each ancestor-type, check all bindings. If any of them is
10653 DEFERRED, look it up starting from sub and see if the found (overriding)
10654 binding is not DEFERRED.
10655 This is not the most efficient way to do this, but it should be ok and is
10656 clearer than something sophisticated. */
10658 gcc_assert (ancestor && ancestor->attr.abstract && !sub->attr.abstract);
10660 /* Walk bindings of this ancestor. */
10661 if (ancestor->f2k_derived)
10663 gfc_try t;
10664 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
10665 if (t == FAILURE)
10666 return FAILURE;
10669 /* Find next ancestor type and recurse on it. */
10670 ancestor = gfc_get_derived_super_type (ancestor);
10671 if (ancestor)
10672 return ensure_not_abstract (sub, ancestor);
10674 return SUCCESS;
10678 static void resolve_symbol (gfc_symbol *sym);
10681 /* Resolve the components of a derived type. */
10683 static gfc_try
10684 resolve_fl_derived (gfc_symbol *sym)
10686 gfc_symbol* super_type;
10687 gfc_component *c;
10688 int i;
10690 super_type = gfc_get_derived_super_type (sym);
10692 /* F2008, C432. */
10693 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
10695 gfc_error ("As extending type '%s' at %L has a coarray component, "
10696 "parent type '%s' shall also have one", sym->name,
10697 &sym->declared_at, super_type->name);
10698 return FAILURE;
10701 /* Ensure the extended type gets resolved before we do. */
10702 if (super_type && resolve_fl_derived (super_type) == FAILURE)
10703 return FAILURE;
10705 /* An ABSTRACT type must be extensible. */
10706 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
10708 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
10709 sym->name, &sym->declared_at);
10710 return FAILURE;
10713 for (c = sym->components; c != NULL; c = c->next)
10715 /* F2008, C442. */
10716 if (c->attr.codimension /* FIXME: c->as check due to PR 43412. */
10717 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
10719 gfc_error ("Coarray component '%s' at %L must be allocatable with "
10720 "deferred shape", c->name, &c->loc);
10721 return FAILURE;
10724 /* F2008, C443. */
10725 if (c->attr.codimension && c->ts.type == BT_DERIVED
10726 && c->ts.u.derived->ts.is_iso_c)
10728 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
10729 "shall not be a coarray", c->name, &c->loc);
10730 return FAILURE;
10733 /* F2008, C444. */
10734 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
10735 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
10736 || c->attr.allocatable))
10738 gfc_error ("Component '%s' at %L with coarray component "
10739 "shall be a nonpointer, nonallocatable scalar",
10740 c->name, &c->loc);
10741 return FAILURE;
10744 if (c->attr.proc_pointer && c->ts.interface)
10746 if (c->ts.interface->attr.procedure)
10747 gfc_error ("Interface '%s', used by procedure pointer component "
10748 "'%s' at %L, is declared in a later PROCEDURE statement",
10749 c->ts.interface->name, c->name, &c->loc);
10751 /* Get the attributes from the interface (now resolved). */
10752 if (c->ts.interface->attr.if_source
10753 || c->ts.interface->attr.intrinsic)
10755 gfc_symbol *ifc = c->ts.interface;
10757 if (ifc->formal && !ifc->formal_ns)
10758 resolve_symbol (ifc);
10760 if (ifc->attr.intrinsic)
10761 resolve_intrinsic (ifc, &ifc->declared_at);
10763 if (ifc->result)
10765 c->ts = ifc->result->ts;
10766 c->attr.allocatable = ifc->result->attr.allocatable;
10767 c->attr.pointer = ifc->result->attr.pointer;
10768 c->attr.dimension = ifc->result->attr.dimension;
10769 c->as = gfc_copy_array_spec (ifc->result->as);
10771 else
10773 c->ts = ifc->ts;
10774 c->attr.allocatable = ifc->attr.allocatable;
10775 c->attr.pointer = ifc->attr.pointer;
10776 c->attr.dimension = ifc->attr.dimension;
10777 c->as = gfc_copy_array_spec (ifc->as);
10779 c->ts.interface = ifc;
10780 c->attr.function = ifc->attr.function;
10781 c->attr.subroutine = ifc->attr.subroutine;
10782 gfc_copy_formal_args_ppc (c, ifc);
10784 c->attr.pure = ifc->attr.pure;
10785 c->attr.elemental = ifc->attr.elemental;
10786 c->attr.recursive = ifc->attr.recursive;
10787 c->attr.always_explicit = ifc->attr.always_explicit;
10788 c->attr.ext_attr |= ifc->attr.ext_attr;
10789 /* Replace symbols in array spec. */
10790 if (c->as)
10792 int i;
10793 for (i = 0; i < c->as->rank; i++)
10795 gfc_expr_replace_comp (c->as->lower[i], c);
10796 gfc_expr_replace_comp (c->as->upper[i], c);
10799 /* Copy char length. */
10800 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
10802 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
10803 gfc_expr_replace_comp (cl->length, c);
10804 if (cl->length && !cl->resolved
10805 && gfc_resolve_expr (cl->length) == FAILURE)
10806 return FAILURE;
10807 c->ts.u.cl = cl;
10810 else if (c->ts.interface->name[0] != '\0')
10812 gfc_error ("Interface '%s' of procedure pointer component "
10813 "'%s' at %L must be explicit", c->ts.interface->name,
10814 c->name, &c->loc);
10815 return FAILURE;
10818 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
10820 /* Since PPCs are not implicitly typed, a PPC without an explicit
10821 interface must be a subroutine. */
10822 gfc_add_subroutine (&c->attr, c->name, &c->loc);
10825 /* Procedure pointer components: Check PASS arg. */
10826 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0)
10828 gfc_symbol* me_arg;
10830 if (c->tb->pass_arg)
10832 gfc_formal_arglist* i;
10834 /* If an explicit passing argument name is given, walk the arg-list
10835 and look for it. */
10837 me_arg = NULL;
10838 c->tb->pass_arg_num = 1;
10839 for (i = c->formal; i; i = i->next)
10841 if (!strcmp (i->sym->name, c->tb->pass_arg))
10843 me_arg = i->sym;
10844 break;
10846 c->tb->pass_arg_num++;
10849 if (!me_arg)
10851 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
10852 "at %L has no argument '%s'", c->name,
10853 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
10854 c->tb->error = 1;
10855 return FAILURE;
10858 else
10860 /* Otherwise, take the first one; there should in fact be at least
10861 one. */
10862 c->tb->pass_arg_num = 1;
10863 if (!c->formal)
10865 gfc_error ("Procedure pointer component '%s' with PASS at %L "
10866 "must have at least one argument",
10867 c->name, &c->loc);
10868 c->tb->error = 1;
10869 return FAILURE;
10871 me_arg = c->formal->sym;
10874 /* Now check that the argument-type matches. */
10875 gcc_assert (me_arg);
10876 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
10877 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
10878 || (me_arg->ts.type == BT_CLASS
10879 && me_arg->ts.u.derived->components->ts.u.derived != sym))
10881 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10882 " the derived type '%s'", me_arg->name, c->name,
10883 me_arg->name, &c->loc, sym->name);
10884 c->tb->error = 1;
10885 return FAILURE;
10888 /* Check for C453. */
10889 if (me_arg->attr.dimension)
10891 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10892 "must be scalar", me_arg->name, c->name, me_arg->name,
10893 &c->loc);
10894 c->tb->error = 1;
10895 return FAILURE;
10898 if (me_arg->attr.pointer)
10900 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10901 "may not have the POINTER attribute", me_arg->name,
10902 c->name, me_arg->name, &c->loc);
10903 c->tb->error = 1;
10904 return FAILURE;
10907 if (me_arg->attr.allocatable)
10909 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10910 "may not be ALLOCATABLE", me_arg->name, c->name,
10911 me_arg->name, &c->loc);
10912 c->tb->error = 1;
10913 return FAILURE;
10916 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
10917 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10918 " at %L", c->name, &c->loc);
10922 /* Check type-spec if this is not the parent-type component. */
10923 if ((!sym->attr.extension || c != sym->components)
10924 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
10925 return FAILURE;
10927 /* If this type is an extension, set the accessibility of the parent
10928 component. */
10929 if (super_type && c == sym->components
10930 && strcmp (super_type->name, c->name) == 0)
10931 c->attr.access = super_type->attr.access;
10933 /* If this type is an extension, see if this component has the same name
10934 as an inherited type-bound procedure. */
10935 if (super_type
10936 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
10938 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
10939 " inherited type-bound procedure",
10940 c->name, sym->name, &c->loc);
10941 return FAILURE;
10944 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
10946 if (c->ts.u.cl->length == NULL
10947 || (resolve_charlen (c->ts.u.cl) == FAILURE)
10948 || !gfc_is_constant_expr (c->ts.u.cl->length))
10950 gfc_error ("Character length of component '%s' needs to "
10951 "be a constant specification expression at %L",
10952 c->name,
10953 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
10954 return FAILURE;
10958 if (c->ts.type == BT_DERIVED
10959 && sym->component_access != ACCESS_PRIVATE
10960 && gfc_check_access (sym->attr.access, sym->ns->default_access)
10961 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
10962 && !c->ts.u.derived->attr.use_assoc
10963 && !gfc_check_access (c->ts.u.derived->attr.access,
10964 c->ts.u.derived->ns->default_access)
10965 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
10966 "is a PRIVATE type and cannot be a component of "
10967 "'%s', which is PUBLIC at %L", c->name,
10968 sym->name, &sym->declared_at) == FAILURE)
10969 return FAILURE;
10971 if (sym->attr.sequence)
10973 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
10975 gfc_error ("Component %s of SEQUENCE type declared at %L does "
10976 "not have the SEQUENCE attribute",
10977 c->ts.u.derived->name, &sym->declared_at);
10978 return FAILURE;
10982 if (c->ts.type == BT_DERIVED && c->attr.pointer
10983 && c->ts.u.derived->components == NULL
10984 && !c->ts.u.derived->attr.zero_comp)
10986 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
10987 "that has not been declared", c->name, sym->name,
10988 &c->loc);
10989 return FAILURE;
10992 /* C437. */
10993 if (c->ts.type == BT_CLASS
10994 && !(c->ts.u.derived->components->attr.pointer
10995 || c->ts.u.derived->components->attr.allocatable))
10997 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
10998 "or pointer", c->name, &c->loc);
10999 return FAILURE;
11002 /* Ensure that all the derived type components are put on the
11003 derived type list; even in formal namespaces, where derived type
11004 pointer components might not have been declared. */
11005 if (c->ts.type == BT_DERIVED
11006 && c->ts.u.derived
11007 && c->ts.u.derived->components
11008 && c->attr.pointer
11009 && sym != c->ts.u.derived)
11010 add_dt_to_dt_list (c->ts.u.derived);
11012 if (c->attr.pointer || c->attr.proc_pointer || c->attr.allocatable
11013 || c->as == NULL)
11014 continue;
11016 for (i = 0; i < c->as->rank; i++)
11018 if (c->as->lower[i] == NULL
11019 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
11020 || !gfc_is_constant_expr (c->as->lower[i])
11021 || c->as->upper[i] == NULL
11022 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
11023 || !gfc_is_constant_expr (c->as->upper[i]))
11025 gfc_error ("Component '%s' of '%s' at %L must have "
11026 "constant array bounds",
11027 c->name, sym->name, &c->loc);
11028 return FAILURE;
11033 /* Resolve the type-bound procedures. */
11034 if (resolve_typebound_procedures (sym) == FAILURE)
11035 return FAILURE;
11037 /* Resolve the finalizer procedures. */
11038 if (gfc_resolve_finalizers (sym) == FAILURE)
11039 return FAILURE;
11041 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
11042 all DEFERRED bindings are overridden. */
11043 if (super_type && super_type->attr.abstract && !sym->attr.abstract
11044 && ensure_not_abstract (sym, super_type) == FAILURE)
11045 return FAILURE;
11047 /* Add derived type to the derived type list. */
11048 add_dt_to_dt_list (sym);
11050 return SUCCESS;
11054 static gfc_try
11055 resolve_fl_namelist (gfc_symbol *sym)
11057 gfc_namelist *nl;
11058 gfc_symbol *nlsym;
11060 /* Reject PRIVATE objects in a PUBLIC namelist. */
11061 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
11063 for (nl = sym->namelist; nl; nl = nl->next)
11065 if (!nl->sym->attr.use_assoc
11066 && !is_sym_host_assoc (nl->sym, sym->ns)
11067 && !gfc_check_access(nl->sym->attr.access,
11068 nl->sym->ns->default_access))
11070 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
11071 "cannot be member of PUBLIC namelist '%s' at %L",
11072 nl->sym->name, sym->name, &sym->declared_at);
11073 return FAILURE;
11076 /* Types with private components that came here by USE-association. */
11077 if (nl->sym->ts.type == BT_DERIVED
11078 && derived_inaccessible (nl->sym->ts.u.derived))
11080 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
11081 "components and cannot be member of namelist '%s' at %L",
11082 nl->sym->name, sym->name, &sym->declared_at);
11083 return FAILURE;
11086 /* Types with private components that are defined in the same module. */
11087 if (nl->sym->ts.type == BT_DERIVED
11088 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
11089 && !gfc_check_access (nl->sym->ts.u.derived->attr.private_comp
11090 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
11091 nl->sym->ns->default_access))
11093 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
11094 "cannot be a member of PUBLIC namelist '%s' at %L",
11095 nl->sym->name, sym->name, &sym->declared_at);
11096 return FAILURE;
11101 for (nl = sym->namelist; nl; nl = nl->next)
11103 /* Reject namelist arrays of assumed shape. */
11104 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
11105 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
11106 "must not have assumed shape in namelist "
11107 "'%s' at %L", nl->sym->name, sym->name,
11108 &sym->declared_at) == FAILURE)
11109 return FAILURE;
11111 /* Reject namelist arrays that are not constant shape. */
11112 if (is_non_constant_shape_array (nl->sym))
11114 gfc_error ("NAMELIST array object '%s' must have constant "
11115 "shape in namelist '%s' at %L", nl->sym->name,
11116 sym->name, &sym->declared_at);
11117 return FAILURE;
11120 /* Namelist objects cannot have allocatable or pointer components. */
11121 if (nl->sym->ts.type != BT_DERIVED)
11122 continue;
11124 if (nl->sym->ts.u.derived->attr.alloc_comp)
11126 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
11127 "have ALLOCATABLE components",
11128 nl->sym->name, sym->name, &sym->declared_at);
11129 return FAILURE;
11132 if (nl->sym->ts.u.derived->attr.pointer_comp)
11134 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
11135 "have POINTER components",
11136 nl->sym->name, sym->name, &sym->declared_at);
11137 return FAILURE;
11142 /* 14.1.2 A module or internal procedure represent local entities
11143 of the same type as a namelist member and so are not allowed. */
11144 for (nl = sym->namelist; nl; nl = nl->next)
11146 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
11147 continue;
11149 if (nl->sym->attr.function && nl->sym == nl->sym->result)
11150 if ((nl->sym == sym->ns->proc_name)
11152 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
11153 continue;
11155 nlsym = NULL;
11156 if (nl->sym && nl->sym->name)
11157 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
11158 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
11160 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
11161 "attribute in '%s' at %L", nlsym->name,
11162 &sym->declared_at);
11163 return FAILURE;
11167 return SUCCESS;
11171 static gfc_try
11172 resolve_fl_parameter (gfc_symbol *sym)
11174 /* A parameter array's shape needs to be constant. */
11175 if (sym->as != NULL
11176 && (sym->as->type == AS_DEFERRED
11177 || is_non_constant_shape_array (sym)))
11179 gfc_error ("Parameter array '%s' at %L cannot be automatic "
11180 "or of deferred shape", sym->name, &sym->declared_at);
11181 return FAILURE;
11184 /* Make sure a parameter that has been implicitly typed still
11185 matches the implicit type, since PARAMETER statements can precede
11186 IMPLICIT statements. */
11187 if (sym->attr.implicit_type
11188 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
11189 sym->ns)))
11191 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
11192 "later IMPLICIT type", sym->name, &sym->declared_at);
11193 return FAILURE;
11196 /* Make sure the types of derived parameters are consistent. This
11197 type checking is deferred until resolution because the type may
11198 refer to a derived type from the host. */
11199 if (sym->ts.type == BT_DERIVED
11200 && !gfc_compare_types (&sym->ts, &sym->value->ts))
11202 gfc_error ("Incompatible derived type in PARAMETER at %L",
11203 &sym->value->where);
11204 return FAILURE;
11206 return SUCCESS;
11210 /* Do anything necessary to resolve a symbol. Right now, we just
11211 assume that an otherwise unknown symbol is a variable. This sort
11212 of thing commonly happens for symbols in module. */
11214 static void
11215 resolve_symbol (gfc_symbol *sym)
11217 int check_constant, mp_flag;
11218 gfc_symtree *symtree;
11219 gfc_symtree *this_symtree;
11220 gfc_namespace *ns;
11221 gfc_component *c;
11223 if (sym->attr.flavor == FL_UNKNOWN)
11226 /* If we find that a flavorless symbol is an interface in one of the
11227 parent namespaces, find its symtree in this namespace, free the
11228 symbol and set the symtree to point to the interface symbol. */
11229 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
11231 symtree = gfc_find_symtree (ns->sym_root, sym->name);
11232 if (symtree && symtree->n.sym->generic)
11234 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
11235 sym->name);
11236 sym->refs--;
11237 if (!sym->refs)
11238 gfc_free_symbol (sym);
11239 symtree->n.sym->refs++;
11240 this_symtree->n.sym = symtree->n.sym;
11241 return;
11245 /* Otherwise give it a flavor according to such attributes as
11246 it has. */
11247 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
11248 sym->attr.flavor = FL_VARIABLE;
11249 else
11251 sym->attr.flavor = FL_PROCEDURE;
11252 if (sym->attr.dimension)
11253 sym->attr.function = 1;
11257 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
11258 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
11260 if (sym->attr.procedure && sym->ts.interface
11261 && sym->attr.if_source != IFSRC_DECL)
11263 if (sym->ts.interface == sym)
11265 gfc_error ("PROCEDURE '%s' at %L may not be used as its own "
11266 "interface", sym->name, &sym->declared_at);
11267 return;
11269 if (sym->ts.interface->attr.procedure)
11271 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared"
11272 " in a later PROCEDURE statement", sym->ts.interface->name,
11273 sym->name,&sym->declared_at);
11274 return;
11277 /* Get the attributes from the interface (now resolved). */
11278 if (sym->ts.interface->attr.if_source
11279 || sym->ts.interface->attr.intrinsic)
11281 gfc_symbol *ifc = sym->ts.interface;
11282 resolve_symbol (ifc);
11284 if (ifc->attr.intrinsic)
11285 resolve_intrinsic (ifc, &ifc->declared_at);
11287 if (ifc->result)
11288 sym->ts = ifc->result->ts;
11289 else
11290 sym->ts = ifc->ts;
11291 sym->ts.interface = ifc;
11292 sym->attr.function = ifc->attr.function;
11293 sym->attr.subroutine = ifc->attr.subroutine;
11294 gfc_copy_formal_args (sym, ifc);
11296 sym->attr.allocatable = ifc->attr.allocatable;
11297 sym->attr.pointer = ifc->attr.pointer;
11298 sym->attr.pure = ifc->attr.pure;
11299 sym->attr.elemental = ifc->attr.elemental;
11300 sym->attr.dimension = ifc->attr.dimension;
11301 sym->attr.recursive = ifc->attr.recursive;
11302 sym->attr.always_explicit = ifc->attr.always_explicit;
11303 sym->attr.ext_attr |= ifc->attr.ext_attr;
11304 /* Copy array spec. */
11305 sym->as = gfc_copy_array_spec (ifc->as);
11306 if (sym->as)
11308 int i;
11309 for (i = 0; i < sym->as->rank; i++)
11311 gfc_expr_replace_symbols (sym->as->lower[i], sym);
11312 gfc_expr_replace_symbols (sym->as->upper[i], sym);
11315 /* Copy char length. */
11316 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
11318 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
11319 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
11320 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
11321 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
11322 return;
11325 else if (sym->ts.interface->name[0] != '\0')
11327 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
11328 sym->ts.interface->name, sym->name, &sym->declared_at);
11329 return;
11333 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
11334 return;
11336 /* Symbols that are module procedures with results (functions) have
11337 the types and array specification copied for type checking in
11338 procedures that call them, as well as for saving to a module
11339 file. These symbols can't stand the scrutiny that their results
11340 can. */
11341 mp_flag = (sym->result != NULL && sym->result != sym);
11344 /* Make sure that the intrinsic is consistent with its internal
11345 representation. This needs to be done before assigning a default
11346 type to avoid spurious warnings. */
11347 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
11348 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
11349 return;
11351 /* Assign default type to symbols that need one and don't have one. */
11352 if (sym->ts.type == BT_UNKNOWN)
11354 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
11355 gfc_set_default_type (sym, 1, NULL);
11357 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
11358 && !sym->attr.function && !sym->attr.subroutine
11359 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
11360 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
11362 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
11364 /* The specific case of an external procedure should emit an error
11365 in the case that there is no implicit type. */
11366 if (!mp_flag)
11367 gfc_set_default_type (sym, sym->attr.external, NULL);
11368 else
11370 /* Result may be in another namespace. */
11371 resolve_symbol (sym->result);
11373 if (!sym->result->attr.proc_pointer)
11375 sym->ts = sym->result->ts;
11376 sym->as = gfc_copy_array_spec (sym->result->as);
11377 sym->attr.dimension = sym->result->attr.dimension;
11378 sym->attr.pointer = sym->result->attr.pointer;
11379 sym->attr.allocatable = sym->result->attr.allocatable;
11385 /* Assumed size arrays and assumed shape arrays must be dummy
11386 arguments. */
11388 if (sym->as != NULL
11389 && ((sym->as->type == AS_ASSUMED_SIZE && !sym->as->cp_was_assumed)
11390 || sym->as->type == AS_ASSUMED_SHAPE)
11391 && sym->attr.dummy == 0)
11393 if (sym->as->type == AS_ASSUMED_SIZE)
11394 gfc_error ("Assumed size array at %L must be a dummy argument",
11395 &sym->declared_at);
11396 else
11397 gfc_error ("Assumed shape array at %L must be a dummy argument",
11398 &sym->declared_at);
11399 return;
11402 /* Make sure symbols with known intent or optional are really dummy
11403 variable. Because of ENTRY statement, this has to be deferred
11404 until resolution time. */
11406 if (!sym->attr.dummy
11407 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
11409 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
11410 return;
11413 if (sym->attr.value && !sym->attr.dummy)
11415 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
11416 "it is not a dummy argument", sym->name, &sym->declared_at);
11417 return;
11420 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
11422 gfc_charlen *cl = sym->ts.u.cl;
11423 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11425 gfc_error ("Character dummy variable '%s' at %L with VALUE "
11426 "attribute must have constant length",
11427 sym->name, &sym->declared_at);
11428 return;
11431 if (sym->ts.is_c_interop
11432 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
11434 gfc_error ("C interoperable character dummy variable '%s' at %L "
11435 "with VALUE attribute must have length one",
11436 sym->name, &sym->declared_at);
11437 return;
11441 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
11442 do this for something that was implicitly typed because that is handled
11443 in gfc_set_default_type. Handle dummy arguments and procedure
11444 definitions separately. Also, anything that is use associated is not
11445 handled here but instead is handled in the module it is declared in.
11446 Finally, derived type definitions are allowed to be BIND(C) since that
11447 only implies that they're interoperable, and they are checked fully for
11448 interoperability when a variable is declared of that type. */
11449 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
11450 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
11451 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
11453 gfc_try t = SUCCESS;
11455 /* First, make sure the variable is declared at the
11456 module-level scope (J3/04-007, Section 15.3). */
11457 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
11458 sym->attr.in_common == 0)
11460 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
11461 "is neither a COMMON block nor declared at the "
11462 "module level scope", sym->name, &(sym->declared_at));
11463 t = FAILURE;
11465 else if (sym->common_head != NULL)
11467 t = verify_com_block_vars_c_interop (sym->common_head);
11469 else
11471 /* If type() declaration, we need to verify that the components
11472 of the given type are all C interoperable, etc. */
11473 if (sym->ts.type == BT_DERIVED &&
11474 sym->ts.u.derived->attr.is_c_interop != 1)
11476 /* Make sure the user marked the derived type as BIND(C). If
11477 not, call the verify routine. This could print an error
11478 for the derived type more than once if multiple variables
11479 of that type are declared. */
11480 if (sym->ts.u.derived->attr.is_bind_c != 1)
11481 verify_bind_c_derived_type (sym->ts.u.derived);
11482 t = FAILURE;
11485 /* Verify the variable itself as C interoperable if it
11486 is BIND(C). It is not possible for this to succeed if
11487 the verify_bind_c_derived_type failed, so don't have to handle
11488 any error returned by verify_bind_c_derived_type. */
11489 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11490 sym->common_block);
11493 if (t == FAILURE)
11495 /* clear the is_bind_c flag to prevent reporting errors more than
11496 once if something failed. */
11497 sym->attr.is_bind_c = 0;
11498 return;
11502 /* If a derived type symbol has reached this point, without its
11503 type being declared, we have an error. Notice that most
11504 conditions that produce undefined derived types have already
11505 been dealt with. However, the likes of:
11506 implicit type(t) (t) ..... call foo (t) will get us here if
11507 the type is not declared in the scope of the implicit
11508 statement. Change the type to BT_UNKNOWN, both because it is so
11509 and to prevent an ICE. */
11510 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
11511 && !sym->ts.u.derived->attr.zero_comp)
11513 gfc_error ("The derived type '%s' at %L is of type '%s', "
11514 "which has not been defined", sym->name,
11515 &sym->declared_at, sym->ts.u.derived->name);
11516 sym->ts.type = BT_UNKNOWN;
11517 return;
11520 /* Make sure that the derived type has been resolved and that the
11521 derived type is visible in the symbol's namespace, if it is a
11522 module function and is not PRIVATE. */
11523 if (sym->ts.type == BT_DERIVED
11524 && sym->ts.u.derived->attr.use_assoc
11525 && sym->ns->proc_name
11526 && sym->ns->proc_name->attr.flavor == FL_MODULE)
11528 gfc_symbol *ds;
11530 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
11531 return;
11533 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
11534 if (!ds && sym->attr.function
11535 && gfc_check_access (sym->attr.access, sym->ns->default_access))
11537 symtree = gfc_new_symtree (&sym->ns->sym_root,
11538 sym->ts.u.derived->name);
11539 symtree->n.sym = sym->ts.u.derived;
11540 sym->ts.u.derived->refs++;
11544 /* Unless the derived-type declaration is use associated, Fortran 95
11545 does not allow public entries of private derived types.
11546 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
11547 161 in 95-006r3. */
11548 if (sym->ts.type == BT_DERIVED
11549 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
11550 && !sym->ts.u.derived->attr.use_assoc
11551 && gfc_check_access (sym->attr.access, sym->ns->default_access)
11552 && !gfc_check_access (sym->ts.u.derived->attr.access,
11553 sym->ts.u.derived->ns->default_access)
11554 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
11555 "of PRIVATE derived type '%s'",
11556 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
11557 : "variable", sym->name, &sym->declared_at,
11558 sym->ts.u.derived->name) == FAILURE)
11559 return;
11561 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
11562 default initialization is defined (5.1.2.4.4). */
11563 if (sym->ts.type == BT_DERIVED
11564 && sym->attr.dummy
11565 && sym->attr.intent == INTENT_OUT
11566 && sym->as
11567 && sym->as->type == AS_ASSUMED_SIZE)
11569 for (c = sym->ts.u.derived->components; c; c = c->next)
11571 if (c->initializer)
11573 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
11574 "ASSUMED SIZE and so cannot have a default initializer",
11575 sym->name, &sym->declared_at);
11576 return;
11581 /* F2008, C526. */
11582 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
11583 || sym->attr.codimension)
11584 && sym->attr.result)
11585 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
11586 "a coarray component", sym->name, &sym->declared_at);
11588 /* F2008, C524. */
11589 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
11590 && sym->ts.u.derived->ts.is_iso_c)
11591 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
11592 "shall not be a coarray", sym->name, &sym->declared_at);
11594 /* F2008, C525. */
11595 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp
11596 && (sym->attr.codimension || sym->attr.pointer || sym->attr.dimension
11597 || sym->attr.allocatable))
11598 gfc_error ("Variable '%s' at %L with coarray component "
11599 "shall be a nonpointer, nonallocatable scalar",
11600 sym->name, &sym->declared_at);
11602 /* F2008, C526. The function-result case was handled above. */
11603 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
11604 || sym->attr.codimension)
11605 && !(sym->attr.allocatable || sym->attr.dummy || sym->attr.save
11606 || sym->ns->proc_name->attr.flavor == FL_MODULE
11607 || sym->ns->proc_name->attr.is_main_program
11608 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
11609 gfc_error ("Variable '%s' at %L is a coarray or has a coarray "
11610 "component and is not ALLOCATABLE, SAVE nor a "
11611 "dummy argument", sym->name, &sym->declared_at);
11612 /* F2008, C528. */ /* FIXME: sym->as check due to PR 43412. */
11613 else if (sym->attr.codimension && !sym->attr.allocatable
11614 && sym->as && sym->as->cotype == AS_DEFERRED)
11615 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
11616 "deferred shape", sym->name, &sym->declared_at);
11617 else if (sym->attr.codimension && sym->attr.allocatable
11618 && (sym->as->type != AS_DEFERRED || sym->as->cotype != AS_DEFERRED))
11619 gfc_error ("Allocatable coarray variable '%s' at %L must have "
11620 "deferred shape", sym->name, &sym->declared_at);
11623 /* F2008, C541. */
11624 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
11625 || (sym->attr.codimension && sym->attr.allocatable))
11626 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
11627 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
11628 "allocatable coarray or have coarray components",
11629 sym->name, &sym->declared_at);
11631 if (sym->attr.codimension && sym->attr.dummy
11632 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
11633 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
11634 "procedure '%s'", sym->name, &sym->declared_at,
11635 sym->ns->proc_name->name);
11637 switch (sym->attr.flavor)
11639 case FL_VARIABLE:
11640 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
11641 return;
11642 break;
11644 case FL_PROCEDURE:
11645 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
11646 return;
11647 break;
11649 case FL_NAMELIST:
11650 if (resolve_fl_namelist (sym) == FAILURE)
11651 return;
11652 break;
11654 case FL_PARAMETER:
11655 if (resolve_fl_parameter (sym) == FAILURE)
11656 return;
11657 break;
11659 default:
11660 break;
11663 /* Resolve array specifier. Check as well some constraints
11664 on COMMON blocks. */
11666 check_constant = sym->attr.in_common && !sym->attr.pointer;
11668 /* Set the formal_arg_flag so that check_conflict will not throw
11669 an error for host associated variables in the specification
11670 expression for an array_valued function. */
11671 if (sym->attr.function && sym->as)
11672 formal_arg_flag = 1;
11674 gfc_resolve_array_spec (sym->as, check_constant);
11676 formal_arg_flag = 0;
11678 /* Resolve formal namespaces. */
11679 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
11680 && !sym->attr.contained && !sym->attr.intrinsic)
11681 gfc_resolve (sym->formal_ns);
11683 /* Make sure the formal namespace is present. */
11684 if (sym->formal && !sym->formal_ns)
11686 gfc_formal_arglist *formal = sym->formal;
11687 while (formal && !formal->sym)
11688 formal = formal->next;
11690 if (formal)
11692 sym->formal_ns = formal->sym->ns;
11693 sym->formal_ns->refs++;
11697 /* Check threadprivate restrictions. */
11698 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
11699 && (!sym->attr.in_common
11700 && sym->module == NULL
11701 && (sym->ns->proc_name == NULL
11702 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
11703 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
11705 /* If we have come this far we can apply default-initializers, as
11706 described in 14.7.5, to those variables that have not already
11707 been assigned one. */
11708 if (sym->ts.type == BT_DERIVED
11709 && sym->attr.referenced
11710 && sym->ns == gfc_current_ns
11711 && !sym->value
11712 && !sym->attr.allocatable
11713 && !sym->attr.alloc_comp)
11715 symbol_attribute *a = &sym->attr;
11717 if ((!a->save && !a->dummy && !a->pointer
11718 && !a->in_common && !a->use_assoc
11719 && !(a->function && sym != sym->result))
11720 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
11721 apply_default_init (sym);
11724 /* If this symbol has a type-spec, check it. */
11725 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
11726 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
11727 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
11728 == FAILURE)
11729 return;
11733 /************* Resolve DATA statements *************/
11735 static struct
11737 gfc_data_value *vnode;
11738 mpz_t left;
11740 values;
11743 /* Advance the values structure to point to the next value in the data list. */
11745 static gfc_try
11746 next_data_value (void)
11748 while (mpz_cmp_ui (values.left, 0) == 0)
11751 if (values.vnode->next == NULL)
11752 return FAILURE;
11754 values.vnode = values.vnode->next;
11755 mpz_set (values.left, values.vnode->repeat);
11758 return SUCCESS;
11762 static gfc_try
11763 check_data_variable (gfc_data_variable *var, locus *where)
11765 gfc_expr *e;
11766 mpz_t size;
11767 mpz_t offset;
11768 gfc_try t;
11769 ar_type mark = AR_UNKNOWN;
11770 int i;
11771 mpz_t section_index[GFC_MAX_DIMENSIONS];
11772 gfc_ref *ref;
11773 gfc_array_ref *ar;
11774 gfc_symbol *sym;
11775 int has_pointer;
11777 if (gfc_resolve_expr (var->expr) == FAILURE)
11778 return FAILURE;
11780 ar = NULL;
11781 mpz_init_set_si (offset, 0);
11782 e = var->expr;
11784 if (e->expr_type != EXPR_VARIABLE)
11785 gfc_internal_error ("check_data_variable(): Bad expression");
11787 sym = e->symtree->n.sym;
11789 if (sym->ns->is_block_data && !sym->attr.in_common)
11791 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
11792 sym->name, &sym->declared_at);
11795 if (e->ref == NULL && sym->as)
11797 gfc_error ("DATA array '%s' at %L must be specified in a previous"
11798 " declaration", sym->name, where);
11799 return FAILURE;
11802 has_pointer = sym->attr.pointer;
11804 for (ref = e->ref; ref; ref = ref->next)
11806 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
11807 has_pointer = 1;
11809 if (ref->type == REF_ARRAY && ref->u.ar.codimen)
11811 gfc_error ("DATA element '%s' at %L cannot have a coindex",
11812 sym->name, where);
11813 return FAILURE;
11816 if (has_pointer
11817 && ref->type == REF_ARRAY
11818 && ref->u.ar.type != AR_FULL)
11820 gfc_error ("DATA element '%s' at %L is a pointer and so must "
11821 "be a full array", sym->name, where);
11822 return FAILURE;
11826 if (e->rank == 0 || has_pointer)
11828 mpz_init_set_ui (size, 1);
11829 ref = NULL;
11831 else
11833 ref = e->ref;
11835 /* Find the array section reference. */
11836 for (ref = e->ref; ref; ref = ref->next)
11838 if (ref->type != REF_ARRAY)
11839 continue;
11840 if (ref->u.ar.type == AR_ELEMENT)
11841 continue;
11842 break;
11844 gcc_assert (ref);
11846 /* Set marks according to the reference pattern. */
11847 switch (ref->u.ar.type)
11849 case AR_FULL:
11850 mark = AR_FULL;
11851 break;
11853 case AR_SECTION:
11854 ar = &ref->u.ar;
11855 /* Get the start position of array section. */
11856 gfc_get_section_index (ar, section_index, &offset);
11857 mark = AR_SECTION;
11858 break;
11860 default:
11861 gcc_unreachable ();
11864 if (gfc_array_size (e, &size) == FAILURE)
11866 gfc_error ("Nonconstant array section at %L in DATA statement",
11867 &e->where);
11868 mpz_clear (offset);
11869 return FAILURE;
11873 t = SUCCESS;
11875 while (mpz_cmp_ui (size, 0) > 0)
11877 if (next_data_value () == FAILURE)
11879 gfc_error ("DATA statement at %L has more variables than values",
11880 where);
11881 t = FAILURE;
11882 break;
11885 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
11886 if (t == FAILURE)
11887 break;
11889 /* If we have more than one element left in the repeat count,
11890 and we have more than one element left in the target variable,
11891 then create a range assignment. */
11892 /* FIXME: Only done for full arrays for now, since array sections
11893 seem tricky. */
11894 if (mark == AR_FULL && ref && ref->next == NULL
11895 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
11897 mpz_t range;
11899 if (mpz_cmp (size, values.left) >= 0)
11901 mpz_init_set (range, values.left);
11902 mpz_sub (size, size, values.left);
11903 mpz_set_ui (values.left, 0);
11905 else
11907 mpz_init_set (range, size);
11908 mpz_sub (values.left, values.left, size);
11909 mpz_set_ui (size, 0);
11912 gfc_assign_data_value_range (var->expr, values.vnode->expr,
11913 offset, range);
11915 mpz_add (offset, offset, range);
11916 mpz_clear (range);
11919 /* Assign initial value to symbol. */
11920 else
11922 mpz_sub_ui (values.left, values.left, 1);
11923 mpz_sub_ui (size, size, 1);
11925 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
11926 if (t == FAILURE)
11927 break;
11929 if (mark == AR_FULL)
11930 mpz_add_ui (offset, offset, 1);
11932 /* Modify the array section indexes and recalculate the offset
11933 for next element. */
11934 else if (mark == AR_SECTION)
11935 gfc_advance_section (section_index, ar, &offset);
11939 if (mark == AR_SECTION)
11941 for (i = 0; i < ar->dimen; i++)
11942 mpz_clear (section_index[i]);
11945 mpz_clear (size);
11946 mpz_clear (offset);
11948 return t;
11952 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
11954 /* Iterate over a list of elements in a DATA statement. */
11956 static gfc_try
11957 traverse_data_list (gfc_data_variable *var, locus *where)
11959 mpz_t trip;
11960 iterator_stack frame;
11961 gfc_expr *e, *start, *end, *step;
11962 gfc_try retval = SUCCESS;
11964 mpz_init (frame.value);
11966 start = gfc_copy_expr (var->iter.start);
11967 end = gfc_copy_expr (var->iter.end);
11968 step = gfc_copy_expr (var->iter.step);
11970 if (gfc_simplify_expr (start, 1) == FAILURE
11971 || start->expr_type != EXPR_CONSTANT)
11973 gfc_error ("iterator start at %L does not simplify", &start->where);
11974 retval = FAILURE;
11975 goto cleanup;
11977 if (gfc_simplify_expr (end, 1) == FAILURE
11978 || end->expr_type != EXPR_CONSTANT)
11980 gfc_error ("iterator end at %L does not simplify", &end->where);
11981 retval = FAILURE;
11982 goto cleanup;
11984 if (gfc_simplify_expr (step, 1) == FAILURE
11985 || step->expr_type != EXPR_CONSTANT)
11987 gfc_error ("iterator step at %L does not simplify", &step->where);
11988 retval = FAILURE;
11989 goto cleanup;
11992 mpz_init_set (trip, end->value.integer);
11993 mpz_sub (trip, trip, start->value.integer);
11994 mpz_add (trip, trip, step->value.integer);
11996 mpz_div (trip, trip, step->value.integer);
11998 mpz_set (frame.value, start->value.integer);
12000 frame.prev = iter_stack;
12001 frame.variable = var->iter.var->symtree;
12002 iter_stack = &frame;
12004 while (mpz_cmp_ui (trip, 0) > 0)
12006 if (traverse_data_var (var->list, where) == FAILURE)
12008 mpz_clear (trip);
12009 retval = FAILURE;
12010 goto cleanup;
12013 e = gfc_copy_expr (var->expr);
12014 if (gfc_simplify_expr (e, 1) == FAILURE)
12016 gfc_free_expr (e);
12017 mpz_clear (trip);
12018 retval = FAILURE;
12019 goto cleanup;
12022 mpz_add (frame.value, frame.value, step->value.integer);
12024 mpz_sub_ui (trip, trip, 1);
12027 mpz_clear (trip);
12028 cleanup:
12029 mpz_clear (frame.value);
12031 gfc_free_expr (start);
12032 gfc_free_expr (end);
12033 gfc_free_expr (step);
12035 iter_stack = frame.prev;
12036 return retval;
12040 /* Type resolve variables in the variable list of a DATA statement. */
12042 static gfc_try
12043 traverse_data_var (gfc_data_variable *var, locus *where)
12045 gfc_try t;
12047 for (; var; var = var->next)
12049 if (var->expr == NULL)
12050 t = traverse_data_list (var, where);
12051 else
12052 t = check_data_variable (var, where);
12054 if (t == FAILURE)
12055 return FAILURE;
12058 return SUCCESS;
12062 /* Resolve the expressions and iterators associated with a data statement.
12063 This is separate from the assignment checking because data lists should
12064 only be resolved once. */
12066 static gfc_try
12067 resolve_data_variables (gfc_data_variable *d)
12069 for (; d; d = d->next)
12071 if (d->list == NULL)
12073 if (gfc_resolve_expr (d->expr) == FAILURE)
12074 return FAILURE;
12076 else
12078 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
12079 return FAILURE;
12081 if (resolve_data_variables (d->list) == FAILURE)
12082 return FAILURE;
12086 return SUCCESS;
12090 /* Resolve a single DATA statement. We implement this by storing a pointer to
12091 the value list into static variables, and then recursively traversing the
12092 variables list, expanding iterators and such. */
12094 static void
12095 resolve_data (gfc_data *d)
12098 if (resolve_data_variables (d->var) == FAILURE)
12099 return;
12101 values.vnode = d->value;
12102 if (d->value == NULL)
12103 mpz_set_ui (values.left, 0);
12104 else
12105 mpz_set (values.left, d->value->repeat);
12107 if (traverse_data_var (d->var, &d->where) == FAILURE)
12108 return;
12110 /* At this point, we better not have any values left. */
12112 if (next_data_value () == SUCCESS)
12113 gfc_error ("DATA statement at %L has more values than variables",
12114 &d->where);
12118 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
12119 accessed by host or use association, is a dummy argument to a pure function,
12120 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
12121 is storage associated with any such variable, shall not be used in the
12122 following contexts: (clients of this function). */
12124 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
12125 procedure. Returns zero if assignment is OK, nonzero if there is a
12126 problem. */
12128 gfc_impure_variable (gfc_symbol *sym)
12130 gfc_symbol *proc;
12131 gfc_namespace *ns;
12133 if (sym->attr.use_assoc || sym->attr.in_common)
12134 return 1;
12136 /* Check if the symbol's ns is inside the pure procedure. */
12137 for (ns = gfc_current_ns; ns; ns = ns->parent)
12139 if (ns == sym->ns)
12140 break;
12141 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
12142 return 1;
12145 proc = sym->ns->proc_name;
12146 if (sym->attr.dummy && gfc_pure (proc)
12147 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
12149 proc->attr.function))
12150 return 1;
12152 /* TODO: Sort out what can be storage associated, if anything, and include
12153 it here. In principle equivalences should be scanned but it does not
12154 seem to be possible to storage associate an impure variable this way. */
12155 return 0;
12159 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
12160 current namespace is inside a pure procedure. */
12163 gfc_pure (gfc_symbol *sym)
12165 symbol_attribute attr;
12166 gfc_namespace *ns;
12168 if (sym == NULL)
12170 /* Check if the current namespace or one of its parents
12171 belongs to a pure procedure. */
12172 for (ns = gfc_current_ns; ns; ns = ns->parent)
12174 sym = ns->proc_name;
12175 if (sym == NULL)
12176 return 0;
12177 attr = sym->attr;
12178 if (attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental))
12179 return 1;
12181 return 0;
12184 attr = sym->attr;
12186 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
12190 /* Test whether the current procedure is elemental or not. */
12193 gfc_elemental (gfc_symbol *sym)
12195 symbol_attribute attr;
12197 if (sym == NULL)
12198 sym = gfc_current_ns->proc_name;
12199 if (sym == NULL)
12200 return 0;
12201 attr = sym->attr;
12203 return attr.flavor == FL_PROCEDURE && attr.elemental;
12207 /* Warn about unused labels. */
12209 static void
12210 warn_unused_fortran_label (gfc_st_label *label)
12212 if (label == NULL)
12213 return;
12215 warn_unused_fortran_label (label->left);
12217 if (label->defined == ST_LABEL_UNKNOWN)
12218 return;
12220 switch (label->referenced)
12222 case ST_LABEL_UNKNOWN:
12223 gfc_warning ("Label %d at %L defined but not used", label->value,
12224 &label->where);
12225 break;
12227 case ST_LABEL_BAD_TARGET:
12228 gfc_warning ("Label %d at %L defined but cannot be used",
12229 label->value, &label->where);
12230 break;
12232 default:
12233 break;
12236 warn_unused_fortran_label (label->right);
12240 /* Returns the sequence type of a symbol or sequence. */
12242 static seq_type
12243 sequence_type (gfc_typespec ts)
12245 seq_type result;
12246 gfc_component *c;
12248 switch (ts.type)
12250 case BT_DERIVED:
12252 if (ts.u.derived->components == NULL)
12253 return SEQ_NONDEFAULT;
12255 result = sequence_type (ts.u.derived->components->ts);
12256 for (c = ts.u.derived->components->next; c; c = c->next)
12257 if (sequence_type (c->ts) != result)
12258 return SEQ_MIXED;
12260 return result;
12262 case BT_CHARACTER:
12263 if (ts.kind != gfc_default_character_kind)
12264 return SEQ_NONDEFAULT;
12266 return SEQ_CHARACTER;
12268 case BT_INTEGER:
12269 if (ts.kind != gfc_default_integer_kind)
12270 return SEQ_NONDEFAULT;
12272 return SEQ_NUMERIC;
12274 case BT_REAL:
12275 if (!(ts.kind == gfc_default_real_kind
12276 || ts.kind == gfc_default_double_kind))
12277 return SEQ_NONDEFAULT;
12279 return SEQ_NUMERIC;
12281 case BT_COMPLEX:
12282 if (ts.kind != gfc_default_complex_kind)
12283 return SEQ_NONDEFAULT;
12285 return SEQ_NUMERIC;
12287 case BT_LOGICAL:
12288 if (ts.kind != gfc_default_logical_kind)
12289 return SEQ_NONDEFAULT;
12291 return SEQ_NUMERIC;
12293 default:
12294 return SEQ_NONDEFAULT;
12299 /* Resolve derived type EQUIVALENCE object. */
12301 static gfc_try
12302 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
12304 gfc_component *c = derived->components;
12306 if (!derived)
12307 return SUCCESS;
12309 /* Shall not be an object of nonsequence derived type. */
12310 if (!derived->attr.sequence)
12312 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
12313 "attribute to be an EQUIVALENCE object", sym->name,
12314 &e->where);
12315 return FAILURE;
12318 /* Shall not have allocatable components. */
12319 if (derived->attr.alloc_comp)
12321 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
12322 "components to be an EQUIVALENCE object",sym->name,
12323 &e->where);
12324 return FAILURE;
12327 if (sym->attr.in_common && has_default_initializer (sym->ts.u.derived))
12329 gfc_error ("Derived type variable '%s' at %L with default "
12330 "initialization cannot be in EQUIVALENCE with a variable "
12331 "in COMMON", sym->name, &e->where);
12332 return FAILURE;
12335 for (; c ; c = c->next)
12337 if (c->ts.type == BT_DERIVED
12338 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
12339 return FAILURE;
12341 /* Shall not be an object of sequence derived type containing a pointer
12342 in the structure. */
12343 if (c->attr.pointer)
12345 gfc_error ("Derived type variable '%s' at %L with pointer "
12346 "component(s) cannot be an EQUIVALENCE object",
12347 sym->name, &e->where);
12348 return FAILURE;
12351 return SUCCESS;
12355 /* Resolve equivalence object.
12356 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
12357 an allocatable array, an object of nonsequence derived type, an object of
12358 sequence derived type containing a pointer at any level of component
12359 selection, an automatic object, a function name, an entry name, a result
12360 name, a named constant, a structure component, or a subobject of any of
12361 the preceding objects. A substring shall not have length zero. A
12362 derived type shall not have components with default initialization nor
12363 shall two objects of an equivalence group be initialized.
12364 Either all or none of the objects shall have an protected attribute.
12365 The simple constraints are done in symbol.c(check_conflict) and the rest
12366 are implemented here. */
12368 static void
12369 resolve_equivalence (gfc_equiv *eq)
12371 gfc_symbol *sym;
12372 gfc_symbol *first_sym;
12373 gfc_expr *e;
12374 gfc_ref *r;
12375 locus *last_where = NULL;
12376 seq_type eq_type, last_eq_type;
12377 gfc_typespec *last_ts;
12378 int object, cnt_protected;
12379 const char *msg;
12381 last_ts = &eq->expr->symtree->n.sym->ts;
12383 first_sym = eq->expr->symtree->n.sym;
12385 cnt_protected = 0;
12387 for (object = 1; eq; eq = eq->eq, object++)
12389 e = eq->expr;
12391 e->ts = e->symtree->n.sym->ts;
12392 /* match_varspec might not know yet if it is seeing
12393 array reference or substring reference, as it doesn't
12394 know the types. */
12395 if (e->ref && e->ref->type == REF_ARRAY)
12397 gfc_ref *ref = e->ref;
12398 sym = e->symtree->n.sym;
12400 if (sym->attr.dimension)
12402 ref->u.ar.as = sym->as;
12403 ref = ref->next;
12406 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
12407 if (e->ts.type == BT_CHARACTER
12408 && ref
12409 && ref->type == REF_ARRAY
12410 && ref->u.ar.dimen == 1
12411 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
12412 && ref->u.ar.stride[0] == NULL)
12414 gfc_expr *start = ref->u.ar.start[0];
12415 gfc_expr *end = ref->u.ar.end[0];
12416 void *mem = NULL;
12418 /* Optimize away the (:) reference. */
12419 if (start == NULL && end == NULL)
12421 if (e->ref == ref)
12422 e->ref = ref->next;
12423 else
12424 e->ref->next = ref->next;
12425 mem = ref;
12427 else
12429 ref->type = REF_SUBSTRING;
12430 if (start == NULL)
12431 start = gfc_get_int_expr (gfc_default_integer_kind,
12432 NULL, 1);
12433 ref->u.ss.start = start;
12434 if (end == NULL && e->ts.u.cl)
12435 end = gfc_copy_expr (e->ts.u.cl->length);
12436 ref->u.ss.end = end;
12437 ref->u.ss.length = e->ts.u.cl;
12438 e->ts.u.cl = NULL;
12440 ref = ref->next;
12441 gfc_free (mem);
12444 /* Any further ref is an error. */
12445 if (ref)
12447 gcc_assert (ref->type == REF_ARRAY);
12448 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
12449 &ref->u.ar.where);
12450 continue;
12454 if (gfc_resolve_expr (e) == FAILURE)
12455 continue;
12457 sym = e->symtree->n.sym;
12459 if (sym->attr.is_protected)
12460 cnt_protected++;
12461 if (cnt_protected > 0 && cnt_protected != object)
12463 gfc_error ("Either all or none of the objects in the "
12464 "EQUIVALENCE set at %L shall have the "
12465 "PROTECTED attribute",
12466 &e->where);
12467 break;
12470 /* Shall not equivalence common block variables in a PURE procedure. */
12471 if (sym->ns->proc_name
12472 && sym->ns->proc_name->attr.pure
12473 && sym->attr.in_common)
12475 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
12476 "object in the pure procedure '%s'",
12477 sym->name, &e->where, sym->ns->proc_name->name);
12478 break;
12481 /* Shall not be a named constant. */
12482 if (e->expr_type == EXPR_CONSTANT)
12484 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
12485 "object", sym->name, &e->where);
12486 continue;
12489 if (e->ts.type == BT_DERIVED
12490 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
12491 continue;
12493 /* Check that the types correspond correctly:
12494 Note 5.28:
12495 A numeric sequence structure may be equivalenced to another sequence
12496 structure, an object of default integer type, default real type, double
12497 precision real type, default logical type such that components of the
12498 structure ultimately only become associated to objects of the same
12499 kind. A character sequence structure may be equivalenced to an object
12500 of default character kind or another character sequence structure.
12501 Other objects may be equivalenced only to objects of the same type and
12502 kind parameters. */
12504 /* Identical types are unconditionally OK. */
12505 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
12506 goto identical_types;
12508 last_eq_type = sequence_type (*last_ts);
12509 eq_type = sequence_type (sym->ts);
12511 /* Since the pair of objects is not of the same type, mixed or
12512 non-default sequences can be rejected. */
12514 msg = "Sequence %s with mixed components in EQUIVALENCE "
12515 "statement at %L with different type objects";
12516 if ((object ==2
12517 && last_eq_type == SEQ_MIXED
12518 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
12519 == FAILURE)
12520 || (eq_type == SEQ_MIXED
12521 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12522 &e->where) == FAILURE))
12523 continue;
12525 msg = "Non-default type object or sequence %s in EQUIVALENCE "
12526 "statement at %L with objects of different type";
12527 if ((object ==2
12528 && last_eq_type == SEQ_NONDEFAULT
12529 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
12530 last_where) == FAILURE)
12531 || (eq_type == SEQ_NONDEFAULT
12532 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12533 &e->where) == FAILURE))
12534 continue;
12536 msg ="Non-CHARACTER object '%s' in default CHARACTER "
12537 "EQUIVALENCE statement at %L";
12538 if (last_eq_type == SEQ_CHARACTER
12539 && eq_type != SEQ_CHARACTER
12540 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12541 &e->where) == FAILURE)
12542 continue;
12544 msg ="Non-NUMERIC object '%s' in default NUMERIC "
12545 "EQUIVALENCE statement at %L";
12546 if (last_eq_type == SEQ_NUMERIC
12547 && eq_type != SEQ_NUMERIC
12548 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12549 &e->where) == FAILURE)
12550 continue;
12552 identical_types:
12553 last_ts =&sym->ts;
12554 last_where = &e->where;
12556 if (!e->ref)
12557 continue;
12559 /* Shall not be an automatic array. */
12560 if (e->ref->type == REF_ARRAY
12561 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
12563 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
12564 "an EQUIVALENCE object", sym->name, &e->where);
12565 continue;
12568 r = e->ref;
12569 while (r)
12571 /* Shall not be a structure component. */
12572 if (r->type == REF_COMPONENT)
12574 gfc_error ("Structure component '%s' at %L cannot be an "
12575 "EQUIVALENCE object",
12576 r->u.c.component->name, &e->where);
12577 break;
12580 /* A substring shall not have length zero. */
12581 if (r->type == REF_SUBSTRING)
12583 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
12585 gfc_error ("Substring at %L has length zero",
12586 &r->u.ss.start->where);
12587 break;
12590 r = r->next;
12596 /* Resolve function and ENTRY types, issue diagnostics if needed. */
12598 static void
12599 resolve_fntype (gfc_namespace *ns)
12601 gfc_entry_list *el;
12602 gfc_symbol *sym;
12604 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
12605 return;
12607 /* If there are any entries, ns->proc_name is the entry master
12608 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
12609 if (ns->entries)
12610 sym = ns->entries->sym;
12611 else
12612 sym = ns->proc_name;
12613 if (sym->result == sym
12614 && sym->ts.type == BT_UNKNOWN
12615 && gfc_set_default_type (sym, 0, NULL) == FAILURE
12616 && !sym->attr.untyped)
12618 gfc_error ("Function '%s' at %L has no IMPLICIT type",
12619 sym->name, &sym->declared_at);
12620 sym->attr.untyped = 1;
12623 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
12624 && !sym->attr.contained
12625 && !gfc_check_access (sym->ts.u.derived->attr.access,
12626 sym->ts.u.derived->ns->default_access)
12627 && gfc_check_access (sym->attr.access, sym->ns->default_access))
12629 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
12630 "%L of PRIVATE type '%s'", sym->name,
12631 &sym->declared_at, sym->ts.u.derived->name);
12634 if (ns->entries)
12635 for (el = ns->entries->next; el; el = el->next)
12637 if (el->sym->result == el->sym
12638 && el->sym->ts.type == BT_UNKNOWN
12639 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
12640 && !el->sym->attr.untyped)
12642 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
12643 el->sym->name, &el->sym->declared_at);
12644 el->sym->attr.untyped = 1;
12650 /* 12.3.2.1.1 Defined operators. */
12652 static gfc_try
12653 check_uop_procedure (gfc_symbol *sym, locus where)
12655 gfc_formal_arglist *formal;
12657 if (!sym->attr.function)
12659 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
12660 sym->name, &where);
12661 return FAILURE;
12664 if (sym->ts.type == BT_CHARACTER
12665 && !(sym->ts.u.cl && sym->ts.u.cl->length)
12666 && !(sym->result && sym->result->ts.u.cl
12667 && sym->result->ts.u.cl->length))
12669 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
12670 "character length", sym->name, &where);
12671 return FAILURE;
12674 formal = sym->formal;
12675 if (!formal || !formal->sym)
12677 gfc_error ("User operator procedure '%s' at %L must have at least "
12678 "one argument", sym->name, &where);
12679 return FAILURE;
12682 if (formal->sym->attr.intent != INTENT_IN)
12684 gfc_error ("First argument of operator interface at %L must be "
12685 "INTENT(IN)", &where);
12686 return FAILURE;
12689 if (formal->sym->attr.optional)
12691 gfc_error ("First argument of operator interface at %L cannot be "
12692 "optional", &where);
12693 return FAILURE;
12696 formal = formal->next;
12697 if (!formal || !formal->sym)
12698 return SUCCESS;
12700 if (formal->sym->attr.intent != INTENT_IN)
12702 gfc_error ("Second argument of operator interface at %L must be "
12703 "INTENT(IN)", &where);
12704 return FAILURE;
12707 if (formal->sym->attr.optional)
12709 gfc_error ("Second argument of operator interface at %L cannot be "
12710 "optional", &where);
12711 return FAILURE;
12714 if (formal->next)
12716 gfc_error ("Operator interface at %L must have, at most, two "
12717 "arguments", &where);
12718 return FAILURE;
12721 return SUCCESS;
12724 static void
12725 gfc_resolve_uops (gfc_symtree *symtree)
12727 gfc_interface *itr;
12729 if (symtree == NULL)
12730 return;
12732 gfc_resolve_uops (symtree->left);
12733 gfc_resolve_uops (symtree->right);
12735 for (itr = symtree->n.uop->op; itr; itr = itr->next)
12736 check_uop_procedure (itr->sym, itr->sym->declared_at);
12740 /* Examine all of the expressions associated with a program unit,
12741 assign types to all intermediate expressions, make sure that all
12742 assignments are to compatible types and figure out which names
12743 refer to which functions or subroutines. It doesn't check code
12744 block, which is handled by resolve_code. */
12746 static void
12747 resolve_types (gfc_namespace *ns)
12749 gfc_namespace *n;
12750 gfc_charlen *cl;
12751 gfc_data *d;
12752 gfc_equiv *eq;
12753 gfc_namespace* old_ns = gfc_current_ns;
12755 /* Check that all IMPLICIT types are ok. */
12756 if (!ns->seen_implicit_none)
12758 unsigned letter;
12759 for (letter = 0; letter != GFC_LETTERS; ++letter)
12760 if (ns->set_flag[letter]
12761 && resolve_typespec_used (&ns->default_type[letter],
12762 &ns->implicit_loc[letter],
12763 NULL) == FAILURE)
12764 return;
12767 gfc_current_ns = ns;
12769 resolve_entries (ns);
12771 resolve_common_vars (ns->blank_common.head, false);
12772 resolve_common_blocks (ns->common_root);
12774 resolve_contained_functions (ns);
12776 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
12778 for (cl = ns->cl_list; cl; cl = cl->next)
12779 resolve_charlen (cl);
12781 gfc_traverse_ns (ns, resolve_symbol);
12783 resolve_fntype (ns);
12785 for (n = ns->contained; n; n = n->sibling)
12787 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
12788 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
12789 "also be PURE", n->proc_name->name,
12790 &n->proc_name->declared_at);
12792 resolve_types (n);
12795 forall_flag = 0;
12796 gfc_check_interfaces (ns);
12798 gfc_traverse_ns (ns, resolve_values);
12800 if (ns->save_all)
12801 gfc_save_all (ns);
12803 iter_stack = NULL;
12804 for (d = ns->data; d; d = d->next)
12805 resolve_data (d);
12807 iter_stack = NULL;
12808 gfc_traverse_ns (ns, gfc_formalize_init_value);
12810 gfc_traverse_ns (ns, gfc_verify_binding_labels);
12812 if (ns->common_root != NULL)
12813 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
12815 for (eq = ns->equiv; eq; eq = eq->next)
12816 resolve_equivalence (eq);
12818 /* Warn about unused labels. */
12819 if (warn_unused_label)
12820 warn_unused_fortran_label (ns->st_labels);
12822 gfc_resolve_uops (ns->uop_root);
12824 gfc_current_ns = old_ns;
12828 /* Call resolve_code recursively. */
12830 static void
12831 resolve_codes (gfc_namespace *ns)
12833 gfc_namespace *n;
12834 bitmap_obstack old_obstack;
12836 for (n = ns->contained; n; n = n->sibling)
12837 resolve_codes (n);
12839 gfc_current_ns = ns;
12841 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
12842 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
12843 cs_base = NULL;
12845 /* Set to an out of range value. */
12846 current_entry_id = -1;
12848 old_obstack = labels_obstack;
12849 bitmap_obstack_initialize (&labels_obstack);
12851 resolve_code (ns->code, ns);
12853 bitmap_obstack_release (&labels_obstack);
12854 labels_obstack = old_obstack;
12858 /* This function is called after a complete program unit has been compiled.
12859 Its purpose is to examine all of the expressions associated with a program
12860 unit, assign types to all intermediate expressions, make sure that all
12861 assignments are to compatible types and figure out which names refer to
12862 which functions or subroutines. */
12864 void
12865 gfc_resolve (gfc_namespace *ns)
12867 gfc_namespace *old_ns;
12868 code_stack *old_cs_base;
12870 if (ns->resolved)
12871 return;
12873 ns->resolved = -1;
12874 old_ns = gfc_current_ns;
12875 old_cs_base = cs_base;
12877 resolve_types (ns);
12878 resolve_codes (ns);
12880 gfc_current_ns = old_ns;
12881 cs_base = old_cs_base;
12882 ns->resolved = 1;