2010-07-19 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / resolve.c
blob95dbeee43b2478eaea21cb8c795a8ec1e349b0ae
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 /* Resolve common variables. */
707 static void
708 resolve_common_vars (gfc_symbol *sym, bool named_common)
710 gfc_symbol *csym = sym;
712 for (; csym; csym = csym->common_next)
714 if (csym->value || csym->attr.data)
716 if (!csym->ns->is_block_data)
717 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
718 "but only in BLOCK DATA initialization is "
719 "allowed", csym->name, &csym->declared_at);
720 else if (!named_common)
721 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
722 "in a blank COMMON but initialization is only "
723 "allowed in named common blocks", csym->name,
724 &csym->declared_at);
727 if (csym->ts.type != BT_DERIVED)
728 continue;
730 if (!(csym->ts.u.derived->attr.sequence
731 || csym->ts.u.derived->attr.is_bind_c))
732 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
733 "has neither the SEQUENCE nor the BIND(C) "
734 "attribute", csym->name, &csym->declared_at);
735 if (csym->ts.u.derived->attr.alloc_comp)
736 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
737 "has an ultimate component that is "
738 "allocatable", csym->name, &csym->declared_at);
739 if (gfc_has_default_initializer (csym->ts.u.derived))
740 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
741 "may not have default initializer", csym->name,
742 &csym->declared_at);
744 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
745 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
749 /* Resolve common blocks. */
750 static void
751 resolve_common_blocks (gfc_symtree *common_root)
753 gfc_symbol *sym;
755 if (common_root == NULL)
756 return;
758 if (common_root->left)
759 resolve_common_blocks (common_root->left);
760 if (common_root->right)
761 resolve_common_blocks (common_root->right);
763 resolve_common_vars (common_root->n.common->head, true);
765 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
766 if (sym == NULL)
767 return;
769 if (sym->attr.flavor == FL_PARAMETER)
770 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
771 sym->name, &common_root->n.common->where, &sym->declared_at);
773 if (sym->attr.intrinsic)
774 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
775 sym->name, &common_root->n.common->where);
776 else if (sym->attr.result
777 || gfc_is_function_return_value (sym, gfc_current_ns))
778 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
779 "that is also a function result", sym->name,
780 &common_root->n.common->where);
781 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
782 && sym->attr.proc != PROC_ST_FUNCTION)
783 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
784 "that is also a global procedure", sym->name,
785 &common_root->n.common->where);
789 /* Resolve contained function types. Because contained functions can call one
790 another, they have to be worked out before any of the contained procedures
791 can be resolved.
793 The good news is that if a function doesn't already have a type, the only
794 way it can get one is through an IMPLICIT type or a RESULT variable, because
795 by definition contained functions are contained namespace they're contained
796 in, not in a sibling or parent namespace. */
798 static void
799 resolve_contained_functions (gfc_namespace *ns)
801 gfc_namespace *child;
802 gfc_entry_list *el;
804 resolve_formal_arglists (ns);
806 for (child = ns->contained; child; child = child->sibling)
808 /* Resolve alternate entry points first. */
809 resolve_entries (child);
811 /* Then check function return types. */
812 resolve_contained_fntype (child->proc_name, child);
813 for (el = child->entries; el; el = el->next)
814 resolve_contained_fntype (el->sym, child);
819 /* Resolve all of the elements of a structure constructor and make sure that
820 the types are correct. */
822 static gfc_try
823 resolve_structure_cons (gfc_expr *expr)
825 gfc_constructor *cons;
826 gfc_component *comp;
827 gfc_try t;
828 symbol_attribute a;
830 t = SUCCESS;
831 cons = gfc_constructor_first (expr->value.constructor);
832 /* A constructor may have references if it is the result of substituting a
833 parameter variable. In this case we just pull out the component we
834 want. */
835 if (expr->ref)
836 comp = expr->ref->u.c.sym->components;
837 else
838 comp = expr->ts.u.derived->components;
840 /* See if the user is trying to invoke a structure constructor for one of
841 the iso_c_binding derived types. */
842 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
843 && expr->ts.u.derived->ts.is_iso_c && cons
844 && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
846 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
847 expr->ts.u.derived->name, &(expr->where));
848 return FAILURE;
851 /* Return if structure constructor is c_null_(fun)prt. */
852 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
853 && expr->ts.u.derived->ts.is_iso_c && cons
854 && cons->expr && cons->expr->expr_type == EXPR_NULL)
855 return SUCCESS;
857 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
859 int rank;
861 if (!cons->expr)
862 continue;
864 if (gfc_resolve_expr (cons->expr) == FAILURE)
866 t = FAILURE;
867 continue;
870 rank = comp->as ? comp->as->rank : 0;
871 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
872 && (comp->attr.allocatable || cons->expr->rank))
874 gfc_error ("The rank of the element in the derived type "
875 "constructor at %L does not match that of the "
876 "component (%d/%d)", &cons->expr->where,
877 cons->expr->rank, rank);
878 t = FAILURE;
881 /* If we don't have the right type, try to convert it. */
883 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
885 t = FAILURE;
886 if (strcmp (comp->name, "$extends") == 0)
888 /* Can afford to be brutal with the $extends initializer.
889 The derived type can get lost because it is PRIVATE
890 but it is not usage constrained by the standard. */
891 cons->expr->ts = comp->ts;
892 t = SUCCESS;
894 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
895 gfc_error ("The element in the derived type constructor at %L, "
896 "for pointer component '%s', is %s but should be %s",
897 &cons->expr->where, comp->name,
898 gfc_basic_typename (cons->expr->ts.type),
899 gfc_basic_typename (comp->ts.type));
900 else
901 t = gfc_convert_type (cons->expr, &comp->ts, 1);
904 if (cons->expr->expr_type == EXPR_NULL
905 && !(comp->attr.pointer || comp->attr.allocatable
906 || comp->attr.proc_pointer
907 || (comp->ts.type == BT_CLASS
908 && (CLASS_DATA (comp)->attr.class_pointer
909 || CLASS_DATA (comp)->attr.allocatable))))
911 t = FAILURE;
912 gfc_error ("The NULL in the derived type constructor at %L is "
913 "being applied to component '%s', which is neither "
914 "a POINTER nor ALLOCATABLE", &cons->expr->where,
915 comp->name);
918 if (!comp->attr.pointer || cons->expr->expr_type == EXPR_NULL)
919 continue;
921 a = gfc_expr_attr (cons->expr);
923 if (!a.pointer && !a.target)
925 t = FAILURE;
926 gfc_error ("The element in the derived type constructor at %L, "
927 "for pointer component '%s' should be a POINTER or "
928 "a TARGET", &cons->expr->where, comp->name);
931 /* F2003, C1272 (3). */
932 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
933 && (gfc_impure_variable (cons->expr->symtree->n.sym)
934 || gfc_is_coindexed (cons->expr)))
936 t = FAILURE;
937 gfc_error ("Invalid expression in the derived type constructor for "
938 "pointer component '%s' at %L in PURE procedure",
939 comp->name, &cons->expr->where);
943 return t;
947 /****************** Expression name resolution ******************/
949 /* Returns 0 if a symbol was not declared with a type or
950 attribute declaration statement, nonzero otherwise. */
952 static int
953 was_declared (gfc_symbol *sym)
955 symbol_attribute a;
957 a = sym->attr;
959 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
960 return 1;
962 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
963 || a.optional || a.pointer || a.save || a.target || a.volatile_
964 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
965 || a.asynchronous || a.codimension)
966 return 1;
968 return 0;
972 /* Determine if a symbol is generic or not. */
974 static int
975 generic_sym (gfc_symbol *sym)
977 gfc_symbol *s;
979 if (sym->attr.generic ||
980 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
981 return 1;
983 if (was_declared (sym) || sym->ns->parent == NULL)
984 return 0;
986 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
988 if (s != NULL)
990 if (s == sym)
991 return 0;
992 else
993 return generic_sym (s);
996 return 0;
1000 /* Determine if a symbol is specific or not. */
1002 static int
1003 specific_sym (gfc_symbol *sym)
1005 gfc_symbol *s;
1007 if (sym->attr.if_source == IFSRC_IFBODY
1008 || sym->attr.proc == PROC_MODULE
1009 || sym->attr.proc == PROC_INTERNAL
1010 || sym->attr.proc == PROC_ST_FUNCTION
1011 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1012 || sym->attr.external)
1013 return 1;
1015 if (was_declared (sym) || sym->ns->parent == NULL)
1016 return 0;
1018 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1020 return (s == NULL) ? 0 : specific_sym (s);
1024 /* Figure out if the procedure is specific, generic or unknown. */
1026 typedef enum
1027 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1028 proc_type;
1030 static proc_type
1031 procedure_kind (gfc_symbol *sym)
1033 if (generic_sym (sym))
1034 return PTYPE_GENERIC;
1036 if (specific_sym (sym))
1037 return PTYPE_SPECIFIC;
1039 return PTYPE_UNKNOWN;
1042 /* Check references to assumed size arrays. The flag need_full_assumed_size
1043 is nonzero when matching actual arguments. */
1045 static int need_full_assumed_size = 0;
1047 static bool
1048 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1050 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1051 return false;
1053 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1054 What should it be? */
1055 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1056 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1057 && (e->ref->u.ar.type == AR_FULL))
1059 gfc_error ("The upper bound in the last dimension must "
1060 "appear in the reference to the assumed size "
1061 "array '%s' at %L", sym->name, &e->where);
1062 return true;
1064 return false;
1068 /* Look for bad assumed size array references in argument expressions
1069 of elemental and array valued intrinsic procedures. Since this is
1070 called from procedure resolution functions, it only recurses at
1071 operators. */
1073 static bool
1074 resolve_assumed_size_actual (gfc_expr *e)
1076 if (e == NULL)
1077 return false;
1079 switch (e->expr_type)
1081 case EXPR_VARIABLE:
1082 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1083 return true;
1084 break;
1086 case EXPR_OP:
1087 if (resolve_assumed_size_actual (e->value.op.op1)
1088 || resolve_assumed_size_actual (e->value.op.op2))
1089 return true;
1090 break;
1092 default:
1093 break;
1095 return false;
1099 /* Check a generic procedure, passed as an actual argument, to see if
1100 there is a matching specific name. If none, it is an error, and if
1101 more than one, the reference is ambiguous. */
1102 static int
1103 count_specific_procs (gfc_expr *e)
1105 int n;
1106 gfc_interface *p;
1107 gfc_symbol *sym;
1109 n = 0;
1110 sym = e->symtree->n.sym;
1112 for (p = sym->generic; p; p = p->next)
1113 if (strcmp (sym->name, p->sym->name) == 0)
1115 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1116 sym->name);
1117 n++;
1120 if (n > 1)
1121 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1122 &e->where);
1124 if (n == 0)
1125 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1126 "argument at %L", sym->name, &e->where);
1128 return n;
1132 /* See if a call to sym could possibly be a not allowed RECURSION because of
1133 a missing RECURIVE declaration. This means that either sym is the current
1134 context itself, or sym is the parent of a contained procedure calling its
1135 non-RECURSIVE containing procedure.
1136 This also works if sym is an ENTRY. */
1138 static bool
1139 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1141 gfc_symbol* proc_sym;
1142 gfc_symbol* context_proc;
1143 gfc_namespace* real_context;
1145 if (sym->attr.flavor == FL_PROGRAM)
1146 return false;
1148 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1150 /* If we've got an ENTRY, find real procedure. */
1151 if (sym->attr.entry && sym->ns->entries)
1152 proc_sym = sym->ns->entries->sym;
1153 else
1154 proc_sym = sym;
1156 /* If sym is RECURSIVE, all is well of course. */
1157 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1158 return false;
1160 /* Find the context procedure's "real" symbol if it has entries.
1161 We look for a procedure symbol, so recurse on the parents if we don't
1162 find one (like in case of a BLOCK construct). */
1163 for (real_context = context; ; real_context = real_context->parent)
1165 /* We should find something, eventually! */
1166 gcc_assert (real_context);
1168 context_proc = (real_context->entries ? real_context->entries->sym
1169 : real_context->proc_name);
1171 /* In some special cases, there may not be a proc_name, like for this
1172 invalid code:
1173 real(bad_kind()) function foo () ...
1174 when checking the call to bad_kind ().
1175 In these cases, we simply return here and assume that the
1176 call is ok. */
1177 if (!context_proc)
1178 return false;
1180 if (context_proc->attr.flavor != FL_LABEL)
1181 break;
1184 /* A call from sym's body to itself is recursion, of course. */
1185 if (context_proc == proc_sym)
1186 return true;
1188 /* The same is true if context is a contained procedure and sym the
1189 containing one. */
1190 if (context_proc->attr.contained)
1192 gfc_symbol* parent_proc;
1194 gcc_assert (context->parent);
1195 parent_proc = (context->parent->entries ? context->parent->entries->sym
1196 : context->parent->proc_name);
1198 if (parent_proc == proc_sym)
1199 return true;
1202 return false;
1206 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1207 its typespec and formal argument list. */
1209 static gfc_try
1210 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1212 gfc_intrinsic_sym* isym;
1213 const char* symstd;
1215 if (sym->formal)
1216 return SUCCESS;
1218 /* We already know this one is an intrinsic, so we don't call
1219 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1220 gfc_find_subroutine directly to check whether it is a function or
1221 subroutine. */
1223 if ((isym = gfc_find_function (sym->name)))
1225 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1226 && !sym->attr.implicit_type)
1227 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1228 " ignored", sym->name, &sym->declared_at);
1230 if (!sym->attr.function &&
1231 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1232 return FAILURE;
1234 sym->ts = isym->ts;
1236 else if ((isym = gfc_find_subroutine (sym->name)))
1238 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1240 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1241 " specifier", sym->name, &sym->declared_at);
1242 return FAILURE;
1245 if (!sym->attr.subroutine &&
1246 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1247 return FAILURE;
1249 else
1251 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1252 &sym->declared_at);
1253 return FAILURE;
1256 gfc_copy_formal_args_intr (sym, isym);
1258 /* Check it is actually available in the standard settings. */
1259 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1260 == FAILURE)
1262 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1263 " available in the current standard settings but %s. Use"
1264 " an appropriate -std=* option or enable -fall-intrinsics"
1265 " in order to use it.",
1266 sym->name, &sym->declared_at, symstd);
1267 return FAILURE;
1270 return SUCCESS;
1274 /* Resolve a procedure expression, like passing it to a called procedure or as
1275 RHS for a procedure pointer assignment. */
1277 static gfc_try
1278 resolve_procedure_expression (gfc_expr* expr)
1280 gfc_symbol* sym;
1282 if (expr->expr_type != EXPR_VARIABLE)
1283 return SUCCESS;
1284 gcc_assert (expr->symtree);
1286 sym = expr->symtree->n.sym;
1288 if (sym->attr.intrinsic)
1289 resolve_intrinsic (sym, &expr->where);
1291 if (sym->attr.flavor != FL_PROCEDURE
1292 || (sym->attr.function && sym->result == sym))
1293 return SUCCESS;
1295 /* A non-RECURSIVE procedure that is used as procedure expression within its
1296 own body is in danger of being called recursively. */
1297 if (is_illegal_recursion (sym, gfc_current_ns))
1298 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1299 " itself recursively. Declare it RECURSIVE or use"
1300 " -frecursive", sym->name, &expr->where);
1302 return SUCCESS;
1306 /* Resolve an actual argument list. Most of the time, this is just
1307 resolving the expressions in the list.
1308 The exception is that we sometimes have to decide whether arguments
1309 that look like procedure arguments are really simple variable
1310 references. */
1312 static gfc_try
1313 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1314 bool no_formal_args)
1316 gfc_symbol *sym;
1317 gfc_symtree *parent_st;
1318 gfc_expr *e;
1319 int save_need_full_assumed_size;
1320 gfc_component *comp;
1322 for (; arg; arg = arg->next)
1324 e = arg->expr;
1325 if (e == NULL)
1327 /* Check the label is a valid branching target. */
1328 if (arg->label)
1330 if (arg->label->defined == ST_LABEL_UNKNOWN)
1332 gfc_error ("Label %d referenced at %L is never defined",
1333 arg->label->value, &arg->label->where);
1334 return FAILURE;
1337 continue;
1340 if (gfc_is_proc_ptr_comp (e, &comp))
1342 e->ts = comp->ts;
1343 if (e->expr_type == EXPR_PPC)
1345 if (comp->as != NULL)
1346 e->rank = comp->as->rank;
1347 e->expr_type = EXPR_FUNCTION;
1349 if (gfc_resolve_expr (e) == FAILURE)
1350 return FAILURE;
1351 goto argument_list;
1354 if (e->expr_type == EXPR_VARIABLE
1355 && e->symtree->n.sym->attr.generic
1356 && no_formal_args
1357 && count_specific_procs (e) != 1)
1358 return FAILURE;
1360 if (e->ts.type != BT_PROCEDURE)
1362 save_need_full_assumed_size = need_full_assumed_size;
1363 if (e->expr_type != EXPR_VARIABLE)
1364 need_full_assumed_size = 0;
1365 if (gfc_resolve_expr (e) != SUCCESS)
1366 return FAILURE;
1367 need_full_assumed_size = save_need_full_assumed_size;
1368 goto argument_list;
1371 /* See if the expression node should really be a variable reference. */
1373 sym = e->symtree->n.sym;
1375 if (sym->attr.flavor == FL_PROCEDURE
1376 || sym->attr.intrinsic
1377 || sym->attr.external)
1379 int actual_ok;
1381 /* If a procedure is not already determined to be something else
1382 check if it is intrinsic. */
1383 if (!sym->attr.intrinsic
1384 && !(sym->attr.external || sym->attr.use_assoc
1385 || sym->attr.if_source == IFSRC_IFBODY)
1386 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1387 sym->attr.intrinsic = 1;
1389 if (sym->attr.proc == PROC_ST_FUNCTION)
1391 gfc_error ("Statement function '%s' at %L is not allowed as an "
1392 "actual argument", sym->name, &e->where);
1395 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1396 sym->attr.subroutine);
1397 if (sym->attr.intrinsic && actual_ok == 0)
1399 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1400 "actual argument", sym->name, &e->where);
1403 if (sym->attr.contained && !sym->attr.use_assoc
1404 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1406 gfc_error ("Internal procedure '%s' is not allowed as an "
1407 "actual argument at %L", sym->name, &e->where);
1410 if (sym->attr.elemental && !sym->attr.intrinsic)
1412 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1413 "allowed as an actual argument at %L", sym->name,
1414 &e->where);
1417 /* Check if a generic interface has a specific procedure
1418 with the same name before emitting an error. */
1419 if (sym->attr.generic && count_specific_procs (e) != 1)
1420 return FAILURE;
1422 /* Just in case a specific was found for the expression. */
1423 sym = e->symtree->n.sym;
1425 /* If the symbol is the function that names the current (or
1426 parent) scope, then we really have a variable reference. */
1428 if (gfc_is_function_return_value (sym, sym->ns))
1429 goto got_variable;
1431 /* If all else fails, see if we have a specific intrinsic. */
1432 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1434 gfc_intrinsic_sym *isym;
1436 isym = gfc_find_function (sym->name);
1437 if (isym == NULL || !isym->specific)
1439 gfc_error ("Unable to find a specific INTRINSIC procedure "
1440 "for the reference '%s' at %L", sym->name,
1441 &e->where);
1442 return FAILURE;
1444 sym->ts = isym->ts;
1445 sym->attr.intrinsic = 1;
1446 sym->attr.function = 1;
1449 if (gfc_resolve_expr (e) == FAILURE)
1450 return FAILURE;
1451 goto argument_list;
1454 /* See if the name is a module procedure in a parent unit. */
1456 if (was_declared (sym) || sym->ns->parent == NULL)
1457 goto got_variable;
1459 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1461 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1462 return FAILURE;
1465 if (parent_st == NULL)
1466 goto got_variable;
1468 sym = parent_st->n.sym;
1469 e->symtree = parent_st; /* Point to the right thing. */
1471 if (sym->attr.flavor == FL_PROCEDURE
1472 || sym->attr.intrinsic
1473 || sym->attr.external)
1475 if (gfc_resolve_expr (e) == FAILURE)
1476 return FAILURE;
1477 goto argument_list;
1480 got_variable:
1481 e->expr_type = EXPR_VARIABLE;
1482 e->ts = sym->ts;
1483 if (sym->as != NULL)
1485 e->rank = sym->as->rank;
1486 e->ref = gfc_get_ref ();
1487 e->ref->type = REF_ARRAY;
1488 e->ref->u.ar.type = AR_FULL;
1489 e->ref->u.ar.as = sym->as;
1492 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1493 primary.c (match_actual_arg). If above code determines that it
1494 is a variable instead, it needs to be resolved as it was not
1495 done at the beginning of this function. */
1496 save_need_full_assumed_size = need_full_assumed_size;
1497 if (e->expr_type != EXPR_VARIABLE)
1498 need_full_assumed_size = 0;
1499 if (gfc_resolve_expr (e) != SUCCESS)
1500 return FAILURE;
1501 need_full_assumed_size = save_need_full_assumed_size;
1503 argument_list:
1504 /* Check argument list functions %VAL, %LOC and %REF. There is
1505 nothing to do for %REF. */
1506 if (arg->name && arg->name[0] == '%')
1508 if (strncmp ("%VAL", arg->name, 4) == 0)
1510 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1512 gfc_error ("By-value argument at %L is not of numeric "
1513 "type", &e->where);
1514 return FAILURE;
1517 if (e->rank)
1519 gfc_error ("By-value argument at %L cannot be an array or "
1520 "an array section", &e->where);
1521 return FAILURE;
1524 /* Intrinsics are still PROC_UNKNOWN here. However,
1525 since same file external procedures are not resolvable
1526 in gfortran, it is a good deal easier to leave them to
1527 intrinsic.c. */
1528 if (ptype != PROC_UNKNOWN
1529 && ptype != PROC_DUMMY
1530 && ptype != PROC_EXTERNAL
1531 && ptype != PROC_MODULE)
1533 gfc_error ("By-value argument at %L is not allowed "
1534 "in this context", &e->where);
1535 return FAILURE;
1539 /* Statement functions have already been excluded above. */
1540 else if (strncmp ("%LOC", arg->name, 4) == 0
1541 && e->ts.type == BT_PROCEDURE)
1543 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1545 gfc_error ("Passing internal procedure at %L by location "
1546 "not allowed", &e->where);
1547 return FAILURE;
1552 /* Fortran 2008, C1237. */
1553 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1554 && gfc_has_ultimate_pointer (e))
1556 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1557 "component", &e->where);
1558 return FAILURE;
1562 return SUCCESS;
1566 /* Do the checks of the actual argument list that are specific to elemental
1567 procedures. If called with c == NULL, we have a function, otherwise if
1568 expr == NULL, we have a subroutine. */
1570 static gfc_try
1571 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1573 gfc_actual_arglist *arg0;
1574 gfc_actual_arglist *arg;
1575 gfc_symbol *esym = NULL;
1576 gfc_intrinsic_sym *isym = NULL;
1577 gfc_expr *e = NULL;
1578 gfc_intrinsic_arg *iformal = NULL;
1579 gfc_formal_arglist *eformal = NULL;
1580 bool formal_optional = false;
1581 bool set_by_optional = false;
1582 int i;
1583 int rank = 0;
1585 /* Is this an elemental procedure? */
1586 if (expr && expr->value.function.actual != NULL)
1588 if (expr->value.function.esym != NULL
1589 && expr->value.function.esym->attr.elemental)
1591 arg0 = expr->value.function.actual;
1592 esym = expr->value.function.esym;
1594 else if (expr->value.function.isym != NULL
1595 && expr->value.function.isym->elemental)
1597 arg0 = expr->value.function.actual;
1598 isym = expr->value.function.isym;
1600 else
1601 return SUCCESS;
1603 else if (c && c->ext.actual != NULL)
1605 arg0 = c->ext.actual;
1607 if (c->resolved_sym)
1608 esym = c->resolved_sym;
1609 else
1610 esym = c->symtree->n.sym;
1611 gcc_assert (esym);
1613 if (!esym->attr.elemental)
1614 return SUCCESS;
1616 else
1617 return SUCCESS;
1619 /* The rank of an elemental is the rank of its array argument(s). */
1620 for (arg = arg0; arg; arg = arg->next)
1622 if (arg->expr != NULL && arg->expr->rank > 0)
1624 rank = arg->expr->rank;
1625 if (arg->expr->expr_type == EXPR_VARIABLE
1626 && arg->expr->symtree->n.sym->attr.optional)
1627 set_by_optional = true;
1629 /* Function specific; set the result rank and shape. */
1630 if (expr)
1632 expr->rank = rank;
1633 if (!expr->shape && arg->expr->shape)
1635 expr->shape = gfc_get_shape (rank);
1636 for (i = 0; i < rank; i++)
1637 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1640 break;
1644 /* If it is an array, it shall not be supplied as an actual argument
1645 to an elemental procedure unless an array of the same rank is supplied
1646 as an actual argument corresponding to a nonoptional dummy argument of
1647 that elemental procedure(12.4.1.5). */
1648 formal_optional = false;
1649 if (isym)
1650 iformal = isym->formal;
1651 else
1652 eformal = esym->formal;
1654 for (arg = arg0; arg; arg = arg->next)
1656 if (eformal)
1658 if (eformal->sym && eformal->sym->attr.optional)
1659 formal_optional = true;
1660 eformal = eformal->next;
1662 else if (isym && iformal)
1664 if (iformal->optional)
1665 formal_optional = true;
1666 iformal = iformal->next;
1668 else if (isym)
1669 formal_optional = true;
1671 if (pedantic && arg->expr != NULL
1672 && arg->expr->expr_type == EXPR_VARIABLE
1673 && arg->expr->symtree->n.sym->attr.optional
1674 && formal_optional
1675 && arg->expr->rank
1676 && (set_by_optional || arg->expr->rank != rank)
1677 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1679 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1680 "MISSING, it cannot be the actual argument of an "
1681 "ELEMENTAL procedure unless there is a non-optional "
1682 "argument with the same rank (12.4.1.5)",
1683 arg->expr->symtree->n.sym->name, &arg->expr->where);
1684 return FAILURE;
1688 for (arg = arg0; arg; arg = arg->next)
1690 if (arg->expr == NULL || arg->expr->rank == 0)
1691 continue;
1693 /* Being elemental, the last upper bound of an assumed size array
1694 argument must be present. */
1695 if (resolve_assumed_size_actual (arg->expr))
1696 return FAILURE;
1698 /* Elemental procedure's array actual arguments must conform. */
1699 if (e != NULL)
1701 if (gfc_check_conformance (arg->expr, e,
1702 "elemental procedure") == FAILURE)
1703 return FAILURE;
1705 else
1706 e = arg->expr;
1709 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1710 is an array, the intent inout/out variable needs to be also an array. */
1711 if (rank > 0 && esym && expr == NULL)
1712 for (eformal = esym->formal, arg = arg0; arg && eformal;
1713 arg = arg->next, eformal = eformal->next)
1714 if ((eformal->sym->attr.intent == INTENT_OUT
1715 || eformal->sym->attr.intent == INTENT_INOUT)
1716 && arg->expr && arg->expr->rank == 0)
1718 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1719 "ELEMENTAL subroutine '%s' is a scalar, but another "
1720 "actual argument is an array", &arg->expr->where,
1721 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1722 : "INOUT", eformal->sym->name, esym->name);
1723 return FAILURE;
1725 return SUCCESS;
1729 /* Go through each actual argument in ACTUAL and see if it can be
1730 implemented as an inlined, non-copying intrinsic. FNSYM is the
1731 function being called, or NULL if not known. */
1733 static void
1734 find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1736 gfc_actual_arglist *ap;
1737 gfc_expr *expr;
1739 for (ap = actual; ap; ap = ap->next)
1740 if (ap->expr
1741 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1742 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1743 NOT_ELEMENTAL))
1744 ap->expr->inline_noncopying_intrinsic = 1;
1748 /* This function does the checking of references to global procedures
1749 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1750 77 and 95 standards. It checks for a gsymbol for the name, making
1751 one if it does not already exist. If it already exists, then the
1752 reference being resolved must correspond to the type of gsymbol.
1753 Otherwise, the new symbol is equipped with the attributes of the
1754 reference. The corresponding code that is called in creating
1755 global entities is parse.c.
1757 In addition, for all but -std=legacy, the gsymbols are used to
1758 check the interfaces of external procedures from the same file.
1759 The namespace of the gsymbol is resolved and then, once this is
1760 done the interface is checked. */
1763 static bool
1764 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1766 if (!gsym_ns->proc_name->attr.recursive)
1767 return true;
1769 if (sym->ns == gsym_ns)
1770 return false;
1772 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1773 return false;
1775 return true;
1778 static bool
1779 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1781 if (gsym_ns->entries)
1783 gfc_entry_list *entry = gsym_ns->entries;
1785 for (; entry; entry = entry->next)
1787 if (strcmp (sym->name, entry->sym->name) == 0)
1789 if (strcmp (gsym_ns->proc_name->name,
1790 sym->ns->proc_name->name) == 0)
1791 return false;
1793 if (sym->ns->parent
1794 && strcmp (gsym_ns->proc_name->name,
1795 sym->ns->parent->proc_name->name) == 0)
1796 return false;
1800 return true;
1803 static void
1804 resolve_global_procedure (gfc_symbol *sym, locus *where,
1805 gfc_actual_arglist **actual, int sub)
1807 gfc_gsymbol * gsym;
1808 gfc_namespace *ns;
1809 enum gfc_symbol_type type;
1811 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1813 gsym = gfc_get_gsymbol (sym->name);
1815 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1816 gfc_global_used (gsym, where);
1818 if (gfc_option.flag_whole_file
1819 && sym->attr.if_source == IFSRC_UNKNOWN
1820 && gsym->type != GSYM_UNKNOWN
1821 && gsym->ns
1822 && gsym->ns->resolved != -1
1823 && gsym->ns->proc_name
1824 && not_in_recursive (sym, gsym->ns)
1825 && not_entry_self_reference (sym, gsym->ns))
1827 /* Resolve the gsymbol namespace if needed. */
1828 if (!gsym->ns->resolved)
1830 gfc_dt_list *old_dt_list;
1832 /* Stash away derived types so that the backend_decls do not
1833 get mixed up. */
1834 old_dt_list = gfc_derived_types;
1835 gfc_derived_types = NULL;
1837 gfc_resolve (gsym->ns);
1839 /* Store the new derived types with the global namespace. */
1840 if (gfc_derived_types)
1841 gsym->ns->derived_types = gfc_derived_types;
1843 /* Restore the derived types of this namespace. */
1844 gfc_derived_types = old_dt_list;
1847 /* Make sure that translation for the gsymbol occurs before
1848 the procedure currently being resolved. */
1849 ns = gfc_global_ns_list;
1850 for (; ns && ns != gsym->ns; ns = ns->sibling)
1852 if (ns->sibling == gsym->ns)
1854 ns->sibling = gsym->ns->sibling;
1855 gsym->ns->sibling = gfc_global_ns_list;
1856 gfc_global_ns_list = gsym->ns;
1857 break;
1861 /* Differences in constant character lengths. */
1862 if (sym->attr.function && sym->ts.type == BT_CHARACTER)
1864 long int l1 = 0, l2 = 0;
1865 gfc_charlen *cl1 = sym->ts.u.cl;
1866 gfc_charlen *cl2 = gsym->ns->proc_name->ts.u.cl;
1868 if (cl1 != NULL
1869 && cl1->length != NULL
1870 && cl1->length->expr_type == EXPR_CONSTANT)
1871 l1 = mpz_get_si (cl1->length->value.integer);
1873 if (cl2 != NULL
1874 && cl2->length != NULL
1875 && cl2->length->expr_type == EXPR_CONSTANT)
1876 l2 = mpz_get_si (cl2->length->value.integer);
1878 if (l1 && l2 && l1 != l2)
1879 gfc_error ("Character length mismatch in return type of "
1880 "function '%s' at %L (%ld/%ld)", sym->name,
1881 &sym->declared_at, l1, l2);
1884 /* Type mismatch of function return type and expected type. */
1885 if (sym->attr.function
1886 && !gfc_compare_types (&sym->ts, &gsym->ns->proc_name->ts))
1887 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
1888 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
1889 gfc_typename (&gsym->ns->proc_name->ts));
1891 if (gsym->ns->proc_name->formal)
1893 gfc_formal_arglist *arg = gsym->ns->proc_name->formal;
1894 for ( ; arg; arg = arg->next)
1895 if (!arg->sym)
1896 continue;
1897 /* F2003, 12.3.1.1 (2a); F2008, 12.4.2.2 (2a) */
1898 else if (arg->sym->attr.allocatable
1899 || arg->sym->attr.asynchronous
1900 || arg->sym->attr.optional
1901 || arg->sym->attr.pointer
1902 || arg->sym->attr.target
1903 || arg->sym->attr.value
1904 || arg->sym->attr.volatile_)
1906 gfc_error ("Dummy argument '%s' of procedure '%s' at %L "
1907 "has an attribute that requires an explicit "
1908 "interface for this procedure", arg->sym->name,
1909 sym->name, &sym->declared_at);
1910 break;
1912 /* F2003, 12.3.1.1 (2b); F2008, 12.4.2.2 (2b) */
1913 else if (arg->sym && arg->sym->as
1914 && arg->sym->as->type == AS_ASSUMED_SHAPE)
1916 gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
1917 "argument '%s' must have an explicit interface",
1918 sym->name, &sym->declared_at, arg->sym->name);
1919 break;
1921 /* F2008, 12.4.2.2 (2c) */
1922 else if (arg->sym->attr.codimension)
1924 gfc_error ("Procedure '%s' at %L with coarray dummy argument "
1925 "'%s' must have an explicit interface",
1926 sym->name, &sym->declared_at, arg->sym->name);
1927 break;
1929 /* F2003, 12.3.1.1 (2c); F2008, 12.4.2.2 (2d) */
1930 else if (false) /* TODO: is a parametrized derived type */
1932 gfc_error ("Procedure '%s' at %L with parametrized derived "
1933 "type argument '%s' must have an explicit "
1934 "interface", sym->name, &sym->declared_at,
1935 arg->sym->name);
1936 break;
1938 /* F2003, 12.3.1.1 (2d); F2008, 12.4.2.2 (2e) */
1939 else if (arg->sym->ts.type == BT_CLASS)
1941 gfc_error ("Procedure '%s' at %L with polymorphic dummy "
1942 "argument '%s' must have an explicit interface",
1943 sym->name, &sym->declared_at, arg->sym->name);
1944 break;
1948 if (gsym->ns->proc_name->attr.function)
1950 /* F2003, 12.3.1.1 (3a); F2008, 12.4.2.2 (3a) */
1951 if (gsym->ns->proc_name->as
1952 && gsym->ns->proc_name->as->rank
1953 && (!sym->as || sym->as->rank != gsym->ns->proc_name->as->rank))
1954 gfc_error ("The reference to function '%s' at %L either needs an "
1955 "explicit INTERFACE or the rank is incorrect", sym->name,
1956 where);
1958 /* F2003, 12.3.1.1 (3b); F2008, 12.4.2.2 (3b) */
1959 if (gsym->ns->proc_name->result->attr.pointer
1960 || gsym->ns->proc_name->result->attr.allocatable)
1961 gfc_error ("Function '%s' at %L with a POINTER or ALLOCATABLE "
1962 "result must have an explicit interface", sym->name,
1963 where);
1965 /* F2003, 12.3.1.1 (3c); F2008, 12.4.2.2 (3c) */
1966 if (sym->ts.type == BT_CHARACTER
1967 && gsym->ns->proc_name->ts.u.cl->length != NULL)
1969 gfc_charlen *cl = sym->ts.u.cl;
1971 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
1972 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
1974 gfc_error ("Nonconstant character-length function '%s' at %L "
1975 "must have an explicit interface", sym->name,
1976 &sym->declared_at);
1981 /* F2003, 12.3.1.1 (4); F2008, 12.4.2.2 (4) */
1982 if (gsym->ns->proc_name->attr.elemental)
1984 gfc_error ("ELEMENTAL procedure '%s' at %L must have an explicit "
1985 "interface", sym->name, &sym->declared_at);
1988 /* F2003, 12.3.1.1 (5); F2008, 12.4.2.2 (5) */
1989 if (gsym->ns->proc_name->attr.is_bind_c)
1991 gfc_error ("Procedure '%s' at %L with BIND(C) attribute must have "
1992 "an explicit interface", sym->name, &sym->declared_at);
1995 if (gfc_option.flag_whole_file == 1
1996 || ((gfc_option.warn_std & GFC_STD_LEGACY)
1997 && !(gfc_option.warn_std & GFC_STD_GNU)))
1998 gfc_errors_to_warnings (1);
2000 gfc_procedure_use (gsym->ns->proc_name, actual, where);
2002 gfc_errors_to_warnings (0);
2005 if (gsym->type == GSYM_UNKNOWN)
2007 gsym->type = type;
2008 gsym->where = *where;
2011 gsym->used = 1;
2015 /************* Function resolution *************/
2017 /* Resolve a function call known to be generic.
2018 Section 14.1.2.4.1. */
2020 static match
2021 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2023 gfc_symbol *s;
2025 if (sym->attr.generic)
2027 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2028 if (s != NULL)
2030 expr->value.function.name = s->name;
2031 expr->value.function.esym = s;
2033 if (s->ts.type != BT_UNKNOWN)
2034 expr->ts = s->ts;
2035 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2036 expr->ts = s->result->ts;
2038 if (s->as != NULL)
2039 expr->rank = s->as->rank;
2040 else if (s->result != NULL && s->result->as != NULL)
2041 expr->rank = s->result->as->rank;
2043 gfc_set_sym_referenced (expr->value.function.esym);
2045 return MATCH_YES;
2048 /* TODO: Need to search for elemental references in generic
2049 interface. */
2052 if (sym->attr.intrinsic)
2053 return gfc_intrinsic_func_interface (expr, 0);
2055 return MATCH_NO;
2059 static gfc_try
2060 resolve_generic_f (gfc_expr *expr)
2062 gfc_symbol *sym;
2063 match m;
2065 sym = expr->symtree->n.sym;
2067 for (;;)
2069 m = resolve_generic_f0 (expr, sym);
2070 if (m == MATCH_YES)
2071 return SUCCESS;
2072 else if (m == MATCH_ERROR)
2073 return FAILURE;
2075 generic:
2076 if (sym->ns->parent == NULL)
2077 break;
2078 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2080 if (sym == NULL)
2081 break;
2082 if (!generic_sym (sym))
2083 goto generic;
2086 /* Last ditch attempt. See if the reference is to an intrinsic
2087 that possesses a matching interface. 14.1.2.4 */
2088 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
2090 gfc_error ("There is no specific function for the generic '%s' at %L",
2091 expr->symtree->n.sym->name, &expr->where);
2092 return FAILURE;
2095 m = gfc_intrinsic_func_interface (expr, 0);
2096 if (m == MATCH_YES)
2097 return SUCCESS;
2098 if (m == MATCH_NO)
2099 gfc_error ("Generic function '%s' at %L is not consistent with a "
2100 "specific intrinsic interface", expr->symtree->n.sym->name,
2101 &expr->where);
2103 return FAILURE;
2107 /* Resolve a function call known to be specific. */
2109 static match
2110 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2112 match m;
2114 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2116 if (sym->attr.dummy)
2118 sym->attr.proc = PROC_DUMMY;
2119 goto found;
2122 sym->attr.proc = PROC_EXTERNAL;
2123 goto found;
2126 if (sym->attr.proc == PROC_MODULE
2127 || sym->attr.proc == PROC_ST_FUNCTION
2128 || sym->attr.proc == PROC_INTERNAL)
2129 goto found;
2131 if (sym->attr.intrinsic)
2133 m = gfc_intrinsic_func_interface (expr, 1);
2134 if (m == MATCH_YES)
2135 return MATCH_YES;
2136 if (m == MATCH_NO)
2137 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2138 "with an intrinsic", sym->name, &expr->where);
2140 return MATCH_ERROR;
2143 return MATCH_NO;
2145 found:
2146 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2148 if (sym->result)
2149 expr->ts = sym->result->ts;
2150 else
2151 expr->ts = sym->ts;
2152 expr->value.function.name = sym->name;
2153 expr->value.function.esym = sym;
2154 if (sym->as != NULL)
2155 expr->rank = sym->as->rank;
2157 return MATCH_YES;
2161 static gfc_try
2162 resolve_specific_f (gfc_expr *expr)
2164 gfc_symbol *sym;
2165 match m;
2167 sym = expr->symtree->n.sym;
2169 for (;;)
2171 m = resolve_specific_f0 (sym, expr);
2172 if (m == MATCH_YES)
2173 return SUCCESS;
2174 if (m == MATCH_ERROR)
2175 return FAILURE;
2177 if (sym->ns->parent == NULL)
2178 break;
2180 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2182 if (sym == NULL)
2183 break;
2186 gfc_error ("Unable to resolve the specific function '%s' at %L",
2187 expr->symtree->n.sym->name, &expr->where);
2189 return SUCCESS;
2193 /* Resolve a procedure call not known to be generic nor specific. */
2195 static gfc_try
2196 resolve_unknown_f (gfc_expr *expr)
2198 gfc_symbol *sym;
2199 gfc_typespec *ts;
2201 sym = expr->symtree->n.sym;
2203 if (sym->attr.dummy)
2205 sym->attr.proc = PROC_DUMMY;
2206 expr->value.function.name = sym->name;
2207 goto set_type;
2210 /* See if we have an intrinsic function reference. */
2212 if (gfc_is_intrinsic (sym, 0, expr->where))
2214 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2215 return SUCCESS;
2216 return FAILURE;
2219 /* The reference is to an external name. */
2221 sym->attr.proc = PROC_EXTERNAL;
2222 expr->value.function.name = sym->name;
2223 expr->value.function.esym = expr->symtree->n.sym;
2225 if (sym->as != NULL)
2226 expr->rank = sym->as->rank;
2228 /* Type of the expression is either the type of the symbol or the
2229 default type of the symbol. */
2231 set_type:
2232 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2234 if (sym->ts.type != BT_UNKNOWN)
2235 expr->ts = sym->ts;
2236 else
2238 ts = gfc_get_default_type (sym->name, sym->ns);
2240 if (ts->type == BT_UNKNOWN)
2242 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2243 sym->name, &expr->where);
2244 return FAILURE;
2246 else
2247 expr->ts = *ts;
2250 return SUCCESS;
2254 /* Return true, if the symbol is an external procedure. */
2255 static bool
2256 is_external_proc (gfc_symbol *sym)
2258 if (!sym->attr.dummy && !sym->attr.contained
2259 && !(sym->attr.intrinsic
2260 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2261 && sym->attr.proc != PROC_ST_FUNCTION
2262 && !sym->attr.proc_pointer
2263 && !sym->attr.use_assoc
2264 && sym->name)
2265 return true;
2267 return false;
2271 /* Figure out if a function reference is pure or not. Also set the name
2272 of the function for a potential error message. Return nonzero if the
2273 function is PURE, zero if not. */
2274 static int
2275 pure_stmt_function (gfc_expr *, gfc_symbol *);
2277 static int
2278 pure_function (gfc_expr *e, const char **name)
2280 int pure;
2282 *name = NULL;
2284 if (e->symtree != NULL
2285 && e->symtree->n.sym != NULL
2286 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2287 return pure_stmt_function (e, e->symtree->n.sym);
2289 if (e->value.function.esym)
2291 pure = gfc_pure (e->value.function.esym);
2292 *name = e->value.function.esym->name;
2294 else if (e->value.function.isym)
2296 pure = e->value.function.isym->pure
2297 || e->value.function.isym->elemental;
2298 *name = e->value.function.isym->name;
2300 else
2302 /* Implicit functions are not pure. */
2303 pure = 0;
2304 *name = e->value.function.name;
2307 return pure;
2311 static bool
2312 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2313 int *f ATTRIBUTE_UNUSED)
2315 const char *name;
2317 /* Don't bother recursing into other statement functions
2318 since they will be checked individually for purity. */
2319 if (e->expr_type != EXPR_FUNCTION
2320 || !e->symtree
2321 || e->symtree->n.sym == sym
2322 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2323 return false;
2325 return pure_function (e, &name) ? false : true;
2329 static int
2330 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2332 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2336 static gfc_try
2337 is_scalar_expr_ptr (gfc_expr *expr)
2339 gfc_try retval = SUCCESS;
2340 gfc_ref *ref;
2341 int start;
2342 int end;
2344 /* See if we have a gfc_ref, which means we have a substring, array
2345 reference, or a component. */
2346 if (expr->ref != NULL)
2348 ref = expr->ref;
2349 while (ref->next != NULL)
2350 ref = ref->next;
2352 switch (ref->type)
2354 case REF_SUBSTRING:
2355 if (ref->u.ss.length != NULL
2356 && ref->u.ss.length->length != NULL
2357 && ref->u.ss.start
2358 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2359 && ref->u.ss.end
2360 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2362 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2363 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2364 if (end - start + 1 != 1)
2365 retval = FAILURE;
2367 else
2368 retval = FAILURE;
2369 break;
2370 case REF_ARRAY:
2371 if (ref->u.ar.type == AR_ELEMENT)
2372 retval = SUCCESS;
2373 else if (ref->u.ar.type == AR_FULL)
2375 /* The user can give a full array if the array is of size 1. */
2376 if (ref->u.ar.as != NULL
2377 && ref->u.ar.as->rank == 1
2378 && ref->u.ar.as->type == AS_EXPLICIT
2379 && ref->u.ar.as->lower[0] != NULL
2380 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2381 && ref->u.ar.as->upper[0] != NULL
2382 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2384 /* If we have a character string, we need to check if
2385 its length is one. */
2386 if (expr->ts.type == BT_CHARACTER)
2388 if (expr->ts.u.cl == NULL
2389 || expr->ts.u.cl->length == NULL
2390 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2391 != 0)
2392 retval = FAILURE;
2394 else
2396 /* We have constant lower and upper bounds. If the
2397 difference between is 1, it can be considered a
2398 scalar. */
2399 start = (int) mpz_get_si
2400 (ref->u.ar.as->lower[0]->value.integer);
2401 end = (int) mpz_get_si
2402 (ref->u.ar.as->upper[0]->value.integer);
2403 if (end - start + 1 != 1)
2404 retval = FAILURE;
2407 else
2408 retval = FAILURE;
2410 else
2411 retval = FAILURE;
2412 break;
2413 default:
2414 retval = SUCCESS;
2415 break;
2418 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2420 /* Character string. Make sure it's of length 1. */
2421 if (expr->ts.u.cl == NULL
2422 || expr->ts.u.cl->length == NULL
2423 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2424 retval = FAILURE;
2426 else if (expr->rank != 0)
2427 retval = FAILURE;
2429 return retval;
2433 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2434 and, in the case of c_associated, set the binding label based on
2435 the arguments. */
2437 static gfc_try
2438 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2439 gfc_symbol **new_sym)
2441 char name[GFC_MAX_SYMBOL_LEN + 1];
2442 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2443 int optional_arg = 0;
2444 gfc_try retval = SUCCESS;
2445 gfc_symbol *args_sym;
2446 gfc_typespec *arg_ts;
2447 symbol_attribute arg_attr;
2449 if (args->expr->expr_type == EXPR_CONSTANT
2450 || args->expr->expr_type == EXPR_OP
2451 || args->expr->expr_type == EXPR_NULL)
2453 gfc_error ("Argument to '%s' at %L is not a variable",
2454 sym->name, &(args->expr->where));
2455 return FAILURE;
2458 args_sym = args->expr->symtree->n.sym;
2460 /* The typespec for the actual arg should be that stored in the expr
2461 and not necessarily that of the expr symbol (args_sym), because
2462 the actual expression could be a part-ref of the expr symbol. */
2463 arg_ts = &(args->expr->ts);
2464 arg_attr = gfc_expr_attr (args->expr);
2466 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2468 /* If the user gave two args then they are providing something for
2469 the optional arg (the second cptr). Therefore, set the name and
2470 binding label to the c_associated for two cptrs. Otherwise,
2471 set c_associated to expect one cptr. */
2472 if (args->next)
2474 /* two args. */
2475 sprintf (name, "%s_2", sym->name);
2476 sprintf (binding_label, "%s_2", sym->binding_label);
2477 optional_arg = 1;
2479 else
2481 /* one arg. */
2482 sprintf (name, "%s_1", sym->name);
2483 sprintf (binding_label, "%s_1", sym->binding_label);
2484 optional_arg = 0;
2487 /* Get a new symbol for the version of c_associated that
2488 will get called. */
2489 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2491 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2492 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2494 sprintf (name, "%s", sym->name);
2495 sprintf (binding_label, "%s", sym->binding_label);
2497 /* Error check the call. */
2498 if (args->next != NULL)
2500 gfc_error_now ("More actual than formal arguments in '%s' "
2501 "call at %L", name, &(args->expr->where));
2502 retval = FAILURE;
2504 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2506 /* Make sure we have either the target or pointer attribute. */
2507 if (!arg_attr.target && !arg_attr.pointer)
2509 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2510 "a TARGET or an associated pointer",
2511 args_sym->name,
2512 sym->name, &(args->expr->where));
2513 retval = FAILURE;
2516 /* See if we have interoperable type and type param. */
2517 if (verify_c_interop (arg_ts) == SUCCESS
2518 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2520 if (args_sym->attr.target == 1)
2522 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2523 has the target attribute and is interoperable. */
2524 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2525 allocatable variable that has the TARGET attribute and
2526 is not an array of zero size. */
2527 if (args_sym->attr.allocatable == 1)
2529 if (args_sym->attr.dimension != 0
2530 && (args_sym->as && args_sym->as->rank == 0))
2532 gfc_error_now ("Allocatable variable '%s' used as a "
2533 "parameter to '%s' at %L must not be "
2534 "an array of zero size",
2535 args_sym->name, sym->name,
2536 &(args->expr->where));
2537 retval = FAILURE;
2540 else
2542 /* A non-allocatable target variable with C
2543 interoperable type and type parameters must be
2544 interoperable. */
2545 if (args_sym && args_sym->attr.dimension)
2547 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2549 gfc_error ("Assumed-shape array '%s' at %L "
2550 "cannot be an argument to the "
2551 "procedure '%s' because "
2552 "it is not C interoperable",
2553 args_sym->name,
2554 &(args->expr->where), sym->name);
2555 retval = FAILURE;
2557 else if (args_sym->as->type == AS_DEFERRED)
2559 gfc_error ("Deferred-shape array '%s' at %L "
2560 "cannot be an argument to the "
2561 "procedure '%s' because "
2562 "it is not C interoperable",
2563 args_sym->name,
2564 &(args->expr->where), sym->name);
2565 retval = FAILURE;
2569 /* Make sure it's not a character string. Arrays of
2570 any type should be ok if the variable is of a C
2571 interoperable type. */
2572 if (arg_ts->type == BT_CHARACTER)
2573 if (arg_ts->u.cl != NULL
2574 && (arg_ts->u.cl->length == NULL
2575 || arg_ts->u.cl->length->expr_type
2576 != EXPR_CONSTANT
2577 || mpz_cmp_si
2578 (arg_ts->u.cl->length->value.integer, 1)
2579 != 0)
2580 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2582 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2583 "at %L must have a length of 1",
2584 args_sym->name, sym->name,
2585 &(args->expr->where));
2586 retval = FAILURE;
2590 else if (arg_attr.pointer
2591 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2593 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2594 scalar pointer. */
2595 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2596 "associated scalar POINTER", args_sym->name,
2597 sym->name, &(args->expr->where));
2598 retval = FAILURE;
2601 else
2603 /* The parameter is not required to be C interoperable. If it
2604 is not C interoperable, it must be a nonpolymorphic scalar
2605 with no length type parameters. It still must have either
2606 the pointer or target attribute, and it can be
2607 allocatable (but must be allocated when c_loc is called). */
2608 if (args->expr->rank != 0
2609 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2611 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2612 "scalar", args_sym->name, sym->name,
2613 &(args->expr->where));
2614 retval = FAILURE;
2616 else if (arg_ts->type == BT_CHARACTER
2617 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2619 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2620 "%L must have a length of 1",
2621 args_sym->name, sym->name,
2622 &(args->expr->where));
2623 retval = FAILURE;
2625 else if (arg_ts->type == BT_CLASS)
2627 gfc_error_now ("Parameter '%s' to '%s' at %L must not be "
2628 "polymorphic", args_sym->name, sym->name,
2629 &(args->expr->where));
2630 retval = FAILURE;
2634 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2636 if (args_sym->attr.flavor != FL_PROCEDURE)
2638 /* TODO: Update this error message to allow for procedure
2639 pointers once they are implemented. */
2640 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2641 "procedure",
2642 args_sym->name, sym->name,
2643 &(args->expr->where));
2644 retval = FAILURE;
2646 else if (args_sym->attr.is_bind_c != 1)
2648 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2649 "BIND(C)",
2650 args_sym->name, sym->name,
2651 &(args->expr->where));
2652 retval = FAILURE;
2656 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2657 *new_sym = sym;
2659 else
2661 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2662 "iso_c_binding function: '%s'!\n", sym->name);
2665 return retval;
2669 /* Resolve a function call, which means resolving the arguments, then figuring
2670 out which entity the name refers to. */
2671 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2672 to INTENT(OUT) or INTENT(INOUT). */
2674 static gfc_try
2675 resolve_function (gfc_expr *expr)
2677 gfc_actual_arglist *arg;
2678 gfc_symbol *sym;
2679 const char *name;
2680 gfc_try t;
2681 int temp;
2682 procedure_type p = PROC_INTRINSIC;
2683 bool no_formal_args;
2685 sym = NULL;
2686 if (expr->symtree)
2687 sym = expr->symtree->n.sym;
2689 /* If this is a procedure pointer component, it has already been resolved. */
2690 if (gfc_is_proc_ptr_comp (expr, NULL))
2691 return SUCCESS;
2693 if (sym && sym->attr.intrinsic
2694 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2695 return FAILURE;
2697 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2699 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2700 return FAILURE;
2703 /* If this ia a deferred TBP with an abstract interface (which may
2704 of course be referenced), expr->value.function.esym will be set. */
2705 if (sym && sym->attr.abstract && !expr->value.function.esym)
2707 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2708 sym->name, &expr->where);
2709 return FAILURE;
2712 /* Switch off assumed size checking and do this again for certain kinds
2713 of procedure, once the procedure itself is resolved. */
2714 need_full_assumed_size++;
2716 if (expr->symtree && expr->symtree->n.sym)
2717 p = expr->symtree->n.sym->attr.proc;
2719 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2720 inquiry_argument = true;
2721 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2723 if (resolve_actual_arglist (expr->value.function.actual,
2724 p, no_formal_args) == FAILURE)
2726 inquiry_argument = false;
2727 return FAILURE;
2730 inquiry_argument = false;
2732 /* Need to setup the call to the correct c_associated, depending on
2733 the number of cptrs to user gives to compare. */
2734 if (sym && sym->attr.is_iso_c == 1)
2736 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2737 == FAILURE)
2738 return FAILURE;
2740 /* Get the symtree for the new symbol (resolved func).
2741 the old one will be freed later, when it's no longer used. */
2742 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2745 /* Resume assumed_size checking. */
2746 need_full_assumed_size--;
2748 /* If the procedure is external, check for usage. */
2749 if (sym && is_external_proc (sym))
2750 resolve_global_procedure (sym, &expr->where,
2751 &expr->value.function.actual, 0);
2753 if (sym && sym->ts.type == BT_CHARACTER
2754 && sym->ts.u.cl
2755 && sym->ts.u.cl->length == NULL
2756 && !sym->attr.dummy
2757 && expr->value.function.esym == NULL
2758 && !sym->attr.contained)
2760 /* Internal procedures are taken care of in resolve_contained_fntype. */
2761 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2762 "be used at %L since it is not a dummy argument",
2763 sym->name, &expr->where);
2764 return FAILURE;
2767 /* See if function is already resolved. */
2769 if (expr->value.function.name != NULL)
2771 if (expr->ts.type == BT_UNKNOWN)
2772 expr->ts = sym->ts;
2773 t = SUCCESS;
2775 else
2777 /* Apply the rules of section 14.1.2. */
2779 switch (procedure_kind (sym))
2781 case PTYPE_GENERIC:
2782 t = resolve_generic_f (expr);
2783 break;
2785 case PTYPE_SPECIFIC:
2786 t = resolve_specific_f (expr);
2787 break;
2789 case PTYPE_UNKNOWN:
2790 t = resolve_unknown_f (expr);
2791 break;
2793 default:
2794 gfc_internal_error ("resolve_function(): bad function type");
2798 /* If the expression is still a function (it might have simplified),
2799 then we check to see if we are calling an elemental function. */
2801 if (expr->expr_type != EXPR_FUNCTION)
2802 return t;
2804 temp = need_full_assumed_size;
2805 need_full_assumed_size = 0;
2807 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2808 return FAILURE;
2810 if (omp_workshare_flag
2811 && expr->value.function.esym
2812 && ! gfc_elemental (expr->value.function.esym))
2814 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2815 "in WORKSHARE construct", expr->value.function.esym->name,
2816 &expr->where);
2817 t = FAILURE;
2820 #define GENERIC_ID expr->value.function.isym->id
2821 else if (expr->value.function.actual != NULL
2822 && expr->value.function.isym != NULL
2823 && GENERIC_ID != GFC_ISYM_LBOUND
2824 && GENERIC_ID != GFC_ISYM_LEN
2825 && GENERIC_ID != GFC_ISYM_LOC
2826 && GENERIC_ID != GFC_ISYM_PRESENT)
2828 /* Array intrinsics must also have the last upper bound of an
2829 assumed size array argument. UBOUND and SIZE have to be
2830 excluded from the check if the second argument is anything
2831 than a constant. */
2833 for (arg = expr->value.function.actual; arg; arg = arg->next)
2835 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2836 && arg->next != NULL && arg->next->expr)
2838 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2839 break;
2841 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2842 break;
2844 if ((int)mpz_get_si (arg->next->expr->value.integer)
2845 < arg->expr->rank)
2846 break;
2849 if (arg->expr != NULL
2850 && arg->expr->rank > 0
2851 && resolve_assumed_size_actual (arg->expr))
2852 return FAILURE;
2855 #undef GENERIC_ID
2857 need_full_assumed_size = temp;
2858 name = NULL;
2860 if (!pure_function (expr, &name) && name)
2862 if (forall_flag)
2864 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2865 "FORALL %s", name, &expr->where,
2866 forall_flag == 2 ? "mask" : "block");
2867 t = FAILURE;
2869 else if (gfc_pure (NULL))
2871 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2872 "procedure within a PURE procedure", name, &expr->where);
2873 t = FAILURE;
2877 /* Functions without the RECURSIVE attribution are not allowed to
2878 * call themselves. */
2879 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2881 gfc_symbol *esym;
2882 esym = expr->value.function.esym;
2884 if (is_illegal_recursion (esym, gfc_current_ns))
2886 if (esym->attr.entry && esym->ns->entries)
2887 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2888 " function '%s' is not RECURSIVE",
2889 esym->name, &expr->where, esym->ns->entries->sym->name);
2890 else
2891 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2892 " is not RECURSIVE", esym->name, &expr->where);
2894 t = FAILURE;
2898 /* Character lengths of use associated functions may contains references to
2899 symbols not referenced from the current program unit otherwise. Make sure
2900 those symbols are marked as referenced. */
2902 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2903 && expr->value.function.esym->attr.use_assoc)
2905 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
2908 if (t == SUCCESS
2909 && !((expr->value.function.esym
2910 && expr->value.function.esym->attr.elemental)
2912 (expr->value.function.isym
2913 && expr->value.function.isym->elemental)))
2914 find_noncopying_intrinsics (expr->value.function.esym,
2915 expr->value.function.actual);
2917 /* Make sure that the expression has a typespec that works. */
2918 if (expr->ts.type == BT_UNKNOWN)
2920 if (expr->symtree->n.sym->result
2921 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
2922 && !expr->symtree->n.sym->result->attr.proc_pointer)
2923 expr->ts = expr->symtree->n.sym->result->ts;
2926 return t;
2930 /************* Subroutine resolution *************/
2932 static void
2933 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2935 if (gfc_pure (sym))
2936 return;
2938 if (forall_flag)
2939 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2940 sym->name, &c->loc);
2941 else if (gfc_pure (NULL))
2942 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2943 &c->loc);
2947 static match
2948 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2950 gfc_symbol *s;
2952 if (sym->attr.generic)
2954 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2955 if (s != NULL)
2957 c->resolved_sym = s;
2958 pure_subroutine (c, s);
2959 return MATCH_YES;
2962 /* TODO: Need to search for elemental references in generic interface. */
2965 if (sym->attr.intrinsic)
2966 return gfc_intrinsic_sub_interface (c, 0);
2968 return MATCH_NO;
2972 static gfc_try
2973 resolve_generic_s (gfc_code *c)
2975 gfc_symbol *sym;
2976 match m;
2978 sym = c->symtree->n.sym;
2980 for (;;)
2982 m = resolve_generic_s0 (c, sym);
2983 if (m == MATCH_YES)
2984 return SUCCESS;
2985 else if (m == MATCH_ERROR)
2986 return FAILURE;
2988 generic:
2989 if (sym->ns->parent == NULL)
2990 break;
2991 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2993 if (sym == NULL)
2994 break;
2995 if (!generic_sym (sym))
2996 goto generic;
2999 /* Last ditch attempt. See if the reference is to an intrinsic
3000 that possesses a matching interface. 14.1.2.4 */
3001 sym = c->symtree->n.sym;
3003 if (!gfc_is_intrinsic (sym, 1, c->loc))
3005 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3006 sym->name, &c->loc);
3007 return FAILURE;
3010 m = gfc_intrinsic_sub_interface (c, 0);
3011 if (m == MATCH_YES)
3012 return SUCCESS;
3013 if (m == MATCH_NO)
3014 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3015 "intrinsic subroutine interface", sym->name, &c->loc);
3017 return FAILURE;
3021 /* Set the name and binding label of the subroutine symbol in the call
3022 expression represented by 'c' to include the type and kind of the
3023 second parameter. This function is for resolving the appropriate
3024 version of c_f_pointer() and c_f_procpointer(). For example, a
3025 call to c_f_pointer() for a default integer pointer could have a
3026 name of c_f_pointer_i4. If no second arg exists, which is an error
3027 for these two functions, it defaults to the generic symbol's name
3028 and binding label. */
3030 static void
3031 set_name_and_label (gfc_code *c, gfc_symbol *sym,
3032 char *name, char *binding_label)
3034 gfc_expr *arg = NULL;
3035 char type;
3036 int kind;
3038 /* The second arg of c_f_pointer and c_f_procpointer determines
3039 the type and kind for the procedure name. */
3040 arg = c->ext.actual->next->expr;
3042 if (arg != NULL)
3044 /* Set up the name to have the given symbol's name,
3045 plus the type and kind. */
3046 /* a derived type is marked with the type letter 'u' */
3047 if (arg->ts.type == BT_DERIVED)
3049 type = 'd';
3050 kind = 0; /* set the kind as 0 for now */
3052 else
3054 type = gfc_type_letter (arg->ts.type);
3055 kind = arg->ts.kind;
3058 if (arg->ts.type == BT_CHARACTER)
3059 /* Kind info for character strings not needed. */
3060 kind = 0;
3062 sprintf (name, "%s_%c%d", sym->name, type, kind);
3063 /* Set up the binding label as the given symbol's label plus
3064 the type and kind. */
3065 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
3067 else
3069 /* If the second arg is missing, set the name and label as
3070 was, cause it should at least be found, and the missing
3071 arg error will be caught by compare_parameters(). */
3072 sprintf (name, "%s", sym->name);
3073 sprintf (binding_label, "%s", sym->binding_label);
3076 return;
3080 /* Resolve a generic version of the iso_c_binding procedure given
3081 (sym) to the specific one based on the type and kind of the
3082 argument(s). Currently, this function resolves c_f_pointer() and
3083 c_f_procpointer based on the type and kind of the second argument
3084 (FPTR). Other iso_c_binding procedures aren't specially handled.
3085 Upon successfully exiting, c->resolved_sym will hold the resolved
3086 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
3087 otherwise. */
3089 match
3090 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
3092 gfc_symbol *new_sym;
3093 /* this is fine, since we know the names won't use the max */
3094 char name[GFC_MAX_SYMBOL_LEN + 1];
3095 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
3096 /* default to success; will override if find error */
3097 match m = MATCH_YES;
3099 /* Make sure the actual arguments are in the necessary order (based on the
3100 formal args) before resolving. */
3101 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
3103 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3104 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3106 set_name_and_label (c, sym, name, binding_label);
3108 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3110 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
3112 /* Make sure we got a third arg if the second arg has non-zero
3113 rank. We must also check that the type and rank are
3114 correct since we short-circuit this check in
3115 gfc_procedure_use() (called above to sort actual args). */
3116 if (c->ext.actual->next->expr->rank != 0)
3118 if(c->ext.actual->next->next == NULL
3119 || c->ext.actual->next->next->expr == NULL)
3121 m = MATCH_ERROR;
3122 gfc_error ("Missing SHAPE parameter for call to %s "
3123 "at %L", sym->name, &(c->loc));
3125 else if (c->ext.actual->next->next->expr->ts.type
3126 != BT_INTEGER
3127 || c->ext.actual->next->next->expr->rank != 1)
3129 m = MATCH_ERROR;
3130 gfc_error ("SHAPE parameter for call to %s at %L must "
3131 "be a rank 1 INTEGER array", sym->name,
3132 &(c->loc));
3138 if (m != MATCH_ERROR)
3140 /* the 1 means to add the optional arg to formal list */
3141 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
3143 /* for error reporting, say it's declared where the original was */
3144 new_sym->declared_at = sym->declared_at;
3147 else
3149 /* no differences for c_loc or c_funloc */
3150 new_sym = sym;
3153 /* set the resolved symbol */
3154 if (m != MATCH_ERROR)
3155 c->resolved_sym = new_sym;
3156 else
3157 c->resolved_sym = sym;
3159 return m;
3163 /* Resolve a subroutine call known to be specific. */
3165 static match
3166 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3168 match m;
3170 if(sym->attr.is_iso_c)
3172 m = gfc_iso_c_sub_interface (c,sym);
3173 return m;
3176 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3178 if (sym->attr.dummy)
3180 sym->attr.proc = PROC_DUMMY;
3181 goto found;
3184 sym->attr.proc = PROC_EXTERNAL;
3185 goto found;
3188 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3189 goto found;
3191 if (sym->attr.intrinsic)
3193 m = gfc_intrinsic_sub_interface (c, 1);
3194 if (m == MATCH_YES)
3195 return MATCH_YES;
3196 if (m == MATCH_NO)
3197 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3198 "with an intrinsic", sym->name, &c->loc);
3200 return MATCH_ERROR;
3203 return MATCH_NO;
3205 found:
3206 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3208 c->resolved_sym = sym;
3209 pure_subroutine (c, sym);
3211 return MATCH_YES;
3215 static gfc_try
3216 resolve_specific_s (gfc_code *c)
3218 gfc_symbol *sym;
3219 match m;
3221 sym = c->symtree->n.sym;
3223 for (;;)
3225 m = resolve_specific_s0 (c, sym);
3226 if (m == MATCH_YES)
3227 return SUCCESS;
3228 if (m == MATCH_ERROR)
3229 return FAILURE;
3231 if (sym->ns->parent == NULL)
3232 break;
3234 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3236 if (sym == NULL)
3237 break;
3240 sym = c->symtree->n.sym;
3241 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3242 sym->name, &c->loc);
3244 return FAILURE;
3248 /* Resolve a subroutine call not known to be generic nor specific. */
3250 static gfc_try
3251 resolve_unknown_s (gfc_code *c)
3253 gfc_symbol *sym;
3255 sym = c->symtree->n.sym;
3257 if (sym->attr.dummy)
3259 sym->attr.proc = PROC_DUMMY;
3260 goto found;
3263 /* See if we have an intrinsic function reference. */
3265 if (gfc_is_intrinsic (sym, 1, c->loc))
3267 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3268 return SUCCESS;
3269 return FAILURE;
3272 /* The reference is to an external name. */
3274 found:
3275 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3277 c->resolved_sym = sym;
3279 pure_subroutine (c, sym);
3281 return SUCCESS;
3285 /* Resolve a subroutine call. Although it was tempting to use the same code
3286 for functions, subroutines and functions are stored differently and this
3287 makes things awkward. */
3289 static gfc_try
3290 resolve_call (gfc_code *c)
3292 gfc_try t;
3293 procedure_type ptype = PROC_INTRINSIC;
3294 gfc_symbol *csym, *sym;
3295 bool no_formal_args;
3297 csym = c->symtree ? c->symtree->n.sym : NULL;
3299 if (csym && csym->ts.type != BT_UNKNOWN)
3301 gfc_error ("'%s' at %L has a type, which is not consistent with "
3302 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3303 return FAILURE;
3306 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3308 gfc_symtree *st;
3309 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3310 sym = st ? st->n.sym : NULL;
3311 if (sym && csym != sym
3312 && sym->ns == gfc_current_ns
3313 && sym->attr.flavor == FL_PROCEDURE
3314 && sym->attr.contained)
3316 sym->refs++;
3317 if (csym->attr.generic)
3318 c->symtree->n.sym = sym;
3319 else
3320 c->symtree = st;
3321 csym = c->symtree->n.sym;
3325 /* If this ia a deferred TBP with an abstract interface
3326 (which may of course be referenced), c->expr1 will be set. */
3327 if (csym && csym->attr.abstract && !c->expr1)
3329 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3330 csym->name, &c->loc);
3331 return FAILURE;
3334 /* Subroutines without the RECURSIVE attribution are not allowed to
3335 * call themselves. */
3336 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3338 if (csym->attr.entry && csym->ns->entries)
3339 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3340 " subroutine '%s' is not RECURSIVE",
3341 csym->name, &c->loc, csym->ns->entries->sym->name);
3342 else
3343 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3344 " is not RECURSIVE", csym->name, &c->loc);
3346 t = FAILURE;
3349 /* Switch off assumed size checking and do this again for certain kinds
3350 of procedure, once the procedure itself is resolved. */
3351 need_full_assumed_size++;
3353 if (csym)
3354 ptype = csym->attr.proc;
3356 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3357 if (resolve_actual_arglist (c->ext.actual, ptype,
3358 no_formal_args) == FAILURE)
3359 return FAILURE;
3361 /* Resume assumed_size checking. */
3362 need_full_assumed_size--;
3364 /* If external, check for usage. */
3365 if (csym && is_external_proc (csym))
3366 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3368 t = SUCCESS;
3369 if (c->resolved_sym == NULL)
3371 c->resolved_isym = NULL;
3372 switch (procedure_kind (csym))
3374 case PTYPE_GENERIC:
3375 t = resolve_generic_s (c);
3376 break;
3378 case PTYPE_SPECIFIC:
3379 t = resolve_specific_s (c);
3380 break;
3382 case PTYPE_UNKNOWN:
3383 t = resolve_unknown_s (c);
3384 break;
3386 default:
3387 gfc_internal_error ("resolve_subroutine(): bad function type");
3391 /* Some checks of elemental subroutine actual arguments. */
3392 if (resolve_elemental_actual (NULL, c) == FAILURE)
3393 return FAILURE;
3395 if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
3396 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
3397 return t;
3401 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3402 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3403 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3404 if their shapes do not match. If either op1->shape or op2->shape is
3405 NULL, return SUCCESS. */
3407 static gfc_try
3408 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3410 gfc_try t;
3411 int i;
3413 t = SUCCESS;
3415 if (op1->shape != NULL && op2->shape != NULL)
3417 for (i = 0; i < op1->rank; i++)
3419 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3421 gfc_error ("Shapes for operands at %L and %L are not conformable",
3422 &op1->where, &op2->where);
3423 t = FAILURE;
3424 break;
3429 return t;
3433 /* Resolve an operator expression node. This can involve replacing the
3434 operation with a user defined function call. */
3436 static gfc_try
3437 resolve_operator (gfc_expr *e)
3439 gfc_expr *op1, *op2;
3440 char msg[200];
3441 bool dual_locus_error;
3442 gfc_try t;
3444 /* Resolve all subnodes-- give them types. */
3446 switch (e->value.op.op)
3448 default:
3449 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3450 return FAILURE;
3452 /* Fall through... */
3454 case INTRINSIC_NOT:
3455 case INTRINSIC_UPLUS:
3456 case INTRINSIC_UMINUS:
3457 case INTRINSIC_PARENTHESES:
3458 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3459 return FAILURE;
3460 break;
3463 /* Typecheck the new node. */
3465 op1 = e->value.op.op1;
3466 op2 = e->value.op.op2;
3467 dual_locus_error = false;
3469 if ((op1 && op1->expr_type == EXPR_NULL)
3470 || (op2 && op2->expr_type == EXPR_NULL))
3472 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3473 goto bad_op;
3476 switch (e->value.op.op)
3478 case INTRINSIC_UPLUS:
3479 case INTRINSIC_UMINUS:
3480 if (op1->ts.type == BT_INTEGER
3481 || op1->ts.type == BT_REAL
3482 || op1->ts.type == BT_COMPLEX)
3484 e->ts = op1->ts;
3485 break;
3488 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3489 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3490 goto bad_op;
3492 case INTRINSIC_PLUS:
3493 case INTRINSIC_MINUS:
3494 case INTRINSIC_TIMES:
3495 case INTRINSIC_DIVIDE:
3496 case INTRINSIC_POWER:
3497 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3499 gfc_type_convert_binary (e, 1);
3500 break;
3503 sprintf (msg,
3504 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3505 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3506 gfc_typename (&op2->ts));
3507 goto bad_op;
3509 case INTRINSIC_CONCAT:
3510 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3511 && op1->ts.kind == op2->ts.kind)
3513 e->ts.type = BT_CHARACTER;
3514 e->ts.kind = op1->ts.kind;
3515 break;
3518 sprintf (msg,
3519 _("Operands of string concatenation operator at %%L are %s/%s"),
3520 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3521 goto bad_op;
3523 case INTRINSIC_AND:
3524 case INTRINSIC_OR:
3525 case INTRINSIC_EQV:
3526 case INTRINSIC_NEQV:
3527 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3529 e->ts.type = BT_LOGICAL;
3530 e->ts.kind = gfc_kind_max (op1, op2);
3531 if (op1->ts.kind < e->ts.kind)
3532 gfc_convert_type (op1, &e->ts, 2);
3533 else if (op2->ts.kind < e->ts.kind)
3534 gfc_convert_type (op2, &e->ts, 2);
3535 break;
3538 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3539 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3540 gfc_typename (&op2->ts));
3542 goto bad_op;
3544 case INTRINSIC_NOT:
3545 if (op1->ts.type == BT_LOGICAL)
3547 e->ts.type = BT_LOGICAL;
3548 e->ts.kind = op1->ts.kind;
3549 break;
3552 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3553 gfc_typename (&op1->ts));
3554 goto bad_op;
3556 case INTRINSIC_GT:
3557 case INTRINSIC_GT_OS:
3558 case INTRINSIC_GE:
3559 case INTRINSIC_GE_OS:
3560 case INTRINSIC_LT:
3561 case INTRINSIC_LT_OS:
3562 case INTRINSIC_LE:
3563 case INTRINSIC_LE_OS:
3564 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3566 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3567 goto bad_op;
3570 /* Fall through... */
3572 case INTRINSIC_EQ:
3573 case INTRINSIC_EQ_OS:
3574 case INTRINSIC_NE:
3575 case INTRINSIC_NE_OS:
3576 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3577 && op1->ts.kind == op2->ts.kind)
3579 e->ts.type = BT_LOGICAL;
3580 e->ts.kind = gfc_default_logical_kind;
3581 break;
3584 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3586 gfc_type_convert_binary (e, 1);
3588 e->ts.type = BT_LOGICAL;
3589 e->ts.kind = gfc_default_logical_kind;
3590 break;
3593 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3594 sprintf (msg,
3595 _("Logicals at %%L must be compared with %s instead of %s"),
3596 (e->value.op.op == INTRINSIC_EQ
3597 || e->value.op.op == INTRINSIC_EQ_OS)
3598 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3599 else
3600 sprintf (msg,
3601 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3602 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3603 gfc_typename (&op2->ts));
3605 goto bad_op;
3607 case INTRINSIC_USER:
3608 if (e->value.op.uop->op == NULL)
3609 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3610 else if (op2 == NULL)
3611 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3612 e->value.op.uop->name, gfc_typename (&op1->ts));
3613 else
3614 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3615 e->value.op.uop->name, gfc_typename (&op1->ts),
3616 gfc_typename (&op2->ts));
3618 goto bad_op;
3620 case INTRINSIC_PARENTHESES:
3621 e->ts = op1->ts;
3622 if (e->ts.type == BT_CHARACTER)
3623 e->ts.u.cl = op1->ts.u.cl;
3624 break;
3626 default:
3627 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3630 /* Deal with arrayness of an operand through an operator. */
3632 t = SUCCESS;
3634 switch (e->value.op.op)
3636 case INTRINSIC_PLUS:
3637 case INTRINSIC_MINUS:
3638 case INTRINSIC_TIMES:
3639 case INTRINSIC_DIVIDE:
3640 case INTRINSIC_POWER:
3641 case INTRINSIC_CONCAT:
3642 case INTRINSIC_AND:
3643 case INTRINSIC_OR:
3644 case INTRINSIC_EQV:
3645 case INTRINSIC_NEQV:
3646 case INTRINSIC_EQ:
3647 case INTRINSIC_EQ_OS:
3648 case INTRINSIC_NE:
3649 case INTRINSIC_NE_OS:
3650 case INTRINSIC_GT:
3651 case INTRINSIC_GT_OS:
3652 case INTRINSIC_GE:
3653 case INTRINSIC_GE_OS:
3654 case INTRINSIC_LT:
3655 case INTRINSIC_LT_OS:
3656 case INTRINSIC_LE:
3657 case INTRINSIC_LE_OS:
3659 if (op1->rank == 0 && op2->rank == 0)
3660 e->rank = 0;
3662 if (op1->rank == 0 && op2->rank != 0)
3664 e->rank = op2->rank;
3666 if (e->shape == NULL)
3667 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3670 if (op1->rank != 0 && op2->rank == 0)
3672 e->rank = op1->rank;
3674 if (e->shape == NULL)
3675 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3678 if (op1->rank != 0 && op2->rank != 0)
3680 if (op1->rank == op2->rank)
3682 e->rank = op1->rank;
3683 if (e->shape == NULL)
3685 t = compare_shapes (op1, op2);
3686 if (t == FAILURE)
3687 e->shape = NULL;
3688 else
3689 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3692 else
3694 /* Allow higher level expressions to work. */
3695 e->rank = 0;
3697 /* Try user-defined operators, and otherwise throw an error. */
3698 dual_locus_error = true;
3699 sprintf (msg,
3700 _("Inconsistent ranks for operator at %%L and %%L"));
3701 goto bad_op;
3705 break;
3707 case INTRINSIC_PARENTHESES:
3708 case INTRINSIC_NOT:
3709 case INTRINSIC_UPLUS:
3710 case INTRINSIC_UMINUS:
3711 /* Simply copy arrayness attribute */
3712 e->rank = op1->rank;
3714 if (e->shape == NULL)
3715 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3717 break;
3719 default:
3720 break;
3723 /* Attempt to simplify the expression. */
3724 if (t == SUCCESS)
3726 t = gfc_simplify_expr (e, 0);
3727 /* Some calls do not succeed in simplification and return FAILURE
3728 even though there is no error; e.g. variable references to
3729 PARAMETER arrays. */
3730 if (!gfc_is_constant_expr (e))
3731 t = SUCCESS;
3733 return t;
3735 bad_op:
3738 bool real_error;
3739 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3740 return SUCCESS;
3742 if (real_error)
3743 return FAILURE;
3746 if (dual_locus_error)
3747 gfc_error (msg, &op1->where, &op2->where);
3748 else
3749 gfc_error (msg, &e->where);
3751 return FAILURE;
3755 /************** Array resolution subroutines **************/
3757 typedef enum
3758 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3759 comparison;
3761 /* Compare two integer expressions. */
3763 static comparison
3764 compare_bound (gfc_expr *a, gfc_expr *b)
3766 int i;
3768 if (a == NULL || a->expr_type != EXPR_CONSTANT
3769 || b == NULL || b->expr_type != EXPR_CONSTANT)
3770 return CMP_UNKNOWN;
3772 /* If either of the types isn't INTEGER, we must have
3773 raised an error earlier. */
3775 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3776 return CMP_UNKNOWN;
3778 i = mpz_cmp (a->value.integer, b->value.integer);
3780 if (i < 0)
3781 return CMP_LT;
3782 if (i > 0)
3783 return CMP_GT;
3784 return CMP_EQ;
3788 /* Compare an integer expression with an integer. */
3790 static comparison
3791 compare_bound_int (gfc_expr *a, int b)
3793 int i;
3795 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3796 return CMP_UNKNOWN;
3798 if (a->ts.type != BT_INTEGER)
3799 gfc_internal_error ("compare_bound_int(): Bad expression");
3801 i = mpz_cmp_si (a->value.integer, b);
3803 if (i < 0)
3804 return CMP_LT;
3805 if (i > 0)
3806 return CMP_GT;
3807 return CMP_EQ;
3811 /* Compare an integer expression with a mpz_t. */
3813 static comparison
3814 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3816 int i;
3818 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3819 return CMP_UNKNOWN;
3821 if (a->ts.type != BT_INTEGER)
3822 gfc_internal_error ("compare_bound_int(): Bad expression");
3824 i = mpz_cmp (a->value.integer, b);
3826 if (i < 0)
3827 return CMP_LT;
3828 if (i > 0)
3829 return CMP_GT;
3830 return CMP_EQ;
3834 /* Compute the last value of a sequence given by a triplet.
3835 Return 0 if it wasn't able to compute the last value, or if the
3836 sequence if empty, and 1 otherwise. */
3838 static int
3839 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3840 gfc_expr *stride, mpz_t last)
3842 mpz_t rem;
3844 if (start == NULL || start->expr_type != EXPR_CONSTANT
3845 || end == NULL || end->expr_type != EXPR_CONSTANT
3846 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3847 return 0;
3849 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3850 || (stride != NULL && stride->ts.type != BT_INTEGER))
3851 return 0;
3853 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3855 if (compare_bound (start, end) == CMP_GT)
3856 return 0;
3857 mpz_set (last, end->value.integer);
3858 return 1;
3861 if (compare_bound_int (stride, 0) == CMP_GT)
3863 /* Stride is positive */
3864 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3865 return 0;
3867 else
3869 /* Stride is negative */
3870 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3871 return 0;
3874 mpz_init (rem);
3875 mpz_sub (rem, end->value.integer, start->value.integer);
3876 mpz_tdiv_r (rem, rem, stride->value.integer);
3877 mpz_sub (last, end->value.integer, rem);
3878 mpz_clear (rem);
3880 return 1;
3884 /* Compare a single dimension of an array reference to the array
3885 specification. */
3887 static gfc_try
3888 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3890 mpz_t last_value;
3892 if (ar->dimen_type[i] == DIMEN_STAR)
3894 gcc_assert (ar->stride[i] == NULL);
3895 /* This implies [*] as [*:] and [*:3] are not possible. */
3896 if (ar->start[i] == NULL)
3898 gcc_assert (ar->end[i] == NULL);
3899 return SUCCESS;
3903 /* Given start, end and stride values, calculate the minimum and
3904 maximum referenced indexes. */
3906 switch (ar->dimen_type[i])
3908 case DIMEN_VECTOR:
3909 break;
3911 case DIMEN_STAR:
3912 case DIMEN_ELEMENT:
3913 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3915 if (i < as->rank)
3916 gfc_warning ("Array reference at %L is out of bounds "
3917 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3918 mpz_get_si (ar->start[i]->value.integer),
3919 mpz_get_si (as->lower[i]->value.integer), i+1);
3920 else
3921 gfc_warning ("Array reference at %L is out of bounds "
3922 "(%ld < %ld) in codimension %d", &ar->c_where[i],
3923 mpz_get_si (ar->start[i]->value.integer),
3924 mpz_get_si (as->lower[i]->value.integer),
3925 i + 1 - as->rank);
3926 return SUCCESS;
3928 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3930 if (i < as->rank)
3931 gfc_warning ("Array reference at %L is out of bounds "
3932 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3933 mpz_get_si (ar->start[i]->value.integer),
3934 mpz_get_si (as->upper[i]->value.integer), i+1);
3935 else
3936 gfc_warning ("Array reference at %L is out of bounds "
3937 "(%ld > %ld) in codimension %d", &ar->c_where[i],
3938 mpz_get_si (ar->start[i]->value.integer),
3939 mpz_get_si (as->upper[i]->value.integer),
3940 i + 1 - as->rank);
3941 return SUCCESS;
3944 break;
3946 case DIMEN_RANGE:
3948 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3949 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3951 comparison comp_start_end = compare_bound (AR_START, AR_END);
3953 /* Check for zero stride, which is not allowed. */
3954 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3956 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3957 return FAILURE;
3960 /* if start == len || (stride > 0 && start < len)
3961 || (stride < 0 && start > len),
3962 then the array section contains at least one element. In this
3963 case, there is an out-of-bounds access if
3964 (start < lower || start > upper). */
3965 if (compare_bound (AR_START, AR_END) == CMP_EQ
3966 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3967 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3968 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3969 && comp_start_end == CMP_GT))
3971 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3973 gfc_warning ("Lower array reference at %L is out of bounds "
3974 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3975 mpz_get_si (AR_START->value.integer),
3976 mpz_get_si (as->lower[i]->value.integer), i+1);
3977 return SUCCESS;
3979 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3981 gfc_warning ("Lower array reference at %L is out of bounds "
3982 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3983 mpz_get_si (AR_START->value.integer),
3984 mpz_get_si (as->upper[i]->value.integer), i+1);
3985 return SUCCESS;
3989 /* If we can compute the highest index of the array section,
3990 then it also has to be between lower and upper. */
3991 mpz_init (last_value);
3992 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3993 last_value))
3995 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3997 gfc_warning ("Upper array reference at %L is out of bounds "
3998 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3999 mpz_get_si (last_value),
4000 mpz_get_si (as->lower[i]->value.integer), i+1);
4001 mpz_clear (last_value);
4002 return SUCCESS;
4004 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4006 gfc_warning ("Upper array reference at %L is out of bounds "
4007 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4008 mpz_get_si (last_value),
4009 mpz_get_si (as->upper[i]->value.integer), i+1);
4010 mpz_clear (last_value);
4011 return SUCCESS;
4014 mpz_clear (last_value);
4016 #undef AR_START
4017 #undef AR_END
4019 break;
4021 default:
4022 gfc_internal_error ("check_dimension(): Bad array reference");
4025 return SUCCESS;
4029 /* Compare an array reference with an array specification. */
4031 static gfc_try
4032 compare_spec_to_ref (gfc_array_ref *ar)
4034 gfc_array_spec *as;
4035 int i;
4037 as = ar->as;
4038 i = as->rank - 1;
4039 /* TODO: Full array sections are only allowed as actual parameters. */
4040 if (as->type == AS_ASSUMED_SIZE
4041 && (/*ar->type == AR_FULL
4042 ||*/ (ar->type == AR_SECTION
4043 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4045 gfc_error ("Rightmost upper bound of assumed size array section "
4046 "not specified at %L", &ar->where);
4047 return FAILURE;
4050 if (ar->type == AR_FULL)
4051 return SUCCESS;
4053 if (as->rank != ar->dimen)
4055 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4056 &ar->where, ar->dimen, as->rank);
4057 return FAILURE;
4060 /* ar->codimen == 0 is a local array. */
4061 if (as->corank != ar->codimen && ar->codimen != 0)
4063 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4064 &ar->where, ar->codimen, as->corank);
4065 return FAILURE;
4068 for (i = 0; i < as->rank; i++)
4069 if (check_dimension (i, ar, as) == FAILURE)
4070 return FAILURE;
4072 /* Local access has no coarray spec. */
4073 if (ar->codimen != 0)
4074 for (i = as->rank; i < as->rank + as->corank; i++)
4076 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate)
4078 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4079 i + 1 - as->rank, &ar->where);
4080 return FAILURE;
4082 if (check_dimension (i, ar, as) == FAILURE)
4083 return FAILURE;
4086 return SUCCESS;
4090 /* Resolve one part of an array index. */
4092 static gfc_try
4093 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4094 int force_index_integer_kind)
4096 gfc_typespec ts;
4098 if (index == NULL)
4099 return SUCCESS;
4101 if (gfc_resolve_expr (index) == FAILURE)
4102 return FAILURE;
4104 if (check_scalar && index->rank != 0)
4106 gfc_error ("Array index at %L must be scalar", &index->where);
4107 return FAILURE;
4110 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4112 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4113 &index->where, gfc_basic_typename (index->ts.type));
4114 return FAILURE;
4117 if (index->ts.type == BT_REAL)
4118 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
4119 &index->where) == FAILURE)
4120 return FAILURE;
4122 if ((index->ts.kind != gfc_index_integer_kind
4123 && force_index_integer_kind)
4124 || index->ts.type != BT_INTEGER)
4126 gfc_clear_ts (&ts);
4127 ts.type = BT_INTEGER;
4128 ts.kind = gfc_index_integer_kind;
4130 gfc_convert_type_warn (index, &ts, 2, 0);
4133 return SUCCESS;
4136 /* Resolve one part of an array index. */
4138 gfc_try
4139 gfc_resolve_index (gfc_expr *index, int check_scalar)
4141 return gfc_resolve_index_1 (index, check_scalar, 1);
4144 /* Resolve a dim argument to an intrinsic function. */
4146 gfc_try
4147 gfc_resolve_dim_arg (gfc_expr *dim)
4149 if (dim == NULL)
4150 return SUCCESS;
4152 if (gfc_resolve_expr (dim) == FAILURE)
4153 return FAILURE;
4155 if (dim->rank != 0)
4157 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4158 return FAILURE;
4162 if (dim->ts.type != BT_INTEGER)
4164 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4165 return FAILURE;
4168 if (dim->ts.kind != gfc_index_integer_kind)
4170 gfc_typespec ts;
4172 gfc_clear_ts (&ts);
4173 ts.type = BT_INTEGER;
4174 ts.kind = gfc_index_integer_kind;
4176 gfc_convert_type_warn (dim, &ts, 2, 0);
4179 return SUCCESS;
4182 /* Given an expression that contains array references, update those array
4183 references to point to the right array specifications. While this is
4184 filled in during matching, this information is difficult to save and load
4185 in a module, so we take care of it here.
4187 The idea here is that the original array reference comes from the
4188 base symbol. We traverse the list of reference structures, setting
4189 the stored reference to references. Component references can
4190 provide an additional array specification. */
4192 static void
4193 find_array_spec (gfc_expr *e)
4195 gfc_array_spec *as;
4196 gfc_component *c;
4197 gfc_symbol *derived;
4198 gfc_ref *ref;
4200 if (e->symtree->n.sym->ts.type == BT_CLASS)
4201 as = CLASS_DATA (e->symtree->n.sym)->as;
4202 else
4203 as = e->symtree->n.sym->as;
4204 derived = NULL;
4206 for (ref = e->ref; ref; ref = ref->next)
4207 switch (ref->type)
4209 case REF_ARRAY:
4210 if (as == NULL)
4211 gfc_internal_error ("find_array_spec(): Missing spec");
4213 ref->u.ar.as = as;
4214 as = NULL;
4215 break;
4217 case REF_COMPONENT:
4218 if (derived == NULL)
4219 derived = e->symtree->n.sym->ts.u.derived;
4221 if (derived->attr.is_class)
4222 derived = derived->components->ts.u.derived;
4224 c = derived->components;
4226 for (; c; c = c->next)
4227 if (c == ref->u.c.component)
4229 /* Track the sequence of component references. */
4230 if (c->ts.type == BT_DERIVED)
4231 derived = c->ts.u.derived;
4232 break;
4235 if (c == NULL)
4236 gfc_internal_error ("find_array_spec(): Component not found");
4238 if (c->attr.dimension)
4240 if (as != NULL)
4241 gfc_internal_error ("find_array_spec(): unused as(1)");
4242 as = c->as;
4245 break;
4247 case REF_SUBSTRING:
4248 break;
4251 if (as != NULL)
4252 gfc_internal_error ("find_array_spec(): unused as(2)");
4256 /* Resolve an array reference. */
4258 static gfc_try
4259 resolve_array_ref (gfc_array_ref *ar)
4261 int i, check_scalar;
4262 gfc_expr *e;
4264 for (i = 0; i < ar->dimen + ar->codimen; i++)
4266 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4268 /* Do not force gfc_index_integer_kind for the start. We can
4269 do fine with any integer kind. This avoids temporary arrays
4270 created for indexing with a vector. */
4271 if (gfc_resolve_index_1 (ar->start[i], check_scalar, 0) == FAILURE)
4272 return FAILURE;
4273 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4274 return FAILURE;
4275 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4276 return FAILURE;
4278 e = ar->start[i];
4280 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4281 switch (e->rank)
4283 case 0:
4284 ar->dimen_type[i] = DIMEN_ELEMENT;
4285 break;
4287 case 1:
4288 ar->dimen_type[i] = DIMEN_VECTOR;
4289 if (e->expr_type == EXPR_VARIABLE
4290 && e->symtree->n.sym->ts.type == BT_DERIVED)
4291 ar->start[i] = gfc_get_parentheses (e);
4292 break;
4294 default:
4295 gfc_error ("Array index at %L is an array of rank %d",
4296 &ar->c_where[i], e->rank);
4297 return FAILURE;
4301 if (ar->type == AR_FULL && ar->as->rank == 0)
4302 ar->type = AR_ELEMENT;
4304 /* If the reference type is unknown, figure out what kind it is. */
4306 if (ar->type == AR_UNKNOWN)
4308 ar->type = AR_ELEMENT;
4309 for (i = 0; i < ar->dimen; i++)
4310 if (ar->dimen_type[i] == DIMEN_RANGE
4311 || ar->dimen_type[i] == DIMEN_VECTOR)
4313 ar->type = AR_SECTION;
4314 break;
4318 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4319 return FAILURE;
4321 return SUCCESS;
4325 static gfc_try
4326 resolve_substring (gfc_ref *ref)
4328 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4330 if (ref->u.ss.start != NULL)
4332 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4333 return FAILURE;
4335 if (ref->u.ss.start->ts.type != BT_INTEGER)
4337 gfc_error ("Substring start index at %L must be of type INTEGER",
4338 &ref->u.ss.start->where);
4339 return FAILURE;
4342 if (ref->u.ss.start->rank != 0)
4344 gfc_error ("Substring start index at %L must be scalar",
4345 &ref->u.ss.start->where);
4346 return FAILURE;
4349 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4350 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4351 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4353 gfc_error ("Substring start index at %L is less than one",
4354 &ref->u.ss.start->where);
4355 return FAILURE;
4359 if (ref->u.ss.end != NULL)
4361 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4362 return FAILURE;
4364 if (ref->u.ss.end->ts.type != BT_INTEGER)
4366 gfc_error ("Substring end index at %L must be of type INTEGER",
4367 &ref->u.ss.end->where);
4368 return FAILURE;
4371 if (ref->u.ss.end->rank != 0)
4373 gfc_error ("Substring end index at %L must be scalar",
4374 &ref->u.ss.end->where);
4375 return FAILURE;
4378 if (ref->u.ss.length != NULL
4379 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4380 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4381 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4383 gfc_error ("Substring end index at %L exceeds the string length",
4384 &ref->u.ss.start->where);
4385 return FAILURE;
4388 if (compare_bound_mpz_t (ref->u.ss.end,
4389 gfc_integer_kinds[k].huge) == CMP_GT
4390 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4391 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4393 gfc_error ("Substring end index at %L is too large",
4394 &ref->u.ss.end->where);
4395 return FAILURE;
4399 return SUCCESS;
4403 /* This function supplies missing substring charlens. */
4405 void
4406 gfc_resolve_substring_charlen (gfc_expr *e)
4408 gfc_ref *char_ref;
4409 gfc_expr *start, *end;
4411 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4412 if (char_ref->type == REF_SUBSTRING)
4413 break;
4415 if (!char_ref)
4416 return;
4418 gcc_assert (char_ref->next == NULL);
4420 if (e->ts.u.cl)
4422 if (e->ts.u.cl->length)
4423 gfc_free_expr (e->ts.u.cl->length);
4424 else if (e->expr_type == EXPR_VARIABLE
4425 && e->symtree->n.sym->attr.dummy)
4426 return;
4429 e->ts.type = BT_CHARACTER;
4430 e->ts.kind = gfc_default_character_kind;
4432 if (!e->ts.u.cl)
4433 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4435 if (char_ref->u.ss.start)
4436 start = gfc_copy_expr (char_ref->u.ss.start);
4437 else
4438 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4440 if (char_ref->u.ss.end)
4441 end = gfc_copy_expr (char_ref->u.ss.end);
4442 else if (e->expr_type == EXPR_VARIABLE)
4443 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4444 else
4445 end = NULL;
4447 if (!start || !end)
4448 return;
4450 /* Length = (end - start +1). */
4451 e->ts.u.cl->length = gfc_subtract (end, start);
4452 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4453 gfc_get_int_expr (gfc_default_integer_kind,
4454 NULL, 1));
4456 e->ts.u.cl->length->ts.type = BT_INTEGER;
4457 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4459 /* Make sure that the length is simplified. */
4460 gfc_simplify_expr (e->ts.u.cl->length, 1);
4461 gfc_resolve_expr (e->ts.u.cl->length);
4465 /* Resolve subtype references. */
4467 static gfc_try
4468 resolve_ref (gfc_expr *expr)
4470 int current_part_dimension, n_components, seen_part_dimension;
4471 gfc_ref *ref;
4473 for (ref = expr->ref; ref; ref = ref->next)
4474 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4476 find_array_spec (expr);
4477 break;
4480 for (ref = expr->ref; ref; ref = ref->next)
4481 switch (ref->type)
4483 case REF_ARRAY:
4484 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4485 return FAILURE;
4486 break;
4488 case REF_COMPONENT:
4489 break;
4491 case REF_SUBSTRING:
4492 resolve_substring (ref);
4493 break;
4496 /* Check constraints on part references. */
4498 current_part_dimension = 0;
4499 seen_part_dimension = 0;
4500 n_components = 0;
4502 for (ref = expr->ref; ref; ref = ref->next)
4504 switch (ref->type)
4506 case REF_ARRAY:
4507 switch (ref->u.ar.type)
4509 case AR_FULL:
4510 /* Coarray scalar. */
4511 if (ref->u.ar.as->rank == 0)
4513 current_part_dimension = 0;
4514 break;
4516 /* Fall through. */
4517 case AR_SECTION:
4518 current_part_dimension = 1;
4519 break;
4521 case AR_ELEMENT:
4522 current_part_dimension = 0;
4523 break;
4525 case AR_UNKNOWN:
4526 gfc_internal_error ("resolve_ref(): Bad array reference");
4529 break;
4531 case REF_COMPONENT:
4532 if (current_part_dimension || seen_part_dimension)
4534 /* F03:C614. */
4535 if (ref->u.c.component->attr.pointer
4536 || ref->u.c.component->attr.proc_pointer)
4538 gfc_error ("Component to the right of a part reference "
4539 "with nonzero rank must not have the POINTER "
4540 "attribute at %L", &expr->where);
4541 return FAILURE;
4543 else if (ref->u.c.component->attr.allocatable)
4545 gfc_error ("Component to the right of a part reference "
4546 "with nonzero rank must not have the ALLOCATABLE "
4547 "attribute at %L", &expr->where);
4548 return FAILURE;
4552 n_components++;
4553 break;
4555 case REF_SUBSTRING:
4556 break;
4559 if (((ref->type == REF_COMPONENT && n_components > 1)
4560 || ref->next == NULL)
4561 && current_part_dimension
4562 && seen_part_dimension)
4564 gfc_error ("Two or more part references with nonzero rank must "
4565 "not be specified at %L", &expr->where);
4566 return FAILURE;
4569 if (ref->type == REF_COMPONENT)
4571 if (current_part_dimension)
4572 seen_part_dimension = 1;
4574 /* reset to make sure */
4575 current_part_dimension = 0;
4579 return SUCCESS;
4583 /* Given an expression, determine its shape. This is easier than it sounds.
4584 Leaves the shape array NULL if it is not possible to determine the shape. */
4586 static void
4587 expression_shape (gfc_expr *e)
4589 mpz_t array[GFC_MAX_DIMENSIONS];
4590 int i;
4592 if (e->rank == 0 || e->shape != NULL)
4593 return;
4595 for (i = 0; i < e->rank; i++)
4596 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4597 goto fail;
4599 e->shape = gfc_get_shape (e->rank);
4601 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4603 return;
4605 fail:
4606 for (i--; i >= 0; i--)
4607 mpz_clear (array[i]);
4611 /* Given a variable expression node, compute the rank of the expression by
4612 examining the base symbol and any reference structures it may have. */
4614 static void
4615 expression_rank (gfc_expr *e)
4617 gfc_ref *ref;
4618 int i, rank;
4620 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4621 could lead to serious confusion... */
4622 gcc_assert (e->expr_type != EXPR_COMPCALL);
4624 if (e->ref == NULL)
4626 if (e->expr_type == EXPR_ARRAY)
4627 goto done;
4628 /* Constructors can have a rank different from one via RESHAPE(). */
4630 if (e->symtree == NULL)
4632 e->rank = 0;
4633 goto done;
4636 e->rank = (e->symtree->n.sym->as == NULL)
4637 ? 0 : e->symtree->n.sym->as->rank;
4638 goto done;
4641 rank = 0;
4643 for (ref = e->ref; ref; ref = ref->next)
4645 if (ref->type != REF_ARRAY)
4646 continue;
4648 if (ref->u.ar.type == AR_FULL)
4650 rank = ref->u.ar.as->rank;
4651 break;
4654 if (ref->u.ar.type == AR_SECTION)
4656 /* Figure out the rank of the section. */
4657 if (rank != 0)
4658 gfc_internal_error ("expression_rank(): Two array specs");
4660 for (i = 0; i < ref->u.ar.dimen; i++)
4661 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4662 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4663 rank++;
4665 break;
4669 e->rank = rank;
4671 done:
4672 expression_shape (e);
4676 /* Resolve a variable expression. */
4678 static gfc_try
4679 resolve_variable (gfc_expr *e)
4681 gfc_symbol *sym;
4682 gfc_try t;
4684 t = SUCCESS;
4686 if (e->symtree == NULL)
4687 return FAILURE;
4689 if (e->ref && resolve_ref (e) == FAILURE)
4690 return FAILURE;
4692 sym = e->symtree->n.sym;
4693 if (sym->attr.flavor == FL_PROCEDURE
4694 && (!sym->attr.function
4695 || (sym->attr.function && sym->result
4696 && sym->result->attr.proc_pointer
4697 && !sym->result->attr.function)))
4699 e->ts.type = BT_PROCEDURE;
4700 goto resolve_procedure;
4703 if (sym->ts.type != BT_UNKNOWN)
4704 gfc_variable_attr (e, &e->ts);
4705 else
4707 /* Must be a simple variable reference. */
4708 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4709 return FAILURE;
4710 e->ts = sym->ts;
4713 if (check_assumed_size_reference (sym, e))
4714 return FAILURE;
4716 /* Deal with forward references to entries during resolve_code, to
4717 satisfy, at least partially, 12.5.2.5. */
4718 if (gfc_current_ns->entries
4719 && current_entry_id == sym->entry_id
4720 && cs_base
4721 && cs_base->current
4722 && cs_base->current->op != EXEC_ENTRY)
4724 gfc_entry_list *entry;
4725 gfc_formal_arglist *formal;
4726 int n;
4727 bool seen;
4729 /* If the symbol is a dummy... */
4730 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4732 entry = gfc_current_ns->entries;
4733 seen = false;
4735 /* ...test if the symbol is a parameter of previous entries. */
4736 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4737 for (formal = entry->sym->formal; formal; formal = formal->next)
4739 if (formal->sym && sym->name == formal->sym->name)
4740 seen = true;
4743 /* If it has not been seen as a dummy, this is an error. */
4744 if (!seen)
4746 if (specification_expr)
4747 gfc_error ("Variable '%s', used in a specification expression"
4748 ", is referenced at %L before the ENTRY statement "
4749 "in which it is a parameter",
4750 sym->name, &cs_base->current->loc);
4751 else
4752 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4753 "statement in which it is a parameter",
4754 sym->name, &cs_base->current->loc);
4755 t = FAILURE;
4759 /* Now do the same check on the specification expressions. */
4760 specification_expr = 1;
4761 if (sym->ts.type == BT_CHARACTER
4762 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
4763 t = FAILURE;
4765 if (sym->as)
4766 for (n = 0; n < sym->as->rank; n++)
4768 specification_expr = 1;
4769 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4770 t = FAILURE;
4771 specification_expr = 1;
4772 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4773 t = FAILURE;
4775 specification_expr = 0;
4777 if (t == SUCCESS)
4778 /* Update the symbol's entry level. */
4779 sym->entry_id = current_entry_id + 1;
4782 /* If a symbol has been host_associated mark it. This is used latter,
4783 to identify if aliasing is possible via host association. */
4784 if (sym->attr.flavor == FL_VARIABLE
4785 && gfc_current_ns->parent
4786 && (gfc_current_ns->parent == sym->ns
4787 || (gfc_current_ns->parent->parent
4788 && gfc_current_ns->parent->parent == sym->ns)))
4789 sym->attr.host_assoc = 1;
4791 resolve_procedure:
4792 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
4793 t = FAILURE;
4795 /* F2008, C617 and C1229. */
4796 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
4797 && gfc_is_coindexed (e))
4799 gfc_ref *ref, *ref2 = NULL;
4801 if (e->ts.type == BT_CLASS)
4803 gfc_error ("Polymorphic subobject of coindexed object at %L",
4804 &e->where);
4805 t = FAILURE;
4808 for (ref = e->ref; ref; ref = ref->next)
4810 if (ref->type == REF_COMPONENT)
4811 ref2 = ref;
4812 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4813 break;
4816 for ( ; ref; ref = ref->next)
4817 if (ref->type == REF_COMPONENT)
4818 break;
4820 /* Expression itself is coindexed object. */
4821 if (ref == NULL)
4823 gfc_component *c;
4824 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
4825 for ( ; c; c = c->next)
4826 if (c->attr.allocatable && c->ts.type == BT_CLASS)
4828 gfc_error ("Coindexed object with polymorphic allocatable "
4829 "subcomponent at %L", &e->where);
4830 t = FAILURE;
4831 break;
4836 return t;
4840 /* Checks to see that the correct symbol has been host associated.
4841 The only situation where this arises is that in which a twice
4842 contained function is parsed after the host association is made.
4843 Therefore, on detecting this, change the symbol in the expression
4844 and convert the array reference into an actual arglist if the old
4845 symbol is a variable. */
4846 static bool
4847 check_host_association (gfc_expr *e)
4849 gfc_symbol *sym, *old_sym;
4850 gfc_symtree *st;
4851 int n;
4852 gfc_ref *ref;
4853 gfc_actual_arglist *arg, *tail = NULL;
4854 bool retval = e->expr_type == EXPR_FUNCTION;
4856 /* If the expression is the result of substitution in
4857 interface.c(gfc_extend_expr) because there is no way in
4858 which the host association can be wrong. */
4859 if (e->symtree == NULL
4860 || e->symtree->n.sym == NULL
4861 || e->user_operator)
4862 return retval;
4864 old_sym = e->symtree->n.sym;
4866 if (gfc_current_ns->parent
4867 && old_sym->ns != gfc_current_ns)
4869 /* Use the 'USE' name so that renamed module symbols are
4870 correctly handled. */
4871 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
4873 if (sym && old_sym != sym
4874 && sym->ts.type == old_sym->ts.type
4875 && sym->attr.flavor == FL_PROCEDURE
4876 && sym->attr.contained)
4878 /* Clear the shape, since it might not be valid. */
4879 if (e->shape != NULL)
4881 for (n = 0; n < e->rank; n++)
4882 mpz_clear (e->shape[n]);
4884 gfc_free (e->shape);
4887 /* Give the expression the right symtree! */
4888 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
4889 gcc_assert (st != NULL);
4891 if (old_sym->attr.flavor == FL_PROCEDURE
4892 || e->expr_type == EXPR_FUNCTION)
4894 /* Original was function so point to the new symbol, since
4895 the actual argument list is already attached to the
4896 expression. */
4897 e->value.function.esym = NULL;
4898 e->symtree = st;
4900 else
4902 /* Original was variable so convert array references into
4903 an actual arglist. This does not need any checking now
4904 since gfc_resolve_function will take care of it. */
4905 e->value.function.actual = NULL;
4906 e->expr_type = EXPR_FUNCTION;
4907 e->symtree = st;
4909 /* Ambiguity will not arise if the array reference is not
4910 the last reference. */
4911 for (ref = e->ref; ref; ref = ref->next)
4912 if (ref->type == REF_ARRAY && ref->next == NULL)
4913 break;
4915 gcc_assert (ref->type == REF_ARRAY);
4917 /* Grab the start expressions from the array ref and
4918 copy them into actual arguments. */
4919 for (n = 0; n < ref->u.ar.dimen; n++)
4921 arg = gfc_get_actual_arglist ();
4922 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4923 if (e->value.function.actual == NULL)
4924 tail = e->value.function.actual = arg;
4925 else
4927 tail->next = arg;
4928 tail = arg;
4932 /* Dump the reference list and set the rank. */
4933 gfc_free_ref_list (e->ref);
4934 e->ref = NULL;
4935 e->rank = sym->as ? sym->as->rank : 0;
4938 gfc_resolve_expr (e);
4939 sym->refs++;
4942 /* This might have changed! */
4943 return e->expr_type == EXPR_FUNCTION;
4947 static void
4948 gfc_resolve_character_operator (gfc_expr *e)
4950 gfc_expr *op1 = e->value.op.op1;
4951 gfc_expr *op2 = e->value.op.op2;
4952 gfc_expr *e1 = NULL;
4953 gfc_expr *e2 = NULL;
4955 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
4957 if (op1->ts.u.cl && op1->ts.u.cl->length)
4958 e1 = gfc_copy_expr (op1->ts.u.cl->length);
4959 else if (op1->expr_type == EXPR_CONSTANT)
4960 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
4961 op1->value.character.length);
4963 if (op2->ts.u.cl && op2->ts.u.cl->length)
4964 e2 = gfc_copy_expr (op2->ts.u.cl->length);
4965 else if (op2->expr_type == EXPR_CONSTANT)
4966 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
4967 op2->value.character.length);
4969 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4971 if (!e1 || !e2)
4972 return;
4974 e->ts.u.cl->length = gfc_add (e1, e2);
4975 e->ts.u.cl->length->ts.type = BT_INTEGER;
4976 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4977 gfc_simplify_expr (e->ts.u.cl->length, 0);
4978 gfc_resolve_expr (e->ts.u.cl->length);
4980 return;
4984 /* Ensure that an character expression has a charlen and, if possible, a
4985 length expression. */
4987 static void
4988 fixup_charlen (gfc_expr *e)
4990 /* The cases fall through so that changes in expression type and the need
4991 for multiple fixes are picked up. In all circumstances, a charlen should
4992 be available for the middle end to hang a backend_decl on. */
4993 switch (e->expr_type)
4995 case EXPR_OP:
4996 gfc_resolve_character_operator (e);
4998 case EXPR_ARRAY:
4999 if (e->expr_type == EXPR_ARRAY)
5000 gfc_resolve_character_array_constructor (e);
5002 case EXPR_SUBSTRING:
5003 if (!e->ts.u.cl && e->ref)
5004 gfc_resolve_substring_charlen (e);
5006 default:
5007 if (!e->ts.u.cl)
5008 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5010 break;
5015 /* Update an actual argument to include the passed-object for type-bound
5016 procedures at the right position. */
5018 static gfc_actual_arglist*
5019 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5020 const char *name)
5022 gcc_assert (argpos > 0);
5024 if (argpos == 1)
5026 gfc_actual_arglist* result;
5028 result = gfc_get_actual_arglist ();
5029 result->expr = po;
5030 result->next = lst;
5031 if (name)
5032 result->name = name;
5034 return result;
5037 if (lst)
5038 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5039 else
5040 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5041 return lst;
5045 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5047 static gfc_expr*
5048 extract_compcall_passed_object (gfc_expr* e)
5050 gfc_expr* po;
5052 gcc_assert (e->expr_type == EXPR_COMPCALL);
5054 if (e->value.compcall.base_object)
5055 po = gfc_copy_expr (e->value.compcall.base_object);
5056 else
5058 po = gfc_get_expr ();
5059 po->expr_type = EXPR_VARIABLE;
5060 po->symtree = e->symtree;
5061 po->ref = gfc_copy_ref (e->ref);
5062 po->where = e->where;
5065 if (gfc_resolve_expr (po) == FAILURE)
5066 return NULL;
5068 return po;
5072 /* Update the arglist of an EXPR_COMPCALL expression to include the
5073 passed-object. */
5075 static gfc_try
5076 update_compcall_arglist (gfc_expr* e)
5078 gfc_expr* po;
5079 gfc_typebound_proc* tbp;
5081 tbp = e->value.compcall.tbp;
5083 if (tbp->error)
5084 return FAILURE;
5086 po = extract_compcall_passed_object (e);
5087 if (!po)
5088 return FAILURE;
5090 if (tbp->nopass || e->value.compcall.ignore_pass)
5092 gfc_free_expr (po);
5093 return SUCCESS;
5096 gcc_assert (tbp->pass_arg_num > 0);
5097 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5098 tbp->pass_arg_num,
5099 tbp->pass_arg);
5101 return SUCCESS;
5105 /* Extract the passed object from a PPC call (a copy of it). */
5107 static gfc_expr*
5108 extract_ppc_passed_object (gfc_expr *e)
5110 gfc_expr *po;
5111 gfc_ref **ref;
5113 po = gfc_get_expr ();
5114 po->expr_type = EXPR_VARIABLE;
5115 po->symtree = e->symtree;
5116 po->ref = gfc_copy_ref (e->ref);
5117 po->where = e->where;
5119 /* Remove PPC reference. */
5120 ref = &po->ref;
5121 while ((*ref)->next)
5122 ref = &(*ref)->next;
5123 gfc_free_ref_list (*ref);
5124 *ref = NULL;
5126 if (gfc_resolve_expr (po) == FAILURE)
5127 return NULL;
5129 return po;
5133 /* Update the actual arglist of a procedure pointer component to include the
5134 passed-object. */
5136 static gfc_try
5137 update_ppc_arglist (gfc_expr* e)
5139 gfc_expr* po;
5140 gfc_component *ppc;
5141 gfc_typebound_proc* tb;
5143 if (!gfc_is_proc_ptr_comp (e, &ppc))
5144 return FAILURE;
5146 tb = ppc->tb;
5148 if (tb->error)
5149 return FAILURE;
5150 else if (tb->nopass)
5151 return SUCCESS;
5153 po = extract_ppc_passed_object (e);
5154 if (!po)
5155 return FAILURE;
5157 if (po->rank > 0)
5159 gfc_error ("Passed-object at %L must be scalar", &e->where);
5160 return FAILURE;
5163 gcc_assert (tb->pass_arg_num > 0);
5164 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5165 tb->pass_arg_num,
5166 tb->pass_arg);
5168 return SUCCESS;
5172 /* Check that the object a TBP is called on is valid, i.e. it must not be
5173 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5175 static gfc_try
5176 check_typebound_baseobject (gfc_expr* e)
5178 gfc_expr* base;
5180 base = extract_compcall_passed_object (e);
5181 if (!base)
5182 return FAILURE;
5184 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5186 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5188 gfc_error ("Base object for type-bound procedure call at %L is of"
5189 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5190 return FAILURE;
5193 /* If the procedure called is NOPASS, the base object must be scalar. */
5194 if (e->value.compcall.tbp->nopass && base->rank > 0)
5196 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5197 " be scalar", &e->where);
5198 return FAILURE;
5201 /* FIXME: Remove once PR 41177 (this problem) is fixed completely. */
5202 if (base->rank > 0)
5204 gfc_error ("Non-scalar base object at %L currently not implemented",
5205 &e->where);
5206 return FAILURE;
5209 return SUCCESS;
5213 /* Resolve a call to a type-bound procedure, either function or subroutine,
5214 statically from the data in an EXPR_COMPCALL expression. The adapted
5215 arglist and the target-procedure symtree are returned. */
5217 static gfc_try
5218 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5219 gfc_actual_arglist** actual)
5221 gcc_assert (e->expr_type == EXPR_COMPCALL);
5222 gcc_assert (!e->value.compcall.tbp->is_generic);
5224 /* Update the actual arglist for PASS. */
5225 if (update_compcall_arglist (e) == FAILURE)
5226 return FAILURE;
5228 *actual = e->value.compcall.actual;
5229 *target = e->value.compcall.tbp->u.specific;
5231 gfc_free_ref_list (e->ref);
5232 e->ref = NULL;
5233 e->value.compcall.actual = NULL;
5235 return SUCCESS;
5239 /* Get the ultimate declared type from an expression. In addition,
5240 return the last class/derived type reference and the copy of the
5241 reference list. */
5242 static gfc_symbol*
5243 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5244 gfc_expr *e)
5246 gfc_symbol *declared;
5247 gfc_ref *ref;
5249 declared = NULL;
5250 if (class_ref)
5251 *class_ref = NULL;
5252 if (new_ref)
5253 *new_ref = gfc_copy_ref (e->ref);
5255 for (ref = e->ref; ref; ref = ref->next)
5257 if (ref->type != REF_COMPONENT)
5258 continue;
5260 if (ref->u.c.component->ts.type == BT_CLASS
5261 || ref->u.c.component->ts.type == BT_DERIVED)
5263 declared = ref->u.c.component->ts.u.derived;
5264 if (class_ref)
5265 *class_ref = ref;
5269 if (declared == NULL)
5270 declared = e->symtree->n.sym->ts.u.derived;
5272 return declared;
5276 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5277 which of the specific bindings (if any) matches the arglist and transform
5278 the expression into a call of that binding. */
5280 static gfc_try
5281 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5283 gfc_typebound_proc* genproc;
5284 const char* genname;
5285 gfc_symtree *st;
5286 gfc_symbol *derived;
5288 gcc_assert (e->expr_type == EXPR_COMPCALL);
5289 genname = e->value.compcall.name;
5290 genproc = e->value.compcall.tbp;
5292 if (!genproc->is_generic)
5293 return SUCCESS;
5295 /* Try the bindings on this type and in the inheritance hierarchy. */
5296 for (; genproc; genproc = genproc->overridden)
5298 gfc_tbp_generic* g;
5300 gcc_assert (genproc->is_generic);
5301 for (g = genproc->u.generic; g; g = g->next)
5303 gfc_symbol* target;
5304 gfc_actual_arglist* args;
5305 bool matches;
5307 gcc_assert (g->specific);
5309 if (g->specific->error)
5310 continue;
5312 target = g->specific->u.specific->n.sym;
5314 /* Get the right arglist by handling PASS/NOPASS. */
5315 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5316 if (!g->specific->nopass)
5318 gfc_expr* po;
5319 po = extract_compcall_passed_object (e);
5320 if (!po)
5321 return FAILURE;
5323 gcc_assert (g->specific->pass_arg_num > 0);
5324 gcc_assert (!g->specific->error);
5325 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5326 g->specific->pass_arg);
5328 resolve_actual_arglist (args, target->attr.proc,
5329 is_external_proc (target) && !target->formal);
5331 /* Check if this arglist matches the formal. */
5332 matches = gfc_arglist_matches_symbol (&args, target);
5334 /* Clean up and break out of the loop if we've found it. */
5335 gfc_free_actual_arglist (args);
5336 if (matches)
5338 e->value.compcall.tbp = g->specific;
5339 genname = g->specific_st->name;
5340 /* Pass along the name for CLASS methods, where the vtab
5341 procedure pointer component has to be referenced. */
5342 if (name)
5343 *name = genname;
5344 goto success;
5349 /* Nothing matching found! */
5350 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5351 " '%s' at %L", genname, &e->where);
5352 return FAILURE;
5354 success:
5355 /* Make sure that we have the right specific instance for the name. */
5356 derived = get_declared_from_expr (NULL, NULL, e);
5358 st = gfc_find_typebound_proc (derived, NULL, genname, false, &e->where);
5359 if (st)
5360 e->value.compcall.tbp = st->n.tb;
5362 return SUCCESS;
5366 /* Resolve a call to a type-bound subroutine. */
5368 static gfc_try
5369 resolve_typebound_call (gfc_code* c, const char **name)
5371 gfc_actual_arglist* newactual;
5372 gfc_symtree* target;
5374 /* Check that's really a SUBROUTINE. */
5375 if (!c->expr1->value.compcall.tbp->subroutine)
5377 gfc_error ("'%s' at %L should be a SUBROUTINE",
5378 c->expr1->value.compcall.name, &c->loc);
5379 return FAILURE;
5382 if (check_typebound_baseobject (c->expr1) == FAILURE)
5383 return FAILURE;
5385 /* Pass along the name for CLASS methods, where the vtab
5386 procedure pointer component has to be referenced. */
5387 if (name)
5388 *name = c->expr1->value.compcall.name;
5390 if (resolve_typebound_generic_call (c->expr1, name) == FAILURE)
5391 return FAILURE;
5393 /* Transform into an ordinary EXEC_CALL for now. */
5395 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5396 return FAILURE;
5398 c->ext.actual = newactual;
5399 c->symtree = target;
5400 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5402 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5404 gfc_free_expr (c->expr1);
5405 c->expr1 = gfc_get_expr ();
5406 c->expr1->expr_type = EXPR_FUNCTION;
5407 c->expr1->symtree = target;
5408 c->expr1->where = c->loc;
5410 return resolve_call (c);
5414 /* Resolve a component-call expression. */
5415 static gfc_try
5416 resolve_compcall (gfc_expr* e, const char **name)
5418 gfc_actual_arglist* newactual;
5419 gfc_symtree* target;
5421 /* Check that's really a FUNCTION. */
5422 if (!e->value.compcall.tbp->function)
5424 gfc_error ("'%s' at %L should be a FUNCTION",
5425 e->value.compcall.name, &e->where);
5426 return FAILURE;
5429 /* These must not be assign-calls! */
5430 gcc_assert (!e->value.compcall.assign);
5432 if (check_typebound_baseobject (e) == FAILURE)
5433 return FAILURE;
5435 /* Pass along the name for CLASS methods, where the vtab
5436 procedure pointer component has to be referenced. */
5437 if (name)
5438 *name = e->value.compcall.name;
5440 if (resolve_typebound_generic_call (e, name) == FAILURE)
5441 return FAILURE;
5442 gcc_assert (!e->value.compcall.tbp->is_generic);
5444 /* Take the rank from the function's symbol. */
5445 if (e->value.compcall.tbp->u.specific->n.sym->as)
5446 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5448 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5449 arglist to the TBP's binding target. */
5451 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5452 return FAILURE;
5454 e->value.function.actual = newactual;
5455 e->value.function.name = NULL;
5456 e->value.function.esym = target->n.sym;
5457 e->value.function.isym = NULL;
5458 e->symtree = target;
5459 e->ts = target->n.sym->ts;
5460 e->expr_type = EXPR_FUNCTION;
5462 /* Resolution is not necessary if this is a class subroutine; this
5463 function only has to identify the specific proc. Resolution of
5464 the call will be done next in resolve_typebound_call. */
5465 return gfc_resolve_expr (e);
5470 /* Resolve a typebound function, or 'method'. First separate all
5471 the non-CLASS references by calling resolve_compcall directly. */
5473 static gfc_try
5474 resolve_typebound_function (gfc_expr* e)
5476 gfc_symbol *declared;
5477 gfc_component *c;
5478 gfc_ref *new_ref;
5479 gfc_ref *class_ref;
5480 gfc_symtree *st;
5481 const char *name;
5482 gfc_typespec ts;
5484 st = e->symtree;
5485 if (st == NULL)
5486 return resolve_compcall (e, NULL);
5488 if (resolve_ref (e) == FAILURE)
5489 return FAILURE;
5491 /* Get the CLASS declared type. */
5492 declared = get_declared_from_expr (&class_ref, &new_ref, e);
5494 /* Weed out cases of the ultimate component being a derived type. */
5495 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5496 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5498 gfc_free_ref_list (new_ref);
5499 return resolve_compcall (e, NULL);
5502 c = gfc_find_component (declared, "$data", true, true);
5503 declared = c->ts.u.derived;
5505 /* Treat the call as if it is a typebound procedure, in order to roll
5506 out the correct name for the specific function. */
5507 if (resolve_compcall (e, &name) == FAILURE)
5508 return FAILURE;
5509 ts = e->ts;
5511 /* Then convert the expression to a procedure pointer component call. */
5512 e->value.function.esym = NULL;
5513 e->symtree = st;
5515 if (new_ref)
5516 e->ref = new_ref;
5518 /* '$vptr' points to the vtab, which contains the procedure pointers. */
5519 gfc_add_component_ref (e, "$vptr");
5520 gfc_add_component_ref (e, name);
5522 /* Recover the typespec for the expression. This is really only
5523 necessary for generic procedures, where the additional call
5524 to gfc_add_component_ref seems to throw the collection of the
5525 correct typespec. */
5526 e->ts = ts;
5527 return SUCCESS;
5530 /* Resolve a typebound subroutine, or 'method'. First separate all
5531 the non-CLASS references by calling resolve_typebound_call
5532 directly. */
5534 static gfc_try
5535 resolve_typebound_subroutine (gfc_code *code)
5537 gfc_ref *new_ref;
5538 gfc_ref *class_ref;
5539 gfc_symtree *st;
5540 const char *name;
5541 gfc_typespec ts;
5543 st = code->expr1->symtree;
5544 if (st == NULL)
5545 return resolve_typebound_call (code, NULL);
5547 if (resolve_ref (code->expr1) == FAILURE)
5548 return FAILURE;
5550 /* Get the CLASS declared type. */
5551 get_declared_from_expr (&class_ref, &new_ref, code->expr1);
5553 /* Weed out cases of the ultimate component being a derived type. */
5554 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5555 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5557 gfc_free_ref_list (new_ref);
5558 return resolve_typebound_call (code, NULL);
5561 if (resolve_typebound_call (code, &name) == FAILURE)
5562 return FAILURE;
5563 ts = code->expr1->ts;
5565 /* Then convert the expression to a procedure pointer component call. */
5566 code->expr1->value.function.esym = NULL;
5567 code->expr1->symtree = st;
5569 if (new_ref)
5570 code->expr1->ref = new_ref;
5572 /* '$vptr' points to the vtab, which contains the procedure pointers. */
5573 gfc_add_component_ref (code->expr1, "$vptr");
5574 gfc_add_component_ref (code->expr1, name);
5576 /* Recover the typespec for the expression. This is really only
5577 necessary for generic procedures, where the additional call
5578 to gfc_add_component_ref seems to throw the collection of the
5579 correct typespec. */
5580 code->expr1->ts = ts;
5581 return SUCCESS;
5585 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5587 static gfc_try
5588 resolve_ppc_call (gfc_code* c)
5590 gfc_component *comp;
5591 bool b;
5593 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
5594 gcc_assert (b);
5596 c->resolved_sym = c->expr1->symtree->n.sym;
5597 c->expr1->expr_type = EXPR_VARIABLE;
5599 if (!comp->attr.subroutine)
5600 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5602 if (resolve_ref (c->expr1) == FAILURE)
5603 return FAILURE;
5605 if (update_ppc_arglist (c->expr1) == FAILURE)
5606 return FAILURE;
5608 c->ext.actual = c->expr1->value.compcall.actual;
5610 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5611 comp->formal == NULL) == FAILURE)
5612 return FAILURE;
5614 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5616 return SUCCESS;
5620 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5622 static gfc_try
5623 resolve_expr_ppc (gfc_expr* e)
5625 gfc_component *comp;
5626 bool b;
5628 b = gfc_is_proc_ptr_comp (e, &comp);
5629 gcc_assert (b);
5631 /* Convert to EXPR_FUNCTION. */
5632 e->expr_type = EXPR_FUNCTION;
5633 e->value.function.isym = NULL;
5634 e->value.function.actual = e->value.compcall.actual;
5635 e->ts = comp->ts;
5636 if (comp->as != NULL)
5637 e->rank = comp->as->rank;
5639 if (!comp->attr.function)
5640 gfc_add_function (&comp->attr, comp->name, &e->where);
5642 if (resolve_ref (e) == FAILURE)
5643 return FAILURE;
5645 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5646 comp->formal == NULL) == FAILURE)
5647 return FAILURE;
5649 if (update_ppc_arglist (e) == FAILURE)
5650 return FAILURE;
5652 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
5654 return SUCCESS;
5658 static bool
5659 gfc_is_expandable_expr (gfc_expr *e)
5661 gfc_constructor *con;
5663 if (e->expr_type == EXPR_ARRAY)
5665 /* Traverse the constructor looking for variables that are flavor
5666 parameter. Parameters must be expanded since they are fully used at
5667 compile time. */
5668 con = gfc_constructor_first (e->value.constructor);
5669 for (; con; con = gfc_constructor_next (con))
5671 if (con->expr->expr_type == EXPR_VARIABLE
5672 && con->expr->symtree
5673 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
5674 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
5675 return true;
5676 if (con->expr->expr_type == EXPR_ARRAY
5677 && gfc_is_expandable_expr (con->expr))
5678 return true;
5682 return false;
5685 /* Resolve an expression. That is, make sure that types of operands agree
5686 with their operators, intrinsic operators are converted to function calls
5687 for overloaded types and unresolved function references are resolved. */
5689 gfc_try
5690 gfc_resolve_expr (gfc_expr *e)
5692 gfc_try t;
5693 bool inquiry_save;
5695 if (e == NULL)
5696 return SUCCESS;
5698 /* inquiry_argument only applies to variables. */
5699 inquiry_save = inquiry_argument;
5700 if (e->expr_type != EXPR_VARIABLE)
5701 inquiry_argument = false;
5703 switch (e->expr_type)
5705 case EXPR_OP:
5706 t = resolve_operator (e);
5707 break;
5709 case EXPR_FUNCTION:
5710 case EXPR_VARIABLE:
5712 if (check_host_association (e))
5713 t = resolve_function (e);
5714 else
5716 t = resolve_variable (e);
5717 if (t == SUCCESS)
5718 expression_rank (e);
5721 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
5722 && e->ref->type != REF_SUBSTRING)
5723 gfc_resolve_substring_charlen (e);
5725 break;
5727 case EXPR_COMPCALL:
5728 t = resolve_typebound_function (e);
5729 break;
5731 case EXPR_SUBSTRING:
5732 t = resolve_ref (e);
5733 break;
5735 case EXPR_CONSTANT:
5736 case EXPR_NULL:
5737 t = SUCCESS;
5738 break;
5740 case EXPR_PPC:
5741 t = resolve_expr_ppc (e);
5742 break;
5744 case EXPR_ARRAY:
5745 t = FAILURE;
5746 if (resolve_ref (e) == FAILURE)
5747 break;
5749 t = gfc_resolve_array_constructor (e);
5750 /* Also try to expand a constructor. */
5751 if (t == SUCCESS)
5753 expression_rank (e);
5754 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
5755 gfc_expand_constructor (e, false);
5758 /* This provides the opportunity for the length of constructors with
5759 character valued function elements to propagate the string length
5760 to the expression. */
5761 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
5763 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
5764 here rather then add a duplicate test for it above. */
5765 gfc_expand_constructor (e, false);
5766 t = gfc_resolve_character_array_constructor (e);
5769 break;
5771 case EXPR_STRUCTURE:
5772 t = resolve_ref (e);
5773 if (t == FAILURE)
5774 break;
5776 t = resolve_structure_cons (e);
5777 if (t == FAILURE)
5778 break;
5780 t = gfc_simplify_expr (e, 0);
5781 break;
5783 default:
5784 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
5787 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
5788 fixup_charlen (e);
5790 inquiry_argument = inquiry_save;
5792 return t;
5796 /* Resolve an expression from an iterator. They must be scalar and have
5797 INTEGER or (optionally) REAL type. */
5799 static gfc_try
5800 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
5801 const char *name_msgid)
5803 if (gfc_resolve_expr (expr) == FAILURE)
5804 return FAILURE;
5806 if (expr->rank != 0)
5808 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
5809 return FAILURE;
5812 if (expr->ts.type != BT_INTEGER)
5814 if (expr->ts.type == BT_REAL)
5816 if (real_ok)
5817 return gfc_notify_std (GFC_STD_F95_DEL,
5818 "Deleted feature: %s at %L must be integer",
5819 _(name_msgid), &expr->where);
5820 else
5822 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
5823 &expr->where);
5824 return FAILURE;
5827 else
5829 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
5830 return FAILURE;
5833 return SUCCESS;
5837 /* Resolve the expressions in an iterator structure. If REAL_OK is
5838 false allow only INTEGER type iterators, otherwise allow REAL types. */
5840 gfc_try
5841 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
5843 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
5844 == FAILURE)
5845 return FAILURE;
5847 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
5849 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5850 &iter->var->where);
5851 return FAILURE;
5854 if (gfc_resolve_iterator_expr (iter->start, real_ok,
5855 "Start expression in DO loop") == FAILURE)
5856 return FAILURE;
5858 if (gfc_resolve_iterator_expr (iter->end, real_ok,
5859 "End expression in DO loop") == FAILURE)
5860 return FAILURE;
5862 if (gfc_resolve_iterator_expr (iter->step, real_ok,
5863 "Step expression in DO loop") == FAILURE)
5864 return FAILURE;
5866 if (iter->step->expr_type == EXPR_CONSTANT)
5868 if ((iter->step->ts.type == BT_INTEGER
5869 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
5870 || (iter->step->ts.type == BT_REAL
5871 && mpfr_sgn (iter->step->value.real) == 0))
5873 gfc_error ("Step expression in DO loop at %L cannot be zero",
5874 &iter->step->where);
5875 return FAILURE;
5879 /* Convert start, end, and step to the same type as var. */
5880 if (iter->start->ts.kind != iter->var->ts.kind
5881 || iter->start->ts.type != iter->var->ts.type)
5882 gfc_convert_type (iter->start, &iter->var->ts, 2);
5884 if (iter->end->ts.kind != iter->var->ts.kind
5885 || iter->end->ts.type != iter->var->ts.type)
5886 gfc_convert_type (iter->end, &iter->var->ts, 2);
5888 if (iter->step->ts.kind != iter->var->ts.kind
5889 || iter->step->ts.type != iter->var->ts.type)
5890 gfc_convert_type (iter->step, &iter->var->ts, 2);
5892 if (iter->start->expr_type == EXPR_CONSTANT
5893 && iter->end->expr_type == EXPR_CONSTANT
5894 && iter->step->expr_type == EXPR_CONSTANT)
5896 int sgn, cmp;
5897 if (iter->start->ts.type == BT_INTEGER)
5899 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
5900 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
5902 else
5904 sgn = mpfr_sgn (iter->step->value.real);
5905 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
5907 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
5908 gfc_warning ("DO loop at %L will be executed zero times",
5909 &iter->step->where);
5912 return SUCCESS;
5916 /* Traversal function for find_forall_index. f == 2 signals that
5917 that variable itself is not to be checked - only the references. */
5919 static bool
5920 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
5922 if (expr->expr_type != EXPR_VARIABLE)
5923 return false;
5925 /* A scalar assignment */
5926 if (!expr->ref || *f == 1)
5928 if (expr->symtree->n.sym == sym)
5929 return true;
5930 else
5931 return false;
5934 if (*f == 2)
5935 *f = 1;
5936 return false;
5940 /* Check whether the FORALL index appears in the expression or not.
5941 Returns SUCCESS if SYM is found in EXPR. */
5943 gfc_try
5944 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
5946 if (gfc_traverse_expr (expr, sym, forall_index, f))
5947 return SUCCESS;
5948 else
5949 return FAILURE;
5953 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5954 to be a scalar INTEGER variable. The subscripts and stride are scalar
5955 INTEGERs, and if stride is a constant it must be nonzero.
5956 Furthermore "A subscript or stride in a forall-triplet-spec shall
5957 not contain a reference to any index-name in the
5958 forall-triplet-spec-list in which it appears." (7.5.4.1) */
5960 static void
5961 resolve_forall_iterators (gfc_forall_iterator *it)
5963 gfc_forall_iterator *iter, *iter2;
5965 for (iter = it; iter; iter = iter->next)
5967 if (gfc_resolve_expr (iter->var) == SUCCESS
5968 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
5969 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
5970 &iter->var->where);
5972 if (gfc_resolve_expr (iter->start) == SUCCESS
5973 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
5974 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
5975 &iter->start->where);
5976 if (iter->var->ts.kind != iter->start->ts.kind)
5977 gfc_convert_type (iter->start, &iter->var->ts, 2);
5979 if (gfc_resolve_expr (iter->end) == SUCCESS
5980 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
5981 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
5982 &iter->end->where);
5983 if (iter->var->ts.kind != iter->end->ts.kind)
5984 gfc_convert_type (iter->end, &iter->var->ts, 2);
5986 if (gfc_resolve_expr (iter->stride) == SUCCESS)
5988 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
5989 gfc_error ("FORALL stride expression at %L must be a scalar %s",
5990 &iter->stride->where, "INTEGER");
5992 if (iter->stride->expr_type == EXPR_CONSTANT
5993 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
5994 gfc_error ("FORALL stride expression at %L cannot be zero",
5995 &iter->stride->where);
5997 if (iter->var->ts.kind != iter->stride->ts.kind)
5998 gfc_convert_type (iter->stride, &iter->var->ts, 2);
6001 for (iter = it; iter; iter = iter->next)
6002 for (iter2 = iter; iter2; iter2 = iter2->next)
6004 if (find_forall_index (iter2->start,
6005 iter->var->symtree->n.sym, 0) == SUCCESS
6006 || find_forall_index (iter2->end,
6007 iter->var->symtree->n.sym, 0) == SUCCESS
6008 || find_forall_index (iter2->stride,
6009 iter->var->symtree->n.sym, 0) == SUCCESS)
6010 gfc_error ("FORALL index '%s' may not appear in triplet "
6011 "specification at %L", iter->var->symtree->name,
6012 &iter2->start->where);
6017 /* Given a pointer to a symbol that is a derived type, see if it's
6018 inaccessible, i.e. if it's defined in another module and the components are
6019 PRIVATE. The search is recursive if necessary. Returns zero if no
6020 inaccessible components are found, nonzero otherwise. */
6022 static int
6023 derived_inaccessible (gfc_symbol *sym)
6025 gfc_component *c;
6027 if (sym->attr.use_assoc && sym->attr.private_comp)
6028 return 1;
6030 for (c = sym->components; c; c = c->next)
6032 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6033 return 1;
6036 return 0;
6040 /* Resolve the argument of a deallocate expression. The expression must be
6041 a pointer or a full array. */
6043 static gfc_try
6044 resolve_deallocate_expr (gfc_expr *e)
6046 symbol_attribute attr;
6047 int allocatable, pointer, check_intent_in;
6048 gfc_ref *ref;
6049 gfc_symbol *sym;
6050 gfc_component *c;
6052 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
6053 check_intent_in = 1;
6055 if (gfc_resolve_expr (e) == FAILURE)
6056 return FAILURE;
6058 if (e->expr_type != EXPR_VARIABLE)
6059 goto bad;
6061 sym = e->symtree->n.sym;
6063 if (sym->ts.type == BT_CLASS)
6065 allocatable = CLASS_DATA (sym)->attr.allocatable;
6066 pointer = CLASS_DATA (sym)->attr.class_pointer;
6068 else
6070 allocatable = sym->attr.allocatable;
6071 pointer = sym->attr.pointer;
6073 for (ref = e->ref; ref; ref = ref->next)
6075 if (pointer)
6076 check_intent_in = 0;
6078 switch (ref->type)
6080 case REF_ARRAY:
6081 if (ref->u.ar.type != AR_FULL)
6082 allocatable = 0;
6083 break;
6085 case REF_COMPONENT:
6086 c = ref->u.c.component;
6087 if (c->ts.type == BT_CLASS)
6089 allocatable = CLASS_DATA (c)->attr.allocatable;
6090 pointer = CLASS_DATA (c)->attr.class_pointer;
6092 else
6094 allocatable = c->attr.allocatable;
6095 pointer = c->attr.pointer;
6097 break;
6099 case REF_SUBSTRING:
6100 allocatable = 0;
6101 break;
6105 attr = gfc_expr_attr (e);
6107 if (allocatable == 0 && attr.pointer == 0)
6109 bad:
6110 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6111 &e->where);
6112 return FAILURE;
6115 if (check_intent_in && sym->attr.intent == INTENT_IN)
6117 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
6118 sym->name, &e->where);
6119 return FAILURE;
6122 if (e->ts.type == BT_CLASS)
6124 /* Only deallocate the DATA component. */
6125 gfc_add_component_ref (e, "$data");
6128 return SUCCESS;
6132 /* Returns true if the expression e contains a reference to the symbol sym. */
6133 static bool
6134 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6136 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6137 return true;
6139 return false;
6142 bool
6143 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6145 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6149 /* Given the expression node e for an allocatable/pointer of derived type to be
6150 allocated, get the expression node to be initialized afterwards (needed for
6151 derived types with default initializers, and derived types with allocatable
6152 components that need nullification.) */
6154 gfc_expr *
6155 gfc_expr_to_initialize (gfc_expr *e)
6157 gfc_expr *result;
6158 gfc_ref *ref;
6159 int i;
6161 result = gfc_copy_expr (e);
6163 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6164 for (ref = result->ref; ref; ref = ref->next)
6165 if (ref->type == REF_ARRAY && ref->next == NULL)
6167 ref->u.ar.type = AR_FULL;
6169 for (i = 0; i < ref->u.ar.dimen; i++)
6170 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6172 result->rank = ref->u.ar.dimen;
6173 break;
6176 return result;
6180 /* Used in resolve_allocate_expr to check that a allocation-object and
6181 a source-expr are conformable. This does not catch all possible
6182 cases; in particular a runtime checking is needed. */
6184 static gfc_try
6185 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6187 gfc_ref *tail;
6188 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6190 /* First compare rank. */
6191 if (tail && e1->rank != tail->u.ar.as->rank)
6193 gfc_error ("Source-expr at %L must be scalar or have the "
6194 "same rank as the allocate-object at %L",
6195 &e1->where, &e2->where);
6196 return FAILURE;
6199 if (e1->shape)
6201 int i;
6202 mpz_t s;
6204 mpz_init (s);
6206 for (i = 0; i < e1->rank; i++)
6208 if (tail->u.ar.end[i])
6210 mpz_set (s, tail->u.ar.end[i]->value.integer);
6211 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6212 mpz_add_ui (s, s, 1);
6214 else
6216 mpz_set (s, tail->u.ar.start[i]->value.integer);
6219 if (mpz_cmp (e1->shape[i], s) != 0)
6221 gfc_error ("Source-expr at %L and allocate-object at %L must "
6222 "have the same shape", &e1->where, &e2->where);
6223 mpz_clear (s);
6224 return FAILURE;
6228 mpz_clear (s);
6231 return SUCCESS;
6235 /* Resolve the expression in an ALLOCATE statement, doing the additional
6236 checks to see whether the expression is OK or not. The expression must
6237 have a trailing array reference that gives the size of the array. */
6239 static gfc_try
6240 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6242 int i, pointer, allocatable, dimension, check_intent_in, is_abstract;
6243 int codimension;
6244 symbol_attribute attr;
6245 gfc_ref *ref, *ref2;
6246 gfc_array_ref *ar;
6247 gfc_symbol *sym = NULL;
6248 gfc_alloc *a;
6249 gfc_component *c;
6251 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
6252 check_intent_in = 1;
6254 /* Mark the ultimost array component as being in allocate to allow DIMEN_STAR
6255 checking of coarrays. */
6256 for (ref = e->ref; ref; ref = ref->next)
6257 if (ref->next == NULL)
6258 break;
6260 if (ref && ref->type == REF_ARRAY)
6261 ref->u.ar.in_allocate = true;
6263 if (gfc_resolve_expr (e) == FAILURE)
6264 goto failure;
6266 /* Make sure the expression is allocatable or a pointer. If it is
6267 pointer, the next-to-last reference must be a pointer. */
6269 ref2 = NULL;
6270 if (e->symtree)
6271 sym = e->symtree->n.sym;
6273 /* Check whether ultimate component is abstract and CLASS. */
6274 is_abstract = 0;
6276 if (e->expr_type != EXPR_VARIABLE)
6278 allocatable = 0;
6279 attr = gfc_expr_attr (e);
6280 pointer = attr.pointer;
6281 dimension = attr.dimension;
6282 codimension = attr.codimension;
6284 else
6286 if (sym->ts.type == BT_CLASS)
6288 allocatable = CLASS_DATA (sym)->attr.allocatable;
6289 pointer = CLASS_DATA (sym)->attr.class_pointer;
6290 dimension = CLASS_DATA (sym)->attr.dimension;
6291 codimension = CLASS_DATA (sym)->attr.codimension;
6292 is_abstract = CLASS_DATA (sym)->attr.abstract;
6294 else
6296 allocatable = sym->attr.allocatable;
6297 pointer = sym->attr.pointer;
6298 dimension = sym->attr.dimension;
6299 codimension = sym->attr.codimension;
6302 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6304 if (pointer)
6305 check_intent_in = 0;
6307 switch (ref->type)
6309 case REF_ARRAY:
6310 if (ref->next != NULL)
6311 pointer = 0;
6312 break;
6314 case REF_COMPONENT:
6315 /* F2008, C644. */
6316 if (gfc_is_coindexed (e))
6318 gfc_error ("Coindexed allocatable object at %L",
6319 &e->where);
6320 goto failure;
6323 c = ref->u.c.component;
6324 if (c->ts.type == BT_CLASS)
6326 allocatable = CLASS_DATA (c)->attr.allocatable;
6327 pointer = CLASS_DATA (c)->attr.class_pointer;
6328 dimension = CLASS_DATA (c)->attr.dimension;
6329 codimension = CLASS_DATA (c)->attr.codimension;
6330 is_abstract = CLASS_DATA (c)->attr.abstract;
6332 else
6334 allocatable = c->attr.allocatable;
6335 pointer = c->attr.pointer;
6336 dimension = c->attr.dimension;
6337 codimension = c->attr.codimension;
6338 is_abstract = c->attr.abstract;
6340 break;
6342 case REF_SUBSTRING:
6343 allocatable = 0;
6344 pointer = 0;
6345 break;
6350 if (allocatable == 0 && pointer == 0)
6352 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6353 &e->where);
6354 goto failure;
6357 /* Some checks for the SOURCE tag. */
6358 if (code->expr3)
6360 /* Check F03:C631. */
6361 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6363 gfc_error ("Type of entity at %L is type incompatible with "
6364 "source-expr at %L", &e->where, &code->expr3->where);
6365 goto failure;
6368 /* Check F03:C632 and restriction following Note 6.18. */
6369 if (code->expr3->rank > 0
6370 && conformable_arrays (code->expr3, e) == FAILURE)
6371 goto failure;
6373 /* Check F03:C633. */
6374 if (code->expr3->ts.kind != e->ts.kind)
6376 gfc_error ("The allocate-object at %L and the source-expr at %L "
6377 "shall have the same kind type parameter",
6378 &e->where, &code->expr3->where);
6379 goto failure;
6383 /* Check F08:C629. */
6384 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6385 && !code->expr3)
6387 gcc_assert (e->ts.type == BT_CLASS);
6388 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6389 "type-spec or source-expr", sym->name, &e->where);
6390 goto failure;
6393 if (check_intent_in && sym->attr.intent == INTENT_IN)
6395 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
6396 sym->name, &e->where);
6397 goto failure;
6400 if (!code->expr3 || code->expr3->mold)
6402 /* Add default initializer for those derived types that need them. */
6403 gfc_expr *init_e = NULL;
6404 gfc_typespec ts;
6406 if (code->ext.alloc.ts.type == BT_DERIVED)
6407 ts = code->ext.alloc.ts;
6408 else if (code->expr3)
6409 ts = code->expr3->ts;
6410 else
6411 ts = e->ts;
6413 if (ts.type == BT_DERIVED)
6414 init_e = gfc_default_initializer (&ts);
6415 /* FIXME: Use default init of dynamic type (cf. PR 44541). */
6416 else if (e->ts.type == BT_CLASS)
6417 init_e = gfc_default_initializer (&ts.u.derived->components->ts);
6419 if (init_e)
6421 gfc_code *init_st = gfc_get_code ();
6422 init_st->loc = code->loc;
6423 init_st->op = EXEC_INIT_ASSIGN;
6424 init_st->expr1 = gfc_expr_to_initialize (e);
6425 init_st->expr2 = init_e;
6426 init_st->next = code->next;
6427 code->next = init_st;
6431 if (pointer || (dimension == 0 && codimension == 0))
6432 goto success;
6434 /* Make sure the next-to-last reference node is an array specification. */
6436 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6437 || (dimension && ref2->u.ar.dimen == 0))
6439 gfc_error ("Array specification required in ALLOCATE statement "
6440 "at %L", &e->where);
6441 goto failure;
6444 /* Make sure that the array section reference makes sense in the
6445 context of an ALLOCATE specification. */
6447 ar = &ref2->u.ar;
6449 if (codimension && ar->codimen == 0)
6451 gfc_error ("Coarray specification required in ALLOCATE statement "
6452 "at %L", &e->where);
6453 goto failure;
6456 for (i = 0; i < ar->dimen; i++)
6458 if (ref2->u.ar.type == AR_ELEMENT)
6459 goto check_symbols;
6461 switch (ar->dimen_type[i])
6463 case DIMEN_ELEMENT:
6464 break;
6466 case DIMEN_RANGE:
6467 if (ar->start[i] != NULL
6468 && ar->end[i] != NULL
6469 && ar->stride[i] == NULL)
6470 break;
6472 /* Fall Through... */
6474 case DIMEN_UNKNOWN:
6475 case DIMEN_VECTOR:
6476 case DIMEN_STAR:
6477 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6478 &e->where);
6479 goto failure;
6482 check_symbols:
6483 for (a = code->ext.alloc.list; a; a = a->next)
6485 sym = a->expr->symtree->n.sym;
6487 /* TODO - check derived type components. */
6488 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6489 continue;
6491 if ((ar->start[i] != NULL
6492 && gfc_find_sym_in_expr (sym, ar->start[i]))
6493 || (ar->end[i] != NULL
6494 && gfc_find_sym_in_expr (sym, ar->end[i])))
6496 gfc_error ("'%s' must not appear in the array specification at "
6497 "%L in the same ALLOCATE statement where it is "
6498 "itself allocated", sym->name, &ar->where);
6499 goto failure;
6504 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
6506 if (ar->dimen_type[i] == DIMEN_ELEMENT
6507 || ar->dimen_type[i] == DIMEN_RANGE)
6509 if (i == (ar->dimen + ar->codimen - 1))
6511 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
6512 "statement at %L", &e->where);
6513 goto failure;
6515 break;
6518 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
6519 && ar->stride[i] == NULL)
6520 break;
6522 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
6523 &e->where);
6524 goto failure;
6527 if (codimension && ar->as->rank == 0)
6529 gfc_error ("Sorry, allocatable scalar coarrays are not yet supported "
6530 "at %L", &e->where);
6531 goto failure;
6534 success:
6535 return SUCCESS;
6537 failure:
6538 return FAILURE;
6541 static void
6542 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6544 gfc_expr *stat, *errmsg, *pe, *qe;
6545 gfc_alloc *a, *p, *q;
6547 stat = code->expr1 ? code->expr1 : NULL;
6549 errmsg = code->expr2 ? code->expr2 : NULL;
6551 /* Check the stat variable. */
6552 if (stat)
6554 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
6555 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
6556 stat->symtree->n.sym->name, &stat->where);
6558 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
6559 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
6560 &stat->where);
6562 if ((stat->ts.type != BT_INTEGER
6563 && !(stat->ref && (stat->ref->type == REF_ARRAY
6564 || stat->ref->type == REF_COMPONENT)))
6565 || stat->rank > 0)
6566 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6567 "variable", &stat->where);
6569 for (p = code->ext.alloc.list; p; p = p->next)
6570 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
6572 gfc_ref *ref1, *ref2;
6573 bool found = true;
6575 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
6576 ref1 = ref1->next, ref2 = ref2->next)
6578 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
6579 continue;
6580 if (ref1->u.c.component->name != ref2->u.c.component->name)
6582 found = false;
6583 break;
6587 if (found)
6589 gfc_error ("Stat-variable at %L shall not be %sd within "
6590 "the same %s statement", &stat->where, fcn, fcn);
6591 break;
6596 /* Check the errmsg variable. */
6597 if (errmsg)
6599 if (!stat)
6600 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6601 &errmsg->where);
6603 if (errmsg->symtree->n.sym->attr.intent == INTENT_IN)
6604 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
6605 errmsg->symtree->n.sym->name, &errmsg->where);
6607 if (gfc_pure (NULL) && gfc_impure_variable (errmsg->symtree->n.sym))
6608 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
6609 &errmsg->where);
6611 if ((errmsg->ts.type != BT_CHARACTER
6612 && !(errmsg->ref
6613 && (errmsg->ref->type == REF_ARRAY
6614 || errmsg->ref->type == REF_COMPONENT)))
6615 || errmsg->rank > 0 )
6616 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
6617 "variable", &errmsg->where);
6619 for (p = code->ext.alloc.list; p; p = p->next)
6620 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
6622 gfc_ref *ref1, *ref2;
6623 bool found = true;
6625 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
6626 ref1 = ref1->next, ref2 = ref2->next)
6628 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
6629 continue;
6630 if (ref1->u.c.component->name != ref2->u.c.component->name)
6632 found = false;
6633 break;
6637 if (found)
6639 gfc_error ("Errmsg-variable at %L shall not be %sd within "
6640 "the same %s statement", &errmsg->where, fcn, fcn);
6641 break;
6646 /* Check that an allocate-object appears only once in the statement.
6647 FIXME: Checking derived types is disabled. */
6648 for (p = code->ext.alloc.list; p; p = p->next)
6650 pe = p->expr;
6651 if ((pe->ref && pe->ref->type != REF_COMPONENT)
6652 && (pe->symtree->n.sym->ts.type != BT_DERIVED))
6654 for (q = p->next; q; q = q->next)
6656 qe = q->expr;
6657 if ((qe->ref && qe->ref->type != REF_COMPONENT)
6658 && (qe->symtree->n.sym->ts.type != BT_DERIVED)
6659 && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
6660 gfc_error ("Allocate-object at %L also appears at %L",
6661 &pe->where, &qe->where);
6666 if (strcmp (fcn, "ALLOCATE") == 0)
6668 for (a = code->ext.alloc.list; a; a = a->next)
6669 resolve_allocate_expr (a->expr, code);
6671 else
6673 for (a = code->ext.alloc.list; a; a = a->next)
6674 resolve_deallocate_expr (a->expr);
6679 /************ SELECT CASE resolution subroutines ************/
6681 /* Callback function for our mergesort variant. Determines interval
6682 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
6683 op1 > op2. Assumes we're not dealing with the default case.
6684 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
6685 There are nine situations to check. */
6687 static int
6688 compare_cases (const gfc_case *op1, const gfc_case *op2)
6690 int retval;
6692 if (op1->low == NULL) /* op1 = (:L) */
6694 /* op2 = (:N), so overlap. */
6695 retval = 0;
6696 /* op2 = (M:) or (M:N), L < M */
6697 if (op2->low != NULL
6698 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6699 retval = -1;
6701 else if (op1->high == NULL) /* op1 = (K:) */
6703 /* op2 = (M:), so overlap. */
6704 retval = 0;
6705 /* op2 = (:N) or (M:N), K > N */
6706 if (op2->high != NULL
6707 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6708 retval = 1;
6710 else /* op1 = (K:L) */
6712 if (op2->low == NULL) /* op2 = (:N), K > N */
6713 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6714 ? 1 : 0;
6715 else if (op2->high == NULL) /* op2 = (M:), L < M */
6716 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6717 ? -1 : 0;
6718 else /* op2 = (M:N) */
6720 retval = 0;
6721 /* L < M */
6722 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6723 retval = -1;
6724 /* K > N */
6725 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6726 retval = 1;
6730 return retval;
6734 /* Merge-sort a double linked case list, detecting overlap in the
6735 process. LIST is the head of the double linked case list before it
6736 is sorted. Returns the head of the sorted list if we don't see any
6737 overlap, or NULL otherwise. */
6739 static gfc_case *
6740 check_case_overlap (gfc_case *list)
6742 gfc_case *p, *q, *e, *tail;
6743 int insize, nmerges, psize, qsize, cmp, overlap_seen;
6745 /* If the passed list was empty, return immediately. */
6746 if (!list)
6747 return NULL;
6749 overlap_seen = 0;
6750 insize = 1;
6752 /* Loop unconditionally. The only exit from this loop is a return
6753 statement, when we've finished sorting the case list. */
6754 for (;;)
6756 p = list;
6757 list = NULL;
6758 tail = NULL;
6760 /* Count the number of merges we do in this pass. */
6761 nmerges = 0;
6763 /* Loop while there exists a merge to be done. */
6764 while (p)
6766 int i;
6768 /* Count this merge. */
6769 nmerges++;
6771 /* Cut the list in two pieces by stepping INSIZE places
6772 forward in the list, starting from P. */
6773 psize = 0;
6774 q = p;
6775 for (i = 0; i < insize; i++)
6777 psize++;
6778 q = q->right;
6779 if (!q)
6780 break;
6782 qsize = insize;
6784 /* Now we have two lists. Merge them! */
6785 while (psize > 0 || (qsize > 0 && q != NULL))
6787 /* See from which the next case to merge comes from. */
6788 if (psize == 0)
6790 /* P is empty so the next case must come from Q. */
6791 e = q;
6792 q = q->right;
6793 qsize--;
6795 else if (qsize == 0 || q == NULL)
6797 /* Q is empty. */
6798 e = p;
6799 p = p->right;
6800 psize--;
6802 else
6804 cmp = compare_cases (p, q);
6805 if (cmp < 0)
6807 /* The whole case range for P is less than the
6808 one for Q. */
6809 e = p;
6810 p = p->right;
6811 psize--;
6813 else if (cmp > 0)
6815 /* The whole case range for Q is greater than
6816 the case range for P. */
6817 e = q;
6818 q = q->right;
6819 qsize--;
6821 else
6823 /* The cases overlap, or they are the same
6824 element in the list. Either way, we must
6825 issue an error and get the next case from P. */
6826 /* FIXME: Sort P and Q by line number. */
6827 gfc_error ("CASE label at %L overlaps with CASE "
6828 "label at %L", &p->where, &q->where);
6829 overlap_seen = 1;
6830 e = p;
6831 p = p->right;
6832 psize--;
6836 /* Add the next element to the merged list. */
6837 if (tail)
6838 tail->right = e;
6839 else
6840 list = e;
6841 e->left = tail;
6842 tail = e;
6845 /* P has now stepped INSIZE places along, and so has Q. So
6846 they're the same. */
6847 p = q;
6849 tail->right = NULL;
6851 /* If we have done only one merge or none at all, we've
6852 finished sorting the cases. */
6853 if (nmerges <= 1)
6855 if (!overlap_seen)
6856 return list;
6857 else
6858 return NULL;
6861 /* Otherwise repeat, merging lists twice the size. */
6862 insize *= 2;
6867 /* Check to see if an expression is suitable for use in a CASE statement.
6868 Makes sure that all case expressions are scalar constants of the same
6869 type. Return FAILURE if anything is wrong. */
6871 static gfc_try
6872 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
6874 if (e == NULL) return SUCCESS;
6876 if (e->ts.type != case_expr->ts.type)
6878 gfc_error ("Expression in CASE statement at %L must be of type %s",
6879 &e->where, gfc_basic_typename (case_expr->ts.type));
6880 return FAILURE;
6883 /* C805 (R808) For a given case-construct, each case-value shall be of
6884 the same type as case-expr. For character type, length differences
6885 are allowed, but the kind type parameters shall be the same. */
6887 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
6889 gfc_error ("Expression in CASE statement at %L must be of kind %d",
6890 &e->where, case_expr->ts.kind);
6891 return FAILURE;
6894 /* Convert the case value kind to that of case expression kind,
6895 if needed */
6897 if (e->ts.kind != case_expr->ts.kind)
6898 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
6900 if (e->rank != 0)
6902 gfc_error ("Expression in CASE statement at %L must be scalar",
6903 &e->where);
6904 return FAILURE;
6907 return SUCCESS;
6911 /* Given a completely parsed select statement, we:
6913 - Validate all expressions and code within the SELECT.
6914 - Make sure that the selection expression is not of the wrong type.
6915 - Make sure that no case ranges overlap.
6916 - Eliminate unreachable cases and unreachable code resulting from
6917 removing case labels.
6919 The standard does allow unreachable cases, e.g. CASE (5:3). But
6920 they are a hassle for code generation, and to prevent that, we just
6921 cut them out here. This is not necessary for overlapping cases
6922 because they are illegal and we never even try to generate code.
6924 We have the additional caveat that a SELECT construct could have
6925 been a computed GOTO in the source code. Fortunately we can fairly
6926 easily work around that here: The case_expr for a "real" SELECT CASE
6927 is in code->expr1, but for a computed GOTO it is in code->expr2. All
6928 we have to do is make sure that the case_expr is a scalar integer
6929 expression. */
6931 static void
6932 resolve_select (gfc_code *code)
6934 gfc_code *body;
6935 gfc_expr *case_expr;
6936 gfc_case *cp, *default_case, *tail, *head;
6937 int seen_unreachable;
6938 int seen_logical;
6939 int ncases;
6940 bt type;
6941 gfc_try t;
6943 if (code->expr1 == NULL)
6945 /* This was actually a computed GOTO statement. */
6946 case_expr = code->expr2;
6947 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
6948 gfc_error ("Selection expression in computed GOTO statement "
6949 "at %L must be a scalar integer expression",
6950 &case_expr->where);
6952 /* Further checking is not necessary because this SELECT was built
6953 by the compiler, so it should always be OK. Just move the
6954 case_expr from expr2 to expr so that we can handle computed
6955 GOTOs as normal SELECTs from here on. */
6956 code->expr1 = code->expr2;
6957 code->expr2 = NULL;
6958 return;
6961 case_expr = code->expr1;
6963 type = case_expr->ts.type;
6964 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
6966 gfc_error ("Argument of SELECT statement at %L cannot be %s",
6967 &case_expr->where, gfc_typename (&case_expr->ts));
6969 /* Punt. Going on here just produce more garbage error messages. */
6970 return;
6973 if (case_expr->rank != 0)
6975 gfc_error ("Argument of SELECT statement at %L must be a scalar "
6976 "expression", &case_expr->where);
6978 /* Punt. */
6979 return;
6983 /* Raise a warning if an INTEGER case value exceeds the range of
6984 the case-expr. Later, all expressions will be promoted to the
6985 largest kind of all case-labels. */
6987 if (type == BT_INTEGER)
6988 for (body = code->block; body; body = body->block)
6989 for (cp = body->ext.case_list; cp; cp = cp->next)
6991 if (cp->low
6992 && gfc_check_integer_range (cp->low->value.integer,
6993 case_expr->ts.kind) != ARITH_OK)
6994 gfc_warning ("Expression in CASE statement at %L is "
6995 "not in the range of %s", &cp->low->where,
6996 gfc_typename (&case_expr->ts));
6998 if (cp->high
6999 && cp->low != cp->high
7000 && gfc_check_integer_range (cp->high->value.integer,
7001 case_expr->ts.kind) != ARITH_OK)
7002 gfc_warning ("Expression in CASE statement at %L is "
7003 "not in the range of %s", &cp->high->where,
7004 gfc_typename (&case_expr->ts));
7007 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7008 of the SELECT CASE expression and its CASE values. Walk the lists
7009 of case values, and if we find a mismatch, promote case_expr to
7010 the appropriate kind. */
7012 if (type == BT_LOGICAL || type == BT_INTEGER)
7014 for (body = code->block; body; body = body->block)
7016 /* Walk the case label list. */
7017 for (cp = body->ext.case_list; cp; cp = cp->next)
7019 /* Intercept the DEFAULT case. It does not have a kind. */
7020 if (cp->low == NULL && cp->high == NULL)
7021 continue;
7023 /* Unreachable case ranges are discarded, so ignore. */
7024 if (cp->low != NULL && cp->high != NULL
7025 && cp->low != cp->high
7026 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7027 continue;
7029 if (cp->low != NULL
7030 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7031 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7033 if (cp->high != NULL
7034 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7035 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7040 /* Assume there is no DEFAULT case. */
7041 default_case = NULL;
7042 head = tail = NULL;
7043 ncases = 0;
7044 seen_logical = 0;
7046 for (body = code->block; body; body = body->block)
7048 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7049 t = SUCCESS;
7050 seen_unreachable = 0;
7052 /* Walk the case label list, making sure that all case labels
7053 are legal. */
7054 for (cp = body->ext.case_list; cp; cp = cp->next)
7056 /* Count the number of cases in the whole construct. */
7057 ncases++;
7059 /* Intercept the DEFAULT case. */
7060 if (cp->low == NULL && cp->high == NULL)
7062 if (default_case != NULL)
7064 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7065 "by a second DEFAULT CASE at %L",
7066 &default_case->where, &cp->where);
7067 t = FAILURE;
7068 break;
7070 else
7072 default_case = cp;
7073 continue;
7077 /* Deal with single value cases and case ranges. Errors are
7078 issued from the validation function. */
7079 if (validate_case_label_expr (cp->low, case_expr) != SUCCESS
7080 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
7082 t = FAILURE;
7083 break;
7086 if (type == BT_LOGICAL
7087 && ((cp->low == NULL || cp->high == NULL)
7088 || cp->low != cp->high))
7090 gfc_error ("Logical range in CASE statement at %L is not "
7091 "allowed", &cp->low->where);
7092 t = FAILURE;
7093 break;
7096 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7098 int value;
7099 value = cp->low->value.logical == 0 ? 2 : 1;
7100 if (value & seen_logical)
7102 gfc_error ("Constant logical value in CASE statement "
7103 "is repeated at %L",
7104 &cp->low->where);
7105 t = FAILURE;
7106 break;
7108 seen_logical |= value;
7111 if (cp->low != NULL && cp->high != NULL
7112 && cp->low != cp->high
7113 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7115 if (gfc_option.warn_surprising)
7116 gfc_warning ("Range specification at %L can never "
7117 "be matched", &cp->where);
7119 cp->unreachable = 1;
7120 seen_unreachable = 1;
7122 else
7124 /* If the case range can be matched, it can also overlap with
7125 other cases. To make sure it does not, we put it in a
7126 double linked list here. We sort that with a merge sort
7127 later on to detect any overlapping cases. */
7128 if (!head)
7130 head = tail = cp;
7131 head->right = head->left = NULL;
7133 else
7135 tail->right = cp;
7136 tail->right->left = tail;
7137 tail = tail->right;
7138 tail->right = NULL;
7143 /* It there was a failure in the previous case label, give up
7144 for this case label list. Continue with the next block. */
7145 if (t == FAILURE)
7146 continue;
7148 /* See if any case labels that are unreachable have been seen.
7149 If so, we eliminate them. This is a bit of a kludge because
7150 the case lists for a single case statement (label) is a
7151 single forward linked lists. */
7152 if (seen_unreachable)
7154 /* Advance until the first case in the list is reachable. */
7155 while (body->ext.case_list != NULL
7156 && body->ext.case_list->unreachable)
7158 gfc_case *n = body->ext.case_list;
7159 body->ext.case_list = body->ext.case_list->next;
7160 n->next = NULL;
7161 gfc_free_case_list (n);
7164 /* Strip all other unreachable cases. */
7165 if (body->ext.case_list)
7167 for (cp = body->ext.case_list; cp->next; cp = cp->next)
7169 if (cp->next->unreachable)
7171 gfc_case *n = cp->next;
7172 cp->next = cp->next->next;
7173 n->next = NULL;
7174 gfc_free_case_list (n);
7181 /* See if there were overlapping cases. If the check returns NULL,
7182 there was overlap. In that case we don't do anything. If head
7183 is non-NULL, we prepend the DEFAULT case. The sorted list can
7184 then used during code generation for SELECT CASE constructs with
7185 a case expression of a CHARACTER type. */
7186 if (head)
7188 head = check_case_overlap (head);
7190 /* Prepend the default_case if it is there. */
7191 if (head != NULL && default_case)
7193 default_case->left = NULL;
7194 default_case->right = head;
7195 head->left = default_case;
7199 /* Eliminate dead blocks that may be the result if we've seen
7200 unreachable case labels for a block. */
7201 for (body = code; body && body->block; body = body->block)
7203 if (body->block->ext.case_list == NULL)
7205 /* Cut the unreachable block from the code chain. */
7206 gfc_code *c = body->block;
7207 body->block = c->block;
7209 /* Kill the dead block, but not the blocks below it. */
7210 c->block = NULL;
7211 gfc_free_statements (c);
7215 /* More than two cases is legal but insane for logical selects.
7216 Issue a warning for it. */
7217 if (gfc_option.warn_surprising && type == BT_LOGICAL
7218 && ncases > 2)
7219 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7220 &code->loc);
7224 /* Check if a derived type is extensible. */
7226 bool
7227 gfc_type_is_extensible (gfc_symbol *sym)
7229 return !(sym->attr.is_bind_c || sym->attr.sequence);
7233 /* Resolve a SELECT TYPE statement. */
7235 static void
7236 resolve_select_type (gfc_code *code)
7238 gfc_symbol *selector_type;
7239 gfc_code *body, *new_st, *if_st, *tail;
7240 gfc_code *class_is = NULL, *default_case = NULL;
7241 gfc_case *c;
7242 gfc_symtree *st;
7243 char name[GFC_MAX_SYMBOL_LEN];
7244 gfc_namespace *ns;
7245 int error = 0;
7247 ns = code->ext.block.ns;
7248 gfc_resolve (ns);
7250 /* Check for F03:C813. */
7251 if (code->expr1->ts.type != BT_CLASS
7252 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7254 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7255 "at %L", &code->loc);
7256 return;
7259 if (code->expr2)
7261 if (code->expr1->symtree->n.sym->attr.untyped)
7262 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7263 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7265 else
7266 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7268 /* Loop over TYPE IS / CLASS IS cases. */
7269 for (body = code->block; body; body = body->block)
7271 c = body->ext.case_list;
7273 /* Check F03:C815. */
7274 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7275 && !gfc_type_is_extensible (c->ts.u.derived))
7277 gfc_error ("Derived type '%s' at %L must be extensible",
7278 c->ts.u.derived->name, &c->where);
7279 error++;
7280 continue;
7283 /* Check F03:C816. */
7284 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7285 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
7287 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7288 c->ts.u.derived->name, &c->where, selector_type->name);
7289 error++;
7290 continue;
7293 /* Intercept the DEFAULT case. */
7294 if (c->ts.type == BT_UNKNOWN)
7296 /* Check F03:C818. */
7297 if (default_case)
7299 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7300 "by a second DEFAULT CASE at %L",
7301 &default_case->ext.case_list->where, &c->where);
7302 error++;
7303 continue;
7305 else
7306 default_case = body;
7310 if (error>0)
7311 return;
7313 if (code->expr2)
7315 /* Insert assignment for selector variable. */
7316 new_st = gfc_get_code ();
7317 new_st->op = EXEC_ASSIGN;
7318 new_st->expr1 = gfc_copy_expr (code->expr1);
7319 new_st->expr2 = gfc_copy_expr (code->expr2);
7320 ns->code = new_st;
7323 /* Put SELECT TYPE statement inside a BLOCK. */
7324 new_st = gfc_get_code ();
7325 new_st->op = code->op;
7326 new_st->expr1 = code->expr1;
7327 new_st->expr2 = code->expr2;
7328 new_st->block = code->block;
7329 if (!ns->code)
7330 ns->code = new_st;
7331 else
7332 ns->code->next = new_st;
7333 code->op = EXEC_BLOCK;
7334 code->ext.block.assoc = NULL;
7335 code->expr1 = code->expr2 = NULL;
7336 code->block = NULL;
7338 code = new_st;
7340 /* Transform to EXEC_SELECT. */
7341 code->op = EXEC_SELECT;
7342 gfc_add_component_ref (code->expr1, "$vptr");
7343 gfc_add_component_ref (code->expr1, "$hash");
7345 /* Loop over TYPE IS / CLASS IS cases. */
7346 for (body = code->block; body; body = body->block)
7348 c = body->ext.case_list;
7350 if (c->ts.type == BT_DERIVED)
7351 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
7352 c->ts.u.derived->hash_value);
7354 else if (c->ts.type == BT_UNKNOWN)
7355 continue;
7357 /* Assign temporary to selector. */
7358 if (c->ts.type == BT_CLASS)
7359 sprintf (name, "tmp$class$%s", c->ts.u.derived->name);
7360 else
7361 sprintf (name, "tmp$type$%s", c->ts.u.derived->name);
7362 st = gfc_find_symtree (ns->sym_root, name);
7363 new_st = gfc_get_code ();
7364 new_st->expr1 = gfc_get_variable_expr (st);
7365 new_st->expr2 = gfc_get_variable_expr (code->expr1->symtree);
7366 if (c->ts.type == BT_DERIVED)
7368 new_st->op = EXEC_POINTER_ASSIGN;
7369 gfc_add_component_ref (new_st->expr2, "$data");
7371 else
7372 new_st->op = EXEC_POINTER_ASSIGN;
7373 new_st->next = body->next;
7374 body->next = new_st;
7377 /* Take out CLASS IS cases for separate treatment. */
7378 body = code;
7379 while (body && body->block)
7381 if (body->block->ext.case_list->ts.type == BT_CLASS)
7383 /* Add to class_is list. */
7384 if (class_is == NULL)
7386 class_is = body->block;
7387 tail = class_is;
7389 else
7391 for (tail = class_is; tail->block; tail = tail->block) ;
7392 tail->block = body->block;
7393 tail = tail->block;
7395 /* Remove from EXEC_SELECT list. */
7396 body->block = body->block->block;
7397 tail->block = NULL;
7399 else
7400 body = body->block;
7403 if (class_is)
7405 gfc_symbol *vtab;
7407 if (!default_case)
7409 /* Add a default case to hold the CLASS IS cases. */
7410 for (tail = code; tail->block; tail = tail->block) ;
7411 tail->block = gfc_get_code ();
7412 tail = tail->block;
7413 tail->op = EXEC_SELECT_TYPE;
7414 tail->ext.case_list = gfc_get_case ();
7415 tail->ext.case_list->ts.type = BT_UNKNOWN;
7416 tail->next = NULL;
7417 default_case = tail;
7420 /* More than one CLASS IS block? */
7421 if (class_is->block)
7423 gfc_code **c1,*c2;
7424 bool swapped;
7425 /* Sort CLASS IS blocks by extension level. */
7428 swapped = false;
7429 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
7431 c2 = (*c1)->block;
7432 /* F03:C817 (check for doubles). */
7433 if ((*c1)->ext.case_list->ts.u.derived->hash_value
7434 == c2->ext.case_list->ts.u.derived->hash_value)
7436 gfc_error ("Double CLASS IS block in SELECT TYPE "
7437 "statement at %L", &c2->ext.case_list->where);
7438 return;
7440 if ((*c1)->ext.case_list->ts.u.derived->attr.extension
7441 < c2->ext.case_list->ts.u.derived->attr.extension)
7443 /* Swap. */
7444 (*c1)->block = c2->block;
7445 c2->block = *c1;
7446 *c1 = c2;
7447 swapped = true;
7451 while (swapped);
7454 /* Generate IF chain. */
7455 if_st = gfc_get_code ();
7456 if_st->op = EXEC_IF;
7457 new_st = if_st;
7458 for (body = class_is; body; body = body->block)
7460 new_st->block = gfc_get_code ();
7461 new_st = new_st->block;
7462 new_st->op = EXEC_IF;
7463 /* Set up IF condition: Call _gfortran_is_extension_of. */
7464 new_st->expr1 = gfc_get_expr ();
7465 new_st->expr1->expr_type = EXPR_FUNCTION;
7466 new_st->expr1->ts.type = BT_LOGICAL;
7467 new_st->expr1->ts.kind = 4;
7468 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
7469 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
7470 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
7471 /* Set up arguments. */
7472 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
7473 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
7474 gfc_add_component_ref (new_st->expr1->value.function.actual->expr, "$vptr");
7475 vtab = gfc_find_derived_vtab (body->ext.case_list->ts.u.derived);
7476 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
7477 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
7478 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
7479 new_st->next = body->next;
7481 if (default_case->next)
7483 new_st->block = gfc_get_code ();
7484 new_st = new_st->block;
7485 new_st->op = EXEC_IF;
7486 new_st->next = default_case->next;
7489 /* Replace CLASS DEFAULT code by the IF chain. */
7490 default_case->next = if_st;
7493 resolve_select (code);
7498 /* Resolve a transfer statement. This is making sure that:
7499 -- a derived type being transferred has only non-pointer components
7500 -- a derived type being transferred doesn't have private components, unless
7501 it's being transferred from the module where the type was defined
7502 -- we're not trying to transfer a whole assumed size array. */
7504 static void
7505 resolve_transfer (gfc_code *code)
7507 gfc_typespec *ts;
7508 gfc_symbol *sym;
7509 gfc_ref *ref;
7510 gfc_expr *exp;
7512 exp = code->expr1;
7514 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
7515 return;
7517 sym = exp->symtree->n.sym;
7518 ts = &sym->ts;
7520 /* Go to actual component transferred. */
7521 for (ref = code->expr1->ref; ref; ref = ref->next)
7522 if (ref->type == REF_COMPONENT)
7523 ts = &ref->u.c.component->ts;
7525 if (ts->type == BT_DERIVED)
7527 /* Check that transferred derived type doesn't contain POINTER
7528 components. */
7529 if (ts->u.derived->attr.pointer_comp)
7531 gfc_error ("Data transfer element at %L cannot have "
7532 "POINTER components", &code->loc);
7533 return;
7536 if (ts->u.derived->attr.alloc_comp)
7538 gfc_error ("Data transfer element at %L cannot have "
7539 "ALLOCATABLE components", &code->loc);
7540 return;
7543 if (derived_inaccessible (ts->u.derived))
7545 gfc_error ("Data transfer element at %L cannot have "
7546 "PRIVATE components",&code->loc);
7547 return;
7551 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
7552 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
7554 gfc_error ("Data transfer element at %L cannot be a full reference to "
7555 "an assumed-size array", &code->loc);
7556 return;
7561 /*********** Toplevel code resolution subroutines ***********/
7563 /* Find the set of labels that are reachable from this block. We also
7564 record the last statement in each block. */
7566 static void
7567 find_reachable_labels (gfc_code *block)
7569 gfc_code *c;
7571 if (!block)
7572 return;
7574 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
7576 /* Collect labels in this block. We don't keep those corresponding
7577 to END {IF|SELECT}, these are checked in resolve_branch by going
7578 up through the code_stack. */
7579 for (c = block; c; c = c->next)
7581 if (c->here && c->op != EXEC_END_BLOCK)
7582 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
7585 /* Merge with labels from parent block. */
7586 if (cs_base->prev)
7588 gcc_assert (cs_base->prev->reachable_labels);
7589 bitmap_ior_into (cs_base->reachable_labels,
7590 cs_base->prev->reachable_labels);
7595 static void
7596 resolve_sync (gfc_code *code)
7598 /* Check imageset. The * case matches expr1 == NULL. */
7599 if (code->expr1)
7601 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
7602 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
7603 "INTEGER expression", &code->expr1->where);
7604 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
7605 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
7606 gfc_error ("Imageset argument at %L must between 1 and num_images()",
7607 &code->expr1->where);
7608 else if (code->expr1->expr_type == EXPR_ARRAY
7609 && gfc_simplify_expr (code->expr1, 0) == SUCCESS)
7611 gfc_constructor *cons;
7612 cons = gfc_constructor_first (code->expr1->value.constructor);
7613 for (; cons; cons = gfc_constructor_next (cons))
7614 if (cons->expr->expr_type == EXPR_CONSTANT
7615 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
7616 gfc_error ("Imageset argument at %L must between 1 and "
7617 "num_images()", &cons->expr->where);
7621 /* Check STAT. */
7622 if (code->expr2
7623 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
7624 || code->expr2->expr_type != EXPR_VARIABLE))
7625 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
7626 &code->expr2->where);
7628 /* Check ERRMSG. */
7629 if (code->expr3
7630 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
7631 || code->expr3->expr_type != EXPR_VARIABLE))
7632 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
7633 &code->expr3->where);
7637 /* Given a branch to a label, see if the branch is conforming.
7638 The code node describes where the branch is located. */
7640 static void
7641 resolve_branch (gfc_st_label *label, gfc_code *code)
7643 code_stack *stack;
7645 if (label == NULL)
7646 return;
7648 /* Step one: is this a valid branching target? */
7650 if (label->defined == ST_LABEL_UNKNOWN)
7652 gfc_error ("Label %d referenced at %L is never defined", label->value,
7653 &label->where);
7654 return;
7657 if (label->defined != ST_LABEL_TARGET)
7659 gfc_error ("Statement at %L is not a valid branch target statement "
7660 "for the branch statement at %L", &label->where, &code->loc);
7661 return;
7664 /* Step two: make sure this branch is not a branch to itself ;-) */
7666 if (code->here == label)
7668 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
7669 return;
7672 /* Step three: See if the label is in the same block as the
7673 branching statement. The hard work has been done by setting up
7674 the bitmap reachable_labels. */
7676 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
7678 /* Check now whether there is a CRITICAL construct; if so, check
7679 whether the label is still visible outside of the CRITICAL block,
7680 which is invalid. */
7681 for (stack = cs_base; stack; stack = stack->prev)
7682 if (stack->current->op == EXEC_CRITICAL
7683 && bitmap_bit_p (stack->reachable_labels, label->value))
7684 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
7685 " at %L", &code->loc, &label->where);
7687 return;
7690 /* Step four: If we haven't found the label in the bitmap, it may
7691 still be the label of the END of the enclosing block, in which
7692 case we find it by going up the code_stack. */
7694 for (stack = cs_base; stack; stack = stack->prev)
7696 if (stack->current->next && stack->current->next->here == label)
7697 break;
7698 if (stack->current->op == EXEC_CRITICAL)
7700 /* Note: A label at END CRITICAL does not leave the CRITICAL
7701 construct as END CRITICAL is still part of it. */
7702 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
7703 " at %L", &code->loc, &label->where);
7704 return;
7708 if (stack)
7710 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
7711 return;
7714 /* The label is not in an enclosing block, so illegal. This was
7715 allowed in Fortran 66, so we allow it as extension. No
7716 further checks are necessary in this case. */
7717 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
7718 "as the GOTO statement at %L", &label->where,
7719 &code->loc);
7720 return;
7724 /* Check whether EXPR1 has the same shape as EXPR2. */
7726 static gfc_try
7727 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
7729 mpz_t shape[GFC_MAX_DIMENSIONS];
7730 mpz_t shape2[GFC_MAX_DIMENSIONS];
7731 gfc_try result = FAILURE;
7732 int i;
7734 /* Compare the rank. */
7735 if (expr1->rank != expr2->rank)
7736 return result;
7738 /* Compare the size of each dimension. */
7739 for (i=0; i<expr1->rank; i++)
7741 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
7742 goto ignore;
7744 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
7745 goto ignore;
7747 if (mpz_cmp (shape[i], shape2[i]))
7748 goto over;
7751 /* When either of the two expression is an assumed size array, we
7752 ignore the comparison of dimension sizes. */
7753 ignore:
7754 result = SUCCESS;
7756 over:
7757 for (i--; i >= 0; i--)
7759 mpz_clear (shape[i]);
7760 mpz_clear (shape2[i]);
7762 return result;
7766 /* Check whether a WHERE assignment target or a WHERE mask expression
7767 has the same shape as the outmost WHERE mask expression. */
7769 static void
7770 resolve_where (gfc_code *code, gfc_expr *mask)
7772 gfc_code *cblock;
7773 gfc_code *cnext;
7774 gfc_expr *e = NULL;
7776 cblock = code->block;
7778 /* Store the first WHERE mask-expr of the WHERE statement or construct.
7779 In case of nested WHERE, only the outmost one is stored. */
7780 if (mask == NULL) /* outmost WHERE */
7781 e = cblock->expr1;
7782 else /* inner WHERE */
7783 e = mask;
7785 while (cblock)
7787 if (cblock->expr1)
7789 /* Check if the mask-expr has a consistent shape with the
7790 outmost WHERE mask-expr. */
7791 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
7792 gfc_error ("WHERE mask at %L has inconsistent shape",
7793 &cblock->expr1->where);
7796 /* the assignment statement of a WHERE statement, or the first
7797 statement in where-body-construct of a WHERE construct */
7798 cnext = cblock->next;
7799 while (cnext)
7801 switch (cnext->op)
7803 /* WHERE assignment statement */
7804 case EXEC_ASSIGN:
7806 /* Check shape consistent for WHERE assignment target. */
7807 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
7808 gfc_error ("WHERE assignment target at %L has "
7809 "inconsistent shape", &cnext->expr1->where);
7810 break;
7813 case EXEC_ASSIGN_CALL:
7814 resolve_call (cnext);
7815 if (!cnext->resolved_sym->attr.elemental)
7816 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7817 &cnext->ext.actual->expr->where);
7818 break;
7820 /* WHERE or WHERE construct is part of a where-body-construct */
7821 case EXEC_WHERE:
7822 resolve_where (cnext, e);
7823 break;
7825 default:
7826 gfc_error ("Unsupported statement inside WHERE at %L",
7827 &cnext->loc);
7829 /* the next statement within the same where-body-construct */
7830 cnext = cnext->next;
7832 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7833 cblock = cblock->block;
7838 /* Resolve assignment in FORALL construct.
7839 NVAR is the number of FORALL index variables, and VAR_EXPR records the
7840 FORALL index variables. */
7842 static void
7843 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
7845 int n;
7847 for (n = 0; n < nvar; n++)
7849 gfc_symbol *forall_index;
7851 forall_index = var_expr[n]->symtree->n.sym;
7853 /* Check whether the assignment target is one of the FORALL index
7854 variable. */
7855 if ((code->expr1->expr_type == EXPR_VARIABLE)
7856 && (code->expr1->symtree->n.sym == forall_index))
7857 gfc_error ("Assignment to a FORALL index variable at %L",
7858 &code->expr1->where);
7859 else
7861 /* If one of the FORALL index variables doesn't appear in the
7862 assignment variable, then there could be a many-to-one
7863 assignment. Emit a warning rather than an error because the
7864 mask could be resolving this problem. */
7865 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
7866 gfc_warning ("The FORALL with index '%s' is not used on the "
7867 "left side of the assignment at %L and so might "
7868 "cause multiple assignment to this object",
7869 var_expr[n]->symtree->name, &code->expr1->where);
7875 /* Resolve WHERE statement in FORALL construct. */
7877 static void
7878 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
7879 gfc_expr **var_expr)
7881 gfc_code *cblock;
7882 gfc_code *cnext;
7884 cblock = code->block;
7885 while (cblock)
7887 /* the assignment statement of a WHERE statement, or the first
7888 statement in where-body-construct of a WHERE construct */
7889 cnext = cblock->next;
7890 while (cnext)
7892 switch (cnext->op)
7894 /* WHERE assignment statement */
7895 case EXEC_ASSIGN:
7896 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
7897 break;
7899 /* WHERE operator assignment statement */
7900 case EXEC_ASSIGN_CALL:
7901 resolve_call (cnext);
7902 if (!cnext->resolved_sym->attr.elemental)
7903 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7904 &cnext->ext.actual->expr->where);
7905 break;
7907 /* WHERE or WHERE construct is part of a where-body-construct */
7908 case EXEC_WHERE:
7909 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
7910 break;
7912 default:
7913 gfc_error ("Unsupported statement inside WHERE at %L",
7914 &cnext->loc);
7916 /* the next statement within the same where-body-construct */
7917 cnext = cnext->next;
7919 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7920 cblock = cblock->block;
7925 /* Traverse the FORALL body to check whether the following errors exist:
7926 1. For assignment, check if a many-to-one assignment happens.
7927 2. For WHERE statement, check the WHERE body to see if there is any
7928 many-to-one assignment. */
7930 static void
7931 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
7933 gfc_code *c;
7935 c = code->block->next;
7936 while (c)
7938 switch (c->op)
7940 case EXEC_ASSIGN:
7941 case EXEC_POINTER_ASSIGN:
7942 gfc_resolve_assign_in_forall (c, nvar, var_expr);
7943 break;
7945 case EXEC_ASSIGN_CALL:
7946 resolve_call (c);
7947 break;
7949 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
7950 there is no need to handle it here. */
7951 case EXEC_FORALL:
7952 break;
7953 case EXEC_WHERE:
7954 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
7955 break;
7956 default:
7957 break;
7959 /* The next statement in the FORALL body. */
7960 c = c->next;
7965 /* Counts the number of iterators needed inside a forall construct, including
7966 nested forall constructs. This is used to allocate the needed memory
7967 in gfc_resolve_forall. */
7969 static int
7970 gfc_count_forall_iterators (gfc_code *code)
7972 int max_iters, sub_iters, current_iters;
7973 gfc_forall_iterator *fa;
7975 gcc_assert(code->op == EXEC_FORALL);
7976 max_iters = 0;
7977 current_iters = 0;
7979 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7980 current_iters ++;
7982 code = code->block->next;
7984 while (code)
7986 if (code->op == EXEC_FORALL)
7988 sub_iters = gfc_count_forall_iterators (code);
7989 if (sub_iters > max_iters)
7990 max_iters = sub_iters;
7992 code = code->next;
7995 return current_iters + max_iters;
7999 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8000 gfc_resolve_forall_body to resolve the FORALL body. */
8002 static void
8003 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8005 static gfc_expr **var_expr;
8006 static int total_var = 0;
8007 static int nvar = 0;
8008 int old_nvar, tmp;
8009 gfc_forall_iterator *fa;
8010 int i;
8012 old_nvar = nvar;
8014 /* Start to resolve a FORALL construct */
8015 if (forall_save == 0)
8017 /* Count the total number of FORALL index in the nested FORALL
8018 construct in order to allocate the VAR_EXPR with proper size. */
8019 total_var = gfc_count_forall_iterators (code);
8021 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8022 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
8025 /* The information about FORALL iterator, including FORALL index start, end
8026 and stride. The FORALL index can not appear in start, end or stride. */
8027 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8029 /* Check if any outer FORALL index name is the same as the current
8030 one. */
8031 for (i = 0; i < nvar; i++)
8033 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8035 gfc_error ("An outer FORALL construct already has an index "
8036 "with this name %L", &fa->var->where);
8040 /* Record the current FORALL index. */
8041 var_expr[nvar] = gfc_copy_expr (fa->var);
8043 nvar++;
8045 /* No memory leak. */
8046 gcc_assert (nvar <= total_var);
8049 /* Resolve the FORALL body. */
8050 gfc_resolve_forall_body (code, nvar, var_expr);
8052 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8053 gfc_resolve_blocks (code->block, ns);
8055 tmp = nvar;
8056 nvar = old_nvar;
8057 /* Free only the VAR_EXPRs allocated in this frame. */
8058 for (i = nvar; i < tmp; i++)
8059 gfc_free_expr (var_expr[i]);
8061 if (nvar == 0)
8063 /* We are in the outermost FORALL construct. */
8064 gcc_assert (forall_save == 0);
8066 /* VAR_EXPR is not needed any more. */
8067 gfc_free (var_expr);
8068 total_var = 0;
8073 /* Resolve a BLOCK construct statement. */
8075 static void
8076 resolve_block_construct (gfc_code* code)
8078 /* For an ASSOCIATE block, the associations (and their targets) are already
8079 resolved during gfc_resolve_symbol. */
8081 /* Resolve the BLOCK's namespace. */
8082 gfc_resolve (code->ext.block.ns);
8086 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8087 DO code nodes. */
8089 static void resolve_code (gfc_code *, gfc_namespace *);
8091 void
8092 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8094 gfc_try t;
8096 for (; b; b = b->block)
8098 t = gfc_resolve_expr (b->expr1);
8099 if (gfc_resolve_expr (b->expr2) == FAILURE)
8100 t = FAILURE;
8102 switch (b->op)
8104 case EXEC_IF:
8105 if (t == SUCCESS && b->expr1 != NULL
8106 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
8107 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8108 &b->expr1->where);
8109 break;
8111 case EXEC_WHERE:
8112 if (t == SUCCESS
8113 && b->expr1 != NULL
8114 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
8115 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8116 &b->expr1->where);
8117 break;
8119 case EXEC_GOTO:
8120 resolve_branch (b->label1, b);
8121 break;
8123 case EXEC_BLOCK:
8124 resolve_block_construct (b);
8125 break;
8127 case EXEC_SELECT:
8128 case EXEC_SELECT_TYPE:
8129 case EXEC_FORALL:
8130 case EXEC_DO:
8131 case EXEC_DO_WHILE:
8132 case EXEC_CRITICAL:
8133 case EXEC_READ:
8134 case EXEC_WRITE:
8135 case EXEC_IOLENGTH:
8136 case EXEC_WAIT:
8137 break;
8139 case EXEC_OMP_ATOMIC:
8140 case EXEC_OMP_CRITICAL:
8141 case EXEC_OMP_DO:
8142 case EXEC_OMP_MASTER:
8143 case EXEC_OMP_ORDERED:
8144 case EXEC_OMP_PARALLEL:
8145 case EXEC_OMP_PARALLEL_DO:
8146 case EXEC_OMP_PARALLEL_SECTIONS:
8147 case EXEC_OMP_PARALLEL_WORKSHARE:
8148 case EXEC_OMP_SECTIONS:
8149 case EXEC_OMP_SINGLE:
8150 case EXEC_OMP_TASK:
8151 case EXEC_OMP_TASKWAIT:
8152 case EXEC_OMP_WORKSHARE:
8153 break;
8155 default:
8156 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
8159 resolve_code (b->next, ns);
8164 /* Does everything to resolve an ordinary assignment. Returns true
8165 if this is an interface assignment. */
8166 static bool
8167 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
8169 bool rval = false;
8170 gfc_expr *lhs;
8171 gfc_expr *rhs;
8172 int llen = 0;
8173 int rlen = 0;
8174 int n;
8175 gfc_ref *ref;
8177 if (gfc_extend_assign (code, ns) == SUCCESS)
8179 gfc_expr** rhsptr;
8181 if (code->op == EXEC_ASSIGN_CALL)
8183 lhs = code->ext.actual->expr;
8184 rhsptr = &code->ext.actual->next->expr;
8186 else
8188 gfc_actual_arglist* args;
8189 gfc_typebound_proc* tbp;
8191 gcc_assert (code->op == EXEC_COMPCALL);
8193 args = code->expr1->value.compcall.actual;
8194 lhs = args->expr;
8195 rhsptr = &args->next->expr;
8197 tbp = code->expr1->value.compcall.tbp;
8198 gcc_assert (!tbp->is_generic);
8201 /* Make a temporary rhs when there is a default initializer
8202 and rhs is the same symbol as the lhs. */
8203 if ((*rhsptr)->expr_type == EXPR_VARIABLE
8204 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
8205 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
8206 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
8207 *rhsptr = gfc_get_parentheses (*rhsptr);
8209 return true;
8212 lhs = code->expr1;
8213 rhs = code->expr2;
8215 if (rhs->is_boz
8216 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
8217 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
8218 &code->loc) == FAILURE)
8219 return false;
8221 /* Handle the case of a BOZ literal on the RHS. */
8222 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
8224 int rc;
8225 if (gfc_option.warn_surprising)
8226 gfc_warning ("BOZ literal at %L is bitwise transferred "
8227 "non-integer symbol '%s'", &code->loc,
8228 lhs->symtree->n.sym->name);
8230 if (!gfc_convert_boz (rhs, &lhs->ts))
8231 return false;
8232 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
8234 if (rc == ARITH_UNDERFLOW)
8235 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
8236 ". This check can be disabled with the option "
8237 "-fno-range-check", &rhs->where);
8238 else if (rc == ARITH_OVERFLOW)
8239 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
8240 ". This check can be disabled with the option "
8241 "-fno-range-check", &rhs->where);
8242 else if (rc == ARITH_NAN)
8243 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
8244 ". This check can be disabled with the option "
8245 "-fno-range-check", &rhs->where);
8246 return false;
8251 if (lhs->ts.type == BT_CHARACTER
8252 && gfc_option.warn_character_truncation)
8254 if (lhs->ts.u.cl != NULL
8255 && lhs->ts.u.cl->length != NULL
8256 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8257 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
8259 if (rhs->expr_type == EXPR_CONSTANT)
8260 rlen = rhs->value.character.length;
8262 else if (rhs->ts.u.cl != NULL
8263 && rhs->ts.u.cl->length != NULL
8264 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8265 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
8267 if (rlen && llen && rlen > llen)
8268 gfc_warning_now ("CHARACTER expression will be truncated "
8269 "in assignment (%d/%d) at %L",
8270 llen, rlen, &code->loc);
8273 /* Ensure that a vector index expression for the lvalue is evaluated
8274 to a temporary if the lvalue symbol is referenced in it. */
8275 if (lhs->rank)
8277 for (ref = lhs->ref; ref; ref= ref->next)
8278 if (ref->type == REF_ARRAY)
8280 for (n = 0; n < ref->u.ar.dimen; n++)
8281 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
8282 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
8283 ref->u.ar.start[n]))
8284 ref->u.ar.start[n]
8285 = gfc_get_parentheses (ref->u.ar.start[n]);
8289 if (gfc_pure (NULL))
8291 if (gfc_impure_variable (lhs->symtree->n.sym))
8293 gfc_error ("Cannot assign to variable '%s' in PURE "
8294 "procedure at %L",
8295 lhs->symtree->n.sym->name,
8296 &lhs->where);
8297 return rval;
8300 if (lhs->ts.type == BT_DERIVED
8301 && lhs->expr_type == EXPR_VARIABLE
8302 && lhs->ts.u.derived->attr.pointer_comp
8303 && rhs->expr_type == EXPR_VARIABLE
8304 && (gfc_impure_variable (rhs->symtree->n.sym)
8305 || gfc_is_coindexed (rhs)))
8307 /* F2008, C1283. */
8308 if (gfc_is_coindexed (rhs))
8309 gfc_error ("Coindexed expression at %L is assigned to "
8310 "a derived type variable with a POINTER "
8311 "component in a PURE procedure",
8312 &rhs->where);
8313 else
8314 gfc_error ("The impure variable at %L is assigned to "
8315 "a derived type variable with a POINTER "
8316 "component in a PURE procedure (12.6)",
8317 &rhs->where);
8318 return rval;
8321 /* Fortran 2008, C1283. */
8322 if (gfc_is_coindexed (lhs))
8324 gfc_error ("Assignment to coindexed variable at %L in a PURE "
8325 "procedure", &rhs->where);
8326 return rval;
8330 /* F03:7.4.1.2. */
8331 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
8332 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
8333 if (lhs->ts.type == BT_CLASS)
8335 gfc_error ("Variable must not be polymorphic in assignment at %L",
8336 &lhs->where);
8337 return false;
8340 /* F2008, Section 7.2.1.2. */
8341 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
8343 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
8344 "component in assignment at %L", &lhs->where);
8345 return false;
8348 gfc_check_assign (lhs, rhs, 1);
8349 return false;
8353 /* Given a block of code, recursively resolve everything pointed to by this
8354 code block. */
8356 static void
8357 resolve_code (gfc_code *code, gfc_namespace *ns)
8359 int omp_workshare_save;
8360 int forall_save;
8361 code_stack frame;
8362 gfc_try t;
8364 frame.prev = cs_base;
8365 frame.head = code;
8366 cs_base = &frame;
8368 find_reachable_labels (code);
8370 for (; code; code = code->next)
8372 frame.current = code;
8373 forall_save = forall_flag;
8375 if (code->op == EXEC_FORALL)
8377 forall_flag = 1;
8378 gfc_resolve_forall (code, ns, forall_save);
8379 forall_flag = 2;
8381 else if (code->block)
8383 omp_workshare_save = -1;
8384 switch (code->op)
8386 case EXEC_OMP_PARALLEL_WORKSHARE:
8387 omp_workshare_save = omp_workshare_flag;
8388 omp_workshare_flag = 1;
8389 gfc_resolve_omp_parallel_blocks (code, ns);
8390 break;
8391 case EXEC_OMP_PARALLEL:
8392 case EXEC_OMP_PARALLEL_DO:
8393 case EXEC_OMP_PARALLEL_SECTIONS:
8394 case EXEC_OMP_TASK:
8395 omp_workshare_save = omp_workshare_flag;
8396 omp_workshare_flag = 0;
8397 gfc_resolve_omp_parallel_blocks (code, ns);
8398 break;
8399 case EXEC_OMP_DO:
8400 gfc_resolve_omp_do_blocks (code, ns);
8401 break;
8402 case EXEC_SELECT_TYPE:
8403 gfc_current_ns = code->ext.block.ns;
8404 gfc_resolve_blocks (code->block, gfc_current_ns);
8405 gfc_current_ns = ns;
8406 break;
8407 case EXEC_OMP_WORKSHARE:
8408 omp_workshare_save = omp_workshare_flag;
8409 omp_workshare_flag = 1;
8410 /* FALLTHROUGH */
8411 default:
8412 gfc_resolve_blocks (code->block, ns);
8413 break;
8416 if (omp_workshare_save != -1)
8417 omp_workshare_flag = omp_workshare_save;
8420 t = SUCCESS;
8421 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
8422 t = gfc_resolve_expr (code->expr1);
8423 forall_flag = forall_save;
8425 if (gfc_resolve_expr (code->expr2) == FAILURE)
8426 t = FAILURE;
8428 if (code->op == EXEC_ALLOCATE
8429 && gfc_resolve_expr (code->expr3) == FAILURE)
8430 t = FAILURE;
8432 switch (code->op)
8434 case EXEC_NOP:
8435 case EXEC_END_BLOCK:
8436 case EXEC_CYCLE:
8437 case EXEC_PAUSE:
8438 case EXEC_STOP:
8439 case EXEC_ERROR_STOP:
8440 case EXEC_EXIT:
8441 case EXEC_CONTINUE:
8442 case EXEC_DT_END:
8443 case EXEC_ASSIGN_CALL:
8444 case EXEC_CRITICAL:
8445 break;
8447 case EXEC_SYNC_ALL:
8448 case EXEC_SYNC_IMAGES:
8449 case EXEC_SYNC_MEMORY:
8450 resolve_sync (code);
8451 break;
8453 case EXEC_ENTRY:
8454 /* Keep track of which entry we are up to. */
8455 current_entry_id = code->ext.entry->id;
8456 break;
8458 case EXEC_WHERE:
8459 resolve_where (code, NULL);
8460 break;
8462 case EXEC_GOTO:
8463 if (code->expr1 != NULL)
8465 if (code->expr1->ts.type != BT_INTEGER)
8466 gfc_error ("ASSIGNED GOTO statement at %L requires an "
8467 "INTEGER variable", &code->expr1->where);
8468 else if (code->expr1->symtree->n.sym->attr.assign != 1)
8469 gfc_error ("Variable '%s' has not been assigned a target "
8470 "label at %L", code->expr1->symtree->n.sym->name,
8471 &code->expr1->where);
8473 else
8474 resolve_branch (code->label1, code);
8475 break;
8477 case EXEC_RETURN:
8478 if (code->expr1 != NULL
8479 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
8480 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
8481 "INTEGER return specifier", &code->expr1->where);
8482 break;
8484 case EXEC_INIT_ASSIGN:
8485 case EXEC_END_PROCEDURE:
8486 break;
8488 case EXEC_ASSIGN:
8489 if (t == FAILURE)
8490 break;
8492 if (resolve_ordinary_assign (code, ns))
8494 if (code->op == EXEC_COMPCALL)
8495 goto compcall;
8496 else
8497 goto call;
8499 break;
8501 case EXEC_LABEL_ASSIGN:
8502 if (code->label1->defined == ST_LABEL_UNKNOWN)
8503 gfc_error ("Label %d referenced at %L is never defined",
8504 code->label1->value, &code->label1->where);
8505 if (t == SUCCESS
8506 && (code->expr1->expr_type != EXPR_VARIABLE
8507 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
8508 || code->expr1->symtree->n.sym->ts.kind
8509 != gfc_default_integer_kind
8510 || code->expr1->symtree->n.sym->as != NULL))
8511 gfc_error ("ASSIGN statement at %L requires a scalar "
8512 "default INTEGER variable", &code->expr1->where);
8513 break;
8515 case EXEC_POINTER_ASSIGN:
8516 if (t == FAILURE)
8517 break;
8519 gfc_check_pointer_assign (code->expr1, code->expr2);
8520 break;
8522 case EXEC_ARITHMETIC_IF:
8523 if (t == SUCCESS
8524 && code->expr1->ts.type != BT_INTEGER
8525 && code->expr1->ts.type != BT_REAL)
8526 gfc_error ("Arithmetic IF statement at %L requires a numeric "
8527 "expression", &code->expr1->where);
8529 resolve_branch (code->label1, code);
8530 resolve_branch (code->label2, code);
8531 resolve_branch (code->label3, code);
8532 break;
8534 case EXEC_IF:
8535 if (t == SUCCESS && code->expr1 != NULL
8536 && (code->expr1->ts.type != BT_LOGICAL
8537 || code->expr1->rank != 0))
8538 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8539 &code->expr1->where);
8540 break;
8542 case EXEC_CALL:
8543 call:
8544 resolve_call (code);
8545 break;
8547 case EXEC_COMPCALL:
8548 compcall:
8549 resolve_typebound_subroutine (code);
8550 break;
8552 case EXEC_CALL_PPC:
8553 resolve_ppc_call (code);
8554 break;
8556 case EXEC_SELECT:
8557 /* Select is complicated. Also, a SELECT construct could be
8558 a transformed computed GOTO. */
8559 resolve_select (code);
8560 break;
8562 case EXEC_SELECT_TYPE:
8563 resolve_select_type (code);
8564 break;
8566 case EXEC_BLOCK:
8567 gfc_resolve (code->ext.block.ns);
8568 break;
8570 case EXEC_DO:
8571 if (code->ext.iterator != NULL)
8573 gfc_iterator *iter = code->ext.iterator;
8574 if (gfc_resolve_iterator (iter, true) != FAILURE)
8575 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
8577 break;
8579 case EXEC_DO_WHILE:
8580 if (code->expr1 == NULL)
8581 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
8582 if (t == SUCCESS
8583 && (code->expr1->rank != 0
8584 || code->expr1->ts.type != BT_LOGICAL))
8585 gfc_error ("Exit condition of DO WHILE loop at %L must be "
8586 "a scalar LOGICAL expression", &code->expr1->where);
8587 break;
8589 case EXEC_ALLOCATE:
8590 if (t == SUCCESS)
8591 resolve_allocate_deallocate (code, "ALLOCATE");
8593 break;
8595 case EXEC_DEALLOCATE:
8596 if (t == SUCCESS)
8597 resolve_allocate_deallocate (code, "DEALLOCATE");
8599 break;
8601 case EXEC_OPEN:
8602 if (gfc_resolve_open (code->ext.open) == FAILURE)
8603 break;
8605 resolve_branch (code->ext.open->err, code);
8606 break;
8608 case EXEC_CLOSE:
8609 if (gfc_resolve_close (code->ext.close) == FAILURE)
8610 break;
8612 resolve_branch (code->ext.close->err, code);
8613 break;
8615 case EXEC_BACKSPACE:
8616 case EXEC_ENDFILE:
8617 case EXEC_REWIND:
8618 case EXEC_FLUSH:
8619 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
8620 break;
8622 resolve_branch (code->ext.filepos->err, code);
8623 break;
8625 case EXEC_INQUIRE:
8626 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8627 break;
8629 resolve_branch (code->ext.inquire->err, code);
8630 break;
8632 case EXEC_IOLENGTH:
8633 gcc_assert (code->ext.inquire != NULL);
8634 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8635 break;
8637 resolve_branch (code->ext.inquire->err, code);
8638 break;
8640 case EXEC_WAIT:
8641 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
8642 break;
8644 resolve_branch (code->ext.wait->err, code);
8645 resolve_branch (code->ext.wait->end, code);
8646 resolve_branch (code->ext.wait->eor, code);
8647 break;
8649 case EXEC_READ:
8650 case EXEC_WRITE:
8651 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
8652 break;
8654 resolve_branch (code->ext.dt->err, code);
8655 resolve_branch (code->ext.dt->end, code);
8656 resolve_branch (code->ext.dt->eor, code);
8657 break;
8659 case EXEC_TRANSFER:
8660 resolve_transfer (code);
8661 break;
8663 case EXEC_FORALL:
8664 resolve_forall_iterators (code->ext.forall_iterator);
8666 if (code->expr1 != NULL && code->expr1->ts.type != BT_LOGICAL)
8667 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
8668 "expression", &code->expr1->where);
8669 break;
8671 case EXEC_OMP_ATOMIC:
8672 case EXEC_OMP_BARRIER:
8673 case EXEC_OMP_CRITICAL:
8674 case EXEC_OMP_FLUSH:
8675 case EXEC_OMP_DO:
8676 case EXEC_OMP_MASTER:
8677 case EXEC_OMP_ORDERED:
8678 case EXEC_OMP_SECTIONS:
8679 case EXEC_OMP_SINGLE:
8680 case EXEC_OMP_TASKWAIT:
8681 case EXEC_OMP_WORKSHARE:
8682 gfc_resolve_omp_directive (code, ns);
8683 break;
8685 case EXEC_OMP_PARALLEL:
8686 case EXEC_OMP_PARALLEL_DO:
8687 case EXEC_OMP_PARALLEL_SECTIONS:
8688 case EXEC_OMP_PARALLEL_WORKSHARE:
8689 case EXEC_OMP_TASK:
8690 omp_workshare_save = omp_workshare_flag;
8691 omp_workshare_flag = 0;
8692 gfc_resolve_omp_directive (code, ns);
8693 omp_workshare_flag = omp_workshare_save;
8694 break;
8696 default:
8697 gfc_internal_error ("resolve_code(): Bad statement code");
8701 cs_base = frame.prev;
8705 /* Resolve initial values and make sure they are compatible with
8706 the variable. */
8708 static void
8709 resolve_values (gfc_symbol *sym)
8711 if (sym->value == NULL)
8712 return;
8714 if (gfc_resolve_expr (sym->value) == FAILURE)
8715 return;
8717 gfc_check_assign_symbol (sym, sym->value);
8721 /* Verify the binding labels for common blocks that are BIND(C). The label
8722 for a BIND(C) common block must be identical in all scoping units in which
8723 the common block is declared. Further, the binding label can not collide
8724 with any other global entity in the program. */
8726 static void
8727 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
8729 if (comm_block_tree->n.common->is_bind_c == 1)
8731 gfc_gsymbol *binding_label_gsym;
8732 gfc_gsymbol *comm_name_gsym;
8734 /* See if a global symbol exists by the common block's name. It may
8735 be NULL if the common block is use-associated. */
8736 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
8737 comm_block_tree->n.common->name);
8738 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
8739 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
8740 "with the global entity '%s' at %L",
8741 comm_block_tree->n.common->binding_label,
8742 comm_block_tree->n.common->name,
8743 &(comm_block_tree->n.common->where),
8744 comm_name_gsym->name, &(comm_name_gsym->where));
8745 else if (comm_name_gsym != NULL
8746 && strcmp (comm_name_gsym->name,
8747 comm_block_tree->n.common->name) == 0)
8749 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
8750 as expected. */
8751 if (comm_name_gsym->binding_label == NULL)
8752 /* No binding label for common block stored yet; save this one. */
8753 comm_name_gsym->binding_label =
8754 comm_block_tree->n.common->binding_label;
8755 else
8756 if (strcmp (comm_name_gsym->binding_label,
8757 comm_block_tree->n.common->binding_label) != 0)
8759 /* Common block names match but binding labels do not. */
8760 gfc_error ("Binding label '%s' for common block '%s' at %L "
8761 "does not match the binding label '%s' for common "
8762 "block '%s' at %L",
8763 comm_block_tree->n.common->binding_label,
8764 comm_block_tree->n.common->name,
8765 &(comm_block_tree->n.common->where),
8766 comm_name_gsym->binding_label,
8767 comm_name_gsym->name,
8768 &(comm_name_gsym->where));
8769 return;
8773 /* There is no binding label (NAME="") so we have nothing further to
8774 check and nothing to add as a global symbol for the label. */
8775 if (comm_block_tree->n.common->binding_label[0] == '\0' )
8776 return;
8778 binding_label_gsym =
8779 gfc_find_gsymbol (gfc_gsym_root,
8780 comm_block_tree->n.common->binding_label);
8781 if (binding_label_gsym == NULL)
8783 /* Need to make a global symbol for the binding label to prevent
8784 it from colliding with another. */
8785 binding_label_gsym =
8786 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
8787 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
8788 binding_label_gsym->type = GSYM_COMMON;
8790 else
8792 /* If comm_name_gsym is NULL, the name common block is use
8793 associated and the name could be colliding. */
8794 if (binding_label_gsym->type != GSYM_COMMON)
8795 gfc_error ("Binding label '%s' for common block '%s' at %L "
8796 "collides with the global entity '%s' at %L",
8797 comm_block_tree->n.common->binding_label,
8798 comm_block_tree->n.common->name,
8799 &(comm_block_tree->n.common->where),
8800 binding_label_gsym->name,
8801 &(binding_label_gsym->where));
8802 else if (comm_name_gsym != NULL
8803 && (strcmp (binding_label_gsym->name,
8804 comm_name_gsym->binding_label) != 0)
8805 && (strcmp (binding_label_gsym->sym_name,
8806 comm_name_gsym->name) != 0))
8807 gfc_error ("Binding label '%s' for common block '%s' at %L "
8808 "collides with global entity '%s' at %L",
8809 binding_label_gsym->name, binding_label_gsym->sym_name,
8810 &(comm_block_tree->n.common->where),
8811 comm_name_gsym->name, &(comm_name_gsym->where));
8815 return;
8819 /* Verify any BIND(C) derived types in the namespace so we can report errors
8820 for them once, rather than for each variable declared of that type. */
8822 static void
8823 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
8825 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
8826 && derived_sym->attr.is_bind_c == 1)
8827 verify_bind_c_derived_type (derived_sym);
8829 return;
8833 /* Verify that any binding labels used in a given namespace do not collide
8834 with the names or binding labels of any global symbols. */
8836 static void
8837 gfc_verify_binding_labels (gfc_symbol *sym)
8839 int has_error = 0;
8841 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
8842 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
8844 gfc_gsymbol *bind_c_sym;
8846 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
8847 if (bind_c_sym != NULL
8848 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
8850 if (sym->attr.if_source == IFSRC_DECL
8851 && (bind_c_sym->type != GSYM_SUBROUTINE
8852 && bind_c_sym->type != GSYM_FUNCTION)
8853 && ((sym->attr.contained == 1
8854 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
8855 || (sym->attr.use_assoc == 1
8856 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
8858 /* Make sure global procedures don't collide with anything. */
8859 gfc_error ("Binding label '%s' at %L collides with the global "
8860 "entity '%s' at %L", sym->binding_label,
8861 &(sym->declared_at), bind_c_sym->name,
8862 &(bind_c_sym->where));
8863 has_error = 1;
8865 else if (sym->attr.contained == 0
8866 && (sym->attr.if_source == IFSRC_IFBODY
8867 && sym->attr.flavor == FL_PROCEDURE)
8868 && (bind_c_sym->sym_name != NULL
8869 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
8871 /* Make sure procedures in interface bodies don't collide. */
8872 gfc_error ("Binding label '%s' in interface body at %L collides "
8873 "with the global entity '%s' at %L",
8874 sym->binding_label,
8875 &(sym->declared_at), bind_c_sym->name,
8876 &(bind_c_sym->where));
8877 has_error = 1;
8879 else if (sym->attr.contained == 0
8880 && sym->attr.if_source == IFSRC_UNKNOWN)
8881 if ((sym->attr.use_assoc && bind_c_sym->mod_name
8882 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
8883 || sym->attr.use_assoc == 0)
8885 gfc_error ("Binding label '%s' at %L collides with global "
8886 "entity '%s' at %L", sym->binding_label,
8887 &(sym->declared_at), bind_c_sym->name,
8888 &(bind_c_sym->where));
8889 has_error = 1;
8892 if (has_error != 0)
8893 /* Clear the binding label to prevent checking multiple times. */
8894 sym->binding_label[0] = '\0';
8896 else if (bind_c_sym == NULL)
8898 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
8899 bind_c_sym->where = sym->declared_at;
8900 bind_c_sym->sym_name = sym->name;
8902 if (sym->attr.use_assoc == 1)
8903 bind_c_sym->mod_name = sym->module;
8904 else
8905 if (sym->ns->proc_name != NULL)
8906 bind_c_sym->mod_name = sym->ns->proc_name->name;
8908 if (sym->attr.contained == 0)
8910 if (sym->attr.subroutine)
8911 bind_c_sym->type = GSYM_SUBROUTINE;
8912 else if (sym->attr.function)
8913 bind_c_sym->type = GSYM_FUNCTION;
8917 return;
8921 /* Resolve an index expression. */
8923 static gfc_try
8924 resolve_index_expr (gfc_expr *e)
8926 if (gfc_resolve_expr (e) == FAILURE)
8927 return FAILURE;
8929 if (gfc_simplify_expr (e, 0) == FAILURE)
8930 return FAILURE;
8932 if (gfc_specification_expr (e) == FAILURE)
8933 return FAILURE;
8935 return SUCCESS;
8938 /* Resolve a charlen structure. */
8940 static gfc_try
8941 resolve_charlen (gfc_charlen *cl)
8943 int i, k;
8945 if (cl->resolved)
8946 return SUCCESS;
8948 cl->resolved = 1;
8950 specification_expr = 1;
8952 if (resolve_index_expr (cl->length) == FAILURE)
8954 specification_expr = 0;
8955 return FAILURE;
8958 /* "If the character length parameter value evaluates to a negative
8959 value, the length of character entities declared is zero." */
8960 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
8962 if (gfc_option.warn_surprising)
8963 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
8964 " the length has been set to zero",
8965 &cl->length->where, i);
8966 gfc_replace_expr (cl->length,
8967 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
8970 /* Check that the character length is not too large. */
8971 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
8972 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
8973 && cl->length->ts.type == BT_INTEGER
8974 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
8976 gfc_error ("String length at %L is too large", &cl->length->where);
8977 return FAILURE;
8980 return SUCCESS;
8984 /* Test for non-constant shape arrays. */
8986 static bool
8987 is_non_constant_shape_array (gfc_symbol *sym)
8989 gfc_expr *e;
8990 int i;
8991 bool not_constant;
8993 not_constant = false;
8994 if (sym->as != NULL)
8996 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
8997 has not been simplified; parameter array references. Do the
8998 simplification now. */
8999 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
9001 e = sym->as->lower[i];
9002 if (e && (resolve_index_expr (e) == FAILURE
9003 || !gfc_is_constant_expr (e)))
9004 not_constant = true;
9005 e = sym->as->upper[i];
9006 if (e && (resolve_index_expr (e) == FAILURE
9007 || !gfc_is_constant_expr (e)))
9008 not_constant = true;
9011 return not_constant;
9014 /* Given a symbol and an initialization expression, add code to initialize
9015 the symbol to the function entry. */
9016 static void
9017 build_init_assign (gfc_symbol *sym, gfc_expr *init)
9019 gfc_expr *lval;
9020 gfc_code *init_st;
9021 gfc_namespace *ns = sym->ns;
9023 /* Search for the function namespace if this is a contained
9024 function without an explicit result. */
9025 if (sym->attr.function && sym == sym->result
9026 && sym->name != sym->ns->proc_name->name)
9028 ns = ns->contained;
9029 for (;ns; ns = ns->sibling)
9030 if (strcmp (ns->proc_name->name, sym->name) == 0)
9031 break;
9034 if (ns == NULL)
9036 gfc_free_expr (init);
9037 return;
9040 /* Build an l-value expression for the result. */
9041 lval = gfc_lval_expr_from_sym (sym);
9043 /* Add the code at scope entry. */
9044 init_st = gfc_get_code ();
9045 init_st->next = ns->code;
9046 ns->code = init_st;
9048 /* Assign the default initializer to the l-value. */
9049 init_st->loc = sym->declared_at;
9050 init_st->op = EXEC_INIT_ASSIGN;
9051 init_st->expr1 = lval;
9052 init_st->expr2 = init;
9055 /* Assign the default initializer to a derived type variable or result. */
9057 static void
9058 apply_default_init (gfc_symbol *sym)
9060 gfc_expr *init = NULL;
9062 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9063 return;
9065 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
9066 init = gfc_default_initializer (&sym->ts);
9068 if (init == NULL)
9069 return;
9071 build_init_assign (sym, init);
9074 /* Build an initializer for a local integer, real, complex, logical, or
9075 character variable, based on the command line flags finit-local-zero,
9076 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
9077 null if the symbol should not have a default initialization. */
9078 static gfc_expr *
9079 build_default_init_expr (gfc_symbol *sym)
9081 int char_len;
9082 gfc_expr *init_expr;
9083 int i;
9085 /* These symbols should never have a default initialization. */
9086 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
9087 || sym->attr.external
9088 || sym->attr.dummy
9089 || sym->attr.pointer
9090 || sym->attr.in_equivalence
9091 || sym->attr.in_common
9092 || sym->attr.data
9093 || sym->module
9094 || sym->attr.cray_pointee
9095 || sym->attr.cray_pointer)
9096 return NULL;
9098 /* Now we'll try to build an initializer expression. */
9099 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
9100 &sym->declared_at);
9102 /* We will only initialize integers, reals, complex, logicals, and
9103 characters, and only if the corresponding command-line flags
9104 were set. Otherwise, we free init_expr and return null. */
9105 switch (sym->ts.type)
9107 case BT_INTEGER:
9108 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
9109 mpz_set_si (init_expr->value.integer,
9110 gfc_option.flag_init_integer_value);
9111 else
9113 gfc_free_expr (init_expr);
9114 init_expr = NULL;
9116 break;
9118 case BT_REAL:
9119 switch (gfc_option.flag_init_real)
9121 case GFC_INIT_REAL_SNAN:
9122 init_expr->is_snan = 1;
9123 /* Fall through. */
9124 case GFC_INIT_REAL_NAN:
9125 mpfr_set_nan (init_expr->value.real);
9126 break;
9128 case GFC_INIT_REAL_INF:
9129 mpfr_set_inf (init_expr->value.real, 1);
9130 break;
9132 case GFC_INIT_REAL_NEG_INF:
9133 mpfr_set_inf (init_expr->value.real, -1);
9134 break;
9136 case GFC_INIT_REAL_ZERO:
9137 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
9138 break;
9140 default:
9141 gfc_free_expr (init_expr);
9142 init_expr = NULL;
9143 break;
9145 break;
9147 case BT_COMPLEX:
9148 switch (gfc_option.flag_init_real)
9150 case GFC_INIT_REAL_SNAN:
9151 init_expr->is_snan = 1;
9152 /* Fall through. */
9153 case GFC_INIT_REAL_NAN:
9154 mpfr_set_nan (mpc_realref (init_expr->value.complex));
9155 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
9156 break;
9158 case GFC_INIT_REAL_INF:
9159 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
9160 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
9161 break;
9163 case GFC_INIT_REAL_NEG_INF:
9164 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
9165 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
9166 break;
9168 case GFC_INIT_REAL_ZERO:
9169 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
9170 break;
9172 default:
9173 gfc_free_expr (init_expr);
9174 init_expr = NULL;
9175 break;
9177 break;
9179 case BT_LOGICAL:
9180 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
9181 init_expr->value.logical = 0;
9182 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
9183 init_expr->value.logical = 1;
9184 else
9186 gfc_free_expr (init_expr);
9187 init_expr = NULL;
9189 break;
9191 case BT_CHARACTER:
9192 /* For characters, the length must be constant in order to
9193 create a default initializer. */
9194 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
9195 && sym->ts.u.cl->length
9196 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9198 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
9199 init_expr->value.character.length = char_len;
9200 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
9201 for (i = 0; i < char_len; i++)
9202 init_expr->value.character.string[i]
9203 = (unsigned char) gfc_option.flag_init_character_value;
9205 else
9207 gfc_free_expr (init_expr);
9208 init_expr = NULL;
9210 break;
9212 default:
9213 gfc_free_expr (init_expr);
9214 init_expr = NULL;
9216 return init_expr;
9219 /* Add an initialization expression to a local variable. */
9220 static void
9221 apply_default_init_local (gfc_symbol *sym)
9223 gfc_expr *init = NULL;
9225 /* The symbol should be a variable or a function return value. */
9226 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9227 || (sym->attr.function && sym->result != sym))
9228 return;
9230 /* Try to build the initializer expression. If we can't initialize
9231 this symbol, then init will be NULL. */
9232 init = build_default_init_expr (sym);
9233 if (init == NULL)
9234 return;
9236 /* For saved variables, we don't want to add an initializer at
9237 function entry, so we just add a static initializer. */
9238 if (sym->attr.save || sym->ns->save_all
9239 || gfc_option.flag_max_stack_var_size == 0)
9241 /* Don't clobber an existing initializer! */
9242 gcc_assert (sym->value == NULL);
9243 sym->value = init;
9244 return;
9247 build_init_assign (sym, init);
9250 /* Resolution of common features of flavors variable and procedure. */
9252 static gfc_try
9253 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
9255 /* Constraints on deferred shape variable. */
9256 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
9258 if (sym->attr.allocatable)
9260 if (sym->attr.dimension)
9262 gfc_error ("Allocatable array '%s' at %L must have "
9263 "a deferred shape", sym->name, &sym->declared_at);
9264 return FAILURE;
9266 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
9267 "may not be ALLOCATABLE", sym->name,
9268 &sym->declared_at) == FAILURE)
9269 return FAILURE;
9272 if (sym->attr.pointer && sym->attr.dimension)
9274 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
9275 sym->name, &sym->declared_at);
9276 return FAILURE;
9280 else
9282 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
9283 && !sym->attr.dummy && sym->ts.type != BT_CLASS)
9285 gfc_error ("Array '%s' at %L cannot have a deferred shape",
9286 sym->name, &sym->declared_at);
9287 return FAILURE;
9291 /* Constraints on polymorphic variables. */
9292 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
9294 /* F03:C502. */
9295 if (sym->attr.class_ok
9296 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
9298 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
9299 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
9300 &sym->declared_at);
9301 return FAILURE;
9304 /* F03:C509. */
9305 /* Assume that use associated symbols were checked in the module ns. */
9306 if (!sym->attr.class_ok && !sym->attr.use_assoc)
9308 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
9309 "or pointer", sym->name, &sym->declared_at);
9310 return FAILURE;
9314 return SUCCESS;
9318 /* Additional checks for symbols with flavor variable and derived
9319 type. To be called from resolve_fl_variable. */
9321 static gfc_try
9322 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
9324 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
9326 /* Check to see if a derived type is blocked from being host
9327 associated by the presence of another class I symbol in the same
9328 namespace. 14.6.1.3 of the standard and the discussion on
9329 comp.lang.fortran. */
9330 if (sym->ns != sym->ts.u.derived->ns
9331 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
9333 gfc_symbol *s;
9334 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
9335 if (s && s->attr.flavor != FL_DERIVED)
9337 gfc_error ("The type '%s' cannot be host associated at %L "
9338 "because it is blocked by an incompatible object "
9339 "of the same name declared at %L",
9340 sym->ts.u.derived->name, &sym->declared_at,
9341 &s->declared_at);
9342 return FAILURE;
9346 /* 4th constraint in section 11.3: "If an object of a type for which
9347 component-initialization is specified (R429) appears in the
9348 specification-part of a module and does not have the ALLOCATABLE
9349 or POINTER attribute, the object shall have the SAVE attribute."
9351 The check for initializers is performed with
9352 gfc_has_default_initializer because gfc_default_initializer generates
9353 a hidden default for allocatable components. */
9354 if (!(sym->value || no_init_flag) && sym->ns->proc_name
9355 && sym->ns->proc_name->attr.flavor == FL_MODULE
9356 && !sym->ns->save_all && !sym->attr.save
9357 && !sym->attr.pointer && !sym->attr.allocatable
9358 && gfc_has_default_initializer (sym->ts.u.derived)
9359 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Implied SAVE for "
9360 "module variable '%s' at %L, needed due to "
9361 "the default initialization", sym->name,
9362 &sym->declared_at) == FAILURE)
9363 return FAILURE;
9365 /* Assign default initializer. */
9366 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
9367 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
9369 sym->value = gfc_default_initializer (&sym->ts);
9372 return SUCCESS;
9376 /* Resolve symbols with flavor variable. */
9378 static gfc_try
9379 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
9381 int no_init_flag, automatic_flag;
9382 gfc_expr *e;
9383 const char *auto_save_msg;
9385 auto_save_msg = "Automatic object '%s' at %L cannot have the "
9386 "SAVE attribute";
9388 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9389 return FAILURE;
9391 /* Set this flag to check that variables are parameters of all entries.
9392 This check is effected by the call to gfc_resolve_expr through
9393 is_non_constant_shape_array. */
9394 specification_expr = 1;
9396 if (sym->ns->proc_name
9397 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9398 || sym->ns->proc_name->attr.is_main_program)
9399 && !sym->attr.use_assoc
9400 && !sym->attr.allocatable
9401 && !sym->attr.pointer
9402 && is_non_constant_shape_array (sym))
9404 /* The shape of a main program or module array needs to be
9405 constant. */
9406 gfc_error ("The module or main program array '%s' at %L must "
9407 "have constant shape", sym->name, &sym->declared_at);
9408 specification_expr = 0;
9409 return FAILURE;
9412 if (sym->ts.type == BT_CHARACTER)
9414 /* Make sure that character string variables with assumed length are
9415 dummy arguments. */
9416 e = sym->ts.u.cl->length;
9417 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
9419 gfc_error ("Entity with assumed character length at %L must be a "
9420 "dummy argument or a PARAMETER", &sym->declared_at);
9421 return FAILURE;
9424 if (e && sym->attr.save && !gfc_is_constant_expr (e))
9426 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9427 return FAILURE;
9430 if (!gfc_is_constant_expr (e)
9431 && !(e->expr_type == EXPR_VARIABLE
9432 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
9433 && sym->ns->proc_name
9434 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9435 || sym->ns->proc_name->attr.is_main_program)
9436 && !sym->attr.use_assoc)
9438 gfc_error ("'%s' at %L must have constant character length "
9439 "in this context", sym->name, &sym->declared_at);
9440 return FAILURE;
9444 if (sym->value == NULL && sym->attr.referenced)
9445 apply_default_init_local (sym); /* Try to apply a default initialization. */
9447 /* Determine if the symbol may not have an initializer. */
9448 no_init_flag = automatic_flag = 0;
9449 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
9450 || sym->attr.intrinsic || sym->attr.result)
9451 no_init_flag = 1;
9452 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
9453 && is_non_constant_shape_array (sym))
9455 no_init_flag = automatic_flag = 1;
9457 /* Also, they must not have the SAVE attribute.
9458 SAVE_IMPLICIT is checked below. */
9459 if (sym->attr.save == SAVE_EXPLICIT)
9461 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9462 return FAILURE;
9466 /* Ensure that any initializer is simplified. */
9467 if (sym->value)
9468 gfc_simplify_expr (sym->value, 1);
9470 /* Reject illegal initializers. */
9471 if (!sym->mark && sym->value)
9473 if (sym->attr.allocatable)
9474 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
9475 sym->name, &sym->declared_at);
9476 else if (sym->attr.external)
9477 gfc_error ("External '%s' at %L cannot have an initializer",
9478 sym->name, &sym->declared_at);
9479 else if (sym->attr.dummy
9480 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
9481 gfc_error ("Dummy '%s' at %L cannot have an initializer",
9482 sym->name, &sym->declared_at);
9483 else if (sym->attr.intrinsic)
9484 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
9485 sym->name, &sym->declared_at);
9486 else if (sym->attr.result)
9487 gfc_error ("Function result '%s' at %L cannot have an initializer",
9488 sym->name, &sym->declared_at);
9489 else if (automatic_flag)
9490 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
9491 sym->name, &sym->declared_at);
9492 else
9493 goto no_init_error;
9494 return FAILURE;
9497 no_init_error:
9498 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
9499 return resolve_fl_variable_derived (sym, no_init_flag);
9501 return SUCCESS;
9505 /* Resolve a procedure. */
9507 static gfc_try
9508 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
9510 gfc_formal_arglist *arg;
9512 if (sym->attr.function
9513 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9514 return FAILURE;
9516 if (sym->ts.type == BT_CHARACTER)
9518 gfc_charlen *cl = sym->ts.u.cl;
9520 if (cl && cl->length && gfc_is_constant_expr (cl->length)
9521 && resolve_charlen (cl) == FAILURE)
9522 return FAILURE;
9524 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
9525 && sym->attr.proc == PROC_ST_FUNCTION)
9527 gfc_error ("Character-valued statement function '%s' at %L must "
9528 "have constant length", sym->name, &sym->declared_at);
9529 return FAILURE;
9533 /* Ensure that derived type for are not of a private type. Internal
9534 module procedures are excluded by 2.2.3.3 - i.e., they are not
9535 externally accessible and can access all the objects accessible in
9536 the host. */
9537 if (!(sym->ns->parent
9538 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
9539 && gfc_check_access(sym->attr.access, sym->ns->default_access))
9541 gfc_interface *iface;
9543 for (arg = sym->formal; arg; arg = arg->next)
9545 if (arg->sym
9546 && arg->sym->ts.type == BT_DERIVED
9547 && !arg->sym->ts.u.derived->attr.use_assoc
9548 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9549 arg->sym->ts.u.derived->ns->default_access)
9550 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
9551 "PRIVATE type and cannot be a dummy argument"
9552 " of '%s', which is PUBLIC at %L",
9553 arg->sym->name, sym->name, &sym->declared_at)
9554 == FAILURE)
9556 /* Stop this message from recurring. */
9557 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9558 return FAILURE;
9562 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9563 PRIVATE to the containing module. */
9564 for (iface = sym->generic; iface; iface = iface->next)
9566 for (arg = iface->sym->formal; arg; arg = arg->next)
9568 if (arg->sym
9569 && arg->sym->ts.type == BT_DERIVED
9570 && !arg->sym->ts.u.derived->attr.use_assoc
9571 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9572 arg->sym->ts.u.derived->ns->default_access)
9573 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9574 "'%s' in PUBLIC interface '%s' at %L "
9575 "takes dummy arguments of '%s' which is "
9576 "PRIVATE", iface->sym->name, sym->name,
9577 &iface->sym->declared_at,
9578 gfc_typename (&arg->sym->ts)) == FAILURE)
9580 /* Stop this message from recurring. */
9581 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9582 return FAILURE;
9587 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9588 PRIVATE to the containing module. */
9589 for (iface = sym->generic; iface; iface = iface->next)
9591 for (arg = iface->sym->formal; arg; arg = arg->next)
9593 if (arg->sym
9594 && arg->sym->ts.type == BT_DERIVED
9595 && !arg->sym->ts.u.derived->attr.use_assoc
9596 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9597 arg->sym->ts.u.derived->ns->default_access)
9598 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9599 "'%s' in PUBLIC interface '%s' at %L "
9600 "takes dummy arguments of '%s' which is "
9601 "PRIVATE", iface->sym->name, sym->name,
9602 &iface->sym->declared_at,
9603 gfc_typename (&arg->sym->ts)) == FAILURE)
9605 /* Stop this message from recurring. */
9606 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9607 return FAILURE;
9613 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
9614 && !sym->attr.proc_pointer)
9616 gfc_error ("Function '%s' at %L cannot have an initializer",
9617 sym->name, &sym->declared_at);
9618 return FAILURE;
9621 /* An external symbol may not have an initializer because it is taken to be
9622 a procedure. Exception: Procedure Pointers. */
9623 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
9625 gfc_error ("External object '%s' at %L may not have an initializer",
9626 sym->name, &sym->declared_at);
9627 return FAILURE;
9630 /* An elemental function is required to return a scalar 12.7.1 */
9631 if (sym->attr.elemental && sym->attr.function && sym->as)
9633 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
9634 "result", sym->name, &sym->declared_at);
9635 /* Reset so that the error only occurs once. */
9636 sym->attr.elemental = 0;
9637 return FAILURE;
9640 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
9641 char-len-param shall not be array-valued, pointer-valued, recursive
9642 or pure. ....snip... A character value of * may only be used in the
9643 following ways: (i) Dummy arg of procedure - dummy associates with
9644 actual length; (ii) To declare a named constant; or (iii) External
9645 function - but length must be declared in calling scoping unit. */
9646 if (sym->attr.function
9647 && sym->ts.type == BT_CHARACTER
9648 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
9650 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
9651 || (sym->attr.recursive) || (sym->attr.pure))
9653 if (sym->as && sym->as->rank)
9654 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9655 "array-valued", sym->name, &sym->declared_at);
9657 if (sym->attr.pointer)
9658 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9659 "pointer-valued", sym->name, &sym->declared_at);
9661 if (sym->attr.pure)
9662 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9663 "pure", sym->name, &sym->declared_at);
9665 if (sym->attr.recursive)
9666 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9667 "recursive", sym->name, &sym->declared_at);
9669 return FAILURE;
9672 /* Appendix B.2 of the standard. Contained functions give an
9673 error anyway. Fixed-form is likely to be F77/legacy. */
9674 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
9675 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
9676 "CHARACTER(*) function '%s' at %L",
9677 sym->name, &sym->declared_at);
9680 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
9682 gfc_formal_arglist *curr_arg;
9683 int has_non_interop_arg = 0;
9685 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
9686 sym->common_block) == FAILURE)
9688 /* Clear these to prevent looking at them again if there was an
9689 error. */
9690 sym->attr.is_bind_c = 0;
9691 sym->attr.is_c_interop = 0;
9692 sym->ts.is_c_interop = 0;
9694 else
9696 /* So far, no errors have been found. */
9697 sym->attr.is_c_interop = 1;
9698 sym->ts.is_c_interop = 1;
9701 curr_arg = sym->formal;
9702 while (curr_arg != NULL)
9704 /* Skip implicitly typed dummy args here. */
9705 if (curr_arg->sym->attr.implicit_type == 0)
9706 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
9707 /* If something is found to fail, record the fact so we
9708 can mark the symbol for the procedure as not being
9709 BIND(C) to try and prevent multiple errors being
9710 reported. */
9711 has_non_interop_arg = 1;
9713 curr_arg = curr_arg->next;
9716 /* See if any of the arguments were not interoperable and if so, clear
9717 the procedure symbol to prevent duplicate error messages. */
9718 if (has_non_interop_arg != 0)
9720 sym->attr.is_c_interop = 0;
9721 sym->ts.is_c_interop = 0;
9722 sym->attr.is_bind_c = 0;
9726 if (!sym->attr.proc_pointer)
9728 if (sym->attr.save == SAVE_EXPLICIT)
9730 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
9731 "in '%s' at %L", sym->name, &sym->declared_at);
9732 return FAILURE;
9734 if (sym->attr.intent)
9736 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
9737 "in '%s' at %L", sym->name, &sym->declared_at);
9738 return FAILURE;
9740 if (sym->attr.subroutine && sym->attr.result)
9742 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
9743 "in '%s' at %L", sym->name, &sym->declared_at);
9744 return FAILURE;
9746 if (sym->attr.external && sym->attr.function
9747 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
9748 || sym->attr.contained))
9750 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
9751 "in '%s' at %L", sym->name, &sym->declared_at);
9752 return FAILURE;
9754 if (strcmp ("ppr@", sym->name) == 0)
9756 gfc_error ("Procedure pointer result '%s' at %L "
9757 "is missing the pointer attribute",
9758 sym->ns->proc_name->name, &sym->declared_at);
9759 return FAILURE;
9763 return SUCCESS;
9767 /* Resolve a list of finalizer procedures. That is, after they have hopefully
9768 been defined and we now know their defined arguments, check that they fulfill
9769 the requirements of the standard for procedures used as finalizers. */
9771 static gfc_try
9772 gfc_resolve_finalizers (gfc_symbol* derived)
9774 gfc_finalizer* list;
9775 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
9776 gfc_try result = SUCCESS;
9777 bool seen_scalar = false;
9779 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
9780 return SUCCESS;
9782 /* Walk over the list of finalizer-procedures, check them, and if any one
9783 does not fit in with the standard's definition, print an error and remove
9784 it from the list. */
9785 prev_link = &derived->f2k_derived->finalizers;
9786 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
9788 gfc_symbol* arg;
9789 gfc_finalizer* i;
9790 int my_rank;
9792 /* Skip this finalizer if we already resolved it. */
9793 if (list->proc_tree)
9795 prev_link = &(list->next);
9796 continue;
9799 /* Check this exists and is a SUBROUTINE. */
9800 if (!list->proc_sym->attr.subroutine)
9802 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
9803 list->proc_sym->name, &list->where);
9804 goto error;
9807 /* We should have exactly one argument. */
9808 if (!list->proc_sym->formal || list->proc_sym->formal->next)
9810 gfc_error ("FINAL procedure at %L must have exactly one argument",
9811 &list->where);
9812 goto error;
9814 arg = list->proc_sym->formal->sym;
9816 /* This argument must be of our type. */
9817 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
9819 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
9820 &arg->declared_at, derived->name);
9821 goto error;
9824 /* It must neither be a pointer nor allocatable nor optional. */
9825 if (arg->attr.pointer)
9827 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
9828 &arg->declared_at);
9829 goto error;
9831 if (arg->attr.allocatable)
9833 gfc_error ("Argument of FINAL procedure at %L must not be"
9834 " ALLOCATABLE", &arg->declared_at);
9835 goto error;
9837 if (arg->attr.optional)
9839 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
9840 &arg->declared_at);
9841 goto error;
9844 /* It must not be INTENT(OUT). */
9845 if (arg->attr.intent == INTENT_OUT)
9847 gfc_error ("Argument of FINAL procedure at %L must not be"
9848 " INTENT(OUT)", &arg->declared_at);
9849 goto error;
9852 /* Warn if the procedure is non-scalar and not assumed shape. */
9853 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
9854 && arg->as->type != AS_ASSUMED_SHAPE)
9855 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
9856 " shape argument", &arg->declared_at);
9858 /* Check that it does not match in kind and rank with a FINAL procedure
9859 defined earlier. To really loop over the *earlier* declarations,
9860 we need to walk the tail of the list as new ones were pushed at the
9861 front. */
9862 /* TODO: Handle kind parameters once they are implemented. */
9863 my_rank = (arg->as ? arg->as->rank : 0);
9864 for (i = list->next; i; i = i->next)
9866 /* Argument list might be empty; that is an error signalled earlier,
9867 but we nevertheless continued resolving. */
9868 if (i->proc_sym->formal)
9870 gfc_symbol* i_arg = i->proc_sym->formal->sym;
9871 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
9872 if (i_rank == my_rank)
9874 gfc_error ("FINAL procedure '%s' declared at %L has the same"
9875 " rank (%d) as '%s'",
9876 list->proc_sym->name, &list->where, my_rank,
9877 i->proc_sym->name);
9878 goto error;
9883 /* Is this the/a scalar finalizer procedure? */
9884 if (!arg->as || arg->as->rank == 0)
9885 seen_scalar = true;
9887 /* Find the symtree for this procedure. */
9888 gcc_assert (!list->proc_tree);
9889 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
9891 prev_link = &list->next;
9892 continue;
9894 /* Remove wrong nodes immediately from the list so we don't risk any
9895 troubles in the future when they might fail later expectations. */
9896 error:
9897 result = FAILURE;
9898 i = list;
9899 *prev_link = list->next;
9900 gfc_free_finalizer (i);
9903 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
9904 were nodes in the list, must have been for arrays. It is surely a good
9905 idea to have a scalar version there if there's something to finalize. */
9906 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
9907 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
9908 " defined at %L, suggest also scalar one",
9909 derived->name, &derived->declared_at);
9911 /* TODO: Remove this error when finalization is finished. */
9912 gfc_error ("Finalization at %L is not yet implemented",
9913 &derived->declared_at);
9915 return result;
9919 /* Check that it is ok for the typebound procedure proc to override the
9920 procedure old. */
9922 static gfc_try
9923 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
9925 locus where;
9926 const gfc_symbol* proc_target;
9927 const gfc_symbol* old_target;
9928 unsigned proc_pass_arg, old_pass_arg, argpos;
9929 gfc_formal_arglist* proc_formal;
9930 gfc_formal_arglist* old_formal;
9932 /* This procedure should only be called for non-GENERIC proc. */
9933 gcc_assert (!proc->n.tb->is_generic);
9935 /* If the overwritten procedure is GENERIC, this is an error. */
9936 if (old->n.tb->is_generic)
9938 gfc_error ("Can't overwrite GENERIC '%s' at %L",
9939 old->name, &proc->n.tb->where);
9940 return FAILURE;
9943 where = proc->n.tb->where;
9944 proc_target = proc->n.tb->u.specific->n.sym;
9945 old_target = old->n.tb->u.specific->n.sym;
9947 /* Check that overridden binding is not NON_OVERRIDABLE. */
9948 if (old->n.tb->non_overridable)
9950 gfc_error ("'%s' at %L overrides a procedure binding declared"
9951 " NON_OVERRIDABLE", proc->name, &where);
9952 return FAILURE;
9955 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
9956 if (!old->n.tb->deferred && proc->n.tb->deferred)
9958 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
9959 " non-DEFERRED binding", proc->name, &where);
9960 return FAILURE;
9963 /* If the overridden binding is PURE, the overriding must be, too. */
9964 if (old_target->attr.pure && !proc_target->attr.pure)
9966 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
9967 proc->name, &where);
9968 return FAILURE;
9971 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
9972 is not, the overriding must not be either. */
9973 if (old_target->attr.elemental && !proc_target->attr.elemental)
9975 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
9976 " ELEMENTAL", proc->name, &where);
9977 return FAILURE;
9979 if (!old_target->attr.elemental && proc_target->attr.elemental)
9981 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
9982 " be ELEMENTAL, either", proc->name, &where);
9983 return FAILURE;
9986 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
9987 SUBROUTINE. */
9988 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
9990 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
9991 " SUBROUTINE", proc->name, &where);
9992 return FAILURE;
9995 /* If the overridden binding is a FUNCTION, the overriding must also be a
9996 FUNCTION and have the same characteristics. */
9997 if (old_target->attr.function)
9999 if (!proc_target->attr.function)
10001 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
10002 " FUNCTION", proc->name, &where);
10003 return FAILURE;
10006 /* FIXME: Do more comprehensive checking (including, for instance, the
10007 rank and array-shape). */
10008 gcc_assert (proc_target->result && old_target->result);
10009 if (!gfc_compare_types (&proc_target->result->ts,
10010 &old_target->result->ts))
10012 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
10013 " matching result types", proc->name, &where);
10014 return FAILURE;
10018 /* If the overridden binding is PUBLIC, the overriding one must not be
10019 PRIVATE. */
10020 if (old->n.tb->access == ACCESS_PUBLIC
10021 && proc->n.tb->access == ACCESS_PRIVATE)
10023 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
10024 " PRIVATE", proc->name, &where);
10025 return FAILURE;
10028 /* Compare the formal argument lists of both procedures. This is also abused
10029 to find the position of the passed-object dummy arguments of both
10030 bindings as at least the overridden one might not yet be resolved and we
10031 need those positions in the check below. */
10032 proc_pass_arg = old_pass_arg = 0;
10033 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
10034 proc_pass_arg = 1;
10035 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
10036 old_pass_arg = 1;
10037 argpos = 1;
10038 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
10039 proc_formal && old_formal;
10040 proc_formal = proc_formal->next, old_formal = old_formal->next)
10042 if (proc->n.tb->pass_arg
10043 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
10044 proc_pass_arg = argpos;
10045 if (old->n.tb->pass_arg
10046 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
10047 old_pass_arg = argpos;
10049 /* Check that the names correspond. */
10050 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
10052 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
10053 " to match the corresponding argument of the overridden"
10054 " procedure", proc_formal->sym->name, proc->name, &where,
10055 old_formal->sym->name);
10056 return FAILURE;
10059 /* Check that the types correspond if neither is the passed-object
10060 argument. */
10061 /* FIXME: Do more comprehensive testing here. */
10062 if (proc_pass_arg != argpos && old_pass_arg != argpos
10063 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
10065 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
10066 "in respect to the overridden procedure",
10067 proc_formal->sym->name, proc->name, &where);
10068 return FAILURE;
10071 ++argpos;
10073 if (proc_formal || old_formal)
10075 gfc_error ("'%s' at %L must have the same number of formal arguments as"
10076 " the overridden procedure", proc->name, &where);
10077 return FAILURE;
10080 /* If the overridden binding is NOPASS, the overriding one must also be
10081 NOPASS. */
10082 if (old->n.tb->nopass && !proc->n.tb->nopass)
10084 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
10085 " NOPASS", proc->name, &where);
10086 return FAILURE;
10089 /* If the overridden binding is PASS(x), the overriding one must also be
10090 PASS and the passed-object dummy arguments must correspond. */
10091 if (!old->n.tb->nopass)
10093 if (proc->n.tb->nopass)
10095 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
10096 " PASS", proc->name, &where);
10097 return FAILURE;
10100 if (proc_pass_arg != old_pass_arg)
10102 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
10103 " the same position as the passed-object dummy argument of"
10104 " the overridden procedure", proc->name, &where);
10105 return FAILURE;
10109 return SUCCESS;
10113 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
10115 static gfc_try
10116 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
10117 const char* generic_name, locus where)
10119 gfc_symbol* sym1;
10120 gfc_symbol* sym2;
10122 gcc_assert (t1->specific && t2->specific);
10123 gcc_assert (!t1->specific->is_generic);
10124 gcc_assert (!t2->specific->is_generic);
10126 sym1 = t1->specific->u.specific->n.sym;
10127 sym2 = t2->specific->u.specific->n.sym;
10129 if (sym1 == sym2)
10130 return SUCCESS;
10132 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
10133 if (sym1->attr.subroutine != sym2->attr.subroutine
10134 || sym1->attr.function != sym2->attr.function)
10136 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
10137 " GENERIC '%s' at %L",
10138 sym1->name, sym2->name, generic_name, &where);
10139 return FAILURE;
10142 /* Compare the interfaces. */
10143 if (gfc_compare_interfaces (sym1, sym2, sym2->name, 1, 0, NULL, 0))
10145 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
10146 sym1->name, sym2->name, generic_name, &where);
10147 return FAILURE;
10150 return SUCCESS;
10154 /* Worker function for resolving a generic procedure binding; this is used to
10155 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
10157 The difference between those cases is finding possible inherited bindings
10158 that are overridden, as one has to look for them in tb_sym_root,
10159 tb_uop_root or tb_op, respectively. Thus the caller must already find
10160 the super-type and set p->overridden correctly. */
10162 static gfc_try
10163 resolve_tb_generic_targets (gfc_symbol* super_type,
10164 gfc_typebound_proc* p, const char* name)
10166 gfc_tbp_generic* target;
10167 gfc_symtree* first_target;
10168 gfc_symtree* inherited;
10170 gcc_assert (p && p->is_generic);
10172 /* Try to find the specific bindings for the symtrees in our target-list. */
10173 gcc_assert (p->u.generic);
10174 for (target = p->u.generic; target; target = target->next)
10175 if (!target->specific)
10177 gfc_typebound_proc* overridden_tbp;
10178 gfc_tbp_generic* g;
10179 const char* target_name;
10181 target_name = target->specific_st->name;
10183 /* Defined for this type directly. */
10184 if (target->specific_st->n.tb)
10186 target->specific = target->specific_st->n.tb;
10187 goto specific_found;
10190 /* Look for an inherited specific binding. */
10191 if (super_type)
10193 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
10194 true, NULL);
10196 if (inherited)
10198 gcc_assert (inherited->n.tb);
10199 target->specific = inherited->n.tb;
10200 goto specific_found;
10204 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
10205 " at %L", target_name, name, &p->where);
10206 return FAILURE;
10208 /* Once we've found the specific binding, check it is not ambiguous with
10209 other specifics already found or inherited for the same GENERIC. */
10210 specific_found:
10211 gcc_assert (target->specific);
10213 /* This must really be a specific binding! */
10214 if (target->specific->is_generic)
10216 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
10217 " '%s' is GENERIC, too", name, &p->where, target_name);
10218 return FAILURE;
10221 /* Check those already resolved on this type directly. */
10222 for (g = p->u.generic; g; g = g->next)
10223 if (g != target && g->specific
10224 && check_generic_tbp_ambiguity (target, g, name, p->where)
10225 == FAILURE)
10226 return FAILURE;
10228 /* Check for ambiguity with inherited specific targets. */
10229 for (overridden_tbp = p->overridden; overridden_tbp;
10230 overridden_tbp = overridden_tbp->overridden)
10231 if (overridden_tbp->is_generic)
10233 for (g = overridden_tbp->u.generic; g; g = g->next)
10235 gcc_assert (g->specific);
10236 if (check_generic_tbp_ambiguity (target, g,
10237 name, p->where) == FAILURE)
10238 return FAILURE;
10243 /* If we attempt to "overwrite" a specific binding, this is an error. */
10244 if (p->overridden && !p->overridden->is_generic)
10246 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
10247 " the same name", name, &p->where);
10248 return FAILURE;
10251 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
10252 all must have the same attributes here. */
10253 first_target = p->u.generic->specific->u.specific;
10254 gcc_assert (first_target);
10255 p->subroutine = first_target->n.sym->attr.subroutine;
10256 p->function = first_target->n.sym->attr.function;
10258 return SUCCESS;
10262 /* Resolve a GENERIC procedure binding for a derived type. */
10264 static gfc_try
10265 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
10267 gfc_symbol* super_type;
10269 /* Find the overridden binding if any. */
10270 st->n.tb->overridden = NULL;
10271 super_type = gfc_get_derived_super_type (derived);
10272 if (super_type)
10274 gfc_symtree* overridden;
10275 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
10276 true, NULL);
10278 if (overridden && overridden->n.tb)
10279 st->n.tb->overridden = overridden->n.tb;
10282 /* Resolve using worker function. */
10283 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
10287 /* Retrieve the target-procedure of an operator binding and do some checks in
10288 common for intrinsic and user-defined type-bound operators. */
10290 static gfc_symbol*
10291 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
10293 gfc_symbol* target_proc;
10295 gcc_assert (target->specific && !target->specific->is_generic);
10296 target_proc = target->specific->u.specific->n.sym;
10297 gcc_assert (target_proc);
10299 /* All operator bindings must have a passed-object dummy argument. */
10300 if (target->specific->nopass)
10302 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
10303 return NULL;
10306 return target_proc;
10310 /* Resolve a type-bound intrinsic operator. */
10312 static gfc_try
10313 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
10314 gfc_typebound_proc* p)
10316 gfc_symbol* super_type;
10317 gfc_tbp_generic* target;
10319 /* If there's already an error here, do nothing (but don't fail again). */
10320 if (p->error)
10321 return SUCCESS;
10323 /* Operators should always be GENERIC bindings. */
10324 gcc_assert (p->is_generic);
10326 /* Look for an overridden binding. */
10327 super_type = gfc_get_derived_super_type (derived);
10328 if (super_type && super_type->f2k_derived)
10329 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
10330 op, true, NULL);
10331 else
10332 p->overridden = NULL;
10334 /* Resolve general GENERIC properties using worker function. */
10335 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
10336 goto error;
10338 /* Check the targets to be procedures of correct interface. */
10339 for (target = p->u.generic; target; target = target->next)
10341 gfc_symbol* target_proc;
10343 target_proc = get_checked_tb_operator_target (target, p->where);
10344 if (!target_proc)
10345 goto error;
10347 if (!gfc_check_operator_interface (target_proc, op, p->where))
10348 goto error;
10351 return SUCCESS;
10353 error:
10354 p->error = 1;
10355 return FAILURE;
10359 /* Resolve a type-bound user operator (tree-walker callback). */
10361 static gfc_symbol* resolve_bindings_derived;
10362 static gfc_try resolve_bindings_result;
10364 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
10366 static void
10367 resolve_typebound_user_op (gfc_symtree* stree)
10369 gfc_symbol* super_type;
10370 gfc_tbp_generic* target;
10372 gcc_assert (stree && stree->n.tb);
10374 if (stree->n.tb->error)
10375 return;
10377 /* Operators should always be GENERIC bindings. */
10378 gcc_assert (stree->n.tb->is_generic);
10380 /* Find overridden procedure, if any. */
10381 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10382 if (super_type && super_type->f2k_derived)
10384 gfc_symtree* overridden;
10385 overridden = gfc_find_typebound_user_op (super_type, NULL,
10386 stree->name, true, NULL);
10388 if (overridden && overridden->n.tb)
10389 stree->n.tb->overridden = overridden->n.tb;
10391 else
10392 stree->n.tb->overridden = NULL;
10394 /* Resolve basically using worker function. */
10395 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
10396 == FAILURE)
10397 goto error;
10399 /* Check the targets to be functions of correct interface. */
10400 for (target = stree->n.tb->u.generic; target; target = target->next)
10402 gfc_symbol* target_proc;
10404 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
10405 if (!target_proc)
10406 goto error;
10408 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
10409 goto error;
10412 return;
10414 error:
10415 resolve_bindings_result = FAILURE;
10416 stree->n.tb->error = 1;
10420 /* Resolve the type-bound procedures for a derived type. */
10422 static void
10423 resolve_typebound_procedure (gfc_symtree* stree)
10425 gfc_symbol* proc;
10426 locus where;
10427 gfc_symbol* me_arg;
10428 gfc_symbol* super_type;
10429 gfc_component* comp;
10431 gcc_assert (stree);
10433 /* Undefined specific symbol from GENERIC target definition. */
10434 if (!stree->n.tb)
10435 return;
10437 if (stree->n.tb->error)
10438 return;
10440 /* If this is a GENERIC binding, use that routine. */
10441 if (stree->n.tb->is_generic)
10443 if (resolve_typebound_generic (resolve_bindings_derived, stree)
10444 == FAILURE)
10445 goto error;
10446 return;
10449 /* Get the target-procedure to check it. */
10450 gcc_assert (!stree->n.tb->is_generic);
10451 gcc_assert (stree->n.tb->u.specific);
10452 proc = stree->n.tb->u.specific->n.sym;
10453 where = stree->n.tb->where;
10455 /* Default access should already be resolved from the parser. */
10456 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
10458 /* It should be a module procedure or an external procedure with explicit
10459 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
10460 if ((!proc->attr.subroutine && !proc->attr.function)
10461 || (proc->attr.proc != PROC_MODULE
10462 && proc->attr.if_source != IFSRC_IFBODY)
10463 || (proc->attr.abstract && !stree->n.tb->deferred))
10465 gfc_error ("'%s' must be a module procedure or an external procedure with"
10466 " an explicit interface at %L", proc->name, &where);
10467 goto error;
10469 stree->n.tb->subroutine = proc->attr.subroutine;
10470 stree->n.tb->function = proc->attr.function;
10472 /* Find the super-type of the current derived type. We could do this once and
10473 store in a global if speed is needed, but as long as not I believe this is
10474 more readable and clearer. */
10475 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10477 /* If PASS, resolve and check arguments if not already resolved / loaded
10478 from a .mod file. */
10479 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
10481 if (stree->n.tb->pass_arg)
10483 gfc_formal_arglist* i;
10485 /* If an explicit passing argument name is given, walk the arg-list
10486 and look for it. */
10488 me_arg = NULL;
10489 stree->n.tb->pass_arg_num = 1;
10490 for (i = proc->formal; i; i = i->next)
10492 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
10494 me_arg = i->sym;
10495 break;
10497 ++stree->n.tb->pass_arg_num;
10500 if (!me_arg)
10502 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
10503 " argument '%s'",
10504 proc->name, stree->n.tb->pass_arg, &where,
10505 stree->n.tb->pass_arg);
10506 goto error;
10509 else
10511 /* Otherwise, take the first one; there should in fact be at least
10512 one. */
10513 stree->n.tb->pass_arg_num = 1;
10514 if (!proc->formal)
10516 gfc_error ("Procedure '%s' with PASS at %L must have at"
10517 " least one argument", proc->name, &where);
10518 goto error;
10520 me_arg = proc->formal->sym;
10523 /* Now check that the argument-type matches and the passed-object
10524 dummy argument is generally fine. */
10526 gcc_assert (me_arg);
10528 if (me_arg->ts.type != BT_CLASS)
10530 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10531 " at %L", proc->name, &where);
10532 goto error;
10535 if (CLASS_DATA (me_arg)->ts.u.derived
10536 != resolve_bindings_derived)
10538 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10539 " the derived-type '%s'", me_arg->name, proc->name,
10540 me_arg->name, &where, resolve_bindings_derived->name);
10541 goto error;
10544 gcc_assert (me_arg->ts.type == BT_CLASS);
10545 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank > 0)
10547 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
10548 " scalar", proc->name, &where);
10549 goto error;
10551 if (CLASS_DATA (me_arg)->attr.allocatable)
10553 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10554 " be ALLOCATABLE", proc->name, &where);
10555 goto error;
10557 if (CLASS_DATA (me_arg)->attr.class_pointer)
10559 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10560 " be POINTER", proc->name, &where);
10561 goto error;
10565 /* If we are extending some type, check that we don't override a procedure
10566 flagged NON_OVERRIDABLE. */
10567 stree->n.tb->overridden = NULL;
10568 if (super_type)
10570 gfc_symtree* overridden;
10571 overridden = gfc_find_typebound_proc (super_type, NULL,
10572 stree->name, true, NULL);
10574 if (overridden && overridden->n.tb)
10575 stree->n.tb->overridden = overridden->n.tb;
10577 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
10578 goto error;
10581 /* See if there's a name collision with a component directly in this type. */
10582 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
10583 if (!strcmp (comp->name, stree->name))
10585 gfc_error ("Procedure '%s' at %L has the same name as a component of"
10586 " '%s'",
10587 stree->name, &where, resolve_bindings_derived->name);
10588 goto error;
10591 /* Try to find a name collision with an inherited component. */
10592 if (super_type && gfc_find_component (super_type, stree->name, true, true))
10594 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
10595 " component of '%s'",
10596 stree->name, &where, resolve_bindings_derived->name);
10597 goto error;
10600 stree->n.tb->error = 0;
10601 return;
10603 error:
10604 resolve_bindings_result = FAILURE;
10605 stree->n.tb->error = 1;
10608 static gfc_try
10609 resolve_typebound_procedures (gfc_symbol* derived)
10611 int op;
10613 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
10614 return SUCCESS;
10616 resolve_bindings_derived = derived;
10617 resolve_bindings_result = SUCCESS;
10619 if (derived->f2k_derived->tb_sym_root)
10620 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
10621 &resolve_typebound_procedure);
10623 if (derived->f2k_derived->tb_uop_root)
10624 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
10625 &resolve_typebound_user_op);
10627 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
10629 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
10630 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
10631 p) == FAILURE)
10632 resolve_bindings_result = FAILURE;
10635 return resolve_bindings_result;
10639 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
10640 to give all identical derived types the same backend_decl. */
10641 static void
10642 add_dt_to_dt_list (gfc_symbol *derived)
10644 gfc_dt_list *dt_list;
10646 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
10647 if (derived == dt_list->derived)
10648 break;
10650 if (dt_list == NULL)
10652 dt_list = gfc_get_dt_list ();
10653 dt_list->next = gfc_derived_types;
10654 dt_list->derived = derived;
10655 gfc_derived_types = dt_list;
10660 /* Ensure that a derived-type is really not abstract, meaning that every
10661 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
10663 static gfc_try
10664 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
10666 if (!st)
10667 return SUCCESS;
10669 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
10670 return FAILURE;
10671 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
10672 return FAILURE;
10674 if (st->n.tb && st->n.tb->deferred)
10676 gfc_symtree* overriding;
10677 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
10678 if (!overriding)
10679 return FAILURE;
10680 gcc_assert (overriding->n.tb);
10681 if (overriding->n.tb->deferred)
10683 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
10684 " '%s' is DEFERRED and not overridden",
10685 sub->name, &sub->declared_at, st->name);
10686 return FAILURE;
10690 return SUCCESS;
10693 static gfc_try
10694 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
10696 /* The algorithm used here is to recursively travel up the ancestry of sub
10697 and for each ancestor-type, check all bindings. If any of them is
10698 DEFERRED, look it up starting from sub and see if the found (overriding)
10699 binding is not DEFERRED.
10700 This is not the most efficient way to do this, but it should be ok and is
10701 clearer than something sophisticated. */
10703 gcc_assert (ancestor && !sub->attr.abstract);
10705 if (!ancestor->attr.abstract)
10706 return SUCCESS;
10708 /* Walk bindings of this ancestor. */
10709 if (ancestor->f2k_derived)
10711 gfc_try t;
10712 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
10713 if (t == FAILURE)
10714 return FAILURE;
10717 /* Find next ancestor type and recurse on it. */
10718 ancestor = gfc_get_derived_super_type (ancestor);
10719 if (ancestor)
10720 return ensure_not_abstract (sub, ancestor);
10722 return SUCCESS;
10726 static void resolve_symbol (gfc_symbol *sym);
10729 /* Resolve the components of a derived type. */
10731 static gfc_try
10732 resolve_fl_derived (gfc_symbol *sym)
10734 gfc_symbol* super_type;
10735 gfc_component *c;
10736 int i;
10738 super_type = gfc_get_derived_super_type (sym);
10740 if (sym->attr.is_class && sym->ts.u.derived == NULL)
10742 /* Fix up incomplete CLASS symbols. */
10743 gfc_component *data = gfc_find_component (sym, "$data", true, true);
10744 gfc_component *vptr = gfc_find_component (sym, "$vptr", true, true);
10745 if (vptr->ts.u.derived == NULL)
10747 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
10748 gcc_assert (vtab);
10749 vptr->ts.u.derived = vtab->ts.u.derived;
10753 /* F2008, C432. */
10754 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
10756 gfc_error ("As extending type '%s' at %L has a coarray component, "
10757 "parent type '%s' shall also have one", sym->name,
10758 &sym->declared_at, super_type->name);
10759 return FAILURE;
10762 /* Ensure the extended type gets resolved before we do. */
10763 if (super_type && resolve_fl_derived (super_type) == FAILURE)
10764 return FAILURE;
10766 /* An ABSTRACT type must be extensible. */
10767 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
10769 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
10770 sym->name, &sym->declared_at);
10771 return FAILURE;
10774 for (c = sym->components; c != NULL; c = c->next)
10776 /* F2008, C442. */
10777 if (c->attr.codimension /* FIXME: c->as check due to PR 43412. */
10778 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
10780 gfc_error ("Coarray component '%s' at %L must be allocatable with "
10781 "deferred shape", c->name, &c->loc);
10782 return FAILURE;
10785 /* F2008, C443. */
10786 if (c->attr.codimension && c->ts.type == BT_DERIVED
10787 && c->ts.u.derived->ts.is_iso_c)
10789 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
10790 "shall not be a coarray", c->name, &c->loc);
10791 return FAILURE;
10794 /* F2008, C444. */
10795 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
10796 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
10797 || c->attr.allocatable))
10799 gfc_error ("Component '%s' at %L with coarray component "
10800 "shall be a nonpointer, nonallocatable scalar",
10801 c->name, &c->loc);
10802 return FAILURE;
10805 /* F2008, C448. */
10806 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
10808 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
10809 "is not an array pointer", c->name, &c->loc);
10810 return FAILURE;
10813 if (c->attr.proc_pointer && c->ts.interface)
10815 if (c->ts.interface->attr.procedure && !sym->attr.vtype)
10816 gfc_error ("Interface '%s', used by procedure pointer component "
10817 "'%s' at %L, is declared in a later PROCEDURE statement",
10818 c->ts.interface->name, c->name, &c->loc);
10820 /* Get the attributes from the interface (now resolved). */
10821 if (c->ts.interface->attr.if_source
10822 || c->ts.interface->attr.intrinsic)
10824 gfc_symbol *ifc = c->ts.interface;
10826 if (ifc->formal && !ifc->formal_ns)
10827 resolve_symbol (ifc);
10829 if (ifc->attr.intrinsic)
10830 resolve_intrinsic (ifc, &ifc->declared_at);
10832 if (ifc->result)
10834 c->ts = ifc->result->ts;
10835 c->attr.allocatable = ifc->result->attr.allocatable;
10836 c->attr.pointer = ifc->result->attr.pointer;
10837 c->attr.dimension = ifc->result->attr.dimension;
10838 c->as = gfc_copy_array_spec (ifc->result->as);
10840 else
10842 c->ts = ifc->ts;
10843 c->attr.allocatable = ifc->attr.allocatable;
10844 c->attr.pointer = ifc->attr.pointer;
10845 c->attr.dimension = ifc->attr.dimension;
10846 c->as = gfc_copy_array_spec (ifc->as);
10848 c->ts.interface = ifc;
10849 c->attr.function = ifc->attr.function;
10850 c->attr.subroutine = ifc->attr.subroutine;
10851 gfc_copy_formal_args_ppc (c, ifc);
10853 c->attr.pure = ifc->attr.pure;
10854 c->attr.elemental = ifc->attr.elemental;
10855 c->attr.recursive = ifc->attr.recursive;
10856 c->attr.always_explicit = ifc->attr.always_explicit;
10857 c->attr.ext_attr |= ifc->attr.ext_attr;
10858 /* Replace symbols in array spec. */
10859 if (c->as)
10861 int i;
10862 for (i = 0; i < c->as->rank; i++)
10864 gfc_expr_replace_comp (c->as->lower[i], c);
10865 gfc_expr_replace_comp (c->as->upper[i], c);
10868 /* Copy char length. */
10869 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
10871 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
10872 gfc_expr_replace_comp (cl->length, c);
10873 if (cl->length && !cl->resolved
10874 && gfc_resolve_expr (cl->length) == FAILURE)
10875 return FAILURE;
10876 c->ts.u.cl = cl;
10879 else if (!sym->attr.vtype && c->ts.interface->name[0] != '\0')
10881 gfc_error ("Interface '%s' of procedure pointer component "
10882 "'%s' at %L must be explicit", c->ts.interface->name,
10883 c->name, &c->loc);
10884 return FAILURE;
10887 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
10889 /* Since PPCs are not implicitly typed, a PPC without an explicit
10890 interface must be a subroutine. */
10891 gfc_add_subroutine (&c->attr, c->name, &c->loc);
10894 /* Procedure pointer components: Check PASS arg. */
10895 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
10896 && !sym->attr.vtype)
10898 gfc_symbol* me_arg;
10900 if (c->tb->pass_arg)
10902 gfc_formal_arglist* i;
10904 /* If an explicit passing argument name is given, walk the arg-list
10905 and look for it. */
10907 me_arg = NULL;
10908 c->tb->pass_arg_num = 1;
10909 for (i = c->formal; i; i = i->next)
10911 if (!strcmp (i->sym->name, c->tb->pass_arg))
10913 me_arg = i->sym;
10914 break;
10916 c->tb->pass_arg_num++;
10919 if (!me_arg)
10921 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
10922 "at %L has no argument '%s'", c->name,
10923 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
10924 c->tb->error = 1;
10925 return FAILURE;
10928 else
10930 /* Otherwise, take the first one; there should in fact be at least
10931 one. */
10932 c->tb->pass_arg_num = 1;
10933 if (!c->formal)
10935 gfc_error ("Procedure pointer component '%s' with PASS at %L "
10936 "must have at least one argument",
10937 c->name, &c->loc);
10938 c->tb->error = 1;
10939 return FAILURE;
10941 me_arg = c->formal->sym;
10944 /* Now check that the argument-type matches. */
10945 gcc_assert (me_arg);
10946 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
10947 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
10948 || (me_arg->ts.type == BT_CLASS
10949 && CLASS_DATA (me_arg)->ts.u.derived != sym))
10951 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10952 " the derived type '%s'", me_arg->name, c->name,
10953 me_arg->name, &c->loc, sym->name);
10954 c->tb->error = 1;
10955 return FAILURE;
10958 /* Check for C453. */
10959 if (me_arg->attr.dimension)
10961 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10962 "must be scalar", me_arg->name, c->name, me_arg->name,
10963 &c->loc);
10964 c->tb->error = 1;
10965 return FAILURE;
10968 if (me_arg->attr.pointer)
10970 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10971 "may not have the POINTER attribute", me_arg->name,
10972 c->name, me_arg->name, &c->loc);
10973 c->tb->error = 1;
10974 return FAILURE;
10977 if (me_arg->attr.allocatable)
10979 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10980 "may not be ALLOCATABLE", me_arg->name, c->name,
10981 me_arg->name, &c->loc);
10982 c->tb->error = 1;
10983 return FAILURE;
10986 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
10987 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10988 " at %L", c->name, &c->loc);
10992 /* Check type-spec if this is not the parent-type component. */
10993 if ((!sym->attr.extension || c != sym->components)
10994 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
10995 return FAILURE;
10997 /* If this type is an extension, set the accessibility of the parent
10998 component. */
10999 if (super_type && c == sym->components
11000 && strcmp (super_type->name, c->name) == 0)
11001 c->attr.access = super_type->attr.access;
11003 /* If this type is an extension, see if this component has the same name
11004 as an inherited type-bound procedure. */
11005 if (super_type && !sym->attr.is_class
11006 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
11008 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
11009 " inherited type-bound procedure",
11010 c->name, sym->name, &c->loc);
11011 return FAILURE;
11014 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
11016 if (c->ts.u.cl->length == NULL
11017 || (resolve_charlen (c->ts.u.cl) == FAILURE)
11018 || !gfc_is_constant_expr (c->ts.u.cl->length))
11020 gfc_error ("Character length of component '%s' needs to "
11021 "be a constant specification expression at %L",
11022 c->name,
11023 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
11024 return FAILURE;
11028 if (c->ts.type == BT_DERIVED
11029 && sym->component_access != ACCESS_PRIVATE
11030 && gfc_check_access (sym->attr.access, sym->ns->default_access)
11031 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
11032 && !c->ts.u.derived->attr.use_assoc
11033 && !gfc_check_access (c->ts.u.derived->attr.access,
11034 c->ts.u.derived->ns->default_access)
11035 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
11036 "is a PRIVATE type and cannot be a component of "
11037 "'%s', which is PUBLIC at %L", c->name,
11038 sym->name, &sym->declared_at) == FAILURE)
11039 return FAILURE;
11041 if (sym->attr.sequence)
11043 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
11045 gfc_error ("Component %s of SEQUENCE type declared at %L does "
11046 "not have the SEQUENCE attribute",
11047 c->ts.u.derived->name, &sym->declared_at);
11048 return FAILURE;
11052 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && c->attr.pointer
11053 && c->ts.u.derived->components == NULL
11054 && !c->ts.u.derived->attr.zero_comp)
11056 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11057 "that has not been declared", c->name, sym->name,
11058 &c->loc);
11059 return FAILURE;
11062 if (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.class_pointer
11063 && CLASS_DATA (c)->ts.u.derived->components == NULL
11064 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp)
11066 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11067 "that has not been declared", c->name, sym->name,
11068 &c->loc);
11069 return FAILURE;
11072 /* C437. */
11073 if (c->ts.type == BT_CLASS
11074 && !(CLASS_DATA (c)->attr.class_pointer
11075 || CLASS_DATA (c)->attr.allocatable))
11077 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
11078 "or pointer", c->name, &c->loc);
11079 return FAILURE;
11082 /* Ensure that all the derived type components are put on the
11083 derived type list; even in formal namespaces, where derived type
11084 pointer components might not have been declared. */
11085 if (c->ts.type == BT_DERIVED
11086 && c->ts.u.derived
11087 && c->ts.u.derived->components
11088 && c->attr.pointer
11089 && sym != c->ts.u.derived)
11090 add_dt_to_dt_list (c->ts.u.derived);
11092 if (c->attr.pointer || c->attr.proc_pointer || c->attr.allocatable
11093 || c->as == NULL)
11094 continue;
11096 for (i = 0; i < c->as->rank; i++)
11098 if (c->as->lower[i] == NULL
11099 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
11100 || !gfc_is_constant_expr (c->as->lower[i])
11101 || c->as->upper[i] == NULL
11102 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
11103 || !gfc_is_constant_expr (c->as->upper[i]))
11105 gfc_error ("Component '%s' of '%s' at %L must have "
11106 "constant array bounds",
11107 c->name, sym->name, &c->loc);
11108 return FAILURE;
11113 /* Resolve the type-bound procedures. */
11114 if (resolve_typebound_procedures (sym) == FAILURE)
11115 return FAILURE;
11117 /* Resolve the finalizer procedures. */
11118 if (gfc_resolve_finalizers (sym) == FAILURE)
11119 return FAILURE;
11121 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
11122 all DEFERRED bindings are overridden. */
11123 if (super_type && super_type->attr.abstract && !sym->attr.abstract
11124 && !sym->attr.is_class
11125 && ensure_not_abstract (sym, super_type) == FAILURE)
11126 return FAILURE;
11128 /* Add derived type to the derived type list. */
11129 add_dt_to_dt_list (sym);
11131 return SUCCESS;
11135 static gfc_try
11136 resolve_fl_namelist (gfc_symbol *sym)
11138 gfc_namelist *nl;
11139 gfc_symbol *nlsym;
11141 /* Reject PRIVATE objects in a PUBLIC namelist. */
11142 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
11144 for (nl = sym->namelist; nl; nl = nl->next)
11146 if (!nl->sym->attr.use_assoc
11147 && !is_sym_host_assoc (nl->sym, sym->ns)
11148 && !gfc_check_access(nl->sym->attr.access,
11149 nl->sym->ns->default_access))
11151 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
11152 "cannot be member of PUBLIC namelist '%s' at %L",
11153 nl->sym->name, sym->name, &sym->declared_at);
11154 return FAILURE;
11157 /* Types with private components that came here by USE-association. */
11158 if (nl->sym->ts.type == BT_DERIVED
11159 && derived_inaccessible (nl->sym->ts.u.derived))
11161 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
11162 "components and cannot be member of namelist '%s' at %L",
11163 nl->sym->name, sym->name, &sym->declared_at);
11164 return FAILURE;
11167 /* Types with private components that are defined in the same module. */
11168 if (nl->sym->ts.type == BT_DERIVED
11169 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
11170 && !gfc_check_access (nl->sym->ts.u.derived->attr.private_comp
11171 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
11172 nl->sym->ns->default_access))
11174 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
11175 "cannot be a member of PUBLIC namelist '%s' at %L",
11176 nl->sym->name, sym->name, &sym->declared_at);
11177 return FAILURE;
11182 for (nl = sym->namelist; nl; nl = nl->next)
11184 /* Reject namelist arrays of assumed shape. */
11185 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
11186 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
11187 "must not have assumed shape in namelist "
11188 "'%s' at %L", nl->sym->name, sym->name,
11189 &sym->declared_at) == FAILURE)
11190 return FAILURE;
11192 /* Reject namelist arrays that are not constant shape. */
11193 if (is_non_constant_shape_array (nl->sym))
11195 gfc_error ("NAMELIST array object '%s' must have constant "
11196 "shape in namelist '%s' at %L", nl->sym->name,
11197 sym->name, &sym->declared_at);
11198 return FAILURE;
11201 /* Namelist objects cannot have allocatable or pointer components. */
11202 if (nl->sym->ts.type != BT_DERIVED)
11203 continue;
11205 if (nl->sym->ts.u.derived->attr.alloc_comp)
11207 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
11208 "have ALLOCATABLE components",
11209 nl->sym->name, sym->name, &sym->declared_at);
11210 return FAILURE;
11213 if (nl->sym->ts.u.derived->attr.pointer_comp)
11215 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
11216 "have POINTER components",
11217 nl->sym->name, sym->name, &sym->declared_at);
11218 return FAILURE;
11223 /* 14.1.2 A module or internal procedure represent local entities
11224 of the same type as a namelist member and so are not allowed. */
11225 for (nl = sym->namelist; nl; nl = nl->next)
11227 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
11228 continue;
11230 if (nl->sym->attr.function && nl->sym == nl->sym->result)
11231 if ((nl->sym == sym->ns->proc_name)
11233 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
11234 continue;
11236 nlsym = NULL;
11237 if (nl->sym && nl->sym->name)
11238 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
11239 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
11241 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
11242 "attribute in '%s' at %L", nlsym->name,
11243 &sym->declared_at);
11244 return FAILURE;
11248 return SUCCESS;
11252 static gfc_try
11253 resolve_fl_parameter (gfc_symbol *sym)
11255 /* A parameter array's shape needs to be constant. */
11256 if (sym->as != NULL
11257 && (sym->as->type == AS_DEFERRED
11258 || is_non_constant_shape_array (sym)))
11260 gfc_error ("Parameter array '%s' at %L cannot be automatic "
11261 "or of deferred shape", sym->name, &sym->declared_at);
11262 return FAILURE;
11265 /* Make sure a parameter that has been implicitly typed still
11266 matches the implicit type, since PARAMETER statements can precede
11267 IMPLICIT statements. */
11268 if (sym->attr.implicit_type
11269 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
11270 sym->ns)))
11272 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
11273 "later IMPLICIT type", sym->name, &sym->declared_at);
11274 return FAILURE;
11277 /* Make sure the types of derived parameters are consistent. This
11278 type checking is deferred until resolution because the type may
11279 refer to a derived type from the host. */
11280 if (sym->ts.type == BT_DERIVED
11281 && !gfc_compare_types (&sym->ts, &sym->value->ts))
11283 gfc_error ("Incompatible derived type in PARAMETER at %L",
11284 &sym->value->where);
11285 return FAILURE;
11287 return SUCCESS;
11291 /* Do anything necessary to resolve a symbol. Right now, we just
11292 assume that an otherwise unknown symbol is a variable. This sort
11293 of thing commonly happens for symbols in module. */
11295 static void
11296 resolve_symbol (gfc_symbol *sym)
11298 int check_constant, mp_flag;
11299 gfc_symtree *symtree;
11300 gfc_symtree *this_symtree;
11301 gfc_namespace *ns;
11302 gfc_component *c;
11304 /* Avoid double resolution of function result symbols. */
11305 if ((sym->result || sym->attr.result) && (sym->ns != gfc_current_ns))
11306 return;
11308 if (sym->attr.flavor == FL_UNKNOWN)
11311 /* If we find that a flavorless symbol is an interface in one of the
11312 parent namespaces, find its symtree in this namespace, free the
11313 symbol and set the symtree to point to the interface symbol. */
11314 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
11316 symtree = gfc_find_symtree (ns->sym_root, sym->name);
11317 if (symtree && symtree->n.sym->generic)
11319 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
11320 sym->name);
11321 sym->refs--;
11322 if (!sym->refs)
11323 gfc_free_symbol (sym);
11324 symtree->n.sym->refs++;
11325 this_symtree->n.sym = symtree->n.sym;
11326 return;
11330 /* Otherwise give it a flavor according to such attributes as
11331 it has. */
11332 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
11333 sym->attr.flavor = FL_VARIABLE;
11334 else
11336 sym->attr.flavor = FL_PROCEDURE;
11337 if (sym->attr.dimension)
11338 sym->attr.function = 1;
11342 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
11343 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
11345 if (sym->attr.procedure && sym->ts.interface
11346 && sym->attr.if_source != IFSRC_DECL)
11348 if (sym->ts.interface == sym)
11350 gfc_error ("PROCEDURE '%s' at %L may not be used as its own "
11351 "interface", sym->name, &sym->declared_at);
11352 return;
11354 if (sym->ts.interface->attr.procedure)
11356 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared"
11357 " in a later PROCEDURE statement", sym->ts.interface->name,
11358 sym->name,&sym->declared_at);
11359 return;
11362 /* Get the attributes from the interface (now resolved). */
11363 if (sym->ts.interface->attr.if_source
11364 || sym->ts.interface->attr.intrinsic)
11366 gfc_symbol *ifc = sym->ts.interface;
11367 resolve_symbol (ifc);
11369 if (ifc->attr.intrinsic)
11370 resolve_intrinsic (ifc, &ifc->declared_at);
11372 if (ifc->result)
11373 sym->ts = ifc->result->ts;
11374 else
11375 sym->ts = ifc->ts;
11376 sym->ts.interface = ifc;
11377 sym->attr.function = ifc->attr.function;
11378 sym->attr.subroutine = ifc->attr.subroutine;
11379 gfc_copy_formal_args (sym, ifc);
11381 sym->attr.allocatable = ifc->attr.allocatable;
11382 sym->attr.pointer = ifc->attr.pointer;
11383 sym->attr.pure = ifc->attr.pure;
11384 sym->attr.elemental = ifc->attr.elemental;
11385 sym->attr.dimension = ifc->attr.dimension;
11386 sym->attr.contiguous = ifc->attr.contiguous;
11387 sym->attr.recursive = ifc->attr.recursive;
11388 sym->attr.always_explicit = ifc->attr.always_explicit;
11389 sym->attr.ext_attr |= ifc->attr.ext_attr;
11390 /* Copy array spec. */
11391 sym->as = gfc_copy_array_spec (ifc->as);
11392 if (sym->as)
11394 int i;
11395 for (i = 0; i < sym->as->rank; i++)
11397 gfc_expr_replace_symbols (sym->as->lower[i], sym);
11398 gfc_expr_replace_symbols (sym->as->upper[i], sym);
11401 /* Copy char length. */
11402 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
11404 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
11405 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
11406 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
11407 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
11408 return;
11411 else if (sym->ts.interface->name[0] != '\0')
11413 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
11414 sym->ts.interface->name, sym->name, &sym->declared_at);
11415 return;
11419 if (sym->attr.is_protected && !sym->attr.proc_pointer
11420 && (sym->attr.procedure || sym->attr.external))
11422 if (sym->attr.external)
11423 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
11424 "at %L", &sym->declared_at);
11425 else
11426 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
11427 "at %L", &sym->declared_at);
11429 return;
11433 /* F2008, C530. */
11434 if (sym->attr.contiguous
11435 && (!sym->attr.dimension || (sym->as->type != AS_ASSUMED_SHAPE
11436 && !sym->attr.pointer)))
11438 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
11439 "array pointer or an assumed-shape array", sym->name,
11440 &sym->declared_at);
11441 return;
11444 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
11445 return;
11447 /* Symbols that are module procedures with results (functions) have
11448 the types and array specification copied for type checking in
11449 procedures that call them, as well as for saving to a module
11450 file. These symbols can't stand the scrutiny that their results
11451 can. */
11452 mp_flag = (sym->result != NULL && sym->result != sym);
11454 /* Make sure that the intrinsic is consistent with its internal
11455 representation. This needs to be done before assigning a default
11456 type to avoid spurious warnings. */
11457 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
11458 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
11459 return;
11461 /* For associate names, resolve corresponding expression and make sure
11462 they get their type-spec set this way. */
11463 if (sym->assoc)
11465 gcc_assert (sym->attr.flavor == FL_VARIABLE);
11466 if (gfc_resolve_expr (sym->assoc->target) != SUCCESS)
11467 return;
11469 sym->ts = sym->assoc->target->ts;
11470 gcc_assert (sym->ts.type != BT_UNKNOWN);
11473 /* Assign default type to symbols that need one and don't have one. */
11474 if (sym->ts.type == BT_UNKNOWN)
11476 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
11477 gfc_set_default_type (sym, 1, NULL);
11479 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
11480 && !sym->attr.function && !sym->attr.subroutine
11481 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
11482 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
11484 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
11486 /* The specific case of an external procedure should emit an error
11487 in the case that there is no implicit type. */
11488 if (!mp_flag)
11489 gfc_set_default_type (sym, sym->attr.external, NULL);
11490 else
11492 /* Result may be in another namespace. */
11493 resolve_symbol (sym->result);
11495 if (!sym->result->attr.proc_pointer)
11497 sym->ts = sym->result->ts;
11498 sym->as = gfc_copy_array_spec (sym->result->as);
11499 sym->attr.dimension = sym->result->attr.dimension;
11500 sym->attr.pointer = sym->result->attr.pointer;
11501 sym->attr.allocatable = sym->result->attr.allocatable;
11502 sym->attr.contiguous = sym->result->attr.contiguous;
11508 /* Assumed size arrays and assumed shape arrays must be dummy
11509 arguments. */
11511 if (sym->as != NULL
11512 && ((sym->as->type == AS_ASSUMED_SIZE && !sym->as->cp_was_assumed)
11513 || sym->as->type == AS_ASSUMED_SHAPE)
11514 && sym->attr.dummy == 0)
11516 if (sym->as->type == AS_ASSUMED_SIZE)
11517 gfc_error ("Assumed size array at %L must be a dummy argument",
11518 &sym->declared_at);
11519 else
11520 gfc_error ("Assumed shape array at %L must be a dummy argument",
11521 &sym->declared_at);
11522 return;
11525 /* Make sure symbols with known intent or optional are really dummy
11526 variable. Because of ENTRY statement, this has to be deferred
11527 until resolution time. */
11529 if (!sym->attr.dummy
11530 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
11532 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
11533 return;
11536 if (sym->attr.value && !sym->attr.dummy)
11538 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
11539 "it is not a dummy argument", sym->name, &sym->declared_at);
11540 return;
11543 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
11545 gfc_charlen *cl = sym->ts.u.cl;
11546 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11548 gfc_error ("Character dummy variable '%s' at %L with VALUE "
11549 "attribute must have constant length",
11550 sym->name, &sym->declared_at);
11551 return;
11554 if (sym->ts.is_c_interop
11555 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
11557 gfc_error ("C interoperable character dummy variable '%s' at %L "
11558 "with VALUE attribute must have length one",
11559 sym->name, &sym->declared_at);
11560 return;
11564 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
11565 do this for something that was implicitly typed because that is handled
11566 in gfc_set_default_type. Handle dummy arguments and procedure
11567 definitions separately. Also, anything that is use associated is not
11568 handled here but instead is handled in the module it is declared in.
11569 Finally, derived type definitions are allowed to be BIND(C) since that
11570 only implies that they're interoperable, and they are checked fully for
11571 interoperability when a variable is declared of that type. */
11572 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
11573 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
11574 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
11576 gfc_try t = SUCCESS;
11578 /* First, make sure the variable is declared at the
11579 module-level scope (J3/04-007, Section 15.3). */
11580 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
11581 sym->attr.in_common == 0)
11583 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
11584 "is neither a COMMON block nor declared at the "
11585 "module level scope", sym->name, &(sym->declared_at));
11586 t = FAILURE;
11588 else if (sym->common_head != NULL)
11590 t = verify_com_block_vars_c_interop (sym->common_head);
11592 else
11594 /* If type() declaration, we need to verify that the components
11595 of the given type are all C interoperable, etc. */
11596 if (sym->ts.type == BT_DERIVED &&
11597 sym->ts.u.derived->attr.is_c_interop != 1)
11599 /* Make sure the user marked the derived type as BIND(C). If
11600 not, call the verify routine. This could print an error
11601 for the derived type more than once if multiple variables
11602 of that type are declared. */
11603 if (sym->ts.u.derived->attr.is_bind_c != 1)
11604 verify_bind_c_derived_type (sym->ts.u.derived);
11605 t = FAILURE;
11608 /* Verify the variable itself as C interoperable if it
11609 is BIND(C). It is not possible for this to succeed if
11610 the verify_bind_c_derived_type failed, so don't have to handle
11611 any error returned by verify_bind_c_derived_type. */
11612 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11613 sym->common_block);
11616 if (t == FAILURE)
11618 /* clear the is_bind_c flag to prevent reporting errors more than
11619 once if something failed. */
11620 sym->attr.is_bind_c = 0;
11621 return;
11625 /* If a derived type symbol has reached this point, without its
11626 type being declared, we have an error. Notice that most
11627 conditions that produce undefined derived types have already
11628 been dealt with. However, the likes of:
11629 implicit type(t) (t) ..... call foo (t) will get us here if
11630 the type is not declared in the scope of the implicit
11631 statement. Change the type to BT_UNKNOWN, both because it is so
11632 and to prevent an ICE. */
11633 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
11634 && !sym->ts.u.derived->attr.zero_comp)
11636 gfc_error ("The derived type '%s' at %L is of type '%s', "
11637 "which has not been defined", sym->name,
11638 &sym->declared_at, sym->ts.u.derived->name);
11639 sym->ts.type = BT_UNKNOWN;
11640 return;
11643 /* Make sure that the derived type has been resolved and that the
11644 derived type is visible in the symbol's namespace, if it is a
11645 module function and is not PRIVATE. */
11646 if (sym->ts.type == BT_DERIVED
11647 && sym->ts.u.derived->attr.use_assoc
11648 && sym->ns->proc_name
11649 && sym->ns->proc_name->attr.flavor == FL_MODULE)
11651 gfc_symbol *ds;
11653 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
11654 return;
11656 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
11657 if (!ds && sym->attr.function
11658 && gfc_check_access (sym->attr.access, sym->ns->default_access))
11660 symtree = gfc_new_symtree (&sym->ns->sym_root,
11661 sym->ts.u.derived->name);
11662 symtree->n.sym = sym->ts.u.derived;
11663 sym->ts.u.derived->refs++;
11667 /* Unless the derived-type declaration is use associated, Fortran 95
11668 does not allow public entries of private derived types.
11669 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
11670 161 in 95-006r3. */
11671 if (sym->ts.type == BT_DERIVED
11672 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
11673 && !sym->ts.u.derived->attr.use_assoc
11674 && gfc_check_access (sym->attr.access, sym->ns->default_access)
11675 && !gfc_check_access (sym->ts.u.derived->attr.access,
11676 sym->ts.u.derived->ns->default_access)
11677 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
11678 "of PRIVATE derived type '%s'",
11679 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
11680 : "variable", sym->name, &sym->declared_at,
11681 sym->ts.u.derived->name) == FAILURE)
11682 return;
11684 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
11685 default initialization is defined (5.1.2.4.4). */
11686 if (sym->ts.type == BT_DERIVED
11687 && sym->attr.dummy
11688 && sym->attr.intent == INTENT_OUT
11689 && sym->as
11690 && sym->as->type == AS_ASSUMED_SIZE)
11692 for (c = sym->ts.u.derived->components; c; c = c->next)
11694 if (c->initializer)
11696 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
11697 "ASSUMED SIZE and so cannot have a default initializer",
11698 sym->name, &sym->declared_at);
11699 return;
11704 /* F2008, C526. */
11705 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
11706 || sym->attr.codimension)
11707 && sym->attr.result)
11708 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
11709 "a coarray component", sym->name, &sym->declared_at);
11711 /* F2008, C524. */
11712 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
11713 && sym->ts.u.derived->ts.is_iso_c)
11714 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
11715 "shall not be a coarray", sym->name, &sym->declared_at);
11717 /* F2008, C525. */
11718 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp
11719 && (sym->attr.codimension || sym->attr.pointer || sym->attr.dimension
11720 || sym->attr.allocatable))
11721 gfc_error ("Variable '%s' at %L with coarray component "
11722 "shall be a nonpointer, nonallocatable scalar",
11723 sym->name, &sym->declared_at);
11725 /* F2008, C526. The function-result case was handled above. */
11726 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
11727 || sym->attr.codimension)
11728 && !(sym->attr.allocatable || sym->attr.dummy || sym->attr.save
11729 || sym->ns->proc_name->attr.flavor == FL_MODULE
11730 || sym->ns->proc_name->attr.is_main_program
11731 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
11732 gfc_error ("Variable '%s' at %L is a coarray or has a coarray "
11733 "component and is not ALLOCATABLE, SAVE nor a "
11734 "dummy argument", sym->name, &sym->declared_at);
11735 /* F2008, C528. */ /* FIXME: sym->as check due to PR 43412. */
11736 else if (sym->attr.codimension && !sym->attr.allocatable
11737 && sym->as && sym->as->cotype == AS_DEFERRED)
11738 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
11739 "deferred shape", sym->name, &sym->declared_at);
11740 else if (sym->attr.codimension && sym->attr.allocatable
11741 && (sym->as->type != AS_DEFERRED || sym->as->cotype != AS_DEFERRED))
11742 gfc_error ("Allocatable coarray variable '%s' at %L must have "
11743 "deferred shape", sym->name, &sym->declared_at);
11746 /* F2008, C541. */
11747 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
11748 || (sym->attr.codimension && sym->attr.allocatable))
11749 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
11750 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
11751 "allocatable coarray or have coarray components",
11752 sym->name, &sym->declared_at);
11754 if (sym->attr.codimension && sym->attr.dummy
11755 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
11756 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
11757 "procedure '%s'", sym->name, &sym->declared_at,
11758 sym->ns->proc_name->name);
11760 switch (sym->attr.flavor)
11762 case FL_VARIABLE:
11763 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
11764 return;
11765 break;
11767 case FL_PROCEDURE:
11768 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
11769 return;
11770 break;
11772 case FL_NAMELIST:
11773 if (resolve_fl_namelist (sym) == FAILURE)
11774 return;
11775 break;
11777 case FL_PARAMETER:
11778 if (resolve_fl_parameter (sym) == FAILURE)
11779 return;
11780 break;
11782 default:
11783 break;
11786 /* Resolve array specifier. Check as well some constraints
11787 on COMMON blocks. */
11789 check_constant = sym->attr.in_common && !sym->attr.pointer;
11791 /* Set the formal_arg_flag so that check_conflict will not throw
11792 an error for host associated variables in the specification
11793 expression for an array_valued function. */
11794 if (sym->attr.function && sym->as)
11795 formal_arg_flag = 1;
11797 gfc_resolve_array_spec (sym->as, check_constant);
11799 formal_arg_flag = 0;
11801 /* Resolve formal namespaces. */
11802 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
11803 && !sym->attr.contained && !sym->attr.intrinsic)
11804 gfc_resolve (sym->formal_ns);
11806 /* Make sure the formal namespace is present. */
11807 if (sym->formal && !sym->formal_ns)
11809 gfc_formal_arglist *formal = sym->formal;
11810 while (formal && !formal->sym)
11811 formal = formal->next;
11813 if (formal)
11815 sym->formal_ns = formal->sym->ns;
11816 sym->formal_ns->refs++;
11820 /* Check threadprivate restrictions. */
11821 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
11822 && (!sym->attr.in_common
11823 && sym->module == NULL
11824 && (sym->ns->proc_name == NULL
11825 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
11826 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
11828 /* If we have come this far we can apply default-initializers, as
11829 described in 14.7.5, to those variables that have not already
11830 been assigned one. */
11831 if (sym->ts.type == BT_DERIVED
11832 && sym->attr.referenced
11833 && sym->ns == gfc_current_ns
11834 && !sym->value
11835 && !sym->attr.allocatable
11836 && !sym->attr.alloc_comp)
11838 symbol_attribute *a = &sym->attr;
11840 if ((!a->save && !a->dummy && !a->pointer
11841 && !a->in_common && !a->use_assoc
11842 && !(a->function && sym != sym->result))
11843 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
11844 apply_default_init (sym);
11847 /* If this symbol has a type-spec, check it. */
11848 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
11849 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
11850 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
11851 == FAILURE)
11852 return;
11856 /************* Resolve DATA statements *************/
11858 static struct
11860 gfc_data_value *vnode;
11861 mpz_t left;
11863 values;
11866 /* Advance the values structure to point to the next value in the data list. */
11868 static gfc_try
11869 next_data_value (void)
11871 while (mpz_cmp_ui (values.left, 0) == 0)
11874 if (values.vnode->next == NULL)
11875 return FAILURE;
11877 values.vnode = values.vnode->next;
11878 mpz_set (values.left, values.vnode->repeat);
11881 return SUCCESS;
11885 static gfc_try
11886 check_data_variable (gfc_data_variable *var, locus *where)
11888 gfc_expr *e;
11889 mpz_t size;
11890 mpz_t offset;
11891 gfc_try t;
11892 ar_type mark = AR_UNKNOWN;
11893 int i;
11894 mpz_t section_index[GFC_MAX_DIMENSIONS];
11895 gfc_ref *ref;
11896 gfc_array_ref *ar;
11897 gfc_symbol *sym;
11898 int has_pointer;
11900 if (gfc_resolve_expr (var->expr) == FAILURE)
11901 return FAILURE;
11903 ar = NULL;
11904 mpz_init_set_si (offset, 0);
11905 e = var->expr;
11907 if (e->expr_type != EXPR_VARIABLE)
11908 gfc_internal_error ("check_data_variable(): Bad expression");
11910 sym = e->symtree->n.sym;
11912 if (sym->ns->is_block_data && !sym->attr.in_common)
11914 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
11915 sym->name, &sym->declared_at);
11918 if (e->ref == NULL && sym->as)
11920 gfc_error ("DATA array '%s' at %L must be specified in a previous"
11921 " declaration", sym->name, where);
11922 return FAILURE;
11925 has_pointer = sym->attr.pointer;
11927 for (ref = e->ref; ref; ref = ref->next)
11929 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
11930 has_pointer = 1;
11932 if (ref->type == REF_ARRAY && ref->u.ar.codimen)
11934 gfc_error ("DATA element '%s' at %L cannot have a coindex",
11935 sym->name, where);
11936 return FAILURE;
11939 if (has_pointer
11940 && ref->type == REF_ARRAY
11941 && ref->u.ar.type != AR_FULL)
11943 gfc_error ("DATA element '%s' at %L is a pointer and so must "
11944 "be a full array", sym->name, where);
11945 return FAILURE;
11949 if (e->rank == 0 || has_pointer)
11951 mpz_init_set_ui (size, 1);
11952 ref = NULL;
11954 else
11956 ref = e->ref;
11958 /* Find the array section reference. */
11959 for (ref = e->ref; ref; ref = ref->next)
11961 if (ref->type != REF_ARRAY)
11962 continue;
11963 if (ref->u.ar.type == AR_ELEMENT)
11964 continue;
11965 break;
11967 gcc_assert (ref);
11969 /* Set marks according to the reference pattern. */
11970 switch (ref->u.ar.type)
11972 case AR_FULL:
11973 mark = AR_FULL;
11974 break;
11976 case AR_SECTION:
11977 ar = &ref->u.ar;
11978 /* Get the start position of array section. */
11979 gfc_get_section_index (ar, section_index, &offset);
11980 mark = AR_SECTION;
11981 break;
11983 default:
11984 gcc_unreachable ();
11987 if (gfc_array_size (e, &size) == FAILURE)
11989 gfc_error ("Nonconstant array section at %L in DATA statement",
11990 &e->where);
11991 mpz_clear (offset);
11992 return FAILURE;
11996 t = SUCCESS;
11998 while (mpz_cmp_ui (size, 0) > 0)
12000 if (next_data_value () == FAILURE)
12002 gfc_error ("DATA statement at %L has more variables than values",
12003 where);
12004 t = FAILURE;
12005 break;
12008 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
12009 if (t == FAILURE)
12010 break;
12012 /* If we have more than one element left in the repeat count,
12013 and we have more than one element left in the target variable,
12014 then create a range assignment. */
12015 /* FIXME: Only done for full arrays for now, since array sections
12016 seem tricky. */
12017 if (mark == AR_FULL && ref && ref->next == NULL
12018 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
12020 mpz_t range;
12022 if (mpz_cmp (size, values.left) >= 0)
12024 mpz_init_set (range, values.left);
12025 mpz_sub (size, size, values.left);
12026 mpz_set_ui (values.left, 0);
12028 else
12030 mpz_init_set (range, size);
12031 mpz_sub (values.left, values.left, size);
12032 mpz_set_ui (size, 0);
12035 t = gfc_assign_data_value_range (var->expr, values.vnode->expr,
12036 offset, range);
12038 mpz_add (offset, offset, range);
12039 mpz_clear (range);
12041 if (t == FAILURE)
12042 break;
12045 /* Assign initial value to symbol. */
12046 else
12048 mpz_sub_ui (values.left, values.left, 1);
12049 mpz_sub_ui (size, size, 1);
12051 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
12052 if (t == FAILURE)
12053 break;
12055 if (mark == AR_FULL)
12056 mpz_add_ui (offset, offset, 1);
12058 /* Modify the array section indexes and recalculate the offset
12059 for next element. */
12060 else if (mark == AR_SECTION)
12061 gfc_advance_section (section_index, ar, &offset);
12065 if (mark == AR_SECTION)
12067 for (i = 0; i < ar->dimen; i++)
12068 mpz_clear (section_index[i]);
12071 mpz_clear (size);
12072 mpz_clear (offset);
12074 return t;
12078 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
12080 /* Iterate over a list of elements in a DATA statement. */
12082 static gfc_try
12083 traverse_data_list (gfc_data_variable *var, locus *where)
12085 mpz_t trip;
12086 iterator_stack frame;
12087 gfc_expr *e, *start, *end, *step;
12088 gfc_try retval = SUCCESS;
12090 mpz_init (frame.value);
12091 mpz_init (trip);
12093 start = gfc_copy_expr (var->iter.start);
12094 end = gfc_copy_expr (var->iter.end);
12095 step = gfc_copy_expr (var->iter.step);
12097 if (gfc_simplify_expr (start, 1) == FAILURE
12098 || start->expr_type != EXPR_CONSTANT)
12100 gfc_error ("start of implied-do loop at %L could not be "
12101 "simplified to a constant value", &start->where);
12102 retval = FAILURE;
12103 goto cleanup;
12105 if (gfc_simplify_expr (end, 1) == FAILURE
12106 || end->expr_type != EXPR_CONSTANT)
12108 gfc_error ("end of implied-do loop at %L could not be "
12109 "simplified to a constant value", &start->where);
12110 retval = FAILURE;
12111 goto cleanup;
12113 if (gfc_simplify_expr (step, 1) == FAILURE
12114 || step->expr_type != EXPR_CONSTANT)
12116 gfc_error ("step of implied-do loop at %L could not be "
12117 "simplified to a constant value", &start->where);
12118 retval = FAILURE;
12119 goto cleanup;
12122 mpz_set (trip, end->value.integer);
12123 mpz_sub (trip, trip, start->value.integer);
12124 mpz_add (trip, trip, step->value.integer);
12126 mpz_div (trip, trip, step->value.integer);
12128 mpz_set (frame.value, start->value.integer);
12130 frame.prev = iter_stack;
12131 frame.variable = var->iter.var->symtree;
12132 iter_stack = &frame;
12134 while (mpz_cmp_ui (trip, 0) > 0)
12136 if (traverse_data_var (var->list, where) == FAILURE)
12138 retval = FAILURE;
12139 goto cleanup;
12142 e = gfc_copy_expr (var->expr);
12143 if (gfc_simplify_expr (e, 1) == FAILURE)
12145 gfc_free_expr (e);
12146 retval = FAILURE;
12147 goto cleanup;
12150 mpz_add (frame.value, frame.value, step->value.integer);
12152 mpz_sub_ui (trip, trip, 1);
12155 cleanup:
12156 mpz_clear (frame.value);
12157 mpz_clear (trip);
12159 gfc_free_expr (start);
12160 gfc_free_expr (end);
12161 gfc_free_expr (step);
12163 iter_stack = frame.prev;
12164 return retval;
12168 /* Type resolve variables in the variable list of a DATA statement. */
12170 static gfc_try
12171 traverse_data_var (gfc_data_variable *var, locus *where)
12173 gfc_try t;
12175 for (; var; var = var->next)
12177 if (var->expr == NULL)
12178 t = traverse_data_list (var, where);
12179 else
12180 t = check_data_variable (var, where);
12182 if (t == FAILURE)
12183 return FAILURE;
12186 return SUCCESS;
12190 /* Resolve the expressions and iterators associated with a data statement.
12191 This is separate from the assignment checking because data lists should
12192 only be resolved once. */
12194 static gfc_try
12195 resolve_data_variables (gfc_data_variable *d)
12197 for (; d; d = d->next)
12199 if (d->list == NULL)
12201 if (gfc_resolve_expr (d->expr) == FAILURE)
12202 return FAILURE;
12204 else
12206 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
12207 return FAILURE;
12209 if (resolve_data_variables (d->list) == FAILURE)
12210 return FAILURE;
12214 return SUCCESS;
12218 /* Resolve a single DATA statement. We implement this by storing a pointer to
12219 the value list into static variables, and then recursively traversing the
12220 variables list, expanding iterators and such. */
12222 static void
12223 resolve_data (gfc_data *d)
12226 if (resolve_data_variables (d->var) == FAILURE)
12227 return;
12229 values.vnode = d->value;
12230 if (d->value == NULL)
12231 mpz_set_ui (values.left, 0);
12232 else
12233 mpz_set (values.left, d->value->repeat);
12235 if (traverse_data_var (d->var, &d->where) == FAILURE)
12236 return;
12238 /* At this point, we better not have any values left. */
12240 if (next_data_value () == SUCCESS)
12241 gfc_error ("DATA statement at %L has more values than variables",
12242 &d->where);
12246 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
12247 accessed by host or use association, is a dummy argument to a pure function,
12248 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
12249 is storage associated with any such variable, shall not be used in the
12250 following contexts: (clients of this function). */
12252 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
12253 procedure. Returns zero if assignment is OK, nonzero if there is a
12254 problem. */
12256 gfc_impure_variable (gfc_symbol *sym)
12258 gfc_symbol *proc;
12259 gfc_namespace *ns;
12261 if (sym->attr.use_assoc || sym->attr.in_common)
12262 return 1;
12264 /* Check if the symbol's ns is inside the pure procedure. */
12265 for (ns = gfc_current_ns; ns; ns = ns->parent)
12267 if (ns == sym->ns)
12268 break;
12269 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
12270 return 1;
12273 proc = sym->ns->proc_name;
12274 if (sym->attr.dummy && gfc_pure (proc)
12275 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
12277 proc->attr.function))
12278 return 1;
12280 /* TODO: Sort out what can be storage associated, if anything, and include
12281 it here. In principle equivalences should be scanned but it does not
12282 seem to be possible to storage associate an impure variable this way. */
12283 return 0;
12287 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
12288 current namespace is inside a pure procedure. */
12291 gfc_pure (gfc_symbol *sym)
12293 symbol_attribute attr;
12294 gfc_namespace *ns;
12296 if (sym == NULL)
12298 /* Check if the current namespace or one of its parents
12299 belongs to a pure procedure. */
12300 for (ns = gfc_current_ns; ns; ns = ns->parent)
12302 sym = ns->proc_name;
12303 if (sym == NULL)
12304 return 0;
12305 attr = sym->attr;
12306 if (attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental))
12307 return 1;
12309 return 0;
12312 attr = sym->attr;
12314 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
12318 /* Test whether the current procedure is elemental or not. */
12321 gfc_elemental (gfc_symbol *sym)
12323 symbol_attribute attr;
12325 if (sym == NULL)
12326 sym = gfc_current_ns->proc_name;
12327 if (sym == NULL)
12328 return 0;
12329 attr = sym->attr;
12331 return attr.flavor == FL_PROCEDURE && attr.elemental;
12335 /* Warn about unused labels. */
12337 static void
12338 warn_unused_fortran_label (gfc_st_label *label)
12340 if (label == NULL)
12341 return;
12343 warn_unused_fortran_label (label->left);
12345 if (label->defined == ST_LABEL_UNKNOWN)
12346 return;
12348 switch (label->referenced)
12350 case ST_LABEL_UNKNOWN:
12351 gfc_warning ("Label %d at %L defined but not used", label->value,
12352 &label->where);
12353 break;
12355 case ST_LABEL_BAD_TARGET:
12356 gfc_warning ("Label %d at %L defined but cannot be used",
12357 label->value, &label->where);
12358 break;
12360 default:
12361 break;
12364 warn_unused_fortran_label (label->right);
12368 /* Returns the sequence type of a symbol or sequence. */
12370 static seq_type
12371 sequence_type (gfc_typespec ts)
12373 seq_type result;
12374 gfc_component *c;
12376 switch (ts.type)
12378 case BT_DERIVED:
12380 if (ts.u.derived->components == NULL)
12381 return SEQ_NONDEFAULT;
12383 result = sequence_type (ts.u.derived->components->ts);
12384 for (c = ts.u.derived->components->next; c; c = c->next)
12385 if (sequence_type (c->ts) != result)
12386 return SEQ_MIXED;
12388 return result;
12390 case BT_CHARACTER:
12391 if (ts.kind != gfc_default_character_kind)
12392 return SEQ_NONDEFAULT;
12394 return SEQ_CHARACTER;
12396 case BT_INTEGER:
12397 if (ts.kind != gfc_default_integer_kind)
12398 return SEQ_NONDEFAULT;
12400 return SEQ_NUMERIC;
12402 case BT_REAL:
12403 if (!(ts.kind == gfc_default_real_kind
12404 || ts.kind == gfc_default_double_kind))
12405 return SEQ_NONDEFAULT;
12407 return SEQ_NUMERIC;
12409 case BT_COMPLEX:
12410 if (ts.kind != gfc_default_complex_kind)
12411 return SEQ_NONDEFAULT;
12413 return SEQ_NUMERIC;
12415 case BT_LOGICAL:
12416 if (ts.kind != gfc_default_logical_kind)
12417 return SEQ_NONDEFAULT;
12419 return SEQ_NUMERIC;
12421 default:
12422 return SEQ_NONDEFAULT;
12427 /* Resolve derived type EQUIVALENCE object. */
12429 static gfc_try
12430 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
12432 gfc_component *c = derived->components;
12434 if (!derived)
12435 return SUCCESS;
12437 /* Shall not be an object of nonsequence derived type. */
12438 if (!derived->attr.sequence)
12440 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
12441 "attribute to be an EQUIVALENCE object", sym->name,
12442 &e->where);
12443 return FAILURE;
12446 /* Shall not have allocatable components. */
12447 if (derived->attr.alloc_comp)
12449 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
12450 "components to be an EQUIVALENCE object",sym->name,
12451 &e->where);
12452 return FAILURE;
12455 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
12457 gfc_error ("Derived type variable '%s' at %L with default "
12458 "initialization cannot be in EQUIVALENCE with a variable "
12459 "in COMMON", sym->name, &e->where);
12460 return FAILURE;
12463 for (; c ; c = c->next)
12465 if (c->ts.type == BT_DERIVED
12466 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
12467 return FAILURE;
12469 /* Shall not be an object of sequence derived type containing a pointer
12470 in the structure. */
12471 if (c->attr.pointer)
12473 gfc_error ("Derived type variable '%s' at %L with pointer "
12474 "component(s) cannot be an EQUIVALENCE object",
12475 sym->name, &e->where);
12476 return FAILURE;
12479 return SUCCESS;
12483 /* Resolve equivalence object.
12484 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
12485 an allocatable array, an object of nonsequence derived type, an object of
12486 sequence derived type containing a pointer at any level of component
12487 selection, an automatic object, a function name, an entry name, a result
12488 name, a named constant, a structure component, or a subobject of any of
12489 the preceding objects. A substring shall not have length zero. A
12490 derived type shall not have components with default initialization nor
12491 shall two objects of an equivalence group be initialized.
12492 Either all or none of the objects shall have an protected attribute.
12493 The simple constraints are done in symbol.c(check_conflict) and the rest
12494 are implemented here. */
12496 static void
12497 resolve_equivalence (gfc_equiv *eq)
12499 gfc_symbol *sym;
12500 gfc_symbol *first_sym;
12501 gfc_expr *e;
12502 gfc_ref *r;
12503 locus *last_where = NULL;
12504 seq_type eq_type, last_eq_type;
12505 gfc_typespec *last_ts;
12506 int object, cnt_protected;
12507 const char *msg;
12509 last_ts = &eq->expr->symtree->n.sym->ts;
12511 first_sym = eq->expr->symtree->n.sym;
12513 cnt_protected = 0;
12515 for (object = 1; eq; eq = eq->eq, object++)
12517 e = eq->expr;
12519 e->ts = e->symtree->n.sym->ts;
12520 /* match_varspec might not know yet if it is seeing
12521 array reference or substring reference, as it doesn't
12522 know the types. */
12523 if (e->ref && e->ref->type == REF_ARRAY)
12525 gfc_ref *ref = e->ref;
12526 sym = e->symtree->n.sym;
12528 if (sym->attr.dimension)
12530 ref->u.ar.as = sym->as;
12531 ref = ref->next;
12534 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
12535 if (e->ts.type == BT_CHARACTER
12536 && ref
12537 && ref->type == REF_ARRAY
12538 && ref->u.ar.dimen == 1
12539 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
12540 && ref->u.ar.stride[0] == NULL)
12542 gfc_expr *start = ref->u.ar.start[0];
12543 gfc_expr *end = ref->u.ar.end[0];
12544 void *mem = NULL;
12546 /* Optimize away the (:) reference. */
12547 if (start == NULL && end == NULL)
12549 if (e->ref == ref)
12550 e->ref = ref->next;
12551 else
12552 e->ref->next = ref->next;
12553 mem = ref;
12555 else
12557 ref->type = REF_SUBSTRING;
12558 if (start == NULL)
12559 start = gfc_get_int_expr (gfc_default_integer_kind,
12560 NULL, 1);
12561 ref->u.ss.start = start;
12562 if (end == NULL && e->ts.u.cl)
12563 end = gfc_copy_expr (e->ts.u.cl->length);
12564 ref->u.ss.end = end;
12565 ref->u.ss.length = e->ts.u.cl;
12566 e->ts.u.cl = NULL;
12568 ref = ref->next;
12569 gfc_free (mem);
12572 /* Any further ref is an error. */
12573 if (ref)
12575 gcc_assert (ref->type == REF_ARRAY);
12576 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
12577 &ref->u.ar.where);
12578 continue;
12582 if (gfc_resolve_expr (e) == FAILURE)
12583 continue;
12585 sym = e->symtree->n.sym;
12587 if (sym->attr.is_protected)
12588 cnt_protected++;
12589 if (cnt_protected > 0 && cnt_protected != object)
12591 gfc_error ("Either all or none of the objects in the "
12592 "EQUIVALENCE set at %L shall have the "
12593 "PROTECTED attribute",
12594 &e->where);
12595 break;
12598 /* Shall not equivalence common block variables in a PURE procedure. */
12599 if (sym->ns->proc_name
12600 && sym->ns->proc_name->attr.pure
12601 && sym->attr.in_common)
12603 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
12604 "object in the pure procedure '%s'",
12605 sym->name, &e->where, sym->ns->proc_name->name);
12606 break;
12609 /* Shall not be a named constant. */
12610 if (e->expr_type == EXPR_CONSTANT)
12612 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
12613 "object", sym->name, &e->where);
12614 continue;
12617 if (e->ts.type == BT_DERIVED
12618 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
12619 continue;
12621 /* Check that the types correspond correctly:
12622 Note 5.28:
12623 A numeric sequence structure may be equivalenced to another sequence
12624 structure, an object of default integer type, default real type, double
12625 precision real type, default logical type such that components of the
12626 structure ultimately only become associated to objects of the same
12627 kind. A character sequence structure may be equivalenced to an object
12628 of default character kind or another character sequence structure.
12629 Other objects may be equivalenced only to objects of the same type and
12630 kind parameters. */
12632 /* Identical types are unconditionally OK. */
12633 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
12634 goto identical_types;
12636 last_eq_type = sequence_type (*last_ts);
12637 eq_type = sequence_type (sym->ts);
12639 /* Since the pair of objects is not of the same type, mixed or
12640 non-default sequences can be rejected. */
12642 msg = "Sequence %s with mixed components in EQUIVALENCE "
12643 "statement at %L with different type objects";
12644 if ((object ==2
12645 && last_eq_type == SEQ_MIXED
12646 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
12647 == FAILURE)
12648 || (eq_type == SEQ_MIXED
12649 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12650 &e->where) == FAILURE))
12651 continue;
12653 msg = "Non-default type object or sequence %s in EQUIVALENCE "
12654 "statement at %L with objects of different type";
12655 if ((object ==2
12656 && last_eq_type == SEQ_NONDEFAULT
12657 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
12658 last_where) == FAILURE)
12659 || (eq_type == SEQ_NONDEFAULT
12660 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12661 &e->where) == FAILURE))
12662 continue;
12664 msg ="Non-CHARACTER object '%s' in default CHARACTER "
12665 "EQUIVALENCE statement at %L";
12666 if (last_eq_type == SEQ_CHARACTER
12667 && eq_type != SEQ_CHARACTER
12668 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12669 &e->where) == FAILURE)
12670 continue;
12672 msg ="Non-NUMERIC object '%s' in default NUMERIC "
12673 "EQUIVALENCE statement at %L";
12674 if (last_eq_type == SEQ_NUMERIC
12675 && eq_type != SEQ_NUMERIC
12676 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12677 &e->where) == FAILURE)
12678 continue;
12680 identical_types:
12681 last_ts =&sym->ts;
12682 last_where = &e->where;
12684 if (!e->ref)
12685 continue;
12687 /* Shall not be an automatic array. */
12688 if (e->ref->type == REF_ARRAY
12689 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
12691 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
12692 "an EQUIVALENCE object", sym->name, &e->where);
12693 continue;
12696 r = e->ref;
12697 while (r)
12699 /* Shall not be a structure component. */
12700 if (r->type == REF_COMPONENT)
12702 gfc_error ("Structure component '%s' at %L cannot be an "
12703 "EQUIVALENCE object",
12704 r->u.c.component->name, &e->where);
12705 break;
12708 /* A substring shall not have length zero. */
12709 if (r->type == REF_SUBSTRING)
12711 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
12713 gfc_error ("Substring at %L has length zero",
12714 &r->u.ss.start->where);
12715 break;
12718 r = r->next;
12724 /* Resolve function and ENTRY types, issue diagnostics if needed. */
12726 static void
12727 resolve_fntype (gfc_namespace *ns)
12729 gfc_entry_list *el;
12730 gfc_symbol *sym;
12732 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
12733 return;
12735 /* If there are any entries, ns->proc_name is the entry master
12736 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
12737 if (ns->entries)
12738 sym = ns->entries->sym;
12739 else
12740 sym = ns->proc_name;
12741 if (sym->result == sym
12742 && sym->ts.type == BT_UNKNOWN
12743 && gfc_set_default_type (sym, 0, NULL) == FAILURE
12744 && !sym->attr.untyped)
12746 gfc_error ("Function '%s' at %L has no IMPLICIT type",
12747 sym->name, &sym->declared_at);
12748 sym->attr.untyped = 1;
12751 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
12752 && !sym->attr.contained
12753 && !gfc_check_access (sym->ts.u.derived->attr.access,
12754 sym->ts.u.derived->ns->default_access)
12755 && gfc_check_access (sym->attr.access, sym->ns->default_access))
12757 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
12758 "%L of PRIVATE type '%s'", sym->name,
12759 &sym->declared_at, sym->ts.u.derived->name);
12762 if (ns->entries)
12763 for (el = ns->entries->next; el; el = el->next)
12765 if (el->sym->result == el->sym
12766 && el->sym->ts.type == BT_UNKNOWN
12767 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
12768 && !el->sym->attr.untyped)
12770 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
12771 el->sym->name, &el->sym->declared_at);
12772 el->sym->attr.untyped = 1;
12778 /* 12.3.2.1.1 Defined operators. */
12780 static gfc_try
12781 check_uop_procedure (gfc_symbol *sym, locus where)
12783 gfc_formal_arglist *formal;
12785 if (!sym->attr.function)
12787 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
12788 sym->name, &where);
12789 return FAILURE;
12792 if (sym->ts.type == BT_CHARACTER
12793 && !(sym->ts.u.cl && sym->ts.u.cl->length)
12794 && !(sym->result && sym->result->ts.u.cl
12795 && sym->result->ts.u.cl->length))
12797 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
12798 "character length", sym->name, &where);
12799 return FAILURE;
12802 formal = sym->formal;
12803 if (!formal || !formal->sym)
12805 gfc_error ("User operator procedure '%s' at %L must have at least "
12806 "one argument", sym->name, &where);
12807 return FAILURE;
12810 if (formal->sym->attr.intent != INTENT_IN)
12812 gfc_error ("First argument of operator interface at %L must be "
12813 "INTENT(IN)", &where);
12814 return FAILURE;
12817 if (formal->sym->attr.optional)
12819 gfc_error ("First argument of operator interface at %L cannot be "
12820 "optional", &where);
12821 return FAILURE;
12824 formal = formal->next;
12825 if (!formal || !formal->sym)
12826 return SUCCESS;
12828 if (formal->sym->attr.intent != INTENT_IN)
12830 gfc_error ("Second argument of operator interface at %L must be "
12831 "INTENT(IN)", &where);
12832 return FAILURE;
12835 if (formal->sym->attr.optional)
12837 gfc_error ("Second argument of operator interface at %L cannot be "
12838 "optional", &where);
12839 return FAILURE;
12842 if (formal->next)
12844 gfc_error ("Operator interface at %L must have, at most, two "
12845 "arguments", &where);
12846 return FAILURE;
12849 return SUCCESS;
12852 static void
12853 gfc_resolve_uops (gfc_symtree *symtree)
12855 gfc_interface *itr;
12857 if (symtree == NULL)
12858 return;
12860 gfc_resolve_uops (symtree->left);
12861 gfc_resolve_uops (symtree->right);
12863 for (itr = symtree->n.uop->op; itr; itr = itr->next)
12864 check_uop_procedure (itr->sym, itr->sym->declared_at);
12868 /* Examine all of the expressions associated with a program unit,
12869 assign types to all intermediate expressions, make sure that all
12870 assignments are to compatible types and figure out which names
12871 refer to which functions or subroutines. It doesn't check code
12872 block, which is handled by resolve_code. */
12874 static void
12875 resolve_types (gfc_namespace *ns)
12877 gfc_namespace *n;
12878 gfc_charlen *cl;
12879 gfc_data *d;
12880 gfc_equiv *eq;
12881 gfc_namespace* old_ns = gfc_current_ns;
12883 /* Check that all IMPLICIT types are ok. */
12884 if (!ns->seen_implicit_none)
12886 unsigned letter;
12887 for (letter = 0; letter != GFC_LETTERS; ++letter)
12888 if (ns->set_flag[letter]
12889 && resolve_typespec_used (&ns->default_type[letter],
12890 &ns->implicit_loc[letter],
12891 NULL) == FAILURE)
12892 return;
12895 gfc_current_ns = ns;
12897 resolve_entries (ns);
12899 resolve_common_vars (ns->blank_common.head, false);
12900 resolve_common_blocks (ns->common_root);
12902 resolve_contained_functions (ns);
12904 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
12906 for (cl = ns->cl_list; cl; cl = cl->next)
12907 resolve_charlen (cl);
12909 gfc_traverse_ns (ns, resolve_symbol);
12911 resolve_fntype (ns);
12913 for (n = ns->contained; n; n = n->sibling)
12915 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
12916 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
12917 "also be PURE", n->proc_name->name,
12918 &n->proc_name->declared_at);
12920 resolve_types (n);
12923 forall_flag = 0;
12924 gfc_check_interfaces (ns);
12926 gfc_traverse_ns (ns, resolve_values);
12928 if (ns->save_all)
12929 gfc_save_all (ns);
12931 iter_stack = NULL;
12932 for (d = ns->data; d; d = d->next)
12933 resolve_data (d);
12935 iter_stack = NULL;
12936 gfc_traverse_ns (ns, gfc_formalize_init_value);
12938 gfc_traverse_ns (ns, gfc_verify_binding_labels);
12940 if (ns->common_root != NULL)
12941 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
12943 for (eq = ns->equiv; eq; eq = eq->next)
12944 resolve_equivalence (eq);
12946 /* Warn about unused labels. */
12947 if (warn_unused_label)
12948 warn_unused_fortran_label (ns->st_labels);
12950 gfc_resolve_uops (ns->uop_root);
12952 gfc_current_ns = old_ns;
12956 /* Call resolve_code recursively. */
12958 static void
12959 resolve_codes (gfc_namespace *ns)
12961 gfc_namespace *n;
12962 bitmap_obstack old_obstack;
12964 for (n = ns->contained; n; n = n->sibling)
12965 resolve_codes (n);
12967 gfc_current_ns = ns;
12969 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
12970 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
12971 cs_base = NULL;
12973 /* Set to an out of range value. */
12974 current_entry_id = -1;
12976 old_obstack = labels_obstack;
12977 bitmap_obstack_initialize (&labels_obstack);
12979 resolve_code (ns->code, ns);
12981 bitmap_obstack_release (&labels_obstack);
12982 labels_obstack = old_obstack;
12986 /* This function is called after a complete program unit has been compiled.
12987 Its purpose is to examine all of the expressions associated with a program
12988 unit, assign types to all intermediate expressions, make sure that all
12989 assignments are to compatible types and figure out which names refer to
12990 which functions or subroutines. */
12992 void
12993 gfc_resolve (gfc_namespace *ns)
12995 gfc_namespace *old_ns;
12996 code_stack *old_cs_base;
12998 if (ns->resolved)
12999 return;
13001 ns->resolved = -1;
13002 old_ns = gfc_current_ns;
13003 old_cs_base = cs_base;
13005 resolve_types (ns);
13006 resolve_codes (ns);
13008 gfc_current_ns = old_ns;
13009 cs_base = old_cs_base;
13010 ns->resolved = 1;