Remove outermost loop parameter.
[official-gcc/graphite-test-results.git] / gcc / fortran / resolve.c
blob48bb6187c1712cbc7324ff2400d663328c9cac42
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.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 if (gsym->ns->proc_name->attr.function
1862 && gsym->ns->proc_name->as
1863 && gsym->ns->proc_name->as->rank
1864 && (!sym->as || sym->as->rank != gsym->ns->proc_name->as->rank))
1865 gfc_error ("The reference to function '%s' at %L either needs an "
1866 "explicit INTERFACE or the rank is incorrect", sym->name,
1867 where);
1869 /* Non-assumed length character functions. */
1870 if (sym->attr.function && sym->ts.type == BT_CHARACTER
1871 && gsym->ns->proc_name->ts.u.cl->length != NULL)
1873 gfc_charlen *cl = sym->ts.u.cl;
1875 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
1876 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
1878 gfc_error ("Nonconstant character-length function '%s' at %L "
1879 "must have an explicit interface", sym->name,
1880 &sym->declared_at);
1884 /* Differences in constant character lengths. */
1885 if (sym->attr.function && sym->ts.type == BT_CHARACTER)
1887 long int l1 = 0, l2 = 0;
1888 gfc_charlen *cl1 = sym->ts.u.cl;
1889 gfc_charlen *cl2 = gsym->ns->proc_name->ts.u.cl;
1891 if (cl1 != NULL
1892 && cl1->length != NULL
1893 && cl1->length->expr_type == EXPR_CONSTANT)
1894 l1 = mpz_get_si (cl1->length->value.integer);
1896 if (cl2 != NULL
1897 && cl2->length != NULL
1898 && cl2->length->expr_type == EXPR_CONSTANT)
1899 l2 = mpz_get_si (cl2->length->value.integer);
1901 if (l1 && l2 && l1 != l2)
1902 gfc_error ("Character length mismatch in return type of "
1903 "function '%s' at %L (%ld/%ld)", sym->name,
1904 &sym->declared_at, l1, l2);
1907 /* Type mismatch of function return type and expected type. */
1908 if (sym->attr.function
1909 && !gfc_compare_types (&sym->ts, &gsym->ns->proc_name->ts))
1910 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
1911 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
1912 gfc_typename (&gsym->ns->proc_name->ts));
1914 /* Assumed shape arrays as dummy arguments. */
1915 if (gsym->ns->proc_name->formal)
1917 gfc_formal_arglist *arg = gsym->ns->proc_name->formal;
1918 for ( ; arg; arg = arg->next)
1919 if (arg->sym && arg->sym->as
1920 && arg->sym->as->type == AS_ASSUMED_SHAPE)
1922 gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
1923 "'%s' argument must have an explicit interface",
1924 sym->name, &sym->declared_at, arg->sym->name);
1925 break;
1927 else if (arg->sym && arg->sym->attr.optional)
1929 gfc_error ("Procedure '%s' at %L with optional dummy argument "
1930 "'%s' must have an explicit interface",
1931 sym->name, &sym->declared_at, arg->sym->name);
1932 break;
1936 if (gfc_option.flag_whole_file == 1
1937 || ((gfc_option.warn_std & GFC_STD_LEGACY)
1938 && !(gfc_option.warn_std & GFC_STD_GNU)))
1939 gfc_errors_to_warnings (1);
1941 gfc_procedure_use (gsym->ns->proc_name, actual, where);
1943 gfc_errors_to_warnings (0);
1946 if (gsym->type == GSYM_UNKNOWN)
1948 gsym->type = type;
1949 gsym->where = *where;
1952 gsym->used = 1;
1956 /************* Function resolution *************/
1958 /* Resolve a function call known to be generic.
1959 Section 14.1.2.4.1. */
1961 static match
1962 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
1964 gfc_symbol *s;
1966 if (sym->attr.generic)
1968 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1969 if (s != NULL)
1971 expr->value.function.name = s->name;
1972 expr->value.function.esym = s;
1974 if (s->ts.type != BT_UNKNOWN)
1975 expr->ts = s->ts;
1976 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1977 expr->ts = s->result->ts;
1979 if (s->as != NULL)
1980 expr->rank = s->as->rank;
1981 else if (s->result != NULL && s->result->as != NULL)
1982 expr->rank = s->result->as->rank;
1984 gfc_set_sym_referenced (expr->value.function.esym);
1986 return MATCH_YES;
1989 /* TODO: Need to search for elemental references in generic
1990 interface. */
1993 if (sym->attr.intrinsic)
1994 return gfc_intrinsic_func_interface (expr, 0);
1996 return MATCH_NO;
2000 static gfc_try
2001 resolve_generic_f (gfc_expr *expr)
2003 gfc_symbol *sym;
2004 match m;
2006 sym = expr->symtree->n.sym;
2008 for (;;)
2010 m = resolve_generic_f0 (expr, sym);
2011 if (m == MATCH_YES)
2012 return SUCCESS;
2013 else if (m == MATCH_ERROR)
2014 return FAILURE;
2016 generic:
2017 if (sym->ns->parent == NULL)
2018 break;
2019 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2021 if (sym == NULL)
2022 break;
2023 if (!generic_sym (sym))
2024 goto generic;
2027 /* Last ditch attempt. See if the reference is to an intrinsic
2028 that possesses a matching interface. 14.1.2.4 */
2029 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
2031 gfc_error ("There is no specific function for the generic '%s' at %L",
2032 expr->symtree->n.sym->name, &expr->where);
2033 return FAILURE;
2036 m = gfc_intrinsic_func_interface (expr, 0);
2037 if (m == MATCH_YES)
2038 return SUCCESS;
2039 if (m == MATCH_NO)
2040 gfc_error ("Generic function '%s' at %L is not consistent with a "
2041 "specific intrinsic interface", expr->symtree->n.sym->name,
2042 &expr->where);
2044 return FAILURE;
2048 /* Resolve a function call known to be specific. */
2050 static match
2051 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2053 match m;
2055 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2057 if (sym->attr.dummy)
2059 sym->attr.proc = PROC_DUMMY;
2060 goto found;
2063 sym->attr.proc = PROC_EXTERNAL;
2064 goto found;
2067 if (sym->attr.proc == PROC_MODULE
2068 || sym->attr.proc == PROC_ST_FUNCTION
2069 || sym->attr.proc == PROC_INTERNAL)
2070 goto found;
2072 if (sym->attr.intrinsic)
2074 m = gfc_intrinsic_func_interface (expr, 1);
2075 if (m == MATCH_YES)
2076 return MATCH_YES;
2077 if (m == MATCH_NO)
2078 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2079 "with an intrinsic", sym->name, &expr->where);
2081 return MATCH_ERROR;
2084 return MATCH_NO;
2086 found:
2087 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2089 if (sym->result)
2090 expr->ts = sym->result->ts;
2091 else
2092 expr->ts = sym->ts;
2093 expr->value.function.name = sym->name;
2094 expr->value.function.esym = sym;
2095 if (sym->as != NULL)
2096 expr->rank = sym->as->rank;
2098 return MATCH_YES;
2102 static gfc_try
2103 resolve_specific_f (gfc_expr *expr)
2105 gfc_symbol *sym;
2106 match m;
2108 sym = expr->symtree->n.sym;
2110 for (;;)
2112 m = resolve_specific_f0 (sym, expr);
2113 if (m == MATCH_YES)
2114 return SUCCESS;
2115 if (m == MATCH_ERROR)
2116 return FAILURE;
2118 if (sym->ns->parent == NULL)
2119 break;
2121 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2123 if (sym == NULL)
2124 break;
2127 gfc_error ("Unable to resolve the specific function '%s' at %L",
2128 expr->symtree->n.sym->name, &expr->where);
2130 return SUCCESS;
2134 /* Resolve a procedure call not known to be generic nor specific. */
2136 static gfc_try
2137 resolve_unknown_f (gfc_expr *expr)
2139 gfc_symbol *sym;
2140 gfc_typespec *ts;
2142 sym = expr->symtree->n.sym;
2144 if (sym->attr.dummy)
2146 sym->attr.proc = PROC_DUMMY;
2147 expr->value.function.name = sym->name;
2148 goto set_type;
2151 /* See if we have an intrinsic function reference. */
2153 if (gfc_is_intrinsic (sym, 0, expr->where))
2155 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2156 return SUCCESS;
2157 return FAILURE;
2160 /* The reference is to an external name. */
2162 sym->attr.proc = PROC_EXTERNAL;
2163 expr->value.function.name = sym->name;
2164 expr->value.function.esym = expr->symtree->n.sym;
2166 if (sym->as != NULL)
2167 expr->rank = sym->as->rank;
2169 /* Type of the expression is either the type of the symbol or the
2170 default type of the symbol. */
2172 set_type:
2173 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2175 if (sym->ts.type != BT_UNKNOWN)
2176 expr->ts = sym->ts;
2177 else
2179 ts = gfc_get_default_type (sym->name, sym->ns);
2181 if (ts->type == BT_UNKNOWN)
2183 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2184 sym->name, &expr->where);
2185 return FAILURE;
2187 else
2188 expr->ts = *ts;
2191 return SUCCESS;
2195 /* Return true, if the symbol is an external procedure. */
2196 static bool
2197 is_external_proc (gfc_symbol *sym)
2199 if (!sym->attr.dummy && !sym->attr.contained
2200 && !(sym->attr.intrinsic
2201 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2202 && sym->attr.proc != PROC_ST_FUNCTION
2203 && !sym->attr.use_assoc
2204 && sym->name)
2205 return true;
2207 return false;
2211 /* Figure out if a function reference is pure or not. Also set the name
2212 of the function for a potential error message. Return nonzero if the
2213 function is PURE, zero if not. */
2214 static int
2215 pure_stmt_function (gfc_expr *, gfc_symbol *);
2217 static int
2218 pure_function (gfc_expr *e, const char **name)
2220 int pure;
2222 *name = NULL;
2224 if (e->symtree != NULL
2225 && e->symtree->n.sym != NULL
2226 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2227 return pure_stmt_function (e, e->symtree->n.sym);
2229 if (e->value.function.esym)
2231 pure = gfc_pure (e->value.function.esym);
2232 *name = e->value.function.esym->name;
2234 else if (e->value.function.isym)
2236 pure = e->value.function.isym->pure
2237 || e->value.function.isym->elemental;
2238 *name = e->value.function.isym->name;
2240 else
2242 /* Implicit functions are not pure. */
2243 pure = 0;
2244 *name = e->value.function.name;
2247 return pure;
2251 static bool
2252 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2253 int *f ATTRIBUTE_UNUSED)
2255 const char *name;
2257 /* Don't bother recursing into other statement functions
2258 since they will be checked individually for purity. */
2259 if (e->expr_type != EXPR_FUNCTION
2260 || !e->symtree
2261 || e->symtree->n.sym == sym
2262 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2263 return false;
2265 return pure_function (e, &name) ? false : true;
2269 static int
2270 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2272 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2276 static gfc_try
2277 is_scalar_expr_ptr (gfc_expr *expr)
2279 gfc_try retval = SUCCESS;
2280 gfc_ref *ref;
2281 int start;
2282 int end;
2284 /* See if we have a gfc_ref, which means we have a substring, array
2285 reference, or a component. */
2286 if (expr->ref != NULL)
2288 ref = expr->ref;
2289 while (ref->next != NULL)
2290 ref = ref->next;
2292 switch (ref->type)
2294 case REF_SUBSTRING:
2295 if (ref->u.ss.length != NULL
2296 && ref->u.ss.length->length != NULL
2297 && ref->u.ss.start
2298 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2299 && ref->u.ss.end
2300 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2302 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2303 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2304 if (end - start + 1 != 1)
2305 retval = FAILURE;
2307 else
2308 retval = FAILURE;
2309 break;
2310 case REF_ARRAY:
2311 if (ref->u.ar.type == AR_ELEMENT)
2312 retval = SUCCESS;
2313 else if (ref->u.ar.type == AR_FULL)
2315 /* The user can give a full array if the array is of size 1. */
2316 if (ref->u.ar.as != NULL
2317 && ref->u.ar.as->rank == 1
2318 && ref->u.ar.as->type == AS_EXPLICIT
2319 && ref->u.ar.as->lower[0] != NULL
2320 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2321 && ref->u.ar.as->upper[0] != NULL
2322 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2324 /* If we have a character string, we need to check if
2325 its length is one. */
2326 if (expr->ts.type == BT_CHARACTER)
2328 if (expr->ts.u.cl == NULL
2329 || expr->ts.u.cl->length == NULL
2330 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2331 != 0)
2332 retval = FAILURE;
2334 else
2336 /* We have constant lower and upper bounds. If the
2337 difference between is 1, it can be considered a
2338 scalar. */
2339 start = (int) mpz_get_si
2340 (ref->u.ar.as->lower[0]->value.integer);
2341 end = (int) mpz_get_si
2342 (ref->u.ar.as->upper[0]->value.integer);
2343 if (end - start + 1 != 1)
2344 retval = FAILURE;
2347 else
2348 retval = FAILURE;
2350 else
2351 retval = FAILURE;
2352 break;
2353 default:
2354 retval = SUCCESS;
2355 break;
2358 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2360 /* Character string. Make sure it's of length 1. */
2361 if (expr->ts.u.cl == NULL
2362 || expr->ts.u.cl->length == NULL
2363 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2364 retval = FAILURE;
2366 else if (expr->rank != 0)
2367 retval = FAILURE;
2369 return retval;
2373 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2374 and, in the case of c_associated, set the binding label based on
2375 the arguments. */
2377 static gfc_try
2378 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2379 gfc_symbol **new_sym)
2381 char name[GFC_MAX_SYMBOL_LEN + 1];
2382 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2383 int optional_arg = 0, is_pointer = 0;
2384 gfc_try retval = SUCCESS;
2385 gfc_symbol *args_sym;
2386 gfc_typespec *arg_ts;
2388 if (args->expr->expr_type == EXPR_CONSTANT
2389 || args->expr->expr_type == EXPR_OP
2390 || args->expr->expr_type == EXPR_NULL)
2392 gfc_error ("Argument to '%s' at %L is not a variable",
2393 sym->name, &(args->expr->where));
2394 return FAILURE;
2397 args_sym = args->expr->symtree->n.sym;
2399 /* The typespec for the actual arg should be that stored in the expr
2400 and not necessarily that of the expr symbol (args_sym), because
2401 the actual expression could be a part-ref of the expr symbol. */
2402 arg_ts = &(args->expr->ts);
2404 is_pointer = gfc_is_data_pointer (args->expr);
2406 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2408 /* If the user gave two args then they are providing something for
2409 the optional arg (the second cptr). Therefore, set the name and
2410 binding label to the c_associated for two cptrs. Otherwise,
2411 set c_associated to expect one cptr. */
2412 if (args->next)
2414 /* two args. */
2415 sprintf (name, "%s_2", sym->name);
2416 sprintf (binding_label, "%s_2", sym->binding_label);
2417 optional_arg = 1;
2419 else
2421 /* one arg. */
2422 sprintf (name, "%s_1", sym->name);
2423 sprintf (binding_label, "%s_1", sym->binding_label);
2424 optional_arg = 0;
2427 /* Get a new symbol for the version of c_associated that
2428 will get called. */
2429 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2431 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2432 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2434 sprintf (name, "%s", sym->name);
2435 sprintf (binding_label, "%s", sym->binding_label);
2437 /* Error check the call. */
2438 if (args->next != NULL)
2440 gfc_error_now ("More actual than formal arguments in '%s' "
2441 "call at %L", name, &(args->expr->where));
2442 retval = FAILURE;
2444 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2446 /* Make sure we have either the target or pointer attribute. */
2447 if (!args_sym->attr.target && !is_pointer)
2449 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2450 "a TARGET or an associated pointer",
2451 args_sym->name,
2452 sym->name, &(args->expr->where));
2453 retval = FAILURE;
2456 /* See if we have interoperable type and type param. */
2457 if (verify_c_interop (arg_ts) == SUCCESS
2458 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2460 if (args_sym->attr.target == 1)
2462 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2463 has the target attribute and is interoperable. */
2464 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2465 allocatable variable that has the TARGET attribute and
2466 is not an array of zero size. */
2467 if (args_sym->attr.allocatable == 1)
2469 if (args_sym->attr.dimension != 0
2470 && (args_sym->as && args_sym->as->rank == 0))
2472 gfc_error_now ("Allocatable variable '%s' used as a "
2473 "parameter to '%s' at %L must not be "
2474 "an array of zero size",
2475 args_sym->name, sym->name,
2476 &(args->expr->where));
2477 retval = FAILURE;
2480 else
2482 /* A non-allocatable target variable with C
2483 interoperable type and type parameters must be
2484 interoperable. */
2485 if (args_sym && args_sym->attr.dimension)
2487 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2489 gfc_error ("Assumed-shape array '%s' at %L "
2490 "cannot be an argument to the "
2491 "procedure '%s' because "
2492 "it is not C interoperable",
2493 args_sym->name,
2494 &(args->expr->where), sym->name);
2495 retval = FAILURE;
2497 else if (args_sym->as->type == AS_DEFERRED)
2499 gfc_error ("Deferred-shape array '%s' at %L "
2500 "cannot be an argument to the "
2501 "procedure '%s' because "
2502 "it is not C interoperable",
2503 args_sym->name,
2504 &(args->expr->where), sym->name);
2505 retval = FAILURE;
2509 /* Make sure it's not a character string. Arrays of
2510 any type should be ok if the variable is of a C
2511 interoperable type. */
2512 if (arg_ts->type == BT_CHARACTER)
2513 if (arg_ts->u.cl != NULL
2514 && (arg_ts->u.cl->length == NULL
2515 || arg_ts->u.cl->length->expr_type
2516 != EXPR_CONSTANT
2517 || mpz_cmp_si
2518 (arg_ts->u.cl->length->value.integer, 1)
2519 != 0)
2520 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2522 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2523 "at %L must have a length of 1",
2524 args_sym->name, sym->name,
2525 &(args->expr->where));
2526 retval = FAILURE;
2530 else if (is_pointer
2531 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2533 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2534 scalar pointer. */
2535 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2536 "associated scalar POINTER", args_sym->name,
2537 sym->name, &(args->expr->where));
2538 retval = FAILURE;
2541 else
2543 /* The parameter is not required to be C interoperable. If it
2544 is not C interoperable, it must be a nonpolymorphic scalar
2545 with no length type parameters. It still must have either
2546 the pointer or target attribute, and it can be
2547 allocatable (but must be allocated when c_loc is called). */
2548 if (args->expr->rank != 0
2549 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2551 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2552 "scalar", args_sym->name, sym->name,
2553 &(args->expr->where));
2554 retval = FAILURE;
2556 else if (arg_ts->type == BT_CHARACTER
2557 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2559 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2560 "%L must have a length of 1",
2561 args_sym->name, sym->name,
2562 &(args->expr->where));
2563 retval = FAILURE;
2567 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2569 if (args_sym->attr.flavor != FL_PROCEDURE)
2571 /* TODO: Update this error message to allow for procedure
2572 pointers once they are implemented. */
2573 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2574 "procedure",
2575 args_sym->name, sym->name,
2576 &(args->expr->where));
2577 retval = FAILURE;
2579 else if (args_sym->attr.is_bind_c != 1)
2581 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2582 "BIND(C)",
2583 args_sym->name, sym->name,
2584 &(args->expr->where));
2585 retval = FAILURE;
2589 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2590 *new_sym = sym;
2592 else
2594 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2595 "iso_c_binding function: '%s'!\n", sym->name);
2598 return retval;
2602 /* Resolve a function call, which means resolving the arguments, then figuring
2603 out which entity the name refers to. */
2604 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2605 to INTENT(OUT) or INTENT(INOUT). */
2607 static gfc_try
2608 resolve_function (gfc_expr *expr)
2610 gfc_actual_arglist *arg;
2611 gfc_symbol *sym;
2612 const char *name;
2613 gfc_try t;
2614 int temp;
2615 procedure_type p = PROC_INTRINSIC;
2616 bool no_formal_args;
2618 sym = NULL;
2619 if (expr->symtree)
2620 sym = expr->symtree->n.sym;
2622 /* If this is a procedure pointer component, it has already been resolved. */
2623 if (gfc_is_proc_ptr_comp (expr, NULL))
2624 return SUCCESS;
2626 if (sym && sym->attr.intrinsic
2627 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2628 return FAILURE;
2630 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2632 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2633 return FAILURE;
2636 /* If this ia a deferred TBP with an abstract interface (which may
2637 of course be referenced), expr->value.function.esym will be set. */
2638 if (sym && sym->attr.abstract && !expr->value.function.esym)
2640 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2641 sym->name, &expr->where);
2642 return FAILURE;
2645 /* Switch off assumed size checking and do this again for certain kinds
2646 of procedure, once the procedure itself is resolved. */
2647 need_full_assumed_size++;
2649 if (expr->symtree && expr->symtree->n.sym)
2650 p = expr->symtree->n.sym->attr.proc;
2652 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2653 inquiry_argument = true;
2654 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2656 if (resolve_actual_arglist (expr->value.function.actual,
2657 p, no_formal_args) == FAILURE)
2659 inquiry_argument = false;
2660 return FAILURE;
2663 inquiry_argument = false;
2665 /* Need to setup the call to the correct c_associated, depending on
2666 the number of cptrs to user gives to compare. */
2667 if (sym && sym->attr.is_iso_c == 1)
2669 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2670 == FAILURE)
2671 return FAILURE;
2673 /* Get the symtree for the new symbol (resolved func).
2674 the old one will be freed later, when it's no longer used. */
2675 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2678 /* Resume assumed_size checking. */
2679 need_full_assumed_size--;
2681 /* If the procedure is external, check for usage. */
2682 if (sym && is_external_proc (sym))
2683 resolve_global_procedure (sym, &expr->where,
2684 &expr->value.function.actual, 0);
2686 if (sym && sym->ts.type == BT_CHARACTER
2687 && sym->ts.u.cl
2688 && sym->ts.u.cl->length == NULL
2689 && !sym->attr.dummy
2690 && expr->value.function.esym == NULL
2691 && !sym->attr.contained)
2693 /* Internal procedures are taken care of in resolve_contained_fntype. */
2694 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2695 "be used at %L since it is not a dummy argument",
2696 sym->name, &expr->where);
2697 return FAILURE;
2700 /* See if function is already resolved. */
2702 if (expr->value.function.name != NULL)
2704 if (expr->ts.type == BT_UNKNOWN)
2705 expr->ts = sym->ts;
2706 t = SUCCESS;
2708 else
2710 /* Apply the rules of section 14.1.2. */
2712 switch (procedure_kind (sym))
2714 case PTYPE_GENERIC:
2715 t = resolve_generic_f (expr);
2716 break;
2718 case PTYPE_SPECIFIC:
2719 t = resolve_specific_f (expr);
2720 break;
2722 case PTYPE_UNKNOWN:
2723 t = resolve_unknown_f (expr);
2724 break;
2726 default:
2727 gfc_internal_error ("resolve_function(): bad function type");
2731 /* If the expression is still a function (it might have simplified),
2732 then we check to see if we are calling an elemental function. */
2734 if (expr->expr_type != EXPR_FUNCTION)
2735 return t;
2737 temp = need_full_assumed_size;
2738 need_full_assumed_size = 0;
2740 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2741 return FAILURE;
2743 if (omp_workshare_flag
2744 && expr->value.function.esym
2745 && ! gfc_elemental (expr->value.function.esym))
2747 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2748 "in WORKSHARE construct", expr->value.function.esym->name,
2749 &expr->where);
2750 t = FAILURE;
2753 #define GENERIC_ID expr->value.function.isym->id
2754 else if (expr->value.function.actual != NULL
2755 && expr->value.function.isym != NULL
2756 && GENERIC_ID != GFC_ISYM_LBOUND
2757 && GENERIC_ID != GFC_ISYM_LEN
2758 && GENERIC_ID != GFC_ISYM_LOC
2759 && GENERIC_ID != GFC_ISYM_PRESENT)
2761 /* Array intrinsics must also have the last upper bound of an
2762 assumed size array argument. UBOUND and SIZE have to be
2763 excluded from the check if the second argument is anything
2764 than a constant. */
2766 for (arg = expr->value.function.actual; arg; arg = arg->next)
2768 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2769 && arg->next != NULL && arg->next->expr)
2771 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2772 break;
2774 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2775 break;
2777 if ((int)mpz_get_si (arg->next->expr->value.integer)
2778 < arg->expr->rank)
2779 break;
2782 if (arg->expr != NULL
2783 && arg->expr->rank > 0
2784 && resolve_assumed_size_actual (arg->expr))
2785 return FAILURE;
2788 #undef GENERIC_ID
2790 need_full_assumed_size = temp;
2791 name = NULL;
2793 if (!pure_function (expr, &name) && name)
2795 if (forall_flag)
2797 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2798 "FORALL %s", name, &expr->where,
2799 forall_flag == 2 ? "mask" : "block");
2800 t = FAILURE;
2802 else if (gfc_pure (NULL))
2804 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2805 "procedure within a PURE procedure", name, &expr->where);
2806 t = FAILURE;
2810 /* Functions without the RECURSIVE attribution are not allowed to
2811 * call themselves. */
2812 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2814 gfc_symbol *esym;
2815 esym = expr->value.function.esym;
2817 if (is_illegal_recursion (esym, gfc_current_ns))
2819 if (esym->attr.entry && esym->ns->entries)
2820 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2821 " function '%s' is not RECURSIVE",
2822 esym->name, &expr->where, esym->ns->entries->sym->name);
2823 else
2824 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2825 " is not RECURSIVE", esym->name, &expr->where);
2827 t = FAILURE;
2831 /* Character lengths of use associated functions may contains references to
2832 symbols not referenced from the current program unit otherwise. Make sure
2833 those symbols are marked as referenced. */
2835 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2836 && expr->value.function.esym->attr.use_assoc)
2838 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
2841 if (t == SUCCESS
2842 && !((expr->value.function.esym
2843 && expr->value.function.esym->attr.elemental)
2845 (expr->value.function.isym
2846 && expr->value.function.isym->elemental)))
2847 find_noncopying_intrinsics (expr->value.function.esym,
2848 expr->value.function.actual);
2850 /* Make sure that the expression has a typespec that works. */
2851 if (expr->ts.type == BT_UNKNOWN)
2853 if (expr->symtree->n.sym->result
2854 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
2855 && !expr->symtree->n.sym->result->attr.proc_pointer)
2856 expr->ts = expr->symtree->n.sym->result->ts;
2859 return t;
2863 /************* Subroutine resolution *************/
2865 static void
2866 pure_subroutine (gfc_code *c, gfc_symbol *sym)
2868 if (gfc_pure (sym))
2869 return;
2871 if (forall_flag)
2872 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2873 sym->name, &c->loc);
2874 else if (gfc_pure (NULL))
2875 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2876 &c->loc);
2880 static match
2881 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
2883 gfc_symbol *s;
2885 if (sym->attr.generic)
2887 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2888 if (s != NULL)
2890 c->resolved_sym = s;
2891 pure_subroutine (c, s);
2892 return MATCH_YES;
2895 /* TODO: Need to search for elemental references in generic interface. */
2898 if (sym->attr.intrinsic)
2899 return gfc_intrinsic_sub_interface (c, 0);
2901 return MATCH_NO;
2905 static gfc_try
2906 resolve_generic_s (gfc_code *c)
2908 gfc_symbol *sym;
2909 match m;
2911 sym = c->symtree->n.sym;
2913 for (;;)
2915 m = resolve_generic_s0 (c, sym);
2916 if (m == MATCH_YES)
2917 return SUCCESS;
2918 else if (m == MATCH_ERROR)
2919 return FAILURE;
2921 generic:
2922 if (sym->ns->parent == NULL)
2923 break;
2924 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2926 if (sym == NULL)
2927 break;
2928 if (!generic_sym (sym))
2929 goto generic;
2932 /* Last ditch attempt. See if the reference is to an intrinsic
2933 that possesses a matching interface. 14.1.2.4 */
2934 sym = c->symtree->n.sym;
2936 if (!gfc_is_intrinsic (sym, 1, c->loc))
2938 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2939 sym->name, &c->loc);
2940 return FAILURE;
2943 m = gfc_intrinsic_sub_interface (c, 0);
2944 if (m == MATCH_YES)
2945 return SUCCESS;
2946 if (m == MATCH_NO)
2947 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2948 "intrinsic subroutine interface", sym->name, &c->loc);
2950 return FAILURE;
2954 /* Set the name and binding label of the subroutine symbol in the call
2955 expression represented by 'c' to include the type and kind of the
2956 second parameter. This function is for resolving the appropriate
2957 version of c_f_pointer() and c_f_procpointer(). For example, a
2958 call to c_f_pointer() for a default integer pointer could have a
2959 name of c_f_pointer_i4. If no second arg exists, which is an error
2960 for these two functions, it defaults to the generic symbol's name
2961 and binding label. */
2963 static void
2964 set_name_and_label (gfc_code *c, gfc_symbol *sym,
2965 char *name, char *binding_label)
2967 gfc_expr *arg = NULL;
2968 char type;
2969 int kind;
2971 /* The second arg of c_f_pointer and c_f_procpointer determines
2972 the type and kind for the procedure name. */
2973 arg = c->ext.actual->next->expr;
2975 if (arg != NULL)
2977 /* Set up the name to have the given symbol's name,
2978 plus the type and kind. */
2979 /* a derived type is marked with the type letter 'u' */
2980 if (arg->ts.type == BT_DERIVED)
2982 type = 'd';
2983 kind = 0; /* set the kind as 0 for now */
2985 else
2987 type = gfc_type_letter (arg->ts.type);
2988 kind = arg->ts.kind;
2991 if (arg->ts.type == BT_CHARACTER)
2992 /* Kind info for character strings not needed. */
2993 kind = 0;
2995 sprintf (name, "%s_%c%d", sym->name, type, kind);
2996 /* Set up the binding label as the given symbol's label plus
2997 the type and kind. */
2998 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
3000 else
3002 /* If the second arg is missing, set the name and label as
3003 was, cause it should at least be found, and the missing
3004 arg error will be caught by compare_parameters(). */
3005 sprintf (name, "%s", sym->name);
3006 sprintf (binding_label, "%s", sym->binding_label);
3009 return;
3013 /* Resolve a generic version of the iso_c_binding procedure given
3014 (sym) to the specific one based on the type and kind of the
3015 argument(s). Currently, this function resolves c_f_pointer() and
3016 c_f_procpointer based on the type and kind of the second argument
3017 (FPTR). Other iso_c_binding procedures aren't specially handled.
3018 Upon successfully exiting, c->resolved_sym will hold the resolved
3019 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
3020 otherwise. */
3022 match
3023 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
3025 gfc_symbol *new_sym;
3026 /* this is fine, since we know the names won't use the max */
3027 char name[GFC_MAX_SYMBOL_LEN + 1];
3028 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
3029 /* default to success; will override if find error */
3030 match m = MATCH_YES;
3032 /* Make sure the actual arguments are in the necessary order (based on the
3033 formal args) before resolving. */
3034 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
3036 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3037 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3039 set_name_and_label (c, sym, name, binding_label);
3041 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3043 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
3045 /* Make sure we got a third arg if the second arg has non-zero
3046 rank. We must also check that the type and rank are
3047 correct since we short-circuit this check in
3048 gfc_procedure_use() (called above to sort actual args). */
3049 if (c->ext.actual->next->expr->rank != 0)
3051 if(c->ext.actual->next->next == NULL
3052 || c->ext.actual->next->next->expr == NULL)
3054 m = MATCH_ERROR;
3055 gfc_error ("Missing SHAPE parameter for call to %s "
3056 "at %L", sym->name, &(c->loc));
3058 else if (c->ext.actual->next->next->expr->ts.type
3059 != BT_INTEGER
3060 || c->ext.actual->next->next->expr->rank != 1)
3062 m = MATCH_ERROR;
3063 gfc_error ("SHAPE parameter for call to %s at %L must "
3064 "be a rank 1 INTEGER array", sym->name,
3065 &(c->loc));
3071 if (m != MATCH_ERROR)
3073 /* the 1 means to add the optional arg to formal list */
3074 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
3076 /* for error reporting, say it's declared where the original was */
3077 new_sym->declared_at = sym->declared_at;
3080 else
3082 /* no differences for c_loc or c_funloc */
3083 new_sym = sym;
3086 /* set the resolved symbol */
3087 if (m != MATCH_ERROR)
3088 c->resolved_sym = new_sym;
3089 else
3090 c->resolved_sym = sym;
3092 return m;
3096 /* Resolve a subroutine call known to be specific. */
3098 static match
3099 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3101 match m;
3103 if(sym->attr.is_iso_c)
3105 m = gfc_iso_c_sub_interface (c,sym);
3106 return m;
3109 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3111 if (sym->attr.dummy)
3113 sym->attr.proc = PROC_DUMMY;
3114 goto found;
3117 sym->attr.proc = PROC_EXTERNAL;
3118 goto found;
3121 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3122 goto found;
3124 if (sym->attr.intrinsic)
3126 m = gfc_intrinsic_sub_interface (c, 1);
3127 if (m == MATCH_YES)
3128 return MATCH_YES;
3129 if (m == MATCH_NO)
3130 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3131 "with an intrinsic", sym->name, &c->loc);
3133 return MATCH_ERROR;
3136 return MATCH_NO;
3138 found:
3139 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3141 c->resolved_sym = sym;
3142 pure_subroutine (c, sym);
3144 return MATCH_YES;
3148 static gfc_try
3149 resolve_specific_s (gfc_code *c)
3151 gfc_symbol *sym;
3152 match m;
3154 sym = c->symtree->n.sym;
3156 for (;;)
3158 m = resolve_specific_s0 (c, sym);
3159 if (m == MATCH_YES)
3160 return SUCCESS;
3161 if (m == MATCH_ERROR)
3162 return FAILURE;
3164 if (sym->ns->parent == NULL)
3165 break;
3167 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3169 if (sym == NULL)
3170 break;
3173 sym = c->symtree->n.sym;
3174 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3175 sym->name, &c->loc);
3177 return FAILURE;
3181 /* Resolve a subroutine call not known to be generic nor specific. */
3183 static gfc_try
3184 resolve_unknown_s (gfc_code *c)
3186 gfc_symbol *sym;
3188 sym = c->symtree->n.sym;
3190 if (sym->attr.dummy)
3192 sym->attr.proc = PROC_DUMMY;
3193 goto found;
3196 /* See if we have an intrinsic function reference. */
3198 if (gfc_is_intrinsic (sym, 1, c->loc))
3200 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3201 return SUCCESS;
3202 return FAILURE;
3205 /* The reference is to an external name. */
3207 found:
3208 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3210 c->resolved_sym = sym;
3212 pure_subroutine (c, sym);
3214 return SUCCESS;
3218 /* Resolve a subroutine call. Although it was tempting to use the same code
3219 for functions, subroutines and functions are stored differently and this
3220 makes things awkward. */
3222 static gfc_try
3223 resolve_call (gfc_code *c)
3225 gfc_try t;
3226 procedure_type ptype = PROC_INTRINSIC;
3227 gfc_symbol *csym, *sym;
3228 bool no_formal_args;
3230 csym = c->symtree ? c->symtree->n.sym : NULL;
3232 if (csym && csym->ts.type != BT_UNKNOWN)
3234 gfc_error ("'%s' at %L has a type, which is not consistent with "
3235 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3236 return FAILURE;
3239 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3241 gfc_symtree *st;
3242 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3243 sym = st ? st->n.sym : NULL;
3244 if (sym && csym != sym
3245 && sym->ns == gfc_current_ns
3246 && sym->attr.flavor == FL_PROCEDURE
3247 && sym->attr.contained)
3249 sym->refs++;
3250 if (csym->attr.generic)
3251 c->symtree->n.sym = sym;
3252 else
3253 c->symtree = st;
3254 csym = c->symtree->n.sym;
3258 /* If this ia a deferred TBP with an abstract interface
3259 (which may of course be referenced), c->expr1 will be set. */
3260 if (csym && csym->attr.abstract && !c->expr1)
3262 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3263 csym->name, &c->loc);
3264 return FAILURE;
3267 /* Subroutines without the RECURSIVE attribution are not allowed to
3268 * call themselves. */
3269 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3271 if (csym->attr.entry && csym->ns->entries)
3272 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3273 " subroutine '%s' is not RECURSIVE",
3274 csym->name, &c->loc, csym->ns->entries->sym->name);
3275 else
3276 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3277 " is not RECURSIVE", csym->name, &c->loc);
3279 t = FAILURE;
3282 /* Switch off assumed size checking and do this again for certain kinds
3283 of procedure, once the procedure itself is resolved. */
3284 need_full_assumed_size++;
3286 if (csym)
3287 ptype = csym->attr.proc;
3289 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3290 if (resolve_actual_arglist (c->ext.actual, ptype,
3291 no_formal_args) == FAILURE)
3292 return FAILURE;
3294 /* Resume assumed_size checking. */
3295 need_full_assumed_size--;
3297 /* If external, check for usage. */
3298 if (csym && is_external_proc (csym))
3299 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3301 t = SUCCESS;
3302 if (c->resolved_sym == NULL)
3304 c->resolved_isym = NULL;
3305 switch (procedure_kind (csym))
3307 case PTYPE_GENERIC:
3308 t = resolve_generic_s (c);
3309 break;
3311 case PTYPE_SPECIFIC:
3312 t = resolve_specific_s (c);
3313 break;
3315 case PTYPE_UNKNOWN:
3316 t = resolve_unknown_s (c);
3317 break;
3319 default:
3320 gfc_internal_error ("resolve_subroutine(): bad function type");
3324 /* Some checks of elemental subroutine actual arguments. */
3325 if (resolve_elemental_actual (NULL, c) == FAILURE)
3326 return FAILURE;
3328 if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
3329 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
3330 return t;
3334 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3335 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3336 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3337 if their shapes do not match. If either op1->shape or op2->shape is
3338 NULL, return SUCCESS. */
3340 static gfc_try
3341 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3343 gfc_try t;
3344 int i;
3346 t = SUCCESS;
3348 if (op1->shape != NULL && op2->shape != NULL)
3350 for (i = 0; i < op1->rank; i++)
3352 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3354 gfc_error ("Shapes for operands at %L and %L are not conformable",
3355 &op1->where, &op2->where);
3356 t = FAILURE;
3357 break;
3362 return t;
3366 /* Resolve an operator expression node. This can involve replacing the
3367 operation with a user defined function call. */
3369 static gfc_try
3370 resolve_operator (gfc_expr *e)
3372 gfc_expr *op1, *op2;
3373 char msg[200];
3374 bool dual_locus_error;
3375 gfc_try t;
3377 /* Resolve all subnodes-- give them types. */
3379 switch (e->value.op.op)
3381 default:
3382 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3383 return FAILURE;
3385 /* Fall through... */
3387 case INTRINSIC_NOT:
3388 case INTRINSIC_UPLUS:
3389 case INTRINSIC_UMINUS:
3390 case INTRINSIC_PARENTHESES:
3391 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3392 return FAILURE;
3393 break;
3396 /* Typecheck the new node. */
3398 op1 = e->value.op.op1;
3399 op2 = e->value.op.op2;
3400 dual_locus_error = false;
3402 if ((op1 && op1->expr_type == EXPR_NULL)
3403 || (op2 && op2->expr_type == EXPR_NULL))
3405 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3406 goto bad_op;
3409 switch (e->value.op.op)
3411 case INTRINSIC_UPLUS:
3412 case INTRINSIC_UMINUS:
3413 if (op1->ts.type == BT_INTEGER
3414 || op1->ts.type == BT_REAL
3415 || op1->ts.type == BT_COMPLEX)
3417 e->ts = op1->ts;
3418 break;
3421 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3422 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3423 goto bad_op;
3425 case INTRINSIC_PLUS:
3426 case INTRINSIC_MINUS:
3427 case INTRINSIC_TIMES:
3428 case INTRINSIC_DIVIDE:
3429 case INTRINSIC_POWER:
3430 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3432 gfc_type_convert_binary (e, 1);
3433 break;
3436 sprintf (msg,
3437 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3438 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3439 gfc_typename (&op2->ts));
3440 goto bad_op;
3442 case INTRINSIC_CONCAT:
3443 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3444 && op1->ts.kind == op2->ts.kind)
3446 e->ts.type = BT_CHARACTER;
3447 e->ts.kind = op1->ts.kind;
3448 break;
3451 sprintf (msg,
3452 _("Operands of string concatenation operator at %%L are %s/%s"),
3453 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3454 goto bad_op;
3456 case INTRINSIC_AND:
3457 case INTRINSIC_OR:
3458 case INTRINSIC_EQV:
3459 case INTRINSIC_NEQV:
3460 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3462 e->ts.type = BT_LOGICAL;
3463 e->ts.kind = gfc_kind_max (op1, op2);
3464 if (op1->ts.kind < e->ts.kind)
3465 gfc_convert_type (op1, &e->ts, 2);
3466 else if (op2->ts.kind < e->ts.kind)
3467 gfc_convert_type (op2, &e->ts, 2);
3468 break;
3471 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3472 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3473 gfc_typename (&op2->ts));
3475 goto bad_op;
3477 case INTRINSIC_NOT:
3478 if (op1->ts.type == BT_LOGICAL)
3480 e->ts.type = BT_LOGICAL;
3481 e->ts.kind = op1->ts.kind;
3482 break;
3485 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3486 gfc_typename (&op1->ts));
3487 goto bad_op;
3489 case INTRINSIC_GT:
3490 case INTRINSIC_GT_OS:
3491 case INTRINSIC_GE:
3492 case INTRINSIC_GE_OS:
3493 case INTRINSIC_LT:
3494 case INTRINSIC_LT_OS:
3495 case INTRINSIC_LE:
3496 case INTRINSIC_LE_OS:
3497 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3499 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3500 goto bad_op;
3503 /* Fall through... */
3505 case INTRINSIC_EQ:
3506 case INTRINSIC_EQ_OS:
3507 case INTRINSIC_NE:
3508 case INTRINSIC_NE_OS:
3509 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3510 && op1->ts.kind == op2->ts.kind)
3512 e->ts.type = BT_LOGICAL;
3513 e->ts.kind = gfc_default_logical_kind;
3514 break;
3517 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3519 gfc_type_convert_binary (e, 1);
3521 e->ts.type = BT_LOGICAL;
3522 e->ts.kind = gfc_default_logical_kind;
3523 break;
3526 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3527 sprintf (msg,
3528 _("Logicals at %%L must be compared with %s instead of %s"),
3529 (e->value.op.op == INTRINSIC_EQ
3530 || e->value.op.op == INTRINSIC_EQ_OS)
3531 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3532 else
3533 sprintf (msg,
3534 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3535 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3536 gfc_typename (&op2->ts));
3538 goto bad_op;
3540 case INTRINSIC_USER:
3541 if (e->value.op.uop->op == NULL)
3542 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3543 else if (op2 == NULL)
3544 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3545 e->value.op.uop->name, gfc_typename (&op1->ts));
3546 else
3547 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3548 e->value.op.uop->name, gfc_typename (&op1->ts),
3549 gfc_typename (&op2->ts));
3551 goto bad_op;
3553 case INTRINSIC_PARENTHESES:
3554 e->ts = op1->ts;
3555 if (e->ts.type == BT_CHARACTER)
3556 e->ts.u.cl = op1->ts.u.cl;
3557 break;
3559 default:
3560 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3563 /* Deal with arrayness of an operand through an operator. */
3565 t = SUCCESS;
3567 switch (e->value.op.op)
3569 case INTRINSIC_PLUS:
3570 case INTRINSIC_MINUS:
3571 case INTRINSIC_TIMES:
3572 case INTRINSIC_DIVIDE:
3573 case INTRINSIC_POWER:
3574 case INTRINSIC_CONCAT:
3575 case INTRINSIC_AND:
3576 case INTRINSIC_OR:
3577 case INTRINSIC_EQV:
3578 case INTRINSIC_NEQV:
3579 case INTRINSIC_EQ:
3580 case INTRINSIC_EQ_OS:
3581 case INTRINSIC_NE:
3582 case INTRINSIC_NE_OS:
3583 case INTRINSIC_GT:
3584 case INTRINSIC_GT_OS:
3585 case INTRINSIC_GE:
3586 case INTRINSIC_GE_OS:
3587 case INTRINSIC_LT:
3588 case INTRINSIC_LT_OS:
3589 case INTRINSIC_LE:
3590 case INTRINSIC_LE_OS:
3592 if (op1->rank == 0 && op2->rank == 0)
3593 e->rank = 0;
3595 if (op1->rank == 0 && op2->rank != 0)
3597 e->rank = op2->rank;
3599 if (e->shape == NULL)
3600 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3603 if (op1->rank != 0 && op2->rank == 0)
3605 e->rank = op1->rank;
3607 if (e->shape == NULL)
3608 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3611 if (op1->rank != 0 && op2->rank != 0)
3613 if (op1->rank == op2->rank)
3615 e->rank = op1->rank;
3616 if (e->shape == NULL)
3618 t = compare_shapes(op1, op2);
3619 if (t == FAILURE)
3620 e->shape = NULL;
3621 else
3622 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3625 else
3627 /* Allow higher level expressions to work. */
3628 e->rank = 0;
3630 /* Try user-defined operators, and otherwise throw an error. */
3631 dual_locus_error = true;
3632 sprintf (msg,
3633 _("Inconsistent ranks for operator at %%L and %%L"));
3634 goto bad_op;
3638 break;
3640 case INTRINSIC_PARENTHESES:
3641 case INTRINSIC_NOT:
3642 case INTRINSIC_UPLUS:
3643 case INTRINSIC_UMINUS:
3644 /* Simply copy arrayness attribute */
3645 e->rank = op1->rank;
3647 if (e->shape == NULL)
3648 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3650 break;
3652 default:
3653 break;
3656 /* Attempt to simplify the expression. */
3657 if (t == SUCCESS)
3659 t = gfc_simplify_expr (e, 0);
3660 /* Some calls do not succeed in simplification and return FAILURE
3661 even though there is no error; e.g. variable references to
3662 PARAMETER arrays. */
3663 if (!gfc_is_constant_expr (e))
3664 t = SUCCESS;
3666 return t;
3668 bad_op:
3671 bool real_error;
3672 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3673 return SUCCESS;
3675 if (real_error)
3676 return FAILURE;
3679 if (dual_locus_error)
3680 gfc_error (msg, &op1->where, &op2->where);
3681 else
3682 gfc_error (msg, &e->where);
3684 return FAILURE;
3688 /************** Array resolution subroutines **************/
3690 typedef enum
3691 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3692 comparison;
3694 /* Compare two integer expressions. */
3696 static comparison
3697 compare_bound (gfc_expr *a, gfc_expr *b)
3699 int i;
3701 if (a == NULL || a->expr_type != EXPR_CONSTANT
3702 || b == NULL || b->expr_type != EXPR_CONSTANT)
3703 return CMP_UNKNOWN;
3705 /* If either of the types isn't INTEGER, we must have
3706 raised an error earlier. */
3708 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3709 return CMP_UNKNOWN;
3711 i = mpz_cmp (a->value.integer, b->value.integer);
3713 if (i < 0)
3714 return CMP_LT;
3715 if (i > 0)
3716 return CMP_GT;
3717 return CMP_EQ;
3721 /* Compare an integer expression with an integer. */
3723 static comparison
3724 compare_bound_int (gfc_expr *a, int b)
3726 int i;
3728 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3729 return CMP_UNKNOWN;
3731 if (a->ts.type != BT_INTEGER)
3732 gfc_internal_error ("compare_bound_int(): Bad expression");
3734 i = mpz_cmp_si (a->value.integer, b);
3736 if (i < 0)
3737 return CMP_LT;
3738 if (i > 0)
3739 return CMP_GT;
3740 return CMP_EQ;
3744 /* Compare an integer expression with a mpz_t. */
3746 static comparison
3747 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3749 int i;
3751 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3752 return CMP_UNKNOWN;
3754 if (a->ts.type != BT_INTEGER)
3755 gfc_internal_error ("compare_bound_int(): Bad expression");
3757 i = mpz_cmp (a->value.integer, b);
3759 if (i < 0)
3760 return CMP_LT;
3761 if (i > 0)
3762 return CMP_GT;
3763 return CMP_EQ;
3767 /* Compute the last value of a sequence given by a triplet.
3768 Return 0 if it wasn't able to compute the last value, or if the
3769 sequence if empty, and 1 otherwise. */
3771 static int
3772 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3773 gfc_expr *stride, mpz_t last)
3775 mpz_t rem;
3777 if (start == NULL || start->expr_type != EXPR_CONSTANT
3778 || end == NULL || end->expr_type != EXPR_CONSTANT
3779 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3780 return 0;
3782 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3783 || (stride != NULL && stride->ts.type != BT_INTEGER))
3784 return 0;
3786 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3788 if (compare_bound (start, end) == CMP_GT)
3789 return 0;
3790 mpz_set (last, end->value.integer);
3791 return 1;
3794 if (compare_bound_int (stride, 0) == CMP_GT)
3796 /* Stride is positive */
3797 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3798 return 0;
3800 else
3802 /* Stride is negative */
3803 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3804 return 0;
3807 mpz_init (rem);
3808 mpz_sub (rem, end->value.integer, start->value.integer);
3809 mpz_tdiv_r (rem, rem, stride->value.integer);
3810 mpz_sub (last, end->value.integer, rem);
3811 mpz_clear (rem);
3813 return 1;
3817 /* Compare a single dimension of an array reference to the array
3818 specification. */
3820 static gfc_try
3821 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3823 mpz_t last_value;
3825 if (ar->dimen_type[i] == DIMEN_STAR)
3827 gcc_assert (ar->stride[i] == NULL);
3828 /* This implies [*] as [*:] and [*:3] are not possible. */
3829 if (ar->start[i] == NULL)
3831 gcc_assert (ar->end[i] == NULL);
3832 return SUCCESS;
3836 /* Given start, end and stride values, calculate the minimum and
3837 maximum referenced indexes. */
3839 switch (ar->dimen_type[i])
3841 case DIMEN_VECTOR:
3842 break;
3844 case DIMEN_STAR:
3845 case DIMEN_ELEMENT:
3846 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3848 if (i < as->rank)
3849 gfc_warning ("Array reference at %L is out of bounds "
3850 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3851 mpz_get_si (ar->start[i]->value.integer),
3852 mpz_get_si (as->lower[i]->value.integer), i+1);
3853 else
3854 gfc_warning ("Array reference at %L is out of bounds "
3855 "(%ld < %ld) in codimension %d", &ar->c_where[i],
3856 mpz_get_si (ar->start[i]->value.integer),
3857 mpz_get_si (as->lower[i]->value.integer),
3858 i + 1 - as->rank);
3859 return SUCCESS;
3861 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3863 if (i < as->rank)
3864 gfc_warning ("Array reference at %L is out of bounds "
3865 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3866 mpz_get_si (ar->start[i]->value.integer),
3867 mpz_get_si (as->upper[i]->value.integer), i+1);
3868 else
3869 gfc_warning ("Array reference at %L is out of bounds "
3870 "(%ld > %ld) in codimension %d", &ar->c_where[i],
3871 mpz_get_si (ar->start[i]->value.integer),
3872 mpz_get_si (as->upper[i]->value.integer),
3873 i + 1 - as->rank);
3874 return SUCCESS;
3877 break;
3879 case DIMEN_RANGE:
3881 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3882 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3884 comparison comp_start_end = compare_bound (AR_START, AR_END);
3886 /* Check for zero stride, which is not allowed. */
3887 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3889 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3890 return FAILURE;
3893 /* if start == len || (stride > 0 && start < len)
3894 || (stride < 0 && start > len),
3895 then the array section contains at least one element. In this
3896 case, there is an out-of-bounds access if
3897 (start < lower || start > upper). */
3898 if (compare_bound (AR_START, AR_END) == CMP_EQ
3899 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3900 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3901 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3902 && comp_start_end == CMP_GT))
3904 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3906 gfc_warning ("Lower array reference at %L is out of bounds "
3907 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3908 mpz_get_si (AR_START->value.integer),
3909 mpz_get_si (as->lower[i]->value.integer), i+1);
3910 return SUCCESS;
3912 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3914 gfc_warning ("Lower array reference at %L is out of bounds "
3915 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3916 mpz_get_si (AR_START->value.integer),
3917 mpz_get_si (as->upper[i]->value.integer), i+1);
3918 return SUCCESS;
3922 /* If we can compute the highest index of the array section,
3923 then it also has to be between lower and upper. */
3924 mpz_init (last_value);
3925 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3926 last_value))
3928 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3930 gfc_warning ("Upper array reference at %L is out of bounds "
3931 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3932 mpz_get_si (last_value),
3933 mpz_get_si (as->lower[i]->value.integer), i+1);
3934 mpz_clear (last_value);
3935 return SUCCESS;
3937 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
3939 gfc_warning ("Upper array reference at %L is out of bounds "
3940 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3941 mpz_get_si (last_value),
3942 mpz_get_si (as->upper[i]->value.integer), i+1);
3943 mpz_clear (last_value);
3944 return SUCCESS;
3947 mpz_clear (last_value);
3949 #undef AR_START
3950 #undef AR_END
3952 break;
3954 default:
3955 gfc_internal_error ("check_dimension(): Bad array reference");
3958 return SUCCESS;
3962 /* Compare an array reference with an array specification. */
3964 static gfc_try
3965 compare_spec_to_ref (gfc_array_ref *ar)
3967 gfc_array_spec *as;
3968 int i;
3970 as = ar->as;
3971 i = as->rank - 1;
3972 /* TODO: Full array sections are only allowed as actual parameters. */
3973 if (as->type == AS_ASSUMED_SIZE
3974 && (/*ar->type == AR_FULL
3975 ||*/ (ar->type == AR_SECTION
3976 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
3978 gfc_error ("Rightmost upper bound of assumed size array section "
3979 "not specified at %L", &ar->where);
3980 return FAILURE;
3983 if (ar->type == AR_FULL)
3984 return SUCCESS;
3986 if (as->rank != ar->dimen)
3988 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3989 &ar->where, ar->dimen, as->rank);
3990 return FAILURE;
3993 /* ar->codimen == 0 is a local array. */
3994 if (as->corank != ar->codimen && ar->codimen != 0)
3996 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
3997 &ar->where, ar->codimen, as->corank);
3998 return FAILURE;
4001 for (i = 0; i < as->rank; i++)
4002 if (check_dimension (i, ar, as) == FAILURE)
4003 return FAILURE;
4005 /* Local access has no coarray spec. */
4006 if (ar->codimen != 0)
4007 for (i = as->rank; i < as->rank + as->corank; i++)
4009 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate)
4011 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4012 i + 1 - as->rank, &ar->where);
4013 return FAILURE;
4015 if (check_dimension (i, ar, as) == FAILURE)
4016 return FAILURE;
4019 return SUCCESS;
4023 /* Resolve one part of an array index. */
4025 static gfc_try
4026 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4027 int force_index_integer_kind)
4029 gfc_typespec ts;
4031 if (index == NULL)
4032 return SUCCESS;
4034 if (gfc_resolve_expr (index) == FAILURE)
4035 return FAILURE;
4037 if (check_scalar && index->rank != 0)
4039 gfc_error ("Array index at %L must be scalar", &index->where);
4040 return FAILURE;
4043 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4045 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4046 &index->where, gfc_basic_typename (index->ts.type));
4047 return FAILURE;
4050 if (index->ts.type == BT_REAL)
4051 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
4052 &index->where) == FAILURE)
4053 return FAILURE;
4055 if ((index->ts.kind != gfc_index_integer_kind
4056 && force_index_integer_kind)
4057 || index->ts.type != BT_INTEGER)
4059 gfc_clear_ts (&ts);
4060 ts.type = BT_INTEGER;
4061 ts.kind = gfc_index_integer_kind;
4063 gfc_convert_type_warn (index, &ts, 2, 0);
4066 return SUCCESS;
4069 /* Resolve one part of an array index. */
4071 gfc_try
4072 gfc_resolve_index (gfc_expr *index, int check_scalar)
4074 return gfc_resolve_index_1 (index, check_scalar, 1);
4077 /* Resolve a dim argument to an intrinsic function. */
4079 gfc_try
4080 gfc_resolve_dim_arg (gfc_expr *dim)
4082 if (dim == NULL)
4083 return SUCCESS;
4085 if (gfc_resolve_expr (dim) == FAILURE)
4086 return FAILURE;
4088 if (dim->rank != 0)
4090 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4091 return FAILURE;
4095 if (dim->ts.type != BT_INTEGER)
4097 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4098 return FAILURE;
4101 if (dim->ts.kind != gfc_index_integer_kind)
4103 gfc_typespec ts;
4105 gfc_clear_ts (&ts);
4106 ts.type = BT_INTEGER;
4107 ts.kind = gfc_index_integer_kind;
4109 gfc_convert_type_warn (dim, &ts, 2, 0);
4112 return SUCCESS;
4115 /* Given an expression that contains array references, update those array
4116 references to point to the right array specifications. While this is
4117 filled in during matching, this information is difficult to save and load
4118 in a module, so we take care of it here.
4120 The idea here is that the original array reference comes from the
4121 base symbol. We traverse the list of reference structures, setting
4122 the stored reference to references. Component references can
4123 provide an additional array specification. */
4125 static void
4126 find_array_spec (gfc_expr *e)
4128 gfc_array_spec *as;
4129 gfc_component *c;
4130 gfc_symbol *derived;
4131 gfc_ref *ref;
4133 if (e->symtree->n.sym->ts.type == BT_CLASS)
4134 as = CLASS_DATA (e->symtree->n.sym)->as;
4135 else
4136 as = e->symtree->n.sym->as;
4137 derived = NULL;
4139 for (ref = e->ref; ref; ref = ref->next)
4140 switch (ref->type)
4142 case REF_ARRAY:
4143 if (as == NULL)
4144 gfc_internal_error ("find_array_spec(): Missing spec");
4146 ref->u.ar.as = as;
4147 as = NULL;
4148 break;
4150 case REF_COMPONENT:
4151 if (derived == NULL)
4152 derived = e->symtree->n.sym->ts.u.derived;
4154 if (derived->attr.is_class)
4155 derived = derived->components->ts.u.derived;
4157 c = derived->components;
4159 for (; c; c = c->next)
4160 if (c == ref->u.c.component)
4162 /* Track the sequence of component references. */
4163 if (c->ts.type == BT_DERIVED)
4164 derived = c->ts.u.derived;
4165 break;
4168 if (c == NULL)
4169 gfc_internal_error ("find_array_spec(): Component not found");
4171 if (c->attr.dimension)
4173 if (as != NULL)
4174 gfc_internal_error ("find_array_spec(): unused as(1)");
4175 as = c->as;
4178 break;
4180 case REF_SUBSTRING:
4181 break;
4184 if (as != NULL)
4185 gfc_internal_error ("find_array_spec(): unused as(2)");
4189 /* Resolve an array reference. */
4191 static gfc_try
4192 resolve_array_ref (gfc_array_ref *ar)
4194 int i, check_scalar;
4195 gfc_expr *e;
4197 for (i = 0; i < ar->dimen + ar->codimen; i++)
4199 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4201 /* Do not force gfc_index_integer_kind for the start. We can
4202 do fine with any integer kind. This avoids temporary arrays
4203 created for indexing with a vector. */
4204 if (gfc_resolve_index_1 (ar->start[i], check_scalar, 0) == FAILURE)
4205 return FAILURE;
4206 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4207 return FAILURE;
4208 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4209 return FAILURE;
4211 e = ar->start[i];
4213 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4214 switch (e->rank)
4216 case 0:
4217 ar->dimen_type[i] = DIMEN_ELEMENT;
4218 break;
4220 case 1:
4221 ar->dimen_type[i] = DIMEN_VECTOR;
4222 if (e->expr_type == EXPR_VARIABLE
4223 && e->symtree->n.sym->ts.type == BT_DERIVED)
4224 ar->start[i] = gfc_get_parentheses (e);
4225 break;
4227 default:
4228 gfc_error ("Array index at %L is an array of rank %d",
4229 &ar->c_where[i], e->rank);
4230 return FAILURE;
4234 if (ar->type == AR_FULL && ar->as->rank == 0)
4235 ar->type = AR_ELEMENT;
4237 /* If the reference type is unknown, figure out what kind it is. */
4239 if (ar->type == AR_UNKNOWN)
4241 ar->type = AR_ELEMENT;
4242 for (i = 0; i < ar->dimen; i++)
4243 if (ar->dimen_type[i] == DIMEN_RANGE
4244 || ar->dimen_type[i] == DIMEN_VECTOR)
4246 ar->type = AR_SECTION;
4247 break;
4251 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4252 return FAILURE;
4254 return SUCCESS;
4258 static gfc_try
4259 resolve_substring (gfc_ref *ref)
4261 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4263 if (ref->u.ss.start != NULL)
4265 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4266 return FAILURE;
4268 if (ref->u.ss.start->ts.type != BT_INTEGER)
4270 gfc_error ("Substring start index at %L must be of type INTEGER",
4271 &ref->u.ss.start->where);
4272 return FAILURE;
4275 if (ref->u.ss.start->rank != 0)
4277 gfc_error ("Substring start index at %L must be scalar",
4278 &ref->u.ss.start->where);
4279 return FAILURE;
4282 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4283 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4284 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4286 gfc_error ("Substring start index at %L is less than one",
4287 &ref->u.ss.start->where);
4288 return FAILURE;
4292 if (ref->u.ss.end != NULL)
4294 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4295 return FAILURE;
4297 if (ref->u.ss.end->ts.type != BT_INTEGER)
4299 gfc_error ("Substring end index at %L must be of type INTEGER",
4300 &ref->u.ss.end->where);
4301 return FAILURE;
4304 if (ref->u.ss.end->rank != 0)
4306 gfc_error ("Substring end index at %L must be scalar",
4307 &ref->u.ss.end->where);
4308 return FAILURE;
4311 if (ref->u.ss.length != NULL
4312 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4313 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4314 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4316 gfc_error ("Substring end index at %L exceeds the string length",
4317 &ref->u.ss.start->where);
4318 return FAILURE;
4321 if (compare_bound_mpz_t (ref->u.ss.end,
4322 gfc_integer_kinds[k].huge) == CMP_GT
4323 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4324 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4326 gfc_error ("Substring end index at %L is too large",
4327 &ref->u.ss.end->where);
4328 return FAILURE;
4332 return SUCCESS;
4336 /* This function supplies missing substring charlens. */
4338 void
4339 gfc_resolve_substring_charlen (gfc_expr *e)
4341 gfc_ref *char_ref;
4342 gfc_expr *start, *end;
4344 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4345 if (char_ref->type == REF_SUBSTRING)
4346 break;
4348 if (!char_ref)
4349 return;
4351 gcc_assert (char_ref->next == NULL);
4353 if (e->ts.u.cl)
4355 if (e->ts.u.cl->length)
4356 gfc_free_expr (e->ts.u.cl->length);
4357 else if (e->expr_type == EXPR_VARIABLE
4358 && e->symtree->n.sym->attr.dummy)
4359 return;
4362 e->ts.type = BT_CHARACTER;
4363 e->ts.kind = gfc_default_character_kind;
4365 if (!e->ts.u.cl)
4366 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4368 if (char_ref->u.ss.start)
4369 start = gfc_copy_expr (char_ref->u.ss.start);
4370 else
4371 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4373 if (char_ref->u.ss.end)
4374 end = gfc_copy_expr (char_ref->u.ss.end);
4375 else if (e->expr_type == EXPR_VARIABLE)
4376 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4377 else
4378 end = NULL;
4380 if (!start || !end)
4381 return;
4383 /* Length = (end - start +1). */
4384 e->ts.u.cl->length = gfc_subtract (end, start);
4385 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4386 gfc_get_int_expr (gfc_default_integer_kind,
4387 NULL, 1));
4389 e->ts.u.cl->length->ts.type = BT_INTEGER;
4390 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4392 /* Make sure that the length is simplified. */
4393 gfc_simplify_expr (e->ts.u.cl->length, 1);
4394 gfc_resolve_expr (e->ts.u.cl->length);
4398 /* Resolve subtype references. */
4400 static gfc_try
4401 resolve_ref (gfc_expr *expr)
4403 int current_part_dimension, n_components, seen_part_dimension;
4404 gfc_ref *ref;
4406 for (ref = expr->ref; ref; ref = ref->next)
4407 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4409 find_array_spec (expr);
4410 break;
4413 for (ref = expr->ref; ref; ref = ref->next)
4414 switch (ref->type)
4416 case REF_ARRAY:
4417 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4418 return FAILURE;
4419 break;
4421 case REF_COMPONENT:
4422 break;
4424 case REF_SUBSTRING:
4425 resolve_substring (ref);
4426 break;
4429 /* Check constraints on part references. */
4431 current_part_dimension = 0;
4432 seen_part_dimension = 0;
4433 n_components = 0;
4435 for (ref = expr->ref; ref; ref = ref->next)
4437 switch (ref->type)
4439 case REF_ARRAY:
4440 switch (ref->u.ar.type)
4442 case AR_FULL:
4443 /* Coarray scalar. */
4444 if (ref->u.ar.as->rank == 0)
4446 current_part_dimension = 0;
4447 break;
4449 /* Fall through. */
4450 case AR_SECTION:
4451 current_part_dimension = 1;
4452 break;
4454 case AR_ELEMENT:
4455 current_part_dimension = 0;
4456 break;
4458 case AR_UNKNOWN:
4459 gfc_internal_error ("resolve_ref(): Bad array reference");
4462 break;
4464 case REF_COMPONENT:
4465 if (current_part_dimension || seen_part_dimension)
4467 /* F03:C614. */
4468 if (ref->u.c.component->attr.pointer
4469 || ref->u.c.component->attr.proc_pointer)
4471 gfc_error ("Component to the right of a part reference "
4472 "with nonzero rank must not have the POINTER "
4473 "attribute at %L", &expr->where);
4474 return FAILURE;
4476 else if (ref->u.c.component->attr.allocatable)
4478 gfc_error ("Component to the right of a part reference "
4479 "with nonzero rank must not have the ALLOCATABLE "
4480 "attribute at %L", &expr->where);
4481 return FAILURE;
4485 n_components++;
4486 break;
4488 case REF_SUBSTRING:
4489 break;
4492 if (((ref->type == REF_COMPONENT && n_components > 1)
4493 || ref->next == NULL)
4494 && current_part_dimension
4495 && seen_part_dimension)
4497 gfc_error ("Two or more part references with nonzero rank must "
4498 "not be specified at %L", &expr->where);
4499 return FAILURE;
4502 if (ref->type == REF_COMPONENT)
4504 if (current_part_dimension)
4505 seen_part_dimension = 1;
4507 /* reset to make sure */
4508 current_part_dimension = 0;
4512 return SUCCESS;
4516 /* Given an expression, determine its shape. This is easier than it sounds.
4517 Leaves the shape array NULL if it is not possible to determine the shape. */
4519 static void
4520 expression_shape (gfc_expr *e)
4522 mpz_t array[GFC_MAX_DIMENSIONS];
4523 int i;
4525 if (e->rank == 0 || e->shape != NULL)
4526 return;
4528 for (i = 0; i < e->rank; i++)
4529 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4530 goto fail;
4532 e->shape = gfc_get_shape (e->rank);
4534 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4536 return;
4538 fail:
4539 for (i--; i >= 0; i--)
4540 mpz_clear (array[i]);
4544 /* Given a variable expression node, compute the rank of the expression by
4545 examining the base symbol and any reference structures it may have. */
4547 static void
4548 expression_rank (gfc_expr *e)
4550 gfc_ref *ref;
4551 int i, rank;
4553 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4554 could lead to serious confusion... */
4555 gcc_assert (e->expr_type != EXPR_COMPCALL);
4557 if (e->ref == NULL)
4559 if (e->expr_type == EXPR_ARRAY)
4560 goto done;
4561 /* Constructors can have a rank different from one via RESHAPE(). */
4563 if (e->symtree == NULL)
4565 e->rank = 0;
4566 goto done;
4569 e->rank = (e->symtree->n.sym->as == NULL)
4570 ? 0 : e->symtree->n.sym->as->rank;
4571 goto done;
4574 rank = 0;
4576 for (ref = e->ref; ref; ref = ref->next)
4578 if (ref->type != REF_ARRAY)
4579 continue;
4581 if (ref->u.ar.type == AR_FULL)
4583 rank = ref->u.ar.as->rank;
4584 break;
4587 if (ref->u.ar.type == AR_SECTION)
4589 /* Figure out the rank of the section. */
4590 if (rank != 0)
4591 gfc_internal_error ("expression_rank(): Two array specs");
4593 for (i = 0; i < ref->u.ar.dimen; i++)
4594 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4595 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4596 rank++;
4598 break;
4602 e->rank = rank;
4604 done:
4605 expression_shape (e);
4609 /* Resolve a variable expression. */
4611 static gfc_try
4612 resolve_variable (gfc_expr *e)
4614 gfc_symbol *sym;
4615 gfc_try t;
4617 t = SUCCESS;
4619 if (e->symtree == NULL)
4620 return FAILURE;
4622 if (e->ref && resolve_ref (e) == FAILURE)
4623 return FAILURE;
4625 sym = e->symtree->n.sym;
4626 if (sym->attr.flavor == FL_PROCEDURE
4627 && (!sym->attr.function
4628 || (sym->attr.function && sym->result
4629 && sym->result->attr.proc_pointer
4630 && !sym->result->attr.function)))
4632 e->ts.type = BT_PROCEDURE;
4633 goto resolve_procedure;
4636 if (sym->ts.type != BT_UNKNOWN)
4637 gfc_variable_attr (e, &e->ts);
4638 else
4640 /* Must be a simple variable reference. */
4641 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4642 return FAILURE;
4643 e->ts = sym->ts;
4646 if (check_assumed_size_reference (sym, e))
4647 return FAILURE;
4649 /* Deal with forward references to entries during resolve_code, to
4650 satisfy, at least partially, 12.5.2.5. */
4651 if (gfc_current_ns->entries
4652 && current_entry_id == sym->entry_id
4653 && cs_base
4654 && cs_base->current
4655 && cs_base->current->op != EXEC_ENTRY)
4657 gfc_entry_list *entry;
4658 gfc_formal_arglist *formal;
4659 int n;
4660 bool seen;
4662 /* If the symbol is a dummy... */
4663 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4665 entry = gfc_current_ns->entries;
4666 seen = false;
4668 /* ...test if the symbol is a parameter of previous entries. */
4669 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4670 for (formal = entry->sym->formal; formal; formal = formal->next)
4672 if (formal->sym && sym->name == formal->sym->name)
4673 seen = true;
4676 /* If it has not been seen as a dummy, this is an error. */
4677 if (!seen)
4679 if (specification_expr)
4680 gfc_error ("Variable '%s', used in a specification expression"
4681 ", is referenced at %L before the ENTRY statement "
4682 "in which it is a parameter",
4683 sym->name, &cs_base->current->loc);
4684 else
4685 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4686 "statement in which it is a parameter",
4687 sym->name, &cs_base->current->loc);
4688 t = FAILURE;
4692 /* Now do the same check on the specification expressions. */
4693 specification_expr = 1;
4694 if (sym->ts.type == BT_CHARACTER
4695 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
4696 t = FAILURE;
4698 if (sym->as)
4699 for (n = 0; n < sym->as->rank; n++)
4701 specification_expr = 1;
4702 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4703 t = FAILURE;
4704 specification_expr = 1;
4705 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4706 t = FAILURE;
4708 specification_expr = 0;
4710 if (t == SUCCESS)
4711 /* Update the symbol's entry level. */
4712 sym->entry_id = current_entry_id + 1;
4715 resolve_procedure:
4716 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
4717 t = FAILURE;
4719 /* F2008, C617 and C1229. */
4720 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
4721 && gfc_is_coindexed (e))
4723 gfc_ref *ref, *ref2 = NULL;
4725 if (e->ts.type == BT_CLASS)
4727 gfc_error ("Polymorphic subobject of coindexed object at %L",
4728 &e->where);
4729 t = FAILURE;
4732 for (ref = e->ref; ref; ref = ref->next)
4734 if (ref->type == REF_COMPONENT)
4735 ref2 = ref;
4736 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4737 break;
4740 for ( ; ref; ref = ref->next)
4741 if (ref->type == REF_COMPONENT)
4742 break;
4744 /* Expression itself is coindexed object. */
4745 if (ref == NULL)
4747 gfc_component *c;
4748 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
4749 for ( ; c; c = c->next)
4750 if (c->attr.allocatable && c->ts.type == BT_CLASS)
4752 gfc_error ("Coindexed object with polymorphic allocatable "
4753 "subcomponent at %L", &e->where);
4754 t = FAILURE;
4755 break;
4760 return t;
4764 /* Checks to see that the correct symbol has been host associated.
4765 The only situation where this arises is that in which a twice
4766 contained function is parsed after the host association is made.
4767 Therefore, on detecting this, change the symbol in the expression
4768 and convert the array reference into an actual arglist if the old
4769 symbol is a variable. */
4770 static bool
4771 check_host_association (gfc_expr *e)
4773 gfc_symbol *sym, *old_sym;
4774 gfc_symtree *st;
4775 int n;
4776 gfc_ref *ref;
4777 gfc_actual_arglist *arg, *tail = NULL;
4778 bool retval = e->expr_type == EXPR_FUNCTION;
4780 /* If the expression is the result of substitution in
4781 interface.c(gfc_extend_expr) because there is no way in
4782 which the host association can be wrong. */
4783 if (e->symtree == NULL
4784 || e->symtree->n.sym == NULL
4785 || e->user_operator)
4786 return retval;
4788 old_sym = e->symtree->n.sym;
4790 if (gfc_current_ns->parent
4791 && old_sym->ns != gfc_current_ns)
4793 /* Use the 'USE' name so that renamed module symbols are
4794 correctly handled. */
4795 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
4797 if (sym && old_sym != sym
4798 && sym->ts.type == old_sym->ts.type
4799 && sym->attr.flavor == FL_PROCEDURE
4800 && sym->attr.contained)
4802 /* Clear the shape, since it might not be valid. */
4803 if (e->shape != NULL)
4805 for (n = 0; n < e->rank; n++)
4806 mpz_clear (e->shape[n]);
4808 gfc_free (e->shape);
4811 /* Give the expression the right symtree! */
4812 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
4813 gcc_assert (st != NULL);
4815 if (old_sym->attr.flavor == FL_PROCEDURE
4816 || e->expr_type == EXPR_FUNCTION)
4818 /* Original was function so point to the new symbol, since
4819 the actual argument list is already attached to the
4820 expression. */
4821 e->value.function.esym = NULL;
4822 e->symtree = st;
4824 else
4826 /* Original was variable so convert array references into
4827 an actual arglist. This does not need any checking now
4828 since gfc_resolve_function will take care of it. */
4829 e->value.function.actual = NULL;
4830 e->expr_type = EXPR_FUNCTION;
4831 e->symtree = st;
4833 /* Ambiguity will not arise if the array reference is not
4834 the last reference. */
4835 for (ref = e->ref; ref; ref = ref->next)
4836 if (ref->type == REF_ARRAY && ref->next == NULL)
4837 break;
4839 gcc_assert (ref->type == REF_ARRAY);
4841 /* Grab the start expressions from the array ref and
4842 copy them into actual arguments. */
4843 for (n = 0; n < ref->u.ar.dimen; n++)
4845 arg = gfc_get_actual_arglist ();
4846 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4847 if (e->value.function.actual == NULL)
4848 tail = e->value.function.actual = arg;
4849 else
4851 tail->next = arg;
4852 tail = arg;
4856 /* Dump the reference list and set the rank. */
4857 gfc_free_ref_list (e->ref);
4858 e->ref = NULL;
4859 e->rank = sym->as ? sym->as->rank : 0;
4862 gfc_resolve_expr (e);
4863 sym->refs++;
4866 /* This might have changed! */
4867 return e->expr_type == EXPR_FUNCTION;
4871 static void
4872 gfc_resolve_character_operator (gfc_expr *e)
4874 gfc_expr *op1 = e->value.op.op1;
4875 gfc_expr *op2 = e->value.op.op2;
4876 gfc_expr *e1 = NULL;
4877 gfc_expr *e2 = NULL;
4879 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
4881 if (op1->ts.u.cl && op1->ts.u.cl->length)
4882 e1 = gfc_copy_expr (op1->ts.u.cl->length);
4883 else if (op1->expr_type == EXPR_CONSTANT)
4884 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
4885 op1->value.character.length);
4887 if (op2->ts.u.cl && op2->ts.u.cl->length)
4888 e2 = gfc_copy_expr (op2->ts.u.cl->length);
4889 else if (op2->expr_type == EXPR_CONSTANT)
4890 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
4891 op2->value.character.length);
4893 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4895 if (!e1 || !e2)
4896 return;
4898 e->ts.u.cl->length = gfc_add (e1, e2);
4899 e->ts.u.cl->length->ts.type = BT_INTEGER;
4900 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4901 gfc_simplify_expr (e->ts.u.cl->length, 0);
4902 gfc_resolve_expr (e->ts.u.cl->length);
4904 return;
4908 /* Ensure that an character expression has a charlen and, if possible, a
4909 length expression. */
4911 static void
4912 fixup_charlen (gfc_expr *e)
4914 /* The cases fall through so that changes in expression type and the need
4915 for multiple fixes are picked up. In all circumstances, a charlen should
4916 be available for the middle end to hang a backend_decl on. */
4917 switch (e->expr_type)
4919 case EXPR_OP:
4920 gfc_resolve_character_operator (e);
4922 case EXPR_ARRAY:
4923 if (e->expr_type == EXPR_ARRAY)
4924 gfc_resolve_character_array_constructor (e);
4926 case EXPR_SUBSTRING:
4927 if (!e->ts.u.cl && e->ref)
4928 gfc_resolve_substring_charlen (e);
4930 default:
4931 if (!e->ts.u.cl)
4932 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4934 break;
4939 /* Update an actual argument to include the passed-object for type-bound
4940 procedures at the right position. */
4942 static gfc_actual_arglist*
4943 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
4944 const char *name)
4946 gcc_assert (argpos > 0);
4948 if (argpos == 1)
4950 gfc_actual_arglist* result;
4952 result = gfc_get_actual_arglist ();
4953 result->expr = po;
4954 result->next = lst;
4955 if (name)
4956 result->name = name;
4958 return result;
4961 if (lst)
4962 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
4963 else
4964 lst = update_arglist_pass (NULL, po, argpos - 1, name);
4965 return lst;
4969 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
4971 static gfc_expr*
4972 extract_compcall_passed_object (gfc_expr* e)
4974 gfc_expr* po;
4976 gcc_assert (e->expr_type == EXPR_COMPCALL);
4978 if (e->value.compcall.base_object)
4979 po = gfc_copy_expr (e->value.compcall.base_object);
4980 else
4982 po = gfc_get_expr ();
4983 po->expr_type = EXPR_VARIABLE;
4984 po->symtree = e->symtree;
4985 po->ref = gfc_copy_ref (e->ref);
4986 po->where = e->where;
4989 if (gfc_resolve_expr (po) == FAILURE)
4990 return NULL;
4992 return po;
4996 /* Update the arglist of an EXPR_COMPCALL expression to include the
4997 passed-object. */
4999 static gfc_try
5000 update_compcall_arglist (gfc_expr* e)
5002 gfc_expr* po;
5003 gfc_typebound_proc* tbp;
5005 tbp = e->value.compcall.tbp;
5007 if (tbp->error)
5008 return FAILURE;
5010 po = extract_compcall_passed_object (e);
5011 if (!po)
5012 return FAILURE;
5014 if (tbp->nopass || e->value.compcall.ignore_pass)
5016 gfc_free_expr (po);
5017 return SUCCESS;
5020 gcc_assert (tbp->pass_arg_num > 0);
5021 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5022 tbp->pass_arg_num,
5023 tbp->pass_arg);
5025 return SUCCESS;
5029 /* Extract the passed object from a PPC call (a copy of it). */
5031 static gfc_expr*
5032 extract_ppc_passed_object (gfc_expr *e)
5034 gfc_expr *po;
5035 gfc_ref **ref;
5037 po = gfc_get_expr ();
5038 po->expr_type = EXPR_VARIABLE;
5039 po->symtree = e->symtree;
5040 po->ref = gfc_copy_ref (e->ref);
5041 po->where = e->where;
5043 /* Remove PPC reference. */
5044 ref = &po->ref;
5045 while ((*ref)->next)
5046 ref = &(*ref)->next;
5047 gfc_free_ref_list (*ref);
5048 *ref = NULL;
5050 if (gfc_resolve_expr (po) == FAILURE)
5051 return NULL;
5053 return po;
5057 /* Update the actual arglist of a procedure pointer component to include the
5058 passed-object. */
5060 static gfc_try
5061 update_ppc_arglist (gfc_expr* e)
5063 gfc_expr* po;
5064 gfc_component *ppc;
5065 gfc_typebound_proc* tb;
5067 if (!gfc_is_proc_ptr_comp (e, &ppc))
5068 return FAILURE;
5070 tb = ppc->tb;
5072 if (tb->error)
5073 return FAILURE;
5074 else if (tb->nopass)
5075 return SUCCESS;
5077 po = extract_ppc_passed_object (e);
5078 if (!po)
5079 return FAILURE;
5081 if (po->rank > 0)
5083 gfc_error ("Passed-object at %L must be scalar", &e->where);
5084 return FAILURE;
5087 gcc_assert (tb->pass_arg_num > 0);
5088 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5089 tb->pass_arg_num,
5090 tb->pass_arg);
5092 return SUCCESS;
5096 /* Check that the object a TBP is called on is valid, i.e. it must not be
5097 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5099 static gfc_try
5100 check_typebound_baseobject (gfc_expr* e)
5102 gfc_expr* base;
5104 base = extract_compcall_passed_object (e);
5105 if (!base)
5106 return FAILURE;
5108 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5110 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5112 gfc_error ("Base object for type-bound procedure call at %L is of"
5113 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5114 return FAILURE;
5117 /* If the procedure called is NOPASS, the base object must be scalar. */
5118 if (e->value.compcall.tbp->nopass && base->rank > 0)
5120 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5121 " be scalar", &e->where);
5122 return FAILURE;
5125 /* FIXME: Remove once PR 41177 (this problem) is fixed completely. */
5126 if (base->rank > 0)
5128 gfc_error ("Non-scalar base object at %L currently not implemented",
5129 &e->where);
5130 return FAILURE;
5133 return SUCCESS;
5137 /* Resolve a call to a type-bound procedure, either function or subroutine,
5138 statically from the data in an EXPR_COMPCALL expression. The adapted
5139 arglist and the target-procedure symtree are returned. */
5141 static gfc_try
5142 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5143 gfc_actual_arglist** actual)
5145 gcc_assert (e->expr_type == EXPR_COMPCALL);
5146 gcc_assert (!e->value.compcall.tbp->is_generic);
5148 /* Update the actual arglist for PASS. */
5149 if (update_compcall_arglist (e) == FAILURE)
5150 return FAILURE;
5152 *actual = e->value.compcall.actual;
5153 *target = e->value.compcall.tbp->u.specific;
5155 gfc_free_ref_list (e->ref);
5156 e->ref = NULL;
5157 e->value.compcall.actual = NULL;
5159 return SUCCESS;
5163 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5164 which of the specific bindings (if any) matches the arglist and transform
5165 the expression into a call of that binding. */
5167 static gfc_try
5168 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5170 gfc_typebound_proc* genproc;
5171 const char* genname;
5173 gcc_assert (e->expr_type == EXPR_COMPCALL);
5174 genname = e->value.compcall.name;
5175 genproc = e->value.compcall.tbp;
5177 if (!genproc->is_generic)
5178 return SUCCESS;
5180 /* Try the bindings on this type and in the inheritance hierarchy. */
5181 for (; genproc; genproc = genproc->overridden)
5183 gfc_tbp_generic* g;
5185 gcc_assert (genproc->is_generic);
5186 for (g = genproc->u.generic; g; g = g->next)
5188 gfc_symbol* target;
5189 gfc_actual_arglist* args;
5190 bool matches;
5192 gcc_assert (g->specific);
5194 if (g->specific->error)
5195 continue;
5197 target = g->specific->u.specific->n.sym;
5199 /* Get the right arglist by handling PASS/NOPASS. */
5200 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5201 if (!g->specific->nopass)
5203 gfc_expr* po;
5204 po = extract_compcall_passed_object (e);
5205 if (!po)
5206 return FAILURE;
5208 gcc_assert (g->specific->pass_arg_num > 0);
5209 gcc_assert (!g->specific->error);
5210 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5211 g->specific->pass_arg);
5213 resolve_actual_arglist (args, target->attr.proc,
5214 is_external_proc (target) && !target->formal);
5216 /* Check if this arglist matches the formal. */
5217 matches = gfc_arglist_matches_symbol (&args, target);
5219 /* Clean up and break out of the loop if we've found it. */
5220 gfc_free_actual_arglist (args);
5221 if (matches)
5223 e->value.compcall.tbp = g->specific;
5224 /* Pass along the name for CLASS methods, where the vtab
5225 procedure pointer component has to be referenced. */
5226 if (name)
5227 *name = g->specific_st->name;
5228 goto success;
5233 /* Nothing matching found! */
5234 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5235 " '%s' at %L", genname, &e->where);
5236 return FAILURE;
5238 success:
5239 return SUCCESS;
5243 /* Resolve a call to a type-bound subroutine. */
5245 static gfc_try
5246 resolve_typebound_call (gfc_code* c, const char **name)
5248 gfc_actual_arglist* newactual;
5249 gfc_symtree* target;
5251 /* Check that's really a SUBROUTINE. */
5252 if (!c->expr1->value.compcall.tbp->subroutine)
5254 gfc_error ("'%s' at %L should be a SUBROUTINE",
5255 c->expr1->value.compcall.name, &c->loc);
5256 return FAILURE;
5259 if (check_typebound_baseobject (c->expr1) == FAILURE)
5260 return FAILURE;
5262 /* Pass along the name for CLASS methods, where the vtab
5263 procedure pointer component has to be referenced. */
5264 if (name)
5265 *name = c->expr1->value.compcall.name;
5267 if (resolve_typebound_generic_call (c->expr1, name) == FAILURE)
5268 return FAILURE;
5270 /* Transform into an ordinary EXEC_CALL for now. */
5272 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5273 return FAILURE;
5275 c->ext.actual = newactual;
5276 c->symtree = target;
5277 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5279 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5281 gfc_free_expr (c->expr1);
5282 c->expr1 = gfc_get_expr ();
5283 c->expr1->expr_type = EXPR_FUNCTION;
5284 c->expr1->symtree = target;
5285 c->expr1->where = c->loc;
5287 return resolve_call (c);
5291 /* Resolve a component-call expression. */
5292 static gfc_try
5293 resolve_compcall (gfc_expr* e, const char **name)
5295 gfc_actual_arglist* newactual;
5296 gfc_symtree* target;
5298 /* Check that's really a FUNCTION. */
5299 if (!e->value.compcall.tbp->function)
5301 gfc_error ("'%s' at %L should be a FUNCTION",
5302 e->value.compcall.name, &e->where);
5303 return FAILURE;
5306 /* These must not be assign-calls! */
5307 gcc_assert (!e->value.compcall.assign);
5309 if (check_typebound_baseobject (e) == FAILURE)
5310 return FAILURE;
5312 /* Pass along the name for CLASS methods, where the vtab
5313 procedure pointer component has to be referenced. */
5314 if (name)
5315 *name = e->value.compcall.name;
5317 if (resolve_typebound_generic_call (e, name) == FAILURE)
5318 return FAILURE;
5319 gcc_assert (!e->value.compcall.tbp->is_generic);
5321 /* Take the rank from the function's symbol. */
5322 if (e->value.compcall.tbp->u.specific->n.sym->as)
5323 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5325 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5326 arglist to the TBP's binding target. */
5328 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5329 return FAILURE;
5331 e->value.function.actual = newactual;
5332 e->value.function.name = NULL;
5333 e->value.function.esym = target->n.sym;
5334 e->value.function.isym = NULL;
5335 e->symtree = target;
5336 e->ts = target->n.sym->ts;
5337 e->expr_type = EXPR_FUNCTION;
5339 /* Resolution is not necessary if this is a class subroutine; this
5340 function only has to identify the specific proc. Resolution of
5341 the call will be done next in resolve_typebound_call. */
5342 return gfc_resolve_expr (e);
5346 /* Get the ultimate declared type from an expression. In addition,
5347 return the last class/derived type reference and the copy of the
5348 reference list. */
5349 static gfc_symbol*
5350 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5351 gfc_expr *e)
5353 gfc_symbol *declared;
5354 gfc_ref *ref;
5356 declared = NULL;
5357 *class_ref = NULL;
5358 *new_ref = gfc_copy_ref (e->ref);
5359 for (ref = *new_ref; ref; ref = ref->next)
5361 if (ref->type != REF_COMPONENT)
5362 continue;
5364 if (ref->u.c.component->ts.type == BT_CLASS
5365 || ref->u.c.component->ts.type == BT_DERIVED)
5367 declared = ref->u.c.component->ts.u.derived;
5368 *class_ref = ref;
5372 if (declared == NULL)
5373 declared = e->symtree->n.sym->ts.u.derived;
5375 return declared;
5379 /* Resolve a typebound function, or 'method'. First separate all
5380 the non-CLASS references by calling resolve_compcall directly. */
5382 static gfc_try
5383 resolve_typebound_function (gfc_expr* e)
5385 gfc_symbol *declared;
5386 gfc_component *c;
5387 gfc_ref *new_ref;
5388 gfc_ref *class_ref;
5389 gfc_symtree *st;
5390 const char *name;
5391 const char *genname;
5392 gfc_typespec ts;
5394 st = e->symtree;
5395 if (st == NULL)
5396 return resolve_compcall (e, NULL);
5398 /* Get the CLASS declared type. */
5399 declared = get_declared_from_expr (&class_ref, &new_ref, e);
5401 /* Weed out cases of the ultimate component being a derived type. */
5402 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5403 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5405 gfc_free_ref_list (new_ref);
5406 return resolve_compcall (e, NULL);
5409 c = gfc_find_component (declared, "$data", true, true);
5410 declared = c->ts.u.derived;
5412 /* Keep the generic name so that the vtab reference can be made. */
5413 genname = NULL;
5414 if (e->value.compcall.tbp->is_generic)
5415 genname = e->value.compcall.name;
5417 /* Treat the call as if it is a typebound procedure, in order to roll
5418 out the correct name for the specific function. */
5419 resolve_compcall (e, &name);
5420 ts = e->ts;
5422 /* Then convert the expression to a procedure pointer component call. */
5423 e->value.function.esym = NULL;
5424 e->symtree = st;
5426 if (class_ref)
5428 gfc_free_ref_list (class_ref->next);
5429 e->ref = new_ref;
5432 /* '$vptr' points to the vtab, which contains the procedure pointers. */
5433 gfc_add_component_ref (e, "$vptr");
5434 if (genname)
5436 /* A generic procedure needs the subsidiary vtabs and vtypes for
5437 the specific procedures to have been build. */
5438 gfc_symbol *vtab;
5439 vtab = gfc_find_derived_vtab (declared, true);
5440 gcc_assert (vtab);
5441 gfc_add_component_ref (e, genname);
5443 gfc_add_component_ref (e, name);
5445 /* Recover the typespec for the expression. This is really only
5446 necessary for generic procedures, where the additional call
5447 to gfc_add_component_ref seems to throw the collection of the
5448 correct typespec. */
5449 e->ts = ts;
5450 return SUCCESS;
5453 /* Resolve a typebound subroutine, or 'method'. First separate all
5454 the non-CLASS references by calling resolve_typebound_call
5455 directly. */
5457 static gfc_try
5458 resolve_typebound_subroutine (gfc_code *code)
5460 gfc_symbol *declared;
5461 gfc_component *c;
5462 gfc_ref *new_ref;
5463 gfc_ref *class_ref;
5464 gfc_symtree *st;
5465 const char *genname;
5466 const char *name;
5467 gfc_typespec ts;
5469 st = code->expr1->symtree;
5470 if (st == NULL)
5471 return resolve_typebound_call (code, NULL);
5473 /* Get the CLASS declared type. */
5474 declared = get_declared_from_expr (&class_ref, &new_ref, code->expr1);
5476 /* Weed out cases of the ultimate component being a derived type. */
5477 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5478 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5480 gfc_free_ref_list (new_ref);
5481 return resolve_typebound_call (code, NULL);
5484 c = gfc_find_component (declared, "$data", true, true);
5485 declared = c->ts.u.derived;
5487 /* Keep the generic name so that the vtab reference can be made. */
5488 genname = NULL;
5489 if (code->expr1->value.compcall.tbp->is_generic)
5490 genname = code->expr1->value.compcall.name;
5492 resolve_typebound_call (code, &name);
5493 ts = code->expr1->ts;
5495 /* Then convert the expression to a procedure pointer component call. */
5496 code->expr1->value.function.esym = NULL;
5497 code->expr1->symtree = st;
5499 if (class_ref)
5501 gfc_free_ref_list (class_ref->next);
5502 code->expr1->ref = new_ref;
5505 /* '$vptr' points to the vtab, which contains the procedure pointers. */
5506 gfc_add_component_ref (code->expr1, "$vptr");
5507 if (genname)
5509 /* A generic procedure needs the subsidiary vtabs and vtypes for
5510 the specific procedures to have been build. */
5511 gfc_symbol *vtab;
5512 vtab = gfc_find_derived_vtab (declared, true);
5513 gcc_assert (vtab);
5514 gfc_add_component_ref (code->expr1, genname);
5516 gfc_add_component_ref (code->expr1, name);
5518 /* Recover the typespec for the expression. This is really only
5519 necessary for generic procedures, where the additional call
5520 to gfc_add_component_ref seems to throw the collection of the
5521 correct typespec. */
5522 code->expr1->ts = ts;
5523 return SUCCESS;
5527 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5529 static gfc_try
5530 resolve_ppc_call (gfc_code* c)
5532 gfc_component *comp;
5533 bool b;
5535 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
5536 gcc_assert (b);
5538 c->resolved_sym = c->expr1->symtree->n.sym;
5539 c->expr1->expr_type = EXPR_VARIABLE;
5541 if (!comp->attr.subroutine)
5542 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5544 if (resolve_ref (c->expr1) == FAILURE)
5545 return FAILURE;
5547 if (update_ppc_arglist (c->expr1) == FAILURE)
5548 return FAILURE;
5550 c->ext.actual = c->expr1->value.compcall.actual;
5552 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5553 comp->formal == NULL) == FAILURE)
5554 return FAILURE;
5556 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5558 return SUCCESS;
5562 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5564 static gfc_try
5565 resolve_expr_ppc (gfc_expr* e)
5567 gfc_component *comp;
5568 bool b;
5570 b = gfc_is_proc_ptr_comp (e, &comp);
5571 gcc_assert (b);
5573 /* Convert to EXPR_FUNCTION. */
5574 e->expr_type = EXPR_FUNCTION;
5575 e->value.function.isym = NULL;
5576 e->value.function.actual = e->value.compcall.actual;
5577 e->ts = comp->ts;
5578 if (comp->as != NULL)
5579 e->rank = comp->as->rank;
5581 if (!comp->attr.function)
5582 gfc_add_function (&comp->attr, comp->name, &e->where);
5584 if (resolve_ref (e) == FAILURE)
5585 return FAILURE;
5587 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5588 comp->formal == NULL) == FAILURE)
5589 return FAILURE;
5591 if (update_ppc_arglist (e) == FAILURE)
5592 return FAILURE;
5594 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
5596 return SUCCESS;
5600 static bool
5601 gfc_is_expandable_expr (gfc_expr *e)
5603 gfc_constructor *con;
5605 if (e->expr_type == EXPR_ARRAY)
5607 /* Traverse the constructor looking for variables that are flavor
5608 parameter. Parameters must be expanded since they are fully used at
5609 compile time. */
5610 con = gfc_constructor_first (e->value.constructor);
5611 for (; con; con = gfc_constructor_next (con))
5613 if (con->expr->expr_type == EXPR_VARIABLE
5614 && con->expr->symtree
5615 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
5616 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
5617 return true;
5618 if (con->expr->expr_type == EXPR_ARRAY
5619 && gfc_is_expandable_expr (con->expr))
5620 return true;
5624 return false;
5627 /* Resolve an expression. That is, make sure that types of operands agree
5628 with their operators, intrinsic operators are converted to function calls
5629 for overloaded types and unresolved function references are resolved. */
5631 gfc_try
5632 gfc_resolve_expr (gfc_expr *e)
5634 gfc_try t;
5635 bool inquiry_save;
5637 if (e == NULL)
5638 return SUCCESS;
5640 /* inquiry_argument only applies to variables. */
5641 inquiry_save = inquiry_argument;
5642 if (e->expr_type != EXPR_VARIABLE)
5643 inquiry_argument = false;
5645 switch (e->expr_type)
5647 case EXPR_OP:
5648 t = resolve_operator (e);
5649 break;
5651 case EXPR_FUNCTION:
5652 case EXPR_VARIABLE:
5654 if (check_host_association (e))
5655 t = resolve_function (e);
5656 else
5658 t = resolve_variable (e);
5659 if (t == SUCCESS)
5660 expression_rank (e);
5663 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
5664 && e->ref->type != REF_SUBSTRING)
5665 gfc_resolve_substring_charlen (e);
5667 break;
5669 case EXPR_COMPCALL:
5670 t = resolve_typebound_function (e);
5671 break;
5673 case EXPR_SUBSTRING:
5674 t = resolve_ref (e);
5675 break;
5677 case EXPR_CONSTANT:
5678 case EXPR_NULL:
5679 t = SUCCESS;
5680 break;
5682 case EXPR_PPC:
5683 t = resolve_expr_ppc (e);
5684 break;
5686 case EXPR_ARRAY:
5687 t = FAILURE;
5688 if (resolve_ref (e) == FAILURE)
5689 break;
5691 t = gfc_resolve_array_constructor (e);
5692 /* Also try to expand a constructor. */
5693 if (t == SUCCESS)
5695 expression_rank (e);
5696 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
5697 gfc_expand_constructor (e);
5700 /* This provides the opportunity for the length of constructors with
5701 character valued function elements to propagate the string length
5702 to the expression. */
5703 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
5705 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
5706 here rather then add a duplicate test for it above. */
5707 gfc_expand_constructor (e);
5708 t = gfc_resolve_character_array_constructor (e);
5711 break;
5713 case EXPR_STRUCTURE:
5714 t = resolve_ref (e);
5715 if (t == FAILURE)
5716 break;
5718 t = resolve_structure_cons (e);
5719 if (t == FAILURE)
5720 break;
5722 t = gfc_simplify_expr (e, 0);
5723 break;
5725 default:
5726 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
5729 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
5730 fixup_charlen (e);
5732 inquiry_argument = inquiry_save;
5734 return t;
5738 /* Resolve an expression from an iterator. They must be scalar and have
5739 INTEGER or (optionally) REAL type. */
5741 static gfc_try
5742 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
5743 const char *name_msgid)
5745 if (gfc_resolve_expr (expr) == FAILURE)
5746 return FAILURE;
5748 if (expr->rank != 0)
5750 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
5751 return FAILURE;
5754 if (expr->ts.type != BT_INTEGER)
5756 if (expr->ts.type == BT_REAL)
5758 if (real_ok)
5759 return gfc_notify_std (GFC_STD_F95_DEL,
5760 "Deleted feature: %s at %L must be integer",
5761 _(name_msgid), &expr->where);
5762 else
5764 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
5765 &expr->where);
5766 return FAILURE;
5769 else
5771 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
5772 return FAILURE;
5775 return SUCCESS;
5779 /* Resolve the expressions in an iterator structure. If REAL_OK is
5780 false allow only INTEGER type iterators, otherwise allow REAL types. */
5782 gfc_try
5783 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
5785 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
5786 == FAILURE)
5787 return FAILURE;
5789 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
5791 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5792 &iter->var->where);
5793 return FAILURE;
5796 if (gfc_resolve_iterator_expr (iter->start, real_ok,
5797 "Start expression in DO loop") == FAILURE)
5798 return FAILURE;
5800 if (gfc_resolve_iterator_expr (iter->end, real_ok,
5801 "End expression in DO loop") == FAILURE)
5802 return FAILURE;
5804 if (gfc_resolve_iterator_expr (iter->step, real_ok,
5805 "Step expression in DO loop") == FAILURE)
5806 return FAILURE;
5808 if (iter->step->expr_type == EXPR_CONSTANT)
5810 if ((iter->step->ts.type == BT_INTEGER
5811 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
5812 || (iter->step->ts.type == BT_REAL
5813 && mpfr_sgn (iter->step->value.real) == 0))
5815 gfc_error ("Step expression in DO loop at %L cannot be zero",
5816 &iter->step->where);
5817 return FAILURE;
5821 /* Convert start, end, and step to the same type as var. */
5822 if (iter->start->ts.kind != iter->var->ts.kind
5823 || iter->start->ts.type != iter->var->ts.type)
5824 gfc_convert_type (iter->start, &iter->var->ts, 2);
5826 if (iter->end->ts.kind != iter->var->ts.kind
5827 || iter->end->ts.type != iter->var->ts.type)
5828 gfc_convert_type (iter->end, &iter->var->ts, 2);
5830 if (iter->step->ts.kind != iter->var->ts.kind
5831 || iter->step->ts.type != iter->var->ts.type)
5832 gfc_convert_type (iter->step, &iter->var->ts, 2);
5834 if (iter->start->expr_type == EXPR_CONSTANT
5835 && iter->end->expr_type == EXPR_CONSTANT
5836 && iter->step->expr_type == EXPR_CONSTANT)
5838 int sgn, cmp;
5839 if (iter->start->ts.type == BT_INTEGER)
5841 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
5842 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
5844 else
5846 sgn = mpfr_sgn (iter->step->value.real);
5847 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
5849 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
5850 gfc_warning ("DO loop at %L will be executed zero times",
5851 &iter->step->where);
5854 return SUCCESS;
5858 /* Traversal function for find_forall_index. f == 2 signals that
5859 that variable itself is not to be checked - only the references. */
5861 static bool
5862 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
5864 if (expr->expr_type != EXPR_VARIABLE)
5865 return false;
5867 /* A scalar assignment */
5868 if (!expr->ref || *f == 1)
5870 if (expr->symtree->n.sym == sym)
5871 return true;
5872 else
5873 return false;
5876 if (*f == 2)
5877 *f = 1;
5878 return false;
5882 /* Check whether the FORALL index appears in the expression or not.
5883 Returns SUCCESS if SYM is found in EXPR. */
5885 gfc_try
5886 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
5888 if (gfc_traverse_expr (expr, sym, forall_index, f))
5889 return SUCCESS;
5890 else
5891 return FAILURE;
5895 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5896 to be a scalar INTEGER variable. The subscripts and stride are scalar
5897 INTEGERs, and if stride is a constant it must be nonzero.
5898 Furthermore "A subscript or stride in a forall-triplet-spec shall
5899 not contain a reference to any index-name in the
5900 forall-triplet-spec-list in which it appears." (7.5.4.1) */
5902 static void
5903 resolve_forall_iterators (gfc_forall_iterator *it)
5905 gfc_forall_iterator *iter, *iter2;
5907 for (iter = it; iter; iter = iter->next)
5909 if (gfc_resolve_expr (iter->var) == SUCCESS
5910 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
5911 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
5912 &iter->var->where);
5914 if (gfc_resolve_expr (iter->start) == SUCCESS
5915 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
5916 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
5917 &iter->start->where);
5918 if (iter->var->ts.kind != iter->start->ts.kind)
5919 gfc_convert_type (iter->start, &iter->var->ts, 2);
5921 if (gfc_resolve_expr (iter->end) == SUCCESS
5922 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
5923 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
5924 &iter->end->where);
5925 if (iter->var->ts.kind != iter->end->ts.kind)
5926 gfc_convert_type (iter->end, &iter->var->ts, 2);
5928 if (gfc_resolve_expr (iter->stride) == SUCCESS)
5930 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
5931 gfc_error ("FORALL stride expression at %L must be a scalar %s",
5932 &iter->stride->where, "INTEGER");
5934 if (iter->stride->expr_type == EXPR_CONSTANT
5935 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
5936 gfc_error ("FORALL stride expression at %L cannot be zero",
5937 &iter->stride->where);
5939 if (iter->var->ts.kind != iter->stride->ts.kind)
5940 gfc_convert_type (iter->stride, &iter->var->ts, 2);
5943 for (iter = it; iter; iter = iter->next)
5944 for (iter2 = iter; iter2; iter2 = iter2->next)
5946 if (find_forall_index (iter2->start,
5947 iter->var->symtree->n.sym, 0) == SUCCESS
5948 || find_forall_index (iter2->end,
5949 iter->var->symtree->n.sym, 0) == SUCCESS
5950 || find_forall_index (iter2->stride,
5951 iter->var->symtree->n.sym, 0) == SUCCESS)
5952 gfc_error ("FORALL index '%s' may not appear in triplet "
5953 "specification at %L", iter->var->symtree->name,
5954 &iter2->start->where);
5959 /* Given a pointer to a symbol that is a derived type, see if it's
5960 inaccessible, i.e. if it's defined in another module and the components are
5961 PRIVATE. The search is recursive if necessary. Returns zero if no
5962 inaccessible components are found, nonzero otherwise. */
5964 static int
5965 derived_inaccessible (gfc_symbol *sym)
5967 gfc_component *c;
5969 if (sym->attr.use_assoc && sym->attr.private_comp)
5970 return 1;
5972 for (c = sym->components; c; c = c->next)
5974 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
5975 return 1;
5978 return 0;
5982 /* Resolve the argument of a deallocate expression. The expression must be
5983 a pointer or a full array. */
5985 static gfc_try
5986 resolve_deallocate_expr (gfc_expr *e)
5988 symbol_attribute attr;
5989 int allocatable, pointer, check_intent_in;
5990 gfc_ref *ref;
5991 gfc_symbol *sym;
5992 gfc_component *c;
5994 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5995 check_intent_in = 1;
5997 if (gfc_resolve_expr (e) == FAILURE)
5998 return FAILURE;
6000 if (e->expr_type != EXPR_VARIABLE)
6001 goto bad;
6003 sym = e->symtree->n.sym;
6005 if (sym->ts.type == BT_CLASS)
6007 allocatable = CLASS_DATA (sym)->attr.allocatable;
6008 pointer = CLASS_DATA (sym)->attr.pointer;
6010 else
6012 allocatable = sym->attr.allocatable;
6013 pointer = sym->attr.pointer;
6015 for (ref = e->ref; ref; ref = ref->next)
6017 if (pointer)
6018 check_intent_in = 0;
6020 switch (ref->type)
6022 case REF_ARRAY:
6023 if (ref->u.ar.type != AR_FULL)
6024 allocatable = 0;
6025 break;
6027 case REF_COMPONENT:
6028 c = ref->u.c.component;
6029 if (c->ts.type == BT_CLASS)
6031 allocatable = CLASS_DATA (c)->attr.allocatable;
6032 pointer = CLASS_DATA (c)->attr.pointer;
6034 else
6036 allocatable = c->attr.allocatable;
6037 pointer = c->attr.pointer;
6039 break;
6041 case REF_SUBSTRING:
6042 allocatable = 0;
6043 break;
6047 attr = gfc_expr_attr (e);
6049 if (allocatable == 0 && attr.pointer == 0)
6051 bad:
6052 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6053 &e->where);
6056 if (check_intent_in && sym->attr.intent == INTENT_IN)
6058 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
6059 sym->name, &e->where);
6060 return FAILURE;
6063 if (e->ts.type == BT_CLASS)
6065 /* Only deallocate the DATA component. */
6066 gfc_add_component_ref (e, "$data");
6069 return SUCCESS;
6073 /* Returns true if the expression e contains a reference to the symbol sym. */
6074 static bool
6075 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6077 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6078 return true;
6080 return false;
6083 bool
6084 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6086 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6090 /* Given the expression node e for an allocatable/pointer of derived type to be
6091 allocated, get the expression node to be initialized afterwards (needed for
6092 derived types with default initializers, and derived types with allocatable
6093 components that need nullification.) */
6095 gfc_expr *
6096 gfc_expr_to_initialize (gfc_expr *e)
6098 gfc_expr *result;
6099 gfc_ref *ref;
6100 int i;
6102 result = gfc_copy_expr (e);
6104 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6105 for (ref = result->ref; ref; ref = ref->next)
6106 if (ref->type == REF_ARRAY && ref->next == NULL)
6108 ref->u.ar.type = AR_FULL;
6110 for (i = 0; i < ref->u.ar.dimen; i++)
6111 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6113 result->rank = ref->u.ar.dimen;
6114 break;
6117 return result;
6121 /* Used in resolve_allocate_expr to check that a allocation-object and
6122 a source-expr are conformable. This does not catch all possible
6123 cases; in particular a runtime checking is needed. */
6125 static gfc_try
6126 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6128 /* First compare rank. */
6129 if (e2->ref && e1->rank != e2->ref->u.ar.as->rank)
6131 gfc_error ("Source-expr at %L must be scalar or have the "
6132 "same rank as the allocate-object at %L",
6133 &e1->where, &e2->where);
6134 return FAILURE;
6137 if (e1->shape)
6139 int i;
6140 mpz_t s;
6142 mpz_init (s);
6144 for (i = 0; i < e1->rank; i++)
6146 if (e2->ref->u.ar.end[i])
6148 mpz_set (s, e2->ref->u.ar.end[i]->value.integer);
6149 mpz_sub (s, s, e2->ref->u.ar.start[i]->value.integer);
6150 mpz_add_ui (s, s, 1);
6152 else
6154 mpz_set (s, e2->ref->u.ar.start[i]->value.integer);
6157 if (mpz_cmp (e1->shape[i], s) != 0)
6159 gfc_error ("Source-expr at %L and allocate-object at %L must "
6160 "have the same shape", &e1->where, &e2->where);
6161 mpz_clear (s);
6162 return FAILURE;
6166 mpz_clear (s);
6169 return SUCCESS;
6173 /* Resolve the expression in an ALLOCATE statement, doing the additional
6174 checks to see whether the expression is OK or not. The expression must
6175 have a trailing array reference that gives the size of the array. */
6177 static gfc_try
6178 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6180 int i, pointer, allocatable, dimension, check_intent_in, is_abstract;
6181 int codimension;
6182 symbol_attribute attr;
6183 gfc_ref *ref, *ref2;
6184 gfc_array_ref *ar;
6185 gfc_symbol *sym;
6186 gfc_alloc *a;
6187 gfc_component *c;
6188 gfc_expr *init_e;
6190 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
6191 check_intent_in = 1;
6193 /* Mark the ultimost array component as being in allocate to allow DIMEN_STAR
6194 checking of coarrays. */
6195 for (ref = e->ref; ref; ref = ref->next)
6196 if (ref->next == NULL)
6197 break;
6199 if (ref && ref->type == REF_ARRAY)
6200 ref->u.ar.in_allocate = true;
6202 if (gfc_resolve_expr (e) == FAILURE)
6203 goto failure;
6205 /* Make sure the expression is allocatable or a pointer. If it is
6206 pointer, the next-to-last reference must be a pointer. */
6208 ref2 = NULL;
6209 if (e->symtree)
6210 sym = e->symtree->n.sym;
6212 /* Check whether ultimate component is abstract and CLASS. */
6213 is_abstract = 0;
6215 if (e->expr_type != EXPR_VARIABLE)
6217 allocatable = 0;
6218 attr = gfc_expr_attr (e);
6219 pointer = attr.pointer;
6220 dimension = attr.dimension;
6221 codimension = attr.codimension;
6223 else
6225 if (sym->ts.type == BT_CLASS)
6227 allocatable = CLASS_DATA (sym)->attr.allocatable;
6228 pointer = CLASS_DATA (sym)->attr.pointer;
6229 dimension = CLASS_DATA (sym)->attr.dimension;
6230 codimension = CLASS_DATA (sym)->attr.codimension;
6231 is_abstract = CLASS_DATA (sym)->attr.abstract;
6233 else
6235 allocatable = sym->attr.allocatable;
6236 pointer = sym->attr.pointer;
6237 dimension = sym->attr.dimension;
6238 codimension = sym->attr.codimension;
6241 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6243 if (pointer)
6244 check_intent_in = 0;
6246 switch (ref->type)
6248 case REF_ARRAY:
6249 if (ref->next != NULL)
6250 pointer = 0;
6251 break;
6253 case REF_COMPONENT:
6254 /* F2008, C644. */
6255 if (gfc_is_coindexed (e))
6257 gfc_error ("Coindexed allocatable object at %L",
6258 &e->where);
6259 goto failure;
6262 c = ref->u.c.component;
6263 if (c->ts.type == BT_CLASS)
6265 allocatable = CLASS_DATA (c)->attr.allocatable;
6266 pointer = CLASS_DATA (c)->attr.pointer;
6267 dimension = CLASS_DATA (c)->attr.dimension;
6268 codimension = CLASS_DATA (c)->attr.codimension;
6269 is_abstract = CLASS_DATA (c)->attr.abstract;
6271 else
6273 allocatable = c->attr.allocatable;
6274 pointer = c->attr.pointer;
6275 dimension = c->attr.dimension;
6276 codimension = c->attr.codimension;
6277 is_abstract = c->attr.abstract;
6279 break;
6281 case REF_SUBSTRING:
6282 allocatable = 0;
6283 pointer = 0;
6284 break;
6289 if (allocatable == 0 && pointer == 0)
6291 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6292 &e->where);
6293 goto failure;
6296 /* Some checks for the SOURCE tag. */
6297 if (code->expr3)
6299 /* Check F03:C631. */
6300 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6302 gfc_error ("Type of entity at %L is type incompatible with "
6303 "source-expr at %L", &e->where, &code->expr3->where);
6304 goto failure;
6307 /* Check F03:C632 and restriction following Note 6.18. */
6308 if (code->expr3->rank > 0
6309 && conformable_arrays (code->expr3, e) == FAILURE)
6310 goto failure;
6312 /* Check F03:C633. */
6313 if (code->expr3->ts.kind != e->ts.kind)
6315 gfc_error ("The allocate-object at %L and the source-expr at %L "
6316 "shall have the same kind type parameter",
6317 &e->where, &code->expr3->where);
6318 goto failure;
6321 else if (is_abstract&& code->ext.alloc.ts.type == BT_UNKNOWN)
6323 gcc_assert (e->ts.type == BT_CLASS);
6324 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6325 "type-spec or SOURCE=", sym->name, &e->where);
6326 goto failure;
6329 if (check_intent_in && sym->attr.intent == INTENT_IN)
6331 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
6332 sym->name, &e->where);
6333 goto failure;
6336 if (!code->expr3)
6338 /* Add default initializer for those derived types that need them. */
6339 if (e->ts.type == BT_DERIVED
6340 && (init_e = gfc_default_initializer (&e->ts)))
6342 gfc_code *init_st = gfc_get_code ();
6343 init_st->loc = code->loc;
6344 init_st->op = EXEC_INIT_ASSIGN;
6345 init_st->expr1 = gfc_expr_to_initialize (e);
6346 init_st->expr2 = init_e;
6347 init_st->next = code->next;
6348 code->next = init_st;
6350 else if (e->ts.type == BT_CLASS
6351 && ((code->ext.alloc.ts.type == BT_UNKNOWN
6352 && (init_e = gfc_default_initializer (&CLASS_DATA (e)->ts)))
6353 || (code->ext.alloc.ts.type == BT_DERIVED
6354 && (init_e = gfc_default_initializer (&code->ext.alloc.ts)))))
6356 gfc_code *init_st = gfc_get_code ();
6357 init_st->loc = code->loc;
6358 init_st->op = EXEC_INIT_ASSIGN;
6359 init_st->expr1 = gfc_expr_to_initialize (e);
6360 init_st->expr2 = init_e;
6361 init_st->next = code->next;
6362 code->next = init_st;
6366 if (pointer || (dimension == 0 && codimension == 0))
6367 goto success;
6369 /* Make sure the next-to-last reference node is an array specification. */
6371 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6372 || (dimension && ref2->u.ar.dimen == 0))
6374 gfc_error ("Array specification required in ALLOCATE statement "
6375 "at %L", &e->where);
6376 goto failure;
6379 /* Make sure that the array section reference makes sense in the
6380 context of an ALLOCATE specification. */
6382 ar = &ref2->u.ar;
6384 if (codimension && ar->codimen == 0)
6386 gfc_error ("Coarray specification required in ALLOCATE statement "
6387 "at %L", &e->where);
6388 goto failure;
6391 for (i = 0; i < ar->dimen; i++)
6393 if (ref2->u.ar.type == AR_ELEMENT)
6394 goto check_symbols;
6396 switch (ar->dimen_type[i])
6398 case DIMEN_ELEMENT:
6399 break;
6401 case DIMEN_RANGE:
6402 if (ar->start[i] != NULL
6403 && ar->end[i] != NULL
6404 && ar->stride[i] == NULL)
6405 break;
6407 /* Fall Through... */
6409 case DIMEN_UNKNOWN:
6410 case DIMEN_VECTOR:
6411 case DIMEN_STAR:
6412 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6413 &e->where);
6414 goto failure;
6417 check_symbols:
6418 for (a = code->ext.alloc.list; a; a = a->next)
6420 sym = a->expr->symtree->n.sym;
6422 /* TODO - check derived type components. */
6423 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6424 continue;
6426 if ((ar->start[i] != NULL
6427 && gfc_find_sym_in_expr (sym, ar->start[i]))
6428 || (ar->end[i] != NULL
6429 && gfc_find_sym_in_expr (sym, ar->end[i])))
6431 gfc_error ("'%s' must not appear in the array specification at "
6432 "%L in the same ALLOCATE statement where it is "
6433 "itself allocated", sym->name, &ar->where);
6434 goto failure;
6439 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
6441 if (ar->dimen_type[i] == DIMEN_ELEMENT
6442 || ar->dimen_type[i] == DIMEN_RANGE)
6444 if (i == (ar->dimen + ar->codimen - 1))
6446 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
6447 "statement at %L", &e->where);
6448 goto failure;
6450 break;
6453 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
6454 && ar->stride[i] == NULL)
6455 break;
6457 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
6458 &e->where);
6459 goto failure;
6462 if (codimension && ar->as->rank == 0)
6464 gfc_error ("Sorry, allocatable scalar coarrays are not yet supported "
6465 "at %L", &e->where);
6466 goto failure;
6469 success:
6470 return SUCCESS;
6472 failure:
6473 return FAILURE;
6476 static void
6477 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6479 gfc_expr *stat, *errmsg, *pe, *qe;
6480 gfc_alloc *a, *p, *q;
6482 stat = code->expr1 ? code->expr1 : NULL;
6484 errmsg = code->expr2 ? code->expr2 : NULL;
6486 /* Check the stat variable. */
6487 if (stat)
6489 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
6490 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
6491 stat->symtree->n.sym->name, &stat->where);
6493 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
6494 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
6495 &stat->where);
6497 if ((stat->ts.type != BT_INTEGER
6498 && !(stat->ref && (stat->ref->type == REF_ARRAY
6499 || stat->ref->type == REF_COMPONENT)))
6500 || stat->rank > 0)
6501 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6502 "variable", &stat->where);
6504 for (p = code->ext.alloc.list; p; p = p->next)
6505 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
6506 gfc_error ("Stat-variable at %L shall not be %sd within "
6507 "the same %s statement", &stat->where, fcn, fcn);
6510 /* Check the errmsg variable. */
6511 if (errmsg)
6513 if (!stat)
6514 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6515 &errmsg->where);
6517 if (errmsg->symtree->n.sym->attr.intent == INTENT_IN)
6518 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
6519 errmsg->symtree->n.sym->name, &errmsg->where);
6521 if (gfc_pure (NULL) && gfc_impure_variable (errmsg->symtree->n.sym))
6522 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
6523 &errmsg->where);
6525 if ((errmsg->ts.type != BT_CHARACTER
6526 && !(errmsg->ref
6527 && (errmsg->ref->type == REF_ARRAY
6528 || errmsg->ref->type == REF_COMPONENT)))
6529 || errmsg->rank > 0 )
6530 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
6531 "variable", &errmsg->where);
6533 for (p = code->ext.alloc.list; p; p = p->next)
6534 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
6535 gfc_error ("Errmsg-variable at %L shall not be %sd within "
6536 "the same %s statement", &errmsg->where, fcn, fcn);
6539 /* Check that an allocate-object appears only once in the statement.
6540 FIXME: Checking derived types is disabled. */
6541 for (p = code->ext.alloc.list; p; p = p->next)
6543 pe = p->expr;
6544 if ((pe->ref && pe->ref->type != REF_COMPONENT)
6545 && (pe->symtree->n.sym->ts.type != BT_DERIVED))
6547 for (q = p->next; q; q = q->next)
6549 qe = q->expr;
6550 if ((qe->ref && qe->ref->type != REF_COMPONENT)
6551 && (qe->symtree->n.sym->ts.type != BT_DERIVED)
6552 && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
6553 gfc_error ("Allocate-object at %L also appears at %L",
6554 &pe->where, &qe->where);
6559 if (strcmp (fcn, "ALLOCATE") == 0)
6561 for (a = code->ext.alloc.list; a; a = a->next)
6562 resolve_allocate_expr (a->expr, code);
6564 else
6566 for (a = code->ext.alloc.list; a; a = a->next)
6567 resolve_deallocate_expr (a->expr);
6572 /************ SELECT CASE resolution subroutines ************/
6574 /* Callback function for our mergesort variant. Determines interval
6575 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
6576 op1 > op2. Assumes we're not dealing with the default case.
6577 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
6578 There are nine situations to check. */
6580 static int
6581 compare_cases (const gfc_case *op1, const gfc_case *op2)
6583 int retval;
6585 if (op1->low == NULL) /* op1 = (:L) */
6587 /* op2 = (:N), so overlap. */
6588 retval = 0;
6589 /* op2 = (M:) or (M:N), L < M */
6590 if (op2->low != NULL
6591 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6592 retval = -1;
6594 else if (op1->high == NULL) /* op1 = (K:) */
6596 /* op2 = (M:), so overlap. */
6597 retval = 0;
6598 /* op2 = (:N) or (M:N), K > N */
6599 if (op2->high != NULL
6600 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6601 retval = 1;
6603 else /* op1 = (K:L) */
6605 if (op2->low == NULL) /* op2 = (:N), K > N */
6606 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6607 ? 1 : 0;
6608 else if (op2->high == NULL) /* op2 = (M:), L < M */
6609 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6610 ? -1 : 0;
6611 else /* op2 = (M:N) */
6613 retval = 0;
6614 /* L < M */
6615 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6616 retval = -1;
6617 /* K > N */
6618 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6619 retval = 1;
6623 return retval;
6627 /* Merge-sort a double linked case list, detecting overlap in the
6628 process. LIST is the head of the double linked case list before it
6629 is sorted. Returns the head of the sorted list if we don't see any
6630 overlap, or NULL otherwise. */
6632 static gfc_case *
6633 check_case_overlap (gfc_case *list)
6635 gfc_case *p, *q, *e, *tail;
6636 int insize, nmerges, psize, qsize, cmp, overlap_seen;
6638 /* If the passed list was empty, return immediately. */
6639 if (!list)
6640 return NULL;
6642 overlap_seen = 0;
6643 insize = 1;
6645 /* Loop unconditionally. The only exit from this loop is a return
6646 statement, when we've finished sorting the case list. */
6647 for (;;)
6649 p = list;
6650 list = NULL;
6651 tail = NULL;
6653 /* Count the number of merges we do in this pass. */
6654 nmerges = 0;
6656 /* Loop while there exists a merge to be done. */
6657 while (p)
6659 int i;
6661 /* Count this merge. */
6662 nmerges++;
6664 /* Cut the list in two pieces by stepping INSIZE places
6665 forward in the list, starting from P. */
6666 psize = 0;
6667 q = p;
6668 for (i = 0; i < insize; i++)
6670 psize++;
6671 q = q->right;
6672 if (!q)
6673 break;
6675 qsize = insize;
6677 /* Now we have two lists. Merge them! */
6678 while (psize > 0 || (qsize > 0 && q != NULL))
6680 /* See from which the next case to merge comes from. */
6681 if (psize == 0)
6683 /* P is empty so the next case must come from Q. */
6684 e = q;
6685 q = q->right;
6686 qsize--;
6688 else if (qsize == 0 || q == NULL)
6690 /* Q is empty. */
6691 e = p;
6692 p = p->right;
6693 psize--;
6695 else
6697 cmp = compare_cases (p, q);
6698 if (cmp < 0)
6700 /* The whole case range for P is less than the
6701 one for Q. */
6702 e = p;
6703 p = p->right;
6704 psize--;
6706 else if (cmp > 0)
6708 /* The whole case range for Q is greater than
6709 the case range for P. */
6710 e = q;
6711 q = q->right;
6712 qsize--;
6714 else
6716 /* The cases overlap, or they are the same
6717 element in the list. Either way, we must
6718 issue an error and get the next case from P. */
6719 /* FIXME: Sort P and Q by line number. */
6720 gfc_error ("CASE label at %L overlaps with CASE "
6721 "label at %L", &p->where, &q->where);
6722 overlap_seen = 1;
6723 e = p;
6724 p = p->right;
6725 psize--;
6729 /* Add the next element to the merged list. */
6730 if (tail)
6731 tail->right = e;
6732 else
6733 list = e;
6734 e->left = tail;
6735 tail = e;
6738 /* P has now stepped INSIZE places along, and so has Q. So
6739 they're the same. */
6740 p = q;
6742 tail->right = NULL;
6744 /* If we have done only one merge or none at all, we've
6745 finished sorting the cases. */
6746 if (nmerges <= 1)
6748 if (!overlap_seen)
6749 return list;
6750 else
6751 return NULL;
6754 /* Otherwise repeat, merging lists twice the size. */
6755 insize *= 2;
6760 /* Check to see if an expression is suitable for use in a CASE statement.
6761 Makes sure that all case expressions are scalar constants of the same
6762 type. Return FAILURE if anything is wrong. */
6764 static gfc_try
6765 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
6767 if (e == NULL) return SUCCESS;
6769 if (e->ts.type != case_expr->ts.type)
6771 gfc_error ("Expression in CASE statement at %L must be of type %s",
6772 &e->where, gfc_basic_typename (case_expr->ts.type));
6773 return FAILURE;
6776 /* C805 (R808) For a given case-construct, each case-value shall be of
6777 the same type as case-expr. For character type, length differences
6778 are allowed, but the kind type parameters shall be the same. */
6780 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
6782 gfc_error ("Expression in CASE statement at %L must be of kind %d",
6783 &e->where, case_expr->ts.kind);
6784 return FAILURE;
6787 /* Convert the case value kind to that of case expression kind,
6788 if needed */
6790 if (e->ts.kind != case_expr->ts.kind)
6791 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
6793 if (e->rank != 0)
6795 gfc_error ("Expression in CASE statement at %L must be scalar",
6796 &e->where);
6797 return FAILURE;
6800 return SUCCESS;
6804 /* Given a completely parsed select statement, we:
6806 - Validate all expressions and code within the SELECT.
6807 - Make sure that the selection expression is not of the wrong type.
6808 - Make sure that no case ranges overlap.
6809 - Eliminate unreachable cases and unreachable code resulting from
6810 removing case labels.
6812 The standard does allow unreachable cases, e.g. CASE (5:3). But
6813 they are a hassle for code generation, and to prevent that, we just
6814 cut them out here. This is not necessary for overlapping cases
6815 because they are illegal and we never even try to generate code.
6817 We have the additional caveat that a SELECT construct could have
6818 been a computed GOTO in the source code. Fortunately we can fairly
6819 easily work around that here: The case_expr for a "real" SELECT CASE
6820 is in code->expr1, but for a computed GOTO it is in code->expr2. All
6821 we have to do is make sure that the case_expr is a scalar integer
6822 expression. */
6824 static void
6825 resolve_select (gfc_code *code)
6827 gfc_code *body;
6828 gfc_expr *case_expr;
6829 gfc_case *cp, *default_case, *tail, *head;
6830 int seen_unreachable;
6831 int seen_logical;
6832 int ncases;
6833 bt type;
6834 gfc_try t;
6836 if (code->expr1 == NULL)
6838 /* This was actually a computed GOTO statement. */
6839 case_expr = code->expr2;
6840 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
6841 gfc_error ("Selection expression in computed GOTO statement "
6842 "at %L must be a scalar integer expression",
6843 &case_expr->where);
6845 /* Further checking is not necessary because this SELECT was built
6846 by the compiler, so it should always be OK. Just move the
6847 case_expr from expr2 to expr so that we can handle computed
6848 GOTOs as normal SELECTs from here on. */
6849 code->expr1 = code->expr2;
6850 code->expr2 = NULL;
6851 return;
6854 case_expr = code->expr1;
6856 type = case_expr->ts.type;
6857 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
6859 gfc_error ("Argument of SELECT statement at %L cannot be %s",
6860 &case_expr->where, gfc_typename (&case_expr->ts));
6862 /* Punt. Going on here just produce more garbage error messages. */
6863 return;
6866 if (case_expr->rank != 0)
6868 gfc_error ("Argument of SELECT statement at %L must be a scalar "
6869 "expression", &case_expr->where);
6871 /* Punt. */
6872 return;
6876 /* Raise a warning if an INTEGER case value exceeds the range of
6877 the case-expr. Later, all expressions will be promoted to the
6878 largest kind of all case-labels. */
6880 if (type == BT_INTEGER)
6881 for (body = code->block; body; body = body->block)
6882 for (cp = body->ext.case_list; cp; cp = cp->next)
6884 if (cp->low
6885 && gfc_check_integer_range (cp->low->value.integer,
6886 case_expr->ts.kind) != ARITH_OK)
6887 gfc_warning ("Expression in CASE statement at %L is "
6888 "not in the range of %s", &cp->low->where,
6889 gfc_typename (&case_expr->ts));
6891 if (cp->high
6892 && cp->low != cp->high
6893 && gfc_check_integer_range (cp->high->value.integer,
6894 case_expr->ts.kind) != ARITH_OK)
6895 gfc_warning ("Expression in CASE statement at %L is "
6896 "not in the range of %s", &cp->high->where,
6897 gfc_typename (&case_expr->ts));
6900 /* PR 19168 has a long discussion concerning a mismatch of the kinds
6901 of the SELECT CASE expression and its CASE values. Walk the lists
6902 of case values, and if we find a mismatch, promote case_expr to
6903 the appropriate kind. */
6905 if (type == BT_LOGICAL || type == BT_INTEGER)
6907 for (body = code->block; body; body = body->block)
6909 /* Walk the case label list. */
6910 for (cp = body->ext.case_list; cp; cp = cp->next)
6912 /* Intercept the DEFAULT case. It does not have a kind. */
6913 if (cp->low == NULL && cp->high == NULL)
6914 continue;
6916 /* Unreachable case ranges are discarded, so ignore. */
6917 if (cp->low != NULL && cp->high != NULL
6918 && cp->low != cp->high
6919 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6920 continue;
6922 if (cp->low != NULL
6923 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
6924 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
6926 if (cp->high != NULL
6927 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
6928 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
6933 /* Assume there is no DEFAULT case. */
6934 default_case = NULL;
6935 head = tail = NULL;
6936 ncases = 0;
6937 seen_logical = 0;
6939 for (body = code->block; body; body = body->block)
6941 /* Assume the CASE list is OK, and all CASE labels can be matched. */
6942 t = SUCCESS;
6943 seen_unreachable = 0;
6945 /* Walk the case label list, making sure that all case labels
6946 are legal. */
6947 for (cp = body->ext.case_list; cp; cp = cp->next)
6949 /* Count the number of cases in the whole construct. */
6950 ncases++;
6952 /* Intercept the DEFAULT case. */
6953 if (cp->low == NULL && cp->high == NULL)
6955 if (default_case != NULL)
6957 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6958 "by a second DEFAULT CASE at %L",
6959 &default_case->where, &cp->where);
6960 t = FAILURE;
6961 break;
6963 else
6965 default_case = cp;
6966 continue;
6970 /* Deal with single value cases and case ranges. Errors are
6971 issued from the validation function. */
6972 if (validate_case_label_expr (cp->low, case_expr) != SUCCESS
6973 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
6975 t = FAILURE;
6976 break;
6979 if (type == BT_LOGICAL
6980 && ((cp->low == NULL || cp->high == NULL)
6981 || cp->low != cp->high))
6983 gfc_error ("Logical range in CASE statement at %L is not "
6984 "allowed", &cp->low->where);
6985 t = FAILURE;
6986 break;
6989 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
6991 int value;
6992 value = cp->low->value.logical == 0 ? 2 : 1;
6993 if (value & seen_logical)
6995 gfc_error ("Constant logical value in CASE statement "
6996 "is repeated at %L",
6997 &cp->low->where);
6998 t = FAILURE;
6999 break;
7001 seen_logical |= value;
7004 if (cp->low != NULL && cp->high != NULL
7005 && cp->low != cp->high
7006 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7008 if (gfc_option.warn_surprising)
7009 gfc_warning ("Range specification at %L can never "
7010 "be matched", &cp->where);
7012 cp->unreachable = 1;
7013 seen_unreachable = 1;
7015 else
7017 /* If the case range can be matched, it can also overlap with
7018 other cases. To make sure it does not, we put it in a
7019 double linked list here. We sort that with a merge sort
7020 later on to detect any overlapping cases. */
7021 if (!head)
7023 head = tail = cp;
7024 head->right = head->left = NULL;
7026 else
7028 tail->right = cp;
7029 tail->right->left = tail;
7030 tail = tail->right;
7031 tail->right = NULL;
7036 /* It there was a failure in the previous case label, give up
7037 for this case label list. Continue with the next block. */
7038 if (t == FAILURE)
7039 continue;
7041 /* See if any case labels that are unreachable have been seen.
7042 If so, we eliminate them. This is a bit of a kludge because
7043 the case lists for a single case statement (label) is a
7044 single forward linked lists. */
7045 if (seen_unreachable)
7047 /* Advance until the first case in the list is reachable. */
7048 while (body->ext.case_list != NULL
7049 && body->ext.case_list->unreachable)
7051 gfc_case *n = body->ext.case_list;
7052 body->ext.case_list = body->ext.case_list->next;
7053 n->next = NULL;
7054 gfc_free_case_list (n);
7057 /* Strip all other unreachable cases. */
7058 if (body->ext.case_list)
7060 for (cp = body->ext.case_list; cp->next; cp = cp->next)
7062 if (cp->next->unreachable)
7064 gfc_case *n = cp->next;
7065 cp->next = cp->next->next;
7066 n->next = NULL;
7067 gfc_free_case_list (n);
7074 /* See if there were overlapping cases. If the check returns NULL,
7075 there was overlap. In that case we don't do anything. If head
7076 is non-NULL, we prepend the DEFAULT case. The sorted list can
7077 then used during code generation for SELECT CASE constructs with
7078 a case expression of a CHARACTER type. */
7079 if (head)
7081 head = check_case_overlap (head);
7083 /* Prepend the default_case if it is there. */
7084 if (head != NULL && default_case)
7086 default_case->left = NULL;
7087 default_case->right = head;
7088 head->left = default_case;
7092 /* Eliminate dead blocks that may be the result if we've seen
7093 unreachable case labels for a block. */
7094 for (body = code; body && body->block; body = body->block)
7096 if (body->block->ext.case_list == NULL)
7098 /* Cut the unreachable block from the code chain. */
7099 gfc_code *c = body->block;
7100 body->block = c->block;
7102 /* Kill the dead block, but not the blocks below it. */
7103 c->block = NULL;
7104 gfc_free_statements (c);
7108 /* More than two cases is legal but insane for logical selects.
7109 Issue a warning for it. */
7110 if (gfc_option.warn_surprising && type == BT_LOGICAL
7111 && ncases > 2)
7112 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7113 &code->loc);
7117 /* Check if a derived type is extensible. */
7119 bool
7120 gfc_type_is_extensible (gfc_symbol *sym)
7122 return !(sym->attr.is_bind_c || sym->attr.sequence);
7126 /* Resolve a SELECT TYPE statement. */
7128 static void
7129 resolve_select_type (gfc_code *code)
7131 gfc_symbol *selector_type;
7132 gfc_code *body, *new_st, *if_st, *tail;
7133 gfc_code *class_is = NULL, *default_case = NULL;
7134 gfc_case *c;
7135 gfc_symtree *st;
7136 char name[GFC_MAX_SYMBOL_LEN];
7137 gfc_namespace *ns;
7138 int error = 0;
7140 ns = code->ext.ns;
7141 gfc_resolve (ns);
7143 /* Check for F03:C813. */
7144 if (code->expr1->ts.type != BT_CLASS
7145 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7147 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7148 "at %L", &code->loc);
7149 return;
7152 if (code->expr2)
7154 if (code->expr1->symtree->n.sym->attr.untyped)
7155 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7156 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7158 else
7159 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7161 /* Loop over TYPE IS / CLASS IS cases. */
7162 for (body = code->block; body; body = body->block)
7164 c = body->ext.case_list;
7166 /* Check F03:C815. */
7167 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7168 && !gfc_type_is_extensible (c->ts.u.derived))
7170 gfc_error ("Derived type '%s' at %L must be extensible",
7171 c->ts.u.derived->name, &c->where);
7172 error++;
7173 continue;
7176 /* Check F03:C816. */
7177 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7178 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
7180 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7181 c->ts.u.derived->name, &c->where, selector_type->name);
7182 error++;
7183 continue;
7186 /* Intercept the DEFAULT case. */
7187 if (c->ts.type == BT_UNKNOWN)
7189 /* Check F03:C818. */
7190 if (default_case)
7192 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7193 "by a second DEFAULT CASE at %L",
7194 &default_case->ext.case_list->where, &c->where);
7195 error++;
7196 continue;
7198 else
7199 default_case = body;
7203 if (error>0)
7204 return;
7206 if (code->expr2)
7208 /* Insert assignment for selector variable. */
7209 new_st = gfc_get_code ();
7210 new_st->op = EXEC_ASSIGN;
7211 new_st->expr1 = gfc_copy_expr (code->expr1);
7212 new_st->expr2 = gfc_copy_expr (code->expr2);
7213 ns->code = new_st;
7216 /* Put SELECT TYPE statement inside a BLOCK. */
7217 new_st = gfc_get_code ();
7218 new_st->op = code->op;
7219 new_st->expr1 = code->expr1;
7220 new_st->expr2 = code->expr2;
7221 new_st->block = code->block;
7222 if (!ns->code)
7223 ns->code = new_st;
7224 else
7225 ns->code->next = new_st;
7226 code->op = EXEC_BLOCK;
7227 code->expr1 = code->expr2 = NULL;
7228 code->block = NULL;
7230 code = new_st;
7232 /* Transform to EXEC_SELECT. */
7233 code->op = EXEC_SELECT;
7234 gfc_add_component_ref (code->expr1, "$vptr");
7235 gfc_add_component_ref (code->expr1, "$hash");
7237 /* Loop over TYPE IS / CLASS IS cases. */
7238 for (body = code->block; body; body = body->block)
7240 c = body->ext.case_list;
7242 if (c->ts.type == BT_DERIVED)
7243 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
7244 c->ts.u.derived->hash_value);
7246 else if (c->ts.type == BT_UNKNOWN)
7247 continue;
7249 /* Assign temporary to selector. */
7250 if (c->ts.type == BT_CLASS)
7251 sprintf (name, "tmp$class$%s", c->ts.u.derived->name);
7252 else
7253 sprintf (name, "tmp$type$%s", c->ts.u.derived->name);
7254 st = gfc_find_symtree (ns->sym_root, name);
7255 new_st = gfc_get_code ();
7256 new_st->expr1 = gfc_get_variable_expr (st);
7257 new_st->expr2 = gfc_get_variable_expr (code->expr1->symtree);
7258 if (c->ts.type == BT_DERIVED)
7260 new_st->op = EXEC_POINTER_ASSIGN;
7261 gfc_add_component_ref (new_st->expr2, "$data");
7263 else
7264 new_st->op = EXEC_POINTER_ASSIGN;
7265 new_st->next = body->next;
7266 body->next = new_st;
7269 /* Take out CLASS IS cases for separate treatment. */
7270 body = code;
7271 while (body && body->block)
7273 if (body->block->ext.case_list->ts.type == BT_CLASS)
7275 /* Add to class_is list. */
7276 if (class_is == NULL)
7278 class_is = body->block;
7279 tail = class_is;
7281 else
7283 for (tail = class_is; tail->block; tail = tail->block) ;
7284 tail->block = body->block;
7285 tail = tail->block;
7287 /* Remove from EXEC_SELECT list. */
7288 body->block = body->block->block;
7289 tail->block = NULL;
7291 else
7292 body = body->block;
7295 if (class_is)
7297 gfc_symbol *vtab;
7299 if (!default_case)
7301 /* Add a default case to hold the CLASS IS cases. */
7302 for (tail = code; tail->block; tail = tail->block) ;
7303 tail->block = gfc_get_code ();
7304 tail = tail->block;
7305 tail->op = EXEC_SELECT_TYPE;
7306 tail->ext.case_list = gfc_get_case ();
7307 tail->ext.case_list->ts.type = BT_UNKNOWN;
7308 tail->next = NULL;
7309 default_case = tail;
7312 /* More than one CLASS IS block? */
7313 if (class_is->block)
7315 gfc_code **c1,*c2;
7316 bool swapped;
7317 /* Sort CLASS IS blocks by extension level. */
7320 swapped = false;
7321 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
7323 c2 = (*c1)->block;
7324 /* F03:C817 (check for doubles). */
7325 if ((*c1)->ext.case_list->ts.u.derived->hash_value
7326 == c2->ext.case_list->ts.u.derived->hash_value)
7328 gfc_error ("Double CLASS IS block in SELECT TYPE "
7329 "statement at %L", &c2->ext.case_list->where);
7330 return;
7332 if ((*c1)->ext.case_list->ts.u.derived->attr.extension
7333 < c2->ext.case_list->ts.u.derived->attr.extension)
7335 /* Swap. */
7336 (*c1)->block = c2->block;
7337 c2->block = *c1;
7338 *c1 = c2;
7339 swapped = true;
7343 while (swapped);
7346 /* Generate IF chain. */
7347 if_st = gfc_get_code ();
7348 if_st->op = EXEC_IF;
7349 new_st = if_st;
7350 for (body = class_is; body; body = body->block)
7352 new_st->block = gfc_get_code ();
7353 new_st = new_st->block;
7354 new_st->op = EXEC_IF;
7355 /* Set up IF condition: Call _gfortran_is_extension_of. */
7356 new_st->expr1 = gfc_get_expr ();
7357 new_st->expr1->expr_type = EXPR_FUNCTION;
7358 new_st->expr1->ts.type = BT_LOGICAL;
7359 new_st->expr1->ts.kind = 4;
7360 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
7361 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
7362 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
7363 /* Set up arguments. */
7364 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
7365 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
7366 gfc_add_component_ref (new_st->expr1->value.function.actual->expr, "$vptr");
7367 vtab = gfc_find_derived_vtab (body->ext.case_list->ts.u.derived, true);
7368 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
7369 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
7370 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
7371 new_st->next = body->next;
7373 if (default_case->next)
7375 new_st->block = gfc_get_code ();
7376 new_st = new_st->block;
7377 new_st->op = EXEC_IF;
7378 new_st->next = default_case->next;
7381 /* Replace CLASS DEFAULT code by the IF chain. */
7382 default_case->next = if_st;
7385 resolve_select (code);
7390 /* Resolve a transfer statement. This is making sure that:
7391 -- a derived type being transferred has only non-pointer components
7392 -- a derived type being transferred doesn't have private components, unless
7393 it's being transferred from the module where the type was defined
7394 -- we're not trying to transfer a whole assumed size array. */
7396 static void
7397 resolve_transfer (gfc_code *code)
7399 gfc_typespec *ts;
7400 gfc_symbol *sym;
7401 gfc_ref *ref;
7402 gfc_expr *exp;
7404 exp = code->expr1;
7406 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
7407 return;
7409 sym = exp->symtree->n.sym;
7410 ts = &sym->ts;
7412 /* Go to actual component transferred. */
7413 for (ref = code->expr1->ref; ref; ref = ref->next)
7414 if (ref->type == REF_COMPONENT)
7415 ts = &ref->u.c.component->ts;
7417 if (ts->type == BT_DERIVED)
7419 /* Check that transferred derived type doesn't contain POINTER
7420 components. */
7421 if (ts->u.derived->attr.pointer_comp)
7423 gfc_error ("Data transfer element at %L cannot have "
7424 "POINTER components", &code->loc);
7425 return;
7428 if (ts->u.derived->attr.alloc_comp)
7430 gfc_error ("Data transfer element at %L cannot have "
7431 "ALLOCATABLE components", &code->loc);
7432 return;
7435 if (derived_inaccessible (ts->u.derived))
7437 gfc_error ("Data transfer element at %L cannot have "
7438 "PRIVATE components",&code->loc);
7439 return;
7443 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
7444 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
7446 gfc_error ("Data transfer element at %L cannot be a full reference to "
7447 "an assumed-size array", &code->loc);
7448 return;
7453 /*********** Toplevel code resolution subroutines ***********/
7455 /* Find the set of labels that are reachable from this block. We also
7456 record the last statement in each block. */
7458 static void
7459 find_reachable_labels (gfc_code *block)
7461 gfc_code *c;
7463 if (!block)
7464 return;
7466 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
7468 /* Collect labels in this block. We don't keep those corresponding
7469 to END {IF|SELECT}, these are checked in resolve_branch by going
7470 up through the code_stack. */
7471 for (c = block; c; c = c->next)
7473 if (c->here && c->op != EXEC_END_BLOCK)
7474 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
7477 /* Merge with labels from parent block. */
7478 if (cs_base->prev)
7480 gcc_assert (cs_base->prev->reachable_labels);
7481 bitmap_ior_into (cs_base->reachable_labels,
7482 cs_base->prev->reachable_labels);
7487 static void
7488 resolve_sync (gfc_code *code)
7490 /* Check imageset. The * case matches expr1 == NULL. */
7491 if (code->expr1)
7493 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
7494 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
7495 "INTEGER expression", &code->expr1->where);
7496 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
7497 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
7498 gfc_error ("Imageset argument at %L must between 1 and num_images()",
7499 &code->expr1->where);
7500 else if (code->expr1->expr_type == EXPR_ARRAY
7501 && gfc_simplify_expr (code->expr1, 0) == SUCCESS)
7503 gfc_constructor *cons;
7504 cons = gfc_constructor_first (code->expr1->value.constructor);
7505 for (; cons; cons = gfc_constructor_next (cons))
7506 if (cons->expr->expr_type == EXPR_CONSTANT
7507 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
7508 gfc_error ("Imageset argument at %L must between 1 and "
7509 "num_images()", &cons->expr->where);
7513 /* Check STAT. */
7514 if (code->expr2
7515 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
7516 || code->expr2->expr_type != EXPR_VARIABLE))
7517 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
7518 &code->expr2->where);
7520 /* Check ERRMSG. */
7521 if (code->expr3
7522 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
7523 || code->expr3->expr_type != EXPR_VARIABLE))
7524 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
7525 &code->expr3->where);
7529 /* Given a branch to a label, see if the branch is conforming.
7530 The code node describes where the branch is located. */
7532 static void
7533 resolve_branch (gfc_st_label *label, gfc_code *code)
7535 code_stack *stack;
7537 if (label == NULL)
7538 return;
7540 /* Step one: is this a valid branching target? */
7542 if (label->defined == ST_LABEL_UNKNOWN)
7544 gfc_error ("Label %d referenced at %L is never defined", label->value,
7545 &label->where);
7546 return;
7549 if (label->defined != ST_LABEL_TARGET)
7551 gfc_error ("Statement at %L is not a valid branch target statement "
7552 "for the branch statement at %L", &label->where, &code->loc);
7553 return;
7556 /* Step two: make sure this branch is not a branch to itself ;-) */
7558 if (code->here == label)
7560 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
7561 return;
7564 /* Step three: See if the label is in the same block as the
7565 branching statement. The hard work has been done by setting up
7566 the bitmap reachable_labels. */
7568 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
7570 /* Check now whether there is a CRITICAL construct; if so, check
7571 whether the label is still visible outside of the CRITICAL block,
7572 which is invalid. */
7573 for (stack = cs_base; stack; stack = stack->prev)
7574 if (stack->current->op == EXEC_CRITICAL
7575 && bitmap_bit_p (stack->reachable_labels, label->value))
7576 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
7577 " at %L", &code->loc, &label->where);
7579 return;
7582 /* Step four: If we haven't found the label in the bitmap, it may
7583 still be the label of the END of the enclosing block, in which
7584 case we find it by going up the code_stack. */
7586 for (stack = cs_base; stack; stack = stack->prev)
7588 if (stack->current->next && stack->current->next->here == label)
7589 break;
7590 if (stack->current->op == EXEC_CRITICAL)
7592 /* Note: A label at END CRITICAL does not leave the CRITICAL
7593 construct as END CRITICAL is still part of it. */
7594 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
7595 " at %L", &code->loc, &label->where);
7596 return;
7600 if (stack)
7602 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
7603 return;
7606 /* The label is not in an enclosing block, so illegal. This was
7607 allowed in Fortran 66, so we allow it as extension. No
7608 further checks are necessary in this case. */
7609 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
7610 "as the GOTO statement at %L", &label->where,
7611 &code->loc);
7612 return;
7616 /* Check whether EXPR1 has the same shape as EXPR2. */
7618 static gfc_try
7619 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
7621 mpz_t shape[GFC_MAX_DIMENSIONS];
7622 mpz_t shape2[GFC_MAX_DIMENSIONS];
7623 gfc_try result = FAILURE;
7624 int i;
7626 /* Compare the rank. */
7627 if (expr1->rank != expr2->rank)
7628 return result;
7630 /* Compare the size of each dimension. */
7631 for (i=0; i<expr1->rank; i++)
7633 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
7634 goto ignore;
7636 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
7637 goto ignore;
7639 if (mpz_cmp (shape[i], shape2[i]))
7640 goto over;
7643 /* When either of the two expression is an assumed size array, we
7644 ignore the comparison of dimension sizes. */
7645 ignore:
7646 result = SUCCESS;
7648 over:
7649 for (i--; i >= 0; i--)
7651 mpz_clear (shape[i]);
7652 mpz_clear (shape2[i]);
7654 return result;
7658 /* Check whether a WHERE assignment target or a WHERE mask expression
7659 has the same shape as the outmost WHERE mask expression. */
7661 static void
7662 resolve_where (gfc_code *code, gfc_expr *mask)
7664 gfc_code *cblock;
7665 gfc_code *cnext;
7666 gfc_expr *e = NULL;
7668 cblock = code->block;
7670 /* Store the first WHERE mask-expr of the WHERE statement or construct.
7671 In case of nested WHERE, only the outmost one is stored. */
7672 if (mask == NULL) /* outmost WHERE */
7673 e = cblock->expr1;
7674 else /* inner WHERE */
7675 e = mask;
7677 while (cblock)
7679 if (cblock->expr1)
7681 /* Check if the mask-expr has a consistent shape with the
7682 outmost WHERE mask-expr. */
7683 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
7684 gfc_error ("WHERE mask at %L has inconsistent shape",
7685 &cblock->expr1->where);
7688 /* the assignment statement of a WHERE statement, or the first
7689 statement in where-body-construct of a WHERE construct */
7690 cnext = cblock->next;
7691 while (cnext)
7693 switch (cnext->op)
7695 /* WHERE assignment statement */
7696 case EXEC_ASSIGN:
7698 /* Check shape consistent for WHERE assignment target. */
7699 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
7700 gfc_error ("WHERE assignment target at %L has "
7701 "inconsistent shape", &cnext->expr1->where);
7702 break;
7705 case EXEC_ASSIGN_CALL:
7706 resolve_call (cnext);
7707 if (!cnext->resolved_sym->attr.elemental)
7708 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7709 &cnext->ext.actual->expr->where);
7710 break;
7712 /* WHERE or WHERE construct is part of a where-body-construct */
7713 case EXEC_WHERE:
7714 resolve_where (cnext, e);
7715 break;
7717 default:
7718 gfc_error ("Unsupported statement inside WHERE at %L",
7719 &cnext->loc);
7721 /* the next statement within the same where-body-construct */
7722 cnext = cnext->next;
7724 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7725 cblock = cblock->block;
7730 /* Resolve assignment in FORALL construct.
7731 NVAR is the number of FORALL index variables, and VAR_EXPR records the
7732 FORALL index variables. */
7734 static void
7735 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
7737 int n;
7739 for (n = 0; n < nvar; n++)
7741 gfc_symbol *forall_index;
7743 forall_index = var_expr[n]->symtree->n.sym;
7745 /* Check whether the assignment target is one of the FORALL index
7746 variable. */
7747 if ((code->expr1->expr_type == EXPR_VARIABLE)
7748 && (code->expr1->symtree->n.sym == forall_index))
7749 gfc_error ("Assignment to a FORALL index variable at %L",
7750 &code->expr1->where);
7751 else
7753 /* If one of the FORALL index variables doesn't appear in the
7754 assignment variable, then there could be a many-to-one
7755 assignment. Emit a warning rather than an error because the
7756 mask could be resolving this problem. */
7757 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
7758 gfc_warning ("The FORALL with index '%s' is not used on the "
7759 "left side of the assignment at %L and so might "
7760 "cause multiple assignment to this object",
7761 var_expr[n]->symtree->name, &code->expr1->where);
7767 /* Resolve WHERE statement in FORALL construct. */
7769 static void
7770 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
7771 gfc_expr **var_expr)
7773 gfc_code *cblock;
7774 gfc_code *cnext;
7776 cblock = code->block;
7777 while (cblock)
7779 /* the assignment statement of a WHERE statement, or the first
7780 statement in where-body-construct of a WHERE construct */
7781 cnext = cblock->next;
7782 while (cnext)
7784 switch (cnext->op)
7786 /* WHERE assignment statement */
7787 case EXEC_ASSIGN:
7788 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
7789 break;
7791 /* WHERE operator assignment statement */
7792 case EXEC_ASSIGN_CALL:
7793 resolve_call (cnext);
7794 if (!cnext->resolved_sym->attr.elemental)
7795 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
7796 &cnext->ext.actual->expr->where);
7797 break;
7799 /* WHERE or WHERE construct is part of a where-body-construct */
7800 case EXEC_WHERE:
7801 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
7802 break;
7804 default:
7805 gfc_error ("Unsupported statement inside WHERE at %L",
7806 &cnext->loc);
7808 /* the next statement within the same where-body-construct */
7809 cnext = cnext->next;
7811 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7812 cblock = cblock->block;
7817 /* Traverse the FORALL body to check whether the following errors exist:
7818 1. For assignment, check if a many-to-one assignment happens.
7819 2. For WHERE statement, check the WHERE body to see if there is any
7820 many-to-one assignment. */
7822 static void
7823 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
7825 gfc_code *c;
7827 c = code->block->next;
7828 while (c)
7830 switch (c->op)
7832 case EXEC_ASSIGN:
7833 case EXEC_POINTER_ASSIGN:
7834 gfc_resolve_assign_in_forall (c, nvar, var_expr);
7835 break;
7837 case EXEC_ASSIGN_CALL:
7838 resolve_call (c);
7839 break;
7841 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
7842 there is no need to handle it here. */
7843 case EXEC_FORALL:
7844 break;
7845 case EXEC_WHERE:
7846 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
7847 break;
7848 default:
7849 break;
7851 /* The next statement in the FORALL body. */
7852 c = c->next;
7857 /* Counts the number of iterators needed inside a forall construct, including
7858 nested forall constructs. This is used to allocate the needed memory
7859 in gfc_resolve_forall. */
7861 static int
7862 gfc_count_forall_iterators (gfc_code *code)
7864 int max_iters, sub_iters, current_iters;
7865 gfc_forall_iterator *fa;
7867 gcc_assert(code->op == EXEC_FORALL);
7868 max_iters = 0;
7869 current_iters = 0;
7871 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7872 current_iters ++;
7874 code = code->block->next;
7876 while (code)
7878 if (code->op == EXEC_FORALL)
7880 sub_iters = gfc_count_forall_iterators (code);
7881 if (sub_iters > max_iters)
7882 max_iters = sub_iters;
7884 code = code->next;
7887 return current_iters + max_iters;
7891 /* Given a FORALL construct, first resolve the FORALL iterator, then call
7892 gfc_resolve_forall_body to resolve the FORALL body. */
7894 static void
7895 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
7897 static gfc_expr **var_expr;
7898 static int total_var = 0;
7899 static int nvar = 0;
7900 int old_nvar, tmp;
7901 gfc_forall_iterator *fa;
7902 int i;
7904 old_nvar = nvar;
7906 /* Start to resolve a FORALL construct */
7907 if (forall_save == 0)
7909 /* Count the total number of FORALL index in the nested FORALL
7910 construct in order to allocate the VAR_EXPR with proper size. */
7911 total_var = gfc_count_forall_iterators (code);
7913 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
7914 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
7917 /* The information about FORALL iterator, including FORALL index start, end
7918 and stride. The FORALL index can not appear in start, end or stride. */
7919 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7921 /* Check if any outer FORALL index name is the same as the current
7922 one. */
7923 for (i = 0; i < nvar; i++)
7925 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
7927 gfc_error ("An outer FORALL construct already has an index "
7928 "with this name %L", &fa->var->where);
7932 /* Record the current FORALL index. */
7933 var_expr[nvar] = gfc_copy_expr (fa->var);
7935 nvar++;
7937 /* No memory leak. */
7938 gcc_assert (nvar <= total_var);
7941 /* Resolve the FORALL body. */
7942 gfc_resolve_forall_body (code, nvar, var_expr);
7944 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
7945 gfc_resolve_blocks (code->block, ns);
7947 tmp = nvar;
7948 nvar = old_nvar;
7949 /* Free only the VAR_EXPRs allocated in this frame. */
7950 for (i = nvar; i < tmp; i++)
7951 gfc_free_expr (var_expr[i]);
7953 if (nvar == 0)
7955 /* We are in the outermost FORALL construct. */
7956 gcc_assert (forall_save == 0);
7958 /* VAR_EXPR is not needed any more. */
7959 gfc_free (var_expr);
7960 total_var = 0;
7965 /* Resolve a BLOCK construct statement. */
7967 static void
7968 resolve_block_construct (gfc_code* code)
7970 /* Eventually, we may want to do some checks here or handle special stuff.
7971 But so far the only thing we can do is resolving the local namespace. */
7973 gfc_resolve (code->ext.ns);
7977 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
7978 DO code nodes. */
7980 static void resolve_code (gfc_code *, gfc_namespace *);
7982 void
7983 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
7985 gfc_try t;
7987 for (; b; b = b->block)
7989 t = gfc_resolve_expr (b->expr1);
7990 if (gfc_resolve_expr (b->expr2) == FAILURE)
7991 t = FAILURE;
7993 switch (b->op)
7995 case EXEC_IF:
7996 if (t == SUCCESS && b->expr1 != NULL
7997 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
7998 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
7999 &b->expr1->where);
8000 break;
8002 case EXEC_WHERE:
8003 if (t == SUCCESS
8004 && b->expr1 != NULL
8005 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
8006 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8007 &b->expr1->where);
8008 break;
8010 case EXEC_GOTO:
8011 resolve_branch (b->label1, b);
8012 break;
8014 case EXEC_BLOCK:
8015 resolve_block_construct (b);
8016 break;
8018 case EXEC_SELECT:
8019 case EXEC_SELECT_TYPE:
8020 case EXEC_FORALL:
8021 case EXEC_DO:
8022 case EXEC_DO_WHILE:
8023 case EXEC_CRITICAL:
8024 case EXEC_READ:
8025 case EXEC_WRITE:
8026 case EXEC_IOLENGTH:
8027 case EXEC_WAIT:
8028 break;
8030 case EXEC_OMP_ATOMIC:
8031 case EXEC_OMP_CRITICAL:
8032 case EXEC_OMP_DO:
8033 case EXEC_OMP_MASTER:
8034 case EXEC_OMP_ORDERED:
8035 case EXEC_OMP_PARALLEL:
8036 case EXEC_OMP_PARALLEL_DO:
8037 case EXEC_OMP_PARALLEL_SECTIONS:
8038 case EXEC_OMP_PARALLEL_WORKSHARE:
8039 case EXEC_OMP_SECTIONS:
8040 case EXEC_OMP_SINGLE:
8041 case EXEC_OMP_TASK:
8042 case EXEC_OMP_TASKWAIT:
8043 case EXEC_OMP_WORKSHARE:
8044 break;
8046 default:
8047 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
8050 resolve_code (b->next, ns);
8055 /* Does everything to resolve an ordinary assignment. Returns true
8056 if this is an interface assignment. */
8057 static bool
8058 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
8060 bool rval = false;
8061 gfc_expr *lhs;
8062 gfc_expr *rhs;
8063 int llen = 0;
8064 int rlen = 0;
8065 int n;
8066 gfc_ref *ref;
8068 if (gfc_extend_assign (code, ns) == SUCCESS)
8070 gfc_expr** rhsptr;
8072 if (code->op == EXEC_ASSIGN_CALL)
8074 lhs = code->ext.actual->expr;
8075 rhsptr = &code->ext.actual->next->expr;
8077 else
8079 gfc_actual_arglist* args;
8080 gfc_typebound_proc* tbp;
8082 gcc_assert (code->op == EXEC_COMPCALL);
8084 args = code->expr1->value.compcall.actual;
8085 lhs = args->expr;
8086 rhsptr = &args->next->expr;
8088 tbp = code->expr1->value.compcall.tbp;
8089 gcc_assert (!tbp->is_generic);
8092 /* Make a temporary rhs when there is a default initializer
8093 and rhs is the same symbol as the lhs. */
8094 if ((*rhsptr)->expr_type == EXPR_VARIABLE
8095 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
8096 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
8097 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
8098 *rhsptr = gfc_get_parentheses (*rhsptr);
8100 return true;
8103 lhs = code->expr1;
8104 rhs = code->expr2;
8106 if (rhs->is_boz
8107 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
8108 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
8109 &code->loc) == FAILURE)
8110 return false;
8112 /* Handle the case of a BOZ literal on the RHS. */
8113 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
8115 int rc;
8116 if (gfc_option.warn_surprising)
8117 gfc_warning ("BOZ literal at %L is bitwise transferred "
8118 "non-integer symbol '%s'", &code->loc,
8119 lhs->symtree->n.sym->name);
8121 if (!gfc_convert_boz (rhs, &lhs->ts))
8122 return false;
8123 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
8125 if (rc == ARITH_UNDERFLOW)
8126 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
8127 ". This check can be disabled with the option "
8128 "-fno-range-check", &rhs->where);
8129 else if (rc == ARITH_OVERFLOW)
8130 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
8131 ". This check can be disabled with the option "
8132 "-fno-range-check", &rhs->where);
8133 else if (rc == ARITH_NAN)
8134 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
8135 ". This check can be disabled with the option "
8136 "-fno-range-check", &rhs->where);
8137 return false;
8142 if (lhs->ts.type == BT_CHARACTER
8143 && gfc_option.warn_character_truncation)
8145 if (lhs->ts.u.cl != NULL
8146 && lhs->ts.u.cl->length != NULL
8147 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8148 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
8150 if (rhs->expr_type == EXPR_CONSTANT)
8151 rlen = rhs->value.character.length;
8153 else if (rhs->ts.u.cl != NULL
8154 && rhs->ts.u.cl->length != NULL
8155 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8156 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
8158 if (rlen && llen && rlen > llen)
8159 gfc_warning_now ("CHARACTER expression will be truncated "
8160 "in assignment (%d/%d) at %L",
8161 llen, rlen, &code->loc);
8164 /* Ensure that a vector index expression for the lvalue is evaluated
8165 to a temporary if the lvalue symbol is referenced in it. */
8166 if (lhs->rank)
8168 for (ref = lhs->ref; ref; ref= ref->next)
8169 if (ref->type == REF_ARRAY)
8171 for (n = 0; n < ref->u.ar.dimen; n++)
8172 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
8173 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
8174 ref->u.ar.start[n]))
8175 ref->u.ar.start[n]
8176 = gfc_get_parentheses (ref->u.ar.start[n]);
8180 if (gfc_pure (NULL))
8182 if (gfc_impure_variable (lhs->symtree->n.sym))
8184 gfc_error ("Cannot assign to variable '%s' in PURE "
8185 "procedure at %L",
8186 lhs->symtree->n.sym->name,
8187 &lhs->where);
8188 return rval;
8191 if (lhs->ts.type == BT_DERIVED
8192 && lhs->expr_type == EXPR_VARIABLE
8193 && lhs->ts.u.derived->attr.pointer_comp
8194 && rhs->expr_type == EXPR_VARIABLE
8195 && (gfc_impure_variable (rhs->symtree->n.sym)
8196 || gfc_is_coindexed (rhs)))
8198 /* F2008, C1283. */
8199 if (gfc_is_coindexed (rhs))
8200 gfc_error ("Coindexed expression at %L is assigned to "
8201 "a derived type variable with a POINTER "
8202 "component in a PURE procedure",
8203 &rhs->where);
8204 else
8205 gfc_error ("The impure variable at %L is assigned to "
8206 "a derived type variable with a POINTER "
8207 "component in a PURE procedure (12.6)",
8208 &rhs->where);
8209 return rval;
8212 /* Fortran 2008, C1283. */
8213 if (gfc_is_coindexed (lhs))
8215 gfc_error ("Assignment to coindexed variable at %L in a PURE "
8216 "procedure", &rhs->where);
8217 return rval;
8221 /* F03:7.4.1.2. */
8222 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
8223 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
8224 if (lhs->ts.type == BT_CLASS)
8226 gfc_error ("Variable must not be polymorphic in assignment at %L",
8227 &lhs->where);
8228 return false;
8231 /* F2008, Section 7.2.1.2. */
8232 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
8234 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
8235 "component in assignment at %L", &lhs->where);
8236 return false;
8239 gfc_check_assign (lhs, rhs, 1);
8240 return false;
8244 /* Given a block of code, recursively resolve everything pointed to by this
8245 code block. */
8247 static void
8248 resolve_code (gfc_code *code, gfc_namespace *ns)
8250 int omp_workshare_save;
8251 int forall_save;
8252 code_stack frame;
8253 gfc_try t;
8255 frame.prev = cs_base;
8256 frame.head = code;
8257 cs_base = &frame;
8259 find_reachable_labels (code);
8261 for (; code; code = code->next)
8263 frame.current = code;
8264 forall_save = forall_flag;
8266 if (code->op == EXEC_FORALL)
8268 forall_flag = 1;
8269 gfc_resolve_forall (code, ns, forall_save);
8270 forall_flag = 2;
8272 else if (code->block)
8274 omp_workshare_save = -1;
8275 switch (code->op)
8277 case EXEC_OMP_PARALLEL_WORKSHARE:
8278 omp_workshare_save = omp_workshare_flag;
8279 omp_workshare_flag = 1;
8280 gfc_resolve_omp_parallel_blocks (code, ns);
8281 break;
8282 case EXEC_OMP_PARALLEL:
8283 case EXEC_OMP_PARALLEL_DO:
8284 case EXEC_OMP_PARALLEL_SECTIONS:
8285 case EXEC_OMP_TASK:
8286 omp_workshare_save = omp_workshare_flag;
8287 omp_workshare_flag = 0;
8288 gfc_resolve_omp_parallel_blocks (code, ns);
8289 break;
8290 case EXEC_OMP_DO:
8291 gfc_resolve_omp_do_blocks (code, ns);
8292 break;
8293 case EXEC_SELECT_TYPE:
8294 gfc_current_ns = code->ext.ns;
8295 gfc_resolve_blocks (code->block, gfc_current_ns);
8296 gfc_current_ns = ns;
8297 break;
8298 case EXEC_OMP_WORKSHARE:
8299 omp_workshare_save = omp_workshare_flag;
8300 omp_workshare_flag = 1;
8301 /* FALLTHROUGH */
8302 default:
8303 gfc_resolve_blocks (code->block, ns);
8304 break;
8307 if (omp_workshare_save != -1)
8308 omp_workshare_flag = omp_workshare_save;
8311 t = SUCCESS;
8312 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
8313 t = gfc_resolve_expr (code->expr1);
8314 forall_flag = forall_save;
8316 if (gfc_resolve_expr (code->expr2) == FAILURE)
8317 t = FAILURE;
8319 if (code->op == EXEC_ALLOCATE
8320 && gfc_resolve_expr (code->expr3) == FAILURE)
8321 t = FAILURE;
8323 switch (code->op)
8325 case EXEC_NOP:
8326 case EXEC_END_BLOCK:
8327 case EXEC_CYCLE:
8328 case EXEC_PAUSE:
8329 case EXEC_STOP:
8330 case EXEC_ERROR_STOP:
8331 case EXEC_EXIT:
8332 case EXEC_CONTINUE:
8333 case EXEC_DT_END:
8334 case EXEC_ASSIGN_CALL:
8335 case EXEC_CRITICAL:
8336 break;
8338 case EXEC_SYNC_ALL:
8339 case EXEC_SYNC_IMAGES:
8340 case EXEC_SYNC_MEMORY:
8341 resolve_sync (code);
8342 break;
8344 case EXEC_ENTRY:
8345 /* Keep track of which entry we are up to. */
8346 current_entry_id = code->ext.entry->id;
8347 break;
8349 case EXEC_WHERE:
8350 resolve_where (code, NULL);
8351 break;
8353 case EXEC_GOTO:
8354 if (code->expr1 != NULL)
8356 if (code->expr1->ts.type != BT_INTEGER)
8357 gfc_error ("ASSIGNED GOTO statement at %L requires an "
8358 "INTEGER variable", &code->expr1->where);
8359 else if (code->expr1->symtree->n.sym->attr.assign != 1)
8360 gfc_error ("Variable '%s' has not been assigned a target "
8361 "label at %L", code->expr1->symtree->n.sym->name,
8362 &code->expr1->where);
8364 else
8365 resolve_branch (code->label1, code);
8366 break;
8368 case EXEC_RETURN:
8369 if (code->expr1 != NULL
8370 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
8371 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
8372 "INTEGER return specifier", &code->expr1->where);
8373 break;
8375 case EXEC_INIT_ASSIGN:
8376 case EXEC_END_PROCEDURE:
8377 break;
8379 case EXEC_ASSIGN:
8380 if (t == FAILURE)
8381 break;
8383 if (resolve_ordinary_assign (code, ns))
8385 if (code->op == EXEC_COMPCALL)
8386 goto compcall;
8387 else
8388 goto call;
8390 break;
8392 case EXEC_LABEL_ASSIGN:
8393 if (code->label1->defined == ST_LABEL_UNKNOWN)
8394 gfc_error ("Label %d referenced at %L is never defined",
8395 code->label1->value, &code->label1->where);
8396 if (t == SUCCESS
8397 && (code->expr1->expr_type != EXPR_VARIABLE
8398 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
8399 || code->expr1->symtree->n.sym->ts.kind
8400 != gfc_default_integer_kind
8401 || code->expr1->symtree->n.sym->as != NULL))
8402 gfc_error ("ASSIGN statement at %L requires a scalar "
8403 "default INTEGER variable", &code->expr1->where);
8404 break;
8406 case EXEC_POINTER_ASSIGN:
8407 if (t == FAILURE)
8408 break;
8410 gfc_check_pointer_assign (code->expr1, code->expr2);
8411 break;
8413 case EXEC_ARITHMETIC_IF:
8414 if (t == SUCCESS
8415 && code->expr1->ts.type != BT_INTEGER
8416 && code->expr1->ts.type != BT_REAL)
8417 gfc_error ("Arithmetic IF statement at %L requires a numeric "
8418 "expression", &code->expr1->where);
8420 resolve_branch (code->label1, code);
8421 resolve_branch (code->label2, code);
8422 resolve_branch (code->label3, code);
8423 break;
8425 case EXEC_IF:
8426 if (t == SUCCESS && code->expr1 != NULL
8427 && (code->expr1->ts.type != BT_LOGICAL
8428 || code->expr1->rank != 0))
8429 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8430 &code->expr1->where);
8431 break;
8433 case EXEC_CALL:
8434 call:
8435 resolve_call (code);
8436 break;
8438 case EXEC_COMPCALL:
8439 compcall:
8440 resolve_typebound_subroutine (code);
8441 break;
8443 case EXEC_CALL_PPC:
8444 resolve_ppc_call (code);
8445 break;
8447 case EXEC_SELECT:
8448 /* Select is complicated. Also, a SELECT construct could be
8449 a transformed computed GOTO. */
8450 resolve_select (code);
8451 break;
8453 case EXEC_SELECT_TYPE:
8454 resolve_select_type (code);
8455 break;
8457 case EXEC_BLOCK:
8458 gfc_resolve (code->ext.ns);
8459 break;
8461 case EXEC_DO:
8462 if (code->ext.iterator != NULL)
8464 gfc_iterator *iter = code->ext.iterator;
8465 if (gfc_resolve_iterator (iter, true) != FAILURE)
8466 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
8468 break;
8470 case EXEC_DO_WHILE:
8471 if (code->expr1 == NULL)
8472 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
8473 if (t == SUCCESS
8474 && (code->expr1->rank != 0
8475 || code->expr1->ts.type != BT_LOGICAL))
8476 gfc_error ("Exit condition of DO WHILE loop at %L must be "
8477 "a scalar LOGICAL expression", &code->expr1->where);
8478 break;
8480 case EXEC_ALLOCATE:
8481 if (t == SUCCESS)
8482 resolve_allocate_deallocate (code, "ALLOCATE");
8484 break;
8486 case EXEC_DEALLOCATE:
8487 if (t == SUCCESS)
8488 resolve_allocate_deallocate (code, "DEALLOCATE");
8490 break;
8492 case EXEC_OPEN:
8493 if (gfc_resolve_open (code->ext.open) == FAILURE)
8494 break;
8496 resolve_branch (code->ext.open->err, code);
8497 break;
8499 case EXEC_CLOSE:
8500 if (gfc_resolve_close (code->ext.close) == FAILURE)
8501 break;
8503 resolve_branch (code->ext.close->err, code);
8504 break;
8506 case EXEC_BACKSPACE:
8507 case EXEC_ENDFILE:
8508 case EXEC_REWIND:
8509 case EXEC_FLUSH:
8510 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
8511 break;
8513 resolve_branch (code->ext.filepos->err, code);
8514 break;
8516 case EXEC_INQUIRE:
8517 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8518 break;
8520 resolve_branch (code->ext.inquire->err, code);
8521 break;
8523 case EXEC_IOLENGTH:
8524 gcc_assert (code->ext.inquire != NULL);
8525 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8526 break;
8528 resolve_branch (code->ext.inquire->err, code);
8529 break;
8531 case EXEC_WAIT:
8532 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
8533 break;
8535 resolve_branch (code->ext.wait->err, code);
8536 resolve_branch (code->ext.wait->end, code);
8537 resolve_branch (code->ext.wait->eor, code);
8538 break;
8540 case EXEC_READ:
8541 case EXEC_WRITE:
8542 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
8543 break;
8545 resolve_branch (code->ext.dt->err, code);
8546 resolve_branch (code->ext.dt->end, code);
8547 resolve_branch (code->ext.dt->eor, code);
8548 break;
8550 case EXEC_TRANSFER:
8551 resolve_transfer (code);
8552 break;
8554 case EXEC_FORALL:
8555 resolve_forall_iterators (code->ext.forall_iterator);
8557 if (code->expr1 != NULL && code->expr1->ts.type != BT_LOGICAL)
8558 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
8559 "expression", &code->expr1->where);
8560 break;
8562 case EXEC_OMP_ATOMIC:
8563 case EXEC_OMP_BARRIER:
8564 case EXEC_OMP_CRITICAL:
8565 case EXEC_OMP_FLUSH:
8566 case EXEC_OMP_DO:
8567 case EXEC_OMP_MASTER:
8568 case EXEC_OMP_ORDERED:
8569 case EXEC_OMP_SECTIONS:
8570 case EXEC_OMP_SINGLE:
8571 case EXEC_OMP_TASKWAIT:
8572 case EXEC_OMP_WORKSHARE:
8573 gfc_resolve_omp_directive (code, ns);
8574 break;
8576 case EXEC_OMP_PARALLEL:
8577 case EXEC_OMP_PARALLEL_DO:
8578 case EXEC_OMP_PARALLEL_SECTIONS:
8579 case EXEC_OMP_PARALLEL_WORKSHARE:
8580 case EXEC_OMP_TASK:
8581 omp_workshare_save = omp_workshare_flag;
8582 omp_workshare_flag = 0;
8583 gfc_resolve_omp_directive (code, ns);
8584 omp_workshare_flag = omp_workshare_save;
8585 break;
8587 default:
8588 gfc_internal_error ("resolve_code(): Bad statement code");
8592 cs_base = frame.prev;
8596 /* Resolve initial values and make sure they are compatible with
8597 the variable. */
8599 static void
8600 resolve_values (gfc_symbol *sym)
8602 if (sym->value == NULL)
8603 return;
8605 if (gfc_resolve_expr (sym->value) == FAILURE)
8606 return;
8608 gfc_check_assign_symbol (sym, sym->value);
8612 /* Verify the binding labels for common blocks that are BIND(C). The label
8613 for a BIND(C) common block must be identical in all scoping units in which
8614 the common block is declared. Further, the binding label can not collide
8615 with any other global entity in the program. */
8617 static void
8618 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
8620 if (comm_block_tree->n.common->is_bind_c == 1)
8622 gfc_gsymbol *binding_label_gsym;
8623 gfc_gsymbol *comm_name_gsym;
8625 /* See if a global symbol exists by the common block's name. It may
8626 be NULL if the common block is use-associated. */
8627 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
8628 comm_block_tree->n.common->name);
8629 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
8630 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
8631 "with the global entity '%s' at %L",
8632 comm_block_tree->n.common->binding_label,
8633 comm_block_tree->n.common->name,
8634 &(comm_block_tree->n.common->where),
8635 comm_name_gsym->name, &(comm_name_gsym->where));
8636 else if (comm_name_gsym != NULL
8637 && strcmp (comm_name_gsym->name,
8638 comm_block_tree->n.common->name) == 0)
8640 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
8641 as expected. */
8642 if (comm_name_gsym->binding_label == NULL)
8643 /* No binding label for common block stored yet; save this one. */
8644 comm_name_gsym->binding_label =
8645 comm_block_tree->n.common->binding_label;
8646 else
8647 if (strcmp (comm_name_gsym->binding_label,
8648 comm_block_tree->n.common->binding_label) != 0)
8650 /* Common block names match but binding labels do not. */
8651 gfc_error ("Binding label '%s' for common block '%s' at %L "
8652 "does not match the binding label '%s' for common "
8653 "block '%s' at %L",
8654 comm_block_tree->n.common->binding_label,
8655 comm_block_tree->n.common->name,
8656 &(comm_block_tree->n.common->where),
8657 comm_name_gsym->binding_label,
8658 comm_name_gsym->name,
8659 &(comm_name_gsym->where));
8660 return;
8664 /* There is no binding label (NAME="") so we have nothing further to
8665 check and nothing to add as a global symbol for the label. */
8666 if (comm_block_tree->n.common->binding_label[0] == '\0' )
8667 return;
8669 binding_label_gsym =
8670 gfc_find_gsymbol (gfc_gsym_root,
8671 comm_block_tree->n.common->binding_label);
8672 if (binding_label_gsym == NULL)
8674 /* Need to make a global symbol for the binding label to prevent
8675 it from colliding with another. */
8676 binding_label_gsym =
8677 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
8678 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
8679 binding_label_gsym->type = GSYM_COMMON;
8681 else
8683 /* If comm_name_gsym is NULL, the name common block is use
8684 associated and the name could be colliding. */
8685 if (binding_label_gsym->type != GSYM_COMMON)
8686 gfc_error ("Binding label '%s' for common block '%s' at %L "
8687 "collides with the global entity '%s' at %L",
8688 comm_block_tree->n.common->binding_label,
8689 comm_block_tree->n.common->name,
8690 &(comm_block_tree->n.common->where),
8691 binding_label_gsym->name,
8692 &(binding_label_gsym->where));
8693 else if (comm_name_gsym != NULL
8694 && (strcmp (binding_label_gsym->name,
8695 comm_name_gsym->binding_label) != 0)
8696 && (strcmp (binding_label_gsym->sym_name,
8697 comm_name_gsym->name) != 0))
8698 gfc_error ("Binding label '%s' for common block '%s' at %L "
8699 "collides with global entity '%s' at %L",
8700 binding_label_gsym->name, binding_label_gsym->sym_name,
8701 &(comm_block_tree->n.common->where),
8702 comm_name_gsym->name, &(comm_name_gsym->where));
8706 return;
8710 /* Verify any BIND(C) derived types in the namespace so we can report errors
8711 for them once, rather than for each variable declared of that type. */
8713 static void
8714 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
8716 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
8717 && derived_sym->attr.is_bind_c == 1)
8718 verify_bind_c_derived_type (derived_sym);
8720 return;
8724 /* Verify that any binding labels used in a given namespace do not collide
8725 with the names or binding labels of any global symbols. */
8727 static void
8728 gfc_verify_binding_labels (gfc_symbol *sym)
8730 int has_error = 0;
8732 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
8733 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
8735 gfc_gsymbol *bind_c_sym;
8737 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
8738 if (bind_c_sym != NULL
8739 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
8741 if (sym->attr.if_source == IFSRC_DECL
8742 && (bind_c_sym->type != GSYM_SUBROUTINE
8743 && bind_c_sym->type != GSYM_FUNCTION)
8744 && ((sym->attr.contained == 1
8745 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
8746 || (sym->attr.use_assoc == 1
8747 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
8749 /* Make sure global procedures don't collide with anything. */
8750 gfc_error ("Binding label '%s' at %L collides with the global "
8751 "entity '%s' at %L", sym->binding_label,
8752 &(sym->declared_at), bind_c_sym->name,
8753 &(bind_c_sym->where));
8754 has_error = 1;
8756 else if (sym->attr.contained == 0
8757 && (sym->attr.if_source == IFSRC_IFBODY
8758 && sym->attr.flavor == FL_PROCEDURE)
8759 && (bind_c_sym->sym_name != NULL
8760 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
8762 /* Make sure procedures in interface bodies don't collide. */
8763 gfc_error ("Binding label '%s' in interface body at %L collides "
8764 "with the global entity '%s' at %L",
8765 sym->binding_label,
8766 &(sym->declared_at), bind_c_sym->name,
8767 &(bind_c_sym->where));
8768 has_error = 1;
8770 else if (sym->attr.contained == 0
8771 && sym->attr.if_source == IFSRC_UNKNOWN)
8772 if ((sym->attr.use_assoc && bind_c_sym->mod_name
8773 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
8774 || sym->attr.use_assoc == 0)
8776 gfc_error ("Binding label '%s' at %L collides with global "
8777 "entity '%s' at %L", sym->binding_label,
8778 &(sym->declared_at), bind_c_sym->name,
8779 &(bind_c_sym->where));
8780 has_error = 1;
8783 if (has_error != 0)
8784 /* Clear the binding label to prevent checking multiple times. */
8785 sym->binding_label[0] = '\0';
8787 else if (bind_c_sym == NULL)
8789 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
8790 bind_c_sym->where = sym->declared_at;
8791 bind_c_sym->sym_name = sym->name;
8793 if (sym->attr.use_assoc == 1)
8794 bind_c_sym->mod_name = sym->module;
8795 else
8796 if (sym->ns->proc_name != NULL)
8797 bind_c_sym->mod_name = sym->ns->proc_name->name;
8799 if (sym->attr.contained == 0)
8801 if (sym->attr.subroutine)
8802 bind_c_sym->type = GSYM_SUBROUTINE;
8803 else if (sym->attr.function)
8804 bind_c_sym->type = GSYM_FUNCTION;
8808 return;
8812 /* Resolve an index expression. */
8814 static gfc_try
8815 resolve_index_expr (gfc_expr *e)
8817 if (gfc_resolve_expr (e) == FAILURE)
8818 return FAILURE;
8820 if (gfc_simplify_expr (e, 0) == FAILURE)
8821 return FAILURE;
8823 if (gfc_specification_expr (e) == FAILURE)
8824 return FAILURE;
8826 return SUCCESS;
8829 /* Resolve a charlen structure. */
8831 static gfc_try
8832 resolve_charlen (gfc_charlen *cl)
8834 int i, k;
8836 if (cl->resolved)
8837 return SUCCESS;
8839 cl->resolved = 1;
8841 specification_expr = 1;
8843 if (resolve_index_expr (cl->length) == FAILURE)
8845 specification_expr = 0;
8846 return FAILURE;
8849 /* "If the character length parameter value evaluates to a negative
8850 value, the length of character entities declared is zero." */
8851 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
8853 if (gfc_option.warn_surprising)
8854 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
8855 " the length has been set to zero",
8856 &cl->length->where, i);
8857 gfc_replace_expr (cl->length,
8858 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
8861 /* Check that the character length is not too large. */
8862 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
8863 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
8864 && cl->length->ts.type == BT_INTEGER
8865 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
8867 gfc_error ("String length at %L is too large", &cl->length->where);
8868 return FAILURE;
8871 return SUCCESS;
8875 /* Test for non-constant shape arrays. */
8877 static bool
8878 is_non_constant_shape_array (gfc_symbol *sym)
8880 gfc_expr *e;
8881 int i;
8882 bool not_constant;
8884 not_constant = false;
8885 if (sym->as != NULL)
8887 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
8888 has not been simplified; parameter array references. Do the
8889 simplification now. */
8890 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
8892 e = sym->as->lower[i];
8893 if (e && (resolve_index_expr (e) == FAILURE
8894 || !gfc_is_constant_expr (e)))
8895 not_constant = true;
8896 e = sym->as->upper[i];
8897 if (e && (resolve_index_expr (e) == FAILURE
8898 || !gfc_is_constant_expr (e)))
8899 not_constant = true;
8902 return not_constant;
8905 /* Given a symbol and an initialization expression, add code to initialize
8906 the symbol to the function entry. */
8907 static void
8908 build_init_assign (gfc_symbol *sym, gfc_expr *init)
8910 gfc_expr *lval;
8911 gfc_code *init_st;
8912 gfc_namespace *ns = sym->ns;
8914 /* Search for the function namespace if this is a contained
8915 function without an explicit result. */
8916 if (sym->attr.function && sym == sym->result
8917 && sym->name != sym->ns->proc_name->name)
8919 ns = ns->contained;
8920 for (;ns; ns = ns->sibling)
8921 if (strcmp (ns->proc_name->name, sym->name) == 0)
8922 break;
8925 if (ns == NULL)
8927 gfc_free_expr (init);
8928 return;
8931 /* Build an l-value expression for the result. */
8932 lval = gfc_lval_expr_from_sym (sym);
8934 /* Add the code at scope entry. */
8935 init_st = gfc_get_code ();
8936 init_st->next = ns->code;
8937 ns->code = init_st;
8939 /* Assign the default initializer to the l-value. */
8940 init_st->loc = sym->declared_at;
8941 init_st->op = EXEC_INIT_ASSIGN;
8942 init_st->expr1 = lval;
8943 init_st->expr2 = init;
8946 /* Assign the default initializer to a derived type variable or result. */
8948 static void
8949 apply_default_init (gfc_symbol *sym)
8951 gfc_expr *init = NULL;
8953 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
8954 return;
8956 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
8957 init = gfc_default_initializer (&sym->ts);
8959 if (init == NULL)
8960 return;
8962 build_init_assign (sym, init);
8965 /* Build an initializer for a local integer, real, complex, logical, or
8966 character variable, based on the command line flags finit-local-zero,
8967 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
8968 null if the symbol should not have a default initialization. */
8969 static gfc_expr *
8970 build_default_init_expr (gfc_symbol *sym)
8972 int char_len;
8973 gfc_expr *init_expr;
8974 int i;
8976 /* These symbols should never have a default initialization. */
8977 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
8978 || sym->attr.external
8979 || sym->attr.dummy
8980 || sym->attr.pointer
8981 || sym->attr.in_equivalence
8982 || sym->attr.in_common
8983 || sym->attr.data
8984 || sym->module
8985 || sym->attr.cray_pointee
8986 || sym->attr.cray_pointer)
8987 return NULL;
8989 /* Now we'll try to build an initializer expression. */
8990 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
8991 &sym->declared_at);
8993 /* We will only initialize integers, reals, complex, logicals, and
8994 characters, and only if the corresponding command-line flags
8995 were set. Otherwise, we free init_expr and return null. */
8996 switch (sym->ts.type)
8998 case BT_INTEGER:
8999 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
9000 mpz_init_set_si (init_expr->value.integer,
9001 gfc_option.flag_init_integer_value);
9002 else
9004 gfc_free_expr (init_expr);
9005 init_expr = NULL;
9007 break;
9009 case BT_REAL:
9010 mpfr_init (init_expr->value.real);
9011 switch (gfc_option.flag_init_real)
9013 case GFC_INIT_REAL_SNAN:
9014 init_expr->is_snan = 1;
9015 /* Fall through. */
9016 case GFC_INIT_REAL_NAN:
9017 mpfr_set_nan (init_expr->value.real);
9018 break;
9020 case GFC_INIT_REAL_INF:
9021 mpfr_set_inf (init_expr->value.real, 1);
9022 break;
9024 case GFC_INIT_REAL_NEG_INF:
9025 mpfr_set_inf (init_expr->value.real, -1);
9026 break;
9028 case GFC_INIT_REAL_ZERO:
9029 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
9030 break;
9032 default:
9033 gfc_free_expr (init_expr);
9034 init_expr = NULL;
9035 break;
9037 break;
9039 case BT_COMPLEX:
9040 mpc_init2 (init_expr->value.complex, mpfr_get_default_prec());
9041 switch (gfc_option.flag_init_real)
9043 case GFC_INIT_REAL_SNAN:
9044 init_expr->is_snan = 1;
9045 /* Fall through. */
9046 case GFC_INIT_REAL_NAN:
9047 mpfr_set_nan (mpc_realref (init_expr->value.complex));
9048 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
9049 break;
9051 case GFC_INIT_REAL_INF:
9052 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
9053 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
9054 break;
9056 case GFC_INIT_REAL_NEG_INF:
9057 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
9058 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
9059 break;
9061 case GFC_INIT_REAL_ZERO:
9062 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
9063 break;
9065 default:
9066 gfc_free_expr (init_expr);
9067 init_expr = NULL;
9068 break;
9070 break;
9072 case BT_LOGICAL:
9073 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
9074 init_expr->value.logical = 0;
9075 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
9076 init_expr->value.logical = 1;
9077 else
9079 gfc_free_expr (init_expr);
9080 init_expr = NULL;
9082 break;
9084 case BT_CHARACTER:
9085 /* For characters, the length must be constant in order to
9086 create a default initializer. */
9087 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
9088 && sym->ts.u.cl->length
9089 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9091 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
9092 init_expr->value.character.length = char_len;
9093 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
9094 for (i = 0; i < char_len; i++)
9095 init_expr->value.character.string[i]
9096 = (unsigned char) gfc_option.flag_init_character_value;
9098 else
9100 gfc_free_expr (init_expr);
9101 init_expr = NULL;
9103 break;
9105 default:
9106 gfc_free_expr (init_expr);
9107 init_expr = NULL;
9109 return init_expr;
9112 /* Add an initialization expression to a local variable. */
9113 static void
9114 apply_default_init_local (gfc_symbol *sym)
9116 gfc_expr *init = NULL;
9118 /* The symbol should be a variable or a function return value. */
9119 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9120 || (sym->attr.function && sym->result != sym))
9121 return;
9123 /* Try to build the initializer expression. If we can't initialize
9124 this symbol, then init will be NULL. */
9125 init = build_default_init_expr (sym);
9126 if (init == NULL)
9127 return;
9129 /* For saved variables, we don't want to add an initializer at
9130 function entry, so we just add a static initializer. */
9131 if (sym->attr.save || sym->ns->save_all
9132 || gfc_option.flag_max_stack_var_size == 0)
9134 /* Don't clobber an existing initializer! */
9135 gcc_assert (sym->value == NULL);
9136 sym->value = init;
9137 return;
9140 build_init_assign (sym, init);
9143 /* Resolution of common features of flavors variable and procedure. */
9145 static gfc_try
9146 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
9148 /* Constraints on deferred shape variable. */
9149 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
9151 if (sym->attr.allocatable)
9153 if (sym->attr.dimension)
9155 gfc_error ("Allocatable array '%s' at %L must have "
9156 "a deferred shape", sym->name, &sym->declared_at);
9157 return FAILURE;
9159 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
9160 "may not be ALLOCATABLE", sym->name,
9161 &sym->declared_at) == FAILURE)
9162 return FAILURE;
9165 if (sym->attr.pointer && sym->attr.dimension)
9167 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
9168 sym->name, &sym->declared_at);
9169 return FAILURE;
9173 else
9175 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
9176 && !sym->attr.dummy && sym->ts.type != BT_CLASS)
9178 gfc_error ("Array '%s' at %L cannot have a deferred shape",
9179 sym->name, &sym->declared_at);
9180 return FAILURE;
9184 /* Constraints on polymorphic variables. */
9185 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
9187 /* F03:C502. */
9188 if (!gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
9190 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
9191 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
9192 &sym->declared_at);
9193 return FAILURE;
9196 /* F03:C509. */
9197 /* Assume that use associated symbols were checked in the module ns. */
9198 if (!sym->attr.class_ok && !sym->attr.use_assoc)
9200 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
9201 "or pointer", sym->name, &sym->declared_at);
9202 return FAILURE;
9206 return SUCCESS;
9210 /* Additional checks for symbols with flavor variable and derived
9211 type. To be called from resolve_fl_variable. */
9213 static gfc_try
9214 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
9216 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
9218 /* Check to see if a derived type is blocked from being host
9219 associated by the presence of another class I symbol in the same
9220 namespace. 14.6.1.3 of the standard and the discussion on
9221 comp.lang.fortran. */
9222 if (sym->ns != sym->ts.u.derived->ns
9223 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
9225 gfc_symbol *s;
9226 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
9227 if (s && s->attr.flavor != FL_DERIVED)
9229 gfc_error ("The type '%s' cannot be host associated at %L "
9230 "because it is blocked by an incompatible object "
9231 "of the same name declared at %L",
9232 sym->ts.u.derived->name, &sym->declared_at,
9233 &s->declared_at);
9234 return FAILURE;
9238 /* 4th constraint in section 11.3: "If an object of a type for which
9239 component-initialization is specified (R429) appears in the
9240 specification-part of a module and does not have the ALLOCATABLE
9241 or POINTER attribute, the object shall have the SAVE attribute."
9243 The check for initializers is performed with
9244 gfc_has_default_initializer because gfc_default_initializer generates
9245 a hidden default for allocatable components. */
9246 if (!(sym->value || no_init_flag) && sym->ns->proc_name
9247 && sym->ns->proc_name->attr.flavor == FL_MODULE
9248 && !sym->ns->save_all && !sym->attr.save
9249 && !sym->attr.pointer && !sym->attr.allocatable
9250 && gfc_has_default_initializer (sym->ts.u.derived)
9251 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Implied SAVE for "
9252 "module variable '%s' at %L, needed due to "
9253 "the default initialization", sym->name,
9254 &sym->declared_at) == FAILURE)
9255 return FAILURE;
9257 /* Assign default initializer. */
9258 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
9259 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
9261 sym->value = gfc_default_initializer (&sym->ts);
9264 return SUCCESS;
9268 /* Resolve symbols with flavor variable. */
9270 static gfc_try
9271 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
9273 int no_init_flag, automatic_flag;
9274 gfc_expr *e;
9275 const char *auto_save_msg;
9277 auto_save_msg = "Automatic object '%s' at %L cannot have the "
9278 "SAVE attribute";
9280 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9281 return FAILURE;
9283 /* Set this flag to check that variables are parameters of all entries.
9284 This check is effected by the call to gfc_resolve_expr through
9285 is_non_constant_shape_array. */
9286 specification_expr = 1;
9288 if (sym->ns->proc_name
9289 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9290 || sym->ns->proc_name->attr.is_main_program)
9291 && !sym->attr.use_assoc
9292 && !sym->attr.allocatable
9293 && !sym->attr.pointer
9294 && is_non_constant_shape_array (sym))
9296 /* The shape of a main program or module array needs to be
9297 constant. */
9298 gfc_error ("The module or main program array '%s' at %L must "
9299 "have constant shape", sym->name, &sym->declared_at);
9300 specification_expr = 0;
9301 return FAILURE;
9304 if (sym->ts.type == BT_CHARACTER)
9306 /* Make sure that character string variables with assumed length are
9307 dummy arguments. */
9308 e = sym->ts.u.cl->length;
9309 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
9311 gfc_error ("Entity with assumed character length at %L must be a "
9312 "dummy argument or a PARAMETER", &sym->declared_at);
9313 return FAILURE;
9316 if (e && sym->attr.save && !gfc_is_constant_expr (e))
9318 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9319 return FAILURE;
9322 if (!gfc_is_constant_expr (e)
9323 && !(e->expr_type == EXPR_VARIABLE
9324 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
9325 && sym->ns->proc_name
9326 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9327 || sym->ns->proc_name->attr.is_main_program)
9328 && !sym->attr.use_assoc)
9330 gfc_error ("'%s' at %L must have constant character length "
9331 "in this context", sym->name, &sym->declared_at);
9332 return FAILURE;
9336 if (sym->value == NULL && sym->attr.referenced)
9337 apply_default_init_local (sym); /* Try to apply a default initialization. */
9339 /* Determine if the symbol may not have an initializer. */
9340 no_init_flag = automatic_flag = 0;
9341 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
9342 || sym->attr.intrinsic || sym->attr.result)
9343 no_init_flag = 1;
9344 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
9345 && is_non_constant_shape_array (sym))
9347 no_init_flag = automatic_flag = 1;
9349 /* Also, they must not have the SAVE attribute.
9350 SAVE_IMPLICIT is checked below. */
9351 if (sym->attr.save == SAVE_EXPLICIT)
9353 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9354 return FAILURE;
9358 /* Ensure that any initializer is simplified. */
9359 if (sym->value)
9360 gfc_simplify_expr (sym->value, 1);
9362 /* Reject illegal initializers. */
9363 if (!sym->mark && sym->value)
9365 if (sym->attr.allocatable)
9366 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
9367 sym->name, &sym->declared_at);
9368 else if (sym->attr.external)
9369 gfc_error ("External '%s' at %L cannot have an initializer",
9370 sym->name, &sym->declared_at);
9371 else if (sym->attr.dummy
9372 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
9373 gfc_error ("Dummy '%s' at %L cannot have an initializer",
9374 sym->name, &sym->declared_at);
9375 else if (sym->attr.intrinsic)
9376 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
9377 sym->name, &sym->declared_at);
9378 else if (sym->attr.result)
9379 gfc_error ("Function result '%s' at %L cannot have an initializer",
9380 sym->name, &sym->declared_at);
9381 else if (automatic_flag)
9382 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
9383 sym->name, &sym->declared_at);
9384 else
9385 goto no_init_error;
9386 return FAILURE;
9389 no_init_error:
9390 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
9391 return resolve_fl_variable_derived (sym, no_init_flag);
9393 return SUCCESS;
9397 /* Resolve a procedure. */
9399 static gfc_try
9400 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
9402 gfc_formal_arglist *arg;
9404 if (sym->attr.function
9405 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9406 return FAILURE;
9408 if (sym->ts.type == BT_CHARACTER)
9410 gfc_charlen *cl = sym->ts.u.cl;
9412 if (cl && cl->length && gfc_is_constant_expr (cl->length)
9413 && resolve_charlen (cl) == FAILURE)
9414 return FAILURE;
9416 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
9417 && sym->attr.proc == PROC_ST_FUNCTION)
9419 gfc_error ("Character-valued statement function '%s' at %L must "
9420 "have constant length", sym->name, &sym->declared_at);
9421 return FAILURE;
9425 /* Ensure that derived type for are not of a private type. Internal
9426 module procedures are excluded by 2.2.3.3 - i.e., they are not
9427 externally accessible and can access all the objects accessible in
9428 the host. */
9429 if (!(sym->ns->parent
9430 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
9431 && gfc_check_access(sym->attr.access, sym->ns->default_access))
9433 gfc_interface *iface;
9435 for (arg = sym->formal; arg; arg = arg->next)
9437 if (arg->sym
9438 && arg->sym->ts.type == BT_DERIVED
9439 && !arg->sym->ts.u.derived->attr.use_assoc
9440 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9441 arg->sym->ts.u.derived->ns->default_access)
9442 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
9443 "PRIVATE type and cannot be a dummy argument"
9444 " of '%s', which is PUBLIC at %L",
9445 arg->sym->name, sym->name, &sym->declared_at)
9446 == FAILURE)
9448 /* Stop this message from recurring. */
9449 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9450 return FAILURE;
9454 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9455 PRIVATE to the containing module. */
9456 for (iface = sym->generic; iface; iface = iface->next)
9458 for (arg = iface->sym->formal; arg; arg = arg->next)
9460 if (arg->sym
9461 && arg->sym->ts.type == BT_DERIVED
9462 && !arg->sym->ts.u.derived->attr.use_assoc
9463 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9464 arg->sym->ts.u.derived->ns->default_access)
9465 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9466 "'%s' in PUBLIC interface '%s' at %L "
9467 "takes dummy arguments of '%s' which is "
9468 "PRIVATE", iface->sym->name, sym->name,
9469 &iface->sym->declared_at,
9470 gfc_typename (&arg->sym->ts)) == FAILURE)
9472 /* Stop this message from recurring. */
9473 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9474 return FAILURE;
9479 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9480 PRIVATE to the containing module. */
9481 for (iface = sym->generic; iface; iface = iface->next)
9483 for (arg = iface->sym->formal; arg; arg = arg->next)
9485 if (arg->sym
9486 && arg->sym->ts.type == BT_DERIVED
9487 && !arg->sym->ts.u.derived->attr.use_assoc
9488 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9489 arg->sym->ts.u.derived->ns->default_access)
9490 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9491 "'%s' in PUBLIC interface '%s' at %L "
9492 "takes dummy arguments of '%s' which is "
9493 "PRIVATE", iface->sym->name, sym->name,
9494 &iface->sym->declared_at,
9495 gfc_typename (&arg->sym->ts)) == FAILURE)
9497 /* Stop this message from recurring. */
9498 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9499 return FAILURE;
9505 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
9506 && !sym->attr.proc_pointer)
9508 gfc_error ("Function '%s' at %L cannot have an initializer",
9509 sym->name, &sym->declared_at);
9510 return FAILURE;
9513 /* An external symbol may not have an initializer because it is taken to be
9514 a procedure. Exception: Procedure Pointers. */
9515 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
9517 gfc_error ("External object '%s' at %L may not have an initializer",
9518 sym->name, &sym->declared_at);
9519 return FAILURE;
9522 /* An elemental function is required to return a scalar 12.7.1 */
9523 if (sym->attr.elemental && sym->attr.function && sym->as)
9525 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
9526 "result", sym->name, &sym->declared_at);
9527 /* Reset so that the error only occurs once. */
9528 sym->attr.elemental = 0;
9529 return FAILURE;
9532 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
9533 char-len-param shall not be array-valued, pointer-valued, recursive
9534 or pure. ....snip... A character value of * may only be used in the
9535 following ways: (i) Dummy arg of procedure - dummy associates with
9536 actual length; (ii) To declare a named constant; or (iii) External
9537 function - but length must be declared in calling scoping unit. */
9538 if (sym->attr.function
9539 && sym->ts.type == BT_CHARACTER
9540 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
9542 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
9543 || (sym->attr.recursive) || (sym->attr.pure))
9545 if (sym->as && sym->as->rank)
9546 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9547 "array-valued", sym->name, &sym->declared_at);
9549 if (sym->attr.pointer)
9550 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9551 "pointer-valued", sym->name, &sym->declared_at);
9553 if (sym->attr.pure)
9554 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9555 "pure", sym->name, &sym->declared_at);
9557 if (sym->attr.recursive)
9558 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9559 "recursive", sym->name, &sym->declared_at);
9561 return FAILURE;
9564 /* Appendix B.2 of the standard. Contained functions give an
9565 error anyway. Fixed-form is likely to be F77/legacy. */
9566 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
9567 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
9568 "CHARACTER(*) function '%s' at %L",
9569 sym->name, &sym->declared_at);
9572 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
9574 gfc_formal_arglist *curr_arg;
9575 int has_non_interop_arg = 0;
9577 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
9578 sym->common_block) == FAILURE)
9580 /* Clear these to prevent looking at them again if there was an
9581 error. */
9582 sym->attr.is_bind_c = 0;
9583 sym->attr.is_c_interop = 0;
9584 sym->ts.is_c_interop = 0;
9586 else
9588 /* So far, no errors have been found. */
9589 sym->attr.is_c_interop = 1;
9590 sym->ts.is_c_interop = 1;
9593 curr_arg = sym->formal;
9594 while (curr_arg != NULL)
9596 /* Skip implicitly typed dummy args here. */
9597 if (curr_arg->sym->attr.implicit_type == 0)
9598 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
9599 /* If something is found to fail, record the fact so we
9600 can mark the symbol for the procedure as not being
9601 BIND(C) to try and prevent multiple errors being
9602 reported. */
9603 has_non_interop_arg = 1;
9605 curr_arg = curr_arg->next;
9608 /* See if any of the arguments were not interoperable and if so, clear
9609 the procedure symbol to prevent duplicate error messages. */
9610 if (has_non_interop_arg != 0)
9612 sym->attr.is_c_interop = 0;
9613 sym->ts.is_c_interop = 0;
9614 sym->attr.is_bind_c = 0;
9618 if (!sym->attr.proc_pointer)
9620 if (sym->attr.save == SAVE_EXPLICIT)
9622 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
9623 "in '%s' at %L", sym->name, &sym->declared_at);
9624 return FAILURE;
9626 if (sym->attr.intent)
9628 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
9629 "in '%s' at %L", sym->name, &sym->declared_at);
9630 return FAILURE;
9632 if (sym->attr.subroutine && sym->attr.result)
9634 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
9635 "in '%s' at %L", sym->name, &sym->declared_at);
9636 return FAILURE;
9638 if (sym->attr.external && sym->attr.function
9639 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
9640 || sym->attr.contained))
9642 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
9643 "in '%s' at %L", sym->name, &sym->declared_at);
9644 return FAILURE;
9646 if (strcmp ("ppr@", sym->name) == 0)
9648 gfc_error ("Procedure pointer result '%s' at %L "
9649 "is missing the pointer attribute",
9650 sym->ns->proc_name->name, &sym->declared_at);
9651 return FAILURE;
9655 return SUCCESS;
9659 /* Resolve a list of finalizer procedures. That is, after they have hopefully
9660 been defined and we now know their defined arguments, check that they fulfill
9661 the requirements of the standard for procedures used as finalizers. */
9663 static gfc_try
9664 gfc_resolve_finalizers (gfc_symbol* derived)
9666 gfc_finalizer* list;
9667 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
9668 gfc_try result = SUCCESS;
9669 bool seen_scalar = false;
9671 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
9672 return SUCCESS;
9674 /* Walk over the list of finalizer-procedures, check them, and if any one
9675 does not fit in with the standard's definition, print an error and remove
9676 it from the list. */
9677 prev_link = &derived->f2k_derived->finalizers;
9678 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
9680 gfc_symbol* arg;
9681 gfc_finalizer* i;
9682 int my_rank;
9684 /* Skip this finalizer if we already resolved it. */
9685 if (list->proc_tree)
9687 prev_link = &(list->next);
9688 continue;
9691 /* Check this exists and is a SUBROUTINE. */
9692 if (!list->proc_sym->attr.subroutine)
9694 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
9695 list->proc_sym->name, &list->where);
9696 goto error;
9699 /* We should have exactly one argument. */
9700 if (!list->proc_sym->formal || list->proc_sym->formal->next)
9702 gfc_error ("FINAL procedure at %L must have exactly one argument",
9703 &list->where);
9704 goto error;
9706 arg = list->proc_sym->formal->sym;
9708 /* This argument must be of our type. */
9709 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
9711 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
9712 &arg->declared_at, derived->name);
9713 goto error;
9716 /* It must neither be a pointer nor allocatable nor optional. */
9717 if (arg->attr.pointer)
9719 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
9720 &arg->declared_at);
9721 goto error;
9723 if (arg->attr.allocatable)
9725 gfc_error ("Argument of FINAL procedure at %L must not be"
9726 " ALLOCATABLE", &arg->declared_at);
9727 goto error;
9729 if (arg->attr.optional)
9731 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
9732 &arg->declared_at);
9733 goto error;
9736 /* It must not be INTENT(OUT). */
9737 if (arg->attr.intent == INTENT_OUT)
9739 gfc_error ("Argument of FINAL procedure at %L must not be"
9740 " INTENT(OUT)", &arg->declared_at);
9741 goto error;
9744 /* Warn if the procedure is non-scalar and not assumed shape. */
9745 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
9746 && arg->as->type != AS_ASSUMED_SHAPE)
9747 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
9748 " shape argument", &arg->declared_at);
9750 /* Check that it does not match in kind and rank with a FINAL procedure
9751 defined earlier. To really loop over the *earlier* declarations,
9752 we need to walk the tail of the list as new ones were pushed at the
9753 front. */
9754 /* TODO: Handle kind parameters once they are implemented. */
9755 my_rank = (arg->as ? arg->as->rank : 0);
9756 for (i = list->next; i; i = i->next)
9758 /* Argument list might be empty; that is an error signalled earlier,
9759 but we nevertheless continued resolving. */
9760 if (i->proc_sym->formal)
9762 gfc_symbol* i_arg = i->proc_sym->formal->sym;
9763 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
9764 if (i_rank == my_rank)
9766 gfc_error ("FINAL procedure '%s' declared at %L has the same"
9767 " rank (%d) as '%s'",
9768 list->proc_sym->name, &list->where, my_rank,
9769 i->proc_sym->name);
9770 goto error;
9775 /* Is this the/a scalar finalizer procedure? */
9776 if (!arg->as || arg->as->rank == 0)
9777 seen_scalar = true;
9779 /* Find the symtree for this procedure. */
9780 gcc_assert (!list->proc_tree);
9781 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
9783 prev_link = &list->next;
9784 continue;
9786 /* Remove wrong nodes immediately from the list so we don't risk any
9787 troubles in the future when they might fail later expectations. */
9788 error:
9789 result = FAILURE;
9790 i = list;
9791 *prev_link = list->next;
9792 gfc_free_finalizer (i);
9795 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
9796 were nodes in the list, must have been for arrays. It is surely a good
9797 idea to have a scalar version there if there's something to finalize. */
9798 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
9799 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
9800 " defined at %L, suggest also scalar one",
9801 derived->name, &derived->declared_at);
9803 /* TODO: Remove this error when finalization is finished. */
9804 gfc_error ("Finalization at %L is not yet implemented",
9805 &derived->declared_at);
9807 return result;
9811 /* Check that it is ok for the typebound procedure proc to override the
9812 procedure old. */
9814 static gfc_try
9815 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
9817 locus where;
9818 const gfc_symbol* proc_target;
9819 const gfc_symbol* old_target;
9820 unsigned proc_pass_arg, old_pass_arg, argpos;
9821 gfc_formal_arglist* proc_formal;
9822 gfc_formal_arglist* old_formal;
9824 /* This procedure should only be called for non-GENERIC proc. */
9825 gcc_assert (!proc->n.tb->is_generic);
9827 /* If the overwritten procedure is GENERIC, this is an error. */
9828 if (old->n.tb->is_generic)
9830 gfc_error ("Can't overwrite GENERIC '%s' at %L",
9831 old->name, &proc->n.tb->where);
9832 return FAILURE;
9835 where = proc->n.tb->where;
9836 proc_target = proc->n.tb->u.specific->n.sym;
9837 old_target = old->n.tb->u.specific->n.sym;
9839 /* Check that overridden binding is not NON_OVERRIDABLE. */
9840 if (old->n.tb->non_overridable)
9842 gfc_error ("'%s' at %L overrides a procedure binding declared"
9843 " NON_OVERRIDABLE", proc->name, &where);
9844 return FAILURE;
9847 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
9848 if (!old->n.tb->deferred && proc->n.tb->deferred)
9850 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
9851 " non-DEFERRED binding", proc->name, &where);
9852 return FAILURE;
9855 /* If the overridden binding is PURE, the overriding must be, too. */
9856 if (old_target->attr.pure && !proc_target->attr.pure)
9858 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
9859 proc->name, &where);
9860 return FAILURE;
9863 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
9864 is not, the overriding must not be either. */
9865 if (old_target->attr.elemental && !proc_target->attr.elemental)
9867 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
9868 " ELEMENTAL", proc->name, &where);
9869 return FAILURE;
9871 if (!old_target->attr.elemental && proc_target->attr.elemental)
9873 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
9874 " be ELEMENTAL, either", proc->name, &where);
9875 return FAILURE;
9878 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
9879 SUBROUTINE. */
9880 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
9882 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
9883 " SUBROUTINE", proc->name, &where);
9884 return FAILURE;
9887 /* If the overridden binding is a FUNCTION, the overriding must also be a
9888 FUNCTION and have the same characteristics. */
9889 if (old_target->attr.function)
9891 if (!proc_target->attr.function)
9893 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
9894 " FUNCTION", proc->name, &where);
9895 return FAILURE;
9898 /* FIXME: Do more comprehensive checking (including, for instance, the
9899 rank and array-shape). */
9900 gcc_assert (proc_target->result && old_target->result);
9901 if (!gfc_compare_types (&proc_target->result->ts,
9902 &old_target->result->ts))
9904 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
9905 " matching result types", proc->name, &where);
9906 return FAILURE;
9910 /* If the overridden binding is PUBLIC, the overriding one must not be
9911 PRIVATE. */
9912 if (old->n.tb->access == ACCESS_PUBLIC
9913 && proc->n.tb->access == ACCESS_PRIVATE)
9915 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
9916 " PRIVATE", proc->name, &where);
9917 return FAILURE;
9920 /* Compare the formal argument lists of both procedures. This is also abused
9921 to find the position of the passed-object dummy arguments of both
9922 bindings as at least the overridden one might not yet be resolved and we
9923 need those positions in the check below. */
9924 proc_pass_arg = old_pass_arg = 0;
9925 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
9926 proc_pass_arg = 1;
9927 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
9928 old_pass_arg = 1;
9929 argpos = 1;
9930 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
9931 proc_formal && old_formal;
9932 proc_formal = proc_formal->next, old_formal = old_formal->next)
9934 if (proc->n.tb->pass_arg
9935 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
9936 proc_pass_arg = argpos;
9937 if (old->n.tb->pass_arg
9938 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
9939 old_pass_arg = argpos;
9941 /* Check that the names correspond. */
9942 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
9944 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
9945 " to match the corresponding argument of the overridden"
9946 " procedure", proc_formal->sym->name, proc->name, &where,
9947 old_formal->sym->name);
9948 return FAILURE;
9951 /* Check that the types correspond if neither is the passed-object
9952 argument. */
9953 /* FIXME: Do more comprehensive testing here. */
9954 if (proc_pass_arg != argpos && old_pass_arg != argpos
9955 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
9957 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
9958 "in respect to the overridden procedure",
9959 proc_formal->sym->name, proc->name, &where);
9960 return FAILURE;
9963 ++argpos;
9965 if (proc_formal || old_formal)
9967 gfc_error ("'%s' at %L must have the same number of formal arguments as"
9968 " the overridden procedure", proc->name, &where);
9969 return FAILURE;
9972 /* If the overridden binding is NOPASS, the overriding one must also be
9973 NOPASS. */
9974 if (old->n.tb->nopass && !proc->n.tb->nopass)
9976 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
9977 " NOPASS", proc->name, &where);
9978 return FAILURE;
9981 /* If the overridden binding is PASS(x), the overriding one must also be
9982 PASS and the passed-object dummy arguments must correspond. */
9983 if (!old->n.tb->nopass)
9985 if (proc->n.tb->nopass)
9987 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
9988 " PASS", proc->name, &where);
9989 return FAILURE;
9992 if (proc_pass_arg != old_pass_arg)
9994 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
9995 " the same position as the passed-object dummy argument of"
9996 " the overridden procedure", proc->name, &where);
9997 return FAILURE;
10001 return SUCCESS;
10005 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
10007 static gfc_try
10008 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
10009 const char* generic_name, locus where)
10011 gfc_symbol* sym1;
10012 gfc_symbol* sym2;
10014 gcc_assert (t1->specific && t2->specific);
10015 gcc_assert (!t1->specific->is_generic);
10016 gcc_assert (!t2->specific->is_generic);
10018 sym1 = t1->specific->u.specific->n.sym;
10019 sym2 = t2->specific->u.specific->n.sym;
10021 if (sym1 == sym2)
10022 return SUCCESS;
10024 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
10025 if (sym1->attr.subroutine != sym2->attr.subroutine
10026 || sym1->attr.function != sym2->attr.function)
10028 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
10029 " GENERIC '%s' at %L",
10030 sym1->name, sym2->name, generic_name, &where);
10031 return FAILURE;
10034 /* Compare the interfaces. */
10035 if (gfc_compare_interfaces (sym1, sym2, sym2->name, 1, 0, NULL, 0))
10037 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
10038 sym1->name, sym2->name, generic_name, &where);
10039 return FAILURE;
10042 return SUCCESS;
10046 /* Worker function for resolving a generic procedure binding; this is used to
10047 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
10049 The difference between those cases is finding possible inherited bindings
10050 that are overridden, as one has to look for them in tb_sym_root,
10051 tb_uop_root or tb_op, respectively. Thus the caller must already find
10052 the super-type and set p->overridden correctly. */
10054 static gfc_try
10055 resolve_tb_generic_targets (gfc_symbol* super_type,
10056 gfc_typebound_proc* p, const char* name)
10058 gfc_tbp_generic* target;
10059 gfc_symtree* first_target;
10060 gfc_symtree* inherited;
10062 gcc_assert (p && p->is_generic);
10064 /* Try to find the specific bindings for the symtrees in our target-list. */
10065 gcc_assert (p->u.generic);
10066 for (target = p->u.generic; target; target = target->next)
10067 if (!target->specific)
10069 gfc_typebound_proc* overridden_tbp;
10070 gfc_tbp_generic* g;
10071 const char* target_name;
10073 target_name = target->specific_st->name;
10075 /* Defined for this type directly. */
10076 if (target->specific_st->n.tb)
10078 target->specific = target->specific_st->n.tb;
10079 goto specific_found;
10082 /* Look for an inherited specific binding. */
10083 if (super_type)
10085 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
10086 true, NULL);
10088 if (inherited)
10090 gcc_assert (inherited->n.tb);
10091 target->specific = inherited->n.tb;
10092 goto specific_found;
10096 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
10097 " at %L", target_name, name, &p->where);
10098 return FAILURE;
10100 /* Once we've found the specific binding, check it is not ambiguous with
10101 other specifics already found or inherited for the same GENERIC. */
10102 specific_found:
10103 gcc_assert (target->specific);
10105 /* This must really be a specific binding! */
10106 if (target->specific->is_generic)
10108 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
10109 " '%s' is GENERIC, too", name, &p->where, target_name);
10110 return FAILURE;
10113 /* Check those already resolved on this type directly. */
10114 for (g = p->u.generic; g; g = g->next)
10115 if (g != target && g->specific
10116 && check_generic_tbp_ambiguity (target, g, name, p->where)
10117 == FAILURE)
10118 return FAILURE;
10120 /* Check for ambiguity with inherited specific targets. */
10121 for (overridden_tbp = p->overridden; overridden_tbp;
10122 overridden_tbp = overridden_tbp->overridden)
10123 if (overridden_tbp->is_generic)
10125 for (g = overridden_tbp->u.generic; g; g = g->next)
10127 gcc_assert (g->specific);
10128 if (check_generic_tbp_ambiguity (target, g,
10129 name, p->where) == FAILURE)
10130 return FAILURE;
10135 /* If we attempt to "overwrite" a specific binding, this is an error. */
10136 if (p->overridden && !p->overridden->is_generic)
10138 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
10139 " the same name", name, &p->where);
10140 return FAILURE;
10143 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
10144 all must have the same attributes here. */
10145 first_target = p->u.generic->specific->u.specific;
10146 gcc_assert (first_target);
10147 p->subroutine = first_target->n.sym->attr.subroutine;
10148 p->function = first_target->n.sym->attr.function;
10150 return SUCCESS;
10154 /* Resolve a GENERIC procedure binding for a derived type. */
10156 static gfc_try
10157 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
10159 gfc_symbol* super_type;
10161 /* Find the overridden binding if any. */
10162 st->n.tb->overridden = NULL;
10163 super_type = gfc_get_derived_super_type (derived);
10164 if (super_type)
10166 gfc_symtree* overridden;
10167 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
10168 true, NULL);
10170 if (overridden && overridden->n.tb)
10171 st->n.tb->overridden = overridden->n.tb;
10174 /* Resolve using worker function. */
10175 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
10179 /* Retrieve the target-procedure of an operator binding and do some checks in
10180 common for intrinsic and user-defined type-bound operators. */
10182 static gfc_symbol*
10183 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
10185 gfc_symbol* target_proc;
10187 gcc_assert (target->specific && !target->specific->is_generic);
10188 target_proc = target->specific->u.specific->n.sym;
10189 gcc_assert (target_proc);
10191 /* All operator bindings must have a passed-object dummy argument. */
10192 if (target->specific->nopass)
10194 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
10195 return NULL;
10198 return target_proc;
10202 /* Resolve a type-bound intrinsic operator. */
10204 static gfc_try
10205 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
10206 gfc_typebound_proc* p)
10208 gfc_symbol* super_type;
10209 gfc_tbp_generic* target;
10211 /* If there's already an error here, do nothing (but don't fail again). */
10212 if (p->error)
10213 return SUCCESS;
10215 /* Operators should always be GENERIC bindings. */
10216 gcc_assert (p->is_generic);
10218 /* Look for an overridden binding. */
10219 super_type = gfc_get_derived_super_type (derived);
10220 if (super_type && super_type->f2k_derived)
10221 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
10222 op, true, NULL);
10223 else
10224 p->overridden = NULL;
10226 /* Resolve general GENERIC properties using worker function. */
10227 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
10228 goto error;
10230 /* Check the targets to be procedures of correct interface. */
10231 for (target = p->u.generic; target; target = target->next)
10233 gfc_symbol* target_proc;
10235 target_proc = get_checked_tb_operator_target (target, p->where);
10236 if (!target_proc)
10237 goto error;
10239 if (!gfc_check_operator_interface (target_proc, op, p->where))
10240 goto error;
10243 return SUCCESS;
10245 error:
10246 p->error = 1;
10247 return FAILURE;
10251 /* Resolve a type-bound user operator (tree-walker callback). */
10253 static gfc_symbol* resolve_bindings_derived;
10254 static gfc_try resolve_bindings_result;
10256 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
10258 static void
10259 resolve_typebound_user_op (gfc_symtree* stree)
10261 gfc_symbol* super_type;
10262 gfc_tbp_generic* target;
10264 gcc_assert (stree && stree->n.tb);
10266 if (stree->n.tb->error)
10267 return;
10269 /* Operators should always be GENERIC bindings. */
10270 gcc_assert (stree->n.tb->is_generic);
10272 /* Find overridden procedure, if any. */
10273 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10274 if (super_type && super_type->f2k_derived)
10276 gfc_symtree* overridden;
10277 overridden = gfc_find_typebound_user_op (super_type, NULL,
10278 stree->name, true, NULL);
10280 if (overridden && overridden->n.tb)
10281 stree->n.tb->overridden = overridden->n.tb;
10283 else
10284 stree->n.tb->overridden = NULL;
10286 /* Resolve basically using worker function. */
10287 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
10288 == FAILURE)
10289 goto error;
10291 /* Check the targets to be functions of correct interface. */
10292 for (target = stree->n.tb->u.generic; target; target = target->next)
10294 gfc_symbol* target_proc;
10296 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
10297 if (!target_proc)
10298 goto error;
10300 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
10301 goto error;
10304 return;
10306 error:
10307 resolve_bindings_result = FAILURE;
10308 stree->n.tb->error = 1;
10312 /* Resolve the type-bound procedures for a derived type. */
10314 static void
10315 resolve_typebound_procedure (gfc_symtree* stree)
10317 gfc_symbol* proc;
10318 locus where;
10319 gfc_symbol* me_arg;
10320 gfc_symbol* super_type;
10321 gfc_component* comp;
10323 gcc_assert (stree);
10325 /* Undefined specific symbol from GENERIC target definition. */
10326 if (!stree->n.tb)
10327 return;
10329 if (stree->n.tb->error)
10330 return;
10332 /* If this is a GENERIC binding, use that routine. */
10333 if (stree->n.tb->is_generic)
10335 if (resolve_typebound_generic (resolve_bindings_derived, stree)
10336 == FAILURE)
10337 goto error;
10338 return;
10341 /* Get the target-procedure to check it. */
10342 gcc_assert (!stree->n.tb->is_generic);
10343 gcc_assert (stree->n.tb->u.specific);
10344 proc = stree->n.tb->u.specific->n.sym;
10345 where = stree->n.tb->where;
10347 /* Default access should already be resolved from the parser. */
10348 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
10350 /* It should be a module procedure or an external procedure with explicit
10351 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
10352 if ((!proc->attr.subroutine && !proc->attr.function)
10353 || (proc->attr.proc != PROC_MODULE
10354 && proc->attr.if_source != IFSRC_IFBODY)
10355 || (proc->attr.abstract && !stree->n.tb->deferred))
10357 gfc_error ("'%s' must be a module procedure or an external procedure with"
10358 " an explicit interface at %L", proc->name, &where);
10359 goto error;
10361 stree->n.tb->subroutine = proc->attr.subroutine;
10362 stree->n.tb->function = proc->attr.function;
10364 /* Find the super-type of the current derived type. We could do this once and
10365 store in a global if speed is needed, but as long as not I believe this is
10366 more readable and clearer. */
10367 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10369 /* If PASS, resolve and check arguments if not already resolved / loaded
10370 from a .mod file. */
10371 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
10373 if (stree->n.tb->pass_arg)
10375 gfc_formal_arglist* i;
10377 /* If an explicit passing argument name is given, walk the arg-list
10378 and look for it. */
10380 me_arg = NULL;
10381 stree->n.tb->pass_arg_num = 1;
10382 for (i = proc->formal; i; i = i->next)
10384 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
10386 me_arg = i->sym;
10387 break;
10389 ++stree->n.tb->pass_arg_num;
10392 if (!me_arg)
10394 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
10395 " argument '%s'",
10396 proc->name, stree->n.tb->pass_arg, &where,
10397 stree->n.tb->pass_arg);
10398 goto error;
10401 else
10403 /* Otherwise, take the first one; there should in fact be at least
10404 one. */
10405 stree->n.tb->pass_arg_num = 1;
10406 if (!proc->formal)
10408 gfc_error ("Procedure '%s' with PASS at %L must have at"
10409 " least one argument", proc->name, &where);
10410 goto error;
10412 me_arg = proc->formal->sym;
10415 /* Now check that the argument-type matches and the passed-object
10416 dummy argument is generally fine. */
10418 gcc_assert (me_arg);
10420 if (me_arg->ts.type != BT_CLASS)
10422 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10423 " at %L", proc->name, &where);
10424 goto error;
10427 if (CLASS_DATA (me_arg)->ts.u.derived
10428 != resolve_bindings_derived)
10430 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10431 " the derived-type '%s'", me_arg->name, proc->name,
10432 me_arg->name, &where, resolve_bindings_derived->name);
10433 goto error;
10436 gcc_assert (me_arg->ts.type == BT_CLASS);
10437 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank > 0)
10439 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
10440 " scalar", proc->name, &where);
10441 goto error;
10443 if (CLASS_DATA (me_arg)->attr.allocatable)
10445 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10446 " be ALLOCATABLE", proc->name, &where);
10447 goto error;
10449 if (CLASS_DATA (me_arg)->attr.class_pointer)
10451 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10452 " be POINTER", proc->name, &where);
10453 goto error;
10457 /* If we are extending some type, check that we don't override a procedure
10458 flagged NON_OVERRIDABLE. */
10459 stree->n.tb->overridden = NULL;
10460 if (super_type)
10462 gfc_symtree* overridden;
10463 overridden = gfc_find_typebound_proc (super_type, NULL,
10464 stree->name, true, NULL);
10466 if (overridden && overridden->n.tb)
10467 stree->n.tb->overridden = overridden->n.tb;
10469 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
10470 goto error;
10473 /* See if there's a name collision with a component directly in this type. */
10474 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
10475 if (!strcmp (comp->name, stree->name))
10477 gfc_error ("Procedure '%s' at %L has the same name as a component of"
10478 " '%s'",
10479 stree->name, &where, resolve_bindings_derived->name);
10480 goto error;
10483 /* Try to find a name collision with an inherited component. */
10484 if (super_type && gfc_find_component (super_type, stree->name, true, true))
10486 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
10487 " component of '%s'",
10488 stree->name, &where, resolve_bindings_derived->name);
10489 goto error;
10492 stree->n.tb->error = 0;
10493 return;
10495 error:
10496 resolve_bindings_result = FAILURE;
10497 stree->n.tb->error = 1;
10500 static gfc_try
10501 resolve_typebound_procedures (gfc_symbol* derived)
10503 int op;
10505 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
10506 return SUCCESS;
10508 resolve_bindings_derived = derived;
10509 resolve_bindings_result = SUCCESS;
10511 if (derived->f2k_derived->tb_sym_root)
10512 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
10513 &resolve_typebound_procedure);
10515 if (derived->f2k_derived->tb_uop_root)
10516 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
10517 &resolve_typebound_user_op);
10519 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
10521 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
10522 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
10523 p) == FAILURE)
10524 resolve_bindings_result = FAILURE;
10527 return resolve_bindings_result;
10531 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
10532 to give all identical derived types the same backend_decl. */
10533 static void
10534 add_dt_to_dt_list (gfc_symbol *derived)
10536 gfc_dt_list *dt_list;
10538 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
10539 if (derived == dt_list->derived)
10540 break;
10542 if (dt_list == NULL)
10544 dt_list = gfc_get_dt_list ();
10545 dt_list->next = gfc_derived_types;
10546 dt_list->derived = derived;
10547 gfc_derived_types = dt_list;
10552 /* Ensure that a derived-type is really not abstract, meaning that every
10553 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
10555 static gfc_try
10556 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
10558 if (!st)
10559 return SUCCESS;
10561 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
10562 return FAILURE;
10563 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
10564 return FAILURE;
10566 if (st->n.tb && st->n.tb->deferred)
10568 gfc_symtree* overriding;
10569 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
10570 if (!overriding)
10571 return FAILURE;
10572 gcc_assert (overriding->n.tb);
10573 if (overriding->n.tb->deferred)
10575 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
10576 " '%s' is DEFERRED and not overridden",
10577 sub->name, &sub->declared_at, st->name);
10578 return FAILURE;
10582 return SUCCESS;
10585 static gfc_try
10586 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
10588 /* The algorithm used here is to recursively travel up the ancestry of sub
10589 and for each ancestor-type, check all bindings. If any of them is
10590 DEFERRED, look it up starting from sub and see if the found (overriding)
10591 binding is not DEFERRED.
10592 This is not the most efficient way to do this, but it should be ok and is
10593 clearer than something sophisticated. */
10595 gcc_assert (ancestor && !sub->attr.abstract);
10597 if (!ancestor->attr.abstract)
10598 return SUCCESS;
10600 /* Walk bindings of this ancestor. */
10601 if (ancestor->f2k_derived)
10603 gfc_try t;
10604 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
10605 if (t == FAILURE)
10606 return FAILURE;
10609 /* Find next ancestor type and recurse on it. */
10610 ancestor = gfc_get_derived_super_type (ancestor);
10611 if (ancestor)
10612 return ensure_not_abstract (sub, ancestor);
10614 return SUCCESS;
10618 static void resolve_symbol (gfc_symbol *sym);
10621 /* Resolve the components of a derived type. */
10623 static gfc_try
10624 resolve_fl_derived (gfc_symbol *sym)
10626 gfc_symbol* super_type;
10627 gfc_component *c;
10628 int i;
10630 super_type = gfc_get_derived_super_type (sym);
10632 if (sym->attr.is_class && sym->ts.u.derived == NULL)
10634 /* Fix up incomplete CLASS symbols. */
10635 gfc_component *data = gfc_find_component (sym, "$data", true, true);
10636 gfc_component *vptr = gfc_find_component (sym, "$vptr", true, true);
10637 if (vptr->ts.u.derived == NULL)
10639 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived, false);
10640 gcc_assert (vtab);
10641 vptr->ts.u.derived = vtab->ts.u.derived;
10645 /* F2008, C432. */
10646 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
10648 gfc_error ("As extending type '%s' at %L has a coarray component, "
10649 "parent type '%s' shall also have one", sym->name,
10650 &sym->declared_at, super_type->name);
10651 return FAILURE;
10654 /* Ensure the extended type gets resolved before we do. */
10655 if (super_type && resolve_fl_derived (super_type) == FAILURE)
10656 return FAILURE;
10658 /* An ABSTRACT type must be extensible. */
10659 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
10661 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
10662 sym->name, &sym->declared_at);
10663 return FAILURE;
10666 for (c = sym->components; c != NULL; c = c->next)
10668 /* F2008, C442. */
10669 if (c->attr.codimension /* FIXME: c->as check due to PR 43412. */
10670 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
10672 gfc_error ("Coarray component '%s' at %L must be allocatable with "
10673 "deferred shape", c->name, &c->loc);
10674 return FAILURE;
10677 /* F2008, C443. */
10678 if (c->attr.codimension && c->ts.type == BT_DERIVED
10679 && c->ts.u.derived->ts.is_iso_c)
10681 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
10682 "shall not be a coarray", c->name, &c->loc);
10683 return FAILURE;
10686 /* F2008, C444. */
10687 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
10688 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
10689 || c->attr.allocatable))
10691 gfc_error ("Component '%s' at %L with coarray component "
10692 "shall be a nonpointer, nonallocatable scalar",
10693 c->name, &c->loc);
10694 return FAILURE;
10697 if (c->attr.proc_pointer && c->ts.interface)
10699 if (c->ts.interface->attr.procedure && !sym->attr.vtype)
10700 gfc_error ("Interface '%s', used by procedure pointer component "
10701 "'%s' at %L, is declared in a later PROCEDURE statement",
10702 c->ts.interface->name, c->name, &c->loc);
10704 /* Get the attributes from the interface (now resolved). */
10705 if (c->ts.interface->attr.if_source
10706 || c->ts.interface->attr.intrinsic)
10708 gfc_symbol *ifc = c->ts.interface;
10710 if (ifc->formal && !ifc->formal_ns)
10711 resolve_symbol (ifc);
10713 if (ifc->attr.intrinsic)
10714 resolve_intrinsic (ifc, &ifc->declared_at);
10716 if (ifc->result)
10718 c->ts = ifc->result->ts;
10719 c->attr.allocatable = ifc->result->attr.allocatable;
10720 c->attr.pointer = ifc->result->attr.pointer;
10721 c->attr.dimension = ifc->result->attr.dimension;
10722 c->as = gfc_copy_array_spec (ifc->result->as);
10724 else
10726 c->ts = ifc->ts;
10727 c->attr.allocatable = ifc->attr.allocatable;
10728 c->attr.pointer = ifc->attr.pointer;
10729 c->attr.dimension = ifc->attr.dimension;
10730 c->as = gfc_copy_array_spec (ifc->as);
10732 c->ts.interface = ifc;
10733 c->attr.function = ifc->attr.function;
10734 c->attr.subroutine = ifc->attr.subroutine;
10735 gfc_copy_formal_args_ppc (c, ifc);
10737 c->attr.pure = ifc->attr.pure;
10738 c->attr.elemental = ifc->attr.elemental;
10739 c->attr.recursive = ifc->attr.recursive;
10740 c->attr.always_explicit = ifc->attr.always_explicit;
10741 c->attr.ext_attr |= ifc->attr.ext_attr;
10742 /* Replace symbols in array spec. */
10743 if (c->as)
10745 int i;
10746 for (i = 0; i < c->as->rank; i++)
10748 gfc_expr_replace_comp (c->as->lower[i], c);
10749 gfc_expr_replace_comp (c->as->upper[i], c);
10752 /* Copy char length. */
10753 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
10755 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
10756 gfc_expr_replace_comp (cl->length, c);
10757 if (cl->length && !cl->resolved
10758 && gfc_resolve_expr (cl->length) == FAILURE)
10759 return FAILURE;
10760 c->ts.u.cl = cl;
10763 else if (c->ts.interface->name[0] != '\0' && !sym->attr.vtype)
10765 gfc_error ("Interface '%s' of procedure pointer component "
10766 "'%s' at %L must be explicit", c->ts.interface->name,
10767 c->name, &c->loc);
10768 return FAILURE;
10771 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
10773 /* Since PPCs are not implicitly typed, a PPC without an explicit
10774 interface must be a subroutine. */
10775 gfc_add_subroutine (&c->attr, c->name, &c->loc);
10778 /* Procedure pointer components: Check PASS arg. */
10779 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
10780 && !sym->attr.vtype)
10782 gfc_symbol* me_arg;
10784 if (c->tb->pass_arg)
10786 gfc_formal_arglist* i;
10788 /* If an explicit passing argument name is given, walk the arg-list
10789 and look for it. */
10791 me_arg = NULL;
10792 c->tb->pass_arg_num = 1;
10793 for (i = c->formal; i; i = i->next)
10795 if (!strcmp (i->sym->name, c->tb->pass_arg))
10797 me_arg = i->sym;
10798 break;
10800 c->tb->pass_arg_num++;
10803 if (!me_arg)
10805 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
10806 "at %L has no argument '%s'", c->name,
10807 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
10808 c->tb->error = 1;
10809 return FAILURE;
10812 else
10814 /* Otherwise, take the first one; there should in fact be at least
10815 one. */
10816 c->tb->pass_arg_num = 1;
10817 if (!c->formal)
10819 gfc_error ("Procedure pointer component '%s' with PASS at %L "
10820 "must have at least one argument",
10821 c->name, &c->loc);
10822 c->tb->error = 1;
10823 return FAILURE;
10825 me_arg = c->formal->sym;
10828 /* Now check that the argument-type matches. */
10829 gcc_assert (me_arg);
10830 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
10831 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
10832 || (me_arg->ts.type == BT_CLASS
10833 && CLASS_DATA (me_arg)->ts.u.derived != sym))
10835 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10836 " the derived type '%s'", me_arg->name, c->name,
10837 me_arg->name, &c->loc, sym->name);
10838 c->tb->error = 1;
10839 return FAILURE;
10842 /* Check for C453. */
10843 if (me_arg->attr.dimension)
10845 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10846 "must be scalar", me_arg->name, c->name, me_arg->name,
10847 &c->loc);
10848 c->tb->error = 1;
10849 return FAILURE;
10852 if (me_arg->attr.pointer)
10854 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10855 "may not have the POINTER attribute", me_arg->name,
10856 c->name, me_arg->name, &c->loc);
10857 c->tb->error = 1;
10858 return FAILURE;
10861 if (me_arg->attr.allocatable)
10863 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10864 "may not be ALLOCATABLE", me_arg->name, c->name,
10865 me_arg->name, &c->loc);
10866 c->tb->error = 1;
10867 return FAILURE;
10870 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
10871 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10872 " at %L", c->name, &c->loc);
10876 /* Check type-spec if this is not the parent-type component. */
10877 if ((!sym->attr.extension || c != sym->components)
10878 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
10879 return FAILURE;
10881 /* If this type is an extension, set the accessibility of the parent
10882 component. */
10883 if (super_type && c == sym->components
10884 && strcmp (super_type->name, c->name) == 0)
10885 c->attr.access = super_type->attr.access;
10887 /* If this type is an extension, see if this component has the same name
10888 as an inherited type-bound procedure. */
10889 if (super_type && !sym->attr.is_class
10890 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
10892 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
10893 " inherited type-bound procedure",
10894 c->name, sym->name, &c->loc);
10895 return FAILURE;
10898 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
10900 if (c->ts.u.cl->length == NULL
10901 || (resolve_charlen (c->ts.u.cl) == FAILURE)
10902 || !gfc_is_constant_expr (c->ts.u.cl->length))
10904 gfc_error ("Character length of component '%s' needs to "
10905 "be a constant specification expression at %L",
10906 c->name,
10907 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
10908 return FAILURE;
10912 if (c->ts.type == BT_DERIVED
10913 && sym->component_access != ACCESS_PRIVATE
10914 && gfc_check_access (sym->attr.access, sym->ns->default_access)
10915 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
10916 && !c->ts.u.derived->attr.use_assoc
10917 && !gfc_check_access (c->ts.u.derived->attr.access,
10918 c->ts.u.derived->ns->default_access)
10919 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
10920 "is a PRIVATE type and cannot be a component of "
10921 "'%s', which is PUBLIC at %L", c->name,
10922 sym->name, &sym->declared_at) == FAILURE)
10923 return FAILURE;
10925 if (sym->attr.sequence)
10927 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
10929 gfc_error ("Component %s of SEQUENCE type declared at %L does "
10930 "not have the SEQUENCE attribute",
10931 c->ts.u.derived->name, &sym->declared_at);
10932 return FAILURE;
10936 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && c->attr.pointer
10937 && c->ts.u.derived->components == NULL
10938 && !c->ts.u.derived->attr.zero_comp)
10940 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
10941 "that has not been declared", c->name, sym->name,
10942 &c->loc);
10943 return FAILURE;
10946 if (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.pointer
10947 && CLASS_DATA (c)->ts.u.derived->components == NULL
10948 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp)
10950 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
10951 "that has not been declared", c->name, sym->name,
10952 &c->loc);
10953 return FAILURE;
10956 /* C437. */
10957 if (c->ts.type == BT_CLASS
10958 && !(CLASS_DATA (c)->attr.pointer || CLASS_DATA (c)->attr.allocatable))
10960 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
10961 "or pointer", c->name, &c->loc);
10962 return FAILURE;
10965 /* Ensure that all the derived type components are put on the
10966 derived type list; even in formal namespaces, where derived type
10967 pointer components might not have been declared. */
10968 if (c->ts.type == BT_DERIVED
10969 && c->ts.u.derived
10970 && c->ts.u.derived->components
10971 && c->attr.pointer
10972 && sym != c->ts.u.derived)
10973 add_dt_to_dt_list (c->ts.u.derived);
10975 if (c->attr.pointer || c->attr.proc_pointer || c->attr.allocatable
10976 || c->as == NULL)
10977 continue;
10979 for (i = 0; i < c->as->rank; i++)
10981 if (c->as->lower[i] == NULL
10982 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
10983 || !gfc_is_constant_expr (c->as->lower[i])
10984 || c->as->upper[i] == NULL
10985 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
10986 || !gfc_is_constant_expr (c->as->upper[i]))
10988 gfc_error ("Component '%s' of '%s' at %L must have "
10989 "constant array bounds",
10990 c->name, sym->name, &c->loc);
10991 return FAILURE;
10996 /* Resolve the type-bound procedures. */
10997 if (resolve_typebound_procedures (sym) == FAILURE)
10998 return FAILURE;
11000 /* Resolve the finalizer procedures. */
11001 if (gfc_resolve_finalizers (sym) == FAILURE)
11002 return FAILURE;
11004 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
11005 all DEFERRED bindings are overridden. */
11006 if (super_type && super_type->attr.abstract && !sym->attr.abstract
11007 && ensure_not_abstract (sym, super_type) == FAILURE)
11008 return FAILURE;
11010 /* Add derived type to the derived type list. */
11011 add_dt_to_dt_list (sym);
11013 return SUCCESS;
11017 static gfc_try
11018 resolve_fl_namelist (gfc_symbol *sym)
11020 gfc_namelist *nl;
11021 gfc_symbol *nlsym;
11023 /* Reject PRIVATE objects in a PUBLIC namelist. */
11024 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
11026 for (nl = sym->namelist; nl; nl = nl->next)
11028 if (!nl->sym->attr.use_assoc
11029 && !is_sym_host_assoc (nl->sym, sym->ns)
11030 && !gfc_check_access(nl->sym->attr.access,
11031 nl->sym->ns->default_access))
11033 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
11034 "cannot be member of PUBLIC namelist '%s' at %L",
11035 nl->sym->name, sym->name, &sym->declared_at);
11036 return FAILURE;
11039 /* Types with private components that came here by USE-association. */
11040 if (nl->sym->ts.type == BT_DERIVED
11041 && derived_inaccessible (nl->sym->ts.u.derived))
11043 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
11044 "components and cannot be member of namelist '%s' at %L",
11045 nl->sym->name, sym->name, &sym->declared_at);
11046 return FAILURE;
11049 /* Types with private components that are defined in the same module. */
11050 if (nl->sym->ts.type == BT_DERIVED
11051 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
11052 && !gfc_check_access (nl->sym->ts.u.derived->attr.private_comp
11053 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
11054 nl->sym->ns->default_access))
11056 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
11057 "cannot be a member of PUBLIC namelist '%s' at %L",
11058 nl->sym->name, sym->name, &sym->declared_at);
11059 return FAILURE;
11064 for (nl = sym->namelist; nl; nl = nl->next)
11066 /* Reject namelist arrays of assumed shape. */
11067 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
11068 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
11069 "must not have assumed shape in namelist "
11070 "'%s' at %L", nl->sym->name, sym->name,
11071 &sym->declared_at) == FAILURE)
11072 return FAILURE;
11074 /* Reject namelist arrays that are not constant shape. */
11075 if (is_non_constant_shape_array (nl->sym))
11077 gfc_error ("NAMELIST array object '%s' must have constant "
11078 "shape in namelist '%s' at %L", nl->sym->name,
11079 sym->name, &sym->declared_at);
11080 return FAILURE;
11083 /* Namelist objects cannot have allocatable or pointer components. */
11084 if (nl->sym->ts.type != BT_DERIVED)
11085 continue;
11087 if (nl->sym->ts.u.derived->attr.alloc_comp)
11089 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
11090 "have ALLOCATABLE components",
11091 nl->sym->name, sym->name, &sym->declared_at);
11092 return FAILURE;
11095 if (nl->sym->ts.u.derived->attr.pointer_comp)
11097 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
11098 "have POINTER components",
11099 nl->sym->name, sym->name, &sym->declared_at);
11100 return FAILURE;
11105 /* 14.1.2 A module or internal procedure represent local entities
11106 of the same type as a namelist member and so are not allowed. */
11107 for (nl = sym->namelist; nl; nl = nl->next)
11109 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
11110 continue;
11112 if (nl->sym->attr.function && nl->sym == nl->sym->result)
11113 if ((nl->sym == sym->ns->proc_name)
11115 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
11116 continue;
11118 nlsym = NULL;
11119 if (nl->sym && nl->sym->name)
11120 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
11121 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
11123 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
11124 "attribute in '%s' at %L", nlsym->name,
11125 &sym->declared_at);
11126 return FAILURE;
11130 return SUCCESS;
11134 static gfc_try
11135 resolve_fl_parameter (gfc_symbol *sym)
11137 /* A parameter array's shape needs to be constant. */
11138 if (sym->as != NULL
11139 && (sym->as->type == AS_DEFERRED
11140 || is_non_constant_shape_array (sym)))
11142 gfc_error ("Parameter array '%s' at %L cannot be automatic "
11143 "or of deferred shape", sym->name, &sym->declared_at);
11144 return FAILURE;
11147 /* Make sure a parameter that has been implicitly typed still
11148 matches the implicit type, since PARAMETER statements can precede
11149 IMPLICIT statements. */
11150 if (sym->attr.implicit_type
11151 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
11152 sym->ns)))
11154 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
11155 "later IMPLICIT type", sym->name, &sym->declared_at);
11156 return FAILURE;
11159 /* Make sure the types of derived parameters are consistent. This
11160 type checking is deferred until resolution because the type may
11161 refer to a derived type from the host. */
11162 if (sym->ts.type == BT_DERIVED
11163 && !gfc_compare_types (&sym->ts, &sym->value->ts))
11165 gfc_error ("Incompatible derived type in PARAMETER at %L",
11166 &sym->value->where);
11167 return FAILURE;
11169 return SUCCESS;
11173 /* Do anything necessary to resolve a symbol. Right now, we just
11174 assume that an otherwise unknown symbol is a variable. This sort
11175 of thing commonly happens for symbols in module. */
11177 static void
11178 resolve_symbol (gfc_symbol *sym)
11180 int check_constant, mp_flag;
11181 gfc_symtree *symtree;
11182 gfc_symtree *this_symtree;
11183 gfc_namespace *ns;
11184 gfc_component *c;
11186 /* Avoid double resolution of function result symbols. */
11187 if ((sym->result || sym->attr.result) && (sym->ns != gfc_current_ns))
11188 return;
11190 if (sym->attr.flavor == FL_UNKNOWN)
11193 /* If we find that a flavorless symbol is an interface in one of the
11194 parent namespaces, find its symtree in this namespace, free the
11195 symbol and set the symtree to point to the interface symbol. */
11196 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
11198 symtree = gfc_find_symtree (ns->sym_root, sym->name);
11199 if (symtree && symtree->n.sym->generic)
11201 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
11202 sym->name);
11203 sym->refs--;
11204 if (!sym->refs)
11205 gfc_free_symbol (sym);
11206 symtree->n.sym->refs++;
11207 this_symtree->n.sym = symtree->n.sym;
11208 return;
11212 /* Otherwise give it a flavor according to such attributes as
11213 it has. */
11214 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
11215 sym->attr.flavor = FL_VARIABLE;
11216 else
11218 sym->attr.flavor = FL_PROCEDURE;
11219 if (sym->attr.dimension)
11220 sym->attr.function = 1;
11224 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
11225 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
11227 if (sym->attr.procedure && sym->ts.interface
11228 && sym->attr.if_source != IFSRC_DECL)
11230 if (sym->ts.interface == sym)
11232 gfc_error ("PROCEDURE '%s' at %L may not be used as its own "
11233 "interface", sym->name, &sym->declared_at);
11234 return;
11236 if (sym->ts.interface->attr.procedure)
11238 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared"
11239 " in a later PROCEDURE statement", sym->ts.interface->name,
11240 sym->name,&sym->declared_at);
11241 return;
11244 /* Get the attributes from the interface (now resolved). */
11245 if (sym->ts.interface->attr.if_source
11246 || sym->ts.interface->attr.intrinsic)
11248 gfc_symbol *ifc = sym->ts.interface;
11249 resolve_symbol (ifc);
11251 if (ifc->attr.intrinsic)
11252 resolve_intrinsic (ifc, &ifc->declared_at);
11254 if (ifc->result)
11255 sym->ts = ifc->result->ts;
11256 else
11257 sym->ts = ifc->ts;
11258 sym->ts.interface = ifc;
11259 sym->attr.function = ifc->attr.function;
11260 sym->attr.subroutine = ifc->attr.subroutine;
11261 gfc_copy_formal_args (sym, ifc);
11263 sym->attr.allocatable = ifc->attr.allocatable;
11264 sym->attr.pointer = ifc->attr.pointer;
11265 sym->attr.pure = ifc->attr.pure;
11266 sym->attr.elemental = ifc->attr.elemental;
11267 sym->attr.dimension = ifc->attr.dimension;
11268 sym->attr.recursive = ifc->attr.recursive;
11269 sym->attr.always_explicit = ifc->attr.always_explicit;
11270 sym->attr.ext_attr |= ifc->attr.ext_attr;
11271 /* Copy array spec. */
11272 sym->as = gfc_copy_array_spec (ifc->as);
11273 if (sym->as)
11275 int i;
11276 for (i = 0; i < sym->as->rank; i++)
11278 gfc_expr_replace_symbols (sym->as->lower[i], sym);
11279 gfc_expr_replace_symbols (sym->as->upper[i], sym);
11282 /* Copy char length. */
11283 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
11285 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
11286 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
11287 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
11288 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
11289 return;
11292 else if (sym->ts.interface->name[0] != '\0')
11294 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
11295 sym->ts.interface->name, sym->name, &sym->declared_at);
11296 return;
11300 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
11301 return;
11303 /* Symbols that are module procedures with results (functions) have
11304 the types and array specification copied for type checking in
11305 procedures that call them, as well as for saving to a module
11306 file. These symbols can't stand the scrutiny that their results
11307 can. */
11308 mp_flag = (sym->result != NULL && sym->result != sym);
11311 /* Make sure that the intrinsic is consistent with its internal
11312 representation. This needs to be done before assigning a default
11313 type to avoid spurious warnings. */
11314 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
11315 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
11316 return;
11318 /* Assign default type to symbols that need one and don't have one. */
11319 if (sym->ts.type == BT_UNKNOWN)
11321 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
11322 gfc_set_default_type (sym, 1, NULL);
11324 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
11325 && !sym->attr.function && !sym->attr.subroutine
11326 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
11327 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
11329 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
11331 /* The specific case of an external procedure should emit an error
11332 in the case that there is no implicit type. */
11333 if (!mp_flag)
11334 gfc_set_default_type (sym, sym->attr.external, NULL);
11335 else
11337 /* Result may be in another namespace. */
11338 resolve_symbol (sym->result);
11340 if (!sym->result->attr.proc_pointer)
11342 sym->ts = sym->result->ts;
11343 sym->as = gfc_copy_array_spec (sym->result->as);
11344 sym->attr.dimension = sym->result->attr.dimension;
11345 sym->attr.pointer = sym->result->attr.pointer;
11346 sym->attr.allocatable = sym->result->attr.allocatable;
11352 /* Assumed size arrays and assumed shape arrays must be dummy
11353 arguments. */
11355 if (sym->as != NULL
11356 && ((sym->as->type == AS_ASSUMED_SIZE && !sym->as->cp_was_assumed)
11357 || sym->as->type == AS_ASSUMED_SHAPE)
11358 && sym->attr.dummy == 0)
11360 if (sym->as->type == AS_ASSUMED_SIZE)
11361 gfc_error ("Assumed size array at %L must be a dummy argument",
11362 &sym->declared_at);
11363 else
11364 gfc_error ("Assumed shape array at %L must be a dummy argument",
11365 &sym->declared_at);
11366 return;
11369 /* Make sure symbols with known intent or optional are really dummy
11370 variable. Because of ENTRY statement, this has to be deferred
11371 until resolution time. */
11373 if (!sym->attr.dummy
11374 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
11376 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
11377 return;
11380 if (sym->attr.value && !sym->attr.dummy)
11382 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
11383 "it is not a dummy argument", sym->name, &sym->declared_at);
11384 return;
11387 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
11389 gfc_charlen *cl = sym->ts.u.cl;
11390 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11392 gfc_error ("Character dummy variable '%s' at %L with VALUE "
11393 "attribute must have constant length",
11394 sym->name, &sym->declared_at);
11395 return;
11398 if (sym->ts.is_c_interop
11399 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
11401 gfc_error ("C interoperable character dummy variable '%s' at %L "
11402 "with VALUE attribute must have length one",
11403 sym->name, &sym->declared_at);
11404 return;
11408 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
11409 do this for something that was implicitly typed because that is handled
11410 in gfc_set_default_type. Handle dummy arguments and procedure
11411 definitions separately. Also, anything that is use associated is not
11412 handled here but instead is handled in the module it is declared in.
11413 Finally, derived type definitions are allowed to be BIND(C) since that
11414 only implies that they're interoperable, and they are checked fully for
11415 interoperability when a variable is declared of that type. */
11416 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
11417 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
11418 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
11420 gfc_try t = SUCCESS;
11422 /* First, make sure the variable is declared at the
11423 module-level scope (J3/04-007, Section 15.3). */
11424 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
11425 sym->attr.in_common == 0)
11427 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
11428 "is neither a COMMON block nor declared at the "
11429 "module level scope", sym->name, &(sym->declared_at));
11430 t = FAILURE;
11432 else if (sym->common_head != NULL)
11434 t = verify_com_block_vars_c_interop (sym->common_head);
11436 else
11438 /* If type() declaration, we need to verify that the components
11439 of the given type are all C interoperable, etc. */
11440 if (sym->ts.type == BT_DERIVED &&
11441 sym->ts.u.derived->attr.is_c_interop != 1)
11443 /* Make sure the user marked the derived type as BIND(C). If
11444 not, call the verify routine. This could print an error
11445 for the derived type more than once if multiple variables
11446 of that type are declared. */
11447 if (sym->ts.u.derived->attr.is_bind_c != 1)
11448 verify_bind_c_derived_type (sym->ts.u.derived);
11449 t = FAILURE;
11452 /* Verify the variable itself as C interoperable if it
11453 is BIND(C). It is not possible for this to succeed if
11454 the verify_bind_c_derived_type failed, so don't have to handle
11455 any error returned by verify_bind_c_derived_type. */
11456 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11457 sym->common_block);
11460 if (t == FAILURE)
11462 /* clear the is_bind_c flag to prevent reporting errors more than
11463 once if something failed. */
11464 sym->attr.is_bind_c = 0;
11465 return;
11469 /* If a derived type symbol has reached this point, without its
11470 type being declared, we have an error. Notice that most
11471 conditions that produce undefined derived types have already
11472 been dealt with. However, the likes of:
11473 implicit type(t) (t) ..... call foo (t) will get us here if
11474 the type is not declared in the scope of the implicit
11475 statement. Change the type to BT_UNKNOWN, both because it is so
11476 and to prevent an ICE. */
11477 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
11478 && !sym->ts.u.derived->attr.zero_comp)
11480 gfc_error ("The derived type '%s' at %L is of type '%s', "
11481 "which has not been defined", sym->name,
11482 &sym->declared_at, sym->ts.u.derived->name);
11483 sym->ts.type = BT_UNKNOWN;
11484 return;
11487 /* Make sure that the derived type has been resolved and that the
11488 derived type is visible in the symbol's namespace, if it is a
11489 module function and is not PRIVATE. */
11490 if (sym->ts.type == BT_DERIVED
11491 && sym->ts.u.derived->attr.use_assoc
11492 && sym->ns->proc_name
11493 && sym->ns->proc_name->attr.flavor == FL_MODULE)
11495 gfc_symbol *ds;
11497 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
11498 return;
11500 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
11501 if (!ds && sym->attr.function
11502 && gfc_check_access (sym->attr.access, sym->ns->default_access))
11504 symtree = gfc_new_symtree (&sym->ns->sym_root,
11505 sym->ts.u.derived->name);
11506 symtree->n.sym = sym->ts.u.derived;
11507 sym->ts.u.derived->refs++;
11511 /* Unless the derived-type declaration is use associated, Fortran 95
11512 does not allow public entries of private derived types.
11513 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
11514 161 in 95-006r3. */
11515 if (sym->ts.type == BT_DERIVED
11516 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
11517 && !sym->ts.u.derived->attr.use_assoc
11518 && gfc_check_access (sym->attr.access, sym->ns->default_access)
11519 && !gfc_check_access (sym->ts.u.derived->attr.access,
11520 sym->ts.u.derived->ns->default_access)
11521 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
11522 "of PRIVATE derived type '%s'",
11523 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
11524 : "variable", sym->name, &sym->declared_at,
11525 sym->ts.u.derived->name) == FAILURE)
11526 return;
11528 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
11529 default initialization is defined (5.1.2.4.4). */
11530 if (sym->ts.type == BT_DERIVED
11531 && sym->attr.dummy
11532 && sym->attr.intent == INTENT_OUT
11533 && sym->as
11534 && sym->as->type == AS_ASSUMED_SIZE)
11536 for (c = sym->ts.u.derived->components; c; c = c->next)
11538 if (c->initializer)
11540 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
11541 "ASSUMED SIZE and so cannot have a default initializer",
11542 sym->name, &sym->declared_at);
11543 return;
11548 /* F2008, C526. */
11549 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
11550 || sym->attr.codimension)
11551 && sym->attr.result)
11552 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
11553 "a coarray component", sym->name, &sym->declared_at);
11555 /* F2008, C524. */
11556 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
11557 && sym->ts.u.derived->ts.is_iso_c)
11558 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
11559 "shall not be a coarray", sym->name, &sym->declared_at);
11561 /* F2008, C525. */
11562 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp
11563 && (sym->attr.codimension || sym->attr.pointer || sym->attr.dimension
11564 || sym->attr.allocatable))
11565 gfc_error ("Variable '%s' at %L with coarray component "
11566 "shall be a nonpointer, nonallocatable scalar",
11567 sym->name, &sym->declared_at);
11569 /* F2008, C526. The function-result case was handled above. */
11570 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
11571 || sym->attr.codimension)
11572 && !(sym->attr.allocatable || sym->attr.dummy || sym->attr.save
11573 || sym->ns->proc_name->attr.flavor == FL_MODULE
11574 || sym->ns->proc_name->attr.is_main_program
11575 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
11576 gfc_error ("Variable '%s' at %L is a coarray or has a coarray "
11577 "component and is not ALLOCATABLE, SAVE nor a "
11578 "dummy argument", sym->name, &sym->declared_at);
11579 /* F2008, C528. */ /* FIXME: sym->as check due to PR 43412. */
11580 else if (sym->attr.codimension && !sym->attr.allocatable
11581 && sym->as && sym->as->cotype == AS_DEFERRED)
11582 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
11583 "deferred shape", sym->name, &sym->declared_at);
11584 else if (sym->attr.codimension && sym->attr.allocatable
11585 && (sym->as->type != AS_DEFERRED || sym->as->cotype != AS_DEFERRED))
11586 gfc_error ("Allocatable coarray variable '%s' at %L must have "
11587 "deferred shape", sym->name, &sym->declared_at);
11590 /* F2008, C541. */
11591 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
11592 || (sym->attr.codimension && sym->attr.allocatable))
11593 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
11594 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
11595 "allocatable coarray or have coarray components",
11596 sym->name, &sym->declared_at);
11598 if (sym->attr.codimension && sym->attr.dummy
11599 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
11600 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
11601 "procedure '%s'", sym->name, &sym->declared_at,
11602 sym->ns->proc_name->name);
11604 switch (sym->attr.flavor)
11606 case FL_VARIABLE:
11607 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
11608 return;
11609 break;
11611 case FL_PROCEDURE:
11612 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
11613 return;
11614 break;
11616 case FL_NAMELIST:
11617 if (resolve_fl_namelist (sym) == FAILURE)
11618 return;
11619 break;
11621 case FL_PARAMETER:
11622 if (resolve_fl_parameter (sym) == FAILURE)
11623 return;
11624 break;
11626 default:
11627 break;
11630 /* Resolve array specifier. Check as well some constraints
11631 on COMMON blocks. */
11633 check_constant = sym->attr.in_common && !sym->attr.pointer;
11635 /* Set the formal_arg_flag so that check_conflict will not throw
11636 an error for host associated variables in the specification
11637 expression for an array_valued function. */
11638 if (sym->attr.function && sym->as)
11639 formal_arg_flag = 1;
11641 gfc_resolve_array_spec (sym->as, check_constant);
11643 formal_arg_flag = 0;
11645 /* Resolve formal namespaces. */
11646 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
11647 && !sym->attr.contained && !sym->attr.intrinsic)
11648 gfc_resolve (sym->formal_ns);
11650 /* Make sure the formal namespace is present. */
11651 if (sym->formal && !sym->formal_ns)
11653 gfc_formal_arglist *formal = sym->formal;
11654 while (formal && !formal->sym)
11655 formal = formal->next;
11657 if (formal)
11659 sym->formal_ns = formal->sym->ns;
11660 sym->formal_ns->refs++;
11664 /* Check threadprivate restrictions. */
11665 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
11666 && (!sym->attr.in_common
11667 && sym->module == NULL
11668 && (sym->ns->proc_name == NULL
11669 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
11670 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
11672 /* If we have come this far we can apply default-initializers, as
11673 described in 14.7.5, to those variables that have not already
11674 been assigned one. */
11675 if (sym->ts.type == BT_DERIVED
11676 && sym->attr.referenced
11677 && sym->ns == gfc_current_ns
11678 && !sym->value
11679 && !sym->attr.allocatable
11680 && !sym->attr.alloc_comp)
11682 symbol_attribute *a = &sym->attr;
11684 if ((!a->save && !a->dummy && !a->pointer
11685 && !a->in_common && !a->use_assoc
11686 && !(a->function && sym != sym->result))
11687 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
11688 apply_default_init (sym);
11691 /* If this symbol has a type-spec, check it. */
11692 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
11693 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
11694 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
11695 == FAILURE)
11696 return;
11700 /************* Resolve DATA statements *************/
11702 static struct
11704 gfc_data_value *vnode;
11705 mpz_t left;
11707 values;
11710 /* Advance the values structure to point to the next value in the data list. */
11712 static gfc_try
11713 next_data_value (void)
11715 while (mpz_cmp_ui (values.left, 0) == 0)
11718 if (values.vnode->next == NULL)
11719 return FAILURE;
11721 values.vnode = values.vnode->next;
11722 mpz_set (values.left, values.vnode->repeat);
11725 return SUCCESS;
11729 static gfc_try
11730 check_data_variable (gfc_data_variable *var, locus *where)
11732 gfc_expr *e;
11733 mpz_t size;
11734 mpz_t offset;
11735 gfc_try t;
11736 ar_type mark = AR_UNKNOWN;
11737 int i;
11738 mpz_t section_index[GFC_MAX_DIMENSIONS];
11739 gfc_ref *ref;
11740 gfc_array_ref *ar;
11741 gfc_symbol *sym;
11742 int has_pointer;
11744 if (gfc_resolve_expr (var->expr) == FAILURE)
11745 return FAILURE;
11747 ar = NULL;
11748 mpz_init_set_si (offset, 0);
11749 e = var->expr;
11751 if (e->expr_type != EXPR_VARIABLE)
11752 gfc_internal_error ("check_data_variable(): Bad expression");
11754 sym = e->symtree->n.sym;
11756 if (sym->ns->is_block_data && !sym->attr.in_common)
11758 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
11759 sym->name, &sym->declared_at);
11762 if (e->ref == NULL && sym->as)
11764 gfc_error ("DATA array '%s' at %L must be specified in a previous"
11765 " declaration", sym->name, where);
11766 return FAILURE;
11769 has_pointer = sym->attr.pointer;
11771 for (ref = e->ref; ref; ref = ref->next)
11773 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
11774 has_pointer = 1;
11776 if (ref->type == REF_ARRAY && ref->u.ar.codimen)
11778 gfc_error ("DATA element '%s' at %L cannot have a coindex",
11779 sym->name, where);
11780 return FAILURE;
11783 if (has_pointer
11784 && ref->type == REF_ARRAY
11785 && ref->u.ar.type != AR_FULL)
11787 gfc_error ("DATA element '%s' at %L is a pointer and so must "
11788 "be a full array", sym->name, where);
11789 return FAILURE;
11793 if (e->rank == 0 || has_pointer)
11795 mpz_init_set_ui (size, 1);
11796 ref = NULL;
11798 else
11800 ref = e->ref;
11802 /* Find the array section reference. */
11803 for (ref = e->ref; ref; ref = ref->next)
11805 if (ref->type != REF_ARRAY)
11806 continue;
11807 if (ref->u.ar.type == AR_ELEMENT)
11808 continue;
11809 break;
11811 gcc_assert (ref);
11813 /* Set marks according to the reference pattern. */
11814 switch (ref->u.ar.type)
11816 case AR_FULL:
11817 mark = AR_FULL;
11818 break;
11820 case AR_SECTION:
11821 ar = &ref->u.ar;
11822 /* Get the start position of array section. */
11823 gfc_get_section_index (ar, section_index, &offset);
11824 mark = AR_SECTION;
11825 break;
11827 default:
11828 gcc_unreachable ();
11831 if (gfc_array_size (e, &size) == FAILURE)
11833 gfc_error ("Nonconstant array section at %L in DATA statement",
11834 &e->where);
11835 mpz_clear (offset);
11836 return FAILURE;
11840 t = SUCCESS;
11842 while (mpz_cmp_ui (size, 0) > 0)
11844 if (next_data_value () == FAILURE)
11846 gfc_error ("DATA statement at %L has more variables than values",
11847 where);
11848 t = FAILURE;
11849 break;
11852 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
11853 if (t == FAILURE)
11854 break;
11856 /* If we have more than one element left in the repeat count,
11857 and we have more than one element left in the target variable,
11858 then create a range assignment. */
11859 /* FIXME: Only done for full arrays for now, since array sections
11860 seem tricky. */
11861 if (mark == AR_FULL && ref && ref->next == NULL
11862 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
11864 mpz_t range;
11866 if (mpz_cmp (size, values.left) >= 0)
11868 mpz_init_set (range, values.left);
11869 mpz_sub (size, size, values.left);
11870 mpz_set_ui (values.left, 0);
11872 else
11874 mpz_init_set (range, size);
11875 mpz_sub (values.left, values.left, size);
11876 mpz_set_ui (size, 0);
11879 t = gfc_assign_data_value_range (var->expr, values.vnode->expr,
11880 offset, range);
11882 mpz_add (offset, offset, range);
11883 mpz_clear (range);
11885 if (t == FAILURE)
11886 break;
11889 /* Assign initial value to symbol. */
11890 else
11892 mpz_sub_ui (values.left, values.left, 1);
11893 mpz_sub_ui (size, size, 1);
11895 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
11896 if (t == FAILURE)
11897 break;
11899 if (mark == AR_FULL)
11900 mpz_add_ui (offset, offset, 1);
11902 /* Modify the array section indexes and recalculate the offset
11903 for next element. */
11904 else if (mark == AR_SECTION)
11905 gfc_advance_section (section_index, ar, &offset);
11909 if (mark == AR_SECTION)
11911 for (i = 0; i < ar->dimen; i++)
11912 mpz_clear (section_index[i]);
11915 mpz_clear (size);
11916 mpz_clear (offset);
11918 return t;
11922 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
11924 /* Iterate over a list of elements in a DATA statement. */
11926 static gfc_try
11927 traverse_data_list (gfc_data_variable *var, locus *where)
11929 mpz_t trip;
11930 iterator_stack frame;
11931 gfc_expr *e, *start, *end, *step;
11932 gfc_try retval = SUCCESS;
11934 mpz_init (frame.value);
11935 mpz_init (trip);
11937 start = gfc_copy_expr (var->iter.start);
11938 end = gfc_copy_expr (var->iter.end);
11939 step = gfc_copy_expr (var->iter.step);
11941 if (gfc_simplify_expr (start, 1) == FAILURE
11942 || start->expr_type != EXPR_CONSTANT)
11944 gfc_error ("start of implied-do loop at %L could not be "
11945 "simplified to a constant value", &start->where);
11946 retval = FAILURE;
11947 goto cleanup;
11949 if (gfc_simplify_expr (end, 1) == FAILURE
11950 || end->expr_type != EXPR_CONSTANT)
11952 gfc_error ("end of implied-do loop at %L could not be "
11953 "simplified to a constant value", &start->where);
11954 retval = FAILURE;
11955 goto cleanup;
11957 if (gfc_simplify_expr (step, 1) == FAILURE
11958 || step->expr_type != EXPR_CONSTANT)
11960 gfc_error ("step of implied-do loop at %L could not be "
11961 "simplified to a constant value", &start->where);
11962 retval = FAILURE;
11963 goto cleanup;
11966 mpz_set (trip, end->value.integer);
11967 mpz_sub (trip, trip, start->value.integer);
11968 mpz_add (trip, trip, step->value.integer);
11970 mpz_div (trip, trip, step->value.integer);
11972 mpz_set (frame.value, start->value.integer);
11974 frame.prev = iter_stack;
11975 frame.variable = var->iter.var->symtree;
11976 iter_stack = &frame;
11978 while (mpz_cmp_ui (trip, 0) > 0)
11980 if (traverse_data_var (var->list, where) == FAILURE)
11982 retval = FAILURE;
11983 goto cleanup;
11986 e = gfc_copy_expr (var->expr);
11987 if (gfc_simplify_expr (e, 1) == FAILURE)
11989 gfc_free_expr (e);
11990 retval = FAILURE;
11991 goto cleanup;
11994 mpz_add (frame.value, frame.value, step->value.integer);
11996 mpz_sub_ui (trip, trip, 1);
11999 cleanup:
12000 mpz_clear (frame.value);
12001 mpz_clear (trip);
12003 gfc_free_expr (start);
12004 gfc_free_expr (end);
12005 gfc_free_expr (step);
12007 iter_stack = frame.prev;
12008 return retval;
12012 /* Type resolve variables in the variable list of a DATA statement. */
12014 static gfc_try
12015 traverse_data_var (gfc_data_variable *var, locus *where)
12017 gfc_try t;
12019 for (; var; var = var->next)
12021 if (var->expr == NULL)
12022 t = traverse_data_list (var, where);
12023 else
12024 t = check_data_variable (var, where);
12026 if (t == FAILURE)
12027 return FAILURE;
12030 return SUCCESS;
12034 /* Resolve the expressions and iterators associated with a data statement.
12035 This is separate from the assignment checking because data lists should
12036 only be resolved once. */
12038 static gfc_try
12039 resolve_data_variables (gfc_data_variable *d)
12041 for (; d; d = d->next)
12043 if (d->list == NULL)
12045 if (gfc_resolve_expr (d->expr) == FAILURE)
12046 return FAILURE;
12048 else
12050 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
12051 return FAILURE;
12053 if (resolve_data_variables (d->list) == FAILURE)
12054 return FAILURE;
12058 return SUCCESS;
12062 /* Resolve a single DATA statement. We implement this by storing a pointer to
12063 the value list into static variables, and then recursively traversing the
12064 variables list, expanding iterators and such. */
12066 static void
12067 resolve_data (gfc_data *d)
12070 if (resolve_data_variables (d->var) == FAILURE)
12071 return;
12073 values.vnode = d->value;
12074 if (d->value == NULL)
12075 mpz_set_ui (values.left, 0);
12076 else
12077 mpz_set (values.left, d->value->repeat);
12079 if (traverse_data_var (d->var, &d->where) == FAILURE)
12080 return;
12082 /* At this point, we better not have any values left. */
12084 if (next_data_value () == SUCCESS)
12085 gfc_error ("DATA statement at %L has more values than variables",
12086 &d->where);
12090 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
12091 accessed by host or use association, is a dummy argument to a pure function,
12092 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
12093 is storage associated with any such variable, shall not be used in the
12094 following contexts: (clients of this function). */
12096 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
12097 procedure. Returns zero if assignment is OK, nonzero if there is a
12098 problem. */
12100 gfc_impure_variable (gfc_symbol *sym)
12102 gfc_symbol *proc;
12103 gfc_namespace *ns;
12105 if (sym->attr.use_assoc || sym->attr.in_common)
12106 return 1;
12108 /* Check if the symbol's ns is inside the pure procedure. */
12109 for (ns = gfc_current_ns; ns; ns = ns->parent)
12111 if (ns == sym->ns)
12112 break;
12113 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
12114 return 1;
12117 proc = sym->ns->proc_name;
12118 if (sym->attr.dummy && gfc_pure (proc)
12119 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
12121 proc->attr.function))
12122 return 1;
12124 /* TODO: Sort out what can be storage associated, if anything, and include
12125 it here. In principle equivalences should be scanned but it does not
12126 seem to be possible to storage associate an impure variable this way. */
12127 return 0;
12131 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
12132 current namespace is inside a pure procedure. */
12135 gfc_pure (gfc_symbol *sym)
12137 symbol_attribute attr;
12138 gfc_namespace *ns;
12140 if (sym == NULL)
12142 /* Check if the current namespace or one of its parents
12143 belongs to a pure procedure. */
12144 for (ns = gfc_current_ns; ns; ns = ns->parent)
12146 sym = ns->proc_name;
12147 if (sym == NULL)
12148 return 0;
12149 attr = sym->attr;
12150 if (attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental))
12151 return 1;
12153 return 0;
12156 attr = sym->attr;
12158 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
12162 /* Test whether the current procedure is elemental or not. */
12165 gfc_elemental (gfc_symbol *sym)
12167 symbol_attribute attr;
12169 if (sym == NULL)
12170 sym = gfc_current_ns->proc_name;
12171 if (sym == NULL)
12172 return 0;
12173 attr = sym->attr;
12175 return attr.flavor == FL_PROCEDURE && attr.elemental;
12179 /* Warn about unused labels. */
12181 static void
12182 warn_unused_fortran_label (gfc_st_label *label)
12184 if (label == NULL)
12185 return;
12187 warn_unused_fortran_label (label->left);
12189 if (label->defined == ST_LABEL_UNKNOWN)
12190 return;
12192 switch (label->referenced)
12194 case ST_LABEL_UNKNOWN:
12195 gfc_warning ("Label %d at %L defined but not used", label->value,
12196 &label->where);
12197 break;
12199 case ST_LABEL_BAD_TARGET:
12200 gfc_warning ("Label %d at %L defined but cannot be used",
12201 label->value, &label->where);
12202 break;
12204 default:
12205 break;
12208 warn_unused_fortran_label (label->right);
12212 /* Returns the sequence type of a symbol or sequence. */
12214 static seq_type
12215 sequence_type (gfc_typespec ts)
12217 seq_type result;
12218 gfc_component *c;
12220 switch (ts.type)
12222 case BT_DERIVED:
12224 if (ts.u.derived->components == NULL)
12225 return SEQ_NONDEFAULT;
12227 result = sequence_type (ts.u.derived->components->ts);
12228 for (c = ts.u.derived->components->next; c; c = c->next)
12229 if (sequence_type (c->ts) != result)
12230 return SEQ_MIXED;
12232 return result;
12234 case BT_CHARACTER:
12235 if (ts.kind != gfc_default_character_kind)
12236 return SEQ_NONDEFAULT;
12238 return SEQ_CHARACTER;
12240 case BT_INTEGER:
12241 if (ts.kind != gfc_default_integer_kind)
12242 return SEQ_NONDEFAULT;
12244 return SEQ_NUMERIC;
12246 case BT_REAL:
12247 if (!(ts.kind == gfc_default_real_kind
12248 || ts.kind == gfc_default_double_kind))
12249 return SEQ_NONDEFAULT;
12251 return SEQ_NUMERIC;
12253 case BT_COMPLEX:
12254 if (ts.kind != gfc_default_complex_kind)
12255 return SEQ_NONDEFAULT;
12257 return SEQ_NUMERIC;
12259 case BT_LOGICAL:
12260 if (ts.kind != gfc_default_logical_kind)
12261 return SEQ_NONDEFAULT;
12263 return SEQ_NUMERIC;
12265 default:
12266 return SEQ_NONDEFAULT;
12271 /* Resolve derived type EQUIVALENCE object. */
12273 static gfc_try
12274 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
12276 gfc_component *c = derived->components;
12278 if (!derived)
12279 return SUCCESS;
12281 /* Shall not be an object of nonsequence derived type. */
12282 if (!derived->attr.sequence)
12284 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
12285 "attribute to be an EQUIVALENCE object", sym->name,
12286 &e->where);
12287 return FAILURE;
12290 /* Shall not have allocatable components. */
12291 if (derived->attr.alloc_comp)
12293 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
12294 "components to be an EQUIVALENCE object",sym->name,
12295 &e->where);
12296 return FAILURE;
12299 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
12301 gfc_error ("Derived type variable '%s' at %L with default "
12302 "initialization cannot be in EQUIVALENCE with a variable "
12303 "in COMMON", sym->name, &e->where);
12304 return FAILURE;
12307 for (; c ; c = c->next)
12309 if (c->ts.type == BT_DERIVED
12310 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
12311 return FAILURE;
12313 /* Shall not be an object of sequence derived type containing a pointer
12314 in the structure. */
12315 if (c->attr.pointer)
12317 gfc_error ("Derived type variable '%s' at %L with pointer "
12318 "component(s) cannot be an EQUIVALENCE object",
12319 sym->name, &e->where);
12320 return FAILURE;
12323 return SUCCESS;
12327 /* Resolve equivalence object.
12328 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
12329 an allocatable array, an object of nonsequence derived type, an object of
12330 sequence derived type containing a pointer at any level of component
12331 selection, an automatic object, a function name, an entry name, a result
12332 name, a named constant, a structure component, or a subobject of any of
12333 the preceding objects. A substring shall not have length zero. A
12334 derived type shall not have components with default initialization nor
12335 shall two objects of an equivalence group be initialized.
12336 Either all or none of the objects shall have an protected attribute.
12337 The simple constraints are done in symbol.c(check_conflict) and the rest
12338 are implemented here. */
12340 static void
12341 resolve_equivalence (gfc_equiv *eq)
12343 gfc_symbol *sym;
12344 gfc_symbol *first_sym;
12345 gfc_expr *e;
12346 gfc_ref *r;
12347 locus *last_where = NULL;
12348 seq_type eq_type, last_eq_type;
12349 gfc_typespec *last_ts;
12350 int object, cnt_protected;
12351 const char *msg;
12353 last_ts = &eq->expr->symtree->n.sym->ts;
12355 first_sym = eq->expr->symtree->n.sym;
12357 cnt_protected = 0;
12359 for (object = 1; eq; eq = eq->eq, object++)
12361 e = eq->expr;
12363 e->ts = e->symtree->n.sym->ts;
12364 /* match_varspec might not know yet if it is seeing
12365 array reference or substring reference, as it doesn't
12366 know the types. */
12367 if (e->ref && e->ref->type == REF_ARRAY)
12369 gfc_ref *ref = e->ref;
12370 sym = e->symtree->n.sym;
12372 if (sym->attr.dimension)
12374 ref->u.ar.as = sym->as;
12375 ref = ref->next;
12378 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
12379 if (e->ts.type == BT_CHARACTER
12380 && ref
12381 && ref->type == REF_ARRAY
12382 && ref->u.ar.dimen == 1
12383 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
12384 && ref->u.ar.stride[0] == NULL)
12386 gfc_expr *start = ref->u.ar.start[0];
12387 gfc_expr *end = ref->u.ar.end[0];
12388 void *mem = NULL;
12390 /* Optimize away the (:) reference. */
12391 if (start == NULL && end == NULL)
12393 if (e->ref == ref)
12394 e->ref = ref->next;
12395 else
12396 e->ref->next = ref->next;
12397 mem = ref;
12399 else
12401 ref->type = REF_SUBSTRING;
12402 if (start == NULL)
12403 start = gfc_get_int_expr (gfc_default_integer_kind,
12404 NULL, 1);
12405 ref->u.ss.start = start;
12406 if (end == NULL && e->ts.u.cl)
12407 end = gfc_copy_expr (e->ts.u.cl->length);
12408 ref->u.ss.end = end;
12409 ref->u.ss.length = e->ts.u.cl;
12410 e->ts.u.cl = NULL;
12412 ref = ref->next;
12413 gfc_free (mem);
12416 /* Any further ref is an error. */
12417 if (ref)
12419 gcc_assert (ref->type == REF_ARRAY);
12420 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
12421 &ref->u.ar.where);
12422 continue;
12426 if (gfc_resolve_expr (e) == FAILURE)
12427 continue;
12429 sym = e->symtree->n.sym;
12431 if (sym->attr.is_protected)
12432 cnt_protected++;
12433 if (cnt_protected > 0 && cnt_protected != object)
12435 gfc_error ("Either all or none of the objects in the "
12436 "EQUIVALENCE set at %L shall have the "
12437 "PROTECTED attribute",
12438 &e->where);
12439 break;
12442 /* Shall not equivalence common block variables in a PURE procedure. */
12443 if (sym->ns->proc_name
12444 && sym->ns->proc_name->attr.pure
12445 && sym->attr.in_common)
12447 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
12448 "object in the pure procedure '%s'",
12449 sym->name, &e->where, sym->ns->proc_name->name);
12450 break;
12453 /* Shall not be a named constant. */
12454 if (e->expr_type == EXPR_CONSTANT)
12456 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
12457 "object", sym->name, &e->where);
12458 continue;
12461 if (e->ts.type == BT_DERIVED
12462 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
12463 continue;
12465 /* Check that the types correspond correctly:
12466 Note 5.28:
12467 A numeric sequence structure may be equivalenced to another sequence
12468 structure, an object of default integer type, default real type, double
12469 precision real type, default logical type such that components of the
12470 structure ultimately only become associated to objects of the same
12471 kind. A character sequence structure may be equivalenced to an object
12472 of default character kind or another character sequence structure.
12473 Other objects may be equivalenced only to objects of the same type and
12474 kind parameters. */
12476 /* Identical types are unconditionally OK. */
12477 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
12478 goto identical_types;
12480 last_eq_type = sequence_type (*last_ts);
12481 eq_type = sequence_type (sym->ts);
12483 /* Since the pair of objects is not of the same type, mixed or
12484 non-default sequences can be rejected. */
12486 msg = "Sequence %s with mixed components in EQUIVALENCE "
12487 "statement at %L with different type objects";
12488 if ((object ==2
12489 && last_eq_type == SEQ_MIXED
12490 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
12491 == FAILURE)
12492 || (eq_type == SEQ_MIXED
12493 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12494 &e->where) == FAILURE))
12495 continue;
12497 msg = "Non-default type object or sequence %s in EQUIVALENCE "
12498 "statement at %L with objects of different type";
12499 if ((object ==2
12500 && last_eq_type == SEQ_NONDEFAULT
12501 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
12502 last_where) == FAILURE)
12503 || (eq_type == SEQ_NONDEFAULT
12504 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12505 &e->where) == FAILURE))
12506 continue;
12508 msg ="Non-CHARACTER object '%s' in default CHARACTER "
12509 "EQUIVALENCE statement at %L";
12510 if (last_eq_type == SEQ_CHARACTER
12511 && eq_type != SEQ_CHARACTER
12512 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12513 &e->where) == FAILURE)
12514 continue;
12516 msg ="Non-NUMERIC object '%s' in default NUMERIC "
12517 "EQUIVALENCE statement at %L";
12518 if (last_eq_type == SEQ_NUMERIC
12519 && eq_type != SEQ_NUMERIC
12520 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12521 &e->where) == FAILURE)
12522 continue;
12524 identical_types:
12525 last_ts =&sym->ts;
12526 last_where = &e->where;
12528 if (!e->ref)
12529 continue;
12531 /* Shall not be an automatic array. */
12532 if (e->ref->type == REF_ARRAY
12533 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
12535 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
12536 "an EQUIVALENCE object", sym->name, &e->where);
12537 continue;
12540 r = e->ref;
12541 while (r)
12543 /* Shall not be a structure component. */
12544 if (r->type == REF_COMPONENT)
12546 gfc_error ("Structure component '%s' at %L cannot be an "
12547 "EQUIVALENCE object",
12548 r->u.c.component->name, &e->where);
12549 break;
12552 /* A substring shall not have length zero. */
12553 if (r->type == REF_SUBSTRING)
12555 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
12557 gfc_error ("Substring at %L has length zero",
12558 &r->u.ss.start->where);
12559 break;
12562 r = r->next;
12568 /* Resolve function and ENTRY types, issue diagnostics if needed. */
12570 static void
12571 resolve_fntype (gfc_namespace *ns)
12573 gfc_entry_list *el;
12574 gfc_symbol *sym;
12576 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
12577 return;
12579 /* If there are any entries, ns->proc_name is the entry master
12580 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
12581 if (ns->entries)
12582 sym = ns->entries->sym;
12583 else
12584 sym = ns->proc_name;
12585 if (sym->result == sym
12586 && sym->ts.type == BT_UNKNOWN
12587 && gfc_set_default_type (sym, 0, NULL) == FAILURE
12588 && !sym->attr.untyped)
12590 gfc_error ("Function '%s' at %L has no IMPLICIT type",
12591 sym->name, &sym->declared_at);
12592 sym->attr.untyped = 1;
12595 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
12596 && !sym->attr.contained
12597 && !gfc_check_access (sym->ts.u.derived->attr.access,
12598 sym->ts.u.derived->ns->default_access)
12599 && gfc_check_access (sym->attr.access, sym->ns->default_access))
12601 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
12602 "%L of PRIVATE type '%s'", sym->name,
12603 &sym->declared_at, sym->ts.u.derived->name);
12606 if (ns->entries)
12607 for (el = ns->entries->next; el; el = el->next)
12609 if (el->sym->result == el->sym
12610 && el->sym->ts.type == BT_UNKNOWN
12611 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
12612 && !el->sym->attr.untyped)
12614 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
12615 el->sym->name, &el->sym->declared_at);
12616 el->sym->attr.untyped = 1;
12622 /* 12.3.2.1.1 Defined operators. */
12624 static gfc_try
12625 check_uop_procedure (gfc_symbol *sym, locus where)
12627 gfc_formal_arglist *formal;
12629 if (!sym->attr.function)
12631 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
12632 sym->name, &where);
12633 return FAILURE;
12636 if (sym->ts.type == BT_CHARACTER
12637 && !(sym->ts.u.cl && sym->ts.u.cl->length)
12638 && !(sym->result && sym->result->ts.u.cl
12639 && sym->result->ts.u.cl->length))
12641 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
12642 "character length", sym->name, &where);
12643 return FAILURE;
12646 formal = sym->formal;
12647 if (!formal || !formal->sym)
12649 gfc_error ("User operator procedure '%s' at %L must have at least "
12650 "one argument", sym->name, &where);
12651 return FAILURE;
12654 if (formal->sym->attr.intent != INTENT_IN)
12656 gfc_error ("First argument of operator interface at %L must be "
12657 "INTENT(IN)", &where);
12658 return FAILURE;
12661 if (formal->sym->attr.optional)
12663 gfc_error ("First argument of operator interface at %L cannot be "
12664 "optional", &where);
12665 return FAILURE;
12668 formal = formal->next;
12669 if (!formal || !formal->sym)
12670 return SUCCESS;
12672 if (formal->sym->attr.intent != INTENT_IN)
12674 gfc_error ("Second argument of operator interface at %L must be "
12675 "INTENT(IN)", &where);
12676 return FAILURE;
12679 if (formal->sym->attr.optional)
12681 gfc_error ("Second argument of operator interface at %L cannot be "
12682 "optional", &where);
12683 return FAILURE;
12686 if (formal->next)
12688 gfc_error ("Operator interface at %L must have, at most, two "
12689 "arguments", &where);
12690 return FAILURE;
12693 return SUCCESS;
12696 static void
12697 gfc_resolve_uops (gfc_symtree *symtree)
12699 gfc_interface *itr;
12701 if (symtree == NULL)
12702 return;
12704 gfc_resolve_uops (symtree->left);
12705 gfc_resolve_uops (symtree->right);
12707 for (itr = symtree->n.uop->op; itr; itr = itr->next)
12708 check_uop_procedure (itr->sym, itr->sym->declared_at);
12712 /* Examine all of the expressions associated with a program unit,
12713 assign types to all intermediate expressions, make sure that all
12714 assignments are to compatible types and figure out which names
12715 refer to which functions or subroutines. It doesn't check code
12716 block, which is handled by resolve_code. */
12718 static void
12719 resolve_types (gfc_namespace *ns)
12721 gfc_namespace *n;
12722 gfc_charlen *cl;
12723 gfc_data *d;
12724 gfc_equiv *eq;
12725 gfc_namespace* old_ns = gfc_current_ns;
12727 /* Check that all IMPLICIT types are ok. */
12728 if (!ns->seen_implicit_none)
12730 unsigned letter;
12731 for (letter = 0; letter != GFC_LETTERS; ++letter)
12732 if (ns->set_flag[letter]
12733 && resolve_typespec_used (&ns->default_type[letter],
12734 &ns->implicit_loc[letter],
12735 NULL) == FAILURE)
12736 return;
12739 gfc_current_ns = ns;
12741 resolve_entries (ns);
12743 resolve_common_vars (ns->blank_common.head, false);
12744 resolve_common_blocks (ns->common_root);
12746 resolve_contained_functions (ns);
12748 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
12750 for (cl = ns->cl_list; cl; cl = cl->next)
12751 resolve_charlen (cl);
12753 gfc_traverse_ns (ns, resolve_symbol);
12755 resolve_fntype (ns);
12757 for (n = ns->contained; n; n = n->sibling)
12759 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
12760 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
12761 "also be PURE", n->proc_name->name,
12762 &n->proc_name->declared_at);
12764 resolve_types (n);
12767 forall_flag = 0;
12768 gfc_check_interfaces (ns);
12770 gfc_traverse_ns (ns, resolve_values);
12772 if (ns->save_all)
12773 gfc_save_all (ns);
12775 iter_stack = NULL;
12776 for (d = ns->data; d; d = d->next)
12777 resolve_data (d);
12779 iter_stack = NULL;
12780 gfc_traverse_ns (ns, gfc_formalize_init_value);
12782 gfc_traverse_ns (ns, gfc_verify_binding_labels);
12784 if (ns->common_root != NULL)
12785 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
12787 for (eq = ns->equiv; eq; eq = eq->next)
12788 resolve_equivalence (eq);
12790 /* Warn about unused labels. */
12791 if (warn_unused_label)
12792 warn_unused_fortran_label (ns->st_labels);
12794 gfc_resolve_uops (ns->uop_root);
12796 gfc_current_ns = old_ns;
12800 /* Call resolve_code recursively. */
12802 static void
12803 resolve_codes (gfc_namespace *ns)
12805 gfc_namespace *n;
12806 bitmap_obstack old_obstack;
12808 for (n = ns->contained; n; n = n->sibling)
12809 resolve_codes (n);
12811 gfc_current_ns = ns;
12813 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
12814 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
12815 cs_base = NULL;
12817 /* Set to an out of range value. */
12818 current_entry_id = -1;
12820 old_obstack = labels_obstack;
12821 bitmap_obstack_initialize (&labels_obstack);
12823 resolve_code (ns->code, ns);
12825 bitmap_obstack_release (&labels_obstack);
12826 labels_obstack = old_obstack;
12830 /* This function is called after a complete program unit has been compiled.
12831 Its purpose is to examine all of the expressions associated with a program
12832 unit, assign types to all intermediate expressions, make sure that all
12833 assignments are to compatible types and figure out which names refer to
12834 which functions or subroutines. */
12836 void
12837 gfc_resolve (gfc_namespace *ns)
12839 gfc_namespace *old_ns;
12840 code_stack *old_cs_base;
12842 if (ns->resolved)
12843 return;
12845 ns->resolved = -1;
12846 old_ns = gfc_current_ns;
12847 old_cs_base = cs_base;
12849 resolve_types (ns);
12850 resolve_codes (ns);
12852 gfc_current_ns = old_ns;
12853 cs_base = old_cs_base;
12854 ns->resolved = 1;