2016-09-23 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / resolve.c
blob9998302714a8beacf5b255daf82154cd434eedbc
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2016 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "bitmap.h"
26 #include "gfortran.h"
27 #include "arith.h" /* For gfc_compare_expr(). */
28 #include "dependency.h"
29 #include "data.h"
30 #include "target-memory.h" /* for gfc_simplify_transfer */
31 #include "constructor.h"
33 /* Types used in equivalence statements. */
35 enum seq_type
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
40 /* Stack to keep track of the nesting of blocks as we move through the
41 code. See resolve_branch() and gfc_resolve_code(). */
43 typedef struct code_stack
45 struct gfc_code *head, *current;
46 struct code_stack *prev;
48 /* This bitmap keeps track of the targets valid for a branch from
49 inside this block except for END {IF|SELECT}s of enclosing
50 blocks. */
51 bitmap reachable_labels;
53 code_stack;
55 static code_stack *cs_base = NULL;
58 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
60 static int forall_flag;
61 int gfc_do_concurrent_flag;
63 /* True when we are resolving an expression that is an actual argument to
64 a procedure. */
65 static bool actual_arg = false;
66 /* True when we are resolving an expression that is the first actual argument
67 to a procedure. */
68 static bool first_actual_arg = false;
71 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
73 static int omp_workshare_flag;
75 /* Nonzero if we are processing a formal arglist. The corresponding function
76 resets the flag each time that it is read. */
77 static int formal_arg_flag = 0;
79 /* True if we are resolving a specification expression. */
80 static bool specification_expr = false;
82 /* The id of the last entry seen. */
83 static int current_entry_id;
85 /* We use bitmaps to determine if a branch target is valid. */
86 static bitmap_obstack labels_obstack;
88 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
89 static bool inquiry_argument = false;
92 int
93 gfc_is_formal_arg (void)
95 return formal_arg_flag;
98 /* Is the symbol host associated? */
99 static bool
100 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
102 for (ns = ns->parent; ns; ns = ns->parent)
104 if (sym->ns == ns)
105 return true;
108 return false;
111 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
112 an ABSTRACT derived-type. If where is not NULL, an error message with that
113 locus is printed, optionally using name. */
115 static bool
116 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
118 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
120 if (where)
122 if (name)
123 gfc_error ("%qs at %L is of the ABSTRACT type %qs",
124 name, where, ts->u.derived->name);
125 else
126 gfc_error ("ABSTRACT type %qs used at %L",
127 ts->u.derived->name, where);
130 return false;
133 return true;
137 static bool
138 check_proc_interface (gfc_symbol *ifc, locus *where)
140 /* Several checks for F08:C1216. */
141 if (ifc->attr.procedure)
143 gfc_error ("Interface %qs at %L is declared "
144 "in a later PROCEDURE statement", ifc->name, where);
145 return false;
147 if (ifc->generic)
149 /* For generic interfaces, check if there is
150 a specific procedure with the same name. */
151 gfc_interface *gen = ifc->generic;
152 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
153 gen = gen->next;
154 if (!gen)
156 gfc_error ("Interface %qs at %L may not be generic",
157 ifc->name, where);
158 return false;
161 if (ifc->attr.proc == PROC_ST_FUNCTION)
163 gfc_error ("Interface %qs at %L may not be a statement function",
164 ifc->name, where);
165 return false;
167 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
168 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
169 ifc->attr.intrinsic = 1;
170 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
172 gfc_error ("Intrinsic procedure %qs not allowed in "
173 "PROCEDURE statement at %L", ifc->name, where);
174 return false;
176 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
178 gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
179 return false;
181 return true;
185 static void resolve_symbol (gfc_symbol *sym);
188 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
190 static bool
191 resolve_procedure_interface (gfc_symbol *sym)
193 gfc_symbol *ifc = sym->ts.interface;
195 if (!ifc)
196 return true;
198 if (ifc == sym)
200 gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
201 sym->name, &sym->declared_at);
202 return false;
204 if (!check_proc_interface (ifc, &sym->declared_at))
205 return false;
207 if (ifc->attr.if_source || ifc->attr.intrinsic)
209 /* Resolve interface and copy attributes. */
210 resolve_symbol (ifc);
211 if (ifc->attr.intrinsic)
212 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
214 if (ifc->result)
216 sym->ts = ifc->result->ts;
217 sym->result = sym;
219 else
220 sym->ts = ifc->ts;
221 sym->ts.interface = ifc;
222 sym->attr.function = ifc->attr.function;
223 sym->attr.subroutine = ifc->attr.subroutine;
225 sym->attr.allocatable = ifc->attr.allocatable;
226 sym->attr.pointer = ifc->attr.pointer;
227 sym->attr.pure = ifc->attr.pure;
228 sym->attr.elemental = ifc->attr.elemental;
229 sym->attr.dimension = ifc->attr.dimension;
230 sym->attr.contiguous = ifc->attr.contiguous;
231 sym->attr.recursive = ifc->attr.recursive;
232 sym->attr.always_explicit = ifc->attr.always_explicit;
233 sym->attr.ext_attr |= ifc->attr.ext_attr;
234 sym->attr.is_bind_c = ifc->attr.is_bind_c;
235 sym->attr.class_ok = ifc->attr.class_ok;
236 /* Copy array spec. */
237 sym->as = gfc_copy_array_spec (ifc->as);
238 /* Copy char length. */
239 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
241 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
242 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
243 && !gfc_resolve_expr (sym->ts.u.cl->length))
244 return false;
248 return true;
252 /* Resolve types of formal argument lists. These have to be done early so that
253 the formal argument lists of module procedures can be copied to the
254 containing module before the individual procedures are resolved
255 individually. We also resolve argument lists of procedures in interface
256 blocks because they are self-contained scoping units.
258 Since a dummy argument cannot be a non-dummy procedure, the only
259 resort left for untyped names are the IMPLICIT types. */
261 static void
262 resolve_formal_arglist (gfc_symbol *proc)
264 gfc_formal_arglist *f;
265 gfc_symbol *sym;
266 bool saved_specification_expr;
267 int i;
269 if (proc->result != NULL)
270 sym = proc->result;
271 else
272 sym = proc;
274 if (gfc_elemental (proc)
275 || sym->attr.pointer || sym->attr.allocatable
276 || (sym->as && sym->as->rank != 0))
278 proc->attr.always_explicit = 1;
279 sym->attr.always_explicit = 1;
282 formal_arg_flag = 1;
284 for (f = proc->formal; f; f = f->next)
286 gfc_array_spec *as;
288 sym = f->sym;
290 if (sym == NULL)
292 /* Alternate return placeholder. */
293 if (gfc_elemental (proc))
294 gfc_error ("Alternate return specifier in elemental subroutine "
295 "%qs at %L is not allowed", proc->name,
296 &proc->declared_at);
297 if (proc->attr.function)
298 gfc_error ("Alternate return specifier in function "
299 "%qs at %L is not allowed", proc->name,
300 &proc->declared_at);
301 continue;
303 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
304 && !resolve_procedure_interface (sym))
305 return;
307 if (strcmp (proc->name, sym->name) == 0)
309 gfc_error ("Self-referential argument "
310 "%qs at %L is not allowed", sym->name,
311 &proc->declared_at);
312 return;
315 if (sym->attr.if_source != IFSRC_UNKNOWN)
316 resolve_formal_arglist (sym);
318 if (sym->attr.subroutine || sym->attr.external)
320 if (sym->attr.flavor == FL_UNKNOWN)
321 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
323 else
325 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
326 && (!sym->attr.function || sym->result == sym))
327 gfc_set_default_type (sym, 1, sym->ns);
330 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
331 ? CLASS_DATA (sym)->as : sym->as;
333 saved_specification_expr = specification_expr;
334 specification_expr = true;
335 gfc_resolve_array_spec (as, 0);
336 specification_expr = saved_specification_expr;
338 /* We can't tell if an array with dimension (:) is assumed or deferred
339 shape until we know if it has the pointer or allocatable attributes.
341 if (as && as->rank > 0 && as->type == AS_DEFERRED
342 && ((sym->ts.type != BT_CLASS
343 && !(sym->attr.pointer || sym->attr.allocatable))
344 || (sym->ts.type == BT_CLASS
345 && !(CLASS_DATA (sym)->attr.class_pointer
346 || CLASS_DATA (sym)->attr.allocatable)))
347 && sym->attr.flavor != FL_PROCEDURE)
349 as->type = AS_ASSUMED_SHAPE;
350 for (i = 0; i < as->rank; i++)
351 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
354 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
355 || (as && as->type == AS_ASSUMED_RANK)
356 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
357 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
358 && (CLASS_DATA (sym)->attr.class_pointer
359 || CLASS_DATA (sym)->attr.allocatable
360 || CLASS_DATA (sym)->attr.target))
361 || sym->attr.optional)
363 proc->attr.always_explicit = 1;
364 if (proc->result)
365 proc->result->attr.always_explicit = 1;
368 /* If the flavor is unknown at this point, it has to be a variable.
369 A procedure specification would have already set the type. */
371 if (sym->attr.flavor == FL_UNKNOWN)
372 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
374 if (gfc_pure (proc))
376 if (sym->attr.flavor == FL_PROCEDURE)
378 /* F08:C1279. */
379 if (!gfc_pure (sym))
381 gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
382 "also be PURE", sym->name, &sym->declared_at);
383 continue;
386 else if (!sym->attr.pointer)
388 if (proc->attr.function && sym->attr.intent != INTENT_IN)
390 if (sym->attr.value)
391 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
392 " of pure function %qs at %L with VALUE "
393 "attribute but without INTENT(IN)",
394 sym->name, proc->name, &sym->declared_at);
395 else
396 gfc_error ("Argument %qs of pure function %qs at %L must "
397 "be INTENT(IN) or VALUE", sym->name, proc->name,
398 &sym->declared_at);
401 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
403 if (sym->attr.value)
404 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
405 " of pure subroutine %qs at %L with VALUE "
406 "attribute but without INTENT", sym->name,
407 proc->name, &sym->declared_at);
408 else
409 gfc_error ("Argument %qs of pure subroutine %qs at %L "
410 "must have its INTENT specified or have the "
411 "VALUE attribute", sym->name, proc->name,
412 &sym->declared_at);
416 /* F08:C1278a. */
417 if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
419 gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
420 " may not be polymorphic", sym->name, proc->name,
421 &sym->declared_at);
422 continue;
426 if (proc->attr.implicit_pure)
428 if (sym->attr.flavor == FL_PROCEDURE)
430 if (!gfc_pure (sym))
431 proc->attr.implicit_pure = 0;
433 else if (!sym->attr.pointer)
435 if (proc->attr.function && sym->attr.intent != INTENT_IN
436 && !sym->value)
437 proc->attr.implicit_pure = 0;
439 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
440 && !sym->value)
441 proc->attr.implicit_pure = 0;
445 if (gfc_elemental (proc))
447 /* F08:C1289. */
448 if (sym->attr.codimension
449 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
450 && CLASS_DATA (sym)->attr.codimension))
452 gfc_error ("Coarray dummy argument %qs at %L to elemental "
453 "procedure", sym->name, &sym->declared_at);
454 continue;
457 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
458 && CLASS_DATA (sym)->as))
460 gfc_error ("Argument %qs of elemental procedure at %L must "
461 "be scalar", sym->name, &sym->declared_at);
462 continue;
465 if (sym->attr.allocatable
466 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
467 && CLASS_DATA (sym)->attr.allocatable))
469 gfc_error ("Argument %qs of elemental procedure at %L cannot "
470 "have the ALLOCATABLE attribute", sym->name,
471 &sym->declared_at);
472 continue;
475 if (sym->attr.pointer
476 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
477 && CLASS_DATA (sym)->attr.class_pointer))
479 gfc_error ("Argument %qs of elemental procedure at %L cannot "
480 "have the POINTER attribute", sym->name,
481 &sym->declared_at);
482 continue;
485 if (sym->attr.flavor == FL_PROCEDURE)
487 gfc_error ("Dummy procedure %qs not allowed in elemental "
488 "procedure %qs at %L", sym->name, proc->name,
489 &sym->declared_at);
490 continue;
493 /* Fortran 2008 Corrigendum 1, C1290a. */
494 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
496 gfc_error ("Argument %qs of elemental procedure %qs at %L must "
497 "have its INTENT specified or have the VALUE "
498 "attribute", sym->name, proc->name,
499 &sym->declared_at);
500 continue;
504 /* Each dummy shall be specified to be scalar. */
505 if (proc->attr.proc == PROC_ST_FUNCTION)
507 if (sym->as != NULL)
509 gfc_error ("Argument %qs of statement function at %L must "
510 "be scalar", sym->name, &sym->declared_at);
511 continue;
514 if (sym->ts.type == BT_CHARACTER)
516 gfc_charlen *cl = sym->ts.u.cl;
517 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
519 gfc_error ("Character-valued argument %qs of statement "
520 "function at %L must have constant length",
521 sym->name, &sym->declared_at);
522 continue;
527 formal_arg_flag = 0;
531 /* Work function called when searching for symbols that have argument lists
532 associated with them. */
534 static void
535 find_arglists (gfc_symbol *sym)
537 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
538 || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
539 return;
541 resolve_formal_arglist (sym);
545 /* Given a namespace, resolve all formal argument lists within the namespace.
548 static void
549 resolve_formal_arglists (gfc_namespace *ns)
551 if (ns == NULL)
552 return;
554 gfc_traverse_ns (ns, find_arglists);
558 static void
559 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
561 bool t;
563 /* If this namespace is not a function or an entry master function,
564 ignore it. */
565 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
566 || sym->attr.entry_master)
567 return;
569 /* Try to find out of what the return type is. */
570 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
572 t = gfc_set_default_type (sym->result, 0, ns);
574 if (!t && !sym->result->attr.untyped)
576 if (sym->result == sym)
577 gfc_error ("Contained function %qs at %L has no IMPLICIT type",
578 sym->name, &sym->declared_at);
579 else if (!sym->result->attr.proc_pointer)
580 gfc_error ("Result %qs of contained function %qs at %L has "
581 "no IMPLICIT type", sym->result->name, sym->name,
582 &sym->result->declared_at);
583 sym->result->attr.untyped = 1;
587 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
588 type, lists the only ways a character length value of * can be used:
589 dummy arguments of procedures, named constants, and function results
590 in external functions. Internal function results and results of module
591 procedures are not on this list, ergo, not permitted. */
593 if (sym->result->ts.type == BT_CHARACTER)
595 gfc_charlen *cl = sym->result->ts.u.cl;
596 if ((!cl || !cl->length) && !sym->result->ts.deferred)
598 /* See if this is a module-procedure and adapt error message
599 accordingly. */
600 bool module_proc;
601 gcc_assert (ns->parent && ns->parent->proc_name);
602 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
604 gfc_error ("Character-valued %s %qs at %L must not be"
605 " assumed length",
606 module_proc ? _("module procedure")
607 : _("internal function"),
608 sym->name, &sym->declared_at);
614 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
615 introduce duplicates. */
617 static void
618 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
620 gfc_formal_arglist *f, *new_arglist;
621 gfc_symbol *new_sym;
623 for (; new_args != NULL; new_args = new_args->next)
625 new_sym = new_args->sym;
626 /* See if this arg is already in the formal argument list. */
627 for (f = proc->formal; f; f = f->next)
629 if (new_sym == f->sym)
630 break;
633 if (f)
634 continue;
636 /* Add a new argument. Argument order is not important. */
637 new_arglist = gfc_get_formal_arglist ();
638 new_arglist->sym = new_sym;
639 new_arglist->next = proc->formal;
640 proc->formal = new_arglist;
645 /* Flag the arguments that are not present in all entries. */
647 static void
648 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
650 gfc_formal_arglist *f, *head;
651 head = new_args;
653 for (f = proc->formal; f; f = f->next)
655 if (f->sym == NULL)
656 continue;
658 for (new_args = head; new_args; new_args = new_args->next)
660 if (new_args->sym == f->sym)
661 break;
664 if (new_args)
665 continue;
667 f->sym->attr.not_always_present = 1;
672 /* Resolve alternate entry points. If a symbol has multiple entry points we
673 create a new master symbol for the main routine, and turn the existing
674 symbol into an entry point. */
676 static void
677 resolve_entries (gfc_namespace *ns)
679 gfc_namespace *old_ns;
680 gfc_code *c;
681 gfc_symbol *proc;
682 gfc_entry_list *el;
683 char name[GFC_MAX_SYMBOL_LEN + 1];
684 static int master_count = 0;
686 if (ns->proc_name == NULL)
687 return;
689 /* No need to do anything if this procedure doesn't have alternate entry
690 points. */
691 if (!ns->entries)
692 return;
694 /* We may already have resolved alternate entry points. */
695 if (ns->proc_name->attr.entry_master)
696 return;
698 /* If this isn't a procedure something has gone horribly wrong. */
699 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
701 /* Remember the current namespace. */
702 old_ns = gfc_current_ns;
704 gfc_current_ns = ns;
706 /* Add the main entry point to the list of entry points. */
707 el = gfc_get_entry_list ();
708 el->sym = ns->proc_name;
709 el->id = 0;
710 el->next = ns->entries;
711 ns->entries = el;
712 ns->proc_name->attr.entry = 1;
714 /* If it is a module function, it needs to be in the right namespace
715 so that gfc_get_fake_result_decl can gather up the results. The
716 need for this arose in get_proc_name, where these beasts were
717 left in their own namespace, to keep prior references linked to
718 the entry declaration.*/
719 if (ns->proc_name->attr.function
720 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
721 el->sym->ns = ns;
723 /* Do the same for entries where the master is not a module
724 procedure. These are retained in the module namespace because
725 of the module procedure declaration. */
726 for (el = el->next; el; el = el->next)
727 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
728 && el->sym->attr.mod_proc)
729 el->sym->ns = ns;
730 el = ns->entries;
732 /* Add an entry statement for it. */
733 c = gfc_get_code (EXEC_ENTRY);
734 c->ext.entry = el;
735 c->next = ns->code;
736 ns->code = c;
738 /* Create a new symbol for the master function. */
739 /* Give the internal function a unique name (within this file).
740 Also include the function name so the user has some hope of figuring
741 out what is going on. */
742 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
743 master_count++, ns->proc_name->name);
744 gfc_get_ha_symbol (name, &proc);
745 gcc_assert (proc != NULL);
747 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
748 if (ns->proc_name->attr.subroutine)
749 gfc_add_subroutine (&proc->attr, proc->name, NULL);
750 else
752 gfc_symbol *sym;
753 gfc_typespec *ts, *fts;
754 gfc_array_spec *as, *fas;
755 gfc_add_function (&proc->attr, proc->name, NULL);
756 proc->result = proc;
757 fas = ns->entries->sym->as;
758 fas = fas ? fas : ns->entries->sym->result->as;
759 fts = &ns->entries->sym->result->ts;
760 if (fts->type == BT_UNKNOWN)
761 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
762 for (el = ns->entries->next; el; el = el->next)
764 ts = &el->sym->result->ts;
765 as = el->sym->as;
766 as = as ? as : el->sym->result->as;
767 if (ts->type == BT_UNKNOWN)
768 ts = gfc_get_default_type (el->sym->result->name, NULL);
770 if (! gfc_compare_types (ts, fts)
771 || (el->sym->result->attr.dimension
772 != ns->entries->sym->result->attr.dimension)
773 || (el->sym->result->attr.pointer
774 != ns->entries->sym->result->attr.pointer))
775 break;
776 else if (as && fas && ns->entries->sym->result != el->sym->result
777 && gfc_compare_array_spec (as, fas) == 0)
778 gfc_error ("Function %s at %L has entries with mismatched "
779 "array specifications", ns->entries->sym->name,
780 &ns->entries->sym->declared_at);
781 /* The characteristics need to match and thus both need to have
782 the same string length, i.e. both len=*, or both len=4.
783 Having both len=<variable> is also possible, but difficult to
784 check at compile time. */
785 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
786 && (((ts->u.cl->length && !fts->u.cl->length)
787 ||(!ts->u.cl->length && fts->u.cl->length))
788 || (ts->u.cl->length
789 && ts->u.cl->length->expr_type
790 != fts->u.cl->length->expr_type)
791 || (ts->u.cl->length
792 && ts->u.cl->length->expr_type == EXPR_CONSTANT
793 && mpz_cmp (ts->u.cl->length->value.integer,
794 fts->u.cl->length->value.integer) != 0)))
795 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
796 "entries returning variables of different "
797 "string lengths", ns->entries->sym->name,
798 &ns->entries->sym->declared_at);
801 if (el == NULL)
803 sym = ns->entries->sym->result;
804 /* All result types the same. */
805 proc->ts = *fts;
806 if (sym->attr.dimension)
807 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
808 if (sym->attr.pointer)
809 gfc_add_pointer (&proc->attr, NULL);
811 else
813 /* Otherwise the result will be passed through a union by
814 reference. */
815 proc->attr.mixed_entry_master = 1;
816 for (el = ns->entries; el; el = el->next)
818 sym = el->sym->result;
819 if (sym->attr.dimension)
821 if (el == ns->entries)
822 gfc_error ("FUNCTION result %s can't be an array in "
823 "FUNCTION %s at %L", sym->name,
824 ns->entries->sym->name, &sym->declared_at);
825 else
826 gfc_error ("ENTRY result %s can't be an array in "
827 "FUNCTION %s at %L", sym->name,
828 ns->entries->sym->name, &sym->declared_at);
830 else if (sym->attr.pointer)
832 if (el == ns->entries)
833 gfc_error ("FUNCTION result %s can't be a POINTER in "
834 "FUNCTION %s at %L", sym->name,
835 ns->entries->sym->name, &sym->declared_at);
836 else
837 gfc_error ("ENTRY result %s can't be a POINTER in "
838 "FUNCTION %s at %L", sym->name,
839 ns->entries->sym->name, &sym->declared_at);
841 else
843 ts = &sym->ts;
844 if (ts->type == BT_UNKNOWN)
845 ts = gfc_get_default_type (sym->name, NULL);
846 switch (ts->type)
848 case BT_INTEGER:
849 if (ts->kind == gfc_default_integer_kind)
850 sym = NULL;
851 break;
852 case BT_REAL:
853 if (ts->kind == gfc_default_real_kind
854 || ts->kind == gfc_default_double_kind)
855 sym = NULL;
856 break;
857 case BT_COMPLEX:
858 if (ts->kind == gfc_default_complex_kind)
859 sym = NULL;
860 break;
861 case BT_LOGICAL:
862 if (ts->kind == gfc_default_logical_kind)
863 sym = NULL;
864 break;
865 case BT_UNKNOWN:
866 /* We will issue error elsewhere. */
867 sym = NULL;
868 break;
869 default:
870 break;
872 if (sym)
874 if (el == ns->entries)
875 gfc_error ("FUNCTION result %s can't be of type %s "
876 "in FUNCTION %s at %L", sym->name,
877 gfc_typename (ts), ns->entries->sym->name,
878 &sym->declared_at);
879 else
880 gfc_error ("ENTRY result %s can't be of type %s "
881 "in FUNCTION %s at %L", sym->name,
882 gfc_typename (ts), ns->entries->sym->name,
883 &sym->declared_at);
889 proc->attr.access = ACCESS_PRIVATE;
890 proc->attr.entry_master = 1;
892 /* Merge all the entry point arguments. */
893 for (el = ns->entries; el; el = el->next)
894 merge_argument_lists (proc, el->sym->formal);
896 /* Check the master formal arguments for any that are not
897 present in all entry points. */
898 for (el = ns->entries; el; el = el->next)
899 check_argument_lists (proc, el->sym->formal);
901 /* Use the master function for the function body. */
902 ns->proc_name = proc;
904 /* Finalize the new symbols. */
905 gfc_commit_symbols ();
907 /* Restore the original namespace. */
908 gfc_current_ns = old_ns;
912 /* Resolve common variables. */
913 static void
914 resolve_common_vars (gfc_common_head *common_block, bool named_common)
916 gfc_symbol *csym = common_block->head;
918 for (; csym; csym = csym->common_next)
920 /* gfc_add_in_common may have been called before, but the reported errors
921 have been ignored to continue parsing.
922 We do the checks again here. */
923 if (!csym->attr.use_assoc)
924 gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
926 if (csym->value || csym->attr.data)
928 if (!csym->ns->is_block_data)
929 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
930 "but only in BLOCK DATA initialization is "
931 "allowed", csym->name, &csym->declared_at);
932 else if (!named_common)
933 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
934 "in a blank COMMON but initialization is only "
935 "allowed in named common blocks", csym->name,
936 &csym->declared_at);
939 if (UNLIMITED_POLY (csym))
940 gfc_error_now ("%qs in cannot appear in COMMON at %L "
941 "[F2008:C5100]", csym->name, &csym->declared_at);
943 if (csym->ts.type != BT_DERIVED)
944 continue;
946 if (!(csym->ts.u.derived->attr.sequence
947 || csym->ts.u.derived->attr.is_bind_c))
948 gfc_error_now ("Derived type variable %qs in COMMON at %L "
949 "has neither the SEQUENCE nor the BIND(C) "
950 "attribute", csym->name, &csym->declared_at);
951 if (csym->ts.u.derived->attr.alloc_comp)
952 gfc_error_now ("Derived type variable %qs in COMMON at %L "
953 "has an ultimate component that is "
954 "allocatable", csym->name, &csym->declared_at);
955 if (gfc_has_default_initializer (csym->ts.u.derived))
956 gfc_error_now ("Derived type variable %qs in COMMON at %L "
957 "may not have default initializer", csym->name,
958 &csym->declared_at);
960 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
961 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
965 /* Resolve common blocks. */
966 static void
967 resolve_common_blocks (gfc_symtree *common_root)
969 gfc_symbol *sym;
970 gfc_gsymbol * gsym;
972 if (common_root == NULL)
973 return;
975 if (common_root->left)
976 resolve_common_blocks (common_root->left);
977 if (common_root->right)
978 resolve_common_blocks (common_root->right);
980 resolve_common_vars (common_root->n.common, true);
982 /* The common name is a global name - in Fortran 2003 also if it has a
983 C binding name, since Fortran 2008 only the C binding name is a global
984 identifier. */
985 if (!common_root->n.common->binding_label
986 || gfc_notification_std (GFC_STD_F2008))
988 gsym = gfc_find_gsymbol (gfc_gsym_root,
989 common_root->n.common->name);
991 if (gsym && gfc_notification_std (GFC_STD_F2008)
992 && gsym->type == GSYM_COMMON
993 && ((common_root->n.common->binding_label
994 && (!gsym->binding_label
995 || strcmp (common_root->n.common->binding_label,
996 gsym->binding_label) != 0))
997 || (!common_root->n.common->binding_label
998 && gsym->binding_label)))
1000 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1001 "identifier and must thus have the same binding name "
1002 "as the same-named COMMON block at %L: %s vs %s",
1003 common_root->n.common->name, &common_root->n.common->where,
1004 &gsym->where,
1005 common_root->n.common->binding_label
1006 ? common_root->n.common->binding_label : "(blank)",
1007 gsym->binding_label ? gsym->binding_label : "(blank)");
1008 return;
1011 if (gsym && gsym->type != GSYM_COMMON
1012 && !common_root->n.common->binding_label)
1014 gfc_error ("COMMON block %qs at %L uses the same global identifier "
1015 "as entity at %L",
1016 common_root->n.common->name, &common_root->n.common->where,
1017 &gsym->where);
1018 return;
1020 if (gsym && gsym->type != GSYM_COMMON)
1022 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1023 "%L sharing the identifier with global non-COMMON-block "
1024 "entity at %L", common_root->n.common->name,
1025 &common_root->n.common->where, &gsym->where);
1026 return;
1028 if (!gsym)
1030 gsym = gfc_get_gsymbol (common_root->n.common->name);
1031 gsym->type = GSYM_COMMON;
1032 gsym->where = common_root->n.common->where;
1033 gsym->defined = 1;
1035 gsym->used = 1;
1038 if (common_root->n.common->binding_label)
1040 gsym = gfc_find_gsymbol (gfc_gsym_root,
1041 common_root->n.common->binding_label);
1042 if (gsym && gsym->type != GSYM_COMMON)
1044 gfc_error ("COMMON block at %L with binding label %s uses the same "
1045 "global identifier as entity at %L",
1046 &common_root->n.common->where,
1047 common_root->n.common->binding_label, &gsym->where);
1048 return;
1050 if (!gsym)
1052 gsym = gfc_get_gsymbol (common_root->n.common->binding_label);
1053 gsym->type = GSYM_COMMON;
1054 gsym->where = common_root->n.common->where;
1055 gsym->defined = 1;
1057 gsym->used = 1;
1060 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1061 if (sym == NULL)
1062 return;
1064 if (sym->attr.flavor == FL_PARAMETER)
1065 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1066 sym->name, &common_root->n.common->where, &sym->declared_at);
1068 if (sym->attr.external)
1069 gfc_error ("COMMON block %qs at %L can not have the EXTERNAL attribute",
1070 sym->name, &common_root->n.common->where);
1072 if (sym->attr.intrinsic)
1073 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1074 sym->name, &common_root->n.common->where);
1075 else if (sym->attr.result
1076 || gfc_is_function_return_value (sym, gfc_current_ns))
1077 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1078 "that is also a function result", sym->name,
1079 &common_root->n.common->where);
1080 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1081 && sym->attr.proc != PROC_ST_FUNCTION)
1082 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1083 "that is also a global procedure", sym->name,
1084 &common_root->n.common->where);
1088 /* Resolve contained function types. Because contained functions can call one
1089 another, they have to be worked out before any of the contained procedures
1090 can be resolved.
1092 The good news is that if a function doesn't already have a type, the only
1093 way it can get one is through an IMPLICIT type or a RESULT variable, because
1094 by definition contained functions are contained namespace they're contained
1095 in, not in a sibling or parent namespace. */
1097 static void
1098 resolve_contained_functions (gfc_namespace *ns)
1100 gfc_namespace *child;
1101 gfc_entry_list *el;
1103 resolve_formal_arglists (ns);
1105 for (child = ns->contained; child; child = child->sibling)
1107 /* Resolve alternate entry points first. */
1108 resolve_entries (child);
1110 /* Then check function return types. */
1111 resolve_contained_fntype (child->proc_name, child);
1112 for (el = child->entries; el; el = el->next)
1113 resolve_contained_fntype (el->sym, child);
1118 static bool resolve_fl_derived0 (gfc_symbol *sym);
1119 static bool resolve_fl_struct (gfc_symbol *sym);
1122 /* Resolve all of the elements of a structure constructor and make sure that
1123 the types are correct. The 'init' flag indicates that the given
1124 constructor is an initializer. */
1126 static bool
1127 resolve_structure_cons (gfc_expr *expr, int init)
1129 gfc_constructor *cons;
1130 gfc_component *comp;
1131 bool t;
1132 symbol_attribute a;
1134 t = true;
1136 if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1138 if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1139 resolve_fl_derived0 (expr->ts.u.derived);
1140 else
1141 resolve_fl_struct (expr->ts.u.derived);
1144 cons = gfc_constructor_first (expr->value.constructor);
1146 /* A constructor may have references if it is the result of substituting a
1147 parameter variable. In this case we just pull out the component we
1148 want. */
1149 if (expr->ref)
1150 comp = expr->ref->u.c.sym->components;
1151 else
1152 comp = expr->ts.u.derived->components;
1154 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1156 int rank;
1158 if (!cons->expr)
1159 continue;
1161 if (!gfc_resolve_expr (cons->expr))
1163 t = false;
1164 continue;
1167 rank = comp->as ? comp->as->rank : 0;
1168 if (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->as)
1169 rank = CLASS_DATA (comp)->as->rank;
1171 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1172 && (comp->attr.allocatable || cons->expr->rank))
1174 gfc_error ("The rank of the element in the structure "
1175 "constructor at %L does not match that of the "
1176 "component (%d/%d)", &cons->expr->where,
1177 cons->expr->rank, rank);
1178 t = false;
1181 /* If we don't have the right type, try to convert it. */
1183 if (!comp->attr.proc_pointer &&
1184 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1186 if (strcmp (comp->name, "_extends") == 0)
1188 /* Can afford to be brutal with the _extends initializer.
1189 The derived type can get lost because it is PRIVATE
1190 but it is not usage constrained by the standard. */
1191 cons->expr->ts = comp->ts;
1193 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1195 gfc_error ("The element in the structure constructor at %L, "
1196 "for pointer component %qs, is %s but should be %s",
1197 &cons->expr->where, comp->name,
1198 gfc_basic_typename (cons->expr->ts.type),
1199 gfc_basic_typename (comp->ts.type));
1200 t = false;
1202 else
1204 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1205 if (t)
1206 t = t2;
1210 /* For strings, the length of the constructor should be the same as
1211 the one of the structure, ensure this if the lengths are known at
1212 compile time and when we are dealing with PARAMETER or structure
1213 constructors. */
1214 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1215 && comp->ts.u.cl->length
1216 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1217 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1218 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1219 && cons->expr->rank != 0
1220 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1221 comp->ts.u.cl->length->value.integer) != 0)
1223 if (cons->expr->expr_type == EXPR_VARIABLE
1224 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1226 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1227 to make use of the gfc_resolve_character_array_constructor
1228 machinery. The expression is later simplified away to
1229 an array of string literals. */
1230 gfc_expr *para = cons->expr;
1231 cons->expr = gfc_get_expr ();
1232 cons->expr->ts = para->ts;
1233 cons->expr->where = para->where;
1234 cons->expr->expr_type = EXPR_ARRAY;
1235 cons->expr->rank = para->rank;
1236 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1237 gfc_constructor_append_expr (&cons->expr->value.constructor,
1238 para, &cons->expr->where);
1240 if (cons->expr->expr_type == EXPR_ARRAY)
1242 gfc_constructor *p;
1243 p = gfc_constructor_first (cons->expr->value.constructor);
1244 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1246 gfc_charlen *cl, *cl2;
1248 cl2 = NULL;
1249 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1251 if (cl == cons->expr->ts.u.cl)
1252 break;
1253 cl2 = cl;
1256 gcc_assert (cl);
1258 if (cl2)
1259 cl2->next = cl->next;
1261 gfc_free_expr (cl->length);
1262 free (cl);
1265 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1266 cons->expr->ts.u.cl->length_from_typespec = true;
1267 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1268 gfc_resolve_character_array_constructor (cons->expr);
1272 if (cons->expr->expr_type == EXPR_NULL
1273 && !(comp->attr.pointer || comp->attr.allocatable
1274 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1275 || (comp->ts.type == BT_CLASS
1276 && (CLASS_DATA (comp)->attr.class_pointer
1277 || CLASS_DATA (comp)->attr.allocatable))))
1279 t = false;
1280 gfc_error ("The NULL in the structure constructor at %L is "
1281 "being applied to component %qs, which is neither "
1282 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1283 comp->name);
1286 if (comp->attr.proc_pointer && comp->ts.interface)
1288 /* Check procedure pointer interface. */
1289 gfc_symbol *s2 = NULL;
1290 gfc_component *c2;
1291 const char *name;
1292 char err[200];
1294 c2 = gfc_get_proc_ptr_comp (cons->expr);
1295 if (c2)
1297 s2 = c2->ts.interface;
1298 name = c2->name;
1300 else if (cons->expr->expr_type == EXPR_FUNCTION)
1302 s2 = cons->expr->symtree->n.sym->result;
1303 name = cons->expr->symtree->n.sym->result->name;
1305 else if (cons->expr->expr_type != EXPR_NULL)
1307 s2 = cons->expr->symtree->n.sym;
1308 name = cons->expr->symtree->n.sym->name;
1311 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1312 err, sizeof (err), NULL, NULL))
1314 gfc_error ("Interface mismatch for procedure-pointer component "
1315 "%qs in structure constructor at %L: %s",
1316 comp->name, &cons->expr->where, err);
1317 return false;
1321 if (!comp->attr.pointer || comp->attr.proc_pointer
1322 || cons->expr->expr_type == EXPR_NULL)
1323 continue;
1325 a = gfc_expr_attr (cons->expr);
1327 if (!a.pointer && !a.target)
1329 t = false;
1330 gfc_error ("The element in the structure constructor at %L, "
1331 "for pointer component %qs should be a POINTER or "
1332 "a TARGET", &cons->expr->where, comp->name);
1335 if (init)
1337 /* F08:C461. Additional checks for pointer initialization. */
1338 if (a.allocatable)
1340 t = false;
1341 gfc_error ("Pointer initialization target at %L "
1342 "must not be ALLOCATABLE ", &cons->expr->where);
1344 if (!a.save)
1346 t = false;
1347 gfc_error ("Pointer initialization target at %L "
1348 "must have the SAVE attribute", &cons->expr->where);
1352 /* F2003, C1272 (3). */
1353 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1354 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1355 || gfc_is_coindexed (cons->expr));
1356 if (impure && gfc_pure (NULL))
1358 t = false;
1359 gfc_error ("Invalid expression in the structure constructor for "
1360 "pointer component %qs at %L in PURE procedure",
1361 comp->name, &cons->expr->where);
1364 if (impure)
1365 gfc_unset_implicit_pure (NULL);
1368 return t;
1372 /****************** Expression name resolution ******************/
1374 /* Returns 0 if a symbol was not declared with a type or
1375 attribute declaration statement, nonzero otherwise. */
1377 static int
1378 was_declared (gfc_symbol *sym)
1380 symbol_attribute a;
1382 a = sym->attr;
1384 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1385 return 1;
1387 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1388 || a.optional || a.pointer || a.save || a.target || a.volatile_
1389 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1390 || a.asynchronous || a.codimension)
1391 return 1;
1393 return 0;
1397 /* Determine if a symbol is generic or not. */
1399 static int
1400 generic_sym (gfc_symbol *sym)
1402 gfc_symbol *s;
1404 if (sym->attr.generic ||
1405 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1406 return 1;
1408 if (was_declared (sym) || sym->ns->parent == NULL)
1409 return 0;
1411 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1413 if (s != NULL)
1415 if (s == sym)
1416 return 0;
1417 else
1418 return generic_sym (s);
1421 return 0;
1425 /* Determine if a symbol is specific or not. */
1427 static int
1428 specific_sym (gfc_symbol *sym)
1430 gfc_symbol *s;
1432 if (sym->attr.if_source == IFSRC_IFBODY
1433 || sym->attr.proc == PROC_MODULE
1434 || sym->attr.proc == PROC_INTERNAL
1435 || sym->attr.proc == PROC_ST_FUNCTION
1436 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1437 || sym->attr.external)
1438 return 1;
1440 if (was_declared (sym) || sym->ns->parent == NULL)
1441 return 0;
1443 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1445 return (s == NULL) ? 0 : specific_sym (s);
1449 /* Figure out if the procedure is specific, generic or unknown. */
1451 enum proc_type
1452 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1454 static proc_type
1455 procedure_kind (gfc_symbol *sym)
1457 if (generic_sym (sym))
1458 return PTYPE_GENERIC;
1460 if (specific_sym (sym))
1461 return PTYPE_SPECIFIC;
1463 return PTYPE_UNKNOWN;
1466 /* Check references to assumed size arrays. The flag need_full_assumed_size
1467 is nonzero when matching actual arguments. */
1469 static int need_full_assumed_size = 0;
1471 static bool
1472 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1474 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1475 return false;
1477 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1478 What should it be? */
1479 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1480 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1481 && (e->ref->u.ar.type == AR_FULL))
1483 gfc_error ("The upper bound in the last dimension must "
1484 "appear in the reference to the assumed size "
1485 "array %qs at %L", sym->name, &e->where);
1486 return true;
1488 return false;
1492 /* Look for bad assumed size array references in argument expressions
1493 of elemental and array valued intrinsic procedures. Since this is
1494 called from procedure resolution functions, it only recurses at
1495 operators. */
1497 static bool
1498 resolve_assumed_size_actual (gfc_expr *e)
1500 if (e == NULL)
1501 return false;
1503 switch (e->expr_type)
1505 case EXPR_VARIABLE:
1506 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1507 return true;
1508 break;
1510 case EXPR_OP:
1511 if (resolve_assumed_size_actual (e->value.op.op1)
1512 || resolve_assumed_size_actual (e->value.op.op2))
1513 return true;
1514 break;
1516 default:
1517 break;
1519 return false;
1523 /* Check a generic procedure, passed as an actual argument, to see if
1524 there is a matching specific name. If none, it is an error, and if
1525 more than one, the reference is ambiguous. */
1526 static int
1527 count_specific_procs (gfc_expr *e)
1529 int n;
1530 gfc_interface *p;
1531 gfc_symbol *sym;
1533 n = 0;
1534 sym = e->symtree->n.sym;
1536 for (p = sym->generic; p; p = p->next)
1537 if (strcmp (sym->name, p->sym->name) == 0)
1539 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1540 sym->name);
1541 n++;
1544 if (n > 1)
1545 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1546 &e->where);
1548 if (n == 0)
1549 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1550 "argument at %L", sym->name, &e->where);
1552 return n;
1556 /* See if a call to sym could possibly be a not allowed RECURSION because of
1557 a missing RECURSIVE declaration. This means that either sym is the current
1558 context itself, or sym is the parent of a contained procedure calling its
1559 non-RECURSIVE containing procedure.
1560 This also works if sym is an ENTRY. */
1562 static bool
1563 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1565 gfc_symbol* proc_sym;
1566 gfc_symbol* context_proc;
1567 gfc_namespace* real_context;
1569 if (sym->attr.flavor == FL_PROGRAM
1570 || gfc_fl_struct (sym->attr.flavor))
1571 return false;
1573 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1575 /* If we've got an ENTRY, find real procedure. */
1576 if (sym->attr.entry && sym->ns->entries)
1577 proc_sym = sym->ns->entries->sym;
1578 else
1579 proc_sym = sym;
1581 /* If sym is RECURSIVE, all is well of course. */
1582 if (proc_sym->attr.recursive || flag_recursive)
1583 return false;
1585 /* Find the context procedure's "real" symbol if it has entries.
1586 We look for a procedure symbol, so recurse on the parents if we don't
1587 find one (like in case of a BLOCK construct). */
1588 for (real_context = context; ; real_context = real_context->parent)
1590 /* We should find something, eventually! */
1591 gcc_assert (real_context);
1593 context_proc = (real_context->entries ? real_context->entries->sym
1594 : real_context->proc_name);
1596 /* In some special cases, there may not be a proc_name, like for this
1597 invalid code:
1598 real(bad_kind()) function foo () ...
1599 when checking the call to bad_kind ().
1600 In these cases, we simply return here and assume that the
1601 call is ok. */
1602 if (!context_proc)
1603 return false;
1605 if (context_proc->attr.flavor != FL_LABEL)
1606 break;
1609 /* A call from sym's body to itself is recursion, of course. */
1610 if (context_proc == proc_sym)
1611 return true;
1613 /* The same is true if context is a contained procedure and sym the
1614 containing one. */
1615 if (context_proc->attr.contained)
1617 gfc_symbol* parent_proc;
1619 gcc_assert (context->parent);
1620 parent_proc = (context->parent->entries ? context->parent->entries->sym
1621 : context->parent->proc_name);
1623 if (parent_proc == proc_sym)
1624 return true;
1627 return false;
1631 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1632 its typespec and formal argument list. */
1634 bool
1635 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1637 gfc_intrinsic_sym* isym = NULL;
1638 const char* symstd;
1640 if (sym->formal)
1641 return true;
1643 /* Already resolved. */
1644 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1645 return true;
1647 /* We already know this one is an intrinsic, so we don't call
1648 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1649 gfc_find_subroutine directly to check whether it is a function or
1650 subroutine. */
1652 if (sym->intmod_sym_id && sym->attr.subroutine)
1654 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1655 isym = gfc_intrinsic_subroutine_by_id (id);
1657 else if (sym->intmod_sym_id)
1659 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1660 isym = gfc_intrinsic_function_by_id (id);
1662 else if (!sym->attr.subroutine)
1663 isym = gfc_find_function (sym->name);
1665 if (isym && !sym->attr.subroutine)
1667 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1668 && !sym->attr.implicit_type)
1669 gfc_warning (OPT_Wsurprising,
1670 "Type specified for intrinsic function %qs at %L is"
1671 " ignored", sym->name, &sym->declared_at);
1673 if (!sym->attr.function &&
1674 !gfc_add_function(&sym->attr, sym->name, loc))
1675 return false;
1677 sym->ts = isym->ts;
1679 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1681 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1683 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1684 " specifier", sym->name, &sym->declared_at);
1685 return false;
1688 if (!sym->attr.subroutine &&
1689 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1690 return false;
1692 else
1694 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1695 &sym->declared_at);
1696 return false;
1699 gfc_copy_formal_args_intr (sym, isym, NULL);
1701 sym->attr.pure = isym->pure;
1702 sym->attr.elemental = isym->elemental;
1704 /* Check it is actually available in the standard settings. */
1705 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1707 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1708 "available in the current standard settings but %s. Use "
1709 "an appropriate %<-std=*%> option or enable "
1710 "%<-fall-intrinsics%> in order to use it.",
1711 sym->name, &sym->declared_at, symstd);
1712 return false;
1715 return true;
1719 /* Resolve a procedure expression, like passing it to a called procedure or as
1720 RHS for a procedure pointer assignment. */
1722 static bool
1723 resolve_procedure_expression (gfc_expr* expr)
1725 gfc_symbol* sym;
1727 if (expr->expr_type != EXPR_VARIABLE)
1728 return true;
1729 gcc_assert (expr->symtree);
1731 sym = expr->symtree->n.sym;
1733 if (sym->attr.intrinsic)
1734 gfc_resolve_intrinsic (sym, &expr->where);
1736 if (sym->attr.flavor != FL_PROCEDURE
1737 || (sym->attr.function && sym->result == sym))
1738 return true;
1740 /* A non-RECURSIVE procedure that is used as procedure expression within its
1741 own body is in danger of being called recursively. */
1742 if (is_illegal_recursion (sym, gfc_current_ns))
1743 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1744 " itself recursively. Declare it RECURSIVE or use"
1745 " %<-frecursive%>", sym->name, &expr->where);
1747 return true;
1751 /* Resolve an actual argument list. Most of the time, this is just
1752 resolving the expressions in the list.
1753 The exception is that we sometimes have to decide whether arguments
1754 that look like procedure arguments are really simple variable
1755 references. */
1757 static bool
1758 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1759 bool no_formal_args)
1761 gfc_symbol *sym;
1762 gfc_symtree *parent_st;
1763 gfc_expr *e;
1764 gfc_component *comp;
1765 int save_need_full_assumed_size;
1766 bool return_value = false;
1767 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1769 actual_arg = true;
1770 first_actual_arg = true;
1772 for (; arg; arg = arg->next)
1774 e = arg->expr;
1775 if (e == NULL)
1777 /* Check the label is a valid branching target. */
1778 if (arg->label)
1780 if (arg->label->defined == ST_LABEL_UNKNOWN)
1782 gfc_error ("Label %d referenced at %L is never defined",
1783 arg->label->value, &arg->label->where);
1784 goto cleanup;
1787 first_actual_arg = false;
1788 continue;
1791 if (e->expr_type == EXPR_VARIABLE
1792 && e->symtree->n.sym->attr.generic
1793 && no_formal_args
1794 && count_specific_procs (e) != 1)
1795 goto cleanup;
1797 if (e->ts.type != BT_PROCEDURE)
1799 save_need_full_assumed_size = need_full_assumed_size;
1800 if (e->expr_type != EXPR_VARIABLE)
1801 need_full_assumed_size = 0;
1802 if (!gfc_resolve_expr (e))
1803 goto cleanup;
1804 need_full_assumed_size = save_need_full_assumed_size;
1805 goto argument_list;
1808 /* See if the expression node should really be a variable reference. */
1810 sym = e->symtree->n.sym;
1812 if (sym->attr.flavor == FL_PROCEDURE
1813 || sym->attr.intrinsic
1814 || sym->attr.external)
1816 int actual_ok;
1818 /* If a procedure is not already determined to be something else
1819 check if it is intrinsic. */
1820 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1821 sym->attr.intrinsic = 1;
1823 if (sym->attr.proc == PROC_ST_FUNCTION)
1825 gfc_error ("Statement function %qs at %L is not allowed as an "
1826 "actual argument", sym->name, &e->where);
1829 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1830 sym->attr.subroutine);
1831 if (sym->attr.intrinsic && actual_ok == 0)
1833 gfc_error ("Intrinsic %qs at %L is not allowed as an "
1834 "actual argument", sym->name, &e->where);
1837 if (sym->attr.contained && !sym->attr.use_assoc
1838 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1840 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
1841 " used as actual argument at %L",
1842 sym->name, &e->where))
1843 goto cleanup;
1846 if (sym->attr.elemental && !sym->attr.intrinsic)
1848 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
1849 "allowed as an actual argument at %L", sym->name,
1850 &e->where);
1853 /* Check if a generic interface has a specific procedure
1854 with the same name before emitting an error. */
1855 if (sym->attr.generic && count_specific_procs (e) != 1)
1856 goto cleanup;
1858 /* Just in case a specific was found for the expression. */
1859 sym = e->symtree->n.sym;
1861 /* If the symbol is the function that names the current (or
1862 parent) scope, then we really have a variable reference. */
1864 if (gfc_is_function_return_value (sym, sym->ns))
1865 goto got_variable;
1867 /* If all else fails, see if we have a specific intrinsic. */
1868 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1870 gfc_intrinsic_sym *isym;
1872 isym = gfc_find_function (sym->name);
1873 if (isym == NULL || !isym->specific)
1875 gfc_error ("Unable to find a specific INTRINSIC procedure "
1876 "for the reference %qs at %L", sym->name,
1877 &e->where);
1878 goto cleanup;
1880 sym->ts = isym->ts;
1881 sym->attr.intrinsic = 1;
1882 sym->attr.function = 1;
1885 if (!gfc_resolve_expr (e))
1886 goto cleanup;
1887 goto argument_list;
1890 /* See if the name is a module procedure in a parent unit. */
1892 if (was_declared (sym) || sym->ns->parent == NULL)
1893 goto got_variable;
1895 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1897 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
1898 goto cleanup;
1901 if (parent_st == NULL)
1902 goto got_variable;
1904 sym = parent_st->n.sym;
1905 e->symtree = parent_st; /* Point to the right thing. */
1907 if (sym->attr.flavor == FL_PROCEDURE
1908 || sym->attr.intrinsic
1909 || sym->attr.external)
1911 if (!gfc_resolve_expr (e))
1912 goto cleanup;
1913 goto argument_list;
1916 got_variable:
1917 e->expr_type = EXPR_VARIABLE;
1918 e->ts = sym->ts;
1919 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1920 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1921 && CLASS_DATA (sym)->as))
1923 e->rank = sym->ts.type == BT_CLASS
1924 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
1925 e->ref = gfc_get_ref ();
1926 e->ref->type = REF_ARRAY;
1927 e->ref->u.ar.type = AR_FULL;
1928 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1929 ? CLASS_DATA (sym)->as : sym->as;
1932 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1933 primary.c (match_actual_arg). If above code determines that it
1934 is a variable instead, it needs to be resolved as it was not
1935 done at the beginning of this function. */
1936 save_need_full_assumed_size = need_full_assumed_size;
1937 if (e->expr_type != EXPR_VARIABLE)
1938 need_full_assumed_size = 0;
1939 if (!gfc_resolve_expr (e))
1940 goto cleanup;
1941 need_full_assumed_size = save_need_full_assumed_size;
1943 argument_list:
1944 /* Check argument list functions %VAL, %LOC and %REF. There is
1945 nothing to do for %REF. */
1946 if (arg->name && arg->name[0] == '%')
1948 if (strncmp ("%VAL", arg->name, 4) == 0)
1950 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1952 gfc_error ("By-value argument at %L is not of numeric "
1953 "type", &e->where);
1954 goto cleanup;
1957 if (e->rank)
1959 gfc_error ("By-value argument at %L cannot be an array or "
1960 "an array section", &e->where);
1961 goto cleanup;
1964 /* Intrinsics are still PROC_UNKNOWN here. However,
1965 since same file external procedures are not resolvable
1966 in gfortran, it is a good deal easier to leave them to
1967 intrinsic.c. */
1968 if (ptype != PROC_UNKNOWN
1969 && ptype != PROC_DUMMY
1970 && ptype != PROC_EXTERNAL
1971 && ptype != PROC_MODULE)
1973 gfc_error ("By-value argument at %L is not allowed "
1974 "in this context", &e->where);
1975 goto cleanup;
1979 /* Statement functions have already been excluded above. */
1980 else if (strncmp ("%LOC", arg->name, 4) == 0
1981 && e->ts.type == BT_PROCEDURE)
1983 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1985 gfc_error ("Passing internal procedure at %L by location "
1986 "not allowed", &e->where);
1987 goto cleanup;
1992 comp = gfc_get_proc_ptr_comp(e);
1993 if (e->expr_type == EXPR_VARIABLE
1994 && comp && comp->attr.elemental)
1996 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
1997 "allowed as an actual argument at %L", comp->name,
1998 &e->where);
2001 /* Fortran 2008, C1237. */
2002 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2003 && gfc_has_ultimate_pointer (e))
2005 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2006 "component", &e->where);
2007 goto cleanup;
2010 first_actual_arg = false;
2013 return_value = true;
2015 cleanup:
2016 actual_arg = actual_arg_sav;
2017 first_actual_arg = first_actual_arg_sav;
2019 return return_value;
2023 /* Do the checks of the actual argument list that are specific to elemental
2024 procedures. If called with c == NULL, we have a function, otherwise if
2025 expr == NULL, we have a subroutine. */
2027 static bool
2028 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2030 gfc_actual_arglist *arg0;
2031 gfc_actual_arglist *arg;
2032 gfc_symbol *esym = NULL;
2033 gfc_intrinsic_sym *isym = NULL;
2034 gfc_expr *e = NULL;
2035 gfc_intrinsic_arg *iformal = NULL;
2036 gfc_formal_arglist *eformal = NULL;
2037 bool formal_optional = false;
2038 bool set_by_optional = false;
2039 int i;
2040 int rank = 0;
2042 /* Is this an elemental procedure? */
2043 if (expr && expr->value.function.actual != NULL)
2045 if (expr->value.function.esym != NULL
2046 && expr->value.function.esym->attr.elemental)
2048 arg0 = expr->value.function.actual;
2049 esym = expr->value.function.esym;
2051 else if (expr->value.function.isym != NULL
2052 && expr->value.function.isym->elemental)
2054 arg0 = expr->value.function.actual;
2055 isym = expr->value.function.isym;
2057 else
2058 return true;
2060 else if (c && c->ext.actual != NULL)
2062 arg0 = c->ext.actual;
2064 if (c->resolved_sym)
2065 esym = c->resolved_sym;
2066 else
2067 esym = c->symtree->n.sym;
2068 gcc_assert (esym);
2070 if (!esym->attr.elemental)
2071 return true;
2073 else
2074 return true;
2076 /* The rank of an elemental is the rank of its array argument(s). */
2077 for (arg = arg0; arg; arg = arg->next)
2079 if (arg->expr != NULL && arg->expr->rank != 0)
2081 rank = arg->expr->rank;
2082 if (arg->expr->expr_type == EXPR_VARIABLE
2083 && arg->expr->symtree->n.sym->attr.optional)
2084 set_by_optional = true;
2086 /* Function specific; set the result rank and shape. */
2087 if (expr)
2089 expr->rank = rank;
2090 if (!expr->shape && arg->expr->shape)
2092 expr->shape = gfc_get_shape (rank);
2093 for (i = 0; i < rank; i++)
2094 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2097 break;
2101 /* If it is an array, it shall not be supplied as an actual argument
2102 to an elemental procedure unless an array of the same rank is supplied
2103 as an actual argument corresponding to a nonoptional dummy argument of
2104 that elemental procedure(12.4.1.5). */
2105 formal_optional = false;
2106 if (isym)
2107 iformal = isym->formal;
2108 else
2109 eformal = esym->formal;
2111 for (arg = arg0; arg; arg = arg->next)
2113 if (eformal)
2115 if (eformal->sym && eformal->sym->attr.optional)
2116 formal_optional = true;
2117 eformal = eformal->next;
2119 else if (isym && iformal)
2121 if (iformal->optional)
2122 formal_optional = true;
2123 iformal = iformal->next;
2125 else if (isym)
2126 formal_optional = true;
2128 if (pedantic && arg->expr != NULL
2129 && arg->expr->expr_type == EXPR_VARIABLE
2130 && arg->expr->symtree->n.sym->attr.optional
2131 && formal_optional
2132 && arg->expr->rank
2133 && (set_by_optional || arg->expr->rank != rank)
2134 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2136 gfc_warning (0, "%qs at %L is an array and OPTIONAL; IF IT IS "
2137 "MISSING, it cannot be the actual argument of an "
2138 "ELEMENTAL procedure unless there is a non-optional "
2139 "argument with the same rank (12.4.1.5)",
2140 arg->expr->symtree->n.sym->name, &arg->expr->where);
2144 for (arg = arg0; arg; arg = arg->next)
2146 if (arg->expr == NULL || arg->expr->rank == 0)
2147 continue;
2149 /* Being elemental, the last upper bound of an assumed size array
2150 argument must be present. */
2151 if (resolve_assumed_size_actual (arg->expr))
2152 return false;
2154 /* Elemental procedure's array actual arguments must conform. */
2155 if (e != NULL)
2157 if (!gfc_check_conformance (arg->expr, e, "elemental procedure"))
2158 return false;
2160 else
2161 e = arg->expr;
2164 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2165 is an array, the intent inout/out variable needs to be also an array. */
2166 if (rank > 0 && esym && expr == NULL)
2167 for (eformal = esym->formal, arg = arg0; arg && eformal;
2168 arg = arg->next, eformal = eformal->next)
2169 if ((eformal->sym->attr.intent == INTENT_OUT
2170 || eformal->sym->attr.intent == INTENT_INOUT)
2171 && arg->expr && arg->expr->rank == 0)
2173 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2174 "ELEMENTAL subroutine %qs is a scalar, but another "
2175 "actual argument is an array", &arg->expr->where,
2176 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2177 : "INOUT", eformal->sym->name, esym->name);
2178 return false;
2180 return true;
2184 /* This function does the checking of references to global procedures
2185 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2186 77 and 95 standards. It checks for a gsymbol for the name, making
2187 one if it does not already exist. If it already exists, then the
2188 reference being resolved must correspond to the type of gsymbol.
2189 Otherwise, the new symbol is equipped with the attributes of the
2190 reference. The corresponding code that is called in creating
2191 global entities is parse.c.
2193 In addition, for all but -std=legacy, the gsymbols are used to
2194 check the interfaces of external procedures from the same file.
2195 The namespace of the gsymbol is resolved and then, once this is
2196 done the interface is checked. */
2199 static bool
2200 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2202 if (!gsym_ns->proc_name->attr.recursive)
2203 return true;
2205 if (sym->ns == gsym_ns)
2206 return false;
2208 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2209 return false;
2211 return true;
2214 static bool
2215 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2217 if (gsym_ns->entries)
2219 gfc_entry_list *entry = gsym_ns->entries;
2221 for (; entry; entry = entry->next)
2223 if (strcmp (sym->name, entry->sym->name) == 0)
2225 if (strcmp (gsym_ns->proc_name->name,
2226 sym->ns->proc_name->name) == 0)
2227 return false;
2229 if (sym->ns->parent
2230 && strcmp (gsym_ns->proc_name->name,
2231 sym->ns->parent->proc_name->name) == 0)
2232 return false;
2236 return true;
2240 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2242 bool
2243 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2245 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2247 for ( ; arg; arg = arg->next)
2249 if (!arg->sym)
2250 continue;
2252 if (arg->sym->attr.allocatable) /* (2a) */
2254 strncpy (errmsg, _("allocatable argument"), err_len);
2255 return true;
2257 else if (arg->sym->attr.asynchronous)
2259 strncpy (errmsg, _("asynchronous argument"), err_len);
2260 return true;
2262 else if (arg->sym->attr.optional)
2264 strncpy (errmsg, _("optional argument"), err_len);
2265 return true;
2267 else if (arg->sym->attr.pointer)
2269 strncpy (errmsg, _("pointer argument"), err_len);
2270 return true;
2272 else if (arg->sym->attr.target)
2274 strncpy (errmsg, _("target argument"), err_len);
2275 return true;
2277 else if (arg->sym->attr.value)
2279 strncpy (errmsg, _("value argument"), err_len);
2280 return true;
2282 else if (arg->sym->attr.volatile_)
2284 strncpy (errmsg, _("volatile argument"), err_len);
2285 return true;
2287 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2289 strncpy (errmsg, _("assumed-shape argument"), err_len);
2290 return true;
2292 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2294 strncpy (errmsg, _("assumed-rank argument"), err_len);
2295 return true;
2297 else if (arg->sym->attr.codimension) /* (2c) */
2299 strncpy (errmsg, _("coarray argument"), err_len);
2300 return true;
2302 else if (false) /* (2d) TODO: parametrized derived type */
2304 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2305 return true;
2307 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2309 strncpy (errmsg, _("polymorphic argument"), err_len);
2310 return true;
2312 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2314 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2315 return true;
2317 else if (arg->sym->ts.type == BT_ASSUMED)
2319 /* As assumed-type is unlimited polymorphic (cf. above).
2320 See also TS 29113, Note 6.1. */
2321 strncpy (errmsg, _("assumed-type argument"), err_len);
2322 return true;
2326 if (sym->attr.function)
2328 gfc_symbol *res = sym->result ? sym->result : sym;
2330 if (res->attr.dimension) /* (3a) */
2332 strncpy (errmsg, _("array result"), err_len);
2333 return true;
2335 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2337 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2338 return true;
2340 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2341 && res->ts.u.cl->length
2342 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2344 strncpy (errmsg, _("result with non-constant character length"), err_len);
2345 return true;
2349 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2351 strncpy (errmsg, _("elemental procedure"), err_len);
2352 return true;
2354 else if (sym->attr.is_bind_c) /* (5) */
2356 strncpy (errmsg, _("bind(c) procedure"), err_len);
2357 return true;
2360 return false;
2364 static void
2365 resolve_global_procedure (gfc_symbol *sym, locus *where,
2366 gfc_actual_arglist **actual, int sub)
2368 gfc_gsymbol * gsym;
2369 gfc_namespace *ns;
2370 enum gfc_symbol_type type;
2371 char reason[200];
2373 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2375 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name);
2377 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2378 gfc_global_used (gsym, where);
2380 if ((sym->attr.if_source == IFSRC_UNKNOWN
2381 || sym->attr.if_source == IFSRC_IFBODY)
2382 && gsym->type != GSYM_UNKNOWN
2383 && !gsym->binding_label
2384 && gsym->ns
2385 && gsym->ns->resolved != -1
2386 && gsym->ns->proc_name
2387 && not_in_recursive (sym, gsym->ns)
2388 && not_entry_self_reference (sym, gsym->ns))
2390 gfc_symbol *def_sym;
2392 /* Resolve the gsymbol namespace if needed. */
2393 if (!gsym->ns->resolved)
2395 gfc_dt_list *old_dt_list;
2397 /* Stash away derived types so that the backend_decls do not
2398 get mixed up. */
2399 old_dt_list = gfc_derived_types;
2400 gfc_derived_types = NULL;
2402 gfc_resolve (gsym->ns);
2404 /* Store the new derived types with the global namespace. */
2405 if (gfc_derived_types)
2406 gsym->ns->derived_types = gfc_derived_types;
2408 /* Restore the derived types of this namespace. */
2409 gfc_derived_types = old_dt_list;
2412 /* Make sure that translation for the gsymbol occurs before
2413 the procedure currently being resolved. */
2414 ns = gfc_global_ns_list;
2415 for (; ns && ns != gsym->ns; ns = ns->sibling)
2417 if (ns->sibling == gsym->ns)
2419 ns->sibling = gsym->ns->sibling;
2420 gsym->ns->sibling = gfc_global_ns_list;
2421 gfc_global_ns_list = gsym->ns;
2422 break;
2426 def_sym = gsym->ns->proc_name;
2428 /* This can happen if a binding name has been specified. */
2429 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2430 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2432 if (def_sym->attr.entry_master)
2434 gfc_entry_list *entry;
2435 for (entry = gsym->ns->entries; entry; entry = entry->next)
2436 if (strcmp (entry->sym->name, sym->name) == 0)
2438 def_sym = entry->sym;
2439 break;
2443 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2445 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2446 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2447 gfc_typename (&def_sym->ts));
2448 goto done;
2451 if (sym->attr.if_source == IFSRC_UNKNOWN
2452 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2454 gfc_error ("Explicit interface required for %qs at %L: %s",
2455 sym->name, &sym->declared_at, reason);
2456 goto done;
2459 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2460 /* Turn erros into warnings with -std=gnu and -std=legacy. */
2461 gfc_errors_to_warnings (true);
2463 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2464 reason, sizeof(reason), NULL, NULL))
2466 gfc_error ("Interface mismatch in global procedure %qs at %L: %s ",
2467 sym->name, &sym->declared_at, reason);
2468 goto done;
2471 if (!pedantic
2472 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2473 && !(gfc_option.warn_std & GFC_STD_GNU)))
2474 gfc_errors_to_warnings (true);
2476 if (sym->attr.if_source != IFSRC_IFBODY)
2477 gfc_procedure_use (def_sym, actual, where);
2480 done:
2481 gfc_errors_to_warnings (false);
2483 if (gsym->type == GSYM_UNKNOWN)
2485 gsym->type = type;
2486 gsym->where = *where;
2489 gsym->used = 1;
2493 /************* Function resolution *************/
2495 /* Resolve a function call known to be generic.
2496 Section 14.1.2.4.1. */
2498 static match
2499 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2501 gfc_symbol *s;
2503 if (sym->attr.generic)
2505 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2506 if (s != NULL)
2508 expr->value.function.name = s->name;
2509 expr->value.function.esym = s;
2511 if (s->ts.type != BT_UNKNOWN)
2512 expr->ts = s->ts;
2513 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2514 expr->ts = s->result->ts;
2516 if (s->as != NULL)
2517 expr->rank = s->as->rank;
2518 else if (s->result != NULL && s->result->as != NULL)
2519 expr->rank = s->result->as->rank;
2521 gfc_set_sym_referenced (expr->value.function.esym);
2523 return MATCH_YES;
2526 /* TODO: Need to search for elemental references in generic
2527 interface. */
2530 if (sym->attr.intrinsic)
2531 return gfc_intrinsic_func_interface (expr, 0);
2533 return MATCH_NO;
2537 static bool
2538 resolve_generic_f (gfc_expr *expr)
2540 gfc_symbol *sym;
2541 match m;
2542 gfc_interface *intr = NULL;
2544 sym = expr->symtree->n.sym;
2546 for (;;)
2548 m = resolve_generic_f0 (expr, sym);
2549 if (m == MATCH_YES)
2550 return true;
2551 else if (m == MATCH_ERROR)
2552 return false;
2554 generic:
2555 if (!intr)
2556 for (intr = sym->generic; intr; intr = intr->next)
2557 if (gfc_fl_struct (intr->sym->attr.flavor))
2558 break;
2560 if (sym->ns->parent == NULL)
2561 break;
2562 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2564 if (sym == NULL)
2565 break;
2566 if (!generic_sym (sym))
2567 goto generic;
2570 /* Last ditch attempt. See if the reference is to an intrinsic
2571 that possesses a matching interface. 14.1.2.4 */
2572 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2574 if (gfc_init_expr_flag)
2575 gfc_error ("Function %qs in initialization expression at %L "
2576 "must be an intrinsic function",
2577 expr->symtree->n.sym->name, &expr->where);
2578 else
2579 gfc_error ("There is no specific function for the generic %qs "
2580 "at %L", expr->symtree->n.sym->name, &expr->where);
2581 return false;
2584 if (intr)
2586 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2587 NULL, false))
2588 return false;
2589 return resolve_structure_cons (expr, 0);
2592 m = gfc_intrinsic_func_interface (expr, 0);
2593 if (m == MATCH_YES)
2594 return true;
2596 if (m == MATCH_NO)
2597 gfc_error ("Generic function %qs at %L is not consistent with a "
2598 "specific intrinsic interface", expr->symtree->n.sym->name,
2599 &expr->where);
2601 return false;
2605 /* Resolve a function call known to be specific. */
2607 static match
2608 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2610 match m;
2612 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2614 if (sym->attr.dummy)
2616 sym->attr.proc = PROC_DUMMY;
2617 goto found;
2620 sym->attr.proc = PROC_EXTERNAL;
2621 goto found;
2624 if (sym->attr.proc == PROC_MODULE
2625 || sym->attr.proc == PROC_ST_FUNCTION
2626 || sym->attr.proc == PROC_INTERNAL)
2627 goto found;
2629 if (sym->attr.intrinsic)
2631 m = gfc_intrinsic_func_interface (expr, 1);
2632 if (m == MATCH_YES)
2633 return MATCH_YES;
2634 if (m == MATCH_NO)
2635 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2636 "with an intrinsic", sym->name, &expr->where);
2638 return MATCH_ERROR;
2641 return MATCH_NO;
2643 found:
2644 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2646 if (sym->result)
2647 expr->ts = sym->result->ts;
2648 else
2649 expr->ts = sym->ts;
2650 expr->value.function.name = sym->name;
2651 expr->value.function.esym = sym;
2652 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2653 error(s). */
2654 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2655 return MATCH_ERROR;
2656 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2657 expr->rank = CLASS_DATA (sym)->as->rank;
2658 else if (sym->as != NULL)
2659 expr->rank = sym->as->rank;
2661 return MATCH_YES;
2665 static bool
2666 resolve_specific_f (gfc_expr *expr)
2668 gfc_symbol *sym;
2669 match m;
2671 sym = expr->symtree->n.sym;
2673 for (;;)
2675 m = resolve_specific_f0 (sym, expr);
2676 if (m == MATCH_YES)
2677 return true;
2678 if (m == MATCH_ERROR)
2679 return false;
2681 if (sym->ns->parent == NULL)
2682 break;
2684 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2686 if (sym == NULL)
2687 break;
2690 gfc_error ("Unable to resolve the specific function %qs at %L",
2691 expr->symtree->n.sym->name, &expr->where);
2693 return true;
2697 /* Resolve a procedure call not known to be generic nor specific. */
2699 static bool
2700 resolve_unknown_f (gfc_expr *expr)
2702 gfc_symbol *sym;
2703 gfc_typespec *ts;
2705 sym = expr->symtree->n.sym;
2707 if (sym->attr.dummy)
2709 sym->attr.proc = PROC_DUMMY;
2710 expr->value.function.name = sym->name;
2711 goto set_type;
2714 /* See if we have an intrinsic function reference. */
2716 if (gfc_is_intrinsic (sym, 0, expr->where))
2718 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2719 return true;
2720 return false;
2723 /* The reference is to an external name. */
2725 sym->attr.proc = PROC_EXTERNAL;
2726 expr->value.function.name = sym->name;
2727 expr->value.function.esym = expr->symtree->n.sym;
2729 if (sym->as != NULL)
2730 expr->rank = sym->as->rank;
2732 /* Type of the expression is either the type of the symbol or the
2733 default type of the symbol. */
2735 set_type:
2736 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2738 if (sym->ts.type != BT_UNKNOWN)
2739 expr->ts = sym->ts;
2740 else
2742 ts = gfc_get_default_type (sym->name, sym->ns);
2744 if (ts->type == BT_UNKNOWN)
2746 gfc_error ("Function %qs at %L has no IMPLICIT type",
2747 sym->name, &expr->where);
2748 return false;
2750 else
2751 expr->ts = *ts;
2754 return true;
2758 /* Return true, if the symbol is an external procedure. */
2759 static bool
2760 is_external_proc (gfc_symbol *sym)
2762 if (!sym->attr.dummy && !sym->attr.contained
2763 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2764 && sym->attr.proc != PROC_ST_FUNCTION
2765 && !sym->attr.proc_pointer
2766 && !sym->attr.use_assoc
2767 && sym->name)
2768 return true;
2770 return false;
2774 /* Figure out if a function reference is pure or not. Also set the name
2775 of the function for a potential error message. Return nonzero if the
2776 function is PURE, zero if not. */
2777 static int
2778 pure_stmt_function (gfc_expr *, gfc_symbol *);
2780 static int
2781 pure_function (gfc_expr *e, const char **name)
2783 int pure;
2784 gfc_component *comp;
2786 *name = NULL;
2788 if (e->symtree != NULL
2789 && e->symtree->n.sym != NULL
2790 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2791 return pure_stmt_function (e, e->symtree->n.sym);
2793 comp = gfc_get_proc_ptr_comp (e);
2794 if (comp)
2796 pure = gfc_pure (comp->ts.interface);
2797 *name = comp->name;
2799 else if (e->value.function.esym)
2801 pure = gfc_pure (e->value.function.esym);
2802 *name = e->value.function.esym->name;
2804 else if (e->value.function.isym)
2806 pure = e->value.function.isym->pure
2807 || e->value.function.isym->elemental;
2808 *name = e->value.function.isym->name;
2810 else
2812 /* Implicit functions are not pure. */
2813 pure = 0;
2814 *name = e->value.function.name;
2817 return pure;
2821 static bool
2822 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2823 int *f ATTRIBUTE_UNUSED)
2825 const char *name;
2827 /* Don't bother recursing into other statement functions
2828 since they will be checked individually for purity. */
2829 if (e->expr_type != EXPR_FUNCTION
2830 || !e->symtree
2831 || e->symtree->n.sym == sym
2832 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2833 return false;
2835 return pure_function (e, &name) ? false : true;
2839 static int
2840 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2842 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2846 /* Check if an impure function is allowed in the current context. */
2848 static bool check_pure_function (gfc_expr *e)
2850 const char *name = NULL;
2851 if (!pure_function (e, &name) && name)
2853 if (forall_flag)
2855 gfc_error ("Reference to impure function %qs at %L inside a "
2856 "FORALL %s", name, &e->where,
2857 forall_flag == 2 ? "mask" : "block");
2858 return false;
2860 else if (gfc_do_concurrent_flag)
2862 gfc_error ("Reference to impure function %qs at %L inside a "
2863 "DO CONCURRENT %s", name, &e->where,
2864 gfc_do_concurrent_flag == 2 ? "mask" : "block");
2865 return false;
2867 else if (gfc_pure (NULL))
2869 gfc_error ("Reference to impure function %qs at %L "
2870 "within a PURE procedure", name, &e->where);
2871 return false;
2873 gfc_unset_implicit_pure (NULL);
2875 return true;
2879 /* Update current procedure's array_outer_dependency flag, considering
2880 a call to procedure SYM. */
2882 static void
2883 update_current_proc_array_outer_dependency (gfc_symbol *sym)
2885 /* Check to see if this is a sibling function that has not yet
2886 been resolved. */
2887 gfc_namespace *sibling = gfc_current_ns->sibling;
2888 for (; sibling; sibling = sibling->sibling)
2890 if (sibling->proc_name == sym)
2892 gfc_resolve (sibling);
2893 break;
2897 /* If SYM has references to outer arrays, so has the procedure calling
2898 SYM. If SYM is a procedure pointer, we can assume the worst. */
2899 if (sym->attr.array_outer_dependency
2900 || sym->attr.proc_pointer)
2901 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
2905 /* Resolve a function call, which means resolving the arguments, then figuring
2906 out which entity the name refers to. */
2908 static bool
2909 resolve_function (gfc_expr *expr)
2911 gfc_actual_arglist *arg;
2912 gfc_symbol *sym;
2913 bool t;
2914 int temp;
2915 procedure_type p = PROC_INTRINSIC;
2916 bool no_formal_args;
2918 sym = NULL;
2919 if (expr->symtree)
2920 sym = expr->symtree->n.sym;
2922 /* If this is a procedure pointer component, it has already been resolved. */
2923 if (gfc_is_proc_ptr_comp (expr))
2924 return true;
2926 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
2927 another caf_get. */
2928 if (sym && sym->attr.intrinsic
2929 && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
2930 || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
2931 return true;
2933 if (sym && sym->attr.intrinsic
2934 && !gfc_resolve_intrinsic (sym, &expr->where))
2935 return false;
2937 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2939 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
2940 return false;
2943 /* If this ia a deferred TBP with an abstract interface (which may
2944 of course be referenced), expr->value.function.esym will be set. */
2945 if (sym && sym->attr.abstract && !expr->value.function.esym)
2947 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
2948 sym->name, &expr->where);
2949 return false;
2952 /* Switch off assumed size checking and do this again for certain kinds
2953 of procedure, once the procedure itself is resolved. */
2954 need_full_assumed_size++;
2956 if (expr->symtree && expr->symtree->n.sym)
2957 p = expr->symtree->n.sym->attr.proc;
2959 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2960 inquiry_argument = true;
2961 no_formal_args = sym && is_external_proc (sym)
2962 && gfc_sym_get_dummy_args (sym) == NULL;
2964 if (!resolve_actual_arglist (expr->value.function.actual,
2965 p, no_formal_args))
2967 inquiry_argument = false;
2968 return false;
2971 inquiry_argument = false;
2973 /* Resume assumed_size checking. */
2974 need_full_assumed_size--;
2976 /* If the procedure is external, check for usage. */
2977 if (sym && is_external_proc (sym))
2978 resolve_global_procedure (sym, &expr->where,
2979 &expr->value.function.actual, 0);
2981 if (sym && sym->ts.type == BT_CHARACTER
2982 && sym->ts.u.cl
2983 && sym->ts.u.cl->length == NULL
2984 && !sym->attr.dummy
2985 && !sym->ts.deferred
2986 && expr->value.function.esym == NULL
2987 && !sym->attr.contained)
2989 /* Internal procedures are taken care of in resolve_contained_fntype. */
2990 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
2991 "be used at %L since it is not a dummy argument",
2992 sym->name, &expr->where);
2993 return false;
2996 /* See if function is already resolved. */
2998 if (expr->value.function.name != NULL
2999 || expr->value.function.isym != NULL)
3001 if (expr->ts.type == BT_UNKNOWN)
3002 expr->ts = sym->ts;
3003 t = true;
3005 else
3007 /* Apply the rules of section 14.1.2. */
3009 switch (procedure_kind (sym))
3011 case PTYPE_GENERIC:
3012 t = resolve_generic_f (expr);
3013 break;
3015 case PTYPE_SPECIFIC:
3016 t = resolve_specific_f (expr);
3017 break;
3019 case PTYPE_UNKNOWN:
3020 t = resolve_unknown_f (expr);
3021 break;
3023 default:
3024 gfc_internal_error ("resolve_function(): bad function type");
3028 /* If the expression is still a function (it might have simplified),
3029 then we check to see if we are calling an elemental function. */
3031 if (expr->expr_type != EXPR_FUNCTION)
3032 return t;
3034 temp = need_full_assumed_size;
3035 need_full_assumed_size = 0;
3037 if (!resolve_elemental_actual (expr, NULL))
3038 return false;
3040 if (omp_workshare_flag
3041 && expr->value.function.esym
3042 && ! gfc_elemental (expr->value.function.esym))
3044 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3045 "in WORKSHARE construct", expr->value.function.esym->name,
3046 &expr->where);
3047 t = false;
3050 #define GENERIC_ID expr->value.function.isym->id
3051 else if (expr->value.function.actual != NULL
3052 && expr->value.function.isym != NULL
3053 && GENERIC_ID != GFC_ISYM_LBOUND
3054 && GENERIC_ID != GFC_ISYM_LCOBOUND
3055 && GENERIC_ID != GFC_ISYM_UCOBOUND
3056 && GENERIC_ID != GFC_ISYM_LEN
3057 && GENERIC_ID != GFC_ISYM_LOC
3058 && GENERIC_ID != GFC_ISYM_C_LOC
3059 && GENERIC_ID != GFC_ISYM_PRESENT)
3061 /* Array intrinsics must also have the last upper bound of an
3062 assumed size array argument. UBOUND and SIZE have to be
3063 excluded from the check if the second argument is anything
3064 than a constant. */
3066 for (arg = expr->value.function.actual; arg; arg = arg->next)
3068 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3069 && arg == expr->value.function.actual
3070 && arg->next != NULL && arg->next->expr)
3072 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3073 break;
3075 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
3076 break;
3078 if ((int)mpz_get_si (arg->next->expr->value.integer)
3079 < arg->expr->rank)
3080 break;
3083 if (arg->expr != NULL
3084 && arg->expr->rank > 0
3085 && resolve_assumed_size_actual (arg->expr))
3086 return false;
3089 #undef GENERIC_ID
3091 need_full_assumed_size = temp;
3093 if (!check_pure_function(expr))
3094 t = false;
3096 /* Functions without the RECURSIVE attribution are not allowed to
3097 * call themselves. */
3098 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3100 gfc_symbol *esym;
3101 esym = expr->value.function.esym;
3103 if (is_illegal_recursion (esym, gfc_current_ns))
3105 if (esym->attr.entry && esym->ns->entries)
3106 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3107 " function %qs is not RECURSIVE",
3108 esym->name, &expr->where, esym->ns->entries->sym->name);
3109 else
3110 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3111 " is not RECURSIVE", esym->name, &expr->where);
3113 t = false;
3117 /* Character lengths of use associated functions may contains references to
3118 symbols not referenced from the current program unit otherwise. Make sure
3119 those symbols are marked as referenced. */
3121 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3122 && expr->value.function.esym->attr.use_assoc)
3124 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3127 /* Make sure that the expression has a typespec that works. */
3128 if (expr->ts.type == BT_UNKNOWN)
3130 if (expr->symtree->n.sym->result
3131 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3132 && !expr->symtree->n.sym->result->attr.proc_pointer)
3133 expr->ts = expr->symtree->n.sym->result->ts;
3136 if (!expr->ref && !expr->value.function.isym)
3138 if (expr->value.function.esym)
3139 update_current_proc_array_outer_dependency (expr->value.function.esym);
3140 else
3141 update_current_proc_array_outer_dependency (sym);
3143 else if (expr->ref)
3144 /* typebound procedure: Assume the worst. */
3145 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3147 return t;
3151 /************* Subroutine resolution *************/
3153 static bool
3154 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3156 if (gfc_pure (sym))
3157 return true;
3159 if (forall_flag)
3161 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3162 name, loc);
3163 return false;
3165 else if (gfc_do_concurrent_flag)
3167 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3168 "PURE", name, loc);
3169 return false;
3171 else if (gfc_pure (NULL))
3173 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3174 return false;
3177 gfc_unset_implicit_pure (NULL);
3178 return true;
3182 static match
3183 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3185 gfc_symbol *s;
3187 if (sym->attr.generic)
3189 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3190 if (s != NULL)
3192 c->resolved_sym = s;
3193 if (!pure_subroutine (s, s->name, &c->loc))
3194 return MATCH_ERROR;
3195 return MATCH_YES;
3198 /* TODO: Need to search for elemental references in generic interface. */
3201 if (sym->attr.intrinsic)
3202 return gfc_intrinsic_sub_interface (c, 0);
3204 return MATCH_NO;
3208 static bool
3209 resolve_generic_s (gfc_code *c)
3211 gfc_symbol *sym;
3212 match m;
3214 sym = c->symtree->n.sym;
3216 for (;;)
3218 m = resolve_generic_s0 (c, sym);
3219 if (m == MATCH_YES)
3220 return true;
3221 else if (m == MATCH_ERROR)
3222 return false;
3224 generic:
3225 if (sym->ns->parent == NULL)
3226 break;
3227 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3229 if (sym == NULL)
3230 break;
3231 if (!generic_sym (sym))
3232 goto generic;
3235 /* Last ditch attempt. See if the reference is to an intrinsic
3236 that possesses a matching interface. 14.1.2.4 */
3237 sym = c->symtree->n.sym;
3239 if (!gfc_is_intrinsic (sym, 1, c->loc))
3241 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3242 sym->name, &c->loc);
3243 return false;
3246 m = gfc_intrinsic_sub_interface (c, 0);
3247 if (m == MATCH_YES)
3248 return true;
3249 if (m == MATCH_NO)
3250 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3251 "intrinsic subroutine interface", sym->name, &c->loc);
3253 return false;
3257 /* Resolve a subroutine call known to be specific. */
3259 static match
3260 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3262 match m;
3264 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3266 if (sym->attr.dummy)
3268 sym->attr.proc = PROC_DUMMY;
3269 goto found;
3272 sym->attr.proc = PROC_EXTERNAL;
3273 goto found;
3276 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3277 goto found;
3279 if (sym->attr.intrinsic)
3281 m = gfc_intrinsic_sub_interface (c, 1);
3282 if (m == MATCH_YES)
3283 return MATCH_YES;
3284 if (m == MATCH_NO)
3285 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3286 "with an intrinsic", sym->name, &c->loc);
3288 return MATCH_ERROR;
3291 return MATCH_NO;
3293 found:
3294 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3296 c->resolved_sym = sym;
3297 if (!pure_subroutine (sym, sym->name, &c->loc))
3298 return MATCH_ERROR;
3300 return MATCH_YES;
3304 static bool
3305 resolve_specific_s (gfc_code *c)
3307 gfc_symbol *sym;
3308 match m;
3310 sym = c->symtree->n.sym;
3312 for (;;)
3314 m = resolve_specific_s0 (c, sym);
3315 if (m == MATCH_YES)
3316 return true;
3317 if (m == MATCH_ERROR)
3318 return false;
3320 if (sym->ns->parent == NULL)
3321 break;
3323 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3325 if (sym == NULL)
3326 break;
3329 sym = c->symtree->n.sym;
3330 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3331 sym->name, &c->loc);
3333 return false;
3337 /* Resolve a subroutine call not known to be generic nor specific. */
3339 static bool
3340 resolve_unknown_s (gfc_code *c)
3342 gfc_symbol *sym;
3344 sym = c->symtree->n.sym;
3346 if (sym->attr.dummy)
3348 sym->attr.proc = PROC_DUMMY;
3349 goto found;
3352 /* See if we have an intrinsic function reference. */
3354 if (gfc_is_intrinsic (sym, 1, c->loc))
3356 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3357 return true;
3358 return false;
3361 /* The reference is to an external name. */
3363 found:
3364 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3366 c->resolved_sym = sym;
3368 return pure_subroutine (sym, sym->name, &c->loc);
3372 /* Resolve a subroutine call. Although it was tempting to use the same code
3373 for functions, subroutines and functions are stored differently and this
3374 makes things awkward. */
3376 static bool
3377 resolve_call (gfc_code *c)
3379 bool t;
3380 procedure_type ptype = PROC_INTRINSIC;
3381 gfc_symbol *csym, *sym;
3382 bool no_formal_args;
3384 csym = c->symtree ? c->symtree->n.sym : NULL;
3386 if (csym && csym->ts.type != BT_UNKNOWN)
3388 gfc_error ("%qs at %L has a type, which is not consistent with "
3389 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3390 return false;
3393 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3395 gfc_symtree *st;
3396 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3397 sym = st ? st->n.sym : NULL;
3398 if (sym && csym != sym
3399 && sym->ns == gfc_current_ns
3400 && sym->attr.flavor == FL_PROCEDURE
3401 && sym->attr.contained)
3403 sym->refs++;
3404 if (csym->attr.generic)
3405 c->symtree->n.sym = sym;
3406 else
3407 c->symtree = st;
3408 csym = c->symtree->n.sym;
3412 /* If this ia a deferred TBP, c->expr1 will be set. */
3413 if (!c->expr1 && csym)
3415 if (csym->attr.abstract)
3417 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3418 csym->name, &c->loc);
3419 return false;
3422 /* Subroutines without the RECURSIVE attribution are not allowed to
3423 call themselves. */
3424 if (is_illegal_recursion (csym, gfc_current_ns))
3426 if (csym->attr.entry && csym->ns->entries)
3427 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3428 "as subroutine %qs is not RECURSIVE",
3429 csym->name, &c->loc, csym->ns->entries->sym->name);
3430 else
3431 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3432 "as it is not RECURSIVE", csym->name, &c->loc);
3434 t = false;
3438 /* Switch off assumed size checking and do this again for certain kinds
3439 of procedure, once the procedure itself is resolved. */
3440 need_full_assumed_size++;
3442 if (csym)
3443 ptype = csym->attr.proc;
3445 no_formal_args = csym && is_external_proc (csym)
3446 && gfc_sym_get_dummy_args (csym) == NULL;
3447 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3448 return false;
3450 /* Resume assumed_size checking. */
3451 need_full_assumed_size--;
3453 /* If external, check for usage. */
3454 if (csym && is_external_proc (csym))
3455 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3457 t = true;
3458 if (c->resolved_sym == NULL)
3460 c->resolved_isym = NULL;
3461 switch (procedure_kind (csym))
3463 case PTYPE_GENERIC:
3464 t = resolve_generic_s (c);
3465 break;
3467 case PTYPE_SPECIFIC:
3468 t = resolve_specific_s (c);
3469 break;
3471 case PTYPE_UNKNOWN:
3472 t = resolve_unknown_s (c);
3473 break;
3475 default:
3476 gfc_internal_error ("resolve_subroutine(): bad function type");
3480 /* Some checks of elemental subroutine actual arguments. */
3481 if (!resolve_elemental_actual (NULL, c))
3482 return false;
3484 if (!c->expr1)
3485 update_current_proc_array_outer_dependency (csym);
3486 else
3487 /* Typebound procedure: Assume the worst. */
3488 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3490 return t;
3494 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3495 op1->shape and op2->shape are non-NULL return true if their shapes
3496 match. If both op1->shape and op2->shape are non-NULL return false
3497 if their shapes do not match. If either op1->shape or op2->shape is
3498 NULL, return true. */
3500 static bool
3501 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3503 bool t;
3504 int i;
3506 t = true;
3508 if (op1->shape != NULL && op2->shape != NULL)
3510 for (i = 0; i < op1->rank; i++)
3512 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3514 gfc_error ("Shapes for operands at %L and %L are not conformable",
3515 &op1->where, &op2->where);
3516 t = false;
3517 break;
3522 return t;
3526 /* Resolve an operator expression node. This can involve replacing the
3527 operation with a user defined function call. */
3529 static bool
3530 resolve_operator (gfc_expr *e)
3532 gfc_expr *op1, *op2;
3533 char msg[200];
3534 bool dual_locus_error;
3535 bool t;
3537 /* Resolve all subnodes-- give them types. */
3539 switch (e->value.op.op)
3541 default:
3542 if (!gfc_resolve_expr (e->value.op.op2))
3543 return false;
3545 /* Fall through. */
3547 case INTRINSIC_NOT:
3548 case INTRINSIC_UPLUS:
3549 case INTRINSIC_UMINUS:
3550 case INTRINSIC_PARENTHESES:
3551 if (!gfc_resolve_expr (e->value.op.op1))
3552 return false;
3553 break;
3556 /* Typecheck the new node. */
3558 op1 = e->value.op.op1;
3559 op2 = e->value.op.op2;
3560 dual_locus_error = false;
3562 if ((op1 && op1->expr_type == EXPR_NULL)
3563 || (op2 && op2->expr_type == EXPR_NULL))
3565 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3566 goto bad_op;
3569 switch (e->value.op.op)
3571 case INTRINSIC_UPLUS:
3572 case INTRINSIC_UMINUS:
3573 if (op1->ts.type == BT_INTEGER
3574 || op1->ts.type == BT_REAL
3575 || op1->ts.type == BT_COMPLEX)
3577 e->ts = op1->ts;
3578 break;
3581 sprintf (msg, _("Operand of unary numeric operator %%<%s%%> at %%L is %s"),
3582 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3583 goto bad_op;
3585 case INTRINSIC_PLUS:
3586 case INTRINSIC_MINUS:
3587 case INTRINSIC_TIMES:
3588 case INTRINSIC_DIVIDE:
3589 case INTRINSIC_POWER:
3590 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3592 gfc_type_convert_binary (e, 1);
3593 break;
3596 sprintf (msg,
3597 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"),
3598 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3599 gfc_typename (&op2->ts));
3600 goto bad_op;
3602 case INTRINSIC_CONCAT:
3603 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3604 && op1->ts.kind == op2->ts.kind)
3606 e->ts.type = BT_CHARACTER;
3607 e->ts.kind = op1->ts.kind;
3608 break;
3611 sprintf (msg,
3612 _("Operands of string concatenation operator at %%L are %s/%s"),
3613 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3614 goto bad_op;
3616 case INTRINSIC_AND:
3617 case INTRINSIC_OR:
3618 case INTRINSIC_EQV:
3619 case INTRINSIC_NEQV:
3620 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3622 e->ts.type = BT_LOGICAL;
3623 e->ts.kind = gfc_kind_max (op1, op2);
3624 if (op1->ts.kind < e->ts.kind)
3625 gfc_convert_type (op1, &e->ts, 2);
3626 else if (op2->ts.kind < e->ts.kind)
3627 gfc_convert_type (op2, &e->ts, 2);
3628 break;
3631 sprintf (msg, _("Operands of logical operator %%<%s%%> at %%L are %s/%s"),
3632 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3633 gfc_typename (&op2->ts));
3635 goto bad_op;
3637 case INTRINSIC_NOT:
3638 if (op1->ts.type == BT_LOGICAL)
3640 e->ts.type = BT_LOGICAL;
3641 e->ts.kind = op1->ts.kind;
3642 break;
3645 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3646 gfc_typename (&op1->ts));
3647 goto bad_op;
3649 case INTRINSIC_GT:
3650 case INTRINSIC_GT_OS:
3651 case INTRINSIC_GE:
3652 case INTRINSIC_GE_OS:
3653 case INTRINSIC_LT:
3654 case INTRINSIC_LT_OS:
3655 case INTRINSIC_LE:
3656 case INTRINSIC_LE_OS:
3657 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3659 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3660 goto bad_op;
3663 /* Fall through. */
3665 case INTRINSIC_EQ:
3666 case INTRINSIC_EQ_OS:
3667 case INTRINSIC_NE:
3668 case INTRINSIC_NE_OS:
3669 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3670 && op1->ts.kind == op2->ts.kind)
3672 e->ts.type = BT_LOGICAL;
3673 e->ts.kind = gfc_default_logical_kind;
3674 break;
3677 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3679 gfc_type_convert_binary (e, 1);
3681 e->ts.type = BT_LOGICAL;
3682 e->ts.kind = gfc_default_logical_kind;
3684 if (warn_compare_reals)
3686 gfc_intrinsic_op op = e->value.op.op;
3688 /* Type conversion has made sure that the types of op1 and op2
3689 agree, so it is only necessary to check the first one. */
3690 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3691 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3692 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3694 const char *msg;
3696 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3697 msg = "Equality comparison for %s at %L";
3698 else
3699 msg = "Inequality comparison for %s at %L";
3701 gfc_warning (0, msg, gfc_typename (&op1->ts), &op1->where);
3705 break;
3708 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3709 sprintf (msg,
3710 _("Logicals at %%L must be compared with %s instead of %s"),
3711 (e->value.op.op == INTRINSIC_EQ
3712 || e->value.op.op == INTRINSIC_EQ_OS)
3713 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3714 else
3715 sprintf (msg,
3716 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"),
3717 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3718 gfc_typename (&op2->ts));
3720 goto bad_op;
3722 case INTRINSIC_USER:
3723 if (e->value.op.uop->op == NULL)
3724 sprintf (msg, _("Unknown operator %%<%s%%> at %%L"),
3725 e->value.op.uop->name);
3726 else if (op2 == NULL)
3727 sprintf (msg, _("Operand of user operator %%<%s%%> at %%L is %s"),
3728 e->value.op.uop->name, gfc_typename (&op1->ts));
3729 else
3731 sprintf (msg, _("Operands of user operator %%<%s%%> at %%L are %s/%s"),
3732 e->value.op.uop->name, gfc_typename (&op1->ts),
3733 gfc_typename (&op2->ts));
3734 e->value.op.uop->op->sym->attr.referenced = 1;
3737 goto bad_op;
3739 case INTRINSIC_PARENTHESES:
3740 e->ts = op1->ts;
3741 if (e->ts.type == BT_CHARACTER)
3742 e->ts.u.cl = op1->ts.u.cl;
3743 break;
3745 default:
3746 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3749 /* Deal with arrayness of an operand through an operator. */
3751 t = true;
3753 switch (e->value.op.op)
3755 case INTRINSIC_PLUS:
3756 case INTRINSIC_MINUS:
3757 case INTRINSIC_TIMES:
3758 case INTRINSIC_DIVIDE:
3759 case INTRINSIC_POWER:
3760 case INTRINSIC_CONCAT:
3761 case INTRINSIC_AND:
3762 case INTRINSIC_OR:
3763 case INTRINSIC_EQV:
3764 case INTRINSIC_NEQV:
3765 case INTRINSIC_EQ:
3766 case INTRINSIC_EQ_OS:
3767 case INTRINSIC_NE:
3768 case INTRINSIC_NE_OS:
3769 case INTRINSIC_GT:
3770 case INTRINSIC_GT_OS:
3771 case INTRINSIC_GE:
3772 case INTRINSIC_GE_OS:
3773 case INTRINSIC_LT:
3774 case INTRINSIC_LT_OS:
3775 case INTRINSIC_LE:
3776 case INTRINSIC_LE_OS:
3778 if (op1->rank == 0 && op2->rank == 0)
3779 e->rank = 0;
3781 if (op1->rank == 0 && op2->rank != 0)
3783 e->rank = op2->rank;
3785 if (e->shape == NULL)
3786 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3789 if (op1->rank != 0 && op2->rank == 0)
3791 e->rank = op1->rank;
3793 if (e->shape == NULL)
3794 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3797 if (op1->rank != 0 && op2->rank != 0)
3799 if (op1->rank == op2->rank)
3801 e->rank = op1->rank;
3802 if (e->shape == NULL)
3804 t = compare_shapes (op1, op2);
3805 if (!t)
3806 e->shape = NULL;
3807 else
3808 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3811 else
3813 /* Allow higher level expressions to work. */
3814 e->rank = 0;
3816 /* Try user-defined operators, and otherwise throw an error. */
3817 dual_locus_error = true;
3818 sprintf (msg,
3819 _("Inconsistent ranks for operator at %%L and %%L"));
3820 goto bad_op;
3824 break;
3826 case INTRINSIC_PARENTHESES:
3827 case INTRINSIC_NOT:
3828 case INTRINSIC_UPLUS:
3829 case INTRINSIC_UMINUS:
3830 /* Simply copy arrayness attribute */
3831 e->rank = op1->rank;
3833 if (e->shape == NULL)
3834 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3836 break;
3838 default:
3839 break;
3842 /* Attempt to simplify the expression. */
3843 if (t)
3845 t = gfc_simplify_expr (e, 0);
3846 /* Some calls do not succeed in simplification and return false
3847 even though there is no error; e.g. variable references to
3848 PARAMETER arrays. */
3849 if (!gfc_is_constant_expr (e))
3850 t = true;
3852 return t;
3854 bad_op:
3857 match m = gfc_extend_expr (e);
3858 if (m == MATCH_YES)
3859 return true;
3860 if (m == MATCH_ERROR)
3861 return false;
3864 if (dual_locus_error)
3865 gfc_error (msg, &op1->where, &op2->where);
3866 else
3867 gfc_error (msg, &e->where);
3869 return false;
3873 /************** Array resolution subroutines **************/
3875 enum compare_result
3876 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
3878 /* Compare two integer expressions. */
3880 static compare_result
3881 compare_bound (gfc_expr *a, gfc_expr *b)
3883 int i;
3885 if (a == NULL || a->expr_type != EXPR_CONSTANT
3886 || b == NULL || b->expr_type != EXPR_CONSTANT)
3887 return CMP_UNKNOWN;
3889 /* If either of the types isn't INTEGER, we must have
3890 raised an error earlier. */
3892 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3893 return CMP_UNKNOWN;
3895 i = mpz_cmp (a->value.integer, b->value.integer);
3897 if (i < 0)
3898 return CMP_LT;
3899 if (i > 0)
3900 return CMP_GT;
3901 return CMP_EQ;
3905 /* Compare an integer expression with an integer. */
3907 static compare_result
3908 compare_bound_int (gfc_expr *a, int b)
3910 int i;
3912 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3913 return CMP_UNKNOWN;
3915 if (a->ts.type != BT_INTEGER)
3916 gfc_internal_error ("compare_bound_int(): Bad expression");
3918 i = mpz_cmp_si (a->value.integer, b);
3920 if (i < 0)
3921 return CMP_LT;
3922 if (i > 0)
3923 return CMP_GT;
3924 return CMP_EQ;
3928 /* Compare an integer expression with a mpz_t. */
3930 static compare_result
3931 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3933 int i;
3935 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3936 return CMP_UNKNOWN;
3938 if (a->ts.type != BT_INTEGER)
3939 gfc_internal_error ("compare_bound_int(): Bad expression");
3941 i = mpz_cmp (a->value.integer, b);
3943 if (i < 0)
3944 return CMP_LT;
3945 if (i > 0)
3946 return CMP_GT;
3947 return CMP_EQ;
3951 /* Compute the last value of a sequence given by a triplet.
3952 Return 0 if it wasn't able to compute the last value, or if the
3953 sequence if empty, and 1 otherwise. */
3955 static int
3956 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3957 gfc_expr *stride, mpz_t last)
3959 mpz_t rem;
3961 if (start == NULL || start->expr_type != EXPR_CONSTANT
3962 || end == NULL || end->expr_type != EXPR_CONSTANT
3963 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3964 return 0;
3966 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3967 || (stride != NULL && stride->ts.type != BT_INTEGER))
3968 return 0;
3970 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
3972 if (compare_bound (start, end) == CMP_GT)
3973 return 0;
3974 mpz_set (last, end->value.integer);
3975 return 1;
3978 if (compare_bound_int (stride, 0) == CMP_GT)
3980 /* Stride is positive */
3981 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3982 return 0;
3984 else
3986 /* Stride is negative */
3987 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3988 return 0;
3991 mpz_init (rem);
3992 mpz_sub (rem, end->value.integer, start->value.integer);
3993 mpz_tdiv_r (rem, rem, stride->value.integer);
3994 mpz_sub (last, end->value.integer, rem);
3995 mpz_clear (rem);
3997 return 1;
4001 /* Compare a single dimension of an array reference to the array
4002 specification. */
4004 static bool
4005 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4007 mpz_t last_value;
4009 if (ar->dimen_type[i] == DIMEN_STAR)
4011 gcc_assert (ar->stride[i] == NULL);
4012 /* This implies [*] as [*:] and [*:3] are not possible. */
4013 if (ar->start[i] == NULL)
4015 gcc_assert (ar->end[i] == NULL);
4016 return true;
4020 /* Given start, end and stride values, calculate the minimum and
4021 maximum referenced indexes. */
4023 switch (ar->dimen_type[i])
4025 case DIMEN_VECTOR:
4026 case DIMEN_THIS_IMAGE:
4027 break;
4029 case DIMEN_STAR:
4030 case DIMEN_ELEMENT:
4031 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4033 if (i < as->rank)
4034 gfc_warning (0, "Array reference at %L is out of bounds "
4035 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4036 mpz_get_si (ar->start[i]->value.integer),
4037 mpz_get_si (as->lower[i]->value.integer), i+1);
4038 else
4039 gfc_warning (0, "Array reference at %L is out of bounds "
4040 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4041 mpz_get_si (ar->start[i]->value.integer),
4042 mpz_get_si (as->lower[i]->value.integer),
4043 i + 1 - as->rank);
4044 return true;
4046 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4048 if (i < as->rank)
4049 gfc_warning (0, "Array reference at %L is out of bounds "
4050 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4051 mpz_get_si (ar->start[i]->value.integer),
4052 mpz_get_si (as->upper[i]->value.integer), i+1);
4053 else
4054 gfc_warning (0, "Array reference at %L is out of bounds "
4055 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4056 mpz_get_si (ar->start[i]->value.integer),
4057 mpz_get_si (as->upper[i]->value.integer),
4058 i + 1 - as->rank);
4059 return true;
4062 break;
4064 case DIMEN_RANGE:
4066 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4067 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4069 compare_result comp_start_end = compare_bound (AR_START, AR_END);
4071 /* Check for zero stride, which is not allowed. */
4072 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4074 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4075 return false;
4078 /* if start == len || (stride > 0 && start < len)
4079 || (stride < 0 && start > len),
4080 then the array section contains at least one element. In this
4081 case, there is an out-of-bounds access if
4082 (start < lower || start > upper). */
4083 if (compare_bound (AR_START, AR_END) == CMP_EQ
4084 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4085 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4086 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4087 && comp_start_end == CMP_GT))
4089 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4091 gfc_warning (0, "Lower array reference at %L is out of bounds "
4092 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4093 mpz_get_si (AR_START->value.integer),
4094 mpz_get_si (as->lower[i]->value.integer), i+1);
4095 return true;
4097 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4099 gfc_warning (0, "Lower array reference at %L is out of bounds "
4100 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4101 mpz_get_si (AR_START->value.integer),
4102 mpz_get_si (as->upper[i]->value.integer), i+1);
4103 return true;
4107 /* If we can compute the highest index of the array section,
4108 then it also has to be between lower and upper. */
4109 mpz_init (last_value);
4110 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4111 last_value))
4113 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4115 gfc_warning (0, "Upper array reference at %L is out of bounds "
4116 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4117 mpz_get_si (last_value),
4118 mpz_get_si (as->lower[i]->value.integer), i+1);
4119 mpz_clear (last_value);
4120 return true;
4122 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4124 gfc_warning (0, "Upper array reference at %L is out of bounds "
4125 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4126 mpz_get_si (last_value),
4127 mpz_get_si (as->upper[i]->value.integer), i+1);
4128 mpz_clear (last_value);
4129 return true;
4132 mpz_clear (last_value);
4134 #undef AR_START
4135 #undef AR_END
4137 break;
4139 default:
4140 gfc_internal_error ("check_dimension(): Bad array reference");
4143 return true;
4147 /* Compare an array reference with an array specification. */
4149 static bool
4150 compare_spec_to_ref (gfc_array_ref *ar)
4152 gfc_array_spec *as;
4153 int i;
4155 as = ar->as;
4156 i = as->rank - 1;
4157 /* TODO: Full array sections are only allowed as actual parameters. */
4158 if (as->type == AS_ASSUMED_SIZE
4159 && (/*ar->type == AR_FULL
4160 ||*/ (ar->type == AR_SECTION
4161 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4163 gfc_error ("Rightmost upper bound of assumed size array section "
4164 "not specified at %L", &ar->where);
4165 return false;
4168 if (ar->type == AR_FULL)
4169 return true;
4171 if (as->rank != ar->dimen)
4173 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4174 &ar->where, ar->dimen, as->rank);
4175 return false;
4178 /* ar->codimen == 0 is a local array. */
4179 if (as->corank != ar->codimen && ar->codimen != 0)
4181 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4182 &ar->where, ar->codimen, as->corank);
4183 return false;
4186 for (i = 0; i < as->rank; i++)
4187 if (!check_dimension (i, ar, as))
4188 return false;
4190 /* Local access has no coarray spec. */
4191 if (ar->codimen != 0)
4192 for (i = as->rank; i < as->rank + as->corank; i++)
4194 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4195 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4197 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4198 i + 1 - as->rank, &ar->where);
4199 return false;
4201 if (!check_dimension (i, ar, as))
4202 return false;
4205 return true;
4209 /* Resolve one part of an array index. */
4211 static bool
4212 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4213 int force_index_integer_kind)
4215 gfc_typespec ts;
4217 if (index == NULL)
4218 return true;
4220 if (!gfc_resolve_expr (index))
4221 return false;
4223 if (check_scalar && index->rank != 0)
4225 gfc_error ("Array index at %L must be scalar", &index->where);
4226 return false;
4229 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4231 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4232 &index->where, gfc_basic_typename (index->ts.type));
4233 return false;
4236 if (index->ts.type == BT_REAL)
4237 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4238 &index->where))
4239 return false;
4241 if ((index->ts.kind != gfc_index_integer_kind
4242 && force_index_integer_kind)
4243 || index->ts.type != BT_INTEGER)
4245 gfc_clear_ts (&ts);
4246 ts.type = BT_INTEGER;
4247 ts.kind = gfc_index_integer_kind;
4249 gfc_convert_type_warn (index, &ts, 2, 0);
4252 return true;
4255 /* Resolve one part of an array index. */
4257 bool
4258 gfc_resolve_index (gfc_expr *index, int check_scalar)
4260 return gfc_resolve_index_1 (index, check_scalar, 1);
4263 /* Resolve a dim argument to an intrinsic function. */
4265 bool
4266 gfc_resolve_dim_arg (gfc_expr *dim)
4268 if (dim == NULL)
4269 return true;
4271 if (!gfc_resolve_expr (dim))
4272 return false;
4274 if (dim->rank != 0)
4276 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4277 return false;
4281 if (dim->ts.type != BT_INTEGER)
4283 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4284 return false;
4287 if (dim->ts.kind != gfc_index_integer_kind)
4289 gfc_typespec ts;
4291 gfc_clear_ts (&ts);
4292 ts.type = BT_INTEGER;
4293 ts.kind = gfc_index_integer_kind;
4295 gfc_convert_type_warn (dim, &ts, 2, 0);
4298 return true;
4301 /* Given an expression that contains array references, update those array
4302 references to point to the right array specifications. While this is
4303 filled in during matching, this information is difficult to save and load
4304 in a module, so we take care of it here.
4306 The idea here is that the original array reference comes from the
4307 base symbol. We traverse the list of reference structures, setting
4308 the stored reference to references. Component references can
4309 provide an additional array specification. */
4311 static void
4312 find_array_spec (gfc_expr *e)
4314 gfc_array_spec *as;
4315 gfc_component *c;
4316 gfc_ref *ref;
4318 if (e->symtree->n.sym->ts.type == BT_CLASS)
4319 as = CLASS_DATA (e->symtree->n.sym)->as;
4320 else
4321 as = e->symtree->n.sym->as;
4323 for (ref = e->ref; ref; ref = ref->next)
4324 switch (ref->type)
4326 case REF_ARRAY:
4327 if (as == NULL)
4328 gfc_internal_error ("find_array_spec(): Missing spec");
4330 ref->u.ar.as = as;
4331 as = NULL;
4332 break;
4334 case REF_COMPONENT:
4335 c = ref->u.c.component;
4336 if (c->attr.dimension)
4338 if (as != NULL)
4339 gfc_internal_error ("find_array_spec(): unused as(1)");
4340 as = c->as;
4343 break;
4345 case REF_SUBSTRING:
4346 break;
4349 if (as != NULL)
4350 gfc_internal_error ("find_array_spec(): unused as(2)");
4354 /* Resolve an array reference. */
4356 static bool
4357 resolve_array_ref (gfc_array_ref *ar)
4359 int i, check_scalar;
4360 gfc_expr *e;
4362 for (i = 0; i < ar->dimen + ar->codimen; i++)
4364 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4366 /* Do not force gfc_index_integer_kind for the start. We can
4367 do fine with any integer kind. This avoids temporary arrays
4368 created for indexing with a vector. */
4369 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4370 return false;
4371 if (!gfc_resolve_index (ar->end[i], check_scalar))
4372 return false;
4373 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4374 return false;
4376 e = ar->start[i];
4378 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4379 switch (e->rank)
4381 case 0:
4382 ar->dimen_type[i] = DIMEN_ELEMENT;
4383 break;
4385 case 1:
4386 ar->dimen_type[i] = DIMEN_VECTOR;
4387 if (e->expr_type == EXPR_VARIABLE
4388 && e->symtree->n.sym->ts.type == BT_DERIVED)
4389 ar->start[i] = gfc_get_parentheses (e);
4390 break;
4392 default:
4393 gfc_error ("Array index at %L is an array of rank %d",
4394 &ar->c_where[i], e->rank);
4395 return false;
4398 /* Fill in the upper bound, which may be lower than the
4399 specified one for something like a(2:10:5), which is
4400 identical to a(2:7:5). Only relevant for strides not equal
4401 to one. Don't try a division by zero. */
4402 if (ar->dimen_type[i] == DIMEN_RANGE
4403 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4404 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4405 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4407 mpz_t size, end;
4409 if (gfc_ref_dimen_size (ar, i, &size, &end))
4411 if (ar->end[i] == NULL)
4413 ar->end[i] =
4414 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4415 &ar->where);
4416 mpz_set (ar->end[i]->value.integer, end);
4418 else if (ar->end[i]->ts.type == BT_INTEGER
4419 && ar->end[i]->expr_type == EXPR_CONSTANT)
4421 mpz_set (ar->end[i]->value.integer, end);
4423 else
4424 gcc_unreachable ();
4426 mpz_clear (size);
4427 mpz_clear (end);
4432 if (ar->type == AR_FULL)
4434 if (ar->as->rank == 0)
4435 ar->type = AR_ELEMENT;
4437 /* Make sure array is the same as array(:,:), this way
4438 we don't need to special case all the time. */
4439 ar->dimen = ar->as->rank;
4440 for (i = 0; i < ar->dimen; i++)
4442 ar->dimen_type[i] = DIMEN_RANGE;
4444 gcc_assert (ar->start[i] == NULL);
4445 gcc_assert (ar->end[i] == NULL);
4446 gcc_assert (ar->stride[i] == NULL);
4450 /* If the reference type is unknown, figure out what kind it is. */
4452 if (ar->type == AR_UNKNOWN)
4454 ar->type = AR_ELEMENT;
4455 for (i = 0; i < ar->dimen; i++)
4456 if (ar->dimen_type[i] == DIMEN_RANGE
4457 || ar->dimen_type[i] == DIMEN_VECTOR)
4459 ar->type = AR_SECTION;
4460 break;
4464 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4465 return false;
4467 if (ar->as->corank && ar->codimen == 0)
4469 int n;
4470 ar->codimen = ar->as->corank;
4471 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4472 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4475 return true;
4479 static bool
4480 resolve_substring (gfc_ref *ref)
4482 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4484 if (ref->u.ss.start != NULL)
4486 if (!gfc_resolve_expr (ref->u.ss.start))
4487 return false;
4489 if (ref->u.ss.start->ts.type != BT_INTEGER)
4491 gfc_error ("Substring start index at %L must be of type INTEGER",
4492 &ref->u.ss.start->where);
4493 return false;
4496 if (ref->u.ss.start->rank != 0)
4498 gfc_error ("Substring start index at %L must be scalar",
4499 &ref->u.ss.start->where);
4500 return false;
4503 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4504 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4505 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4507 gfc_error ("Substring start index at %L is less than one",
4508 &ref->u.ss.start->where);
4509 return false;
4513 if (ref->u.ss.end != NULL)
4515 if (!gfc_resolve_expr (ref->u.ss.end))
4516 return false;
4518 if (ref->u.ss.end->ts.type != BT_INTEGER)
4520 gfc_error ("Substring end index at %L must be of type INTEGER",
4521 &ref->u.ss.end->where);
4522 return false;
4525 if (ref->u.ss.end->rank != 0)
4527 gfc_error ("Substring end index at %L must be scalar",
4528 &ref->u.ss.end->where);
4529 return false;
4532 if (ref->u.ss.length != NULL
4533 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4534 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4535 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4537 gfc_error ("Substring end index at %L exceeds the string length",
4538 &ref->u.ss.start->where);
4539 return false;
4542 if (compare_bound_mpz_t (ref->u.ss.end,
4543 gfc_integer_kinds[k].huge) == CMP_GT
4544 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4545 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4547 gfc_error ("Substring end index at %L is too large",
4548 &ref->u.ss.end->where);
4549 return false;
4553 return true;
4557 /* This function supplies missing substring charlens. */
4559 void
4560 gfc_resolve_substring_charlen (gfc_expr *e)
4562 gfc_ref *char_ref;
4563 gfc_expr *start, *end;
4564 gfc_typespec *ts = NULL;
4566 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4568 if (char_ref->type == REF_SUBSTRING)
4569 break;
4570 if (char_ref->type == REF_COMPONENT)
4571 ts = &char_ref->u.c.component->ts;
4574 if (!char_ref)
4575 return;
4577 gcc_assert (char_ref->next == NULL);
4579 if (e->ts.u.cl)
4581 if (e->ts.u.cl->length)
4582 gfc_free_expr (e->ts.u.cl->length);
4583 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
4584 return;
4587 e->ts.type = BT_CHARACTER;
4588 e->ts.kind = gfc_default_character_kind;
4590 if (!e->ts.u.cl)
4591 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4593 if (char_ref->u.ss.start)
4594 start = gfc_copy_expr (char_ref->u.ss.start);
4595 else
4596 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4598 if (char_ref->u.ss.end)
4599 end = gfc_copy_expr (char_ref->u.ss.end);
4600 else if (e->expr_type == EXPR_VARIABLE)
4602 if (!ts)
4603 ts = &e->symtree->n.sym->ts;
4604 end = gfc_copy_expr (ts->u.cl->length);
4606 else
4607 end = NULL;
4609 if (!start || !end)
4611 gfc_free_expr (start);
4612 gfc_free_expr (end);
4613 return;
4616 /* Length = (end - start + 1). */
4617 e->ts.u.cl->length = gfc_subtract (end, start);
4618 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4619 gfc_get_int_expr (gfc_default_integer_kind,
4620 NULL, 1));
4622 /* F2008, 6.4.1: Both the starting point and the ending point shall
4623 be within the range 1, 2, ..., n unless the starting point exceeds
4624 the ending point, in which case the substring has length zero. */
4626 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
4627 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
4629 e->ts.u.cl->length->ts.type = BT_INTEGER;
4630 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4632 /* Make sure that the length is simplified. */
4633 gfc_simplify_expr (e->ts.u.cl->length, 1);
4634 gfc_resolve_expr (e->ts.u.cl->length);
4638 /* Resolve subtype references. */
4640 static bool
4641 resolve_ref (gfc_expr *expr)
4643 int current_part_dimension, n_components, seen_part_dimension;
4644 gfc_ref *ref;
4646 for (ref = expr->ref; ref; ref = ref->next)
4647 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4649 find_array_spec (expr);
4650 break;
4653 for (ref = expr->ref; ref; ref = ref->next)
4654 switch (ref->type)
4656 case REF_ARRAY:
4657 if (!resolve_array_ref (&ref->u.ar))
4658 return false;
4659 break;
4661 case REF_COMPONENT:
4662 break;
4664 case REF_SUBSTRING:
4665 if (!resolve_substring (ref))
4666 return false;
4667 break;
4670 /* Check constraints on part references. */
4672 current_part_dimension = 0;
4673 seen_part_dimension = 0;
4674 n_components = 0;
4676 for (ref = expr->ref; ref; ref = ref->next)
4678 switch (ref->type)
4680 case REF_ARRAY:
4681 switch (ref->u.ar.type)
4683 case AR_FULL:
4684 /* Coarray scalar. */
4685 if (ref->u.ar.as->rank == 0)
4687 current_part_dimension = 0;
4688 break;
4690 /* Fall through. */
4691 case AR_SECTION:
4692 current_part_dimension = 1;
4693 break;
4695 case AR_ELEMENT:
4696 current_part_dimension = 0;
4697 break;
4699 case AR_UNKNOWN:
4700 gfc_internal_error ("resolve_ref(): Bad array reference");
4703 break;
4705 case REF_COMPONENT:
4706 if (current_part_dimension || seen_part_dimension)
4708 /* F03:C614. */
4709 if (ref->u.c.component->attr.pointer
4710 || ref->u.c.component->attr.proc_pointer
4711 || (ref->u.c.component->ts.type == BT_CLASS
4712 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4714 gfc_error ("Component to the right of a part reference "
4715 "with nonzero rank must not have the POINTER "
4716 "attribute at %L", &expr->where);
4717 return false;
4719 else if (ref->u.c.component->attr.allocatable
4720 || (ref->u.c.component->ts.type == BT_CLASS
4721 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4724 gfc_error ("Component to the right of a part reference "
4725 "with nonzero rank must not have the ALLOCATABLE "
4726 "attribute at %L", &expr->where);
4727 return false;
4731 n_components++;
4732 break;
4734 case REF_SUBSTRING:
4735 break;
4738 if (((ref->type == REF_COMPONENT && n_components > 1)
4739 || ref->next == NULL)
4740 && current_part_dimension
4741 && seen_part_dimension)
4743 gfc_error ("Two or more part references with nonzero rank must "
4744 "not be specified at %L", &expr->where);
4745 return false;
4748 if (ref->type == REF_COMPONENT)
4750 if (current_part_dimension)
4751 seen_part_dimension = 1;
4753 /* reset to make sure */
4754 current_part_dimension = 0;
4758 return true;
4762 /* Given an expression, determine its shape. This is easier than it sounds.
4763 Leaves the shape array NULL if it is not possible to determine the shape. */
4765 static void
4766 expression_shape (gfc_expr *e)
4768 mpz_t array[GFC_MAX_DIMENSIONS];
4769 int i;
4771 if (e->rank <= 0 || e->shape != NULL)
4772 return;
4774 for (i = 0; i < e->rank; i++)
4775 if (!gfc_array_dimen_size (e, i, &array[i]))
4776 goto fail;
4778 e->shape = gfc_get_shape (e->rank);
4780 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4782 return;
4784 fail:
4785 for (i--; i >= 0; i--)
4786 mpz_clear (array[i]);
4790 /* Given a variable expression node, compute the rank of the expression by
4791 examining the base symbol and any reference structures it may have. */
4793 void
4794 expression_rank (gfc_expr *e)
4796 gfc_ref *ref;
4797 int i, rank;
4799 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4800 could lead to serious confusion... */
4801 gcc_assert (e->expr_type != EXPR_COMPCALL);
4803 if (e->ref == NULL)
4805 if (e->expr_type == EXPR_ARRAY)
4806 goto done;
4807 /* Constructors can have a rank different from one via RESHAPE(). */
4809 if (e->symtree == NULL)
4811 e->rank = 0;
4812 goto done;
4815 e->rank = (e->symtree->n.sym->as == NULL)
4816 ? 0 : e->symtree->n.sym->as->rank;
4817 goto done;
4820 rank = 0;
4822 for (ref = e->ref; ref; ref = ref->next)
4824 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4825 && ref->u.c.component->attr.function && !ref->next)
4826 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4828 if (ref->type != REF_ARRAY)
4829 continue;
4831 if (ref->u.ar.type == AR_FULL)
4833 rank = ref->u.ar.as->rank;
4834 break;
4837 if (ref->u.ar.type == AR_SECTION)
4839 /* Figure out the rank of the section. */
4840 if (rank != 0)
4841 gfc_internal_error ("expression_rank(): Two array specs");
4843 for (i = 0; i < ref->u.ar.dimen; i++)
4844 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4845 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4846 rank++;
4848 break;
4852 e->rank = rank;
4854 done:
4855 expression_shape (e);
4859 static void
4860 add_caf_get_intrinsic (gfc_expr *e)
4862 gfc_expr *wrapper, *tmp_expr;
4863 gfc_ref *ref;
4864 int n;
4866 for (ref = e->ref; ref; ref = ref->next)
4867 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4868 break;
4869 if (ref == NULL)
4870 return;
4872 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
4873 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
4874 return;
4876 tmp_expr = XCNEW (gfc_expr);
4877 *tmp_expr = *e;
4878 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
4879 "caf_get", tmp_expr->where, 1, tmp_expr);
4880 wrapper->ts = e->ts;
4881 wrapper->rank = e->rank;
4882 if (e->rank)
4883 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
4884 *e = *wrapper;
4885 free (wrapper);
4889 static void
4890 remove_caf_get_intrinsic (gfc_expr *e)
4892 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
4893 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
4894 gfc_expr *e2 = e->value.function.actual->expr;
4895 e->value.function.actual->expr = NULL;
4896 gfc_free_actual_arglist (e->value.function.actual);
4897 gfc_free_shape (&e->shape, e->rank);
4898 *e = *e2;
4899 free (e2);
4903 /* Resolve a variable expression. */
4905 static bool
4906 resolve_variable (gfc_expr *e)
4908 gfc_symbol *sym;
4909 bool t;
4911 t = true;
4913 if (e->symtree == NULL)
4914 return false;
4915 sym = e->symtree->n.sym;
4917 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4918 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4919 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4921 if (!actual_arg || inquiry_argument)
4923 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4924 "be used as actual argument", sym->name, &e->where);
4925 return false;
4928 /* TS 29113, 407b. */
4929 else if (e->ts.type == BT_ASSUMED)
4931 if (!actual_arg)
4933 gfc_error ("Assumed-type variable %s at %L may only be used "
4934 "as actual argument", sym->name, &e->where);
4935 return false;
4937 else if (inquiry_argument && !first_actual_arg)
4939 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4940 for all inquiry functions in resolve_function; the reason is
4941 that the function-name resolution happens too late in that
4942 function. */
4943 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4944 "an inquiry function shall be the first argument",
4945 sym->name, &e->where);
4946 return false;
4949 /* TS 29113, C535b. */
4950 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4951 && CLASS_DATA (sym)->as
4952 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4953 || (sym->ts.type != BT_CLASS && sym->as
4954 && sym->as->type == AS_ASSUMED_RANK))
4956 if (!actual_arg)
4958 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4959 "actual argument", sym->name, &e->where);
4960 return false;
4962 else if (inquiry_argument && !first_actual_arg)
4964 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4965 for all inquiry functions in resolve_function; the reason is
4966 that the function-name resolution happens too late in that
4967 function. */
4968 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4969 "to an inquiry function shall be the first argument",
4970 sym->name, &e->where);
4971 return false;
4975 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
4976 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4977 && e->ref->next == NULL))
4979 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4980 "a subobject reference", sym->name, &e->ref->u.ar.where);
4981 return false;
4983 /* TS 29113, 407b. */
4984 else if (e->ts.type == BT_ASSUMED && e->ref
4985 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4986 && e->ref->next == NULL))
4988 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4989 "reference", sym->name, &e->ref->u.ar.where);
4990 return false;
4993 /* TS 29113, C535b. */
4994 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4995 && CLASS_DATA (sym)->as
4996 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4997 || (sym->ts.type != BT_CLASS && sym->as
4998 && sym->as->type == AS_ASSUMED_RANK))
4999 && e->ref
5000 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5001 && e->ref->next == NULL))
5003 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
5004 "reference", sym->name, &e->ref->u.ar.where);
5005 return false;
5008 /* For variables that are used in an associate (target => object) where
5009 the object's basetype is array valued while the target is scalar,
5010 the ts' type of the component refs is still array valued, which
5011 can't be translated that way. */
5012 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
5013 && sym->assoc->target->ts.type == BT_CLASS
5014 && CLASS_DATA (sym->assoc->target)->as)
5016 gfc_ref *ref = e->ref;
5017 while (ref)
5019 switch (ref->type)
5021 case REF_COMPONENT:
5022 ref->u.c.sym = sym->ts.u.derived;
5023 /* Stop the loop. */
5024 ref = NULL;
5025 break;
5026 default:
5027 ref = ref->next;
5028 break;
5033 /* If this is an associate-name, it may be parsed with an array reference
5034 in error even though the target is scalar. Fail directly in this case.
5035 TODO Understand why class scalar expressions must be excluded. */
5036 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5038 if (sym->ts.type == BT_CLASS)
5039 gfc_fix_class_refs (e);
5040 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5041 return false;
5044 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5045 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5047 /* On the other hand, the parser may not have known this is an array;
5048 in this case, we have to add a FULL reference. */
5049 if (sym->assoc && sym->attr.dimension && !e->ref)
5051 e->ref = gfc_get_ref ();
5052 e->ref->type = REF_ARRAY;
5053 e->ref->u.ar.type = AR_FULL;
5054 e->ref->u.ar.dimen = 0;
5057 /* Like above, but for class types, where the checking whether an array
5058 ref is present is more complicated. Furthermore make sure not to add
5059 the full array ref to _vptr or _len refs. */
5060 if (sym->assoc && sym->ts.type == BT_CLASS
5061 && CLASS_DATA (sym)->attr.dimension
5062 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
5064 gfc_ref *ref, *newref;
5066 newref = gfc_get_ref ();
5067 newref->type = REF_ARRAY;
5068 newref->u.ar.type = AR_FULL;
5069 newref->u.ar.dimen = 0;
5070 /* Because this is an associate var and the first ref either is a ref to
5071 the _data component or not, no traversal of the ref chain is
5072 needed. The array ref needs to be inserted after the _data ref,
5073 or when that is not present, which may happend for polymorphic
5074 types, then at the first position. */
5075 ref = e->ref;
5076 if (!ref)
5077 e->ref = newref;
5078 else if (ref->type == REF_COMPONENT
5079 && strcmp ("_data", ref->u.c.component->name) == 0)
5081 if (!ref->next || ref->next->type != REF_ARRAY)
5083 newref->next = ref->next;
5084 ref->next = newref;
5086 else
5087 /* Array ref present already. */
5088 gfc_free_ref_list (newref);
5090 else if (ref->type == REF_ARRAY)
5091 /* Array ref present already. */
5092 gfc_free_ref_list (newref);
5093 else
5095 newref->next = ref;
5096 e->ref = newref;
5100 if (e->ref && !resolve_ref (e))
5101 return false;
5103 if (sym->attr.flavor == FL_PROCEDURE
5104 && (!sym->attr.function
5105 || (sym->attr.function && sym->result
5106 && sym->result->attr.proc_pointer
5107 && !sym->result->attr.function)))
5109 e->ts.type = BT_PROCEDURE;
5110 goto resolve_procedure;
5113 if (sym->ts.type != BT_UNKNOWN)
5114 gfc_variable_attr (e, &e->ts);
5115 else
5117 /* Must be a simple variable reference. */
5118 if (!gfc_set_default_type (sym, 1, sym->ns))
5119 return false;
5120 e->ts = sym->ts;
5123 if (check_assumed_size_reference (sym, e))
5124 return false;
5126 /* Deal with forward references to entries during gfc_resolve_code, to
5127 satisfy, at least partially, 12.5.2.5. */
5128 if (gfc_current_ns->entries
5129 && current_entry_id == sym->entry_id
5130 && cs_base
5131 && cs_base->current
5132 && cs_base->current->op != EXEC_ENTRY)
5134 gfc_entry_list *entry;
5135 gfc_formal_arglist *formal;
5136 int n;
5137 bool seen, saved_specification_expr;
5139 /* If the symbol is a dummy... */
5140 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5142 entry = gfc_current_ns->entries;
5143 seen = false;
5145 /* ...test if the symbol is a parameter of previous entries. */
5146 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5147 for (formal = entry->sym->formal; formal; formal = formal->next)
5149 if (formal->sym && sym->name == formal->sym->name)
5151 seen = true;
5152 break;
5156 /* If it has not been seen as a dummy, this is an error. */
5157 if (!seen)
5159 if (specification_expr)
5160 gfc_error ("Variable %qs, used in a specification expression"
5161 ", is referenced at %L before the ENTRY statement "
5162 "in which it is a parameter",
5163 sym->name, &cs_base->current->loc);
5164 else
5165 gfc_error ("Variable %qs is used at %L before the ENTRY "
5166 "statement in which it is a parameter",
5167 sym->name, &cs_base->current->loc);
5168 t = false;
5172 /* Now do the same check on the specification expressions. */
5173 saved_specification_expr = specification_expr;
5174 specification_expr = true;
5175 if (sym->ts.type == BT_CHARACTER
5176 && !gfc_resolve_expr (sym->ts.u.cl->length))
5177 t = false;
5179 if (sym->as)
5180 for (n = 0; n < sym->as->rank; n++)
5182 if (!gfc_resolve_expr (sym->as->lower[n]))
5183 t = false;
5184 if (!gfc_resolve_expr (sym->as->upper[n]))
5185 t = false;
5187 specification_expr = saved_specification_expr;
5189 if (t)
5190 /* Update the symbol's entry level. */
5191 sym->entry_id = current_entry_id + 1;
5194 /* If a symbol has been host_associated mark it. This is used latter,
5195 to identify if aliasing is possible via host association. */
5196 if (sym->attr.flavor == FL_VARIABLE
5197 && gfc_current_ns->parent
5198 && (gfc_current_ns->parent == sym->ns
5199 || (gfc_current_ns->parent->parent
5200 && gfc_current_ns->parent->parent == sym->ns)))
5201 sym->attr.host_assoc = 1;
5203 if (gfc_current_ns->proc_name
5204 && sym->attr.dimension
5205 && (sym->ns != gfc_current_ns
5206 || sym->attr.use_assoc
5207 || sym->attr.in_common))
5208 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
5210 resolve_procedure:
5211 if (t && !resolve_procedure_expression (e))
5212 t = false;
5214 /* F2008, C617 and C1229. */
5215 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5216 && gfc_is_coindexed (e))
5218 gfc_ref *ref, *ref2 = NULL;
5220 for (ref = e->ref; ref; ref = ref->next)
5222 if (ref->type == REF_COMPONENT)
5223 ref2 = ref;
5224 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5225 break;
5228 for ( ; ref; ref = ref->next)
5229 if (ref->type == REF_COMPONENT)
5230 break;
5232 /* Expression itself is not coindexed object. */
5233 if (ref && e->ts.type == BT_CLASS)
5235 gfc_error ("Polymorphic subobject of coindexed object at %L",
5236 &e->where);
5237 t = false;
5240 /* Expression itself is coindexed object. */
5241 if (ref == NULL)
5243 gfc_component *c;
5244 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5245 for ( ; c; c = c->next)
5246 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5248 gfc_error ("Coindexed object with polymorphic allocatable "
5249 "subcomponent at %L", &e->where);
5250 t = false;
5251 break;
5256 if (t)
5257 expression_rank (e);
5259 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
5260 add_caf_get_intrinsic (e);
5262 return t;
5266 /* Checks to see that the correct symbol has been host associated.
5267 The only situation where this arises is that in which a twice
5268 contained function is parsed after the host association is made.
5269 Therefore, on detecting this, change the symbol in the expression
5270 and convert the array reference into an actual arglist if the old
5271 symbol is a variable. */
5272 static bool
5273 check_host_association (gfc_expr *e)
5275 gfc_symbol *sym, *old_sym;
5276 gfc_symtree *st;
5277 int n;
5278 gfc_ref *ref;
5279 gfc_actual_arglist *arg, *tail = NULL;
5280 bool retval = e->expr_type == EXPR_FUNCTION;
5282 /* If the expression is the result of substitution in
5283 interface.c(gfc_extend_expr) because there is no way in
5284 which the host association can be wrong. */
5285 if (e->symtree == NULL
5286 || e->symtree->n.sym == NULL
5287 || e->user_operator)
5288 return retval;
5290 old_sym = e->symtree->n.sym;
5292 if (gfc_current_ns->parent
5293 && old_sym->ns != gfc_current_ns)
5295 /* Use the 'USE' name so that renamed module symbols are
5296 correctly handled. */
5297 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5299 if (sym && old_sym != sym
5300 && sym->ts.type == old_sym->ts.type
5301 && sym->attr.flavor == FL_PROCEDURE
5302 && sym->attr.contained)
5304 /* Clear the shape, since it might not be valid. */
5305 gfc_free_shape (&e->shape, e->rank);
5307 /* Give the expression the right symtree! */
5308 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5309 gcc_assert (st != NULL);
5311 if (old_sym->attr.flavor == FL_PROCEDURE
5312 || e->expr_type == EXPR_FUNCTION)
5314 /* Original was function so point to the new symbol, since
5315 the actual argument list is already attached to the
5316 expression. */
5317 e->value.function.esym = NULL;
5318 e->symtree = st;
5320 else
5322 /* Original was variable so convert array references into
5323 an actual arglist. This does not need any checking now
5324 since resolve_function will take care of it. */
5325 e->value.function.actual = NULL;
5326 e->expr_type = EXPR_FUNCTION;
5327 e->symtree = st;
5329 /* Ambiguity will not arise if the array reference is not
5330 the last reference. */
5331 for (ref = e->ref; ref; ref = ref->next)
5332 if (ref->type == REF_ARRAY && ref->next == NULL)
5333 break;
5335 gcc_assert (ref->type == REF_ARRAY);
5337 /* Grab the start expressions from the array ref and
5338 copy them into actual arguments. */
5339 for (n = 0; n < ref->u.ar.dimen; n++)
5341 arg = gfc_get_actual_arglist ();
5342 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5343 if (e->value.function.actual == NULL)
5344 tail = e->value.function.actual = arg;
5345 else
5347 tail->next = arg;
5348 tail = arg;
5352 /* Dump the reference list and set the rank. */
5353 gfc_free_ref_list (e->ref);
5354 e->ref = NULL;
5355 e->rank = sym->as ? sym->as->rank : 0;
5358 gfc_resolve_expr (e);
5359 sym->refs++;
5362 /* This might have changed! */
5363 return e->expr_type == EXPR_FUNCTION;
5367 static void
5368 gfc_resolve_character_operator (gfc_expr *e)
5370 gfc_expr *op1 = e->value.op.op1;
5371 gfc_expr *op2 = e->value.op.op2;
5372 gfc_expr *e1 = NULL;
5373 gfc_expr *e2 = NULL;
5375 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5377 if (op1->ts.u.cl && op1->ts.u.cl->length)
5378 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5379 else if (op1->expr_type == EXPR_CONSTANT)
5380 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5381 op1->value.character.length);
5383 if (op2->ts.u.cl && op2->ts.u.cl->length)
5384 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5385 else if (op2->expr_type == EXPR_CONSTANT)
5386 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5387 op2->value.character.length);
5389 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5391 if (!e1 || !e2)
5393 gfc_free_expr (e1);
5394 gfc_free_expr (e2);
5396 return;
5399 e->ts.u.cl->length = gfc_add (e1, e2);
5400 e->ts.u.cl->length->ts.type = BT_INTEGER;
5401 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5402 gfc_simplify_expr (e->ts.u.cl->length, 0);
5403 gfc_resolve_expr (e->ts.u.cl->length);
5405 return;
5409 /* Ensure that an character expression has a charlen and, if possible, a
5410 length expression. */
5412 static void
5413 fixup_charlen (gfc_expr *e)
5415 /* The cases fall through so that changes in expression type and the need
5416 for multiple fixes are picked up. In all circumstances, a charlen should
5417 be available for the middle end to hang a backend_decl on. */
5418 switch (e->expr_type)
5420 case EXPR_OP:
5421 gfc_resolve_character_operator (e);
5422 /* FALLTHRU */
5424 case EXPR_ARRAY:
5425 if (e->expr_type == EXPR_ARRAY)
5426 gfc_resolve_character_array_constructor (e);
5427 /* FALLTHRU */
5429 case EXPR_SUBSTRING:
5430 if (!e->ts.u.cl && e->ref)
5431 gfc_resolve_substring_charlen (e);
5432 /* FALLTHRU */
5434 default:
5435 if (!e->ts.u.cl)
5436 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5438 break;
5443 /* Update an actual argument to include the passed-object for type-bound
5444 procedures at the right position. */
5446 static gfc_actual_arglist*
5447 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5448 const char *name)
5450 gcc_assert (argpos > 0);
5452 if (argpos == 1)
5454 gfc_actual_arglist* result;
5456 result = gfc_get_actual_arglist ();
5457 result->expr = po;
5458 result->next = lst;
5459 if (name)
5460 result->name = name;
5462 return result;
5465 if (lst)
5466 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5467 else
5468 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5469 return lst;
5473 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5475 static gfc_expr*
5476 extract_compcall_passed_object (gfc_expr* e)
5478 gfc_expr* po;
5480 gcc_assert (e->expr_type == EXPR_COMPCALL);
5482 if (e->value.compcall.base_object)
5483 po = gfc_copy_expr (e->value.compcall.base_object);
5484 else
5486 po = gfc_get_expr ();
5487 po->expr_type = EXPR_VARIABLE;
5488 po->symtree = e->symtree;
5489 po->ref = gfc_copy_ref (e->ref);
5490 po->where = e->where;
5493 if (!gfc_resolve_expr (po))
5494 return NULL;
5496 return po;
5500 /* Update the arglist of an EXPR_COMPCALL expression to include the
5501 passed-object. */
5503 static bool
5504 update_compcall_arglist (gfc_expr* e)
5506 gfc_expr* po;
5507 gfc_typebound_proc* tbp;
5509 tbp = e->value.compcall.tbp;
5511 if (tbp->error)
5512 return false;
5514 po = extract_compcall_passed_object (e);
5515 if (!po)
5516 return false;
5518 if (tbp->nopass || e->value.compcall.ignore_pass)
5520 gfc_free_expr (po);
5521 return true;
5524 gcc_assert (tbp->pass_arg_num > 0);
5525 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5526 tbp->pass_arg_num,
5527 tbp->pass_arg);
5529 return true;
5533 /* Extract the passed object from a PPC call (a copy of it). */
5535 static gfc_expr*
5536 extract_ppc_passed_object (gfc_expr *e)
5538 gfc_expr *po;
5539 gfc_ref **ref;
5541 po = gfc_get_expr ();
5542 po->expr_type = EXPR_VARIABLE;
5543 po->symtree = e->symtree;
5544 po->ref = gfc_copy_ref (e->ref);
5545 po->where = e->where;
5547 /* Remove PPC reference. */
5548 ref = &po->ref;
5549 while ((*ref)->next)
5550 ref = &(*ref)->next;
5551 gfc_free_ref_list (*ref);
5552 *ref = NULL;
5554 if (!gfc_resolve_expr (po))
5555 return NULL;
5557 return po;
5561 /* Update the actual arglist of a procedure pointer component to include the
5562 passed-object. */
5564 static bool
5565 update_ppc_arglist (gfc_expr* e)
5567 gfc_expr* po;
5568 gfc_component *ppc;
5569 gfc_typebound_proc* tb;
5571 ppc = gfc_get_proc_ptr_comp (e);
5572 if (!ppc)
5573 return false;
5575 tb = ppc->tb;
5577 if (tb->error)
5578 return false;
5579 else if (tb->nopass)
5580 return true;
5582 po = extract_ppc_passed_object (e);
5583 if (!po)
5584 return false;
5586 /* F08:R739. */
5587 if (po->rank != 0)
5589 gfc_error ("Passed-object at %L must be scalar", &e->where);
5590 return false;
5593 /* F08:C611. */
5594 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5596 gfc_error ("Base object for procedure-pointer component call at %L is of"
5597 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
5598 return false;
5601 gcc_assert (tb->pass_arg_num > 0);
5602 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5603 tb->pass_arg_num,
5604 tb->pass_arg);
5606 return true;
5610 /* Check that the object a TBP is called on is valid, i.e. it must not be
5611 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5613 static bool
5614 check_typebound_baseobject (gfc_expr* e)
5616 gfc_expr* base;
5617 bool return_value = false;
5619 base = extract_compcall_passed_object (e);
5620 if (!base)
5621 return false;
5623 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5625 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
5626 return false;
5628 /* F08:C611. */
5629 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5631 gfc_error ("Base object for type-bound procedure call at %L is of"
5632 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
5633 goto cleanup;
5636 /* F08:C1230. If the procedure called is NOPASS,
5637 the base object must be scalar. */
5638 if (e->value.compcall.tbp->nopass && base->rank != 0)
5640 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5641 " be scalar", &e->where);
5642 goto cleanup;
5645 return_value = true;
5647 cleanup:
5648 gfc_free_expr (base);
5649 return return_value;
5653 /* Resolve a call to a type-bound procedure, either function or subroutine,
5654 statically from the data in an EXPR_COMPCALL expression. The adapted
5655 arglist and the target-procedure symtree are returned. */
5657 static bool
5658 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5659 gfc_actual_arglist** actual)
5661 gcc_assert (e->expr_type == EXPR_COMPCALL);
5662 gcc_assert (!e->value.compcall.tbp->is_generic);
5664 /* Update the actual arglist for PASS. */
5665 if (!update_compcall_arglist (e))
5666 return false;
5668 *actual = e->value.compcall.actual;
5669 *target = e->value.compcall.tbp->u.specific;
5671 gfc_free_ref_list (e->ref);
5672 e->ref = NULL;
5673 e->value.compcall.actual = NULL;
5675 /* If we find a deferred typebound procedure, check for derived types
5676 that an overriding typebound procedure has not been missed. */
5677 if (e->value.compcall.name
5678 && !e->value.compcall.tbp->non_overridable
5679 && e->value.compcall.base_object
5680 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5682 gfc_symtree *st;
5683 gfc_symbol *derived;
5685 /* Use the derived type of the base_object. */
5686 derived = e->value.compcall.base_object->ts.u.derived;
5687 st = NULL;
5689 /* If necessary, go through the inheritance chain. */
5690 while (!st && derived)
5692 /* Look for the typebound procedure 'name'. */
5693 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5694 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5695 e->value.compcall.name);
5696 if (!st)
5697 derived = gfc_get_derived_super_type (derived);
5700 /* Now find the specific name in the derived type namespace. */
5701 if (st && st->n.tb && st->n.tb->u.specific)
5702 gfc_find_sym_tree (st->n.tb->u.specific->name,
5703 derived->ns, 1, &st);
5704 if (st)
5705 *target = st;
5707 return true;
5711 /* Get the ultimate declared type from an expression. In addition,
5712 return the last class/derived type reference and the copy of the
5713 reference list. If check_types is set true, derived types are
5714 identified as well as class references. */
5715 static gfc_symbol*
5716 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5717 gfc_expr *e, bool check_types)
5719 gfc_symbol *declared;
5720 gfc_ref *ref;
5722 declared = NULL;
5723 if (class_ref)
5724 *class_ref = NULL;
5725 if (new_ref)
5726 *new_ref = gfc_copy_ref (e->ref);
5728 for (ref = e->ref; ref; ref = ref->next)
5730 if (ref->type != REF_COMPONENT)
5731 continue;
5733 if ((ref->u.c.component->ts.type == BT_CLASS
5734 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
5735 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5737 declared = ref->u.c.component->ts.u.derived;
5738 if (class_ref)
5739 *class_ref = ref;
5743 if (declared == NULL)
5744 declared = e->symtree->n.sym->ts.u.derived;
5746 return declared;
5750 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5751 which of the specific bindings (if any) matches the arglist and transform
5752 the expression into a call of that binding. */
5754 static bool
5755 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5757 gfc_typebound_proc* genproc;
5758 const char* genname;
5759 gfc_symtree *st;
5760 gfc_symbol *derived;
5762 gcc_assert (e->expr_type == EXPR_COMPCALL);
5763 genname = e->value.compcall.name;
5764 genproc = e->value.compcall.tbp;
5766 if (!genproc->is_generic)
5767 return true;
5769 /* Try the bindings on this type and in the inheritance hierarchy. */
5770 for (; genproc; genproc = genproc->overridden)
5772 gfc_tbp_generic* g;
5774 gcc_assert (genproc->is_generic);
5775 for (g = genproc->u.generic; g; g = g->next)
5777 gfc_symbol* target;
5778 gfc_actual_arglist* args;
5779 bool matches;
5781 gcc_assert (g->specific);
5783 if (g->specific->error)
5784 continue;
5786 target = g->specific->u.specific->n.sym;
5788 /* Get the right arglist by handling PASS/NOPASS. */
5789 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5790 if (!g->specific->nopass)
5792 gfc_expr* po;
5793 po = extract_compcall_passed_object (e);
5794 if (!po)
5796 gfc_free_actual_arglist (args);
5797 return false;
5800 gcc_assert (g->specific->pass_arg_num > 0);
5801 gcc_assert (!g->specific->error);
5802 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5803 g->specific->pass_arg);
5805 resolve_actual_arglist (args, target->attr.proc,
5806 is_external_proc (target)
5807 && gfc_sym_get_dummy_args (target) == NULL);
5809 /* Check if this arglist matches the formal. */
5810 matches = gfc_arglist_matches_symbol (&args, target);
5812 /* Clean up and break out of the loop if we've found it. */
5813 gfc_free_actual_arglist (args);
5814 if (matches)
5816 e->value.compcall.tbp = g->specific;
5817 genname = g->specific_st->name;
5818 /* Pass along the name for CLASS methods, where the vtab
5819 procedure pointer component has to be referenced. */
5820 if (name)
5821 *name = genname;
5822 goto success;
5827 /* Nothing matching found! */
5828 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5829 " %qs at %L", genname, &e->where);
5830 return false;
5832 success:
5833 /* Make sure that we have the right specific instance for the name. */
5834 derived = get_declared_from_expr (NULL, NULL, e, true);
5836 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5837 if (st)
5838 e->value.compcall.tbp = st->n.tb;
5840 return true;
5844 /* Resolve a call to a type-bound subroutine. */
5846 static bool
5847 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
5849 gfc_actual_arglist* newactual;
5850 gfc_symtree* target;
5852 /* Check that's really a SUBROUTINE. */
5853 if (!c->expr1->value.compcall.tbp->subroutine)
5855 gfc_error ("%qs at %L should be a SUBROUTINE",
5856 c->expr1->value.compcall.name, &c->loc);
5857 return false;
5860 if (!check_typebound_baseobject (c->expr1))
5861 return false;
5863 /* Pass along the name for CLASS methods, where the vtab
5864 procedure pointer component has to be referenced. */
5865 if (name)
5866 *name = c->expr1->value.compcall.name;
5868 if (!resolve_typebound_generic_call (c->expr1, name))
5869 return false;
5871 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
5872 if (overridable)
5873 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
5875 /* Transform into an ordinary EXEC_CALL for now. */
5877 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5878 return false;
5880 c->ext.actual = newactual;
5881 c->symtree = target;
5882 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5884 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5886 gfc_free_expr (c->expr1);
5887 c->expr1 = gfc_get_expr ();
5888 c->expr1->expr_type = EXPR_FUNCTION;
5889 c->expr1->symtree = target;
5890 c->expr1->where = c->loc;
5892 return resolve_call (c);
5896 /* Resolve a component-call expression. */
5897 static bool
5898 resolve_compcall (gfc_expr* e, const char **name)
5900 gfc_actual_arglist* newactual;
5901 gfc_symtree* target;
5903 /* Check that's really a FUNCTION. */
5904 if (!e->value.compcall.tbp->function)
5906 gfc_error ("%qs at %L should be a FUNCTION",
5907 e->value.compcall.name, &e->where);
5908 return false;
5911 /* These must not be assign-calls! */
5912 gcc_assert (!e->value.compcall.assign);
5914 if (!check_typebound_baseobject (e))
5915 return false;
5917 /* Pass along the name for CLASS methods, where the vtab
5918 procedure pointer component has to be referenced. */
5919 if (name)
5920 *name = e->value.compcall.name;
5922 if (!resolve_typebound_generic_call (e, name))
5923 return false;
5924 gcc_assert (!e->value.compcall.tbp->is_generic);
5926 /* Take the rank from the function's symbol. */
5927 if (e->value.compcall.tbp->u.specific->n.sym->as)
5928 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5930 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5931 arglist to the TBP's binding target. */
5933 if (!resolve_typebound_static (e, &target, &newactual))
5934 return false;
5936 e->value.function.actual = newactual;
5937 e->value.function.name = NULL;
5938 e->value.function.esym = target->n.sym;
5939 e->value.function.isym = NULL;
5940 e->symtree = target;
5941 e->ts = target->n.sym->ts;
5942 e->expr_type = EXPR_FUNCTION;
5944 /* Resolution is not necessary if this is a class subroutine; this
5945 function only has to identify the specific proc. Resolution of
5946 the call will be done next in resolve_typebound_call. */
5947 return gfc_resolve_expr (e);
5951 static bool resolve_fl_derived (gfc_symbol *sym);
5954 /* Resolve a typebound function, or 'method'. First separate all
5955 the non-CLASS references by calling resolve_compcall directly. */
5957 static bool
5958 resolve_typebound_function (gfc_expr* e)
5960 gfc_symbol *declared;
5961 gfc_component *c;
5962 gfc_ref *new_ref;
5963 gfc_ref *class_ref;
5964 gfc_symtree *st;
5965 const char *name;
5966 gfc_typespec ts;
5967 gfc_expr *expr;
5968 bool overridable;
5970 st = e->symtree;
5972 /* Deal with typebound operators for CLASS objects. */
5973 expr = e->value.compcall.base_object;
5974 overridable = !e->value.compcall.tbp->non_overridable;
5975 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5977 /* If the base_object is not a variable, the corresponding actual
5978 argument expression must be stored in e->base_expression so
5979 that the corresponding tree temporary can be used as the base
5980 object in gfc_conv_procedure_call. */
5981 if (expr->expr_type != EXPR_VARIABLE)
5983 gfc_actual_arglist *args;
5985 for (args= e->value.function.actual; args; args = args->next)
5987 if (expr == args->expr)
5988 expr = args->expr;
5992 /* Since the typebound operators are generic, we have to ensure
5993 that any delays in resolution are corrected and that the vtab
5994 is present. */
5995 ts = expr->ts;
5996 declared = ts.u.derived;
5997 c = gfc_find_component (declared, "_vptr", true, true, NULL);
5998 if (c->ts.u.derived == NULL)
5999 c->ts.u.derived = gfc_find_derived_vtab (declared);
6001 if (!resolve_compcall (e, &name))
6002 return false;
6004 /* Use the generic name if it is there. */
6005 name = name ? name : e->value.function.esym->name;
6006 e->symtree = expr->symtree;
6007 e->ref = gfc_copy_ref (expr->ref);
6008 get_declared_from_expr (&class_ref, NULL, e, false);
6010 /* Trim away the extraneous references that emerge from nested
6011 use of interface.c (extend_expr). */
6012 if (class_ref && class_ref->next)
6014 gfc_free_ref_list (class_ref->next);
6015 class_ref->next = NULL;
6017 else if (e->ref && !class_ref)
6019 gfc_free_ref_list (e->ref);
6020 e->ref = NULL;
6023 gfc_add_vptr_component (e);
6024 gfc_add_component_ref (e, name);
6025 e->value.function.esym = NULL;
6026 if (expr->expr_type != EXPR_VARIABLE)
6027 e->base_expr = expr;
6028 return true;
6031 if (st == NULL)
6032 return resolve_compcall (e, NULL);
6034 if (!resolve_ref (e))
6035 return false;
6037 /* Get the CLASS declared type. */
6038 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6040 if (!resolve_fl_derived (declared))
6041 return false;
6043 /* Weed out cases of the ultimate component being a derived type. */
6044 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6045 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6047 gfc_free_ref_list (new_ref);
6048 return resolve_compcall (e, NULL);
6051 c = gfc_find_component (declared, "_data", true, true, NULL);
6052 declared = c->ts.u.derived;
6054 /* Treat the call as if it is a typebound procedure, in order to roll
6055 out the correct name for the specific function. */
6056 if (!resolve_compcall (e, &name))
6058 gfc_free_ref_list (new_ref);
6059 return false;
6061 ts = e->ts;
6063 if (overridable)
6065 /* Convert the expression to a procedure pointer component call. */
6066 e->value.function.esym = NULL;
6067 e->symtree = st;
6069 if (new_ref)
6070 e->ref = new_ref;
6072 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6073 gfc_add_vptr_component (e);
6074 gfc_add_component_ref (e, name);
6076 /* Recover the typespec for the expression. This is really only
6077 necessary for generic procedures, where the additional call
6078 to gfc_add_component_ref seems to throw the collection of the
6079 correct typespec. */
6080 e->ts = ts;
6082 else if (new_ref)
6083 gfc_free_ref_list (new_ref);
6085 return true;
6088 /* Resolve a typebound subroutine, or 'method'. First separate all
6089 the non-CLASS references by calling resolve_typebound_call
6090 directly. */
6092 static bool
6093 resolve_typebound_subroutine (gfc_code *code)
6095 gfc_symbol *declared;
6096 gfc_component *c;
6097 gfc_ref *new_ref;
6098 gfc_ref *class_ref;
6099 gfc_symtree *st;
6100 const char *name;
6101 gfc_typespec ts;
6102 gfc_expr *expr;
6103 bool overridable;
6105 st = code->expr1->symtree;
6107 /* Deal with typebound operators for CLASS objects. */
6108 expr = code->expr1->value.compcall.base_object;
6109 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6110 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6112 /* If the base_object is not a variable, the corresponding actual
6113 argument expression must be stored in e->base_expression so
6114 that the corresponding tree temporary can be used as the base
6115 object in gfc_conv_procedure_call. */
6116 if (expr->expr_type != EXPR_VARIABLE)
6118 gfc_actual_arglist *args;
6120 args= code->expr1->value.function.actual;
6121 for (; args; args = args->next)
6122 if (expr == args->expr)
6123 expr = args->expr;
6126 /* Since the typebound operators are generic, we have to ensure
6127 that any delays in resolution are corrected and that the vtab
6128 is present. */
6129 declared = expr->ts.u.derived;
6130 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6131 if (c->ts.u.derived == NULL)
6132 c->ts.u.derived = gfc_find_derived_vtab (declared);
6134 if (!resolve_typebound_call (code, &name, NULL))
6135 return false;
6137 /* Use the generic name if it is there. */
6138 name = name ? name : code->expr1->value.function.esym->name;
6139 code->expr1->symtree = expr->symtree;
6140 code->expr1->ref = gfc_copy_ref (expr->ref);
6142 /* Trim away the extraneous references that emerge from nested
6143 use of interface.c (extend_expr). */
6144 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6145 if (class_ref && class_ref->next)
6147 gfc_free_ref_list (class_ref->next);
6148 class_ref->next = NULL;
6150 else if (code->expr1->ref && !class_ref)
6152 gfc_free_ref_list (code->expr1->ref);
6153 code->expr1->ref = NULL;
6156 /* Now use the procedure in the vtable. */
6157 gfc_add_vptr_component (code->expr1);
6158 gfc_add_component_ref (code->expr1, name);
6159 code->expr1->value.function.esym = NULL;
6160 if (expr->expr_type != EXPR_VARIABLE)
6161 code->expr1->base_expr = expr;
6162 return true;
6165 if (st == NULL)
6166 return resolve_typebound_call (code, NULL, NULL);
6168 if (!resolve_ref (code->expr1))
6169 return false;
6171 /* Get the CLASS declared type. */
6172 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
6174 /* Weed out cases of the ultimate component being a derived type. */
6175 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6176 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6178 gfc_free_ref_list (new_ref);
6179 return resolve_typebound_call (code, NULL, NULL);
6182 if (!resolve_typebound_call (code, &name, &overridable))
6184 gfc_free_ref_list (new_ref);
6185 return false;
6187 ts = code->expr1->ts;
6189 if (overridable)
6191 /* Convert the expression to a procedure pointer component call. */
6192 code->expr1->value.function.esym = NULL;
6193 code->expr1->symtree = st;
6195 if (new_ref)
6196 code->expr1->ref = new_ref;
6198 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6199 gfc_add_vptr_component (code->expr1);
6200 gfc_add_component_ref (code->expr1, name);
6202 /* Recover the typespec for the expression. This is really only
6203 necessary for generic procedures, where the additional call
6204 to gfc_add_component_ref seems to throw the collection of the
6205 correct typespec. */
6206 code->expr1->ts = ts;
6208 else if (new_ref)
6209 gfc_free_ref_list (new_ref);
6211 return true;
6215 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6217 static bool
6218 resolve_ppc_call (gfc_code* c)
6220 gfc_component *comp;
6222 comp = gfc_get_proc_ptr_comp (c->expr1);
6223 gcc_assert (comp != NULL);
6225 c->resolved_sym = c->expr1->symtree->n.sym;
6226 c->expr1->expr_type = EXPR_VARIABLE;
6228 if (!comp->attr.subroutine)
6229 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6231 if (!resolve_ref (c->expr1))
6232 return false;
6234 if (!update_ppc_arglist (c->expr1))
6235 return false;
6237 c->ext.actual = c->expr1->value.compcall.actual;
6239 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6240 !(comp->ts.interface
6241 && comp->ts.interface->formal)))
6242 return false;
6244 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
6245 return false;
6247 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6249 return true;
6253 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6255 static bool
6256 resolve_expr_ppc (gfc_expr* e)
6258 gfc_component *comp;
6260 comp = gfc_get_proc_ptr_comp (e);
6261 gcc_assert (comp != NULL);
6263 /* Convert to EXPR_FUNCTION. */
6264 e->expr_type = EXPR_FUNCTION;
6265 e->value.function.isym = NULL;
6266 e->value.function.actual = e->value.compcall.actual;
6267 e->ts = comp->ts;
6268 if (comp->as != NULL)
6269 e->rank = comp->as->rank;
6271 if (!comp->attr.function)
6272 gfc_add_function (&comp->attr, comp->name, &e->where);
6274 if (!resolve_ref (e))
6275 return false;
6277 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6278 !(comp->ts.interface
6279 && comp->ts.interface->formal)))
6280 return false;
6282 if (!update_ppc_arglist (e))
6283 return false;
6285 if (!check_pure_function(e))
6286 return false;
6288 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6290 return true;
6294 static bool
6295 gfc_is_expandable_expr (gfc_expr *e)
6297 gfc_constructor *con;
6299 if (e->expr_type == EXPR_ARRAY)
6301 /* Traverse the constructor looking for variables that are flavor
6302 parameter. Parameters must be expanded since they are fully used at
6303 compile time. */
6304 con = gfc_constructor_first (e->value.constructor);
6305 for (; con; con = gfc_constructor_next (con))
6307 if (con->expr->expr_type == EXPR_VARIABLE
6308 && con->expr->symtree
6309 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6310 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6311 return true;
6312 if (con->expr->expr_type == EXPR_ARRAY
6313 && gfc_is_expandable_expr (con->expr))
6314 return true;
6318 return false;
6321 /* Resolve an expression. That is, make sure that types of operands agree
6322 with their operators, intrinsic operators are converted to function calls
6323 for overloaded types and unresolved function references are resolved. */
6325 bool
6326 gfc_resolve_expr (gfc_expr *e)
6328 bool t;
6329 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6331 if (e == NULL)
6332 return true;
6334 /* inquiry_argument only applies to variables. */
6335 inquiry_save = inquiry_argument;
6336 actual_arg_save = actual_arg;
6337 first_actual_arg_save = first_actual_arg;
6339 if (e->expr_type != EXPR_VARIABLE)
6341 inquiry_argument = false;
6342 actual_arg = false;
6343 first_actual_arg = false;
6346 switch (e->expr_type)
6348 case EXPR_OP:
6349 t = resolve_operator (e);
6350 break;
6352 case EXPR_FUNCTION:
6353 case EXPR_VARIABLE:
6355 if (check_host_association (e))
6356 t = resolve_function (e);
6357 else
6358 t = resolve_variable (e);
6360 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6361 && e->ref->type != REF_SUBSTRING)
6362 gfc_resolve_substring_charlen (e);
6364 break;
6366 case EXPR_COMPCALL:
6367 t = resolve_typebound_function (e);
6368 break;
6370 case EXPR_SUBSTRING:
6371 t = resolve_ref (e);
6372 break;
6374 case EXPR_CONSTANT:
6375 case EXPR_NULL:
6376 t = true;
6377 break;
6379 case EXPR_PPC:
6380 t = resolve_expr_ppc (e);
6381 break;
6383 case EXPR_ARRAY:
6384 t = false;
6385 if (!resolve_ref (e))
6386 break;
6388 t = gfc_resolve_array_constructor (e);
6389 /* Also try to expand a constructor. */
6390 if (t)
6392 expression_rank (e);
6393 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6394 gfc_expand_constructor (e, false);
6397 /* This provides the opportunity for the length of constructors with
6398 character valued function elements to propagate the string length
6399 to the expression. */
6400 if (t && e->ts.type == BT_CHARACTER)
6402 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6403 here rather then add a duplicate test for it above. */
6404 gfc_expand_constructor (e, false);
6405 t = gfc_resolve_character_array_constructor (e);
6408 break;
6410 case EXPR_STRUCTURE:
6411 t = resolve_ref (e);
6412 if (!t)
6413 break;
6415 t = resolve_structure_cons (e, 0);
6416 if (!t)
6417 break;
6419 t = gfc_simplify_expr (e, 0);
6420 break;
6422 default:
6423 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6426 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
6427 fixup_charlen (e);
6429 inquiry_argument = inquiry_save;
6430 actual_arg = actual_arg_save;
6431 first_actual_arg = first_actual_arg_save;
6433 return t;
6437 /* Resolve an expression from an iterator. They must be scalar and have
6438 INTEGER or (optionally) REAL type. */
6440 static bool
6441 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6442 const char *name_msgid)
6444 if (!gfc_resolve_expr (expr))
6445 return false;
6447 if (expr->rank != 0)
6449 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6450 return false;
6453 if (expr->ts.type != BT_INTEGER)
6455 if (expr->ts.type == BT_REAL)
6457 if (real_ok)
6458 return gfc_notify_std (GFC_STD_F95_DEL,
6459 "%s at %L must be integer",
6460 _(name_msgid), &expr->where);
6461 else
6463 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6464 &expr->where);
6465 return false;
6468 else
6470 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6471 return false;
6474 return true;
6478 /* Resolve the expressions in an iterator structure. If REAL_OK is
6479 false allow only INTEGER type iterators, otherwise allow REAL types.
6480 Set own_scope to true for ac-implied-do and data-implied-do as those
6481 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
6483 bool
6484 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
6486 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6487 return false;
6489 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6490 _("iterator variable")))
6491 return false;
6493 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6494 "Start expression in DO loop"))
6495 return false;
6497 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6498 "End expression in DO loop"))
6499 return false;
6501 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6502 "Step expression in DO loop"))
6503 return false;
6505 if (iter->step->expr_type == EXPR_CONSTANT)
6507 if ((iter->step->ts.type == BT_INTEGER
6508 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6509 || (iter->step->ts.type == BT_REAL
6510 && mpfr_sgn (iter->step->value.real) == 0))
6512 gfc_error ("Step expression in DO loop at %L cannot be zero",
6513 &iter->step->where);
6514 return false;
6518 /* Convert start, end, and step to the same type as var. */
6519 if (iter->start->ts.kind != iter->var->ts.kind
6520 || iter->start->ts.type != iter->var->ts.type)
6521 gfc_convert_type (iter->start, &iter->var->ts, 1);
6523 if (iter->end->ts.kind != iter->var->ts.kind
6524 || iter->end->ts.type != iter->var->ts.type)
6525 gfc_convert_type (iter->end, &iter->var->ts, 1);
6527 if (iter->step->ts.kind != iter->var->ts.kind
6528 || iter->step->ts.type != iter->var->ts.type)
6529 gfc_convert_type (iter->step, &iter->var->ts, 1);
6531 if (iter->start->expr_type == EXPR_CONSTANT
6532 && iter->end->expr_type == EXPR_CONSTANT
6533 && iter->step->expr_type == EXPR_CONSTANT)
6535 int sgn, cmp;
6536 if (iter->start->ts.type == BT_INTEGER)
6538 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6539 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6541 else
6543 sgn = mpfr_sgn (iter->step->value.real);
6544 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6546 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
6547 gfc_warning (OPT_Wzerotrip,
6548 "DO loop at %L will be executed zero times",
6549 &iter->step->where);
6552 if (iter->end->expr_type == EXPR_CONSTANT
6553 && iter->end->ts.type == BT_INTEGER
6554 && iter->step->expr_type == EXPR_CONSTANT
6555 && iter->step->ts.type == BT_INTEGER
6556 && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
6557 || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
6559 bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
6560 int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
6562 if (is_step_positive
6563 && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
6564 gfc_warning (OPT_Wundefined_do_loop,
6565 "DO loop at %L is undefined as it overflows",
6566 &iter->step->where);
6567 else if (!is_step_positive
6568 && mpz_cmp (iter->end->value.integer,
6569 gfc_integer_kinds[k].min_int) == 0)
6570 gfc_warning (OPT_Wundefined_do_loop,
6571 "DO loop at %L is undefined as it underflows",
6572 &iter->step->where);
6575 return true;
6579 /* Traversal function for find_forall_index. f == 2 signals that
6580 that variable itself is not to be checked - only the references. */
6582 static bool
6583 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6585 if (expr->expr_type != EXPR_VARIABLE)
6586 return false;
6588 /* A scalar assignment */
6589 if (!expr->ref || *f == 1)
6591 if (expr->symtree->n.sym == sym)
6592 return true;
6593 else
6594 return false;
6597 if (*f == 2)
6598 *f = 1;
6599 return false;
6603 /* Check whether the FORALL index appears in the expression or not.
6604 Returns true if SYM is found in EXPR. */
6606 bool
6607 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6609 if (gfc_traverse_expr (expr, sym, forall_index, f))
6610 return true;
6611 else
6612 return false;
6616 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6617 to be a scalar INTEGER variable. The subscripts and stride are scalar
6618 INTEGERs, and if stride is a constant it must be nonzero.
6619 Furthermore "A subscript or stride in a forall-triplet-spec shall
6620 not contain a reference to any index-name in the
6621 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6623 static void
6624 resolve_forall_iterators (gfc_forall_iterator *it)
6626 gfc_forall_iterator *iter, *iter2;
6628 for (iter = it; iter; iter = iter->next)
6630 if (gfc_resolve_expr (iter->var)
6631 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6632 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6633 &iter->var->where);
6635 if (gfc_resolve_expr (iter->start)
6636 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6637 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6638 &iter->start->where);
6639 if (iter->var->ts.kind != iter->start->ts.kind)
6640 gfc_convert_type (iter->start, &iter->var->ts, 1);
6642 if (gfc_resolve_expr (iter->end)
6643 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6644 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6645 &iter->end->where);
6646 if (iter->var->ts.kind != iter->end->ts.kind)
6647 gfc_convert_type (iter->end, &iter->var->ts, 1);
6649 if (gfc_resolve_expr (iter->stride))
6651 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6652 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6653 &iter->stride->where, "INTEGER");
6655 if (iter->stride->expr_type == EXPR_CONSTANT
6656 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
6657 gfc_error ("FORALL stride expression at %L cannot be zero",
6658 &iter->stride->where);
6660 if (iter->var->ts.kind != iter->stride->ts.kind)
6661 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6664 for (iter = it; iter; iter = iter->next)
6665 for (iter2 = iter; iter2; iter2 = iter2->next)
6667 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6668 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6669 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
6670 gfc_error ("FORALL index %qs may not appear in triplet "
6671 "specification at %L", iter->var->symtree->name,
6672 &iter2->start->where);
6677 /* Given a pointer to a symbol that is a derived type, see if it's
6678 inaccessible, i.e. if it's defined in another module and the components are
6679 PRIVATE. The search is recursive if necessary. Returns zero if no
6680 inaccessible components are found, nonzero otherwise. */
6682 static int
6683 derived_inaccessible (gfc_symbol *sym)
6685 gfc_component *c;
6687 if (sym->attr.use_assoc && sym->attr.private_comp)
6688 return 1;
6690 for (c = sym->components; c; c = c->next)
6692 /* Prevent an infinite loop through this function. */
6693 if (c->ts.type == BT_DERIVED && c->attr.pointer
6694 && sym == c->ts.u.derived)
6695 continue;
6697 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6698 return 1;
6701 return 0;
6705 /* Resolve the argument of a deallocate expression. The expression must be
6706 a pointer or a full array. */
6708 static bool
6709 resolve_deallocate_expr (gfc_expr *e)
6711 symbol_attribute attr;
6712 int allocatable, pointer;
6713 gfc_ref *ref;
6714 gfc_symbol *sym;
6715 gfc_component *c;
6716 bool unlimited;
6718 if (!gfc_resolve_expr (e))
6719 return false;
6721 if (e->expr_type != EXPR_VARIABLE)
6722 goto bad;
6724 sym = e->symtree->n.sym;
6725 unlimited = UNLIMITED_POLY(sym);
6727 if (sym->ts.type == BT_CLASS)
6729 allocatable = CLASS_DATA (sym)->attr.allocatable;
6730 pointer = CLASS_DATA (sym)->attr.class_pointer;
6732 else
6734 allocatable = sym->attr.allocatable;
6735 pointer = sym->attr.pointer;
6737 for (ref = e->ref; ref; ref = ref->next)
6739 switch (ref->type)
6741 case REF_ARRAY:
6742 if (ref->u.ar.type != AR_FULL
6743 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6744 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6745 allocatable = 0;
6746 break;
6748 case REF_COMPONENT:
6749 c = ref->u.c.component;
6750 if (c->ts.type == BT_CLASS)
6752 allocatable = CLASS_DATA (c)->attr.allocatable;
6753 pointer = CLASS_DATA (c)->attr.class_pointer;
6755 else
6757 allocatable = c->attr.allocatable;
6758 pointer = c->attr.pointer;
6760 break;
6762 case REF_SUBSTRING:
6763 allocatable = 0;
6764 break;
6768 attr = gfc_expr_attr (e);
6770 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
6772 bad:
6773 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6774 &e->where);
6775 return false;
6778 /* F2008, C644. */
6779 if (gfc_is_coindexed (e))
6781 gfc_error ("Coindexed allocatable object at %L", &e->where);
6782 return false;
6785 if (pointer
6786 && !gfc_check_vardef_context (e, true, true, false,
6787 _("DEALLOCATE object")))
6788 return false;
6789 if (!gfc_check_vardef_context (e, false, true, false,
6790 _("DEALLOCATE object")))
6791 return false;
6793 return true;
6797 /* Returns true if the expression e contains a reference to the symbol sym. */
6798 static bool
6799 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6801 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6802 return true;
6804 return false;
6807 bool
6808 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6810 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6814 /* Given the expression node e for an allocatable/pointer of derived type to be
6815 allocated, get the expression node to be initialized afterwards (needed for
6816 derived types with default initializers, and derived types with allocatable
6817 components that need nullification.) */
6819 gfc_expr *
6820 gfc_expr_to_initialize (gfc_expr *e)
6822 gfc_expr *result;
6823 gfc_ref *ref;
6824 int i;
6826 result = gfc_copy_expr (e);
6828 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6829 for (ref = result->ref; ref; ref = ref->next)
6830 if (ref->type == REF_ARRAY && ref->next == NULL)
6832 ref->u.ar.type = AR_FULL;
6834 for (i = 0; i < ref->u.ar.dimen; i++)
6835 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6837 break;
6840 gfc_free_shape (&result->shape, result->rank);
6842 /* Recalculate rank, shape, etc. */
6843 gfc_resolve_expr (result);
6844 return result;
6848 /* If the last ref of an expression is an array ref, return a copy of the
6849 expression with that one removed. Otherwise, a copy of the original
6850 expression. This is used for allocate-expressions and pointer assignment
6851 LHS, where there may be an array specification that needs to be stripped
6852 off when using gfc_check_vardef_context. */
6854 static gfc_expr*
6855 remove_last_array_ref (gfc_expr* e)
6857 gfc_expr* e2;
6858 gfc_ref** r;
6860 e2 = gfc_copy_expr (e);
6861 for (r = &e2->ref; *r; r = &(*r)->next)
6862 if ((*r)->type == REF_ARRAY && !(*r)->next)
6864 gfc_free_ref_list (*r);
6865 *r = NULL;
6866 break;
6869 return e2;
6873 /* Used in resolve_allocate_expr to check that a allocation-object and
6874 a source-expr are conformable. This does not catch all possible
6875 cases; in particular a runtime checking is needed. */
6877 static bool
6878 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6880 gfc_ref *tail;
6881 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6883 /* First compare rank. */
6884 if ((tail && e1->rank != tail->u.ar.as->rank)
6885 || (!tail && e1->rank != e2->rank))
6887 gfc_error ("Source-expr at %L must be scalar or have the "
6888 "same rank as the allocate-object at %L",
6889 &e1->where, &e2->where);
6890 return false;
6893 if (e1->shape)
6895 int i;
6896 mpz_t s;
6898 mpz_init (s);
6900 for (i = 0; i < e1->rank; i++)
6902 if (tail->u.ar.start[i] == NULL)
6903 break;
6905 if (tail->u.ar.end[i])
6907 mpz_set (s, tail->u.ar.end[i]->value.integer);
6908 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6909 mpz_add_ui (s, s, 1);
6911 else
6913 mpz_set (s, tail->u.ar.start[i]->value.integer);
6916 if (mpz_cmp (e1->shape[i], s) != 0)
6918 gfc_error ("Source-expr at %L and allocate-object at %L must "
6919 "have the same shape", &e1->where, &e2->where);
6920 mpz_clear (s);
6921 return false;
6925 mpz_clear (s);
6928 return true;
6931 static void
6932 cond_init (gfc_code *code, gfc_expr *e, int pointer, gfc_expr *init_e)
6934 gfc_code *block;
6935 gfc_expr *cond;
6936 gfc_code *init_st;
6937 gfc_expr *e_to_init = gfc_expr_to_initialize (e);
6939 cond = pointer
6940 ? gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_ASSOCIATED,
6941 "associated", code->loc, 2, gfc_copy_expr (e_to_init), NULL)
6942 : gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_ALLOCATED,
6943 "allocated", code->loc, 1, gfc_copy_expr (e_to_init));
6945 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
6946 init_st->loc = code->loc;
6947 init_st->expr1 = e_to_init;
6948 init_st->expr2 = init_e;
6950 block = gfc_get_code (EXEC_IF);
6951 block->loc = code->loc;
6952 block->block = gfc_get_code (EXEC_IF);
6953 block->block->loc = code->loc;
6954 block->block->expr1 = cond;
6955 block->block->next = init_st;
6956 block->next = code->next;
6958 code->next = block;
6961 /* Resolve the expression in an ALLOCATE statement, doing the additional
6962 checks to see whether the expression is OK or not. The expression must
6963 have a trailing array reference that gives the size of the array. */
6965 static bool
6966 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
6968 int i, pointer, allocatable, dimension, is_abstract;
6969 int codimension;
6970 bool coindexed;
6971 bool unlimited;
6972 symbol_attribute attr;
6973 gfc_ref *ref, *ref2;
6974 gfc_expr *e2;
6975 gfc_array_ref *ar;
6976 gfc_symbol *sym = NULL;
6977 gfc_alloc *a;
6978 gfc_component *c;
6979 bool t;
6981 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
6982 checking of coarrays. */
6983 for (ref = e->ref; ref; ref = ref->next)
6984 if (ref->next == NULL)
6985 break;
6987 if (ref && ref->type == REF_ARRAY)
6988 ref->u.ar.in_allocate = true;
6990 if (!gfc_resolve_expr (e))
6991 goto failure;
6993 /* Make sure the expression is allocatable or a pointer. If it is
6994 pointer, the next-to-last reference must be a pointer. */
6996 ref2 = NULL;
6997 if (e->symtree)
6998 sym = e->symtree->n.sym;
7000 /* Check whether ultimate component is abstract and CLASS. */
7001 is_abstract = 0;
7003 /* Is the allocate-object unlimited polymorphic? */
7004 unlimited = UNLIMITED_POLY(e);
7006 if (e->expr_type != EXPR_VARIABLE)
7008 allocatable = 0;
7009 attr = gfc_expr_attr (e);
7010 pointer = attr.pointer;
7011 dimension = attr.dimension;
7012 codimension = attr.codimension;
7014 else
7016 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
7018 allocatable = CLASS_DATA (sym)->attr.allocatable;
7019 pointer = CLASS_DATA (sym)->attr.class_pointer;
7020 dimension = CLASS_DATA (sym)->attr.dimension;
7021 codimension = CLASS_DATA (sym)->attr.codimension;
7022 is_abstract = CLASS_DATA (sym)->attr.abstract;
7024 else
7026 allocatable = sym->attr.allocatable;
7027 pointer = sym->attr.pointer;
7028 dimension = sym->attr.dimension;
7029 codimension = sym->attr.codimension;
7032 coindexed = false;
7034 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
7036 switch (ref->type)
7038 case REF_ARRAY:
7039 if (ref->u.ar.codimen > 0)
7041 int n;
7042 for (n = ref->u.ar.dimen;
7043 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
7044 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
7046 coindexed = true;
7047 break;
7051 if (ref->next != NULL)
7052 pointer = 0;
7053 break;
7055 case REF_COMPONENT:
7056 /* F2008, C644. */
7057 if (coindexed)
7059 gfc_error ("Coindexed allocatable object at %L",
7060 &e->where);
7061 goto failure;
7064 c = ref->u.c.component;
7065 if (c->ts.type == BT_CLASS)
7067 allocatable = CLASS_DATA (c)->attr.allocatable;
7068 pointer = CLASS_DATA (c)->attr.class_pointer;
7069 dimension = CLASS_DATA (c)->attr.dimension;
7070 codimension = CLASS_DATA (c)->attr.codimension;
7071 is_abstract = CLASS_DATA (c)->attr.abstract;
7073 else
7075 allocatable = c->attr.allocatable;
7076 pointer = c->attr.pointer;
7077 dimension = c->attr.dimension;
7078 codimension = c->attr.codimension;
7079 is_abstract = c->attr.abstract;
7081 break;
7083 case REF_SUBSTRING:
7084 allocatable = 0;
7085 pointer = 0;
7086 break;
7091 /* Check for F08:C628. */
7092 if (allocatable == 0 && pointer == 0 && !unlimited)
7094 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7095 &e->where);
7096 goto failure;
7099 /* Some checks for the SOURCE tag. */
7100 if (code->expr3)
7102 /* Check F03:C631. */
7103 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7105 gfc_error ("Type of entity at %L is type incompatible with "
7106 "source-expr at %L", &e->where, &code->expr3->where);
7107 goto failure;
7110 /* Check F03:C632 and restriction following Note 6.18. */
7111 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
7112 goto failure;
7114 /* Check F03:C633. */
7115 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
7117 gfc_error ("The allocate-object at %L and the source-expr at %L "
7118 "shall have the same kind type parameter",
7119 &e->where, &code->expr3->where);
7120 goto failure;
7123 /* Check F2008, C642. */
7124 if (code->expr3->ts.type == BT_DERIVED
7125 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7126 || (code->expr3->ts.u.derived->from_intmod
7127 == INTMOD_ISO_FORTRAN_ENV
7128 && code->expr3->ts.u.derived->intmod_sym_id
7129 == ISOFORTRAN_LOCK_TYPE)))
7131 gfc_error ("The source-expr at %L shall neither be of type "
7132 "LOCK_TYPE nor have a LOCK_TYPE component if "
7133 "allocate-object at %L is a coarray",
7134 &code->expr3->where, &e->where);
7135 goto failure;
7138 /* Check TS18508, C702/C703. */
7139 if (code->expr3->ts.type == BT_DERIVED
7140 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
7141 || (code->expr3->ts.u.derived->from_intmod
7142 == INTMOD_ISO_FORTRAN_ENV
7143 && code->expr3->ts.u.derived->intmod_sym_id
7144 == ISOFORTRAN_EVENT_TYPE)))
7146 gfc_error ("The source-expr at %L shall neither be of type "
7147 "EVENT_TYPE nor have a EVENT_TYPE component if "
7148 "allocate-object at %L is a coarray",
7149 &code->expr3->where, &e->where);
7150 goto failure;
7154 /* Check F08:C629. */
7155 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7156 && !code->expr3)
7158 gcc_assert (e->ts.type == BT_CLASS);
7159 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7160 "type-spec or source-expr", sym->name, &e->where);
7161 goto failure;
7164 /* Check F08:C632. */
7165 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
7166 && !UNLIMITED_POLY (e))
7168 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7169 code->ext.alloc.ts.u.cl->length);
7170 if (cmp == 1 || cmp == -1 || cmp == -3)
7172 gfc_error ("Allocating %s at %L with type-spec requires the same "
7173 "character-length parameter as in the declaration",
7174 sym->name, &e->where);
7175 goto failure;
7179 /* In the variable definition context checks, gfc_expr_attr is used
7180 on the expression. This is fooled by the array specification
7181 present in e, thus we have to eliminate that one temporarily. */
7182 e2 = remove_last_array_ref (e);
7183 t = true;
7184 if (t && pointer)
7185 t = gfc_check_vardef_context (e2, true, true, false,
7186 _("ALLOCATE object"));
7187 if (t)
7188 t = gfc_check_vardef_context (e2, false, true, false,
7189 _("ALLOCATE object"));
7190 gfc_free_expr (e2);
7191 if (!t)
7192 goto failure;
7194 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7195 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7197 /* For class arrays, the initialization with SOURCE is done
7198 using _copy and trans_call. It is convenient to exploit that
7199 when the allocated type is different from the declared type but
7200 no SOURCE exists by setting expr3. */
7201 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
7203 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
7204 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
7205 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
7207 /* We have to zero initialize the integer variable. */
7208 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
7210 else if (!code->expr3)
7212 /* Set up default initializer if needed. */
7213 gfc_typespec ts;
7214 gfc_expr *init_e;
7216 if (gfc_bt_struct (code->ext.alloc.ts.type))
7217 ts = code->ext.alloc.ts;
7218 else
7219 ts = e->ts;
7221 if (ts.type == BT_CLASS)
7222 ts = ts.u.derived->components->ts;
7224 if (gfc_bt_struct (ts.type) && (init_e = gfc_default_initializer (&ts)))
7225 cond_init (code, e, pointer, init_e);
7227 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
7229 /* Default initialization via MOLD (non-polymorphic). */
7230 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
7231 if (rhs != NULL)
7233 gfc_resolve_expr (rhs);
7234 gfc_free_expr (code->expr3);
7235 code->expr3 = rhs;
7239 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
7241 /* Make sure the vtab symbol is present when
7242 the module variables are generated. */
7243 gfc_typespec ts = e->ts;
7244 if (code->expr3)
7245 ts = code->expr3->ts;
7246 else if (code->ext.alloc.ts.type == BT_DERIVED)
7247 ts = code->ext.alloc.ts;
7249 gfc_find_derived_vtab (ts.u.derived);
7251 if (dimension)
7252 e = gfc_expr_to_initialize (e);
7254 else if (unlimited && !UNLIMITED_POLY (code->expr3))
7256 /* Again, make sure the vtab symbol is present when
7257 the module variables are generated. */
7258 gfc_typespec *ts = NULL;
7259 if (code->expr3)
7260 ts = &code->expr3->ts;
7261 else
7262 ts = &code->ext.alloc.ts;
7264 gcc_assert (ts);
7266 gfc_find_vtab (ts);
7268 if (dimension)
7269 e = gfc_expr_to_initialize (e);
7272 if (dimension == 0 && codimension == 0)
7273 goto success;
7275 /* Make sure the last reference node is an array specification. */
7277 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
7278 || (dimension && ref2->u.ar.dimen == 0))
7280 /* F08:C633. */
7281 if (code->expr3)
7283 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
7284 "in ALLOCATE statement at %L", &e->where))
7285 goto failure;
7286 if (code->expr3->rank != 0)
7287 *array_alloc_wo_spec = true;
7288 else
7290 gfc_error ("Array specification or array-valued SOURCE= "
7291 "expression required in ALLOCATE statement at %L",
7292 &e->where);
7293 goto failure;
7296 else
7298 gfc_error ("Array specification required in ALLOCATE statement "
7299 "at %L", &e->where);
7300 goto failure;
7304 /* Make sure that the array section reference makes sense in the
7305 context of an ALLOCATE specification. */
7307 ar = &ref2->u.ar;
7309 if (codimension)
7310 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
7311 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
7313 gfc_error ("Coarray specification required in ALLOCATE statement "
7314 "at %L", &e->where);
7315 goto failure;
7318 for (i = 0; i < ar->dimen; i++)
7320 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
7321 goto check_symbols;
7323 switch (ar->dimen_type[i])
7325 case DIMEN_ELEMENT:
7326 break;
7328 case DIMEN_RANGE:
7329 if (ar->start[i] != NULL
7330 && ar->end[i] != NULL
7331 && ar->stride[i] == NULL)
7332 break;
7334 /* Fall through. */
7336 case DIMEN_UNKNOWN:
7337 case DIMEN_VECTOR:
7338 case DIMEN_STAR:
7339 case DIMEN_THIS_IMAGE:
7340 gfc_error ("Bad array specification in ALLOCATE statement at %L",
7341 &e->where);
7342 goto failure;
7345 check_symbols:
7346 for (a = code->ext.alloc.list; a; a = a->next)
7348 sym = a->expr->symtree->n.sym;
7350 /* TODO - check derived type components. */
7351 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
7352 continue;
7354 if ((ar->start[i] != NULL
7355 && gfc_find_sym_in_expr (sym, ar->start[i]))
7356 || (ar->end[i] != NULL
7357 && gfc_find_sym_in_expr (sym, ar->end[i])))
7359 gfc_error ("%qs must not appear in the array specification at "
7360 "%L in the same ALLOCATE statement where it is "
7361 "itself allocated", sym->name, &ar->where);
7362 goto failure;
7367 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7369 if (ar->dimen_type[i] == DIMEN_ELEMENT
7370 || ar->dimen_type[i] == DIMEN_RANGE)
7372 if (i == (ar->dimen + ar->codimen - 1))
7374 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7375 "statement at %L", &e->where);
7376 goto failure;
7378 continue;
7381 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7382 && ar->stride[i] == NULL)
7383 break;
7385 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7386 &e->where);
7387 goto failure;
7390 success:
7391 return true;
7393 failure:
7394 return false;
7398 static void
7399 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7401 gfc_expr *stat, *errmsg, *pe, *qe;
7402 gfc_alloc *a, *p, *q;
7404 stat = code->expr1;
7405 errmsg = code->expr2;
7407 /* Check the stat variable. */
7408 if (stat)
7410 gfc_check_vardef_context (stat, false, false, false,
7411 _("STAT variable"));
7413 if ((stat->ts.type != BT_INTEGER
7414 && !(stat->ref && (stat->ref->type == REF_ARRAY
7415 || stat->ref->type == REF_COMPONENT)))
7416 || stat->rank > 0)
7417 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7418 "variable", &stat->where);
7420 for (p = code->ext.alloc.list; p; p = p->next)
7421 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7423 gfc_ref *ref1, *ref2;
7424 bool found = true;
7426 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7427 ref1 = ref1->next, ref2 = ref2->next)
7429 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7430 continue;
7431 if (ref1->u.c.component->name != ref2->u.c.component->name)
7433 found = false;
7434 break;
7438 if (found)
7440 gfc_error ("Stat-variable at %L shall not be %sd within "
7441 "the same %s statement", &stat->where, fcn, fcn);
7442 break;
7447 /* Check the errmsg variable. */
7448 if (errmsg)
7450 if (!stat)
7451 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
7452 &errmsg->where);
7454 gfc_check_vardef_context (errmsg, false, false, false,
7455 _("ERRMSG variable"));
7457 if ((errmsg->ts.type != BT_CHARACTER
7458 && !(errmsg->ref
7459 && (errmsg->ref->type == REF_ARRAY
7460 || errmsg->ref->type == REF_COMPONENT)))
7461 || errmsg->rank > 0 )
7462 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7463 "variable", &errmsg->where);
7465 for (p = code->ext.alloc.list; p; p = p->next)
7466 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7468 gfc_ref *ref1, *ref2;
7469 bool found = true;
7471 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7472 ref1 = ref1->next, ref2 = ref2->next)
7474 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7475 continue;
7476 if (ref1->u.c.component->name != ref2->u.c.component->name)
7478 found = false;
7479 break;
7483 if (found)
7485 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7486 "the same %s statement", &errmsg->where, fcn, fcn);
7487 break;
7492 /* Check that an allocate-object appears only once in the statement. */
7494 for (p = code->ext.alloc.list; p; p = p->next)
7496 pe = p->expr;
7497 for (q = p->next; q; q = q->next)
7499 qe = q->expr;
7500 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7502 /* This is a potential collision. */
7503 gfc_ref *pr = pe->ref;
7504 gfc_ref *qr = qe->ref;
7506 /* Follow the references until
7507 a) They start to differ, in which case there is no error;
7508 you can deallocate a%b and a%c in a single statement
7509 b) Both of them stop, which is an error
7510 c) One of them stops, which is also an error. */
7511 while (1)
7513 if (pr == NULL && qr == NULL)
7515 gfc_error ("Allocate-object at %L also appears at %L",
7516 &pe->where, &qe->where);
7517 break;
7519 else if (pr != NULL && qr == NULL)
7521 gfc_error ("Allocate-object at %L is subobject of"
7522 " object at %L", &pe->where, &qe->where);
7523 break;
7525 else if (pr == NULL && qr != NULL)
7527 gfc_error ("Allocate-object at %L is subobject of"
7528 " object at %L", &qe->where, &pe->where);
7529 break;
7531 /* Here, pr != NULL && qr != NULL */
7532 gcc_assert(pr->type == qr->type);
7533 if (pr->type == REF_ARRAY)
7535 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7536 which are legal. */
7537 gcc_assert (qr->type == REF_ARRAY);
7539 if (pr->next && qr->next)
7541 int i;
7542 gfc_array_ref *par = &(pr->u.ar);
7543 gfc_array_ref *qar = &(qr->u.ar);
7545 for (i=0; i<par->dimen; i++)
7547 if ((par->start[i] != NULL
7548 || qar->start[i] != NULL)
7549 && gfc_dep_compare_expr (par->start[i],
7550 qar->start[i]) != 0)
7551 goto break_label;
7555 else
7557 if (pr->u.c.component->name != qr->u.c.component->name)
7558 break;
7561 pr = pr->next;
7562 qr = qr->next;
7564 break_label:
7570 if (strcmp (fcn, "ALLOCATE") == 0)
7572 bool arr_alloc_wo_spec = false;
7573 for (a = code->ext.alloc.list; a; a = a->next)
7574 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
7576 if (arr_alloc_wo_spec && code->expr3)
7578 /* Mark the allocate to have to take the array specification
7579 from the expr3. */
7580 code->ext.alloc.arr_spec_from_expr3 = 1;
7583 else
7585 for (a = code->ext.alloc.list; a; a = a->next)
7586 resolve_deallocate_expr (a->expr);
7591 /************ SELECT CASE resolution subroutines ************/
7593 /* Callback function for our mergesort variant. Determines interval
7594 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7595 op1 > op2. Assumes we're not dealing with the default case.
7596 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7597 There are nine situations to check. */
7599 static int
7600 compare_cases (const gfc_case *op1, const gfc_case *op2)
7602 int retval;
7604 if (op1->low == NULL) /* op1 = (:L) */
7606 /* op2 = (:N), so overlap. */
7607 retval = 0;
7608 /* op2 = (M:) or (M:N), L < M */
7609 if (op2->low != NULL
7610 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7611 retval = -1;
7613 else if (op1->high == NULL) /* op1 = (K:) */
7615 /* op2 = (M:), so overlap. */
7616 retval = 0;
7617 /* op2 = (:N) or (M:N), K > N */
7618 if (op2->high != NULL
7619 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7620 retval = 1;
7622 else /* op1 = (K:L) */
7624 if (op2->low == NULL) /* op2 = (:N), K > N */
7625 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7626 ? 1 : 0;
7627 else if (op2->high == NULL) /* op2 = (M:), L < M */
7628 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7629 ? -1 : 0;
7630 else /* op2 = (M:N) */
7632 retval = 0;
7633 /* L < M */
7634 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7635 retval = -1;
7636 /* K > N */
7637 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7638 retval = 1;
7642 return retval;
7646 /* Merge-sort a double linked case list, detecting overlap in the
7647 process. LIST is the head of the double linked case list before it
7648 is sorted. Returns the head of the sorted list if we don't see any
7649 overlap, or NULL otherwise. */
7651 static gfc_case *
7652 check_case_overlap (gfc_case *list)
7654 gfc_case *p, *q, *e, *tail;
7655 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7657 /* If the passed list was empty, return immediately. */
7658 if (!list)
7659 return NULL;
7661 overlap_seen = 0;
7662 insize = 1;
7664 /* Loop unconditionally. The only exit from this loop is a return
7665 statement, when we've finished sorting the case list. */
7666 for (;;)
7668 p = list;
7669 list = NULL;
7670 tail = NULL;
7672 /* Count the number of merges we do in this pass. */
7673 nmerges = 0;
7675 /* Loop while there exists a merge to be done. */
7676 while (p)
7678 int i;
7680 /* Count this merge. */
7681 nmerges++;
7683 /* Cut the list in two pieces by stepping INSIZE places
7684 forward in the list, starting from P. */
7685 psize = 0;
7686 q = p;
7687 for (i = 0; i < insize; i++)
7689 psize++;
7690 q = q->right;
7691 if (!q)
7692 break;
7694 qsize = insize;
7696 /* Now we have two lists. Merge them! */
7697 while (psize > 0 || (qsize > 0 && q != NULL))
7699 /* See from which the next case to merge comes from. */
7700 if (psize == 0)
7702 /* P is empty so the next case must come from Q. */
7703 e = q;
7704 q = q->right;
7705 qsize--;
7707 else if (qsize == 0 || q == NULL)
7709 /* Q is empty. */
7710 e = p;
7711 p = p->right;
7712 psize--;
7714 else
7716 cmp = compare_cases (p, q);
7717 if (cmp < 0)
7719 /* The whole case range for P is less than the
7720 one for Q. */
7721 e = p;
7722 p = p->right;
7723 psize--;
7725 else if (cmp > 0)
7727 /* The whole case range for Q is greater than
7728 the case range for P. */
7729 e = q;
7730 q = q->right;
7731 qsize--;
7733 else
7735 /* The cases overlap, or they are the same
7736 element in the list. Either way, we must
7737 issue an error and get the next case from P. */
7738 /* FIXME: Sort P and Q by line number. */
7739 gfc_error ("CASE label at %L overlaps with CASE "
7740 "label at %L", &p->where, &q->where);
7741 overlap_seen = 1;
7742 e = p;
7743 p = p->right;
7744 psize--;
7748 /* Add the next element to the merged list. */
7749 if (tail)
7750 tail->right = e;
7751 else
7752 list = e;
7753 e->left = tail;
7754 tail = e;
7757 /* P has now stepped INSIZE places along, and so has Q. So
7758 they're the same. */
7759 p = q;
7761 tail->right = NULL;
7763 /* If we have done only one merge or none at all, we've
7764 finished sorting the cases. */
7765 if (nmerges <= 1)
7767 if (!overlap_seen)
7768 return list;
7769 else
7770 return NULL;
7773 /* Otherwise repeat, merging lists twice the size. */
7774 insize *= 2;
7779 /* Check to see if an expression is suitable for use in a CASE statement.
7780 Makes sure that all case expressions are scalar constants of the same
7781 type. Return false if anything is wrong. */
7783 static bool
7784 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7786 if (e == NULL) return true;
7788 if (e->ts.type != case_expr->ts.type)
7790 gfc_error ("Expression in CASE statement at %L must be of type %s",
7791 &e->where, gfc_basic_typename (case_expr->ts.type));
7792 return false;
7795 /* C805 (R808) For a given case-construct, each case-value shall be of
7796 the same type as case-expr. For character type, length differences
7797 are allowed, but the kind type parameters shall be the same. */
7799 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7801 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7802 &e->where, case_expr->ts.kind);
7803 return false;
7806 /* Convert the case value kind to that of case expression kind,
7807 if needed */
7809 if (e->ts.kind != case_expr->ts.kind)
7810 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7812 if (e->rank != 0)
7814 gfc_error ("Expression in CASE statement at %L must be scalar",
7815 &e->where);
7816 return false;
7819 return true;
7823 /* Given a completely parsed select statement, we:
7825 - Validate all expressions and code within the SELECT.
7826 - Make sure that the selection expression is not of the wrong type.
7827 - Make sure that no case ranges overlap.
7828 - Eliminate unreachable cases and unreachable code resulting from
7829 removing case labels.
7831 The standard does allow unreachable cases, e.g. CASE (5:3). But
7832 they are a hassle for code generation, and to prevent that, we just
7833 cut them out here. This is not necessary for overlapping cases
7834 because they are illegal and we never even try to generate code.
7836 We have the additional caveat that a SELECT construct could have
7837 been a computed GOTO in the source code. Fortunately we can fairly
7838 easily work around that here: The case_expr for a "real" SELECT CASE
7839 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7840 we have to do is make sure that the case_expr is a scalar integer
7841 expression. */
7843 static void
7844 resolve_select (gfc_code *code, bool select_type)
7846 gfc_code *body;
7847 gfc_expr *case_expr;
7848 gfc_case *cp, *default_case, *tail, *head;
7849 int seen_unreachable;
7850 int seen_logical;
7851 int ncases;
7852 bt type;
7853 bool t;
7855 if (code->expr1 == NULL)
7857 /* This was actually a computed GOTO statement. */
7858 case_expr = code->expr2;
7859 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7860 gfc_error ("Selection expression in computed GOTO statement "
7861 "at %L must be a scalar integer expression",
7862 &case_expr->where);
7864 /* Further checking is not necessary because this SELECT was built
7865 by the compiler, so it should always be OK. Just move the
7866 case_expr from expr2 to expr so that we can handle computed
7867 GOTOs as normal SELECTs from here on. */
7868 code->expr1 = code->expr2;
7869 code->expr2 = NULL;
7870 return;
7873 case_expr = code->expr1;
7874 type = case_expr->ts.type;
7876 /* F08:C830. */
7877 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7879 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7880 &case_expr->where, gfc_typename (&case_expr->ts));
7882 /* Punt. Going on here just produce more garbage error messages. */
7883 return;
7886 /* F08:R842. */
7887 if (!select_type && case_expr->rank != 0)
7889 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7890 "expression", &case_expr->where);
7892 /* Punt. */
7893 return;
7896 /* Raise a warning if an INTEGER case value exceeds the range of
7897 the case-expr. Later, all expressions will be promoted to the
7898 largest kind of all case-labels. */
7900 if (type == BT_INTEGER)
7901 for (body = code->block; body; body = body->block)
7902 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7904 if (cp->low
7905 && gfc_check_integer_range (cp->low->value.integer,
7906 case_expr->ts.kind) != ARITH_OK)
7907 gfc_warning (0, "Expression in CASE statement at %L is "
7908 "not in the range of %s", &cp->low->where,
7909 gfc_typename (&case_expr->ts));
7911 if (cp->high
7912 && cp->low != cp->high
7913 && gfc_check_integer_range (cp->high->value.integer,
7914 case_expr->ts.kind) != ARITH_OK)
7915 gfc_warning (0, "Expression in CASE statement at %L is "
7916 "not in the range of %s", &cp->high->where,
7917 gfc_typename (&case_expr->ts));
7920 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7921 of the SELECT CASE expression and its CASE values. Walk the lists
7922 of case values, and if we find a mismatch, promote case_expr to
7923 the appropriate kind. */
7925 if (type == BT_LOGICAL || type == BT_INTEGER)
7927 for (body = code->block; body; body = body->block)
7929 /* Walk the case label list. */
7930 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7932 /* Intercept the DEFAULT case. It does not have a kind. */
7933 if (cp->low == NULL && cp->high == NULL)
7934 continue;
7936 /* Unreachable case ranges are discarded, so ignore. */
7937 if (cp->low != NULL && cp->high != NULL
7938 && cp->low != cp->high
7939 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7940 continue;
7942 if (cp->low != NULL
7943 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7944 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7946 if (cp->high != NULL
7947 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7948 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7953 /* Assume there is no DEFAULT case. */
7954 default_case = NULL;
7955 head = tail = NULL;
7956 ncases = 0;
7957 seen_logical = 0;
7959 for (body = code->block; body; body = body->block)
7961 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7962 t = true;
7963 seen_unreachable = 0;
7965 /* Walk the case label list, making sure that all case labels
7966 are legal. */
7967 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7969 /* Count the number of cases in the whole construct. */
7970 ncases++;
7972 /* Intercept the DEFAULT case. */
7973 if (cp->low == NULL && cp->high == NULL)
7975 if (default_case != NULL)
7977 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7978 "by a second DEFAULT CASE at %L",
7979 &default_case->where, &cp->where);
7980 t = false;
7981 break;
7983 else
7985 default_case = cp;
7986 continue;
7990 /* Deal with single value cases and case ranges. Errors are
7991 issued from the validation function. */
7992 if (!validate_case_label_expr (cp->low, case_expr)
7993 || !validate_case_label_expr (cp->high, case_expr))
7995 t = false;
7996 break;
7999 if (type == BT_LOGICAL
8000 && ((cp->low == NULL || cp->high == NULL)
8001 || cp->low != cp->high))
8003 gfc_error ("Logical range in CASE statement at %L is not "
8004 "allowed", &cp->low->where);
8005 t = false;
8006 break;
8009 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
8011 int value;
8012 value = cp->low->value.logical == 0 ? 2 : 1;
8013 if (value & seen_logical)
8015 gfc_error ("Constant logical value in CASE statement "
8016 "is repeated at %L",
8017 &cp->low->where);
8018 t = false;
8019 break;
8021 seen_logical |= value;
8024 if (cp->low != NULL && cp->high != NULL
8025 && cp->low != cp->high
8026 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8028 if (warn_surprising)
8029 gfc_warning (OPT_Wsurprising,
8030 "Range specification at %L can never be matched",
8031 &cp->where);
8033 cp->unreachable = 1;
8034 seen_unreachable = 1;
8036 else
8038 /* If the case range can be matched, it can also overlap with
8039 other cases. To make sure it does not, we put it in a
8040 double linked list here. We sort that with a merge sort
8041 later on to detect any overlapping cases. */
8042 if (!head)
8044 head = tail = cp;
8045 head->right = head->left = NULL;
8047 else
8049 tail->right = cp;
8050 tail->right->left = tail;
8051 tail = tail->right;
8052 tail->right = NULL;
8057 /* It there was a failure in the previous case label, give up
8058 for this case label list. Continue with the next block. */
8059 if (!t)
8060 continue;
8062 /* See if any case labels that are unreachable have been seen.
8063 If so, we eliminate them. This is a bit of a kludge because
8064 the case lists for a single case statement (label) is a
8065 single forward linked lists. */
8066 if (seen_unreachable)
8068 /* Advance until the first case in the list is reachable. */
8069 while (body->ext.block.case_list != NULL
8070 && body->ext.block.case_list->unreachable)
8072 gfc_case *n = body->ext.block.case_list;
8073 body->ext.block.case_list = body->ext.block.case_list->next;
8074 n->next = NULL;
8075 gfc_free_case_list (n);
8078 /* Strip all other unreachable cases. */
8079 if (body->ext.block.case_list)
8081 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
8083 if (cp->next->unreachable)
8085 gfc_case *n = cp->next;
8086 cp->next = cp->next->next;
8087 n->next = NULL;
8088 gfc_free_case_list (n);
8095 /* See if there were overlapping cases. If the check returns NULL,
8096 there was overlap. In that case we don't do anything. If head
8097 is non-NULL, we prepend the DEFAULT case. The sorted list can
8098 then used during code generation for SELECT CASE constructs with
8099 a case expression of a CHARACTER type. */
8100 if (head)
8102 head = check_case_overlap (head);
8104 /* Prepend the default_case if it is there. */
8105 if (head != NULL && default_case)
8107 default_case->left = NULL;
8108 default_case->right = head;
8109 head->left = default_case;
8113 /* Eliminate dead blocks that may be the result if we've seen
8114 unreachable case labels for a block. */
8115 for (body = code; body && body->block; body = body->block)
8117 if (body->block->ext.block.case_list == NULL)
8119 /* Cut the unreachable block from the code chain. */
8120 gfc_code *c = body->block;
8121 body->block = c->block;
8123 /* Kill the dead block, but not the blocks below it. */
8124 c->block = NULL;
8125 gfc_free_statements (c);
8129 /* More than two cases is legal but insane for logical selects.
8130 Issue a warning for it. */
8131 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
8132 gfc_warning (OPT_Wsurprising,
8133 "Logical SELECT CASE block at %L has more that two cases",
8134 &code->loc);
8138 /* Check if a derived type is extensible. */
8140 bool
8141 gfc_type_is_extensible (gfc_symbol *sym)
8143 return !(sym->attr.is_bind_c || sym->attr.sequence
8144 || (sym->attr.is_class
8145 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
8149 static void
8150 resolve_types (gfc_namespace *ns);
8152 /* Resolve an associate-name: Resolve target and ensure the type-spec is
8153 correct as well as possibly the array-spec. */
8155 static void
8156 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
8158 gfc_expr* target;
8160 gcc_assert (sym->assoc);
8161 gcc_assert (sym->attr.flavor == FL_VARIABLE);
8163 /* If this is for SELECT TYPE, the target may not yet be set. In that
8164 case, return. Resolution will be called later manually again when
8165 this is done. */
8166 target = sym->assoc->target;
8167 if (!target)
8168 return;
8169 gcc_assert (!sym->assoc->dangling);
8171 if (resolve_target && !gfc_resolve_expr (target))
8172 return;
8174 /* For variable targets, we get some attributes from the target. */
8175 if (target->expr_type == EXPR_VARIABLE)
8177 gfc_symbol* tsym;
8179 gcc_assert (target->symtree);
8180 tsym = target->symtree->n.sym;
8182 sym->attr.asynchronous = tsym->attr.asynchronous;
8183 sym->attr.volatile_ = tsym->attr.volatile_;
8185 sym->attr.target = tsym->attr.target
8186 || gfc_expr_attr (target).pointer;
8187 if (is_subref_array (target))
8188 sym->attr.subref_array_pointer = 1;
8191 /* Get type if this was not already set. Note that it can be
8192 some other type than the target in case this is a SELECT TYPE
8193 selector! So we must not update when the type is already there. */
8194 if (sym->ts.type == BT_UNKNOWN)
8195 sym->ts = target->ts;
8196 gcc_assert (sym->ts.type != BT_UNKNOWN);
8198 /* See if this is a valid association-to-variable. */
8199 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
8200 && !gfc_has_vector_subscript (target));
8202 /* Finally resolve if this is an array or not. */
8203 if (sym->attr.dimension && target->rank == 0)
8205 /* primary.c makes the assumption that a reference to an associate
8206 name followed by a left parenthesis is an array reference. */
8207 if (sym->ts.type != BT_CHARACTER)
8208 gfc_error ("Associate-name %qs at %L is used as array",
8209 sym->name, &sym->declared_at);
8210 sym->attr.dimension = 0;
8211 return;
8215 /* We cannot deal with class selectors that need temporaries. */
8216 if (target->ts.type == BT_CLASS
8217 && gfc_ref_needs_temporary_p (target->ref))
8219 gfc_error ("CLASS selector at %L needs a temporary which is not "
8220 "yet implemented", &target->where);
8221 return;
8224 if (target->ts.type == BT_CLASS)
8225 gfc_fix_class_refs (target);
8227 if (target->rank != 0)
8229 gfc_array_spec *as;
8230 /* The rank may be incorrectly guessed at parsing, therefore make sure
8231 it is corrected now. */
8232 if (sym->ts.type != BT_CLASS && (!sym->as || sym->assoc->rankguessed))
8234 if (!sym->as)
8235 sym->as = gfc_get_array_spec ();
8236 as = sym->as;
8237 as->rank = target->rank;
8238 as->type = AS_DEFERRED;
8239 as->corank = gfc_get_corank (target);
8240 sym->attr.dimension = 1;
8241 if (as->corank != 0)
8242 sym->attr.codimension = 1;
8245 else
8247 /* target's rank is 0, but the type of the sym is still array valued,
8248 which has to be corrected. */
8249 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
8251 gfc_array_spec *as;
8252 symbol_attribute attr;
8253 /* The associated variable's type is still the array type
8254 correct this now. */
8255 gfc_typespec *ts = &target->ts;
8256 gfc_ref *ref;
8257 gfc_component *c;
8258 for (ref = target->ref; ref != NULL; ref = ref->next)
8260 switch (ref->type)
8262 case REF_COMPONENT:
8263 ts = &ref->u.c.component->ts;
8264 break;
8265 case REF_ARRAY:
8266 if (ts->type == BT_CLASS)
8267 ts = &ts->u.derived->components->ts;
8268 break;
8269 default:
8270 break;
8273 /* Create a scalar instance of the current class type. Because the
8274 rank of a class array goes into its name, the type has to be
8275 rebuild. The alternative of (re-)setting just the attributes
8276 and as in the current type, destroys the type also in other
8277 places. */
8278 as = NULL;
8279 sym->ts = *ts;
8280 sym->ts.type = BT_CLASS;
8281 attr = CLASS_DATA (sym)->attr;
8282 attr.class_ok = 0;
8283 attr.associate_var = 1;
8284 attr.dimension = attr.codimension = 0;
8285 attr.class_pointer = 1;
8286 if (!gfc_build_class_symbol (&sym->ts, &attr, &as))
8287 gcc_unreachable ();
8288 /* Make sure the _vptr is set. */
8289 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true, NULL);
8290 if (c->ts.u.derived == NULL)
8291 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived);
8292 CLASS_DATA (sym)->attr.pointer = 1;
8293 CLASS_DATA (sym)->attr.class_pointer = 1;
8294 gfc_set_sym_referenced (sym->ts.u.derived);
8295 gfc_commit_symbol (sym->ts.u.derived);
8296 /* _vptr now has the _vtab in it, change it to the _vtype. */
8297 if (c->ts.u.derived->attr.vtab)
8298 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
8299 c->ts.u.derived->ns->types_resolved = 0;
8300 resolve_types (c->ts.u.derived->ns);
8304 /* Mark this as an associate variable. */
8305 sym->attr.associate_var = 1;
8307 /* If the target is a good class object, so is the associate variable. */
8308 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
8309 sym->attr.class_ok = 1;
8313 /* Resolve a SELECT TYPE statement. */
8315 static void
8316 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
8318 gfc_symbol *selector_type;
8319 gfc_code *body, *new_st, *if_st, *tail;
8320 gfc_code *class_is = NULL, *default_case = NULL;
8321 gfc_case *c;
8322 gfc_symtree *st;
8323 char name[GFC_MAX_SYMBOL_LEN];
8324 gfc_namespace *ns;
8325 int error = 0;
8326 int charlen = 0;
8328 ns = code->ext.block.ns;
8329 gfc_resolve (ns);
8331 /* Check for F03:C813. */
8332 if (code->expr1->ts.type != BT_CLASS
8333 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
8335 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
8336 "at %L", &code->loc);
8337 return;
8340 if (!code->expr1->symtree->n.sym->attr.class_ok)
8341 return;
8343 if (code->expr2)
8345 if (code->expr1->symtree->n.sym->attr.untyped)
8346 code->expr1->symtree->n.sym->ts = code->expr2->ts;
8347 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
8349 /* F2008: C803 The selector expression must not be coindexed. */
8350 if (gfc_is_coindexed (code->expr2))
8352 gfc_error ("Selector at %L must not be coindexed",
8353 &code->expr2->where);
8354 return;
8358 else
8360 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
8362 if (gfc_is_coindexed (code->expr1))
8364 gfc_error ("Selector at %L must not be coindexed",
8365 &code->expr1->where);
8366 return;
8370 /* Loop over TYPE IS / CLASS IS cases. */
8371 for (body = code->block; body; body = body->block)
8373 c = body->ext.block.case_list;
8375 /* Check F03:C815. */
8376 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8377 && !selector_type->attr.unlimited_polymorphic
8378 && !gfc_type_is_extensible (c->ts.u.derived))
8380 gfc_error ("Derived type %qs at %L must be extensible",
8381 c->ts.u.derived->name, &c->where);
8382 error++;
8383 continue;
8386 /* Check F03:C816. */
8387 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
8388 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
8389 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
8391 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8392 gfc_error ("Derived type %qs at %L must be an extension of %qs",
8393 c->ts.u.derived->name, &c->where, selector_type->name);
8394 else
8395 gfc_error ("Unexpected intrinsic type %qs at %L",
8396 gfc_basic_typename (c->ts.type), &c->where);
8397 error++;
8398 continue;
8401 /* Check F03:C814. */
8402 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
8404 gfc_error ("The type-spec at %L shall specify that each length "
8405 "type parameter is assumed", &c->where);
8406 error++;
8407 continue;
8410 /* Intercept the DEFAULT case. */
8411 if (c->ts.type == BT_UNKNOWN)
8413 /* Check F03:C818. */
8414 if (default_case)
8416 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8417 "by a second DEFAULT CASE at %L",
8418 &default_case->ext.block.case_list->where, &c->where);
8419 error++;
8420 continue;
8423 default_case = body;
8427 if (error > 0)
8428 return;
8430 /* Transform SELECT TYPE statement to BLOCK and associate selector to
8431 target if present. If there are any EXIT statements referring to the
8432 SELECT TYPE construct, this is no problem because the gfc_code
8433 reference stays the same and EXIT is equally possible from the BLOCK
8434 it is changed to. */
8435 code->op = EXEC_BLOCK;
8436 if (code->expr2)
8438 gfc_association_list* assoc;
8440 assoc = gfc_get_association_list ();
8441 assoc->st = code->expr1->symtree;
8442 assoc->target = gfc_copy_expr (code->expr2);
8443 assoc->target->where = code->expr2->where;
8444 /* assoc->variable will be set by resolve_assoc_var. */
8446 code->ext.block.assoc = assoc;
8447 code->expr1->symtree->n.sym->assoc = assoc;
8449 resolve_assoc_var (code->expr1->symtree->n.sym, false);
8451 else
8452 code->ext.block.assoc = NULL;
8454 /* Add EXEC_SELECT to switch on type. */
8455 new_st = gfc_get_code (code->op);
8456 new_st->expr1 = code->expr1;
8457 new_st->expr2 = code->expr2;
8458 new_st->block = code->block;
8459 code->expr1 = code->expr2 = NULL;
8460 code->block = NULL;
8461 if (!ns->code)
8462 ns->code = new_st;
8463 else
8464 ns->code->next = new_st;
8465 code = new_st;
8466 code->op = EXEC_SELECT;
8468 gfc_add_vptr_component (code->expr1);
8469 gfc_add_hash_component (code->expr1);
8471 /* Loop over TYPE IS / CLASS IS cases. */
8472 for (body = code->block; body; body = body->block)
8474 c = body->ext.block.case_list;
8476 if (c->ts.type == BT_DERIVED)
8477 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8478 c->ts.u.derived->hash_value);
8479 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8481 gfc_symbol *ivtab;
8482 gfc_expr *e;
8484 ivtab = gfc_find_vtab (&c->ts);
8485 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
8486 e = CLASS_DATA (ivtab)->initializer;
8487 c->low = c->high = gfc_copy_expr (e);
8490 else if (c->ts.type == BT_UNKNOWN)
8491 continue;
8493 /* Associate temporary to selector. This should only be done
8494 when this case is actually true, so build a new ASSOCIATE
8495 that does precisely this here (instead of using the
8496 'global' one). */
8498 if (c->ts.type == BT_CLASS)
8499 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8500 else if (c->ts.type == BT_DERIVED)
8501 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8502 else if (c->ts.type == BT_CHARACTER)
8504 if (c->ts.u.cl && c->ts.u.cl->length
8505 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8506 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8507 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8508 charlen, c->ts.kind);
8510 else
8511 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8512 c->ts.kind);
8514 st = gfc_find_symtree (ns->sym_root, name);
8515 gcc_assert (st->n.sym->assoc);
8516 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
8517 st->n.sym->assoc->target->where = code->expr1->where;
8518 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8519 gfc_add_data_component (st->n.sym->assoc->target);
8521 new_st = gfc_get_code (EXEC_BLOCK);
8522 new_st->ext.block.ns = gfc_build_block_ns (ns);
8523 new_st->ext.block.ns->code = body->next;
8524 body->next = new_st;
8526 /* Chain in the new list only if it is marked as dangling. Otherwise
8527 there is a CASE label overlap and this is already used. Just ignore,
8528 the error is diagnosed elsewhere. */
8529 if (st->n.sym->assoc->dangling)
8531 new_st->ext.block.assoc = st->n.sym->assoc;
8532 st->n.sym->assoc->dangling = 0;
8535 resolve_assoc_var (st->n.sym, false);
8538 /* Take out CLASS IS cases for separate treatment. */
8539 body = code;
8540 while (body && body->block)
8542 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8544 /* Add to class_is list. */
8545 if (class_is == NULL)
8547 class_is = body->block;
8548 tail = class_is;
8550 else
8552 for (tail = class_is; tail->block; tail = tail->block) ;
8553 tail->block = body->block;
8554 tail = tail->block;
8556 /* Remove from EXEC_SELECT list. */
8557 body->block = body->block->block;
8558 tail->block = NULL;
8560 else
8561 body = body->block;
8564 if (class_is)
8566 gfc_symbol *vtab;
8568 if (!default_case)
8570 /* Add a default case to hold the CLASS IS cases. */
8571 for (tail = code; tail->block; tail = tail->block) ;
8572 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
8573 tail = tail->block;
8574 tail->ext.block.case_list = gfc_get_case ();
8575 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8576 tail->next = NULL;
8577 default_case = tail;
8580 /* More than one CLASS IS block? */
8581 if (class_is->block)
8583 gfc_code **c1,*c2;
8584 bool swapped;
8585 /* Sort CLASS IS blocks by extension level. */
8588 swapped = false;
8589 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8591 c2 = (*c1)->block;
8592 /* F03:C817 (check for doubles). */
8593 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8594 == c2->ext.block.case_list->ts.u.derived->hash_value)
8596 gfc_error ("Double CLASS IS block in SELECT TYPE "
8597 "statement at %L",
8598 &c2->ext.block.case_list->where);
8599 return;
8601 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8602 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8604 /* Swap. */
8605 (*c1)->block = c2->block;
8606 c2->block = *c1;
8607 *c1 = c2;
8608 swapped = true;
8612 while (swapped);
8615 /* Generate IF chain. */
8616 if_st = gfc_get_code (EXEC_IF);
8617 new_st = if_st;
8618 for (body = class_is; body; body = body->block)
8620 new_st->block = gfc_get_code (EXEC_IF);
8621 new_st = new_st->block;
8622 /* Set up IF condition: Call _gfortran_is_extension_of. */
8623 new_st->expr1 = gfc_get_expr ();
8624 new_st->expr1->expr_type = EXPR_FUNCTION;
8625 new_st->expr1->ts.type = BT_LOGICAL;
8626 new_st->expr1->ts.kind = 4;
8627 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8628 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8629 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8630 /* Set up arguments. */
8631 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8632 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8633 new_st->expr1->value.function.actual->expr->where = code->loc;
8634 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8635 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8636 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8637 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8638 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8639 new_st->next = body->next;
8641 if (default_case->next)
8643 new_st->block = gfc_get_code (EXEC_IF);
8644 new_st = new_st->block;
8645 new_st->next = default_case->next;
8648 /* Replace CLASS DEFAULT code by the IF chain. */
8649 default_case->next = if_st;
8652 /* Resolve the internal code. This can not be done earlier because
8653 it requires that the sym->assoc of selectors is set already. */
8654 gfc_current_ns = ns;
8655 gfc_resolve_blocks (code->block, gfc_current_ns);
8656 gfc_current_ns = old_ns;
8658 resolve_select (code, true);
8662 /* Resolve a transfer statement. This is making sure that:
8663 -- a derived type being transferred has only non-pointer components
8664 -- a derived type being transferred doesn't have private components, unless
8665 it's being transferred from the module where the type was defined
8666 -- we're not trying to transfer a whole assumed size array. */
8668 static void
8669 resolve_transfer (gfc_code *code)
8671 gfc_typespec *ts;
8672 gfc_symbol *sym, *derived;
8673 gfc_ref *ref;
8674 gfc_expr *exp;
8675 bool write = false;
8676 bool formatted = false;
8677 gfc_dt *dt = code->ext.dt;
8678 gfc_symbol *dtio_sub = NULL;
8680 exp = code->expr1;
8682 while (exp != NULL && exp->expr_type == EXPR_OP
8683 && exp->value.op.op == INTRINSIC_PARENTHESES)
8684 exp = exp->value.op.op1;
8686 if (exp && exp->expr_type == EXPR_NULL
8687 && code->ext.dt)
8689 gfc_error ("Invalid context for NULL () intrinsic at %L",
8690 &exp->where);
8691 return;
8694 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8695 && exp->expr_type != EXPR_FUNCTION
8696 && exp->expr_type != EXPR_STRUCTURE))
8697 return;
8699 /* If we are reading, the variable will be changed. Note that
8700 code->ext.dt may be NULL if the TRANSFER is related to
8701 an INQUIRE statement -- but in this case, we are not reading, either. */
8702 if (dt && dt->dt_io_kind->value.iokind == M_READ
8703 && !gfc_check_vardef_context (exp, false, false, false,
8704 _("item in READ")))
8705 return;
8707 ts = exp->expr_type == EXPR_STRUCTURE ? &exp->ts : &exp->symtree->n.sym->ts;
8709 /* Go to actual component transferred. */
8710 for (ref = exp->ref; ref; ref = ref->next)
8711 if (ref->type == REF_COMPONENT)
8712 ts = &ref->u.c.component->ts;
8714 if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
8715 && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
8717 if (ts->type == BT_DERIVED)
8718 derived = ts->u.derived;
8719 else
8720 derived = ts->u.derived->components->ts.u.derived;
8722 if (dt->format_expr)
8724 char *fmt;
8725 fmt = gfc_widechar_to_char (dt->format_expr->value.character.string,
8726 -1);
8727 if (strtok (fmt, "DT") != NULL)
8728 formatted = true;
8730 else if (dt->format_label == &format_asterisk)
8732 /* List directed io must call the formatted DTIO procedure. */
8733 formatted = true;
8736 write = dt->dt_io_kind->value.iokind == M_WRITE
8737 || dt->dt_io_kind->value.iokind == M_PRINT;
8738 dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
8740 if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
8742 dt->udtio = exp;
8743 sym = exp->symtree->n.sym->ns->proc_name;
8744 /* Check to see if this is a nested DTIO call, with the
8745 dummy as the io-list object. */
8746 if (sym && sym == dtio_sub && sym->formal
8747 && sym->formal->sym == exp->symtree->n.sym
8748 && exp->ref == NULL)
8750 if (!sym->attr.recursive)
8752 gfc_error ("DTIO %s procedure at %L must be recursive",
8753 sym->name, &sym->declared_at);
8754 return;
8760 if (ts->type == BT_CLASS && dtio_sub == NULL)
8762 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8763 "it is processed by a defined input/output procedure",
8764 &code->loc);
8765 return;
8768 if (ts->type == BT_DERIVED)
8770 /* Check that transferred derived type doesn't contain POINTER
8771 components unless it is processed by a defined input/output
8772 procedure". */
8773 if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
8775 gfc_error ("Data transfer element at %L cannot have POINTER "
8776 "components unless it is processed by a defined "
8777 "input/output procedure", &code->loc);
8778 return;
8781 /* F08:C935. */
8782 if (ts->u.derived->attr.proc_pointer_comp)
8784 gfc_error ("Data transfer element at %L cannot have "
8785 "procedure pointer components", &code->loc);
8786 return;
8789 if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
8791 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8792 "components unless it is processed by a defined "
8793 "input/output procedure", &code->loc);
8794 return;
8797 /* C_PTR and C_FUNPTR have private components which means they can not
8798 be printed. However, if -std=gnu and not -pedantic, allow
8799 the component to be printed to help debugging. */
8800 if (ts->u.derived->ts.f90_type == BT_VOID)
8802 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8803 "cannot have PRIVATE components", &code->loc))
8804 return;
8806 else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
8808 gfc_error ("Data transfer element at %L cannot have "
8809 "PRIVATE components unless it is processed by "
8810 "a defined input/output procedure", &code->loc);
8811 return;
8815 if (exp->expr_type == EXPR_STRUCTURE)
8816 return;
8818 sym = exp->symtree->n.sym;
8820 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8821 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8823 gfc_error ("Data transfer element at %L cannot be a full reference to "
8824 "an assumed-size array", &code->loc);
8825 return;
8830 /*********** Toplevel code resolution subroutines ***********/
8832 /* Find the set of labels that are reachable from this block. We also
8833 record the last statement in each block. */
8835 static void
8836 find_reachable_labels (gfc_code *block)
8838 gfc_code *c;
8840 if (!block)
8841 return;
8843 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8845 /* Collect labels in this block. We don't keep those corresponding
8846 to END {IF|SELECT}, these are checked in resolve_branch by going
8847 up through the code_stack. */
8848 for (c = block; c; c = c->next)
8850 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8851 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8854 /* Merge with labels from parent block. */
8855 if (cs_base->prev)
8857 gcc_assert (cs_base->prev->reachable_labels);
8858 bitmap_ior_into (cs_base->reachable_labels,
8859 cs_base->prev->reachable_labels);
8864 static void
8865 resolve_lock_unlock_event (gfc_code *code)
8867 if (code->expr1->expr_type == EXPR_FUNCTION
8868 && code->expr1->value.function.isym
8869 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
8870 remove_caf_get_intrinsic (code->expr1);
8872 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
8873 && (code->expr1->ts.type != BT_DERIVED
8874 || code->expr1->expr_type != EXPR_VARIABLE
8875 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8876 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8877 || code->expr1->rank != 0
8878 || (!gfc_is_coarray (code->expr1) &&
8879 !gfc_is_coindexed (code->expr1))))
8880 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8881 &code->expr1->where);
8882 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
8883 && (code->expr1->ts.type != BT_DERIVED
8884 || code->expr1->expr_type != EXPR_VARIABLE
8885 || code->expr1->ts.u.derived->from_intmod
8886 != INTMOD_ISO_FORTRAN_ENV
8887 || code->expr1->ts.u.derived->intmod_sym_id
8888 != ISOFORTRAN_EVENT_TYPE
8889 || code->expr1->rank != 0))
8890 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
8891 &code->expr1->where);
8892 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
8893 && !gfc_is_coindexed (code->expr1))
8894 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
8895 &code->expr1->where);
8896 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
8897 gfc_error ("Event variable argument at %L must be a coarray but not "
8898 "coindexed", &code->expr1->where);
8900 /* Check STAT. */
8901 if (code->expr2
8902 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8903 || code->expr2->expr_type != EXPR_VARIABLE))
8904 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8905 &code->expr2->where);
8907 if (code->expr2
8908 && !gfc_check_vardef_context (code->expr2, false, false, false,
8909 _("STAT variable")))
8910 return;
8912 /* Check ERRMSG. */
8913 if (code->expr3
8914 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8915 || code->expr3->expr_type != EXPR_VARIABLE))
8916 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8917 &code->expr3->where);
8919 if (code->expr3
8920 && !gfc_check_vardef_context (code->expr3, false, false, false,
8921 _("ERRMSG variable")))
8922 return;
8924 /* Check for LOCK the ACQUIRED_LOCK. */
8925 if (code->op != EXEC_EVENT_WAIT && code->expr4
8926 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8927 || code->expr4->expr_type != EXPR_VARIABLE))
8928 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8929 "variable", &code->expr4->where);
8931 if (code->op != EXEC_EVENT_WAIT && code->expr4
8932 && !gfc_check_vardef_context (code->expr4, false, false, false,
8933 _("ACQUIRED_LOCK variable")))
8934 return;
8936 /* Check for EVENT WAIT the UNTIL_COUNT. */
8937 if (code->op == EXEC_EVENT_WAIT && code->expr4
8938 && (code->expr4->ts.type != BT_INTEGER || code->expr4->rank != 0))
8939 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
8940 "expression", &code->expr4->where);
8944 static void
8945 resolve_critical (gfc_code *code)
8947 gfc_symtree *symtree;
8948 gfc_symbol *lock_type;
8949 char name[GFC_MAX_SYMBOL_LEN];
8950 static int serial = 0;
8952 if (flag_coarray != GFC_FCOARRAY_LIB)
8953 return;
8955 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
8956 GFC_PREFIX ("lock_type"));
8957 if (symtree)
8958 lock_type = symtree->n.sym;
8959 else
8961 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
8962 false) != 0)
8963 gcc_unreachable ();
8964 lock_type = symtree->n.sym;
8965 lock_type->attr.flavor = FL_DERIVED;
8966 lock_type->attr.zero_comp = 1;
8967 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
8968 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
8971 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
8972 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
8973 gcc_unreachable ();
8975 code->resolved_sym = symtree->n.sym;
8976 symtree->n.sym->attr.flavor = FL_VARIABLE;
8977 symtree->n.sym->attr.referenced = 1;
8978 symtree->n.sym->attr.artificial = 1;
8979 symtree->n.sym->attr.codimension = 1;
8980 symtree->n.sym->ts.type = BT_DERIVED;
8981 symtree->n.sym->ts.u.derived = lock_type;
8982 symtree->n.sym->as = gfc_get_array_spec ();
8983 symtree->n.sym->as->corank = 1;
8984 symtree->n.sym->as->type = AS_EXPLICIT;
8985 symtree->n.sym->as->cotype = AS_EXPLICIT;
8986 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
8987 NULL, 1);
8988 gfc_commit_symbols();
8992 static void
8993 resolve_sync (gfc_code *code)
8995 /* Check imageset. The * case matches expr1 == NULL. */
8996 if (code->expr1)
8998 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8999 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
9000 "INTEGER expression", &code->expr1->where);
9001 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
9002 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
9003 gfc_error ("Imageset argument at %L must between 1 and num_images()",
9004 &code->expr1->where);
9005 else if (code->expr1->expr_type == EXPR_ARRAY
9006 && gfc_simplify_expr (code->expr1, 0))
9008 gfc_constructor *cons;
9009 cons = gfc_constructor_first (code->expr1->value.constructor);
9010 for (; cons; cons = gfc_constructor_next (cons))
9011 if (cons->expr->expr_type == EXPR_CONSTANT
9012 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
9013 gfc_error ("Imageset argument at %L must between 1 and "
9014 "num_images()", &cons->expr->where);
9018 /* Check STAT. */
9019 if (code->expr2
9020 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
9021 || code->expr2->expr_type != EXPR_VARIABLE))
9022 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
9023 &code->expr2->where);
9025 /* Check ERRMSG. */
9026 if (code->expr3
9027 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
9028 || code->expr3->expr_type != EXPR_VARIABLE))
9029 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
9030 &code->expr3->where);
9034 /* Given a branch to a label, see if the branch is conforming.
9035 The code node describes where the branch is located. */
9037 static void
9038 resolve_branch (gfc_st_label *label, gfc_code *code)
9040 code_stack *stack;
9042 if (label == NULL)
9043 return;
9045 /* Step one: is this a valid branching target? */
9047 if (label->defined == ST_LABEL_UNKNOWN)
9049 gfc_error ("Label %d referenced at %L is never defined", label->value,
9050 &code->loc);
9051 return;
9054 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
9056 gfc_error ("Statement at %L is not a valid branch target statement "
9057 "for the branch statement at %L", &label->where, &code->loc);
9058 return;
9061 /* Step two: make sure this branch is not a branch to itself ;-) */
9063 if (code->here == label)
9065 gfc_warning (0,
9066 "Branch at %L may result in an infinite loop", &code->loc);
9067 return;
9070 /* Step three: See if the label is in the same block as the
9071 branching statement. The hard work has been done by setting up
9072 the bitmap reachable_labels. */
9074 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
9076 /* Check now whether there is a CRITICAL construct; if so, check
9077 whether the label is still visible outside of the CRITICAL block,
9078 which is invalid. */
9079 for (stack = cs_base; stack; stack = stack->prev)
9081 if (stack->current->op == EXEC_CRITICAL
9082 && bitmap_bit_p (stack->reachable_labels, label->value))
9083 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
9084 "label at %L", &code->loc, &label->where);
9085 else if (stack->current->op == EXEC_DO_CONCURRENT
9086 && bitmap_bit_p (stack->reachable_labels, label->value))
9087 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
9088 "for label at %L", &code->loc, &label->where);
9091 return;
9094 /* Step four: If we haven't found the label in the bitmap, it may
9095 still be the label of the END of the enclosing block, in which
9096 case we find it by going up the code_stack. */
9098 for (stack = cs_base; stack; stack = stack->prev)
9100 if (stack->current->next && stack->current->next->here == label)
9101 break;
9102 if (stack->current->op == EXEC_CRITICAL)
9104 /* Note: A label at END CRITICAL does not leave the CRITICAL
9105 construct as END CRITICAL is still part of it. */
9106 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
9107 " at %L", &code->loc, &label->where);
9108 return;
9110 else if (stack->current->op == EXEC_DO_CONCURRENT)
9112 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
9113 "label at %L", &code->loc, &label->where);
9114 return;
9118 if (stack)
9120 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
9121 return;
9124 /* The label is not in an enclosing block, so illegal. This was
9125 allowed in Fortran 66, so we allow it as extension. No
9126 further checks are necessary in this case. */
9127 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
9128 "as the GOTO statement at %L", &label->where,
9129 &code->loc);
9130 return;
9134 /* Check whether EXPR1 has the same shape as EXPR2. */
9136 static bool
9137 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
9139 mpz_t shape[GFC_MAX_DIMENSIONS];
9140 mpz_t shape2[GFC_MAX_DIMENSIONS];
9141 bool result = false;
9142 int i;
9144 /* Compare the rank. */
9145 if (expr1->rank != expr2->rank)
9146 return result;
9148 /* Compare the size of each dimension. */
9149 for (i=0; i<expr1->rank; i++)
9151 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
9152 goto ignore;
9154 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
9155 goto ignore;
9157 if (mpz_cmp (shape[i], shape2[i]))
9158 goto over;
9161 /* When either of the two expression is an assumed size array, we
9162 ignore the comparison of dimension sizes. */
9163 ignore:
9164 result = true;
9166 over:
9167 gfc_clear_shape (shape, i);
9168 gfc_clear_shape (shape2, i);
9169 return result;
9173 /* Check whether a WHERE assignment target or a WHERE mask expression
9174 has the same shape as the outmost WHERE mask expression. */
9176 static void
9177 resolve_where (gfc_code *code, gfc_expr *mask)
9179 gfc_code *cblock;
9180 gfc_code *cnext;
9181 gfc_expr *e = NULL;
9183 cblock = code->block;
9185 /* Store the first WHERE mask-expr of the WHERE statement or construct.
9186 In case of nested WHERE, only the outmost one is stored. */
9187 if (mask == NULL) /* outmost WHERE */
9188 e = cblock->expr1;
9189 else /* inner WHERE */
9190 e = mask;
9192 while (cblock)
9194 if (cblock->expr1)
9196 /* Check if the mask-expr has a consistent shape with the
9197 outmost WHERE mask-expr. */
9198 if (!resolve_where_shape (cblock->expr1, e))
9199 gfc_error ("WHERE mask at %L has inconsistent shape",
9200 &cblock->expr1->where);
9203 /* the assignment statement of a WHERE statement, or the first
9204 statement in where-body-construct of a WHERE construct */
9205 cnext = cblock->next;
9206 while (cnext)
9208 switch (cnext->op)
9210 /* WHERE assignment statement */
9211 case EXEC_ASSIGN:
9213 /* Check shape consistent for WHERE assignment target. */
9214 if (e && !resolve_where_shape (cnext->expr1, e))
9215 gfc_error ("WHERE assignment target at %L has "
9216 "inconsistent shape", &cnext->expr1->where);
9217 break;
9220 case EXEC_ASSIGN_CALL:
9221 resolve_call (cnext);
9222 if (!cnext->resolved_sym->attr.elemental)
9223 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
9224 &cnext->ext.actual->expr->where);
9225 break;
9227 /* WHERE or WHERE construct is part of a where-body-construct */
9228 case EXEC_WHERE:
9229 resolve_where (cnext, e);
9230 break;
9232 default:
9233 gfc_error ("Unsupported statement inside WHERE at %L",
9234 &cnext->loc);
9236 /* the next statement within the same where-body-construct */
9237 cnext = cnext->next;
9239 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
9240 cblock = cblock->block;
9245 /* Resolve assignment in FORALL construct.
9246 NVAR is the number of FORALL index variables, and VAR_EXPR records the
9247 FORALL index variables. */
9249 static void
9250 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
9252 int n;
9254 for (n = 0; n < nvar; n++)
9256 gfc_symbol *forall_index;
9258 forall_index = var_expr[n]->symtree->n.sym;
9260 /* Check whether the assignment target is one of the FORALL index
9261 variable. */
9262 if ((code->expr1->expr_type == EXPR_VARIABLE)
9263 && (code->expr1->symtree->n.sym == forall_index))
9264 gfc_error ("Assignment to a FORALL index variable at %L",
9265 &code->expr1->where);
9266 else
9268 /* If one of the FORALL index variables doesn't appear in the
9269 assignment variable, then there could be a many-to-one
9270 assignment. Emit a warning rather than an error because the
9271 mask could be resolving this problem. */
9272 if (!find_forall_index (code->expr1, forall_index, 0))
9273 gfc_warning (0, "The FORALL with index %qs is not used on the "
9274 "left side of the assignment at %L and so might "
9275 "cause multiple assignment to this object",
9276 var_expr[n]->symtree->name, &code->expr1->where);
9282 /* Resolve WHERE statement in FORALL construct. */
9284 static void
9285 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
9286 gfc_expr **var_expr)
9288 gfc_code *cblock;
9289 gfc_code *cnext;
9291 cblock = code->block;
9292 while (cblock)
9294 /* the assignment statement of a WHERE statement, or the first
9295 statement in where-body-construct of a WHERE construct */
9296 cnext = cblock->next;
9297 while (cnext)
9299 switch (cnext->op)
9301 /* WHERE assignment statement */
9302 case EXEC_ASSIGN:
9303 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
9304 break;
9306 /* WHERE operator assignment statement */
9307 case EXEC_ASSIGN_CALL:
9308 resolve_call (cnext);
9309 if (!cnext->resolved_sym->attr.elemental)
9310 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
9311 &cnext->ext.actual->expr->where);
9312 break;
9314 /* WHERE or WHERE construct is part of a where-body-construct */
9315 case EXEC_WHERE:
9316 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
9317 break;
9319 default:
9320 gfc_error ("Unsupported statement inside WHERE at %L",
9321 &cnext->loc);
9323 /* the next statement within the same where-body-construct */
9324 cnext = cnext->next;
9326 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
9327 cblock = cblock->block;
9332 /* Traverse the FORALL body to check whether the following errors exist:
9333 1. For assignment, check if a many-to-one assignment happens.
9334 2. For WHERE statement, check the WHERE body to see if there is any
9335 many-to-one assignment. */
9337 static void
9338 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
9340 gfc_code *c;
9342 c = code->block->next;
9343 while (c)
9345 switch (c->op)
9347 case EXEC_ASSIGN:
9348 case EXEC_POINTER_ASSIGN:
9349 gfc_resolve_assign_in_forall (c, nvar, var_expr);
9350 break;
9352 case EXEC_ASSIGN_CALL:
9353 resolve_call (c);
9354 break;
9356 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
9357 there is no need to handle it here. */
9358 case EXEC_FORALL:
9359 break;
9360 case EXEC_WHERE:
9361 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
9362 break;
9363 default:
9364 break;
9366 /* The next statement in the FORALL body. */
9367 c = c->next;
9372 /* Counts the number of iterators needed inside a forall construct, including
9373 nested forall constructs. This is used to allocate the needed memory
9374 in gfc_resolve_forall. */
9376 static int
9377 gfc_count_forall_iterators (gfc_code *code)
9379 int max_iters, sub_iters, current_iters;
9380 gfc_forall_iterator *fa;
9382 gcc_assert(code->op == EXEC_FORALL);
9383 max_iters = 0;
9384 current_iters = 0;
9386 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9387 current_iters ++;
9389 code = code->block->next;
9391 while (code)
9393 if (code->op == EXEC_FORALL)
9395 sub_iters = gfc_count_forall_iterators (code);
9396 if (sub_iters > max_iters)
9397 max_iters = sub_iters;
9399 code = code->next;
9402 return current_iters + max_iters;
9406 /* Given a FORALL construct, first resolve the FORALL iterator, then call
9407 gfc_resolve_forall_body to resolve the FORALL body. */
9409 static void
9410 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
9412 static gfc_expr **var_expr;
9413 static int total_var = 0;
9414 static int nvar = 0;
9415 int old_nvar, tmp;
9416 gfc_forall_iterator *fa;
9417 int i;
9419 old_nvar = nvar;
9421 /* Start to resolve a FORALL construct */
9422 if (forall_save == 0)
9424 /* Count the total number of FORALL index in the nested FORALL
9425 construct in order to allocate the VAR_EXPR with proper size. */
9426 total_var = gfc_count_forall_iterators (code);
9428 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
9429 var_expr = XCNEWVEC (gfc_expr *, total_var);
9432 /* The information about FORALL iterator, including FORALL index start, end
9433 and stride. The FORALL index can not appear in start, end or stride. */
9434 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9436 /* Check if any outer FORALL index name is the same as the current
9437 one. */
9438 for (i = 0; i < nvar; i++)
9440 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
9442 gfc_error ("An outer FORALL construct already has an index "
9443 "with this name %L", &fa->var->where);
9447 /* Record the current FORALL index. */
9448 var_expr[nvar] = gfc_copy_expr (fa->var);
9450 nvar++;
9452 /* No memory leak. */
9453 gcc_assert (nvar <= total_var);
9456 /* Resolve the FORALL body. */
9457 gfc_resolve_forall_body (code, nvar, var_expr);
9459 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
9460 gfc_resolve_blocks (code->block, ns);
9462 tmp = nvar;
9463 nvar = old_nvar;
9464 /* Free only the VAR_EXPRs allocated in this frame. */
9465 for (i = nvar; i < tmp; i++)
9466 gfc_free_expr (var_expr[i]);
9468 if (nvar == 0)
9470 /* We are in the outermost FORALL construct. */
9471 gcc_assert (forall_save == 0);
9473 /* VAR_EXPR is not needed any more. */
9474 free (var_expr);
9475 total_var = 0;
9480 /* Resolve a BLOCK construct statement. */
9482 static void
9483 resolve_block_construct (gfc_code* code)
9485 /* Resolve the BLOCK's namespace. */
9486 gfc_resolve (code->ext.block.ns);
9488 /* For an ASSOCIATE block, the associations (and their targets) are already
9489 resolved during resolve_symbol. */
9493 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
9494 DO code nodes. */
9496 void
9497 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
9499 bool t;
9501 for (; b; b = b->block)
9503 t = gfc_resolve_expr (b->expr1);
9504 if (!gfc_resolve_expr (b->expr2))
9505 t = false;
9507 switch (b->op)
9509 case EXEC_IF:
9510 if (t && b->expr1 != NULL
9511 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
9512 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9513 &b->expr1->where);
9514 break;
9516 case EXEC_WHERE:
9517 if (t
9518 && b->expr1 != NULL
9519 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
9520 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
9521 &b->expr1->where);
9522 break;
9524 case EXEC_GOTO:
9525 resolve_branch (b->label1, b);
9526 break;
9528 case EXEC_BLOCK:
9529 resolve_block_construct (b);
9530 break;
9532 case EXEC_SELECT:
9533 case EXEC_SELECT_TYPE:
9534 case EXEC_FORALL:
9535 case EXEC_DO:
9536 case EXEC_DO_WHILE:
9537 case EXEC_DO_CONCURRENT:
9538 case EXEC_CRITICAL:
9539 case EXEC_READ:
9540 case EXEC_WRITE:
9541 case EXEC_IOLENGTH:
9542 case EXEC_WAIT:
9543 break;
9545 case EXEC_OMP_ATOMIC:
9546 case EXEC_OACC_ATOMIC:
9548 gfc_omp_atomic_op aop
9549 = (gfc_omp_atomic_op) (b->ext.omp_atomic & GFC_OMP_ATOMIC_MASK);
9551 /* Verify this before calling gfc_resolve_code, which might
9552 change it. */
9553 gcc_assert (b->next && b->next->op == EXEC_ASSIGN);
9554 gcc_assert (((aop != GFC_OMP_ATOMIC_CAPTURE)
9555 && b->next->next == NULL)
9556 || ((aop == GFC_OMP_ATOMIC_CAPTURE)
9557 && b->next->next != NULL
9558 && b->next->next->op == EXEC_ASSIGN
9559 && b->next->next->next == NULL));
9561 break;
9563 case EXEC_OACC_PARALLEL_LOOP:
9564 case EXEC_OACC_PARALLEL:
9565 case EXEC_OACC_KERNELS_LOOP:
9566 case EXEC_OACC_KERNELS:
9567 case EXEC_OACC_DATA:
9568 case EXEC_OACC_HOST_DATA:
9569 case EXEC_OACC_LOOP:
9570 case EXEC_OACC_UPDATE:
9571 case EXEC_OACC_WAIT:
9572 case EXEC_OACC_CACHE:
9573 case EXEC_OACC_ENTER_DATA:
9574 case EXEC_OACC_EXIT_DATA:
9575 case EXEC_OACC_ROUTINE:
9576 case EXEC_OMP_CRITICAL:
9577 case EXEC_OMP_DISTRIBUTE:
9578 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
9579 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
9580 case EXEC_OMP_DISTRIBUTE_SIMD:
9581 case EXEC_OMP_DO:
9582 case EXEC_OMP_DO_SIMD:
9583 case EXEC_OMP_MASTER:
9584 case EXEC_OMP_ORDERED:
9585 case EXEC_OMP_PARALLEL:
9586 case EXEC_OMP_PARALLEL_DO:
9587 case EXEC_OMP_PARALLEL_DO_SIMD:
9588 case EXEC_OMP_PARALLEL_SECTIONS:
9589 case EXEC_OMP_PARALLEL_WORKSHARE:
9590 case EXEC_OMP_SECTIONS:
9591 case EXEC_OMP_SIMD:
9592 case EXEC_OMP_SINGLE:
9593 case EXEC_OMP_TARGET:
9594 case EXEC_OMP_TARGET_DATA:
9595 case EXEC_OMP_TARGET_TEAMS:
9596 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
9597 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
9598 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9599 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
9600 case EXEC_OMP_TARGET_UPDATE:
9601 case EXEC_OMP_TASK:
9602 case EXEC_OMP_TASKGROUP:
9603 case EXEC_OMP_TASKWAIT:
9604 case EXEC_OMP_TASKYIELD:
9605 case EXEC_OMP_TEAMS:
9606 case EXEC_OMP_TEAMS_DISTRIBUTE:
9607 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
9608 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9609 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
9610 case EXEC_OMP_WORKSHARE:
9611 break;
9613 default:
9614 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
9617 gfc_resolve_code (b->next, ns);
9622 /* Does everything to resolve an ordinary assignment. Returns true
9623 if this is an interface assignment. */
9624 static bool
9625 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9627 bool rval = false;
9628 gfc_expr *lhs;
9629 gfc_expr *rhs;
9630 int llen = 0;
9631 int rlen = 0;
9632 int n;
9633 gfc_ref *ref;
9634 symbol_attribute attr;
9636 if (gfc_extend_assign (code, ns))
9638 gfc_expr** rhsptr;
9640 if (code->op == EXEC_ASSIGN_CALL)
9642 lhs = code->ext.actual->expr;
9643 rhsptr = &code->ext.actual->next->expr;
9645 else
9647 gfc_actual_arglist* args;
9648 gfc_typebound_proc* tbp;
9650 gcc_assert (code->op == EXEC_COMPCALL);
9652 args = code->expr1->value.compcall.actual;
9653 lhs = args->expr;
9654 rhsptr = &args->next->expr;
9656 tbp = code->expr1->value.compcall.tbp;
9657 gcc_assert (!tbp->is_generic);
9660 /* Make a temporary rhs when there is a default initializer
9661 and rhs is the same symbol as the lhs. */
9662 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9663 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9664 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9665 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9666 *rhsptr = gfc_get_parentheses (*rhsptr);
9668 return true;
9671 lhs = code->expr1;
9672 rhs = code->expr2;
9674 if (rhs->is_boz
9675 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9676 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9677 &code->loc))
9678 return false;
9680 /* Handle the case of a BOZ literal on the RHS. */
9681 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9683 int rc;
9684 if (warn_surprising)
9685 gfc_warning (OPT_Wsurprising,
9686 "BOZ literal at %L is bitwise transferred "
9687 "non-integer symbol %qs", &code->loc,
9688 lhs->symtree->n.sym->name);
9690 if (!gfc_convert_boz (rhs, &lhs->ts))
9691 return false;
9692 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9694 if (rc == ARITH_UNDERFLOW)
9695 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9696 ". This check can be disabled with the option "
9697 "%<-fno-range-check%>", &rhs->where);
9698 else if (rc == ARITH_OVERFLOW)
9699 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9700 ". This check can be disabled with the option "
9701 "%<-fno-range-check%>", &rhs->where);
9702 else if (rc == ARITH_NAN)
9703 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9704 ". This check can be disabled with the option "
9705 "%<-fno-range-check%>", &rhs->where);
9706 return false;
9710 if (lhs->ts.type == BT_CHARACTER
9711 && warn_character_truncation)
9713 if (lhs->ts.u.cl != NULL
9714 && lhs->ts.u.cl->length != NULL
9715 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9716 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9718 if (rhs->expr_type == EXPR_CONSTANT)
9719 rlen = rhs->value.character.length;
9721 else if (rhs->ts.u.cl != NULL
9722 && rhs->ts.u.cl->length != NULL
9723 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9724 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9726 if (rlen && llen && rlen > llen)
9727 gfc_warning_now (OPT_Wcharacter_truncation,
9728 "CHARACTER expression will be truncated "
9729 "in assignment (%d/%d) at %L",
9730 llen, rlen, &code->loc);
9733 /* Ensure that a vector index expression for the lvalue is evaluated
9734 to a temporary if the lvalue symbol is referenced in it. */
9735 if (lhs->rank)
9737 for (ref = lhs->ref; ref; ref= ref->next)
9738 if (ref->type == REF_ARRAY)
9740 for (n = 0; n < ref->u.ar.dimen; n++)
9741 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9742 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9743 ref->u.ar.start[n]))
9744 ref->u.ar.start[n]
9745 = gfc_get_parentheses (ref->u.ar.start[n]);
9749 if (gfc_pure (NULL))
9751 if (lhs->ts.type == BT_DERIVED
9752 && lhs->expr_type == EXPR_VARIABLE
9753 && lhs->ts.u.derived->attr.pointer_comp
9754 && rhs->expr_type == EXPR_VARIABLE
9755 && (gfc_impure_variable (rhs->symtree->n.sym)
9756 || gfc_is_coindexed (rhs)))
9758 /* F2008, C1283. */
9759 if (gfc_is_coindexed (rhs))
9760 gfc_error ("Coindexed expression at %L is assigned to "
9761 "a derived type variable with a POINTER "
9762 "component in a PURE procedure",
9763 &rhs->where);
9764 else
9765 gfc_error ("The impure variable at %L is assigned to "
9766 "a derived type variable with a POINTER "
9767 "component in a PURE procedure (12.6)",
9768 &rhs->where);
9769 return rval;
9772 /* Fortran 2008, C1283. */
9773 if (gfc_is_coindexed (lhs))
9775 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9776 "procedure", &rhs->where);
9777 return rval;
9781 if (gfc_implicit_pure (NULL))
9783 if (lhs->expr_type == EXPR_VARIABLE
9784 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9785 && lhs->symtree->n.sym->ns != gfc_current_ns)
9786 gfc_unset_implicit_pure (NULL);
9788 if (lhs->ts.type == BT_DERIVED
9789 && lhs->expr_type == EXPR_VARIABLE
9790 && lhs->ts.u.derived->attr.pointer_comp
9791 && rhs->expr_type == EXPR_VARIABLE
9792 && (gfc_impure_variable (rhs->symtree->n.sym)
9793 || gfc_is_coindexed (rhs)))
9794 gfc_unset_implicit_pure (NULL);
9796 /* Fortran 2008, C1283. */
9797 if (gfc_is_coindexed (lhs))
9798 gfc_unset_implicit_pure (NULL);
9801 /* F2008, 7.2.1.2. */
9802 attr = gfc_expr_attr (lhs);
9803 if (lhs->ts.type == BT_CLASS && attr.allocatable)
9805 if (attr.codimension)
9807 gfc_error ("Assignment to polymorphic coarray at %L is not "
9808 "permitted", &lhs->where);
9809 return false;
9811 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
9812 "polymorphic variable at %L", &lhs->where))
9813 return false;
9814 if (!flag_realloc_lhs)
9816 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9817 "requires %<-frealloc-lhs%>", &lhs->where);
9818 return false;
9820 /* See PR 43366. */
9821 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9822 "is not yet supported", &lhs->where);
9823 return false;
9825 else if (lhs->ts.type == BT_CLASS)
9827 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
9828 "assignment at %L - check that there is a matching specific "
9829 "subroutine for '=' operator", &lhs->where);
9830 return false;
9833 bool lhs_coindexed = gfc_is_coindexed (lhs);
9835 /* F2008, Section 7.2.1.2. */
9836 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
9838 gfc_error ("Coindexed variable must not have an allocatable ultimate "
9839 "component in assignment at %L", &lhs->where);
9840 return false;
9843 /* Assign the 'data' of a class object to a derived type. */
9844 if (lhs->ts.type == BT_DERIVED
9845 && rhs->ts.type == BT_CLASS)
9846 gfc_add_data_component (rhs);
9848 bool caf_convert_to_send = flag_coarray == GFC_FCOARRAY_LIB
9849 && (lhs_coindexed
9850 || (code->expr2->expr_type == EXPR_FUNCTION
9851 && code->expr2->value.function.isym
9852 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
9853 && (code->expr1->rank == 0 || code->expr2->rank != 0)
9854 && !gfc_expr_attr (rhs).allocatable
9855 && !gfc_has_vector_subscript (rhs)));
9857 gfc_check_assign (lhs, rhs, 1, !caf_convert_to_send);
9859 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
9860 Additionally, insert this code when the RHS is a CAF as we then use the
9861 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
9862 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
9863 noncoindexed array and the RHS is a coindexed scalar, use the normal code
9864 path. */
9865 if (caf_convert_to_send)
9867 if (code->expr2->expr_type == EXPR_FUNCTION
9868 && code->expr2->value.function.isym
9869 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
9870 remove_caf_get_intrinsic (code->expr2);
9871 code->op = EXEC_CALL;
9872 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
9873 code->resolved_sym = code->symtree->n.sym;
9874 code->resolved_sym->attr.flavor = FL_PROCEDURE;
9875 code->resolved_sym->attr.intrinsic = 1;
9876 code->resolved_sym->attr.subroutine = 1;
9877 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
9878 gfc_commit_symbol (code->resolved_sym);
9879 code->ext.actual = gfc_get_actual_arglist ();
9880 code->ext.actual->expr = lhs;
9881 code->ext.actual->next = gfc_get_actual_arglist ();
9882 code->ext.actual->next->expr = rhs;
9883 code->expr1 = NULL;
9884 code->expr2 = NULL;
9887 return false;
9891 /* Add a component reference onto an expression. */
9893 static void
9894 add_comp_ref (gfc_expr *e, gfc_component *c)
9896 gfc_ref **ref;
9897 ref = &(e->ref);
9898 while (*ref)
9899 ref = &((*ref)->next);
9900 *ref = gfc_get_ref ();
9901 (*ref)->type = REF_COMPONENT;
9902 (*ref)->u.c.sym = e->ts.u.derived;
9903 (*ref)->u.c.component = c;
9904 e->ts = c->ts;
9906 /* Add a full array ref, as necessary. */
9907 if (c->as)
9909 gfc_add_full_array_ref (e, c->as);
9910 e->rank = c->as->rank;
9915 /* Build an assignment. Keep the argument 'op' for future use, so that
9916 pointer assignments can be made. */
9918 static gfc_code *
9919 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9920 gfc_component *comp1, gfc_component *comp2, locus loc)
9922 gfc_code *this_code;
9924 this_code = gfc_get_code (op);
9925 this_code->next = NULL;
9926 this_code->expr1 = gfc_copy_expr (expr1);
9927 this_code->expr2 = gfc_copy_expr (expr2);
9928 this_code->loc = loc;
9929 if (comp1 && comp2)
9931 add_comp_ref (this_code->expr1, comp1);
9932 add_comp_ref (this_code->expr2, comp2);
9935 return this_code;
9939 /* Makes a temporary variable expression based on the characteristics of
9940 a given variable expression. */
9942 static gfc_expr*
9943 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9945 static int serial = 0;
9946 char name[GFC_MAX_SYMBOL_LEN];
9947 gfc_symtree *tmp;
9948 gfc_array_spec *as;
9949 gfc_array_ref *aref;
9950 gfc_ref *ref;
9952 sprintf (name, GFC_PREFIX("DA%d"), serial++);
9953 gfc_get_sym_tree (name, ns, &tmp, false);
9954 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9956 as = NULL;
9957 ref = NULL;
9958 aref = NULL;
9960 /* Obtain the arrayspec for the temporary. */
9961 if (e->rank && e->expr_type != EXPR_ARRAY
9962 && e->expr_type != EXPR_FUNCTION
9963 && e->expr_type != EXPR_OP)
9965 aref = gfc_find_array_ref (e);
9966 if (e->expr_type == EXPR_VARIABLE
9967 && e->symtree->n.sym->as == aref->as)
9968 as = aref->as;
9969 else
9971 for (ref = e->ref; ref; ref = ref->next)
9972 if (ref->type == REF_COMPONENT
9973 && ref->u.c.component->as == aref->as)
9975 as = aref->as;
9976 break;
9981 /* Add the attributes and the arrayspec to the temporary. */
9982 tmp->n.sym->attr = gfc_expr_attr (e);
9983 tmp->n.sym->attr.function = 0;
9984 tmp->n.sym->attr.result = 0;
9985 tmp->n.sym->attr.flavor = FL_VARIABLE;
9987 if (as)
9989 tmp->n.sym->as = gfc_copy_array_spec (as);
9990 if (!ref)
9991 ref = e->ref;
9992 if (as->type == AS_DEFERRED)
9993 tmp->n.sym->attr.allocatable = 1;
9995 else if (e->rank && (e->expr_type == EXPR_ARRAY
9996 || e->expr_type == EXPR_FUNCTION
9997 || e->expr_type == EXPR_OP))
9999 tmp->n.sym->as = gfc_get_array_spec ();
10000 tmp->n.sym->as->type = AS_DEFERRED;
10001 tmp->n.sym->as->rank = e->rank;
10002 tmp->n.sym->attr.allocatable = 1;
10003 tmp->n.sym->attr.dimension = 1;
10005 else
10006 tmp->n.sym->attr.dimension = 0;
10008 gfc_set_sym_referenced (tmp->n.sym);
10009 gfc_commit_symbol (tmp->n.sym);
10010 e = gfc_lval_expr_from_sym (tmp->n.sym);
10012 /* Should the lhs be a section, use its array ref for the
10013 temporary expression. */
10014 if (aref && aref->type != AR_FULL)
10016 gfc_free_ref_list (e->ref);
10017 e->ref = gfc_copy_ref (ref);
10019 return e;
10023 /* Add one line of code to the code chain, making sure that 'head' and
10024 'tail' are appropriately updated. */
10026 static void
10027 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
10029 gcc_assert (this_code);
10030 if (*head == NULL)
10031 *head = *tail = *this_code;
10032 else
10033 *tail = gfc_append_code (*tail, *this_code);
10034 *this_code = NULL;
10038 /* Counts the potential number of part array references that would
10039 result from resolution of typebound defined assignments. */
10041 static int
10042 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
10044 gfc_component *c;
10045 int c_depth = 0, t_depth;
10047 for (c= derived->components; c; c = c->next)
10049 if ((!gfc_bt_struct (c->ts.type)
10050 || c->attr.pointer
10051 || c->attr.allocatable
10052 || c->attr.proc_pointer_comp
10053 || c->attr.class_pointer
10054 || c->attr.proc_pointer)
10055 && !c->attr.defined_assign_comp)
10056 continue;
10058 if (c->as && c_depth == 0)
10059 c_depth = 1;
10061 if (c->ts.u.derived->attr.defined_assign_comp)
10062 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
10063 c->as ? 1 : 0);
10064 else
10065 t_depth = 0;
10067 c_depth = t_depth > c_depth ? t_depth : c_depth;
10069 return depth + c_depth;
10073 /* Implement 7.2.1.3 of the F08 standard:
10074 "An intrinsic assignment where the variable is of derived type is
10075 performed as if each component of the variable were assigned from the
10076 corresponding component of expr using pointer assignment (7.2.2) for
10077 each pointer component, defined assignment for each nonpointer
10078 nonallocatable component of a type that has a type-bound defined
10079 assignment consistent with the component, intrinsic assignment for
10080 each other nonpointer nonallocatable component, ..."
10082 The pointer assignments are taken care of by the intrinsic
10083 assignment of the structure itself. This function recursively adds
10084 defined assignments where required. The recursion is accomplished
10085 by calling gfc_resolve_code.
10087 When the lhs in a defined assignment has intent INOUT, we need a
10088 temporary for the lhs. In pseudo-code:
10090 ! Only call function lhs once.
10091 if (lhs is not a constant or an variable)
10092 temp_x = expr2
10093 expr2 => temp_x
10094 ! Do the intrinsic assignment
10095 expr1 = expr2
10096 ! Now do the defined assignments
10097 do over components with typebound defined assignment [%cmp]
10098 #if one component's assignment procedure is INOUT
10099 t1 = expr1
10100 #if expr2 non-variable
10101 temp_x = expr2
10102 expr2 => temp_x
10103 # endif
10104 expr1 = expr2
10105 # for each cmp
10106 t1%cmp {defined=} expr2%cmp
10107 expr1%cmp = t1%cmp
10108 #else
10109 expr1 = expr2
10111 # for each cmp
10112 expr1%cmp {defined=} expr2%cmp
10113 #endif
10116 /* The temporary assignments have to be put on top of the additional
10117 code to avoid the result being changed by the intrinsic assignment.
10119 static int component_assignment_level = 0;
10120 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
10122 static void
10123 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
10125 gfc_component *comp1, *comp2;
10126 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
10127 gfc_expr *t1;
10128 int error_count, depth;
10130 gfc_get_errors (NULL, &error_count);
10132 /* Filter out continuing processing after an error. */
10133 if (error_count
10134 || (*code)->expr1->ts.type != BT_DERIVED
10135 || (*code)->expr2->ts.type != BT_DERIVED)
10136 return;
10138 /* TODO: Handle more than one part array reference in assignments. */
10139 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
10140 (*code)->expr1->rank ? 1 : 0);
10141 if (depth > 1)
10143 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
10144 "done because multiple part array references would "
10145 "occur in intermediate expressions.", &(*code)->loc);
10146 return;
10149 component_assignment_level++;
10151 /* Create a temporary so that functions get called only once. */
10152 if ((*code)->expr2->expr_type != EXPR_VARIABLE
10153 && (*code)->expr2->expr_type != EXPR_CONSTANT)
10155 gfc_expr *tmp_expr;
10157 /* Assign the rhs to the temporary. */
10158 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
10159 this_code = build_assignment (EXEC_ASSIGN,
10160 tmp_expr, (*code)->expr2,
10161 NULL, NULL, (*code)->loc);
10162 /* Add the code and substitute the rhs expression. */
10163 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
10164 gfc_free_expr ((*code)->expr2);
10165 (*code)->expr2 = tmp_expr;
10168 /* Do the intrinsic assignment. This is not needed if the lhs is one
10169 of the temporaries generated here, since the intrinsic assignment
10170 to the final result already does this. */
10171 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
10173 this_code = build_assignment (EXEC_ASSIGN,
10174 (*code)->expr1, (*code)->expr2,
10175 NULL, NULL, (*code)->loc);
10176 add_code_to_chain (&this_code, &head, &tail);
10179 comp1 = (*code)->expr1->ts.u.derived->components;
10180 comp2 = (*code)->expr2->ts.u.derived->components;
10182 t1 = NULL;
10183 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
10185 bool inout = false;
10187 /* The intrinsic assignment does the right thing for pointers
10188 of all kinds and allocatable components. */
10189 if (!gfc_bt_struct (comp1->ts.type)
10190 || comp1->attr.pointer
10191 || comp1->attr.allocatable
10192 || comp1->attr.proc_pointer_comp
10193 || comp1->attr.class_pointer
10194 || comp1->attr.proc_pointer)
10195 continue;
10197 /* Make an assigment for this component. */
10198 this_code = build_assignment (EXEC_ASSIGN,
10199 (*code)->expr1, (*code)->expr2,
10200 comp1, comp2, (*code)->loc);
10202 /* Convert the assignment if there is a defined assignment for
10203 this type. Otherwise, using the call from gfc_resolve_code,
10204 recurse into its components. */
10205 gfc_resolve_code (this_code, ns);
10207 if (this_code->op == EXEC_ASSIGN_CALL)
10209 gfc_formal_arglist *dummy_args;
10210 gfc_symbol *rsym;
10211 /* Check that there is a typebound defined assignment. If not,
10212 then this must be a module defined assignment. We cannot
10213 use the defined_assign_comp attribute here because it must
10214 be this derived type that has the defined assignment and not
10215 a parent type. */
10216 if (!(comp1->ts.u.derived->f2k_derived
10217 && comp1->ts.u.derived->f2k_derived
10218 ->tb_op[INTRINSIC_ASSIGN]))
10220 gfc_free_statements (this_code);
10221 this_code = NULL;
10222 continue;
10225 /* If the first argument of the subroutine has intent INOUT
10226 a temporary must be generated and used instead. */
10227 rsym = this_code->resolved_sym;
10228 dummy_args = gfc_sym_get_dummy_args (rsym);
10229 if (dummy_args
10230 && dummy_args->sym->attr.intent == INTENT_INOUT)
10232 gfc_code *temp_code;
10233 inout = true;
10235 /* Build the temporary required for the assignment and put
10236 it at the head of the generated code. */
10237 if (!t1)
10239 t1 = get_temp_from_expr ((*code)->expr1, ns);
10240 temp_code = build_assignment (EXEC_ASSIGN,
10241 t1, (*code)->expr1,
10242 NULL, NULL, (*code)->loc);
10244 /* For allocatable LHS, check whether it is allocated. Note
10245 that allocatable components with defined assignment are
10246 not yet support. See PR 57696. */
10247 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
10249 gfc_code *block;
10250 gfc_expr *e =
10251 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10252 block = gfc_get_code (EXEC_IF);
10253 block->block = gfc_get_code (EXEC_IF);
10254 block->block->expr1
10255 = gfc_build_intrinsic_call (ns,
10256 GFC_ISYM_ALLOCATED, "allocated",
10257 (*code)->loc, 1, e);
10258 block->block->next = temp_code;
10259 temp_code = block;
10261 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
10264 /* Replace the first actual arg with the component of the
10265 temporary. */
10266 gfc_free_expr (this_code->ext.actual->expr);
10267 this_code->ext.actual->expr = gfc_copy_expr (t1);
10268 add_comp_ref (this_code->ext.actual->expr, comp1);
10270 /* If the LHS variable is allocatable and wasn't allocated and
10271 the temporary is allocatable, pointer assign the address of
10272 the freshly allocated LHS to the temporary. */
10273 if ((*code)->expr1->symtree->n.sym->attr.allocatable
10274 && gfc_expr_attr ((*code)->expr1).allocatable)
10276 gfc_code *block;
10277 gfc_expr *cond;
10279 cond = gfc_get_expr ();
10280 cond->ts.type = BT_LOGICAL;
10281 cond->ts.kind = gfc_default_logical_kind;
10282 cond->expr_type = EXPR_OP;
10283 cond->where = (*code)->loc;
10284 cond->value.op.op = INTRINSIC_NOT;
10285 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
10286 GFC_ISYM_ALLOCATED, "allocated",
10287 (*code)->loc, 1, gfc_copy_expr (t1));
10288 block = gfc_get_code (EXEC_IF);
10289 block->block = gfc_get_code (EXEC_IF);
10290 block->block->expr1 = cond;
10291 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10292 t1, (*code)->expr1,
10293 NULL, NULL, (*code)->loc);
10294 add_code_to_chain (&block, &head, &tail);
10298 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
10300 /* Don't add intrinsic assignments since they are already
10301 effected by the intrinsic assignment of the structure. */
10302 gfc_free_statements (this_code);
10303 this_code = NULL;
10304 continue;
10307 add_code_to_chain (&this_code, &head, &tail);
10309 if (t1 && inout)
10311 /* Transfer the value to the final result. */
10312 this_code = build_assignment (EXEC_ASSIGN,
10313 (*code)->expr1, t1,
10314 comp1, comp2, (*code)->loc);
10315 add_code_to_chain (&this_code, &head, &tail);
10319 /* Put the temporary assignments at the top of the generated code. */
10320 if (tmp_head && component_assignment_level == 1)
10322 gfc_append_code (tmp_head, head);
10323 head = tmp_head;
10324 tmp_head = tmp_tail = NULL;
10327 // If we did a pointer assignment - thus, we need to ensure that the LHS is
10328 // not accidentally deallocated. Hence, nullify t1.
10329 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
10330 && gfc_expr_attr ((*code)->expr1).allocatable)
10332 gfc_code *block;
10333 gfc_expr *cond;
10334 gfc_expr *e;
10336 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10337 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
10338 (*code)->loc, 2, gfc_copy_expr (t1), e);
10339 block = gfc_get_code (EXEC_IF);
10340 block->block = gfc_get_code (EXEC_IF);
10341 block->block->expr1 = cond;
10342 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10343 t1, gfc_get_null_expr (&(*code)->loc),
10344 NULL, NULL, (*code)->loc);
10345 gfc_append_code (tail, block);
10346 tail = block;
10349 /* Now attach the remaining code chain to the input code. Step on
10350 to the end of the new code since resolution is complete. */
10351 gcc_assert ((*code)->op == EXEC_ASSIGN);
10352 tail->next = (*code)->next;
10353 /* Overwrite 'code' because this would place the intrinsic assignment
10354 before the temporary for the lhs is created. */
10355 gfc_free_expr ((*code)->expr1);
10356 gfc_free_expr ((*code)->expr2);
10357 **code = *head;
10358 if (head != tail)
10359 free (head);
10360 *code = tail;
10362 component_assignment_level--;
10366 /* F2008: Pointer function assignments are of the form:
10367 ptr_fcn (args) = expr
10368 This function breaks these assignments into two statements:
10369 temporary_pointer => ptr_fcn(args)
10370 temporary_pointer = expr */
10372 static bool
10373 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
10375 gfc_expr *tmp_ptr_expr;
10376 gfc_code *this_code;
10377 gfc_component *comp;
10378 gfc_symbol *s;
10380 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
10381 return false;
10383 /* Even if standard does not support this feature, continue to build
10384 the two statements to avoid upsetting frontend_passes.c. */
10385 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
10386 "%L", &(*code)->loc);
10388 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
10390 if (comp)
10391 s = comp->ts.interface;
10392 else
10393 s = (*code)->expr1->symtree->n.sym;
10395 if (s == NULL || !s->result->attr.pointer)
10397 gfc_error ("The function result on the lhs of the assignment at "
10398 "%L must have the pointer attribute.",
10399 &(*code)->expr1->where);
10400 (*code)->op = EXEC_NOP;
10401 return false;
10404 tmp_ptr_expr = get_temp_from_expr ((*code)->expr2, ns);
10406 /* get_temp_from_expression is set up for ordinary assignments. To that
10407 end, where array bounds are not known, arrays are made allocatable.
10408 Change the temporary to a pointer here. */
10409 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
10410 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
10411 tmp_ptr_expr->where = (*code)->loc;
10413 this_code = build_assignment (EXEC_ASSIGN,
10414 tmp_ptr_expr, (*code)->expr2,
10415 NULL, NULL, (*code)->loc);
10416 this_code->next = (*code)->next;
10417 (*code)->next = this_code;
10418 (*code)->op = EXEC_POINTER_ASSIGN;
10419 (*code)->expr2 = (*code)->expr1;
10420 (*code)->expr1 = tmp_ptr_expr;
10422 return true;
10426 /* Deferred character length assignments from an operator expression
10427 require a temporary because the character length of the lhs can
10428 change in the course of the assignment. */
10430 static bool
10431 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
10433 gfc_expr *tmp_expr;
10434 gfc_code *this_code;
10436 if (!((*code)->expr1->ts.type == BT_CHARACTER
10437 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
10438 && (*code)->expr2->expr_type == EXPR_OP))
10439 return false;
10441 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
10442 return false;
10444 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
10445 tmp_expr->where = (*code)->loc;
10447 /* A new charlen is required to ensure that the variable string
10448 length is different to that of the original lhs. */
10449 tmp_expr->ts.u.cl = gfc_get_charlen();
10450 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
10451 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
10452 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
10454 tmp_expr->symtree->n.sym->ts.deferred = 1;
10456 this_code = build_assignment (EXEC_ASSIGN,
10457 (*code)->expr1,
10458 gfc_copy_expr (tmp_expr),
10459 NULL, NULL, (*code)->loc);
10461 (*code)->expr1 = tmp_expr;
10463 this_code->next = (*code)->next;
10464 (*code)->next = this_code;
10466 return true;
10470 /* Given a block of code, recursively resolve everything pointed to by this
10471 code block. */
10473 void
10474 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
10476 int omp_workshare_save;
10477 int forall_save, do_concurrent_save;
10478 code_stack frame;
10479 bool t;
10481 frame.prev = cs_base;
10482 frame.head = code;
10483 cs_base = &frame;
10485 find_reachable_labels (code);
10487 for (; code; code = code->next)
10489 frame.current = code;
10490 forall_save = forall_flag;
10491 do_concurrent_save = gfc_do_concurrent_flag;
10493 if (code->op == EXEC_FORALL)
10495 forall_flag = 1;
10496 gfc_resolve_forall (code, ns, forall_save);
10497 forall_flag = 2;
10499 else if (code->block)
10501 omp_workshare_save = -1;
10502 switch (code->op)
10504 case EXEC_OACC_PARALLEL_LOOP:
10505 case EXEC_OACC_PARALLEL:
10506 case EXEC_OACC_KERNELS_LOOP:
10507 case EXEC_OACC_KERNELS:
10508 case EXEC_OACC_DATA:
10509 case EXEC_OACC_HOST_DATA:
10510 case EXEC_OACC_LOOP:
10511 gfc_resolve_oacc_blocks (code, ns);
10512 break;
10513 case EXEC_OMP_PARALLEL_WORKSHARE:
10514 omp_workshare_save = omp_workshare_flag;
10515 omp_workshare_flag = 1;
10516 gfc_resolve_omp_parallel_blocks (code, ns);
10517 break;
10518 case EXEC_OMP_PARALLEL:
10519 case EXEC_OMP_PARALLEL_DO:
10520 case EXEC_OMP_PARALLEL_DO_SIMD:
10521 case EXEC_OMP_PARALLEL_SECTIONS:
10522 case EXEC_OMP_TARGET_TEAMS:
10523 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10524 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10525 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10526 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10527 case EXEC_OMP_TASK:
10528 case EXEC_OMP_TEAMS:
10529 case EXEC_OMP_TEAMS_DISTRIBUTE:
10530 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10531 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10532 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10533 omp_workshare_save = omp_workshare_flag;
10534 omp_workshare_flag = 0;
10535 gfc_resolve_omp_parallel_blocks (code, ns);
10536 break;
10537 case EXEC_OMP_DISTRIBUTE:
10538 case EXEC_OMP_DISTRIBUTE_SIMD:
10539 case EXEC_OMP_DO:
10540 case EXEC_OMP_DO_SIMD:
10541 case EXEC_OMP_SIMD:
10542 gfc_resolve_omp_do_blocks (code, ns);
10543 break;
10544 case EXEC_SELECT_TYPE:
10545 /* Blocks are handled in resolve_select_type because we have
10546 to transform the SELECT TYPE into ASSOCIATE first. */
10547 break;
10548 case EXEC_DO_CONCURRENT:
10549 gfc_do_concurrent_flag = 1;
10550 gfc_resolve_blocks (code->block, ns);
10551 gfc_do_concurrent_flag = 2;
10552 break;
10553 case EXEC_OMP_WORKSHARE:
10554 omp_workshare_save = omp_workshare_flag;
10555 omp_workshare_flag = 1;
10556 /* FALL THROUGH */
10557 default:
10558 gfc_resolve_blocks (code->block, ns);
10559 break;
10562 if (omp_workshare_save != -1)
10563 omp_workshare_flag = omp_workshare_save;
10565 start:
10566 t = true;
10567 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
10568 t = gfc_resolve_expr (code->expr1);
10569 forall_flag = forall_save;
10570 gfc_do_concurrent_flag = do_concurrent_save;
10572 if (!gfc_resolve_expr (code->expr2))
10573 t = false;
10575 if (code->op == EXEC_ALLOCATE
10576 && !gfc_resolve_expr (code->expr3))
10577 t = false;
10579 switch (code->op)
10581 case EXEC_NOP:
10582 case EXEC_END_BLOCK:
10583 case EXEC_END_NESTED_BLOCK:
10584 case EXEC_CYCLE:
10585 case EXEC_PAUSE:
10586 case EXEC_STOP:
10587 case EXEC_ERROR_STOP:
10588 case EXEC_EXIT:
10589 case EXEC_CONTINUE:
10590 case EXEC_DT_END:
10591 case EXEC_ASSIGN_CALL:
10592 break;
10594 case EXEC_CRITICAL:
10595 resolve_critical (code);
10596 break;
10598 case EXEC_SYNC_ALL:
10599 case EXEC_SYNC_IMAGES:
10600 case EXEC_SYNC_MEMORY:
10601 resolve_sync (code);
10602 break;
10604 case EXEC_LOCK:
10605 case EXEC_UNLOCK:
10606 case EXEC_EVENT_POST:
10607 case EXEC_EVENT_WAIT:
10608 resolve_lock_unlock_event (code);
10609 break;
10611 case EXEC_ENTRY:
10612 /* Keep track of which entry we are up to. */
10613 current_entry_id = code->ext.entry->id;
10614 break;
10616 case EXEC_WHERE:
10617 resolve_where (code, NULL);
10618 break;
10620 case EXEC_GOTO:
10621 if (code->expr1 != NULL)
10623 if (code->expr1->ts.type != BT_INTEGER)
10624 gfc_error ("ASSIGNED GOTO statement at %L requires an "
10625 "INTEGER variable", &code->expr1->where);
10626 else if (code->expr1->symtree->n.sym->attr.assign != 1)
10627 gfc_error ("Variable %qs has not been assigned a target "
10628 "label at %L", code->expr1->symtree->n.sym->name,
10629 &code->expr1->where);
10631 else
10632 resolve_branch (code->label1, code);
10633 break;
10635 case EXEC_RETURN:
10636 if (code->expr1 != NULL
10637 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
10638 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
10639 "INTEGER return specifier", &code->expr1->where);
10640 break;
10642 case EXEC_INIT_ASSIGN:
10643 case EXEC_END_PROCEDURE:
10644 break;
10646 case EXEC_ASSIGN:
10647 if (!t)
10648 break;
10650 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
10651 the LHS. */
10652 if (code->expr1->expr_type == EXPR_FUNCTION
10653 && code->expr1->value.function.isym
10654 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10655 remove_caf_get_intrinsic (code->expr1);
10657 /* If this is a pointer function in an lvalue variable context,
10658 the new code will have to be resolved afresh. This is also the
10659 case with an error, where the code is transformed into NOP to
10660 prevent ICEs downstream. */
10661 if (resolve_ptr_fcn_assign (&code, ns)
10662 || code->op == EXEC_NOP)
10663 goto start;
10665 if (!gfc_check_vardef_context (code->expr1, false, false, false,
10666 _("assignment")))
10667 break;
10669 if (resolve_ordinary_assign (code, ns))
10671 if (code->op == EXEC_COMPCALL)
10672 goto compcall;
10673 else
10674 goto call;
10677 /* Check for dependencies in deferred character length array
10678 assignments and generate a temporary, if necessary. */
10679 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
10680 break;
10682 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
10683 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
10684 && code->expr1->ts.u.derived
10685 && code->expr1->ts.u.derived->attr.defined_assign_comp)
10686 generate_component_assignments (&code, ns);
10688 break;
10690 case EXEC_LABEL_ASSIGN:
10691 if (code->label1->defined == ST_LABEL_UNKNOWN)
10692 gfc_error ("Label %d referenced at %L is never defined",
10693 code->label1->value, &code->label1->where);
10694 if (t
10695 && (code->expr1->expr_type != EXPR_VARIABLE
10696 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
10697 || code->expr1->symtree->n.sym->ts.kind
10698 != gfc_default_integer_kind
10699 || code->expr1->symtree->n.sym->as != NULL))
10700 gfc_error ("ASSIGN statement at %L requires a scalar "
10701 "default INTEGER variable", &code->expr1->where);
10702 break;
10704 case EXEC_POINTER_ASSIGN:
10706 gfc_expr* e;
10708 if (!t)
10709 break;
10711 /* This is both a variable definition and pointer assignment
10712 context, so check both of them. For rank remapping, a final
10713 array ref may be present on the LHS and fool gfc_expr_attr
10714 used in gfc_check_vardef_context. Remove it. */
10715 e = remove_last_array_ref (code->expr1);
10716 t = gfc_check_vardef_context (e, true, false, false,
10717 _("pointer assignment"));
10718 if (t)
10719 t = gfc_check_vardef_context (e, false, false, false,
10720 _("pointer assignment"));
10721 gfc_free_expr (e);
10722 if (!t)
10723 break;
10725 gfc_check_pointer_assign (code->expr1, code->expr2);
10726 break;
10729 case EXEC_ARITHMETIC_IF:
10731 gfc_expr *e = code->expr1;
10733 gfc_resolve_expr (e);
10734 if (e->expr_type == EXPR_NULL)
10735 gfc_error ("Invalid NULL at %L", &e->where);
10737 if (t && (e->rank > 0
10738 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
10739 gfc_error ("Arithmetic IF statement at %L requires a scalar "
10740 "REAL or INTEGER expression", &e->where);
10742 resolve_branch (code->label1, code);
10743 resolve_branch (code->label2, code);
10744 resolve_branch (code->label3, code);
10746 break;
10748 case EXEC_IF:
10749 if (t && code->expr1 != NULL
10750 && (code->expr1->ts.type != BT_LOGICAL
10751 || code->expr1->rank != 0))
10752 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10753 &code->expr1->where);
10754 break;
10756 case EXEC_CALL:
10757 call:
10758 resolve_call (code);
10759 break;
10761 case EXEC_COMPCALL:
10762 compcall:
10763 resolve_typebound_subroutine (code);
10764 break;
10766 case EXEC_CALL_PPC:
10767 resolve_ppc_call (code);
10768 break;
10770 case EXEC_SELECT:
10771 /* Select is complicated. Also, a SELECT construct could be
10772 a transformed computed GOTO. */
10773 resolve_select (code, false);
10774 break;
10776 case EXEC_SELECT_TYPE:
10777 resolve_select_type (code, ns);
10778 break;
10780 case EXEC_BLOCK:
10781 resolve_block_construct (code);
10782 break;
10784 case EXEC_DO:
10785 if (code->ext.iterator != NULL)
10787 gfc_iterator *iter = code->ext.iterator;
10788 if (gfc_resolve_iterator (iter, true, false))
10789 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
10791 break;
10793 case EXEC_DO_WHILE:
10794 if (code->expr1 == NULL)
10795 gfc_internal_error ("gfc_resolve_code(): No expression on "
10796 "DO WHILE");
10797 if (t
10798 && (code->expr1->rank != 0
10799 || code->expr1->ts.type != BT_LOGICAL))
10800 gfc_error ("Exit condition of DO WHILE loop at %L must be "
10801 "a scalar LOGICAL expression", &code->expr1->where);
10802 break;
10804 case EXEC_ALLOCATE:
10805 if (t)
10806 resolve_allocate_deallocate (code, "ALLOCATE");
10808 break;
10810 case EXEC_DEALLOCATE:
10811 if (t)
10812 resolve_allocate_deallocate (code, "DEALLOCATE");
10814 break;
10816 case EXEC_OPEN:
10817 if (!gfc_resolve_open (code->ext.open))
10818 break;
10820 resolve_branch (code->ext.open->err, code);
10821 break;
10823 case EXEC_CLOSE:
10824 if (!gfc_resolve_close (code->ext.close))
10825 break;
10827 resolve_branch (code->ext.close->err, code);
10828 break;
10830 case EXEC_BACKSPACE:
10831 case EXEC_ENDFILE:
10832 case EXEC_REWIND:
10833 case EXEC_FLUSH:
10834 if (!gfc_resolve_filepos (code->ext.filepos))
10835 break;
10837 resolve_branch (code->ext.filepos->err, code);
10838 break;
10840 case EXEC_INQUIRE:
10841 if (!gfc_resolve_inquire (code->ext.inquire))
10842 break;
10844 resolve_branch (code->ext.inquire->err, code);
10845 break;
10847 case EXEC_IOLENGTH:
10848 gcc_assert (code->ext.inquire != NULL);
10849 if (!gfc_resolve_inquire (code->ext.inquire))
10850 break;
10852 resolve_branch (code->ext.inquire->err, code);
10853 break;
10855 case EXEC_WAIT:
10856 if (!gfc_resolve_wait (code->ext.wait))
10857 break;
10859 resolve_branch (code->ext.wait->err, code);
10860 resolve_branch (code->ext.wait->end, code);
10861 resolve_branch (code->ext.wait->eor, code);
10862 break;
10864 case EXEC_READ:
10865 case EXEC_WRITE:
10866 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
10867 break;
10869 resolve_branch (code->ext.dt->err, code);
10870 resolve_branch (code->ext.dt->end, code);
10871 resolve_branch (code->ext.dt->eor, code);
10872 break;
10874 case EXEC_TRANSFER:
10875 resolve_transfer (code);
10876 break;
10878 case EXEC_DO_CONCURRENT:
10879 case EXEC_FORALL:
10880 resolve_forall_iterators (code->ext.forall_iterator);
10882 if (code->expr1 != NULL
10883 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
10884 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
10885 "expression", &code->expr1->where);
10886 break;
10888 case EXEC_OACC_PARALLEL_LOOP:
10889 case EXEC_OACC_PARALLEL:
10890 case EXEC_OACC_KERNELS_LOOP:
10891 case EXEC_OACC_KERNELS:
10892 case EXEC_OACC_DATA:
10893 case EXEC_OACC_HOST_DATA:
10894 case EXEC_OACC_LOOP:
10895 case EXEC_OACC_UPDATE:
10896 case EXEC_OACC_WAIT:
10897 case EXEC_OACC_CACHE:
10898 case EXEC_OACC_ENTER_DATA:
10899 case EXEC_OACC_EXIT_DATA:
10900 case EXEC_OACC_ATOMIC:
10901 case EXEC_OACC_DECLARE:
10902 gfc_resolve_oacc_directive (code, ns);
10903 break;
10905 case EXEC_OMP_ATOMIC:
10906 case EXEC_OMP_BARRIER:
10907 case EXEC_OMP_CANCEL:
10908 case EXEC_OMP_CANCELLATION_POINT:
10909 case EXEC_OMP_CRITICAL:
10910 case EXEC_OMP_FLUSH:
10911 case EXEC_OMP_DISTRIBUTE:
10912 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10913 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10914 case EXEC_OMP_DISTRIBUTE_SIMD:
10915 case EXEC_OMP_DO:
10916 case EXEC_OMP_DO_SIMD:
10917 case EXEC_OMP_MASTER:
10918 case EXEC_OMP_ORDERED:
10919 case EXEC_OMP_SECTIONS:
10920 case EXEC_OMP_SIMD:
10921 case EXEC_OMP_SINGLE:
10922 case EXEC_OMP_TARGET:
10923 case EXEC_OMP_TARGET_DATA:
10924 case EXEC_OMP_TARGET_TEAMS:
10925 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10926 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10927 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10928 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10929 case EXEC_OMP_TARGET_UPDATE:
10930 case EXEC_OMP_TASK:
10931 case EXEC_OMP_TASKGROUP:
10932 case EXEC_OMP_TASKWAIT:
10933 case EXEC_OMP_TASKYIELD:
10934 case EXEC_OMP_TEAMS:
10935 case EXEC_OMP_TEAMS_DISTRIBUTE:
10936 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10937 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10938 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10939 case EXEC_OMP_WORKSHARE:
10940 gfc_resolve_omp_directive (code, ns);
10941 break;
10943 case EXEC_OMP_PARALLEL:
10944 case EXEC_OMP_PARALLEL_DO:
10945 case EXEC_OMP_PARALLEL_DO_SIMD:
10946 case EXEC_OMP_PARALLEL_SECTIONS:
10947 case EXEC_OMP_PARALLEL_WORKSHARE:
10948 omp_workshare_save = omp_workshare_flag;
10949 omp_workshare_flag = 0;
10950 gfc_resolve_omp_directive (code, ns);
10951 omp_workshare_flag = omp_workshare_save;
10952 break;
10954 default:
10955 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
10959 cs_base = frame.prev;
10963 /* Resolve initial values and make sure they are compatible with
10964 the variable. */
10966 static void
10967 resolve_values (gfc_symbol *sym)
10969 bool t;
10971 if (sym->value == NULL)
10972 return;
10974 if (sym->value->expr_type == EXPR_STRUCTURE)
10975 t= resolve_structure_cons (sym->value, 1);
10976 else
10977 t = gfc_resolve_expr (sym->value);
10979 if (!t)
10980 return;
10982 gfc_check_assign_symbol (sym, NULL, sym->value);
10986 /* Verify any BIND(C) derived types in the namespace so we can report errors
10987 for them once, rather than for each variable declared of that type. */
10989 static void
10990 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10992 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10993 && derived_sym->attr.is_bind_c == 1)
10994 verify_bind_c_derived_type (derived_sym);
10996 return;
11000 /* Check the interfaces of DTIO procedures associated with derived
11001 type 'sym'. These procedures can either have typebound bindings or
11002 can appear in DTIO generic interfaces. */
11004 static void
11005 gfc_verify_DTIO_procedures (gfc_symbol *sym)
11007 if (!sym || sym->attr.flavor != FL_DERIVED)
11008 return;
11010 gfc_check_dtio_interfaces (sym);
11012 return;
11015 /* Verify that any binding labels used in a given namespace do not collide
11016 with the names or binding labels of any global symbols. Multiple INTERFACE
11017 for the same procedure are permitted. */
11019 static void
11020 gfc_verify_binding_labels (gfc_symbol *sym)
11022 gfc_gsymbol *gsym;
11023 const char *module;
11025 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
11026 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
11027 return;
11029 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
11031 if (sym->module)
11032 module = sym->module;
11033 else if (sym->ns && sym->ns->proc_name
11034 && sym->ns->proc_name->attr.flavor == FL_MODULE)
11035 module = sym->ns->proc_name->name;
11036 else if (sym->ns && sym->ns->parent
11037 && sym->ns && sym->ns->parent->proc_name
11038 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
11039 module = sym->ns->parent->proc_name->name;
11040 else
11041 module = NULL;
11043 if (!gsym
11044 || (!gsym->defined
11045 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
11047 if (!gsym)
11048 gsym = gfc_get_gsymbol (sym->binding_label);
11049 gsym->where = sym->declared_at;
11050 gsym->sym_name = sym->name;
11051 gsym->binding_label = sym->binding_label;
11052 gsym->ns = sym->ns;
11053 gsym->mod_name = module;
11054 if (sym->attr.function)
11055 gsym->type = GSYM_FUNCTION;
11056 else if (sym->attr.subroutine)
11057 gsym->type = GSYM_SUBROUTINE;
11058 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
11059 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
11060 return;
11063 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
11065 gfc_error ("Variable %s with binding label %s at %L uses the same global "
11066 "identifier as entity at %L", sym->name,
11067 sym->binding_label, &sym->declared_at, &gsym->where);
11068 /* Clear the binding label to prevent checking multiple times. */
11069 sym->binding_label = NULL;
11072 else if (sym->attr.flavor == FL_VARIABLE && module
11073 && (strcmp (module, gsym->mod_name) != 0
11074 || strcmp (sym->name, gsym->sym_name) != 0))
11076 /* This can only happen if the variable is defined in a module - if it
11077 isn't the same module, reject it. */
11078 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
11079 "the same global identifier as entity at %L from module %s",
11080 sym->name, module, sym->binding_label,
11081 &sym->declared_at, &gsym->where, gsym->mod_name);
11082 sym->binding_label = NULL;
11084 else if ((sym->attr.function || sym->attr.subroutine)
11085 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
11086 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
11087 && sym != gsym->ns->proc_name
11088 && (module != gsym->mod_name
11089 || strcmp (gsym->sym_name, sym->name) != 0
11090 || (module && strcmp (module, gsym->mod_name) != 0)))
11092 /* Print an error if the procedure is defined multiple times; we have to
11093 exclude references to the same procedure via module association or
11094 multiple checks for the same procedure. */
11095 gfc_error ("Procedure %s with binding label %s at %L uses the same "
11096 "global identifier as entity at %L", sym->name,
11097 sym->binding_label, &sym->declared_at, &gsym->where);
11098 sym->binding_label = NULL;
11103 /* Resolve an index expression. */
11105 static bool
11106 resolve_index_expr (gfc_expr *e)
11108 if (!gfc_resolve_expr (e))
11109 return false;
11111 if (!gfc_simplify_expr (e, 0))
11112 return false;
11114 if (!gfc_specification_expr (e))
11115 return false;
11117 return true;
11121 /* Resolve a charlen structure. */
11123 static bool
11124 resolve_charlen (gfc_charlen *cl)
11126 int i, k;
11127 bool saved_specification_expr;
11129 if (cl->resolved)
11130 return true;
11132 cl->resolved = 1;
11133 saved_specification_expr = specification_expr;
11134 specification_expr = true;
11136 if (cl->length_from_typespec)
11138 if (!gfc_resolve_expr (cl->length))
11140 specification_expr = saved_specification_expr;
11141 return false;
11144 if (!gfc_simplify_expr (cl->length, 0))
11146 specification_expr = saved_specification_expr;
11147 return false;
11150 else
11153 if (!resolve_index_expr (cl->length))
11155 specification_expr = saved_specification_expr;
11156 return false;
11160 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
11161 a negative value, the length of character entities declared is zero. */
11162 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
11163 gfc_replace_expr (cl->length,
11164 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
11166 /* Check that the character length is not too large. */
11167 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
11168 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
11169 && cl->length->ts.type == BT_INTEGER
11170 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
11172 gfc_error ("String length at %L is too large", &cl->length->where);
11173 specification_expr = saved_specification_expr;
11174 return false;
11177 specification_expr = saved_specification_expr;
11178 return true;
11182 /* Test for non-constant shape arrays. */
11184 static bool
11185 is_non_constant_shape_array (gfc_symbol *sym)
11187 gfc_expr *e;
11188 int i;
11189 bool not_constant;
11191 not_constant = false;
11192 if (sym->as != NULL)
11194 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
11195 has not been simplified; parameter array references. Do the
11196 simplification now. */
11197 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
11199 e = sym->as->lower[i];
11200 if (e && (!resolve_index_expr(e)
11201 || !gfc_is_constant_expr (e)))
11202 not_constant = true;
11203 e = sym->as->upper[i];
11204 if (e && (!resolve_index_expr(e)
11205 || !gfc_is_constant_expr (e)))
11206 not_constant = true;
11209 return not_constant;
11212 /* Given a symbol and an initialization expression, add code to initialize
11213 the symbol to the function entry. */
11214 static void
11215 build_init_assign (gfc_symbol *sym, gfc_expr *init)
11217 gfc_expr *lval;
11218 gfc_code *init_st;
11219 gfc_namespace *ns = sym->ns;
11221 /* Search for the function namespace if this is a contained
11222 function without an explicit result. */
11223 if (sym->attr.function && sym == sym->result
11224 && sym->name != sym->ns->proc_name->name)
11226 ns = ns->contained;
11227 for (;ns; ns = ns->sibling)
11228 if (strcmp (ns->proc_name->name, sym->name) == 0)
11229 break;
11232 if (ns == NULL)
11234 gfc_free_expr (init);
11235 return;
11238 /* Build an l-value expression for the result. */
11239 lval = gfc_lval_expr_from_sym (sym);
11241 /* Add the code at scope entry. */
11242 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
11243 init_st->next = ns->code;
11244 ns->code = init_st;
11246 /* Assign the default initializer to the l-value. */
11247 init_st->loc = sym->declared_at;
11248 init_st->expr1 = lval;
11249 init_st->expr2 = init;
11253 /* Whether or not we can generate a default initializer for a symbol. */
11255 static bool
11256 can_generate_init (gfc_symbol *sym)
11258 symbol_attribute *a;
11259 if (!sym)
11260 return false;
11261 a = &sym->attr;
11263 /* These symbols should never have a default initialization. */
11264 return !(
11265 a->allocatable
11266 || a->external
11267 || a->pointer
11268 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
11269 && (CLASS_DATA (sym)->attr.class_pointer
11270 || CLASS_DATA (sym)->attr.proc_pointer))
11271 || a->in_equivalence
11272 || a->in_common
11273 || a->data
11274 || sym->module
11275 || a->cray_pointee
11276 || a->cray_pointer
11277 || sym->assoc
11278 || (!a->referenced && !a->result)
11279 || (a->dummy && a->intent != INTENT_OUT)
11280 || (a->function && sym != sym->result)
11285 /* Assign the default initializer to a derived type variable or result. */
11287 static void
11288 apply_default_init (gfc_symbol *sym)
11290 gfc_expr *init = NULL;
11292 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11293 return;
11295 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
11296 init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
11298 if (init == NULL && sym->ts.type != BT_CLASS)
11299 return;
11301 build_init_assign (sym, init);
11302 sym->attr.referenced = 1;
11306 /* Build an initializer for a local. Returns null if the symbol should not have
11307 a default initialization. */
11309 static gfc_expr *
11310 build_default_init_expr (gfc_symbol *sym)
11312 /* These symbols should never have a default initialization. */
11313 if (sym->attr.allocatable
11314 || sym->attr.external
11315 || sym->attr.dummy
11316 || sym->attr.pointer
11317 || sym->attr.in_equivalence
11318 || sym->attr.in_common
11319 || sym->attr.data
11320 || sym->module
11321 || sym->attr.cray_pointee
11322 || sym->attr.cray_pointer
11323 || sym->assoc)
11324 return NULL;
11326 /* Get the appropriate init expression. */
11327 return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
11330 /* Add an initialization expression to a local variable. */
11331 static void
11332 apply_default_init_local (gfc_symbol *sym)
11334 gfc_expr *init = NULL;
11336 /* The symbol should be a variable or a function return value. */
11337 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11338 || (sym->attr.function && sym->result != sym))
11339 return;
11341 /* Try to build the initializer expression. If we can't initialize
11342 this symbol, then init will be NULL. */
11343 init = build_default_init_expr (sym);
11344 if (init == NULL)
11345 return;
11347 /* For saved variables, we don't want to add an initializer at function
11348 entry, so we just add a static initializer. Note that automatic variables
11349 are stack allocated even with -fno-automatic; we have also to exclude
11350 result variable, which are also nonstatic. */
11351 if (sym->attr.save || sym->ns->save_all
11352 || (flag_max_stack_var_size == 0 && !sym->attr.result
11353 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
11354 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
11356 /* Don't clobber an existing initializer! */
11357 gcc_assert (sym->value == NULL);
11358 sym->value = init;
11359 return;
11362 build_init_assign (sym, init);
11366 /* Resolution of common features of flavors variable and procedure. */
11368 static bool
11369 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
11371 gfc_array_spec *as;
11373 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11374 as = CLASS_DATA (sym)->as;
11375 else
11376 as = sym->as;
11378 /* Constraints on deferred shape variable. */
11379 if (as == NULL || as->type != AS_DEFERRED)
11381 bool pointer, allocatable, dimension;
11383 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11385 pointer = CLASS_DATA (sym)->attr.class_pointer;
11386 allocatable = CLASS_DATA (sym)->attr.allocatable;
11387 dimension = CLASS_DATA (sym)->attr.dimension;
11389 else
11391 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
11392 allocatable = sym->attr.allocatable;
11393 dimension = sym->attr.dimension;
11396 if (allocatable)
11398 if (dimension && as->type != AS_ASSUMED_RANK)
11400 gfc_error ("Allocatable array %qs at %L must have a deferred "
11401 "shape or assumed rank", sym->name, &sym->declared_at);
11402 return false;
11404 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
11405 "%qs at %L may not be ALLOCATABLE",
11406 sym->name, &sym->declared_at))
11407 return false;
11410 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
11412 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
11413 "assumed rank", sym->name, &sym->declared_at);
11414 return false;
11417 else
11419 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
11420 && sym->ts.type != BT_CLASS && !sym->assoc)
11422 gfc_error ("Array %qs at %L cannot have a deferred shape",
11423 sym->name, &sym->declared_at);
11424 return false;
11428 /* Constraints on polymorphic variables. */
11429 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
11431 /* F03:C502. */
11432 if (sym->attr.class_ok
11433 && !sym->attr.select_type_temporary
11434 && !UNLIMITED_POLY (sym)
11435 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
11437 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
11438 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
11439 &sym->declared_at);
11440 return false;
11443 /* F03:C509. */
11444 /* Assume that use associated symbols were checked in the module ns.
11445 Class-variables that are associate-names are also something special
11446 and excepted from the test. */
11447 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
11449 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
11450 "or pointer", sym->name, &sym->declared_at);
11451 return false;
11455 return true;
11459 /* Additional checks for symbols with flavor variable and derived
11460 type. To be called from resolve_fl_variable. */
11462 static bool
11463 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
11465 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
11467 /* Check to see if a derived type is blocked from being host
11468 associated by the presence of another class I symbol in the same
11469 namespace. 14.6.1.3 of the standard and the discussion on
11470 comp.lang.fortran. */
11471 if (sym->ns != sym->ts.u.derived->ns
11472 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
11474 gfc_symbol *s;
11475 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
11476 if (s && s->attr.generic)
11477 s = gfc_find_dt_in_generic (s);
11478 if (s && !gfc_fl_struct (s->attr.flavor))
11480 gfc_error ("The type %qs cannot be host associated at %L "
11481 "because it is blocked by an incompatible object "
11482 "of the same name declared at %L",
11483 sym->ts.u.derived->name, &sym->declared_at,
11484 &s->declared_at);
11485 return false;
11489 /* 4th constraint in section 11.3: "If an object of a type for which
11490 component-initialization is specified (R429) appears in the
11491 specification-part of a module and does not have the ALLOCATABLE
11492 or POINTER attribute, the object shall have the SAVE attribute."
11494 The check for initializers is performed with
11495 gfc_has_default_initializer because gfc_default_initializer generates
11496 a hidden default for allocatable components. */
11497 if (!(sym->value || no_init_flag) && sym->ns->proc_name
11498 && sym->ns->proc_name->attr.flavor == FL_MODULE
11499 && !sym->ns->save_all && !sym->attr.save
11500 && !sym->attr.pointer && !sym->attr.allocatable
11501 && gfc_has_default_initializer (sym->ts.u.derived)
11502 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
11503 "%qs at %L, needed due to the default "
11504 "initialization", sym->name, &sym->declared_at))
11505 return false;
11507 /* Assign default initializer. */
11508 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
11509 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
11510 sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
11512 return true;
11516 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
11517 except in the declaration of an entity or component that has the POINTER
11518 or ALLOCATABLE attribute. */
11520 static bool
11521 deferred_requirements (gfc_symbol *sym)
11523 if (sym->ts.deferred
11524 && !(sym->attr.pointer
11525 || sym->attr.allocatable
11526 || sym->attr.omp_udr_artificial_var))
11528 gfc_error ("Entity %qs at %L has a deferred type parameter and "
11529 "requires either the POINTER or ALLOCATABLE attribute",
11530 sym->name, &sym->declared_at);
11531 return false;
11533 return true;
11537 /* Resolve symbols with flavor variable. */
11539 static bool
11540 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
11542 int no_init_flag, automatic_flag;
11543 gfc_expr *e;
11544 const char *auto_save_msg;
11545 bool saved_specification_expr;
11547 auto_save_msg = "Automatic object %qs at %L cannot have the "
11548 "SAVE attribute";
11550 if (!resolve_fl_var_and_proc (sym, mp_flag))
11551 return false;
11553 /* Set this flag to check that variables are parameters of all entries.
11554 This check is effected by the call to gfc_resolve_expr through
11555 is_non_constant_shape_array. */
11556 saved_specification_expr = specification_expr;
11557 specification_expr = true;
11559 if (sym->ns->proc_name
11560 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11561 || sym->ns->proc_name->attr.is_main_program)
11562 && !sym->attr.use_assoc
11563 && !sym->attr.allocatable
11564 && !sym->attr.pointer
11565 && is_non_constant_shape_array (sym))
11567 /* The shape of a main program or module array needs to be
11568 constant. */
11569 gfc_error ("The module or main program array %qs at %L must "
11570 "have constant shape", sym->name, &sym->declared_at);
11571 specification_expr = saved_specification_expr;
11572 return false;
11575 /* Constraints on deferred type parameter. */
11576 if (!deferred_requirements (sym))
11577 return false;
11579 if (sym->ts.type == BT_CHARACTER)
11581 /* Make sure that character string variables with assumed length are
11582 dummy arguments. */
11583 e = sym->ts.u.cl->length;
11584 if (e == NULL && !sym->attr.dummy && !sym->attr.result
11585 && !sym->ts.deferred && !sym->attr.select_type_temporary
11586 && !sym->attr.omp_udr_artificial_var)
11588 gfc_error ("Entity with assumed character length at %L must be a "
11589 "dummy argument or a PARAMETER", &sym->declared_at);
11590 specification_expr = saved_specification_expr;
11591 return false;
11594 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
11596 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
11597 specification_expr = saved_specification_expr;
11598 return false;
11601 if (!gfc_is_constant_expr (e)
11602 && !(e->expr_type == EXPR_VARIABLE
11603 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
11605 if (!sym->attr.use_assoc && sym->ns->proc_name
11606 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11607 || sym->ns->proc_name->attr.is_main_program))
11609 gfc_error ("%qs at %L must have constant character length "
11610 "in this context", sym->name, &sym->declared_at);
11611 specification_expr = saved_specification_expr;
11612 return false;
11614 if (sym->attr.in_common)
11616 gfc_error ("COMMON variable %qs at %L must have constant "
11617 "character length", sym->name, &sym->declared_at);
11618 specification_expr = saved_specification_expr;
11619 return false;
11624 if (sym->value == NULL && sym->attr.referenced)
11625 apply_default_init_local (sym); /* Try to apply a default initialization. */
11627 /* Determine if the symbol may not have an initializer. */
11628 no_init_flag = automatic_flag = 0;
11629 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
11630 || sym->attr.intrinsic || sym->attr.result)
11631 no_init_flag = 1;
11632 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
11633 && is_non_constant_shape_array (sym))
11635 no_init_flag = automatic_flag = 1;
11637 /* Also, they must not have the SAVE attribute.
11638 SAVE_IMPLICIT is checked below. */
11639 if (sym->as && sym->attr.codimension)
11641 int corank = sym->as->corank;
11642 sym->as->corank = 0;
11643 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
11644 sym->as->corank = corank;
11646 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
11648 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
11649 specification_expr = saved_specification_expr;
11650 return false;
11654 /* Ensure that any initializer is simplified. */
11655 if (sym->value)
11656 gfc_simplify_expr (sym->value, 1);
11658 /* Reject illegal initializers. */
11659 if (!sym->mark && sym->value)
11661 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
11662 && CLASS_DATA (sym)->attr.allocatable))
11663 gfc_error ("Allocatable %qs at %L cannot have an initializer",
11664 sym->name, &sym->declared_at);
11665 else if (sym->attr.external)
11666 gfc_error ("External %qs at %L cannot have an initializer",
11667 sym->name, &sym->declared_at);
11668 else if (sym->attr.dummy
11669 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
11670 gfc_error ("Dummy %qs at %L cannot have an initializer",
11671 sym->name, &sym->declared_at);
11672 else if (sym->attr.intrinsic)
11673 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
11674 sym->name, &sym->declared_at);
11675 else if (sym->attr.result)
11676 gfc_error ("Function result %qs at %L cannot have an initializer",
11677 sym->name, &sym->declared_at);
11678 else if (automatic_flag)
11679 gfc_error ("Automatic array %qs at %L cannot have an initializer",
11680 sym->name, &sym->declared_at);
11681 else
11682 goto no_init_error;
11683 specification_expr = saved_specification_expr;
11684 return false;
11687 no_init_error:
11688 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
11690 bool res = resolve_fl_variable_derived (sym, no_init_flag);
11691 specification_expr = saved_specification_expr;
11692 return res;
11695 specification_expr = saved_specification_expr;
11696 return true;
11700 /* Compare the dummy characteristics of a module procedure interface
11701 declaration with the corresponding declaration in a submodule. */
11702 static gfc_formal_arglist *new_formal;
11703 static char errmsg[200];
11705 static void
11706 compare_fsyms (gfc_symbol *sym)
11708 gfc_symbol *fsym;
11710 if (sym == NULL || new_formal == NULL)
11711 return;
11713 fsym = new_formal->sym;
11715 if (sym == fsym)
11716 return;
11718 if (strcmp (sym->name, fsym->name) == 0)
11720 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
11721 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
11726 /* Resolve a procedure. */
11728 static bool
11729 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
11731 gfc_formal_arglist *arg;
11733 if (sym->attr.function
11734 && !resolve_fl_var_and_proc (sym, mp_flag))
11735 return false;
11737 if (sym->ts.type == BT_CHARACTER)
11739 gfc_charlen *cl = sym->ts.u.cl;
11741 if (cl && cl->length && gfc_is_constant_expr (cl->length)
11742 && !resolve_charlen (cl))
11743 return false;
11745 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11746 && sym->attr.proc == PROC_ST_FUNCTION)
11748 gfc_error ("Character-valued statement function %qs at %L must "
11749 "have constant length", sym->name, &sym->declared_at);
11750 return false;
11754 /* Ensure that derived type for are not of a private type. Internal
11755 module procedures are excluded by 2.2.3.3 - i.e., they are not
11756 externally accessible and can access all the objects accessible in
11757 the host. */
11758 if (!(sym->ns->parent
11759 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
11760 && gfc_check_symbol_access (sym))
11762 gfc_interface *iface;
11764 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
11766 if (arg->sym
11767 && arg->sym->ts.type == BT_DERIVED
11768 && !arg->sym->ts.u.derived->attr.use_assoc
11769 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11770 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
11771 "and cannot be a dummy argument"
11772 " of %qs, which is PUBLIC at %L",
11773 arg->sym->name, sym->name,
11774 &sym->declared_at))
11776 /* Stop this message from recurring. */
11777 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11778 return false;
11782 /* PUBLIC interfaces may expose PRIVATE procedures that take types
11783 PRIVATE to the containing module. */
11784 for (iface = sym->generic; iface; iface = iface->next)
11786 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
11788 if (arg->sym
11789 && arg->sym->ts.type == BT_DERIVED
11790 && !arg->sym->ts.u.derived->attr.use_assoc
11791 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11792 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
11793 "PUBLIC interface %qs at %L "
11794 "takes dummy arguments of %qs which "
11795 "is PRIVATE", iface->sym->name,
11796 sym->name, &iface->sym->declared_at,
11797 gfc_typename(&arg->sym->ts)))
11799 /* Stop this message from recurring. */
11800 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11801 return false;
11807 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
11808 && !sym->attr.proc_pointer)
11810 gfc_error ("Function %qs at %L cannot have an initializer",
11811 sym->name, &sym->declared_at);
11812 return false;
11815 /* An external symbol may not have an initializer because it is taken to be
11816 a procedure. Exception: Procedure Pointers. */
11817 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
11819 gfc_error ("External object %qs at %L may not have an initializer",
11820 sym->name, &sym->declared_at);
11821 return false;
11824 /* An elemental function is required to return a scalar 12.7.1 */
11825 if (sym->attr.elemental && sym->attr.function && sym->as)
11827 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
11828 "result", sym->name, &sym->declared_at);
11829 /* Reset so that the error only occurs once. */
11830 sym->attr.elemental = 0;
11831 return false;
11834 if (sym->attr.proc == PROC_ST_FUNCTION
11835 && (sym->attr.allocatable || sym->attr.pointer))
11837 gfc_error ("Statement function %qs at %L may not have pointer or "
11838 "allocatable attribute", sym->name, &sym->declared_at);
11839 return false;
11842 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
11843 char-len-param shall not be array-valued, pointer-valued, recursive
11844 or pure. ....snip... A character value of * may only be used in the
11845 following ways: (i) Dummy arg of procedure - dummy associates with
11846 actual length; (ii) To declare a named constant; or (iii) External
11847 function - but length must be declared in calling scoping unit. */
11848 if (sym->attr.function
11849 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
11850 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
11852 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
11853 || (sym->attr.recursive) || (sym->attr.pure))
11855 if (sym->as && sym->as->rank)
11856 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11857 "array-valued", sym->name, &sym->declared_at);
11859 if (sym->attr.pointer)
11860 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11861 "pointer-valued", sym->name, &sym->declared_at);
11863 if (sym->attr.pure)
11864 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11865 "pure", sym->name, &sym->declared_at);
11867 if (sym->attr.recursive)
11868 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11869 "recursive", sym->name, &sym->declared_at);
11871 return false;
11874 /* Appendix B.2 of the standard. Contained functions give an
11875 error anyway. Deferred character length is an F2003 feature.
11876 Don't warn on intrinsic conversion functions, which start
11877 with two underscores. */
11878 if (!sym->attr.contained && !sym->ts.deferred
11879 && (sym->name[0] != '_' || sym->name[1] != '_'))
11880 gfc_notify_std (GFC_STD_F95_OBS,
11881 "CHARACTER(*) function %qs at %L",
11882 sym->name, &sym->declared_at);
11885 /* F2008, C1218. */
11886 if (sym->attr.elemental)
11888 if (sym->attr.proc_pointer)
11890 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
11891 sym->name, &sym->declared_at);
11892 return false;
11894 if (sym->attr.dummy)
11896 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
11897 sym->name, &sym->declared_at);
11898 return false;
11902 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11904 gfc_formal_arglist *curr_arg;
11905 int has_non_interop_arg = 0;
11907 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11908 sym->common_block))
11910 /* Clear these to prevent looking at them again if there was an
11911 error. */
11912 sym->attr.is_bind_c = 0;
11913 sym->attr.is_c_interop = 0;
11914 sym->ts.is_c_interop = 0;
11916 else
11918 /* So far, no errors have been found. */
11919 sym->attr.is_c_interop = 1;
11920 sym->ts.is_c_interop = 1;
11923 curr_arg = gfc_sym_get_dummy_args (sym);
11924 while (curr_arg != NULL)
11926 /* Skip implicitly typed dummy args here. */
11927 if (curr_arg->sym->attr.implicit_type == 0)
11928 if (!gfc_verify_c_interop_param (curr_arg->sym))
11929 /* If something is found to fail, record the fact so we
11930 can mark the symbol for the procedure as not being
11931 BIND(C) to try and prevent multiple errors being
11932 reported. */
11933 has_non_interop_arg = 1;
11935 curr_arg = curr_arg->next;
11938 /* See if any of the arguments were not interoperable and if so, clear
11939 the procedure symbol to prevent duplicate error messages. */
11940 if (has_non_interop_arg != 0)
11942 sym->attr.is_c_interop = 0;
11943 sym->ts.is_c_interop = 0;
11944 sym->attr.is_bind_c = 0;
11948 if (!sym->attr.proc_pointer)
11950 if (sym->attr.save == SAVE_EXPLICIT)
11952 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11953 "in %qs at %L", sym->name, &sym->declared_at);
11954 return false;
11956 if (sym->attr.intent)
11958 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11959 "in %qs at %L", sym->name, &sym->declared_at);
11960 return false;
11962 if (sym->attr.subroutine && sym->attr.result)
11964 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11965 "in %qs at %L", sym->name, &sym->declared_at);
11966 return false;
11968 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
11969 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11970 || sym->attr.contained))
11972 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11973 "in %qs at %L", sym->name, &sym->declared_at);
11974 return false;
11976 if (strcmp ("ppr@", sym->name) == 0)
11978 gfc_error ("Procedure pointer result %qs at %L "
11979 "is missing the pointer attribute",
11980 sym->ns->proc_name->name, &sym->declared_at);
11981 return false;
11985 /* Assume that a procedure whose body is not known has references
11986 to external arrays. */
11987 if (sym->attr.if_source != IFSRC_DECL)
11988 sym->attr.array_outer_dependency = 1;
11990 /* Compare the characteristics of a module procedure with the
11991 interface declaration. Ideally this would be done with
11992 gfc_compare_interfaces but, at present, the formal interface
11993 cannot be copied to the ts.interface. */
11994 if (sym->attr.module_procedure
11995 && sym->attr.if_source == IFSRC_DECL)
11997 gfc_symbol *iface;
11998 char name[2*GFC_MAX_SYMBOL_LEN + 1];
11999 char *module_name;
12000 char *submodule_name;
12001 strcpy (name, sym->ns->proc_name->name);
12002 module_name = strtok (name, ".");
12003 submodule_name = strtok (NULL, ".");
12005 /* Stop the dummy characteristics test from using the interface
12006 symbol instead of 'sym'. */
12007 iface = sym->ts.interface;
12008 sym->ts.interface = NULL;
12010 /* Make sure that the result uses the correct charlen for deferred
12011 length results. */
12012 if (iface && sym->result
12013 && iface->ts.type == BT_CHARACTER
12014 && iface->ts.deferred)
12015 sym->result->ts.u.cl = iface->ts.u.cl;
12017 if (iface == NULL)
12018 goto check_formal;
12020 /* Check the procedure characteristics. */
12021 if (sym->attr.elemental != iface->attr.elemental)
12023 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
12024 "PROCEDURE at %L and its interface in %s",
12025 &sym->declared_at, module_name);
12026 return false;
12029 if (sym->attr.pure != iface->attr.pure)
12031 gfc_error ("Mismatch in PURE attribute between MODULE "
12032 "PROCEDURE at %L and its interface in %s",
12033 &sym->declared_at, module_name);
12034 return false;
12037 if (sym->attr.recursive != iface->attr.recursive)
12039 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
12040 "PROCEDURE at %L and its interface in %s",
12041 &sym->declared_at, module_name);
12042 return false;
12045 /* Check the result characteristics. */
12046 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
12048 gfc_error ("%s between the MODULE PROCEDURE declaration "
12049 "in module %s and the declaration at %L in "
12050 "SUBMODULE %s", errmsg, module_name,
12051 &sym->declared_at, submodule_name);
12052 return false;
12055 check_formal:
12056 /* Check the charcateristics of the formal arguments. */
12057 if (sym->formal && sym->formal_ns)
12059 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
12061 new_formal = arg;
12062 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
12066 sym->ts.interface = iface;
12068 return true;
12072 /* Resolve a list of finalizer procedures. That is, after they have hopefully
12073 been defined and we now know their defined arguments, check that they fulfill
12074 the requirements of the standard for procedures used as finalizers. */
12076 static bool
12077 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
12079 gfc_finalizer* list;
12080 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
12081 bool result = true;
12082 bool seen_scalar = false;
12083 gfc_symbol *vtab;
12084 gfc_component *c;
12085 gfc_symbol *parent = gfc_get_derived_super_type (derived);
12087 if (parent)
12088 gfc_resolve_finalizers (parent, finalizable);
12090 /* Return early when not finalizable. Additionally, ensure that derived-type
12091 components have a their finalizables resolved. */
12092 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
12094 bool has_final = false;
12095 for (c = derived->components; c; c = c->next)
12096 if (c->ts.type == BT_DERIVED
12097 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
12099 bool has_final2 = false;
12100 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final))
12101 return false; /* Error. */
12102 has_final = has_final || has_final2;
12104 if (!has_final)
12106 if (finalizable)
12107 *finalizable = false;
12108 return true;
12112 /* Walk over the list of finalizer-procedures, check them, and if any one
12113 does not fit in with the standard's definition, print an error and remove
12114 it from the list. */
12115 prev_link = &derived->f2k_derived->finalizers;
12116 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
12118 gfc_formal_arglist *dummy_args;
12119 gfc_symbol* arg;
12120 gfc_finalizer* i;
12121 int my_rank;
12123 /* Skip this finalizer if we already resolved it. */
12124 if (list->proc_tree)
12126 prev_link = &(list->next);
12127 continue;
12130 /* Check this exists and is a SUBROUTINE. */
12131 if (!list->proc_sym->attr.subroutine)
12133 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
12134 list->proc_sym->name, &list->where);
12135 goto error;
12138 /* We should have exactly one argument. */
12139 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
12140 if (!dummy_args || dummy_args->next)
12142 gfc_error ("FINAL procedure at %L must have exactly one argument",
12143 &list->where);
12144 goto error;
12146 arg = dummy_args->sym;
12148 /* This argument must be of our type. */
12149 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
12151 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
12152 &arg->declared_at, derived->name);
12153 goto error;
12156 /* It must neither be a pointer nor allocatable nor optional. */
12157 if (arg->attr.pointer)
12159 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
12160 &arg->declared_at);
12161 goto error;
12163 if (arg->attr.allocatable)
12165 gfc_error ("Argument of FINAL procedure at %L must not be"
12166 " ALLOCATABLE", &arg->declared_at);
12167 goto error;
12169 if (arg->attr.optional)
12171 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
12172 &arg->declared_at);
12173 goto error;
12176 /* It must not be INTENT(OUT). */
12177 if (arg->attr.intent == INTENT_OUT)
12179 gfc_error ("Argument of FINAL procedure at %L must not be"
12180 " INTENT(OUT)", &arg->declared_at);
12181 goto error;
12184 /* Warn if the procedure is non-scalar and not assumed shape. */
12185 if (warn_surprising && arg->as && arg->as->rank != 0
12186 && arg->as->type != AS_ASSUMED_SHAPE)
12187 gfc_warning (OPT_Wsurprising,
12188 "Non-scalar FINAL procedure at %L should have assumed"
12189 " shape argument", &arg->declared_at);
12191 /* Check that it does not match in kind and rank with a FINAL procedure
12192 defined earlier. To really loop over the *earlier* declarations,
12193 we need to walk the tail of the list as new ones were pushed at the
12194 front. */
12195 /* TODO: Handle kind parameters once they are implemented. */
12196 my_rank = (arg->as ? arg->as->rank : 0);
12197 for (i = list->next; i; i = i->next)
12199 gfc_formal_arglist *dummy_args;
12201 /* Argument list might be empty; that is an error signalled earlier,
12202 but we nevertheless continued resolving. */
12203 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
12204 if (dummy_args)
12206 gfc_symbol* i_arg = dummy_args->sym;
12207 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
12208 if (i_rank == my_rank)
12210 gfc_error ("FINAL procedure %qs declared at %L has the same"
12211 " rank (%d) as %qs",
12212 list->proc_sym->name, &list->where, my_rank,
12213 i->proc_sym->name);
12214 goto error;
12219 /* Is this the/a scalar finalizer procedure? */
12220 if (!arg->as || arg->as->rank == 0)
12221 seen_scalar = true;
12223 /* Find the symtree for this procedure. */
12224 gcc_assert (!list->proc_tree);
12225 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
12227 prev_link = &list->next;
12228 continue;
12230 /* Remove wrong nodes immediately from the list so we don't risk any
12231 troubles in the future when they might fail later expectations. */
12232 error:
12233 i = list;
12234 *prev_link = list->next;
12235 gfc_free_finalizer (i);
12236 result = false;
12239 if (result == false)
12240 return false;
12242 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
12243 were nodes in the list, must have been for arrays. It is surely a good
12244 idea to have a scalar version there if there's something to finalize. */
12245 if (warn_surprising && result && !seen_scalar)
12246 gfc_warning (OPT_Wsurprising,
12247 "Only array FINAL procedures declared for derived type %qs"
12248 " defined at %L, suggest also scalar one",
12249 derived->name, &derived->declared_at);
12251 vtab = gfc_find_derived_vtab (derived);
12252 c = vtab->ts.u.derived->components->next->next->next->next->next;
12253 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
12255 if (finalizable)
12256 *finalizable = true;
12258 return true;
12262 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
12264 static bool
12265 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
12266 const char* generic_name, locus where)
12268 gfc_symbol *sym1, *sym2;
12269 const char *pass1, *pass2;
12270 gfc_formal_arglist *dummy_args;
12272 gcc_assert (t1->specific && t2->specific);
12273 gcc_assert (!t1->specific->is_generic);
12274 gcc_assert (!t2->specific->is_generic);
12275 gcc_assert (t1->is_operator == t2->is_operator);
12277 sym1 = t1->specific->u.specific->n.sym;
12278 sym2 = t2->specific->u.specific->n.sym;
12280 if (sym1 == sym2)
12281 return true;
12283 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
12284 if (sym1->attr.subroutine != sym2->attr.subroutine
12285 || sym1->attr.function != sym2->attr.function)
12287 gfc_error ("%qs and %qs can't be mixed FUNCTION/SUBROUTINE for"
12288 " GENERIC %qs at %L",
12289 sym1->name, sym2->name, generic_name, &where);
12290 return false;
12293 /* Determine PASS arguments. */
12294 if (t1->specific->nopass)
12295 pass1 = NULL;
12296 else if (t1->specific->pass_arg)
12297 pass1 = t1->specific->pass_arg;
12298 else
12300 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
12301 if (dummy_args)
12302 pass1 = dummy_args->sym->name;
12303 else
12304 pass1 = NULL;
12306 if (t2->specific->nopass)
12307 pass2 = NULL;
12308 else if (t2->specific->pass_arg)
12309 pass2 = t2->specific->pass_arg;
12310 else
12312 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
12313 if (dummy_args)
12314 pass2 = dummy_args->sym->name;
12315 else
12316 pass2 = NULL;
12319 /* Compare the interfaces. */
12320 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
12321 NULL, 0, pass1, pass2))
12323 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
12324 sym1->name, sym2->name, generic_name, &where);
12325 return false;
12328 return true;
12332 /* Worker function for resolving a generic procedure binding; this is used to
12333 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
12335 The difference between those cases is finding possible inherited bindings
12336 that are overridden, as one has to look for them in tb_sym_root,
12337 tb_uop_root or tb_op, respectively. Thus the caller must already find
12338 the super-type and set p->overridden correctly. */
12340 static bool
12341 resolve_tb_generic_targets (gfc_symbol* super_type,
12342 gfc_typebound_proc* p, const char* name)
12344 gfc_tbp_generic* target;
12345 gfc_symtree* first_target;
12346 gfc_symtree* inherited;
12348 gcc_assert (p && p->is_generic);
12350 /* Try to find the specific bindings for the symtrees in our target-list. */
12351 gcc_assert (p->u.generic);
12352 for (target = p->u.generic; target; target = target->next)
12353 if (!target->specific)
12355 gfc_typebound_proc* overridden_tbp;
12356 gfc_tbp_generic* g;
12357 const char* target_name;
12359 target_name = target->specific_st->name;
12361 /* Defined for this type directly. */
12362 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
12364 target->specific = target->specific_st->n.tb;
12365 goto specific_found;
12368 /* Look for an inherited specific binding. */
12369 if (super_type)
12371 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
12372 true, NULL);
12374 if (inherited)
12376 gcc_assert (inherited->n.tb);
12377 target->specific = inherited->n.tb;
12378 goto specific_found;
12382 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
12383 " at %L", target_name, name, &p->where);
12384 return false;
12386 /* Once we've found the specific binding, check it is not ambiguous with
12387 other specifics already found or inherited for the same GENERIC. */
12388 specific_found:
12389 gcc_assert (target->specific);
12391 /* This must really be a specific binding! */
12392 if (target->specific->is_generic)
12394 gfc_error ("GENERIC %qs at %L must target a specific binding,"
12395 " %qs is GENERIC, too", name, &p->where, target_name);
12396 return false;
12399 /* Check those already resolved on this type directly. */
12400 for (g = p->u.generic; g; g = g->next)
12401 if (g != target && g->specific
12402 && !check_generic_tbp_ambiguity (target, g, name, p->where))
12403 return false;
12405 /* Check for ambiguity with inherited specific targets. */
12406 for (overridden_tbp = p->overridden; overridden_tbp;
12407 overridden_tbp = overridden_tbp->overridden)
12408 if (overridden_tbp->is_generic)
12410 for (g = overridden_tbp->u.generic; g; g = g->next)
12412 gcc_assert (g->specific);
12413 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
12414 return false;
12419 /* If we attempt to "overwrite" a specific binding, this is an error. */
12420 if (p->overridden && !p->overridden->is_generic)
12422 gfc_error ("GENERIC %qs at %L can't overwrite specific binding with"
12423 " the same name", name, &p->where);
12424 return false;
12427 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
12428 all must have the same attributes here. */
12429 first_target = p->u.generic->specific->u.specific;
12430 gcc_assert (first_target);
12431 p->subroutine = first_target->n.sym->attr.subroutine;
12432 p->function = first_target->n.sym->attr.function;
12434 return true;
12438 /* Resolve a GENERIC procedure binding for a derived type. */
12440 static bool
12441 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
12443 gfc_symbol* super_type;
12445 /* Find the overridden binding if any. */
12446 st->n.tb->overridden = NULL;
12447 super_type = gfc_get_derived_super_type (derived);
12448 if (super_type)
12450 gfc_symtree* overridden;
12451 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
12452 true, NULL);
12454 if (overridden && overridden->n.tb)
12455 st->n.tb->overridden = overridden->n.tb;
12458 /* Resolve using worker function. */
12459 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
12463 /* Retrieve the target-procedure of an operator binding and do some checks in
12464 common for intrinsic and user-defined type-bound operators. */
12466 static gfc_symbol*
12467 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
12469 gfc_symbol* target_proc;
12471 gcc_assert (target->specific && !target->specific->is_generic);
12472 target_proc = target->specific->u.specific->n.sym;
12473 gcc_assert (target_proc);
12475 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
12476 if (target->specific->nopass)
12478 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
12479 return NULL;
12482 return target_proc;
12486 /* Resolve a type-bound intrinsic operator. */
12488 static bool
12489 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
12490 gfc_typebound_proc* p)
12492 gfc_symbol* super_type;
12493 gfc_tbp_generic* target;
12495 /* If there's already an error here, do nothing (but don't fail again). */
12496 if (p->error)
12497 return true;
12499 /* Operators should always be GENERIC bindings. */
12500 gcc_assert (p->is_generic);
12502 /* Look for an overridden binding. */
12503 super_type = gfc_get_derived_super_type (derived);
12504 if (super_type && super_type->f2k_derived)
12505 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
12506 op, true, NULL);
12507 else
12508 p->overridden = NULL;
12510 /* Resolve general GENERIC properties using worker function. */
12511 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
12512 goto error;
12514 /* Check the targets to be procedures of correct interface. */
12515 for (target = p->u.generic; target; target = target->next)
12517 gfc_symbol* target_proc;
12519 target_proc = get_checked_tb_operator_target (target, p->where);
12520 if (!target_proc)
12521 goto error;
12523 if (!gfc_check_operator_interface (target_proc, op, p->where))
12524 goto error;
12526 /* Add target to non-typebound operator list. */
12527 if (!target->specific->deferred && !derived->attr.use_assoc
12528 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
12530 gfc_interface *head, *intr;
12531 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
12532 return false;
12533 head = derived->ns->op[op];
12534 intr = gfc_get_interface ();
12535 intr->sym = target_proc;
12536 intr->where = p->where;
12537 intr->next = head;
12538 derived->ns->op[op] = intr;
12542 return true;
12544 error:
12545 p->error = 1;
12546 return false;
12550 /* Resolve a type-bound user operator (tree-walker callback). */
12552 static gfc_symbol* resolve_bindings_derived;
12553 static bool resolve_bindings_result;
12555 static bool check_uop_procedure (gfc_symbol* sym, locus where);
12557 static void
12558 resolve_typebound_user_op (gfc_symtree* stree)
12560 gfc_symbol* super_type;
12561 gfc_tbp_generic* target;
12563 gcc_assert (stree && stree->n.tb);
12565 if (stree->n.tb->error)
12566 return;
12568 /* Operators should always be GENERIC bindings. */
12569 gcc_assert (stree->n.tb->is_generic);
12571 /* Find overridden procedure, if any. */
12572 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12573 if (super_type && super_type->f2k_derived)
12575 gfc_symtree* overridden;
12576 overridden = gfc_find_typebound_user_op (super_type, NULL,
12577 stree->name, true, NULL);
12579 if (overridden && overridden->n.tb)
12580 stree->n.tb->overridden = overridden->n.tb;
12582 else
12583 stree->n.tb->overridden = NULL;
12585 /* Resolve basically using worker function. */
12586 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
12587 goto error;
12589 /* Check the targets to be functions of correct interface. */
12590 for (target = stree->n.tb->u.generic; target; target = target->next)
12592 gfc_symbol* target_proc;
12594 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
12595 if (!target_proc)
12596 goto error;
12598 if (!check_uop_procedure (target_proc, stree->n.tb->where))
12599 goto error;
12602 return;
12604 error:
12605 resolve_bindings_result = false;
12606 stree->n.tb->error = 1;
12610 /* Resolve the type-bound procedures for a derived type. */
12612 static void
12613 resolve_typebound_procedure (gfc_symtree* stree)
12615 gfc_symbol* proc;
12616 locus where;
12617 gfc_symbol* me_arg;
12618 gfc_symbol* super_type;
12619 gfc_component* comp;
12621 gcc_assert (stree);
12623 /* Undefined specific symbol from GENERIC target definition. */
12624 if (!stree->n.tb)
12625 return;
12627 if (stree->n.tb->error)
12628 return;
12630 /* If this is a GENERIC binding, use that routine. */
12631 if (stree->n.tb->is_generic)
12633 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
12634 goto error;
12635 return;
12638 /* Get the target-procedure to check it. */
12639 gcc_assert (!stree->n.tb->is_generic);
12640 gcc_assert (stree->n.tb->u.specific);
12641 proc = stree->n.tb->u.specific->n.sym;
12642 where = stree->n.tb->where;
12644 /* Default access should already be resolved from the parser. */
12645 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
12647 if (stree->n.tb->deferred)
12649 if (!check_proc_interface (proc, &where))
12650 goto error;
12652 else
12654 /* Check for F08:C465. */
12655 if ((!proc->attr.subroutine && !proc->attr.function)
12656 || (proc->attr.proc != PROC_MODULE
12657 && proc->attr.if_source != IFSRC_IFBODY)
12658 || proc->attr.abstract)
12660 gfc_error ("%qs must be a module procedure or an external procedure with"
12661 " an explicit interface at %L", proc->name, &where);
12662 goto error;
12666 stree->n.tb->subroutine = proc->attr.subroutine;
12667 stree->n.tb->function = proc->attr.function;
12669 /* Find the super-type of the current derived type. We could do this once and
12670 store in a global if speed is needed, but as long as not I believe this is
12671 more readable and clearer. */
12672 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12674 /* If PASS, resolve and check arguments if not already resolved / loaded
12675 from a .mod file. */
12676 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
12678 gfc_formal_arglist *dummy_args;
12680 dummy_args = gfc_sym_get_dummy_args (proc);
12681 if (stree->n.tb->pass_arg)
12683 gfc_formal_arglist *i;
12685 /* If an explicit passing argument name is given, walk the arg-list
12686 and look for it. */
12688 me_arg = NULL;
12689 stree->n.tb->pass_arg_num = 1;
12690 for (i = dummy_args; i; i = i->next)
12692 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
12694 me_arg = i->sym;
12695 break;
12697 ++stree->n.tb->pass_arg_num;
12700 if (!me_arg)
12702 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
12703 " argument %qs",
12704 proc->name, stree->n.tb->pass_arg, &where,
12705 stree->n.tb->pass_arg);
12706 goto error;
12709 else
12711 /* Otherwise, take the first one; there should in fact be at least
12712 one. */
12713 stree->n.tb->pass_arg_num = 1;
12714 if (!dummy_args)
12716 gfc_error ("Procedure %qs with PASS at %L must have at"
12717 " least one argument", proc->name, &where);
12718 goto error;
12720 me_arg = dummy_args->sym;
12723 /* Now check that the argument-type matches and the passed-object
12724 dummy argument is generally fine. */
12726 gcc_assert (me_arg);
12728 if (me_arg->ts.type != BT_CLASS)
12730 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
12731 " at %L", proc->name, &where);
12732 goto error;
12735 if (CLASS_DATA (me_arg)->ts.u.derived
12736 != resolve_bindings_derived)
12738 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
12739 " the derived-type %qs", me_arg->name, proc->name,
12740 me_arg->name, &where, resolve_bindings_derived->name);
12741 goto error;
12744 gcc_assert (me_arg->ts.type == BT_CLASS);
12745 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
12747 gfc_error ("Passed-object dummy argument of %qs at %L must be"
12748 " scalar", proc->name, &where);
12749 goto error;
12751 if (CLASS_DATA (me_arg)->attr.allocatable)
12753 gfc_error ("Passed-object dummy argument of %qs at %L must not"
12754 " be ALLOCATABLE", proc->name, &where);
12755 goto error;
12757 if (CLASS_DATA (me_arg)->attr.class_pointer)
12759 gfc_error ("Passed-object dummy argument of %qs at %L must not"
12760 " be POINTER", proc->name, &where);
12761 goto error;
12765 /* If we are extending some type, check that we don't override a procedure
12766 flagged NON_OVERRIDABLE. */
12767 stree->n.tb->overridden = NULL;
12768 if (super_type)
12770 gfc_symtree* overridden;
12771 overridden = gfc_find_typebound_proc (super_type, NULL,
12772 stree->name, true, NULL);
12774 if (overridden)
12776 if (overridden->n.tb)
12777 stree->n.tb->overridden = overridden->n.tb;
12779 if (!gfc_check_typebound_override (stree, overridden))
12780 goto error;
12784 /* See if there's a name collision with a component directly in this type. */
12785 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
12786 if (!strcmp (comp->name, stree->name))
12788 gfc_error ("Procedure %qs at %L has the same name as a component of"
12789 " %qs",
12790 stree->name, &where, resolve_bindings_derived->name);
12791 goto error;
12794 /* Try to find a name collision with an inherited component. */
12795 if (super_type && gfc_find_component (super_type, stree->name, true, true,
12796 NULL))
12798 gfc_error ("Procedure %qs at %L has the same name as an inherited"
12799 " component of %qs",
12800 stree->name, &where, resolve_bindings_derived->name);
12801 goto error;
12804 stree->n.tb->error = 0;
12805 return;
12807 error:
12808 resolve_bindings_result = false;
12809 stree->n.tb->error = 1;
12813 static bool
12814 resolve_typebound_procedures (gfc_symbol* derived)
12816 int op;
12817 gfc_symbol* super_type;
12819 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
12820 return true;
12822 super_type = gfc_get_derived_super_type (derived);
12823 if (super_type)
12824 resolve_symbol (super_type);
12826 resolve_bindings_derived = derived;
12827 resolve_bindings_result = true;
12829 if (derived->f2k_derived->tb_sym_root)
12830 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
12831 &resolve_typebound_procedure);
12833 if (derived->f2k_derived->tb_uop_root)
12834 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
12835 &resolve_typebound_user_op);
12837 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
12839 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
12840 if (p && !resolve_typebound_intrinsic_op (derived,
12841 (gfc_intrinsic_op)op, p))
12842 resolve_bindings_result = false;
12845 return resolve_bindings_result;
12849 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
12850 to give all identical derived types the same backend_decl. */
12851 static void
12852 add_dt_to_dt_list (gfc_symbol *derived)
12854 gfc_dt_list *dt_list;
12856 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
12857 if (derived == dt_list->derived)
12858 return;
12860 dt_list = gfc_get_dt_list ();
12861 dt_list->next = gfc_derived_types;
12862 dt_list->derived = derived;
12863 gfc_derived_types = dt_list;
12867 /* Ensure that a derived-type is really not abstract, meaning that every
12868 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
12870 static bool
12871 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
12873 if (!st)
12874 return true;
12876 if (!ensure_not_abstract_walker (sub, st->left))
12877 return false;
12878 if (!ensure_not_abstract_walker (sub, st->right))
12879 return false;
12881 if (st->n.tb && st->n.tb->deferred)
12883 gfc_symtree* overriding;
12884 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
12885 if (!overriding)
12886 return false;
12887 gcc_assert (overriding->n.tb);
12888 if (overriding->n.tb->deferred)
12890 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
12891 " %qs is DEFERRED and not overridden",
12892 sub->name, &sub->declared_at, st->name);
12893 return false;
12897 return true;
12900 static bool
12901 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
12903 /* The algorithm used here is to recursively travel up the ancestry of sub
12904 and for each ancestor-type, check all bindings. If any of them is
12905 DEFERRED, look it up starting from sub and see if the found (overriding)
12906 binding is not DEFERRED.
12907 This is not the most efficient way to do this, but it should be ok and is
12908 clearer than something sophisticated. */
12910 gcc_assert (ancestor && !sub->attr.abstract);
12912 if (!ancestor->attr.abstract)
12913 return true;
12915 /* Walk bindings of this ancestor. */
12916 if (ancestor->f2k_derived)
12918 bool t;
12919 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
12920 if (!t)
12921 return false;
12924 /* Find next ancestor type and recurse on it. */
12925 ancestor = gfc_get_derived_super_type (ancestor);
12926 if (ancestor)
12927 return ensure_not_abstract (sub, ancestor);
12929 return true;
12933 /* This check for typebound defined assignments is done recursively
12934 since the order in which derived types are resolved is not always in
12935 order of the declarations. */
12937 static void
12938 check_defined_assignments (gfc_symbol *derived)
12940 gfc_component *c;
12942 for (c = derived->components; c; c = c->next)
12944 if (!gfc_bt_struct (c->ts.type)
12945 || c->attr.pointer
12946 || c->attr.allocatable
12947 || c->attr.proc_pointer_comp
12948 || c->attr.class_pointer
12949 || c->attr.proc_pointer)
12950 continue;
12952 if (c->ts.u.derived->attr.defined_assign_comp
12953 || (c->ts.u.derived->f2k_derived
12954 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
12956 derived->attr.defined_assign_comp = 1;
12957 return;
12960 check_defined_assignments (c->ts.u.derived);
12961 if (c->ts.u.derived->attr.defined_assign_comp)
12963 derived->attr.defined_assign_comp = 1;
12964 return;
12970 /* Resolve a single component of a derived type or structure. */
12972 static bool
12973 resolve_component (gfc_component *c, gfc_symbol *sym)
12975 gfc_symbol *super_type;
12977 if (c->attr.artificial)
12978 return true;
12980 /* F2008, C442. */
12981 if ((!sym->attr.is_class || c != sym->components)
12982 && c->attr.codimension
12983 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
12985 gfc_error ("Coarray component %qs at %L must be allocatable with "
12986 "deferred shape", c->name, &c->loc);
12987 return false;
12990 /* F2008, C443. */
12991 if (c->attr.codimension && c->ts.type == BT_DERIVED
12992 && c->ts.u.derived->ts.is_iso_c)
12994 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12995 "shall not be a coarray", c->name, &c->loc);
12996 return false;
12999 /* F2008, C444. */
13000 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
13001 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
13002 || c->attr.allocatable))
13004 gfc_error ("Component %qs at %L with coarray component "
13005 "shall be a nonpointer, nonallocatable scalar",
13006 c->name, &c->loc);
13007 return false;
13010 /* F2008, C448. */
13011 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
13013 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
13014 "is not an array pointer", c->name, &c->loc);
13015 return false;
13018 if (c->attr.proc_pointer && c->ts.interface)
13020 gfc_symbol *ifc = c->ts.interface;
13022 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
13024 c->tb->error = 1;
13025 return false;
13028 if (ifc->attr.if_source || ifc->attr.intrinsic)
13030 /* Resolve interface and copy attributes. */
13031 if (ifc->formal && !ifc->formal_ns)
13032 resolve_symbol (ifc);
13033 if (ifc->attr.intrinsic)
13034 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
13036 if (ifc->result)
13038 c->ts = ifc->result->ts;
13039 c->attr.allocatable = ifc->result->attr.allocatable;
13040 c->attr.pointer = ifc->result->attr.pointer;
13041 c->attr.dimension = ifc->result->attr.dimension;
13042 c->as = gfc_copy_array_spec (ifc->result->as);
13043 c->attr.class_ok = ifc->result->attr.class_ok;
13045 else
13047 c->ts = ifc->ts;
13048 c->attr.allocatable = ifc->attr.allocatable;
13049 c->attr.pointer = ifc->attr.pointer;
13050 c->attr.dimension = ifc->attr.dimension;
13051 c->as = gfc_copy_array_spec (ifc->as);
13052 c->attr.class_ok = ifc->attr.class_ok;
13054 c->ts.interface = ifc;
13055 c->attr.function = ifc->attr.function;
13056 c->attr.subroutine = ifc->attr.subroutine;
13058 c->attr.pure = ifc->attr.pure;
13059 c->attr.elemental = ifc->attr.elemental;
13060 c->attr.recursive = ifc->attr.recursive;
13061 c->attr.always_explicit = ifc->attr.always_explicit;
13062 c->attr.ext_attr |= ifc->attr.ext_attr;
13063 /* Copy char length. */
13064 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
13066 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
13067 if (cl->length && !cl->resolved
13068 && !gfc_resolve_expr (cl->length))
13070 c->tb->error = 1;
13071 return false;
13073 c->ts.u.cl = cl;
13077 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
13079 /* Since PPCs are not implicitly typed, a PPC without an explicit
13080 interface must be a subroutine. */
13081 gfc_add_subroutine (&c->attr, c->name, &c->loc);
13084 /* Procedure pointer components: Check PASS arg. */
13085 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
13086 && !sym->attr.vtype)
13088 gfc_symbol* me_arg;
13090 if (c->tb->pass_arg)
13092 gfc_formal_arglist* i;
13094 /* If an explicit passing argument name is given, walk the arg-list
13095 and look for it. */
13097 me_arg = NULL;
13098 c->tb->pass_arg_num = 1;
13099 for (i = c->ts.interface->formal; i; i = i->next)
13101 if (!strcmp (i->sym->name, c->tb->pass_arg))
13103 me_arg = i->sym;
13104 break;
13106 c->tb->pass_arg_num++;
13109 if (!me_arg)
13111 gfc_error ("Procedure pointer component %qs with PASS(%s) "
13112 "at %L has no argument %qs", c->name,
13113 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
13114 c->tb->error = 1;
13115 return false;
13118 else
13120 /* Otherwise, take the first one; there should in fact be at least
13121 one. */
13122 c->tb->pass_arg_num = 1;
13123 if (!c->ts.interface->formal)
13125 gfc_error ("Procedure pointer component %qs with PASS at %L "
13126 "must have at least one argument",
13127 c->name, &c->loc);
13128 c->tb->error = 1;
13129 return false;
13131 me_arg = c->ts.interface->formal->sym;
13134 /* Now check that the argument-type matches. */
13135 gcc_assert (me_arg);
13136 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
13137 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
13138 || (me_arg->ts.type == BT_CLASS
13139 && CLASS_DATA (me_arg)->ts.u.derived != sym))
13141 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
13142 " the derived type %qs", me_arg->name, c->name,
13143 me_arg->name, &c->loc, sym->name);
13144 c->tb->error = 1;
13145 return false;
13148 /* Check for C453. */
13149 if (me_arg->attr.dimension)
13151 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13152 "must be scalar", me_arg->name, c->name, me_arg->name,
13153 &c->loc);
13154 c->tb->error = 1;
13155 return false;
13158 if (me_arg->attr.pointer)
13160 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13161 "may not have the POINTER attribute", me_arg->name,
13162 c->name, me_arg->name, &c->loc);
13163 c->tb->error = 1;
13164 return false;
13167 if (me_arg->attr.allocatable)
13169 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13170 "may not be ALLOCATABLE", me_arg->name, c->name,
13171 me_arg->name, &c->loc);
13172 c->tb->error = 1;
13173 return false;
13176 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
13178 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
13179 " at %L", c->name, &c->loc);
13180 return false;
13185 /* Check type-spec if this is not the parent-type component. */
13186 if (((sym->attr.is_class
13187 && (!sym->components->ts.u.derived->attr.extension
13188 || c != sym->components->ts.u.derived->components))
13189 || (!sym->attr.is_class
13190 && (!sym->attr.extension || c != sym->components)))
13191 && !sym->attr.vtype
13192 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
13193 return false;
13195 super_type = gfc_get_derived_super_type (sym);
13197 /* If this type is an extension, set the accessibility of the parent
13198 component. */
13199 if (super_type
13200 && ((sym->attr.is_class
13201 && c == sym->components->ts.u.derived->components)
13202 || (!sym->attr.is_class && c == sym->components))
13203 && strcmp (super_type->name, c->name) == 0)
13204 c->attr.access = super_type->attr.access;
13206 /* If this type is an extension, see if this component has the same name
13207 as an inherited type-bound procedure. */
13208 if (super_type && !sym->attr.is_class
13209 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
13211 gfc_error ("Component %qs of %qs at %L has the same name as an"
13212 " inherited type-bound procedure",
13213 c->name, sym->name, &c->loc);
13214 return false;
13217 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
13218 && !c->ts.deferred)
13220 if (c->ts.u.cl->length == NULL
13221 || (!resolve_charlen(c->ts.u.cl))
13222 || !gfc_is_constant_expr (c->ts.u.cl->length))
13224 gfc_error ("Character length of component %qs needs to "
13225 "be a constant specification expression at %L",
13226 c->name,
13227 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
13228 return false;
13232 if (c->ts.type == BT_CHARACTER && c->ts.deferred
13233 && !c->attr.pointer && !c->attr.allocatable)
13235 gfc_error ("Character component %qs of %qs at %L with deferred "
13236 "length must be a POINTER or ALLOCATABLE",
13237 c->name, sym->name, &c->loc);
13238 return false;
13241 /* Add the hidden deferred length field. */
13242 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function
13243 && !sym->attr.is_class)
13245 char name[GFC_MAX_SYMBOL_LEN+9];
13246 gfc_component *strlen;
13247 sprintf (name, "_%s_length", c->name);
13248 strlen = gfc_find_component (sym, name, true, true, NULL);
13249 if (strlen == NULL)
13251 if (!gfc_add_component (sym, name, &strlen))
13252 return false;
13253 strlen->ts.type = BT_INTEGER;
13254 strlen->ts.kind = gfc_charlen_int_kind;
13255 strlen->attr.access = ACCESS_PRIVATE;
13256 strlen->attr.artificial = 1;
13260 if (c->ts.type == BT_DERIVED
13261 && sym->component_access != ACCESS_PRIVATE
13262 && gfc_check_symbol_access (sym)
13263 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
13264 && !c->ts.u.derived->attr.use_assoc
13265 && !gfc_check_symbol_access (c->ts.u.derived)
13266 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
13267 "PRIVATE type and cannot be a component of "
13268 "%qs, which is PUBLIC at %L", c->name,
13269 sym->name, &sym->declared_at))
13270 return false;
13272 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
13274 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
13275 "type %s", c->name, &c->loc, sym->name);
13276 return false;
13279 if (sym->attr.sequence)
13281 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
13283 gfc_error ("Component %s of SEQUENCE type declared at %L does "
13284 "not have the SEQUENCE attribute",
13285 c->ts.u.derived->name, &sym->declared_at);
13286 return false;
13290 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
13291 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
13292 else if (c->ts.type == BT_CLASS && c->attr.class_ok
13293 && CLASS_DATA (c)->ts.u.derived->attr.generic)
13294 CLASS_DATA (c)->ts.u.derived
13295 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
13297 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
13298 && c->attr.pointer && c->ts.u.derived->components == NULL
13299 && !c->ts.u.derived->attr.zero_comp)
13301 gfc_error ("The pointer component %qs of %qs at %L is a type "
13302 "that has not been declared", c->name, sym->name,
13303 &c->loc);
13304 return false;
13307 if (c->ts.type == BT_CLASS && c->attr.class_ok
13308 && CLASS_DATA (c)->attr.class_pointer
13309 && CLASS_DATA (c)->ts.u.derived->components == NULL
13310 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
13311 && !UNLIMITED_POLY (c))
13313 gfc_error ("The pointer component %qs of %qs at %L is a type "
13314 "that has not been declared", c->name, sym->name,
13315 &c->loc);
13316 return false;
13319 /* C437. */
13320 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
13321 && (!c->attr.class_ok
13322 || !(CLASS_DATA (c)->attr.class_pointer
13323 || CLASS_DATA (c)->attr.allocatable)))
13325 gfc_error ("Component %qs with CLASS at %L must be allocatable "
13326 "or pointer", c->name, &c->loc);
13327 /* Prevent a recurrence of the error. */
13328 c->ts.type = BT_UNKNOWN;
13329 return false;
13332 /* Ensure that all the derived type components are put on the
13333 derived type list; even in formal namespaces, where derived type
13334 pointer components might not have been declared. */
13335 if (c->ts.type == BT_DERIVED
13336 && c->ts.u.derived
13337 && c->ts.u.derived->components
13338 && c->attr.pointer
13339 && sym != c->ts.u.derived)
13340 add_dt_to_dt_list (c->ts.u.derived);
13342 if (!gfc_resolve_array_spec (c->as,
13343 !(c->attr.pointer || c->attr.proc_pointer
13344 || c->attr.allocatable)))
13345 return false;
13347 if (c->initializer && !sym->attr.vtype
13348 && !gfc_check_assign_symbol (sym, c, c->initializer))
13349 return false;
13351 return true;
13355 /* Be nice about the locus for a structure expression - show the locus of the
13356 first non-null sub-expression if we can. */
13358 static locus *
13359 cons_where (gfc_expr *struct_expr)
13361 gfc_constructor *cons;
13363 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
13365 cons = gfc_constructor_first (struct_expr->value.constructor);
13366 for (; cons; cons = gfc_constructor_next (cons))
13368 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
13369 return &cons->expr->where;
13372 return &struct_expr->where;
13375 /* Resolve the components of a structure type. Much less work than derived
13376 types. */
13378 static bool
13379 resolve_fl_struct (gfc_symbol *sym)
13381 gfc_component *c;
13382 gfc_expr *init = NULL;
13383 bool success;
13385 /* Make sure UNIONs do not have overlapping initializers. */
13386 if (sym->attr.flavor == FL_UNION)
13388 for (c = sym->components; c; c = c->next)
13390 if (init && c->initializer)
13392 gfc_error ("Conflicting initializers in union at %L and %L",
13393 cons_where (init), cons_where (c->initializer));
13394 gfc_free_expr (c->initializer);
13395 c->initializer = NULL;
13397 if (init == NULL)
13398 init = c->initializer;
13402 success = true;
13403 for (c = sym->components; c; c = c->next)
13404 if (!resolve_component (c, sym))
13405 success = false;
13407 if (!success)
13408 return false;
13410 if (sym->components)
13411 add_dt_to_dt_list (sym);
13413 return true;
13417 /* Resolve the components of a derived type. This does not have to wait until
13418 resolution stage, but can be done as soon as the dt declaration has been
13419 parsed. */
13421 static bool
13422 resolve_fl_derived0 (gfc_symbol *sym)
13424 gfc_symbol* super_type;
13425 gfc_component *c;
13426 bool success;
13428 if (sym->attr.unlimited_polymorphic)
13429 return true;
13431 super_type = gfc_get_derived_super_type (sym);
13433 /* F2008, C432. */
13434 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
13436 gfc_error ("As extending type %qs at %L has a coarray component, "
13437 "parent type %qs shall also have one", sym->name,
13438 &sym->declared_at, super_type->name);
13439 return false;
13442 /* Ensure the extended type gets resolved before we do. */
13443 if (super_type && !resolve_fl_derived0 (super_type))
13444 return false;
13446 /* An ABSTRACT type must be extensible. */
13447 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
13449 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
13450 sym->name, &sym->declared_at);
13451 return false;
13454 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
13455 : sym->components;
13457 success = true;
13458 for ( ; c != NULL; c = c->next)
13459 if (!resolve_component (c, sym))
13460 success = false;
13462 if (!success)
13463 return false;
13465 check_defined_assignments (sym);
13467 if (!sym->attr.defined_assign_comp && super_type)
13468 sym->attr.defined_assign_comp
13469 = super_type->attr.defined_assign_comp;
13471 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
13472 all DEFERRED bindings are overridden. */
13473 if (super_type && super_type->attr.abstract && !sym->attr.abstract
13474 && !sym->attr.is_class
13475 && !ensure_not_abstract (sym, super_type))
13476 return false;
13478 /* Add derived type to the derived type list. */
13479 add_dt_to_dt_list (sym);
13481 return true;
13485 /* The following procedure does the full resolution of a derived type,
13486 including resolution of all type-bound procedures (if present). In contrast
13487 to 'resolve_fl_derived0' this can only be done after the module has been
13488 parsed completely. */
13490 static bool
13491 resolve_fl_derived (gfc_symbol *sym)
13493 gfc_symbol *gen_dt = NULL;
13495 if (sym->attr.unlimited_polymorphic)
13496 return true;
13498 if (!sym->attr.is_class)
13499 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
13500 if (gen_dt && gen_dt->generic && gen_dt->generic->next
13501 && (!gen_dt->generic->sym->attr.use_assoc
13502 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
13503 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
13504 "%qs at %L being the same name as derived "
13505 "type at %L", sym->name,
13506 gen_dt->generic->sym == sym
13507 ? gen_dt->generic->next->sym->name
13508 : gen_dt->generic->sym->name,
13509 gen_dt->generic->sym == sym
13510 ? &gen_dt->generic->next->sym->declared_at
13511 : &gen_dt->generic->sym->declared_at,
13512 &sym->declared_at))
13513 return false;
13515 /* Resolve the finalizer procedures. */
13516 if (!gfc_resolve_finalizers (sym, NULL))
13517 return false;
13519 if (sym->attr.is_class && sym->ts.u.derived == NULL)
13521 /* Fix up incomplete CLASS symbols. */
13522 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
13523 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
13525 /* Nothing more to do for unlimited polymorphic entities. */
13526 if (data->ts.u.derived->attr.unlimited_polymorphic)
13527 return true;
13528 else if (vptr->ts.u.derived == NULL)
13530 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
13531 gcc_assert (vtab);
13532 vptr->ts.u.derived = vtab->ts.u.derived;
13536 if (!resolve_fl_derived0 (sym))
13537 return false;
13539 /* Resolve the type-bound procedures. */
13540 if (!resolve_typebound_procedures (sym))
13541 return false;
13543 return true;
13547 /* Check for formatted read and write DTIO procedures. */
13549 static bool
13550 dtio_procs_present (gfc_symbol *sym)
13552 gfc_symbol *derived;
13554 if (sym->ts.type == BT_CLASS)
13555 derived = CLASS_DATA (sym)->ts.u.derived;
13556 else if (sym->ts.type == BT_DERIVED)
13557 derived = sym->ts.u.derived;
13558 else
13559 return false;
13561 return gfc_find_specific_dtio_proc (derived, true, true) != NULL
13562 && gfc_find_specific_dtio_proc (derived, false, true) != NULL;
13566 static bool
13567 resolve_fl_namelist (gfc_symbol *sym)
13569 gfc_namelist *nl;
13570 gfc_symbol *nlsym;
13571 bool dtio;
13573 for (nl = sym->namelist; nl; nl = nl->next)
13575 /* Check again, the check in match only works if NAMELIST comes
13576 after the decl. */
13577 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
13579 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
13580 "allowed", nl->sym->name, sym->name, &sym->declared_at);
13581 return false;
13584 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
13585 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13586 "with assumed shape in namelist %qs at %L",
13587 nl->sym->name, sym->name, &sym->declared_at))
13588 return false;
13590 if (is_non_constant_shape_array (nl->sym)
13591 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13592 "with nonconstant shape in namelist %qs at %L",
13593 nl->sym->name, sym->name, &sym->declared_at))
13594 return false;
13596 if (nl->sym->ts.type == BT_CHARACTER
13597 && (nl->sym->ts.u.cl->length == NULL
13598 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
13599 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
13600 "nonconstant character length in "
13601 "namelist %qs at %L", nl->sym->name,
13602 sym->name, &sym->declared_at))
13603 return false;
13605 dtio = dtio_procs_present (nl->sym);
13607 if (nl->sym->ts.type == BT_CLASS && !dtio)
13609 gfc_error ("NAMELIST object %qs in namelist %qs at %L is "
13610 "polymorphic and requires a defined input/output "
13611 "procedure", nl->sym->name, sym->name, &sym->declared_at);
13612 return false;
13615 if (nl->sym->ts.type == BT_DERIVED
13616 && (nl->sym->ts.u.derived->attr.alloc_comp
13617 || nl->sym->ts.u.derived->attr.pointer_comp))
13619 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
13620 "namelist %qs at %L with ALLOCATABLE "
13621 "or POINTER components", nl->sym->name,
13622 sym->name, &sym->declared_at))
13623 return false;
13625 if (!dtio)
13627 gfc_error ("NAMELIST object %qs in namelist %qs at %L has "
13628 "ALLOCATABLE or POINTER components and thus requires "
13629 "a defined input/output procedure", nl->sym->name,
13630 sym->name, &sym->declared_at);
13631 return false;
13636 /* Reject PRIVATE objects in a PUBLIC namelist. */
13637 if (gfc_check_symbol_access (sym))
13639 for (nl = sym->namelist; nl; nl = nl->next)
13641 if (!nl->sym->attr.use_assoc
13642 && !is_sym_host_assoc (nl->sym, sym->ns)
13643 && !gfc_check_symbol_access (nl->sym))
13645 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
13646 "cannot be member of PUBLIC namelist %qs at %L",
13647 nl->sym->name, sym->name, &sym->declared_at);
13648 return false;
13651 /* If the derived type has specific DTIO procedures for both read and
13652 write then namelist objects with private components are OK. */
13653 if (dtio_procs_present (nl->sym))
13654 continue;
13656 /* Types with private components that came here by USE-association. */
13657 if (nl->sym->ts.type == BT_DERIVED
13658 && derived_inaccessible (nl->sym->ts.u.derived))
13660 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
13661 "components and cannot be member of namelist %qs at %L",
13662 nl->sym->name, sym->name, &sym->declared_at);
13663 return false;
13666 /* Types with private components that are defined in the same module. */
13667 if (nl->sym->ts.type == BT_DERIVED
13668 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
13669 && nl->sym->ts.u.derived->attr.private_comp)
13671 gfc_error ("NAMELIST object %qs has PRIVATE components and "
13672 "cannot be a member of PUBLIC namelist %qs at %L",
13673 nl->sym->name, sym->name, &sym->declared_at);
13674 return false;
13680 /* 14.1.2 A module or internal procedure represent local entities
13681 of the same type as a namelist member and so are not allowed. */
13682 for (nl = sym->namelist; nl; nl = nl->next)
13684 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
13685 continue;
13687 if (nl->sym->attr.function && nl->sym == nl->sym->result)
13688 if ((nl->sym == sym->ns->proc_name)
13690 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
13691 continue;
13693 nlsym = NULL;
13694 if (nl->sym->name)
13695 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
13696 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
13698 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
13699 "attribute in %qs at %L", nlsym->name,
13700 &sym->declared_at);
13701 return false;
13705 return true;
13709 static bool
13710 resolve_fl_parameter (gfc_symbol *sym)
13712 /* A parameter array's shape needs to be constant. */
13713 if (sym->as != NULL
13714 && (sym->as->type == AS_DEFERRED
13715 || is_non_constant_shape_array (sym)))
13717 gfc_error ("Parameter array %qs at %L cannot be automatic "
13718 "or of deferred shape", sym->name, &sym->declared_at);
13719 return false;
13722 /* Constraints on deferred type parameter. */
13723 if (!deferred_requirements (sym))
13724 return false;
13726 /* Make sure a parameter that has been implicitly typed still
13727 matches the implicit type, since PARAMETER statements can precede
13728 IMPLICIT statements. */
13729 if (sym->attr.implicit_type
13730 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
13731 sym->ns)))
13733 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
13734 "later IMPLICIT type", sym->name, &sym->declared_at);
13735 return false;
13738 /* Make sure the types of derived parameters are consistent. This
13739 type checking is deferred until resolution because the type may
13740 refer to a derived type from the host. */
13741 if (sym->ts.type == BT_DERIVED
13742 && !gfc_compare_types (&sym->ts, &sym->value->ts))
13744 gfc_error ("Incompatible derived type in PARAMETER at %L",
13745 &sym->value->where);
13746 return false;
13748 return true;
13752 /* Do anything necessary to resolve a symbol. Right now, we just
13753 assume that an otherwise unknown symbol is a variable. This sort
13754 of thing commonly happens for symbols in module. */
13756 static void
13757 resolve_symbol (gfc_symbol *sym)
13759 int check_constant, mp_flag;
13760 gfc_symtree *symtree;
13761 gfc_symtree *this_symtree;
13762 gfc_namespace *ns;
13763 gfc_component *c;
13764 symbol_attribute class_attr;
13765 gfc_array_spec *as;
13766 bool saved_specification_expr;
13768 if (sym->resolved)
13769 return;
13770 sym->resolved = 1;
13772 /* No symbol will ever have union type; only components can be unions.
13773 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
13774 (just like derived type declaration symbols have flavor FL_DERIVED). */
13775 gcc_assert (sym->ts.type != BT_UNION);
13777 if (sym->attr.artificial)
13778 return;
13780 if (sym->attr.unlimited_polymorphic)
13781 return;
13783 if (sym->attr.flavor == FL_UNKNOWN
13784 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
13785 && !sym->attr.generic && !sym->attr.external
13786 && sym->attr.if_source == IFSRC_UNKNOWN
13787 && sym->ts.type == BT_UNKNOWN))
13790 /* If we find that a flavorless symbol is an interface in one of the
13791 parent namespaces, find its symtree in this namespace, free the
13792 symbol and set the symtree to point to the interface symbol. */
13793 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
13795 symtree = gfc_find_symtree (ns->sym_root, sym->name);
13796 if (symtree && (symtree->n.sym->generic ||
13797 (symtree->n.sym->attr.flavor == FL_PROCEDURE
13798 && sym->ns->construct_entities)))
13800 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
13801 sym->name);
13802 if (this_symtree->n.sym == sym)
13804 symtree->n.sym->refs++;
13805 gfc_release_symbol (sym);
13806 this_symtree->n.sym = symtree->n.sym;
13807 return;
13812 /* Otherwise give it a flavor according to such attributes as
13813 it has. */
13814 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
13815 && sym->attr.intrinsic == 0)
13816 sym->attr.flavor = FL_VARIABLE;
13817 else if (sym->attr.flavor == FL_UNKNOWN)
13819 sym->attr.flavor = FL_PROCEDURE;
13820 if (sym->attr.dimension)
13821 sym->attr.function = 1;
13825 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
13826 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
13828 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
13829 && !resolve_procedure_interface (sym))
13830 return;
13832 if (sym->attr.is_protected && !sym->attr.proc_pointer
13833 && (sym->attr.procedure || sym->attr.external))
13835 if (sym->attr.external)
13836 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
13837 "at %L", &sym->declared_at);
13838 else
13839 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
13840 "at %L", &sym->declared_at);
13842 return;
13845 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
13846 return;
13848 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
13849 && !resolve_fl_struct (sym))
13850 return;
13852 /* Symbols that are module procedures with results (functions) have
13853 the types and array specification copied for type checking in
13854 procedures that call them, as well as for saving to a module
13855 file. These symbols can't stand the scrutiny that their results
13856 can. */
13857 mp_flag = (sym->result != NULL && sym->result != sym);
13859 /* Make sure that the intrinsic is consistent with its internal
13860 representation. This needs to be done before assigning a default
13861 type to avoid spurious warnings. */
13862 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
13863 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
13864 return;
13866 /* Resolve associate names. */
13867 if (sym->assoc)
13868 resolve_assoc_var (sym, true);
13870 /* Assign default type to symbols that need one and don't have one. */
13871 if (sym->ts.type == BT_UNKNOWN)
13873 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
13875 gfc_set_default_type (sym, 1, NULL);
13878 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
13879 && !sym->attr.function && !sym->attr.subroutine
13880 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
13881 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
13883 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
13885 /* The specific case of an external procedure should emit an error
13886 in the case that there is no implicit type. */
13887 if (!mp_flag)
13888 gfc_set_default_type (sym, sym->attr.external, NULL);
13889 else
13891 /* Result may be in another namespace. */
13892 resolve_symbol (sym->result);
13894 if (!sym->result->attr.proc_pointer)
13896 sym->ts = sym->result->ts;
13897 sym->as = gfc_copy_array_spec (sym->result->as);
13898 sym->attr.dimension = sym->result->attr.dimension;
13899 sym->attr.pointer = sym->result->attr.pointer;
13900 sym->attr.allocatable = sym->result->attr.allocatable;
13901 sym->attr.contiguous = sym->result->attr.contiguous;
13906 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
13908 bool saved_specification_expr = specification_expr;
13909 specification_expr = true;
13910 gfc_resolve_array_spec (sym->result->as, false);
13911 specification_expr = saved_specification_expr;
13914 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
13916 as = CLASS_DATA (sym)->as;
13917 class_attr = CLASS_DATA (sym)->attr;
13918 class_attr.pointer = class_attr.class_pointer;
13920 else
13922 class_attr = sym->attr;
13923 as = sym->as;
13926 /* F2008, C530. */
13927 if (sym->attr.contiguous
13928 && (!class_attr.dimension
13929 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
13930 && !class_attr.pointer)))
13932 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
13933 "array pointer or an assumed-shape or assumed-rank array",
13934 sym->name, &sym->declared_at);
13935 return;
13938 /* Assumed size arrays and assumed shape arrays must be dummy
13939 arguments. Array-spec's of implied-shape should have been resolved to
13940 AS_EXPLICIT already. */
13942 if (as)
13944 gcc_assert (as->type != AS_IMPLIED_SHAPE);
13945 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
13946 || as->type == AS_ASSUMED_SHAPE)
13947 && !sym->attr.dummy && !sym->attr.select_type_temporary)
13949 if (as->type == AS_ASSUMED_SIZE)
13950 gfc_error ("Assumed size array at %L must be a dummy argument",
13951 &sym->declared_at);
13952 else
13953 gfc_error ("Assumed shape array at %L must be a dummy argument",
13954 &sym->declared_at);
13955 return;
13957 /* TS 29113, C535a. */
13958 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
13959 && !sym->attr.select_type_temporary)
13961 gfc_error ("Assumed-rank array at %L must be a dummy argument",
13962 &sym->declared_at);
13963 return;
13965 if (as->type == AS_ASSUMED_RANK
13966 && (sym->attr.codimension || sym->attr.value))
13968 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
13969 "CODIMENSION attribute", &sym->declared_at);
13970 return;
13974 /* Make sure symbols with known intent or optional are really dummy
13975 variable. Because of ENTRY statement, this has to be deferred
13976 until resolution time. */
13978 if (!sym->attr.dummy
13979 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
13981 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
13982 return;
13985 if (sym->attr.value && !sym->attr.dummy)
13987 gfc_error ("%qs at %L cannot have the VALUE attribute because "
13988 "it is not a dummy argument", sym->name, &sym->declared_at);
13989 return;
13992 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
13994 gfc_charlen *cl = sym->ts.u.cl;
13995 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13997 gfc_error ("Character dummy variable %qs at %L with VALUE "
13998 "attribute must have constant length",
13999 sym->name, &sym->declared_at);
14000 return;
14003 if (sym->ts.is_c_interop
14004 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
14006 gfc_error ("C interoperable character dummy variable %qs at %L "
14007 "with VALUE attribute must have length one",
14008 sym->name, &sym->declared_at);
14009 return;
14013 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
14014 && sym->ts.u.derived->attr.generic)
14016 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
14017 if (!sym->ts.u.derived)
14019 gfc_error ("The derived type %qs at %L is of type %qs, "
14020 "which has not been defined", sym->name,
14021 &sym->declared_at, sym->ts.u.derived->name);
14022 sym->ts.type = BT_UNKNOWN;
14023 return;
14027 /* Use the same constraints as TYPE(*), except for the type check
14028 and that only scalars and assumed-size arrays are permitted. */
14029 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
14031 if (!sym->attr.dummy)
14033 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
14034 "a dummy argument", sym->name, &sym->declared_at);
14035 return;
14038 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
14039 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
14040 && sym->ts.type != BT_COMPLEX)
14042 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
14043 "of type TYPE(*) or of an numeric intrinsic type",
14044 sym->name, &sym->declared_at);
14045 return;
14048 if (sym->attr.allocatable || sym->attr.codimension
14049 || sym->attr.pointer || sym->attr.value)
14051 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
14052 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
14053 "attribute", sym->name, &sym->declared_at);
14054 return;
14057 if (sym->attr.intent == INTENT_OUT)
14059 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
14060 "have the INTENT(OUT) attribute",
14061 sym->name, &sym->declared_at);
14062 return;
14064 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
14066 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
14067 "either be a scalar or an assumed-size array",
14068 sym->name, &sym->declared_at);
14069 return;
14072 /* Set the type to TYPE(*) and add a dimension(*) to ensure
14073 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
14074 packing. */
14075 sym->ts.type = BT_ASSUMED;
14076 sym->as = gfc_get_array_spec ();
14077 sym->as->type = AS_ASSUMED_SIZE;
14078 sym->as->rank = 1;
14079 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
14081 else if (sym->ts.type == BT_ASSUMED)
14083 /* TS 29113, C407a. */
14084 if (!sym->attr.dummy)
14086 gfc_error ("Assumed type of variable %s at %L is only permitted "
14087 "for dummy variables", sym->name, &sym->declared_at);
14088 return;
14090 if (sym->attr.allocatable || sym->attr.codimension
14091 || sym->attr.pointer || sym->attr.value)
14093 gfc_error ("Assumed-type variable %s at %L may not have the "
14094 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
14095 sym->name, &sym->declared_at);
14096 return;
14098 if (sym->attr.intent == INTENT_OUT)
14100 gfc_error ("Assumed-type variable %s at %L may not have the "
14101 "INTENT(OUT) attribute",
14102 sym->name, &sym->declared_at);
14103 return;
14105 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
14107 gfc_error ("Assumed-type variable %s at %L shall not be an "
14108 "explicit-shape array", sym->name, &sym->declared_at);
14109 return;
14113 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
14114 do this for something that was implicitly typed because that is handled
14115 in gfc_set_default_type. Handle dummy arguments and procedure
14116 definitions separately. Also, anything that is use associated is not
14117 handled here but instead is handled in the module it is declared in.
14118 Finally, derived type definitions are allowed to be BIND(C) since that
14119 only implies that they're interoperable, and they are checked fully for
14120 interoperability when a variable is declared of that type. */
14121 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
14122 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
14123 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
14125 bool t = true;
14127 /* First, make sure the variable is declared at the
14128 module-level scope (J3/04-007, Section 15.3). */
14129 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
14130 sym->attr.in_common == 0)
14132 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
14133 "is neither a COMMON block nor declared at the "
14134 "module level scope", sym->name, &(sym->declared_at));
14135 t = false;
14137 else if (sym->common_head != NULL)
14139 t = verify_com_block_vars_c_interop (sym->common_head);
14141 else
14143 /* If type() declaration, we need to verify that the components
14144 of the given type are all C interoperable, etc. */
14145 if (sym->ts.type == BT_DERIVED &&
14146 sym->ts.u.derived->attr.is_c_interop != 1)
14148 /* Make sure the user marked the derived type as BIND(C). If
14149 not, call the verify routine. This could print an error
14150 for the derived type more than once if multiple variables
14151 of that type are declared. */
14152 if (sym->ts.u.derived->attr.is_bind_c != 1)
14153 verify_bind_c_derived_type (sym->ts.u.derived);
14154 t = false;
14157 /* Verify the variable itself as C interoperable if it
14158 is BIND(C). It is not possible for this to succeed if
14159 the verify_bind_c_derived_type failed, so don't have to handle
14160 any error returned by verify_bind_c_derived_type. */
14161 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
14162 sym->common_block);
14165 if (!t)
14167 /* clear the is_bind_c flag to prevent reporting errors more than
14168 once if something failed. */
14169 sym->attr.is_bind_c = 0;
14170 return;
14174 /* If a derived type symbol has reached this point, without its
14175 type being declared, we have an error. Notice that most
14176 conditions that produce undefined derived types have already
14177 been dealt with. However, the likes of:
14178 implicit type(t) (t) ..... call foo (t) will get us here if
14179 the type is not declared in the scope of the implicit
14180 statement. Change the type to BT_UNKNOWN, both because it is so
14181 and to prevent an ICE. */
14182 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
14183 && sym->ts.u.derived->components == NULL
14184 && !sym->ts.u.derived->attr.zero_comp)
14186 gfc_error ("The derived type %qs at %L is of type %qs, "
14187 "which has not been defined", sym->name,
14188 &sym->declared_at, sym->ts.u.derived->name);
14189 sym->ts.type = BT_UNKNOWN;
14190 return;
14193 /* Make sure that the derived type has been resolved and that the
14194 derived type is visible in the symbol's namespace, if it is a
14195 module function and is not PRIVATE. */
14196 if (sym->ts.type == BT_DERIVED
14197 && sym->ts.u.derived->attr.use_assoc
14198 && sym->ns->proc_name
14199 && sym->ns->proc_name->attr.flavor == FL_MODULE
14200 && !resolve_fl_derived (sym->ts.u.derived))
14201 return;
14203 /* Unless the derived-type declaration is use associated, Fortran 95
14204 does not allow public entries of private derived types.
14205 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
14206 161 in 95-006r3. */
14207 if (sym->ts.type == BT_DERIVED
14208 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
14209 && !sym->ts.u.derived->attr.use_assoc
14210 && gfc_check_symbol_access (sym)
14211 && !gfc_check_symbol_access (sym->ts.u.derived)
14212 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
14213 "derived type %qs",
14214 (sym->attr.flavor == FL_PARAMETER)
14215 ? "parameter" : "variable",
14216 sym->name, &sym->declared_at,
14217 sym->ts.u.derived->name))
14218 return;
14220 /* F2008, C1302. */
14221 if (sym->ts.type == BT_DERIVED
14222 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
14223 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
14224 || sym->ts.u.derived->attr.lock_comp)
14225 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
14227 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
14228 "type LOCK_TYPE must be a coarray", sym->name,
14229 &sym->declared_at);
14230 return;
14233 /* TS18508, C702/C703. */
14234 if (sym->ts.type == BT_DERIVED
14235 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
14236 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
14237 || sym->ts.u.derived->attr.event_comp)
14238 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
14240 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
14241 "type LOCK_TYPE must be a coarray", sym->name,
14242 &sym->declared_at);
14243 return;
14246 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
14247 default initialization is defined (5.1.2.4.4). */
14248 if (sym->ts.type == BT_DERIVED
14249 && sym->attr.dummy
14250 && sym->attr.intent == INTENT_OUT
14251 && sym->as
14252 && sym->as->type == AS_ASSUMED_SIZE)
14254 for (c = sym->ts.u.derived->components; c; c = c->next)
14256 if (c->initializer)
14258 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
14259 "ASSUMED SIZE and so cannot have a default initializer",
14260 sym->name, &sym->declared_at);
14261 return;
14266 /* F2008, C542. */
14267 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
14268 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
14270 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
14271 "INTENT(OUT)", sym->name, &sym->declared_at);
14272 return;
14275 /* TS18508. */
14276 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
14277 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
14279 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
14280 "INTENT(OUT)", sym->name, &sym->declared_at);
14281 return;
14284 /* F2008, C525. */
14285 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14286 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14287 && CLASS_DATA (sym)->attr.coarray_comp))
14288 || class_attr.codimension)
14289 && (sym->attr.result || sym->result == sym))
14291 gfc_error ("Function result %qs at %L shall not be a coarray or have "
14292 "a coarray component", sym->name, &sym->declared_at);
14293 return;
14296 /* F2008, C524. */
14297 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
14298 && sym->ts.u.derived->ts.is_iso_c)
14300 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14301 "shall not be a coarray", sym->name, &sym->declared_at);
14302 return;
14305 /* F2008, C525. */
14306 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14307 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14308 && CLASS_DATA (sym)->attr.coarray_comp))
14309 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
14310 || class_attr.allocatable))
14312 gfc_error ("Variable %qs at %L with coarray component shall be a "
14313 "nonpointer, nonallocatable scalar, which is not a coarray",
14314 sym->name, &sym->declared_at);
14315 return;
14318 /* F2008, C526. The function-result case was handled above. */
14319 if (class_attr.codimension
14320 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
14321 || sym->attr.select_type_temporary
14322 || sym->ns->save_all
14323 || sym->ns->proc_name->attr.flavor == FL_MODULE
14324 || sym->ns->proc_name->attr.is_main_program
14325 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
14327 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
14328 "nor a dummy argument", sym->name, &sym->declared_at);
14329 return;
14331 /* F2008, C528. */
14332 else if (class_attr.codimension && !sym->attr.select_type_temporary
14333 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
14335 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
14336 "deferred shape", sym->name, &sym->declared_at);
14337 return;
14339 else if (class_attr.codimension && class_attr.allocatable && as
14340 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
14342 gfc_error ("Allocatable coarray variable %qs at %L must have "
14343 "deferred shape", sym->name, &sym->declared_at);
14344 return;
14347 /* F2008, C541. */
14348 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14349 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14350 && CLASS_DATA (sym)->attr.coarray_comp))
14351 || (class_attr.codimension && class_attr.allocatable))
14352 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
14354 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
14355 "allocatable coarray or have coarray components",
14356 sym->name, &sym->declared_at);
14357 return;
14360 if (class_attr.codimension && sym->attr.dummy
14361 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
14363 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
14364 "procedure %qs", sym->name, &sym->declared_at,
14365 sym->ns->proc_name->name);
14366 return;
14369 if (sym->ts.type == BT_LOGICAL
14370 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
14371 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
14372 && sym->ns->proc_name->attr.is_bind_c)))
14374 int i;
14375 for (i = 0; gfc_logical_kinds[i].kind; i++)
14376 if (gfc_logical_kinds[i].kind == sym->ts.kind)
14377 break;
14378 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
14379 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
14380 "%L with non-C_Bool kind in BIND(C) procedure "
14381 "%qs", sym->name, &sym->declared_at,
14382 sym->ns->proc_name->name))
14383 return;
14384 else if (!gfc_logical_kinds[i].c_bool
14385 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
14386 "%qs at %L with non-C_Bool kind in "
14387 "BIND(C) procedure %qs", sym->name,
14388 &sym->declared_at,
14389 sym->attr.function ? sym->name
14390 : sym->ns->proc_name->name))
14391 return;
14394 switch (sym->attr.flavor)
14396 case FL_VARIABLE:
14397 if (!resolve_fl_variable (sym, mp_flag))
14398 return;
14399 break;
14401 case FL_PROCEDURE:
14402 if (sym->formal && !sym->formal_ns)
14404 /* Check that none of the arguments are a namelist. */
14405 gfc_formal_arglist *formal = sym->formal;
14407 for (; formal; formal = formal->next)
14408 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
14410 gfc_error ("Namelist '%s' can not be an argument to "
14411 "subroutine or function at %L",
14412 formal->sym->name, &sym->declared_at);
14413 return;
14417 if (!resolve_fl_procedure (sym, mp_flag))
14418 return;
14419 break;
14421 case FL_NAMELIST:
14422 if (!resolve_fl_namelist (sym))
14423 return;
14424 break;
14426 case FL_PARAMETER:
14427 if (!resolve_fl_parameter (sym))
14428 return;
14429 break;
14431 default:
14432 break;
14435 /* Resolve array specifier. Check as well some constraints
14436 on COMMON blocks. */
14438 check_constant = sym->attr.in_common && !sym->attr.pointer;
14440 /* Set the formal_arg_flag so that check_conflict will not throw
14441 an error for host associated variables in the specification
14442 expression for an array_valued function. */
14443 if (sym->attr.function && sym->as)
14444 formal_arg_flag = 1;
14446 saved_specification_expr = specification_expr;
14447 specification_expr = true;
14448 gfc_resolve_array_spec (sym->as, check_constant);
14449 specification_expr = saved_specification_expr;
14451 formal_arg_flag = 0;
14453 /* Resolve formal namespaces. */
14454 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
14455 && !sym->attr.contained && !sym->attr.intrinsic)
14456 gfc_resolve (sym->formal_ns);
14458 /* Make sure the formal namespace is present. */
14459 if (sym->formal && !sym->formal_ns)
14461 gfc_formal_arglist *formal = sym->formal;
14462 while (formal && !formal->sym)
14463 formal = formal->next;
14465 if (formal)
14467 sym->formal_ns = formal->sym->ns;
14468 if (sym->ns != formal->sym->ns)
14469 sym->formal_ns->refs++;
14473 /* Check threadprivate restrictions. */
14474 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
14475 && (!sym->attr.in_common
14476 && sym->module == NULL
14477 && (sym->ns->proc_name == NULL
14478 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
14479 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
14481 /* Check omp declare target restrictions. */
14482 if (sym->attr.omp_declare_target
14483 && sym->attr.flavor == FL_VARIABLE
14484 && !sym->attr.save
14485 && !sym->ns->save_all
14486 && (!sym->attr.in_common
14487 && sym->module == NULL
14488 && (sym->ns->proc_name == NULL
14489 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
14490 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
14491 sym->name, &sym->declared_at);
14493 /* If we have come this far we can apply default-initializers, as
14494 described in 14.7.5, to those variables that have not already
14495 been assigned one. */
14496 if (sym->ts.type == BT_DERIVED
14497 && !sym->value
14498 && !sym->attr.allocatable
14499 && !sym->attr.alloc_comp)
14501 symbol_attribute *a = &sym->attr;
14503 if ((!a->save && !a->dummy && !a->pointer
14504 && !a->in_common && !a->use_assoc
14505 && !a->result && !a->function)
14506 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
14507 apply_default_init (sym);
14508 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
14509 && (sym->ts.u.derived->attr.alloc_comp
14510 || sym->ts.u.derived->attr.pointer_comp))
14511 /* Mark the result symbol to be referenced, when it has allocatable
14512 components. */
14513 sym->result->attr.referenced = 1;
14516 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
14517 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
14518 && !CLASS_DATA (sym)->attr.class_pointer
14519 && !CLASS_DATA (sym)->attr.allocatable)
14520 apply_default_init (sym);
14522 /* If this symbol has a type-spec, check it. */
14523 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
14524 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
14525 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
14526 return;
14530 /************* Resolve DATA statements *************/
14532 static struct
14534 gfc_data_value *vnode;
14535 mpz_t left;
14537 values;
14540 /* Advance the values structure to point to the next value in the data list. */
14542 static bool
14543 next_data_value (void)
14545 while (mpz_cmp_ui (values.left, 0) == 0)
14548 if (values.vnode->next == NULL)
14549 return false;
14551 values.vnode = values.vnode->next;
14552 mpz_set (values.left, values.vnode->repeat);
14555 return true;
14559 static bool
14560 check_data_variable (gfc_data_variable *var, locus *where)
14562 gfc_expr *e;
14563 mpz_t size;
14564 mpz_t offset;
14565 bool t;
14566 ar_type mark = AR_UNKNOWN;
14567 int i;
14568 mpz_t section_index[GFC_MAX_DIMENSIONS];
14569 gfc_ref *ref;
14570 gfc_array_ref *ar;
14571 gfc_symbol *sym;
14572 int has_pointer;
14574 if (!gfc_resolve_expr (var->expr))
14575 return false;
14577 ar = NULL;
14578 mpz_init_set_si (offset, 0);
14579 e = var->expr;
14581 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
14582 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
14583 e = e->value.function.actual->expr;
14585 if (e->expr_type != EXPR_VARIABLE)
14586 gfc_internal_error ("check_data_variable(): Bad expression");
14588 sym = e->symtree->n.sym;
14590 if (sym->ns->is_block_data && !sym->attr.in_common)
14592 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
14593 sym->name, &sym->declared_at);
14596 if (e->ref == NULL && sym->as)
14598 gfc_error ("DATA array %qs at %L must be specified in a previous"
14599 " declaration", sym->name, where);
14600 return false;
14603 has_pointer = sym->attr.pointer;
14605 if (gfc_is_coindexed (e))
14607 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
14608 where);
14609 return false;
14612 for (ref = e->ref; ref; ref = ref->next)
14614 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
14615 has_pointer = 1;
14617 if (has_pointer
14618 && ref->type == REF_ARRAY
14619 && ref->u.ar.type != AR_FULL)
14621 gfc_error ("DATA element %qs at %L is a pointer and so must "
14622 "be a full array", sym->name, where);
14623 return false;
14627 if (e->rank == 0 || has_pointer)
14629 mpz_init_set_ui (size, 1);
14630 ref = NULL;
14632 else
14634 ref = e->ref;
14636 /* Find the array section reference. */
14637 for (ref = e->ref; ref; ref = ref->next)
14639 if (ref->type != REF_ARRAY)
14640 continue;
14641 if (ref->u.ar.type == AR_ELEMENT)
14642 continue;
14643 break;
14645 gcc_assert (ref);
14647 /* Set marks according to the reference pattern. */
14648 switch (ref->u.ar.type)
14650 case AR_FULL:
14651 mark = AR_FULL;
14652 break;
14654 case AR_SECTION:
14655 ar = &ref->u.ar;
14656 /* Get the start position of array section. */
14657 gfc_get_section_index (ar, section_index, &offset);
14658 mark = AR_SECTION;
14659 break;
14661 default:
14662 gcc_unreachable ();
14665 if (!gfc_array_size (e, &size))
14667 gfc_error ("Nonconstant array section at %L in DATA statement",
14668 &e->where);
14669 mpz_clear (offset);
14670 return false;
14674 t = true;
14676 while (mpz_cmp_ui (size, 0) > 0)
14678 if (!next_data_value ())
14680 gfc_error ("DATA statement at %L has more variables than values",
14681 where);
14682 t = false;
14683 break;
14686 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
14687 if (!t)
14688 break;
14690 /* If we have more than one element left in the repeat count,
14691 and we have more than one element left in the target variable,
14692 then create a range assignment. */
14693 /* FIXME: Only done for full arrays for now, since array sections
14694 seem tricky. */
14695 if (mark == AR_FULL && ref && ref->next == NULL
14696 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
14698 mpz_t range;
14700 if (mpz_cmp (size, values.left) >= 0)
14702 mpz_init_set (range, values.left);
14703 mpz_sub (size, size, values.left);
14704 mpz_set_ui (values.left, 0);
14706 else
14708 mpz_init_set (range, size);
14709 mpz_sub (values.left, values.left, size);
14710 mpz_set_ui (size, 0);
14713 t = gfc_assign_data_value (var->expr, values.vnode->expr,
14714 offset, &range);
14716 mpz_add (offset, offset, range);
14717 mpz_clear (range);
14719 if (!t)
14720 break;
14723 /* Assign initial value to symbol. */
14724 else
14726 mpz_sub_ui (values.left, values.left, 1);
14727 mpz_sub_ui (size, size, 1);
14729 t = gfc_assign_data_value (var->expr, values.vnode->expr,
14730 offset, NULL);
14731 if (!t)
14732 break;
14734 if (mark == AR_FULL)
14735 mpz_add_ui (offset, offset, 1);
14737 /* Modify the array section indexes and recalculate the offset
14738 for next element. */
14739 else if (mark == AR_SECTION)
14740 gfc_advance_section (section_index, ar, &offset);
14744 if (mark == AR_SECTION)
14746 for (i = 0; i < ar->dimen; i++)
14747 mpz_clear (section_index[i]);
14750 mpz_clear (size);
14751 mpz_clear (offset);
14753 return t;
14757 static bool traverse_data_var (gfc_data_variable *, locus *);
14759 /* Iterate over a list of elements in a DATA statement. */
14761 static bool
14762 traverse_data_list (gfc_data_variable *var, locus *where)
14764 mpz_t trip;
14765 iterator_stack frame;
14766 gfc_expr *e, *start, *end, *step;
14767 bool retval = true;
14769 mpz_init (frame.value);
14770 mpz_init (trip);
14772 start = gfc_copy_expr (var->iter.start);
14773 end = gfc_copy_expr (var->iter.end);
14774 step = gfc_copy_expr (var->iter.step);
14776 if (!gfc_simplify_expr (start, 1)
14777 || start->expr_type != EXPR_CONSTANT)
14779 gfc_error ("start of implied-do loop at %L could not be "
14780 "simplified to a constant value", &start->where);
14781 retval = false;
14782 goto cleanup;
14784 if (!gfc_simplify_expr (end, 1)
14785 || end->expr_type != EXPR_CONSTANT)
14787 gfc_error ("end of implied-do loop at %L could not be "
14788 "simplified to a constant value", &start->where);
14789 retval = false;
14790 goto cleanup;
14792 if (!gfc_simplify_expr (step, 1)
14793 || step->expr_type != EXPR_CONSTANT)
14795 gfc_error ("step of implied-do loop at %L could not be "
14796 "simplified to a constant value", &start->where);
14797 retval = false;
14798 goto cleanup;
14801 mpz_set (trip, end->value.integer);
14802 mpz_sub (trip, trip, start->value.integer);
14803 mpz_add (trip, trip, step->value.integer);
14805 mpz_div (trip, trip, step->value.integer);
14807 mpz_set (frame.value, start->value.integer);
14809 frame.prev = iter_stack;
14810 frame.variable = var->iter.var->symtree;
14811 iter_stack = &frame;
14813 while (mpz_cmp_ui (trip, 0) > 0)
14815 if (!traverse_data_var (var->list, where))
14817 retval = false;
14818 goto cleanup;
14821 e = gfc_copy_expr (var->expr);
14822 if (!gfc_simplify_expr (e, 1))
14824 gfc_free_expr (e);
14825 retval = false;
14826 goto cleanup;
14829 mpz_add (frame.value, frame.value, step->value.integer);
14831 mpz_sub_ui (trip, trip, 1);
14834 cleanup:
14835 mpz_clear (frame.value);
14836 mpz_clear (trip);
14838 gfc_free_expr (start);
14839 gfc_free_expr (end);
14840 gfc_free_expr (step);
14842 iter_stack = frame.prev;
14843 return retval;
14847 /* Type resolve variables in the variable list of a DATA statement. */
14849 static bool
14850 traverse_data_var (gfc_data_variable *var, locus *where)
14852 bool t;
14854 for (; var; var = var->next)
14856 if (var->expr == NULL)
14857 t = traverse_data_list (var, where);
14858 else
14859 t = check_data_variable (var, where);
14861 if (!t)
14862 return false;
14865 return true;
14869 /* Resolve the expressions and iterators associated with a data statement.
14870 This is separate from the assignment checking because data lists should
14871 only be resolved once. */
14873 static bool
14874 resolve_data_variables (gfc_data_variable *d)
14876 for (; d; d = d->next)
14878 if (d->list == NULL)
14880 if (!gfc_resolve_expr (d->expr))
14881 return false;
14883 else
14885 if (!gfc_resolve_iterator (&d->iter, false, true))
14886 return false;
14888 if (!resolve_data_variables (d->list))
14889 return false;
14893 return true;
14897 /* Resolve a single DATA statement. We implement this by storing a pointer to
14898 the value list into static variables, and then recursively traversing the
14899 variables list, expanding iterators and such. */
14901 static void
14902 resolve_data (gfc_data *d)
14905 if (!resolve_data_variables (d->var))
14906 return;
14908 values.vnode = d->value;
14909 if (d->value == NULL)
14910 mpz_set_ui (values.left, 0);
14911 else
14912 mpz_set (values.left, d->value->repeat);
14914 if (!traverse_data_var (d->var, &d->where))
14915 return;
14917 /* At this point, we better not have any values left. */
14919 if (next_data_value ())
14920 gfc_error ("DATA statement at %L has more values than variables",
14921 &d->where);
14925 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
14926 accessed by host or use association, is a dummy argument to a pure function,
14927 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
14928 is storage associated with any such variable, shall not be used in the
14929 following contexts: (clients of this function). */
14931 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
14932 procedure. Returns zero if assignment is OK, nonzero if there is a
14933 problem. */
14935 gfc_impure_variable (gfc_symbol *sym)
14937 gfc_symbol *proc;
14938 gfc_namespace *ns;
14940 if (sym->attr.use_assoc || sym->attr.in_common)
14941 return 1;
14943 /* Check if the symbol's ns is inside the pure procedure. */
14944 for (ns = gfc_current_ns; ns; ns = ns->parent)
14946 if (ns == sym->ns)
14947 break;
14948 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
14949 return 1;
14952 proc = sym->ns->proc_name;
14953 if (sym->attr.dummy
14954 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
14955 || proc->attr.function))
14956 return 1;
14958 /* TODO: Sort out what can be storage associated, if anything, and include
14959 it here. In principle equivalences should be scanned but it does not
14960 seem to be possible to storage associate an impure variable this way. */
14961 return 0;
14965 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
14966 current namespace is inside a pure procedure. */
14969 gfc_pure (gfc_symbol *sym)
14971 symbol_attribute attr;
14972 gfc_namespace *ns;
14974 if (sym == NULL)
14976 /* Check if the current namespace or one of its parents
14977 belongs to a pure procedure. */
14978 for (ns = gfc_current_ns; ns; ns = ns->parent)
14980 sym = ns->proc_name;
14981 if (sym == NULL)
14982 return 0;
14983 attr = sym->attr;
14984 if (attr.flavor == FL_PROCEDURE && attr.pure)
14985 return 1;
14987 return 0;
14990 attr = sym->attr;
14992 return attr.flavor == FL_PROCEDURE && attr.pure;
14996 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
14997 checks if the current namespace is implicitly pure. Note that this
14998 function returns false for a PURE procedure. */
15001 gfc_implicit_pure (gfc_symbol *sym)
15003 gfc_namespace *ns;
15005 if (sym == NULL)
15007 /* Check if the current procedure is implicit_pure. Walk up
15008 the procedure list until we find a procedure. */
15009 for (ns = gfc_current_ns; ns; ns = ns->parent)
15011 sym = ns->proc_name;
15012 if (sym == NULL)
15013 return 0;
15015 if (sym->attr.flavor == FL_PROCEDURE)
15016 break;
15020 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
15021 && !sym->attr.pure;
15025 void
15026 gfc_unset_implicit_pure (gfc_symbol *sym)
15028 gfc_namespace *ns;
15030 if (sym == NULL)
15032 /* Check if the current procedure is implicit_pure. Walk up
15033 the procedure list until we find a procedure. */
15034 for (ns = gfc_current_ns; ns; ns = ns->parent)
15036 sym = ns->proc_name;
15037 if (sym == NULL)
15038 return;
15040 if (sym->attr.flavor == FL_PROCEDURE)
15041 break;
15045 if (sym->attr.flavor == FL_PROCEDURE)
15046 sym->attr.implicit_pure = 0;
15047 else
15048 sym->attr.pure = 0;
15052 /* Test whether the current procedure is elemental or not. */
15055 gfc_elemental (gfc_symbol *sym)
15057 symbol_attribute attr;
15059 if (sym == NULL)
15060 sym = gfc_current_ns->proc_name;
15061 if (sym == NULL)
15062 return 0;
15063 attr = sym->attr;
15065 return attr.flavor == FL_PROCEDURE && attr.elemental;
15069 /* Warn about unused labels. */
15071 static void
15072 warn_unused_fortran_label (gfc_st_label *label)
15074 if (label == NULL)
15075 return;
15077 warn_unused_fortran_label (label->left);
15079 if (label->defined == ST_LABEL_UNKNOWN)
15080 return;
15082 switch (label->referenced)
15084 case ST_LABEL_UNKNOWN:
15085 gfc_warning (0, "Label %d at %L defined but not used", label->value,
15086 &label->where);
15087 break;
15089 case ST_LABEL_BAD_TARGET:
15090 gfc_warning (0, "Label %d at %L defined but cannot be used",
15091 label->value, &label->where);
15092 break;
15094 default:
15095 break;
15098 warn_unused_fortran_label (label->right);
15102 /* Returns the sequence type of a symbol or sequence. */
15104 static seq_type
15105 sequence_type (gfc_typespec ts)
15107 seq_type result;
15108 gfc_component *c;
15110 switch (ts.type)
15112 case BT_DERIVED:
15114 if (ts.u.derived->components == NULL)
15115 return SEQ_NONDEFAULT;
15117 result = sequence_type (ts.u.derived->components->ts);
15118 for (c = ts.u.derived->components->next; c; c = c->next)
15119 if (sequence_type (c->ts) != result)
15120 return SEQ_MIXED;
15122 return result;
15124 case BT_CHARACTER:
15125 if (ts.kind != gfc_default_character_kind)
15126 return SEQ_NONDEFAULT;
15128 return SEQ_CHARACTER;
15130 case BT_INTEGER:
15131 if (ts.kind != gfc_default_integer_kind)
15132 return SEQ_NONDEFAULT;
15134 return SEQ_NUMERIC;
15136 case BT_REAL:
15137 if (!(ts.kind == gfc_default_real_kind
15138 || ts.kind == gfc_default_double_kind))
15139 return SEQ_NONDEFAULT;
15141 return SEQ_NUMERIC;
15143 case BT_COMPLEX:
15144 if (ts.kind != gfc_default_complex_kind)
15145 return SEQ_NONDEFAULT;
15147 return SEQ_NUMERIC;
15149 case BT_LOGICAL:
15150 if (ts.kind != gfc_default_logical_kind)
15151 return SEQ_NONDEFAULT;
15153 return SEQ_NUMERIC;
15155 default:
15156 return SEQ_NONDEFAULT;
15161 /* Resolve derived type EQUIVALENCE object. */
15163 static bool
15164 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
15166 gfc_component *c = derived->components;
15168 if (!derived)
15169 return true;
15171 /* Shall not be an object of nonsequence derived type. */
15172 if (!derived->attr.sequence)
15174 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
15175 "attribute to be an EQUIVALENCE object", sym->name,
15176 &e->where);
15177 return false;
15180 /* Shall not have allocatable components. */
15181 if (derived->attr.alloc_comp)
15183 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
15184 "components to be an EQUIVALENCE object",sym->name,
15185 &e->where);
15186 return false;
15189 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
15191 gfc_error ("Derived type variable %qs at %L with default "
15192 "initialization cannot be in EQUIVALENCE with a variable "
15193 "in COMMON", sym->name, &e->where);
15194 return false;
15197 for (; c ; c = c->next)
15199 if (gfc_bt_struct (c->ts.type)
15200 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
15201 return false;
15203 /* Shall not be an object of sequence derived type containing a pointer
15204 in the structure. */
15205 if (c->attr.pointer)
15207 gfc_error ("Derived type variable %qs at %L with pointer "
15208 "component(s) cannot be an EQUIVALENCE object",
15209 sym->name, &e->where);
15210 return false;
15213 return true;
15217 /* Resolve equivalence object.
15218 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
15219 an allocatable array, an object of nonsequence derived type, an object of
15220 sequence derived type containing a pointer at any level of component
15221 selection, an automatic object, a function name, an entry name, a result
15222 name, a named constant, a structure component, or a subobject of any of
15223 the preceding objects. A substring shall not have length zero. A
15224 derived type shall not have components with default initialization nor
15225 shall two objects of an equivalence group be initialized.
15226 Either all or none of the objects shall have an protected attribute.
15227 The simple constraints are done in symbol.c(check_conflict) and the rest
15228 are implemented here. */
15230 static void
15231 resolve_equivalence (gfc_equiv *eq)
15233 gfc_symbol *sym;
15234 gfc_symbol *first_sym;
15235 gfc_expr *e;
15236 gfc_ref *r;
15237 locus *last_where = NULL;
15238 seq_type eq_type, last_eq_type;
15239 gfc_typespec *last_ts;
15240 int object, cnt_protected;
15241 const char *msg;
15243 last_ts = &eq->expr->symtree->n.sym->ts;
15245 first_sym = eq->expr->symtree->n.sym;
15247 cnt_protected = 0;
15249 for (object = 1; eq; eq = eq->eq, object++)
15251 e = eq->expr;
15253 e->ts = e->symtree->n.sym->ts;
15254 /* match_varspec might not know yet if it is seeing
15255 array reference or substring reference, as it doesn't
15256 know the types. */
15257 if (e->ref && e->ref->type == REF_ARRAY)
15259 gfc_ref *ref = e->ref;
15260 sym = e->symtree->n.sym;
15262 if (sym->attr.dimension)
15264 ref->u.ar.as = sym->as;
15265 ref = ref->next;
15268 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
15269 if (e->ts.type == BT_CHARACTER
15270 && ref
15271 && ref->type == REF_ARRAY
15272 && ref->u.ar.dimen == 1
15273 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
15274 && ref->u.ar.stride[0] == NULL)
15276 gfc_expr *start = ref->u.ar.start[0];
15277 gfc_expr *end = ref->u.ar.end[0];
15278 void *mem = NULL;
15280 /* Optimize away the (:) reference. */
15281 if (start == NULL && end == NULL)
15283 if (e->ref == ref)
15284 e->ref = ref->next;
15285 else
15286 e->ref->next = ref->next;
15287 mem = ref;
15289 else
15291 ref->type = REF_SUBSTRING;
15292 if (start == NULL)
15293 start = gfc_get_int_expr (gfc_default_integer_kind,
15294 NULL, 1);
15295 ref->u.ss.start = start;
15296 if (end == NULL && e->ts.u.cl)
15297 end = gfc_copy_expr (e->ts.u.cl->length);
15298 ref->u.ss.end = end;
15299 ref->u.ss.length = e->ts.u.cl;
15300 e->ts.u.cl = NULL;
15302 ref = ref->next;
15303 free (mem);
15306 /* Any further ref is an error. */
15307 if (ref)
15309 gcc_assert (ref->type == REF_ARRAY);
15310 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
15311 &ref->u.ar.where);
15312 continue;
15316 if (!gfc_resolve_expr (e))
15317 continue;
15319 sym = e->symtree->n.sym;
15321 if (sym->attr.is_protected)
15322 cnt_protected++;
15323 if (cnt_protected > 0 && cnt_protected != object)
15325 gfc_error ("Either all or none of the objects in the "
15326 "EQUIVALENCE set at %L shall have the "
15327 "PROTECTED attribute",
15328 &e->where);
15329 break;
15332 /* Shall not equivalence common block variables in a PURE procedure. */
15333 if (sym->ns->proc_name
15334 && sym->ns->proc_name->attr.pure
15335 && sym->attr.in_common)
15337 gfc_error ("Common block member %qs at %L cannot be an EQUIVALENCE "
15338 "object in the pure procedure %qs",
15339 sym->name, &e->where, sym->ns->proc_name->name);
15340 break;
15343 /* Shall not be a named constant. */
15344 if (e->expr_type == EXPR_CONSTANT)
15346 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
15347 "object", sym->name, &e->where);
15348 continue;
15351 if (e->ts.type == BT_DERIVED
15352 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
15353 continue;
15355 /* Check that the types correspond correctly:
15356 Note 5.28:
15357 A numeric sequence structure may be equivalenced to another sequence
15358 structure, an object of default integer type, default real type, double
15359 precision real type, default logical type such that components of the
15360 structure ultimately only become associated to objects of the same
15361 kind. A character sequence structure may be equivalenced to an object
15362 of default character kind or another character sequence structure.
15363 Other objects may be equivalenced only to objects of the same type and
15364 kind parameters. */
15366 /* Identical types are unconditionally OK. */
15367 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
15368 goto identical_types;
15370 last_eq_type = sequence_type (*last_ts);
15371 eq_type = sequence_type (sym->ts);
15373 /* Since the pair of objects is not of the same type, mixed or
15374 non-default sequences can be rejected. */
15376 msg = "Sequence %s with mixed components in EQUIVALENCE "
15377 "statement at %L with different type objects";
15378 if ((object ==2
15379 && last_eq_type == SEQ_MIXED
15380 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
15381 || (eq_type == SEQ_MIXED
15382 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
15383 continue;
15385 msg = "Non-default type object or sequence %s in EQUIVALENCE "
15386 "statement at %L with objects of different type";
15387 if ((object ==2
15388 && last_eq_type == SEQ_NONDEFAULT
15389 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
15390 || (eq_type == SEQ_NONDEFAULT
15391 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
15392 continue;
15394 msg ="Non-CHARACTER object %qs in default CHARACTER "
15395 "EQUIVALENCE statement at %L";
15396 if (last_eq_type == SEQ_CHARACTER
15397 && eq_type != SEQ_CHARACTER
15398 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
15399 continue;
15401 msg ="Non-NUMERIC object %qs in default NUMERIC "
15402 "EQUIVALENCE statement at %L";
15403 if (last_eq_type == SEQ_NUMERIC
15404 && eq_type != SEQ_NUMERIC
15405 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
15406 continue;
15408 identical_types:
15409 last_ts =&sym->ts;
15410 last_where = &e->where;
15412 if (!e->ref)
15413 continue;
15415 /* Shall not be an automatic array. */
15416 if (e->ref->type == REF_ARRAY
15417 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
15419 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
15420 "an EQUIVALENCE object", sym->name, &e->where);
15421 continue;
15424 r = e->ref;
15425 while (r)
15427 /* Shall not be a structure component. */
15428 if (r->type == REF_COMPONENT)
15430 gfc_error ("Structure component %qs at %L cannot be an "
15431 "EQUIVALENCE object",
15432 r->u.c.component->name, &e->where);
15433 break;
15436 /* A substring shall not have length zero. */
15437 if (r->type == REF_SUBSTRING)
15439 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
15441 gfc_error ("Substring at %L has length zero",
15442 &r->u.ss.start->where);
15443 break;
15446 r = r->next;
15452 /* Resolve function and ENTRY types, issue diagnostics if needed. */
15454 static void
15455 resolve_fntype (gfc_namespace *ns)
15457 gfc_entry_list *el;
15458 gfc_symbol *sym;
15460 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
15461 return;
15463 /* If there are any entries, ns->proc_name is the entry master
15464 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
15465 if (ns->entries)
15466 sym = ns->entries->sym;
15467 else
15468 sym = ns->proc_name;
15469 if (sym->result == sym
15470 && sym->ts.type == BT_UNKNOWN
15471 && !gfc_set_default_type (sym, 0, NULL)
15472 && !sym->attr.untyped)
15474 gfc_error ("Function %qs at %L has no IMPLICIT type",
15475 sym->name, &sym->declared_at);
15476 sym->attr.untyped = 1;
15479 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
15480 && !sym->attr.contained
15481 && !gfc_check_symbol_access (sym->ts.u.derived)
15482 && gfc_check_symbol_access (sym))
15484 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
15485 "%L of PRIVATE type %qs", sym->name,
15486 &sym->declared_at, sym->ts.u.derived->name);
15489 if (ns->entries)
15490 for (el = ns->entries->next; el; el = el->next)
15492 if (el->sym->result == el->sym
15493 && el->sym->ts.type == BT_UNKNOWN
15494 && !gfc_set_default_type (el->sym, 0, NULL)
15495 && !el->sym->attr.untyped)
15497 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
15498 el->sym->name, &el->sym->declared_at);
15499 el->sym->attr.untyped = 1;
15505 /* 12.3.2.1.1 Defined operators. */
15507 static bool
15508 check_uop_procedure (gfc_symbol *sym, locus where)
15510 gfc_formal_arglist *formal;
15512 if (!sym->attr.function)
15514 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
15515 sym->name, &where);
15516 return false;
15519 if (sym->ts.type == BT_CHARACTER
15520 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
15521 && !(sym->result && ((sym->result->ts.u.cl
15522 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
15524 gfc_error ("User operator procedure %qs at %L cannot be assumed "
15525 "character length", sym->name, &where);
15526 return false;
15529 formal = gfc_sym_get_dummy_args (sym);
15530 if (!formal || !formal->sym)
15532 gfc_error ("User operator procedure %qs at %L must have at least "
15533 "one argument", sym->name, &where);
15534 return false;
15537 if (formal->sym->attr.intent != INTENT_IN)
15539 gfc_error ("First argument of operator interface at %L must be "
15540 "INTENT(IN)", &where);
15541 return false;
15544 if (formal->sym->attr.optional)
15546 gfc_error ("First argument of operator interface at %L cannot be "
15547 "optional", &where);
15548 return false;
15551 formal = formal->next;
15552 if (!formal || !formal->sym)
15553 return true;
15555 if (formal->sym->attr.intent != INTENT_IN)
15557 gfc_error ("Second argument of operator interface at %L must be "
15558 "INTENT(IN)", &where);
15559 return false;
15562 if (formal->sym->attr.optional)
15564 gfc_error ("Second argument of operator interface at %L cannot be "
15565 "optional", &where);
15566 return false;
15569 if (formal->next)
15571 gfc_error ("Operator interface at %L must have, at most, two "
15572 "arguments", &where);
15573 return false;
15576 return true;
15579 static void
15580 gfc_resolve_uops (gfc_symtree *symtree)
15582 gfc_interface *itr;
15584 if (symtree == NULL)
15585 return;
15587 gfc_resolve_uops (symtree->left);
15588 gfc_resolve_uops (symtree->right);
15590 for (itr = symtree->n.uop->op; itr; itr = itr->next)
15591 check_uop_procedure (itr->sym, itr->sym->declared_at);
15595 /* Examine all of the expressions associated with a program unit,
15596 assign types to all intermediate expressions, make sure that all
15597 assignments are to compatible types and figure out which names
15598 refer to which functions or subroutines. It doesn't check code
15599 block, which is handled by gfc_resolve_code. */
15601 static void
15602 resolve_types (gfc_namespace *ns)
15604 gfc_namespace *n;
15605 gfc_charlen *cl;
15606 gfc_data *d;
15607 gfc_equiv *eq;
15608 gfc_namespace* old_ns = gfc_current_ns;
15610 if (ns->types_resolved)
15611 return;
15613 /* Check that all IMPLICIT types are ok. */
15614 if (!ns->seen_implicit_none)
15616 unsigned letter;
15617 for (letter = 0; letter != GFC_LETTERS; ++letter)
15618 if (ns->set_flag[letter]
15619 && !resolve_typespec_used (&ns->default_type[letter],
15620 &ns->implicit_loc[letter], NULL))
15621 return;
15624 gfc_current_ns = ns;
15626 resolve_entries (ns);
15628 resolve_common_vars (&ns->blank_common, false);
15629 resolve_common_blocks (ns->common_root);
15631 resolve_contained_functions (ns);
15633 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
15634 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
15635 resolve_formal_arglist (ns->proc_name);
15637 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
15639 for (cl = ns->cl_list; cl; cl = cl->next)
15640 resolve_charlen (cl);
15642 gfc_traverse_ns (ns, resolve_symbol);
15644 resolve_fntype (ns);
15646 for (n = ns->contained; n; n = n->sibling)
15648 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
15649 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
15650 "also be PURE", n->proc_name->name,
15651 &n->proc_name->declared_at);
15653 resolve_types (n);
15656 forall_flag = 0;
15657 gfc_do_concurrent_flag = 0;
15658 gfc_check_interfaces (ns);
15660 gfc_traverse_ns (ns, resolve_values);
15662 if (ns->save_all)
15663 gfc_save_all (ns);
15665 iter_stack = NULL;
15666 for (d = ns->data; d; d = d->next)
15667 resolve_data (d);
15669 iter_stack = NULL;
15670 gfc_traverse_ns (ns, gfc_formalize_init_value);
15672 gfc_traverse_ns (ns, gfc_verify_binding_labels);
15674 for (eq = ns->equiv; eq; eq = eq->next)
15675 resolve_equivalence (eq);
15677 /* Warn about unused labels. */
15678 if (warn_unused_label)
15679 warn_unused_fortran_label (ns->st_labels);
15681 gfc_resolve_uops (ns->uop_root);
15683 gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
15685 gfc_resolve_omp_declare_simd (ns);
15687 gfc_resolve_omp_udrs (ns->omp_udr_root);
15689 ns->types_resolved = 1;
15691 gfc_current_ns = old_ns;
15695 /* Call gfc_resolve_code recursively. */
15697 static void
15698 resolve_codes (gfc_namespace *ns)
15700 gfc_namespace *n;
15701 bitmap_obstack old_obstack;
15703 if (ns->resolved == 1)
15704 return;
15706 for (n = ns->contained; n; n = n->sibling)
15707 resolve_codes (n);
15709 gfc_current_ns = ns;
15711 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
15712 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
15713 cs_base = NULL;
15715 /* Set to an out of range value. */
15716 current_entry_id = -1;
15718 old_obstack = labels_obstack;
15719 bitmap_obstack_initialize (&labels_obstack);
15721 gfc_resolve_oacc_declare (ns);
15722 gfc_resolve_code (ns->code, ns);
15724 bitmap_obstack_release (&labels_obstack);
15725 labels_obstack = old_obstack;
15729 /* This function is called after a complete program unit has been compiled.
15730 Its purpose is to examine all of the expressions associated with a program
15731 unit, assign types to all intermediate expressions, make sure that all
15732 assignments are to compatible types and figure out which names refer to
15733 which functions or subroutines. */
15735 void
15736 gfc_resolve (gfc_namespace *ns)
15738 gfc_namespace *old_ns;
15739 code_stack *old_cs_base;
15740 struct gfc_omp_saved_state old_omp_state;
15742 if (ns->resolved)
15743 return;
15745 ns->resolved = -1;
15746 old_ns = gfc_current_ns;
15747 old_cs_base = cs_base;
15749 /* As gfc_resolve can be called during resolution of an OpenMP construct
15750 body, we should clear any state associated to it, so that say NS's
15751 DO loops are not interpreted as OpenMP loops. */
15752 if (!ns->construct_entities)
15753 gfc_omp_save_and_clear_state (&old_omp_state);
15755 resolve_types (ns);
15756 component_assignment_level = 0;
15757 resolve_codes (ns);
15759 gfc_current_ns = old_ns;
15760 cs_base = old_cs_base;
15761 ns->resolved = 1;
15763 gfc_run_passes (ns);
15765 if (!ns->construct_entities)
15766 gfc_omp_restore_state (&old_omp_state);