Daily bump.
[official-gcc.git] / gcc / fortran / resolve.c
blob30b96b2f597c92a12a9d9566f87470d14d86e89a
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2021 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "bitmap.h"
26 #include "gfortran.h"
27 #include "arith.h" /* For gfc_compare_expr(). */
28 #include "dependency.h"
29 #include "data.h"
30 #include "target-memory.h" /* for gfc_simplify_transfer */
31 #include "constructor.h"
33 /* Types used in equivalence statements. */
35 enum seq_type
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
40 /* Stack to keep track of the nesting of blocks as we move through the
41 code. See resolve_branch() and gfc_resolve_code(). */
43 typedef struct code_stack
45 struct gfc_code *head, *current;
46 struct code_stack *prev;
48 /* This bitmap keeps track of the targets valid for a branch from
49 inside this block except for END {IF|SELECT}s of enclosing
50 blocks. */
51 bitmap reachable_labels;
53 code_stack;
55 static code_stack *cs_base = NULL;
58 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
60 static int forall_flag;
61 int gfc_do_concurrent_flag;
63 /* True when we are resolving an expression that is an actual argument to
64 a procedure. */
65 static bool actual_arg = false;
66 /* True when we are resolving an expression that is the first actual argument
67 to a procedure. */
68 static bool first_actual_arg = false;
71 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
73 static int omp_workshare_flag;
75 /* True if we are processing a formal arglist. The corresponding function
76 resets the flag each time that it is read. */
77 static bool formal_arg_flag = false;
79 /* True if we are resolving a specification expression. */
80 static bool specification_expr = false;
82 /* The id of the last entry seen. */
83 static int current_entry_id;
85 /* We use bitmaps to determine if a branch target is valid. */
86 static bitmap_obstack labels_obstack;
88 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
89 static bool inquiry_argument = false;
92 bool
93 gfc_is_formal_arg (void)
95 return formal_arg_flag;
98 /* Is the symbol host associated? */
99 static bool
100 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
102 for (ns = ns->parent; ns; ns = ns->parent)
104 if (sym->ns == ns)
105 return true;
108 return false;
111 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
112 an ABSTRACT derived-type. If where is not NULL, an error message with that
113 locus is printed, optionally using name. */
115 static bool
116 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
118 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
120 if (where)
122 if (name)
123 gfc_error ("%qs at %L is of the ABSTRACT type %qs",
124 name, where, ts->u.derived->name);
125 else
126 gfc_error ("ABSTRACT type %qs used at %L",
127 ts->u.derived->name, where);
130 return false;
133 return true;
137 static bool
138 check_proc_interface (gfc_symbol *ifc, locus *where)
140 /* Several checks for F08:C1216. */
141 if (ifc->attr.procedure)
143 gfc_error ("Interface %qs at %L is declared "
144 "in a later PROCEDURE statement", ifc->name, where);
145 return false;
147 if (ifc->generic)
149 /* For generic interfaces, check if there is
150 a specific procedure with the same name. */
151 gfc_interface *gen = ifc->generic;
152 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
153 gen = gen->next;
154 if (!gen)
156 gfc_error ("Interface %qs at %L may not be generic",
157 ifc->name, where);
158 return false;
161 if (ifc->attr.proc == PROC_ST_FUNCTION)
163 gfc_error ("Interface %qs at %L may not be a statement function",
164 ifc->name, where);
165 return false;
167 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
168 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
169 ifc->attr.intrinsic = 1;
170 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
172 gfc_error ("Intrinsic procedure %qs not allowed in "
173 "PROCEDURE statement at %L", ifc->name, where);
174 return false;
176 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
178 gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
179 return false;
181 return true;
185 static void resolve_symbol (gfc_symbol *sym);
188 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
190 static bool
191 resolve_procedure_interface (gfc_symbol *sym)
193 gfc_symbol *ifc = sym->ts.interface;
195 if (!ifc)
196 return true;
198 if (ifc == sym)
200 gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
201 sym->name, &sym->declared_at);
202 return false;
204 if (!check_proc_interface (ifc, &sym->declared_at))
205 return false;
207 if (ifc->attr.if_source || ifc->attr.intrinsic)
209 /* Resolve interface and copy attributes. */
210 resolve_symbol (ifc);
211 if (ifc->attr.intrinsic)
212 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
214 if (ifc->result)
216 sym->ts = ifc->result->ts;
217 sym->attr.allocatable = ifc->result->attr.allocatable;
218 sym->attr.pointer = ifc->result->attr.pointer;
219 sym->attr.dimension = ifc->result->attr.dimension;
220 sym->attr.class_ok = ifc->result->attr.class_ok;
221 sym->as = gfc_copy_array_spec (ifc->result->as);
222 sym->result = sym;
224 else
226 sym->ts = ifc->ts;
227 sym->attr.allocatable = ifc->attr.allocatable;
228 sym->attr.pointer = ifc->attr.pointer;
229 sym->attr.dimension = ifc->attr.dimension;
230 sym->attr.class_ok = ifc->attr.class_ok;
231 sym->as = gfc_copy_array_spec (ifc->as);
233 sym->ts.interface = ifc;
234 sym->attr.function = ifc->attr.function;
235 sym->attr.subroutine = ifc->attr.subroutine;
237 sym->attr.pure = ifc->attr.pure;
238 sym->attr.elemental = ifc->attr.elemental;
239 sym->attr.contiguous = ifc->attr.contiguous;
240 sym->attr.recursive = ifc->attr.recursive;
241 sym->attr.always_explicit = ifc->attr.always_explicit;
242 sym->attr.ext_attr |= ifc->attr.ext_attr;
243 sym->attr.is_bind_c = ifc->attr.is_bind_c;
244 /* Copy char length. */
245 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
247 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
248 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
249 && !gfc_resolve_expr (sym->ts.u.cl->length))
250 return false;
254 return true;
258 /* Resolve types of formal argument lists. These have to be done early so that
259 the formal argument lists of module procedures can be copied to the
260 containing module before the individual procedures are resolved
261 individually. We also resolve argument lists of procedures in interface
262 blocks because they are self-contained scoping units.
264 Since a dummy argument cannot be a non-dummy procedure, the only
265 resort left for untyped names are the IMPLICIT types. */
267 void
268 gfc_resolve_formal_arglist (gfc_symbol *proc)
270 gfc_formal_arglist *f;
271 gfc_symbol *sym;
272 bool saved_specification_expr;
273 int i;
275 if (proc->result != NULL)
276 sym = proc->result;
277 else
278 sym = proc;
280 if (gfc_elemental (proc)
281 || sym->attr.pointer || sym->attr.allocatable
282 || (sym->as && sym->as->rank != 0))
284 proc->attr.always_explicit = 1;
285 sym->attr.always_explicit = 1;
288 formal_arg_flag = true;
290 for (f = proc->formal; f; f = f->next)
292 gfc_array_spec *as;
294 sym = f->sym;
296 if (sym == NULL)
298 /* Alternate return placeholder. */
299 if (gfc_elemental (proc))
300 gfc_error ("Alternate return specifier in elemental subroutine "
301 "%qs at %L is not allowed", proc->name,
302 &proc->declared_at);
303 if (proc->attr.function)
304 gfc_error ("Alternate return specifier in function "
305 "%qs at %L is not allowed", proc->name,
306 &proc->declared_at);
307 continue;
309 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
310 && !resolve_procedure_interface (sym))
311 return;
313 if (strcmp (proc->name, sym->name) == 0)
315 gfc_error ("Self-referential argument "
316 "%qs at %L is not allowed", sym->name,
317 &proc->declared_at);
318 return;
321 if (sym->attr.if_source != IFSRC_UNKNOWN)
322 gfc_resolve_formal_arglist (sym);
324 if (sym->attr.subroutine || sym->attr.external)
326 if (sym->attr.flavor == FL_UNKNOWN)
327 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
329 else
331 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
332 && (!sym->attr.function || sym->result == sym))
333 gfc_set_default_type (sym, 1, sym->ns);
336 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
337 ? CLASS_DATA (sym)->as : sym->as;
339 saved_specification_expr = specification_expr;
340 specification_expr = true;
341 gfc_resolve_array_spec (as, 0);
342 specification_expr = saved_specification_expr;
344 /* We can't tell if an array with dimension (:) is assumed or deferred
345 shape until we know if it has the pointer or allocatable attributes.
347 if (as && as->rank > 0 && as->type == AS_DEFERRED
348 && ((sym->ts.type != BT_CLASS
349 && !(sym->attr.pointer || sym->attr.allocatable))
350 || (sym->ts.type == BT_CLASS
351 && !(CLASS_DATA (sym)->attr.class_pointer
352 || CLASS_DATA (sym)->attr.allocatable)))
353 && sym->attr.flavor != FL_PROCEDURE)
355 as->type = AS_ASSUMED_SHAPE;
356 for (i = 0; i < as->rank; i++)
357 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
360 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
361 || (as && as->type == AS_ASSUMED_RANK)
362 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
363 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
364 && (CLASS_DATA (sym)->attr.class_pointer
365 || CLASS_DATA (sym)->attr.allocatable
366 || CLASS_DATA (sym)->attr.target))
367 || sym->attr.optional)
369 proc->attr.always_explicit = 1;
370 if (proc->result)
371 proc->result->attr.always_explicit = 1;
374 /* If the flavor is unknown at this point, it has to be a variable.
375 A procedure specification would have already set the type. */
377 if (sym->attr.flavor == FL_UNKNOWN)
378 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
380 if (gfc_pure (proc))
382 if (sym->attr.flavor == FL_PROCEDURE)
384 /* F08:C1279. */
385 if (!gfc_pure (sym))
387 gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
388 "also be PURE", sym->name, &sym->declared_at);
389 continue;
392 else if (!sym->attr.pointer)
394 if (proc->attr.function && sym->attr.intent != INTENT_IN)
396 if (sym->attr.value)
397 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
398 " of pure function %qs at %L with VALUE "
399 "attribute but without INTENT(IN)",
400 sym->name, proc->name, &sym->declared_at);
401 else
402 gfc_error ("Argument %qs of pure function %qs at %L must "
403 "be INTENT(IN) or VALUE", sym->name, proc->name,
404 &sym->declared_at);
407 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
409 if (sym->attr.value)
410 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
411 " of pure subroutine %qs at %L with VALUE "
412 "attribute but without INTENT", sym->name,
413 proc->name, &sym->declared_at);
414 else
415 gfc_error ("Argument %qs of pure subroutine %qs at %L "
416 "must have its INTENT specified or have the "
417 "VALUE attribute", sym->name, proc->name,
418 &sym->declared_at);
422 /* F08:C1278a. */
423 if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
425 gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
426 " may not be polymorphic", sym->name, proc->name,
427 &sym->declared_at);
428 continue;
432 if (proc->attr.implicit_pure)
434 if (sym->attr.flavor == FL_PROCEDURE)
436 if (!gfc_pure (sym))
437 proc->attr.implicit_pure = 0;
439 else if (!sym->attr.pointer)
441 if (proc->attr.function && sym->attr.intent != INTENT_IN
442 && !sym->value)
443 proc->attr.implicit_pure = 0;
445 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
446 && !sym->value)
447 proc->attr.implicit_pure = 0;
451 if (gfc_elemental (proc))
453 /* F08:C1289. */
454 if (sym->attr.codimension
455 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
456 && CLASS_DATA (sym)->attr.codimension))
458 gfc_error ("Coarray dummy argument %qs at %L to elemental "
459 "procedure", sym->name, &sym->declared_at);
460 continue;
463 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
464 && CLASS_DATA (sym)->as))
466 gfc_error ("Argument %qs of elemental procedure at %L must "
467 "be scalar", sym->name, &sym->declared_at);
468 continue;
471 if (sym->attr.allocatable
472 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
473 && CLASS_DATA (sym)->attr.allocatable))
475 gfc_error ("Argument %qs of elemental procedure at %L cannot "
476 "have the ALLOCATABLE attribute", sym->name,
477 &sym->declared_at);
478 continue;
481 if (sym->attr.pointer
482 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
483 && CLASS_DATA (sym)->attr.class_pointer))
485 gfc_error ("Argument %qs of elemental procedure at %L cannot "
486 "have the POINTER attribute", sym->name,
487 &sym->declared_at);
488 continue;
491 if (sym->attr.flavor == FL_PROCEDURE)
493 gfc_error ("Dummy procedure %qs not allowed in elemental "
494 "procedure %qs at %L", sym->name, proc->name,
495 &sym->declared_at);
496 continue;
499 /* Fortran 2008 Corrigendum 1, C1290a. */
500 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
502 gfc_error ("Argument %qs of elemental procedure %qs at %L must "
503 "have its INTENT specified or have the VALUE "
504 "attribute", sym->name, proc->name,
505 &sym->declared_at);
506 continue;
510 /* Each dummy shall be specified to be scalar. */
511 if (proc->attr.proc == PROC_ST_FUNCTION)
513 if (sym->as != NULL)
515 /* F03:C1263 (R1238) The function-name and each dummy-arg-name
516 shall be specified, explicitly or implicitly, to be scalar. */
517 gfc_error ("Argument '%s' of statement function '%s' at %L "
518 "must be scalar", sym->name, proc->name,
519 &proc->declared_at);
520 continue;
523 if (sym->ts.type == BT_CHARACTER)
525 gfc_charlen *cl = sym->ts.u.cl;
526 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
528 gfc_error ("Character-valued argument %qs of statement "
529 "function at %L must have constant length",
530 sym->name, &sym->declared_at);
531 continue;
536 formal_arg_flag = false;
540 /* Work function called when searching for symbols that have argument lists
541 associated with them. */
543 static void
544 find_arglists (gfc_symbol *sym)
546 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
547 || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
548 return;
550 gfc_resolve_formal_arglist (sym);
554 /* Given a namespace, resolve all formal argument lists within the namespace.
557 static void
558 resolve_formal_arglists (gfc_namespace *ns)
560 if (ns == NULL)
561 return;
563 gfc_traverse_ns (ns, find_arglists);
567 static void
568 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
570 bool t;
572 if (sym && sym->attr.flavor == FL_PROCEDURE
573 && sym->ns->parent
574 && sym->ns->parent->proc_name
575 && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
576 && !strcmp (sym->name, sym->ns->parent->proc_name->name))
577 gfc_error ("Contained procedure %qs at %L has the same name as its "
578 "encompassing procedure", sym->name, &sym->declared_at);
580 /* If this namespace is not a function or an entry master function,
581 ignore it. */
582 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
583 || sym->attr.entry_master)
584 return;
586 if (!sym->result)
587 return;
589 /* Try to find out of what the return type is. */
590 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
592 t = gfc_set_default_type (sym->result, 0, ns);
594 if (!t && !sym->result->attr.untyped)
596 if (sym->result == sym)
597 gfc_error ("Contained function %qs at %L has no IMPLICIT type",
598 sym->name, &sym->declared_at);
599 else if (!sym->result->attr.proc_pointer)
600 gfc_error ("Result %qs of contained function %qs at %L has "
601 "no IMPLICIT type", sym->result->name, sym->name,
602 &sym->result->declared_at);
603 sym->result->attr.untyped = 1;
607 /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
608 type, lists the only ways a character length value of * can be used:
609 dummy arguments of procedures, named constants, function results and
610 in allocate statements if the allocate_object is an assumed length dummy
611 in external functions. Internal function results and results of module
612 procedures are not on this list, ergo, not permitted. */
614 if (sym->result->ts.type == BT_CHARACTER)
616 gfc_charlen *cl = sym->result->ts.u.cl;
617 if ((!cl || !cl->length) && !sym->result->ts.deferred)
619 /* See if this is a module-procedure and adapt error message
620 accordingly. */
621 bool module_proc;
622 gcc_assert (ns->parent && ns->parent->proc_name);
623 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
625 gfc_error (module_proc
626 ? G_("Character-valued module procedure %qs at %L"
627 " must not be assumed length")
628 : G_("Character-valued internal function %qs at %L"
629 " must not be assumed length"),
630 sym->name, &sym->declared_at);
636 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
637 introduce duplicates. */
639 static void
640 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
642 gfc_formal_arglist *f, *new_arglist;
643 gfc_symbol *new_sym;
645 for (; new_args != NULL; new_args = new_args->next)
647 new_sym = new_args->sym;
648 /* See if this arg is already in the formal argument list. */
649 for (f = proc->formal; f; f = f->next)
651 if (new_sym == f->sym)
652 break;
655 if (f)
656 continue;
658 /* Add a new argument. Argument order is not important. */
659 new_arglist = gfc_get_formal_arglist ();
660 new_arglist->sym = new_sym;
661 new_arglist->next = proc->formal;
662 proc->formal = new_arglist;
667 /* Flag the arguments that are not present in all entries. */
669 static void
670 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
672 gfc_formal_arglist *f, *head;
673 head = new_args;
675 for (f = proc->formal; f; f = f->next)
677 if (f->sym == NULL)
678 continue;
680 for (new_args = head; new_args; new_args = new_args->next)
682 if (new_args->sym == f->sym)
683 break;
686 if (new_args)
687 continue;
689 f->sym->attr.not_always_present = 1;
694 /* Resolve alternate entry points. If a symbol has multiple entry points we
695 create a new master symbol for the main routine, and turn the existing
696 symbol into an entry point. */
698 static void
699 resolve_entries (gfc_namespace *ns)
701 gfc_namespace *old_ns;
702 gfc_code *c;
703 gfc_symbol *proc;
704 gfc_entry_list *el;
705 char name[GFC_MAX_SYMBOL_LEN + 1];
706 static int master_count = 0;
708 if (ns->proc_name == NULL)
709 return;
711 /* No need to do anything if this procedure doesn't have alternate entry
712 points. */
713 if (!ns->entries)
714 return;
716 /* We may already have resolved alternate entry points. */
717 if (ns->proc_name->attr.entry_master)
718 return;
720 /* If this isn't a procedure something has gone horribly wrong. */
721 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
723 /* Remember the current namespace. */
724 old_ns = gfc_current_ns;
726 gfc_current_ns = ns;
728 /* Add the main entry point to the list of entry points. */
729 el = gfc_get_entry_list ();
730 el->sym = ns->proc_name;
731 el->id = 0;
732 el->next = ns->entries;
733 ns->entries = el;
734 ns->proc_name->attr.entry = 1;
736 /* If it is a module function, it needs to be in the right namespace
737 so that gfc_get_fake_result_decl can gather up the results. The
738 need for this arose in get_proc_name, where these beasts were
739 left in their own namespace, to keep prior references linked to
740 the entry declaration.*/
741 if (ns->proc_name->attr.function
742 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
743 el->sym->ns = ns;
745 /* Do the same for entries where the master is not a module
746 procedure. These are retained in the module namespace because
747 of the module procedure declaration. */
748 for (el = el->next; el; el = el->next)
749 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
750 && el->sym->attr.mod_proc)
751 el->sym->ns = ns;
752 el = ns->entries;
754 /* Add an entry statement for it. */
755 c = gfc_get_code (EXEC_ENTRY);
756 c->ext.entry = el;
757 c->next = ns->code;
758 ns->code = c;
760 /* Create a new symbol for the master function. */
761 /* Give the internal function a unique name (within this file).
762 Also include the function name so the user has some hope of figuring
763 out what is going on. */
764 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
765 master_count++, ns->proc_name->name);
766 gfc_get_ha_symbol (name, &proc);
767 gcc_assert (proc != NULL);
769 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
770 if (ns->proc_name->attr.subroutine)
771 gfc_add_subroutine (&proc->attr, proc->name, NULL);
772 else
774 gfc_symbol *sym;
775 gfc_typespec *ts, *fts;
776 gfc_array_spec *as, *fas;
777 gfc_add_function (&proc->attr, proc->name, NULL);
778 proc->result = proc;
779 fas = ns->entries->sym->as;
780 fas = fas ? fas : ns->entries->sym->result->as;
781 fts = &ns->entries->sym->result->ts;
782 if (fts->type == BT_UNKNOWN)
783 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
784 for (el = ns->entries->next; el; el = el->next)
786 ts = &el->sym->result->ts;
787 as = el->sym->as;
788 as = as ? as : el->sym->result->as;
789 if (ts->type == BT_UNKNOWN)
790 ts = gfc_get_default_type (el->sym->result->name, NULL);
792 if (! gfc_compare_types (ts, fts)
793 || (el->sym->result->attr.dimension
794 != ns->entries->sym->result->attr.dimension)
795 || (el->sym->result->attr.pointer
796 != ns->entries->sym->result->attr.pointer))
797 break;
798 else if (as && fas && ns->entries->sym->result != el->sym->result
799 && gfc_compare_array_spec (as, fas) == 0)
800 gfc_error ("Function %s at %L has entries with mismatched "
801 "array specifications", ns->entries->sym->name,
802 &ns->entries->sym->declared_at);
803 /* The characteristics need to match and thus both need to have
804 the same string length, i.e. both len=*, or both len=4.
805 Having both len=<variable> is also possible, but difficult to
806 check at compile time. */
807 else if (ts->type == BT_CHARACTER
808 && (el->sym->result->attr.allocatable
809 != ns->entries->sym->result->attr.allocatable))
811 gfc_error ("Function %s at %L has entry %s with mismatched "
812 "characteristics", ns->entries->sym->name,
813 &ns->entries->sym->declared_at, el->sym->name);
814 goto cleanup;
816 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
817 && (((ts->u.cl->length && !fts->u.cl->length)
818 ||(!ts->u.cl->length && fts->u.cl->length))
819 || (ts->u.cl->length
820 && ts->u.cl->length->expr_type
821 != fts->u.cl->length->expr_type)
822 || (ts->u.cl->length
823 && ts->u.cl->length->expr_type == EXPR_CONSTANT
824 && mpz_cmp (ts->u.cl->length->value.integer,
825 fts->u.cl->length->value.integer) != 0)))
826 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
827 "entries returning variables of different "
828 "string lengths", ns->entries->sym->name,
829 &ns->entries->sym->declared_at);
832 if (el == NULL)
834 sym = ns->entries->sym->result;
835 /* All result types the same. */
836 proc->ts = *fts;
837 if (sym->attr.dimension)
838 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
839 if (sym->attr.pointer)
840 gfc_add_pointer (&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
874 ts = &sym->ts;
875 if (ts->type == BT_UNKNOWN)
876 ts = gfc_get_default_type (sym->name, NULL);
877 switch (ts->type)
879 case BT_INTEGER:
880 if (ts->kind == gfc_default_integer_kind)
881 sym = NULL;
882 break;
883 case BT_REAL:
884 if (ts->kind == gfc_default_real_kind
885 || ts->kind == gfc_default_double_kind)
886 sym = NULL;
887 break;
888 case BT_COMPLEX:
889 if (ts->kind == gfc_default_complex_kind)
890 sym = NULL;
891 break;
892 case BT_LOGICAL:
893 if (ts->kind == gfc_default_logical_kind)
894 sym = NULL;
895 break;
896 case BT_UNKNOWN:
897 /* We will issue error elsewhere. */
898 sym = NULL;
899 break;
900 default:
901 break;
903 if (sym)
905 if (el == ns->entries)
906 gfc_error ("FUNCTION result %s cannot be of type %s "
907 "in FUNCTION %s at %L", sym->name,
908 gfc_typename (ts), ns->entries->sym->name,
909 &sym->declared_at);
910 else
911 gfc_error ("ENTRY result %s cannot be of type %s "
912 "in FUNCTION %s at %L", sym->name,
913 gfc_typename (ts), ns->entries->sym->name,
914 &sym->declared_at);
921 cleanup:
922 proc->attr.access = ACCESS_PRIVATE;
923 proc->attr.entry_master = 1;
925 /* Merge all the entry point arguments. */
926 for (el = ns->entries; el; el = el->next)
927 merge_argument_lists (proc, el->sym->formal);
929 /* Check the master formal arguments for any that are not
930 present in all entry points. */
931 for (el = ns->entries; el; el = el->next)
932 check_argument_lists (proc, el->sym->formal);
934 /* Use the master function for the function body. */
935 ns->proc_name = proc;
937 /* Finalize the new symbols. */
938 gfc_commit_symbols ();
940 /* Restore the original namespace. */
941 gfc_current_ns = old_ns;
945 /* Resolve common variables. */
946 static void
947 resolve_common_vars (gfc_common_head *common_block, bool named_common)
949 gfc_symbol *csym = common_block->head;
950 gfc_gsymbol *gsym;
952 for (; csym; csym = csym->common_next)
954 gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
955 if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
956 gfc_error_now ("Global entity %qs at %L cannot appear in a "
957 "COMMON block at %L", gsym->name,
958 &gsym->where, &csym->common_block->where);
960 /* gfc_add_in_common may have been called before, but the reported errors
961 have been ignored to continue parsing.
962 We do the checks again here. */
963 if (!csym->attr.use_assoc)
965 gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
966 gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
967 &common_block->where);
970 if (csym->value || csym->attr.data)
972 if (!csym->ns->is_block_data)
973 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
974 "but only in BLOCK DATA initialization is "
975 "allowed", csym->name, &csym->declared_at);
976 else if (!named_common)
977 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
978 "in a blank COMMON but initialization is only "
979 "allowed in named common blocks", csym->name,
980 &csym->declared_at);
983 if (UNLIMITED_POLY (csym))
984 gfc_error_now ("%qs at %L cannot appear in COMMON "
985 "[F2008:C5100]", csym->name, &csym->declared_at);
987 if (csym->ts.type != BT_DERIVED)
988 continue;
990 if (!(csym->ts.u.derived->attr.sequence
991 || csym->ts.u.derived->attr.is_bind_c))
992 gfc_error_now ("Derived type variable %qs in COMMON at %L "
993 "has neither the SEQUENCE nor the BIND(C) "
994 "attribute", csym->name, &csym->declared_at);
995 if (csym->ts.u.derived->attr.alloc_comp)
996 gfc_error_now ("Derived type variable %qs in COMMON at %L "
997 "has an ultimate component that is "
998 "allocatable", csym->name, &csym->declared_at);
999 if (gfc_has_default_initializer (csym->ts.u.derived))
1000 gfc_error_now ("Derived type variable %qs in COMMON at %L "
1001 "may not have default initializer", csym->name,
1002 &csym->declared_at);
1004 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
1005 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
1009 /* Resolve common blocks. */
1010 static void
1011 resolve_common_blocks (gfc_symtree *common_root)
1013 gfc_symbol *sym;
1014 gfc_gsymbol * gsym;
1016 if (common_root == NULL)
1017 return;
1019 if (common_root->left)
1020 resolve_common_blocks (common_root->left);
1021 if (common_root->right)
1022 resolve_common_blocks (common_root->right);
1024 resolve_common_vars (common_root->n.common, true);
1026 /* The common name is a global name - in Fortran 2003 also if it has a
1027 C binding name, since Fortran 2008 only the C binding name is a global
1028 identifier. */
1029 if (!common_root->n.common->binding_label
1030 || gfc_notification_std (GFC_STD_F2008))
1032 gsym = gfc_find_gsymbol (gfc_gsym_root,
1033 common_root->n.common->name);
1035 if (gsym && gfc_notification_std (GFC_STD_F2008)
1036 && gsym->type == GSYM_COMMON
1037 && ((common_root->n.common->binding_label
1038 && (!gsym->binding_label
1039 || strcmp (common_root->n.common->binding_label,
1040 gsym->binding_label) != 0))
1041 || (!common_root->n.common->binding_label
1042 && gsym->binding_label)))
1044 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1045 "identifier and must thus have the same binding name "
1046 "as the same-named COMMON block at %L: %s vs %s",
1047 common_root->n.common->name, &common_root->n.common->where,
1048 &gsym->where,
1049 common_root->n.common->binding_label
1050 ? common_root->n.common->binding_label : "(blank)",
1051 gsym->binding_label ? gsym->binding_label : "(blank)");
1052 return;
1055 if (gsym && gsym->type != GSYM_COMMON
1056 && !common_root->n.common->binding_label)
1058 gfc_error ("COMMON block %qs at %L uses the same global identifier "
1059 "as entity at %L",
1060 common_root->n.common->name, &common_root->n.common->where,
1061 &gsym->where);
1062 return;
1064 if (gsym && gsym->type != GSYM_COMMON)
1066 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1067 "%L sharing the identifier with global non-COMMON-block "
1068 "entity at %L", common_root->n.common->name,
1069 &common_root->n.common->where, &gsym->where);
1070 return;
1072 if (!gsym)
1074 gsym = gfc_get_gsymbol (common_root->n.common->name, false);
1075 gsym->type = GSYM_COMMON;
1076 gsym->where = common_root->n.common->where;
1077 gsym->defined = 1;
1079 gsym->used = 1;
1082 if (common_root->n.common->binding_label)
1084 gsym = gfc_find_gsymbol (gfc_gsym_root,
1085 common_root->n.common->binding_label);
1086 if (gsym && gsym->type != GSYM_COMMON)
1088 gfc_error ("COMMON block at %L with binding label %qs uses the same "
1089 "global identifier as entity at %L",
1090 &common_root->n.common->where,
1091 common_root->n.common->binding_label, &gsym->where);
1092 return;
1094 if (!gsym)
1096 gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
1097 gsym->type = GSYM_COMMON;
1098 gsym->where = common_root->n.common->where;
1099 gsym->defined = 1;
1101 gsym->used = 1;
1104 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1105 if (sym == NULL)
1106 return;
1108 if (sym->attr.flavor == FL_PARAMETER)
1109 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1110 sym->name, &common_root->n.common->where, &sym->declared_at);
1112 if (sym->attr.external)
1113 gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
1114 sym->name, &common_root->n.common->where);
1116 if (sym->attr.intrinsic)
1117 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1118 sym->name, &common_root->n.common->where);
1119 else if (sym->attr.result
1120 || gfc_is_function_return_value (sym, gfc_current_ns))
1121 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1122 "that is also a function result", sym->name,
1123 &common_root->n.common->where);
1124 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1125 && sym->attr.proc != PROC_ST_FUNCTION)
1126 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1127 "that is also a global procedure", sym->name,
1128 &common_root->n.common->where);
1132 /* Resolve contained function types. Because contained functions can call one
1133 another, they have to be worked out before any of the contained procedures
1134 can be resolved.
1136 The good news is that if a function doesn't already have a type, the only
1137 way it can get one is through an IMPLICIT type or a RESULT variable, because
1138 by definition contained functions are contained namespace they're contained
1139 in, not in a sibling or parent namespace. */
1141 static void
1142 resolve_contained_functions (gfc_namespace *ns)
1144 gfc_namespace *child;
1145 gfc_entry_list *el;
1147 resolve_formal_arglists (ns);
1149 for (child = ns->contained; child; child = child->sibling)
1151 /* Resolve alternate entry points first. */
1152 resolve_entries (child);
1154 /* Then check function return types. */
1155 resolve_contained_fntype (child->proc_name, child);
1156 for (el = child->entries; el; el = el->next)
1157 resolve_contained_fntype (el->sym, child);
1163 /* A Parameterized Derived Type constructor must contain values for
1164 the PDT KIND parameters or they must have a default initializer.
1165 Go through the constructor picking out the KIND expressions,
1166 storing them in 'param_list' and then call gfc_get_pdt_instance
1167 to obtain the PDT instance. */
1169 static gfc_actual_arglist *param_list, *param_tail, *param;
1171 static bool
1172 get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
1174 param = gfc_get_actual_arglist ();
1175 if (!param_list)
1176 param_list = param_tail = param;
1177 else
1179 param_tail->next = param;
1180 param_tail = param_tail->next;
1183 param_tail->name = c->name;
1184 if (expr)
1185 param_tail->expr = gfc_copy_expr (expr);
1186 else if (c->initializer)
1187 param_tail->expr = gfc_copy_expr (c->initializer);
1188 else
1190 param_tail->spec_type = SPEC_ASSUMED;
1191 if (c->attr.pdt_kind)
1193 gfc_error ("The KIND parameter %qs in the PDT constructor "
1194 "at %C has no value", param->name);
1195 return false;
1199 return true;
1202 static bool
1203 get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
1204 gfc_symbol *derived)
1206 gfc_constructor *cons = NULL;
1207 gfc_component *comp;
1208 bool t = true;
1210 if (expr && expr->expr_type == EXPR_STRUCTURE)
1211 cons = gfc_constructor_first (expr->value.constructor);
1212 else if (constr)
1213 cons = *constr;
1214 gcc_assert (cons);
1216 comp = derived->components;
1218 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1220 if (cons->expr
1221 && cons->expr->expr_type == EXPR_STRUCTURE
1222 && comp->ts.type == BT_DERIVED)
1224 t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
1225 if (!t)
1226 return t;
1228 else if (comp->ts.type == BT_DERIVED)
1230 t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
1231 if (!t)
1232 return t;
1234 else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
1235 && derived->attr.pdt_template)
1237 t = get_pdt_spec_expr (comp, cons->expr);
1238 if (!t)
1239 return t;
1242 return t;
1246 static bool resolve_fl_derived0 (gfc_symbol *sym);
1247 static bool resolve_fl_struct (gfc_symbol *sym);
1250 /* Resolve all of the elements of a structure constructor and make sure that
1251 the types are correct. The 'init' flag indicates that the given
1252 constructor is an initializer. */
1254 static bool
1255 resolve_structure_cons (gfc_expr *expr, int init)
1257 gfc_constructor *cons;
1258 gfc_component *comp;
1259 bool t;
1260 symbol_attribute a;
1262 t = true;
1264 if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1266 if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1267 resolve_fl_derived0 (expr->ts.u.derived);
1268 else
1269 resolve_fl_struct (expr->ts.u.derived);
1271 /* If this is a Parameterized Derived Type template, find the
1272 instance corresponding to the PDT kind parameters. */
1273 if (expr->ts.u.derived->attr.pdt_template)
1275 param_list = NULL;
1276 t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
1277 if (!t)
1278 return t;
1279 gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
1281 expr->param_list = gfc_copy_actual_arglist (param_list);
1283 if (param_list)
1284 gfc_free_actual_arglist (param_list);
1286 if (!expr->ts.u.derived->attr.pdt_type)
1287 return false;
1291 cons = gfc_constructor_first (expr->value.constructor);
1293 /* A constructor may have references if it is the result of substituting a
1294 parameter variable. In this case we just pull out the component we
1295 want. */
1296 if (expr->ref)
1297 comp = expr->ref->u.c.sym->components;
1298 else
1299 comp = expr->ts.u.derived->components;
1301 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1303 int rank;
1305 if (!cons->expr)
1306 continue;
1308 /* Unions use an EXPR_NULL contrived expression to tell the translation
1309 phase to generate an initializer of the appropriate length.
1310 Ignore it here. */
1311 if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1312 continue;
1314 if (!gfc_resolve_expr (cons->expr))
1316 t = false;
1317 continue;
1320 rank = comp->as ? comp->as->rank : 0;
1321 if (comp->ts.type == BT_CLASS
1322 && !comp->ts.u.derived->attr.unlimited_polymorphic
1323 && CLASS_DATA (comp)->as)
1324 rank = CLASS_DATA (comp)->as->rank;
1326 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1327 && (comp->attr.allocatable || cons->expr->rank))
1329 gfc_error ("The rank of the element in the structure "
1330 "constructor at %L does not match that of the "
1331 "component (%d/%d)", &cons->expr->where,
1332 cons->expr->rank, rank);
1333 t = false;
1336 /* If we don't have the right type, try to convert it. */
1338 if (!comp->attr.proc_pointer &&
1339 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1341 if (strcmp (comp->name, "_extends") == 0)
1343 /* Can afford to be brutal with the _extends initializer.
1344 The derived type can get lost because it is PRIVATE
1345 but it is not usage constrained by the standard. */
1346 cons->expr->ts = comp->ts;
1348 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1350 gfc_error ("The element in the structure constructor at %L, "
1351 "for pointer component %qs, is %s but should be %s",
1352 &cons->expr->where, comp->name,
1353 gfc_basic_typename (cons->expr->ts.type),
1354 gfc_basic_typename (comp->ts.type));
1355 t = false;
1357 else
1359 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1360 if (t)
1361 t = t2;
1365 /* For strings, the length of the constructor should be the same as
1366 the one of the structure, ensure this if the lengths are known at
1367 compile time and when we are dealing with PARAMETER or structure
1368 constructors. */
1369 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1370 && comp->ts.u.cl->length
1371 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1372 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1373 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1374 && cons->expr->rank != 0
1375 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1376 comp->ts.u.cl->length->value.integer) != 0)
1378 if (cons->expr->expr_type == EXPR_VARIABLE
1379 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1381 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1382 to make use of the gfc_resolve_character_array_constructor
1383 machinery. The expression is later simplified away to
1384 an array of string literals. */
1385 gfc_expr *para = cons->expr;
1386 cons->expr = gfc_get_expr ();
1387 cons->expr->ts = para->ts;
1388 cons->expr->where = para->where;
1389 cons->expr->expr_type = EXPR_ARRAY;
1390 cons->expr->rank = para->rank;
1391 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1392 gfc_constructor_append_expr (&cons->expr->value.constructor,
1393 para, &cons->expr->where);
1396 if (cons->expr->expr_type == EXPR_ARRAY)
1398 /* Rely on the cleanup of the namespace to deal correctly with
1399 the old charlen. (There was a block here that attempted to
1400 remove the charlen but broke the chain in so doing.) */
1401 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1402 cons->expr->ts.u.cl->length_from_typespec = true;
1403 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1404 gfc_resolve_character_array_constructor (cons->expr);
1408 if (cons->expr->expr_type == EXPR_NULL
1409 && !(comp->attr.pointer || comp->attr.allocatable
1410 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1411 || (comp->ts.type == BT_CLASS
1412 && (CLASS_DATA (comp)->attr.class_pointer
1413 || CLASS_DATA (comp)->attr.allocatable))))
1415 t = false;
1416 gfc_error ("The NULL in the structure constructor at %L is "
1417 "being applied to component %qs, which is neither "
1418 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1419 comp->name);
1422 if (comp->attr.proc_pointer && comp->ts.interface)
1424 /* Check procedure pointer interface. */
1425 gfc_symbol *s2 = NULL;
1426 gfc_component *c2;
1427 const char *name;
1428 char err[200];
1430 c2 = gfc_get_proc_ptr_comp (cons->expr);
1431 if (c2)
1433 s2 = c2->ts.interface;
1434 name = c2->name;
1436 else if (cons->expr->expr_type == EXPR_FUNCTION)
1438 s2 = cons->expr->symtree->n.sym->result;
1439 name = cons->expr->symtree->n.sym->result->name;
1441 else if (cons->expr->expr_type != EXPR_NULL)
1443 s2 = cons->expr->symtree->n.sym;
1444 name = cons->expr->symtree->n.sym->name;
1447 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1448 err, sizeof (err), NULL, NULL))
1450 gfc_error_opt (0, "Interface mismatch for procedure-pointer "
1451 "component %qs in structure constructor at %L:"
1452 " %s", comp->name, &cons->expr->where, err);
1453 return false;
1457 if (!comp->attr.pointer || comp->attr.proc_pointer
1458 || cons->expr->expr_type == EXPR_NULL)
1459 continue;
1461 a = gfc_expr_attr (cons->expr);
1463 if (!a.pointer && !a.target)
1465 t = false;
1466 gfc_error ("The element in the structure constructor at %L, "
1467 "for pointer component %qs should be a POINTER or "
1468 "a TARGET", &cons->expr->where, comp->name);
1471 if (init)
1473 /* F08:C461. Additional checks for pointer initialization. */
1474 if (a.allocatable)
1476 t = false;
1477 gfc_error ("Pointer initialization target at %L "
1478 "must not be ALLOCATABLE", &cons->expr->where);
1480 if (!a.save)
1482 t = false;
1483 gfc_error ("Pointer initialization target at %L "
1484 "must have the SAVE attribute", &cons->expr->where);
1488 /* F2003, C1272 (3). */
1489 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1490 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1491 || gfc_is_coindexed (cons->expr));
1492 if (impure && gfc_pure (NULL))
1494 t = false;
1495 gfc_error ("Invalid expression in the structure constructor for "
1496 "pointer component %qs at %L in PURE procedure",
1497 comp->name, &cons->expr->where);
1500 if (impure)
1501 gfc_unset_implicit_pure (NULL);
1504 return t;
1508 /****************** Expression name resolution ******************/
1510 /* Returns 0 if a symbol was not declared with a type or
1511 attribute declaration statement, nonzero otherwise. */
1513 static int
1514 was_declared (gfc_symbol *sym)
1516 symbol_attribute a;
1518 a = sym->attr;
1520 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1521 return 1;
1523 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1524 || a.optional || a.pointer || a.save || a.target || a.volatile_
1525 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1526 || a.asynchronous || a.codimension)
1527 return 1;
1529 return 0;
1533 /* Determine if a symbol is generic or not. */
1535 static int
1536 generic_sym (gfc_symbol *sym)
1538 gfc_symbol *s;
1540 if (sym->attr.generic ||
1541 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1542 return 1;
1544 if (was_declared (sym) || sym->ns->parent == NULL)
1545 return 0;
1547 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1549 if (s != NULL)
1551 if (s == sym)
1552 return 0;
1553 else
1554 return generic_sym (s);
1557 return 0;
1561 /* Determine if a symbol is specific or not. */
1563 static int
1564 specific_sym (gfc_symbol *sym)
1566 gfc_symbol *s;
1568 if (sym->attr.if_source == IFSRC_IFBODY
1569 || sym->attr.proc == PROC_MODULE
1570 || sym->attr.proc == PROC_INTERNAL
1571 || sym->attr.proc == PROC_ST_FUNCTION
1572 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1573 || sym->attr.external)
1574 return 1;
1576 if (was_declared (sym) || sym->ns->parent == NULL)
1577 return 0;
1579 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1581 return (s == NULL) ? 0 : specific_sym (s);
1585 /* Figure out if the procedure is specific, generic or unknown. */
1587 enum proc_type
1588 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1590 static proc_type
1591 procedure_kind (gfc_symbol *sym)
1593 if (generic_sym (sym))
1594 return PTYPE_GENERIC;
1596 if (specific_sym (sym))
1597 return PTYPE_SPECIFIC;
1599 return PTYPE_UNKNOWN;
1602 /* Check references to assumed size arrays. The flag need_full_assumed_size
1603 is nonzero when matching actual arguments. */
1605 static int need_full_assumed_size = 0;
1607 static bool
1608 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1610 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1611 return false;
1613 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1614 What should it be? */
1615 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1616 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1617 && (e->ref->u.ar.type == AR_FULL))
1619 gfc_error ("The upper bound in the last dimension must "
1620 "appear in the reference to the assumed size "
1621 "array %qs at %L", sym->name, &e->where);
1622 return true;
1624 return false;
1628 /* Look for bad assumed size array references in argument expressions
1629 of elemental and array valued intrinsic procedures. Since this is
1630 called from procedure resolution functions, it only recurses at
1631 operators. */
1633 static bool
1634 resolve_assumed_size_actual (gfc_expr *e)
1636 if (e == NULL)
1637 return false;
1639 switch (e->expr_type)
1641 case EXPR_VARIABLE:
1642 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1643 return true;
1644 break;
1646 case EXPR_OP:
1647 if (resolve_assumed_size_actual (e->value.op.op1)
1648 || resolve_assumed_size_actual (e->value.op.op2))
1649 return true;
1650 break;
1652 default:
1653 break;
1655 return false;
1659 /* Check a generic procedure, passed as an actual argument, to see if
1660 there is a matching specific name. If none, it is an error, and if
1661 more than one, the reference is ambiguous. */
1662 static int
1663 count_specific_procs (gfc_expr *e)
1665 int n;
1666 gfc_interface *p;
1667 gfc_symbol *sym;
1669 n = 0;
1670 sym = e->symtree->n.sym;
1672 for (p = sym->generic; p; p = p->next)
1673 if (strcmp (sym->name, p->sym->name) == 0)
1675 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1676 sym->name);
1677 n++;
1680 if (n > 1)
1681 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1682 &e->where);
1684 if (n == 0)
1685 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1686 "argument at %L", sym->name, &e->where);
1688 return n;
1692 /* See if a call to sym could possibly be a not allowed RECURSION because of
1693 a missing RECURSIVE declaration. This means that either sym is the current
1694 context itself, or sym is the parent of a contained procedure calling its
1695 non-RECURSIVE containing procedure.
1696 This also works if sym is an ENTRY. */
1698 static bool
1699 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1701 gfc_symbol* proc_sym;
1702 gfc_symbol* context_proc;
1703 gfc_namespace* real_context;
1705 if (sym->attr.flavor == FL_PROGRAM
1706 || gfc_fl_struct (sym->attr.flavor))
1707 return false;
1709 /* If we've got an ENTRY, find real procedure. */
1710 if (sym->attr.entry && sym->ns->entries)
1711 proc_sym = sym->ns->entries->sym;
1712 else
1713 proc_sym = sym;
1715 /* If sym is RECURSIVE, all is well of course. */
1716 if (proc_sym->attr.recursive || flag_recursive)
1717 return false;
1719 /* Find the context procedure's "real" symbol if it has entries.
1720 We look for a procedure symbol, so recurse on the parents if we don't
1721 find one (like in case of a BLOCK construct). */
1722 for (real_context = context; ; real_context = real_context->parent)
1724 /* We should find something, eventually! */
1725 gcc_assert (real_context);
1727 context_proc = (real_context->entries ? real_context->entries->sym
1728 : real_context->proc_name);
1730 /* In some special cases, there may not be a proc_name, like for this
1731 invalid code:
1732 real(bad_kind()) function foo () ...
1733 when checking the call to bad_kind ().
1734 In these cases, we simply return here and assume that the
1735 call is ok. */
1736 if (!context_proc)
1737 return false;
1739 if (context_proc->attr.flavor != FL_LABEL)
1740 break;
1743 /* A call from sym's body to itself is recursion, of course. */
1744 if (context_proc == proc_sym)
1745 return true;
1747 /* The same is true if context is a contained procedure and sym the
1748 containing one. */
1749 if (context_proc->attr.contained)
1751 gfc_symbol* parent_proc;
1753 gcc_assert (context->parent);
1754 parent_proc = (context->parent->entries ? context->parent->entries->sym
1755 : context->parent->proc_name);
1757 if (parent_proc == proc_sym)
1758 return true;
1761 return false;
1765 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1766 its typespec and formal argument list. */
1768 bool
1769 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1771 gfc_intrinsic_sym* isym = NULL;
1772 const char* symstd;
1774 if (sym->resolve_symbol_called >= 2)
1775 return true;
1777 sym->resolve_symbol_called = 2;
1779 /* Already resolved. */
1780 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1781 return true;
1783 /* We already know this one is an intrinsic, so we don't call
1784 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1785 gfc_find_subroutine directly to check whether it is a function or
1786 subroutine. */
1788 if (sym->intmod_sym_id && sym->attr.subroutine)
1790 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1791 isym = gfc_intrinsic_subroutine_by_id (id);
1793 else if (sym->intmod_sym_id)
1795 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1796 isym = gfc_intrinsic_function_by_id (id);
1798 else if (!sym->attr.subroutine)
1799 isym = gfc_find_function (sym->name);
1801 if (isym && !sym->attr.subroutine)
1803 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1804 && !sym->attr.implicit_type)
1805 gfc_warning (OPT_Wsurprising,
1806 "Type specified for intrinsic function %qs at %L is"
1807 " ignored", sym->name, &sym->declared_at);
1809 if (!sym->attr.function &&
1810 !gfc_add_function(&sym->attr, sym->name, loc))
1811 return false;
1813 sym->ts = isym->ts;
1815 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1817 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1819 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1820 " specifier", sym->name, &sym->declared_at);
1821 return false;
1824 if (!sym->attr.subroutine &&
1825 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1826 return false;
1828 else
1830 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1831 &sym->declared_at);
1832 return false;
1835 gfc_copy_formal_args_intr (sym, isym, NULL);
1837 sym->attr.pure = isym->pure;
1838 sym->attr.elemental = isym->elemental;
1840 /* Check it is actually available in the standard settings. */
1841 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1843 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1844 "available in the current standard settings but %s. Use "
1845 "an appropriate %<-std=*%> option or enable "
1846 "%<-fall-intrinsics%> in order to use it.",
1847 sym->name, &sym->declared_at, symstd);
1848 return false;
1851 return true;
1855 /* Resolve a procedure expression, like passing it to a called procedure or as
1856 RHS for a procedure pointer assignment. */
1858 static bool
1859 resolve_procedure_expression (gfc_expr* expr)
1861 gfc_symbol* sym;
1863 if (expr->expr_type != EXPR_VARIABLE)
1864 return true;
1865 gcc_assert (expr->symtree);
1867 sym = expr->symtree->n.sym;
1869 if (sym->attr.intrinsic)
1870 gfc_resolve_intrinsic (sym, &expr->where);
1872 if (sym->attr.flavor != FL_PROCEDURE
1873 || (sym->attr.function && sym->result == sym))
1874 return true;
1876 /* A non-RECURSIVE procedure that is used as procedure expression within its
1877 own body is in danger of being called recursively. */
1878 if (is_illegal_recursion (sym, gfc_current_ns))
1879 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1880 " itself recursively. Declare it RECURSIVE or use"
1881 " %<-frecursive%>", sym->name, &expr->where);
1883 return true;
1887 /* Check that name is not a derived type. */
1889 static bool
1890 is_dt_name (const char *name)
1892 gfc_symbol *dt_list, *dt_first;
1894 dt_list = dt_first = gfc_derived_types;
1895 for (; dt_list; dt_list = dt_list->dt_next)
1897 if (strcmp(dt_list->name, name) == 0)
1898 return true;
1899 if (dt_first == dt_list->dt_next)
1900 break;
1902 return false;
1906 /* Resolve an actual argument list. Most of the time, this is just
1907 resolving the expressions in the list.
1908 The exception is that we sometimes have to decide whether arguments
1909 that look like procedure arguments are really simple variable
1910 references. */
1912 static bool
1913 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1914 bool no_formal_args)
1916 gfc_symbol *sym;
1917 gfc_symtree *parent_st;
1918 gfc_expr *e;
1919 gfc_component *comp;
1920 int save_need_full_assumed_size;
1921 bool return_value = false;
1922 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1924 actual_arg = true;
1925 first_actual_arg = true;
1927 for (; arg; arg = arg->next)
1929 e = arg->expr;
1930 if (e == NULL)
1932 /* Check the label is a valid branching target. */
1933 if (arg->label)
1935 if (arg->label->defined == ST_LABEL_UNKNOWN)
1937 gfc_error ("Label %d referenced at %L is never defined",
1938 arg->label->value, &arg->label->where);
1939 goto cleanup;
1942 first_actual_arg = false;
1943 continue;
1946 if (e->expr_type == EXPR_VARIABLE
1947 && e->symtree->n.sym->attr.generic
1948 && no_formal_args
1949 && count_specific_procs (e) != 1)
1950 goto cleanup;
1952 if (e->ts.type != BT_PROCEDURE)
1954 save_need_full_assumed_size = need_full_assumed_size;
1955 if (e->expr_type != EXPR_VARIABLE)
1956 need_full_assumed_size = 0;
1957 if (!gfc_resolve_expr (e))
1958 goto cleanup;
1959 need_full_assumed_size = save_need_full_assumed_size;
1960 goto argument_list;
1963 /* See if the expression node should really be a variable reference. */
1965 sym = e->symtree->n.sym;
1967 if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
1969 gfc_error ("Derived type %qs is used as an actual "
1970 "argument at %L", sym->name, &e->where);
1971 goto cleanup;
1974 if (sym->attr.flavor == FL_PROCEDURE
1975 || sym->attr.intrinsic
1976 || sym->attr.external)
1978 int actual_ok;
1980 /* If a procedure is not already determined to be something else
1981 check if it is intrinsic. */
1982 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1983 sym->attr.intrinsic = 1;
1985 if (sym->attr.proc == PROC_ST_FUNCTION)
1987 gfc_error ("Statement function %qs at %L is not allowed as an "
1988 "actual argument", sym->name, &e->where);
1991 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1992 sym->attr.subroutine);
1993 if (sym->attr.intrinsic && actual_ok == 0)
1995 gfc_error ("Intrinsic %qs at %L is not allowed as an "
1996 "actual argument", sym->name, &e->where);
1999 if (sym->attr.contained && !sym->attr.use_assoc
2000 && sym->ns->proc_name->attr.flavor != FL_MODULE)
2002 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
2003 " used as actual argument at %L",
2004 sym->name, &e->where))
2005 goto cleanup;
2008 if (sym->attr.elemental && !sym->attr.intrinsic)
2010 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
2011 "allowed as an actual argument at %L", sym->name,
2012 &e->where);
2015 /* Check if a generic interface has a specific procedure
2016 with the same name before emitting an error. */
2017 if (sym->attr.generic && count_specific_procs (e) != 1)
2018 goto cleanup;
2020 /* Just in case a specific was found for the expression. */
2021 sym = e->symtree->n.sym;
2023 /* If the symbol is the function that names the current (or
2024 parent) scope, then we really have a variable reference. */
2026 if (gfc_is_function_return_value (sym, sym->ns))
2027 goto got_variable;
2029 /* If all else fails, see if we have a specific intrinsic. */
2030 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
2032 gfc_intrinsic_sym *isym;
2034 isym = gfc_find_function (sym->name);
2035 if (isym == NULL || !isym->specific)
2037 gfc_error ("Unable to find a specific INTRINSIC procedure "
2038 "for the reference %qs at %L", sym->name,
2039 &e->where);
2040 goto cleanup;
2042 sym->ts = isym->ts;
2043 sym->attr.intrinsic = 1;
2044 sym->attr.function = 1;
2047 if (!gfc_resolve_expr (e))
2048 goto cleanup;
2049 goto argument_list;
2052 /* See if the name is a module procedure in a parent unit. */
2054 if (was_declared (sym) || sym->ns->parent == NULL)
2055 goto got_variable;
2057 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
2059 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
2060 goto cleanup;
2063 if (parent_st == NULL)
2064 goto got_variable;
2066 sym = parent_st->n.sym;
2067 e->symtree = parent_st; /* Point to the right thing. */
2069 if (sym->attr.flavor == FL_PROCEDURE
2070 || sym->attr.intrinsic
2071 || sym->attr.external)
2073 if (!gfc_resolve_expr (e))
2074 goto cleanup;
2075 goto argument_list;
2078 got_variable:
2079 e->expr_type = EXPR_VARIABLE;
2080 e->ts = sym->ts;
2081 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2082 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2083 && CLASS_DATA (sym)->as))
2085 e->rank = sym->ts.type == BT_CLASS
2086 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
2087 e->ref = gfc_get_ref ();
2088 e->ref->type = REF_ARRAY;
2089 e->ref->u.ar.type = AR_FULL;
2090 e->ref->u.ar.as = sym->ts.type == BT_CLASS
2091 ? CLASS_DATA (sym)->as : sym->as;
2094 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2095 primary.c (match_actual_arg). If above code determines that it
2096 is a variable instead, it needs to be resolved as it was not
2097 done at the beginning of this function. */
2098 save_need_full_assumed_size = need_full_assumed_size;
2099 if (e->expr_type != EXPR_VARIABLE)
2100 need_full_assumed_size = 0;
2101 if (!gfc_resolve_expr (e))
2102 goto cleanup;
2103 need_full_assumed_size = save_need_full_assumed_size;
2105 argument_list:
2106 /* Check argument list functions %VAL, %LOC and %REF. There is
2107 nothing to do for %REF. */
2108 if (arg->name && arg->name[0] == '%')
2110 if (strcmp ("%VAL", arg->name) == 0)
2112 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
2114 gfc_error ("By-value argument at %L is not of numeric "
2115 "type", &e->where);
2116 goto cleanup;
2119 if (e->rank)
2121 gfc_error ("By-value argument at %L cannot be an array or "
2122 "an array section", &e->where);
2123 goto cleanup;
2126 /* Intrinsics are still PROC_UNKNOWN here. However,
2127 since same file external procedures are not resolvable
2128 in gfortran, it is a good deal easier to leave them to
2129 intrinsic.c. */
2130 if (ptype != PROC_UNKNOWN
2131 && ptype != PROC_DUMMY
2132 && ptype != PROC_EXTERNAL
2133 && ptype != PROC_MODULE)
2135 gfc_error ("By-value argument at %L is not allowed "
2136 "in this context", &e->where);
2137 goto cleanup;
2141 /* Statement functions have already been excluded above. */
2142 else if (strcmp ("%LOC", arg->name) == 0
2143 && e->ts.type == BT_PROCEDURE)
2145 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
2147 gfc_error ("Passing internal procedure at %L by location "
2148 "not allowed", &e->where);
2149 goto cleanup;
2154 comp = gfc_get_proc_ptr_comp(e);
2155 if (e->expr_type == EXPR_VARIABLE
2156 && comp && comp->attr.elemental)
2158 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2159 "allowed as an actual argument at %L", comp->name,
2160 &e->where);
2163 /* Fortran 2008, C1237. */
2164 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2165 && gfc_has_ultimate_pointer (e))
2167 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2168 "component", &e->where);
2169 goto cleanup;
2172 first_actual_arg = false;
2175 return_value = true;
2177 cleanup:
2178 actual_arg = actual_arg_sav;
2179 first_actual_arg = first_actual_arg_sav;
2181 return return_value;
2185 /* Do the checks of the actual argument list that are specific to elemental
2186 procedures. If called with c == NULL, we have a function, otherwise if
2187 expr == NULL, we have a subroutine. */
2189 static bool
2190 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2192 gfc_actual_arglist *arg0;
2193 gfc_actual_arglist *arg;
2194 gfc_symbol *esym = NULL;
2195 gfc_intrinsic_sym *isym = NULL;
2196 gfc_expr *e = NULL;
2197 gfc_intrinsic_arg *iformal = NULL;
2198 gfc_formal_arglist *eformal = NULL;
2199 bool formal_optional = false;
2200 bool set_by_optional = false;
2201 int i;
2202 int rank = 0;
2204 /* Is this an elemental procedure? */
2205 if (expr && expr->value.function.actual != NULL)
2207 if (expr->value.function.esym != NULL
2208 && expr->value.function.esym->attr.elemental)
2210 arg0 = expr->value.function.actual;
2211 esym = expr->value.function.esym;
2213 else if (expr->value.function.isym != NULL
2214 && expr->value.function.isym->elemental)
2216 arg0 = expr->value.function.actual;
2217 isym = expr->value.function.isym;
2219 else
2220 return true;
2222 else if (c && c->ext.actual != NULL)
2224 arg0 = c->ext.actual;
2226 if (c->resolved_sym)
2227 esym = c->resolved_sym;
2228 else
2229 esym = c->symtree->n.sym;
2230 gcc_assert (esym);
2232 if (!esym->attr.elemental)
2233 return true;
2235 else
2236 return true;
2238 /* The rank of an elemental is the rank of its array argument(s). */
2239 for (arg = arg0; arg; arg = arg->next)
2241 if (arg->expr != NULL && arg->expr->rank != 0)
2243 rank = arg->expr->rank;
2244 if (arg->expr->expr_type == EXPR_VARIABLE
2245 && arg->expr->symtree->n.sym->attr.optional)
2246 set_by_optional = true;
2248 /* Function specific; set the result rank and shape. */
2249 if (expr)
2251 expr->rank = rank;
2252 if (!expr->shape && arg->expr->shape)
2254 expr->shape = gfc_get_shape (rank);
2255 for (i = 0; i < rank; i++)
2256 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2259 break;
2263 /* If it is an array, it shall not be supplied as an actual argument
2264 to an elemental procedure unless an array of the same rank is supplied
2265 as an actual argument corresponding to a nonoptional dummy argument of
2266 that elemental procedure(12.4.1.5). */
2267 formal_optional = false;
2268 if (isym)
2269 iformal = isym->formal;
2270 else
2271 eformal = esym->formal;
2273 for (arg = arg0; arg; arg = arg->next)
2275 if (eformal)
2277 if (eformal->sym && eformal->sym->attr.optional)
2278 formal_optional = true;
2279 eformal = eformal->next;
2281 else if (isym && iformal)
2283 if (iformal->optional)
2284 formal_optional = true;
2285 iformal = iformal->next;
2287 else if (isym)
2288 formal_optional = true;
2290 if (pedantic && arg->expr != NULL
2291 && arg->expr->expr_type == EXPR_VARIABLE
2292 && arg->expr->symtree->n.sym->attr.optional
2293 && formal_optional
2294 && arg->expr->rank
2295 && (set_by_optional || arg->expr->rank != rank)
2296 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2298 bool t = false;
2299 gfc_actual_arglist *a;
2301 /* Scan the argument list for a non-optional argument with the
2302 same rank as arg. */
2303 for (a = arg0; a; a = a->next)
2304 if (a != arg
2305 && a->expr->rank == arg->expr->rank
2306 && !a->expr->symtree->n.sym->attr.optional)
2308 t = true;
2309 break;
2312 if (!t)
2313 gfc_warning (OPT_Wpedantic,
2314 "%qs at %L is an array and OPTIONAL; If it is not "
2315 "present, then it cannot be the actual argument of "
2316 "an ELEMENTAL procedure unless there is a non-optional"
2317 " argument with the same rank "
2318 "(Fortran 2018, 15.5.2.12)",
2319 arg->expr->symtree->n.sym->name, &arg->expr->where);
2323 for (arg = arg0; arg; arg = arg->next)
2325 if (arg->expr == NULL || arg->expr->rank == 0)
2326 continue;
2328 /* Being elemental, the last upper bound of an assumed size array
2329 argument must be present. */
2330 if (resolve_assumed_size_actual (arg->expr))
2331 return false;
2333 /* Elemental procedure's array actual arguments must conform. */
2334 if (e != NULL)
2336 if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
2337 return false;
2339 else
2340 e = arg->expr;
2343 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2344 is an array, the intent inout/out variable needs to be also an array. */
2345 if (rank > 0 && esym && expr == NULL)
2346 for (eformal = esym->formal, arg = arg0; arg && eformal;
2347 arg = arg->next, eformal = eformal->next)
2348 if ((eformal->sym->attr.intent == INTENT_OUT
2349 || eformal->sym->attr.intent == INTENT_INOUT)
2350 && arg->expr && arg->expr->rank == 0)
2352 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2353 "ELEMENTAL subroutine %qs is a scalar, but another "
2354 "actual argument is an array", &arg->expr->where,
2355 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2356 : "INOUT", eformal->sym->name, esym->name);
2357 return false;
2359 return true;
2363 /* This function does the checking of references to global procedures
2364 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2365 77 and 95 standards. It checks for a gsymbol for the name, making
2366 one if it does not already exist. If it already exists, then the
2367 reference being resolved must correspond to the type of gsymbol.
2368 Otherwise, the new symbol is equipped with the attributes of the
2369 reference. The corresponding code that is called in creating
2370 global entities is parse.c.
2372 In addition, for all but -std=legacy, the gsymbols are used to
2373 check the interfaces of external procedures from the same file.
2374 The namespace of the gsymbol is resolved and then, once this is
2375 done the interface is checked. */
2378 static bool
2379 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2381 if (!gsym_ns->proc_name->attr.recursive)
2382 return true;
2384 if (sym->ns == gsym_ns)
2385 return false;
2387 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2388 return false;
2390 return true;
2393 static bool
2394 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2396 if (gsym_ns->entries)
2398 gfc_entry_list *entry = gsym_ns->entries;
2400 for (; entry; entry = entry->next)
2402 if (strcmp (sym->name, entry->sym->name) == 0)
2404 if (strcmp (gsym_ns->proc_name->name,
2405 sym->ns->proc_name->name) == 0)
2406 return false;
2408 if (sym->ns->parent
2409 && strcmp (gsym_ns->proc_name->name,
2410 sym->ns->parent->proc_name->name) == 0)
2411 return false;
2415 return true;
2419 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2421 bool
2422 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2424 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2426 for ( ; arg; arg = arg->next)
2428 if (!arg->sym)
2429 continue;
2431 if (arg->sym->attr.allocatable) /* (2a) */
2433 strncpy (errmsg, _("allocatable argument"), err_len);
2434 return true;
2436 else if (arg->sym->attr.asynchronous)
2438 strncpy (errmsg, _("asynchronous argument"), err_len);
2439 return true;
2441 else if (arg->sym->attr.optional)
2443 strncpy (errmsg, _("optional argument"), err_len);
2444 return true;
2446 else if (arg->sym->attr.pointer)
2448 strncpy (errmsg, _("pointer argument"), err_len);
2449 return true;
2451 else if (arg->sym->attr.target)
2453 strncpy (errmsg, _("target argument"), err_len);
2454 return true;
2456 else if (arg->sym->attr.value)
2458 strncpy (errmsg, _("value argument"), err_len);
2459 return true;
2461 else if (arg->sym->attr.volatile_)
2463 strncpy (errmsg, _("volatile argument"), err_len);
2464 return true;
2466 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2468 strncpy (errmsg, _("assumed-shape argument"), err_len);
2469 return true;
2471 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2473 strncpy (errmsg, _("assumed-rank argument"), err_len);
2474 return true;
2476 else if (arg->sym->attr.codimension) /* (2c) */
2478 strncpy (errmsg, _("coarray argument"), err_len);
2479 return true;
2481 else if (false) /* (2d) TODO: parametrized derived type */
2483 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2484 return true;
2486 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2488 strncpy (errmsg, _("polymorphic argument"), err_len);
2489 return true;
2491 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2493 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2494 return true;
2496 else if (arg->sym->ts.type == BT_ASSUMED)
2498 /* As assumed-type is unlimited polymorphic (cf. above).
2499 See also TS 29113, Note 6.1. */
2500 strncpy (errmsg, _("assumed-type argument"), err_len);
2501 return true;
2505 if (sym->attr.function)
2507 gfc_symbol *res = sym->result ? sym->result : sym;
2509 if (res->attr.dimension) /* (3a) */
2511 strncpy (errmsg, _("array result"), err_len);
2512 return true;
2514 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2516 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2517 return true;
2519 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2520 && res->ts.u.cl->length
2521 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2523 strncpy (errmsg, _("result with non-constant character length"), err_len);
2524 return true;
2528 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2530 strncpy (errmsg, _("elemental procedure"), err_len);
2531 return true;
2533 else if (sym->attr.is_bind_c) /* (5) */
2535 strncpy (errmsg, _("bind(c) procedure"), err_len);
2536 return true;
2539 return false;
2543 static void
2544 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
2546 gfc_gsymbol * gsym;
2547 gfc_namespace *ns;
2548 enum gfc_symbol_type type;
2549 char reason[200];
2551 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2553 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
2554 sym->binding_label != NULL);
2556 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2557 gfc_global_used (gsym, where);
2559 if ((sym->attr.if_source == IFSRC_UNKNOWN
2560 || sym->attr.if_source == IFSRC_IFBODY)
2561 && gsym->type != GSYM_UNKNOWN
2562 && !gsym->binding_label
2563 && gsym->ns
2564 && gsym->ns->proc_name
2565 && not_in_recursive (sym, gsym->ns)
2566 && not_entry_self_reference (sym, gsym->ns))
2568 gfc_symbol *def_sym;
2569 def_sym = gsym->ns->proc_name;
2571 if (gsym->ns->resolved != -1)
2574 /* Resolve the gsymbol namespace if needed. */
2575 if (!gsym->ns->resolved)
2577 gfc_symbol *old_dt_list;
2579 /* Stash away derived types so that the backend_decls
2580 do not get mixed up. */
2581 old_dt_list = gfc_derived_types;
2582 gfc_derived_types = NULL;
2584 gfc_resolve (gsym->ns);
2586 /* Store the new derived types with the global namespace. */
2587 if (gfc_derived_types)
2588 gsym->ns->derived_types = gfc_derived_types;
2590 /* Restore the derived types of this namespace. */
2591 gfc_derived_types = old_dt_list;
2594 /* Make sure that translation for the gsymbol occurs before
2595 the procedure currently being resolved. */
2596 ns = gfc_global_ns_list;
2597 for (; ns && ns != gsym->ns; ns = ns->sibling)
2599 if (ns->sibling == gsym->ns)
2601 ns->sibling = gsym->ns->sibling;
2602 gsym->ns->sibling = gfc_global_ns_list;
2603 gfc_global_ns_list = gsym->ns;
2604 break;
2608 /* This can happen if a binding name has been specified. */
2609 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2610 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2612 if (def_sym->attr.entry_master || def_sym->attr.entry)
2614 gfc_entry_list *entry;
2615 for (entry = gsym->ns->entries; entry; entry = entry->next)
2616 if (strcmp (entry->sym->name, sym->name) == 0)
2618 def_sym = entry->sym;
2619 break;
2624 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2626 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2627 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2628 gfc_typename (&def_sym->ts));
2629 goto done;
2632 if (sym->attr.if_source == IFSRC_UNKNOWN
2633 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2635 gfc_error ("Explicit interface required for %qs at %L: %s",
2636 sym->name, &sym->declared_at, reason);
2637 goto done;
2640 bool bad_result_characteristics;
2641 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2642 reason, sizeof(reason), NULL, NULL,
2643 &bad_result_characteristics))
2645 /* Turn erros into warnings with -std=gnu and -std=legacy,
2646 unless a function returns a wrong type, which can lead
2647 to all kinds of ICEs and wrong code. */
2649 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
2650 && !bad_result_characteristics)
2651 gfc_errors_to_warnings (true);
2653 gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
2654 sym->name, &sym->declared_at, reason);
2655 sym->error = 1;
2656 gfc_errors_to_warnings (false);
2657 goto done;
2661 done:
2663 if (gsym->type == GSYM_UNKNOWN)
2665 gsym->type = type;
2666 gsym->where = *where;
2669 gsym->used = 1;
2673 /************* Function resolution *************/
2675 /* Resolve a function call known to be generic.
2676 Section 14.1.2.4.1. */
2678 static match
2679 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2681 gfc_symbol *s;
2683 if (sym->attr.generic)
2685 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2686 if (s != NULL)
2688 expr->value.function.name = s->name;
2689 expr->value.function.esym = s;
2691 if (s->ts.type != BT_UNKNOWN)
2692 expr->ts = s->ts;
2693 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2694 expr->ts = s->result->ts;
2696 if (s->as != NULL)
2697 expr->rank = s->as->rank;
2698 else if (s->result != NULL && s->result->as != NULL)
2699 expr->rank = s->result->as->rank;
2701 gfc_set_sym_referenced (expr->value.function.esym);
2703 return MATCH_YES;
2706 /* TODO: Need to search for elemental references in generic
2707 interface. */
2710 if (sym->attr.intrinsic)
2711 return gfc_intrinsic_func_interface (expr, 0);
2713 return MATCH_NO;
2717 static bool
2718 resolve_generic_f (gfc_expr *expr)
2720 gfc_symbol *sym;
2721 match m;
2722 gfc_interface *intr = NULL;
2724 sym = expr->symtree->n.sym;
2726 for (;;)
2728 m = resolve_generic_f0 (expr, sym);
2729 if (m == MATCH_YES)
2730 return true;
2731 else if (m == MATCH_ERROR)
2732 return false;
2734 generic:
2735 if (!intr)
2736 for (intr = sym->generic; intr; intr = intr->next)
2737 if (gfc_fl_struct (intr->sym->attr.flavor))
2738 break;
2740 if (sym->ns->parent == NULL)
2741 break;
2742 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2744 if (sym == NULL)
2745 break;
2746 if (!generic_sym (sym))
2747 goto generic;
2750 /* Last ditch attempt. See if the reference is to an intrinsic
2751 that possesses a matching interface. 14.1.2.4 */
2752 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2754 if (gfc_init_expr_flag)
2755 gfc_error ("Function %qs in initialization expression at %L "
2756 "must be an intrinsic function",
2757 expr->symtree->n.sym->name, &expr->where);
2758 else
2759 gfc_error ("There is no specific function for the generic %qs "
2760 "at %L", expr->symtree->n.sym->name, &expr->where);
2761 return false;
2764 if (intr)
2766 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2767 NULL, false))
2768 return false;
2769 if (!gfc_use_derived (expr->ts.u.derived))
2770 return false;
2771 return resolve_structure_cons (expr, 0);
2774 m = gfc_intrinsic_func_interface (expr, 0);
2775 if (m == MATCH_YES)
2776 return true;
2778 if (m == MATCH_NO)
2779 gfc_error ("Generic function %qs at %L is not consistent with a "
2780 "specific intrinsic interface", expr->symtree->n.sym->name,
2781 &expr->where);
2783 return false;
2787 /* Resolve a function call known to be specific. */
2789 static match
2790 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2792 match m;
2794 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2796 if (sym->attr.dummy)
2798 sym->attr.proc = PROC_DUMMY;
2799 goto found;
2802 sym->attr.proc = PROC_EXTERNAL;
2803 goto found;
2806 if (sym->attr.proc == PROC_MODULE
2807 || sym->attr.proc == PROC_ST_FUNCTION
2808 || sym->attr.proc == PROC_INTERNAL)
2809 goto found;
2811 if (sym->attr.intrinsic)
2813 m = gfc_intrinsic_func_interface (expr, 1);
2814 if (m == MATCH_YES)
2815 return MATCH_YES;
2816 if (m == MATCH_NO)
2817 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2818 "with an intrinsic", sym->name, &expr->where);
2820 return MATCH_ERROR;
2823 return MATCH_NO;
2825 found:
2826 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2828 if (sym->result)
2829 expr->ts = sym->result->ts;
2830 else
2831 expr->ts = sym->ts;
2832 expr->value.function.name = sym->name;
2833 expr->value.function.esym = sym;
2834 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2835 error(s). */
2836 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2837 return MATCH_ERROR;
2838 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2839 expr->rank = CLASS_DATA (sym)->as->rank;
2840 else if (sym->as != NULL)
2841 expr->rank = sym->as->rank;
2843 return MATCH_YES;
2847 static bool
2848 resolve_specific_f (gfc_expr *expr)
2850 gfc_symbol *sym;
2851 match m;
2853 sym = expr->symtree->n.sym;
2855 for (;;)
2857 m = resolve_specific_f0 (sym, expr);
2858 if (m == MATCH_YES)
2859 return true;
2860 if (m == MATCH_ERROR)
2861 return false;
2863 if (sym->ns->parent == NULL)
2864 break;
2866 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2868 if (sym == NULL)
2869 break;
2872 gfc_error ("Unable to resolve the specific function %qs at %L",
2873 expr->symtree->n.sym->name, &expr->where);
2875 return true;
2878 /* Recursively append candidate SYM to CANDIDATES. Store the number of
2879 candidates in CANDIDATES_LEN. */
2881 static void
2882 lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
2883 char **&candidates,
2884 size_t &candidates_len)
2886 gfc_symtree *p;
2888 if (sym == NULL)
2889 return;
2890 if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
2891 && sym->n.sym->attr.flavor == FL_PROCEDURE)
2892 vec_push (candidates, candidates_len, sym->name);
2894 p = sym->left;
2895 if (p)
2896 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
2898 p = sym->right;
2899 if (p)
2900 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
2904 /* Lookup function FN fuzzily, taking names in SYMROOT into account. */
2906 const char*
2907 gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
2909 char **candidates = NULL;
2910 size_t candidates_len = 0;
2911 lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
2912 return gfc_closest_fuzzy_match (fn, candidates);
2916 /* Resolve a procedure call not known to be generic nor specific. */
2918 static bool
2919 resolve_unknown_f (gfc_expr *expr)
2921 gfc_symbol *sym;
2922 gfc_typespec *ts;
2924 sym = expr->symtree->n.sym;
2926 if (sym->attr.dummy)
2928 sym->attr.proc = PROC_DUMMY;
2929 expr->value.function.name = sym->name;
2930 goto set_type;
2933 /* See if we have an intrinsic function reference. */
2935 if (gfc_is_intrinsic (sym, 0, expr->where))
2937 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2938 return true;
2939 return false;
2942 /* The reference is to an external name. */
2944 sym->attr.proc = PROC_EXTERNAL;
2945 expr->value.function.name = sym->name;
2946 expr->value.function.esym = expr->symtree->n.sym;
2948 if (sym->as != NULL)
2949 expr->rank = sym->as->rank;
2951 /* Type of the expression is either the type of the symbol or the
2952 default type of the symbol. */
2954 set_type:
2955 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2957 if (sym->ts.type != BT_UNKNOWN)
2958 expr->ts = sym->ts;
2959 else
2961 ts = gfc_get_default_type (sym->name, sym->ns);
2963 if (ts->type == BT_UNKNOWN)
2965 const char *guessed
2966 = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
2967 if (guessed)
2968 gfc_error ("Function %qs at %L has no IMPLICIT type"
2969 "; did you mean %qs?",
2970 sym->name, &expr->where, guessed);
2971 else
2972 gfc_error ("Function %qs at %L has no IMPLICIT type",
2973 sym->name, &expr->where);
2974 return false;
2976 else
2977 expr->ts = *ts;
2980 return true;
2984 /* Return true, if the symbol is an external procedure. */
2985 static bool
2986 is_external_proc (gfc_symbol *sym)
2988 if (!sym->attr.dummy && !sym->attr.contained
2989 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2990 && sym->attr.proc != PROC_ST_FUNCTION
2991 && !sym->attr.proc_pointer
2992 && !sym->attr.use_assoc
2993 && sym->name)
2994 return true;
2996 return false;
3000 /* Figure out if a function reference is pure or not. Also set the name
3001 of the function for a potential error message. Return nonzero if the
3002 function is PURE, zero if not. */
3003 static int
3004 pure_stmt_function (gfc_expr *, gfc_symbol *);
3007 gfc_pure_function (gfc_expr *e, const char **name)
3009 int pure;
3010 gfc_component *comp;
3012 *name = NULL;
3014 if (e->symtree != NULL
3015 && e->symtree->n.sym != NULL
3016 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3017 return pure_stmt_function (e, e->symtree->n.sym);
3019 comp = gfc_get_proc_ptr_comp (e);
3020 if (comp)
3022 pure = gfc_pure (comp->ts.interface);
3023 *name = comp->name;
3025 else if (e->value.function.esym)
3027 pure = gfc_pure (e->value.function.esym);
3028 *name = e->value.function.esym->name;
3030 else if (e->value.function.isym)
3032 pure = e->value.function.isym->pure
3033 || e->value.function.isym->elemental;
3034 *name = e->value.function.isym->name;
3036 else
3038 /* Implicit functions are not pure. */
3039 pure = 0;
3040 *name = e->value.function.name;
3043 return pure;
3047 /* Check if the expression is a reference to an implicitly pure function. */
3050 gfc_implicit_pure_function (gfc_expr *e)
3052 gfc_component *comp = gfc_get_proc_ptr_comp (e);
3053 if (comp)
3054 return gfc_implicit_pure (comp->ts.interface);
3055 else if (e->value.function.esym)
3056 return gfc_implicit_pure (e->value.function.esym);
3057 else
3058 return 0;
3062 static bool
3063 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
3064 int *f ATTRIBUTE_UNUSED)
3066 const char *name;
3068 /* Don't bother recursing into other statement functions
3069 since they will be checked individually for purity. */
3070 if (e->expr_type != EXPR_FUNCTION
3071 || !e->symtree
3072 || e->symtree->n.sym == sym
3073 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3074 return false;
3076 return gfc_pure_function (e, &name) ? false : true;
3080 static int
3081 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
3083 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
3087 /* Check if an impure function is allowed in the current context. */
3089 static bool check_pure_function (gfc_expr *e)
3091 const char *name = NULL;
3092 if (!gfc_pure_function (e, &name) && name)
3094 if (forall_flag)
3096 gfc_error ("Reference to impure function %qs at %L inside a "
3097 "FORALL %s", name, &e->where,
3098 forall_flag == 2 ? "mask" : "block");
3099 return false;
3101 else if (gfc_do_concurrent_flag)
3103 gfc_error ("Reference to impure function %qs at %L inside a "
3104 "DO CONCURRENT %s", name, &e->where,
3105 gfc_do_concurrent_flag == 2 ? "mask" : "block");
3106 return false;
3108 else if (gfc_pure (NULL))
3110 gfc_error ("Reference to impure function %qs at %L "
3111 "within a PURE procedure", name, &e->where);
3112 return false;
3114 if (!gfc_implicit_pure_function (e))
3115 gfc_unset_implicit_pure (NULL);
3117 return true;
3121 /* Update current procedure's array_outer_dependency flag, considering
3122 a call to procedure SYM. */
3124 static void
3125 update_current_proc_array_outer_dependency (gfc_symbol *sym)
3127 /* Check to see if this is a sibling function that has not yet
3128 been resolved. */
3129 gfc_namespace *sibling = gfc_current_ns->sibling;
3130 for (; sibling; sibling = sibling->sibling)
3132 if (sibling->proc_name == sym)
3134 gfc_resolve (sibling);
3135 break;
3139 /* If SYM has references to outer arrays, so has the procedure calling
3140 SYM. If SYM is a procedure pointer, we can assume the worst. */
3141 if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
3142 && gfc_current_ns->proc_name)
3143 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3147 /* Resolve a function call, which means resolving the arguments, then figuring
3148 out which entity the name refers to. */
3150 static bool
3151 resolve_function (gfc_expr *expr)
3153 gfc_actual_arglist *arg;
3154 gfc_symbol *sym;
3155 bool t;
3156 int temp;
3157 procedure_type p = PROC_INTRINSIC;
3158 bool no_formal_args;
3160 sym = NULL;
3161 if (expr->symtree)
3162 sym = expr->symtree->n.sym;
3164 /* If this is a procedure pointer component, it has already been resolved. */
3165 if (gfc_is_proc_ptr_comp (expr))
3166 return true;
3168 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3169 another caf_get. */
3170 if (sym && sym->attr.intrinsic
3171 && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
3172 || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
3173 return true;
3175 if (expr->ref)
3177 gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
3178 &expr->where);
3179 return false;
3182 if (sym && sym->attr.intrinsic
3183 && !gfc_resolve_intrinsic (sym, &expr->where))
3184 return false;
3186 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3188 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
3189 return false;
3192 /* If this is a deferred TBP with an abstract interface (which may
3193 of course be referenced), expr->value.function.esym will be set. */
3194 if (sym && sym->attr.abstract && !expr->value.function.esym)
3196 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3197 sym->name, &expr->where);
3198 return false;
3201 /* If this is a deferred TBP with an abstract interface, its result
3202 cannot be an assumed length character (F2003: C418). */
3203 if (sym && sym->attr.abstract && sym->attr.function
3204 && sym->result->ts.u.cl
3205 && sym->result->ts.u.cl->length == NULL
3206 && !sym->result->ts.deferred)
3208 gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
3209 "character length result (F2008: C418)", sym->name,
3210 &sym->declared_at);
3211 return false;
3214 /* Switch off assumed size checking and do this again for certain kinds
3215 of procedure, once the procedure itself is resolved. */
3216 need_full_assumed_size++;
3218 if (expr->symtree && expr->symtree->n.sym)
3219 p = expr->symtree->n.sym->attr.proc;
3221 if (expr->value.function.isym && expr->value.function.isym->inquiry)
3222 inquiry_argument = true;
3223 no_formal_args = sym && is_external_proc (sym)
3224 && gfc_sym_get_dummy_args (sym) == NULL;
3226 if (!resolve_actual_arglist (expr->value.function.actual,
3227 p, no_formal_args))
3229 inquiry_argument = false;
3230 return false;
3233 inquiry_argument = false;
3235 /* Resume assumed_size checking. */
3236 need_full_assumed_size--;
3238 /* If the procedure is external, check for usage. */
3239 if (sym && is_external_proc (sym))
3240 resolve_global_procedure (sym, &expr->where, 0);
3242 if (sym && sym->ts.type == BT_CHARACTER
3243 && sym->ts.u.cl
3244 && sym->ts.u.cl->length == NULL
3245 && !sym->attr.dummy
3246 && !sym->ts.deferred
3247 && expr->value.function.esym == NULL
3248 && !sym->attr.contained)
3250 /* Internal procedures are taken care of in resolve_contained_fntype. */
3251 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3252 "be used at %L since it is not a dummy argument",
3253 sym->name, &expr->where);
3254 return false;
3257 /* See if function is already resolved. */
3259 if (expr->value.function.name != NULL
3260 || expr->value.function.isym != NULL)
3262 if (expr->ts.type == BT_UNKNOWN)
3263 expr->ts = sym->ts;
3264 t = true;
3266 else
3268 /* Apply the rules of section 14.1.2. */
3270 switch (procedure_kind (sym))
3272 case PTYPE_GENERIC:
3273 t = resolve_generic_f (expr);
3274 break;
3276 case PTYPE_SPECIFIC:
3277 t = resolve_specific_f (expr);
3278 break;
3280 case PTYPE_UNKNOWN:
3281 t = resolve_unknown_f (expr);
3282 break;
3284 default:
3285 gfc_internal_error ("resolve_function(): bad function type");
3289 /* If the expression is still a function (it might have simplified),
3290 then we check to see if we are calling an elemental function. */
3292 if (expr->expr_type != EXPR_FUNCTION)
3293 return t;
3295 /* Walk the argument list looking for invalid BOZ. */
3296 for (arg = expr->value.function.actual; arg; arg = arg->next)
3297 if (arg->expr && arg->expr->ts.type == BT_BOZ)
3299 gfc_error ("A BOZ literal constant at %L cannot appear as an "
3300 "actual argument in a function reference",
3301 &arg->expr->where);
3302 return false;
3305 temp = need_full_assumed_size;
3306 need_full_assumed_size = 0;
3308 if (!resolve_elemental_actual (expr, NULL))
3309 return false;
3311 if (omp_workshare_flag
3312 && expr->value.function.esym
3313 && ! gfc_elemental (expr->value.function.esym))
3315 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3316 "in WORKSHARE construct", expr->value.function.esym->name,
3317 &expr->where);
3318 t = false;
3321 #define GENERIC_ID expr->value.function.isym->id
3322 else if (expr->value.function.actual != NULL
3323 && expr->value.function.isym != NULL
3324 && GENERIC_ID != GFC_ISYM_LBOUND
3325 && GENERIC_ID != GFC_ISYM_LCOBOUND
3326 && GENERIC_ID != GFC_ISYM_UCOBOUND
3327 && GENERIC_ID != GFC_ISYM_LEN
3328 && GENERIC_ID != GFC_ISYM_LOC
3329 && GENERIC_ID != GFC_ISYM_C_LOC
3330 && GENERIC_ID != GFC_ISYM_PRESENT)
3332 /* Array intrinsics must also have the last upper bound of an
3333 assumed size array argument. UBOUND and SIZE have to be
3334 excluded from the check if the second argument is anything
3335 than a constant. */
3337 for (arg = expr->value.function.actual; arg; arg = arg->next)
3339 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3340 && arg == expr->value.function.actual
3341 && arg->next != NULL && arg->next->expr)
3343 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3344 break;
3346 if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
3347 break;
3349 if ((int)mpz_get_si (arg->next->expr->value.integer)
3350 < arg->expr->rank)
3351 break;
3354 if (arg->expr != NULL
3355 && arg->expr->rank > 0
3356 && resolve_assumed_size_actual (arg->expr))
3357 return false;
3360 #undef GENERIC_ID
3362 need_full_assumed_size = temp;
3364 if (!check_pure_function(expr))
3365 t = false;
3367 /* Functions without the RECURSIVE attribution are not allowed to
3368 * call themselves. */
3369 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3371 gfc_symbol *esym;
3372 esym = expr->value.function.esym;
3374 if (is_illegal_recursion (esym, gfc_current_ns))
3376 if (esym->attr.entry && esym->ns->entries)
3377 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3378 " function %qs is not RECURSIVE",
3379 esym->name, &expr->where, esym->ns->entries->sym->name);
3380 else
3381 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3382 " is not RECURSIVE", esym->name, &expr->where);
3384 t = false;
3388 /* Character lengths of use associated functions may contains references to
3389 symbols not referenced from the current program unit otherwise. Make sure
3390 those symbols are marked as referenced. */
3392 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3393 && expr->value.function.esym->attr.use_assoc)
3395 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3398 /* Make sure that the expression has a typespec that works. */
3399 if (expr->ts.type == BT_UNKNOWN)
3401 if (expr->symtree->n.sym->result
3402 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3403 && !expr->symtree->n.sym->result->attr.proc_pointer)
3404 expr->ts = expr->symtree->n.sym->result->ts;
3407 if (!expr->ref && !expr->value.function.isym)
3409 if (expr->value.function.esym)
3410 update_current_proc_array_outer_dependency (expr->value.function.esym);
3411 else
3412 update_current_proc_array_outer_dependency (sym);
3414 else if (expr->ref)
3415 /* typebound procedure: Assume the worst. */
3416 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3418 if (expr->value.function.esym
3419 && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3420 gfc_warning (OPT_Wdeprecated_declarations,
3421 "Using function %qs at %L is deprecated",
3422 sym->name, &expr->where);
3423 return t;
3427 /************* Subroutine resolution *************/
3429 static bool
3430 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3432 if (gfc_pure (sym))
3433 return true;
3435 if (forall_flag)
3437 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3438 name, loc);
3439 return false;
3441 else if (gfc_do_concurrent_flag)
3443 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3444 "PURE", name, loc);
3445 return false;
3447 else if (gfc_pure (NULL))
3449 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3450 return false;
3453 gfc_unset_implicit_pure (NULL);
3454 return true;
3458 static match
3459 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3461 gfc_symbol *s;
3463 if (sym->attr.generic)
3465 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3466 if (s != NULL)
3468 c->resolved_sym = s;
3469 if (!pure_subroutine (s, s->name, &c->loc))
3470 return MATCH_ERROR;
3471 return MATCH_YES;
3474 /* TODO: Need to search for elemental references in generic interface. */
3477 if (sym->attr.intrinsic)
3478 return gfc_intrinsic_sub_interface (c, 0);
3480 return MATCH_NO;
3484 static bool
3485 resolve_generic_s (gfc_code *c)
3487 gfc_symbol *sym;
3488 match m;
3490 sym = c->symtree->n.sym;
3492 for (;;)
3494 m = resolve_generic_s0 (c, sym);
3495 if (m == MATCH_YES)
3496 return true;
3497 else if (m == MATCH_ERROR)
3498 return false;
3500 generic:
3501 if (sym->ns->parent == NULL)
3502 break;
3503 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3505 if (sym == NULL)
3506 break;
3507 if (!generic_sym (sym))
3508 goto generic;
3511 /* Last ditch attempt. See if the reference is to an intrinsic
3512 that possesses a matching interface. 14.1.2.4 */
3513 sym = c->symtree->n.sym;
3515 if (!gfc_is_intrinsic (sym, 1, c->loc))
3517 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3518 sym->name, &c->loc);
3519 return false;
3522 m = gfc_intrinsic_sub_interface (c, 0);
3523 if (m == MATCH_YES)
3524 return true;
3525 if (m == MATCH_NO)
3526 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3527 "intrinsic subroutine interface", sym->name, &c->loc);
3529 return false;
3533 /* Resolve a subroutine call known to be specific. */
3535 static match
3536 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3538 match m;
3540 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3542 if (sym->attr.dummy)
3544 sym->attr.proc = PROC_DUMMY;
3545 goto found;
3548 sym->attr.proc = PROC_EXTERNAL;
3549 goto found;
3552 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3553 goto found;
3555 if (sym->attr.intrinsic)
3557 m = gfc_intrinsic_sub_interface (c, 1);
3558 if (m == MATCH_YES)
3559 return MATCH_YES;
3560 if (m == MATCH_NO)
3561 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3562 "with an intrinsic", sym->name, &c->loc);
3564 return MATCH_ERROR;
3567 return MATCH_NO;
3569 found:
3570 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3572 c->resolved_sym = sym;
3573 if (!pure_subroutine (sym, sym->name, &c->loc))
3574 return MATCH_ERROR;
3576 return MATCH_YES;
3580 static bool
3581 resolve_specific_s (gfc_code *c)
3583 gfc_symbol *sym;
3584 match m;
3586 sym = c->symtree->n.sym;
3588 for (;;)
3590 m = resolve_specific_s0 (c, sym);
3591 if (m == MATCH_YES)
3592 return true;
3593 if (m == MATCH_ERROR)
3594 return false;
3596 if (sym->ns->parent == NULL)
3597 break;
3599 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3601 if (sym == NULL)
3602 break;
3605 sym = c->symtree->n.sym;
3606 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3607 sym->name, &c->loc);
3609 return false;
3613 /* Resolve a subroutine call not known to be generic nor specific. */
3615 static bool
3616 resolve_unknown_s (gfc_code *c)
3618 gfc_symbol *sym;
3620 sym = c->symtree->n.sym;
3622 if (sym->attr.dummy)
3624 sym->attr.proc = PROC_DUMMY;
3625 goto found;
3628 /* See if we have an intrinsic function reference. */
3630 if (gfc_is_intrinsic (sym, 1, c->loc))
3632 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3633 return true;
3634 return false;
3637 /* The reference is to an external name. */
3639 found:
3640 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3642 c->resolved_sym = sym;
3644 return pure_subroutine (sym, sym->name, &c->loc);
3648 /* Resolve a subroutine call. Although it was tempting to use the same code
3649 for functions, subroutines and functions are stored differently and this
3650 makes things awkward. */
3652 static bool
3653 resolve_call (gfc_code *c)
3655 bool t;
3656 procedure_type ptype = PROC_INTRINSIC;
3657 gfc_symbol *csym, *sym;
3658 bool no_formal_args;
3660 csym = c->symtree ? c->symtree->n.sym : NULL;
3662 if (csym && csym->ts.type != BT_UNKNOWN)
3664 gfc_error ("%qs at %L has a type, which is not consistent with "
3665 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3666 return false;
3669 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3671 gfc_symtree *st;
3672 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3673 sym = st ? st->n.sym : NULL;
3674 if (sym && csym != sym
3675 && sym->ns == gfc_current_ns
3676 && sym->attr.flavor == FL_PROCEDURE
3677 && sym->attr.contained)
3679 sym->refs++;
3680 if (csym->attr.generic)
3681 c->symtree->n.sym = sym;
3682 else
3683 c->symtree = st;
3684 csym = c->symtree->n.sym;
3688 /* If this ia a deferred TBP, c->expr1 will be set. */
3689 if (!c->expr1 && csym)
3691 if (csym->attr.abstract)
3693 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3694 csym->name, &c->loc);
3695 return false;
3698 /* Subroutines without the RECURSIVE attribution are not allowed to
3699 call themselves. */
3700 if (is_illegal_recursion (csym, gfc_current_ns))
3702 if (csym->attr.entry && csym->ns->entries)
3703 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3704 "as subroutine %qs is not RECURSIVE",
3705 csym->name, &c->loc, csym->ns->entries->sym->name);
3706 else
3707 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3708 "as it is not RECURSIVE", csym->name, &c->loc);
3710 t = false;
3714 /* Switch off assumed size checking and do this again for certain kinds
3715 of procedure, once the procedure itself is resolved. */
3716 need_full_assumed_size++;
3718 if (csym)
3719 ptype = csym->attr.proc;
3721 no_formal_args = csym && is_external_proc (csym)
3722 && gfc_sym_get_dummy_args (csym) == NULL;
3723 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3724 return false;
3726 /* Resume assumed_size checking. */
3727 need_full_assumed_size--;
3729 /* If external, check for usage. */
3730 if (csym && is_external_proc (csym))
3731 resolve_global_procedure (csym, &c->loc, 1);
3733 t = true;
3734 if (c->resolved_sym == NULL)
3736 c->resolved_isym = NULL;
3737 switch (procedure_kind (csym))
3739 case PTYPE_GENERIC:
3740 t = resolve_generic_s (c);
3741 break;
3743 case PTYPE_SPECIFIC:
3744 t = resolve_specific_s (c);
3745 break;
3747 case PTYPE_UNKNOWN:
3748 t = resolve_unknown_s (c);
3749 break;
3751 default:
3752 gfc_internal_error ("resolve_subroutine(): bad function type");
3756 /* Some checks of elemental subroutine actual arguments. */
3757 if (!resolve_elemental_actual (NULL, c))
3758 return false;
3760 if (!c->expr1)
3761 update_current_proc_array_outer_dependency (csym);
3762 else
3763 /* Typebound procedure: Assume the worst. */
3764 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3766 if (c->resolved_sym
3767 && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3768 gfc_warning (OPT_Wdeprecated_declarations,
3769 "Using subroutine %qs at %L is deprecated",
3770 c->resolved_sym->name, &c->loc);
3772 return t;
3776 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3777 op1->shape and op2->shape are non-NULL return true if their shapes
3778 match. If both op1->shape and op2->shape are non-NULL return false
3779 if their shapes do not match. If either op1->shape or op2->shape is
3780 NULL, return true. */
3782 static bool
3783 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3785 bool t;
3786 int i;
3788 t = true;
3790 if (op1->shape != NULL && op2->shape != NULL)
3792 for (i = 0; i < op1->rank; i++)
3794 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3796 gfc_error ("Shapes for operands at %L and %L are not conformable",
3797 &op1->where, &op2->where);
3798 t = false;
3799 break;
3804 return t;
3807 /* Convert a logical operator to the corresponding bitwise intrinsic call.
3808 For example A .AND. B becomes IAND(A, B). */
3809 static gfc_expr *
3810 logical_to_bitwise (gfc_expr *e)
3812 gfc_expr *tmp, *op1, *op2;
3813 gfc_isym_id isym;
3814 gfc_actual_arglist *args = NULL;
3816 gcc_assert (e->expr_type == EXPR_OP);
3818 isym = GFC_ISYM_NONE;
3819 op1 = e->value.op.op1;
3820 op2 = e->value.op.op2;
3822 switch (e->value.op.op)
3824 case INTRINSIC_NOT:
3825 isym = GFC_ISYM_NOT;
3826 break;
3827 case INTRINSIC_AND:
3828 isym = GFC_ISYM_IAND;
3829 break;
3830 case INTRINSIC_OR:
3831 isym = GFC_ISYM_IOR;
3832 break;
3833 case INTRINSIC_NEQV:
3834 isym = GFC_ISYM_IEOR;
3835 break;
3836 case INTRINSIC_EQV:
3837 /* "Bitwise eqv" is just the complement of NEQV === IEOR.
3838 Change the old expression to NEQV, which will get replaced by IEOR,
3839 and wrap it in NOT. */
3840 tmp = gfc_copy_expr (e);
3841 tmp->value.op.op = INTRINSIC_NEQV;
3842 tmp = logical_to_bitwise (tmp);
3843 isym = GFC_ISYM_NOT;
3844 op1 = tmp;
3845 op2 = NULL;
3846 break;
3847 default:
3848 gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
3851 /* Inherit the original operation's operands as arguments. */
3852 args = gfc_get_actual_arglist ();
3853 args->expr = op1;
3854 if (op2)
3856 args->next = gfc_get_actual_arglist ();
3857 args->next->expr = op2;
3860 /* Convert the expression to a function call. */
3861 e->expr_type = EXPR_FUNCTION;
3862 e->value.function.actual = args;
3863 e->value.function.isym = gfc_intrinsic_function_by_id (isym);
3864 e->value.function.name = e->value.function.isym->name;
3865 e->value.function.esym = NULL;
3867 /* Make up a pre-resolved function call symtree if we need to. */
3868 if (!e->symtree || !e->symtree->n.sym)
3870 gfc_symbol *sym;
3871 gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
3872 sym = e->symtree->n.sym;
3873 sym->result = sym;
3874 sym->attr.flavor = FL_PROCEDURE;
3875 sym->attr.function = 1;
3876 sym->attr.elemental = 1;
3877 sym->attr.pure = 1;
3878 sym->attr.referenced = 1;
3879 gfc_intrinsic_symbol (sym);
3880 gfc_commit_symbol (sym);
3883 args->name = e->value.function.isym->formal->name;
3884 if (e->value.function.isym->formal->next)
3885 args->next->name = e->value.function.isym->formal->next->name;
3887 return e;
3890 /* Recursively append candidate UOP to CANDIDATES. Store the number of
3891 candidates in CANDIDATES_LEN. */
3892 static void
3893 lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
3894 char **&candidates,
3895 size_t &candidates_len)
3897 gfc_symtree *p;
3899 if (uop == NULL)
3900 return;
3902 /* Not sure how to properly filter here. Use all for a start.
3903 n.uop.op is NULL for empty interface operators (is that legal?) disregard
3904 these as i suppose they don't make terribly sense. */
3906 if (uop->n.uop->op != NULL)
3907 vec_push (candidates, candidates_len, uop->name);
3909 p = uop->left;
3910 if (p)
3911 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
3913 p = uop->right;
3914 if (p)
3915 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
3918 /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
3920 static const char*
3921 lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
3923 char **candidates = NULL;
3924 size_t candidates_len = 0;
3925 lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
3926 return gfc_closest_fuzzy_match (op, candidates);
3930 /* Callback finding an impure function as an operand to an .and. or
3931 .or. expression. Remember the last function warned about to
3932 avoid double warnings when recursing. */
3934 static int
3935 impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
3936 void *data)
3938 gfc_expr *f = *e;
3939 const char *name;
3940 static gfc_expr *last = NULL;
3941 bool *found = (bool *) data;
3943 if (f->expr_type == EXPR_FUNCTION)
3945 *found = 1;
3946 if (f != last && !gfc_pure_function (f, &name)
3947 && !gfc_implicit_pure_function (f))
3949 if (name)
3950 gfc_warning (OPT_Wfunction_elimination,
3951 "Impure function %qs at %L might not be evaluated",
3952 name, &f->where);
3953 else
3954 gfc_warning (OPT_Wfunction_elimination,
3955 "Impure function at %L might not be evaluated",
3956 &f->where);
3958 last = f;
3961 return 0;
3964 /* Return true if TYPE is character based, false otherwise. */
3966 static int
3967 is_character_based (bt type)
3969 return type == BT_CHARACTER || type == BT_HOLLERITH;
3973 /* If expression is a hollerith, convert it to character and issue a warning
3974 for the conversion. */
3976 static void
3977 convert_hollerith_to_character (gfc_expr *e)
3979 if (e->ts.type == BT_HOLLERITH)
3981 gfc_typespec t;
3982 gfc_clear_ts (&t);
3983 t.type = BT_CHARACTER;
3984 t.kind = e->ts.kind;
3985 gfc_convert_type_warn (e, &t, 2, 1);
3989 /* Convert to numeric and issue a warning for the conversion. */
3991 static void
3992 convert_to_numeric (gfc_expr *a, gfc_expr *b)
3994 gfc_typespec t;
3995 gfc_clear_ts (&t);
3996 t.type = b->ts.type;
3997 t.kind = b->ts.kind;
3998 gfc_convert_type_warn (a, &t, 2, 1);
4001 /* Resolve an operator expression node. This can involve replacing the
4002 operation with a user defined function call. */
4004 static bool
4005 resolve_operator (gfc_expr *e)
4007 gfc_expr *op1, *op2;
4008 /* One error uses 3 names; additional space for wording (also via gettext). */
4009 char msg[3*GFC_MAX_SYMBOL_LEN + 1 + 50];
4010 bool dual_locus_error;
4011 bool t = true;
4013 /* Resolve all subnodes-- give them types. */
4015 switch (e->value.op.op)
4017 default:
4018 if (!gfc_resolve_expr (e->value.op.op2))
4019 return false;
4021 /* Fall through. */
4023 case INTRINSIC_NOT:
4024 case INTRINSIC_UPLUS:
4025 case INTRINSIC_UMINUS:
4026 case INTRINSIC_PARENTHESES:
4027 if (!gfc_resolve_expr (e->value.op.op1))
4028 return false;
4029 if (e->value.op.op1
4030 && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
4032 gfc_error ("BOZ literal constant at %L cannot be an operand of "
4033 "unary operator %qs", &e->value.op.op1->where,
4034 gfc_op2string (e->value.op.op));
4035 return false;
4037 break;
4040 /* Typecheck the new node. */
4042 op1 = e->value.op.op1;
4043 op2 = e->value.op.op2;
4044 if (op1 == NULL && op2 == NULL)
4045 return false;
4047 dual_locus_error = false;
4049 /* op1 and op2 cannot both be BOZ. */
4050 if (op1 && op1->ts.type == BT_BOZ
4051 && op2 && op2->ts.type == BT_BOZ)
4053 gfc_error ("Operands at %L and %L cannot appear as operands of "
4054 "binary operator %qs", &op1->where, &op2->where,
4055 gfc_op2string (e->value.op.op));
4056 return false;
4059 if ((op1 && op1->expr_type == EXPR_NULL)
4060 || (op2 && op2->expr_type == EXPR_NULL))
4062 snprintf (msg, sizeof (msg),
4063 _("Invalid context for NULL() pointer at %%L"));
4064 goto bad_op;
4067 switch (e->value.op.op)
4069 case INTRINSIC_UPLUS:
4070 case INTRINSIC_UMINUS:
4071 if (op1->ts.type == BT_INTEGER
4072 || op1->ts.type == BT_REAL
4073 || op1->ts.type == BT_COMPLEX)
4075 e->ts = op1->ts;
4076 break;
4079 snprintf (msg, sizeof (msg),
4080 _("Operand of unary numeric operator %%<%s%%> at %%L is %s"),
4081 gfc_op2string (e->value.op.op), gfc_typename (e));
4082 goto bad_op;
4084 case INTRINSIC_PLUS:
4085 case INTRINSIC_MINUS:
4086 case INTRINSIC_TIMES:
4087 case INTRINSIC_DIVIDE:
4088 case INTRINSIC_POWER:
4089 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4091 gfc_type_convert_binary (e, 1);
4092 break;
4095 if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
4096 snprintf (msg, sizeof (msg),
4097 _("Unexpected derived-type entities in binary intrinsic "
4098 "numeric operator %%<%s%%> at %%L"),
4099 gfc_op2string (e->value.op.op));
4100 else
4101 snprintf (msg, sizeof(msg),
4102 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"),
4103 gfc_op2string (e->value.op.op), gfc_typename (op1),
4104 gfc_typename (op2));
4105 goto bad_op;
4107 case INTRINSIC_CONCAT:
4108 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4109 && op1->ts.kind == op2->ts.kind)
4111 e->ts.type = BT_CHARACTER;
4112 e->ts.kind = op1->ts.kind;
4113 break;
4116 snprintf (msg, sizeof (msg),
4117 _("Operands of string concatenation operator at %%L are %s/%s"),
4118 gfc_typename (op1), gfc_typename (op2));
4119 goto bad_op;
4121 case INTRINSIC_AND:
4122 case INTRINSIC_OR:
4123 case INTRINSIC_EQV:
4124 case INTRINSIC_NEQV:
4125 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4127 e->ts.type = BT_LOGICAL;
4128 e->ts.kind = gfc_kind_max (op1, op2);
4129 if (op1->ts.kind < e->ts.kind)
4130 gfc_convert_type (op1, &e->ts, 2);
4131 else if (op2->ts.kind < e->ts.kind)
4132 gfc_convert_type (op2, &e->ts, 2);
4134 if (flag_frontend_optimize &&
4135 (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
4137 /* Warn about short-circuiting
4138 with impure function as second operand. */
4139 bool op2_f = false;
4140 gfc_expr_walker (&op2, impure_function_callback, &op2_f);
4142 break;
4145 /* Logical ops on integers become bitwise ops with -fdec. */
4146 else if (flag_dec
4147 && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
4149 e->ts.type = BT_INTEGER;
4150 e->ts.kind = gfc_kind_max (op1, op2);
4151 if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
4152 gfc_convert_type (op1, &e->ts, 1);
4153 if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
4154 gfc_convert_type (op2, &e->ts, 1);
4155 e = logical_to_bitwise (e);
4156 goto simplify_op;
4159 snprintf (msg, sizeof (msg),
4160 _("Operands of logical operator %%<%s%%> at %%L are %s/%s"),
4161 gfc_op2string (e->value.op.op), gfc_typename (op1),
4162 gfc_typename (op2));
4164 goto bad_op;
4166 case INTRINSIC_NOT:
4167 /* Logical ops on integers become bitwise ops with -fdec. */
4168 if (flag_dec && op1->ts.type == BT_INTEGER)
4170 e->ts.type = BT_INTEGER;
4171 e->ts.kind = op1->ts.kind;
4172 e = logical_to_bitwise (e);
4173 goto simplify_op;
4176 if (op1->ts.type == BT_LOGICAL)
4178 e->ts.type = BT_LOGICAL;
4179 e->ts.kind = op1->ts.kind;
4180 break;
4183 snprintf (msg, sizeof (msg), _("Operand of .not. operator at %%L is %s"),
4184 gfc_typename (op1));
4185 goto bad_op;
4187 case INTRINSIC_GT:
4188 case INTRINSIC_GT_OS:
4189 case INTRINSIC_GE:
4190 case INTRINSIC_GE_OS:
4191 case INTRINSIC_LT:
4192 case INTRINSIC_LT_OS:
4193 case INTRINSIC_LE:
4194 case INTRINSIC_LE_OS:
4195 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
4197 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
4198 goto bad_op;
4201 /* Fall through. */
4203 case INTRINSIC_EQ:
4204 case INTRINSIC_EQ_OS:
4205 case INTRINSIC_NE:
4206 case INTRINSIC_NE_OS:
4208 if (flag_dec
4209 && is_character_based (op1->ts.type)
4210 && is_character_based (op2->ts.type))
4212 convert_hollerith_to_character (op1);
4213 convert_hollerith_to_character (op2);
4216 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4217 && op1->ts.kind == op2->ts.kind)
4219 e->ts.type = BT_LOGICAL;
4220 e->ts.kind = gfc_default_logical_kind;
4221 break;
4224 /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4225 if (op1->ts.type == BT_BOZ)
4227 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4228 "as an operand of a relational operator"),
4229 &op1->where))
4230 return false;
4232 if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
4233 return false;
4235 if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
4236 return false;
4239 /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4240 if (op2->ts.type == BT_BOZ)
4242 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4243 " as an operand of a relational operator"),
4244 &op2->where))
4245 return false;
4247 if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
4248 return false;
4250 if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
4251 return false;
4253 if (flag_dec
4254 && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
4255 convert_to_numeric (op1, op2);
4257 if (flag_dec
4258 && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
4259 convert_to_numeric (op2, op1);
4261 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4263 gfc_type_convert_binary (e, 1);
4265 e->ts.type = BT_LOGICAL;
4266 e->ts.kind = gfc_default_logical_kind;
4268 if (warn_compare_reals)
4270 gfc_intrinsic_op op = e->value.op.op;
4272 /* Type conversion has made sure that the types of op1 and op2
4273 agree, so it is only necessary to check the first one. */
4274 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
4275 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
4276 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
4278 const char *msg;
4280 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
4281 msg = G_("Equality comparison for %s at %L");
4282 else
4283 msg = G_("Inequality comparison for %s at %L");
4285 gfc_warning (OPT_Wcompare_reals, msg,
4286 gfc_typename (op1), &op1->where);
4290 break;
4293 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4294 snprintf (msg, sizeof (msg),
4295 _("Logicals at %%L must be compared with %s instead of %s"),
4296 (e->value.op.op == INTRINSIC_EQ
4297 || e->value.op.op == INTRINSIC_EQ_OS)
4298 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
4299 else
4300 snprintf (msg, sizeof (msg),
4301 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"),
4302 gfc_op2string (e->value.op.op), gfc_typename (op1),
4303 gfc_typename (op2));
4305 goto bad_op;
4307 case INTRINSIC_USER:
4308 if (e->value.op.uop->op == NULL)
4310 const char *name = e->value.op.uop->name;
4311 const char *guessed;
4312 guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
4313 if (guessed)
4314 snprintf (msg, sizeof (msg),
4315 _("Unknown operator %%<%s%%> at %%L; did you mean '%s'?"),
4316 name, guessed);
4317 else
4318 snprintf (msg, sizeof (msg), _("Unknown operator %%<%s%%> at %%L"),
4319 name);
4321 else if (op2 == NULL)
4322 snprintf (msg, sizeof (msg),
4323 _("Operand of user operator %%<%s%%> at %%L is %s"),
4324 e->value.op.uop->name, gfc_typename (op1));
4325 else
4327 snprintf (msg, sizeof (msg),
4328 _("Operands of user operator %%<%s%%> at %%L are %s/%s"),
4329 e->value.op.uop->name, gfc_typename (op1),
4330 gfc_typename (op2));
4331 e->value.op.uop->op->sym->attr.referenced = 1;
4334 goto bad_op;
4336 case INTRINSIC_PARENTHESES:
4337 e->ts = op1->ts;
4338 if (e->ts.type == BT_CHARACTER)
4339 e->ts.u.cl = op1->ts.u.cl;
4340 break;
4342 default:
4343 gfc_internal_error ("resolve_operator(): Bad intrinsic");
4346 /* Deal with arrayness of an operand through an operator. */
4348 switch (e->value.op.op)
4350 case INTRINSIC_PLUS:
4351 case INTRINSIC_MINUS:
4352 case INTRINSIC_TIMES:
4353 case INTRINSIC_DIVIDE:
4354 case INTRINSIC_POWER:
4355 case INTRINSIC_CONCAT:
4356 case INTRINSIC_AND:
4357 case INTRINSIC_OR:
4358 case INTRINSIC_EQV:
4359 case INTRINSIC_NEQV:
4360 case INTRINSIC_EQ:
4361 case INTRINSIC_EQ_OS:
4362 case INTRINSIC_NE:
4363 case INTRINSIC_NE_OS:
4364 case INTRINSIC_GT:
4365 case INTRINSIC_GT_OS:
4366 case INTRINSIC_GE:
4367 case INTRINSIC_GE_OS:
4368 case INTRINSIC_LT:
4369 case INTRINSIC_LT_OS:
4370 case INTRINSIC_LE:
4371 case INTRINSIC_LE_OS:
4373 if (op1->rank == 0 && op2->rank == 0)
4374 e->rank = 0;
4376 if (op1->rank == 0 && op2->rank != 0)
4378 e->rank = op2->rank;
4380 if (e->shape == NULL)
4381 e->shape = gfc_copy_shape (op2->shape, op2->rank);
4384 if (op1->rank != 0 && op2->rank == 0)
4386 e->rank = op1->rank;
4388 if (e->shape == NULL)
4389 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4392 if (op1->rank != 0 && op2->rank != 0)
4394 if (op1->rank == op2->rank)
4396 e->rank = op1->rank;
4397 if (e->shape == NULL)
4399 t = compare_shapes (op1, op2);
4400 if (!t)
4401 e->shape = NULL;
4402 else
4403 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4406 else
4408 /* Allow higher level expressions to work. */
4409 e->rank = 0;
4411 /* Try user-defined operators, and otherwise throw an error. */
4412 dual_locus_error = true;
4413 snprintf (msg, sizeof (msg),
4414 _("Inconsistent ranks for operator at %%L and %%L"));
4415 goto bad_op;
4419 break;
4421 case INTRINSIC_PARENTHESES:
4422 case INTRINSIC_NOT:
4423 case INTRINSIC_UPLUS:
4424 case INTRINSIC_UMINUS:
4425 /* Simply copy arrayness attribute */
4426 e->rank = op1->rank;
4428 if (e->shape == NULL)
4429 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4431 break;
4433 default:
4434 break;
4437 simplify_op:
4439 /* Attempt to simplify the expression. */
4440 if (t)
4442 t = gfc_simplify_expr (e, 0);
4443 /* Some calls do not succeed in simplification and return false
4444 even though there is no error; e.g. variable references to
4445 PARAMETER arrays. */
4446 if (!gfc_is_constant_expr (e))
4447 t = true;
4449 return t;
4451 bad_op:
4454 match m = gfc_extend_expr (e);
4455 if (m == MATCH_YES)
4456 return true;
4457 if (m == MATCH_ERROR)
4458 return false;
4461 if (dual_locus_error)
4462 gfc_error (msg, &op1->where, &op2->where);
4463 else
4464 gfc_error (msg, &e->where);
4466 return false;
4470 /************** Array resolution subroutines **************/
4472 enum compare_result
4473 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
4475 /* Compare two integer expressions. */
4477 static compare_result
4478 compare_bound (gfc_expr *a, gfc_expr *b)
4480 int i;
4482 if (a == NULL || a->expr_type != EXPR_CONSTANT
4483 || b == NULL || b->expr_type != EXPR_CONSTANT)
4484 return CMP_UNKNOWN;
4486 /* If either of the types isn't INTEGER, we must have
4487 raised an error earlier. */
4489 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4490 return CMP_UNKNOWN;
4492 i = mpz_cmp (a->value.integer, b->value.integer);
4494 if (i < 0)
4495 return CMP_LT;
4496 if (i > 0)
4497 return CMP_GT;
4498 return CMP_EQ;
4502 /* Compare an integer expression with an integer. */
4504 static compare_result
4505 compare_bound_int (gfc_expr *a, int b)
4507 int i;
4509 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4510 return CMP_UNKNOWN;
4512 if (a->ts.type != BT_INTEGER)
4513 gfc_internal_error ("compare_bound_int(): Bad expression");
4515 i = mpz_cmp_si (a->value.integer, b);
4517 if (i < 0)
4518 return CMP_LT;
4519 if (i > 0)
4520 return CMP_GT;
4521 return CMP_EQ;
4525 /* Compare an integer expression with a mpz_t. */
4527 static compare_result
4528 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4530 int i;
4532 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4533 return CMP_UNKNOWN;
4535 if (a->ts.type != BT_INTEGER)
4536 gfc_internal_error ("compare_bound_int(): Bad expression");
4538 i = mpz_cmp (a->value.integer, b);
4540 if (i < 0)
4541 return CMP_LT;
4542 if (i > 0)
4543 return CMP_GT;
4544 return CMP_EQ;
4548 /* Compute the last value of a sequence given by a triplet.
4549 Return 0 if it wasn't able to compute the last value, or if the
4550 sequence if empty, and 1 otherwise. */
4552 static int
4553 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4554 gfc_expr *stride, mpz_t last)
4556 mpz_t rem;
4558 if (start == NULL || start->expr_type != EXPR_CONSTANT
4559 || end == NULL || end->expr_type != EXPR_CONSTANT
4560 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4561 return 0;
4563 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4564 || (stride != NULL && stride->ts.type != BT_INTEGER))
4565 return 0;
4567 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
4569 if (compare_bound (start, end) == CMP_GT)
4570 return 0;
4571 mpz_set (last, end->value.integer);
4572 return 1;
4575 if (compare_bound_int (stride, 0) == CMP_GT)
4577 /* Stride is positive */
4578 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4579 return 0;
4581 else
4583 /* Stride is negative */
4584 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4585 return 0;
4588 mpz_init (rem);
4589 mpz_sub (rem, end->value.integer, start->value.integer);
4590 mpz_tdiv_r (rem, rem, stride->value.integer);
4591 mpz_sub (last, end->value.integer, rem);
4592 mpz_clear (rem);
4594 return 1;
4598 /* Compare a single dimension of an array reference to the array
4599 specification. */
4601 static bool
4602 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4604 mpz_t last_value;
4606 if (ar->dimen_type[i] == DIMEN_STAR)
4608 gcc_assert (ar->stride[i] == NULL);
4609 /* This implies [*] as [*:] and [*:3] are not possible. */
4610 if (ar->start[i] == NULL)
4612 gcc_assert (ar->end[i] == NULL);
4613 return true;
4617 /* Given start, end and stride values, calculate the minimum and
4618 maximum referenced indexes. */
4620 switch (ar->dimen_type[i])
4622 case DIMEN_VECTOR:
4623 case DIMEN_THIS_IMAGE:
4624 break;
4626 case DIMEN_STAR:
4627 case DIMEN_ELEMENT:
4628 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4630 if (i < as->rank)
4631 gfc_warning (0, "Array reference at %L is out of bounds "
4632 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4633 mpz_get_si (ar->start[i]->value.integer),
4634 mpz_get_si (as->lower[i]->value.integer), i+1);
4635 else
4636 gfc_warning (0, "Array reference at %L is out of bounds "
4637 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4638 mpz_get_si (ar->start[i]->value.integer),
4639 mpz_get_si (as->lower[i]->value.integer),
4640 i + 1 - as->rank);
4641 return true;
4643 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4645 if (i < as->rank)
4646 gfc_warning (0, "Array reference at %L is out of bounds "
4647 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4648 mpz_get_si (ar->start[i]->value.integer),
4649 mpz_get_si (as->upper[i]->value.integer), i+1);
4650 else
4651 gfc_warning (0, "Array reference at %L is out of bounds "
4652 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4653 mpz_get_si (ar->start[i]->value.integer),
4654 mpz_get_si (as->upper[i]->value.integer),
4655 i + 1 - as->rank);
4656 return true;
4659 break;
4661 case DIMEN_RANGE:
4663 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4664 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4666 compare_result comp_start_end = compare_bound (AR_START, AR_END);
4668 /* Check for zero stride, which is not allowed. */
4669 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4671 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4672 return false;
4675 /* if start == len || (stride > 0 && start < len)
4676 || (stride < 0 && start > len),
4677 then the array section contains at least one element. In this
4678 case, there is an out-of-bounds access if
4679 (start < lower || start > upper). */
4680 if (compare_bound (AR_START, AR_END) == CMP_EQ
4681 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4682 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4683 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4684 && comp_start_end == CMP_GT))
4686 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4688 gfc_warning (0, "Lower array reference at %L is out of bounds "
4689 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4690 mpz_get_si (AR_START->value.integer),
4691 mpz_get_si (as->lower[i]->value.integer), i+1);
4692 return true;
4694 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4696 gfc_warning (0, "Lower array reference at %L is out of bounds "
4697 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4698 mpz_get_si (AR_START->value.integer),
4699 mpz_get_si (as->upper[i]->value.integer), i+1);
4700 return true;
4704 /* If we can compute the highest index of the array section,
4705 then it also has to be between lower and upper. */
4706 mpz_init (last_value);
4707 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4708 last_value))
4710 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4712 gfc_warning (0, "Upper array reference at %L is out of bounds "
4713 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4714 mpz_get_si (last_value),
4715 mpz_get_si (as->lower[i]->value.integer), i+1);
4716 mpz_clear (last_value);
4717 return true;
4719 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4721 gfc_warning (0, "Upper array reference at %L is out of bounds "
4722 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4723 mpz_get_si (last_value),
4724 mpz_get_si (as->upper[i]->value.integer), i+1);
4725 mpz_clear (last_value);
4726 return true;
4729 mpz_clear (last_value);
4731 #undef AR_START
4732 #undef AR_END
4734 break;
4736 default:
4737 gfc_internal_error ("check_dimension(): Bad array reference");
4740 return true;
4744 /* Compare an array reference with an array specification. */
4746 static bool
4747 compare_spec_to_ref (gfc_array_ref *ar)
4749 gfc_array_spec *as;
4750 int i;
4752 as = ar->as;
4753 i = as->rank - 1;
4754 /* TODO: Full array sections are only allowed as actual parameters. */
4755 if (as->type == AS_ASSUMED_SIZE
4756 && (/*ar->type == AR_FULL
4757 ||*/ (ar->type == AR_SECTION
4758 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4760 gfc_error ("Rightmost upper bound of assumed size array section "
4761 "not specified at %L", &ar->where);
4762 return false;
4765 if (ar->type == AR_FULL)
4766 return true;
4768 if (as->rank != ar->dimen)
4770 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4771 &ar->where, ar->dimen, as->rank);
4772 return false;
4775 /* ar->codimen == 0 is a local array. */
4776 if (as->corank != ar->codimen && ar->codimen != 0)
4778 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4779 &ar->where, ar->codimen, as->corank);
4780 return false;
4783 for (i = 0; i < as->rank; i++)
4784 if (!check_dimension (i, ar, as))
4785 return false;
4787 /* Local access has no coarray spec. */
4788 if (ar->codimen != 0)
4789 for (i = as->rank; i < as->rank + as->corank; i++)
4791 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4792 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4794 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4795 i + 1 - as->rank, &ar->where);
4796 return false;
4798 if (!check_dimension (i, ar, as))
4799 return false;
4802 return true;
4806 /* Resolve one part of an array index. */
4808 static bool
4809 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4810 int force_index_integer_kind)
4812 gfc_typespec ts;
4814 if (index == NULL)
4815 return true;
4817 if (!gfc_resolve_expr (index))
4818 return false;
4820 if (check_scalar && index->rank != 0)
4822 gfc_error ("Array index at %L must be scalar", &index->where);
4823 return false;
4826 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4828 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4829 &index->where, gfc_basic_typename (index->ts.type));
4830 return false;
4833 if (index->ts.type == BT_REAL)
4834 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4835 &index->where))
4836 return false;
4838 if ((index->ts.kind != gfc_index_integer_kind
4839 && force_index_integer_kind)
4840 || index->ts.type != BT_INTEGER)
4842 gfc_clear_ts (&ts);
4843 ts.type = BT_INTEGER;
4844 ts.kind = gfc_index_integer_kind;
4846 gfc_convert_type_warn (index, &ts, 2, 0);
4849 return true;
4852 /* Resolve one part of an array index. */
4854 bool
4855 gfc_resolve_index (gfc_expr *index, int check_scalar)
4857 return gfc_resolve_index_1 (index, check_scalar, 1);
4860 /* Resolve a dim argument to an intrinsic function. */
4862 bool
4863 gfc_resolve_dim_arg (gfc_expr *dim)
4865 if (dim == NULL)
4866 return true;
4868 if (!gfc_resolve_expr (dim))
4869 return false;
4871 if (dim->rank != 0)
4873 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4874 return false;
4878 if (dim->ts.type != BT_INTEGER)
4880 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4881 return false;
4884 if (dim->ts.kind != gfc_index_integer_kind)
4886 gfc_typespec ts;
4888 gfc_clear_ts (&ts);
4889 ts.type = BT_INTEGER;
4890 ts.kind = gfc_index_integer_kind;
4892 gfc_convert_type_warn (dim, &ts, 2, 0);
4895 return true;
4898 /* Given an expression that contains array references, update those array
4899 references to point to the right array specifications. While this is
4900 filled in during matching, this information is difficult to save and load
4901 in a module, so we take care of it here.
4903 The idea here is that the original array reference comes from the
4904 base symbol. We traverse the list of reference structures, setting
4905 the stored reference to references. Component references can
4906 provide an additional array specification. */
4907 static void
4908 resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
4910 static void
4911 find_array_spec (gfc_expr *e)
4913 gfc_array_spec *as;
4914 gfc_component *c;
4915 gfc_ref *ref;
4916 bool class_as = false;
4918 if (e->symtree->n.sym->assoc)
4920 if (e->symtree->n.sym->assoc->target)
4921 gfc_resolve_expr (e->symtree->n.sym->assoc->target);
4922 resolve_assoc_var (e->symtree->n.sym, false);
4925 if (e->symtree->n.sym->ts.type == BT_CLASS)
4927 as = CLASS_DATA (e->symtree->n.sym)->as;
4928 class_as = true;
4930 else
4931 as = e->symtree->n.sym->as;
4933 for (ref = e->ref; ref; ref = ref->next)
4934 switch (ref->type)
4936 case REF_ARRAY:
4937 if (as == NULL)
4938 gfc_internal_error ("find_array_spec(): Missing spec");
4940 ref->u.ar.as = as;
4941 as = NULL;
4942 break;
4944 case REF_COMPONENT:
4945 c = ref->u.c.component;
4946 if (c->attr.dimension)
4948 if (as != NULL && !(class_as && as == c->as))
4949 gfc_internal_error ("find_array_spec(): unused as(1)");
4950 as = c->as;
4953 break;
4955 case REF_SUBSTRING:
4956 case REF_INQUIRY:
4957 break;
4960 if (as != NULL)
4961 gfc_internal_error ("find_array_spec(): unused as(2)");
4965 /* Resolve an array reference. */
4967 static bool
4968 resolve_array_ref (gfc_array_ref *ar)
4970 int i, check_scalar;
4971 gfc_expr *e;
4973 for (i = 0; i < ar->dimen + ar->codimen; i++)
4975 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4977 /* Do not force gfc_index_integer_kind for the start. We can
4978 do fine with any integer kind. This avoids temporary arrays
4979 created for indexing with a vector. */
4980 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4981 return false;
4982 if (!gfc_resolve_index (ar->end[i], check_scalar))
4983 return false;
4984 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4985 return false;
4987 e = ar->start[i];
4989 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4990 switch (e->rank)
4992 case 0:
4993 ar->dimen_type[i] = DIMEN_ELEMENT;
4994 break;
4996 case 1:
4997 ar->dimen_type[i] = DIMEN_VECTOR;
4998 if (e->expr_type == EXPR_VARIABLE
4999 && e->symtree->n.sym->ts.type == BT_DERIVED)
5000 ar->start[i] = gfc_get_parentheses (e);
5001 break;
5003 default:
5004 gfc_error ("Array index at %L is an array of rank %d",
5005 &ar->c_where[i], e->rank);
5006 return false;
5009 /* Fill in the upper bound, which may be lower than the
5010 specified one for something like a(2:10:5), which is
5011 identical to a(2:7:5). Only relevant for strides not equal
5012 to one. Don't try a division by zero. */
5013 if (ar->dimen_type[i] == DIMEN_RANGE
5014 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
5015 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
5016 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
5018 mpz_t size, end;
5020 if (gfc_ref_dimen_size (ar, i, &size, &end))
5022 if (ar->end[i] == NULL)
5024 ar->end[i] =
5025 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
5026 &ar->where);
5027 mpz_set (ar->end[i]->value.integer, end);
5029 else if (ar->end[i]->ts.type == BT_INTEGER
5030 && ar->end[i]->expr_type == EXPR_CONSTANT)
5032 mpz_set (ar->end[i]->value.integer, end);
5034 else
5035 gcc_unreachable ();
5037 mpz_clear (size);
5038 mpz_clear (end);
5043 if (ar->type == AR_FULL)
5045 if (ar->as->rank == 0)
5046 ar->type = AR_ELEMENT;
5048 /* Make sure array is the same as array(:,:), this way
5049 we don't need to special case all the time. */
5050 ar->dimen = ar->as->rank;
5051 for (i = 0; i < ar->dimen; i++)
5053 ar->dimen_type[i] = DIMEN_RANGE;
5055 gcc_assert (ar->start[i] == NULL);
5056 gcc_assert (ar->end[i] == NULL);
5057 gcc_assert (ar->stride[i] == NULL);
5061 /* If the reference type is unknown, figure out what kind it is. */
5063 if (ar->type == AR_UNKNOWN)
5065 ar->type = AR_ELEMENT;
5066 for (i = 0; i < ar->dimen; i++)
5067 if (ar->dimen_type[i] == DIMEN_RANGE
5068 || ar->dimen_type[i] == DIMEN_VECTOR)
5070 ar->type = AR_SECTION;
5071 break;
5075 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
5076 return false;
5078 if (ar->as->corank && ar->codimen == 0)
5080 int n;
5081 ar->codimen = ar->as->corank;
5082 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
5083 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
5086 return true;
5090 bool
5091 gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
5093 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
5095 if (ref->u.ss.start != NULL)
5097 if (!gfc_resolve_expr (ref->u.ss.start))
5098 return false;
5100 if (ref->u.ss.start->ts.type != BT_INTEGER)
5102 gfc_error ("Substring start index at %L must be of type INTEGER",
5103 &ref->u.ss.start->where);
5104 return false;
5107 if (ref->u.ss.start->rank != 0)
5109 gfc_error ("Substring start index at %L must be scalar",
5110 &ref->u.ss.start->where);
5111 return false;
5114 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
5115 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5116 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5118 gfc_error ("Substring start index at %L is less than one",
5119 &ref->u.ss.start->where);
5120 return false;
5124 if (ref->u.ss.end != NULL)
5126 if (!gfc_resolve_expr (ref->u.ss.end))
5127 return false;
5129 if (ref->u.ss.end->ts.type != BT_INTEGER)
5131 gfc_error ("Substring end index at %L must be of type INTEGER",
5132 &ref->u.ss.end->where);
5133 return false;
5136 if (ref->u.ss.end->rank != 0)
5138 gfc_error ("Substring end index at %L must be scalar",
5139 &ref->u.ss.end->where);
5140 return false;
5143 if (ref->u.ss.length != NULL
5144 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
5145 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5146 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5148 gfc_error ("Substring end index at %L exceeds the string length",
5149 &ref->u.ss.start->where);
5150 return false;
5153 if (compare_bound_mpz_t (ref->u.ss.end,
5154 gfc_integer_kinds[k].huge) == CMP_GT
5155 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5156 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5158 gfc_error ("Substring end index at %L is too large",
5159 &ref->u.ss.end->where);
5160 return false;
5162 /* If the substring has the same length as the original
5163 variable, the reference itself can be deleted. */
5165 if (ref->u.ss.length != NULL
5166 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
5167 && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
5168 *equal_length = true;
5171 return true;
5175 /* This function supplies missing substring charlens. */
5177 void
5178 gfc_resolve_substring_charlen (gfc_expr *e)
5180 gfc_ref *char_ref;
5181 gfc_expr *start, *end;
5182 gfc_typespec *ts = NULL;
5183 mpz_t diff;
5185 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
5187 if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
5188 break;
5189 if (char_ref->type == REF_COMPONENT)
5190 ts = &char_ref->u.c.component->ts;
5193 if (!char_ref || char_ref->type == REF_INQUIRY)
5194 return;
5196 gcc_assert (char_ref->next == NULL);
5198 if (e->ts.u.cl)
5200 if (e->ts.u.cl->length)
5201 gfc_free_expr (e->ts.u.cl->length);
5202 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
5203 return;
5206 if (!e->ts.u.cl)
5207 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5209 if (char_ref->u.ss.start)
5210 start = gfc_copy_expr (char_ref->u.ss.start);
5211 else
5212 start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
5214 if (char_ref->u.ss.end)
5215 end = gfc_copy_expr (char_ref->u.ss.end);
5216 else if (e->expr_type == EXPR_VARIABLE)
5218 if (!ts)
5219 ts = &e->symtree->n.sym->ts;
5220 end = gfc_copy_expr (ts->u.cl->length);
5222 else
5223 end = NULL;
5225 if (!start || !end)
5227 gfc_free_expr (start);
5228 gfc_free_expr (end);
5229 return;
5232 /* Length = (end - start + 1).
5233 Check first whether it has a constant length. */
5234 if (gfc_dep_difference (end, start, &diff))
5236 gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
5237 &e->where);
5239 mpz_add_ui (len->value.integer, diff, 1);
5240 mpz_clear (diff);
5241 e->ts.u.cl->length = len;
5242 /* The check for length < 0 is handled below */
5244 else
5246 e->ts.u.cl->length = gfc_subtract (end, start);
5247 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
5248 gfc_get_int_expr (gfc_charlen_int_kind,
5249 NULL, 1));
5252 /* F2008, 6.4.1: Both the starting point and the ending point shall
5253 be within the range 1, 2, ..., n unless the starting point exceeds
5254 the ending point, in which case the substring has length zero. */
5256 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
5257 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
5259 e->ts.u.cl->length->ts.type = BT_INTEGER;
5260 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5262 /* Make sure that the length is simplified. */
5263 gfc_simplify_expr (e->ts.u.cl->length, 1);
5264 gfc_resolve_expr (e->ts.u.cl->length);
5268 /* Resolve subtype references. */
5270 bool
5271 gfc_resolve_ref (gfc_expr *expr)
5273 int current_part_dimension, n_components, seen_part_dimension, dim;
5274 gfc_ref *ref, **prev, *array_ref;
5275 bool equal_length;
5277 for (ref = expr->ref; ref; ref = ref->next)
5278 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
5280 find_array_spec (expr);
5281 break;
5284 for (prev = &expr->ref; *prev != NULL;
5285 prev = *prev == NULL ? prev : &(*prev)->next)
5286 switch ((*prev)->type)
5288 case REF_ARRAY:
5289 if (!resolve_array_ref (&(*prev)->u.ar))
5290 return false;
5291 break;
5293 case REF_COMPONENT:
5294 case REF_INQUIRY:
5295 break;
5297 case REF_SUBSTRING:
5298 equal_length = false;
5299 if (!gfc_resolve_substring (*prev, &equal_length))
5300 return false;
5302 if (expr->expr_type != EXPR_SUBSTRING && equal_length)
5304 /* Remove the reference and move the charlen, if any. */
5305 ref = *prev;
5306 *prev = ref->next;
5307 ref->next = NULL;
5308 expr->ts.u.cl = ref->u.ss.length;
5309 ref->u.ss.length = NULL;
5310 gfc_free_ref_list (ref);
5312 break;
5315 /* Check constraints on part references. */
5317 current_part_dimension = 0;
5318 seen_part_dimension = 0;
5319 n_components = 0;
5320 array_ref = NULL;
5322 for (ref = expr->ref; ref; ref = ref->next)
5324 switch (ref->type)
5326 case REF_ARRAY:
5327 array_ref = ref;
5328 switch (ref->u.ar.type)
5330 case AR_FULL:
5331 /* Coarray scalar. */
5332 if (ref->u.ar.as->rank == 0)
5334 current_part_dimension = 0;
5335 break;
5337 /* Fall through. */
5338 case AR_SECTION:
5339 current_part_dimension = 1;
5340 break;
5342 case AR_ELEMENT:
5343 array_ref = NULL;
5344 current_part_dimension = 0;
5345 break;
5347 case AR_UNKNOWN:
5348 gfc_internal_error ("resolve_ref(): Bad array reference");
5351 break;
5353 case REF_COMPONENT:
5354 if (current_part_dimension || seen_part_dimension)
5356 /* F03:C614. */
5357 if (ref->u.c.component->attr.pointer
5358 || ref->u.c.component->attr.proc_pointer
5359 || (ref->u.c.component->ts.type == BT_CLASS
5360 && CLASS_DATA (ref->u.c.component)->attr.pointer))
5362 gfc_error ("Component to the right of a part reference "
5363 "with nonzero rank must not have the POINTER "
5364 "attribute at %L", &expr->where);
5365 return false;
5367 else if (ref->u.c.component->attr.allocatable
5368 || (ref->u.c.component->ts.type == BT_CLASS
5369 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
5372 gfc_error ("Component to the right of a part reference "
5373 "with nonzero rank must not have the ALLOCATABLE "
5374 "attribute at %L", &expr->where);
5375 return false;
5379 n_components++;
5380 break;
5382 case REF_SUBSTRING:
5383 break;
5385 case REF_INQUIRY:
5386 /* Implement requirement in note 9.7 of F2018 that the result of the
5387 LEN inquiry be a scalar. */
5388 if (ref->u.i == INQUIRY_LEN && array_ref && expr->ts.deferred)
5390 array_ref->u.ar.type = AR_ELEMENT;
5391 expr->rank = 0;
5392 /* INQUIRY_LEN is not evaluated from the rest of the expr
5393 but directly from the string length. This means that setting
5394 the array indices to one does not matter but might trigger
5395 a runtime bounds error. Suppress the check. */
5396 expr->no_bounds_check = 1;
5397 for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
5399 array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
5400 if (array_ref->u.ar.start[dim])
5401 gfc_free_expr (array_ref->u.ar.start[dim]);
5402 array_ref->u.ar.start[dim]
5403 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
5404 if (array_ref->u.ar.end[dim])
5405 gfc_free_expr (array_ref->u.ar.end[dim]);
5406 if (array_ref->u.ar.stride[dim])
5407 gfc_free_expr (array_ref->u.ar.stride[dim]);
5410 break;
5413 if (((ref->type == REF_COMPONENT && n_components > 1)
5414 || ref->next == NULL)
5415 && current_part_dimension
5416 && seen_part_dimension)
5418 gfc_error ("Two or more part references with nonzero rank must "
5419 "not be specified at %L", &expr->where);
5420 return false;
5423 if (ref->type == REF_COMPONENT)
5425 if (current_part_dimension)
5426 seen_part_dimension = 1;
5428 /* reset to make sure */
5429 current_part_dimension = 0;
5433 return true;
5437 /* Given an expression, determine its shape. This is easier than it sounds.
5438 Leaves the shape array NULL if it is not possible to determine the shape. */
5440 static void
5441 expression_shape (gfc_expr *e)
5443 mpz_t array[GFC_MAX_DIMENSIONS];
5444 int i;
5446 if (e->rank <= 0 || e->shape != NULL)
5447 return;
5449 for (i = 0; i < e->rank; i++)
5450 if (!gfc_array_dimen_size (e, i, &array[i]))
5451 goto fail;
5453 e->shape = gfc_get_shape (e->rank);
5455 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
5457 return;
5459 fail:
5460 for (i--; i >= 0; i--)
5461 mpz_clear (array[i]);
5465 /* Given a variable expression node, compute the rank of the expression by
5466 examining the base symbol and any reference structures it may have. */
5468 void
5469 gfc_expression_rank (gfc_expr *e)
5471 gfc_ref *ref;
5472 int i, rank;
5474 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
5475 could lead to serious confusion... */
5476 gcc_assert (e->expr_type != EXPR_COMPCALL);
5478 if (e->ref == NULL)
5480 if (e->expr_type == EXPR_ARRAY)
5481 goto done;
5482 /* Constructors can have a rank different from one via RESHAPE(). */
5484 e->rank = ((e->symtree == NULL || e->symtree->n.sym->as == NULL)
5485 ? 0 : e->symtree->n.sym->as->rank);
5486 goto done;
5489 rank = 0;
5491 for (ref = e->ref; ref; ref = ref->next)
5493 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
5494 && ref->u.c.component->attr.function && !ref->next)
5495 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
5497 if (ref->type != REF_ARRAY)
5498 continue;
5500 if (ref->u.ar.type == AR_FULL)
5502 rank = ref->u.ar.as->rank;
5503 break;
5506 if (ref->u.ar.type == AR_SECTION)
5508 /* Figure out the rank of the section. */
5509 if (rank != 0)
5510 gfc_internal_error ("gfc_expression_rank(): Two array specs");
5512 for (i = 0; i < ref->u.ar.dimen; i++)
5513 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5514 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
5515 rank++;
5517 break;
5521 e->rank = rank;
5523 done:
5524 expression_shape (e);
5528 static void
5529 add_caf_get_intrinsic (gfc_expr *e)
5531 gfc_expr *wrapper, *tmp_expr;
5532 gfc_ref *ref;
5533 int n;
5535 for (ref = e->ref; ref; ref = ref->next)
5536 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5537 break;
5538 if (ref == NULL)
5539 return;
5541 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
5542 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
5543 return;
5545 tmp_expr = XCNEW (gfc_expr);
5546 *tmp_expr = *e;
5547 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
5548 "caf_get", tmp_expr->where, 1, tmp_expr);
5549 wrapper->ts = e->ts;
5550 wrapper->rank = e->rank;
5551 if (e->rank)
5552 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
5553 *e = *wrapper;
5554 free (wrapper);
5558 static void
5559 remove_caf_get_intrinsic (gfc_expr *e)
5561 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
5562 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
5563 gfc_expr *e2 = e->value.function.actual->expr;
5564 e->value.function.actual->expr = NULL;
5565 gfc_free_actual_arglist (e->value.function.actual);
5566 gfc_free_shape (&e->shape, e->rank);
5567 *e = *e2;
5568 free (e2);
5572 /* Resolve a variable expression. */
5574 static bool
5575 resolve_variable (gfc_expr *e)
5577 gfc_symbol *sym;
5578 bool t;
5580 t = true;
5582 if (e->symtree == NULL)
5583 return false;
5584 sym = e->symtree->n.sym;
5586 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
5587 as ts.type is set to BT_ASSUMED in resolve_symbol. */
5588 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
5590 if (!actual_arg || inquiry_argument)
5592 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
5593 "be used as actual argument", sym->name, &e->where);
5594 return false;
5597 /* TS 29113, 407b. */
5598 else if (e->ts.type == BT_ASSUMED)
5600 if (!actual_arg)
5602 gfc_error ("Assumed-type variable %s at %L may only be used "
5603 "as actual argument", sym->name, &e->where);
5604 return false;
5606 else if (inquiry_argument && !first_actual_arg)
5608 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5609 for all inquiry functions in resolve_function; the reason is
5610 that the function-name resolution happens too late in that
5611 function. */
5612 gfc_error ("Assumed-type variable %s at %L as actual argument to "
5613 "an inquiry function shall be the first argument",
5614 sym->name, &e->where);
5615 return false;
5618 /* TS 29113, C535b. */
5619 else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5620 && sym->ts.u.derived && CLASS_DATA (sym)
5621 && CLASS_DATA (sym)->as
5622 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5623 || (sym->ts.type != BT_CLASS && sym->as
5624 && sym->as->type == AS_ASSUMED_RANK))
5625 && !sym->attr.select_rank_temporary)
5627 if (!actual_arg
5628 && !(cs_base && cs_base->current
5629 && cs_base->current->op == EXEC_SELECT_RANK))
5631 gfc_error ("Assumed-rank variable %s at %L may only be used as "
5632 "actual argument", sym->name, &e->where);
5633 return false;
5635 else if (inquiry_argument && !first_actual_arg)
5637 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5638 for all inquiry functions in resolve_function; the reason is
5639 that the function-name resolution happens too late in that
5640 function. */
5641 gfc_error ("Assumed-rank variable %s at %L as actual argument "
5642 "to an inquiry function shall be the first argument",
5643 sym->name, &e->where);
5644 return false;
5648 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
5649 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5650 && e->ref->next == NULL))
5652 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
5653 "a subobject reference", sym->name, &e->ref->u.ar.where);
5654 return false;
5656 /* TS 29113, 407b. */
5657 else if (e->ts.type == BT_ASSUMED && e->ref
5658 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5659 && e->ref->next == NULL))
5661 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
5662 "reference", sym->name, &e->ref->u.ar.where);
5663 return false;
5666 /* TS 29113, C535b. */
5667 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5668 && sym->ts.u.derived && CLASS_DATA (sym)
5669 && CLASS_DATA (sym)->as
5670 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5671 || (sym->ts.type != BT_CLASS && sym->as
5672 && sym->as->type == AS_ASSUMED_RANK))
5673 && e->ref
5674 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5675 && e->ref->next == NULL))
5677 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
5678 "reference", sym->name, &e->ref->u.ar.where);
5679 return false;
5682 /* For variables that are used in an associate (target => object) where
5683 the object's basetype is array valued while the target is scalar,
5684 the ts' type of the component refs is still array valued, which
5685 can't be translated that way. */
5686 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
5687 && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
5688 && CLASS_DATA (sym->assoc->target)->as)
5690 gfc_ref *ref = e->ref;
5691 while (ref)
5693 switch (ref->type)
5695 case REF_COMPONENT:
5696 ref->u.c.sym = sym->ts.u.derived;
5697 /* Stop the loop. */
5698 ref = NULL;
5699 break;
5700 default:
5701 ref = ref->next;
5702 break;
5707 /* If this is an associate-name, it may be parsed with an array reference
5708 in error even though the target is scalar. Fail directly in this case.
5709 TODO Understand why class scalar expressions must be excluded. */
5710 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5712 if (sym->ts.type == BT_CLASS)
5713 gfc_fix_class_refs (e);
5714 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5715 return false;
5716 else if (sym->attr.dimension && (!e->ref || e->ref->type != REF_ARRAY))
5718 /* This can happen because the parser did not detect that the
5719 associate name is an array and the expression had no array
5720 part_ref. */
5721 gfc_ref *ref = gfc_get_ref ();
5722 ref->type = REF_ARRAY;
5723 ref->u.ar.type = AR_FULL;
5724 if (sym->as)
5726 ref->u.ar.as = sym->as;
5727 ref->u.ar.dimen = sym->as->rank;
5729 ref->next = e->ref;
5730 e->ref = ref;
5735 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5736 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5738 /* On the other hand, the parser may not have known this is an array;
5739 in this case, we have to add a FULL reference. */
5740 if (sym->assoc && sym->attr.dimension && !e->ref)
5742 e->ref = gfc_get_ref ();
5743 e->ref->type = REF_ARRAY;
5744 e->ref->u.ar.type = AR_FULL;
5745 e->ref->u.ar.dimen = 0;
5748 /* Like above, but for class types, where the checking whether an array
5749 ref is present is more complicated. Furthermore make sure not to add
5750 the full array ref to _vptr or _len refs. */
5751 if (sym->assoc && sym->ts.type == BT_CLASS
5752 && CLASS_DATA (sym)->attr.dimension
5753 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
5755 gfc_ref *ref, *newref;
5757 newref = gfc_get_ref ();
5758 newref->type = REF_ARRAY;
5759 newref->u.ar.type = AR_FULL;
5760 newref->u.ar.dimen = 0;
5761 /* Because this is an associate var and the first ref either is a ref to
5762 the _data component or not, no traversal of the ref chain is
5763 needed. The array ref needs to be inserted after the _data ref,
5764 or when that is not present, which may happend for polymorphic
5765 types, then at the first position. */
5766 ref = e->ref;
5767 if (!ref)
5768 e->ref = newref;
5769 else if (ref->type == REF_COMPONENT
5770 && strcmp ("_data", ref->u.c.component->name) == 0)
5772 if (!ref->next || ref->next->type != REF_ARRAY)
5774 newref->next = ref->next;
5775 ref->next = newref;
5777 else
5778 /* Array ref present already. */
5779 gfc_free_ref_list (newref);
5781 else if (ref->type == REF_ARRAY)
5782 /* Array ref present already. */
5783 gfc_free_ref_list (newref);
5784 else
5786 newref->next = ref;
5787 e->ref = newref;
5791 if (e->ref && !gfc_resolve_ref (e))
5792 return false;
5794 if (sym->attr.flavor == FL_PROCEDURE
5795 && (!sym->attr.function
5796 || (sym->attr.function && sym->result
5797 && sym->result->attr.proc_pointer
5798 && !sym->result->attr.function)))
5800 e->ts.type = BT_PROCEDURE;
5801 goto resolve_procedure;
5804 if (sym->ts.type != BT_UNKNOWN)
5805 gfc_variable_attr (e, &e->ts);
5806 else if (sym->attr.flavor == FL_PROCEDURE
5807 && sym->attr.function && sym->result
5808 && sym->result->ts.type != BT_UNKNOWN
5809 && sym->result->attr.proc_pointer)
5810 e->ts = sym->result->ts;
5811 else
5813 /* Must be a simple variable reference. */
5814 if (!gfc_set_default_type (sym, 1, sym->ns))
5815 return false;
5816 e->ts = sym->ts;
5819 if (check_assumed_size_reference (sym, e))
5820 return false;
5822 /* Deal with forward references to entries during gfc_resolve_code, to
5823 satisfy, at least partially, 12.5.2.5. */
5824 if (gfc_current_ns->entries
5825 && current_entry_id == sym->entry_id
5826 && cs_base
5827 && cs_base->current
5828 && cs_base->current->op != EXEC_ENTRY)
5830 gfc_entry_list *entry;
5831 gfc_formal_arglist *formal;
5832 int n;
5833 bool seen, saved_specification_expr;
5835 /* If the symbol is a dummy... */
5836 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5838 entry = gfc_current_ns->entries;
5839 seen = false;
5841 /* ...test if the symbol is a parameter of previous entries. */
5842 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5843 for (formal = entry->sym->formal; formal; formal = formal->next)
5845 if (formal->sym && sym->name == formal->sym->name)
5847 seen = true;
5848 break;
5852 /* If it has not been seen as a dummy, this is an error. */
5853 if (!seen)
5855 if (specification_expr)
5856 gfc_error ("Variable %qs, used in a specification expression"
5857 ", is referenced at %L before the ENTRY statement "
5858 "in which it is a parameter",
5859 sym->name, &cs_base->current->loc);
5860 else
5861 gfc_error ("Variable %qs is used at %L before the ENTRY "
5862 "statement in which it is a parameter",
5863 sym->name, &cs_base->current->loc);
5864 t = false;
5868 /* Now do the same check on the specification expressions. */
5869 saved_specification_expr = specification_expr;
5870 specification_expr = true;
5871 if (sym->ts.type == BT_CHARACTER
5872 && !gfc_resolve_expr (sym->ts.u.cl->length))
5873 t = false;
5875 if (sym->as)
5876 for (n = 0; n < sym->as->rank; n++)
5878 if (!gfc_resolve_expr (sym->as->lower[n]))
5879 t = false;
5880 if (!gfc_resolve_expr (sym->as->upper[n]))
5881 t = false;
5883 specification_expr = saved_specification_expr;
5885 if (t)
5886 /* Update the symbol's entry level. */
5887 sym->entry_id = current_entry_id + 1;
5890 /* If a symbol has been host_associated mark it. This is used latter,
5891 to identify if aliasing is possible via host association. */
5892 if (sym->attr.flavor == FL_VARIABLE
5893 && gfc_current_ns->parent
5894 && (gfc_current_ns->parent == sym->ns
5895 || (gfc_current_ns->parent->parent
5896 && gfc_current_ns->parent->parent == sym->ns)))
5897 sym->attr.host_assoc = 1;
5899 if (gfc_current_ns->proc_name
5900 && sym->attr.dimension
5901 && (sym->ns != gfc_current_ns
5902 || sym->attr.use_assoc
5903 || sym->attr.in_common))
5904 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
5906 resolve_procedure:
5907 if (t && !resolve_procedure_expression (e))
5908 t = false;
5910 /* F2008, C617 and C1229. */
5911 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5912 && gfc_is_coindexed (e))
5914 gfc_ref *ref, *ref2 = NULL;
5916 for (ref = e->ref; ref; ref = ref->next)
5918 if (ref->type == REF_COMPONENT)
5919 ref2 = ref;
5920 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5921 break;
5924 for ( ; ref; ref = ref->next)
5925 if (ref->type == REF_COMPONENT)
5926 break;
5928 /* Expression itself is not coindexed object. */
5929 if (ref && e->ts.type == BT_CLASS)
5931 gfc_error ("Polymorphic subobject of coindexed object at %L",
5932 &e->where);
5933 t = false;
5936 /* Expression itself is coindexed object. */
5937 if (ref == NULL)
5939 gfc_component *c;
5940 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5941 for ( ; c; c = c->next)
5942 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5944 gfc_error ("Coindexed object with polymorphic allocatable "
5945 "subcomponent at %L", &e->where);
5946 t = false;
5947 break;
5952 if (t)
5953 gfc_expression_rank (e);
5955 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
5956 add_caf_get_intrinsic (e);
5958 if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
5959 gfc_warning (OPT_Wdeprecated_declarations,
5960 "Using variable %qs at %L is deprecated",
5961 sym->name, &e->where);
5962 /* Simplify cases where access to a parameter array results in a
5963 single constant. Suppress errors since those will have been
5964 issued before, as warnings. */
5965 if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
5967 gfc_push_suppress_errors ();
5968 gfc_simplify_expr (e, 1);
5969 gfc_pop_suppress_errors ();
5972 return t;
5976 /* Checks to see that the correct symbol has been host associated.
5977 The only situation where this arises is that in which a twice
5978 contained function is parsed after the host association is made.
5979 Therefore, on detecting this, change the symbol in the expression
5980 and convert the array reference into an actual arglist if the old
5981 symbol is a variable. */
5982 static bool
5983 check_host_association (gfc_expr *e)
5985 gfc_symbol *sym, *old_sym;
5986 gfc_symtree *st;
5987 int n;
5988 gfc_ref *ref;
5989 gfc_actual_arglist *arg, *tail = NULL;
5990 bool retval = e->expr_type == EXPR_FUNCTION;
5992 /* If the expression is the result of substitution in
5993 interface.c(gfc_extend_expr) because there is no way in
5994 which the host association can be wrong. */
5995 if (e->symtree == NULL
5996 || e->symtree->n.sym == NULL
5997 || e->user_operator)
5998 return retval;
6000 old_sym = e->symtree->n.sym;
6002 if (gfc_current_ns->parent
6003 && old_sym->ns != gfc_current_ns)
6005 /* Use the 'USE' name so that renamed module symbols are
6006 correctly handled. */
6007 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
6009 if (sym && old_sym != sym
6010 && sym->ts.type == old_sym->ts.type
6011 && sym->attr.flavor == FL_PROCEDURE
6012 && sym->attr.contained)
6014 /* Clear the shape, since it might not be valid. */
6015 gfc_free_shape (&e->shape, e->rank);
6017 /* Give the expression the right symtree! */
6018 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
6019 gcc_assert (st != NULL);
6021 if (old_sym->attr.flavor == FL_PROCEDURE
6022 || e->expr_type == EXPR_FUNCTION)
6024 /* Original was function so point to the new symbol, since
6025 the actual argument list is already attached to the
6026 expression. */
6027 e->value.function.esym = NULL;
6028 e->symtree = st;
6030 else
6032 /* Original was variable so convert array references into
6033 an actual arglist. This does not need any checking now
6034 since resolve_function will take care of it. */
6035 e->value.function.actual = NULL;
6036 e->expr_type = EXPR_FUNCTION;
6037 e->symtree = st;
6039 /* Ambiguity will not arise if the array reference is not
6040 the last reference. */
6041 for (ref = e->ref; ref; ref = ref->next)
6042 if (ref->type == REF_ARRAY && ref->next == NULL)
6043 break;
6045 if ((ref == NULL || ref->type != REF_ARRAY)
6046 && sym->attr.proc == PROC_INTERNAL)
6048 gfc_error ("%qs at %L is host associated at %L into "
6049 "a contained procedure with an internal "
6050 "procedure of the same name", sym->name,
6051 &old_sym->declared_at, &e->where);
6052 return false;
6055 gcc_assert (ref->type == REF_ARRAY);
6057 /* Grab the start expressions from the array ref and
6058 copy them into actual arguments. */
6059 for (n = 0; n < ref->u.ar.dimen; n++)
6061 arg = gfc_get_actual_arglist ();
6062 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
6063 if (e->value.function.actual == NULL)
6064 tail = e->value.function.actual = arg;
6065 else
6067 tail->next = arg;
6068 tail = arg;
6072 /* Dump the reference list and set the rank. */
6073 gfc_free_ref_list (e->ref);
6074 e->ref = NULL;
6075 e->rank = sym->as ? sym->as->rank : 0;
6078 gfc_resolve_expr (e);
6079 sym->refs++;
6082 /* This might have changed! */
6083 return e->expr_type == EXPR_FUNCTION;
6087 static void
6088 gfc_resolve_character_operator (gfc_expr *e)
6090 gfc_expr *op1 = e->value.op.op1;
6091 gfc_expr *op2 = e->value.op.op2;
6092 gfc_expr *e1 = NULL;
6093 gfc_expr *e2 = NULL;
6095 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
6097 if (op1->ts.u.cl && op1->ts.u.cl->length)
6098 e1 = gfc_copy_expr (op1->ts.u.cl->length);
6099 else if (op1->expr_type == EXPR_CONSTANT)
6100 e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6101 op1->value.character.length);
6103 if (op2->ts.u.cl && op2->ts.u.cl->length)
6104 e2 = gfc_copy_expr (op2->ts.u.cl->length);
6105 else if (op2->expr_type == EXPR_CONSTANT)
6106 e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6107 op2->value.character.length);
6109 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6111 if (!e1 || !e2)
6113 gfc_free_expr (e1);
6114 gfc_free_expr (e2);
6116 return;
6119 e->ts.u.cl->length = gfc_add (e1, e2);
6120 e->ts.u.cl->length->ts.type = BT_INTEGER;
6121 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
6122 gfc_simplify_expr (e->ts.u.cl->length, 0);
6123 gfc_resolve_expr (e->ts.u.cl->length);
6125 return;
6129 /* Ensure that an character expression has a charlen and, if possible, a
6130 length expression. */
6132 static void
6133 fixup_charlen (gfc_expr *e)
6135 /* The cases fall through so that changes in expression type and the need
6136 for multiple fixes are picked up. In all circumstances, a charlen should
6137 be available for the middle end to hang a backend_decl on. */
6138 switch (e->expr_type)
6140 case EXPR_OP:
6141 gfc_resolve_character_operator (e);
6142 /* FALLTHRU */
6144 case EXPR_ARRAY:
6145 if (e->expr_type == EXPR_ARRAY)
6146 gfc_resolve_character_array_constructor (e);
6147 /* FALLTHRU */
6149 case EXPR_SUBSTRING:
6150 if (!e->ts.u.cl && e->ref)
6151 gfc_resolve_substring_charlen (e);
6152 /* FALLTHRU */
6154 default:
6155 if (!e->ts.u.cl)
6156 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6158 break;
6163 /* Update an actual argument to include the passed-object for type-bound
6164 procedures at the right position. */
6166 static gfc_actual_arglist*
6167 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
6168 const char *name)
6170 gcc_assert (argpos > 0);
6172 if (argpos == 1)
6174 gfc_actual_arglist* result;
6176 result = gfc_get_actual_arglist ();
6177 result->expr = po;
6178 result->next = lst;
6179 if (name)
6180 result->name = name;
6182 return result;
6185 if (lst)
6186 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
6187 else
6188 lst = update_arglist_pass (NULL, po, argpos - 1, name);
6189 return lst;
6193 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
6195 static gfc_expr*
6196 extract_compcall_passed_object (gfc_expr* e)
6198 gfc_expr* po;
6200 if (e->expr_type == EXPR_UNKNOWN)
6202 gfc_error ("Error in typebound call at %L",
6203 &e->where);
6204 return NULL;
6207 gcc_assert (e->expr_type == EXPR_COMPCALL);
6209 if (e->value.compcall.base_object)
6210 po = gfc_copy_expr (e->value.compcall.base_object);
6211 else
6213 po = gfc_get_expr ();
6214 po->expr_type = EXPR_VARIABLE;
6215 po->symtree = e->symtree;
6216 po->ref = gfc_copy_ref (e->ref);
6217 po->where = e->where;
6220 if (!gfc_resolve_expr (po))
6221 return NULL;
6223 return po;
6227 /* Update the arglist of an EXPR_COMPCALL expression to include the
6228 passed-object. */
6230 static bool
6231 update_compcall_arglist (gfc_expr* e)
6233 gfc_expr* po;
6234 gfc_typebound_proc* tbp;
6236 tbp = e->value.compcall.tbp;
6238 if (tbp->error)
6239 return false;
6241 po = extract_compcall_passed_object (e);
6242 if (!po)
6243 return false;
6245 if (tbp->nopass || e->value.compcall.ignore_pass)
6247 gfc_free_expr (po);
6248 return true;
6251 if (tbp->pass_arg_num <= 0)
6252 return false;
6254 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6255 tbp->pass_arg_num,
6256 tbp->pass_arg);
6258 return true;
6262 /* Extract the passed object from a PPC call (a copy of it). */
6264 static gfc_expr*
6265 extract_ppc_passed_object (gfc_expr *e)
6267 gfc_expr *po;
6268 gfc_ref **ref;
6270 po = gfc_get_expr ();
6271 po->expr_type = EXPR_VARIABLE;
6272 po->symtree = e->symtree;
6273 po->ref = gfc_copy_ref (e->ref);
6274 po->where = e->where;
6276 /* Remove PPC reference. */
6277 ref = &po->ref;
6278 while ((*ref)->next)
6279 ref = &(*ref)->next;
6280 gfc_free_ref_list (*ref);
6281 *ref = NULL;
6283 if (!gfc_resolve_expr (po))
6284 return NULL;
6286 return po;
6290 /* Update the actual arglist of a procedure pointer component to include the
6291 passed-object. */
6293 static bool
6294 update_ppc_arglist (gfc_expr* e)
6296 gfc_expr* po;
6297 gfc_component *ppc;
6298 gfc_typebound_proc* tb;
6300 ppc = gfc_get_proc_ptr_comp (e);
6301 if (!ppc)
6302 return false;
6304 tb = ppc->tb;
6306 if (tb->error)
6307 return false;
6308 else if (tb->nopass)
6309 return true;
6311 po = extract_ppc_passed_object (e);
6312 if (!po)
6313 return false;
6315 /* F08:R739. */
6316 if (po->rank != 0)
6318 gfc_error ("Passed-object at %L must be scalar", &e->where);
6319 return false;
6322 /* F08:C611. */
6323 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
6325 gfc_error ("Base object for procedure-pointer component call at %L is of"
6326 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
6327 return false;
6330 gcc_assert (tb->pass_arg_num > 0);
6331 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6332 tb->pass_arg_num,
6333 tb->pass_arg);
6335 return true;
6339 /* Check that the object a TBP is called on is valid, i.e. it must not be
6340 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
6342 static bool
6343 check_typebound_baseobject (gfc_expr* e)
6345 gfc_expr* base;
6346 bool return_value = false;
6348 base = extract_compcall_passed_object (e);
6349 if (!base)
6350 return false;
6352 if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
6354 gfc_error ("Error in typebound call at %L", &e->where);
6355 goto cleanup;
6358 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
6359 return false;
6361 /* F08:C611. */
6362 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
6364 gfc_error ("Base object for type-bound procedure call at %L is of"
6365 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
6366 goto cleanup;
6369 /* F08:C1230. If the procedure called is NOPASS,
6370 the base object must be scalar. */
6371 if (e->value.compcall.tbp->nopass && base->rank != 0)
6373 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
6374 " be scalar", &e->where);
6375 goto cleanup;
6378 return_value = true;
6380 cleanup:
6381 gfc_free_expr (base);
6382 return return_value;
6386 /* Resolve a call to a type-bound procedure, either function or subroutine,
6387 statically from the data in an EXPR_COMPCALL expression. The adapted
6388 arglist and the target-procedure symtree are returned. */
6390 static bool
6391 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
6392 gfc_actual_arglist** actual)
6394 gcc_assert (e->expr_type == EXPR_COMPCALL);
6395 gcc_assert (!e->value.compcall.tbp->is_generic);
6397 /* Update the actual arglist for PASS. */
6398 if (!update_compcall_arglist (e))
6399 return false;
6401 *actual = e->value.compcall.actual;
6402 *target = e->value.compcall.tbp->u.specific;
6404 gfc_free_ref_list (e->ref);
6405 e->ref = NULL;
6406 e->value.compcall.actual = NULL;
6408 /* If we find a deferred typebound procedure, check for derived types
6409 that an overriding typebound procedure has not been missed. */
6410 if (e->value.compcall.name
6411 && !e->value.compcall.tbp->non_overridable
6412 && e->value.compcall.base_object
6413 && e->value.compcall.base_object->ts.type == BT_DERIVED)
6415 gfc_symtree *st;
6416 gfc_symbol *derived;
6418 /* Use the derived type of the base_object. */
6419 derived = e->value.compcall.base_object->ts.u.derived;
6420 st = NULL;
6422 /* If necessary, go through the inheritance chain. */
6423 while (!st && derived)
6425 /* Look for the typebound procedure 'name'. */
6426 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
6427 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
6428 e->value.compcall.name);
6429 if (!st)
6430 derived = gfc_get_derived_super_type (derived);
6433 /* Now find the specific name in the derived type namespace. */
6434 if (st && st->n.tb && st->n.tb->u.specific)
6435 gfc_find_sym_tree (st->n.tb->u.specific->name,
6436 derived->ns, 1, &st);
6437 if (st)
6438 *target = st;
6440 return true;
6444 /* Get the ultimate declared type from an expression. In addition,
6445 return the last class/derived type reference and the copy of the
6446 reference list. If check_types is set true, derived types are
6447 identified as well as class references. */
6448 static gfc_symbol*
6449 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
6450 gfc_expr *e, bool check_types)
6452 gfc_symbol *declared;
6453 gfc_ref *ref;
6455 declared = NULL;
6456 if (class_ref)
6457 *class_ref = NULL;
6458 if (new_ref)
6459 *new_ref = gfc_copy_ref (e->ref);
6461 for (ref = e->ref; ref; ref = ref->next)
6463 if (ref->type != REF_COMPONENT)
6464 continue;
6466 if ((ref->u.c.component->ts.type == BT_CLASS
6467 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
6468 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
6470 declared = ref->u.c.component->ts.u.derived;
6471 if (class_ref)
6472 *class_ref = ref;
6476 if (declared == NULL)
6477 declared = e->symtree->n.sym->ts.u.derived;
6479 return declared;
6483 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
6484 which of the specific bindings (if any) matches the arglist and transform
6485 the expression into a call of that binding. */
6487 static bool
6488 resolve_typebound_generic_call (gfc_expr* e, const char **name)
6490 gfc_typebound_proc* genproc;
6491 const char* genname;
6492 gfc_symtree *st;
6493 gfc_symbol *derived;
6495 gcc_assert (e->expr_type == EXPR_COMPCALL);
6496 genname = e->value.compcall.name;
6497 genproc = e->value.compcall.tbp;
6499 if (!genproc->is_generic)
6500 return true;
6502 /* Try the bindings on this type and in the inheritance hierarchy. */
6503 for (; genproc; genproc = genproc->overridden)
6505 gfc_tbp_generic* g;
6507 gcc_assert (genproc->is_generic);
6508 for (g = genproc->u.generic; g; g = g->next)
6510 gfc_symbol* target;
6511 gfc_actual_arglist* args;
6512 bool matches;
6514 gcc_assert (g->specific);
6516 if (g->specific->error)
6517 continue;
6519 target = g->specific->u.specific->n.sym;
6521 /* Get the right arglist by handling PASS/NOPASS. */
6522 args = gfc_copy_actual_arglist (e->value.compcall.actual);
6523 if (!g->specific->nopass)
6525 gfc_expr* po;
6526 po = extract_compcall_passed_object (e);
6527 if (!po)
6529 gfc_free_actual_arglist (args);
6530 return false;
6533 gcc_assert (g->specific->pass_arg_num > 0);
6534 gcc_assert (!g->specific->error);
6535 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
6536 g->specific->pass_arg);
6538 resolve_actual_arglist (args, target->attr.proc,
6539 is_external_proc (target)
6540 && gfc_sym_get_dummy_args (target) == NULL);
6542 /* Check if this arglist matches the formal. */
6543 matches = gfc_arglist_matches_symbol (&args, target);
6545 /* Clean up and break out of the loop if we've found it. */
6546 gfc_free_actual_arglist (args);
6547 if (matches)
6549 e->value.compcall.tbp = g->specific;
6550 genname = g->specific_st->name;
6551 /* Pass along the name for CLASS methods, where the vtab
6552 procedure pointer component has to be referenced. */
6553 if (name)
6554 *name = genname;
6555 goto success;
6560 /* Nothing matching found! */
6561 gfc_error ("Found no matching specific binding for the call to the GENERIC"
6562 " %qs at %L", genname, &e->where);
6563 return false;
6565 success:
6566 /* Make sure that we have the right specific instance for the name. */
6567 derived = get_declared_from_expr (NULL, NULL, e, true);
6569 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
6570 if (st)
6571 e->value.compcall.tbp = st->n.tb;
6573 return true;
6577 /* Resolve a call to a type-bound subroutine. */
6579 static bool
6580 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
6582 gfc_actual_arglist* newactual;
6583 gfc_symtree* target;
6585 /* Check that's really a SUBROUTINE. */
6586 if (!c->expr1->value.compcall.tbp->subroutine)
6588 if (!c->expr1->value.compcall.tbp->is_generic
6589 && c->expr1->value.compcall.tbp->u.specific
6590 && c->expr1->value.compcall.tbp->u.specific->n.sym
6591 && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
6592 c->expr1->value.compcall.tbp->subroutine = 1;
6593 else
6595 gfc_error ("%qs at %L should be a SUBROUTINE",
6596 c->expr1->value.compcall.name, &c->loc);
6597 return false;
6601 if (!check_typebound_baseobject (c->expr1))
6602 return false;
6604 /* Pass along the name for CLASS methods, where the vtab
6605 procedure pointer component has to be referenced. */
6606 if (name)
6607 *name = c->expr1->value.compcall.name;
6609 if (!resolve_typebound_generic_call (c->expr1, name))
6610 return false;
6612 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
6613 if (overridable)
6614 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
6616 /* Transform into an ordinary EXEC_CALL for now. */
6618 if (!resolve_typebound_static (c->expr1, &target, &newactual))
6619 return false;
6621 c->ext.actual = newactual;
6622 c->symtree = target;
6623 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
6625 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
6627 gfc_free_expr (c->expr1);
6628 c->expr1 = gfc_get_expr ();
6629 c->expr1->expr_type = EXPR_FUNCTION;
6630 c->expr1->symtree = target;
6631 c->expr1->where = c->loc;
6633 return resolve_call (c);
6637 /* Resolve a component-call expression. */
6638 static bool
6639 resolve_compcall (gfc_expr* e, const char **name)
6641 gfc_actual_arglist* newactual;
6642 gfc_symtree* target;
6644 /* Check that's really a FUNCTION. */
6645 if (!e->value.compcall.tbp->function)
6647 gfc_error ("%qs at %L should be a FUNCTION",
6648 e->value.compcall.name, &e->where);
6649 return false;
6653 /* These must not be assign-calls! */
6654 gcc_assert (!e->value.compcall.assign);
6656 if (!check_typebound_baseobject (e))
6657 return false;
6659 /* Pass along the name for CLASS methods, where the vtab
6660 procedure pointer component has to be referenced. */
6661 if (name)
6662 *name = e->value.compcall.name;
6664 if (!resolve_typebound_generic_call (e, name))
6665 return false;
6666 gcc_assert (!e->value.compcall.tbp->is_generic);
6668 /* Take the rank from the function's symbol. */
6669 if (e->value.compcall.tbp->u.specific->n.sym->as)
6670 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
6672 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
6673 arglist to the TBP's binding target. */
6675 if (!resolve_typebound_static (e, &target, &newactual))
6676 return false;
6678 e->value.function.actual = newactual;
6679 e->value.function.name = NULL;
6680 e->value.function.esym = target->n.sym;
6681 e->value.function.isym = NULL;
6682 e->symtree = target;
6683 e->ts = target->n.sym->ts;
6684 e->expr_type = EXPR_FUNCTION;
6686 /* Resolution is not necessary if this is a class subroutine; this
6687 function only has to identify the specific proc. Resolution of
6688 the call will be done next in resolve_typebound_call. */
6689 return gfc_resolve_expr (e);
6693 static bool resolve_fl_derived (gfc_symbol *sym);
6696 /* Resolve a typebound function, or 'method'. First separate all
6697 the non-CLASS references by calling resolve_compcall directly. */
6699 static bool
6700 resolve_typebound_function (gfc_expr* e)
6702 gfc_symbol *declared;
6703 gfc_component *c;
6704 gfc_ref *new_ref;
6705 gfc_ref *class_ref;
6706 gfc_symtree *st;
6707 const char *name;
6708 gfc_typespec ts;
6709 gfc_expr *expr;
6710 bool overridable;
6712 st = e->symtree;
6714 /* Deal with typebound operators for CLASS objects. */
6715 expr = e->value.compcall.base_object;
6716 overridable = !e->value.compcall.tbp->non_overridable;
6717 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
6719 /* Since the typebound operators are generic, we have to ensure
6720 that any delays in resolution are corrected and that the vtab
6721 is present. */
6722 ts = expr->ts;
6723 declared = ts.u.derived;
6724 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6725 if (c->ts.u.derived == NULL)
6726 c->ts.u.derived = gfc_find_derived_vtab (declared);
6728 if (!resolve_compcall (e, &name))
6729 return false;
6731 /* Use the generic name if it is there. */
6732 name = name ? name : e->value.function.esym->name;
6733 e->symtree = expr->symtree;
6734 e->ref = gfc_copy_ref (expr->ref);
6735 get_declared_from_expr (&class_ref, NULL, e, false);
6737 /* Trim away the extraneous references that emerge from nested
6738 use of interface.c (extend_expr). */
6739 if (class_ref && class_ref->next)
6741 gfc_free_ref_list (class_ref->next);
6742 class_ref->next = NULL;
6744 else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
6746 gfc_free_ref_list (e->ref);
6747 e->ref = NULL;
6750 gfc_add_vptr_component (e);
6751 gfc_add_component_ref (e, name);
6752 e->value.function.esym = NULL;
6753 if (expr->expr_type != EXPR_VARIABLE)
6754 e->base_expr = expr;
6755 return true;
6758 if (st == NULL)
6759 return resolve_compcall (e, NULL);
6761 if (!gfc_resolve_ref (e))
6762 return false;
6764 /* Get the CLASS declared type. */
6765 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6767 if (!resolve_fl_derived (declared))
6768 return false;
6770 /* Weed out cases of the ultimate component being a derived type. */
6771 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6772 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6774 gfc_free_ref_list (new_ref);
6775 return resolve_compcall (e, NULL);
6778 c = gfc_find_component (declared, "_data", true, true, NULL);
6780 /* Treat the call as if it is a typebound procedure, in order to roll
6781 out the correct name for the specific function. */
6782 if (!resolve_compcall (e, &name))
6784 gfc_free_ref_list (new_ref);
6785 return false;
6787 ts = e->ts;
6789 if (overridable)
6791 /* Convert the expression to a procedure pointer component call. */
6792 e->value.function.esym = NULL;
6793 e->symtree = st;
6795 if (new_ref)
6796 e->ref = new_ref;
6798 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6799 gfc_add_vptr_component (e);
6800 gfc_add_component_ref (e, name);
6802 /* Recover the typespec for the expression. This is really only
6803 necessary for generic procedures, where the additional call
6804 to gfc_add_component_ref seems to throw the collection of the
6805 correct typespec. */
6806 e->ts = ts;
6808 else if (new_ref)
6809 gfc_free_ref_list (new_ref);
6811 return true;
6814 /* Resolve a typebound subroutine, or 'method'. First separate all
6815 the non-CLASS references by calling resolve_typebound_call
6816 directly. */
6818 static bool
6819 resolve_typebound_subroutine (gfc_code *code)
6821 gfc_symbol *declared;
6822 gfc_component *c;
6823 gfc_ref *new_ref;
6824 gfc_ref *class_ref;
6825 gfc_symtree *st;
6826 const char *name;
6827 gfc_typespec ts;
6828 gfc_expr *expr;
6829 bool overridable;
6831 st = code->expr1->symtree;
6833 /* Deal with typebound operators for CLASS objects. */
6834 expr = code->expr1->value.compcall.base_object;
6835 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6836 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6838 /* If the base_object is not a variable, the corresponding actual
6839 argument expression must be stored in e->base_expression so
6840 that the corresponding tree temporary can be used as the base
6841 object in gfc_conv_procedure_call. */
6842 if (expr->expr_type != EXPR_VARIABLE)
6844 gfc_actual_arglist *args;
6846 args= code->expr1->value.function.actual;
6847 for (; args; args = args->next)
6848 if (expr == args->expr)
6849 expr = args->expr;
6852 /* Since the typebound operators are generic, we have to ensure
6853 that any delays in resolution are corrected and that the vtab
6854 is present. */
6855 declared = expr->ts.u.derived;
6856 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6857 if (c->ts.u.derived == NULL)
6858 c->ts.u.derived = gfc_find_derived_vtab (declared);
6860 if (!resolve_typebound_call (code, &name, NULL))
6861 return false;
6863 /* Use the generic name if it is there. */
6864 name = name ? name : code->expr1->value.function.esym->name;
6865 code->expr1->symtree = expr->symtree;
6866 code->expr1->ref = gfc_copy_ref (expr->ref);
6868 /* Trim away the extraneous references that emerge from nested
6869 use of interface.c (extend_expr). */
6870 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6871 if (class_ref && class_ref->next)
6873 gfc_free_ref_list (class_ref->next);
6874 class_ref->next = NULL;
6876 else if (code->expr1->ref && !class_ref)
6878 gfc_free_ref_list (code->expr1->ref);
6879 code->expr1->ref = NULL;
6882 /* Now use the procedure in the vtable. */
6883 gfc_add_vptr_component (code->expr1);
6884 gfc_add_component_ref (code->expr1, name);
6885 code->expr1->value.function.esym = NULL;
6886 if (expr->expr_type != EXPR_VARIABLE)
6887 code->expr1->base_expr = expr;
6888 return true;
6891 if (st == NULL)
6892 return resolve_typebound_call (code, NULL, NULL);
6894 if (!gfc_resolve_ref (code->expr1))
6895 return false;
6897 /* Get the CLASS declared type. */
6898 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
6900 /* Weed out cases of the ultimate component being a derived type. */
6901 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6902 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6904 gfc_free_ref_list (new_ref);
6905 return resolve_typebound_call (code, NULL, NULL);
6908 if (!resolve_typebound_call (code, &name, &overridable))
6910 gfc_free_ref_list (new_ref);
6911 return false;
6913 ts = code->expr1->ts;
6915 if (overridable)
6917 /* Convert the expression to a procedure pointer component call. */
6918 code->expr1->value.function.esym = NULL;
6919 code->expr1->symtree = st;
6921 if (new_ref)
6922 code->expr1->ref = new_ref;
6924 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6925 gfc_add_vptr_component (code->expr1);
6926 gfc_add_component_ref (code->expr1, name);
6928 /* Recover the typespec for the expression. This is really only
6929 necessary for generic procedures, where the additional call
6930 to gfc_add_component_ref seems to throw the collection of the
6931 correct typespec. */
6932 code->expr1->ts = ts;
6934 else if (new_ref)
6935 gfc_free_ref_list (new_ref);
6937 return true;
6941 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6943 static bool
6944 resolve_ppc_call (gfc_code* c)
6946 gfc_component *comp;
6948 comp = gfc_get_proc_ptr_comp (c->expr1);
6949 gcc_assert (comp != NULL);
6951 c->resolved_sym = c->expr1->symtree->n.sym;
6952 c->expr1->expr_type = EXPR_VARIABLE;
6954 if (!comp->attr.subroutine)
6955 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6957 if (!gfc_resolve_ref (c->expr1))
6958 return false;
6960 if (!update_ppc_arglist (c->expr1))
6961 return false;
6963 c->ext.actual = c->expr1->value.compcall.actual;
6965 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6966 !(comp->ts.interface
6967 && comp->ts.interface->formal)))
6968 return false;
6970 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
6971 return false;
6973 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6975 return true;
6979 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6981 static bool
6982 resolve_expr_ppc (gfc_expr* e)
6984 gfc_component *comp;
6986 comp = gfc_get_proc_ptr_comp (e);
6987 gcc_assert (comp != NULL);
6989 /* Convert to EXPR_FUNCTION. */
6990 e->expr_type = EXPR_FUNCTION;
6991 e->value.function.isym = NULL;
6992 e->value.function.actual = e->value.compcall.actual;
6993 e->ts = comp->ts;
6994 if (comp->as != NULL)
6995 e->rank = comp->as->rank;
6997 if (!comp->attr.function)
6998 gfc_add_function (&comp->attr, comp->name, &e->where);
7000 if (!gfc_resolve_ref (e))
7001 return false;
7003 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
7004 !(comp->ts.interface
7005 && comp->ts.interface->formal)))
7006 return false;
7008 if (!update_ppc_arglist (e))
7009 return false;
7011 if (!check_pure_function(e))
7012 return false;
7014 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
7016 return true;
7020 static bool
7021 gfc_is_expandable_expr (gfc_expr *e)
7023 gfc_constructor *con;
7025 if (e->expr_type == EXPR_ARRAY)
7027 /* Traverse the constructor looking for variables that are flavor
7028 parameter. Parameters must be expanded since they are fully used at
7029 compile time. */
7030 con = gfc_constructor_first (e->value.constructor);
7031 for (; con; con = gfc_constructor_next (con))
7033 if (con->expr->expr_type == EXPR_VARIABLE
7034 && con->expr->symtree
7035 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
7036 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
7037 return true;
7038 if (con->expr->expr_type == EXPR_ARRAY
7039 && gfc_is_expandable_expr (con->expr))
7040 return true;
7044 return false;
7048 /* Sometimes variables in specification expressions of the result
7049 of module procedures in submodules wind up not being the 'real'
7050 dummy. Find this, if possible, in the namespace of the first
7051 formal argument. */
7053 static void
7054 fixup_unique_dummy (gfc_expr *e)
7056 gfc_symtree *st = NULL;
7057 gfc_symbol *s = NULL;
7059 if (e->symtree->n.sym->ns->proc_name
7060 && e->symtree->n.sym->ns->proc_name->formal)
7061 s = e->symtree->n.sym->ns->proc_name->formal->sym;
7063 if (s != NULL)
7064 st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
7066 if (st != NULL
7067 && st->n.sym != NULL
7068 && st->n.sym->attr.dummy)
7069 e->symtree = st;
7072 /* Resolve an expression. That is, make sure that types of operands agree
7073 with their operators, intrinsic operators are converted to function calls
7074 for overloaded types and unresolved function references are resolved. */
7076 bool
7077 gfc_resolve_expr (gfc_expr *e)
7079 bool t;
7080 bool inquiry_save, actual_arg_save, first_actual_arg_save;
7082 if (e == NULL || e->do_not_resolve_again)
7083 return true;
7085 /* inquiry_argument only applies to variables. */
7086 inquiry_save = inquiry_argument;
7087 actual_arg_save = actual_arg;
7088 first_actual_arg_save = first_actual_arg;
7090 if (e->expr_type != EXPR_VARIABLE)
7092 inquiry_argument = false;
7093 actual_arg = false;
7094 first_actual_arg = false;
7096 else if (e->symtree != NULL
7097 && *e->symtree->name == '@'
7098 && e->symtree->n.sym->attr.dummy)
7100 /* Deal with submodule specification expressions that are not
7101 found to be referenced in module.c(read_cleanup). */
7102 fixup_unique_dummy (e);
7105 switch (e->expr_type)
7107 case EXPR_OP:
7108 t = resolve_operator (e);
7109 break;
7111 case EXPR_FUNCTION:
7112 case EXPR_VARIABLE:
7114 if (check_host_association (e))
7115 t = resolve_function (e);
7116 else
7117 t = resolve_variable (e);
7119 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
7120 && e->ref->type != REF_SUBSTRING)
7121 gfc_resolve_substring_charlen (e);
7123 break;
7125 case EXPR_COMPCALL:
7126 t = resolve_typebound_function (e);
7127 break;
7129 case EXPR_SUBSTRING:
7130 t = gfc_resolve_ref (e);
7131 break;
7133 case EXPR_CONSTANT:
7134 case EXPR_NULL:
7135 t = true;
7136 break;
7138 case EXPR_PPC:
7139 t = resolve_expr_ppc (e);
7140 break;
7142 case EXPR_ARRAY:
7143 t = false;
7144 if (!gfc_resolve_ref (e))
7145 break;
7147 t = gfc_resolve_array_constructor (e);
7148 /* Also try to expand a constructor. */
7149 if (t)
7151 gfc_expression_rank (e);
7152 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
7153 gfc_expand_constructor (e, false);
7156 /* This provides the opportunity for the length of constructors with
7157 character valued function elements to propagate the string length
7158 to the expression. */
7159 if (t && e->ts.type == BT_CHARACTER)
7161 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
7162 here rather then add a duplicate test for it above. */
7163 gfc_expand_constructor (e, false);
7164 t = gfc_resolve_character_array_constructor (e);
7167 break;
7169 case EXPR_STRUCTURE:
7170 t = gfc_resolve_ref (e);
7171 if (!t)
7172 break;
7174 t = resolve_structure_cons (e, 0);
7175 if (!t)
7176 break;
7178 t = gfc_simplify_expr (e, 0);
7179 break;
7181 default:
7182 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
7185 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
7186 fixup_charlen (e);
7188 inquiry_argument = inquiry_save;
7189 actual_arg = actual_arg_save;
7190 first_actual_arg = first_actual_arg_save;
7192 /* For some reason, resolving these expressions a second time mangles
7193 the typespec of the expression itself. */
7194 if (t && e->expr_type == EXPR_VARIABLE
7195 && e->symtree->n.sym->attr.select_rank_temporary
7196 && UNLIMITED_POLY (e->symtree->n.sym))
7197 e->do_not_resolve_again = 1;
7199 return t;
7203 /* Resolve an expression from an iterator. They must be scalar and have
7204 INTEGER or (optionally) REAL type. */
7206 static bool
7207 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
7208 const char *name_msgid)
7210 if (!gfc_resolve_expr (expr))
7211 return false;
7213 if (expr->rank != 0)
7215 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
7216 return false;
7219 if (expr->ts.type != BT_INTEGER)
7221 if (expr->ts.type == BT_REAL)
7223 if (real_ok)
7224 return gfc_notify_std (GFC_STD_F95_DEL,
7225 "%s at %L must be integer",
7226 _(name_msgid), &expr->where);
7227 else
7229 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
7230 &expr->where);
7231 return false;
7234 else
7236 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
7237 return false;
7240 return true;
7244 /* Resolve the expressions in an iterator structure. If REAL_OK is
7245 false allow only INTEGER type iterators, otherwise allow REAL types.
7246 Set own_scope to true for ac-implied-do and data-implied-do as those
7247 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
7249 bool
7250 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
7252 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
7253 return false;
7255 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
7256 _("iterator variable")))
7257 return false;
7259 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
7260 "Start expression in DO loop"))
7261 return false;
7263 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
7264 "End expression in DO loop"))
7265 return false;
7267 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
7268 "Step expression in DO loop"))
7269 return false;
7271 /* Convert start, end, and step to the same type as var. */
7272 if (iter->start->ts.kind != iter->var->ts.kind
7273 || iter->start->ts.type != iter->var->ts.type)
7274 gfc_convert_type (iter->start, &iter->var->ts, 1);
7276 if (iter->end->ts.kind != iter->var->ts.kind
7277 || iter->end->ts.type != iter->var->ts.type)
7278 gfc_convert_type (iter->end, &iter->var->ts, 1);
7280 if (iter->step->ts.kind != iter->var->ts.kind
7281 || iter->step->ts.type != iter->var->ts.type)
7282 gfc_convert_type (iter->step, &iter->var->ts, 1);
7284 if (iter->step->expr_type == EXPR_CONSTANT)
7286 if ((iter->step->ts.type == BT_INTEGER
7287 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
7288 || (iter->step->ts.type == BT_REAL
7289 && mpfr_sgn (iter->step->value.real) == 0))
7291 gfc_error ("Step expression in DO loop at %L cannot be zero",
7292 &iter->step->where);
7293 return false;
7297 if (iter->start->expr_type == EXPR_CONSTANT
7298 && iter->end->expr_type == EXPR_CONSTANT
7299 && iter->step->expr_type == EXPR_CONSTANT)
7301 int sgn, cmp;
7302 if (iter->start->ts.type == BT_INTEGER)
7304 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
7305 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
7307 else
7309 sgn = mpfr_sgn (iter->step->value.real);
7310 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
7312 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
7313 gfc_warning (OPT_Wzerotrip,
7314 "DO loop at %L will be executed zero times",
7315 &iter->step->where);
7318 if (iter->end->expr_type == EXPR_CONSTANT
7319 && iter->end->ts.type == BT_INTEGER
7320 && iter->step->expr_type == EXPR_CONSTANT
7321 && iter->step->ts.type == BT_INTEGER
7322 && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
7323 || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
7325 bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
7326 int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
7328 if (is_step_positive
7329 && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
7330 gfc_warning (OPT_Wundefined_do_loop,
7331 "DO loop at %L is undefined as it overflows",
7332 &iter->step->where);
7333 else if (!is_step_positive
7334 && mpz_cmp (iter->end->value.integer,
7335 gfc_integer_kinds[k].min_int) == 0)
7336 gfc_warning (OPT_Wundefined_do_loop,
7337 "DO loop at %L is undefined as it underflows",
7338 &iter->step->where);
7341 return true;
7345 /* Traversal function for find_forall_index. f == 2 signals that
7346 that variable itself is not to be checked - only the references. */
7348 static bool
7349 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
7351 if (expr->expr_type != EXPR_VARIABLE)
7352 return false;
7354 /* A scalar assignment */
7355 if (!expr->ref || *f == 1)
7357 if (expr->symtree->n.sym == sym)
7358 return true;
7359 else
7360 return false;
7363 if (*f == 2)
7364 *f = 1;
7365 return false;
7369 /* Check whether the FORALL index appears in the expression or not.
7370 Returns true if SYM is found in EXPR. */
7372 bool
7373 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
7375 if (gfc_traverse_expr (expr, sym, forall_index, f))
7376 return true;
7377 else
7378 return false;
7382 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
7383 to be a scalar INTEGER variable. The subscripts and stride are scalar
7384 INTEGERs, and if stride is a constant it must be nonzero.
7385 Furthermore "A subscript or stride in a forall-triplet-spec shall
7386 not contain a reference to any index-name in the
7387 forall-triplet-spec-list in which it appears." (7.5.4.1) */
7389 static void
7390 resolve_forall_iterators (gfc_forall_iterator *it)
7392 gfc_forall_iterator *iter, *iter2;
7394 for (iter = it; iter; iter = iter->next)
7396 if (gfc_resolve_expr (iter->var)
7397 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
7398 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
7399 &iter->var->where);
7401 if (gfc_resolve_expr (iter->start)
7402 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
7403 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
7404 &iter->start->where);
7405 if (iter->var->ts.kind != iter->start->ts.kind)
7406 gfc_convert_type (iter->start, &iter->var->ts, 1);
7408 if (gfc_resolve_expr (iter->end)
7409 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
7410 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
7411 &iter->end->where);
7412 if (iter->var->ts.kind != iter->end->ts.kind)
7413 gfc_convert_type (iter->end, &iter->var->ts, 1);
7415 if (gfc_resolve_expr (iter->stride))
7417 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
7418 gfc_error ("FORALL stride expression at %L must be a scalar %s",
7419 &iter->stride->where, "INTEGER");
7421 if (iter->stride->expr_type == EXPR_CONSTANT
7422 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
7423 gfc_error ("FORALL stride expression at %L cannot be zero",
7424 &iter->stride->where);
7426 if (iter->var->ts.kind != iter->stride->ts.kind)
7427 gfc_convert_type (iter->stride, &iter->var->ts, 1);
7430 for (iter = it; iter; iter = iter->next)
7431 for (iter2 = iter; iter2; iter2 = iter2->next)
7433 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
7434 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
7435 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
7436 gfc_error ("FORALL index %qs may not appear in triplet "
7437 "specification at %L", iter->var->symtree->name,
7438 &iter2->start->where);
7443 /* Given a pointer to a symbol that is a derived type, see if it's
7444 inaccessible, i.e. if it's defined in another module and the components are
7445 PRIVATE. The search is recursive if necessary. Returns zero if no
7446 inaccessible components are found, nonzero otherwise. */
7448 static int
7449 derived_inaccessible (gfc_symbol *sym)
7451 gfc_component *c;
7453 if (sym->attr.use_assoc && sym->attr.private_comp)
7454 return 1;
7456 for (c = sym->components; c; c = c->next)
7458 /* Prevent an infinite loop through this function. */
7459 if (c->ts.type == BT_DERIVED && c->attr.pointer
7460 && sym == c->ts.u.derived)
7461 continue;
7463 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
7464 return 1;
7467 return 0;
7471 /* Resolve the argument of a deallocate expression. The expression must be
7472 a pointer or a full array. */
7474 static bool
7475 resolve_deallocate_expr (gfc_expr *e)
7477 symbol_attribute attr;
7478 int allocatable, pointer;
7479 gfc_ref *ref;
7480 gfc_symbol *sym;
7481 gfc_component *c;
7482 bool unlimited;
7484 if (!gfc_resolve_expr (e))
7485 return false;
7487 if (e->expr_type != EXPR_VARIABLE)
7488 goto bad;
7490 sym = e->symtree->n.sym;
7491 unlimited = UNLIMITED_POLY(sym);
7493 if (sym->ts.type == BT_CLASS)
7495 allocatable = CLASS_DATA (sym)->attr.allocatable;
7496 pointer = CLASS_DATA (sym)->attr.class_pointer;
7498 else
7500 allocatable = sym->attr.allocatable;
7501 pointer = sym->attr.pointer;
7503 for (ref = e->ref; ref; ref = ref->next)
7505 switch (ref->type)
7507 case REF_ARRAY:
7508 if (ref->u.ar.type != AR_FULL
7509 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
7510 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
7511 allocatable = 0;
7512 break;
7514 case REF_COMPONENT:
7515 c = ref->u.c.component;
7516 if (c->ts.type == BT_CLASS)
7518 allocatable = CLASS_DATA (c)->attr.allocatable;
7519 pointer = CLASS_DATA (c)->attr.class_pointer;
7521 else
7523 allocatable = c->attr.allocatable;
7524 pointer = c->attr.pointer;
7526 break;
7528 case REF_SUBSTRING:
7529 case REF_INQUIRY:
7530 allocatable = 0;
7531 break;
7535 attr = gfc_expr_attr (e);
7537 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
7539 bad:
7540 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7541 &e->where);
7542 return false;
7545 /* F2008, C644. */
7546 if (gfc_is_coindexed (e))
7548 gfc_error ("Coindexed allocatable object at %L", &e->where);
7549 return false;
7552 if (pointer
7553 && !gfc_check_vardef_context (e, true, true, false,
7554 _("DEALLOCATE object")))
7555 return false;
7556 if (!gfc_check_vardef_context (e, false, true, false,
7557 _("DEALLOCATE object")))
7558 return false;
7560 return true;
7564 /* Returns true if the expression e contains a reference to the symbol sym. */
7565 static bool
7566 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
7568 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
7569 return true;
7571 return false;
7574 bool
7575 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
7577 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
7581 /* Given the expression node e for an allocatable/pointer of derived type to be
7582 allocated, get the expression node to be initialized afterwards (needed for
7583 derived types with default initializers, and derived types with allocatable
7584 components that need nullification.) */
7586 gfc_expr *
7587 gfc_expr_to_initialize (gfc_expr *e)
7589 gfc_expr *result;
7590 gfc_ref *ref;
7591 int i;
7593 result = gfc_copy_expr (e);
7595 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
7596 for (ref = result->ref; ref; ref = ref->next)
7597 if (ref->type == REF_ARRAY && ref->next == NULL)
7599 if (ref->u.ar.dimen == 0
7600 && ref->u.ar.as && ref->u.ar.as->corank)
7601 return result;
7603 ref->u.ar.type = AR_FULL;
7605 for (i = 0; i < ref->u.ar.dimen; i++)
7606 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
7608 break;
7611 gfc_free_shape (&result->shape, result->rank);
7613 /* Recalculate rank, shape, etc. */
7614 gfc_resolve_expr (result);
7615 return result;
7619 /* If the last ref of an expression is an array ref, return a copy of the
7620 expression with that one removed. Otherwise, a copy of the original
7621 expression. This is used for allocate-expressions and pointer assignment
7622 LHS, where there may be an array specification that needs to be stripped
7623 off when using gfc_check_vardef_context. */
7625 static gfc_expr*
7626 remove_last_array_ref (gfc_expr* e)
7628 gfc_expr* e2;
7629 gfc_ref** r;
7631 e2 = gfc_copy_expr (e);
7632 for (r = &e2->ref; *r; r = &(*r)->next)
7633 if ((*r)->type == REF_ARRAY && !(*r)->next)
7635 gfc_free_ref_list (*r);
7636 *r = NULL;
7637 break;
7640 return e2;
7644 /* Used in resolve_allocate_expr to check that a allocation-object and
7645 a source-expr are conformable. This does not catch all possible
7646 cases; in particular a runtime checking is needed. */
7648 static bool
7649 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
7651 gfc_ref *tail;
7652 for (tail = e2->ref; tail && tail->next; tail = tail->next);
7654 /* First compare rank. */
7655 if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
7656 || (!tail && e1->rank != e2->rank))
7658 gfc_error ("Source-expr at %L must be scalar or have the "
7659 "same rank as the allocate-object at %L",
7660 &e1->where, &e2->where);
7661 return false;
7664 if (e1->shape)
7666 int i;
7667 mpz_t s;
7669 mpz_init (s);
7671 for (i = 0; i < e1->rank; i++)
7673 if (tail->u.ar.start[i] == NULL)
7674 break;
7676 if (tail->u.ar.end[i])
7678 mpz_set (s, tail->u.ar.end[i]->value.integer);
7679 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
7680 mpz_add_ui (s, s, 1);
7682 else
7684 mpz_set (s, tail->u.ar.start[i]->value.integer);
7687 if (mpz_cmp (e1->shape[i], s) != 0)
7689 gfc_error ("Source-expr at %L and allocate-object at %L must "
7690 "have the same shape", &e1->where, &e2->where);
7691 mpz_clear (s);
7692 return false;
7696 mpz_clear (s);
7699 return true;
7703 /* Resolve the expression in an ALLOCATE statement, doing the additional
7704 checks to see whether the expression is OK or not. The expression must
7705 have a trailing array reference that gives the size of the array. */
7707 static bool
7708 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
7710 int i, pointer, allocatable, dimension, is_abstract;
7711 int codimension;
7712 bool coindexed;
7713 bool unlimited;
7714 symbol_attribute attr;
7715 gfc_ref *ref, *ref2;
7716 gfc_expr *e2;
7717 gfc_array_ref *ar;
7718 gfc_symbol *sym = NULL;
7719 gfc_alloc *a;
7720 gfc_component *c;
7721 bool t;
7723 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
7724 checking of coarrays. */
7725 for (ref = e->ref; ref; ref = ref->next)
7726 if (ref->next == NULL)
7727 break;
7729 if (ref && ref->type == REF_ARRAY)
7730 ref->u.ar.in_allocate = true;
7732 if (!gfc_resolve_expr (e))
7733 goto failure;
7735 /* Make sure the expression is allocatable or a pointer. If it is
7736 pointer, the next-to-last reference must be a pointer. */
7738 ref2 = NULL;
7739 if (e->symtree)
7740 sym = e->symtree->n.sym;
7742 /* Check whether ultimate component is abstract and CLASS. */
7743 is_abstract = 0;
7745 /* Is the allocate-object unlimited polymorphic? */
7746 unlimited = UNLIMITED_POLY(e);
7748 if (e->expr_type != EXPR_VARIABLE)
7750 allocatable = 0;
7751 attr = gfc_expr_attr (e);
7752 pointer = attr.pointer;
7753 dimension = attr.dimension;
7754 codimension = attr.codimension;
7756 else
7758 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
7760 allocatable = CLASS_DATA (sym)->attr.allocatable;
7761 pointer = CLASS_DATA (sym)->attr.class_pointer;
7762 dimension = CLASS_DATA (sym)->attr.dimension;
7763 codimension = CLASS_DATA (sym)->attr.codimension;
7764 is_abstract = CLASS_DATA (sym)->attr.abstract;
7766 else
7768 allocatable = sym->attr.allocatable;
7769 pointer = sym->attr.pointer;
7770 dimension = sym->attr.dimension;
7771 codimension = sym->attr.codimension;
7774 coindexed = false;
7776 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
7778 switch (ref->type)
7780 case REF_ARRAY:
7781 if (ref->u.ar.codimen > 0)
7783 int n;
7784 for (n = ref->u.ar.dimen;
7785 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
7786 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
7788 coindexed = true;
7789 break;
7793 if (ref->next != NULL)
7794 pointer = 0;
7795 break;
7797 case REF_COMPONENT:
7798 /* F2008, C644. */
7799 if (coindexed)
7801 gfc_error ("Coindexed allocatable object at %L",
7802 &e->where);
7803 goto failure;
7806 c = ref->u.c.component;
7807 if (c->ts.type == BT_CLASS)
7809 allocatable = CLASS_DATA (c)->attr.allocatable;
7810 pointer = CLASS_DATA (c)->attr.class_pointer;
7811 dimension = CLASS_DATA (c)->attr.dimension;
7812 codimension = CLASS_DATA (c)->attr.codimension;
7813 is_abstract = CLASS_DATA (c)->attr.abstract;
7815 else
7817 allocatable = c->attr.allocatable;
7818 pointer = c->attr.pointer;
7819 dimension = c->attr.dimension;
7820 codimension = c->attr.codimension;
7821 is_abstract = c->attr.abstract;
7823 break;
7825 case REF_SUBSTRING:
7826 case REF_INQUIRY:
7827 allocatable = 0;
7828 pointer = 0;
7829 break;
7834 /* Check for F08:C628 (F2018:C932). Each allocate-object shall be a data
7835 pointer or an allocatable variable. */
7836 if (allocatable == 0 && pointer == 0)
7838 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7839 &e->where);
7840 goto failure;
7843 /* Some checks for the SOURCE tag. */
7844 if (code->expr3)
7846 /* Check F03:C631. */
7847 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7849 gfc_error ("Type of entity at %L is type incompatible with "
7850 "source-expr at %L", &e->where, &code->expr3->where);
7851 goto failure;
7854 /* Check F03:C632 and restriction following Note 6.18. */
7855 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
7856 goto failure;
7858 /* Check F03:C633. */
7859 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
7861 gfc_error ("The allocate-object at %L and the source-expr at %L "
7862 "shall have the same kind type parameter",
7863 &e->where, &code->expr3->where);
7864 goto failure;
7867 /* Check F2008, C642. */
7868 if (code->expr3->ts.type == BT_DERIVED
7869 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7870 || (code->expr3->ts.u.derived->from_intmod
7871 == INTMOD_ISO_FORTRAN_ENV
7872 && code->expr3->ts.u.derived->intmod_sym_id
7873 == ISOFORTRAN_LOCK_TYPE)))
7875 gfc_error ("The source-expr at %L shall neither be of type "
7876 "LOCK_TYPE nor have a LOCK_TYPE component if "
7877 "allocate-object at %L is a coarray",
7878 &code->expr3->where, &e->where);
7879 goto failure;
7882 /* Check TS18508, C702/C703. */
7883 if (code->expr3->ts.type == BT_DERIVED
7884 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
7885 || (code->expr3->ts.u.derived->from_intmod
7886 == INTMOD_ISO_FORTRAN_ENV
7887 && code->expr3->ts.u.derived->intmod_sym_id
7888 == ISOFORTRAN_EVENT_TYPE)))
7890 gfc_error ("The source-expr at %L shall neither be of type "
7891 "EVENT_TYPE nor have a EVENT_TYPE component if "
7892 "allocate-object at %L is a coarray",
7893 &code->expr3->where, &e->where);
7894 goto failure;
7898 /* Check F08:C629. */
7899 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7900 && !code->expr3)
7902 gcc_assert (e->ts.type == BT_CLASS);
7903 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7904 "type-spec or source-expr", sym->name, &e->where);
7905 goto failure;
7908 /* Check F08:C632. */
7909 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
7910 && !UNLIMITED_POLY (e))
7912 int cmp;
7914 if (!e->ts.u.cl->length)
7915 goto failure;
7917 cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7918 code->ext.alloc.ts.u.cl->length);
7919 if (cmp == 1 || cmp == -1 || cmp == -3)
7921 gfc_error ("Allocating %s at %L with type-spec requires the same "
7922 "character-length parameter as in the declaration",
7923 sym->name, &e->where);
7924 goto failure;
7928 /* In the variable definition context checks, gfc_expr_attr is used
7929 on the expression. This is fooled by the array specification
7930 present in e, thus we have to eliminate that one temporarily. */
7931 e2 = remove_last_array_ref (e);
7932 t = true;
7933 if (t && pointer)
7934 t = gfc_check_vardef_context (e2, true, true, false,
7935 _("ALLOCATE object"));
7936 if (t)
7937 t = gfc_check_vardef_context (e2, false, true, false,
7938 _("ALLOCATE object"));
7939 gfc_free_expr (e2);
7940 if (!t)
7941 goto failure;
7943 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7944 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7946 /* For class arrays, the initialization with SOURCE is done
7947 using _copy and trans_call. It is convenient to exploit that
7948 when the allocated type is different from the declared type but
7949 no SOURCE exists by setting expr3. */
7950 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
7952 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
7953 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
7954 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
7956 /* We have to zero initialize the integer variable. */
7957 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
7960 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
7962 /* Make sure the vtab symbol is present when
7963 the module variables are generated. */
7964 gfc_typespec ts = e->ts;
7965 if (code->expr3)
7966 ts = code->expr3->ts;
7967 else if (code->ext.alloc.ts.type == BT_DERIVED)
7968 ts = code->ext.alloc.ts;
7970 /* Finding the vtab also publishes the type's symbol. Therefore this
7971 statement is necessary. */
7972 gfc_find_derived_vtab (ts.u.derived);
7974 else if (unlimited && !UNLIMITED_POLY (code->expr3))
7976 /* Again, make sure the vtab symbol is present when
7977 the module variables are generated. */
7978 gfc_typespec *ts = NULL;
7979 if (code->expr3)
7980 ts = &code->expr3->ts;
7981 else
7982 ts = &code->ext.alloc.ts;
7984 gcc_assert (ts);
7986 /* Finding the vtab also publishes the type's symbol. Therefore this
7987 statement is necessary. */
7988 gfc_find_vtab (ts);
7991 if (dimension == 0 && codimension == 0)
7992 goto success;
7994 /* Make sure the last reference node is an array specification. */
7996 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
7997 || (dimension && ref2->u.ar.dimen == 0))
7999 /* F08:C633. */
8000 if (code->expr3)
8002 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
8003 "in ALLOCATE statement at %L", &e->where))
8004 goto failure;
8005 if (code->expr3->rank != 0)
8006 *array_alloc_wo_spec = true;
8007 else
8009 gfc_error ("Array specification or array-valued SOURCE= "
8010 "expression required in ALLOCATE statement at %L",
8011 &e->where);
8012 goto failure;
8015 else
8017 gfc_error ("Array specification required in ALLOCATE statement "
8018 "at %L", &e->where);
8019 goto failure;
8023 /* Make sure that the array section reference makes sense in the
8024 context of an ALLOCATE specification. */
8026 ar = &ref2->u.ar;
8028 if (codimension)
8029 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
8031 switch (ar->dimen_type[i])
8033 case DIMEN_THIS_IMAGE:
8034 gfc_error ("Coarray specification required in ALLOCATE statement "
8035 "at %L", &e->where);
8036 goto failure;
8038 case DIMEN_RANGE:
8039 if (ar->start[i] == 0 || ar->end[i] == 0)
8041 /* If ar->stride[i] is NULL, we issued a previous error. */
8042 if (ar->stride[i] == NULL)
8043 gfc_error ("Bad array specification in ALLOCATE statement "
8044 "at %L", &e->where);
8045 goto failure;
8047 else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
8049 gfc_error ("Upper cobound is less than lower cobound at %L",
8050 &ar->start[i]->where);
8051 goto failure;
8053 break;
8055 case DIMEN_ELEMENT:
8056 if (ar->start[i]->expr_type == EXPR_CONSTANT)
8058 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
8059 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
8061 gfc_error ("Upper cobound is less than lower cobound "
8062 "of 1 at %L", &ar->start[i]->where);
8063 goto failure;
8066 break;
8068 case DIMEN_STAR:
8069 break;
8071 default:
8072 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8073 &e->where);
8074 goto failure;
8078 for (i = 0; i < ar->dimen; i++)
8080 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
8081 goto check_symbols;
8083 switch (ar->dimen_type[i])
8085 case DIMEN_ELEMENT:
8086 break;
8088 case DIMEN_RANGE:
8089 if (ar->start[i] != NULL
8090 && ar->end[i] != NULL
8091 && ar->stride[i] == NULL)
8092 break;
8094 /* Fall through. */
8096 case DIMEN_UNKNOWN:
8097 case DIMEN_VECTOR:
8098 case DIMEN_STAR:
8099 case DIMEN_THIS_IMAGE:
8100 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8101 &e->where);
8102 goto failure;
8105 check_symbols:
8106 for (a = code->ext.alloc.list; a; a = a->next)
8108 sym = a->expr->symtree->n.sym;
8110 /* TODO - check derived type components. */
8111 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
8112 continue;
8114 if ((ar->start[i] != NULL
8115 && gfc_find_sym_in_expr (sym, ar->start[i]))
8116 || (ar->end[i] != NULL
8117 && gfc_find_sym_in_expr (sym, ar->end[i])))
8119 gfc_error ("%qs must not appear in the array specification at "
8120 "%L in the same ALLOCATE statement where it is "
8121 "itself allocated", sym->name, &ar->where);
8122 goto failure;
8127 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
8129 if (ar->dimen_type[i] == DIMEN_ELEMENT
8130 || ar->dimen_type[i] == DIMEN_RANGE)
8132 if (i == (ar->dimen + ar->codimen - 1))
8134 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
8135 "statement at %L", &e->where);
8136 goto failure;
8138 continue;
8141 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
8142 && ar->stride[i] == NULL)
8143 break;
8145 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
8146 &e->where);
8147 goto failure;
8150 success:
8151 return true;
8153 failure:
8154 return false;
8158 static void
8159 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
8161 gfc_expr *stat, *errmsg, *pe, *qe;
8162 gfc_alloc *a, *p, *q;
8164 stat = code->expr1;
8165 errmsg = code->expr2;
8167 /* Check the stat variable. */
8168 if (stat)
8170 if (!gfc_check_vardef_context (stat, false, false, false,
8171 _("STAT variable")))
8172 goto done_stat;
8174 if (stat->ts.type != BT_INTEGER
8175 || stat->rank > 0)
8176 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
8177 "variable", &stat->where);
8179 if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
8180 goto done_stat;
8182 /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
8183 * within the ALLOCATE or DEALLOCATE statement in which it appears ...
8185 for (p = code->ext.alloc.list; p; p = p->next)
8186 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
8188 gfc_ref *ref1, *ref2;
8189 bool found = true;
8191 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
8192 ref1 = ref1->next, ref2 = ref2->next)
8194 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8195 continue;
8196 if (ref1->u.c.component->name != ref2->u.c.component->name)
8198 found = false;
8199 break;
8203 if (found)
8205 gfc_error ("Stat-variable at %L shall not be %sd within "
8206 "the same %s statement", &stat->where, fcn, fcn);
8207 break;
8212 done_stat:
8214 /* Check the errmsg variable. */
8215 if (errmsg)
8217 if (!stat)
8218 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
8219 &errmsg->where);
8221 if (!gfc_check_vardef_context (errmsg, false, false, false,
8222 _("ERRMSG variable")))
8223 goto done_errmsg;
8225 /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
8226 F18:R930 errmsg-variable is scalar-default-char-variable
8227 F18:R906 default-char-variable is variable
8228 F18:C906 default-char-variable shall be default character. */
8229 if (errmsg->ts.type != BT_CHARACTER
8230 || errmsg->rank > 0
8231 || errmsg->ts.kind != gfc_default_character_kind)
8232 gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
8233 "variable", &errmsg->where);
8235 if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
8236 goto done_errmsg;
8238 /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
8239 * within the ALLOCATE or DEALLOCATE statement in which it appears ...
8241 for (p = code->ext.alloc.list; p; p = p->next)
8242 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
8244 gfc_ref *ref1, *ref2;
8245 bool found = true;
8247 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
8248 ref1 = ref1->next, ref2 = ref2->next)
8250 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8251 continue;
8252 if (ref1->u.c.component->name != ref2->u.c.component->name)
8254 found = false;
8255 break;
8259 if (found)
8261 gfc_error ("Errmsg-variable at %L shall not be %sd within "
8262 "the same %s statement", &errmsg->where, fcn, fcn);
8263 break;
8268 done_errmsg:
8270 /* Check that an allocate-object appears only once in the statement. */
8272 for (p = code->ext.alloc.list; p; p = p->next)
8274 pe = p->expr;
8275 for (q = p->next; q; q = q->next)
8277 qe = q->expr;
8278 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
8280 /* This is a potential collision. */
8281 gfc_ref *pr = pe->ref;
8282 gfc_ref *qr = qe->ref;
8284 /* Follow the references until
8285 a) They start to differ, in which case there is no error;
8286 you can deallocate a%b and a%c in a single statement
8287 b) Both of them stop, which is an error
8288 c) One of them stops, which is also an error. */
8289 while (1)
8291 if (pr == NULL && qr == NULL)
8293 gfc_error ("Allocate-object at %L also appears at %L",
8294 &pe->where, &qe->where);
8295 break;
8297 else if (pr != NULL && qr == NULL)
8299 gfc_error ("Allocate-object at %L is subobject of"
8300 " object at %L", &pe->where, &qe->where);
8301 break;
8303 else if (pr == NULL && qr != NULL)
8305 gfc_error ("Allocate-object at %L is subobject of"
8306 " object at %L", &qe->where, &pe->where);
8307 break;
8309 /* Here, pr != NULL && qr != NULL */
8310 gcc_assert(pr->type == qr->type);
8311 if (pr->type == REF_ARRAY)
8313 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
8314 which are legal. */
8315 gcc_assert (qr->type == REF_ARRAY);
8317 if (pr->next && qr->next)
8319 int i;
8320 gfc_array_ref *par = &(pr->u.ar);
8321 gfc_array_ref *qar = &(qr->u.ar);
8323 for (i=0; i<par->dimen; i++)
8325 if ((par->start[i] != NULL
8326 || qar->start[i] != NULL)
8327 && gfc_dep_compare_expr (par->start[i],
8328 qar->start[i]) != 0)
8329 goto break_label;
8333 else
8335 if (pr->u.c.component->name != qr->u.c.component->name)
8336 break;
8339 pr = pr->next;
8340 qr = qr->next;
8342 break_label:
8348 if (strcmp (fcn, "ALLOCATE") == 0)
8350 bool arr_alloc_wo_spec = false;
8352 /* Resolving the expr3 in the loop over all objects to allocate would
8353 execute loop invariant code for each loop item. Therefore do it just
8354 once here. */
8355 if (code->expr3 && code->expr3->mold
8356 && code->expr3->ts.type == BT_DERIVED)
8358 /* Default initialization via MOLD (non-polymorphic). */
8359 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
8360 if (rhs != NULL)
8362 gfc_resolve_expr (rhs);
8363 gfc_free_expr (code->expr3);
8364 code->expr3 = rhs;
8367 for (a = code->ext.alloc.list; a; a = a->next)
8368 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
8370 if (arr_alloc_wo_spec && code->expr3)
8372 /* Mark the allocate to have to take the array specification
8373 from the expr3. */
8374 code->ext.alloc.arr_spec_from_expr3 = 1;
8377 else
8379 for (a = code->ext.alloc.list; a; a = a->next)
8380 resolve_deallocate_expr (a->expr);
8385 /************ SELECT CASE resolution subroutines ************/
8387 /* Callback function for our mergesort variant. Determines interval
8388 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
8389 op1 > op2. Assumes we're not dealing with the default case.
8390 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
8391 There are nine situations to check. */
8393 static int
8394 compare_cases (const gfc_case *op1, const gfc_case *op2)
8396 int retval;
8398 if (op1->low == NULL) /* op1 = (:L) */
8400 /* op2 = (:N), so overlap. */
8401 retval = 0;
8402 /* op2 = (M:) or (M:N), L < M */
8403 if (op2->low != NULL
8404 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8405 retval = -1;
8407 else if (op1->high == NULL) /* op1 = (K:) */
8409 /* op2 = (M:), so overlap. */
8410 retval = 0;
8411 /* op2 = (:N) or (M:N), K > N */
8412 if (op2->high != NULL
8413 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8414 retval = 1;
8416 else /* op1 = (K:L) */
8418 if (op2->low == NULL) /* op2 = (:N), K > N */
8419 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8420 ? 1 : 0;
8421 else if (op2->high == NULL) /* op2 = (M:), L < M */
8422 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8423 ? -1 : 0;
8424 else /* op2 = (M:N) */
8426 retval = 0;
8427 /* L < M */
8428 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8429 retval = -1;
8430 /* K > N */
8431 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8432 retval = 1;
8436 return retval;
8440 /* Merge-sort a double linked case list, detecting overlap in the
8441 process. LIST is the head of the double linked case list before it
8442 is sorted. Returns the head of the sorted list if we don't see any
8443 overlap, or NULL otherwise. */
8445 static gfc_case *
8446 check_case_overlap (gfc_case *list)
8448 gfc_case *p, *q, *e, *tail;
8449 int insize, nmerges, psize, qsize, cmp, overlap_seen;
8451 /* If the passed list was empty, return immediately. */
8452 if (!list)
8453 return NULL;
8455 overlap_seen = 0;
8456 insize = 1;
8458 /* Loop unconditionally. The only exit from this loop is a return
8459 statement, when we've finished sorting the case list. */
8460 for (;;)
8462 p = list;
8463 list = NULL;
8464 tail = NULL;
8466 /* Count the number of merges we do in this pass. */
8467 nmerges = 0;
8469 /* Loop while there exists a merge to be done. */
8470 while (p)
8472 int i;
8474 /* Count this merge. */
8475 nmerges++;
8477 /* Cut the list in two pieces by stepping INSIZE places
8478 forward in the list, starting from P. */
8479 psize = 0;
8480 q = p;
8481 for (i = 0; i < insize; i++)
8483 psize++;
8484 q = q->right;
8485 if (!q)
8486 break;
8488 qsize = insize;
8490 /* Now we have two lists. Merge them! */
8491 while (psize > 0 || (qsize > 0 && q != NULL))
8493 /* See from which the next case to merge comes from. */
8494 if (psize == 0)
8496 /* P is empty so the next case must come from Q. */
8497 e = q;
8498 q = q->right;
8499 qsize--;
8501 else if (qsize == 0 || q == NULL)
8503 /* Q is empty. */
8504 e = p;
8505 p = p->right;
8506 psize--;
8508 else
8510 cmp = compare_cases (p, q);
8511 if (cmp < 0)
8513 /* The whole case range for P is less than the
8514 one for Q. */
8515 e = p;
8516 p = p->right;
8517 psize--;
8519 else if (cmp > 0)
8521 /* The whole case range for Q is greater than
8522 the case range for P. */
8523 e = q;
8524 q = q->right;
8525 qsize--;
8527 else
8529 /* The cases overlap, or they are the same
8530 element in the list. Either way, we must
8531 issue an error and get the next case from P. */
8532 /* FIXME: Sort P and Q by line number. */
8533 gfc_error ("CASE label at %L overlaps with CASE "
8534 "label at %L", &p->where, &q->where);
8535 overlap_seen = 1;
8536 e = p;
8537 p = p->right;
8538 psize--;
8542 /* Add the next element to the merged list. */
8543 if (tail)
8544 tail->right = e;
8545 else
8546 list = e;
8547 e->left = tail;
8548 tail = e;
8551 /* P has now stepped INSIZE places along, and so has Q. So
8552 they're the same. */
8553 p = q;
8555 tail->right = NULL;
8557 /* If we have done only one merge or none at all, we've
8558 finished sorting the cases. */
8559 if (nmerges <= 1)
8561 if (!overlap_seen)
8562 return list;
8563 else
8564 return NULL;
8567 /* Otherwise repeat, merging lists twice the size. */
8568 insize *= 2;
8573 /* Check to see if an expression is suitable for use in a CASE statement.
8574 Makes sure that all case expressions are scalar constants of the same
8575 type. Return false if anything is wrong. */
8577 static bool
8578 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
8580 if (e == NULL) return true;
8582 if (e->ts.type != case_expr->ts.type)
8584 gfc_error ("Expression in CASE statement at %L must be of type %s",
8585 &e->where, gfc_basic_typename (case_expr->ts.type));
8586 return false;
8589 /* C805 (R808) For a given case-construct, each case-value shall be of
8590 the same type as case-expr. For character type, length differences
8591 are allowed, but the kind type parameters shall be the same. */
8593 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
8595 gfc_error ("Expression in CASE statement at %L must be of kind %d",
8596 &e->where, case_expr->ts.kind);
8597 return false;
8600 /* Convert the case value kind to that of case expression kind,
8601 if needed */
8603 if (e->ts.kind != case_expr->ts.kind)
8604 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
8606 if (e->rank != 0)
8608 gfc_error ("Expression in CASE statement at %L must be scalar",
8609 &e->where);
8610 return false;
8613 return true;
8617 /* Given a completely parsed select statement, we:
8619 - Validate all expressions and code within the SELECT.
8620 - Make sure that the selection expression is not of the wrong type.
8621 - Make sure that no case ranges overlap.
8622 - Eliminate unreachable cases and unreachable code resulting from
8623 removing case labels.
8625 The standard does allow unreachable cases, e.g. CASE (5:3). But
8626 they are a hassle for code generation, and to prevent that, we just
8627 cut them out here. This is not necessary for overlapping cases
8628 because they are illegal and we never even try to generate code.
8630 We have the additional caveat that a SELECT construct could have
8631 been a computed GOTO in the source code. Fortunately we can fairly
8632 easily work around that here: The case_expr for a "real" SELECT CASE
8633 is in code->expr1, but for a computed GOTO it is in code->expr2. All
8634 we have to do is make sure that the case_expr is a scalar integer
8635 expression. */
8637 static void
8638 resolve_select (gfc_code *code, bool select_type)
8640 gfc_code *body;
8641 gfc_expr *case_expr;
8642 gfc_case *cp, *default_case, *tail, *head;
8643 int seen_unreachable;
8644 int seen_logical;
8645 int ncases;
8646 bt type;
8647 bool t;
8649 if (code->expr1 == NULL)
8651 /* This was actually a computed GOTO statement. */
8652 case_expr = code->expr2;
8653 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
8654 gfc_error ("Selection expression in computed GOTO statement "
8655 "at %L must be a scalar integer expression",
8656 &case_expr->where);
8658 /* Further checking is not necessary because this SELECT was built
8659 by the compiler, so it should always be OK. Just move the
8660 case_expr from expr2 to expr so that we can handle computed
8661 GOTOs as normal SELECTs from here on. */
8662 code->expr1 = code->expr2;
8663 code->expr2 = NULL;
8664 return;
8667 case_expr = code->expr1;
8668 type = case_expr->ts.type;
8670 /* F08:C830. */
8671 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
8673 gfc_error ("Argument of SELECT statement at %L cannot be %s",
8674 &case_expr->where, gfc_typename (case_expr));
8676 /* Punt. Going on here just produce more garbage error messages. */
8677 return;
8680 /* F08:R842. */
8681 if (!select_type && case_expr->rank != 0)
8683 gfc_error ("Argument of SELECT statement at %L must be a scalar "
8684 "expression", &case_expr->where);
8686 /* Punt. */
8687 return;
8690 /* Raise a warning if an INTEGER case value exceeds the range of
8691 the case-expr. Later, all expressions will be promoted to the
8692 largest kind of all case-labels. */
8694 if (type == BT_INTEGER)
8695 for (body = code->block; body; body = body->block)
8696 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8698 if (cp->low
8699 && gfc_check_integer_range (cp->low->value.integer,
8700 case_expr->ts.kind) != ARITH_OK)
8701 gfc_warning (0, "Expression in CASE statement at %L is "
8702 "not in the range of %s", &cp->low->where,
8703 gfc_typename (case_expr));
8705 if (cp->high
8706 && cp->low != cp->high
8707 && gfc_check_integer_range (cp->high->value.integer,
8708 case_expr->ts.kind) != ARITH_OK)
8709 gfc_warning (0, "Expression in CASE statement at %L is "
8710 "not in the range of %s", &cp->high->where,
8711 gfc_typename (case_expr));
8714 /* PR 19168 has a long discussion concerning a mismatch of the kinds
8715 of the SELECT CASE expression and its CASE values. Walk the lists
8716 of case values, and if we find a mismatch, promote case_expr to
8717 the appropriate kind. */
8719 if (type == BT_LOGICAL || type == BT_INTEGER)
8721 for (body = code->block; body; body = body->block)
8723 /* Walk the case label list. */
8724 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8726 /* Intercept the DEFAULT case. It does not have a kind. */
8727 if (cp->low == NULL && cp->high == NULL)
8728 continue;
8730 /* Unreachable case ranges are discarded, so ignore. */
8731 if (cp->low != NULL && cp->high != NULL
8732 && cp->low != cp->high
8733 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8734 continue;
8736 if (cp->low != NULL
8737 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
8738 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
8740 if (cp->high != NULL
8741 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
8742 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
8747 /* Assume there is no DEFAULT case. */
8748 default_case = NULL;
8749 head = tail = NULL;
8750 ncases = 0;
8751 seen_logical = 0;
8753 for (body = code->block; body; body = body->block)
8755 /* Assume the CASE list is OK, and all CASE labels can be matched. */
8756 t = true;
8757 seen_unreachable = 0;
8759 /* Walk the case label list, making sure that all case labels
8760 are legal. */
8761 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8763 /* Count the number of cases in the whole construct. */
8764 ncases++;
8766 /* Intercept the DEFAULT case. */
8767 if (cp->low == NULL && cp->high == NULL)
8769 if (default_case != NULL)
8771 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8772 "by a second DEFAULT CASE at %L",
8773 &default_case->where, &cp->where);
8774 t = false;
8775 break;
8777 else
8779 default_case = cp;
8780 continue;
8784 /* Deal with single value cases and case ranges. Errors are
8785 issued from the validation function. */
8786 if (!validate_case_label_expr (cp->low, case_expr)
8787 || !validate_case_label_expr (cp->high, case_expr))
8789 t = false;
8790 break;
8793 if (type == BT_LOGICAL
8794 && ((cp->low == NULL || cp->high == NULL)
8795 || cp->low != cp->high))
8797 gfc_error ("Logical range in CASE statement at %L is not "
8798 "allowed", &cp->low->where);
8799 t = false;
8800 break;
8803 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
8805 int value;
8806 value = cp->low->value.logical == 0 ? 2 : 1;
8807 if (value & seen_logical)
8809 gfc_error ("Constant logical value in CASE statement "
8810 "is repeated at %L",
8811 &cp->low->where);
8812 t = false;
8813 break;
8815 seen_logical |= value;
8818 if (cp->low != NULL && cp->high != NULL
8819 && cp->low != cp->high
8820 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8822 if (warn_surprising)
8823 gfc_warning (OPT_Wsurprising,
8824 "Range specification at %L can never be matched",
8825 &cp->where);
8827 cp->unreachable = 1;
8828 seen_unreachable = 1;
8830 else
8832 /* If the case range can be matched, it can also overlap with
8833 other cases. To make sure it does not, we put it in a
8834 double linked list here. We sort that with a merge sort
8835 later on to detect any overlapping cases. */
8836 if (!head)
8838 head = tail = cp;
8839 head->right = head->left = NULL;
8841 else
8843 tail->right = cp;
8844 tail->right->left = tail;
8845 tail = tail->right;
8846 tail->right = NULL;
8851 /* It there was a failure in the previous case label, give up
8852 for this case label list. Continue with the next block. */
8853 if (!t)
8854 continue;
8856 /* See if any case labels that are unreachable have been seen.
8857 If so, we eliminate them. This is a bit of a kludge because
8858 the case lists for a single case statement (label) is a
8859 single forward linked lists. */
8860 if (seen_unreachable)
8862 /* Advance until the first case in the list is reachable. */
8863 while (body->ext.block.case_list != NULL
8864 && body->ext.block.case_list->unreachable)
8866 gfc_case *n = body->ext.block.case_list;
8867 body->ext.block.case_list = body->ext.block.case_list->next;
8868 n->next = NULL;
8869 gfc_free_case_list (n);
8872 /* Strip all other unreachable cases. */
8873 if (body->ext.block.case_list)
8875 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
8877 if (cp->next->unreachable)
8879 gfc_case *n = cp->next;
8880 cp->next = cp->next->next;
8881 n->next = NULL;
8882 gfc_free_case_list (n);
8889 /* See if there were overlapping cases. If the check returns NULL,
8890 there was overlap. In that case we don't do anything. If head
8891 is non-NULL, we prepend the DEFAULT case. The sorted list can
8892 then used during code generation for SELECT CASE constructs with
8893 a case expression of a CHARACTER type. */
8894 if (head)
8896 head = check_case_overlap (head);
8898 /* Prepend the default_case if it is there. */
8899 if (head != NULL && default_case)
8901 default_case->left = NULL;
8902 default_case->right = head;
8903 head->left = default_case;
8907 /* Eliminate dead blocks that may be the result if we've seen
8908 unreachable case labels for a block. */
8909 for (body = code; body && body->block; body = body->block)
8911 if (body->block->ext.block.case_list == NULL)
8913 /* Cut the unreachable block from the code chain. */
8914 gfc_code *c = body->block;
8915 body->block = c->block;
8917 /* Kill the dead block, but not the blocks below it. */
8918 c->block = NULL;
8919 gfc_free_statements (c);
8923 /* More than two cases is legal but insane for logical selects.
8924 Issue a warning for it. */
8925 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
8926 gfc_warning (OPT_Wsurprising,
8927 "Logical SELECT CASE block at %L has more that two cases",
8928 &code->loc);
8932 /* Check if a derived type is extensible. */
8934 bool
8935 gfc_type_is_extensible (gfc_symbol *sym)
8937 return !(sym->attr.is_bind_c || sym->attr.sequence
8938 || (sym->attr.is_class
8939 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
8943 static void
8944 resolve_types (gfc_namespace *ns);
8946 /* Resolve an associate-name: Resolve target and ensure the type-spec is
8947 correct as well as possibly the array-spec. */
8949 static void
8950 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
8952 gfc_expr* target;
8954 gcc_assert (sym->assoc);
8955 gcc_assert (sym->attr.flavor == FL_VARIABLE);
8957 /* If this is for SELECT TYPE, the target may not yet be set. In that
8958 case, return. Resolution will be called later manually again when
8959 this is done. */
8960 target = sym->assoc->target;
8961 if (!target)
8962 return;
8963 gcc_assert (!sym->assoc->dangling);
8965 if (resolve_target && !gfc_resolve_expr (target))
8966 return;
8968 /* For variable targets, we get some attributes from the target. */
8969 if (target->expr_type == EXPR_VARIABLE)
8971 gfc_symbol *tsym, *dsym;
8973 gcc_assert (target->symtree);
8974 tsym = target->symtree->n.sym;
8976 if (gfc_expr_attr (target).proc_pointer)
8978 gfc_error ("Associating entity %qs at %L is a procedure pointer",
8979 tsym->name, &target->where);
8980 return;
8983 if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
8984 && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
8985 && dsym->attr.flavor == FL_DERIVED)
8987 gfc_error ("Derived type %qs cannot be used as a variable at %L",
8988 tsym->name, &target->where);
8989 return;
8992 if (tsym->attr.flavor == FL_PROCEDURE)
8994 bool is_error = true;
8995 if (tsym->attr.function && tsym->result == tsym)
8996 for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
8997 if (tsym == ns->proc_name)
8999 is_error = false;
9000 break;
9002 if (is_error)
9004 gfc_error ("Associating entity %qs at %L is a procedure name",
9005 tsym->name, &target->where);
9006 return;
9010 sym->attr.asynchronous = tsym->attr.asynchronous;
9011 sym->attr.volatile_ = tsym->attr.volatile_;
9013 sym->attr.target = tsym->attr.target
9014 || gfc_expr_attr (target).pointer;
9015 if (is_subref_array (target))
9016 sym->attr.subref_array_pointer = 1;
9018 else if (target->ts.type == BT_PROCEDURE)
9020 gfc_error ("Associating selector-expression at %L yields a procedure",
9021 &target->where);
9022 return;
9025 if (target->expr_type == EXPR_NULL)
9027 gfc_error ("Selector at %L cannot be NULL()", &target->where);
9028 return;
9030 else if (target->ts.type == BT_UNKNOWN)
9032 gfc_error ("Selector at %L has no type", &target->where);
9033 return;
9036 /* Get type if this was not already set. Note that it can be
9037 some other type than the target in case this is a SELECT TYPE
9038 selector! So we must not update when the type is already there. */
9039 if (sym->ts.type == BT_UNKNOWN)
9040 sym->ts = target->ts;
9042 gcc_assert (sym->ts.type != BT_UNKNOWN);
9044 /* See if this is a valid association-to-variable. */
9045 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
9046 && !gfc_has_vector_subscript (target));
9048 /* Finally resolve if this is an array or not. */
9049 if (sym->attr.dimension && target->rank == 0)
9051 /* primary.c makes the assumption that a reference to an associate
9052 name followed by a left parenthesis is an array reference. */
9053 if (sym->ts.type != BT_CHARACTER)
9054 gfc_error ("Associate-name %qs at %L is used as array",
9055 sym->name, &sym->declared_at);
9056 sym->attr.dimension = 0;
9057 return;
9061 /* We cannot deal with class selectors that need temporaries. */
9062 if (target->ts.type == BT_CLASS
9063 && gfc_ref_needs_temporary_p (target->ref))
9065 gfc_error ("CLASS selector at %L needs a temporary which is not "
9066 "yet implemented", &target->where);
9067 return;
9070 if (target->ts.type == BT_CLASS)
9071 gfc_fix_class_refs (target);
9073 if (target->rank != 0 && !sym->attr.select_rank_temporary)
9075 gfc_array_spec *as;
9076 /* The rank may be incorrectly guessed at parsing, therefore make sure
9077 it is corrected now. */
9078 if (sym->ts.type != BT_CLASS && (!sym->as || sym->assoc->rankguessed))
9080 if (!sym->as)
9081 sym->as = gfc_get_array_spec ();
9082 as = sym->as;
9083 as->rank = target->rank;
9084 as->type = AS_DEFERRED;
9085 as->corank = gfc_get_corank (target);
9086 sym->attr.dimension = 1;
9087 if (as->corank != 0)
9088 sym->attr.codimension = 1;
9090 else if (sym->ts.type == BT_CLASS
9091 && CLASS_DATA (sym)
9092 && (!CLASS_DATA (sym)->as || sym->assoc->rankguessed))
9094 if (!CLASS_DATA (sym)->as)
9095 CLASS_DATA (sym)->as = gfc_get_array_spec ();
9096 as = CLASS_DATA (sym)->as;
9097 as->rank = target->rank;
9098 as->type = AS_DEFERRED;
9099 as->corank = gfc_get_corank (target);
9100 CLASS_DATA (sym)->attr.dimension = 1;
9101 if (as->corank != 0)
9102 CLASS_DATA (sym)->attr.codimension = 1;
9105 else if (!sym->attr.select_rank_temporary)
9107 /* target's rank is 0, but the type of the sym is still array valued,
9108 which has to be corrected. */
9109 if (sym->ts.type == BT_CLASS && sym->ts.u.derived
9110 && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
9112 gfc_array_spec *as;
9113 symbol_attribute attr;
9114 /* The associated variable's type is still the array type
9115 correct this now. */
9116 gfc_typespec *ts = &target->ts;
9117 gfc_ref *ref;
9118 gfc_component *c;
9119 for (ref = target->ref; ref != NULL; ref = ref->next)
9121 switch (ref->type)
9123 case REF_COMPONENT:
9124 ts = &ref->u.c.component->ts;
9125 break;
9126 case REF_ARRAY:
9127 if (ts->type == BT_CLASS)
9128 ts = &ts->u.derived->components->ts;
9129 break;
9130 default:
9131 break;
9134 /* Create a scalar instance of the current class type. Because the
9135 rank of a class array goes into its name, the type has to be
9136 rebuild. The alternative of (re-)setting just the attributes
9137 and as in the current type, destroys the type also in other
9138 places. */
9139 as = NULL;
9140 sym->ts = *ts;
9141 sym->ts.type = BT_CLASS;
9142 attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
9143 attr.class_ok = 0;
9144 attr.associate_var = 1;
9145 attr.dimension = attr.codimension = 0;
9146 attr.class_pointer = 1;
9147 if (!gfc_build_class_symbol (&sym->ts, &attr, &as))
9148 gcc_unreachable ();
9149 /* Make sure the _vptr is set. */
9150 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true, NULL);
9151 if (c->ts.u.derived == NULL)
9152 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived);
9153 CLASS_DATA (sym)->attr.pointer = 1;
9154 CLASS_DATA (sym)->attr.class_pointer = 1;
9155 gfc_set_sym_referenced (sym->ts.u.derived);
9156 gfc_commit_symbol (sym->ts.u.derived);
9157 /* _vptr now has the _vtab in it, change it to the _vtype. */
9158 if (c->ts.u.derived->attr.vtab)
9159 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
9160 c->ts.u.derived->ns->types_resolved = 0;
9161 resolve_types (c->ts.u.derived->ns);
9165 /* Mark this as an associate variable. */
9166 sym->attr.associate_var = 1;
9168 /* Fix up the type-spec for CHARACTER types. */
9169 if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
9171 if (!sym->ts.u.cl)
9172 sym->ts.u.cl = target->ts.u.cl;
9174 if (sym->ts.deferred && target->expr_type == EXPR_VARIABLE
9175 && target->symtree->n.sym->attr.dummy
9176 && sym->ts.u.cl == target->ts.u.cl)
9178 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9179 sym->ts.deferred = 1;
9182 if (!sym->ts.u.cl->length
9183 && !sym->ts.deferred
9184 && target->expr_type == EXPR_CONSTANT)
9186 sym->ts.u.cl->length =
9187 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
9188 target->value.character.length);
9190 else if ((!sym->ts.u.cl->length
9191 || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
9192 && target->expr_type != EXPR_VARIABLE)
9194 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9195 sym->ts.deferred = 1;
9197 /* This is reset in trans-stmt.c after the assignment
9198 of the target expression to the associate name. */
9199 sym->attr.allocatable = 1;
9203 /* If the target is a good class object, so is the associate variable. */
9204 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
9205 sym->attr.class_ok = 1;
9209 /* Ensure that SELECT TYPE expressions have the correct rank and a full
9210 array reference, where necessary. The symbols are artificial and so
9211 the dimension attribute and arrayspec can also be set. In addition,
9212 sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
9213 This is corrected here as well.*/
9215 static void
9216 fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2,
9217 int rank, gfc_ref *ref)
9219 gfc_ref *nref = (*expr1)->ref;
9220 gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
9221 gfc_symbol *sym2 = expr2 ? expr2->symtree->n.sym : NULL;
9222 (*expr1)->rank = rank;
9223 if (sym1->ts.type == BT_CLASS)
9225 if ((*expr1)->ts.type != BT_CLASS)
9226 (*expr1)->ts = sym1->ts;
9228 CLASS_DATA (sym1)->attr.dimension = 1;
9229 if (CLASS_DATA (sym1)->as == NULL && sym2)
9230 CLASS_DATA (sym1)->as
9231 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
9233 else
9235 sym1->attr.dimension = 1;
9236 if (sym1->as == NULL && sym2)
9237 sym1->as = gfc_copy_array_spec (sym2->as);
9240 for (; nref; nref = nref->next)
9241 if (nref->next == NULL)
9242 break;
9244 if (ref && nref && nref->type != REF_ARRAY)
9245 nref->next = gfc_copy_ref (ref);
9246 else if (ref && !nref)
9247 (*expr1)->ref = gfc_copy_ref (ref);
9251 static gfc_expr *
9252 build_loc_call (gfc_expr *sym_expr)
9254 gfc_expr *loc_call;
9255 loc_call = gfc_get_expr ();
9256 loc_call->expr_type = EXPR_FUNCTION;
9257 gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
9258 loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
9259 loc_call->symtree->n.sym->attr.intrinsic = 1;
9260 loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
9261 gfc_commit_symbol (loc_call->symtree->n.sym);
9262 loc_call->ts.type = BT_INTEGER;
9263 loc_call->ts.kind = gfc_index_integer_kind;
9264 loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
9265 loc_call->value.function.actual = gfc_get_actual_arglist ();
9266 loc_call->value.function.actual->expr = sym_expr;
9267 loc_call->where = sym_expr->where;
9268 return loc_call;
9271 /* Resolve a SELECT TYPE statement. */
9273 static void
9274 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
9276 gfc_symbol *selector_type;
9277 gfc_code *body, *new_st, *if_st, *tail;
9278 gfc_code *class_is = NULL, *default_case = NULL;
9279 gfc_case *c;
9280 gfc_symtree *st;
9281 char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
9282 gfc_namespace *ns;
9283 int error = 0;
9284 int rank = 0;
9285 gfc_ref* ref = NULL;
9286 gfc_expr *selector_expr = NULL;
9288 ns = code->ext.block.ns;
9289 gfc_resolve (ns);
9291 /* Check for F03:C813. */
9292 if (code->expr1->ts.type != BT_CLASS
9293 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
9295 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
9296 "at %L", &code->loc);
9297 return;
9300 if (!code->expr1->symtree->n.sym->attr.class_ok)
9301 return;
9303 if (code->expr2)
9305 gfc_ref *ref2 = NULL;
9306 for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
9307 if (ref->type == REF_COMPONENT
9308 && ref->u.c.component->ts.type == BT_CLASS)
9309 ref2 = ref;
9311 if (ref2)
9313 if (code->expr1->symtree->n.sym->attr.untyped)
9314 code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
9315 selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
9317 else
9319 if (code->expr1->symtree->n.sym->attr.untyped)
9320 code->expr1->symtree->n.sym->ts = code->expr2->ts;
9321 selector_type = CLASS_DATA (code->expr2)
9322 ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
9325 if (code->expr2->rank
9326 && code->expr1->ts.type == BT_CLASS
9327 && CLASS_DATA (code->expr1)->as)
9328 CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
9330 /* F2008: C803 The selector expression must not be coindexed. */
9331 if (gfc_is_coindexed (code->expr2))
9333 gfc_error ("Selector at %L must not be coindexed",
9334 &code->expr2->where);
9335 return;
9339 else
9341 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
9343 if (gfc_is_coindexed (code->expr1))
9345 gfc_error ("Selector at %L must not be coindexed",
9346 &code->expr1->where);
9347 return;
9351 /* Loop over TYPE IS / CLASS IS cases. */
9352 for (body = code->block; body; body = body->block)
9354 c = body->ext.block.case_list;
9356 if (!error)
9358 /* Check for repeated cases. */
9359 for (tail = code->block; tail; tail = tail->block)
9361 gfc_case *d = tail->ext.block.case_list;
9362 if (tail == body)
9363 break;
9365 if (c->ts.type == d->ts.type
9366 && ((c->ts.type == BT_DERIVED
9367 && c->ts.u.derived && d->ts.u.derived
9368 && !strcmp (c->ts.u.derived->name,
9369 d->ts.u.derived->name))
9370 || c->ts.type == BT_UNKNOWN
9371 || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9372 && c->ts.kind == d->ts.kind)))
9374 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
9375 &c->where, &d->where);
9376 return;
9381 /* Check F03:C815. */
9382 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9383 && !selector_type->attr.unlimited_polymorphic
9384 && !gfc_type_is_extensible (c->ts.u.derived))
9386 gfc_error ("Derived type %qs at %L must be extensible",
9387 c->ts.u.derived->name, &c->where);
9388 error++;
9389 continue;
9392 /* Check F03:C816. */
9393 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
9394 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
9395 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
9397 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9398 gfc_error ("Derived type %qs at %L must be an extension of %qs",
9399 c->ts.u.derived->name, &c->where, selector_type->name);
9400 else
9401 gfc_error ("Unexpected intrinsic type %qs at %L",
9402 gfc_basic_typename (c->ts.type), &c->where);
9403 error++;
9404 continue;
9407 /* Check F03:C814. */
9408 if (c->ts.type == BT_CHARACTER
9409 && (c->ts.u.cl->length != NULL || c->ts.deferred))
9411 gfc_error ("The type-spec at %L shall specify that each length "
9412 "type parameter is assumed", &c->where);
9413 error++;
9414 continue;
9417 /* Intercept the DEFAULT case. */
9418 if (c->ts.type == BT_UNKNOWN)
9420 /* Check F03:C818. */
9421 if (default_case)
9423 gfc_error ("The DEFAULT CASE at %L cannot be followed "
9424 "by a second DEFAULT CASE at %L",
9425 &default_case->ext.block.case_list->where, &c->where);
9426 error++;
9427 continue;
9430 default_case = body;
9434 if (error > 0)
9435 return;
9437 /* Transform SELECT TYPE statement to BLOCK and associate selector to
9438 target if present. If there are any EXIT statements referring to the
9439 SELECT TYPE construct, this is no problem because the gfc_code
9440 reference stays the same and EXIT is equally possible from the BLOCK
9441 it is changed to. */
9442 code->op = EXEC_BLOCK;
9443 if (code->expr2)
9445 gfc_association_list* assoc;
9447 assoc = gfc_get_association_list ();
9448 assoc->st = code->expr1->symtree;
9449 assoc->target = gfc_copy_expr (code->expr2);
9450 assoc->target->where = code->expr2->where;
9451 /* assoc->variable will be set by resolve_assoc_var. */
9453 code->ext.block.assoc = assoc;
9454 code->expr1->symtree->n.sym->assoc = assoc;
9456 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9458 else
9459 code->ext.block.assoc = NULL;
9461 /* Ensure that the selector rank and arrayspec are available to
9462 correct expressions in which they might be missing. */
9463 if (code->expr2 && code->expr2->rank)
9465 rank = code->expr2->rank;
9466 for (ref = code->expr2->ref; ref; ref = ref->next)
9467 if (ref->next == NULL)
9468 break;
9469 if (ref && ref->type == REF_ARRAY)
9470 ref = gfc_copy_ref (ref);
9472 /* Fixup expr1 if necessary. */
9473 if (rank)
9474 fixup_array_ref (&code->expr1, code->expr2, rank, ref);
9476 else if (code->expr1->rank)
9478 rank = code->expr1->rank;
9479 for (ref = code->expr1->ref; ref; ref = ref->next)
9480 if (ref->next == NULL)
9481 break;
9482 if (ref && ref->type == REF_ARRAY)
9483 ref = gfc_copy_ref (ref);
9486 /* Add EXEC_SELECT to switch on type. */
9487 new_st = gfc_get_code (code->op);
9488 new_st->expr1 = code->expr1;
9489 new_st->expr2 = code->expr2;
9490 new_st->block = code->block;
9491 code->expr1 = code->expr2 = NULL;
9492 code->block = NULL;
9493 if (!ns->code)
9494 ns->code = new_st;
9495 else
9496 ns->code->next = new_st;
9497 code = new_st;
9498 code->op = EXEC_SELECT_TYPE;
9500 /* Use the intrinsic LOC function to generate an integer expression
9501 for the vtable of the selector. Note that the rank of the selector
9502 expression has to be set to zero. */
9503 gfc_add_vptr_component (code->expr1);
9504 code->expr1->rank = 0;
9505 code->expr1 = build_loc_call (code->expr1);
9506 selector_expr = code->expr1->value.function.actual->expr;
9508 /* Loop over TYPE IS / CLASS IS cases. */
9509 for (body = code->block; body; body = body->block)
9511 gfc_symbol *vtab;
9512 gfc_expr *e;
9513 c = body->ext.block.case_list;
9515 /* Generate an index integer expression for address of the
9516 TYPE/CLASS vtable and store it in c->low. The hash expression
9517 is stored in c->high and is used to resolve intrinsic cases. */
9518 if (c->ts.type != BT_UNKNOWN)
9520 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9522 vtab = gfc_find_derived_vtab (c->ts.u.derived);
9523 gcc_assert (vtab);
9524 c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
9525 c->ts.u.derived->hash_value);
9527 else
9529 vtab = gfc_find_vtab (&c->ts);
9530 gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
9531 e = CLASS_DATA (vtab)->initializer;
9532 c->high = gfc_copy_expr (e);
9533 if (c->high->ts.kind != gfc_integer_4_kind)
9535 gfc_typespec ts;
9536 ts.kind = gfc_integer_4_kind;
9537 ts.type = BT_INTEGER;
9538 gfc_convert_type_warn (c->high, &ts, 2, 0);
9542 e = gfc_lval_expr_from_sym (vtab);
9543 c->low = build_loc_call (e);
9545 else
9546 continue;
9548 /* Associate temporary to selector. This should only be done
9549 when this case is actually true, so build a new ASSOCIATE
9550 that does precisely this here (instead of using the
9551 'global' one). */
9553 if (c->ts.type == BT_CLASS)
9554 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
9555 else if (c->ts.type == BT_DERIVED)
9556 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
9557 else if (c->ts.type == BT_CHARACTER)
9559 HOST_WIDE_INT charlen = 0;
9560 if (c->ts.u.cl && c->ts.u.cl->length
9561 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9562 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9563 snprintf (name, sizeof (name),
9564 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9565 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9567 else
9568 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
9569 c->ts.kind);
9571 st = gfc_find_symtree (ns->sym_root, name);
9572 gcc_assert (st->n.sym->assoc);
9573 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9574 st->n.sym->assoc->target->where = selector_expr->where;
9575 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
9577 gfc_add_data_component (st->n.sym->assoc->target);
9578 /* Fixup the target expression if necessary. */
9579 if (rank)
9580 fixup_array_ref (&st->n.sym->assoc->target, NULL, rank, ref);
9583 new_st = gfc_get_code (EXEC_BLOCK);
9584 new_st->ext.block.ns = gfc_build_block_ns (ns);
9585 new_st->ext.block.ns->code = body->next;
9586 body->next = new_st;
9588 /* Chain in the new list only if it is marked as dangling. Otherwise
9589 there is a CASE label overlap and this is already used. Just ignore,
9590 the error is diagnosed elsewhere. */
9591 if (st->n.sym->assoc->dangling)
9593 new_st->ext.block.assoc = st->n.sym->assoc;
9594 st->n.sym->assoc->dangling = 0;
9597 resolve_assoc_var (st->n.sym, false);
9600 /* Take out CLASS IS cases for separate treatment. */
9601 body = code;
9602 while (body && body->block)
9604 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
9606 /* Add to class_is list. */
9607 if (class_is == NULL)
9609 class_is = body->block;
9610 tail = class_is;
9612 else
9614 for (tail = class_is; tail->block; tail = tail->block) ;
9615 tail->block = body->block;
9616 tail = tail->block;
9618 /* Remove from EXEC_SELECT list. */
9619 body->block = body->block->block;
9620 tail->block = NULL;
9622 else
9623 body = body->block;
9626 if (class_is)
9628 gfc_symbol *vtab;
9630 if (!default_case)
9632 /* Add a default case to hold the CLASS IS cases. */
9633 for (tail = code; tail->block; tail = tail->block) ;
9634 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
9635 tail = tail->block;
9636 tail->ext.block.case_list = gfc_get_case ();
9637 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
9638 tail->next = NULL;
9639 default_case = tail;
9642 /* More than one CLASS IS block? */
9643 if (class_is->block)
9645 gfc_code **c1,*c2;
9646 bool swapped;
9647 /* Sort CLASS IS blocks by extension level. */
9650 swapped = false;
9651 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
9653 c2 = (*c1)->block;
9654 /* F03:C817 (check for doubles). */
9655 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
9656 == c2->ext.block.case_list->ts.u.derived->hash_value)
9658 gfc_error ("Double CLASS IS block in SELECT TYPE "
9659 "statement at %L",
9660 &c2->ext.block.case_list->where);
9661 return;
9663 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
9664 < c2->ext.block.case_list->ts.u.derived->attr.extension)
9666 /* Swap. */
9667 (*c1)->block = c2->block;
9668 c2->block = *c1;
9669 *c1 = c2;
9670 swapped = true;
9674 while (swapped);
9677 /* Generate IF chain. */
9678 if_st = gfc_get_code (EXEC_IF);
9679 new_st = if_st;
9680 for (body = class_is; body; body = body->block)
9682 new_st->block = gfc_get_code (EXEC_IF);
9683 new_st = new_st->block;
9684 /* Set up IF condition: Call _gfortran_is_extension_of. */
9685 new_st->expr1 = gfc_get_expr ();
9686 new_st->expr1->expr_type = EXPR_FUNCTION;
9687 new_st->expr1->ts.type = BT_LOGICAL;
9688 new_st->expr1->ts.kind = 4;
9689 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
9690 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
9691 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
9692 /* Set up arguments. */
9693 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
9694 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
9695 new_st->expr1->value.function.actual->expr->where = code->loc;
9696 new_st->expr1->where = code->loc;
9697 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
9698 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
9699 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
9700 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
9701 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
9702 new_st->expr1->value.function.actual->next->expr->where = code->loc;
9703 /* Set up types in formal arg list. */
9704 new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
9705 new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
9706 new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
9707 new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
9709 new_st->next = body->next;
9711 if (default_case->next)
9713 new_st->block = gfc_get_code (EXEC_IF);
9714 new_st = new_st->block;
9715 new_st->next = default_case->next;
9718 /* Replace CLASS DEFAULT code by the IF chain. */
9719 default_case->next = if_st;
9722 /* Resolve the internal code. This cannot be done earlier because
9723 it requires that the sym->assoc of selectors is set already. */
9724 gfc_current_ns = ns;
9725 gfc_resolve_blocks (code->block, gfc_current_ns);
9726 gfc_current_ns = old_ns;
9728 if (ref)
9729 free (ref);
9733 /* Resolve a SELECT RANK statement. */
9735 static void
9736 resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
9738 gfc_namespace *ns;
9739 gfc_code *body, *new_st, *tail;
9740 gfc_case *c;
9741 char tname[GFC_MAX_SYMBOL_LEN + 7];
9742 char name[2 * GFC_MAX_SYMBOL_LEN];
9743 gfc_symtree *st;
9744 gfc_expr *selector_expr = NULL;
9745 int case_value;
9746 HOST_WIDE_INT charlen = 0;
9748 ns = code->ext.block.ns;
9749 gfc_resolve (ns);
9751 code->op = EXEC_BLOCK;
9752 if (code->expr2)
9754 gfc_association_list* assoc;
9756 assoc = gfc_get_association_list ();
9757 assoc->st = code->expr1->symtree;
9758 assoc->target = gfc_copy_expr (code->expr2);
9759 assoc->target->where = code->expr2->where;
9760 /* assoc->variable will be set by resolve_assoc_var. */
9762 code->ext.block.assoc = assoc;
9763 code->expr1->symtree->n.sym->assoc = assoc;
9765 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9767 else
9768 code->ext.block.assoc = NULL;
9770 /* Loop over RANK cases. Note that returning on the errors causes a
9771 cascade of further errors because the case blocks do not compile
9772 correctly. */
9773 for (body = code->block; body; body = body->block)
9775 c = body->ext.block.case_list;
9776 if (c->low)
9777 case_value = (int) mpz_get_si (c->low->value.integer);
9778 else
9779 case_value = -2;
9781 /* Check for repeated cases. */
9782 for (tail = code->block; tail; tail = tail->block)
9784 gfc_case *d = tail->ext.block.case_list;
9785 int case_value2;
9787 if (tail == body)
9788 break;
9790 /* Check F2018: C1153. */
9791 if (!c->low && !d->low)
9792 gfc_error ("RANK DEFAULT at %L is repeated at %L",
9793 &c->where, &d->where);
9795 if (!c->low || !d->low)
9796 continue;
9798 /* Check F2018: C1153. */
9799 case_value2 = (int) mpz_get_si (d->low->value.integer);
9800 if ((case_value == case_value2) && case_value == -1)
9801 gfc_error ("RANK (*) at %L is repeated at %L",
9802 &c->where, &d->where);
9803 else if (case_value == case_value2)
9804 gfc_error ("RANK (%i) at %L is repeated at %L",
9805 case_value, &c->where, &d->where);
9808 if (!c->low)
9809 continue;
9811 /* Check F2018: C1155. */
9812 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9813 || gfc_expr_attr (code->expr1).pointer))
9814 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9815 "allocatable selector at %L", &c->where, &code->expr1->where);
9817 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9818 || gfc_expr_attr (code->expr1).pointer))
9819 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9820 "allocatable selector at %L", &c->where, &code->expr1->where);
9823 /* Add EXEC_SELECT to switch on rank. */
9824 new_st = gfc_get_code (code->op);
9825 new_st->expr1 = code->expr1;
9826 new_st->expr2 = code->expr2;
9827 new_st->block = code->block;
9828 code->expr1 = code->expr2 = NULL;
9829 code->block = NULL;
9830 if (!ns->code)
9831 ns->code = new_st;
9832 else
9833 ns->code->next = new_st;
9834 code = new_st;
9835 code->op = EXEC_SELECT_RANK;
9837 selector_expr = code->expr1;
9839 /* Loop over SELECT RANK cases. */
9840 for (body = code->block; body; body = body->block)
9842 c = body->ext.block.case_list;
9843 int case_value;
9845 /* Pass on the default case. */
9846 if (c->low == NULL)
9847 continue;
9849 /* Associate temporary to selector. This should only be done
9850 when this case is actually true, so build a new ASSOCIATE
9851 that does precisely this here (instead of using the
9852 'global' one). */
9853 if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
9854 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9855 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9857 if (c->ts.type == BT_CLASS)
9858 sprintf (tname, "class_%s", c->ts.u.derived->name);
9859 else if (c->ts.type == BT_DERIVED)
9860 sprintf (tname, "type_%s", c->ts.u.derived->name);
9861 else if (c->ts.type != BT_CHARACTER)
9862 sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
9863 else
9864 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9865 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9867 case_value = (int) mpz_get_si (c->low->value.integer);
9868 if (case_value >= 0)
9869 sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
9870 else
9871 sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
9873 st = gfc_find_symtree (ns->sym_root, name);
9874 gcc_assert (st->n.sym->assoc);
9876 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9877 st->n.sym->assoc->target->where = selector_expr->where;
9879 new_st = gfc_get_code (EXEC_BLOCK);
9880 new_st->ext.block.ns = gfc_build_block_ns (ns);
9881 new_st->ext.block.ns->code = body->next;
9882 body->next = new_st;
9884 /* Chain in the new list only if it is marked as dangling. Otherwise
9885 there is a CASE label overlap and this is already used. Just ignore,
9886 the error is diagnosed elsewhere. */
9887 if (st->n.sym->assoc->dangling)
9889 new_st->ext.block.assoc = st->n.sym->assoc;
9890 st->n.sym->assoc->dangling = 0;
9893 resolve_assoc_var (st->n.sym, false);
9896 gfc_current_ns = ns;
9897 gfc_resolve_blocks (code->block, gfc_current_ns);
9898 gfc_current_ns = old_ns;
9902 /* Resolve a transfer statement. This is making sure that:
9903 -- a derived type being transferred has only non-pointer components
9904 -- a derived type being transferred doesn't have private components, unless
9905 it's being transferred from the module where the type was defined
9906 -- we're not trying to transfer a whole assumed size array. */
9908 static void
9909 resolve_transfer (gfc_code *code)
9911 gfc_symbol *sym, *derived;
9912 gfc_ref *ref;
9913 gfc_expr *exp;
9914 bool write = false;
9915 bool formatted = false;
9916 gfc_dt *dt = code->ext.dt;
9917 gfc_symbol *dtio_sub = NULL;
9919 exp = code->expr1;
9921 while (exp != NULL && exp->expr_type == EXPR_OP
9922 && exp->value.op.op == INTRINSIC_PARENTHESES)
9923 exp = exp->value.op.op1;
9925 if (exp && exp->expr_type == EXPR_NULL
9926 && code->ext.dt)
9928 gfc_error ("Invalid context for NULL () intrinsic at %L",
9929 &exp->where);
9930 return;
9933 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
9934 && exp->expr_type != EXPR_FUNCTION
9935 && exp->expr_type != EXPR_STRUCTURE))
9936 return;
9938 /* If we are reading, the variable will be changed. Note that
9939 code->ext.dt may be NULL if the TRANSFER is related to
9940 an INQUIRE statement -- but in this case, we are not reading, either. */
9941 if (dt && dt->dt_io_kind->value.iokind == M_READ
9942 && !gfc_check_vardef_context (exp, false, false, false,
9943 _("item in READ")))
9944 return;
9946 const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
9947 || exp->expr_type == EXPR_FUNCTION
9948 ? &exp->ts : &exp->symtree->n.sym->ts;
9950 /* Go to actual component transferred. */
9951 for (ref = exp->ref; ref; ref = ref->next)
9952 if (ref->type == REF_COMPONENT)
9953 ts = &ref->u.c.component->ts;
9955 if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
9956 && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
9958 derived = ts->u.derived;
9960 /* Determine when to use the formatted DTIO procedure. */
9961 if (dt && (dt->format_expr || dt->format_label))
9962 formatted = true;
9964 write = dt->dt_io_kind->value.iokind == M_WRITE
9965 || dt->dt_io_kind->value.iokind == M_PRINT;
9966 dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
9968 if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
9970 dt->udtio = exp;
9971 sym = exp->symtree->n.sym->ns->proc_name;
9972 /* Check to see if this is a nested DTIO call, with the
9973 dummy as the io-list object. */
9974 if (sym && sym == dtio_sub && sym->formal
9975 && sym->formal->sym == exp->symtree->n.sym
9976 && exp->ref == NULL)
9978 if (!sym->attr.recursive)
9980 gfc_error ("DTIO %s procedure at %L must be recursive",
9981 sym->name, &sym->declared_at);
9982 return;
9988 if (ts->type == BT_CLASS && dtio_sub == NULL)
9990 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
9991 "it is processed by a defined input/output procedure",
9992 &code->loc);
9993 return;
9996 if (ts->type == BT_DERIVED)
9998 /* Check that transferred derived type doesn't contain POINTER
9999 components unless it is processed by a defined input/output
10000 procedure". */
10001 if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
10003 gfc_error ("Data transfer element at %L cannot have POINTER "
10004 "components unless it is processed by a defined "
10005 "input/output procedure", &code->loc);
10006 return;
10009 /* F08:C935. */
10010 if (ts->u.derived->attr.proc_pointer_comp)
10012 gfc_error ("Data transfer element at %L cannot have "
10013 "procedure pointer components", &code->loc);
10014 return;
10017 if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
10019 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
10020 "components unless it is processed by a defined "
10021 "input/output procedure", &code->loc);
10022 return;
10025 /* C_PTR and C_FUNPTR have private components which means they cannot
10026 be printed. However, if -std=gnu and not -pedantic, allow
10027 the component to be printed to help debugging. */
10028 if (ts->u.derived->ts.f90_type == BT_VOID)
10030 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
10031 "cannot have PRIVATE components", &code->loc))
10032 return;
10034 else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
10036 gfc_error ("Data transfer element at %L cannot have "
10037 "PRIVATE components unless it is processed by "
10038 "a defined input/output procedure", &code->loc);
10039 return;
10043 if (exp->expr_type == EXPR_STRUCTURE)
10044 return;
10046 sym = exp->symtree->n.sym;
10048 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
10049 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
10051 gfc_error ("Data transfer element at %L cannot be a full reference to "
10052 "an assumed-size array", &code->loc);
10053 return;
10058 /*********** Toplevel code resolution subroutines ***********/
10060 /* Find the set of labels that are reachable from this block. We also
10061 record the last statement in each block. */
10063 static void
10064 find_reachable_labels (gfc_code *block)
10066 gfc_code *c;
10068 if (!block)
10069 return;
10071 cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
10073 /* Collect labels in this block. We don't keep those corresponding
10074 to END {IF|SELECT}, these are checked in resolve_branch by going
10075 up through the code_stack. */
10076 for (c = block; c; c = c->next)
10078 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
10079 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
10082 /* Merge with labels from parent block. */
10083 if (cs_base->prev)
10085 gcc_assert (cs_base->prev->reachable_labels);
10086 bitmap_ior_into (cs_base->reachable_labels,
10087 cs_base->prev->reachable_labels);
10092 static void
10093 resolve_lock_unlock_event (gfc_code *code)
10095 if (code->expr1->expr_type == EXPR_FUNCTION
10096 && code->expr1->value.function.isym
10097 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10098 remove_caf_get_intrinsic (code->expr1);
10100 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
10101 && (code->expr1->ts.type != BT_DERIVED
10102 || code->expr1->expr_type != EXPR_VARIABLE
10103 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
10104 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
10105 || code->expr1->rank != 0
10106 || (!gfc_is_coarray (code->expr1) &&
10107 !gfc_is_coindexed (code->expr1))))
10108 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
10109 &code->expr1->where);
10110 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
10111 && (code->expr1->ts.type != BT_DERIVED
10112 || code->expr1->expr_type != EXPR_VARIABLE
10113 || code->expr1->ts.u.derived->from_intmod
10114 != INTMOD_ISO_FORTRAN_ENV
10115 || code->expr1->ts.u.derived->intmod_sym_id
10116 != ISOFORTRAN_EVENT_TYPE
10117 || code->expr1->rank != 0))
10118 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
10119 &code->expr1->where);
10120 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
10121 && !gfc_is_coindexed (code->expr1))
10122 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
10123 &code->expr1->where);
10124 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
10125 gfc_error ("Event variable argument at %L must be a coarray but not "
10126 "coindexed", &code->expr1->where);
10128 /* Check STAT. */
10129 if (code->expr2
10130 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10131 || code->expr2->expr_type != EXPR_VARIABLE))
10132 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10133 &code->expr2->where);
10135 if (code->expr2
10136 && !gfc_check_vardef_context (code->expr2, false, false, false,
10137 _("STAT variable")))
10138 return;
10140 /* Check ERRMSG. */
10141 if (code->expr3
10142 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10143 || code->expr3->expr_type != EXPR_VARIABLE))
10144 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10145 &code->expr3->where);
10147 if (code->expr3
10148 && !gfc_check_vardef_context (code->expr3, false, false, false,
10149 _("ERRMSG variable")))
10150 return;
10152 /* Check for LOCK the ACQUIRED_LOCK. */
10153 if (code->op != EXEC_EVENT_WAIT && code->expr4
10154 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
10155 || code->expr4->expr_type != EXPR_VARIABLE))
10156 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
10157 "variable", &code->expr4->where);
10159 if (code->op != EXEC_EVENT_WAIT && code->expr4
10160 && !gfc_check_vardef_context (code->expr4, false, false, false,
10161 _("ACQUIRED_LOCK variable")))
10162 return;
10164 /* Check for EVENT WAIT the UNTIL_COUNT. */
10165 if (code->op == EXEC_EVENT_WAIT && code->expr4)
10167 if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
10168 || code->expr4->rank != 0)
10169 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
10170 "expression", &code->expr4->where);
10175 static void
10176 resolve_critical (gfc_code *code)
10178 gfc_symtree *symtree;
10179 gfc_symbol *lock_type;
10180 char name[GFC_MAX_SYMBOL_LEN];
10181 static int serial = 0;
10183 if (flag_coarray != GFC_FCOARRAY_LIB)
10184 return;
10186 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10187 GFC_PREFIX ("lock_type"));
10188 if (symtree)
10189 lock_type = symtree->n.sym;
10190 else
10192 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
10193 false) != 0)
10194 gcc_unreachable ();
10195 lock_type = symtree->n.sym;
10196 lock_type->attr.flavor = FL_DERIVED;
10197 lock_type->attr.zero_comp = 1;
10198 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
10199 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
10202 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
10203 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
10204 gcc_unreachable ();
10206 code->resolved_sym = symtree->n.sym;
10207 symtree->n.sym->attr.flavor = FL_VARIABLE;
10208 symtree->n.sym->attr.referenced = 1;
10209 symtree->n.sym->attr.artificial = 1;
10210 symtree->n.sym->attr.codimension = 1;
10211 symtree->n.sym->ts.type = BT_DERIVED;
10212 symtree->n.sym->ts.u.derived = lock_type;
10213 symtree->n.sym->as = gfc_get_array_spec ();
10214 symtree->n.sym->as->corank = 1;
10215 symtree->n.sym->as->type = AS_EXPLICIT;
10216 symtree->n.sym->as->cotype = AS_EXPLICIT;
10217 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
10218 NULL, 1);
10219 gfc_commit_symbols();
10223 static void
10224 resolve_sync (gfc_code *code)
10226 /* Check imageset. The * case matches expr1 == NULL. */
10227 if (code->expr1)
10229 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
10230 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
10231 "INTEGER expression", &code->expr1->where);
10232 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
10233 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
10234 gfc_error ("Imageset argument at %L must between 1 and num_images()",
10235 &code->expr1->where);
10236 else if (code->expr1->expr_type == EXPR_ARRAY
10237 && gfc_simplify_expr (code->expr1, 0))
10239 gfc_constructor *cons;
10240 cons = gfc_constructor_first (code->expr1->value.constructor);
10241 for (; cons; cons = gfc_constructor_next (cons))
10242 if (cons->expr->expr_type == EXPR_CONSTANT
10243 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
10244 gfc_error ("Imageset argument at %L must between 1 and "
10245 "num_images()", &cons->expr->where);
10249 /* Check STAT. */
10250 gfc_resolve_expr (code->expr2);
10251 if (code->expr2)
10253 if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
10254 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10255 &code->expr2->where);
10256 else
10257 gfc_check_vardef_context (code->expr2, false, false, false,
10258 _("STAT variable"));
10261 /* Check ERRMSG. */
10262 gfc_resolve_expr (code->expr3);
10263 if (code->expr3)
10265 if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
10266 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10267 &code->expr3->where);
10268 else
10269 gfc_check_vardef_context (code->expr3, false, false, false,
10270 _("ERRMSG variable"));
10275 /* Given a branch to a label, see if the branch is conforming.
10276 The code node describes where the branch is located. */
10278 static void
10279 resolve_branch (gfc_st_label *label, gfc_code *code)
10281 code_stack *stack;
10283 if (label == NULL)
10284 return;
10286 /* Step one: is this a valid branching target? */
10288 if (label->defined == ST_LABEL_UNKNOWN)
10290 gfc_error ("Label %d referenced at %L is never defined", label->value,
10291 &code->loc);
10292 return;
10295 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
10297 gfc_error ("Statement at %L is not a valid branch target statement "
10298 "for the branch statement at %L", &label->where, &code->loc);
10299 return;
10302 /* Step two: make sure this branch is not a branch to itself ;-) */
10304 if (code->here == label)
10306 gfc_warning (0,
10307 "Branch at %L may result in an infinite loop", &code->loc);
10308 return;
10311 /* Step three: See if the label is in the same block as the
10312 branching statement. The hard work has been done by setting up
10313 the bitmap reachable_labels. */
10315 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
10317 /* Check now whether there is a CRITICAL construct; if so, check
10318 whether the label is still visible outside of the CRITICAL block,
10319 which is invalid. */
10320 for (stack = cs_base; stack; stack = stack->prev)
10322 if (stack->current->op == EXEC_CRITICAL
10323 && bitmap_bit_p (stack->reachable_labels, label->value))
10324 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
10325 "label at %L", &code->loc, &label->where);
10326 else if (stack->current->op == EXEC_DO_CONCURRENT
10327 && bitmap_bit_p (stack->reachable_labels, label->value))
10328 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
10329 "for label at %L", &code->loc, &label->where);
10332 return;
10335 /* Step four: If we haven't found the label in the bitmap, it may
10336 still be the label of the END of the enclosing block, in which
10337 case we find it by going up the code_stack. */
10339 for (stack = cs_base; stack; stack = stack->prev)
10341 if (stack->current->next && stack->current->next->here == label)
10342 break;
10343 if (stack->current->op == EXEC_CRITICAL)
10345 /* Note: A label at END CRITICAL does not leave the CRITICAL
10346 construct as END CRITICAL is still part of it. */
10347 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
10348 " at %L", &code->loc, &label->where);
10349 return;
10351 else if (stack->current->op == EXEC_DO_CONCURRENT)
10353 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
10354 "label at %L", &code->loc, &label->where);
10355 return;
10359 if (stack)
10361 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
10362 return;
10365 /* The label is not in an enclosing block, so illegal. This was
10366 allowed in Fortran 66, so we allow it as extension. No
10367 further checks are necessary in this case. */
10368 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
10369 "as the GOTO statement at %L", &label->where,
10370 &code->loc);
10371 return;
10375 /* Check whether EXPR1 has the same shape as EXPR2. */
10377 static bool
10378 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
10380 mpz_t shape[GFC_MAX_DIMENSIONS];
10381 mpz_t shape2[GFC_MAX_DIMENSIONS];
10382 bool result = false;
10383 int i;
10385 /* Compare the rank. */
10386 if (expr1->rank != expr2->rank)
10387 return result;
10389 /* Compare the size of each dimension. */
10390 for (i=0; i<expr1->rank; i++)
10392 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
10393 goto ignore;
10395 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
10396 goto ignore;
10398 if (mpz_cmp (shape[i], shape2[i]))
10399 goto over;
10402 /* When either of the two expression is an assumed size array, we
10403 ignore the comparison of dimension sizes. */
10404 ignore:
10405 result = true;
10407 over:
10408 gfc_clear_shape (shape, i);
10409 gfc_clear_shape (shape2, i);
10410 return result;
10414 /* Check whether a WHERE assignment target or a WHERE mask expression
10415 has the same shape as the outmost WHERE mask expression. */
10417 static void
10418 resolve_where (gfc_code *code, gfc_expr *mask)
10420 gfc_code *cblock;
10421 gfc_code *cnext;
10422 gfc_expr *e = NULL;
10424 cblock = code->block;
10426 /* Store the first WHERE mask-expr of the WHERE statement or construct.
10427 In case of nested WHERE, only the outmost one is stored. */
10428 if (mask == NULL) /* outmost WHERE */
10429 e = cblock->expr1;
10430 else /* inner WHERE */
10431 e = mask;
10433 while (cblock)
10435 if (cblock->expr1)
10437 /* Check if the mask-expr has a consistent shape with the
10438 outmost WHERE mask-expr. */
10439 if (!resolve_where_shape (cblock->expr1, e))
10440 gfc_error ("WHERE mask at %L has inconsistent shape",
10441 &cblock->expr1->where);
10444 /* the assignment statement of a WHERE statement, or the first
10445 statement in where-body-construct of a WHERE construct */
10446 cnext = cblock->next;
10447 while (cnext)
10449 switch (cnext->op)
10451 /* WHERE assignment statement */
10452 case EXEC_ASSIGN:
10454 /* Check shape consistent for WHERE assignment target. */
10455 if (e && !resolve_where_shape (cnext->expr1, e))
10456 gfc_error ("WHERE assignment target at %L has "
10457 "inconsistent shape", &cnext->expr1->where);
10458 break;
10461 case EXEC_ASSIGN_CALL:
10462 resolve_call (cnext);
10463 if (!cnext->resolved_sym->attr.elemental)
10464 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10465 &cnext->ext.actual->expr->where);
10466 break;
10468 /* WHERE or WHERE construct is part of a where-body-construct */
10469 case EXEC_WHERE:
10470 resolve_where (cnext, e);
10471 break;
10473 default:
10474 gfc_error ("Unsupported statement inside WHERE at %L",
10475 &cnext->loc);
10477 /* the next statement within the same where-body-construct */
10478 cnext = cnext->next;
10480 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10481 cblock = cblock->block;
10486 /* Resolve assignment in FORALL construct.
10487 NVAR is the number of FORALL index variables, and VAR_EXPR records the
10488 FORALL index variables. */
10490 static void
10491 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
10493 int n;
10495 for (n = 0; n < nvar; n++)
10497 gfc_symbol *forall_index;
10499 forall_index = var_expr[n]->symtree->n.sym;
10501 /* Check whether the assignment target is one of the FORALL index
10502 variable. */
10503 if ((code->expr1->expr_type == EXPR_VARIABLE)
10504 && (code->expr1->symtree->n.sym == forall_index))
10505 gfc_error ("Assignment to a FORALL index variable at %L",
10506 &code->expr1->where);
10507 else
10509 /* If one of the FORALL index variables doesn't appear in the
10510 assignment variable, then there could be a many-to-one
10511 assignment. Emit a warning rather than an error because the
10512 mask could be resolving this problem. */
10513 if (!find_forall_index (code->expr1, forall_index, 0))
10514 gfc_warning (0, "The FORALL with index %qs is not used on the "
10515 "left side of the assignment at %L and so might "
10516 "cause multiple assignment to this object",
10517 var_expr[n]->symtree->name, &code->expr1->where);
10523 /* Resolve WHERE statement in FORALL construct. */
10525 static void
10526 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
10527 gfc_expr **var_expr)
10529 gfc_code *cblock;
10530 gfc_code *cnext;
10532 cblock = code->block;
10533 while (cblock)
10535 /* the assignment statement of a WHERE statement, or the first
10536 statement in where-body-construct of a WHERE construct */
10537 cnext = cblock->next;
10538 while (cnext)
10540 switch (cnext->op)
10542 /* WHERE assignment statement */
10543 case EXEC_ASSIGN:
10544 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
10545 break;
10547 /* WHERE operator assignment statement */
10548 case EXEC_ASSIGN_CALL:
10549 resolve_call (cnext);
10550 if (!cnext->resolved_sym->attr.elemental)
10551 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10552 &cnext->ext.actual->expr->where);
10553 break;
10555 /* WHERE or WHERE construct is part of a where-body-construct */
10556 case EXEC_WHERE:
10557 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
10558 break;
10560 default:
10561 gfc_error ("Unsupported statement inside WHERE at %L",
10562 &cnext->loc);
10564 /* the next statement within the same where-body-construct */
10565 cnext = cnext->next;
10567 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10568 cblock = cblock->block;
10573 /* Traverse the FORALL body to check whether the following errors exist:
10574 1. For assignment, check if a many-to-one assignment happens.
10575 2. For WHERE statement, check the WHERE body to see if there is any
10576 many-to-one assignment. */
10578 static void
10579 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
10581 gfc_code *c;
10583 c = code->block->next;
10584 while (c)
10586 switch (c->op)
10588 case EXEC_ASSIGN:
10589 case EXEC_POINTER_ASSIGN:
10590 gfc_resolve_assign_in_forall (c, nvar, var_expr);
10591 break;
10593 case EXEC_ASSIGN_CALL:
10594 resolve_call (c);
10595 break;
10597 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
10598 there is no need to handle it here. */
10599 case EXEC_FORALL:
10600 break;
10601 case EXEC_WHERE:
10602 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
10603 break;
10604 default:
10605 break;
10607 /* The next statement in the FORALL body. */
10608 c = c->next;
10613 /* Counts the number of iterators needed inside a forall construct, including
10614 nested forall constructs. This is used to allocate the needed memory
10615 in gfc_resolve_forall. */
10617 static int
10618 gfc_count_forall_iterators (gfc_code *code)
10620 int max_iters, sub_iters, current_iters;
10621 gfc_forall_iterator *fa;
10623 gcc_assert(code->op == EXEC_FORALL);
10624 max_iters = 0;
10625 current_iters = 0;
10627 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10628 current_iters ++;
10630 code = code->block->next;
10632 while (code)
10634 if (code->op == EXEC_FORALL)
10636 sub_iters = gfc_count_forall_iterators (code);
10637 if (sub_iters > max_iters)
10638 max_iters = sub_iters;
10640 code = code->next;
10643 return current_iters + max_iters;
10647 /* Given a FORALL construct, first resolve the FORALL iterator, then call
10648 gfc_resolve_forall_body to resolve the FORALL body. */
10650 static void
10651 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
10653 static gfc_expr **var_expr;
10654 static int total_var = 0;
10655 static int nvar = 0;
10656 int i, old_nvar, tmp;
10657 gfc_forall_iterator *fa;
10659 old_nvar = nvar;
10661 if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
10662 return;
10664 /* Start to resolve a FORALL construct */
10665 if (forall_save == 0)
10667 /* Count the total number of FORALL indices in the nested FORALL
10668 construct in order to allocate the VAR_EXPR with proper size. */
10669 total_var = gfc_count_forall_iterators (code);
10671 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
10672 var_expr = XCNEWVEC (gfc_expr *, total_var);
10675 /* The information about FORALL iterator, including FORALL indices start, end
10676 and stride. An outer FORALL indice cannot appear in start, end or stride. */
10677 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10679 /* Fortran 20008: C738 (R753). */
10680 if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
10682 gfc_error ("FORALL index-name at %L must be a scalar variable "
10683 "of type integer", &fa->var->where);
10684 continue;
10687 /* Check if any outer FORALL index name is the same as the current
10688 one. */
10689 for (i = 0; i < nvar; i++)
10691 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
10692 gfc_error ("An outer FORALL construct already has an index "
10693 "with this name %L", &fa->var->where);
10696 /* Record the current FORALL index. */
10697 var_expr[nvar] = gfc_copy_expr (fa->var);
10699 nvar++;
10701 /* No memory leak. */
10702 gcc_assert (nvar <= total_var);
10705 /* Resolve the FORALL body. */
10706 gfc_resolve_forall_body (code, nvar, var_expr);
10708 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
10709 gfc_resolve_blocks (code->block, ns);
10711 tmp = nvar;
10712 nvar = old_nvar;
10713 /* Free only the VAR_EXPRs allocated in this frame. */
10714 for (i = nvar; i < tmp; i++)
10715 gfc_free_expr (var_expr[i]);
10717 if (nvar == 0)
10719 /* We are in the outermost FORALL construct. */
10720 gcc_assert (forall_save == 0);
10722 /* VAR_EXPR is not needed any more. */
10723 free (var_expr);
10724 total_var = 0;
10729 /* Resolve a BLOCK construct statement. */
10731 static void
10732 resolve_block_construct (gfc_code* code)
10734 /* Resolve the BLOCK's namespace. */
10735 gfc_resolve (code->ext.block.ns);
10737 /* For an ASSOCIATE block, the associations (and their targets) are already
10738 resolved during resolve_symbol. */
10742 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
10743 DO code nodes. */
10745 void
10746 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
10748 bool t;
10750 for (; b; b = b->block)
10752 t = gfc_resolve_expr (b->expr1);
10753 if (!gfc_resolve_expr (b->expr2))
10754 t = false;
10756 switch (b->op)
10758 case EXEC_IF:
10759 if (t && b->expr1 != NULL
10760 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
10761 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10762 &b->expr1->where);
10763 break;
10765 case EXEC_WHERE:
10766 if (t
10767 && b->expr1 != NULL
10768 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
10769 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
10770 &b->expr1->where);
10771 break;
10773 case EXEC_GOTO:
10774 resolve_branch (b->label1, b);
10775 break;
10777 case EXEC_BLOCK:
10778 resolve_block_construct (b);
10779 break;
10781 case EXEC_SELECT:
10782 case EXEC_SELECT_TYPE:
10783 case EXEC_SELECT_RANK:
10784 case EXEC_FORALL:
10785 case EXEC_DO:
10786 case EXEC_DO_WHILE:
10787 case EXEC_DO_CONCURRENT:
10788 case EXEC_CRITICAL:
10789 case EXEC_READ:
10790 case EXEC_WRITE:
10791 case EXEC_IOLENGTH:
10792 case EXEC_WAIT:
10793 break;
10795 case EXEC_OMP_ATOMIC:
10796 case EXEC_OACC_ATOMIC:
10798 /* Verify this before calling gfc_resolve_code, which might
10799 change it. */
10800 gcc_assert (b->next && b->next->op == EXEC_ASSIGN);
10801 gcc_assert ((!b->ext.omp_clauses->capture
10802 && b->next->next == NULL)
10803 || (b->ext.omp_clauses->capture
10804 && b->next->next != NULL
10805 && b->next->next->op == EXEC_ASSIGN
10806 && b->next->next->next == NULL));
10808 break;
10810 case EXEC_OACC_PARALLEL_LOOP:
10811 case EXEC_OACC_PARALLEL:
10812 case EXEC_OACC_KERNELS_LOOP:
10813 case EXEC_OACC_KERNELS:
10814 case EXEC_OACC_SERIAL_LOOP:
10815 case EXEC_OACC_SERIAL:
10816 case EXEC_OACC_DATA:
10817 case EXEC_OACC_HOST_DATA:
10818 case EXEC_OACC_LOOP:
10819 case EXEC_OACC_UPDATE:
10820 case EXEC_OACC_WAIT:
10821 case EXEC_OACC_CACHE:
10822 case EXEC_OACC_ENTER_DATA:
10823 case EXEC_OACC_EXIT_DATA:
10824 case EXEC_OACC_ROUTINE:
10825 case EXEC_OMP_CRITICAL:
10826 case EXEC_OMP_DISTRIBUTE:
10827 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10828 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10829 case EXEC_OMP_DISTRIBUTE_SIMD:
10830 case EXEC_OMP_DO:
10831 case EXEC_OMP_DO_SIMD:
10832 case EXEC_OMP_ERROR:
10833 case EXEC_OMP_LOOP:
10834 case EXEC_OMP_MASKED:
10835 case EXEC_OMP_MASKED_TASKLOOP:
10836 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
10837 case EXEC_OMP_MASTER:
10838 case EXEC_OMP_MASTER_TASKLOOP:
10839 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
10840 case EXEC_OMP_ORDERED:
10841 case EXEC_OMP_PARALLEL:
10842 case EXEC_OMP_PARALLEL_DO:
10843 case EXEC_OMP_PARALLEL_DO_SIMD:
10844 case EXEC_OMP_PARALLEL_LOOP:
10845 case EXEC_OMP_PARALLEL_MASKED:
10846 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
10847 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
10848 case EXEC_OMP_PARALLEL_MASTER:
10849 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
10850 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
10851 case EXEC_OMP_PARALLEL_SECTIONS:
10852 case EXEC_OMP_PARALLEL_WORKSHARE:
10853 case EXEC_OMP_SECTIONS:
10854 case EXEC_OMP_SIMD:
10855 case EXEC_OMP_SCOPE:
10856 case EXEC_OMP_SINGLE:
10857 case EXEC_OMP_TARGET:
10858 case EXEC_OMP_TARGET_DATA:
10859 case EXEC_OMP_TARGET_ENTER_DATA:
10860 case EXEC_OMP_TARGET_EXIT_DATA:
10861 case EXEC_OMP_TARGET_PARALLEL:
10862 case EXEC_OMP_TARGET_PARALLEL_DO:
10863 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
10864 case EXEC_OMP_TARGET_PARALLEL_LOOP:
10865 case EXEC_OMP_TARGET_SIMD:
10866 case EXEC_OMP_TARGET_TEAMS:
10867 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10868 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10869 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10870 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10871 case EXEC_OMP_TARGET_TEAMS_LOOP:
10872 case EXEC_OMP_TARGET_UPDATE:
10873 case EXEC_OMP_TASK:
10874 case EXEC_OMP_TASKGROUP:
10875 case EXEC_OMP_TASKLOOP:
10876 case EXEC_OMP_TASKLOOP_SIMD:
10877 case EXEC_OMP_TASKWAIT:
10878 case EXEC_OMP_TASKYIELD:
10879 case EXEC_OMP_TEAMS:
10880 case EXEC_OMP_TEAMS_DISTRIBUTE:
10881 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10882 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10883 case EXEC_OMP_TEAMS_LOOP:
10884 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10885 case EXEC_OMP_WORKSHARE:
10886 break;
10888 default:
10889 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
10892 gfc_resolve_code (b->next, ns);
10897 /* Does everything to resolve an ordinary assignment. Returns true
10898 if this is an interface assignment. */
10899 static bool
10900 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
10902 bool rval = false;
10903 gfc_expr *lhs;
10904 gfc_expr *rhs;
10905 int n;
10906 gfc_ref *ref;
10907 symbol_attribute attr;
10909 if (gfc_extend_assign (code, ns))
10911 gfc_expr** rhsptr;
10913 if (code->op == EXEC_ASSIGN_CALL)
10915 lhs = code->ext.actual->expr;
10916 rhsptr = &code->ext.actual->next->expr;
10918 else
10920 gfc_actual_arglist* args;
10921 gfc_typebound_proc* tbp;
10923 gcc_assert (code->op == EXEC_COMPCALL);
10925 args = code->expr1->value.compcall.actual;
10926 lhs = args->expr;
10927 rhsptr = &args->next->expr;
10929 tbp = code->expr1->value.compcall.tbp;
10930 gcc_assert (!tbp->is_generic);
10933 /* Make a temporary rhs when there is a default initializer
10934 and rhs is the same symbol as the lhs. */
10935 if ((*rhsptr)->expr_type == EXPR_VARIABLE
10936 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
10937 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
10938 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
10939 *rhsptr = gfc_get_parentheses (*rhsptr);
10941 return true;
10944 lhs = code->expr1;
10945 rhs = code->expr2;
10947 if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
10948 && rhs->ts.type == BT_CHARACTER
10949 && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
10951 /* Use of -fdec-char-conversions allows assignment of character data
10952 to non-character variables. This not permited for nonconstant
10953 strings. */
10954 gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
10955 gfc_typename (lhs), &rhs->where);
10956 return false;
10959 /* Handle the case of a BOZ literal on the RHS. */
10960 if (rhs->ts.type == BT_BOZ)
10962 if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
10963 "statement value nor an actual argument of "
10964 "INT/REAL/DBLE/CMPLX intrinsic subprogram",
10965 &rhs->where))
10966 return false;
10968 switch (lhs->ts.type)
10970 case BT_INTEGER:
10971 if (!gfc_boz2int (rhs, lhs->ts.kind))
10972 return false;
10973 break;
10974 case BT_REAL:
10975 if (!gfc_boz2real (rhs, lhs->ts.kind))
10976 return false;
10977 break;
10978 default:
10979 gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
10980 return false;
10984 if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
10986 HOST_WIDE_INT llen = 0, rlen = 0;
10987 if (lhs->ts.u.cl != NULL
10988 && lhs->ts.u.cl->length != NULL
10989 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10990 llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
10992 if (rhs->expr_type == EXPR_CONSTANT)
10993 rlen = rhs->value.character.length;
10995 else if (rhs->ts.u.cl != NULL
10996 && rhs->ts.u.cl->length != NULL
10997 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10998 rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
11000 if (rlen && llen && rlen > llen)
11001 gfc_warning_now (OPT_Wcharacter_truncation,
11002 "CHARACTER expression will be truncated "
11003 "in assignment (%ld/%ld) at %L",
11004 (long) llen, (long) rlen, &code->loc);
11007 /* Ensure that a vector index expression for the lvalue is evaluated
11008 to a temporary if the lvalue symbol is referenced in it. */
11009 if (lhs->rank)
11011 for (ref = lhs->ref; ref; ref= ref->next)
11012 if (ref->type == REF_ARRAY)
11014 for (n = 0; n < ref->u.ar.dimen; n++)
11015 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
11016 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
11017 ref->u.ar.start[n]))
11018 ref->u.ar.start[n]
11019 = gfc_get_parentheses (ref->u.ar.start[n]);
11023 if (gfc_pure (NULL))
11025 if (lhs->ts.type == BT_DERIVED
11026 && lhs->expr_type == EXPR_VARIABLE
11027 && lhs->ts.u.derived->attr.pointer_comp
11028 && rhs->expr_type == EXPR_VARIABLE
11029 && (gfc_impure_variable (rhs->symtree->n.sym)
11030 || gfc_is_coindexed (rhs)))
11032 /* F2008, C1283. */
11033 if (gfc_is_coindexed (rhs))
11034 gfc_error ("Coindexed expression at %L is assigned to "
11035 "a derived type variable with a POINTER "
11036 "component in a PURE procedure",
11037 &rhs->where);
11038 else
11039 /* F2008, C1283 (4). */
11040 gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
11041 "shall not be used as the expr at %L of an intrinsic "
11042 "assignment statement in which the variable is of a "
11043 "derived type if the derived type has a pointer "
11044 "component at any level of component selection.",
11045 &rhs->where);
11046 return rval;
11049 /* Fortran 2008, C1283. */
11050 if (gfc_is_coindexed (lhs))
11052 gfc_error ("Assignment to coindexed variable at %L in a PURE "
11053 "procedure", &rhs->where);
11054 return rval;
11058 if (gfc_implicit_pure (NULL))
11060 if (lhs->expr_type == EXPR_VARIABLE
11061 && lhs->symtree->n.sym != gfc_current_ns->proc_name
11062 && lhs->symtree->n.sym->ns != gfc_current_ns)
11063 gfc_unset_implicit_pure (NULL);
11065 if (lhs->ts.type == BT_DERIVED
11066 && lhs->expr_type == EXPR_VARIABLE
11067 && lhs->ts.u.derived->attr.pointer_comp
11068 && rhs->expr_type == EXPR_VARIABLE
11069 && (gfc_impure_variable (rhs->symtree->n.sym)
11070 || gfc_is_coindexed (rhs)))
11071 gfc_unset_implicit_pure (NULL);
11073 /* Fortran 2008, C1283. */
11074 if (gfc_is_coindexed (lhs))
11075 gfc_unset_implicit_pure (NULL);
11078 /* F2008, 7.2.1.2. */
11079 attr = gfc_expr_attr (lhs);
11080 if (lhs->ts.type == BT_CLASS && attr.allocatable)
11082 if (attr.codimension)
11084 gfc_error ("Assignment to polymorphic coarray at %L is not "
11085 "permitted", &lhs->where);
11086 return false;
11088 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
11089 "polymorphic variable at %L", &lhs->where))
11090 return false;
11091 if (!flag_realloc_lhs)
11093 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
11094 "requires %<-frealloc-lhs%>", &lhs->where);
11095 return false;
11098 else if (lhs->ts.type == BT_CLASS)
11100 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
11101 "assignment at %L - check that there is a matching specific "
11102 "subroutine for '=' operator", &lhs->where);
11103 return false;
11106 bool lhs_coindexed = gfc_is_coindexed (lhs);
11108 /* F2008, Section 7.2.1.2. */
11109 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
11111 gfc_error ("Coindexed variable must not have an allocatable ultimate "
11112 "component in assignment at %L", &lhs->where);
11113 return false;
11116 /* Assign the 'data' of a class object to a derived type. */
11117 if (lhs->ts.type == BT_DERIVED
11118 && rhs->ts.type == BT_CLASS
11119 && rhs->expr_type != EXPR_ARRAY)
11120 gfc_add_data_component (rhs);
11122 /* Make sure there is a vtable and, in particular, a _copy for the
11123 rhs type. */
11124 if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
11125 gfc_find_vtab (&rhs->ts);
11127 bool caf_convert_to_send = flag_coarray == GFC_FCOARRAY_LIB
11128 && (lhs_coindexed
11129 || (code->expr2->expr_type == EXPR_FUNCTION
11130 && code->expr2->value.function.isym
11131 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
11132 && (code->expr1->rank == 0 || code->expr2->rank != 0)
11133 && !gfc_expr_attr (rhs).allocatable
11134 && !gfc_has_vector_subscript (rhs)));
11136 gfc_check_assign (lhs, rhs, 1, !caf_convert_to_send);
11138 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
11139 Additionally, insert this code when the RHS is a CAF as we then use the
11140 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
11141 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
11142 noncoindexed array and the RHS is a coindexed scalar, use the normal code
11143 path. */
11144 if (caf_convert_to_send)
11146 if (code->expr2->expr_type == EXPR_FUNCTION
11147 && code->expr2->value.function.isym
11148 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
11149 remove_caf_get_intrinsic (code->expr2);
11150 code->op = EXEC_CALL;
11151 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
11152 code->resolved_sym = code->symtree->n.sym;
11153 code->resolved_sym->attr.flavor = FL_PROCEDURE;
11154 code->resolved_sym->attr.intrinsic = 1;
11155 code->resolved_sym->attr.subroutine = 1;
11156 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
11157 gfc_commit_symbol (code->resolved_sym);
11158 code->ext.actual = gfc_get_actual_arglist ();
11159 code->ext.actual->expr = lhs;
11160 code->ext.actual->next = gfc_get_actual_arglist ();
11161 code->ext.actual->next->expr = rhs;
11162 code->expr1 = NULL;
11163 code->expr2 = NULL;
11166 return false;
11170 /* Add a component reference onto an expression. */
11172 static void
11173 add_comp_ref (gfc_expr *e, gfc_component *c)
11175 gfc_ref **ref;
11176 ref = &(e->ref);
11177 while (*ref)
11178 ref = &((*ref)->next);
11179 *ref = gfc_get_ref ();
11180 (*ref)->type = REF_COMPONENT;
11181 (*ref)->u.c.sym = e->ts.u.derived;
11182 (*ref)->u.c.component = c;
11183 e->ts = c->ts;
11185 /* Add a full array ref, as necessary. */
11186 if (c->as)
11188 gfc_add_full_array_ref (e, c->as);
11189 e->rank = c->as->rank;
11194 /* Build an assignment. Keep the argument 'op' for future use, so that
11195 pointer assignments can be made. */
11197 static gfc_code *
11198 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
11199 gfc_component *comp1, gfc_component *comp2, locus loc)
11201 gfc_code *this_code;
11203 this_code = gfc_get_code (op);
11204 this_code->next = NULL;
11205 this_code->expr1 = gfc_copy_expr (expr1);
11206 this_code->expr2 = gfc_copy_expr (expr2);
11207 this_code->loc = loc;
11208 if (comp1 && comp2)
11210 add_comp_ref (this_code->expr1, comp1);
11211 add_comp_ref (this_code->expr2, comp2);
11214 return this_code;
11218 /* Makes a temporary variable expression based on the characteristics of
11219 a given variable expression. */
11221 static gfc_expr*
11222 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
11224 static int serial = 0;
11225 char name[GFC_MAX_SYMBOL_LEN];
11226 gfc_symtree *tmp;
11227 gfc_array_spec *as;
11228 gfc_array_ref *aref;
11229 gfc_ref *ref;
11231 sprintf (name, GFC_PREFIX("DA%d"), serial++);
11232 gfc_get_sym_tree (name, ns, &tmp, false);
11233 gfc_add_type (tmp->n.sym, &e->ts, NULL);
11235 if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
11236 tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
11237 NULL,
11238 e->value.character.length);
11240 as = NULL;
11241 ref = NULL;
11242 aref = NULL;
11244 /* Obtain the arrayspec for the temporary. */
11245 if (e->rank && e->expr_type != EXPR_ARRAY
11246 && e->expr_type != EXPR_FUNCTION
11247 && e->expr_type != EXPR_OP)
11249 aref = gfc_find_array_ref (e);
11250 if (e->expr_type == EXPR_VARIABLE
11251 && e->symtree->n.sym->as == aref->as)
11252 as = aref->as;
11253 else
11255 for (ref = e->ref; ref; ref = ref->next)
11256 if (ref->type == REF_COMPONENT
11257 && ref->u.c.component->as == aref->as)
11259 as = aref->as;
11260 break;
11265 /* Add the attributes and the arrayspec to the temporary. */
11266 tmp->n.sym->attr = gfc_expr_attr (e);
11267 tmp->n.sym->attr.function = 0;
11268 tmp->n.sym->attr.proc_pointer = 0;
11269 tmp->n.sym->attr.result = 0;
11270 tmp->n.sym->attr.flavor = FL_VARIABLE;
11271 tmp->n.sym->attr.dummy = 0;
11272 tmp->n.sym->attr.use_assoc = 0;
11273 tmp->n.sym->attr.intent = INTENT_UNKNOWN;
11275 if (as)
11277 tmp->n.sym->as = gfc_copy_array_spec (as);
11278 if (!ref)
11279 ref = e->ref;
11280 if (as->type == AS_DEFERRED)
11281 tmp->n.sym->attr.allocatable = 1;
11283 else if (e->rank && (e->expr_type == EXPR_ARRAY
11284 || e->expr_type == EXPR_FUNCTION
11285 || e->expr_type == EXPR_OP))
11287 tmp->n.sym->as = gfc_get_array_spec ();
11288 tmp->n.sym->as->type = AS_DEFERRED;
11289 tmp->n.sym->as->rank = e->rank;
11290 tmp->n.sym->attr.allocatable = 1;
11291 tmp->n.sym->attr.dimension = 1;
11293 else
11294 tmp->n.sym->attr.dimension = 0;
11296 gfc_set_sym_referenced (tmp->n.sym);
11297 gfc_commit_symbol (tmp->n.sym);
11298 e = gfc_lval_expr_from_sym (tmp->n.sym);
11300 /* Should the lhs be a section, use its array ref for the
11301 temporary expression. */
11302 if (aref && aref->type != AR_FULL)
11304 gfc_free_ref_list (e->ref);
11305 e->ref = gfc_copy_ref (ref);
11307 return e;
11311 /* Add one line of code to the code chain, making sure that 'head' and
11312 'tail' are appropriately updated. */
11314 static void
11315 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
11317 gcc_assert (this_code);
11318 if (*head == NULL)
11319 *head = *tail = *this_code;
11320 else
11321 *tail = gfc_append_code (*tail, *this_code);
11322 *this_code = NULL;
11326 /* Counts the potential number of part array references that would
11327 result from resolution of typebound defined assignments. */
11329 static int
11330 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
11332 gfc_component *c;
11333 int c_depth = 0, t_depth;
11335 for (c= derived->components; c; c = c->next)
11337 if ((!gfc_bt_struct (c->ts.type)
11338 || c->attr.pointer
11339 || c->attr.allocatable
11340 || c->attr.proc_pointer_comp
11341 || c->attr.class_pointer
11342 || c->attr.proc_pointer)
11343 && !c->attr.defined_assign_comp)
11344 continue;
11346 if (c->as && c_depth == 0)
11347 c_depth = 1;
11349 if (c->ts.u.derived->attr.defined_assign_comp)
11350 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
11351 c->as ? 1 : 0);
11352 else
11353 t_depth = 0;
11355 c_depth = t_depth > c_depth ? t_depth : c_depth;
11357 return depth + c_depth;
11361 /* Implement 7.2.1.3 of the F08 standard:
11362 "An intrinsic assignment where the variable is of derived type is
11363 performed as if each component of the variable were assigned from the
11364 corresponding component of expr using pointer assignment (7.2.2) for
11365 each pointer component, defined assignment for each nonpointer
11366 nonallocatable component of a type that has a type-bound defined
11367 assignment consistent with the component, intrinsic assignment for
11368 each other nonpointer nonallocatable component, ..."
11370 The pointer assignments are taken care of by the intrinsic
11371 assignment of the structure itself. This function recursively adds
11372 defined assignments where required. The recursion is accomplished
11373 by calling gfc_resolve_code.
11375 When the lhs in a defined assignment has intent INOUT, we need a
11376 temporary for the lhs. In pseudo-code:
11378 ! Only call function lhs once.
11379 if (lhs is not a constant or an variable)
11380 temp_x = expr2
11381 expr2 => temp_x
11382 ! Do the intrinsic assignment
11383 expr1 = expr2
11384 ! Now do the defined assignments
11385 do over components with typebound defined assignment [%cmp]
11386 #if one component's assignment procedure is INOUT
11387 t1 = expr1
11388 #if expr2 non-variable
11389 temp_x = expr2
11390 expr2 => temp_x
11391 # endif
11392 expr1 = expr2
11393 # for each cmp
11394 t1%cmp {defined=} expr2%cmp
11395 expr1%cmp = t1%cmp
11396 #else
11397 expr1 = expr2
11399 # for each cmp
11400 expr1%cmp {defined=} expr2%cmp
11401 #endif
11404 /* The temporary assignments have to be put on top of the additional
11405 code to avoid the result being changed by the intrinsic assignment.
11407 static int component_assignment_level = 0;
11408 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
11410 static void
11411 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
11413 gfc_component *comp1, *comp2;
11414 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
11415 gfc_expr *t1;
11416 int error_count, depth;
11418 gfc_get_errors (NULL, &error_count);
11420 /* Filter out continuing processing after an error. */
11421 if (error_count
11422 || (*code)->expr1->ts.type != BT_DERIVED
11423 || (*code)->expr2->ts.type != BT_DERIVED)
11424 return;
11426 /* TODO: Handle more than one part array reference in assignments. */
11427 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
11428 (*code)->expr1->rank ? 1 : 0);
11429 if (depth > 1)
11431 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
11432 "done because multiple part array references would "
11433 "occur in intermediate expressions.", &(*code)->loc);
11434 return;
11437 component_assignment_level++;
11439 /* Create a temporary so that functions get called only once. */
11440 if ((*code)->expr2->expr_type != EXPR_VARIABLE
11441 && (*code)->expr2->expr_type != EXPR_CONSTANT)
11443 gfc_expr *tmp_expr;
11445 /* Assign the rhs to the temporary. */
11446 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11447 this_code = build_assignment (EXEC_ASSIGN,
11448 tmp_expr, (*code)->expr2,
11449 NULL, NULL, (*code)->loc);
11450 /* Add the code and substitute the rhs expression. */
11451 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
11452 gfc_free_expr ((*code)->expr2);
11453 (*code)->expr2 = tmp_expr;
11456 /* Do the intrinsic assignment. This is not needed if the lhs is one
11457 of the temporaries generated here, since the intrinsic assignment
11458 to the final result already does this. */
11459 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
11461 this_code = build_assignment (EXEC_ASSIGN,
11462 (*code)->expr1, (*code)->expr2,
11463 NULL, NULL, (*code)->loc);
11464 add_code_to_chain (&this_code, &head, &tail);
11467 comp1 = (*code)->expr1->ts.u.derived->components;
11468 comp2 = (*code)->expr2->ts.u.derived->components;
11470 t1 = NULL;
11471 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
11473 bool inout = false;
11475 /* The intrinsic assignment does the right thing for pointers
11476 of all kinds and allocatable components. */
11477 if (!gfc_bt_struct (comp1->ts.type)
11478 || comp1->attr.pointer
11479 || comp1->attr.allocatable
11480 || comp1->attr.proc_pointer_comp
11481 || comp1->attr.class_pointer
11482 || comp1->attr.proc_pointer)
11483 continue;
11485 /* Make an assigment for this component. */
11486 this_code = build_assignment (EXEC_ASSIGN,
11487 (*code)->expr1, (*code)->expr2,
11488 comp1, comp2, (*code)->loc);
11490 /* Convert the assignment if there is a defined assignment for
11491 this type. Otherwise, using the call from gfc_resolve_code,
11492 recurse into its components. */
11493 gfc_resolve_code (this_code, ns);
11495 if (this_code->op == EXEC_ASSIGN_CALL)
11497 gfc_formal_arglist *dummy_args;
11498 gfc_symbol *rsym;
11499 /* Check that there is a typebound defined assignment. If not,
11500 then this must be a module defined assignment. We cannot
11501 use the defined_assign_comp attribute here because it must
11502 be this derived type that has the defined assignment and not
11503 a parent type. */
11504 if (!(comp1->ts.u.derived->f2k_derived
11505 && comp1->ts.u.derived->f2k_derived
11506 ->tb_op[INTRINSIC_ASSIGN]))
11508 gfc_free_statements (this_code);
11509 this_code = NULL;
11510 continue;
11513 /* If the first argument of the subroutine has intent INOUT
11514 a temporary must be generated and used instead. */
11515 rsym = this_code->resolved_sym;
11516 dummy_args = gfc_sym_get_dummy_args (rsym);
11517 if (dummy_args
11518 && dummy_args->sym->attr.intent == INTENT_INOUT)
11520 gfc_code *temp_code;
11521 inout = true;
11523 /* Build the temporary required for the assignment and put
11524 it at the head of the generated code. */
11525 if (!t1)
11527 t1 = get_temp_from_expr ((*code)->expr1, ns);
11528 temp_code = build_assignment (EXEC_ASSIGN,
11529 t1, (*code)->expr1,
11530 NULL, NULL, (*code)->loc);
11532 /* For allocatable LHS, check whether it is allocated. Note
11533 that allocatable components with defined assignment are
11534 not yet support. See PR 57696. */
11535 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
11537 gfc_code *block;
11538 gfc_expr *e =
11539 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11540 block = gfc_get_code (EXEC_IF);
11541 block->block = gfc_get_code (EXEC_IF);
11542 block->block->expr1
11543 = gfc_build_intrinsic_call (ns,
11544 GFC_ISYM_ALLOCATED, "allocated",
11545 (*code)->loc, 1, e);
11546 block->block->next = temp_code;
11547 temp_code = block;
11549 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
11552 /* Replace the first actual arg with the component of the
11553 temporary. */
11554 gfc_free_expr (this_code->ext.actual->expr);
11555 this_code->ext.actual->expr = gfc_copy_expr (t1);
11556 add_comp_ref (this_code->ext.actual->expr, comp1);
11558 /* If the LHS variable is allocatable and wasn't allocated and
11559 the temporary is allocatable, pointer assign the address of
11560 the freshly allocated LHS to the temporary. */
11561 if ((*code)->expr1->symtree->n.sym->attr.allocatable
11562 && gfc_expr_attr ((*code)->expr1).allocatable)
11564 gfc_code *block;
11565 gfc_expr *cond;
11567 cond = gfc_get_expr ();
11568 cond->ts.type = BT_LOGICAL;
11569 cond->ts.kind = gfc_default_logical_kind;
11570 cond->expr_type = EXPR_OP;
11571 cond->where = (*code)->loc;
11572 cond->value.op.op = INTRINSIC_NOT;
11573 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
11574 GFC_ISYM_ALLOCATED, "allocated",
11575 (*code)->loc, 1, gfc_copy_expr (t1));
11576 block = gfc_get_code (EXEC_IF);
11577 block->block = gfc_get_code (EXEC_IF);
11578 block->block->expr1 = cond;
11579 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11580 t1, (*code)->expr1,
11581 NULL, NULL, (*code)->loc);
11582 add_code_to_chain (&block, &head, &tail);
11586 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
11588 /* Don't add intrinsic assignments since they are already
11589 effected by the intrinsic assignment of the structure. */
11590 gfc_free_statements (this_code);
11591 this_code = NULL;
11592 continue;
11595 add_code_to_chain (&this_code, &head, &tail);
11597 if (t1 && inout)
11599 /* Transfer the value to the final result. */
11600 this_code = build_assignment (EXEC_ASSIGN,
11601 (*code)->expr1, t1,
11602 comp1, comp2, (*code)->loc);
11603 add_code_to_chain (&this_code, &head, &tail);
11607 /* Put the temporary assignments at the top of the generated code. */
11608 if (tmp_head && component_assignment_level == 1)
11610 gfc_append_code (tmp_head, head);
11611 head = tmp_head;
11612 tmp_head = tmp_tail = NULL;
11615 // If we did a pointer assignment - thus, we need to ensure that the LHS is
11616 // not accidentally deallocated. Hence, nullify t1.
11617 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
11618 && gfc_expr_attr ((*code)->expr1).allocatable)
11620 gfc_code *block;
11621 gfc_expr *cond;
11622 gfc_expr *e;
11624 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11625 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
11626 (*code)->loc, 2, gfc_copy_expr (t1), e);
11627 block = gfc_get_code (EXEC_IF);
11628 block->block = gfc_get_code (EXEC_IF);
11629 block->block->expr1 = cond;
11630 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11631 t1, gfc_get_null_expr (&(*code)->loc),
11632 NULL, NULL, (*code)->loc);
11633 gfc_append_code (tail, block);
11634 tail = block;
11637 /* Now attach the remaining code chain to the input code. Step on
11638 to the end of the new code since resolution is complete. */
11639 gcc_assert ((*code)->op == EXEC_ASSIGN);
11640 tail->next = (*code)->next;
11641 /* Overwrite 'code' because this would place the intrinsic assignment
11642 before the temporary for the lhs is created. */
11643 gfc_free_expr ((*code)->expr1);
11644 gfc_free_expr ((*code)->expr2);
11645 **code = *head;
11646 if (head != tail)
11647 free (head);
11648 *code = tail;
11650 component_assignment_level--;
11654 /* F2008: Pointer function assignments are of the form:
11655 ptr_fcn (args) = expr
11656 This function breaks these assignments into two statements:
11657 temporary_pointer => ptr_fcn(args)
11658 temporary_pointer = expr */
11660 static bool
11661 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
11663 gfc_expr *tmp_ptr_expr;
11664 gfc_code *this_code;
11665 gfc_component *comp;
11666 gfc_symbol *s;
11668 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
11669 return false;
11671 /* Even if standard does not support this feature, continue to build
11672 the two statements to avoid upsetting frontend_passes.c. */
11673 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
11674 "%L", &(*code)->loc);
11676 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
11678 if (comp)
11679 s = comp->ts.interface;
11680 else
11681 s = (*code)->expr1->symtree->n.sym;
11683 if (s == NULL || !s->result->attr.pointer)
11685 gfc_error ("The function result on the lhs of the assignment at "
11686 "%L must have the pointer attribute.",
11687 &(*code)->expr1->where);
11688 (*code)->op = EXEC_NOP;
11689 return false;
11692 tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
11694 /* get_temp_from_expression is set up for ordinary assignments. To that
11695 end, where array bounds are not known, arrays are made allocatable.
11696 Change the temporary to a pointer here. */
11697 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
11698 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
11699 tmp_ptr_expr->where = (*code)->loc;
11701 this_code = build_assignment (EXEC_ASSIGN,
11702 tmp_ptr_expr, (*code)->expr2,
11703 NULL, NULL, (*code)->loc);
11704 this_code->next = (*code)->next;
11705 (*code)->next = this_code;
11706 (*code)->op = EXEC_POINTER_ASSIGN;
11707 (*code)->expr2 = (*code)->expr1;
11708 (*code)->expr1 = tmp_ptr_expr;
11710 return true;
11714 /* Deferred character length assignments from an operator expression
11715 require a temporary because the character length of the lhs can
11716 change in the course of the assignment. */
11718 static bool
11719 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
11721 gfc_expr *tmp_expr;
11722 gfc_code *this_code;
11724 if (!((*code)->expr1->ts.type == BT_CHARACTER
11725 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
11726 && (*code)->expr2->expr_type == EXPR_OP))
11727 return false;
11729 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
11730 return false;
11732 if (gfc_expr_attr ((*code)->expr1).pointer)
11733 return false;
11735 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11736 tmp_expr->where = (*code)->loc;
11738 /* A new charlen is required to ensure that the variable string
11739 length is different to that of the original lhs. */
11740 tmp_expr->ts.u.cl = gfc_get_charlen();
11741 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
11742 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
11743 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
11745 tmp_expr->symtree->n.sym->ts.deferred = 1;
11747 this_code = build_assignment (EXEC_ASSIGN,
11748 (*code)->expr1,
11749 gfc_copy_expr (tmp_expr),
11750 NULL, NULL, (*code)->loc);
11752 (*code)->expr1 = tmp_expr;
11754 this_code->next = (*code)->next;
11755 (*code)->next = this_code;
11757 return true;
11761 /* Given a block of code, recursively resolve everything pointed to by this
11762 code block. */
11764 void
11765 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
11767 int omp_workshare_save;
11768 int forall_save, do_concurrent_save;
11769 code_stack frame;
11770 bool t;
11772 frame.prev = cs_base;
11773 frame.head = code;
11774 cs_base = &frame;
11776 find_reachable_labels (code);
11778 for (; code; code = code->next)
11780 frame.current = code;
11781 forall_save = forall_flag;
11782 do_concurrent_save = gfc_do_concurrent_flag;
11784 if (code->op == EXEC_FORALL)
11786 forall_flag = 1;
11787 gfc_resolve_forall (code, ns, forall_save);
11788 forall_flag = 2;
11790 else if (code->block)
11792 omp_workshare_save = -1;
11793 switch (code->op)
11795 case EXEC_OACC_PARALLEL_LOOP:
11796 case EXEC_OACC_PARALLEL:
11797 case EXEC_OACC_KERNELS_LOOP:
11798 case EXEC_OACC_KERNELS:
11799 case EXEC_OACC_SERIAL_LOOP:
11800 case EXEC_OACC_SERIAL:
11801 case EXEC_OACC_DATA:
11802 case EXEC_OACC_HOST_DATA:
11803 case EXEC_OACC_LOOP:
11804 gfc_resolve_oacc_blocks (code, ns);
11805 break;
11806 case EXEC_OMP_PARALLEL_WORKSHARE:
11807 omp_workshare_save = omp_workshare_flag;
11808 omp_workshare_flag = 1;
11809 gfc_resolve_omp_parallel_blocks (code, ns);
11810 break;
11811 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
11812 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
11813 case EXEC_OMP_PARALLEL:
11814 case EXEC_OMP_PARALLEL_DO:
11815 case EXEC_OMP_PARALLEL_DO_SIMD:
11816 case EXEC_OMP_PARALLEL_MASKED:
11817 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
11818 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
11819 case EXEC_OMP_PARALLEL_MASTER:
11820 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
11821 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
11822 case EXEC_OMP_PARALLEL_SECTIONS:
11823 case EXEC_OMP_TARGET_PARALLEL:
11824 case EXEC_OMP_TARGET_PARALLEL_DO:
11825 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
11826 case EXEC_OMP_TARGET_TEAMS:
11827 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
11828 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
11829 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11830 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
11831 case EXEC_OMP_TASK:
11832 case EXEC_OMP_TASKLOOP:
11833 case EXEC_OMP_TASKLOOP_SIMD:
11834 case EXEC_OMP_TEAMS:
11835 case EXEC_OMP_TEAMS_DISTRIBUTE:
11836 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
11837 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11838 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
11839 omp_workshare_save = omp_workshare_flag;
11840 omp_workshare_flag = 0;
11841 gfc_resolve_omp_parallel_blocks (code, ns);
11842 break;
11843 case EXEC_OMP_DISTRIBUTE:
11844 case EXEC_OMP_DISTRIBUTE_SIMD:
11845 case EXEC_OMP_DO:
11846 case EXEC_OMP_DO_SIMD:
11847 case EXEC_OMP_SIMD:
11848 case EXEC_OMP_TARGET_SIMD:
11849 gfc_resolve_omp_do_blocks (code, ns);
11850 break;
11851 case EXEC_SELECT_TYPE:
11852 case EXEC_SELECT_RANK:
11853 /* Blocks are handled in resolve_select_type/rank because we
11854 have to transform the SELECT TYPE into ASSOCIATE first. */
11855 break;
11856 case EXEC_DO_CONCURRENT:
11857 gfc_do_concurrent_flag = 1;
11858 gfc_resolve_blocks (code->block, ns);
11859 gfc_do_concurrent_flag = 2;
11860 break;
11861 case EXEC_OMP_WORKSHARE:
11862 omp_workshare_save = omp_workshare_flag;
11863 omp_workshare_flag = 1;
11864 /* FALL THROUGH */
11865 default:
11866 gfc_resolve_blocks (code->block, ns);
11867 break;
11870 if (omp_workshare_save != -1)
11871 omp_workshare_flag = omp_workshare_save;
11873 start:
11874 t = true;
11875 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
11876 t = gfc_resolve_expr (code->expr1);
11877 forall_flag = forall_save;
11878 gfc_do_concurrent_flag = do_concurrent_save;
11880 if (!gfc_resolve_expr (code->expr2))
11881 t = false;
11883 if (code->op == EXEC_ALLOCATE
11884 && !gfc_resolve_expr (code->expr3))
11885 t = false;
11887 switch (code->op)
11889 case EXEC_NOP:
11890 case EXEC_END_BLOCK:
11891 case EXEC_END_NESTED_BLOCK:
11892 case EXEC_CYCLE:
11893 case EXEC_PAUSE:
11894 case EXEC_STOP:
11895 case EXEC_ERROR_STOP:
11896 case EXEC_EXIT:
11897 case EXEC_CONTINUE:
11898 case EXEC_DT_END:
11899 case EXEC_ASSIGN_CALL:
11900 break;
11902 case EXEC_CRITICAL:
11903 resolve_critical (code);
11904 break;
11906 case EXEC_SYNC_ALL:
11907 case EXEC_SYNC_IMAGES:
11908 case EXEC_SYNC_MEMORY:
11909 resolve_sync (code);
11910 break;
11912 case EXEC_LOCK:
11913 case EXEC_UNLOCK:
11914 case EXEC_EVENT_POST:
11915 case EXEC_EVENT_WAIT:
11916 resolve_lock_unlock_event (code);
11917 break;
11919 case EXEC_FAIL_IMAGE:
11920 case EXEC_FORM_TEAM:
11921 case EXEC_CHANGE_TEAM:
11922 case EXEC_END_TEAM:
11923 case EXEC_SYNC_TEAM:
11924 break;
11926 case EXEC_ENTRY:
11927 /* Keep track of which entry we are up to. */
11928 current_entry_id = code->ext.entry->id;
11929 break;
11931 case EXEC_WHERE:
11932 resolve_where (code, NULL);
11933 break;
11935 case EXEC_GOTO:
11936 if (code->expr1 != NULL)
11938 if (code->expr1->expr_type != EXPR_VARIABLE
11939 || code->expr1->ts.type != BT_INTEGER
11940 || (code->expr1->ref
11941 && code->expr1->ref->type == REF_ARRAY)
11942 || code->expr1->symtree == NULL
11943 || (code->expr1->symtree->n.sym
11944 && (code->expr1->symtree->n.sym->attr.flavor
11945 == FL_PARAMETER)))
11946 gfc_error ("ASSIGNED GOTO statement at %L requires a "
11947 "scalar INTEGER variable", &code->expr1->where);
11948 else if (code->expr1->symtree->n.sym
11949 && code->expr1->symtree->n.sym->attr.assign != 1)
11950 gfc_error ("Variable %qs has not been assigned a target "
11951 "label at %L", code->expr1->symtree->n.sym->name,
11952 &code->expr1->where);
11954 else
11955 resolve_branch (code->label1, code);
11956 break;
11958 case EXEC_RETURN:
11959 if (code->expr1 != NULL
11960 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
11961 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
11962 "INTEGER return specifier", &code->expr1->where);
11963 break;
11965 case EXEC_INIT_ASSIGN:
11966 case EXEC_END_PROCEDURE:
11967 break;
11969 case EXEC_ASSIGN:
11970 if (!t)
11971 break;
11973 if (code->expr1->ts.type == BT_CLASS)
11974 gfc_find_vtab (&code->expr2->ts);
11976 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
11977 the LHS. */
11978 if (code->expr1->expr_type == EXPR_FUNCTION
11979 && code->expr1->value.function.isym
11980 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
11981 remove_caf_get_intrinsic (code->expr1);
11983 /* If this is a pointer function in an lvalue variable context,
11984 the new code will have to be resolved afresh. This is also the
11985 case with an error, where the code is transformed into NOP to
11986 prevent ICEs downstream. */
11987 if (resolve_ptr_fcn_assign (&code, ns)
11988 || code->op == EXEC_NOP)
11989 goto start;
11991 if (!gfc_check_vardef_context (code->expr1, false, false, false,
11992 _("assignment")))
11993 break;
11995 if (resolve_ordinary_assign (code, ns))
11997 if (omp_workshare_flag)
11999 gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
12000 "at %L", &code->loc);
12001 break;
12003 if (code->op == EXEC_COMPCALL)
12004 goto compcall;
12005 else
12006 goto call;
12009 /* Check for dependencies in deferred character length array
12010 assignments and generate a temporary, if necessary. */
12011 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
12012 break;
12014 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
12015 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
12016 && code->expr1->ts.u.derived
12017 && code->expr1->ts.u.derived->attr.defined_assign_comp)
12018 generate_component_assignments (&code, ns);
12020 break;
12022 case EXEC_LABEL_ASSIGN:
12023 if (code->label1->defined == ST_LABEL_UNKNOWN)
12024 gfc_error ("Label %d referenced at %L is never defined",
12025 code->label1->value, &code->label1->where);
12026 if (t
12027 && (code->expr1->expr_type != EXPR_VARIABLE
12028 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
12029 || code->expr1->symtree->n.sym->ts.kind
12030 != gfc_default_integer_kind
12031 || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
12032 || code->expr1->symtree->n.sym->as != NULL))
12033 gfc_error ("ASSIGN statement at %L requires a scalar "
12034 "default INTEGER variable", &code->expr1->where);
12035 break;
12037 case EXEC_POINTER_ASSIGN:
12039 gfc_expr* e;
12041 if (!t)
12042 break;
12044 /* This is both a variable definition and pointer assignment
12045 context, so check both of them. For rank remapping, a final
12046 array ref may be present on the LHS and fool gfc_expr_attr
12047 used in gfc_check_vardef_context. Remove it. */
12048 e = remove_last_array_ref (code->expr1);
12049 t = gfc_check_vardef_context (e, true, false, false,
12050 _("pointer assignment"));
12051 if (t)
12052 t = gfc_check_vardef_context (e, false, false, false,
12053 _("pointer assignment"));
12054 gfc_free_expr (e);
12056 t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
12058 if (!t)
12059 break;
12061 /* Assigning a class object always is a regular assign. */
12062 if (code->expr2->ts.type == BT_CLASS
12063 && code->expr1->ts.type == BT_CLASS
12064 && CLASS_DATA (code->expr2)
12065 && !CLASS_DATA (code->expr2)->attr.dimension
12066 && !(gfc_expr_attr (code->expr1).proc_pointer
12067 && code->expr2->expr_type == EXPR_VARIABLE
12068 && code->expr2->symtree->n.sym->attr.flavor
12069 == FL_PROCEDURE))
12070 code->op = EXEC_ASSIGN;
12071 break;
12074 case EXEC_ARITHMETIC_IF:
12076 gfc_expr *e = code->expr1;
12078 gfc_resolve_expr (e);
12079 if (e->expr_type == EXPR_NULL)
12080 gfc_error ("Invalid NULL at %L", &e->where);
12082 if (t && (e->rank > 0
12083 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
12084 gfc_error ("Arithmetic IF statement at %L requires a scalar "
12085 "REAL or INTEGER expression", &e->where);
12087 resolve_branch (code->label1, code);
12088 resolve_branch (code->label2, code);
12089 resolve_branch (code->label3, code);
12091 break;
12093 case EXEC_IF:
12094 if (t && code->expr1 != NULL
12095 && (code->expr1->ts.type != BT_LOGICAL
12096 || code->expr1->rank != 0))
12097 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
12098 &code->expr1->where);
12099 break;
12101 case EXEC_CALL:
12102 call:
12103 resolve_call (code);
12104 break;
12106 case EXEC_COMPCALL:
12107 compcall:
12108 resolve_typebound_subroutine (code);
12109 break;
12111 case EXEC_CALL_PPC:
12112 resolve_ppc_call (code);
12113 break;
12115 case EXEC_SELECT:
12116 /* Select is complicated. Also, a SELECT construct could be
12117 a transformed computed GOTO. */
12118 resolve_select (code, false);
12119 break;
12121 case EXEC_SELECT_TYPE:
12122 resolve_select_type (code, ns);
12123 break;
12125 case EXEC_SELECT_RANK:
12126 resolve_select_rank (code, ns);
12127 break;
12129 case EXEC_BLOCK:
12130 resolve_block_construct (code);
12131 break;
12133 case EXEC_DO:
12134 if (code->ext.iterator != NULL)
12136 gfc_iterator *iter = code->ext.iterator;
12137 if (gfc_resolve_iterator (iter, true, false))
12138 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
12139 true);
12141 break;
12143 case EXEC_DO_WHILE:
12144 if (code->expr1 == NULL)
12145 gfc_internal_error ("gfc_resolve_code(): No expression on "
12146 "DO WHILE");
12147 if (t
12148 && (code->expr1->rank != 0
12149 || code->expr1->ts.type != BT_LOGICAL))
12150 gfc_error ("Exit condition of DO WHILE loop at %L must be "
12151 "a scalar LOGICAL expression", &code->expr1->where);
12152 break;
12154 case EXEC_ALLOCATE:
12155 if (t)
12156 resolve_allocate_deallocate (code, "ALLOCATE");
12158 break;
12160 case EXEC_DEALLOCATE:
12161 if (t)
12162 resolve_allocate_deallocate (code, "DEALLOCATE");
12164 break;
12166 case EXEC_OPEN:
12167 if (!gfc_resolve_open (code->ext.open, &code->loc))
12168 break;
12170 resolve_branch (code->ext.open->err, code);
12171 break;
12173 case EXEC_CLOSE:
12174 if (!gfc_resolve_close (code->ext.close, &code->loc))
12175 break;
12177 resolve_branch (code->ext.close->err, code);
12178 break;
12180 case EXEC_BACKSPACE:
12181 case EXEC_ENDFILE:
12182 case EXEC_REWIND:
12183 case EXEC_FLUSH:
12184 if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
12185 break;
12187 resolve_branch (code->ext.filepos->err, code);
12188 break;
12190 case EXEC_INQUIRE:
12191 if (!gfc_resolve_inquire (code->ext.inquire))
12192 break;
12194 resolve_branch (code->ext.inquire->err, code);
12195 break;
12197 case EXEC_IOLENGTH:
12198 gcc_assert (code->ext.inquire != NULL);
12199 if (!gfc_resolve_inquire (code->ext.inquire))
12200 break;
12202 resolve_branch (code->ext.inquire->err, code);
12203 break;
12205 case EXEC_WAIT:
12206 if (!gfc_resolve_wait (code->ext.wait))
12207 break;
12209 resolve_branch (code->ext.wait->err, code);
12210 resolve_branch (code->ext.wait->end, code);
12211 resolve_branch (code->ext.wait->eor, code);
12212 break;
12214 case EXEC_READ:
12215 case EXEC_WRITE:
12216 if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
12217 break;
12219 resolve_branch (code->ext.dt->err, code);
12220 resolve_branch (code->ext.dt->end, code);
12221 resolve_branch (code->ext.dt->eor, code);
12222 break;
12224 case EXEC_TRANSFER:
12225 resolve_transfer (code);
12226 break;
12228 case EXEC_DO_CONCURRENT:
12229 case EXEC_FORALL:
12230 resolve_forall_iterators (code->ext.forall_iterator);
12232 if (code->expr1 != NULL
12233 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
12234 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
12235 "expression", &code->expr1->where);
12236 break;
12238 case EXEC_OACC_PARALLEL_LOOP:
12239 case EXEC_OACC_PARALLEL:
12240 case EXEC_OACC_KERNELS_LOOP:
12241 case EXEC_OACC_KERNELS:
12242 case EXEC_OACC_SERIAL_LOOP:
12243 case EXEC_OACC_SERIAL:
12244 case EXEC_OACC_DATA:
12245 case EXEC_OACC_HOST_DATA:
12246 case EXEC_OACC_LOOP:
12247 case EXEC_OACC_UPDATE:
12248 case EXEC_OACC_WAIT:
12249 case EXEC_OACC_CACHE:
12250 case EXEC_OACC_ENTER_DATA:
12251 case EXEC_OACC_EXIT_DATA:
12252 case EXEC_OACC_ATOMIC:
12253 case EXEC_OACC_DECLARE:
12254 gfc_resolve_oacc_directive (code, ns);
12255 break;
12257 case EXEC_OMP_ATOMIC:
12258 case EXEC_OMP_BARRIER:
12259 case EXEC_OMP_CANCEL:
12260 case EXEC_OMP_CANCELLATION_POINT:
12261 case EXEC_OMP_CRITICAL:
12262 case EXEC_OMP_FLUSH:
12263 case EXEC_OMP_DEPOBJ:
12264 case EXEC_OMP_DISTRIBUTE:
12265 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12266 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12267 case EXEC_OMP_DISTRIBUTE_SIMD:
12268 case EXEC_OMP_DO:
12269 case EXEC_OMP_DO_SIMD:
12270 case EXEC_OMP_ERROR:
12271 case EXEC_OMP_LOOP:
12272 case EXEC_OMP_MASTER:
12273 case EXEC_OMP_MASTER_TASKLOOP:
12274 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
12275 case EXEC_OMP_MASKED:
12276 case EXEC_OMP_MASKED_TASKLOOP:
12277 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
12278 case EXEC_OMP_ORDERED:
12279 case EXEC_OMP_SCAN:
12280 case EXEC_OMP_SCOPE:
12281 case EXEC_OMP_SECTIONS:
12282 case EXEC_OMP_SIMD:
12283 case EXEC_OMP_SINGLE:
12284 case EXEC_OMP_TARGET:
12285 case EXEC_OMP_TARGET_DATA:
12286 case EXEC_OMP_TARGET_ENTER_DATA:
12287 case EXEC_OMP_TARGET_EXIT_DATA:
12288 case EXEC_OMP_TARGET_PARALLEL:
12289 case EXEC_OMP_TARGET_PARALLEL_DO:
12290 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12291 case EXEC_OMP_TARGET_PARALLEL_LOOP:
12292 case EXEC_OMP_TARGET_SIMD:
12293 case EXEC_OMP_TARGET_TEAMS:
12294 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12295 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12296 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12297 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12298 case EXEC_OMP_TARGET_TEAMS_LOOP:
12299 case EXEC_OMP_TARGET_UPDATE:
12300 case EXEC_OMP_TASK:
12301 case EXEC_OMP_TASKGROUP:
12302 case EXEC_OMP_TASKLOOP:
12303 case EXEC_OMP_TASKLOOP_SIMD:
12304 case EXEC_OMP_TASKWAIT:
12305 case EXEC_OMP_TASKYIELD:
12306 case EXEC_OMP_TEAMS:
12307 case EXEC_OMP_TEAMS_DISTRIBUTE:
12308 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12309 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12310 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12311 case EXEC_OMP_TEAMS_LOOP:
12312 case EXEC_OMP_WORKSHARE:
12313 gfc_resolve_omp_directive (code, ns);
12314 break;
12316 case EXEC_OMP_PARALLEL:
12317 case EXEC_OMP_PARALLEL_DO:
12318 case EXEC_OMP_PARALLEL_DO_SIMD:
12319 case EXEC_OMP_PARALLEL_LOOP:
12320 case EXEC_OMP_PARALLEL_MASKED:
12321 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
12322 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
12323 case EXEC_OMP_PARALLEL_MASTER:
12324 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
12325 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
12326 case EXEC_OMP_PARALLEL_SECTIONS:
12327 case EXEC_OMP_PARALLEL_WORKSHARE:
12328 omp_workshare_save = omp_workshare_flag;
12329 omp_workshare_flag = 0;
12330 gfc_resolve_omp_directive (code, ns);
12331 omp_workshare_flag = omp_workshare_save;
12332 break;
12334 default:
12335 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
12339 cs_base = frame.prev;
12343 /* Resolve initial values and make sure they are compatible with
12344 the variable. */
12346 static void
12347 resolve_values (gfc_symbol *sym)
12349 bool t;
12351 if (sym->value == NULL)
12352 return;
12354 if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
12355 gfc_warning (OPT_Wdeprecated_declarations,
12356 "Using parameter %qs declared at %L is deprecated",
12357 sym->name, &sym->declared_at);
12359 if (sym->value->expr_type == EXPR_STRUCTURE)
12360 t= resolve_structure_cons (sym->value, 1);
12361 else
12362 t = gfc_resolve_expr (sym->value);
12364 if (!t)
12365 return;
12367 gfc_check_assign_symbol (sym, NULL, sym->value);
12371 /* Verify any BIND(C) derived types in the namespace so we can report errors
12372 for them once, rather than for each variable declared of that type. */
12374 static void
12375 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
12377 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
12378 && derived_sym->attr.is_bind_c == 1)
12379 verify_bind_c_derived_type (derived_sym);
12381 return;
12385 /* Check the interfaces of DTIO procedures associated with derived
12386 type 'sym'. These procedures can either have typebound bindings or
12387 can appear in DTIO generic interfaces. */
12389 static void
12390 gfc_verify_DTIO_procedures (gfc_symbol *sym)
12392 if (!sym || sym->attr.flavor != FL_DERIVED)
12393 return;
12395 gfc_check_dtio_interfaces (sym);
12397 return;
12400 /* Verify that any binding labels used in a given namespace do not collide
12401 with the names or binding labels of any global symbols. Multiple INTERFACE
12402 for the same procedure are permitted. */
12404 static void
12405 gfc_verify_binding_labels (gfc_symbol *sym)
12407 gfc_gsymbol *gsym;
12408 const char *module;
12410 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
12411 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
12412 return;
12414 gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
12416 if (sym->module)
12417 module = sym->module;
12418 else if (sym->ns && sym->ns->proc_name
12419 && sym->ns->proc_name->attr.flavor == FL_MODULE)
12420 module = sym->ns->proc_name->name;
12421 else if (sym->ns && sym->ns->parent
12422 && sym->ns && sym->ns->parent->proc_name
12423 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
12424 module = sym->ns->parent->proc_name->name;
12425 else
12426 module = NULL;
12428 if (!gsym
12429 || (!gsym->defined
12430 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
12432 if (!gsym)
12433 gsym = gfc_get_gsymbol (sym->binding_label, true);
12434 gsym->where = sym->declared_at;
12435 gsym->sym_name = sym->name;
12436 gsym->binding_label = sym->binding_label;
12437 gsym->ns = sym->ns;
12438 gsym->mod_name = module;
12439 if (sym->attr.function)
12440 gsym->type = GSYM_FUNCTION;
12441 else if (sym->attr.subroutine)
12442 gsym->type = GSYM_SUBROUTINE;
12443 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
12444 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
12445 return;
12448 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
12450 gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
12451 "identifier as entity at %L", sym->name,
12452 sym->binding_label, &sym->declared_at, &gsym->where);
12453 /* Clear the binding label to prevent checking multiple times. */
12454 sym->binding_label = NULL;
12455 return;
12458 if (sym->attr.flavor == FL_VARIABLE && module
12459 && (strcmp (module, gsym->mod_name) != 0
12460 || strcmp (sym->name, gsym->sym_name) != 0))
12462 /* This can only happen if the variable is defined in a module - if it
12463 isn't the same module, reject it. */
12464 gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
12465 "uses the same global identifier as entity at %L from module %qs",
12466 sym->name, module, sym->binding_label,
12467 &sym->declared_at, &gsym->where, gsym->mod_name);
12468 sym->binding_label = NULL;
12469 return;
12472 if ((sym->attr.function || sym->attr.subroutine)
12473 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
12474 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
12475 && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
12476 && (module != gsym->mod_name
12477 || strcmp (gsym->sym_name, sym->name) != 0
12478 || (module && strcmp (module, gsym->mod_name) != 0)))
12480 /* Print an error if the procedure is defined multiple times; we have to
12481 exclude references to the same procedure via module association or
12482 multiple checks for the same procedure. */
12483 gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
12484 "global identifier as entity at %L", sym->name,
12485 sym->binding_label, &sym->declared_at, &gsym->where);
12486 sym->binding_label = NULL;
12491 /* Resolve an index expression. */
12493 static bool
12494 resolve_index_expr (gfc_expr *e)
12496 if (!gfc_resolve_expr (e))
12497 return false;
12499 if (!gfc_simplify_expr (e, 0))
12500 return false;
12502 if (!gfc_specification_expr (e))
12503 return false;
12505 return true;
12509 /* Resolve a charlen structure. */
12511 static bool
12512 resolve_charlen (gfc_charlen *cl)
12514 int k;
12515 bool saved_specification_expr;
12517 if (cl->resolved)
12518 return true;
12520 cl->resolved = 1;
12521 saved_specification_expr = specification_expr;
12522 specification_expr = true;
12524 if (cl->length_from_typespec)
12526 if (!gfc_resolve_expr (cl->length))
12528 specification_expr = saved_specification_expr;
12529 return false;
12532 if (!gfc_simplify_expr (cl->length, 0))
12534 specification_expr = saved_specification_expr;
12535 return false;
12538 /* cl->length has been resolved. It should have an integer type. */
12539 if (cl->length
12540 && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
12542 gfc_error ("Scalar INTEGER expression expected at %L",
12543 &cl->length->where);
12544 return false;
12547 else
12549 if (!resolve_index_expr (cl->length))
12551 specification_expr = saved_specification_expr;
12552 return false;
12556 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
12557 a negative value, the length of character entities declared is zero. */
12558 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12559 && mpz_sgn (cl->length->value.integer) < 0)
12560 gfc_replace_expr (cl->length,
12561 gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
12563 /* Check that the character length is not too large. */
12564 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
12565 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12566 && cl->length->ts.type == BT_INTEGER
12567 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
12569 gfc_error ("String length at %L is too large", &cl->length->where);
12570 specification_expr = saved_specification_expr;
12571 return false;
12574 specification_expr = saved_specification_expr;
12575 return true;
12579 /* Test for non-constant shape arrays. */
12581 static bool
12582 is_non_constant_shape_array (gfc_symbol *sym)
12584 gfc_expr *e;
12585 int i;
12586 bool not_constant;
12588 not_constant = false;
12589 if (sym->as != NULL)
12591 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
12592 has not been simplified; parameter array references. Do the
12593 simplification now. */
12594 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
12596 if (i == GFC_MAX_DIMENSIONS)
12597 break;
12599 e = sym->as->lower[i];
12600 if (e && (!resolve_index_expr(e)
12601 || !gfc_is_constant_expr (e)))
12602 not_constant = true;
12603 e = sym->as->upper[i];
12604 if (e && (!resolve_index_expr(e)
12605 || !gfc_is_constant_expr (e)))
12606 not_constant = true;
12609 return not_constant;
12612 /* Given a symbol and an initialization expression, add code to initialize
12613 the symbol to the function entry. */
12614 static void
12615 build_init_assign (gfc_symbol *sym, gfc_expr *init)
12617 gfc_expr *lval;
12618 gfc_code *init_st;
12619 gfc_namespace *ns = sym->ns;
12621 /* Search for the function namespace if this is a contained
12622 function without an explicit result. */
12623 if (sym->attr.function && sym == sym->result
12624 && sym->name != sym->ns->proc_name->name)
12626 ns = ns->contained;
12627 for (;ns; ns = ns->sibling)
12628 if (strcmp (ns->proc_name->name, sym->name) == 0)
12629 break;
12632 if (ns == NULL)
12634 gfc_free_expr (init);
12635 return;
12638 /* Build an l-value expression for the result. */
12639 lval = gfc_lval_expr_from_sym (sym);
12641 /* Add the code at scope entry. */
12642 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
12643 init_st->next = ns->code;
12644 ns->code = init_st;
12646 /* Assign the default initializer to the l-value. */
12647 init_st->loc = sym->declared_at;
12648 init_st->expr1 = lval;
12649 init_st->expr2 = init;
12653 /* Whether or not we can generate a default initializer for a symbol. */
12655 static bool
12656 can_generate_init (gfc_symbol *sym)
12658 symbol_attribute *a;
12659 if (!sym)
12660 return false;
12661 a = &sym->attr;
12663 /* These symbols should never have a default initialization. */
12664 return !(
12665 a->allocatable
12666 || a->external
12667 || a->pointer
12668 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
12669 && (CLASS_DATA (sym)->attr.class_pointer
12670 || CLASS_DATA (sym)->attr.proc_pointer))
12671 || a->in_equivalence
12672 || a->in_common
12673 || a->data
12674 || sym->module
12675 || a->cray_pointee
12676 || a->cray_pointer
12677 || sym->assoc
12678 || (!a->referenced && !a->result)
12679 || (a->dummy && a->intent != INTENT_OUT)
12680 || (a->function && sym != sym->result)
12685 /* Assign the default initializer to a derived type variable or result. */
12687 static void
12688 apply_default_init (gfc_symbol *sym)
12690 gfc_expr *init = NULL;
12692 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
12693 return;
12695 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
12696 init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
12698 if (init == NULL && sym->ts.type != BT_CLASS)
12699 return;
12701 build_init_assign (sym, init);
12702 sym->attr.referenced = 1;
12706 /* Build an initializer for a local. Returns null if the symbol should not have
12707 a default initialization. */
12709 static gfc_expr *
12710 build_default_init_expr (gfc_symbol *sym)
12712 /* These symbols should never have a default initialization. */
12713 if (sym->attr.allocatable
12714 || sym->attr.external
12715 || sym->attr.dummy
12716 || sym->attr.pointer
12717 || sym->attr.in_equivalence
12718 || sym->attr.in_common
12719 || sym->attr.data
12720 || sym->module
12721 || sym->attr.cray_pointee
12722 || sym->attr.cray_pointer
12723 || sym->assoc)
12724 return NULL;
12726 /* Get the appropriate init expression. */
12727 return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
12730 /* Add an initialization expression to a local variable. */
12731 static void
12732 apply_default_init_local (gfc_symbol *sym)
12734 gfc_expr *init = NULL;
12736 /* The symbol should be a variable or a function return value. */
12737 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
12738 || (sym->attr.function && sym->result != sym))
12739 return;
12741 /* Try to build the initializer expression. If we can't initialize
12742 this symbol, then init will be NULL. */
12743 init = build_default_init_expr (sym);
12744 if (init == NULL)
12745 return;
12747 /* For saved variables, we don't want to add an initializer at function
12748 entry, so we just add a static initializer. Note that automatic variables
12749 are stack allocated even with -fno-automatic; we have also to exclude
12750 result variable, which are also nonstatic. */
12751 if (!sym->attr.automatic
12752 && (sym->attr.save || sym->ns->save_all
12753 || (flag_max_stack_var_size == 0 && !sym->attr.result
12754 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
12755 && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
12757 /* Don't clobber an existing initializer! */
12758 gcc_assert (sym->value == NULL);
12759 sym->value = init;
12760 return;
12763 build_init_assign (sym, init);
12767 /* Resolution of common features of flavors variable and procedure. */
12769 static bool
12770 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
12772 gfc_array_spec *as;
12774 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
12775 && sym->ts.u.derived && CLASS_DATA (sym))
12776 as = CLASS_DATA (sym)->as;
12777 else
12778 as = sym->as;
12780 /* Constraints on deferred shape variable. */
12781 if (as == NULL || as->type != AS_DEFERRED)
12783 bool pointer, allocatable, dimension;
12785 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
12786 && sym->ts.u.derived && CLASS_DATA (sym))
12788 pointer = CLASS_DATA (sym)->attr.class_pointer;
12789 allocatable = CLASS_DATA (sym)->attr.allocatable;
12790 dimension = CLASS_DATA (sym)->attr.dimension;
12792 else
12794 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
12795 allocatable = sym->attr.allocatable;
12796 dimension = sym->attr.dimension;
12799 if (allocatable)
12801 if (dimension && as->type != AS_ASSUMED_RANK)
12803 gfc_error ("Allocatable array %qs at %L must have a deferred "
12804 "shape or assumed rank", sym->name, &sym->declared_at);
12805 return false;
12807 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
12808 "%qs at %L may not be ALLOCATABLE",
12809 sym->name, &sym->declared_at))
12810 return false;
12813 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
12815 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
12816 "assumed rank", sym->name, &sym->declared_at);
12817 sym->error = 1;
12818 return false;
12821 else
12823 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
12824 && sym->ts.type != BT_CLASS && !sym->assoc)
12826 gfc_error ("Array %qs at %L cannot have a deferred shape",
12827 sym->name, &sym->declared_at);
12828 return false;
12832 /* Constraints on polymorphic variables. */
12833 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
12835 /* F03:C502. */
12836 if (sym->attr.class_ok
12837 && sym->ts.u.derived
12838 && !sym->attr.select_type_temporary
12839 && !UNLIMITED_POLY (sym)
12840 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
12842 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
12843 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
12844 &sym->declared_at);
12845 return false;
12848 /* F03:C509. */
12849 /* Assume that use associated symbols were checked in the module ns.
12850 Class-variables that are associate-names are also something special
12851 and excepted from the test. */
12852 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
12854 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
12855 "or pointer", sym->name, &sym->declared_at);
12856 return false;
12860 return true;
12864 /* Additional checks for symbols with flavor variable and derived
12865 type. To be called from resolve_fl_variable. */
12867 static bool
12868 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
12870 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
12872 /* Check to see if a derived type is blocked from being host
12873 associated by the presence of another class I symbol in the same
12874 namespace. 14.6.1.3 of the standard and the discussion on
12875 comp.lang.fortran. */
12876 if (sym->ts.u.derived
12877 && sym->ns != sym->ts.u.derived->ns
12878 && !sym->ts.u.derived->attr.use_assoc
12879 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
12881 gfc_symbol *s;
12882 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
12883 if (s && s->attr.generic)
12884 s = gfc_find_dt_in_generic (s);
12885 if (s && !gfc_fl_struct (s->attr.flavor))
12887 gfc_error ("The type %qs cannot be host associated at %L "
12888 "because it is blocked by an incompatible object "
12889 "of the same name declared at %L",
12890 sym->ts.u.derived->name, &sym->declared_at,
12891 &s->declared_at);
12892 return false;
12896 /* 4th constraint in section 11.3: "If an object of a type for which
12897 component-initialization is specified (R429) appears in the
12898 specification-part of a module and does not have the ALLOCATABLE
12899 or POINTER attribute, the object shall have the SAVE attribute."
12901 The check for initializers is performed with
12902 gfc_has_default_initializer because gfc_default_initializer generates
12903 a hidden default for allocatable components. */
12904 if (!(sym->value || no_init_flag) && sym->ns->proc_name
12905 && sym->ns->proc_name->attr.flavor == FL_MODULE
12906 && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
12907 && !sym->attr.pointer && !sym->attr.allocatable
12908 && gfc_has_default_initializer (sym->ts.u.derived)
12909 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
12910 "%qs at %L, needed due to the default "
12911 "initialization", sym->name, &sym->declared_at))
12912 return false;
12914 /* Assign default initializer. */
12915 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
12916 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
12917 sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
12919 return true;
12923 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
12924 except in the declaration of an entity or component that has the POINTER
12925 or ALLOCATABLE attribute. */
12927 static bool
12928 deferred_requirements (gfc_symbol *sym)
12930 if (sym->ts.deferred
12931 && !(sym->attr.pointer
12932 || sym->attr.allocatable
12933 || sym->attr.associate_var
12934 || sym->attr.omp_udr_artificial_var))
12936 /* If a function has a result variable, only check the variable. */
12937 if (sym->result && sym->name != sym->result->name)
12938 return true;
12940 gfc_error ("Entity %qs at %L has a deferred type parameter and "
12941 "requires either the POINTER or ALLOCATABLE attribute",
12942 sym->name, &sym->declared_at);
12943 return false;
12945 return true;
12949 /* Resolve symbols with flavor variable. */
12951 static bool
12952 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
12954 const char *auto_save_msg = "Automatic object %qs at %L cannot have the "
12955 "SAVE attribute";
12957 if (!resolve_fl_var_and_proc (sym, mp_flag))
12958 return false;
12960 /* Set this flag to check that variables are parameters of all entries.
12961 This check is effected by the call to gfc_resolve_expr through
12962 is_non_constant_shape_array. */
12963 bool saved_specification_expr = specification_expr;
12964 specification_expr = true;
12966 if (sym->ns->proc_name
12967 && (sym->ns->proc_name->attr.flavor == FL_MODULE
12968 || sym->ns->proc_name->attr.is_main_program)
12969 && !sym->attr.use_assoc
12970 && !sym->attr.allocatable
12971 && !sym->attr.pointer
12972 && is_non_constant_shape_array (sym))
12974 /* F08:C541. The shape of an array defined in a main program or module
12975 * needs to be constant. */
12976 gfc_error ("The module or main program array %qs at %L must "
12977 "have constant shape", sym->name, &sym->declared_at);
12978 specification_expr = saved_specification_expr;
12979 return false;
12982 /* Constraints on deferred type parameter. */
12983 if (!deferred_requirements (sym))
12984 return false;
12986 if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
12988 /* Make sure that character string variables with assumed length are
12989 dummy arguments. */
12990 gfc_expr *e = NULL;
12992 if (sym->ts.u.cl)
12993 e = sym->ts.u.cl->length;
12994 else
12995 return false;
12997 if (e == NULL && !sym->attr.dummy && !sym->attr.result
12998 && !sym->ts.deferred && !sym->attr.select_type_temporary
12999 && !sym->attr.omp_udr_artificial_var)
13001 gfc_error ("Entity with assumed character length at %L must be a "
13002 "dummy argument or a PARAMETER", &sym->declared_at);
13003 specification_expr = saved_specification_expr;
13004 return false;
13007 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
13009 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
13010 specification_expr = saved_specification_expr;
13011 return false;
13014 if (!gfc_is_constant_expr (e)
13015 && !(e->expr_type == EXPR_VARIABLE
13016 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
13018 if (!sym->attr.use_assoc && sym->ns->proc_name
13019 && (sym->ns->proc_name->attr.flavor == FL_MODULE
13020 || sym->ns->proc_name->attr.is_main_program))
13022 gfc_error ("%qs at %L must have constant character length "
13023 "in this context", sym->name, &sym->declared_at);
13024 specification_expr = saved_specification_expr;
13025 return false;
13027 if (sym->attr.in_common)
13029 gfc_error ("COMMON variable %qs at %L must have constant "
13030 "character length", sym->name, &sym->declared_at);
13031 specification_expr = saved_specification_expr;
13032 return false;
13037 if (sym->value == NULL && sym->attr.referenced)
13038 apply_default_init_local (sym); /* Try to apply a default initialization. */
13040 /* Determine if the symbol may not have an initializer. */
13041 int no_init_flag = 0, automatic_flag = 0;
13042 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
13043 || sym->attr.intrinsic || sym->attr.result)
13044 no_init_flag = 1;
13045 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
13046 && is_non_constant_shape_array (sym))
13048 no_init_flag = automatic_flag = 1;
13050 /* Also, they must not have the SAVE attribute.
13051 SAVE_IMPLICIT is checked below. */
13052 if (sym->as && sym->attr.codimension)
13054 int corank = sym->as->corank;
13055 sym->as->corank = 0;
13056 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
13057 sym->as->corank = corank;
13059 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
13061 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
13062 specification_expr = saved_specification_expr;
13063 return false;
13067 /* Ensure that any initializer is simplified. */
13068 if (sym->value)
13069 gfc_simplify_expr (sym->value, 1);
13071 /* Reject illegal initializers. */
13072 if (!sym->mark && sym->value)
13074 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
13075 && CLASS_DATA (sym)->attr.allocatable))
13076 gfc_error ("Allocatable %qs at %L cannot have an initializer",
13077 sym->name, &sym->declared_at);
13078 else if (sym->attr.external)
13079 gfc_error ("External %qs at %L cannot have an initializer",
13080 sym->name, &sym->declared_at);
13081 else if (sym->attr.dummy)
13082 gfc_error ("Dummy %qs at %L cannot have an initializer",
13083 sym->name, &sym->declared_at);
13084 else if (sym->attr.intrinsic)
13085 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
13086 sym->name, &sym->declared_at);
13087 else if (sym->attr.result)
13088 gfc_error ("Function result %qs at %L cannot have an initializer",
13089 sym->name, &sym->declared_at);
13090 else if (automatic_flag)
13091 gfc_error ("Automatic array %qs at %L cannot have an initializer",
13092 sym->name, &sym->declared_at);
13093 else
13094 goto no_init_error;
13095 specification_expr = saved_specification_expr;
13096 return false;
13099 no_init_error:
13100 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
13102 bool res = resolve_fl_variable_derived (sym, no_init_flag);
13103 specification_expr = saved_specification_expr;
13104 return res;
13107 specification_expr = saved_specification_expr;
13108 return true;
13112 /* Compare the dummy characteristics of a module procedure interface
13113 declaration with the corresponding declaration in a submodule. */
13114 static gfc_formal_arglist *new_formal;
13115 static char errmsg[200];
13117 static void
13118 compare_fsyms (gfc_symbol *sym)
13120 gfc_symbol *fsym;
13122 if (sym == NULL || new_formal == NULL)
13123 return;
13125 fsym = new_formal->sym;
13127 if (sym == fsym)
13128 return;
13130 if (strcmp (sym->name, fsym->name) == 0)
13132 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
13133 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
13138 /* Resolve a procedure. */
13140 static bool
13141 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
13143 gfc_formal_arglist *arg;
13144 bool allocatable_or_pointer;
13146 if (sym->attr.function
13147 && !resolve_fl_var_and_proc (sym, mp_flag))
13148 return false;
13150 /* Constraints on deferred type parameter. */
13151 if (!deferred_requirements (sym))
13152 return false;
13154 if (sym->ts.type == BT_CHARACTER)
13156 gfc_charlen *cl = sym->ts.u.cl;
13158 if (cl && cl->length && gfc_is_constant_expr (cl->length)
13159 && !resolve_charlen (cl))
13160 return false;
13162 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13163 && sym->attr.proc == PROC_ST_FUNCTION)
13165 gfc_error ("Character-valued statement function %qs at %L must "
13166 "have constant length", sym->name, &sym->declared_at);
13167 return false;
13171 /* Ensure that derived type for are not of a private type. Internal
13172 module procedures are excluded by 2.2.3.3 - i.e., they are not
13173 externally accessible and can access all the objects accessible in
13174 the host. */
13175 if (!(sym->ns->parent && sym->ns->parent->proc_name
13176 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
13177 && gfc_check_symbol_access (sym))
13179 gfc_interface *iface;
13181 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
13183 if (arg->sym
13184 && arg->sym->ts.type == BT_DERIVED
13185 && arg->sym->ts.u.derived
13186 && !arg->sym->ts.u.derived->attr.use_assoc
13187 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13188 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
13189 "and cannot be a dummy argument"
13190 " of %qs, which is PUBLIC at %L",
13191 arg->sym->name, sym->name,
13192 &sym->declared_at))
13194 /* Stop this message from recurring. */
13195 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13196 return false;
13200 /* PUBLIC interfaces may expose PRIVATE procedures that take types
13201 PRIVATE to the containing module. */
13202 for (iface = sym->generic; iface; iface = iface->next)
13204 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
13206 if (arg->sym
13207 && arg->sym->ts.type == BT_DERIVED
13208 && !arg->sym->ts.u.derived->attr.use_assoc
13209 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13210 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
13211 "PUBLIC interface %qs at %L "
13212 "takes dummy arguments of %qs which "
13213 "is PRIVATE", iface->sym->name,
13214 sym->name, &iface->sym->declared_at,
13215 gfc_typename(&arg->sym->ts)))
13217 /* Stop this message from recurring. */
13218 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13219 return false;
13225 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
13226 && !sym->attr.proc_pointer)
13228 gfc_error ("Function %qs at %L cannot have an initializer",
13229 sym->name, &sym->declared_at);
13231 /* Make sure no second error is issued for this. */
13232 sym->value->error = 1;
13233 return false;
13236 /* An external symbol may not have an initializer because it is taken to be
13237 a procedure. Exception: Procedure Pointers. */
13238 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
13240 gfc_error ("External object %qs at %L may not have an initializer",
13241 sym->name, &sym->declared_at);
13242 return false;
13245 /* An elemental function is required to return a scalar 12.7.1 */
13246 if (sym->attr.elemental && sym->attr.function
13247 && (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)))
13249 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
13250 "result", sym->name, &sym->declared_at);
13251 /* Reset so that the error only occurs once. */
13252 sym->attr.elemental = 0;
13253 return false;
13256 if (sym->attr.proc == PROC_ST_FUNCTION
13257 && (sym->attr.allocatable || sym->attr.pointer))
13259 gfc_error ("Statement function %qs at %L may not have pointer or "
13260 "allocatable attribute", sym->name, &sym->declared_at);
13261 return false;
13264 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
13265 char-len-param shall not be array-valued, pointer-valued, recursive
13266 or pure. ....snip... A character value of * may only be used in the
13267 following ways: (i) Dummy arg of procedure - dummy associates with
13268 actual length; (ii) To declare a named constant; or (iii) External
13269 function - but length must be declared in calling scoping unit. */
13270 if (sym->attr.function
13271 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
13272 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
13274 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
13275 || (sym->attr.recursive) || (sym->attr.pure))
13277 if (sym->as && sym->as->rank)
13278 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13279 "array-valued", sym->name, &sym->declared_at);
13281 if (sym->attr.pointer)
13282 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13283 "pointer-valued", sym->name, &sym->declared_at);
13285 if (sym->attr.pure)
13286 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13287 "pure", sym->name, &sym->declared_at);
13289 if (sym->attr.recursive)
13290 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13291 "recursive", sym->name, &sym->declared_at);
13293 return false;
13296 /* Appendix B.2 of the standard. Contained functions give an
13297 error anyway. Deferred character length is an F2003 feature.
13298 Don't warn on intrinsic conversion functions, which start
13299 with two underscores. */
13300 if (!sym->attr.contained && !sym->ts.deferred
13301 && (sym->name[0] != '_' || sym->name[1] != '_'))
13302 gfc_notify_std (GFC_STD_F95_OBS,
13303 "CHARACTER(*) function %qs at %L",
13304 sym->name, &sym->declared_at);
13307 /* F2008, C1218. */
13308 if (sym->attr.elemental)
13310 if (sym->attr.proc_pointer)
13312 const char* name = (sym->attr.result ? sym->ns->proc_name->name
13313 : sym->name);
13314 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
13315 name, &sym->declared_at);
13316 return false;
13318 if (sym->attr.dummy)
13320 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
13321 sym->name, &sym->declared_at);
13322 return false;
13326 /* F2018, C15100: "The result of an elemental function shall be scalar,
13327 and shall not have the POINTER or ALLOCATABLE attribute." The scalar
13328 pointer is tested and caught elsewhere. */
13329 if (sym->result)
13330 allocatable_or_pointer = sym->result->ts.type == BT_CLASS
13331 && CLASS_DATA (sym->result) ?
13332 (CLASS_DATA (sym->result)->attr.allocatable
13333 || CLASS_DATA (sym->result)->attr.pointer) :
13334 (sym->result->attr.allocatable
13335 || sym->result->attr.pointer);
13337 if (sym->attr.elemental && sym->result
13338 && allocatable_or_pointer)
13340 gfc_error ("Function result variable %qs at %L of elemental "
13341 "function %qs shall not have an ALLOCATABLE or POINTER "
13342 "attribute", sym->result->name,
13343 &sym->result->declared_at, sym->name);
13344 return false;
13347 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
13349 gfc_formal_arglist *curr_arg;
13350 int has_non_interop_arg = 0;
13352 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13353 sym->common_block))
13355 /* Clear these to prevent looking at them again if there was an
13356 error. */
13357 sym->attr.is_bind_c = 0;
13358 sym->attr.is_c_interop = 0;
13359 sym->ts.is_c_interop = 0;
13361 else
13363 /* So far, no errors have been found. */
13364 sym->attr.is_c_interop = 1;
13365 sym->ts.is_c_interop = 1;
13368 curr_arg = gfc_sym_get_dummy_args (sym);
13369 while (curr_arg != NULL)
13371 /* Skip implicitly typed dummy args here. */
13372 if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
13373 if (!gfc_verify_c_interop_param (curr_arg->sym))
13374 /* If something is found to fail, record the fact so we
13375 can mark the symbol for the procedure as not being
13376 BIND(C) to try and prevent multiple errors being
13377 reported. */
13378 has_non_interop_arg = 1;
13380 curr_arg = curr_arg->next;
13383 /* See if any of the arguments were not interoperable and if so, clear
13384 the procedure symbol to prevent duplicate error messages. */
13385 if (has_non_interop_arg != 0)
13387 sym->attr.is_c_interop = 0;
13388 sym->ts.is_c_interop = 0;
13389 sym->attr.is_bind_c = 0;
13393 if (!sym->attr.proc_pointer)
13395 if (sym->attr.save == SAVE_EXPLICIT)
13397 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
13398 "in %qs at %L", sym->name, &sym->declared_at);
13399 return false;
13401 if (sym->attr.intent)
13403 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
13404 "in %qs at %L", sym->name, &sym->declared_at);
13405 return false;
13407 if (sym->attr.subroutine && sym->attr.result)
13409 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
13410 "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
13411 return false;
13413 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
13414 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
13415 || sym->attr.contained))
13417 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
13418 "in %qs at %L", sym->name, &sym->declared_at);
13419 return false;
13421 if (strcmp ("ppr@", sym->name) == 0)
13423 gfc_error ("Procedure pointer result %qs at %L "
13424 "is missing the pointer attribute",
13425 sym->ns->proc_name->name, &sym->declared_at);
13426 return false;
13430 /* Assume that a procedure whose body is not known has references
13431 to external arrays. */
13432 if (sym->attr.if_source != IFSRC_DECL)
13433 sym->attr.array_outer_dependency = 1;
13435 /* Compare the characteristics of a module procedure with the
13436 interface declaration. Ideally this would be done with
13437 gfc_compare_interfaces but, at present, the formal interface
13438 cannot be copied to the ts.interface. */
13439 if (sym->attr.module_procedure
13440 && sym->attr.if_source == IFSRC_DECL)
13442 gfc_symbol *iface;
13443 char name[2*GFC_MAX_SYMBOL_LEN + 1];
13444 char *module_name;
13445 char *submodule_name;
13446 strcpy (name, sym->ns->proc_name->name);
13447 module_name = strtok (name, ".");
13448 submodule_name = strtok (NULL, ".");
13450 iface = sym->tlink;
13451 sym->tlink = NULL;
13453 /* Make sure that the result uses the correct charlen for deferred
13454 length results. */
13455 if (iface && sym->result
13456 && iface->ts.type == BT_CHARACTER
13457 && iface->ts.deferred)
13458 sym->result->ts.u.cl = iface->ts.u.cl;
13460 if (iface == NULL)
13461 goto check_formal;
13463 /* Check the procedure characteristics. */
13464 if (sym->attr.elemental != iface->attr.elemental)
13466 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
13467 "PROCEDURE at %L and its interface in %s",
13468 &sym->declared_at, module_name);
13469 return false;
13472 if (sym->attr.pure != iface->attr.pure)
13474 gfc_error ("Mismatch in PURE attribute between MODULE "
13475 "PROCEDURE at %L and its interface in %s",
13476 &sym->declared_at, module_name);
13477 return false;
13480 if (sym->attr.recursive != iface->attr.recursive)
13482 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
13483 "PROCEDURE at %L and its interface in %s",
13484 &sym->declared_at, module_name);
13485 return false;
13488 /* Check the result characteristics. */
13489 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
13491 gfc_error ("%s between the MODULE PROCEDURE declaration "
13492 "in MODULE %qs and the declaration at %L in "
13493 "(SUB)MODULE %qs",
13494 errmsg, module_name, &sym->declared_at,
13495 submodule_name ? submodule_name : module_name);
13496 return false;
13499 check_formal:
13500 /* Check the characteristics of the formal arguments. */
13501 if (sym->formal && sym->formal_ns)
13503 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
13505 new_formal = arg;
13506 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
13510 return true;
13514 /* Resolve a list of finalizer procedures. That is, after they have hopefully
13515 been defined and we now know their defined arguments, check that they fulfill
13516 the requirements of the standard for procedures used as finalizers. */
13518 static bool
13519 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
13521 gfc_finalizer* list;
13522 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
13523 bool result = true;
13524 bool seen_scalar = false;
13525 gfc_symbol *vtab;
13526 gfc_component *c;
13527 gfc_symbol *parent = gfc_get_derived_super_type (derived);
13529 if (parent)
13530 gfc_resolve_finalizers (parent, finalizable);
13532 /* Ensure that derived-type components have a their finalizers resolved. */
13533 bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
13534 for (c = derived->components; c; c = c->next)
13535 if (c->ts.type == BT_DERIVED
13536 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
13538 bool has_final2 = false;
13539 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
13540 return false; /* Error. */
13541 has_final = has_final || has_final2;
13543 /* Return early if not finalizable. */
13544 if (!has_final)
13546 if (finalizable)
13547 *finalizable = false;
13548 return true;
13551 /* Walk over the list of finalizer-procedures, check them, and if any one
13552 does not fit in with the standard's definition, print an error and remove
13553 it from the list. */
13554 prev_link = &derived->f2k_derived->finalizers;
13555 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
13557 gfc_formal_arglist *dummy_args;
13558 gfc_symbol* arg;
13559 gfc_finalizer* i;
13560 int my_rank;
13562 /* Skip this finalizer if we already resolved it. */
13563 if (list->proc_tree)
13565 if (list->proc_tree->n.sym->formal->sym->as == NULL
13566 || list->proc_tree->n.sym->formal->sym->as->rank == 0)
13567 seen_scalar = true;
13568 prev_link = &(list->next);
13569 continue;
13572 /* Check this exists and is a SUBROUTINE. */
13573 if (!list->proc_sym->attr.subroutine)
13575 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
13576 list->proc_sym->name, &list->where);
13577 goto error;
13580 /* We should have exactly one argument. */
13581 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
13582 if (!dummy_args || dummy_args->next)
13584 gfc_error ("FINAL procedure at %L must have exactly one argument",
13585 &list->where);
13586 goto error;
13588 arg = dummy_args->sym;
13590 /* This argument must be of our type. */
13591 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
13593 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
13594 &arg->declared_at, derived->name);
13595 goto error;
13598 /* It must neither be a pointer nor allocatable nor optional. */
13599 if (arg->attr.pointer)
13601 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
13602 &arg->declared_at);
13603 goto error;
13605 if (arg->attr.allocatable)
13607 gfc_error ("Argument of FINAL procedure at %L must not be"
13608 " ALLOCATABLE", &arg->declared_at);
13609 goto error;
13611 if (arg->attr.optional)
13613 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
13614 &arg->declared_at);
13615 goto error;
13618 /* It must not be INTENT(OUT). */
13619 if (arg->attr.intent == INTENT_OUT)
13621 gfc_error ("Argument of FINAL procedure at %L must not be"
13622 " INTENT(OUT)", &arg->declared_at);
13623 goto error;
13626 /* Warn if the procedure is non-scalar and not assumed shape. */
13627 if (warn_surprising && arg->as && arg->as->rank != 0
13628 && arg->as->type != AS_ASSUMED_SHAPE)
13629 gfc_warning (OPT_Wsurprising,
13630 "Non-scalar FINAL procedure at %L should have assumed"
13631 " shape argument", &arg->declared_at);
13633 /* Check that it does not match in kind and rank with a FINAL procedure
13634 defined earlier. To really loop over the *earlier* declarations,
13635 we need to walk the tail of the list as new ones were pushed at the
13636 front. */
13637 /* TODO: Handle kind parameters once they are implemented. */
13638 my_rank = (arg->as ? arg->as->rank : 0);
13639 for (i = list->next; i; i = i->next)
13641 gfc_formal_arglist *dummy_args;
13643 /* Argument list might be empty; that is an error signalled earlier,
13644 but we nevertheless continued resolving. */
13645 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
13646 if (dummy_args)
13648 gfc_symbol* i_arg = dummy_args->sym;
13649 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
13650 if (i_rank == my_rank)
13652 gfc_error ("FINAL procedure %qs declared at %L has the same"
13653 " rank (%d) as %qs",
13654 list->proc_sym->name, &list->where, my_rank,
13655 i->proc_sym->name);
13656 goto error;
13661 /* Is this the/a scalar finalizer procedure? */
13662 if (my_rank == 0)
13663 seen_scalar = true;
13665 /* Find the symtree for this procedure. */
13666 gcc_assert (!list->proc_tree);
13667 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
13669 prev_link = &list->next;
13670 continue;
13672 /* Remove wrong nodes immediately from the list so we don't risk any
13673 troubles in the future when they might fail later expectations. */
13674 error:
13675 i = list;
13676 *prev_link = list->next;
13677 gfc_free_finalizer (i);
13678 result = false;
13681 if (result == false)
13682 return false;
13684 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
13685 were nodes in the list, must have been for arrays. It is surely a good
13686 idea to have a scalar version there if there's something to finalize. */
13687 if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
13688 gfc_warning (OPT_Wsurprising,
13689 "Only array FINAL procedures declared for derived type %qs"
13690 " defined at %L, suggest also scalar one",
13691 derived->name, &derived->declared_at);
13693 vtab = gfc_find_derived_vtab (derived);
13694 c = vtab->ts.u.derived->components->next->next->next->next->next;
13695 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
13697 if (finalizable)
13698 *finalizable = true;
13700 return true;
13704 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
13706 static bool
13707 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
13708 const char* generic_name, locus where)
13710 gfc_symbol *sym1, *sym2;
13711 const char *pass1, *pass2;
13712 gfc_formal_arglist *dummy_args;
13714 gcc_assert (t1->specific && t2->specific);
13715 gcc_assert (!t1->specific->is_generic);
13716 gcc_assert (!t2->specific->is_generic);
13717 gcc_assert (t1->is_operator == t2->is_operator);
13719 sym1 = t1->specific->u.specific->n.sym;
13720 sym2 = t2->specific->u.specific->n.sym;
13722 if (sym1 == sym2)
13723 return true;
13725 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
13726 if (sym1->attr.subroutine != sym2->attr.subroutine
13727 || sym1->attr.function != sym2->attr.function)
13729 gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
13730 " GENERIC %qs at %L",
13731 sym1->name, sym2->name, generic_name, &where);
13732 return false;
13735 /* Determine PASS arguments. */
13736 if (t1->specific->nopass)
13737 pass1 = NULL;
13738 else if (t1->specific->pass_arg)
13739 pass1 = t1->specific->pass_arg;
13740 else
13742 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
13743 if (dummy_args)
13744 pass1 = dummy_args->sym->name;
13745 else
13746 pass1 = NULL;
13748 if (t2->specific->nopass)
13749 pass2 = NULL;
13750 else if (t2->specific->pass_arg)
13751 pass2 = t2->specific->pass_arg;
13752 else
13754 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
13755 if (dummy_args)
13756 pass2 = dummy_args->sym->name;
13757 else
13758 pass2 = NULL;
13761 /* Compare the interfaces. */
13762 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
13763 NULL, 0, pass1, pass2))
13765 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
13766 sym1->name, sym2->name, generic_name, &where);
13767 return false;
13770 return true;
13774 /* Worker function for resolving a generic procedure binding; this is used to
13775 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
13777 The difference between those cases is finding possible inherited bindings
13778 that are overridden, as one has to look for them in tb_sym_root,
13779 tb_uop_root or tb_op, respectively. Thus the caller must already find
13780 the super-type and set p->overridden correctly. */
13782 static bool
13783 resolve_tb_generic_targets (gfc_symbol* super_type,
13784 gfc_typebound_proc* p, const char* name)
13786 gfc_tbp_generic* target;
13787 gfc_symtree* first_target;
13788 gfc_symtree* inherited;
13790 gcc_assert (p && p->is_generic);
13792 /* Try to find the specific bindings for the symtrees in our target-list. */
13793 gcc_assert (p->u.generic);
13794 for (target = p->u.generic; target; target = target->next)
13795 if (!target->specific)
13797 gfc_typebound_proc* overridden_tbp;
13798 gfc_tbp_generic* g;
13799 const char* target_name;
13801 target_name = target->specific_st->name;
13803 /* Defined for this type directly. */
13804 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
13806 target->specific = target->specific_st->n.tb;
13807 goto specific_found;
13810 /* Look for an inherited specific binding. */
13811 if (super_type)
13813 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
13814 true, NULL);
13816 if (inherited)
13818 gcc_assert (inherited->n.tb);
13819 target->specific = inherited->n.tb;
13820 goto specific_found;
13824 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
13825 " at %L", target_name, name, &p->where);
13826 return false;
13828 /* Once we've found the specific binding, check it is not ambiguous with
13829 other specifics already found or inherited for the same GENERIC. */
13830 specific_found:
13831 gcc_assert (target->specific);
13833 /* This must really be a specific binding! */
13834 if (target->specific->is_generic)
13836 gfc_error ("GENERIC %qs at %L must target a specific binding,"
13837 " %qs is GENERIC, too", name, &p->where, target_name);
13838 return false;
13841 /* Check those already resolved on this type directly. */
13842 for (g = p->u.generic; g; g = g->next)
13843 if (g != target && g->specific
13844 && !check_generic_tbp_ambiguity (target, g, name, p->where))
13845 return false;
13847 /* Check for ambiguity with inherited specific targets. */
13848 for (overridden_tbp = p->overridden; overridden_tbp;
13849 overridden_tbp = overridden_tbp->overridden)
13850 if (overridden_tbp->is_generic)
13852 for (g = overridden_tbp->u.generic; g; g = g->next)
13854 gcc_assert (g->specific);
13855 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
13856 return false;
13861 /* If we attempt to "overwrite" a specific binding, this is an error. */
13862 if (p->overridden && !p->overridden->is_generic)
13864 gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
13865 " the same name", name, &p->where);
13866 return false;
13869 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
13870 all must have the same attributes here. */
13871 first_target = p->u.generic->specific->u.specific;
13872 gcc_assert (first_target);
13873 p->subroutine = first_target->n.sym->attr.subroutine;
13874 p->function = first_target->n.sym->attr.function;
13876 return true;
13880 /* Resolve a GENERIC procedure binding for a derived type. */
13882 static bool
13883 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
13885 gfc_symbol* super_type;
13887 /* Find the overridden binding if any. */
13888 st->n.tb->overridden = NULL;
13889 super_type = gfc_get_derived_super_type (derived);
13890 if (super_type)
13892 gfc_symtree* overridden;
13893 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
13894 true, NULL);
13896 if (overridden && overridden->n.tb)
13897 st->n.tb->overridden = overridden->n.tb;
13900 /* Resolve using worker function. */
13901 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
13905 /* Retrieve the target-procedure of an operator binding and do some checks in
13906 common for intrinsic and user-defined type-bound operators. */
13908 static gfc_symbol*
13909 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
13911 gfc_symbol* target_proc;
13913 gcc_assert (target->specific && !target->specific->is_generic);
13914 target_proc = target->specific->u.specific->n.sym;
13915 gcc_assert (target_proc);
13917 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
13918 if (target->specific->nopass)
13920 gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
13921 return NULL;
13924 return target_proc;
13928 /* Resolve a type-bound intrinsic operator. */
13930 static bool
13931 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
13932 gfc_typebound_proc* p)
13934 gfc_symbol* super_type;
13935 gfc_tbp_generic* target;
13937 /* If there's already an error here, do nothing (but don't fail again). */
13938 if (p->error)
13939 return true;
13941 /* Operators should always be GENERIC bindings. */
13942 gcc_assert (p->is_generic);
13944 /* Look for an overridden binding. */
13945 super_type = gfc_get_derived_super_type (derived);
13946 if (super_type && super_type->f2k_derived)
13947 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
13948 op, true, NULL);
13949 else
13950 p->overridden = NULL;
13952 /* Resolve general GENERIC properties using worker function. */
13953 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
13954 goto error;
13956 /* Check the targets to be procedures of correct interface. */
13957 for (target = p->u.generic; target; target = target->next)
13959 gfc_symbol* target_proc;
13961 target_proc = get_checked_tb_operator_target (target, p->where);
13962 if (!target_proc)
13963 goto error;
13965 if (!gfc_check_operator_interface (target_proc, op, p->where))
13966 goto error;
13968 /* Add target to non-typebound operator list. */
13969 if (!target->specific->deferred && !derived->attr.use_assoc
13970 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
13972 gfc_interface *head, *intr;
13974 /* Preempt 'gfc_check_new_interface' for submodules, where the
13975 mechanism for handling module procedures winds up resolving
13976 operator interfaces twice and would otherwise cause an error. */
13977 for (intr = derived->ns->op[op]; intr; intr = intr->next)
13978 if (intr->sym == target_proc
13979 && target_proc->attr.used_in_submodule)
13980 return true;
13982 if (!gfc_check_new_interface (derived->ns->op[op],
13983 target_proc, p->where))
13984 return false;
13985 head = derived->ns->op[op];
13986 intr = gfc_get_interface ();
13987 intr->sym = target_proc;
13988 intr->where = p->where;
13989 intr->next = head;
13990 derived->ns->op[op] = intr;
13994 return true;
13996 error:
13997 p->error = 1;
13998 return false;
14002 /* Resolve a type-bound user operator (tree-walker callback). */
14004 static gfc_symbol* resolve_bindings_derived;
14005 static bool resolve_bindings_result;
14007 static bool check_uop_procedure (gfc_symbol* sym, locus where);
14009 static void
14010 resolve_typebound_user_op (gfc_symtree* stree)
14012 gfc_symbol* super_type;
14013 gfc_tbp_generic* target;
14015 gcc_assert (stree && stree->n.tb);
14017 if (stree->n.tb->error)
14018 return;
14020 /* Operators should always be GENERIC bindings. */
14021 gcc_assert (stree->n.tb->is_generic);
14023 /* Find overridden procedure, if any. */
14024 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
14025 if (super_type && super_type->f2k_derived)
14027 gfc_symtree* overridden;
14028 overridden = gfc_find_typebound_user_op (super_type, NULL,
14029 stree->name, true, NULL);
14031 if (overridden && overridden->n.tb)
14032 stree->n.tb->overridden = overridden->n.tb;
14034 else
14035 stree->n.tb->overridden = NULL;
14037 /* Resolve basically using worker function. */
14038 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
14039 goto error;
14041 /* Check the targets to be functions of correct interface. */
14042 for (target = stree->n.tb->u.generic; target; target = target->next)
14044 gfc_symbol* target_proc;
14046 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
14047 if (!target_proc)
14048 goto error;
14050 if (!check_uop_procedure (target_proc, stree->n.tb->where))
14051 goto error;
14054 return;
14056 error:
14057 resolve_bindings_result = false;
14058 stree->n.tb->error = 1;
14062 /* Resolve the type-bound procedures for a derived type. */
14064 static void
14065 resolve_typebound_procedure (gfc_symtree* stree)
14067 gfc_symbol* proc;
14068 locus where;
14069 gfc_symbol* me_arg;
14070 gfc_symbol* super_type;
14071 gfc_component* comp;
14073 gcc_assert (stree);
14075 /* Undefined specific symbol from GENERIC target definition. */
14076 if (!stree->n.tb)
14077 return;
14079 if (stree->n.tb->error)
14080 return;
14082 /* If this is a GENERIC binding, use that routine. */
14083 if (stree->n.tb->is_generic)
14085 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
14086 goto error;
14087 return;
14090 /* Get the target-procedure to check it. */
14091 gcc_assert (!stree->n.tb->is_generic);
14092 gcc_assert (stree->n.tb->u.specific);
14093 proc = stree->n.tb->u.specific->n.sym;
14094 where = stree->n.tb->where;
14096 /* Default access should already be resolved from the parser. */
14097 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
14099 if (stree->n.tb->deferred)
14101 if (!check_proc_interface (proc, &where))
14102 goto error;
14104 else
14106 /* If proc has not been resolved at this point, proc->name may
14107 actually be a USE associated entity. See PR fortran/89647. */
14108 if (!proc->resolve_symbol_called
14109 && proc->attr.function == 0 && proc->attr.subroutine == 0)
14111 gfc_symbol *tmp;
14112 gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
14113 if (tmp && tmp->attr.use_assoc)
14115 proc->module = tmp->module;
14116 proc->attr.proc = tmp->attr.proc;
14117 proc->attr.function = tmp->attr.function;
14118 proc->attr.subroutine = tmp->attr.subroutine;
14119 proc->attr.use_assoc = tmp->attr.use_assoc;
14120 proc->ts = tmp->ts;
14121 proc->result = tmp->result;
14125 /* Check for F08:C465. */
14126 if ((!proc->attr.subroutine && !proc->attr.function)
14127 || (proc->attr.proc != PROC_MODULE
14128 && proc->attr.if_source != IFSRC_IFBODY
14129 && !proc->attr.module_procedure)
14130 || proc->attr.abstract)
14132 gfc_error ("%qs must be a module procedure or an external "
14133 "procedure with an explicit interface at %L",
14134 proc->name, &where);
14135 goto error;
14139 stree->n.tb->subroutine = proc->attr.subroutine;
14140 stree->n.tb->function = proc->attr.function;
14142 /* Find the super-type of the current derived type. We could do this once and
14143 store in a global if speed is needed, but as long as not I believe this is
14144 more readable and clearer. */
14145 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
14147 /* If PASS, resolve and check arguments if not already resolved / loaded
14148 from a .mod file. */
14149 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
14151 gfc_formal_arglist *dummy_args;
14153 dummy_args = gfc_sym_get_dummy_args (proc);
14154 if (stree->n.tb->pass_arg)
14156 gfc_formal_arglist *i;
14158 /* If an explicit passing argument name is given, walk the arg-list
14159 and look for it. */
14161 me_arg = NULL;
14162 stree->n.tb->pass_arg_num = 1;
14163 for (i = dummy_args; i; i = i->next)
14165 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
14167 me_arg = i->sym;
14168 break;
14170 ++stree->n.tb->pass_arg_num;
14173 if (!me_arg)
14175 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
14176 " argument %qs",
14177 proc->name, stree->n.tb->pass_arg, &where,
14178 stree->n.tb->pass_arg);
14179 goto error;
14182 else
14184 /* Otherwise, take the first one; there should in fact be at least
14185 one. */
14186 stree->n.tb->pass_arg_num = 1;
14187 if (!dummy_args)
14189 gfc_error ("Procedure %qs with PASS at %L must have at"
14190 " least one argument", proc->name, &where);
14191 goto error;
14193 me_arg = dummy_args->sym;
14196 /* Now check that the argument-type matches and the passed-object
14197 dummy argument is generally fine. */
14199 gcc_assert (me_arg);
14201 if (me_arg->ts.type != BT_CLASS)
14203 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14204 " at %L", proc->name, &where);
14205 goto error;
14208 if (CLASS_DATA (me_arg)->ts.u.derived
14209 != resolve_bindings_derived)
14211 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14212 " the derived-type %qs", me_arg->name, proc->name,
14213 me_arg->name, &where, resolve_bindings_derived->name);
14214 goto error;
14217 gcc_assert (me_arg->ts.type == BT_CLASS);
14218 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
14220 gfc_error ("Passed-object dummy argument of %qs at %L must be"
14221 " scalar", proc->name, &where);
14222 goto error;
14224 if (CLASS_DATA (me_arg)->attr.allocatable)
14226 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14227 " be ALLOCATABLE", proc->name, &where);
14228 goto error;
14230 if (CLASS_DATA (me_arg)->attr.class_pointer)
14232 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14233 " be POINTER", proc->name, &where);
14234 goto error;
14238 /* If we are extending some type, check that we don't override a procedure
14239 flagged NON_OVERRIDABLE. */
14240 stree->n.tb->overridden = NULL;
14241 if (super_type)
14243 gfc_symtree* overridden;
14244 overridden = gfc_find_typebound_proc (super_type, NULL,
14245 stree->name, true, NULL);
14247 if (overridden)
14249 if (overridden->n.tb)
14250 stree->n.tb->overridden = overridden->n.tb;
14252 if (!gfc_check_typebound_override (stree, overridden))
14253 goto error;
14257 /* See if there's a name collision with a component directly in this type. */
14258 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
14259 if (!strcmp (comp->name, stree->name))
14261 gfc_error ("Procedure %qs at %L has the same name as a component of"
14262 " %qs",
14263 stree->name, &where, resolve_bindings_derived->name);
14264 goto error;
14267 /* Try to find a name collision with an inherited component. */
14268 if (super_type && gfc_find_component (super_type, stree->name, true, true,
14269 NULL))
14271 gfc_error ("Procedure %qs at %L has the same name as an inherited"
14272 " component of %qs",
14273 stree->name, &where, resolve_bindings_derived->name);
14274 goto error;
14277 stree->n.tb->error = 0;
14278 return;
14280 error:
14281 resolve_bindings_result = false;
14282 stree->n.tb->error = 1;
14286 static bool
14287 resolve_typebound_procedures (gfc_symbol* derived)
14289 int op;
14290 gfc_symbol* super_type;
14292 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
14293 return true;
14295 super_type = gfc_get_derived_super_type (derived);
14296 if (super_type)
14297 resolve_symbol (super_type);
14299 resolve_bindings_derived = derived;
14300 resolve_bindings_result = true;
14302 if (derived->f2k_derived->tb_sym_root)
14303 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
14304 &resolve_typebound_procedure);
14306 if (derived->f2k_derived->tb_uop_root)
14307 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
14308 &resolve_typebound_user_op);
14310 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
14312 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
14313 if (p && !resolve_typebound_intrinsic_op (derived,
14314 (gfc_intrinsic_op)op, p))
14315 resolve_bindings_result = false;
14318 return resolve_bindings_result;
14322 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
14323 to give all identical derived types the same backend_decl. */
14324 static void
14325 add_dt_to_dt_list (gfc_symbol *derived)
14327 if (!derived->dt_next)
14329 if (gfc_derived_types)
14331 derived->dt_next = gfc_derived_types->dt_next;
14332 gfc_derived_types->dt_next = derived;
14334 else
14336 derived->dt_next = derived;
14338 gfc_derived_types = derived;
14343 /* Ensure that a derived-type is really not abstract, meaning that every
14344 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
14346 static bool
14347 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
14349 if (!st)
14350 return true;
14352 if (!ensure_not_abstract_walker (sub, st->left))
14353 return false;
14354 if (!ensure_not_abstract_walker (sub, st->right))
14355 return false;
14357 if (st->n.tb && st->n.tb->deferred)
14359 gfc_symtree* overriding;
14360 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
14361 if (!overriding)
14362 return false;
14363 gcc_assert (overriding->n.tb);
14364 if (overriding->n.tb->deferred)
14366 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
14367 " %qs is DEFERRED and not overridden",
14368 sub->name, &sub->declared_at, st->name);
14369 return false;
14373 return true;
14376 static bool
14377 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
14379 /* The algorithm used here is to recursively travel up the ancestry of sub
14380 and for each ancestor-type, check all bindings. If any of them is
14381 DEFERRED, look it up starting from sub and see if the found (overriding)
14382 binding is not DEFERRED.
14383 This is not the most efficient way to do this, but it should be ok and is
14384 clearer than something sophisticated. */
14386 gcc_assert (ancestor && !sub->attr.abstract);
14388 if (!ancestor->attr.abstract)
14389 return true;
14391 /* Walk bindings of this ancestor. */
14392 if (ancestor->f2k_derived)
14394 bool t;
14395 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
14396 if (!t)
14397 return false;
14400 /* Find next ancestor type and recurse on it. */
14401 ancestor = gfc_get_derived_super_type (ancestor);
14402 if (ancestor)
14403 return ensure_not_abstract (sub, ancestor);
14405 return true;
14409 /* This check for typebound defined assignments is done recursively
14410 since the order in which derived types are resolved is not always in
14411 order of the declarations. */
14413 static void
14414 check_defined_assignments (gfc_symbol *derived)
14416 gfc_component *c;
14418 for (c = derived->components; c; c = c->next)
14420 if (!gfc_bt_struct (c->ts.type)
14421 || c->attr.pointer
14422 || c->attr.allocatable
14423 || c->attr.proc_pointer_comp
14424 || c->attr.class_pointer
14425 || c->attr.proc_pointer)
14426 continue;
14428 if (c->ts.u.derived->attr.defined_assign_comp
14429 || (c->ts.u.derived->f2k_derived
14430 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
14432 derived->attr.defined_assign_comp = 1;
14433 return;
14436 check_defined_assignments (c->ts.u.derived);
14437 if (c->ts.u.derived->attr.defined_assign_comp)
14439 derived->attr.defined_assign_comp = 1;
14440 return;
14446 /* Resolve a single component of a derived type or structure. */
14448 static bool
14449 resolve_component (gfc_component *c, gfc_symbol *sym)
14451 gfc_symbol *super_type;
14452 symbol_attribute *attr;
14454 if (c->attr.artificial)
14455 return true;
14457 /* Do not allow vtype components to be resolved in nameless namespaces
14458 such as block data because the procedure pointers will cause ICEs
14459 and vtables are not needed in these contexts. */
14460 if (sym->attr.vtype && sym->attr.use_assoc
14461 && sym->ns->proc_name == NULL)
14462 return true;
14464 /* F2008, C442. */
14465 if ((!sym->attr.is_class || c != sym->components)
14466 && c->attr.codimension
14467 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
14469 gfc_error ("Coarray component %qs at %L must be allocatable with "
14470 "deferred shape", c->name, &c->loc);
14471 return false;
14474 /* F2008, C443. */
14475 if (c->attr.codimension && c->ts.type == BT_DERIVED
14476 && c->ts.u.derived->ts.is_iso_c)
14478 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14479 "shall not be a coarray", c->name, &c->loc);
14480 return false;
14483 /* F2008, C444. */
14484 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
14485 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
14486 || c->attr.allocatable))
14488 gfc_error ("Component %qs at %L with coarray component "
14489 "shall be a nonpointer, nonallocatable scalar",
14490 c->name, &c->loc);
14491 return false;
14494 /* F2008, C448. */
14495 if (c->ts.type == BT_CLASS)
14497 if (c->attr.class_ok && CLASS_DATA (c))
14499 attr = &(CLASS_DATA (c)->attr);
14501 /* Fix up contiguous attribute. */
14502 if (c->attr.contiguous)
14503 attr->contiguous = 1;
14505 else
14506 attr = NULL;
14508 else
14509 attr = &c->attr;
14511 if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
14513 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
14514 "is not an array pointer", c->name, &c->loc);
14515 return false;
14518 /* F2003, 15.2.1 - length has to be one. */
14519 if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
14520 && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
14521 || !gfc_is_constant_expr (c->ts.u.cl->length)
14522 || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
14524 gfc_error ("Component %qs of BIND(C) type at %L must have length one",
14525 c->name, &c->loc);
14526 return false;
14529 if (c->attr.proc_pointer && c->ts.interface)
14531 gfc_symbol *ifc = c->ts.interface;
14533 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
14535 c->tb->error = 1;
14536 return false;
14539 if (ifc->attr.if_source || ifc->attr.intrinsic)
14541 /* Resolve interface and copy attributes. */
14542 if (ifc->formal && !ifc->formal_ns)
14543 resolve_symbol (ifc);
14544 if (ifc->attr.intrinsic)
14545 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
14547 if (ifc->result)
14549 c->ts = ifc->result->ts;
14550 c->attr.allocatable = ifc->result->attr.allocatable;
14551 c->attr.pointer = ifc->result->attr.pointer;
14552 c->attr.dimension = ifc->result->attr.dimension;
14553 c->as = gfc_copy_array_spec (ifc->result->as);
14554 c->attr.class_ok = ifc->result->attr.class_ok;
14556 else
14558 c->ts = ifc->ts;
14559 c->attr.allocatable = ifc->attr.allocatable;
14560 c->attr.pointer = ifc->attr.pointer;
14561 c->attr.dimension = ifc->attr.dimension;
14562 c->as = gfc_copy_array_spec (ifc->as);
14563 c->attr.class_ok = ifc->attr.class_ok;
14565 c->ts.interface = ifc;
14566 c->attr.function = ifc->attr.function;
14567 c->attr.subroutine = ifc->attr.subroutine;
14569 c->attr.pure = ifc->attr.pure;
14570 c->attr.elemental = ifc->attr.elemental;
14571 c->attr.recursive = ifc->attr.recursive;
14572 c->attr.always_explicit = ifc->attr.always_explicit;
14573 c->attr.ext_attr |= ifc->attr.ext_attr;
14574 /* Copy char length. */
14575 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
14577 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
14578 if (cl->length && !cl->resolved
14579 && !gfc_resolve_expr (cl->length))
14581 c->tb->error = 1;
14582 return false;
14584 c->ts.u.cl = cl;
14588 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
14590 /* Since PPCs are not implicitly typed, a PPC without an explicit
14591 interface must be a subroutine. */
14592 gfc_add_subroutine (&c->attr, c->name, &c->loc);
14595 /* Procedure pointer components: Check PASS arg. */
14596 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
14597 && !sym->attr.vtype)
14599 gfc_symbol* me_arg;
14601 if (c->tb->pass_arg)
14603 gfc_formal_arglist* i;
14605 /* If an explicit passing argument name is given, walk the arg-list
14606 and look for it. */
14608 me_arg = NULL;
14609 c->tb->pass_arg_num = 1;
14610 for (i = c->ts.interface->formal; i; i = i->next)
14612 if (!strcmp (i->sym->name, c->tb->pass_arg))
14614 me_arg = i->sym;
14615 break;
14617 c->tb->pass_arg_num++;
14620 if (!me_arg)
14622 gfc_error ("Procedure pointer component %qs with PASS(%s) "
14623 "at %L has no argument %qs", c->name,
14624 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
14625 c->tb->error = 1;
14626 return false;
14629 else
14631 /* Otherwise, take the first one; there should in fact be at least
14632 one. */
14633 c->tb->pass_arg_num = 1;
14634 if (!c->ts.interface->formal)
14636 gfc_error ("Procedure pointer component %qs with PASS at %L "
14637 "must have at least one argument",
14638 c->name, &c->loc);
14639 c->tb->error = 1;
14640 return false;
14642 me_arg = c->ts.interface->formal->sym;
14645 /* Now check that the argument-type matches. */
14646 gcc_assert (me_arg);
14647 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
14648 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
14649 || (me_arg->ts.type == BT_CLASS
14650 && CLASS_DATA (me_arg)->ts.u.derived != sym))
14652 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14653 " the derived type %qs", me_arg->name, c->name,
14654 me_arg->name, &c->loc, sym->name);
14655 c->tb->error = 1;
14656 return false;
14659 /* Check for F03:C453. */
14660 if (CLASS_DATA (me_arg)->attr.dimension)
14662 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14663 "must be scalar", me_arg->name, c->name, me_arg->name,
14664 &c->loc);
14665 c->tb->error = 1;
14666 return false;
14669 if (CLASS_DATA (me_arg)->attr.class_pointer)
14671 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14672 "may not have the POINTER attribute", me_arg->name,
14673 c->name, me_arg->name, &c->loc);
14674 c->tb->error = 1;
14675 return false;
14678 if (CLASS_DATA (me_arg)->attr.allocatable)
14680 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14681 "may not be ALLOCATABLE", me_arg->name, c->name,
14682 me_arg->name, &c->loc);
14683 c->tb->error = 1;
14684 return false;
14687 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
14689 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14690 " at %L", c->name, &c->loc);
14691 return false;
14696 /* Check type-spec if this is not the parent-type component. */
14697 if (((sym->attr.is_class
14698 && (!sym->components->ts.u.derived->attr.extension
14699 || c != sym->components->ts.u.derived->components))
14700 || (!sym->attr.is_class
14701 && (!sym->attr.extension || c != sym->components)))
14702 && !sym->attr.vtype
14703 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
14704 return false;
14706 super_type = gfc_get_derived_super_type (sym);
14708 /* If this type is an extension, set the accessibility of the parent
14709 component. */
14710 if (super_type
14711 && ((sym->attr.is_class
14712 && c == sym->components->ts.u.derived->components)
14713 || (!sym->attr.is_class && c == sym->components))
14714 && strcmp (super_type->name, c->name) == 0)
14715 c->attr.access = super_type->attr.access;
14717 /* If this type is an extension, see if this component has the same name
14718 as an inherited type-bound procedure. */
14719 if (super_type && !sym->attr.is_class
14720 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
14722 gfc_error ("Component %qs of %qs at %L has the same name as an"
14723 " inherited type-bound procedure",
14724 c->name, sym->name, &c->loc);
14725 return false;
14728 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
14729 && !c->ts.deferred)
14731 if (c->ts.u.cl->length == NULL
14732 || (!resolve_charlen(c->ts.u.cl))
14733 || !gfc_is_constant_expr (c->ts.u.cl->length))
14735 gfc_error ("Character length of component %qs needs to "
14736 "be a constant specification expression at %L",
14737 c->name,
14738 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
14739 return false;
14743 if (c->ts.type == BT_CHARACTER && c->ts.deferred
14744 && !c->attr.pointer && !c->attr.allocatable)
14746 gfc_error ("Character component %qs of %qs at %L with deferred "
14747 "length must be a POINTER or ALLOCATABLE",
14748 c->name, sym->name, &c->loc);
14749 return false;
14752 /* Add the hidden deferred length field. */
14753 if (c->ts.type == BT_CHARACTER
14754 && (c->ts.deferred || c->attr.pdt_string)
14755 && !c->attr.function
14756 && !sym->attr.is_class)
14758 char name[GFC_MAX_SYMBOL_LEN+9];
14759 gfc_component *strlen;
14760 sprintf (name, "_%s_length", c->name);
14761 strlen = gfc_find_component (sym, name, true, true, NULL);
14762 if (strlen == NULL)
14764 if (!gfc_add_component (sym, name, &strlen))
14765 return false;
14766 strlen->ts.type = BT_INTEGER;
14767 strlen->ts.kind = gfc_charlen_int_kind;
14768 strlen->attr.access = ACCESS_PRIVATE;
14769 strlen->attr.artificial = 1;
14773 if (c->ts.type == BT_DERIVED
14774 && sym->component_access != ACCESS_PRIVATE
14775 && gfc_check_symbol_access (sym)
14776 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
14777 && !c->ts.u.derived->attr.use_assoc
14778 && !gfc_check_symbol_access (c->ts.u.derived)
14779 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
14780 "PRIVATE type and cannot be a component of "
14781 "%qs, which is PUBLIC at %L", c->name,
14782 sym->name, &sym->declared_at))
14783 return false;
14785 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
14787 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
14788 "type %s", c->name, &c->loc, sym->name);
14789 return false;
14792 if (sym->attr.sequence)
14794 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
14796 gfc_error ("Component %s of SEQUENCE type declared at %L does "
14797 "not have the SEQUENCE attribute",
14798 c->ts.u.derived->name, &sym->declared_at);
14799 return false;
14803 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
14804 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
14805 else if (c->ts.type == BT_CLASS && c->attr.class_ok
14806 && CLASS_DATA (c)->ts.u.derived->attr.generic)
14807 CLASS_DATA (c)->ts.u.derived
14808 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
14810 /* If an allocatable component derived type is of the same type as
14811 the enclosing derived type, we need a vtable generating so that
14812 the __deallocate procedure is created. */
14813 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
14814 && c->ts.u.derived == sym && c->attr.allocatable == 1)
14815 gfc_find_vtab (&c->ts);
14817 /* Ensure that all the derived type components are put on the
14818 derived type list; even in formal namespaces, where derived type
14819 pointer components might not have been declared. */
14820 if (c->ts.type == BT_DERIVED
14821 && c->ts.u.derived
14822 && c->ts.u.derived->components
14823 && c->attr.pointer
14824 && sym != c->ts.u.derived)
14825 add_dt_to_dt_list (c->ts.u.derived);
14827 if (c->as && c->as->type != AS_DEFERRED
14828 && (c->attr.pointer || c->attr.allocatable))
14829 return false;
14831 if (!gfc_resolve_array_spec (c->as,
14832 !(c->attr.pointer || c->attr.proc_pointer
14833 || c->attr.allocatable)))
14834 return false;
14836 if (c->initializer && !sym->attr.vtype
14837 && !c->attr.pdt_kind && !c->attr.pdt_len
14838 && !gfc_check_assign_symbol (sym, c, c->initializer))
14839 return false;
14841 return true;
14845 /* Be nice about the locus for a structure expression - show the locus of the
14846 first non-null sub-expression if we can. */
14848 static locus *
14849 cons_where (gfc_expr *struct_expr)
14851 gfc_constructor *cons;
14853 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
14855 cons = gfc_constructor_first (struct_expr->value.constructor);
14856 for (; cons; cons = gfc_constructor_next (cons))
14858 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
14859 return &cons->expr->where;
14862 return &struct_expr->where;
14865 /* Resolve the components of a structure type. Much less work than derived
14866 types. */
14868 static bool
14869 resolve_fl_struct (gfc_symbol *sym)
14871 gfc_component *c;
14872 gfc_expr *init = NULL;
14873 bool success;
14875 /* Make sure UNIONs do not have overlapping initializers. */
14876 if (sym->attr.flavor == FL_UNION)
14878 for (c = sym->components; c; c = c->next)
14880 if (init && c->initializer)
14882 gfc_error ("Conflicting initializers in union at %L and %L",
14883 cons_where (init), cons_where (c->initializer));
14884 gfc_free_expr (c->initializer);
14885 c->initializer = NULL;
14887 if (init == NULL)
14888 init = c->initializer;
14892 success = true;
14893 for (c = sym->components; c; c = c->next)
14894 if (!resolve_component (c, sym))
14895 success = false;
14897 if (!success)
14898 return false;
14900 if (sym->components)
14901 add_dt_to_dt_list (sym);
14903 return true;
14907 /* Resolve the components of a derived type. This does not have to wait until
14908 resolution stage, but can be done as soon as the dt declaration has been
14909 parsed. */
14911 static bool
14912 resolve_fl_derived0 (gfc_symbol *sym)
14914 gfc_symbol* super_type;
14915 gfc_component *c;
14916 gfc_formal_arglist *f;
14917 bool success;
14919 if (sym->attr.unlimited_polymorphic)
14920 return true;
14922 super_type = gfc_get_derived_super_type (sym);
14924 /* F2008, C432. */
14925 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
14927 gfc_error ("As extending type %qs at %L has a coarray component, "
14928 "parent type %qs shall also have one", sym->name,
14929 &sym->declared_at, super_type->name);
14930 return false;
14933 /* Ensure the extended type gets resolved before we do. */
14934 if (super_type && !resolve_fl_derived0 (super_type))
14935 return false;
14937 /* An ABSTRACT type must be extensible. */
14938 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
14940 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
14941 sym->name, &sym->declared_at);
14942 return false;
14945 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
14946 : sym->components;
14948 success = true;
14949 for ( ; c != NULL; c = c->next)
14950 if (!resolve_component (c, sym))
14951 success = false;
14953 if (!success)
14954 return false;
14956 /* Now add the caf token field, where needed. */
14957 if (flag_coarray != GFC_FCOARRAY_NONE
14958 && !sym->attr.is_class && !sym->attr.vtype)
14960 for (c = sym->components; c; c = c->next)
14961 if (!c->attr.dimension && !c->attr.codimension
14962 && (c->attr.allocatable || c->attr.pointer))
14964 char name[GFC_MAX_SYMBOL_LEN+9];
14965 gfc_component *token;
14966 sprintf (name, "_caf_%s", c->name);
14967 token = gfc_find_component (sym, name, true, true, NULL);
14968 if (token == NULL)
14970 if (!gfc_add_component (sym, name, &token))
14971 return false;
14972 token->ts.type = BT_VOID;
14973 token->ts.kind = gfc_default_integer_kind;
14974 token->attr.access = ACCESS_PRIVATE;
14975 token->attr.artificial = 1;
14976 token->attr.caf_token = 1;
14981 check_defined_assignments (sym);
14983 if (!sym->attr.defined_assign_comp && super_type)
14984 sym->attr.defined_assign_comp
14985 = super_type->attr.defined_assign_comp;
14987 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
14988 all DEFERRED bindings are overridden. */
14989 if (super_type && super_type->attr.abstract && !sym->attr.abstract
14990 && !sym->attr.is_class
14991 && !ensure_not_abstract (sym, super_type))
14992 return false;
14994 /* Check that there is a component for every PDT parameter. */
14995 if (sym->attr.pdt_template)
14997 for (f = sym->formal; f; f = f->next)
14999 if (!f->sym)
15000 continue;
15001 c = gfc_find_component (sym, f->sym->name, true, true, NULL);
15002 if (c == NULL)
15004 gfc_error ("Parameterized type %qs does not have a component "
15005 "corresponding to parameter %qs at %L", sym->name,
15006 f->sym->name, &sym->declared_at);
15007 break;
15012 /* Add derived type to the derived type list. */
15013 add_dt_to_dt_list (sym);
15015 return true;
15019 /* The following procedure does the full resolution of a derived type,
15020 including resolution of all type-bound procedures (if present). In contrast
15021 to 'resolve_fl_derived0' this can only be done after the module has been
15022 parsed completely. */
15024 static bool
15025 resolve_fl_derived (gfc_symbol *sym)
15027 gfc_symbol *gen_dt = NULL;
15029 if (sym->attr.unlimited_polymorphic)
15030 return true;
15032 if (!sym->attr.is_class)
15033 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
15034 if (gen_dt && gen_dt->generic && gen_dt->generic->next
15035 && (!gen_dt->generic->sym->attr.use_assoc
15036 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
15037 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
15038 "%qs at %L being the same name as derived "
15039 "type at %L", sym->name,
15040 gen_dt->generic->sym == sym
15041 ? gen_dt->generic->next->sym->name
15042 : gen_dt->generic->sym->name,
15043 gen_dt->generic->sym == sym
15044 ? &gen_dt->generic->next->sym->declared_at
15045 : &gen_dt->generic->sym->declared_at,
15046 &sym->declared_at))
15047 return false;
15049 if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
15051 gfc_error ("Derived type %qs at %L has not been declared",
15052 sym->name, &sym->declared_at);
15053 return false;
15056 /* Resolve the finalizer procedures. */
15057 if (!gfc_resolve_finalizers (sym, NULL))
15058 return false;
15060 if (sym->attr.is_class && sym->ts.u.derived == NULL)
15062 /* Fix up incomplete CLASS symbols. */
15063 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
15064 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
15066 /* Nothing more to do for unlimited polymorphic entities. */
15067 if (data->ts.u.derived->attr.unlimited_polymorphic)
15068 return true;
15069 else if (vptr->ts.u.derived == NULL)
15071 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
15072 gcc_assert (vtab);
15073 vptr->ts.u.derived = vtab->ts.u.derived;
15074 if (!resolve_fl_derived0 (vptr->ts.u.derived))
15075 return false;
15079 if (!resolve_fl_derived0 (sym))
15080 return false;
15082 /* Resolve the type-bound procedures. */
15083 if (!resolve_typebound_procedures (sym))
15084 return false;
15086 /* Generate module vtables subject to their accessibility and their not
15087 being vtables or pdt templates. If this is not done class declarations
15088 in external procedures wind up with their own version and so SELECT TYPE
15089 fails because the vptrs do not have the same address. */
15090 if (gfc_option.allow_std & GFC_STD_F2003
15091 && sym->ns->proc_name
15092 && sym->ns->proc_name->attr.flavor == FL_MODULE
15093 && sym->attr.access != ACCESS_PRIVATE
15094 && !(sym->attr.use_assoc || sym->attr.vtype || sym->attr.pdt_template))
15096 gfc_symbol *vtab = gfc_find_derived_vtab (sym);
15097 gfc_set_sym_referenced (vtab);
15100 return true;
15104 static bool
15105 resolve_fl_namelist (gfc_symbol *sym)
15107 gfc_namelist *nl;
15108 gfc_symbol *nlsym;
15110 for (nl = sym->namelist; nl; nl = nl->next)
15112 /* Check again, the check in match only works if NAMELIST comes
15113 after the decl. */
15114 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
15116 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
15117 "allowed", nl->sym->name, sym->name, &sym->declared_at);
15118 return false;
15121 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
15122 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
15123 "with assumed shape in namelist %qs at %L",
15124 nl->sym->name, sym->name, &sym->declared_at))
15125 return false;
15127 if (is_non_constant_shape_array (nl->sym)
15128 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
15129 "with nonconstant shape in namelist %qs at %L",
15130 nl->sym->name, sym->name, &sym->declared_at))
15131 return false;
15133 if (nl->sym->ts.type == BT_CHARACTER
15134 && (nl->sym->ts.u.cl->length == NULL
15135 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
15136 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
15137 "nonconstant character length in "
15138 "namelist %qs at %L", nl->sym->name,
15139 sym->name, &sym->declared_at))
15140 return false;
15144 /* Reject PRIVATE objects in a PUBLIC namelist. */
15145 if (gfc_check_symbol_access (sym))
15147 for (nl = sym->namelist; nl; nl = nl->next)
15149 if (!nl->sym->attr.use_assoc
15150 && !is_sym_host_assoc (nl->sym, sym->ns)
15151 && !gfc_check_symbol_access (nl->sym))
15153 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
15154 "cannot be member of PUBLIC namelist %qs at %L",
15155 nl->sym->name, sym->name, &sym->declared_at);
15156 return false;
15159 if (nl->sym->ts.type == BT_DERIVED
15160 && (nl->sym->ts.u.derived->attr.alloc_comp
15161 || nl->sym->ts.u.derived->attr.pointer_comp))
15163 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
15164 "namelist %qs at %L with ALLOCATABLE "
15165 "or POINTER components", nl->sym->name,
15166 sym->name, &sym->declared_at))
15167 return false;
15168 return true;
15171 /* Types with private components that came here by USE-association. */
15172 if (nl->sym->ts.type == BT_DERIVED
15173 && derived_inaccessible (nl->sym->ts.u.derived))
15175 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
15176 "components and cannot be member of namelist %qs at %L",
15177 nl->sym->name, sym->name, &sym->declared_at);
15178 return false;
15181 /* Types with private components that are defined in the same module. */
15182 if (nl->sym->ts.type == BT_DERIVED
15183 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
15184 && nl->sym->ts.u.derived->attr.private_comp)
15186 gfc_error ("NAMELIST object %qs has PRIVATE components and "
15187 "cannot be a member of PUBLIC namelist %qs at %L",
15188 nl->sym->name, sym->name, &sym->declared_at);
15189 return false;
15195 /* 14.1.2 A module or internal procedure represent local entities
15196 of the same type as a namelist member and so are not allowed. */
15197 for (nl = sym->namelist; nl; nl = nl->next)
15199 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
15200 continue;
15202 if (nl->sym->attr.function && nl->sym == nl->sym->result)
15203 if ((nl->sym == sym->ns->proc_name)
15205 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
15206 continue;
15208 nlsym = NULL;
15209 if (nl->sym->name)
15210 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
15211 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
15213 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
15214 "attribute in %qs at %L", nlsym->name,
15215 &sym->declared_at);
15216 return false;
15220 return true;
15224 static bool
15225 resolve_fl_parameter (gfc_symbol *sym)
15227 /* A parameter array's shape needs to be constant. */
15228 if (sym->as != NULL
15229 && (sym->as->type == AS_DEFERRED
15230 || is_non_constant_shape_array (sym)))
15232 gfc_error ("Parameter array %qs at %L cannot be automatic "
15233 "or of deferred shape", sym->name, &sym->declared_at);
15234 return false;
15237 /* Constraints on deferred type parameter. */
15238 if (!deferred_requirements (sym))
15239 return false;
15241 /* Make sure a parameter that has been implicitly typed still
15242 matches the implicit type, since PARAMETER statements can precede
15243 IMPLICIT statements. */
15244 if (sym->attr.implicit_type
15245 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
15246 sym->ns)))
15248 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
15249 "later IMPLICIT type", sym->name, &sym->declared_at);
15250 return false;
15253 /* Make sure the types of derived parameters are consistent. This
15254 type checking is deferred until resolution because the type may
15255 refer to a derived type from the host. */
15256 if (sym->ts.type == BT_DERIVED
15257 && !gfc_compare_types (&sym->ts, &sym->value->ts))
15259 gfc_error ("Incompatible derived type in PARAMETER at %L",
15260 &sym->value->where);
15261 return false;
15264 /* F03:C509,C514. */
15265 if (sym->ts.type == BT_CLASS)
15267 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
15268 sym->name, &sym->declared_at);
15269 return false;
15272 return true;
15276 /* Called by resolve_symbol to check PDTs. */
15278 static void
15279 resolve_pdt (gfc_symbol* sym)
15281 gfc_symbol *derived = NULL;
15282 gfc_actual_arglist *param;
15283 gfc_component *c;
15284 bool const_len_exprs = true;
15285 bool assumed_len_exprs = false;
15286 symbol_attribute *attr;
15288 if (sym->ts.type == BT_DERIVED)
15290 derived = sym->ts.u.derived;
15291 attr = &(sym->attr);
15293 else if (sym->ts.type == BT_CLASS)
15295 derived = CLASS_DATA (sym)->ts.u.derived;
15296 attr = &(CLASS_DATA (sym)->attr);
15298 else
15299 gcc_unreachable ();
15301 gcc_assert (derived->attr.pdt_type);
15303 for (param = sym->param_list; param; param = param->next)
15305 c = gfc_find_component (derived, param->name, false, true, NULL);
15306 gcc_assert (c);
15307 if (c->attr.pdt_kind)
15308 continue;
15310 if (param->expr && !gfc_is_constant_expr (param->expr)
15311 && c->attr.pdt_len)
15312 const_len_exprs = false;
15313 else if (param->spec_type == SPEC_ASSUMED)
15314 assumed_len_exprs = true;
15316 if (param->spec_type == SPEC_DEFERRED
15317 && !attr->allocatable && !attr->pointer)
15318 gfc_error ("The object %qs at %L has a deferred LEN "
15319 "parameter %qs and is neither allocatable "
15320 "nor a pointer", sym->name, &sym->declared_at,
15321 param->name);
15325 if (!const_len_exprs
15326 && (sym->ns->proc_name->attr.is_main_program
15327 || sym->ns->proc_name->attr.flavor == FL_MODULE
15328 || sym->attr.save != SAVE_NONE))
15329 gfc_error ("The AUTOMATIC object %qs at %L must not have the "
15330 "SAVE attribute or be a variable declared in the "
15331 "main program, a module or a submodule(F08/C513)",
15332 sym->name, &sym->declared_at);
15334 if (assumed_len_exprs && !(sym->attr.dummy
15335 || sym->attr.select_type_temporary || sym->attr.associate_var))
15336 gfc_error ("The object %qs at %L with ASSUMED type parameters "
15337 "must be a dummy or a SELECT TYPE selector(F08/4.2)",
15338 sym->name, &sym->declared_at);
15342 /* Do anything necessary to resolve a symbol. Right now, we just
15343 assume that an otherwise unknown symbol is a variable. This sort
15344 of thing commonly happens for symbols in module. */
15346 static void
15347 resolve_symbol (gfc_symbol *sym)
15349 int check_constant, mp_flag;
15350 gfc_symtree *symtree;
15351 gfc_symtree *this_symtree;
15352 gfc_namespace *ns;
15353 gfc_component *c;
15354 symbol_attribute class_attr;
15355 gfc_array_spec *as;
15356 bool saved_specification_expr;
15358 if (sym->resolve_symbol_called >= 1)
15359 return;
15360 sym->resolve_symbol_called = 1;
15362 /* No symbol will ever have union type; only components can be unions.
15363 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
15364 (just like derived type declaration symbols have flavor FL_DERIVED). */
15365 gcc_assert (sym->ts.type != BT_UNION);
15367 /* Coarrayed polymorphic objects with allocatable or pointer components are
15368 yet unsupported for -fcoarray=lib. */
15369 if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
15370 && sym->ts.u.derived && CLASS_DATA (sym)
15371 && CLASS_DATA (sym)->attr.codimension
15372 && CLASS_DATA (sym)->ts.u.derived
15373 && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
15374 || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
15376 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
15377 "type coarrays at %L are unsupported", &sym->declared_at);
15378 return;
15381 if (sym->attr.artificial)
15382 return;
15384 if (sym->attr.unlimited_polymorphic)
15385 return;
15387 if (sym->attr.flavor == FL_UNKNOWN
15388 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
15389 && !sym->attr.generic && !sym->attr.external
15390 && sym->attr.if_source == IFSRC_UNKNOWN
15391 && sym->ts.type == BT_UNKNOWN))
15394 /* If we find that a flavorless symbol is an interface in one of the
15395 parent namespaces, find its symtree in this namespace, free the
15396 symbol and set the symtree to point to the interface symbol. */
15397 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
15399 symtree = gfc_find_symtree (ns->sym_root, sym->name);
15400 if (symtree && (symtree->n.sym->generic ||
15401 (symtree->n.sym->attr.flavor == FL_PROCEDURE
15402 && sym->ns->construct_entities)))
15404 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
15405 sym->name);
15406 if (this_symtree->n.sym == sym)
15408 symtree->n.sym->refs++;
15409 gfc_release_symbol (sym);
15410 this_symtree->n.sym = symtree->n.sym;
15411 return;
15416 /* Otherwise give it a flavor according to such attributes as
15417 it has. */
15418 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
15419 && sym->attr.intrinsic == 0)
15420 sym->attr.flavor = FL_VARIABLE;
15421 else if (sym->attr.flavor == FL_UNKNOWN)
15423 sym->attr.flavor = FL_PROCEDURE;
15424 if (sym->attr.dimension)
15425 sym->attr.function = 1;
15429 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
15430 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
15432 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
15433 && !resolve_procedure_interface (sym))
15434 return;
15436 if (sym->attr.is_protected && !sym->attr.proc_pointer
15437 && (sym->attr.procedure || sym->attr.external))
15439 if (sym->attr.external)
15440 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
15441 "at %L", &sym->declared_at);
15442 else
15443 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
15444 "at %L", &sym->declared_at);
15446 return;
15449 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
15450 return;
15452 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
15453 && !resolve_fl_struct (sym))
15454 return;
15456 /* Symbols that are module procedures with results (functions) have
15457 the types and array specification copied for type checking in
15458 procedures that call them, as well as for saving to a module
15459 file. These symbols can't stand the scrutiny that their results
15460 can. */
15461 mp_flag = (sym->result != NULL && sym->result != sym);
15463 /* Make sure that the intrinsic is consistent with its internal
15464 representation. This needs to be done before assigning a default
15465 type to avoid spurious warnings. */
15466 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
15467 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
15468 return;
15470 /* Resolve associate names. */
15471 if (sym->assoc)
15472 resolve_assoc_var (sym, true);
15474 /* Assign default type to symbols that need one and don't have one. */
15475 if (sym->ts.type == BT_UNKNOWN)
15477 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
15479 gfc_set_default_type (sym, 1, NULL);
15482 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
15483 && !sym->attr.function && !sym->attr.subroutine
15484 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
15485 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
15487 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15489 /* The specific case of an external procedure should emit an error
15490 in the case that there is no implicit type. */
15491 if (!mp_flag)
15493 if (!sym->attr.mixed_entry_master)
15494 gfc_set_default_type (sym, sym->attr.external, NULL);
15496 else
15498 /* Result may be in another namespace. */
15499 resolve_symbol (sym->result);
15501 if (!sym->result->attr.proc_pointer)
15503 sym->ts = sym->result->ts;
15504 sym->as = gfc_copy_array_spec (sym->result->as);
15505 sym->attr.dimension = sym->result->attr.dimension;
15506 sym->attr.pointer = sym->result->attr.pointer;
15507 sym->attr.allocatable = sym->result->attr.allocatable;
15508 sym->attr.contiguous = sym->result->attr.contiguous;
15513 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15515 bool saved_specification_expr = specification_expr;
15516 bool saved_formal_arg_flag = formal_arg_flag;
15518 specification_expr = true;
15519 formal_arg_flag = true;
15520 gfc_resolve_array_spec (sym->result->as, false);
15521 formal_arg_flag = saved_formal_arg_flag;
15522 specification_expr = saved_specification_expr;
15525 if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived)
15527 as = CLASS_DATA (sym)->as;
15528 class_attr = CLASS_DATA (sym)->attr;
15529 class_attr.pointer = class_attr.class_pointer;
15531 else
15533 class_attr = sym->attr;
15534 as = sym->as;
15537 /* F2008, C530. */
15538 if (sym->attr.contiguous
15539 && (!class_attr.dimension
15540 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
15541 && !class_attr.pointer)))
15543 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
15544 "array pointer or an assumed-shape or assumed-rank array",
15545 sym->name, &sym->declared_at);
15546 return;
15549 /* Assumed size arrays and assumed shape arrays must be dummy
15550 arguments. Array-spec's of implied-shape should have been resolved to
15551 AS_EXPLICIT already. */
15553 if (as)
15555 /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
15556 specification expression. */
15557 if (as->type == AS_IMPLIED_SHAPE)
15559 int i;
15560 for (i=0; i<as->rank; i++)
15562 if (as->lower[i] != NULL && as->upper[i] == NULL)
15564 gfc_error ("Bad specification for assumed size array at %L",
15565 &as->lower[i]->where);
15566 return;
15569 gcc_unreachable();
15572 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
15573 || as->type == AS_ASSUMED_SHAPE)
15574 && !sym->attr.dummy && !sym->attr.select_type_temporary)
15576 if (as->type == AS_ASSUMED_SIZE)
15577 gfc_error ("Assumed size array at %L must be a dummy argument",
15578 &sym->declared_at);
15579 else
15580 gfc_error ("Assumed shape array at %L must be a dummy argument",
15581 &sym->declared_at);
15582 return;
15584 /* TS 29113, C535a. */
15585 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
15586 && !sym->attr.select_type_temporary
15587 && !(cs_base && cs_base->current
15588 && cs_base->current->op == EXEC_SELECT_RANK))
15590 gfc_error ("Assumed-rank array at %L must be a dummy argument",
15591 &sym->declared_at);
15592 return;
15594 if (as->type == AS_ASSUMED_RANK
15595 && (sym->attr.codimension || sym->attr.value))
15597 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
15598 "CODIMENSION attribute", &sym->declared_at);
15599 return;
15603 /* Make sure symbols with known intent or optional are really dummy
15604 variable. Because of ENTRY statement, this has to be deferred
15605 until resolution time. */
15607 if (!sym->attr.dummy
15608 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
15610 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
15611 return;
15614 if (sym->attr.value && !sym->attr.dummy)
15616 gfc_error ("%qs at %L cannot have the VALUE attribute because "
15617 "it is not a dummy argument", sym->name, &sym->declared_at);
15618 return;
15621 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
15623 gfc_charlen *cl = sym->ts.u.cl;
15624 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
15626 gfc_error ("Character dummy variable %qs at %L with VALUE "
15627 "attribute must have constant length",
15628 sym->name, &sym->declared_at);
15629 return;
15632 if (sym->ts.is_c_interop
15633 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
15635 gfc_error ("C interoperable character dummy variable %qs at %L "
15636 "with VALUE attribute must have length one",
15637 sym->name, &sym->declared_at);
15638 return;
15642 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
15643 && sym->ts.u.derived->attr.generic)
15645 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
15646 if (!sym->ts.u.derived)
15648 gfc_error ("The derived type %qs at %L is of type %qs, "
15649 "which has not been defined", sym->name,
15650 &sym->declared_at, sym->ts.u.derived->name);
15651 sym->ts.type = BT_UNKNOWN;
15652 return;
15656 /* Use the same constraints as TYPE(*), except for the type check
15657 and that only scalars and assumed-size arrays are permitted. */
15658 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
15660 if (!sym->attr.dummy)
15662 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15663 "a dummy argument", sym->name, &sym->declared_at);
15664 return;
15667 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
15668 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
15669 && sym->ts.type != BT_COMPLEX)
15671 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15672 "of type TYPE(*) or of an numeric intrinsic type",
15673 sym->name, &sym->declared_at);
15674 return;
15677 if (sym->attr.allocatable || sym->attr.codimension
15678 || sym->attr.pointer || sym->attr.value)
15680 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15681 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
15682 "attribute", sym->name, &sym->declared_at);
15683 return;
15686 if (sym->attr.intent == INTENT_OUT)
15688 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15689 "have the INTENT(OUT) attribute",
15690 sym->name, &sym->declared_at);
15691 return;
15693 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
15695 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
15696 "either be a scalar or an assumed-size array",
15697 sym->name, &sym->declared_at);
15698 return;
15701 /* Set the type to TYPE(*) and add a dimension(*) to ensure
15702 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
15703 packing. */
15704 sym->ts.type = BT_ASSUMED;
15705 sym->as = gfc_get_array_spec ();
15706 sym->as->type = AS_ASSUMED_SIZE;
15707 sym->as->rank = 1;
15708 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
15710 else if (sym->ts.type == BT_ASSUMED)
15712 /* TS 29113, C407a. */
15713 if (!sym->attr.dummy)
15715 gfc_error ("Assumed type of variable %s at %L is only permitted "
15716 "for dummy variables", sym->name, &sym->declared_at);
15717 return;
15719 if (sym->attr.allocatable || sym->attr.codimension
15720 || sym->attr.pointer || sym->attr.value)
15722 gfc_error ("Assumed-type variable %s at %L may not have the "
15723 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
15724 sym->name, &sym->declared_at);
15725 return;
15727 if (sym->attr.intent == INTENT_OUT)
15729 gfc_error ("Assumed-type variable %s at %L may not have the "
15730 "INTENT(OUT) attribute",
15731 sym->name, &sym->declared_at);
15732 return;
15734 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
15736 gfc_error ("Assumed-type variable %s at %L shall not be an "
15737 "explicit-shape array", sym->name, &sym->declared_at);
15738 return;
15742 /* If the symbol is marked as bind(c), that it is declared at module level
15743 scope and verify its type and kind. Do not do the latter for symbols
15744 that are implicitly typed because that is handled in
15745 gfc_set_default_type. Handle dummy arguments and procedure definitions
15746 separately. Also, anything that is use associated is not handled here
15747 but instead is handled in the module it is declared in. Finally, derived
15748 type definitions are allowed to be BIND(C) since that only implies that
15749 they're interoperable, and they are checked fully for interoperability
15750 when a variable is declared of that type. */
15751 if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
15752 && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
15753 && sym->attr.flavor != FL_DERIVED)
15755 bool t = true;
15757 /* First, make sure the variable is declared at the
15758 module-level scope (J3/04-007, Section 15.3). */
15759 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
15760 sym->attr.in_common == 0)
15762 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
15763 "is neither a COMMON block nor declared at the "
15764 "module level scope", sym->name, &(sym->declared_at));
15765 t = false;
15767 else if (sym->ts.type == BT_CHARACTER
15768 && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
15769 || !gfc_is_constant_expr (sym->ts.u.cl->length)
15770 || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
15772 gfc_error ("BIND(C) Variable %qs at %L must have length one",
15773 sym->name, &sym->declared_at);
15774 t = false;
15776 else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
15778 t = verify_com_block_vars_c_interop (sym->common_head);
15780 else if (sym->attr.implicit_type == 0)
15782 /* If type() declaration, we need to verify that the components
15783 of the given type are all C interoperable, etc. */
15784 if (sym->ts.type == BT_DERIVED &&
15785 sym->ts.u.derived->attr.is_c_interop != 1)
15787 /* Make sure the user marked the derived type as BIND(C). If
15788 not, call the verify routine. This could print an error
15789 for the derived type more than once if multiple variables
15790 of that type are declared. */
15791 if (sym->ts.u.derived->attr.is_bind_c != 1)
15792 verify_bind_c_derived_type (sym->ts.u.derived);
15793 t = false;
15796 /* Verify the variable itself as C interoperable if it
15797 is BIND(C). It is not possible for this to succeed if
15798 the verify_bind_c_derived_type failed, so don't have to handle
15799 any error returned by verify_bind_c_derived_type. */
15800 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
15801 sym->common_block);
15804 if (!t)
15806 /* clear the is_bind_c flag to prevent reporting errors more than
15807 once if something failed. */
15808 sym->attr.is_bind_c = 0;
15809 return;
15813 /* If a derived type symbol has reached this point, without its
15814 type being declared, we have an error. Notice that most
15815 conditions that produce undefined derived types have already
15816 been dealt with. However, the likes of:
15817 implicit type(t) (t) ..... call foo (t) will get us here if
15818 the type is not declared in the scope of the implicit
15819 statement. Change the type to BT_UNKNOWN, both because it is so
15820 and to prevent an ICE. */
15821 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
15822 && sym->ts.u.derived->components == NULL
15823 && !sym->ts.u.derived->attr.zero_comp)
15825 gfc_error ("The derived type %qs at %L is of type %qs, "
15826 "which has not been defined", sym->name,
15827 &sym->declared_at, sym->ts.u.derived->name);
15828 sym->ts.type = BT_UNKNOWN;
15829 return;
15832 /* Make sure that the derived type has been resolved and that the
15833 derived type is visible in the symbol's namespace, if it is a
15834 module function and is not PRIVATE. */
15835 if (sym->ts.type == BT_DERIVED
15836 && sym->ts.u.derived->attr.use_assoc
15837 && sym->ns->proc_name
15838 && sym->ns->proc_name->attr.flavor == FL_MODULE
15839 && !resolve_fl_derived (sym->ts.u.derived))
15840 return;
15842 /* Unless the derived-type declaration is use associated, Fortran 95
15843 does not allow public entries of private derived types.
15844 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
15845 161 in 95-006r3. */
15846 if (sym->ts.type == BT_DERIVED
15847 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
15848 && !sym->ts.u.derived->attr.use_assoc
15849 && gfc_check_symbol_access (sym)
15850 && !gfc_check_symbol_access (sym->ts.u.derived)
15851 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
15852 "derived type %qs",
15853 (sym->attr.flavor == FL_PARAMETER)
15854 ? "parameter" : "variable",
15855 sym->name, &sym->declared_at,
15856 sym->ts.u.derived->name))
15857 return;
15859 /* F2008, C1302. */
15860 if (sym->ts.type == BT_DERIVED
15861 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
15862 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
15863 || sym->ts.u.derived->attr.lock_comp)
15864 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
15866 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
15867 "type LOCK_TYPE must be a coarray", sym->name,
15868 &sym->declared_at);
15869 return;
15872 /* TS18508, C702/C703. */
15873 if (sym->ts.type == BT_DERIVED
15874 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
15875 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
15876 || sym->ts.u.derived->attr.event_comp)
15877 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
15879 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
15880 "type EVENT_TYPE must be a coarray", sym->name,
15881 &sym->declared_at);
15882 return;
15885 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
15886 default initialization is defined (5.1.2.4.4). */
15887 if (sym->ts.type == BT_DERIVED
15888 && sym->attr.dummy
15889 && sym->attr.intent == INTENT_OUT
15890 && sym->as
15891 && sym->as->type == AS_ASSUMED_SIZE)
15893 for (c = sym->ts.u.derived->components; c; c = c->next)
15895 if (c->initializer)
15897 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
15898 "ASSUMED SIZE and so cannot have a default initializer",
15899 sym->name, &sym->declared_at);
15900 return;
15905 /* F2008, C542. */
15906 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
15907 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
15909 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
15910 "INTENT(OUT)", sym->name, &sym->declared_at);
15911 return;
15914 /* TS18508. */
15915 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
15916 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
15918 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
15919 "INTENT(OUT)", sym->name, &sym->declared_at);
15920 return;
15923 /* F2008, C525. */
15924 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15925 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15926 && sym->ts.u.derived && CLASS_DATA (sym)
15927 && CLASS_DATA (sym)->attr.coarray_comp))
15928 || class_attr.codimension)
15929 && (sym->attr.result || sym->result == sym))
15931 gfc_error ("Function result %qs at %L shall not be a coarray or have "
15932 "a coarray component", sym->name, &sym->declared_at);
15933 return;
15936 /* F2008, C524. */
15937 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
15938 && sym->ts.u.derived->ts.is_iso_c)
15940 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
15941 "shall not be a coarray", sym->name, &sym->declared_at);
15942 return;
15945 /* F2008, C525. */
15946 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15947 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15948 && sym->ts.u.derived && CLASS_DATA (sym)
15949 && CLASS_DATA (sym)->attr.coarray_comp))
15950 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
15951 || class_attr.allocatable))
15953 gfc_error ("Variable %qs at %L with coarray component shall be a "
15954 "nonpointer, nonallocatable scalar, which is not a coarray",
15955 sym->name, &sym->declared_at);
15956 return;
15959 /* F2008, C526. The function-result case was handled above. */
15960 if (class_attr.codimension
15961 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
15962 || sym->attr.select_type_temporary
15963 || sym->attr.associate_var
15964 || (sym->ns->save_all && !sym->attr.automatic)
15965 || sym->ns->proc_name->attr.flavor == FL_MODULE
15966 || sym->ns->proc_name->attr.is_main_program
15967 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
15969 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
15970 "nor a dummy argument", sym->name, &sym->declared_at);
15971 return;
15973 /* F2008, C528. */
15974 else if (class_attr.codimension && !sym->attr.select_type_temporary
15975 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
15977 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
15978 "deferred shape", sym->name, &sym->declared_at);
15979 return;
15981 else if (class_attr.codimension && class_attr.allocatable && as
15982 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
15984 gfc_error ("Allocatable coarray variable %qs at %L must have "
15985 "deferred shape", sym->name, &sym->declared_at);
15986 return;
15989 /* F2008, C541. */
15990 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15991 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15992 && sym->ts.u.derived && CLASS_DATA (sym)
15993 && CLASS_DATA (sym)->attr.coarray_comp))
15994 || (class_attr.codimension && class_attr.allocatable))
15995 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
15997 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
15998 "allocatable coarray or have coarray components",
15999 sym->name, &sym->declared_at);
16000 return;
16003 if (class_attr.codimension && sym->attr.dummy
16004 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
16006 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
16007 "procedure %qs", sym->name, &sym->declared_at,
16008 sym->ns->proc_name->name);
16009 return;
16012 if (sym->ts.type == BT_LOGICAL
16013 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
16014 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
16015 && sym->ns->proc_name->attr.is_bind_c)))
16017 int i;
16018 for (i = 0; gfc_logical_kinds[i].kind; i++)
16019 if (gfc_logical_kinds[i].kind == sym->ts.kind)
16020 break;
16021 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
16022 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
16023 "%L with non-C_Bool kind in BIND(C) procedure "
16024 "%qs", sym->name, &sym->declared_at,
16025 sym->ns->proc_name->name))
16026 return;
16027 else if (!gfc_logical_kinds[i].c_bool
16028 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
16029 "%qs at %L with non-C_Bool kind in "
16030 "BIND(C) procedure %qs", sym->name,
16031 &sym->declared_at,
16032 sym->attr.function ? sym->name
16033 : sym->ns->proc_name->name))
16034 return;
16037 switch (sym->attr.flavor)
16039 case FL_VARIABLE:
16040 if (!resolve_fl_variable (sym, mp_flag))
16041 return;
16042 break;
16044 case FL_PROCEDURE:
16045 if (sym->formal && !sym->formal_ns)
16047 /* Check that none of the arguments are a namelist. */
16048 gfc_formal_arglist *formal = sym->formal;
16050 for (; formal; formal = formal->next)
16051 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
16053 gfc_error ("Namelist %qs cannot be an argument to "
16054 "subroutine or function at %L",
16055 formal->sym->name, &sym->declared_at);
16056 return;
16060 if (!resolve_fl_procedure (sym, mp_flag))
16061 return;
16062 break;
16064 case FL_NAMELIST:
16065 if (!resolve_fl_namelist (sym))
16066 return;
16067 break;
16069 case FL_PARAMETER:
16070 if (!resolve_fl_parameter (sym))
16071 return;
16072 break;
16074 default:
16075 break;
16078 /* Resolve array specifier. Check as well some constraints
16079 on COMMON blocks. */
16081 check_constant = sym->attr.in_common && !sym->attr.pointer;
16083 /* Set the formal_arg_flag so that check_conflict will not throw
16084 an error for host associated variables in the specification
16085 expression for an array_valued function. */
16086 if ((sym->attr.function || sym->attr.result) && sym->as)
16087 formal_arg_flag = true;
16089 saved_specification_expr = specification_expr;
16090 specification_expr = true;
16091 gfc_resolve_array_spec (sym->as, check_constant);
16092 specification_expr = saved_specification_expr;
16094 formal_arg_flag = false;
16096 /* Resolve formal namespaces. */
16097 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
16098 && !sym->attr.contained && !sym->attr.intrinsic)
16099 gfc_resolve (sym->formal_ns);
16101 /* Make sure the formal namespace is present. */
16102 if (sym->formal && !sym->formal_ns)
16104 gfc_formal_arglist *formal = sym->formal;
16105 while (formal && !formal->sym)
16106 formal = formal->next;
16108 if (formal)
16110 sym->formal_ns = formal->sym->ns;
16111 if (sym->formal_ns && sym->ns != formal->sym->ns)
16112 sym->formal_ns->refs++;
16116 /* Check threadprivate restrictions. */
16117 if (sym->attr.threadprivate
16118 && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
16119 && !(sym->ns->save_all && !sym->attr.automatic)
16120 && sym->module == NULL
16121 && (sym->ns->proc_name == NULL
16122 || (sym->ns->proc_name->attr.flavor != FL_MODULE
16123 && !sym->ns->proc_name->attr.is_main_program)))
16124 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
16126 /* Check omp declare target restrictions. */
16127 if (sym->attr.omp_declare_target
16128 && sym->attr.flavor == FL_VARIABLE
16129 && !sym->attr.save
16130 && !(sym->ns->save_all && !sym->attr.automatic)
16131 && (!sym->attr.in_common
16132 && sym->module == NULL
16133 && (sym->ns->proc_name == NULL
16134 || (sym->ns->proc_name->attr.flavor != FL_MODULE
16135 && !sym->ns->proc_name->attr.is_main_program))))
16136 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
16137 sym->name, &sym->declared_at);
16139 /* If we have come this far we can apply default-initializers, as
16140 described in 14.7.5, to those variables that have not already
16141 been assigned one. */
16142 if (sym->ts.type == BT_DERIVED
16143 && !sym->value
16144 && !sym->attr.allocatable
16145 && !sym->attr.alloc_comp)
16147 symbol_attribute *a = &sym->attr;
16149 if ((!a->save && !a->dummy && !a->pointer
16150 && !a->in_common && !a->use_assoc
16151 && a->referenced
16152 && !((a->function || a->result)
16153 && (!a->dimension
16154 || sym->ts.u.derived->attr.alloc_comp
16155 || sym->ts.u.derived->attr.pointer_comp))
16156 && !(a->function && sym != sym->result))
16157 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
16158 apply_default_init (sym);
16159 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
16160 && (sym->ts.u.derived->attr.alloc_comp
16161 || sym->ts.u.derived->attr.pointer_comp))
16162 /* Mark the result symbol to be referenced, when it has allocatable
16163 components. */
16164 sym->result->attr.referenced = 1;
16167 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
16168 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
16169 && !CLASS_DATA (sym)->attr.class_pointer
16170 && !CLASS_DATA (sym)->attr.allocatable)
16171 apply_default_init (sym);
16173 /* If this symbol has a type-spec, check it. */
16174 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
16175 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
16176 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
16177 return;
16179 if (sym->param_list)
16180 resolve_pdt (sym);
16184 /************* Resolve DATA statements *************/
16186 static struct
16188 gfc_data_value *vnode;
16189 mpz_t left;
16191 values;
16194 /* Advance the values structure to point to the next value in the data list. */
16196 static bool
16197 next_data_value (void)
16199 while (mpz_cmp_ui (values.left, 0) == 0)
16202 if (values.vnode->next == NULL)
16203 return false;
16205 values.vnode = values.vnode->next;
16206 mpz_set (values.left, values.vnode->repeat);
16209 return true;
16213 static bool
16214 check_data_variable (gfc_data_variable *var, locus *where)
16216 gfc_expr *e;
16217 mpz_t size;
16218 mpz_t offset;
16219 bool t;
16220 ar_type mark = AR_UNKNOWN;
16221 int i;
16222 mpz_t section_index[GFC_MAX_DIMENSIONS];
16223 gfc_ref *ref;
16224 gfc_array_ref *ar;
16225 gfc_symbol *sym;
16226 int has_pointer;
16228 if (!gfc_resolve_expr (var->expr))
16229 return false;
16231 ar = NULL;
16232 mpz_init_set_si (offset, 0);
16233 e = var->expr;
16235 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
16236 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
16237 e = e->value.function.actual->expr;
16239 if (e->expr_type != EXPR_VARIABLE)
16241 gfc_error ("Expecting definable entity near %L", where);
16242 return false;
16245 sym = e->symtree->n.sym;
16247 if (sym->ns->is_block_data && !sym->attr.in_common)
16249 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
16250 sym->name, &sym->declared_at);
16251 return false;
16254 if (e->ref == NULL && sym->as)
16256 gfc_error ("DATA array %qs at %L must be specified in a previous"
16257 " declaration", sym->name, where);
16258 return false;
16261 if (gfc_is_coindexed (e))
16263 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
16264 where);
16265 return false;
16268 has_pointer = sym->attr.pointer;
16270 for (ref = e->ref; ref; ref = ref->next)
16272 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
16273 has_pointer = 1;
16275 if (has_pointer)
16277 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
16279 gfc_error ("DATA element %qs at %L is a pointer and so must "
16280 "be a full array", sym->name, where);
16281 return false;
16284 if (values.vnode->expr->expr_type == EXPR_CONSTANT)
16286 gfc_error ("DATA object near %L has the pointer attribute "
16287 "and the corresponding DATA value is not a valid "
16288 "initial-data-target", where);
16289 return false;
16293 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
16295 gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
16296 "attribute", ref->u.c.component->name, &e->where);
16297 return false;
16301 if (e->rank == 0 || has_pointer)
16303 mpz_init_set_ui (size, 1);
16304 ref = NULL;
16306 else
16308 ref = e->ref;
16310 /* Find the array section reference. */
16311 for (ref = e->ref; ref; ref = ref->next)
16313 if (ref->type != REF_ARRAY)
16314 continue;
16315 if (ref->u.ar.type == AR_ELEMENT)
16316 continue;
16317 break;
16319 gcc_assert (ref);
16321 /* Set marks according to the reference pattern. */
16322 switch (ref->u.ar.type)
16324 case AR_FULL:
16325 mark = AR_FULL;
16326 break;
16328 case AR_SECTION:
16329 ar = &ref->u.ar;
16330 /* Get the start position of array section. */
16331 gfc_get_section_index (ar, section_index, &offset);
16332 mark = AR_SECTION;
16333 break;
16335 default:
16336 gcc_unreachable ();
16339 if (!gfc_array_size (e, &size))
16341 gfc_error ("Nonconstant array section at %L in DATA statement",
16342 where);
16343 mpz_clear (offset);
16344 return false;
16348 t = true;
16350 while (mpz_cmp_ui (size, 0) > 0)
16352 if (!next_data_value ())
16354 gfc_error ("DATA statement at %L has more variables than values",
16355 where);
16356 t = false;
16357 break;
16360 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
16361 if (!t)
16362 break;
16364 /* If we have more than one element left in the repeat count,
16365 and we have more than one element left in the target variable,
16366 then create a range assignment. */
16367 /* FIXME: Only done for full arrays for now, since array sections
16368 seem tricky. */
16369 if (mark == AR_FULL && ref && ref->next == NULL
16370 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
16372 mpz_t range;
16374 if (mpz_cmp (size, values.left) >= 0)
16376 mpz_init_set (range, values.left);
16377 mpz_sub (size, size, values.left);
16378 mpz_set_ui (values.left, 0);
16380 else
16382 mpz_init_set (range, size);
16383 mpz_sub (values.left, values.left, size);
16384 mpz_set_ui (size, 0);
16387 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16388 offset, &range);
16390 mpz_add (offset, offset, range);
16391 mpz_clear (range);
16393 if (!t)
16394 break;
16397 /* Assign initial value to symbol. */
16398 else
16400 mpz_sub_ui (values.left, values.left, 1);
16401 mpz_sub_ui (size, size, 1);
16403 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16404 offset, NULL);
16405 if (!t)
16406 break;
16408 if (mark == AR_FULL)
16409 mpz_add_ui (offset, offset, 1);
16411 /* Modify the array section indexes and recalculate the offset
16412 for next element. */
16413 else if (mark == AR_SECTION)
16414 gfc_advance_section (section_index, ar, &offset);
16418 if (mark == AR_SECTION)
16420 for (i = 0; i < ar->dimen; i++)
16421 mpz_clear (section_index[i]);
16424 mpz_clear (size);
16425 mpz_clear (offset);
16427 return t;
16431 static bool traverse_data_var (gfc_data_variable *, locus *);
16433 /* Iterate over a list of elements in a DATA statement. */
16435 static bool
16436 traverse_data_list (gfc_data_variable *var, locus *where)
16438 mpz_t trip;
16439 iterator_stack frame;
16440 gfc_expr *e, *start, *end, *step;
16441 bool retval = true;
16443 mpz_init (frame.value);
16444 mpz_init (trip);
16446 start = gfc_copy_expr (var->iter.start);
16447 end = gfc_copy_expr (var->iter.end);
16448 step = gfc_copy_expr (var->iter.step);
16450 if (!gfc_simplify_expr (start, 1)
16451 || start->expr_type != EXPR_CONSTANT)
16453 gfc_error ("start of implied-do loop at %L could not be "
16454 "simplified to a constant value", &start->where);
16455 retval = false;
16456 goto cleanup;
16458 if (!gfc_simplify_expr (end, 1)
16459 || end->expr_type != EXPR_CONSTANT)
16461 gfc_error ("end of implied-do loop at %L could not be "
16462 "simplified to a constant value", &end->where);
16463 retval = false;
16464 goto cleanup;
16466 if (!gfc_simplify_expr (step, 1)
16467 || step->expr_type != EXPR_CONSTANT)
16469 gfc_error ("step of implied-do loop at %L could not be "
16470 "simplified to a constant value", &step->where);
16471 retval = false;
16472 goto cleanup;
16474 if (mpz_cmp_si (step->value.integer, 0) == 0)
16476 gfc_error ("step of implied-do loop at %L shall not be zero",
16477 &step->where);
16478 retval = false;
16479 goto cleanup;
16482 mpz_set (trip, end->value.integer);
16483 mpz_sub (trip, trip, start->value.integer);
16484 mpz_add (trip, trip, step->value.integer);
16486 mpz_div (trip, trip, step->value.integer);
16488 mpz_set (frame.value, start->value.integer);
16490 frame.prev = iter_stack;
16491 frame.variable = var->iter.var->symtree;
16492 iter_stack = &frame;
16494 while (mpz_cmp_ui (trip, 0) > 0)
16496 if (!traverse_data_var (var->list, where))
16498 retval = false;
16499 goto cleanup;
16502 e = gfc_copy_expr (var->expr);
16503 if (!gfc_simplify_expr (e, 1))
16505 gfc_free_expr (e);
16506 retval = false;
16507 goto cleanup;
16510 mpz_add (frame.value, frame.value, step->value.integer);
16512 mpz_sub_ui (trip, trip, 1);
16515 cleanup:
16516 mpz_clear (frame.value);
16517 mpz_clear (trip);
16519 gfc_free_expr (start);
16520 gfc_free_expr (end);
16521 gfc_free_expr (step);
16523 iter_stack = frame.prev;
16524 return retval;
16528 /* Type resolve variables in the variable list of a DATA statement. */
16530 static bool
16531 traverse_data_var (gfc_data_variable *var, locus *where)
16533 bool t;
16535 for (; var; var = var->next)
16537 if (var->expr == NULL)
16538 t = traverse_data_list (var, where);
16539 else
16540 t = check_data_variable (var, where);
16542 if (!t)
16543 return false;
16546 return true;
16550 /* Resolve the expressions and iterators associated with a data statement.
16551 This is separate from the assignment checking because data lists should
16552 only be resolved once. */
16554 static bool
16555 resolve_data_variables (gfc_data_variable *d)
16557 for (; d; d = d->next)
16559 if (d->list == NULL)
16561 if (!gfc_resolve_expr (d->expr))
16562 return false;
16564 else
16566 if (!gfc_resolve_iterator (&d->iter, false, true))
16567 return false;
16569 if (!resolve_data_variables (d->list))
16570 return false;
16574 return true;
16578 /* Resolve a single DATA statement. We implement this by storing a pointer to
16579 the value list into static variables, and then recursively traversing the
16580 variables list, expanding iterators and such. */
16582 static void
16583 resolve_data (gfc_data *d)
16586 if (!resolve_data_variables (d->var))
16587 return;
16589 values.vnode = d->value;
16590 if (d->value == NULL)
16591 mpz_set_ui (values.left, 0);
16592 else
16593 mpz_set (values.left, d->value->repeat);
16595 if (!traverse_data_var (d->var, &d->where))
16596 return;
16598 /* At this point, we better not have any values left. */
16600 if (next_data_value ())
16601 gfc_error ("DATA statement at %L has more values than variables",
16602 &d->where);
16606 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
16607 accessed by host or use association, is a dummy argument to a pure function,
16608 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
16609 is storage associated with any such variable, shall not be used in the
16610 following contexts: (clients of this function). */
16612 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
16613 procedure. Returns zero if assignment is OK, nonzero if there is a
16614 problem. */
16616 gfc_impure_variable (gfc_symbol *sym)
16618 gfc_symbol *proc;
16619 gfc_namespace *ns;
16621 if (sym->attr.use_assoc || sym->attr.in_common)
16622 return 1;
16624 /* Check if the symbol's ns is inside the pure procedure. */
16625 for (ns = gfc_current_ns; ns; ns = ns->parent)
16627 if (ns == sym->ns)
16628 break;
16629 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
16630 return 1;
16633 proc = sym->ns->proc_name;
16634 if (sym->attr.dummy
16635 && !sym->attr.value
16636 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
16637 || proc->attr.function))
16638 return 1;
16640 /* TODO: Sort out what can be storage associated, if anything, and include
16641 it here. In principle equivalences should be scanned but it does not
16642 seem to be possible to storage associate an impure variable this way. */
16643 return 0;
16647 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
16648 current namespace is inside a pure procedure. */
16651 gfc_pure (gfc_symbol *sym)
16653 symbol_attribute attr;
16654 gfc_namespace *ns;
16656 if (sym == NULL)
16658 /* Check if the current namespace or one of its parents
16659 belongs to a pure procedure. */
16660 for (ns = gfc_current_ns; ns; ns = ns->parent)
16662 sym = ns->proc_name;
16663 if (sym == NULL)
16664 return 0;
16665 attr = sym->attr;
16666 if (attr.flavor == FL_PROCEDURE && attr.pure)
16667 return 1;
16669 return 0;
16672 attr = sym->attr;
16674 return attr.flavor == FL_PROCEDURE && attr.pure;
16678 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
16679 checks if the current namespace is implicitly pure. Note that this
16680 function returns false for a PURE procedure. */
16683 gfc_implicit_pure (gfc_symbol *sym)
16685 gfc_namespace *ns;
16687 if (sym == NULL)
16689 /* Check if the current procedure is implicit_pure. Walk up
16690 the procedure list until we find a procedure. */
16691 for (ns = gfc_current_ns; ns; ns = ns->parent)
16693 sym = ns->proc_name;
16694 if (sym == NULL)
16695 return 0;
16697 if (sym->attr.flavor == FL_PROCEDURE)
16698 break;
16702 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
16703 && !sym->attr.pure;
16707 void
16708 gfc_unset_implicit_pure (gfc_symbol *sym)
16710 gfc_namespace *ns;
16712 if (sym == NULL)
16714 /* Check if the current procedure is implicit_pure. Walk up
16715 the procedure list until we find a procedure. */
16716 for (ns = gfc_current_ns; ns; ns = ns->parent)
16718 sym = ns->proc_name;
16719 if (sym == NULL)
16720 return;
16722 if (sym->attr.flavor == FL_PROCEDURE)
16723 break;
16727 if (sym->attr.flavor == FL_PROCEDURE)
16728 sym->attr.implicit_pure = 0;
16729 else
16730 sym->attr.pure = 0;
16734 /* Test whether the current procedure is elemental or not. */
16737 gfc_elemental (gfc_symbol *sym)
16739 symbol_attribute attr;
16741 if (sym == NULL)
16742 sym = gfc_current_ns->proc_name;
16743 if (sym == NULL)
16744 return 0;
16745 attr = sym->attr;
16747 return attr.flavor == FL_PROCEDURE && attr.elemental;
16751 /* Warn about unused labels. */
16753 static void
16754 warn_unused_fortran_label (gfc_st_label *label)
16756 if (label == NULL)
16757 return;
16759 warn_unused_fortran_label (label->left);
16761 if (label->defined == ST_LABEL_UNKNOWN)
16762 return;
16764 switch (label->referenced)
16766 case ST_LABEL_UNKNOWN:
16767 gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
16768 label->value, &label->where);
16769 break;
16771 case ST_LABEL_BAD_TARGET:
16772 gfc_warning (OPT_Wunused_label,
16773 "Label %d at %L defined but cannot be used",
16774 label->value, &label->where);
16775 break;
16777 default:
16778 break;
16781 warn_unused_fortran_label (label->right);
16785 /* Returns the sequence type of a symbol or sequence. */
16787 static seq_type
16788 sequence_type (gfc_typespec ts)
16790 seq_type result;
16791 gfc_component *c;
16793 switch (ts.type)
16795 case BT_DERIVED:
16797 if (ts.u.derived->components == NULL)
16798 return SEQ_NONDEFAULT;
16800 result = sequence_type (ts.u.derived->components->ts);
16801 for (c = ts.u.derived->components->next; c; c = c->next)
16802 if (sequence_type (c->ts) != result)
16803 return SEQ_MIXED;
16805 return result;
16807 case BT_CHARACTER:
16808 if (ts.kind != gfc_default_character_kind)
16809 return SEQ_NONDEFAULT;
16811 return SEQ_CHARACTER;
16813 case BT_INTEGER:
16814 if (ts.kind != gfc_default_integer_kind)
16815 return SEQ_NONDEFAULT;
16817 return SEQ_NUMERIC;
16819 case BT_REAL:
16820 if (!(ts.kind == gfc_default_real_kind
16821 || ts.kind == gfc_default_double_kind))
16822 return SEQ_NONDEFAULT;
16824 return SEQ_NUMERIC;
16826 case BT_COMPLEX:
16827 if (ts.kind != gfc_default_complex_kind)
16828 return SEQ_NONDEFAULT;
16830 return SEQ_NUMERIC;
16832 case BT_LOGICAL:
16833 if (ts.kind != gfc_default_logical_kind)
16834 return SEQ_NONDEFAULT;
16836 return SEQ_NUMERIC;
16838 default:
16839 return SEQ_NONDEFAULT;
16844 /* Resolve derived type EQUIVALENCE object. */
16846 static bool
16847 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
16849 gfc_component *c = derived->components;
16851 if (!derived)
16852 return true;
16854 /* Shall not be an object of nonsequence derived type. */
16855 if (!derived->attr.sequence)
16857 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
16858 "attribute to be an EQUIVALENCE object", sym->name,
16859 &e->where);
16860 return false;
16863 /* Shall not have allocatable components. */
16864 if (derived->attr.alloc_comp)
16866 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
16867 "components to be an EQUIVALENCE object",sym->name,
16868 &e->where);
16869 return false;
16872 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
16874 gfc_error ("Derived type variable %qs at %L with default "
16875 "initialization cannot be in EQUIVALENCE with a variable "
16876 "in COMMON", sym->name, &e->where);
16877 return false;
16880 for (; c ; c = c->next)
16882 if (gfc_bt_struct (c->ts.type)
16883 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
16884 return false;
16886 /* Shall not be an object of sequence derived type containing a pointer
16887 in the structure. */
16888 if (c->attr.pointer)
16890 gfc_error ("Derived type variable %qs at %L with pointer "
16891 "component(s) cannot be an EQUIVALENCE object",
16892 sym->name, &e->where);
16893 return false;
16896 return true;
16900 /* Resolve equivalence object.
16901 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
16902 an allocatable array, an object of nonsequence derived type, an object of
16903 sequence derived type containing a pointer at any level of component
16904 selection, an automatic object, a function name, an entry name, a result
16905 name, a named constant, a structure component, or a subobject of any of
16906 the preceding objects. A substring shall not have length zero. A
16907 derived type shall not have components with default initialization nor
16908 shall two objects of an equivalence group be initialized.
16909 Either all or none of the objects shall have an protected attribute.
16910 The simple constraints are done in symbol.c(check_conflict) and the rest
16911 are implemented here. */
16913 static void
16914 resolve_equivalence (gfc_equiv *eq)
16916 gfc_symbol *sym;
16917 gfc_symbol *first_sym;
16918 gfc_expr *e;
16919 gfc_ref *r;
16920 locus *last_where = NULL;
16921 seq_type eq_type, last_eq_type;
16922 gfc_typespec *last_ts;
16923 int object, cnt_protected;
16924 const char *msg;
16926 last_ts = &eq->expr->symtree->n.sym->ts;
16928 first_sym = eq->expr->symtree->n.sym;
16930 cnt_protected = 0;
16932 for (object = 1; eq; eq = eq->eq, object++)
16934 e = eq->expr;
16936 e->ts = e->symtree->n.sym->ts;
16937 /* match_varspec might not know yet if it is seeing
16938 array reference or substring reference, as it doesn't
16939 know the types. */
16940 if (e->ref && e->ref->type == REF_ARRAY)
16942 gfc_ref *ref = e->ref;
16943 sym = e->symtree->n.sym;
16945 if (sym->attr.dimension)
16947 ref->u.ar.as = sym->as;
16948 ref = ref->next;
16951 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
16952 if (e->ts.type == BT_CHARACTER
16953 && ref
16954 && ref->type == REF_ARRAY
16955 && ref->u.ar.dimen == 1
16956 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
16957 && ref->u.ar.stride[0] == NULL)
16959 gfc_expr *start = ref->u.ar.start[0];
16960 gfc_expr *end = ref->u.ar.end[0];
16961 void *mem = NULL;
16963 /* Optimize away the (:) reference. */
16964 if (start == NULL && end == NULL)
16966 if (e->ref == ref)
16967 e->ref = ref->next;
16968 else
16969 e->ref->next = ref->next;
16970 mem = ref;
16972 else
16974 ref->type = REF_SUBSTRING;
16975 if (start == NULL)
16976 start = gfc_get_int_expr (gfc_charlen_int_kind,
16977 NULL, 1);
16978 ref->u.ss.start = start;
16979 if (end == NULL && e->ts.u.cl)
16980 end = gfc_copy_expr (e->ts.u.cl->length);
16981 ref->u.ss.end = end;
16982 ref->u.ss.length = e->ts.u.cl;
16983 e->ts.u.cl = NULL;
16985 ref = ref->next;
16986 free (mem);
16989 /* Any further ref is an error. */
16990 if (ref)
16992 gcc_assert (ref->type == REF_ARRAY);
16993 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
16994 &ref->u.ar.where);
16995 continue;
16999 if (!gfc_resolve_expr (e))
17000 continue;
17002 sym = e->symtree->n.sym;
17004 if (sym->attr.is_protected)
17005 cnt_protected++;
17006 if (cnt_protected > 0 && cnt_protected != object)
17008 gfc_error ("Either all or none of the objects in the "
17009 "EQUIVALENCE set at %L shall have the "
17010 "PROTECTED attribute",
17011 &e->where);
17012 break;
17015 /* Shall not equivalence common block variables in a PURE procedure. */
17016 if (sym->ns->proc_name
17017 && sym->ns->proc_name->attr.pure
17018 && sym->attr.in_common)
17020 /* Need to check for symbols that may have entered the pure
17021 procedure via a USE statement. */
17022 bool saw_sym = false;
17023 if (sym->ns->use_stmts)
17025 gfc_use_rename *r;
17026 for (r = sym->ns->use_stmts->rename; r; r = r->next)
17027 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
17029 else
17030 saw_sym = true;
17032 if (saw_sym)
17033 gfc_error ("COMMON block member %qs at %L cannot be an "
17034 "EQUIVALENCE object in the pure procedure %qs",
17035 sym->name, &e->where, sym->ns->proc_name->name);
17036 break;
17039 /* Shall not be a named constant. */
17040 if (e->expr_type == EXPR_CONSTANT)
17042 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
17043 "object", sym->name, &e->where);
17044 continue;
17047 if (e->ts.type == BT_DERIVED
17048 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
17049 continue;
17051 /* Check that the types correspond correctly:
17052 Note 5.28:
17053 A numeric sequence structure may be equivalenced to another sequence
17054 structure, an object of default integer type, default real type, double
17055 precision real type, default logical type such that components of the
17056 structure ultimately only become associated to objects of the same
17057 kind. A character sequence structure may be equivalenced to an object
17058 of default character kind or another character sequence structure.
17059 Other objects may be equivalenced only to objects of the same type and
17060 kind parameters. */
17062 /* Identical types are unconditionally OK. */
17063 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
17064 goto identical_types;
17066 last_eq_type = sequence_type (*last_ts);
17067 eq_type = sequence_type (sym->ts);
17069 /* Since the pair of objects is not of the same type, mixed or
17070 non-default sequences can be rejected. */
17072 msg = "Sequence %s with mixed components in EQUIVALENCE "
17073 "statement at %L with different type objects";
17074 if ((object ==2
17075 && last_eq_type == SEQ_MIXED
17076 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
17077 || (eq_type == SEQ_MIXED
17078 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
17079 continue;
17081 msg = "Non-default type object or sequence %s in EQUIVALENCE "
17082 "statement at %L with objects of different type";
17083 if ((object ==2
17084 && last_eq_type == SEQ_NONDEFAULT
17085 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
17086 || (eq_type == SEQ_NONDEFAULT
17087 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
17088 continue;
17090 msg ="Non-CHARACTER object %qs in default CHARACTER "
17091 "EQUIVALENCE statement at %L";
17092 if (last_eq_type == SEQ_CHARACTER
17093 && eq_type != SEQ_CHARACTER
17094 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
17095 continue;
17097 msg ="Non-NUMERIC object %qs in default NUMERIC "
17098 "EQUIVALENCE statement at %L";
17099 if (last_eq_type == SEQ_NUMERIC
17100 && eq_type != SEQ_NUMERIC
17101 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
17102 continue;
17104 identical_types:
17106 last_ts =&sym->ts;
17107 last_where = &e->where;
17109 if (!e->ref)
17110 continue;
17112 /* Shall not be an automatic array. */
17113 if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
17115 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
17116 "an EQUIVALENCE object", sym->name, &e->where);
17117 continue;
17120 r = e->ref;
17121 while (r)
17123 /* Shall not be a structure component. */
17124 if (r->type == REF_COMPONENT)
17126 gfc_error ("Structure component %qs at %L cannot be an "
17127 "EQUIVALENCE object",
17128 r->u.c.component->name, &e->where);
17129 break;
17132 /* A substring shall not have length zero. */
17133 if (r->type == REF_SUBSTRING)
17135 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
17137 gfc_error ("Substring at %L has length zero",
17138 &r->u.ss.start->where);
17139 break;
17142 r = r->next;
17148 /* Function called by resolve_fntype to flag other symbols used in the
17149 length type parameter specification of function results. */
17151 static bool
17152 flag_fn_result_spec (gfc_expr *expr,
17153 gfc_symbol *sym,
17154 int *f ATTRIBUTE_UNUSED)
17156 gfc_namespace *ns;
17157 gfc_symbol *s;
17159 if (expr->expr_type == EXPR_VARIABLE)
17161 s = expr->symtree->n.sym;
17162 for (ns = s->ns; ns; ns = ns->parent)
17163 if (!ns->parent)
17164 break;
17166 if (sym == s)
17168 gfc_error ("Self reference in character length expression "
17169 "for %qs at %L", sym->name, &expr->where);
17170 return true;
17173 if (!s->fn_result_spec
17174 && s->attr.flavor == FL_PARAMETER)
17176 /* Function contained in a module.... */
17177 if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
17179 gfc_symtree *st;
17180 s->fn_result_spec = 1;
17181 /* Make sure that this symbol is translated as a module
17182 variable. */
17183 st = gfc_get_unique_symtree (ns);
17184 st->n.sym = s;
17185 s->refs++;
17187 /* ... which is use associated and called. */
17188 else if (s->attr.use_assoc || s->attr.used_in_submodule
17190 /* External function matched with an interface. */
17191 (s->ns->proc_name
17192 && ((s->ns == ns
17193 && s->ns->proc_name->attr.if_source == IFSRC_DECL)
17194 || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
17195 && s->ns->proc_name->attr.function))
17196 s->fn_result_spec = 1;
17199 return false;
17203 /* Resolve function and ENTRY types, issue diagnostics if needed. */
17205 static void
17206 resolve_fntype (gfc_namespace *ns)
17208 gfc_entry_list *el;
17209 gfc_symbol *sym;
17211 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
17212 return;
17214 /* If there are any entries, ns->proc_name is the entry master
17215 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
17216 if (ns->entries)
17217 sym = ns->entries->sym;
17218 else
17219 sym = ns->proc_name;
17220 if (sym->result == sym
17221 && sym->ts.type == BT_UNKNOWN
17222 && !gfc_set_default_type (sym, 0, NULL)
17223 && !sym->attr.untyped)
17225 gfc_error ("Function %qs at %L has no IMPLICIT type",
17226 sym->name, &sym->declared_at);
17227 sym->attr.untyped = 1;
17230 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
17231 && !sym->attr.contained
17232 && !gfc_check_symbol_access (sym->ts.u.derived)
17233 && gfc_check_symbol_access (sym))
17235 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
17236 "%L of PRIVATE type %qs", sym->name,
17237 &sym->declared_at, sym->ts.u.derived->name);
17240 if (ns->entries)
17241 for (el = ns->entries->next; el; el = el->next)
17243 if (el->sym->result == el->sym
17244 && el->sym->ts.type == BT_UNKNOWN
17245 && !gfc_set_default_type (el->sym, 0, NULL)
17246 && !el->sym->attr.untyped)
17248 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
17249 el->sym->name, &el->sym->declared_at);
17250 el->sym->attr.untyped = 1;
17254 if (sym->ts.type == BT_CHARACTER)
17255 gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
17259 /* 12.3.2.1.1 Defined operators. */
17261 static bool
17262 check_uop_procedure (gfc_symbol *sym, locus where)
17264 gfc_formal_arglist *formal;
17266 if (!sym->attr.function)
17268 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
17269 sym->name, &where);
17270 return false;
17273 if (sym->ts.type == BT_CHARACTER
17274 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
17275 && !(sym->result && ((sym->result->ts.u.cl
17276 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
17278 gfc_error ("User operator procedure %qs at %L cannot be assumed "
17279 "character length", sym->name, &where);
17280 return false;
17283 formal = gfc_sym_get_dummy_args (sym);
17284 if (!formal || !formal->sym)
17286 gfc_error ("User operator procedure %qs at %L must have at least "
17287 "one argument", sym->name, &where);
17288 return false;
17291 if (formal->sym->attr.intent != INTENT_IN)
17293 gfc_error ("First argument of operator interface at %L must be "
17294 "INTENT(IN)", &where);
17295 return false;
17298 if (formal->sym->attr.optional)
17300 gfc_error ("First argument of operator interface at %L cannot be "
17301 "optional", &where);
17302 return false;
17305 formal = formal->next;
17306 if (!formal || !formal->sym)
17307 return true;
17309 if (formal->sym->attr.intent != INTENT_IN)
17311 gfc_error ("Second argument of operator interface at %L must be "
17312 "INTENT(IN)", &where);
17313 return false;
17316 if (formal->sym->attr.optional)
17318 gfc_error ("Second argument of operator interface at %L cannot be "
17319 "optional", &where);
17320 return false;
17323 if (formal->next)
17325 gfc_error ("Operator interface at %L must have, at most, two "
17326 "arguments", &where);
17327 return false;
17330 return true;
17333 static void
17334 gfc_resolve_uops (gfc_symtree *symtree)
17336 gfc_interface *itr;
17338 if (symtree == NULL)
17339 return;
17341 gfc_resolve_uops (symtree->left);
17342 gfc_resolve_uops (symtree->right);
17344 for (itr = symtree->n.uop->op; itr; itr = itr->next)
17345 check_uop_procedure (itr->sym, itr->sym->declared_at);
17349 /* Examine all of the expressions associated with a program unit,
17350 assign types to all intermediate expressions, make sure that all
17351 assignments are to compatible types and figure out which names
17352 refer to which functions or subroutines. It doesn't check code
17353 block, which is handled by gfc_resolve_code. */
17355 static void
17356 resolve_types (gfc_namespace *ns)
17358 gfc_namespace *n;
17359 gfc_charlen *cl;
17360 gfc_data *d;
17361 gfc_equiv *eq;
17362 gfc_namespace* old_ns = gfc_current_ns;
17363 bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
17365 if (ns->types_resolved)
17366 return;
17368 /* Check that all IMPLICIT types are ok. */
17369 if (!ns->seen_implicit_none)
17371 unsigned letter;
17372 for (letter = 0; letter != GFC_LETTERS; ++letter)
17373 if (ns->set_flag[letter]
17374 && !resolve_typespec_used (&ns->default_type[letter],
17375 &ns->implicit_loc[letter], NULL))
17376 return;
17379 gfc_current_ns = ns;
17381 resolve_entries (ns);
17383 resolve_common_vars (&ns->blank_common, false);
17384 resolve_common_blocks (ns->common_root);
17386 resolve_contained_functions (ns);
17388 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
17389 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
17390 gfc_resolve_formal_arglist (ns->proc_name);
17392 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
17394 for (cl = ns->cl_list; cl; cl = cl->next)
17395 resolve_charlen (cl);
17397 gfc_traverse_ns (ns, resolve_symbol);
17399 resolve_fntype (ns);
17401 for (n = ns->contained; n; n = n->sibling)
17403 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
17404 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
17405 "also be PURE", n->proc_name->name,
17406 &n->proc_name->declared_at);
17408 resolve_types (n);
17411 forall_flag = 0;
17412 gfc_do_concurrent_flag = 0;
17413 gfc_check_interfaces (ns);
17415 gfc_traverse_ns (ns, resolve_values);
17417 if (ns->save_all || (!flag_automatic && !recursive))
17418 gfc_save_all (ns);
17420 iter_stack = NULL;
17421 for (d = ns->data; d; d = d->next)
17422 resolve_data (d);
17424 iter_stack = NULL;
17425 gfc_traverse_ns (ns, gfc_formalize_init_value);
17427 gfc_traverse_ns (ns, gfc_verify_binding_labels);
17429 for (eq = ns->equiv; eq; eq = eq->next)
17430 resolve_equivalence (eq);
17432 /* Warn about unused labels. */
17433 if (warn_unused_label)
17434 warn_unused_fortran_label (ns->st_labels);
17436 gfc_resolve_uops (ns->uop_root);
17438 gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
17440 gfc_resolve_omp_declare_simd (ns);
17442 gfc_resolve_omp_udrs (ns->omp_udr_root);
17444 ns->types_resolved = 1;
17446 gfc_current_ns = old_ns;
17450 /* Call gfc_resolve_code recursively. */
17452 static void
17453 resolve_codes (gfc_namespace *ns)
17455 gfc_namespace *n;
17456 bitmap_obstack old_obstack;
17458 if (ns->resolved == 1)
17459 return;
17461 for (n = ns->contained; n; n = n->sibling)
17462 resolve_codes (n);
17464 gfc_current_ns = ns;
17466 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
17467 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
17468 cs_base = NULL;
17470 /* Set to an out of range value. */
17471 current_entry_id = -1;
17473 old_obstack = labels_obstack;
17474 bitmap_obstack_initialize (&labels_obstack);
17476 gfc_resolve_oacc_declare (ns);
17477 gfc_resolve_oacc_routines (ns);
17478 gfc_resolve_omp_local_vars (ns);
17479 gfc_resolve_code (ns->code, ns);
17481 bitmap_obstack_release (&labels_obstack);
17482 labels_obstack = old_obstack;
17486 /* This function is called after a complete program unit has been compiled.
17487 Its purpose is to examine all of the expressions associated with a program
17488 unit, assign types to all intermediate expressions, make sure that all
17489 assignments are to compatible types and figure out which names refer to
17490 which functions or subroutines. */
17492 void
17493 gfc_resolve (gfc_namespace *ns)
17495 gfc_namespace *old_ns;
17496 code_stack *old_cs_base;
17497 struct gfc_omp_saved_state old_omp_state;
17499 if (ns->resolved)
17500 return;
17502 ns->resolved = -1;
17503 old_ns = gfc_current_ns;
17504 old_cs_base = cs_base;
17506 /* As gfc_resolve can be called during resolution of an OpenMP construct
17507 body, we should clear any state associated to it, so that say NS's
17508 DO loops are not interpreted as OpenMP loops. */
17509 if (!ns->construct_entities)
17510 gfc_omp_save_and_clear_state (&old_omp_state);
17512 resolve_types (ns);
17513 component_assignment_level = 0;
17514 resolve_codes (ns);
17516 gfc_current_ns = old_ns;
17517 cs_base = old_cs_base;
17518 ns->resolved = 1;
17520 gfc_run_passes (ns);
17522 if (!ns->construct_entities)
17523 gfc_omp_restore_state (&old_omp_state);