Fix memory leak in tree-vect-slp.c
[official-gcc.git] / gcc / fortran / resolve.c
blob2c68af2b7e83c766dca87be109e65c8bbadd4673
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 if (sym && sym->attr.intrinsic
2927 && !gfc_resolve_intrinsic (sym, &expr->where))
2928 return false;
2930 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2932 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
2933 return false;
2936 /* If this ia a deferred TBP with an abstract interface (which may
2937 of course be referenced), expr->value.function.esym will be set. */
2938 if (sym && sym->attr.abstract && !expr->value.function.esym)
2940 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
2941 sym->name, &expr->where);
2942 return false;
2945 /* Switch off assumed size checking and do this again for certain kinds
2946 of procedure, once the procedure itself is resolved. */
2947 need_full_assumed_size++;
2949 if (expr->symtree && expr->symtree->n.sym)
2950 p = expr->symtree->n.sym->attr.proc;
2952 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2953 inquiry_argument = true;
2954 no_formal_args = sym && is_external_proc (sym)
2955 && gfc_sym_get_dummy_args (sym) == NULL;
2957 if (!resolve_actual_arglist (expr->value.function.actual,
2958 p, no_formal_args))
2960 inquiry_argument = false;
2961 return false;
2964 inquiry_argument = false;
2966 /* Resume assumed_size checking. */
2967 need_full_assumed_size--;
2969 /* If the procedure is external, check for usage. */
2970 if (sym && is_external_proc (sym))
2971 resolve_global_procedure (sym, &expr->where,
2972 &expr->value.function.actual, 0);
2974 if (sym && sym->ts.type == BT_CHARACTER
2975 && sym->ts.u.cl
2976 && sym->ts.u.cl->length == NULL
2977 && !sym->attr.dummy
2978 && !sym->ts.deferred
2979 && expr->value.function.esym == NULL
2980 && !sym->attr.contained)
2982 /* Internal procedures are taken care of in resolve_contained_fntype. */
2983 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
2984 "be used at %L since it is not a dummy argument",
2985 sym->name, &expr->where);
2986 return false;
2989 /* See if function is already resolved. */
2991 if (expr->value.function.name != NULL
2992 || expr->value.function.isym != NULL)
2994 if (expr->ts.type == BT_UNKNOWN)
2995 expr->ts = sym->ts;
2996 t = true;
2998 else
3000 /* Apply the rules of section 14.1.2. */
3002 switch (procedure_kind (sym))
3004 case PTYPE_GENERIC:
3005 t = resolve_generic_f (expr);
3006 break;
3008 case PTYPE_SPECIFIC:
3009 t = resolve_specific_f (expr);
3010 break;
3012 case PTYPE_UNKNOWN:
3013 t = resolve_unknown_f (expr);
3014 break;
3016 default:
3017 gfc_internal_error ("resolve_function(): bad function type");
3021 /* If the expression is still a function (it might have simplified),
3022 then we check to see if we are calling an elemental function. */
3024 if (expr->expr_type != EXPR_FUNCTION)
3025 return t;
3027 temp = need_full_assumed_size;
3028 need_full_assumed_size = 0;
3030 if (!resolve_elemental_actual (expr, NULL))
3031 return false;
3033 if (omp_workshare_flag
3034 && expr->value.function.esym
3035 && ! gfc_elemental (expr->value.function.esym))
3037 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3038 "in WORKSHARE construct", expr->value.function.esym->name,
3039 &expr->where);
3040 t = false;
3043 #define GENERIC_ID expr->value.function.isym->id
3044 else if (expr->value.function.actual != NULL
3045 && expr->value.function.isym != NULL
3046 && GENERIC_ID != GFC_ISYM_LBOUND
3047 && GENERIC_ID != GFC_ISYM_LCOBOUND
3048 && GENERIC_ID != GFC_ISYM_UCOBOUND
3049 && GENERIC_ID != GFC_ISYM_LEN
3050 && GENERIC_ID != GFC_ISYM_LOC
3051 && GENERIC_ID != GFC_ISYM_C_LOC
3052 && GENERIC_ID != GFC_ISYM_PRESENT)
3054 /* Array intrinsics must also have the last upper bound of an
3055 assumed size array argument. UBOUND and SIZE have to be
3056 excluded from the check if the second argument is anything
3057 than a constant. */
3059 for (arg = expr->value.function.actual; arg; arg = arg->next)
3061 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3062 && arg == expr->value.function.actual
3063 && arg->next != NULL && arg->next->expr)
3065 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3066 break;
3068 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
3069 break;
3071 if ((int)mpz_get_si (arg->next->expr->value.integer)
3072 < arg->expr->rank)
3073 break;
3076 if (arg->expr != NULL
3077 && arg->expr->rank > 0
3078 && resolve_assumed_size_actual (arg->expr))
3079 return false;
3082 #undef GENERIC_ID
3084 need_full_assumed_size = temp;
3086 if (!check_pure_function(expr))
3087 t = false;
3089 /* Functions without the RECURSIVE attribution are not allowed to
3090 * call themselves. */
3091 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3093 gfc_symbol *esym;
3094 esym = expr->value.function.esym;
3096 if (is_illegal_recursion (esym, gfc_current_ns))
3098 if (esym->attr.entry && esym->ns->entries)
3099 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3100 " function %qs is not RECURSIVE",
3101 esym->name, &expr->where, esym->ns->entries->sym->name);
3102 else
3103 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3104 " is not RECURSIVE", esym->name, &expr->where);
3106 t = false;
3110 /* Character lengths of use associated functions may contains references to
3111 symbols not referenced from the current program unit otherwise. Make sure
3112 those symbols are marked as referenced. */
3114 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3115 && expr->value.function.esym->attr.use_assoc)
3117 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3120 /* Make sure that the expression has a typespec that works. */
3121 if (expr->ts.type == BT_UNKNOWN)
3123 if (expr->symtree->n.sym->result
3124 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3125 && !expr->symtree->n.sym->result->attr.proc_pointer)
3126 expr->ts = expr->symtree->n.sym->result->ts;
3129 if (!expr->ref && !expr->value.function.isym)
3131 if (expr->value.function.esym)
3132 update_current_proc_array_outer_dependency (expr->value.function.esym);
3133 else
3134 update_current_proc_array_outer_dependency (sym);
3136 else if (expr->ref)
3137 /* typebound procedure: Assume the worst. */
3138 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3140 return t;
3144 /************* Subroutine resolution *************/
3146 static bool
3147 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3149 if (gfc_pure (sym))
3150 return true;
3152 if (forall_flag)
3154 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3155 name, loc);
3156 return false;
3158 else if (gfc_do_concurrent_flag)
3160 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3161 "PURE", name, loc);
3162 return false;
3164 else if (gfc_pure (NULL))
3166 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3167 return false;
3170 gfc_unset_implicit_pure (NULL);
3171 return true;
3175 static match
3176 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3178 gfc_symbol *s;
3180 if (sym->attr.generic)
3182 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3183 if (s != NULL)
3185 c->resolved_sym = s;
3186 if (!pure_subroutine (s, s->name, &c->loc))
3187 return MATCH_ERROR;
3188 return MATCH_YES;
3191 /* TODO: Need to search for elemental references in generic interface. */
3194 if (sym->attr.intrinsic)
3195 return gfc_intrinsic_sub_interface (c, 0);
3197 return MATCH_NO;
3201 static bool
3202 resolve_generic_s (gfc_code *c)
3204 gfc_symbol *sym;
3205 match m;
3207 sym = c->symtree->n.sym;
3209 for (;;)
3211 m = resolve_generic_s0 (c, sym);
3212 if (m == MATCH_YES)
3213 return true;
3214 else if (m == MATCH_ERROR)
3215 return false;
3217 generic:
3218 if (sym->ns->parent == NULL)
3219 break;
3220 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3222 if (sym == NULL)
3223 break;
3224 if (!generic_sym (sym))
3225 goto generic;
3228 /* Last ditch attempt. See if the reference is to an intrinsic
3229 that possesses a matching interface. 14.1.2.4 */
3230 sym = c->symtree->n.sym;
3232 if (!gfc_is_intrinsic (sym, 1, c->loc))
3234 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3235 sym->name, &c->loc);
3236 return false;
3239 m = gfc_intrinsic_sub_interface (c, 0);
3240 if (m == MATCH_YES)
3241 return true;
3242 if (m == MATCH_NO)
3243 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3244 "intrinsic subroutine interface", sym->name, &c->loc);
3246 return false;
3250 /* Resolve a subroutine call known to be specific. */
3252 static match
3253 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3255 match m;
3257 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3259 if (sym->attr.dummy)
3261 sym->attr.proc = PROC_DUMMY;
3262 goto found;
3265 sym->attr.proc = PROC_EXTERNAL;
3266 goto found;
3269 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3270 goto found;
3272 if (sym->attr.intrinsic)
3274 m = gfc_intrinsic_sub_interface (c, 1);
3275 if (m == MATCH_YES)
3276 return MATCH_YES;
3277 if (m == MATCH_NO)
3278 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3279 "with an intrinsic", sym->name, &c->loc);
3281 return MATCH_ERROR;
3284 return MATCH_NO;
3286 found:
3287 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3289 c->resolved_sym = sym;
3290 if (!pure_subroutine (sym, sym->name, &c->loc))
3291 return MATCH_ERROR;
3293 return MATCH_YES;
3297 static bool
3298 resolve_specific_s (gfc_code *c)
3300 gfc_symbol *sym;
3301 match m;
3303 sym = c->symtree->n.sym;
3305 for (;;)
3307 m = resolve_specific_s0 (c, sym);
3308 if (m == MATCH_YES)
3309 return true;
3310 if (m == MATCH_ERROR)
3311 return false;
3313 if (sym->ns->parent == NULL)
3314 break;
3316 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3318 if (sym == NULL)
3319 break;
3322 sym = c->symtree->n.sym;
3323 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3324 sym->name, &c->loc);
3326 return false;
3330 /* Resolve a subroutine call not known to be generic nor specific. */
3332 static bool
3333 resolve_unknown_s (gfc_code *c)
3335 gfc_symbol *sym;
3337 sym = c->symtree->n.sym;
3339 if (sym->attr.dummy)
3341 sym->attr.proc = PROC_DUMMY;
3342 goto found;
3345 /* See if we have an intrinsic function reference. */
3347 if (gfc_is_intrinsic (sym, 1, c->loc))
3349 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3350 return true;
3351 return false;
3354 /* The reference is to an external name. */
3356 found:
3357 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3359 c->resolved_sym = sym;
3361 return pure_subroutine (sym, sym->name, &c->loc);
3365 /* Resolve a subroutine call. Although it was tempting to use the same code
3366 for functions, subroutines and functions are stored differently and this
3367 makes things awkward. */
3369 static bool
3370 resolve_call (gfc_code *c)
3372 bool t;
3373 procedure_type ptype = PROC_INTRINSIC;
3374 gfc_symbol *csym, *sym;
3375 bool no_formal_args;
3377 csym = c->symtree ? c->symtree->n.sym : NULL;
3379 if (csym && csym->ts.type != BT_UNKNOWN)
3381 gfc_error ("%qs at %L has a type, which is not consistent with "
3382 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3383 return false;
3386 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3388 gfc_symtree *st;
3389 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3390 sym = st ? st->n.sym : NULL;
3391 if (sym && csym != sym
3392 && sym->ns == gfc_current_ns
3393 && sym->attr.flavor == FL_PROCEDURE
3394 && sym->attr.contained)
3396 sym->refs++;
3397 if (csym->attr.generic)
3398 c->symtree->n.sym = sym;
3399 else
3400 c->symtree = st;
3401 csym = c->symtree->n.sym;
3405 /* If this ia a deferred TBP, c->expr1 will be set. */
3406 if (!c->expr1 && csym)
3408 if (csym->attr.abstract)
3410 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3411 csym->name, &c->loc);
3412 return false;
3415 /* Subroutines without the RECURSIVE attribution are not allowed to
3416 call themselves. */
3417 if (is_illegal_recursion (csym, gfc_current_ns))
3419 if (csym->attr.entry && csym->ns->entries)
3420 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3421 "as subroutine %qs is not RECURSIVE",
3422 csym->name, &c->loc, csym->ns->entries->sym->name);
3423 else
3424 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3425 "as it is not RECURSIVE", csym->name, &c->loc);
3427 t = false;
3431 /* Switch off assumed size checking and do this again for certain kinds
3432 of procedure, once the procedure itself is resolved. */
3433 need_full_assumed_size++;
3435 if (csym)
3436 ptype = csym->attr.proc;
3438 no_formal_args = csym && is_external_proc (csym)
3439 && gfc_sym_get_dummy_args (csym) == NULL;
3440 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3441 return false;
3443 /* Resume assumed_size checking. */
3444 need_full_assumed_size--;
3446 /* If external, check for usage. */
3447 if (csym && is_external_proc (csym))
3448 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3450 t = true;
3451 if (c->resolved_sym == NULL)
3453 c->resolved_isym = NULL;
3454 switch (procedure_kind (csym))
3456 case PTYPE_GENERIC:
3457 t = resolve_generic_s (c);
3458 break;
3460 case PTYPE_SPECIFIC:
3461 t = resolve_specific_s (c);
3462 break;
3464 case PTYPE_UNKNOWN:
3465 t = resolve_unknown_s (c);
3466 break;
3468 default:
3469 gfc_internal_error ("resolve_subroutine(): bad function type");
3473 /* Some checks of elemental subroutine actual arguments. */
3474 if (!resolve_elemental_actual (NULL, c))
3475 return false;
3477 if (!c->expr1)
3478 update_current_proc_array_outer_dependency (csym);
3479 else
3480 /* Typebound procedure: Assume the worst. */
3481 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3483 return t;
3487 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3488 op1->shape and op2->shape are non-NULL return true if their shapes
3489 match. If both op1->shape and op2->shape are non-NULL return false
3490 if their shapes do not match. If either op1->shape or op2->shape is
3491 NULL, return true. */
3493 static bool
3494 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3496 bool t;
3497 int i;
3499 t = true;
3501 if (op1->shape != NULL && op2->shape != NULL)
3503 for (i = 0; i < op1->rank; i++)
3505 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3507 gfc_error ("Shapes for operands at %L and %L are not conformable",
3508 &op1->where, &op2->where);
3509 t = false;
3510 break;
3515 return t;
3519 /* Resolve an operator expression node. This can involve replacing the
3520 operation with a user defined function call. */
3522 static bool
3523 resolve_operator (gfc_expr *e)
3525 gfc_expr *op1, *op2;
3526 char msg[200];
3527 bool dual_locus_error;
3528 bool t;
3530 /* Resolve all subnodes-- give them types. */
3532 switch (e->value.op.op)
3534 default:
3535 if (!gfc_resolve_expr (e->value.op.op2))
3536 return false;
3538 /* Fall through... */
3540 case INTRINSIC_NOT:
3541 case INTRINSIC_UPLUS:
3542 case INTRINSIC_UMINUS:
3543 case INTRINSIC_PARENTHESES:
3544 if (!gfc_resolve_expr (e->value.op.op1))
3545 return false;
3546 break;
3549 /* Typecheck the new node. */
3551 op1 = e->value.op.op1;
3552 op2 = e->value.op.op2;
3553 dual_locus_error = false;
3555 if ((op1 && op1->expr_type == EXPR_NULL)
3556 || (op2 && op2->expr_type == EXPR_NULL))
3558 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3559 goto bad_op;
3562 switch (e->value.op.op)
3564 case INTRINSIC_UPLUS:
3565 case INTRINSIC_UMINUS:
3566 if (op1->ts.type == BT_INTEGER
3567 || op1->ts.type == BT_REAL
3568 || op1->ts.type == BT_COMPLEX)
3570 e->ts = op1->ts;
3571 break;
3574 sprintf (msg, _("Operand of unary numeric operator %%<%s%%> at %%L is %s"),
3575 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3576 goto bad_op;
3578 case INTRINSIC_PLUS:
3579 case INTRINSIC_MINUS:
3580 case INTRINSIC_TIMES:
3581 case INTRINSIC_DIVIDE:
3582 case INTRINSIC_POWER:
3583 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3585 gfc_type_convert_binary (e, 1);
3586 break;
3589 sprintf (msg,
3590 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"),
3591 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3592 gfc_typename (&op2->ts));
3593 goto bad_op;
3595 case INTRINSIC_CONCAT:
3596 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3597 && op1->ts.kind == op2->ts.kind)
3599 e->ts.type = BT_CHARACTER;
3600 e->ts.kind = op1->ts.kind;
3601 break;
3604 sprintf (msg,
3605 _("Operands of string concatenation operator at %%L are %s/%s"),
3606 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3607 goto bad_op;
3609 case INTRINSIC_AND:
3610 case INTRINSIC_OR:
3611 case INTRINSIC_EQV:
3612 case INTRINSIC_NEQV:
3613 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3615 e->ts.type = BT_LOGICAL;
3616 e->ts.kind = gfc_kind_max (op1, op2);
3617 if (op1->ts.kind < e->ts.kind)
3618 gfc_convert_type (op1, &e->ts, 2);
3619 else if (op2->ts.kind < e->ts.kind)
3620 gfc_convert_type (op2, &e->ts, 2);
3621 break;
3624 sprintf (msg, _("Operands of logical operator %%<%s%%> at %%L are %s/%s"),
3625 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3626 gfc_typename (&op2->ts));
3628 goto bad_op;
3630 case INTRINSIC_NOT:
3631 if (op1->ts.type == BT_LOGICAL)
3633 e->ts.type = BT_LOGICAL;
3634 e->ts.kind = op1->ts.kind;
3635 break;
3638 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3639 gfc_typename (&op1->ts));
3640 goto bad_op;
3642 case INTRINSIC_GT:
3643 case INTRINSIC_GT_OS:
3644 case INTRINSIC_GE:
3645 case INTRINSIC_GE_OS:
3646 case INTRINSIC_LT:
3647 case INTRINSIC_LT_OS:
3648 case INTRINSIC_LE:
3649 case INTRINSIC_LE_OS:
3650 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3652 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3653 goto bad_op;
3656 /* Fall through... */
3658 case INTRINSIC_EQ:
3659 case INTRINSIC_EQ_OS:
3660 case INTRINSIC_NE:
3661 case INTRINSIC_NE_OS:
3662 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3663 && op1->ts.kind == op2->ts.kind)
3665 e->ts.type = BT_LOGICAL;
3666 e->ts.kind = gfc_default_logical_kind;
3667 break;
3670 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3672 gfc_type_convert_binary (e, 1);
3674 e->ts.type = BT_LOGICAL;
3675 e->ts.kind = gfc_default_logical_kind;
3677 if (warn_compare_reals)
3679 gfc_intrinsic_op op = e->value.op.op;
3681 /* Type conversion has made sure that the types of op1 and op2
3682 agree, so it is only necessary to check the first one. */
3683 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3684 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3685 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3687 const char *msg;
3689 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3690 msg = "Equality comparison for %s at %L";
3691 else
3692 msg = "Inequality comparison for %s at %L";
3694 gfc_warning (0, msg, gfc_typename (&op1->ts), &op1->where);
3698 break;
3701 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3702 sprintf (msg,
3703 _("Logicals at %%L must be compared with %s instead of %s"),
3704 (e->value.op.op == INTRINSIC_EQ
3705 || e->value.op.op == INTRINSIC_EQ_OS)
3706 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3707 else
3708 sprintf (msg,
3709 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"),
3710 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3711 gfc_typename (&op2->ts));
3713 goto bad_op;
3715 case INTRINSIC_USER:
3716 if (e->value.op.uop->op == NULL)
3717 sprintf (msg, _("Unknown operator %%<%s%%> at %%L"),
3718 e->value.op.uop->name);
3719 else if (op2 == NULL)
3720 sprintf (msg, _("Operand of user operator %%<%s%%> at %%L is %s"),
3721 e->value.op.uop->name, gfc_typename (&op1->ts));
3722 else
3724 sprintf (msg, _("Operands of user operator %%<%s%%> at %%L are %s/%s"),
3725 e->value.op.uop->name, gfc_typename (&op1->ts),
3726 gfc_typename (&op2->ts));
3727 e->value.op.uop->op->sym->attr.referenced = 1;
3730 goto bad_op;
3732 case INTRINSIC_PARENTHESES:
3733 e->ts = op1->ts;
3734 if (e->ts.type == BT_CHARACTER)
3735 e->ts.u.cl = op1->ts.u.cl;
3736 break;
3738 default:
3739 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3742 /* Deal with arrayness of an operand through an operator. */
3744 t = true;
3746 switch (e->value.op.op)
3748 case INTRINSIC_PLUS:
3749 case INTRINSIC_MINUS:
3750 case INTRINSIC_TIMES:
3751 case INTRINSIC_DIVIDE:
3752 case INTRINSIC_POWER:
3753 case INTRINSIC_CONCAT:
3754 case INTRINSIC_AND:
3755 case INTRINSIC_OR:
3756 case INTRINSIC_EQV:
3757 case INTRINSIC_NEQV:
3758 case INTRINSIC_EQ:
3759 case INTRINSIC_EQ_OS:
3760 case INTRINSIC_NE:
3761 case INTRINSIC_NE_OS:
3762 case INTRINSIC_GT:
3763 case INTRINSIC_GT_OS:
3764 case INTRINSIC_GE:
3765 case INTRINSIC_GE_OS:
3766 case INTRINSIC_LT:
3767 case INTRINSIC_LT_OS:
3768 case INTRINSIC_LE:
3769 case INTRINSIC_LE_OS:
3771 if (op1->rank == 0 && op2->rank == 0)
3772 e->rank = 0;
3774 if (op1->rank == 0 && op2->rank != 0)
3776 e->rank = op2->rank;
3778 if (e->shape == NULL)
3779 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3782 if (op1->rank != 0 && op2->rank == 0)
3784 e->rank = op1->rank;
3786 if (e->shape == NULL)
3787 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3790 if (op1->rank != 0 && op2->rank != 0)
3792 if (op1->rank == op2->rank)
3794 e->rank = op1->rank;
3795 if (e->shape == NULL)
3797 t = compare_shapes (op1, op2);
3798 if (!t)
3799 e->shape = NULL;
3800 else
3801 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3804 else
3806 /* Allow higher level expressions to work. */
3807 e->rank = 0;
3809 /* Try user-defined operators, and otherwise throw an error. */
3810 dual_locus_error = true;
3811 sprintf (msg,
3812 _("Inconsistent ranks for operator at %%L and %%L"));
3813 goto bad_op;
3817 break;
3819 case INTRINSIC_PARENTHESES:
3820 case INTRINSIC_NOT:
3821 case INTRINSIC_UPLUS:
3822 case INTRINSIC_UMINUS:
3823 /* Simply copy arrayness attribute */
3824 e->rank = op1->rank;
3826 if (e->shape == NULL)
3827 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3829 break;
3831 default:
3832 break;
3835 /* Attempt to simplify the expression. */
3836 if (t)
3838 t = gfc_simplify_expr (e, 0);
3839 /* Some calls do not succeed in simplification and return false
3840 even though there is no error; e.g. variable references to
3841 PARAMETER arrays. */
3842 if (!gfc_is_constant_expr (e))
3843 t = true;
3845 return t;
3847 bad_op:
3850 match m = gfc_extend_expr (e);
3851 if (m == MATCH_YES)
3852 return true;
3853 if (m == MATCH_ERROR)
3854 return false;
3857 if (dual_locus_error)
3858 gfc_error (msg, &op1->where, &op2->where);
3859 else
3860 gfc_error (msg, &e->where);
3862 return false;
3866 /************** Array resolution subroutines **************/
3868 enum compare_result
3869 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
3871 /* Compare two integer expressions. */
3873 static compare_result
3874 compare_bound (gfc_expr *a, gfc_expr *b)
3876 int i;
3878 if (a == NULL || a->expr_type != EXPR_CONSTANT
3879 || b == NULL || b->expr_type != EXPR_CONSTANT)
3880 return CMP_UNKNOWN;
3882 /* If either of the types isn't INTEGER, we must have
3883 raised an error earlier. */
3885 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3886 return CMP_UNKNOWN;
3888 i = mpz_cmp (a->value.integer, b->value.integer);
3890 if (i < 0)
3891 return CMP_LT;
3892 if (i > 0)
3893 return CMP_GT;
3894 return CMP_EQ;
3898 /* Compare an integer expression with an integer. */
3900 static compare_result
3901 compare_bound_int (gfc_expr *a, int b)
3903 int i;
3905 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3906 return CMP_UNKNOWN;
3908 if (a->ts.type != BT_INTEGER)
3909 gfc_internal_error ("compare_bound_int(): Bad expression");
3911 i = mpz_cmp_si (a->value.integer, b);
3913 if (i < 0)
3914 return CMP_LT;
3915 if (i > 0)
3916 return CMP_GT;
3917 return CMP_EQ;
3921 /* Compare an integer expression with a mpz_t. */
3923 static compare_result
3924 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3926 int i;
3928 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3929 return CMP_UNKNOWN;
3931 if (a->ts.type != BT_INTEGER)
3932 gfc_internal_error ("compare_bound_int(): Bad expression");
3934 i = mpz_cmp (a->value.integer, b);
3936 if (i < 0)
3937 return CMP_LT;
3938 if (i > 0)
3939 return CMP_GT;
3940 return CMP_EQ;
3944 /* Compute the last value of a sequence given by a triplet.
3945 Return 0 if it wasn't able to compute the last value, or if the
3946 sequence if empty, and 1 otherwise. */
3948 static int
3949 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3950 gfc_expr *stride, mpz_t last)
3952 mpz_t rem;
3954 if (start == NULL || start->expr_type != EXPR_CONSTANT
3955 || end == NULL || end->expr_type != EXPR_CONSTANT
3956 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3957 return 0;
3959 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3960 || (stride != NULL && stride->ts.type != BT_INTEGER))
3961 return 0;
3963 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
3965 if (compare_bound (start, end) == CMP_GT)
3966 return 0;
3967 mpz_set (last, end->value.integer);
3968 return 1;
3971 if (compare_bound_int (stride, 0) == CMP_GT)
3973 /* Stride is positive */
3974 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3975 return 0;
3977 else
3979 /* Stride is negative */
3980 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3981 return 0;
3984 mpz_init (rem);
3985 mpz_sub (rem, end->value.integer, start->value.integer);
3986 mpz_tdiv_r (rem, rem, stride->value.integer);
3987 mpz_sub (last, end->value.integer, rem);
3988 mpz_clear (rem);
3990 return 1;
3994 /* Compare a single dimension of an array reference to the array
3995 specification. */
3997 static bool
3998 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4000 mpz_t last_value;
4002 if (ar->dimen_type[i] == DIMEN_STAR)
4004 gcc_assert (ar->stride[i] == NULL);
4005 /* This implies [*] as [*:] and [*:3] are not possible. */
4006 if (ar->start[i] == NULL)
4008 gcc_assert (ar->end[i] == NULL);
4009 return true;
4013 /* Given start, end and stride values, calculate the minimum and
4014 maximum referenced indexes. */
4016 switch (ar->dimen_type[i])
4018 case DIMEN_VECTOR:
4019 case DIMEN_THIS_IMAGE:
4020 break;
4022 case DIMEN_STAR:
4023 case DIMEN_ELEMENT:
4024 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4026 if (i < as->rank)
4027 gfc_warning (0, "Array reference at %L is out of bounds "
4028 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4029 mpz_get_si (ar->start[i]->value.integer),
4030 mpz_get_si (as->lower[i]->value.integer), i+1);
4031 else
4032 gfc_warning (0, "Array reference at %L is out of bounds "
4033 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4034 mpz_get_si (ar->start[i]->value.integer),
4035 mpz_get_si (as->lower[i]->value.integer),
4036 i + 1 - as->rank);
4037 return true;
4039 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4041 if (i < as->rank)
4042 gfc_warning (0, "Array reference at %L is out of bounds "
4043 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4044 mpz_get_si (ar->start[i]->value.integer),
4045 mpz_get_si (as->upper[i]->value.integer), i+1);
4046 else
4047 gfc_warning (0, "Array reference at %L is out of bounds "
4048 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4049 mpz_get_si (ar->start[i]->value.integer),
4050 mpz_get_si (as->upper[i]->value.integer),
4051 i + 1 - as->rank);
4052 return true;
4055 break;
4057 case DIMEN_RANGE:
4059 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4060 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4062 compare_result comp_start_end = compare_bound (AR_START, AR_END);
4064 /* Check for zero stride, which is not allowed. */
4065 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4067 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4068 return false;
4071 /* if start == len || (stride > 0 && start < len)
4072 || (stride < 0 && start > len),
4073 then the array section contains at least one element. In this
4074 case, there is an out-of-bounds access if
4075 (start < lower || start > upper). */
4076 if (compare_bound (AR_START, AR_END) == CMP_EQ
4077 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4078 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4079 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4080 && comp_start_end == CMP_GT))
4082 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4084 gfc_warning (0, "Lower array reference at %L is out of bounds "
4085 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4086 mpz_get_si (AR_START->value.integer),
4087 mpz_get_si (as->lower[i]->value.integer), i+1);
4088 return true;
4090 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4092 gfc_warning (0, "Lower array reference at %L is out of bounds "
4093 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4094 mpz_get_si (AR_START->value.integer),
4095 mpz_get_si (as->upper[i]->value.integer), i+1);
4096 return true;
4100 /* If we can compute the highest index of the array section,
4101 then it also has to be between lower and upper. */
4102 mpz_init (last_value);
4103 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4104 last_value))
4106 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4108 gfc_warning (0, "Upper array reference at %L is out of bounds "
4109 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4110 mpz_get_si (last_value),
4111 mpz_get_si (as->lower[i]->value.integer), i+1);
4112 mpz_clear (last_value);
4113 return true;
4115 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4117 gfc_warning (0, "Upper array reference at %L is out of bounds "
4118 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4119 mpz_get_si (last_value),
4120 mpz_get_si (as->upper[i]->value.integer), i+1);
4121 mpz_clear (last_value);
4122 return true;
4125 mpz_clear (last_value);
4127 #undef AR_START
4128 #undef AR_END
4130 break;
4132 default:
4133 gfc_internal_error ("check_dimension(): Bad array reference");
4136 return true;
4140 /* Compare an array reference with an array specification. */
4142 static bool
4143 compare_spec_to_ref (gfc_array_ref *ar)
4145 gfc_array_spec *as;
4146 int i;
4148 as = ar->as;
4149 i = as->rank - 1;
4150 /* TODO: Full array sections are only allowed as actual parameters. */
4151 if (as->type == AS_ASSUMED_SIZE
4152 && (/*ar->type == AR_FULL
4153 ||*/ (ar->type == AR_SECTION
4154 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4156 gfc_error ("Rightmost upper bound of assumed size array section "
4157 "not specified at %L", &ar->where);
4158 return false;
4161 if (ar->type == AR_FULL)
4162 return true;
4164 if (as->rank != ar->dimen)
4166 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4167 &ar->where, ar->dimen, as->rank);
4168 return false;
4171 /* ar->codimen == 0 is a local array. */
4172 if (as->corank != ar->codimen && ar->codimen != 0)
4174 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4175 &ar->where, ar->codimen, as->corank);
4176 return false;
4179 for (i = 0; i < as->rank; i++)
4180 if (!check_dimension (i, ar, as))
4181 return false;
4183 /* Local access has no coarray spec. */
4184 if (ar->codimen != 0)
4185 for (i = as->rank; i < as->rank + as->corank; i++)
4187 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4188 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4190 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4191 i + 1 - as->rank, &ar->where);
4192 return false;
4194 if (!check_dimension (i, ar, as))
4195 return false;
4198 return true;
4202 /* Resolve one part of an array index. */
4204 static bool
4205 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4206 int force_index_integer_kind)
4208 gfc_typespec ts;
4210 if (index == NULL)
4211 return true;
4213 if (!gfc_resolve_expr (index))
4214 return false;
4216 if (check_scalar && index->rank != 0)
4218 gfc_error ("Array index at %L must be scalar", &index->where);
4219 return false;
4222 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4224 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4225 &index->where, gfc_basic_typename (index->ts.type));
4226 return false;
4229 if (index->ts.type == BT_REAL)
4230 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4231 &index->where))
4232 return false;
4234 if ((index->ts.kind != gfc_index_integer_kind
4235 && force_index_integer_kind)
4236 || index->ts.type != BT_INTEGER)
4238 gfc_clear_ts (&ts);
4239 ts.type = BT_INTEGER;
4240 ts.kind = gfc_index_integer_kind;
4242 gfc_convert_type_warn (index, &ts, 2, 0);
4245 return true;
4248 /* Resolve one part of an array index. */
4250 bool
4251 gfc_resolve_index (gfc_expr *index, int check_scalar)
4253 return gfc_resolve_index_1 (index, check_scalar, 1);
4256 /* Resolve a dim argument to an intrinsic function. */
4258 bool
4259 gfc_resolve_dim_arg (gfc_expr *dim)
4261 if (dim == NULL)
4262 return true;
4264 if (!gfc_resolve_expr (dim))
4265 return false;
4267 if (dim->rank != 0)
4269 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4270 return false;
4274 if (dim->ts.type != BT_INTEGER)
4276 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4277 return false;
4280 if (dim->ts.kind != gfc_index_integer_kind)
4282 gfc_typespec ts;
4284 gfc_clear_ts (&ts);
4285 ts.type = BT_INTEGER;
4286 ts.kind = gfc_index_integer_kind;
4288 gfc_convert_type_warn (dim, &ts, 2, 0);
4291 return true;
4294 /* Given an expression that contains array references, update those array
4295 references to point to the right array specifications. While this is
4296 filled in during matching, this information is difficult to save and load
4297 in a module, so we take care of it here.
4299 The idea here is that the original array reference comes from the
4300 base symbol. We traverse the list of reference structures, setting
4301 the stored reference to references. Component references can
4302 provide an additional array specification. */
4304 static void
4305 find_array_spec (gfc_expr *e)
4307 gfc_array_spec *as;
4308 gfc_component *c;
4309 gfc_ref *ref;
4311 if (e->symtree->n.sym->ts.type == BT_CLASS)
4312 as = CLASS_DATA (e->symtree->n.sym)->as;
4313 else
4314 as = e->symtree->n.sym->as;
4316 for (ref = e->ref; ref; ref = ref->next)
4317 switch (ref->type)
4319 case REF_ARRAY:
4320 if (as == NULL)
4321 gfc_internal_error ("find_array_spec(): Missing spec");
4323 ref->u.ar.as = as;
4324 as = NULL;
4325 break;
4327 case REF_COMPONENT:
4328 c = ref->u.c.component;
4329 if (c->attr.dimension)
4331 if (as != NULL)
4332 gfc_internal_error ("find_array_spec(): unused as(1)");
4333 as = c->as;
4336 break;
4338 case REF_SUBSTRING:
4339 break;
4342 if (as != NULL)
4343 gfc_internal_error ("find_array_spec(): unused as(2)");
4347 /* Resolve an array reference. */
4349 static bool
4350 resolve_array_ref (gfc_array_ref *ar)
4352 int i, check_scalar;
4353 gfc_expr *e;
4355 for (i = 0; i < ar->dimen + ar->codimen; i++)
4357 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4359 /* Do not force gfc_index_integer_kind for the start. We can
4360 do fine with any integer kind. This avoids temporary arrays
4361 created for indexing with a vector. */
4362 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4363 return false;
4364 if (!gfc_resolve_index (ar->end[i], check_scalar))
4365 return false;
4366 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4367 return false;
4369 e = ar->start[i];
4371 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4372 switch (e->rank)
4374 case 0:
4375 ar->dimen_type[i] = DIMEN_ELEMENT;
4376 break;
4378 case 1:
4379 ar->dimen_type[i] = DIMEN_VECTOR;
4380 if (e->expr_type == EXPR_VARIABLE
4381 && e->symtree->n.sym->ts.type == BT_DERIVED)
4382 ar->start[i] = gfc_get_parentheses (e);
4383 break;
4385 default:
4386 gfc_error ("Array index at %L is an array of rank %d",
4387 &ar->c_where[i], e->rank);
4388 return false;
4391 /* Fill in the upper bound, which may be lower than the
4392 specified one for something like a(2:10:5), which is
4393 identical to a(2:7:5). Only relevant for strides not equal
4394 to one. Don't try a division by zero. */
4395 if (ar->dimen_type[i] == DIMEN_RANGE
4396 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4397 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4398 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4400 mpz_t size, end;
4402 if (gfc_ref_dimen_size (ar, i, &size, &end))
4404 if (ar->end[i] == NULL)
4406 ar->end[i] =
4407 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4408 &ar->where);
4409 mpz_set (ar->end[i]->value.integer, end);
4411 else if (ar->end[i]->ts.type == BT_INTEGER
4412 && ar->end[i]->expr_type == EXPR_CONSTANT)
4414 mpz_set (ar->end[i]->value.integer, end);
4416 else
4417 gcc_unreachable ();
4419 mpz_clear (size);
4420 mpz_clear (end);
4425 if (ar->type == AR_FULL)
4427 if (ar->as->rank == 0)
4428 ar->type = AR_ELEMENT;
4430 /* Make sure array is the same as array(:,:), this way
4431 we don't need to special case all the time. */
4432 ar->dimen = ar->as->rank;
4433 for (i = 0; i < ar->dimen; i++)
4435 ar->dimen_type[i] = DIMEN_RANGE;
4437 gcc_assert (ar->start[i] == NULL);
4438 gcc_assert (ar->end[i] == NULL);
4439 gcc_assert (ar->stride[i] == NULL);
4443 /* If the reference type is unknown, figure out what kind it is. */
4445 if (ar->type == AR_UNKNOWN)
4447 ar->type = AR_ELEMENT;
4448 for (i = 0; i < ar->dimen; i++)
4449 if (ar->dimen_type[i] == DIMEN_RANGE
4450 || ar->dimen_type[i] == DIMEN_VECTOR)
4452 ar->type = AR_SECTION;
4453 break;
4457 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4458 return false;
4460 if (ar->as->corank && ar->codimen == 0)
4462 int n;
4463 ar->codimen = ar->as->corank;
4464 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4465 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4468 return true;
4472 static bool
4473 resolve_substring (gfc_ref *ref)
4475 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4477 if (ref->u.ss.start != NULL)
4479 if (!gfc_resolve_expr (ref->u.ss.start))
4480 return false;
4482 if (ref->u.ss.start->ts.type != BT_INTEGER)
4484 gfc_error ("Substring start index at %L must be of type INTEGER",
4485 &ref->u.ss.start->where);
4486 return false;
4489 if (ref->u.ss.start->rank != 0)
4491 gfc_error ("Substring start index at %L must be scalar",
4492 &ref->u.ss.start->where);
4493 return false;
4496 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4497 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4498 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4500 gfc_error ("Substring start index at %L is less than one",
4501 &ref->u.ss.start->where);
4502 return false;
4506 if (ref->u.ss.end != NULL)
4508 if (!gfc_resolve_expr (ref->u.ss.end))
4509 return false;
4511 if (ref->u.ss.end->ts.type != BT_INTEGER)
4513 gfc_error ("Substring end index at %L must be of type INTEGER",
4514 &ref->u.ss.end->where);
4515 return false;
4518 if (ref->u.ss.end->rank != 0)
4520 gfc_error ("Substring end index at %L must be scalar",
4521 &ref->u.ss.end->where);
4522 return false;
4525 if (ref->u.ss.length != NULL
4526 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4527 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4528 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4530 gfc_error ("Substring end index at %L exceeds the string length",
4531 &ref->u.ss.start->where);
4532 return false;
4535 if (compare_bound_mpz_t (ref->u.ss.end,
4536 gfc_integer_kinds[k].huge) == CMP_GT
4537 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4538 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4540 gfc_error ("Substring end index at %L is too large",
4541 &ref->u.ss.end->where);
4542 return false;
4546 return true;
4550 /* This function supplies missing substring charlens. */
4552 void
4553 gfc_resolve_substring_charlen (gfc_expr *e)
4555 gfc_ref *char_ref;
4556 gfc_expr *start, *end;
4557 gfc_typespec *ts = NULL;
4559 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4561 if (char_ref->type == REF_SUBSTRING)
4562 break;
4563 if (char_ref->type == REF_COMPONENT)
4564 ts = &char_ref->u.c.component->ts;
4567 if (!char_ref)
4568 return;
4570 gcc_assert (char_ref->next == NULL);
4572 if (e->ts.u.cl)
4574 if (e->ts.u.cl->length)
4575 gfc_free_expr (e->ts.u.cl->length);
4576 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
4577 return;
4580 e->ts.type = BT_CHARACTER;
4581 e->ts.kind = gfc_default_character_kind;
4583 if (!e->ts.u.cl)
4584 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4586 if (char_ref->u.ss.start)
4587 start = gfc_copy_expr (char_ref->u.ss.start);
4588 else
4589 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4591 if (char_ref->u.ss.end)
4592 end = gfc_copy_expr (char_ref->u.ss.end);
4593 else if (e->expr_type == EXPR_VARIABLE)
4595 if (!ts)
4596 ts = &e->symtree->n.sym->ts;
4597 end = gfc_copy_expr (ts->u.cl->length);
4599 else
4600 end = NULL;
4602 if (!start || !end)
4604 gfc_free_expr (start);
4605 gfc_free_expr (end);
4606 return;
4609 /* Length = (end - start + 1). */
4610 e->ts.u.cl->length = gfc_subtract (end, start);
4611 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4612 gfc_get_int_expr (gfc_default_integer_kind,
4613 NULL, 1));
4615 /* F2008, 6.4.1: Both the starting point and the ending point shall
4616 be within the range 1, 2, ..., n unless the starting point exceeds
4617 the ending point, in which case the substring has length zero. */
4619 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
4620 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
4622 e->ts.u.cl->length->ts.type = BT_INTEGER;
4623 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4625 /* Make sure that the length is simplified. */
4626 gfc_simplify_expr (e->ts.u.cl->length, 1);
4627 gfc_resolve_expr (e->ts.u.cl->length);
4631 /* Resolve subtype references. */
4633 static bool
4634 resolve_ref (gfc_expr *expr)
4636 int current_part_dimension, n_components, seen_part_dimension;
4637 gfc_ref *ref;
4639 for (ref = expr->ref; ref; ref = ref->next)
4640 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4642 find_array_spec (expr);
4643 break;
4646 for (ref = expr->ref; ref; ref = ref->next)
4647 switch (ref->type)
4649 case REF_ARRAY:
4650 if (!resolve_array_ref (&ref->u.ar))
4651 return false;
4652 break;
4654 case REF_COMPONENT:
4655 break;
4657 case REF_SUBSTRING:
4658 if (!resolve_substring (ref))
4659 return false;
4660 break;
4663 /* Check constraints on part references. */
4665 current_part_dimension = 0;
4666 seen_part_dimension = 0;
4667 n_components = 0;
4669 for (ref = expr->ref; ref; ref = ref->next)
4671 switch (ref->type)
4673 case REF_ARRAY:
4674 switch (ref->u.ar.type)
4676 case AR_FULL:
4677 /* Coarray scalar. */
4678 if (ref->u.ar.as->rank == 0)
4680 current_part_dimension = 0;
4681 break;
4683 /* Fall through. */
4684 case AR_SECTION:
4685 current_part_dimension = 1;
4686 break;
4688 case AR_ELEMENT:
4689 current_part_dimension = 0;
4690 break;
4692 case AR_UNKNOWN:
4693 gfc_internal_error ("resolve_ref(): Bad array reference");
4696 break;
4698 case REF_COMPONENT:
4699 if (current_part_dimension || seen_part_dimension)
4701 /* F03:C614. */
4702 if (ref->u.c.component->attr.pointer
4703 || ref->u.c.component->attr.proc_pointer
4704 || (ref->u.c.component->ts.type == BT_CLASS
4705 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4707 gfc_error ("Component to the right of a part reference "
4708 "with nonzero rank must not have the POINTER "
4709 "attribute at %L", &expr->where);
4710 return false;
4712 else if (ref->u.c.component->attr.allocatable
4713 || (ref->u.c.component->ts.type == BT_CLASS
4714 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4717 gfc_error ("Component to the right of a part reference "
4718 "with nonzero rank must not have the ALLOCATABLE "
4719 "attribute at %L", &expr->where);
4720 return false;
4724 n_components++;
4725 break;
4727 case REF_SUBSTRING:
4728 break;
4731 if (((ref->type == REF_COMPONENT && n_components > 1)
4732 || ref->next == NULL)
4733 && current_part_dimension
4734 && seen_part_dimension)
4736 gfc_error ("Two or more part references with nonzero rank must "
4737 "not be specified at %L", &expr->where);
4738 return false;
4741 if (ref->type == REF_COMPONENT)
4743 if (current_part_dimension)
4744 seen_part_dimension = 1;
4746 /* reset to make sure */
4747 current_part_dimension = 0;
4751 return true;
4755 /* Given an expression, determine its shape. This is easier than it sounds.
4756 Leaves the shape array NULL if it is not possible to determine the shape. */
4758 static void
4759 expression_shape (gfc_expr *e)
4761 mpz_t array[GFC_MAX_DIMENSIONS];
4762 int i;
4764 if (e->rank <= 0 || e->shape != NULL)
4765 return;
4767 for (i = 0; i < e->rank; i++)
4768 if (!gfc_array_dimen_size (e, i, &array[i]))
4769 goto fail;
4771 e->shape = gfc_get_shape (e->rank);
4773 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4775 return;
4777 fail:
4778 for (i--; i >= 0; i--)
4779 mpz_clear (array[i]);
4783 /* Given a variable expression node, compute the rank of the expression by
4784 examining the base symbol and any reference structures it may have. */
4786 void
4787 expression_rank (gfc_expr *e)
4789 gfc_ref *ref;
4790 int i, rank;
4792 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4793 could lead to serious confusion... */
4794 gcc_assert (e->expr_type != EXPR_COMPCALL);
4796 if (e->ref == NULL)
4798 if (e->expr_type == EXPR_ARRAY)
4799 goto done;
4800 /* Constructors can have a rank different from one via RESHAPE(). */
4802 if (e->symtree == NULL)
4804 e->rank = 0;
4805 goto done;
4808 e->rank = (e->symtree->n.sym->as == NULL)
4809 ? 0 : e->symtree->n.sym->as->rank;
4810 goto done;
4813 rank = 0;
4815 for (ref = e->ref; ref; ref = ref->next)
4817 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4818 && ref->u.c.component->attr.function && !ref->next)
4819 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4821 if (ref->type != REF_ARRAY)
4822 continue;
4824 if (ref->u.ar.type == AR_FULL)
4826 rank = ref->u.ar.as->rank;
4827 break;
4830 if (ref->u.ar.type == AR_SECTION)
4832 /* Figure out the rank of the section. */
4833 if (rank != 0)
4834 gfc_internal_error ("expression_rank(): Two array specs");
4836 for (i = 0; i < ref->u.ar.dimen; i++)
4837 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4838 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4839 rank++;
4841 break;
4845 e->rank = rank;
4847 done:
4848 expression_shape (e);
4852 static void
4853 add_caf_get_intrinsic (gfc_expr *e)
4855 gfc_expr *wrapper, *tmp_expr;
4856 gfc_ref *ref;
4857 int n;
4859 for (ref = e->ref; ref; ref = ref->next)
4860 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4861 break;
4862 if (ref == NULL)
4863 return;
4865 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
4866 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
4867 return;
4869 tmp_expr = XCNEW (gfc_expr);
4870 *tmp_expr = *e;
4871 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
4872 "caf_get", tmp_expr->where, 1, tmp_expr);
4873 wrapper->ts = e->ts;
4874 wrapper->rank = e->rank;
4875 if (e->rank)
4876 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
4877 *e = *wrapper;
4878 free (wrapper);
4882 static void
4883 remove_caf_get_intrinsic (gfc_expr *e)
4885 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
4886 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
4887 gfc_expr *e2 = e->value.function.actual->expr;
4888 e->value.function.actual->expr = NULL;
4889 gfc_free_actual_arglist (e->value.function.actual);
4890 gfc_free_shape (&e->shape, e->rank);
4891 *e = *e2;
4892 free (e2);
4896 /* Resolve a variable expression. */
4898 static bool
4899 resolve_variable (gfc_expr *e)
4901 gfc_symbol *sym;
4902 bool t;
4904 t = true;
4906 if (e->symtree == NULL)
4907 return false;
4908 sym = e->symtree->n.sym;
4910 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4911 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4912 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4914 if (!actual_arg || inquiry_argument)
4916 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4917 "be used as actual argument", sym->name, &e->where);
4918 return false;
4921 /* TS 29113, 407b. */
4922 else if (e->ts.type == BT_ASSUMED)
4924 if (!actual_arg)
4926 gfc_error ("Assumed-type variable %s at %L may only be used "
4927 "as actual argument", sym->name, &e->where);
4928 return false;
4930 else if (inquiry_argument && !first_actual_arg)
4932 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4933 for all inquiry functions in resolve_function; the reason is
4934 that the function-name resolution happens too late in that
4935 function. */
4936 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4937 "an inquiry function shall be the first argument",
4938 sym->name, &e->where);
4939 return false;
4942 /* TS 29113, C535b. */
4943 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4944 && CLASS_DATA (sym)->as
4945 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4946 || (sym->ts.type != BT_CLASS && sym->as
4947 && sym->as->type == AS_ASSUMED_RANK))
4949 if (!actual_arg)
4951 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4952 "actual argument", sym->name, &e->where);
4953 return false;
4955 else if (inquiry_argument && !first_actual_arg)
4957 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4958 for all inquiry functions in resolve_function; the reason is
4959 that the function-name resolution happens too late in that
4960 function. */
4961 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4962 "to an inquiry function shall be the first argument",
4963 sym->name, &e->where);
4964 return false;
4968 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
4969 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4970 && e->ref->next == NULL))
4972 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4973 "a subobject reference", sym->name, &e->ref->u.ar.where);
4974 return false;
4976 /* TS 29113, 407b. */
4977 else if (e->ts.type == BT_ASSUMED && e->ref
4978 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4979 && e->ref->next == NULL))
4981 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4982 "reference", sym->name, &e->ref->u.ar.where);
4983 return false;
4986 /* TS 29113, C535b. */
4987 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4988 && CLASS_DATA (sym)->as
4989 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4990 || (sym->ts.type != BT_CLASS && sym->as
4991 && sym->as->type == AS_ASSUMED_RANK))
4992 && e->ref
4993 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4994 && e->ref->next == NULL))
4996 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
4997 "reference", sym->name, &e->ref->u.ar.where);
4998 return false;
5001 /* For variables that are used in an associate (target => object) where
5002 the object's basetype is array valued while the target is scalar,
5003 the ts' type of the component refs is still array valued, which
5004 can't be translated that way. */
5005 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
5006 && sym->assoc->target->ts.type == BT_CLASS
5007 && CLASS_DATA (sym->assoc->target)->as)
5009 gfc_ref *ref = e->ref;
5010 while (ref)
5012 switch (ref->type)
5014 case REF_COMPONENT:
5015 ref->u.c.sym = sym->ts.u.derived;
5016 /* Stop the loop. */
5017 ref = NULL;
5018 break;
5019 default:
5020 ref = ref->next;
5021 break;
5026 /* If this is an associate-name, it may be parsed with an array reference
5027 in error even though the target is scalar. Fail directly in this case.
5028 TODO Understand why class scalar expressions must be excluded. */
5029 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5031 if (sym->ts.type == BT_CLASS)
5032 gfc_fix_class_refs (e);
5033 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5034 return false;
5037 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5038 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5040 /* On the other hand, the parser may not have known this is an array;
5041 in this case, we have to add a FULL reference. */
5042 if (sym->assoc && sym->attr.dimension && !e->ref)
5044 e->ref = gfc_get_ref ();
5045 e->ref->type = REF_ARRAY;
5046 e->ref->u.ar.type = AR_FULL;
5047 e->ref->u.ar.dimen = 0;
5050 /* Like above, but for class types, where the checking whether an array
5051 ref is present is more complicated. Furthermore make sure not to add
5052 the full array ref to _vptr or _len refs. */
5053 if (sym->assoc && sym->ts.type == BT_CLASS
5054 && CLASS_DATA (sym)->attr.dimension
5055 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
5057 gfc_ref *ref, *newref;
5059 newref = gfc_get_ref ();
5060 newref->type = REF_ARRAY;
5061 newref->u.ar.type = AR_FULL;
5062 newref->u.ar.dimen = 0;
5063 /* Because this is an associate var and the first ref either is a ref to
5064 the _data component or not, no traversal of the ref chain is
5065 needed. The array ref needs to be inserted after the _data ref,
5066 or when that is not present, which may happend for polymorphic
5067 types, then at the first position. */
5068 ref = e->ref;
5069 if (!ref)
5070 e->ref = newref;
5071 else if (ref->type == REF_COMPONENT
5072 && strcmp ("_data", ref->u.c.component->name) == 0)
5074 if (!ref->next || ref->next->type != REF_ARRAY)
5076 newref->next = ref->next;
5077 ref->next = newref;
5079 else
5080 /* Array ref present already. */
5081 gfc_free_ref_list (newref);
5083 else if (ref->type == REF_ARRAY)
5084 /* Array ref present already. */
5085 gfc_free_ref_list (newref);
5086 else
5088 newref->next = ref;
5089 e->ref = newref;
5093 if (e->ref && !resolve_ref (e))
5094 return false;
5096 if (sym->attr.flavor == FL_PROCEDURE
5097 && (!sym->attr.function
5098 || (sym->attr.function && sym->result
5099 && sym->result->attr.proc_pointer
5100 && !sym->result->attr.function)))
5102 e->ts.type = BT_PROCEDURE;
5103 goto resolve_procedure;
5106 if (sym->ts.type != BT_UNKNOWN)
5107 gfc_variable_attr (e, &e->ts);
5108 else
5110 /* Must be a simple variable reference. */
5111 if (!gfc_set_default_type (sym, 1, sym->ns))
5112 return false;
5113 e->ts = sym->ts;
5116 if (check_assumed_size_reference (sym, e))
5117 return false;
5119 /* Deal with forward references to entries during gfc_resolve_code, to
5120 satisfy, at least partially, 12.5.2.5. */
5121 if (gfc_current_ns->entries
5122 && current_entry_id == sym->entry_id
5123 && cs_base
5124 && cs_base->current
5125 && cs_base->current->op != EXEC_ENTRY)
5127 gfc_entry_list *entry;
5128 gfc_formal_arglist *formal;
5129 int n;
5130 bool seen, saved_specification_expr;
5132 /* If the symbol is a dummy... */
5133 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5135 entry = gfc_current_ns->entries;
5136 seen = false;
5138 /* ...test if the symbol is a parameter of previous entries. */
5139 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5140 for (formal = entry->sym->formal; formal; formal = formal->next)
5142 if (formal->sym && sym->name == formal->sym->name)
5144 seen = true;
5145 break;
5149 /* If it has not been seen as a dummy, this is an error. */
5150 if (!seen)
5152 if (specification_expr)
5153 gfc_error ("Variable %qs, used in a specification expression"
5154 ", is referenced at %L before the ENTRY statement "
5155 "in which it is a parameter",
5156 sym->name, &cs_base->current->loc);
5157 else
5158 gfc_error ("Variable %qs is used at %L before the ENTRY "
5159 "statement in which it is a parameter",
5160 sym->name, &cs_base->current->loc);
5161 t = false;
5165 /* Now do the same check on the specification expressions. */
5166 saved_specification_expr = specification_expr;
5167 specification_expr = true;
5168 if (sym->ts.type == BT_CHARACTER
5169 && !gfc_resolve_expr (sym->ts.u.cl->length))
5170 t = false;
5172 if (sym->as)
5173 for (n = 0; n < sym->as->rank; n++)
5175 if (!gfc_resolve_expr (sym->as->lower[n]))
5176 t = false;
5177 if (!gfc_resolve_expr (sym->as->upper[n]))
5178 t = false;
5180 specification_expr = saved_specification_expr;
5182 if (t)
5183 /* Update the symbol's entry level. */
5184 sym->entry_id = current_entry_id + 1;
5187 /* If a symbol has been host_associated mark it. This is used latter,
5188 to identify if aliasing is possible via host association. */
5189 if (sym->attr.flavor == FL_VARIABLE
5190 && gfc_current_ns->parent
5191 && (gfc_current_ns->parent == sym->ns
5192 || (gfc_current_ns->parent->parent
5193 && gfc_current_ns->parent->parent == sym->ns)))
5194 sym->attr.host_assoc = 1;
5196 if (gfc_current_ns->proc_name
5197 && sym->attr.dimension
5198 && (sym->ns != gfc_current_ns
5199 || sym->attr.use_assoc
5200 || sym->attr.in_common))
5201 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
5203 resolve_procedure:
5204 if (t && !resolve_procedure_expression (e))
5205 t = false;
5207 /* F2008, C617 and C1229. */
5208 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5209 && gfc_is_coindexed (e))
5211 gfc_ref *ref, *ref2 = NULL;
5213 for (ref = e->ref; ref; ref = ref->next)
5215 if (ref->type == REF_COMPONENT)
5216 ref2 = ref;
5217 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5218 break;
5221 for ( ; ref; ref = ref->next)
5222 if (ref->type == REF_COMPONENT)
5223 break;
5225 /* Expression itself is not coindexed object. */
5226 if (ref && e->ts.type == BT_CLASS)
5228 gfc_error ("Polymorphic subobject of coindexed object at %L",
5229 &e->where);
5230 t = false;
5233 /* Expression itself is coindexed object. */
5234 if (ref == NULL)
5236 gfc_component *c;
5237 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5238 for ( ; c; c = c->next)
5239 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5241 gfc_error ("Coindexed object with polymorphic allocatable "
5242 "subcomponent at %L", &e->where);
5243 t = false;
5244 break;
5249 if (t)
5250 expression_rank (e);
5252 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
5253 add_caf_get_intrinsic (e);
5255 return t;
5259 /* Checks to see that the correct symbol has been host associated.
5260 The only situation where this arises is that in which a twice
5261 contained function is parsed after the host association is made.
5262 Therefore, on detecting this, change the symbol in the expression
5263 and convert the array reference into an actual arglist if the old
5264 symbol is a variable. */
5265 static bool
5266 check_host_association (gfc_expr *e)
5268 gfc_symbol *sym, *old_sym;
5269 gfc_symtree *st;
5270 int n;
5271 gfc_ref *ref;
5272 gfc_actual_arglist *arg, *tail = NULL;
5273 bool retval = e->expr_type == EXPR_FUNCTION;
5275 /* If the expression is the result of substitution in
5276 interface.c(gfc_extend_expr) because there is no way in
5277 which the host association can be wrong. */
5278 if (e->symtree == NULL
5279 || e->symtree->n.sym == NULL
5280 || e->user_operator)
5281 return retval;
5283 old_sym = e->symtree->n.sym;
5285 if (gfc_current_ns->parent
5286 && old_sym->ns != gfc_current_ns)
5288 /* Use the 'USE' name so that renamed module symbols are
5289 correctly handled. */
5290 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5292 if (sym && old_sym != sym
5293 && sym->ts.type == old_sym->ts.type
5294 && sym->attr.flavor == FL_PROCEDURE
5295 && sym->attr.contained)
5297 /* Clear the shape, since it might not be valid. */
5298 gfc_free_shape (&e->shape, e->rank);
5300 /* Give the expression the right symtree! */
5301 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5302 gcc_assert (st != NULL);
5304 if (old_sym->attr.flavor == FL_PROCEDURE
5305 || e->expr_type == EXPR_FUNCTION)
5307 /* Original was function so point to the new symbol, since
5308 the actual argument list is already attached to the
5309 expression. */
5310 e->value.function.esym = NULL;
5311 e->symtree = st;
5313 else
5315 /* Original was variable so convert array references into
5316 an actual arglist. This does not need any checking now
5317 since resolve_function will take care of it. */
5318 e->value.function.actual = NULL;
5319 e->expr_type = EXPR_FUNCTION;
5320 e->symtree = st;
5322 /* Ambiguity will not arise if the array reference is not
5323 the last reference. */
5324 for (ref = e->ref; ref; ref = ref->next)
5325 if (ref->type == REF_ARRAY && ref->next == NULL)
5326 break;
5328 gcc_assert (ref->type == REF_ARRAY);
5330 /* Grab the start expressions from the array ref and
5331 copy them into actual arguments. */
5332 for (n = 0; n < ref->u.ar.dimen; n++)
5334 arg = gfc_get_actual_arglist ();
5335 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5336 if (e->value.function.actual == NULL)
5337 tail = e->value.function.actual = arg;
5338 else
5340 tail->next = arg;
5341 tail = arg;
5345 /* Dump the reference list and set the rank. */
5346 gfc_free_ref_list (e->ref);
5347 e->ref = NULL;
5348 e->rank = sym->as ? sym->as->rank : 0;
5351 gfc_resolve_expr (e);
5352 sym->refs++;
5355 /* This might have changed! */
5356 return e->expr_type == EXPR_FUNCTION;
5360 static void
5361 gfc_resolve_character_operator (gfc_expr *e)
5363 gfc_expr *op1 = e->value.op.op1;
5364 gfc_expr *op2 = e->value.op.op2;
5365 gfc_expr *e1 = NULL;
5366 gfc_expr *e2 = NULL;
5368 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5370 if (op1->ts.u.cl && op1->ts.u.cl->length)
5371 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5372 else if (op1->expr_type == EXPR_CONSTANT)
5373 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5374 op1->value.character.length);
5376 if (op2->ts.u.cl && op2->ts.u.cl->length)
5377 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5378 else if (op2->expr_type == EXPR_CONSTANT)
5379 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5380 op2->value.character.length);
5382 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5384 if (!e1 || !e2)
5386 gfc_free_expr (e1);
5387 gfc_free_expr (e2);
5389 return;
5392 e->ts.u.cl->length = gfc_add (e1, e2);
5393 e->ts.u.cl->length->ts.type = BT_INTEGER;
5394 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5395 gfc_simplify_expr (e->ts.u.cl->length, 0);
5396 gfc_resolve_expr (e->ts.u.cl->length);
5398 return;
5402 /* Ensure that an character expression has a charlen and, if possible, a
5403 length expression. */
5405 static void
5406 fixup_charlen (gfc_expr *e)
5408 /* The cases fall through so that changes in expression type and the need
5409 for multiple fixes are picked up. In all circumstances, a charlen should
5410 be available for the middle end to hang a backend_decl on. */
5411 switch (e->expr_type)
5413 case EXPR_OP:
5414 gfc_resolve_character_operator (e);
5416 case EXPR_ARRAY:
5417 if (e->expr_type == EXPR_ARRAY)
5418 gfc_resolve_character_array_constructor (e);
5420 case EXPR_SUBSTRING:
5421 if (!e->ts.u.cl && e->ref)
5422 gfc_resolve_substring_charlen (e);
5424 default:
5425 if (!e->ts.u.cl)
5426 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5428 break;
5433 /* Update an actual argument to include the passed-object for type-bound
5434 procedures at the right position. */
5436 static gfc_actual_arglist*
5437 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5438 const char *name)
5440 gcc_assert (argpos > 0);
5442 if (argpos == 1)
5444 gfc_actual_arglist* result;
5446 result = gfc_get_actual_arglist ();
5447 result->expr = po;
5448 result->next = lst;
5449 if (name)
5450 result->name = name;
5452 return result;
5455 if (lst)
5456 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5457 else
5458 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5459 return lst;
5463 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5465 static gfc_expr*
5466 extract_compcall_passed_object (gfc_expr* e)
5468 gfc_expr* po;
5470 gcc_assert (e->expr_type == EXPR_COMPCALL);
5472 if (e->value.compcall.base_object)
5473 po = gfc_copy_expr (e->value.compcall.base_object);
5474 else
5476 po = gfc_get_expr ();
5477 po->expr_type = EXPR_VARIABLE;
5478 po->symtree = e->symtree;
5479 po->ref = gfc_copy_ref (e->ref);
5480 po->where = e->where;
5483 if (!gfc_resolve_expr (po))
5484 return NULL;
5486 return po;
5490 /* Update the arglist of an EXPR_COMPCALL expression to include the
5491 passed-object. */
5493 static bool
5494 update_compcall_arglist (gfc_expr* e)
5496 gfc_expr* po;
5497 gfc_typebound_proc* tbp;
5499 tbp = e->value.compcall.tbp;
5501 if (tbp->error)
5502 return false;
5504 po = extract_compcall_passed_object (e);
5505 if (!po)
5506 return false;
5508 if (tbp->nopass || e->value.compcall.ignore_pass)
5510 gfc_free_expr (po);
5511 return true;
5514 gcc_assert (tbp->pass_arg_num > 0);
5515 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5516 tbp->pass_arg_num,
5517 tbp->pass_arg);
5519 return true;
5523 /* Extract the passed object from a PPC call (a copy of it). */
5525 static gfc_expr*
5526 extract_ppc_passed_object (gfc_expr *e)
5528 gfc_expr *po;
5529 gfc_ref **ref;
5531 po = gfc_get_expr ();
5532 po->expr_type = EXPR_VARIABLE;
5533 po->symtree = e->symtree;
5534 po->ref = gfc_copy_ref (e->ref);
5535 po->where = e->where;
5537 /* Remove PPC reference. */
5538 ref = &po->ref;
5539 while ((*ref)->next)
5540 ref = &(*ref)->next;
5541 gfc_free_ref_list (*ref);
5542 *ref = NULL;
5544 if (!gfc_resolve_expr (po))
5545 return NULL;
5547 return po;
5551 /* Update the actual arglist of a procedure pointer component to include the
5552 passed-object. */
5554 static bool
5555 update_ppc_arglist (gfc_expr* e)
5557 gfc_expr* po;
5558 gfc_component *ppc;
5559 gfc_typebound_proc* tb;
5561 ppc = gfc_get_proc_ptr_comp (e);
5562 if (!ppc)
5563 return false;
5565 tb = ppc->tb;
5567 if (tb->error)
5568 return false;
5569 else if (tb->nopass)
5570 return true;
5572 po = extract_ppc_passed_object (e);
5573 if (!po)
5574 return false;
5576 /* F08:R739. */
5577 if (po->rank != 0)
5579 gfc_error ("Passed-object at %L must be scalar", &e->where);
5580 return false;
5583 /* F08:C611. */
5584 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5586 gfc_error ("Base object for procedure-pointer component call at %L is of"
5587 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
5588 return false;
5591 gcc_assert (tb->pass_arg_num > 0);
5592 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5593 tb->pass_arg_num,
5594 tb->pass_arg);
5596 return true;
5600 /* Check that the object a TBP is called on is valid, i.e. it must not be
5601 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5603 static bool
5604 check_typebound_baseobject (gfc_expr* e)
5606 gfc_expr* base;
5607 bool return_value = false;
5609 base = extract_compcall_passed_object (e);
5610 if (!base)
5611 return false;
5613 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5615 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
5616 return false;
5618 /* F08:C611. */
5619 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5621 gfc_error ("Base object for type-bound procedure call at %L is of"
5622 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
5623 goto cleanup;
5626 /* F08:C1230. If the procedure called is NOPASS,
5627 the base object must be scalar. */
5628 if (e->value.compcall.tbp->nopass && base->rank != 0)
5630 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5631 " be scalar", &e->where);
5632 goto cleanup;
5635 return_value = true;
5637 cleanup:
5638 gfc_free_expr (base);
5639 return return_value;
5643 /* Resolve a call to a type-bound procedure, either function or subroutine,
5644 statically from the data in an EXPR_COMPCALL expression. The adapted
5645 arglist and the target-procedure symtree are returned. */
5647 static bool
5648 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5649 gfc_actual_arglist** actual)
5651 gcc_assert (e->expr_type == EXPR_COMPCALL);
5652 gcc_assert (!e->value.compcall.tbp->is_generic);
5654 /* Update the actual arglist for PASS. */
5655 if (!update_compcall_arglist (e))
5656 return false;
5658 *actual = e->value.compcall.actual;
5659 *target = e->value.compcall.tbp->u.specific;
5661 gfc_free_ref_list (e->ref);
5662 e->ref = NULL;
5663 e->value.compcall.actual = NULL;
5665 /* If we find a deferred typebound procedure, check for derived types
5666 that an overriding typebound procedure has not been missed. */
5667 if (e->value.compcall.name
5668 && !e->value.compcall.tbp->non_overridable
5669 && e->value.compcall.base_object
5670 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5672 gfc_symtree *st;
5673 gfc_symbol *derived;
5675 /* Use the derived type of the base_object. */
5676 derived = e->value.compcall.base_object->ts.u.derived;
5677 st = NULL;
5679 /* If necessary, go through the inheritance chain. */
5680 while (!st && derived)
5682 /* Look for the typebound procedure 'name'. */
5683 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5684 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5685 e->value.compcall.name);
5686 if (!st)
5687 derived = gfc_get_derived_super_type (derived);
5690 /* Now find the specific name in the derived type namespace. */
5691 if (st && st->n.tb && st->n.tb->u.specific)
5692 gfc_find_sym_tree (st->n.tb->u.specific->name,
5693 derived->ns, 1, &st);
5694 if (st)
5695 *target = st;
5697 return true;
5701 /* Get the ultimate declared type from an expression. In addition,
5702 return the last class/derived type reference and the copy of the
5703 reference list. If check_types is set true, derived types are
5704 identified as well as class references. */
5705 static gfc_symbol*
5706 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5707 gfc_expr *e, bool check_types)
5709 gfc_symbol *declared;
5710 gfc_ref *ref;
5712 declared = NULL;
5713 if (class_ref)
5714 *class_ref = NULL;
5715 if (new_ref)
5716 *new_ref = gfc_copy_ref (e->ref);
5718 for (ref = e->ref; ref; ref = ref->next)
5720 if (ref->type != REF_COMPONENT)
5721 continue;
5723 if ((ref->u.c.component->ts.type == BT_CLASS
5724 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
5725 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5727 declared = ref->u.c.component->ts.u.derived;
5728 if (class_ref)
5729 *class_ref = ref;
5733 if (declared == NULL)
5734 declared = e->symtree->n.sym->ts.u.derived;
5736 return declared;
5740 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5741 which of the specific bindings (if any) matches the arglist and transform
5742 the expression into a call of that binding. */
5744 static bool
5745 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5747 gfc_typebound_proc* genproc;
5748 const char* genname;
5749 gfc_symtree *st;
5750 gfc_symbol *derived;
5752 gcc_assert (e->expr_type == EXPR_COMPCALL);
5753 genname = e->value.compcall.name;
5754 genproc = e->value.compcall.tbp;
5756 if (!genproc->is_generic)
5757 return true;
5759 /* Try the bindings on this type and in the inheritance hierarchy. */
5760 for (; genproc; genproc = genproc->overridden)
5762 gfc_tbp_generic* g;
5764 gcc_assert (genproc->is_generic);
5765 for (g = genproc->u.generic; g; g = g->next)
5767 gfc_symbol* target;
5768 gfc_actual_arglist* args;
5769 bool matches;
5771 gcc_assert (g->specific);
5773 if (g->specific->error)
5774 continue;
5776 target = g->specific->u.specific->n.sym;
5778 /* Get the right arglist by handling PASS/NOPASS. */
5779 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5780 if (!g->specific->nopass)
5782 gfc_expr* po;
5783 po = extract_compcall_passed_object (e);
5784 if (!po)
5786 gfc_free_actual_arglist (args);
5787 return false;
5790 gcc_assert (g->specific->pass_arg_num > 0);
5791 gcc_assert (!g->specific->error);
5792 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5793 g->specific->pass_arg);
5795 resolve_actual_arglist (args, target->attr.proc,
5796 is_external_proc (target)
5797 && gfc_sym_get_dummy_args (target) == NULL);
5799 /* Check if this arglist matches the formal. */
5800 matches = gfc_arglist_matches_symbol (&args, target);
5802 /* Clean up and break out of the loop if we've found it. */
5803 gfc_free_actual_arglist (args);
5804 if (matches)
5806 e->value.compcall.tbp = g->specific;
5807 genname = g->specific_st->name;
5808 /* Pass along the name for CLASS methods, where the vtab
5809 procedure pointer component has to be referenced. */
5810 if (name)
5811 *name = genname;
5812 goto success;
5817 /* Nothing matching found! */
5818 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5819 " %qs at %L", genname, &e->where);
5820 return false;
5822 success:
5823 /* Make sure that we have the right specific instance for the name. */
5824 derived = get_declared_from_expr (NULL, NULL, e, true);
5826 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5827 if (st)
5828 e->value.compcall.tbp = st->n.tb;
5830 return true;
5834 /* Resolve a call to a type-bound subroutine. */
5836 static bool
5837 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
5839 gfc_actual_arglist* newactual;
5840 gfc_symtree* target;
5842 /* Check that's really a SUBROUTINE. */
5843 if (!c->expr1->value.compcall.tbp->subroutine)
5845 gfc_error ("%qs at %L should be a SUBROUTINE",
5846 c->expr1->value.compcall.name, &c->loc);
5847 return false;
5850 if (!check_typebound_baseobject (c->expr1))
5851 return false;
5853 /* Pass along the name for CLASS methods, where the vtab
5854 procedure pointer component has to be referenced. */
5855 if (name)
5856 *name = c->expr1->value.compcall.name;
5858 if (!resolve_typebound_generic_call (c->expr1, name))
5859 return false;
5861 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
5862 if (overridable)
5863 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
5865 /* Transform into an ordinary EXEC_CALL for now. */
5867 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5868 return false;
5870 c->ext.actual = newactual;
5871 c->symtree = target;
5872 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5874 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5876 gfc_free_expr (c->expr1);
5877 c->expr1 = gfc_get_expr ();
5878 c->expr1->expr_type = EXPR_FUNCTION;
5879 c->expr1->symtree = target;
5880 c->expr1->where = c->loc;
5882 return resolve_call (c);
5886 /* Resolve a component-call expression. */
5887 static bool
5888 resolve_compcall (gfc_expr* e, const char **name)
5890 gfc_actual_arglist* newactual;
5891 gfc_symtree* target;
5893 /* Check that's really a FUNCTION. */
5894 if (!e->value.compcall.tbp->function)
5896 gfc_error ("%qs at %L should be a FUNCTION",
5897 e->value.compcall.name, &e->where);
5898 return false;
5901 /* These must not be assign-calls! */
5902 gcc_assert (!e->value.compcall.assign);
5904 if (!check_typebound_baseobject (e))
5905 return false;
5907 /* Pass along the name for CLASS methods, where the vtab
5908 procedure pointer component has to be referenced. */
5909 if (name)
5910 *name = e->value.compcall.name;
5912 if (!resolve_typebound_generic_call (e, name))
5913 return false;
5914 gcc_assert (!e->value.compcall.tbp->is_generic);
5916 /* Take the rank from the function's symbol. */
5917 if (e->value.compcall.tbp->u.specific->n.sym->as)
5918 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5920 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5921 arglist to the TBP's binding target. */
5923 if (!resolve_typebound_static (e, &target, &newactual))
5924 return false;
5926 e->value.function.actual = newactual;
5927 e->value.function.name = NULL;
5928 e->value.function.esym = target->n.sym;
5929 e->value.function.isym = NULL;
5930 e->symtree = target;
5931 e->ts = target->n.sym->ts;
5932 e->expr_type = EXPR_FUNCTION;
5934 /* Resolution is not necessary if this is a class subroutine; this
5935 function only has to identify the specific proc. Resolution of
5936 the call will be done next in resolve_typebound_call. */
5937 return gfc_resolve_expr (e);
5941 static bool resolve_fl_derived (gfc_symbol *sym);
5944 /* Resolve a typebound function, or 'method'. First separate all
5945 the non-CLASS references by calling resolve_compcall directly. */
5947 static bool
5948 resolve_typebound_function (gfc_expr* e)
5950 gfc_symbol *declared;
5951 gfc_component *c;
5952 gfc_ref *new_ref;
5953 gfc_ref *class_ref;
5954 gfc_symtree *st;
5955 const char *name;
5956 gfc_typespec ts;
5957 gfc_expr *expr;
5958 bool overridable;
5960 st = e->symtree;
5962 /* Deal with typebound operators for CLASS objects. */
5963 expr = e->value.compcall.base_object;
5964 overridable = !e->value.compcall.tbp->non_overridable;
5965 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5967 /* If the base_object is not a variable, the corresponding actual
5968 argument expression must be stored in e->base_expression so
5969 that the corresponding tree temporary can be used as the base
5970 object in gfc_conv_procedure_call. */
5971 if (expr->expr_type != EXPR_VARIABLE)
5973 gfc_actual_arglist *args;
5975 for (args= e->value.function.actual; args; args = args->next)
5977 if (expr == args->expr)
5978 expr = args->expr;
5982 /* Since the typebound operators are generic, we have to ensure
5983 that any delays in resolution are corrected and that the vtab
5984 is present. */
5985 ts = expr->ts;
5986 declared = ts.u.derived;
5987 c = gfc_find_component (declared, "_vptr", true, true, NULL);
5988 if (c->ts.u.derived == NULL)
5989 c->ts.u.derived = gfc_find_derived_vtab (declared);
5991 if (!resolve_compcall (e, &name))
5992 return false;
5994 /* Use the generic name if it is there. */
5995 name = name ? name : e->value.function.esym->name;
5996 e->symtree = expr->symtree;
5997 e->ref = gfc_copy_ref (expr->ref);
5998 get_declared_from_expr (&class_ref, NULL, e, false);
6000 /* Trim away the extraneous references that emerge from nested
6001 use of interface.c (extend_expr). */
6002 if (class_ref && class_ref->next)
6004 gfc_free_ref_list (class_ref->next);
6005 class_ref->next = NULL;
6007 else if (e->ref && !class_ref)
6009 gfc_free_ref_list (e->ref);
6010 e->ref = NULL;
6013 gfc_add_vptr_component (e);
6014 gfc_add_component_ref (e, name);
6015 e->value.function.esym = NULL;
6016 if (expr->expr_type != EXPR_VARIABLE)
6017 e->base_expr = expr;
6018 return true;
6021 if (st == NULL)
6022 return resolve_compcall (e, NULL);
6024 if (!resolve_ref (e))
6025 return false;
6027 /* Get the CLASS declared type. */
6028 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6030 if (!resolve_fl_derived (declared))
6031 return false;
6033 /* Weed out cases of the ultimate component being a derived type. */
6034 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6035 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6037 gfc_free_ref_list (new_ref);
6038 return resolve_compcall (e, NULL);
6041 c = gfc_find_component (declared, "_data", true, true, NULL);
6042 declared = c->ts.u.derived;
6044 /* Treat the call as if it is a typebound procedure, in order to roll
6045 out the correct name for the specific function. */
6046 if (!resolve_compcall (e, &name))
6048 gfc_free_ref_list (new_ref);
6049 return false;
6051 ts = e->ts;
6053 if (overridable)
6055 /* Convert the expression to a procedure pointer component call. */
6056 e->value.function.esym = NULL;
6057 e->symtree = st;
6059 if (new_ref)
6060 e->ref = new_ref;
6062 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6063 gfc_add_vptr_component (e);
6064 gfc_add_component_ref (e, name);
6066 /* Recover the typespec for the expression. This is really only
6067 necessary for generic procedures, where the additional call
6068 to gfc_add_component_ref seems to throw the collection of the
6069 correct typespec. */
6070 e->ts = ts;
6072 else if (new_ref)
6073 gfc_free_ref_list (new_ref);
6075 return true;
6078 /* Resolve a typebound subroutine, or 'method'. First separate all
6079 the non-CLASS references by calling resolve_typebound_call
6080 directly. */
6082 static bool
6083 resolve_typebound_subroutine (gfc_code *code)
6085 gfc_symbol *declared;
6086 gfc_component *c;
6087 gfc_ref *new_ref;
6088 gfc_ref *class_ref;
6089 gfc_symtree *st;
6090 const char *name;
6091 gfc_typespec ts;
6092 gfc_expr *expr;
6093 bool overridable;
6095 st = code->expr1->symtree;
6097 /* Deal with typebound operators for CLASS objects. */
6098 expr = code->expr1->value.compcall.base_object;
6099 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6100 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6102 /* If the base_object is not a variable, the corresponding actual
6103 argument expression must be stored in e->base_expression so
6104 that the corresponding tree temporary can be used as the base
6105 object in gfc_conv_procedure_call. */
6106 if (expr->expr_type != EXPR_VARIABLE)
6108 gfc_actual_arglist *args;
6110 args= code->expr1->value.function.actual;
6111 for (; args; args = args->next)
6112 if (expr == args->expr)
6113 expr = args->expr;
6116 /* Since the typebound operators are generic, we have to ensure
6117 that any delays in resolution are corrected and that the vtab
6118 is present. */
6119 declared = expr->ts.u.derived;
6120 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6121 if (c->ts.u.derived == NULL)
6122 c->ts.u.derived = gfc_find_derived_vtab (declared);
6124 if (!resolve_typebound_call (code, &name, NULL))
6125 return false;
6127 /* Use the generic name if it is there. */
6128 name = name ? name : code->expr1->value.function.esym->name;
6129 code->expr1->symtree = expr->symtree;
6130 code->expr1->ref = gfc_copy_ref (expr->ref);
6132 /* Trim away the extraneous references that emerge from nested
6133 use of interface.c (extend_expr). */
6134 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6135 if (class_ref && class_ref->next)
6137 gfc_free_ref_list (class_ref->next);
6138 class_ref->next = NULL;
6140 else if (code->expr1->ref && !class_ref)
6142 gfc_free_ref_list (code->expr1->ref);
6143 code->expr1->ref = NULL;
6146 /* Now use the procedure in the vtable. */
6147 gfc_add_vptr_component (code->expr1);
6148 gfc_add_component_ref (code->expr1, name);
6149 code->expr1->value.function.esym = NULL;
6150 if (expr->expr_type != EXPR_VARIABLE)
6151 code->expr1->base_expr = expr;
6152 return true;
6155 if (st == NULL)
6156 return resolve_typebound_call (code, NULL, NULL);
6158 if (!resolve_ref (code->expr1))
6159 return false;
6161 /* Get the CLASS declared type. */
6162 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
6164 /* Weed out cases of the ultimate component being a derived type. */
6165 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6166 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6168 gfc_free_ref_list (new_ref);
6169 return resolve_typebound_call (code, NULL, NULL);
6172 if (!resolve_typebound_call (code, &name, &overridable))
6174 gfc_free_ref_list (new_ref);
6175 return false;
6177 ts = code->expr1->ts;
6179 if (overridable)
6181 /* Convert the expression to a procedure pointer component call. */
6182 code->expr1->value.function.esym = NULL;
6183 code->expr1->symtree = st;
6185 if (new_ref)
6186 code->expr1->ref = new_ref;
6188 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6189 gfc_add_vptr_component (code->expr1);
6190 gfc_add_component_ref (code->expr1, name);
6192 /* Recover the typespec for the expression. This is really only
6193 necessary for generic procedures, where the additional call
6194 to gfc_add_component_ref seems to throw the collection of the
6195 correct typespec. */
6196 code->expr1->ts = ts;
6198 else if (new_ref)
6199 gfc_free_ref_list (new_ref);
6201 return true;
6205 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6207 static bool
6208 resolve_ppc_call (gfc_code* c)
6210 gfc_component *comp;
6212 comp = gfc_get_proc_ptr_comp (c->expr1);
6213 gcc_assert (comp != NULL);
6215 c->resolved_sym = c->expr1->symtree->n.sym;
6216 c->expr1->expr_type = EXPR_VARIABLE;
6218 if (!comp->attr.subroutine)
6219 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6221 if (!resolve_ref (c->expr1))
6222 return false;
6224 if (!update_ppc_arglist (c->expr1))
6225 return false;
6227 c->ext.actual = c->expr1->value.compcall.actual;
6229 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6230 !(comp->ts.interface
6231 && comp->ts.interface->formal)))
6232 return false;
6234 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
6235 return false;
6237 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6239 return true;
6243 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6245 static bool
6246 resolve_expr_ppc (gfc_expr* e)
6248 gfc_component *comp;
6250 comp = gfc_get_proc_ptr_comp (e);
6251 gcc_assert (comp != NULL);
6253 /* Convert to EXPR_FUNCTION. */
6254 e->expr_type = EXPR_FUNCTION;
6255 e->value.function.isym = NULL;
6256 e->value.function.actual = e->value.compcall.actual;
6257 e->ts = comp->ts;
6258 if (comp->as != NULL)
6259 e->rank = comp->as->rank;
6261 if (!comp->attr.function)
6262 gfc_add_function (&comp->attr, comp->name, &e->where);
6264 if (!resolve_ref (e))
6265 return false;
6267 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6268 !(comp->ts.interface
6269 && comp->ts.interface->formal)))
6270 return false;
6272 if (!update_ppc_arglist (e))
6273 return false;
6275 if (!check_pure_function(e))
6276 return false;
6278 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6280 return true;
6284 static bool
6285 gfc_is_expandable_expr (gfc_expr *e)
6287 gfc_constructor *con;
6289 if (e->expr_type == EXPR_ARRAY)
6291 /* Traverse the constructor looking for variables that are flavor
6292 parameter. Parameters must be expanded since they are fully used at
6293 compile time. */
6294 con = gfc_constructor_first (e->value.constructor);
6295 for (; con; con = gfc_constructor_next (con))
6297 if (con->expr->expr_type == EXPR_VARIABLE
6298 && con->expr->symtree
6299 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6300 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6301 return true;
6302 if (con->expr->expr_type == EXPR_ARRAY
6303 && gfc_is_expandable_expr (con->expr))
6304 return true;
6308 return false;
6311 /* Resolve an expression. That is, make sure that types of operands agree
6312 with their operators, intrinsic operators are converted to function calls
6313 for overloaded types and unresolved function references are resolved. */
6315 bool
6316 gfc_resolve_expr (gfc_expr *e)
6318 bool t;
6319 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6321 if (e == NULL)
6322 return true;
6324 /* inquiry_argument only applies to variables. */
6325 inquiry_save = inquiry_argument;
6326 actual_arg_save = actual_arg;
6327 first_actual_arg_save = first_actual_arg;
6329 if (e->expr_type != EXPR_VARIABLE)
6331 inquiry_argument = false;
6332 actual_arg = false;
6333 first_actual_arg = false;
6336 switch (e->expr_type)
6338 case EXPR_OP:
6339 t = resolve_operator (e);
6340 break;
6342 case EXPR_FUNCTION:
6343 case EXPR_VARIABLE:
6345 if (check_host_association (e))
6346 t = resolve_function (e);
6347 else
6348 t = resolve_variable (e);
6350 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6351 && e->ref->type != REF_SUBSTRING)
6352 gfc_resolve_substring_charlen (e);
6354 break;
6356 case EXPR_COMPCALL:
6357 t = resolve_typebound_function (e);
6358 break;
6360 case EXPR_SUBSTRING:
6361 t = resolve_ref (e);
6362 break;
6364 case EXPR_CONSTANT:
6365 case EXPR_NULL:
6366 t = true;
6367 break;
6369 case EXPR_PPC:
6370 t = resolve_expr_ppc (e);
6371 break;
6373 case EXPR_ARRAY:
6374 t = false;
6375 if (!resolve_ref (e))
6376 break;
6378 t = gfc_resolve_array_constructor (e);
6379 /* Also try to expand a constructor. */
6380 if (t)
6382 expression_rank (e);
6383 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6384 gfc_expand_constructor (e, false);
6387 /* This provides the opportunity for the length of constructors with
6388 character valued function elements to propagate the string length
6389 to the expression. */
6390 if (t && e->ts.type == BT_CHARACTER)
6392 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6393 here rather then add a duplicate test for it above. */
6394 gfc_expand_constructor (e, false);
6395 t = gfc_resolve_character_array_constructor (e);
6398 break;
6400 case EXPR_STRUCTURE:
6401 t = resolve_ref (e);
6402 if (!t)
6403 break;
6405 t = resolve_structure_cons (e, 0);
6406 if (!t)
6407 break;
6409 t = gfc_simplify_expr (e, 0);
6410 break;
6412 default:
6413 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6416 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
6417 fixup_charlen (e);
6419 inquiry_argument = inquiry_save;
6420 actual_arg = actual_arg_save;
6421 first_actual_arg = first_actual_arg_save;
6423 return t;
6427 /* Resolve an expression from an iterator. They must be scalar and have
6428 INTEGER or (optionally) REAL type. */
6430 static bool
6431 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6432 const char *name_msgid)
6434 if (!gfc_resolve_expr (expr))
6435 return false;
6437 if (expr->rank != 0)
6439 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6440 return false;
6443 if (expr->ts.type != BT_INTEGER)
6445 if (expr->ts.type == BT_REAL)
6447 if (real_ok)
6448 return gfc_notify_std (GFC_STD_F95_DEL,
6449 "%s at %L must be integer",
6450 _(name_msgid), &expr->where);
6451 else
6453 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6454 &expr->where);
6455 return false;
6458 else
6460 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6461 return false;
6464 return true;
6468 /* Resolve the expressions in an iterator structure. If REAL_OK is
6469 false allow only INTEGER type iterators, otherwise allow REAL types.
6470 Set own_scope to true for ac-implied-do and data-implied-do as those
6471 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
6473 bool
6474 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
6476 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6477 return false;
6479 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6480 _("iterator variable")))
6481 return false;
6483 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6484 "Start expression in DO loop"))
6485 return false;
6487 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6488 "End expression in DO loop"))
6489 return false;
6491 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6492 "Step expression in DO loop"))
6493 return false;
6495 if (iter->step->expr_type == EXPR_CONSTANT)
6497 if ((iter->step->ts.type == BT_INTEGER
6498 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6499 || (iter->step->ts.type == BT_REAL
6500 && mpfr_sgn (iter->step->value.real) == 0))
6502 gfc_error ("Step expression in DO loop at %L cannot be zero",
6503 &iter->step->where);
6504 return false;
6508 /* Convert start, end, and step to the same type as var. */
6509 if (iter->start->ts.kind != iter->var->ts.kind
6510 || iter->start->ts.type != iter->var->ts.type)
6511 gfc_convert_type (iter->start, &iter->var->ts, 2);
6513 if (iter->end->ts.kind != iter->var->ts.kind
6514 || iter->end->ts.type != iter->var->ts.type)
6515 gfc_convert_type (iter->end, &iter->var->ts, 2);
6517 if (iter->step->ts.kind != iter->var->ts.kind
6518 || iter->step->ts.type != iter->var->ts.type)
6519 gfc_convert_type (iter->step, &iter->var->ts, 2);
6521 if (iter->start->expr_type == EXPR_CONSTANT
6522 && iter->end->expr_type == EXPR_CONSTANT
6523 && iter->step->expr_type == EXPR_CONSTANT)
6525 int sgn, cmp;
6526 if (iter->start->ts.type == BT_INTEGER)
6528 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6529 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6531 else
6533 sgn = mpfr_sgn (iter->step->value.real);
6534 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6536 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
6537 gfc_warning (OPT_Wzerotrip,
6538 "DO loop at %L will be executed zero times",
6539 &iter->step->where);
6542 return true;
6546 /* Traversal function for find_forall_index. f == 2 signals that
6547 that variable itself is not to be checked - only the references. */
6549 static bool
6550 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6552 if (expr->expr_type != EXPR_VARIABLE)
6553 return false;
6555 /* A scalar assignment */
6556 if (!expr->ref || *f == 1)
6558 if (expr->symtree->n.sym == sym)
6559 return true;
6560 else
6561 return false;
6564 if (*f == 2)
6565 *f = 1;
6566 return false;
6570 /* Check whether the FORALL index appears in the expression or not.
6571 Returns true if SYM is found in EXPR. */
6573 bool
6574 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6576 if (gfc_traverse_expr (expr, sym, forall_index, f))
6577 return true;
6578 else
6579 return false;
6583 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6584 to be a scalar INTEGER variable. The subscripts and stride are scalar
6585 INTEGERs, and if stride is a constant it must be nonzero.
6586 Furthermore "A subscript or stride in a forall-triplet-spec shall
6587 not contain a reference to any index-name in the
6588 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6590 static void
6591 resolve_forall_iterators (gfc_forall_iterator *it)
6593 gfc_forall_iterator *iter, *iter2;
6595 for (iter = it; iter; iter = iter->next)
6597 if (gfc_resolve_expr (iter->var)
6598 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6599 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6600 &iter->var->where);
6602 if (gfc_resolve_expr (iter->start)
6603 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6604 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6605 &iter->start->where);
6606 if (iter->var->ts.kind != iter->start->ts.kind)
6607 gfc_convert_type (iter->start, &iter->var->ts, 1);
6609 if (gfc_resolve_expr (iter->end)
6610 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6611 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6612 &iter->end->where);
6613 if (iter->var->ts.kind != iter->end->ts.kind)
6614 gfc_convert_type (iter->end, &iter->var->ts, 1);
6616 if (gfc_resolve_expr (iter->stride))
6618 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6619 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6620 &iter->stride->where, "INTEGER");
6622 if (iter->stride->expr_type == EXPR_CONSTANT
6623 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
6624 gfc_error ("FORALL stride expression at %L cannot be zero",
6625 &iter->stride->where);
6627 if (iter->var->ts.kind != iter->stride->ts.kind)
6628 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6631 for (iter = it; iter; iter = iter->next)
6632 for (iter2 = iter; iter2; iter2 = iter2->next)
6634 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6635 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6636 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
6637 gfc_error ("FORALL index %qs may not appear in triplet "
6638 "specification at %L", iter->var->symtree->name,
6639 &iter2->start->where);
6644 /* Given a pointer to a symbol that is a derived type, see if it's
6645 inaccessible, i.e. if it's defined in another module and the components are
6646 PRIVATE. The search is recursive if necessary. Returns zero if no
6647 inaccessible components are found, nonzero otherwise. */
6649 static int
6650 derived_inaccessible (gfc_symbol *sym)
6652 gfc_component *c;
6654 if (sym->attr.use_assoc && sym->attr.private_comp)
6655 return 1;
6657 for (c = sym->components; c; c = c->next)
6659 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6660 return 1;
6663 return 0;
6667 /* Resolve the argument of a deallocate expression. The expression must be
6668 a pointer or a full array. */
6670 static bool
6671 resolve_deallocate_expr (gfc_expr *e)
6673 symbol_attribute attr;
6674 int allocatable, pointer;
6675 gfc_ref *ref;
6676 gfc_symbol *sym;
6677 gfc_component *c;
6678 bool unlimited;
6680 if (!gfc_resolve_expr (e))
6681 return false;
6683 if (e->expr_type != EXPR_VARIABLE)
6684 goto bad;
6686 sym = e->symtree->n.sym;
6687 unlimited = UNLIMITED_POLY(sym);
6689 if (sym->ts.type == BT_CLASS)
6691 allocatable = CLASS_DATA (sym)->attr.allocatable;
6692 pointer = CLASS_DATA (sym)->attr.class_pointer;
6694 else
6696 allocatable = sym->attr.allocatable;
6697 pointer = sym->attr.pointer;
6699 for (ref = e->ref; ref; ref = ref->next)
6701 switch (ref->type)
6703 case REF_ARRAY:
6704 if (ref->u.ar.type != AR_FULL
6705 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6706 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6707 allocatable = 0;
6708 break;
6710 case REF_COMPONENT:
6711 c = ref->u.c.component;
6712 if (c->ts.type == BT_CLASS)
6714 allocatable = CLASS_DATA (c)->attr.allocatable;
6715 pointer = CLASS_DATA (c)->attr.class_pointer;
6717 else
6719 allocatable = c->attr.allocatable;
6720 pointer = c->attr.pointer;
6722 break;
6724 case REF_SUBSTRING:
6725 allocatable = 0;
6726 break;
6730 attr = gfc_expr_attr (e);
6732 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
6734 bad:
6735 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6736 &e->where);
6737 return false;
6740 /* F2008, C644. */
6741 if (gfc_is_coindexed (e))
6743 gfc_error ("Coindexed allocatable object at %L", &e->where);
6744 return false;
6747 if (pointer
6748 && !gfc_check_vardef_context (e, true, true, false,
6749 _("DEALLOCATE object")))
6750 return false;
6751 if (!gfc_check_vardef_context (e, false, true, false,
6752 _("DEALLOCATE object")))
6753 return false;
6755 return true;
6759 /* Returns true if the expression e contains a reference to the symbol sym. */
6760 static bool
6761 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6763 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6764 return true;
6766 return false;
6769 bool
6770 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6772 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6776 /* Given the expression node e for an allocatable/pointer of derived type to be
6777 allocated, get the expression node to be initialized afterwards (needed for
6778 derived types with default initializers, and derived types with allocatable
6779 components that need nullification.) */
6781 gfc_expr *
6782 gfc_expr_to_initialize (gfc_expr *e)
6784 gfc_expr *result;
6785 gfc_ref *ref;
6786 int i;
6788 result = gfc_copy_expr (e);
6790 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6791 for (ref = result->ref; ref; ref = ref->next)
6792 if (ref->type == REF_ARRAY && ref->next == NULL)
6794 ref->u.ar.type = AR_FULL;
6796 for (i = 0; i < ref->u.ar.dimen; i++)
6797 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6799 break;
6802 gfc_free_shape (&result->shape, result->rank);
6804 /* Recalculate rank, shape, etc. */
6805 gfc_resolve_expr (result);
6806 return result;
6810 /* If the last ref of an expression is an array ref, return a copy of the
6811 expression with that one removed. Otherwise, a copy of the original
6812 expression. This is used for allocate-expressions and pointer assignment
6813 LHS, where there may be an array specification that needs to be stripped
6814 off when using gfc_check_vardef_context. */
6816 static gfc_expr*
6817 remove_last_array_ref (gfc_expr* e)
6819 gfc_expr* e2;
6820 gfc_ref** r;
6822 e2 = gfc_copy_expr (e);
6823 for (r = &e2->ref; *r; r = &(*r)->next)
6824 if ((*r)->type == REF_ARRAY && !(*r)->next)
6826 gfc_free_ref_list (*r);
6827 *r = NULL;
6828 break;
6831 return e2;
6835 /* Used in resolve_allocate_expr to check that a allocation-object and
6836 a source-expr are conformable. This does not catch all possible
6837 cases; in particular a runtime checking is needed. */
6839 static bool
6840 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6842 gfc_ref *tail;
6843 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6845 /* First compare rank. */
6846 if ((tail && e1->rank != tail->u.ar.as->rank)
6847 || (!tail && e1->rank != e2->rank))
6849 gfc_error ("Source-expr at %L must be scalar or have the "
6850 "same rank as the allocate-object at %L",
6851 &e1->where, &e2->where);
6852 return false;
6855 if (e1->shape)
6857 int i;
6858 mpz_t s;
6860 mpz_init (s);
6862 for (i = 0; i < e1->rank; i++)
6864 if (tail->u.ar.start[i] == NULL)
6865 break;
6867 if (tail->u.ar.end[i])
6869 mpz_set (s, tail->u.ar.end[i]->value.integer);
6870 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6871 mpz_add_ui (s, s, 1);
6873 else
6875 mpz_set (s, tail->u.ar.start[i]->value.integer);
6878 if (mpz_cmp (e1->shape[i], s) != 0)
6880 gfc_error ("Source-expr at %L and allocate-object at %L must "
6881 "have the same shape", &e1->where, &e2->where);
6882 mpz_clear (s);
6883 return false;
6887 mpz_clear (s);
6890 return true;
6894 /* Resolve the expression in an ALLOCATE statement, doing the additional
6895 checks to see whether the expression is OK or not. The expression must
6896 have a trailing array reference that gives the size of the array. */
6898 static bool
6899 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
6901 int i, pointer, allocatable, dimension, is_abstract;
6902 int codimension;
6903 bool coindexed;
6904 bool unlimited;
6905 symbol_attribute attr;
6906 gfc_ref *ref, *ref2;
6907 gfc_expr *e2;
6908 gfc_array_ref *ar;
6909 gfc_symbol *sym = NULL;
6910 gfc_alloc *a;
6911 gfc_component *c;
6912 bool t;
6914 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
6915 checking of coarrays. */
6916 for (ref = e->ref; ref; ref = ref->next)
6917 if (ref->next == NULL)
6918 break;
6920 if (ref && ref->type == REF_ARRAY)
6921 ref->u.ar.in_allocate = true;
6923 if (!gfc_resolve_expr (e))
6924 goto failure;
6926 /* Make sure the expression is allocatable or a pointer. If it is
6927 pointer, the next-to-last reference must be a pointer. */
6929 ref2 = NULL;
6930 if (e->symtree)
6931 sym = e->symtree->n.sym;
6933 /* Check whether ultimate component is abstract and CLASS. */
6934 is_abstract = 0;
6936 /* Is the allocate-object unlimited polymorphic? */
6937 unlimited = UNLIMITED_POLY(e);
6939 if (e->expr_type != EXPR_VARIABLE)
6941 allocatable = 0;
6942 attr = gfc_expr_attr (e);
6943 pointer = attr.pointer;
6944 dimension = attr.dimension;
6945 codimension = attr.codimension;
6947 else
6949 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
6951 allocatable = CLASS_DATA (sym)->attr.allocatable;
6952 pointer = CLASS_DATA (sym)->attr.class_pointer;
6953 dimension = CLASS_DATA (sym)->attr.dimension;
6954 codimension = CLASS_DATA (sym)->attr.codimension;
6955 is_abstract = CLASS_DATA (sym)->attr.abstract;
6957 else
6959 allocatable = sym->attr.allocatable;
6960 pointer = sym->attr.pointer;
6961 dimension = sym->attr.dimension;
6962 codimension = sym->attr.codimension;
6965 coindexed = false;
6967 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6969 switch (ref->type)
6971 case REF_ARRAY:
6972 if (ref->u.ar.codimen > 0)
6974 int n;
6975 for (n = ref->u.ar.dimen;
6976 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6977 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6979 coindexed = true;
6980 break;
6984 if (ref->next != NULL)
6985 pointer = 0;
6986 break;
6988 case REF_COMPONENT:
6989 /* F2008, C644. */
6990 if (coindexed)
6992 gfc_error ("Coindexed allocatable object at %L",
6993 &e->where);
6994 goto failure;
6997 c = ref->u.c.component;
6998 if (c->ts.type == BT_CLASS)
7000 allocatable = CLASS_DATA (c)->attr.allocatable;
7001 pointer = CLASS_DATA (c)->attr.class_pointer;
7002 dimension = CLASS_DATA (c)->attr.dimension;
7003 codimension = CLASS_DATA (c)->attr.codimension;
7004 is_abstract = CLASS_DATA (c)->attr.abstract;
7006 else
7008 allocatable = c->attr.allocatable;
7009 pointer = c->attr.pointer;
7010 dimension = c->attr.dimension;
7011 codimension = c->attr.codimension;
7012 is_abstract = c->attr.abstract;
7014 break;
7016 case REF_SUBSTRING:
7017 allocatable = 0;
7018 pointer = 0;
7019 break;
7024 /* Check for F08:C628. */
7025 if (allocatable == 0 && pointer == 0 && !unlimited)
7027 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7028 &e->where);
7029 goto failure;
7032 /* Some checks for the SOURCE tag. */
7033 if (code->expr3)
7035 /* Check F03:C631. */
7036 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7038 gfc_error ("Type of entity at %L is type incompatible with "
7039 "source-expr at %L", &e->where, &code->expr3->where);
7040 goto failure;
7043 /* Check F03:C632 and restriction following Note 6.18. */
7044 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
7045 goto failure;
7047 /* Check F03:C633. */
7048 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
7050 gfc_error ("The allocate-object at %L and the source-expr at %L "
7051 "shall have the same kind type parameter",
7052 &e->where, &code->expr3->where);
7053 goto failure;
7056 /* Check F2008, C642. */
7057 if (code->expr3->ts.type == BT_DERIVED
7058 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7059 || (code->expr3->ts.u.derived->from_intmod
7060 == INTMOD_ISO_FORTRAN_ENV
7061 && code->expr3->ts.u.derived->intmod_sym_id
7062 == ISOFORTRAN_LOCK_TYPE)))
7064 gfc_error ("The source-expr at %L shall neither be of type "
7065 "LOCK_TYPE nor have a LOCK_TYPE component if "
7066 "allocate-object at %L is a coarray",
7067 &code->expr3->where, &e->where);
7068 goto failure;
7071 /* Check TS18508, C702/C703. */
7072 if (code->expr3->ts.type == BT_DERIVED
7073 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
7074 || (code->expr3->ts.u.derived->from_intmod
7075 == INTMOD_ISO_FORTRAN_ENV
7076 && code->expr3->ts.u.derived->intmod_sym_id
7077 == ISOFORTRAN_EVENT_TYPE)))
7079 gfc_error ("The source-expr at %L shall neither be of type "
7080 "EVENT_TYPE nor have a EVENT_TYPE component if "
7081 "allocate-object at %L is a coarray",
7082 &code->expr3->where, &e->where);
7083 goto failure;
7087 /* Check F08:C629. */
7088 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7089 && !code->expr3)
7091 gcc_assert (e->ts.type == BT_CLASS);
7092 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7093 "type-spec or source-expr", sym->name, &e->where);
7094 goto failure;
7097 /* Check F08:C632. */
7098 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
7099 && !UNLIMITED_POLY (e))
7101 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7102 code->ext.alloc.ts.u.cl->length);
7103 if (cmp == 1 || cmp == -1 || cmp == -3)
7105 gfc_error ("Allocating %s at %L with type-spec requires the same "
7106 "character-length parameter as in the declaration",
7107 sym->name, &e->where);
7108 goto failure;
7112 /* In the variable definition context checks, gfc_expr_attr is used
7113 on the expression. This is fooled by the array specification
7114 present in e, thus we have to eliminate that one temporarily. */
7115 e2 = remove_last_array_ref (e);
7116 t = true;
7117 if (t && pointer)
7118 t = gfc_check_vardef_context (e2, true, true, false,
7119 _("ALLOCATE object"));
7120 if (t)
7121 t = gfc_check_vardef_context (e2, false, true, false,
7122 _("ALLOCATE object"));
7123 gfc_free_expr (e2);
7124 if (!t)
7125 goto failure;
7127 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7128 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7130 /* For class arrays, the initialization with SOURCE is done
7131 using _copy and trans_call. It is convenient to exploit that
7132 when the allocated type is different from the declared type but
7133 no SOURCE exists by setting expr3. */
7134 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
7136 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
7137 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
7138 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
7140 /* We have to zero initialize the integer variable. */
7141 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
7143 else if (!code->expr3)
7145 /* Set up default initializer if needed. */
7146 gfc_typespec ts;
7147 gfc_expr *init_e;
7149 if (gfc_bt_struct (code->ext.alloc.ts.type))
7150 ts = code->ext.alloc.ts;
7151 else
7152 ts = e->ts;
7154 if (ts.type == BT_CLASS)
7155 ts = ts.u.derived->components->ts;
7157 if (gfc_bt_struct (ts.type) && (init_e = gfc_default_initializer (&ts)))
7159 gfc_code *init_st = gfc_get_code (EXEC_INIT_ASSIGN);
7160 init_st->loc = code->loc;
7161 init_st->expr1 = gfc_expr_to_initialize (e);
7162 init_st->expr2 = init_e;
7163 init_st->next = code->next;
7164 code->next = init_st;
7167 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
7169 /* Default initialization via MOLD (non-polymorphic). */
7170 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
7171 if (rhs != NULL)
7173 gfc_resolve_expr (rhs);
7174 gfc_free_expr (code->expr3);
7175 code->expr3 = rhs;
7179 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
7181 /* Make sure the vtab symbol is present when
7182 the module variables are generated. */
7183 gfc_typespec ts = e->ts;
7184 if (code->expr3)
7185 ts = code->expr3->ts;
7186 else if (code->ext.alloc.ts.type == BT_DERIVED)
7187 ts = code->ext.alloc.ts;
7189 gfc_find_derived_vtab (ts.u.derived);
7191 if (dimension)
7192 e = gfc_expr_to_initialize (e);
7194 else if (unlimited && !UNLIMITED_POLY (code->expr3))
7196 /* Again, make sure the vtab symbol is present when
7197 the module variables are generated. */
7198 gfc_typespec *ts = NULL;
7199 if (code->expr3)
7200 ts = &code->expr3->ts;
7201 else
7202 ts = &code->ext.alloc.ts;
7204 gcc_assert (ts);
7206 gfc_find_vtab (ts);
7208 if (dimension)
7209 e = gfc_expr_to_initialize (e);
7212 if (dimension == 0 && codimension == 0)
7213 goto success;
7215 /* Make sure the last reference node is an array specification. */
7217 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
7218 || (dimension && ref2->u.ar.dimen == 0))
7220 /* F08:C633. */
7221 if (code->expr3)
7223 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
7224 "in ALLOCATE statement at %L", &e->where))
7225 goto failure;
7226 if (code->expr3->rank != 0)
7227 *array_alloc_wo_spec = true;
7228 else
7230 gfc_error ("Array specification or array-valued SOURCE= "
7231 "expression required in ALLOCATE statement at %L",
7232 &e->where);
7233 goto failure;
7236 else
7238 gfc_error ("Array specification required in ALLOCATE statement "
7239 "at %L", &e->where);
7240 goto failure;
7244 /* Make sure that the array section reference makes sense in the
7245 context of an ALLOCATE specification. */
7247 ar = &ref2->u.ar;
7249 if (codimension)
7250 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
7251 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
7253 gfc_error ("Coarray specification required in ALLOCATE statement "
7254 "at %L", &e->where);
7255 goto failure;
7258 for (i = 0; i < ar->dimen; i++)
7260 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
7261 goto check_symbols;
7263 switch (ar->dimen_type[i])
7265 case DIMEN_ELEMENT:
7266 break;
7268 case DIMEN_RANGE:
7269 if (ar->start[i] != NULL
7270 && ar->end[i] != NULL
7271 && ar->stride[i] == NULL)
7272 break;
7274 /* Fall Through... */
7276 case DIMEN_UNKNOWN:
7277 case DIMEN_VECTOR:
7278 case DIMEN_STAR:
7279 case DIMEN_THIS_IMAGE:
7280 gfc_error ("Bad array specification in ALLOCATE statement at %L",
7281 &e->where);
7282 goto failure;
7285 check_symbols:
7286 for (a = code->ext.alloc.list; a; a = a->next)
7288 sym = a->expr->symtree->n.sym;
7290 /* TODO - check derived type components. */
7291 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
7292 continue;
7294 if ((ar->start[i] != NULL
7295 && gfc_find_sym_in_expr (sym, ar->start[i]))
7296 || (ar->end[i] != NULL
7297 && gfc_find_sym_in_expr (sym, ar->end[i])))
7299 gfc_error ("%qs must not appear in the array specification at "
7300 "%L in the same ALLOCATE statement where it is "
7301 "itself allocated", sym->name, &ar->where);
7302 goto failure;
7307 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7309 if (ar->dimen_type[i] == DIMEN_ELEMENT
7310 || ar->dimen_type[i] == DIMEN_RANGE)
7312 if (i == (ar->dimen + ar->codimen - 1))
7314 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7315 "statement at %L", &e->where);
7316 goto failure;
7318 continue;
7321 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7322 && ar->stride[i] == NULL)
7323 break;
7325 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7326 &e->where);
7327 goto failure;
7330 success:
7331 return true;
7333 failure:
7334 return false;
7338 static void
7339 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7341 gfc_expr *stat, *errmsg, *pe, *qe;
7342 gfc_alloc *a, *p, *q;
7344 stat = code->expr1;
7345 errmsg = code->expr2;
7347 /* Check the stat variable. */
7348 if (stat)
7350 gfc_check_vardef_context (stat, false, false, false,
7351 _("STAT variable"));
7353 if ((stat->ts.type != BT_INTEGER
7354 && !(stat->ref && (stat->ref->type == REF_ARRAY
7355 || stat->ref->type == REF_COMPONENT)))
7356 || stat->rank > 0)
7357 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7358 "variable", &stat->where);
7360 for (p = code->ext.alloc.list; p; p = p->next)
7361 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7363 gfc_ref *ref1, *ref2;
7364 bool found = true;
7366 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7367 ref1 = ref1->next, ref2 = ref2->next)
7369 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7370 continue;
7371 if (ref1->u.c.component->name != ref2->u.c.component->name)
7373 found = false;
7374 break;
7378 if (found)
7380 gfc_error ("Stat-variable at %L shall not be %sd within "
7381 "the same %s statement", &stat->where, fcn, fcn);
7382 break;
7387 /* Check the errmsg variable. */
7388 if (errmsg)
7390 if (!stat)
7391 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
7392 &errmsg->where);
7394 gfc_check_vardef_context (errmsg, false, false, false,
7395 _("ERRMSG variable"));
7397 if ((errmsg->ts.type != BT_CHARACTER
7398 && !(errmsg->ref
7399 && (errmsg->ref->type == REF_ARRAY
7400 || errmsg->ref->type == REF_COMPONENT)))
7401 || errmsg->rank > 0 )
7402 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7403 "variable", &errmsg->where);
7405 for (p = code->ext.alloc.list; p; p = p->next)
7406 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7408 gfc_ref *ref1, *ref2;
7409 bool found = true;
7411 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7412 ref1 = ref1->next, ref2 = ref2->next)
7414 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7415 continue;
7416 if (ref1->u.c.component->name != ref2->u.c.component->name)
7418 found = false;
7419 break;
7423 if (found)
7425 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7426 "the same %s statement", &errmsg->where, fcn, fcn);
7427 break;
7432 /* Check that an allocate-object appears only once in the statement. */
7434 for (p = code->ext.alloc.list; p; p = p->next)
7436 pe = p->expr;
7437 for (q = p->next; q; q = q->next)
7439 qe = q->expr;
7440 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7442 /* This is a potential collision. */
7443 gfc_ref *pr = pe->ref;
7444 gfc_ref *qr = qe->ref;
7446 /* Follow the references until
7447 a) They start to differ, in which case there is no error;
7448 you can deallocate a%b and a%c in a single statement
7449 b) Both of them stop, which is an error
7450 c) One of them stops, which is also an error. */
7451 while (1)
7453 if (pr == NULL && qr == NULL)
7455 gfc_error ("Allocate-object at %L also appears at %L",
7456 &pe->where, &qe->where);
7457 break;
7459 else if (pr != NULL && qr == NULL)
7461 gfc_error ("Allocate-object at %L is subobject of"
7462 " object at %L", &pe->where, &qe->where);
7463 break;
7465 else if (pr == NULL && qr != NULL)
7467 gfc_error ("Allocate-object at %L is subobject of"
7468 " object at %L", &qe->where, &pe->where);
7469 break;
7471 /* Here, pr != NULL && qr != NULL */
7472 gcc_assert(pr->type == qr->type);
7473 if (pr->type == REF_ARRAY)
7475 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7476 which are legal. */
7477 gcc_assert (qr->type == REF_ARRAY);
7479 if (pr->next && qr->next)
7481 int i;
7482 gfc_array_ref *par = &(pr->u.ar);
7483 gfc_array_ref *qar = &(qr->u.ar);
7485 for (i=0; i<par->dimen; i++)
7487 if ((par->start[i] != NULL
7488 || qar->start[i] != NULL)
7489 && gfc_dep_compare_expr (par->start[i],
7490 qar->start[i]) != 0)
7491 goto break_label;
7495 else
7497 if (pr->u.c.component->name != qr->u.c.component->name)
7498 break;
7501 pr = pr->next;
7502 qr = qr->next;
7504 break_label:
7510 if (strcmp (fcn, "ALLOCATE") == 0)
7512 bool arr_alloc_wo_spec = false;
7513 for (a = code->ext.alloc.list; a; a = a->next)
7514 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
7516 if (arr_alloc_wo_spec && code->expr3)
7518 /* Mark the allocate to have to take the array specification
7519 from the expr3. */
7520 code->ext.alloc.arr_spec_from_expr3 = 1;
7523 else
7525 for (a = code->ext.alloc.list; a; a = a->next)
7526 resolve_deallocate_expr (a->expr);
7531 /************ SELECT CASE resolution subroutines ************/
7533 /* Callback function for our mergesort variant. Determines interval
7534 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7535 op1 > op2. Assumes we're not dealing with the default case.
7536 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7537 There are nine situations to check. */
7539 static int
7540 compare_cases (const gfc_case *op1, const gfc_case *op2)
7542 int retval;
7544 if (op1->low == NULL) /* op1 = (:L) */
7546 /* op2 = (:N), so overlap. */
7547 retval = 0;
7548 /* op2 = (M:) or (M:N), L < M */
7549 if (op2->low != NULL
7550 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7551 retval = -1;
7553 else if (op1->high == NULL) /* op1 = (K:) */
7555 /* op2 = (M:), so overlap. */
7556 retval = 0;
7557 /* op2 = (:N) or (M:N), K > N */
7558 if (op2->high != NULL
7559 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7560 retval = 1;
7562 else /* op1 = (K:L) */
7564 if (op2->low == NULL) /* op2 = (:N), K > N */
7565 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7566 ? 1 : 0;
7567 else if (op2->high == NULL) /* op2 = (M:), L < M */
7568 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7569 ? -1 : 0;
7570 else /* op2 = (M:N) */
7572 retval = 0;
7573 /* L < M */
7574 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7575 retval = -1;
7576 /* K > N */
7577 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7578 retval = 1;
7582 return retval;
7586 /* Merge-sort a double linked case list, detecting overlap in the
7587 process. LIST is the head of the double linked case list before it
7588 is sorted. Returns the head of the sorted list if we don't see any
7589 overlap, or NULL otherwise. */
7591 static gfc_case *
7592 check_case_overlap (gfc_case *list)
7594 gfc_case *p, *q, *e, *tail;
7595 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7597 /* If the passed list was empty, return immediately. */
7598 if (!list)
7599 return NULL;
7601 overlap_seen = 0;
7602 insize = 1;
7604 /* Loop unconditionally. The only exit from this loop is a return
7605 statement, when we've finished sorting the case list. */
7606 for (;;)
7608 p = list;
7609 list = NULL;
7610 tail = NULL;
7612 /* Count the number of merges we do in this pass. */
7613 nmerges = 0;
7615 /* Loop while there exists a merge to be done. */
7616 while (p)
7618 int i;
7620 /* Count this merge. */
7621 nmerges++;
7623 /* Cut the list in two pieces by stepping INSIZE places
7624 forward in the list, starting from P. */
7625 psize = 0;
7626 q = p;
7627 for (i = 0; i < insize; i++)
7629 psize++;
7630 q = q->right;
7631 if (!q)
7632 break;
7634 qsize = insize;
7636 /* Now we have two lists. Merge them! */
7637 while (psize > 0 || (qsize > 0 && q != NULL))
7639 /* See from which the next case to merge comes from. */
7640 if (psize == 0)
7642 /* P is empty so the next case must come from Q. */
7643 e = q;
7644 q = q->right;
7645 qsize--;
7647 else if (qsize == 0 || q == NULL)
7649 /* Q is empty. */
7650 e = p;
7651 p = p->right;
7652 psize--;
7654 else
7656 cmp = compare_cases (p, q);
7657 if (cmp < 0)
7659 /* The whole case range for P is less than the
7660 one for Q. */
7661 e = p;
7662 p = p->right;
7663 psize--;
7665 else if (cmp > 0)
7667 /* The whole case range for Q is greater than
7668 the case range for P. */
7669 e = q;
7670 q = q->right;
7671 qsize--;
7673 else
7675 /* The cases overlap, or they are the same
7676 element in the list. Either way, we must
7677 issue an error and get the next case from P. */
7678 /* FIXME: Sort P and Q by line number. */
7679 gfc_error ("CASE label at %L overlaps with CASE "
7680 "label at %L", &p->where, &q->where);
7681 overlap_seen = 1;
7682 e = p;
7683 p = p->right;
7684 psize--;
7688 /* Add the next element to the merged list. */
7689 if (tail)
7690 tail->right = e;
7691 else
7692 list = e;
7693 e->left = tail;
7694 tail = e;
7697 /* P has now stepped INSIZE places along, and so has Q. So
7698 they're the same. */
7699 p = q;
7701 tail->right = NULL;
7703 /* If we have done only one merge or none at all, we've
7704 finished sorting the cases. */
7705 if (nmerges <= 1)
7707 if (!overlap_seen)
7708 return list;
7709 else
7710 return NULL;
7713 /* Otherwise repeat, merging lists twice the size. */
7714 insize *= 2;
7719 /* Check to see if an expression is suitable for use in a CASE statement.
7720 Makes sure that all case expressions are scalar constants of the same
7721 type. Return false if anything is wrong. */
7723 static bool
7724 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7726 if (e == NULL) return true;
7728 if (e->ts.type != case_expr->ts.type)
7730 gfc_error ("Expression in CASE statement at %L must be of type %s",
7731 &e->where, gfc_basic_typename (case_expr->ts.type));
7732 return false;
7735 /* C805 (R808) For a given case-construct, each case-value shall be of
7736 the same type as case-expr. For character type, length differences
7737 are allowed, but the kind type parameters shall be the same. */
7739 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7741 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7742 &e->where, case_expr->ts.kind);
7743 return false;
7746 /* Convert the case value kind to that of case expression kind,
7747 if needed */
7749 if (e->ts.kind != case_expr->ts.kind)
7750 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7752 if (e->rank != 0)
7754 gfc_error ("Expression in CASE statement at %L must be scalar",
7755 &e->where);
7756 return false;
7759 return true;
7763 /* Given a completely parsed select statement, we:
7765 - Validate all expressions and code within the SELECT.
7766 - Make sure that the selection expression is not of the wrong type.
7767 - Make sure that no case ranges overlap.
7768 - Eliminate unreachable cases and unreachable code resulting from
7769 removing case labels.
7771 The standard does allow unreachable cases, e.g. CASE (5:3). But
7772 they are a hassle for code generation, and to prevent that, we just
7773 cut them out here. This is not necessary for overlapping cases
7774 because they are illegal and we never even try to generate code.
7776 We have the additional caveat that a SELECT construct could have
7777 been a computed GOTO in the source code. Fortunately we can fairly
7778 easily work around that here: The case_expr for a "real" SELECT CASE
7779 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7780 we have to do is make sure that the case_expr is a scalar integer
7781 expression. */
7783 static void
7784 resolve_select (gfc_code *code, bool select_type)
7786 gfc_code *body;
7787 gfc_expr *case_expr;
7788 gfc_case *cp, *default_case, *tail, *head;
7789 int seen_unreachable;
7790 int seen_logical;
7791 int ncases;
7792 bt type;
7793 bool t;
7795 if (code->expr1 == NULL)
7797 /* This was actually a computed GOTO statement. */
7798 case_expr = code->expr2;
7799 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7800 gfc_error ("Selection expression in computed GOTO statement "
7801 "at %L must be a scalar integer expression",
7802 &case_expr->where);
7804 /* Further checking is not necessary because this SELECT was built
7805 by the compiler, so it should always be OK. Just move the
7806 case_expr from expr2 to expr so that we can handle computed
7807 GOTOs as normal SELECTs from here on. */
7808 code->expr1 = code->expr2;
7809 code->expr2 = NULL;
7810 return;
7813 case_expr = code->expr1;
7814 type = case_expr->ts.type;
7816 /* F08:C830. */
7817 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7819 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7820 &case_expr->where, gfc_typename (&case_expr->ts));
7822 /* Punt. Going on here just produce more garbage error messages. */
7823 return;
7826 /* F08:R842. */
7827 if (!select_type && case_expr->rank != 0)
7829 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7830 "expression", &case_expr->where);
7832 /* Punt. */
7833 return;
7836 /* Raise a warning if an INTEGER case value exceeds the range of
7837 the case-expr. Later, all expressions will be promoted to the
7838 largest kind of all case-labels. */
7840 if (type == BT_INTEGER)
7841 for (body = code->block; body; body = body->block)
7842 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7844 if (cp->low
7845 && gfc_check_integer_range (cp->low->value.integer,
7846 case_expr->ts.kind) != ARITH_OK)
7847 gfc_warning (0, "Expression in CASE statement at %L is "
7848 "not in the range of %s", &cp->low->where,
7849 gfc_typename (&case_expr->ts));
7851 if (cp->high
7852 && cp->low != cp->high
7853 && gfc_check_integer_range (cp->high->value.integer,
7854 case_expr->ts.kind) != ARITH_OK)
7855 gfc_warning (0, "Expression in CASE statement at %L is "
7856 "not in the range of %s", &cp->high->where,
7857 gfc_typename (&case_expr->ts));
7860 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7861 of the SELECT CASE expression and its CASE values. Walk the lists
7862 of case values, and if we find a mismatch, promote case_expr to
7863 the appropriate kind. */
7865 if (type == BT_LOGICAL || type == BT_INTEGER)
7867 for (body = code->block; body; body = body->block)
7869 /* Walk the case label list. */
7870 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7872 /* Intercept the DEFAULT case. It does not have a kind. */
7873 if (cp->low == NULL && cp->high == NULL)
7874 continue;
7876 /* Unreachable case ranges are discarded, so ignore. */
7877 if (cp->low != NULL && cp->high != NULL
7878 && cp->low != cp->high
7879 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7880 continue;
7882 if (cp->low != NULL
7883 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7884 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7886 if (cp->high != NULL
7887 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7888 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7893 /* Assume there is no DEFAULT case. */
7894 default_case = NULL;
7895 head = tail = NULL;
7896 ncases = 0;
7897 seen_logical = 0;
7899 for (body = code->block; body; body = body->block)
7901 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7902 t = true;
7903 seen_unreachable = 0;
7905 /* Walk the case label list, making sure that all case labels
7906 are legal. */
7907 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7909 /* Count the number of cases in the whole construct. */
7910 ncases++;
7912 /* Intercept the DEFAULT case. */
7913 if (cp->low == NULL && cp->high == NULL)
7915 if (default_case != NULL)
7917 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7918 "by a second DEFAULT CASE at %L",
7919 &default_case->where, &cp->where);
7920 t = false;
7921 break;
7923 else
7925 default_case = cp;
7926 continue;
7930 /* Deal with single value cases and case ranges. Errors are
7931 issued from the validation function. */
7932 if (!validate_case_label_expr (cp->low, case_expr)
7933 || !validate_case_label_expr (cp->high, case_expr))
7935 t = false;
7936 break;
7939 if (type == BT_LOGICAL
7940 && ((cp->low == NULL || cp->high == NULL)
7941 || cp->low != cp->high))
7943 gfc_error ("Logical range in CASE statement at %L is not "
7944 "allowed", &cp->low->where);
7945 t = false;
7946 break;
7949 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7951 int value;
7952 value = cp->low->value.logical == 0 ? 2 : 1;
7953 if (value & seen_logical)
7955 gfc_error ("Constant logical value in CASE statement "
7956 "is repeated at %L",
7957 &cp->low->where);
7958 t = false;
7959 break;
7961 seen_logical |= value;
7964 if (cp->low != NULL && cp->high != NULL
7965 && cp->low != cp->high
7966 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7968 if (warn_surprising)
7969 gfc_warning (OPT_Wsurprising,
7970 "Range specification at %L can never be matched",
7971 &cp->where);
7973 cp->unreachable = 1;
7974 seen_unreachable = 1;
7976 else
7978 /* If the case range can be matched, it can also overlap with
7979 other cases. To make sure it does not, we put it in a
7980 double linked list here. We sort that with a merge sort
7981 later on to detect any overlapping cases. */
7982 if (!head)
7984 head = tail = cp;
7985 head->right = head->left = NULL;
7987 else
7989 tail->right = cp;
7990 tail->right->left = tail;
7991 tail = tail->right;
7992 tail->right = NULL;
7997 /* It there was a failure in the previous case label, give up
7998 for this case label list. Continue with the next block. */
7999 if (!t)
8000 continue;
8002 /* See if any case labels that are unreachable have been seen.
8003 If so, we eliminate them. This is a bit of a kludge because
8004 the case lists for a single case statement (label) is a
8005 single forward linked lists. */
8006 if (seen_unreachable)
8008 /* Advance until the first case in the list is reachable. */
8009 while (body->ext.block.case_list != NULL
8010 && body->ext.block.case_list->unreachable)
8012 gfc_case *n = body->ext.block.case_list;
8013 body->ext.block.case_list = body->ext.block.case_list->next;
8014 n->next = NULL;
8015 gfc_free_case_list (n);
8018 /* Strip all other unreachable cases. */
8019 if (body->ext.block.case_list)
8021 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
8023 if (cp->next->unreachable)
8025 gfc_case *n = cp->next;
8026 cp->next = cp->next->next;
8027 n->next = NULL;
8028 gfc_free_case_list (n);
8035 /* See if there were overlapping cases. If the check returns NULL,
8036 there was overlap. In that case we don't do anything. If head
8037 is non-NULL, we prepend the DEFAULT case. The sorted list can
8038 then used during code generation for SELECT CASE constructs with
8039 a case expression of a CHARACTER type. */
8040 if (head)
8042 head = check_case_overlap (head);
8044 /* Prepend the default_case if it is there. */
8045 if (head != NULL && default_case)
8047 default_case->left = NULL;
8048 default_case->right = head;
8049 head->left = default_case;
8053 /* Eliminate dead blocks that may be the result if we've seen
8054 unreachable case labels for a block. */
8055 for (body = code; body && body->block; body = body->block)
8057 if (body->block->ext.block.case_list == NULL)
8059 /* Cut the unreachable block from the code chain. */
8060 gfc_code *c = body->block;
8061 body->block = c->block;
8063 /* Kill the dead block, but not the blocks below it. */
8064 c->block = NULL;
8065 gfc_free_statements (c);
8069 /* More than two cases is legal but insane for logical selects.
8070 Issue a warning for it. */
8071 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
8072 gfc_warning (OPT_Wsurprising,
8073 "Logical SELECT CASE block at %L has more that two cases",
8074 &code->loc);
8078 /* Check if a derived type is extensible. */
8080 bool
8081 gfc_type_is_extensible (gfc_symbol *sym)
8083 return !(sym->attr.is_bind_c || sym->attr.sequence
8084 || (sym->attr.is_class
8085 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
8089 static void
8090 resolve_types (gfc_namespace *ns);
8092 /* Resolve an associate-name: Resolve target and ensure the type-spec is
8093 correct as well as possibly the array-spec. */
8095 static void
8096 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
8098 gfc_expr* target;
8100 gcc_assert (sym->assoc);
8101 gcc_assert (sym->attr.flavor == FL_VARIABLE);
8103 /* If this is for SELECT TYPE, the target may not yet be set. In that
8104 case, return. Resolution will be called later manually again when
8105 this is done. */
8106 target = sym->assoc->target;
8107 if (!target)
8108 return;
8109 gcc_assert (!sym->assoc->dangling);
8111 if (resolve_target && !gfc_resolve_expr (target))
8112 return;
8114 /* For variable targets, we get some attributes from the target. */
8115 if (target->expr_type == EXPR_VARIABLE)
8117 gfc_symbol* tsym;
8119 gcc_assert (target->symtree);
8120 tsym = target->symtree->n.sym;
8122 sym->attr.asynchronous = tsym->attr.asynchronous;
8123 sym->attr.volatile_ = tsym->attr.volatile_;
8125 sym->attr.target = tsym->attr.target
8126 || gfc_expr_attr (target).pointer;
8127 if (is_subref_array (target))
8128 sym->attr.subref_array_pointer = 1;
8131 /* Get type if this was not already set. Note that it can be
8132 some other type than the target in case this is a SELECT TYPE
8133 selector! So we must not update when the type is already there. */
8134 if (sym->ts.type == BT_UNKNOWN)
8135 sym->ts = target->ts;
8136 gcc_assert (sym->ts.type != BT_UNKNOWN);
8138 /* See if this is a valid association-to-variable. */
8139 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
8140 && !gfc_has_vector_subscript (target));
8142 /* Finally resolve if this is an array or not. */
8143 if (sym->attr.dimension && target->rank == 0)
8145 /* primary.c makes the assumption that a reference to an associate
8146 name followed by a left parenthesis is an array reference. */
8147 if (sym->ts.type != BT_CHARACTER)
8148 gfc_error ("Associate-name %qs at %L is used as array",
8149 sym->name, &sym->declared_at);
8150 sym->attr.dimension = 0;
8151 return;
8155 /* We cannot deal with class selectors that need temporaries. */
8156 if (target->ts.type == BT_CLASS
8157 && gfc_ref_needs_temporary_p (target->ref))
8159 gfc_error ("CLASS selector at %L needs a temporary which is not "
8160 "yet implemented", &target->where);
8161 return;
8164 if (target->ts.type == BT_CLASS)
8165 gfc_fix_class_refs (target);
8167 if (target->rank != 0)
8169 gfc_array_spec *as;
8170 /* The rank may be incorrectly guessed at parsing, therefore make sure
8171 it is corrected now. */
8172 if (sym->ts.type != BT_CLASS && (!sym->as || sym->assoc->rankguessed))
8174 if (!sym->as)
8175 sym->as = gfc_get_array_spec ();
8176 as = sym->as;
8177 as->rank = target->rank;
8178 as->type = AS_DEFERRED;
8179 as->corank = gfc_get_corank (target);
8180 sym->attr.dimension = 1;
8181 if (as->corank != 0)
8182 sym->attr.codimension = 1;
8185 else
8187 /* target's rank is 0, but the type of the sym is still array valued,
8188 which has to be corrected. */
8189 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
8191 gfc_array_spec *as;
8192 symbol_attribute attr;
8193 /* The associated variable's type is still the array type
8194 correct this now. */
8195 gfc_typespec *ts = &target->ts;
8196 gfc_ref *ref;
8197 gfc_component *c;
8198 for (ref = target->ref; ref != NULL; ref = ref->next)
8200 switch (ref->type)
8202 case REF_COMPONENT:
8203 ts = &ref->u.c.component->ts;
8204 break;
8205 case REF_ARRAY:
8206 if (ts->type == BT_CLASS)
8207 ts = &ts->u.derived->components->ts;
8208 break;
8209 default:
8210 break;
8213 /* Create a scalar instance of the current class type. Because the
8214 rank of a class array goes into its name, the type has to be
8215 rebuild. The alternative of (re-)setting just the attributes
8216 and as in the current type, destroys the type also in other
8217 places. */
8218 as = NULL;
8219 sym->ts = *ts;
8220 sym->ts.type = BT_CLASS;
8221 attr = CLASS_DATA (sym)->attr;
8222 attr.class_ok = 0;
8223 attr.associate_var = 1;
8224 attr.dimension = attr.codimension = 0;
8225 attr.class_pointer = 1;
8226 if (!gfc_build_class_symbol (&sym->ts, &attr, &as))
8227 gcc_unreachable ();
8228 /* Make sure the _vptr is set. */
8229 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true, NULL);
8230 if (c->ts.u.derived == NULL)
8231 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived);
8232 CLASS_DATA (sym)->attr.pointer = 1;
8233 CLASS_DATA (sym)->attr.class_pointer = 1;
8234 gfc_set_sym_referenced (sym->ts.u.derived);
8235 gfc_commit_symbol (sym->ts.u.derived);
8236 /* _vptr now has the _vtab in it, change it to the _vtype. */
8237 if (c->ts.u.derived->attr.vtab)
8238 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
8239 c->ts.u.derived->ns->types_resolved = 0;
8240 resolve_types (c->ts.u.derived->ns);
8244 /* Mark this as an associate variable. */
8245 sym->attr.associate_var = 1;
8247 /* If the target is a good class object, so is the associate variable. */
8248 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
8249 sym->attr.class_ok = 1;
8253 /* Resolve a SELECT TYPE statement. */
8255 static void
8256 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
8258 gfc_symbol *selector_type;
8259 gfc_code *body, *new_st, *if_st, *tail;
8260 gfc_code *class_is = NULL, *default_case = NULL;
8261 gfc_case *c;
8262 gfc_symtree *st;
8263 char name[GFC_MAX_SYMBOL_LEN];
8264 gfc_namespace *ns;
8265 int error = 0;
8266 int charlen = 0;
8268 ns = code->ext.block.ns;
8269 gfc_resolve (ns);
8271 /* Check for F03:C813. */
8272 if (code->expr1->ts.type != BT_CLASS
8273 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
8275 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
8276 "at %L", &code->loc);
8277 return;
8280 if (!code->expr1->symtree->n.sym->attr.class_ok)
8281 return;
8283 if (code->expr2)
8285 if (code->expr1->symtree->n.sym->attr.untyped)
8286 code->expr1->symtree->n.sym->ts = code->expr2->ts;
8287 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
8289 /* F2008: C803 The selector expression must not be coindexed. */
8290 if (gfc_is_coindexed (code->expr2))
8292 gfc_error ("Selector at %L must not be coindexed",
8293 &code->expr2->where);
8294 return;
8298 else
8300 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
8302 if (gfc_is_coindexed (code->expr1))
8304 gfc_error ("Selector at %L must not be coindexed",
8305 &code->expr1->where);
8306 return;
8310 /* Loop over TYPE IS / CLASS IS cases. */
8311 for (body = code->block; body; body = body->block)
8313 c = body->ext.block.case_list;
8315 /* Check F03:C815. */
8316 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8317 && !selector_type->attr.unlimited_polymorphic
8318 && !gfc_type_is_extensible (c->ts.u.derived))
8320 gfc_error ("Derived type %qs at %L must be extensible",
8321 c->ts.u.derived->name, &c->where);
8322 error++;
8323 continue;
8326 /* Check F03:C816. */
8327 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
8328 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
8329 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
8331 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8332 gfc_error ("Derived type %qs at %L must be an extension of %qs",
8333 c->ts.u.derived->name, &c->where, selector_type->name);
8334 else
8335 gfc_error ("Unexpected intrinsic type %qs at %L",
8336 gfc_basic_typename (c->ts.type), &c->where);
8337 error++;
8338 continue;
8341 /* Check F03:C814. */
8342 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
8344 gfc_error ("The type-spec at %L shall specify that each length "
8345 "type parameter is assumed", &c->where);
8346 error++;
8347 continue;
8350 /* Intercept the DEFAULT case. */
8351 if (c->ts.type == BT_UNKNOWN)
8353 /* Check F03:C818. */
8354 if (default_case)
8356 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8357 "by a second DEFAULT CASE at %L",
8358 &default_case->ext.block.case_list->where, &c->where);
8359 error++;
8360 continue;
8363 default_case = body;
8367 if (error > 0)
8368 return;
8370 /* Transform SELECT TYPE statement to BLOCK and associate selector to
8371 target if present. If there are any EXIT statements referring to the
8372 SELECT TYPE construct, this is no problem because the gfc_code
8373 reference stays the same and EXIT is equally possible from the BLOCK
8374 it is changed to. */
8375 code->op = EXEC_BLOCK;
8376 if (code->expr2)
8378 gfc_association_list* assoc;
8380 assoc = gfc_get_association_list ();
8381 assoc->st = code->expr1->symtree;
8382 assoc->target = gfc_copy_expr (code->expr2);
8383 assoc->target->where = code->expr2->where;
8384 /* assoc->variable will be set by resolve_assoc_var. */
8386 code->ext.block.assoc = assoc;
8387 code->expr1->symtree->n.sym->assoc = assoc;
8389 resolve_assoc_var (code->expr1->symtree->n.sym, false);
8391 else
8392 code->ext.block.assoc = NULL;
8394 /* Add EXEC_SELECT to switch on type. */
8395 new_st = gfc_get_code (code->op);
8396 new_st->expr1 = code->expr1;
8397 new_st->expr2 = code->expr2;
8398 new_st->block = code->block;
8399 code->expr1 = code->expr2 = NULL;
8400 code->block = NULL;
8401 if (!ns->code)
8402 ns->code = new_st;
8403 else
8404 ns->code->next = new_st;
8405 code = new_st;
8406 code->op = EXEC_SELECT;
8408 gfc_add_vptr_component (code->expr1);
8409 gfc_add_hash_component (code->expr1);
8411 /* Loop over TYPE IS / CLASS IS cases. */
8412 for (body = code->block; body; body = body->block)
8414 c = body->ext.block.case_list;
8416 if (c->ts.type == BT_DERIVED)
8417 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8418 c->ts.u.derived->hash_value);
8419 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8421 gfc_symbol *ivtab;
8422 gfc_expr *e;
8424 ivtab = gfc_find_vtab (&c->ts);
8425 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
8426 e = CLASS_DATA (ivtab)->initializer;
8427 c->low = c->high = gfc_copy_expr (e);
8430 else if (c->ts.type == BT_UNKNOWN)
8431 continue;
8433 /* Associate temporary to selector. This should only be done
8434 when this case is actually true, so build a new ASSOCIATE
8435 that does precisely this here (instead of using the
8436 'global' one). */
8438 if (c->ts.type == BT_CLASS)
8439 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8440 else if (c->ts.type == BT_DERIVED)
8441 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8442 else if (c->ts.type == BT_CHARACTER)
8444 if (c->ts.u.cl && c->ts.u.cl->length
8445 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8446 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8447 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8448 charlen, c->ts.kind);
8450 else
8451 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8452 c->ts.kind);
8454 st = gfc_find_symtree (ns->sym_root, name);
8455 gcc_assert (st->n.sym->assoc);
8456 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
8457 st->n.sym->assoc->target->where = code->expr1->where;
8458 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8459 gfc_add_data_component (st->n.sym->assoc->target);
8461 new_st = gfc_get_code (EXEC_BLOCK);
8462 new_st->ext.block.ns = gfc_build_block_ns (ns);
8463 new_st->ext.block.ns->code = body->next;
8464 body->next = new_st;
8466 /* Chain in the new list only if it is marked as dangling. Otherwise
8467 there is a CASE label overlap and this is already used. Just ignore,
8468 the error is diagnosed elsewhere. */
8469 if (st->n.sym->assoc->dangling)
8471 new_st->ext.block.assoc = st->n.sym->assoc;
8472 st->n.sym->assoc->dangling = 0;
8475 resolve_assoc_var (st->n.sym, false);
8478 /* Take out CLASS IS cases for separate treatment. */
8479 body = code;
8480 while (body && body->block)
8482 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8484 /* Add to class_is list. */
8485 if (class_is == NULL)
8487 class_is = body->block;
8488 tail = class_is;
8490 else
8492 for (tail = class_is; tail->block; tail = tail->block) ;
8493 tail->block = body->block;
8494 tail = tail->block;
8496 /* Remove from EXEC_SELECT list. */
8497 body->block = body->block->block;
8498 tail->block = NULL;
8500 else
8501 body = body->block;
8504 if (class_is)
8506 gfc_symbol *vtab;
8508 if (!default_case)
8510 /* Add a default case to hold the CLASS IS cases. */
8511 for (tail = code; tail->block; tail = tail->block) ;
8512 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
8513 tail = tail->block;
8514 tail->ext.block.case_list = gfc_get_case ();
8515 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8516 tail->next = NULL;
8517 default_case = tail;
8520 /* More than one CLASS IS block? */
8521 if (class_is->block)
8523 gfc_code **c1,*c2;
8524 bool swapped;
8525 /* Sort CLASS IS blocks by extension level. */
8528 swapped = false;
8529 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8531 c2 = (*c1)->block;
8532 /* F03:C817 (check for doubles). */
8533 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8534 == c2->ext.block.case_list->ts.u.derived->hash_value)
8536 gfc_error ("Double CLASS IS block in SELECT TYPE "
8537 "statement at %L",
8538 &c2->ext.block.case_list->where);
8539 return;
8541 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8542 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8544 /* Swap. */
8545 (*c1)->block = c2->block;
8546 c2->block = *c1;
8547 *c1 = c2;
8548 swapped = true;
8552 while (swapped);
8555 /* Generate IF chain. */
8556 if_st = gfc_get_code (EXEC_IF);
8557 new_st = if_st;
8558 for (body = class_is; body; body = body->block)
8560 new_st->block = gfc_get_code (EXEC_IF);
8561 new_st = new_st->block;
8562 /* Set up IF condition: Call _gfortran_is_extension_of. */
8563 new_st->expr1 = gfc_get_expr ();
8564 new_st->expr1->expr_type = EXPR_FUNCTION;
8565 new_st->expr1->ts.type = BT_LOGICAL;
8566 new_st->expr1->ts.kind = 4;
8567 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8568 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8569 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8570 /* Set up arguments. */
8571 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8572 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8573 new_st->expr1->value.function.actual->expr->where = code->loc;
8574 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8575 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8576 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8577 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8578 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8579 new_st->next = body->next;
8581 if (default_case->next)
8583 new_st->block = gfc_get_code (EXEC_IF);
8584 new_st = new_st->block;
8585 new_st->next = default_case->next;
8588 /* Replace CLASS DEFAULT code by the IF chain. */
8589 default_case->next = if_st;
8592 /* Resolve the internal code. This can not be done earlier because
8593 it requires that the sym->assoc of selectors is set already. */
8594 gfc_current_ns = ns;
8595 gfc_resolve_blocks (code->block, gfc_current_ns);
8596 gfc_current_ns = old_ns;
8598 resolve_select (code, true);
8602 /* Resolve a transfer statement. This is making sure that:
8603 -- a derived type being transferred has only non-pointer components
8604 -- a derived type being transferred doesn't have private components, unless
8605 it's being transferred from the module where the type was defined
8606 -- we're not trying to transfer a whole assumed size array. */
8608 static void
8609 resolve_transfer (gfc_code *code)
8611 gfc_typespec *ts;
8612 gfc_symbol *sym;
8613 gfc_ref *ref;
8614 gfc_expr *exp;
8616 exp = code->expr1;
8618 while (exp != NULL && exp->expr_type == EXPR_OP
8619 && exp->value.op.op == INTRINSIC_PARENTHESES)
8620 exp = exp->value.op.op1;
8622 if (exp && exp->expr_type == EXPR_NULL
8623 && code->ext.dt)
8625 gfc_error ("Invalid context for NULL () intrinsic at %L",
8626 &exp->where);
8627 return;
8630 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8631 && exp->expr_type != EXPR_FUNCTION
8632 && exp->expr_type != EXPR_STRUCTURE))
8633 return;
8635 /* If we are reading, the variable will be changed. Note that
8636 code->ext.dt may be NULL if the TRANSFER is related to
8637 an INQUIRE statement -- but in this case, we are not reading, either. */
8638 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8639 && !gfc_check_vardef_context (exp, false, false, false,
8640 _("item in READ")))
8641 return;
8643 ts = exp->expr_type == EXPR_STRUCTURE ? &exp->ts : &exp->symtree->n.sym->ts;
8645 /* Go to actual component transferred. */
8646 for (ref = exp->ref; ref; ref = ref->next)
8647 if (ref->type == REF_COMPONENT)
8648 ts = &ref->u.c.component->ts;
8650 if (ts->type == BT_CLASS)
8652 /* FIXME: Test for defined input/output. */
8653 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8654 "it is processed by a defined input/output procedure",
8655 &code->loc);
8656 return;
8659 if (ts->type == BT_DERIVED)
8661 /* Check that transferred derived type doesn't contain POINTER
8662 components. */
8663 if (ts->u.derived->attr.pointer_comp)
8665 gfc_error ("Data transfer element at %L cannot have POINTER "
8666 "components unless it is processed by a defined "
8667 "input/output procedure", &code->loc);
8668 return;
8671 /* F08:C935. */
8672 if (ts->u.derived->attr.proc_pointer_comp)
8674 gfc_error ("Data transfer element at %L cannot have "
8675 "procedure pointer components", &code->loc);
8676 return;
8679 if (ts->u.derived->attr.alloc_comp)
8681 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8682 "components unless it is processed by a defined "
8683 "input/output procedure", &code->loc);
8684 return;
8687 /* C_PTR and C_FUNPTR have private components which means they can not
8688 be printed. However, if -std=gnu and not -pedantic, allow
8689 the component to be printed to help debugging. */
8690 if (ts->u.derived->ts.f90_type == BT_VOID)
8692 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8693 "cannot have PRIVATE components", &code->loc))
8694 return;
8696 else if (derived_inaccessible (ts->u.derived))
8698 gfc_error ("Data transfer element at %L cannot have "
8699 "PRIVATE components",&code->loc);
8700 return;
8704 if (exp->expr_type == EXPR_STRUCTURE)
8705 return;
8707 sym = exp->symtree->n.sym;
8709 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8710 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8712 gfc_error ("Data transfer element at %L cannot be a full reference to "
8713 "an assumed-size array", &code->loc);
8714 return;
8719 /*********** Toplevel code resolution subroutines ***********/
8721 /* Find the set of labels that are reachable from this block. We also
8722 record the last statement in each block. */
8724 static void
8725 find_reachable_labels (gfc_code *block)
8727 gfc_code *c;
8729 if (!block)
8730 return;
8732 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8734 /* Collect labels in this block. We don't keep those corresponding
8735 to END {IF|SELECT}, these are checked in resolve_branch by going
8736 up through the code_stack. */
8737 for (c = block; c; c = c->next)
8739 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8740 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8743 /* Merge with labels from parent block. */
8744 if (cs_base->prev)
8746 gcc_assert (cs_base->prev->reachable_labels);
8747 bitmap_ior_into (cs_base->reachable_labels,
8748 cs_base->prev->reachable_labels);
8753 static void
8754 resolve_lock_unlock_event (gfc_code *code)
8756 if (code->expr1->expr_type == EXPR_FUNCTION
8757 && code->expr1->value.function.isym
8758 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
8759 remove_caf_get_intrinsic (code->expr1);
8761 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
8762 && (code->expr1->ts.type != BT_DERIVED
8763 || code->expr1->expr_type != EXPR_VARIABLE
8764 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8765 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8766 || code->expr1->rank != 0
8767 || (!gfc_is_coarray (code->expr1) &&
8768 !gfc_is_coindexed (code->expr1))))
8769 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8770 &code->expr1->where);
8771 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
8772 && (code->expr1->ts.type != BT_DERIVED
8773 || code->expr1->expr_type != EXPR_VARIABLE
8774 || code->expr1->ts.u.derived->from_intmod
8775 != INTMOD_ISO_FORTRAN_ENV
8776 || code->expr1->ts.u.derived->intmod_sym_id
8777 != ISOFORTRAN_EVENT_TYPE
8778 || code->expr1->rank != 0))
8779 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
8780 &code->expr1->where);
8781 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
8782 && !gfc_is_coindexed (code->expr1))
8783 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
8784 &code->expr1->where);
8785 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
8786 gfc_error ("Event variable argument at %L must be a coarray but not "
8787 "coindexed", &code->expr1->where);
8789 /* Check STAT. */
8790 if (code->expr2
8791 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8792 || code->expr2->expr_type != EXPR_VARIABLE))
8793 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8794 &code->expr2->where);
8796 if (code->expr2
8797 && !gfc_check_vardef_context (code->expr2, false, false, false,
8798 _("STAT variable")))
8799 return;
8801 /* Check ERRMSG. */
8802 if (code->expr3
8803 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8804 || code->expr3->expr_type != EXPR_VARIABLE))
8805 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8806 &code->expr3->where);
8808 if (code->expr3
8809 && !gfc_check_vardef_context (code->expr3, false, false, false,
8810 _("ERRMSG variable")))
8811 return;
8813 /* Check for LOCK the ACQUIRED_LOCK. */
8814 if (code->op != EXEC_EVENT_WAIT && code->expr4
8815 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8816 || code->expr4->expr_type != EXPR_VARIABLE))
8817 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8818 "variable", &code->expr4->where);
8820 if (code->op != EXEC_EVENT_WAIT && code->expr4
8821 && !gfc_check_vardef_context (code->expr4, false, false, false,
8822 _("ACQUIRED_LOCK variable")))
8823 return;
8825 /* Check for EVENT WAIT the UNTIL_COUNT. */
8826 if (code->op == EXEC_EVENT_WAIT && code->expr4
8827 && (code->expr4->ts.type != BT_INTEGER || code->expr4->rank != 0))
8828 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
8829 "expression", &code->expr4->where);
8833 static void
8834 resolve_critical (gfc_code *code)
8836 gfc_symtree *symtree;
8837 gfc_symbol *lock_type;
8838 char name[GFC_MAX_SYMBOL_LEN];
8839 static int serial = 0;
8841 if (flag_coarray != GFC_FCOARRAY_LIB)
8842 return;
8844 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
8845 GFC_PREFIX ("lock_type"));
8846 if (symtree)
8847 lock_type = symtree->n.sym;
8848 else
8850 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
8851 false) != 0)
8852 gcc_unreachable ();
8853 lock_type = symtree->n.sym;
8854 lock_type->attr.flavor = FL_DERIVED;
8855 lock_type->attr.zero_comp = 1;
8856 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
8857 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
8860 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
8861 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
8862 gcc_unreachable ();
8864 code->resolved_sym = symtree->n.sym;
8865 symtree->n.sym->attr.flavor = FL_VARIABLE;
8866 symtree->n.sym->attr.referenced = 1;
8867 symtree->n.sym->attr.artificial = 1;
8868 symtree->n.sym->attr.codimension = 1;
8869 symtree->n.sym->ts.type = BT_DERIVED;
8870 symtree->n.sym->ts.u.derived = lock_type;
8871 symtree->n.sym->as = gfc_get_array_spec ();
8872 symtree->n.sym->as->corank = 1;
8873 symtree->n.sym->as->type = AS_EXPLICIT;
8874 symtree->n.sym->as->cotype = AS_EXPLICIT;
8875 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
8876 NULL, 1);
8877 gfc_commit_symbols();
8881 static void
8882 resolve_sync (gfc_code *code)
8884 /* Check imageset. The * case matches expr1 == NULL. */
8885 if (code->expr1)
8887 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8888 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8889 "INTEGER expression", &code->expr1->where);
8890 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8891 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8892 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8893 &code->expr1->where);
8894 else if (code->expr1->expr_type == EXPR_ARRAY
8895 && gfc_simplify_expr (code->expr1, 0))
8897 gfc_constructor *cons;
8898 cons = gfc_constructor_first (code->expr1->value.constructor);
8899 for (; cons; cons = gfc_constructor_next (cons))
8900 if (cons->expr->expr_type == EXPR_CONSTANT
8901 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8902 gfc_error ("Imageset argument at %L must between 1 and "
8903 "num_images()", &cons->expr->where);
8907 /* Check STAT. */
8908 if (code->expr2
8909 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8910 || code->expr2->expr_type != EXPR_VARIABLE))
8911 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8912 &code->expr2->where);
8914 /* Check ERRMSG. */
8915 if (code->expr3
8916 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8917 || code->expr3->expr_type != EXPR_VARIABLE))
8918 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8919 &code->expr3->where);
8923 /* Given a branch to a label, see if the branch is conforming.
8924 The code node describes where the branch is located. */
8926 static void
8927 resolve_branch (gfc_st_label *label, gfc_code *code)
8929 code_stack *stack;
8931 if (label == NULL)
8932 return;
8934 /* Step one: is this a valid branching target? */
8936 if (label->defined == ST_LABEL_UNKNOWN)
8938 gfc_error ("Label %d referenced at %L is never defined", label->value,
8939 &label->where);
8940 return;
8943 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
8945 gfc_error ("Statement at %L is not a valid branch target statement "
8946 "for the branch statement at %L", &label->where, &code->loc);
8947 return;
8950 /* Step two: make sure this branch is not a branch to itself ;-) */
8952 if (code->here == label)
8954 gfc_warning (0,
8955 "Branch at %L may result in an infinite loop", &code->loc);
8956 return;
8959 /* Step three: See if the label is in the same block as the
8960 branching statement. The hard work has been done by setting up
8961 the bitmap reachable_labels. */
8963 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8965 /* Check now whether there is a CRITICAL construct; if so, check
8966 whether the label is still visible outside of the CRITICAL block,
8967 which is invalid. */
8968 for (stack = cs_base; stack; stack = stack->prev)
8970 if (stack->current->op == EXEC_CRITICAL
8971 && bitmap_bit_p (stack->reachable_labels, label->value))
8972 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8973 "label at %L", &code->loc, &label->where);
8974 else if (stack->current->op == EXEC_DO_CONCURRENT
8975 && bitmap_bit_p (stack->reachable_labels, label->value))
8976 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8977 "for label at %L", &code->loc, &label->where);
8980 return;
8983 /* Step four: If we haven't found the label in the bitmap, it may
8984 still be the label of the END of the enclosing block, in which
8985 case we find it by going up the code_stack. */
8987 for (stack = cs_base; stack; stack = stack->prev)
8989 if (stack->current->next && stack->current->next->here == label)
8990 break;
8991 if (stack->current->op == EXEC_CRITICAL)
8993 /* Note: A label at END CRITICAL does not leave the CRITICAL
8994 construct as END CRITICAL is still part of it. */
8995 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8996 " at %L", &code->loc, &label->where);
8997 return;
8999 else if (stack->current->op == EXEC_DO_CONCURRENT)
9001 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
9002 "label at %L", &code->loc, &label->where);
9003 return;
9007 if (stack)
9009 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
9010 return;
9013 /* The label is not in an enclosing block, so illegal. This was
9014 allowed in Fortran 66, so we allow it as extension. No
9015 further checks are necessary in this case. */
9016 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
9017 "as the GOTO statement at %L", &label->where,
9018 &code->loc);
9019 return;
9023 /* Check whether EXPR1 has the same shape as EXPR2. */
9025 static bool
9026 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
9028 mpz_t shape[GFC_MAX_DIMENSIONS];
9029 mpz_t shape2[GFC_MAX_DIMENSIONS];
9030 bool result = false;
9031 int i;
9033 /* Compare the rank. */
9034 if (expr1->rank != expr2->rank)
9035 return result;
9037 /* Compare the size of each dimension. */
9038 for (i=0; i<expr1->rank; i++)
9040 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
9041 goto ignore;
9043 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
9044 goto ignore;
9046 if (mpz_cmp (shape[i], shape2[i]))
9047 goto over;
9050 /* When either of the two expression is an assumed size array, we
9051 ignore the comparison of dimension sizes. */
9052 ignore:
9053 result = true;
9055 over:
9056 gfc_clear_shape (shape, i);
9057 gfc_clear_shape (shape2, i);
9058 return result;
9062 /* Check whether a WHERE assignment target or a WHERE mask expression
9063 has the same shape as the outmost WHERE mask expression. */
9065 static void
9066 resolve_where (gfc_code *code, gfc_expr *mask)
9068 gfc_code *cblock;
9069 gfc_code *cnext;
9070 gfc_expr *e = NULL;
9072 cblock = code->block;
9074 /* Store the first WHERE mask-expr of the WHERE statement or construct.
9075 In case of nested WHERE, only the outmost one is stored. */
9076 if (mask == NULL) /* outmost WHERE */
9077 e = cblock->expr1;
9078 else /* inner WHERE */
9079 e = mask;
9081 while (cblock)
9083 if (cblock->expr1)
9085 /* Check if the mask-expr has a consistent shape with the
9086 outmost WHERE mask-expr. */
9087 if (!resolve_where_shape (cblock->expr1, e))
9088 gfc_error ("WHERE mask at %L has inconsistent shape",
9089 &cblock->expr1->where);
9092 /* the assignment statement of a WHERE statement, or the first
9093 statement in where-body-construct of a WHERE construct */
9094 cnext = cblock->next;
9095 while (cnext)
9097 switch (cnext->op)
9099 /* WHERE assignment statement */
9100 case EXEC_ASSIGN:
9102 /* Check shape consistent for WHERE assignment target. */
9103 if (e && !resolve_where_shape (cnext->expr1, e))
9104 gfc_error ("WHERE assignment target at %L has "
9105 "inconsistent shape", &cnext->expr1->where);
9106 break;
9109 case EXEC_ASSIGN_CALL:
9110 resolve_call (cnext);
9111 if (!cnext->resolved_sym->attr.elemental)
9112 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
9113 &cnext->ext.actual->expr->where);
9114 break;
9116 /* WHERE or WHERE construct is part of a where-body-construct */
9117 case EXEC_WHERE:
9118 resolve_where (cnext, e);
9119 break;
9121 default:
9122 gfc_error ("Unsupported statement inside WHERE at %L",
9123 &cnext->loc);
9125 /* the next statement within the same where-body-construct */
9126 cnext = cnext->next;
9128 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
9129 cblock = cblock->block;
9134 /* Resolve assignment in FORALL construct.
9135 NVAR is the number of FORALL index variables, and VAR_EXPR records the
9136 FORALL index variables. */
9138 static void
9139 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
9141 int n;
9143 for (n = 0; n < nvar; n++)
9145 gfc_symbol *forall_index;
9147 forall_index = var_expr[n]->symtree->n.sym;
9149 /* Check whether the assignment target is one of the FORALL index
9150 variable. */
9151 if ((code->expr1->expr_type == EXPR_VARIABLE)
9152 && (code->expr1->symtree->n.sym == forall_index))
9153 gfc_error ("Assignment to a FORALL index variable at %L",
9154 &code->expr1->where);
9155 else
9157 /* If one of the FORALL index variables doesn't appear in the
9158 assignment variable, then there could be a many-to-one
9159 assignment. Emit a warning rather than an error because the
9160 mask could be resolving this problem. */
9161 if (!find_forall_index (code->expr1, forall_index, 0))
9162 gfc_warning (0, "The FORALL with index %qs is not used on the "
9163 "left side of the assignment at %L and so might "
9164 "cause multiple assignment to this object",
9165 var_expr[n]->symtree->name, &code->expr1->where);
9171 /* Resolve WHERE statement in FORALL construct. */
9173 static void
9174 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
9175 gfc_expr **var_expr)
9177 gfc_code *cblock;
9178 gfc_code *cnext;
9180 cblock = code->block;
9181 while (cblock)
9183 /* the assignment statement of a WHERE statement, or the first
9184 statement in where-body-construct of a WHERE construct */
9185 cnext = cblock->next;
9186 while (cnext)
9188 switch (cnext->op)
9190 /* WHERE assignment statement */
9191 case EXEC_ASSIGN:
9192 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
9193 break;
9195 /* WHERE operator assignment statement */
9196 case EXEC_ASSIGN_CALL:
9197 resolve_call (cnext);
9198 if (!cnext->resolved_sym->attr.elemental)
9199 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
9200 &cnext->ext.actual->expr->where);
9201 break;
9203 /* WHERE or WHERE construct is part of a where-body-construct */
9204 case EXEC_WHERE:
9205 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
9206 break;
9208 default:
9209 gfc_error ("Unsupported statement inside WHERE at %L",
9210 &cnext->loc);
9212 /* the next statement within the same where-body-construct */
9213 cnext = cnext->next;
9215 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
9216 cblock = cblock->block;
9221 /* Traverse the FORALL body to check whether the following errors exist:
9222 1. For assignment, check if a many-to-one assignment happens.
9223 2. For WHERE statement, check the WHERE body to see if there is any
9224 many-to-one assignment. */
9226 static void
9227 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
9229 gfc_code *c;
9231 c = code->block->next;
9232 while (c)
9234 switch (c->op)
9236 case EXEC_ASSIGN:
9237 case EXEC_POINTER_ASSIGN:
9238 gfc_resolve_assign_in_forall (c, nvar, var_expr);
9239 break;
9241 case EXEC_ASSIGN_CALL:
9242 resolve_call (c);
9243 break;
9245 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
9246 there is no need to handle it here. */
9247 case EXEC_FORALL:
9248 break;
9249 case EXEC_WHERE:
9250 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
9251 break;
9252 default:
9253 break;
9255 /* The next statement in the FORALL body. */
9256 c = c->next;
9261 /* Counts the number of iterators needed inside a forall construct, including
9262 nested forall constructs. This is used to allocate the needed memory
9263 in gfc_resolve_forall. */
9265 static int
9266 gfc_count_forall_iterators (gfc_code *code)
9268 int max_iters, sub_iters, current_iters;
9269 gfc_forall_iterator *fa;
9271 gcc_assert(code->op == EXEC_FORALL);
9272 max_iters = 0;
9273 current_iters = 0;
9275 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9276 current_iters ++;
9278 code = code->block->next;
9280 while (code)
9282 if (code->op == EXEC_FORALL)
9284 sub_iters = gfc_count_forall_iterators (code);
9285 if (sub_iters > max_iters)
9286 max_iters = sub_iters;
9288 code = code->next;
9291 return current_iters + max_iters;
9295 /* Given a FORALL construct, first resolve the FORALL iterator, then call
9296 gfc_resolve_forall_body to resolve the FORALL body. */
9298 static void
9299 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
9301 static gfc_expr **var_expr;
9302 static int total_var = 0;
9303 static int nvar = 0;
9304 int old_nvar, tmp;
9305 gfc_forall_iterator *fa;
9306 int i;
9308 old_nvar = nvar;
9310 /* Start to resolve a FORALL construct */
9311 if (forall_save == 0)
9313 /* Count the total number of FORALL index in the nested FORALL
9314 construct in order to allocate the VAR_EXPR with proper size. */
9315 total_var = gfc_count_forall_iterators (code);
9317 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
9318 var_expr = XCNEWVEC (gfc_expr *, total_var);
9321 /* The information about FORALL iterator, including FORALL index start, end
9322 and stride. The FORALL index can not appear in start, end or stride. */
9323 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9325 /* Check if any outer FORALL index name is the same as the current
9326 one. */
9327 for (i = 0; i < nvar; i++)
9329 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
9331 gfc_error ("An outer FORALL construct already has an index "
9332 "with this name %L", &fa->var->where);
9336 /* Record the current FORALL index. */
9337 var_expr[nvar] = gfc_copy_expr (fa->var);
9339 nvar++;
9341 /* No memory leak. */
9342 gcc_assert (nvar <= total_var);
9345 /* Resolve the FORALL body. */
9346 gfc_resolve_forall_body (code, nvar, var_expr);
9348 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
9349 gfc_resolve_blocks (code->block, ns);
9351 tmp = nvar;
9352 nvar = old_nvar;
9353 /* Free only the VAR_EXPRs allocated in this frame. */
9354 for (i = nvar; i < tmp; i++)
9355 gfc_free_expr (var_expr[i]);
9357 if (nvar == 0)
9359 /* We are in the outermost FORALL construct. */
9360 gcc_assert (forall_save == 0);
9362 /* VAR_EXPR is not needed any more. */
9363 free (var_expr);
9364 total_var = 0;
9369 /* Resolve a BLOCK construct statement. */
9371 static void
9372 resolve_block_construct (gfc_code* code)
9374 /* Resolve the BLOCK's namespace. */
9375 gfc_resolve (code->ext.block.ns);
9377 /* For an ASSOCIATE block, the associations (and their targets) are already
9378 resolved during resolve_symbol. */
9382 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
9383 DO code nodes. */
9385 void
9386 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
9388 bool t;
9390 for (; b; b = b->block)
9392 t = gfc_resolve_expr (b->expr1);
9393 if (!gfc_resolve_expr (b->expr2))
9394 t = false;
9396 switch (b->op)
9398 case EXEC_IF:
9399 if (t && b->expr1 != NULL
9400 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
9401 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9402 &b->expr1->where);
9403 break;
9405 case EXEC_WHERE:
9406 if (t
9407 && b->expr1 != NULL
9408 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
9409 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
9410 &b->expr1->where);
9411 break;
9413 case EXEC_GOTO:
9414 resolve_branch (b->label1, b);
9415 break;
9417 case EXEC_BLOCK:
9418 resolve_block_construct (b);
9419 break;
9421 case EXEC_SELECT:
9422 case EXEC_SELECT_TYPE:
9423 case EXEC_FORALL:
9424 case EXEC_DO:
9425 case EXEC_DO_WHILE:
9426 case EXEC_DO_CONCURRENT:
9427 case EXEC_CRITICAL:
9428 case EXEC_READ:
9429 case EXEC_WRITE:
9430 case EXEC_IOLENGTH:
9431 case EXEC_WAIT:
9432 break;
9434 case EXEC_OACC_PARALLEL_LOOP:
9435 case EXEC_OACC_PARALLEL:
9436 case EXEC_OACC_KERNELS_LOOP:
9437 case EXEC_OACC_KERNELS:
9438 case EXEC_OACC_DATA:
9439 case EXEC_OACC_HOST_DATA:
9440 case EXEC_OACC_LOOP:
9441 case EXEC_OACC_UPDATE:
9442 case EXEC_OACC_WAIT:
9443 case EXEC_OACC_CACHE:
9444 case EXEC_OACC_ENTER_DATA:
9445 case EXEC_OACC_EXIT_DATA:
9446 case EXEC_OACC_ATOMIC:
9447 case EXEC_OACC_ROUTINE:
9448 case EXEC_OMP_ATOMIC:
9449 case EXEC_OMP_CRITICAL:
9450 case EXEC_OMP_DISTRIBUTE:
9451 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
9452 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
9453 case EXEC_OMP_DISTRIBUTE_SIMD:
9454 case EXEC_OMP_DO:
9455 case EXEC_OMP_DO_SIMD:
9456 case EXEC_OMP_MASTER:
9457 case EXEC_OMP_ORDERED:
9458 case EXEC_OMP_PARALLEL:
9459 case EXEC_OMP_PARALLEL_DO:
9460 case EXEC_OMP_PARALLEL_DO_SIMD:
9461 case EXEC_OMP_PARALLEL_SECTIONS:
9462 case EXEC_OMP_PARALLEL_WORKSHARE:
9463 case EXEC_OMP_SECTIONS:
9464 case EXEC_OMP_SIMD:
9465 case EXEC_OMP_SINGLE:
9466 case EXEC_OMP_TARGET:
9467 case EXEC_OMP_TARGET_DATA:
9468 case EXEC_OMP_TARGET_TEAMS:
9469 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
9470 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
9471 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9472 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
9473 case EXEC_OMP_TARGET_UPDATE:
9474 case EXEC_OMP_TASK:
9475 case EXEC_OMP_TASKGROUP:
9476 case EXEC_OMP_TASKWAIT:
9477 case EXEC_OMP_TASKYIELD:
9478 case EXEC_OMP_TEAMS:
9479 case EXEC_OMP_TEAMS_DISTRIBUTE:
9480 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
9481 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9482 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
9483 case EXEC_OMP_WORKSHARE:
9484 break;
9486 default:
9487 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
9490 gfc_resolve_code (b->next, ns);
9495 /* Does everything to resolve an ordinary assignment. Returns true
9496 if this is an interface assignment. */
9497 static bool
9498 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9500 bool rval = false;
9501 gfc_expr *lhs;
9502 gfc_expr *rhs;
9503 int llen = 0;
9504 int rlen = 0;
9505 int n;
9506 gfc_ref *ref;
9507 symbol_attribute attr;
9509 if (gfc_extend_assign (code, ns))
9511 gfc_expr** rhsptr;
9513 if (code->op == EXEC_ASSIGN_CALL)
9515 lhs = code->ext.actual->expr;
9516 rhsptr = &code->ext.actual->next->expr;
9518 else
9520 gfc_actual_arglist* args;
9521 gfc_typebound_proc* tbp;
9523 gcc_assert (code->op == EXEC_COMPCALL);
9525 args = code->expr1->value.compcall.actual;
9526 lhs = args->expr;
9527 rhsptr = &args->next->expr;
9529 tbp = code->expr1->value.compcall.tbp;
9530 gcc_assert (!tbp->is_generic);
9533 /* Make a temporary rhs when there is a default initializer
9534 and rhs is the same symbol as the lhs. */
9535 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9536 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9537 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9538 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9539 *rhsptr = gfc_get_parentheses (*rhsptr);
9541 return true;
9544 lhs = code->expr1;
9545 rhs = code->expr2;
9547 if (rhs->is_boz
9548 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9549 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9550 &code->loc))
9551 return false;
9553 /* Handle the case of a BOZ literal on the RHS. */
9554 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9556 int rc;
9557 if (warn_surprising)
9558 gfc_warning (OPT_Wsurprising,
9559 "BOZ literal at %L is bitwise transferred "
9560 "non-integer symbol %qs", &code->loc,
9561 lhs->symtree->n.sym->name);
9563 if (!gfc_convert_boz (rhs, &lhs->ts))
9564 return false;
9565 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9567 if (rc == ARITH_UNDERFLOW)
9568 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9569 ". This check can be disabled with the option "
9570 "%<-fno-range-check%>", &rhs->where);
9571 else if (rc == ARITH_OVERFLOW)
9572 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9573 ". This check can be disabled with the option "
9574 "%<-fno-range-check%>", &rhs->where);
9575 else if (rc == ARITH_NAN)
9576 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9577 ". This check can be disabled with the option "
9578 "%<-fno-range-check%>", &rhs->where);
9579 return false;
9583 if (lhs->ts.type == BT_CHARACTER
9584 && warn_character_truncation)
9586 if (lhs->ts.u.cl != NULL
9587 && lhs->ts.u.cl->length != NULL
9588 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9589 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9591 if (rhs->expr_type == EXPR_CONSTANT)
9592 rlen = rhs->value.character.length;
9594 else if (rhs->ts.u.cl != NULL
9595 && rhs->ts.u.cl->length != NULL
9596 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9597 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9599 if (rlen && llen && rlen > llen)
9600 gfc_warning_now (OPT_Wcharacter_truncation,
9601 "CHARACTER expression will be truncated "
9602 "in assignment (%d/%d) at %L",
9603 llen, rlen, &code->loc);
9606 /* Ensure that a vector index expression for the lvalue is evaluated
9607 to a temporary if the lvalue symbol is referenced in it. */
9608 if (lhs->rank)
9610 for (ref = lhs->ref; ref; ref= ref->next)
9611 if (ref->type == REF_ARRAY)
9613 for (n = 0; n < ref->u.ar.dimen; n++)
9614 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9615 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9616 ref->u.ar.start[n]))
9617 ref->u.ar.start[n]
9618 = gfc_get_parentheses (ref->u.ar.start[n]);
9622 if (gfc_pure (NULL))
9624 if (lhs->ts.type == BT_DERIVED
9625 && lhs->expr_type == EXPR_VARIABLE
9626 && lhs->ts.u.derived->attr.pointer_comp
9627 && rhs->expr_type == EXPR_VARIABLE
9628 && (gfc_impure_variable (rhs->symtree->n.sym)
9629 || gfc_is_coindexed (rhs)))
9631 /* F2008, C1283. */
9632 if (gfc_is_coindexed (rhs))
9633 gfc_error ("Coindexed expression at %L is assigned to "
9634 "a derived type variable with a POINTER "
9635 "component in a PURE procedure",
9636 &rhs->where);
9637 else
9638 gfc_error ("The impure variable at %L is assigned to "
9639 "a derived type variable with a POINTER "
9640 "component in a PURE procedure (12.6)",
9641 &rhs->where);
9642 return rval;
9645 /* Fortran 2008, C1283. */
9646 if (gfc_is_coindexed (lhs))
9648 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9649 "procedure", &rhs->where);
9650 return rval;
9654 if (gfc_implicit_pure (NULL))
9656 if (lhs->expr_type == EXPR_VARIABLE
9657 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9658 && lhs->symtree->n.sym->ns != gfc_current_ns)
9659 gfc_unset_implicit_pure (NULL);
9661 if (lhs->ts.type == BT_DERIVED
9662 && lhs->expr_type == EXPR_VARIABLE
9663 && lhs->ts.u.derived->attr.pointer_comp
9664 && rhs->expr_type == EXPR_VARIABLE
9665 && (gfc_impure_variable (rhs->symtree->n.sym)
9666 || gfc_is_coindexed (rhs)))
9667 gfc_unset_implicit_pure (NULL);
9669 /* Fortran 2008, C1283. */
9670 if (gfc_is_coindexed (lhs))
9671 gfc_unset_implicit_pure (NULL);
9674 /* F2008, 7.2.1.2. */
9675 attr = gfc_expr_attr (lhs);
9676 if (lhs->ts.type == BT_CLASS && attr.allocatable)
9678 if (attr.codimension)
9680 gfc_error ("Assignment to polymorphic coarray at %L is not "
9681 "permitted", &lhs->where);
9682 return false;
9684 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
9685 "polymorphic variable at %L", &lhs->where))
9686 return false;
9687 if (!flag_realloc_lhs)
9689 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9690 "requires %<-frealloc-lhs%>", &lhs->where);
9691 return false;
9693 /* See PR 43366. */
9694 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9695 "is not yet supported", &lhs->where);
9696 return false;
9698 else if (lhs->ts.type == BT_CLASS)
9700 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
9701 "assignment at %L - check that there is a matching specific "
9702 "subroutine for '=' operator", &lhs->where);
9703 return false;
9706 bool lhs_coindexed = gfc_is_coindexed (lhs);
9708 /* F2008, Section 7.2.1.2. */
9709 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
9711 gfc_error ("Coindexed variable must not have an allocatable ultimate "
9712 "component in assignment at %L", &lhs->where);
9713 return false;
9716 gfc_check_assign (lhs, rhs, 1);
9718 /* Assign the 'data' of a class object to a derived type. */
9719 if (lhs->ts.type == BT_DERIVED
9720 && rhs->ts.type == BT_CLASS)
9721 gfc_add_data_component (rhs);
9723 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
9724 Additionally, insert this code when the RHS is a CAF as we then use the
9725 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
9726 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
9727 noncoindexed array and the RHS is a coindexed scalar, use the normal code
9728 path. */
9729 if (flag_coarray == GFC_FCOARRAY_LIB
9730 && (lhs_coindexed
9731 || (code->expr2->expr_type == EXPR_FUNCTION
9732 && code->expr2->value.function.isym
9733 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
9734 && (code->expr1->rank == 0 || code->expr2->rank != 0)
9735 && !gfc_expr_attr (rhs).allocatable
9736 && !gfc_has_vector_subscript (rhs))))
9738 if (code->expr2->expr_type == EXPR_FUNCTION
9739 && code->expr2->value.function.isym
9740 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
9741 remove_caf_get_intrinsic (code->expr2);
9742 code->op = EXEC_CALL;
9743 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
9744 code->resolved_sym = code->symtree->n.sym;
9745 code->resolved_sym->attr.flavor = FL_PROCEDURE;
9746 code->resolved_sym->attr.intrinsic = 1;
9747 code->resolved_sym->attr.subroutine = 1;
9748 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
9749 gfc_commit_symbol (code->resolved_sym);
9750 code->ext.actual = gfc_get_actual_arglist ();
9751 code->ext.actual->expr = lhs;
9752 code->ext.actual->next = gfc_get_actual_arglist ();
9753 code->ext.actual->next->expr = rhs;
9754 code->expr1 = NULL;
9755 code->expr2 = NULL;
9758 return false;
9762 /* Add a component reference onto an expression. */
9764 static void
9765 add_comp_ref (gfc_expr *e, gfc_component *c)
9767 gfc_ref **ref;
9768 ref = &(e->ref);
9769 while (*ref)
9770 ref = &((*ref)->next);
9771 *ref = gfc_get_ref ();
9772 (*ref)->type = REF_COMPONENT;
9773 (*ref)->u.c.sym = e->ts.u.derived;
9774 (*ref)->u.c.component = c;
9775 e->ts = c->ts;
9777 /* Add a full array ref, as necessary. */
9778 if (c->as)
9780 gfc_add_full_array_ref (e, c->as);
9781 e->rank = c->as->rank;
9786 /* Build an assignment. Keep the argument 'op' for future use, so that
9787 pointer assignments can be made. */
9789 static gfc_code *
9790 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9791 gfc_component *comp1, gfc_component *comp2, locus loc)
9793 gfc_code *this_code;
9795 this_code = gfc_get_code (op);
9796 this_code->next = NULL;
9797 this_code->expr1 = gfc_copy_expr (expr1);
9798 this_code->expr2 = gfc_copy_expr (expr2);
9799 this_code->loc = loc;
9800 if (comp1 && comp2)
9802 add_comp_ref (this_code->expr1, comp1);
9803 add_comp_ref (this_code->expr2, comp2);
9806 return this_code;
9810 /* Makes a temporary variable expression based on the characteristics of
9811 a given variable expression. */
9813 static gfc_expr*
9814 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9816 static int serial = 0;
9817 char name[GFC_MAX_SYMBOL_LEN];
9818 gfc_symtree *tmp;
9819 gfc_array_spec *as;
9820 gfc_array_ref *aref;
9821 gfc_ref *ref;
9823 sprintf (name, GFC_PREFIX("DA%d"), serial++);
9824 gfc_get_sym_tree (name, ns, &tmp, false);
9825 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9827 as = NULL;
9828 ref = NULL;
9829 aref = NULL;
9831 /* Obtain the arrayspec for the temporary. */
9832 if (e->rank && e->expr_type != EXPR_ARRAY
9833 && e->expr_type != EXPR_FUNCTION
9834 && e->expr_type != EXPR_OP)
9836 aref = gfc_find_array_ref (e);
9837 if (e->expr_type == EXPR_VARIABLE
9838 && e->symtree->n.sym->as == aref->as)
9839 as = aref->as;
9840 else
9842 for (ref = e->ref; ref; ref = ref->next)
9843 if (ref->type == REF_COMPONENT
9844 && ref->u.c.component->as == aref->as)
9846 as = aref->as;
9847 break;
9852 /* Add the attributes and the arrayspec to the temporary. */
9853 tmp->n.sym->attr = gfc_expr_attr (e);
9854 tmp->n.sym->attr.function = 0;
9855 tmp->n.sym->attr.result = 0;
9856 tmp->n.sym->attr.flavor = FL_VARIABLE;
9858 if (as)
9860 tmp->n.sym->as = gfc_copy_array_spec (as);
9861 if (!ref)
9862 ref = e->ref;
9863 if (as->type == AS_DEFERRED)
9864 tmp->n.sym->attr.allocatable = 1;
9866 else if (e->rank && (e->expr_type == EXPR_ARRAY
9867 || e->expr_type == EXPR_FUNCTION
9868 || e->expr_type == EXPR_OP))
9870 tmp->n.sym->as = gfc_get_array_spec ();
9871 tmp->n.sym->as->type = AS_DEFERRED;
9872 tmp->n.sym->as->rank = e->rank;
9873 tmp->n.sym->attr.allocatable = 1;
9874 tmp->n.sym->attr.dimension = 1;
9876 else
9877 tmp->n.sym->attr.dimension = 0;
9879 gfc_set_sym_referenced (tmp->n.sym);
9880 gfc_commit_symbol (tmp->n.sym);
9881 e = gfc_lval_expr_from_sym (tmp->n.sym);
9883 /* Should the lhs be a section, use its array ref for the
9884 temporary expression. */
9885 if (aref && aref->type != AR_FULL)
9887 gfc_free_ref_list (e->ref);
9888 e->ref = gfc_copy_ref (ref);
9890 return e;
9894 /* Add one line of code to the code chain, making sure that 'head' and
9895 'tail' are appropriately updated. */
9897 static void
9898 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9900 gcc_assert (this_code);
9901 if (*head == NULL)
9902 *head = *tail = *this_code;
9903 else
9904 *tail = gfc_append_code (*tail, *this_code);
9905 *this_code = NULL;
9909 /* Counts the potential number of part array references that would
9910 result from resolution of typebound defined assignments. */
9912 static int
9913 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9915 gfc_component *c;
9916 int c_depth = 0, t_depth;
9918 for (c= derived->components; c; c = c->next)
9920 if ((!gfc_bt_struct (c->ts.type)
9921 || c->attr.pointer
9922 || c->attr.allocatable
9923 || c->attr.proc_pointer_comp
9924 || c->attr.class_pointer
9925 || c->attr.proc_pointer)
9926 && !c->attr.defined_assign_comp)
9927 continue;
9929 if (c->as && c_depth == 0)
9930 c_depth = 1;
9932 if (c->ts.u.derived->attr.defined_assign_comp)
9933 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9934 c->as ? 1 : 0);
9935 else
9936 t_depth = 0;
9938 c_depth = t_depth > c_depth ? t_depth : c_depth;
9940 return depth + c_depth;
9944 /* Implement 7.2.1.3 of the F08 standard:
9945 "An intrinsic assignment where the variable is of derived type is
9946 performed as if each component of the variable were assigned from the
9947 corresponding component of expr using pointer assignment (7.2.2) for
9948 each pointer component, defined assignment for each nonpointer
9949 nonallocatable component of a type that has a type-bound defined
9950 assignment consistent with the component, intrinsic assignment for
9951 each other nonpointer nonallocatable component, ..."
9953 The pointer assignments are taken care of by the intrinsic
9954 assignment of the structure itself. This function recursively adds
9955 defined assignments where required. The recursion is accomplished
9956 by calling gfc_resolve_code.
9958 When the lhs in a defined assignment has intent INOUT, we need a
9959 temporary for the lhs. In pseudo-code:
9961 ! Only call function lhs once.
9962 if (lhs is not a constant or an variable)
9963 temp_x = expr2
9964 expr2 => temp_x
9965 ! Do the intrinsic assignment
9966 expr1 = expr2
9967 ! Now do the defined assignments
9968 do over components with typebound defined assignment [%cmp]
9969 #if one component's assignment procedure is INOUT
9970 t1 = expr1
9971 #if expr2 non-variable
9972 temp_x = expr2
9973 expr2 => temp_x
9974 # endif
9975 expr1 = expr2
9976 # for each cmp
9977 t1%cmp {defined=} expr2%cmp
9978 expr1%cmp = t1%cmp
9979 #else
9980 expr1 = expr2
9982 # for each cmp
9983 expr1%cmp {defined=} expr2%cmp
9984 #endif
9987 /* The temporary assignments have to be put on top of the additional
9988 code to avoid the result being changed by the intrinsic assignment.
9990 static int component_assignment_level = 0;
9991 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
9993 static void
9994 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
9996 gfc_component *comp1, *comp2;
9997 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
9998 gfc_expr *t1;
9999 int error_count, depth;
10001 gfc_get_errors (NULL, &error_count);
10003 /* Filter out continuing processing after an error. */
10004 if (error_count
10005 || (*code)->expr1->ts.type != BT_DERIVED
10006 || (*code)->expr2->ts.type != BT_DERIVED)
10007 return;
10009 /* TODO: Handle more than one part array reference in assignments. */
10010 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
10011 (*code)->expr1->rank ? 1 : 0);
10012 if (depth > 1)
10014 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
10015 "done because multiple part array references would "
10016 "occur in intermediate expressions.", &(*code)->loc);
10017 return;
10020 component_assignment_level++;
10022 /* Create a temporary so that functions get called only once. */
10023 if ((*code)->expr2->expr_type != EXPR_VARIABLE
10024 && (*code)->expr2->expr_type != EXPR_CONSTANT)
10026 gfc_expr *tmp_expr;
10028 /* Assign the rhs to the temporary. */
10029 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
10030 this_code = build_assignment (EXEC_ASSIGN,
10031 tmp_expr, (*code)->expr2,
10032 NULL, NULL, (*code)->loc);
10033 /* Add the code and substitute the rhs expression. */
10034 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
10035 gfc_free_expr ((*code)->expr2);
10036 (*code)->expr2 = tmp_expr;
10039 /* Do the intrinsic assignment. This is not needed if the lhs is one
10040 of the temporaries generated here, since the intrinsic assignment
10041 to the final result already does this. */
10042 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
10044 this_code = build_assignment (EXEC_ASSIGN,
10045 (*code)->expr1, (*code)->expr2,
10046 NULL, NULL, (*code)->loc);
10047 add_code_to_chain (&this_code, &head, &tail);
10050 comp1 = (*code)->expr1->ts.u.derived->components;
10051 comp2 = (*code)->expr2->ts.u.derived->components;
10053 t1 = NULL;
10054 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
10056 bool inout = false;
10058 /* The intrinsic assignment does the right thing for pointers
10059 of all kinds and allocatable components. */
10060 if (!gfc_bt_struct (comp1->ts.type)
10061 || comp1->attr.pointer
10062 || comp1->attr.allocatable
10063 || comp1->attr.proc_pointer_comp
10064 || comp1->attr.class_pointer
10065 || comp1->attr.proc_pointer)
10066 continue;
10068 /* Make an assigment for this component. */
10069 this_code = build_assignment (EXEC_ASSIGN,
10070 (*code)->expr1, (*code)->expr2,
10071 comp1, comp2, (*code)->loc);
10073 /* Convert the assignment if there is a defined assignment for
10074 this type. Otherwise, using the call from gfc_resolve_code,
10075 recurse into its components. */
10076 gfc_resolve_code (this_code, ns);
10078 if (this_code->op == EXEC_ASSIGN_CALL)
10080 gfc_formal_arglist *dummy_args;
10081 gfc_symbol *rsym;
10082 /* Check that there is a typebound defined assignment. If not,
10083 then this must be a module defined assignment. We cannot
10084 use the defined_assign_comp attribute here because it must
10085 be this derived type that has the defined assignment and not
10086 a parent type. */
10087 if (!(comp1->ts.u.derived->f2k_derived
10088 && comp1->ts.u.derived->f2k_derived
10089 ->tb_op[INTRINSIC_ASSIGN]))
10091 gfc_free_statements (this_code);
10092 this_code = NULL;
10093 continue;
10096 /* If the first argument of the subroutine has intent INOUT
10097 a temporary must be generated and used instead. */
10098 rsym = this_code->resolved_sym;
10099 dummy_args = gfc_sym_get_dummy_args (rsym);
10100 if (dummy_args
10101 && dummy_args->sym->attr.intent == INTENT_INOUT)
10103 gfc_code *temp_code;
10104 inout = true;
10106 /* Build the temporary required for the assignment and put
10107 it at the head of the generated code. */
10108 if (!t1)
10110 t1 = get_temp_from_expr ((*code)->expr1, ns);
10111 temp_code = build_assignment (EXEC_ASSIGN,
10112 t1, (*code)->expr1,
10113 NULL, NULL, (*code)->loc);
10115 /* For allocatable LHS, check whether it is allocated. Note
10116 that allocatable components with defined assignment are
10117 not yet support. See PR 57696. */
10118 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
10120 gfc_code *block;
10121 gfc_expr *e =
10122 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10123 block = gfc_get_code (EXEC_IF);
10124 block->block = gfc_get_code (EXEC_IF);
10125 block->block->expr1
10126 = gfc_build_intrinsic_call (ns,
10127 GFC_ISYM_ALLOCATED, "allocated",
10128 (*code)->loc, 1, e);
10129 block->block->next = temp_code;
10130 temp_code = block;
10132 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
10135 /* Replace the first actual arg with the component of the
10136 temporary. */
10137 gfc_free_expr (this_code->ext.actual->expr);
10138 this_code->ext.actual->expr = gfc_copy_expr (t1);
10139 add_comp_ref (this_code->ext.actual->expr, comp1);
10141 /* If the LHS variable is allocatable and wasn't allocated and
10142 the temporary is allocatable, pointer assign the address of
10143 the freshly allocated LHS to the temporary. */
10144 if ((*code)->expr1->symtree->n.sym->attr.allocatable
10145 && gfc_expr_attr ((*code)->expr1).allocatable)
10147 gfc_code *block;
10148 gfc_expr *cond;
10150 cond = gfc_get_expr ();
10151 cond->ts.type = BT_LOGICAL;
10152 cond->ts.kind = gfc_default_logical_kind;
10153 cond->expr_type = EXPR_OP;
10154 cond->where = (*code)->loc;
10155 cond->value.op.op = INTRINSIC_NOT;
10156 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
10157 GFC_ISYM_ALLOCATED, "allocated",
10158 (*code)->loc, 1, gfc_copy_expr (t1));
10159 block = gfc_get_code (EXEC_IF);
10160 block->block = gfc_get_code (EXEC_IF);
10161 block->block->expr1 = cond;
10162 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10163 t1, (*code)->expr1,
10164 NULL, NULL, (*code)->loc);
10165 add_code_to_chain (&block, &head, &tail);
10169 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
10171 /* Don't add intrinsic assignments since they are already
10172 effected by the intrinsic assignment of the structure. */
10173 gfc_free_statements (this_code);
10174 this_code = NULL;
10175 continue;
10178 add_code_to_chain (&this_code, &head, &tail);
10180 if (t1 && inout)
10182 /* Transfer the value to the final result. */
10183 this_code = build_assignment (EXEC_ASSIGN,
10184 (*code)->expr1, t1,
10185 comp1, comp2, (*code)->loc);
10186 add_code_to_chain (&this_code, &head, &tail);
10190 /* Put the temporary assignments at the top of the generated code. */
10191 if (tmp_head && component_assignment_level == 1)
10193 gfc_append_code (tmp_head, head);
10194 head = tmp_head;
10195 tmp_head = tmp_tail = NULL;
10198 // If we did a pointer assignment - thus, we need to ensure that the LHS is
10199 // not accidentally deallocated. Hence, nullify t1.
10200 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
10201 && gfc_expr_attr ((*code)->expr1).allocatable)
10203 gfc_code *block;
10204 gfc_expr *cond;
10205 gfc_expr *e;
10207 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10208 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
10209 (*code)->loc, 2, gfc_copy_expr (t1), e);
10210 block = gfc_get_code (EXEC_IF);
10211 block->block = gfc_get_code (EXEC_IF);
10212 block->block->expr1 = cond;
10213 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10214 t1, gfc_get_null_expr (&(*code)->loc),
10215 NULL, NULL, (*code)->loc);
10216 gfc_append_code (tail, block);
10217 tail = block;
10220 /* Now attach the remaining code chain to the input code. Step on
10221 to the end of the new code since resolution is complete. */
10222 gcc_assert ((*code)->op == EXEC_ASSIGN);
10223 tail->next = (*code)->next;
10224 /* Overwrite 'code' because this would place the intrinsic assignment
10225 before the temporary for the lhs is created. */
10226 gfc_free_expr ((*code)->expr1);
10227 gfc_free_expr ((*code)->expr2);
10228 **code = *head;
10229 if (head != tail)
10230 free (head);
10231 *code = tail;
10233 component_assignment_level--;
10237 /* F2008: Pointer function assignments are of the form:
10238 ptr_fcn (args) = expr
10239 This function breaks these assignments into two statements:
10240 temporary_pointer => ptr_fcn(args)
10241 temporary_pointer = expr */
10243 static bool
10244 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
10246 gfc_expr *tmp_ptr_expr;
10247 gfc_code *this_code;
10248 gfc_component *comp;
10249 gfc_symbol *s;
10251 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
10252 return false;
10254 /* Even if standard does not support this feature, continue to build
10255 the two statements to avoid upsetting frontend_passes.c. */
10256 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
10257 "%L", &(*code)->loc);
10259 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
10261 if (comp)
10262 s = comp->ts.interface;
10263 else
10264 s = (*code)->expr1->symtree->n.sym;
10266 if (s == NULL || !s->result->attr.pointer)
10268 gfc_error ("The function result on the lhs of the assignment at "
10269 "%L must have the pointer attribute.",
10270 &(*code)->expr1->where);
10271 (*code)->op = EXEC_NOP;
10272 return false;
10275 tmp_ptr_expr = get_temp_from_expr ((*code)->expr2, ns);
10277 /* get_temp_from_expression is set up for ordinary assignments. To that
10278 end, where array bounds are not known, arrays are made allocatable.
10279 Change the temporary to a pointer here. */
10280 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
10281 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
10282 tmp_ptr_expr->where = (*code)->loc;
10284 this_code = build_assignment (EXEC_ASSIGN,
10285 tmp_ptr_expr, (*code)->expr2,
10286 NULL, NULL, (*code)->loc);
10287 this_code->next = (*code)->next;
10288 (*code)->next = this_code;
10289 (*code)->op = EXEC_POINTER_ASSIGN;
10290 (*code)->expr2 = (*code)->expr1;
10291 (*code)->expr1 = tmp_ptr_expr;
10293 return true;
10297 /* Deferred character length assignments from an operator expression
10298 require a temporary because the character length of the lhs can
10299 change in the course of the assignment. */
10301 static bool
10302 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
10304 gfc_expr *tmp_expr;
10305 gfc_code *this_code;
10307 if (!((*code)->expr1->ts.type == BT_CHARACTER
10308 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
10309 && (*code)->expr2->expr_type == EXPR_OP))
10310 return false;
10312 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
10313 return false;
10315 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
10316 tmp_expr->where = (*code)->loc;
10318 /* A new charlen is required to ensure that the variable string
10319 length is different to that of the original lhs. */
10320 tmp_expr->ts.u.cl = gfc_get_charlen();
10321 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
10322 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
10323 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
10325 tmp_expr->symtree->n.sym->ts.deferred = 1;
10327 this_code = build_assignment (EXEC_ASSIGN,
10328 (*code)->expr1,
10329 gfc_copy_expr (tmp_expr),
10330 NULL, NULL, (*code)->loc);
10332 (*code)->expr1 = tmp_expr;
10334 this_code->next = (*code)->next;
10335 (*code)->next = this_code;
10337 return true;
10341 /* Given a block of code, recursively resolve everything pointed to by this
10342 code block. */
10344 void
10345 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
10347 int omp_workshare_save;
10348 int forall_save, do_concurrent_save;
10349 code_stack frame;
10350 bool t;
10352 frame.prev = cs_base;
10353 frame.head = code;
10354 cs_base = &frame;
10356 find_reachable_labels (code);
10358 for (; code; code = code->next)
10360 frame.current = code;
10361 forall_save = forall_flag;
10362 do_concurrent_save = gfc_do_concurrent_flag;
10364 if (code->op == EXEC_FORALL)
10366 forall_flag = 1;
10367 gfc_resolve_forall (code, ns, forall_save);
10368 forall_flag = 2;
10370 else if (code->block)
10372 omp_workshare_save = -1;
10373 switch (code->op)
10375 case EXEC_OACC_PARALLEL_LOOP:
10376 case EXEC_OACC_PARALLEL:
10377 case EXEC_OACC_KERNELS_LOOP:
10378 case EXEC_OACC_KERNELS:
10379 case EXEC_OACC_DATA:
10380 case EXEC_OACC_HOST_DATA:
10381 case EXEC_OACC_LOOP:
10382 gfc_resolve_oacc_blocks (code, ns);
10383 break;
10384 case EXEC_OMP_PARALLEL_WORKSHARE:
10385 omp_workshare_save = omp_workshare_flag;
10386 omp_workshare_flag = 1;
10387 gfc_resolve_omp_parallel_blocks (code, ns);
10388 break;
10389 case EXEC_OMP_PARALLEL:
10390 case EXEC_OMP_PARALLEL_DO:
10391 case EXEC_OMP_PARALLEL_DO_SIMD:
10392 case EXEC_OMP_PARALLEL_SECTIONS:
10393 case EXEC_OMP_TARGET_TEAMS:
10394 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10395 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10396 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10397 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10398 case EXEC_OMP_TASK:
10399 case EXEC_OMP_TEAMS:
10400 case EXEC_OMP_TEAMS_DISTRIBUTE:
10401 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10402 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10403 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10404 omp_workshare_save = omp_workshare_flag;
10405 omp_workshare_flag = 0;
10406 gfc_resolve_omp_parallel_blocks (code, ns);
10407 break;
10408 case EXEC_OMP_DISTRIBUTE:
10409 case EXEC_OMP_DISTRIBUTE_SIMD:
10410 case EXEC_OMP_DO:
10411 case EXEC_OMP_DO_SIMD:
10412 case EXEC_OMP_SIMD:
10413 gfc_resolve_omp_do_blocks (code, ns);
10414 break;
10415 case EXEC_SELECT_TYPE:
10416 /* Blocks are handled in resolve_select_type because we have
10417 to transform the SELECT TYPE into ASSOCIATE first. */
10418 break;
10419 case EXEC_DO_CONCURRENT:
10420 gfc_do_concurrent_flag = 1;
10421 gfc_resolve_blocks (code->block, ns);
10422 gfc_do_concurrent_flag = 2;
10423 break;
10424 case EXEC_OMP_WORKSHARE:
10425 omp_workshare_save = omp_workshare_flag;
10426 omp_workshare_flag = 1;
10427 /* FALL THROUGH */
10428 default:
10429 gfc_resolve_blocks (code->block, ns);
10430 break;
10433 if (omp_workshare_save != -1)
10434 omp_workshare_flag = omp_workshare_save;
10436 start:
10437 t = true;
10438 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
10439 t = gfc_resolve_expr (code->expr1);
10440 forall_flag = forall_save;
10441 gfc_do_concurrent_flag = do_concurrent_save;
10443 if (!gfc_resolve_expr (code->expr2))
10444 t = false;
10446 if (code->op == EXEC_ALLOCATE
10447 && !gfc_resolve_expr (code->expr3))
10448 t = false;
10450 switch (code->op)
10452 case EXEC_NOP:
10453 case EXEC_END_BLOCK:
10454 case EXEC_END_NESTED_BLOCK:
10455 case EXEC_CYCLE:
10456 case EXEC_PAUSE:
10457 case EXEC_STOP:
10458 case EXEC_ERROR_STOP:
10459 case EXEC_EXIT:
10460 case EXEC_CONTINUE:
10461 case EXEC_DT_END:
10462 case EXEC_ASSIGN_CALL:
10463 break;
10465 case EXEC_CRITICAL:
10466 resolve_critical (code);
10467 break;
10469 case EXEC_SYNC_ALL:
10470 case EXEC_SYNC_IMAGES:
10471 case EXEC_SYNC_MEMORY:
10472 resolve_sync (code);
10473 break;
10475 case EXEC_LOCK:
10476 case EXEC_UNLOCK:
10477 case EXEC_EVENT_POST:
10478 case EXEC_EVENT_WAIT:
10479 resolve_lock_unlock_event (code);
10480 break;
10482 case EXEC_ENTRY:
10483 /* Keep track of which entry we are up to. */
10484 current_entry_id = code->ext.entry->id;
10485 break;
10487 case EXEC_WHERE:
10488 resolve_where (code, NULL);
10489 break;
10491 case EXEC_GOTO:
10492 if (code->expr1 != NULL)
10494 if (code->expr1->ts.type != BT_INTEGER)
10495 gfc_error ("ASSIGNED GOTO statement at %L requires an "
10496 "INTEGER variable", &code->expr1->where);
10497 else if (code->expr1->symtree->n.sym->attr.assign != 1)
10498 gfc_error ("Variable %qs has not been assigned a target "
10499 "label at %L", code->expr1->symtree->n.sym->name,
10500 &code->expr1->where);
10502 else
10503 resolve_branch (code->label1, code);
10504 break;
10506 case EXEC_RETURN:
10507 if (code->expr1 != NULL
10508 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
10509 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
10510 "INTEGER return specifier", &code->expr1->where);
10511 break;
10513 case EXEC_INIT_ASSIGN:
10514 case EXEC_END_PROCEDURE:
10515 break;
10517 case EXEC_ASSIGN:
10518 if (!t)
10519 break;
10521 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
10522 the LHS. */
10523 if (code->expr1->expr_type == EXPR_FUNCTION
10524 && code->expr1->value.function.isym
10525 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10526 remove_caf_get_intrinsic (code->expr1);
10528 /* If this is a pointer function in an lvalue variable context,
10529 the new code will have to be resolved afresh. This is also the
10530 case with an error, where the code is transformed into NOP to
10531 prevent ICEs downstream. */
10532 if (resolve_ptr_fcn_assign (&code, ns)
10533 || code->op == EXEC_NOP)
10534 goto start;
10536 if (!gfc_check_vardef_context (code->expr1, false, false, false,
10537 _("assignment")))
10538 break;
10540 if (resolve_ordinary_assign (code, ns))
10542 if (code->op == EXEC_COMPCALL)
10543 goto compcall;
10544 else
10545 goto call;
10548 /* Check for dependencies in deferred character length array
10549 assignments and generate a temporary, if necessary. */
10550 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
10551 break;
10553 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
10554 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
10555 && code->expr1->ts.u.derived
10556 && code->expr1->ts.u.derived->attr.defined_assign_comp)
10557 generate_component_assignments (&code, ns);
10559 break;
10561 case EXEC_LABEL_ASSIGN:
10562 if (code->label1->defined == ST_LABEL_UNKNOWN)
10563 gfc_error ("Label %d referenced at %L is never defined",
10564 code->label1->value, &code->label1->where);
10565 if (t
10566 && (code->expr1->expr_type != EXPR_VARIABLE
10567 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
10568 || code->expr1->symtree->n.sym->ts.kind
10569 != gfc_default_integer_kind
10570 || code->expr1->symtree->n.sym->as != NULL))
10571 gfc_error ("ASSIGN statement at %L requires a scalar "
10572 "default INTEGER variable", &code->expr1->where);
10573 break;
10575 case EXEC_POINTER_ASSIGN:
10577 gfc_expr* e;
10579 if (!t)
10580 break;
10582 /* This is both a variable definition and pointer assignment
10583 context, so check both of them. For rank remapping, a final
10584 array ref may be present on the LHS and fool gfc_expr_attr
10585 used in gfc_check_vardef_context. Remove it. */
10586 e = remove_last_array_ref (code->expr1);
10587 t = gfc_check_vardef_context (e, true, false, false,
10588 _("pointer assignment"));
10589 if (t)
10590 t = gfc_check_vardef_context (e, false, false, false,
10591 _("pointer assignment"));
10592 gfc_free_expr (e);
10593 if (!t)
10594 break;
10596 gfc_check_pointer_assign (code->expr1, code->expr2);
10597 break;
10600 case EXEC_ARITHMETIC_IF:
10602 gfc_expr *e = code->expr1;
10604 gfc_resolve_expr (e);
10605 if (e->expr_type == EXPR_NULL)
10606 gfc_error ("Invalid NULL at %L", &e->where);
10608 if (t && (e->rank > 0
10609 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
10610 gfc_error ("Arithmetic IF statement at %L requires a scalar "
10611 "REAL or INTEGER expression", &e->where);
10613 resolve_branch (code->label1, code);
10614 resolve_branch (code->label2, code);
10615 resolve_branch (code->label3, code);
10617 break;
10619 case EXEC_IF:
10620 if (t && code->expr1 != NULL
10621 && (code->expr1->ts.type != BT_LOGICAL
10622 || code->expr1->rank != 0))
10623 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10624 &code->expr1->where);
10625 break;
10627 case EXEC_CALL:
10628 call:
10629 resolve_call (code);
10630 break;
10632 case EXEC_COMPCALL:
10633 compcall:
10634 resolve_typebound_subroutine (code);
10635 break;
10637 case EXEC_CALL_PPC:
10638 resolve_ppc_call (code);
10639 break;
10641 case EXEC_SELECT:
10642 /* Select is complicated. Also, a SELECT construct could be
10643 a transformed computed GOTO. */
10644 resolve_select (code, false);
10645 break;
10647 case EXEC_SELECT_TYPE:
10648 resolve_select_type (code, ns);
10649 break;
10651 case EXEC_BLOCK:
10652 resolve_block_construct (code);
10653 break;
10655 case EXEC_DO:
10656 if (code->ext.iterator != NULL)
10658 gfc_iterator *iter = code->ext.iterator;
10659 if (gfc_resolve_iterator (iter, true, false))
10660 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
10662 break;
10664 case EXEC_DO_WHILE:
10665 if (code->expr1 == NULL)
10666 gfc_internal_error ("gfc_resolve_code(): No expression on "
10667 "DO WHILE");
10668 if (t
10669 && (code->expr1->rank != 0
10670 || code->expr1->ts.type != BT_LOGICAL))
10671 gfc_error ("Exit condition of DO WHILE loop at %L must be "
10672 "a scalar LOGICAL expression", &code->expr1->where);
10673 break;
10675 case EXEC_ALLOCATE:
10676 if (t)
10677 resolve_allocate_deallocate (code, "ALLOCATE");
10679 break;
10681 case EXEC_DEALLOCATE:
10682 if (t)
10683 resolve_allocate_deallocate (code, "DEALLOCATE");
10685 break;
10687 case EXEC_OPEN:
10688 if (!gfc_resolve_open (code->ext.open))
10689 break;
10691 resolve_branch (code->ext.open->err, code);
10692 break;
10694 case EXEC_CLOSE:
10695 if (!gfc_resolve_close (code->ext.close))
10696 break;
10698 resolve_branch (code->ext.close->err, code);
10699 break;
10701 case EXEC_BACKSPACE:
10702 case EXEC_ENDFILE:
10703 case EXEC_REWIND:
10704 case EXEC_FLUSH:
10705 if (!gfc_resolve_filepos (code->ext.filepos))
10706 break;
10708 resolve_branch (code->ext.filepos->err, code);
10709 break;
10711 case EXEC_INQUIRE:
10712 if (!gfc_resolve_inquire (code->ext.inquire))
10713 break;
10715 resolve_branch (code->ext.inquire->err, code);
10716 break;
10718 case EXEC_IOLENGTH:
10719 gcc_assert (code->ext.inquire != NULL);
10720 if (!gfc_resolve_inquire (code->ext.inquire))
10721 break;
10723 resolve_branch (code->ext.inquire->err, code);
10724 break;
10726 case EXEC_WAIT:
10727 if (!gfc_resolve_wait (code->ext.wait))
10728 break;
10730 resolve_branch (code->ext.wait->err, code);
10731 resolve_branch (code->ext.wait->end, code);
10732 resolve_branch (code->ext.wait->eor, code);
10733 break;
10735 case EXEC_READ:
10736 case EXEC_WRITE:
10737 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
10738 break;
10740 resolve_branch (code->ext.dt->err, code);
10741 resolve_branch (code->ext.dt->end, code);
10742 resolve_branch (code->ext.dt->eor, code);
10743 break;
10745 case EXEC_TRANSFER:
10746 resolve_transfer (code);
10747 break;
10749 case EXEC_DO_CONCURRENT:
10750 case EXEC_FORALL:
10751 resolve_forall_iterators (code->ext.forall_iterator);
10753 if (code->expr1 != NULL
10754 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
10755 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
10756 "expression", &code->expr1->where);
10757 break;
10759 case EXEC_OACC_PARALLEL_LOOP:
10760 case EXEC_OACC_PARALLEL:
10761 case EXEC_OACC_KERNELS_LOOP:
10762 case EXEC_OACC_KERNELS:
10763 case EXEC_OACC_DATA:
10764 case EXEC_OACC_HOST_DATA:
10765 case EXEC_OACC_LOOP:
10766 case EXEC_OACC_UPDATE:
10767 case EXEC_OACC_WAIT:
10768 case EXEC_OACC_CACHE:
10769 case EXEC_OACC_ENTER_DATA:
10770 case EXEC_OACC_EXIT_DATA:
10771 case EXEC_OACC_ATOMIC:
10772 case EXEC_OACC_DECLARE:
10773 gfc_resolve_oacc_directive (code, ns);
10774 break;
10776 case EXEC_OMP_ATOMIC:
10777 case EXEC_OMP_BARRIER:
10778 case EXEC_OMP_CANCEL:
10779 case EXEC_OMP_CANCELLATION_POINT:
10780 case EXEC_OMP_CRITICAL:
10781 case EXEC_OMP_FLUSH:
10782 case EXEC_OMP_DISTRIBUTE:
10783 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10784 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10785 case EXEC_OMP_DISTRIBUTE_SIMD:
10786 case EXEC_OMP_DO:
10787 case EXEC_OMP_DO_SIMD:
10788 case EXEC_OMP_MASTER:
10789 case EXEC_OMP_ORDERED:
10790 case EXEC_OMP_SECTIONS:
10791 case EXEC_OMP_SIMD:
10792 case EXEC_OMP_SINGLE:
10793 case EXEC_OMP_TARGET:
10794 case EXEC_OMP_TARGET_DATA:
10795 case EXEC_OMP_TARGET_TEAMS:
10796 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10797 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10798 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10799 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10800 case EXEC_OMP_TARGET_UPDATE:
10801 case EXEC_OMP_TASK:
10802 case EXEC_OMP_TASKGROUP:
10803 case EXEC_OMP_TASKWAIT:
10804 case EXEC_OMP_TASKYIELD:
10805 case EXEC_OMP_TEAMS:
10806 case EXEC_OMP_TEAMS_DISTRIBUTE:
10807 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10808 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10809 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10810 case EXEC_OMP_WORKSHARE:
10811 gfc_resolve_omp_directive (code, ns);
10812 break;
10814 case EXEC_OMP_PARALLEL:
10815 case EXEC_OMP_PARALLEL_DO:
10816 case EXEC_OMP_PARALLEL_DO_SIMD:
10817 case EXEC_OMP_PARALLEL_SECTIONS:
10818 case EXEC_OMP_PARALLEL_WORKSHARE:
10819 omp_workshare_save = omp_workshare_flag;
10820 omp_workshare_flag = 0;
10821 gfc_resolve_omp_directive (code, ns);
10822 omp_workshare_flag = omp_workshare_save;
10823 break;
10825 default:
10826 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
10830 cs_base = frame.prev;
10834 /* Resolve initial values and make sure they are compatible with
10835 the variable. */
10837 static void
10838 resolve_values (gfc_symbol *sym)
10840 bool t;
10842 if (sym->value == NULL)
10843 return;
10845 if (sym->value->expr_type == EXPR_STRUCTURE)
10846 t= resolve_structure_cons (sym->value, 1);
10847 else
10848 t = gfc_resolve_expr (sym->value);
10850 if (!t)
10851 return;
10853 gfc_check_assign_symbol (sym, NULL, sym->value);
10857 /* Verify any BIND(C) derived types in the namespace so we can report errors
10858 for them once, rather than for each variable declared of that type. */
10860 static void
10861 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10863 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10864 && derived_sym->attr.is_bind_c == 1)
10865 verify_bind_c_derived_type (derived_sym);
10867 return;
10871 /* Verify that any binding labels used in a given namespace do not collide
10872 with the names or binding labels of any global symbols. Multiple INTERFACE
10873 for the same procedure are permitted. */
10875 static void
10876 gfc_verify_binding_labels (gfc_symbol *sym)
10878 gfc_gsymbol *gsym;
10879 const char *module;
10881 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
10882 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
10883 return;
10885 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10887 if (sym->module)
10888 module = sym->module;
10889 else if (sym->ns && sym->ns->proc_name
10890 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10891 module = sym->ns->proc_name->name;
10892 else if (sym->ns && sym->ns->parent
10893 && sym->ns && sym->ns->parent->proc_name
10894 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10895 module = sym->ns->parent->proc_name->name;
10896 else
10897 module = NULL;
10899 if (!gsym
10900 || (!gsym->defined
10901 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
10903 if (!gsym)
10904 gsym = gfc_get_gsymbol (sym->binding_label);
10905 gsym->where = sym->declared_at;
10906 gsym->sym_name = sym->name;
10907 gsym->binding_label = sym->binding_label;
10908 gsym->ns = sym->ns;
10909 gsym->mod_name = module;
10910 if (sym->attr.function)
10911 gsym->type = GSYM_FUNCTION;
10912 else if (sym->attr.subroutine)
10913 gsym->type = GSYM_SUBROUTINE;
10914 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
10915 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
10916 return;
10919 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
10921 gfc_error ("Variable %s with binding label %s at %L uses the same global "
10922 "identifier as entity at %L", sym->name,
10923 sym->binding_label, &sym->declared_at, &gsym->where);
10924 /* Clear the binding label to prevent checking multiple times. */
10925 sym->binding_label = NULL;
10928 else if (sym->attr.flavor == FL_VARIABLE && module
10929 && (strcmp (module, gsym->mod_name) != 0
10930 || strcmp (sym->name, gsym->sym_name) != 0))
10932 /* This can only happen if the variable is defined in a module - if it
10933 isn't the same module, reject it. */
10934 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
10935 "the same global identifier as entity at %L from module %s",
10936 sym->name, module, sym->binding_label,
10937 &sym->declared_at, &gsym->where, gsym->mod_name);
10938 sym->binding_label = NULL;
10940 else if ((sym->attr.function || sym->attr.subroutine)
10941 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
10942 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
10943 && sym != gsym->ns->proc_name
10944 && (module != gsym->mod_name
10945 || strcmp (gsym->sym_name, sym->name) != 0
10946 || (module && strcmp (module, gsym->mod_name) != 0)))
10948 /* Print an error if the procedure is defined multiple times; we have to
10949 exclude references to the same procedure via module association or
10950 multiple checks for the same procedure. */
10951 gfc_error ("Procedure %s with binding label %s at %L uses the same "
10952 "global identifier as entity at %L", sym->name,
10953 sym->binding_label, &sym->declared_at, &gsym->where);
10954 sym->binding_label = NULL;
10959 /* Resolve an index expression. */
10961 static bool
10962 resolve_index_expr (gfc_expr *e)
10964 if (!gfc_resolve_expr (e))
10965 return false;
10967 if (!gfc_simplify_expr (e, 0))
10968 return false;
10970 if (!gfc_specification_expr (e))
10971 return false;
10973 return true;
10977 /* Resolve a charlen structure. */
10979 static bool
10980 resolve_charlen (gfc_charlen *cl)
10982 int i, k;
10983 bool saved_specification_expr;
10985 if (cl->resolved)
10986 return true;
10988 cl->resolved = 1;
10989 saved_specification_expr = specification_expr;
10990 specification_expr = true;
10992 if (cl->length_from_typespec)
10994 if (!gfc_resolve_expr (cl->length))
10996 specification_expr = saved_specification_expr;
10997 return false;
11000 if (!gfc_simplify_expr (cl->length, 0))
11002 specification_expr = saved_specification_expr;
11003 return false;
11006 else
11009 if (!resolve_index_expr (cl->length))
11011 specification_expr = saved_specification_expr;
11012 return false;
11016 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
11017 a negative value, the length of character entities declared is zero. */
11018 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
11019 gfc_replace_expr (cl->length,
11020 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
11022 /* Check that the character length is not too large. */
11023 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
11024 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
11025 && cl->length->ts.type == BT_INTEGER
11026 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
11028 gfc_error ("String length at %L is too large", &cl->length->where);
11029 specification_expr = saved_specification_expr;
11030 return false;
11033 specification_expr = saved_specification_expr;
11034 return true;
11038 /* Test for non-constant shape arrays. */
11040 static bool
11041 is_non_constant_shape_array (gfc_symbol *sym)
11043 gfc_expr *e;
11044 int i;
11045 bool not_constant;
11047 not_constant = false;
11048 if (sym->as != NULL)
11050 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
11051 has not been simplified; parameter array references. Do the
11052 simplification now. */
11053 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
11055 e = sym->as->lower[i];
11056 if (e && (!resolve_index_expr(e)
11057 || !gfc_is_constant_expr (e)))
11058 not_constant = true;
11059 e = sym->as->upper[i];
11060 if (e && (!resolve_index_expr(e)
11061 || !gfc_is_constant_expr (e)))
11062 not_constant = true;
11065 return not_constant;
11068 /* Given a symbol and an initialization expression, add code to initialize
11069 the symbol to the function entry. */
11070 static void
11071 build_init_assign (gfc_symbol *sym, gfc_expr *init)
11073 gfc_expr *lval;
11074 gfc_code *init_st;
11075 gfc_namespace *ns = sym->ns;
11077 /* Search for the function namespace if this is a contained
11078 function without an explicit result. */
11079 if (sym->attr.function && sym == sym->result
11080 && sym->name != sym->ns->proc_name->name)
11082 ns = ns->contained;
11083 for (;ns; ns = ns->sibling)
11084 if (strcmp (ns->proc_name->name, sym->name) == 0)
11085 break;
11088 if (ns == NULL)
11090 gfc_free_expr (init);
11091 return;
11094 /* Build an l-value expression for the result. */
11095 lval = gfc_lval_expr_from_sym (sym);
11097 /* Add the code at scope entry. */
11098 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
11099 init_st->next = ns->code;
11100 ns->code = init_st;
11102 /* Assign the default initializer to the l-value. */
11103 init_st->loc = sym->declared_at;
11104 init_st->expr1 = lval;
11105 init_st->expr2 = init;
11108 /* Assign the default initializer to a derived type variable or result. */
11110 static void
11111 apply_default_init (gfc_symbol *sym)
11113 gfc_expr *init = NULL;
11115 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11116 return;
11118 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
11119 init = gfc_default_initializer (&sym->ts);
11121 if (init == NULL && sym->ts.type != BT_CLASS)
11122 return;
11124 build_init_assign (sym, init);
11125 sym->attr.referenced = 1;
11128 /* Build an initializer for a local integer, real, complex, logical, or
11129 character variable, based on the command line flags finit-local-zero,
11130 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
11131 null if the symbol should not have a default initialization. */
11132 static gfc_expr *
11133 build_default_init_expr (gfc_symbol *sym)
11135 int char_len;
11136 gfc_expr *init_expr;
11137 int i;
11139 /* These symbols should never have a default initialization. */
11140 if (sym->attr.allocatable
11141 || sym->attr.external
11142 || sym->attr.dummy
11143 || sym->attr.pointer
11144 || sym->attr.in_equivalence
11145 || sym->attr.in_common
11146 || sym->attr.data
11147 || sym->module
11148 || sym->attr.cray_pointee
11149 || sym->attr.cray_pointer
11150 || sym->assoc)
11151 return NULL;
11153 /* Now we'll try to build an initializer expression. */
11154 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
11155 &sym->declared_at);
11157 /* We will only initialize integers, reals, complex, logicals, and
11158 characters, and only if the corresponding command-line flags
11159 were set. Otherwise, we free init_expr and return null. */
11160 switch (sym->ts.type)
11162 case BT_INTEGER:
11163 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
11164 mpz_set_si (init_expr->value.integer,
11165 gfc_option.flag_init_integer_value);
11166 else
11168 gfc_free_expr (init_expr);
11169 init_expr = NULL;
11171 break;
11173 case BT_REAL:
11174 switch (flag_init_real)
11176 case GFC_INIT_REAL_SNAN:
11177 init_expr->is_snan = 1;
11178 /* Fall through. */
11179 case GFC_INIT_REAL_NAN:
11180 mpfr_set_nan (init_expr->value.real);
11181 break;
11183 case GFC_INIT_REAL_INF:
11184 mpfr_set_inf (init_expr->value.real, 1);
11185 break;
11187 case GFC_INIT_REAL_NEG_INF:
11188 mpfr_set_inf (init_expr->value.real, -1);
11189 break;
11191 case GFC_INIT_REAL_ZERO:
11192 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
11193 break;
11195 default:
11196 gfc_free_expr (init_expr);
11197 init_expr = NULL;
11198 break;
11200 break;
11202 case BT_COMPLEX:
11203 switch (flag_init_real)
11205 case GFC_INIT_REAL_SNAN:
11206 init_expr->is_snan = 1;
11207 /* Fall through. */
11208 case GFC_INIT_REAL_NAN:
11209 mpfr_set_nan (mpc_realref (init_expr->value.complex));
11210 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
11211 break;
11213 case GFC_INIT_REAL_INF:
11214 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
11215 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
11216 break;
11218 case GFC_INIT_REAL_NEG_INF:
11219 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
11220 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
11221 break;
11223 case GFC_INIT_REAL_ZERO:
11224 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
11225 break;
11227 default:
11228 gfc_free_expr (init_expr);
11229 init_expr = NULL;
11230 break;
11232 break;
11234 case BT_LOGICAL:
11235 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
11236 init_expr->value.logical = 0;
11237 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
11238 init_expr->value.logical = 1;
11239 else
11241 gfc_free_expr (init_expr);
11242 init_expr = NULL;
11244 break;
11246 case BT_CHARACTER:
11247 /* For characters, the length must be constant in order to
11248 create a default initializer. */
11249 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
11250 && sym->ts.u.cl->length
11251 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11253 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
11254 init_expr->value.character.length = char_len;
11255 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
11256 for (i = 0; i < char_len; i++)
11257 init_expr->value.character.string[i]
11258 = (unsigned char) gfc_option.flag_init_character_value;
11260 else
11262 gfc_free_expr (init_expr);
11263 init_expr = NULL;
11265 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
11266 && sym->ts.u.cl->length && flag_max_stack_var_size != 0)
11268 gfc_actual_arglist *arg;
11269 init_expr = gfc_get_expr ();
11270 init_expr->where = sym->declared_at;
11271 init_expr->ts = sym->ts;
11272 init_expr->expr_type = EXPR_FUNCTION;
11273 init_expr->value.function.isym =
11274 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
11275 init_expr->value.function.name = "repeat";
11276 arg = gfc_get_actual_arglist ();
11277 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
11278 NULL, 1);
11279 arg->expr->value.character.string[0]
11280 = gfc_option.flag_init_character_value;
11281 arg->next = gfc_get_actual_arglist ();
11282 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
11283 init_expr->value.function.actual = arg;
11285 break;
11287 default:
11288 gfc_free_expr (init_expr);
11289 init_expr = NULL;
11291 return init_expr;
11294 /* Add an initialization expression to a local variable. */
11295 static void
11296 apply_default_init_local (gfc_symbol *sym)
11298 gfc_expr *init = NULL;
11300 /* The symbol should be a variable or a function return value. */
11301 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11302 || (sym->attr.function && sym->result != sym))
11303 return;
11305 /* Try to build the initializer expression. If we can't initialize
11306 this symbol, then init will be NULL. */
11307 init = build_default_init_expr (sym);
11308 if (init == NULL)
11309 return;
11311 /* For saved variables, we don't want to add an initializer at function
11312 entry, so we just add a static initializer. Note that automatic variables
11313 are stack allocated even with -fno-automatic; we have also to exclude
11314 result variable, which are also nonstatic. */
11315 if (sym->attr.save || sym->ns->save_all
11316 || (flag_max_stack_var_size == 0 && !sym->attr.result
11317 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
11318 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
11320 /* Don't clobber an existing initializer! */
11321 gcc_assert (sym->value == NULL);
11322 sym->value = init;
11323 return;
11326 build_init_assign (sym, init);
11330 /* Resolution of common features of flavors variable and procedure. */
11332 static bool
11333 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
11335 gfc_array_spec *as;
11337 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11338 as = CLASS_DATA (sym)->as;
11339 else
11340 as = sym->as;
11342 /* Constraints on deferred shape variable. */
11343 if (as == NULL || as->type != AS_DEFERRED)
11345 bool pointer, allocatable, dimension;
11347 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11349 pointer = CLASS_DATA (sym)->attr.class_pointer;
11350 allocatable = CLASS_DATA (sym)->attr.allocatable;
11351 dimension = CLASS_DATA (sym)->attr.dimension;
11353 else
11355 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
11356 allocatable = sym->attr.allocatable;
11357 dimension = sym->attr.dimension;
11360 if (allocatable)
11362 if (dimension && as->type != AS_ASSUMED_RANK)
11364 gfc_error ("Allocatable array %qs at %L must have a deferred "
11365 "shape or assumed rank", sym->name, &sym->declared_at);
11366 return false;
11368 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
11369 "%qs at %L may not be ALLOCATABLE",
11370 sym->name, &sym->declared_at))
11371 return false;
11374 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
11376 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
11377 "assumed rank", sym->name, &sym->declared_at);
11378 return false;
11381 else
11383 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
11384 && sym->ts.type != BT_CLASS && !sym->assoc)
11386 gfc_error ("Array %qs at %L cannot have a deferred shape",
11387 sym->name, &sym->declared_at);
11388 return false;
11392 /* Constraints on polymorphic variables. */
11393 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
11395 /* F03:C502. */
11396 if (sym->attr.class_ok
11397 && !sym->attr.select_type_temporary
11398 && !UNLIMITED_POLY (sym)
11399 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
11401 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
11402 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
11403 &sym->declared_at);
11404 return false;
11407 /* F03:C509. */
11408 /* Assume that use associated symbols were checked in the module ns.
11409 Class-variables that are associate-names are also something special
11410 and excepted from the test. */
11411 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
11413 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
11414 "or pointer", sym->name, &sym->declared_at);
11415 return false;
11419 return true;
11423 /* Additional checks for symbols with flavor variable and derived
11424 type. To be called from resolve_fl_variable. */
11426 static bool
11427 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
11429 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
11431 /* Check to see if a derived type is blocked from being host
11432 associated by the presence of another class I symbol in the same
11433 namespace. 14.6.1.3 of the standard and the discussion on
11434 comp.lang.fortran. */
11435 if (sym->ns != sym->ts.u.derived->ns
11436 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
11438 gfc_symbol *s;
11439 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
11440 if (s && s->attr.generic)
11441 s = gfc_find_dt_in_generic (s);
11442 if (s && !gfc_fl_struct (s->attr.flavor))
11444 gfc_error ("The type %qs cannot be host associated at %L "
11445 "because it is blocked by an incompatible object "
11446 "of the same name declared at %L",
11447 sym->ts.u.derived->name, &sym->declared_at,
11448 &s->declared_at);
11449 return false;
11453 /* 4th constraint in section 11.3: "If an object of a type for which
11454 component-initialization is specified (R429) appears in the
11455 specification-part of a module and does not have the ALLOCATABLE
11456 or POINTER attribute, the object shall have the SAVE attribute."
11458 The check for initializers is performed with
11459 gfc_has_default_initializer because gfc_default_initializer generates
11460 a hidden default for allocatable components. */
11461 if (!(sym->value || no_init_flag) && sym->ns->proc_name
11462 && sym->ns->proc_name->attr.flavor == FL_MODULE
11463 && !sym->ns->save_all && !sym->attr.save
11464 && !sym->attr.pointer && !sym->attr.allocatable
11465 && gfc_has_default_initializer (sym->ts.u.derived)
11466 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
11467 "%qs at %L, needed due to the default "
11468 "initialization", sym->name, &sym->declared_at))
11469 return false;
11471 /* Assign default initializer. */
11472 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
11473 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
11475 sym->value = gfc_default_initializer (&sym->ts);
11478 return true;
11482 /* Resolve symbols with flavor variable. */
11484 static bool
11485 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
11487 int no_init_flag, automatic_flag;
11488 gfc_expr *e;
11489 const char *auto_save_msg;
11490 bool saved_specification_expr;
11492 auto_save_msg = "Automatic object %qs at %L cannot have the "
11493 "SAVE attribute";
11495 if (!resolve_fl_var_and_proc (sym, mp_flag))
11496 return false;
11498 /* Set this flag to check that variables are parameters of all entries.
11499 This check is effected by the call to gfc_resolve_expr through
11500 is_non_constant_shape_array. */
11501 saved_specification_expr = specification_expr;
11502 specification_expr = true;
11504 if (sym->ns->proc_name
11505 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11506 || sym->ns->proc_name->attr.is_main_program)
11507 && !sym->attr.use_assoc
11508 && !sym->attr.allocatable
11509 && !sym->attr.pointer
11510 && is_non_constant_shape_array (sym))
11512 /* The shape of a main program or module array needs to be
11513 constant. */
11514 gfc_error ("The module or main program array %qs at %L must "
11515 "have constant shape", sym->name, &sym->declared_at);
11516 specification_expr = saved_specification_expr;
11517 return false;
11520 /* Constraints on deferred type parameter. */
11521 if (sym->ts.deferred
11522 && !(sym->attr.pointer
11523 || sym->attr.allocatable
11524 || sym->attr.omp_udr_artificial_var))
11526 gfc_error ("Entity %qs at %L has a deferred type parameter and "
11527 "requires either the pointer or allocatable attribute",
11528 sym->name, &sym->declared_at);
11529 specification_expr = saved_specification_expr;
11530 return false;
11533 if (sym->ts.type == BT_CHARACTER)
11535 /* Make sure that character string variables with assumed length are
11536 dummy arguments. */
11537 e = sym->ts.u.cl->length;
11538 if (e == NULL && !sym->attr.dummy && !sym->attr.result
11539 && !sym->ts.deferred && !sym->attr.select_type_temporary
11540 && !sym->attr.omp_udr_artificial_var)
11542 gfc_error ("Entity with assumed character length at %L must be a "
11543 "dummy argument or a PARAMETER", &sym->declared_at);
11544 specification_expr = saved_specification_expr;
11545 return false;
11548 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
11550 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
11551 specification_expr = saved_specification_expr;
11552 return false;
11555 if (!gfc_is_constant_expr (e)
11556 && !(e->expr_type == EXPR_VARIABLE
11557 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
11559 if (!sym->attr.use_assoc && sym->ns->proc_name
11560 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11561 || sym->ns->proc_name->attr.is_main_program))
11563 gfc_error ("%qs at %L must have constant character length "
11564 "in this context", sym->name, &sym->declared_at);
11565 specification_expr = saved_specification_expr;
11566 return false;
11568 if (sym->attr.in_common)
11570 gfc_error ("COMMON variable %qs at %L must have constant "
11571 "character length", sym->name, &sym->declared_at);
11572 specification_expr = saved_specification_expr;
11573 return false;
11578 if (sym->value == NULL && sym->attr.referenced)
11579 apply_default_init_local (sym); /* Try to apply a default initialization. */
11581 /* Determine if the symbol may not have an initializer. */
11582 no_init_flag = automatic_flag = 0;
11583 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
11584 || sym->attr.intrinsic || sym->attr.result)
11585 no_init_flag = 1;
11586 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
11587 && is_non_constant_shape_array (sym))
11589 no_init_flag = automatic_flag = 1;
11591 /* Also, they must not have the SAVE attribute.
11592 SAVE_IMPLICIT is checked below. */
11593 if (sym->as && sym->attr.codimension)
11595 int corank = sym->as->corank;
11596 sym->as->corank = 0;
11597 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
11598 sym->as->corank = corank;
11600 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
11602 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
11603 specification_expr = saved_specification_expr;
11604 return false;
11608 /* Ensure that any initializer is simplified. */
11609 if (sym->value)
11610 gfc_simplify_expr (sym->value, 1);
11612 /* Reject illegal initializers. */
11613 if (!sym->mark && sym->value)
11615 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
11616 && CLASS_DATA (sym)->attr.allocatable))
11617 gfc_error ("Allocatable %qs at %L cannot have an initializer",
11618 sym->name, &sym->declared_at);
11619 else if (sym->attr.external)
11620 gfc_error ("External %qs at %L cannot have an initializer",
11621 sym->name, &sym->declared_at);
11622 else if (sym->attr.dummy
11623 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
11624 gfc_error ("Dummy %qs at %L cannot have an initializer",
11625 sym->name, &sym->declared_at);
11626 else if (sym->attr.intrinsic)
11627 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
11628 sym->name, &sym->declared_at);
11629 else if (sym->attr.result)
11630 gfc_error ("Function result %qs at %L cannot have an initializer",
11631 sym->name, &sym->declared_at);
11632 else if (automatic_flag)
11633 gfc_error ("Automatic array %qs at %L cannot have an initializer",
11634 sym->name, &sym->declared_at);
11635 else
11636 goto no_init_error;
11637 specification_expr = saved_specification_expr;
11638 return false;
11641 no_init_error:
11642 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
11644 bool res = resolve_fl_variable_derived (sym, no_init_flag);
11645 specification_expr = saved_specification_expr;
11646 return res;
11649 specification_expr = saved_specification_expr;
11650 return true;
11654 /* Compare the dummy characteristics of a module procedure interface
11655 declaration with the corresponding declaration in a submodule. */
11656 static gfc_formal_arglist *new_formal;
11657 static char errmsg[200];
11659 static void
11660 compare_fsyms (gfc_symbol *sym)
11662 gfc_symbol *fsym;
11664 if (sym == NULL || new_formal == NULL)
11665 return;
11667 fsym = new_formal->sym;
11669 if (sym == fsym)
11670 return;
11672 if (strcmp (sym->name, fsym->name) == 0)
11674 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
11675 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
11680 /* Resolve a procedure. */
11682 static bool
11683 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
11685 gfc_formal_arglist *arg;
11687 if (sym->attr.function
11688 && !resolve_fl_var_and_proc (sym, mp_flag))
11689 return false;
11691 if (sym->ts.type == BT_CHARACTER)
11693 gfc_charlen *cl = sym->ts.u.cl;
11695 if (cl && cl->length && gfc_is_constant_expr (cl->length)
11696 && !resolve_charlen (cl))
11697 return false;
11699 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11700 && sym->attr.proc == PROC_ST_FUNCTION)
11702 gfc_error ("Character-valued statement function %qs at %L must "
11703 "have constant length", sym->name, &sym->declared_at);
11704 return false;
11708 /* Ensure that derived type for are not of a private type. Internal
11709 module procedures are excluded by 2.2.3.3 - i.e., they are not
11710 externally accessible and can access all the objects accessible in
11711 the host. */
11712 if (!(sym->ns->parent
11713 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
11714 && gfc_check_symbol_access (sym))
11716 gfc_interface *iface;
11718 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
11720 if (arg->sym
11721 && arg->sym->ts.type == BT_DERIVED
11722 && !arg->sym->ts.u.derived->attr.use_assoc
11723 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11724 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
11725 "and cannot be a dummy argument"
11726 " of %qs, which is PUBLIC at %L",
11727 arg->sym->name, sym->name,
11728 &sym->declared_at))
11730 /* Stop this message from recurring. */
11731 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11732 return false;
11736 /* PUBLIC interfaces may expose PRIVATE procedures that take types
11737 PRIVATE to the containing module. */
11738 for (iface = sym->generic; iface; iface = iface->next)
11740 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
11742 if (arg->sym
11743 && arg->sym->ts.type == BT_DERIVED
11744 && !arg->sym->ts.u.derived->attr.use_assoc
11745 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11746 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
11747 "PUBLIC interface %qs at %L "
11748 "takes dummy arguments of %qs which "
11749 "is PRIVATE", iface->sym->name,
11750 sym->name, &iface->sym->declared_at,
11751 gfc_typename(&arg->sym->ts)))
11753 /* Stop this message from recurring. */
11754 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11755 return false;
11761 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
11762 && !sym->attr.proc_pointer)
11764 gfc_error ("Function %qs at %L cannot have an initializer",
11765 sym->name, &sym->declared_at);
11766 return false;
11769 /* An external symbol may not have an initializer because it is taken to be
11770 a procedure. Exception: Procedure Pointers. */
11771 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
11773 gfc_error ("External object %qs at %L may not have an initializer",
11774 sym->name, &sym->declared_at);
11775 return false;
11778 /* An elemental function is required to return a scalar 12.7.1 */
11779 if (sym->attr.elemental && sym->attr.function && sym->as)
11781 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
11782 "result", sym->name, &sym->declared_at);
11783 /* Reset so that the error only occurs once. */
11784 sym->attr.elemental = 0;
11785 return false;
11788 if (sym->attr.proc == PROC_ST_FUNCTION
11789 && (sym->attr.allocatable || sym->attr.pointer))
11791 gfc_error ("Statement function %qs at %L may not have pointer or "
11792 "allocatable attribute", sym->name, &sym->declared_at);
11793 return false;
11796 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
11797 char-len-param shall not be array-valued, pointer-valued, recursive
11798 or pure. ....snip... A character value of * may only be used in the
11799 following ways: (i) Dummy arg of procedure - dummy associates with
11800 actual length; (ii) To declare a named constant; or (iii) External
11801 function - but length must be declared in calling scoping unit. */
11802 if (sym->attr.function
11803 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
11804 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
11806 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
11807 || (sym->attr.recursive) || (sym->attr.pure))
11809 if (sym->as && sym->as->rank)
11810 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11811 "array-valued", sym->name, &sym->declared_at);
11813 if (sym->attr.pointer)
11814 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11815 "pointer-valued", sym->name, &sym->declared_at);
11817 if (sym->attr.pure)
11818 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11819 "pure", sym->name, &sym->declared_at);
11821 if (sym->attr.recursive)
11822 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11823 "recursive", sym->name, &sym->declared_at);
11825 return false;
11828 /* Appendix B.2 of the standard. Contained functions give an
11829 error anyway. Deferred character length is an F2003 feature.
11830 Don't warn on intrinsic conversion functions, which start
11831 with two underscores. */
11832 if (!sym->attr.contained && !sym->ts.deferred
11833 && (sym->name[0] != '_' || sym->name[1] != '_'))
11834 gfc_notify_std (GFC_STD_F95_OBS,
11835 "CHARACTER(*) function %qs at %L",
11836 sym->name, &sym->declared_at);
11839 /* F2008, C1218. */
11840 if (sym->attr.elemental)
11842 if (sym->attr.proc_pointer)
11844 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
11845 sym->name, &sym->declared_at);
11846 return false;
11848 if (sym->attr.dummy)
11850 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
11851 sym->name, &sym->declared_at);
11852 return false;
11856 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11858 gfc_formal_arglist *curr_arg;
11859 int has_non_interop_arg = 0;
11861 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11862 sym->common_block))
11864 /* Clear these to prevent looking at them again if there was an
11865 error. */
11866 sym->attr.is_bind_c = 0;
11867 sym->attr.is_c_interop = 0;
11868 sym->ts.is_c_interop = 0;
11870 else
11872 /* So far, no errors have been found. */
11873 sym->attr.is_c_interop = 1;
11874 sym->ts.is_c_interop = 1;
11877 curr_arg = gfc_sym_get_dummy_args (sym);
11878 while (curr_arg != NULL)
11880 /* Skip implicitly typed dummy args here. */
11881 if (curr_arg->sym->attr.implicit_type == 0)
11882 if (!gfc_verify_c_interop_param (curr_arg->sym))
11883 /* If something is found to fail, record the fact so we
11884 can mark the symbol for the procedure as not being
11885 BIND(C) to try and prevent multiple errors being
11886 reported. */
11887 has_non_interop_arg = 1;
11889 curr_arg = curr_arg->next;
11892 /* See if any of the arguments were not interoperable and if so, clear
11893 the procedure symbol to prevent duplicate error messages. */
11894 if (has_non_interop_arg != 0)
11896 sym->attr.is_c_interop = 0;
11897 sym->ts.is_c_interop = 0;
11898 sym->attr.is_bind_c = 0;
11902 if (!sym->attr.proc_pointer)
11904 if (sym->attr.save == SAVE_EXPLICIT)
11906 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11907 "in %qs at %L", sym->name, &sym->declared_at);
11908 return false;
11910 if (sym->attr.intent)
11912 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11913 "in %qs at %L", sym->name, &sym->declared_at);
11914 return false;
11916 if (sym->attr.subroutine && sym->attr.result)
11918 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11919 "in %qs at %L", sym->name, &sym->declared_at);
11920 return false;
11922 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
11923 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11924 || sym->attr.contained))
11926 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11927 "in %qs at %L", sym->name, &sym->declared_at);
11928 return false;
11930 if (strcmp ("ppr@", sym->name) == 0)
11932 gfc_error ("Procedure pointer result %qs at %L "
11933 "is missing the pointer attribute",
11934 sym->ns->proc_name->name, &sym->declared_at);
11935 return false;
11939 /* Assume that a procedure whose body is not known has references
11940 to external arrays. */
11941 if (sym->attr.if_source != IFSRC_DECL)
11942 sym->attr.array_outer_dependency = 1;
11944 /* Compare the characteristics of a module procedure with the
11945 interface declaration. Ideally this would be done with
11946 gfc_compare_interfaces but, at present, the formal interface
11947 cannot be copied to the ts.interface. */
11948 if (sym->attr.module_procedure
11949 && sym->attr.if_source == IFSRC_DECL)
11951 gfc_symbol *iface;
11952 char name[2*GFC_MAX_SYMBOL_LEN + 1];
11953 char *module_name;
11954 char *submodule_name;
11955 strcpy (name, sym->ns->proc_name->name);
11956 module_name = strtok (name, ".");
11957 submodule_name = strtok (NULL, ".");
11959 /* Stop the dummy characteristics test from using the interface
11960 symbol instead of 'sym'. */
11961 iface = sym->ts.interface;
11962 sym->ts.interface = NULL;
11964 if (iface == NULL)
11965 goto check_formal;
11967 /* Check the procedure characteristics. */
11968 if (sym->attr.pure != iface->attr.pure)
11970 gfc_error ("Mismatch in PURE attribute between MODULE "
11971 "PROCEDURE at %L and its interface in %s",
11972 &sym->declared_at, module_name);
11973 return false;
11976 if (sym->attr.elemental != iface->attr.elemental)
11978 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
11979 "PROCEDURE at %L and its interface in %s",
11980 &sym->declared_at, module_name);
11981 return false;
11984 if (sym->attr.recursive != iface->attr.recursive)
11986 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
11987 "PROCEDURE at %L and its interface in %s",
11988 &sym->declared_at, module_name);
11989 return false;
11992 /* Check the result characteristics. */
11993 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
11995 gfc_error ("%s between the MODULE PROCEDURE declaration "
11996 "in module %s and the declaration at %L in "
11997 "SUBMODULE %s", errmsg, module_name,
11998 &sym->declared_at, submodule_name);
11999 return false;
12002 check_formal:
12003 /* Check the charcateristics of the formal arguments. */
12004 if (sym->formal && sym->formal_ns)
12006 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
12008 new_formal = arg;
12009 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
12013 sym->ts.interface = iface;
12015 return true;
12019 /* Resolve a list of finalizer procedures. That is, after they have hopefully
12020 been defined and we now know their defined arguments, check that they fulfill
12021 the requirements of the standard for procedures used as finalizers. */
12023 static bool
12024 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
12026 gfc_finalizer* list;
12027 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
12028 bool result = true;
12029 bool seen_scalar = false;
12030 gfc_symbol *vtab;
12031 gfc_component *c;
12032 gfc_symbol *parent = gfc_get_derived_super_type (derived);
12034 if (parent)
12035 gfc_resolve_finalizers (parent, finalizable);
12037 /* Return early when not finalizable. Additionally, ensure that derived-type
12038 components have a their finalizables resolved. */
12039 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
12041 bool has_final = false;
12042 for (c = derived->components; c; c = c->next)
12043 if (c->ts.type == BT_DERIVED
12044 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
12046 bool has_final2 = false;
12047 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final))
12048 return false; /* Error. */
12049 has_final = has_final || has_final2;
12051 if (!has_final)
12053 if (finalizable)
12054 *finalizable = false;
12055 return true;
12059 /* Walk over the list of finalizer-procedures, check them, and if any one
12060 does not fit in with the standard's definition, print an error and remove
12061 it from the list. */
12062 prev_link = &derived->f2k_derived->finalizers;
12063 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
12065 gfc_formal_arglist *dummy_args;
12066 gfc_symbol* arg;
12067 gfc_finalizer* i;
12068 int my_rank;
12070 /* Skip this finalizer if we already resolved it. */
12071 if (list->proc_tree)
12073 prev_link = &(list->next);
12074 continue;
12077 /* Check this exists and is a SUBROUTINE. */
12078 if (!list->proc_sym->attr.subroutine)
12080 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
12081 list->proc_sym->name, &list->where);
12082 goto error;
12085 /* We should have exactly one argument. */
12086 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
12087 if (!dummy_args || dummy_args->next)
12089 gfc_error ("FINAL procedure at %L must have exactly one argument",
12090 &list->where);
12091 goto error;
12093 arg = dummy_args->sym;
12095 /* This argument must be of our type. */
12096 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
12098 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
12099 &arg->declared_at, derived->name);
12100 goto error;
12103 /* It must neither be a pointer nor allocatable nor optional. */
12104 if (arg->attr.pointer)
12106 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
12107 &arg->declared_at);
12108 goto error;
12110 if (arg->attr.allocatable)
12112 gfc_error ("Argument of FINAL procedure at %L must not be"
12113 " ALLOCATABLE", &arg->declared_at);
12114 goto error;
12116 if (arg->attr.optional)
12118 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
12119 &arg->declared_at);
12120 goto error;
12123 /* It must not be INTENT(OUT). */
12124 if (arg->attr.intent == INTENT_OUT)
12126 gfc_error ("Argument of FINAL procedure at %L must not be"
12127 " INTENT(OUT)", &arg->declared_at);
12128 goto error;
12131 /* Warn if the procedure is non-scalar and not assumed shape. */
12132 if (warn_surprising && arg->as && arg->as->rank != 0
12133 && arg->as->type != AS_ASSUMED_SHAPE)
12134 gfc_warning (OPT_Wsurprising,
12135 "Non-scalar FINAL procedure at %L should have assumed"
12136 " shape argument", &arg->declared_at);
12138 /* Check that it does not match in kind and rank with a FINAL procedure
12139 defined earlier. To really loop over the *earlier* declarations,
12140 we need to walk the tail of the list as new ones were pushed at the
12141 front. */
12142 /* TODO: Handle kind parameters once they are implemented. */
12143 my_rank = (arg->as ? arg->as->rank : 0);
12144 for (i = list->next; i; i = i->next)
12146 gfc_formal_arglist *dummy_args;
12148 /* Argument list might be empty; that is an error signalled earlier,
12149 but we nevertheless continued resolving. */
12150 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
12151 if (dummy_args)
12153 gfc_symbol* i_arg = dummy_args->sym;
12154 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
12155 if (i_rank == my_rank)
12157 gfc_error ("FINAL procedure %qs declared at %L has the same"
12158 " rank (%d) as %qs",
12159 list->proc_sym->name, &list->where, my_rank,
12160 i->proc_sym->name);
12161 goto error;
12166 /* Is this the/a scalar finalizer procedure? */
12167 if (!arg->as || arg->as->rank == 0)
12168 seen_scalar = true;
12170 /* Find the symtree for this procedure. */
12171 gcc_assert (!list->proc_tree);
12172 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
12174 prev_link = &list->next;
12175 continue;
12177 /* Remove wrong nodes immediately from the list so we don't risk any
12178 troubles in the future when they might fail later expectations. */
12179 error:
12180 i = list;
12181 *prev_link = list->next;
12182 gfc_free_finalizer (i);
12183 result = false;
12186 if (result == false)
12187 return false;
12189 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
12190 were nodes in the list, must have been for arrays. It is surely a good
12191 idea to have a scalar version there if there's something to finalize. */
12192 if (warn_surprising && result && !seen_scalar)
12193 gfc_warning (OPT_Wsurprising,
12194 "Only array FINAL procedures declared for derived type %qs"
12195 " defined at %L, suggest also scalar one",
12196 derived->name, &derived->declared_at);
12198 vtab = gfc_find_derived_vtab (derived);
12199 c = vtab->ts.u.derived->components->next->next->next->next->next;
12200 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
12202 if (finalizable)
12203 *finalizable = true;
12205 return true;
12209 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
12211 static bool
12212 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
12213 const char* generic_name, locus where)
12215 gfc_symbol *sym1, *sym2;
12216 const char *pass1, *pass2;
12217 gfc_formal_arglist *dummy_args;
12219 gcc_assert (t1->specific && t2->specific);
12220 gcc_assert (!t1->specific->is_generic);
12221 gcc_assert (!t2->specific->is_generic);
12222 gcc_assert (t1->is_operator == t2->is_operator);
12224 sym1 = t1->specific->u.specific->n.sym;
12225 sym2 = t2->specific->u.specific->n.sym;
12227 if (sym1 == sym2)
12228 return true;
12230 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
12231 if (sym1->attr.subroutine != sym2->attr.subroutine
12232 || sym1->attr.function != sym2->attr.function)
12234 gfc_error ("%qs and %qs can't be mixed FUNCTION/SUBROUTINE for"
12235 " GENERIC %qs at %L",
12236 sym1->name, sym2->name, generic_name, &where);
12237 return false;
12240 /* Determine PASS arguments. */
12241 if (t1->specific->nopass)
12242 pass1 = NULL;
12243 else if (t1->specific->pass_arg)
12244 pass1 = t1->specific->pass_arg;
12245 else
12247 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
12248 if (dummy_args)
12249 pass1 = dummy_args->sym->name;
12250 else
12251 pass1 = NULL;
12253 if (t2->specific->nopass)
12254 pass2 = NULL;
12255 else if (t2->specific->pass_arg)
12256 pass2 = t2->specific->pass_arg;
12257 else
12259 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
12260 if (dummy_args)
12261 pass2 = dummy_args->sym->name;
12262 else
12263 pass2 = NULL;
12266 /* Compare the interfaces. */
12267 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
12268 NULL, 0, pass1, pass2))
12270 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
12271 sym1->name, sym2->name, generic_name, &where);
12272 return false;
12275 return true;
12279 /* Worker function for resolving a generic procedure binding; this is used to
12280 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
12282 The difference between those cases is finding possible inherited bindings
12283 that are overridden, as one has to look for them in tb_sym_root,
12284 tb_uop_root or tb_op, respectively. Thus the caller must already find
12285 the super-type and set p->overridden correctly. */
12287 static bool
12288 resolve_tb_generic_targets (gfc_symbol* super_type,
12289 gfc_typebound_proc* p, const char* name)
12291 gfc_tbp_generic* target;
12292 gfc_symtree* first_target;
12293 gfc_symtree* inherited;
12295 gcc_assert (p && p->is_generic);
12297 /* Try to find the specific bindings for the symtrees in our target-list. */
12298 gcc_assert (p->u.generic);
12299 for (target = p->u.generic; target; target = target->next)
12300 if (!target->specific)
12302 gfc_typebound_proc* overridden_tbp;
12303 gfc_tbp_generic* g;
12304 const char* target_name;
12306 target_name = target->specific_st->name;
12308 /* Defined for this type directly. */
12309 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
12311 target->specific = target->specific_st->n.tb;
12312 goto specific_found;
12315 /* Look for an inherited specific binding. */
12316 if (super_type)
12318 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
12319 true, NULL);
12321 if (inherited)
12323 gcc_assert (inherited->n.tb);
12324 target->specific = inherited->n.tb;
12325 goto specific_found;
12329 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
12330 " at %L", target_name, name, &p->where);
12331 return false;
12333 /* Once we've found the specific binding, check it is not ambiguous with
12334 other specifics already found or inherited for the same GENERIC. */
12335 specific_found:
12336 gcc_assert (target->specific);
12338 /* This must really be a specific binding! */
12339 if (target->specific->is_generic)
12341 gfc_error ("GENERIC %qs at %L must target a specific binding,"
12342 " %qs is GENERIC, too", name, &p->where, target_name);
12343 return false;
12346 /* Check those already resolved on this type directly. */
12347 for (g = p->u.generic; g; g = g->next)
12348 if (g != target && g->specific
12349 && !check_generic_tbp_ambiguity (target, g, name, p->where))
12350 return false;
12352 /* Check for ambiguity with inherited specific targets. */
12353 for (overridden_tbp = p->overridden; overridden_tbp;
12354 overridden_tbp = overridden_tbp->overridden)
12355 if (overridden_tbp->is_generic)
12357 for (g = overridden_tbp->u.generic; g; g = g->next)
12359 gcc_assert (g->specific);
12360 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
12361 return false;
12366 /* If we attempt to "overwrite" a specific binding, this is an error. */
12367 if (p->overridden && !p->overridden->is_generic)
12369 gfc_error ("GENERIC %qs at %L can't overwrite specific binding with"
12370 " the same name", name, &p->where);
12371 return false;
12374 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
12375 all must have the same attributes here. */
12376 first_target = p->u.generic->specific->u.specific;
12377 gcc_assert (first_target);
12378 p->subroutine = first_target->n.sym->attr.subroutine;
12379 p->function = first_target->n.sym->attr.function;
12381 return true;
12385 /* Resolve a GENERIC procedure binding for a derived type. */
12387 static bool
12388 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
12390 gfc_symbol* super_type;
12392 /* Find the overridden binding if any. */
12393 st->n.tb->overridden = NULL;
12394 super_type = gfc_get_derived_super_type (derived);
12395 if (super_type)
12397 gfc_symtree* overridden;
12398 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
12399 true, NULL);
12401 if (overridden && overridden->n.tb)
12402 st->n.tb->overridden = overridden->n.tb;
12405 /* Resolve using worker function. */
12406 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
12410 /* Retrieve the target-procedure of an operator binding and do some checks in
12411 common for intrinsic and user-defined type-bound operators. */
12413 static gfc_symbol*
12414 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
12416 gfc_symbol* target_proc;
12418 gcc_assert (target->specific && !target->specific->is_generic);
12419 target_proc = target->specific->u.specific->n.sym;
12420 gcc_assert (target_proc);
12422 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
12423 if (target->specific->nopass)
12425 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
12426 return NULL;
12429 return target_proc;
12433 /* Resolve a type-bound intrinsic operator. */
12435 static bool
12436 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
12437 gfc_typebound_proc* p)
12439 gfc_symbol* super_type;
12440 gfc_tbp_generic* target;
12442 /* If there's already an error here, do nothing (but don't fail again). */
12443 if (p->error)
12444 return true;
12446 /* Operators should always be GENERIC bindings. */
12447 gcc_assert (p->is_generic);
12449 /* Look for an overridden binding. */
12450 super_type = gfc_get_derived_super_type (derived);
12451 if (super_type && super_type->f2k_derived)
12452 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
12453 op, true, NULL);
12454 else
12455 p->overridden = NULL;
12457 /* Resolve general GENERIC properties using worker function. */
12458 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
12459 goto error;
12461 /* Check the targets to be procedures of correct interface. */
12462 for (target = p->u.generic; target; target = target->next)
12464 gfc_symbol* target_proc;
12466 target_proc = get_checked_tb_operator_target (target, p->where);
12467 if (!target_proc)
12468 goto error;
12470 if (!gfc_check_operator_interface (target_proc, op, p->where))
12471 goto error;
12473 /* Add target to non-typebound operator list. */
12474 if (!target->specific->deferred && !derived->attr.use_assoc
12475 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
12477 gfc_interface *head, *intr;
12478 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
12479 return false;
12480 head = derived->ns->op[op];
12481 intr = gfc_get_interface ();
12482 intr->sym = target_proc;
12483 intr->where = p->where;
12484 intr->next = head;
12485 derived->ns->op[op] = intr;
12489 return true;
12491 error:
12492 p->error = 1;
12493 return false;
12497 /* Resolve a type-bound user operator (tree-walker callback). */
12499 static gfc_symbol* resolve_bindings_derived;
12500 static bool resolve_bindings_result;
12502 static bool check_uop_procedure (gfc_symbol* sym, locus where);
12504 static void
12505 resolve_typebound_user_op (gfc_symtree* stree)
12507 gfc_symbol* super_type;
12508 gfc_tbp_generic* target;
12510 gcc_assert (stree && stree->n.tb);
12512 if (stree->n.tb->error)
12513 return;
12515 /* Operators should always be GENERIC bindings. */
12516 gcc_assert (stree->n.tb->is_generic);
12518 /* Find overridden procedure, if any. */
12519 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12520 if (super_type && super_type->f2k_derived)
12522 gfc_symtree* overridden;
12523 overridden = gfc_find_typebound_user_op (super_type, NULL,
12524 stree->name, true, NULL);
12526 if (overridden && overridden->n.tb)
12527 stree->n.tb->overridden = overridden->n.tb;
12529 else
12530 stree->n.tb->overridden = NULL;
12532 /* Resolve basically using worker function. */
12533 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
12534 goto error;
12536 /* Check the targets to be functions of correct interface. */
12537 for (target = stree->n.tb->u.generic; target; target = target->next)
12539 gfc_symbol* target_proc;
12541 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
12542 if (!target_proc)
12543 goto error;
12545 if (!check_uop_procedure (target_proc, stree->n.tb->where))
12546 goto error;
12549 return;
12551 error:
12552 resolve_bindings_result = false;
12553 stree->n.tb->error = 1;
12557 /* Resolve the type-bound procedures for a derived type. */
12559 static void
12560 resolve_typebound_procedure (gfc_symtree* stree)
12562 gfc_symbol* proc;
12563 locus where;
12564 gfc_symbol* me_arg;
12565 gfc_symbol* super_type;
12566 gfc_component* comp;
12568 gcc_assert (stree);
12570 /* Undefined specific symbol from GENERIC target definition. */
12571 if (!stree->n.tb)
12572 return;
12574 if (stree->n.tb->error)
12575 return;
12577 /* If this is a GENERIC binding, use that routine. */
12578 if (stree->n.tb->is_generic)
12580 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
12581 goto error;
12582 return;
12585 /* Get the target-procedure to check it. */
12586 gcc_assert (!stree->n.tb->is_generic);
12587 gcc_assert (stree->n.tb->u.specific);
12588 proc = stree->n.tb->u.specific->n.sym;
12589 where = stree->n.tb->where;
12591 /* Default access should already be resolved from the parser. */
12592 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
12594 if (stree->n.tb->deferred)
12596 if (!check_proc_interface (proc, &where))
12597 goto error;
12599 else
12601 /* Check for F08:C465. */
12602 if ((!proc->attr.subroutine && !proc->attr.function)
12603 || (proc->attr.proc != PROC_MODULE
12604 && proc->attr.if_source != IFSRC_IFBODY)
12605 || proc->attr.abstract)
12607 gfc_error ("%qs must be a module procedure or an external procedure with"
12608 " an explicit interface at %L", proc->name, &where);
12609 goto error;
12613 stree->n.tb->subroutine = proc->attr.subroutine;
12614 stree->n.tb->function = proc->attr.function;
12616 /* Find the super-type of the current derived type. We could do this once and
12617 store in a global if speed is needed, but as long as not I believe this is
12618 more readable and clearer. */
12619 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12621 /* If PASS, resolve and check arguments if not already resolved / loaded
12622 from a .mod file. */
12623 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
12625 gfc_formal_arglist *dummy_args;
12627 dummy_args = gfc_sym_get_dummy_args (proc);
12628 if (stree->n.tb->pass_arg)
12630 gfc_formal_arglist *i;
12632 /* If an explicit passing argument name is given, walk the arg-list
12633 and look for it. */
12635 me_arg = NULL;
12636 stree->n.tb->pass_arg_num = 1;
12637 for (i = dummy_args; i; i = i->next)
12639 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
12641 me_arg = i->sym;
12642 break;
12644 ++stree->n.tb->pass_arg_num;
12647 if (!me_arg)
12649 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
12650 " argument %qs",
12651 proc->name, stree->n.tb->pass_arg, &where,
12652 stree->n.tb->pass_arg);
12653 goto error;
12656 else
12658 /* Otherwise, take the first one; there should in fact be at least
12659 one. */
12660 stree->n.tb->pass_arg_num = 1;
12661 if (!dummy_args)
12663 gfc_error ("Procedure %qs with PASS at %L must have at"
12664 " least one argument", proc->name, &where);
12665 goto error;
12667 me_arg = dummy_args->sym;
12670 /* Now check that the argument-type matches and the passed-object
12671 dummy argument is generally fine. */
12673 gcc_assert (me_arg);
12675 if (me_arg->ts.type != BT_CLASS)
12677 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
12678 " at %L", proc->name, &where);
12679 goto error;
12682 if (CLASS_DATA (me_arg)->ts.u.derived
12683 != resolve_bindings_derived)
12685 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
12686 " the derived-type %qs", me_arg->name, proc->name,
12687 me_arg->name, &where, resolve_bindings_derived->name);
12688 goto error;
12691 gcc_assert (me_arg->ts.type == BT_CLASS);
12692 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
12694 gfc_error ("Passed-object dummy argument of %qs at %L must be"
12695 " scalar", proc->name, &where);
12696 goto error;
12698 if (CLASS_DATA (me_arg)->attr.allocatable)
12700 gfc_error ("Passed-object dummy argument of %qs at %L must not"
12701 " be ALLOCATABLE", proc->name, &where);
12702 goto error;
12704 if (CLASS_DATA (me_arg)->attr.class_pointer)
12706 gfc_error ("Passed-object dummy argument of %qs at %L must not"
12707 " be POINTER", proc->name, &where);
12708 goto error;
12712 /* If we are extending some type, check that we don't override a procedure
12713 flagged NON_OVERRIDABLE. */
12714 stree->n.tb->overridden = NULL;
12715 if (super_type)
12717 gfc_symtree* overridden;
12718 overridden = gfc_find_typebound_proc (super_type, NULL,
12719 stree->name, true, NULL);
12721 if (overridden)
12723 if (overridden->n.tb)
12724 stree->n.tb->overridden = overridden->n.tb;
12726 if (!gfc_check_typebound_override (stree, overridden))
12727 goto error;
12731 /* See if there's a name collision with a component directly in this type. */
12732 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
12733 if (!strcmp (comp->name, stree->name))
12735 gfc_error ("Procedure %qs at %L has the same name as a component of"
12736 " %qs",
12737 stree->name, &where, resolve_bindings_derived->name);
12738 goto error;
12741 /* Try to find a name collision with an inherited component. */
12742 if (super_type && gfc_find_component (super_type, stree->name, true, true,
12743 NULL))
12745 gfc_error ("Procedure %qs at %L has the same name as an inherited"
12746 " component of %qs",
12747 stree->name, &where, resolve_bindings_derived->name);
12748 goto error;
12751 stree->n.tb->error = 0;
12752 return;
12754 error:
12755 resolve_bindings_result = false;
12756 stree->n.tb->error = 1;
12760 static bool
12761 resolve_typebound_procedures (gfc_symbol* derived)
12763 int op;
12764 gfc_symbol* super_type;
12766 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
12767 return true;
12769 super_type = gfc_get_derived_super_type (derived);
12770 if (super_type)
12771 resolve_symbol (super_type);
12773 resolve_bindings_derived = derived;
12774 resolve_bindings_result = true;
12776 if (derived->f2k_derived->tb_sym_root)
12777 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
12778 &resolve_typebound_procedure);
12780 if (derived->f2k_derived->tb_uop_root)
12781 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
12782 &resolve_typebound_user_op);
12784 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
12786 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
12787 if (p && !resolve_typebound_intrinsic_op (derived,
12788 (gfc_intrinsic_op)op, p))
12789 resolve_bindings_result = false;
12792 return resolve_bindings_result;
12796 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
12797 to give all identical derived types the same backend_decl. */
12798 static void
12799 add_dt_to_dt_list (gfc_symbol *derived)
12801 gfc_dt_list *dt_list;
12803 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
12804 if (derived == dt_list->derived)
12805 return;
12807 dt_list = gfc_get_dt_list ();
12808 dt_list->next = gfc_derived_types;
12809 dt_list->derived = derived;
12810 gfc_derived_types = dt_list;
12814 /* Ensure that a derived-type is really not abstract, meaning that every
12815 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
12817 static bool
12818 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
12820 if (!st)
12821 return true;
12823 if (!ensure_not_abstract_walker (sub, st->left))
12824 return false;
12825 if (!ensure_not_abstract_walker (sub, st->right))
12826 return false;
12828 if (st->n.tb && st->n.tb->deferred)
12830 gfc_symtree* overriding;
12831 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
12832 if (!overriding)
12833 return false;
12834 gcc_assert (overriding->n.tb);
12835 if (overriding->n.tb->deferred)
12837 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
12838 " %qs is DEFERRED and not overridden",
12839 sub->name, &sub->declared_at, st->name);
12840 return false;
12844 return true;
12847 static bool
12848 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
12850 /* The algorithm used here is to recursively travel up the ancestry of sub
12851 and for each ancestor-type, check all bindings. If any of them is
12852 DEFERRED, look it up starting from sub and see if the found (overriding)
12853 binding is not DEFERRED.
12854 This is not the most efficient way to do this, but it should be ok and is
12855 clearer than something sophisticated. */
12857 gcc_assert (ancestor && !sub->attr.abstract);
12859 if (!ancestor->attr.abstract)
12860 return true;
12862 /* Walk bindings of this ancestor. */
12863 if (ancestor->f2k_derived)
12865 bool t;
12866 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
12867 if (!t)
12868 return false;
12871 /* Find next ancestor type and recurse on it. */
12872 ancestor = gfc_get_derived_super_type (ancestor);
12873 if (ancestor)
12874 return ensure_not_abstract (sub, ancestor);
12876 return true;
12880 /* This check for typebound defined assignments is done recursively
12881 since the order in which derived types are resolved is not always in
12882 order of the declarations. */
12884 static void
12885 check_defined_assignments (gfc_symbol *derived)
12887 gfc_component *c;
12889 for (c = derived->components; c; c = c->next)
12891 if (!gfc_bt_struct (c->ts.type)
12892 || c->attr.pointer
12893 || c->attr.allocatable
12894 || c->attr.proc_pointer_comp
12895 || c->attr.class_pointer
12896 || c->attr.proc_pointer)
12897 continue;
12899 if (c->ts.u.derived->attr.defined_assign_comp
12900 || (c->ts.u.derived->f2k_derived
12901 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
12903 derived->attr.defined_assign_comp = 1;
12904 return;
12907 check_defined_assignments (c->ts.u.derived);
12908 if (c->ts.u.derived->attr.defined_assign_comp)
12910 derived->attr.defined_assign_comp = 1;
12911 return;
12917 /* Resolve a single component of a derived type or structure. */
12919 static bool
12920 resolve_component (gfc_component *c, gfc_symbol *sym)
12922 gfc_symbol *super_type;
12924 if (c->attr.artificial)
12925 return true;
12927 /* F2008, C442. */
12928 if ((!sym->attr.is_class || c != sym->components)
12929 && c->attr.codimension
12930 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
12932 gfc_error ("Coarray component %qs at %L must be allocatable with "
12933 "deferred shape", c->name, &c->loc);
12934 return false;
12937 /* F2008, C443. */
12938 if (c->attr.codimension && c->ts.type == BT_DERIVED
12939 && c->ts.u.derived->ts.is_iso_c)
12941 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12942 "shall not be a coarray", c->name, &c->loc);
12943 return false;
12946 /* F2008, C444. */
12947 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
12948 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12949 || c->attr.allocatable))
12951 gfc_error ("Component %qs at %L with coarray component "
12952 "shall be a nonpointer, nonallocatable scalar",
12953 c->name, &c->loc);
12954 return false;
12957 /* F2008, C448. */
12958 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12960 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
12961 "is not an array pointer", c->name, &c->loc);
12962 return false;
12965 if (c->attr.proc_pointer && c->ts.interface)
12967 gfc_symbol *ifc = c->ts.interface;
12969 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
12971 c->tb->error = 1;
12972 return false;
12975 if (ifc->attr.if_source || ifc->attr.intrinsic)
12977 /* Resolve interface and copy attributes. */
12978 if (ifc->formal && !ifc->formal_ns)
12979 resolve_symbol (ifc);
12980 if (ifc->attr.intrinsic)
12981 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
12983 if (ifc->result)
12985 c->ts = ifc->result->ts;
12986 c->attr.allocatable = ifc->result->attr.allocatable;
12987 c->attr.pointer = ifc->result->attr.pointer;
12988 c->attr.dimension = ifc->result->attr.dimension;
12989 c->as = gfc_copy_array_spec (ifc->result->as);
12990 c->attr.class_ok = ifc->result->attr.class_ok;
12992 else
12994 c->ts = ifc->ts;
12995 c->attr.allocatable = ifc->attr.allocatable;
12996 c->attr.pointer = ifc->attr.pointer;
12997 c->attr.dimension = ifc->attr.dimension;
12998 c->as = gfc_copy_array_spec (ifc->as);
12999 c->attr.class_ok = ifc->attr.class_ok;
13001 c->ts.interface = ifc;
13002 c->attr.function = ifc->attr.function;
13003 c->attr.subroutine = ifc->attr.subroutine;
13005 c->attr.pure = ifc->attr.pure;
13006 c->attr.elemental = ifc->attr.elemental;
13007 c->attr.recursive = ifc->attr.recursive;
13008 c->attr.always_explicit = ifc->attr.always_explicit;
13009 c->attr.ext_attr |= ifc->attr.ext_attr;
13010 /* Copy char length. */
13011 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
13013 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
13014 if (cl->length && !cl->resolved
13015 && !gfc_resolve_expr (cl->length))
13017 c->tb->error = 1;
13018 return false;
13020 c->ts.u.cl = cl;
13024 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
13026 /* Since PPCs are not implicitly typed, a PPC without an explicit
13027 interface must be a subroutine. */
13028 gfc_add_subroutine (&c->attr, c->name, &c->loc);
13031 /* Procedure pointer components: Check PASS arg. */
13032 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
13033 && !sym->attr.vtype)
13035 gfc_symbol* me_arg;
13037 if (c->tb->pass_arg)
13039 gfc_formal_arglist* i;
13041 /* If an explicit passing argument name is given, walk the arg-list
13042 and look for it. */
13044 me_arg = NULL;
13045 c->tb->pass_arg_num = 1;
13046 for (i = c->ts.interface->formal; i; i = i->next)
13048 if (!strcmp (i->sym->name, c->tb->pass_arg))
13050 me_arg = i->sym;
13051 break;
13053 c->tb->pass_arg_num++;
13056 if (!me_arg)
13058 gfc_error ("Procedure pointer component %qs with PASS(%s) "
13059 "at %L has no argument %qs", c->name,
13060 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
13061 c->tb->error = 1;
13062 return false;
13065 else
13067 /* Otherwise, take the first one; there should in fact be at least
13068 one. */
13069 c->tb->pass_arg_num = 1;
13070 if (!c->ts.interface->formal)
13072 gfc_error ("Procedure pointer component %qs with PASS at %L "
13073 "must have at least one argument",
13074 c->name, &c->loc);
13075 c->tb->error = 1;
13076 return false;
13078 me_arg = c->ts.interface->formal->sym;
13081 /* Now check that the argument-type matches. */
13082 gcc_assert (me_arg);
13083 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
13084 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
13085 || (me_arg->ts.type == BT_CLASS
13086 && CLASS_DATA (me_arg)->ts.u.derived != sym))
13088 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
13089 " the derived type %qs", me_arg->name, c->name,
13090 me_arg->name, &c->loc, sym->name);
13091 c->tb->error = 1;
13092 return false;
13095 /* Check for C453. */
13096 if (me_arg->attr.dimension)
13098 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13099 "must be scalar", me_arg->name, c->name, me_arg->name,
13100 &c->loc);
13101 c->tb->error = 1;
13102 return false;
13105 if (me_arg->attr.pointer)
13107 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13108 "may not have the POINTER attribute", me_arg->name,
13109 c->name, me_arg->name, &c->loc);
13110 c->tb->error = 1;
13111 return false;
13114 if (me_arg->attr.allocatable)
13116 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13117 "may not be ALLOCATABLE", me_arg->name, c->name,
13118 me_arg->name, &c->loc);
13119 c->tb->error = 1;
13120 return false;
13123 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
13125 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
13126 " at %L", c->name, &c->loc);
13127 return false;
13132 /* Check type-spec if this is not the parent-type component. */
13133 if (((sym->attr.is_class
13134 && (!sym->components->ts.u.derived->attr.extension
13135 || c != sym->components->ts.u.derived->components))
13136 || (!sym->attr.is_class
13137 && (!sym->attr.extension || c != sym->components)))
13138 && !sym->attr.vtype
13139 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
13140 return false;
13142 super_type = gfc_get_derived_super_type (sym);
13144 /* If this type is an extension, set the accessibility of the parent
13145 component. */
13146 if (super_type
13147 && ((sym->attr.is_class
13148 && c == sym->components->ts.u.derived->components)
13149 || (!sym->attr.is_class && c == sym->components))
13150 && strcmp (super_type->name, c->name) == 0)
13151 c->attr.access = super_type->attr.access;
13153 /* If this type is an extension, see if this component has the same name
13154 as an inherited type-bound procedure. */
13155 if (super_type && !sym->attr.is_class
13156 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
13158 gfc_error ("Component %qs of %qs at %L has the same name as an"
13159 " inherited type-bound procedure",
13160 c->name, sym->name, &c->loc);
13161 return false;
13164 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
13165 && !c->ts.deferred)
13167 if (c->ts.u.cl->length == NULL
13168 || (!resolve_charlen(c->ts.u.cl))
13169 || !gfc_is_constant_expr (c->ts.u.cl->length))
13171 gfc_error ("Character length of component %qs needs to "
13172 "be a constant specification expression at %L",
13173 c->name,
13174 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
13175 return false;
13179 if (c->ts.type == BT_CHARACTER && c->ts.deferred
13180 && !c->attr.pointer && !c->attr.allocatable)
13182 gfc_error ("Character component %qs of %qs at %L with deferred "
13183 "length must be a POINTER or ALLOCATABLE",
13184 c->name, sym->name, &c->loc);
13185 return false;
13188 /* Add the hidden deferred length field. */
13189 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function
13190 && !sym->attr.is_class)
13192 char name[GFC_MAX_SYMBOL_LEN+9];
13193 gfc_component *strlen;
13194 sprintf (name, "_%s_length", c->name);
13195 strlen = gfc_find_component (sym, name, true, true, NULL);
13196 if (strlen == NULL)
13198 if (!gfc_add_component (sym, name, &strlen))
13199 return false;
13200 strlen->ts.type = BT_INTEGER;
13201 strlen->ts.kind = gfc_charlen_int_kind;
13202 strlen->attr.access = ACCESS_PRIVATE;
13203 strlen->attr.artificial = 1;
13207 if (c->ts.type == BT_DERIVED
13208 && sym->component_access != ACCESS_PRIVATE
13209 && gfc_check_symbol_access (sym)
13210 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
13211 && !c->ts.u.derived->attr.use_assoc
13212 && !gfc_check_symbol_access (c->ts.u.derived)
13213 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
13214 "PRIVATE type and cannot be a component of "
13215 "%qs, which is PUBLIC at %L", c->name,
13216 sym->name, &sym->declared_at))
13217 return false;
13219 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
13221 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
13222 "type %s", c->name, &c->loc, sym->name);
13223 return false;
13226 if (sym->attr.sequence)
13228 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
13230 gfc_error ("Component %s of SEQUENCE type declared at %L does "
13231 "not have the SEQUENCE attribute",
13232 c->ts.u.derived->name, &sym->declared_at);
13233 return false;
13237 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
13238 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
13239 else if (c->ts.type == BT_CLASS && c->attr.class_ok
13240 && CLASS_DATA (c)->ts.u.derived->attr.generic)
13241 CLASS_DATA (c)->ts.u.derived
13242 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
13244 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
13245 && c->attr.pointer && c->ts.u.derived->components == NULL
13246 && !c->ts.u.derived->attr.zero_comp)
13248 gfc_error ("The pointer component %qs of %qs at %L is a type "
13249 "that has not been declared", c->name, sym->name,
13250 &c->loc);
13251 return false;
13254 if (c->ts.type == BT_CLASS && c->attr.class_ok
13255 && CLASS_DATA (c)->attr.class_pointer
13256 && CLASS_DATA (c)->ts.u.derived->components == NULL
13257 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
13258 && !UNLIMITED_POLY (c))
13260 gfc_error ("The pointer component %qs of %qs at %L is a type "
13261 "that has not been declared", c->name, sym->name,
13262 &c->loc);
13263 return false;
13266 /* C437. */
13267 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
13268 && (!c->attr.class_ok
13269 || !(CLASS_DATA (c)->attr.class_pointer
13270 || CLASS_DATA (c)->attr.allocatable)))
13272 gfc_error ("Component %qs with CLASS at %L must be allocatable "
13273 "or pointer", c->name, &c->loc);
13274 /* Prevent a recurrence of the error. */
13275 c->ts.type = BT_UNKNOWN;
13276 return false;
13279 /* Ensure that all the derived type components are put on the
13280 derived type list; even in formal namespaces, where derived type
13281 pointer components might not have been declared. */
13282 if (c->ts.type == BT_DERIVED
13283 && c->ts.u.derived
13284 && c->ts.u.derived->components
13285 && c->attr.pointer
13286 && sym != c->ts.u.derived)
13287 add_dt_to_dt_list (c->ts.u.derived);
13289 if (!gfc_resolve_array_spec (c->as,
13290 !(c->attr.pointer || c->attr.proc_pointer
13291 || c->attr.allocatable)))
13292 return false;
13294 if (c->initializer && !sym->attr.vtype
13295 && !gfc_check_assign_symbol (sym, c, c->initializer))
13296 return false;
13298 return true;
13302 /* Be nice about the locus for a structure expression - show the locus of the
13303 first non-null sub-expression if we can. */
13305 static locus *
13306 cons_where (gfc_expr *struct_expr)
13308 gfc_constructor *cons;
13310 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
13312 cons = gfc_constructor_first (struct_expr->value.constructor);
13313 for (; cons; cons = gfc_constructor_next (cons))
13315 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
13316 return &cons->expr->where;
13319 return &struct_expr->where;
13322 /* Resolve the components of a structure type. Much less work than derived
13323 types. */
13325 static bool
13326 resolve_fl_struct (gfc_symbol *sym)
13328 gfc_component *c;
13329 gfc_expr *init = NULL;
13330 bool success;
13332 /* Make sure UNIONs do not have overlapping initializers. */
13333 if (sym->attr.flavor == FL_UNION)
13335 for (c = sym->components; c; c = c->next)
13337 if (init && c->initializer)
13339 gfc_error ("Conflicting initializers in union at %L and %L",
13340 cons_where (init), cons_where (c->initializer));
13341 gfc_free_expr (c->initializer);
13342 c->initializer = NULL;
13344 if (init == NULL)
13345 init = c->initializer;
13349 success = true;
13350 for (c = sym->components; c; c = c->next)
13351 if (!resolve_component (c, sym))
13352 success = false;
13354 if (!success)
13355 return false;
13357 if (sym->components)
13358 add_dt_to_dt_list (sym);
13360 return true;
13364 /* Resolve the components of a derived type. This does not have to wait until
13365 resolution stage, but can be done as soon as the dt declaration has been
13366 parsed. */
13368 static bool
13369 resolve_fl_derived0 (gfc_symbol *sym)
13371 gfc_symbol* super_type;
13372 gfc_component *c;
13373 bool success;
13375 if (sym->attr.unlimited_polymorphic)
13376 return true;
13378 super_type = gfc_get_derived_super_type (sym);
13380 /* F2008, C432. */
13381 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
13383 gfc_error ("As extending type %qs at %L has a coarray component, "
13384 "parent type %qs shall also have one", sym->name,
13385 &sym->declared_at, super_type->name);
13386 return false;
13389 /* Ensure the extended type gets resolved before we do. */
13390 if (super_type && !resolve_fl_derived0 (super_type))
13391 return false;
13393 /* An ABSTRACT type must be extensible. */
13394 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
13396 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
13397 sym->name, &sym->declared_at);
13398 return false;
13401 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
13402 : sym->components;
13404 success = true;
13405 for ( ; c != NULL; c = c->next)
13406 if (!resolve_component (c, sym))
13407 success = false;
13409 if (!success)
13410 return false;
13412 check_defined_assignments (sym);
13414 if (!sym->attr.defined_assign_comp && super_type)
13415 sym->attr.defined_assign_comp
13416 = super_type->attr.defined_assign_comp;
13418 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
13419 all DEFERRED bindings are overridden. */
13420 if (super_type && super_type->attr.abstract && !sym->attr.abstract
13421 && !sym->attr.is_class
13422 && !ensure_not_abstract (sym, super_type))
13423 return false;
13425 /* Add derived type to the derived type list. */
13426 add_dt_to_dt_list (sym);
13428 return true;
13432 /* The following procedure does the full resolution of a derived type,
13433 including resolution of all type-bound procedures (if present). In contrast
13434 to 'resolve_fl_derived0' this can only be done after the module has been
13435 parsed completely. */
13437 static bool
13438 resolve_fl_derived (gfc_symbol *sym)
13440 gfc_symbol *gen_dt = NULL;
13442 if (sym->attr.unlimited_polymorphic)
13443 return true;
13445 if (!sym->attr.is_class)
13446 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
13447 if (gen_dt && gen_dt->generic && gen_dt->generic->next
13448 && (!gen_dt->generic->sym->attr.use_assoc
13449 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
13450 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
13451 "%qs at %L being the same name as derived "
13452 "type at %L", sym->name,
13453 gen_dt->generic->sym == sym
13454 ? gen_dt->generic->next->sym->name
13455 : gen_dt->generic->sym->name,
13456 gen_dt->generic->sym == sym
13457 ? &gen_dt->generic->next->sym->declared_at
13458 : &gen_dt->generic->sym->declared_at,
13459 &sym->declared_at))
13460 return false;
13462 /* Resolve the finalizer procedures. */
13463 if (!gfc_resolve_finalizers (sym, NULL))
13464 return false;
13466 if (sym->attr.is_class && sym->ts.u.derived == NULL)
13468 /* Fix up incomplete CLASS symbols. */
13469 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
13470 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
13472 /* Nothing more to do for unlimited polymorphic entities. */
13473 if (data->ts.u.derived->attr.unlimited_polymorphic)
13474 return true;
13475 else if (vptr->ts.u.derived == NULL)
13477 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
13478 gcc_assert (vtab);
13479 vptr->ts.u.derived = vtab->ts.u.derived;
13483 if (!resolve_fl_derived0 (sym))
13484 return false;
13486 /* Resolve the type-bound procedures. */
13487 if (!resolve_typebound_procedures (sym))
13488 return false;
13490 return true;
13494 static bool
13495 resolve_fl_namelist (gfc_symbol *sym)
13497 gfc_namelist *nl;
13498 gfc_symbol *nlsym;
13500 for (nl = sym->namelist; nl; nl = nl->next)
13502 /* Check again, the check in match only works if NAMELIST comes
13503 after the decl. */
13504 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
13506 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
13507 "allowed", nl->sym->name, sym->name, &sym->declared_at);
13508 return false;
13511 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
13512 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13513 "with assumed shape in namelist %qs at %L",
13514 nl->sym->name, sym->name, &sym->declared_at))
13515 return false;
13517 if (is_non_constant_shape_array (nl->sym)
13518 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13519 "with nonconstant shape in namelist %qs at %L",
13520 nl->sym->name, sym->name, &sym->declared_at))
13521 return false;
13523 if (nl->sym->ts.type == BT_CHARACTER
13524 && (nl->sym->ts.u.cl->length == NULL
13525 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
13526 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
13527 "nonconstant character length in "
13528 "namelist %qs at %L", nl->sym->name,
13529 sym->name, &sym->declared_at))
13530 return false;
13532 /* FIXME: Once UDDTIO is implemented, the following can be
13533 removed. */
13534 if (nl->sym->ts.type == BT_CLASS)
13536 gfc_error ("NAMELIST object %qs in namelist %qs at %L is "
13537 "polymorphic and requires a defined input/output "
13538 "procedure", nl->sym->name, sym->name, &sym->declared_at);
13539 return false;
13542 if (nl->sym->ts.type == BT_DERIVED
13543 && (nl->sym->ts.u.derived->attr.alloc_comp
13544 || nl->sym->ts.u.derived->attr.pointer_comp))
13546 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
13547 "namelist %qs at %L with ALLOCATABLE "
13548 "or POINTER components", nl->sym->name,
13549 sym->name, &sym->declared_at))
13550 return false;
13552 /* FIXME: Once UDDTIO is implemented, the following can be
13553 removed. */
13554 gfc_error ("NAMELIST object %qs in namelist %qs at %L has "
13555 "ALLOCATABLE or POINTER components and thus requires "
13556 "a defined input/output procedure", nl->sym->name,
13557 sym->name, &sym->declared_at);
13558 return false;
13562 /* Reject PRIVATE objects in a PUBLIC namelist. */
13563 if (gfc_check_symbol_access (sym))
13565 for (nl = sym->namelist; nl; nl = nl->next)
13567 if (!nl->sym->attr.use_assoc
13568 && !is_sym_host_assoc (nl->sym, sym->ns)
13569 && !gfc_check_symbol_access (nl->sym))
13571 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
13572 "cannot be member of PUBLIC namelist %qs at %L",
13573 nl->sym->name, sym->name, &sym->declared_at);
13574 return false;
13577 /* Types with private components that came here by USE-association. */
13578 if (nl->sym->ts.type == BT_DERIVED
13579 && derived_inaccessible (nl->sym->ts.u.derived))
13581 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
13582 "components and cannot be member of namelist %qs at %L",
13583 nl->sym->name, sym->name, &sym->declared_at);
13584 return false;
13587 /* Types with private components that are defined in the same module. */
13588 if (nl->sym->ts.type == BT_DERIVED
13589 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
13590 && nl->sym->ts.u.derived->attr.private_comp)
13592 gfc_error ("NAMELIST object %qs has PRIVATE components and "
13593 "cannot be a member of PUBLIC namelist %qs at %L",
13594 nl->sym->name, sym->name, &sym->declared_at);
13595 return false;
13601 /* 14.1.2 A module or internal procedure represent local entities
13602 of the same type as a namelist member and so are not allowed. */
13603 for (nl = sym->namelist; nl; nl = nl->next)
13605 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
13606 continue;
13608 if (nl->sym->attr.function && nl->sym == nl->sym->result)
13609 if ((nl->sym == sym->ns->proc_name)
13611 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
13612 continue;
13614 nlsym = NULL;
13615 if (nl->sym->name)
13616 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
13617 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
13619 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
13620 "attribute in %qs at %L", nlsym->name,
13621 &sym->declared_at);
13622 return false;
13626 return true;
13630 static bool
13631 resolve_fl_parameter (gfc_symbol *sym)
13633 /* A parameter array's shape needs to be constant. */
13634 if (sym->as != NULL
13635 && (sym->as->type == AS_DEFERRED
13636 || is_non_constant_shape_array (sym)))
13638 gfc_error ("Parameter array %qs at %L cannot be automatic "
13639 "or of deferred shape", sym->name, &sym->declared_at);
13640 return false;
13643 /* Make sure a parameter that has been implicitly typed still
13644 matches the implicit type, since PARAMETER statements can precede
13645 IMPLICIT statements. */
13646 if (sym->attr.implicit_type
13647 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
13648 sym->ns)))
13650 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
13651 "later IMPLICIT type", sym->name, &sym->declared_at);
13652 return false;
13655 /* Make sure the types of derived parameters are consistent. This
13656 type checking is deferred until resolution because the type may
13657 refer to a derived type from the host. */
13658 if (sym->ts.type == BT_DERIVED
13659 && !gfc_compare_types (&sym->ts, &sym->value->ts))
13661 gfc_error ("Incompatible derived type in PARAMETER at %L",
13662 &sym->value->where);
13663 return false;
13665 return true;
13669 /* Do anything necessary to resolve a symbol. Right now, we just
13670 assume that an otherwise unknown symbol is a variable. This sort
13671 of thing commonly happens for symbols in module. */
13673 static void
13674 resolve_symbol (gfc_symbol *sym)
13676 int check_constant, mp_flag;
13677 gfc_symtree *symtree;
13678 gfc_symtree *this_symtree;
13679 gfc_namespace *ns;
13680 gfc_component *c;
13681 symbol_attribute class_attr;
13682 gfc_array_spec *as;
13683 bool saved_specification_expr;
13685 if (sym->resolved)
13686 return;
13687 sym->resolved = 1;
13689 /* No symbol will ever have union type; only components can be unions.
13690 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
13691 (just like derived type declaration symbols have flavor FL_DERIVED). */
13692 gcc_assert (sym->ts.type != BT_UNION);
13694 if (sym->attr.artificial)
13695 return;
13697 if (sym->attr.unlimited_polymorphic)
13698 return;
13700 if (sym->attr.flavor == FL_UNKNOWN
13701 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
13702 && !sym->attr.generic && !sym->attr.external
13703 && sym->attr.if_source == IFSRC_UNKNOWN
13704 && sym->ts.type == BT_UNKNOWN))
13707 /* If we find that a flavorless symbol is an interface in one of the
13708 parent namespaces, find its symtree in this namespace, free the
13709 symbol and set the symtree to point to the interface symbol. */
13710 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
13712 symtree = gfc_find_symtree (ns->sym_root, sym->name);
13713 if (symtree && (symtree->n.sym->generic ||
13714 (symtree->n.sym->attr.flavor == FL_PROCEDURE
13715 && sym->ns->construct_entities)))
13717 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
13718 sym->name);
13719 if (this_symtree->n.sym == sym)
13721 symtree->n.sym->refs++;
13722 gfc_release_symbol (sym);
13723 this_symtree->n.sym = symtree->n.sym;
13724 return;
13729 /* Otherwise give it a flavor according to such attributes as
13730 it has. */
13731 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
13732 && sym->attr.intrinsic == 0)
13733 sym->attr.flavor = FL_VARIABLE;
13734 else if (sym->attr.flavor == FL_UNKNOWN)
13736 sym->attr.flavor = FL_PROCEDURE;
13737 if (sym->attr.dimension)
13738 sym->attr.function = 1;
13742 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
13743 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
13745 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
13746 && !resolve_procedure_interface (sym))
13747 return;
13749 if (sym->attr.is_protected && !sym->attr.proc_pointer
13750 && (sym->attr.procedure || sym->attr.external))
13752 if (sym->attr.external)
13753 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
13754 "at %L", &sym->declared_at);
13755 else
13756 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
13757 "at %L", &sym->declared_at);
13759 return;
13762 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
13763 return;
13765 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
13766 && !resolve_fl_struct (sym))
13767 return;
13769 /* Symbols that are module procedures with results (functions) have
13770 the types and array specification copied for type checking in
13771 procedures that call them, as well as for saving to a module
13772 file. These symbols can't stand the scrutiny that their results
13773 can. */
13774 mp_flag = (sym->result != NULL && sym->result != sym);
13776 /* Make sure that the intrinsic is consistent with its internal
13777 representation. This needs to be done before assigning a default
13778 type to avoid spurious warnings. */
13779 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
13780 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
13781 return;
13783 /* Resolve associate names. */
13784 if (sym->assoc)
13785 resolve_assoc_var (sym, true);
13787 /* Assign default type to symbols that need one and don't have one. */
13788 if (sym->ts.type == BT_UNKNOWN)
13790 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
13792 gfc_set_default_type (sym, 1, NULL);
13795 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
13796 && !sym->attr.function && !sym->attr.subroutine
13797 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
13798 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
13800 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
13802 /* The specific case of an external procedure should emit an error
13803 in the case that there is no implicit type. */
13804 if (!mp_flag)
13805 gfc_set_default_type (sym, sym->attr.external, NULL);
13806 else
13808 /* Result may be in another namespace. */
13809 resolve_symbol (sym->result);
13811 if (!sym->result->attr.proc_pointer)
13813 sym->ts = sym->result->ts;
13814 sym->as = gfc_copy_array_spec (sym->result->as);
13815 sym->attr.dimension = sym->result->attr.dimension;
13816 sym->attr.pointer = sym->result->attr.pointer;
13817 sym->attr.allocatable = sym->result->attr.allocatable;
13818 sym->attr.contiguous = sym->result->attr.contiguous;
13823 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
13825 bool saved_specification_expr = specification_expr;
13826 specification_expr = true;
13827 gfc_resolve_array_spec (sym->result->as, false);
13828 specification_expr = saved_specification_expr;
13831 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
13833 as = CLASS_DATA (sym)->as;
13834 class_attr = CLASS_DATA (sym)->attr;
13835 class_attr.pointer = class_attr.class_pointer;
13837 else
13839 class_attr = sym->attr;
13840 as = sym->as;
13843 /* F2008, C530. */
13844 if (sym->attr.contiguous
13845 && (!class_attr.dimension
13846 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
13847 && !class_attr.pointer)))
13849 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
13850 "array pointer or an assumed-shape or assumed-rank array",
13851 sym->name, &sym->declared_at);
13852 return;
13855 /* Assumed size arrays and assumed shape arrays must be dummy
13856 arguments. Array-spec's of implied-shape should have been resolved to
13857 AS_EXPLICIT already. */
13859 if (as)
13861 gcc_assert (as->type != AS_IMPLIED_SHAPE);
13862 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
13863 || as->type == AS_ASSUMED_SHAPE)
13864 && !sym->attr.dummy && !sym->attr.select_type_temporary)
13866 if (as->type == AS_ASSUMED_SIZE)
13867 gfc_error ("Assumed size array at %L must be a dummy argument",
13868 &sym->declared_at);
13869 else
13870 gfc_error ("Assumed shape array at %L must be a dummy argument",
13871 &sym->declared_at);
13872 return;
13874 /* TS 29113, C535a. */
13875 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
13876 && !sym->attr.select_type_temporary)
13878 gfc_error ("Assumed-rank array at %L must be a dummy argument",
13879 &sym->declared_at);
13880 return;
13882 if (as->type == AS_ASSUMED_RANK
13883 && (sym->attr.codimension || sym->attr.value))
13885 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
13886 "CODIMENSION attribute", &sym->declared_at);
13887 return;
13891 /* Make sure symbols with known intent or optional are really dummy
13892 variable. Because of ENTRY statement, this has to be deferred
13893 until resolution time. */
13895 if (!sym->attr.dummy
13896 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
13898 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
13899 return;
13902 if (sym->attr.value && !sym->attr.dummy)
13904 gfc_error ("%qs at %L cannot have the VALUE attribute because "
13905 "it is not a dummy argument", sym->name, &sym->declared_at);
13906 return;
13909 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
13911 gfc_charlen *cl = sym->ts.u.cl;
13912 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13914 gfc_error ("Character dummy variable %qs at %L with VALUE "
13915 "attribute must have constant length",
13916 sym->name, &sym->declared_at);
13917 return;
13920 if (sym->ts.is_c_interop
13921 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
13923 gfc_error ("C interoperable character dummy variable %qs at %L "
13924 "with VALUE attribute must have length one",
13925 sym->name, &sym->declared_at);
13926 return;
13930 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13931 && sym->ts.u.derived->attr.generic)
13933 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
13934 if (!sym->ts.u.derived)
13936 gfc_error ("The derived type %qs at %L is of type %qs, "
13937 "which has not been defined", sym->name,
13938 &sym->declared_at, sym->ts.u.derived->name);
13939 sym->ts.type = BT_UNKNOWN;
13940 return;
13944 /* Use the same constraints as TYPE(*), except for the type check
13945 and that only scalars and assumed-size arrays are permitted. */
13946 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
13948 if (!sym->attr.dummy)
13950 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13951 "a dummy argument", sym->name, &sym->declared_at);
13952 return;
13955 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
13956 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
13957 && sym->ts.type != BT_COMPLEX)
13959 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13960 "of type TYPE(*) or of an numeric intrinsic type",
13961 sym->name, &sym->declared_at);
13962 return;
13965 if (sym->attr.allocatable || sym->attr.codimension
13966 || sym->attr.pointer || sym->attr.value)
13968 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13969 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
13970 "attribute", sym->name, &sym->declared_at);
13971 return;
13974 if (sym->attr.intent == INTENT_OUT)
13976 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13977 "have the INTENT(OUT) attribute",
13978 sym->name, &sym->declared_at);
13979 return;
13981 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
13983 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
13984 "either be a scalar or an assumed-size array",
13985 sym->name, &sym->declared_at);
13986 return;
13989 /* Set the type to TYPE(*) and add a dimension(*) to ensure
13990 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
13991 packing. */
13992 sym->ts.type = BT_ASSUMED;
13993 sym->as = gfc_get_array_spec ();
13994 sym->as->type = AS_ASSUMED_SIZE;
13995 sym->as->rank = 1;
13996 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
13998 else if (sym->ts.type == BT_ASSUMED)
14000 /* TS 29113, C407a. */
14001 if (!sym->attr.dummy)
14003 gfc_error ("Assumed type of variable %s at %L is only permitted "
14004 "for dummy variables", sym->name, &sym->declared_at);
14005 return;
14007 if (sym->attr.allocatable || sym->attr.codimension
14008 || sym->attr.pointer || sym->attr.value)
14010 gfc_error ("Assumed-type variable %s at %L may not have the "
14011 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
14012 sym->name, &sym->declared_at);
14013 return;
14015 if (sym->attr.intent == INTENT_OUT)
14017 gfc_error ("Assumed-type variable %s at %L may not have the "
14018 "INTENT(OUT) attribute",
14019 sym->name, &sym->declared_at);
14020 return;
14022 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
14024 gfc_error ("Assumed-type variable %s at %L shall not be an "
14025 "explicit-shape array", sym->name, &sym->declared_at);
14026 return;
14030 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
14031 do this for something that was implicitly typed because that is handled
14032 in gfc_set_default_type. Handle dummy arguments and procedure
14033 definitions separately. Also, anything that is use associated is not
14034 handled here but instead is handled in the module it is declared in.
14035 Finally, derived type definitions are allowed to be BIND(C) since that
14036 only implies that they're interoperable, and they are checked fully for
14037 interoperability when a variable is declared of that type. */
14038 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
14039 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
14040 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
14042 bool t = true;
14044 /* First, make sure the variable is declared at the
14045 module-level scope (J3/04-007, Section 15.3). */
14046 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
14047 sym->attr.in_common == 0)
14049 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
14050 "is neither a COMMON block nor declared at the "
14051 "module level scope", sym->name, &(sym->declared_at));
14052 t = false;
14054 else if (sym->common_head != NULL)
14056 t = verify_com_block_vars_c_interop (sym->common_head);
14058 else
14060 /* If type() declaration, we need to verify that the components
14061 of the given type are all C interoperable, etc. */
14062 if (sym->ts.type == BT_DERIVED &&
14063 sym->ts.u.derived->attr.is_c_interop != 1)
14065 /* Make sure the user marked the derived type as BIND(C). If
14066 not, call the verify routine. This could print an error
14067 for the derived type more than once if multiple variables
14068 of that type are declared. */
14069 if (sym->ts.u.derived->attr.is_bind_c != 1)
14070 verify_bind_c_derived_type (sym->ts.u.derived);
14071 t = false;
14074 /* Verify the variable itself as C interoperable if it
14075 is BIND(C). It is not possible for this to succeed if
14076 the verify_bind_c_derived_type failed, so don't have to handle
14077 any error returned by verify_bind_c_derived_type. */
14078 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
14079 sym->common_block);
14082 if (!t)
14084 /* clear the is_bind_c flag to prevent reporting errors more than
14085 once if something failed. */
14086 sym->attr.is_bind_c = 0;
14087 return;
14091 /* If a derived type symbol has reached this point, without its
14092 type being declared, we have an error. Notice that most
14093 conditions that produce undefined derived types have already
14094 been dealt with. However, the likes of:
14095 implicit type(t) (t) ..... call foo (t) will get us here if
14096 the type is not declared in the scope of the implicit
14097 statement. Change the type to BT_UNKNOWN, both because it is so
14098 and to prevent an ICE. */
14099 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
14100 && sym->ts.u.derived->components == NULL
14101 && !sym->ts.u.derived->attr.zero_comp)
14103 gfc_error ("The derived type %qs at %L is of type %qs, "
14104 "which has not been defined", sym->name,
14105 &sym->declared_at, sym->ts.u.derived->name);
14106 sym->ts.type = BT_UNKNOWN;
14107 return;
14110 /* Make sure that the derived type has been resolved and that the
14111 derived type is visible in the symbol's namespace, if it is a
14112 module function and is not PRIVATE. */
14113 if (sym->ts.type == BT_DERIVED
14114 && sym->ts.u.derived->attr.use_assoc
14115 && sym->ns->proc_name
14116 && sym->ns->proc_name->attr.flavor == FL_MODULE
14117 && !resolve_fl_derived (sym->ts.u.derived))
14118 return;
14120 /* Unless the derived-type declaration is use associated, Fortran 95
14121 does not allow public entries of private derived types.
14122 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
14123 161 in 95-006r3. */
14124 if (sym->ts.type == BT_DERIVED
14125 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
14126 && !sym->ts.u.derived->attr.use_assoc
14127 && gfc_check_symbol_access (sym)
14128 && !gfc_check_symbol_access (sym->ts.u.derived)
14129 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
14130 "derived type %qs",
14131 (sym->attr.flavor == FL_PARAMETER)
14132 ? "parameter" : "variable",
14133 sym->name, &sym->declared_at,
14134 sym->ts.u.derived->name))
14135 return;
14137 /* F2008, C1302. */
14138 if (sym->ts.type == BT_DERIVED
14139 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
14140 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
14141 || sym->ts.u.derived->attr.lock_comp)
14142 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
14144 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
14145 "type LOCK_TYPE must be a coarray", sym->name,
14146 &sym->declared_at);
14147 return;
14150 /* TS18508, C702/C703. */
14151 if (sym->ts.type == BT_DERIVED
14152 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
14153 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
14154 || sym->ts.u.derived->attr.event_comp)
14155 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
14157 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
14158 "type LOCK_TYPE must be a coarray", sym->name,
14159 &sym->declared_at);
14160 return;
14163 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
14164 default initialization is defined (5.1.2.4.4). */
14165 if (sym->ts.type == BT_DERIVED
14166 && sym->attr.dummy
14167 && sym->attr.intent == INTENT_OUT
14168 && sym->as
14169 && sym->as->type == AS_ASSUMED_SIZE)
14171 for (c = sym->ts.u.derived->components; c; c = c->next)
14173 if (c->initializer)
14175 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
14176 "ASSUMED SIZE and so cannot have a default initializer",
14177 sym->name, &sym->declared_at);
14178 return;
14183 /* F2008, C542. */
14184 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
14185 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
14187 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
14188 "INTENT(OUT)", sym->name, &sym->declared_at);
14189 return;
14192 /* TS18508. */
14193 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
14194 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
14196 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
14197 "INTENT(OUT)", sym->name, &sym->declared_at);
14198 return;
14201 /* F2008, C525. */
14202 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14203 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14204 && CLASS_DATA (sym)->attr.coarray_comp))
14205 || class_attr.codimension)
14206 && (sym->attr.result || sym->result == sym))
14208 gfc_error ("Function result %qs at %L shall not be a coarray or have "
14209 "a coarray component", sym->name, &sym->declared_at);
14210 return;
14213 /* F2008, C524. */
14214 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
14215 && sym->ts.u.derived->ts.is_iso_c)
14217 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14218 "shall not be a coarray", sym->name, &sym->declared_at);
14219 return;
14222 /* F2008, C525. */
14223 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14224 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14225 && CLASS_DATA (sym)->attr.coarray_comp))
14226 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
14227 || class_attr.allocatable))
14229 gfc_error ("Variable %qs at %L with coarray component shall be a "
14230 "nonpointer, nonallocatable scalar, which is not a coarray",
14231 sym->name, &sym->declared_at);
14232 return;
14235 /* F2008, C526. The function-result case was handled above. */
14236 if (class_attr.codimension
14237 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
14238 || sym->attr.select_type_temporary
14239 || sym->ns->save_all
14240 || sym->ns->proc_name->attr.flavor == FL_MODULE
14241 || sym->ns->proc_name->attr.is_main_program
14242 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
14244 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
14245 "nor a dummy argument", sym->name, &sym->declared_at);
14246 return;
14248 /* F2008, C528. */
14249 else if (class_attr.codimension && !sym->attr.select_type_temporary
14250 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
14252 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
14253 "deferred shape", sym->name, &sym->declared_at);
14254 return;
14256 else if (class_attr.codimension && class_attr.allocatable && as
14257 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
14259 gfc_error ("Allocatable coarray variable %qs at %L must have "
14260 "deferred shape", sym->name, &sym->declared_at);
14261 return;
14264 /* F2008, C541. */
14265 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14266 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14267 && CLASS_DATA (sym)->attr.coarray_comp))
14268 || (class_attr.codimension && class_attr.allocatable))
14269 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
14271 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
14272 "allocatable coarray or have coarray components",
14273 sym->name, &sym->declared_at);
14274 return;
14277 if (class_attr.codimension && sym->attr.dummy
14278 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
14280 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
14281 "procedure %qs", sym->name, &sym->declared_at,
14282 sym->ns->proc_name->name);
14283 return;
14286 if (sym->ts.type == BT_LOGICAL
14287 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
14288 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
14289 && sym->ns->proc_name->attr.is_bind_c)))
14291 int i;
14292 for (i = 0; gfc_logical_kinds[i].kind; i++)
14293 if (gfc_logical_kinds[i].kind == sym->ts.kind)
14294 break;
14295 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
14296 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
14297 "%L with non-C_Bool kind in BIND(C) procedure "
14298 "%qs", sym->name, &sym->declared_at,
14299 sym->ns->proc_name->name))
14300 return;
14301 else if (!gfc_logical_kinds[i].c_bool
14302 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
14303 "%qs at %L with non-C_Bool kind in "
14304 "BIND(C) procedure %qs", sym->name,
14305 &sym->declared_at,
14306 sym->attr.function ? sym->name
14307 : sym->ns->proc_name->name))
14308 return;
14311 switch (sym->attr.flavor)
14313 case FL_VARIABLE:
14314 if (!resolve_fl_variable (sym, mp_flag))
14315 return;
14316 break;
14318 case FL_PROCEDURE:
14319 if (sym->formal && !sym->formal_ns)
14321 /* Check that none of the arguments are a namelist. */
14322 gfc_formal_arglist *formal = sym->formal;
14324 for (; formal; formal = formal->next)
14325 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
14327 gfc_error ("Namelist '%s' can not be an argument to "
14328 "subroutine or function at %L",
14329 formal->sym->name, &sym->declared_at);
14330 return;
14334 if (!resolve_fl_procedure (sym, mp_flag))
14335 return;
14336 break;
14338 case FL_NAMELIST:
14339 if (!resolve_fl_namelist (sym))
14340 return;
14341 break;
14343 case FL_PARAMETER:
14344 if (!resolve_fl_parameter (sym))
14345 return;
14346 break;
14348 default:
14349 break;
14352 /* Resolve array specifier. Check as well some constraints
14353 on COMMON blocks. */
14355 check_constant = sym->attr.in_common && !sym->attr.pointer;
14357 /* Set the formal_arg_flag so that check_conflict will not throw
14358 an error for host associated variables in the specification
14359 expression for an array_valued function. */
14360 if (sym->attr.function && sym->as)
14361 formal_arg_flag = 1;
14363 saved_specification_expr = specification_expr;
14364 specification_expr = true;
14365 gfc_resolve_array_spec (sym->as, check_constant);
14366 specification_expr = saved_specification_expr;
14368 formal_arg_flag = 0;
14370 /* Resolve formal namespaces. */
14371 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
14372 && !sym->attr.contained && !sym->attr.intrinsic)
14373 gfc_resolve (sym->formal_ns);
14375 /* Make sure the formal namespace is present. */
14376 if (sym->formal && !sym->formal_ns)
14378 gfc_formal_arglist *formal = sym->formal;
14379 while (formal && !formal->sym)
14380 formal = formal->next;
14382 if (formal)
14384 sym->formal_ns = formal->sym->ns;
14385 if (sym->ns != formal->sym->ns)
14386 sym->formal_ns->refs++;
14390 /* Check threadprivate restrictions. */
14391 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
14392 && (!sym->attr.in_common
14393 && sym->module == NULL
14394 && (sym->ns->proc_name == NULL
14395 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
14396 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
14398 /* Check omp declare target restrictions. */
14399 if (sym->attr.omp_declare_target
14400 && sym->attr.flavor == FL_VARIABLE
14401 && !sym->attr.save
14402 && !sym->ns->save_all
14403 && (!sym->attr.in_common
14404 && sym->module == NULL
14405 && (sym->ns->proc_name == NULL
14406 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
14407 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
14408 sym->name, &sym->declared_at);
14410 /* If we have come this far we can apply default-initializers, as
14411 described in 14.7.5, to those variables that have not already
14412 been assigned one. */
14413 if (sym->ts.type == BT_DERIVED
14414 && !sym->value
14415 && !sym->attr.allocatable
14416 && !sym->attr.alloc_comp)
14418 symbol_attribute *a = &sym->attr;
14420 if ((!a->save && !a->dummy && !a->pointer
14421 && !a->in_common && !a->use_assoc
14422 && !a->result && !a->function)
14423 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
14424 apply_default_init (sym);
14425 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
14426 && (sym->ts.u.derived->attr.alloc_comp
14427 || sym->ts.u.derived->attr.pointer_comp))
14428 /* Mark the result symbol to be referenced, when it has allocatable
14429 components. */
14430 sym->result->attr.referenced = 1;
14433 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
14434 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
14435 && !CLASS_DATA (sym)->attr.class_pointer
14436 && !CLASS_DATA (sym)->attr.allocatable)
14437 apply_default_init (sym);
14439 /* If this symbol has a type-spec, check it. */
14440 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
14441 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
14442 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
14443 return;
14447 /************* Resolve DATA statements *************/
14449 static struct
14451 gfc_data_value *vnode;
14452 mpz_t left;
14454 values;
14457 /* Advance the values structure to point to the next value in the data list. */
14459 static bool
14460 next_data_value (void)
14462 while (mpz_cmp_ui (values.left, 0) == 0)
14465 if (values.vnode->next == NULL)
14466 return false;
14468 values.vnode = values.vnode->next;
14469 mpz_set (values.left, values.vnode->repeat);
14472 return true;
14476 static bool
14477 check_data_variable (gfc_data_variable *var, locus *where)
14479 gfc_expr *e;
14480 mpz_t size;
14481 mpz_t offset;
14482 bool t;
14483 ar_type mark = AR_UNKNOWN;
14484 int i;
14485 mpz_t section_index[GFC_MAX_DIMENSIONS];
14486 gfc_ref *ref;
14487 gfc_array_ref *ar;
14488 gfc_symbol *sym;
14489 int has_pointer;
14491 if (!gfc_resolve_expr (var->expr))
14492 return false;
14494 ar = NULL;
14495 mpz_init_set_si (offset, 0);
14496 e = var->expr;
14498 if (e->expr_type != EXPR_VARIABLE)
14499 gfc_internal_error ("check_data_variable(): Bad expression");
14501 sym = e->symtree->n.sym;
14503 if (sym->ns->is_block_data && !sym->attr.in_common)
14505 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
14506 sym->name, &sym->declared_at);
14509 if (e->ref == NULL && sym->as)
14511 gfc_error ("DATA array %qs at %L must be specified in a previous"
14512 " declaration", sym->name, where);
14513 return false;
14516 has_pointer = sym->attr.pointer;
14518 if (gfc_is_coindexed (e))
14520 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
14521 where);
14522 return false;
14525 for (ref = e->ref; ref; ref = ref->next)
14527 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
14528 has_pointer = 1;
14530 if (has_pointer
14531 && ref->type == REF_ARRAY
14532 && ref->u.ar.type != AR_FULL)
14534 gfc_error ("DATA element %qs at %L is a pointer and so must "
14535 "be a full array", sym->name, where);
14536 return false;
14540 if (e->rank == 0 || has_pointer)
14542 mpz_init_set_ui (size, 1);
14543 ref = NULL;
14545 else
14547 ref = e->ref;
14549 /* Find the array section reference. */
14550 for (ref = e->ref; ref; ref = ref->next)
14552 if (ref->type != REF_ARRAY)
14553 continue;
14554 if (ref->u.ar.type == AR_ELEMENT)
14555 continue;
14556 break;
14558 gcc_assert (ref);
14560 /* Set marks according to the reference pattern. */
14561 switch (ref->u.ar.type)
14563 case AR_FULL:
14564 mark = AR_FULL;
14565 break;
14567 case AR_SECTION:
14568 ar = &ref->u.ar;
14569 /* Get the start position of array section. */
14570 gfc_get_section_index (ar, section_index, &offset);
14571 mark = AR_SECTION;
14572 break;
14574 default:
14575 gcc_unreachable ();
14578 if (!gfc_array_size (e, &size))
14580 gfc_error ("Nonconstant array section at %L in DATA statement",
14581 &e->where);
14582 mpz_clear (offset);
14583 return false;
14587 t = true;
14589 while (mpz_cmp_ui (size, 0) > 0)
14591 if (!next_data_value ())
14593 gfc_error ("DATA statement at %L has more variables than values",
14594 where);
14595 t = false;
14596 break;
14599 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
14600 if (!t)
14601 break;
14603 /* If we have more than one element left in the repeat count,
14604 and we have more than one element left in the target variable,
14605 then create a range assignment. */
14606 /* FIXME: Only done for full arrays for now, since array sections
14607 seem tricky. */
14608 if (mark == AR_FULL && ref && ref->next == NULL
14609 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
14611 mpz_t range;
14613 if (mpz_cmp (size, values.left) >= 0)
14615 mpz_init_set (range, values.left);
14616 mpz_sub (size, size, values.left);
14617 mpz_set_ui (values.left, 0);
14619 else
14621 mpz_init_set (range, size);
14622 mpz_sub (values.left, values.left, size);
14623 mpz_set_ui (size, 0);
14626 t = gfc_assign_data_value (var->expr, values.vnode->expr,
14627 offset, &range);
14629 mpz_add (offset, offset, range);
14630 mpz_clear (range);
14632 if (!t)
14633 break;
14636 /* Assign initial value to symbol. */
14637 else
14639 mpz_sub_ui (values.left, values.left, 1);
14640 mpz_sub_ui (size, size, 1);
14642 t = gfc_assign_data_value (var->expr, values.vnode->expr,
14643 offset, NULL);
14644 if (!t)
14645 break;
14647 if (mark == AR_FULL)
14648 mpz_add_ui (offset, offset, 1);
14650 /* Modify the array section indexes and recalculate the offset
14651 for next element. */
14652 else if (mark == AR_SECTION)
14653 gfc_advance_section (section_index, ar, &offset);
14657 if (mark == AR_SECTION)
14659 for (i = 0; i < ar->dimen; i++)
14660 mpz_clear (section_index[i]);
14663 mpz_clear (size);
14664 mpz_clear (offset);
14666 return t;
14670 static bool traverse_data_var (gfc_data_variable *, locus *);
14672 /* Iterate over a list of elements in a DATA statement. */
14674 static bool
14675 traverse_data_list (gfc_data_variable *var, locus *where)
14677 mpz_t trip;
14678 iterator_stack frame;
14679 gfc_expr *e, *start, *end, *step;
14680 bool retval = true;
14682 mpz_init (frame.value);
14683 mpz_init (trip);
14685 start = gfc_copy_expr (var->iter.start);
14686 end = gfc_copy_expr (var->iter.end);
14687 step = gfc_copy_expr (var->iter.step);
14689 if (!gfc_simplify_expr (start, 1)
14690 || start->expr_type != EXPR_CONSTANT)
14692 gfc_error ("start of implied-do loop at %L could not be "
14693 "simplified to a constant value", &start->where);
14694 retval = false;
14695 goto cleanup;
14697 if (!gfc_simplify_expr (end, 1)
14698 || end->expr_type != EXPR_CONSTANT)
14700 gfc_error ("end of implied-do loop at %L could not be "
14701 "simplified to a constant value", &start->where);
14702 retval = false;
14703 goto cleanup;
14705 if (!gfc_simplify_expr (step, 1)
14706 || step->expr_type != EXPR_CONSTANT)
14708 gfc_error ("step of implied-do loop at %L could not be "
14709 "simplified to a constant value", &start->where);
14710 retval = false;
14711 goto cleanup;
14714 mpz_set (trip, end->value.integer);
14715 mpz_sub (trip, trip, start->value.integer);
14716 mpz_add (trip, trip, step->value.integer);
14718 mpz_div (trip, trip, step->value.integer);
14720 mpz_set (frame.value, start->value.integer);
14722 frame.prev = iter_stack;
14723 frame.variable = var->iter.var->symtree;
14724 iter_stack = &frame;
14726 while (mpz_cmp_ui (trip, 0) > 0)
14728 if (!traverse_data_var (var->list, where))
14730 retval = false;
14731 goto cleanup;
14734 e = gfc_copy_expr (var->expr);
14735 if (!gfc_simplify_expr (e, 1))
14737 gfc_free_expr (e);
14738 retval = false;
14739 goto cleanup;
14742 mpz_add (frame.value, frame.value, step->value.integer);
14744 mpz_sub_ui (trip, trip, 1);
14747 cleanup:
14748 mpz_clear (frame.value);
14749 mpz_clear (trip);
14751 gfc_free_expr (start);
14752 gfc_free_expr (end);
14753 gfc_free_expr (step);
14755 iter_stack = frame.prev;
14756 return retval;
14760 /* Type resolve variables in the variable list of a DATA statement. */
14762 static bool
14763 traverse_data_var (gfc_data_variable *var, locus *where)
14765 bool t;
14767 for (; var; var = var->next)
14769 if (var->expr == NULL)
14770 t = traverse_data_list (var, where);
14771 else
14772 t = check_data_variable (var, where);
14774 if (!t)
14775 return false;
14778 return true;
14782 /* Resolve the expressions and iterators associated with a data statement.
14783 This is separate from the assignment checking because data lists should
14784 only be resolved once. */
14786 static bool
14787 resolve_data_variables (gfc_data_variable *d)
14789 for (; d; d = d->next)
14791 if (d->list == NULL)
14793 if (!gfc_resolve_expr (d->expr))
14794 return false;
14796 else
14798 if (!gfc_resolve_iterator (&d->iter, false, true))
14799 return false;
14801 if (!resolve_data_variables (d->list))
14802 return false;
14806 return true;
14810 /* Resolve a single DATA statement. We implement this by storing a pointer to
14811 the value list into static variables, and then recursively traversing the
14812 variables list, expanding iterators and such. */
14814 static void
14815 resolve_data (gfc_data *d)
14818 if (!resolve_data_variables (d->var))
14819 return;
14821 values.vnode = d->value;
14822 if (d->value == NULL)
14823 mpz_set_ui (values.left, 0);
14824 else
14825 mpz_set (values.left, d->value->repeat);
14827 if (!traverse_data_var (d->var, &d->where))
14828 return;
14830 /* At this point, we better not have any values left. */
14832 if (next_data_value ())
14833 gfc_error ("DATA statement at %L has more values than variables",
14834 &d->where);
14838 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
14839 accessed by host or use association, is a dummy argument to a pure function,
14840 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
14841 is storage associated with any such variable, shall not be used in the
14842 following contexts: (clients of this function). */
14844 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
14845 procedure. Returns zero if assignment is OK, nonzero if there is a
14846 problem. */
14848 gfc_impure_variable (gfc_symbol *sym)
14850 gfc_symbol *proc;
14851 gfc_namespace *ns;
14853 if (sym->attr.use_assoc || sym->attr.in_common)
14854 return 1;
14856 /* Check if the symbol's ns is inside the pure procedure. */
14857 for (ns = gfc_current_ns; ns; ns = ns->parent)
14859 if (ns == sym->ns)
14860 break;
14861 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
14862 return 1;
14865 proc = sym->ns->proc_name;
14866 if (sym->attr.dummy
14867 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
14868 || proc->attr.function))
14869 return 1;
14871 /* TODO: Sort out what can be storage associated, if anything, and include
14872 it here. In principle equivalences should be scanned but it does not
14873 seem to be possible to storage associate an impure variable this way. */
14874 return 0;
14878 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
14879 current namespace is inside a pure procedure. */
14882 gfc_pure (gfc_symbol *sym)
14884 symbol_attribute attr;
14885 gfc_namespace *ns;
14887 if (sym == NULL)
14889 /* Check if the current namespace or one of its parents
14890 belongs to a pure procedure. */
14891 for (ns = gfc_current_ns; ns; ns = ns->parent)
14893 sym = ns->proc_name;
14894 if (sym == NULL)
14895 return 0;
14896 attr = sym->attr;
14897 if (attr.flavor == FL_PROCEDURE && attr.pure)
14898 return 1;
14900 return 0;
14903 attr = sym->attr;
14905 return attr.flavor == FL_PROCEDURE && attr.pure;
14909 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
14910 checks if the current namespace is implicitly pure. Note that this
14911 function returns false for a PURE procedure. */
14914 gfc_implicit_pure (gfc_symbol *sym)
14916 gfc_namespace *ns;
14918 if (sym == NULL)
14920 /* Check if the current procedure is implicit_pure. Walk up
14921 the procedure list until we find a procedure. */
14922 for (ns = gfc_current_ns; ns; ns = ns->parent)
14924 sym = ns->proc_name;
14925 if (sym == NULL)
14926 return 0;
14928 if (sym->attr.flavor == FL_PROCEDURE)
14929 break;
14933 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
14934 && !sym->attr.pure;
14938 void
14939 gfc_unset_implicit_pure (gfc_symbol *sym)
14941 gfc_namespace *ns;
14943 if (sym == NULL)
14945 /* Check if the current procedure is implicit_pure. Walk up
14946 the procedure list until we find a procedure. */
14947 for (ns = gfc_current_ns; ns; ns = ns->parent)
14949 sym = ns->proc_name;
14950 if (sym == NULL)
14951 return;
14953 if (sym->attr.flavor == FL_PROCEDURE)
14954 break;
14958 if (sym->attr.flavor == FL_PROCEDURE)
14959 sym->attr.implicit_pure = 0;
14960 else
14961 sym->attr.pure = 0;
14965 /* Test whether the current procedure is elemental or not. */
14968 gfc_elemental (gfc_symbol *sym)
14970 symbol_attribute attr;
14972 if (sym == NULL)
14973 sym = gfc_current_ns->proc_name;
14974 if (sym == NULL)
14975 return 0;
14976 attr = sym->attr;
14978 return attr.flavor == FL_PROCEDURE && attr.elemental;
14982 /* Warn about unused labels. */
14984 static void
14985 warn_unused_fortran_label (gfc_st_label *label)
14987 if (label == NULL)
14988 return;
14990 warn_unused_fortran_label (label->left);
14992 if (label->defined == ST_LABEL_UNKNOWN)
14993 return;
14995 switch (label->referenced)
14997 case ST_LABEL_UNKNOWN:
14998 gfc_warning (0, "Label %d at %L defined but not used", label->value,
14999 &label->where);
15000 break;
15002 case ST_LABEL_BAD_TARGET:
15003 gfc_warning (0, "Label %d at %L defined but cannot be used",
15004 label->value, &label->where);
15005 break;
15007 default:
15008 break;
15011 warn_unused_fortran_label (label->right);
15015 /* Returns the sequence type of a symbol or sequence. */
15017 static seq_type
15018 sequence_type (gfc_typespec ts)
15020 seq_type result;
15021 gfc_component *c;
15023 switch (ts.type)
15025 case BT_DERIVED:
15027 if (ts.u.derived->components == NULL)
15028 return SEQ_NONDEFAULT;
15030 result = sequence_type (ts.u.derived->components->ts);
15031 for (c = ts.u.derived->components->next; c; c = c->next)
15032 if (sequence_type (c->ts) != result)
15033 return SEQ_MIXED;
15035 return result;
15037 case BT_CHARACTER:
15038 if (ts.kind != gfc_default_character_kind)
15039 return SEQ_NONDEFAULT;
15041 return SEQ_CHARACTER;
15043 case BT_INTEGER:
15044 if (ts.kind != gfc_default_integer_kind)
15045 return SEQ_NONDEFAULT;
15047 return SEQ_NUMERIC;
15049 case BT_REAL:
15050 if (!(ts.kind == gfc_default_real_kind
15051 || ts.kind == gfc_default_double_kind))
15052 return SEQ_NONDEFAULT;
15054 return SEQ_NUMERIC;
15056 case BT_COMPLEX:
15057 if (ts.kind != gfc_default_complex_kind)
15058 return SEQ_NONDEFAULT;
15060 return SEQ_NUMERIC;
15062 case BT_LOGICAL:
15063 if (ts.kind != gfc_default_logical_kind)
15064 return SEQ_NONDEFAULT;
15066 return SEQ_NUMERIC;
15068 default:
15069 return SEQ_NONDEFAULT;
15074 /* Resolve derived type EQUIVALENCE object. */
15076 static bool
15077 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
15079 gfc_component *c = derived->components;
15081 if (!derived)
15082 return true;
15084 /* Shall not be an object of nonsequence derived type. */
15085 if (!derived->attr.sequence)
15087 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
15088 "attribute to be an EQUIVALENCE object", sym->name,
15089 &e->where);
15090 return false;
15093 /* Shall not have allocatable components. */
15094 if (derived->attr.alloc_comp)
15096 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
15097 "components to be an EQUIVALENCE object",sym->name,
15098 &e->where);
15099 return false;
15102 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
15104 gfc_error ("Derived type variable %qs at %L with default "
15105 "initialization cannot be in EQUIVALENCE with a variable "
15106 "in COMMON", sym->name, &e->where);
15107 return false;
15110 for (; c ; c = c->next)
15112 if (gfc_bt_struct (c->ts.type)
15113 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
15114 return false;
15116 /* Shall not be an object of sequence derived type containing a pointer
15117 in the structure. */
15118 if (c->attr.pointer)
15120 gfc_error ("Derived type variable %qs at %L with pointer "
15121 "component(s) cannot be an EQUIVALENCE object",
15122 sym->name, &e->where);
15123 return false;
15126 return true;
15130 /* Resolve equivalence object.
15131 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
15132 an allocatable array, an object of nonsequence derived type, an object of
15133 sequence derived type containing a pointer at any level of component
15134 selection, an automatic object, a function name, an entry name, a result
15135 name, a named constant, a structure component, or a subobject of any of
15136 the preceding objects. A substring shall not have length zero. A
15137 derived type shall not have components with default initialization nor
15138 shall two objects of an equivalence group be initialized.
15139 Either all or none of the objects shall have an protected attribute.
15140 The simple constraints are done in symbol.c(check_conflict) and the rest
15141 are implemented here. */
15143 static void
15144 resolve_equivalence (gfc_equiv *eq)
15146 gfc_symbol *sym;
15147 gfc_symbol *first_sym;
15148 gfc_expr *e;
15149 gfc_ref *r;
15150 locus *last_where = NULL;
15151 seq_type eq_type, last_eq_type;
15152 gfc_typespec *last_ts;
15153 int object, cnt_protected;
15154 const char *msg;
15156 last_ts = &eq->expr->symtree->n.sym->ts;
15158 first_sym = eq->expr->symtree->n.sym;
15160 cnt_protected = 0;
15162 for (object = 1; eq; eq = eq->eq, object++)
15164 e = eq->expr;
15166 e->ts = e->symtree->n.sym->ts;
15167 /* match_varspec might not know yet if it is seeing
15168 array reference or substring reference, as it doesn't
15169 know the types. */
15170 if (e->ref && e->ref->type == REF_ARRAY)
15172 gfc_ref *ref = e->ref;
15173 sym = e->symtree->n.sym;
15175 if (sym->attr.dimension)
15177 ref->u.ar.as = sym->as;
15178 ref = ref->next;
15181 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
15182 if (e->ts.type == BT_CHARACTER
15183 && ref
15184 && ref->type == REF_ARRAY
15185 && ref->u.ar.dimen == 1
15186 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
15187 && ref->u.ar.stride[0] == NULL)
15189 gfc_expr *start = ref->u.ar.start[0];
15190 gfc_expr *end = ref->u.ar.end[0];
15191 void *mem = NULL;
15193 /* Optimize away the (:) reference. */
15194 if (start == NULL && end == NULL)
15196 if (e->ref == ref)
15197 e->ref = ref->next;
15198 else
15199 e->ref->next = ref->next;
15200 mem = ref;
15202 else
15204 ref->type = REF_SUBSTRING;
15205 if (start == NULL)
15206 start = gfc_get_int_expr (gfc_default_integer_kind,
15207 NULL, 1);
15208 ref->u.ss.start = start;
15209 if (end == NULL && e->ts.u.cl)
15210 end = gfc_copy_expr (e->ts.u.cl->length);
15211 ref->u.ss.end = end;
15212 ref->u.ss.length = e->ts.u.cl;
15213 e->ts.u.cl = NULL;
15215 ref = ref->next;
15216 free (mem);
15219 /* Any further ref is an error. */
15220 if (ref)
15222 gcc_assert (ref->type == REF_ARRAY);
15223 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
15224 &ref->u.ar.where);
15225 continue;
15229 if (!gfc_resolve_expr (e))
15230 continue;
15232 sym = e->symtree->n.sym;
15234 if (sym->attr.is_protected)
15235 cnt_protected++;
15236 if (cnt_protected > 0 && cnt_protected != object)
15238 gfc_error ("Either all or none of the objects in the "
15239 "EQUIVALENCE set at %L shall have the "
15240 "PROTECTED attribute",
15241 &e->where);
15242 break;
15245 /* Shall not equivalence common block variables in a PURE procedure. */
15246 if (sym->ns->proc_name
15247 && sym->ns->proc_name->attr.pure
15248 && sym->attr.in_common)
15250 gfc_error ("Common block member %qs at %L cannot be an EQUIVALENCE "
15251 "object in the pure procedure %qs",
15252 sym->name, &e->where, sym->ns->proc_name->name);
15253 break;
15256 /* Shall not be a named constant. */
15257 if (e->expr_type == EXPR_CONSTANT)
15259 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
15260 "object", sym->name, &e->where);
15261 continue;
15264 if (e->ts.type == BT_DERIVED
15265 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
15266 continue;
15268 /* Check that the types correspond correctly:
15269 Note 5.28:
15270 A numeric sequence structure may be equivalenced to another sequence
15271 structure, an object of default integer type, default real type, double
15272 precision real type, default logical type such that components of the
15273 structure ultimately only become associated to objects of the same
15274 kind. A character sequence structure may be equivalenced to an object
15275 of default character kind or another character sequence structure.
15276 Other objects may be equivalenced only to objects of the same type and
15277 kind parameters. */
15279 /* Identical types are unconditionally OK. */
15280 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
15281 goto identical_types;
15283 last_eq_type = sequence_type (*last_ts);
15284 eq_type = sequence_type (sym->ts);
15286 /* Since the pair of objects is not of the same type, mixed or
15287 non-default sequences can be rejected. */
15289 msg = "Sequence %s with mixed components in EQUIVALENCE "
15290 "statement at %L with different type objects";
15291 if ((object ==2
15292 && last_eq_type == SEQ_MIXED
15293 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
15294 || (eq_type == SEQ_MIXED
15295 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
15296 continue;
15298 msg = "Non-default type object or sequence %s in EQUIVALENCE "
15299 "statement at %L with objects of different type";
15300 if ((object ==2
15301 && last_eq_type == SEQ_NONDEFAULT
15302 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
15303 || (eq_type == SEQ_NONDEFAULT
15304 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
15305 continue;
15307 msg ="Non-CHARACTER object %qs in default CHARACTER "
15308 "EQUIVALENCE statement at %L";
15309 if (last_eq_type == SEQ_CHARACTER
15310 && eq_type != SEQ_CHARACTER
15311 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
15312 continue;
15314 msg ="Non-NUMERIC object %qs in default NUMERIC "
15315 "EQUIVALENCE statement at %L";
15316 if (last_eq_type == SEQ_NUMERIC
15317 && eq_type != SEQ_NUMERIC
15318 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
15319 continue;
15321 identical_types:
15322 last_ts =&sym->ts;
15323 last_where = &e->where;
15325 if (!e->ref)
15326 continue;
15328 /* Shall not be an automatic array. */
15329 if (e->ref->type == REF_ARRAY
15330 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
15332 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
15333 "an EQUIVALENCE object", sym->name, &e->where);
15334 continue;
15337 r = e->ref;
15338 while (r)
15340 /* Shall not be a structure component. */
15341 if (r->type == REF_COMPONENT)
15343 gfc_error ("Structure component %qs at %L cannot be an "
15344 "EQUIVALENCE object",
15345 r->u.c.component->name, &e->where);
15346 break;
15349 /* A substring shall not have length zero. */
15350 if (r->type == REF_SUBSTRING)
15352 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
15354 gfc_error ("Substring at %L has length zero",
15355 &r->u.ss.start->where);
15356 break;
15359 r = r->next;
15365 /* Resolve function and ENTRY types, issue diagnostics if needed. */
15367 static void
15368 resolve_fntype (gfc_namespace *ns)
15370 gfc_entry_list *el;
15371 gfc_symbol *sym;
15373 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
15374 return;
15376 /* If there are any entries, ns->proc_name is the entry master
15377 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
15378 if (ns->entries)
15379 sym = ns->entries->sym;
15380 else
15381 sym = ns->proc_name;
15382 if (sym->result == sym
15383 && sym->ts.type == BT_UNKNOWN
15384 && !gfc_set_default_type (sym, 0, NULL)
15385 && !sym->attr.untyped)
15387 gfc_error ("Function %qs at %L has no IMPLICIT type",
15388 sym->name, &sym->declared_at);
15389 sym->attr.untyped = 1;
15392 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
15393 && !sym->attr.contained
15394 && !gfc_check_symbol_access (sym->ts.u.derived)
15395 && gfc_check_symbol_access (sym))
15397 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
15398 "%L of PRIVATE type %qs", sym->name,
15399 &sym->declared_at, sym->ts.u.derived->name);
15402 if (ns->entries)
15403 for (el = ns->entries->next; el; el = el->next)
15405 if (el->sym->result == el->sym
15406 && el->sym->ts.type == BT_UNKNOWN
15407 && !gfc_set_default_type (el->sym, 0, NULL)
15408 && !el->sym->attr.untyped)
15410 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
15411 el->sym->name, &el->sym->declared_at);
15412 el->sym->attr.untyped = 1;
15418 /* 12.3.2.1.1 Defined operators. */
15420 static bool
15421 check_uop_procedure (gfc_symbol *sym, locus where)
15423 gfc_formal_arglist *formal;
15425 if (!sym->attr.function)
15427 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
15428 sym->name, &where);
15429 return false;
15432 if (sym->ts.type == BT_CHARACTER
15433 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
15434 && !(sym->result && ((sym->result->ts.u.cl
15435 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
15437 gfc_error ("User operator procedure %qs at %L cannot be assumed "
15438 "character length", sym->name, &where);
15439 return false;
15442 formal = gfc_sym_get_dummy_args (sym);
15443 if (!formal || !formal->sym)
15445 gfc_error ("User operator procedure %qs at %L must have at least "
15446 "one argument", sym->name, &where);
15447 return false;
15450 if (formal->sym->attr.intent != INTENT_IN)
15452 gfc_error ("First argument of operator interface at %L must be "
15453 "INTENT(IN)", &where);
15454 return false;
15457 if (formal->sym->attr.optional)
15459 gfc_error ("First argument of operator interface at %L cannot be "
15460 "optional", &where);
15461 return false;
15464 formal = formal->next;
15465 if (!formal || !formal->sym)
15466 return true;
15468 if (formal->sym->attr.intent != INTENT_IN)
15470 gfc_error ("Second argument of operator interface at %L must be "
15471 "INTENT(IN)", &where);
15472 return false;
15475 if (formal->sym->attr.optional)
15477 gfc_error ("Second argument of operator interface at %L cannot be "
15478 "optional", &where);
15479 return false;
15482 if (formal->next)
15484 gfc_error ("Operator interface at %L must have, at most, two "
15485 "arguments", &where);
15486 return false;
15489 return true;
15492 static void
15493 gfc_resolve_uops (gfc_symtree *symtree)
15495 gfc_interface *itr;
15497 if (symtree == NULL)
15498 return;
15500 gfc_resolve_uops (symtree->left);
15501 gfc_resolve_uops (symtree->right);
15503 for (itr = symtree->n.uop->op; itr; itr = itr->next)
15504 check_uop_procedure (itr->sym, itr->sym->declared_at);
15508 /* Examine all of the expressions associated with a program unit,
15509 assign types to all intermediate expressions, make sure that all
15510 assignments are to compatible types and figure out which names
15511 refer to which functions or subroutines. It doesn't check code
15512 block, which is handled by gfc_resolve_code. */
15514 static void
15515 resolve_types (gfc_namespace *ns)
15517 gfc_namespace *n;
15518 gfc_charlen *cl;
15519 gfc_data *d;
15520 gfc_equiv *eq;
15521 gfc_namespace* old_ns = gfc_current_ns;
15523 if (ns->types_resolved)
15524 return;
15526 /* Check that all IMPLICIT types are ok. */
15527 if (!ns->seen_implicit_none)
15529 unsigned letter;
15530 for (letter = 0; letter != GFC_LETTERS; ++letter)
15531 if (ns->set_flag[letter]
15532 && !resolve_typespec_used (&ns->default_type[letter],
15533 &ns->implicit_loc[letter], NULL))
15534 return;
15537 gfc_current_ns = ns;
15539 resolve_entries (ns);
15541 resolve_common_vars (&ns->blank_common, false);
15542 resolve_common_blocks (ns->common_root);
15544 resolve_contained_functions (ns);
15546 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
15547 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
15548 resolve_formal_arglist (ns->proc_name);
15550 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
15552 for (cl = ns->cl_list; cl; cl = cl->next)
15553 resolve_charlen (cl);
15555 gfc_traverse_ns (ns, resolve_symbol);
15557 resolve_fntype (ns);
15559 for (n = ns->contained; n; n = n->sibling)
15561 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
15562 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
15563 "also be PURE", n->proc_name->name,
15564 &n->proc_name->declared_at);
15566 resolve_types (n);
15569 forall_flag = 0;
15570 gfc_do_concurrent_flag = 0;
15571 gfc_check_interfaces (ns);
15573 gfc_traverse_ns (ns, resolve_values);
15575 if (ns->save_all)
15576 gfc_save_all (ns);
15578 iter_stack = NULL;
15579 for (d = ns->data; d; d = d->next)
15580 resolve_data (d);
15582 iter_stack = NULL;
15583 gfc_traverse_ns (ns, gfc_formalize_init_value);
15585 gfc_traverse_ns (ns, gfc_verify_binding_labels);
15587 for (eq = ns->equiv; eq; eq = eq->next)
15588 resolve_equivalence (eq);
15590 /* Warn about unused labels. */
15591 if (warn_unused_label)
15592 warn_unused_fortran_label (ns->st_labels);
15594 gfc_resolve_uops (ns->uop_root);
15596 gfc_resolve_omp_declare_simd (ns);
15598 gfc_resolve_omp_udrs (ns->omp_udr_root);
15600 ns->types_resolved = 1;
15602 gfc_current_ns = old_ns;
15606 /* Call gfc_resolve_code recursively. */
15608 static void
15609 resolve_codes (gfc_namespace *ns)
15611 gfc_namespace *n;
15612 bitmap_obstack old_obstack;
15614 if (ns->resolved == 1)
15615 return;
15617 for (n = ns->contained; n; n = n->sibling)
15618 resolve_codes (n);
15620 gfc_current_ns = ns;
15622 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
15623 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
15624 cs_base = NULL;
15626 /* Set to an out of range value. */
15627 current_entry_id = -1;
15629 old_obstack = labels_obstack;
15630 bitmap_obstack_initialize (&labels_obstack);
15632 gfc_resolve_oacc_declare (ns);
15633 gfc_resolve_code (ns->code, ns);
15635 bitmap_obstack_release (&labels_obstack);
15636 labels_obstack = old_obstack;
15640 /* This function is called after a complete program unit has been compiled.
15641 Its purpose is to examine all of the expressions associated with a program
15642 unit, assign types to all intermediate expressions, make sure that all
15643 assignments are to compatible types and figure out which names refer to
15644 which functions or subroutines. */
15646 void
15647 gfc_resolve (gfc_namespace *ns)
15649 gfc_namespace *old_ns;
15650 code_stack *old_cs_base;
15651 struct gfc_omp_saved_state old_omp_state;
15653 if (ns->resolved)
15654 return;
15656 ns->resolved = -1;
15657 old_ns = gfc_current_ns;
15658 old_cs_base = cs_base;
15660 /* As gfc_resolve can be called during resolution of an OpenMP construct
15661 body, we should clear any state associated to it, so that say NS's
15662 DO loops are not interpreted as OpenMP loops. */
15663 gfc_omp_save_and_clear_state (&old_omp_state);
15665 resolve_types (ns);
15666 component_assignment_level = 0;
15667 resolve_codes (ns);
15669 gfc_current_ns = old_ns;
15670 cs_base = old_cs_base;
15671 ns->resolved = 1;
15673 gfc_run_passes (ns);
15675 gfc_omp_restore_state (&old_omp_state);