2011-10-07 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / resolve.c
blob84db3ddb019f0aa46d970f26b095dc21de788176
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "flags.h"
26 #include "gfortran.h"
27 #include "obstack.h"
28 #include "bitmap.h"
29 #include "arith.h" /* For gfc_compare_expr(). */
30 #include "dependency.h"
31 #include "data.h"
32 #include "target-memory.h" /* for gfc_simplify_transfer */
33 #include "constructor.h"
35 /* Types used in equivalence statements. */
37 typedef enum seq_type
39 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
41 seq_type;
43 /* Stack to keep track of the nesting of blocks as we move through the
44 code. See resolve_branch() and resolve_code(). */
46 typedef struct code_stack
48 struct gfc_code *head, *current;
49 struct code_stack *prev;
51 /* This bitmap keeps track of the targets valid for a branch from
52 inside this block except for END {IF|SELECT}s of enclosing
53 blocks. */
54 bitmap reachable_labels;
56 code_stack;
58 static code_stack *cs_base = NULL;
61 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
63 static int forall_flag;
64 static int do_concurrent_flag;
66 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
68 static int omp_workshare_flag;
70 /* Nonzero if we are processing a formal arglist. The corresponding function
71 resets the flag each time that it is read. */
72 static int formal_arg_flag = 0;
74 /* True if we are resolving a specification expression. */
75 static int specification_expr = 0;
77 /* The id of the last entry seen. */
78 static int current_entry_id;
80 /* We use bitmaps to determine if a branch target is valid. */
81 static bitmap_obstack labels_obstack;
83 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
84 static bool inquiry_argument = false;
86 int
87 gfc_is_formal_arg (void)
89 return formal_arg_flag;
92 /* Is the symbol host associated? */
93 static bool
94 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
96 for (ns = ns->parent; ns; ns = ns->parent)
98 if (sym->ns == ns)
99 return true;
102 return false;
105 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
106 an ABSTRACT derived-type. If where is not NULL, an error message with that
107 locus is printed, optionally using name. */
109 static gfc_try
110 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
112 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
114 if (where)
116 if (name)
117 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
118 name, where, ts->u.derived->name);
119 else
120 gfc_error ("ABSTRACT type '%s' used at %L",
121 ts->u.derived->name, where);
124 return FAILURE;
127 return SUCCESS;
131 static void resolve_symbol (gfc_symbol *sym);
132 static gfc_try resolve_intrinsic (gfc_symbol *sym, locus *loc);
135 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
137 static gfc_try
138 resolve_procedure_interface (gfc_symbol *sym)
140 if (sym->ts.interface == sym)
142 gfc_error ("PROCEDURE '%s' at %L may not be used as its own interface",
143 sym->name, &sym->declared_at);
144 return FAILURE;
146 if (sym->ts.interface->attr.procedure)
148 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared "
149 "in a later PROCEDURE statement", sym->ts.interface->name,
150 sym->name, &sym->declared_at);
151 return FAILURE;
154 /* Get the attributes from the interface (now resolved). */
155 if (sym->ts.interface->attr.if_source || sym->ts.interface->attr.intrinsic)
157 gfc_symbol *ifc = sym->ts.interface;
158 resolve_symbol (ifc);
160 if (ifc->attr.intrinsic)
161 resolve_intrinsic (ifc, &ifc->declared_at);
163 if (ifc->result)
165 sym->ts = ifc->result->ts;
166 sym->result = sym;
168 else
169 sym->ts = ifc->ts;
170 sym->ts.interface = ifc;
171 sym->attr.function = ifc->attr.function;
172 sym->attr.subroutine = ifc->attr.subroutine;
173 gfc_copy_formal_args (sym, ifc);
175 sym->attr.allocatable = ifc->attr.allocatable;
176 sym->attr.pointer = ifc->attr.pointer;
177 sym->attr.pure = ifc->attr.pure;
178 sym->attr.elemental = ifc->attr.elemental;
179 sym->attr.dimension = ifc->attr.dimension;
180 sym->attr.contiguous = ifc->attr.contiguous;
181 sym->attr.recursive = ifc->attr.recursive;
182 sym->attr.always_explicit = ifc->attr.always_explicit;
183 sym->attr.ext_attr |= ifc->attr.ext_attr;
184 sym->attr.is_bind_c = ifc->attr.is_bind_c;
185 /* Copy array spec. */
186 sym->as = gfc_copy_array_spec (ifc->as);
187 if (sym->as)
189 int i;
190 for (i = 0; i < sym->as->rank; i++)
192 gfc_expr_replace_symbols (sym->as->lower[i], sym);
193 gfc_expr_replace_symbols (sym->as->upper[i], sym);
196 /* Copy char length. */
197 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
199 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
200 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
201 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
202 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
203 return FAILURE;
206 else if (sym->ts.interface->name[0] != '\0')
208 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
209 sym->ts.interface->name, sym->name, &sym->declared_at);
210 return FAILURE;
213 return SUCCESS;
217 /* Resolve types of formal argument lists. These have to be done early so that
218 the formal argument lists of module procedures can be copied to the
219 containing module before the individual procedures are resolved
220 individually. We also resolve argument lists of procedures in interface
221 blocks because they are self-contained scoping units.
223 Since a dummy argument cannot be a non-dummy procedure, the only
224 resort left for untyped names are the IMPLICIT types. */
226 static void
227 resolve_formal_arglist (gfc_symbol *proc)
229 gfc_formal_arglist *f;
230 gfc_symbol *sym;
231 int i;
233 if (proc->result != NULL)
234 sym = proc->result;
235 else
236 sym = proc;
238 if (gfc_elemental (proc)
239 || sym->attr.pointer || sym->attr.allocatable
240 || (sym->as && sym->as->rank > 0))
242 proc->attr.always_explicit = 1;
243 sym->attr.always_explicit = 1;
246 formal_arg_flag = 1;
248 for (f = proc->formal; f; f = f->next)
250 sym = f->sym;
252 if (sym == NULL)
254 /* Alternate return placeholder. */
255 if (gfc_elemental (proc))
256 gfc_error ("Alternate return specifier in elemental subroutine "
257 "'%s' at %L is not allowed", proc->name,
258 &proc->declared_at);
259 if (proc->attr.function)
260 gfc_error ("Alternate return specifier in function "
261 "'%s' at %L is not allowed", proc->name,
262 &proc->declared_at);
263 continue;
265 else if (sym->attr.procedure && sym->ts.interface
266 && sym->attr.if_source != IFSRC_DECL)
267 resolve_procedure_interface (sym);
269 if (sym->attr.if_source != IFSRC_UNKNOWN)
270 resolve_formal_arglist (sym);
272 /* F08:C1279. */
273 if (gfc_pure (proc)
274 && sym->attr.flavor == FL_PROCEDURE && !gfc_pure (sym))
276 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
277 "also be PURE", sym->name, &sym->declared_at);
278 continue;
281 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
283 if (proc->attr.implicit_pure && !gfc_pure(sym))
284 proc->attr.implicit_pure = 0;
286 /* F08:C1289. */
287 if (gfc_elemental (proc))
289 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
290 "procedure", &sym->declared_at);
291 continue;
294 if (sym->attr.function
295 && sym->ts.type == BT_UNKNOWN
296 && sym->attr.intrinsic)
298 gfc_intrinsic_sym *isym;
299 isym = gfc_find_function (sym->name);
300 if (isym == NULL || !isym->specific)
302 gfc_error ("Unable to find a specific INTRINSIC procedure "
303 "for the reference '%s' at %L", sym->name,
304 &sym->declared_at);
306 sym->ts = isym->ts;
309 continue;
312 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
313 && (!sym->attr.function || sym->result == sym))
314 gfc_set_default_type (sym, 1, sym->ns);
316 gfc_resolve_array_spec (sym->as, 0);
318 /* We can't tell if an array with dimension (:) is assumed or deferred
319 shape until we know if it has the pointer or allocatable attributes.
321 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
322 && !(sym->attr.pointer || sym->attr.allocatable)
323 && sym->attr.flavor != FL_PROCEDURE)
325 sym->as->type = AS_ASSUMED_SHAPE;
326 for (i = 0; i < sym->as->rank; i++)
327 sym->as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind,
328 NULL, 1);
331 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
332 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
333 || sym->attr.optional)
335 proc->attr.always_explicit = 1;
336 if (proc->result)
337 proc->result->attr.always_explicit = 1;
340 /* If the flavor is unknown at this point, it has to be a variable.
341 A procedure specification would have already set the type. */
343 if (sym->attr.flavor == FL_UNKNOWN)
344 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
346 if (gfc_pure (proc) && !sym->attr.pointer
347 && sym->attr.flavor != FL_PROCEDURE)
349 if (proc->attr.function && sym->attr.intent != INTENT_IN)
351 if (sym->attr.value)
352 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Argument '%s' "
353 "of pure function '%s' at %L with VALUE "
354 "attribute but without INTENT(IN)", sym->name,
355 proc->name, &sym->declared_at);
356 else
357 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
358 "INTENT(IN) or VALUE", sym->name, proc->name,
359 &sym->declared_at);
362 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
364 if (sym->attr.value)
365 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Argument '%s' "
366 "of pure subroutine '%s' at %L with VALUE "
367 "attribute but without INTENT", sym->name,
368 proc->name, &sym->declared_at);
369 else
370 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
371 "have its INTENT specified or have the VALUE "
372 "attribute", sym->name, proc->name, &sym->declared_at);
376 if (proc->attr.implicit_pure && !sym->attr.pointer
377 && sym->attr.flavor != FL_PROCEDURE)
379 if (proc->attr.function && sym->attr.intent != INTENT_IN)
380 proc->attr.implicit_pure = 0;
382 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
383 proc->attr.implicit_pure = 0;
386 if (gfc_elemental (proc))
388 /* F08:C1289. */
389 if (sym->attr.codimension)
391 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
392 "procedure", sym->name, &sym->declared_at);
393 continue;
396 if (sym->as != NULL)
398 gfc_error ("Argument '%s' of elemental procedure at %L must "
399 "be scalar", sym->name, &sym->declared_at);
400 continue;
403 if (sym->attr.allocatable)
405 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
406 "have the ALLOCATABLE attribute", sym->name,
407 &sym->declared_at);
408 continue;
411 if (sym->attr.pointer)
413 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
414 "have the POINTER attribute", sym->name,
415 &sym->declared_at);
416 continue;
419 if (sym->attr.flavor == FL_PROCEDURE)
421 gfc_error ("Dummy procedure '%s' not allowed in elemental "
422 "procedure '%s' at %L", sym->name, proc->name,
423 &sym->declared_at);
424 continue;
427 if (sym->attr.intent == INTENT_UNKNOWN)
429 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
430 "have its INTENT specified", sym->name, proc->name,
431 &sym->declared_at);
432 continue;
436 /* Each dummy shall be specified to be scalar. */
437 if (proc->attr.proc == PROC_ST_FUNCTION)
439 if (sym->as != NULL)
441 gfc_error ("Argument '%s' of statement function at %L must "
442 "be scalar", sym->name, &sym->declared_at);
443 continue;
446 if (sym->ts.type == BT_CHARACTER)
448 gfc_charlen *cl = sym->ts.u.cl;
449 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
451 gfc_error ("Character-valued argument '%s' of statement "
452 "function at %L must have constant length",
453 sym->name, &sym->declared_at);
454 continue;
459 formal_arg_flag = 0;
463 /* Work function called when searching for symbols that have argument lists
464 associated with them. */
466 static void
467 find_arglists (gfc_symbol *sym)
469 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
470 return;
472 resolve_formal_arglist (sym);
476 /* Given a namespace, resolve all formal argument lists within the namespace.
479 static void
480 resolve_formal_arglists (gfc_namespace *ns)
482 if (ns == NULL)
483 return;
485 gfc_traverse_ns (ns, find_arglists);
489 static void
490 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
492 gfc_try t;
494 /* If this namespace is not a function or an entry master function,
495 ignore it. */
496 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
497 || sym->attr.entry_master)
498 return;
500 /* Try to find out of what the return type is. */
501 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
503 t = gfc_set_default_type (sym->result, 0, ns);
505 if (t == FAILURE && !sym->result->attr.untyped)
507 if (sym->result == sym)
508 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
509 sym->name, &sym->declared_at);
510 else if (!sym->result->attr.proc_pointer)
511 gfc_error ("Result '%s' of contained function '%s' at %L has "
512 "no IMPLICIT type", sym->result->name, sym->name,
513 &sym->result->declared_at);
514 sym->result->attr.untyped = 1;
518 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
519 type, lists the only ways a character length value of * can be used:
520 dummy arguments of procedures, named constants, and function results
521 in external functions. Internal function results and results of module
522 procedures are not on this list, ergo, not permitted. */
524 if (sym->result->ts.type == BT_CHARACTER)
526 gfc_charlen *cl = sym->result->ts.u.cl;
527 if ((!cl || !cl->length) && !sym->result->ts.deferred)
529 /* See if this is a module-procedure and adapt error message
530 accordingly. */
531 bool module_proc;
532 gcc_assert (ns->parent && ns->parent->proc_name);
533 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
535 gfc_error ("Character-valued %s '%s' at %L must not be"
536 " assumed length",
537 module_proc ? _("module procedure")
538 : _("internal function"),
539 sym->name, &sym->declared_at);
545 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
546 introduce duplicates. */
548 static void
549 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
551 gfc_formal_arglist *f, *new_arglist;
552 gfc_symbol *new_sym;
554 for (; new_args != NULL; new_args = new_args->next)
556 new_sym = new_args->sym;
557 /* See if this arg is already in the formal argument list. */
558 for (f = proc->formal; f; f = f->next)
560 if (new_sym == f->sym)
561 break;
564 if (f)
565 continue;
567 /* Add a new argument. Argument order is not important. */
568 new_arglist = gfc_get_formal_arglist ();
569 new_arglist->sym = new_sym;
570 new_arglist->next = proc->formal;
571 proc->formal = new_arglist;
576 /* Flag the arguments that are not present in all entries. */
578 static void
579 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
581 gfc_formal_arglist *f, *head;
582 head = new_args;
584 for (f = proc->formal; f; f = f->next)
586 if (f->sym == NULL)
587 continue;
589 for (new_args = head; new_args; new_args = new_args->next)
591 if (new_args->sym == f->sym)
592 break;
595 if (new_args)
596 continue;
598 f->sym->attr.not_always_present = 1;
603 /* Resolve alternate entry points. If a symbol has multiple entry points we
604 create a new master symbol for the main routine, and turn the existing
605 symbol into an entry point. */
607 static void
608 resolve_entries (gfc_namespace *ns)
610 gfc_namespace *old_ns;
611 gfc_code *c;
612 gfc_symbol *proc;
613 gfc_entry_list *el;
614 char name[GFC_MAX_SYMBOL_LEN + 1];
615 static int master_count = 0;
617 if (ns->proc_name == NULL)
618 return;
620 /* No need to do anything if this procedure doesn't have alternate entry
621 points. */
622 if (!ns->entries)
623 return;
625 /* We may already have resolved alternate entry points. */
626 if (ns->proc_name->attr.entry_master)
627 return;
629 /* If this isn't a procedure something has gone horribly wrong. */
630 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
632 /* Remember the current namespace. */
633 old_ns = gfc_current_ns;
635 gfc_current_ns = ns;
637 /* Add the main entry point to the list of entry points. */
638 el = gfc_get_entry_list ();
639 el->sym = ns->proc_name;
640 el->id = 0;
641 el->next = ns->entries;
642 ns->entries = el;
643 ns->proc_name->attr.entry = 1;
645 /* If it is a module function, it needs to be in the right namespace
646 so that gfc_get_fake_result_decl can gather up the results. The
647 need for this arose in get_proc_name, where these beasts were
648 left in their own namespace, to keep prior references linked to
649 the entry declaration.*/
650 if (ns->proc_name->attr.function
651 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
652 el->sym->ns = ns;
654 /* Do the same for entries where the master is not a module
655 procedure. These are retained in the module namespace because
656 of the module procedure declaration. */
657 for (el = el->next; el; el = el->next)
658 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
659 && el->sym->attr.mod_proc)
660 el->sym->ns = ns;
661 el = ns->entries;
663 /* Add an entry statement for it. */
664 c = gfc_get_code ();
665 c->op = EXEC_ENTRY;
666 c->ext.entry = el;
667 c->next = ns->code;
668 ns->code = c;
670 /* Create a new symbol for the master function. */
671 /* Give the internal function a unique name (within this file).
672 Also include the function name so the user has some hope of figuring
673 out what is going on. */
674 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
675 master_count++, ns->proc_name->name);
676 gfc_get_ha_symbol (name, &proc);
677 gcc_assert (proc != NULL);
679 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
680 if (ns->proc_name->attr.subroutine)
681 gfc_add_subroutine (&proc->attr, proc->name, NULL);
682 else
684 gfc_symbol *sym;
685 gfc_typespec *ts, *fts;
686 gfc_array_spec *as, *fas;
687 gfc_add_function (&proc->attr, proc->name, NULL);
688 proc->result = proc;
689 fas = ns->entries->sym->as;
690 fas = fas ? fas : ns->entries->sym->result->as;
691 fts = &ns->entries->sym->result->ts;
692 if (fts->type == BT_UNKNOWN)
693 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
694 for (el = ns->entries->next; el; el = el->next)
696 ts = &el->sym->result->ts;
697 as = el->sym->as;
698 as = as ? as : el->sym->result->as;
699 if (ts->type == BT_UNKNOWN)
700 ts = gfc_get_default_type (el->sym->result->name, NULL);
702 if (! gfc_compare_types (ts, fts)
703 || (el->sym->result->attr.dimension
704 != ns->entries->sym->result->attr.dimension)
705 || (el->sym->result->attr.pointer
706 != ns->entries->sym->result->attr.pointer))
707 break;
708 else if (as && fas && ns->entries->sym->result != el->sym->result
709 && gfc_compare_array_spec (as, fas) == 0)
710 gfc_error ("Function %s at %L has entries with mismatched "
711 "array specifications", ns->entries->sym->name,
712 &ns->entries->sym->declared_at);
713 /* The characteristics need to match and thus both need to have
714 the same string length, i.e. both len=*, or both len=4.
715 Having both len=<variable> is also possible, but difficult to
716 check at compile time. */
717 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
718 && (((ts->u.cl->length && !fts->u.cl->length)
719 ||(!ts->u.cl->length && fts->u.cl->length))
720 || (ts->u.cl->length
721 && ts->u.cl->length->expr_type
722 != fts->u.cl->length->expr_type)
723 || (ts->u.cl->length
724 && ts->u.cl->length->expr_type == EXPR_CONSTANT
725 && mpz_cmp (ts->u.cl->length->value.integer,
726 fts->u.cl->length->value.integer) != 0)))
727 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
728 "entries returning variables of different "
729 "string lengths", ns->entries->sym->name,
730 &ns->entries->sym->declared_at);
733 if (el == NULL)
735 sym = ns->entries->sym->result;
736 /* All result types the same. */
737 proc->ts = *fts;
738 if (sym->attr.dimension)
739 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
740 if (sym->attr.pointer)
741 gfc_add_pointer (&proc->attr, NULL);
743 else
745 /* Otherwise the result will be passed through a union by
746 reference. */
747 proc->attr.mixed_entry_master = 1;
748 for (el = ns->entries; el; el = el->next)
750 sym = el->sym->result;
751 if (sym->attr.dimension)
753 if (el == ns->entries)
754 gfc_error ("FUNCTION result %s can't be an array in "
755 "FUNCTION %s at %L", sym->name,
756 ns->entries->sym->name, &sym->declared_at);
757 else
758 gfc_error ("ENTRY result %s can't be an array in "
759 "FUNCTION %s at %L", sym->name,
760 ns->entries->sym->name, &sym->declared_at);
762 else if (sym->attr.pointer)
764 if (el == ns->entries)
765 gfc_error ("FUNCTION result %s can't be a POINTER in "
766 "FUNCTION %s at %L", sym->name,
767 ns->entries->sym->name, &sym->declared_at);
768 else
769 gfc_error ("ENTRY result %s can't be a POINTER in "
770 "FUNCTION %s at %L", sym->name,
771 ns->entries->sym->name, &sym->declared_at);
773 else
775 ts = &sym->ts;
776 if (ts->type == BT_UNKNOWN)
777 ts = gfc_get_default_type (sym->name, NULL);
778 switch (ts->type)
780 case BT_INTEGER:
781 if (ts->kind == gfc_default_integer_kind)
782 sym = NULL;
783 break;
784 case BT_REAL:
785 if (ts->kind == gfc_default_real_kind
786 || ts->kind == gfc_default_double_kind)
787 sym = NULL;
788 break;
789 case BT_COMPLEX:
790 if (ts->kind == gfc_default_complex_kind)
791 sym = NULL;
792 break;
793 case BT_LOGICAL:
794 if (ts->kind == gfc_default_logical_kind)
795 sym = NULL;
796 break;
797 case BT_UNKNOWN:
798 /* We will issue error elsewhere. */
799 sym = NULL;
800 break;
801 default:
802 break;
804 if (sym)
806 if (el == ns->entries)
807 gfc_error ("FUNCTION result %s can't be of type %s "
808 "in FUNCTION %s at %L", sym->name,
809 gfc_typename (ts), ns->entries->sym->name,
810 &sym->declared_at);
811 else
812 gfc_error ("ENTRY result %s can't be of type %s "
813 "in FUNCTION %s at %L", sym->name,
814 gfc_typename (ts), ns->entries->sym->name,
815 &sym->declared_at);
821 proc->attr.access = ACCESS_PRIVATE;
822 proc->attr.entry_master = 1;
824 /* Merge all the entry point arguments. */
825 for (el = ns->entries; el; el = el->next)
826 merge_argument_lists (proc, el->sym->formal);
828 /* Check the master formal arguments for any that are not
829 present in all entry points. */
830 for (el = ns->entries; el; el = el->next)
831 check_argument_lists (proc, el->sym->formal);
833 /* Use the master function for the function body. */
834 ns->proc_name = proc;
836 /* Finalize the new symbols. */
837 gfc_commit_symbols ();
839 /* Restore the original namespace. */
840 gfc_current_ns = old_ns;
844 /* Resolve common variables. */
845 static void
846 resolve_common_vars (gfc_symbol *sym, bool named_common)
848 gfc_symbol *csym = sym;
850 for (; csym; csym = csym->common_next)
852 if (csym->value || csym->attr.data)
854 if (!csym->ns->is_block_data)
855 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
856 "but only in BLOCK DATA initialization is "
857 "allowed", csym->name, &csym->declared_at);
858 else if (!named_common)
859 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
860 "in a blank COMMON but initialization is only "
861 "allowed in named common blocks", csym->name,
862 &csym->declared_at);
865 if (csym->ts.type != BT_DERIVED)
866 continue;
868 if (!(csym->ts.u.derived->attr.sequence
869 || csym->ts.u.derived->attr.is_bind_c))
870 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
871 "has neither the SEQUENCE nor the BIND(C) "
872 "attribute", csym->name, &csym->declared_at);
873 if (csym->ts.u.derived->attr.alloc_comp)
874 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
875 "has an ultimate component that is "
876 "allocatable", csym->name, &csym->declared_at);
877 if (gfc_has_default_initializer (csym->ts.u.derived))
878 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
879 "may not have default initializer", csym->name,
880 &csym->declared_at);
882 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
883 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
887 /* Resolve common blocks. */
888 static void
889 resolve_common_blocks (gfc_symtree *common_root)
891 gfc_symbol *sym;
893 if (common_root == NULL)
894 return;
896 if (common_root->left)
897 resolve_common_blocks (common_root->left);
898 if (common_root->right)
899 resolve_common_blocks (common_root->right);
901 resolve_common_vars (common_root->n.common->head, true);
903 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
904 if (sym == NULL)
905 return;
907 if (sym->attr.flavor == FL_PARAMETER)
908 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
909 sym->name, &common_root->n.common->where, &sym->declared_at);
911 if (sym->attr.external)
912 gfc_error ("COMMON block '%s' at %L can not have the EXTERNAL attribute",
913 sym->name, &common_root->n.common->where);
915 if (sym->attr.intrinsic)
916 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
917 sym->name, &common_root->n.common->where);
918 else if (sym->attr.result
919 || gfc_is_function_return_value (sym, gfc_current_ns))
920 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
921 "that is also a function result", sym->name,
922 &common_root->n.common->where);
923 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
924 && sym->attr.proc != PROC_ST_FUNCTION)
925 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
926 "that is also a global procedure", sym->name,
927 &common_root->n.common->where);
931 /* Resolve contained function types. Because contained functions can call one
932 another, they have to be worked out before any of the contained procedures
933 can be resolved.
935 The good news is that if a function doesn't already have a type, the only
936 way it can get one is through an IMPLICIT type or a RESULT variable, because
937 by definition contained functions are contained namespace they're contained
938 in, not in a sibling or parent namespace. */
940 static void
941 resolve_contained_functions (gfc_namespace *ns)
943 gfc_namespace *child;
944 gfc_entry_list *el;
946 resolve_formal_arglists (ns);
948 for (child = ns->contained; child; child = child->sibling)
950 /* Resolve alternate entry points first. */
951 resolve_entries (child);
953 /* Then check function return types. */
954 resolve_contained_fntype (child->proc_name, child);
955 for (el = child->entries; el; el = el->next)
956 resolve_contained_fntype (el->sym, child);
961 static gfc_try resolve_fl_derived0 (gfc_symbol *sym);
964 /* Resolve all of the elements of a structure constructor and make sure that
965 the types are correct. The 'init' flag indicates that the given
966 constructor is an initializer. */
968 static gfc_try
969 resolve_structure_cons (gfc_expr *expr, int init)
971 gfc_constructor *cons;
972 gfc_component *comp;
973 gfc_try t;
974 symbol_attribute a;
976 t = SUCCESS;
978 if (expr->ts.type == BT_DERIVED)
979 resolve_fl_derived0 (expr->ts.u.derived);
981 cons = gfc_constructor_first (expr->value.constructor);
982 /* A constructor may have references if it is the result of substituting a
983 parameter variable. In this case we just pull out the component we
984 want. */
985 if (expr->ref)
986 comp = expr->ref->u.c.sym->components;
987 else
988 comp = expr->ts.u.derived->components;
990 /* See if the user is trying to invoke a structure constructor for one of
991 the iso_c_binding derived types. */
992 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
993 && expr->ts.u.derived->ts.is_iso_c && cons
994 && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
996 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
997 expr->ts.u.derived->name, &(expr->where));
998 return FAILURE;
1001 /* Return if structure constructor is c_null_(fun)prt. */
1002 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
1003 && expr->ts.u.derived->ts.is_iso_c && cons
1004 && cons->expr && cons->expr->expr_type == EXPR_NULL)
1005 return SUCCESS;
1007 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1009 int rank;
1011 if (!cons->expr)
1012 continue;
1014 if (gfc_resolve_expr (cons->expr) == FAILURE)
1016 t = FAILURE;
1017 continue;
1020 rank = comp->as ? comp->as->rank : 0;
1021 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1022 && (comp->attr.allocatable || cons->expr->rank))
1024 gfc_error ("The rank of the element in the structure "
1025 "constructor at %L does not match that of the "
1026 "component (%d/%d)", &cons->expr->where,
1027 cons->expr->rank, rank);
1028 t = FAILURE;
1031 /* If we don't have the right type, try to convert it. */
1033 if (!comp->attr.proc_pointer &&
1034 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1036 t = FAILURE;
1037 if (strcmp (comp->name, "_extends") == 0)
1039 /* Can afford to be brutal with the _extends initializer.
1040 The derived type can get lost because it is PRIVATE
1041 but it is not usage constrained by the standard. */
1042 cons->expr->ts = comp->ts;
1043 t = SUCCESS;
1045 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1046 gfc_error ("The element in the structure constructor at %L, "
1047 "for pointer component '%s', is %s but should be %s",
1048 &cons->expr->where, comp->name,
1049 gfc_basic_typename (cons->expr->ts.type),
1050 gfc_basic_typename (comp->ts.type));
1051 else
1052 t = gfc_convert_type (cons->expr, &comp->ts, 1);
1055 /* For strings, the length of the constructor should be the same as
1056 the one of the structure, ensure this if the lengths are known at
1057 compile time and when we are dealing with PARAMETER or structure
1058 constructors. */
1059 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1060 && comp->ts.u.cl->length
1061 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1062 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1063 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1064 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1065 comp->ts.u.cl->length->value.integer) != 0)
1067 if (cons->expr->expr_type == EXPR_VARIABLE
1068 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1070 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1071 to make use of the gfc_resolve_character_array_constructor
1072 machinery. The expression is later simplified away to
1073 an array of string literals. */
1074 gfc_expr *para = cons->expr;
1075 cons->expr = gfc_get_expr ();
1076 cons->expr->ts = para->ts;
1077 cons->expr->where = para->where;
1078 cons->expr->expr_type = EXPR_ARRAY;
1079 cons->expr->rank = para->rank;
1080 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1081 gfc_constructor_append_expr (&cons->expr->value.constructor,
1082 para, &cons->expr->where);
1084 if (cons->expr->expr_type == EXPR_ARRAY)
1086 gfc_constructor *p;
1087 p = gfc_constructor_first (cons->expr->value.constructor);
1088 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1090 gfc_charlen *cl, *cl2;
1092 cl2 = NULL;
1093 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1095 if (cl == cons->expr->ts.u.cl)
1096 break;
1097 cl2 = cl;
1100 gcc_assert (cl);
1102 if (cl2)
1103 cl2->next = cl->next;
1105 gfc_free_expr (cl->length);
1106 free (cl);
1109 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1110 cons->expr->ts.u.cl->length_from_typespec = true;
1111 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1112 gfc_resolve_character_array_constructor (cons->expr);
1116 if (cons->expr->expr_type == EXPR_NULL
1117 && !(comp->attr.pointer || comp->attr.allocatable
1118 || comp->attr.proc_pointer
1119 || (comp->ts.type == BT_CLASS
1120 && (CLASS_DATA (comp)->attr.class_pointer
1121 || CLASS_DATA (comp)->attr.allocatable))))
1123 t = FAILURE;
1124 gfc_error ("The NULL in the structure constructor at %L is "
1125 "being applied to component '%s', which is neither "
1126 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1127 comp->name);
1130 if (comp->attr.proc_pointer && comp->ts.interface)
1132 /* Check procedure pointer interface. */
1133 gfc_symbol *s2 = NULL;
1134 gfc_component *c2;
1135 const char *name;
1136 char err[200];
1138 if (gfc_is_proc_ptr_comp (cons->expr, &c2))
1140 s2 = c2->ts.interface;
1141 name = c2->name;
1143 else if (cons->expr->expr_type == EXPR_FUNCTION)
1145 s2 = cons->expr->symtree->n.sym->result;
1146 name = cons->expr->symtree->n.sym->result->name;
1148 else if (cons->expr->expr_type != EXPR_NULL)
1150 s2 = cons->expr->symtree->n.sym;
1151 name = cons->expr->symtree->n.sym->name;
1154 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1155 err, sizeof (err)))
1157 gfc_error ("Interface mismatch for procedure-pointer component "
1158 "'%s' in structure constructor at %L: %s",
1159 comp->name, &cons->expr->where, err);
1160 return FAILURE;
1164 if (!comp->attr.pointer || comp->attr.proc_pointer
1165 || cons->expr->expr_type == EXPR_NULL)
1166 continue;
1168 a = gfc_expr_attr (cons->expr);
1170 if (!a.pointer && !a.target)
1172 t = FAILURE;
1173 gfc_error ("The element in the structure constructor at %L, "
1174 "for pointer component '%s' should be a POINTER or "
1175 "a TARGET", &cons->expr->where, comp->name);
1178 if (init)
1180 /* F08:C461. Additional checks for pointer initialization. */
1181 if (a.allocatable)
1183 t = FAILURE;
1184 gfc_error ("Pointer initialization target at %L "
1185 "must not be ALLOCATABLE ", &cons->expr->where);
1187 if (!a.save)
1189 t = FAILURE;
1190 gfc_error ("Pointer initialization target at %L "
1191 "must have the SAVE attribute", &cons->expr->where);
1195 /* F2003, C1272 (3). */
1196 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
1197 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1198 || gfc_is_coindexed (cons->expr)))
1200 t = FAILURE;
1201 gfc_error ("Invalid expression in the structure constructor for "
1202 "pointer component '%s' at %L in PURE procedure",
1203 comp->name, &cons->expr->where);
1206 if (gfc_implicit_pure (NULL)
1207 && cons->expr->expr_type == EXPR_VARIABLE
1208 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1209 || gfc_is_coindexed (cons->expr)))
1210 gfc_current_ns->proc_name->attr.implicit_pure = 0;
1214 return t;
1218 /****************** Expression name resolution ******************/
1220 /* Returns 0 if a symbol was not declared with a type or
1221 attribute declaration statement, nonzero otherwise. */
1223 static int
1224 was_declared (gfc_symbol *sym)
1226 symbol_attribute a;
1228 a = sym->attr;
1230 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1231 return 1;
1233 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1234 || a.optional || a.pointer || a.save || a.target || a.volatile_
1235 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1236 || a.asynchronous || a.codimension)
1237 return 1;
1239 return 0;
1243 /* Determine if a symbol is generic or not. */
1245 static int
1246 generic_sym (gfc_symbol *sym)
1248 gfc_symbol *s;
1250 if (sym->attr.generic ||
1251 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1252 return 1;
1254 if (was_declared (sym) || sym->ns->parent == NULL)
1255 return 0;
1257 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1259 if (s != NULL)
1261 if (s == sym)
1262 return 0;
1263 else
1264 return generic_sym (s);
1267 return 0;
1271 /* Determine if a symbol is specific or not. */
1273 static int
1274 specific_sym (gfc_symbol *sym)
1276 gfc_symbol *s;
1278 if (sym->attr.if_source == IFSRC_IFBODY
1279 || sym->attr.proc == PROC_MODULE
1280 || sym->attr.proc == PROC_INTERNAL
1281 || sym->attr.proc == PROC_ST_FUNCTION
1282 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1283 || sym->attr.external)
1284 return 1;
1286 if (was_declared (sym) || sym->ns->parent == NULL)
1287 return 0;
1289 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1291 return (s == NULL) ? 0 : specific_sym (s);
1295 /* Figure out if the procedure is specific, generic or unknown. */
1297 typedef enum
1298 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1299 proc_type;
1301 static proc_type
1302 procedure_kind (gfc_symbol *sym)
1304 if (generic_sym (sym))
1305 return PTYPE_GENERIC;
1307 if (specific_sym (sym))
1308 return PTYPE_SPECIFIC;
1310 return PTYPE_UNKNOWN;
1313 /* Check references to assumed size arrays. The flag need_full_assumed_size
1314 is nonzero when matching actual arguments. */
1316 static int need_full_assumed_size = 0;
1318 static bool
1319 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1321 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1322 return false;
1324 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1325 What should it be? */
1326 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1327 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1328 && (e->ref->u.ar.type == AR_FULL))
1330 gfc_error ("The upper bound in the last dimension must "
1331 "appear in the reference to the assumed size "
1332 "array '%s' at %L", sym->name, &e->where);
1333 return true;
1335 return false;
1339 /* Look for bad assumed size array references in argument expressions
1340 of elemental and array valued intrinsic procedures. Since this is
1341 called from procedure resolution functions, it only recurses at
1342 operators. */
1344 static bool
1345 resolve_assumed_size_actual (gfc_expr *e)
1347 if (e == NULL)
1348 return false;
1350 switch (e->expr_type)
1352 case EXPR_VARIABLE:
1353 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1354 return true;
1355 break;
1357 case EXPR_OP:
1358 if (resolve_assumed_size_actual (e->value.op.op1)
1359 || resolve_assumed_size_actual (e->value.op.op2))
1360 return true;
1361 break;
1363 default:
1364 break;
1366 return false;
1370 /* Check a generic procedure, passed as an actual argument, to see if
1371 there is a matching specific name. If none, it is an error, and if
1372 more than one, the reference is ambiguous. */
1373 static int
1374 count_specific_procs (gfc_expr *e)
1376 int n;
1377 gfc_interface *p;
1378 gfc_symbol *sym;
1380 n = 0;
1381 sym = e->symtree->n.sym;
1383 for (p = sym->generic; p; p = p->next)
1384 if (strcmp (sym->name, p->sym->name) == 0)
1386 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1387 sym->name);
1388 n++;
1391 if (n > 1)
1392 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1393 &e->where);
1395 if (n == 0)
1396 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1397 "argument at %L", sym->name, &e->where);
1399 return n;
1403 /* See if a call to sym could possibly be a not allowed RECURSION because of
1404 a missing RECURIVE declaration. This means that either sym is the current
1405 context itself, or sym is the parent of a contained procedure calling its
1406 non-RECURSIVE containing procedure.
1407 This also works if sym is an ENTRY. */
1409 static bool
1410 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1412 gfc_symbol* proc_sym;
1413 gfc_symbol* context_proc;
1414 gfc_namespace* real_context;
1416 if (sym->attr.flavor == FL_PROGRAM)
1417 return false;
1419 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1421 /* If we've got an ENTRY, find real procedure. */
1422 if (sym->attr.entry && sym->ns->entries)
1423 proc_sym = sym->ns->entries->sym;
1424 else
1425 proc_sym = sym;
1427 /* If sym is RECURSIVE, all is well of course. */
1428 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1429 return false;
1431 /* Find the context procedure's "real" symbol if it has entries.
1432 We look for a procedure symbol, so recurse on the parents if we don't
1433 find one (like in case of a BLOCK construct). */
1434 for (real_context = context; ; real_context = real_context->parent)
1436 /* We should find something, eventually! */
1437 gcc_assert (real_context);
1439 context_proc = (real_context->entries ? real_context->entries->sym
1440 : real_context->proc_name);
1442 /* In some special cases, there may not be a proc_name, like for this
1443 invalid code:
1444 real(bad_kind()) function foo () ...
1445 when checking the call to bad_kind ().
1446 In these cases, we simply return here and assume that the
1447 call is ok. */
1448 if (!context_proc)
1449 return false;
1451 if (context_proc->attr.flavor != FL_LABEL)
1452 break;
1455 /* A call from sym's body to itself is recursion, of course. */
1456 if (context_proc == proc_sym)
1457 return true;
1459 /* The same is true if context is a contained procedure and sym the
1460 containing one. */
1461 if (context_proc->attr.contained)
1463 gfc_symbol* parent_proc;
1465 gcc_assert (context->parent);
1466 parent_proc = (context->parent->entries ? context->parent->entries->sym
1467 : context->parent->proc_name);
1469 if (parent_proc == proc_sym)
1470 return true;
1473 return false;
1477 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1478 its typespec and formal argument list. */
1480 static gfc_try
1481 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1483 gfc_intrinsic_sym* isym = NULL;
1484 const char* symstd;
1486 if (sym->formal)
1487 return SUCCESS;
1489 /* Already resolved. */
1490 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1491 return SUCCESS;
1493 /* We already know this one is an intrinsic, so we don't call
1494 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1495 gfc_find_subroutine directly to check whether it is a function or
1496 subroutine. */
1498 if (sym->intmod_sym_id)
1499 isym = gfc_intrinsic_function_by_id ((gfc_isym_id) sym->intmod_sym_id);
1500 else
1501 isym = gfc_find_function (sym->name);
1503 if (isym)
1505 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1506 && !sym->attr.implicit_type)
1507 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1508 " ignored", sym->name, &sym->declared_at);
1510 if (!sym->attr.function &&
1511 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1512 return FAILURE;
1514 sym->ts = isym->ts;
1516 else if ((isym = gfc_find_subroutine (sym->name)))
1518 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1520 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1521 " specifier", sym->name, &sym->declared_at);
1522 return FAILURE;
1525 if (!sym->attr.subroutine &&
1526 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1527 return FAILURE;
1529 else
1531 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1532 &sym->declared_at);
1533 return FAILURE;
1536 gfc_copy_formal_args_intr (sym, isym);
1538 /* Check it is actually available in the standard settings. */
1539 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1540 == FAILURE)
1542 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1543 " available in the current standard settings but %s. Use"
1544 " an appropriate -std=* option or enable -fall-intrinsics"
1545 " in order to use it.",
1546 sym->name, &sym->declared_at, symstd);
1547 return FAILURE;
1550 return SUCCESS;
1554 /* Resolve a procedure expression, like passing it to a called procedure or as
1555 RHS for a procedure pointer assignment. */
1557 static gfc_try
1558 resolve_procedure_expression (gfc_expr* expr)
1560 gfc_symbol* sym;
1562 if (expr->expr_type != EXPR_VARIABLE)
1563 return SUCCESS;
1564 gcc_assert (expr->symtree);
1566 sym = expr->symtree->n.sym;
1568 if (sym->attr.intrinsic)
1569 resolve_intrinsic (sym, &expr->where);
1571 if (sym->attr.flavor != FL_PROCEDURE
1572 || (sym->attr.function && sym->result == sym))
1573 return SUCCESS;
1575 /* A non-RECURSIVE procedure that is used as procedure expression within its
1576 own body is in danger of being called recursively. */
1577 if (is_illegal_recursion (sym, gfc_current_ns))
1578 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1579 " itself recursively. Declare it RECURSIVE or use"
1580 " -frecursive", sym->name, &expr->where);
1582 return SUCCESS;
1586 /* Resolve an actual argument list. Most of the time, this is just
1587 resolving the expressions in the list.
1588 The exception is that we sometimes have to decide whether arguments
1589 that look like procedure arguments are really simple variable
1590 references. */
1592 static gfc_try
1593 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1594 bool no_formal_args)
1596 gfc_symbol *sym;
1597 gfc_symtree *parent_st;
1598 gfc_expr *e;
1599 int save_need_full_assumed_size;
1601 for (; arg; arg = arg->next)
1603 e = arg->expr;
1604 if (e == NULL)
1606 /* Check the label is a valid branching target. */
1607 if (arg->label)
1609 if (arg->label->defined == ST_LABEL_UNKNOWN)
1611 gfc_error ("Label %d referenced at %L is never defined",
1612 arg->label->value, &arg->label->where);
1613 return FAILURE;
1616 continue;
1619 if (e->expr_type == EXPR_VARIABLE
1620 && e->symtree->n.sym->attr.generic
1621 && no_formal_args
1622 && count_specific_procs (e) != 1)
1623 return FAILURE;
1625 if (e->ts.type != BT_PROCEDURE)
1627 save_need_full_assumed_size = need_full_assumed_size;
1628 if (e->expr_type != EXPR_VARIABLE)
1629 need_full_assumed_size = 0;
1630 if (gfc_resolve_expr (e) != SUCCESS)
1631 return FAILURE;
1632 need_full_assumed_size = save_need_full_assumed_size;
1633 goto argument_list;
1636 /* See if the expression node should really be a variable reference. */
1638 sym = e->symtree->n.sym;
1640 if (sym->attr.flavor == FL_PROCEDURE
1641 || sym->attr.intrinsic
1642 || sym->attr.external)
1644 int actual_ok;
1646 /* If a procedure is not already determined to be something else
1647 check if it is intrinsic. */
1648 if (!sym->attr.intrinsic
1649 && !(sym->attr.external || sym->attr.use_assoc
1650 || sym->attr.if_source == IFSRC_IFBODY)
1651 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1652 sym->attr.intrinsic = 1;
1654 if (sym->attr.proc == PROC_ST_FUNCTION)
1656 gfc_error ("Statement function '%s' at %L is not allowed as an "
1657 "actual argument", sym->name, &e->where);
1660 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1661 sym->attr.subroutine);
1662 if (sym->attr.intrinsic && actual_ok == 0)
1664 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1665 "actual argument", sym->name, &e->where);
1668 if (sym->attr.contained && !sym->attr.use_assoc
1669 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1671 if (gfc_notify_std (GFC_STD_F2008,
1672 "Fortran 2008: Internal procedure '%s' is"
1673 " used as actual argument at %L",
1674 sym->name, &e->where) == FAILURE)
1675 return FAILURE;
1678 if (sym->attr.elemental && !sym->attr.intrinsic)
1680 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1681 "allowed as an actual argument at %L", sym->name,
1682 &e->where);
1685 /* Check if a generic interface has a specific procedure
1686 with the same name before emitting an error. */
1687 if (sym->attr.generic && count_specific_procs (e) != 1)
1688 return FAILURE;
1690 /* Just in case a specific was found for the expression. */
1691 sym = e->symtree->n.sym;
1693 /* If the symbol is the function that names the current (or
1694 parent) scope, then we really have a variable reference. */
1696 if (gfc_is_function_return_value (sym, sym->ns))
1697 goto got_variable;
1699 /* If all else fails, see if we have a specific intrinsic. */
1700 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1702 gfc_intrinsic_sym *isym;
1704 isym = gfc_find_function (sym->name);
1705 if (isym == NULL || !isym->specific)
1707 gfc_error ("Unable to find a specific INTRINSIC procedure "
1708 "for the reference '%s' at %L", sym->name,
1709 &e->where);
1710 return FAILURE;
1712 sym->ts = isym->ts;
1713 sym->attr.intrinsic = 1;
1714 sym->attr.function = 1;
1717 if (gfc_resolve_expr (e) == FAILURE)
1718 return FAILURE;
1719 goto argument_list;
1722 /* See if the name is a module procedure in a parent unit. */
1724 if (was_declared (sym) || sym->ns->parent == NULL)
1725 goto got_variable;
1727 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1729 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1730 return FAILURE;
1733 if (parent_st == NULL)
1734 goto got_variable;
1736 sym = parent_st->n.sym;
1737 e->symtree = parent_st; /* Point to the right thing. */
1739 if (sym->attr.flavor == FL_PROCEDURE
1740 || sym->attr.intrinsic
1741 || sym->attr.external)
1743 if (gfc_resolve_expr (e) == FAILURE)
1744 return FAILURE;
1745 goto argument_list;
1748 got_variable:
1749 e->expr_type = EXPR_VARIABLE;
1750 e->ts = sym->ts;
1751 if (sym->as != NULL)
1753 e->rank = sym->as->rank;
1754 e->ref = gfc_get_ref ();
1755 e->ref->type = REF_ARRAY;
1756 e->ref->u.ar.type = AR_FULL;
1757 e->ref->u.ar.as = sym->as;
1760 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1761 primary.c (match_actual_arg). If above code determines that it
1762 is a variable instead, it needs to be resolved as it was not
1763 done at the beginning of this function. */
1764 save_need_full_assumed_size = need_full_assumed_size;
1765 if (e->expr_type != EXPR_VARIABLE)
1766 need_full_assumed_size = 0;
1767 if (gfc_resolve_expr (e) != SUCCESS)
1768 return FAILURE;
1769 need_full_assumed_size = save_need_full_assumed_size;
1771 argument_list:
1772 /* Check argument list functions %VAL, %LOC and %REF. There is
1773 nothing to do for %REF. */
1774 if (arg->name && arg->name[0] == '%')
1776 if (strncmp ("%VAL", arg->name, 4) == 0)
1778 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1780 gfc_error ("By-value argument at %L is not of numeric "
1781 "type", &e->where);
1782 return FAILURE;
1785 if (e->rank)
1787 gfc_error ("By-value argument at %L cannot be an array or "
1788 "an array section", &e->where);
1789 return FAILURE;
1792 /* Intrinsics are still PROC_UNKNOWN here. However,
1793 since same file external procedures are not resolvable
1794 in gfortran, it is a good deal easier to leave them to
1795 intrinsic.c. */
1796 if (ptype != PROC_UNKNOWN
1797 && ptype != PROC_DUMMY
1798 && ptype != PROC_EXTERNAL
1799 && ptype != PROC_MODULE)
1801 gfc_error ("By-value argument at %L is not allowed "
1802 "in this context", &e->where);
1803 return FAILURE;
1807 /* Statement functions have already been excluded above. */
1808 else if (strncmp ("%LOC", arg->name, 4) == 0
1809 && e->ts.type == BT_PROCEDURE)
1811 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1813 gfc_error ("Passing internal procedure at %L by location "
1814 "not allowed", &e->where);
1815 return FAILURE;
1820 /* Fortran 2008, C1237. */
1821 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1822 && gfc_has_ultimate_pointer (e))
1824 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1825 "component", &e->where);
1826 return FAILURE;
1830 return SUCCESS;
1834 /* Do the checks of the actual argument list that are specific to elemental
1835 procedures. If called with c == NULL, we have a function, otherwise if
1836 expr == NULL, we have a subroutine. */
1838 static gfc_try
1839 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1841 gfc_actual_arglist *arg0;
1842 gfc_actual_arglist *arg;
1843 gfc_symbol *esym = NULL;
1844 gfc_intrinsic_sym *isym = NULL;
1845 gfc_expr *e = NULL;
1846 gfc_intrinsic_arg *iformal = NULL;
1847 gfc_formal_arglist *eformal = NULL;
1848 bool formal_optional = false;
1849 bool set_by_optional = false;
1850 int i;
1851 int rank = 0;
1853 /* Is this an elemental procedure? */
1854 if (expr && expr->value.function.actual != NULL)
1856 if (expr->value.function.esym != NULL
1857 && expr->value.function.esym->attr.elemental)
1859 arg0 = expr->value.function.actual;
1860 esym = expr->value.function.esym;
1862 else if (expr->value.function.isym != NULL
1863 && expr->value.function.isym->elemental)
1865 arg0 = expr->value.function.actual;
1866 isym = expr->value.function.isym;
1868 else
1869 return SUCCESS;
1871 else if (c && c->ext.actual != NULL)
1873 arg0 = c->ext.actual;
1875 if (c->resolved_sym)
1876 esym = c->resolved_sym;
1877 else
1878 esym = c->symtree->n.sym;
1879 gcc_assert (esym);
1881 if (!esym->attr.elemental)
1882 return SUCCESS;
1884 else
1885 return SUCCESS;
1887 /* The rank of an elemental is the rank of its array argument(s). */
1888 for (arg = arg0; arg; arg = arg->next)
1890 if (arg->expr != NULL && arg->expr->rank > 0)
1892 rank = arg->expr->rank;
1893 if (arg->expr->expr_type == EXPR_VARIABLE
1894 && arg->expr->symtree->n.sym->attr.optional)
1895 set_by_optional = true;
1897 /* Function specific; set the result rank and shape. */
1898 if (expr)
1900 expr->rank = rank;
1901 if (!expr->shape && arg->expr->shape)
1903 expr->shape = gfc_get_shape (rank);
1904 for (i = 0; i < rank; i++)
1905 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1908 break;
1912 /* If it is an array, it shall not be supplied as an actual argument
1913 to an elemental procedure unless an array of the same rank is supplied
1914 as an actual argument corresponding to a nonoptional dummy argument of
1915 that elemental procedure(12.4.1.5). */
1916 formal_optional = false;
1917 if (isym)
1918 iformal = isym->formal;
1919 else
1920 eformal = esym->formal;
1922 for (arg = arg0; arg; arg = arg->next)
1924 if (eformal)
1926 if (eformal->sym && eformal->sym->attr.optional)
1927 formal_optional = true;
1928 eformal = eformal->next;
1930 else if (isym && iformal)
1932 if (iformal->optional)
1933 formal_optional = true;
1934 iformal = iformal->next;
1936 else if (isym)
1937 formal_optional = true;
1939 if (pedantic && arg->expr != NULL
1940 && arg->expr->expr_type == EXPR_VARIABLE
1941 && arg->expr->symtree->n.sym->attr.optional
1942 && formal_optional
1943 && arg->expr->rank
1944 && (set_by_optional || arg->expr->rank != rank)
1945 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1947 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1948 "MISSING, it cannot be the actual argument of an "
1949 "ELEMENTAL procedure unless there is a non-optional "
1950 "argument with the same rank (12.4.1.5)",
1951 arg->expr->symtree->n.sym->name, &arg->expr->where);
1952 return FAILURE;
1956 for (arg = arg0; arg; arg = arg->next)
1958 if (arg->expr == NULL || arg->expr->rank == 0)
1959 continue;
1961 /* Being elemental, the last upper bound of an assumed size array
1962 argument must be present. */
1963 if (resolve_assumed_size_actual (arg->expr))
1964 return FAILURE;
1966 /* Elemental procedure's array actual arguments must conform. */
1967 if (e != NULL)
1969 if (gfc_check_conformance (arg->expr, e,
1970 "elemental procedure") == FAILURE)
1971 return FAILURE;
1973 else
1974 e = arg->expr;
1977 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1978 is an array, the intent inout/out variable needs to be also an array. */
1979 if (rank > 0 && esym && expr == NULL)
1980 for (eformal = esym->formal, arg = arg0; arg && eformal;
1981 arg = arg->next, eformal = eformal->next)
1982 if ((eformal->sym->attr.intent == INTENT_OUT
1983 || eformal->sym->attr.intent == INTENT_INOUT)
1984 && arg->expr && arg->expr->rank == 0)
1986 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1987 "ELEMENTAL subroutine '%s' is a scalar, but another "
1988 "actual argument is an array", &arg->expr->where,
1989 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1990 : "INOUT", eformal->sym->name, esym->name);
1991 return FAILURE;
1993 return SUCCESS;
1997 /* This function does the checking of references to global procedures
1998 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1999 77 and 95 standards. It checks for a gsymbol for the name, making
2000 one if it does not already exist. If it already exists, then the
2001 reference being resolved must correspond to the type of gsymbol.
2002 Otherwise, the new symbol is equipped with the attributes of the
2003 reference. The corresponding code that is called in creating
2004 global entities is parse.c.
2006 In addition, for all but -std=legacy, the gsymbols are used to
2007 check the interfaces of external procedures from the same file.
2008 The namespace of the gsymbol is resolved and then, once this is
2009 done the interface is checked. */
2012 static bool
2013 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2015 if (!gsym_ns->proc_name->attr.recursive)
2016 return true;
2018 if (sym->ns == gsym_ns)
2019 return false;
2021 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2022 return false;
2024 return true;
2027 static bool
2028 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2030 if (gsym_ns->entries)
2032 gfc_entry_list *entry = gsym_ns->entries;
2034 for (; entry; entry = entry->next)
2036 if (strcmp (sym->name, entry->sym->name) == 0)
2038 if (strcmp (gsym_ns->proc_name->name,
2039 sym->ns->proc_name->name) == 0)
2040 return false;
2042 if (sym->ns->parent
2043 && strcmp (gsym_ns->proc_name->name,
2044 sym->ns->parent->proc_name->name) == 0)
2045 return false;
2049 return true;
2052 static void
2053 resolve_global_procedure (gfc_symbol *sym, locus *where,
2054 gfc_actual_arglist **actual, int sub)
2056 gfc_gsymbol * gsym;
2057 gfc_namespace *ns;
2058 enum gfc_symbol_type type;
2060 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2062 gsym = gfc_get_gsymbol (sym->name);
2064 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2065 gfc_global_used (gsym, where);
2067 if (gfc_option.flag_whole_file
2068 && (sym->attr.if_source == IFSRC_UNKNOWN
2069 || sym->attr.if_source == IFSRC_IFBODY)
2070 && gsym->type != GSYM_UNKNOWN
2071 && gsym->ns
2072 && gsym->ns->resolved != -1
2073 && gsym->ns->proc_name
2074 && not_in_recursive (sym, gsym->ns)
2075 && not_entry_self_reference (sym, gsym->ns))
2077 gfc_symbol *def_sym;
2079 /* Resolve the gsymbol namespace if needed. */
2080 if (!gsym->ns->resolved)
2082 gfc_dt_list *old_dt_list;
2083 struct gfc_omp_saved_state old_omp_state;
2085 /* Stash away derived types so that the backend_decls do not
2086 get mixed up. */
2087 old_dt_list = gfc_derived_types;
2088 gfc_derived_types = NULL;
2089 /* And stash away openmp state. */
2090 gfc_omp_save_and_clear_state (&old_omp_state);
2092 gfc_resolve (gsym->ns);
2094 /* Store the new derived types with the global namespace. */
2095 if (gfc_derived_types)
2096 gsym->ns->derived_types = gfc_derived_types;
2098 /* Restore the derived types of this namespace. */
2099 gfc_derived_types = old_dt_list;
2100 /* And openmp state. */
2101 gfc_omp_restore_state (&old_omp_state);
2104 /* Make sure that translation for the gsymbol occurs before
2105 the procedure currently being resolved. */
2106 ns = gfc_global_ns_list;
2107 for (; ns && ns != gsym->ns; ns = ns->sibling)
2109 if (ns->sibling == gsym->ns)
2111 ns->sibling = gsym->ns->sibling;
2112 gsym->ns->sibling = gfc_global_ns_list;
2113 gfc_global_ns_list = gsym->ns;
2114 break;
2118 def_sym = gsym->ns->proc_name;
2119 if (def_sym->attr.entry_master)
2121 gfc_entry_list *entry;
2122 for (entry = gsym->ns->entries; entry; entry = entry->next)
2123 if (strcmp (entry->sym->name, sym->name) == 0)
2125 def_sym = entry->sym;
2126 break;
2130 /* Differences in constant character lengths. */
2131 if (sym->attr.function && sym->ts.type == BT_CHARACTER)
2133 long int l1 = 0, l2 = 0;
2134 gfc_charlen *cl1 = sym->ts.u.cl;
2135 gfc_charlen *cl2 = def_sym->ts.u.cl;
2137 if (cl1 != NULL
2138 && cl1->length != NULL
2139 && cl1->length->expr_type == EXPR_CONSTANT)
2140 l1 = mpz_get_si (cl1->length->value.integer);
2142 if (cl2 != NULL
2143 && cl2->length != NULL
2144 && cl2->length->expr_type == EXPR_CONSTANT)
2145 l2 = mpz_get_si (cl2->length->value.integer);
2147 if (l1 && l2 && l1 != l2)
2148 gfc_error ("Character length mismatch in return type of "
2149 "function '%s' at %L (%ld/%ld)", sym->name,
2150 &sym->declared_at, l1, l2);
2153 /* Type mismatch of function return type and expected type. */
2154 if (sym->attr.function
2155 && !gfc_compare_types (&sym->ts, &def_sym->ts))
2156 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2157 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2158 gfc_typename (&def_sym->ts));
2160 if (def_sym->formal && sym->attr.if_source != IFSRC_IFBODY)
2162 gfc_formal_arglist *arg = def_sym->formal;
2163 for ( ; arg; arg = arg->next)
2164 if (!arg->sym)
2165 continue;
2166 /* F2003, 12.3.1.1 (2a); F2008, 12.4.2.2 (2a) */
2167 else if (arg->sym->attr.allocatable
2168 || arg->sym->attr.asynchronous
2169 || arg->sym->attr.optional
2170 || arg->sym->attr.pointer
2171 || arg->sym->attr.target
2172 || arg->sym->attr.value
2173 || arg->sym->attr.volatile_)
2175 gfc_error ("Dummy argument '%s' of procedure '%s' at %L "
2176 "has an attribute that requires an explicit "
2177 "interface for this procedure", arg->sym->name,
2178 sym->name, &sym->declared_at);
2179 break;
2181 /* F2003, 12.3.1.1 (2b); F2008, 12.4.2.2 (2b) */
2182 else if (arg->sym && arg->sym->as
2183 && arg->sym->as->type == AS_ASSUMED_SHAPE)
2185 gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
2186 "argument '%s' must have an explicit interface",
2187 sym->name, &sym->declared_at, arg->sym->name);
2188 break;
2190 /* F2008, 12.4.2.2 (2c) */
2191 else if (arg->sym->attr.codimension)
2193 gfc_error ("Procedure '%s' at %L with coarray dummy argument "
2194 "'%s' must have an explicit interface",
2195 sym->name, &sym->declared_at, arg->sym->name);
2196 break;
2198 /* F2003, 12.3.1.1 (2c); F2008, 12.4.2.2 (2d) */
2199 else if (false) /* TODO: is a parametrized derived type */
2201 gfc_error ("Procedure '%s' at %L with parametrized derived "
2202 "type argument '%s' must have an explicit "
2203 "interface", sym->name, &sym->declared_at,
2204 arg->sym->name);
2205 break;
2207 /* F2003, 12.3.1.1 (2d); F2008, 12.4.2.2 (2e) */
2208 else if (arg->sym->ts.type == BT_CLASS)
2210 gfc_error ("Procedure '%s' at %L with polymorphic dummy "
2211 "argument '%s' must have an explicit interface",
2212 sym->name, &sym->declared_at, arg->sym->name);
2213 break;
2217 if (def_sym->attr.function)
2219 /* F2003, 12.3.1.1 (3a); F2008, 12.4.2.2 (3a) */
2220 if (def_sym->as && def_sym->as->rank
2221 && (!sym->as || sym->as->rank != def_sym->as->rank))
2222 gfc_error ("The reference to function '%s' at %L either needs an "
2223 "explicit INTERFACE or the rank is incorrect", sym->name,
2224 where);
2226 /* F2003, 12.3.1.1 (3b); F2008, 12.4.2.2 (3b) */
2227 if ((def_sym->result->attr.pointer
2228 || def_sym->result->attr.allocatable)
2229 && (sym->attr.if_source != IFSRC_IFBODY
2230 || def_sym->result->attr.pointer
2231 != sym->result->attr.pointer
2232 || def_sym->result->attr.allocatable
2233 != sym->result->attr.allocatable))
2234 gfc_error ("Function '%s' at %L with a POINTER or ALLOCATABLE "
2235 "result must have an explicit interface", sym->name,
2236 where);
2238 /* F2003, 12.3.1.1 (3c); F2008, 12.4.2.2 (3c) */
2239 if (sym->ts.type == BT_CHARACTER && sym->attr.if_source != IFSRC_IFBODY
2240 && def_sym->ts.type == BT_CHARACTER && def_sym->ts.u.cl->length != NULL)
2242 gfc_charlen *cl = sym->ts.u.cl;
2244 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
2245 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
2247 gfc_error ("Nonconstant character-length function '%s' at %L "
2248 "must have an explicit interface", sym->name,
2249 &sym->declared_at);
2254 /* F2003, 12.3.1.1 (4); F2008, 12.4.2.2 (4) */
2255 if (def_sym->attr.elemental && !sym->attr.elemental)
2257 gfc_error ("ELEMENTAL procedure '%s' at %L must have an explicit "
2258 "interface", sym->name, &sym->declared_at);
2261 /* F2003, 12.3.1.1 (5); F2008, 12.4.2.2 (5) */
2262 if (def_sym->attr.is_bind_c && !sym->attr.is_bind_c)
2264 gfc_error ("Procedure '%s' at %L with BIND(C) attribute must have "
2265 "an explicit interface", sym->name, &sym->declared_at);
2268 if (gfc_option.flag_whole_file == 1
2269 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2270 && !(gfc_option.warn_std & GFC_STD_GNU)))
2271 gfc_errors_to_warnings (1);
2273 if (sym->attr.if_source != IFSRC_IFBODY)
2274 gfc_procedure_use (def_sym, actual, where);
2276 gfc_errors_to_warnings (0);
2279 if (gsym->type == GSYM_UNKNOWN)
2281 gsym->type = type;
2282 gsym->where = *where;
2285 gsym->used = 1;
2289 /************* Function resolution *************/
2291 /* Resolve a function call known to be generic.
2292 Section 14.1.2.4.1. */
2294 static match
2295 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2297 gfc_symbol *s;
2299 if (sym->attr.generic)
2301 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2302 if (s != NULL)
2304 expr->value.function.name = s->name;
2305 expr->value.function.esym = s;
2307 if (s->ts.type != BT_UNKNOWN)
2308 expr->ts = s->ts;
2309 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2310 expr->ts = s->result->ts;
2312 if (s->as != NULL)
2313 expr->rank = s->as->rank;
2314 else if (s->result != NULL && s->result->as != NULL)
2315 expr->rank = s->result->as->rank;
2317 gfc_set_sym_referenced (expr->value.function.esym);
2319 return MATCH_YES;
2322 /* TODO: Need to search for elemental references in generic
2323 interface. */
2326 if (sym->attr.intrinsic)
2327 return gfc_intrinsic_func_interface (expr, 0);
2329 return MATCH_NO;
2333 static gfc_try
2334 resolve_generic_f (gfc_expr *expr)
2336 gfc_symbol *sym;
2337 match m;
2339 sym = expr->symtree->n.sym;
2341 for (;;)
2343 m = resolve_generic_f0 (expr, sym);
2344 if (m == MATCH_YES)
2345 return SUCCESS;
2346 else if (m == MATCH_ERROR)
2347 return FAILURE;
2349 generic:
2350 if (sym->ns->parent == NULL)
2351 break;
2352 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2354 if (sym == NULL)
2355 break;
2356 if (!generic_sym (sym))
2357 goto generic;
2360 /* Last ditch attempt. See if the reference is to an intrinsic
2361 that possesses a matching interface. 14.1.2.4 */
2362 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
2364 gfc_error ("There is no specific function for the generic '%s' at %L",
2365 expr->symtree->n.sym->name, &expr->where);
2366 return FAILURE;
2369 m = gfc_intrinsic_func_interface (expr, 0);
2370 if (m == MATCH_YES)
2371 return SUCCESS;
2372 if (m == MATCH_NO)
2373 gfc_error ("Generic function '%s' at %L is not consistent with a "
2374 "specific intrinsic interface", expr->symtree->n.sym->name,
2375 &expr->where);
2377 return FAILURE;
2381 /* Resolve a function call known to be specific. */
2383 static match
2384 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2386 match m;
2388 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2390 if (sym->attr.dummy)
2392 sym->attr.proc = PROC_DUMMY;
2393 goto found;
2396 sym->attr.proc = PROC_EXTERNAL;
2397 goto found;
2400 if (sym->attr.proc == PROC_MODULE
2401 || sym->attr.proc == PROC_ST_FUNCTION
2402 || sym->attr.proc == PROC_INTERNAL)
2403 goto found;
2405 if (sym->attr.intrinsic)
2407 m = gfc_intrinsic_func_interface (expr, 1);
2408 if (m == MATCH_YES)
2409 return MATCH_YES;
2410 if (m == MATCH_NO)
2411 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2412 "with an intrinsic", sym->name, &expr->where);
2414 return MATCH_ERROR;
2417 return MATCH_NO;
2419 found:
2420 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2422 if (sym->result)
2423 expr->ts = sym->result->ts;
2424 else
2425 expr->ts = sym->ts;
2426 expr->value.function.name = sym->name;
2427 expr->value.function.esym = sym;
2428 if (sym->as != NULL)
2429 expr->rank = sym->as->rank;
2431 return MATCH_YES;
2435 static gfc_try
2436 resolve_specific_f (gfc_expr *expr)
2438 gfc_symbol *sym;
2439 match m;
2441 sym = expr->symtree->n.sym;
2443 for (;;)
2445 m = resolve_specific_f0 (sym, expr);
2446 if (m == MATCH_YES)
2447 return SUCCESS;
2448 if (m == MATCH_ERROR)
2449 return FAILURE;
2451 if (sym->ns->parent == NULL)
2452 break;
2454 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2456 if (sym == NULL)
2457 break;
2460 gfc_error ("Unable to resolve the specific function '%s' at %L",
2461 expr->symtree->n.sym->name, &expr->where);
2463 return SUCCESS;
2467 /* Resolve a procedure call not known to be generic nor specific. */
2469 static gfc_try
2470 resolve_unknown_f (gfc_expr *expr)
2472 gfc_symbol *sym;
2473 gfc_typespec *ts;
2475 sym = expr->symtree->n.sym;
2477 if (sym->attr.dummy)
2479 sym->attr.proc = PROC_DUMMY;
2480 expr->value.function.name = sym->name;
2481 goto set_type;
2484 /* See if we have an intrinsic function reference. */
2486 if (gfc_is_intrinsic (sym, 0, expr->where))
2488 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2489 return SUCCESS;
2490 return FAILURE;
2493 /* The reference is to an external name. */
2495 sym->attr.proc = PROC_EXTERNAL;
2496 expr->value.function.name = sym->name;
2497 expr->value.function.esym = expr->symtree->n.sym;
2499 if (sym->as != NULL)
2500 expr->rank = sym->as->rank;
2502 /* Type of the expression is either the type of the symbol or the
2503 default type of the symbol. */
2505 set_type:
2506 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2508 if (sym->ts.type != BT_UNKNOWN)
2509 expr->ts = sym->ts;
2510 else
2512 ts = gfc_get_default_type (sym->name, sym->ns);
2514 if (ts->type == BT_UNKNOWN)
2516 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2517 sym->name, &expr->where);
2518 return FAILURE;
2520 else
2521 expr->ts = *ts;
2524 return SUCCESS;
2528 /* Return true, if the symbol is an external procedure. */
2529 static bool
2530 is_external_proc (gfc_symbol *sym)
2532 if (!sym->attr.dummy && !sym->attr.contained
2533 && !(sym->attr.intrinsic
2534 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2535 && sym->attr.proc != PROC_ST_FUNCTION
2536 && !sym->attr.proc_pointer
2537 && !sym->attr.use_assoc
2538 && sym->name)
2539 return true;
2541 return false;
2545 /* Figure out if a function reference is pure or not. Also set the name
2546 of the function for a potential error message. Return nonzero if the
2547 function is PURE, zero if not. */
2548 static int
2549 pure_stmt_function (gfc_expr *, gfc_symbol *);
2551 static int
2552 pure_function (gfc_expr *e, const char **name)
2554 int pure;
2556 *name = NULL;
2558 if (e->symtree != NULL
2559 && e->symtree->n.sym != NULL
2560 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2561 return pure_stmt_function (e, e->symtree->n.sym);
2563 if (e->value.function.esym)
2565 pure = gfc_pure (e->value.function.esym);
2566 *name = e->value.function.esym->name;
2568 else if (e->value.function.isym)
2570 pure = e->value.function.isym->pure
2571 || e->value.function.isym->elemental;
2572 *name = e->value.function.isym->name;
2574 else
2576 /* Implicit functions are not pure. */
2577 pure = 0;
2578 *name = e->value.function.name;
2581 return pure;
2585 static bool
2586 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2587 int *f ATTRIBUTE_UNUSED)
2589 const char *name;
2591 /* Don't bother recursing into other statement functions
2592 since they will be checked individually for purity. */
2593 if (e->expr_type != EXPR_FUNCTION
2594 || !e->symtree
2595 || e->symtree->n.sym == sym
2596 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2597 return false;
2599 return pure_function (e, &name) ? false : true;
2603 static int
2604 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2606 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2610 static gfc_try
2611 is_scalar_expr_ptr (gfc_expr *expr)
2613 gfc_try retval = SUCCESS;
2614 gfc_ref *ref;
2615 int start;
2616 int end;
2618 /* See if we have a gfc_ref, which means we have a substring, array
2619 reference, or a component. */
2620 if (expr->ref != NULL)
2622 ref = expr->ref;
2623 while (ref->next != NULL)
2624 ref = ref->next;
2626 switch (ref->type)
2628 case REF_SUBSTRING:
2629 if (ref->u.ss.start == NULL || ref->u.ss.end == NULL
2630 || gfc_dep_compare_expr (ref->u.ss.start, ref->u.ss.end) != 0)
2631 retval = FAILURE;
2632 break;
2634 case REF_ARRAY:
2635 if (ref->u.ar.type == AR_ELEMENT)
2636 retval = SUCCESS;
2637 else if (ref->u.ar.type == AR_FULL)
2639 /* The user can give a full array if the array is of size 1. */
2640 if (ref->u.ar.as != NULL
2641 && ref->u.ar.as->rank == 1
2642 && ref->u.ar.as->type == AS_EXPLICIT
2643 && ref->u.ar.as->lower[0] != NULL
2644 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2645 && ref->u.ar.as->upper[0] != NULL
2646 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2648 /* If we have a character string, we need to check if
2649 its length is one. */
2650 if (expr->ts.type == BT_CHARACTER)
2652 if (expr->ts.u.cl == NULL
2653 || expr->ts.u.cl->length == NULL
2654 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2655 != 0)
2656 retval = FAILURE;
2658 else
2660 /* We have constant lower and upper bounds. If the
2661 difference between is 1, it can be considered a
2662 scalar.
2663 FIXME: Use gfc_dep_compare_expr instead. */
2664 start = (int) mpz_get_si
2665 (ref->u.ar.as->lower[0]->value.integer);
2666 end = (int) mpz_get_si
2667 (ref->u.ar.as->upper[0]->value.integer);
2668 if (end - start + 1 != 1)
2669 retval = FAILURE;
2672 else
2673 retval = FAILURE;
2675 else
2676 retval = FAILURE;
2677 break;
2678 default:
2679 retval = SUCCESS;
2680 break;
2683 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2685 /* Character string. Make sure it's of length 1. */
2686 if (expr->ts.u.cl == NULL
2687 || expr->ts.u.cl->length == NULL
2688 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2689 retval = FAILURE;
2691 else if (expr->rank != 0)
2692 retval = FAILURE;
2694 return retval;
2698 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2699 and, in the case of c_associated, set the binding label based on
2700 the arguments. */
2702 static gfc_try
2703 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2704 gfc_symbol **new_sym)
2706 char name[GFC_MAX_SYMBOL_LEN + 1];
2707 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2708 int optional_arg = 0;
2709 gfc_try retval = SUCCESS;
2710 gfc_symbol *args_sym;
2711 gfc_typespec *arg_ts;
2712 symbol_attribute arg_attr;
2714 if (args->expr->expr_type == EXPR_CONSTANT
2715 || args->expr->expr_type == EXPR_OP
2716 || args->expr->expr_type == EXPR_NULL)
2718 gfc_error ("Argument to '%s' at %L is not a variable",
2719 sym->name, &(args->expr->where));
2720 return FAILURE;
2723 args_sym = args->expr->symtree->n.sym;
2725 /* The typespec for the actual arg should be that stored in the expr
2726 and not necessarily that of the expr symbol (args_sym), because
2727 the actual expression could be a part-ref of the expr symbol. */
2728 arg_ts = &(args->expr->ts);
2729 arg_attr = gfc_expr_attr (args->expr);
2731 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2733 /* If the user gave two args then they are providing something for
2734 the optional arg (the second cptr). Therefore, set the name and
2735 binding label to the c_associated for two cptrs. Otherwise,
2736 set c_associated to expect one cptr. */
2737 if (args->next)
2739 /* two args. */
2740 sprintf (name, "%s_2", sym->name);
2741 sprintf (binding_label, "%s_2", sym->binding_label);
2742 optional_arg = 1;
2744 else
2746 /* one arg. */
2747 sprintf (name, "%s_1", sym->name);
2748 sprintf (binding_label, "%s_1", sym->binding_label);
2749 optional_arg = 0;
2752 /* Get a new symbol for the version of c_associated that
2753 will get called. */
2754 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2756 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2757 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2759 sprintf (name, "%s", sym->name);
2760 sprintf (binding_label, "%s", sym->binding_label);
2762 /* Error check the call. */
2763 if (args->next != NULL)
2765 gfc_error_now ("More actual than formal arguments in '%s' "
2766 "call at %L", name, &(args->expr->where));
2767 retval = FAILURE;
2769 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2771 gfc_ref *ref;
2772 bool seen_section;
2774 /* Make sure we have either the target or pointer attribute. */
2775 if (!arg_attr.target && !arg_attr.pointer)
2777 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2778 "a TARGET or an associated pointer",
2779 args_sym->name,
2780 sym->name, &(args->expr->where));
2781 retval = FAILURE;
2784 if (gfc_is_coindexed (args->expr))
2786 gfc_error_now ("Coindexed argument not permitted"
2787 " in '%s' call at %L", name,
2788 &(args->expr->where));
2789 retval = FAILURE;
2792 /* Follow references to make sure there are no array
2793 sections. */
2794 seen_section = false;
2796 for (ref=args->expr->ref; ref; ref = ref->next)
2798 if (ref->type == REF_ARRAY)
2800 if (ref->u.ar.type == AR_SECTION)
2801 seen_section = true;
2803 if (ref->u.ar.type != AR_ELEMENT)
2805 gfc_ref *r;
2806 for (r = ref->next; r; r=r->next)
2807 if (r->type == REF_COMPONENT)
2809 gfc_error_now ("Array section not permitted"
2810 " in '%s' call at %L", name,
2811 &(args->expr->where));
2812 retval = FAILURE;
2813 break;
2819 if (seen_section && retval == SUCCESS)
2820 gfc_warning ("Array section in '%s' call at %L", name,
2821 &(args->expr->where));
2823 /* See if we have interoperable type and type param. */
2824 if (verify_c_interop (arg_ts) == SUCCESS
2825 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2827 if (args_sym->attr.target == 1)
2829 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2830 has the target attribute and is interoperable. */
2831 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2832 allocatable variable that has the TARGET attribute and
2833 is not an array of zero size. */
2834 if (args_sym->attr.allocatable == 1)
2836 if (args_sym->attr.dimension != 0
2837 && (args_sym->as && args_sym->as->rank == 0))
2839 gfc_error_now ("Allocatable variable '%s' used as a "
2840 "parameter to '%s' at %L must not be "
2841 "an array of zero size",
2842 args_sym->name, sym->name,
2843 &(args->expr->where));
2844 retval = FAILURE;
2847 else
2849 /* A non-allocatable target variable with C
2850 interoperable type and type parameters must be
2851 interoperable. */
2852 if (args_sym && args_sym->attr.dimension)
2854 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2856 gfc_error ("Assumed-shape array '%s' at %L "
2857 "cannot be an argument to the "
2858 "procedure '%s' because "
2859 "it is not C interoperable",
2860 args_sym->name,
2861 &(args->expr->where), sym->name);
2862 retval = FAILURE;
2864 else if (args_sym->as->type == AS_DEFERRED)
2866 gfc_error ("Deferred-shape array '%s' at %L "
2867 "cannot be an argument to the "
2868 "procedure '%s' because "
2869 "it is not C interoperable",
2870 args_sym->name,
2871 &(args->expr->where), sym->name);
2872 retval = FAILURE;
2876 /* Make sure it's not a character string. Arrays of
2877 any type should be ok if the variable is of a C
2878 interoperable type. */
2879 if (arg_ts->type == BT_CHARACTER)
2880 if (arg_ts->u.cl != NULL
2881 && (arg_ts->u.cl->length == NULL
2882 || arg_ts->u.cl->length->expr_type
2883 != EXPR_CONSTANT
2884 || mpz_cmp_si
2885 (arg_ts->u.cl->length->value.integer, 1)
2886 != 0)
2887 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2889 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2890 "at %L must have a length of 1",
2891 args_sym->name, sym->name,
2892 &(args->expr->where));
2893 retval = FAILURE;
2897 else if (arg_attr.pointer
2898 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2900 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2901 scalar pointer. */
2902 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2903 "associated scalar POINTER", args_sym->name,
2904 sym->name, &(args->expr->where));
2905 retval = FAILURE;
2908 else
2910 /* The parameter is not required to be C interoperable. If it
2911 is not C interoperable, it must be a nonpolymorphic scalar
2912 with no length type parameters. It still must have either
2913 the pointer or target attribute, and it can be
2914 allocatable (but must be allocated when c_loc is called). */
2915 if (args->expr->rank != 0
2916 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2918 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2919 "scalar", args_sym->name, sym->name,
2920 &(args->expr->where));
2921 retval = FAILURE;
2923 else if (arg_ts->type == BT_CHARACTER
2924 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2926 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2927 "%L must have a length of 1",
2928 args_sym->name, sym->name,
2929 &(args->expr->where));
2930 retval = FAILURE;
2932 else if (arg_ts->type == BT_CLASS)
2934 gfc_error_now ("Parameter '%s' to '%s' at %L must not be "
2935 "polymorphic", args_sym->name, sym->name,
2936 &(args->expr->where));
2937 retval = FAILURE;
2941 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2943 if (args_sym->attr.flavor != FL_PROCEDURE)
2945 /* TODO: Update this error message to allow for procedure
2946 pointers once they are implemented. */
2947 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2948 "procedure",
2949 args_sym->name, sym->name,
2950 &(args->expr->where));
2951 retval = FAILURE;
2953 else if (args_sym->attr.is_bind_c != 1)
2955 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2956 "BIND(C)",
2957 args_sym->name, sym->name,
2958 &(args->expr->where));
2959 retval = FAILURE;
2963 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2964 *new_sym = sym;
2966 else
2968 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2969 "iso_c_binding function: '%s'!\n", sym->name);
2972 return retval;
2976 /* Resolve a function call, which means resolving the arguments, then figuring
2977 out which entity the name refers to. */
2979 static gfc_try
2980 resolve_function (gfc_expr *expr)
2982 gfc_actual_arglist *arg;
2983 gfc_symbol *sym;
2984 const char *name;
2985 gfc_try t;
2986 int temp;
2987 procedure_type p = PROC_INTRINSIC;
2988 bool no_formal_args;
2990 sym = NULL;
2991 if (expr->symtree)
2992 sym = expr->symtree->n.sym;
2994 /* If this is a procedure pointer component, it has already been resolved. */
2995 if (gfc_is_proc_ptr_comp (expr, NULL))
2996 return SUCCESS;
2998 if (sym && sym->attr.intrinsic
2999 && resolve_intrinsic (sym, &expr->where) == FAILURE)
3000 return FAILURE;
3002 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3004 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
3005 return FAILURE;
3008 /* If this ia a deferred TBP with an abstract interface (which may
3009 of course be referenced), expr->value.function.esym will be set. */
3010 if (sym && sym->attr.abstract && !expr->value.function.esym)
3012 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3013 sym->name, &expr->where);
3014 return FAILURE;
3017 /* Switch off assumed size checking and do this again for certain kinds
3018 of procedure, once the procedure itself is resolved. */
3019 need_full_assumed_size++;
3021 if (expr->symtree && expr->symtree->n.sym)
3022 p = expr->symtree->n.sym->attr.proc;
3024 if (expr->value.function.isym && expr->value.function.isym->inquiry)
3025 inquiry_argument = true;
3026 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
3028 if (resolve_actual_arglist (expr->value.function.actual,
3029 p, no_formal_args) == FAILURE)
3031 inquiry_argument = false;
3032 return FAILURE;
3035 inquiry_argument = false;
3037 /* Need to setup the call to the correct c_associated, depending on
3038 the number of cptrs to user gives to compare. */
3039 if (sym && sym->attr.is_iso_c == 1)
3041 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
3042 == FAILURE)
3043 return FAILURE;
3045 /* Get the symtree for the new symbol (resolved func).
3046 the old one will be freed later, when it's no longer used. */
3047 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
3050 /* Resume assumed_size checking. */
3051 need_full_assumed_size--;
3053 /* If the procedure is external, check for usage. */
3054 if (sym && is_external_proc (sym))
3055 resolve_global_procedure (sym, &expr->where,
3056 &expr->value.function.actual, 0);
3058 if (sym && sym->ts.type == BT_CHARACTER
3059 && sym->ts.u.cl
3060 && sym->ts.u.cl->length == NULL
3061 && !sym->attr.dummy
3062 && !sym->ts.deferred
3063 && expr->value.function.esym == NULL
3064 && !sym->attr.contained)
3066 /* Internal procedures are taken care of in resolve_contained_fntype. */
3067 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
3068 "be used at %L since it is not a dummy argument",
3069 sym->name, &expr->where);
3070 return FAILURE;
3073 /* See if function is already resolved. */
3075 if (expr->value.function.name != NULL)
3077 if (expr->ts.type == BT_UNKNOWN)
3078 expr->ts = sym->ts;
3079 t = SUCCESS;
3081 else
3083 /* Apply the rules of section 14.1.2. */
3085 switch (procedure_kind (sym))
3087 case PTYPE_GENERIC:
3088 t = resolve_generic_f (expr);
3089 break;
3091 case PTYPE_SPECIFIC:
3092 t = resolve_specific_f (expr);
3093 break;
3095 case PTYPE_UNKNOWN:
3096 t = resolve_unknown_f (expr);
3097 break;
3099 default:
3100 gfc_internal_error ("resolve_function(): bad function type");
3104 /* If the expression is still a function (it might have simplified),
3105 then we check to see if we are calling an elemental function. */
3107 if (expr->expr_type != EXPR_FUNCTION)
3108 return t;
3110 temp = need_full_assumed_size;
3111 need_full_assumed_size = 0;
3113 if (resolve_elemental_actual (expr, NULL) == FAILURE)
3114 return FAILURE;
3116 if (omp_workshare_flag
3117 && expr->value.function.esym
3118 && ! gfc_elemental (expr->value.function.esym))
3120 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
3121 "in WORKSHARE construct", expr->value.function.esym->name,
3122 &expr->where);
3123 t = FAILURE;
3126 #define GENERIC_ID expr->value.function.isym->id
3127 else if (expr->value.function.actual != NULL
3128 && expr->value.function.isym != NULL
3129 && GENERIC_ID != GFC_ISYM_LBOUND
3130 && GENERIC_ID != GFC_ISYM_LEN
3131 && GENERIC_ID != GFC_ISYM_LOC
3132 && GENERIC_ID != GFC_ISYM_PRESENT)
3134 /* Array intrinsics must also have the last upper bound of an
3135 assumed size array argument. UBOUND and SIZE have to be
3136 excluded from the check if the second argument is anything
3137 than a constant. */
3139 for (arg = expr->value.function.actual; arg; arg = arg->next)
3141 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3142 && arg->next != NULL && arg->next->expr)
3144 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3145 break;
3147 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
3148 break;
3150 if ((int)mpz_get_si (arg->next->expr->value.integer)
3151 < arg->expr->rank)
3152 break;
3155 if (arg->expr != NULL
3156 && arg->expr->rank > 0
3157 && resolve_assumed_size_actual (arg->expr))
3158 return FAILURE;
3161 #undef GENERIC_ID
3163 need_full_assumed_size = temp;
3164 name = NULL;
3166 if (!pure_function (expr, &name) && name)
3168 if (forall_flag)
3170 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
3171 "FORALL %s", name, &expr->where,
3172 forall_flag == 2 ? "mask" : "block");
3173 t = FAILURE;
3175 else if (do_concurrent_flag)
3177 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
3178 "DO CONCURRENT %s", name, &expr->where,
3179 do_concurrent_flag == 2 ? "mask" : "block");
3180 t = FAILURE;
3182 else if (gfc_pure (NULL))
3184 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3185 "procedure within a PURE procedure", name, &expr->where);
3186 t = FAILURE;
3190 if (!pure_function (expr, &name) && name && gfc_implicit_pure (NULL))
3191 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3193 /* Functions without the RECURSIVE attribution are not allowed to
3194 * call themselves. */
3195 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3197 gfc_symbol *esym;
3198 esym = expr->value.function.esym;
3200 if (is_illegal_recursion (esym, gfc_current_ns))
3202 if (esym->attr.entry && esym->ns->entries)
3203 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3204 " function '%s' is not RECURSIVE",
3205 esym->name, &expr->where, esym->ns->entries->sym->name);
3206 else
3207 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3208 " is not RECURSIVE", esym->name, &expr->where);
3210 t = FAILURE;
3214 /* Character lengths of use associated functions may contains references to
3215 symbols not referenced from the current program unit otherwise. Make sure
3216 those symbols are marked as referenced. */
3218 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3219 && expr->value.function.esym->attr.use_assoc)
3221 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3224 /* Make sure that the expression has a typespec that works. */
3225 if (expr->ts.type == BT_UNKNOWN)
3227 if (expr->symtree->n.sym->result
3228 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3229 && !expr->symtree->n.sym->result->attr.proc_pointer)
3230 expr->ts = expr->symtree->n.sym->result->ts;
3233 return t;
3237 /************* Subroutine resolution *************/
3239 static void
3240 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3242 if (gfc_pure (sym))
3243 return;
3245 if (forall_flag)
3246 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3247 sym->name, &c->loc);
3248 else if (do_concurrent_flag)
3249 gfc_error ("Subroutine call to '%s' in DO CONCURRENT block at %L is not "
3250 "PURE", sym->name, &c->loc);
3251 else if (gfc_pure (NULL))
3252 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3253 &c->loc);
3257 static match
3258 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3260 gfc_symbol *s;
3262 if (sym->attr.generic)
3264 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3265 if (s != NULL)
3267 c->resolved_sym = s;
3268 pure_subroutine (c, s);
3269 return MATCH_YES;
3272 /* TODO: Need to search for elemental references in generic interface. */
3275 if (sym->attr.intrinsic)
3276 return gfc_intrinsic_sub_interface (c, 0);
3278 return MATCH_NO;
3282 static gfc_try
3283 resolve_generic_s (gfc_code *c)
3285 gfc_symbol *sym;
3286 match m;
3288 sym = c->symtree->n.sym;
3290 for (;;)
3292 m = resolve_generic_s0 (c, sym);
3293 if (m == MATCH_YES)
3294 return SUCCESS;
3295 else if (m == MATCH_ERROR)
3296 return FAILURE;
3298 generic:
3299 if (sym->ns->parent == NULL)
3300 break;
3301 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3303 if (sym == NULL)
3304 break;
3305 if (!generic_sym (sym))
3306 goto generic;
3309 /* Last ditch attempt. See if the reference is to an intrinsic
3310 that possesses a matching interface. 14.1.2.4 */
3311 sym = c->symtree->n.sym;
3313 if (!gfc_is_intrinsic (sym, 1, c->loc))
3315 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3316 sym->name, &c->loc);
3317 return FAILURE;
3320 m = gfc_intrinsic_sub_interface (c, 0);
3321 if (m == MATCH_YES)
3322 return SUCCESS;
3323 if (m == MATCH_NO)
3324 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3325 "intrinsic subroutine interface", sym->name, &c->loc);
3327 return FAILURE;
3331 /* Set the name and binding label of the subroutine symbol in the call
3332 expression represented by 'c' to include the type and kind of the
3333 second parameter. This function is for resolving the appropriate
3334 version of c_f_pointer() and c_f_procpointer(). For example, a
3335 call to c_f_pointer() for a default integer pointer could have a
3336 name of c_f_pointer_i4. If no second arg exists, which is an error
3337 for these two functions, it defaults to the generic symbol's name
3338 and binding label. */
3340 static void
3341 set_name_and_label (gfc_code *c, gfc_symbol *sym,
3342 char *name, char *binding_label)
3344 gfc_expr *arg = NULL;
3345 char type;
3346 int kind;
3348 /* The second arg of c_f_pointer and c_f_procpointer determines
3349 the type and kind for the procedure name. */
3350 arg = c->ext.actual->next->expr;
3352 if (arg != NULL)
3354 /* Set up the name to have the given symbol's name,
3355 plus the type and kind. */
3356 /* a derived type is marked with the type letter 'u' */
3357 if (arg->ts.type == BT_DERIVED)
3359 type = 'd';
3360 kind = 0; /* set the kind as 0 for now */
3362 else
3364 type = gfc_type_letter (arg->ts.type);
3365 kind = arg->ts.kind;
3368 if (arg->ts.type == BT_CHARACTER)
3369 /* Kind info for character strings not needed. */
3370 kind = 0;
3372 sprintf (name, "%s_%c%d", sym->name, type, kind);
3373 /* Set up the binding label as the given symbol's label plus
3374 the type and kind. */
3375 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
3377 else
3379 /* If the second arg is missing, set the name and label as
3380 was, cause it should at least be found, and the missing
3381 arg error will be caught by compare_parameters(). */
3382 sprintf (name, "%s", sym->name);
3383 sprintf (binding_label, "%s", sym->binding_label);
3386 return;
3390 /* Resolve a generic version of the iso_c_binding procedure given
3391 (sym) to the specific one based on the type and kind of the
3392 argument(s). Currently, this function resolves c_f_pointer() and
3393 c_f_procpointer based on the type and kind of the second argument
3394 (FPTR). Other iso_c_binding procedures aren't specially handled.
3395 Upon successfully exiting, c->resolved_sym will hold the resolved
3396 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
3397 otherwise. */
3399 match
3400 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
3402 gfc_symbol *new_sym;
3403 /* this is fine, since we know the names won't use the max */
3404 char name[GFC_MAX_SYMBOL_LEN + 1];
3405 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
3406 /* default to success; will override if find error */
3407 match m = MATCH_YES;
3409 /* Make sure the actual arguments are in the necessary order (based on the
3410 formal args) before resolving. */
3411 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
3413 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3414 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3416 set_name_and_label (c, sym, name, binding_label);
3418 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3420 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
3422 /* Make sure we got a third arg if the second arg has non-zero
3423 rank. We must also check that the type and rank are
3424 correct since we short-circuit this check in
3425 gfc_procedure_use() (called above to sort actual args). */
3426 if (c->ext.actual->next->expr->rank != 0)
3428 if(c->ext.actual->next->next == NULL
3429 || c->ext.actual->next->next->expr == NULL)
3431 m = MATCH_ERROR;
3432 gfc_error ("Missing SHAPE parameter for call to %s "
3433 "at %L", sym->name, &(c->loc));
3435 else if (c->ext.actual->next->next->expr->ts.type
3436 != BT_INTEGER
3437 || c->ext.actual->next->next->expr->rank != 1)
3439 m = MATCH_ERROR;
3440 gfc_error ("SHAPE parameter for call to %s at %L must "
3441 "be a rank 1 INTEGER array", sym->name,
3442 &(c->loc));
3448 if (m != MATCH_ERROR)
3450 /* the 1 means to add the optional arg to formal list */
3451 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
3453 /* for error reporting, say it's declared where the original was */
3454 new_sym->declared_at = sym->declared_at;
3457 else
3459 /* no differences for c_loc or c_funloc */
3460 new_sym = sym;
3463 /* set the resolved symbol */
3464 if (m != MATCH_ERROR)
3465 c->resolved_sym = new_sym;
3466 else
3467 c->resolved_sym = sym;
3469 return m;
3473 /* Resolve a subroutine call known to be specific. */
3475 static match
3476 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3478 match m;
3480 if(sym->attr.is_iso_c)
3482 m = gfc_iso_c_sub_interface (c,sym);
3483 return m;
3486 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3488 if (sym->attr.dummy)
3490 sym->attr.proc = PROC_DUMMY;
3491 goto found;
3494 sym->attr.proc = PROC_EXTERNAL;
3495 goto found;
3498 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3499 goto found;
3501 if (sym->attr.intrinsic)
3503 m = gfc_intrinsic_sub_interface (c, 1);
3504 if (m == MATCH_YES)
3505 return MATCH_YES;
3506 if (m == MATCH_NO)
3507 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3508 "with an intrinsic", sym->name, &c->loc);
3510 return MATCH_ERROR;
3513 return MATCH_NO;
3515 found:
3516 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3518 c->resolved_sym = sym;
3519 pure_subroutine (c, sym);
3521 return MATCH_YES;
3525 static gfc_try
3526 resolve_specific_s (gfc_code *c)
3528 gfc_symbol *sym;
3529 match m;
3531 sym = c->symtree->n.sym;
3533 for (;;)
3535 m = resolve_specific_s0 (c, sym);
3536 if (m == MATCH_YES)
3537 return SUCCESS;
3538 if (m == MATCH_ERROR)
3539 return FAILURE;
3541 if (sym->ns->parent == NULL)
3542 break;
3544 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3546 if (sym == NULL)
3547 break;
3550 sym = c->symtree->n.sym;
3551 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3552 sym->name, &c->loc);
3554 return FAILURE;
3558 /* Resolve a subroutine call not known to be generic nor specific. */
3560 static gfc_try
3561 resolve_unknown_s (gfc_code *c)
3563 gfc_symbol *sym;
3565 sym = c->symtree->n.sym;
3567 if (sym->attr.dummy)
3569 sym->attr.proc = PROC_DUMMY;
3570 goto found;
3573 /* See if we have an intrinsic function reference. */
3575 if (gfc_is_intrinsic (sym, 1, c->loc))
3577 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3578 return SUCCESS;
3579 return FAILURE;
3582 /* The reference is to an external name. */
3584 found:
3585 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3587 c->resolved_sym = sym;
3589 pure_subroutine (c, sym);
3591 return SUCCESS;
3595 /* Resolve a subroutine call. Although it was tempting to use the same code
3596 for functions, subroutines and functions are stored differently and this
3597 makes things awkward. */
3599 static gfc_try
3600 resolve_call (gfc_code *c)
3602 gfc_try t;
3603 procedure_type ptype = PROC_INTRINSIC;
3604 gfc_symbol *csym, *sym;
3605 bool no_formal_args;
3607 csym = c->symtree ? c->symtree->n.sym : NULL;
3609 if (csym && csym->ts.type != BT_UNKNOWN)
3611 gfc_error ("'%s' at %L has a type, which is not consistent with "
3612 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3613 return FAILURE;
3616 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3618 gfc_symtree *st;
3619 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3620 sym = st ? st->n.sym : NULL;
3621 if (sym && csym != sym
3622 && sym->ns == gfc_current_ns
3623 && sym->attr.flavor == FL_PROCEDURE
3624 && sym->attr.contained)
3626 sym->refs++;
3627 if (csym->attr.generic)
3628 c->symtree->n.sym = sym;
3629 else
3630 c->symtree = st;
3631 csym = c->symtree->n.sym;
3635 /* If this ia a deferred TBP with an abstract interface
3636 (which may of course be referenced), c->expr1 will be set. */
3637 if (csym && csym->attr.abstract && !c->expr1)
3639 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3640 csym->name, &c->loc);
3641 return FAILURE;
3644 /* Subroutines without the RECURSIVE attribution are not allowed to
3645 * call themselves. */
3646 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3648 if (csym->attr.entry && csym->ns->entries)
3649 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3650 " subroutine '%s' is not RECURSIVE",
3651 csym->name, &c->loc, csym->ns->entries->sym->name);
3652 else
3653 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3654 " is not RECURSIVE", csym->name, &c->loc);
3656 t = FAILURE;
3659 /* Switch off assumed size checking and do this again for certain kinds
3660 of procedure, once the procedure itself is resolved. */
3661 need_full_assumed_size++;
3663 if (csym)
3664 ptype = csym->attr.proc;
3666 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3667 if (resolve_actual_arglist (c->ext.actual, ptype,
3668 no_formal_args) == FAILURE)
3669 return FAILURE;
3671 /* Resume assumed_size checking. */
3672 need_full_assumed_size--;
3674 /* If external, check for usage. */
3675 if (csym && is_external_proc (csym))
3676 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3678 t = SUCCESS;
3679 if (c->resolved_sym == NULL)
3681 c->resolved_isym = NULL;
3682 switch (procedure_kind (csym))
3684 case PTYPE_GENERIC:
3685 t = resolve_generic_s (c);
3686 break;
3688 case PTYPE_SPECIFIC:
3689 t = resolve_specific_s (c);
3690 break;
3692 case PTYPE_UNKNOWN:
3693 t = resolve_unknown_s (c);
3694 break;
3696 default:
3697 gfc_internal_error ("resolve_subroutine(): bad function type");
3701 /* Some checks of elemental subroutine actual arguments. */
3702 if (resolve_elemental_actual (NULL, c) == FAILURE)
3703 return FAILURE;
3705 return t;
3709 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3710 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3711 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3712 if their shapes do not match. If either op1->shape or op2->shape is
3713 NULL, return SUCCESS. */
3715 static gfc_try
3716 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3718 gfc_try t;
3719 int i;
3721 t = SUCCESS;
3723 if (op1->shape != NULL && op2->shape != NULL)
3725 for (i = 0; i < op1->rank; i++)
3727 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3729 gfc_error ("Shapes for operands at %L and %L are not conformable",
3730 &op1->where, &op2->where);
3731 t = FAILURE;
3732 break;
3737 return t;
3741 /* Resolve an operator expression node. This can involve replacing the
3742 operation with a user defined function call. */
3744 static gfc_try
3745 resolve_operator (gfc_expr *e)
3747 gfc_expr *op1, *op2;
3748 char msg[200];
3749 bool dual_locus_error;
3750 gfc_try t;
3752 /* Resolve all subnodes-- give them types. */
3754 switch (e->value.op.op)
3756 default:
3757 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3758 return FAILURE;
3760 /* Fall through... */
3762 case INTRINSIC_NOT:
3763 case INTRINSIC_UPLUS:
3764 case INTRINSIC_UMINUS:
3765 case INTRINSIC_PARENTHESES:
3766 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3767 return FAILURE;
3768 break;
3771 /* Typecheck the new node. */
3773 op1 = e->value.op.op1;
3774 op2 = e->value.op.op2;
3775 dual_locus_error = false;
3777 if ((op1 && op1->expr_type == EXPR_NULL)
3778 || (op2 && op2->expr_type == EXPR_NULL))
3780 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3781 goto bad_op;
3784 switch (e->value.op.op)
3786 case INTRINSIC_UPLUS:
3787 case INTRINSIC_UMINUS:
3788 if (op1->ts.type == BT_INTEGER
3789 || op1->ts.type == BT_REAL
3790 || op1->ts.type == BT_COMPLEX)
3792 e->ts = op1->ts;
3793 break;
3796 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3797 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3798 goto bad_op;
3800 case INTRINSIC_PLUS:
3801 case INTRINSIC_MINUS:
3802 case INTRINSIC_TIMES:
3803 case INTRINSIC_DIVIDE:
3804 case INTRINSIC_POWER:
3805 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3807 gfc_type_convert_binary (e, 1);
3808 break;
3811 sprintf (msg,
3812 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3813 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3814 gfc_typename (&op2->ts));
3815 goto bad_op;
3817 case INTRINSIC_CONCAT:
3818 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3819 && op1->ts.kind == op2->ts.kind)
3821 e->ts.type = BT_CHARACTER;
3822 e->ts.kind = op1->ts.kind;
3823 break;
3826 sprintf (msg,
3827 _("Operands of string concatenation operator at %%L are %s/%s"),
3828 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3829 goto bad_op;
3831 case INTRINSIC_AND:
3832 case INTRINSIC_OR:
3833 case INTRINSIC_EQV:
3834 case INTRINSIC_NEQV:
3835 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3837 e->ts.type = BT_LOGICAL;
3838 e->ts.kind = gfc_kind_max (op1, op2);
3839 if (op1->ts.kind < e->ts.kind)
3840 gfc_convert_type (op1, &e->ts, 2);
3841 else if (op2->ts.kind < e->ts.kind)
3842 gfc_convert_type (op2, &e->ts, 2);
3843 break;
3846 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3847 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3848 gfc_typename (&op2->ts));
3850 goto bad_op;
3852 case INTRINSIC_NOT:
3853 if (op1->ts.type == BT_LOGICAL)
3855 e->ts.type = BT_LOGICAL;
3856 e->ts.kind = op1->ts.kind;
3857 break;
3860 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3861 gfc_typename (&op1->ts));
3862 goto bad_op;
3864 case INTRINSIC_GT:
3865 case INTRINSIC_GT_OS:
3866 case INTRINSIC_GE:
3867 case INTRINSIC_GE_OS:
3868 case INTRINSIC_LT:
3869 case INTRINSIC_LT_OS:
3870 case INTRINSIC_LE:
3871 case INTRINSIC_LE_OS:
3872 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3874 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3875 goto bad_op;
3878 /* Fall through... */
3880 case INTRINSIC_EQ:
3881 case INTRINSIC_EQ_OS:
3882 case INTRINSIC_NE:
3883 case INTRINSIC_NE_OS:
3884 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3885 && op1->ts.kind == op2->ts.kind)
3887 e->ts.type = BT_LOGICAL;
3888 e->ts.kind = gfc_default_logical_kind;
3889 break;
3892 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3894 gfc_type_convert_binary (e, 1);
3896 e->ts.type = BT_LOGICAL;
3897 e->ts.kind = gfc_default_logical_kind;
3898 break;
3901 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3902 sprintf (msg,
3903 _("Logicals at %%L must be compared with %s instead of %s"),
3904 (e->value.op.op == INTRINSIC_EQ
3905 || e->value.op.op == INTRINSIC_EQ_OS)
3906 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3907 else
3908 sprintf (msg,
3909 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3910 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3911 gfc_typename (&op2->ts));
3913 goto bad_op;
3915 case INTRINSIC_USER:
3916 if (e->value.op.uop->op == NULL)
3917 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3918 else if (op2 == NULL)
3919 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3920 e->value.op.uop->name, gfc_typename (&op1->ts));
3921 else
3923 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3924 e->value.op.uop->name, gfc_typename (&op1->ts),
3925 gfc_typename (&op2->ts));
3926 e->value.op.uop->op->sym->attr.referenced = 1;
3929 goto bad_op;
3931 case INTRINSIC_PARENTHESES:
3932 e->ts = op1->ts;
3933 if (e->ts.type == BT_CHARACTER)
3934 e->ts.u.cl = op1->ts.u.cl;
3935 break;
3937 default:
3938 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3941 /* Deal with arrayness of an operand through an operator. */
3943 t = SUCCESS;
3945 switch (e->value.op.op)
3947 case INTRINSIC_PLUS:
3948 case INTRINSIC_MINUS:
3949 case INTRINSIC_TIMES:
3950 case INTRINSIC_DIVIDE:
3951 case INTRINSIC_POWER:
3952 case INTRINSIC_CONCAT:
3953 case INTRINSIC_AND:
3954 case INTRINSIC_OR:
3955 case INTRINSIC_EQV:
3956 case INTRINSIC_NEQV:
3957 case INTRINSIC_EQ:
3958 case INTRINSIC_EQ_OS:
3959 case INTRINSIC_NE:
3960 case INTRINSIC_NE_OS:
3961 case INTRINSIC_GT:
3962 case INTRINSIC_GT_OS:
3963 case INTRINSIC_GE:
3964 case INTRINSIC_GE_OS:
3965 case INTRINSIC_LT:
3966 case INTRINSIC_LT_OS:
3967 case INTRINSIC_LE:
3968 case INTRINSIC_LE_OS:
3970 if (op1->rank == 0 && op2->rank == 0)
3971 e->rank = 0;
3973 if (op1->rank == 0 && op2->rank != 0)
3975 e->rank = op2->rank;
3977 if (e->shape == NULL)
3978 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3981 if (op1->rank != 0 && op2->rank == 0)
3983 e->rank = op1->rank;
3985 if (e->shape == NULL)
3986 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3989 if (op1->rank != 0 && op2->rank != 0)
3991 if (op1->rank == op2->rank)
3993 e->rank = op1->rank;
3994 if (e->shape == NULL)
3996 t = compare_shapes (op1, op2);
3997 if (t == FAILURE)
3998 e->shape = NULL;
3999 else
4000 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4003 else
4005 /* Allow higher level expressions to work. */
4006 e->rank = 0;
4008 /* Try user-defined operators, and otherwise throw an error. */
4009 dual_locus_error = true;
4010 sprintf (msg,
4011 _("Inconsistent ranks for operator at %%L and %%L"));
4012 goto bad_op;
4016 break;
4018 case INTRINSIC_PARENTHESES:
4019 case INTRINSIC_NOT:
4020 case INTRINSIC_UPLUS:
4021 case INTRINSIC_UMINUS:
4022 /* Simply copy arrayness attribute */
4023 e->rank = op1->rank;
4025 if (e->shape == NULL)
4026 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4028 break;
4030 default:
4031 break;
4034 /* Attempt to simplify the expression. */
4035 if (t == SUCCESS)
4037 t = gfc_simplify_expr (e, 0);
4038 /* Some calls do not succeed in simplification and return FAILURE
4039 even though there is no error; e.g. variable references to
4040 PARAMETER arrays. */
4041 if (!gfc_is_constant_expr (e))
4042 t = SUCCESS;
4044 return t;
4046 bad_op:
4049 bool real_error;
4050 if (gfc_extend_expr (e, &real_error) == SUCCESS)
4051 return SUCCESS;
4053 if (real_error)
4054 return FAILURE;
4057 if (dual_locus_error)
4058 gfc_error (msg, &op1->where, &op2->where);
4059 else
4060 gfc_error (msg, &e->where);
4062 return FAILURE;
4066 /************** Array resolution subroutines **************/
4068 typedef enum
4069 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
4070 comparison;
4072 /* Compare two integer expressions. */
4074 static comparison
4075 compare_bound (gfc_expr *a, gfc_expr *b)
4077 int i;
4079 if (a == NULL || a->expr_type != EXPR_CONSTANT
4080 || b == NULL || b->expr_type != EXPR_CONSTANT)
4081 return CMP_UNKNOWN;
4083 /* If either of the types isn't INTEGER, we must have
4084 raised an error earlier. */
4086 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4087 return CMP_UNKNOWN;
4089 i = mpz_cmp (a->value.integer, b->value.integer);
4091 if (i < 0)
4092 return CMP_LT;
4093 if (i > 0)
4094 return CMP_GT;
4095 return CMP_EQ;
4099 /* Compare an integer expression with an integer. */
4101 static comparison
4102 compare_bound_int (gfc_expr *a, int b)
4104 int i;
4106 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4107 return CMP_UNKNOWN;
4109 if (a->ts.type != BT_INTEGER)
4110 gfc_internal_error ("compare_bound_int(): Bad expression");
4112 i = mpz_cmp_si (a->value.integer, b);
4114 if (i < 0)
4115 return CMP_LT;
4116 if (i > 0)
4117 return CMP_GT;
4118 return CMP_EQ;
4122 /* Compare an integer expression with a mpz_t. */
4124 static comparison
4125 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4127 int i;
4129 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4130 return CMP_UNKNOWN;
4132 if (a->ts.type != BT_INTEGER)
4133 gfc_internal_error ("compare_bound_int(): Bad expression");
4135 i = mpz_cmp (a->value.integer, b);
4137 if (i < 0)
4138 return CMP_LT;
4139 if (i > 0)
4140 return CMP_GT;
4141 return CMP_EQ;
4145 /* Compute the last value of a sequence given by a triplet.
4146 Return 0 if it wasn't able to compute the last value, or if the
4147 sequence if empty, and 1 otherwise. */
4149 static int
4150 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4151 gfc_expr *stride, mpz_t last)
4153 mpz_t rem;
4155 if (start == NULL || start->expr_type != EXPR_CONSTANT
4156 || end == NULL || end->expr_type != EXPR_CONSTANT
4157 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4158 return 0;
4160 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4161 || (stride != NULL && stride->ts.type != BT_INTEGER))
4162 return 0;
4164 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
4166 if (compare_bound (start, end) == CMP_GT)
4167 return 0;
4168 mpz_set (last, end->value.integer);
4169 return 1;
4172 if (compare_bound_int (stride, 0) == CMP_GT)
4174 /* Stride is positive */
4175 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4176 return 0;
4178 else
4180 /* Stride is negative */
4181 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4182 return 0;
4185 mpz_init (rem);
4186 mpz_sub (rem, end->value.integer, start->value.integer);
4187 mpz_tdiv_r (rem, rem, stride->value.integer);
4188 mpz_sub (last, end->value.integer, rem);
4189 mpz_clear (rem);
4191 return 1;
4195 /* Compare a single dimension of an array reference to the array
4196 specification. */
4198 static gfc_try
4199 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4201 mpz_t last_value;
4203 if (ar->dimen_type[i] == DIMEN_STAR)
4205 gcc_assert (ar->stride[i] == NULL);
4206 /* This implies [*] as [*:] and [*:3] are not possible. */
4207 if (ar->start[i] == NULL)
4209 gcc_assert (ar->end[i] == NULL);
4210 return SUCCESS;
4214 /* Given start, end and stride values, calculate the minimum and
4215 maximum referenced indexes. */
4217 switch (ar->dimen_type[i])
4219 case DIMEN_VECTOR:
4220 case DIMEN_THIS_IMAGE:
4221 break;
4223 case DIMEN_STAR:
4224 case DIMEN_ELEMENT:
4225 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4227 if (i < as->rank)
4228 gfc_warning ("Array reference at %L is out of bounds "
4229 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4230 mpz_get_si (ar->start[i]->value.integer),
4231 mpz_get_si (as->lower[i]->value.integer), i+1);
4232 else
4233 gfc_warning ("Array reference at %L is out of bounds "
4234 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4235 mpz_get_si (ar->start[i]->value.integer),
4236 mpz_get_si (as->lower[i]->value.integer),
4237 i + 1 - as->rank);
4238 return SUCCESS;
4240 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4242 if (i < as->rank)
4243 gfc_warning ("Array reference at %L is out of bounds "
4244 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4245 mpz_get_si (ar->start[i]->value.integer),
4246 mpz_get_si (as->upper[i]->value.integer), i+1);
4247 else
4248 gfc_warning ("Array reference at %L is out of bounds "
4249 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4250 mpz_get_si (ar->start[i]->value.integer),
4251 mpz_get_si (as->upper[i]->value.integer),
4252 i + 1 - as->rank);
4253 return SUCCESS;
4256 break;
4258 case DIMEN_RANGE:
4260 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4261 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4263 comparison comp_start_end = compare_bound (AR_START, AR_END);
4265 /* Check for zero stride, which is not allowed. */
4266 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4268 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4269 return FAILURE;
4272 /* if start == len || (stride > 0 && start < len)
4273 || (stride < 0 && start > len),
4274 then the array section contains at least one element. In this
4275 case, there is an out-of-bounds access if
4276 (start < lower || start > upper). */
4277 if (compare_bound (AR_START, AR_END) == CMP_EQ
4278 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4279 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4280 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4281 && comp_start_end == CMP_GT))
4283 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4285 gfc_warning ("Lower array reference at %L is out of bounds "
4286 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4287 mpz_get_si (AR_START->value.integer),
4288 mpz_get_si (as->lower[i]->value.integer), i+1);
4289 return SUCCESS;
4291 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4293 gfc_warning ("Lower array reference at %L is out of bounds "
4294 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4295 mpz_get_si (AR_START->value.integer),
4296 mpz_get_si (as->upper[i]->value.integer), i+1);
4297 return SUCCESS;
4301 /* If we can compute the highest index of the array section,
4302 then it also has to be between lower and upper. */
4303 mpz_init (last_value);
4304 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4305 last_value))
4307 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4309 gfc_warning ("Upper array reference at %L is out of bounds "
4310 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4311 mpz_get_si (last_value),
4312 mpz_get_si (as->lower[i]->value.integer), i+1);
4313 mpz_clear (last_value);
4314 return SUCCESS;
4316 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4318 gfc_warning ("Upper array reference at %L is out of bounds "
4319 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4320 mpz_get_si (last_value),
4321 mpz_get_si (as->upper[i]->value.integer), i+1);
4322 mpz_clear (last_value);
4323 return SUCCESS;
4326 mpz_clear (last_value);
4328 #undef AR_START
4329 #undef AR_END
4331 break;
4333 default:
4334 gfc_internal_error ("check_dimension(): Bad array reference");
4337 return SUCCESS;
4341 /* Compare an array reference with an array specification. */
4343 static gfc_try
4344 compare_spec_to_ref (gfc_array_ref *ar)
4346 gfc_array_spec *as;
4347 int i;
4349 as = ar->as;
4350 i = as->rank - 1;
4351 /* TODO: Full array sections are only allowed as actual parameters. */
4352 if (as->type == AS_ASSUMED_SIZE
4353 && (/*ar->type == AR_FULL
4354 ||*/ (ar->type == AR_SECTION
4355 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4357 gfc_error ("Rightmost upper bound of assumed size array section "
4358 "not specified at %L", &ar->where);
4359 return FAILURE;
4362 if (ar->type == AR_FULL)
4363 return SUCCESS;
4365 if (as->rank != ar->dimen)
4367 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4368 &ar->where, ar->dimen, as->rank);
4369 return FAILURE;
4372 /* ar->codimen == 0 is a local array. */
4373 if (as->corank != ar->codimen && ar->codimen != 0)
4375 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4376 &ar->where, ar->codimen, as->corank);
4377 return FAILURE;
4380 for (i = 0; i < as->rank; i++)
4381 if (check_dimension (i, ar, as) == FAILURE)
4382 return FAILURE;
4384 /* Local access has no coarray spec. */
4385 if (ar->codimen != 0)
4386 for (i = as->rank; i < as->rank + as->corank; i++)
4388 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4389 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4391 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4392 i + 1 - as->rank, &ar->where);
4393 return FAILURE;
4395 if (check_dimension (i, ar, as) == FAILURE)
4396 return FAILURE;
4399 if (as->corank && ar->codimen == 0)
4401 int n;
4402 ar->codimen = as->corank;
4403 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4404 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4407 return SUCCESS;
4411 /* Resolve one part of an array index. */
4413 static gfc_try
4414 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4415 int force_index_integer_kind)
4417 gfc_typespec ts;
4419 if (index == NULL)
4420 return SUCCESS;
4422 if (gfc_resolve_expr (index) == FAILURE)
4423 return FAILURE;
4425 if (check_scalar && index->rank != 0)
4427 gfc_error ("Array index at %L must be scalar", &index->where);
4428 return FAILURE;
4431 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4433 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4434 &index->where, gfc_basic_typename (index->ts.type));
4435 return FAILURE;
4438 if (index->ts.type == BT_REAL)
4439 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
4440 &index->where) == FAILURE)
4441 return FAILURE;
4443 if ((index->ts.kind != gfc_index_integer_kind
4444 && force_index_integer_kind)
4445 || index->ts.type != BT_INTEGER)
4447 gfc_clear_ts (&ts);
4448 ts.type = BT_INTEGER;
4449 ts.kind = gfc_index_integer_kind;
4451 gfc_convert_type_warn (index, &ts, 2, 0);
4454 return SUCCESS;
4457 /* Resolve one part of an array index. */
4459 gfc_try
4460 gfc_resolve_index (gfc_expr *index, int check_scalar)
4462 return gfc_resolve_index_1 (index, check_scalar, 1);
4465 /* Resolve a dim argument to an intrinsic function. */
4467 gfc_try
4468 gfc_resolve_dim_arg (gfc_expr *dim)
4470 if (dim == NULL)
4471 return SUCCESS;
4473 if (gfc_resolve_expr (dim) == FAILURE)
4474 return FAILURE;
4476 if (dim->rank != 0)
4478 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4479 return FAILURE;
4483 if (dim->ts.type != BT_INTEGER)
4485 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4486 return FAILURE;
4489 if (dim->ts.kind != gfc_index_integer_kind)
4491 gfc_typespec ts;
4493 gfc_clear_ts (&ts);
4494 ts.type = BT_INTEGER;
4495 ts.kind = gfc_index_integer_kind;
4497 gfc_convert_type_warn (dim, &ts, 2, 0);
4500 return SUCCESS;
4503 /* Given an expression that contains array references, update those array
4504 references to point to the right array specifications. While this is
4505 filled in during matching, this information is difficult to save and load
4506 in a module, so we take care of it here.
4508 The idea here is that the original array reference comes from the
4509 base symbol. We traverse the list of reference structures, setting
4510 the stored reference to references. Component references can
4511 provide an additional array specification. */
4513 static void
4514 find_array_spec (gfc_expr *e)
4516 gfc_array_spec *as;
4517 gfc_component *c;
4518 gfc_symbol *derived;
4519 gfc_ref *ref;
4521 if (e->symtree->n.sym->ts.type == BT_CLASS)
4522 as = CLASS_DATA (e->symtree->n.sym)->as;
4523 else
4524 as = e->symtree->n.sym->as;
4525 derived = NULL;
4527 for (ref = e->ref; ref; ref = ref->next)
4528 switch (ref->type)
4530 case REF_ARRAY:
4531 if (as == NULL)
4532 gfc_internal_error ("find_array_spec(): Missing spec");
4534 ref->u.ar.as = as;
4535 as = NULL;
4536 break;
4538 case REF_COMPONENT:
4539 if (derived == NULL)
4540 derived = e->symtree->n.sym->ts.u.derived;
4542 if (derived->attr.is_class)
4543 derived = derived->components->ts.u.derived;
4545 c = derived->components;
4547 for (; c; c = c->next)
4548 if (c == ref->u.c.component)
4550 /* Track the sequence of component references. */
4551 if (c->ts.type == BT_DERIVED)
4552 derived = c->ts.u.derived;
4553 break;
4556 if (c == NULL)
4557 gfc_internal_error ("find_array_spec(): Component not found");
4559 if (c->attr.dimension)
4561 if (as != NULL)
4562 gfc_internal_error ("find_array_spec(): unused as(1)");
4563 as = c->as;
4566 break;
4568 case REF_SUBSTRING:
4569 break;
4572 if (as != NULL)
4573 gfc_internal_error ("find_array_spec(): unused as(2)");
4577 /* Resolve an array reference. */
4579 static gfc_try
4580 resolve_array_ref (gfc_array_ref *ar)
4582 int i, check_scalar;
4583 gfc_expr *e;
4585 for (i = 0; i < ar->dimen + ar->codimen; i++)
4587 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4589 /* Do not force gfc_index_integer_kind for the start. We can
4590 do fine with any integer kind. This avoids temporary arrays
4591 created for indexing with a vector. */
4592 if (gfc_resolve_index_1 (ar->start[i], check_scalar, 0) == FAILURE)
4593 return FAILURE;
4594 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4595 return FAILURE;
4596 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4597 return FAILURE;
4599 e = ar->start[i];
4601 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4602 switch (e->rank)
4604 case 0:
4605 ar->dimen_type[i] = DIMEN_ELEMENT;
4606 break;
4608 case 1:
4609 ar->dimen_type[i] = DIMEN_VECTOR;
4610 if (e->expr_type == EXPR_VARIABLE
4611 && e->symtree->n.sym->ts.type == BT_DERIVED)
4612 ar->start[i] = gfc_get_parentheses (e);
4613 break;
4615 default:
4616 gfc_error ("Array index at %L is an array of rank %d",
4617 &ar->c_where[i], e->rank);
4618 return FAILURE;
4621 /* Fill in the upper bound, which may be lower than the
4622 specified one for something like a(2:10:5), which is
4623 identical to a(2:7:5). Only relevant for strides not equal
4624 to one. Don't try a division by zero. */
4625 if (ar->dimen_type[i] == DIMEN_RANGE
4626 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4627 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4628 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4630 mpz_t size, end;
4632 if (gfc_ref_dimen_size (ar, i, &size, &end) == SUCCESS)
4634 if (ar->end[i] == NULL)
4636 ar->end[i] =
4637 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4638 &ar->where);
4639 mpz_set (ar->end[i]->value.integer, end);
4641 else if (ar->end[i]->ts.type == BT_INTEGER
4642 && ar->end[i]->expr_type == EXPR_CONSTANT)
4644 mpz_set (ar->end[i]->value.integer, end);
4646 else
4647 gcc_unreachable ();
4649 mpz_clear (size);
4650 mpz_clear (end);
4655 if (ar->type == AR_FULL && ar->as->rank == 0)
4656 ar->type = AR_ELEMENT;
4658 /* If the reference type is unknown, figure out what kind it is. */
4660 if (ar->type == AR_UNKNOWN)
4662 ar->type = AR_ELEMENT;
4663 for (i = 0; i < ar->dimen; i++)
4664 if (ar->dimen_type[i] == DIMEN_RANGE
4665 || ar->dimen_type[i] == DIMEN_VECTOR)
4667 ar->type = AR_SECTION;
4668 break;
4672 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4673 return FAILURE;
4675 return SUCCESS;
4679 static gfc_try
4680 resolve_substring (gfc_ref *ref)
4682 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4684 if (ref->u.ss.start != NULL)
4686 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4687 return FAILURE;
4689 if (ref->u.ss.start->ts.type != BT_INTEGER)
4691 gfc_error ("Substring start index at %L must be of type INTEGER",
4692 &ref->u.ss.start->where);
4693 return FAILURE;
4696 if (ref->u.ss.start->rank != 0)
4698 gfc_error ("Substring start index at %L must be scalar",
4699 &ref->u.ss.start->where);
4700 return FAILURE;
4703 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4704 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4705 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4707 gfc_error ("Substring start index at %L is less than one",
4708 &ref->u.ss.start->where);
4709 return FAILURE;
4713 if (ref->u.ss.end != NULL)
4715 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4716 return FAILURE;
4718 if (ref->u.ss.end->ts.type != BT_INTEGER)
4720 gfc_error ("Substring end index at %L must be of type INTEGER",
4721 &ref->u.ss.end->where);
4722 return FAILURE;
4725 if (ref->u.ss.end->rank != 0)
4727 gfc_error ("Substring end index at %L must be scalar",
4728 &ref->u.ss.end->where);
4729 return FAILURE;
4732 if (ref->u.ss.length != NULL
4733 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4734 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4735 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4737 gfc_error ("Substring end index at %L exceeds the string length",
4738 &ref->u.ss.start->where);
4739 return FAILURE;
4742 if (compare_bound_mpz_t (ref->u.ss.end,
4743 gfc_integer_kinds[k].huge) == CMP_GT
4744 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4745 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4747 gfc_error ("Substring end index at %L is too large",
4748 &ref->u.ss.end->where);
4749 return FAILURE;
4753 return SUCCESS;
4757 /* This function supplies missing substring charlens. */
4759 void
4760 gfc_resolve_substring_charlen (gfc_expr *e)
4762 gfc_ref *char_ref;
4763 gfc_expr *start, *end;
4765 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4766 if (char_ref->type == REF_SUBSTRING)
4767 break;
4769 if (!char_ref)
4770 return;
4772 gcc_assert (char_ref->next == NULL);
4774 if (e->ts.u.cl)
4776 if (e->ts.u.cl->length)
4777 gfc_free_expr (e->ts.u.cl->length);
4778 else if (e->expr_type == EXPR_VARIABLE
4779 && e->symtree->n.sym->attr.dummy)
4780 return;
4783 e->ts.type = BT_CHARACTER;
4784 e->ts.kind = gfc_default_character_kind;
4786 if (!e->ts.u.cl)
4787 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4789 if (char_ref->u.ss.start)
4790 start = gfc_copy_expr (char_ref->u.ss.start);
4791 else
4792 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4794 if (char_ref->u.ss.end)
4795 end = gfc_copy_expr (char_ref->u.ss.end);
4796 else if (e->expr_type == EXPR_VARIABLE)
4797 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4798 else
4799 end = NULL;
4801 if (!start || !end)
4802 return;
4804 /* Length = (end - start +1). */
4805 e->ts.u.cl->length = gfc_subtract (end, start);
4806 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4807 gfc_get_int_expr (gfc_default_integer_kind,
4808 NULL, 1));
4810 e->ts.u.cl->length->ts.type = BT_INTEGER;
4811 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4813 /* Make sure that the length is simplified. */
4814 gfc_simplify_expr (e->ts.u.cl->length, 1);
4815 gfc_resolve_expr (e->ts.u.cl->length);
4819 /* Resolve subtype references. */
4821 static gfc_try
4822 resolve_ref (gfc_expr *expr)
4824 int current_part_dimension, n_components, seen_part_dimension;
4825 gfc_ref *ref;
4827 for (ref = expr->ref; ref; ref = ref->next)
4828 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4830 find_array_spec (expr);
4831 break;
4834 for (ref = expr->ref; ref; ref = ref->next)
4835 switch (ref->type)
4837 case REF_ARRAY:
4838 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4839 return FAILURE;
4840 break;
4842 case REF_COMPONENT:
4843 break;
4845 case REF_SUBSTRING:
4846 resolve_substring (ref);
4847 break;
4850 /* Check constraints on part references. */
4852 current_part_dimension = 0;
4853 seen_part_dimension = 0;
4854 n_components = 0;
4856 for (ref = expr->ref; ref; ref = ref->next)
4858 switch (ref->type)
4860 case REF_ARRAY:
4861 switch (ref->u.ar.type)
4863 case AR_FULL:
4864 /* Coarray scalar. */
4865 if (ref->u.ar.as->rank == 0)
4867 current_part_dimension = 0;
4868 break;
4870 /* Fall through. */
4871 case AR_SECTION:
4872 current_part_dimension = 1;
4873 break;
4875 case AR_ELEMENT:
4876 current_part_dimension = 0;
4877 break;
4879 case AR_UNKNOWN:
4880 gfc_internal_error ("resolve_ref(): Bad array reference");
4883 break;
4885 case REF_COMPONENT:
4886 if (current_part_dimension || seen_part_dimension)
4888 /* F03:C614. */
4889 if (ref->u.c.component->attr.pointer
4890 || ref->u.c.component->attr.proc_pointer)
4892 gfc_error ("Component to the right of a part reference "
4893 "with nonzero rank must not have the POINTER "
4894 "attribute at %L", &expr->where);
4895 return FAILURE;
4897 else if (ref->u.c.component->attr.allocatable)
4899 gfc_error ("Component to the right of a part reference "
4900 "with nonzero rank must not have the ALLOCATABLE "
4901 "attribute at %L", &expr->where);
4902 return FAILURE;
4906 n_components++;
4907 break;
4909 case REF_SUBSTRING:
4910 break;
4913 if (((ref->type == REF_COMPONENT && n_components > 1)
4914 || ref->next == NULL)
4915 && current_part_dimension
4916 && seen_part_dimension)
4918 gfc_error ("Two or more part references with nonzero rank must "
4919 "not be specified at %L", &expr->where);
4920 return FAILURE;
4923 if (ref->type == REF_COMPONENT)
4925 if (current_part_dimension)
4926 seen_part_dimension = 1;
4928 /* reset to make sure */
4929 current_part_dimension = 0;
4933 return SUCCESS;
4937 /* Given an expression, determine its shape. This is easier than it sounds.
4938 Leaves the shape array NULL if it is not possible to determine the shape. */
4940 static void
4941 expression_shape (gfc_expr *e)
4943 mpz_t array[GFC_MAX_DIMENSIONS];
4944 int i;
4946 if (e->rank == 0 || e->shape != NULL)
4947 return;
4949 for (i = 0; i < e->rank; i++)
4950 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4951 goto fail;
4953 e->shape = gfc_get_shape (e->rank);
4955 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4957 return;
4959 fail:
4960 for (i--; i >= 0; i--)
4961 mpz_clear (array[i]);
4965 /* Given a variable expression node, compute the rank of the expression by
4966 examining the base symbol and any reference structures it may have. */
4968 static void
4969 expression_rank (gfc_expr *e)
4971 gfc_ref *ref;
4972 int i, rank;
4974 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4975 could lead to serious confusion... */
4976 gcc_assert (e->expr_type != EXPR_COMPCALL);
4978 if (e->ref == NULL)
4980 if (e->expr_type == EXPR_ARRAY)
4981 goto done;
4982 /* Constructors can have a rank different from one via RESHAPE(). */
4984 if (e->symtree == NULL)
4986 e->rank = 0;
4987 goto done;
4990 e->rank = (e->symtree->n.sym->as == NULL)
4991 ? 0 : e->symtree->n.sym->as->rank;
4992 goto done;
4995 rank = 0;
4997 for (ref = e->ref; ref; ref = ref->next)
4999 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
5000 && ref->u.c.component->attr.function && !ref->next)
5001 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
5003 if (ref->type != REF_ARRAY)
5004 continue;
5006 if (ref->u.ar.type == AR_FULL)
5008 rank = ref->u.ar.as->rank;
5009 break;
5012 if (ref->u.ar.type == AR_SECTION)
5014 /* Figure out the rank of the section. */
5015 if (rank != 0)
5016 gfc_internal_error ("expression_rank(): Two array specs");
5018 for (i = 0; i < ref->u.ar.dimen; i++)
5019 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5020 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
5021 rank++;
5023 break;
5027 e->rank = rank;
5029 done:
5030 expression_shape (e);
5034 /* Resolve a variable expression. */
5036 static gfc_try
5037 resolve_variable (gfc_expr *e)
5039 gfc_symbol *sym;
5040 gfc_try t;
5042 t = SUCCESS;
5044 if (e->symtree == NULL)
5045 return FAILURE;
5046 sym = e->symtree->n.sym;
5048 /* If this is an associate-name, it may be parsed with an array reference
5049 in error even though the target is scalar. Fail directly in this case. */
5050 if (sym->assoc && !sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5051 return FAILURE;
5053 /* On the other hand, the parser may not have known this is an array;
5054 in this case, we have to add a FULL reference. */
5055 if (sym->assoc && sym->attr.dimension && !e->ref)
5057 e->ref = gfc_get_ref ();
5058 e->ref->type = REF_ARRAY;
5059 e->ref->u.ar.type = AR_FULL;
5060 e->ref->u.ar.dimen = 0;
5063 if (e->ref && resolve_ref (e) == FAILURE)
5064 return FAILURE;
5066 if (sym->attr.flavor == FL_PROCEDURE
5067 && (!sym->attr.function
5068 || (sym->attr.function && sym->result
5069 && sym->result->attr.proc_pointer
5070 && !sym->result->attr.function)))
5072 e->ts.type = BT_PROCEDURE;
5073 goto resolve_procedure;
5076 if (sym->ts.type != BT_UNKNOWN)
5077 gfc_variable_attr (e, &e->ts);
5078 else
5080 /* Must be a simple variable reference. */
5081 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
5082 return FAILURE;
5083 e->ts = sym->ts;
5086 if (check_assumed_size_reference (sym, e))
5087 return FAILURE;
5089 /* Deal with forward references to entries during resolve_code, to
5090 satisfy, at least partially, 12.5.2.5. */
5091 if (gfc_current_ns->entries
5092 && current_entry_id == sym->entry_id
5093 && cs_base
5094 && cs_base->current
5095 && cs_base->current->op != EXEC_ENTRY)
5097 gfc_entry_list *entry;
5098 gfc_formal_arglist *formal;
5099 int n;
5100 bool seen;
5102 /* If the symbol is a dummy... */
5103 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5105 entry = gfc_current_ns->entries;
5106 seen = false;
5108 /* ...test if the symbol is a parameter of previous entries. */
5109 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5110 for (formal = entry->sym->formal; formal; formal = formal->next)
5112 if (formal->sym && sym->name == formal->sym->name)
5113 seen = true;
5116 /* If it has not been seen as a dummy, this is an error. */
5117 if (!seen)
5119 if (specification_expr)
5120 gfc_error ("Variable '%s', used in a specification expression"
5121 ", is referenced at %L before the ENTRY statement "
5122 "in which it is a parameter",
5123 sym->name, &cs_base->current->loc);
5124 else
5125 gfc_error ("Variable '%s' is used at %L before the ENTRY "
5126 "statement in which it is a parameter",
5127 sym->name, &cs_base->current->loc);
5128 t = FAILURE;
5132 /* Now do the same check on the specification expressions. */
5133 specification_expr = 1;
5134 if (sym->ts.type == BT_CHARACTER
5135 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
5136 t = FAILURE;
5138 if (sym->as)
5139 for (n = 0; n < sym->as->rank; n++)
5141 specification_expr = 1;
5142 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
5143 t = FAILURE;
5144 specification_expr = 1;
5145 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
5146 t = FAILURE;
5148 specification_expr = 0;
5150 if (t == SUCCESS)
5151 /* Update the symbol's entry level. */
5152 sym->entry_id = current_entry_id + 1;
5155 /* If a symbol has been host_associated mark it. This is used latter,
5156 to identify if aliasing is possible via host association. */
5157 if (sym->attr.flavor == FL_VARIABLE
5158 && gfc_current_ns->parent
5159 && (gfc_current_ns->parent == sym->ns
5160 || (gfc_current_ns->parent->parent
5161 && gfc_current_ns->parent->parent == sym->ns)))
5162 sym->attr.host_assoc = 1;
5164 resolve_procedure:
5165 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
5166 t = FAILURE;
5168 /* F2008, C617 and C1229. */
5169 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5170 && gfc_is_coindexed (e))
5172 gfc_ref *ref, *ref2 = NULL;
5174 for (ref = e->ref; ref; ref = ref->next)
5176 if (ref->type == REF_COMPONENT)
5177 ref2 = ref;
5178 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5179 break;
5182 for ( ; ref; ref = ref->next)
5183 if (ref->type == REF_COMPONENT)
5184 break;
5186 /* Expression itself is not coindexed object. */
5187 if (ref && e->ts.type == BT_CLASS)
5189 gfc_error ("Polymorphic subobject of coindexed object at %L",
5190 &e->where);
5191 t = FAILURE;
5194 /* Expression itself is coindexed object. */
5195 if (ref == NULL)
5197 gfc_component *c;
5198 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5199 for ( ; c; c = c->next)
5200 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5202 gfc_error ("Coindexed object with polymorphic allocatable "
5203 "subcomponent at %L", &e->where);
5204 t = FAILURE;
5205 break;
5210 return t;
5214 /* Checks to see that the correct symbol has been host associated.
5215 The only situation where this arises is that in which a twice
5216 contained function is parsed after the host association is made.
5217 Therefore, on detecting this, change the symbol in the expression
5218 and convert the array reference into an actual arglist if the old
5219 symbol is a variable. */
5220 static bool
5221 check_host_association (gfc_expr *e)
5223 gfc_symbol *sym, *old_sym;
5224 gfc_symtree *st;
5225 int n;
5226 gfc_ref *ref;
5227 gfc_actual_arglist *arg, *tail = NULL;
5228 bool retval = e->expr_type == EXPR_FUNCTION;
5230 /* If the expression is the result of substitution in
5231 interface.c(gfc_extend_expr) because there is no way in
5232 which the host association can be wrong. */
5233 if (e->symtree == NULL
5234 || e->symtree->n.sym == NULL
5235 || e->user_operator)
5236 return retval;
5238 old_sym = e->symtree->n.sym;
5240 if (gfc_current_ns->parent
5241 && old_sym->ns != gfc_current_ns)
5243 /* Use the 'USE' name so that renamed module symbols are
5244 correctly handled. */
5245 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5247 if (sym && old_sym != sym
5248 && sym->ts.type == old_sym->ts.type
5249 && sym->attr.flavor == FL_PROCEDURE
5250 && sym->attr.contained)
5252 /* Clear the shape, since it might not be valid. */
5253 gfc_free_shape (&e->shape, e->rank);
5255 /* Give the expression the right symtree! */
5256 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5257 gcc_assert (st != NULL);
5259 if (old_sym->attr.flavor == FL_PROCEDURE
5260 || e->expr_type == EXPR_FUNCTION)
5262 /* Original was function so point to the new symbol, since
5263 the actual argument list is already attached to the
5264 expression. */
5265 e->value.function.esym = NULL;
5266 e->symtree = st;
5268 else
5270 /* Original was variable so convert array references into
5271 an actual arglist. This does not need any checking now
5272 since resolve_function will take care of it. */
5273 e->value.function.actual = NULL;
5274 e->expr_type = EXPR_FUNCTION;
5275 e->symtree = st;
5277 /* Ambiguity will not arise if the array reference is not
5278 the last reference. */
5279 for (ref = e->ref; ref; ref = ref->next)
5280 if (ref->type == REF_ARRAY && ref->next == NULL)
5281 break;
5283 gcc_assert (ref->type == REF_ARRAY);
5285 /* Grab the start expressions from the array ref and
5286 copy them into actual arguments. */
5287 for (n = 0; n < ref->u.ar.dimen; n++)
5289 arg = gfc_get_actual_arglist ();
5290 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5291 if (e->value.function.actual == NULL)
5292 tail = e->value.function.actual = arg;
5293 else
5295 tail->next = arg;
5296 tail = arg;
5300 /* Dump the reference list and set the rank. */
5301 gfc_free_ref_list (e->ref);
5302 e->ref = NULL;
5303 e->rank = sym->as ? sym->as->rank : 0;
5306 gfc_resolve_expr (e);
5307 sym->refs++;
5310 /* This might have changed! */
5311 return e->expr_type == EXPR_FUNCTION;
5315 static void
5316 gfc_resolve_character_operator (gfc_expr *e)
5318 gfc_expr *op1 = e->value.op.op1;
5319 gfc_expr *op2 = e->value.op.op2;
5320 gfc_expr *e1 = NULL;
5321 gfc_expr *e2 = NULL;
5323 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5325 if (op1->ts.u.cl && op1->ts.u.cl->length)
5326 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5327 else if (op1->expr_type == EXPR_CONSTANT)
5328 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5329 op1->value.character.length);
5331 if (op2->ts.u.cl && op2->ts.u.cl->length)
5332 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5333 else if (op2->expr_type == EXPR_CONSTANT)
5334 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5335 op2->value.character.length);
5337 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5339 if (!e1 || !e2)
5340 return;
5342 e->ts.u.cl->length = gfc_add (e1, e2);
5343 e->ts.u.cl->length->ts.type = BT_INTEGER;
5344 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5345 gfc_simplify_expr (e->ts.u.cl->length, 0);
5346 gfc_resolve_expr (e->ts.u.cl->length);
5348 return;
5352 /* Ensure that an character expression has a charlen and, if possible, a
5353 length expression. */
5355 static void
5356 fixup_charlen (gfc_expr *e)
5358 /* The cases fall through so that changes in expression type and the need
5359 for multiple fixes are picked up. In all circumstances, a charlen should
5360 be available for the middle end to hang a backend_decl on. */
5361 switch (e->expr_type)
5363 case EXPR_OP:
5364 gfc_resolve_character_operator (e);
5366 case EXPR_ARRAY:
5367 if (e->expr_type == EXPR_ARRAY)
5368 gfc_resolve_character_array_constructor (e);
5370 case EXPR_SUBSTRING:
5371 if (!e->ts.u.cl && e->ref)
5372 gfc_resolve_substring_charlen (e);
5374 default:
5375 if (!e->ts.u.cl)
5376 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5378 break;
5383 /* Update an actual argument to include the passed-object for type-bound
5384 procedures at the right position. */
5386 static gfc_actual_arglist*
5387 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5388 const char *name)
5390 gcc_assert (argpos > 0);
5392 if (argpos == 1)
5394 gfc_actual_arglist* result;
5396 result = gfc_get_actual_arglist ();
5397 result->expr = po;
5398 result->next = lst;
5399 if (name)
5400 result->name = name;
5402 return result;
5405 if (lst)
5406 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5407 else
5408 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5409 return lst;
5413 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5415 static gfc_expr*
5416 extract_compcall_passed_object (gfc_expr* e)
5418 gfc_expr* po;
5420 gcc_assert (e->expr_type == EXPR_COMPCALL);
5422 if (e->value.compcall.base_object)
5423 po = gfc_copy_expr (e->value.compcall.base_object);
5424 else
5426 po = gfc_get_expr ();
5427 po->expr_type = EXPR_VARIABLE;
5428 po->symtree = e->symtree;
5429 po->ref = gfc_copy_ref (e->ref);
5430 po->where = e->where;
5433 if (gfc_resolve_expr (po) == FAILURE)
5434 return NULL;
5436 return po;
5440 /* Update the arglist of an EXPR_COMPCALL expression to include the
5441 passed-object. */
5443 static gfc_try
5444 update_compcall_arglist (gfc_expr* e)
5446 gfc_expr* po;
5447 gfc_typebound_proc* tbp;
5449 tbp = e->value.compcall.tbp;
5451 if (tbp->error)
5452 return FAILURE;
5454 po = extract_compcall_passed_object (e);
5455 if (!po)
5456 return FAILURE;
5458 if (tbp->nopass || e->value.compcall.ignore_pass)
5460 gfc_free_expr (po);
5461 return SUCCESS;
5464 gcc_assert (tbp->pass_arg_num > 0);
5465 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5466 tbp->pass_arg_num,
5467 tbp->pass_arg);
5469 return SUCCESS;
5473 /* Extract the passed object from a PPC call (a copy of it). */
5475 static gfc_expr*
5476 extract_ppc_passed_object (gfc_expr *e)
5478 gfc_expr *po;
5479 gfc_ref **ref;
5481 po = gfc_get_expr ();
5482 po->expr_type = EXPR_VARIABLE;
5483 po->symtree = e->symtree;
5484 po->ref = gfc_copy_ref (e->ref);
5485 po->where = e->where;
5487 /* Remove PPC reference. */
5488 ref = &po->ref;
5489 while ((*ref)->next)
5490 ref = &(*ref)->next;
5491 gfc_free_ref_list (*ref);
5492 *ref = NULL;
5494 if (gfc_resolve_expr (po) == FAILURE)
5495 return NULL;
5497 return po;
5501 /* Update the actual arglist of a procedure pointer component to include the
5502 passed-object. */
5504 static gfc_try
5505 update_ppc_arglist (gfc_expr* e)
5507 gfc_expr* po;
5508 gfc_component *ppc;
5509 gfc_typebound_proc* tb;
5511 if (!gfc_is_proc_ptr_comp (e, &ppc))
5512 return FAILURE;
5514 tb = ppc->tb;
5516 if (tb->error)
5517 return FAILURE;
5518 else if (tb->nopass)
5519 return SUCCESS;
5521 po = extract_ppc_passed_object (e);
5522 if (!po)
5523 return FAILURE;
5525 /* F08:R739. */
5526 if (po->rank > 0)
5528 gfc_error ("Passed-object at %L must be scalar", &e->where);
5529 return FAILURE;
5532 /* F08:C611. */
5533 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5535 gfc_error ("Base object for procedure-pointer component call at %L is of"
5536 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
5537 return FAILURE;
5540 gcc_assert (tb->pass_arg_num > 0);
5541 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5542 tb->pass_arg_num,
5543 tb->pass_arg);
5545 return SUCCESS;
5549 /* Check that the object a TBP is called on is valid, i.e. it must not be
5550 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5552 static gfc_try
5553 check_typebound_baseobject (gfc_expr* e)
5555 gfc_expr* base;
5556 gfc_try return_value = FAILURE;
5558 base = extract_compcall_passed_object (e);
5559 if (!base)
5560 return FAILURE;
5562 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5564 /* F08:C611. */
5565 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5567 gfc_error ("Base object for type-bound procedure call at %L is of"
5568 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5569 goto cleanup;
5572 /* F08:C1230. If the procedure called is NOPASS,
5573 the base object must be scalar. */
5574 if (e->value.compcall.tbp->nopass && base->rank > 0)
5576 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5577 " be scalar", &e->where);
5578 goto cleanup;
5581 /* FIXME: Remove once PR 43214 is fixed (TBP with non-scalar PASS). */
5582 if (base->rank > 0)
5584 gfc_error ("Non-scalar base object at %L currently not implemented",
5585 &e->where);
5586 goto cleanup;
5589 return_value = SUCCESS;
5591 cleanup:
5592 gfc_free_expr (base);
5593 return return_value;
5597 /* Resolve a call to a type-bound procedure, either function or subroutine,
5598 statically from the data in an EXPR_COMPCALL expression. The adapted
5599 arglist and the target-procedure symtree are returned. */
5601 static gfc_try
5602 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5603 gfc_actual_arglist** actual)
5605 gcc_assert (e->expr_type == EXPR_COMPCALL);
5606 gcc_assert (!e->value.compcall.tbp->is_generic);
5608 /* Update the actual arglist for PASS. */
5609 if (update_compcall_arglist (e) == FAILURE)
5610 return FAILURE;
5612 *actual = e->value.compcall.actual;
5613 *target = e->value.compcall.tbp->u.specific;
5615 gfc_free_ref_list (e->ref);
5616 e->ref = NULL;
5617 e->value.compcall.actual = NULL;
5619 return SUCCESS;
5623 /* Get the ultimate declared type from an expression. In addition,
5624 return the last class/derived type reference and the copy of the
5625 reference list. */
5626 static gfc_symbol*
5627 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5628 gfc_expr *e)
5630 gfc_symbol *declared;
5631 gfc_ref *ref;
5633 declared = NULL;
5634 if (class_ref)
5635 *class_ref = NULL;
5636 if (new_ref)
5637 *new_ref = gfc_copy_ref (e->ref);
5639 for (ref = e->ref; ref; ref = ref->next)
5641 if (ref->type != REF_COMPONENT)
5642 continue;
5644 if (ref->u.c.component->ts.type == BT_CLASS
5645 || ref->u.c.component->ts.type == BT_DERIVED)
5647 declared = ref->u.c.component->ts.u.derived;
5648 if (class_ref)
5649 *class_ref = ref;
5653 if (declared == NULL)
5654 declared = e->symtree->n.sym->ts.u.derived;
5656 return declared;
5660 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5661 which of the specific bindings (if any) matches the arglist and transform
5662 the expression into a call of that binding. */
5664 static gfc_try
5665 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5667 gfc_typebound_proc* genproc;
5668 const char* genname;
5669 gfc_symtree *st;
5670 gfc_symbol *derived;
5672 gcc_assert (e->expr_type == EXPR_COMPCALL);
5673 genname = e->value.compcall.name;
5674 genproc = e->value.compcall.tbp;
5676 if (!genproc->is_generic)
5677 return SUCCESS;
5679 /* Try the bindings on this type and in the inheritance hierarchy. */
5680 for (; genproc; genproc = genproc->overridden)
5682 gfc_tbp_generic* g;
5684 gcc_assert (genproc->is_generic);
5685 for (g = genproc->u.generic; g; g = g->next)
5687 gfc_symbol* target;
5688 gfc_actual_arglist* args;
5689 bool matches;
5691 gcc_assert (g->specific);
5693 if (g->specific->error)
5694 continue;
5696 target = g->specific->u.specific->n.sym;
5698 /* Get the right arglist by handling PASS/NOPASS. */
5699 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5700 if (!g->specific->nopass)
5702 gfc_expr* po;
5703 po = extract_compcall_passed_object (e);
5704 if (!po)
5705 return FAILURE;
5707 gcc_assert (g->specific->pass_arg_num > 0);
5708 gcc_assert (!g->specific->error);
5709 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5710 g->specific->pass_arg);
5712 resolve_actual_arglist (args, target->attr.proc,
5713 is_external_proc (target) && !target->formal);
5715 /* Check if this arglist matches the formal. */
5716 matches = gfc_arglist_matches_symbol (&args, target);
5718 /* Clean up and break out of the loop if we've found it. */
5719 gfc_free_actual_arglist (args);
5720 if (matches)
5722 e->value.compcall.tbp = g->specific;
5723 genname = g->specific_st->name;
5724 /* Pass along the name for CLASS methods, where the vtab
5725 procedure pointer component has to be referenced. */
5726 if (name)
5727 *name = genname;
5728 goto success;
5733 /* Nothing matching found! */
5734 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5735 " '%s' at %L", genname, &e->where);
5736 return FAILURE;
5738 success:
5739 /* Make sure that we have the right specific instance for the name. */
5740 derived = get_declared_from_expr (NULL, NULL, e);
5742 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5743 if (st)
5744 e->value.compcall.tbp = st->n.tb;
5746 return SUCCESS;
5750 /* Resolve a call to a type-bound subroutine. */
5752 static gfc_try
5753 resolve_typebound_call (gfc_code* c, const char **name)
5755 gfc_actual_arglist* newactual;
5756 gfc_symtree* target;
5758 /* Check that's really a SUBROUTINE. */
5759 if (!c->expr1->value.compcall.tbp->subroutine)
5761 gfc_error ("'%s' at %L should be a SUBROUTINE",
5762 c->expr1->value.compcall.name, &c->loc);
5763 return FAILURE;
5766 if (check_typebound_baseobject (c->expr1) == FAILURE)
5767 return FAILURE;
5769 /* Pass along the name for CLASS methods, where the vtab
5770 procedure pointer component has to be referenced. */
5771 if (name)
5772 *name = c->expr1->value.compcall.name;
5774 if (resolve_typebound_generic_call (c->expr1, name) == FAILURE)
5775 return FAILURE;
5777 /* Transform into an ordinary EXEC_CALL for now. */
5779 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5780 return FAILURE;
5782 c->ext.actual = newactual;
5783 c->symtree = target;
5784 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5786 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5788 gfc_free_expr (c->expr1);
5789 c->expr1 = gfc_get_expr ();
5790 c->expr1->expr_type = EXPR_FUNCTION;
5791 c->expr1->symtree = target;
5792 c->expr1->where = c->loc;
5794 return resolve_call (c);
5798 /* Resolve a component-call expression. */
5799 static gfc_try
5800 resolve_compcall (gfc_expr* e, const char **name)
5802 gfc_actual_arglist* newactual;
5803 gfc_symtree* target;
5805 /* Check that's really a FUNCTION. */
5806 if (!e->value.compcall.tbp->function)
5808 gfc_error ("'%s' at %L should be a FUNCTION",
5809 e->value.compcall.name, &e->where);
5810 return FAILURE;
5813 /* These must not be assign-calls! */
5814 gcc_assert (!e->value.compcall.assign);
5816 if (check_typebound_baseobject (e) == FAILURE)
5817 return FAILURE;
5819 /* Pass along the name for CLASS methods, where the vtab
5820 procedure pointer component has to be referenced. */
5821 if (name)
5822 *name = e->value.compcall.name;
5824 if (resolve_typebound_generic_call (e, name) == FAILURE)
5825 return FAILURE;
5826 gcc_assert (!e->value.compcall.tbp->is_generic);
5828 /* Take the rank from the function's symbol. */
5829 if (e->value.compcall.tbp->u.specific->n.sym->as)
5830 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5832 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5833 arglist to the TBP's binding target. */
5835 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5836 return FAILURE;
5838 e->value.function.actual = newactual;
5839 e->value.function.name = NULL;
5840 e->value.function.esym = target->n.sym;
5841 e->value.function.isym = NULL;
5842 e->symtree = target;
5843 e->ts = target->n.sym->ts;
5844 e->expr_type = EXPR_FUNCTION;
5846 /* Resolution is not necessary if this is a class subroutine; this
5847 function only has to identify the specific proc. Resolution of
5848 the call will be done next in resolve_typebound_call. */
5849 return gfc_resolve_expr (e);
5854 /* Resolve a typebound function, or 'method'. First separate all
5855 the non-CLASS references by calling resolve_compcall directly. */
5857 static gfc_try
5858 resolve_typebound_function (gfc_expr* e)
5860 gfc_symbol *declared;
5861 gfc_component *c;
5862 gfc_ref *new_ref;
5863 gfc_ref *class_ref;
5864 gfc_symtree *st;
5865 const char *name;
5866 gfc_typespec ts;
5867 gfc_expr *expr;
5869 st = e->symtree;
5871 /* Deal with typebound operators for CLASS objects. */
5872 expr = e->value.compcall.base_object;
5873 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5875 /* Since the typebound operators are generic, we have to ensure
5876 that any delays in resolution are corrected and that the vtab
5877 is present. */
5878 ts = expr->ts;
5879 declared = ts.u.derived;
5880 c = gfc_find_component (declared, "_vptr", true, true);
5881 if (c->ts.u.derived == NULL)
5882 c->ts.u.derived = gfc_find_derived_vtab (declared);
5884 if (resolve_compcall (e, &name) == FAILURE)
5885 return FAILURE;
5887 /* Use the generic name if it is there. */
5888 name = name ? name : e->value.function.esym->name;
5889 e->symtree = expr->symtree;
5890 e->ref = gfc_copy_ref (expr->ref);
5891 gfc_add_vptr_component (e);
5892 gfc_add_component_ref (e, name);
5893 e->value.function.esym = NULL;
5894 return SUCCESS;
5897 if (st == NULL)
5898 return resolve_compcall (e, NULL);
5900 if (resolve_ref (e) == FAILURE)
5901 return FAILURE;
5903 /* Get the CLASS declared type. */
5904 declared = get_declared_from_expr (&class_ref, &new_ref, e);
5906 /* Weed out cases of the ultimate component being a derived type. */
5907 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5908 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5910 gfc_free_ref_list (new_ref);
5911 return resolve_compcall (e, NULL);
5914 c = gfc_find_component (declared, "_data", true, true);
5915 declared = c->ts.u.derived;
5917 /* Treat the call as if it is a typebound procedure, in order to roll
5918 out the correct name for the specific function. */
5919 if (resolve_compcall (e, &name) == FAILURE)
5920 return FAILURE;
5921 ts = e->ts;
5923 /* Then convert the expression to a procedure pointer component call. */
5924 e->value.function.esym = NULL;
5925 e->symtree = st;
5927 if (new_ref)
5928 e->ref = new_ref;
5930 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5931 gfc_add_vptr_component (e);
5932 gfc_add_component_ref (e, name);
5934 /* Recover the typespec for the expression. This is really only
5935 necessary for generic procedures, where the additional call
5936 to gfc_add_component_ref seems to throw the collection of the
5937 correct typespec. */
5938 e->ts = ts;
5939 return SUCCESS;
5942 /* Resolve a typebound subroutine, or 'method'. First separate all
5943 the non-CLASS references by calling resolve_typebound_call
5944 directly. */
5946 static gfc_try
5947 resolve_typebound_subroutine (gfc_code *code)
5949 gfc_symbol *declared;
5950 gfc_component *c;
5951 gfc_ref *new_ref;
5952 gfc_ref *class_ref;
5953 gfc_symtree *st;
5954 const char *name;
5955 gfc_typespec ts;
5956 gfc_expr *expr;
5958 st = code->expr1->symtree;
5960 /* Deal with typebound operators for CLASS objects. */
5961 expr = code->expr1->value.compcall.base_object;
5962 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
5964 /* Since the typebound operators are generic, we have to ensure
5965 that any delays in resolution are corrected and that the vtab
5966 is present. */
5967 declared = expr->ts.u.derived;
5968 c = gfc_find_component (declared, "_vptr", true, true);
5969 if (c->ts.u.derived == NULL)
5970 c->ts.u.derived = gfc_find_derived_vtab (declared);
5972 if (resolve_typebound_call (code, &name) == FAILURE)
5973 return FAILURE;
5975 /* Use the generic name if it is there. */
5976 name = name ? name : code->expr1->value.function.esym->name;
5977 code->expr1->symtree = expr->symtree;
5978 code->expr1->ref = gfc_copy_ref (expr->ref);
5979 gfc_add_vptr_component (code->expr1);
5980 gfc_add_component_ref (code->expr1, name);
5981 code->expr1->value.function.esym = NULL;
5982 return SUCCESS;
5985 if (st == NULL)
5986 return resolve_typebound_call (code, NULL);
5988 if (resolve_ref (code->expr1) == FAILURE)
5989 return FAILURE;
5991 /* Get the CLASS declared type. */
5992 get_declared_from_expr (&class_ref, &new_ref, code->expr1);
5994 /* Weed out cases of the ultimate component being a derived type. */
5995 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5996 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5998 gfc_free_ref_list (new_ref);
5999 return resolve_typebound_call (code, NULL);
6002 if (resolve_typebound_call (code, &name) == FAILURE)
6003 return FAILURE;
6004 ts = code->expr1->ts;
6006 /* Then convert the expression to a procedure pointer component call. */
6007 code->expr1->value.function.esym = NULL;
6008 code->expr1->symtree = st;
6010 if (new_ref)
6011 code->expr1->ref = new_ref;
6013 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6014 gfc_add_vptr_component (code->expr1);
6015 gfc_add_component_ref (code->expr1, name);
6017 /* Recover the typespec for the expression. This is really only
6018 necessary for generic procedures, where the additional call
6019 to gfc_add_component_ref seems to throw the collection of the
6020 correct typespec. */
6021 code->expr1->ts = ts;
6022 return SUCCESS;
6026 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6028 static gfc_try
6029 resolve_ppc_call (gfc_code* c)
6031 gfc_component *comp;
6032 bool b;
6034 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
6035 gcc_assert (b);
6037 c->resolved_sym = c->expr1->symtree->n.sym;
6038 c->expr1->expr_type = EXPR_VARIABLE;
6040 if (!comp->attr.subroutine)
6041 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6043 if (resolve_ref (c->expr1) == FAILURE)
6044 return FAILURE;
6046 if (update_ppc_arglist (c->expr1) == FAILURE)
6047 return FAILURE;
6049 c->ext.actual = c->expr1->value.compcall.actual;
6051 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6052 comp->formal == NULL) == FAILURE)
6053 return FAILURE;
6055 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6057 return SUCCESS;
6061 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6063 static gfc_try
6064 resolve_expr_ppc (gfc_expr* e)
6066 gfc_component *comp;
6067 bool b;
6069 b = gfc_is_proc_ptr_comp (e, &comp);
6070 gcc_assert (b);
6072 /* Convert to EXPR_FUNCTION. */
6073 e->expr_type = EXPR_FUNCTION;
6074 e->value.function.isym = NULL;
6075 e->value.function.actual = e->value.compcall.actual;
6076 e->ts = comp->ts;
6077 if (comp->as != NULL)
6078 e->rank = comp->as->rank;
6080 if (!comp->attr.function)
6081 gfc_add_function (&comp->attr, comp->name, &e->where);
6083 if (resolve_ref (e) == FAILURE)
6084 return FAILURE;
6086 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6087 comp->formal == NULL) == FAILURE)
6088 return FAILURE;
6090 if (update_ppc_arglist (e) == FAILURE)
6091 return FAILURE;
6093 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6095 return SUCCESS;
6099 static bool
6100 gfc_is_expandable_expr (gfc_expr *e)
6102 gfc_constructor *con;
6104 if (e->expr_type == EXPR_ARRAY)
6106 /* Traverse the constructor looking for variables that are flavor
6107 parameter. Parameters must be expanded since they are fully used at
6108 compile time. */
6109 con = gfc_constructor_first (e->value.constructor);
6110 for (; con; con = gfc_constructor_next (con))
6112 if (con->expr->expr_type == EXPR_VARIABLE
6113 && con->expr->symtree
6114 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6115 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6116 return true;
6117 if (con->expr->expr_type == EXPR_ARRAY
6118 && gfc_is_expandable_expr (con->expr))
6119 return true;
6123 return false;
6126 /* Resolve an expression. That is, make sure that types of operands agree
6127 with their operators, intrinsic operators are converted to function calls
6128 for overloaded types and unresolved function references are resolved. */
6130 gfc_try
6131 gfc_resolve_expr (gfc_expr *e)
6133 gfc_try t;
6134 bool inquiry_save;
6136 if (e == NULL)
6137 return SUCCESS;
6139 /* inquiry_argument only applies to variables. */
6140 inquiry_save = inquiry_argument;
6141 if (e->expr_type != EXPR_VARIABLE)
6142 inquiry_argument = false;
6144 switch (e->expr_type)
6146 case EXPR_OP:
6147 t = resolve_operator (e);
6148 break;
6150 case EXPR_FUNCTION:
6151 case EXPR_VARIABLE:
6153 if (check_host_association (e))
6154 t = resolve_function (e);
6155 else
6157 t = resolve_variable (e);
6158 if (t == SUCCESS)
6159 expression_rank (e);
6162 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6163 && e->ref->type != REF_SUBSTRING)
6164 gfc_resolve_substring_charlen (e);
6166 break;
6168 case EXPR_COMPCALL:
6169 t = resolve_typebound_function (e);
6170 break;
6172 case EXPR_SUBSTRING:
6173 t = resolve_ref (e);
6174 break;
6176 case EXPR_CONSTANT:
6177 case EXPR_NULL:
6178 t = SUCCESS;
6179 break;
6181 case EXPR_PPC:
6182 t = resolve_expr_ppc (e);
6183 break;
6185 case EXPR_ARRAY:
6186 t = FAILURE;
6187 if (resolve_ref (e) == FAILURE)
6188 break;
6190 t = gfc_resolve_array_constructor (e);
6191 /* Also try to expand a constructor. */
6192 if (t == SUCCESS)
6194 expression_rank (e);
6195 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6196 gfc_expand_constructor (e, false);
6199 /* This provides the opportunity for the length of constructors with
6200 character valued function elements to propagate the string length
6201 to the expression. */
6202 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
6204 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6205 here rather then add a duplicate test for it above. */
6206 gfc_expand_constructor (e, false);
6207 t = gfc_resolve_character_array_constructor (e);
6210 break;
6212 case EXPR_STRUCTURE:
6213 t = resolve_ref (e);
6214 if (t == FAILURE)
6215 break;
6217 t = resolve_structure_cons (e, 0);
6218 if (t == FAILURE)
6219 break;
6221 t = gfc_simplify_expr (e, 0);
6222 break;
6224 default:
6225 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6228 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
6229 fixup_charlen (e);
6231 inquiry_argument = inquiry_save;
6233 return t;
6237 /* Resolve an expression from an iterator. They must be scalar and have
6238 INTEGER or (optionally) REAL type. */
6240 static gfc_try
6241 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6242 const char *name_msgid)
6244 if (gfc_resolve_expr (expr) == FAILURE)
6245 return FAILURE;
6247 if (expr->rank != 0)
6249 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6250 return FAILURE;
6253 if (expr->ts.type != BT_INTEGER)
6255 if (expr->ts.type == BT_REAL)
6257 if (real_ok)
6258 return gfc_notify_std (GFC_STD_F95_DEL,
6259 "Deleted feature: %s at %L must be integer",
6260 _(name_msgid), &expr->where);
6261 else
6263 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6264 &expr->where);
6265 return FAILURE;
6268 else
6270 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6271 return FAILURE;
6274 return SUCCESS;
6278 /* Resolve the expressions in an iterator structure. If REAL_OK is
6279 false allow only INTEGER type iterators, otherwise allow REAL types. */
6281 gfc_try
6282 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
6284 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
6285 == FAILURE)
6286 return FAILURE;
6288 if (gfc_check_vardef_context (iter->var, false, false, _("iterator variable"))
6289 == FAILURE)
6290 return FAILURE;
6292 if (gfc_resolve_iterator_expr (iter->start, real_ok,
6293 "Start expression in DO loop") == FAILURE)
6294 return FAILURE;
6296 if (gfc_resolve_iterator_expr (iter->end, real_ok,
6297 "End expression in DO loop") == FAILURE)
6298 return FAILURE;
6300 if (gfc_resolve_iterator_expr (iter->step, real_ok,
6301 "Step expression in DO loop") == FAILURE)
6302 return FAILURE;
6304 if (iter->step->expr_type == EXPR_CONSTANT)
6306 if ((iter->step->ts.type == BT_INTEGER
6307 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6308 || (iter->step->ts.type == BT_REAL
6309 && mpfr_sgn (iter->step->value.real) == 0))
6311 gfc_error ("Step expression in DO loop at %L cannot be zero",
6312 &iter->step->where);
6313 return FAILURE;
6317 /* Convert start, end, and step to the same type as var. */
6318 if (iter->start->ts.kind != iter->var->ts.kind
6319 || iter->start->ts.type != iter->var->ts.type)
6320 gfc_convert_type (iter->start, &iter->var->ts, 2);
6322 if (iter->end->ts.kind != iter->var->ts.kind
6323 || iter->end->ts.type != iter->var->ts.type)
6324 gfc_convert_type (iter->end, &iter->var->ts, 2);
6326 if (iter->step->ts.kind != iter->var->ts.kind
6327 || iter->step->ts.type != iter->var->ts.type)
6328 gfc_convert_type (iter->step, &iter->var->ts, 2);
6330 if (iter->start->expr_type == EXPR_CONSTANT
6331 && iter->end->expr_type == EXPR_CONSTANT
6332 && iter->step->expr_type == EXPR_CONSTANT)
6334 int sgn, cmp;
6335 if (iter->start->ts.type == BT_INTEGER)
6337 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6338 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6340 else
6342 sgn = mpfr_sgn (iter->step->value.real);
6343 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6345 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
6346 gfc_warning ("DO loop at %L will be executed zero times",
6347 &iter->step->where);
6350 return SUCCESS;
6354 /* Traversal function for find_forall_index. f == 2 signals that
6355 that variable itself is not to be checked - only the references. */
6357 static bool
6358 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6360 if (expr->expr_type != EXPR_VARIABLE)
6361 return false;
6363 /* A scalar assignment */
6364 if (!expr->ref || *f == 1)
6366 if (expr->symtree->n.sym == sym)
6367 return true;
6368 else
6369 return false;
6372 if (*f == 2)
6373 *f = 1;
6374 return false;
6378 /* Check whether the FORALL index appears in the expression or not.
6379 Returns SUCCESS if SYM is found in EXPR. */
6381 gfc_try
6382 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6384 if (gfc_traverse_expr (expr, sym, forall_index, f))
6385 return SUCCESS;
6386 else
6387 return FAILURE;
6391 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6392 to be a scalar INTEGER variable. The subscripts and stride are scalar
6393 INTEGERs, and if stride is a constant it must be nonzero.
6394 Furthermore "A subscript or stride in a forall-triplet-spec shall
6395 not contain a reference to any index-name in the
6396 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6398 static void
6399 resolve_forall_iterators (gfc_forall_iterator *it)
6401 gfc_forall_iterator *iter, *iter2;
6403 for (iter = it; iter; iter = iter->next)
6405 if (gfc_resolve_expr (iter->var) == SUCCESS
6406 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6407 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6408 &iter->var->where);
6410 if (gfc_resolve_expr (iter->start) == SUCCESS
6411 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6412 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6413 &iter->start->where);
6414 if (iter->var->ts.kind != iter->start->ts.kind)
6415 gfc_convert_type (iter->start, &iter->var->ts, 2);
6417 if (gfc_resolve_expr (iter->end) == SUCCESS
6418 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6419 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6420 &iter->end->where);
6421 if (iter->var->ts.kind != iter->end->ts.kind)
6422 gfc_convert_type (iter->end, &iter->var->ts, 2);
6424 if (gfc_resolve_expr (iter->stride) == SUCCESS)
6426 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6427 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6428 &iter->stride->where, "INTEGER");
6430 if (iter->stride->expr_type == EXPR_CONSTANT
6431 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
6432 gfc_error ("FORALL stride expression at %L cannot be zero",
6433 &iter->stride->where);
6435 if (iter->var->ts.kind != iter->stride->ts.kind)
6436 gfc_convert_type (iter->stride, &iter->var->ts, 2);
6439 for (iter = it; iter; iter = iter->next)
6440 for (iter2 = iter; iter2; iter2 = iter2->next)
6442 if (find_forall_index (iter2->start,
6443 iter->var->symtree->n.sym, 0) == SUCCESS
6444 || find_forall_index (iter2->end,
6445 iter->var->symtree->n.sym, 0) == SUCCESS
6446 || find_forall_index (iter2->stride,
6447 iter->var->symtree->n.sym, 0) == SUCCESS)
6448 gfc_error ("FORALL index '%s' may not appear in triplet "
6449 "specification at %L", iter->var->symtree->name,
6450 &iter2->start->where);
6455 /* Given a pointer to a symbol that is a derived type, see if it's
6456 inaccessible, i.e. if it's defined in another module and the components are
6457 PRIVATE. The search is recursive if necessary. Returns zero if no
6458 inaccessible components are found, nonzero otherwise. */
6460 static int
6461 derived_inaccessible (gfc_symbol *sym)
6463 gfc_component *c;
6465 if (sym->attr.use_assoc && sym->attr.private_comp)
6466 return 1;
6468 for (c = sym->components; c; c = c->next)
6470 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6471 return 1;
6474 return 0;
6478 /* Resolve the argument of a deallocate expression. The expression must be
6479 a pointer or a full array. */
6481 static gfc_try
6482 resolve_deallocate_expr (gfc_expr *e)
6484 symbol_attribute attr;
6485 int allocatable, pointer;
6486 gfc_ref *ref;
6487 gfc_symbol *sym;
6488 gfc_component *c;
6490 if (gfc_resolve_expr (e) == FAILURE)
6491 return FAILURE;
6493 if (e->expr_type != EXPR_VARIABLE)
6494 goto bad;
6496 sym = e->symtree->n.sym;
6498 if (sym->ts.type == BT_CLASS)
6500 allocatable = CLASS_DATA (sym)->attr.allocatable;
6501 pointer = CLASS_DATA (sym)->attr.class_pointer;
6503 else
6505 allocatable = sym->attr.allocatable;
6506 pointer = sym->attr.pointer;
6508 for (ref = e->ref; ref; ref = ref->next)
6510 switch (ref->type)
6512 case REF_ARRAY:
6513 if (ref->u.ar.type != AR_FULL
6514 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6515 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6516 allocatable = 0;
6517 break;
6519 case REF_COMPONENT:
6520 c = ref->u.c.component;
6521 if (c->ts.type == BT_CLASS)
6523 allocatable = CLASS_DATA (c)->attr.allocatable;
6524 pointer = CLASS_DATA (c)->attr.class_pointer;
6526 else
6528 allocatable = c->attr.allocatable;
6529 pointer = c->attr.pointer;
6531 break;
6533 case REF_SUBSTRING:
6534 allocatable = 0;
6535 break;
6539 attr = gfc_expr_attr (e);
6541 if (allocatable == 0 && attr.pointer == 0)
6543 bad:
6544 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6545 &e->where);
6546 return FAILURE;
6549 /* F2008, C644. */
6550 if (gfc_is_coindexed (e))
6552 gfc_error ("Coindexed allocatable object at %L", &e->where);
6553 return FAILURE;
6556 if (pointer
6557 && gfc_check_vardef_context (e, true, true, _("DEALLOCATE object"))
6558 == FAILURE)
6559 return FAILURE;
6560 if (gfc_check_vardef_context (e, false, true, _("DEALLOCATE object"))
6561 == FAILURE)
6562 return FAILURE;
6564 return SUCCESS;
6568 /* Returns true if the expression e contains a reference to the symbol sym. */
6569 static bool
6570 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6572 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6573 return true;
6575 return false;
6578 bool
6579 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6581 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6585 /* Given the expression node e for an allocatable/pointer of derived type to be
6586 allocated, get the expression node to be initialized afterwards (needed for
6587 derived types with default initializers, and derived types with allocatable
6588 components that need nullification.) */
6590 gfc_expr *
6591 gfc_expr_to_initialize (gfc_expr *e)
6593 gfc_expr *result;
6594 gfc_ref *ref;
6595 int i;
6597 result = gfc_copy_expr (e);
6599 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6600 for (ref = result->ref; ref; ref = ref->next)
6601 if (ref->type == REF_ARRAY && ref->next == NULL)
6603 ref->u.ar.type = AR_FULL;
6605 for (i = 0; i < ref->u.ar.dimen; i++)
6606 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6608 break;
6611 gfc_free_shape (&result->shape, result->rank);
6613 /* Recalculate rank, shape, etc. */
6614 gfc_resolve_expr (result);
6615 return result;
6619 /* If the last ref of an expression is an array ref, return a copy of the
6620 expression with that one removed. Otherwise, a copy of the original
6621 expression. This is used for allocate-expressions and pointer assignment
6622 LHS, where there may be an array specification that needs to be stripped
6623 off when using gfc_check_vardef_context. */
6625 static gfc_expr*
6626 remove_last_array_ref (gfc_expr* e)
6628 gfc_expr* e2;
6629 gfc_ref** r;
6631 e2 = gfc_copy_expr (e);
6632 for (r = &e2->ref; *r; r = &(*r)->next)
6633 if ((*r)->type == REF_ARRAY && !(*r)->next)
6635 gfc_free_ref_list (*r);
6636 *r = NULL;
6637 break;
6640 return e2;
6644 /* Used in resolve_allocate_expr to check that a allocation-object and
6645 a source-expr are conformable. This does not catch all possible
6646 cases; in particular a runtime checking is needed. */
6648 static gfc_try
6649 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6651 gfc_ref *tail;
6652 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6654 /* First compare rank. */
6655 if (tail && e1->rank != tail->u.ar.as->rank)
6657 gfc_error ("Source-expr at %L must be scalar or have the "
6658 "same rank as the allocate-object at %L",
6659 &e1->where, &e2->where);
6660 return FAILURE;
6663 if (e1->shape)
6665 int i;
6666 mpz_t s;
6668 mpz_init (s);
6670 for (i = 0; i < e1->rank; i++)
6672 if (tail->u.ar.end[i])
6674 mpz_set (s, tail->u.ar.end[i]->value.integer);
6675 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6676 mpz_add_ui (s, s, 1);
6678 else
6680 mpz_set (s, tail->u.ar.start[i]->value.integer);
6683 if (mpz_cmp (e1->shape[i], s) != 0)
6685 gfc_error ("Source-expr at %L and allocate-object at %L must "
6686 "have the same shape", &e1->where, &e2->where);
6687 mpz_clear (s);
6688 return FAILURE;
6692 mpz_clear (s);
6695 return SUCCESS;
6699 /* Resolve the expression in an ALLOCATE statement, doing the additional
6700 checks to see whether the expression is OK or not. The expression must
6701 have a trailing array reference that gives the size of the array. */
6703 static gfc_try
6704 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6706 int i, pointer, allocatable, dimension, is_abstract;
6707 int codimension;
6708 bool coindexed;
6709 symbol_attribute attr;
6710 gfc_ref *ref, *ref2;
6711 gfc_expr *e2;
6712 gfc_array_ref *ar;
6713 gfc_symbol *sym = NULL;
6714 gfc_alloc *a;
6715 gfc_component *c;
6716 gfc_try t;
6718 /* Mark the ultimost array component as being in allocate to allow DIMEN_STAR
6719 checking of coarrays. */
6720 for (ref = e->ref; ref; ref = ref->next)
6721 if (ref->next == NULL)
6722 break;
6724 if (ref && ref->type == REF_ARRAY)
6725 ref->u.ar.in_allocate = true;
6727 if (gfc_resolve_expr (e) == FAILURE)
6728 goto failure;
6730 /* Make sure the expression is allocatable or a pointer. If it is
6731 pointer, the next-to-last reference must be a pointer. */
6733 ref2 = NULL;
6734 if (e->symtree)
6735 sym = e->symtree->n.sym;
6737 /* Check whether ultimate component is abstract and CLASS. */
6738 is_abstract = 0;
6740 if (e->expr_type != EXPR_VARIABLE)
6742 allocatable = 0;
6743 attr = gfc_expr_attr (e);
6744 pointer = attr.pointer;
6745 dimension = attr.dimension;
6746 codimension = attr.codimension;
6748 else
6750 if (sym->ts.type == BT_CLASS)
6752 allocatable = CLASS_DATA (sym)->attr.allocatable;
6753 pointer = CLASS_DATA (sym)->attr.class_pointer;
6754 dimension = CLASS_DATA (sym)->attr.dimension;
6755 codimension = CLASS_DATA (sym)->attr.codimension;
6756 is_abstract = CLASS_DATA (sym)->attr.abstract;
6758 else
6760 allocatable = sym->attr.allocatable;
6761 pointer = sym->attr.pointer;
6762 dimension = sym->attr.dimension;
6763 codimension = sym->attr.codimension;
6766 coindexed = false;
6768 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6770 switch (ref->type)
6772 case REF_ARRAY:
6773 if (ref->u.ar.codimen > 0)
6775 int n;
6776 for (n = ref->u.ar.dimen;
6777 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6778 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6780 coindexed = true;
6781 break;
6785 if (ref->next != NULL)
6786 pointer = 0;
6787 break;
6789 case REF_COMPONENT:
6790 /* F2008, C644. */
6791 if (coindexed)
6793 gfc_error ("Coindexed allocatable object at %L",
6794 &e->where);
6795 goto failure;
6798 c = ref->u.c.component;
6799 if (c->ts.type == BT_CLASS)
6801 allocatable = CLASS_DATA (c)->attr.allocatable;
6802 pointer = CLASS_DATA (c)->attr.class_pointer;
6803 dimension = CLASS_DATA (c)->attr.dimension;
6804 codimension = CLASS_DATA (c)->attr.codimension;
6805 is_abstract = CLASS_DATA (c)->attr.abstract;
6807 else
6809 allocatable = c->attr.allocatable;
6810 pointer = c->attr.pointer;
6811 dimension = c->attr.dimension;
6812 codimension = c->attr.codimension;
6813 is_abstract = c->attr.abstract;
6815 break;
6817 case REF_SUBSTRING:
6818 allocatable = 0;
6819 pointer = 0;
6820 break;
6825 if (allocatable == 0 && pointer == 0)
6827 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6828 &e->where);
6829 goto failure;
6832 /* Some checks for the SOURCE tag. */
6833 if (code->expr3)
6835 /* Check F03:C631. */
6836 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6838 gfc_error ("Type of entity at %L is type incompatible with "
6839 "source-expr at %L", &e->where, &code->expr3->where);
6840 goto failure;
6843 /* Check F03:C632 and restriction following Note 6.18. */
6844 if (code->expr3->rank > 0
6845 && conformable_arrays (code->expr3, e) == FAILURE)
6846 goto failure;
6848 /* Check F03:C633. */
6849 if (code->expr3->ts.kind != e->ts.kind)
6851 gfc_error ("The allocate-object at %L and the source-expr at %L "
6852 "shall have the same kind type parameter",
6853 &e->where, &code->expr3->where);
6854 goto failure;
6857 /* Check F2008, C642. */
6858 if (code->expr3->ts.type == BT_DERIVED
6859 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
6860 || (code->expr3->ts.u.derived->from_intmod
6861 == INTMOD_ISO_FORTRAN_ENV
6862 && code->expr3->ts.u.derived->intmod_sym_id
6863 == ISOFORTRAN_LOCK_TYPE)))
6865 gfc_error ("The source-expr at %L shall neither be of type "
6866 "LOCK_TYPE nor have a LOCK_TYPE component if "
6867 "allocate-object at %L is a coarray",
6868 &code->expr3->where, &e->where);
6869 goto failure;
6873 /* Check F08:C629. */
6874 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6875 && !code->expr3)
6877 gcc_assert (e->ts.type == BT_CLASS);
6878 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6879 "type-spec or source-expr", sym->name, &e->where);
6880 goto failure;
6883 /* In the variable definition context checks, gfc_expr_attr is used
6884 on the expression. This is fooled by the array specification
6885 present in e, thus we have to eliminate that one temporarily. */
6886 e2 = remove_last_array_ref (e);
6887 t = SUCCESS;
6888 if (t == SUCCESS && pointer)
6889 t = gfc_check_vardef_context (e2, true, true, _("ALLOCATE object"));
6890 if (t == SUCCESS)
6891 t = gfc_check_vardef_context (e2, false, true, _("ALLOCATE object"));
6892 gfc_free_expr (e2);
6893 if (t == FAILURE)
6894 goto failure;
6896 if (!code->expr3)
6898 /* Set up default initializer if needed. */
6899 gfc_typespec ts;
6900 gfc_expr *init_e;
6902 if (code->ext.alloc.ts.type == BT_DERIVED)
6903 ts = code->ext.alloc.ts;
6904 else
6905 ts = e->ts;
6907 if (ts.type == BT_CLASS)
6908 ts = ts.u.derived->components->ts;
6910 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
6912 gfc_code *init_st = gfc_get_code ();
6913 init_st->loc = code->loc;
6914 init_st->op = EXEC_INIT_ASSIGN;
6915 init_st->expr1 = gfc_expr_to_initialize (e);
6916 init_st->expr2 = init_e;
6917 init_st->next = code->next;
6918 code->next = init_st;
6921 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
6923 /* Default initialization via MOLD (non-polymorphic). */
6924 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
6925 gfc_resolve_expr (rhs);
6926 gfc_free_expr (code->expr3);
6927 code->expr3 = rhs;
6930 if (e->ts.type == BT_CLASS)
6932 /* Make sure the vtab symbol is present when
6933 the module variables are generated. */
6934 gfc_typespec ts = e->ts;
6935 if (code->expr3)
6936 ts = code->expr3->ts;
6937 else if (code->ext.alloc.ts.type == BT_DERIVED)
6938 ts = code->ext.alloc.ts;
6939 gfc_find_derived_vtab (ts.u.derived);
6942 if (dimension == 0 && codimension == 0)
6943 goto success;
6945 /* Make sure the last reference node is an array specifiction. */
6947 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6948 || (dimension && ref2->u.ar.dimen == 0))
6950 gfc_error ("Array specification required in ALLOCATE statement "
6951 "at %L", &e->where);
6952 goto failure;
6955 /* Make sure that the array section reference makes sense in the
6956 context of an ALLOCATE specification. */
6958 ar = &ref2->u.ar;
6960 if (codimension)
6961 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
6962 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
6964 gfc_error ("Coarray specification required in ALLOCATE statement "
6965 "at %L", &e->where);
6966 goto failure;
6969 for (i = 0; i < ar->dimen; i++)
6971 if (ref2->u.ar.type == AR_ELEMENT)
6972 goto check_symbols;
6974 switch (ar->dimen_type[i])
6976 case DIMEN_ELEMENT:
6977 break;
6979 case DIMEN_RANGE:
6980 if (ar->start[i] != NULL
6981 && ar->end[i] != NULL
6982 && ar->stride[i] == NULL)
6983 break;
6985 /* Fall Through... */
6987 case DIMEN_UNKNOWN:
6988 case DIMEN_VECTOR:
6989 case DIMEN_STAR:
6990 case DIMEN_THIS_IMAGE:
6991 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6992 &e->where);
6993 goto failure;
6996 check_symbols:
6997 for (a = code->ext.alloc.list; a; a = a->next)
6999 sym = a->expr->symtree->n.sym;
7001 /* TODO - check derived type components. */
7002 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
7003 continue;
7005 if ((ar->start[i] != NULL
7006 && gfc_find_sym_in_expr (sym, ar->start[i]))
7007 || (ar->end[i] != NULL
7008 && gfc_find_sym_in_expr (sym, ar->end[i])))
7010 gfc_error ("'%s' must not appear in the array specification at "
7011 "%L in the same ALLOCATE statement where it is "
7012 "itself allocated", sym->name, &ar->where);
7013 goto failure;
7018 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7020 if (ar->dimen_type[i] == DIMEN_ELEMENT
7021 || ar->dimen_type[i] == DIMEN_RANGE)
7023 if (i == (ar->dimen + ar->codimen - 1))
7025 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7026 "statement at %L", &e->where);
7027 goto failure;
7029 break;
7032 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7033 && ar->stride[i] == NULL)
7034 break;
7036 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7037 &e->where);
7038 goto failure;
7041 success:
7042 return SUCCESS;
7044 failure:
7045 return FAILURE;
7048 static void
7049 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7051 gfc_expr *stat, *errmsg, *pe, *qe;
7052 gfc_alloc *a, *p, *q;
7054 stat = code->expr1;
7055 errmsg = code->expr2;
7057 /* Check the stat variable. */
7058 if (stat)
7060 gfc_check_vardef_context (stat, false, false, _("STAT variable"));
7062 if ((stat->ts.type != BT_INTEGER
7063 && !(stat->ref && (stat->ref->type == REF_ARRAY
7064 || stat->ref->type == REF_COMPONENT)))
7065 || stat->rank > 0)
7066 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7067 "variable", &stat->where);
7069 for (p = code->ext.alloc.list; p; p = p->next)
7070 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7072 gfc_ref *ref1, *ref2;
7073 bool found = true;
7075 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7076 ref1 = ref1->next, ref2 = ref2->next)
7078 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7079 continue;
7080 if (ref1->u.c.component->name != ref2->u.c.component->name)
7082 found = false;
7083 break;
7087 if (found)
7089 gfc_error ("Stat-variable at %L shall not be %sd within "
7090 "the same %s statement", &stat->where, fcn, fcn);
7091 break;
7096 /* Check the errmsg variable. */
7097 if (errmsg)
7099 if (!stat)
7100 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7101 &errmsg->where);
7103 gfc_check_vardef_context (errmsg, false, false, _("ERRMSG variable"));
7105 if ((errmsg->ts.type != BT_CHARACTER
7106 && !(errmsg->ref
7107 && (errmsg->ref->type == REF_ARRAY
7108 || errmsg->ref->type == REF_COMPONENT)))
7109 || errmsg->rank > 0 )
7110 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7111 "variable", &errmsg->where);
7113 for (p = code->ext.alloc.list; p; p = p->next)
7114 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7116 gfc_ref *ref1, *ref2;
7117 bool found = true;
7119 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7120 ref1 = ref1->next, ref2 = ref2->next)
7122 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7123 continue;
7124 if (ref1->u.c.component->name != ref2->u.c.component->name)
7126 found = false;
7127 break;
7131 if (found)
7133 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7134 "the same %s statement", &errmsg->where, fcn, fcn);
7135 break;
7140 /* Check that an allocate-object appears only once in the statement.
7141 FIXME: Checking derived types is disabled. */
7142 for (p = code->ext.alloc.list; p; p = p->next)
7144 pe = p->expr;
7145 for (q = p->next; q; q = q->next)
7147 qe = q->expr;
7148 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7150 /* This is a potential collision. */
7151 gfc_ref *pr = pe->ref;
7152 gfc_ref *qr = qe->ref;
7154 /* Follow the references until
7155 a) They start to differ, in which case there is no error;
7156 you can deallocate a%b and a%c in a single statement
7157 b) Both of them stop, which is an error
7158 c) One of them stops, which is also an error. */
7159 while (1)
7161 if (pr == NULL && qr == NULL)
7163 gfc_error ("Allocate-object at %L also appears at %L",
7164 &pe->where, &qe->where);
7165 break;
7167 else if (pr != NULL && qr == NULL)
7169 gfc_error ("Allocate-object at %L is subobject of"
7170 " object at %L", &pe->where, &qe->where);
7171 break;
7173 else if (pr == NULL && qr != NULL)
7175 gfc_error ("Allocate-object at %L is subobject of"
7176 " object at %L", &qe->where, &pe->where);
7177 break;
7179 /* Here, pr != NULL && qr != NULL */
7180 gcc_assert(pr->type == qr->type);
7181 if (pr->type == REF_ARRAY)
7183 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7184 which are legal. */
7185 gcc_assert (qr->type == REF_ARRAY);
7187 if (pr->next && qr->next)
7189 gfc_array_ref *par = &(pr->u.ar);
7190 gfc_array_ref *qar = &(qr->u.ar);
7191 if (gfc_dep_compare_expr (par->start[0],
7192 qar->start[0]) != 0)
7193 break;
7196 else
7198 if (pr->u.c.component->name != qr->u.c.component->name)
7199 break;
7202 pr = pr->next;
7203 qr = qr->next;
7209 if (strcmp (fcn, "ALLOCATE") == 0)
7211 for (a = code->ext.alloc.list; a; a = a->next)
7212 resolve_allocate_expr (a->expr, code);
7214 else
7216 for (a = code->ext.alloc.list; a; a = a->next)
7217 resolve_deallocate_expr (a->expr);
7222 /************ SELECT CASE resolution subroutines ************/
7224 /* Callback function for our mergesort variant. Determines interval
7225 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7226 op1 > op2. Assumes we're not dealing with the default case.
7227 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7228 There are nine situations to check. */
7230 static int
7231 compare_cases (const gfc_case *op1, const gfc_case *op2)
7233 int retval;
7235 if (op1->low == NULL) /* op1 = (:L) */
7237 /* op2 = (:N), so overlap. */
7238 retval = 0;
7239 /* op2 = (M:) or (M:N), L < M */
7240 if (op2->low != NULL
7241 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7242 retval = -1;
7244 else if (op1->high == NULL) /* op1 = (K:) */
7246 /* op2 = (M:), so overlap. */
7247 retval = 0;
7248 /* op2 = (:N) or (M:N), K > N */
7249 if (op2->high != NULL
7250 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7251 retval = 1;
7253 else /* op1 = (K:L) */
7255 if (op2->low == NULL) /* op2 = (:N), K > N */
7256 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7257 ? 1 : 0;
7258 else if (op2->high == NULL) /* op2 = (M:), L < M */
7259 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7260 ? -1 : 0;
7261 else /* op2 = (M:N) */
7263 retval = 0;
7264 /* L < M */
7265 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7266 retval = -1;
7267 /* K > N */
7268 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7269 retval = 1;
7273 return retval;
7277 /* Merge-sort a double linked case list, detecting overlap in the
7278 process. LIST is the head of the double linked case list before it
7279 is sorted. Returns the head of the sorted list if we don't see any
7280 overlap, or NULL otherwise. */
7282 static gfc_case *
7283 check_case_overlap (gfc_case *list)
7285 gfc_case *p, *q, *e, *tail;
7286 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7288 /* If the passed list was empty, return immediately. */
7289 if (!list)
7290 return NULL;
7292 overlap_seen = 0;
7293 insize = 1;
7295 /* Loop unconditionally. The only exit from this loop is a return
7296 statement, when we've finished sorting the case list. */
7297 for (;;)
7299 p = list;
7300 list = NULL;
7301 tail = NULL;
7303 /* Count the number of merges we do in this pass. */
7304 nmerges = 0;
7306 /* Loop while there exists a merge to be done. */
7307 while (p)
7309 int i;
7311 /* Count this merge. */
7312 nmerges++;
7314 /* Cut the list in two pieces by stepping INSIZE places
7315 forward in the list, starting from P. */
7316 psize = 0;
7317 q = p;
7318 for (i = 0; i < insize; i++)
7320 psize++;
7321 q = q->right;
7322 if (!q)
7323 break;
7325 qsize = insize;
7327 /* Now we have two lists. Merge them! */
7328 while (psize > 0 || (qsize > 0 && q != NULL))
7330 /* See from which the next case to merge comes from. */
7331 if (psize == 0)
7333 /* P is empty so the next case must come from Q. */
7334 e = q;
7335 q = q->right;
7336 qsize--;
7338 else if (qsize == 0 || q == NULL)
7340 /* Q is empty. */
7341 e = p;
7342 p = p->right;
7343 psize--;
7345 else
7347 cmp = compare_cases (p, q);
7348 if (cmp < 0)
7350 /* The whole case range for P is less than the
7351 one for Q. */
7352 e = p;
7353 p = p->right;
7354 psize--;
7356 else if (cmp > 0)
7358 /* The whole case range for Q is greater than
7359 the case range for P. */
7360 e = q;
7361 q = q->right;
7362 qsize--;
7364 else
7366 /* The cases overlap, or they are the same
7367 element in the list. Either way, we must
7368 issue an error and get the next case from P. */
7369 /* FIXME: Sort P and Q by line number. */
7370 gfc_error ("CASE label at %L overlaps with CASE "
7371 "label at %L", &p->where, &q->where);
7372 overlap_seen = 1;
7373 e = p;
7374 p = p->right;
7375 psize--;
7379 /* Add the next element to the merged list. */
7380 if (tail)
7381 tail->right = e;
7382 else
7383 list = e;
7384 e->left = tail;
7385 tail = e;
7388 /* P has now stepped INSIZE places along, and so has Q. So
7389 they're the same. */
7390 p = q;
7392 tail->right = NULL;
7394 /* If we have done only one merge or none at all, we've
7395 finished sorting the cases. */
7396 if (nmerges <= 1)
7398 if (!overlap_seen)
7399 return list;
7400 else
7401 return NULL;
7404 /* Otherwise repeat, merging lists twice the size. */
7405 insize *= 2;
7410 /* Check to see if an expression is suitable for use in a CASE statement.
7411 Makes sure that all case expressions are scalar constants of the same
7412 type. Return FAILURE if anything is wrong. */
7414 static gfc_try
7415 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7417 if (e == NULL) return SUCCESS;
7419 if (e->ts.type != case_expr->ts.type)
7421 gfc_error ("Expression in CASE statement at %L must be of type %s",
7422 &e->where, gfc_basic_typename (case_expr->ts.type));
7423 return FAILURE;
7426 /* C805 (R808) For a given case-construct, each case-value shall be of
7427 the same type as case-expr. For character type, length differences
7428 are allowed, but the kind type parameters shall be the same. */
7430 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7432 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7433 &e->where, case_expr->ts.kind);
7434 return FAILURE;
7437 /* Convert the case value kind to that of case expression kind,
7438 if needed */
7440 if (e->ts.kind != case_expr->ts.kind)
7441 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7443 if (e->rank != 0)
7445 gfc_error ("Expression in CASE statement at %L must be scalar",
7446 &e->where);
7447 return FAILURE;
7450 return SUCCESS;
7454 /* Given a completely parsed select statement, we:
7456 - Validate all expressions and code within the SELECT.
7457 - Make sure that the selection expression is not of the wrong type.
7458 - Make sure that no case ranges overlap.
7459 - Eliminate unreachable cases and unreachable code resulting from
7460 removing case labels.
7462 The standard does allow unreachable cases, e.g. CASE (5:3). But
7463 they are a hassle for code generation, and to prevent that, we just
7464 cut them out here. This is not necessary for overlapping cases
7465 because they are illegal and we never even try to generate code.
7467 We have the additional caveat that a SELECT construct could have
7468 been a computed GOTO in the source code. Fortunately we can fairly
7469 easily work around that here: The case_expr for a "real" SELECT CASE
7470 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7471 we have to do is make sure that the case_expr is a scalar integer
7472 expression. */
7474 static void
7475 resolve_select (gfc_code *code)
7477 gfc_code *body;
7478 gfc_expr *case_expr;
7479 gfc_case *cp, *default_case, *tail, *head;
7480 int seen_unreachable;
7481 int seen_logical;
7482 int ncases;
7483 bt type;
7484 gfc_try t;
7486 if (code->expr1 == NULL)
7488 /* This was actually a computed GOTO statement. */
7489 case_expr = code->expr2;
7490 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7491 gfc_error ("Selection expression in computed GOTO statement "
7492 "at %L must be a scalar integer expression",
7493 &case_expr->where);
7495 /* Further checking is not necessary because this SELECT was built
7496 by the compiler, so it should always be OK. Just move the
7497 case_expr from expr2 to expr so that we can handle computed
7498 GOTOs as normal SELECTs from here on. */
7499 code->expr1 = code->expr2;
7500 code->expr2 = NULL;
7501 return;
7504 case_expr = code->expr1;
7506 type = case_expr->ts.type;
7507 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7509 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7510 &case_expr->where, gfc_typename (&case_expr->ts));
7512 /* Punt. Going on here just produce more garbage error messages. */
7513 return;
7516 if (case_expr->rank != 0)
7518 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7519 "expression", &case_expr->where);
7521 /* Punt. */
7522 return;
7526 /* Raise a warning if an INTEGER case value exceeds the range of
7527 the case-expr. Later, all expressions will be promoted to the
7528 largest kind of all case-labels. */
7530 if (type == BT_INTEGER)
7531 for (body = code->block; body; body = body->block)
7532 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7534 if (cp->low
7535 && gfc_check_integer_range (cp->low->value.integer,
7536 case_expr->ts.kind) != ARITH_OK)
7537 gfc_warning ("Expression in CASE statement at %L is "
7538 "not in the range of %s", &cp->low->where,
7539 gfc_typename (&case_expr->ts));
7541 if (cp->high
7542 && cp->low != cp->high
7543 && gfc_check_integer_range (cp->high->value.integer,
7544 case_expr->ts.kind) != ARITH_OK)
7545 gfc_warning ("Expression in CASE statement at %L is "
7546 "not in the range of %s", &cp->high->where,
7547 gfc_typename (&case_expr->ts));
7550 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7551 of the SELECT CASE expression and its CASE values. Walk the lists
7552 of case values, and if we find a mismatch, promote case_expr to
7553 the appropriate kind. */
7555 if (type == BT_LOGICAL || type == BT_INTEGER)
7557 for (body = code->block; body; body = body->block)
7559 /* Walk the case label list. */
7560 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7562 /* Intercept the DEFAULT case. It does not have a kind. */
7563 if (cp->low == NULL && cp->high == NULL)
7564 continue;
7566 /* Unreachable case ranges are discarded, so ignore. */
7567 if (cp->low != NULL && cp->high != NULL
7568 && cp->low != cp->high
7569 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7570 continue;
7572 if (cp->low != NULL
7573 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7574 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7576 if (cp->high != NULL
7577 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7578 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7583 /* Assume there is no DEFAULT case. */
7584 default_case = NULL;
7585 head = tail = NULL;
7586 ncases = 0;
7587 seen_logical = 0;
7589 for (body = code->block; body; body = body->block)
7591 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7592 t = SUCCESS;
7593 seen_unreachable = 0;
7595 /* Walk the case label list, making sure that all case labels
7596 are legal. */
7597 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7599 /* Count the number of cases in the whole construct. */
7600 ncases++;
7602 /* Intercept the DEFAULT case. */
7603 if (cp->low == NULL && cp->high == NULL)
7605 if (default_case != NULL)
7607 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7608 "by a second DEFAULT CASE at %L",
7609 &default_case->where, &cp->where);
7610 t = FAILURE;
7611 break;
7613 else
7615 default_case = cp;
7616 continue;
7620 /* Deal with single value cases and case ranges. Errors are
7621 issued from the validation function. */
7622 if (validate_case_label_expr (cp->low, case_expr) != SUCCESS
7623 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
7625 t = FAILURE;
7626 break;
7629 if (type == BT_LOGICAL
7630 && ((cp->low == NULL || cp->high == NULL)
7631 || cp->low != cp->high))
7633 gfc_error ("Logical range in CASE statement at %L is not "
7634 "allowed", &cp->low->where);
7635 t = FAILURE;
7636 break;
7639 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7641 int value;
7642 value = cp->low->value.logical == 0 ? 2 : 1;
7643 if (value & seen_logical)
7645 gfc_error ("Constant logical value in CASE statement "
7646 "is repeated at %L",
7647 &cp->low->where);
7648 t = FAILURE;
7649 break;
7651 seen_logical |= value;
7654 if (cp->low != NULL && cp->high != NULL
7655 && cp->low != cp->high
7656 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7658 if (gfc_option.warn_surprising)
7659 gfc_warning ("Range specification at %L can never "
7660 "be matched", &cp->where);
7662 cp->unreachable = 1;
7663 seen_unreachable = 1;
7665 else
7667 /* If the case range can be matched, it can also overlap with
7668 other cases. To make sure it does not, we put it in a
7669 double linked list here. We sort that with a merge sort
7670 later on to detect any overlapping cases. */
7671 if (!head)
7673 head = tail = cp;
7674 head->right = head->left = NULL;
7676 else
7678 tail->right = cp;
7679 tail->right->left = tail;
7680 tail = tail->right;
7681 tail->right = NULL;
7686 /* It there was a failure in the previous case label, give up
7687 for this case label list. Continue with the next block. */
7688 if (t == FAILURE)
7689 continue;
7691 /* See if any case labels that are unreachable have been seen.
7692 If so, we eliminate them. This is a bit of a kludge because
7693 the case lists for a single case statement (label) is a
7694 single forward linked lists. */
7695 if (seen_unreachable)
7697 /* Advance until the first case in the list is reachable. */
7698 while (body->ext.block.case_list != NULL
7699 && body->ext.block.case_list->unreachable)
7701 gfc_case *n = body->ext.block.case_list;
7702 body->ext.block.case_list = body->ext.block.case_list->next;
7703 n->next = NULL;
7704 gfc_free_case_list (n);
7707 /* Strip all other unreachable cases. */
7708 if (body->ext.block.case_list)
7710 for (cp = body->ext.block.case_list; cp->next; cp = cp->next)
7712 if (cp->next->unreachable)
7714 gfc_case *n = cp->next;
7715 cp->next = cp->next->next;
7716 n->next = NULL;
7717 gfc_free_case_list (n);
7724 /* See if there were overlapping cases. If the check returns NULL,
7725 there was overlap. In that case we don't do anything. If head
7726 is non-NULL, we prepend the DEFAULT case. The sorted list can
7727 then used during code generation for SELECT CASE constructs with
7728 a case expression of a CHARACTER type. */
7729 if (head)
7731 head = check_case_overlap (head);
7733 /* Prepend the default_case if it is there. */
7734 if (head != NULL && default_case)
7736 default_case->left = NULL;
7737 default_case->right = head;
7738 head->left = default_case;
7742 /* Eliminate dead blocks that may be the result if we've seen
7743 unreachable case labels for a block. */
7744 for (body = code; body && body->block; body = body->block)
7746 if (body->block->ext.block.case_list == NULL)
7748 /* Cut the unreachable block from the code chain. */
7749 gfc_code *c = body->block;
7750 body->block = c->block;
7752 /* Kill the dead block, but not the blocks below it. */
7753 c->block = NULL;
7754 gfc_free_statements (c);
7758 /* More than two cases is legal but insane for logical selects.
7759 Issue a warning for it. */
7760 if (gfc_option.warn_surprising && type == BT_LOGICAL
7761 && ncases > 2)
7762 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7763 &code->loc);
7767 /* Check if a derived type is extensible. */
7769 bool
7770 gfc_type_is_extensible (gfc_symbol *sym)
7772 return !(sym->attr.is_bind_c || sym->attr.sequence);
7776 /* Resolve an associate name: Resolve target and ensure the type-spec is
7777 correct as well as possibly the array-spec. */
7779 static void
7780 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7782 gfc_expr* target;
7784 gcc_assert (sym->assoc);
7785 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7787 /* If this is for SELECT TYPE, the target may not yet be set. In that
7788 case, return. Resolution will be called later manually again when
7789 this is done. */
7790 target = sym->assoc->target;
7791 if (!target)
7792 return;
7793 gcc_assert (!sym->assoc->dangling);
7795 if (resolve_target && gfc_resolve_expr (target) != SUCCESS)
7796 return;
7798 /* For variable targets, we get some attributes from the target. */
7799 if (target->expr_type == EXPR_VARIABLE)
7801 gfc_symbol* tsym;
7803 gcc_assert (target->symtree);
7804 tsym = target->symtree->n.sym;
7806 sym->attr.asynchronous = tsym->attr.asynchronous;
7807 sym->attr.volatile_ = tsym->attr.volatile_;
7809 sym->attr.target = (tsym->attr.target || tsym->attr.pointer);
7812 /* Get type if this was not already set. Note that it can be
7813 some other type than the target in case this is a SELECT TYPE
7814 selector! So we must not update when the type is already there. */
7815 if (sym->ts.type == BT_UNKNOWN)
7816 sym->ts = target->ts;
7817 gcc_assert (sym->ts.type != BT_UNKNOWN);
7819 /* See if this is a valid association-to-variable. */
7820 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7821 && !gfc_has_vector_subscript (target));
7823 /* Finally resolve if this is an array or not. */
7824 if (sym->attr.dimension && target->rank == 0)
7826 gfc_error ("Associate-name '%s' at %L is used as array",
7827 sym->name, &sym->declared_at);
7828 sym->attr.dimension = 0;
7829 return;
7831 if (target->rank > 0)
7832 sym->attr.dimension = 1;
7834 if (sym->attr.dimension)
7836 sym->as = gfc_get_array_spec ();
7837 sym->as->rank = target->rank;
7838 sym->as->type = AS_DEFERRED;
7840 /* Target must not be coindexed, thus the associate-variable
7841 has no corank. */
7842 sym->as->corank = 0;
7847 /* Resolve a SELECT TYPE statement. */
7849 static void
7850 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
7852 gfc_symbol *selector_type;
7853 gfc_code *body, *new_st, *if_st, *tail;
7854 gfc_code *class_is = NULL, *default_case = NULL;
7855 gfc_case *c;
7856 gfc_symtree *st;
7857 char name[GFC_MAX_SYMBOL_LEN];
7858 gfc_namespace *ns;
7859 int error = 0;
7861 ns = code->ext.block.ns;
7862 gfc_resolve (ns);
7864 /* Check for F03:C813. */
7865 if (code->expr1->ts.type != BT_CLASS
7866 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7868 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7869 "at %L", &code->loc);
7870 return;
7873 if (code->expr2)
7875 if (code->expr1->symtree->n.sym->attr.untyped)
7876 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7877 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7879 else
7880 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7882 /* Loop over TYPE IS / CLASS IS cases. */
7883 for (body = code->block; body; body = body->block)
7885 c = body->ext.block.case_list;
7887 /* Check F03:C815. */
7888 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7889 && !gfc_type_is_extensible (c->ts.u.derived))
7891 gfc_error ("Derived type '%s' at %L must be extensible",
7892 c->ts.u.derived->name, &c->where);
7893 error++;
7894 continue;
7897 /* Check F03:C816. */
7898 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7899 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
7901 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7902 c->ts.u.derived->name, &c->where, selector_type->name);
7903 error++;
7904 continue;
7907 /* Intercept the DEFAULT case. */
7908 if (c->ts.type == BT_UNKNOWN)
7910 /* Check F03:C818. */
7911 if (default_case)
7913 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7914 "by a second DEFAULT CASE at %L",
7915 &default_case->ext.block.case_list->where, &c->where);
7916 error++;
7917 continue;
7920 default_case = body;
7924 if (error > 0)
7925 return;
7927 /* Transform SELECT TYPE statement to BLOCK and associate selector to
7928 target if present. If there are any EXIT statements referring to the
7929 SELECT TYPE construct, this is no problem because the gfc_code
7930 reference stays the same and EXIT is equally possible from the BLOCK
7931 it is changed to. */
7932 code->op = EXEC_BLOCK;
7933 if (code->expr2)
7935 gfc_association_list* assoc;
7937 assoc = gfc_get_association_list ();
7938 assoc->st = code->expr1->symtree;
7939 assoc->target = gfc_copy_expr (code->expr2);
7940 /* assoc->variable will be set by resolve_assoc_var. */
7942 code->ext.block.assoc = assoc;
7943 code->expr1->symtree->n.sym->assoc = assoc;
7945 resolve_assoc_var (code->expr1->symtree->n.sym, false);
7947 else
7948 code->ext.block.assoc = NULL;
7950 /* Add EXEC_SELECT to switch on type. */
7951 new_st = gfc_get_code ();
7952 new_st->op = code->op;
7953 new_st->expr1 = code->expr1;
7954 new_st->expr2 = code->expr2;
7955 new_st->block = code->block;
7956 code->expr1 = code->expr2 = NULL;
7957 code->block = NULL;
7958 if (!ns->code)
7959 ns->code = new_st;
7960 else
7961 ns->code->next = new_st;
7962 code = new_st;
7963 code->op = EXEC_SELECT;
7964 gfc_add_vptr_component (code->expr1);
7965 gfc_add_hash_component (code->expr1);
7967 /* Loop over TYPE IS / CLASS IS cases. */
7968 for (body = code->block; body; body = body->block)
7970 c = body->ext.block.case_list;
7972 if (c->ts.type == BT_DERIVED)
7973 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
7974 c->ts.u.derived->hash_value);
7976 else if (c->ts.type == BT_UNKNOWN)
7977 continue;
7979 /* Associate temporary to selector. This should only be done
7980 when this case is actually true, so build a new ASSOCIATE
7981 that does precisely this here (instead of using the
7982 'global' one). */
7984 if (c->ts.type == BT_CLASS)
7985 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
7986 else
7987 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
7988 st = gfc_find_symtree (ns->sym_root, name);
7989 gcc_assert (st->n.sym->assoc);
7990 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
7991 if (c->ts.type == BT_DERIVED)
7992 gfc_add_data_component (st->n.sym->assoc->target);
7994 new_st = gfc_get_code ();
7995 new_st->op = EXEC_BLOCK;
7996 new_st->ext.block.ns = gfc_build_block_ns (ns);
7997 new_st->ext.block.ns->code = body->next;
7998 body->next = new_st;
8000 /* Chain in the new list only if it is marked as dangling. Otherwise
8001 there is a CASE label overlap and this is already used. Just ignore,
8002 the error is diagonsed elsewhere. */
8003 if (st->n.sym->assoc->dangling)
8005 new_st->ext.block.assoc = st->n.sym->assoc;
8006 st->n.sym->assoc->dangling = 0;
8009 resolve_assoc_var (st->n.sym, false);
8012 /* Take out CLASS IS cases for separate treatment. */
8013 body = code;
8014 while (body && body->block)
8016 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8018 /* Add to class_is list. */
8019 if (class_is == NULL)
8021 class_is = body->block;
8022 tail = class_is;
8024 else
8026 for (tail = class_is; tail->block; tail = tail->block) ;
8027 tail->block = body->block;
8028 tail = tail->block;
8030 /* Remove from EXEC_SELECT list. */
8031 body->block = body->block->block;
8032 tail->block = NULL;
8034 else
8035 body = body->block;
8038 if (class_is)
8040 gfc_symbol *vtab;
8042 if (!default_case)
8044 /* Add a default case to hold the CLASS IS cases. */
8045 for (tail = code; tail->block; tail = tail->block) ;
8046 tail->block = gfc_get_code ();
8047 tail = tail->block;
8048 tail->op = EXEC_SELECT_TYPE;
8049 tail->ext.block.case_list = gfc_get_case ();
8050 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8051 tail->next = NULL;
8052 default_case = tail;
8055 /* More than one CLASS IS block? */
8056 if (class_is->block)
8058 gfc_code **c1,*c2;
8059 bool swapped;
8060 /* Sort CLASS IS blocks by extension level. */
8063 swapped = false;
8064 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8066 c2 = (*c1)->block;
8067 /* F03:C817 (check for doubles). */
8068 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8069 == c2->ext.block.case_list->ts.u.derived->hash_value)
8071 gfc_error ("Double CLASS IS block in SELECT TYPE "
8072 "statement at %L",
8073 &c2->ext.block.case_list->where);
8074 return;
8076 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8077 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8079 /* Swap. */
8080 (*c1)->block = c2->block;
8081 c2->block = *c1;
8082 *c1 = c2;
8083 swapped = true;
8087 while (swapped);
8090 /* Generate IF chain. */
8091 if_st = gfc_get_code ();
8092 if_st->op = EXEC_IF;
8093 new_st = if_st;
8094 for (body = class_is; body; body = body->block)
8096 new_st->block = gfc_get_code ();
8097 new_st = new_st->block;
8098 new_st->op = EXEC_IF;
8099 /* Set up IF condition: Call _gfortran_is_extension_of. */
8100 new_st->expr1 = gfc_get_expr ();
8101 new_st->expr1->expr_type = EXPR_FUNCTION;
8102 new_st->expr1->ts.type = BT_LOGICAL;
8103 new_st->expr1->ts.kind = 4;
8104 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8105 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8106 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8107 /* Set up arguments. */
8108 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8109 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8110 new_st->expr1->value.function.actual->expr->where = code->loc;
8111 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8112 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8113 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8114 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8115 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8116 new_st->next = body->next;
8118 if (default_case->next)
8120 new_st->block = gfc_get_code ();
8121 new_st = new_st->block;
8122 new_st->op = EXEC_IF;
8123 new_st->next = default_case->next;
8126 /* Replace CLASS DEFAULT code by the IF chain. */
8127 default_case->next = if_st;
8130 /* Resolve the internal code. This can not be done earlier because
8131 it requires that the sym->assoc of selectors is set already. */
8132 gfc_current_ns = ns;
8133 gfc_resolve_blocks (code->block, gfc_current_ns);
8134 gfc_current_ns = old_ns;
8136 resolve_select (code);
8140 /* Resolve a transfer statement. This is making sure that:
8141 -- a derived type being transferred has only non-pointer components
8142 -- a derived type being transferred doesn't have private components, unless
8143 it's being transferred from the module where the type was defined
8144 -- we're not trying to transfer a whole assumed size array. */
8146 static void
8147 resolve_transfer (gfc_code *code)
8149 gfc_typespec *ts;
8150 gfc_symbol *sym;
8151 gfc_ref *ref;
8152 gfc_expr *exp;
8154 exp = code->expr1;
8156 while (exp != NULL && exp->expr_type == EXPR_OP
8157 && exp->value.op.op == INTRINSIC_PARENTHESES)
8158 exp = exp->value.op.op1;
8160 if (exp && exp->expr_type == EXPR_NULL && exp->ts.type == BT_UNKNOWN)
8162 gfc_error ("NULL intrinsic at %L in data transfer statement requires "
8163 "MOLD=", &exp->where);
8164 return;
8167 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8168 && exp->expr_type != EXPR_FUNCTION))
8169 return;
8171 /* If we are reading, the variable will be changed. Note that
8172 code->ext.dt may be NULL if the TRANSFER is related to
8173 an INQUIRE statement -- but in this case, we are not reading, either. */
8174 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8175 && gfc_check_vardef_context (exp, false, false, _("item in READ"))
8176 == FAILURE)
8177 return;
8179 sym = exp->symtree->n.sym;
8180 ts = &sym->ts;
8182 /* Go to actual component transferred. */
8183 for (ref = exp->ref; ref; ref = ref->next)
8184 if (ref->type == REF_COMPONENT)
8185 ts = &ref->u.c.component->ts;
8187 if (ts->type == BT_CLASS)
8189 /* FIXME: Test for defined input/output. */
8190 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8191 "it is processed by a defined input/output procedure",
8192 &code->loc);
8193 return;
8196 if (ts->type == BT_DERIVED)
8198 /* Check that transferred derived type doesn't contain POINTER
8199 components. */
8200 if (ts->u.derived->attr.pointer_comp)
8202 gfc_error ("Data transfer element at %L cannot have POINTER "
8203 "components unless it is processed by a defined "
8204 "input/output procedure", &code->loc);
8205 return;
8208 /* F08:C935. */
8209 if (ts->u.derived->attr.proc_pointer_comp)
8211 gfc_error ("Data transfer element at %L cannot have "
8212 "procedure pointer components", &code->loc);
8213 return;
8216 if (ts->u.derived->attr.alloc_comp)
8218 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8219 "components unless it is processed by a defined "
8220 "input/output procedure", &code->loc);
8221 return;
8224 if (derived_inaccessible (ts->u.derived))
8226 gfc_error ("Data transfer element at %L cannot have "
8227 "PRIVATE components",&code->loc);
8228 return;
8232 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8233 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8235 gfc_error ("Data transfer element at %L cannot be a full reference to "
8236 "an assumed-size array", &code->loc);
8237 return;
8242 /*********** Toplevel code resolution subroutines ***********/
8244 /* Find the set of labels that are reachable from this block. We also
8245 record the last statement in each block. */
8247 static void
8248 find_reachable_labels (gfc_code *block)
8250 gfc_code *c;
8252 if (!block)
8253 return;
8255 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8257 /* Collect labels in this block. We don't keep those corresponding
8258 to END {IF|SELECT}, these are checked in resolve_branch by going
8259 up through the code_stack. */
8260 for (c = block; c; c = c->next)
8262 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8263 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8266 /* Merge with labels from parent block. */
8267 if (cs_base->prev)
8269 gcc_assert (cs_base->prev->reachable_labels);
8270 bitmap_ior_into (cs_base->reachable_labels,
8271 cs_base->prev->reachable_labels);
8276 static void
8277 resolve_lock_unlock (gfc_code *code)
8279 if (code->expr1->ts.type != BT_DERIVED
8280 || code->expr1->expr_type != EXPR_VARIABLE
8281 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8282 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8283 || code->expr1->rank != 0
8284 || (!gfc_is_coarray (code->expr1) && !gfc_is_coindexed (code->expr1)))
8285 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8286 &code->expr1->where);
8288 /* Check STAT. */
8289 if (code->expr2
8290 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8291 || code->expr2->expr_type != EXPR_VARIABLE))
8292 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8293 &code->expr2->where);
8295 if (code->expr2
8296 && gfc_check_vardef_context (code->expr2, false, false,
8297 _("STAT variable")) == FAILURE)
8298 return;
8300 /* Check ERRMSG. */
8301 if (code->expr3
8302 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8303 || code->expr3->expr_type != EXPR_VARIABLE))
8304 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8305 &code->expr3->where);
8307 if (code->expr3
8308 && gfc_check_vardef_context (code->expr3, false, false,
8309 _("ERRMSG variable")) == FAILURE)
8310 return;
8312 /* Check ACQUIRED_LOCK. */
8313 if (code->expr4
8314 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8315 || code->expr4->expr_type != EXPR_VARIABLE))
8316 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8317 "variable", &code->expr4->where);
8319 if (code->expr4
8320 && gfc_check_vardef_context (code->expr4, false, false,
8321 _("ACQUIRED_LOCK variable")) == FAILURE)
8322 return;
8326 static void
8327 resolve_sync (gfc_code *code)
8329 /* Check imageset. The * case matches expr1 == NULL. */
8330 if (code->expr1)
8332 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8333 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8334 "INTEGER expression", &code->expr1->where);
8335 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8336 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8337 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8338 &code->expr1->where);
8339 else if (code->expr1->expr_type == EXPR_ARRAY
8340 && gfc_simplify_expr (code->expr1, 0) == SUCCESS)
8342 gfc_constructor *cons;
8343 cons = gfc_constructor_first (code->expr1->value.constructor);
8344 for (; cons; cons = gfc_constructor_next (cons))
8345 if (cons->expr->expr_type == EXPR_CONSTANT
8346 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8347 gfc_error ("Imageset argument at %L must between 1 and "
8348 "num_images()", &cons->expr->where);
8352 /* Check STAT. */
8353 if (code->expr2
8354 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8355 || code->expr2->expr_type != EXPR_VARIABLE))
8356 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8357 &code->expr2->where);
8359 /* Check ERRMSG. */
8360 if (code->expr3
8361 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8362 || code->expr3->expr_type != EXPR_VARIABLE))
8363 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8364 &code->expr3->where);
8368 /* Given a branch to a label, see if the branch is conforming.
8369 The code node describes where the branch is located. */
8371 static void
8372 resolve_branch (gfc_st_label *label, gfc_code *code)
8374 code_stack *stack;
8376 if (label == NULL)
8377 return;
8379 /* Step one: is this a valid branching target? */
8381 if (label->defined == ST_LABEL_UNKNOWN)
8383 gfc_error ("Label %d referenced at %L is never defined", label->value,
8384 &label->where);
8385 return;
8388 if (label->defined != ST_LABEL_TARGET)
8390 gfc_error ("Statement at %L is not a valid branch target statement "
8391 "for the branch statement at %L", &label->where, &code->loc);
8392 return;
8395 /* Step two: make sure this branch is not a branch to itself ;-) */
8397 if (code->here == label)
8399 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8400 return;
8403 /* Step three: See if the label is in the same block as the
8404 branching statement. The hard work has been done by setting up
8405 the bitmap reachable_labels. */
8407 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8409 /* Check now whether there is a CRITICAL construct; if so, check
8410 whether the label is still visible outside of the CRITICAL block,
8411 which is invalid. */
8412 for (stack = cs_base; stack; stack = stack->prev)
8414 if (stack->current->op == EXEC_CRITICAL
8415 && bitmap_bit_p (stack->reachable_labels, label->value))
8416 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8417 "label at %L", &code->loc, &label->where);
8418 else if (stack->current->op == EXEC_DO_CONCURRENT
8419 && bitmap_bit_p (stack->reachable_labels, label->value))
8420 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8421 "for label at %L", &code->loc, &label->where);
8424 return;
8427 /* Step four: If we haven't found the label in the bitmap, it may
8428 still be the label of the END of the enclosing block, in which
8429 case we find it by going up the code_stack. */
8431 for (stack = cs_base; stack; stack = stack->prev)
8433 if (stack->current->next && stack->current->next->here == label)
8434 break;
8435 if (stack->current->op == EXEC_CRITICAL)
8437 /* Note: A label at END CRITICAL does not leave the CRITICAL
8438 construct as END CRITICAL is still part of it. */
8439 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8440 " at %L", &code->loc, &label->where);
8441 return;
8443 else if (stack->current->op == EXEC_DO_CONCURRENT)
8445 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
8446 "label at %L", &code->loc, &label->where);
8447 return;
8451 if (stack)
8453 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
8454 return;
8457 /* The label is not in an enclosing block, so illegal. This was
8458 allowed in Fortran 66, so we allow it as extension. No
8459 further checks are necessary in this case. */
8460 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8461 "as the GOTO statement at %L", &label->where,
8462 &code->loc);
8463 return;
8467 /* Check whether EXPR1 has the same shape as EXPR2. */
8469 static gfc_try
8470 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8472 mpz_t shape[GFC_MAX_DIMENSIONS];
8473 mpz_t shape2[GFC_MAX_DIMENSIONS];
8474 gfc_try result = FAILURE;
8475 int i;
8477 /* Compare the rank. */
8478 if (expr1->rank != expr2->rank)
8479 return result;
8481 /* Compare the size of each dimension. */
8482 for (i=0; i<expr1->rank; i++)
8484 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
8485 goto ignore;
8487 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
8488 goto ignore;
8490 if (mpz_cmp (shape[i], shape2[i]))
8491 goto over;
8494 /* When either of the two expression is an assumed size array, we
8495 ignore the comparison of dimension sizes. */
8496 ignore:
8497 result = SUCCESS;
8499 over:
8500 gfc_clear_shape (shape, i);
8501 gfc_clear_shape (shape2, i);
8502 return result;
8506 /* Check whether a WHERE assignment target or a WHERE mask expression
8507 has the same shape as the outmost WHERE mask expression. */
8509 static void
8510 resolve_where (gfc_code *code, gfc_expr *mask)
8512 gfc_code *cblock;
8513 gfc_code *cnext;
8514 gfc_expr *e = NULL;
8516 cblock = code->block;
8518 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8519 In case of nested WHERE, only the outmost one is stored. */
8520 if (mask == NULL) /* outmost WHERE */
8521 e = cblock->expr1;
8522 else /* inner WHERE */
8523 e = mask;
8525 while (cblock)
8527 if (cblock->expr1)
8529 /* Check if the mask-expr has a consistent shape with the
8530 outmost WHERE mask-expr. */
8531 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
8532 gfc_error ("WHERE mask at %L has inconsistent shape",
8533 &cblock->expr1->where);
8536 /* the assignment statement of a WHERE statement, or the first
8537 statement in where-body-construct of a WHERE construct */
8538 cnext = cblock->next;
8539 while (cnext)
8541 switch (cnext->op)
8543 /* WHERE assignment statement */
8544 case EXEC_ASSIGN:
8546 /* Check shape consistent for WHERE assignment target. */
8547 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
8548 gfc_error ("WHERE assignment target at %L has "
8549 "inconsistent shape", &cnext->expr1->where);
8550 break;
8553 case EXEC_ASSIGN_CALL:
8554 resolve_call (cnext);
8555 if (!cnext->resolved_sym->attr.elemental)
8556 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8557 &cnext->ext.actual->expr->where);
8558 break;
8560 /* WHERE or WHERE construct is part of a where-body-construct */
8561 case EXEC_WHERE:
8562 resolve_where (cnext, e);
8563 break;
8565 default:
8566 gfc_error ("Unsupported statement inside WHERE at %L",
8567 &cnext->loc);
8569 /* the next statement within the same where-body-construct */
8570 cnext = cnext->next;
8572 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8573 cblock = cblock->block;
8578 /* Resolve assignment in FORALL construct.
8579 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8580 FORALL index variables. */
8582 static void
8583 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8585 int n;
8587 for (n = 0; n < nvar; n++)
8589 gfc_symbol *forall_index;
8591 forall_index = var_expr[n]->symtree->n.sym;
8593 /* Check whether the assignment target is one of the FORALL index
8594 variable. */
8595 if ((code->expr1->expr_type == EXPR_VARIABLE)
8596 && (code->expr1->symtree->n.sym == forall_index))
8597 gfc_error ("Assignment to a FORALL index variable at %L",
8598 &code->expr1->where);
8599 else
8601 /* If one of the FORALL index variables doesn't appear in the
8602 assignment variable, then there could be a many-to-one
8603 assignment. Emit a warning rather than an error because the
8604 mask could be resolving this problem. */
8605 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
8606 gfc_warning ("The FORALL with index '%s' is not used on the "
8607 "left side of the assignment at %L and so might "
8608 "cause multiple assignment to this object",
8609 var_expr[n]->symtree->name, &code->expr1->where);
8615 /* Resolve WHERE statement in FORALL construct. */
8617 static void
8618 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8619 gfc_expr **var_expr)
8621 gfc_code *cblock;
8622 gfc_code *cnext;
8624 cblock = code->block;
8625 while (cblock)
8627 /* the assignment statement of a WHERE statement, or the first
8628 statement in where-body-construct of a WHERE construct */
8629 cnext = cblock->next;
8630 while (cnext)
8632 switch (cnext->op)
8634 /* WHERE assignment statement */
8635 case EXEC_ASSIGN:
8636 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8637 break;
8639 /* WHERE operator assignment statement */
8640 case EXEC_ASSIGN_CALL:
8641 resolve_call (cnext);
8642 if (!cnext->resolved_sym->attr.elemental)
8643 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8644 &cnext->ext.actual->expr->where);
8645 break;
8647 /* WHERE or WHERE construct is part of a where-body-construct */
8648 case EXEC_WHERE:
8649 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8650 break;
8652 default:
8653 gfc_error ("Unsupported statement inside WHERE at %L",
8654 &cnext->loc);
8656 /* the next statement within the same where-body-construct */
8657 cnext = cnext->next;
8659 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8660 cblock = cblock->block;
8665 /* Traverse the FORALL body to check whether the following errors exist:
8666 1. For assignment, check if a many-to-one assignment happens.
8667 2. For WHERE statement, check the WHERE body to see if there is any
8668 many-to-one assignment. */
8670 static void
8671 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8673 gfc_code *c;
8675 c = code->block->next;
8676 while (c)
8678 switch (c->op)
8680 case EXEC_ASSIGN:
8681 case EXEC_POINTER_ASSIGN:
8682 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8683 break;
8685 case EXEC_ASSIGN_CALL:
8686 resolve_call (c);
8687 break;
8689 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8690 there is no need to handle it here. */
8691 case EXEC_FORALL:
8692 break;
8693 case EXEC_WHERE:
8694 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8695 break;
8696 default:
8697 break;
8699 /* The next statement in the FORALL body. */
8700 c = c->next;
8705 /* Counts the number of iterators needed inside a forall construct, including
8706 nested forall constructs. This is used to allocate the needed memory
8707 in gfc_resolve_forall. */
8709 static int
8710 gfc_count_forall_iterators (gfc_code *code)
8712 int max_iters, sub_iters, current_iters;
8713 gfc_forall_iterator *fa;
8715 gcc_assert(code->op == EXEC_FORALL);
8716 max_iters = 0;
8717 current_iters = 0;
8719 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8720 current_iters ++;
8722 code = code->block->next;
8724 while (code)
8726 if (code->op == EXEC_FORALL)
8728 sub_iters = gfc_count_forall_iterators (code);
8729 if (sub_iters > max_iters)
8730 max_iters = sub_iters;
8732 code = code->next;
8735 return current_iters + max_iters;
8739 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8740 gfc_resolve_forall_body to resolve the FORALL body. */
8742 static void
8743 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8745 static gfc_expr **var_expr;
8746 static int total_var = 0;
8747 static int nvar = 0;
8748 int old_nvar, tmp;
8749 gfc_forall_iterator *fa;
8750 int i;
8752 old_nvar = nvar;
8754 /* Start to resolve a FORALL construct */
8755 if (forall_save == 0)
8757 /* Count the total number of FORALL index in the nested FORALL
8758 construct in order to allocate the VAR_EXPR with proper size. */
8759 total_var = gfc_count_forall_iterators (code);
8761 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8762 var_expr = XCNEWVEC (gfc_expr *, total_var);
8765 /* The information about FORALL iterator, including FORALL index start, end
8766 and stride. The FORALL index can not appear in start, end or stride. */
8767 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8769 /* Check if any outer FORALL index name is the same as the current
8770 one. */
8771 for (i = 0; i < nvar; i++)
8773 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8775 gfc_error ("An outer FORALL construct already has an index "
8776 "with this name %L", &fa->var->where);
8780 /* Record the current FORALL index. */
8781 var_expr[nvar] = gfc_copy_expr (fa->var);
8783 nvar++;
8785 /* No memory leak. */
8786 gcc_assert (nvar <= total_var);
8789 /* Resolve the FORALL body. */
8790 gfc_resolve_forall_body (code, nvar, var_expr);
8792 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8793 gfc_resolve_blocks (code->block, ns);
8795 tmp = nvar;
8796 nvar = old_nvar;
8797 /* Free only the VAR_EXPRs allocated in this frame. */
8798 for (i = nvar; i < tmp; i++)
8799 gfc_free_expr (var_expr[i]);
8801 if (nvar == 0)
8803 /* We are in the outermost FORALL construct. */
8804 gcc_assert (forall_save == 0);
8806 /* VAR_EXPR is not needed any more. */
8807 free (var_expr);
8808 total_var = 0;
8813 /* Resolve a BLOCK construct statement. */
8815 static void
8816 resolve_block_construct (gfc_code* code)
8818 /* Resolve the BLOCK's namespace. */
8819 gfc_resolve (code->ext.block.ns);
8821 /* For an ASSOCIATE block, the associations (and their targets) are already
8822 resolved during resolve_symbol. */
8826 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8827 DO code nodes. */
8829 static void resolve_code (gfc_code *, gfc_namespace *);
8831 void
8832 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8834 gfc_try t;
8836 for (; b; b = b->block)
8838 t = gfc_resolve_expr (b->expr1);
8839 if (gfc_resolve_expr (b->expr2) == FAILURE)
8840 t = FAILURE;
8842 switch (b->op)
8844 case EXEC_IF:
8845 if (t == SUCCESS && b->expr1 != NULL
8846 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
8847 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8848 &b->expr1->where);
8849 break;
8851 case EXEC_WHERE:
8852 if (t == SUCCESS
8853 && b->expr1 != NULL
8854 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
8855 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8856 &b->expr1->where);
8857 break;
8859 case EXEC_GOTO:
8860 resolve_branch (b->label1, b);
8861 break;
8863 case EXEC_BLOCK:
8864 resolve_block_construct (b);
8865 break;
8867 case EXEC_SELECT:
8868 case EXEC_SELECT_TYPE:
8869 case EXEC_FORALL:
8870 case EXEC_DO:
8871 case EXEC_DO_WHILE:
8872 case EXEC_DO_CONCURRENT:
8873 case EXEC_CRITICAL:
8874 case EXEC_READ:
8875 case EXEC_WRITE:
8876 case EXEC_IOLENGTH:
8877 case EXEC_WAIT:
8878 break;
8880 case EXEC_OMP_ATOMIC:
8881 case EXEC_OMP_CRITICAL:
8882 case EXEC_OMP_DO:
8883 case EXEC_OMP_MASTER:
8884 case EXEC_OMP_ORDERED:
8885 case EXEC_OMP_PARALLEL:
8886 case EXEC_OMP_PARALLEL_DO:
8887 case EXEC_OMP_PARALLEL_SECTIONS:
8888 case EXEC_OMP_PARALLEL_WORKSHARE:
8889 case EXEC_OMP_SECTIONS:
8890 case EXEC_OMP_SINGLE:
8891 case EXEC_OMP_TASK:
8892 case EXEC_OMP_TASKWAIT:
8893 case EXEC_OMP_TASKYIELD:
8894 case EXEC_OMP_WORKSHARE:
8895 break;
8897 default:
8898 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
8901 resolve_code (b->next, ns);
8906 /* Does everything to resolve an ordinary assignment. Returns true
8907 if this is an interface assignment. */
8908 static bool
8909 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
8911 bool rval = false;
8912 gfc_expr *lhs;
8913 gfc_expr *rhs;
8914 int llen = 0;
8915 int rlen = 0;
8916 int n;
8917 gfc_ref *ref;
8919 if (gfc_extend_assign (code, ns) == SUCCESS)
8921 gfc_expr** rhsptr;
8923 if (code->op == EXEC_ASSIGN_CALL)
8925 lhs = code->ext.actual->expr;
8926 rhsptr = &code->ext.actual->next->expr;
8928 else
8930 gfc_actual_arglist* args;
8931 gfc_typebound_proc* tbp;
8933 gcc_assert (code->op == EXEC_COMPCALL);
8935 args = code->expr1->value.compcall.actual;
8936 lhs = args->expr;
8937 rhsptr = &args->next->expr;
8939 tbp = code->expr1->value.compcall.tbp;
8940 gcc_assert (!tbp->is_generic);
8943 /* Make a temporary rhs when there is a default initializer
8944 and rhs is the same symbol as the lhs. */
8945 if ((*rhsptr)->expr_type == EXPR_VARIABLE
8946 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
8947 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
8948 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
8949 *rhsptr = gfc_get_parentheses (*rhsptr);
8951 return true;
8954 lhs = code->expr1;
8955 rhs = code->expr2;
8957 if (rhs->is_boz
8958 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
8959 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
8960 &code->loc) == FAILURE)
8961 return false;
8963 /* Handle the case of a BOZ literal on the RHS. */
8964 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
8966 int rc;
8967 if (gfc_option.warn_surprising)
8968 gfc_warning ("BOZ literal at %L is bitwise transferred "
8969 "non-integer symbol '%s'", &code->loc,
8970 lhs->symtree->n.sym->name);
8972 if (!gfc_convert_boz (rhs, &lhs->ts))
8973 return false;
8974 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
8976 if (rc == ARITH_UNDERFLOW)
8977 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
8978 ". This check can be disabled with the option "
8979 "-fno-range-check", &rhs->where);
8980 else if (rc == ARITH_OVERFLOW)
8981 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
8982 ". This check can be disabled with the option "
8983 "-fno-range-check", &rhs->where);
8984 else if (rc == ARITH_NAN)
8985 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
8986 ". This check can be disabled with the option "
8987 "-fno-range-check", &rhs->where);
8988 return false;
8992 if (lhs->ts.type == BT_CHARACTER
8993 && gfc_option.warn_character_truncation)
8995 if (lhs->ts.u.cl != NULL
8996 && lhs->ts.u.cl->length != NULL
8997 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8998 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9000 if (rhs->expr_type == EXPR_CONSTANT)
9001 rlen = rhs->value.character.length;
9003 else if (rhs->ts.u.cl != NULL
9004 && rhs->ts.u.cl->length != NULL
9005 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9006 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9008 if (rlen && llen && rlen > llen)
9009 gfc_warning_now ("CHARACTER expression will be truncated "
9010 "in assignment (%d/%d) at %L",
9011 llen, rlen, &code->loc);
9014 /* Ensure that a vector index expression for the lvalue is evaluated
9015 to a temporary if the lvalue symbol is referenced in it. */
9016 if (lhs->rank)
9018 for (ref = lhs->ref; ref; ref= ref->next)
9019 if (ref->type == REF_ARRAY)
9021 for (n = 0; n < ref->u.ar.dimen; n++)
9022 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9023 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9024 ref->u.ar.start[n]))
9025 ref->u.ar.start[n]
9026 = gfc_get_parentheses (ref->u.ar.start[n]);
9030 if (gfc_pure (NULL))
9032 if (lhs->ts.type == BT_DERIVED
9033 && lhs->expr_type == EXPR_VARIABLE
9034 && lhs->ts.u.derived->attr.pointer_comp
9035 && rhs->expr_type == EXPR_VARIABLE
9036 && (gfc_impure_variable (rhs->symtree->n.sym)
9037 || gfc_is_coindexed (rhs)))
9039 /* F2008, C1283. */
9040 if (gfc_is_coindexed (rhs))
9041 gfc_error ("Coindexed expression at %L is assigned to "
9042 "a derived type variable with a POINTER "
9043 "component in a PURE procedure",
9044 &rhs->where);
9045 else
9046 gfc_error ("The impure variable at %L is assigned to "
9047 "a derived type variable with a POINTER "
9048 "component in a PURE procedure (12.6)",
9049 &rhs->where);
9050 return rval;
9053 /* Fortran 2008, C1283. */
9054 if (gfc_is_coindexed (lhs))
9056 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9057 "procedure", &rhs->where);
9058 return rval;
9062 if (gfc_implicit_pure (NULL))
9064 if (lhs->expr_type == EXPR_VARIABLE
9065 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9066 && lhs->symtree->n.sym->ns != gfc_current_ns)
9067 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9069 if (lhs->ts.type == BT_DERIVED
9070 && lhs->expr_type == EXPR_VARIABLE
9071 && lhs->ts.u.derived->attr.pointer_comp
9072 && rhs->expr_type == EXPR_VARIABLE
9073 && (gfc_impure_variable (rhs->symtree->n.sym)
9074 || gfc_is_coindexed (rhs)))
9075 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9077 /* Fortran 2008, C1283. */
9078 if (gfc_is_coindexed (lhs))
9079 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9082 /* F03:7.4.1.2. */
9083 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
9084 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
9085 if (lhs->ts.type == BT_CLASS)
9087 gfc_error ("Variable must not be polymorphic in assignment at %L",
9088 &lhs->where);
9089 return false;
9092 /* F2008, Section 7.2.1.2. */
9093 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
9095 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
9096 "component in assignment at %L", &lhs->where);
9097 return false;
9100 gfc_check_assign (lhs, rhs, 1);
9101 return false;
9105 /* Given a block of code, recursively resolve everything pointed to by this
9106 code block. */
9108 static void
9109 resolve_code (gfc_code *code, gfc_namespace *ns)
9111 int omp_workshare_save;
9112 int forall_save, do_concurrent_save;
9113 code_stack frame;
9114 gfc_try t;
9116 frame.prev = cs_base;
9117 frame.head = code;
9118 cs_base = &frame;
9120 find_reachable_labels (code);
9122 for (; code; code = code->next)
9124 frame.current = code;
9125 forall_save = forall_flag;
9126 do_concurrent_save = do_concurrent_flag;
9128 if (code->op == EXEC_FORALL)
9130 forall_flag = 1;
9131 gfc_resolve_forall (code, ns, forall_save);
9132 forall_flag = 2;
9134 else if (code->block)
9136 omp_workshare_save = -1;
9137 switch (code->op)
9139 case EXEC_OMP_PARALLEL_WORKSHARE:
9140 omp_workshare_save = omp_workshare_flag;
9141 omp_workshare_flag = 1;
9142 gfc_resolve_omp_parallel_blocks (code, ns);
9143 break;
9144 case EXEC_OMP_PARALLEL:
9145 case EXEC_OMP_PARALLEL_DO:
9146 case EXEC_OMP_PARALLEL_SECTIONS:
9147 case EXEC_OMP_TASK:
9148 omp_workshare_save = omp_workshare_flag;
9149 omp_workshare_flag = 0;
9150 gfc_resolve_omp_parallel_blocks (code, ns);
9151 break;
9152 case EXEC_OMP_DO:
9153 gfc_resolve_omp_do_blocks (code, ns);
9154 break;
9155 case EXEC_SELECT_TYPE:
9156 /* Blocks are handled in resolve_select_type because we have
9157 to transform the SELECT TYPE into ASSOCIATE first. */
9158 break;
9159 case EXEC_DO_CONCURRENT:
9160 do_concurrent_flag = 1;
9161 gfc_resolve_blocks (code->block, ns);
9162 do_concurrent_flag = 2;
9163 break;
9164 case EXEC_OMP_WORKSHARE:
9165 omp_workshare_save = omp_workshare_flag;
9166 omp_workshare_flag = 1;
9167 /* FALLTHROUGH */
9168 default:
9169 gfc_resolve_blocks (code->block, ns);
9170 break;
9173 if (omp_workshare_save != -1)
9174 omp_workshare_flag = omp_workshare_save;
9177 t = SUCCESS;
9178 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
9179 t = gfc_resolve_expr (code->expr1);
9180 forall_flag = forall_save;
9181 do_concurrent_flag = do_concurrent_save;
9183 if (gfc_resolve_expr (code->expr2) == FAILURE)
9184 t = FAILURE;
9186 if (code->op == EXEC_ALLOCATE
9187 && gfc_resolve_expr (code->expr3) == FAILURE)
9188 t = FAILURE;
9190 switch (code->op)
9192 case EXEC_NOP:
9193 case EXEC_END_BLOCK:
9194 case EXEC_END_NESTED_BLOCK:
9195 case EXEC_CYCLE:
9196 case EXEC_PAUSE:
9197 case EXEC_STOP:
9198 case EXEC_ERROR_STOP:
9199 case EXEC_EXIT:
9200 case EXEC_CONTINUE:
9201 case EXEC_DT_END:
9202 case EXEC_ASSIGN_CALL:
9203 case EXEC_CRITICAL:
9204 break;
9206 case EXEC_SYNC_ALL:
9207 case EXEC_SYNC_IMAGES:
9208 case EXEC_SYNC_MEMORY:
9209 resolve_sync (code);
9210 break;
9212 case EXEC_LOCK:
9213 case EXEC_UNLOCK:
9214 resolve_lock_unlock (code);
9215 break;
9217 case EXEC_ENTRY:
9218 /* Keep track of which entry we are up to. */
9219 current_entry_id = code->ext.entry->id;
9220 break;
9222 case EXEC_WHERE:
9223 resolve_where (code, NULL);
9224 break;
9226 case EXEC_GOTO:
9227 if (code->expr1 != NULL)
9229 if (code->expr1->ts.type != BT_INTEGER)
9230 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9231 "INTEGER variable", &code->expr1->where);
9232 else if (code->expr1->symtree->n.sym->attr.assign != 1)
9233 gfc_error ("Variable '%s' has not been assigned a target "
9234 "label at %L", code->expr1->symtree->n.sym->name,
9235 &code->expr1->where);
9237 else
9238 resolve_branch (code->label1, code);
9239 break;
9241 case EXEC_RETURN:
9242 if (code->expr1 != NULL
9243 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
9244 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
9245 "INTEGER return specifier", &code->expr1->where);
9246 break;
9248 case EXEC_INIT_ASSIGN:
9249 case EXEC_END_PROCEDURE:
9250 break;
9252 case EXEC_ASSIGN:
9253 if (t == FAILURE)
9254 break;
9256 if (gfc_check_vardef_context (code->expr1, false, false,
9257 _("assignment")) == FAILURE)
9258 break;
9260 if (resolve_ordinary_assign (code, ns))
9262 if (code->op == EXEC_COMPCALL)
9263 goto compcall;
9264 else
9265 goto call;
9267 break;
9269 case EXEC_LABEL_ASSIGN:
9270 if (code->label1->defined == ST_LABEL_UNKNOWN)
9271 gfc_error ("Label %d referenced at %L is never defined",
9272 code->label1->value, &code->label1->where);
9273 if (t == SUCCESS
9274 && (code->expr1->expr_type != EXPR_VARIABLE
9275 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
9276 || code->expr1->symtree->n.sym->ts.kind
9277 != gfc_default_integer_kind
9278 || code->expr1->symtree->n.sym->as != NULL))
9279 gfc_error ("ASSIGN statement at %L requires a scalar "
9280 "default INTEGER variable", &code->expr1->where);
9281 break;
9283 case EXEC_POINTER_ASSIGN:
9285 gfc_expr* e;
9287 if (t == FAILURE)
9288 break;
9290 /* This is both a variable definition and pointer assignment
9291 context, so check both of them. For rank remapping, a final
9292 array ref may be present on the LHS and fool gfc_expr_attr
9293 used in gfc_check_vardef_context. Remove it. */
9294 e = remove_last_array_ref (code->expr1);
9295 t = gfc_check_vardef_context (e, true, false,
9296 _("pointer assignment"));
9297 if (t == SUCCESS)
9298 t = gfc_check_vardef_context (e, false, false,
9299 _("pointer assignment"));
9300 gfc_free_expr (e);
9301 if (t == FAILURE)
9302 break;
9304 gfc_check_pointer_assign (code->expr1, code->expr2);
9305 break;
9308 case EXEC_ARITHMETIC_IF:
9309 if (t == SUCCESS
9310 && code->expr1->ts.type != BT_INTEGER
9311 && code->expr1->ts.type != BT_REAL)
9312 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9313 "expression", &code->expr1->where);
9315 resolve_branch (code->label1, code);
9316 resolve_branch (code->label2, code);
9317 resolve_branch (code->label3, code);
9318 break;
9320 case EXEC_IF:
9321 if (t == SUCCESS && code->expr1 != NULL
9322 && (code->expr1->ts.type != BT_LOGICAL
9323 || code->expr1->rank != 0))
9324 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9325 &code->expr1->where);
9326 break;
9328 case EXEC_CALL:
9329 call:
9330 resolve_call (code);
9331 break;
9333 case EXEC_COMPCALL:
9334 compcall:
9335 resolve_typebound_subroutine (code);
9336 break;
9338 case EXEC_CALL_PPC:
9339 resolve_ppc_call (code);
9340 break;
9342 case EXEC_SELECT:
9343 /* Select is complicated. Also, a SELECT construct could be
9344 a transformed computed GOTO. */
9345 resolve_select (code);
9346 break;
9348 case EXEC_SELECT_TYPE:
9349 resolve_select_type (code, ns);
9350 break;
9352 case EXEC_BLOCK:
9353 resolve_block_construct (code);
9354 break;
9356 case EXEC_DO:
9357 if (code->ext.iterator != NULL)
9359 gfc_iterator *iter = code->ext.iterator;
9360 if (gfc_resolve_iterator (iter, true) != FAILURE)
9361 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
9363 break;
9365 case EXEC_DO_WHILE:
9366 if (code->expr1 == NULL)
9367 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9368 if (t == SUCCESS
9369 && (code->expr1->rank != 0
9370 || code->expr1->ts.type != BT_LOGICAL))
9371 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9372 "a scalar LOGICAL expression", &code->expr1->where);
9373 break;
9375 case EXEC_ALLOCATE:
9376 if (t == SUCCESS)
9377 resolve_allocate_deallocate (code, "ALLOCATE");
9379 break;
9381 case EXEC_DEALLOCATE:
9382 if (t == SUCCESS)
9383 resolve_allocate_deallocate (code, "DEALLOCATE");
9385 break;
9387 case EXEC_OPEN:
9388 if (gfc_resolve_open (code->ext.open) == FAILURE)
9389 break;
9391 resolve_branch (code->ext.open->err, code);
9392 break;
9394 case EXEC_CLOSE:
9395 if (gfc_resolve_close (code->ext.close) == FAILURE)
9396 break;
9398 resolve_branch (code->ext.close->err, code);
9399 break;
9401 case EXEC_BACKSPACE:
9402 case EXEC_ENDFILE:
9403 case EXEC_REWIND:
9404 case EXEC_FLUSH:
9405 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
9406 break;
9408 resolve_branch (code->ext.filepos->err, code);
9409 break;
9411 case EXEC_INQUIRE:
9412 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
9413 break;
9415 resolve_branch (code->ext.inquire->err, code);
9416 break;
9418 case EXEC_IOLENGTH:
9419 gcc_assert (code->ext.inquire != NULL);
9420 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
9421 break;
9423 resolve_branch (code->ext.inquire->err, code);
9424 break;
9426 case EXEC_WAIT:
9427 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
9428 break;
9430 resolve_branch (code->ext.wait->err, code);
9431 resolve_branch (code->ext.wait->end, code);
9432 resolve_branch (code->ext.wait->eor, code);
9433 break;
9435 case EXEC_READ:
9436 case EXEC_WRITE:
9437 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
9438 break;
9440 resolve_branch (code->ext.dt->err, code);
9441 resolve_branch (code->ext.dt->end, code);
9442 resolve_branch (code->ext.dt->eor, code);
9443 break;
9445 case EXEC_TRANSFER:
9446 resolve_transfer (code);
9447 break;
9449 case EXEC_DO_CONCURRENT:
9450 case EXEC_FORALL:
9451 resolve_forall_iterators (code->ext.forall_iterator);
9453 if (code->expr1 != NULL
9454 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
9455 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
9456 "expression", &code->expr1->where);
9457 break;
9459 case EXEC_OMP_ATOMIC:
9460 case EXEC_OMP_BARRIER:
9461 case EXEC_OMP_CRITICAL:
9462 case EXEC_OMP_FLUSH:
9463 case EXEC_OMP_DO:
9464 case EXEC_OMP_MASTER:
9465 case EXEC_OMP_ORDERED:
9466 case EXEC_OMP_SECTIONS:
9467 case EXEC_OMP_SINGLE:
9468 case EXEC_OMP_TASKWAIT:
9469 case EXEC_OMP_TASKYIELD:
9470 case EXEC_OMP_WORKSHARE:
9471 gfc_resolve_omp_directive (code, ns);
9472 break;
9474 case EXEC_OMP_PARALLEL:
9475 case EXEC_OMP_PARALLEL_DO:
9476 case EXEC_OMP_PARALLEL_SECTIONS:
9477 case EXEC_OMP_PARALLEL_WORKSHARE:
9478 case EXEC_OMP_TASK:
9479 omp_workshare_save = omp_workshare_flag;
9480 omp_workshare_flag = 0;
9481 gfc_resolve_omp_directive (code, ns);
9482 omp_workshare_flag = omp_workshare_save;
9483 break;
9485 default:
9486 gfc_internal_error ("resolve_code(): Bad statement code");
9490 cs_base = frame.prev;
9494 /* Resolve initial values and make sure they are compatible with
9495 the variable. */
9497 static void
9498 resolve_values (gfc_symbol *sym)
9500 gfc_try t;
9502 if (sym->value == NULL)
9503 return;
9505 if (sym->value->expr_type == EXPR_STRUCTURE)
9506 t= resolve_structure_cons (sym->value, 1);
9507 else
9508 t = gfc_resolve_expr (sym->value);
9510 if (t == FAILURE)
9511 return;
9513 gfc_check_assign_symbol (sym, sym->value);
9517 /* Verify the binding labels for common blocks that are BIND(C). The label
9518 for a BIND(C) common block must be identical in all scoping units in which
9519 the common block is declared. Further, the binding label can not collide
9520 with any other global entity in the program. */
9522 static void
9523 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
9525 if (comm_block_tree->n.common->is_bind_c == 1)
9527 gfc_gsymbol *binding_label_gsym;
9528 gfc_gsymbol *comm_name_gsym;
9530 /* See if a global symbol exists by the common block's name. It may
9531 be NULL if the common block is use-associated. */
9532 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
9533 comm_block_tree->n.common->name);
9534 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
9535 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
9536 "with the global entity '%s' at %L",
9537 comm_block_tree->n.common->binding_label,
9538 comm_block_tree->n.common->name,
9539 &(comm_block_tree->n.common->where),
9540 comm_name_gsym->name, &(comm_name_gsym->where));
9541 else if (comm_name_gsym != NULL
9542 && strcmp (comm_name_gsym->name,
9543 comm_block_tree->n.common->name) == 0)
9545 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
9546 as expected. */
9547 if (comm_name_gsym->binding_label == NULL)
9548 /* No binding label for common block stored yet; save this one. */
9549 comm_name_gsym->binding_label =
9550 comm_block_tree->n.common->binding_label;
9551 else
9552 if (strcmp (comm_name_gsym->binding_label,
9553 comm_block_tree->n.common->binding_label) != 0)
9555 /* Common block names match but binding labels do not. */
9556 gfc_error ("Binding label '%s' for common block '%s' at %L "
9557 "does not match the binding label '%s' for common "
9558 "block '%s' at %L",
9559 comm_block_tree->n.common->binding_label,
9560 comm_block_tree->n.common->name,
9561 &(comm_block_tree->n.common->where),
9562 comm_name_gsym->binding_label,
9563 comm_name_gsym->name,
9564 &(comm_name_gsym->where));
9565 return;
9569 /* There is no binding label (NAME="") so we have nothing further to
9570 check and nothing to add as a global symbol for the label. */
9571 if (comm_block_tree->n.common->binding_label[0] == '\0' )
9572 return;
9574 binding_label_gsym =
9575 gfc_find_gsymbol (gfc_gsym_root,
9576 comm_block_tree->n.common->binding_label);
9577 if (binding_label_gsym == NULL)
9579 /* Need to make a global symbol for the binding label to prevent
9580 it from colliding with another. */
9581 binding_label_gsym =
9582 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
9583 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
9584 binding_label_gsym->type = GSYM_COMMON;
9586 else
9588 /* If comm_name_gsym is NULL, the name common block is use
9589 associated and the name could be colliding. */
9590 if (binding_label_gsym->type != GSYM_COMMON)
9591 gfc_error ("Binding label '%s' for common block '%s' at %L "
9592 "collides with the global entity '%s' at %L",
9593 comm_block_tree->n.common->binding_label,
9594 comm_block_tree->n.common->name,
9595 &(comm_block_tree->n.common->where),
9596 binding_label_gsym->name,
9597 &(binding_label_gsym->where));
9598 else if (comm_name_gsym != NULL
9599 && (strcmp (binding_label_gsym->name,
9600 comm_name_gsym->binding_label) != 0)
9601 && (strcmp (binding_label_gsym->sym_name,
9602 comm_name_gsym->name) != 0))
9603 gfc_error ("Binding label '%s' for common block '%s' at %L "
9604 "collides with global entity '%s' at %L",
9605 binding_label_gsym->name, binding_label_gsym->sym_name,
9606 &(comm_block_tree->n.common->where),
9607 comm_name_gsym->name, &(comm_name_gsym->where));
9611 return;
9615 /* Verify any BIND(C) derived types in the namespace so we can report errors
9616 for them once, rather than for each variable declared of that type. */
9618 static void
9619 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
9621 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
9622 && derived_sym->attr.is_bind_c == 1)
9623 verify_bind_c_derived_type (derived_sym);
9625 return;
9629 /* Verify that any binding labels used in a given namespace do not collide
9630 with the names or binding labels of any global symbols. */
9632 static void
9633 gfc_verify_binding_labels (gfc_symbol *sym)
9635 int has_error = 0;
9637 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
9638 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
9640 gfc_gsymbol *bind_c_sym;
9642 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
9643 if (bind_c_sym != NULL
9644 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
9646 if (sym->attr.if_source == IFSRC_DECL
9647 && (bind_c_sym->type != GSYM_SUBROUTINE
9648 && bind_c_sym->type != GSYM_FUNCTION)
9649 && ((sym->attr.contained == 1
9650 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
9651 || (sym->attr.use_assoc == 1
9652 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
9654 /* Make sure global procedures don't collide with anything. */
9655 gfc_error ("Binding label '%s' at %L collides with the global "
9656 "entity '%s' at %L", sym->binding_label,
9657 &(sym->declared_at), bind_c_sym->name,
9658 &(bind_c_sym->where));
9659 has_error = 1;
9661 else if (sym->attr.contained == 0
9662 && (sym->attr.if_source == IFSRC_IFBODY
9663 && sym->attr.flavor == FL_PROCEDURE)
9664 && (bind_c_sym->sym_name != NULL
9665 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
9667 /* Make sure procedures in interface bodies don't collide. */
9668 gfc_error ("Binding label '%s' in interface body at %L collides "
9669 "with the global entity '%s' at %L",
9670 sym->binding_label,
9671 &(sym->declared_at), bind_c_sym->name,
9672 &(bind_c_sym->where));
9673 has_error = 1;
9675 else if (sym->attr.contained == 0
9676 && sym->attr.if_source == IFSRC_UNKNOWN)
9677 if ((sym->attr.use_assoc && bind_c_sym->mod_name
9678 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
9679 || sym->attr.use_assoc == 0)
9681 gfc_error ("Binding label '%s' at %L collides with global "
9682 "entity '%s' at %L", sym->binding_label,
9683 &(sym->declared_at), bind_c_sym->name,
9684 &(bind_c_sym->where));
9685 has_error = 1;
9688 if (has_error != 0)
9689 /* Clear the binding label to prevent checking multiple times. */
9690 sym->binding_label[0] = '\0';
9692 else if (bind_c_sym == NULL)
9694 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
9695 bind_c_sym->where = sym->declared_at;
9696 bind_c_sym->sym_name = sym->name;
9698 if (sym->attr.use_assoc == 1)
9699 bind_c_sym->mod_name = sym->module;
9700 else
9701 if (sym->ns->proc_name != NULL)
9702 bind_c_sym->mod_name = sym->ns->proc_name->name;
9704 if (sym->attr.contained == 0)
9706 if (sym->attr.subroutine)
9707 bind_c_sym->type = GSYM_SUBROUTINE;
9708 else if (sym->attr.function)
9709 bind_c_sym->type = GSYM_FUNCTION;
9713 return;
9717 /* Resolve an index expression. */
9719 static gfc_try
9720 resolve_index_expr (gfc_expr *e)
9722 if (gfc_resolve_expr (e) == FAILURE)
9723 return FAILURE;
9725 if (gfc_simplify_expr (e, 0) == FAILURE)
9726 return FAILURE;
9728 if (gfc_specification_expr (e) == FAILURE)
9729 return FAILURE;
9731 return SUCCESS;
9735 /* Resolve a charlen structure. */
9737 static gfc_try
9738 resolve_charlen (gfc_charlen *cl)
9740 int i, k;
9742 if (cl->resolved)
9743 return SUCCESS;
9745 cl->resolved = 1;
9747 specification_expr = 1;
9749 if (resolve_index_expr (cl->length) == FAILURE)
9751 specification_expr = 0;
9752 return FAILURE;
9755 /* "If the character length parameter value evaluates to a negative
9756 value, the length of character entities declared is zero." */
9757 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
9759 if (gfc_option.warn_surprising)
9760 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
9761 " the length has been set to zero",
9762 &cl->length->where, i);
9763 gfc_replace_expr (cl->length,
9764 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
9767 /* Check that the character length is not too large. */
9768 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
9769 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
9770 && cl->length->ts.type == BT_INTEGER
9771 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
9773 gfc_error ("String length at %L is too large", &cl->length->where);
9774 return FAILURE;
9777 return SUCCESS;
9781 /* Test for non-constant shape arrays. */
9783 static bool
9784 is_non_constant_shape_array (gfc_symbol *sym)
9786 gfc_expr *e;
9787 int i;
9788 bool not_constant;
9790 not_constant = false;
9791 if (sym->as != NULL)
9793 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
9794 has not been simplified; parameter array references. Do the
9795 simplification now. */
9796 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
9798 e = sym->as->lower[i];
9799 if (e && (resolve_index_expr (e) == FAILURE
9800 || !gfc_is_constant_expr (e)))
9801 not_constant = true;
9802 e = sym->as->upper[i];
9803 if (e && (resolve_index_expr (e) == FAILURE
9804 || !gfc_is_constant_expr (e)))
9805 not_constant = true;
9808 return not_constant;
9811 /* Given a symbol and an initialization expression, add code to initialize
9812 the symbol to the function entry. */
9813 static void
9814 build_init_assign (gfc_symbol *sym, gfc_expr *init)
9816 gfc_expr *lval;
9817 gfc_code *init_st;
9818 gfc_namespace *ns = sym->ns;
9820 /* Search for the function namespace if this is a contained
9821 function without an explicit result. */
9822 if (sym->attr.function && sym == sym->result
9823 && sym->name != sym->ns->proc_name->name)
9825 ns = ns->contained;
9826 for (;ns; ns = ns->sibling)
9827 if (strcmp (ns->proc_name->name, sym->name) == 0)
9828 break;
9831 if (ns == NULL)
9833 gfc_free_expr (init);
9834 return;
9837 /* Build an l-value expression for the result. */
9838 lval = gfc_lval_expr_from_sym (sym);
9840 /* Add the code at scope entry. */
9841 init_st = gfc_get_code ();
9842 init_st->next = ns->code;
9843 ns->code = init_st;
9845 /* Assign the default initializer to the l-value. */
9846 init_st->loc = sym->declared_at;
9847 init_st->op = EXEC_INIT_ASSIGN;
9848 init_st->expr1 = lval;
9849 init_st->expr2 = init;
9852 /* Assign the default initializer to a derived type variable or result. */
9854 static void
9855 apply_default_init (gfc_symbol *sym)
9857 gfc_expr *init = NULL;
9859 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9860 return;
9862 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
9863 init = gfc_default_initializer (&sym->ts);
9865 if (init == NULL && sym->ts.type != BT_CLASS)
9866 return;
9868 build_init_assign (sym, init);
9869 sym->attr.referenced = 1;
9872 /* Build an initializer for a local integer, real, complex, logical, or
9873 character variable, based on the command line flags finit-local-zero,
9874 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
9875 null if the symbol should not have a default initialization. */
9876 static gfc_expr *
9877 build_default_init_expr (gfc_symbol *sym)
9879 int char_len;
9880 gfc_expr *init_expr;
9881 int i;
9883 /* These symbols should never have a default initialization. */
9884 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
9885 || sym->attr.external
9886 || sym->attr.dummy
9887 || sym->attr.pointer
9888 || sym->attr.in_equivalence
9889 || sym->attr.in_common
9890 || sym->attr.data
9891 || sym->module
9892 || sym->attr.cray_pointee
9893 || sym->attr.cray_pointer)
9894 return NULL;
9896 /* Now we'll try to build an initializer expression. */
9897 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
9898 &sym->declared_at);
9900 /* We will only initialize integers, reals, complex, logicals, and
9901 characters, and only if the corresponding command-line flags
9902 were set. Otherwise, we free init_expr and return null. */
9903 switch (sym->ts.type)
9905 case BT_INTEGER:
9906 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
9907 mpz_set_si (init_expr->value.integer,
9908 gfc_option.flag_init_integer_value);
9909 else
9911 gfc_free_expr (init_expr);
9912 init_expr = NULL;
9914 break;
9916 case BT_REAL:
9917 switch (gfc_option.flag_init_real)
9919 case GFC_INIT_REAL_SNAN:
9920 init_expr->is_snan = 1;
9921 /* Fall through. */
9922 case GFC_INIT_REAL_NAN:
9923 mpfr_set_nan (init_expr->value.real);
9924 break;
9926 case GFC_INIT_REAL_INF:
9927 mpfr_set_inf (init_expr->value.real, 1);
9928 break;
9930 case GFC_INIT_REAL_NEG_INF:
9931 mpfr_set_inf (init_expr->value.real, -1);
9932 break;
9934 case GFC_INIT_REAL_ZERO:
9935 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
9936 break;
9938 default:
9939 gfc_free_expr (init_expr);
9940 init_expr = NULL;
9941 break;
9943 break;
9945 case BT_COMPLEX:
9946 switch (gfc_option.flag_init_real)
9948 case GFC_INIT_REAL_SNAN:
9949 init_expr->is_snan = 1;
9950 /* Fall through. */
9951 case GFC_INIT_REAL_NAN:
9952 mpfr_set_nan (mpc_realref (init_expr->value.complex));
9953 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
9954 break;
9956 case GFC_INIT_REAL_INF:
9957 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
9958 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
9959 break;
9961 case GFC_INIT_REAL_NEG_INF:
9962 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
9963 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
9964 break;
9966 case GFC_INIT_REAL_ZERO:
9967 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
9968 break;
9970 default:
9971 gfc_free_expr (init_expr);
9972 init_expr = NULL;
9973 break;
9975 break;
9977 case BT_LOGICAL:
9978 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
9979 init_expr->value.logical = 0;
9980 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
9981 init_expr->value.logical = 1;
9982 else
9984 gfc_free_expr (init_expr);
9985 init_expr = NULL;
9987 break;
9989 case BT_CHARACTER:
9990 /* For characters, the length must be constant in order to
9991 create a default initializer. */
9992 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
9993 && sym->ts.u.cl->length
9994 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9996 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
9997 init_expr->value.character.length = char_len;
9998 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
9999 for (i = 0; i < char_len; i++)
10000 init_expr->value.character.string[i]
10001 = (unsigned char) gfc_option.flag_init_character_value;
10003 else
10005 gfc_free_expr (init_expr);
10006 init_expr = NULL;
10008 break;
10010 default:
10011 gfc_free_expr (init_expr);
10012 init_expr = NULL;
10014 return init_expr;
10017 /* Add an initialization expression to a local variable. */
10018 static void
10019 apply_default_init_local (gfc_symbol *sym)
10021 gfc_expr *init = NULL;
10023 /* The symbol should be a variable or a function return value. */
10024 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10025 || (sym->attr.function && sym->result != sym))
10026 return;
10028 /* Try to build the initializer expression. If we can't initialize
10029 this symbol, then init will be NULL. */
10030 init = build_default_init_expr (sym);
10031 if (init == NULL)
10032 return;
10034 /* For saved variables, we don't want to add an initializer at
10035 function entry, so we just add a static initializer. */
10036 if (sym->attr.save || sym->ns->save_all
10037 || gfc_option.flag_max_stack_var_size == 0)
10039 /* Don't clobber an existing initializer! */
10040 gcc_assert (sym->value == NULL);
10041 sym->value = init;
10042 return;
10045 build_init_assign (sym, init);
10049 /* Resolution of common features of flavors variable and procedure. */
10051 static gfc_try
10052 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
10054 /* Avoid double diagnostics for function result symbols. */
10055 if ((sym->result || sym->attr.result) && !sym->attr.dummy
10056 && (sym->ns != gfc_current_ns))
10057 return SUCCESS;
10059 /* Constraints on deferred shape variable. */
10060 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
10062 if (sym->attr.allocatable)
10064 if (sym->attr.dimension)
10066 gfc_error ("Allocatable array '%s' at %L must have "
10067 "a deferred shape", sym->name, &sym->declared_at);
10068 return FAILURE;
10070 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
10071 "may not be ALLOCATABLE", sym->name,
10072 &sym->declared_at) == FAILURE)
10073 return FAILURE;
10076 if (sym->attr.pointer && sym->attr.dimension)
10078 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
10079 sym->name, &sym->declared_at);
10080 return FAILURE;
10083 else
10085 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
10086 && sym->ts.type != BT_CLASS && !sym->assoc)
10088 gfc_error ("Array '%s' at %L cannot have a deferred shape",
10089 sym->name, &sym->declared_at);
10090 return FAILURE;
10094 /* Constraints on polymorphic variables. */
10095 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
10097 /* F03:C502. */
10098 if (sym->attr.class_ok
10099 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
10101 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
10102 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
10103 &sym->declared_at);
10104 return FAILURE;
10107 /* F03:C509. */
10108 /* Assume that use associated symbols were checked in the module ns.
10109 Class-variables that are associate-names are also something special
10110 and excepted from the test. */
10111 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
10113 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
10114 "or pointer", sym->name, &sym->declared_at);
10115 return FAILURE;
10119 return SUCCESS;
10123 /* Additional checks for symbols with flavor variable and derived
10124 type. To be called from resolve_fl_variable. */
10126 static gfc_try
10127 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
10129 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
10131 /* Check to see if a derived type is blocked from being host
10132 associated by the presence of another class I symbol in the same
10133 namespace. 14.6.1.3 of the standard and the discussion on
10134 comp.lang.fortran. */
10135 if (sym->ns != sym->ts.u.derived->ns
10136 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
10138 gfc_symbol *s;
10139 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
10140 if (s && s->attr.flavor != FL_DERIVED)
10142 gfc_error ("The type '%s' cannot be host associated at %L "
10143 "because it is blocked by an incompatible object "
10144 "of the same name declared at %L",
10145 sym->ts.u.derived->name, &sym->declared_at,
10146 &s->declared_at);
10147 return FAILURE;
10151 /* 4th constraint in section 11.3: "If an object of a type for which
10152 component-initialization is specified (R429) appears in the
10153 specification-part of a module and does not have the ALLOCATABLE
10154 or POINTER attribute, the object shall have the SAVE attribute."
10156 The check for initializers is performed with
10157 gfc_has_default_initializer because gfc_default_initializer generates
10158 a hidden default for allocatable components. */
10159 if (!(sym->value || no_init_flag) && sym->ns->proc_name
10160 && sym->ns->proc_name->attr.flavor == FL_MODULE
10161 && !sym->ns->save_all && !sym->attr.save
10162 && !sym->attr.pointer && !sym->attr.allocatable
10163 && gfc_has_default_initializer (sym->ts.u.derived)
10164 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Implied SAVE for "
10165 "module variable '%s' at %L, needed due to "
10166 "the default initialization", sym->name,
10167 &sym->declared_at) == FAILURE)
10168 return FAILURE;
10170 /* Assign default initializer. */
10171 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
10172 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
10174 sym->value = gfc_default_initializer (&sym->ts);
10177 return SUCCESS;
10181 /* Resolve symbols with flavor variable. */
10183 static gfc_try
10184 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
10186 int no_init_flag, automatic_flag;
10187 gfc_expr *e;
10188 const char *auto_save_msg;
10190 auto_save_msg = "Automatic object '%s' at %L cannot have the "
10191 "SAVE attribute";
10193 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
10194 return FAILURE;
10196 /* Set this flag to check that variables are parameters of all entries.
10197 This check is effected by the call to gfc_resolve_expr through
10198 is_non_constant_shape_array. */
10199 specification_expr = 1;
10201 if (sym->ns->proc_name
10202 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10203 || sym->ns->proc_name->attr.is_main_program)
10204 && !sym->attr.use_assoc
10205 && !sym->attr.allocatable
10206 && !sym->attr.pointer
10207 && is_non_constant_shape_array (sym))
10209 /* The shape of a main program or module array needs to be
10210 constant. */
10211 gfc_error ("The module or main program array '%s' at %L must "
10212 "have constant shape", sym->name, &sym->declared_at);
10213 specification_expr = 0;
10214 return FAILURE;
10217 /* Constraints on deferred type parameter. */
10218 if (sym->ts.deferred && !(sym->attr.pointer || sym->attr.allocatable))
10220 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10221 "requires either the pointer or allocatable attribute",
10222 sym->name, &sym->declared_at);
10223 return FAILURE;
10226 if (sym->ts.type == BT_CHARACTER)
10228 /* Make sure that character string variables with assumed length are
10229 dummy arguments. */
10230 e = sym->ts.u.cl->length;
10231 if (e == NULL && !sym->attr.dummy && !sym->attr.result
10232 && !sym->ts.deferred)
10234 gfc_error ("Entity with assumed character length at %L must be a "
10235 "dummy argument or a PARAMETER", &sym->declared_at);
10236 return FAILURE;
10239 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
10241 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10242 return FAILURE;
10245 if (!gfc_is_constant_expr (e)
10246 && !(e->expr_type == EXPR_VARIABLE
10247 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
10249 if (!sym->attr.use_assoc && sym->ns->proc_name
10250 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10251 || sym->ns->proc_name->attr.is_main_program))
10253 gfc_error ("'%s' at %L must have constant character length "
10254 "in this context", sym->name, &sym->declared_at);
10255 return FAILURE;
10257 if (sym->attr.in_common)
10259 gfc_error ("COMMON variable '%s' at %L must have constant "
10260 "character length", sym->name, &sym->declared_at);
10261 return FAILURE;
10266 if (sym->value == NULL && sym->attr.referenced)
10267 apply_default_init_local (sym); /* Try to apply a default initialization. */
10269 /* Determine if the symbol may not have an initializer. */
10270 no_init_flag = automatic_flag = 0;
10271 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
10272 || sym->attr.intrinsic || sym->attr.result)
10273 no_init_flag = 1;
10274 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
10275 && is_non_constant_shape_array (sym))
10277 no_init_flag = automatic_flag = 1;
10279 /* Also, they must not have the SAVE attribute.
10280 SAVE_IMPLICIT is checked below. */
10281 if (sym->as && sym->attr.codimension)
10283 int corank = sym->as->corank;
10284 sym->as->corank = 0;
10285 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
10286 sym->as->corank = corank;
10288 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
10290 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10291 return FAILURE;
10295 /* Ensure that any initializer is simplified. */
10296 if (sym->value)
10297 gfc_simplify_expr (sym->value, 1);
10299 /* Reject illegal initializers. */
10300 if (!sym->mark && sym->value)
10302 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
10303 && CLASS_DATA (sym)->attr.allocatable))
10304 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10305 sym->name, &sym->declared_at);
10306 else if (sym->attr.external)
10307 gfc_error ("External '%s' at %L cannot have an initializer",
10308 sym->name, &sym->declared_at);
10309 else if (sym->attr.dummy
10310 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
10311 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10312 sym->name, &sym->declared_at);
10313 else if (sym->attr.intrinsic)
10314 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10315 sym->name, &sym->declared_at);
10316 else if (sym->attr.result)
10317 gfc_error ("Function result '%s' at %L cannot have an initializer",
10318 sym->name, &sym->declared_at);
10319 else if (automatic_flag)
10320 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10321 sym->name, &sym->declared_at);
10322 else
10323 goto no_init_error;
10324 return FAILURE;
10327 no_init_error:
10328 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
10329 return resolve_fl_variable_derived (sym, no_init_flag);
10331 return SUCCESS;
10335 /* Resolve a procedure. */
10337 static gfc_try
10338 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
10340 gfc_formal_arglist *arg;
10342 if (sym->attr.function
10343 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
10344 return FAILURE;
10346 if (sym->ts.type == BT_CHARACTER)
10348 gfc_charlen *cl = sym->ts.u.cl;
10350 if (cl && cl->length && gfc_is_constant_expr (cl->length)
10351 && resolve_charlen (cl) == FAILURE)
10352 return FAILURE;
10354 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10355 && sym->attr.proc == PROC_ST_FUNCTION)
10357 gfc_error ("Character-valued statement function '%s' at %L must "
10358 "have constant length", sym->name, &sym->declared_at);
10359 return FAILURE;
10363 /* Ensure that derived type for are not of a private type. Internal
10364 module procedures are excluded by 2.2.3.3 - i.e., they are not
10365 externally accessible and can access all the objects accessible in
10366 the host. */
10367 if (!(sym->ns->parent
10368 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10369 && gfc_check_symbol_access (sym))
10371 gfc_interface *iface;
10373 for (arg = sym->formal; arg; arg = arg->next)
10375 if (arg->sym
10376 && arg->sym->ts.type == BT_DERIVED
10377 && !arg->sym->ts.u.derived->attr.use_assoc
10378 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10379 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
10380 "PRIVATE type and cannot be a dummy argument"
10381 " of '%s', which is PUBLIC at %L",
10382 arg->sym->name, sym->name, &sym->declared_at)
10383 == FAILURE)
10385 /* Stop this message from recurring. */
10386 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10387 return FAILURE;
10391 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10392 PRIVATE to the containing module. */
10393 for (iface = sym->generic; iface; iface = iface->next)
10395 for (arg = iface->sym->formal; arg; arg = arg->next)
10397 if (arg->sym
10398 && arg->sym->ts.type == BT_DERIVED
10399 && !arg->sym->ts.u.derived->attr.use_assoc
10400 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10401 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
10402 "'%s' in PUBLIC interface '%s' at %L "
10403 "takes dummy arguments of '%s' which is "
10404 "PRIVATE", iface->sym->name, sym->name,
10405 &iface->sym->declared_at,
10406 gfc_typename (&arg->sym->ts)) == FAILURE)
10408 /* Stop this message from recurring. */
10409 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10410 return FAILURE;
10415 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10416 PRIVATE to the containing module. */
10417 for (iface = sym->generic; iface; iface = iface->next)
10419 for (arg = iface->sym->formal; arg; arg = arg->next)
10421 if (arg->sym
10422 && arg->sym->ts.type == BT_DERIVED
10423 && !arg->sym->ts.u.derived->attr.use_assoc
10424 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10425 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
10426 "'%s' in PUBLIC interface '%s' at %L "
10427 "takes dummy arguments of '%s' which is "
10428 "PRIVATE", iface->sym->name, sym->name,
10429 &iface->sym->declared_at,
10430 gfc_typename (&arg->sym->ts)) == FAILURE)
10432 /* Stop this message from recurring. */
10433 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10434 return FAILURE;
10440 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
10441 && !sym->attr.proc_pointer)
10443 gfc_error ("Function '%s' at %L cannot have an initializer",
10444 sym->name, &sym->declared_at);
10445 return FAILURE;
10448 /* An external symbol may not have an initializer because it is taken to be
10449 a procedure. Exception: Procedure Pointers. */
10450 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
10452 gfc_error ("External object '%s' at %L may not have an initializer",
10453 sym->name, &sym->declared_at);
10454 return FAILURE;
10457 /* An elemental function is required to return a scalar 12.7.1 */
10458 if (sym->attr.elemental && sym->attr.function && sym->as)
10460 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
10461 "result", sym->name, &sym->declared_at);
10462 /* Reset so that the error only occurs once. */
10463 sym->attr.elemental = 0;
10464 return FAILURE;
10467 if (sym->attr.proc == PROC_ST_FUNCTION
10468 && (sym->attr.allocatable || sym->attr.pointer))
10470 gfc_error ("Statement function '%s' at %L may not have pointer or "
10471 "allocatable attribute", sym->name, &sym->declared_at);
10472 return FAILURE;
10475 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
10476 char-len-param shall not be array-valued, pointer-valued, recursive
10477 or pure. ....snip... A character value of * may only be used in the
10478 following ways: (i) Dummy arg of procedure - dummy associates with
10479 actual length; (ii) To declare a named constant; or (iii) External
10480 function - but length must be declared in calling scoping unit. */
10481 if (sym->attr.function
10482 && sym->ts.type == BT_CHARACTER
10483 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
10485 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
10486 || (sym->attr.recursive) || (sym->attr.pure))
10488 if (sym->as && sym->as->rank)
10489 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10490 "array-valued", sym->name, &sym->declared_at);
10492 if (sym->attr.pointer)
10493 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10494 "pointer-valued", sym->name, &sym->declared_at);
10496 if (sym->attr.pure)
10497 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10498 "pure", sym->name, &sym->declared_at);
10500 if (sym->attr.recursive)
10501 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10502 "recursive", sym->name, &sym->declared_at);
10504 return FAILURE;
10507 /* Appendix B.2 of the standard. Contained functions give an
10508 error anyway. Fixed-form is likely to be F77/legacy. Deferred
10509 character length is an F2003 feature. */
10510 if (!sym->attr.contained
10511 && gfc_current_form != FORM_FIXED
10512 && !sym->ts.deferred)
10513 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
10514 "CHARACTER(*) function '%s' at %L",
10515 sym->name, &sym->declared_at);
10518 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
10520 gfc_formal_arglist *curr_arg;
10521 int has_non_interop_arg = 0;
10523 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
10524 sym->common_block) == FAILURE)
10526 /* Clear these to prevent looking at them again if there was an
10527 error. */
10528 sym->attr.is_bind_c = 0;
10529 sym->attr.is_c_interop = 0;
10530 sym->ts.is_c_interop = 0;
10532 else
10534 /* So far, no errors have been found. */
10535 sym->attr.is_c_interop = 1;
10536 sym->ts.is_c_interop = 1;
10539 curr_arg = sym->formal;
10540 while (curr_arg != NULL)
10542 /* Skip implicitly typed dummy args here. */
10543 if (curr_arg->sym->attr.implicit_type == 0)
10544 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
10545 /* If something is found to fail, record the fact so we
10546 can mark the symbol for the procedure as not being
10547 BIND(C) to try and prevent multiple errors being
10548 reported. */
10549 has_non_interop_arg = 1;
10551 curr_arg = curr_arg->next;
10554 /* See if any of the arguments were not interoperable and if so, clear
10555 the procedure symbol to prevent duplicate error messages. */
10556 if (has_non_interop_arg != 0)
10558 sym->attr.is_c_interop = 0;
10559 sym->ts.is_c_interop = 0;
10560 sym->attr.is_bind_c = 0;
10564 if (!sym->attr.proc_pointer)
10566 if (sym->attr.save == SAVE_EXPLICIT)
10568 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
10569 "in '%s' at %L", sym->name, &sym->declared_at);
10570 return FAILURE;
10572 if (sym->attr.intent)
10574 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
10575 "in '%s' at %L", sym->name, &sym->declared_at);
10576 return FAILURE;
10578 if (sym->attr.subroutine && sym->attr.result)
10580 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
10581 "in '%s' at %L", sym->name, &sym->declared_at);
10582 return FAILURE;
10584 if (sym->attr.external && sym->attr.function
10585 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
10586 || sym->attr.contained))
10588 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
10589 "in '%s' at %L", sym->name, &sym->declared_at);
10590 return FAILURE;
10592 if (strcmp ("ppr@", sym->name) == 0)
10594 gfc_error ("Procedure pointer result '%s' at %L "
10595 "is missing the pointer attribute",
10596 sym->ns->proc_name->name, &sym->declared_at);
10597 return FAILURE;
10601 return SUCCESS;
10605 /* Resolve a list of finalizer procedures. That is, after they have hopefully
10606 been defined and we now know their defined arguments, check that they fulfill
10607 the requirements of the standard for procedures used as finalizers. */
10609 static gfc_try
10610 gfc_resolve_finalizers (gfc_symbol* derived)
10612 gfc_finalizer* list;
10613 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
10614 gfc_try result = SUCCESS;
10615 bool seen_scalar = false;
10617 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
10618 return SUCCESS;
10620 /* Walk over the list of finalizer-procedures, check them, and if any one
10621 does not fit in with the standard's definition, print an error and remove
10622 it from the list. */
10623 prev_link = &derived->f2k_derived->finalizers;
10624 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
10626 gfc_symbol* arg;
10627 gfc_finalizer* i;
10628 int my_rank;
10630 /* Skip this finalizer if we already resolved it. */
10631 if (list->proc_tree)
10633 prev_link = &(list->next);
10634 continue;
10637 /* Check this exists and is a SUBROUTINE. */
10638 if (!list->proc_sym->attr.subroutine)
10640 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
10641 list->proc_sym->name, &list->where);
10642 goto error;
10645 /* We should have exactly one argument. */
10646 if (!list->proc_sym->formal || list->proc_sym->formal->next)
10648 gfc_error ("FINAL procedure at %L must have exactly one argument",
10649 &list->where);
10650 goto error;
10652 arg = list->proc_sym->formal->sym;
10654 /* This argument must be of our type. */
10655 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
10657 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
10658 &arg->declared_at, derived->name);
10659 goto error;
10662 /* It must neither be a pointer nor allocatable nor optional. */
10663 if (arg->attr.pointer)
10665 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
10666 &arg->declared_at);
10667 goto error;
10669 if (arg->attr.allocatable)
10671 gfc_error ("Argument of FINAL procedure at %L must not be"
10672 " ALLOCATABLE", &arg->declared_at);
10673 goto error;
10675 if (arg->attr.optional)
10677 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
10678 &arg->declared_at);
10679 goto error;
10682 /* It must not be INTENT(OUT). */
10683 if (arg->attr.intent == INTENT_OUT)
10685 gfc_error ("Argument of FINAL procedure at %L must not be"
10686 " INTENT(OUT)", &arg->declared_at);
10687 goto error;
10690 /* Warn if the procedure is non-scalar and not assumed shape. */
10691 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
10692 && arg->as->type != AS_ASSUMED_SHAPE)
10693 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
10694 " shape argument", &arg->declared_at);
10696 /* Check that it does not match in kind and rank with a FINAL procedure
10697 defined earlier. To really loop over the *earlier* declarations,
10698 we need to walk the tail of the list as new ones were pushed at the
10699 front. */
10700 /* TODO: Handle kind parameters once they are implemented. */
10701 my_rank = (arg->as ? arg->as->rank : 0);
10702 for (i = list->next; i; i = i->next)
10704 /* Argument list might be empty; that is an error signalled earlier,
10705 but we nevertheless continued resolving. */
10706 if (i->proc_sym->formal)
10708 gfc_symbol* i_arg = i->proc_sym->formal->sym;
10709 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
10710 if (i_rank == my_rank)
10712 gfc_error ("FINAL procedure '%s' declared at %L has the same"
10713 " rank (%d) as '%s'",
10714 list->proc_sym->name, &list->where, my_rank,
10715 i->proc_sym->name);
10716 goto error;
10721 /* Is this the/a scalar finalizer procedure? */
10722 if (!arg->as || arg->as->rank == 0)
10723 seen_scalar = true;
10725 /* Find the symtree for this procedure. */
10726 gcc_assert (!list->proc_tree);
10727 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
10729 prev_link = &list->next;
10730 continue;
10732 /* Remove wrong nodes immediately from the list so we don't risk any
10733 troubles in the future when they might fail later expectations. */
10734 error:
10735 result = FAILURE;
10736 i = list;
10737 *prev_link = list->next;
10738 gfc_free_finalizer (i);
10741 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
10742 were nodes in the list, must have been for arrays. It is surely a good
10743 idea to have a scalar version there if there's something to finalize. */
10744 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
10745 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
10746 " defined at %L, suggest also scalar one",
10747 derived->name, &derived->declared_at);
10749 /* TODO: Remove this error when finalization is finished. */
10750 gfc_error ("Finalization at %L is not yet implemented",
10751 &derived->declared_at);
10753 return result;
10757 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
10759 static gfc_try
10760 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
10761 const char* generic_name, locus where)
10763 gfc_symbol* sym1;
10764 gfc_symbol* sym2;
10766 gcc_assert (t1->specific && t2->specific);
10767 gcc_assert (!t1->specific->is_generic);
10768 gcc_assert (!t2->specific->is_generic);
10770 sym1 = t1->specific->u.specific->n.sym;
10771 sym2 = t2->specific->u.specific->n.sym;
10773 if (sym1 == sym2)
10774 return SUCCESS;
10776 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
10777 if (sym1->attr.subroutine != sym2->attr.subroutine
10778 || sym1->attr.function != sym2->attr.function)
10780 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
10781 " GENERIC '%s' at %L",
10782 sym1->name, sym2->name, generic_name, &where);
10783 return FAILURE;
10786 /* Compare the interfaces. */
10787 if (gfc_compare_interfaces (sym1, sym2, sym2->name, 1, 0, NULL, 0))
10789 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
10790 sym1->name, sym2->name, generic_name, &where);
10791 return FAILURE;
10794 return SUCCESS;
10798 /* Worker function for resolving a generic procedure binding; this is used to
10799 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
10801 The difference between those cases is finding possible inherited bindings
10802 that are overridden, as one has to look for them in tb_sym_root,
10803 tb_uop_root or tb_op, respectively. Thus the caller must already find
10804 the super-type and set p->overridden correctly. */
10806 static gfc_try
10807 resolve_tb_generic_targets (gfc_symbol* super_type,
10808 gfc_typebound_proc* p, const char* name)
10810 gfc_tbp_generic* target;
10811 gfc_symtree* first_target;
10812 gfc_symtree* inherited;
10814 gcc_assert (p && p->is_generic);
10816 /* Try to find the specific bindings for the symtrees in our target-list. */
10817 gcc_assert (p->u.generic);
10818 for (target = p->u.generic; target; target = target->next)
10819 if (!target->specific)
10821 gfc_typebound_proc* overridden_tbp;
10822 gfc_tbp_generic* g;
10823 const char* target_name;
10825 target_name = target->specific_st->name;
10827 /* Defined for this type directly. */
10828 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
10830 target->specific = target->specific_st->n.tb;
10831 goto specific_found;
10834 /* Look for an inherited specific binding. */
10835 if (super_type)
10837 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
10838 true, NULL);
10840 if (inherited)
10842 gcc_assert (inherited->n.tb);
10843 target->specific = inherited->n.tb;
10844 goto specific_found;
10848 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
10849 " at %L", target_name, name, &p->where);
10850 return FAILURE;
10852 /* Once we've found the specific binding, check it is not ambiguous with
10853 other specifics already found or inherited for the same GENERIC. */
10854 specific_found:
10855 gcc_assert (target->specific);
10857 /* This must really be a specific binding! */
10858 if (target->specific->is_generic)
10860 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
10861 " '%s' is GENERIC, too", name, &p->where, target_name);
10862 return FAILURE;
10865 /* Check those already resolved on this type directly. */
10866 for (g = p->u.generic; g; g = g->next)
10867 if (g != target && g->specific
10868 && check_generic_tbp_ambiguity (target, g, name, p->where)
10869 == FAILURE)
10870 return FAILURE;
10872 /* Check for ambiguity with inherited specific targets. */
10873 for (overridden_tbp = p->overridden; overridden_tbp;
10874 overridden_tbp = overridden_tbp->overridden)
10875 if (overridden_tbp->is_generic)
10877 for (g = overridden_tbp->u.generic; g; g = g->next)
10879 gcc_assert (g->specific);
10880 if (check_generic_tbp_ambiguity (target, g,
10881 name, p->where) == FAILURE)
10882 return FAILURE;
10887 /* If we attempt to "overwrite" a specific binding, this is an error. */
10888 if (p->overridden && !p->overridden->is_generic)
10890 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
10891 " the same name", name, &p->where);
10892 return FAILURE;
10895 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
10896 all must have the same attributes here. */
10897 first_target = p->u.generic->specific->u.specific;
10898 gcc_assert (first_target);
10899 p->subroutine = first_target->n.sym->attr.subroutine;
10900 p->function = first_target->n.sym->attr.function;
10902 return SUCCESS;
10906 /* Resolve a GENERIC procedure binding for a derived type. */
10908 static gfc_try
10909 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
10911 gfc_symbol* super_type;
10913 /* Find the overridden binding if any. */
10914 st->n.tb->overridden = NULL;
10915 super_type = gfc_get_derived_super_type (derived);
10916 if (super_type)
10918 gfc_symtree* overridden;
10919 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
10920 true, NULL);
10922 if (overridden && overridden->n.tb)
10923 st->n.tb->overridden = overridden->n.tb;
10926 /* Resolve using worker function. */
10927 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
10931 /* Retrieve the target-procedure of an operator binding and do some checks in
10932 common for intrinsic and user-defined type-bound operators. */
10934 static gfc_symbol*
10935 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
10937 gfc_symbol* target_proc;
10939 gcc_assert (target->specific && !target->specific->is_generic);
10940 target_proc = target->specific->u.specific->n.sym;
10941 gcc_assert (target_proc);
10943 /* All operator bindings must have a passed-object dummy argument. */
10944 if (target->specific->nopass)
10946 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
10947 return NULL;
10950 return target_proc;
10954 /* Resolve a type-bound intrinsic operator. */
10956 static gfc_try
10957 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
10958 gfc_typebound_proc* p)
10960 gfc_symbol* super_type;
10961 gfc_tbp_generic* target;
10963 /* If there's already an error here, do nothing (but don't fail again). */
10964 if (p->error)
10965 return SUCCESS;
10967 /* Operators should always be GENERIC bindings. */
10968 gcc_assert (p->is_generic);
10970 /* Look for an overridden binding. */
10971 super_type = gfc_get_derived_super_type (derived);
10972 if (super_type && super_type->f2k_derived)
10973 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
10974 op, true, NULL);
10975 else
10976 p->overridden = NULL;
10978 /* Resolve general GENERIC properties using worker function. */
10979 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
10980 goto error;
10982 /* Check the targets to be procedures of correct interface. */
10983 for (target = p->u.generic; target; target = target->next)
10985 gfc_symbol* target_proc;
10987 target_proc = get_checked_tb_operator_target (target, p->where);
10988 if (!target_proc)
10989 goto error;
10991 if (!gfc_check_operator_interface (target_proc, op, p->where))
10992 goto error;
10995 return SUCCESS;
10997 error:
10998 p->error = 1;
10999 return FAILURE;
11003 /* Resolve a type-bound user operator (tree-walker callback). */
11005 static gfc_symbol* resolve_bindings_derived;
11006 static gfc_try resolve_bindings_result;
11008 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
11010 static void
11011 resolve_typebound_user_op (gfc_symtree* stree)
11013 gfc_symbol* super_type;
11014 gfc_tbp_generic* target;
11016 gcc_assert (stree && stree->n.tb);
11018 if (stree->n.tb->error)
11019 return;
11021 /* Operators should always be GENERIC bindings. */
11022 gcc_assert (stree->n.tb->is_generic);
11024 /* Find overridden procedure, if any. */
11025 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11026 if (super_type && super_type->f2k_derived)
11028 gfc_symtree* overridden;
11029 overridden = gfc_find_typebound_user_op (super_type, NULL,
11030 stree->name, true, NULL);
11032 if (overridden && overridden->n.tb)
11033 stree->n.tb->overridden = overridden->n.tb;
11035 else
11036 stree->n.tb->overridden = NULL;
11038 /* Resolve basically using worker function. */
11039 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
11040 == FAILURE)
11041 goto error;
11043 /* Check the targets to be functions of correct interface. */
11044 for (target = stree->n.tb->u.generic; target; target = target->next)
11046 gfc_symbol* target_proc;
11048 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11049 if (!target_proc)
11050 goto error;
11052 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
11053 goto error;
11056 return;
11058 error:
11059 resolve_bindings_result = FAILURE;
11060 stree->n.tb->error = 1;
11064 /* Resolve the type-bound procedures for a derived type. */
11066 static void
11067 resolve_typebound_procedure (gfc_symtree* stree)
11069 gfc_symbol* proc;
11070 locus where;
11071 gfc_symbol* me_arg;
11072 gfc_symbol* super_type;
11073 gfc_component* comp;
11075 gcc_assert (stree);
11077 /* Undefined specific symbol from GENERIC target definition. */
11078 if (!stree->n.tb)
11079 return;
11081 if (stree->n.tb->error)
11082 return;
11084 /* If this is a GENERIC binding, use that routine. */
11085 if (stree->n.tb->is_generic)
11087 if (resolve_typebound_generic (resolve_bindings_derived, stree)
11088 == FAILURE)
11089 goto error;
11090 return;
11093 /* Get the target-procedure to check it. */
11094 gcc_assert (!stree->n.tb->is_generic);
11095 gcc_assert (stree->n.tb->u.specific);
11096 proc = stree->n.tb->u.specific->n.sym;
11097 where = stree->n.tb->where;
11099 /* Default access should already be resolved from the parser. */
11100 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11102 /* It should be a module procedure or an external procedure with explicit
11103 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
11104 if ((!proc->attr.subroutine && !proc->attr.function)
11105 || (proc->attr.proc != PROC_MODULE
11106 && proc->attr.if_source != IFSRC_IFBODY)
11107 || (proc->attr.abstract && !stree->n.tb->deferred))
11109 gfc_error ("'%s' must be a module procedure or an external procedure with"
11110 " an explicit interface at %L", proc->name, &where);
11111 goto error;
11113 stree->n.tb->subroutine = proc->attr.subroutine;
11114 stree->n.tb->function = proc->attr.function;
11116 /* Find the super-type of the current derived type. We could do this once and
11117 store in a global if speed is needed, but as long as not I believe this is
11118 more readable and clearer. */
11119 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11121 /* If PASS, resolve and check arguments if not already resolved / loaded
11122 from a .mod file. */
11123 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11125 if (stree->n.tb->pass_arg)
11127 gfc_formal_arglist* i;
11129 /* If an explicit passing argument name is given, walk the arg-list
11130 and look for it. */
11132 me_arg = NULL;
11133 stree->n.tb->pass_arg_num = 1;
11134 for (i = proc->formal; i; i = i->next)
11136 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
11138 me_arg = i->sym;
11139 break;
11141 ++stree->n.tb->pass_arg_num;
11144 if (!me_arg)
11146 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11147 " argument '%s'",
11148 proc->name, stree->n.tb->pass_arg, &where,
11149 stree->n.tb->pass_arg);
11150 goto error;
11153 else
11155 /* Otherwise, take the first one; there should in fact be at least
11156 one. */
11157 stree->n.tb->pass_arg_num = 1;
11158 if (!proc->formal)
11160 gfc_error ("Procedure '%s' with PASS at %L must have at"
11161 " least one argument", proc->name, &where);
11162 goto error;
11164 me_arg = proc->formal->sym;
11167 /* Now check that the argument-type matches and the passed-object
11168 dummy argument is generally fine. */
11170 gcc_assert (me_arg);
11172 if (me_arg->ts.type != BT_CLASS)
11174 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11175 " at %L", proc->name, &where);
11176 goto error;
11179 if (CLASS_DATA (me_arg)->ts.u.derived
11180 != resolve_bindings_derived)
11182 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11183 " the derived-type '%s'", me_arg->name, proc->name,
11184 me_arg->name, &where, resolve_bindings_derived->name);
11185 goto error;
11188 gcc_assert (me_arg->ts.type == BT_CLASS);
11189 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank > 0)
11191 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11192 " scalar", proc->name, &where);
11193 goto error;
11195 if (CLASS_DATA (me_arg)->attr.allocatable)
11197 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11198 " be ALLOCATABLE", proc->name, &where);
11199 goto error;
11201 if (CLASS_DATA (me_arg)->attr.class_pointer)
11203 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11204 " be POINTER", proc->name, &where);
11205 goto error;
11209 /* If we are extending some type, check that we don't override a procedure
11210 flagged NON_OVERRIDABLE. */
11211 stree->n.tb->overridden = NULL;
11212 if (super_type)
11214 gfc_symtree* overridden;
11215 overridden = gfc_find_typebound_proc (super_type, NULL,
11216 stree->name, true, NULL);
11218 if (overridden)
11220 if (overridden->n.tb)
11221 stree->n.tb->overridden = overridden->n.tb;
11223 if (gfc_check_typebound_override (stree, overridden) == FAILURE)
11224 goto error;
11228 /* See if there's a name collision with a component directly in this type. */
11229 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11230 if (!strcmp (comp->name, stree->name))
11232 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11233 " '%s'",
11234 stree->name, &where, resolve_bindings_derived->name);
11235 goto error;
11238 /* Try to find a name collision with an inherited component. */
11239 if (super_type && gfc_find_component (super_type, stree->name, true, true))
11241 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11242 " component of '%s'",
11243 stree->name, &where, resolve_bindings_derived->name);
11244 goto error;
11247 stree->n.tb->error = 0;
11248 return;
11250 error:
11251 resolve_bindings_result = FAILURE;
11252 stree->n.tb->error = 1;
11256 static gfc_try
11257 resolve_typebound_procedures (gfc_symbol* derived)
11259 int op;
11260 gfc_symbol* super_type;
11262 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
11263 return SUCCESS;
11265 super_type = gfc_get_derived_super_type (derived);
11266 if (super_type)
11267 resolve_typebound_procedures (super_type);
11269 resolve_bindings_derived = derived;
11270 resolve_bindings_result = SUCCESS;
11272 /* Make sure the vtab has been generated. */
11273 gfc_find_derived_vtab (derived);
11275 if (derived->f2k_derived->tb_sym_root)
11276 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
11277 &resolve_typebound_procedure);
11279 if (derived->f2k_derived->tb_uop_root)
11280 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
11281 &resolve_typebound_user_op);
11283 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
11285 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
11286 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
11287 p) == FAILURE)
11288 resolve_bindings_result = FAILURE;
11291 return resolve_bindings_result;
11295 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11296 to give all identical derived types the same backend_decl. */
11297 static void
11298 add_dt_to_dt_list (gfc_symbol *derived)
11300 gfc_dt_list *dt_list;
11302 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
11303 if (derived == dt_list->derived)
11304 return;
11306 dt_list = gfc_get_dt_list ();
11307 dt_list->next = gfc_derived_types;
11308 dt_list->derived = derived;
11309 gfc_derived_types = dt_list;
11313 /* Ensure that a derived-type is really not abstract, meaning that every
11314 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11316 static gfc_try
11317 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
11319 if (!st)
11320 return SUCCESS;
11322 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
11323 return FAILURE;
11324 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
11325 return FAILURE;
11327 if (st->n.tb && st->n.tb->deferred)
11329 gfc_symtree* overriding;
11330 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
11331 if (!overriding)
11332 return FAILURE;
11333 gcc_assert (overriding->n.tb);
11334 if (overriding->n.tb->deferred)
11336 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11337 " '%s' is DEFERRED and not overridden",
11338 sub->name, &sub->declared_at, st->name);
11339 return FAILURE;
11343 return SUCCESS;
11346 static gfc_try
11347 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
11349 /* The algorithm used here is to recursively travel up the ancestry of sub
11350 and for each ancestor-type, check all bindings. If any of them is
11351 DEFERRED, look it up starting from sub and see if the found (overriding)
11352 binding is not DEFERRED.
11353 This is not the most efficient way to do this, but it should be ok and is
11354 clearer than something sophisticated. */
11356 gcc_assert (ancestor && !sub->attr.abstract);
11358 if (!ancestor->attr.abstract)
11359 return SUCCESS;
11361 /* Walk bindings of this ancestor. */
11362 if (ancestor->f2k_derived)
11364 gfc_try t;
11365 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
11366 if (t == FAILURE)
11367 return FAILURE;
11370 /* Find next ancestor type and recurse on it. */
11371 ancestor = gfc_get_derived_super_type (ancestor);
11372 if (ancestor)
11373 return ensure_not_abstract (sub, ancestor);
11375 return SUCCESS;
11379 /* Resolve the components of a derived type. This does not have to wait until
11380 resolution stage, but can be done as soon as the dt declaration has been
11381 parsed. */
11383 static gfc_try
11384 resolve_fl_derived0 (gfc_symbol *sym)
11386 gfc_symbol* super_type;
11387 gfc_component *c;
11389 super_type = gfc_get_derived_super_type (sym);
11391 /* F2008, C432. */
11392 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
11394 gfc_error ("As extending type '%s' at %L has a coarray component, "
11395 "parent type '%s' shall also have one", sym->name,
11396 &sym->declared_at, super_type->name);
11397 return FAILURE;
11400 /* Ensure the extended type gets resolved before we do. */
11401 if (super_type && resolve_fl_derived0 (super_type) == FAILURE)
11402 return FAILURE;
11404 /* An ABSTRACT type must be extensible. */
11405 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
11407 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
11408 sym->name, &sym->declared_at);
11409 return FAILURE;
11412 for (c = sym->components; c != NULL; c = c->next)
11414 /* F2008, C442. */
11415 if (c->attr.codimension /* FIXME: c->as check due to PR 43412. */
11416 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
11418 gfc_error ("Coarray component '%s' at %L must be allocatable with "
11419 "deferred shape", c->name, &c->loc);
11420 return FAILURE;
11423 /* F2008, C443. */
11424 if (c->attr.codimension && c->ts.type == BT_DERIVED
11425 && c->ts.u.derived->ts.is_iso_c)
11427 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
11428 "shall not be a coarray", c->name, &c->loc);
11429 return FAILURE;
11432 /* F2008, C444. */
11433 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
11434 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
11435 || c->attr.allocatable))
11437 gfc_error ("Component '%s' at %L with coarray component "
11438 "shall be a nonpointer, nonallocatable scalar",
11439 c->name, &c->loc);
11440 return FAILURE;
11443 /* F2008, C448. */
11444 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
11446 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
11447 "is not an array pointer", c->name, &c->loc);
11448 return FAILURE;
11451 if (c->attr.proc_pointer && c->ts.interface)
11453 if (c->ts.interface->attr.procedure && !sym->attr.vtype)
11454 gfc_error ("Interface '%s', used by procedure pointer component "
11455 "'%s' at %L, is declared in a later PROCEDURE statement",
11456 c->ts.interface->name, c->name, &c->loc);
11458 /* Get the attributes from the interface (now resolved). */
11459 if (c->ts.interface->attr.if_source
11460 || c->ts.interface->attr.intrinsic)
11462 gfc_symbol *ifc = c->ts.interface;
11464 if (ifc->formal && !ifc->formal_ns)
11465 resolve_symbol (ifc);
11467 if (ifc->attr.intrinsic)
11468 resolve_intrinsic (ifc, &ifc->declared_at);
11470 if (ifc->result)
11472 c->ts = ifc->result->ts;
11473 c->attr.allocatable = ifc->result->attr.allocatable;
11474 c->attr.pointer = ifc->result->attr.pointer;
11475 c->attr.dimension = ifc->result->attr.dimension;
11476 c->as = gfc_copy_array_spec (ifc->result->as);
11478 else
11480 c->ts = ifc->ts;
11481 c->attr.allocatable = ifc->attr.allocatable;
11482 c->attr.pointer = ifc->attr.pointer;
11483 c->attr.dimension = ifc->attr.dimension;
11484 c->as = gfc_copy_array_spec (ifc->as);
11486 c->ts.interface = ifc;
11487 c->attr.function = ifc->attr.function;
11488 c->attr.subroutine = ifc->attr.subroutine;
11489 gfc_copy_formal_args_ppc (c, ifc);
11491 c->attr.pure = ifc->attr.pure;
11492 c->attr.elemental = ifc->attr.elemental;
11493 c->attr.recursive = ifc->attr.recursive;
11494 c->attr.always_explicit = ifc->attr.always_explicit;
11495 c->attr.ext_attr |= ifc->attr.ext_attr;
11496 /* Replace symbols in array spec. */
11497 if (c->as)
11499 int i;
11500 for (i = 0; i < c->as->rank; i++)
11502 gfc_expr_replace_comp (c->as->lower[i], c);
11503 gfc_expr_replace_comp (c->as->upper[i], c);
11506 /* Copy char length. */
11507 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
11509 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
11510 gfc_expr_replace_comp (cl->length, c);
11511 if (cl->length && !cl->resolved
11512 && gfc_resolve_expr (cl->length) == FAILURE)
11513 return FAILURE;
11514 c->ts.u.cl = cl;
11517 else if (!sym->attr.vtype && c->ts.interface->name[0] != '\0')
11519 gfc_error ("Interface '%s' of procedure pointer component "
11520 "'%s' at %L must be explicit", c->ts.interface->name,
11521 c->name, &c->loc);
11522 return FAILURE;
11525 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
11527 /* Since PPCs are not implicitly typed, a PPC without an explicit
11528 interface must be a subroutine. */
11529 gfc_add_subroutine (&c->attr, c->name, &c->loc);
11532 /* Procedure pointer components: Check PASS arg. */
11533 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
11534 && !sym->attr.vtype)
11536 gfc_symbol* me_arg;
11538 if (c->tb->pass_arg)
11540 gfc_formal_arglist* i;
11542 /* If an explicit passing argument name is given, walk the arg-list
11543 and look for it. */
11545 me_arg = NULL;
11546 c->tb->pass_arg_num = 1;
11547 for (i = c->formal; i; i = i->next)
11549 if (!strcmp (i->sym->name, c->tb->pass_arg))
11551 me_arg = i->sym;
11552 break;
11554 c->tb->pass_arg_num++;
11557 if (!me_arg)
11559 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
11560 "at %L has no argument '%s'", c->name,
11561 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
11562 c->tb->error = 1;
11563 return FAILURE;
11566 else
11568 /* Otherwise, take the first one; there should in fact be at least
11569 one. */
11570 c->tb->pass_arg_num = 1;
11571 if (!c->formal)
11573 gfc_error ("Procedure pointer component '%s' with PASS at %L "
11574 "must have at least one argument",
11575 c->name, &c->loc);
11576 c->tb->error = 1;
11577 return FAILURE;
11579 me_arg = c->formal->sym;
11582 /* Now check that the argument-type matches. */
11583 gcc_assert (me_arg);
11584 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
11585 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
11586 || (me_arg->ts.type == BT_CLASS
11587 && CLASS_DATA (me_arg)->ts.u.derived != sym))
11589 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11590 " the derived type '%s'", me_arg->name, c->name,
11591 me_arg->name, &c->loc, sym->name);
11592 c->tb->error = 1;
11593 return FAILURE;
11596 /* Check for C453. */
11597 if (me_arg->attr.dimension)
11599 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11600 "must be scalar", me_arg->name, c->name, me_arg->name,
11601 &c->loc);
11602 c->tb->error = 1;
11603 return FAILURE;
11606 if (me_arg->attr.pointer)
11608 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11609 "may not have the POINTER attribute", me_arg->name,
11610 c->name, me_arg->name, &c->loc);
11611 c->tb->error = 1;
11612 return FAILURE;
11615 if (me_arg->attr.allocatable)
11617 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11618 "may not be ALLOCATABLE", me_arg->name, c->name,
11619 me_arg->name, &c->loc);
11620 c->tb->error = 1;
11621 return FAILURE;
11624 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
11625 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11626 " at %L", c->name, &c->loc);
11630 /* Check type-spec if this is not the parent-type component. */
11631 if ((!sym->attr.extension || c != sym->components) && !sym->attr.vtype
11632 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
11633 return FAILURE;
11635 /* If this type is an extension, set the accessibility of the parent
11636 component. */
11637 if (super_type && c == sym->components
11638 && strcmp (super_type->name, c->name) == 0)
11639 c->attr.access = super_type->attr.access;
11641 /* If this type is an extension, see if this component has the same name
11642 as an inherited type-bound procedure. */
11643 if (super_type && !sym->attr.is_class
11644 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
11646 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
11647 " inherited type-bound procedure",
11648 c->name, sym->name, &c->loc);
11649 return FAILURE;
11652 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
11653 && !c->ts.deferred)
11655 if (c->ts.u.cl->length == NULL
11656 || (resolve_charlen (c->ts.u.cl) == FAILURE)
11657 || !gfc_is_constant_expr (c->ts.u.cl->length))
11659 gfc_error ("Character length of component '%s' needs to "
11660 "be a constant specification expression at %L",
11661 c->name,
11662 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
11663 return FAILURE;
11667 if (c->ts.type == BT_CHARACTER && c->ts.deferred
11668 && !c->attr.pointer && !c->attr.allocatable)
11670 gfc_error ("Character component '%s' of '%s' at %L with deferred "
11671 "length must be a POINTER or ALLOCATABLE",
11672 c->name, sym->name, &c->loc);
11673 return FAILURE;
11676 if (c->ts.type == BT_DERIVED
11677 && sym->component_access != ACCESS_PRIVATE
11678 && gfc_check_symbol_access (sym)
11679 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
11680 && !c->ts.u.derived->attr.use_assoc
11681 && !gfc_check_symbol_access (c->ts.u.derived)
11682 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
11683 "is a PRIVATE type and cannot be a component of "
11684 "'%s', which is PUBLIC at %L", c->name,
11685 sym->name, &sym->declared_at) == FAILURE)
11686 return FAILURE;
11688 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
11690 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
11691 "type %s", c->name, &c->loc, sym->name);
11692 return FAILURE;
11695 if (sym->attr.sequence)
11697 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
11699 gfc_error ("Component %s of SEQUENCE type declared at %L does "
11700 "not have the SEQUENCE attribute",
11701 c->ts.u.derived->name, &sym->declared_at);
11702 return FAILURE;
11706 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
11707 && c->attr.pointer && c->ts.u.derived->components == NULL
11708 && !c->ts.u.derived->attr.zero_comp)
11710 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11711 "that has not been declared", c->name, sym->name,
11712 &c->loc);
11713 return FAILURE;
11716 if (c->ts.type == BT_CLASS && c->attr.class_ok
11717 && CLASS_DATA (c)->attr.class_pointer
11718 && CLASS_DATA (c)->ts.u.derived->components == NULL
11719 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp)
11721 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11722 "that has not been declared", c->name, sym->name,
11723 &c->loc);
11724 return FAILURE;
11727 /* C437. */
11728 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
11729 && (!c->attr.class_ok
11730 || !(CLASS_DATA (c)->attr.class_pointer
11731 || CLASS_DATA (c)->attr.allocatable)))
11733 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
11734 "or pointer", c->name, &c->loc);
11735 return FAILURE;
11738 /* Ensure that all the derived type components are put on the
11739 derived type list; even in formal namespaces, where derived type
11740 pointer components might not have been declared. */
11741 if (c->ts.type == BT_DERIVED
11742 && c->ts.u.derived
11743 && c->ts.u.derived->components
11744 && c->attr.pointer
11745 && sym != c->ts.u.derived)
11746 add_dt_to_dt_list (c->ts.u.derived);
11748 if (gfc_resolve_array_spec (c->as, !(c->attr.pointer
11749 || c->attr.proc_pointer
11750 || c->attr.allocatable)) == FAILURE)
11751 return FAILURE;
11754 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
11755 all DEFERRED bindings are overridden. */
11756 if (super_type && super_type->attr.abstract && !sym->attr.abstract
11757 && !sym->attr.is_class
11758 && ensure_not_abstract (sym, super_type) == FAILURE)
11759 return FAILURE;
11761 /* Add derived type to the derived type list. */
11762 add_dt_to_dt_list (sym);
11764 return SUCCESS;
11768 /* The following procedure does the full resolution of a derived type,
11769 including resolution of all type-bound procedures (if present). In contrast
11770 to 'resolve_fl_derived0' this can only be done after the module has been
11771 parsed completely. */
11773 static gfc_try
11774 resolve_fl_derived (gfc_symbol *sym)
11776 if (sym->attr.is_class && sym->ts.u.derived == NULL)
11778 /* Fix up incomplete CLASS symbols. */
11779 gfc_component *data = gfc_find_component (sym, "_data", true, true);
11780 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
11781 if (vptr->ts.u.derived == NULL)
11783 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
11784 gcc_assert (vtab);
11785 vptr->ts.u.derived = vtab->ts.u.derived;
11789 if (resolve_fl_derived0 (sym) == FAILURE)
11790 return FAILURE;
11792 /* Resolve the type-bound procedures. */
11793 if (resolve_typebound_procedures (sym) == FAILURE)
11794 return FAILURE;
11796 /* Resolve the finalizer procedures. */
11797 if (gfc_resolve_finalizers (sym) == FAILURE)
11798 return FAILURE;
11800 return SUCCESS;
11804 static gfc_try
11805 resolve_fl_namelist (gfc_symbol *sym)
11807 gfc_namelist *nl;
11808 gfc_symbol *nlsym;
11810 for (nl = sym->namelist; nl; nl = nl->next)
11812 /* Check again, the check in match only works if NAMELIST comes
11813 after the decl. */
11814 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
11816 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
11817 "allowed", nl->sym->name, sym->name, &sym->declared_at);
11818 return FAILURE;
11821 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
11822 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST array "
11823 "object '%s' with assumed shape in namelist "
11824 "'%s' at %L", nl->sym->name, sym->name,
11825 &sym->declared_at) == FAILURE)
11826 return FAILURE;
11828 if (is_non_constant_shape_array (nl->sym)
11829 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST array "
11830 "object '%s' with nonconstant shape in namelist "
11831 "'%s' at %L", nl->sym->name, sym->name,
11832 &sym->declared_at) == FAILURE)
11833 return FAILURE;
11835 if (nl->sym->ts.type == BT_CHARACTER
11836 && (nl->sym->ts.u.cl->length == NULL
11837 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
11838 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST object "
11839 "'%s' with nonconstant character length in "
11840 "namelist '%s' at %L", nl->sym->name, sym->name,
11841 &sym->declared_at) == FAILURE)
11842 return FAILURE;
11844 /* FIXME: Once UDDTIO is implemented, the following can be
11845 removed. */
11846 if (nl->sym->ts.type == BT_CLASS)
11848 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
11849 "polymorphic and requires a defined input/output "
11850 "procedure", nl->sym->name, sym->name, &sym->declared_at);
11851 return FAILURE;
11854 if (nl->sym->ts.type == BT_DERIVED
11855 && (nl->sym->ts.u.derived->attr.alloc_comp
11856 || nl->sym->ts.u.derived->attr.pointer_comp))
11858 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: NAMELIST object "
11859 "'%s' in namelist '%s' at %L with ALLOCATABLE "
11860 "or POINTER components", nl->sym->name,
11861 sym->name, &sym->declared_at) == FAILURE)
11862 return FAILURE;
11864 /* FIXME: Once UDDTIO is implemented, the following can be
11865 removed. */
11866 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
11867 "ALLOCATABLE or POINTER components and thus requires "
11868 "a defined input/output procedure", nl->sym->name,
11869 sym->name, &sym->declared_at);
11870 return FAILURE;
11874 /* Reject PRIVATE objects in a PUBLIC namelist. */
11875 if (gfc_check_symbol_access (sym))
11877 for (nl = sym->namelist; nl; nl = nl->next)
11879 if (!nl->sym->attr.use_assoc
11880 && !is_sym_host_assoc (nl->sym, sym->ns)
11881 && !gfc_check_symbol_access (nl->sym))
11883 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
11884 "cannot be member of PUBLIC namelist '%s' at %L",
11885 nl->sym->name, sym->name, &sym->declared_at);
11886 return FAILURE;
11889 /* Types with private components that came here by USE-association. */
11890 if (nl->sym->ts.type == BT_DERIVED
11891 && derived_inaccessible (nl->sym->ts.u.derived))
11893 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
11894 "components and cannot be member of namelist '%s' at %L",
11895 nl->sym->name, sym->name, &sym->declared_at);
11896 return FAILURE;
11899 /* Types with private components that are defined in the same module. */
11900 if (nl->sym->ts.type == BT_DERIVED
11901 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
11902 && nl->sym->ts.u.derived->attr.private_comp)
11904 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
11905 "cannot be a member of PUBLIC namelist '%s' at %L",
11906 nl->sym->name, sym->name, &sym->declared_at);
11907 return FAILURE;
11913 /* 14.1.2 A module or internal procedure represent local entities
11914 of the same type as a namelist member and so are not allowed. */
11915 for (nl = sym->namelist; nl; nl = nl->next)
11917 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
11918 continue;
11920 if (nl->sym->attr.function && nl->sym == nl->sym->result)
11921 if ((nl->sym == sym->ns->proc_name)
11923 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
11924 continue;
11926 nlsym = NULL;
11927 if (nl->sym && nl->sym->name)
11928 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
11929 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
11931 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
11932 "attribute in '%s' at %L", nlsym->name,
11933 &sym->declared_at);
11934 return FAILURE;
11938 return SUCCESS;
11942 static gfc_try
11943 resolve_fl_parameter (gfc_symbol *sym)
11945 /* A parameter array's shape needs to be constant. */
11946 if (sym->as != NULL
11947 && (sym->as->type == AS_DEFERRED
11948 || is_non_constant_shape_array (sym)))
11950 gfc_error ("Parameter array '%s' at %L cannot be automatic "
11951 "or of deferred shape", sym->name, &sym->declared_at);
11952 return FAILURE;
11955 /* Make sure a parameter that has been implicitly typed still
11956 matches the implicit type, since PARAMETER statements can precede
11957 IMPLICIT statements. */
11958 if (sym->attr.implicit_type
11959 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
11960 sym->ns)))
11962 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
11963 "later IMPLICIT type", sym->name, &sym->declared_at);
11964 return FAILURE;
11967 /* Make sure the types of derived parameters are consistent. This
11968 type checking is deferred until resolution because the type may
11969 refer to a derived type from the host. */
11970 if (sym->ts.type == BT_DERIVED
11971 && !gfc_compare_types (&sym->ts, &sym->value->ts))
11973 gfc_error ("Incompatible derived type in PARAMETER at %L",
11974 &sym->value->where);
11975 return FAILURE;
11977 return SUCCESS;
11981 /* Do anything necessary to resolve a symbol. Right now, we just
11982 assume that an otherwise unknown symbol is a variable. This sort
11983 of thing commonly happens for symbols in module. */
11985 static void
11986 resolve_symbol (gfc_symbol *sym)
11988 int check_constant, mp_flag;
11989 gfc_symtree *symtree;
11990 gfc_symtree *this_symtree;
11991 gfc_namespace *ns;
11992 gfc_component *c;
11994 if (sym->attr.flavor == FL_UNKNOWN)
11997 /* If we find that a flavorless symbol is an interface in one of the
11998 parent namespaces, find its symtree in this namespace, free the
11999 symbol and set the symtree to point to the interface symbol. */
12000 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
12002 symtree = gfc_find_symtree (ns->sym_root, sym->name);
12003 if (symtree && (symtree->n.sym->generic ||
12004 (symtree->n.sym->attr.flavor == FL_PROCEDURE
12005 && sym->ns->construct_entities)))
12007 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12008 sym->name);
12009 gfc_release_symbol (sym);
12010 symtree->n.sym->refs++;
12011 this_symtree->n.sym = symtree->n.sym;
12012 return;
12016 /* Otherwise give it a flavor according to such attributes as
12017 it has. */
12018 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
12019 sym->attr.flavor = FL_VARIABLE;
12020 else
12022 sym->attr.flavor = FL_PROCEDURE;
12023 if (sym->attr.dimension)
12024 sym->attr.function = 1;
12028 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
12029 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
12031 if (sym->attr.procedure && sym->ts.interface
12032 && sym->attr.if_source != IFSRC_DECL
12033 && resolve_procedure_interface (sym) == FAILURE)
12034 return;
12036 if (sym->attr.is_protected && !sym->attr.proc_pointer
12037 && (sym->attr.procedure || sym->attr.external))
12039 if (sym->attr.external)
12040 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12041 "at %L", &sym->declared_at);
12042 else
12043 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12044 "at %L", &sym->declared_at);
12046 return;
12050 /* F2008, C530. */
12051 if (sym->attr.contiguous
12052 && (!sym->attr.dimension || (sym->as->type != AS_ASSUMED_SHAPE
12053 && !sym->attr.pointer)))
12055 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
12056 "array pointer or an assumed-shape array", sym->name,
12057 &sym->declared_at);
12058 return;
12061 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
12062 return;
12064 /* Symbols that are module procedures with results (functions) have
12065 the types and array specification copied for type checking in
12066 procedures that call them, as well as for saving to a module
12067 file. These symbols can't stand the scrutiny that their results
12068 can. */
12069 mp_flag = (sym->result != NULL && sym->result != sym);
12071 /* Make sure that the intrinsic is consistent with its internal
12072 representation. This needs to be done before assigning a default
12073 type to avoid spurious warnings. */
12074 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
12075 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
12076 return;
12078 /* Resolve associate names. */
12079 if (sym->assoc)
12080 resolve_assoc_var (sym, true);
12082 /* Assign default type to symbols that need one and don't have one. */
12083 if (sym->ts.type == BT_UNKNOWN)
12085 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
12086 gfc_set_default_type (sym, 1, NULL);
12088 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
12089 && !sym->attr.function && !sym->attr.subroutine
12090 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
12091 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
12093 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12095 /* The specific case of an external procedure should emit an error
12096 in the case that there is no implicit type. */
12097 if (!mp_flag)
12098 gfc_set_default_type (sym, sym->attr.external, NULL);
12099 else
12101 /* Result may be in another namespace. */
12102 resolve_symbol (sym->result);
12104 if (!sym->result->attr.proc_pointer)
12106 sym->ts = sym->result->ts;
12107 sym->as = gfc_copy_array_spec (sym->result->as);
12108 sym->attr.dimension = sym->result->attr.dimension;
12109 sym->attr.pointer = sym->result->attr.pointer;
12110 sym->attr.allocatable = sym->result->attr.allocatable;
12111 sym->attr.contiguous = sym->result->attr.contiguous;
12116 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12117 gfc_resolve_array_spec (sym->result->as, false);
12119 /* Assumed size arrays and assumed shape arrays must be dummy
12120 arguments. Array-spec's of implied-shape should have been resolved to
12121 AS_EXPLICIT already. */
12123 if (sym->as)
12125 gcc_assert (sym->as->type != AS_IMPLIED_SHAPE);
12126 if (((sym->as->type == AS_ASSUMED_SIZE && !sym->as->cp_was_assumed)
12127 || sym->as->type == AS_ASSUMED_SHAPE)
12128 && sym->attr.dummy == 0)
12130 if (sym->as->type == AS_ASSUMED_SIZE)
12131 gfc_error ("Assumed size array at %L must be a dummy argument",
12132 &sym->declared_at);
12133 else
12134 gfc_error ("Assumed shape array at %L must be a dummy argument",
12135 &sym->declared_at);
12136 return;
12140 /* Make sure symbols with known intent or optional are really dummy
12141 variable. Because of ENTRY statement, this has to be deferred
12142 until resolution time. */
12144 if (!sym->attr.dummy
12145 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
12147 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
12148 return;
12151 if (sym->attr.value && !sym->attr.dummy)
12153 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
12154 "it is not a dummy argument", sym->name, &sym->declared_at);
12155 return;
12158 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
12160 gfc_charlen *cl = sym->ts.u.cl;
12161 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12163 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12164 "attribute must have constant length",
12165 sym->name, &sym->declared_at);
12166 return;
12169 if (sym->ts.is_c_interop
12170 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
12172 gfc_error ("C interoperable character dummy variable '%s' at %L "
12173 "with VALUE attribute must have length one",
12174 sym->name, &sym->declared_at);
12175 return;
12179 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
12180 do this for something that was implicitly typed because that is handled
12181 in gfc_set_default_type. Handle dummy arguments and procedure
12182 definitions separately. Also, anything that is use associated is not
12183 handled here but instead is handled in the module it is declared in.
12184 Finally, derived type definitions are allowed to be BIND(C) since that
12185 only implies that they're interoperable, and they are checked fully for
12186 interoperability when a variable is declared of that type. */
12187 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
12188 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
12189 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
12191 gfc_try t = SUCCESS;
12193 /* First, make sure the variable is declared at the
12194 module-level scope (J3/04-007, Section 15.3). */
12195 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
12196 sym->attr.in_common == 0)
12198 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
12199 "is neither a COMMON block nor declared at the "
12200 "module level scope", sym->name, &(sym->declared_at));
12201 t = FAILURE;
12203 else if (sym->common_head != NULL)
12205 t = verify_com_block_vars_c_interop (sym->common_head);
12207 else
12209 /* If type() declaration, we need to verify that the components
12210 of the given type are all C interoperable, etc. */
12211 if (sym->ts.type == BT_DERIVED &&
12212 sym->ts.u.derived->attr.is_c_interop != 1)
12214 /* Make sure the user marked the derived type as BIND(C). If
12215 not, call the verify routine. This could print an error
12216 for the derived type more than once if multiple variables
12217 of that type are declared. */
12218 if (sym->ts.u.derived->attr.is_bind_c != 1)
12219 verify_bind_c_derived_type (sym->ts.u.derived);
12220 t = FAILURE;
12223 /* Verify the variable itself as C interoperable if it
12224 is BIND(C). It is not possible for this to succeed if
12225 the verify_bind_c_derived_type failed, so don't have to handle
12226 any error returned by verify_bind_c_derived_type. */
12227 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
12228 sym->common_block);
12231 if (t == FAILURE)
12233 /* clear the is_bind_c flag to prevent reporting errors more than
12234 once if something failed. */
12235 sym->attr.is_bind_c = 0;
12236 return;
12240 /* If a derived type symbol has reached this point, without its
12241 type being declared, we have an error. Notice that most
12242 conditions that produce undefined derived types have already
12243 been dealt with. However, the likes of:
12244 implicit type(t) (t) ..... call foo (t) will get us here if
12245 the type is not declared in the scope of the implicit
12246 statement. Change the type to BT_UNKNOWN, both because it is so
12247 and to prevent an ICE. */
12248 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
12249 && !sym->ts.u.derived->attr.zero_comp)
12251 gfc_error ("The derived type '%s' at %L is of type '%s', "
12252 "which has not been defined", sym->name,
12253 &sym->declared_at, sym->ts.u.derived->name);
12254 sym->ts.type = BT_UNKNOWN;
12255 return;
12258 /* Make sure that the derived type has been resolved and that the
12259 derived type is visible in the symbol's namespace, if it is a
12260 module function and is not PRIVATE. */
12261 if (sym->ts.type == BT_DERIVED
12262 && sym->ts.u.derived->attr.use_assoc
12263 && sym->ns->proc_name
12264 && sym->ns->proc_name->attr.flavor == FL_MODULE)
12266 gfc_symbol *ds;
12268 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
12269 return;
12271 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
12272 if (!ds && sym->attr.function && gfc_check_symbol_access (sym))
12274 symtree = gfc_new_symtree (&sym->ns->sym_root,
12275 sym->ts.u.derived->name);
12276 symtree->n.sym = sym->ts.u.derived;
12277 sym->ts.u.derived->refs++;
12281 /* Unless the derived-type declaration is use associated, Fortran 95
12282 does not allow public entries of private derived types.
12283 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
12284 161 in 95-006r3. */
12285 if (sym->ts.type == BT_DERIVED
12286 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
12287 && !sym->ts.u.derived->attr.use_assoc
12288 && gfc_check_symbol_access (sym)
12289 && !gfc_check_symbol_access (sym->ts.u.derived)
12290 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
12291 "of PRIVATE derived type '%s'",
12292 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
12293 : "variable", sym->name, &sym->declared_at,
12294 sym->ts.u.derived->name) == FAILURE)
12295 return;
12297 /* F2008, C1302. */
12298 if (sym->ts.type == BT_DERIVED
12299 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
12300 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
12301 || sym->ts.u.derived->attr.lock_comp)
12302 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
12304 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
12305 "type LOCK_TYPE must be a coarray", sym->name,
12306 &sym->declared_at);
12307 return;
12310 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
12311 default initialization is defined (5.1.2.4.4). */
12312 if (sym->ts.type == BT_DERIVED
12313 && sym->attr.dummy
12314 && sym->attr.intent == INTENT_OUT
12315 && sym->as
12316 && sym->as->type == AS_ASSUMED_SIZE)
12318 for (c = sym->ts.u.derived->components; c; c = c->next)
12320 if (c->initializer)
12322 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
12323 "ASSUMED SIZE and so cannot have a default initializer",
12324 sym->name, &sym->declared_at);
12325 return;
12330 /* F2008, C542. */
12331 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
12332 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
12334 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
12335 "INTENT(OUT)", sym->name, &sym->declared_at);
12336 return;
12339 /* F2008, C525. */
12340 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12341 || sym->attr.codimension)
12342 && (sym->attr.result || sym->result == sym))
12344 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
12345 "a coarray component", sym->name, &sym->declared_at);
12346 return;
12349 /* F2008, C524. */
12350 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
12351 && sym->ts.u.derived->ts.is_iso_c)
12353 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12354 "shall not be a coarray", sym->name, &sym->declared_at);
12355 return;
12358 /* F2008, C525. */
12359 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp
12360 && (sym->attr.codimension || sym->attr.pointer || sym->attr.dimension
12361 || sym->attr.allocatable))
12363 gfc_error ("Variable '%s' at %L with coarray component "
12364 "shall be a nonpointer, nonallocatable scalar",
12365 sym->name, &sym->declared_at);
12366 return;
12369 /* F2008, C526. The function-result case was handled above. */
12370 if (sym->attr.codimension
12371 && !(sym->attr.allocatable || sym->attr.dummy || sym->attr.save
12372 || sym->ns->save_all
12373 || sym->ns->proc_name->attr.flavor == FL_MODULE
12374 || sym->ns->proc_name->attr.is_main_program
12375 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
12377 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
12378 "nor a dummy argument", sym->name, &sym->declared_at);
12379 return;
12381 /* F2008, C528. */ /* FIXME: sym->as check due to PR 43412. */
12382 else if (sym->attr.codimension && !sym->attr.allocatable
12383 && sym->as && sym->as->cotype == AS_DEFERRED)
12385 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
12386 "deferred shape", sym->name, &sym->declared_at);
12387 return;
12389 else if (sym->attr.codimension && sym->attr.allocatable
12390 && (sym->as->type != AS_DEFERRED || sym->as->cotype != AS_DEFERRED))
12392 gfc_error ("Allocatable coarray variable '%s' at %L must have "
12393 "deferred shape", sym->name, &sym->declared_at);
12394 return;
12397 /* F2008, C541. */
12398 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12399 || (sym->attr.codimension && sym->attr.allocatable))
12400 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
12402 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
12403 "allocatable coarray or have coarray components",
12404 sym->name, &sym->declared_at);
12405 return;
12408 if (sym->attr.codimension && sym->attr.dummy
12409 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
12411 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
12412 "procedure '%s'", sym->name, &sym->declared_at,
12413 sym->ns->proc_name->name);
12414 return;
12417 switch (sym->attr.flavor)
12419 case FL_VARIABLE:
12420 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
12421 return;
12422 break;
12424 case FL_PROCEDURE:
12425 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
12426 return;
12427 break;
12429 case FL_NAMELIST:
12430 if (resolve_fl_namelist (sym) == FAILURE)
12431 return;
12432 break;
12434 case FL_PARAMETER:
12435 if (resolve_fl_parameter (sym) == FAILURE)
12436 return;
12437 break;
12439 default:
12440 break;
12443 /* Resolve array specifier. Check as well some constraints
12444 on COMMON blocks. */
12446 check_constant = sym->attr.in_common && !sym->attr.pointer;
12448 /* Set the formal_arg_flag so that check_conflict will not throw
12449 an error for host associated variables in the specification
12450 expression for an array_valued function. */
12451 if (sym->attr.function && sym->as)
12452 formal_arg_flag = 1;
12454 gfc_resolve_array_spec (sym->as, check_constant);
12456 formal_arg_flag = 0;
12458 /* Resolve formal namespaces. */
12459 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
12460 && !sym->attr.contained && !sym->attr.intrinsic)
12461 gfc_resolve (sym->formal_ns);
12463 /* Make sure the formal namespace is present. */
12464 if (sym->formal && !sym->formal_ns)
12466 gfc_formal_arglist *formal = sym->formal;
12467 while (formal && !formal->sym)
12468 formal = formal->next;
12470 if (formal)
12472 sym->formal_ns = formal->sym->ns;
12473 sym->formal_ns->refs++;
12477 /* Check threadprivate restrictions. */
12478 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
12479 && (!sym->attr.in_common
12480 && sym->module == NULL
12481 && (sym->ns->proc_name == NULL
12482 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
12483 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
12485 /* If we have come this far we can apply default-initializers, as
12486 described in 14.7.5, to those variables that have not already
12487 been assigned one. */
12488 if (sym->ts.type == BT_DERIVED
12489 && sym->ns == gfc_current_ns
12490 && !sym->value
12491 && !sym->attr.allocatable
12492 && !sym->attr.alloc_comp)
12494 symbol_attribute *a = &sym->attr;
12496 if ((!a->save && !a->dummy && !a->pointer
12497 && !a->in_common && !a->use_assoc
12498 && (a->referenced || a->result)
12499 && !(a->function && sym != sym->result))
12500 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
12501 apply_default_init (sym);
12504 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
12505 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
12506 && !CLASS_DATA (sym)->attr.class_pointer
12507 && !CLASS_DATA (sym)->attr.allocatable)
12508 apply_default_init (sym);
12510 /* If this symbol has a type-spec, check it. */
12511 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
12512 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
12513 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
12514 == FAILURE)
12515 return;
12519 /************* Resolve DATA statements *************/
12521 static struct
12523 gfc_data_value *vnode;
12524 mpz_t left;
12526 values;
12529 /* Advance the values structure to point to the next value in the data list. */
12531 static gfc_try
12532 next_data_value (void)
12534 while (mpz_cmp_ui (values.left, 0) == 0)
12537 if (values.vnode->next == NULL)
12538 return FAILURE;
12540 values.vnode = values.vnode->next;
12541 mpz_set (values.left, values.vnode->repeat);
12544 return SUCCESS;
12548 static gfc_try
12549 check_data_variable (gfc_data_variable *var, locus *where)
12551 gfc_expr *e;
12552 mpz_t size;
12553 mpz_t offset;
12554 gfc_try t;
12555 ar_type mark = AR_UNKNOWN;
12556 int i;
12557 mpz_t section_index[GFC_MAX_DIMENSIONS];
12558 gfc_ref *ref;
12559 gfc_array_ref *ar;
12560 gfc_symbol *sym;
12561 int has_pointer;
12563 if (gfc_resolve_expr (var->expr) == FAILURE)
12564 return FAILURE;
12566 ar = NULL;
12567 mpz_init_set_si (offset, 0);
12568 e = var->expr;
12570 if (e->expr_type != EXPR_VARIABLE)
12571 gfc_internal_error ("check_data_variable(): Bad expression");
12573 sym = e->symtree->n.sym;
12575 if (sym->ns->is_block_data && !sym->attr.in_common)
12577 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
12578 sym->name, &sym->declared_at);
12581 if (e->ref == NULL && sym->as)
12583 gfc_error ("DATA array '%s' at %L must be specified in a previous"
12584 " declaration", sym->name, where);
12585 return FAILURE;
12588 has_pointer = sym->attr.pointer;
12590 if (gfc_is_coindexed (e))
12592 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
12593 where);
12594 return FAILURE;
12597 for (ref = e->ref; ref; ref = ref->next)
12599 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
12600 has_pointer = 1;
12602 if (has_pointer
12603 && ref->type == REF_ARRAY
12604 && ref->u.ar.type != AR_FULL)
12606 gfc_error ("DATA element '%s' at %L is a pointer and so must "
12607 "be a full array", sym->name, where);
12608 return FAILURE;
12612 if (e->rank == 0 || has_pointer)
12614 mpz_init_set_ui (size, 1);
12615 ref = NULL;
12617 else
12619 ref = e->ref;
12621 /* Find the array section reference. */
12622 for (ref = e->ref; ref; ref = ref->next)
12624 if (ref->type != REF_ARRAY)
12625 continue;
12626 if (ref->u.ar.type == AR_ELEMENT)
12627 continue;
12628 break;
12630 gcc_assert (ref);
12632 /* Set marks according to the reference pattern. */
12633 switch (ref->u.ar.type)
12635 case AR_FULL:
12636 mark = AR_FULL;
12637 break;
12639 case AR_SECTION:
12640 ar = &ref->u.ar;
12641 /* Get the start position of array section. */
12642 gfc_get_section_index (ar, section_index, &offset);
12643 mark = AR_SECTION;
12644 break;
12646 default:
12647 gcc_unreachable ();
12650 if (gfc_array_size (e, &size) == FAILURE)
12652 gfc_error ("Nonconstant array section at %L in DATA statement",
12653 &e->where);
12654 mpz_clear (offset);
12655 return FAILURE;
12659 t = SUCCESS;
12661 while (mpz_cmp_ui (size, 0) > 0)
12663 if (next_data_value () == FAILURE)
12665 gfc_error ("DATA statement at %L has more variables than values",
12666 where);
12667 t = FAILURE;
12668 break;
12671 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
12672 if (t == FAILURE)
12673 break;
12675 /* If we have more than one element left in the repeat count,
12676 and we have more than one element left in the target variable,
12677 then create a range assignment. */
12678 /* FIXME: Only done for full arrays for now, since array sections
12679 seem tricky. */
12680 if (mark == AR_FULL && ref && ref->next == NULL
12681 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
12683 mpz_t range;
12685 if (mpz_cmp (size, values.left) >= 0)
12687 mpz_init_set (range, values.left);
12688 mpz_sub (size, size, values.left);
12689 mpz_set_ui (values.left, 0);
12691 else
12693 mpz_init_set (range, size);
12694 mpz_sub (values.left, values.left, size);
12695 mpz_set_ui (size, 0);
12698 t = gfc_assign_data_value (var->expr, values.vnode->expr,
12699 offset, &range);
12701 mpz_add (offset, offset, range);
12702 mpz_clear (range);
12704 if (t == FAILURE)
12705 break;
12708 /* Assign initial value to symbol. */
12709 else
12711 mpz_sub_ui (values.left, values.left, 1);
12712 mpz_sub_ui (size, size, 1);
12714 t = gfc_assign_data_value (var->expr, values.vnode->expr,
12715 offset, NULL);
12716 if (t == FAILURE)
12717 break;
12719 if (mark == AR_FULL)
12720 mpz_add_ui (offset, offset, 1);
12722 /* Modify the array section indexes and recalculate the offset
12723 for next element. */
12724 else if (mark == AR_SECTION)
12725 gfc_advance_section (section_index, ar, &offset);
12729 if (mark == AR_SECTION)
12731 for (i = 0; i < ar->dimen; i++)
12732 mpz_clear (section_index[i]);
12735 mpz_clear (size);
12736 mpz_clear (offset);
12738 return t;
12742 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
12744 /* Iterate over a list of elements in a DATA statement. */
12746 static gfc_try
12747 traverse_data_list (gfc_data_variable *var, locus *where)
12749 mpz_t trip;
12750 iterator_stack frame;
12751 gfc_expr *e, *start, *end, *step;
12752 gfc_try retval = SUCCESS;
12754 mpz_init (frame.value);
12755 mpz_init (trip);
12757 start = gfc_copy_expr (var->iter.start);
12758 end = gfc_copy_expr (var->iter.end);
12759 step = gfc_copy_expr (var->iter.step);
12761 if (gfc_simplify_expr (start, 1) == FAILURE
12762 || start->expr_type != EXPR_CONSTANT)
12764 gfc_error ("start of implied-do loop at %L could not be "
12765 "simplified to a constant value", &start->where);
12766 retval = FAILURE;
12767 goto cleanup;
12769 if (gfc_simplify_expr (end, 1) == FAILURE
12770 || end->expr_type != EXPR_CONSTANT)
12772 gfc_error ("end of implied-do loop at %L could not be "
12773 "simplified to a constant value", &start->where);
12774 retval = FAILURE;
12775 goto cleanup;
12777 if (gfc_simplify_expr (step, 1) == FAILURE
12778 || step->expr_type != EXPR_CONSTANT)
12780 gfc_error ("step of implied-do loop at %L could not be "
12781 "simplified to a constant value", &start->where);
12782 retval = FAILURE;
12783 goto cleanup;
12786 mpz_set (trip, end->value.integer);
12787 mpz_sub (trip, trip, start->value.integer);
12788 mpz_add (trip, trip, step->value.integer);
12790 mpz_div (trip, trip, step->value.integer);
12792 mpz_set (frame.value, start->value.integer);
12794 frame.prev = iter_stack;
12795 frame.variable = var->iter.var->symtree;
12796 iter_stack = &frame;
12798 while (mpz_cmp_ui (trip, 0) > 0)
12800 if (traverse_data_var (var->list, where) == FAILURE)
12802 retval = FAILURE;
12803 goto cleanup;
12806 e = gfc_copy_expr (var->expr);
12807 if (gfc_simplify_expr (e, 1) == FAILURE)
12809 gfc_free_expr (e);
12810 retval = FAILURE;
12811 goto cleanup;
12814 mpz_add (frame.value, frame.value, step->value.integer);
12816 mpz_sub_ui (trip, trip, 1);
12819 cleanup:
12820 mpz_clear (frame.value);
12821 mpz_clear (trip);
12823 gfc_free_expr (start);
12824 gfc_free_expr (end);
12825 gfc_free_expr (step);
12827 iter_stack = frame.prev;
12828 return retval;
12832 /* Type resolve variables in the variable list of a DATA statement. */
12834 static gfc_try
12835 traverse_data_var (gfc_data_variable *var, locus *where)
12837 gfc_try t;
12839 for (; var; var = var->next)
12841 if (var->expr == NULL)
12842 t = traverse_data_list (var, where);
12843 else
12844 t = check_data_variable (var, where);
12846 if (t == FAILURE)
12847 return FAILURE;
12850 return SUCCESS;
12854 /* Resolve the expressions and iterators associated with a data statement.
12855 This is separate from the assignment checking because data lists should
12856 only be resolved once. */
12858 static gfc_try
12859 resolve_data_variables (gfc_data_variable *d)
12861 for (; d; d = d->next)
12863 if (d->list == NULL)
12865 if (gfc_resolve_expr (d->expr) == FAILURE)
12866 return FAILURE;
12868 else
12870 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
12871 return FAILURE;
12873 if (resolve_data_variables (d->list) == FAILURE)
12874 return FAILURE;
12878 return SUCCESS;
12882 /* Resolve a single DATA statement. We implement this by storing a pointer to
12883 the value list into static variables, and then recursively traversing the
12884 variables list, expanding iterators and such. */
12886 static void
12887 resolve_data (gfc_data *d)
12890 if (resolve_data_variables (d->var) == FAILURE)
12891 return;
12893 values.vnode = d->value;
12894 if (d->value == NULL)
12895 mpz_set_ui (values.left, 0);
12896 else
12897 mpz_set (values.left, d->value->repeat);
12899 if (traverse_data_var (d->var, &d->where) == FAILURE)
12900 return;
12902 /* At this point, we better not have any values left. */
12904 if (next_data_value () == SUCCESS)
12905 gfc_error ("DATA statement at %L has more values than variables",
12906 &d->where);
12910 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
12911 accessed by host or use association, is a dummy argument to a pure function,
12912 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
12913 is storage associated with any such variable, shall not be used in the
12914 following contexts: (clients of this function). */
12916 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
12917 procedure. Returns zero if assignment is OK, nonzero if there is a
12918 problem. */
12920 gfc_impure_variable (gfc_symbol *sym)
12922 gfc_symbol *proc;
12923 gfc_namespace *ns;
12925 if (sym->attr.use_assoc || sym->attr.in_common)
12926 return 1;
12928 /* Check if the symbol's ns is inside the pure procedure. */
12929 for (ns = gfc_current_ns; ns; ns = ns->parent)
12931 if (ns == sym->ns)
12932 break;
12933 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
12934 return 1;
12937 proc = sym->ns->proc_name;
12938 if (sym->attr.dummy && gfc_pure (proc)
12939 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
12941 proc->attr.function))
12942 return 1;
12944 /* TODO: Sort out what can be storage associated, if anything, and include
12945 it here. In principle equivalences should be scanned but it does not
12946 seem to be possible to storage associate an impure variable this way. */
12947 return 0;
12951 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
12952 current namespace is inside a pure procedure. */
12955 gfc_pure (gfc_symbol *sym)
12957 symbol_attribute attr;
12958 gfc_namespace *ns;
12960 if (sym == NULL)
12962 /* Check if the current namespace or one of its parents
12963 belongs to a pure procedure. */
12964 for (ns = gfc_current_ns; ns; ns = ns->parent)
12966 sym = ns->proc_name;
12967 if (sym == NULL)
12968 return 0;
12969 attr = sym->attr;
12970 if (attr.flavor == FL_PROCEDURE && attr.pure)
12971 return 1;
12973 return 0;
12976 attr = sym->attr;
12978 return attr.flavor == FL_PROCEDURE && attr.pure;
12982 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
12983 checks if the current namespace is implicitly pure. Note that this
12984 function returns false for a PURE procedure. */
12987 gfc_implicit_pure (gfc_symbol *sym)
12989 symbol_attribute attr;
12991 if (sym == NULL)
12993 /* Check if the current namespace is implicit_pure. */
12994 sym = gfc_current_ns->proc_name;
12995 if (sym == NULL)
12996 return 0;
12997 attr = sym->attr;
12998 if (attr.flavor == FL_PROCEDURE
12999 && attr.implicit_pure && !attr.pure)
13000 return 1;
13001 return 0;
13004 attr = sym->attr;
13006 return attr.flavor == FL_PROCEDURE && attr.implicit_pure && !attr.pure;
13010 /* Test whether the current procedure is elemental or not. */
13013 gfc_elemental (gfc_symbol *sym)
13015 symbol_attribute attr;
13017 if (sym == NULL)
13018 sym = gfc_current_ns->proc_name;
13019 if (sym == NULL)
13020 return 0;
13021 attr = sym->attr;
13023 return attr.flavor == FL_PROCEDURE && attr.elemental;
13027 /* Warn about unused labels. */
13029 static void
13030 warn_unused_fortran_label (gfc_st_label *label)
13032 if (label == NULL)
13033 return;
13035 warn_unused_fortran_label (label->left);
13037 if (label->defined == ST_LABEL_UNKNOWN)
13038 return;
13040 switch (label->referenced)
13042 case ST_LABEL_UNKNOWN:
13043 gfc_warning ("Label %d at %L defined but not used", label->value,
13044 &label->where);
13045 break;
13047 case ST_LABEL_BAD_TARGET:
13048 gfc_warning ("Label %d at %L defined but cannot be used",
13049 label->value, &label->where);
13050 break;
13052 default:
13053 break;
13056 warn_unused_fortran_label (label->right);
13060 /* Returns the sequence type of a symbol or sequence. */
13062 static seq_type
13063 sequence_type (gfc_typespec ts)
13065 seq_type result;
13066 gfc_component *c;
13068 switch (ts.type)
13070 case BT_DERIVED:
13072 if (ts.u.derived->components == NULL)
13073 return SEQ_NONDEFAULT;
13075 result = sequence_type (ts.u.derived->components->ts);
13076 for (c = ts.u.derived->components->next; c; c = c->next)
13077 if (sequence_type (c->ts) != result)
13078 return SEQ_MIXED;
13080 return result;
13082 case BT_CHARACTER:
13083 if (ts.kind != gfc_default_character_kind)
13084 return SEQ_NONDEFAULT;
13086 return SEQ_CHARACTER;
13088 case BT_INTEGER:
13089 if (ts.kind != gfc_default_integer_kind)
13090 return SEQ_NONDEFAULT;
13092 return SEQ_NUMERIC;
13094 case BT_REAL:
13095 if (!(ts.kind == gfc_default_real_kind
13096 || ts.kind == gfc_default_double_kind))
13097 return SEQ_NONDEFAULT;
13099 return SEQ_NUMERIC;
13101 case BT_COMPLEX:
13102 if (ts.kind != gfc_default_complex_kind)
13103 return SEQ_NONDEFAULT;
13105 return SEQ_NUMERIC;
13107 case BT_LOGICAL:
13108 if (ts.kind != gfc_default_logical_kind)
13109 return SEQ_NONDEFAULT;
13111 return SEQ_NUMERIC;
13113 default:
13114 return SEQ_NONDEFAULT;
13119 /* Resolve derived type EQUIVALENCE object. */
13121 static gfc_try
13122 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
13124 gfc_component *c = derived->components;
13126 if (!derived)
13127 return SUCCESS;
13129 /* Shall not be an object of nonsequence derived type. */
13130 if (!derived->attr.sequence)
13132 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
13133 "attribute to be an EQUIVALENCE object", sym->name,
13134 &e->where);
13135 return FAILURE;
13138 /* Shall not have allocatable components. */
13139 if (derived->attr.alloc_comp)
13141 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
13142 "components to be an EQUIVALENCE object",sym->name,
13143 &e->where);
13144 return FAILURE;
13147 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
13149 gfc_error ("Derived type variable '%s' at %L with default "
13150 "initialization cannot be in EQUIVALENCE with a variable "
13151 "in COMMON", sym->name, &e->where);
13152 return FAILURE;
13155 for (; c ; c = c->next)
13157 if (c->ts.type == BT_DERIVED
13158 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
13159 return FAILURE;
13161 /* Shall not be an object of sequence derived type containing a pointer
13162 in the structure. */
13163 if (c->attr.pointer)
13165 gfc_error ("Derived type variable '%s' at %L with pointer "
13166 "component(s) cannot be an EQUIVALENCE object",
13167 sym->name, &e->where);
13168 return FAILURE;
13171 return SUCCESS;
13175 /* Resolve equivalence object.
13176 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
13177 an allocatable array, an object of nonsequence derived type, an object of
13178 sequence derived type containing a pointer at any level of component
13179 selection, an automatic object, a function name, an entry name, a result
13180 name, a named constant, a structure component, or a subobject of any of
13181 the preceding objects. A substring shall not have length zero. A
13182 derived type shall not have components with default initialization nor
13183 shall two objects of an equivalence group be initialized.
13184 Either all or none of the objects shall have an protected attribute.
13185 The simple constraints are done in symbol.c(check_conflict) and the rest
13186 are implemented here. */
13188 static void
13189 resolve_equivalence (gfc_equiv *eq)
13191 gfc_symbol *sym;
13192 gfc_symbol *first_sym;
13193 gfc_expr *e;
13194 gfc_ref *r;
13195 locus *last_where = NULL;
13196 seq_type eq_type, last_eq_type;
13197 gfc_typespec *last_ts;
13198 int object, cnt_protected;
13199 const char *msg;
13201 last_ts = &eq->expr->symtree->n.sym->ts;
13203 first_sym = eq->expr->symtree->n.sym;
13205 cnt_protected = 0;
13207 for (object = 1; eq; eq = eq->eq, object++)
13209 e = eq->expr;
13211 e->ts = e->symtree->n.sym->ts;
13212 /* match_varspec might not know yet if it is seeing
13213 array reference or substring reference, as it doesn't
13214 know the types. */
13215 if (e->ref && e->ref->type == REF_ARRAY)
13217 gfc_ref *ref = e->ref;
13218 sym = e->symtree->n.sym;
13220 if (sym->attr.dimension)
13222 ref->u.ar.as = sym->as;
13223 ref = ref->next;
13226 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
13227 if (e->ts.type == BT_CHARACTER
13228 && ref
13229 && ref->type == REF_ARRAY
13230 && ref->u.ar.dimen == 1
13231 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
13232 && ref->u.ar.stride[0] == NULL)
13234 gfc_expr *start = ref->u.ar.start[0];
13235 gfc_expr *end = ref->u.ar.end[0];
13236 void *mem = NULL;
13238 /* Optimize away the (:) reference. */
13239 if (start == NULL && end == NULL)
13241 if (e->ref == ref)
13242 e->ref = ref->next;
13243 else
13244 e->ref->next = ref->next;
13245 mem = ref;
13247 else
13249 ref->type = REF_SUBSTRING;
13250 if (start == NULL)
13251 start = gfc_get_int_expr (gfc_default_integer_kind,
13252 NULL, 1);
13253 ref->u.ss.start = start;
13254 if (end == NULL && e->ts.u.cl)
13255 end = gfc_copy_expr (e->ts.u.cl->length);
13256 ref->u.ss.end = end;
13257 ref->u.ss.length = e->ts.u.cl;
13258 e->ts.u.cl = NULL;
13260 ref = ref->next;
13261 free (mem);
13264 /* Any further ref is an error. */
13265 if (ref)
13267 gcc_assert (ref->type == REF_ARRAY);
13268 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
13269 &ref->u.ar.where);
13270 continue;
13274 if (gfc_resolve_expr (e) == FAILURE)
13275 continue;
13277 sym = e->symtree->n.sym;
13279 if (sym->attr.is_protected)
13280 cnt_protected++;
13281 if (cnt_protected > 0 && cnt_protected != object)
13283 gfc_error ("Either all or none of the objects in the "
13284 "EQUIVALENCE set at %L shall have the "
13285 "PROTECTED attribute",
13286 &e->where);
13287 break;
13290 /* Shall not equivalence common block variables in a PURE procedure. */
13291 if (sym->ns->proc_name
13292 && sym->ns->proc_name->attr.pure
13293 && sym->attr.in_common)
13295 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
13296 "object in the pure procedure '%s'",
13297 sym->name, &e->where, sym->ns->proc_name->name);
13298 break;
13301 /* Shall not be a named constant. */
13302 if (e->expr_type == EXPR_CONSTANT)
13304 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
13305 "object", sym->name, &e->where);
13306 continue;
13309 if (e->ts.type == BT_DERIVED
13310 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
13311 continue;
13313 /* Check that the types correspond correctly:
13314 Note 5.28:
13315 A numeric sequence structure may be equivalenced to another sequence
13316 structure, an object of default integer type, default real type, double
13317 precision real type, default logical type such that components of the
13318 structure ultimately only become associated to objects of the same
13319 kind. A character sequence structure may be equivalenced to an object
13320 of default character kind or another character sequence structure.
13321 Other objects may be equivalenced only to objects of the same type and
13322 kind parameters. */
13324 /* Identical types are unconditionally OK. */
13325 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
13326 goto identical_types;
13328 last_eq_type = sequence_type (*last_ts);
13329 eq_type = sequence_type (sym->ts);
13331 /* Since the pair of objects is not of the same type, mixed or
13332 non-default sequences can be rejected. */
13334 msg = "Sequence %s with mixed components in EQUIVALENCE "
13335 "statement at %L with different type objects";
13336 if ((object ==2
13337 && last_eq_type == SEQ_MIXED
13338 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
13339 == FAILURE)
13340 || (eq_type == SEQ_MIXED
13341 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13342 &e->where) == FAILURE))
13343 continue;
13345 msg = "Non-default type object or sequence %s in EQUIVALENCE "
13346 "statement at %L with objects of different type";
13347 if ((object ==2
13348 && last_eq_type == SEQ_NONDEFAULT
13349 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
13350 last_where) == FAILURE)
13351 || (eq_type == SEQ_NONDEFAULT
13352 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13353 &e->where) == FAILURE))
13354 continue;
13356 msg ="Non-CHARACTER object '%s' in default CHARACTER "
13357 "EQUIVALENCE statement at %L";
13358 if (last_eq_type == SEQ_CHARACTER
13359 && eq_type != SEQ_CHARACTER
13360 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13361 &e->where) == FAILURE)
13362 continue;
13364 msg ="Non-NUMERIC object '%s' in default NUMERIC "
13365 "EQUIVALENCE statement at %L";
13366 if (last_eq_type == SEQ_NUMERIC
13367 && eq_type != SEQ_NUMERIC
13368 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13369 &e->where) == FAILURE)
13370 continue;
13372 identical_types:
13373 last_ts =&sym->ts;
13374 last_where = &e->where;
13376 if (!e->ref)
13377 continue;
13379 /* Shall not be an automatic array. */
13380 if (e->ref->type == REF_ARRAY
13381 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
13383 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
13384 "an EQUIVALENCE object", sym->name, &e->where);
13385 continue;
13388 r = e->ref;
13389 while (r)
13391 /* Shall not be a structure component. */
13392 if (r->type == REF_COMPONENT)
13394 gfc_error ("Structure component '%s' at %L cannot be an "
13395 "EQUIVALENCE object",
13396 r->u.c.component->name, &e->where);
13397 break;
13400 /* A substring shall not have length zero. */
13401 if (r->type == REF_SUBSTRING)
13403 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
13405 gfc_error ("Substring at %L has length zero",
13406 &r->u.ss.start->where);
13407 break;
13410 r = r->next;
13416 /* Resolve function and ENTRY types, issue diagnostics if needed. */
13418 static void
13419 resolve_fntype (gfc_namespace *ns)
13421 gfc_entry_list *el;
13422 gfc_symbol *sym;
13424 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
13425 return;
13427 /* If there are any entries, ns->proc_name is the entry master
13428 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
13429 if (ns->entries)
13430 sym = ns->entries->sym;
13431 else
13432 sym = ns->proc_name;
13433 if (sym->result == sym
13434 && sym->ts.type == BT_UNKNOWN
13435 && gfc_set_default_type (sym, 0, NULL) == FAILURE
13436 && !sym->attr.untyped)
13438 gfc_error ("Function '%s' at %L has no IMPLICIT type",
13439 sym->name, &sym->declared_at);
13440 sym->attr.untyped = 1;
13443 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
13444 && !sym->attr.contained
13445 && !gfc_check_symbol_access (sym->ts.u.derived)
13446 && gfc_check_symbol_access (sym))
13448 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
13449 "%L of PRIVATE type '%s'", sym->name,
13450 &sym->declared_at, sym->ts.u.derived->name);
13453 if (ns->entries)
13454 for (el = ns->entries->next; el; el = el->next)
13456 if (el->sym->result == el->sym
13457 && el->sym->ts.type == BT_UNKNOWN
13458 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
13459 && !el->sym->attr.untyped)
13461 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
13462 el->sym->name, &el->sym->declared_at);
13463 el->sym->attr.untyped = 1;
13469 /* 12.3.2.1.1 Defined operators. */
13471 static gfc_try
13472 check_uop_procedure (gfc_symbol *sym, locus where)
13474 gfc_formal_arglist *formal;
13476 if (!sym->attr.function)
13478 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
13479 sym->name, &where);
13480 return FAILURE;
13483 if (sym->ts.type == BT_CHARACTER
13484 && !(sym->ts.u.cl && sym->ts.u.cl->length)
13485 && !(sym->result && sym->result->ts.u.cl
13486 && sym->result->ts.u.cl->length))
13488 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
13489 "character length", sym->name, &where);
13490 return FAILURE;
13493 formal = sym->formal;
13494 if (!formal || !formal->sym)
13496 gfc_error ("User operator procedure '%s' at %L must have at least "
13497 "one argument", sym->name, &where);
13498 return FAILURE;
13501 if (formal->sym->attr.intent != INTENT_IN)
13503 gfc_error ("First argument of operator interface at %L must be "
13504 "INTENT(IN)", &where);
13505 return FAILURE;
13508 if (formal->sym->attr.optional)
13510 gfc_error ("First argument of operator interface at %L cannot be "
13511 "optional", &where);
13512 return FAILURE;
13515 formal = formal->next;
13516 if (!formal || !formal->sym)
13517 return SUCCESS;
13519 if (formal->sym->attr.intent != INTENT_IN)
13521 gfc_error ("Second argument of operator interface at %L must be "
13522 "INTENT(IN)", &where);
13523 return FAILURE;
13526 if (formal->sym->attr.optional)
13528 gfc_error ("Second argument of operator interface at %L cannot be "
13529 "optional", &where);
13530 return FAILURE;
13533 if (formal->next)
13535 gfc_error ("Operator interface at %L must have, at most, two "
13536 "arguments", &where);
13537 return FAILURE;
13540 return SUCCESS;
13543 static void
13544 gfc_resolve_uops (gfc_symtree *symtree)
13546 gfc_interface *itr;
13548 if (symtree == NULL)
13549 return;
13551 gfc_resolve_uops (symtree->left);
13552 gfc_resolve_uops (symtree->right);
13554 for (itr = symtree->n.uop->op; itr; itr = itr->next)
13555 check_uop_procedure (itr->sym, itr->sym->declared_at);
13559 /* Examine all of the expressions associated with a program unit,
13560 assign types to all intermediate expressions, make sure that all
13561 assignments are to compatible types and figure out which names
13562 refer to which functions or subroutines. It doesn't check code
13563 block, which is handled by resolve_code. */
13565 static void
13566 resolve_types (gfc_namespace *ns)
13568 gfc_namespace *n;
13569 gfc_charlen *cl;
13570 gfc_data *d;
13571 gfc_equiv *eq;
13572 gfc_namespace* old_ns = gfc_current_ns;
13574 /* Check that all IMPLICIT types are ok. */
13575 if (!ns->seen_implicit_none)
13577 unsigned letter;
13578 for (letter = 0; letter != GFC_LETTERS; ++letter)
13579 if (ns->set_flag[letter]
13580 && resolve_typespec_used (&ns->default_type[letter],
13581 &ns->implicit_loc[letter],
13582 NULL) == FAILURE)
13583 return;
13586 gfc_current_ns = ns;
13588 resolve_entries (ns);
13590 resolve_common_vars (ns->blank_common.head, false);
13591 resolve_common_blocks (ns->common_root);
13593 resolve_contained_functions (ns);
13595 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
13596 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
13597 resolve_formal_arglist (ns->proc_name);
13599 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
13601 for (cl = ns->cl_list; cl; cl = cl->next)
13602 resolve_charlen (cl);
13604 gfc_traverse_ns (ns, resolve_symbol);
13606 resolve_fntype (ns);
13608 for (n = ns->contained; n; n = n->sibling)
13610 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
13611 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
13612 "also be PURE", n->proc_name->name,
13613 &n->proc_name->declared_at);
13615 resolve_types (n);
13618 forall_flag = 0;
13619 do_concurrent_flag = 0;
13620 gfc_check_interfaces (ns);
13622 gfc_traverse_ns (ns, resolve_values);
13624 if (ns->save_all)
13625 gfc_save_all (ns);
13627 iter_stack = NULL;
13628 for (d = ns->data; d; d = d->next)
13629 resolve_data (d);
13631 iter_stack = NULL;
13632 gfc_traverse_ns (ns, gfc_formalize_init_value);
13634 gfc_traverse_ns (ns, gfc_verify_binding_labels);
13636 if (ns->common_root != NULL)
13637 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
13639 for (eq = ns->equiv; eq; eq = eq->next)
13640 resolve_equivalence (eq);
13642 /* Warn about unused labels. */
13643 if (warn_unused_label)
13644 warn_unused_fortran_label (ns->st_labels);
13646 gfc_resolve_uops (ns->uop_root);
13648 gfc_current_ns = old_ns;
13652 /* Call resolve_code recursively. */
13654 static void
13655 resolve_codes (gfc_namespace *ns)
13657 gfc_namespace *n;
13658 bitmap_obstack old_obstack;
13660 if (ns->resolved == 1)
13661 return;
13663 for (n = ns->contained; n; n = n->sibling)
13664 resolve_codes (n);
13666 gfc_current_ns = ns;
13668 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
13669 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
13670 cs_base = NULL;
13672 /* Set to an out of range value. */
13673 current_entry_id = -1;
13675 old_obstack = labels_obstack;
13676 bitmap_obstack_initialize (&labels_obstack);
13678 resolve_code (ns->code, ns);
13680 bitmap_obstack_release (&labels_obstack);
13681 labels_obstack = old_obstack;
13685 /* This function is called after a complete program unit has been compiled.
13686 Its purpose is to examine all of the expressions associated with a program
13687 unit, assign types to all intermediate expressions, make sure that all
13688 assignments are to compatible types and figure out which names refer to
13689 which functions or subroutines. */
13691 void
13692 gfc_resolve (gfc_namespace *ns)
13694 gfc_namespace *old_ns;
13695 code_stack *old_cs_base;
13697 if (ns->resolved)
13698 return;
13700 ns->resolved = -1;
13701 old_ns = gfc_current_ns;
13702 old_cs_base = cs_base;
13704 resolve_types (ns);
13705 resolve_codes (ns);
13707 gfc_current_ns = old_ns;
13708 cs_base = old_cs_base;
13709 ns->resolved = 1;
13711 gfc_run_passes (ns);