libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / fortran / resolve.cc
blobebe449e71190764dddf7f0017201e43eb2994541
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2024 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;
76 /* True if we are resolving a specification expression. */
77 static bool specification_expr = false;
79 /* The id of the last entry seen. */
80 static int current_entry_id;
82 /* We use bitmaps to determine if a branch target is valid. */
83 static bitmap_obstack labels_obstack;
85 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
86 static bool inquiry_argument = false;
89 /* Is the symbol host associated? */
90 static bool
91 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
93 for (ns = ns->parent; ns; ns = ns->parent)
95 if (sym->ns == ns)
96 return true;
99 return false;
102 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
103 an ABSTRACT derived-type. If where is not NULL, an error message with that
104 locus is printed, optionally using name. */
106 static bool
107 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
109 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
111 if (where)
113 if (name)
114 gfc_error ("%qs at %L is of the ABSTRACT type %qs",
115 name, where, ts->u.derived->name);
116 else
117 gfc_error ("ABSTRACT type %qs used at %L",
118 ts->u.derived->name, where);
121 return false;
124 return true;
128 static bool
129 check_proc_interface (gfc_symbol *ifc, locus *where)
131 /* Several checks for F08:C1216. */
132 if (ifc->attr.procedure)
134 gfc_error ("Interface %qs at %L is declared "
135 "in a later PROCEDURE statement", ifc->name, where);
136 return false;
138 if (ifc->generic)
140 /* For generic interfaces, check if there is
141 a specific procedure with the same name. */
142 gfc_interface *gen = ifc->generic;
143 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
144 gen = gen->next;
145 if (!gen)
147 gfc_error ("Interface %qs at %L may not be generic",
148 ifc->name, where);
149 return false;
152 if (ifc->attr.proc == PROC_ST_FUNCTION)
154 gfc_error ("Interface %qs at %L may not be a statement function",
155 ifc->name, where);
156 return false;
158 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
159 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
160 ifc->attr.intrinsic = 1;
161 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
163 gfc_error ("Intrinsic procedure %qs not allowed in "
164 "PROCEDURE statement at %L", ifc->name, where);
165 return false;
167 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
169 gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
170 return false;
172 return true;
176 static void resolve_symbol (gfc_symbol *sym);
179 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
181 static bool
182 resolve_procedure_interface (gfc_symbol *sym)
184 gfc_symbol *ifc = sym->ts.interface;
186 if (!ifc)
187 return true;
189 if (ifc == sym)
191 gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
192 sym->name, &sym->declared_at);
193 return false;
195 if (!check_proc_interface (ifc, &sym->declared_at))
196 return false;
198 if (ifc->attr.if_source || ifc->attr.intrinsic)
200 /* Resolve interface and copy attributes. */
201 resolve_symbol (ifc);
202 if (ifc->attr.intrinsic)
203 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
205 if (ifc->result)
207 sym->ts = ifc->result->ts;
208 sym->attr.allocatable = ifc->result->attr.allocatable;
209 sym->attr.pointer = ifc->result->attr.pointer;
210 sym->attr.dimension = ifc->result->attr.dimension;
211 sym->attr.class_ok = ifc->result->attr.class_ok;
212 sym->as = gfc_copy_array_spec (ifc->result->as);
213 sym->result = sym;
215 else
217 sym->ts = ifc->ts;
218 sym->attr.allocatable = ifc->attr.allocatable;
219 sym->attr.pointer = ifc->attr.pointer;
220 sym->attr.dimension = ifc->attr.dimension;
221 sym->attr.class_ok = ifc->attr.class_ok;
222 sym->as = gfc_copy_array_spec (ifc->as);
224 sym->ts.interface = ifc;
225 sym->attr.function = ifc->attr.function;
226 sym->attr.subroutine = ifc->attr.subroutine;
228 sym->attr.pure = ifc->attr.pure;
229 sym->attr.elemental = ifc->attr.elemental;
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 /* Copy char length. */
236 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
238 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
239 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
240 && !gfc_resolve_expr (sym->ts.u.cl->length))
241 return false;
245 return true;
249 /* Resolve types of formal argument lists. These have to be done early so that
250 the formal argument lists of module procedures can be copied to the
251 containing module before the individual procedures are resolved
252 individually. We also resolve argument lists of procedures in interface
253 blocks because they are self-contained scoping units.
255 Since a dummy argument cannot be a non-dummy procedure, the only
256 resort left for untyped names are the IMPLICIT types. */
258 void
259 gfc_resolve_formal_arglist (gfc_symbol *proc)
261 gfc_formal_arglist *f;
262 gfc_symbol *sym;
263 bool saved_specification_expr;
264 int i;
266 if (proc->result != NULL)
267 sym = proc->result;
268 else
269 sym = proc;
271 if (gfc_elemental (proc)
272 || sym->attr.pointer || sym->attr.allocatable
273 || (sym->as && sym->as->rank != 0))
275 proc->attr.always_explicit = 1;
276 sym->attr.always_explicit = 1;
279 gfc_namespace *orig_current_ns = gfc_current_ns;
280 gfc_current_ns = gfc_get_procedure_ns (proc);
282 for (f = proc->formal; f; f = f->next)
284 gfc_array_spec *as;
286 sym = f->sym;
288 if (sym == NULL)
290 /* Alternate return placeholder. */
291 if (gfc_elemental (proc))
292 gfc_error ("Alternate return specifier in elemental subroutine "
293 "%qs at %L is not allowed", proc->name,
294 &proc->declared_at);
295 if (proc->attr.function)
296 gfc_error ("Alternate return specifier in function "
297 "%qs at %L is not allowed", proc->name,
298 &proc->declared_at);
299 continue;
302 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
303 && !resolve_procedure_interface (sym))
304 break;
306 if (strcmp (proc->name, sym->name) == 0)
308 gfc_error ("Self-referential argument "
309 "%qs at %L is not allowed", sym->name,
310 &proc->declared_at);
311 break;
314 if (sym->attr.if_source != IFSRC_UNKNOWN)
315 gfc_resolve_formal_arglist (sym);
317 if (sym->attr.subroutine || sym->attr.external)
319 if (sym->attr.flavor == FL_UNKNOWN)
320 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
322 else
324 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
325 && (!sym->attr.function || sym->result == sym))
326 gfc_set_default_type (sym, 1, sym->ns);
329 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
330 ? CLASS_DATA (sym)->as : sym->as;
332 saved_specification_expr = specification_expr;
333 specification_expr = true;
334 gfc_resolve_array_spec (as, 0);
335 specification_expr = saved_specification_expr;
337 /* We can't tell if an array with dimension (:) is assumed or deferred
338 shape until we know if it has the pointer or allocatable attributes.
340 if (as && as->rank > 0 && as->type == AS_DEFERRED
341 && ((sym->ts.type != BT_CLASS
342 && !(sym->attr.pointer || sym->attr.allocatable))
343 || (sym->ts.type == BT_CLASS
344 && !(CLASS_DATA (sym)->attr.class_pointer
345 || CLASS_DATA (sym)->attr.allocatable)))
346 && sym->attr.flavor != FL_PROCEDURE)
348 as->type = AS_ASSUMED_SHAPE;
349 for (i = 0; i < as->rank; i++)
350 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
353 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
354 || (as && as->type == AS_ASSUMED_RANK)
355 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
356 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
357 && (CLASS_DATA (sym)->attr.class_pointer
358 || CLASS_DATA (sym)->attr.allocatable
359 || CLASS_DATA (sym)->attr.target))
360 || sym->attr.optional)
362 proc->attr.always_explicit = 1;
363 if (proc->result)
364 proc->result->attr.always_explicit = 1;
367 /* If the flavor is unknown at this point, it has to be a variable.
368 A procedure specification would have already set the type. */
370 if (sym->attr.flavor == FL_UNKNOWN)
371 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
373 if (gfc_pure (proc))
375 if (sym->attr.flavor == FL_PROCEDURE)
377 /* F08:C1279. */
378 if (!gfc_pure (sym))
380 gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
381 "also be PURE", sym->name, &sym->declared_at);
382 continue;
385 else if (!sym->attr.pointer)
387 if (proc->attr.function && sym->attr.intent != INTENT_IN)
389 if (sym->attr.value)
390 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
391 " of pure function %qs at %L with VALUE "
392 "attribute but without INTENT(IN)",
393 sym->name, proc->name, &sym->declared_at);
394 else
395 gfc_error ("Argument %qs of pure function %qs at %L must "
396 "be INTENT(IN) or VALUE", sym->name, proc->name,
397 &sym->declared_at);
400 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
402 if (sym->attr.value)
403 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
404 " of pure subroutine %qs at %L with VALUE "
405 "attribute but without INTENT", sym->name,
406 proc->name, &sym->declared_at);
407 else
408 gfc_error ("Argument %qs of pure subroutine %qs at %L "
409 "must have its INTENT specified or have the "
410 "VALUE attribute", sym->name, proc->name,
411 &sym->declared_at);
415 /* F08:C1278a. */
416 if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
418 gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
419 " may not be polymorphic", sym->name, proc->name,
420 &sym->declared_at);
421 continue;
425 if (proc->attr.implicit_pure)
427 if (sym->attr.flavor == FL_PROCEDURE)
429 if (!gfc_pure (sym))
430 proc->attr.implicit_pure = 0;
432 else if (!sym->attr.pointer)
434 if (proc->attr.function && sym->attr.intent != INTENT_IN
435 && !sym->value)
436 proc->attr.implicit_pure = 0;
438 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
439 && !sym->value)
440 proc->attr.implicit_pure = 0;
444 if (gfc_elemental (proc))
446 /* F08:C1289. */
447 if (sym->attr.codimension
448 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
449 && CLASS_DATA (sym)->attr.codimension))
451 gfc_error ("Coarray dummy argument %qs at %L to elemental "
452 "procedure", sym->name, &sym->declared_at);
453 continue;
456 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
457 && CLASS_DATA (sym)->as))
459 gfc_error ("Argument %qs of elemental procedure at %L must "
460 "be scalar", sym->name, &sym->declared_at);
461 continue;
464 if (sym->attr.allocatable
465 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
466 && CLASS_DATA (sym)->attr.allocatable))
468 gfc_error ("Argument %qs of elemental procedure at %L cannot "
469 "have the ALLOCATABLE attribute", sym->name,
470 &sym->declared_at);
471 continue;
474 if (sym->attr.pointer
475 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
476 && CLASS_DATA (sym)->attr.class_pointer))
478 gfc_error ("Argument %qs of elemental procedure at %L cannot "
479 "have the POINTER attribute", sym->name,
480 &sym->declared_at);
481 continue;
484 if (sym->attr.flavor == FL_PROCEDURE)
486 gfc_error ("Dummy procedure %qs not allowed in elemental "
487 "procedure %qs at %L", sym->name, proc->name,
488 &sym->declared_at);
489 continue;
492 /* Fortran 2008 Corrigendum 1, C1290a. */
493 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
495 gfc_error ("Argument %qs of elemental procedure %qs at %L must "
496 "have its INTENT specified or have the VALUE "
497 "attribute", sym->name, proc->name,
498 &sym->declared_at);
499 continue;
503 /* Each dummy shall be specified to be scalar. */
504 if (proc->attr.proc == PROC_ST_FUNCTION)
506 if (sym->as != NULL)
508 /* F03:C1263 (R1238) The function-name and each dummy-arg-name
509 shall be specified, explicitly or implicitly, to be scalar. */
510 gfc_error ("Argument %qs of statement function %qs at %L "
511 "must be scalar", sym->name, proc->name,
512 &proc->declared_at);
513 continue;
516 if (sym->ts.type == BT_CHARACTER)
518 gfc_charlen *cl = sym->ts.u.cl;
519 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
521 gfc_error ("Character-valued argument %qs of statement "
522 "function at %L must have constant length",
523 sym->name, &sym->declared_at);
524 continue;
530 gfc_current_ns = orig_current_ns;
534 /* Work function called when searching for symbols that have argument lists
535 associated with them. */
537 static void
538 find_arglists (gfc_symbol *sym)
540 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
541 || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
542 return;
544 gfc_resolve_formal_arglist (sym);
548 /* Given a namespace, resolve all formal argument lists within the namespace.
551 static void
552 resolve_formal_arglists (gfc_namespace *ns)
554 if (ns == NULL)
555 return;
557 gfc_traverse_ns (ns, find_arglists);
561 static void
562 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
564 bool t;
566 if (sym && sym->attr.flavor == FL_PROCEDURE
567 && sym->ns->parent
568 && sym->ns->parent->proc_name
569 && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
570 && !strcmp (sym->name, sym->ns->parent->proc_name->name))
571 gfc_error ("Contained procedure %qs at %L has the same name as its "
572 "encompassing procedure", sym->name, &sym->declared_at);
574 /* If this namespace is not a function or an entry master function,
575 ignore it. */
576 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
577 || sym->attr.entry_master)
578 return;
580 if (!sym->result)
581 return;
583 /* Try to find out of what the return type is. */
584 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
586 t = gfc_set_default_type (sym->result, 0, ns);
588 if (!t && !sym->result->attr.untyped)
590 if (sym->result == sym)
591 gfc_error ("Contained function %qs at %L has no IMPLICIT type",
592 sym->name, &sym->declared_at);
593 else if (!sym->result->attr.proc_pointer)
594 gfc_error ("Result %qs of contained function %qs at %L has "
595 "no IMPLICIT type", sym->result->name, sym->name,
596 &sym->result->declared_at);
597 sym->result->attr.untyped = 1;
601 /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
602 type, lists the only ways a character length value of * can be used:
603 dummy arguments of procedures, named constants, function results and
604 in allocate statements if the allocate_object is an assumed length dummy
605 in external functions. Internal function results and results of module
606 procedures are not on this list, ergo, not permitted. */
608 if (sym->result->ts.type == BT_CHARACTER)
610 gfc_charlen *cl = sym->result->ts.u.cl;
611 if ((!cl || !cl->length) && !sym->result->ts.deferred)
613 /* See if this is a module-procedure and adapt error message
614 accordingly. */
615 bool module_proc;
616 gcc_assert (ns->parent && ns->parent->proc_name);
617 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
619 gfc_error (module_proc
620 ? G_("Character-valued module procedure %qs at %L"
621 " must not be assumed length")
622 : G_("Character-valued internal function %qs at %L"
623 " must not be assumed length"),
624 sym->name, &sym->declared_at);
630 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
631 introduce duplicates. */
633 static void
634 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
636 gfc_formal_arglist *f, *new_arglist;
637 gfc_symbol *new_sym;
639 for (; new_args != NULL; new_args = new_args->next)
641 new_sym = new_args->sym;
642 /* See if this arg is already in the formal argument list. */
643 for (f = proc->formal; f; f = f->next)
645 if (new_sym == f->sym)
646 break;
649 if (f)
650 continue;
652 /* Add a new argument. Argument order is not important. */
653 new_arglist = gfc_get_formal_arglist ();
654 new_arglist->sym = new_sym;
655 new_arglist->next = proc->formal;
656 proc->formal = new_arglist;
661 /* Flag the arguments that are not present in all entries. */
663 static void
664 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
666 gfc_formal_arglist *f, *head;
667 head = new_args;
669 for (f = proc->formal; f; f = f->next)
671 if (f->sym == NULL)
672 continue;
674 for (new_args = head; new_args; new_args = new_args->next)
676 if (new_args->sym == f->sym)
677 break;
680 if (new_args)
681 continue;
683 f->sym->attr.not_always_present = 1;
688 /* Resolve alternate entry points. If a symbol has multiple entry points we
689 create a new master symbol for the main routine, and turn the existing
690 symbol into an entry point. */
692 static void
693 resolve_entries (gfc_namespace *ns)
695 gfc_namespace *old_ns;
696 gfc_code *c;
697 gfc_symbol *proc;
698 gfc_entry_list *el;
699 /* Provide sufficient space to hold "master.%d.%s". */
700 char name[GFC_MAX_SYMBOL_LEN + 1 + 18];
701 static int master_count = 0;
703 if (ns->proc_name == NULL)
704 return;
706 /* No need to do anything if this procedure doesn't have alternate entry
707 points. */
708 if (!ns->entries)
709 return;
711 /* We may already have resolved alternate entry points. */
712 if (ns->proc_name->attr.entry_master)
713 return;
715 /* If this isn't a procedure something has gone horribly wrong. */
716 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
718 /* Remember the current namespace. */
719 old_ns = gfc_current_ns;
721 gfc_current_ns = ns;
723 /* Add the main entry point to the list of entry points. */
724 el = gfc_get_entry_list ();
725 el->sym = ns->proc_name;
726 el->id = 0;
727 el->next = ns->entries;
728 ns->entries = el;
729 ns->proc_name->attr.entry = 1;
731 /* If it is a module function, it needs to be in the right namespace
732 so that gfc_get_fake_result_decl can gather up the results. The
733 need for this arose in get_proc_name, where these beasts were
734 left in their own namespace, to keep prior references linked to
735 the entry declaration.*/
736 if (ns->proc_name->attr.function
737 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
738 el->sym->ns = ns;
740 /* Do the same for entries where the master is not a module
741 procedure. These are retained in the module namespace because
742 of the module procedure declaration. */
743 for (el = el->next; el; el = el->next)
744 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
745 && el->sym->attr.mod_proc)
746 el->sym->ns = ns;
747 el = ns->entries;
749 /* Add an entry statement for it. */
750 c = gfc_get_code (EXEC_ENTRY);
751 c->ext.entry = el;
752 c->next = ns->code;
753 ns->code = c;
755 /* Create a new symbol for the master function. */
756 /* Give the internal function a unique name (within this file).
757 Also include the function name so the user has some hope of figuring
758 out what is going on. */
759 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
760 master_count++, ns->proc_name->name);
761 gfc_get_ha_symbol (name, &proc);
762 gcc_assert (proc != NULL);
764 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
765 if (ns->proc_name->attr.subroutine)
766 gfc_add_subroutine (&proc->attr, proc->name, NULL);
767 else
769 gfc_symbol *sym;
770 gfc_typespec *ts, *fts;
771 gfc_array_spec *as, *fas;
772 gfc_add_function (&proc->attr, proc->name, NULL);
773 proc->result = proc;
774 fas = ns->entries->sym->as;
775 fas = fas ? fas : ns->entries->sym->result->as;
776 fts = &ns->entries->sym->result->ts;
777 if (fts->type == BT_UNKNOWN)
778 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
779 for (el = ns->entries->next; el; el = el->next)
781 ts = &el->sym->result->ts;
782 as = el->sym->as;
783 as = as ? as : el->sym->result->as;
784 if (ts->type == BT_UNKNOWN)
785 ts = gfc_get_default_type (el->sym->result->name, NULL);
787 if (! gfc_compare_types (ts, fts)
788 || (el->sym->result->attr.dimension
789 != ns->entries->sym->result->attr.dimension)
790 || (el->sym->result->attr.pointer
791 != ns->entries->sym->result->attr.pointer))
792 break;
793 else if (as && fas && ns->entries->sym->result != el->sym->result
794 && gfc_compare_array_spec (as, fas) == 0)
795 gfc_error ("Function %s at %L has entries with mismatched "
796 "array specifications", ns->entries->sym->name,
797 &ns->entries->sym->declared_at);
798 /* The characteristics need to match and thus both need to have
799 the same string length, i.e. both len=*, or both len=4.
800 Having both len=<variable> is also possible, but difficult to
801 check at compile time. */
802 else if (ts->type == BT_CHARACTER
803 && (el->sym->result->attr.allocatable
804 != ns->entries->sym->result->attr.allocatable))
806 gfc_error ("Function %s at %L has entry %s with mismatched "
807 "characteristics", ns->entries->sym->name,
808 &ns->entries->sym->declared_at, el->sym->name);
809 goto cleanup;
811 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
812 && (((ts->u.cl->length && !fts->u.cl->length)
813 ||(!ts->u.cl->length && fts->u.cl->length))
814 || (ts->u.cl->length
815 && ts->u.cl->length->expr_type
816 != fts->u.cl->length->expr_type)
817 || (ts->u.cl->length
818 && ts->u.cl->length->expr_type == EXPR_CONSTANT
819 && mpz_cmp (ts->u.cl->length->value.integer,
820 fts->u.cl->length->value.integer) != 0)))
821 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
822 "entries returning variables of different "
823 "string lengths", ns->entries->sym->name,
824 &ns->entries->sym->declared_at);
825 else if (el->sym->result->attr.allocatable
826 != ns->entries->sym->result->attr.allocatable)
827 break;
830 if (el == NULL)
832 sym = ns->entries->sym->result;
833 /* All result types the same. */
834 proc->ts = *fts;
835 if (sym->attr.dimension)
836 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
837 if (sym->attr.pointer)
838 gfc_add_pointer (&proc->attr, NULL);
839 if (sym->attr.allocatable)
840 gfc_add_allocatable (&proc->attr, NULL);
842 else
844 /* Otherwise the result will be passed through a union by
845 reference. */
846 proc->attr.mixed_entry_master = 1;
847 for (el = ns->entries; el; el = el->next)
849 sym = el->sym->result;
850 if (sym->attr.dimension)
852 if (el == ns->entries)
853 gfc_error ("FUNCTION result %s cannot be an array in "
854 "FUNCTION %s at %L", sym->name,
855 ns->entries->sym->name, &sym->declared_at);
856 else
857 gfc_error ("ENTRY result %s cannot be an array in "
858 "FUNCTION %s at %L", sym->name,
859 ns->entries->sym->name, &sym->declared_at);
861 else if (sym->attr.pointer)
863 if (el == ns->entries)
864 gfc_error ("FUNCTION result %s cannot be a POINTER in "
865 "FUNCTION %s at %L", sym->name,
866 ns->entries->sym->name, &sym->declared_at);
867 else
868 gfc_error ("ENTRY result %s cannot be a POINTER in "
869 "FUNCTION %s at %L", sym->name,
870 ns->entries->sym->name, &sym->declared_at);
872 else if (sym->attr.allocatable)
874 if (el == ns->entries)
875 gfc_error ("FUNCTION result %s cannot be ALLOCATABLE in "
876 "FUNCTION %s at %L", sym->name,
877 ns->entries->sym->name, &sym->declared_at);
878 else
879 gfc_error ("ENTRY result %s cannot be ALLOCATABLE in "
880 "FUNCTION %s at %L", sym->name,
881 ns->entries->sym->name, &sym->declared_at);
883 else
885 ts = &sym->ts;
886 if (ts->type == BT_UNKNOWN)
887 ts = gfc_get_default_type (sym->name, NULL);
888 switch (ts->type)
890 case BT_INTEGER:
891 if (ts->kind == gfc_default_integer_kind)
892 sym = NULL;
893 break;
894 case BT_REAL:
895 if (ts->kind == gfc_default_real_kind
896 || ts->kind == gfc_default_double_kind)
897 sym = NULL;
898 break;
899 case BT_COMPLEX:
900 if (ts->kind == gfc_default_complex_kind)
901 sym = NULL;
902 break;
903 case BT_LOGICAL:
904 if (ts->kind == gfc_default_logical_kind)
905 sym = NULL;
906 break;
907 case BT_UNKNOWN:
908 /* We will issue error elsewhere. */
909 sym = NULL;
910 break;
911 default:
912 break;
914 if (sym)
916 if (el == ns->entries)
917 gfc_error ("FUNCTION result %s cannot be of type %s "
918 "in FUNCTION %s at %L", sym->name,
919 gfc_typename (ts), ns->entries->sym->name,
920 &sym->declared_at);
921 else
922 gfc_error ("ENTRY result %s cannot be of type %s "
923 "in FUNCTION %s at %L", sym->name,
924 gfc_typename (ts), ns->entries->sym->name,
925 &sym->declared_at);
932 cleanup:
933 proc->attr.access = ACCESS_PRIVATE;
934 proc->attr.entry_master = 1;
936 /* Merge all the entry point arguments. */
937 for (el = ns->entries; el; el = el->next)
938 merge_argument_lists (proc, el->sym->formal);
940 /* Check the master formal arguments for any that are not
941 present in all entry points. */
942 for (el = ns->entries; el; el = el->next)
943 check_argument_lists (proc, el->sym->formal);
945 /* Use the master function for the function body. */
946 ns->proc_name = proc;
948 /* Finalize the new symbols. */
949 gfc_commit_symbols ();
951 /* Restore the original namespace. */
952 gfc_current_ns = old_ns;
956 /* Forward declaration. */
957 static bool is_non_constant_shape_array (gfc_symbol *sym);
960 /* Resolve common variables. */
961 static void
962 resolve_common_vars (gfc_common_head *common_block, bool named_common)
964 gfc_symbol *csym = common_block->head;
965 gfc_gsymbol *gsym;
967 for (; csym; csym = csym->common_next)
969 gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
970 if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
972 if (csym->common_block)
973 gfc_error_now ("Global entity %qs at %L cannot appear in a "
974 "COMMON block at %L", gsym->name,
975 &gsym->where, &csym->common_block->where);
976 else
977 gfc_error_now ("Global entity %qs at %L cannot appear in a "
978 "COMMON block", gsym->name, &gsym->where);
981 /* gfc_add_in_common may have been called before, but the reported errors
982 have been ignored to continue parsing.
983 We do the checks again here, unless the symbol is USE associated. */
984 if (!csym->attr.use_assoc && !csym->attr.used_in_submodule)
986 gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
987 gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
988 &common_block->where);
991 if (csym->value || csym->attr.data)
993 if (!csym->ns->is_block_data)
994 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
995 "but only in BLOCK DATA initialization is "
996 "allowed", csym->name, &csym->declared_at);
997 else if (!named_common)
998 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
999 "in a blank COMMON but initialization is only "
1000 "allowed in named common blocks", csym->name,
1001 &csym->declared_at);
1004 if (UNLIMITED_POLY (csym))
1005 gfc_error_now ("%qs at %L cannot appear in COMMON "
1006 "[F2008:C5100]", csym->name, &csym->declared_at);
1008 if (csym->attr.dimension && is_non_constant_shape_array (csym))
1010 gfc_error_now ("Automatic object %qs at %L cannot appear in "
1011 "COMMON at %L", csym->name, &csym->declared_at,
1012 &common_block->where);
1013 /* Avoid confusing follow-on error. */
1014 csym->error = 1;
1017 if (csym->ts.type != BT_DERIVED)
1018 continue;
1020 if (!(csym->ts.u.derived->attr.sequence
1021 || csym->ts.u.derived->attr.is_bind_c))
1022 gfc_error_now ("Derived type variable %qs in COMMON at %L "
1023 "has neither the SEQUENCE nor the BIND(C) "
1024 "attribute", csym->name, &csym->declared_at);
1025 if (csym->ts.u.derived->attr.alloc_comp)
1026 gfc_error_now ("Derived type variable %qs in COMMON at %L "
1027 "has an ultimate component that is "
1028 "allocatable", csym->name, &csym->declared_at);
1029 if (gfc_has_default_initializer (csym->ts.u.derived))
1030 gfc_error_now ("Derived type variable %qs in COMMON at %L "
1031 "may not have default initializer", csym->name,
1032 &csym->declared_at);
1034 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
1035 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
1039 /* Resolve common blocks. */
1040 static void
1041 resolve_common_blocks (gfc_symtree *common_root)
1043 gfc_symbol *sym;
1044 gfc_gsymbol * gsym;
1046 if (common_root == NULL)
1047 return;
1049 if (common_root->left)
1050 resolve_common_blocks (common_root->left);
1051 if (common_root->right)
1052 resolve_common_blocks (common_root->right);
1054 resolve_common_vars (common_root->n.common, true);
1056 /* The common name is a global name - in Fortran 2003 also if it has a
1057 C binding name, since Fortran 2008 only the C binding name is a global
1058 identifier. */
1059 if (!common_root->n.common->binding_label
1060 || gfc_notification_std (GFC_STD_F2008))
1062 gsym = gfc_find_gsymbol (gfc_gsym_root,
1063 common_root->n.common->name);
1065 if (gsym && gfc_notification_std (GFC_STD_F2008)
1066 && gsym->type == GSYM_COMMON
1067 && ((common_root->n.common->binding_label
1068 && (!gsym->binding_label
1069 || strcmp (common_root->n.common->binding_label,
1070 gsym->binding_label) != 0))
1071 || (!common_root->n.common->binding_label
1072 && gsym->binding_label)))
1074 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1075 "identifier and must thus have the same binding name "
1076 "as the same-named COMMON block at %L: %s vs %s",
1077 common_root->n.common->name, &common_root->n.common->where,
1078 &gsym->where,
1079 common_root->n.common->binding_label
1080 ? common_root->n.common->binding_label : "(blank)",
1081 gsym->binding_label ? gsym->binding_label : "(blank)");
1082 return;
1085 if (gsym && gsym->type != GSYM_COMMON
1086 && !common_root->n.common->binding_label)
1088 gfc_error ("COMMON block %qs at %L uses the same global identifier "
1089 "as entity at %L",
1090 common_root->n.common->name, &common_root->n.common->where,
1091 &gsym->where);
1092 return;
1094 if (gsym && gsym->type != GSYM_COMMON)
1096 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1097 "%L sharing the identifier with global non-COMMON-block "
1098 "entity at %L", common_root->n.common->name,
1099 &common_root->n.common->where, &gsym->where);
1100 return;
1102 if (!gsym)
1104 gsym = gfc_get_gsymbol (common_root->n.common->name, false);
1105 gsym->type = GSYM_COMMON;
1106 gsym->where = common_root->n.common->where;
1107 gsym->defined = 1;
1109 gsym->used = 1;
1112 if (common_root->n.common->binding_label)
1114 gsym = gfc_find_gsymbol (gfc_gsym_root,
1115 common_root->n.common->binding_label);
1116 if (gsym && gsym->type != GSYM_COMMON)
1118 gfc_error ("COMMON block at %L with binding label %qs uses the same "
1119 "global identifier as entity at %L",
1120 &common_root->n.common->where,
1121 common_root->n.common->binding_label, &gsym->where);
1122 return;
1124 if (!gsym)
1126 gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
1127 gsym->type = GSYM_COMMON;
1128 gsym->where = common_root->n.common->where;
1129 gsym->defined = 1;
1131 gsym->used = 1;
1134 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1135 if (sym == NULL)
1136 return;
1138 if (sym->attr.flavor == FL_PARAMETER)
1139 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1140 sym->name, &common_root->n.common->where, &sym->declared_at);
1142 if (sym->attr.external)
1143 gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
1144 sym->name, &common_root->n.common->where);
1146 if (sym->attr.intrinsic)
1147 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1148 sym->name, &common_root->n.common->where);
1149 else if (sym->attr.result
1150 || gfc_is_function_return_value (sym, gfc_current_ns))
1151 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1152 "that is also a function result", sym->name,
1153 &common_root->n.common->where);
1154 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1155 && sym->attr.proc != PROC_ST_FUNCTION)
1156 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1157 "that is also a global procedure", sym->name,
1158 &common_root->n.common->where);
1162 /* Resolve contained function types. Because contained functions can call one
1163 another, they have to be worked out before any of the contained procedures
1164 can be resolved.
1166 The good news is that if a function doesn't already have a type, the only
1167 way it can get one is through an IMPLICIT type or a RESULT variable, because
1168 by definition contained functions are contained namespace they're contained
1169 in, not in a sibling or parent namespace. */
1171 static void
1172 resolve_contained_functions (gfc_namespace *ns)
1174 gfc_namespace *child;
1175 gfc_entry_list *el;
1177 resolve_formal_arglists (ns);
1179 for (child = ns->contained; child; child = child->sibling)
1181 /* Resolve alternate entry points first. */
1182 resolve_entries (child);
1184 /* Then check function return types. */
1185 resolve_contained_fntype (child->proc_name, child);
1186 for (el = child->entries; el; el = el->next)
1187 resolve_contained_fntype (el->sym, child);
1193 /* A Parameterized Derived Type constructor must contain values for
1194 the PDT KIND parameters or they must have a default initializer.
1195 Go through the constructor picking out the KIND expressions,
1196 storing them in 'param_list' and then call gfc_get_pdt_instance
1197 to obtain the PDT instance. */
1199 static gfc_actual_arglist *param_list, *param_tail, *param;
1201 static bool
1202 get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
1204 param = gfc_get_actual_arglist ();
1205 if (!param_list)
1206 param_list = param_tail = param;
1207 else
1209 param_tail->next = param;
1210 param_tail = param_tail->next;
1213 param_tail->name = c->name;
1214 if (expr)
1215 param_tail->expr = gfc_copy_expr (expr);
1216 else if (c->initializer)
1217 param_tail->expr = gfc_copy_expr (c->initializer);
1218 else
1220 param_tail->spec_type = SPEC_ASSUMED;
1221 if (c->attr.pdt_kind)
1223 gfc_error ("The KIND parameter %qs in the PDT constructor "
1224 "at %C has no value", param->name);
1225 return false;
1229 return true;
1232 static bool
1233 get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
1234 gfc_symbol *derived)
1236 gfc_constructor *cons = NULL;
1237 gfc_component *comp;
1238 bool t = true;
1240 if (expr && expr->expr_type == EXPR_STRUCTURE)
1241 cons = gfc_constructor_first (expr->value.constructor);
1242 else if (constr)
1243 cons = *constr;
1244 gcc_assert (cons);
1246 comp = derived->components;
1248 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1250 if (cons->expr
1251 && cons->expr->expr_type == EXPR_STRUCTURE
1252 && comp->ts.type == BT_DERIVED)
1254 t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
1255 if (!t)
1256 return t;
1258 else if (comp->ts.type == BT_DERIVED)
1260 t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
1261 if (!t)
1262 return t;
1264 else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
1265 && derived->attr.pdt_template)
1267 t = get_pdt_spec_expr (comp, cons->expr);
1268 if (!t)
1269 return t;
1272 return t;
1276 static bool resolve_fl_derived0 (gfc_symbol *sym);
1277 static bool resolve_fl_struct (gfc_symbol *sym);
1280 /* Resolve all of the elements of a structure constructor and make sure that
1281 the types are correct. The 'init' flag indicates that the given
1282 constructor is an initializer. */
1284 static bool
1285 resolve_structure_cons (gfc_expr *expr, int init)
1287 gfc_constructor *cons;
1288 gfc_component *comp;
1289 bool t;
1290 symbol_attribute a;
1292 t = true;
1294 if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1296 if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1297 resolve_fl_derived0 (expr->ts.u.derived);
1298 else
1299 resolve_fl_struct (expr->ts.u.derived);
1301 /* If this is a Parameterized Derived Type template, find the
1302 instance corresponding to the PDT kind parameters. */
1303 if (expr->ts.u.derived->attr.pdt_template)
1305 param_list = NULL;
1306 t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
1307 if (!t)
1308 return t;
1309 gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
1311 expr->param_list = gfc_copy_actual_arglist (param_list);
1313 if (param_list)
1314 gfc_free_actual_arglist (param_list);
1316 if (!expr->ts.u.derived->attr.pdt_type)
1317 return false;
1321 /* A constructor may have references if it is the result of substituting a
1322 parameter variable. In this case we just pull out the component we
1323 want. */
1324 if (expr->ref)
1325 comp = expr->ref->u.c.sym->components;
1326 else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
1327 || expr->ts.type == BT_UNION)
1328 && expr->ts.u.derived)
1329 comp = expr->ts.u.derived->components;
1330 else
1331 return false;
1333 cons = gfc_constructor_first (expr->value.constructor);
1335 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1337 int rank;
1339 if (!cons->expr)
1340 continue;
1342 /* Unions use an EXPR_NULL contrived expression to tell the translation
1343 phase to generate an initializer of the appropriate length.
1344 Ignore it here. */
1345 if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1346 continue;
1348 if (!gfc_resolve_expr (cons->expr))
1350 t = false;
1351 continue;
1354 rank = comp->as ? comp->as->rank : 0;
1355 if (comp->ts.type == BT_CLASS
1356 && !comp->ts.u.derived->attr.unlimited_polymorphic
1357 && CLASS_DATA (comp)->as)
1358 rank = CLASS_DATA (comp)->as->rank;
1360 if (comp->ts.type == BT_CLASS && cons->expr->ts.type != BT_CLASS)
1361 gfc_find_vtab (&cons->expr->ts);
1363 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1364 && (comp->attr.allocatable || cons->expr->rank))
1366 gfc_error ("The rank of the element in the structure "
1367 "constructor at %L does not match that of the "
1368 "component (%d/%d)", &cons->expr->where,
1369 cons->expr->rank, rank);
1370 t = false;
1373 /* If we don't have the right type, try to convert it. */
1375 if (!comp->attr.proc_pointer &&
1376 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1378 if (strcmp (comp->name, "_extends") == 0)
1380 /* Can afford to be brutal with the _extends initializer.
1381 The derived type can get lost because it is PRIVATE
1382 but it is not usage constrained by the standard. */
1383 cons->expr->ts = comp->ts;
1385 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1387 gfc_error ("The element in the structure constructor at %L, "
1388 "for pointer component %qs, is %s but should be %s",
1389 &cons->expr->where, comp->name,
1390 gfc_basic_typename (cons->expr->ts.type),
1391 gfc_basic_typename (comp->ts.type));
1392 t = false;
1394 else if (!UNLIMITED_POLY (comp))
1396 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1397 if (t)
1398 t = t2;
1402 /* For strings, the length of the constructor should be the same as
1403 the one of the structure, ensure this if the lengths are known at
1404 compile time and when we are dealing with PARAMETER or structure
1405 constructors. */
1406 if (cons->expr->ts.type == BT_CHARACTER
1407 && comp->ts.type == BT_CHARACTER
1408 && comp->ts.u.cl && comp->ts.u.cl->length
1409 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1410 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1411 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1412 && cons->expr->ts.u.cl->length->ts.type == BT_INTEGER
1413 && comp->ts.u.cl->length->ts.type == BT_INTEGER
1414 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1415 comp->ts.u.cl->length->value.integer) != 0)
1417 if (comp->attr.pointer)
1419 HOST_WIDE_INT la, lb;
1420 la = gfc_mpz_get_hwi (comp->ts.u.cl->length->value.integer);
1421 lb = gfc_mpz_get_hwi (cons->expr->ts.u.cl->length->value.integer);
1422 gfc_error ("Unequal character lengths (%wd/%wd) for pointer "
1423 "component %qs in constructor at %L",
1424 la, lb, comp->name, &cons->expr->where);
1425 t = false;
1428 if (cons->expr->expr_type == EXPR_VARIABLE
1429 && cons->expr->rank != 0
1430 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1432 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1433 to make use of the gfc_resolve_character_array_constructor
1434 machinery. The expression is later simplified away to
1435 an array of string literals. */
1436 gfc_expr *para = cons->expr;
1437 cons->expr = gfc_get_expr ();
1438 cons->expr->ts = para->ts;
1439 cons->expr->where = para->where;
1440 cons->expr->expr_type = EXPR_ARRAY;
1441 cons->expr->rank = para->rank;
1442 cons->expr->corank = para->corank;
1443 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1444 gfc_constructor_append_expr (&cons->expr->value.constructor,
1445 para, &cons->expr->where);
1448 if (cons->expr->expr_type == EXPR_ARRAY)
1450 /* Rely on the cleanup of the namespace to deal correctly with
1451 the old charlen. (There was a block here that attempted to
1452 remove the charlen but broke the chain in so doing.) */
1453 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1454 cons->expr->ts.u.cl->length_from_typespec = true;
1455 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1456 gfc_resolve_character_array_constructor (cons->expr);
1460 if (cons->expr->expr_type == EXPR_NULL
1461 && !(comp->attr.pointer || comp->attr.allocatable
1462 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1463 || (comp->ts.type == BT_CLASS
1464 && (CLASS_DATA (comp)->attr.class_pointer
1465 || CLASS_DATA (comp)->attr.allocatable))))
1467 t = false;
1468 gfc_error ("The NULL in the structure constructor at %L is "
1469 "being applied to component %qs, which is neither "
1470 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1471 comp->name);
1474 if (comp->attr.proc_pointer && comp->ts.interface)
1476 /* Check procedure pointer interface. */
1477 gfc_symbol *s2 = NULL;
1478 gfc_component *c2;
1479 const char *name;
1480 char err[200];
1482 c2 = gfc_get_proc_ptr_comp (cons->expr);
1483 if (c2)
1485 s2 = c2->ts.interface;
1486 name = c2->name;
1488 else if (cons->expr->expr_type == EXPR_FUNCTION)
1490 s2 = cons->expr->symtree->n.sym->result;
1491 name = cons->expr->symtree->n.sym->result->name;
1493 else if (cons->expr->expr_type != EXPR_NULL)
1495 s2 = cons->expr->symtree->n.sym;
1496 name = cons->expr->symtree->n.sym->name;
1499 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1500 err, sizeof (err), NULL, NULL))
1502 gfc_error_opt (0, "Interface mismatch for procedure-pointer "
1503 "component %qs in structure constructor at %L:"
1504 " %s", comp->name, &cons->expr->where, err);
1505 return false;
1509 /* Validate shape, except for dynamic or PDT arrays. */
1510 if (cons->expr->expr_type == EXPR_ARRAY && rank == cons->expr->rank
1511 && comp->as && !comp->attr.allocatable && !comp->attr.pointer
1512 && !comp->attr.pdt_array)
1514 mpz_t len;
1515 mpz_init (len);
1516 for (int n = 0; n < rank; n++)
1518 if (comp->as->upper[n]->expr_type != EXPR_CONSTANT
1519 || comp->as->lower[n]->expr_type != EXPR_CONSTANT)
1521 gfc_error ("Bad array spec of component %qs referenced in "
1522 "structure constructor at %L",
1523 comp->name, &cons->expr->where);
1524 t = false;
1525 break;
1527 if (cons->expr->shape == NULL)
1528 continue;
1529 mpz_set_ui (len, 1);
1530 mpz_add (len, len, comp->as->upper[n]->value.integer);
1531 mpz_sub (len, len, comp->as->lower[n]->value.integer);
1532 if (mpz_cmp (cons->expr->shape[n], len) != 0)
1534 gfc_error ("The shape of component %qs in the structure "
1535 "constructor at %L differs from the shape of the "
1536 "declared component for dimension %d (%ld/%ld)",
1537 comp->name, &cons->expr->where, n+1,
1538 mpz_get_si (cons->expr->shape[n]),
1539 mpz_get_si (len));
1540 t = false;
1543 mpz_clear (len);
1546 if (!comp->attr.pointer || comp->attr.proc_pointer
1547 || cons->expr->expr_type == EXPR_NULL)
1548 continue;
1550 a = gfc_expr_attr (cons->expr);
1552 if (!a.pointer && !a.target)
1554 t = false;
1555 gfc_error ("The element in the structure constructor at %L, "
1556 "for pointer component %qs should be a POINTER or "
1557 "a TARGET", &cons->expr->where, comp->name);
1560 if (init)
1562 /* F08:C461. Additional checks for pointer initialization. */
1563 if (a.allocatable)
1565 t = false;
1566 gfc_error ("Pointer initialization target at %L "
1567 "must not be ALLOCATABLE", &cons->expr->where);
1569 if (!a.save)
1571 t = false;
1572 gfc_error ("Pointer initialization target at %L "
1573 "must have the SAVE attribute", &cons->expr->where);
1577 /* F2003, C1272 (3). */
1578 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1579 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1580 || gfc_is_coindexed (cons->expr));
1581 if (impure && gfc_pure (NULL))
1583 t = false;
1584 gfc_error ("Invalid expression in the structure constructor for "
1585 "pointer component %qs at %L in PURE procedure",
1586 comp->name, &cons->expr->where);
1589 if (impure)
1590 gfc_unset_implicit_pure (NULL);
1593 return t;
1597 /****************** Expression name resolution ******************/
1599 /* Returns 0 if a symbol was not declared with a type or
1600 attribute declaration statement, nonzero otherwise. */
1602 static bool
1603 was_declared (gfc_symbol *sym)
1605 symbol_attribute a;
1607 a = sym->attr;
1609 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1610 return 1;
1612 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1613 || a.optional || a.pointer || a.save || a.target || a.volatile_
1614 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1615 || a.asynchronous || a.codimension)
1616 return 1;
1618 return 0;
1622 /* Determine if a symbol is generic or not. */
1624 static int
1625 generic_sym (gfc_symbol *sym)
1627 gfc_symbol *s;
1629 if (sym->attr.generic ||
1630 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1631 return 1;
1633 if (was_declared (sym) || sym->ns->parent == NULL)
1634 return 0;
1636 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1638 if (s != NULL)
1640 if (s == sym)
1641 return 0;
1642 else
1643 return generic_sym (s);
1646 return 0;
1650 /* Determine if a symbol is specific or not. */
1652 static int
1653 specific_sym (gfc_symbol *sym)
1655 gfc_symbol *s;
1657 if (sym->attr.if_source == IFSRC_IFBODY
1658 || sym->attr.proc == PROC_MODULE
1659 || sym->attr.proc == PROC_INTERNAL
1660 || sym->attr.proc == PROC_ST_FUNCTION
1661 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1662 || sym->attr.external)
1663 return 1;
1665 if (was_declared (sym) || sym->ns->parent == NULL)
1666 return 0;
1668 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1670 return (s == NULL) ? 0 : specific_sym (s);
1674 /* Figure out if the procedure is specific, generic or unknown. */
1676 enum proc_type
1677 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1679 static proc_type
1680 procedure_kind (gfc_symbol *sym)
1682 if (generic_sym (sym))
1683 return PTYPE_GENERIC;
1685 if (specific_sym (sym))
1686 return PTYPE_SPECIFIC;
1688 return PTYPE_UNKNOWN;
1691 /* Check references to assumed size arrays. The flag need_full_assumed_size
1692 is nonzero when matching actual arguments. */
1694 static int need_full_assumed_size = 0;
1696 static bool
1697 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1699 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1700 return false;
1702 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1703 What should it be? */
1704 if (e->ref
1705 && e->ref->u.ar.as
1706 && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1707 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1708 && (e->ref->u.ar.type == AR_FULL))
1710 gfc_error ("The upper bound in the last dimension must "
1711 "appear in the reference to the assumed size "
1712 "array %qs at %L", sym->name, &e->where);
1713 return true;
1715 return false;
1719 /* Look for bad assumed size array references in argument expressions
1720 of elemental and array valued intrinsic procedures. Since this is
1721 called from procedure resolution functions, it only recurses at
1722 operators. */
1724 static bool
1725 resolve_assumed_size_actual (gfc_expr *e)
1727 if (e == NULL)
1728 return false;
1730 switch (e->expr_type)
1732 case EXPR_VARIABLE:
1733 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1734 return true;
1735 break;
1737 case EXPR_OP:
1738 if (resolve_assumed_size_actual (e->value.op.op1)
1739 || resolve_assumed_size_actual (e->value.op.op2))
1740 return true;
1741 break;
1743 default:
1744 break;
1746 return false;
1750 /* Check a generic procedure, passed as an actual argument, to see if
1751 there is a matching specific name. If none, it is an error, and if
1752 more than one, the reference is ambiguous. */
1753 static int
1754 count_specific_procs (gfc_expr *e)
1756 int n;
1757 gfc_interface *p;
1758 gfc_symbol *sym;
1760 n = 0;
1761 sym = e->symtree->n.sym;
1763 for (p = sym->generic; p; p = p->next)
1764 if (strcmp (sym->name, p->sym->name) == 0)
1766 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1767 sym->name);
1768 n++;
1771 if (n > 1)
1772 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1773 &e->where);
1775 if (n == 0)
1776 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1777 "argument at %L", sym->name, &e->where);
1779 return n;
1783 /* See if a call to sym could possibly be a not allowed RECURSION because of
1784 a missing RECURSIVE declaration. This means that either sym is the current
1785 context itself, or sym is the parent of a contained procedure calling its
1786 non-RECURSIVE containing procedure.
1787 This also works if sym is an ENTRY. */
1789 static bool
1790 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1792 gfc_symbol* proc_sym;
1793 gfc_symbol* context_proc;
1794 gfc_namespace* real_context;
1796 if (sym->attr.flavor == FL_PROGRAM
1797 || gfc_fl_struct (sym->attr.flavor))
1798 return false;
1800 /* If we've got an ENTRY, find real procedure. */
1801 if (sym->attr.entry && sym->ns->entries)
1802 proc_sym = sym->ns->entries->sym;
1803 else
1804 proc_sym = sym;
1806 /* If sym is RECURSIVE, all is well of course. */
1807 if (proc_sym->attr.recursive || flag_recursive)
1808 return false;
1810 /* Find the context procedure's "real" symbol if it has entries.
1811 We look for a procedure symbol, so recurse on the parents if we don't
1812 find one (like in case of a BLOCK construct). */
1813 for (real_context = context; ; real_context = real_context->parent)
1815 /* We should find something, eventually! */
1816 gcc_assert (real_context);
1818 context_proc = (real_context->entries ? real_context->entries->sym
1819 : real_context->proc_name);
1821 /* In some special cases, there may not be a proc_name, like for this
1822 invalid code:
1823 real(bad_kind()) function foo () ...
1824 when checking the call to bad_kind ().
1825 In these cases, we simply return here and assume that the
1826 call is ok. */
1827 if (!context_proc)
1828 return false;
1830 if (context_proc->attr.flavor != FL_LABEL)
1831 break;
1834 /* A call from sym's body to itself is recursion, of course. */
1835 if (context_proc == proc_sym)
1836 return true;
1838 /* The same is true if context is a contained procedure and sym the
1839 containing one. */
1840 if (context_proc->attr.contained)
1842 gfc_symbol* parent_proc;
1844 gcc_assert (context->parent);
1845 parent_proc = (context->parent->entries ? context->parent->entries->sym
1846 : context->parent->proc_name);
1848 if (parent_proc == proc_sym)
1849 return true;
1852 return false;
1856 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1857 its typespec and formal argument list. */
1859 bool
1860 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1862 gfc_intrinsic_sym* isym = NULL;
1863 const char* symstd;
1865 if (sym->resolve_symbol_called >= 2)
1866 return true;
1868 sym->resolve_symbol_called = 2;
1870 /* Already resolved. */
1871 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1872 return true;
1874 /* We already know this one is an intrinsic, so we don't call
1875 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1876 gfc_find_subroutine directly to check whether it is a function or
1877 subroutine. */
1879 if (sym->intmod_sym_id && sym->attr.subroutine)
1881 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1882 isym = gfc_intrinsic_subroutine_by_id (id);
1884 else if (sym->intmod_sym_id)
1886 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1887 isym = gfc_intrinsic_function_by_id (id);
1889 else if (!sym->attr.subroutine)
1890 isym = gfc_find_function (sym->name);
1892 if (isym && !sym->attr.subroutine)
1894 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1895 && !sym->attr.implicit_type)
1896 gfc_warning (OPT_Wsurprising,
1897 "Type specified for intrinsic function %qs at %L is"
1898 " ignored", sym->name, &sym->declared_at);
1900 if (!sym->attr.function &&
1901 !gfc_add_function(&sym->attr, sym->name, loc))
1902 return false;
1904 sym->ts = isym->ts;
1906 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1908 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1910 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1911 " specifier", sym->name, &sym->declared_at);
1912 return false;
1915 if (!sym->attr.subroutine &&
1916 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1917 return false;
1919 else
1921 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1922 &sym->declared_at);
1923 return false;
1926 gfc_copy_formal_args_intr (sym, isym, NULL);
1928 sym->attr.pure = isym->pure;
1929 sym->attr.elemental = isym->elemental;
1931 /* Check it is actually available in the standard settings. */
1932 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1934 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1935 "available in the current standard settings but %s. Use "
1936 "an appropriate %<-std=*%> option or enable "
1937 "%<-fall-intrinsics%> in order to use it.",
1938 sym->name, &sym->declared_at, symstd);
1939 return false;
1942 return true;
1946 /* Resolve a procedure expression, like passing it to a called procedure or as
1947 RHS for a procedure pointer assignment. */
1949 static bool
1950 resolve_procedure_expression (gfc_expr* expr)
1952 gfc_symbol* sym;
1954 if (expr->expr_type != EXPR_VARIABLE)
1955 return true;
1956 gcc_assert (expr->symtree);
1958 sym = expr->symtree->n.sym;
1960 if (sym->attr.intrinsic)
1961 gfc_resolve_intrinsic (sym, &expr->where);
1963 if (sym->attr.flavor != FL_PROCEDURE
1964 || (sym->attr.function && sym->result == sym))
1965 return true;
1967 /* A non-RECURSIVE procedure that is used as procedure expression within its
1968 own body is in danger of being called recursively. */
1969 if (is_illegal_recursion (sym, gfc_current_ns))
1971 if (sym->attr.use_assoc && expr->symtree->name[0] == '@')
1972 gfc_warning (0, "Non-RECURSIVE procedure %qs from module %qs is "
1973 " possibly calling itself recursively in procedure %qs. "
1974 " Declare it RECURSIVE or use %<-frecursive%>",
1975 sym->name, sym->module, gfc_current_ns->proc_name->name);
1976 else
1977 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1978 " itself recursively. Declare it RECURSIVE or use"
1979 " %<-frecursive%>", sym->name, &expr->where);
1982 return true;
1986 /* Check that name is not a derived type. */
1988 static bool
1989 is_dt_name (const char *name)
1991 gfc_symbol *dt_list, *dt_first;
1993 dt_list = dt_first = gfc_derived_types;
1994 for (; dt_list; dt_list = dt_list->dt_next)
1996 if (strcmp(dt_list->name, name) == 0)
1997 return true;
1998 if (dt_first == dt_list->dt_next)
1999 break;
2001 return false;
2005 /* Resolve an actual argument list. Most of the time, this is just
2006 resolving the expressions in the list.
2007 The exception is that we sometimes have to decide whether arguments
2008 that look like procedure arguments are really simple variable
2009 references. */
2011 static bool
2012 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
2013 bool no_formal_args)
2015 gfc_symbol *sym;
2016 gfc_symtree *parent_st;
2017 gfc_expr *e;
2018 gfc_component *comp;
2019 int save_need_full_assumed_size;
2020 bool return_value = false;
2021 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
2023 actual_arg = true;
2024 first_actual_arg = true;
2026 for (; arg; arg = arg->next)
2028 e = arg->expr;
2029 if (e == NULL)
2031 /* Check the label is a valid branching target. */
2032 if (arg->label)
2034 if (arg->label->defined == ST_LABEL_UNKNOWN)
2036 gfc_error ("Label %d referenced at %L is never defined",
2037 arg->label->value, &arg->label->where);
2038 goto cleanup;
2041 first_actual_arg = false;
2042 continue;
2045 if (e->expr_type == EXPR_VARIABLE
2046 && e->symtree->n.sym->attr.generic
2047 && no_formal_args
2048 && count_specific_procs (e) != 1)
2049 goto cleanup;
2051 if (e->ts.type != BT_PROCEDURE)
2053 save_need_full_assumed_size = need_full_assumed_size;
2054 if (e->expr_type != EXPR_VARIABLE)
2055 need_full_assumed_size = 0;
2056 if (!gfc_resolve_expr (e))
2057 goto cleanup;
2058 need_full_assumed_size = save_need_full_assumed_size;
2059 goto argument_list;
2062 /* See if the expression node should really be a variable reference. */
2064 sym = e->symtree->n.sym;
2066 if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
2068 gfc_error ("Derived type %qs is used as an actual "
2069 "argument at %L", sym->name, &e->where);
2070 goto cleanup;
2073 if (sym->attr.flavor == FL_PROCEDURE
2074 || sym->attr.intrinsic
2075 || sym->attr.external)
2077 int actual_ok;
2079 /* If a procedure is not already determined to be something else
2080 check if it is intrinsic. */
2081 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
2082 sym->attr.intrinsic = 1;
2084 if (sym->attr.proc == PROC_ST_FUNCTION)
2086 gfc_error ("Statement function %qs at %L is not allowed as an "
2087 "actual argument", sym->name, &e->where);
2090 actual_ok = gfc_intrinsic_actual_ok (sym->name,
2091 sym->attr.subroutine);
2092 if (sym->attr.intrinsic && actual_ok == 0)
2094 gfc_error ("Intrinsic %qs at %L is not allowed as an "
2095 "actual argument", sym->name, &e->where);
2098 if (sym->attr.contained && !sym->attr.use_assoc
2099 && sym->ns->proc_name->attr.flavor != FL_MODULE)
2101 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
2102 " used as actual argument at %L",
2103 sym->name, &e->where))
2104 goto cleanup;
2107 if (sym->attr.elemental && !sym->attr.intrinsic)
2109 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
2110 "allowed as an actual argument at %L", sym->name,
2111 &e->where);
2114 /* Check if a generic interface has a specific procedure
2115 with the same name before emitting an error. */
2116 if (sym->attr.generic && count_specific_procs (e) != 1)
2117 goto cleanup;
2119 /* Just in case a specific was found for the expression. */
2120 sym = e->symtree->n.sym;
2122 /* If the symbol is the function that names the current (or
2123 parent) scope, then we really have a variable reference. */
2125 if (gfc_is_function_return_value (sym, sym->ns))
2126 goto got_variable;
2128 /* If all else fails, see if we have a specific intrinsic. */
2129 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
2131 gfc_intrinsic_sym *isym;
2133 isym = gfc_find_function (sym->name);
2134 if (isym == NULL || !isym->specific)
2136 gfc_error ("Unable to find a specific INTRINSIC procedure "
2137 "for the reference %qs at %L", sym->name,
2138 &e->where);
2139 goto cleanup;
2141 sym->ts = isym->ts;
2142 sym->attr.intrinsic = 1;
2143 sym->attr.function = 1;
2146 if (!gfc_resolve_expr (e))
2147 goto cleanup;
2148 goto argument_list;
2151 /* See if the name is a module procedure in a parent unit. */
2153 if (was_declared (sym) || sym->ns->parent == NULL)
2154 goto got_variable;
2156 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
2158 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
2159 goto cleanup;
2162 if (parent_st == NULL)
2163 goto got_variable;
2165 sym = parent_st->n.sym;
2166 e->symtree = parent_st; /* Point to the right thing. */
2168 if (sym->attr.flavor == FL_PROCEDURE
2169 || sym->attr.intrinsic
2170 || sym->attr.external)
2172 if (!gfc_resolve_expr (e))
2173 goto cleanup;
2174 goto argument_list;
2177 got_variable:
2178 e->expr_type = EXPR_VARIABLE;
2179 e->ts = sym->ts;
2180 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2181 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2182 && CLASS_DATA (sym)->as))
2184 gfc_array_spec *as
2185 = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
2186 e->rank = as->rank;
2187 e->corank = as->corank;
2188 e->ref = gfc_get_ref ();
2189 e->ref->type = REF_ARRAY;
2190 e->ref->u.ar.type = AR_FULL;
2191 e->ref->u.ar.as = as;
2194 /* These symbols are set untyped by calls to gfc_set_default_type
2195 with 'error_flag' = false. Reset the untyped attribute so that
2196 the error will be generated in gfc_resolve_expr. */
2197 if (e->expr_type == EXPR_VARIABLE
2198 && sym->ts.type == BT_UNKNOWN
2199 && sym->attr.untyped)
2200 sym->attr.untyped = 0;
2202 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2203 primary.cc (match_actual_arg). If above code determines that it
2204 is a variable instead, it needs to be resolved as it was not
2205 done at the beginning of this function. */
2206 save_need_full_assumed_size = need_full_assumed_size;
2207 if (e->expr_type != EXPR_VARIABLE)
2208 need_full_assumed_size = 0;
2209 if (!gfc_resolve_expr (e))
2210 goto cleanup;
2211 need_full_assumed_size = save_need_full_assumed_size;
2213 argument_list:
2214 /* Check argument list functions %VAL, %LOC and %REF. There is
2215 nothing to do for %REF. */
2216 if (arg->name && arg->name[0] == '%')
2218 if (strcmp ("%VAL", arg->name) == 0)
2220 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
2222 gfc_error ("By-value argument at %L is not of numeric "
2223 "type", &e->where);
2224 goto cleanup;
2227 if (e->rank)
2229 gfc_error ("By-value argument at %L cannot be an array or "
2230 "an array section", &e->where);
2231 goto cleanup;
2234 /* Intrinsics are still PROC_UNKNOWN here. However,
2235 since same file external procedures are not resolvable
2236 in gfortran, it is a good deal easier to leave them to
2237 intrinsic.cc. */
2238 if (ptype != PROC_UNKNOWN
2239 && ptype != PROC_DUMMY
2240 && ptype != PROC_EXTERNAL
2241 && ptype != PROC_MODULE)
2243 gfc_error ("By-value argument at %L is not allowed "
2244 "in this context", &e->where);
2245 goto cleanup;
2249 /* Statement functions have already been excluded above. */
2250 else if (strcmp ("%LOC", arg->name) == 0
2251 && e->ts.type == BT_PROCEDURE)
2253 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
2255 gfc_error ("Passing internal procedure at %L by location "
2256 "not allowed", &e->where);
2257 goto cleanup;
2262 comp = gfc_get_proc_ptr_comp(e);
2263 if (e->expr_type == EXPR_VARIABLE
2264 && comp && comp->attr.elemental)
2266 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2267 "allowed as an actual argument at %L", comp->name,
2268 &e->where);
2271 /* Fortran 2008, C1237. */
2272 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2273 && gfc_has_ultimate_pointer (e))
2275 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2276 "component", &e->where);
2277 goto cleanup;
2280 first_actual_arg = false;
2283 return_value = true;
2285 cleanup:
2286 actual_arg = actual_arg_sav;
2287 first_actual_arg = first_actual_arg_sav;
2289 return return_value;
2293 /* Do the checks of the actual argument list that are specific to elemental
2294 procedures. If called with c == NULL, we have a function, otherwise if
2295 expr == NULL, we have a subroutine. */
2297 static bool
2298 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2300 gfc_actual_arglist *arg0;
2301 gfc_actual_arglist *arg;
2302 gfc_symbol *esym = NULL;
2303 gfc_intrinsic_sym *isym = NULL;
2304 gfc_expr *e = NULL;
2305 gfc_intrinsic_arg *iformal = NULL;
2306 gfc_formal_arglist *eformal = NULL;
2307 bool formal_optional = false;
2308 bool set_by_optional = false;
2309 int i;
2310 int rank = 0;
2312 /* Is this an elemental procedure? */
2313 if (expr && expr->value.function.actual != NULL)
2315 if (expr->value.function.esym != NULL
2316 && expr->value.function.esym->attr.elemental)
2318 arg0 = expr->value.function.actual;
2319 esym = expr->value.function.esym;
2321 else if (expr->value.function.isym != NULL
2322 && expr->value.function.isym->elemental)
2324 arg0 = expr->value.function.actual;
2325 isym = expr->value.function.isym;
2327 else
2328 return true;
2330 else if (c && c->ext.actual != NULL)
2332 arg0 = c->ext.actual;
2334 if (c->resolved_sym)
2335 esym = c->resolved_sym;
2336 else
2337 esym = c->symtree->n.sym;
2338 gcc_assert (esym);
2340 if (!esym->attr.elemental)
2341 return true;
2343 else
2344 return true;
2346 /* The rank of an elemental is the rank of its array argument(s). */
2347 for (arg = arg0; arg; arg = arg->next)
2349 if (arg->expr != NULL && arg->expr->rank != 0)
2351 rank = arg->expr->rank;
2352 if (arg->expr->expr_type == EXPR_VARIABLE
2353 && arg->expr->symtree->n.sym->attr.optional)
2354 set_by_optional = true;
2356 /* Function specific; set the result rank and shape. */
2357 if (expr)
2359 expr->rank = rank;
2360 expr->corank = arg->expr->corank;
2361 if (!expr->shape && arg->expr->shape)
2363 expr->shape = gfc_get_shape (rank);
2364 for (i = 0; i < rank; i++)
2365 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2368 break;
2372 /* If it is an array, it shall not be supplied as an actual argument
2373 to an elemental procedure unless an array of the same rank is supplied
2374 as an actual argument corresponding to a nonoptional dummy argument of
2375 that elemental procedure(12.4.1.5). */
2376 formal_optional = false;
2377 if (isym)
2378 iformal = isym->formal;
2379 else
2380 eformal = esym->formal;
2382 for (arg = arg0; arg; arg = arg->next)
2384 if (eformal)
2386 if (eformal->sym && eformal->sym->attr.optional)
2387 formal_optional = true;
2388 eformal = eformal->next;
2390 else if (isym && iformal)
2392 if (iformal->optional)
2393 formal_optional = true;
2394 iformal = iformal->next;
2396 else if (isym)
2397 formal_optional = true;
2399 if (pedantic && arg->expr != NULL
2400 && arg->expr->expr_type == EXPR_VARIABLE
2401 && arg->expr->symtree->n.sym->attr.optional
2402 && formal_optional
2403 && arg->expr->rank
2404 && (set_by_optional || arg->expr->rank != rank)
2405 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2407 bool t = false;
2408 gfc_actual_arglist *a;
2410 /* Scan the argument list for a non-optional argument with the
2411 same rank as arg. */
2412 for (a = arg0; a; a = a->next)
2413 if (a != arg
2414 && a->expr->rank == arg->expr->rank
2415 && !a->expr->symtree->n.sym->attr.optional)
2417 t = true;
2418 break;
2421 if (!t)
2422 gfc_warning (OPT_Wpedantic,
2423 "%qs at %L is an array and OPTIONAL; If it is not "
2424 "present, then it cannot be the actual argument of "
2425 "an ELEMENTAL procedure unless there is a non-optional"
2426 " argument with the same rank "
2427 "(Fortran 2018, 15.5.2.12)",
2428 arg->expr->symtree->n.sym->name, &arg->expr->where);
2432 for (arg = arg0; arg; arg = arg->next)
2434 if (arg->expr == NULL || arg->expr->rank == 0)
2435 continue;
2437 /* Being elemental, the last upper bound of an assumed size array
2438 argument must be present. */
2439 if (resolve_assumed_size_actual (arg->expr))
2440 return false;
2442 /* Elemental procedure's array actual arguments must conform. */
2443 if (e != NULL)
2445 if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
2446 return false;
2448 else
2449 e = arg->expr;
2452 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2453 is an array, the intent inout/out variable needs to be also an array. */
2454 if (rank > 0 && esym && expr == NULL)
2455 for (eformal = esym->formal, arg = arg0; arg && eformal;
2456 arg = arg->next, eformal = eformal->next)
2457 if (eformal->sym
2458 && (eformal->sym->attr.intent == INTENT_OUT
2459 || eformal->sym->attr.intent == INTENT_INOUT)
2460 && arg->expr && arg->expr->rank == 0)
2462 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2463 "ELEMENTAL subroutine %qs is a scalar, but another "
2464 "actual argument is an array", &arg->expr->where,
2465 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2466 : "INOUT", eformal->sym->name, esym->name);
2467 return false;
2469 return true;
2473 /* This function does the checking of references to global procedures
2474 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2475 77 and 95 standards. It checks for a gsymbol for the name, making
2476 one if it does not already exist. If it already exists, then the
2477 reference being resolved must correspond to the type of gsymbol.
2478 Otherwise, the new symbol is equipped with the attributes of the
2479 reference. The corresponding code that is called in creating
2480 global entities is parse.cc.
2482 In addition, for all but -std=legacy, the gsymbols are used to
2483 check the interfaces of external procedures from the same file.
2484 The namespace of the gsymbol is resolved and then, once this is
2485 done the interface is checked. */
2488 static bool
2489 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2491 if (!gsym_ns->proc_name->attr.recursive)
2492 return true;
2494 if (sym->ns == gsym_ns)
2495 return false;
2497 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2498 return false;
2500 return true;
2503 static bool
2504 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2506 if (gsym_ns->entries)
2508 gfc_entry_list *entry = gsym_ns->entries;
2510 for (; entry; entry = entry->next)
2512 if (strcmp (sym->name, entry->sym->name) == 0)
2514 if (strcmp (gsym_ns->proc_name->name,
2515 sym->ns->proc_name->name) == 0)
2516 return false;
2518 if (sym->ns->parent
2519 && strcmp (gsym_ns->proc_name->name,
2520 sym->ns->parent->proc_name->name) == 0)
2521 return false;
2525 return true;
2529 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2531 bool
2532 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2534 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2536 for ( ; arg; arg = arg->next)
2538 if (!arg->sym)
2539 continue;
2541 if (arg->sym->attr.allocatable) /* (2a) */
2543 strncpy (errmsg, _("allocatable argument"), err_len);
2544 return true;
2546 else if (arg->sym->attr.asynchronous)
2548 strncpy (errmsg, _("asynchronous argument"), err_len);
2549 return true;
2551 else if (arg->sym->attr.optional)
2553 strncpy (errmsg, _("optional argument"), err_len);
2554 return true;
2556 else if (arg->sym->attr.pointer)
2558 strncpy (errmsg, _("pointer argument"), err_len);
2559 return true;
2561 else if (arg->sym->attr.target)
2563 strncpy (errmsg, _("target argument"), err_len);
2564 return true;
2566 else if (arg->sym->attr.value)
2568 strncpy (errmsg, _("value argument"), err_len);
2569 return true;
2571 else if (arg->sym->attr.volatile_)
2573 strncpy (errmsg, _("volatile argument"), err_len);
2574 return true;
2576 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2578 strncpy (errmsg, _("assumed-shape argument"), err_len);
2579 return true;
2581 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2583 strncpy (errmsg, _("assumed-rank argument"), err_len);
2584 return true;
2586 else if (arg->sym->attr.codimension) /* (2c) */
2588 strncpy (errmsg, _("coarray argument"), err_len);
2589 return true;
2591 else if (false) /* (2d) TODO: parametrized derived type */
2593 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2594 return true;
2596 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2598 strncpy (errmsg, _("polymorphic argument"), err_len);
2599 return true;
2601 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2603 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2604 return true;
2606 else if (arg->sym->ts.type == BT_ASSUMED)
2608 /* As assumed-type is unlimited polymorphic (cf. above).
2609 See also TS 29113, Note 6.1. */
2610 strncpy (errmsg, _("assumed-type argument"), err_len);
2611 return true;
2615 if (sym->attr.function)
2617 gfc_symbol *res = sym->result ? sym->result : sym;
2619 if (res->attr.dimension) /* (3a) */
2621 strncpy (errmsg, _("array result"), err_len);
2622 return true;
2624 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2626 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2627 return true;
2629 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2630 && res->ts.u.cl->length
2631 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2633 strncpy (errmsg, _("result with non-constant character length"), err_len);
2634 return true;
2638 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2640 strncpy (errmsg, _("elemental procedure"), err_len);
2641 return true;
2643 else if (sym->attr.is_bind_c) /* (5) */
2645 strncpy (errmsg, _("bind(c) procedure"), err_len);
2646 return true;
2649 return false;
2653 static void
2654 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
2656 gfc_gsymbol * gsym;
2657 gfc_namespace *ns;
2658 enum gfc_symbol_type type;
2659 char reason[200];
2661 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2663 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
2664 sym->binding_label != NULL);
2666 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2667 gfc_global_used (gsym, where);
2669 if ((sym->attr.if_source == IFSRC_UNKNOWN
2670 || sym->attr.if_source == IFSRC_IFBODY)
2671 && gsym->type != GSYM_UNKNOWN
2672 && !gsym->binding_label
2673 && gsym->ns
2674 && gsym->ns->proc_name
2675 && not_in_recursive (sym, gsym->ns)
2676 && not_entry_self_reference (sym, gsym->ns))
2678 gfc_symbol *def_sym;
2679 def_sym = gsym->ns->proc_name;
2681 if (gsym->ns->resolved != -1)
2684 /* Resolve the gsymbol namespace if needed. */
2685 if (!gsym->ns->resolved)
2687 gfc_symbol *old_dt_list;
2689 /* Stash away derived types so that the backend_decls
2690 do not get mixed up. */
2691 old_dt_list = gfc_derived_types;
2692 gfc_derived_types = NULL;
2694 gfc_resolve (gsym->ns);
2696 /* Store the new derived types with the global namespace. */
2697 if (gfc_derived_types)
2698 gsym->ns->derived_types = gfc_derived_types;
2700 /* Restore the derived types of this namespace. */
2701 gfc_derived_types = old_dt_list;
2704 /* Make sure that translation for the gsymbol occurs before
2705 the procedure currently being resolved. */
2706 ns = gfc_global_ns_list;
2707 for (; ns && ns != gsym->ns; ns = ns->sibling)
2709 if (ns->sibling == gsym->ns)
2711 ns->sibling = gsym->ns->sibling;
2712 gsym->ns->sibling = gfc_global_ns_list;
2713 gfc_global_ns_list = gsym->ns;
2714 break;
2718 /* This can happen if a binding name has been specified. */
2719 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2720 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2722 if (def_sym->attr.entry_master || def_sym->attr.entry)
2724 gfc_entry_list *entry;
2725 for (entry = gsym->ns->entries; entry; entry = entry->next)
2726 if (strcmp (entry->sym->name, sym->name) == 0)
2728 def_sym = entry->sym;
2729 break;
2734 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2736 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2737 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2738 gfc_typename (&def_sym->ts));
2739 goto done;
2742 if (sym->attr.if_source == IFSRC_UNKNOWN
2743 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2745 gfc_error ("Explicit interface required for %qs at %L: %s",
2746 sym->name, &sym->declared_at, reason);
2747 goto done;
2750 bool bad_result_characteristics;
2751 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2752 reason, sizeof(reason), NULL, NULL,
2753 &bad_result_characteristics))
2755 /* Turn erros into warnings with -std=gnu and -std=legacy,
2756 unless a function returns a wrong type, which can lead
2757 to all kinds of ICEs and wrong code. */
2759 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
2760 && !bad_result_characteristics)
2761 gfc_errors_to_warnings (true);
2763 gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
2764 sym->name, &sym->declared_at, reason);
2765 sym->error = 1;
2766 gfc_errors_to_warnings (false);
2767 goto done;
2771 done:
2773 if (gsym->type == GSYM_UNKNOWN)
2775 gsym->type = type;
2776 gsym->where = *where;
2779 gsym->used = 1;
2783 /************* Function resolution *************/
2785 /* Resolve a function call known to be generic.
2786 Section 14.1.2.4.1. */
2788 static match
2789 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2791 gfc_symbol *s;
2793 if (sym->attr.generic)
2795 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2796 if (s != NULL)
2798 expr->value.function.name = s->name;
2799 expr->value.function.esym = s;
2801 if (s->ts.type != BT_UNKNOWN)
2802 expr->ts = s->ts;
2803 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2804 expr->ts = s->result->ts;
2806 if (s->as != NULL)
2808 expr->rank = s->as->rank;
2809 expr->corank = s->as->corank;
2811 else if (s->result != NULL && s->result->as != NULL)
2813 expr->rank = s->result->as->rank;
2814 expr->corank = s->result->as->corank;
2817 gfc_set_sym_referenced (expr->value.function.esym);
2819 return MATCH_YES;
2822 /* TODO: Need to search for elemental references in generic
2823 interface. */
2826 if (sym->attr.intrinsic)
2827 return gfc_intrinsic_func_interface (expr, 0);
2829 return MATCH_NO;
2833 static bool
2834 resolve_generic_f (gfc_expr *expr)
2836 gfc_symbol *sym;
2837 match m;
2838 gfc_interface *intr = NULL;
2840 sym = expr->symtree->n.sym;
2842 for (;;)
2844 m = resolve_generic_f0 (expr, sym);
2845 if (m == MATCH_YES)
2846 return true;
2847 else if (m == MATCH_ERROR)
2848 return false;
2850 generic:
2851 if (!intr)
2852 for (intr = sym->generic; intr; intr = intr->next)
2853 if (gfc_fl_struct (intr->sym->attr.flavor))
2854 break;
2856 if (sym->ns->parent == NULL)
2857 break;
2858 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2860 if (sym == NULL)
2861 break;
2862 if (!generic_sym (sym))
2863 goto generic;
2866 /* Last ditch attempt. See if the reference is to an intrinsic
2867 that possesses a matching interface. 14.1.2.4 */
2868 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2870 if (gfc_init_expr_flag)
2871 gfc_error ("Function %qs in initialization expression at %L "
2872 "must be an intrinsic function",
2873 expr->symtree->n.sym->name, &expr->where);
2874 else
2875 gfc_error ("There is no specific function for the generic %qs "
2876 "at %L", expr->symtree->n.sym->name, &expr->where);
2877 return false;
2880 if (intr)
2882 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2883 NULL, false))
2884 return false;
2885 if (!gfc_use_derived (expr->ts.u.derived))
2886 return false;
2887 return resolve_structure_cons (expr, 0);
2890 m = gfc_intrinsic_func_interface (expr, 0);
2891 if (m == MATCH_YES)
2892 return true;
2894 if (m == MATCH_NO)
2895 gfc_error ("Generic function %qs at %L is not consistent with a "
2896 "specific intrinsic interface", expr->symtree->n.sym->name,
2897 &expr->where);
2899 return false;
2903 /* Resolve a function call known to be specific. */
2905 static match
2906 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2908 match m;
2910 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2912 if (sym->attr.dummy)
2914 sym->attr.proc = PROC_DUMMY;
2915 goto found;
2918 sym->attr.proc = PROC_EXTERNAL;
2919 goto found;
2922 if (sym->attr.proc == PROC_MODULE
2923 || sym->attr.proc == PROC_ST_FUNCTION
2924 || sym->attr.proc == PROC_INTERNAL)
2925 goto found;
2927 if (sym->attr.intrinsic)
2929 m = gfc_intrinsic_func_interface (expr, 1);
2930 if (m == MATCH_YES)
2931 return MATCH_YES;
2932 if (m == MATCH_NO)
2933 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2934 "with an intrinsic", sym->name, &expr->where);
2936 return MATCH_ERROR;
2939 return MATCH_NO;
2941 found:
2942 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2944 if (sym->result)
2945 expr->ts = sym->result->ts;
2946 else
2947 expr->ts = sym->ts;
2948 expr->value.function.name = sym->name;
2949 expr->value.function.esym = sym;
2950 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2951 error(s). */
2952 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2953 return MATCH_ERROR;
2954 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2956 expr->rank = CLASS_DATA (sym)->as->rank;
2957 expr->corank = CLASS_DATA (sym)->as->corank;
2959 else if (sym->as != NULL)
2961 expr->rank = sym->as->rank;
2962 expr->corank = sym->as->corank;
2965 return MATCH_YES;
2969 static bool
2970 resolve_specific_f (gfc_expr *expr)
2972 gfc_symbol *sym;
2973 match m;
2975 sym = expr->symtree->n.sym;
2977 for (;;)
2979 m = resolve_specific_f0 (sym, expr);
2980 if (m == MATCH_YES)
2981 return true;
2982 if (m == MATCH_ERROR)
2983 return false;
2985 if (sym->ns->parent == NULL)
2986 break;
2988 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2990 if (sym == NULL)
2991 break;
2994 gfc_error ("Unable to resolve the specific function %qs at %L",
2995 expr->symtree->n.sym->name, &expr->where);
2997 return true;
3000 /* Recursively append candidate SYM to CANDIDATES. Store the number of
3001 candidates in CANDIDATES_LEN. */
3003 static void
3004 lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
3005 char **&candidates,
3006 size_t &candidates_len)
3008 gfc_symtree *p;
3010 if (sym == NULL)
3011 return;
3012 if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
3013 && sym->n.sym->attr.flavor == FL_PROCEDURE)
3014 vec_push (candidates, candidates_len, sym->name);
3016 p = sym->left;
3017 if (p)
3018 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
3020 p = sym->right;
3021 if (p)
3022 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
3026 /* Lookup function FN fuzzily, taking names in SYMROOT into account. */
3028 const char*
3029 gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
3031 char **candidates = NULL;
3032 size_t candidates_len = 0;
3033 lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
3034 return gfc_closest_fuzzy_match (fn, candidates);
3038 /* Resolve a procedure call not known to be generic nor specific. */
3040 static bool
3041 resolve_unknown_f (gfc_expr *expr)
3043 gfc_symbol *sym;
3044 gfc_typespec *ts;
3046 sym = expr->symtree->n.sym;
3048 if (sym->attr.dummy)
3050 sym->attr.proc = PROC_DUMMY;
3051 expr->value.function.name = sym->name;
3052 goto set_type;
3055 /* See if we have an intrinsic function reference. */
3057 if (gfc_is_intrinsic (sym, 0, expr->where))
3059 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
3060 return true;
3061 return false;
3064 /* IMPLICIT NONE (external) procedures require an explicit EXTERNAL attr. */
3065 /* Intrinsics were handled above, only non-intrinsics left here. */
3066 if (sym->attr.flavor == FL_PROCEDURE
3067 && sym->attr.implicit_type
3068 && sym->ns
3069 && sym->ns->has_implicit_none_export)
3071 gfc_error ("Missing explicit declaration with EXTERNAL attribute "
3072 "for symbol %qs at %L", sym->name, &sym->declared_at);
3073 sym->error = 1;
3074 return false;
3077 /* The reference is to an external name. */
3079 sym->attr.proc = PROC_EXTERNAL;
3080 expr->value.function.name = sym->name;
3081 expr->value.function.esym = expr->symtree->n.sym;
3083 if (sym->as != NULL)
3085 expr->rank = sym->as->rank;
3086 expr->corank = sym->as->corank;
3089 /* Type of the expression is either the type of the symbol or the
3090 default type of the symbol. */
3092 set_type:
3093 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
3095 if (sym->ts.type != BT_UNKNOWN)
3096 expr->ts = sym->ts;
3097 else
3099 ts = gfc_get_default_type (sym->name, sym->ns);
3101 if (ts->type == BT_UNKNOWN)
3103 const char *guessed
3104 = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
3105 if (guessed)
3106 gfc_error ("Function %qs at %L has no IMPLICIT type"
3107 "; did you mean %qs?",
3108 sym->name, &expr->where, guessed);
3109 else
3110 gfc_error ("Function %qs at %L has no IMPLICIT type",
3111 sym->name, &expr->where);
3112 return false;
3114 else
3115 expr->ts = *ts;
3118 return true;
3122 /* Return true, if the symbol is an external procedure. */
3123 static bool
3124 is_external_proc (gfc_symbol *sym)
3126 if (!sym->attr.dummy && !sym->attr.contained
3127 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
3128 && sym->attr.proc != PROC_ST_FUNCTION
3129 && !sym->attr.proc_pointer
3130 && !sym->attr.use_assoc
3131 && sym->name)
3132 return true;
3134 return false;
3138 /* Figure out if a function reference is pure or not. Also set the name
3139 of the function for a potential error message. Return nonzero if the
3140 function is PURE, zero if not. */
3141 static bool
3142 pure_stmt_function (gfc_expr *, gfc_symbol *);
3144 bool
3145 gfc_pure_function (gfc_expr *e, const char **name)
3147 bool pure;
3148 gfc_component *comp;
3150 *name = NULL;
3152 if (e->symtree != NULL
3153 && e->symtree->n.sym != NULL
3154 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3155 return pure_stmt_function (e, e->symtree->n.sym);
3157 comp = gfc_get_proc_ptr_comp (e);
3158 if (comp)
3160 pure = gfc_pure (comp->ts.interface);
3161 *name = comp->name;
3163 else if (e->value.function.esym)
3165 pure = gfc_pure (e->value.function.esym);
3166 *name = e->value.function.esym->name;
3168 else if (e->value.function.isym)
3170 pure = e->value.function.isym->pure
3171 || e->value.function.isym->elemental;
3172 *name = e->value.function.isym->name;
3174 else
3176 /* Implicit functions are not pure. */
3177 pure = 0;
3178 *name = e->value.function.name;
3181 return pure;
3185 /* Check if the expression is a reference to an implicitly pure function. */
3187 bool
3188 gfc_implicit_pure_function (gfc_expr *e)
3190 gfc_component *comp = gfc_get_proc_ptr_comp (e);
3191 if (comp)
3192 return gfc_implicit_pure (comp->ts.interface);
3193 else if (e->value.function.esym)
3194 return gfc_implicit_pure (e->value.function.esym);
3195 else
3196 return 0;
3200 static bool
3201 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
3202 int *f ATTRIBUTE_UNUSED)
3204 const char *name;
3206 /* Don't bother recursing into other statement functions
3207 since they will be checked individually for purity. */
3208 if (e->expr_type != EXPR_FUNCTION
3209 || !e->symtree
3210 || e->symtree->n.sym == sym
3211 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3212 return false;
3214 return gfc_pure_function (e, &name) ? false : true;
3218 static bool
3219 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
3221 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
3225 /* Check if an impure function is allowed in the current context. */
3227 static bool check_pure_function (gfc_expr *e)
3229 const char *name = NULL;
3230 if (!gfc_pure_function (e, &name) && name)
3232 if (forall_flag)
3234 gfc_error ("Reference to impure function %qs at %L inside a "
3235 "FORALL %s", name, &e->where,
3236 forall_flag == 2 ? "mask" : "block");
3237 return false;
3239 else if (gfc_do_concurrent_flag)
3241 gfc_error ("Reference to impure function %qs at %L inside a "
3242 "DO CONCURRENT %s", name, &e->where,
3243 gfc_do_concurrent_flag == 2 ? "mask" : "block");
3244 return false;
3246 else if (gfc_pure (NULL))
3248 gfc_error ("Reference to impure function %qs at %L "
3249 "within a PURE procedure", name, &e->where);
3250 return false;
3252 if (!gfc_implicit_pure_function (e))
3253 gfc_unset_implicit_pure (NULL);
3255 return true;
3259 /* Update current procedure's array_outer_dependency flag, considering
3260 a call to procedure SYM. */
3262 static void
3263 update_current_proc_array_outer_dependency (gfc_symbol *sym)
3265 /* Check to see if this is a sibling function that has not yet
3266 been resolved. */
3267 gfc_namespace *sibling = gfc_current_ns->sibling;
3268 for (; sibling; sibling = sibling->sibling)
3270 if (sibling->proc_name == sym)
3272 gfc_resolve (sibling);
3273 break;
3277 /* If SYM has references to outer arrays, so has the procedure calling
3278 SYM. If SYM is a procedure pointer, we can assume the worst. */
3279 if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
3280 && gfc_current_ns->proc_name)
3281 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3285 /* Resolve a function call, which means resolving the arguments, then figuring
3286 out which entity the name refers to. */
3288 static bool
3289 resolve_function (gfc_expr *expr)
3291 gfc_actual_arglist *arg;
3292 gfc_symbol *sym;
3293 bool t;
3294 int temp;
3295 procedure_type p = PROC_INTRINSIC;
3296 bool no_formal_args;
3298 sym = NULL;
3299 if (expr->symtree)
3300 sym = expr->symtree->n.sym;
3302 /* If this is a procedure pointer component, it has already been resolved. */
3303 if (gfc_is_proc_ptr_comp (expr))
3304 return true;
3306 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3307 another caf_get. */
3308 if (sym && sym->attr.intrinsic
3309 && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
3310 || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
3311 return true;
3313 if (expr->ref)
3315 gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
3316 &expr->where);
3317 return false;
3320 if (sym && sym->attr.intrinsic
3321 && !gfc_resolve_intrinsic (sym, &expr->where))
3322 return false;
3324 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3326 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
3327 return false;
3330 /* If this is a deferred TBP with an abstract interface (which may
3331 of course be referenced), expr->value.function.esym will be set. */
3332 if (sym && sym->attr.abstract && !expr->value.function.esym)
3334 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3335 sym->name, &expr->where);
3336 return false;
3339 /* If this is a deferred TBP with an abstract interface, its result
3340 cannot be an assumed length character (F2003: C418). */
3341 if (sym && sym->attr.abstract && sym->attr.function
3342 && sym->result->ts.u.cl
3343 && sym->result->ts.u.cl->length == NULL
3344 && !sym->result->ts.deferred)
3346 gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
3347 "character length result (F2008: C418)", sym->name,
3348 &sym->declared_at);
3349 return false;
3352 /* Switch off assumed size checking and do this again for certain kinds
3353 of procedure, once the procedure itself is resolved. */
3354 need_full_assumed_size++;
3356 if (expr->symtree && expr->symtree->n.sym)
3357 p = expr->symtree->n.sym->attr.proc;
3359 if (expr->value.function.isym && expr->value.function.isym->inquiry)
3360 inquiry_argument = true;
3361 no_formal_args = sym && is_external_proc (sym)
3362 && gfc_sym_get_dummy_args (sym) == NULL;
3364 if (!resolve_actual_arglist (expr->value.function.actual,
3365 p, no_formal_args))
3367 inquiry_argument = false;
3368 return false;
3371 inquiry_argument = false;
3373 /* Resume assumed_size checking. */
3374 need_full_assumed_size--;
3376 /* If the procedure is external, check for usage. */
3377 if (sym && is_external_proc (sym))
3378 resolve_global_procedure (sym, &expr->where, 0);
3380 if (sym && sym->ts.type == BT_CHARACTER
3381 && sym->ts.u.cl
3382 && sym->ts.u.cl->length == NULL
3383 && !sym->attr.dummy
3384 && !sym->ts.deferred
3385 && expr->value.function.esym == NULL
3386 && !sym->attr.contained)
3388 /* Internal procedures are taken care of in resolve_contained_fntype. */
3389 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3390 "be used at %L since it is not a dummy argument",
3391 sym->name, &expr->where);
3392 return false;
3395 /* See if function is already resolved. */
3397 if (expr->value.function.name != NULL
3398 || expr->value.function.isym != NULL)
3400 if (expr->ts.type == BT_UNKNOWN)
3401 expr->ts = sym->ts;
3402 t = true;
3404 else
3406 /* Apply the rules of section 14.1.2. */
3408 switch (procedure_kind (sym))
3410 case PTYPE_GENERIC:
3411 t = resolve_generic_f (expr);
3412 break;
3414 case PTYPE_SPECIFIC:
3415 t = resolve_specific_f (expr);
3416 break;
3418 case PTYPE_UNKNOWN:
3419 t = resolve_unknown_f (expr);
3420 break;
3422 default:
3423 gfc_internal_error ("resolve_function(): bad function type");
3427 /* If the expression is still a function (it might have simplified),
3428 then we check to see if we are calling an elemental function. */
3430 if (expr->expr_type != EXPR_FUNCTION)
3431 return t;
3433 /* Walk the argument list looking for invalid BOZ. */
3434 for (arg = expr->value.function.actual; arg; arg = arg->next)
3435 if (arg->expr && arg->expr->ts.type == BT_BOZ)
3437 gfc_error ("A BOZ literal constant at %L cannot appear as an "
3438 "actual argument in a function reference",
3439 &arg->expr->where);
3440 return false;
3443 temp = need_full_assumed_size;
3444 need_full_assumed_size = 0;
3446 if (!resolve_elemental_actual (expr, NULL))
3447 return false;
3449 if (omp_workshare_flag
3450 && expr->value.function.esym
3451 && ! gfc_elemental (expr->value.function.esym))
3453 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3454 "in WORKSHARE construct", expr->value.function.esym->name,
3455 &expr->where);
3456 t = false;
3459 #define GENERIC_ID expr->value.function.isym->id
3460 else if (expr->value.function.actual != NULL
3461 && expr->value.function.isym != NULL
3462 && GENERIC_ID != GFC_ISYM_LBOUND
3463 && GENERIC_ID != GFC_ISYM_LCOBOUND
3464 && GENERIC_ID != GFC_ISYM_UCOBOUND
3465 && GENERIC_ID != GFC_ISYM_LEN
3466 && GENERIC_ID != GFC_ISYM_LOC
3467 && GENERIC_ID != GFC_ISYM_C_LOC
3468 && GENERIC_ID != GFC_ISYM_PRESENT)
3470 /* Array intrinsics must also have the last upper bound of an
3471 assumed size array argument. UBOUND and SIZE have to be
3472 excluded from the check if the second argument is anything
3473 than a constant. */
3475 for (arg = expr->value.function.actual; arg; arg = arg->next)
3477 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3478 && arg == expr->value.function.actual
3479 && arg->next != NULL && arg->next->expr)
3481 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3482 break;
3484 if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
3485 break;
3487 if ((int)mpz_get_si (arg->next->expr->value.integer)
3488 < arg->expr->rank)
3489 break;
3492 if (arg->expr != NULL
3493 && arg->expr->rank > 0
3494 && resolve_assumed_size_actual (arg->expr))
3495 return false;
3498 #undef GENERIC_ID
3500 need_full_assumed_size = temp;
3502 if (!check_pure_function(expr))
3503 t = false;
3505 /* Functions without the RECURSIVE attribution are not allowed to
3506 * call themselves. */
3507 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3509 gfc_symbol *esym;
3510 esym = expr->value.function.esym;
3512 if (is_illegal_recursion (esym, gfc_current_ns))
3514 if (esym->attr.entry && esym->ns->entries)
3515 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3516 " function %qs is not RECURSIVE",
3517 esym->name, &expr->where, esym->ns->entries->sym->name);
3518 else
3519 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3520 " is not RECURSIVE", esym->name, &expr->where);
3522 t = false;
3526 /* Character lengths of use associated functions may contains references to
3527 symbols not referenced from the current program unit otherwise. Make sure
3528 those symbols are marked as referenced. */
3530 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3531 && expr->value.function.esym->attr.use_assoc)
3533 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3536 /* Make sure that the expression has a typespec that works. */
3537 if (expr->ts.type == BT_UNKNOWN)
3539 if (expr->symtree->n.sym->result
3540 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3541 && !expr->symtree->n.sym->result->attr.proc_pointer)
3542 expr->ts = expr->symtree->n.sym->result->ts;
3545 /* These derived types with an incomplete namespace, arising from use
3546 association, cause gfc_get_derived_vtab to segfault. If the function
3547 namespace does not suffice, something is badly wrong. */
3548 if (expr->ts.type == BT_DERIVED
3549 && !expr->ts.u.derived->ns->proc_name)
3551 gfc_symbol *der;
3552 gfc_find_symbol (expr->ts.u.derived->name, expr->symtree->n.sym->ns, 1, &der);
3553 if (der)
3555 expr->ts.u.derived->refs--;
3556 expr->ts.u.derived = der;
3557 der->refs++;
3559 else
3560 expr->ts.u.derived->ns = expr->symtree->n.sym->ns;
3563 if (!expr->ref && !expr->value.function.isym)
3565 if (expr->value.function.esym)
3566 update_current_proc_array_outer_dependency (expr->value.function.esym);
3567 else
3568 update_current_proc_array_outer_dependency (sym);
3570 else if (expr->ref)
3571 /* typebound procedure: Assume the worst. */
3572 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3574 if (expr->value.function.esym
3575 && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3576 gfc_warning (OPT_Wdeprecated_declarations,
3577 "Using function %qs at %L is deprecated",
3578 sym->name, &expr->where);
3579 return t;
3583 /************* Subroutine resolution *************/
3585 static bool
3586 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3588 if (gfc_pure (sym))
3589 return true;
3591 if (forall_flag)
3593 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3594 name, loc);
3595 return false;
3597 else if (gfc_do_concurrent_flag)
3599 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3600 "PURE", name, loc);
3601 return false;
3603 else if (gfc_pure (NULL))
3605 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3606 return false;
3609 gfc_unset_implicit_pure (NULL);
3610 return true;
3614 static match
3615 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3617 gfc_symbol *s;
3619 if (sym->attr.generic)
3621 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3622 if (s != NULL)
3624 c->resolved_sym = s;
3625 if (!pure_subroutine (s, s->name, &c->loc))
3626 return MATCH_ERROR;
3627 return MATCH_YES;
3630 /* TODO: Need to search for elemental references in generic interface. */
3633 if (sym->attr.intrinsic)
3634 return gfc_intrinsic_sub_interface (c, 0);
3636 return MATCH_NO;
3640 static bool
3641 resolve_generic_s (gfc_code *c)
3643 gfc_symbol *sym;
3644 match m;
3646 sym = c->symtree->n.sym;
3648 for (;;)
3650 m = resolve_generic_s0 (c, sym);
3651 if (m == MATCH_YES)
3652 return true;
3653 else if (m == MATCH_ERROR)
3654 return false;
3656 generic:
3657 if (sym->ns->parent == NULL)
3658 break;
3659 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3661 if (sym == NULL)
3662 break;
3663 if (!generic_sym (sym))
3664 goto generic;
3667 /* Last ditch attempt. See if the reference is to an intrinsic
3668 that possesses a matching interface. 14.1.2.4 */
3669 sym = c->symtree->n.sym;
3671 if (!gfc_is_intrinsic (sym, 1, c->loc))
3673 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3674 sym->name, &c->loc);
3675 return false;
3678 m = gfc_intrinsic_sub_interface (c, 0);
3679 if (m == MATCH_YES)
3680 return true;
3681 if (m == MATCH_NO)
3682 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3683 "intrinsic subroutine interface", sym->name, &c->loc);
3685 return false;
3689 /* Resolve a subroutine call known to be specific. */
3691 static match
3692 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3694 match m;
3696 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3698 if (sym->attr.dummy)
3700 sym->attr.proc = PROC_DUMMY;
3701 goto found;
3704 sym->attr.proc = PROC_EXTERNAL;
3705 goto found;
3708 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3709 goto found;
3711 if (sym->attr.intrinsic)
3713 m = gfc_intrinsic_sub_interface (c, 1);
3714 if (m == MATCH_YES)
3715 return MATCH_YES;
3716 if (m == MATCH_NO)
3717 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3718 "with an intrinsic", sym->name, &c->loc);
3720 return MATCH_ERROR;
3723 return MATCH_NO;
3725 found:
3726 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3728 c->resolved_sym = sym;
3729 if (!pure_subroutine (sym, sym->name, &c->loc))
3730 return MATCH_ERROR;
3732 return MATCH_YES;
3736 static bool
3737 resolve_specific_s (gfc_code *c)
3739 gfc_symbol *sym;
3740 match m;
3742 sym = c->symtree->n.sym;
3744 for (;;)
3746 m = resolve_specific_s0 (c, sym);
3747 if (m == MATCH_YES)
3748 return true;
3749 if (m == MATCH_ERROR)
3750 return false;
3752 if (sym->ns->parent == NULL)
3753 break;
3755 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3757 if (sym == NULL)
3758 break;
3761 sym = c->symtree->n.sym;
3762 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3763 sym->name, &c->loc);
3765 return false;
3769 /* Resolve a subroutine call not known to be generic nor specific. */
3771 static bool
3772 resolve_unknown_s (gfc_code *c)
3774 gfc_symbol *sym;
3776 sym = c->symtree->n.sym;
3778 if (sym->attr.dummy)
3780 sym->attr.proc = PROC_DUMMY;
3781 goto found;
3784 /* See if we have an intrinsic function reference. */
3786 if (gfc_is_intrinsic (sym, 1, c->loc))
3788 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3789 return true;
3790 return false;
3793 /* The reference is to an external name. */
3795 found:
3796 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3798 c->resolved_sym = sym;
3800 return pure_subroutine (sym, sym->name, &c->loc);
3804 /* Resolve a subroutine call. Although it was tempting to use the same code
3805 for functions, subroutines and functions are stored differently and this
3806 makes things awkward. */
3808 static bool
3809 resolve_call (gfc_code *c)
3811 bool t;
3812 procedure_type ptype = PROC_INTRINSIC;
3813 gfc_symbol *csym, *sym;
3814 bool no_formal_args;
3816 csym = c->symtree ? c->symtree->n.sym : NULL;
3818 if (csym && csym->ts.type != BT_UNKNOWN)
3820 gfc_error ("%qs at %L has a type, which is not consistent with "
3821 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3822 return false;
3825 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3827 gfc_symtree *st;
3828 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3829 sym = st ? st->n.sym : NULL;
3830 if (sym && csym != sym
3831 && sym->ns == gfc_current_ns
3832 && sym->attr.flavor == FL_PROCEDURE
3833 && sym->attr.contained)
3835 sym->refs++;
3836 if (csym->attr.generic)
3837 c->symtree->n.sym = sym;
3838 else
3839 c->symtree = st;
3840 csym = c->symtree->n.sym;
3844 /* If this ia a deferred TBP, c->expr1 will be set. */
3845 if (!c->expr1 && csym)
3847 if (csym->attr.abstract)
3849 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3850 csym->name, &c->loc);
3851 return false;
3854 /* Subroutines without the RECURSIVE attribution are not allowed to
3855 call themselves. */
3856 if (is_illegal_recursion (csym, gfc_current_ns))
3858 if (csym->attr.entry && csym->ns->entries)
3859 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3860 "as subroutine %qs is not RECURSIVE",
3861 csym->name, &c->loc, csym->ns->entries->sym->name);
3862 else
3863 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3864 "as it is not RECURSIVE", csym->name, &c->loc);
3866 t = false;
3870 /* Switch off assumed size checking and do this again for certain kinds
3871 of procedure, once the procedure itself is resolved. */
3872 need_full_assumed_size++;
3874 if (csym)
3875 ptype = csym->attr.proc;
3877 no_formal_args = csym && is_external_proc (csym)
3878 && gfc_sym_get_dummy_args (csym) == NULL;
3879 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3880 return false;
3882 /* Resume assumed_size checking. */
3883 need_full_assumed_size--;
3885 /* If external, check for usage. */
3886 if (csym && is_external_proc (csym))
3887 resolve_global_procedure (csym, &c->loc, 1);
3889 t = true;
3890 if (c->resolved_sym == NULL)
3892 c->resolved_isym = NULL;
3893 switch (procedure_kind (csym))
3895 case PTYPE_GENERIC:
3896 t = resolve_generic_s (c);
3897 break;
3899 case PTYPE_SPECIFIC:
3900 t = resolve_specific_s (c);
3901 break;
3903 case PTYPE_UNKNOWN:
3904 t = resolve_unknown_s (c);
3905 break;
3907 default:
3908 gfc_internal_error ("resolve_subroutine(): bad function type");
3912 /* Some checks of elemental subroutine actual arguments. */
3913 if (!resolve_elemental_actual (NULL, c))
3914 return false;
3916 if (!c->expr1)
3917 update_current_proc_array_outer_dependency (csym);
3918 else
3919 /* Typebound procedure: Assume the worst. */
3920 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3922 if (c->resolved_sym
3923 && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3924 gfc_warning (OPT_Wdeprecated_declarations,
3925 "Using subroutine %qs at %L is deprecated",
3926 c->resolved_sym->name, &c->loc);
3928 return t;
3932 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3933 op1->shape and op2->shape are non-NULL return true if their shapes
3934 match. If both op1->shape and op2->shape are non-NULL return false
3935 if their shapes do not match. If either op1->shape or op2->shape is
3936 NULL, return true. */
3938 static bool
3939 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3941 bool t;
3942 int i;
3944 t = true;
3946 if (op1->shape != NULL && op2->shape != NULL)
3948 for (i = 0; i < op1->rank; i++)
3950 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3952 gfc_error ("Shapes for operands at %L and %L are not conformable",
3953 &op1->where, &op2->where);
3954 t = false;
3955 break;
3960 return t;
3963 /* Convert a logical operator to the corresponding bitwise intrinsic call.
3964 For example A .AND. B becomes IAND(A, B). */
3965 static gfc_expr *
3966 logical_to_bitwise (gfc_expr *e)
3968 gfc_expr *tmp, *op1, *op2;
3969 gfc_isym_id isym;
3970 gfc_actual_arglist *args = NULL;
3972 gcc_assert (e->expr_type == EXPR_OP);
3974 isym = GFC_ISYM_NONE;
3975 op1 = e->value.op.op1;
3976 op2 = e->value.op.op2;
3978 switch (e->value.op.op)
3980 case INTRINSIC_NOT:
3981 isym = GFC_ISYM_NOT;
3982 break;
3983 case INTRINSIC_AND:
3984 isym = GFC_ISYM_IAND;
3985 break;
3986 case INTRINSIC_OR:
3987 isym = GFC_ISYM_IOR;
3988 break;
3989 case INTRINSIC_NEQV:
3990 isym = GFC_ISYM_IEOR;
3991 break;
3992 case INTRINSIC_EQV:
3993 /* "Bitwise eqv" is just the complement of NEQV === IEOR.
3994 Change the old expression to NEQV, which will get replaced by IEOR,
3995 and wrap it in NOT. */
3996 tmp = gfc_copy_expr (e);
3997 tmp->value.op.op = INTRINSIC_NEQV;
3998 tmp = logical_to_bitwise (tmp);
3999 isym = GFC_ISYM_NOT;
4000 op1 = tmp;
4001 op2 = NULL;
4002 break;
4003 default:
4004 gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
4007 /* Inherit the original operation's operands as arguments. */
4008 args = gfc_get_actual_arglist ();
4009 args->expr = op1;
4010 if (op2)
4012 args->next = gfc_get_actual_arglist ();
4013 args->next->expr = op2;
4016 /* Convert the expression to a function call. */
4017 e->expr_type = EXPR_FUNCTION;
4018 e->value.function.actual = args;
4019 e->value.function.isym = gfc_intrinsic_function_by_id (isym);
4020 e->value.function.name = e->value.function.isym->name;
4021 e->value.function.esym = NULL;
4023 /* Make up a pre-resolved function call symtree if we need to. */
4024 if (!e->symtree || !e->symtree->n.sym)
4026 gfc_symbol *sym;
4027 gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
4028 sym = e->symtree->n.sym;
4029 sym->result = sym;
4030 sym->attr.flavor = FL_PROCEDURE;
4031 sym->attr.function = 1;
4032 sym->attr.elemental = 1;
4033 sym->attr.pure = 1;
4034 sym->attr.referenced = 1;
4035 gfc_intrinsic_symbol (sym);
4036 gfc_commit_symbol (sym);
4039 args->name = e->value.function.isym->formal->name;
4040 if (e->value.function.isym->formal->next)
4041 args->next->name = e->value.function.isym->formal->next->name;
4043 return e;
4046 /* Recursively append candidate UOP to CANDIDATES. Store the number of
4047 candidates in CANDIDATES_LEN. */
4048 static void
4049 lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
4050 char **&candidates,
4051 size_t &candidates_len)
4053 gfc_symtree *p;
4055 if (uop == NULL)
4056 return;
4058 /* Not sure how to properly filter here. Use all for a start.
4059 n.uop.op is NULL for empty interface operators (is that legal?) disregard
4060 these as i suppose they don't make terribly sense. */
4062 if (uop->n.uop->op != NULL)
4063 vec_push (candidates, candidates_len, uop->name);
4065 p = uop->left;
4066 if (p)
4067 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4069 p = uop->right;
4070 if (p)
4071 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4074 /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
4076 static const char*
4077 lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
4079 char **candidates = NULL;
4080 size_t candidates_len = 0;
4081 lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
4082 return gfc_closest_fuzzy_match (op, candidates);
4086 /* Callback finding an impure function as an operand to an .and. or
4087 .or. expression. Remember the last function warned about to
4088 avoid double warnings when recursing. */
4090 static int
4091 impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
4092 void *data)
4094 gfc_expr *f = *e;
4095 const char *name;
4096 static gfc_expr *last = NULL;
4097 bool *found = (bool *) data;
4099 if (f->expr_type == EXPR_FUNCTION)
4101 *found = 1;
4102 if (f != last && !gfc_pure_function (f, &name)
4103 && !gfc_implicit_pure_function (f))
4105 if (name)
4106 gfc_warning (OPT_Wfunction_elimination,
4107 "Impure function %qs at %L might not be evaluated",
4108 name, &f->where);
4109 else
4110 gfc_warning (OPT_Wfunction_elimination,
4111 "Impure function at %L might not be evaluated",
4112 &f->where);
4114 last = f;
4117 return 0;
4120 /* Return true if TYPE is character based, false otherwise. */
4122 static int
4123 is_character_based (bt type)
4125 return type == BT_CHARACTER || type == BT_HOLLERITH;
4129 /* If expression is a hollerith, convert it to character and issue a warning
4130 for the conversion. */
4132 static void
4133 convert_hollerith_to_character (gfc_expr *e)
4135 if (e->ts.type == BT_HOLLERITH)
4137 gfc_typespec t;
4138 gfc_clear_ts (&t);
4139 t.type = BT_CHARACTER;
4140 t.kind = e->ts.kind;
4141 gfc_convert_type_warn (e, &t, 2, 1);
4145 /* Convert to numeric and issue a warning for the conversion. */
4147 static void
4148 convert_to_numeric (gfc_expr *a, gfc_expr *b)
4150 gfc_typespec t;
4151 gfc_clear_ts (&t);
4152 t.type = b->ts.type;
4153 t.kind = b->ts.kind;
4154 gfc_convert_type_warn (a, &t, 2, 1);
4157 /* Resolve an operator expression node. This can involve replacing the
4158 operation with a user defined function call. CHECK_INTERFACES is a
4159 helper macro. */
4161 #define CHECK_INTERFACES \
4163 match m = gfc_extend_expr (e); \
4164 if (m == MATCH_YES) \
4165 return true; \
4166 if (m == MATCH_ERROR) \
4167 return false; \
4170 static bool
4171 resolve_operator (gfc_expr *e)
4173 gfc_expr *op1, *op2;
4174 /* One error uses 3 names; additional space for wording (also via gettext). */
4175 bool t = true;
4177 /* Reduce stacked parentheses to single pair */
4178 while (e->expr_type == EXPR_OP
4179 && e->value.op.op == INTRINSIC_PARENTHESES
4180 && e->value.op.op1->expr_type == EXPR_OP
4181 && e->value.op.op1->value.op.op == INTRINSIC_PARENTHESES)
4183 gfc_expr *tmp = gfc_copy_expr (e->value.op.op1);
4184 gfc_replace_expr (e, tmp);
4187 /* Resolve all subnodes-- give them types. */
4189 switch (e->value.op.op)
4191 default:
4192 if (!gfc_resolve_expr (e->value.op.op2))
4193 t = false;
4195 /* Fall through. */
4197 case INTRINSIC_NOT:
4198 case INTRINSIC_UPLUS:
4199 case INTRINSIC_UMINUS:
4200 case INTRINSIC_PARENTHESES:
4201 if (!gfc_resolve_expr (e->value.op.op1))
4202 return false;
4203 if (e->value.op.op1
4204 && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
4206 gfc_error ("BOZ literal constant at %L cannot be an operand of "
4207 "unary operator %qs", &e->value.op.op1->where,
4208 gfc_op2string (e->value.op.op));
4209 return false;
4211 if (flag_unsigned && pedantic && e->ts.type == BT_UNSIGNED
4212 && e->value.op.op == INTRINSIC_UMINUS)
4214 gfc_error ("Negation of unsigned expression at %L not permitted ",
4215 &e->value.op.op1->where);
4216 return false;
4218 break;
4221 /* Typecheck the new node. */
4223 op1 = e->value.op.op1;
4224 op2 = e->value.op.op2;
4225 if (op1 == NULL && op2 == NULL)
4226 return false;
4227 /* Error out if op2 did not resolve. We already diagnosed op1. */
4228 if (t == false)
4229 return false;
4231 /* op1 and op2 cannot both be BOZ. */
4232 if (op1 && op1->ts.type == BT_BOZ
4233 && op2 && op2->ts.type == BT_BOZ)
4235 gfc_error ("Operands at %L and %L cannot appear as operands of "
4236 "binary operator %qs", &op1->where, &op2->where,
4237 gfc_op2string (e->value.op.op));
4238 return false;
4241 if ((op1 && op1->expr_type == EXPR_NULL)
4242 || (op2 && op2->expr_type == EXPR_NULL))
4244 CHECK_INTERFACES
4245 gfc_error ("Invalid context for NULL() pointer at %L", &e->where);
4246 return false;
4249 switch (e->value.op.op)
4251 case INTRINSIC_UPLUS:
4252 case INTRINSIC_UMINUS:
4253 if (op1->ts.type == BT_INTEGER
4254 || op1->ts.type == BT_REAL
4255 || op1->ts.type == BT_COMPLEX)
4257 e->ts = op1->ts;
4258 break;
4261 CHECK_INTERFACES
4262 gfc_error ("Operand of unary numeric operator %<%s%> at %L is %s",
4263 gfc_op2string (e->value.op.op), &e->where, gfc_typename (e));
4264 return false;
4266 case INTRINSIC_POWER:
4268 if (flag_unsigned)
4270 if (op1->ts.type == BT_UNSIGNED || op2->ts.type == BT_UNSIGNED)
4272 CHECK_INTERFACES
4273 gfc_error ("Exponentiation not valid at %L for %s and %s",
4274 &e->where, gfc_typename (op1), gfc_typename (op2));
4275 return false;
4278 gcc_fallthrough ();
4280 case INTRINSIC_PLUS:
4281 case INTRINSIC_MINUS:
4282 case INTRINSIC_TIMES:
4283 case INTRINSIC_DIVIDE:
4285 /* UNSIGNED cannot appear in a mixed expression without explicit
4286 conversion. */
4287 if (flag_unsigned && gfc_invalid_unsigned_ops (op1, op2))
4289 CHECK_INTERFACES
4290 gfc_error ("Operands of binary numeric operator %<%s%> at %L are "
4291 "%s/%s", gfc_op2string (e->value.op.op), &e->where,
4292 gfc_typename (op1), gfc_typename (op2));
4293 return false;
4296 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4298 /* Do not perform conversions if operands are not conformable as
4299 required for the binary intrinsic operators (F2018:10.1.5).
4300 Defer to a possibly overloading user-defined operator. */
4301 if (!gfc_op_rank_conformable (op1, op2))
4303 CHECK_INTERFACES
4304 gfc_error ("Inconsistent ranks for operator at %L and %L",
4305 &op1->where, &op2->where);
4306 return false;
4309 gfc_type_convert_binary (e, 1);
4310 break;
4313 if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
4315 CHECK_INTERFACES
4316 gfc_error ("Unexpected derived-type entities in binary intrinsic "
4317 "numeric operator %<%s%> at %L",
4318 gfc_op2string (e->value.op.op), &e->where);
4319 return false;
4321 else
4323 CHECK_INTERFACES
4324 gfc_error ("Operands of binary numeric operator %<%s%> at %L are %s/%s",
4325 gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4326 gfc_typename (op2));
4327 return false;
4330 case INTRINSIC_CONCAT:
4331 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4332 && op1->ts.kind == op2->ts.kind)
4334 e->ts.type = BT_CHARACTER;
4335 e->ts.kind = op1->ts.kind;
4336 break;
4339 CHECK_INTERFACES
4340 gfc_error ("Operands of string concatenation operator at %L are %s/%s",
4341 &e->where, gfc_typename (op1), gfc_typename (op2));
4342 return false;
4344 case INTRINSIC_AND:
4345 case INTRINSIC_OR:
4346 case INTRINSIC_EQV:
4347 case INTRINSIC_NEQV:
4348 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4350 e->ts.type = BT_LOGICAL;
4351 e->ts.kind = gfc_kind_max (op1, op2);
4352 if (op1->ts.kind < e->ts.kind)
4353 gfc_convert_type (op1, &e->ts, 2);
4354 else if (op2->ts.kind < e->ts.kind)
4355 gfc_convert_type (op2, &e->ts, 2);
4357 if (flag_frontend_optimize &&
4358 (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
4360 /* Warn about short-circuiting
4361 with impure function as second operand. */
4362 bool op2_f = false;
4363 gfc_expr_walker (&op2, impure_function_callback, &op2_f);
4365 break;
4368 /* Logical ops on integers become bitwise ops with -fdec. */
4369 else if (flag_dec
4370 && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
4372 e->ts.type = BT_INTEGER;
4373 e->ts.kind = gfc_kind_max (op1, op2);
4374 if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
4375 gfc_convert_type (op1, &e->ts, 1);
4376 if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
4377 gfc_convert_type (op2, &e->ts, 1);
4378 e = logical_to_bitwise (e);
4379 goto simplify_op;
4382 CHECK_INTERFACES
4383 gfc_error ("Operands of logical operator %<%s%> at %L are %s/%s",
4384 gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4385 gfc_typename (op2));
4386 return false;
4388 case INTRINSIC_NOT:
4389 /* Logical ops on integers become bitwise ops with -fdec. */
4390 if (flag_dec && op1->ts.type == BT_INTEGER)
4392 e->ts.type = BT_INTEGER;
4393 e->ts.kind = op1->ts.kind;
4394 e = logical_to_bitwise (e);
4395 goto simplify_op;
4398 if (op1->ts.type == BT_LOGICAL)
4400 e->ts.type = BT_LOGICAL;
4401 e->ts.kind = op1->ts.kind;
4402 break;
4405 CHECK_INTERFACES
4406 gfc_error ("Operand of .not. operator at %L is %s", &e->where,
4407 gfc_typename (op1));
4408 return false;
4410 case INTRINSIC_GT:
4411 case INTRINSIC_GT_OS:
4412 case INTRINSIC_GE:
4413 case INTRINSIC_GE_OS:
4414 case INTRINSIC_LT:
4415 case INTRINSIC_LT_OS:
4416 case INTRINSIC_LE:
4417 case INTRINSIC_LE_OS:
4418 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
4420 CHECK_INTERFACES
4421 gfc_error ("COMPLEX quantities cannot be compared at %L", &e->where);
4422 return false;
4425 /* Fall through. */
4427 case INTRINSIC_EQ:
4428 case INTRINSIC_EQ_OS:
4429 case INTRINSIC_NE:
4430 case INTRINSIC_NE_OS:
4432 if (flag_dec
4433 && is_character_based (op1->ts.type)
4434 && is_character_based (op2->ts.type))
4436 convert_hollerith_to_character (op1);
4437 convert_hollerith_to_character (op2);
4440 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4441 && op1->ts.kind == op2->ts.kind)
4443 e->ts.type = BT_LOGICAL;
4444 e->ts.kind = gfc_default_logical_kind;
4445 break;
4448 /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4449 if (op1->ts.type == BT_BOZ)
4451 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4452 "as an operand of a relational operator"),
4453 &op1->where))
4454 return false;
4456 if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
4457 return false;
4459 if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
4460 return false;
4463 /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4464 if (op2->ts.type == BT_BOZ)
4466 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4467 " as an operand of a relational operator"),
4468 &op2->where))
4469 return false;
4471 if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
4472 return false;
4474 if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
4475 return false;
4477 if (flag_dec
4478 && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
4479 convert_to_numeric (op1, op2);
4481 if (flag_dec
4482 && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
4483 convert_to_numeric (op2, op1);
4485 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4487 /* Do not perform conversions if operands are not conformable as
4488 required for the binary intrinsic operators (F2018:10.1.5).
4489 Defer to a possibly overloading user-defined operator. */
4490 if (!gfc_op_rank_conformable (op1, op2))
4492 CHECK_INTERFACES
4493 gfc_error ("Inconsistent ranks for operator at %L and %L",
4494 &op1->where, &op2->where);
4495 return false;
4498 if (flag_unsigned && gfc_invalid_unsigned_ops (op1, op2))
4500 CHECK_INTERFACES
4501 gfc_error ("Inconsistent types for operator at %L and %L: "
4502 "%s and %s", &op1->where, &op2->where,
4503 gfc_typename (op1), gfc_typename (op2));
4504 return false;
4507 gfc_type_convert_binary (e, 1);
4509 e->ts.type = BT_LOGICAL;
4510 e->ts.kind = gfc_default_logical_kind;
4512 if (warn_compare_reals)
4514 gfc_intrinsic_op op = e->value.op.op;
4516 /* Type conversion has made sure that the types of op1 and op2
4517 agree, so it is only necessary to check the first one. */
4518 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
4519 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
4520 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
4522 const char *msg;
4524 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
4525 msg = G_("Equality comparison for %s at %L");
4526 else
4527 msg = G_("Inequality comparison for %s at %L");
4529 gfc_warning (OPT_Wcompare_reals, msg,
4530 gfc_typename (op1), &op1->where);
4534 break;
4537 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4539 CHECK_INTERFACES
4540 gfc_error ("Logicals at %L must be compared with %s instead of %s",
4541 &e->where,
4542 (e->value.op.op == INTRINSIC_EQ || e->value.op.op == INTRINSIC_EQ_OS)
4543 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
4545 else
4547 CHECK_INTERFACES
4548 gfc_error ("Operands of comparison operator %<%s%> at %L are %s/%s",
4549 gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4550 gfc_typename (op2));
4553 return false;
4555 case INTRINSIC_USER:
4556 if (e->value.op.uop->op == NULL)
4558 const char *name = e->value.op.uop->name;
4559 const char *guessed;
4560 guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
4561 CHECK_INTERFACES
4562 if (guessed)
4563 gfc_error ("Unknown operator %<%s%> at %L; did you mean "
4564 "%<%s%>?", name, &e->where, guessed);
4565 else
4566 gfc_error ("Unknown operator %<%s%> at %L", name, &e->where);
4568 else if (op2 == NULL)
4570 CHECK_INTERFACES
4571 gfc_error ("Operand of user operator %<%s%> at %L is %s",
4572 e->value.op.uop->name, &e->where, gfc_typename (op1));
4574 else
4576 e->value.op.uop->op->sym->attr.referenced = 1;
4577 CHECK_INTERFACES
4578 gfc_error ("Operands of user operator %<%s%> at %L are %s/%s",
4579 e->value.op.uop->name, &e->where, gfc_typename (op1),
4580 gfc_typename (op2));
4583 return false;
4585 case INTRINSIC_PARENTHESES:
4586 e->ts = op1->ts;
4587 if (e->ts.type == BT_CHARACTER)
4588 e->ts.u.cl = op1->ts.u.cl;
4589 break;
4591 default:
4592 gfc_internal_error ("resolve_operator(): Bad intrinsic");
4595 /* Deal with arrayness of an operand through an operator. */
4597 switch (e->value.op.op)
4599 case INTRINSIC_PLUS:
4600 case INTRINSIC_MINUS:
4601 case INTRINSIC_TIMES:
4602 case INTRINSIC_DIVIDE:
4603 case INTRINSIC_POWER:
4604 case INTRINSIC_CONCAT:
4605 case INTRINSIC_AND:
4606 case INTRINSIC_OR:
4607 case INTRINSIC_EQV:
4608 case INTRINSIC_NEQV:
4609 case INTRINSIC_EQ:
4610 case INTRINSIC_EQ_OS:
4611 case INTRINSIC_NE:
4612 case INTRINSIC_NE_OS:
4613 case INTRINSIC_GT:
4614 case INTRINSIC_GT_OS:
4615 case INTRINSIC_GE:
4616 case INTRINSIC_GE_OS:
4617 case INTRINSIC_LT:
4618 case INTRINSIC_LT_OS:
4619 case INTRINSIC_LE:
4620 case INTRINSIC_LE_OS:
4622 if (op1->rank == 0 && op2->rank == 0)
4623 e->rank = 0;
4625 if (op1->rank == 0 && op2->rank != 0)
4627 e->rank = op2->rank;
4629 if (e->shape == NULL)
4630 e->shape = gfc_copy_shape (op2->shape, op2->rank);
4633 if (op1->rank != 0 && op2->rank == 0)
4635 e->rank = op1->rank;
4637 if (e->shape == NULL)
4638 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4641 if (op1->rank != 0 && op2->rank != 0)
4643 if (op1->rank == op2->rank)
4645 e->rank = op1->rank;
4646 if (e->shape == NULL)
4648 t = compare_shapes (op1, op2);
4649 if (!t)
4650 e->shape = NULL;
4651 else
4652 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4655 else
4657 /* Allow higher level expressions to work. */
4658 e->rank = 0;
4660 /* Try user-defined operators, and otherwise throw an error. */
4661 CHECK_INTERFACES
4662 gfc_error ("Inconsistent ranks for operator at %L and %L",
4663 &op1->where, &op2->where);
4664 return false;
4668 /* coranks have to be equal or one has to be zero to be combinable. */
4669 if (op1->corank == op2->corank || (op1->corank != 0 && op2->corank == 0))
4671 e->corank = op1->corank;
4672 /* Only do this, when regular array has not set a shape yet. */
4673 if (e->shape == NULL)
4675 if (op1->corank != 0)
4677 e->shape = gfc_copy_shape (op1->shape, op1->corank);
4681 else if (op1->corank == 0 && op2->corank != 0)
4683 e->corank = op2->corank;
4684 /* Only do this, when regular array has not set a shape yet. */
4685 if (e->shape == NULL)
4686 e->shape = gfc_copy_shape (op2->shape, op2->corank);
4688 else
4690 gfc_error ("Inconsistent coranks for operator at %L and %L",
4691 &op1->where, &op2->where);
4692 return false;
4695 break;
4697 case INTRINSIC_PARENTHESES:
4698 case INTRINSIC_NOT:
4699 case INTRINSIC_UPLUS:
4700 case INTRINSIC_UMINUS:
4701 /* Simply copy arrayness attribute */
4702 e->rank = op1->rank;
4703 e->corank = op1->corank;
4705 if (e->shape == NULL)
4706 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4708 break;
4710 default:
4711 break;
4714 simplify_op:
4716 /* Attempt to simplify the expression. */
4717 if (t)
4719 t = gfc_simplify_expr (e, 0);
4720 /* Some calls do not succeed in simplification and return false
4721 even though there is no error; e.g. variable references to
4722 PARAMETER arrays. */
4723 if (!gfc_is_constant_expr (e))
4724 t = true;
4726 return t;
4730 /************** Array resolution subroutines **************/
4732 enum compare_result
4733 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
4735 /* Compare two integer expressions. */
4737 static compare_result
4738 compare_bound (gfc_expr *a, gfc_expr *b)
4740 int i;
4742 if (a == NULL || a->expr_type != EXPR_CONSTANT
4743 || b == NULL || b->expr_type != EXPR_CONSTANT)
4744 return CMP_UNKNOWN;
4746 /* If either of the types isn't INTEGER, we must have
4747 raised an error earlier. */
4749 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4750 return CMP_UNKNOWN;
4752 i = mpz_cmp (a->value.integer, b->value.integer);
4754 if (i < 0)
4755 return CMP_LT;
4756 if (i > 0)
4757 return CMP_GT;
4758 return CMP_EQ;
4762 /* Compare an integer expression with an integer. */
4764 static compare_result
4765 compare_bound_int (gfc_expr *a, int b)
4767 int i;
4769 if (a == NULL
4770 || a->expr_type != EXPR_CONSTANT
4771 || a->ts.type != BT_INTEGER)
4772 return CMP_UNKNOWN;
4774 i = mpz_cmp_si (a->value.integer, b);
4776 if (i < 0)
4777 return CMP_LT;
4778 if (i > 0)
4779 return CMP_GT;
4780 return CMP_EQ;
4784 /* Compare an integer expression with a mpz_t. */
4786 static compare_result
4787 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4789 int i;
4791 if (a == NULL
4792 || a->expr_type != EXPR_CONSTANT
4793 || a->ts.type != BT_INTEGER)
4794 return CMP_UNKNOWN;
4796 i = mpz_cmp (a->value.integer, b);
4798 if (i < 0)
4799 return CMP_LT;
4800 if (i > 0)
4801 return CMP_GT;
4802 return CMP_EQ;
4806 /* Compute the last value of a sequence given by a triplet.
4807 Return 0 if it wasn't able to compute the last value, or if the
4808 sequence if empty, and 1 otherwise. */
4810 static int
4811 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4812 gfc_expr *stride, mpz_t last)
4814 mpz_t rem;
4816 if (start == NULL || start->expr_type != EXPR_CONSTANT
4817 || end == NULL || end->expr_type != EXPR_CONSTANT
4818 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4819 return 0;
4821 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4822 || (stride != NULL && stride->ts.type != BT_INTEGER))
4823 return 0;
4825 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
4827 if (compare_bound (start, end) == CMP_GT)
4828 return 0;
4829 mpz_set (last, end->value.integer);
4830 return 1;
4833 if (compare_bound_int (stride, 0) == CMP_GT)
4835 /* Stride is positive */
4836 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4837 return 0;
4839 else
4841 /* Stride is negative */
4842 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4843 return 0;
4846 mpz_init (rem);
4847 mpz_sub (rem, end->value.integer, start->value.integer);
4848 mpz_tdiv_r (rem, rem, stride->value.integer);
4849 mpz_sub (last, end->value.integer, rem);
4850 mpz_clear (rem);
4852 return 1;
4856 /* Compare a single dimension of an array reference to the array
4857 specification. */
4859 static bool
4860 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4862 mpz_t last_value;
4864 if (ar->dimen_type[i] == DIMEN_STAR)
4866 gcc_assert (ar->stride[i] == NULL);
4867 /* This implies [*] as [*:] and [*:3] are not possible. */
4868 if (ar->start[i] == NULL)
4870 gcc_assert (ar->end[i] == NULL);
4871 return true;
4875 /* Given start, end and stride values, calculate the minimum and
4876 maximum referenced indexes. */
4878 switch (ar->dimen_type[i])
4880 case DIMEN_VECTOR:
4881 case DIMEN_THIS_IMAGE:
4882 break;
4884 case DIMEN_STAR:
4885 case DIMEN_ELEMENT:
4886 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4888 if (i < as->rank)
4889 gfc_warning (0, "Array reference at %L is out of bounds "
4890 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4891 mpz_get_si (ar->start[i]->value.integer),
4892 mpz_get_si (as->lower[i]->value.integer), i+1);
4893 else
4894 gfc_warning (0, "Array reference at %L is out of bounds "
4895 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4896 mpz_get_si (ar->start[i]->value.integer),
4897 mpz_get_si (as->lower[i]->value.integer),
4898 i + 1 - as->rank);
4899 return true;
4901 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4903 if (i < as->rank)
4904 gfc_warning (0, "Array reference at %L is out of bounds "
4905 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4906 mpz_get_si (ar->start[i]->value.integer),
4907 mpz_get_si (as->upper[i]->value.integer), i+1);
4908 else
4909 gfc_warning (0, "Array reference at %L is out of bounds "
4910 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4911 mpz_get_si (ar->start[i]->value.integer),
4912 mpz_get_si (as->upper[i]->value.integer),
4913 i + 1 - as->rank);
4914 return true;
4917 break;
4919 case DIMEN_RANGE:
4921 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4922 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4924 compare_result comp_start_end = compare_bound (AR_START, AR_END);
4925 compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
4927 /* Check for zero stride, which is not allowed. */
4928 if (comp_stride_zero == CMP_EQ)
4930 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4931 return false;
4934 /* if start == end || (stride > 0 && start < end)
4935 || (stride < 0 && start > end),
4936 then the array section contains at least one element. In this
4937 case, there is an out-of-bounds access if
4938 (start < lower || start > upper). */
4939 if (comp_start_end == CMP_EQ
4940 || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
4941 && comp_start_end == CMP_LT)
4942 || (comp_stride_zero == CMP_LT
4943 && comp_start_end == CMP_GT))
4945 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4947 gfc_warning (0, "Lower array reference at %L is out of bounds "
4948 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4949 mpz_get_si (AR_START->value.integer),
4950 mpz_get_si (as->lower[i]->value.integer), i+1);
4951 return true;
4953 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4955 gfc_warning (0, "Lower array reference at %L is out of bounds "
4956 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4957 mpz_get_si (AR_START->value.integer),
4958 mpz_get_si (as->upper[i]->value.integer), i+1);
4959 return true;
4963 /* If we can compute the highest index of the array section,
4964 then it also has to be between lower and upper. */
4965 mpz_init (last_value);
4966 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4967 last_value))
4969 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4971 gfc_warning (0, "Upper array reference at %L is out of bounds "
4972 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4973 mpz_get_si (last_value),
4974 mpz_get_si (as->lower[i]->value.integer), i+1);
4975 mpz_clear (last_value);
4976 return true;
4978 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4980 gfc_warning (0, "Upper array reference at %L is out of bounds "
4981 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4982 mpz_get_si (last_value),
4983 mpz_get_si (as->upper[i]->value.integer), i+1);
4984 mpz_clear (last_value);
4985 return true;
4988 mpz_clear (last_value);
4990 #undef AR_START
4991 #undef AR_END
4993 break;
4995 default:
4996 gfc_internal_error ("check_dimension(): Bad array reference");
4999 return true;
5003 /* Compare an array reference with an array specification. */
5005 static bool
5006 compare_spec_to_ref (gfc_array_ref *ar)
5008 gfc_array_spec *as;
5009 int i;
5011 as = ar->as;
5012 i = as->rank - 1;
5013 /* TODO: Full array sections are only allowed as actual parameters. */
5014 if (as->type == AS_ASSUMED_SIZE
5015 && (/*ar->type == AR_FULL
5016 ||*/ (ar->type == AR_SECTION
5017 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
5019 gfc_error ("Rightmost upper bound of assumed size array section "
5020 "not specified at %L", &ar->where);
5021 return false;
5024 if (ar->type == AR_FULL)
5025 return true;
5027 if (as->rank != ar->dimen)
5029 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
5030 &ar->where, ar->dimen, as->rank);
5031 return false;
5034 /* ar->codimen == 0 is a local array. */
5035 if (as->corank != ar->codimen && ar->codimen != 0)
5037 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
5038 &ar->where, ar->codimen, as->corank);
5039 return false;
5042 for (i = 0; i < as->rank; i++)
5043 if (!check_dimension (i, ar, as))
5044 return false;
5046 /* Local access has no coarray spec. */
5047 if (ar->codimen != 0)
5048 for (i = as->rank; i < as->rank + as->corank; i++)
5050 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
5051 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
5053 gfc_error ("Coindex of codimension %d must be a scalar at %L",
5054 i + 1 - as->rank, &ar->where);
5055 return false;
5057 if (!check_dimension (i, ar, as))
5058 return false;
5061 return true;
5065 /* Resolve one part of an array index. */
5067 static bool
5068 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
5069 int force_index_integer_kind)
5071 gfc_typespec ts;
5073 if (index == NULL)
5074 return true;
5076 if (!gfc_resolve_expr (index))
5077 return false;
5079 if (check_scalar && index->rank != 0)
5081 gfc_error ("Array index at %L must be scalar", &index->where);
5082 return false;
5085 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
5087 gfc_error ("Array index at %L must be of INTEGER type, found %s",
5088 &index->where, gfc_basic_typename (index->ts.type));
5089 return false;
5092 if (index->ts.type == BT_REAL)
5093 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
5094 &index->where))
5095 return false;
5097 if ((index->ts.kind != gfc_index_integer_kind
5098 && force_index_integer_kind)
5099 || (index->ts.type != BT_INTEGER
5100 && index->ts.type != BT_UNKNOWN))
5102 gfc_clear_ts (&ts);
5103 ts.type = BT_INTEGER;
5104 ts.kind = gfc_index_integer_kind;
5106 gfc_convert_type_warn (index, &ts, 2, 0);
5109 return true;
5112 /* Resolve one part of an array index. */
5114 bool
5115 gfc_resolve_index (gfc_expr *index, int check_scalar)
5117 return gfc_resolve_index_1 (index, check_scalar, 1);
5120 /* Resolve a dim argument to an intrinsic function. */
5122 bool
5123 gfc_resolve_dim_arg (gfc_expr *dim)
5125 if (dim == NULL)
5126 return true;
5128 if (!gfc_resolve_expr (dim))
5129 return false;
5131 if (dim->rank != 0)
5133 gfc_error ("Argument dim at %L must be scalar", &dim->where);
5134 return false;
5138 if (dim->ts.type != BT_INTEGER)
5140 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
5141 return false;
5144 if (dim->ts.kind != gfc_index_integer_kind)
5146 gfc_typespec ts;
5148 gfc_clear_ts (&ts);
5149 ts.type = BT_INTEGER;
5150 ts.kind = gfc_index_integer_kind;
5152 gfc_convert_type_warn (dim, &ts, 2, 0);
5155 return true;
5158 /* Given an expression that contains array references, update those array
5159 references to point to the right array specifications. While this is
5160 filled in during matching, this information is difficult to save and load
5161 in a module, so we take care of it here.
5163 The idea here is that the original array reference comes from the
5164 base symbol. We traverse the list of reference structures, setting
5165 the stored reference to references. Component references can
5166 provide an additional array specification. */
5167 static void
5168 resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
5170 static bool
5171 find_array_spec (gfc_expr *e)
5173 gfc_array_spec *as;
5174 gfc_component *c;
5175 gfc_ref *ref;
5176 bool class_as = false;
5178 if (e->symtree->n.sym->assoc)
5180 if (e->symtree->n.sym->assoc->target)
5181 gfc_resolve_expr (e->symtree->n.sym->assoc->target);
5182 resolve_assoc_var (e->symtree->n.sym, false);
5185 if (e->symtree->n.sym->ts.type == BT_CLASS)
5187 as = CLASS_DATA (e->symtree->n.sym)->as;
5188 class_as = true;
5190 else
5191 as = e->symtree->n.sym->as;
5193 for (ref = e->ref; ref; ref = ref->next)
5194 switch (ref->type)
5196 case REF_ARRAY:
5197 if (as == NULL)
5199 locus loc = ref->u.ar.where.lb ? ref->u.ar.where : e->where;
5200 gfc_error ("Invalid array reference of a non-array entity at %L",
5201 &loc);
5202 return false;
5205 ref->u.ar.as = as;
5206 as = NULL;
5207 break;
5209 case REF_COMPONENT:
5210 c = ref->u.c.component;
5211 if (c->attr.dimension)
5213 if (as != NULL && !(class_as && as == c->as))
5214 gfc_internal_error ("find_array_spec(): unused as(1)");
5215 as = c->as;
5218 break;
5220 case REF_SUBSTRING:
5221 case REF_INQUIRY:
5222 break;
5225 if (as != NULL)
5226 gfc_internal_error ("find_array_spec(): unused as(2)");
5228 return true;
5232 /* Resolve an array reference. */
5234 static bool
5235 resolve_array_ref (gfc_array_ref *ar)
5237 int i, check_scalar;
5238 gfc_expr *e;
5240 for (i = 0; i < ar->dimen + ar->codimen; i++)
5242 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
5244 /* Do not force gfc_index_integer_kind for the start. We can
5245 do fine with any integer kind. This avoids temporary arrays
5246 created for indexing with a vector. */
5247 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
5248 return false;
5249 if (!gfc_resolve_index (ar->end[i], check_scalar))
5250 return false;
5251 if (!gfc_resolve_index (ar->stride[i], check_scalar))
5252 return false;
5254 e = ar->start[i];
5256 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
5257 switch (e->rank)
5259 case 0:
5260 ar->dimen_type[i] = DIMEN_ELEMENT;
5261 break;
5263 case 1:
5264 ar->dimen_type[i] = DIMEN_VECTOR;
5265 if (e->expr_type == EXPR_VARIABLE
5266 && e->symtree->n.sym->ts.type == BT_DERIVED)
5267 ar->start[i] = gfc_get_parentheses (e);
5268 break;
5270 default:
5271 gfc_error ("Array index at %L is an array of rank %d",
5272 &ar->c_where[i], e->rank);
5273 return false;
5276 /* Fill in the upper bound, which may be lower than the
5277 specified one for something like a(2:10:5), which is
5278 identical to a(2:7:5). Only relevant for strides not equal
5279 to one. Don't try a division by zero. */
5280 if (ar->dimen_type[i] == DIMEN_RANGE
5281 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
5282 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
5283 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
5285 mpz_t size, end;
5287 if (gfc_ref_dimen_size (ar, i, &size, &end))
5289 if (ar->end[i] == NULL)
5291 ar->end[i] =
5292 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
5293 &ar->where);
5294 mpz_set (ar->end[i]->value.integer, end);
5296 else if (ar->end[i]->ts.type == BT_INTEGER
5297 && ar->end[i]->expr_type == EXPR_CONSTANT)
5299 mpz_set (ar->end[i]->value.integer, end);
5301 else
5302 gcc_unreachable ();
5304 mpz_clear (size);
5305 mpz_clear (end);
5310 if (ar->type == AR_FULL)
5312 if (ar->as->rank == 0)
5313 ar->type = AR_ELEMENT;
5315 /* Make sure array is the same as array(:,:), this way
5316 we don't need to special case all the time. */
5317 ar->dimen = ar->as->rank;
5318 for (i = 0; i < ar->dimen; i++)
5320 ar->dimen_type[i] = DIMEN_RANGE;
5322 gcc_assert (ar->start[i] == NULL);
5323 gcc_assert (ar->end[i] == NULL);
5324 gcc_assert (ar->stride[i] == NULL);
5328 /* If the reference type is unknown, figure out what kind it is. */
5330 if (ar->type == AR_UNKNOWN)
5332 ar->type = AR_ELEMENT;
5333 for (i = 0; i < ar->dimen; i++)
5334 if (ar->dimen_type[i] == DIMEN_RANGE
5335 || ar->dimen_type[i] == DIMEN_VECTOR)
5337 ar->type = AR_SECTION;
5338 break;
5342 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
5343 return false;
5345 if (ar->as->corank && ar->codimen == 0)
5347 int n;
5348 ar->codimen = ar->as->corank;
5349 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
5350 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
5353 return true;
5357 bool
5358 gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
5360 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
5362 if (ref->u.ss.start != NULL)
5364 if (!gfc_resolve_expr (ref->u.ss.start))
5365 return false;
5367 if (ref->u.ss.start->ts.type != BT_INTEGER)
5369 gfc_error ("Substring start index at %L must be of type INTEGER",
5370 &ref->u.ss.start->where);
5371 return false;
5374 if (ref->u.ss.start->rank != 0)
5376 gfc_error ("Substring start index at %L must be scalar",
5377 &ref->u.ss.start->where);
5378 return false;
5381 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
5382 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5383 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5385 gfc_error ("Substring start index at %L is less than one",
5386 &ref->u.ss.start->where);
5387 return false;
5391 if (ref->u.ss.end != NULL)
5393 if (!gfc_resolve_expr (ref->u.ss.end))
5394 return false;
5396 if (ref->u.ss.end->ts.type != BT_INTEGER)
5398 gfc_error ("Substring end index at %L must be of type INTEGER",
5399 &ref->u.ss.end->where);
5400 return false;
5403 if (ref->u.ss.end->rank != 0)
5405 gfc_error ("Substring end index at %L must be scalar",
5406 &ref->u.ss.end->where);
5407 return false;
5410 if (ref->u.ss.length != NULL
5411 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
5412 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5413 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5415 gfc_error ("Substring end index at %L exceeds the string length",
5416 &ref->u.ss.start->where);
5417 return false;
5420 if (compare_bound_mpz_t (ref->u.ss.end,
5421 gfc_integer_kinds[k].huge) == CMP_GT
5422 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5423 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5425 gfc_error ("Substring end index at %L is too large",
5426 &ref->u.ss.end->where);
5427 return false;
5429 /* If the substring has the same length as the original
5430 variable, the reference itself can be deleted. */
5432 if (ref->u.ss.length != NULL
5433 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
5434 && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
5435 *equal_length = true;
5438 return true;
5442 /* This function supplies missing substring charlens. */
5444 void
5445 gfc_resolve_substring_charlen (gfc_expr *e)
5447 gfc_ref *char_ref;
5448 gfc_expr *start, *end;
5449 gfc_typespec *ts = NULL;
5450 mpz_t diff;
5452 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
5454 if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
5455 break;
5456 if (char_ref->type == REF_COMPONENT)
5457 ts = &char_ref->u.c.component->ts;
5460 if (!char_ref || char_ref->type == REF_INQUIRY)
5461 return;
5463 gcc_assert (char_ref->next == NULL);
5465 if (e->ts.u.cl)
5467 if (e->ts.u.cl->length)
5468 gfc_free_expr (e->ts.u.cl->length);
5469 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
5470 return;
5473 if (!e->ts.u.cl)
5474 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5476 if (char_ref->u.ss.start)
5477 start = gfc_copy_expr (char_ref->u.ss.start);
5478 else
5479 start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
5481 if (char_ref->u.ss.end)
5482 end = gfc_copy_expr (char_ref->u.ss.end);
5483 else if (e->expr_type == EXPR_VARIABLE)
5485 if (!ts)
5486 ts = &e->symtree->n.sym->ts;
5487 end = gfc_copy_expr (ts->u.cl->length);
5489 else
5490 end = NULL;
5492 if (!start || !end)
5494 gfc_free_expr (start);
5495 gfc_free_expr (end);
5496 return;
5499 /* Length = (end - start + 1).
5500 Check first whether it has a constant length. */
5501 if (gfc_dep_difference (end, start, &diff))
5503 gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
5504 &e->where);
5506 mpz_add_ui (len->value.integer, diff, 1);
5507 mpz_clear (diff);
5508 e->ts.u.cl->length = len;
5509 /* The check for length < 0 is handled below */
5511 else
5513 e->ts.u.cl->length = gfc_subtract (end, start);
5514 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
5515 gfc_get_int_expr (gfc_charlen_int_kind,
5516 NULL, 1));
5519 /* F2008, 6.4.1: Both the starting point and the ending point shall
5520 be within the range 1, 2, ..., n unless the starting point exceeds
5521 the ending point, in which case the substring has length zero. */
5523 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
5524 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
5526 e->ts.u.cl->length->ts.type = BT_INTEGER;
5527 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5529 /* Make sure that the length is simplified. */
5530 gfc_simplify_expr (e->ts.u.cl->length, 1);
5531 gfc_resolve_expr (e->ts.u.cl->length);
5535 /* Resolve subtype references. */
5537 bool
5538 gfc_resolve_ref (gfc_expr *expr)
5540 int current_part_dimension, n_components, seen_part_dimension, dim;
5541 gfc_ref *ref, **prev, *array_ref;
5542 bool equal_length;
5544 for (ref = expr->ref; ref; ref = ref->next)
5545 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
5547 if (!find_array_spec (expr))
5548 return false;
5549 break;
5552 for (prev = &expr->ref; *prev != NULL;
5553 prev = *prev == NULL ? prev : &(*prev)->next)
5554 switch ((*prev)->type)
5556 case REF_ARRAY:
5557 if (!resolve_array_ref (&(*prev)->u.ar))
5558 return false;
5559 break;
5561 case REF_COMPONENT:
5562 case REF_INQUIRY:
5563 break;
5565 case REF_SUBSTRING:
5566 equal_length = false;
5567 if (!gfc_resolve_substring (*prev, &equal_length))
5568 return false;
5570 if (expr->expr_type != EXPR_SUBSTRING && equal_length)
5572 /* Remove the reference and move the charlen, if any. */
5573 ref = *prev;
5574 *prev = ref->next;
5575 ref->next = NULL;
5576 expr->ts.u.cl = ref->u.ss.length;
5577 ref->u.ss.length = NULL;
5578 gfc_free_ref_list (ref);
5580 break;
5583 /* Check constraints on part references. */
5585 current_part_dimension = 0;
5586 seen_part_dimension = 0;
5587 n_components = 0;
5588 array_ref = NULL;
5590 for (ref = expr->ref; ref; ref = ref->next)
5592 switch (ref->type)
5594 case REF_ARRAY:
5595 array_ref = ref;
5596 switch (ref->u.ar.type)
5598 case AR_FULL:
5599 /* Coarray scalar. */
5600 if (ref->u.ar.as->rank == 0)
5602 current_part_dimension = 0;
5603 break;
5605 /* Fall through. */
5606 case AR_SECTION:
5607 current_part_dimension = 1;
5608 break;
5610 case AR_ELEMENT:
5611 array_ref = NULL;
5612 current_part_dimension = 0;
5613 break;
5615 case AR_UNKNOWN:
5616 gfc_internal_error ("resolve_ref(): Bad array reference");
5619 break;
5621 case REF_COMPONENT:
5622 if (current_part_dimension || seen_part_dimension)
5624 /* F03:C614. */
5625 if (ref->u.c.component->attr.pointer
5626 || ref->u.c.component->attr.proc_pointer
5627 || (ref->u.c.component->ts.type == BT_CLASS
5628 && CLASS_DATA (ref->u.c.component)->attr.pointer))
5630 gfc_error ("Component to the right of a part reference "
5631 "with nonzero rank must not have the POINTER "
5632 "attribute at %L", &expr->where);
5633 return false;
5635 else if (ref->u.c.component->attr.allocatable
5636 || (ref->u.c.component->ts.type == BT_CLASS
5637 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
5640 gfc_error ("Component to the right of a part reference "
5641 "with nonzero rank must not have the ALLOCATABLE "
5642 "attribute at %L", &expr->where);
5643 return false;
5647 n_components++;
5648 break;
5650 case REF_SUBSTRING:
5651 break;
5653 case REF_INQUIRY:
5654 /* Implement requirement in note 9.7 of F2018 that the result of the
5655 LEN inquiry be a scalar. */
5656 if (ref->u.i == INQUIRY_LEN && array_ref
5657 && ((expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->length)
5658 || expr->ts.type == BT_INTEGER))
5660 array_ref->u.ar.type = AR_ELEMENT;
5661 expr->rank = 0;
5662 /* INQUIRY_LEN is not evaluated from the rest of the expr
5663 but directly from the string length. This means that setting
5664 the array indices to one does not matter but might trigger
5665 a runtime bounds error. Suppress the check. */
5666 expr->no_bounds_check = 1;
5667 for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
5669 array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
5670 if (array_ref->u.ar.start[dim])
5671 gfc_free_expr (array_ref->u.ar.start[dim]);
5672 array_ref->u.ar.start[dim]
5673 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
5674 if (array_ref->u.ar.end[dim])
5675 gfc_free_expr (array_ref->u.ar.end[dim]);
5676 if (array_ref->u.ar.stride[dim])
5677 gfc_free_expr (array_ref->u.ar.stride[dim]);
5680 break;
5683 if (((ref->type == REF_COMPONENT && n_components > 1)
5684 || ref->next == NULL)
5685 && current_part_dimension
5686 && seen_part_dimension)
5688 gfc_error ("Two or more part references with nonzero rank must "
5689 "not be specified at %L", &expr->where);
5690 return false;
5693 if (ref->type == REF_COMPONENT)
5695 if (current_part_dimension)
5696 seen_part_dimension = 1;
5698 /* reset to make sure */
5699 current_part_dimension = 0;
5703 return true;
5707 /* Given an expression, determine its shape. This is easier than it sounds.
5708 Leaves the shape array NULL if it is not possible to determine the shape. */
5710 static void
5711 expression_shape (gfc_expr *e)
5713 mpz_t array[GFC_MAX_DIMENSIONS];
5714 int i;
5716 if (e->rank <= 0 || e->shape != NULL)
5717 return;
5719 for (i = 0; i < e->rank; i++)
5720 if (!gfc_array_dimen_size (e, i, &array[i]))
5721 goto fail;
5723 e->shape = gfc_get_shape (e->rank);
5725 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
5727 return;
5729 fail:
5730 for (i--; i >= 0; i--)
5731 mpz_clear (array[i]);
5735 /* Given a variable expression node, compute the rank of the expression by
5736 examining the base symbol and any reference structures it may have. */
5738 void
5739 gfc_expression_rank (gfc_expr *e)
5741 gfc_ref *ref, *last_arr_ref = nullptr;
5742 int i, rank, corank;
5744 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
5745 could lead to serious confusion... */
5746 gcc_assert (e->expr_type != EXPR_COMPCALL);
5748 if (e->ref == NULL)
5750 if (e->expr_type == EXPR_ARRAY)
5751 goto done;
5752 /* Constructors can have a rank different from one via RESHAPE(). */
5754 if (e->symtree != NULL)
5756 /* After errors the ts.u.derived of a CLASS might not be set. */
5757 gfc_array_spec *as = (e->symtree->n.sym->ts.type == BT_CLASS
5758 && e->symtree->n.sym->ts.u.derived
5759 && CLASS_DATA (e->symtree->n.sym))
5760 ? CLASS_DATA (e->symtree->n.sym)->as
5761 : e->symtree->n.sym->as;
5762 if (as)
5764 e->rank = as->rank;
5765 e->corank = as->corank;
5766 goto done;
5769 e->rank = 0;
5770 e->corank = 0;
5771 goto done;
5774 rank = 0;
5775 corank = 0;
5777 for (ref = e->ref; ref; ref = ref->next)
5779 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
5780 && ref->u.c.component->attr.function && !ref->next)
5782 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
5783 corank = ref->u.c.component->as ? ref->u.c.component->as->corank : 0;
5786 if (ref->type != REF_ARRAY)
5787 continue;
5789 last_arr_ref = ref;
5790 if (ref->u.ar.type == AR_FULL && ref->u.ar.as)
5792 rank = ref->u.ar.as->rank;
5793 break;
5796 if (ref->u.ar.type == AR_SECTION)
5798 /* Figure out the rank of the section. */
5799 if (rank != 0)
5800 gfc_internal_error ("gfc_expression_rank(): Two array specs");
5802 for (i = 0; i < ref->u.ar.dimen; i++)
5803 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5804 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
5805 rank++;
5807 break;
5810 if (last_arr_ref && last_arr_ref->u.ar.as)
5812 for (i = last_arr_ref->u.ar.as->rank;
5813 i < last_arr_ref->u.ar.as->rank + last_arr_ref->u.ar.as->corank; ++i)
5815 /* For unknown dimen in non-resolved as assume full corank. */
5816 if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_STAR
5817 || (last_arr_ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
5818 && !last_arr_ref->u.ar.as->resolved))
5820 corank = last_arr_ref->u.ar.as->corank;
5821 break;
5823 else if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_RANGE
5824 || last_arr_ref->u.ar.dimen_type[i] == DIMEN_VECTOR
5825 || last_arr_ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE)
5826 corank++;
5827 else if (last_arr_ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
5828 gfc_internal_error ("Illegal coarray index");
5832 e->rank = rank;
5833 e->corank = corank;
5835 done:
5836 expression_shape (e);
5840 /* Given two expressions, check that their rank is conformable, i.e. either
5841 both have the same rank or at least one is a scalar. */
5843 bool
5844 gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
5846 if (op1->expr_type == EXPR_VARIABLE)
5847 gfc_expression_rank (op1);
5848 if (op2->expr_type == EXPR_VARIABLE)
5849 gfc_expression_rank (op2);
5851 return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank)
5852 && (op1->corank == 0 || op2->corank == 0
5853 || op1->corank == op2->corank);
5857 static void
5858 add_caf_get_intrinsic (gfc_expr *e)
5860 gfc_expr *wrapper, *tmp_expr;
5861 gfc_ref *ref;
5862 int n;
5864 for (ref = e->ref; ref; ref = ref->next)
5865 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5866 break;
5867 if (ref == NULL)
5868 return;
5870 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
5871 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
5872 return;
5874 tmp_expr = XCNEW (gfc_expr);
5875 *tmp_expr = *e;
5876 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
5877 "caf_get", tmp_expr->where, 1, tmp_expr);
5878 wrapper->ts = e->ts;
5879 wrapper->rank = e->rank;
5880 wrapper->corank = e->corank;
5881 if (e->rank)
5882 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
5883 *e = *wrapper;
5884 free (wrapper);
5888 static void
5889 remove_caf_get_intrinsic (gfc_expr *e)
5891 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
5892 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
5893 gfc_expr *e2 = e->value.function.actual->expr;
5894 e->value.function.actual->expr = NULL;
5895 gfc_free_actual_arglist (e->value.function.actual);
5896 gfc_free_shape (&e->shape, e->rank);
5897 *e = *e2;
5898 free (e2);
5902 /* Resolve a variable expression. */
5904 static bool
5905 resolve_variable (gfc_expr *e)
5907 gfc_symbol *sym;
5908 bool t;
5910 t = true;
5912 if (e->symtree == NULL)
5913 return false;
5914 sym = e->symtree->n.sym;
5916 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
5917 as ts.type is set to BT_ASSUMED in resolve_symbol. */
5918 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
5920 if (!actual_arg || inquiry_argument)
5922 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
5923 "be used as actual argument", sym->name, &e->where);
5924 return false;
5927 /* TS 29113, 407b. */
5928 else if (e->ts.type == BT_ASSUMED)
5930 if (!actual_arg)
5932 gfc_error ("Assumed-type variable %s at %L may only be used "
5933 "as actual argument", sym->name, &e->where);
5934 return false;
5936 else if (inquiry_argument && !first_actual_arg)
5938 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5939 for all inquiry functions in resolve_function; the reason is
5940 that the function-name resolution happens too late in that
5941 function. */
5942 gfc_error ("Assumed-type variable %s at %L as actual argument to "
5943 "an inquiry function shall be the first argument",
5944 sym->name, &e->where);
5945 return false;
5948 /* TS 29113, C535b. */
5949 else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5950 && sym->ts.u.derived && CLASS_DATA (sym)
5951 && CLASS_DATA (sym)->as
5952 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5953 || (sym->ts.type != BT_CLASS && sym->as
5954 && sym->as->type == AS_ASSUMED_RANK))
5955 && !sym->attr.select_rank_temporary)
5957 if (!actual_arg
5958 && !(cs_base && cs_base->current
5959 && cs_base->current->op == EXEC_SELECT_RANK))
5961 gfc_error ("Assumed-rank variable %s at %L may only be used as "
5962 "actual argument", sym->name, &e->where);
5963 return false;
5965 else if (inquiry_argument && !first_actual_arg)
5967 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5968 for all inquiry functions in resolve_function; the reason is
5969 that the function-name resolution happens too late in that
5970 function. */
5971 gfc_error ("Assumed-rank variable %s at %L as actual argument "
5972 "to an inquiry function shall be the first argument",
5973 sym->name, &e->where);
5974 return false;
5978 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
5979 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5980 && e->ref->next == NULL))
5982 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
5983 "a subobject reference", sym->name, &e->ref->u.ar.where);
5984 return false;
5986 /* TS 29113, 407b. */
5987 else if (e->ts.type == BT_ASSUMED && e->ref
5988 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5989 && e->ref->next == NULL))
5991 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
5992 "reference", sym->name, &e->ref->u.ar.where);
5993 return false;
5996 /* TS 29113, C535b. */
5997 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5998 && sym->ts.u.derived && CLASS_DATA (sym)
5999 && CLASS_DATA (sym)->as
6000 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
6001 || (sym->ts.type != BT_CLASS && sym->as
6002 && sym->as->type == AS_ASSUMED_RANK))
6003 && e->ref
6004 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6005 && e->ref->next == NULL))
6007 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
6008 "reference", sym->name, &e->ref->u.ar.where);
6009 return false;
6012 /* Guessed type variables are associate_names whose selector had not been
6013 parsed at the time that the construct was parsed. Now the namespace is
6014 being resolved, the TKR of the selector will be available for fixup of
6015 the associate_name. */
6016 if (IS_INFERRED_TYPE (e) && e->ref)
6018 gfc_fixup_inferred_type_refs (e);
6019 /* KIND inquiry ref returns the kind of the target. */
6020 if (e->expr_type == EXPR_CONSTANT)
6021 return true;
6023 else if (sym->attr.select_type_temporary
6024 && sym->ns->assoc_name_inferred)
6025 gfc_fixup_inferred_type_refs (e);
6027 /* For variables that are used in an associate (target => object) where
6028 the object's basetype is array valued while the target is scalar,
6029 the ts' type of the component refs is still array valued, which
6030 can't be translated that way. */
6031 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
6032 && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
6033 && sym->assoc->target->ts.u.derived
6034 && CLASS_DATA (sym->assoc->target)
6035 && CLASS_DATA (sym->assoc->target)->as)
6037 gfc_ref *ref = e->ref;
6038 while (ref)
6040 switch (ref->type)
6042 case REF_COMPONENT:
6043 ref->u.c.sym = sym->ts.u.derived;
6044 /* Stop the loop. */
6045 ref = NULL;
6046 break;
6047 default:
6048 ref = ref->next;
6049 break;
6054 /* If this is an associate-name, it may be parsed with an array reference
6055 in error even though the target is scalar. Fail directly in this case.
6056 TODO Understand why class scalar expressions must be excluded. */
6057 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
6059 if (sym->ts.type == BT_CLASS)
6060 gfc_fix_class_refs (e);
6061 if (!sym->attr.dimension && !sym->attr.codimension && e->ref
6062 && e->ref->type == REF_ARRAY)
6064 /* Unambiguously scalar! */
6065 if (sym->assoc->target
6066 && (sym->assoc->target->expr_type == EXPR_CONSTANT
6067 || sym->assoc->target->expr_type == EXPR_STRUCTURE))
6068 gfc_error ("Scalar variable %qs has an array reference at %L",
6069 sym->name, &e->where);
6070 return false;
6072 else if ((sym->attr.dimension || sym->attr.codimension)
6073 && (!e->ref || e->ref->type != REF_ARRAY))
6075 /* This can happen because the parser did not detect that the
6076 associate name is an array and the expression had no array
6077 part_ref. */
6078 gfc_ref *ref = gfc_get_ref ();
6079 ref->type = REF_ARRAY;
6080 ref->u.ar.type = AR_FULL;
6081 if (sym->as)
6083 ref->u.ar.as = sym->as;
6084 ref->u.ar.dimen = sym->as->rank;
6086 ref->next = e->ref;
6087 e->ref = ref;
6091 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
6092 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
6094 /* On the other hand, the parser may not have known this is an array;
6095 in this case, we have to add a FULL reference. */
6096 if (sym->assoc && (sym->attr.dimension || sym->attr.codimension) && !e->ref)
6098 e->ref = gfc_get_ref ();
6099 e->ref->type = REF_ARRAY;
6100 e->ref->u.ar.type = AR_FULL;
6101 e->ref->u.ar.dimen = 0;
6104 /* Like above, but for class types, where the checking whether an array
6105 ref is present is more complicated. Furthermore make sure not to add
6106 the full array ref to _vptr or _len refs. */
6107 if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
6108 && CLASS_DATA (sym)
6109 && (CLASS_DATA (sym)->attr.dimension
6110 || CLASS_DATA (sym)->attr.codimension)
6111 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
6113 gfc_ref *ref, *newref;
6115 newref = gfc_get_ref ();
6116 newref->type = REF_ARRAY;
6117 newref->u.ar.type = AR_FULL;
6118 newref->u.ar.dimen = 0;
6119 /* Because this is an associate var and the first ref either is a ref to
6120 the _data component or not, no traversal of the ref chain is
6121 needed. The array ref needs to be inserted after the _data ref,
6122 or when that is not present, which may happened for polymorphic
6123 types, then at the first position. */
6124 ref = e->ref;
6125 if (!ref)
6126 e->ref = newref;
6127 else if (ref->type == REF_COMPONENT
6128 && strcmp ("_data", ref->u.c.component->name) == 0)
6130 if (!ref->next || ref->next->type != REF_ARRAY)
6132 newref->next = ref->next;
6133 ref->next = newref;
6135 else
6136 /* Array ref present already. */
6137 gfc_free_ref_list (newref);
6139 else if (ref->type == REF_ARRAY)
6140 /* Array ref present already. */
6141 gfc_free_ref_list (newref);
6142 else
6144 newref->next = ref;
6145 e->ref = newref;
6149 if (e->ref && !gfc_resolve_ref (e))
6150 return false;
6152 if (sym->attr.flavor == FL_PROCEDURE
6153 && (!sym->attr.function
6154 || (sym->attr.function && sym->result
6155 && sym->result->attr.proc_pointer
6156 && !sym->result->attr.function)))
6158 e->ts.type = BT_PROCEDURE;
6159 goto resolve_procedure;
6162 if (sym->ts.type != BT_UNKNOWN)
6163 gfc_variable_attr (e, &e->ts);
6164 else if (sym->attr.flavor == FL_PROCEDURE
6165 && sym->attr.function && sym->result
6166 && sym->result->ts.type != BT_UNKNOWN
6167 && sym->result->attr.proc_pointer)
6168 e->ts = sym->result->ts;
6169 else
6171 /* Must be a simple variable reference. */
6172 if (!gfc_set_default_type (sym, 1, sym->ns))
6173 return false;
6174 e->ts = sym->ts;
6177 if (check_assumed_size_reference (sym, e))
6178 return false;
6180 /* Deal with forward references to entries during gfc_resolve_code, to
6181 satisfy, at least partially, 12.5.2.5. */
6182 if (gfc_current_ns->entries
6183 && current_entry_id == sym->entry_id
6184 && cs_base
6185 && cs_base->current
6186 && cs_base->current->op != EXEC_ENTRY)
6188 gfc_entry_list *entry;
6189 gfc_formal_arglist *formal;
6190 int n;
6191 bool seen, saved_specification_expr;
6193 /* If the symbol is a dummy... */
6194 if (sym->attr.dummy && sym->ns == gfc_current_ns)
6196 entry = gfc_current_ns->entries;
6197 seen = false;
6199 /* ...test if the symbol is a parameter of previous entries. */
6200 for (; entry && entry->id <= current_entry_id; entry = entry->next)
6201 for (formal = entry->sym->formal; formal; formal = formal->next)
6203 if (formal->sym && sym->name == formal->sym->name)
6205 seen = true;
6206 break;
6210 /* If it has not been seen as a dummy, this is an error. */
6211 if (!seen)
6213 if (specification_expr)
6214 gfc_error ("Variable %qs, used in a specification expression"
6215 ", is referenced at %L before the ENTRY statement "
6216 "in which it is a parameter",
6217 sym->name, &cs_base->current->loc);
6218 else
6219 gfc_error ("Variable %qs is used at %L before the ENTRY "
6220 "statement in which it is a parameter",
6221 sym->name, &cs_base->current->loc);
6222 t = false;
6226 /* Now do the same check on the specification expressions. */
6227 saved_specification_expr = specification_expr;
6228 specification_expr = true;
6229 if (sym->ts.type == BT_CHARACTER
6230 && !gfc_resolve_expr (sym->ts.u.cl->length))
6231 t = false;
6233 if (sym->as)
6234 for (n = 0; n < sym->as->rank; n++)
6236 if (!gfc_resolve_expr (sym->as->lower[n]))
6237 t = false;
6238 if (!gfc_resolve_expr (sym->as->upper[n]))
6239 t = false;
6241 specification_expr = saved_specification_expr;
6243 if (t)
6244 /* Update the symbol's entry level. */
6245 sym->entry_id = current_entry_id + 1;
6248 /* If a symbol has been host_associated mark it. This is used latter,
6249 to identify if aliasing is possible via host association. */
6250 if (sym->attr.flavor == FL_VARIABLE
6251 && (!sym->ns->code || sym->ns->code->op != EXEC_BLOCK
6252 || !sym->ns->code->ext.block.assoc)
6253 && gfc_current_ns->parent
6254 && (gfc_current_ns->parent == sym->ns
6255 || (gfc_current_ns->parent->parent
6256 && gfc_current_ns->parent->parent == sym->ns)))
6257 sym->attr.host_assoc = 1;
6259 if (gfc_current_ns->proc_name
6260 && sym->attr.dimension
6261 && (sym->ns != gfc_current_ns
6262 || sym->attr.use_assoc
6263 || sym->attr.in_common))
6264 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
6266 resolve_procedure:
6267 if (t && !resolve_procedure_expression (e))
6268 t = false;
6270 /* F2008, C617 and C1229. */
6271 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
6272 && gfc_is_coindexed (e))
6274 gfc_ref *ref, *ref2 = NULL;
6276 for (ref = e->ref; ref; ref = ref->next)
6278 if (ref->type == REF_COMPONENT)
6279 ref2 = ref;
6280 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
6281 break;
6284 for ( ; ref; ref = ref->next)
6285 if (ref->type == REF_COMPONENT)
6286 break;
6288 /* Expression itself is not coindexed object. */
6289 if (ref && e->ts.type == BT_CLASS)
6291 gfc_error ("Polymorphic subobject of coindexed object at %L",
6292 &e->where);
6293 t = false;
6296 /* Expression itself is coindexed object. */
6297 if (ref == NULL)
6299 gfc_component *c;
6300 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
6301 for ( ; c; c = c->next)
6302 if (c->attr.allocatable && c->ts.type == BT_CLASS)
6304 gfc_error ("Coindexed object with polymorphic allocatable "
6305 "subcomponent at %L", &e->where);
6306 t = false;
6307 break;
6312 if (t)
6313 gfc_expression_rank (e);
6315 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
6316 add_caf_get_intrinsic (e);
6318 if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
6319 gfc_warning (OPT_Wdeprecated_declarations,
6320 "Using variable %qs at %L is deprecated",
6321 sym->name, &e->where);
6322 /* Simplify cases where access to a parameter array results in a
6323 single constant. Suppress errors since those will have been
6324 issued before, as warnings. */
6325 if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
6327 gfc_push_suppress_errors ();
6328 gfc_simplify_expr (e, 1);
6329 gfc_pop_suppress_errors ();
6332 return t;
6336 /* 'sym' was initially guessed to be derived type but has been corrected
6337 in resolve_assoc_var to be a class entity or the derived type correcting.
6338 If a class entity it will certainly need the _data reference or the
6339 reference derived type symbol correcting in the first component ref if
6340 a derived type. */
6342 void
6343 gfc_fixup_inferred_type_refs (gfc_expr *e)
6345 gfc_ref *ref, *new_ref;
6346 gfc_symbol *sym, *derived;
6347 gfc_expr *target;
6348 sym = e->symtree->n.sym;
6350 /* An associate_name whose selector is (i) a component ref of a selector
6351 that is a inferred type associate_name; or (ii) an intrinsic type that
6352 has been inferred from an inquiry ref. */
6353 if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
6355 sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
6356 sym->attr.codimension = sym->assoc->target->corank ? 1 : 0;
6357 if (!sym->attr.dimension && e->ref->type == REF_ARRAY)
6359 ref = e->ref;
6360 /* A substring misidentified as an array section. */
6361 if (sym->ts.type == BT_CHARACTER
6362 && ref->u.ar.start[0] && ref->u.ar.end[0]
6363 && !ref->u.ar.stride[0])
6365 new_ref = gfc_get_ref ();
6366 new_ref->type = REF_SUBSTRING;
6367 new_ref->u.ss.start = ref->u.ar.start[0];
6368 new_ref->u.ss.end = ref->u.ar.end[0];
6369 new_ref->u.ss.length = sym->ts.u.cl;
6370 *ref = *new_ref;
6371 free (new_ref);
6373 else
6375 if (e->ref->u.ar.type == AR_UNKNOWN)
6376 gfc_error ("Invalid array reference at %L", &e->where);
6377 e->ref = ref->next;
6378 free (ref);
6382 /* It is possible for an inquiry reference to be mistaken for a
6383 component reference. Correct this now. */
6384 ref = e->ref;
6385 if (ref && ref->type == REF_ARRAY)
6386 ref = ref->next;
6387 if (ref && ref->type == REF_COMPONENT
6388 && is_inquiry_ref (ref->u.c.component->name, &new_ref))
6390 e->symtree->n.sym = sym;
6391 *ref = *new_ref;
6392 gfc_free_ref_list (new_ref);
6395 /* The kind of the associate name is best evaluated directly from the
6396 selector because of the guesses made in primary.cc, when the type
6397 is still unknown. */
6398 if (ref && ref->type == REF_INQUIRY && ref->u.i == INQUIRY_KIND)
6400 gfc_expr *ne = gfc_get_int_expr (gfc_default_integer_kind, &e->where,
6401 sym->assoc->target->ts.kind);
6402 gfc_replace_expr (e, ne);
6405 /* Now that the references are all sorted out, set the expression rank
6406 and return. */
6407 gfc_expression_rank (e);
6408 return;
6411 derived = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->ts.u.derived
6412 : sym->ts.u.derived;
6414 /* Ensure that class symbols have an array spec and ensure that there
6415 is a _data field reference following class type references. */
6416 if (sym->ts.type == BT_CLASS
6417 && sym->assoc->target->ts.type == BT_CLASS)
6419 e->rank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->rank : 0;
6420 e->corank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->corank : 0;
6421 sym->attr.dimension = 0;
6422 sym->attr.codimension = 0;
6423 CLASS_DATA (sym)->attr.dimension = e->rank ? 1 : 0;
6424 CLASS_DATA (sym)->attr.codimension = e->corank ? 1 : 0;
6425 if (e->ref && (e->ref->type != REF_COMPONENT
6426 || e->ref->u.c.component->name[0] != '_'))
6428 ref = gfc_get_ref ();
6429 ref->type = REF_COMPONENT;
6430 ref->next = e->ref;
6431 e->ref = ref;
6432 ref->u.c.component = gfc_find_component (sym->ts.u.derived, "_data",
6433 true, true, NULL);
6434 ref->u.c.sym = sym->ts.u.derived;
6438 /* Proceed as far as the first component reference and ensure that the
6439 correct derived type is being used. */
6440 for (ref = e->ref; ref; ref = ref->next)
6441 if (ref->type == REF_COMPONENT)
6443 if (ref->u.c.component->name[0] != '_')
6444 ref->u.c.sym = derived;
6445 else
6446 ref->u.c.sym = sym->ts.u.derived;
6447 break;
6450 /* Verify that the type inferrence mechanism has not introduced a spurious
6451 array reference. This can happen with an associate name, whose selector
6452 is an element of another inferred type. */
6453 target = e->symtree->n.sym->assoc->target;
6454 if (!(sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as)
6455 && e != target && !target->rank)
6457 /* First case: array ref after the scalar class or derived
6458 associate_name. */
6459 if (e->ref && e->ref->type == REF_ARRAY
6460 && e->ref->u.ar.type != AR_ELEMENT)
6462 ref = e->ref;
6463 if (ref->u.ar.type == AR_UNKNOWN)
6464 gfc_error ("Invalid array reference at %L", &e->where);
6465 e->ref = ref->next;
6466 free (ref);
6468 /* If it hasn't a ref to the '_data' field supply one. */
6469 if (sym->ts.type == BT_CLASS
6470 && !(e->ref->type == REF_COMPONENT
6471 && strcmp (e->ref->u.c.component->name, "_data")))
6473 gfc_ref *new_ref;
6474 gfc_find_component (e->symtree->n.sym->ts.u.derived,
6475 "_data", true, true, &new_ref);
6476 new_ref->next = e->ref;
6477 e->ref = new_ref;
6480 /* 2nd case: a ref to the '_data' field followed by an array ref. */
6481 else if (e->ref && e->ref->type == REF_COMPONENT
6482 && strcmp (e->ref->u.c.component->name, "_data") == 0
6483 && e->ref->next && e->ref->next->type == REF_ARRAY
6484 && e->ref->next->u.ar.type != AR_ELEMENT)
6486 ref = e->ref->next;
6487 if (ref->u.ar.type == AR_UNKNOWN)
6488 gfc_error ("Invalid array reference at %L", &e->where);
6489 e->ref->next = e->ref->next->next;
6490 free (ref);
6494 /* Now that all the references are OK, get the expression rank. */
6495 gfc_expression_rank (e);
6499 /* Checks to see that the correct symbol has been host associated.
6500 The only situations where this arises are:
6501 (i) That in which a twice contained function is parsed after
6502 the host association is made. On detecting this, change
6503 the symbol in the expression and convert the array reference
6504 into an actual arglist if the old symbol is a variable; or
6505 (ii) That in which an external function is typed but not declared
6506 explicitly to be external. Here, the old symbol is changed
6507 from a variable to an external function. */
6508 static bool
6509 check_host_association (gfc_expr *e)
6511 gfc_symbol *sym, *old_sym;
6512 gfc_symtree *st;
6513 int n;
6514 gfc_ref *ref;
6515 gfc_actual_arglist *arg, *tail = NULL;
6516 bool retval = e->expr_type == EXPR_FUNCTION;
6518 /* If the expression is the result of substitution in
6519 interface.cc(gfc_extend_expr) because there is no way in
6520 which the host association can be wrong. */
6521 if (e->symtree == NULL
6522 || e->symtree->n.sym == NULL
6523 || e->user_operator)
6524 return retval;
6526 old_sym = e->symtree->n.sym;
6528 if (gfc_current_ns->parent
6529 && old_sym->ns != gfc_current_ns)
6531 /* Use the 'USE' name so that renamed module symbols are
6532 correctly handled. */
6533 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
6535 if (sym && old_sym != sym
6536 && sym->attr.flavor == FL_PROCEDURE
6537 && sym->attr.contained)
6539 /* Clear the shape, since it might not be valid. */
6540 gfc_free_shape (&e->shape, e->rank);
6542 /* Give the expression the right symtree! */
6543 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
6544 gcc_assert (st != NULL);
6546 if (old_sym->attr.flavor == FL_PROCEDURE
6547 || e->expr_type == EXPR_FUNCTION)
6549 /* Original was function so point to the new symbol, since
6550 the actual argument list is already attached to the
6551 expression. */
6552 e->value.function.esym = NULL;
6553 e->symtree = st;
6555 else
6557 /* Original was variable so convert array references into
6558 an actual arglist. This does not need any checking now
6559 since resolve_function will take care of it. */
6560 e->value.function.actual = NULL;
6561 e->expr_type = EXPR_FUNCTION;
6562 e->symtree = st;
6564 /* Ambiguity will not arise if the array reference is not
6565 the last reference. */
6566 for (ref = e->ref; ref; ref = ref->next)
6567 if (ref->type == REF_ARRAY && ref->next == NULL)
6568 break;
6570 if ((ref == NULL || ref->type != REF_ARRAY)
6571 && sym->attr.proc == PROC_INTERNAL)
6573 gfc_error ("%qs at %L is host associated at %L into "
6574 "a contained procedure with an internal "
6575 "procedure of the same name", sym->name,
6576 &old_sym->declared_at, &e->where);
6577 return false;
6580 if (ref == NULL)
6581 return false;
6583 gcc_assert (ref->type == REF_ARRAY);
6585 /* Grab the start expressions from the array ref and
6586 copy them into actual arguments. */
6587 for (n = 0; n < ref->u.ar.dimen; n++)
6589 arg = gfc_get_actual_arglist ();
6590 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
6591 if (e->value.function.actual == NULL)
6592 tail = e->value.function.actual = arg;
6593 else
6595 tail->next = arg;
6596 tail = arg;
6600 /* Dump the reference list and set the rank. */
6601 gfc_free_ref_list (e->ref);
6602 e->ref = NULL;
6603 e->rank = sym->as ? sym->as->rank : 0;
6604 e->corank = sym->as ? sym->as->corank : 0;
6607 gfc_resolve_expr (e);
6608 sym->refs++;
6610 /* This case corresponds to a call, from a block or a contained
6611 procedure, to an external function, which has not been declared
6612 as being external in the main program but has been typed. */
6613 else if (sym && old_sym != sym
6614 && !e->ref
6615 && sym->ts.type == BT_UNKNOWN
6616 && old_sym->ts.type != BT_UNKNOWN
6617 && sym->attr.flavor == FL_PROCEDURE
6618 && old_sym->attr.flavor == FL_VARIABLE
6619 && sym->ns->parent == old_sym->ns
6620 && sym->ns->proc_name
6621 && sym->ns->proc_name->attr.proc != PROC_MODULE
6622 && (sym->ns->proc_name->attr.flavor == FL_LABEL
6623 || sym->ns->proc_name->attr.flavor == FL_PROCEDURE))
6625 old_sym->attr.flavor = FL_PROCEDURE;
6626 old_sym->attr.external = 1;
6627 old_sym->attr.function = 1;
6628 old_sym->result = old_sym;
6629 gfc_resolve_expr (e);
6632 /* This might have changed! */
6633 return e->expr_type == EXPR_FUNCTION;
6637 static void
6638 gfc_resolve_character_operator (gfc_expr *e)
6640 gfc_expr *op1 = e->value.op.op1;
6641 gfc_expr *op2 = e->value.op.op2;
6642 gfc_expr *e1 = NULL;
6643 gfc_expr *e2 = NULL;
6645 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
6647 if (op1->ts.u.cl && op1->ts.u.cl->length)
6648 e1 = gfc_copy_expr (op1->ts.u.cl->length);
6649 else if (op1->expr_type == EXPR_CONSTANT)
6650 e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6651 op1->value.character.length);
6653 if (op2->ts.u.cl && op2->ts.u.cl->length)
6654 e2 = gfc_copy_expr (op2->ts.u.cl->length);
6655 else if (op2->expr_type == EXPR_CONSTANT)
6656 e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6657 op2->value.character.length);
6659 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6661 if (!e1 || !e2)
6663 gfc_free_expr (e1);
6664 gfc_free_expr (e2);
6666 return;
6669 e->ts.u.cl->length = gfc_add (e1, e2);
6670 e->ts.u.cl->length->ts.type = BT_INTEGER;
6671 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
6672 gfc_simplify_expr (e->ts.u.cl->length, 0);
6673 gfc_resolve_expr (e->ts.u.cl->length);
6675 return;
6679 /* Ensure that an character expression has a charlen and, if possible, a
6680 length expression. */
6682 static void
6683 fixup_charlen (gfc_expr *e)
6685 /* The cases fall through so that changes in expression type and the need
6686 for multiple fixes are picked up. In all circumstances, a charlen should
6687 be available for the middle end to hang a backend_decl on. */
6688 switch (e->expr_type)
6690 case EXPR_OP:
6691 gfc_resolve_character_operator (e);
6692 /* FALLTHRU */
6694 case EXPR_ARRAY:
6695 if (e->expr_type == EXPR_ARRAY)
6696 gfc_resolve_character_array_constructor (e);
6697 /* FALLTHRU */
6699 case EXPR_SUBSTRING:
6700 if (!e->ts.u.cl && e->ref)
6701 gfc_resolve_substring_charlen (e);
6702 /* FALLTHRU */
6704 default:
6705 if (!e->ts.u.cl)
6706 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6708 break;
6713 /* Update an actual argument to include the passed-object for type-bound
6714 procedures at the right position. */
6716 static gfc_actual_arglist*
6717 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
6718 const char *name)
6720 gcc_assert (argpos > 0);
6722 if (argpos == 1)
6724 gfc_actual_arglist* result;
6726 result = gfc_get_actual_arglist ();
6727 result->expr = po;
6728 result->next = lst;
6729 if (name)
6730 result->name = name;
6732 return result;
6735 if (lst)
6736 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
6737 else
6738 lst = update_arglist_pass (NULL, po, argpos - 1, name);
6739 return lst;
6743 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
6745 static gfc_expr*
6746 extract_compcall_passed_object (gfc_expr* e)
6748 gfc_expr* po;
6750 if (e->expr_type == EXPR_UNKNOWN)
6752 gfc_error ("Error in typebound call at %L",
6753 &e->where);
6754 return NULL;
6757 gcc_assert (e->expr_type == EXPR_COMPCALL);
6759 if (e->value.compcall.base_object)
6760 po = gfc_copy_expr (e->value.compcall.base_object);
6761 else
6763 po = gfc_get_expr ();
6764 po->expr_type = EXPR_VARIABLE;
6765 po->symtree = e->symtree;
6766 po->ref = gfc_copy_ref (e->ref);
6767 po->where = e->where;
6770 if (!gfc_resolve_expr (po))
6771 return NULL;
6773 return po;
6777 /* Update the arglist of an EXPR_COMPCALL expression to include the
6778 passed-object. */
6780 static bool
6781 update_compcall_arglist (gfc_expr* e)
6783 gfc_expr* po;
6784 gfc_typebound_proc* tbp;
6786 tbp = e->value.compcall.tbp;
6788 if (tbp->error)
6789 return false;
6791 po = extract_compcall_passed_object (e);
6792 if (!po)
6793 return false;
6795 if (tbp->nopass || e->value.compcall.ignore_pass)
6797 gfc_free_expr (po);
6798 return true;
6801 if (tbp->pass_arg_num <= 0)
6802 return false;
6804 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6805 tbp->pass_arg_num,
6806 tbp->pass_arg);
6808 return true;
6812 /* Extract the passed object from a PPC call (a copy of it). */
6814 static gfc_expr*
6815 extract_ppc_passed_object (gfc_expr *e)
6817 gfc_expr *po;
6818 gfc_ref **ref;
6820 po = gfc_get_expr ();
6821 po->expr_type = EXPR_VARIABLE;
6822 po->symtree = e->symtree;
6823 po->ref = gfc_copy_ref (e->ref);
6824 po->where = e->where;
6826 /* Remove PPC reference. */
6827 ref = &po->ref;
6828 while ((*ref)->next)
6829 ref = &(*ref)->next;
6830 gfc_free_ref_list (*ref);
6831 *ref = NULL;
6833 if (!gfc_resolve_expr (po))
6834 return NULL;
6836 return po;
6840 /* Update the actual arglist of a procedure pointer component to include the
6841 passed-object. */
6843 static bool
6844 update_ppc_arglist (gfc_expr* e)
6846 gfc_expr* po;
6847 gfc_component *ppc;
6848 gfc_typebound_proc* tb;
6850 ppc = gfc_get_proc_ptr_comp (e);
6851 if (!ppc)
6852 return false;
6854 tb = ppc->tb;
6856 if (tb->error)
6857 return false;
6858 else if (tb->nopass)
6859 return true;
6861 po = extract_ppc_passed_object (e);
6862 if (!po)
6863 return false;
6865 /* F08:R739. */
6866 if (po->rank != 0)
6868 gfc_error ("Passed-object at %L must be scalar", &e->where);
6869 return false;
6872 /* F08:C611. */
6873 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
6875 gfc_error ("Base object for procedure-pointer component call at %L is of"
6876 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
6877 return false;
6880 gcc_assert (tb->pass_arg_num > 0);
6881 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6882 tb->pass_arg_num,
6883 tb->pass_arg);
6885 return true;
6889 /* Check that the object a TBP is called on is valid, i.e. it must not be
6890 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
6892 static bool
6893 check_typebound_baseobject (gfc_expr* e)
6895 gfc_expr* base;
6896 bool return_value = false;
6898 base = extract_compcall_passed_object (e);
6899 if (!base)
6900 return false;
6902 if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
6904 gfc_error ("Error in typebound call at %L", &e->where);
6905 goto cleanup;
6908 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
6909 return false;
6911 /* F08:C611. */
6912 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
6914 gfc_error ("Base object for type-bound procedure call at %L is of"
6915 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
6916 goto cleanup;
6919 /* F08:C1230. If the procedure called is NOPASS,
6920 the base object must be scalar. */
6921 if (e->value.compcall.tbp->nopass && base->rank != 0)
6923 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
6924 " be scalar", &e->where);
6925 goto cleanup;
6928 return_value = true;
6930 cleanup:
6931 gfc_free_expr (base);
6932 return return_value;
6936 /* Resolve a call to a type-bound procedure, either function or subroutine,
6937 statically from the data in an EXPR_COMPCALL expression. The adapted
6938 arglist and the target-procedure symtree are returned. */
6940 static bool
6941 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
6942 gfc_actual_arglist** actual)
6944 gcc_assert (e->expr_type == EXPR_COMPCALL);
6945 gcc_assert (!e->value.compcall.tbp->is_generic);
6947 /* Update the actual arglist for PASS. */
6948 if (!update_compcall_arglist (e))
6949 return false;
6951 *actual = e->value.compcall.actual;
6952 *target = e->value.compcall.tbp->u.specific;
6954 gfc_free_ref_list (e->ref);
6955 e->ref = NULL;
6956 e->value.compcall.actual = NULL;
6958 /* If we find a deferred typebound procedure, check for derived types
6959 that an overriding typebound procedure has not been missed. */
6960 if (e->value.compcall.name
6961 && !e->value.compcall.tbp->non_overridable
6962 && e->value.compcall.base_object
6963 && e->value.compcall.base_object->ts.type == BT_DERIVED)
6965 gfc_symtree *st;
6966 gfc_symbol *derived;
6968 /* Use the derived type of the base_object. */
6969 derived = e->value.compcall.base_object->ts.u.derived;
6970 st = NULL;
6972 /* If necessary, go through the inheritance chain. */
6973 while (!st && derived)
6975 /* Look for the typebound procedure 'name'. */
6976 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
6977 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
6978 e->value.compcall.name);
6979 if (!st)
6980 derived = gfc_get_derived_super_type (derived);
6983 /* Now find the specific name in the derived type namespace. */
6984 if (st && st->n.tb && st->n.tb->u.specific)
6985 gfc_find_sym_tree (st->n.tb->u.specific->name,
6986 derived->ns, 1, &st);
6987 if (st)
6988 *target = st;
6991 if (is_illegal_recursion ((*target)->n.sym, gfc_current_ns)
6992 && !e->value.compcall.tbp->deferred)
6993 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
6994 " itself recursively. Declare it RECURSIVE or use"
6995 " %<-frecursive%>", (*target)->n.sym->name, &e->where);
6997 return true;
7001 /* Get the ultimate declared type from an expression. In addition,
7002 return the last class/derived type reference and the copy of the
7003 reference list. If check_types is set true, derived types are
7004 identified as well as class references. */
7005 static gfc_symbol*
7006 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
7007 gfc_expr *e, bool check_types)
7009 gfc_symbol *declared;
7010 gfc_ref *ref;
7012 declared = NULL;
7013 if (class_ref)
7014 *class_ref = NULL;
7015 if (new_ref)
7016 *new_ref = gfc_copy_ref (e->ref);
7018 for (ref = e->ref; ref; ref = ref->next)
7020 if (ref->type != REF_COMPONENT)
7021 continue;
7023 if ((ref->u.c.component->ts.type == BT_CLASS
7024 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
7025 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
7027 declared = ref->u.c.component->ts.u.derived;
7028 if (class_ref)
7029 *class_ref = ref;
7033 if (declared == NULL)
7034 declared = e->symtree->n.sym->ts.u.derived;
7036 return declared;
7040 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
7041 which of the specific bindings (if any) matches the arglist and transform
7042 the expression into a call of that binding. */
7044 static bool
7045 resolve_typebound_generic_call (gfc_expr* e, const char **name)
7047 gfc_typebound_proc* genproc;
7048 const char* genname;
7049 gfc_symtree *st;
7050 gfc_symbol *derived;
7052 gcc_assert (e->expr_type == EXPR_COMPCALL);
7053 genname = e->value.compcall.name;
7054 genproc = e->value.compcall.tbp;
7056 if (!genproc->is_generic)
7057 return true;
7059 /* Try the bindings on this type and in the inheritance hierarchy. */
7060 for (; genproc; genproc = genproc->overridden)
7062 gfc_tbp_generic* g;
7064 gcc_assert (genproc->is_generic);
7065 for (g = genproc->u.generic; g; g = g->next)
7067 gfc_symbol* target;
7068 gfc_actual_arglist* args;
7069 bool matches;
7071 gcc_assert (g->specific);
7073 if (g->specific->error)
7074 continue;
7076 target = g->specific->u.specific->n.sym;
7078 /* Get the right arglist by handling PASS/NOPASS. */
7079 args = gfc_copy_actual_arglist (e->value.compcall.actual);
7080 if (!g->specific->nopass)
7082 gfc_expr* po;
7083 po = extract_compcall_passed_object (e);
7084 if (!po)
7086 gfc_free_actual_arglist (args);
7087 return false;
7090 gcc_assert (g->specific->pass_arg_num > 0);
7091 gcc_assert (!g->specific->error);
7092 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
7093 g->specific->pass_arg);
7095 resolve_actual_arglist (args, target->attr.proc,
7096 is_external_proc (target)
7097 && gfc_sym_get_dummy_args (target) == NULL);
7099 /* Check if this arglist matches the formal. */
7100 matches = gfc_arglist_matches_symbol (&args, target);
7102 /* Clean up and break out of the loop if we've found it. */
7103 gfc_free_actual_arglist (args);
7104 if (matches)
7106 e->value.compcall.tbp = g->specific;
7107 genname = g->specific_st->name;
7108 /* Pass along the name for CLASS methods, where the vtab
7109 procedure pointer component has to be referenced. */
7110 if (name)
7111 *name = genname;
7112 goto success;
7117 /* Nothing matching found! */
7118 gfc_error ("Found no matching specific binding for the call to the GENERIC"
7119 " %qs at %L", genname, &e->where);
7120 return false;
7122 success:
7123 /* Make sure that we have the right specific instance for the name. */
7124 derived = get_declared_from_expr (NULL, NULL, e, true);
7126 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
7127 if (st)
7128 e->value.compcall.tbp = st->n.tb;
7130 return true;
7134 /* Resolve a call to a type-bound subroutine. */
7136 static bool
7137 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
7139 gfc_actual_arglist* newactual;
7140 gfc_symtree* target;
7142 /* Check that's really a SUBROUTINE. */
7143 if (!c->expr1->value.compcall.tbp->subroutine)
7145 if (!c->expr1->value.compcall.tbp->is_generic
7146 && c->expr1->value.compcall.tbp->u.specific
7147 && c->expr1->value.compcall.tbp->u.specific->n.sym
7148 && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
7149 c->expr1->value.compcall.tbp->subroutine = 1;
7150 else
7152 gfc_error ("%qs at %L should be a SUBROUTINE",
7153 c->expr1->value.compcall.name, &c->loc);
7154 return false;
7158 if (!check_typebound_baseobject (c->expr1))
7159 return false;
7161 /* Pass along the name for CLASS methods, where the vtab
7162 procedure pointer component has to be referenced. */
7163 if (name)
7164 *name = c->expr1->value.compcall.name;
7166 if (!resolve_typebound_generic_call (c->expr1, name))
7167 return false;
7169 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
7170 if (overridable)
7171 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
7173 /* Transform into an ordinary EXEC_CALL for now. */
7175 if (!resolve_typebound_static (c->expr1, &target, &newactual))
7176 return false;
7178 c->ext.actual = newactual;
7179 c->symtree = target;
7180 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
7182 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
7184 gfc_free_expr (c->expr1);
7185 c->expr1 = gfc_get_expr ();
7186 c->expr1->expr_type = EXPR_FUNCTION;
7187 c->expr1->symtree = target;
7188 c->expr1->where = c->loc;
7190 return resolve_call (c);
7194 /* Resolve a component-call expression. */
7195 static bool
7196 resolve_compcall (gfc_expr* e, const char **name)
7198 gfc_actual_arglist* newactual;
7199 gfc_symtree* target;
7201 /* Check that's really a FUNCTION. */
7202 if (!e->value.compcall.tbp->function)
7204 gfc_error ("%qs at %L should be a FUNCTION",
7205 e->value.compcall.name, &e->where);
7206 return false;
7210 /* These must not be assign-calls! */
7211 gcc_assert (!e->value.compcall.assign);
7213 if (!check_typebound_baseobject (e))
7214 return false;
7216 /* Pass along the name for CLASS methods, where the vtab
7217 procedure pointer component has to be referenced. */
7218 if (name)
7219 *name = e->value.compcall.name;
7221 if (!resolve_typebound_generic_call (e, name))
7222 return false;
7223 gcc_assert (!e->value.compcall.tbp->is_generic);
7225 /* Take the rank from the function's symbol. */
7226 if (e->value.compcall.tbp->u.specific->n.sym->as)
7228 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
7229 e->corank = e->value.compcall.tbp->u.specific->n.sym->as->corank;
7232 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
7233 arglist to the TBP's binding target. */
7235 if (!resolve_typebound_static (e, &target, &newactual))
7236 return false;
7238 e->value.function.actual = newactual;
7239 e->value.function.name = NULL;
7240 e->value.function.esym = target->n.sym;
7241 e->value.function.isym = NULL;
7242 e->symtree = target;
7243 e->ts = target->n.sym->ts;
7244 e->expr_type = EXPR_FUNCTION;
7246 /* Resolution is not necessary if this is a class subroutine; this
7247 function only has to identify the specific proc. Resolution of
7248 the call will be done next in resolve_typebound_call. */
7249 return gfc_resolve_expr (e);
7253 static bool resolve_fl_derived (gfc_symbol *sym);
7256 /* Resolve a typebound function, or 'method'. First separate all
7257 the non-CLASS references by calling resolve_compcall directly. */
7259 static bool
7260 resolve_typebound_function (gfc_expr* e)
7262 gfc_symbol *declared;
7263 gfc_component *c;
7264 gfc_ref *new_ref;
7265 gfc_ref *class_ref;
7266 gfc_symtree *st;
7267 const char *name;
7268 gfc_typespec ts;
7269 gfc_expr *expr;
7270 bool overridable;
7272 st = e->symtree;
7274 /* Deal with typebound operators for CLASS objects. */
7275 expr = e->value.compcall.base_object;
7276 overridable = !e->value.compcall.tbp->non_overridable;
7277 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
7279 /* Since the typebound operators are generic, we have to ensure
7280 that any delays in resolution are corrected and that the vtab
7281 is present. */
7282 ts = expr->ts;
7283 declared = ts.u.derived;
7284 c = gfc_find_component (declared, "_vptr", true, true, NULL);
7285 if (c->ts.u.derived == NULL)
7286 c->ts.u.derived = gfc_find_derived_vtab (declared);
7288 if (!resolve_compcall (e, &name))
7289 return false;
7291 /* Use the generic name if it is there. */
7292 name = name ? name : e->value.function.esym->name;
7293 e->symtree = expr->symtree;
7294 e->ref = gfc_copy_ref (expr->ref);
7295 get_declared_from_expr (&class_ref, NULL, e, false);
7297 /* Trim away the extraneous references that emerge from nested
7298 use of interface.cc (extend_expr). */
7299 if (class_ref && class_ref->next)
7301 gfc_free_ref_list (class_ref->next);
7302 class_ref->next = NULL;
7304 else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
7306 gfc_free_ref_list (e->ref);
7307 e->ref = NULL;
7310 gfc_add_vptr_component (e);
7311 gfc_add_component_ref (e, name);
7312 e->value.function.esym = NULL;
7313 if (expr->expr_type != EXPR_VARIABLE)
7314 e->base_expr = expr;
7315 return true;
7318 if (st == NULL)
7319 return resolve_compcall (e, NULL);
7321 if (!gfc_resolve_ref (e))
7322 return false;
7324 /* Get the CLASS declared type. */
7325 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
7327 if (!resolve_fl_derived (declared))
7328 return false;
7330 /* Weed out cases of the ultimate component being a derived type. */
7331 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
7332 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
7334 gfc_free_ref_list (new_ref);
7335 return resolve_compcall (e, NULL);
7338 c = gfc_find_component (declared, "_data", true, true, NULL);
7340 /* Treat the call as if it is a typebound procedure, in order to roll
7341 out the correct name for the specific function. */
7342 if (!resolve_compcall (e, &name))
7344 gfc_free_ref_list (new_ref);
7345 return false;
7347 ts = e->ts;
7349 if (overridable)
7351 /* Convert the expression to a procedure pointer component call. */
7352 e->value.function.esym = NULL;
7353 e->symtree = st;
7355 if (new_ref)
7356 e->ref = new_ref;
7358 /* '_vptr' points to the vtab, which contains the procedure pointers. */
7359 gfc_add_vptr_component (e);
7360 gfc_add_component_ref (e, name);
7362 /* Recover the typespec for the expression. This is really only
7363 necessary for generic procedures, where the additional call
7364 to gfc_add_component_ref seems to throw the collection of the
7365 correct typespec. */
7366 e->ts = ts;
7368 else if (new_ref)
7369 gfc_free_ref_list (new_ref);
7371 return true;
7374 /* Resolve a typebound subroutine, or 'method'. First separate all
7375 the non-CLASS references by calling resolve_typebound_call
7376 directly. */
7378 static bool
7379 resolve_typebound_subroutine (gfc_code *code)
7381 gfc_symbol *declared;
7382 gfc_component *c;
7383 gfc_ref *new_ref;
7384 gfc_ref *class_ref;
7385 gfc_symtree *st;
7386 const char *name;
7387 gfc_typespec ts;
7388 gfc_expr *expr;
7389 bool overridable;
7391 st = code->expr1->symtree;
7393 /* Deal with typebound operators for CLASS objects. */
7394 expr = code->expr1->value.compcall.base_object;
7395 overridable = !code->expr1->value.compcall.tbp->non_overridable;
7396 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
7398 /* If the base_object is not a variable, the corresponding actual
7399 argument expression must be stored in e->base_expression so
7400 that the corresponding tree temporary can be used as the base
7401 object in gfc_conv_procedure_call. */
7402 if (expr->expr_type != EXPR_VARIABLE)
7404 gfc_actual_arglist *args;
7406 args= code->expr1->value.function.actual;
7407 for (; args; args = args->next)
7408 if (expr == args->expr)
7409 expr = args->expr;
7412 /* Since the typebound operators are generic, we have to ensure
7413 that any delays in resolution are corrected and that the vtab
7414 is present. */
7415 declared = expr->ts.u.derived;
7416 c = gfc_find_component (declared, "_vptr", true, true, NULL);
7417 if (c->ts.u.derived == NULL)
7418 c->ts.u.derived = gfc_find_derived_vtab (declared);
7420 if (!resolve_typebound_call (code, &name, NULL))
7421 return false;
7423 /* Use the generic name if it is there. */
7424 name = name ? name : code->expr1->value.function.esym->name;
7425 code->expr1->symtree = expr->symtree;
7426 code->expr1->ref = gfc_copy_ref (expr->ref);
7428 /* Trim away the extraneous references that emerge from nested
7429 use of interface.cc (extend_expr). */
7430 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
7431 if (class_ref && class_ref->next)
7433 gfc_free_ref_list (class_ref->next);
7434 class_ref->next = NULL;
7436 else if (code->expr1->ref && !class_ref)
7438 gfc_free_ref_list (code->expr1->ref);
7439 code->expr1->ref = NULL;
7442 /* Now use the procedure in the vtable. */
7443 gfc_add_vptr_component (code->expr1);
7444 gfc_add_component_ref (code->expr1, name);
7445 code->expr1->value.function.esym = NULL;
7446 if (expr->expr_type != EXPR_VARIABLE)
7447 code->expr1->base_expr = expr;
7448 return true;
7451 if (st == NULL)
7452 return resolve_typebound_call (code, NULL, NULL);
7454 if (!gfc_resolve_ref (code->expr1))
7455 return false;
7457 /* Get the CLASS declared type. */
7458 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
7460 /* Weed out cases of the ultimate component being a derived type. */
7461 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
7462 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
7464 gfc_free_ref_list (new_ref);
7465 return resolve_typebound_call (code, NULL, NULL);
7468 if (!resolve_typebound_call (code, &name, &overridable))
7470 gfc_free_ref_list (new_ref);
7471 return false;
7473 ts = code->expr1->ts;
7475 if (overridable)
7477 /* Convert the expression to a procedure pointer component call. */
7478 code->expr1->value.function.esym = NULL;
7479 code->expr1->symtree = st;
7481 if (new_ref)
7482 code->expr1->ref = new_ref;
7484 /* '_vptr' points to the vtab, which contains the procedure pointers. */
7485 gfc_add_vptr_component (code->expr1);
7486 gfc_add_component_ref (code->expr1, name);
7488 /* Recover the typespec for the expression. This is really only
7489 necessary for generic procedures, where the additional call
7490 to gfc_add_component_ref seems to throw the collection of the
7491 correct typespec. */
7492 code->expr1->ts = ts;
7494 else if (new_ref)
7495 gfc_free_ref_list (new_ref);
7497 return true;
7501 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
7503 static bool
7504 resolve_ppc_call (gfc_code* c)
7506 gfc_component *comp;
7508 comp = gfc_get_proc_ptr_comp (c->expr1);
7509 gcc_assert (comp != NULL);
7511 c->resolved_sym = c->expr1->symtree->n.sym;
7512 c->expr1->expr_type = EXPR_VARIABLE;
7514 if (!comp->attr.subroutine)
7515 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
7517 if (!gfc_resolve_ref (c->expr1))
7518 return false;
7520 if (!update_ppc_arglist (c->expr1))
7521 return false;
7523 c->ext.actual = c->expr1->value.compcall.actual;
7525 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
7526 !(comp->ts.interface
7527 && comp->ts.interface->formal)))
7528 return false;
7530 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
7531 return false;
7533 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
7535 return true;
7539 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
7541 static bool
7542 resolve_expr_ppc (gfc_expr* e)
7544 gfc_component *comp;
7546 comp = gfc_get_proc_ptr_comp (e);
7547 gcc_assert (comp != NULL);
7549 /* Convert to EXPR_FUNCTION. */
7550 e->expr_type = EXPR_FUNCTION;
7551 e->value.function.isym = NULL;
7552 e->value.function.actual = e->value.compcall.actual;
7553 e->ts = comp->ts;
7554 if (comp->as != NULL)
7556 e->rank = comp->as->rank;
7557 e->corank = comp->as->corank;
7560 if (!comp->attr.function)
7561 gfc_add_function (&comp->attr, comp->name, &e->where);
7563 if (!gfc_resolve_ref (e))
7564 return false;
7566 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
7567 !(comp->ts.interface
7568 && comp->ts.interface->formal)))
7569 return false;
7571 if (!update_ppc_arglist (e))
7572 return false;
7574 if (!check_pure_function(e))
7575 return false;
7577 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
7579 return true;
7583 static bool
7584 gfc_is_expandable_expr (gfc_expr *e)
7586 gfc_constructor *con;
7588 if (e->expr_type == EXPR_ARRAY)
7590 /* Traverse the constructor looking for variables that are flavor
7591 parameter. Parameters must be expanded since they are fully used at
7592 compile time. */
7593 con = gfc_constructor_first (e->value.constructor);
7594 for (; con; con = gfc_constructor_next (con))
7596 if (con->expr->expr_type == EXPR_VARIABLE
7597 && con->expr->symtree
7598 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
7599 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
7600 return true;
7601 if (con->expr->expr_type == EXPR_ARRAY
7602 && gfc_is_expandable_expr (con->expr))
7603 return true;
7607 return false;
7611 /* Sometimes variables in specification expressions of the result
7612 of module procedures in submodules wind up not being the 'real'
7613 dummy. Find this, if possible, in the namespace of the first
7614 formal argument. */
7616 static void
7617 fixup_unique_dummy (gfc_expr *e)
7619 gfc_symtree *st = NULL;
7620 gfc_symbol *s = NULL;
7622 if (e->symtree->n.sym->ns->proc_name
7623 && e->symtree->n.sym->ns->proc_name->formal)
7624 s = e->symtree->n.sym->ns->proc_name->formal->sym;
7626 if (s != NULL)
7627 st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
7629 if (st != NULL
7630 && st->n.sym != NULL
7631 && st->n.sym->attr.dummy)
7632 e->symtree = st;
7635 /* Resolve an expression. That is, make sure that types of operands agree
7636 with their operators, intrinsic operators are converted to function calls
7637 for overloaded types and unresolved function references are resolved. */
7639 bool
7640 gfc_resolve_expr (gfc_expr *e)
7642 bool t;
7643 bool inquiry_save, actual_arg_save, first_actual_arg_save;
7645 if (e == NULL || e->do_not_resolve_again)
7646 return true;
7648 /* inquiry_argument only applies to variables. */
7649 inquiry_save = inquiry_argument;
7650 actual_arg_save = actual_arg;
7651 first_actual_arg_save = first_actual_arg;
7653 if (e->expr_type != EXPR_VARIABLE)
7655 inquiry_argument = false;
7656 actual_arg = false;
7657 first_actual_arg = false;
7659 else if (e->symtree != NULL
7660 && *e->symtree->name == '@'
7661 && e->symtree->n.sym->attr.dummy)
7663 /* Deal with submodule specification expressions that are not
7664 found to be referenced in module.cc(read_cleanup). */
7665 fixup_unique_dummy (e);
7668 switch (e->expr_type)
7670 case EXPR_OP:
7671 t = resolve_operator (e);
7672 break;
7674 case EXPR_FUNCTION:
7675 case EXPR_VARIABLE:
7677 if (check_host_association (e))
7678 t = resolve_function (e);
7679 else
7680 t = resolve_variable (e);
7682 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
7683 && e->ref->type != REF_SUBSTRING)
7684 gfc_resolve_substring_charlen (e);
7686 break;
7688 case EXPR_COMPCALL:
7689 t = resolve_typebound_function (e);
7690 break;
7692 case EXPR_SUBSTRING:
7693 t = gfc_resolve_ref (e);
7694 break;
7696 case EXPR_CONSTANT:
7697 case EXPR_NULL:
7698 t = true;
7699 break;
7701 case EXPR_PPC:
7702 t = resolve_expr_ppc (e);
7703 break;
7705 case EXPR_ARRAY:
7706 t = false;
7707 if (!gfc_resolve_ref (e))
7708 break;
7710 t = gfc_resolve_array_constructor (e);
7711 /* Also try to expand a constructor. */
7712 if (t)
7714 gfc_expression_rank (e);
7715 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
7716 gfc_expand_constructor (e, false);
7719 /* This provides the opportunity for the length of constructors with
7720 character valued function elements to propagate the string length
7721 to the expression. */
7722 if (t && e->ts.type == BT_CHARACTER)
7724 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
7725 here rather then add a duplicate test for it above. */
7726 gfc_expand_constructor (e, false);
7727 t = gfc_resolve_character_array_constructor (e);
7730 break;
7732 case EXPR_STRUCTURE:
7733 t = gfc_resolve_ref (e);
7734 if (!t)
7735 break;
7737 t = resolve_structure_cons (e, 0);
7738 if (!t)
7739 break;
7741 t = gfc_simplify_expr (e, 0);
7742 break;
7744 default:
7745 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
7748 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
7749 fixup_charlen (e);
7751 inquiry_argument = inquiry_save;
7752 actual_arg = actual_arg_save;
7753 first_actual_arg = first_actual_arg_save;
7755 /* For some reason, resolving these expressions a second time mangles
7756 the typespec of the expression itself. */
7757 if (t && e->expr_type == EXPR_VARIABLE
7758 && e->symtree->n.sym->attr.select_rank_temporary
7759 && UNLIMITED_POLY (e->symtree->n.sym))
7760 e->do_not_resolve_again = 1;
7762 return t;
7766 /* Resolve an expression from an iterator. They must be scalar and have
7767 INTEGER or (optionally) REAL type. */
7769 static bool
7770 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
7771 const char *name_msgid)
7773 if (!gfc_resolve_expr (expr))
7774 return false;
7776 if (expr->rank != 0)
7778 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
7779 return false;
7782 if (expr->ts.type != BT_INTEGER)
7784 if (expr->ts.type == BT_REAL)
7786 if (real_ok)
7787 return gfc_notify_std (GFC_STD_F95_DEL,
7788 "%s at %L must be integer",
7789 _(name_msgid), &expr->where);
7790 else
7792 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
7793 &expr->where);
7794 return false;
7797 else
7799 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
7800 return false;
7803 return true;
7807 /* Resolve the expressions in an iterator structure. If REAL_OK is
7808 false allow only INTEGER type iterators, otherwise allow REAL types.
7809 Set own_scope to true for ac-implied-do and data-implied-do as those
7810 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
7812 bool
7813 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
7815 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
7816 return false;
7818 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
7819 _("iterator variable")))
7820 return false;
7822 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
7823 "Start expression in DO loop"))
7824 return false;
7826 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
7827 "End expression in DO loop"))
7828 return false;
7830 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
7831 "Step expression in DO loop"))
7832 return false;
7834 /* Convert start, end, and step to the same type as var. */
7835 if (iter->start->ts.kind != iter->var->ts.kind
7836 || iter->start->ts.type != iter->var->ts.type)
7837 gfc_convert_type (iter->start, &iter->var->ts, 1);
7839 if (iter->end->ts.kind != iter->var->ts.kind
7840 || iter->end->ts.type != iter->var->ts.type)
7841 gfc_convert_type (iter->end, &iter->var->ts, 1);
7843 if (iter->step->ts.kind != iter->var->ts.kind
7844 || iter->step->ts.type != iter->var->ts.type)
7845 gfc_convert_type (iter->step, &iter->var->ts, 1);
7847 if (iter->step->expr_type == EXPR_CONSTANT)
7849 if ((iter->step->ts.type == BT_INTEGER
7850 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
7851 || (iter->step->ts.type == BT_REAL
7852 && mpfr_sgn (iter->step->value.real) == 0))
7854 gfc_error ("Step expression in DO loop at %L cannot be zero",
7855 &iter->step->where);
7856 return false;
7860 if (iter->start->expr_type == EXPR_CONSTANT
7861 && iter->end->expr_type == EXPR_CONSTANT
7862 && iter->step->expr_type == EXPR_CONSTANT)
7864 int sgn, cmp;
7865 if (iter->start->ts.type == BT_INTEGER)
7867 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
7868 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
7870 else
7872 sgn = mpfr_sgn (iter->step->value.real);
7873 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
7875 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
7876 gfc_warning (OPT_Wzerotrip,
7877 "DO loop at %L will be executed zero times",
7878 &iter->step->where);
7881 if (iter->end->expr_type == EXPR_CONSTANT
7882 && iter->end->ts.type == BT_INTEGER
7883 && iter->step->expr_type == EXPR_CONSTANT
7884 && iter->step->ts.type == BT_INTEGER
7885 && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
7886 || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
7888 bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
7889 int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
7891 if (is_step_positive
7892 && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
7893 gfc_warning (OPT_Wundefined_do_loop,
7894 "DO loop at %L is undefined as it overflows",
7895 &iter->step->where);
7896 else if (!is_step_positive
7897 && mpz_cmp (iter->end->value.integer,
7898 gfc_integer_kinds[k].min_int) == 0)
7899 gfc_warning (OPT_Wundefined_do_loop,
7900 "DO loop at %L is undefined as it underflows",
7901 &iter->step->where);
7904 return true;
7908 /* Traversal function for find_forall_index. f == 2 signals that
7909 that variable itself is not to be checked - only the references. */
7911 static bool
7912 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
7914 if (expr->expr_type != EXPR_VARIABLE)
7915 return false;
7917 /* A scalar assignment */
7918 if (!expr->ref || *f == 1)
7920 if (expr->symtree->n.sym == sym)
7921 return true;
7922 else
7923 return false;
7926 if (*f == 2)
7927 *f = 1;
7928 return false;
7932 /* Check whether the FORALL index appears in the expression or not.
7933 Returns true if SYM is found in EXPR. */
7935 bool
7936 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
7938 if (gfc_traverse_expr (expr, sym, forall_index, f))
7939 return true;
7940 else
7941 return false;
7945 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
7946 to be a scalar INTEGER variable. The subscripts and stride are scalar
7947 INTEGERs, and if stride is a constant it must be nonzero.
7948 Furthermore "A subscript or stride in a forall-triplet-spec shall
7949 not contain a reference to any index-name in the
7950 forall-triplet-spec-list in which it appears." (7.5.4.1) */
7952 static void
7953 resolve_forall_iterators (gfc_forall_iterator *it)
7955 gfc_forall_iterator *iter, *iter2;
7957 for (iter = it; iter; iter = iter->next)
7959 if (gfc_resolve_expr (iter->var)
7960 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
7961 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
7962 &iter->var->where);
7964 if (gfc_resolve_expr (iter->start)
7965 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
7966 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
7967 &iter->start->where);
7968 if (iter->var->ts.kind != iter->start->ts.kind)
7969 gfc_convert_type (iter->start, &iter->var->ts, 1);
7971 if (gfc_resolve_expr (iter->end)
7972 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
7973 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
7974 &iter->end->where);
7975 if (iter->var->ts.kind != iter->end->ts.kind)
7976 gfc_convert_type (iter->end, &iter->var->ts, 1);
7978 if (gfc_resolve_expr (iter->stride))
7980 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
7981 gfc_error ("FORALL stride expression at %L must be a scalar %s",
7982 &iter->stride->where, "INTEGER");
7984 if (iter->stride->expr_type == EXPR_CONSTANT
7985 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
7986 gfc_error ("FORALL stride expression at %L cannot be zero",
7987 &iter->stride->where);
7989 if (iter->var->ts.kind != iter->stride->ts.kind)
7990 gfc_convert_type (iter->stride, &iter->var->ts, 1);
7993 for (iter = it; iter; iter = iter->next)
7994 for (iter2 = iter; iter2; iter2 = iter2->next)
7996 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
7997 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
7998 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
7999 gfc_error ("FORALL index %qs may not appear in triplet "
8000 "specification at %L", iter->var->symtree->name,
8001 &iter2->start->where);
8006 /* Given a pointer to a symbol that is a derived type, see if it's
8007 inaccessible, i.e. if it's defined in another module and the components are
8008 PRIVATE. The search is recursive if necessary. Returns zero if no
8009 inaccessible components are found, nonzero otherwise. */
8011 static bool
8012 derived_inaccessible (gfc_symbol *sym)
8014 gfc_component *c;
8016 if (sym->attr.use_assoc && sym->attr.private_comp)
8017 return 1;
8019 for (c = sym->components; c; c = c->next)
8021 /* Prevent an infinite loop through this function. */
8022 if (c->ts.type == BT_DERIVED
8023 && (c->attr.pointer || c->attr.allocatable)
8024 && sym == c->ts.u.derived)
8025 continue;
8027 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
8028 return 1;
8031 return 0;
8035 /* Resolve the argument of a deallocate expression. The expression must be
8036 a pointer or a full array. */
8038 static bool
8039 resolve_deallocate_expr (gfc_expr *e)
8041 symbol_attribute attr;
8042 int allocatable, pointer;
8043 gfc_ref *ref;
8044 gfc_symbol *sym;
8045 gfc_component *c;
8046 bool unlimited;
8048 if (!gfc_resolve_expr (e))
8049 return false;
8051 if (e->expr_type != EXPR_VARIABLE)
8052 goto bad;
8054 sym = e->symtree->n.sym;
8055 unlimited = UNLIMITED_POLY(sym);
8057 if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
8059 allocatable = CLASS_DATA (sym)->attr.allocatable;
8060 pointer = CLASS_DATA (sym)->attr.class_pointer;
8062 else
8064 allocatable = sym->attr.allocatable;
8065 pointer = sym->attr.pointer;
8067 for (ref = e->ref; ref; ref = ref->next)
8069 switch (ref->type)
8071 case REF_ARRAY:
8072 if (ref->u.ar.type != AR_FULL
8073 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
8074 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
8075 allocatable = 0;
8076 break;
8078 case REF_COMPONENT:
8079 c = ref->u.c.component;
8080 if (c->ts.type == BT_CLASS)
8082 allocatable = CLASS_DATA (c)->attr.allocatable;
8083 pointer = CLASS_DATA (c)->attr.class_pointer;
8085 else
8087 allocatable = c->attr.allocatable;
8088 pointer = c->attr.pointer;
8090 break;
8092 case REF_SUBSTRING:
8093 case REF_INQUIRY:
8094 allocatable = 0;
8095 break;
8099 attr = gfc_expr_attr (e);
8101 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
8103 bad:
8104 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
8105 &e->where);
8106 return false;
8109 /* F2008, C644. */
8110 if (gfc_is_coindexed (e))
8112 gfc_error ("Coindexed allocatable object at %L", &e->where);
8113 return false;
8116 if (pointer
8117 && !gfc_check_vardef_context (e, true, true, false,
8118 _("DEALLOCATE object")))
8119 return false;
8120 if (!gfc_check_vardef_context (e, false, true, false,
8121 _("DEALLOCATE object")))
8122 return false;
8124 return true;
8128 /* Returns true if the expression e contains a reference to the symbol sym. */
8129 static bool
8130 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
8132 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
8133 return true;
8135 return false;
8138 bool
8139 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
8141 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
8145 /* Given the expression node e for an allocatable/pointer of derived type to be
8146 allocated, get the expression node to be initialized afterwards (needed for
8147 derived types with default initializers, and derived types with allocatable
8148 components that need nullification.) */
8150 gfc_expr *
8151 gfc_expr_to_initialize (gfc_expr *e)
8153 gfc_expr *result;
8154 gfc_ref *ref;
8155 int i;
8157 result = gfc_copy_expr (e);
8159 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
8160 for (ref = result->ref; ref; ref = ref->next)
8161 if (ref->type == REF_ARRAY && ref->next == NULL)
8163 if (ref->u.ar.dimen == 0
8164 && ref->u.ar.as && ref->u.ar.as->corank)
8165 return result;
8167 ref->u.ar.type = AR_FULL;
8169 for (i = 0; i < ref->u.ar.dimen; i++)
8170 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
8172 break;
8175 gfc_free_shape (&result->shape, result->rank);
8177 /* Recalculate rank, shape, etc. */
8178 gfc_resolve_expr (result);
8179 return result;
8183 /* If the last ref of an expression is an array ref, return a copy of the
8184 expression with that one removed. Otherwise, a copy of the original
8185 expression. This is used for allocate-expressions and pointer assignment
8186 LHS, where there may be an array specification that needs to be stripped
8187 off when using gfc_check_vardef_context. */
8189 static gfc_expr*
8190 remove_last_array_ref (gfc_expr* e)
8192 gfc_expr* e2;
8193 gfc_ref** r;
8195 e2 = gfc_copy_expr (e);
8196 for (r = &e2->ref; *r; r = &(*r)->next)
8197 if ((*r)->type == REF_ARRAY && !(*r)->next)
8199 gfc_free_ref_list (*r);
8200 *r = NULL;
8201 break;
8204 return e2;
8208 /* Used in resolve_allocate_expr to check that a allocation-object and
8209 a source-expr are conformable. This does not catch all possible
8210 cases; in particular a runtime checking is needed. */
8212 static bool
8213 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
8215 gfc_ref *tail;
8216 for (tail = e2->ref; tail && tail->next; tail = tail->next);
8218 /* First compare rank. */
8219 if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
8220 || (!tail && e1->rank != e2->rank))
8222 gfc_error ("Source-expr at %L must be scalar or have the "
8223 "same rank as the allocate-object at %L",
8224 &e1->where, &e2->where);
8225 return false;
8228 if (e1->shape)
8230 int i;
8231 mpz_t s;
8233 mpz_init (s);
8235 for (i = 0; i < e1->rank; i++)
8237 if (tail->u.ar.start[i] == NULL)
8238 break;
8240 if (tail->u.ar.end[i])
8242 mpz_set (s, tail->u.ar.end[i]->value.integer);
8243 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
8244 mpz_add_ui (s, s, 1);
8246 else
8248 mpz_set (s, tail->u.ar.start[i]->value.integer);
8251 if (mpz_cmp (e1->shape[i], s) != 0)
8253 gfc_error ("Source-expr at %L and allocate-object at %L must "
8254 "have the same shape", &e1->where, &e2->where);
8255 mpz_clear (s);
8256 return false;
8260 mpz_clear (s);
8263 return true;
8267 /* Resolve the expression in an ALLOCATE statement, doing the additional
8268 checks to see whether the expression is OK or not. The expression must
8269 have a trailing array reference that gives the size of the array. */
8271 static bool
8272 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
8274 int i, pointer, allocatable, dimension, is_abstract;
8275 int codimension;
8276 bool coindexed;
8277 bool unlimited;
8278 symbol_attribute attr;
8279 gfc_ref *ref, *ref2;
8280 gfc_expr *e2;
8281 gfc_array_ref *ar;
8282 gfc_symbol *sym = NULL;
8283 gfc_alloc *a;
8284 gfc_component *c;
8285 bool t;
8287 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
8288 checking of coarrays. */
8289 for (ref = e->ref; ref; ref = ref->next)
8290 if (ref->next == NULL)
8291 break;
8293 if (ref && ref->type == REF_ARRAY)
8294 ref->u.ar.in_allocate = true;
8296 if (!gfc_resolve_expr (e))
8297 goto failure;
8299 /* Make sure the expression is allocatable or a pointer. If it is
8300 pointer, the next-to-last reference must be a pointer. */
8302 ref2 = NULL;
8303 if (e->symtree)
8304 sym = e->symtree->n.sym;
8306 /* Check whether ultimate component is abstract and CLASS. */
8307 is_abstract = 0;
8309 /* Is the allocate-object unlimited polymorphic? */
8310 unlimited = UNLIMITED_POLY(e);
8312 if (e->expr_type != EXPR_VARIABLE)
8314 allocatable = 0;
8315 attr = gfc_expr_attr (e);
8316 pointer = attr.pointer;
8317 dimension = attr.dimension;
8318 codimension = attr.codimension;
8320 else
8322 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
8324 allocatable = CLASS_DATA (sym)->attr.allocatable;
8325 pointer = CLASS_DATA (sym)->attr.class_pointer;
8326 dimension = CLASS_DATA (sym)->attr.dimension;
8327 codimension = CLASS_DATA (sym)->attr.codimension;
8328 is_abstract = CLASS_DATA (sym)->attr.abstract;
8330 else
8332 allocatable = sym->attr.allocatable;
8333 pointer = sym->attr.pointer;
8334 dimension = sym->attr.dimension;
8335 codimension = sym->attr.codimension;
8338 coindexed = false;
8340 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
8342 switch (ref->type)
8344 case REF_ARRAY:
8345 if (ref->u.ar.codimen > 0)
8347 int n;
8348 for (n = ref->u.ar.dimen;
8349 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
8350 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
8352 coindexed = true;
8353 break;
8357 if (ref->next != NULL)
8358 pointer = 0;
8359 break;
8361 case REF_COMPONENT:
8362 /* F2008, C644. */
8363 if (coindexed)
8365 gfc_error ("Coindexed allocatable object at %L",
8366 &e->where);
8367 goto failure;
8370 c = ref->u.c.component;
8371 if (c->ts.type == BT_CLASS)
8373 allocatable = CLASS_DATA (c)->attr.allocatable;
8374 pointer = CLASS_DATA (c)->attr.class_pointer;
8375 dimension = CLASS_DATA (c)->attr.dimension;
8376 codimension = CLASS_DATA (c)->attr.codimension;
8377 is_abstract = CLASS_DATA (c)->attr.abstract;
8379 else
8381 allocatable = c->attr.allocatable;
8382 pointer = c->attr.pointer;
8383 dimension = c->attr.dimension;
8384 codimension = c->attr.codimension;
8385 is_abstract = c->attr.abstract;
8387 break;
8389 case REF_SUBSTRING:
8390 case REF_INQUIRY:
8391 allocatable = 0;
8392 pointer = 0;
8393 break;
8398 /* Check for F08:C628 (F2018:C932). Each allocate-object shall be a data
8399 pointer or an allocatable variable. */
8400 if (allocatable == 0 && pointer == 0)
8402 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
8403 &e->where);
8404 goto failure;
8407 /* Some checks for the SOURCE tag. */
8408 if (code->expr3)
8410 /* Check F03:C631. */
8411 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
8413 gfc_error ("Type of entity at %L is type incompatible with "
8414 "source-expr at %L", &e->where, &code->expr3->where);
8415 goto failure;
8418 /* Check F03:C632 and restriction following Note 6.18. */
8419 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
8420 goto failure;
8422 /* Check F03:C633. */
8423 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
8425 gfc_error ("The allocate-object at %L and the source-expr at %L "
8426 "shall have the same kind type parameter",
8427 &e->where, &code->expr3->where);
8428 goto failure;
8431 /* Check F2008, C642. */
8432 if (code->expr3->ts.type == BT_DERIVED
8433 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
8434 || (code->expr3->ts.u.derived->from_intmod
8435 == INTMOD_ISO_FORTRAN_ENV
8436 && code->expr3->ts.u.derived->intmod_sym_id
8437 == ISOFORTRAN_LOCK_TYPE)))
8439 gfc_error ("The source-expr at %L shall neither be of type "
8440 "LOCK_TYPE nor have a LOCK_TYPE component if "
8441 "allocate-object at %L is a coarray",
8442 &code->expr3->where, &e->where);
8443 goto failure;
8446 /* Check F2008:C639: "Corresponding kind type parameters of
8447 allocate-object and source-expr shall have the same values." */
8448 if (e->ts.type == BT_CHARACTER
8449 && !e->ts.deferred
8450 && e->ts.u.cl->length
8451 && code->expr3->ts.type == BT_CHARACTER
8452 && !gfc_check_same_strlen (e, code->expr3, "ALLOCATE with "
8453 "SOURCE= or MOLD= specifier"))
8454 goto failure;
8456 /* Check TS18508, C702/C703. */
8457 if (code->expr3->ts.type == BT_DERIVED
8458 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
8459 || (code->expr3->ts.u.derived->from_intmod
8460 == INTMOD_ISO_FORTRAN_ENV
8461 && code->expr3->ts.u.derived->intmod_sym_id
8462 == ISOFORTRAN_EVENT_TYPE)))
8464 gfc_error ("The source-expr at %L shall neither be of type "
8465 "EVENT_TYPE nor have a EVENT_TYPE component if "
8466 "allocate-object at %L is a coarray",
8467 &code->expr3->where, &e->where);
8468 goto failure;
8472 /* Check F08:C629. */
8473 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
8474 && !code->expr3)
8476 gcc_assert (e->ts.type == BT_CLASS);
8477 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
8478 "type-spec or source-expr", sym->name, &e->where);
8479 goto failure;
8482 /* Check F08:C632. */
8483 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
8484 && !UNLIMITED_POLY (e))
8486 int cmp;
8488 if (!e->ts.u.cl->length)
8489 goto failure;
8491 cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
8492 code->ext.alloc.ts.u.cl->length);
8493 if (cmp == 1 || cmp == -1 || cmp == -3)
8495 gfc_error ("Allocating %s at %L with type-spec requires the same "
8496 "character-length parameter as in the declaration",
8497 sym->name, &e->where);
8498 goto failure;
8502 /* In the variable definition context checks, gfc_expr_attr is used
8503 on the expression. This is fooled by the array specification
8504 present in e, thus we have to eliminate that one temporarily. */
8505 e2 = remove_last_array_ref (e);
8506 t = true;
8507 if (t && pointer)
8508 t = gfc_check_vardef_context (e2, true, true, false,
8509 _("ALLOCATE object"));
8510 if (t)
8511 t = gfc_check_vardef_context (e2, false, true, false,
8512 _("ALLOCATE object"));
8513 gfc_free_expr (e2);
8514 if (!t)
8515 goto failure;
8517 code->ext.alloc.expr3_not_explicit = 0;
8518 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
8519 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
8521 /* For class arrays, the initialization with SOURCE is done
8522 using _copy and trans_call. It is convenient to exploit that
8523 when the allocated type is different from the declared type but
8524 no SOURCE exists by setting expr3. */
8525 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
8526 code->ext.alloc.expr3_not_explicit = 1;
8528 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
8529 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
8530 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
8532 /* We have to zero initialize the integer variable. */
8533 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
8534 code->ext.alloc.expr3_not_explicit = 1;
8537 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
8539 /* Make sure the vtab symbol is present when
8540 the module variables are generated. */
8541 gfc_typespec ts = e->ts;
8542 if (code->expr3)
8543 ts = code->expr3->ts;
8544 else if (code->ext.alloc.ts.type == BT_DERIVED)
8545 ts = code->ext.alloc.ts;
8547 /* Finding the vtab also publishes the type's symbol. Therefore this
8548 statement is necessary. */
8549 gfc_find_derived_vtab (ts.u.derived);
8551 else if (unlimited && !UNLIMITED_POLY (code->expr3))
8553 /* Again, make sure the vtab symbol is present when
8554 the module variables are generated. */
8555 gfc_typespec *ts = NULL;
8556 if (code->expr3)
8557 ts = &code->expr3->ts;
8558 else
8559 ts = &code->ext.alloc.ts;
8561 gcc_assert (ts);
8563 /* Finding the vtab also publishes the type's symbol. Therefore this
8564 statement is necessary. */
8565 gfc_find_vtab (ts);
8568 if (dimension == 0 && codimension == 0)
8569 goto success;
8571 /* Make sure the last reference node is an array specification. */
8573 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
8574 || (dimension && ref2->u.ar.dimen == 0))
8576 /* F08:C633. */
8577 if (code->expr3)
8579 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
8580 "in ALLOCATE statement at %L", &e->where))
8581 goto failure;
8582 if (code->expr3->rank != 0)
8583 *array_alloc_wo_spec = true;
8584 else
8586 gfc_error ("Array specification or array-valued SOURCE= "
8587 "expression required in ALLOCATE statement at %L",
8588 &e->where);
8589 goto failure;
8592 else
8594 gfc_error ("Array specification required in ALLOCATE statement "
8595 "at %L", &e->where);
8596 goto failure;
8600 /* Make sure that the array section reference makes sense in the
8601 context of an ALLOCATE specification. */
8603 ar = &ref2->u.ar;
8605 if (codimension)
8606 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
8608 switch (ar->dimen_type[i])
8610 case DIMEN_THIS_IMAGE:
8611 gfc_error ("Coarray specification required in ALLOCATE statement "
8612 "at %L", &e->where);
8613 goto failure;
8615 case DIMEN_RANGE:
8616 /* F2018:R937:
8617 * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
8619 if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
8621 gfc_error ("Bad coarray specification in ALLOCATE statement "
8622 "at %L", &e->where);
8623 goto failure;
8625 else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
8627 gfc_error ("Upper cobound is less than lower cobound at %L",
8628 &ar->start[i]->where);
8629 goto failure;
8631 break;
8633 case DIMEN_ELEMENT:
8634 if (ar->start[i]->expr_type == EXPR_CONSTANT)
8636 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
8637 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
8639 gfc_error ("Upper cobound is less than lower cobound "
8640 "of 1 at %L", &ar->start[i]->where);
8641 goto failure;
8644 break;
8646 case DIMEN_STAR:
8647 break;
8649 default:
8650 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8651 &e->where);
8652 goto failure;
8656 for (i = 0; i < ar->dimen; i++)
8658 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
8659 goto check_symbols;
8661 switch (ar->dimen_type[i])
8663 case DIMEN_ELEMENT:
8664 break;
8666 case DIMEN_RANGE:
8667 if (ar->start[i] != NULL
8668 && ar->end[i] != NULL
8669 && ar->stride[i] == NULL)
8670 break;
8672 /* Fall through. */
8674 case DIMEN_UNKNOWN:
8675 case DIMEN_VECTOR:
8676 case DIMEN_STAR:
8677 case DIMEN_THIS_IMAGE:
8678 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8679 &e->where);
8680 goto failure;
8683 check_symbols:
8684 for (a = code->ext.alloc.list; a; a = a->next)
8686 sym = a->expr->symtree->n.sym;
8688 /* TODO - check derived type components. */
8689 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
8690 continue;
8692 if ((ar->start[i] != NULL
8693 && gfc_find_sym_in_expr (sym, ar->start[i]))
8694 || (ar->end[i] != NULL
8695 && gfc_find_sym_in_expr (sym, ar->end[i])))
8697 gfc_error ("%qs must not appear in the array specification at "
8698 "%L in the same ALLOCATE statement where it is "
8699 "itself allocated", sym->name, &ar->where);
8700 goto failure;
8705 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
8707 if (ar->dimen_type[i] == DIMEN_ELEMENT
8708 || ar->dimen_type[i] == DIMEN_RANGE)
8710 if (i == (ar->dimen + ar->codimen - 1))
8712 gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
8713 "statement at %L", &e->where);
8714 goto failure;
8716 continue;
8719 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
8720 && ar->stride[i] == NULL)
8721 break;
8723 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
8724 &e->where);
8725 goto failure;
8728 success:
8729 return true;
8731 failure:
8732 return false;
8736 static void
8737 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
8739 gfc_expr *stat, *errmsg, *pe, *qe;
8740 gfc_alloc *a, *p, *q;
8742 stat = code->expr1;
8743 errmsg = code->expr2;
8745 /* Check the stat variable. */
8746 if (stat)
8748 if (!gfc_check_vardef_context (stat, false, false, false,
8749 _("STAT variable")))
8750 goto done_stat;
8752 if (stat->ts.type != BT_INTEGER
8753 || stat->rank > 0)
8754 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
8755 "variable", &stat->where);
8757 if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
8758 goto done_stat;
8760 /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
8761 * within the ALLOCATE or DEALLOCATE statement in which it appears ...
8763 for (p = code->ext.alloc.list; p; p = p->next)
8764 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
8766 gfc_ref *ref1, *ref2;
8767 bool found = true;
8769 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
8770 ref1 = ref1->next, ref2 = ref2->next)
8772 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8773 continue;
8774 if (ref1->u.c.component->name != ref2->u.c.component->name)
8776 found = false;
8777 break;
8781 if (found)
8783 gfc_error ("Stat-variable at %L shall not be %sd within "
8784 "the same %s statement", &stat->where, fcn, fcn);
8785 break;
8790 done_stat:
8792 /* Check the errmsg variable. */
8793 if (errmsg)
8795 if (!stat)
8796 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
8797 &errmsg->where);
8799 if (!gfc_check_vardef_context (errmsg, false, false, false,
8800 _("ERRMSG variable")))
8801 goto done_errmsg;
8803 /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
8804 F18:R930 errmsg-variable is scalar-default-char-variable
8805 F18:R906 default-char-variable is variable
8806 F18:C906 default-char-variable shall be default character. */
8807 if (errmsg->ts.type != BT_CHARACTER
8808 || errmsg->rank > 0
8809 || errmsg->ts.kind != gfc_default_character_kind)
8810 gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
8811 "variable", &errmsg->where);
8813 if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
8814 goto done_errmsg;
8816 /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
8817 * within the ALLOCATE or DEALLOCATE statement in which it appears ...
8819 for (p = code->ext.alloc.list; p; p = p->next)
8820 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
8822 gfc_ref *ref1, *ref2;
8823 bool found = true;
8825 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
8826 ref1 = ref1->next, ref2 = ref2->next)
8828 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8829 continue;
8830 if (ref1->u.c.component->name != ref2->u.c.component->name)
8832 found = false;
8833 break;
8837 if (found)
8839 gfc_error ("Errmsg-variable at %L shall not be %sd within "
8840 "the same %s statement", &errmsg->where, fcn, fcn);
8841 break;
8846 done_errmsg:
8848 /* Check that an allocate-object appears only once in the statement. */
8850 for (p = code->ext.alloc.list; p; p = p->next)
8852 pe = p->expr;
8853 for (q = p->next; q; q = q->next)
8855 qe = q->expr;
8856 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
8858 /* This is a potential collision. */
8859 gfc_ref *pr = pe->ref;
8860 gfc_ref *qr = qe->ref;
8862 /* Follow the references until
8863 a) They start to differ, in which case there is no error;
8864 you can deallocate a%b and a%c in a single statement
8865 b) Both of them stop, which is an error
8866 c) One of them stops, which is also an error. */
8867 while (1)
8869 if (pr == NULL && qr == NULL)
8871 gfc_error ("Allocate-object at %L also appears at %L",
8872 &pe->where, &qe->where);
8873 break;
8875 else if (pr != NULL && qr == NULL)
8877 gfc_error ("Allocate-object at %L is subobject of"
8878 " object at %L", &pe->where, &qe->where);
8879 break;
8881 else if (pr == NULL && qr != NULL)
8883 gfc_error ("Allocate-object at %L is subobject of"
8884 " object at %L", &qe->where, &pe->where);
8885 break;
8887 /* Here, pr != NULL && qr != NULL */
8888 gcc_assert(pr->type == qr->type);
8889 if (pr->type == REF_ARRAY)
8891 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
8892 which are legal. */
8893 gcc_assert (qr->type == REF_ARRAY);
8895 if (pr->next && qr->next)
8897 int i;
8898 gfc_array_ref *par = &(pr->u.ar);
8899 gfc_array_ref *qar = &(qr->u.ar);
8901 for (i=0; i<par->dimen; i++)
8903 if ((par->start[i] != NULL
8904 || qar->start[i] != NULL)
8905 && gfc_dep_compare_expr (par->start[i],
8906 qar->start[i]) != 0)
8907 goto break_label;
8911 else
8913 if (pr->u.c.component->name != qr->u.c.component->name)
8914 break;
8917 pr = pr->next;
8918 qr = qr->next;
8920 break_label:
8926 if (strcmp (fcn, "ALLOCATE") == 0)
8928 bool arr_alloc_wo_spec = false;
8930 /* Resolving the expr3 in the loop over all objects to allocate would
8931 execute loop invariant code for each loop item. Therefore do it just
8932 once here. */
8933 if (code->expr3 && code->expr3->mold
8934 && code->expr3->ts.type == BT_DERIVED)
8936 /* Default initialization via MOLD (non-polymorphic). */
8937 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
8938 if (rhs != NULL)
8940 gfc_resolve_expr (rhs);
8941 gfc_free_expr (code->expr3);
8942 code->expr3 = rhs;
8945 for (a = code->ext.alloc.list; a; a = a->next)
8946 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
8948 if (arr_alloc_wo_spec && code->expr3)
8950 /* Mark the allocate to have to take the array specification
8951 from the expr3. */
8952 code->ext.alloc.arr_spec_from_expr3 = 1;
8955 else
8957 for (a = code->ext.alloc.list; a; a = a->next)
8958 resolve_deallocate_expr (a->expr);
8963 /************ SELECT CASE resolution subroutines ************/
8965 /* Callback function for our mergesort variant. Determines interval
8966 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
8967 op1 > op2. Assumes we're not dealing with the default case.
8968 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
8969 There are nine situations to check. */
8971 static int
8972 compare_cases (const gfc_case *op1, const gfc_case *op2)
8974 int retval;
8976 if (op1->low == NULL) /* op1 = (:L) */
8978 /* op2 = (:N), so overlap. */
8979 retval = 0;
8980 /* op2 = (M:) or (M:N), L < M */
8981 if (op2->low != NULL
8982 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8983 retval = -1;
8985 else if (op1->high == NULL) /* op1 = (K:) */
8987 /* op2 = (M:), so overlap. */
8988 retval = 0;
8989 /* op2 = (:N) or (M:N), K > N */
8990 if (op2->high != NULL
8991 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8992 retval = 1;
8994 else /* op1 = (K:L) */
8996 if (op2->low == NULL) /* op2 = (:N), K > N */
8997 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8998 ? 1 : 0;
8999 else if (op2->high == NULL) /* op2 = (M:), L < M */
9000 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9001 ? -1 : 0;
9002 else /* op2 = (M:N) */
9004 retval = 0;
9005 /* L < M */
9006 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9007 retval = -1;
9008 /* K > N */
9009 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9010 retval = 1;
9014 return retval;
9018 /* Merge-sort a double linked case list, detecting overlap in the
9019 process. LIST is the head of the double linked case list before it
9020 is sorted. Returns the head of the sorted list if we don't see any
9021 overlap, or NULL otherwise. */
9023 static gfc_case *
9024 check_case_overlap (gfc_case *list)
9026 gfc_case *p, *q, *e, *tail;
9027 int insize, nmerges, psize, qsize, cmp, overlap_seen;
9029 /* If the passed list was empty, return immediately. */
9030 if (!list)
9031 return NULL;
9033 overlap_seen = 0;
9034 insize = 1;
9036 /* Loop unconditionally. The only exit from this loop is a return
9037 statement, when we've finished sorting the case list. */
9038 for (;;)
9040 p = list;
9041 list = NULL;
9042 tail = NULL;
9044 /* Count the number of merges we do in this pass. */
9045 nmerges = 0;
9047 /* Loop while there exists a merge to be done. */
9048 while (p)
9050 int i;
9052 /* Count this merge. */
9053 nmerges++;
9055 /* Cut the list in two pieces by stepping INSIZE places
9056 forward in the list, starting from P. */
9057 psize = 0;
9058 q = p;
9059 for (i = 0; i < insize; i++)
9061 psize++;
9062 q = q->right;
9063 if (!q)
9064 break;
9066 qsize = insize;
9068 /* Now we have two lists. Merge them! */
9069 while (psize > 0 || (qsize > 0 && q != NULL))
9071 /* See from which the next case to merge comes from. */
9072 if (psize == 0)
9074 /* P is empty so the next case must come from Q. */
9075 e = q;
9076 q = q->right;
9077 qsize--;
9079 else if (qsize == 0 || q == NULL)
9081 /* Q is empty. */
9082 e = p;
9083 p = p->right;
9084 psize--;
9086 else
9088 cmp = compare_cases (p, q);
9089 if (cmp < 0)
9091 /* The whole case range for P is less than the
9092 one for Q. */
9093 e = p;
9094 p = p->right;
9095 psize--;
9097 else if (cmp > 0)
9099 /* The whole case range for Q is greater than
9100 the case range for P. */
9101 e = q;
9102 q = q->right;
9103 qsize--;
9105 else
9107 /* The cases overlap, or they are the same
9108 element in the list. Either way, we must
9109 issue an error and get the next case from P. */
9110 /* FIXME: Sort P and Q by line number. */
9111 gfc_error ("CASE label at %L overlaps with CASE "
9112 "label at %L", &p->where, &q->where);
9113 overlap_seen = 1;
9114 e = p;
9115 p = p->right;
9116 psize--;
9120 /* Add the next element to the merged list. */
9121 if (tail)
9122 tail->right = e;
9123 else
9124 list = e;
9125 e->left = tail;
9126 tail = e;
9129 /* P has now stepped INSIZE places along, and so has Q. So
9130 they're the same. */
9131 p = q;
9133 tail->right = NULL;
9135 /* If we have done only one merge or none at all, we've
9136 finished sorting the cases. */
9137 if (nmerges <= 1)
9139 if (!overlap_seen)
9140 return list;
9141 else
9142 return NULL;
9145 /* Otherwise repeat, merging lists twice the size. */
9146 insize *= 2;
9151 /* Check to see if an expression is suitable for use in a CASE statement.
9152 Makes sure that all case expressions are scalar constants of the same
9153 type. Return false if anything is wrong. */
9155 static bool
9156 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
9158 if (e == NULL) return true;
9160 if (e->ts.type != case_expr->ts.type)
9162 gfc_error ("Expression in CASE statement at %L must be of type %s",
9163 &e->where, gfc_basic_typename (case_expr->ts.type));
9164 return false;
9167 /* C805 (R808) For a given case-construct, each case-value shall be of
9168 the same type as case-expr. For character type, length differences
9169 are allowed, but the kind type parameters shall be the same. */
9171 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
9173 gfc_error ("Expression in CASE statement at %L must be of kind %d",
9174 &e->where, case_expr->ts.kind);
9175 return false;
9178 /* Convert the case value kind to that of case expression kind,
9179 if needed */
9181 if (e->ts.kind != case_expr->ts.kind)
9182 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
9184 if (e->rank != 0)
9186 gfc_error ("Expression in CASE statement at %L must be scalar",
9187 &e->where);
9188 return false;
9191 return true;
9195 /* Given a completely parsed select statement, we:
9197 - Validate all expressions and code within the SELECT.
9198 - Make sure that the selection expression is not of the wrong type.
9199 - Make sure that no case ranges overlap.
9200 - Eliminate unreachable cases and unreachable code resulting from
9201 removing case labels.
9203 The standard does allow unreachable cases, e.g. CASE (5:3). But
9204 they are a hassle for code generation, and to prevent that, we just
9205 cut them out here. This is not necessary for overlapping cases
9206 because they are illegal and we never even try to generate code.
9208 We have the additional caveat that a SELECT construct could have
9209 been a computed GOTO in the source code. Fortunately we can fairly
9210 easily work around that here: The case_expr for a "real" SELECT CASE
9211 is in code->expr1, but for a computed GOTO it is in code->expr2. All
9212 we have to do is make sure that the case_expr is a scalar integer
9213 expression. */
9215 static void
9216 resolve_select (gfc_code *code, bool select_type)
9218 gfc_code *body;
9219 gfc_expr *case_expr;
9220 gfc_case *cp, *default_case, *tail, *head;
9221 int seen_unreachable;
9222 int seen_logical;
9223 int ncases;
9224 bt type;
9225 bool t;
9227 if (code->expr1 == NULL)
9229 /* This was actually a computed GOTO statement. */
9230 case_expr = code->expr2;
9231 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
9232 gfc_error ("Selection expression in computed GOTO statement "
9233 "at %L must be a scalar integer expression",
9234 &case_expr->where);
9236 /* Further checking is not necessary because this SELECT was built
9237 by the compiler, so it should always be OK. Just move the
9238 case_expr from expr2 to expr so that we can handle computed
9239 GOTOs as normal SELECTs from here on. */
9240 code->expr1 = code->expr2;
9241 code->expr2 = NULL;
9242 return;
9245 case_expr = code->expr1;
9246 type = case_expr->ts.type;
9248 /* F08:C830. */
9249 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER
9250 && (!flag_unsigned || (flag_unsigned && type != BT_UNSIGNED)))
9253 gfc_error ("Argument of SELECT statement at %L cannot be %s",
9254 &case_expr->where, gfc_typename (case_expr));
9256 /* Punt. Going on here just produce more garbage error messages. */
9257 return;
9260 /* F08:R842. */
9261 if (!select_type && case_expr->rank != 0)
9263 gfc_error ("Argument of SELECT statement at %L must be a scalar "
9264 "expression", &case_expr->where);
9266 /* Punt. */
9267 return;
9270 /* Raise a warning if an INTEGER case value exceeds the range of
9271 the case-expr. Later, all expressions will be promoted to the
9272 largest kind of all case-labels. */
9274 if (type == BT_INTEGER)
9275 for (body = code->block; body; body = body->block)
9276 for (cp = body->ext.block.case_list; cp; cp = cp->next)
9278 if (cp->low
9279 && gfc_check_integer_range (cp->low->value.integer,
9280 case_expr->ts.kind) != ARITH_OK)
9281 gfc_warning (0, "Expression in CASE statement at %L is "
9282 "not in the range of %s", &cp->low->where,
9283 gfc_typename (case_expr));
9285 if (cp->high
9286 && cp->low != cp->high
9287 && gfc_check_integer_range (cp->high->value.integer,
9288 case_expr->ts.kind) != ARITH_OK)
9289 gfc_warning (0, "Expression in CASE statement at %L is "
9290 "not in the range of %s", &cp->high->where,
9291 gfc_typename (case_expr));
9294 /* PR 19168 has a long discussion concerning a mismatch of the kinds
9295 of the SELECT CASE expression and its CASE values. Walk the lists
9296 of case values, and if we find a mismatch, promote case_expr to
9297 the appropriate kind. */
9299 if (type == BT_LOGICAL || type == BT_INTEGER)
9301 for (body = code->block; body; body = body->block)
9303 /* Walk the case label list. */
9304 for (cp = body->ext.block.case_list; cp; cp = cp->next)
9306 /* Intercept the DEFAULT case. It does not have a kind. */
9307 if (cp->low == NULL && cp->high == NULL)
9308 continue;
9310 /* Unreachable case ranges are discarded, so ignore. */
9311 if (cp->low != NULL && cp->high != NULL
9312 && cp->low != cp->high
9313 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
9314 continue;
9316 if (cp->low != NULL
9317 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
9318 gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
9320 if (cp->high != NULL
9321 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
9322 gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
9327 /* Assume there is no DEFAULT case. */
9328 default_case = NULL;
9329 head = tail = NULL;
9330 ncases = 0;
9331 seen_logical = 0;
9333 for (body = code->block; body; body = body->block)
9335 /* Assume the CASE list is OK, and all CASE labels can be matched. */
9336 t = true;
9337 seen_unreachable = 0;
9339 /* Walk the case label list, making sure that all case labels
9340 are legal. */
9341 for (cp = body->ext.block.case_list; cp; cp = cp->next)
9343 /* Count the number of cases in the whole construct. */
9344 ncases++;
9346 /* Intercept the DEFAULT case. */
9347 if (cp->low == NULL && cp->high == NULL)
9349 if (default_case != NULL)
9351 gfc_error ("The DEFAULT CASE at %L cannot be followed "
9352 "by a second DEFAULT CASE at %L",
9353 &default_case->where, &cp->where);
9354 t = false;
9355 break;
9357 else
9359 default_case = cp;
9360 continue;
9364 /* Deal with single value cases and case ranges. Errors are
9365 issued from the validation function. */
9366 if (!validate_case_label_expr (cp->low, case_expr)
9367 || !validate_case_label_expr (cp->high, case_expr))
9369 t = false;
9370 break;
9373 if (type == BT_LOGICAL
9374 && ((cp->low == NULL || cp->high == NULL)
9375 || cp->low != cp->high))
9377 gfc_error ("Logical range in CASE statement at %L is not "
9378 "allowed",
9379 cp->low ? &cp->low->where : &cp->high->where);
9380 t = false;
9381 break;
9384 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
9386 int value;
9387 value = cp->low->value.logical == 0 ? 2 : 1;
9388 if (value & seen_logical)
9390 gfc_error ("Constant logical value in CASE statement "
9391 "is repeated at %L",
9392 &cp->low->where);
9393 t = false;
9394 break;
9396 seen_logical |= value;
9399 if (cp->low != NULL && cp->high != NULL
9400 && cp->low != cp->high
9401 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
9403 if (warn_surprising)
9404 gfc_warning (OPT_Wsurprising,
9405 "Range specification at %L can never be matched",
9406 &cp->where);
9408 cp->unreachable = 1;
9409 seen_unreachable = 1;
9411 else
9413 /* If the case range can be matched, it can also overlap with
9414 other cases. To make sure it does not, we put it in a
9415 double linked list here. We sort that with a merge sort
9416 later on to detect any overlapping cases. */
9417 if (!head)
9419 head = tail = cp;
9420 head->right = head->left = NULL;
9422 else
9424 tail->right = cp;
9425 tail->right->left = tail;
9426 tail = tail->right;
9427 tail->right = NULL;
9432 /* It there was a failure in the previous case label, give up
9433 for this case label list. Continue with the next block. */
9434 if (!t)
9435 continue;
9437 /* See if any case labels that are unreachable have been seen.
9438 If so, we eliminate them. This is a bit of a kludge because
9439 the case lists for a single case statement (label) is a
9440 single forward linked lists. */
9441 if (seen_unreachable)
9443 /* Advance until the first case in the list is reachable. */
9444 while (body->ext.block.case_list != NULL
9445 && body->ext.block.case_list->unreachable)
9447 gfc_case *n = body->ext.block.case_list;
9448 body->ext.block.case_list = body->ext.block.case_list->next;
9449 n->next = NULL;
9450 gfc_free_case_list (n);
9453 /* Strip all other unreachable cases. */
9454 if (body->ext.block.case_list)
9456 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
9458 if (cp->next->unreachable)
9460 gfc_case *n = cp->next;
9461 cp->next = cp->next->next;
9462 n->next = NULL;
9463 gfc_free_case_list (n);
9470 /* See if there were overlapping cases. If the check returns NULL,
9471 there was overlap. In that case we don't do anything. If head
9472 is non-NULL, we prepend the DEFAULT case. The sorted list can
9473 then used during code generation for SELECT CASE constructs with
9474 a case expression of a CHARACTER type. */
9475 if (head)
9477 head = check_case_overlap (head);
9479 /* Prepend the default_case if it is there. */
9480 if (head != NULL && default_case)
9482 default_case->left = NULL;
9483 default_case->right = head;
9484 head->left = default_case;
9488 /* Eliminate dead blocks that may be the result if we've seen
9489 unreachable case labels for a block. */
9490 for (body = code; body && body->block; body = body->block)
9492 if (body->block->ext.block.case_list == NULL)
9494 /* Cut the unreachable block from the code chain. */
9495 gfc_code *c = body->block;
9496 body->block = c->block;
9498 /* Kill the dead block, but not the blocks below it. */
9499 c->block = NULL;
9500 gfc_free_statements (c);
9504 /* More than two cases is legal but insane for logical selects.
9505 Issue a warning for it. */
9506 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
9507 gfc_warning (OPT_Wsurprising,
9508 "Logical SELECT CASE block at %L has more that two cases",
9509 &code->loc);
9513 /* Check if a derived type is extensible. */
9515 bool
9516 gfc_type_is_extensible (gfc_symbol *sym)
9518 return !(sym->attr.is_bind_c || sym->attr.sequence
9519 || (sym->attr.is_class
9520 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
9524 static void
9525 resolve_types (gfc_namespace *ns);
9527 /* Resolve an associate-name: Resolve target and ensure the type-spec is
9528 correct as well as possibly the array-spec. */
9530 static void
9531 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
9533 gfc_expr* target;
9534 bool parentheses = false;
9536 gcc_assert (sym->assoc);
9537 gcc_assert (sym->attr.flavor == FL_VARIABLE);
9539 /* If this is for SELECT TYPE, the target may not yet be set. In that
9540 case, return. Resolution will be called later manually again when
9541 this is done. */
9542 target = sym->assoc->target;
9543 if (!target)
9544 return;
9545 gcc_assert (!sym->assoc->dangling);
9547 if (target->expr_type == EXPR_OP
9548 && target->value.op.op == INTRINSIC_PARENTHESES
9549 && target->value.op.op1->expr_type == EXPR_VARIABLE)
9551 sym->assoc->target = gfc_copy_expr (target->value.op.op1);
9552 gfc_free_expr (target);
9553 target = sym->assoc->target;
9554 parentheses = true;
9557 if (resolve_target && !gfc_resolve_expr (target))
9558 return;
9560 /* For variable targets, we get some attributes from the target. */
9561 if (target->expr_type == EXPR_VARIABLE)
9563 gfc_symbol *tsym, *dsym;
9565 gcc_assert (target->symtree);
9566 tsym = target->symtree->n.sym;
9568 if (gfc_expr_attr (target).proc_pointer)
9570 gfc_error ("Associating entity %qs at %L is a procedure pointer",
9571 tsym->name, &target->where);
9572 return;
9575 if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
9576 && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
9577 && dsym->attr.flavor == FL_DERIVED)
9579 gfc_error ("Derived type %qs cannot be used as a variable at %L",
9580 tsym->name, &target->where);
9581 return;
9584 if (tsym->attr.flavor == FL_PROCEDURE)
9586 bool is_error = true;
9587 if (tsym->attr.function && tsym->result == tsym)
9588 for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
9589 if (tsym == ns->proc_name)
9591 is_error = false;
9592 break;
9594 if (is_error)
9596 gfc_error ("Associating entity %qs at %L is a procedure name",
9597 tsym->name, &target->where);
9598 return;
9602 sym->attr.asynchronous = tsym->attr.asynchronous;
9603 sym->attr.volatile_ = tsym->attr.volatile_;
9605 sym->attr.target = tsym->attr.target
9606 || gfc_expr_attr (target).pointer;
9607 if (is_subref_array (target))
9608 sym->attr.subref_array_pointer = 1;
9610 else if (target->ts.type == BT_PROCEDURE)
9612 gfc_error ("Associating selector-expression at %L yields a procedure",
9613 &target->where);
9614 return;
9617 if (sym->assoc->inferred_type || IS_INFERRED_TYPE (target))
9619 /* By now, the type of the target has been fixed up. */
9620 symbol_attribute attr;
9622 if (sym->ts.type == BT_DERIVED
9623 && target->ts.type == BT_CLASS
9624 && !UNLIMITED_POLY (target))
9626 /* Inferred to be derived type but the target has type class. */
9627 sym->ts = CLASS_DATA (target)->ts;
9628 if (!sym->as)
9629 sym->as = gfc_copy_array_spec (CLASS_DATA (target)->as);
9630 attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
9631 sym->attr.dimension = target->rank ? 1 : 0;
9632 gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
9633 target->corank);
9634 sym->as = NULL;
9636 else if (target->ts.type == BT_DERIVED
9637 && target->symtree && target->symtree->n.sym
9638 && target->symtree->n.sym->ts.type == BT_CLASS
9639 && IS_INFERRED_TYPE (target)
9640 && target->ref && target->ref->next
9641 && target->ref->next->type == REF_ARRAY
9642 && !target->ref->next->next)
9644 /* A inferred type selector whose symbol has been determined to be
9645 a class array but which only has an array reference. Change the
9646 associate name and the selector to class type. */
9647 sym->ts = target->ts;
9648 attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
9649 sym->attr.dimension = target->rank ? 1 : 0;
9650 gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
9651 target->corank);
9652 sym->as = NULL;
9653 target->ts = sym->ts;
9655 else if ((target->ts.type == BT_DERIVED)
9656 || (sym->ts.type == BT_CLASS && target->ts.type == BT_CLASS
9657 && CLASS_DATA (target)->as && !CLASS_DATA (sym)->as))
9658 /* Confirmed to be either a derived type or misidentified to be a
9659 scalar class object, when the selector is a class array. */
9660 sym->ts = target->ts;
9664 if (target->expr_type == EXPR_NULL)
9666 gfc_error ("Selector at %L cannot be NULL()", &target->where);
9667 return;
9669 else if (target->ts.type == BT_UNKNOWN)
9671 gfc_error ("Selector at %L has no type", &target->where);
9672 return;
9675 /* Get type if this was not already set. Note that it can be
9676 some other type than the target in case this is a SELECT TYPE
9677 selector! So we must not update when the type is already there. */
9678 if (sym->ts.type == BT_UNKNOWN)
9679 sym->ts = target->ts;
9681 gcc_assert (sym->ts.type != BT_UNKNOWN);
9683 /* See if this is a valid association-to-variable. */
9684 sym->assoc->variable = ((target->expr_type == EXPR_VARIABLE
9685 && !parentheses
9686 && !gfc_has_vector_subscript (target))
9687 || gfc_is_ptr_fcn (target));
9689 /* Finally resolve if this is an array or not. */
9690 if (target->expr_type == EXPR_FUNCTION
9691 && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
9693 gfc_expression_rank (target);
9694 if (target->ts.type == BT_DERIVED
9695 && !sym->as
9696 && target->symtree->n.sym->as)
9698 sym->as = gfc_copy_array_spec (target->symtree->n.sym->as);
9699 sym->attr.dimension = 1;
9701 else if (target->ts.type == BT_CLASS
9702 && CLASS_DATA (target)->as)
9704 target->rank = CLASS_DATA (target)->as->rank;
9705 target->corank = CLASS_DATA (target)->as->corank;
9706 if (!(sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
9708 sym->ts = target->ts;
9709 sym->attr.dimension = 0;
9715 if (sym->attr.dimension && target->rank == 0)
9717 /* primary.cc makes the assumption that a reference to an associate
9718 name followed by a left parenthesis is an array reference. */
9719 if (sym->assoc->inferred_type && sym->ts.type != BT_CLASS)
9721 gfc_expression_rank (sym->assoc->target);
9722 sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
9723 if (!sym->attr.dimension && sym->as)
9724 sym->as = NULL;
9727 if (sym->attr.dimension && target->rank == 0)
9729 if (sym->ts.type != BT_CHARACTER)
9730 gfc_error ("Associate-name %qs at %L is used as array",
9731 sym->name, &sym->declared_at);
9732 sym->attr.dimension = 0;
9733 return;
9737 /* We cannot deal with class selectors that need temporaries. */
9738 if (target->ts.type == BT_CLASS
9739 && gfc_ref_needs_temporary_p (target->ref))
9741 gfc_error ("CLASS selector at %L needs a temporary which is not "
9742 "yet implemented", &target->where);
9743 return;
9746 if (target->ts.type == BT_CLASS)
9747 gfc_fix_class_refs (target);
9749 if ((target->rank != 0 || target->corank != 0)
9750 && !sym->attr.select_rank_temporary)
9752 gfc_array_spec *as;
9753 /* The rank may be incorrectly guessed at parsing, therefore make sure
9754 it is corrected now. */
9755 if (sym->ts.type != BT_CLASS
9756 && (!sym->as || sym->as->corank != target->corank))
9758 if (!sym->as)
9759 sym->as = gfc_get_array_spec ();
9760 as = sym->as;
9761 as->rank = target->rank;
9762 as->type = AS_DEFERRED;
9763 as->corank = target->corank;
9764 sym->attr.dimension = 1;
9765 if (as->corank != 0)
9766 sym->attr.codimension = 1;
9768 else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
9769 && (!CLASS_DATA (sym)->as
9770 || CLASS_DATA (sym)->as->corank != target->corank))
9772 if (!CLASS_DATA (sym)->as)
9773 CLASS_DATA (sym)->as = gfc_get_array_spec ();
9774 as = CLASS_DATA (sym)->as;
9775 as->rank = target->rank;
9776 as->type = AS_DEFERRED;
9777 as->corank = target->corank;
9778 CLASS_DATA (sym)->attr.dimension = 1;
9779 if (as->corank != 0)
9780 CLASS_DATA (sym)->attr.codimension = 1;
9783 else if (!sym->attr.select_rank_temporary)
9785 /* target's rank is 0, but the type of the sym is still array valued,
9786 which has to be corrected. */
9787 if (sym->ts.type == BT_CLASS && sym->ts.u.derived
9788 && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
9790 gfc_array_spec *as;
9791 symbol_attribute attr;
9792 /* The associated variable's type is still the array type
9793 correct this now. */
9794 gfc_typespec *ts = &target->ts;
9795 gfc_ref *ref;
9796 /* Internal_ref is true, when this is ref'ing only _data and co-ref.
9798 bool internal_ref = true;
9800 for (ref = target->ref; ref != NULL; ref = ref->next)
9802 switch (ref->type)
9804 case REF_COMPONENT:
9805 ts = &ref->u.c.component->ts;
9806 internal_ref
9807 = target->ref == ref && ref->next
9808 && strncmp ("_data", ref->u.c.component->name, 5) == 0;
9809 break;
9810 case REF_ARRAY:
9811 if (ts->type == BT_CLASS)
9812 ts = &ts->u.derived->components->ts;
9813 if (internal_ref && ref->u.ar.codimen > 0)
9814 for (int i = ref->u.ar.dimen;
9815 internal_ref
9816 && i < ref->u.ar.dimen + ref->u.ar.codimen;
9817 ++i)
9818 internal_ref
9819 = ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE;
9820 break;
9821 default:
9822 break;
9825 /* Only rewrite the type of this symbol, when the refs are not the
9826 internal ones for class and co-array this-image. */
9827 if (!internal_ref)
9829 /* Create a scalar instance of the current class type. Because
9830 the rank of a class array goes into its name, the type has to
9831 be rebuilt. The alternative of (re-)setting just the
9832 attributes and as in the current type, destroys the type also
9833 in other places. */
9834 as = NULL;
9835 sym->ts = *ts;
9836 sym->ts.type = BT_CLASS;
9837 attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
9838 gfc_change_class (&sym->ts, &attr, as, 0, 0);
9839 sym->as = NULL;
9844 /* Mark this as an associate variable. */
9845 sym->attr.associate_var = 1;
9847 /* Fix up the type-spec for CHARACTER types. */
9848 if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
9850 if (!sym->ts.u.cl)
9851 sym->ts.u.cl = target->ts.u.cl;
9853 if (sym->ts.deferred
9854 && sym->ts.u.cl == target->ts.u.cl)
9856 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9857 sym->ts.deferred = 1;
9860 if (!sym->ts.u.cl->length
9861 && !sym->ts.deferred
9862 && target->expr_type == EXPR_CONSTANT)
9864 sym->ts.u.cl->length =
9865 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
9866 target->value.character.length);
9868 else if ((!sym->ts.u.cl->length
9869 || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
9870 && target->expr_type != EXPR_VARIABLE)
9872 if (!sym->ts.deferred)
9874 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9875 sym->ts.deferred = 1;
9878 /* This is reset in trans-stmt.cc after the assignment
9879 of the target expression to the associate name. */
9880 sym->attr.allocatable = 1;
9884 if (sym->ts.type == BT_CLASS
9885 && IS_INFERRED_TYPE (target)
9886 && target->ts.type == BT_DERIVED
9887 && CLASS_DATA (sym)->ts.u.derived == target->ts.u.derived
9888 && target->ref && target->ref->next && !target->ref->next->next
9889 && target->ref->next->type == REF_ARRAY)
9890 target->ts = target->symtree->n.sym->ts;
9892 /* If the target is a good class object, so is the associate variable. */
9893 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
9894 sym->attr.class_ok = 1;
9898 /* Ensure that SELECT TYPE expressions have the correct rank and a full
9899 array reference, where necessary. The symbols are artificial and so
9900 the dimension attribute and arrayspec can also be set. In addition,
9901 sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
9902 This is corrected here as well.*/
9904 static void
9905 fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, int rank, int corank,
9906 gfc_ref *ref)
9908 gfc_ref *nref = (*expr1)->ref;
9909 gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
9910 gfc_symbol *sym2;
9911 gfc_expr *selector = gfc_copy_expr (expr2);
9913 (*expr1)->rank = rank;
9914 (*expr1)->corank = corank;
9915 if (selector)
9917 gfc_resolve_expr (selector);
9918 if (selector->expr_type == EXPR_OP
9919 && selector->value.op.op == INTRINSIC_PARENTHESES)
9920 sym2 = selector->value.op.op1->symtree->n.sym;
9921 else if (selector->expr_type == EXPR_VARIABLE
9922 || selector->expr_type == EXPR_FUNCTION)
9923 sym2 = selector->symtree->n.sym;
9924 else
9925 gcc_unreachable ();
9927 else
9928 sym2 = NULL;
9930 if (sym1->ts.type == BT_CLASS)
9932 if ((*expr1)->ts.type != BT_CLASS)
9933 (*expr1)->ts = sym1->ts;
9935 CLASS_DATA (sym1)->attr.dimension = rank > 0 ? 1 : 0;
9936 CLASS_DATA (sym1)->attr.codimension = corank > 0 ? 1 : 0;
9937 if (CLASS_DATA (sym1)->as == NULL && sym2)
9938 CLASS_DATA (sym1)->as
9939 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
9941 else
9943 sym1->attr.dimension = rank > 0 ? 1 : 0;
9944 sym1->attr.codimension = corank > 0 ? 1 : 0;
9945 if (sym1->as == NULL && sym2)
9946 sym1->as = gfc_copy_array_spec (sym2->as);
9949 for (; nref; nref = nref->next)
9950 if (nref->next == NULL)
9951 break;
9953 if (ref && nref && nref->type != REF_ARRAY)
9954 nref->next = gfc_copy_ref (ref);
9955 else if (ref && !nref)
9956 (*expr1)->ref = gfc_copy_ref (ref);
9957 else if (ref && nref->u.ar.codimen != corank)
9959 for (int i = nref->u.ar.dimen; i < GFC_MAX_DIMENSIONS; ++i)
9960 nref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
9961 nref->u.ar.codimen = corank;
9966 static gfc_expr *
9967 build_loc_call (gfc_expr *sym_expr)
9969 gfc_expr *loc_call;
9970 loc_call = gfc_get_expr ();
9971 loc_call->expr_type = EXPR_FUNCTION;
9972 gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
9973 loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
9974 loc_call->symtree->n.sym->attr.intrinsic = 1;
9975 loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
9976 gfc_commit_symbol (loc_call->symtree->n.sym);
9977 loc_call->ts.type = BT_INTEGER;
9978 loc_call->ts.kind = gfc_index_integer_kind;
9979 loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
9980 loc_call->value.function.actual = gfc_get_actual_arglist ();
9981 loc_call->value.function.actual->expr = sym_expr;
9982 loc_call->where = sym_expr->where;
9983 return loc_call;
9986 /* Resolve a SELECT TYPE statement. */
9988 static void
9989 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
9991 gfc_symbol *selector_type;
9992 gfc_code *body, *new_st, *if_st, *tail;
9993 gfc_code *class_is = NULL, *default_case = NULL;
9994 gfc_case *c;
9995 gfc_symtree *st;
9996 char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
9997 gfc_namespace *ns;
9998 int error = 0;
9999 int rank = 0, corank = 0;
10000 gfc_ref* ref = NULL;
10001 gfc_expr *selector_expr = NULL;
10003 ns = code->ext.block.ns;
10004 if (code->expr2)
10006 /* Set this, or coarray checks in resolve will fail. */
10007 code->expr1->symtree->n.sym->attr.select_type_temporary = 1;
10009 gfc_resolve (ns);
10011 /* Check for F03:C813. */
10012 if (code->expr1->ts.type != BT_CLASS
10013 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
10015 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
10016 "at %L", &code->loc);
10017 return;
10020 /* Prevent segfault, when class type is not initialized due to previous
10021 error. */
10022 if (!code->expr1->symtree->n.sym->attr.class_ok
10023 || (code->expr1->ts.type == BT_CLASS && !code->expr1->ts.u.derived))
10024 return;
10026 if (code->expr2)
10028 gfc_ref *ref2 = NULL;
10029 for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
10030 if (ref->type == REF_COMPONENT
10031 && ref->u.c.component->ts.type == BT_CLASS)
10032 ref2 = ref;
10034 if (ref2)
10036 if (code->expr1->symtree->n.sym->attr.untyped)
10037 code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
10038 selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
10040 else
10042 if (code->expr1->symtree->n.sym->attr.untyped)
10043 code->expr1->symtree->n.sym->ts = code->expr2->ts;
10044 /* Sometimes the selector expression is given the typespec of the
10045 '_data' field, which is logical enough but inappropriate here. */
10046 if (code->expr2->ts.type == BT_DERIVED
10047 && code->expr2->symtree
10048 && code->expr2->symtree->n.sym->ts.type == BT_CLASS)
10049 code->expr2->ts = code->expr2->symtree->n.sym->ts;
10050 selector_type = CLASS_DATA (code->expr2)
10051 ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
10054 if (code->expr1->ts.type == BT_CLASS && CLASS_DATA (code->expr1)->as)
10056 CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
10057 CLASS_DATA (code->expr1)->as->corank = code->expr2->corank;
10058 CLASS_DATA (code->expr1)->as->cotype = AS_DEFERRED;
10061 /* F2008: C803 The selector expression must not be coindexed. */
10062 if (gfc_is_coindexed (code->expr2))
10064 gfc_error ("Selector at %L must not be coindexed",
10065 &code->expr2->where);
10066 return;
10070 else
10072 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
10074 if (gfc_is_coindexed (code->expr1))
10076 gfc_error ("Selector at %L must not be coindexed",
10077 &code->expr1->where);
10078 return;
10082 /* Loop over TYPE IS / CLASS IS cases. */
10083 for (body = code->block; body; body = body->block)
10085 c = body->ext.block.case_list;
10087 if (!error)
10089 /* Check for repeated cases. */
10090 for (tail = code->block; tail; tail = tail->block)
10092 gfc_case *d = tail->ext.block.case_list;
10093 if (tail == body)
10094 break;
10096 if (c->ts.type == d->ts.type
10097 && ((c->ts.type == BT_DERIVED
10098 && c->ts.u.derived && d->ts.u.derived
10099 && !strcmp (c->ts.u.derived->name,
10100 d->ts.u.derived->name))
10101 || c->ts.type == BT_UNKNOWN
10102 || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10103 && c->ts.kind == d->ts.kind)))
10105 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
10106 &c->where, &d->where);
10107 return;
10112 /* Check F03:C815. */
10113 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10114 && selector_type
10115 && !selector_type->attr.unlimited_polymorphic
10116 && !gfc_type_is_extensible (c->ts.u.derived))
10118 gfc_error ("Derived type %qs at %L must be extensible",
10119 c->ts.u.derived->name, &c->where);
10120 error++;
10121 continue;
10124 /* Check F03:C816. */
10125 if (c->ts.type != BT_UNKNOWN
10126 && selector_type && !selector_type->attr.unlimited_polymorphic
10127 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
10128 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
10130 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10131 gfc_error ("Derived type %qs at %L must be an extension of %qs",
10132 c->ts.u.derived->name, &c->where, selector_type->name);
10133 else
10134 gfc_error ("Unexpected intrinsic type %qs at %L",
10135 gfc_basic_typename (c->ts.type), &c->where);
10136 error++;
10137 continue;
10140 /* Check F03:C814. */
10141 if (c->ts.type == BT_CHARACTER
10142 && (c->ts.u.cl->length != NULL || c->ts.deferred))
10144 gfc_error ("The type-spec at %L shall specify that each length "
10145 "type parameter is assumed", &c->where);
10146 error++;
10147 continue;
10150 /* Intercept the DEFAULT case. */
10151 if (c->ts.type == BT_UNKNOWN)
10153 /* Check F03:C818. */
10154 if (default_case)
10156 gfc_error ("The DEFAULT CASE at %L cannot be followed "
10157 "by a second DEFAULT CASE at %L",
10158 &default_case->ext.block.case_list->where, &c->where);
10159 error++;
10160 continue;
10163 default_case = body;
10167 if (error > 0)
10168 return;
10170 /* Transform SELECT TYPE statement to BLOCK and associate selector to
10171 target if present. If there are any EXIT statements referring to the
10172 SELECT TYPE construct, this is no problem because the gfc_code
10173 reference stays the same and EXIT is equally possible from the BLOCK
10174 it is changed to. */
10175 code->op = EXEC_BLOCK;
10176 if (code->expr2)
10178 gfc_association_list* assoc;
10180 assoc = gfc_get_association_list ();
10181 assoc->st = code->expr1->symtree;
10182 assoc->target = gfc_copy_expr (code->expr2);
10183 assoc->target->where = code->expr2->where;
10184 /* assoc->variable will be set by resolve_assoc_var. */
10186 code->ext.block.assoc = assoc;
10187 code->expr1->symtree->n.sym->assoc = assoc;
10189 resolve_assoc_var (code->expr1->symtree->n.sym, false);
10191 else
10192 code->ext.block.assoc = NULL;
10194 /* Ensure that the selector rank and arrayspec are available to
10195 correct expressions in which they might be missing. */
10196 if (code->expr2 && (code->expr2->rank || code->expr2->corank))
10198 rank = code->expr2->rank;
10199 corank = code->expr2->corank;
10200 for (ref = code->expr2->ref; ref; ref = ref->next)
10201 if (ref->next == NULL)
10202 break;
10203 if (ref && ref->type == REF_ARRAY)
10204 ref = gfc_copy_ref (ref);
10206 /* Fixup expr1 if necessary. */
10207 if (rank || corank)
10208 fixup_array_ref (&code->expr1, code->expr2, rank, corank, ref);
10210 else if (code->expr1->rank || code->expr1->corank)
10212 rank = code->expr1->rank;
10213 corank = code->expr1->corank;
10214 for (ref = code->expr1->ref; ref; ref = ref->next)
10215 if (ref->next == NULL)
10216 break;
10217 if (ref && ref->type == REF_ARRAY)
10218 ref = gfc_copy_ref (ref);
10221 /* Add EXEC_SELECT to switch on type. */
10222 new_st = gfc_get_code (code->op);
10223 new_st->expr1 = code->expr1;
10224 new_st->expr2 = code->expr2;
10225 new_st->block = code->block;
10226 code->expr1 = code->expr2 = NULL;
10227 code->block = NULL;
10228 if (!ns->code)
10229 ns->code = new_st;
10230 else
10231 ns->code->next = new_st;
10232 code = new_st;
10233 code->op = EXEC_SELECT_TYPE;
10235 /* Use the intrinsic LOC function to generate an integer expression
10236 for the vtable of the selector. Note that the rank of the selector
10237 expression has to be set to zero. */
10238 gfc_add_vptr_component (code->expr1);
10239 code->expr1->rank = 0;
10240 code->expr1->corank = 0;
10241 code->expr1 = build_loc_call (code->expr1);
10242 selector_expr = code->expr1->value.function.actual->expr;
10244 /* Loop over TYPE IS / CLASS IS cases. */
10245 for (body = code->block; body; body = body->block)
10247 gfc_symbol *vtab;
10248 gfc_expr *e;
10249 c = body->ext.block.case_list;
10251 /* Generate an index integer expression for address of the
10252 TYPE/CLASS vtable and store it in c->low. The hash expression
10253 is stored in c->high and is used to resolve intrinsic cases. */
10254 if (c->ts.type != BT_UNKNOWN)
10256 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10258 vtab = gfc_find_derived_vtab (c->ts.u.derived);
10259 gcc_assert (vtab);
10260 c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
10261 c->ts.u.derived->hash_value);
10263 else
10265 vtab = gfc_find_vtab (&c->ts);
10266 gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
10267 e = CLASS_DATA (vtab)->initializer;
10268 c->high = gfc_copy_expr (e);
10269 if (c->high->ts.kind != gfc_integer_4_kind)
10271 gfc_typespec ts;
10272 ts.kind = gfc_integer_4_kind;
10273 ts.type = BT_INTEGER;
10274 gfc_convert_type_warn (c->high, &ts, 2, 0);
10278 e = gfc_lval_expr_from_sym (vtab);
10279 c->low = build_loc_call (e);
10281 else
10282 continue;
10284 /* Associate temporary to selector. This should only be done
10285 when this case is actually true, so build a new ASSOCIATE
10286 that does precisely this here (instead of using the
10287 'global' one). */
10289 if (c->ts.type == BT_CLASS)
10290 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
10291 else if (c->ts.type == BT_DERIVED)
10292 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
10293 else if (c->ts.type == BT_CHARACTER)
10295 HOST_WIDE_INT charlen = 0;
10296 if (c->ts.u.cl && c->ts.u.cl->length
10297 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10298 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
10299 snprintf (name, sizeof (name),
10300 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
10301 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
10303 else
10304 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
10305 c->ts.kind);
10307 st = gfc_find_symtree (ns->sym_root, name);
10308 gcc_assert (st->n.sym->assoc);
10309 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
10310 st->n.sym->assoc->target->where = selector_expr->where;
10311 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
10313 gfc_add_data_component (st->n.sym->assoc->target);
10314 /* Fixup the target expression if necessary. */
10315 if (rank || corank)
10316 fixup_array_ref (&st->n.sym->assoc->target, nullptr, rank, corank,
10317 ref);
10320 new_st = gfc_get_code (EXEC_BLOCK);
10321 new_st->ext.block.ns = gfc_build_block_ns (ns);
10322 new_st->ext.block.ns->code = body->next;
10323 body->next = new_st;
10325 /* Chain in the new list only if it is marked as dangling. Otherwise
10326 there is a CASE label overlap and this is already used. Just ignore,
10327 the error is diagnosed elsewhere. */
10328 if (st->n.sym->assoc->dangling)
10330 new_st->ext.block.assoc = st->n.sym->assoc;
10331 st->n.sym->assoc->dangling = 0;
10334 resolve_assoc_var (st->n.sym, false);
10337 /* Take out CLASS IS cases for separate treatment. */
10338 body = code;
10339 while (body && body->block)
10341 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
10343 /* Add to class_is list. */
10344 if (class_is == NULL)
10346 class_is = body->block;
10347 tail = class_is;
10349 else
10351 for (tail = class_is; tail->block; tail = tail->block) ;
10352 tail->block = body->block;
10353 tail = tail->block;
10355 /* Remove from EXEC_SELECT list. */
10356 body->block = body->block->block;
10357 tail->block = NULL;
10359 else
10360 body = body->block;
10363 if (class_is)
10365 gfc_symbol *vtab;
10367 if (!default_case)
10369 /* Add a default case to hold the CLASS IS cases. */
10370 for (tail = code; tail->block; tail = tail->block) ;
10371 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
10372 tail = tail->block;
10373 tail->ext.block.case_list = gfc_get_case ();
10374 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
10375 tail->next = NULL;
10376 default_case = tail;
10379 /* More than one CLASS IS block? */
10380 if (class_is->block)
10382 gfc_code **c1,*c2;
10383 bool swapped;
10384 /* Sort CLASS IS blocks by extension level. */
10387 swapped = false;
10388 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
10390 c2 = (*c1)->block;
10391 /* F03:C817 (check for doubles). */
10392 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
10393 == c2->ext.block.case_list->ts.u.derived->hash_value)
10395 gfc_error ("Double CLASS IS block in SELECT TYPE "
10396 "statement at %L",
10397 &c2->ext.block.case_list->where);
10398 return;
10400 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
10401 < c2->ext.block.case_list->ts.u.derived->attr.extension)
10403 /* Swap. */
10404 (*c1)->block = c2->block;
10405 c2->block = *c1;
10406 *c1 = c2;
10407 swapped = true;
10411 while (swapped);
10414 /* Generate IF chain. */
10415 if_st = gfc_get_code (EXEC_IF);
10416 new_st = if_st;
10417 for (body = class_is; body; body = body->block)
10419 new_st->block = gfc_get_code (EXEC_IF);
10420 new_st = new_st->block;
10421 /* Set up IF condition: Call _gfortran_is_extension_of. */
10422 new_st->expr1 = gfc_get_expr ();
10423 new_st->expr1->expr_type = EXPR_FUNCTION;
10424 new_st->expr1->ts.type = BT_LOGICAL;
10425 new_st->expr1->ts.kind = 4;
10426 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
10427 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
10428 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
10429 /* Set up arguments. */
10430 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
10431 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
10432 new_st->expr1->value.function.actual->expr->where = code->loc;
10433 new_st->expr1->where = code->loc;
10434 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
10435 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
10436 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
10437 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
10438 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
10439 new_st->expr1->value.function.actual->next->expr->where = code->loc;
10440 /* Set up types in formal arg list. */
10441 new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
10442 new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
10443 new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
10444 new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
10446 new_st->next = body->next;
10448 if (default_case->next)
10450 new_st->block = gfc_get_code (EXEC_IF);
10451 new_st = new_st->block;
10452 new_st->next = default_case->next;
10455 /* Replace CLASS DEFAULT code by the IF chain. */
10456 default_case->next = if_st;
10459 /* Resolve the internal code. This cannot be done earlier because
10460 it requires that the sym->assoc of selectors is set already. */
10461 gfc_current_ns = ns;
10462 gfc_resolve_blocks (code->block, gfc_current_ns);
10463 gfc_current_ns = old_ns;
10465 free (ref);
10469 /* Resolve a SELECT RANK statement. */
10471 static void
10472 resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
10474 gfc_namespace *ns;
10475 gfc_code *body, *new_st, *tail;
10476 gfc_case *c;
10477 char tname[GFC_MAX_SYMBOL_LEN + 7];
10478 char name[2 * GFC_MAX_SYMBOL_LEN];
10479 gfc_symtree *st;
10480 gfc_expr *selector_expr = NULL;
10481 int case_value;
10482 HOST_WIDE_INT charlen = 0;
10484 ns = code->ext.block.ns;
10485 gfc_resolve (ns);
10487 code->op = EXEC_BLOCK;
10488 if (code->expr2)
10490 gfc_association_list* assoc;
10492 assoc = gfc_get_association_list ();
10493 assoc->st = code->expr1->symtree;
10494 assoc->target = gfc_copy_expr (code->expr2);
10495 assoc->target->where = code->expr2->where;
10496 /* assoc->variable will be set by resolve_assoc_var. */
10498 code->ext.block.assoc = assoc;
10499 code->expr1->symtree->n.sym->assoc = assoc;
10501 resolve_assoc_var (code->expr1->symtree->n.sym, false);
10503 else
10504 code->ext.block.assoc = NULL;
10506 /* Loop over RANK cases. Note that returning on the errors causes a
10507 cascade of further errors because the case blocks do not compile
10508 correctly. */
10509 for (body = code->block; body; body = body->block)
10511 c = body->ext.block.case_list;
10512 if (c->low)
10513 case_value = (int) mpz_get_si (c->low->value.integer);
10514 else
10515 case_value = -2;
10517 /* Check for repeated cases. */
10518 for (tail = code->block; tail; tail = tail->block)
10520 gfc_case *d = tail->ext.block.case_list;
10521 int case_value2;
10523 if (tail == body)
10524 break;
10526 /* Check F2018: C1153. */
10527 if (!c->low && !d->low)
10528 gfc_error ("RANK DEFAULT at %L is repeated at %L",
10529 &c->where, &d->where);
10531 if (!c->low || !d->low)
10532 continue;
10534 /* Check F2018: C1153. */
10535 case_value2 = (int) mpz_get_si (d->low->value.integer);
10536 if ((case_value == case_value2) && case_value == -1)
10537 gfc_error ("RANK (*) at %L is repeated at %L",
10538 &c->where, &d->where);
10539 else if (case_value == case_value2)
10540 gfc_error ("RANK (%i) at %L is repeated at %L",
10541 case_value, &c->where, &d->where);
10544 if (!c->low)
10545 continue;
10547 /* Check F2018: C1155. */
10548 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
10549 || gfc_expr_attr (code->expr1).pointer))
10550 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
10551 "allocatable selector at %L", &c->where, &code->expr1->where);
10554 /* Add EXEC_SELECT to switch on rank. */
10555 new_st = gfc_get_code (code->op);
10556 new_st->expr1 = code->expr1;
10557 new_st->expr2 = code->expr2;
10558 new_st->block = code->block;
10559 code->expr1 = code->expr2 = NULL;
10560 code->block = NULL;
10561 if (!ns->code)
10562 ns->code = new_st;
10563 else
10564 ns->code->next = new_st;
10565 code = new_st;
10566 code->op = EXEC_SELECT_RANK;
10568 selector_expr = code->expr1;
10570 /* Loop over SELECT RANK cases. */
10571 for (body = code->block; body; body = body->block)
10573 c = body->ext.block.case_list;
10574 int case_value;
10576 /* Pass on the default case. */
10577 if (c->low == NULL)
10578 continue;
10580 /* Associate temporary to selector. This should only be done
10581 when this case is actually true, so build a new ASSOCIATE
10582 that does precisely this here (instead of using the
10583 'global' one). */
10584 if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
10585 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10586 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
10588 if (c->ts.type == BT_CLASS)
10589 sprintf (tname, "class_%s", c->ts.u.derived->name);
10590 else if (c->ts.type == BT_DERIVED)
10591 sprintf (tname, "type_%s", c->ts.u.derived->name);
10592 else if (c->ts.type != BT_CHARACTER)
10593 sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
10594 else
10595 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
10596 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
10598 case_value = (int) mpz_get_si (c->low->value.integer);
10599 if (case_value >= 0)
10600 sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
10601 else
10602 sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
10604 st = gfc_find_symtree (ns->sym_root, name);
10605 gcc_assert (st->n.sym->assoc);
10607 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
10608 st->n.sym->assoc->target->where = selector_expr->where;
10610 new_st = gfc_get_code (EXEC_BLOCK);
10611 new_st->ext.block.ns = gfc_build_block_ns (ns);
10612 new_st->ext.block.ns->code = body->next;
10613 body->next = new_st;
10615 /* Chain in the new list only if it is marked as dangling. Otherwise
10616 there is a CASE label overlap and this is already used. Just ignore,
10617 the error is diagnosed elsewhere. */
10618 if (st->n.sym->assoc->dangling)
10620 new_st->ext.block.assoc = st->n.sym->assoc;
10621 st->n.sym->assoc->dangling = 0;
10624 resolve_assoc_var (st->n.sym, false);
10627 gfc_current_ns = ns;
10628 gfc_resolve_blocks (code->block, gfc_current_ns);
10629 gfc_current_ns = old_ns;
10633 /* Resolve a transfer statement. This is making sure that:
10634 -- a derived type being transferred has only non-pointer components
10635 -- a derived type being transferred doesn't have private components, unless
10636 it's being transferred from the module where the type was defined
10637 -- we're not trying to transfer a whole assumed size array. */
10639 static void
10640 resolve_transfer (gfc_code *code)
10642 gfc_symbol *sym, *derived;
10643 gfc_ref *ref;
10644 gfc_expr *exp;
10645 bool write = false;
10646 bool formatted = false;
10647 gfc_dt *dt = code->ext.dt;
10648 gfc_symbol *dtio_sub = NULL;
10650 exp = code->expr1;
10652 while (exp != NULL && exp->expr_type == EXPR_OP
10653 && exp->value.op.op == INTRINSIC_PARENTHESES)
10654 exp = exp->value.op.op1;
10656 if (exp && exp->expr_type == EXPR_NULL
10657 && code->ext.dt)
10659 gfc_error ("Invalid context for NULL () intrinsic at %L",
10660 &exp->where);
10661 return;
10664 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
10665 && exp->expr_type != EXPR_FUNCTION
10666 && exp->expr_type != EXPR_ARRAY
10667 && exp->expr_type != EXPR_STRUCTURE))
10668 return;
10670 /* If we are reading, the variable will be changed. Note that
10671 code->ext.dt may be NULL if the TRANSFER is related to
10672 an INQUIRE statement -- but in this case, we are not reading, either. */
10673 if (dt && dt->dt_io_kind->value.iokind == M_READ
10674 && !gfc_check_vardef_context (exp, false, false, false,
10675 _("item in READ")))
10676 return;
10678 const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
10679 || exp->expr_type == EXPR_FUNCTION
10680 || exp->expr_type == EXPR_ARRAY
10681 ? &exp->ts : &exp->symtree->n.sym->ts;
10683 /* Go to actual component transferred. */
10684 for (ref = exp->ref; ref; ref = ref->next)
10685 if (ref->type == REF_COMPONENT)
10686 ts = &ref->u.c.component->ts;
10688 if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
10689 && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
10691 derived = ts->u.derived;
10693 /* Determine when to use the formatted DTIO procedure. */
10694 if (dt && (dt->format_expr || dt->format_label))
10695 formatted = true;
10697 write = dt->dt_io_kind->value.iokind == M_WRITE
10698 || dt->dt_io_kind->value.iokind == M_PRINT;
10699 dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
10701 if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
10703 dt->udtio = exp;
10704 sym = exp->symtree->n.sym->ns->proc_name;
10705 /* Check to see if this is a nested DTIO call, with the
10706 dummy as the io-list object. */
10707 if (sym && sym == dtio_sub && sym->formal
10708 && sym->formal->sym == exp->symtree->n.sym
10709 && exp->ref == NULL)
10711 if (!sym->attr.recursive)
10713 gfc_error ("DTIO %s procedure at %L must be recursive",
10714 sym->name, &sym->declared_at);
10715 return;
10721 if (ts->type == BT_CLASS && dtio_sub == NULL)
10723 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
10724 "it is processed by a defined input/output procedure",
10725 &code->loc);
10726 return;
10729 if (ts->type == BT_DERIVED)
10731 /* Check that transferred derived type doesn't contain POINTER
10732 components unless it is processed by a defined input/output
10733 procedure". */
10734 if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
10736 gfc_error ("Data transfer element at %L cannot have POINTER "
10737 "components unless it is processed by a defined "
10738 "input/output procedure", &code->loc);
10739 return;
10742 /* F08:C935. */
10743 if (ts->u.derived->attr.proc_pointer_comp)
10745 gfc_error ("Data transfer element at %L cannot have "
10746 "procedure pointer components", &code->loc);
10747 return;
10750 if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
10752 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
10753 "components unless it is processed by a defined "
10754 "input/output procedure", &code->loc);
10755 return;
10758 /* C_PTR and C_FUNPTR have private components which means they cannot
10759 be printed. However, if -std=gnu and not -pedantic, allow
10760 the component to be printed to help debugging. */
10761 if (ts->u.derived->ts.f90_type == BT_VOID)
10763 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
10764 "cannot have PRIVATE components", &code->loc))
10765 return;
10767 else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
10769 gfc_error ("Data transfer element at %L cannot have "
10770 "PRIVATE components unless it is processed by "
10771 "a defined input/output procedure", &code->loc);
10772 return;
10776 if (exp->expr_type == EXPR_STRUCTURE)
10777 return;
10779 if (exp->expr_type == EXPR_ARRAY)
10780 return;
10782 sym = exp->symtree->n.sym;
10784 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
10785 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
10787 gfc_error ("Data transfer element at %L cannot be a full reference to "
10788 "an assumed-size array", &code->loc);
10789 return;
10794 /*********** Toplevel code resolution subroutines ***********/
10796 /* Find the set of labels that are reachable from this block. We also
10797 record the last statement in each block. */
10799 static void
10800 find_reachable_labels (gfc_code *block)
10802 gfc_code *c;
10804 if (!block)
10805 return;
10807 cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
10809 /* Collect labels in this block. We don't keep those corresponding
10810 to END {IF|SELECT}, these are checked in resolve_branch by going
10811 up through the code_stack. */
10812 for (c = block; c; c = c->next)
10814 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
10815 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
10818 /* Merge with labels from parent block. */
10819 if (cs_base->prev)
10821 gcc_assert (cs_base->prev->reachable_labels);
10822 bitmap_ior_into (cs_base->reachable_labels,
10823 cs_base->prev->reachable_labels);
10828 static void
10829 resolve_lock_unlock_event (gfc_code *code)
10831 if (code->expr1->expr_type == EXPR_FUNCTION
10832 && code->expr1->value.function.isym
10833 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10834 remove_caf_get_intrinsic (code->expr1);
10836 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
10837 && (code->expr1->ts.type != BT_DERIVED
10838 || code->expr1->expr_type != EXPR_VARIABLE
10839 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
10840 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
10841 || code->expr1->rank != 0
10842 || (!gfc_is_coarray (code->expr1) &&
10843 !gfc_is_coindexed (code->expr1))))
10844 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
10845 &code->expr1->where);
10846 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
10847 && (code->expr1->ts.type != BT_DERIVED
10848 || code->expr1->expr_type != EXPR_VARIABLE
10849 || code->expr1->ts.u.derived->from_intmod
10850 != INTMOD_ISO_FORTRAN_ENV
10851 || code->expr1->ts.u.derived->intmod_sym_id
10852 != ISOFORTRAN_EVENT_TYPE
10853 || code->expr1->rank != 0))
10854 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
10855 &code->expr1->where);
10856 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
10857 && !gfc_is_coindexed (code->expr1))
10858 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
10859 &code->expr1->where);
10860 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
10861 gfc_error ("Event variable argument at %L must be a coarray but not "
10862 "coindexed", &code->expr1->where);
10864 /* Check STAT. */
10865 if (code->expr2
10866 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10867 || code->expr2->expr_type != EXPR_VARIABLE))
10868 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10869 &code->expr2->where);
10871 if (code->expr2
10872 && !gfc_check_vardef_context (code->expr2, false, false, false,
10873 _("STAT variable")))
10874 return;
10876 /* Check ERRMSG. */
10877 if (code->expr3
10878 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10879 || code->expr3->expr_type != EXPR_VARIABLE))
10880 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10881 &code->expr3->where);
10883 if (code->expr3
10884 && !gfc_check_vardef_context (code->expr3, false, false, false,
10885 _("ERRMSG variable")))
10886 return;
10888 /* Check for LOCK the ACQUIRED_LOCK. */
10889 if (code->op != EXEC_EVENT_WAIT && code->expr4
10890 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
10891 || code->expr4->expr_type != EXPR_VARIABLE))
10892 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
10893 "variable", &code->expr4->where);
10895 if (code->op != EXEC_EVENT_WAIT && code->expr4
10896 && !gfc_check_vardef_context (code->expr4, false, false, false,
10897 _("ACQUIRED_LOCK variable")))
10898 return;
10900 /* Check for EVENT WAIT the UNTIL_COUNT. */
10901 if (code->op == EXEC_EVENT_WAIT && code->expr4)
10903 if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
10904 || code->expr4->rank != 0)
10905 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
10906 "expression", &code->expr4->where);
10911 static void
10912 resolve_critical (gfc_code *code)
10914 gfc_symtree *symtree;
10915 gfc_symbol *lock_type;
10916 char name[GFC_MAX_SYMBOL_LEN];
10917 static int serial = 0;
10919 if (flag_coarray != GFC_FCOARRAY_LIB)
10920 return;
10922 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10923 GFC_PREFIX ("lock_type"));
10924 if (symtree)
10925 lock_type = symtree->n.sym;
10926 else
10928 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
10929 false) != 0)
10930 gcc_unreachable ();
10931 lock_type = symtree->n.sym;
10932 lock_type->attr.flavor = FL_DERIVED;
10933 lock_type->attr.zero_comp = 1;
10934 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
10935 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
10938 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
10939 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
10940 gcc_unreachable ();
10942 code->resolved_sym = symtree->n.sym;
10943 symtree->n.sym->attr.flavor = FL_VARIABLE;
10944 symtree->n.sym->attr.referenced = 1;
10945 symtree->n.sym->attr.artificial = 1;
10946 symtree->n.sym->attr.codimension = 1;
10947 symtree->n.sym->ts.type = BT_DERIVED;
10948 symtree->n.sym->ts.u.derived = lock_type;
10949 symtree->n.sym->as = gfc_get_array_spec ();
10950 symtree->n.sym->as->corank = 1;
10951 symtree->n.sym->as->type = AS_EXPLICIT;
10952 symtree->n.sym->as->cotype = AS_EXPLICIT;
10953 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
10954 NULL, 1);
10955 gfc_commit_symbols();
10959 static void
10960 resolve_sync (gfc_code *code)
10962 /* Check imageset. The * case matches expr1 == NULL. */
10963 if (code->expr1)
10965 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
10966 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
10967 "INTEGER expression", &code->expr1->where);
10968 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
10969 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
10970 gfc_error ("Imageset argument at %L must between 1 and num_images()",
10971 &code->expr1->where);
10972 else if (code->expr1->expr_type == EXPR_ARRAY
10973 && gfc_simplify_expr (code->expr1, 0))
10975 gfc_constructor *cons;
10976 cons = gfc_constructor_first (code->expr1->value.constructor);
10977 for (; cons; cons = gfc_constructor_next (cons))
10978 if (cons->expr->expr_type == EXPR_CONSTANT
10979 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
10980 gfc_error ("Imageset argument at %L must between 1 and "
10981 "num_images()", &cons->expr->where);
10985 /* Check STAT. */
10986 gfc_resolve_expr (code->expr2);
10987 if (code->expr2)
10989 if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
10990 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10991 &code->expr2->where);
10992 else
10993 gfc_check_vardef_context (code->expr2, false, false, false,
10994 _("STAT variable"));
10997 /* Check ERRMSG. */
10998 gfc_resolve_expr (code->expr3);
10999 if (code->expr3)
11001 if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
11002 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
11003 &code->expr3->where);
11004 else
11005 gfc_check_vardef_context (code->expr3, false, false, false,
11006 _("ERRMSG variable"));
11011 /* Given a branch to a label, see if the branch is conforming.
11012 The code node describes where the branch is located. */
11014 static void
11015 resolve_branch (gfc_st_label *label, gfc_code *code)
11017 code_stack *stack;
11019 if (label == NULL)
11020 return;
11022 /* Step one: is this a valid branching target? */
11024 if (label->defined == ST_LABEL_UNKNOWN)
11026 gfc_error ("Label %d referenced at %L is never defined", label->value,
11027 &code->loc);
11028 return;
11031 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
11033 gfc_error ("Statement at %L is not a valid branch target statement "
11034 "for the branch statement at %L", &label->where, &code->loc);
11035 return;
11038 /* Step two: make sure this branch is not a branch to itself ;-) */
11040 if (code->here == label)
11042 gfc_warning (0,
11043 "Branch at %L may result in an infinite loop", &code->loc);
11044 return;
11047 /* Step three: See if the label is in the same block as the
11048 branching statement. The hard work has been done by setting up
11049 the bitmap reachable_labels. */
11051 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
11053 /* Check now whether there is a CRITICAL construct; if so, check
11054 whether the label is still visible outside of the CRITICAL block,
11055 which is invalid. */
11056 for (stack = cs_base; stack; stack = stack->prev)
11058 if (stack->current->op == EXEC_CRITICAL
11059 && bitmap_bit_p (stack->reachable_labels, label->value))
11060 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
11061 "label at %L", &code->loc, &label->where);
11062 else if (stack->current->op == EXEC_DO_CONCURRENT
11063 && bitmap_bit_p (stack->reachable_labels, label->value))
11064 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
11065 "for label at %L", &code->loc, &label->where);
11068 return;
11071 /* Step four: If we haven't found the label in the bitmap, it may
11072 still be the label of the END of the enclosing block, in which
11073 case we find it by going up the code_stack. */
11075 for (stack = cs_base; stack; stack = stack->prev)
11077 if (stack->current->next && stack->current->next->here == label)
11078 break;
11079 if (stack->current->op == EXEC_CRITICAL)
11081 /* Note: A label at END CRITICAL does not leave the CRITICAL
11082 construct as END CRITICAL is still part of it. */
11083 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
11084 " at %L", &code->loc, &label->where);
11085 return;
11087 else if (stack->current->op == EXEC_DO_CONCURRENT)
11089 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
11090 "label at %L", &code->loc, &label->where);
11091 return;
11095 if (stack)
11097 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
11098 return;
11101 /* The label is not in an enclosing block, so illegal. This was
11102 allowed in Fortran 66, so we allow it as extension. No
11103 further checks are necessary in this case. */
11104 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
11105 "as the GOTO statement at %L", &label->where,
11106 &code->loc);
11107 return;
11111 /* Check whether EXPR1 has the same shape as EXPR2. */
11113 static bool
11114 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
11116 mpz_t shape[GFC_MAX_DIMENSIONS];
11117 mpz_t shape2[GFC_MAX_DIMENSIONS];
11118 bool result = false;
11119 int i;
11121 /* Compare the rank. */
11122 if (expr1->rank != expr2->rank)
11123 return result;
11125 /* Compare the size of each dimension. */
11126 for (i=0; i<expr1->rank; i++)
11128 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
11129 goto ignore;
11131 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
11132 goto ignore;
11134 if (mpz_cmp (shape[i], shape2[i]))
11135 goto over;
11138 /* When either of the two expression is an assumed size array, we
11139 ignore the comparison of dimension sizes. */
11140 ignore:
11141 result = true;
11143 over:
11144 gfc_clear_shape (shape, i);
11145 gfc_clear_shape (shape2, i);
11146 return result;
11150 /* Check whether a WHERE assignment target or a WHERE mask expression
11151 has the same shape as the outmost WHERE mask expression. */
11153 static void
11154 resolve_where (gfc_code *code, gfc_expr *mask)
11156 gfc_code *cblock;
11157 gfc_code *cnext;
11158 gfc_expr *e = NULL;
11160 cblock = code->block;
11162 /* Store the first WHERE mask-expr of the WHERE statement or construct.
11163 In case of nested WHERE, only the outmost one is stored. */
11164 if (mask == NULL) /* outmost WHERE */
11165 e = cblock->expr1;
11166 else /* inner WHERE */
11167 e = mask;
11169 while (cblock)
11171 if (cblock->expr1)
11173 /* Check if the mask-expr has a consistent shape with the
11174 outmost WHERE mask-expr. */
11175 if (!resolve_where_shape (cblock->expr1, e))
11176 gfc_error ("WHERE mask at %L has inconsistent shape",
11177 &cblock->expr1->where);
11180 /* the assignment statement of a WHERE statement, or the first
11181 statement in where-body-construct of a WHERE construct */
11182 cnext = cblock->next;
11183 while (cnext)
11185 switch (cnext->op)
11187 /* WHERE assignment statement */
11188 case EXEC_ASSIGN:
11190 /* Check shape consistent for WHERE assignment target. */
11191 if (e && !resolve_where_shape (cnext->expr1, e))
11192 gfc_error ("WHERE assignment target at %L has "
11193 "inconsistent shape", &cnext->expr1->where);
11195 if (cnext->op == EXEC_ASSIGN
11196 && gfc_may_be_finalized (cnext->expr1->ts))
11197 cnext->expr1->must_finalize = 1;
11199 break;
11202 case EXEC_ASSIGN_CALL:
11203 resolve_call (cnext);
11204 if (!cnext->resolved_sym->attr.elemental)
11205 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
11206 &cnext->ext.actual->expr->where);
11207 break;
11209 /* WHERE or WHERE construct is part of a where-body-construct */
11210 case EXEC_WHERE:
11211 resolve_where (cnext, e);
11212 break;
11214 default:
11215 gfc_error ("Unsupported statement inside WHERE at %L",
11216 &cnext->loc);
11218 /* the next statement within the same where-body-construct */
11219 cnext = cnext->next;
11221 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
11222 cblock = cblock->block;
11227 /* Resolve assignment in FORALL construct.
11228 NVAR is the number of FORALL index variables, and VAR_EXPR records the
11229 FORALL index variables. */
11231 static void
11232 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
11234 int n;
11236 for (n = 0; n < nvar; n++)
11238 gfc_symbol *forall_index;
11240 forall_index = var_expr[n]->symtree->n.sym;
11242 /* Check whether the assignment target is one of the FORALL index
11243 variable. */
11244 if ((code->expr1->expr_type == EXPR_VARIABLE)
11245 && (code->expr1->symtree->n.sym == forall_index))
11246 gfc_error ("Assignment to a FORALL index variable at %L",
11247 &code->expr1->where);
11248 else
11250 /* If one of the FORALL index variables doesn't appear in the
11251 assignment variable, then there could be a many-to-one
11252 assignment. Emit a warning rather than an error because the
11253 mask could be resolving this problem. */
11254 if (!find_forall_index (code->expr1, forall_index, 0))
11255 gfc_warning (0, "The FORALL with index %qs is not used on the "
11256 "left side of the assignment at %L and so might "
11257 "cause multiple assignment to this object",
11258 var_expr[n]->symtree->name, &code->expr1->where);
11264 /* Resolve WHERE statement in FORALL construct. */
11266 static void
11267 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
11268 gfc_expr **var_expr)
11270 gfc_code *cblock;
11271 gfc_code *cnext;
11273 cblock = code->block;
11274 while (cblock)
11276 /* the assignment statement of a WHERE statement, or the first
11277 statement in where-body-construct of a WHERE construct */
11278 cnext = cblock->next;
11279 while (cnext)
11281 switch (cnext->op)
11283 /* WHERE assignment statement */
11284 case EXEC_ASSIGN:
11285 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
11287 if (cnext->op == EXEC_ASSIGN
11288 && gfc_may_be_finalized (cnext->expr1->ts))
11289 cnext->expr1->must_finalize = 1;
11291 break;
11293 /* WHERE operator assignment statement */
11294 case EXEC_ASSIGN_CALL:
11295 resolve_call (cnext);
11296 if (!cnext->resolved_sym->attr.elemental)
11297 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
11298 &cnext->ext.actual->expr->where);
11299 break;
11301 /* WHERE or WHERE construct is part of a where-body-construct */
11302 case EXEC_WHERE:
11303 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
11304 break;
11306 default:
11307 gfc_error ("Unsupported statement inside WHERE at %L",
11308 &cnext->loc);
11310 /* the next statement within the same where-body-construct */
11311 cnext = cnext->next;
11313 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
11314 cblock = cblock->block;
11319 /* Traverse the FORALL body to check whether the following errors exist:
11320 1. For assignment, check if a many-to-one assignment happens.
11321 2. For WHERE statement, check the WHERE body to see if there is any
11322 many-to-one assignment. */
11324 static void
11325 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
11327 gfc_code *c;
11329 c = code->block->next;
11330 while (c)
11332 switch (c->op)
11334 case EXEC_ASSIGN:
11335 case EXEC_POINTER_ASSIGN:
11336 gfc_resolve_assign_in_forall (c, nvar, var_expr);
11338 if (c->op == EXEC_ASSIGN
11339 && gfc_may_be_finalized (c->expr1->ts))
11340 c->expr1->must_finalize = 1;
11342 break;
11344 case EXEC_ASSIGN_CALL:
11345 resolve_call (c);
11346 break;
11348 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
11349 there is no need to handle it here. */
11350 case EXEC_FORALL:
11351 break;
11352 case EXEC_WHERE:
11353 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
11354 break;
11355 default:
11356 break;
11358 /* The next statement in the FORALL body. */
11359 c = c->next;
11364 /* Counts the number of iterators needed inside a forall construct, including
11365 nested forall constructs. This is used to allocate the needed memory
11366 in gfc_resolve_forall. */
11368 static int
11369 gfc_count_forall_iterators (gfc_code *code)
11371 int max_iters, sub_iters, current_iters;
11372 gfc_forall_iterator *fa;
11374 gcc_assert(code->op == EXEC_FORALL);
11375 max_iters = 0;
11376 current_iters = 0;
11378 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
11379 current_iters ++;
11381 code = code->block->next;
11383 while (code)
11385 if (code->op == EXEC_FORALL)
11387 sub_iters = gfc_count_forall_iterators (code);
11388 if (sub_iters > max_iters)
11389 max_iters = sub_iters;
11391 code = code->next;
11394 return current_iters + max_iters;
11398 /* Given a FORALL construct, first resolve the FORALL iterator, then call
11399 gfc_resolve_forall_body to resolve the FORALL body. */
11401 static void
11402 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
11404 static gfc_expr **var_expr;
11405 static int total_var = 0;
11406 static int nvar = 0;
11407 int i, old_nvar, tmp;
11408 gfc_forall_iterator *fa;
11410 old_nvar = nvar;
11412 if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
11413 return;
11415 /* Start to resolve a FORALL construct */
11416 if (forall_save == 0)
11418 /* Count the total number of FORALL indices in the nested FORALL
11419 construct in order to allocate the VAR_EXPR with proper size. */
11420 total_var = gfc_count_forall_iterators (code);
11422 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
11423 var_expr = XCNEWVEC (gfc_expr *, total_var);
11426 /* The information about FORALL iterator, including FORALL indices start, end
11427 and stride. An outer FORALL indice cannot appear in start, end or stride. */
11428 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
11430 /* Fortran 20008: C738 (R753). */
11431 if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
11433 gfc_error ("FORALL index-name at %L must be a scalar variable "
11434 "of type integer", &fa->var->where);
11435 continue;
11438 /* Check if any outer FORALL index name is the same as the current
11439 one. */
11440 for (i = 0; i < nvar; i++)
11442 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
11443 gfc_error ("An outer FORALL construct already has an index "
11444 "with this name %L", &fa->var->where);
11447 /* Record the current FORALL index. */
11448 var_expr[nvar] = gfc_copy_expr (fa->var);
11450 nvar++;
11452 /* No memory leak. */
11453 gcc_assert (nvar <= total_var);
11456 /* Resolve the FORALL body. */
11457 gfc_resolve_forall_body (code, nvar, var_expr);
11459 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
11460 gfc_resolve_blocks (code->block, ns);
11462 tmp = nvar;
11463 nvar = old_nvar;
11464 /* Free only the VAR_EXPRs allocated in this frame. */
11465 for (i = nvar; i < tmp; i++)
11466 gfc_free_expr (var_expr[i]);
11468 if (nvar == 0)
11470 /* We are in the outermost FORALL construct. */
11471 gcc_assert (forall_save == 0);
11473 /* VAR_EXPR is not needed any more. */
11474 free (var_expr);
11475 total_var = 0;
11480 /* Resolve a BLOCK construct statement. */
11482 static void
11483 resolve_block_construct (gfc_code* code)
11485 gfc_namespace *ns = code->ext.block.ns;
11487 /* For an ASSOCIATE block, the associations (and their targets) are already
11488 resolved during resolve_symbol. Resolve the BLOCK's namespace. */
11489 gfc_resolve (ns);
11493 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
11494 DO code nodes. */
11496 void
11497 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
11499 bool t;
11501 for (; b; b = b->block)
11503 t = gfc_resolve_expr (b->expr1);
11504 if (!gfc_resolve_expr (b->expr2))
11505 t = false;
11507 switch (b->op)
11509 case EXEC_IF:
11510 if (t && b->expr1 != NULL
11511 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
11512 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
11513 &b->expr1->where);
11514 break;
11516 case EXEC_WHERE:
11517 if (t
11518 && b->expr1 != NULL
11519 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
11520 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
11521 &b->expr1->where);
11522 break;
11524 case EXEC_GOTO:
11525 resolve_branch (b->label1, b);
11526 break;
11528 case EXEC_BLOCK:
11529 resolve_block_construct (b);
11530 break;
11532 case EXEC_SELECT:
11533 case EXEC_SELECT_TYPE:
11534 case EXEC_SELECT_RANK:
11535 case EXEC_FORALL:
11536 case EXEC_DO:
11537 case EXEC_DO_WHILE:
11538 case EXEC_DO_CONCURRENT:
11539 case EXEC_CRITICAL:
11540 case EXEC_READ:
11541 case EXEC_WRITE:
11542 case EXEC_IOLENGTH:
11543 case EXEC_WAIT:
11544 break;
11546 case EXEC_OMP_ATOMIC:
11547 case EXEC_OACC_ATOMIC:
11549 /* Verify this before calling gfc_resolve_code, which might
11550 change it. */
11551 gcc_assert (b->op == EXEC_OMP_ATOMIC
11552 || (b->next && b->next->op == EXEC_ASSIGN));
11554 break;
11556 case EXEC_OACC_PARALLEL_LOOP:
11557 case EXEC_OACC_PARALLEL:
11558 case EXEC_OACC_KERNELS_LOOP:
11559 case EXEC_OACC_KERNELS:
11560 case EXEC_OACC_SERIAL_LOOP:
11561 case EXEC_OACC_SERIAL:
11562 case EXEC_OACC_DATA:
11563 case EXEC_OACC_HOST_DATA:
11564 case EXEC_OACC_LOOP:
11565 case EXEC_OACC_UPDATE:
11566 case EXEC_OACC_WAIT:
11567 case EXEC_OACC_CACHE:
11568 case EXEC_OACC_ENTER_DATA:
11569 case EXEC_OACC_EXIT_DATA:
11570 case EXEC_OACC_ROUTINE:
11571 case EXEC_OMP_ALLOCATE:
11572 case EXEC_OMP_ALLOCATORS:
11573 case EXEC_OMP_ASSUME:
11574 case EXEC_OMP_CRITICAL:
11575 case EXEC_OMP_DISTRIBUTE:
11576 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
11577 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
11578 case EXEC_OMP_DISTRIBUTE_SIMD:
11579 case EXEC_OMP_DO:
11580 case EXEC_OMP_DO_SIMD:
11581 case EXEC_OMP_ERROR:
11582 case EXEC_OMP_LOOP:
11583 case EXEC_OMP_MASKED:
11584 case EXEC_OMP_MASKED_TASKLOOP:
11585 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
11586 case EXEC_OMP_MASTER:
11587 case EXEC_OMP_MASTER_TASKLOOP:
11588 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
11589 case EXEC_OMP_ORDERED:
11590 case EXEC_OMP_PARALLEL:
11591 case EXEC_OMP_PARALLEL_DO:
11592 case EXEC_OMP_PARALLEL_DO_SIMD:
11593 case EXEC_OMP_PARALLEL_LOOP:
11594 case EXEC_OMP_PARALLEL_MASKED:
11595 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
11596 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
11597 case EXEC_OMP_PARALLEL_MASTER:
11598 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
11599 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
11600 case EXEC_OMP_PARALLEL_SECTIONS:
11601 case EXEC_OMP_PARALLEL_WORKSHARE:
11602 case EXEC_OMP_SECTIONS:
11603 case EXEC_OMP_SIMD:
11604 case EXEC_OMP_SCOPE:
11605 case EXEC_OMP_SINGLE:
11606 case EXEC_OMP_TARGET:
11607 case EXEC_OMP_TARGET_DATA:
11608 case EXEC_OMP_TARGET_ENTER_DATA:
11609 case EXEC_OMP_TARGET_EXIT_DATA:
11610 case EXEC_OMP_TARGET_PARALLEL:
11611 case EXEC_OMP_TARGET_PARALLEL_DO:
11612 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
11613 case EXEC_OMP_TARGET_PARALLEL_LOOP:
11614 case EXEC_OMP_TARGET_SIMD:
11615 case EXEC_OMP_TARGET_TEAMS:
11616 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
11617 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
11618 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11619 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
11620 case EXEC_OMP_TARGET_TEAMS_LOOP:
11621 case EXEC_OMP_TARGET_UPDATE:
11622 case EXEC_OMP_TASK:
11623 case EXEC_OMP_TASKGROUP:
11624 case EXEC_OMP_TASKLOOP:
11625 case EXEC_OMP_TASKLOOP_SIMD:
11626 case EXEC_OMP_TASKWAIT:
11627 case EXEC_OMP_TASKYIELD:
11628 case EXEC_OMP_TEAMS:
11629 case EXEC_OMP_TEAMS_DISTRIBUTE:
11630 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
11631 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11632 case EXEC_OMP_TEAMS_LOOP:
11633 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
11634 case EXEC_OMP_TILE:
11635 case EXEC_OMP_UNROLL:
11636 case EXEC_OMP_WORKSHARE:
11637 break;
11639 default:
11640 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
11643 gfc_resolve_code (b->next, ns);
11647 bool
11648 caf_possible_reallocate (gfc_expr *e)
11650 symbol_attribute caf_attr;
11651 gfc_ref *last_arr_ref = nullptr;
11653 caf_attr = gfc_caf_attr (e);
11654 if (!caf_attr.codimension || !caf_attr.allocatable || !caf_attr.dimension)
11655 return false;
11657 /* Only full array refs can indicate a needed reallocation. */
11658 for (gfc_ref *ref = e->ref; ref; ref = ref->next)
11659 if (ref->type == REF_ARRAY && ref->u.ar.dimen)
11660 last_arr_ref = ref;
11662 return last_arr_ref && last_arr_ref->u.ar.type == AR_FULL;
11665 /* Does everything to resolve an ordinary assignment. Returns true
11666 if this is an interface assignment. */
11667 static bool
11668 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
11670 bool rval = false;
11671 gfc_expr *lhs;
11672 gfc_expr *rhs;
11673 int n;
11674 gfc_ref *ref;
11675 symbol_attribute attr;
11677 if (gfc_extend_assign (code, ns))
11679 gfc_expr** rhsptr;
11681 if (code->op == EXEC_ASSIGN_CALL)
11683 lhs = code->ext.actual->expr;
11684 rhsptr = &code->ext.actual->next->expr;
11686 else
11688 gfc_actual_arglist* args;
11689 gfc_typebound_proc* tbp;
11691 gcc_assert (code->op == EXEC_COMPCALL);
11693 args = code->expr1->value.compcall.actual;
11694 lhs = args->expr;
11695 rhsptr = &args->next->expr;
11697 tbp = code->expr1->value.compcall.tbp;
11698 gcc_assert (!tbp->is_generic);
11701 /* Make a temporary rhs when there is a default initializer
11702 and rhs is the same symbol as the lhs. */
11703 if ((*rhsptr)->expr_type == EXPR_VARIABLE
11704 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
11705 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
11706 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
11707 *rhsptr = gfc_get_parentheses (*rhsptr);
11709 return true;
11712 lhs = code->expr1;
11713 rhs = code->expr2;
11715 if ((lhs->symtree->n.sym->ts.type == BT_DERIVED
11716 || lhs->symtree->n.sym->ts.type == BT_CLASS)
11717 && !lhs->symtree->n.sym->attr.proc_pointer
11718 && gfc_expr_attr (lhs).proc_pointer)
11720 gfc_error ("Variable in the ordinary assignment at %L is a procedure "
11721 "pointer component",
11722 &lhs->where);
11723 return false;
11726 if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
11727 && rhs->ts.type == BT_CHARACTER
11728 && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
11730 /* Use of -fdec-char-conversions allows assignment of character data
11731 to non-character variables. This not permitted for nonconstant
11732 strings. */
11733 gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
11734 gfc_typename (lhs), &rhs->where);
11735 return false;
11738 if (flag_unsigned && gfc_invalid_unsigned_ops (lhs, rhs))
11740 gfc_error ("Cannot assign %s to %s at %L", gfc_typename (rhs),
11741 gfc_typename (lhs), &rhs->where);
11742 return false;
11745 /* Handle the case of a BOZ literal on the RHS. */
11746 if (rhs->ts.type == BT_BOZ)
11748 if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
11749 "statement value nor an actual argument of "
11750 "INT/REAL/DBLE/CMPLX intrinsic subprogram",
11751 &rhs->where))
11752 return false;
11754 switch (lhs->ts.type)
11756 case BT_INTEGER:
11757 if (!gfc_boz2int (rhs, lhs->ts.kind))
11758 return false;
11759 break;
11760 case BT_REAL:
11761 if (!gfc_boz2real (rhs, lhs->ts.kind))
11762 return false;
11763 break;
11764 default:
11765 gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
11766 return false;
11770 if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
11772 HOST_WIDE_INT llen = 0, rlen = 0;
11773 if (lhs->ts.u.cl != NULL
11774 && lhs->ts.u.cl->length != NULL
11775 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11776 llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
11778 if (rhs->expr_type == EXPR_CONSTANT)
11779 rlen = rhs->value.character.length;
11781 else if (rhs->ts.u.cl != NULL
11782 && rhs->ts.u.cl->length != NULL
11783 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11784 rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
11786 if (rlen && llen && rlen > llen)
11787 gfc_warning_now (OPT_Wcharacter_truncation,
11788 "CHARACTER expression will be truncated "
11789 "in assignment (%wd/%wd) at %L",
11790 llen, rlen, &code->loc);
11793 /* Ensure that a vector index expression for the lvalue is evaluated
11794 to a temporary if the lvalue symbol is referenced in it. */
11795 if (lhs->rank)
11797 for (ref = lhs->ref; ref; ref= ref->next)
11798 if (ref->type == REF_ARRAY)
11800 for (n = 0; n < ref->u.ar.dimen; n++)
11801 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
11802 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
11803 ref->u.ar.start[n]))
11804 ref->u.ar.start[n]
11805 = gfc_get_parentheses (ref->u.ar.start[n]);
11809 if (gfc_pure (NULL))
11811 if (lhs->ts.type == BT_DERIVED
11812 && lhs->expr_type == EXPR_VARIABLE
11813 && lhs->ts.u.derived->attr.pointer_comp
11814 && rhs->expr_type == EXPR_VARIABLE
11815 && (gfc_impure_variable (rhs->symtree->n.sym)
11816 || gfc_is_coindexed (rhs)))
11818 /* F2008, C1283. */
11819 if (gfc_is_coindexed (rhs))
11820 gfc_error ("Coindexed expression at %L is assigned to "
11821 "a derived type variable with a POINTER "
11822 "component in a PURE procedure",
11823 &rhs->where);
11824 else
11825 /* F2008, C1283 (4). */
11826 gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
11827 "shall not be used as the expr at %L of an intrinsic "
11828 "assignment statement in which the variable is of a "
11829 "derived type if the derived type has a pointer "
11830 "component at any level of component selection.",
11831 &rhs->where);
11832 return rval;
11835 /* Fortran 2008, C1283. */
11836 if (gfc_is_coindexed (lhs))
11838 gfc_error ("Assignment to coindexed variable at %L in a PURE "
11839 "procedure", &rhs->where);
11840 return rval;
11844 if (gfc_implicit_pure (NULL))
11846 if (lhs->expr_type == EXPR_VARIABLE
11847 && lhs->symtree->n.sym != gfc_current_ns->proc_name
11848 && lhs->symtree->n.sym->ns != gfc_current_ns)
11849 gfc_unset_implicit_pure (NULL);
11851 if (lhs->ts.type == BT_DERIVED
11852 && lhs->expr_type == EXPR_VARIABLE
11853 && lhs->ts.u.derived->attr.pointer_comp
11854 && rhs->expr_type == EXPR_VARIABLE
11855 && (gfc_impure_variable (rhs->symtree->n.sym)
11856 || gfc_is_coindexed (rhs)))
11857 gfc_unset_implicit_pure (NULL);
11859 /* Fortran 2008, C1283. */
11860 if (gfc_is_coindexed (lhs))
11861 gfc_unset_implicit_pure (NULL);
11864 /* F2008, 7.2.1.2. */
11865 attr = gfc_expr_attr (lhs);
11866 if (lhs->ts.type == BT_CLASS && attr.allocatable)
11868 if (attr.codimension)
11870 gfc_error ("Assignment to polymorphic coarray at %L is not "
11871 "permitted", &lhs->where);
11872 return false;
11874 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
11875 "polymorphic variable at %L", &lhs->where))
11876 return false;
11877 if (!flag_realloc_lhs)
11879 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
11880 "requires %<-frealloc-lhs%>", &lhs->where);
11881 return false;
11884 else if (lhs->ts.type == BT_CLASS)
11886 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
11887 "assignment at %L - check that there is a matching specific "
11888 "subroutine for %<=%> operator", &lhs->where);
11889 return false;
11892 bool lhs_coindexed = gfc_is_coindexed (lhs);
11894 /* F2008, Section 7.2.1.2. */
11895 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
11897 gfc_error ("Coindexed variable must not have an allocatable ultimate "
11898 "component in assignment at %L", &lhs->where);
11899 return false;
11902 /* Assign the 'data' of a class object to a derived type. */
11903 if (lhs->ts.type == BT_DERIVED
11904 && rhs->ts.type == BT_CLASS
11905 && rhs->expr_type != EXPR_ARRAY)
11906 gfc_add_data_component (rhs);
11908 /* Make sure there is a vtable and, in particular, a _copy for the
11909 rhs type. */
11910 if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
11911 gfc_find_vtab (&rhs->ts);
11913 bool caf_convert_to_send = flag_coarray == GFC_FCOARRAY_LIB
11914 && (lhs_coindexed
11915 || caf_possible_reallocate (lhs)
11916 || (code->expr2->expr_type == EXPR_FUNCTION
11917 && code->expr2->value.function.isym
11918 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
11919 && (code->expr1->rank == 0 || code->expr2->rank != 0)
11920 && !gfc_expr_attr (rhs).allocatable
11921 && !gfc_has_vector_subscript (rhs)));
11923 gfc_check_assign (lhs, rhs, 1, !caf_convert_to_send);
11925 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
11926 Additionally, insert this code when the RHS is a CAF as we then use the
11927 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
11928 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
11929 noncoindexed array and the RHS is a coindexed scalar, use the normal code
11930 path. */
11931 if (caf_convert_to_send)
11933 if (code->expr2->expr_type == EXPR_FUNCTION
11934 && code->expr2->value.function.isym
11935 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
11936 remove_caf_get_intrinsic (code->expr2);
11937 code->op = EXEC_CALL;
11938 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
11939 code->resolved_sym = code->symtree->n.sym;
11940 code->resolved_sym->attr.flavor = FL_PROCEDURE;
11941 code->resolved_sym->attr.intrinsic = 1;
11942 code->resolved_sym->attr.subroutine = 1;
11943 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
11944 gfc_commit_symbol (code->resolved_sym);
11945 code->ext.actual = gfc_get_actual_arglist ();
11946 code->ext.actual->expr = lhs;
11947 code->ext.actual->next = gfc_get_actual_arglist ();
11948 code->ext.actual->next->expr = rhs;
11949 code->expr1 = NULL;
11950 code->expr2 = NULL;
11953 return false;
11957 /* Add a component reference onto an expression. */
11959 static void
11960 add_comp_ref (gfc_expr *e, gfc_component *c)
11962 gfc_ref **ref;
11963 ref = &(e->ref);
11964 while (*ref)
11965 ref = &((*ref)->next);
11966 *ref = gfc_get_ref ();
11967 (*ref)->type = REF_COMPONENT;
11968 (*ref)->u.c.sym = e->ts.u.derived;
11969 (*ref)->u.c.component = c;
11970 e->ts = c->ts;
11972 /* Add a full array ref, as necessary. */
11973 if (c->as)
11975 gfc_add_full_array_ref (e, c->as);
11976 e->rank = c->as->rank;
11977 e->corank = c->as->corank;
11982 /* Build an assignment. Keep the argument 'op' for future use, so that
11983 pointer assignments can be made. */
11985 static gfc_code *
11986 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
11987 gfc_component *comp1, gfc_component *comp2, locus loc)
11989 gfc_code *this_code;
11991 this_code = gfc_get_code (op);
11992 this_code->next = NULL;
11993 this_code->expr1 = gfc_copy_expr (expr1);
11994 this_code->expr2 = gfc_copy_expr (expr2);
11995 this_code->loc = loc;
11996 if (comp1 && comp2)
11998 add_comp_ref (this_code->expr1, comp1);
11999 add_comp_ref (this_code->expr2, comp2);
12002 return this_code;
12006 /* Makes a temporary variable expression based on the characteristics of
12007 a given variable expression. */
12009 static gfc_expr*
12010 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
12012 static int serial = 0;
12013 char name[GFC_MAX_SYMBOL_LEN];
12014 gfc_symtree *tmp;
12015 gfc_array_spec *as;
12016 gfc_array_ref *aref;
12017 gfc_ref *ref;
12019 sprintf (name, GFC_PREFIX("DA%d"), serial++);
12020 gfc_get_sym_tree (name, ns, &tmp, false);
12021 gfc_add_type (tmp->n.sym, &e->ts, NULL);
12023 if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
12024 tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
12025 NULL,
12026 e->value.character.length);
12028 as = NULL;
12029 ref = NULL;
12030 aref = NULL;
12032 /* Obtain the arrayspec for the temporary. */
12033 if (e->rank && e->expr_type != EXPR_ARRAY
12034 && e->expr_type != EXPR_FUNCTION
12035 && e->expr_type != EXPR_OP)
12037 aref = gfc_find_array_ref (e);
12038 if (e->expr_type == EXPR_VARIABLE
12039 && e->symtree->n.sym->as == aref->as)
12040 as = aref->as;
12041 else
12043 for (ref = e->ref; ref; ref = ref->next)
12044 if (ref->type == REF_COMPONENT
12045 && ref->u.c.component->as == aref->as)
12047 as = aref->as;
12048 break;
12053 /* Add the attributes and the arrayspec to the temporary. */
12054 tmp->n.sym->attr = gfc_expr_attr (e);
12055 tmp->n.sym->attr.function = 0;
12056 tmp->n.sym->attr.proc_pointer = 0;
12057 tmp->n.sym->attr.result = 0;
12058 tmp->n.sym->attr.flavor = FL_VARIABLE;
12059 tmp->n.sym->attr.dummy = 0;
12060 tmp->n.sym->attr.use_assoc = 0;
12061 tmp->n.sym->attr.intent = INTENT_UNKNOWN;
12064 if (as)
12066 tmp->n.sym->as = gfc_copy_array_spec (as);
12067 if (!ref)
12068 ref = e->ref;
12069 if (as->type == AS_DEFERRED)
12070 tmp->n.sym->attr.allocatable = 1;
12072 else if ((e->rank || e->corank)
12073 && (e->expr_type == EXPR_ARRAY || e->expr_type == EXPR_FUNCTION
12074 || e->expr_type == EXPR_OP))
12076 tmp->n.sym->as = gfc_get_array_spec ();
12077 tmp->n.sym->as->type = AS_DEFERRED;
12078 tmp->n.sym->as->rank = e->rank;
12079 tmp->n.sym->as->corank = e->corank;
12080 tmp->n.sym->attr.allocatable = 1;
12081 tmp->n.sym->attr.dimension = e->rank ? 1 : 0;
12082 tmp->n.sym->attr.codimension = e->corank ? 1 : 0;
12084 else
12085 tmp->n.sym->attr.dimension = 0;
12087 gfc_set_sym_referenced (tmp->n.sym);
12088 gfc_commit_symbol (tmp->n.sym);
12089 e = gfc_lval_expr_from_sym (tmp->n.sym);
12091 /* Should the lhs be a section, use its array ref for the
12092 temporary expression. */
12093 if (aref && aref->type != AR_FULL)
12095 gfc_free_ref_list (e->ref);
12096 e->ref = gfc_copy_ref (ref);
12098 return e;
12102 /* Add one line of code to the code chain, making sure that 'head' and
12103 'tail' are appropriately updated. */
12105 static void
12106 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
12108 gcc_assert (this_code);
12109 if (*head == NULL)
12110 *head = *tail = *this_code;
12111 else
12112 *tail = gfc_append_code (*tail, *this_code);
12113 *this_code = NULL;
12117 /* Generate a final call from a variable expression */
12119 static void
12120 generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
12122 gfc_code *this_code;
12123 gfc_expr *final_expr = NULL;
12124 gfc_expr *size_expr;
12125 gfc_expr *fini_coarray;
12127 gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
12128 if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
12129 return;
12131 /* Now generate the finalizer call. */
12132 this_code = gfc_get_code (EXEC_CALL);
12133 this_code->symtree = final_expr->symtree;
12134 this_code->resolved_sym = final_expr->symtree->n.sym;
12136 //* Expression to be finalized */
12137 this_code->ext.actual = gfc_get_actual_arglist ();
12138 this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
12140 /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE. */
12141 this_code->ext.actual->next = gfc_get_actual_arglist ();
12142 size_expr = gfc_get_expr ();
12143 size_expr->where = gfc_current_locus;
12144 size_expr->expr_type = EXPR_OP;
12145 size_expr->value.op.op = INTRINSIC_DIVIDE;
12146 size_expr->value.op.op1
12147 = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
12148 "storage_size", gfc_current_locus, 2,
12149 gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
12150 gfc_get_int_expr (gfc_index_integer_kind,
12151 NULL, 0));
12152 size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
12153 gfc_character_storage_size);
12154 size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
12155 size_expr->ts = size_expr->value.op.op1->ts;
12156 this_code->ext.actual->next->expr = size_expr;
12158 /* fini_coarray */
12159 this_code->ext.actual->next->next = gfc_get_actual_arglist ();
12160 fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
12161 &tmp_expr->where);
12162 fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
12163 this_code->ext.actual->next->next->expr = fini_coarray;
12165 add_code_to_chain (&this_code, head, tail);
12169 /* Counts the potential number of part array references that would
12170 result from resolution of typebound defined assignments. */
12173 static int
12174 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
12176 gfc_component *c;
12177 int c_depth = 0, t_depth;
12179 for (c= derived->components; c; c = c->next)
12181 if ((!gfc_bt_struct (c->ts.type)
12182 || c->attr.pointer
12183 || c->attr.allocatable
12184 || c->attr.proc_pointer_comp
12185 || c->attr.class_pointer
12186 || c->attr.proc_pointer)
12187 && !c->attr.defined_assign_comp)
12188 continue;
12190 if (c->as && c_depth == 0)
12191 c_depth = 1;
12193 if (c->ts.u.derived->attr.defined_assign_comp)
12194 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
12195 c->as ? 1 : 0);
12196 else
12197 t_depth = 0;
12199 c_depth = t_depth > c_depth ? t_depth : c_depth;
12201 return depth + c_depth;
12205 /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
12206 "An intrinsic assignment where the variable is of derived type is performed
12207 as if each component of the variable were assigned from the corresponding
12208 component of expr using pointer assignment (10.2.2) for each pointer
12209 component, defined assignment for each nonpointer nonallocatable component
12210 of a type that has a type-bound defined assignment consistent with the
12211 component, intrinsic assignment for each other nonpointer nonallocatable
12212 component, and intrinsic assignment for each allocated coarray component.
12213 For unallocated coarray components, the corresponding component of the
12214 variable shall be unallocated. For a noncoarray allocatable component the
12215 following sequence of operations is applied.
12216 (1) If the component of the variable is allocated, it is deallocated.
12217 (2) If the component of the value of expr is allocated, the
12218 corresponding component of the variable is allocated with the same
12219 dynamic type and type parameters as the component of the value of
12220 expr. If it is an array, it is allocated with the same bounds. The
12221 value of the component of the value of expr is then assigned to the
12222 corresponding component of the variable using defined assignment if
12223 the declared type of the component has a type-bound defined
12224 assignment consistent with the component, and intrinsic assignment
12225 for the dynamic type of that component otherwise."
12227 The pointer assignments are taken care of by the intrinsic assignment of the
12228 structure itself. This function recursively adds defined assignments where
12229 required. The recursion is accomplished by calling gfc_resolve_code.
12231 When the lhs in a defined assignment has intent INOUT or is intent OUT
12232 and the component of 'var' is finalizable, we need a temporary for the
12233 lhs. In pseudo-code for an assignment var = expr:
12235 ! Confine finalization of temporaries, as far as possible.
12236 Enclose the code for the assignment in a block
12237 ! Only call function 'expr' once.
12238 #if ('expr is not a constant or an variable)
12239 temp_expr = expr
12240 expr = temp_x
12241 ! Do the intrinsic assignment
12242 #if typeof ('var') has a typebound final subroutine
12243 finalize (var)
12244 var = expr
12245 ! Now do the component assignments
12246 #do over derived type components [%cmp]
12247 #if (cmp is a pointer of any kind)
12248 continue
12249 build the assignment
12250 resolve the code
12251 #if the code is a typebound assignment
12252 #if (arg1 is INOUT or finalizable OUT && !t1)
12253 t1 = var
12254 arg1 = t1
12255 deal with allocatation or not of var and this component
12256 #elseif the code is an assignment by itself
12257 #if this component does not need finalization
12258 delete code and continue
12259 #else
12260 remove the leading assignment
12261 #endif
12262 commit the code
12263 #if (t1 and (arg1 is INOUT or finalizable OUT))
12264 var%cmp = t1%cmp
12265 #enddo
12266 put all code chunks involving t1 to the top of the generated code
12267 insert the generated block in place of the original code
12270 static bool
12271 is_finalizable_type (gfc_typespec ts)
12273 gfc_component *c;
12275 if (ts.type != BT_DERIVED)
12276 return false;
12278 /* (1) Check for FINAL subroutines. */
12279 if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
12280 return true;
12282 /* (2) Check for components of finalizable type. */
12283 for (c = ts.u.derived->components; c; c = c->next)
12284 if (c->ts.type == BT_DERIVED
12285 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
12286 && c->ts.u.derived->f2k_derived
12287 && c->ts.u.derived->f2k_derived->finalizers)
12288 return true;
12290 return false;
12293 /* The temporary assignments have to be put on top of the additional
12294 code to avoid the result being changed by the intrinsic assignment.
12296 static int component_assignment_level = 0;
12297 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
12298 static bool finalizable_comp;
12300 static void
12301 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
12303 gfc_component *comp1, *comp2;
12304 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
12305 gfc_code *tmp_code = NULL;
12306 gfc_expr *t1 = NULL;
12307 gfc_expr *tmp_expr = NULL;
12308 int error_count, depth;
12309 bool finalizable_lhs;
12311 gfc_get_errors (NULL, &error_count);
12313 /* Filter out continuing processing after an error. */
12314 if (error_count
12315 || (*code)->expr1->ts.type != BT_DERIVED
12316 || (*code)->expr2->ts.type != BT_DERIVED)
12317 return;
12319 /* TODO: Handle more than one part array reference in assignments. */
12320 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
12321 (*code)->expr1->rank ? 1 : 0);
12322 if (depth > 1)
12324 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
12325 "done because multiple part array references would "
12326 "occur in intermediate expressions.", &(*code)->loc);
12327 return;
12330 if (!component_assignment_level)
12331 finalizable_comp = true;
12333 /* Build a block so that function result temporaries are finalized
12334 locally on exiting the rather than enclosing scope. */
12335 if (!component_assignment_level)
12337 ns = gfc_build_block_ns (ns);
12338 tmp_code = gfc_get_code (EXEC_NOP);
12339 *tmp_code = **code;
12340 tmp_code->next = NULL;
12341 (*code)->op = EXEC_BLOCK;
12342 (*code)->ext.block.ns = ns;
12343 (*code)->ext.block.assoc = NULL;
12344 (*code)->expr1 = (*code)->expr2 = NULL;
12345 ns->code = tmp_code;
12346 code = &ns->code;
12349 component_assignment_level++;
12351 finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
12353 /* Create a temporary so that functions get called only once. */
12354 if ((*code)->expr2->expr_type != EXPR_VARIABLE
12355 && (*code)->expr2->expr_type != EXPR_CONSTANT)
12357 /* Assign the rhs to the temporary. */
12358 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
12359 this_code = build_assignment (EXEC_ASSIGN,
12360 tmp_expr, (*code)->expr2,
12361 NULL, NULL, (*code)->loc);
12362 this_code->expr2->must_finalize = 1;
12363 /* Add the code and substitute the rhs expression. */
12364 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
12365 gfc_free_expr ((*code)->expr2);
12366 (*code)->expr2 = tmp_expr;
12369 /* Do the intrinsic assignment. This is not needed if the lhs is one
12370 of the temporaries generated here, since the intrinsic assignment
12371 to the final result already does this. */
12372 if ((*code)->expr1->symtree->n.sym->name[2] != '.')
12374 if (finalizable_lhs)
12375 (*code)->expr1->must_finalize = 1;
12376 this_code = build_assignment (EXEC_ASSIGN,
12377 (*code)->expr1, (*code)->expr2,
12378 NULL, NULL, (*code)->loc);
12379 add_code_to_chain (&this_code, &head, &tail);
12382 comp1 = (*code)->expr1->ts.u.derived->components;
12383 comp2 = (*code)->expr2->ts.u.derived->components;
12385 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
12387 bool inout = false;
12388 bool finalizable_out = false;
12390 /* The intrinsic assignment does the right thing for pointers
12391 of all kinds and allocatable components. */
12392 if (!gfc_bt_struct (comp1->ts.type)
12393 || comp1->attr.pointer
12394 || comp1->attr.allocatable
12395 || comp1->attr.proc_pointer_comp
12396 || comp1->attr.class_pointer
12397 || comp1->attr.proc_pointer)
12398 continue;
12400 finalizable_comp = is_finalizable_type (comp1->ts)
12401 && !finalizable_lhs;
12403 /* Make an assignment for this component. */
12404 this_code = build_assignment (EXEC_ASSIGN,
12405 (*code)->expr1, (*code)->expr2,
12406 comp1, comp2, (*code)->loc);
12408 /* Convert the assignment if there is a defined assignment for
12409 this type. Otherwise, using the call from gfc_resolve_code,
12410 recurse into its components. */
12411 gfc_resolve_code (this_code, ns);
12413 if (this_code->op == EXEC_ASSIGN_CALL)
12415 gfc_formal_arglist *dummy_args;
12416 gfc_symbol *rsym;
12417 /* Check that there is a typebound defined assignment. If not,
12418 then this must be a module defined assignment. We cannot
12419 use the defined_assign_comp attribute here because it must
12420 be this derived type that has the defined assignment and not
12421 a parent type. */
12422 if (!(comp1->ts.u.derived->f2k_derived
12423 && comp1->ts.u.derived->f2k_derived
12424 ->tb_op[INTRINSIC_ASSIGN]))
12426 gfc_free_statements (this_code);
12427 this_code = NULL;
12428 continue;
12431 /* If the first argument of the subroutine has intent INOUT
12432 a temporary must be generated and used instead. */
12433 rsym = this_code->resolved_sym;
12434 dummy_args = gfc_sym_get_dummy_args (rsym);
12435 finalizable_out = gfc_may_be_finalized (comp1->ts)
12436 && dummy_args
12437 && dummy_args->sym->attr.intent == INTENT_OUT;
12438 inout = dummy_args
12439 && dummy_args->sym->attr.intent == INTENT_INOUT;
12440 if ((inout || finalizable_out)
12441 && !comp1->attr.allocatable)
12443 gfc_code *temp_code;
12444 inout = true;
12446 /* Build the temporary required for the assignment and put
12447 it at the head of the generated code. */
12448 if (!t1)
12450 gfc_namespace *tmp_ns = ns;
12451 if (ns->parent && gfc_may_be_finalized (comp1->ts))
12452 tmp_ns = (*code)->expr1->symtree->n.sym->ns;
12453 t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
12454 t1->symtree->n.sym->attr.artificial = 1;
12455 temp_code = build_assignment (EXEC_ASSIGN,
12456 t1, (*code)->expr1,
12457 NULL, NULL, (*code)->loc);
12459 /* For allocatable LHS, check whether it is allocated. Note
12460 that allocatable components with defined assignment are
12461 not yet support. See PR 57696. */
12462 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
12464 gfc_code *block;
12465 gfc_expr *e =
12466 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
12467 block = gfc_get_code (EXEC_IF);
12468 block->block = gfc_get_code (EXEC_IF);
12469 block->block->expr1
12470 = gfc_build_intrinsic_call (ns,
12471 GFC_ISYM_ALLOCATED, "allocated",
12472 (*code)->loc, 1, e);
12473 block->block->next = temp_code;
12474 temp_code = block;
12476 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
12479 /* Replace the first actual arg with the component of the
12480 temporary. */
12481 gfc_free_expr (this_code->ext.actual->expr);
12482 this_code->ext.actual->expr = gfc_copy_expr (t1);
12483 add_comp_ref (this_code->ext.actual->expr, comp1);
12485 /* If the LHS variable is allocatable and wasn't allocated and
12486 the temporary is allocatable, pointer assign the address of
12487 the freshly allocated LHS to the temporary. */
12488 if ((*code)->expr1->symtree->n.sym->attr.allocatable
12489 && gfc_expr_attr ((*code)->expr1).allocatable)
12491 gfc_code *block;
12492 gfc_expr *cond;
12494 cond = gfc_get_expr ();
12495 cond->ts.type = BT_LOGICAL;
12496 cond->ts.kind = gfc_default_logical_kind;
12497 cond->expr_type = EXPR_OP;
12498 cond->where = (*code)->loc;
12499 cond->value.op.op = INTRINSIC_NOT;
12500 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
12501 GFC_ISYM_ALLOCATED, "allocated",
12502 (*code)->loc, 1, gfc_copy_expr (t1));
12503 block = gfc_get_code (EXEC_IF);
12504 block->block = gfc_get_code (EXEC_IF);
12505 block->block->expr1 = cond;
12506 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
12507 t1, (*code)->expr1,
12508 NULL, NULL, (*code)->loc);
12509 add_code_to_chain (&block, &head, &tail);
12513 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
12515 /* Don't add intrinsic assignments since they are already
12516 effected by the intrinsic assignment of the structure, unless
12517 finalization is required. */
12518 if (finalizable_comp)
12519 this_code->expr1->must_finalize = 1;
12520 else
12522 gfc_free_statements (this_code);
12523 this_code = NULL;
12524 continue;
12527 else
12529 /* Resolution has expanded an assignment of a derived type with
12530 defined assigned components. Remove the redundant, leading
12531 assignment. */
12532 gcc_assert (this_code->op == EXEC_ASSIGN);
12533 gfc_code *tmp = this_code;
12534 this_code = this_code->next;
12535 tmp->next = NULL;
12536 gfc_free_statements (tmp);
12539 add_code_to_chain (&this_code, &head, &tail);
12541 if (t1 && (inout || finalizable_out))
12543 /* Transfer the value to the final result. */
12544 this_code = build_assignment (EXEC_ASSIGN,
12545 (*code)->expr1, t1,
12546 comp1, comp2, (*code)->loc);
12547 this_code->expr1->must_finalize = 0;
12548 add_code_to_chain (&this_code, &head, &tail);
12552 /* Put the temporary assignments at the top of the generated code. */
12553 if (tmp_head && component_assignment_level == 1)
12555 gfc_append_code (tmp_head, head);
12556 head = tmp_head;
12557 tmp_head = tmp_tail = NULL;
12560 /* If we did a pointer assignment - thus, we need to ensure that the LHS is
12561 not accidentally deallocated. Hence, nullify t1. */
12562 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
12563 && gfc_expr_attr ((*code)->expr1).allocatable)
12565 gfc_code *block;
12566 gfc_expr *cond;
12567 gfc_expr *e;
12569 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
12570 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
12571 (*code)->loc, 2, gfc_copy_expr (t1), e);
12572 block = gfc_get_code (EXEC_IF);
12573 block->block = gfc_get_code (EXEC_IF);
12574 block->block->expr1 = cond;
12575 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
12576 t1, gfc_get_null_expr (&(*code)->loc),
12577 NULL, NULL, (*code)->loc);
12578 gfc_append_code (tail, block);
12579 tail = block;
12582 component_assignment_level--;
12584 /* Make an explicit final call for the function result. */
12585 if (tmp_expr)
12586 generate_final_call (tmp_expr, &head, &tail);
12588 if (tmp_code)
12590 ns->code = head;
12591 return;
12594 /* Now attach the remaining code chain to the input code. Step on
12595 to the end of the new code since resolution is complete. */
12596 gcc_assert ((*code)->op == EXEC_ASSIGN);
12597 tail->next = (*code)->next;
12598 /* Overwrite 'code' because this would place the intrinsic assignment
12599 before the temporary for the lhs is created. */
12600 gfc_free_expr ((*code)->expr1);
12601 gfc_free_expr ((*code)->expr2);
12602 **code = *head;
12603 if (head != tail)
12604 free (head);
12605 *code = tail;
12609 /* F2008: Pointer function assignments are of the form:
12610 ptr_fcn (args) = expr
12611 This function breaks these assignments into two statements:
12612 temporary_pointer => ptr_fcn(args)
12613 temporary_pointer = expr */
12615 static bool
12616 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
12618 gfc_expr *tmp_ptr_expr;
12619 gfc_code *this_code;
12620 gfc_component *comp;
12621 gfc_symbol *s;
12623 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
12624 return false;
12626 /* Even if standard does not support this feature, continue to build
12627 the two statements to avoid upsetting frontend_passes.c. */
12628 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
12629 "%L", &(*code)->loc);
12631 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
12633 if (comp)
12634 s = comp->ts.interface;
12635 else
12636 s = (*code)->expr1->symtree->n.sym;
12638 if (s == NULL || !s->result->attr.pointer)
12640 gfc_error ("The function result on the lhs of the assignment at "
12641 "%L must have the pointer attribute.",
12642 &(*code)->expr1->where);
12643 (*code)->op = EXEC_NOP;
12644 return false;
12647 tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
12649 /* get_temp_from_expression is set up for ordinary assignments. To that
12650 end, where array bounds are not known, arrays are made allocatable.
12651 Change the temporary to a pointer here. */
12652 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
12653 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
12654 tmp_ptr_expr->where = (*code)->loc;
12656 this_code = build_assignment (EXEC_ASSIGN,
12657 tmp_ptr_expr, (*code)->expr2,
12658 NULL, NULL, (*code)->loc);
12659 this_code->next = (*code)->next;
12660 (*code)->next = this_code;
12661 (*code)->op = EXEC_POINTER_ASSIGN;
12662 (*code)->expr2 = (*code)->expr1;
12663 (*code)->expr1 = tmp_ptr_expr;
12665 return true;
12669 /* Deferred character length assignments from an operator expression
12670 require a temporary because the character length of the lhs can
12671 change in the course of the assignment. */
12673 static bool
12674 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
12676 gfc_expr *tmp_expr;
12677 gfc_code *this_code;
12679 if (!((*code)->expr1->ts.type == BT_CHARACTER
12680 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
12681 && (*code)->expr2->ts.type == BT_CHARACTER
12682 && (*code)->expr2->expr_type == EXPR_OP))
12683 return false;
12685 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
12686 return false;
12688 if (gfc_expr_attr ((*code)->expr1).pointer)
12689 return false;
12691 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
12692 tmp_expr->where = (*code)->loc;
12694 /* A new charlen is required to ensure that the variable string
12695 length is different to that of the original lhs. */
12696 tmp_expr->ts.u.cl = gfc_get_charlen();
12697 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
12698 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
12699 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
12701 tmp_expr->symtree->n.sym->ts.deferred = 1;
12703 this_code = build_assignment (EXEC_ASSIGN,
12704 (*code)->expr1,
12705 gfc_copy_expr (tmp_expr),
12706 NULL, NULL, (*code)->loc);
12708 (*code)->expr1 = tmp_expr;
12710 this_code->next = (*code)->next;
12711 (*code)->next = this_code;
12713 return true;
12717 static bool
12718 check_team (gfc_expr *team, const char *intrinsic)
12720 if (team->rank != 0
12721 || team->ts.type != BT_DERIVED
12722 || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
12723 || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
12725 gfc_error ("TEAM argument to %qs at %L must be a scalar expression "
12726 "of type TEAM_TYPE", intrinsic, &team->where);
12727 return false;
12730 return true;
12734 /* Given a block of code, recursively resolve everything pointed to by this
12735 code block. */
12737 void
12738 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
12740 int omp_workshare_save;
12741 int forall_save, do_concurrent_save;
12742 code_stack frame;
12743 bool t;
12745 frame.prev = cs_base;
12746 frame.head = code;
12747 cs_base = &frame;
12749 find_reachable_labels (code);
12751 for (; code; code = code->next)
12753 frame.current = code;
12754 forall_save = forall_flag;
12755 do_concurrent_save = gfc_do_concurrent_flag;
12757 if (code->op == EXEC_FORALL)
12759 forall_flag = 1;
12760 gfc_resolve_forall (code, ns, forall_save);
12761 forall_flag = 2;
12763 else if (code->block)
12765 omp_workshare_save = -1;
12766 switch (code->op)
12768 case EXEC_OACC_PARALLEL_LOOP:
12769 case EXEC_OACC_PARALLEL:
12770 case EXEC_OACC_KERNELS_LOOP:
12771 case EXEC_OACC_KERNELS:
12772 case EXEC_OACC_SERIAL_LOOP:
12773 case EXEC_OACC_SERIAL:
12774 case EXEC_OACC_DATA:
12775 case EXEC_OACC_HOST_DATA:
12776 case EXEC_OACC_LOOP:
12777 gfc_resolve_oacc_blocks (code, ns);
12778 break;
12779 case EXEC_OMP_PARALLEL_WORKSHARE:
12780 omp_workshare_save = omp_workshare_flag;
12781 omp_workshare_flag = 1;
12782 gfc_resolve_omp_parallel_blocks (code, ns);
12783 break;
12784 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12785 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12786 case EXEC_OMP_MASKED_TASKLOOP:
12787 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
12788 case EXEC_OMP_MASTER_TASKLOOP:
12789 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
12790 case EXEC_OMP_PARALLEL:
12791 case EXEC_OMP_PARALLEL_DO:
12792 case EXEC_OMP_PARALLEL_DO_SIMD:
12793 case EXEC_OMP_PARALLEL_LOOP:
12794 case EXEC_OMP_PARALLEL_MASKED:
12795 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
12796 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
12797 case EXEC_OMP_PARALLEL_MASTER:
12798 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
12799 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
12800 case EXEC_OMP_PARALLEL_SECTIONS:
12801 case EXEC_OMP_TARGET_PARALLEL:
12802 case EXEC_OMP_TARGET_PARALLEL_DO:
12803 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12804 case EXEC_OMP_TARGET_PARALLEL_LOOP:
12805 case EXEC_OMP_TARGET_TEAMS:
12806 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12807 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12808 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12809 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12810 case EXEC_OMP_TARGET_TEAMS_LOOP:
12811 case EXEC_OMP_TASK:
12812 case EXEC_OMP_TASKLOOP:
12813 case EXEC_OMP_TASKLOOP_SIMD:
12814 case EXEC_OMP_TEAMS:
12815 case EXEC_OMP_TEAMS_DISTRIBUTE:
12816 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12817 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12818 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12819 case EXEC_OMP_TEAMS_LOOP:
12820 omp_workshare_save = omp_workshare_flag;
12821 omp_workshare_flag = 0;
12822 gfc_resolve_omp_parallel_blocks (code, ns);
12823 break;
12824 case EXEC_OMP_DISTRIBUTE:
12825 case EXEC_OMP_DISTRIBUTE_SIMD:
12826 case EXEC_OMP_DO:
12827 case EXEC_OMP_DO_SIMD:
12828 case EXEC_OMP_LOOP:
12829 case EXEC_OMP_SIMD:
12830 case EXEC_OMP_TARGET_SIMD:
12831 case EXEC_OMP_TILE:
12832 case EXEC_OMP_UNROLL:
12833 gfc_resolve_omp_do_blocks (code, ns);
12834 break;
12835 case EXEC_SELECT_TYPE:
12836 case EXEC_SELECT_RANK:
12837 /* Blocks are handled in resolve_select_type/rank because we
12838 have to transform the SELECT TYPE into ASSOCIATE first. */
12839 break;
12840 case EXEC_DO_CONCURRENT:
12841 gfc_do_concurrent_flag = 1;
12842 gfc_resolve_blocks (code->block, ns);
12843 gfc_do_concurrent_flag = 2;
12844 break;
12845 case EXEC_OMP_WORKSHARE:
12846 omp_workshare_save = omp_workshare_flag;
12847 omp_workshare_flag = 1;
12848 /* FALL THROUGH */
12849 default:
12850 gfc_resolve_blocks (code->block, ns);
12851 break;
12854 if (omp_workshare_save != -1)
12855 omp_workshare_flag = omp_workshare_save;
12857 start:
12858 t = true;
12859 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
12860 t = gfc_resolve_expr (code->expr1);
12861 forall_flag = forall_save;
12862 gfc_do_concurrent_flag = do_concurrent_save;
12864 if (!gfc_resolve_expr (code->expr2))
12865 t = false;
12867 if (code->op == EXEC_ALLOCATE
12868 && !gfc_resolve_expr (code->expr3))
12869 t = false;
12871 switch (code->op)
12873 case EXEC_NOP:
12874 case EXEC_END_BLOCK:
12875 case EXEC_END_NESTED_BLOCK:
12876 case EXEC_CYCLE:
12877 case EXEC_PAUSE:
12878 break;
12880 case EXEC_STOP:
12881 case EXEC_ERROR_STOP:
12882 if (code->expr2 != NULL
12883 && (code->expr2->ts.type != BT_LOGICAL
12884 || code->expr2->rank != 0))
12885 gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
12886 &code->expr2->where);
12887 break;
12889 case EXEC_EXIT:
12890 case EXEC_CONTINUE:
12891 case EXEC_DT_END:
12892 case EXEC_ASSIGN_CALL:
12893 break;
12895 case EXEC_CRITICAL:
12896 resolve_critical (code);
12897 break;
12899 case EXEC_SYNC_ALL:
12900 case EXEC_SYNC_IMAGES:
12901 case EXEC_SYNC_MEMORY:
12902 resolve_sync (code);
12903 break;
12905 case EXEC_LOCK:
12906 case EXEC_UNLOCK:
12907 case EXEC_EVENT_POST:
12908 case EXEC_EVENT_WAIT:
12909 resolve_lock_unlock_event (code);
12910 break;
12912 case EXEC_FAIL_IMAGE:
12913 break;
12915 case EXEC_FORM_TEAM:
12916 if (code->expr1 != NULL
12917 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
12918 gfc_error ("TEAM NUMBER argument to FORM TEAM at %L must be "
12919 "a scalar INTEGER", &code->expr1->where);
12920 check_team (code->expr2, "FORM TEAM");
12921 break;
12923 case EXEC_CHANGE_TEAM:
12924 check_team (code->expr1, "CHANGE TEAM");
12925 break;
12927 case EXEC_END_TEAM:
12928 break;
12930 case EXEC_SYNC_TEAM:
12931 check_team (code->expr1, "SYNC TEAM");
12932 break;
12934 case EXEC_ENTRY:
12935 /* Keep track of which entry we are up to. */
12936 current_entry_id = code->ext.entry->id;
12937 break;
12939 case EXEC_WHERE:
12940 resolve_where (code, NULL);
12941 break;
12943 case EXEC_GOTO:
12944 if (code->expr1 != NULL)
12946 if (code->expr1->expr_type != EXPR_VARIABLE
12947 || code->expr1->ts.type != BT_INTEGER
12948 || (code->expr1->ref
12949 && code->expr1->ref->type == REF_ARRAY)
12950 || code->expr1->symtree == NULL
12951 || (code->expr1->symtree->n.sym
12952 && (code->expr1->symtree->n.sym->attr.flavor
12953 == FL_PARAMETER)))
12954 gfc_error ("ASSIGNED GOTO statement at %L requires a "
12955 "scalar INTEGER variable", &code->expr1->where);
12956 else if (code->expr1->symtree->n.sym
12957 && code->expr1->symtree->n.sym->attr.assign != 1)
12958 gfc_error ("Variable %qs has not been assigned a target "
12959 "label at %L", code->expr1->symtree->n.sym->name,
12960 &code->expr1->where);
12962 else
12963 resolve_branch (code->label1, code);
12964 break;
12966 case EXEC_RETURN:
12967 if (code->expr1 != NULL
12968 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
12969 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
12970 "INTEGER return specifier", &code->expr1->where);
12971 break;
12973 case EXEC_INIT_ASSIGN:
12974 case EXEC_END_PROCEDURE:
12975 break;
12977 case EXEC_ASSIGN:
12978 if (!t)
12979 break;
12981 if (code->expr1->ts.type == BT_CLASS)
12982 gfc_find_vtab (&code->expr2->ts);
12984 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
12985 the LHS. */
12986 if (code->expr1->expr_type == EXPR_FUNCTION
12987 && code->expr1->value.function.isym
12988 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
12989 remove_caf_get_intrinsic (code->expr1);
12991 /* If this is a pointer function in an lvalue variable context,
12992 the new code will have to be resolved afresh. This is also the
12993 case with an error, where the code is transformed into NOP to
12994 prevent ICEs downstream. */
12995 if (resolve_ptr_fcn_assign (&code, ns)
12996 || code->op == EXEC_NOP)
12997 goto start;
12999 if (!gfc_check_vardef_context (code->expr1, false, false, false,
13000 _("assignment")))
13001 break;
13003 if (resolve_ordinary_assign (code, ns))
13005 if (omp_workshare_flag)
13007 gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
13008 "at %L", &code->loc);
13009 break;
13011 if (code->op == EXEC_COMPCALL)
13012 goto compcall;
13013 else
13014 goto call;
13017 /* Check for dependencies in deferred character length array
13018 assignments and generate a temporary, if necessary. */
13019 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
13020 break;
13022 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
13023 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
13024 && code->expr1->ts.u.derived
13025 && code->expr1->ts.u.derived->attr.defined_assign_comp)
13026 generate_component_assignments (&code, ns);
13027 else if (code->op == EXEC_ASSIGN)
13029 if (gfc_may_be_finalized (code->expr1->ts))
13030 code->expr1->must_finalize = 1;
13031 if (code->expr2->expr_type == EXPR_ARRAY
13032 && gfc_may_be_finalized (code->expr2->ts))
13033 code->expr2->must_finalize = 1;
13036 break;
13038 case EXEC_LABEL_ASSIGN:
13039 if (code->label1->defined == ST_LABEL_UNKNOWN)
13040 gfc_error ("Label %d referenced at %L is never defined",
13041 code->label1->value, &code->label1->where);
13042 if (t
13043 && (code->expr1->expr_type != EXPR_VARIABLE
13044 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
13045 || code->expr1->symtree->n.sym->ts.kind
13046 != gfc_default_integer_kind
13047 || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
13048 || code->expr1->symtree->n.sym->as != NULL))
13049 gfc_error ("ASSIGN statement at %L requires a scalar "
13050 "default INTEGER variable", &code->expr1->where);
13051 break;
13053 case EXEC_POINTER_ASSIGN:
13055 gfc_expr* e;
13057 if (!t)
13058 break;
13060 /* This is both a variable definition and pointer assignment
13061 context, so check both of them. For rank remapping, a final
13062 array ref may be present on the LHS and fool gfc_expr_attr
13063 used in gfc_check_vardef_context. Remove it. */
13064 e = remove_last_array_ref (code->expr1);
13065 t = gfc_check_vardef_context (e, true, false, false,
13066 _("pointer assignment"));
13067 if (t)
13068 t = gfc_check_vardef_context (e, false, false, false,
13069 _("pointer assignment"));
13070 gfc_free_expr (e);
13072 t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
13074 if (!t)
13075 break;
13077 /* Assigning a class object always is a regular assign. */
13078 if (code->expr2->ts.type == BT_CLASS
13079 && code->expr1->ts.type == BT_CLASS
13080 && CLASS_DATA (code->expr2)
13081 && !CLASS_DATA (code->expr2)->attr.dimension
13082 && !(gfc_expr_attr (code->expr1).proc_pointer
13083 && code->expr2->expr_type == EXPR_VARIABLE
13084 && code->expr2->symtree->n.sym->attr.flavor
13085 == FL_PROCEDURE))
13086 code->op = EXEC_ASSIGN;
13087 break;
13090 case EXEC_ARITHMETIC_IF:
13092 gfc_expr *e = code->expr1;
13094 gfc_resolve_expr (e);
13095 if (e->expr_type == EXPR_NULL)
13096 gfc_error ("Invalid NULL at %L", &e->where);
13098 if (t && (e->rank > 0
13099 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
13100 gfc_error ("Arithmetic IF statement at %L requires a scalar "
13101 "REAL or INTEGER expression", &e->where);
13103 resolve_branch (code->label1, code);
13104 resolve_branch (code->label2, code);
13105 resolve_branch (code->label3, code);
13107 break;
13109 case EXEC_IF:
13110 if (t && code->expr1 != NULL
13111 && (code->expr1->ts.type != BT_LOGICAL
13112 || code->expr1->rank != 0))
13113 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
13114 &code->expr1->where);
13115 break;
13117 case EXEC_CALL:
13118 call:
13119 resolve_call (code);
13120 break;
13122 case EXEC_COMPCALL:
13123 compcall:
13124 resolve_typebound_subroutine (code);
13125 break;
13127 case EXEC_CALL_PPC:
13128 resolve_ppc_call (code);
13129 break;
13131 case EXEC_SELECT:
13132 /* Select is complicated. Also, a SELECT construct could be
13133 a transformed computed GOTO. */
13134 resolve_select (code, false);
13135 break;
13137 case EXEC_SELECT_TYPE:
13138 resolve_select_type (code, ns);
13139 break;
13141 case EXEC_SELECT_RANK:
13142 resolve_select_rank (code, ns);
13143 break;
13145 case EXEC_BLOCK:
13146 resolve_block_construct (code);
13147 break;
13149 case EXEC_DO:
13150 if (code->ext.iterator != NULL)
13152 gfc_iterator *iter = code->ext.iterator;
13153 if (gfc_resolve_iterator (iter, true, false))
13154 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
13155 true);
13157 break;
13159 case EXEC_DO_WHILE:
13160 if (code->expr1 == NULL)
13161 gfc_internal_error ("gfc_resolve_code(): No expression on "
13162 "DO WHILE");
13163 if (t
13164 && (code->expr1->rank != 0
13165 || code->expr1->ts.type != BT_LOGICAL))
13166 gfc_error ("Exit condition of DO WHILE loop at %L must be "
13167 "a scalar LOGICAL expression", &code->expr1->where);
13168 break;
13170 case EXEC_ALLOCATE:
13171 if (t)
13172 resolve_allocate_deallocate (code, "ALLOCATE");
13174 break;
13176 case EXEC_DEALLOCATE:
13177 if (t)
13178 resolve_allocate_deallocate (code, "DEALLOCATE");
13180 break;
13182 case EXEC_OPEN:
13183 if (!gfc_resolve_open (code->ext.open, &code->loc))
13184 break;
13186 resolve_branch (code->ext.open->err, code);
13187 break;
13189 case EXEC_CLOSE:
13190 if (!gfc_resolve_close (code->ext.close, &code->loc))
13191 break;
13193 resolve_branch (code->ext.close->err, code);
13194 break;
13196 case EXEC_BACKSPACE:
13197 case EXEC_ENDFILE:
13198 case EXEC_REWIND:
13199 case EXEC_FLUSH:
13200 if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
13201 break;
13203 resolve_branch (code->ext.filepos->err, code);
13204 break;
13206 case EXEC_INQUIRE:
13207 if (!gfc_resolve_inquire (code->ext.inquire))
13208 break;
13210 resolve_branch (code->ext.inquire->err, code);
13211 break;
13213 case EXEC_IOLENGTH:
13214 gcc_assert (code->ext.inquire != NULL);
13215 if (!gfc_resolve_inquire (code->ext.inquire))
13216 break;
13218 resolve_branch (code->ext.inquire->err, code);
13219 break;
13221 case EXEC_WAIT:
13222 if (!gfc_resolve_wait (code->ext.wait))
13223 break;
13225 resolve_branch (code->ext.wait->err, code);
13226 resolve_branch (code->ext.wait->end, code);
13227 resolve_branch (code->ext.wait->eor, code);
13228 break;
13230 case EXEC_READ:
13231 case EXEC_WRITE:
13232 if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
13233 break;
13235 resolve_branch (code->ext.dt->err, code);
13236 resolve_branch (code->ext.dt->end, code);
13237 resolve_branch (code->ext.dt->eor, code);
13238 break;
13240 case EXEC_TRANSFER:
13241 resolve_transfer (code);
13242 break;
13244 case EXEC_DO_CONCURRENT:
13245 case EXEC_FORALL:
13246 resolve_forall_iterators (code->ext.forall_iterator);
13248 if (code->expr1 != NULL
13249 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
13250 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
13251 "expression", &code->expr1->where);
13252 break;
13254 case EXEC_OACC_PARALLEL_LOOP:
13255 case EXEC_OACC_PARALLEL:
13256 case EXEC_OACC_KERNELS_LOOP:
13257 case EXEC_OACC_KERNELS:
13258 case EXEC_OACC_SERIAL_LOOP:
13259 case EXEC_OACC_SERIAL:
13260 case EXEC_OACC_DATA:
13261 case EXEC_OACC_HOST_DATA:
13262 case EXEC_OACC_LOOP:
13263 case EXEC_OACC_UPDATE:
13264 case EXEC_OACC_WAIT:
13265 case EXEC_OACC_CACHE:
13266 case EXEC_OACC_ENTER_DATA:
13267 case EXEC_OACC_EXIT_DATA:
13268 case EXEC_OACC_ATOMIC:
13269 case EXEC_OACC_DECLARE:
13270 gfc_resolve_oacc_directive (code, ns);
13271 break;
13273 case EXEC_OMP_ALLOCATE:
13274 case EXEC_OMP_ALLOCATORS:
13275 case EXEC_OMP_ASSUME:
13276 case EXEC_OMP_ATOMIC:
13277 case EXEC_OMP_BARRIER:
13278 case EXEC_OMP_CANCEL:
13279 case EXEC_OMP_CANCELLATION_POINT:
13280 case EXEC_OMP_CRITICAL:
13281 case EXEC_OMP_FLUSH:
13282 case EXEC_OMP_DEPOBJ:
13283 case EXEC_OMP_DISTRIBUTE:
13284 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
13285 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
13286 case EXEC_OMP_DISTRIBUTE_SIMD:
13287 case EXEC_OMP_DO:
13288 case EXEC_OMP_DO_SIMD:
13289 case EXEC_OMP_ERROR:
13290 case EXEC_OMP_INTEROP:
13291 case EXEC_OMP_LOOP:
13292 case EXEC_OMP_MASTER:
13293 case EXEC_OMP_MASTER_TASKLOOP:
13294 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
13295 case EXEC_OMP_MASKED:
13296 case EXEC_OMP_MASKED_TASKLOOP:
13297 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
13298 case EXEC_OMP_ORDERED:
13299 case EXEC_OMP_SCAN:
13300 case EXEC_OMP_SCOPE:
13301 case EXEC_OMP_SECTIONS:
13302 case EXEC_OMP_SIMD:
13303 case EXEC_OMP_SINGLE:
13304 case EXEC_OMP_TARGET:
13305 case EXEC_OMP_TARGET_DATA:
13306 case EXEC_OMP_TARGET_ENTER_DATA:
13307 case EXEC_OMP_TARGET_EXIT_DATA:
13308 case EXEC_OMP_TARGET_PARALLEL:
13309 case EXEC_OMP_TARGET_PARALLEL_DO:
13310 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
13311 case EXEC_OMP_TARGET_PARALLEL_LOOP:
13312 case EXEC_OMP_TARGET_SIMD:
13313 case EXEC_OMP_TARGET_TEAMS:
13314 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
13315 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
13316 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13317 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
13318 case EXEC_OMP_TARGET_TEAMS_LOOP:
13319 case EXEC_OMP_TARGET_UPDATE:
13320 case EXEC_OMP_TASK:
13321 case EXEC_OMP_TASKGROUP:
13322 case EXEC_OMP_TASKLOOP:
13323 case EXEC_OMP_TASKLOOP_SIMD:
13324 case EXEC_OMP_TASKWAIT:
13325 case EXEC_OMP_TASKYIELD:
13326 case EXEC_OMP_TEAMS:
13327 case EXEC_OMP_TEAMS_DISTRIBUTE:
13328 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
13329 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13330 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
13331 case EXEC_OMP_TEAMS_LOOP:
13332 case EXEC_OMP_TILE:
13333 case EXEC_OMP_UNROLL:
13334 case EXEC_OMP_WORKSHARE:
13335 gfc_resolve_omp_directive (code, ns);
13336 break;
13338 case EXEC_OMP_PARALLEL:
13339 case EXEC_OMP_PARALLEL_DO:
13340 case EXEC_OMP_PARALLEL_DO_SIMD:
13341 case EXEC_OMP_PARALLEL_LOOP:
13342 case EXEC_OMP_PARALLEL_MASKED:
13343 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
13344 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
13345 case EXEC_OMP_PARALLEL_MASTER:
13346 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
13347 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
13348 case EXEC_OMP_PARALLEL_SECTIONS:
13349 case EXEC_OMP_PARALLEL_WORKSHARE:
13350 omp_workshare_save = omp_workshare_flag;
13351 omp_workshare_flag = 0;
13352 gfc_resolve_omp_directive (code, ns);
13353 omp_workshare_flag = omp_workshare_save;
13354 break;
13356 default:
13357 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
13361 cs_base = frame.prev;
13365 /* Resolve initial values and make sure they are compatible with
13366 the variable. */
13368 static void
13369 resolve_values (gfc_symbol *sym)
13371 bool t;
13373 if (sym->value == NULL)
13374 return;
13376 if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
13377 gfc_warning (OPT_Wdeprecated_declarations,
13378 "Using parameter %qs declared at %L is deprecated",
13379 sym->name, &sym->declared_at);
13381 if (sym->value->expr_type == EXPR_STRUCTURE)
13382 t= resolve_structure_cons (sym->value, 1);
13383 else
13384 t = gfc_resolve_expr (sym->value);
13386 if (!t)
13387 return;
13389 gfc_check_assign_symbol (sym, NULL, sym->value);
13393 /* Verify any BIND(C) derived types in the namespace so we can report errors
13394 for them once, rather than for each variable declared of that type. */
13396 static void
13397 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
13399 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
13400 && derived_sym->attr.is_bind_c == 1)
13401 verify_bind_c_derived_type (derived_sym);
13403 return;
13407 /* Check the interfaces of DTIO procedures associated with derived
13408 type 'sym'. These procedures can either have typebound bindings or
13409 can appear in DTIO generic interfaces. */
13411 static void
13412 gfc_verify_DTIO_procedures (gfc_symbol *sym)
13414 if (!sym || sym->attr.flavor != FL_DERIVED)
13415 return;
13417 gfc_check_dtio_interfaces (sym);
13419 return;
13422 /* Verify that any binding labels used in a given namespace do not collide
13423 with the names or binding labels of any global symbols. Multiple INTERFACE
13424 for the same procedure are permitted. */
13426 static void
13427 gfc_verify_binding_labels (gfc_symbol *sym)
13429 gfc_gsymbol *gsym;
13430 const char *module;
13432 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
13433 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
13434 return;
13436 gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
13438 if (sym->module)
13439 module = sym->module;
13440 else if (sym->ns && sym->ns->proc_name
13441 && sym->ns->proc_name->attr.flavor == FL_MODULE)
13442 module = sym->ns->proc_name->name;
13443 else if (sym->ns && sym->ns->parent
13444 && sym->ns && sym->ns->parent->proc_name
13445 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
13446 module = sym->ns->parent->proc_name->name;
13447 else
13448 module = NULL;
13450 if (!gsym
13451 || (!gsym->defined
13452 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
13454 if (!gsym)
13455 gsym = gfc_get_gsymbol (sym->binding_label, true);
13456 gsym->where = sym->declared_at;
13457 gsym->sym_name = sym->name;
13458 gsym->binding_label = sym->binding_label;
13459 gsym->ns = sym->ns;
13460 gsym->mod_name = module;
13461 if (sym->attr.function)
13462 gsym->type = GSYM_FUNCTION;
13463 else if (sym->attr.subroutine)
13464 gsym->type = GSYM_SUBROUTINE;
13465 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
13466 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
13467 return;
13470 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
13472 gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
13473 "identifier as entity at %L", sym->name,
13474 sym->binding_label, &sym->declared_at, &gsym->where);
13475 /* Clear the binding label to prevent checking multiple times. */
13476 sym->binding_label = NULL;
13477 return;
13480 if (sym->attr.flavor == FL_VARIABLE && module
13481 && (strcmp (module, gsym->mod_name) != 0
13482 || strcmp (sym->name, gsym->sym_name) != 0))
13484 /* This can only happen if the variable is defined in a module - if it
13485 isn't the same module, reject it. */
13486 gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
13487 "uses the same global identifier as entity at %L from module %qs",
13488 sym->name, module, sym->binding_label,
13489 &sym->declared_at, &gsym->where, gsym->mod_name);
13490 sym->binding_label = NULL;
13491 return;
13494 if ((sym->attr.function || sym->attr.subroutine)
13495 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
13496 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
13497 && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
13498 && (module != gsym->mod_name
13499 || strcmp (gsym->sym_name, sym->name) != 0
13500 || (module && strcmp (module, gsym->mod_name) != 0)))
13502 /* Print an error if the procedure is defined multiple times; we have to
13503 exclude references to the same procedure via module association or
13504 multiple checks for the same procedure. */
13505 gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
13506 "global identifier as entity at %L", sym->name,
13507 sym->binding_label, &sym->declared_at, &gsym->where);
13508 sym->binding_label = NULL;
13513 /* Resolve an index expression. */
13515 static bool
13516 resolve_index_expr (gfc_expr *e)
13518 if (!gfc_resolve_expr (e))
13519 return false;
13521 if (!gfc_simplify_expr (e, 0))
13522 return false;
13524 if (!gfc_specification_expr (e))
13525 return false;
13527 return true;
13531 /* Resolve a charlen structure. */
13533 static bool
13534 resolve_charlen (gfc_charlen *cl)
13536 int k;
13537 bool saved_specification_expr;
13539 if (cl->resolved)
13540 return true;
13542 cl->resolved = 1;
13543 saved_specification_expr = specification_expr;
13544 specification_expr = true;
13546 if (cl->length_from_typespec)
13548 if (!gfc_resolve_expr (cl->length))
13550 specification_expr = saved_specification_expr;
13551 return false;
13554 if (!gfc_simplify_expr (cl->length, 0))
13556 specification_expr = saved_specification_expr;
13557 return false;
13560 /* cl->length has been resolved. It should have an integer type. */
13561 if (cl->length
13562 && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
13564 gfc_error ("Scalar INTEGER expression expected at %L",
13565 &cl->length->where);
13566 return false;
13569 else
13571 if (!resolve_index_expr (cl->length))
13573 specification_expr = saved_specification_expr;
13574 return false;
13578 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
13579 a negative value, the length of character entities declared is zero. */
13580 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
13581 && mpz_sgn (cl->length->value.integer) < 0)
13582 gfc_replace_expr (cl->length,
13583 gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
13585 /* Check that the character length is not too large. */
13586 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
13587 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
13588 && cl->length->ts.type == BT_INTEGER
13589 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
13591 gfc_error ("String length at %L is too large", &cl->length->where);
13592 specification_expr = saved_specification_expr;
13593 return false;
13596 specification_expr = saved_specification_expr;
13597 return true;
13601 /* Test for non-constant shape arrays. */
13603 static bool
13604 is_non_constant_shape_array (gfc_symbol *sym)
13606 gfc_expr *e;
13607 int i;
13608 bool not_constant;
13610 not_constant = false;
13611 if (sym->as != NULL)
13613 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
13614 has not been simplified; parameter array references. Do the
13615 simplification now. */
13616 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
13618 if (i == GFC_MAX_DIMENSIONS)
13619 break;
13621 e = sym->as->lower[i];
13622 if (e && (!resolve_index_expr(e)
13623 || !gfc_is_constant_expr (e)))
13624 not_constant = true;
13625 e = sym->as->upper[i];
13626 if (e && (!resolve_index_expr(e)
13627 || !gfc_is_constant_expr (e)))
13628 not_constant = true;
13631 return not_constant;
13634 /* Given a symbol and an initialization expression, add code to initialize
13635 the symbol to the function entry. */
13636 static void
13637 build_init_assign (gfc_symbol *sym, gfc_expr *init)
13639 gfc_expr *lval;
13640 gfc_code *init_st;
13641 gfc_namespace *ns = sym->ns;
13643 /* Search for the function namespace if this is a contained
13644 function without an explicit result. */
13645 if (sym->attr.function && sym == sym->result
13646 && sym->name != sym->ns->proc_name->name)
13648 ns = ns->contained;
13649 for (;ns; ns = ns->sibling)
13650 if (strcmp (ns->proc_name->name, sym->name) == 0)
13651 break;
13654 if (ns == NULL)
13656 gfc_free_expr (init);
13657 return;
13660 /* Build an l-value expression for the result. */
13661 lval = gfc_lval_expr_from_sym (sym);
13663 /* Add the code at scope entry. */
13664 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
13665 init_st->next = ns->code;
13666 ns->code = init_st;
13668 /* Assign the default initializer to the l-value. */
13669 init_st->loc = sym->declared_at;
13670 init_st->expr1 = lval;
13671 init_st->expr2 = init;
13675 /* Whether or not we can generate a default initializer for a symbol. */
13677 static bool
13678 can_generate_init (gfc_symbol *sym)
13680 symbol_attribute *a;
13681 if (!sym)
13682 return false;
13683 a = &sym->attr;
13685 /* These symbols should never have a default initialization. */
13686 return !(
13687 a->allocatable
13688 || a->external
13689 || a->pointer
13690 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
13691 && (CLASS_DATA (sym)->attr.class_pointer
13692 || CLASS_DATA (sym)->attr.proc_pointer))
13693 || a->in_equivalence
13694 || a->in_common
13695 || a->data
13696 || sym->module
13697 || a->cray_pointee
13698 || a->cray_pointer
13699 || sym->assoc
13700 || (!a->referenced && !a->result)
13701 || (a->dummy && (a->intent != INTENT_OUT
13702 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
13703 || (a->function && sym != sym->result)
13708 /* Assign the default initializer to a derived type variable or result. */
13710 static void
13711 apply_default_init (gfc_symbol *sym)
13713 gfc_expr *init = NULL;
13715 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
13716 return;
13718 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
13719 init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
13721 if (init == NULL && sym->ts.type != BT_CLASS)
13722 return;
13724 build_init_assign (sym, init);
13725 sym->attr.referenced = 1;
13729 /* Build an initializer for a local. Returns null if the symbol should not have
13730 a default initialization. */
13732 static gfc_expr *
13733 build_default_init_expr (gfc_symbol *sym)
13735 /* These symbols should never have a default initialization. */
13736 if (sym->attr.allocatable
13737 || sym->attr.external
13738 || sym->attr.dummy
13739 || sym->attr.pointer
13740 || sym->attr.in_equivalence
13741 || sym->attr.in_common
13742 || sym->attr.data
13743 || sym->module
13744 || sym->attr.cray_pointee
13745 || sym->attr.cray_pointer
13746 || sym->assoc)
13747 return NULL;
13749 /* Get the appropriate init expression. */
13750 return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
13753 /* Add an initialization expression to a local variable. */
13754 static void
13755 apply_default_init_local (gfc_symbol *sym)
13757 gfc_expr *init = NULL;
13759 /* The symbol should be a variable or a function return value. */
13760 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
13761 || (sym->attr.function && sym->result != sym))
13762 return;
13764 /* Try to build the initializer expression. If we can't initialize
13765 this symbol, then init will be NULL. */
13766 init = build_default_init_expr (sym);
13767 if (init == NULL)
13768 return;
13770 /* For saved variables, we don't want to add an initializer at function
13771 entry, so we just add a static initializer. Note that automatic variables
13772 are stack allocated even with -fno-automatic; we have also to exclude
13773 result variable, which are also nonstatic. */
13774 if (!sym->attr.automatic
13775 && (sym->attr.save || sym->ns->save_all
13776 || (flag_max_stack_var_size == 0 && !sym->attr.result
13777 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
13778 && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
13780 /* Don't clobber an existing initializer! */
13781 gcc_assert (sym->value == NULL);
13782 sym->value = init;
13783 return;
13786 build_init_assign (sym, init);
13790 /* Resolution of common features of flavors variable and procedure. */
13792 static bool
13793 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
13795 gfc_array_spec *as;
13797 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
13798 && sym->ts.u.derived && CLASS_DATA (sym))
13799 as = CLASS_DATA (sym)->as;
13800 else
13801 as = sym->as;
13803 /* Constraints on deferred shape variable. */
13804 if (as == NULL || as->type != AS_DEFERRED)
13806 bool pointer, allocatable, dimension;
13808 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
13809 && sym->ts.u.derived && CLASS_DATA (sym))
13811 pointer = CLASS_DATA (sym)->attr.class_pointer;
13812 allocatable = CLASS_DATA (sym)->attr.allocatable;
13813 dimension = CLASS_DATA (sym)->attr.dimension;
13815 else
13817 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
13818 allocatable = sym->attr.allocatable;
13819 dimension = sym->attr.dimension;
13822 if (allocatable)
13824 if (dimension
13825 && as
13826 && as->type != AS_ASSUMED_RANK
13827 && !sym->attr.select_rank_temporary)
13829 gfc_error ("Allocatable array %qs at %L must have a deferred "
13830 "shape or assumed rank", sym->name, &sym->declared_at);
13831 return false;
13833 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
13834 "%qs at %L may not be ALLOCATABLE",
13835 sym->name, &sym->declared_at))
13836 return false;
13839 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
13841 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
13842 "assumed rank", sym->name, &sym->declared_at);
13843 sym->error = 1;
13844 return false;
13847 else
13849 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
13850 && sym->ts.type != BT_CLASS && !sym->assoc)
13852 gfc_error ("Array %qs at %L cannot have a deferred shape",
13853 sym->name, &sym->declared_at);
13854 return false;
13858 /* Constraints on polymorphic variables. */
13859 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
13861 /* F03:C502. */
13862 if (sym->attr.class_ok
13863 && sym->ts.u.derived
13864 && !sym->attr.select_type_temporary
13865 && !UNLIMITED_POLY (sym)
13866 && CLASS_DATA (sym)
13867 && CLASS_DATA (sym)->ts.u.derived
13868 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
13870 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
13871 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
13872 &sym->declared_at);
13873 return false;
13876 /* F03:C509. */
13877 /* Assume that use associated symbols were checked in the module ns.
13878 Class-variables that are associate-names are also something special
13879 and excepted from the test. */
13880 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc
13881 && !sym->attr.select_type_temporary
13882 && !sym->attr.select_rank_temporary)
13884 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
13885 "or pointer", sym->name, &sym->declared_at);
13886 return false;
13890 return true;
13894 /* Additional checks for symbols with flavor variable and derived
13895 type. To be called from resolve_fl_variable. */
13897 static bool
13898 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
13900 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
13902 /* Check to see if a derived type is blocked from being host
13903 associated by the presence of another class I symbol in the same
13904 namespace. 14.6.1.3 of the standard and the discussion on
13905 comp.lang.fortran. */
13906 if (sym->ts.u.derived
13907 && sym->ns != sym->ts.u.derived->ns
13908 && !sym->ts.u.derived->attr.use_assoc
13909 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
13911 gfc_symbol *s;
13912 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
13913 if (s && s->attr.generic)
13914 s = gfc_find_dt_in_generic (s);
13915 if (s && !gfc_fl_struct (s->attr.flavor))
13917 gfc_error ("The type %qs cannot be host associated at %L "
13918 "because it is blocked by an incompatible object "
13919 "of the same name declared at %L",
13920 sym->ts.u.derived->name, &sym->declared_at,
13921 &s->declared_at);
13922 return false;
13926 /* 4th constraint in section 11.3: "If an object of a type for which
13927 component-initialization is specified (R429) appears in the
13928 specification-part of a module and does not have the ALLOCATABLE
13929 or POINTER attribute, the object shall have the SAVE attribute."
13931 The check for initializers is performed with
13932 gfc_has_default_initializer because gfc_default_initializer generates
13933 a hidden default for allocatable components. */
13934 if (!(sym->value || no_init_flag) && sym->ns->proc_name
13935 && sym->ns->proc_name->attr.flavor == FL_MODULE
13936 && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
13937 && !sym->attr.pointer && !sym->attr.allocatable
13938 && gfc_has_default_initializer (sym->ts.u.derived)
13939 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
13940 "%qs at %L, needed due to the default "
13941 "initialization", sym->name, &sym->declared_at))
13942 return false;
13944 /* Assign default initializer. */
13945 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
13946 && (!no_init_flag
13947 || (sym->attr.intent == INTENT_OUT
13948 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
13949 sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
13951 return true;
13955 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
13956 except in the declaration of an entity or component that has the POINTER
13957 or ALLOCATABLE attribute. */
13959 static bool
13960 deferred_requirements (gfc_symbol *sym)
13962 if (sym->ts.deferred
13963 && !(sym->attr.pointer
13964 || sym->attr.allocatable
13965 || sym->attr.associate_var
13966 || sym->attr.omp_udr_artificial_var))
13968 /* If a function has a result variable, only check the variable. */
13969 if (sym->result && sym->name != sym->result->name)
13970 return true;
13972 gfc_error ("Entity %qs at %L has a deferred type parameter and "
13973 "requires either the POINTER or ALLOCATABLE attribute",
13974 sym->name, &sym->declared_at);
13975 return false;
13977 return true;
13981 /* Resolve symbols with flavor variable. */
13983 static bool
13984 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
13986 const char *auto_save_msg = "Automatic object %qs at %L cannot have the "
13987 "SAVE attribute";
13989 if (!resolve_fl_var_and_proc (sym, mp_flag))
13990 return false;
13992 /* Set this flag to check that variables are parameters of all entries.
13993 This check is effected by the call to gfc_resolve_expr through
13994 is_non_constant_shape_array. */
13995 bool saved_specification_expr = specification_expr;
13996 specification_expr = true;
13998 if (sym->ns->proc_name
13999 && (sym->ns->proc_name->attr.flavor == FL_MODULE
14000 || sym->ns->proc_name->attr.is_main_program)
14001 && !sym->attr.use_assoc
14002 && !sym->attr.allocatable
14003 && !sym->attr.pointer
14004 && is_non_constant_shape_array (sym))
14006 /* F08:C541. The shape of an array defined in a main program or module
14007 * needs to be constant. */
14008 gfc_error ("The module or main program array %qs at %L must "
14009 "have constant shape", sym->name, &sym->declared_at);
14010 specification_expr = saved_specification_expr;
14011 return false;
14014 /* Constraints on deferred type parameter. */
14015 if (!deferred_requirements (sym))
14016 return false;
14018 if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
14020 /* Make sure that character string variables with assumed length are
14021 dummy arguments. */
14022 gfc_expr *e = NULL;
14024 if (sym->ts.u.cl)
14025 e = sym->ts.u.cl->length;
14026 else
14027 return false;
14029 if (e == NULL && !sym->attr.dummy && !sym->attr.result
14030 && !sym->ts.deferred && !sym->attr.select_type_temporary
14031 && !sym->attr.omp_udr_artificial_var)
14033 gfc_error ("Entity with assumed character length at %L must be a "
14034 "dummy argument or a PARAMETER", &sym->declared_at);
14035 specification_expr = saved_specification_expr;
14036 return false;
14039 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
14041 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
14042 specification_expr = saved_specification_expr;
14043 return false;
14046 if (!gfc_is_constant_expr (e)
14047 && !(e->expr_type == EXPR_VARIABLE
14048 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
14050 if (!sym->attr.use_assoc && sym->ns->proc_name
14051 && (sym->ns->proc_name->attr.flavor == FL_MODULE
14052 || sym->ns->proc_name->attr.is_main_program))
14054 gfc_error ("%qs at %L must have constant character length "
14055 "in this context", sym->name, &sym->declared_at);
14056 specification_expr = saved_specification_expr;
14057 return false;
14059 if (sym->attr.in_common)
14061 gfc_error ("COMMON variable %qs at %L must have constant "
14062 "character length", sym->name, &sym->declared_at);
14063 specification_expr = saved_specification_expr;
14064 return false;
14069 if (sym->value == NULL && sym->attr.referenced
14070 && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
14071 apply_default_init_local (sym); /* Try to apply a default initialization. */
14073 /* Determine if the symbol may not have an initializer. */
14074 int no_init_flag = 0, automatic_flag = 0;
14075 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
14076 || sym->attr.intrinsic || sym->attr.result)
14077 no_init_flag = 1;
14078 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
14079 && is_non_constant_shape_array (sym))
14081 no_init_flag = automatic_flag = 1;
14083 /* Also, they must not have the SAVE attribute.
14084 SAVE_IMPLICIT is checked below. */
14085 if (sym->as && sym->attr.codimension)
14087 int corank = sym->as->corank;
14088 sym->as->corank = 0;
14089 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
14090 sym->as->corank = corank;
14092 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
14094 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
14095 specification_expr = saved_specification_expr;
14096 return false;
14100 /* Ensure that any initializer is simplified. */
14101 if (sym->value)
14102 gfc_simplify_expr (sym->value, 1);
14104 /* Reject illegal initializers. */
14105 if (!sym->mark && sym->value)
14107 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
14108 && CLASS_DATA (sym)->attr.allocatable))
14109 gfc_error ("Allocatable %qs at %L cannot have an initializer",
14110 sym->name, &sym->declared_at);
14111 else if (sym->attr.external)
14112 gfc_error ("External %qs at %L cannot have an initializer",
14113 sym->name, &sym->declared_at);
14114 else if (sym->attr.dummy)
14115 gfc_error ("Dummy %qs at %L cannot have an initializer",
14116 sym->name, &sym->declared_at);
14117 else if (sym->attr.intrinsic)
14118 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
14119 sym->name, &sym->declared_at);
14120 else if (sym->attr.result)
14121 gfc_error ("Function result %qs at %L cannot have an initializer",
14122 sym->name, &sym->declared_at);
14123 else if (automatic_flag)
14124 gfc_error ("Automatic array %qs at %L cannot have an initializer",
14125 sym->name, &sym->declared_at);
14126 else
14127 goto no_init_error;
14128 specification_expr = saved_specification_expr;
14129 return false;
14132 no_init_error:
14133 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
14135 bool res = resolve_fl_variable_derived (sym, no_init_flag);
14136 specification_expr = saved_specification_expr;
14137 return res;
14140 specification_expr = saved_specification_expr;
14141 return true;
14145 /* Compare the dummy characteristics of a module procedure interface
14146 declaration with the corresponding declaration in a submodule. */
14147 static gfc_formal_arglist *new_formal;
14148 static char errmsg[200];
14150 static void
14151 compare_fsyms (gfc_symbol *sym)
14153 gfc_symbol *fsym;
14155 if (sym == NULL || new_formal == NULL)
14156 return;
14158 fsym = new_formal->sym;
14160 if (sym == fsym)
14161 return;
14163 if (strcmp (sym->name, fsym->name) == 0)
14165 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
14166 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
14171 /* Resolve a procedure. */
14173 static bool
14174 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
14176 gfc_formal_arglist *arg;
14177 bool allocatable_or_pointer = false;
14179 if (sym->attr.function
14180 && !resolve_fl_var_and_proc (sym, mp_flag))
14181 return false;
14183 /* Constraints on deferred type parameter. */
14184 if (!deferred_requirements (sym))
14185 return false;
14187 if (sym->ts.type == BT_CHARACTER)
14189 gfc_charlen *cl = sym->ts.u.cl;
14191 if (cl && cl->length && gfc_is_constant_expr (cl->length)
14192 && !resolve_charlen (cl))
14193 return false;
14195 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
14196 && sym->attr.proc == PROC_ST_FUNCTION)
14198 gfc_error ("Character-valued statement function %qs at %L must "
14199 "have constant length", sym->name, &sym->declared_at);
14200 return false;
14204 /* Ensure that derived type for are not of a private type. Internal
14205 module procedures are excluded by 2.2.3.3 - i.e., they are not
14206 externally accessible and can access all the objects accessible in
14207 the host. */
14208 if (!(sym->ns->parent && sym->ns->parent->proc_name
14209 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
14210 && gfc_check_symbol_access (sym))
14212 gfc_interface *iface;
14214 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
14216 if (arg->sym
14217 && arg->sym->ts.type == BT_DERIVED
14218 && arg->sym->ts.u.derived
14219 && !arg->sym->ts.u.derived->attr.use_assoc
14220 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
14221 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
14222 "and cannot be a dummy argument"
14223 " of %qs, which is PUBLIC at %L",
14224 arg->sym->name, sym->name,
14225 &sym->declared_at))
14227 /* Stop this message from recurring. */
14228 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
14229 return false;
14233 /* PUBLIC interfaces may expose PRIVATE procedures that take types
14234 PRIVATE to the containing module. */
14235 for (iface = sym->generic; iface; iface = iface->next)
14237 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
14239 if (arg->sym
14240 && arg->sym->ts.type == BT_DERIVED
14241 && !arg->sym->ts.u.derived->attr.use_assoc
14242 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
14243 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
14244 "PUBLIC interface %qs at %L "
14245 "takes dummy arguments of %qs which "
14246 "is PRIVATE", iface->sym->name,
14247 sym->name, &iface->sym->declared_at,
14248 gfc_typename(&arg->sym->ts)))
14250 /* Stop this message from recurring. */
14251 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
14252 return false;
14258 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
14259 && !sym->attr.proc_pointer)
14261 gfc_error ("Function %qs at %L cannot have an initializer",
14262 sym->name, &sym->declared_at);
14264 /* Make sure no second error is issued for this. */
14265 sym->value->error = 1;
14266 return false;
14269 /* An external symbol may not have an initializer because it is taken to be
14270 a procedure. Exception: Procedure Pointers. */
14271 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
14273 gfc_error ("External object %qs at %L may not have an initializer",
14274 sym->name, &sym->declared_at);
14275 return false;
14278 /* An elemental function is required to return a scalar 12.7.1 */
14279 if (sym->attr.elemental && sym->attr.function
14280 && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14281 && CLASS_DATA (sym)->as)))
14283 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
14284 "result", sym->name, &sym->declared_at);
14285 /* Reset so that the error only occurs once. */
14286 sym->attr.elemental = 0;
14287 return false;
14290 if (sym->attr.proc == PROC_ST_FUNCTION
14291 && (sym->attr.allocatable || sym->attr.pointer))
14293 gfc_error ("Statement function %qs at %L may not have pointer or "
14294 "allocatable attribute", sym->name, &sym->declared_at);
14295 return false;
14298 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
14299 char-len-param shall not be array-valued, pointer-valued, recursive
14300 or pure. ....snip... A character value of * may only be used in the
14301 following ways: (i) Dummy arg of procedure - dummy associates with
14302 actual length; (ii) To declare a named constant; or (iii) External
14303 function - but length must be declared in calling scoping unit. */
14304 if (sym->attr.function
14305 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
14306 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
14308 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
14309 || (sym->attr.recursive) || (sym->attr.pure))
14311 if (sym->as && sym->as->rank)
14312 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
14313 "array-valued", sym->name, &sym->declared_at);
14315 if (sym->attr.pointer)
14316 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
14317 "pointer-valued", sym->name, &sym->declared_at);
14319 if (sym->attr.pure)
14320 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
14321 "pure", sym->name, &sym->declared_at);
14323 if (sym->attr.recursive)
14324 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
14325 "recursive", sym->name, &sym->declared_at);
14327 return false;
14330 /* Appendix B.2 of the standard. Contained functions give an
14331 error anyway. Deferred character length is an F2003 feature.
14332 Don't warn on intrinsic conversion functions, which start
14333 with two underscores. */
14334 if (!sym->attr.contained && !sym->ts.deferred
14335 && (sym->name[0] != '_' || sym->name[1] != '_'))
14336 gfc_notify_std (GFC_STD_F95_OBS,
14337 "CHARACTER(*) function %qs at %L",
14338 sym->name, &sym->declared_at);
14341 /* F2008, C1218. */
14342 if (sym->attr.elemental)
14344 if (sym->attr.proc_pointer)
14346 const char* name = (sym->attr.result ? sym->ns->proc_name->name
14347 : sym->name);
14348 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
14349 name, &sym->declared_at);
14350 return false;
14352 if (sym->attr.dummy)
14354 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
14355 sym->name, &sym->declared_at);
14356 return false;
14360 /* F2018, C15100: "The result of an elemental function shall be scalar,
14361 and shall not have the POINTER or ALLOCATABLE attribute." The scalar
14362 pointer is tested and caught elsewhere. */
14363 if (sym->result)
14364 allocatable_or_pointer = sym->result->ts.type == BT_CLASS
14365 && CLASS_DATA (sym->result) ?
14366 (CLASS_DATA (sym->result)->attr.allocatable
14367 || CLASS_DATA (sym->result)->attr.pointer) :
14368 (sym->result->attr.allocatable
14369 || sym->result->attr.pointer);
14371 if (sym->attr.elemental && sym->result
14372 && allocatable_or_pointer)
14374 gfc_error ("Function result variable %qs at %L of elemental "
14375 "function %qs shall not have an ALLOCATABLE or POINTER "
14376 "attribute", sym->result->name,
14377 &sym->result->declared_at, sym->name);
14378 return false;
14381 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
14383 gfc_formal_arglist *curr_arg;
14384 int has_non_interop_arg = 0;
14386 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
14387 sym->common_block))
14389 /* Clear these to prevent looking at them again if there was an
14390 error. */
14391 sym->attr.is_bind_c = 0;
14392 sym->attr.is_c_interop = 0;
14393 sym->ts.is_c_interop = 0;
14395 else
14397 /* So far, no errors have been found. */
14398 sym->attr.is_c_interop = 1;
14399 sym->ts.is_c_interop = 1;
14402 curr_arg = gfc_sym_get_dummy_args (sym);
14403 while (curr_arg != NULL)
14405 /* Skip implicitly typed dummy args here. */
14406 if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
14407 if (!gfc_verify_c_interop_param (curr_arg->sym))
14408 /* If something is found to fail, record the fact so we
14409 can mark the symbol for the procedure as not being
14410 BIND(C) to try and prevent multiple errors being
14411 reported. */
14412 has_non_interop_arg = 1;
14414 curr_arg = curr_arg->next;
14417 /* See if any of the arguments were not interoperable and if so, clear
14418 the procedure symbol to prevent duplicate error messages. */
14419 if (has_non_interop_arg != 0)
14421 sym->attr.is_c_interop = 0;
14422 sym->ts.is_c_interop = 0;
14423 sym->attr.is_bind_c = 0;
14427 if (!sym->attr.proc_pointer)
14429 if (sym->attr.save == SAVE_EXPLICIT)
14431 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
14432 "in %qs at %L", sym->name, &sym->declared_at);
14433 return false;
14435 if (sym->attr.intent)
14437 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
14438 "in %qs at %L", sym->name, &sym->declared_at);
14439 return false;
14441 if (sym->attr.subroutine && sym->attr.result)
14443 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
14444 "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
14445 return false;
14447 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
14448 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
14449 || sym->attr.contained))
14451 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
14452 "in %qs at %L", sym->name, &sym->declared_at);
14453 return false;
14455 if (strcmp ("ppr@", sym->name) == 0)
14457 gfc_error ("Procedure pointer result %qs at %L "
14458 "is missing the pointer attribute",
14459 sym->ns->proc_name->name, &sym->declared_at);
14460 return false;
14464 /* Assume that a procedure whose body is not known has references
14465 to external arrays. */
14466 if (sym->attr.if_source != IFSRC_DECL)
14467 sym->attr.array_outer_dependency = 1;
14469 /* Compare the characteristics of a module procedure with the
14470 interface declaration. Ideally this would be done with
14471 gfc_compare_interfaces but, at present, the formal interface
14472 cannot be copied to the ts.interface. */
14473 if (sym->attr.module_procedure
14474 && sym->attr.if_source == IFSRC_DECL)
14476 gfc_symbol *iface;
14477 char name[2*GFC_MAX_SYMBOL_LEN + 1];
14478 char *module_name;
14479 char *submodule_name;
14480 strcpy (name, sym->ns->proc_name->name);
14481 module_name = strtok (name, ".");
14482 submodule_name = strtok (NULL, ".");
14484 iface = sym->tlink;
14485 sym->tlink = NULL;
14487 /* Make sure that the result uses the correct charlen for deferred
14488 length results. */
14489 if (iface && sym->result
14490 && iface->ts.type == BT_CHARACTER
14491 && iface->ts.deferred)
14492 sym->result->ts.u.cl = iface->ts.u.cl;
14494 if (iface == NULL)
14495 goto check_formal;
14497 /* Check the procedure characteristics. */
14498 if (sym->attr.elemental != iface->attr.elemental)
14500 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
14501 "PROCEDURE at %L and its interface in %s",
14502 &sym->declared_at, module_name);
14503 return false;
14506 if (sym->attr.pure != iface->attr.pure)
14508 gfc_error ("Mismatch in PURE attribute between MODULE "
14509 "PROCEDURE at %L and its interface in %s",
14510 &sym->declared_at, module_name);
14511 return false;
14514 if (sym->attr.recursive != iface->attr.recursive)
14516 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
14517 "PROCEDURE at %L and its interface in %s",
14518 &sym->declared_at, module_name);
14519 return false;
14522 /* Check the result characteristics. */
14523 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
14525 gfc_error ("%s between the MODULE PROCEDURE declaration "
14526 "in MODULE %qs and the declaration at %L in "
14527 "(SUB)MODULE %qs",
14528 errmsg, module_name, &sym->declared_at,
14529 submodule_name ? submodule_name : module_name);
14530 return false;
14533 check_formal:
14534 /* Check the characteristics of the formal arguments. */
14535 if (sym->formal && sym->formal_ns)
14537 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
14539 new_formal = arg;
14540 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
14545 /* F2018:15.4.2.2 requires an explicit interface for procedures with the
14546 BIND(C) attribute. */
14547 if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
14549 gfc_error ("Interface of %qs at %L must be explicit",
14550 sym->name, &sym->declared_at);
14551 return false;
14554 return true;
14558 /* Resolve a list of finalizer procedures. That is, after they have hopefully
14559 been defined and we now know their defined arguments, check that they fulfill
14560 the requirements of the standard for procedures used as finalizers. */
14562 static bool
14563 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
14565 gfc_finalizer* list;
14566 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
14567 bool result = true;
14568 bool seen_scalar = false;
14569 gfc_symbol *vtab;
14570 gfc_component *c;
14571 gfc_symbol *parent = gfc_get_derived_super_type (derived);
14573 if (parent)
14574 gfc_resolve_finalizers (parent, finalizable);
14576 /* Ensure that derived-type components have a their finalizers resolved. */
14577 bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
14578 for (c = derived->components; c; c = c->next)
14579 if (c->ts.type == BT_DERIVED
14580 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
14582 bool has_final2 = false;
14583 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
14584 return false; /* Error. */
14585 has_final = has_final || has_final2;
14587 /* Return early if not finalizable. */
14588 if (!has_final)
14590 if (finalizable)
14591 *finalizable = false;
14592 return true;
14595 /* Walk over the list of finalizer-procedures, check them, and if any one
14596 does not fit in with the standard's definition, print an error and remove
14597 it from the list. */
14598 prev_link = &derived->f2k_derived->finalizers;
14599 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
14601 gfc_formal_arglist *dummy_args;
14602 gfc_symbol* arg;
14603 gfc_finalizer* i;
14604 int my_rank;
14606 /* Skip this finalizer if we already resolved it. */
14607 if (list->proc_tree)
14609 if (list->proc_tree->n.sym->formal->sym->as == NULL
14610 || list->proc_tree->n.sym->formal->sym->as->rank == 0)
14611 seen_scalar = true;
14612 prev_link = &(list->next);
14613 continue;
14616 /* Check this exists and is a SUBROUTINE. */
14617 if (!list->proc_sym->attr.subroutine)
14619 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
14620 list->proc_sym->name, &list->where);
14621 goto error;
14624 /* We should have exactly one argument. */
14625 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
14626 if (!dummy_args || dummy_args->next)
14628 gfc_error ("FINAL procedure at %L must have exactly one argument",
14629 &list->where);
14630 goto error;
14632 arg = dummy_args->sym;
14634 if (!arg)
14636 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
14637 &list->proc_sym->declared_at, derived->name);
14638 goto error;
14641 if (arg->as && arg->as->type == AS_ASSUMED_RANK
14642 && ((list != derived->f2k_derived->finalizers) || list->next))
14644 gfc_error ("FINAL procedure at %L with assumed rank argument must "
14645 "be the only finalizer with the same kind/type "
14646 "(F2018: C790)", &list->where);
14647 goto error;
14650 /* This argument must be of our type. */
14651 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
14653 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
14654 &arg->declared_at, derived->name);
14655 goto error;
14658 /* It must neither be a pointer nor allocatable nor optional. */
14659 if (arg->attr.pointer)
14661 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
14662 &arg->declared_at);
14663 goto error;
14665 if (arg->attr.allocatable)
14667 gfc_error ("Argument of FINAL procedure at %L must not be"
14668 " ALLOCATABLE", &arg->declared_at);
14669 goto error;
14671 if (arg->attr.optional)
14673 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
14674 &arg->declared_at);
14675 goto error;
14678 /* It must not be INTENT(OUT). */
14679 if (arg->attr.intent == INTENT_OUT)
14681 gfc_error ("Argument of FINAL procedure at %L must not be"
14682 " INTENT(OUT)", &arg->declared_at);
14683 goto error;
14686 /* Warn if the procedure is non-scalar and not assumed shape. */
14687 if (warn_surprising && arg->as && arg->as->rank != 0
14688 && arg->as->type != AS_ASSUMED_SHAPE)
14689 gfc_warning (OPT_Wsurprising,
14690 "Non-scalar FINAL procedure at %L should have assumed"
14691 " shape argument", &arg->declared_at);
14693 /* Check that it does not match in kind and rank with a FINAL procedure
14694 defined earlier. To really loop over the *earlier* declarations,
14695 we need to walk the tail of the list as new ones were pushed at the
14696 front. */
14697 /* TODO: Handle kind parameters once they are implemented. */
14698 my_rank = (arg->as ? arg->as->rank : 0);
14699 for (i = list->next; i; i = i->next)
14701 gfc_formal_arglist *dummy_args;
14703 /* Argument list might be empty; that is an error signalled earlier,
14704 but we nevertheless continued resolving. */
14705 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
14706 if (dummy_args)
14708 gfc_symbol* i_arg = dummy_args->sym;
14709 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
14710 if (i_rank == my_rank)
14712 gfc_error ("FINAL procedure %qs declared at %L has the same"
14713 " rank (%d) as %qs",
14714 list->proc_sym->name, &list->where, my_rank,
14715 i->proc_sym->name);
14716 goto error;
14721 /* Is this the/a scalar finalizer procedure? */
14722 if (my_rank == 0)
14723 seen_scalar = true;
14725 /* Find the symtree for this procedure. */
14726 gcc_assert (!list->proc_tree);
14727 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
14729 prev_link = &list->next;
14730 continue;
14732 /* Remove wrong nodes immediately from the list so we don't risk any
14733 troubles in the future when they might fail later expectations. */
14734 error:
14735 i = list;
14736 *prev_link = list->next;
14737 gfc_free_finalizer (i);
14738 result = false;
14741 if (result == false)
14742 return false;
14744 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
14745 were nodes in the list, must have been for arrays. It is surely a good
14746 idea to have a scalar version there if there's something to finalize. */
14747 if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
14748 gfc_warning (OPT_Wsurprising,
14749 "Only array FINAL procedures declared for derived type %qs"
14750 " defined at %L, suggest also scalar one unless an assumed"
14751 " rank finalizer has been declared",
14752 derived->name, &derived->declared_at);
14754 vtab = gfc_find_derived_vtab (derived);
14755 c = vtab->ts.u.derived->components->next->next->next->next->next;
14756 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
14758 if (finalizable)
14759 *finalizable = true;
14761 return true;
14765 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
14767 static bool
14768 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
14769 const char* generic_name, locus where)
14771 gfc_symbol *sym1, *sym2;
14772 const char *pass1, *pass2;
14773 gfc_formal_arglist *dummy_args;
14775 gcc_assert (t1->specific && t2->specific);
14776 gcc_assert (!t1->specific->is_generic);
14777 gcc_assert (!t2->specific->is_generic);
14778 gcc_assert (t1->is_operator == t2->is_operator);
14780 sym1 = t1->specific->u.specific->n.sym;
14781 sym2 = t2->specific->u.specific->n.sym;
14783 if (sym1 == sym2)
14784 return true;
14786 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
14787 if (sym1->attr.subroutine != sym2->attr.subroutine
14788 || sym1->attr.function != sym2->attr.function)
14790 gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
14791 " GENERIC %qs at %L",
14792 sym1->name, sym2->name, generic_name, &where);
14793 return false;
14796 /* Determine PASS arguments. */
14797 if (t1->specific->nopass)
14798 pass1 = NULL;
14799 else if (t1->specific->pass_arg)
14800 pass1 = t1->specific->pass_arg;
14801 else
14803 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
14804 if (dummy_args)
14805 pass1 = dummy_args->sym->name;
14806 else
14807 pass1 = NULL;
14809 if (t2->specific->nopass)
14810 pass2 = NULL;
14811 else if (t2->specific->pass_arg)
14812 pass2 = t2->specific->pass_arg;
14813 else
14815 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
14816 if (dummy_args)
14817 pass2 = dummy_args->sym->name;
14818 else
14819 pass2 = NULL;
14822 /* Compare the interfaces. */
14823 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
14824 NULL, 0, pass1, pass2))
14826 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
14827 sym1->name, sym2->name, generic_name, &where);
14828 return false;
14831 return true;
14835 /* Worker function for resolving a generic procedure binding; this is used to
14836 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
14838 The difference between those cases is finding possible inherited bindings
14839 that are overridden, as one has to look for them in tb_sym_root,
14840 tb_uop_root or tb_op, respectively. Thus the caller must already find
14841 the super-type and set p->overridden correctly. */
14843 static bool
14844 resolve_tb_generic_targets (gfc_symbol* super_type,
14845 gfc_typebound_proc* p, const char* name)
14847 gfc_tbp_generic* target;
14848 gfc_symtree* first_target;
14849 gfc_symtree* inherited;
14851 gcc_assert (p && p->is_generic);
14853 /* Try to find the specific bindings for the symtrees in our target-list. */
14854 gcc_assert (p->u.generic);
14855 for (target = p->u.generic; target; target = target->next)
14856 if (!target->specific)
14858 gfc_typebound_proc* overridden_tbp;
14859 gfc_tbp_generic* g;
14860 const char* target_name;
14862 target_name = target->specific_st->name;
14864 /* Defined for this type directly. */
14865 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
14867 target->specific = target->specific_st->n.tb;
14868 goto specific_found;
14871 /* Look for an inherited specific binding. */
14872 if (super_type)
14874 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
14875 true, NULL);
14877 if (inherited)
14879 gcc_assert (inherited->n.tb);
14880 target->specific = inherited->n.tb;
14881 goto specific_found;
14885 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
14886 " at %L", target_name, name, &p->where);
14887 return false;
14889 /* Once we've found the specific binding, check it is not ambiguous with
14890 other specifics already found or inherited for the same GENERIC. */
14891 specific_found:
14892 gcc_assert (target->specific);
14894 /* This must really be a specific binding! */
14895 if (target->specific->is_generic)
14897 gfc_error ("GENERIC %qs at %L must target a specific binding,"
14898 " %qs is GENERIC, too", name, &p->where, target_name);
14899 return false;
14902 /* Check those already resolved on this type directly. */
14903 for (g = p->u.generic; g; g = g->next)
14904 if (g != target && g->specific
14905 && !check_generic_tbp_ambiguity (target, g, name, p->where))
14906 return false;
14908 /* Check for ambiguity with inherited specific targets. */
14909 for (overridden_tbp = p->overridden; overridden_tbp;
14910 overridden_tbp = overridden_tbp->overridden)
14911 if (overridden_tbp->is_generic)
14913 for (g = overridden_tbp->u.generic; g; g = g->next)
14915 gcc_assert (g->specific);
14916 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
14917 return false;
14922 /* If we attempt to "overwrite" a specific binding, this is an error. */
14923 if (p->overridden && !p->overridden->is_generic)
14925 gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
14926 " the same name", name, &p->where);
14927 return false;
14930 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
14931 all must have the same attributes here. */
14932 first_target = p->u.generic->specific->u.specific;
14933 gcc_assert (first_target);
14934 p->subroutine = first_target->n.sym->attr.subroutine;
14935 p->function = first_target->n.sym->attr.function;
14937 return true;
14941 /* Resolve a GENERIC procedure binding for a derived type. */
14943 static bool
14944 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
14946 gfc_symbol* super_type;
14948 /* Find the overridden binding if any. */
14949 st->n.tb->overridden = NULL;
14950 super_type = gfc_get_derived_super_type (derived);
14951 if (super_type)
14953 gfc_symtree* overridden;
14954 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
14955 true, NULL);
14957 if (overridden && overridden->n.tb)
14958 st->n.tb->overridden = overridden->n.tb;
14961 /* Resolve using worker function. */
14962 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
14966 /* Retrieve the target-procedure of an operator binding and do some checks in
14967 common for intrinsic and user-defined type-bound operators. */
14969 static gfc_symbol*
14970 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
14972 gfc_symbol* target_proc;
14974 gcc_assert (target->specific && !target->specific->is_generic);
14975 target_proc = target->specific->u.specific->n.sym;
14976 gcc_assert (target_proc);
14978 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
14979 if (target->specific->nopass)
14981 gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
14982 return NULL;
14985 return target_proc;
14989 /* Resolve a type-bound intrinsic operator. */
14991 static bool
14992 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
14993 gfc_typebound_proc* p)
14995 gfc_symbol* super_type;
14996 gfc_tbp_generic* target;
14998 /* If there's already an error here, do nothing (but don't fail again). */
14999 if (p->error)
15000 return true;
15002 /* Operators should always be GENERIC bindings. */
15003 gcc_assert (p->is_generic);
15005 /* Look for an overridden binding. */
15006 super_type = gfc_get_derived_super_type (derived);
15007 if (super_type && super_type->f2k_derived)
15008 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
15009 op, true, NULL);
15010 else
15011 p->overridden = NULL;
15013 /* Resolve general GENERIC properties using worker function. */
15014 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
15015 goto error;
15017 /* Check the targets to be procedures of correct interface. */
15018 for (target = p->u.generic; target; target = target->next)
15020 gfc_symbol* target_proc;
15022 target_proc = get_checked_tb_operator_target (target, p->where);
15023 if (!target_proc)
15024 goto error;
15026 if (!gfc_check_operator_interface (target_proc, op, p->where))
15027 goto error;
15029 /* Add target to non-typebound operator list. */
15030 if (!target->specific->deferred && !derived->attr.use_assoc
15031 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
15033 gfc_interface *head, *intr;
15035 /* Preempt 'gfc_check_new_interface' for submodules, where the
15036 mechanism for handling module procedures winds up resolving
15037 operator interfaces twice and would otherwise cause an error. */
15038 for (intr = derived->ns->op[op]; intr; intr = intr->next)
15039 if (intr->sym == target_proc
15040 && target_proc->attr.used_in_submodule)
15041 return true;
15043 if (!gfc_check_new_interface (derived->ns->op[op],
15044 target_proc, p->where))
15045 return false;
15046 head = derived->ns->op[op];
15047 intr = gfc_get_interface ();
15048 intr->sym = target_proc;
15049 intr->where = p->where;
15050 intr->next = head;
15051 derived->ns->op[op] = intr;
15055 return true;
15057 error:
15058 p->error = 1;
15059 return false;
15063 /* Resolve a type-bound user operator (tree-walker callback). */
15065 static gfc_symbol* resolve_bindings_derived;
15066 static bool resolve_bindings_result;
15068 static bool check_uop_procedure (gfc_symbol* sym, locus where);
15070 static void
15071 resolve_typebound_user_op (gfc_symtree* stree)
15073 gfc_symbol* super_type;
15074 gfc_tbp_generic* target;
15076 gcc_assert (stree && stree->n.tb);
15078 if (stree->n.tb->error)
15079 return;
15081 /* Operators should always be GENERIC bindings. */
15082 gcc_assert (stree->n.tb->is_generic);
15084 /* Find overridden procedure, if any. */
15085 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
15086 if (super_type && super_type->f2k_derived)
15088 gfc_symtree* overridden;
15089 overridden = gfc_find_typebound_user_op (super_type, NULL,
15090 stree->name, true, NULL);
15092 if (overridden && overridden->n.tb)
15093 stree->n.tb->overridden = overridden->n.tb;
15095 else
15096 stree->n.tb->overridden = NULL;
15098 /* Resolve basically using worker function. */
15099 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
15100 goto error;
15102 /* Check the targets to be functions of correct interface. */
15103 for (target = stree->n.tb->u.generic; target; target = target->next)
15105 gfc_symbol* target_proc;
15107 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
15108 if (!target_proc)
15109 goto error;
15111 if (!check_uop_procedure (target_proc, stree->n.tb->where))
15112 goto error;
15115 return;
15117 error:
15118 resolve_bindings_result = false;
15119 stree->n.tb->error = 1;
15123 /* Resolve the type-bound procedures for a derived type. */
15125 static void
15126 resolve_typebound_procedure (gfc_symtree* stree)
15128 gfc_symbol* proc;
15129 locus where;
15130 gfc_symbol* me_arg;
15131 gfc_symbol* super_type;
15132 gfc_component* comp;
15134 gcc_assert (stree);
15136 /* Undefined specific symbol from GENERIC target definition. */
15137 if (!stree->n.tb)
15138 return;
15140 if (stree->n.tb->error)
15141 return;
15143 /* If this is a GENERIC binding, use that routine. */
15144 if (stree->n.tb->is_generic)
15146 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
15147 goto error;
15148 return;
15151 /* Get the target-procedure to check it. */
15152 gcc_assert (!stree->n.tb->is_generic);
15153 gcc_assert (stree->n.tb->u.specific);
15154 proc = stree->n.tb->u.specific->n.sym;
15155 where = stree->n.tb->where;
15157 /* Default access should already be resolved from the parser. */
15158 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
15160 if (stree->n.tb->deferred)
15162 if (!check_proc_interface (proc, &where))
15163 goto error;
15165 else
15167 /* If proc has not been resolved at this point, proc->name may
15168 actually be a USE associated entity. See PR fortran/89647. */
15169 if (!proc->resolve_symbol_called
15170 && proc->attr.function == 0 && proc->attr.subroutine == 0)
15172 gfc_symbol *tmp;
15173 gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
15174 if (tmp && tmp->attr.use_assoc)
15176 proc->module = tmp->module;
15177 proc->attr.proc = tmp->attr.proc;
15178 proc->attr.function = tmp->attr.function;
15179 proc->attr.subroutine = tmp->attr.subroutine;
15180 proc->attr.use_assoc = tmp->attr.use_assoc;
15181 proc->ts = tmp->ts;
15182 proc->result = tmp->result;
15186 /* Check for F08:C465. */
15187 if ((!proc->attr.subroutine && !proc->attr.function)
15188 || (proc->attr.proc != PROC_MODULE
15189 && proc->attr.if_source != IFSRC_IFBODY
15190 && !proc->attr.module_procedure)
15191 || proc->attr.abstract)
15193 gfc_error ("%qs must be a module procedure or an external "
15194 "procedure with an explicit interface at %L",
15195 proc->name, &where);
15196 goto error;
15200 stree->n.tb->subroutine = proc->attr.subroutine;
15201 stree->n.tb->function = proc->attr.function;
15203 /* Find the super-type of the current derived type. We could do this once and
15204 store in a global if speed is needed, but as long as not I believe this is
15205 more readable and clearer. */
15206 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
15208 /* If PASS, resolve and check arguments if not already resolved / loaded
15209 from a .mod file. */
15210 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
15212 gfc_formal_arglist *dummy_args;
15214 dummy_args = gfc_sym_get_dummy_args (proc);
15215 if (stree->n.tb->pass_arg)
15217 gfc_formal_arglist *i;
15219 /* If an explicit passing argument name is given, walk the arg-list
15220 and look for it. */
15222 me_arg = NULL;
15223 stree->n.tb->pass_arg_num = 1;
15224 for (i = dummy_args; i; i = i->next)
15226 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
15228 me_arg = i->sym;
15229 break;
15231 ++stree->n.tb->pass_arg_num;
15234 if (!me_arg)
15236 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
15237 " argument %qs",
15238 proc->name, stree->n.tb->pass_arg, &where,
15239 stree->n.tb->pass_arg);
15240 goto error;
15243 else
15245 /* Otherwise, take the first one; there should in fact be at least
15246 one. */
15247 stree->n.tb->pass_arg_num = 1;
15248 if (!dummy_args)
15250 gfc_error ("Procedure %qs with PASS at %L must have at"
15251 " least one argument", proc->name, &where);
15252 goto error;
15254 me_arg = dummy_args->sym;
15257 /* Now check that the argument-type matches and the passed-object
15258 dummy argument is generally fine. */
15260 gcc_assert (me_arg);
15262 if (me_arg->ts.type != BT_CLASS)
15264 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
15265 " at %L", proc->name, &where);
15266 goto error;
15269 /* The derived type is not a PDT template. Resolve as usual. */
15270 if (!resolve_bindings_derived->attr.pdt_template
15271 && (CLASS_DATA (me_arg)->ts.u.derived != resolve_bindings_derived))
15273 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
15274 "the derived-type %qs", me_arg->name, proc->name,
15275 me_arg->name, &where, resolve_bindings_derived->name);
15276 goto error;
15279 if (resolve_bindings_derived->attr.pdt_template
15280 && !gfc_pdt_is_instance_of (resolve_bindings_derived,
15281 CLASS_DATA (me_arg)->ts.u.derived))
15283 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
15284 "the parametric derived-type %qs", me_arg->name,
15285 proc->name, me_arg->name, &where,
15286 resolve_bindings_derived->name);
15287 goto error;
15290 if (resolve_bindings_derived->attr.pdt_template
15291 && gfc_pdt_is_instance_of (resolve_bindings_derived,
15292 CLASS_DATA (me_arg)->ts.u.derived)
15293 && (me_arg->param_list != NULL)
15294 && (gfc_spec_list_type (me_arg->param_list,
15295 CLASS_DATA(me_arg)->ts.u.derived)
15296 != SPEC_ASSUMED))
15299 /* Add a check to verify if there are any LEN parameters in the
15300 first place. If there are LEN parameters, throw this error.
15301 If there are only KIND parameters, then don't trigger
15302 this error. */
15303 gfc_component *c;
15304 bool seen_len_param = false;
15305 gfc_actual_arglist *me_arg_param = me_arg->param_list;
15307 for (; me_arg_param; me_arg_param = me_arg_param->next)
15309 c = gfc_find_component (CLASS_DATA(me_arg)->ts.u.derived,
15310 me_arg_param->name, true, true, NULL);
15312 gcc_assert (c != NULL);
15314 if (c->attr.pdt_kind)
15315 continue;
15317 /* Getting here implies that there is a pdt_len parameter
15318 in the list. */
15319 seen_len_param = true;
15320 break;
15323 if (seen_len_param)
15325 gfc_error ("All LEN type parameters of the passed dummy "
15326 "argument %qs of %qs at %L must be ASSUMED.",
15327 me_arg->name, proc->name, &where);
15328 goto error;
15332 gcc_assert (me_arg->ts.type == BT_CLASS);
15333 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
15335 gfc_error ("Passed-object dummy argument of %qs at %L must be"
15336 " scalar", proc->name, &where);
15337 goto error;
15339 if (CLASS_DATA (me_arg)->attr.allocatable)
15341 gfc_error ("Passed-object dummy argument of %qs at %L must not"
15342 " be ALLOCATABLE", proc->name, &where);
15343 goto error;
15345 if (CLASS_DATA (me_arg)->attr.class_pointer)
15347 gfc_error ("Passed-object dummy argument of %qs at %L must not"
15348 " be POINTER", proc->name, &where);
15349 goto error;
15353 /* If we are extending some type, check that we don't override a procedure
15354 flagged NON_OVERRIDABLE. */
15355 stree->n.tb->overridden = NULL;
15356 if (super_type)
15358 gfc_symtree* overridden;
15359 overridden = gfc_find_typebound_proc (super_type, NULL,
15360 stree->name, true, NULL);
15362 if (overridden)
15364 if (overridden->n.tb)
15365 stree->n.tb->overridden = overridden->n.tb;
15367 if (!gfc_check_typebound_override (stree, overridden))
15368 goto error;
15372 /* See if there's a name collision with a component directly in this type. */
15373 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
15374 if (!strcmp (comp->name, stree->name))
15376 gfc_error ("Procedure %qs at %L has the same name as a component of"
15377 " %qs",
15378 stree->name, &where, resolve_bindings_derived->name);
15379 goto error;
15382 /* Try to find a name collision with an inherited component. */
15383 if (super_type && gfc_find_component (super_type, stree->name, true, true,
15384 NULL))
15386 gfc_error ("Procedure %qs at %L has the same name as an inherited"
15387 " component of %qs",
15388 stree->name, &where, resolve_bindings_derived->name);
15389 goto error;
15392 stree->n.tb->error = 0;
15393 return;
15395 error:
15396 resolve_bindings_result = false;
15397 stree->n.tb->error = 1;
15401 static bool
15402 resolve_typebound_procedures (gfc_symbol* derived)
15404 int op;
15405 gfc_symbol* super_type;
15407 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
15408 return true;
15410 super_type = gfc_get_derived_super_type (derived);
15411 if (super_type)
15412 resolve_symbol (super_type);
15414 resolve_bindings_derived = derived;
15415 resolve_bindings_result = true;
15417 if (derived->f2k_derived->tb_sym_root)
15418 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
15419 &resolve_typebound_procedure);
15421 if (derived->f2k_derived->tb_uop_root)
15422 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
15423 &resolve_typebound_user_op);
15425 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
15427 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
15428 if (p && !resolve_typebound_intrinsic_op (derived,
15429 (gfc_intrinsic_op)op, p))
15430 resolve_bindings_result = false;
15433 return resolve_bindings_result;
15437 /* Add a derived type to the dt_list. The dt_list is used in trans-types.cc
15438 to give all identical derived types the same backend_decl. */
15439 static void
15440 add_dt_to_dt_list (gfc_symbol *derived)
15442 if (!derived->dt_next)
15444 if (gfc_derived_types)
15446 derived->dt_next = gfc_derived_types->dt_next;
15447 gfc_derived_types->dt_next = derived;
15449 else
15451 derived->dt_next = derived;
15453 gfc_derived_types = derived;
15458 /* Ensure that a derived-type is really not abstract, meaning that every
15459 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
15461 static bool
15462 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
15464 if (!st)
15465 return true;
15467 if (!ensure_not_abstract_walker (sub, st->left))
15468 return false;
15469 if (!ensure_not_abstract_walker (sub, st->right))
15470 return false;
15472 if (st->n.tb && st->n.tb->deferred)
15474 gfc_symtree* overriding;
15475 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
15476 if (!overriding)
15477 return false;
15478 gcc_assert (overriding->n.tb);
15479 if (overriding->n.tb->deferred)
15481 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
15482 " %qs is DEFERRED and not overridden",
15483 sub->name, &sub->declared_at, st->name);
15484 return false;
15488 return true;
15491 static bool
15492 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
15494 /* The algorithm used here is to recursively travel up the ancestry of sub
15495 and for each ancestor-type, check all bindings. If any of them is
15496 DEFERRED, look it up starting from sub and see if the found (overriding)
15497 binding is not DEFERRED.
15498 This is not the most efficient way to do this, but it should be ok and is
15499 clearer than something sophisticated. */
15501 gcc_assert (ancestor && !sub->attr.abstract);
15503 if (!ancestor->attr.abstract)
15504 return true;
15506 /* Walk bindings of this ancestor. */
15507 if (ancestor->f2k_derived)
15509 bool t;
15510 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
15511 if (!t)
15512 return false;
15515 /* Find next ancestor type and recurse on it. */
15516 ancestor = gfc_get_derived_super_type (ancestor);
15517 if (ancestor)
15518 return ensure_not_abstract (sub, ancestor);
15520 return true;
15524 /* This check for typebound defined assignments is done recursively
15525 since the order in which derived types are resolved is not always in
15526 order of the declarations. */
15528 static void
15529 check_defined_assignments (gfc_symbol *derived)
15531 gfc_component *c;
15533 for (c = derived->components; c; c = c->next)
15535 if (!gfc_bt_struct (c->ts.type)
15536 || c->attr.pointer
15537 || c->attr.proc_pointer_comp
15538 || c->attr.class_pointer
15539 || c->attr.proc_pointer)
15540 continue;
15542 if (c->ts.u.derived->attr.defined_assign_comp
15543 || (c->ts.u.derived->f2k_derived
15544 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
15546 derived->attr.defined_assign_comp = 1;
15547 return;
15550 if (c->attr.allocatable)
15551 continue;
15553 check_defined_assignments (c->ts.u.derived);
15554 if (c->ts.u.derived->attr.defined_assign_comp)
15556 derived->attr.defined_assign_comp = 1;
15557 return;
15563 /* Resolve a single component of a derived type or structure. */
15565 static bool
15566 resolve_component (gfc_component *c, gfc_symbol *sym)
15568 gfc_symbol *super_type;
15569 symbol_attribute *attr;
15571 if (c->attr.artificial)
15572 return true;
15574 /* Do not allow vtype components to be resolved in nameless namespaces
15575 such as block data because the procedure pointers will cause ICEs
15576 and vtables are not needed in these contexts. */
15577 if (sym->attr.vtype && sym->attr.use_assoc
15578 && sym->ns->proc_name == NULL)
15579 return true;
15581 /* F2008, C442. */
15582 if ((!sym->attr.is_class || c != sym->components)
15583 && c->attr.codimension
15584 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
15586 gfc_error ("Coarray component %qs at %L must be allocatable with "
15587 "deferred shape", c->name, &c->loc);
15588 return false;
15591 /* F2008, C443. */
15592 if (c->attr.codimension && c->ts.type == BT_DERIVED
15593 && c->ts.u.derived->ts.is_iso_c)
15595 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
15596 "shall not be a coarray", c->name, &c->loc);
15597 return false;
15600 /* F2008, C444. */
15601 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
15602 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
15603 || c->attr.allocatable))
15605 gfc_error ("Component %qs at %L with coarray component "
15606 "shall be a nonpointer, nonallocatable scalar",
15607 c->name, &c->loc);
15608 return false;
15611 /* F2008, C448. */
15612 if (c->ts.type == BT_CLASS)
15614 if (c->attr.class_ok && CLASS_DATA (c))
15616 attr = &(CLASS_DATA (c)->attr);
15618 /* Fix up contiguous attribute. */
15619 if (c->attr.contiguous)
15620 attr->contiguous = 1;
15622 else
15623 attr = NULL;
15625 else
15626 attr = &c->attr;
15628 if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
15630 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
15631 "is not an array pointer", c->name, &c->loc);
15632 return false;
15635 /* F2003, 15.2.1 - length has to be one. */
15636 if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
15637 && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
15638 || !gfc_is_constant_expr (c->ts.u.cl->length)
15639 || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
15641 gfc_error ("Component %qs of BIND(C) type at %L must have length one",
15642 c->name, &c->loc);
15643 return false;
15646 if (c->attr.proc_pointer && c->ts.interface)
15648 gfc_symbol *ifc = c->ts.interface;
15650 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
15652 c->tb->error = 1;
15653 return false;
15656 if (ifc->attr.if_source || ifc->attr.intrinsic)
15658 /* Resolve interface and copy attributes. */
15659 if (ifc->formal && !ifc->formal_ns)
15660 resolve_symbol (ifc);
15661 if (ifc->attr.intrinsic)
15662 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
15664 if (ifc->result)
15666 c->ts = ifc->result->ts;
15667 c->attr.allocatable = ifc->result->attr.allocatable;
15668 c->attr.pointer = ifc->result->attr.pointer;
15669 c->attr.dimension = ifc->result->attr.dimension;
15670 c->as = gfc_copy_array_spec (ifc->result->as);
15671 c->attr.class_ok = ifc->result->attr.class_ok;
15673 else
15675 c->ts = ifc->ts;
15676 c->attr.allocatable = ifc->attr.allocatable;
15677 c->attr.pointer = ifc->attr.pointer;
15678 c->attr.dimension = ifc->attr.dimension;
15679 c->as = gfc_copy_array_spec (ifc->as);
15680 c->attr.class_ok = ifc->attr.class_ok;
15682 c->ts.interface = ifc;
15683 c->attr.function = ifc->attr.function;
15684 c->attr.subroutine = ifc->attr.subroutine;
15686 c->attr.pure = ifc->attr.pure;
15687 c->attr.elemental = ifc->attr.elemental;
15688 c->attr.recursive = ifc->attr.recursive;
15689 c->attr.always_explicit = ifc->attr.always_explicit;
15690 c->attr.ext_attr |= ifc->attr.ext_attr;
15691 /* Copy char length. */
15692 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
15694 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
15695 if (cl->length && !cl->resolved
15696 && !gfc_resolve_expr (cl->length))
15698 c->tb->error = 1;
15699 return false;
15701 c->ts.u.cl = cl;
15705 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
15707 /* Since PPCs are not implicitly typed, a PPC without an explicit
15708 interface must be a subroutine. */
15709 gfc_add_subroutine (&c->attr, c->name, &c->loc);
15712 /* Procedure pointer components: Check PASS arg. */
15713 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
15714 && !sym->attr.vtype)
15716 gfc_symbol* me_arg;
15718 if (c->tb->pass_arg)
15720 gfc_formal_arglist* i;
15722 /* If an explicit passing argument name is given, walk the arg-list
15723 and look for it. */
15725 me_arg = NULL;
15726 c->tb->pass_arg_num = 1;
15727 for (i = c->ts.interface->formal; i; i = i->next)
15729 if (!strcmp (i->sym->name, c->tb->pass_arg))
15731 me_arg = i->sym;
15732 break;
15734 c->tb->pass_arg_num++;
15737 if (!me_arg)
15739 gfc_error ("Procedure pointer component %qs with PASS(%s) "
15740 "at %L has no argument %qs", c->name,
15741 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
15742 c->tb->error = 1;
15743 return false;
15746 else
15748 /* Otherwise, take the first one; there should in fact be at least
15749 one. */
15750 c->tb->pass_arg_num = 1;
15751 if (!c->ts.interface->formal)
15753 gfc_error ("Procedure pointer component %qs with PASS at %L "
15754 "must have at least one argument",
15755 c->name, &c->loc);
15756 c->tb->error = 1;
15757 return false;
15759 me_arg = c->ts.interface->formal->sym;
15762 /* Now check that the argument-type matches. */
15763 gcc_assert (me_arg);
15764 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
15765 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
15766 || (me_arg->ts.type == BT_CLASS
15767 && CLASS_DATA (me_arg)->ts.u.derived != sym))
15769 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
15770 " the derived type %qs", me_arg->name, c->name,
15771 me_arg->name, &c->loc, sym->name);
15772 c->tb->error = 1;
15773 return false;
15776 /* Check for F03:C453. */
15777 if (CLASS_DATA (me_arg)->attr.dimension)
15779 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
15780 "must be scalar", me_arg->name, c->name, me_arg->name,
15781 &c->loc);
15782 c->tb->error = 1;
15783 return false;
15786 if (CLASS_DATA (me_arg)->attr.class_pointer)
15788 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
15789 "may not have the POINTER attribute", me_arg->name,
15790 c->name, me_arg->name, &c->loc);
15791 c->tb->error = 1;
15792 return false;
15795 if (CLASS_DATA (me_arg)->attr.allocatable)
15797 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
15798 "may not be ALLOCATABLE", me_arg->name, c->name,
15799 me_arg->name, &c->loc);
15800 c->tb->error = 1;
15801 return false;
15804 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
15806 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
15807 " at %L", c->name, &c->loc);
15808 return false;
15813 /* Check type-spec if this is not the parent-type component. */
15814 if (((sym->attr.is_class
15815 && (!sym->components->ts.u.derived->attr.extension
15816 || c != CLASS_DATA (sym->components)))
15817 || (!sym->attr.is_class
15818 && (!sym->attr.extension || c != sym->components)))
15819 && !sym->attr.vtype
15820 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
15821 return false;
15823 super_type = gfc_get_derived_super_type (sym);
15825 /* If this type is an extension, set the accessibility of the parent
15826 component. */
15827 if (super_type
15828 && ((sym->attr.is_class
15829 && c == CLASS_DATA (sym->components))
15830 || (!sym->attr.is_class && c == sym->components))
15831 && strcmp (super_type->name, c->name) == 0)
15832 c->attr.access = super_type->attr.access;
15834 /* If this type is an extension, see if this component has the same name
15835 as an inherited type-bound procedure. */
15836 if (super_type && !sym->attr.is_class
15837 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
15839 gfc_error ("Component %qs of %qs at %L has the same name as an"
15840 " inherited type-bound procedure",
15841 c->name, sym->name, &c->loc);
15842 return false;
15845 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
15846 && !c->ts.deferred)
15848 if (c->ts.u.cl->length == NULL
15849 || (!resolve_charlen(c->ts.u.cl))
15850 || !gfc_is_constant_expr (c->ts.u.cl->length))
15852 gfc_error ("Character length of component %qs needs to "
15853 "be a constant specification expression at %L",
15854 c->name,
15855 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
15856 return false;
15859 if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
15861 if (!c->ts.u.cl->length->error)
15863 gfc_error ("Character length expression of component %qs at %L "
15864 "must be of INTEGER type, found %s",
15865 c->name, &c->ts.u.cl->length->where,
15866 gfc_basic_typename (c->ts.u.cl->length->ts.type));
15867 c->ts.u.cl->length->error = 1;
15869 return false;
15873 if (c->ts.type == BT_CHARACTER && c->ts.deferred
15874 && !c->attr.pointer && !c->attr.allocatable)
15876 gfc_error ("Character component %qs of %qs at %L with deferred "
15877 "length must be a POINTER or ALLOCATABLE",
15878 c->name, sym->name, &c->loc);
15879 return false;
15882 /* Add the hidden deferred length field. */
15883 if (c->ts.type == BT_CHARACTER
15884 && (c->ts.deferred || c->attr.pdt_string)
15885 && !c->attr.function
15886 && !sym->attr.is_class)
15888 char name[GFC_MAX_SYMBOL_LEN+9];
15889 gfc_component *strlen;
15890 sprintf (name, "_%s_length", c->name);
15891 strlen = gfc_find_component (sym, name, true, true, NULL);
15892 if (strlen == NULL)
15894 if (!gfc_add_component (sym, name, &strlen))
15895 return false;
15896 strlen->ts.type = BT_INTEGER;
15897 strlen->ts.kind = gfc_charlen_int_kind;
15898 strlen->attr.access = ACCESS_PRIVATE;
15899 strlen->attr.artificial = 1;
15903 if (c->ts.type == BT_DERIVED
15904 && sym->component_access != ACCESS_PRIVATE
15905 && gfc_check_symbol_access (sym)
15906 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
15907 && !c->ts.u.derived->attr.use_assoc
15908 && !gfc_check_symbol_access (c->ts.u.derived)
15909 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
15910 "PRIVATE type and cannot be a component of "
15911 "%qs, which is PUBLIC at %L", c->name,
15912 sym->name, &sym->declared_at))
15913 return false;
15915 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
15917 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
15918 "type %s", c->name, &c->loc, sym->name);
15919 return false;
15922 if (sym->attr.sequence)
15924 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
15926 gfc_error ("Component %s of SEQUENCE type declared at %L does "
15927 "not have the SEQUENCE attribute",
15928 c->ts.u.derived->name, &sym->declared_at);
15929 return false;
15933 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
15934 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
15935 else if (c->ts.type == BT_CLASS && c->attr.class_ok
15936 && CLASS_DATA (c)->ts.u.derived->attr.generic)
15937 CLASS_DATA (c)->ts.u.derived
15938 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
15940 /* If an allocatable component derived type is of the same type as
15941 the enclosing derived type, we need a vtable generating so that
15942 the __deallocate procedure is created. */
15943 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
15944 && c->ts.u.derived == sym && c->attr.allocatable == 1)
15945 gfc_find_vtab (&c->ts);
15947 /* Ensure that all the derived type components are put on the
15948 derived type list; even in formal namespaces, where derived type
15949 pointer components might not have been declared. */
15950 if (c->ts.type == BT_DERIVED
15951 && c->ts.u.derived
15952 && c->ts.u.derived->components
15953 && c->attr.pointer
15954 && sym != c->ts.u.derived)
15955 add_dt_to_dt_list (c->ts.u.derived);
15957 if (c->as && c->as->type != AS_DEFERRED
15958 && (c->attr.pointer || c->attr.allocatable))
15959 return false;
15961 if (!gfc_resolve_array_spec (c->as,
15962 !(c->attr.pointer || c->attr.proc_pointer
15963 || c->attr.allocatable)))
15964 return false;
15966 if (c->initializer && !sym->attr.vtype
15967 && !c->attr.pdt_kind && !c->attr.pdt_len
15968 && !gfc_check_assign_symbol (sym, c, c->initializer))
15969 return false;
15971 return true;
15975 /* Be nice about the locus for a structure expression - show the locus of the
15976 first non-null sub-expression if we can. */
15978 static locus *
15979 cons_where (gfc_expr *struct_expr)
15981 gfc_constructor *cons;
15983 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
15985 cons = gfc_constructor_first (struct_expr->value.constructor);
15986 for (; cons; cons = gfc_constructor_next (cons))
15988 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
15989 return &cons->expr->where;
15992 return &struct_expr->where;
15995 /* Resolve the components of a structure type. Much less work than derived
15996 types. */
15998 static bool
15999 resolve_fl_struct (gfc_symbol *sym)
16001 gfc_component *c;
16002 gfc_expr *init = NULL;
16003 bool success;
16005 /* Make sure UNIONs do not have overlapping initializers. */
16006 if (sym->attr.flavor == FL_UNION)
16008 for (c = sym->components; c; c = c->next)
16010 if (init && c->initializer)
16012 gfc_error ("Conflicting initializers in union at %L and %L",
16013 cons_where (init), cons_where (c->initializer));
16014 gfc_free_expr (c->initializer);
16015 c->initializer = NULL;
16017 if (init == NULL)
16018 init = c->initializer;
16022 success = true;
16023 for (c = sym->components; c; c = c->next)
16024 if (!resolve_component (c, sym))
16025 success = false;
16027 if (!success)
16028 return false;
16030 if (sym->components)
16031 add_dt_to_dt_list (sym);
16033 return true;
16037 /* Resolve the components of a derived type. This does not have to wait until
16038 resolution stage, but can be done as soon as the dt declaration has been
16039 parsed. */
16041 static bool
16042 resolve_fl_derived0 (gfc_symbol *sym)
16044 gfc_symbol* super_type;
16045 gfc_component *c;
16046 gfc_formal_arglist *f;
16047 bool success;
16049 if (sym->attr.unlimited_polymorphic)
16050 return true;
16052 super_type = gfc_get_derived_super_type (sym);
16054 /* F2008, C432. */
16055 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
16057 gfc_error ("As extending type %qs at %L has a coarray component, "
16058 "parent type %qs shall also have one", sym->name,
16059 &sym->declared_at, super_type->name);
16060 return false;
16063 /* Ensure the extended type gets resolved before we do. */
16064 if (super_type && !resolve_fl_derived0 (super_type))
16065 return false;
16067 /* An ABSTRACT type must be extensible. */
16068 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
16070 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
16071 sym->name, &sym->declared_at);
16072 return false;
16075 c = (sym->attr.is_class) ? CLASS_DATA (sym->components)
16076 : sym->components;
16078 success = true;
16079 for ( ; c != NULL; c = c->next)
16080 if (!resolve_component (c, sym))
16081 success = false;
16083 if (!success)
16084 return false;
16086 /* Now add the caf token field, where needed. */
16087 if (flag_coarray != GFC_FCOARRAY_NONE
16088 && !sym->attr.is_class && !sym->attr.vtype)
16090 for (c = sym->components; c; c = c->next)
16091 if (!c->attr.dimension && !c->attr.codimension
16092 && (c->attr.allocatable || c->attr.pointer))
16094 char name[GFC_MAX_SYMBOL_LEN+9];
16095 gfc_component *token;
16096 sprintf (name, "_caf_%s", c->name);
16097 token = gfc_find_component (sym, name, true, true, NULL);
16098 if (token == NULL)
16100 if (!gfc_add_component (sym, name, &token))
16101 return false;
16102 token->ts.type = BT_VOID;
16103 token->ts.kind = gfc_default_integer_kind;
16104 token->attr.access = ACCESS_PRIVATE;
16105 token->attr.artificial = 1;
16106 token->attr.caf_token = 1;
16111 check_defined_assignments (sym);
16113 if (!sym->attr.defined_assign_comp && super_type)
16114 sym->attr.defined_assign_comp
16115 = super_type->attr.defined_assign_comp;
16117 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
16118 all DEFERRED bindings are overridden. */
16119 if (super_type && super_type->attr.abstract && !sym->attr.abstract
16120 && !sym->attr.is_class
16121 && !ensure_not_abstract (sym, super_type))
16122 return false;
16124 /* Check that there is a component for every PDT parameter. */
16125 if (sym->attr.pdt_template)
16127 for (f = sym->formal; f; f = f->next)
16129 if (!f->sym)
16130 continue;
16131 c = gfc_find_component (sym, f->sym->name, true, true, NULL);
16132 if (c == NULL)
16134 gfc_error ("Parameterized type %qs does not have a component "
16135 "corresponding to parameter %qs at %L", sym->name,
16136 f->sym->name, &sym->declared_at);
16137 break;
16142 /* Add derived type to the derived type list. */
16143 add_dt_to_dt_list (sym);
16145 return true;
16149 /* The following procedure does the full resolution of a derived type,
16150 including resolution of all type-bound procedures (if present). In contrast
16151 to 'resolve_fl_derived0' this can only be done after the module has been
16152 parsed completely. */
16154 static bool
16155 resolve_fl_derived (gfc_symbol *sym)
16157 gfc_symbol *gen_dt = NULL;
16159 if (sym->attr.unlimited_polymorphic)
16160 return true;
16162 if (!sym->attr.is_class)
16163 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
16164 if (gen_dt && gen_dt->generic && gen_dt->generic->next
16165 && (!gen_dt->generic->sym->attr.use_assoc
16166 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
16167 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
16168 "%qs at %L being the same name as derived "
16169 "type at %L", sym->name,
16170 gen_dt->generic->sym == sym
16171 ? gen_dt->generic->next->sym->name
16172 : gen_dt->generic->sym->name,
16173 gen_dt->generic->sym == sym
16174 ? &gen_dt->generic->next->sym->declared_at
16175 : &gen_dt->generic->sym->declared_at,
16176 &sym->declared_at))
16177 return false;
16179 if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
16181 gfc_error ("Derived type %qs at %L has not been declared",
16182 sym->name, &sym->declared_at);
16183 return false;
16186 /* Resolve the finalizer procedures. */
16187 if (!gfc_resolve_finalizers (sym, NULL))
16188 return false;
16190 if (sym->attr.is_class && sym->ts.u.derived == NULL)
16192 /* Fix up incomplete CLASS symbols. */
16193 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
16194 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
16196 /* Nothing more to do for unlimited polymorphic entities. */
16197 if (data->ts.u.derived->attr.unlimited_polymorphic)
16199 add_dt_to_dt_list (sym);
16200 return true;
16202 else if (vptr->ts.u.derived == NULL)
16204 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
16205 gcc_assert (vtab);
16206 vptr->ts.u.derived = vtab->ts.u.derived;
16207 if (!resolve_fl_derived0 (vptr->ts.u.derived))
16208 return false;
16212 if (!resolve_fl_derived0 (sym))
16213 return false;
16215 /* Resolve the type-bound procedures. */
16216 if (!resolve_typebound_procedures (sym))
16217 return false;
16219 /* Generate module vtables subject to their accessibility and their not
16220 being vtables or pdt templates. If this is not done class declarations
16221 in external procedures wind up with their own version and so SELECT TYPE
16222 fails because the vptrs do not have the same address. */
16223 if (gfc_option.allow_std & GFC_STD_F2003
16224 && sym->ns->proc_name
16225 && sym->ns->proc_name->attr.flavor == FL_MODULE
16226 && sym->attr.access != ACCESS_PRIVATE
16227 && !(sym->attr.vtype || sym->attr.pdt_template))
16229 gfc_symbol *vtab = gfc_find_derived_vtab (sym);
16230 gfc_set_sym_referenced (vtab);
16233 return true;
16237 static bool
16238 resolve_fl_namelist (gfc_symbol *sym)
16240 gfc_namelist *nl;
16241 gfc_symbol *nlsym;
16243 for (nl = sym->namelist; nl; nl = nl->next)
16245 /* Check again, the check in match only works if NAMELIST comes
16246 after the decl. */
16247 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
16249 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
16250 "allowed", nl->sym->name, sym->name, &sym->declared_at);
16251 return false;
16254 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
16255 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
16256 "with assumed shape in namelist %qs at %L",
16257 nl->sym->name, sym->name, &sym->declared_at))
16258 return false;
16260 if (is_non_constant_shape_array (nl->sym)
16261 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
16262 "with nonconstant shape in namelist %qs at %L",
16263 nl->sym->name, sym->name, &sym->declared_at))
16264 return false;
16266 if (nl->sym->ts.type == BT_CHARACTER
16267 && (nl->sym->ts.u.cl->length == NULL
16268 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
16269 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
16270 "nonconstant character length in "
16271 "namelist %qs at %L", nl->sym->name,
16272 sym->name, &sym->declared_at))
16273 return false;
16277 /* Reject PRIVATE objects in a PUBLIC namelist. */
16278 if (gfc_check_symbol_access (sym))
16280 for (nl = sym->namelist; nl; nl = nl->next)
16282 if (!nl->sym->attr.use_assoc
16283 && !is_sym_host_assoc (nl->sym, sym->ns)
16284 && !gfc_check_symbol_access (nl->sym))
16286 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
16287 "cannot be member of PUBLIC namelist %qs at %L",
16288 nl->sym->name, sym->name, &sym->declared_at);
16289 return false;
16292 if (nl->sym->ts.type == BT_DERIVED
16293 && (nl->sym->ts.u.derived->attr.alloc_comp
16294 || nl->sym->ts.u.derived->attr.pointer_comp))
16296 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
16297 "namelist %qs at %L with ALLOCATABLE "
16298 "or POINTER components", nl->sym->name,
16299 sym->name, &sym->declared_at))
16300 return false;
16301 return true;
16304 /* Types with private components that came here by USE-association. */
16305 if (nl->sym->ts.type == BT_DERIVED
16306 && derived_inaccessible (nl->sym->ts.u.derived))
16308 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
16309 "components and cannot be member of namelist %qs at %L",
16310 nl->sym->name, sym->name, &sym->declared_at);
16311 return false;
16314 /* Types with private components that are defined in the same module. */
16315 if (nl->sym->ts.type == BT_DERIVED
16316 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
16317 && nl->sym->ts.u.derived->attr.private_comp)
16319 gfc_error ("NAMELIST object %qs has PRIVATE components and "
16320 "cannot be a member of PUBLIC namelist %qs at %L",
16321 nl->sym->name, sym->name, &sym->declared_at);
16322 return false;
16328 /* 14.1.2 A module or internal procedure represent local entities
16329 of the same type as a namelist member and so are not allowed. */
16330 for (nl = sym->namelist; nl; nl = nl->next)
16332 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
16333 continue;
16335 if (nl->sym->attr.function && nl->sym == nl->sym->result)
16336 if ((nl->sym == sym->ns->proc_name)
16338 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
16339 continue;
16341 nlsym = NULL;
16342 if (nl->sym->name)
16343 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
16344 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
16346 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
16347 "attribute in %qs at %L", nlsym->name,
16348 &sym->declared_at);
16349 return false;
16353 return true;
16357 static bool
16358 resolve_fl_parameter (gfc_symbol *sym)
16360 /* A parameter array's shape needs to be constant. */
16361 if (sym->as != NULL
16362 && (sym->as->type == AS_DEFERRED
16363 || is_non_constant_shape_array (sym)))
16365 gfc_error ("Parameter array %qs at %L cannot be automatic "
16366 "or of deferred shape", sym->name, &sym->declared_at);
16367 return false;
16370 /* Constraints on deferred type parameter. */
16371 if (!deferred_requirements (sym))
16372 return false;
16374 /* Make sure a parameter that has been implicitly typed still
16375 matches the implicit type, since PARAMETER statements can precede
16376 IMPLICIT statements. */
16377 if (sym->attr.implicit_type
16378 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
16379 sym->ns)))
16381 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
16382 "later IMPLICIT type", sym->name, &sym->declared_at);
16383 return false;
16386 /* Make sure the types of derived parameters are consistent. This
16387 type checking is deferred until resolution because the type may
16388 refer to a derived type from the host. */
16389 if (sym->ts.type == BT_DERIVED
16390 && !gfc_compare_types (&sym->ts, &sym->value->ts))
16392 gfc_error ("Incompatible derived type in PARAMETER at %L",
16393 &sym->value->where);
16394 return false;
16397 /* F03:C509,C514. */
16398 if (sym->ts.type == BT_CLASS)
16400 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
16401 sym->name, &sym->declared_at);
16402 return false;
16405 return true;
16409 /* Called by resolve_symbol to check PDTs. */
16411 static void
16412 resolve_pdt (gfc_symbol* sym)
16414 gfc_symbol *derived = NULL;
16415 gfc_actual_arglist *param;
16416 gfc_component *c;
16417 bool const_len_exprs = true;
16418 bool assumed_len_exprs = false;
16419 symbol_attribute *attr;
16421 if (sym->ts.type == BT_DERIVED)
16423 derived = sym->ts.u.derived;
16424 attr = &(sym->attr);
16426 else if (sym->ts.type == BT_CLASS)
16428 derived = CLASS_DATA (sym)->ts.u.derived;
16429 attr = &(CLASS_DATA (sym)->attr);
16431 else
16432 gcc_unreachable ();
16434 gcc_assert (derived->attr.pdt_type);
16436 for (param = sym->param_list; param; param = param->next)
16438 c = gfc_find_component (derived, param->name, false, true, NULL);
16439 gcc_assert (c);
16440 if (c->attr.pdt_kind)
16441 continue;
16443 if (param->expr && !gfc_is_constant_expr (param->expr)
16444 && c->attr.pdt_len)
16445 const_len_exprs = false;
16446 else if (param->spec_type == SPEC_ASSUMED)
16447 assumed_len_exprs = true;
16449 if (param->spec_type == SPEC_DEFERRED && !attr->allocatable
16450 && ((sym->ts.type == BT_DERIVED && !attr->pointer)
16451 || (sym->ts.type == BT_CLASS && !attr->class_pointer)))
16452 gfc_error ("Entity %qs at %L has a deferred LEN "
16453 "parameter %qs and requires either the POINTER "
16454 "or ALLOCATABLE attribute",
16455 sym->name, &sym->declared_at,
16456 param->name);
16460 if (!const_len_exprs
16461 && (sym->ns->proc_name->attr.is_main_program
16462 || sym->ns->proc_name->attr.flavor == FL_MODULE
16463 || sym->attr.save != SAVE_NONE))
16464 gfc_error ("The AUTOMATIC object %qs at %L must not have the "
16465 "SAVE attribute or be a variable declared in the "
16466 "main program, a module or a submodule(F08/C513)",
16467 sym->name, &sym->declared_at);
16469 if (assumed_len_exprs && !(sym->attr.dummy
16470 || sym->attr.select_type_temporary || sym->attr.associate_var))
16471 gfc_error ("The object %qs at %L with ASSUMED type parameters "
16472 "must be a dummy or a SELECT TYPE selector(F08/4.2)",
16473 sym->name, &sym->declared_at);
16477 /* Resolve the symbol's array spec. */
16479 static bool
16480 resolve_symbol_array_spec (gfc_symbol *sym, int check_constant)
16482 gfc_namespace *orig_current_ns = gfc_current_ns;
16483 gfc_current_ns = gfc_get_spec_ns (sym);
16485 bool saved_specification_expr = specification_expr;
16486 specification_expr = true;
16488 bool result = gfc_resolve_array_spec (sym->as, check_constant);
16490 specification_expr = saved_specification_expr;
16491 gfc_current_ns = orig_current_ns;
16493 return result;
16497 /* Do anything necessary to resolve a symbol. Right now, we just
16498 assume that an otherwise unknown symbol is a variable. This sort
16499 of thing commonly happens for symbols in module. */
16501 static void
16502 resolve_symbol (gfc_symbol *sym)
16504 int check_constant, mp_flag;
16505 gfc_symtree *symtree;
16506 gfc_symtree *this_symtree;
16507 gfc_namespace *ns;
16508 gfc_component *c;
16509 symbol_attribute class_attr;
16510 gfc_array_spec *as;
16512 if (sym->resolve_symbol_called >= 1)
16513 return;
16514 sym->resolve_symbol_called = 1;
16516 /* No symbol will ever have union type; only components can be unions.
16517 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
16518 (just like derived type declaration symbols have flavor FL_DERIVED). */
16519 gcc_assert (sym->ts.type != BT_UNION);
16521 /* Coarrayed polymorphic objects with allocatable or pointer components are
16522 yet unsupported for -fcoarray=lib. */
16523 if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
16524 && sym->ts.u.derived && CLASS_DATA (sym)
16525 && CLASS_DATA (sym)->attr.codimension
16526 && CLASS_DATA (sym)->ts.u.derived
16527 && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
16528 || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
16530 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
16531 "type coarrays at %L are unsupported", &sym->declared_at);
16532 return;
16535 if (sym->attr.artificial)
16536 return;
16538 if (sym->attr.unlimited_polymorphic)
16539 return;
16541 if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
16543 gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
16544 "the OpenMP DEPEND clause", &sym->declared_at);
16545 return;
16548 if (sym->attr.flavor == FL_UNKNOWN
16549 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
16550 && !sym->attr.generic && !sym->attr.external
16551 && sym->attr.if_source == IFSRC_UNKNOWN
16552 && sym->ts.type == BT_UNKNOWN))
16555 /* If we find that a flavorless symbol is an interface in one of the
16556 parent namespaces, find its symtree in this namespace, free the
16557 symbol and set the symtree to point to the interface symbol. */
16558 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
16560 symtree = gfc_find_symtree (ns->sym_root, sym->name);
16561 if (symtree && (symtree->n.sym->generic ||
16562 (symtree->n.sym->attr.flavor == FL_PROCEDURE
16563 && sym->ns->construct_entities)))
16565 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
16566 sym->name);
16567 if (this_symtree->n.sym == sym)
16569 symtree->n.sym->refs++;
16570 gfc_release_symbol (sym);
16571 this_symtree->n.sym = symtree->n.sym;
16572 return;
16577 /* Otherwise give it a flavor according to such attributes as
16578 it has. */
16579 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
16580 && sym->attr.intrinsic == 0)
16581 sym->attr.flavor = FL_VARIABLE;
16582 else if (sym->attr.flavor == FL_UNKNOWN)
16584 sym->attr.flavor = FL_PROCEDURE;
16585 if (sym->attr.dimension)
16586 sym->attr.function = 1;
16590 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
16591 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
16593 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
16594 && !resolve_procedure_interface (sym))
16595 return;
16597 if (sym->attr.is_protected && !sym->attr.proc_pointer
16598 && (sym->attr.procedure || sym->attr.external))
16600 if (sym->attr.external)
16601 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
16602 "at %L", &sym->declared_at);
16603 else
16604 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
16605 "at %L", &sym->declared_at);
16607 return;
16610 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
16611 return;
16613 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
16614 && !resolve_fl_struct (sym))
16615 return;
16617 /* Symbols that are module procedures with results (functions) have
16618 the types and array specification copied for type checking in
16619 procedures that call them, as well as for saving to a module
16620 file. These symbols can't stand the scrutiny that their results
16621 can. */
16622 mp_flag = (sym->result != NULL && sym->result != sym);
16624 /* Make sure that the intrinsic is consistent with its internal
16625 representation. This needs to be done before assigning a default
16626 type to avoid spurious warnings. */
16627 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
16628 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
16629 return;
16631 /* Resolve associate names. */
16632 if (sym->assoc)
16633 resolve_assoc_var (sym, true);
16635 /* Assign default type to symbols that need one and don't have one. */
16636 if (sym->ts.type == BT_UNKNOWN)
16638 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
16640 gfc_set_default_type (sym, 1, NULL);
16643 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
16644 && !sym->attr.function && !sym->attr.subroutine
16645 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
16646 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
16648 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
16650 /* The specific case of an external procedure should emit an error
16651 in the case that there is no implicit type. */
16652 if (!mp_flag)
16654 if (!sym->attr.mixed_entry_master)
16655 gfc_set_default_type (sym, sym->attr.external, NULL);
16657 else
16659 /* Result may be in another namespace. */
16660 resolve_symbol (sym->result);
16662 if (!sym->result->attr.proc_pointer)
16664 sym->ts = sym->result->ts;
16665 sym->as = gfc_copy_array_spec (sym->result->as);
16666 sym->attr.dimension = sym->result->attr.dimension;
16667 sym->attr.codimension = sym->result->attr.codimension;
16668 sym->attr.pointer = sym->result->attr.pointer;
16669 sym->attr.allocatable = sym->result->attr.allocatable;
16670 sym->attr.contiguous = sym->result->attr.contiguous;
16675 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
16676 resolve_symbol_array_spec (sym->result, false);
16678 /* For a CLASS-valued function with a result variable, affirm that it has
16679 been resolved also when looking at the symbol 'sym'. */
16680 if (mp_flag && sym->ts.type == BT_CLASS && sym->result->attr.class_ok)
16681 sym->attr.class_ok = sym->result->attr.class_ok;
16683 if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
16684 && CLASS_DATA (sym))
16686 as = CLASS_DATA (sym)->as;
16687 class_attr = CLASS_DATA (sym)->attr;
16688 class_attr.pointer = class_attr.class_pointer;
16690 else
16692 class_attr = sym->attr;
16693 as = sym->as;
16696 /* F2008, C530. */
16697 if (sym->attr.contiguous
16698 && (!class_attr.dimension
16699 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
16700 && !class_attr.pointer)))
16702 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
16703 "array pointer or an assumed-shape or assumed-rank array",
16704 sym->name, &sym->declared_at);
16705 return;
16708 /* Assumed size arrays and assumed shape arrays must be dummy
16709 arguments. Array-spec's of implied-shape should have been resolved to
16710 AS_EXPLICIT already. */
16712 if (as)
16714 /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
16715 specification expression. */
16716 if (as->type == AS_IMPLIED_SHAPE)
16718 int i;
16719 for (i=0; i<as->rank; i++)
16721 if (as->lower[i] != NULL && as->upper[i] == NULL)
16723 gfc_error ("Bad specification for assumed size array at %L",
16724 &as->lower[i]->where);
16725 return;
16728 gcc_unreachable();
16731 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
16732 || as->type == AS_ASSUMED_SHAPE)
16733 && !sym->attr.dummy && !sym->attr.select_type_temporary
16734 && !sym->attr.associate_var)
16736 if (as->type == AS_ASSUMED_SIZE)
16737 gfc_error ("Assumed size array at %L must be a dummy argument",
16738 &sym->declared_at);
16739 else
16740 gfc_error ("Assumed shape array at %L must be a dummy argument",
16741 &sym->declared_at);
16742 return;
16744 /* TS 29113, C535a. */
16745 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
16746 && !sym->attr.select_type_temporary
16747 && !(cs_base && cs_base->current
16748 && cs_base->current->op == EXEC_SELECT_RANK))
16750 gfc_error ("Assumed-rank array at %L must be a dummy argument",
16751 &sym->declared_at);
16752 return;
16754 if (as->type == AS_ASSUMED_RANK
16755 && (sym->attr.codimension || sym->attr.value))
16757 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
16758 "CODIMENSION attribute", &sym->declared_at);
16759 return;
16763 /* Make sure symbols with known intent or optional are really dummy
16764 variable. Because of ENTRY statement, this has to be deferred
16765 until resolution time. */
16767 if (!sym->attr.dummy
16768 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
16770 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
16771 return;
16774 if (sym->attr.value && !sym->attr.dummy)
16776 gfc_error ("%qs at %L cannot have the VALUE attribute because "
16777 "it is not a dummy argument", sym->name, &sym->declared_at);
16778 return;
16781 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
16783 gfc_charlen *cl = sym->ts.u.cl;
16784 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
16786 gfc_error ("Character dummy variable %qs at %L with VALUE "
16787 "attribute must have constant length",
16788 sym->name, &sym->declared_at);
16789 return;
16792 if (sym->ts.is_c_interop
16793 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
16795 gfc_error ("C interoperable character dummy variable %qs at %L "
16796 "with VALUE attribute must have length one",
16797 sym->name, &sym->declared_at);
16798 return;
16802 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
16803 && sym->ts.u.derived->attr.generic)
16805 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
16806 if (!sym->ts.u.derived)
16808 gfc_error ("The derived type %qs at %L is of type %qs, "
16809 "which has not been defined", sym->name,
16810 &sym->declared_at, sym->ts.u.derived->name);
16811 sym->ts.type = BT_UNKNOWN;
16812 return;
16816 /* Use the same constraints as TYPE(*), except for the type check
16817 and that only scalars and assumed-size arrays are permitted. */
16818 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
16820 if (!sym->attr.dummy)
16822 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
16823 "a dummy argument", sym->name, &sym->declared_at);
16824 return;
16827 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
16828 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
16829 && sym->ts.type != BT_COMPLEX)
16831 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
16832 "of type TYPE(*) or of an numeric intrinsic type",
16833 sym->name, &sym->declared_at);
16834 return;
16837 if (sym->attr.allocatable || sym->attr.codimension
16838 || sym->attr.pointer || sym->attr.value)
16840 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
16841 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
16842 "attribute", sym->name, &sym->declared_at);
16843 return;
16846 if (sym->attr.intent == INTENT_OUT)
16848 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
16849 "have the INTENT(OUT) attribute",
16850 sym->name, &sym->declared_at);
16851 return;
16853 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
16855 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
16856 "either be a scalar or an assumed-size array",
16857 sym->name, &sym->declared_at);
16858 return;
16861 /* Set the type to TYPE(*) and add a dimension(*) to ensure
16862 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
16863 packing. */
16864 sym->ts.type = BT_ASSUMED;
16865 sym->as = gfc_get_array_spec ();
16866 sym->as->type = AS_ASSUMED_SIZE;
16867 sym->as->rank = 1;
16868 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
16870 else if (sym->ts.type == BT_ASSUMED)
16872 /* TS 29113, C407a. */
16873 if (!sym->attr.dummy)
16875 gfc_error ("Assumed type of variable %s at %L is only permitted "
16876 "for dummy variables", sym->name, &sym->declared_at);
16877 return;
16879 if (sym->attr.allocatable || sym->attr.codimension
16880 || sym->attr.pointer || sym->attr.value)
16882 gfc_error ("Assumed-type variable %s at %L may not have the "
16883 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
16884 sym->name, &sym->declared_at);
16885 return;
16887 if (sym->attr.intent == INTENT_OUT)
16889 gfc_error ("Assumed-type variable %s at %L may not have the "
16890 "INTENT(OUT) attribute",
16891 sym->name, &sym->declared_at);
16892 return;
16894 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
16896 gfc_error ("Assumed-type variable %s at %L shall not be an "
16897 "explicit-shape array", sym->name, &sym->declared_at);
16898 return;
16902 /* If the symbol is marked as bind(c), that it is declared at module level
16903 scope and verify its type and kind. Do not do the latter for symbols
16904 that are implicitly typed because that is handled in
16905 gfc_set_default_type. Handle dummy arguments and procedure definitions
16906 separately. Also, anything that is use associated is not handled here
16907 but instead is handled in the module it is declared in. Finally, derived
16908 type definitions are allowed to be BIND(C) since that only implies that
16909 they're interoperable, and they are checked fully for interoperability
16910 when a variable is declared of that type. */
16911 if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
16912 && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
16913 && sym->attr.flavor != FL_DERIVED)
16915 bool t = true;
16917 /* First, make sure the variable is declared at the
16918 module-level scope (J3/04-007, Section 15.3). */
16919 if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
16920 && !sym->attr.in_common)
16922 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
16923 "is neither a COMMON block nor declared at the "
16924 "module level scope", sym->name, &(sym->declared_at));
16925 t = false;
16927 else if (sym->ts.type == BT_CHARACTER
16928 && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
16929 || !gfc_is_constant_expr (sym->ts.u.cl->length)
16930 || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
16932 gfc_error ("BIND(C) Variable %qs at %L must have length one",
16933 sym->name, &sym->declared_at);
16934 t = false;
16936 else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
16938 t = verify_com_block_vars_c_interop (sym->common_head);
16940 else if (sym->attr.implicit_type == 0)
16942 /* If type() declaration, we need to verify that the components
16943 of the given type are all C interoperable, etc. */
16944 if (sym->ts.type == BT_DERIVED &&
16945 sym->ts.u.derived->attr.is_c_interop != 1)
16947 /* Make sure the user marked the derived type as BIND(C). If
16948 not, call the verify routine. This could print an error
16949 for the derived type more than once if multiple variables
16950 of that type are declared. */
16951 if (sym->ts.u.derived->attr.is_bind_c != 1)
16952 verify_bind_c_derived_type (sym->ts.u.derived);
16953 t = false;
16956 /* Verify the variable itself as C interoperable if it
16957 is BIND(C). It is not possible for this to succeed if
16958 the verify_bind_c_derived_type failed, so don't have to handle
16959 any error returned by verify_bind_c_derived_type. */
16960 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
16961 sym->common_block);
16964 if (!t)
16966 /* clear the is_bind_c flag to prevent reporting errors more than
16967 once if something failed. */
16968 sym->attr.is_bind_c = 0;
16969 return;
16973 /* If a derived type symbol has reached this point, without its
16974 type being declared, we have an error. Notice that most
16975 conditions that produce undefined derived types have already
16976 been dealt with. However, the likes of:
16977 implicit type(t) (t) ..... call foo (t) will get us here if
16978 the type is not declared in the scope of the implicit
16979 statement. Change the type to BT_UNKNOWN, both because it is so
16980 and to prevent an ICE. */
16981 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
16982 && sym->ts.u.derived->components == NULL
16983 && !sym->ts.u.derived->attr.zero_comp)
16985 gfc_error ("The derived type %qs at %L is of type %qs, "
16986 "which has not been defined", sym->name,
16987 &sym->declared_at, sym->ts.u.derived->name);
16988 sym->ts.type = BT_UNKNOWN;
16989 return;
16992 /* Make sure that the derived type has been resolved and that the
16993 derived type is visible in the symbol's namespace, if it is a
16994 module function and is not PRIVATE. */
16995 if (sym->ts.type == BT_DERIVED
16996 && sym->ts.u.derived->attr.use_assoc
16997 && sym->ns->proc_name
16998 && sym->ns->proc_name->attr.flavor == FL_MODULE
16999 && !resolve_fl_derived (sym->ts.u.derived))
17000 return;
17002 /* Unless the derived-type declaration is use associated, Fortran 95
17003 does not allow public entries of private derived types.
17004 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
17005 161 in 95-006r3. */
17006 if (sym->ts.type == BT_DERIVED
17007 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
17008 && !sym->ts.u.derived->attr.use_assoc
17009 && gfc_check_symbol_access (sym)
17010 && !gfc_check_symbol_access (sym->ts.u.derived)
17011 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
17012 "derived type %qs",
17013 (sym->attr.flavor == FL_PARAMETER)
17014 ? "parameter" : "variable",
17015 sym->name, &sym->declared_at,
17016 sym->ts.u.derived->name))
17017 return;
17019 /* F2008, C1302. */
17020 if (sym->ts.type == BT_DERIVED
17021 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
17022 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
17023 || sym->ts.u.derived->attr.lock_comp)
17024 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
17026 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
17027 "type LOCK_TYPE must be a coarray", sym->name,
17028 &sym->declared_at);
17029 return;
17032 /* TS18508, C702/C703. */
17033 if (sym->ts.type == BT_DERIVED
17034 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
17035 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
17036 || sym->ts.u.derived->attr.event_comp)
17037 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
17039 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
17040 "type EVENT_TYPE must be a coarray", sym->name,
17041 &sym->declared_at);
17042 return;
17045 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
17046 default initialization is defined (5.1.2.4.4). */
17047 if (sym->ts.type == BT_DERIVED
17048 && sym->attr.dummy
17049 && sym->attr.intent == INTENT_OUT
17050 && sym->as
17051 && sym->as->type == AS_ASSUMED_SIZE)
17053 for (c = sym->ts.u.derived->components; c; c = c->next)
17055 if (c->initializer)
17057 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
17058 "ASSUMED SIZE and so cannot have a default initializer",
17059 sym->name, &sym->declared_at);
17060 return;
17065 /* F2008, C542. */
17066 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
17067 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
17069 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
17070 "INTENT(OUT)", sym->name, &sym->declared_at);
17071 return;
17074 /* TS18508. */
17075 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
17076 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
17078 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
17079 "INTENT(OUT)", sym->name, &sym->declared_at);
17080 return;
17083 /* F2008, C525. */
17084 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
17085 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
17086 && sym->ts.u.derived && CLASS_DATA (sym)
17087 && CLASS_DATA (sym)->attr.coarray_comp))
17088 || class_attr.codimension)
17089 && (sym->attr.result || sym->result == sym))
17091 gfc_error ("Function result %qs at %L shall not be a coarray or have "
17092 "a coarray component", sym->name, &sym->declared_at);
17093 return;
17096 /* F2008, C524. */
17097 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
17098 && sym->ts.u.derived->ts.is_iso_c)
17100 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
17101 "shall not be a coarray", sym->name, &sym->declared_at);
17102 return;
17105 /* F2008, C525. */
17106 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
17107 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
17108 && sym->ts.u.derived && CLASS_DATA (sym)
17109 && CLASS_DATA (sym)->attr.coarray_comp))
17110 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
17111 || class_attr.allocatable))
17113 gfc_error ("Variable %qs at %L with coarray component shall be a "
17114 "nonpointer, nonallocatable scalar, which is not a coarray",
17115 sym->name, &sym->declared_at);
17116 return;
17119 /* F2008, C526. The function-result case was handled above. */
17120 if (class_attr.codimension
17121 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
17122 || sym->attr.select_type_temporary
17123 || sym->attr.associate_var
17124 || (sym->ns->save_all && !sym->attr.automatic)
17125 || sym->ns->proc_name->attr.flavor == FL_MODULE
17126 || sym->ns->proc_name->attr.is_main_program
17127 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
17129 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
17130 "nor a dummy argument", sym->name, &sym->declared_at);
17131 return;
17133 /* F2008, C528. */
17134 else if (class_attr.codimension && !sym->attr.select_type_temporary
17135 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
17137 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
17138 "deferred shape without allocatable", sym->name,
17139 &sym->declared_at);
17140 return;
17142 else if (class_attr.codimension && class_attr.allocatable && as
17143 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
17145 gfc_error ("Allocatable coarray variable %qs at %L must have "
17146 "deferred shape", sym->name, &sym->declared_at);
17147 return;
17150 /* F2008, C541. */
17151 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
17152 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
17153 && sym->ts.u.derived && CLASS_DATA (sym)
17154 && CLASS_DATA (sym)->attr.coarray_comp))
17155 || (class_attr.codimension && class_attr.allocatable))
17156 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
17158 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
17159 "allocatable coarray or have coarray components",
17160 sym->name, &sym->declared_at);
17161 return;
17164 if (class_attr.codimension && sym->attr.dummy
17165 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
17167 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
17168 "procedure %qs", sym->name, &sym->declared_at,
17169 sym->ns->proc_name->name);
17170 return;
17173 if (sym->ts.type == BT_LOGICAL
17174 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
17175 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
17176 && sym->ns->proc_name->attr.is_bind_c)))
17178 int i;
17179 for (i = 0; gfc_logical_kinds[i].kind; i++)
17180 if (gfc_logical_kinds[i].kind == sym->ts.kind)
17181 break;
17182 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
17183 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
17184 "%L with non-C_Bool kind in BIND(C) procedure "
17185 "%qs", sym->name, &sym->declared_at,
17186 sym->ns->proc_name->name))
17187 return;
17188 else if (!gfc_logical_kinds[i].c_bool
17189 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
17190 "%qs at %L with non-C_Bool kind in "
17191 "BIND(C) procedure %qs", sym->name,
17192 &sym->declared_at,
17193 sym->attr.function ? sym->name
17194 : sym->ns->proc_name->name))
17195 return;
17198 switch (sym->attr.flavor)
17200 case FL_VARIABLE:
17201 if (!resolve_fl_variable (sym, mp_flag))
17202 return;
17203 break;
17205 case FL_PROCEDURE:
17206 if (sym->formal && !sym->formal_ns)
17208 /* Check that none of the arguments are a namelist. */
17209 gfc_formal_arglist *formal = sym->formal;
17211 for (; formal; formal = formal->next)
17212 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
17214 gfc_error ("Namelist %qs cannot be an argument to "
17215 "subroutine or function at %L",
17216 formal->sym->name, &sym->declared_at);
17217 return;
17221 if (!resolve_fl_procedure (sym, mp_flag))
17222 return;
17223 break;
17225 case FL_NAMELIST:
17226 if (!resolve_fl_namelist (sym))
17227 return;
17228 break;
17230 case FL_PARAMETER:
17231 if (!resolve_fl_parameter (sym))
17232 return;
17233 break;
17235 default:
17236 break;
17239 /* Resolve array specifier. Check as well some constraints
17240 on COMMON blocks. */
17242 check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;
17244 resolve_symbol_array_spec (sym, check_constant);
17246 /* Resolve formal namespaces. */
17247 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
17248 && !sym->attr.contained && !sym->attr.intrinsic)
17249 gfc_resolve (sym->formal_ns);
17251 /* Make sure the formal namespace is present. */
17252 if (sym->formal && !sym->formal_ns)
17254 gfc_formal_arglist *formal = sym->formal;
17255 while (formal && !formal->sym)
17256 formal = formal->next;
17258 if (formal)
17260 sym->formal_ns = formal->sym->ns;
17261 if (sym->formal_ns && sym->ns != formal->sym->ns)
17262 sym->formal_ns->refs++;
17266 /* Check threadprivate restrictions. */
17267 if (sym->attr.threadprivate
17268 && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
17269 && !(sym->ns->save_all && !sym->attr.automatic)
17270 && sym->module == NULL
17271 && (sym->ns->proc_name == NULL
17272 || (sym->ns->proc_name->attr.flavor != FL_MODULE
17273 && !sym->ns->proc_name->attr.is_main_program)))
17274 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
17276 /* Check omp declare target restrictions. */
17277 if (sym->attr.omp_declare_target
17278 && sym->attr.flavor == FL_VARIABLE
17279 && !sym->attr.save
17280 && !(sym->ns->save_all && !sym->attr.automatic)
17281 && (!sym->attr.in_common
17282 && sym->module == NULL
17283 && (sym->ns->proc_name == NULL
17284 || (sym->ns->proc_name->attr.flavor != FL_MODULE
17285 && !sym->ns->proc_name->attr.is_main_program))))
17286 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
17287 sym->name, &sym->declared_at);
17289 /* If we have come this far we can apply default-initializers, as
17290 described in 14.7.5, to those variables that have not already
17291 been assigned one. */
17292 if (sym->ts.type == BT_DERIVED
17293 && !sym->value
17294 && !sym->attr.allocatable
17295 && !sym->attr.alloc_comp)
17297 symbol_attribute *a = &sym->attr;
17299 if ((!a->save && !a->dummy && !a->pointer
17300 && !a->in_common && !a->use_assoc
17301 && a->referenced
17302 && !((a->function || a->result)
17303 && (!a->dimension
17304 || sym->ts.u.derived->attr.alloc_comp
17305 || sym->ts.u.derived->attr.pointer_comp))
17306 && !(a->function && sym != sym->result))
17307 || (a->dummy && !a->pointer && a->intent == INTENT_OUT
17308 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
17309 apply_default_init (sym);
17310 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
17311 && (sym->ts.u.derived->attr.alloc_comp
17312 || sym->ts.u.derived->attr.pointer_comp))
17313 /* Mark the result symbol to be referenced, when it has allocatable
17314 components. */
17315 sym->result->attr.referenced = 1;
17316 else if (a->function && !a->pointer && !a->allocatable && sym->result)
17317 /* Default initialization for function results. */
17318 apply_default_init (sym->result);
17321 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
17322 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
17323 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
17324 && !CLASS_DATA (sym)->attr.class_pointer
17325 && !CLASS_DATA (sym)->attr.allocatable)
17326 apply_default_init (sym);
17328 /* If this symbol has a type-spec, check it. */
17329 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
17330 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
17331 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
17332 return;
17334 if (sym->param_list)
17335 resolve_pdt (sym);
17339 /************* Resolve DATA statements *************/
17341 static struct
17343 gfc_data_value *vnode;
17344 mpz_t left;
17346 values;
17349 /* Advance the values structure to point to the next value in the data list. */
17351 static bool
17352 next_data_value (void)
17354 while (mpz_cmp_ui (values.left, 0) == 0)
17357 if (values.vnode->next == NULL)
17358 return false;
17360 values.vnode = values.vnode->next;
17361 mpz_set (values.left, values.vnode->repeat);
17364 return true;
17368 static bool
17369 check_data_variable (gfc_data_variable *var, locus *where)
17371 gfc_expr *e;
17372 mpz_t size;
17373 mpz_t offset;
17374 bool t;
17375 ar_type mark = AR_UNKNOWN;
17376 int i;
17377 mpz_t section_index[GFC_MAX_DIMENSIONS];
17378 int vector_offset[GFC_MAX_DIMENSIONS];
17379 gfc_ref *ref;
17380 gfc_array_ref *ar;
17381 gfc_symbol *sym;
17382 int has_pointer;
17384 if (!gfc_resolve_expr (var->expr))
17385 return false;
17387 ar = NULL;
17388 e = var->expr;
17390 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
17391 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
17392 e = e->value.function.actual->expr;
17394 if (e->expr_type != EXPR_VARIABLE)
17396 gfc_error ("Expecting definable entity near %L", where);
17397 return false;
17400 sym = e->symtree->n.sym;
17402 if (sym->ns->is_block_data && !sym->attr.in_common)
17404 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
17405 sym->name, &sym->declared_at);
17406 return false;
17409 if (e->ref == NULL && sym->as)
17411 gfc_error ("DATA array %qs at %L must be specified in a previous"
17412 " declaration", sym->name, where);
17413 return false;
17416 if (gfc_is_coindexed (e))
17418 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
17419 where);
17420 return false;
17423 has_pointer = sym->attr.pointer;
17425 for (ref = e->ref; ref; ref = ref->next)
17427 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
17428 has_pointer = 1;
17430 if (has_pointer)
17432 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
17434 gfc_error ("DATA element %qs at %L is a pointer and so must "
17435 "be a full array", sym->name, where);
17436 return false;
17439 if (values.vnode->expr->expr_type == EXPR_CONSTANT)
17441 gfc_error ("DATA object near %L has the pointer attribute "
17442 "and the corresponding DATA value is not a valid "
17443 "initial-data-target", where);
17444 return false;
17448 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
17450 gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
17451 "attribute", ref->u.c.component->name, &e->where);
17452 return false;
17455 /* Reject substrings of strings of non-constant length. */
17456 if (ref->type == REF_SUBSTRING
17457 && ref->u.ss.length
17458 && ref->u.ss.length->length
17459 && !gfc_is_constant_expr (ref->u.ss.length->length))
17460 goto bad_charlen;
17463 /* Reject strings with deferred length or non-constant length. */
17464 if (e->ts.type == BT_CHARACTER
17465 && (e->ts.deferred
17466 || (e->ts.u.cl->length
17467 && !gfc_is_constant_expr (e->ts.u.cl->length))))
17468 goto bad_charlen;
17470 mpz_init_set_si (offset, 0);
17472 if (e->rank == 0 || has_pointer)
17474 mpz_init_set_ui (size, 1);
17475 ref = NULL;
17477 else
17479 ref = e->ref;
17481 /* Find the array section reference. */
17482 for (ref = e->ref; ref; ref = ref->next)
17484 if (ref->type != REF_ARRAY)
17485 continue;
17486 if (ref->u.ar.type == AR_ELEMENT)
17487 continue;
17488 break;
17490 gcc_assert (ref);
17492 /* Set marks according to the reference pattern. */
17493 switch (ref->u.ar.type)
17495 case AR_FULL:
17496 mark = AR_FULL;
17497 break;
17499 case AR_SECTION:
17500 ar = &ref->u.ar;
17501 /* Get the start position of array section. */
17502 gfc_get_section_index (ar, section_index, &offset, vector_offset);
17503 mark = AR_SECTION;
17504 break;
17506 default:
17507 gcc_unreachable ();
17510 if (!gfc_array_size (e, &size))
17512 gfc_error ("Nonconstant array section at %L in DATA statement",
17513 where);
17514 mpz_clear (offset);
17515 return false;
17519 t = true;
17521 while (mpz_cmp_ui (size, 0) > 0)
17523 if (!next_data_value ())
17525 gfc_error ("DATA statement at %L has more variables than values",
17526 where);
17527 t = false;
17528 break;
17531 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
17532 if (!t)
17533 break;
17535 /* If we have more than one element left in the repeat count,
17536 and we have more than one element left in the target variable,
17537 then create a range assignment. */
17538 /* FIXME: Only done for full arrays for now, since array sections
17539 seem tricky. */
17540 if (mark == AR_FULL && ref && ref->next == NULL
17541 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
17543 mpz_t range;
17545 if (mpz_cmp (size, values.left) >= 0)
17547 mpz_init_set (range, values.left);
17548 mpz_sub (size, size, values.left);
17549 mpz_set_ui (values.left, 0);
17551 else
17553 mpz_init_set (range, size);
17554 mpz_sub (values.left, values.left, size);
17555 mpz_set_ui (size, 0);
17558 t = gfc_assign_data_value (var->expr, values.vnode->expr,
17559 offset, &range);
17561 mpz_add (offset, offset, range);
17562 mpz_clear (range);
17564 if (!t)
17565 break;
17568 /* Assign initial value to symbol. */
17569 else
17571 mpz_sub_ui (values.left, values.left, 1);
17572 mpz_sub_ui (size, size, 1);
17574 t = gfc_assign_data_value (var->expr, values.vnode->expr,
17575 offset, NULL);
17576 if (!t)
17577 break;
17579 if (mark == AR_FULL)
17580 mpz_add_ui (offset, offset, 1);
17582 /* Modify the array section indexes and recalculate the offset
17583 for next element. */
17584 else if (mark == AR_SECTION)
17585 gfc_advance_section (section_index, ar, &offset, vector_offset);
17589 if (mark == AR_SECTION)
17591 for (i = 0; i < ar->dimen; i++)
17592 mpz_clear (section_index[i]);
17595 mpz_clear (size);
17596 mpz_clear (offset);
17598 return t;
17600 bad_charlen:
17601 gfc_error ("Non-constant character length at %L in DATA statement",
17602 &e->where);
17603 return false;
17607 static bool traverse_data_var (gfc_data_variable *, locus *);
17609 /* Iterate over a list of elements in a DATA statement. */
17611 static bool
17612 traverse_data_list (gfc_data_variable *var, locus *where)
17614 mpz_t trip;
17615 iterator_stack frame;
17616 gfc_expr *e, *start, *end, *step;
17617 bool retval = true;
17619 mpz_init (frame.value);
17620 mpz_init (trip);
17622 start = gfc_copy_expr (var->iter.start);
17623 end = gfc_copy_expr (var->iter.end);
17624 step = gfc_copy_expr (var->iter.step);
17626 if (!gfc_simplify_expr (start, 1)
17627 || start->expr_type != EXPR_CONSTANT)
17629 gfc_error ("start of implied-do loop at %L could not be "
17630 "simplified to a constant value", &start->where);
17631 retval = false;
17632 goto cleanup;
17634 if (!gfc_simplify_expr (end, 1)
17635 || end->expr_type != EXPR_CONSTANT)
17637 gfc_error ("end of implied-do loop at %L could not be "
17638 "simplified to a constant value", &end->where);
17639 retval = false;
17640 goto cleanup;
17642 if (!gfc_simplify_expr (step, 1)
17643 || step->expr_type != EXPR_CONSTANT)
17645 gfc_error ("step of implied-do loop at %L could not be "
17646 "simplified to a constant value", &step->where);
17647 retval = false;
17648 goto cleanup;
17650 if (mpz_cmp_si (step->value.integer, 0) == 0)
17652 gfc_error ("step of implied-do loop at %L shall not be zero",
17653 &step->where);
17654 retval = false;
17655 goto cleanup;
17658 mpz_set (trip, end->value.integer);
17659 mpz_sub (trip, trip, start->value.integer);
17660 mpz_add (trip, trip, step->value.integer);
17662 mpz_div (trip, trip, step->value.integer);
17664 mpz_set (frame.value, start->value.integer);
17666 frame.prev = iter_stack;
17667 frame.variable = var->iter.var->symtree;
17668 iter_stack = &frame;
17670 while (mpz_cmp_ui (trip, 0) > 0)
17672 if (!traverse_data_var (var->list, where))
17674 retval = false;
17675 goto cleanup;
17678 e = gfc_copy_expr (var->expr);
17679 if (!gfc_simplify_expr (e, 1))
17681 gfc_free_expr (e);
17682 retval = false;
17683 goto cleanup;
17686 mpz_add (frame.value, frame.value, step->value.integer);
17688 mpz_sub_ui (trip, trip, 1);
17691 cleanup:
17692 mpz_clear (frame.value);
17693 mpz_clear (trip);
17695 gfc_free_expr (start);
17696 gfc_free_expr (end);
17697 gfc_free_expr (step);
17699 iter_stack = frame.prev;
17700 return retval;
17704 /* Type resolve variables in the variable list of a DATA statement. */
17706 static bool
17707 traverse_data_var (gfc_data_variable *var, locus *where)
17709 bool t;
17711 for (; var; var = var->next)
17713 if (var->expr == NULL)
17714 t = traverse_data_list (var, where);
17715 else
17716 t = check_data_variable (var, where);
17718 if (!t)
17719 return false;
17722 return true;
17726 /* Resolve the expressions and iterators associated with a data statement.
17727 This is separate from the assignment checking because data lists should
17728 only be resolved once. */
17730 static bool
17731 resolve_data_variables (gfc_data_variable *d)
17733 for (; d; d = d->next)
17735 if (d->list == NULL)
17737 if (!gfc_resolve_expr (d->expr))
17738 return false;
17740 else
17742 if (!gfc_resolve_iterator (&d->iter, false, true))
17743 return false;
17745 if (!resolve_data_variables (d->list))
17746 return false;
17750 return true;
17754 /* Resolve a single DATA statement. We implement this by storing a pointer to
17755 the value list into static variables, and then recursively traversing the
17756 variables list, expanding iterators and such. */
17758 static void
17759 resolve_data (gfc_data *d)
17762 if (!resolve_data_variables (d->var))
17763 return;
17765 values.vnode = d->value;
17766 if (d->value == NULL)
17767 mpz_set_ui (values.left, 0);
17768 else
17769 mpz_set (values.left, d->value->repeat);
17771 if (!traverse_data_var (d->var, &d->where))
17772 return;
17774 /* At this point, we better not have any values left. */
17776 if (next_data_value ())
17777 gfc_error ("DATA statement at %L has more values than variables",
17778 &d->where);
17782 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
17783 accessed by host or use association, is a dummy argument to a pure function,
17784 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
17785 is storage associated with any such variable, shall not be used in the
17786 following contexts: (clients of this function). */
17788 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
17789 procedure. Returns zero if assignment is OK, nonzero if there is a
17790 problem. */
17791 bool
17792 gfc_impure_variable (gfc_symbol *sym)
17794 gfc_symbol *proc;
17795 gfc_namespace *ns;
17797 if (sym->attr.use_assoc || sym->attr.in_common)
17798 return 1;
17800 /* Check if the symbol's ns is inside the pure procedure. */
17801 for (ns = gfc_current_ns; ns; ns = ns->parent)
17803 if (ns == sym->ns)
17804 break;
17805 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
17806 return 1;
17809 proc = sym->ns->proc_name;
17810 if (sym->attr.dummy
17811 && !sym->attr.value
17812 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
17813 || proc->attr.function))
17814 return 1;
17816 /* TODO: Sort out what can be storage associated, if anything, and include
17817 it here. In principle equivalences should be scanned but it does not
17818 seem to be possible to storage associate an impure variable this way. */
17819 return 0;
17823 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
17824 current namespace is inside a pure procedure. */
17826 bool
17827 gfc_pure (gfc_symbol *sym)
17829 symbol_attribute attr;
17830 gfc_namespace *ns;
17832 if (sym == NULL)
17834 /* Check if the current namespace or one of its parents
17835 belongs to a pure procedure. */
17836 for (ns = gfc_current_ns; ns; ns = ns->parent)
17838 sym = ns->proc_name;
17839 if (sym == NULL)
17840 return 0;
17841 attr = sym->attr;
17842 if (attr.flavor == FL_PROCEDURE && attr.pure)
17843 return 1;
17845 return 0;
17848 attr = sym->attr;
17850 return attr.flavor == FL_PROCEDURE && attr.pure;
17854 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
17855 checks if the current namespace is implicitly pure. Note that this
17856 function returns false for a PURE procedure. */
17858 bool
17859 gfc_implicit_pure (gfc_symbol *sym)
17861 gfc_namespace *ns;
17863 if (sym == NULL)
17865 /* Check if the current procedure is implicit_pure. Walk up
17866 the procedure list until we find a procedure. */
17867 for (ns = gfc_current_ns; ns; ns = ns->parent)
17869 sym = ns->proc_name;
17870 if (sym == NULL)
17871 return 0;
17873 if (sym->attr.flavor == FL_PROCEDURE)
17874 break;
17878 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
17879 && !sym->attr.pure;
17883 void
17884 gfc_unset_implicit_pure (gfc_symbol *sym)
17886 gfc_namespace *ns;
17888 if (sym == NULL)
17890 /* Check if the current procedure is implicit_pure. Walk up
17891 the procedure list until we find a procedure. */
17892 for (ns = gfc_current_ns; ns; ns = ns->parent)
17894 sym = ns->proc_name;
17895 if (sym == NULL)
17896 return;
17898 if (sym->attr.flavor == FL_PROCEDURE)
17899 break;
17903 if (sym->attr.flavor == FL_PROCEDURE)
17904 sym->attr.implicit_pure = 0;
17905 else
17906 sym->attr.pure = 0;
17910 /* Test whether the current procedure is elemental or not. */
17912 bool
17913 gfc_elemental (gfc_symbol *sym)
17915 symbol_attribute attr;
17917 if (sym == NULL)
17918 sym = gfc_current_ns->proc_name;
17919 if (sym == NULL)
17920 return 0;
17921 attr = sym->attr;
17923 return attr.flavor == FL_PROCEDURE && attr.elemental;
17927 /* Warn about unused labels. */
17929 static void
17930 warn_unused_fortran_label (gfc_st_label *label)
17932 if (label == NULL)
17933 return;
17935 warn_unused_fortran_label (label->left);
17937 if (label->defined == ST_LABEL_UNKNOWN)
17938 return;
17940 switch (label->referenced)
17942 case ST_LABEL_UNKNOWN:
17943 gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
17944 label->value, &label->where);
17945 break;
17947 case ST_LABEL_BAD_TARGET:
17948 gfc_warning (OPT_Wunused_label,
17949 "Label %d at %L defined but cannot be used",
17950 label->value, &label->where);
17951 break;
17953 default:
17954 break;
17957 warn_unused_fortran_label (label->right);
17961 /* Returns the sequence type of a symbol or sequence. */
17963 static seq_type
17964 sequence_type (gfc_typespec ts)
17966 seq_type result;
17967 gfc_component *c;
17969 switch (ts.type)
17971 case BT_DERIVED:
17973 if (ts.u.derived->components == NULL)
17974 return SEQ_NONDEFAULT;
17976 result = sequence_type (ts.u.derived->components->ts);
17977 for (c = ts.u.derived->components->next; c; c = c->next)
17978 if (sequence_type (c->ts) != result)
17979 return SEQ_MIXED;
17981 return result;
17983 case BT_CHARACTER:
17984 if (ts.kind != gfc_default_character_kind)
17985 return SEQ_NONDEFAULT;
17987 return SEQ_CHARACTER;
17989 case BT_INTEGER:
17990 if (ts.kind != gfc_default_integer_kind)
17991 return SEQ_NONDEFAULT;
17993 return SEQ_NUMERIC;
17995 case BT_REAL:
17996 if (!(ts.kind == gfc_default_real_kind
17997 || ts.kind == gfc_default_double_kind))
17998 return SEQ_NONDEFAULT;
18000 return SEQ_NUMERIC;
18002 case BT_COMPLEX:
18003 if (ts.kind != gfc_default_complex_kind)
18004 return SEQ_NONDEFAULT;
18006 return SEQ_NUMERIC;
18008 case BT_LOGICAL:
18009 if (ts.kind != gfc_default_logical_kind)
18010 return SEQ_NONDEFAULT;
18012 return SEQ_NUMERIC;
18014 default:
18015 return SEQ_NONDEFAULT;
18020 /* Resolve derived type EQUIVALENCE object. */
18022 static bool
18023 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
18025 gfc_component *c = derived->components;
18027 if (!derived)
18028 return true;
18030 /* Shall not be an object of nonsequence derived type. */
18031 if (!derived->attr.sequence)
18033 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
18034 "attribute to be an EQUIVALENCE object", sym->name,
18035 &e->where);
18036 return false;
18039 /* Shall not have allocatable components. */
18040 if (derived->attr.alloc_comp)
18042 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
18043 "components to be an EQUIVALENCE object",sym->name,
18044 &e->where);
18045 return false;
18048 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
18050 gfc_error ("Derived type variable %qs at %L with default "
18051 "initialization cannot be in EQUIVALENCE with a variable "
18052 "in COMMON", sym->name, &e->where);
18053 return false;
18056 for (; c ; c = c->next)
18058 if (gfc_bt_struct (c->ts.type)
18059 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
18060 return false;
18062 /* Shall not be an object of sequence derived type containing a pointer
18063 in the structure. */
18064 if (c->attr.pointer)
18066 gfc_error ("Derived type variable %qs at %L with pointer "
18067 "component(s) cannot be an EQUIVALENCE object",
18068 sym->name, &e->where);
18069 return false;
18072 return true;
18076 /* Resolve equivalence object.
18077 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
18078 an allocatable array, an object of nonsequence derived type, an object of
18079 sequence derived type containing a pointer at any level of component
18080 selection, an automatic object, a function name, an entry name, a result
18081 name, a named constant, a structure component, or a subobject of any of
18082 the preceding objects. A substring shall not have length zero. A
18083 derived type shall not have components with default initialization nor
18084 shall two objects of an equivalence group be initialized.
18085 Either all or none of the objects shall have an protected attribute.
18086 The simple constraints are done in symbol.cc(check_conflict) and the rest
18087 are implemented here. */
18089 static void
18090 resolve_equivalence (gfc_equiv *eq)
18092 gfc_symbol *sym;
18093 gfc_symbol *first_sym;
18094 gfc_expr *e;
18095 gfc_ref *r;
18096 locus *last_where = NULL;
18097 seq_type eq_type, last_eq_type;
18098 gfc_typespec *last_ts;
18099 int object, cnt_protected;
18100 const char *msg;
18102 last_ts = &eq->expr->symtree->n.sym->ts;
18104 first_sym = eq->expr->symtree->n.sym;
18106 cnt_protected = 0;
18108 for (object = 1; eq; eq = eq->eq, object++)
18110 e = eq->expr;
18112 e->ts = e->symtree->n.sym->ts;
18113 /* match_varspec might not know yet if it is seeing
18114 array reference or substring reference, as it doesn't
18115 know the types. */
18116 if (e->ref && e->ref->type == REF_ARRAY)
18118 gfc_ref *ref = e->ref;
18119 sym = e->symtree->n.sym;
18121 if (sym->attr.dimension)
18123 ref->u.ar.as = sym->as;
18124 ref = ref->next;
18127 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
18128 if (e->ts.type == BT_CHARACTER
18129 && ref
18130 && ref->type == REF_ARRAY
18131 && ref->u.ar.dimen == 1
18132 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
18133 && ref->u.ar.stride[0] == NULL)
18135 gfc_expr *start = ref->u.ar.start[0];
18136 gfc_expr *end = ref->u.ar.end[0];
18137 void *mem = NULL;
18139 /* Optimize away the (:) reference. */
18140 if (start == NULL && end == NULL)
18142 if (e->ref == ref)
18143 e->ref = ref->next;
18144 else
18145 e->ref->next = ref->next;
18146 mem = ref;
18148 else
18150 ref->type = REF_SUBSTRING;
18151 if (start == NULL)
18152 start = gfc_get_int_expr (gfc_charlen_int_kind,
18153 NULL, 1);
18154 ref->u.ss.start = start;
18155 if (end == NULL && e->ts.u.cl)
18156 end = gfc_copy_expr (e->ts.u.cl->length);
18157 ref->u.ss.end = end;
18158 ref->u.ss.length = e->ts.u.cl;
18159 e->ts.u.cl = NULL;
18161 ref = ref->next;
18162 free (mem);
18165 /* Any further ref is an error. */
18166 if (ref)
18168 gcc_assert (ref->type == REF_ARRAY);
18169 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
18170 &ref->u.ar.where);
18171 continue;
18175 if (!gfc_resolve_expr (e))
18176 continue;
18178 sym = e->symtree->n.sym;
18180 if (sym->attr.is_protected)
18181 cnt_protected++;
18182 if (cnt_protected > 0 && cnt_protected != object)
18184 gfc_error ("Either all or none of the objects in the "
18185 "EQUIVALENCE set at %L shall have the "
18186 "PROTECTED attribute",
18187 &e->where);
18188 break;
18191 /* Shall not equivalence common block variables in a PURE procedure. */
18192 if (sym->ns->proc_name
18193 && sym->ns->proc_name->attr.pure
18194 && sym->attr.in_common)
18196 /* Need to check for symbols that may have entered the pure
18197 procedure via a USE statement. */
18198 bool saw_sym = false;
18199 if (sym->ns->use_stmts)
18201 gfc_use_rename *r;
18202 for (r = sym->ns->use_stmts->rename; r; r = r->next)
18203 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
18205 else
18206 saw_sym = true;
18208 if (saw_sym)
18209 gfc_error ("COMMON block member %qs at %L cannot be an "
18210 "EQUIVALENCE object in the pure procedure %qs",
18211 sym->name, &e->where, sym->ns->proc_name->name);
18212 break;
18215 /* Shall not be a named constant. */
18216 if (e->expr_type == EXPR_CONSTANT)
18218 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
18219 "object", sym->name, &e->where);
18220 continue;
18223 if (e->ts.type == BT_DERIVED
18224 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
18225 continue;
18227 /* Check that the types correspond correctly:
18228 Note 5.28:
18229 A numeric sequence structure may be equivalenced to another sequence
18230 structure, an object of default integer type, default real type, double
18231 precision real type, default logical type such that components of the
18232 structure ultimately only become associated to objects of the same
18233 kind. A character sequence structure may be equivalenced to an object
18234 of default character kind or another character sequence structure.
18235 Other objects may be equivalenced only to objects of the same type and
18236 kind parameters. */
18238 /* Identical types are unconditionally OK. */
18239 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
18240 goto identical_types;
18242 last_eq_type = sequence_type (*last_ts);
18243 eq_type = sequence_type (sym->ts);
18245 /* Since the pair of objects is not of the same type, mixed or
18246 non-default sequences can be rejected. */
18248 msg = "Sequence %s with mixed components in EQUIVALENCE "
18249 "statement at %L with different type objects";
18250 if ((object ==2
18251 && last_eq_type == SEQ_MIXED
18252 && last_where
18253 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
18254 || (eq_type == SEQ_MIXED
18255 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
18256 continue;
18258 msg = "Non-default type object or sequence %s in EQUIVALENCE "
18259 "statement at %L with objects of different type";
18260 if ((object ==2
18261 && last_eq_type == SEQ_NONDEFAULT
18262 && last_where
18263 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
18264 || (eq_type == SEQ_NONDEFAULT
18265 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
18266 continue;
18268 msg ="Non-CHARACTER object %qs in default CHARACTER "
18269 "EQUIVALENCE statement at %L";
18270 if (last_eq_type == SEQ_CHARACTER
18271 && eq_type != SEQ_CHARACTER
18272 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
18273 continue;
18275 msg ="Non-NUMERIC object %qs in default NUMERIC "
18276 "EQUIVALENCE statement at %L";
18277 if (last_eq_type == SEQ_NUMERIC
18278 && eq_type != SEQ_NUMERIC
18279 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
18280 continue;
18282 identical_types:
18284 last_ts =&sym->ts;
18285 last_where = &e->where;
18287 if (!e->ref)
18288 continue;
18290 /* Shall not be an automatic array. */
18291 if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
18293 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
18294 "an EQUIVALENCE object", sym->name, &e->where);
18295 continue;
18298 r = e->ref;
18299 while (r)
18301 /* Shall not be a structure component. */
18302 if (r->type == REF_COMPONENT)
18304 gfc_error ("Structure component %qs at %L cannot be an "
18305 "EQUIVALENCE object",
18306 r->u.c.component->name, &e->where);
18307 break;
18310 /* A substring shall not have length zero. */
18311 if (r->type == REF_SUBSTRING)
18313 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
18315 gfc_error ("Substring at %L has length zero",
18316 &r->u.ss.start->where);
18317 break;
18320 r = r->next;
18326 /* Function called by resolve_fntype to flag other symbols used in the
18327 length type parameter specification of function results. */
18329 static bool
18330 flag_fn_result_spec (gfc_expr *expr,
18331 gfc_symbol *sym,
18332 int *f ATTRIBUTE_UNUSED)
18334 gfc_namespace *ns;
18335 gfc_symbol *s;
18337 if (expr->expr_type == EXPR_VARIABLE)
18339 s = expr->symtree->n.sym;
18340 for (ns = s->ns; ns; ns = ns->parent)
18341 if (!ns->parent)
18342 break;
18344 if (sym == s)
18346 gfc_error ("Self reference in character length expression "
18347 "for %qs at %L", sym->name, &expr->where);
18348 return true;
18351 if (!s->fn_result_spec
18352 && s->attr.flavor == FL_PARAMETER)
18354 /* Function contained in a module.... */
18355 if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
18357 gfc_symtree *st;
18358 s->fn_result_spec = 1;
18359 /* Make sure that this symbol is translated as a module
18360 variable. */
18361 st = gfc_get_unique_symtree (ns);
18362 st->n.sym = s;
18363 s->refs++;
18365 /* ... which is use associated and called. */
18366 else if (s->attr.use_assoc || s->attr.used_in_submodule
18368 /* External function matched with an interface. */
18369 (s->ns->proc_name
18370 && ((s->ns == ns
18371 && s->ns->proc_name->attr.if_source == IFSRC_DECL)
18372 || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
18373 && s->ns->proc_name->attr.function))
18374 s->fn_result_spec = 1;
18377 return false;
18381 /* Resolve function and ENTRY types, issue diagnostics if needed. */
18383 static void
18384 resolve_fntype (gfc_namespace *ns)
18386 gfc_entry_list *el;
18387 gfc_symbol *sym;
18389 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
18390 return;
18392 /* If there are any entries, ns->proc_name is the entry master
18393 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
18394 if (ns->entries)
18395 sym = ns->entries->sym;
18396 else
18397 sym = ns->proc_name;
18398 if (sym->result == sym
18399 && sym->ts.type == BT_UNKNOWN
18400 && !gfc_set_default_type (sym, 0, NULL)
18401 && !sym->attr.untyped)
18403 gfc_error ("Function %qs at %L has no IMPLICIT type",
18404 sym->name, &sym->declared_at);
18405 sym->attr.untyped = 1;
18408 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
18409 && !sym->attr.contained
18410 && !gfc_check_symbol_access (sym->ts.u.derived)
18411 && gfc_check_symbol_access (sym))
18413 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
18414 "%L of PRIVATE type %qs", sym->name,
18415 &sym->declared_at, sym->ts.u.derived->name);
18418 if (ns->entries)
18419 for (el = ns->entries->next; el; el = el->next)
18421 if (el->sym->result == el->sym
18422 && el->sym->ts.type == BT_UNKNOWN
18423 && !gfc_set_default_type (el->sym, 0, NULL)
18424 && !el->sym->attr.untyped)
18426 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
18427 el->sym->name, &el->sym->declared_at);
18428 el->sym->attr.untyped = 1;
18432 if (sym->ts.type == BT_CHARACTER
18433 && sym->ts.u.cl->length
18434 && sym->ts.u.cl->length->ts.type == BT_INTEGER)
18435 gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
18439 /* 12.3.2.1.1 Defined operators. */
18441 static bool
18442 check_uop_procedure (gfc_symbol *sym, locus where)
18444 gfc_formal_arglist *formal;
18446 if (!sym->attr.function)
18448 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
18449 sym->name, &where);
18450 return false;
18453 if (sym->ts.type == BT_CHARACTER
18454 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
18455 && !(sym->result && ((sym->result->ts.u.cl
18456 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
18458 gfc_error ("User operator procedure %qs at %L cannot be assumed "
18459 "character length", sym->name, &where);
18460 return false;
18463 formal = gfc_sym_get_dummy_args (sym);
18464 if (!formal || !formal->sym)
18466 gfc_error ("User operator procedure %qs at %L must have at least "
18467 "one argument", sym->name, &where);
18468 return false;
18471 if (formal->sym->attr.intent != INTENT_IN)
18473 gfc_error ("First argument of operator interface at %L must be "
18474 "INTENT(IN)", &where);
18475 return false;
18478 if (formal->sym->attr.optional)
18480 gfc_error ("First argument of operator interface at %L cannot be "
18481 "optional", &where);
18482 return false;
18485 formal = formal->next;
18486 if (!formal || !formal->sym)
18487 return true;
18489 if (formal->sym->attr.intent != INTENT_IN)
18491 gfc_error ("Second argument of operator interface at %L must be "
18492 "INTENT(IN)", &where);
18493 return false;
18496 if (formal->sym->attr.optional)
18498 gfc_error ("Second argument of operator interface at %L cannot be "
18499 "optional", &where);
18500 return false;
18503 if (formal->next)
18505 gfc_error ("Operator interface at %L must have, at most, two "
18506 "arguments", &where);
18507 return false;
18510 return true;
18513 static void
18514 gfc_resolve_uops (gfc_symtree *symtree)
18516 gfc_interface *itr;
18518 if (symtree == NULL)
18519 return;
18521 gfc_resolve_uops (symtree->left);
18522 gfc_resolve_uops (symtree->right);
18524 for (itr = symtree->n.uop->op; itr; itr = itr->next)
18525 check_uop_procedure (itr->sym, itr->sym->declared_at);
18529 /* Examine all of the expressions associated with a program unit,
18530 assign types to all intermediate expressions, make sure that all
18531 assignments are to compatible types and figure out which names
18532 refer to which functions or subroutines. It doesn't check code
18533 block, which is handled by gfc_resolve_code. */
18535 static void
18536 resolve_types (gfc_namespace *ns)
18538 gfc_namespace *n;
18539 gfc_charlen *cl;
18540 gfc_data *d;
18541 gfc_equiv *eq;
18542 gfc_namespace* old_ns = gfc_current_ns;
18543 bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
18545 if (ns->types_resolved)
18546 return;
18548 /* Check that all IMPLICIT types are ok. */
18549 if (!ns->seen_implicit_none)
18551 unsigned letter;
18552 for (letter = 0; letter != GFC_LETTERS; ++letter)
18553 if (ns->set_flag[letter]
18554 && !resolve_typespec_used (&ns->default_type[letter],
18555 &ns->implicit_loc[letter], NULL))
18556 return;
18559 gfc_current_ns = ns;
18561 resolve_entries (ns);
18563 resolve_common_vars (&ns->blank_common, false);
18564 resolve_common_blocks (ns->common_root);
18566 resolve_contained_functions (ns);
18568 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
18569 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
18570 gfc_resolve_formal_arglist (ns->proc_name);
18572 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
18574 for (cl = ns->cl_list; cl; cl = cl->next)
18575 resolve_charlen (cl);
18577 gfc_traverse_ns (ns, resolve_symbol);
18579 resolve_fntype (ns);
18581 for (n = ns->contained; n; n = n->sibling)
18583 /* Exclude final wrappers with the test for the artificial attribute. */
18584 if (gfc_pure (ns->proc_name)
18585 && !gfc_pure (n->proc_name)
18586 && !n->proc_name->attr.artificial)
18587 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
18588 "also be PURE", n->proc_name->name,
18589 &n->proc_name->declared_at);
18591 resolve_types (n);
18594 forall_flag = 0;
18595 gfc_do_concurrent_flag = 0;
18596 gfc_check_interfaces (ns);
18598 gfc_traverse_ns (ns, resolve_values);
18600 if (ns->save_all || (!flag_automatic && !recursive))
18601 gfc_save_all (ns);
18603 iter_stack = NULL;
18604 for (d = ns->data; d; d = d->next)
18605 resolve_data (d);
18607 iter_stack = NULL;
18608 gfc_traverse_ns (ns, gfc_formalize_init_value);
18610 gfc_traverse_ns (ns, gfc_verify_binding_labels);
18612 for (eq = ns->equiv; eq; eq = eq->next)
18613 resolve_equivalence (eq);
18615 /* Warn about unused labels. */
18616 if (warn_unused_label)
18617 warn_unused_fortran_label (ns->st_labels);
18619 gfc_resolve_uops (ns->uop_root);
18621 gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
18623 gfc_resolve_omp_declare_simd (ns);
18625 gfc_resolve_omp_udrs (ns->omp_udr_root);
18627 ns->types_resolved = 1;
18629 gfc_current_ns = old_ns;
18633 /* Call gfc_resolve_code recursively. */
18635 static void
18636 resolve_codes (gfc_namespace *ns)
18638 gfc_namespace *n;
18639 bitmap_obstack old_obstack;
18641 if (ns->resolved == 1)
18642 return;
18644 for (n = ns->contained; n; n = n->sibling)
18645 resolve_codes (n);
18647 gfc_current_ns = ns;
18649 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
18650 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
18651 cs_base = NULL;
18653 /* Set to an out of range value. */
18654 current_entry_id = -1;
18656 old_obstack = labels_obstack;
18657 bitmap_obstack_initialize (&labels_obstack);
18659 gfc_resolve_oacc_declare (ns);
18660 gfc_resolve_oacc_routines (ns);
18661 gfc_resolve_omp_local_vars (ns);
18662 if (ns->omp_allocate)
18663 gfc_resolve_omp_allocate (ns, ns->omp_allocate);
18664 gfc_resolve_code (ns->code, ns);
18666 bitmap_obstack_release (&labels_obstack);
18667 labels_obstack = old_obstack;
18671 /* This function is called after a complete program unit has been compiled.
18672 Its purpose is to examine all of the expressions associated with a program
18673 unit, assign types to all intermediate expressions, make sure that all
18674 assignments are to compatible types and figure out which names refer to
18675 which functions or subroutines. */
18677 void
18678 gfc_resolve (gfc_namespace *ns)
18680 gfc_namespace *old_ns;
18681 code_stack *old_cs_base;
18682 struct gfc_omp_saved_state old_omp_state;
18684 if (ns->resolved)
18685 return;
18687 ns->resolved = -1;
18688 old_ns = gfc_current_ns;
18689 old_cs_base = cs_base;
18691 /* As gfc_resolve can be called during resolution of an OpenMP construct
18692 body, we should clear any state associated to it, so that say NS's
18693 DO loops are not interpreted as OpenMP loops. */
18694 if (!ns->construct_entities)
18695 gfc_omp_save_and_clear_state (&old_omp_state);
18697 resolve_types (ns);
18698 component_assignment_level = 0;
18699 resolve_codes (ns);
18701 if (ns->omp_assumes)
18702 gfc_resolve_omp_assumptions (ns->omp_assumes);
18704 gfc_current_ns = old_ns;
18705 cs_base = old_cs_base;
18706 ns->resolved = 1;
18708 gfc_run_passes (ns);
18710 if (!ns->construct_entities)
18711 gfc_omp_restore_state (&old_omp_state);