testsuite: Correct vec-rlmi-rlnm.c testsuite expected result
[official-gcc.git] / gcc / fortran / resolve.c
bloba210f9aad43e8245f3bb8348bba29e2bf82b5428
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2020 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 && ts->u.cl && fts->u.cl
808 && (((ts->u.cl->length && !fts->u.cl->length)
809 ||(!ts->u.cl->length && fts->u.cl->length))
810 || (ts->u.cl->length
811 && ts->u.cl->length->expr_type
812 != fts->u.cl->length->expr_type)
813 || (ts->u.cl->length
814 && ts->u.cl->length->expr_type == EXPR_CONSTANT
815 && mpz_cmp (ts->u.cl->length->value.integer,
816 fts->u.cl->length->value.integer) != 0)))
817 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
818 "entries returning variables of different "
819 "string lengths", ns->entries->sym->name,
820 &ns->entries->sym->declared_at);
823 if (el == NULL)
825 sym = ns->entries->sym->result;
826 /* All result types the same. */
827 proc->ts = *fts;
828 if (sym->attr.dimension)
829 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
830 if (sym->attr.pointer)
831 gfc_add_pointer (&proc->attr, NULL);
833 else
835 /* Otherwise the result will be passed through a union by
836 reference. */
837 proc->attr.mixed_entry_master = 1;
838 for (el = ns->entries; el; el = el->next)
840 sym = el->sym->result;
841 if (sym->attr.dimension)
843 if (el == ns->entries)
844 gfc_error ("FUNCTION result %s cannot be an array in "
845 "FUNCTION %s at %L", sym->name,
846 ns->entries->sym->name, &sym->declared_at);
847 else
848 gfc_error ("ENTRY result %s cannot be an array in "
849 "FUNCTION %s at %L", sym->name,
850 ns->entries->sym->name, &sym->declared_at);
852 else if (sym->attr.pointer)
854 if (el == ns->entries)
855 gfc_error ("FUNCTION result %s cannot be a POINTER in "
856 "FUNCTION %s at %L", sym->name,
857 ns->entries->sym->name, &sym->declared_at);
858 else
859 gfc_error ("ENTRY result %s cannot be a POINTER in "
860 "FUNCTION %s at %L", sym->name,
861 ns->entries->sym->name, &sym->declared_at);
863 else
865 ts = &sym->ts;
866 if (ts->type == BT_UNKNOWN)
867 ts = gfc_get_default_type (sym->name, NULL);
868 switch (ts->type)
870 case BT_INTEGER:
871 if (ts->kind == gfc_default_integer_kind)
872 sym = NULL;
873 break;
874 case BT_REAL:
875 if (ts->kind == gfc_default_real_kind
876 || ts->kind == gfc_default_double_kind)
877 sym = NULL;
878 break;
879 case BT_COMPLEX:
880 if (ts->kind == gfc_default_complex_kind)
881 sym = NULL;
882 break;
883 case BT_LOGICAL:
884 if (ts->kind == gfc_default_logical_kind)
885 sym = NULL;
886 break;
887 case BT_UNKNOWN:
888 /* We will issue error elsewhere. */
889 sym = NULL;
890 break;
891 default:
892 break;
894 if (sym)
896 if (el == ns->entries)
897 gfc_error ("FUNCTION result %s cannot be of type %s "
898 "in FUNCTION %s at %L", sym->name,
899 gfc_typename (ts), ns->entries->sym->name,
900 &sym->declared_at);
901 else
902 gfc_error ("ENTRY result %s cannot be of type %s "
903 "in FUNCTION %s at %L", sym->name,
904 gfc_typename (ts), ns->entries->sym->name,
905 &sym->declared_at);
911 proc->attr.access = ACCESS_PRIVATE;
912 proc->attr.entry_master = 1;
914 /* Merge all the entry point arguments. */
915 for (el = ns->entries; el; el = el->next)
916 merge_argument_lists (proc, el->sym->formal);
918 /* Check the master formal arguments for any that are not
919 present in all entry points. */
920 for (el = ns->entries; el; el = el->next)
921 check_argument_lists (proc, el->sym->formal);
923 /* Use the master function for the function body. */
924 ns->proc_name = proc;
926 /* Finalize the new symbols. */
927 gfc_commit_symbols ();
929 /* Restore the original namespace. */
930 gfc_current_ns = old_ns;
934 /* Resolve common variables. */
935 static void
936 resolve_common_vars (gfc_common_head *common_block, bool named_common)
938 gfc_symbol *csym = common_block->head;
939 gfc_gsymbol *gsym;
941 for (; csym; csym = csym->common_next)
943 gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
944 if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
945 gfc_error_now ("Global entity %qs at %L cannot appear in a "
946 "COMMON block at %L", gsym->name,
947 &gsym->where, &csym->common_block->where);
949 /* gfc_add_in_common may have been called before, but the reported errors
950 have been ignored to continue parsing.
951 We do the checks again here. */
952 if (!csym->attr.use_assoc)
954 gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
955 gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
956 &common_block->where);
959 if (csym->value || csym->attr.data)
961 if (!csym->ns->is_block_data)
962 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
963 "but only in BLOCK DATA initialization is "
964 "allowed", csym->name, &csym->declared_at);
965 else if (!named_common)
966 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
967 "in a blank COMMON but initialization is only "
968 "allowed in named common blocks", csym->name,
969 &csym->declared_at);
972 if (UNLIMITED_POLY (csym))
973 gfc_error_now ("%qs in cannot appear in COMMON at %L "
974 "[F2008:C5100]", csym->name, &csym->declared_at);
976 if (csym->ts.type != BT_DERIVED)
977 continue;
979 if (!(csym->ts.u.derived->attr.sequence
980 || csym->ts.u.derived->attr.is_bind_c))
981 gfc_error_now ("Derived type variable %qs in COMMON at %L "
982 "has neither the SEQUENCE nor the BIND(C) "
983 "attribute", csym->name, &csym->declared_at);
984 if (csym->ts.u.derived->attr.alloc_comp)
985 gfc_error_now ("Derived type variable %qs in COMMON at %L "
986 "has an ultimate component that is "
987 "allocatable", csym->name, &csym->declared_at);
988 if (gfc_has_default_initializer (csym->ts.u.derived))
989 gfc_error_now ("Derived type variable %qs in COMMON at %L "
990 "may not have default initializer", csym->name,
991 &csym->declared_at);
993 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
994 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
998 /* Resolve common blocks. */
999 static void
1000 resolve_common_blocks (gfc_symtree *common_root)
1002 gfc_symbol *sym;
1003 gfc_gsymbol * gsym;
1005 if (common_root == NULL)
1006 return;
1008 if (common_root->left)
1009 resolve_common_blocks (common_root->left);
1010 if (common_root->right)
1011 resolve_common_blocks (common_root->right);
1013 resolve_common_vars (common_root->n.common, true);
1015 /* The common name is a global name - in Fortran 2003 also if it has a
1016 C binding name, since Fortran 2008 only the C binding name is a global
1017 identifier. */
1018 if (!common_root->n.common->binding_label
1019 || gfc_notification_std (GFC_STD_F2008))
1021 gsym = gfc_find_gsymbol (gfc_gsym_root,
1022 common_root->n.common->name);
1024 if (gsym && gfc_notification_std (GFC_STD_F2008)
1025 && gsym->type == GSYM_COMMON
1026 && ((common_root->n.common->binding_label
1027 && (!gsym->binding_label
1028 || strcmp (common_root->n.common->binding_label,
1029 gsym->binding_label) != 0))
1030 || (!common_root->n.common->binding_label
1031 && gsym->binding_label)))
1033 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1034 "identifier and must thus have the same binding name "
1035 "as the same-named COMMON block at %L: %s vs %s",
1036 common_root->n.common->name, &common_root->n.common->where,
1037 &gsym->where,
1038 common_root->n.common->binding_label
1039 ? common_root->n.common->binding_label : "(blank)",
1040 gsym->binding_label ? gsym->binding_label : "(blank)");
1041 return;
1044 if (gsym && gsym->type != GSYM_COMMON
1045 && !common_root->n.common->binding_label)
1047 gfc_error ("COMMON block %qs at %L uses the same global identifier "
1048 "as entity at %L",
1049 common_root->n.common->name, &common_root->n.common->where,
1050 &gsym->where);
1051 return;
1053 if (gsym && gsym->type != GSYM_COMMON)
1055 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1056 "%L sharing the identifier with global non-COMMON-block "
1057 "entity at %L", common_root->n.common->name,
1058 &common_root->n.common->where, &gsym->where);
1059 return;
1061 if (!gsym)
1063 gsym = gfc_get_gsymbol (common_root->n.common->name, false);
1064 gsym->type = GSYM_COMMON;
1065 gsym->where = common_root->n.common->where;
1066 gsym->defined = 1;
1068 gsym->used = 1;
1071 if (common_root->n.common->binding_label)
1073 gsym = gfc_find_gsymbol (gfc_gsym_root,
1074 common_root->n.common->binding_label);
1075 if (gsym && gsym->type != GSYM_COMMON)
1077 gfc_error ("COMMON block at %L with binding label %qs uses the same "
1078 "global identifier as entity at %L",
1079 &common_root->n.common->where,
1080 common_root->n.common->binding_label, &gsym->where);
1081 return;
1083 if (!gsym)
1085 gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
1086 gsym->type = GSYM_COMMON;
1087 gsym->where = common_root->n.common->where;
1088 gsym->defined = 1;
1090 gsym->used = 1;
1093 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1094 if (sym == NULL)
1095 return;
1097 if (sym->attr.flavor == FL_PARAMETER)
1098 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1099 sym->name, &common_root->n.common->where, &sym->declared_at);
1101 if (sym->attr.external)
1102 gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
1103 sym->name, &common_root->n.common->where);
1105 if (sym->attr.intrinsic)
1106 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1107 sym->name, &common_root->n.common->where);
1108 else if (sym->attr.result
1109 || gfc_is_function_return_value (sym, gfc_current_ns))
1110 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1111 "that is also a function result", sym->name,
1112 &common_root->n.common->where);
1113 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1114 && sym->attr.proc != PROC_ST_FUNCTION)
1115 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1116 "that is also a global procedure", sym->name,
1117 &common_root->n.common->where);
1121 /* Resolve contained function types. Because contained functions can call one
1122 another, they have to be worked out before any of the contained procedures
1123 can be resolved.
1125 The good news is that if a function doesn't already have a type, the only
1126 way it can get one is through an IMPLICIT type or a RESULT variable, because
1127 by definition contained functions are contained namespace they're contained
1128 in, not in a sibling or parent namespace. */
1130 static void
1131 resolve_contained_functions (gfc_namespace *ns)
1133 gfc_namespace *child;
1134 gfc_entry_list *el;
1136 resolve_formal_arglists (ns);
1138 for (child = ns->contained; child; child = child->sibling)
1140 /* Resolve alternate entry points first. */
1141 resolve_entries (child);
1143 /* Then check function return types. */
1144 resolve_contained_fntype (child->proc_name, child);
1145 for (el = child->entries; el; el = el->next)
1146 resolve_contained_fntype (el->sym, child);
1152 /* A Parameterized Derived Type constructor must contain values for
1153 the PDT KIND parameters or they must have a default initializer.
1154 Go through the constructor picking out the KIND expressions,
1155 storing them in 'param_list' and then call gfc_get_pdt_instance
1156 to obtain the PDT instance. */
1158 static gfc_actual_arglist *param_list, *param_tail, *param;
1160 static bool
1161 get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
1163 param = gfc_get_actual_arglist ();
1164 if (!param_list)
1165 param_list = param_tail = param;
1166 else
1168 param_tail->next = param;
1169 param_tail = param_tail->next;
1172 param_tail->name = c->name;
1173 if (expr)
1174 param_tail->expr = gfc_copy_expr (expr);
1175 else if (c->initializer)
1176 param_tail->expr = gfc_copy_expr (c->initializer);
1177 else
1179 param_tail->spec_type = SPEC_ASSUMED;
1180 if (c->attr.pdt_kind)
1182 gfc_error ("The KIND parameter %qs in the PDT constructor "
1183 "at %C has no value", param->name);
1184 return false;
1188 return true;
1191 static bool
1192 get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
1193 gfc_symbol *derived)
1195 gfc_constructor *cons = NULL;
1196 gfc_component *comp;
1197 bool t = true;
1199 if (expr && expr->expr_type == EXPR_STRUCTURE)
1200 cons = gfc_constructor_first (expr->value.constructor);
1201 else if (constr)
1202 cons = *constr;
1203 gcc_assert (cons);
1205 comp = derived->components;
1207 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1209 if (cons->expr
1210 && cons->expr->expr_type == EXPR_STRUCTURE
1211 && comp->ts.type == BT_DERIVED)
1213 t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
1214 if (!t)
1215 return t;
1217 else if (comp->ts.type == BT_DERIVED)
1219 t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
1220 if (!t)
1221 return t;
1223 else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
1224 && derived->attr.pdt_template)
1226 t = get_pdt_spec_expr (comp, cons->expr);
1227 if (!t)
1228 return t;
1231 return t;
1235 static bool resolve_fl_derived0 (gfc_symbol *sym);
1236 static bool resolve_fl_struct (gfc_symbol *sym);
1239 /* Resolve all of the elements of a structure constructor and make sure that
1240 the types are correct. The 'init' flag indicates that the given
1241 constructor is an initializer. */
1243 static bool
1244 resolve_structure_cons (gfc_expr *expr, int init)
1246 gfc_constructor *cons;
1247 gfc_component *comp;
1248 bool t;
1249 symbol_attribute a;
1251 t = true;
1253 if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1255 if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1256 resolve_fl_derived0 (expr->ts.u.derived);
1257 else
1258 resolve_fl_struct (expr->ts.u.derived);
1260 /* If this is a Parameterized Derived Type template, find the
1261 instance corresponding to the PDT kind parameters. */
1262 if (expr->ts.u.derived->attr.pdt_template)
1264 param_list = NULL;
1265 t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
1266 if (!t)
1267 return t;
1268 gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
1270 expr->param_list = gfc_copy_actual_arglist (param_list);
1272 if (param_list)
1273 gfc_free_actual_arglist (param_list);
1275 if (!expr->ts.u.derived->attr.pdt_type)
1276 return false;
1280 cons = gfc_constructor_first (expr->value.constructor);
1282 /* A constructor may have references if it is the result of substituting a
1283 parameter variable. In this case we just pull out the component we
1284 want. */
1285 if (expr->ref)
1286 comp = expr->ref->u.c.sym->components;
1287 else
1288 comp = expr->ts.u.derived->components;
1290 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1292 int rank;
1294 if (!cons->expr)
1295 continue;
1297 /* Unions use an EXPR_NULL contrived expression to tell the translation
1298 phase to generate an initializer of the appropriate length.
1299 Ignore it here. */
1300 if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1301 continue;
1303 if (!gfc_resolve_expr (cons->expr))
1305 t = false;
1306 continue;
1309 rank = comp->as ? comp->as->rank : 0;
1310 if (comp->ts.type == BT_CLASS
1311 && !comp->ts.u.derived->attr.unlimited_polymorphic
1312 && CLASS_DATA (comp)->as)
1313 rank = CLASS_DATA (comp)->as->rank;
1315 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1316 && (comp->attr.allocatable || cons->expr->rank))
1318 gfc_error ("The rank of the element in the structure "
1319 "constructor at %L does not match that of the "
1320 "component (%d/%d)", &cons->expr->where,
1321 cons->expr->rank, rank);
1322 t = false;
1325 /* If we don't have the right type, try to convert it. */
1327 if (!comp->attr.proc_pointer &&
1328 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1330 if (strcmp (comp->name, "_extends") == 0)
1332 /* Can afford to be brutal with the _extends initializer.
1333 The derived type can get lost because it is PRIVATE
1334 but it is not usage constrained by the standard. */
1335 cons->expr->ts = comp->ts;
1337 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1339 gfc_error ("The element in the structure constructor at %L, "
1340 "for pointer component %qs, is %s but should be %s",
1341 &cons->expr->where, comp->name,
1342 gfc_basic_typename (cons->expr->ts.type),
1343 gfc_basic_typename (comp->ts.type));
1344 t = false;
1346 else
1348 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1349 if (t)
1350 t = t2;
1354 /* For strings, the length of the constructor should be the same as
1355 the one of the structure, ensure this if the lengths are known at
1356 compile time and when we are dealing with PARAMETER or structure
1357 constructors. */
1358 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1359 && comp->ts.u.cl->length
1360 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1361 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1362 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1363 && cons->expr->rank != 0
1364 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1365 comp->ts.u.cl->length->value.integer) != 0)
1367 if (cons->expr->expr_type == EXPR_VARIABLE
1368 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1370 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1371 to make use of the gfc_resolve_character_array_constructor
1372 machinery. The expression is later simplified away to
1373 an array of string literals. */
1374 gfc_expr *para = cons->expr;
1375 cons->expr = gfc_get_expr ();
1376 cons->expr->ts = para->ts;
1377 cons->expr->where = para->where;
1378 cons->expr->expr_type = EXPR_ARRAY;
1379 cons->expr->rank = para->rank;
1380 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1381 gfc_constructor_append_expr (&cons->expr->value.constructor,
1382 para, &cons->expr->where);
1385 if (cons->expr->expr_type == EXPR_ARRAY)
1387 /* Rely on the cleanup of the namespace to deal correctly with
1388 the old charlen. (There was a block here that attempted to
1389 remove the charlen but broke the chain in so doing.) */
1390 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1391 cons->expr->ts.u.cl->length_from_typespec = true;
1392 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1393 gfc_resolve_character_array_constructor (cons->expr);
1397 if (cons->expr->expr_type == EXPR_NULL
1398 && !(comp->attr.pointer || comp->attr.allocatable
1399 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1400 || (comp->ts.type == BT_CLASS
1401 && (CLASS_DATA (comp)->attr.class_pointer
1402 || CLASS_DATA (comp)->attr.allocatable))))
1404 t = false;
1405 gfc_error ("The NULL in the structure constructor at %L is "
1406 "being applied to component %qs, which is neither "
1407 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1408 comp->name);
1411 if (comp->attr.proc_pointer && comp->ts.interface)
1413 /* Check procedure pointer interface. */
1414 gfc_symbol *s2 = NULL;
1415 gfc_component *c2;
1416 const char *name;
1417 char err[200];
1419 c2 = gfc_get_proc_ptr_comp (cons->expr);
1420 if (c2)
1422 s2 = c2->ts.interface;
1423 name = c2->name;
1425 else if (cons->expr->expr_type == EXPR_FUNCTION)
1427 s2 = cons->expr->symtree->n.sym->result;
1428 name = cons->expr->symtree->n.sym->result->name;
1430 else if (cons->expr->expr_type != EXPR_NULL)
1432 s2 = cons->expr->symtree->n.sym;
1433 name = cons->expr->symtree->n.sym->name;
1436 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1437 err, sizeof (err), NULL, NULL))
1439 gfc_error_opt (0, "Interface mismatch for procedure-pointer "
1440 "component %qs in structure constructor at %L:"
1441 " %s", comp->name, &cons->expr->where, err);
1442 return false;
1446 if (!comp->attr.pointer || comp->attr.proc_pointer
1447 || cons->expr->expr_type == EXPR_NULL)
1448 continue;
1450 a = gfc_expr_attr (cons->expr);
1452 if (!a.pointer && !a.target)
1454 t = false;
1455 gfc_error ("The element in the structure constructor at %L, "
1456 "for pointer component %qs should be a POINTER or "
1457 "a TARGET", &cons->expr->where, comp->name);
1460 if (init)
1462 /* F08:C461. Additional checks for pointer initialization. */
1463 if (a.allocatable)
1465 t = false;
1466 gfc_error ("Pointer initialization target at %L "
1467 "must not be ALLOCATABLE", &cons->expr->where);
1469 if (!a.save)
1471 t = false;
1472 gfc_error ("Pointer initialization target at %L "
1473 "must have the SAVE attribute", &cons->expr->where);
1477 /* F2003, C1272 (3). */
1478 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1479 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1480 || gfc_is_coindexed (cons->expr));
1481 if (impure && gfc_pure (NULL))
1483 t = false;
1484 gfc_error ("Invalid expression in the structure constructor for "
1485 "pointer component %qs at %L in PURE procedure",
1486 comp->name, &cons->expr->where);
1489 if (impure)
1490 gfc_unset_implicit_pure (NULL);
1493 return t;
1497 /****************** Expression name resolution ******************/
1499 /* Returns 0 if a symbol was not declared with a type or
1500 attribute declaration statement, nonzero otherwise. */
1502 static int
1503 was_declared (gfc_symbol *sym)
1505 symbol_attribute a;
1507 a = sym->attr;
1509 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1510 return 1;
1512 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1513 || a.optional || a.pointer || a.save || a.target || a.volatile_
1514 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1515 || a.asynchronous || a.codimension)
1516 return 1;
1518 return 0;
1522 /* Determine if a symbol is generic or not. */
1524 static int
1525 generic_sym (gfc_symbol *sym)
1527 gfc_symbol *s;
1529 if (sym->attr.generic ||
1530 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1531 return 1;
1533 if (was_declared (sym) || sym->ns->parent == NULL)
1534 return 0;
1536 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1538 if (s != NULL)
1540 if (s == sym)
1541 return 0;
1542 else
1543 return generic_sym (s);
1546 return 0;
1550 /* Determine if a symbol is specific or not. */
1552 static int
1553 specific_sym (gfc_symbol *sym)
1555 gfc_symbol *s;
1557 if (sym->attr.if_source == IFSRC_IFBODY
1558 || sym->attr.proc == PROC_MODULE
1559 || sym->attr.proc == PROC_INTERNAL
1560 || sym->attr.proc == PROC_ST_FUNCTION
1561 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1562 || sym->attr.external)
1563 return 1;
1565 if (was_declared (sym) || sym->ns->parent == NULL)
1566 return 0;
1568 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1570 return (s == NULL) ? 0 : specific_sym (s);
1574 /* Figure out if the procedure is specific, generic or unknown. */
1576 enum proc_type
1577 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1579 static proc_type
1580 procedure_kind (gfc_symbol *sym)
1582 if (generic_sym (sym))
1583 return PTYPE_GENERIC;
1585 if (specific_sym (sym))
1586 return PTYPE_SPECIFIC;
1588 return PTYPE_UNKNOWN;
1591 /* Check references to assumed size arrays. The flag need_full_assumed_size
1592 is nonzero when matching actual arguments. */
1594 static int need_full_assumed_size = 0;
1596 static bool
1597 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1599 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1600 return false;
1602 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1603 What should it be? */
1604 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1605 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1606 && (e->ref->u.ar.type == AR_FULL))
1608 gfc_error ("The upper bound in the last dimension must "
1609 "appear in the reference to the assumed size "
1610 "array %qs at %L", sym->name, &e->where);
1611 return true;
1613 return false;
1617 /* Look for bad assumed size array references in argument expressions
1618 of elemental and array valued intrinsic procedures. Since this is
1619 called from procedure resolution functions, it only recurses at
1620 operators. */
1622 static bool
1623 resolve_assumed_size_actual (gfc_expr *e)
1625 if (e == NULL)
1626 return false;
1628 switch (e->expr_type)
1630 case EXPR_VARIABLE:
1631 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1632 return true;
1633 break;
1635 case EXPR_OP:
1636 if (resolve_assumed_size_actual (e->value.op.op1)
1637 || resolve_assumed_size_actual (e->value.op.op2))
1638 return true;
1639 break;
1641 default:
1642 break;
1644 return false;
1648 /* Check a generic procedure, passed as an actual argument, to see if
1649 there is a matching specific name. If none, it is an error, and if
1650 more than one, the reference is ambiguous. */
1651 static int
1652 count_specific_procs (gfc_expr *e)
1654 int n;
1655 gfc_interface *p;
1656 gfc_symbol *sym;
1658 n = 0;
1659 sym = e->symtree->n.sym;
1661 for (p = sym->generic; p; p = p->next)
1662 if (strcmp (sym->name, p->sym->name) == 0)
1664 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1665 sym->name);
1666 n++;
1669 if (n > 1)
1670 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1671 &e->where);
1673 if (n == 0)
1674 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1675 "argument at %L", sym->name, &e->where);
1677 return n;
1681 /* See if a call to sym could possibly be a not allowed RECURSION because of
1682 a missing RECURSIVE declaration. This means that either sym is the current
1683 context itself, or sym is the parent of a contained procedure calling its
1684 non-RECURSIVE containing procedure.
1685 This also works if sym is an ENTRY. */
1687 static bool
1688 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1690 gfc_symbol* proc_sym;
1691 gfc_symbol* context_proc;
1692 gfc_namespace* real_context;
1694 if (sym->attr.flavor == FL_PROGRAM
1695 || gfc_fl_struct (sym->attr.flavor))
1696 return false;
1698 /* If we've got an ENTRY, find real procedure. */
1699 if (sym->attr.entry && sym->ns->entries)
1700 proc_sym = sym->ns->entries->sym;
1701 else
1702 proc_sym = sym;
1704 /* If sym is RECURSIVE, all is well of course. */
1705 if (proc_sym->attr.recursive || flag_recursive)
1706 return false;
1708 /* Find the context procedure's "real" symbol if it has entries.
1709 We look for a procedure symbol, so recurse on the parents if we don't
1710 find one (like in case of a BLOCK construct). */
1711 for (real_context = context; ; real_context = real_context->parent)
1713 /* We should find something, eventually! */
1714 gcc_assert (real_context);
1716 context_proc = (real_context->entries ? real_context->entries->sym
1717 : real_context->proc_name);
1719 /* In some special cases, there may not be a proc_name, like for this
1720 invalid code:
1721 real(bad_kind()) function foo () ...
1722 when checking the call to bad_kind ().
1723 In these cases, we simply return here and assume that the
1724 call is ok. */
1725 if (!context_proc)
1726 return false;
1728 if (context_proc->attr.flavor != FL_LABEL)
1729 break;
1732 /* A call from sym's body to itself is recursion, of course. */
1733 if (context_proc == proc_sym)
1734 return true;
1736 /* The same is true if context is a contained procedure and sym the
1737 containing one. */
1738 if (context_proc->attr.contained)
1740 gfc_symbol* parent_proc;
1742 gcc_assert (context->parent);
1743 parent_proc = (context->parent->entries ? context->parent->entries->sym
1744 : context->parent->proc_name);
1746 if (parent_proc == proc_sym)
1747 return true;
1750 return false;
1754 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1755 its typespec and formal argument list. */
1757 bool
1758 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1760 gfc_intrinsic_sym* isym = NULL;
1761 const char* symstd;
1763 if (sym->resolve_symbol_called >= 2)
1764 return true;
1766 sym->resolve_symbol_called = 2;
1768 /* Already resolved. */
1769 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1770 return true;
1772 /* We already know this one is an intrinsic, so we don't call
1773 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1774 gfc_find_subroutine directly to check whether it is a function or
1775 subroutine. */
1777 if (sym->intmod_sym_id && sym->attr.subroutine)
1779 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1780 isym = gfc_intrinsic_subroutine_by_id (id);
1782 else if (sym->intmod_sym_id)
1784 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1785 isym = gfc_intrinsic_function_by_id (id);
1787 else if (!sym->attr.subroutine)
1788 isym = gfc_find_function (sym->name);
1790 if (isym && !sym->attr.subroutine)
1792 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1793 && !sym->attr.implicit_type)
1794 gfc_warning (OPT_Wsurprising,
1795 "Type specified for intrinsic function %qs at %L is"
1796 " ignored", sym->name, &sym->declared_at);
1798 if (!sym->attr.function &&
1799 !gfc_add_function(&sym->attr, sym->name, loc))
1800 return false;
1802 sym->ts = isym->ts;
1804 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1806 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1808 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1809 " specifier", sym->name, &sym->declared_at);
1810 return false;
1813 if (!sym->attr.subroutine &&
1814 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1815 return false;
1817 else
1819 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1820 &sym->declared_at);
1821 return false;
1824 gfc_copy_formal_args_intr (sym, isym, NULL);
1826 sym->attr.pure = isym->pure;
1827 sym->attr.elemental = isym->elemental;
1829 /* Check it is actually available in the standard settings. */
1830 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1832 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1833 "available in the current standard settings but %s. Use "
1834 "an appropriate %<-std=*%> option or enable "
1835 "%<-fall-intrinsics%> in order to use it.",
1836 sym->name, &sym->declared_at, symstd);
1837 return false;
1840 return true;
1844 /* Resolve a procedure expression, like passing it to a called procedure or as
1845 RHS for a procedure pointer assignment. */
1847 static bool
1848 resolve_procedure_expression (gfc_expr* expr)
1850 gfc_symbol* sym;
1852 if (expr->expr_type != EXPR_VARIABLE)
1853 return true;
1854 gcc_assert (expr->symtree);
1856 sym = expr->symtree->n.sym;
1858 if (sym->attr.intrinsic)
1859 gfc_resolve_intrinsic (sym, &expr->where);
1861 if (sym->attr.flavor != FL_PROCEDURE
1862 || (sym->attr.function && sym->result == sym))
1863 return true;
1865 /* A non-RECURSIVE procedure that is used as procedure expression within its
1866 own body is in danger of being called recursively. */
1867 if (is_illegal_recursion (sym, gfc_current_ns))
1868 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1869 " itself recursively. Declare it RECURSIVE or use"
1870 " %<-frecursive%>", sym->name, &expr->where);
1872 return true;
1876 /* Check that name is not a derived type. */
1878 static bool
1879 is_dt_name (const char *name)
1881 gfc_symbol *dt_list, *dt_first;
1883 dt_list = dt_first = gfc_derived_types;
1884 for (; dt_list; dt_list = dt_list->dt_next)
1886 if (strcmp(dt_list->name, name) == 0)
1887 return true;
1888 if (dt_first == dt_list->dt_next)
1889 break;
1891 return false;
1895 /* Resolve an actual argument list. Most of the time, this is just
1896 resolving the expressions in the list.
1897 The exception is that we sometimes have to decide whether arguments
1898 that look like procedure arguments are really simple variable
1899 references. */
1901 static bool
1902 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1903 bool no_formal_args)
1905 gfc_symbol *sym;
1906 gfc_symtree *parent_st;
1907 gfc_expr *e;
1908 gfc_component *comp;
1909 int save_need_full_assumed_size;
1910 bool return_value = false;
1911 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1913 actual_arg = true;
1914 first_actual_arg = true;
1916 for (; arg; arg = arg->next)
1918 e = arg->expr;
1919 if (e == NULL)
1921 /* Check the label is a valid branching target. */
1922 if (arg->label)
1924 if (arg->label->defined == ST_LABEL_UNKNOWN)
1926 gfc_error ("Label %d referenced at %L is never defined",
1927 arg->label->value, &arg->label->where);
1928 goto cleanup;
1931 first_actual_arg = false;
1932 continue;
1935 if (e->expr_type == EXPR_VARIABLE
1936 && e->symtree->n.sym->attr.generic
1937 && no_formal_args
1938 && count_specific_procs (e) != 1)
1939 goto cleanup;
1941 if (e->ts.type != BT_PROCEDURE)
1943 save_need_full_assumed_size = need_full_assumed_size;
1944 if (e->expr_type != EXPR_VARIABLE)
1945 need_full_assumed_size = 0;
1946 if (!gfc_resolve_expr (e))
1947 goto cleanup;
1948 need_full_assumed_size = save_need_full_assumed_size;
1949 goto argument_list;
1952 /* See if the expression node should really be a variable reference. */
1954 sym = e->symtree->n.sym;
1956 if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
1958 gfc_error ("Derived type %qs is used as an actual "
1959 "argument at %L", sym->name, &e->where);
1960 goto cleanup;
1963 if (sym->attr.flavor == FL_PROCEDURE
1964 || sym->attr.intrinsic
1965 || sym->attr.external)
1967 int actual_ok;
1969 /* If a procedure is not already determined to be something else
1970 check if it is intrinsic. */
1971 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1972 sym->attr.intrinsic = 1;
1974 if (sym->attr.proc == PROC_ST_FUNCTION)
1976 gfc_error ("Statement function %qs at %L is not allowed as an "
1977 "actual argument", sym->name, &e->where);
1980 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1981 sym->attr.subroutine);
1982 if (sym->attr.intrinsic && actual_ok == 0)
1984 gfc_error ("Intrinsic %qs at %L is not allowed as an "
1985 "actual argument", sym->name, &e->where);
1988 if (sym->attr.contained && !sym->attr.use_assoc
1989 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1991 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
1992 " used as actual argument at %L",
1993 sym->name, &e->where))
1994 goto cleanup;
1997 if (sym->attr.elemental && !sym->attr.intrinsic)
1999 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
2000 "allowed as an actual argument at %L", sym->name,
2001 &e->where);
2004 /* Check if a generic interface has a specific procedure
2005 with the same name before emitting an error. */
2006 if (sym->attr.generic && count_specific_procs (e) != 1)
2007 goto cleanup;
2009 /* Just in case a specific was found for the expression. */
2010 sym = e->symtree->n.sym;
2012 /* If the symbol is the function that names the current (or
2013 parent) scope, then we really have a variable reference. */
2015 if (gfc_is_function_return_value (sym, sym->ns))
2016 goto got_variable;
2018 /* If all else fails, see if we have a specific intrinsic. */
2019 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
2021 gfc_intrinsic_sym *isym;
2023 isym = gfc_find_function (sym->name);
2024 if (isym == NULL || !isym->specific)
2026 gfc_error ("Unable to find a specific INTRINSIC procedure "
2027 "for the reference %qs at %L", sym->name,
2028 &e->where);
2029 goto cleanup;
2031 sym->ts = isym->ts;
2032 sym->attr.intrinsic = 1;
2033 sym->attr.function = 1;
2036 if (!gfc_resolve_expr (e))
2037 goto cleanup;
2038 goto argument_list;
2041 /* See if the name is a module procedure in a parent unit. */
2043 if (was_declared (sym) || sym->ns->parent == NULL)
2044 goto got_variable;
2046 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
2048 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
2049 goto cleanup;
2052 if (parent_st == NULL)
2053 goto got_variable;
2055 sym = parent_st->n.sym;
2056 e->symtree = parent_st; /* Point to the right thing. */
2058 if (sym->attr.flavor == FL_PROCEDURE
2059 || sym->attr.intrinsic
2060 || sym->attr.external)
2062 if (!gfc_resolve_expr (e))
2063 goto cleanup;
2064 goto argument_list;
2067 got_variable:
2068 e->expr_type = EXPR_VARIABLE;
2069 e->ts = sym->ts;
2070 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2071 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2072 && CLASS_DATA (sym)->as))
2074 e->rank = sym->ts.type == BT_CLASS
2075 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
2076 e->ref = gfc_get_ref ();
2077 e->ref->type = REF_ARRAY;
2078 e->ref->u.ar.type = AR_FULL;
2079 e->ref->u.ar.as = sym->ts.type == BT_CLASS
2080 ? CLASS_DATA (sym)->as : sym->as;
2083 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2084 primary.c (match_actual_arg). If above code determines that it
2085 is a variable instead, it needs to be resolved as it was not
2086 done at the beginning of this function. */
2087 save_need_full_assumed_size = need_full_assumed_size;
2088 if (e->expr_type != EXPR_VARIABLE)
2089 need_full_assumed_size = 0;
2090 if (!gfc_resolve_expr (e))
2091 goto cleanup;
2092 need_full_assumed_size = save_need_full_assumed_size;
2094 argument_list:
2095 /* Check argument list functions %VAL, %LOC and %REF. There is
2096 nothing to do for %REF. */
2097 if (arg->name && arg->name[0] == '%')
2099 if (strcmp ("%VAL", arg->name) == 0)
2101 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
2103 gfc_error ("By-value argument at %L is not of numeric "
2104 "type", &e->where);
2105 goto cleanup;
2108 if (e->rank)
2110 gfc_error ("By-value argument at %L cannot be an array or "
2111 "an array section", &e->where);
2112 goto cleanup;
2115 /* Intrinsics are still PROC_UNKNOWN here. However,
2116 since same file external procedures are not resolvable
2117 in gfortran, it is a good deal easier to leave them to
2118 intrinsic.c. */
2119 if (ptype != PROC_UNKNOWN
2120 && ptype != PROC_DUMMY
2121 && ptype != PROC_EXTERNAL
2122 && ptype != PROC_MODULE)
2124 gfc_error ("By-value argument at %L is not allowed "
2125 "in this context", &e->where);
2126 goto cleanup;
2130 /* Statement functions have already been excluded above. */
2131 else if (strcmp ("%LOC", arg->name) == 0
2132 && e->ts.type == BT_PROCEDURE)
2134 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
2136 gfc_error ("Passing internal procedure at %L by location "
2137 "not allowed", &e->where);
2138 goto cleanup;
2143 comp = gfc_get_proc_ptr_comp(e);
2144 if (e->expr_type == EXPR_VARIABLE
2145 && comp && comp->attr.elemental)
2147 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2148 "allowed as an actual argument at %L", comp->name,
2149 &e->where);
2152 /* Fortran 2008, C1237. */
2153 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2154 && gfc_has_ultimate_pointer (e))
2156 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2157 "component", &e->where);
2158 goto cleanup;
2161 first_actual_arg = false;
2164 return_value = true;
2166 cleanup:
2167 actual_arg = actual_arg_sav;
2168 first_actual_arg = first_actual_arg_sav;
2170 return return_value;
2174 /* Do the checks of the actual argument list that are specific to elemental
2175 procedures. If called with c == NULL, we have a function, otherwise if
2176 expr == NULL, we have a subroutine. */
2178 static bool
2179 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2181 gfc_actual_arglist *arg0;
2182 gfc_actual_arglist *arg;
2183 gfc_symbol *esym = NULL;
2184 gfc_intrinsic_sym *isym = NULL;
2185 gfc_expr *e = NULL;
2186 gfc_intrinsic_arg *iformal = NULL;
2187 gfc_formal_arglist *eformal = NULL;
2188 bool formal_optional = false;
2189 bool set_by_optional = false;
2190 int i;
2191 int rank = 0;
2193 /* Is this an elemental procedure? */
2194 if (expr && expr->value.function.actual != NULL)
2196 if (expr->value.function.esym != NULL
2197 && expr->value.function.esym->attr.elemental)
2199 arg0 = expr->value.function.actual;
2200 esym = expr->value.function.esym;
2202 else if (expr->value.function.isym != NULL
2203 && expr->value.function.isym->elemental)
2205 arg0 = expr->value.function.actual;
2206 isym = expr->value.function.isym;
2208 else
2209 return true;
2211 else if (c && c->ext.actual != NULL)
2213 arg0 = c->ext.actual;
2215 if (c->resolved_sym)
2216 esym = c->resolved_sym;
2217 else
2218 esym = c->symtree->n.sym;
2219 gcc_assert (esym);
2221 if (!esym->attr.elemental)
2222 return true;
2224 else
2225 return true;
2227 /* The rank of an elemental is the rank of its array argument(s). */
2228 for (arg = arg0; arg; arg = arg->next)
2230 if (arg->expr != NULL && arg->expr->rank != 0)
2232 rank = arg->expr->rank;
2233 if (arg->expr->expr_type == EXPR_VARIABLE
2234 && arg->expr->symtree->n.sym->attr.optional)
2235 set_by_optional = true;
2237 /* Function specific; set the result rank and shape. */
2238 if (expr)
2240 expr->rank = rank;
2241 if (!expr->shape && arg->expr->shape)
2243 expr->shape = gfc_get_shape (rank);
2244 for (i = 0; i < rank; i++)
2245 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2248 break;
2252 /* If it is an array, it shall not be supplied as an actual argument
2253 to an elemental procedure unless an array of the same rank is supplied
2254 as an actual argument corresponding to a nonoptional dummy argument of
2255 that elemental procedure(12.4.1.5). */
2256 formal_optional = false;
2257 if (isym)
2258 iformal = isym->formal;
2259 else
2260 eformal = esym->formal;
2262 for (arg = arg0; arg; arg = arg->next)
2264 if (eformal)
2266 if (eformal->sym && eformal->sym->attr.optional)
2267 formal_optional = true;
2268 eformal = eformal->next;
2270 else if (isym && iformal)
2272 if (iformal->optional)
2273 formal_optional = true;
2274 iformal = iformal->next;
2276 else if (isym)
2277 formal_optional = true;
2279 if (pedantic && arg->expr != NULL
2280 && arg->expr->expr_type == EXPR_VARIABLE
2281 && arg->expr->symtree->n.sym->attr.optional
2282 && formal_optional
2283 && arg->expr->rank
2284 && (set_by_optional || arg->expr->rank != rank)
2285 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2287 bool t = false;
2288 gfc_actual_arglist *a;
2290 /* Scan the argument list for a non-optional argument with the
2291 same rank as arg. */
2292 for (a = arg0; a; a = a->next)
2293 if (a != arg
2294 && a->expr->rank == arg->expr->rank
2295 && !a->expr->symtree->n.sym->attr.optional)
2297 t = true;
2298 break;
2301 if (!t)
2302 gfc_warning (OPT_Wpedantic,
2303 "%qs at %L is an array and OPTIONAL; If it is not "
2304 "present, then it cannot be the actual argument of "
2305 "an ELEMENTAL procedure unless there is a non-optional"
2306 " argument with the same rank "
2307 "(Fortran 2018, 15.5.2.12)",
2308 arg->expr->symtree->n.sym->name, &arg->expr->where);
2312 for (arg = arg0; arg; arg = arg->next)
2314 if (arg->expr == NULL || arg->expr->rank == 0)
2315 continue;
2317 /* Being elemental, the last upper bound of an assumed size array
2318 argument must be present. */
2319 if (resolve_assumed_size_actual (arg->expr))
2320 return false;
2322 /* Elemental procedure's array actual arguments must conform. */
2323 if (e != NULL)
2325 if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
2326 return false;
2328 else
2329 e = arg->expr;
2332 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2333 is an array, the intent inout/out variable needs to be also an array. */
2334 if (rank > 0 && esym && expr == NULL)
2335 for (eformal = esym->formal, arg = arg0; arg && eformal;
2336 arg = arg->next, eformal = eformal->next)
2337 if ((eformal->sym->attr.intent == INTENT_OUT
2338 || eformal->sym->attr.intent == INTENT_INOUT)
2339 && arg->expr && arg->expr->rank == 0)
2341 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2342 "ELEMENTAL subroutine %qs is a scalar, but another "
2343 "actual argument is an array", &arg->expr->where,
2344 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2345 : "INOUT", eformal->sym->name, esym->name);
2346 return false;
2348 return true;
2352 /* This function does the checking of references to global procedures
2353 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2354 77 and 95 standards. It checks for a gsymbol for the name, making
2355 one if it does not already exist. If it already exists, then the
2356 reference being resolved must correspond to the type of gsymbol.
2357 Otherwise, the new symbol is equipped with the attributes of the
2358 reference. The corresponding code that is called in creating
2359 global entities is parse.c.
2361 In addition, for all but -std=legacy, the gsymbols are used to
2362 check the interfaces of external procedures from the same file.
2363 The namespace of the gsymbol is resolved and then, once this is
2364 done the interface is checked. */
2367 static bool
2368 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2370 if (!gsym_ns->proc_name->attr.recursive)
2371 return true;
2373 if (sym->ns == gsym_ns)
2374 return false;
2376 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2377 return false;
2379 return true;
2382 static bool
2383 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2385 if (gsym_ns->entries)
2387 gfc_entry_list *entry = gsym_ns->entries;
2389 for (; entry; entry = entry->next)
2391 if (strcmp (sym->name, entry->sym->name) == 0)
2393 if (strcmp (gsym_ns->proc_name->name,
2394 sym->ns->proc_name->name) == 0)
2395 return false;
2397 if (sym->ns->parent
2398 && strcmp (gsym_ns->proc_name->name,
2399 sym->ns->parent->proc_name->name) == 0)
2400 return false;
2404 return true;
2408 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2410 bool
2411 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2413 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2415 for ( ; arg; arg = arg->next)
2417 if (!arg->sym)
2418 continue;
2420 if (arg->sym->attr.allocatable) /* (2a) */
2422 strncpy (errmsg, _("allocatable argument"), err_len);
2423 return true;
2425 else if (arg->sym->attr.asynchronous)
2427 strncpy (errmsg, _("asynchronous argument"), err_len);
2428 return true;
2430 else if (arg->sym->attr.optional)
2432 strncpy (errmsg, _("optional argument"), err_len);
2433 return true;
2435 else if (arg->sym->attr.pointer)
2437 strncpy (errmsg, _("pointer argument"), err_len);
2438 return true;
2440 else if (arg->sym->attr.target)
2442 strncpy (errmsg, _("target argument"), err_len);
2443 return true;
2445 else if (arg->sym->attr.value)
2447 strncpy (errmsg, _("value argument"), err_len);
2448 return true;
2450 else if (arg->sym->attr.volatile_)
2452 strncpy (errmsg, _("volatile argument"), err_len);
2453 return true;
2455 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2457 strncpy (errmsg, _("assumed-shape argument"), err_len);
2458 return true;
2460 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2462 strncpy (errmsg, _("assumed-rank argument"), err_len);
2463 return true;
2465 else if (arg->sym->attr.codimension) /* (2c) */
2467 strncpy (errmsg, _("coarray argument"), err_len);
2468 return true;
2470 else if (false) /* (2d) TODO: parametrized derived type */
2472 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2473 return true;
2475 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2477 strncpy (errmsg, _("polymorphic argument"), err_len);
2478 return true;
2480 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2482 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2483 return true;
2485 else if (arg->sym->ts.type == BT_ASSUMED)
2487 /* As assumed-type is unlimited polymorphic (cf. above).
2488 See also TS 29113, Note 6.1. */
2489 strncpy (errmsg, _("assumed-type argument"), err_len);
2490 return true;
2494 if (sym->attr.function)
2496 gfc_symbol *res = sym->result ? sym->result : sym;
2498 if (res->attr.dimension) /* (3a) */
2500 strncpy (errmsg, _("array result"), err_len);
2501 return true;
2503 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2505 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2506 return true;
2508 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2509 && res->ts.u.cl->length
2510 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2512 strncpy (errmsg, _("result with non-constant character length"), err_len);
2513 return true;
2517 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2519 strncpy (errmsg, _("elemental procedure"), err_len);
2520 return true;
2522 else if (sym->attr.is_bind_c) /* (5) */
2524 strncpy (errmsg, _("bind(c) procedure"), err_len);
2525 return true;
2528 return false;
2532 static void
2533 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
2535 gfc_gsymbol * gsym;
2536 gfc_namespace *ns;
2537 enum gfc_symbol_type type;
2538 char reason[200];
2540 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2542 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
2543 sym->binding_label != NULL);
2545 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2546 gfc_global_used (gsym, where);
2548 if ((sym->attr.if_source == IFSRC_UNKNOWN
2549 || sym->attr.if_source == IFSRC_IFBODY)
2550 && gsym->type != GSYM_UNKNOWN
2551 && !gsym->binding_label
2552 && gsym->ns
2553 && gsym->ns->proc_name
2554 && not_in_recursive (sym, gsym->ns)
2555 && not_entry_self_reference (sym, gsym->ns))
2557 gfc_symbol *def_sym;
2558 def_sym = gsym->ns->proc_name;
2560 if (gsym->ns->resolved != -1)
2563 /* Resolve the gsymbol namespace if needed. */
2564 if (!gsym->ns->resolved)
2566 gfc_symbol *old_dt_list;
2568 /* Stash away derived types so that the backend_decls
2569 do not get mixed up. */
2570 old_dt_list = gfc_derived_types;
2571 gfc_derived_types = NULL;
2573 gfc_resolve (gsym->ns);
2575 /* Store the new derived types with the global namespace. */
2576 if (gfc_derived_types)
2577 gsym->ns->derived_types = gfc_derived_types;
2579 /* Restore the derived types of this namespace. */
2580 gfc_derived_types = old_dt_list;
2583 /* Make sure that translation for the gsymbol occurs before
2584 the procedure currently being resolved. */
2585 ns = gfc_global_ns_list;
2586 for (; ns && ns != gsym->ns; ns = ns->sibling)
2588 if (ns->sibling == gsym->ns)
2590 ns->sibling = gsym->ns->sibling;
2591 gsym->ns->sibling = gfc_global_ns_list;
2592 gfc_global_ns_list = gsym->ns;
2593 break;
2597 /* This can happen if a binding name has been specified. */
2598 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2599 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2601 if (def_sym->attr.entry_master || def_sym->attr.entry)
2603 gfc_entry_list *entry;
2604 for (entry = gsym->ns->entries; entry; entry = entry->next)
2605 if (strcmp (entry->sym->name, sym->name) == 0)
2607 def_sym = entry->sym;
2608 break;
2613 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2615 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2616 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2617 gfc_typename (&def_sym->ts));
2618 goto done;
2621 if (sym->attr.if_source == IFSRC_UNKNOWN
2622 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2624 gfc_error ("Explicit interface required for %qs at %L: %s",
2625 sym->name, &sym->declared_at, reason);
2626 goto done;
2629 bool bad_result_characteristics;
2630 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2631 reason, sizeof(reason), NULL, NULL,
2632 &bad_result_characteristics))
2634 /* Turn erros into warnings with -std=gnu and -std=legacy,
2635 unless a function returns a wrong type, which can lead
2636 to all kinds of ICEs and wrong code. */
2638 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
2639 && !bad_result_characteristics)
2640 gfc_errors_to_warnings (true);
2642 gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
2643 sym->name, &sym->declared_at, reason);
2644 sym->error = 1;
2645 gfc_errors_to_warnings (false);
2646 goto done;
2650 done:
2652 if (gsym->type == GSYM_UNKNOWN)
2654 gsym->type = type;
2655 gsym->where = *where;
2658 gsym->used = 1;
2662 /************* Function resolution *************/
2664 /* Resolve a function call known to be generic.
2665 Section 14.1.2.4.1. */
2667 static match
2668 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2670 gfc_symbol *s;
2672 if (sym->attr.generic)
2674 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2675 if (s != NULL)
2677 expr->value.function.name = s->name;
2678 expr->value.function.esym = s;
2680 if (s->ts.type != BT_UNKNOWN)
2681 expr->ts = s->ts;
2682 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2683 expr->ts = s->result->ts;
2685 if (s->as != NULL)
2686 expr->rank = s->as->rank;
2687 else if (s->result != NULL && s->result->as != NULL)
2688 expr->rank = s->result->as->rank;
2690 gfc_set_sym_referenced (expr->value.function.esym);
2692 return MATCH_YES;
2695 /* TODO: Need to search for elemental references in generic
2696 interface. */
2699 if (sym->attr.intrinsic)
2700 return gfc_intrinsic_func_interface (expr, 0);
2702 return MATCH_NO;
2706 static bool
2707 resolve_generic_f (gfc_expr *expr)
2709 gfc_symbol *sym;
2710 match m;
2711 gfc_interface *intr = NULL;
2713 sym = expr->symtree->n.sym;
2715 for (;;)
2717 m = resolve_generic_f0 (expr, sym);
2718 if (m == MATCH_YES)
2719 return true;
2720 else if (m == MATCH_ERROR)
2721 return false;
2723 generic:
2724 if (!intr)
2725 for (intr = sym->generic; intr; intr = intr->next)
2726 if (gfc_fl_struct (intr->sym->attr.flavor))
2727 break;
2729 if (sym->ns->parent == NULL)
2730 break;
2731 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2733 if (sym == NULL)
2734 break;
2735 if (!generic_sym (sym))
2736 goto generic;
2739 /* Last ditch attempt. See if the reference is to an intrinsic
2740 that possesses a matching interface. 14.1.2.4 */
2741 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2743 if (gfc_init_expr_flag)
2744 gfc_error ("Function %qs in initialization expression at %L "
2745 "must be an intrinsic function",
2746 expr->symtree->n.sym->name, &expr->where);
2747 else
2748 gfc_error ("There is no specific function for the generic %qs "
2749 "at %L", expr->symtree->n.sym->name, &expr->where);
2750 return false;
2753 if (intr)
2755 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2756 NULL, false))
2757 return false;
2758 if (!gfc_use_derived (expr->ts.u.derived))
2759 return false;
2760 return resolve_structure_cons (expr, 0);
2763 m = gfc_intrinsic_func_interface (expr, 0);
2764 if (m == MATCH_YES)
2765 return true;
2767 if (m == MATCH_NO)
2768 gfc_error ("Generic function %qs at %L is not consistent with a "
2769 "specific intrinsic interface", expr->symtree->n.sym->name,
2770 &expr->where);
2772 return false;
2776 /* Resolve a function call known to be specific. */
2778 static match
2779 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2781 match m;
2783 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2785 if (sym->attr.dummy)
2787 sym->attr.proc = PROC_DUMMY;
2788 goto found;
2791 sym->attr.proc = PROC_EXTERNAL;
2792 goto found;
2795 if (sym->attr.proc == PROC_MODULE
2796 || sym->attr.proc == PROC_ST_FUNCTION
2797 || sym->attr.proc == PROC_INTERNAL)
2798 goto found;
2800 if (sym->attr.intrinsic)
2802 m = gfc_intrinsic_func_interface (expr, 1);
2803 if (m == MATCH_YES)
2804 return MATCH_YES;
2805 if (m == MATCH_NO)
2806 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2807 "with an intrinsic", sym->name, &expr->where);
2809 return MATCH_ERROR;
2812 return MATCH_NO;
2814 found:
2815 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2817 if (sym->result)
2818 expr->ts = sym->result->ts;
2819 else
2820 expr->ts = sym->ts;
2821 expr->value.function.name = sym->name;
2822 expr->value.function.esym = sym;
2823 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2824 error(s). */
2825 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2826 return MATCH_ERROR;
2827 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2828 expr->rank = CLASS_DATA (sym)->as->rank;
2829 else if (sym->as != NULL)
2830 expr->rank = sym->as->rank;
2832 return MATCH_YES;
2836 static bool
2837 resolve_specific_f (gfc_expr *expr)
2839 gfc_symbol *sym;
2840 match m;
2842 sym = expr->symtree->n.sym;
2844 for (;;)
2846 m = resolve_specific_f0 (sym, expr);
2847 if (m == MATCH_YES)
2848 return true;
2849 if (m == MATCH_ERROR)
2850 return false;
2852 if (sym->ns->parent == NULL)
2853 break;
2855 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2857 if (sym == NULL)
2858 break;
2861 gfc_error ("Unable to resolve the specific function %qs at %L",
2862 expr->symtree->n.sym->name, &expr->where);
2864 return true;
2867 /* Recursively append candidate SYM to CANDIDATES. Store the number of
2868 candidates in CANDIDATES_LEN. */
2870 static void
2871 lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
2872 char **&candidates,
2873 size_t &candidates_len)
2875 gfc_symtree *p;
2877 if (sym == NULL)
2878 return;
2879 if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
2880 && sym->n.sym->attr.flavor == FL_PROCEDURE)
2881 vec_push (candidates, candidates_len, sym->name);
2883 p = sym->left;
2884 if (p)
2885 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
2887 p = sym->right;
2888 if (p)
2889 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
2893 /* Lookup function FN fuzzily, taking names in SYMROOT into account. */
2895 const char*
2896 gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
2898 char **candidates = NULL;
2899 size_t candidates_len = 0;
2900 lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
2901 return gfc_closest_fuzzy_match (fn, candidates);
2905 /* Resolve a procedure call not known to be generic nor specific. */
2907 static bool
2908 resolve_unknown_f (gfc_expr *expr)
2910 gfc_symbol *sym;
2911 gfc_typespec *ts;
2913 sym = expr->symtree->n.sym;
2915 if (sym->attr.dummy)
2917 sym->attr.proc = PROC_DUMMY;
2918 expr->value.function.name = sym->name;
2919 goto set_type;
2922 /* See if we have an intrinsic function reference. */
2924 if (gfc_is_intrinsic (sym, 0, expr->where))
2926 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2927 return true;
2928 return false;
2931 /* The reference is to an external name. */
2933 sym->attr.proc = PROC_EXTERNAL;
2934 expr->value.function.name = sym->name;
2935 expr->value.function.esym = expr->symtree->n.sym;
2937 if (sym->as != NULL)
2938 expr->rank = sym->as->rank;
2940 /* Type of the expression is either the type of the symbol or the
2941 default type of the symbol. */
2943 set_type:
2944 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2946 if (sym->ts.type != BT_UNKNOWN)
2947 expr->ts = sym->ts;
2948 else
2950 ts = gfc_get_default_type (sym->name, sym->ns);
2952 if (ts->type == BT_UNKNOWN)
2954 const char *guessed
2955 = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
2956 if (guessed)
2957 gfc_error ("Function %qs at %L has no IMPLICIT type"
2958 "; did you mean %qs?",
2959 sym->name, &expr->where, guessed);
2960 else
2961 gfc_error ("Function %qs at %L has no IMPLICIT type",
2962 sym->name, &expr->where);
2963 return false;
2965 else
2966 expr->ts = *ts;
2969 return true;
2973 /* Return true, if the symbol is an external procedure. */
2974 static bool
2975 is_external_proc (gfc_symbol *sym)
2977 if (!sym->attr.dummy && !sym->attr.contained
2978 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2979 && sym->attr.proc != PROC_ST_FUNCTION
2980 && !sym->attr.proc_pointer
2981 && !sym->attr.use_assoc
2982 && sym->name)
2983 return true;
2985 return false;
2989 /* Figure out if a function reference is pure or not. Also set the name
2990 of the function for a potential error message. Return nonzero if the
2991 function is PURE, zero if not. */
2992 static int
2993 pure_stmt_function (gfc_expr *, gfc_symbol *);
2996 gfc_pure_function (gfc_expr *e, const char **name)
2998 int pure;
2999 gfc_component *comp;
3001 *name = NULL;
3003 if (e->symtree != NULL
3004 && e->symtree->n.sym != NULL
3005 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3006 return pure_stmt_function (e, e->symtree->n.sym);
3008 comp = gfc_get_proc_ptr_comp (e);
3009 if (comp)
3011 pure = gfc_pure (comp->ts.interface);
3012 *name = comp->name;
3014 else if (e->value.function.esym)
3016 pure = gfc_pure (e->value.function.esym);
3017 *name = e->value.function.esym->name;
3019 else if (e->value.function.isym)
3021 pure = e->value.function.isym->pure
3022 || e->value.function.isym->elemental;
3023 *name = e->value.function.isym->name;
3025 else
3027 /* Implicit functions are not pure. */
3028 pure = 0;
3029 *name = e->value.function.name;
3032 return pure;
3036 /* Check if the expression is a reference to an implicitly pure function. */
3039 gfc_implicit_pure_function (gfc_expr *e)
3041 gfc_component *comp = gfc_get_proc_ptr_comp (e);
3042 if (comp)
3043 return gfc_implicit_pure (comp->ts.interface);
3044 else if (e->value.function.esym)
3045 return gfc_implicit_pure (e->value.function.esym);
3046 else
3047 return 0;
3051 static bool
3052 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
3053 int *f ATTRIBUTE_UNUSED)
3055 const char *name;
3057 /* Don't bother recursing into other statement functions
3058 since they will be checked individually for purity. */
3059 if (e->expr_type != EXPR_FUNCTION
3060 || !e->symtree
3061 || e->symtree->n.sym == sym
3062 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3063 return false;
3065 return gfc_pure_function (e, &name) ? false : true;
3069 static int
3070 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
3072 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
3076 /* Check if an impure function is allowed in the current context. */
3078 static bool check_pure_function (gfc_expr *e)
3080 const char *name = NULL;
3081 if (!gfc_pure_function (e, &name) && name)
3083 if (forall_flag)
3085 gfc_error ("Reference to impure function %qs at %L inside a "
3086 "FORALL %s", name, &e->where,
3087 forall_flag == 2 ? "mask" : "block");
3088 return false;
3090 else if (gfc_do_concurrent_flag)
3092 gfc_error ("Reference to impure function %qs at %L inside a "
3093 "DO CONCURRENT %s", name, &e->where,
3094 gfc_do_concurrent_flag == 2 ? "mask" : "block");
3095 return false;
3097 else if (gfc_pure (NULL))
3099 gfc_error ("Reference to impure function %qs at %L "
3100 "within a PURE procedure", name, &e->where);
3101 return false;
3103 if (!gfc_implicit_pure_function (e))
3104 gfc_unset_implicit_pure (NULL);
3106 return true;
3110 /* Update current procedure's array_outer_dependency flag, considering
3111 a call to procedure SYM. */
3113 static void
3114 update_current_proc_array_outer_dependency (gfc_symbol *sym)
3116 /* Check to see if this is a sibling function that has not yet
3117 been resolved. */
3118 gfc_namespace *sibling = gfc_current_ns->sibling;
3119 for (; sibling; sibling = sibling->sibling)
3121 if (sibling->proc_name == sym)
3123 gfc_resolve (sibling);
3124 break;
3128 /* If SYM has references to outer arrays, so has the procedure calling
3129 SYM. If SYM is a procedure pointer, we can assume the worst. */
3130 if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
3131 && gfc_current_ns->proc_name)
3132 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3136 /* Resolve a function call, which means resolving the arguments, then figuring
3137 out which entity the name refers to. */
3139 static bool
3140 resolve_function (gfc_expr *expr)
3142 gfc_actual_arglist *arg;
3143 gfc_symbol *sym;
3144 bool t;
3145 int temp;
3146 procedure_type p = PROC_INTRINSIC;
3147 bool no_formal_args;
3149 sym = NULL;
3150 if (expr->symtree)
3151 sym = expr->symtree->n.sym;
3153 /* If this is a procedure pointer component, it has already been resolved. */
3154 if (gfc_is_proc_ptr_comp (expr))
3155 return true;
3157 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3158 another caf_get. */
3159 if (sym && sym->attr.intrinsic
3160 && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
3161 || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
3162 return true;
3164 if (expr->ref)
3166 gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
3167 &expr->where);
3168 return false;
3171 if (sym && sym->attr.intrinsic
3172 && !gfc_resolve_intrinsic (sym, &expr->where))
3173 return false;
3175 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3177 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
3178 return false;
3181 /* If this is a deferred TBP with an abstract interface (which may
3182 of course be referenced), expr->value.function.esym will be set. */
3183 if (sym && sym->attr.abstract && !expr->value.function.esym)
3185 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3186 sym->name, &expr->where);
3187 return false;
3190 /* If this is a deferred TBP with an abstract interface, its result
3191 cannot be an assumed length character (F2003: C418). */
3192 if (sym && sym->attr.abstract && sym->attr.function
3193 && sym->result->ts.u.cl
3194 && sym->result->ts.u.cl->length == NULL
3195 && !sym->result->ts.deferred)
3197 gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
3198 "character length result (F2008: C418)", sym->name,
3199 &sym->declared_at);
3200 return false;
3203 /* Switch off assumed size checking and do this again for certain kinds
3204 of procedure, once the procedure itself is resolved. */
3205 need_full_assumed_size++;
3207 if (expr->symtree && expr->symtree->n.sym)
3208 p = expr->symtree->n.sym->attr.proc;
3210 if (expr->value.function.isym && expr->value.function.isym->inquiry)
3211 inquiry_argument = true;
3212 no_formal_args = sym && is_external_proc (sym)
3213 && gfc_sym_get_dummy_args (sym) == NULL;
3215 if (!resolve_actual_arglist (expr->value.function.actual,
3216 p, no_formal_args))
3218 inquiry_argument = false;
3219 return false;
3222 inquiry_argument = false;
3224 /* Resume assumed_size checking. */
3225 need_full_assumed_size--;
3227 /* If the procedure is external, check for usage. */
3228 if (sym && is_external_proc (sym))
3229 resolve_global_procedure (sym, &expr->where, 0);
3231 if (sym && sym->ts.type == BT_CHARACTER
3232 && sym->ts.u.cl
3233 && sym->ts.u.cl->length == NULL
3234 && !sym->attr.dummy
3235 && !sym->ts.deferred
3236 && expr->value.function.esym == NULL
3237 && !sym->attr.contained)
3239 /* Internal procedures are taken care of in resolve_contained_fntype. */
3240 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3241 "be used at %L since it is not a dummy argument",
3242 sym->name, &expr->where);
3243 return false;
3246 /* See if function is already resolved. */
3248 if (expr->value.function.name != NULL
3249 || expr->value.function.isym != NULL)
3251 if (expr->ts.type == BT_UNKNOWN)
3252 expr->ts = sym->ts;
3253 t = true;
3255 else
3257 /* Apply the rules of section 14.1.2. */
3259 switch (procedure_kind (sym))
3261 case PTYPE_GENERIC:
3262 t = resolve_generic_f (expr);
3263 break;
3265 case PTYPE_SPECIFIC:
3266 t = resolve_specific_f (expr);
3267 break;
3269 case PTYPE_UNKNOWN:
3270 t = resolve_unknown_f (expr);
3271 break;
3273 default:
3274 gfc_internal_error ("resolve_function(): bad function type");
3278 /* If the expression is still a function (it might have simplified),
3279 then we check to see if we are calling an elemental function. */
3281 if (expr->expr_type != EXPR_FUNCTION)
3282 return t;
3284 /* Walk the argument list looking for invalid BOZ. */
3285 for (arg = expr->value.function.actual; arg; arg = arg->next)
3286 if (arg->expr && arg->expr->ts.type == BT_BOZ)
3288 gfc_error ("A BOZ literal constant at %L cannot appear as an "
3289 "actual argument in a function reference",
3290 &arg->expr->where);
3291 return false;
3294 temp = need_full_assumed_size;
3295 need_full_assumed_size = 0;
3297 if (!resolve_elemental_actual (expr, NULL))
3298 return false;
3300 if (omp_workshare_flag
3301 && expr->value.function.esym
3302 && ! gfc_elemental (expr->value.function.esym))
3304 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3305 "in WORKSHARE construct", expr->value.function.esym->name,
3306 &expr->where);
3307 t = false;
3310 #define GENERIC_ID expr->value.function.isym->id
3311 else if (expr->value.function.actual != NULL
3312 && expr->value.function.isym != NULL
3313 && GENERIC_ID != GFC_ISYM_LBOUND
3314 && GENERIC_ID != GFC_ISYM_LCOBOUND
3315 && GENERIC_ID != GFC_ISYM_UCOBOUND
3316 && GENERIC_ID != GFC_ISYM_LEN
3317 && GENERIC_ID != GFC_ISYM_LOC
3318 && GENERIC_ID != GFC_ISYM_C_LOC
3319 && GENERIC_ID != GFC_ISYM_PRESENT)
3321 /* Array intrinsics must also have the last upper bound of an
3322 assumed size array argument. UBOUND and SIZE have to be
3323 excluded from the check if the second argument is anything
3324 than a constant. */
3326 for (arg = expr->value.function.actual; arg; arg = arg->next)
3328 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3329 && arg == expr->value.function.actual
3330 && arg->next != NULL && arg->next->expr)
3332 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3333 break;
3335 if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
3336 break;
3338 if ((int)mpz_get_si (arg->next->expr->value.integer)
3339 < arg->expr->rank)
3340 break;
3343 if (arg->expr != NULL
3344 && arg->expr->rank > 0
3345 && resolve_assumed_size_actual (arg->expr))
3346 return false;
3349 #undef GENERIC_ID
3351 need_full_assumed_size = temp;
3353 if (!check_pure_function(expr))
3354 t = false;
3356 /* Functions without the RECURSIVE attribution are not allowed to
3357 * call themselves. */
3358 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3360 gfc_symbol *esym;
3361 esym = expr->value.function.esym;
3363 if (is_illegal_recursion (esym, gfc_current_ns))
3365 if (esym->attr.entry && esym->ns->entries)
3366 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3367 " function %qs is not RECURSIVE",
3368 esym->name, &expr->where, esym->ns->entries->sym->name);
3369 else
3370 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3371 " is not RECURSIVE", esym->name, &expr->where);
3373 t = false;
3377 /* Character lengths of use associated functions may contains references to
3378 symbols not referenced from the current program unit otherwise. Make sure
3379 those symbols are marked as referenced. */
3381 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3382 && expr->value.function.esym->attr.use_assoc)
3384 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3387 /* Make sure that the expression has a typespec that works. */
3388 if (expr->ts.type == BT_UNKNOWN)
3390 if (expr->symtree->n.sym->result
3391 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3392 && !expr->symtree->n.sym->result->attr.proc_pointer)
3393 expr->ts = expr->symtree->n.sym->result->ts;
3396 if (!expr->ref && !expr->value.function.isym)
3398 if (expr->value.function.esym)
3399 update_current_proc_array_outer_dependency (expr->value.function.esym);
3400 else
3401 update_current_proc_array_outer_dependency (sym);
3403 else if (expr->ref)
3404 /* typebound procedure: Assume the worst. */
3405 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3407 return t;
3411 /************* Subroutine resolution *************/
3413 static bool
3414 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3416 if (gfc_pure (sym))
3417 return true;
3419 if (forall_flag)
3421 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3422 name, loc);
3423 return false;
3425 else if (gfc_do_concurrent_flag)
3427 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3428 "PURE", name, loc);
3429 return false;
3431 else if (gfc_pure (NULL))
3433 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3434 return false;
3437 gfc_unset_implicit_pure (NULL);
3438 return true;
3442 static match
3443 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3445 gfc_symbol *s;
3447 if (sym->attr.generic)
3449 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3450 if (s != NULL)
3452 c->resolved_sym = s;
3453 if (!pure_subroutine (s, s->name, &c->loc))
3454 return MATCH_ERROR;
3455 return MATCH_YES;
3458 /* TODO: Need to search for elemental references in generic interface. */
3461 if (sym->attr.intrinsic)
3462 return gfc_intrinsic_sub_interface (c, 0);
3464 return MATCH_NO;
3468 static bool
3469 resolve_generic_s (gfc_code *c)
3471 gfc_symbol *sym;
3472 match m;
3474 sym = c->symtree->n.sym;
3476 for (;;)
3478 m = resolve_generic_s0 (c, sym);
3479 if (m == MATCH_YES)
3480 return true;
3481 else if (m == MATCH_ERROR)
3482 return false;
3484 generic:
3485 if (sym->ns->parent == NULL)
3486 break;
3487 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3489 if (sym == NULL)
3490 break;
3491 if (!generic_sym (sym))
3492 goto generic;
3495 /* Last ditch attempt. See if the reference is to an intrinsic
3496 that possesses a matching interface. 14.1.2.4 */
3497 sym = c->symtree->n.sym;
3499 if (!gfc_is_intrinsic (sym, 1, c->loc))
3501 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3502 sym->name, &c->loc);
3503 return false;
3506 m = gfc_intrinsic_sub_interface (c, 0);
3507 if (m == MATCH_YES)
3508 return true;
3509 if (m == MATCH_NO)
3510 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3511 "intrinsic subroutine interface", sym->name, &c->loc);
3513 return false;
3517 /* Resolve a subroutine call known to be specific. */
3519 static match
3520 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3522 match m;
3524 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3526 if (sym->attr.dummy)
3528 sym->attr.proc = PROC_DUMMY;
3529 goto found;
3532 sym->attr.proc = PROC_EXTERNAL;
3533 goto found;
3536 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3537 goto found;
3539 if (sym->attr.intrinsic)
3541 m = gfc_intrinsic_sub_interface (c, 1);
3542 if (m == MATCH_YES)
3543 return MATCH_YES;
3544 if (m == MATCH_NO)
3545 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3546 "with an intrinsic", sym->name, &c->loc);
3548 return MATCH_ERROR;
3551 return MATCH_NO;
3553 found:
3554 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3556 c->resolved_sym = sym;
3557 if (!pure_subroutine (sym, sym->name, &c->loc))
3558 return MATCH_ERROR;
3560 return MATCH_YES;
3564 static bool
3565 resolve_specific_s (gfc_code *c)
3567 gfc_symbol *sym;
3568 match m;
3570 sym = c->symtree->n.sym;
3572 for (;;)
3574 m = resolve_specific_s0 (c, sym);
3575 if (m == MATCH_YES)
3576 return true;
3577 if (m == MATCH_ERROR)
3578 return false;
3580 if (sym->ns->parent == NULL)
3581 break;
3583 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3585 if (sym == NULL)
3586 break;
3589 sym = c->symtree->n.sym;
3590 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3591 sym->name, &c->loc);
3593 return false;
3597 /* Resolve a subroutine call not known to be generic nor specific. */
3599 static bool
3600 resolve_unknown_s (gfc_code *c)
3602 gfc_symbol *sym;
3604 sym = c->symtree->n.sym;
3606 if (sym->attr.dummy)
3608 sym->attr.proc = PROC_DUMMY;
3609 goto found;
3612 /* See if we have an intrinsic function reference. */
3614 if (gfc_is_intrinsic (sym, 1, c->loc))
3616 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3617 return true;
3618 return false;
3621 /* The reference is to an external name. */
3623 found:
3624 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3626 c->resolved_sym = sym;
3628 return pure_subroutine (sym, sym->name, &c->loc);
3632 /* Resolve a subroutine call. Although it was tempting to use the same code
3633 for functions, subroutines and functions are stored differently and this
3634 makes things awkward. */
3636 static bool
3637 resolve_call (gfc_code *c)
3639 bool t;
3640 procedure_type ptype = PROC_INTRINSIC;
3641 gfc_symbol *csym, *sym;
3642 bool no_formal_args;
3644 csym = c->symtree ? c->symtree->n.sym : NULL;
3646 if (csym && csym->ts.type != BT_UNKNOWN)
3648 gfc_error ("%qs at %L has a type, which is not consistent with "
3649 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3650 return false;
3653 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3655 gfc_symtree *st;
3656 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3657 sym = st ? st->n.sym : NULL;
3658 if (sym && csym != sym
3659 && sym->ns == gfc_current_ns
3660 && sym->attr.flavor == FL_PROCEDURE
3661 && sym->attr.contained)
3663 sym->refs++;
3664 if (csym->attr.generic)
3665 c->symtree->n.sym = sym;
3666 else
3667 c->symtree = st;
3668 csym = c->symtree->n.sym;
3672 /* If this ia a deferred TBP, c->expr1 will be set. */
3673 if (!c->expr1 && csym)
3675 if (csym->attr.abstract)
3677 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3678 csym->name, &c->loc);
3679 return false;
3682 /* Subroutines without the RECURSIVE attribution are not allowed to
3683 call themselves. */
3684 if (is_illegal_recursion (csym, gfc_current_ns))
3686 if (csym->attr.entry && csym->ns->entries)
3687 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3688 "as subroutine %qs is not RECURSIVE",
3689 csym->name, &c->loc, csym->ns->entries->sym->name);
3690 else
3691 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3692 "as it is not RECURSIVE", csym->name, &c->loc);
3694 t = false;
3698 /* Switch off assumed size checking and do this again for certain kinds
3699 of procedure, once the procedure itself is resolved. */
3700 need_full_assumed_size++;
3702 if (csym)
3703 ptype = csym->attr.proc;
3705 no_formal_args = csym && is_external_proc (csym)
3706 && gfc_sym_get_dummy_args (csym) == NULL;
3707 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3708 return false;
3710 /* Resume assumed_size checking. */
3711 need_full_assumed_size--;
3713 /* If external, check for usage. */
3714 if (csym && is_external_proc (csym))
3715 resolve_global_procedure (csym, &c->loc, 1);
3717 t = true;
3718 if (c->resolved_sym == NULL)
3720 c->resolved_isym = NULL;
3721 switch (procedure_kind (csym))
3723 case PTYPE_GENERIC:
3724 t = resolve_generic_s (c);
3725 break;
3727 case PTYPE_SPECIFIC:
3728 t = resolve_specific_s (c);
3729 break;
3731 case PTYPE_UNKNOWN:
3732 t = resolve_unknown_s (c);
3733 break;
3735 default:
3736 gfc_internal_error ("resolve_subroutine(): bad function type");
3740 /* Some checks of elemental subroutine actual arguments. */
3741 if (!resolve_elemental_actual (NULL, c))
3742 return false;
3744 if (!c->expr1)
3745 update_current_proc_array_outer_dependency (csym);
3746 else
3747 /* Typebound procedure: Assume the worst. */
3748 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3750 return t;
3754 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3755 op1->shape and op2->shape are non-NULL return true if their shapes
3756 match. If both op1->shape and op2->shape are non-NULL return false
3757 if their shapes do not match. If either op1->shape or op2->shape is
3758 NULL, return true. */
3760 static bool
3761 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3763 bool t;
3764 int i;
3766 t = true;
3768 if (op1->shape != NULL && op2->shape != NULL)
3770 for (i = 0; i < op1->rank; i++)
3772 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3774 gfc_error ("Shapes for operands at %L and %L are not conformable",
3775 &op1->where, &op2->where);
3776 t = false;
3777 break;
3782 return t;
3785 /* Convert a logical operator to the corresponding bitwise intrinsic call.
3786 For example A .AND. B becomes IAND(A, B). */
3787 static gfc_expr *
3788 logical_to_bitwise (gfc_expr *e)
3790 gfc_expr *tmp, *op1, *op2;
3791 gfc_isym_id isym;
3792 gfc_actual_arglist *args = NULL;
3794 gcc_assert (e->expr_type == EXPR_OP);
3796 isym = GFC_ISYM_NONE;
3797 op1 = e->value.op.op1;
3798 op2 = e->value.op.op2;
3800 switch (e->value.op.op)
3802 case INTRINSIC_NOT:
3803 isym = GFC_ISYM_NOT;
3804 break;
3805 case INTRINSIC_AND:
3806 isym = GFC_ISYM_IAND;
3807 break;
3808 case INTRINSIC_OR:
3809 isym = GFC_ISYM_IOR;
3810 break;
3811 case INTRINSIC_NEQV:
3812 isym = GFC_ISYM_IEOR;
3813 break;
3814 case INTRINSIC_EQV:
3815 /* "Bitwise eqv" is just the complement of NEQV === IEOR.
3816 Change the old expression to NEQV, which will get replaced by IEOR,
3817 and wrap it in NOT. */
3818 tmp = gfc_copy_expr (e);
3819 tmp->value.op.op = INTRINSIC_NEQV;
3820 tmp = logical_to_bitwise (tmp);
3821 isym = GFC_ISYM_NOT;
3822 op1 = tmp;
3823 op2 = NULL;
3824 break;
3825 default:
3826 gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
3829 /* Inherit the original operation's operands as arguments. */
3830 args = gfc_get_actual_arglist ();
3831 args->expr = op1;
3832 if (op2)
3834 args->next = gfc_get_actual_arglist ();
3835 args->next->expr = op2;
3838 /* Convert the expression to a function call. */
3839 e->expr_type = EXPR_FUNCTION;
3840 e->value.function.actual = args;
3841 e->value.function.isym = gfc_intrinsic_function_by_id (isym);
3842 e->value.function.name = e->value.function.isym->name;
3843 e->value.function.esym = NULL;
3845 /* Make up a pre-resolved function call symtree if we need to. */
3846 if (!e->symtree || !e->symtree->n.sym)
3848 gfc_symbol *sym;
3849 gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
3850 sym = e->symtree->n.sym;
3851 sym->result = sym;
3852 sym->attr.flavor = FL_PROCEDURE;
3853 sym->attr.function = 1;
3854 sym->attr.elemental = 1;
3855 sym->attr.pure = 1;
3856 sym->attr.referenced = 1;
3857 gfc_intrinsic_symbol (sym);
3858 gfc_commit_symbol (sym);
3861 args->name = e->value.function.isym->formal->name;
3862 if (e->value.function.isym->formal->next)
3863 args->next->name = e->value.function.isym->formal->next->name;
3865 return e;
3868 /* Recursively append candidate UOP to CANDIDATES. Store the number of
3869 candidates in CANDIDATES_LEN. */
3870 static void
3871 lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
3872 char **&candidates,
3873 size_t &candidates_len)
3875 gfc_symtree *p;
3877 if (uop == NULL)
3878 return;
3880 /* Not sure how to properly filter here. Use all for a start.
3881 n.uop.op is NULL for empty interface operators (is that legal?) disregard
3882 these as i suppose they don't make terribly sense. */
3884 if (uop->n.uop->op != NULL)
3885 vec_push (candidates, candidates_len, uop->name);
3887 p = uop->left;
3888 if (p)
3889 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
3891 p = uop->right;
3892 if (p)
3893 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
3896 /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
3898 static const char*
3899 lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
3901 char **candidates = NULL;
3902 size_t candidates_len = 0;
3903 lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
3904 return gfc_closest_fuzzy_match (op, candidates);
3908 /* Callback finding an impure function as an operand to an .and. or
3909 .or. expression. Remember the last function warned about to
3910 avoid double warnings when recursing. */
3912 static int
3913 impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
3914 void *data)
3916 gfc_expr *f = *e;
3917 const char *name;
3918 static gfc_expr *last = NULL;
3919 bool *found = (bool *) data;
3921 if (f->expr_type == EXPR_FUNCTION)
3923 *found = 1;
3924 if (f != last && !gfc_pure_function (f, &name)
3925 && !gfc_implicit_pure_function (f))
3927 if (name)
3928 gfc_warning (OPT_Wfunction_elimination,
3929 "Impure function %qs at %L might not be evaluated",
3930 name, &f->where);
3931 else
3932 gfc_warning (OPT_Wfunction_elimination,
3933 "Impure function at %L might not be evaluated",
3934 &f->where);
3936 last = f;
3939 return 0;
3942 /* Return true if TYPE is character based, false otherwise. */
3944 static int
3945 is_character_based (bt type)
3947 return type == BT_CHARACTER || type == BT_HOLLERITH;
3951 /* If expression is a hollerith, convert it to character and issue a warning
3952 for the conversion. */
3954 static void
3955 convert_hollerith_to_character (gfc_expr *e)
3957 if (e->ts.type == BT_HOLLERITH)
3959 gfc_typespec t;
3960 gfc_clear_ts (&t);
3961 t.type = BT_CHARACTER;
3962 t.kind = e->ts.kind;
3963 gfc_convert_type_warn (e, &t, 2, 1);
3967 /* Convert to numeric and issue a warning for the conversion. */
3969 static void
3970 convert_to_numeric (gfc_expr *a, gfc_expr *b)
3972 gfc_typespec t;
3973 gfc_clear_ts (&t);
3974 t.type = b->ts.type;
3975 t.kind = b->ts.kind;
3976 gfc_convert_type_warn (a, &t, 2, 1);
3979 /* Resolve an operator expression node. This can involve replacing the
3980 operation with a user defined function call. */
3982 static bool
3983 resolve_operator (gfc_expr *e)
3985 gfc_expr *op1, *op2;
3986 char msg[200];
3987 bool dual_locus_error;
3988 bool t = true;
3990 /* Resolve all subnodes-- give them types. */
3992 switch (e->value.op.op)
3994 default:
3995 if (!gfc_resolve_expr (e->value.op.op2))
3996 return false;
3998 /* Fall through. */
4000 case INTRINSIC_NOT:
4001 case INTRINSIC_UPLUS:
4002 case INTRINSIC_UMINUS:
4003 case INTRINSIC_PARENTHESES:
4004 if (!gfc_resolve_expr (e->value.op.op1))
4005 return false;
4006 if (e->value.op.op1
4007 && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
4009 gfc_error ("BOZ literal constant at %L cannot be an operand of "
4010 "unary operator %qs", &e->value.op.op1->where,
4011 gfc_op2string (e->value.op.op));
4012 return false;
4014 break;
4017 /* Typecheck the new node. */
4019 op1 = e->value.op.op1;
4020 op2 = e->value.op.op2;
4021 if (op1 == NULL && op2 == NULL)
4022 return false;
4024 dual_locus_error = false;
4026 /* op1 and op2 cannot both be BOZ. */
4027 if (op1 && op1->ts.type == BT_BOZ
4028 && op2 && op2->ts.type == BT_BOZ)
4030 gfc_error ("Operands at %L and %L cannot appear as operands of "
4031 "binary operator %qs", &op1->where, &op2->where,
4032 gfc_op2string (e->value.op.op));
4033 return false;
4036 if ((op1 && op1->expr_type == EXPR_NULL)
4037 || (op2 && op2->expr_type == EXPR_NULL))
4039 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
4040 goto bad_op;
4043 switch (e->value.op.op)
4045 case INTRINSIC_UPLUS:
4046 case INTRINSIC_UMINUS:
4047 if (op1->ts.type == BT_INTEGER
4048 || op1->ts.type == BT_REAL
4049 || op1->ts.type == BT_COMPLEX)
4051 e->ts = op1->ts;
4052 break;
4055 sprintf (msg, _("Operand of unary numeric operator %%<%s%%> at %%L is %s"),
4056 gfc_op2string (e->value.op.op), gfc_typename (e));
4057 goto bad_op;
4059 case INTRINSIC_PLUS:
4060 case INTRINSIC_MINUS:
4061 case INTRINSIC_TIMES:
4062 case INTRINSIC_DIVIDE:
4063 case INTRINSIC_POWER:
4064 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4066 gfc_type_convert_binary (e, 1);
4067 break;
4070 if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
4071 sprintf (msg,
4072 _("Unexpected derived-type entities in binary intrinsic "
4073 "numeric operator %%<%s%%> at %%L"),
4074 gfc_op2string (e->value.op.op));
4075 else
4076 sprintf (msg,
4077 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"),
4078 gfc_op2string (e->value.op.op), gfc_typename (op1),
4079 gfc_typename (op2));
4080 goto bad_op;
4082 case INTRINSIC_CONCAT:
4083 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4084 && op1->ts.kind == op2->ts.kind)
4086 e->ts.type = BT_CHARACTER;
4087 e->ts.kind = op1->ts.kind;
4088 break;
4091 sprintf (msg,
4092 _("Operands of string concatenation operator at %%L are %s/%s"),
4093 gfc_typename (op1), gfc_typename (op2));
4094 goto bad_op;
4096 case INTRINSIC_AND:
4097 case INTRINSIC_OR:
4098 case INTRINSIC_EQV:
4099 case INTRINSIC_NEQV:
4100 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4102 e->ts.type = BT_LOGICAL;
4103 e->ts.kind = gfc_kind_max (op1, op2);
4104 if (op1->ts.kind < e->ts.kind)
4105 gfc_convert_type (op1, &e->ts, 2);
4106 else if (op2->ts.kind < e->ts.kind)
4107 gfc_convert_type (op2, &e->ts, 2);
4109 if (flag_frontend_optimize &&
4110 (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
4112 /* Warn about short-circuiting
4113 with impure function as second operand. */
4114 bool op2_f = false;
4115 gfc_expr_walker (&op2, impure_function_callback, &op2_f);
4117 break;
4120 /* Logical ops on integers become bitwise ops with -fdec. */
4121 else if (flag_dec
4122 && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
4124 e->ts.type = BT_INTEGER;
4125 e->ts.kind = gfc_kind_max (op1, op2);
4126 if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
4127 gfc_convert_type (op1, &e->ts, 1);
4128 if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
4129 gfc_convert_type (op2, &e->ts, 1);
4130 e = logical_to_bitwise (e);
4131 goto simplify_op;
4134 sprintf (msg, _("Operands of logical operator %%<%s%%> at %%L are %s/%s"),
4135 gfc_op2string (e->value.op.op), gfc_typename (op1),
4136 gfc_typename (op2));
4138 goto bad_op;
4140 case INTRINSIC_NOT:
4141 /* Logical ops on integers become bitwise ops with -fdec. */
4142 if (flag_dec && op1->ts.type == BT_INTEGER)
4144 e->ts.type = BT_INTEGER;
4145 e->ts.kind = op1->ts.kind;
4146 e = logical_to_bitwise (e);
4147 goto simplify_op;
4150 if (op1->ts.type == BT_LOGICAL)
4152 e->ts.type = BT_LOGICAL;
4153 e->ts.kind = op1->ts.kind;
4154 break;
4157 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
4158 gfc_typename (op1));
4159 goto bad_op;
4161 case INTRINSIC_GT:
4162 case INTRINSIC_GT_OS:
4163 case INTRINSIC_GE:
4164 case INTRINSIC_GE_OS:
4165 case INTRINSIC_LT:
4166 case INTRINSIC_LT_OS:
4167 case INTRINSIC_LE:
4168 case INTRINSIC_LE_OS:
4169 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
4171 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
4172 goto bad_op;
4175 /* Fall through. */
4177 case INTRINSIC_EQ:
4178 case INTRINSIC_EQ_OS:
4179 case INTRINSIC_NE:
4180 case INTRINSIC_NE_OS:
4182 if (flag_dec
4183 && is_character_based (op1->ts.type)
4184 && is_character_based (op2->ts.type))
4186 convert_hollerith_to_character (op1);
4187 convert_hollerith_to_character (op2);
4190 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4191 && op1->ts.kind == op2->ts.kind)
4193 e->ts.type = BT_LOGICAL;
4194 e->ts.kind = gfc_default_logical_kind;
4195 break;
4198 /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4199 if (op1->ts.type == BT_BOZ)
4201 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4202 "as an operand of a relational operator"),
4203 &op1->where))
4204 return false;
4206 if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
4207 return false;
4209 if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
4210 return false;
4213 /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4214 if (op2->ts.type == BT_BOZ)
4216 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4217 " as an operand of a relational operator"),
4218 &op2->where))
4219 return false;
4221 if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
4222 return false;
4224 if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
4225 return false;
4227 if (flag_dec
4228 && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
4229 convert_to_numeric (op1, op2);
4231 if (flag_dec
4232 && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
4233 convert_to_numeric (op2, op1);
4235 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4237 gfc_type_convert_binary (e, 1);
4239 e->ts.type = BT_LOGICAL;
4240 e->ts.kind = gfc_default_logical_kind;
4242 if (warn_compare_reals)
4244 gfc_intrinsic_op op = e->value.op.op;
4246 /* Type conversion has made sure that the types of op1 and op2
4247 agree, so it is only necessary to check the first one. */
4248 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
4249 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
4250 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
4252 const char *msg;
4254 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
4255 msg = G_("Equality comparison for %s at %L");
4256 else
4257 msg = G_("Inequality comparison for %s at %L");
4259 gfc_warning (OPT_Wcompare_reals, msg,
4260 gfc_typename (op1), &op1->where);
4264 break;
4267 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4268 sprintf (msg,
4269 _("Logicals at %%L must be compared with %s instead of %s"),
4270 (e->value.op.op == INTRINSIC_EQ
4271 || e->value.op.op == INTRINSIC_EQ_OS)
4272 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
4273 else
4274 sprintf (msg,
4275 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"),
4276 gfc_op2string (e->value.op.op), gfc_typename (op1),
4277 gfc_typename (op2));
4279 goto bad_op;
4281 case INTRINSIC_USER:
4282 if (e->value.op.uop->op == NULL)
4284 const char *name = e->value.op.uop->name;
4285 const char *guessed;
4286 guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
4287 if (guessed)
4288 sprintf (msg, _("Unknown operator %%<%s%%> at %%L; did you mean '%s'?"),
4289 name, guessed);
4290 else
4291 sprintf (msg, _("Unknown operator %%<%s%%> at %%L"), name);
4293 else if (op2 == NULL)
4294 sprintf (msg, _("Operand of user operator %%<%s%%> at %%L is %s"),
4295 e->value.op.uop->name, gfc_typename (op1));
4296 else
4298 sprintf (msg, _("Operands of user operator %%<%s%%> at %%L are %s/%s"),
4299 e->value.op.uop->name, gfc_typename (op1),
4300 gfc_typename (op2));
4301 e->value.op.uop->op->sym->attr.referenced = 1;
4304 goto bad_op;
4306 case INTRINSIC_PARENTHESES:
4307 e->ts = op1->ts;
4308 if (e->ts.type == BT_CHARACTER)
4309 e->ts.u.cl = op1->ts.u.cl;
4310 break;
4312 default:
4313 gfc_internal_error ("resolve_operator(): Bad intrinsic");
4316 /* Deal with arrayness of an operand through an operator. */
4318 switch (e->value.op.op)
4320 case INTRINSIC_PLUS:
4321 case INTRINSIC_MINUS:
4322 case INTRINSIC_TIMES:
4323 case INTRINSIC_DIVIDE:
4324 case INTRINSIC_POWER:
4325 case INTRINSIC_CONCAT:
4326 case INTRINSIC_AND:
4327 case INTRINSIC_OR:
4328 case INTRINSIC_EQV:
4329 case INTRINSIC_NEQV:
4330 case INTRINSIC_EQ:
4331 case INTRINSIC_EQ_OS:
4332 case INTRINSIC_NE:
4333 case INTRINSIC_NE_OS:
4334 case INTRINSIC_GT:
4335 case INTRINSIC_GT_OS:
4336 case INTRINSIC_GE:
4337 case INTRINSIC_GE_OS:
4338 case INTRINSIC_LT:
4339 case INTRINSIC_LT_OS:
4340 case INTRINSIC_LE:
4341 case INTRINSIC_LE_OS:
4343 if (op1->rank == 0 && op2->rank == 0)
4344 e->rank = 0;
4346 if (op1->rank == 0 && op2->rank != 0)
4348 e->rank = op2->rank;
4350 if (e->shape == NULL)
4351 e->shape = gfc_copy_shape (op2->shape, op2->rank);
4354 if (op1->rank != 0 && op2->rank == 0)
4356 e->rank = op1->rank;
4358 if (e->shape == NULL)
4359 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4362 if (op1->rank != 0 && op2->rank != 0)
4364 if (op1->rank == op2->rank)
4366 e->rank = op1->rank;
4367 if (e->shape == NULL)
4369 t = compare_shapes (op1, op2);
4370 if (!t)
4371 e->shape = NULL;
4372 else
4373 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4376 else
4378 /* Allow higher level expressions to work. */
4379 e->rank = 0;
4381 /* Try user-defined operators, and otherwise throw an error. */
4382 dual_locus_error = true;
4383 sprintf (msg,
4384 _("Inconsistent ranks for operator at %%L and %%L"));
4385 goto bad_op;
4389 break;
4391 case INTRINSIC_PARENTHESES:
4392 case INTRINSIC_NOT:
4393 case INTRINSIC_UPLUS:
4394 case INTRINSIC_UMINUS:
4395 /* Simply copy arrayness attribute */
4396 e->rank = op1->rank;
4398 if (e->shape == NULL)
4399 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4401 break;
4403 default:
4404 break;
4407 simplify_op:
4409 /* Attempt to simplify the expression. */
4410 if (t)
4412 t = gfc_simplify_expr (e, 0);
4413 /* Some calls do not succeed in simplification and return false
4414 even though there is no error; e.g. variable references to
4415 PARAMETER arrays. */
4416 if (!gfc_is_constant_expr (e))
4417 t = true;
4419 return t;
4421 bad_op:
4424 match m = gfc_extend_expr (e);
4425 if (m == MATCH_YES)
4426 return true;
4427 if (m == MATCH_ERROR)
4428 return false;
4431 if (dual_locus_error)
4432 gfc_error (msg, &op1->where, &op2->where);
4433 else
4434 gfc_error (msg, &e->where);
4436 return false;
4440 /************** Array resolution subroutines **************/
4442 enum compare_result
4443 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
4445 /* Compare two integer expressions. */
4447 static compare_result
4448 compare_bound (gfc_expr *a, gfc_expr *b)
4450 int i;
4452 if (a == NULL || a->expr_type != EXPR_CONSTANT
4453 || b == NULL || b->expr_type != EXPR_CONSTANT)
4454 return CMP_UNKNOWN;
4456 /* If either of the types isn't INTEGER, we must have
4457 raised an error earlier. */
4459 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4460 return CMP_UNKNOWN;
4462 i = mpz_cmp (a->value.integer, b->value.integer);
4464 if (i < 0)
4465 return CMP_LT;
4466 if (i > 0)
4467 return CMP_GT;
4468 return CMP_EQ;
4472 /* Compare an integer expression with an integer. */
4474 static compare_result
4475 compare_bound_int (gfc_expr *a, int b)
4477 int i;
4479 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4480 return CMP_UNKNOWN;
4482 if (a->ts.type != BT_INTEGER)
4483 gfc_internal_error ("compare_bound_int(): Bad expression");
4485 i = mpz_cmp_si (a->value.integer, b);
4487 if (i < 0)
4488 return CMP_LT;
4489 if (i > 0)
4490 return CMP_GT;
4491 return CMP_EQ;
4495 /* Compare an integer expression with a mpz_t. */
4497 static compare_result
4498 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4500 int i;
4502 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4503 return CMP_UNKNOWN;
4505 if (a->ts.type != BT_INTEGER)
4506 gfc_internal_error ("compare_bound_int(): Bad expression");
4508 i = mpz_cmp (a->value.integer, b);
4510 if (i < 0)
4511 return CMP_LT;
4512 if (i > 0)
4513 return CMP_GT;
4514 return CMP_EQ;
4518 /* Compute the last value of a sequence given by a triplet.
4519 Return 0 if it wasn't able to compute the last value, or if the
4520 sequence if empty, and 1 otherwise. */
4522 static int
4523 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4524 gfc_expr *stride, mpz_t last)
4526 mpz_t rem;
4528 if (start == NULL || start->expr_type != EXPR_CONSTANT
4529 || end == NULL || end->expr_type != EXPR_CONSTANT
4530 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4531 return 0;
4533 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4534 || (stride != NULL && stride->ts.type != BT_INTEGER))
4535 return 0;
4537 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
4539 if (compare_bound (start, end) == CMP_GT)
4540 return 0;
4541 mpz_set (last, end->value.integer);
4542 return 1;
4545 if (compare_bound_int (stride, 0) == CMP_GT)
4547 /* Stride is positive */
4548 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4549 return 0;
4551 else
4553 /* Stride is negative */
4554 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4555 return 0;
4558 mpz_init (rem);
4559 mpz_sub (rem, end->value.integer, start->value.integer);
4560 mpz_tdiv_r (rem, rem, stride->value.integer);
4561 mpz_sub (last, end->value.integer, rem);
4562 mpz_clear (rem);
4564 return 1;
4568 /* Compare a single dimension of an array reference to the array
4569 specification. */
4571 static bool
4572 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4574 mpz_t last_value;
4576 if (ar->dimen_type[i] == DIMEN_STAR)
4578 gcc_assert (ar->stride[i] == NULL);
4579 /* This implies [*] as [*:] and [*:3] are not possible. */
4580 if (ar->start[i] == NULL)
4582 gcc_assert (ar->end[i] == NULL);
4583 return true;
4587 /* Given start, end and stride values, calculate the minimum and
4588 maximum referenced indexes. */
4590 switch (ar->dimen_type[i])
4592 case DIMEN_VECTOR:
4593 case DIMEN_THIS_IMAGE:
4594 break;
4596 case DIMEN_STAR:
4597 case DIMEN_ELEMENT:
4598 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4600 if (i < as->rank)
4601 gfc_warning (0, "Array reference at %L is out of bounds "
4602 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4603 mpz_get_si (ar->start[i]->value.integer),
4604 mpz_get_si (as->lower[i]->value.integer), i+1);
4605 else
4606 gfc_warning (0, "Array reference at %L is out of bounds "
4607 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4608 mpz_get_si (ar->start[i]->value.integer),
4609 mpz_get_si (as->lower[i]->value.integer),
4610 i + 1 - as->rank);
4611 return true;
4613 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4615 if (i < as->rank)
4616 gfc_warning (0, "Array reference at %L is out of bounds "
4617 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4618 mpz_get_si (ar->start[i]->value.integer),
4619 mpz_get_si (as->upper[i]->value.integer), i+1);
4620 else
4621 gfc_warning (0, "Array reference at %L is out of bounds "
4622 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4623 mpz_get_si (ar->start[i]->value.integer),
4624 mpz_get_si (as->upper[i]->value.integer),
4625 i + 1 - as->rank);
4626 return true;
4629 break;
4631 case DIMEN_RANGE:
4633 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4634 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4636 compare_result comp_start_end = compare_bound (AR_START, AR_END);
4638 /* Check for zero stride, which is not allowed. */
4639 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4641 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4642 return false;
4645 /* if start == len || (stride > 0 && start < len)
4646 || (stride < 0 && start > len),
4647 then the array section contains at least one element. In this
4648 case, there is an out-of-bounds access if
4649 (start < lower || start > upper). */
4650 if (compare_bound (AR_START, AR_END) == CMP_EQ
4651 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4652 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4653 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4654 && comp_start_end == CMP_GT))
4656 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4658 gfc_warning (0, "Lower array reference at %L is out of bounds "
4659 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4660 mpz_get_si (AR_START->value.integer),
4661 mpz_get_si (as->lower[i]->value.integer), i+1);
4662 return true;
4664 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4666 gfc_warning (0, "Lower array reference at %L is out of bounds "
4667 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4668 mpz_get_si (AR_START->value.integer),
4669 mpz_get_si (as->upper[i]->value.integer), i+1);
4670 return true;
4674 /* If we can compute the highest index of the array section,
4675 then it also has to be between lower and upper. */
4676 mpz_init (last_value);
4677 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4678 last_value))
4680 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4682 gfc_warning (0, "Upper array reference at %L is out of bounds "
4683 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4684 mpz_get_si (last_value),
4685 mpz_get_si (as->lower[i]->value.integer), i+1);
4686 mpz_clear (last_value);
4687 return true;
4689 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4691 gfc_warning (0, "Upper array reference at %L is out of bounds "
4692 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4693 mpz_get_si (last_value),
4694 mpz_get_si (as->upper[i]->value.integer), i+1);
4695 mpz_clear (last_value);
4696 return true;
4699 mpz_clear (last_value);
4701 #undef AR_START
4702 #undef AR_END
4704 break;
4706 default:
4707 gfc_internal_error ("check_dimension(): Bad array reference");
4710 return true;
4714 /* Compare an array reference with an array specification. */
4716 static bool
4717 compare_spec_to_ref (gfc_array_ref *ar)
4719 gfc_array_spec *as;
4720 int i;
4722 as = ar->as;
4723 i = as->rank - 1;
4724 /* TODO: Full array sections are only allowed as actual parameters. */
4725 if (as->type == AS_ASSUMED_SIZE
4726 && (/*ar->type == AR_FULL
4727 ||*/ (ar->type == AR_SECTION
4728 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4730 gfc_error ("Rightmost upper bound of assumed size array section "
4731 "not specified at %L", &ar->where);
4732 return false;
4735 if (ar->type == AR_FULL)
4736 return true;
4738 if (as->rank != ar->dimen)
4740 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4741 &ar->where, ar->dimen, as->rank);
4742 return false;
4745 /* ar->codimen == 0 is a local array. */
4746 if (as->corank != ar->codimen && ar->codimen != 0)
4748 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4749 &ar->where, ar->codimen, as->corank);
4750 return false;
4753 for (i = 0; i < as->rank; i++)
4754 if (!check_dimension (i, ar, as))
4755 return false;
4757 /* Local access has no coarray spec. */
4758 if (ar->codimen != 0)
4759 for (i = as->rank; i < as->rank + as->corank; i++)
4761 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4762 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4764 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4765 i + 1 - as->rank, &ar->where);
4766 return false;
4768 if (!check_dimension (i, ar, as))
4769 return false;
4772 return true;
4776 /* Resolve one part of an array index. */
4778 static bool
4779 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4780 int force_index_integer_kind)
4782 gfc_typespec ts;
4784 if (index == NULL)
4785 return true;
4787 if (!gfc_resolve_expr (index))
4788 return false;
4790 if (check_scalar && index->rank != 0)
4792 gfc_error ("Array index at %L must be scalar", &index->where);
4793 return false;
4796 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4798 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4799 &index->where, gfc_basic_typename (index->ts.type));
4800 return false;
4803 if (index->ts.type == BT_REAL)
4804 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4805 &index->where))
4806 return false;
4808 if ((index->ts.kind != gfc_index_integer_kind
4809 && force_index_integer_kind)
4810 || index->ts.type != BT_INTEGER)
4812 gfc_clear_ts (&ts);
4813 ts.type = BT_INTEGER;
4814 ts.kind = gfc_index_integer_kind;
4816 gfc_convert_type_warn (index, &ts, 2, 0);
4819 return true;
4822 /* Resolve one part of an array index. */
4824 bool
4825 gfc_resolve_index (gfc_expr *index, int check_scalar)
4827 return gfc_resolve_index_1 (index, check_scalar, 1);
4830 /* Resolve a dim argument to an intrinsic function. */
4832 bool
4833 gfc_resolve_dim_arg (gfc_expr *dim)
4835 if (dim == NULL)
4836 return true;
4838 if (!gfc_resolve_expr (dim))
4839 return false;
4841 if (dim->rank != 0)
4843 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4844 return false;
4848 if (dim->ts.type != BT_INTEGER)
4850 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4851 return false;
4854 if (dim->ts.kind != gfc_index_integer_kind)
4856 gfc_typespec ts;
4858 gfc_clear_ts (&ts);
4859 ts.type = BT_INTEGER;
4860 ts.kind = gfc_index_integer_kind;
4862 gfc_convert_type_warn (dim, &ts, 2, 0);
4865 return true;
4868 /* Given an expression that contains array references, update those array
4869 references to point to the right array specifications. While this is
4870 filled in during matching, this information is difficult to save and load
4871 in a module, so we take care of it here.
4873 The idea here is that the original array reference comes from the
4874 base symbol. We traverse the list of reference structures, setting
4875 the stored reference to references. Component references can
4876 provide an additional array specification. */
4878 static void
4879 find_array_spec (gfc_expr *e)
4881 gfc_array_spec *as;
4882 gfc_component *c;
4883 gfc_ref *ref;
4884 bool class_as = false;
4886 if (e->symtree->n.sym->ts.type == BT_CLASS)
4888 as = CLASS_DATA (e->symtree->n.sym)->as;
4889 class_as = true;
4891 else
4892 as = e->symtree->n.sym->as;
4894 for (ref = e->ref; ref; ref = ref->next)
4895 switch (ref->type)
4897 case REF_ARRAY:
4898 if (as == NULL)
4899 gfc_internal_error ("find_array_spec(): Missing spec");
4901 ref->u.ar.as = as;
4902 as = NULL;
4903 break;
4905 case REF_COMPONENT:
4906 c = ref->u.c.component;
4907 if (c->attr.dimension)
4909 if (as != NULL && !(class_as && as == c->as))
4910 gfc_internal_error ("find_array_spec(): unused as(1)");
4911 as = c->as;
4914 break;
4916 case REF_SUBSTRING:
4917 case REF_INQUIRY:
4918 break;
4921 if (as != NULL)
4922 gfc_internal_error ("find_array_spec(): unused as(2)");
4926 /* Resolve an array reference. */
4928 static bool
4929 resolve_array_ref (gfc_array_ref *ar)
4931 int i, check_scalar;
4932 gfc_expr *e;
4934 for (i = 0; i < ar->dimen + ar->codimen; i++)
4936 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4938 /* Do not force gfc_index_integer_kind for the start. We can
4939 do fine with any integer kind. This avoids temporary arrays
4940 created for indexing with a vector. */
4941 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4942 return false;
4943 if (!gfc_resolve_index (ar->end[i], check_scalar))
4944 return false;
4945 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4946 return false;
4948 e = ar->start[i];
4950 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4951 switch (e->rank)
4953 case 0:
4954 ar->dimen_type[i] = DIMEN_ELEMENT;
4955 break;
4957 case 1:
4958 ar->dimen_type[i] = DIMEN_VECTOR;
4959 if (e->expr_type == EXPR_VARIABLE
4960 && e->symtree->n.sym->ts.type == BT_DERIVED)
4961 ar->start[i] = gfc_get_parentheses (e);
4962 break;
4964 default:
4965 gfc_error ("Array index at %L is an array of rank %d",
4966 &ar->c_where[i], e->rank);
4967 return false;
4970 /* Fill in the upper bound, which may be lower than the
4971 specified one for something like a(2:10:5), which is
4972 identical to a(2:7:5). Only relevant for strides not equal
4973 to one. Don't try a division by zero. */
4974 if (ar->dimen_type[i] == DIMEN_RANGE
4975 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4976 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4977 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4979 mpz_t size, end;
4981 if (gfc_ref_dimen_size (ar, i, &size, &end))
4983 if (ar->end[i] == NULL)
4985 ar->end[i] =
4986 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4987 &ar->where);
4988 mpz_set (ar->end[i]->value.integer, end);
4990 else if (ar->end[i]->ts.type == BT_INTEGER
4991 && ar->end[i]->expr_type == EXPR_CONSTANT)
4993 mpz_set (ar->end[i]->value.integer, end);
4995 else
4996 gcc_unreachable ();
4998 mpz_clear (size);
4999 mpz_clear (end);
5004 if (ar->type == AR_FULL)
5006 if (ar->as->rank == 0)
5007 ar->type = AR_ELEMENT;
5009 /* Make sure array is the same as array(:,:), this way
5010 we don't need to special case all the time. */
5011 ar->dimen = ar->as->rank;
5012 for (i = 0; i < ar->dimen; i++)
5014 ar->dimen_type[i] = DIMEN_RANGE;
5016 gcc_assert (ar->start[i] == NULL);
5017 gcc_assert (ar->end[i] == NULL);
5018 gcc_assert (ar->stride[i] == NULL);
5022 /* If the reference type is unknown, figure out what kind it is. */
5024 if (ar->type == AR_UNKNOWN)
5026 ar->type = AR_ELEMENT;
5027 for (i = 0; i < ar->dimen; i++)
5028 if (ar->dimen_type[i] == DIMEN_RANGE
5029 || ar->dimen_type[i] == DIMEN_VECTOR)
5031 ar->type = AR_SECTION;
5032 break;
5036 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
5037 return false;
5039 if (ar->as->corank && ar->codimen == 0)
5041 int n;
5042 ar->codimen = ar->as->corank;
5043 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
5044 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
5047 return true;
5051 static bool
5052 resolve_substring (gfc_ref *ref, bool *equal_length)
5054 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
5056 if (ref->u.ss.start != NULL)
5058 if (!gfc_resolve_expr (ref->u.ss.start))
5059 return false;
5061 if (ref->u.ss.start->ts.type != BT_INTEGER)
5063 gfc_error ("Substring start index at %L must be of type INTEGER",
5064 &ref->u.ss.start->where);
5065 return false;
5068 if (ref->u.ss.start->rank != 0)
5070 gfc_error ("Substring start index at %L must be scalar",
5071 &ref->u.ss.start->where);
5072 return false;
5075 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
5076 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5077 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5079 gfc_error ("Substring start index at %L is less than one",
5080 &ref->u.ss.start->where);
5081 return false;
5085 if (ref->u.ss.end != NULL)
5087 if (!gfc_resolve_expr (ref->u.ss.end))
5088 return false;
5090 if (ref->u.ss.end->ts.type != BT_INTEGER)
5092 gfc_error ("Substring end index at %L must be of type INTEGER",
5093 &ref->u.ss.end->where);
5094 return false;
5097 if (ref->u.ss.end->rank != 0)
5099 gfc_error ("Substring end index at %L must be scalar",
5100 &ref->u.ss.end->where);
5101 return false;
5104 if (ref->u.ss.length != NULL
5105 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
5106 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5107 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5109 gfc_error ("Substring end index at %L exceeds the string length",
5110 &ref->u.ss.start->where);
5111 return false;
5114 if (compare_bound_mpz_t (ref->u.ss.end,
5115 gfc_integer_kinds[k].huge) == CMP_GT
5116 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5117 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5119 gfc_error ("Substring end index at %L is too large",
5120 &ref->u.ss.end->where);
5121 return false;
5123 /* If the substring has the same length as the original
5124 variable, the reference itself can be deleted. */
5126 if (ref->u.ss.length != NULL
5127 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
5128 && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
5129 *equal_length = true;
5132 return true;
5136 /* This function supplies missing substring charlens. */
5138 void
5139 gfc_resolve_substring_charlen (gfc_expr *e)
5141 gfc_ref *char_ref;
5142 gfc_expr *start, *end;
5143 gfc_typespec *ts = NULL;
5144 mpz_t diff;
5146 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
5148 if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
5149 break;
5150 if (char_ref->type == REF_COMPONENT)
5151 ts = &char_ref->u.c.component->ts;
5154 if (!char_ref || char_ref->type == REF_INQUIRY)
5155 return;
5157 gcc_assert (char_ref->next == NULL);
5159 if (e->ts.u.cl)
5161 if (e->ts.u.cl->length)
5162 gfc_free_expr (e->ts.u.cl->length);
5163 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
5164 return;
5167 if (!e->ts.u.cl)
5168 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5170 if (char_ref->u.ss.start)
5171 start = gfc_copy_expr (char_ref->u.ss.start);
5172 else
5173 start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
5175 if (char_ref->u.ss.end)
5176 end = gfc_copy_expr (char_ref->u.ss.end);
5177 else if (e->expr_type == EXPR_VARIABLE)
5179 if (!ts)
5180 ts = &e->symtree->n.sym->ts;
5181 end = gfc_copy_expr (ts->u.cl->length);
5183 else
5184 end = NULL;
5186 if (!start || !end)
5188 gfc_free_expr (start);
5189 gfc_free_expr (end);
5190 return;
5193 /* Length = (end - start + 1).
5194 Check first whether it has a constant length. */
5195 if (gfc_dep_difference (end, start, &diff))
5197 gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
5198 &e->where);
5200 mpz_add_ui (len->value.integer, diff, 1);
5201 mpz_clear (diff);
5202 e->ts.u.cl->length = len;
5203 /* The check for length < 0 is handled below */
5205 else
5207 e->ts.u.cl->length = gfc_subtract (end, start);
5208 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
5209 gfc_get_int_expr (gfc_charlen_int_kind,
5210 NULL, 1));
5213 /* F2008, 6.4.1: Both the starting point and the ending point shall
5214 be within the range 1, 2, ..., n unless the starting point exceeds
5215 the ending point, in which case the substring has length zero. */
5217 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
5218 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
5220 e->ts.u.cl->length->ts.type = BT_INTEGER;
5221 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5223 /* Make sure that the length is simplified. */
5224 gfc_simplify_expr (e->ts.u.cl->length, 1);
5225 gfc_resolve_expr (e->ts.u.cl->length);
5229 /* Resolve subtype references. */
5231 bool
5232 gfc_resolve_ref (gfc_expr *expr)
5234 int current_part_dimension, n_components, seen_part_dimension, dim;
5235 gfc_ref *ref, **prev, *array_ref;
5236 bool equal_length;
5238 for (ref = expr->ref; ref; ref = ref->next)
5239 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
5241 find_array_spec (expr);
5242 break;
5245 for (prev = &expr->ref; *prev != NULL;
5246 prev = *prev == NULL ? prev : &(*prev)->next)
5247 switch ((*prev)->type)
5249 case REF_ARRAY:
5250 if (!resolve_array_ref (&(*prev)->u.ar))
5251 return false;
5252 break;
5254 case REF_COMPONENT:
5255 case REF_INQUIRY:
5256 break;
5258 case REF_SUBSTRING:
5259 equal_length = false;
5260 if (!resolve_substring (*prev, &equal_length))
5261 return false;
5263 if (expr->expr_type != EXPR_SUBSTRING && equal_length)
5265 /* Remove the reference and move the charlen, if any. */
5266 ref = *prev;
5267 *prev = ref->next;
5268 ref->next = NULL;
5269 expr->ts.u.cl = ref->u.ss.length;
5270 ref->u.ss.length = NULL;
5271 gfc_free_ref_list (ref);
5273 break;
5276 /* Check constraints on part references. */
5278 current_part_dimension = 0;
5279 seen_part_dimension = 0;
5280 n_components = 0;
5281 array_ref = NULL;
5283 for (ref = expr->ref; ref; ref = ref->next)
5285 switch (ref->type)
5287 case REF_ARRAY:
5288 array_ref = ref;
5289 switch (ref->u.ar.type)
5291 case AR_FULL:
5292 /* Coarray scalar. */
5293 if (ref->u.ar.as->rank == 0)
5295 current_part_dimension = 0;
5296 break;
5298 /* Fall through. */
5299 case AR_SECTION:
5300 current_part_dimension = 1;
5301 break;
5303 case AR_ELEMENT:
5304 array_ref = NULL;
5305 current_part_dimension = 0;
5306 break;
5308 case AR_UNKNOWN:
5309 gfc_internal_error ("resolve_ref(): Bad array reference");
5312 break;
5314 case REF_COMPONENT:
5315 if (current_part_dimension || seen_part_dimension)
5317 /* F03:C614. */
5318 if (ref->u.c.component->attr.pointer
5319 || ref->u.c.component->attr.proc_pointer
5320 || (ref->u.c.component->ts.type == BT_CLASS
5321 && CLASS_DATA (ref->u.c.component)->attr.pointer))
5323 gfc_error ("Component to the right of a part reference "
5324 "with nonzero rank must not have the POINTER "
5325 "attribute at %L", &expr->where);
5326 return false;
5328 else if (ref->u.c.component->attr.allocatable
5329 || (ref->u.c.component->ts.type == BT_CLASS
5330 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
5333 gfc_error ("Component to the right of a part reference "
5334 "with nonzero rank must not have the ALLOCATABLE "
5335 "attribute at %L", &expr->where);
5336 return false;
5340 n_components++;
5341 break;
5343 case REF_SUBSTRING:
5344 break;
5346 case REF_INQUIRY:
5347 /* Implement requirement in note 9.7 of F2018 that the result of the
5348 LEN inquiry be a scalar. */
5349 if (ref->u.i == INQUIRY_LEN && array_ref && expr->ts.deferred)
5351 array_ref->u.ar.type = AR_ELEMENT;
5352 expr->rank = 0;
5353 /* INQUIRY_LEN is not evaluated from the rest of the expr
5354 but directly from the string length. This means that setting
5355 the array indices to one does not matter but might trigger
5356 a runtime bounds error. Suppress the check. */
5357 expr->no_bounds_check = 1;
5358 for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
5360 array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
5361 if (array_ref->u.ar.start[dim])
5362 gfc_free_expr (array_ref->u.ar.start[dim]);
5363 array_ref->u.ar.start[dim]
5364 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
5365 if (array_ref->u.ar.end[dim])
5366 gfc_free_expr (array_ref->u.ar.end[dim]);
5367 if (array_ref->u.ar.stride[dim])
5368 gfc_free_expr (array_ref->u.ar.stride[dim]);
5371 break;
5374 if (((ref->type == REF_COMPONENT && n_components > 1)
5375 || ref->next == NULL)
5376 && current_part_dimension
5377 && seen_part_dimension)
5379 gfc_error ("Two or more part references with nonzero rank must "
5380 "not be specified at %L", &expr->where);
5381 return false;
5384 if (ref->type == REF_COMPONENT)
5386 if (current_part_dimension)
5387 seen_part_dimension = 1;
5389 /* reset to make sure */
5390 current_part_dimension = 0;
5394 return true;
5398 /* Given an expression, determine its shape. This is easier than it sounds.
5399 Leaves the shape array NULL if it is not possible to determine the shape. */
5401 static void
5402 expression_shape (gfc_expr *e)
5404 mpz_t array[GFC_MAX_DIMENSIONS];
5405 int i;
5407 if (e->rank <= 0 || e->shape != NULL)
5408 return;
5410 for (i = 0; i < e->rank; i++)
5411 if (!gfc_array_dimen_size (e, i, &array[i]))
5412 goto fail;
5414 e->shape = gfc_get_shape (e->rank);
5416 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
5418 return;
5420 fail:
5421 for (i--; i >= 0; i--)
5422 mpz_clear (array[i]);
5426 /* Given a variable expression node, compute the rank of the expression by
5427 examining the base symbol and any reference structures it may have. */
5429 void
5430 gfc_expression_rank (gfc_expr *e)
5432 gfc_ref *ref;
5433 int i, rank;
5435 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
5436 could lead to serious confusion... */
5437 gcc_assert (e->expr_type != EXPR_COMPCALL);
5439 if (e->ref == NULL)
5441 if (e->expr_type == EXPR_ARRAY)
5442 goto done;
5443 /* Constructors can have a rank different from one via RESHAPE(). */
5445 e->rank = ((e->symtree == NULL || e->symtree->n.sym->as == NULL)
5446 ? 0 : e->symtree->n.sym->as->rank);
5447 goto done;
5450 rank = 0;
5452 for (ref = e->ref; ref; ref = ref->next)
5454 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
5455 && ref->u.c.component->attr.function && !ref->next)
5456 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
5458 if (ref->type != REF_ARRAY)
5459 continue;
5461 if (ref->u.ar.type == AR_FULL)
5463 rank = ref->u.ar.as->rank;
5464 break;
5467 if (ref->u.ar.type == AR_SECTION)
5469 /* Figure out the rank of the section. */
5470 if (rank != 0)
5471 gfc_internal_error ("gfc_expression_rank(): Two array specs");
5473 for (i = 0; i < ref->u.ar.dimen; i++)
5474 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5475 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
5476 rank++;
5478 break;
5482 e->rank = rank;
5484 done:
5485 expression_shape (e);
5489 static void
5490 add_caf_get_intrinsic (gfc_expr *e)
5492 gfc_expr *wrapper, *tmp_expr;
5493 gfc_ref *ref;
5494 int n;
5496 for (ref = e->ref; ref; ref = ref->next)
5497 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5498 break;
5499 if (ref == NULL)
5500 return;
5502 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
5503 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
5504 return;
5506 tmp_expr = XCNEW (gfc_expr);
5507 *tmp_expr = *e;
5508 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
5509 "caf_get", tmp_expr->where, 1, tmp_expr);
5510 wrapper->ts = e->ts;
5511 wrapper->rank = e->rank;
5512 if (e->rank)
5513 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
5514 *e = *wrapper;
5515 free (wrapper);
5519 static void
5520 remove_caf_get_intrinsic (gfc_expr *e)
5522 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
5523 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
5524 gfc_expr *e2 = e->value.function.actual->expr;
5525 e->value.function.actual->expr = NULL;
5526 gfc_free_actual_arglist (e->value.function.actual);
5527 gfc_free_shape (&e->shape, e->rank);
5528 *e = *e2;
5529 free (e2);
5533 /* Resolve a variable expression. */
5535 static bool
5536 resolve_variable (gfc_expr *e)
5538 gfc_symbol *sym;
5539 bool t;
5541 t = true;
5543 if (e->symtree == NULL)
5544 return false;
5545 sym = e->symtree->n.sym;
5547 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
5548 as ts.type is set to BT_ASSUMED in resolve_symbol. */
5549 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
5551 if (!actual_arg || inquiry_argument)
5553 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
5554 "be used as actual argument", sym->name, &e->where);
5555 return false;
5558 /* TS 29113, 407b. */
5559 else if (e->ts.type == BT_ASSUMED)
5561 if (!actual_arg)
5563 gfc_error ("Assumed-type variable %s at %L may only be used "
5564 "as actual argument", sym->name, &e->where);
5565 return false;
5567 else if (inquiry_argument && !first_actual_arg)
5569 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5570 for all inquiry functions in resolve_function; the reason is
5571 that the function-name resolution happens too late in that
5572 function. */
5573 gfc_error ("Assumed-type variable %s at %L as actual argument to "
5574 "an inquiry function shall be the first argument",
5575 sym->name, &e->where);
5576 return false;
5579 /* TS 29113, C535b. */
5580 else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5581 && sym->ts.u.derived && CLASS_DATA (sym)
5582 && CLASS_DATA (sym)->as
5583 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5584 || (sym->ts.type != BT_CLASS && sym->as
5585 && sym->as->type == AS_ASSUMED_RANK))
5586 && !sym->attr.select_rank_temporary)
5588 if (!actual_arg
5589 && !(cs_base && cs_base->current
5590 && cs_base->current->op == EXEC_SELECT_RANK))
5592 gfc_error ("Assumed-rank variable %s at %L may only be used as "
5593 "actual argument", sym->name, &e->where);
5594 return false;
5596 else if (inquiry_argument && !first_actual_arg)
5598 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5599 for all inquiry functions in resolve_function; the reason is
5600 that the function-name resolution happens too late in that
5601 function. */
5602 gfc_error ("Assumed-rank variable %s at %L as actual argument "
5603 "to an inquiry function shall be the first argument",
5604 sym->name, &e->where);
5605 return false;
5609 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
5610 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5611 && e->ref->next == NULL))
5613 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
5614 "a subobject reference", sym->name, &e->ref->u.ar.where);
5615 return false;
5617 /* TS 29113, 407b. */
5618 else if (e->ts.type == BT_ASSUMED && e->ref
5619 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5620 && e->ref->next == NULL))
5622 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
5623 "reference", sym->name, &e->ref->u.ar.where);
5624 return false;
5627 /* TS 29113, C535b. */
5628 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5629 && sym->ts.u.derived && CLASS_DATA (sym)
5630 && CLASS_DATA (sym)->as
5631 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5632 || (sym->ts.type != BT_CLASS && sym->as
5633 && sym->as->type == AS_ASSUMED_RANK))
5634 && e->ref
5635 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5636 && e->ref->next == NULL))
5638 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
5639 "reference", sym->name, &e->ref->u.ar.where);
5640 return false;
5643 /* For variables that are used in an associate (target => object) where
5644 the object's basetype is array valued while the target is scalar,
5645 the ts' type of the component refs is still array valued, which
5646 can't be translated that way. */
5647 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
5648 && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
5649 && CLASS_DATA (sym->assoc->target)->as)
5651 gfc_ref *ref = e->ref;
5652 while (ref)
5654 switch (ref->type)
5656 case REF_COMPONENT:
5657 ref->u.c.sym = sym->ts.u.derived;
5658 /* Stop the loop. */
5659 ref = NULL;
5660 break;
5661 default:
5662 ref = ref->next;
5663 break;
5668 /* If this is an associate-name, it may be parsed with an array reference
5669 in error even though the target is scalar. Fail directly in this case.
5670 TODO Understand why class scalar expressions must be excluded. */
5671 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5673 if (sym->ts.type == BT_CLASS)
5674 gfc_fix_class_refs (e);
5675 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5676 return false;
5677 else if (sym->attr.dimension && (!e->ref || e->ref->type != REF_ARRAY))
5679 /* This can happen because the parser did not detect that the
5680 associate name is an array and the expression had no array
5681 part_ref. */
5682 gfc_ref *ref = gfc_get_ref ();
5683 ref->type = REF_ARRAY;
5684 ref->u.ar = *gfc_get_array_ref();
5685 ref->u.ar.type = AR_FULL;
5686 if (sym->as)
5688 ref->u.ar.as = sym->as;
5689 ref->u.ar.dimen = sym->as->rank;
5691 ref->next = e->ref;
5692 e->ref = ref;
5697 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5698 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5700 /* On the other hand, the parser may not have known this is an array;
5701 in this case, we have to add a FULL reference. */
5702 if (sym->assoc && sym->attr.dimension && !e->ref)
5704 e->ref = gfc_get_ref ();
5705 e->ref->type = REF_ARRAY;
5706 e->ref->u.ar.type = AR_FULL;
5707 e->ref->u.ar.dimen = 0;
5710 /* Like above, but for class types, where the checking whether an array
5711 ref is present is more complicated. Furthermore make sure not to add
5712 the full array ref to _vptr or _len refs. */
5713 if (sym->assoc && sym->ts.type == BT_CLASS
5714 && CLASS_DATA (sym)->attr.dimension
5715 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
5717 gfc_ref *ref, *newref;
5719 newref = gfc_get_ref ();
5720 newref->type = REF_ARRAY;
5721 newref->u.ar.type = AR_FULL;
5722 newref->u.ar.dimen = 0;
5723 /* Because this is an associate var and the first ref either is a ref to
5724 the _data component or not, no traversal of the ref chain is
5725 needed. The array ref needs to be inserted after the _data ref,
5726 or when that is not present, which may happend for polymorphic
5727 types, then at the first position. */
5728 ref = e->ref;
5729 if (!ref)
5730 e->ref = newref;
5731 else if (ref->type == REF_COMPONENT
5732 && strcmp ("_data", ref->u.c.component->name) == 0)
5734 if (!ref->next || ref->next->type != REF_ARRAY)
5736 newref->next = ref->next;
5737 ref->next = newref;
5739 else
5740 /* Array ref present already. */
5741 gfc_free_ref_list (newref);
5743 else if (ref->type == REF_ARRAY)
5744 /* Array ref present already. */
5745 gfc_free_ref_list (newref);
5746 else
5748 newref->next = ref;
5749 e->ref = newref;
5753 if (e->ref && !gfc_resolve_ref (e))
5754 return false;
5756 if (sym->attr.flavor == FL_PROCEDURE
5757 && (!sym->attr.function
5758 || (sym->attr.function && sym->result
5759 && sym->result->attr.proc_pointer
5760 && !sym->result->attr.function)))
5762 e->ts.type = BT_PROCEDURE;
5763 goto resolve_procedure;
5766 if (sym->ts.type != BT_UNKNOWN)
5767 gfc_variable_attr (e, &e->ts);
5768 else if (sym->attr.flavor == FL_PROCEDURE
5769 && sym->attr.function && sym->result
5770 && sym->result->ts.type != BT_UNKNOWN
5771 && sym->result->attr.proc_pointer)
5772 e->ts = sym->result->ts;
5773 else
5775 /* Must be a simple variable reference. */
5776 if (!gfc_set_default_type (sym, 1, sym->ns))
5777 return false;
5778 e->ts = sym->ts;
5781 if (check_assumed_size_reference (sym, e))
5782 return false;
5784 /* Deal with forward references to entries during gfc_resolve_code, to
5785 satisfy, at least partially, 12.5.2.5. */
5786 if (gfc_current_ns->entries
5787 && current_entry_id == sym->entry_id
5788 && cs_base
5789 && cs_base->current
5790 && cs_base->current->op != EXEC_ENTRY)
5792 gfc_entry_list *entry;
5793 gfc_formal_arglist *formal;
5794 int n;
5795 bool seen, saved_specification_expr;
5797 /* If the symbol is a dummy... */
5798 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5800 entry = gfc_current_ns->entries;
5801 seen = false;
5803 /* ...test if the symbol is a parameter of previous entries. */
5804 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5805 for (formal = entry->sym->formal; formal; formal = formal->next)
5807 if (formal->sym && sym->name == formal->sym->name)
5809 seen = true;
5810 break;
5814 /* If it has not been seen as a dummy, this is an error. */
5815 if (!seen)
5817 if (specification_expr)
5818 gfc_error ("Variable %qs, used in a specification expression"
5819 ", is referenced at %L before the ENTRY statement "
5820 "in which it is a parameter",
5821 sym->name, &cs_base->current->loc);
5822 else
5823 gfc_error ("Variable %qs is used at %L before the ENTRY "
5824 "statement in which it is a parameter",
5825 sym->name, &cs_base->current->loc);
5826 t = false;
5830 /* Now do the same check on the specification expressions. */
5831 saved_specification_expr = specification_expr;
5832 specification_expr = true;
5833 if (sym->ts.type == BT_CHARACTER
5834 && !gfc_resolve_expr (sym->ts.u.cl->length))
5835 t = false;
5837 if (sym->as)
5838 for (n = 0; n < sym->as->rank; n++)
5840 if (!gfc_resolve_expr (sym->as->lower[n]))
5841 t = false;
5842 if (!gfc_resolve_expr (sym->as->upper[n]))
5843 t = false;
5845 specification_expr = saved_specification_expr;
5847 if (t)
5848 /* Update the symbol's entry level. */
5849 sym->entry_id = current_entry_id + 1;
5852 /* If a symbol has been host_associated mark it. This is used latter,
5853 to identify if aliasing is possible via host association. */
5854 if (sym->attr.flavor == FL_VARIABLE
5855 && gfc_current_ns->parent
5856 && (gfc_current_ns->parent == sym->ns
5857 || (gfc_current_ns->parent->parent
5858 && gfc_current_ns->parent->parent == sym->ns)))
5859 sym->attr.host_assoc = 1;
5861 if (gfc_current_ns->proc_name
5862 && sym->attr.dimension
5863 && (sym->ns != gfc_current_ns
5864 || sym->attr.use_assoc
5865 || sym->attr.in_common))
5866 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
5868 resolve_procedure:
5869 if (t && !resolve_procedure_expression (e))
5870 t = false;
5872 /* F2008, C617 and C1229. */
5873 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5874 && gfc_is_coindexed (e))
5876 gfc_ref *ref, *ref2 = NULL;
5878 for (ref = e->ref; ref; ref = ref->next)
5880 if (ref->type == REF_COMPONENT)
5881 ref2 = ref;
5882 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5883 break;
5886 for ( ; ref; ref = ref->next)
5887 if (ref->type == REF_COMPONENT)
5888 break;
5890 /* Expression itself is not coindexed object. */
5891 if (ref && e->ts.type == BT_CLASS)
5893 gfc_error ("Polymorphic subobject of coindexed object at %L",
5894 &e->where);
5895 t = false;
5898 /* Expression itself is coindexed object. */
5899 if (ref == NULL)
5901 gfc_component *c;
5902 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5903 for ( ; c; c = c->next)
5904 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5906 gfc_error ("Coindexed object with polymorphic allocatable "
5907 "subcomponent at %L", &e->where);
5908 t = false;
5909 break;
5914 if (t)
5915 gfc_expression_rank (e);
5917 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
5918 add_caf_get_intrinsic (e);
5920 /* Simplify cases where access to a parameter array results in a
5921 single constant. Suppress errors since those will have been
5922 issued before, as warnings. */
5923 if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
5925 gfc_push_suppress_errors ();
5926 gfc_simplify_expr (e, 1);
5927 gfc_pop_suppress_errors ();
5930 return t;
5934 /* Checks to see that the correct symbol has been host associated.
5935 The only situation where this arises is that in which a twice
5936 contained function is parsed after the host association is made.
5937 Therefore, on detecting this, change the symbol in the expression
5938 and convert the array reference into an actual arglist if the old
5939 symbol is a variable. */
5940 static bool
5941 check_host_association (gfc_expr *e)
5943 gfc_symbol *sym, *old_sym;
5944 gfc_symtree *st;
5945 int n;
5946 gfc_ref *ref;
5947 gfc_actual_arglist *arg, *tail = NULL;
5948 bool retval = e->expr_type == EXPR_FUNCTION;
5950 /* If the expression is the result of substitution in
5951 interface.c(gfc_extend_expr) because there is no way in
5952 which the host association can be wrong. */
5953 if (e->symtree == NULL
5954 || e->symtree->n.sym == NULL
5955 || e->user_operator)
5956 return retval;
5958 old_sym = e->symtree->n.sym;
5960 if (gfc_current_ns->parent
5961 && old_sym->ns != gfc_current_ns)
5963 /* Use the 'USE' name so that renamed module symbols are
5964 correctly handled. */
5965 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5967 if (sym && old_sym != sym
5968 && sym->ts.type == old_sym->ts.type
5969 && sym->attr.flavor == FL_PROCEDURE
5970 && sym->attr.contained)
5972 /* Clear the shape, since it might not be valid. */
5973 gfc_free_shape (&e->shape, e->rank);
5975 /* Give the expression the right symtree! */
5976 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5977 gcc_assert (st != NULL);
5979 if (old_sym->attr.flavor == FL_PROCEDURE
5980 || e->expr_type == EXPR_FUNCTION)
5982 /* Original was function so point to the new symbol, since
5983 the actual argument list is already attached to the
5984 expression. */
5985 e->value.function.esym = NULL;
5986 e->symtree = st;
5988 else
5990 /* Original was variable so convert array references into
5991 an actual arglist. This does not need any checking now
5992 since resolve_function will take care of it. */
5993 e->value.function.actual = NULL;
5994 e->expr_type = EXPR_FUNCTION;
5995 e->symtree = st;
5997 /* Ambiguity will not arise if the array reference is not
5998 the last reference. */
5999 for (ref = e->ref; ref; ref = ref->next)
6000 if (ref->type == REF_ARRAY && ref->next == NULL)
6001 break;
6003 if ((ref == NULL || ref->type != REF_ARRAY)
6004 && sym->attr.proc == PROC_INTERNAL)
6006 gfc_error ("%qs at %L is host associated at %L into "
6007 "a contained procedure with an internal "
6008 "procedure of the same name", sym->name,
6009 &old_sym->declared_at, &e->where);
6010 return false;
6013 gcc_assert (ref->type == REF_ARRAY);
6015 /* Grab the start expressions from the array ref and
6016 copy them into actual arguments. */
6017 for (n = 0; n < ref->u.ar.dimen; n++)
6019 arg = gfc_get_actual_arglist ();
6020 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
6021 if (e->value.function.actual == NULL)
6022 tail = e->value.function.actual = arg;
6023 else
6025 tail->next = arg;
6026 tail = arg;
6030 /* Dump the reference list and set the rank. */
6031 gfc_free_ref_list (e->ref);
6032 e->ref = NULL;
6033 e->rank = sym->as ? sym->as->rank : 0;
6036 gfc_resolve_expr (e);
6037 sym->refs++;
6040 /* This might have changed! */
6041 return e->expr_type == EXPR_FUNCTION;
6045 static void
6046 gfc_resolve_character_operator (gfc_expr *e)
6048 gfc_expr *op1 = e->value.op.op1;
6049 gfc_expr *op2 = e->value.op.op2;
6050 gfc_expr *e1 = NULL;
6051 gfc_expr *e2 = NULL;
6053 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
6055 if (op1->ts.u.cl && op1->ts.u.cl->length)
6056 e1 = gfc_copy_expr (op1->ts.u.cl->length);
6057 else if (op1->expr_type == EXPR_CONSTANT)
6058 e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6059 op1->value.character.length);
6061 if (op2->ts.u.cl && op2->ts.u.cl->length)
6062 e2 = gfc_copy_expr (op2->ts.u.cl->length);
6063 else if (op2->expr_type == EXPR_CONSTANT)
6064 e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6065 op2->value.character.length);
6067 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6069 if (!e1 || !e2)
6071 gfc_free_expr (e1);
6072 gfc_free_expr (e2);
6074 return;
6077 e->ts.u.cl->length = gfc_add (e1, e2);
6078 e->ts.u.cl->length->ts.type = BT_INTEGER;
6079 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
6080 gfc_simplify_expr (e->ts.u.cl->length, 0);
6081 gfc_resolve_expr (e->ts.u.cl->length);
6083 return;
6087 /* Ensure that an character expression has a charlen and, if possible, a
6088 length expression. */
6090 static void
6091 fixup_charlen (gfc_expr *e)
6093 /* The cases fall through so that changes in expression type and the need
6094 for multiple fixes are picked up. In all circumstances, a charlen should
6095 be available for the middle end to hang a backend_decl on. */
6096 switch (e->expr_type)
6098 case EXPR_OP:
6099 gfc_resolve_character_operator (e);
6100 /* FALLTHRU */
6102 case EXPR_ARRAY:
6103 if (e->expr_type == EXPR_ARRAY)
6104 gfc_resolve_character_array_constructor (e);
6105 /* FALLTHRU */
6107 case EXPR_SUBSTRING:
6108 if (!e->ts.u.cl && e->ref)
6109 gfc_resolve_substring_charlen (e);
6110 /* FALLTHRU */
6112 default:
6113 if (!e->ts.u.cl)
6114 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6116 break;
6121 /* Update an actual argument to include the passed-object for type-bound
6122 procedures at the right position. */
6124 static gfc_actual_arglist*
6125 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
6126 const char *name)
6128 gcc_assert (argpos > 0);
6130 if (argpos == 1)
6132 gfc_actual_arglist* result;
6134 result = gfc_get_actual_arglist ();
6135 result->expr = po;
6136 result->next = lst;
6137 if (name)
6138 result->name = name;
6140 return result;
6143 if (lst)
6144 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
6145 else
6146 lst = update_arglist_pass (NULL, po, argpos - 1, name);
6147 return lst;
6151 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
6153 static gfc_expr*
6154 extract_compcall_passed_object (gfc_expr* e)
6156 gfc_expr* po;
6158 if (e->expr_type == EXPR_UNKNOWN)
6160 gfc_error ("Error in typebound call at %L",
6161 &e->where);
6162 return NULL;
6165 gcc_assert (e->expr_type == EXPR_COMPCALL);
6167 if (e->value.compcall.base_object)
6168 po = gfc_copy_expr (e->value.compcall.base_object);
6169 else
6171 po = gfc_get_expr ();
6172 po->expr_type = EXPR_VARIABLE;
6173 po->symtree = e->symtree;
6174 po->ref = gfc_copy_ref (e->ref);
6175 po->where = e->where;
6178 if (!gfc_resolve_expr (po))
6179 return NULL;
6181 return po;
6185 /* Update the arglist of an EXPR_COMPCALL expression to include the
6186 passed-object. */
6188 static bool
6189 update_compcall_arglist (gfc_expr* e)
6191 gfc_expr* po;
6192 gfc_typebound_proc* tbp;
6194 tbp = e->value.compcall.tbp;
6196 if (tbp->error)
6197 return false;
6199 po = extract_compcall_passed_object (e);
6200 if (!po)
6201 return false;
6203 if (tbp->nopass || e->value.compcall.ignore_pass)
6205 gfc_free_expr (po);
6206 return true;
6209 if (tbp->pass_arg_num <= 0)
6210 return false;
6212 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6213 tbp->pass_arg_num,
6214 tbp->pass_arg);
6216 return true;
6220 /* Extract the passed object from a PPC call (a copy of it). */
6222 static gfc_expr*
6223 extract_ppc_passed_object (gfc_expr *e)
6225 gfc_expr *po;
6226 gfc_ref **ref;
6228 po = gfc_get_expr ();
6229 po->expr_type = EXPR_VARIABLE;
6230 po->symtree = e->symtree;
6231 po->ref = gfc_copy_ref (e->ref);
6232 po->where = e->where;
6234 /* Remove PPC reference. */
6235 ref = &po->ref;
6236 while ((*ref)->next)
6237 ref = &(*ref)->next;
6238 gfc_free_ref_list (*ref);
6239 *ref = NULL;
6241 if (!gfc_resolve_expr (po))
6242 return NULL;
6244 return po;
6248 /* Update the actual arglist of a procedure pointer component to include the
6249 passed-object. */
6251 static bool
6252 update_ppc_arglist (gfc_expr* e)
6254 gfc_expr* po;
6255 gfc_component *ppc;
6256 gfc_typebound_proc* tb;
6258 ppc = gfc_get_proc_ptr_comp (e);
6259 if (!ppc)
6260 return false;
6262 tb = ppc->tb;
6264 if (tb->error)
6265 return false;
6266 else if (tb->nopass)
6267 return true;
6269 po = extract_ppc_passed_object (e);
6270 if (!po)
6271 return false;
6273 /* F08:R739. */
6274 if (po->rank != 0)
6276 gfc_error ("Passed-object at %L must be scalar", &e->where);
6277 return false;
6280 /* F08:C611. */
6281 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
6283 gfc_error ("Base object for procedure-pointer component call at %L is of"
6284 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
6285 return false;
6288 gcc_assert (tb->pass_arg_num > 0);
6289 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6290 tb->pass_arg_num,
6291 tb->pass_arg);
6293 return true;
6297 /* Check that the object a TBP is called on is valid, i.e. it must not be
6298 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
6300 static bool
6301 check_typebound_baseobject (gfc_expr* e)
6303 gfc_expr* base;
6304 bool return_value = false;
6306 base = extract_compcall_passed_object (e);
6307 if (!base)
6308 return false;
6310 if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
6312 gfc_error ("Error in typebound call at %L", &e->where);
6313 goto cleanup;
6316 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
6317 return false;
6319 /* F08:C611. */
6320 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
6322 gfc_error ("Base object for type-bound procedure call at %L is of"
6323 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
6324 goto cleanup;
6327 /* F08:C1230. If the procedure called is NOPASS,
6328 the base object must be scalar. */
6329 if (e->value.compcall.tbp->nopass && base->rank != 0)
6331 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
6332 " be scalar", &e->where);
6333 goto cleanup;
6336 return_value = true;
6338 cleanup:
6339 gfc_free_expr (base);
6340 return return_value;
6344 /* Resolve a call to a type-bound procedure, either function or subroutine,
6345 statically from the data in an EXPR_COMPCALL expression. The adapted
6346 arglist and the target-procedure symtree are returned. */
6348 static bool
6349 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
6350 gfc_actual_arglist** actual)
6352 gcc_assert (e->expr_type == EXPR_COMPCALL);
6353 gcc_assert (!e->value.compcall.tbp->is_generic);
6355 /* Update the actual arglist for PASS. */
6356 if (!update_compcall_arglist (e))
6357 return false;
6359 *actual = e->value.compcall.actual;
6360 *target = e->value.compcall.tbp->u.specific;
6362 gfc_free_ref_list (e->ref);
6363 e->ref = NULL;
6364 e->value.compcall.actual = NULL;
6366 /* If we find a deferred typebound procedure, check for derived types
6367 that an overriding typebound procedure has not been missed. */
6368 if (e->value.compcall.name
6369 && !e->value.compcall.tbp->non_overridable
6370 && e->value.compcall.base_object
6371 && e->value.compcall.base_object->ts.type == BT_DERIVED)
6373 gfc_symtree *st;
6374 gfc_symbol *derived;
6376 /* Use the derived type of the base_object. */
6377 derived = e->value.compcall.base_object->ts.u.derived;
6378 st = NULL;
6380 /* If necessary, go through the inheritance chain. */
6381 while (!st && derived)
6383 /* Look for the typebound procedure 'name'. */
6384 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
6385 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
6386 e->value.compcall.name);
6387 if (!st)
6388 derived = gfc_get_derived_super_type (derived);
6391 /* Now find the specific name in the derived type namespace. */
6392 if (st && st->n.tb && st->n.tb->u.specific)
6393 gfc_find_sym_tree (st->n.tb->u.specific->name,
6394 derived->ns, 1, &st);
6395 if (st)
6396 *target = st;
6398 return true;
6402 /* Get the ultimate declared type from an expression. In addition,
6403 return the last class/derived type reference and the copy of the
6404 reference list. If check_types is set true, derived types are
6405 identified as well as class references. */
6406 static gfc_symbol*
6407 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
6408 gfc_expr *e, bool check_types)
6410 gfc_symbol *declared;
6411 gfc_ref *ref;
6413 declared = NULL;
6414 if (class_ref)
6415 *class_ref = NULL;
6416 if (new_ref)
6417 *new_ref = gfc_copy_ref (e->ref);
6419 for (ref = e->ref; ref; ref = ref->next)
6421 if (ref->type != REF_COMPONENT)
6422 continue;
6424 if ((ref->u.c.component->ts.type == BT_CLASS
6425 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
6426 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
6428 declared = ref->u.c.component->ts.u.derived;
6429 if (class_ref)
6430 *class_ref = ref;
6434 if (declared == NULL)
6435 declared = e->symtree->n.sym->ts.u.derived;
6437 return declared;
6441 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
6442 which of the specific bindings (if any) matches the arglist and transform
6443 the expression into a call of that binding. */
6445 static bool
6446 resolve_typebound_generic_call (gfc_expr* e, const char **name)
6448 gfc_typebound_proc* genproc;
6449 const char* genname;
6450 gfc_symtree *st;
6451 gfc_symbol *derived;
6453 gcc_assert (e->expr_type == EXPR_COMPCALL);
6454 genname = e->value.compcall.name;
6455 genproc = e->value.compcall.tbp;
6457 if (!genproc->is_generic)
6458 return true;
6460 /* Try the bindings on this type and in the inheritance hierarchy. */
6461 for (; genproc; genproc = genproc->overridden)
6463 gfc_tbp_generic* g;
6465 gcc_assert (genproc->is_generic);
6466 for (g = genproc->u.generic; g; g = g->next)
6468 gfc_symbol* target;
6469 gfc_actual_arglist* args;
6470 bool matches;
6472 gcc_assert (g->specific);
6474 if (g->specific->error)
6475 continue;
6477 target = g->specific->u.specific->n.sym;
6479 /* Get the right arglist by handling PASS/NOPASS. */
6480 args = gfc_copy_actual_arglist (e->value.compcall.actual);
6481 if (!g->specific->nopass)
6483 gfc_expr* po;
6484 po = extract_compcall_passed_object (e);
6485 if (!po)
6487 gfc_free_actual_arglist (args);
6488 return false;
6491 gcc_assert (g->specific->pass_arg_num > 0);
6492 gcc_assert (!g->specific->error);
6493 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
6494 g->specific->pass_arg);
6496 resolve_actual_arglist (args, target->attr.proc,
6497 is_external_proc (target)
6498 && gfc_sym_get_dummy_args (target) == NULL);
6500 /* Check if this arglist matches the formal. */
6501 matches = gfc_arglist_matches_symbol (&args, target);
6503 /* Clean up and break out of the loop if we've found it. */
6504 gfc_free_actual_arglist (args);
6505 if (matches)
6507 e->value.compcall.tbp = g->specific;
6508 genname = g->specific_st->name;
6509 /* Pass along the name for CLASS methods, where the vtab
6510 procedure pointer component has to be referenced. */
6511 if (name)
6512 *name = genname;
6513 goto success;
6518 /* Nothing matching found! */
6519 gfc_error ("Found no matching specific binding for the call to the GENERIC"
6520 " %qs at %L", genname, &e->where);
6521 return false;
6523 success:
6524 /* Make sure that we have the right specific instance for the name. */
6525 derived = get_declared_from_expr (NULL, NULL, e, true);
6527 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
6528 if (st)
6529 e->value.compcall.tbp = st->n.tb;
6531 return true;
6535 /* Resolve a call to a type-bound subroutine. */
6537 static bool
6538 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
6540 gfc_actual_arglist* newactual;
6541 gfc_symtree* target;
6543 /* Check that's really a SUBROUTINE. */
6544 if (!c->expr1->value.compcall.tbp->subroutine)
6546 if (!c->expr1->value.compcall.tbp->is_generic
6547 && c->expr1->value.compcall.tbp->u.specific
6548 && c->expr1->value.compcall.tbp->u.specific->n.sym
6549 && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
6550 c->expr1->value.compcall.tbp->subroutine = 1;
6551 else
6553 gfc_error ("%qs at %L should be a SUBROUTINE",
6554 c->expr1->value.compcall.name, &c->loc);
6555 return false;
6559 if (!check_typebound_baseobject (c->expr1))
6560 return false;
6562 /* Pass along the name for CLASS methods, where the vtab
6563 procedure pointer component has to be referenced. */
6564 if (name)
6565 *name = c->expr1->value.compcall.name;
6567 if (!resolve_typebound_generic_call (c->expr1, name))
6568 return false;
6570 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
6571 if (overridable)
6572 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
6574 /* Transform into an ordinary EXEC_CALL for now. */
6576 if (!resolve_typebound_static (c->expr1, &target, &newactual))
6577 return false;
6579 c->ext.actual = newactual;
6580 c->symtree = target;
6581 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
6583 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
6585 gfc_free_expr (c->expr1);
6586 c->expr1 = gfc_get_expr ();
6587 c->expr1->expr_type = EXPR_FUNCTION;
6588 c->expr1->symtree = target;
6589 c->expr1->where = c->loc;
6591 return resolve_call (c);
6595 /* Resolve a component-call expression. */
6596 static bool
6597 resolve_compcall (gfc_expr* e, const char **name)
6599 gfc_actual_arglist* newactual;
6600 gfc_symtree* target;
6602 /* Check that's really a FUNCTION. */
6603 if (!e->value.compcall.tbp->function)
6605 gfc_error ("%qs at %L should be a FUNCTION",
6606 e->value.compcall.name, &e->where);
6607 return false;
6611 /* These must not be assign-calls! */
6612 gcc_assert (!e->value.compcall.assign);
6614 if (!check_typebound_baseobject (e))
6615 return false;
6617 /* Pass along the name for CLASS methods, where the vtab
6618 procedure pointer component has to be referenced. */
6619 if (name)
6620 *name = e->value.compcall.name;
6622 if (!resolve_typebound_generic_call (e, name))
6623 return false;
6624 gcc_assert (!e->value.compcall.tbp->is_generic);
6626 /* Take the rank from the function's symbol. */
6627 if (e->value.compcall.tbp->u.specific->n.sym->as)
6628 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
6630 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
6631 arglist to the TBP's binding target. */
6633 if (!resolve_typebound_static (e, &target, &newactual))
6634 return false;
6636 e->value.function.actual = newactual;
6637 e->value.function.name = NULL;
6638 e->value.function.esym = target->n.sym;
6639 e->value.function.isym = NULL;
6640 e->symtree = target;
6641 e->ts = target->n.sym->ts;
6642 e->expr_type = EXPR_FUNCTION;
6644 /* Resolution is not necessary if this is a class subroutine; this
6645 function only has to identify the specific proc. Resolution of
6646 the call will be done next in resolve_typebound_call. */
6647 return gfc_resolve_expr (e);
6651 static bool resolve_fl_derived (gfc_symbol *sym);
6654 /* Resolve a typebound function, or 'method'. First separate all
6655 the non-CLASS references by calling resolve_compcall directly. */
6657 static bool
6658 resolve_typebound_function (gfc_expr* e)
6660 gfc_symbol *declared;
6661 gfc_component *c;
6662 gfc_ref *new_ref;
6663 gfc_ref *class_ref;
6664 gfc_symtree *st;
6665 const char *name;
6666 gfc_typespec ts;
6667 gfc_expr *expr;
6668 bool overridable;
6670 st = e->symtree;
6672 /* Deal with typebound operators for CLASS objects. */
6673 expr = e->value.compcall.base_object;
6674 overridable = !e->value.compcall.tbp->non_overridable;
6675 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
6677 /* Since the typebound operators are generic, we have to ensure
6678 that any delays in resolution are corrected and that the vtab
6679 is present. */
6680 ts = expr->ts;
6681 declared = ts.u.derived;
6682 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6683 if (c->ts.u.derived == NULL)
6684 c->ts.u.derived = gfc_find_derived_vtab (declared);
6686 if (!resolve_compcall (e, &name))
6687 return false;
6689 /* Use the generic name if it is there. */
6690 name = name ? name : e->value.function.esym->name;
6691 e->symtree = expr->symtree;
6692 e->ref = gfc_copy_ref (expr->ref);
6693 get_declared_from_expr (&class_ref, NULL, e, false);
6695 /* Trim away the extraneous references that emerge from nested
6696 use of interface.c (extend_expr). */
6697 if (class_ref && class_ref->next)
6699 gfc_free_ref_list (class_ref->next);
6700 class_ref->next = NULL;
6702 else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
6704 gfc_free_ref_list (e->ref);
6705 e->ref = NULL;
6708 gfc_add_vptr_component (e);
6709 gfc_add_component_ref (e, name);
6710 e->value.function.esym = NULL;
6711 if (expr->expr_type != EXPR_VARIABLE)
6712 e->base_expr = expr;
6713 return true;
6716 if (st == NULL)
6717 return resolve_compcall (e, NULL);
6719 if (!gfc_resolve_ref (e))
6720 return false;
6722 /* Get the CLASS declared type. */
6723 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6725 if (!resolve_fl_derived (declared))
6726 return false;
6728 /* Weed out cases of the ultimate component being a derived type. */
6729 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6730 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6732 gfc_free_ref_list (new_ref);
6733 return resolve_compcall (e, NULL);
6736 c = gfc_find_component (declared, "_data", true, true, NULL);
6738 /* Treat the call as if it is a typebound procedure, in order to roll
6739 out the correct name for the specific function. */
6740 if (!resolve_compcall (e, &name))
6742 gfc_free_ref_list (new_ref);
6743 return false;
6745 ts = e->ts;
6747 if (overridable)
6749 /* Convert the expression to a procedure pointer component call. */
6750 e->value.function.esym = NULL;
6751 e->symtree = st;
6753 if (new_ref)
6754 e->ref = new_ref;
6756 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6757 gfc_add_vptr_component (e);
6758 gfc_add_component_ref (e, name);
6760 /* Recover the typespec for the expression. This is really only
6761 necessary for generic procedures, where the additional call
6762 to gfc_add_component_ref seems to throw the collection of the
6763 correct typespec. */
6764 e->ts = ts;
6766 else if (new_ref)
6767 gfc_free_ref_list (new_ref);
6769 return true;
6772 /* Resolve a typebound subroutine, or 'method'. First separate all
6773 the non-CLASS references by calling resolve_typebound_call
6774 directly. */
6776 static bool
6777 resolve_typebound_subroutine (gfc_code *code)
6779 gfc_symbol *declared;
6780 gfc_component *c;
6781 gfc_ref *new_ref;
6782 gfc_ref *class_ref;
6783 gfc_symtree *st;
6784 const char *name;
6785 gfc_typespec ts;
6786 gfc_expr *expr;
6787 bool overridable;
6789 st = code->expr1->symtree;
6791 /* Deal with typebound operators for CLASS objects. */
6792 expr = code->expr1->value.compcall.base_object;
6793 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6794 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6796 /* If the base_object is not a variable, the corresponding actual
6797 argument expression must be stored in e->base_expression so
6798 that the corresponding tree temporary can be used as the base
6799 object in gfc_conv_procedure_call. */
6800 if (expr->expr_type != EXPR_VARIABLE)
6802 gfc_actual_arglist *args;
6804 args= code->expr1->value.function.actual;
6805 for (; args; args = args->next)
6806 if (expr == args->expr)
6807 expr = args->expr;
6810 /* Since the typebound operators are generic, we have to ensure
6811 that any delays in resolution are corrected and that the vtab
6812 is present. */
6813 declared = expr->ts.u.derived;
6814 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6815 if (c->ts.u.derived == NULL)
6816 c->ts.u.derived = gfc_find_derived_vtab (declared);
6818 if (!resolve_typebound_call (code, &name, NULL))
6819 return false;
6821 /* Use the generic name if it is there. */
6822 name = name ? name : code->expr1->value.function.esym->name;
6823 code->expr1->symtree = expr->symtree;
6824 code->expr1->ref = gfc_copy_ref (expr->ref);
6826 /* Trim away the extraneous references that emerge from nested
6827 use of interface.c (extend_expr). */
6828 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6829 if (class_ref && class_ref->next)
6831 gfc_free_ref_list (class_ref->next);
6832 class_ref->next = NULL;
6834 else if (code->expr1->ref && !class_ref)
6836 gfc_free_ref_list (code->expr1->ref);
6837 code->expr1->ref = NULL;
6840 /* Now use the procedure in the vtable. */
6841 gfc_add_vptr_component (code->expr1);
6842 gfc_add_component_ref (code->expr1, name);
6843 code->expr1->value.function.esym = NULL;
6844 if (expr->expr_type != EXPR_VARIABLE)
6845 code->expr1->base_expr = expr;
6846 return true;
6849 if (st == NULL)
6850 return resolve_typebound_call (code, NULL, NULL);
6852 if (!gfc_resolve_ref (code->expr1))
6853 return false;
6855 /* Get the CLASS declared type. */
6856 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
6858 /* Weed out cases of the ultimate component being a derived type. */
6859 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6860 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6862 gfc_free_ref_list (new_ref);
6863 return resolve_typebound_call (code, NULL, NULL);
6866 if (!resolve_typebound_call (code, &name, &overridable))
6868 gfc_free_ref_list (new_ref);
6869 return false;
6871 ts = code->expr1->ts;
6873 if (overridable)
6875 /* Convert the expression to a procedure pointer component call. */
6876 code->expr1->value.function.esym = NULL;
6877 code->expr1->symtree = st;
6879 if (new_ref)
6880 code->expr1->ref = new_ref;
6882 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6883 gfc_add_vptr_component (code->expr1);
6884 gfc_add_component_ref (code->expr1, name);
6886 /* Recover the typespec for the expression. This is really only
6887 necessary for generic procedures, where the additional call
6888 to gfc_add_component_ref seems to throw the collection of the
6889 correct typespec. */
6890 code->expr1->ts = ts;
6892 else if (new_ref)
6893 gfc_free_ref_list (new_ref);
6895 return true;
6899 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6901 static bool
6902 resolve_ppc_call (gfc_code* c)
6904 gfc_component *comp;
6906 comp = gfc_get_proc_ptr_comp (c->expr1);
6907 gcc_assert (comp != NULL);
6909 c->resolved_sym = c->expr1->symtree->n.sym;
6910 c->expr1->expr_type = EXPR_VARIABLE;
6912 if (!comp->attr.subroutine)
6913 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6915 if (!gfc_resolve_ref (c->expr1))
6916 return false;
6918 if (!update_ppc_arglist (c->expr1))
6919 return false;
6921 c->ext.actual = c->expr1->value.compcall.actual;
6923 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6924 !(comp->ts.interface
6925 && comp->ts.interface->formal)))
6926 return false;
6928 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
6929 return false;
6931 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6933 return true;
6937 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6939 static bool
6940 resolve_expr_ppc (gfc_expr* e)
6942 gfc_component *comp;
6944 comp = gfc_get_proc_ptr_comp (e);
6945 gcc_assert (comp != NULL);
6947 /* Convert to EXPR_FUNCTION. */
6948 e->expr_type = EXPR_FUNCTION;
6949 e->value.function.isym = NULL;
6950 e->value.function.actual = e->value.compcall.actual;
6951 e->ts = comp->ts;
6952 if (comp->as != NULL)
6953 e->rank = comp->as->rank;
6955 if (!comp->attr.function)
6956 gfc_add_function (&comp->attr, comp->name, &e->where);
6958 if (!gfc_resolve_ref (e))
6959 return false;
6961 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6962 !(comp->ts.interface
6963 && comp->ts.interface->formal)))
6964 return false;
6966 if (!update_ppc_arglist (e))
6967 return false;
6969 if (!check_pure_function(e))
6970 return false;
6972 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6974 return true;
6978 static bool
6979 gfc_is_expandable_expr (gfc_expr *e)
6981 gfc_constructor *con;
6983 if (e->expr_type == EXPR_ARRAY)
6985 /* Traverse the constructor looking for variables that are flavor
6986 parameter. Parameters must be expanded since they are fully used at
6987 compile time. */
6988 con = gfc_constructor_first (e->value.constructor);
6989 for (; con; con = gfc_constructor_next (con))
6991 if (con->expr->expr_type == EXPR_VARIABLE
6992 && con->expr->symtree
6993 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6994 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6995 return true;
6996 if (con->expr->expr_type == EXPR_ARRAY
6997 && gfc_is_expandable_expr (con->expr))
6998 return true;
7002 return false;
7006 /* Sometimes variables in specification expressions of the result
7007 of module procedures in submodules wind up not being the 'real'
7008 dummy. Find this, if possible, in the namespace of the first
7009 formal argument. */
7011 static void
7012 fixup_unique_dummy (gfc_expr *e)
7014 gfc_symtree *st = NULL;
7015 gfc_symbol *s = NULL;
7017 if (e->symtree->n.sym->ns->proc_name
7018 && e->symtree->n.sym->ns->proc_name->formal)
7019 s = e->symtree->n.sym->ns->proc_name->formal->sym;
7021 if (s != NULL)
7022 st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
7024 if (st != NULL
7025 && st->n.sym != NULL
7026 && st->n.sym->attr.dummy)
7027 e->symtree = st;
7030 /* Resolve an expression. That is, make sure that types of operands agree
7031 with their operators, intrinsic operators are converted to function calls
7032 for overloaded types and unresolved function references are resolved. */
7034 bool
7035 gfc_resolve_expr (gfc_expr *e)
7037 bool t;
7038 bool inquiry_save, actual_arg_save, first_actual_arg_save;
7040 if (e == NULL || e->do_not_resolve_again)
7041 return true;
7043 /* inquiry_argument only applies to variables. */
7044 inquiry_save = inquiry_argument;
7045 actual_arg_save = actual_arg;
7046 first_actual_arg_save = first_actual_arg;
7048 if (e->expr_type != EXPR_VARIABLE)
7050 inquiry_argument = false;
7051 actual_arg = false;
7052 first_actual_arg = false;
7054 else if (e->symtree != NULL
7055 && *e->symtree->name == '@'
7056 && e->symtree->n.sym->attr.dummy)
7058 /* Deal with submodule specification expressions that are not
7059 found to be referenced in module.c(read_cleanup). */
7060 fixup_unique_dummy (e);
7063 switch (e->expr_type)
7065 case EXPR_OP:
7066 t = resolve_operator (e);
7067 break;
7069 case EXPR_FUNCTION:
7070 case EXPR_VARIABLE:
7072 if (check_host_association (e))
7073 t = resolve_function (e);
7074 else
7075 t = resolve_variable (e);
7077 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
7078 && e->ref->type != REF_SUBSTRING)
7079 gfc_resolve_substring_charlen (e);
7081 break;
7083 case EXPR_COMPCALL:
7084 t = resolve_typebound_function (e);
7085 break;
7087 case EXPR_SUBSTRING:
7088 t = gfc_resolve_ref (e);
7089 break;
7091 case EXPR_CONSTANT:
7092 case EXPR_NULL:
7093 t = true;
7094 break;
7096 case EXPR_PPC:
7097 t = resolve_expr_ppc (e);
7098 break;
7100 case EXPR_ARRAY:
7101 t = false;
7102 if (!gfc_resolve_ref (e))
7103 break;
7105 t = gfc_resolve_array_constructor (e);
7106 /* Also try to expand a constructor. */
7107 if (t)
7109 gfc_expression_rank (e);
7110 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
7111 gfc_expand_constructor (e, false);
7114 /* This provides the opportunity for the length of constructors with
7115 character valued function elements to propagate the string length
7116 to the expression. */
7117 if (t && e->ts.type == BT_CHARACTER)
7119 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
7120 here rather then add a duplicate test for it above. */
7121 gfc_expand_constructor (e, false);
7122 t = gfc_resolve_character_array_constructor (e);
7125 break;
7127 case EXPR_STRUCTURE:
7128 t = gfc_resolve_ref (e);
7129 if (!t)
7130 break;
7132 t = resolve_structure_cons (e, 0);
7133 if (!t)
7134 break;
7136 t = gfc_simplify_expr (e, 0);
7137 break;
7139 default:
7140 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
7143 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
7144 fixup_charlen (e);
7146 inquiry_argument = inquiry_save;
7147 actual_arg = actual_arg_save;
7148 first_actual_arg = first_actual_arg_save;
7150 /* For some reason, resolving these expressions a second time mangles
7151 the typespec of the expression itself. */
7152 if (t && e->expr_type == EXPR_VARIABLE
7153 && e->symtree->n.sym->attr.select_rank_temporary
7154 && UNLIMITED_POLY (e->symtree->n.sym))
7155 e->do_not_resolve_again = 1;
7157 return t;
7161 /* Resolve an expression from an iterator. They must be scalar and have
7162 INTEGER or (optionally) REAL type. */
7164 static bool
7165 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
7166 const char *name_msgid)
7168 if (!gfc_resolve_expr (expr))
7169 return false;
7171 if (expr->rank != 0)
7173 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
7174 return false;
7177 if (expr->ts.type != BT_INTEGER)
7179 if (expr->ts.type == BT_REAL)
7181 if (real_ok)
7182 return gfc_notify_std (GFC_STD_F95_DEL,
7183 "%s at %L must be integer",
7184 _(name_msgid), &expr->where);
7185 else
7187 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
7188 &expr->where);
7189 return false;
7192 else
7194 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
7195 return false;
7198 return true;
7202 /* Resolve the expressions in an iterator structure. If REAL_OK is
7203 false allow only INTEGER type iterators, otherwise allow REAL types.
7204 Set own_scope to true for ac-implied-do and data-implied-do as those
7205 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
7207 bool
7208 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
7210 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
7211 return false;
7213 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
7214 _("iterator variable")))
7215 return false;
7217 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
7218 "Start expression in DO loop"))
7219 return false;
7221 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
7222 "End expression in DO loop"))
7223 return false;
7225 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
7226 "Step expression in DO loop"))
7227 return false;
7229 /* Convert start, end, and step to the same type as var. */
7230 if (iter->start->ts.kind != iter->var->ts.kind
7231 || iter->start->ts.type != iter->var->ts.type)
7232 gfc_convert_type (iter->start, &iter->var->ts, 1);
7234 if (iter->end->ts.kind != iter->var->ts.kind
7235 || iter->end->ts.type != iter->var->ts.type)
7236 gfc_convert_type (iter->end, &iter->var->ts, 1);
7238 if (iter->step->ts.kind != iter->var->ts.kind
7239 || iter->step->ts.type != iter->var->ts.type)
7240 gfc_convert_type (iter->step, &iter->var->ts, 1);
7242 if (iter->step->expr_type == EXPR_CONSTANT)
7244 if ((iter->step->ts.type == BT_INTEGER
7245 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
7246 || (iter->step->ts.type == BT_REAL
7247 && mpfr_sgn (iter->step->value.real) == 0))
7249 gfc_error ("Step expression in DO loop at %L cannot be zero",
7250 &iter->step->where);
7251 return false;
7255 if (iter->start->expr_type == EXPR_CONSTANT
7256 && iter->end->expr_type == EXPR_CONSTANT
7257 && iter->step->expr_type == EXPR_CONSTANT)
7259 int sgn, cmp;
7260 if (iter->start->ts.type == BT_INTEGER)
7262 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
7263 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
7265 else
7267 sgn = mpfr_sgn (iter->step->value.real);
7268 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
7270 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
7271 gfc_warning (OPT_Wzerotrip,
7272 "DO loop at %L will be executed zero times",
7273 &iter->step->where);
7276 if (iter->end->expr_type == EXPR_CONSTANT
7277 && iter->end->ts.type == BT_INTEGER
7278 && iter->step->expr_type == EXPR_CONSTANT
7279 && iter->step->ts.type == BT_INTEGER
7280 && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
7281 || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
7283 bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
7284 int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
7286 if (is_step_positive
7287 && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
7288 gfc_warning (OPT_Wundefined_do_loop,
7289 "DO loop at %L is undefined as it overflows",
7290 &iter->step->where);
7291 else if (!is_step_positive
7292 && mpz_cmp (iter->end->value.integer,
7293 gfc_integer_kinds[k].min_int) == 0)
7294 gfc_warning (OPT_Wundefined_do_loop,
7295 "DO loop at %L is undefined as it underflows",
7296 &iter->step->where);
7299 return true;
7303 /* Traversal function for find_forall_index. f == 2 signals that
7304 that variable itself is not to be checked - only the references. */
7306 static bool
7307 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
7309 if (expr->expr_type != EXPR_VARIABLE)
7310 return false;
7312 /* A scalar assignment */
7313 if (!expr->ref || *f == 1)
7315 if (expr->symtree->n.sym == sym)
7316 return true;
7317 else
7318 return false;
7321 if (*f == 2)
7322 *f = 1;
7323 return false;
7327 /* Check whether the FORALL index appears in the expression or not.
7328 Returns true if SYM is found in EXPR. */
7330 bool
7331 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
7333 if (gfc_traverse_expr (expr, sym, forall_index, f))
7334 return true;
7335 else
7336 return false;
7340 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
7341 to be a scalar INTEGER variable. The subscripts and stride are scalar
7342 INTEGERs, and if stride is a constant it must be nonzero.
7343 Furthermore "A subscript or stride in a forall-triplet-spec shall
7344 not contain a reference to any index-name in the
7345 forall-triplet-spec-list in which it appears." (7.5.4.1) */
7347 static void
7348 resolve_forall_iterators (gfc_forall_iterator *it)
7350 gfc_forall_iterator *iter, *iter2;
7352 for (iter = it; iter; iter = iter->next)
7354 if (gfc_resolve_expr (iter->var)
7355 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
7356 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
7357 &iter->var->where);
7359 if (gfc_resolve_expr (iter->start)
7360 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
7361 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
7362 &iter->start->where);
7363 if (iter->var->ts.kind != iter->start->ts.kind)
7364 gfc_convert_type (iter->start, &iter->var->ts, 1);
7366 if (gfc_resolve_expr (iter->end)
7367 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
7368 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
7369 &iter->end->where);
7370 if (iter->var->ts.kind != iter->end->ts.kind)
7371 gfc_convert_type (iter->end, &iter->var->ts, 1);
7373 if (gfc_resolve_expr (iter->stride))
7375 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
7376 gfc_error ("FORALL stride expression at %L must be a scalar %s",
7377 &iter->stride->where, "INTEGER");
7379 if (iter->stride->expr_type == EXPR_CONSTANT
7380 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
7381 gfc_error ("FORALL stride expression at %L cannot be zero",
7382 &iter->stride->where);
7384 if (iter->var->ts.kind != iter->stride->ts.kind)
7385 gfc_convert_type (iter->stride, &iter->var->ts, 1);
7388 for (iter = it; iter; iter = iter->next)
7389 for (iter2 = iter; iter2; iter2 = iter2->next)
7391 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
7392 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
7393 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
7394 gfc_error ("FORALL index %qs may not appear in triplet "
7395 "specification at %L", iter->var->symtree->name,
7396 &iter2->start->where);
7401 /* Given a pointer to a symbol that is a derived type, see if it's
7402 inaccessible, i.e. if it's defined in another module and the components are
7403 PRIVATE. The search is recursive if necessary. Returns zero if no
7404 inaccessible components are found, nonzero otherwise. */
7406 static int
7407 derived_inaccessible (gfc_symbol *sym)
7409 gfc_component *c;
7411 if (sym->attr.use_assoc && sym->attr.private_comp)
7412 return 1;
7414 for (c = sym->components; c; c = c->next)
7416 /* Prevent an infinite loop through this function. */
7417 if (c->ts.type == BT_DERIVED && c->attr.pointer
7418 && sym == c->ts.u.derived)
7419 continue;
7421 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
7422 return 1;
7425 return 0;
7429 /* Resolve the argument of a deallocate expression. The expression must be
7430 a pointer or a full array. */
7432 static bool
7433 resolve_deallocate_expr (gfc_expr *e)
7435 symbol_attribute attr;
7436 int allocatable, pointer;
7437 gfc_ref *ref;
7438 gfc_symbol *sym;
7439 gfc_component *c;
7440 bool unlimited;
7442 if (!gfc_resolve_expr (e))
7443 return false;
7445 if (e->expr_type != EXPR_VARIABLE)
7446 goto bad;
7448 sym = e->symtree->n.sym;
7449 unlimited = UNLIMITED_POLY(sym);
7451 if (sym->ts.type == BT_CLASS)
7453 allocatable = CLASS_DATA (sym)->attr.allocatable;
7454 pointer = CLASS_DATA (sym)->attr.class_pointer;
7456 else
7458 allocatable = sym->attr.allocatable;
7459 pointer = sym->attr.pointer;
7461 for (ref = e->ref; ref; ref = ref->next)
7463 switch (ref->type)
7465 case REF_ARRAY:
7466 if (ref->u.ar.type != AR_FULL
7467 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
7468 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
7469 allocatable = 0;
7470 break;
7472 case REF_COMPONENT:
7473 c = ref->u.c.component;
7474 if (c->ts.type == BT_CLASS)
7476 allocatable = CLASS_DATA (c)->attr.allocatable;
7477 pointer = CLASS_DATA (c)->attr.class_pointer;
7479 else
7481 allocatable = c->attr.allocatable;
7482 pointer = c->attr.pointer;
7484 break;
7486 case REF_SUBSTRING:
7487 case REF_INQUIRY:
7488 allocatable = 0;
7489 break;
7493 attr = gfc_expr_attr (e);
7495 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
7497 bad:
7498 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7499 &e->where);
7500 return false;
7503 /* F2008, C644. */
7504 if (gfc_is_coindexed (e))
7506 gfc_error ("Coindexed allocatable object at %L", &e->where);
7507 return false;
7510 if (pointer
7511 && !gfc_check_vardef_context (e, true, true, false,
7512 _("DEALLOCATE object")))
7513 return false;
7514 if (!gfc_check_vardef_context (e, false, true, false,
7515 _("DEALLOCATE object")))
7516 return false;
7518 return true;
7522 /* Returns true if the expression e contains a reference to the symbol sym. */
7523 static bool
7524 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
7526 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
7527 return true;
7529 return false;
7532 bool
7533 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
7535 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
7539 /* Given the expression node e for an allocatable/pointer of derived type to be
7540 allocated, get the expression node to be initialized afterwards (needed for
7541 derived types with default initializers, and derived types with allocatable
7542 components that need nullification.) */
7544 gfc_expr *
7545 gfc_expr_to_initialize (gfc_expr *e)
7547 gfc_expr *result;
7548 gfc_ref *ref;
7549 int i;
7551 result = gfc_copy_expr (e);
7553 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
7554 for (ref = result->ref; ref; ref = ref->next)
7555 if (ref->type == REF_ARRAY && ref->next == NULL)
7557 if (ref->u.ar.dimen == 0
7558 && ref->u.ar.as && ref->u.ar.as->corank)
7559 return result;
7561 ref->u.ar.type = AR_FULL;
7563 for (i = 0; i < ref->u.ar.dimen; i++)
7564 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
7566 break;
7569 gfc_free_shape (&result->shape, result->rank);
7571 /* Recalculate rank, shape, etc. */
7572 gfc_resolve_expr (result);
7573 return result;
7577 /* If the last ref of an expression is an array ref, return a copy of the
7578 expression with that one removed. Otherwise, a copy of the original
7579 expression. This is used for allocate-expressions and pointer assignment
7580 LHS, where there may be an array specification that needs to be stripped
7581 off when using gfc_check_vardef_context. */
7583 static gfc_expr*
7584 remove_last_array_ref (gfc_expr* e)
7586 gfc_expr* e2;
7587 gfc_ref** r;
7589 e2 = gfc_copy_expr (e);
7590 for (r = &e2->ref; *r; r = &(*r)->next)
7591 if ((*r)->type == REF_ARRAY && !(*r)->next)
7593 gfc_free_ref_list (*r);
7594 *r = NULL;
7595 break;
7598 return e2;
7602 /* Used in resolve_allocate_expr to check that a allocation-object and
7603 a source-expr are conformable. This does not catch all possible
7604 cases; in particular a runtime checking is needed. */
7606 static bool
7607 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
7609 gfc_ref *tail;
7610 for (tail = e2->ref; tail && tail->next; tail = tail->next);
7612 /* First compare rank. */
7613 if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
7614 || (!tail && e1->rank != e2->rank))
7616 gfc_error ("Source-expr at %L must be scalar or have the "
7617 "same rank as the allocate-object at %L",
7618 &e1->where, &e2->where);
7619 return false;
7622 if (e1->shape)
7624 int i;
7625 mpz_t s;
7627 mpz_init (s);
7629 for (i = 0; i < e1->rank; i++)
7631 if (tail->u.ar.start[i] == NULL)
7632 break;
7634 if (tail->u.ar.end[i])
7636 mpz_set (s, tail->u.ar.end[i]->value.integer);
7637 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
7638 mpz_add_ui (s, s, 1);
7640 else
7642 mpz_set (s, tail->u.ar.start[i]->value.integer);
7645 if (mpz_cmp (e1->shape[i], s) != 0)
7647 gfc_error ("Source-expr at %L and allocate-object at %L must "
7648 "have the same shape", &e1->where, &e2->where);
7649 mpz_clear (s);
7650 return false;
7654 mpz_clear (s);
7657 return true;
7661 /* Resolve the expression in an ALLOCATE statement, doing the additional
7662 checks to see whether the expression is OK or not. The expression must
7663 have a trailing array reference that gives the size of the array. */
7665 static bool
7666 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
7668 int i, pointer, allocatable, dimension, is_abstract;
7669 int codimension;
7670 bool coindexed;
7671 bool unlimited;
7672 symbol_attribute attr;
7673 gfc_ref *ref, *ref2;
7674 gfc_expr *e2;
7675 gfc_array_ref *ar;
7676 gfc_symbol *sym = NULL;
7677 gfc_alloc *a;
7678 gfc_component *c;
7679 bool t;
7681 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
7682 checking of coarrays. */
7683 for (ref = e->ref; ref; ref = ref->next)
7684 if (ref->next == NULL)
7685 break;
7687 if (ref && ref->type == REF_ARRAY)
7688 ref->u.ar.in_allocate = true;
7690 if (!gfc_resolve_expr (e))
7691 goto failure;
7693 /* Make sure the expression is allocatable or a pointer. If it is
7694 pointer, the next-to-last reference must be a pointer. */
7696 ref2 = NULL;
7697 if (e->symtree)
7698 sym = e->symtree->n.sym;
7700 /* Check whether ultimate component is abstract and CLASS. */
7701 is_abstract = 0;
7703 /* Is the allocate-object unlimited polymorphic? */
7704 unlimited = UNLIMITED_POLY(e);
7706 if (e->expr_type != EXPR_VARIABLE)
7708 allocatable = 0;
7709 attr = gfc_expr_attr (e);
7710 pointer = attr.pointer;
7711 dimension = attr.dimension;
7712 codimension = attr.codimension;
7714 else
7716 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
7718 allocatable = CLASS_DATA (sym)->attr.allocatable;
7719 pointer = CLASS_DATA (sym)->attr.class_pointer;
7720 dimension = CLASS_DATA (sym)->attr.dimension;
7721 codimension = CLASS_DATA (sym)->attr.codimension;
7722 is_abstract = CLASS_DATA (sym)->attr.abstract;
7724 else
7726 allocatable = sym->attr.allocatable;
7727 pointer = sym->attr.pointer;
7728 dimension = sym->attr.dimension;
7729 codimension = sym->attr.codimension;
7732 coindexed = false;
7734 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
7736 switch (ref->type)
7738 case REF_ARRAY:
7739 if (ref->u.ar.codimen > 0)
7741 int n;
7742 for (n = ref->u.ar.dimen;
7743 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
7744 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
7746 coindexed = true;
7747 break;
7751 if (ref->next != NULL)
7752 pointer = 0;
7753 break;
7755 case REF_COMPONENT:
7756 /* F2008, C644. */
7757 if (coindexed)
7759 gfc_error ("Coindexed allocatable object at %L",
7760 &e->where);
7761 goto failure;
7764 c = ref->u.c.component;
7765 if (c->ts.type == BT_CLASS)
7767 allocatable = CLASS_DATA (c)->attr.allocatable;
7768 pointer = CLASS_DATA (c)->attr.class_pointer;
7769 dimension = CLASS_DATA (c)->attr.dimension;
7770 codimension = CLASS_DATA (c)->attr.codimension;
7771 is_abstract = CLASS_DATA (c)->attr.abstract;
7773 else
7775 allocatable = c->attr.allocatable;
7776 pointer = c->attr.pointer;
7777 dimension = c->attr.dimension;
7778 codimension = c->attr.codimension;
7779 is_abstract = c->attr.abstract;
7781 break;
7783 case REF_SUBSTRING:
7784 case REF_INQUIRY:
7785 allocatable = 0;
7786 pointer = 0;
7787 break;
7792 /* Check for F08:C628. */
7793 if (allocatable == 0 && pointer == 0 && !unlimited)
7795 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7796 &e->where);
7797 goto failure;
7800 /* Some checks for the SOURCE tag. */
7801 if (code->expr3)
7803 /* Check F03:C631. */
7804 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7806 gfc_error ("Type of entity at %L is type incompatible with "
7807 "source-expr at %L", &e->where, &code->expr3->where);
7808 goto failure;
7811 /* Check F03:C632 and restriction following Note 6.18. */
7812 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
7813 goto failure;
7815 /* Check F03:C633. */
7816 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
7818 gfc_error ("The allocate-object at %L and the source-expr at %L "
7819 "shall have the same kind type parameter",
7820 &e->where, &code->expr3->where);
7821 goto failure;
7824 /* Check F2008, C642. */
7825 if (code->expr3->ts.type == BT_DERIVED
7826 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7827 || (code->expr3->ts.u.derived->from_intmod
7828 == INTMOD_ISO_FORTRAN_ENV
7829 && code->expr3->ts.u.derived->intmod_sym_id
7830 == ISOFORTRAN_LOCK_TYPE)))
7832 gfc_error ("The source-expr at %L shall neither be of type "
7833 "LOCK_TYPE nor have a LOCK_TYPE component if "
7834 "allocate-object at %L is a coarray",
7835 &code->expr3->where, &e->where);
7836 goto failure;
7839 /* Check TS18508, C702/C703. */
7840 if (code->expr3->ts.type == BT_DERIVED
7841 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
7842 || (code->expr3->ts.u.derived->from_intmod
7843 == INTMOD_ISO_FORTRAN_ENV
7844 && code->expr3->ts.u.derived->intmod_sym_id
7845 == ISOFORTRAN_EVENT_TYPE)))
7847 gfc_error ("The source-expr at %L shall neither be of type "
7848 "EVENT_TYPE nor have a EVENT_TYPE component if "
7849 "allocate-object at %L is a coarray",
7850 &code->expr3->where, &e->where);
7851 goto failure;
7855 /* Check F08:C629. */
7856 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7857 && !code->expr3)
7859 gcc_assert (e->ts.type == BT_CLASS);
7860 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7861 "type-spec or source-expr", sym->name, &e->where);
7862 goto failure;
7865 /* Check F08:C632. */
7866 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
7867 && !UNLIMITED_POLY (e))
7869 int cmp;
7871 if (!e->ts.u.cl->length)
7872 goto failure;
7874 cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7875 code->ext.alloc.ts.u.cl->length);
7876 if (cmp == 1 || cmp == -1 || cmp == -3)
7878 gfc_error ("Allocating %s at %L with type-spec requires the same "
7879 "character-length parameter as in the declaration",
7880 sym->name, &e->where);
7881 goto failure;
7885 /* In the variable definition context checks, gfc_expr_attr is used
7886 on the expression. This is fooled by the array specification
7887 present in e, thus we have to eliminate that one temporarily. */
7888 e2 = remove_last_array_ref (e);
7889 t = true;
7890 if (t && pointer)
7891 t = gfc_check_vardef_context (e2, true, true, false,
7892 _("ALLOCATE object"));
7893 if (t)
7894 t = gfc_check_vardef_context (e2, false, true, false,
7895 _("ALLOCATE object"));
7896 gfc_free_expr (e2);
7897 if (!t)
7898 goto failure;
7900 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7901 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7903 /* For class arrays, the initialization with SOURCE is done
7904 using _copy and trans_call. It is convenient to exploit that
7905 when the allocated type is different from the declared type but
7906 no SOURCE exists by setting expr3. */
7907 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
7909 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
7910 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
7911 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
7913 /* We have to zero initialize the integer variable. */
7914 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
7917 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
7919 /* Make sure the vtab symbol is present when
7920 the module variables are generated. */
7921 gfc_typespec ts = e->ts;
7922 if (code->expr3)
7923 ts = code->expr3->ts;
7924 else if (code->ext.alloc.ts.type == BT_DERIVED)
7925 ts = code->ext.alloc.ts;
7927 /* Finding the vtab also publishes the type's symbol. Therefore this
7928 statement is necessary. */
7929 gfc_find_derived_vtab (ts.u.derived);
7931 else if (unlimited && !UNLIMITED_POLY (code->expr3))
7933 /* Again, make sure the vtab symbol is present when
7934 the module variables are generated. */
7935 gfc_typespec *ts = NULL;
7936 if (code->expr3)
7937 ts = &code->expr3->ts;
7938 else
7939 ts = &code->ext.alloc.ts;
7941 gcc_assert (ts);
7943 /* Finding the vtab also publishes the type's symbol. Therefore this
7944 statement is necessary. */
7945 gfc_find_vtab (ts);
7948 if (dimension == 0 && codimension == 0)
7949 goto success;
7951 /* Make sure the last reference node is an array specification. */
7953 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
7954 || (dimension && ref2->u.ar.dimen == 0))
7956 /* F08:C633. */
7957 if (code->expr3)
7959 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
7960 "in ALLOCATE statement at %L", &e->where))
7961 goto failure;
7962 if (code->expr3->rank != 0)
7963 *array_alloc_wo_spec = true;
7964 else
7966 gfc_error ("Array specification or array-valued SOURCE= "
7967 "expression required in ALLOCATE statement at %L",
7968 &e->where);
7969 goto failure;
7972 else
7974 gfc_error ("Array specification required in ALLOCATE statement "
7975 "at %L", &e->where);
7976 goto failure;
7980 /* Make sure that the array section reference makes sense in the
7981 context of an ALLOCATE specification. */
7983 ar = &ref2->u.ar;
7985 if (codimension)
7986 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
7988 switch (ar->dimen_type[i])
7990 case DIMEN_THIS_IMAGE:
7991 gfc_error ("Coarray specification required in ALLOCATE statement "
7992 "at %L", &e->where);
7993 goto failure;
7995 case DIMEN_RANGE:
7996 if (ar->start[i] == 0 || ar->end[i] == 0)
7998 /* If ar->stride[i] is NULL, we issued a previous error. */
7999 if (ar->stride[i] == NULL)
8000 gfc_error ("Bad array specification in ALLOCATE statement "
8001 "at %L", &e->where);
8002 goto failure;
8004 else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
8006 gfc_error ("Upper cobound is less than lower cobound at %L",
8007 &ar->start[i]->where);
8008 goto failure;
8010 break;
8012 case DIMEN_ELEMENT:
8013 if (ar->start[i]->expr_type == EXPR_CONSTANT)
8015 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
8016 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
8018 gfc_error ("Upper cobound is less than lower cobound "
8019 "of 1 at %L", &ar->start[i]->where);
8020 goto failure;
8023 break;
8025 case DIMEN_STAR:
8026 break;
8028 default:
8029 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8030 &e->where);
8031 goto failure;
8035 for (i = 0; i < ar->dimen; i++)
8037 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
8038 goto check_symbols;
8040 switch (ar->dimen_type[i])
8042 case DIMEN_ELEMENT:
8043 break;
8045 case DIMEN_RANGE:
8046 if (ar->start[i] != NULL
8047 && ar->end[i] != NULL
8048 && ar->stride[i] == NULL)
8049 break;
8051 /* Fall through. */
8053 case DIMEN_UNKNOWN:
8054 case DIMEN_VECTOR:
8055 case DIMEN_STAR:
8056 case DIMEN_THIS_IMAGE:
8057 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8058 &e->where);
8059 goto failure;
8062 check_symbols:
8063 for (a = code->ext.alloc.list; a; a = a->next)
8065 sym = a->expr->symtree->n.sym;
8067 /* TODO - check derived type components. */
8068 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
8069 continue;
8071 if ((ar->start[i] != NULL
8072 && gfc_find_sym_in_expr (sym, ar->start[i]))
8073 || (ar->end[i] != NULL
8074 && gfc_find_sym_in_expr (sym, ar->end[i])))
8076 gfc_error ("%qs must not appear in the array specification at "
8077 "%L in the same ALLOCATE statement where it is "
8078 "itself allocated", sym->name, &ar->where);
8079 goto failure;
8084 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
8086 if (ar->dimen_type[i] == DIMEN_ELEMENT
8087 || ar->dimen_type[i] == DIMEN_RANGE)
8089 if (i == (ar->dimen + ar->codimen - 1))
8091 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
8092 "statement at %L", &e->where);
8093 goto failure;
8095 continue;
8098 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
8099 && ar->stride[i] == NULL)
8100 break;
8102 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
8103 &e->where);
8104 goto failure;
8107 success:
8108 return true;
8110 failure:
8111 return false;
8115 static void
8116 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
8118 gfc_expr *stat, *errmsg, *pe, *qe;
8119 gfc_alloc *a, *p, *q;
8121 stat = code->expr1;
8122 errmsg = code->expr2;
8124 /* Check the stat variable. */
8125 if (stat)
8127 gfc_check_vardef_context (stat, false, false, false,
8128 _("STAT variable"));
8130 if ((stat->ts.type != BT_INTEGER
8131 && !(stat->ref && (stat->ref->type == REF_ARRAY
8132 || stat->ref->type == REF_COMPONENT)))
8133 || stat->rank > 0)
8134 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
8135 "variable", &stat->where);
8137 for (p = code->ext.alloc.list; p; p = p->next)
8138 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
8140 gfc_ref *ref1, *ref2;
8141 bool found = true;
8143 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
8144 ref1 = ref1->next, ref2 = ref2->next)
8146 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8147 continue;
8148 if (ref1->u.c.component->name != ref2->u.c.component->name)
8150 found = false;
8151 break;
8155 if (found)
8157 gfc_error ("Stat-variable at %L shall not be %sd within "
8158 "the same %s statement", &stat->where, fcn, fcn);
8159 break;
8164 /* Check the errmsg variable. */
8165 if (errmsg)
8167 if (!stat)
8168 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
8169 &errmsg->where);
8171 gfc_check_vardef_context (errmsg, false, false, false,
8172 _("ERRMSG variable"));
8174 /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
8175 F18:R930 errmsg-variable is scalar-default-char-variable
8176 F18:R906 default-char-variable is variable
8177 F18:C906 default-char-variable shall be default character. */
8178 if ((errmsg->ts.type != BT_CHARACTER
8179 && !(errmsg->ref
8180 && (errmsg->ref->type == REF_ARRAY
8181 || errmsg->ref->type == REF_COMPONENT)))
8182 || errmsg->rank > 0
8183 || errmsg->ts.kind != gfc_default_character_kind)
8184 gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
8185 "variable", &errmsg->where);
8187 for (p = code->ext.alloc.list; p; p = p->next)
8188 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
8190 gfc_ref *ref1, *ref2;
8191 bool found = true;
8193 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
8194 ref1 = ref1->next, ref2 = ref2->next)
8196 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8197 continue;
8198 if (ref1->u.c.component->name != ref2->u.c.component->name)
8200 found = false;
8201 break;
8205 if (found)
8207 gfc_error ("Errmsg-variable at %L shall not be %sd within "
8208 "the same %s statement", &errmsg->where, fcn, fcn);
8209 break;
8214 /* Check that an allocate-object appears only once in the statement. */
8216 for (p = code->ext.alloc.list; p; p = p->next)
8218 pe = p->expr;
8219 for (q = p->next; q; q = q->next)
8221 qe = q->expr;
8222 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
8224 /* This is a potential collision. */
8225 gfc_ref *pr = pe->ref;
8226 gfc_ref *qr = qe->ref;
8228 /* Follow the references until
8229 a) They start to differ, in which case there is no error;
8230 you can deallocate a%b and a%c in a single statement
8231 b) Both of them stop, which is an error
8232 c) One of them stops, which is also an error. */
8233 while (1)
8235 if (pr == NULL && qr == NULL)
8237 gfc_error ("Allocate-object at %L also appears at %L",
8238 &pe->where, &qe->where);
8239 break;
8241 else if (pr != NULL && qr == NULL)
8243 gfc_error ("Allocate-object at %L is subobject of"
8244 " object at %L", &pe->where, &qe->where);
8245 break;
8247 else if (pr == NULL && qr != NULL)
8249 gfc_error ("Allocate-object at %L is subobject of"
8250 " object at %L", &qe->where, &pe->where);
8251 break;
8253 /* Here, pr != NULL && qr != NULL */
8254 gcc_assert(pr->type == qr->type);
8255 if (pr->type == REF_ARRAY)
8257 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
8258 which are legal. */
8259 gcc_assert (qr->type == REF_ARRAY);
8261 if (pr->next && qr->next)
8263 int i;
8264 gfc_array_ref *par = &(pr->u.ar);
8265 gfc_array_ref *qar = &(qr->u.ar);
8267 for (i=0; i<par->dimen; i++)
8269 if ((par->start[i] != NULL
8270 || qar->start[i] != NULL)
8271 && gfc_dep_compare_expr (par->start[i],
8272 qar->start[i]) != 0)
8273 goto break_label;
8277 else
8279 if (pr->u.c.component->name != qr->u.c.component->name)
8280 break;
8283 pr = pr->next;
8284 qr = qr->next;
8286 break_label:
8292 if (strcmp (fcn, "ALLOCATE") == 0)
8294 bool arr_alloc_wo_spec = false;
8296 /* Resolving the expr3 in the loop over all objects to allocate would
8297 execute loop invariant code for each loop item. Therefore do it just
8298 once here. */
8299 if (code->expr3 && code->expr3->mold
8300 && code->expr3->ts.type == BT_DERIVED)
8302 /* Default initialization via MOLD (non-polymorphic). */
8303 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
8304 if (rhs != NULL)
8306 gfc_resolve_expr (rhs);
8307 gfc_free_expr (code->expr3);
8308 code->expr3 = rhs;
8311 for (a = code->ext.alloc.list; a; a = a->next)
8312 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
8314 if (arr_alloc_wo_spec && code->expr3)
8316 /* Mark the allocate to have to take the array specification
8317 from the expr3. */
8318 code->ext.alloc.arr_spec_from_expr3 = 1;
8321 else
8323 for (a = code->ext.alloc.list; a; a = a->next)
8324 resolve_deallocate_expr (a->expr);
8329 /************ SELECT CASE resolution subroutines ************/
8331 /* Callback function for our mergesort variant. Determines interval
8332 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
8333 op1 > op2. Assumes we're not dealing with the default case.
8334 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
8335 There are nine situations to check. */
8337 static int
8338 compare_cases (const gfc_case *op1, const gfc_case *op2)
8340 int retval;
8342 if (op1->low == NULL) /* op1 = (:L) */
8344 /* op2 = (:N), so overlap. */
8345 retval = 0;
8346 /* op2 = (M:) or (M:N), L < M */
8347 if (op2->low != NULL
8348 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8349 retval = -1;
8351 else if (op1->high == NULL) /* op1 = (K:) */
8353 /* op2 = (M:), so overlap. */
8354 retval = 0;
8355 /* op2 = (:N) or (M:N), K > N */
8356 if (op2->high != NULL
8357 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8358 retval = 1;
8360 else /* op1 = (K:L) */
8362 if (op2->low == NULL) /* op2 = (:N), K > N */
8363 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8364 ? 1 : 0;
8365 else if (op2->high == NULL) /* op2 = (M:), L < M */
8366 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8367 ? -1 : 0;
8368 else /* op2 = (M:N) */
8370 retval = 0;
8371 /* L < M */
8372 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8373 retval = -1;
8374 /* K > N */
8375 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8376 retval = 1;
8380 return retval;
8384 /* Merge-sort a double linked case list, detecting overlap in the
8385 process. LIST is the head of the double linked case list before it
8386 is sorted. Returns the head of the sorted list if we don't see any
8387 overlap, or NULL otherwise. */
8389 static gfc_case *
8390 check_case_overlap (gfc_case *list)
8392 gfc_case *p, *q, *e, *tail;
8393 int insize, nmerges, psize, qsize, cmp, overlap_seen;
8395 /* If the passed list was empty, return immediately. */
8396 if (!list)
8397 return NULL;
8399 overlap_seen = 0;
8400 insize = 1;
8402 /* Loop unconditionally. The only exit from this loop is a return
8403 statement, when we've finished sorting the case list. */
8404 for (;;)
8406 p = list;
8407 list = NULL;
8408 tail = NULL;
8410 /* Count the number of merges we do in this pass. */
8411 nmerges = 0;
8413 /* Loop while there exists a merge to be done. */
8414 while (p)
8416 int i;
8418 /* Count this merge. */
8419 nmerges++;
8421 /* Cut the list in two pieces by stepping INSIZE places
8422 forward in the list, starting from P. */
8423 psize = 0;
8424 q = p;
8425 for (i = 0; i < insize; i++)
8427 psize++;
8428 q = q->right;
8429 if (!q)
8430 break;
8432 qsize = insize;
8434 /* Now we have two lists. Merge them! */
8435 while (psize > 0 || (qsize > 0 && q != NULL))
8437 /* See from which the next case to merge comes from. */
8438 if (psize == 0)
8440 /* P is empty so the next case must come from Q. */
8441 e = q;
8442 q = q->right;
8443 qsize--;
8445 else if (qsize == 0 || q == NULL)
8447 /* Q is empty. */
8448 e = p;
8449 p = p->right;
8450 psize--;
8452 else
8454 cmp = compare_cases (p, q);
8455 if (cmp < 0)
8457 /* The whole case range for P is less than the
8458 one for Q. */
8459 e = p;
8460 p = p->right;
8461 psize--;
8463 else if (cmp > 0)
8465 /* The whole case range for Q is greater than
8466 the case range for P. */
8467 e = q;
8468 q = q->right;
8469 qsize--;
8471 else
8473 /* The cases overlap, or they are the same
8474 element in the list. Either way, we must
8475 issue an error and get the next case from P. */
8476 /* FIXME: Sort P and Q by line number. */
8477 gfc_error ("CASE label at %L overlaps with CASE "
8478 "label at %L", &p->where, &q->where);
8479 overlap_seen = 1;
8480 e = p;
8481 p = p->right;
8482 psize--;
8486 /* Add the next element to the merged list. */
8487 if (tail)
8488 tail->right = e;
8489 else
8490 list = e;
8491 e->left = tail;
8492 tail = e;
8495 /* P has now stepped INSIZE places along, and so has Q. So
8496 they're the same. */
8497 p = q;
8499 tail->right = NULL;
8501 /* If we have done only one merge or none at all, we've
8502 finished sorting the cases. */
8503 if (nmerges <= 1)
8505 if (!overlap_seen)
8506 return list;
8507 else
8508 return NULL;
8511 /* Otherwise repeat, merging lists twice the size. */
8512 insize *= 2;
8517 /* Check to see if an expression is suitable for use in a CASE statement.
8518 Makes sure that all case expressions are scalar constants of the same
8519 type. Return false if anything is wrong. */
8521 static bool
8522 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
8524 if (e == NULL) return true;
8526 if (e->ts.type != case_expr->ts.type)
8528 gfc_error ("Expression in CASE statement at %L must be of type %s",
8529 &e->where, gfc_basic_typename (case_expr->ts.type));
8530 return false;
8533 /* C805 (R808) For a given case-construct, each case-value shall be of
8534 the same type as case-expr. For character type, length differences
8535 are allowed, but the kind type parameters shall be the same. */
8537 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
8539 gfc_error ("Expression in CASE statement at %L must be of kind %d",
8540 &e->where, case_expr->ts.kind);
8541 return false;
8544 /* Convert the case value kind to that of case expression kind,
8545 if needed */
8547 if (e->ts.kind != case_expr->ts.kind)
8548 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
8550 if (e->rank != 0)
8552 gfc_error ("Expression in CASE statement at %L must be scalar",
8553 &e->where);
8554 return false;
8557 return true;
8561 /* Given a completely parsed select statement, we:
8563 - Validate all expressions and code within the SELECT.
8564 - Make sure that the selection expression is not of the wrong type.
8565 - Make sure that no case ranges overlap.
8566 - Eliminate unreachable cases and unreachable code resulting from
8567 removing case labels.
8569 The standard does allow unreachable cases, e.g. CASE (5:3). But
8570 they are a hassle for code generation, and to prevent that, we just
8571 cut them out here. This is not necessary for overlapping cases
8572 because they are illegal and we never even try to generate code.
8574 We have the additional caveat that a SELECT construct could have
8575 been a computed GOTO in the source code. Fortunately we can fairly
8576 easily work around that here: The case_expr for a "real" SELECT CASE
8577 is in code->expr1, but for a computed GOTO it is in code->expr2. All
8578 we have to do is make sure that the case_expr is a scalar integer
8579 expression. */
8581 static void
8582 resolve_select (gfc_code *code, bool select_type)
8584 gfc_code *body;
8585 gfc_expr *case_expr;
8586 gfc_case *cp, *default_case, *tail, *head;
8587 int seen_unreachable;
8588 int seen_logical;
8589 int ncases;
8590 bt type;
8591 bool t;
8593 if (code->expr1 == NULL)
8595 /* This was actually a computed GOTO statement. */
8596 case_expr = code->expr2;
8597 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
8598 gfc_error ("Selection expression in computed GOTO statement "
8599 "at %L must be a scalar integer expression",
8600 &case_expr->where);
8602 /* Further checking is not necessary because this SELECT was built
8603 by the compiler, so it should always be OK. Just move the
8604 case_expr from expr2 to expr so that we can handle computed
8605 GOTOs as normal SELECTs from here on. */
8606 code->expr1 = code->expr2;
8607 code->expr2 = NULL;
8608 return;
8611 case_expr = code->expr1;
8612 type = case_expr->ts.type;
8614 /* F08:C830. */
8615 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
8617 gfc_error ("Argument of SELECT statement at %L cannot be %s",
8618 &case_expr->where, gfc_typename (case_expr));
8620 /* Punt. Going on here just produce more garbage error messages. */
8621 return;
8624 /* F08:R842. */
8625 if (!select_type && case_expr->rank != 0)
8627 gfc_error ("Argument of SELECT statement at %L must be a scalar "
8628 "expression", &case_expr->where);
8630 /* Punt. */
8631 return;
8634 /* Raise a warning if an INTEGER case value exceeds the range of
8635 the case-expr. Later, all expressions will be promoted to the
8636 largest kind of all case-labels. */
8638 if (type == BT_INTEGER)
8639 for (body = code->block; body; body = body->block)
8640 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8642 if (cp->low
8643 && gfc_check_integer_range (cp->low->value.integer,
8644 case_expr->ts.kind) != ARITH_OK)
8645 gfc_warning (0, "Expression in CASE statement at %L is "
8646 "not in the range of %s", &cp->low->where,
8647 gfc_typename (case_expr));
8649 if (cp->high
8650 && cp->low != cp->high
8651 && gfc_check_integer_range (cp->high->value.integer,
8652 case_expr->ts.kind) != ARITH_OK)
8653 gfc_warning (0, "Expression in CASE statement at %L is "
8654 "not in the range of %s", &cp->high->where,
8655 gfc_typename (case_expr));
8658 /* PR 19168 has a long discussion concerning a mismatch of the kinds
8659 of the SELECT CASE expression and its CASE values. Walk the lists
8660 of case values, and if we find a mismatch, promote case_expr to
8661 the appropriate kind. */
8663 if (type == BT_LOGICAL || type == BT_INTEGER)
8665 for (body = code->block; body; body = body->block)
8667 /* Walk the case label list. */
8668 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8670 /* Intercept the DEFAULT case. It does not have a kind. */
8671 if (cp->low == NULL && cp->high == NULL)
8672 continue;
8674 /* Unreachable case ranges are discarded, so ignore. */
8675 if (cp->low != NULL && cp->high != NULL
8676 && cp->low != cp->high
8677 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8678 continue;
8680 if (cp->low != NULL
8681 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
8682 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
8684 if (cp->high != NULL
8685 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
8686 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
8691 /* Assume there is no DEFAULT case. */
8692 default_case = NULL;
8693 head = tail = NULL;
8694 ncases = 0;
8695 seen_logical = 0;
8697 for (body = code->block; body; body = body->block)
8699 /* Assume the CASE list is OK, and all CASE labels can be matched. */
8700 t = true;
8701 seen_unreachable = 0;
8703 /* Walk the case label list, making sure that all case labels
8704 are legal. */
8705 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8707 /* Count the number of cases in the whole construct. */
8708 ncases++;
8710 /* Intercept the DEFAULT case. */
8711 if (cp->low == NULL && cp->high == NULL)
8713 if (default_case != NULL)
8715 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8716 "by a second DEFAULT CASE at %L",
8717 &default_case->where, &cp->where);
8718 t = false;
8719 break;
8721 else
8723 default_case = cp;
8724 continue;
8728 /* Deal with single value cases and case ranges. Errors are
8729 issued from the validation function. */
8730 if (!validate_case_label_expr (cp->low, case_expr)
8731 || !validate_case_label_expr (cp->high, case_expr))
8733 t = false;
8734 break;
8737 if (type == BT_LOGICAL
8738 && ((cp->low == NULL || cp->high == NULL)
8739 || cp->low != cp->high))
8741 gfc_error ("Logical range in CASE statement at %L is not "
8742 "allowed", &cp->low->where);
8743 t = false;
8744 break;
8747 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
8749 int value;
8750 value = cp->low->value.logical == 0 ? 2 : 1;
8751 if (value & seen_logical)
8753 gfc_error ("Constant logical value in CASE statement "
8754 "is repeated at %L",
8755 &cp->low->where);
8756 t = false;
8757 break;
8759 seen_logical |= value;
8762 if (cp->low != NULL && cp->high != NULL
8763 && cp->low != cp->high
8764 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8766 if (warn_surprising)
8767 gfc_warning (OPT_Wsurprising,
8768 "Range specification at %L can never be matched",
8769 &cp->where);
8771 cp->unreachable = 1;
8772 seen_unreachable = 1;
8774 else
8776 /* If the case range can be matched, it can also overlap with
8777 other cases. To make sure it does not, we put it in a
8778 double linked list here. We sort that with a merge sort
8779 later on to detect any overlapping cases. */
8780 if (!head)
8782 head = tail = cp;
8783 head->right = head->left = NULL;
8785 else
8787 tail->right = cp;
8788 tail->right->left = tail;
8789 tail = tail->right;
8790 tail->right = NULL;
8795 /* It there was a failure in the previous case label, give up
8796 for this case label list. Continue with the next block. */
8797 if (!t)
8798 continue;
8800 /* See if any case labels that are unreachable have been seen.
8801 If so, we eliminate them. This is a bit of a kludge because
8802 the case lists for a single case statement (label) is a
8803 single forward linked lists. */
8804 if (seen_unreachable)
8806 /* Advance until the first case in the list is reachable. */
8807 while (body->ext.block.case_list != NULL
8808 && body->ext.block.case_list->unreachable)
8810 gfc_case *n = body->ext.block.case_list;
8811 body->ext.block.case_list = body->ext.block.case_list->next;
8812 n->next = NULL;
8813 gfc_free_case_list (n);
8816 /* Strip all other unreachable cases. */
8817 if (body->ext.block.case_list)
8819 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
8821 if (cp->next->unreachable)
8823 gfc_case *n = cp->next;
8824 cp->next = cp->next->next;
8825 n->next = NULL;
8826 gfc_free_case_list (n);
8833 /* See if there were overlapping cases. If the check returns NULL,
8834 there was overlap. In that case we don't do anything. If head
8835 is non-NULL, we prepend the DEFAULT case. The sorted list can
8836 then used during code generation for SELECT CASE constructs with
8837 a case expression of a CHARACTER type. */
8838 if (head)
8840 head = check_case_overlap (head);
8842 /* Prepend the default_case if it is there. */
8843 if (head != NULL && default_case)
8845 default_case->left = NULL;
8846 default_case->right = head;
8847 head->left = default_case;
8851 /* Eliminate dead blocks that may be the result if we've seen
8852 unreachable case labels for a block. */
8853 for (body = code; body && body->block; body = body->block)
8855 if (body->block->ext.block.case_list == NULL)
8857 /* Cut the unreachable block from the code chain. */
8858 gfc_code *c = body->block;
8859 body->block = c->block;
8861 /* Kill the dead block, but not the blocks below it. */
8862 c->block = NULL;
8863 gfc_free_statements (c);
8867 /* More than two cases is legal but insane for logical selects.
8868 Issue a warning for it. */
8869 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
8870 gfc_warning (OPT_Wsurprising,
8871 "Logical SELECT CASE block at %L has more that two cases",
8872 &code->loc);
8876 /* Check if a derived type is extensible. */
8878 bool
8879 gfc_type_is_extensible (gfc_symbol *sym)
8881 return !(sym->attr.is_bind_c || sym->attr.sequence
8882 || (sym->attr.is_class
8883 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
8887 static void
8888 resolve_types (gfc_namespace *ns);
8890 /* Resolve an associate-name: Resolve target and ensure the type-spec is
8891 correct as well as possibly the array-spec. */
8893 static void
8894 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
8896 gfc_expr* target;
8898 gcc_assert (sym->assoc);
8899 gcc_assert (sym->attr.flavor == FL_VARIABLE);
8901 /* If this is for SELECT TYPE, the target may not yet be set. In that
8902 case, return. Resolution will be called later manually again when
8903 this is done. */
8904 target = sym->assoc->target;
8905 if (!target)
8906 return;
8907 gcc_assert (!sym->assoc->dangling);
8909 if (resolve_target && !gfc_resolve_expr (target))
8910 return;
8912 /* For variable targets, we get some attributes from the target. */
8913 if (target->expr_type == EXPR_VARIABLE)
8915 gfc_symbol *tsym, *dsym;
8917 gcc_assert (target->symtree);
8918 tsym = target->symtree->n.sym;
8920 if (gfc_expr_attr (target).proc_pointer)
8922 gfc_error ("Associating entity %qs at %L is a procedure pointer",
8923 tsym->name, &target->where);
8924 return;
8927 if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
8928 && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
8929 && dsym->attr.flavor == FL_DERIVED)
8931 gfc_error ("Derived type %qs cannot be used as a variable at %L",
8932 tsym->name, &target->where);
8933 return;
8936 if (tsym->attr.flavor == FL_PROCEDURE)
8938 bool is_error = true;
8939 if (tsym->attr.function && tsym->result == tsym)
8940 for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
8941 if (tsym == ns->proc_name)
8943 is_error = false;
8944 break;
8946 if (is_error)
8948 gfc_error ("Associating entity %qs at %L is a procedure name",
8949 tsym->name, &target->where);
8950 return;
8954 sym->attr.asynchronous = tsym->attr.asynchronous;
8955 sym->attr.volatile_ = tsym->attr.volatile_;
8957 sym->attr.target = tsym->attr.target
8958 || gfc_expr_attr (target).pointer;
8959 if (is_subref_array (target))
8960 sym->attr.subref_array_pointer = 1;
8962 else if (target->ts.type == BT_PROCEDURE)
8964 gfc_error ("Associating selector-expression at %L yields a procedure",
8965 &target->where);
8966 return;
8969 if (target->expr_type == EXPR_NULL)
8971 gfc_error ("Selector at %L cannot be NULL()", &target->where);
8972 return;
8974 else if (target->ts.type == BT_UNKNOWN)
8976 gfc_error ("Selector at %L has no type", &target->where);
8977 return;
8980 /* Get type if this was not already set. Note that it can be
8981 some other type than the target in case this is a SELECT TYPE
8982 selector! So we must not update when the type is already there. */
8983 if (sym->ts.type == BT_UNKNOWN)
8984 sym->ts = target->ts;
8986 gcc_assert (sym->ts.type != BT_UNKNOWN);
8988 /* See if this is a valid association-to-variable. */
8989 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
8990 && !gfc_has_vector_subscript (target));
8992 /* Finally resolve if this is an array or not. */
8993 if (sym->attr.dimension && target->rank == 0)
8995 /* primary.c makes the assumption that a reference to an associate
8996 name followed by a left parenthesis is an array reference. */
8997 if (sym->ts.type != BT_CHARACTER)
8998 gfc_error ("Associate-name %qs at %L is used as array",
8999 sym->name, &sym->declared_at);
9000 sym->attr.dimension = 0;
9001 return;
9005 /* We cannot deal with class selectors that need temporaries. */
9006 if (target->ts.type == BT_CLASS
9007 && gfc_ref_needs_temporary_p (target->ref))
9009 gfc_error ("CLASS selector at %L needs a temporary which is not "
9010 "yet implemented", &target->where);
9011 return;
9014 if (target->ts.type == BT_CLASS)
9015 gfc_fix_class_refs (target);
9017 if (target->rank != 0 && !sym->attr.select_rank_temporary)
9019 gfc_array_spec *as;
9020 /* The rank may be incorrectly guessed at parsing, therefore make sure
9021 it is corrected now. */
9022 if (sym->ts.type != BT_CLASS && (!sym->as || sym->assoc->rankguessed))
9024 if (!sym->as)
9025 sym->as = gfc_get_array_spec ();
9026 as = sym->as;
9027 as->rank = target->rank;
9028 as->type = AS_DEFERRED;
9029 as->corank = gfc_get_corank (target);
9030 sym->attr.dimension = 1;
9031 if (as->corank != 0)
9032 sym->attr.codimension = 1;
9034 else if (sym->ts.type == BT_CLASS
9035 && CLASS_DATA (sym)
9036 && (!CLASS_DATA (sym)->as || sym->assoc->rankguessed))
9038 if (!CLASS_DATA (sym)->as)
9039 CLASS_DATA (sym)->as = gfc_get_array_spec ();
9040 as = CLASS_DATA (sym)->as;
9041 as->rank = target->rank;
9042 as->type = AS_DEFERRED;
9043 as->corank = gfc_get_corank (target);
9044 CLASS_DATA (sym)->attr.dimension = 1;
9045 if (as->corank != 0)
9046 CLASS_DATA (sym)->attr.codimension = 1;
9049 else if (!sym->attr.select_rank_temporary)
9051 /* target's rank is 0, but the type of the sym is still array valued,
9052 which has to be corrected. */
9053 if (sym->ts.type == BT_CLASS && sym->ts.u.derived
9054 && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
9056 gfc_array_spec *as;
9057 symbol_attribute attr;
9058 /* The associated variable's type is still the array type
9059 correct this now. */
9060 gfc_typespec *ts = &target->ts;
9061 gfc_ref *ref;
9062 gfc_component *c;
9063 for (ref = target->ref; ref != NULL; ref = ref->next)
9065 switch (ref->type)
9067 case REF_COMPONENT:
9068 ts = &ref->u.c.component->ts;
9069 break;
9070 case REF_ARRAY:
9071 if (ts->type == BT_CLASS)
9072 ts = &ts->u.derived->components->ts;
9073 break;
9074 default:
9075 break;
9078 /* Create a scalar instance of the current class type. Because the
9079 rank of a class array goes into its name, the type has to be
9080 rebuild. The alternative of (re-)setting just the attributes
9081 and as in the current type, destroys the type also in other
9082 places. */
9083 as = NULL;
9084 sym->ts = *ts;
9085 sym->ts.type = BT_CLASS;
9086 attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
9087 attr.class_ok = 0;
9088 attr.associate_var = 1;
9089 attr.dimension = attr.codimension = 0;
9090 attr.class_pointer = 1;
9091 if (!gfc_build_class_symbol (&sym->ts, &attr, &as))
9092 gcc_unreachable ();
9093 /* Make sure the _vptr is set. */
9094 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true, NULL);
9095 if (c->ts.u.derived == NULL)
9096 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived);
9097 CLASS_DATA (sym)->attr.pointer = 1;
9098 CLASS_DATA (sym)->attr.class_pointer = 1;
9099 gfc_set_sym_referenced (sym->ts.u.derived);
9100 gfc_commit_symbol (sym->ts.u.derived);
9101 /* _vptr now has the _vtab in it, change it to the _vtype. */
9102 if (c->ts.u.derived->attr.vtab)
9103 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
9104 c->ts.u.derived->ns->types_resolved = 0;
9105 resolve_types (c->ts.u.derived->ns);
9109 /* Mark this as an associate variable. */
9110 sym->attr.associate_var = 1;
9112 /* Fix up the type-spec for CHARACTER types. */
9113 if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
9115 if (!sym->ts.u.cl)
9116 sym->ts.u.cl = target->ts.u.cl;
9118 if (sym->ts.deferred && target->expr_type == EXPR_VARIABLE
9119 && target->symtree->n.sym->attr.dummy
9120 && sym->ts.u.cl == target->ts.u.cl)
9122 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9123 sym->ts.deferred = 1;
9126 if (!sym->ts.u.cl->length
9127 && !sym->ts.deferred
9128 && target->expr_type == EXPR_CONSTANT)
9130 sym->ts.u.cl->length =
9131 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
9132 target->value.character.length);
9134 else if ((!sym->ts.u.cl->length
9135 || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
9136 && target->expr_type != EXPR_VARIABLE)
9138 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9139 sym->ts.deferred = 1;
9141 /* This is reset in trans-stmt.c after the assignment
9142 of the target expression to the associate name. */
9143 sym->attr.allocatable = 1;
9147 /* If the target is a good class object, so is the associate variable. */
9148 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
9149 sym->attr.class_ok = 1;
9153 /* Ensure that SELECT TYPE expressions have the correct rank and a full
9154 array reference, where necessary. The symbols are artificial and so
9155 the dimension attribute and arrayspec can also be set. In addition,
9156 sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
9157 This is corrected here as well.*/
9159 static void
9160 fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2,
9161 int rank, gfc_ref *ref)
9163 gfc_ref *nref = (*expr1)->ref;
9164 gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
9165 gfc_symbol *sym2 = expr2 ? expr2->symtree->n.sym : NULL;
9166 (*expr1)->rank = rank;
9167 if (sym1->ts.type == BT_CLASS)
9169 if ((*expr1)->ts.type != BT_CLASS)
9170 (*expr1)->ts = sym1->ts;
9172 CLASS_DATA (sym1)->attr.dimension = 1;
9173 if (CLASS_DATA (sym1)->as == NULL && sym2)
9174 CLASS_DATA (sym1)->as
9175 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
9177 else
9179 sym1->attr.dimension = 1;
9180 if (sym1->as == NULL && sym2)
9181 sym1->as = gfc_copy_array_spec (sym2->as);
9184 for (; nref; nref = nref->next)
9185 if (nref->next == NULL)
9186 break;
9188 if (ref && nref && nref->type != REF_ARRAY)
9189 nref->next = gfc_copy_ref (ref);
9190 else if (ref && !nref)
9191 (*expr1)->ref = gfc_copy_ref (ref);
9195 static gfc_expr *
9196 build_loc_call (gfc_expr *sym_expr)
9198 gfc_expr *loc_call;
9199 loc_call = gfc_get_expr ();
9200 loc_call->expr_type = EXPR_FUNCTION;
9201 gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
9202 loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
9203 loc_call->symtree->n.sym->attr.intrinsic = 1;
9204 loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
9205 gfc_commit_symbol (loc_call->symtree->n.sym);
9206 loc_call->ts.type = BT_INTEGER;
9207 loc_call->ts.kind = gfc_index_integer_kind;
9208 loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
9209 loc_call->value.function.actual = gfc_get_actual_arglist ();
9210 loc_call->value.function.actual->expr = sym_expr;
9211 loc_call->where = sym_expr->where;
9212 return loc_call;
9215 /* Resolve a SELECT TYPE statement. */
9217 static void
9218 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
9220 gfc_symbol *selector_type;
9221 gfc_code *body, *new_st, *if_st, *tail;
9222 gfc_code *class_is = NULL, *default_case = NULL;
9223 gfc_case *c;
9224 gfc_symtree *st;
9225 char name[GFC_MAX_SYMBOL_LEN];
9226 gfc_namespace *ns;
9227 int error = 0;
9228 int rank = 0;
9229 gfc_ref* ref = NULL;
9230 gfc_expr *selector_expr = NULL;
9232 ns = code->ext.block.ns;
9233 gfc_resolve (ns);
9235 /* Check for F03:C813. */
9236 if (code->expr1->ts.type != BT_CLASS
9237 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
9239 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
9240 "at %L", &code->loc);
9241 return;
9244 if (!code->expr1->symtree->n.sym->attr.class_ok)
9245 return;
9247 if (code->expr2)
9249 gfc_ref *ref2 = NULL;
9250 for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
9251 if (ref->type == REF_COMPONENT
9252 && ref->u.c.component->ts.type == BT_CLASS)
9253 ref2 = ref;
9255 if (ref2)
9257 if (code->expr1->symtree->n.sym->attr.untyped)
9258 code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
9259 selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
9261 else
9263 if (code->expr1->symtree->n.sym->attr.untyped)
9264 code->expr1->symtree->n.sym->ts = code->expr2->ts;
9265 selector_type = CLASS_DATA (code->expr2)
9266 ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
9269 if (code->expr2->rank
9270 && code->expr1->ts.type == BT_CLASS
9271 && CLASS_DATA (code->expr1)->as)
9272 CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
9274 /* F2008: C803 The selector expression must not be coindexed. */
9275 if (gfc_is_coindexed (code->expr2))
9277 gfc_error ("Selector at %L must not be coindexed",
9278 &code->expr2->where);
9279 return;
9283 else
9285 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
9287 if (gfc_is_coindexed (code->expr1))
9289 gfc_error ("Selector at %L must not be coindexed",
9290 &code->expr1->where);
9291 return;
9295 /* Loop over TYPE IS / CLASS IS cases. */
9296 for (body = code->block; body; body = body->block)
9298 c = body->ext.block.case_list;
9300 if (!error)
9302 /* Check for repeated cases. */
9303 for (tail = code->block; tail; tail = tail->block)
9305 gfc_case *d = tail->ext.block.case_list;
9306 if (tail == body)
9307 break;
9309 if (c->ts.type == d->ts.type
9310 && ((c->ts.type == BT_DERIVED
9311 && c->ts.u.derived && d->ts.u.derived
9312 && !strcmp (c->ts.u.derived->name,
9313 d->ts.u.derived->name))
9314 || c->ts.type == BT_UNKNOWN
9315 || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9316 && c->ts.kind == d->ts.kind)))
9318 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
9319 &c->where, &d->where);
9320 return;
9325 /* Check F03:C815. */
9326 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9327 && !selector_type->attr.unlimited_polymorphic
9328 && !gfc_type_is_extensible (c->ts.u.derived))
9330 gfc_error ("Derived type %qs at %L must be extensible",
9331 c->ts.u.derived->name, &c->where);
9332 error++;
9333 continue;
9336 /* Check F03:C816. */
9337 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
9338 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
9339 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
9341 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9342 gfc_error ("Derived type %qs at %L must be an extension of %qs",
9343 c->ts.u.derived->name, &c->where, selector_type->name);
9344 else
9345 gfc_error ("Unexpected intrinsic type %qs at %L",
9346 gfc_basic_typename (c->ts.type), &c->where);
9347 error++;
9348 continue;
9351 /* Check F03:C814. */
9352 if (c->ts.type == BT_CHARACTER
9353 && (c->ts.u.cl->length != NULL || c->ts.deferred))
9355 gfc_error ("The type-spec at %L shall specify that each length "
9356 "type parameter is assumed", &c->where);
9357 error++;
9358 continue;
9361 /* Intercept the DEFAULT case. */
9362 if (c->ts.type == BT_UNKNOWN)
9364 /* Check F03:C818. */
9365 if (default_case)
9367 gfc_error ("The DEFAULT CASE at %L cannot be followed "
9368 "by a second DEFAULT CASE at %L",
9369 &default_case->ext.block.case_list->where, &c->where);
9370 error++;
9371 continue;
9374 default_case = body;
9378 if (error > 0)
9379 return;
9381 /* Transform SELECT TYPE statement to BLOCK and associate selector to
9382 target if present. If there are any EXIT statements referring to the
9383 SELECT TYPE construct, this is no problem because the gfc_code
9384 reference stays the same and EXIT is equally possible from the BLOCK
9385 it is changed to. */
9386 code->op = EXEC_BLOCK;
9387 if (code->expr2)
9389 gfc_association_list* assoc;
9391 assoc = gfc_get_association_list ();
9392 assoc->st = code->expr1->symtree;
9393 assoc->target = gfc_copy_expr (code->expr2);
9394 assoc->target->where = code->expr2->where;
9395 /* assoc->variable will be set by resolve_assoc_var. */
9397 code->ext.block.assoc = assoc;
9398 code->expr1->symtree->n.sym->assoc = assoc;
9400 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9402 else
9403 code->ext.block.assoc = NULL;
9405 /* Ensure that the selector rank and arrayspec are available to
9406 correct expressions in which they might be missing. */
9407 if (code->expr2 && code->expr2->rank)
9409 rank = code->expr2->rank;
9410 for (ref = code->expr2->ref; ref; ref = ref->next)
9411 if (ref->next == NULL)
9412 break;
9413 if (ref && ref->type == REF_ARRAY)
9414 ref = gfc_copy_ref (ref);
9416 /* Fixup expr1 if necessary. */
9417 if (rank)
9418 fixup_array_ref (&code->expr1, code->expr2, rank, ref);
9420 else if (code->expr1->rank)
9422 rank = code->expr1->rank;
9423 for (ref = code->expr1->ref; ref; ref = ref->next)
9424 if (ref->next == NULL)
9425 break;
9426 if (ref && ref->type == REF_ARRAY)
9427 ref = gfc_copy_ref (ref);
9430 /* Add EXEC_SELECT to switch on type. */
9431 new_st = gfc_get_code (code->op);
9432 new_st->expr1 = code->expr1;
9433 new_st->expr2 = code->expr2;
9434 new_st->block = code->block;
9435 code->expr1 = code->expr2 = NULL;
9436 code->block = NULL;
9437 if (!ns->code)
9438 ns->code = new_st;
9439 else
9440 ns->code->next = new_st;
9441 code = new_st;
9442 code->op = EXEC_SELECT_TYPE;
9444 /* Use the intrinsic LOC function to generate an integer expression
9445 for the vtable of the selector. Note that the rank of the selector
9446 expression has to be set to zero. */
9447 gfc_add_vptr_component (code->expr1);
9448 code->expr1->rank = 0;
9449 code->expr1 = build_loc_call (code->expr1);
9450 selector_expr = code->expr1->value.function.actual->expr;
9452 /* Loop over TYPE IS / CLASS IS cases. */
9453 for (body = code->block; body; body = body->block)
9455 gfc_symbol *vtab;
9456 gfc_expr *e;
9457 c = body->ext.block.case_list;
9459 /* Generate an index integer expression for address of the
9460 TYPE/CLASS vtable and store it in c->low. The hash expression
9461 is stored in c->high and is used to resolve intrinsic cases. */
9462 if (c->ts.type != BT_UNKNOWN)
9464 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9466 vtab = gfc_find_derived_vtab (c->ts.u.derived);
9467 gcc_assert (vtab);
9468 c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
9469 c->ts.u.derived->hash_value);
9471 else
9473 vtab = gfc_find_vtab (&c->ts);
9474 gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
9475 e = CLASS_DATA (vtab)->initializer;
9476 c->high = gfc_copy_expr (e);
9477 if (c->high->ts.kind != gfc_integer_4_kind)
9479 gfc_typespec ts;
9480 ts.kind = gfc_integer_4_kind;
9481 ts.type = BT_INTEGER;
9482 gfc_convert_type_warn (c->high, &ts, 2, 0);
9486 e = gfc_lval_expr_from_sym (vtab);
9487 c->low = build_loc_call (e);
9489 else
9490 continue;
9492 /* Associate temporary to selector. This should only be done
9493 when this case is actually true, so build a new ASSOCIATE
9494 that does precisely this here (instead of using the
9495 'global' one). */
9497 if (c->ts.type == BT_CLASS)
9498 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
9499 else if (c->ts.type == BT_DERIVED)
9500 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
9501 else if (c->ts.type == BT_CHARACTER)
9503 HOST_WIDE_INT charlen = 0;
9504 if (c->ts.u.cl && c->ts.u.cl->length
9505 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9506 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9507 snprintf (name, sizeof (name),
9508 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9509 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9511 else
9512 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
9513 c->ts.kind);
9515 st = gfc_find_symtree (ns->sym_root, name);
9516 gcc_assert (st->n.sym->assoc);
9517 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9518 st->n.sym->assoc->target->where = selector_expr->where;
9519 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
9521 gfc_add_data_component (st->n.sym->assoc->target);
9522 /* Fixup the target expression if necessary. */
9523 if (rank)
9524 fixup_array_ref (&st->n.sym->assoc->target, NULL, rank, ref);
9527 new_st = gfc_get_code (EXEC_BLOCK);
9528 new_st->ext.block.ns = gfc_build_block_ns (ns);
9529 new_st->ext.block.ns->code = body->next;
9530 body->next = new_st;
9532 /* Chain in the new list only if it is marked as dangling. Otherwise
9533 there is a CASE label overlap and this is already used. Just ignore,
9534 the error is diagnosed elsewhere. */
9535 if (st->n.sym->assoc->dangling)
9537 new_st->ext.block.assoc = st->n.sym->assoc;
9538 st->n.sym->assoc->dangling = 0;
9541 resolve_assoc_var (st->n.sym, false);
9544 /* Take out CLASS IS cases for separate treatment. */
9545 body = code;
9546 while (body && body->block)
9548 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
9550 /* Add to class_is list. */
9551 if (class_is == NULL)
9553 class_is = body->block;
9554 tail = class_is;
9556 else
9558 for (tail = class_is; tail->block; tail = tail->block) ;
9559 tail->block = body->block;
9560 tail = tail->block;
9562 /* Remove from EXEC_SELECT list. */
9563 body->block = body->block->block;
9564 tail->block = NULL;
9566 else
9567 body = body->block;
9570 if (class_is)
9572 gfc_symbol *vtab;
9574 if (!default_case)
9576 /* Add a default case to hold the CLASS IS cases. */
9577 for (tail = code; tail->block; tail = tail->block) ;
9578 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
9579 tail = tail->block;
9580 tail->ext.block.case_list = gfc_get_case ();
9581 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
9582 tail->next = NULL;
9583 default_case = tail;
9586 /* More than one CLASS IS block? */
9587 if (class_is->block)
9589 gfc_code **c1,*c2;
9590 bool swapped;
9591 /* Sort CLASS IS blocks by extension level. */
9594 swapped = false;
9595 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
9597 c2 = (*c1)->block;
9598 /* F03:C817 (check for doubles). */
9599 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
9600 == c2->ext.block.case_list->ts.u.derived->hash_value)
9602 gfc_error ("Double CLASS IS block in SELECT TYPE "
9603 "statement at %L",
9604 &c2->ext.block.case_list->where);
9605 return;
9607 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
9608 < c2->ext.block.case_list->ts.u.derived->attr.extension)
9610 /* Swap. */
9611 (*c1)->block = c2->block;
9612 c2->block = *c1;
9613 *c1 = c2;
9614 swapped = true;
9618 while (swapped);
9621 /* Generate IF chain. */
9622 if_st = gfc_get_code (EXEC_IF);
9623 new_st = if_st;
9624 for (body = class_is; body; body = body->block)
9626 new_st->block = gfc_get_code (EXEC_IF);
9627 new_st = new_st->block;
9628 /* Set up IF condition: Call _gfortran_is_extension_of. */
9629 new_st->expr1 = gfc_get_expr ();
9630 new_st->expr1->expr_type = EXPR_FUNCTION;
9631 new_st->expr1->ts.type = BT_LOGICAL;
9632 new_st->expr1->ts.kind = 4;
9633 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
9634 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
9635 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
9636 /* Set up arguments. */
9637 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
9638 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
9639 new_st->expr1->value.function.actual->expr->where = code->loc;
9640 new_st->expr1->where = code->loc;
9641 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
9642 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
9643 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
9644 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
9645 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
9646 new_st->expr1->value.function.actual->next->expr->where = code->loc;
9647 /* Set up types in formal arg list. */
9648 new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
9649 new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
9650 new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
9651 new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
9653 new_st->next = body->next;
9655 if (default_case->next)
9657 new_st->block = gfc_get_code (EXEC_IF);
9658 new_st = new_st->block;
9659 new_st->next = default_case->next;
9662 /* Replace CLASS DEFAULT code by the IF chain. */
9663 default_case->next = if_st;
9666 /* Resolve the internal code. This cannot be done earlier because
9667 it requires that the sym->assoc of selectors is set already. */
9668 gfc_current_ns = ns;
9669 gfc_resolve_blocks (code->block, gfc_current_ns);
9670 gfc_current_ns = old_ns;
9672 if (ref)
9673 free (ref);
9677 /* Resolve a SELECT RANK statement. */
9679 static void
9680 resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
9682 gfc_namespace *ns;
9683 gfc_code *body, *new_st, *tail;
9684 gfc_case *c;
9685 char tname[GFC_MAX_SYMBOL_LEN + 7];
9686 char name[2 * GFC_MAX_SYMBOL_LEN];
9687 gfc_symtree *st;
9688 gfc_expr *selector_expr = NULL;
9689 int case_value;
9690 HOST_WIDE_INT charlen = 0;
9692 ns = code->ext.block.ns;
9693 gfc_resolve (ns);
9695 code->op = EXEC_BLOCK;
9696 if (code->expr2)
9698 gfc_association_list* assoc;
9700 assoc = gfc_get_association_list ();
9701 assoc->st = code->expr1->symtree;
9702 assoc->target = gfc_copy_expr (code->expr2);
9703 assoc->target->where = code->expr2->where;
9704 /* assoc->variable will be set by resolve_assoc_var. */
9706 code->ext.block.assoc = assoc;
9707 code->expr1->symtree->n.sym->assoc = assoc;
9709 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9711 else
9712 code->ext.block.assoc = NULL;
9714 /* Loop over RANK cases. Note that returning on the errors causes a
9715 cascade of further errors because the case blocks do not compile
9716 correctly. */
9717 for (body = code->block; body; body = body->block)
9719 c = body->ext.block.case_list;
9720 if (c->low)
9721 case_value = (int) mpz_get_si (c->low->value.integer);
9722 else
9723 case_value = -2;
9725 /* Check for repeated cases. */
9726 for (tail = code->block; tail; tail = tail->block)
9728 gfc_case *d = tail->ext.block.case_list;
9729 int case_value2;
9731 if (tail == body)
9732 break;
9734 /* Check F2018: C1153. */
9735 if (!c->low && !d->low)
9736 gfc_error ("RANK DEFAULT at %L is repeated at %L",
9737 &c->where, &d->where);
9739 if (!c->low || !d->low)
9740 continue;
9742 /* Check F2018: C1153. */
9743 case_value2 = (int) mpz_get_si (d->low->value.integer);
9744 if ((case_value == case_value2) && case_value == -1)
9745 gfc_error ("RANK (*) at %L is repeated at %L",
9746 &c->where, &d->where);
9747 else if (case_value == case_value2)
9748 gfc_error ("RANK (%i) at %L is repeated at %L",
9749 case_value, &c->where, &d->where);
9752 if (!c->low)
9753 continue;
9755 /* Check F2018: C1155. */
9756 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9757 || gfc_expr_attr (code->expr1).pointer))
9758 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9759 "allocatable selector at %L", &c->where, &code->expr1->where);
9761 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9762 || gfc_expr_attr (code->expr1).pointer))
9763 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9764 "allocatable selector at %L", &c->where, &code->expr1->where);
9767 /* Add EXEC_SELECT to switch on rank. */
9768 new_st = gfc_get_code (code->op);
9769 new_st->expr1 = code->expr1;
9770 new_st->expr2 = code->expr2;
9771 new_st->block = code->block;
9772 code->expr1 = code->expr2 = NULL;
9773 code->block = NULL;
9774 if (!ns->code)
9775 ns->code = new_st;
9776 else
9777 ns->code->next = new_st;
9778 code = new_st;
9779 code->op = EXEC_SELECT_RANK;
9781 selector_expr = code->expr1;
9783 /* Loop over SELECT RANK cases. */
9784 for (body = code->block; body; body = body->block)
9786 c = body->ext.block.case_list;
9787 int case_value;
9789 /* Pass on the default case. */
9790 if (c->low == NULL)
9791 continue;
9793 /* Associate temporary to selector. This should only be done
9794 when this case is actually true, so build a new ASSOCIATE
9795 that does precisely this here (instead of using the
9796 'global' one). */
9797 if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
9798 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9799 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9801 if (c->ts.type == BT_CLASS)
9802 sprintf (tname, "class_%s", c->ts.u.derived->name);
9803 else if (c->ts.type == BT_DERIVED)
9804 sprintf (tname, "type_%s", c->ts.u.derived->name);
9805 else if (c->ts.type != BT_CHARACTER)
9806 sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
9807 else
9808 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9809 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9811 case_value = (int) mpz_get_si (c->low->value.integer);
9812 if (case_value >= 0)
9813 sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
9814 else
9815 sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
9817 st = gfc_find_symtree (ns->sym_root, name);
9818 gcc_assert (st->n.sym->assoc);
9820 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9821 st->n.sym->assoc->target->where = selector_expr->where;
9823 new_st = gfc_get_code (EXEC_BLOCK);
9824 new_st->ext.block.ns = gfc_build_block_ns (ns);
9825 new_st->ext.block.ns->code = body->next;
9826 body->next = new_st;
9828 /* Chain in the new list only if it is marked as dangling. Otherwise
9829 there is a CASE label overlap and this is already used. Just ignore,
9830 the error is diagnosed elsewhere. */
9831 if (st->n.sym->assoc->dangling)
9833 new_st->ext.block.assoc = st->n.sym->assoc;
9834 st->n.sym->assoc->dangling = 0;
9837 resolve_assoc_var (st->n.sym, false);
9840 gfc_current_ns = ns;
9841 gfc_resolve_blocks (code->block, gfc_current_ns);
9842 gfc_current_ns = old_ns;
9846 /* Resolve a transfer statement. This is making sure that:
9847 -- a derived type being transferred has only non-pointer components
9848 -- a derived type being transferred doesn't have private components, unless
9849 it's being transferred from the module where the type was defined
9850 -- we're not trying to transfer a whole assumed size array. */
9852 static void
9853 resolve_transfer (gfc_code *code)
9855 gfc_symbol *sym, *derived;
9856 gfc_ref *ref;
9857 gfc_expr *exp;
9858 bool write = false;
9859 bool formatted = false;
9860 gfc_dt *dt = code->ext.dt;
9861 gfc_symbol *dtio_sub = NULL;
9863 exp = code->expr1;
9865 while (exp != NULL && exp->expr_type == EXPR_OP
9866 && exp->value.op.op == INTRINSIC_PARENTHESES)
9867 exp = exp->value.op.op1;
9869 if (exp && exp->expr_type == EXPR_NULL
9870 && code->ext.dt)
9872 gfc_error ("Invalid context for NULL () intrinsic at %L",
9873 &exp->where);
9874 return;
9877 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
9878 && exp->expr_type != EXPR_FUNCTION
9879 && exp->expr_type != EXPR_STRUCTURE))
9880 return;
9882 /* If we are reading, the variable will be changed. Note that
9883 code->ext.dt may be NULL if the TRANSFER is related to
9884 an INQUIRE statement -- but in this case, we are not reading, either. */
9885 if (dt && dt->dt_io_kind->value.iokind == M_READ
9886 && !gfc_check_vardef_context (exp, false, false, false,
9887 _("item in READ")))
9888 return;
9890 const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
9891 || exp->expr_type == EXPR_FUNCTION
9892 ? &exp->ts : &exp->symtree->n.sym->ts;
9894 /* Go to actual component transferred. */
9895 for (ref = exp->ref; ref; ref = ref->next)
9896 if (ref->type == REF_COMPONENT)
9897 ts = &ref->u.c.component->ts;
9899 if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
9900 && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
9902 derived = ts->u.derived;
9904 /* Determine when to use the formatted DTIO procedure. */
9905 if (dt && (dt->format_expr || dt->format_label))
9906 formatted = true;
9908 write = dt->dt_io_kind->value.iokind == M_WRITE
9909 || dt->dt_io_kind->value.iokind == M_PRINT;
9910 dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
9912 if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
9914 dt->udtio = exp;
9915 sym = exp->symtree->n.sym->ns->proc_name;
9916 /* Check to see if this is a nested DTIO call, with the
9917 dummy as the io-list object. */
9918 if (sym && sym == dtio_sub && sym->formal
9919 && sym->formal->sym == exp->symtree->n.sym
9920 && exp->ref == NULL)
9922 if (!sym->attr.recursive)
9924 gfc_error ("DTIO %s procedure at %L must be recursive",
9925 sym->name, &sym->declared_at);
9926 return;
9932 if (ts->type == BT_CLASS && dtio_sub == NULL)
9934 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
9935 "it is processed by a defined input/output procedure",
9936 &code->loc);
9937 return;
9940 if (ts->type == BT_DERIVED)
9942 /* Check that transferred derived type doesn't contain POINTER
9943 components unless it is processed by a defined input/output
9944 procedure". */
9945 if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
9947 gfc_error ("Data transfer element at %L cannot have POINTER "
9948 "components unless it is processed by a defined "
9949 "input/output procedure", &code->loc);
9950 return;
9953 /* F08:C935. */
9954 if (ts->u.derived->attr.proc_pointer_comp)
9956 gfc_error ("Data transfer element at %L cannot have "
9957 "procedure pointer components", &code->loc);
9958 return;
9961 if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
9963 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
9964 "components unless it is processed by a defined "
9965 "input/output procedure", &code->loc);
9966 return;
9969 /* C_PTR and C_FUNPTR have private components which means they cannot
9970 be printed. However, if -std=gnu and not -pedantic, allow
9971 the component to be printed to help debugging. */
9972 if (ts->u.derived->ts.f90_type == BT_VOID)
9974 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
9975 "cannot have PRIVATE components", &code->loc))
9976 return;
9978 else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
9980 gfc_error ("Data transfer element at %L cannot have "
9981 "PRIVATE components unless it is processed by "
9982 "a defined input/output procedure", &code->loc);
9983 return;
9987 if (exp->expr_type == EXPR_STRUCTURE)
9988 return;
9990 sym = exp->symtree->n.sym;
9992 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
9993 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
9995 gfc_error ("Data transfer element at %L cannot be a full reference to "
9996 "an assumed-size array", &code->loc);
9997 return;
10002 /*********** Toplevel code resolution subroutines ***********/
10004 /* Find the set of labels that are reachable from this block. We also
10005 record the last statement in each block. */
10007 static void
10008 find_reachable_labels (gfc_code *block)
10010 gfc_code *c;
10012 if (!block)
10013 return;
10015 cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
10017 /* Collect labels in this block. We don't keep those corresponding
10018 to END {IF|SELECT}, these are checked in resolve_branch by going
10019 up through the code_stack. */
10020 for (c = block; c; c = c->next)
10022 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
10023 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
10026 /* Merge with labels from parent block. */
10027 if (cs_base->prev)
10029 gcc_assert (cs_base->prev->reachable_labels);
10030 bitmap_ior_into (cs_base->reachable_labels,
10031 cs_base->prev->reachable_labels);
10036 static void
10037 resolve_lock_unlock_event (gfc_code *code)
10039 if (code->expr1->expr_type == EXPR_FUNCTION
10040 && code->expr1->value.function.isym
10041 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10042 remove_caf_get_intrinsic (code->expr1);
10044 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
10045 && (code->expr1->ts.type != BT_DERIVED
10046 || code->expr1->expr_type != EXPR_VARIABLE
10047 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
10048 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
10049 || code->expr1->rank != 0
10050 || (!gfc_is_coarray (code->expr1) &&
10051 !gfc_is_coindexed (code->expr1))))
10052 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
10053 &code->expr1->where);
10054 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
10055 && (code->expr1->ts.type != BT_DERIVED
10056 || code->expr1->expr_type != EXPR_VARIABLE
10057 || code->expr1->ts.u.derived->from_intmod
10058 != INTMOD_ISO_FORTRAN_ENV
10059 || code->expr1->ts.u.derived->intmod_sym_id
10060 != ISOFORTRAN_EVENT_TYPE
10061 || code->expr1->rank != 0))
10062 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
10063 &code->expr1->where);
10064 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
10065 && !gfc_is_coindexed (code->expr1))
10066 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
10067 &code->expr1->where);
10068 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
10069 gfc_error ("Event variable argument at %L must be a coarray but not "
10070 "coindexed", &code->expr1->where);
10072 /* Check STAT. */
10073 if (code->expr2
10074 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10075 || code->expr2->expr_type != EXPR_VARIABLE))
10076 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10077 &code->expr2->where);
10079 if (code->expr2
10080 && !gfc_check_vardef_context (code->expr2, false, false, false,
10081 _("STAT variable")))
10082 return;
10084 /* Check ERRMSG. */
10085 if (code->expr3
10086 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10087 || code->expr3->expr_type != EXPR_VARIABLE))
10088 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10089 &code->expr3->where);
10091 if (code->expr3
10092 && !gfc_check_vardef_context (code->expr3, false, false, false,
10093 _("ERRMSG variable")))
10094 return;
10096 /* Check for LOCK the ACQUIRED_LOCK. */
10097 if (code->op != EXEC_EVENT_WAIT && code->expr4
10098 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
10099 || code->expr4->expr_type != EXPR_VARIABLE))
10100 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
10101 "variable", &code->expr4->where);
10103 if (code->op != EXEC_EVENT_WAIT && code->expr4
10104 && !gfc_check_vardef_context (code->expr4, false, false, false,
10105 _("ACQUIRED_LOCK variable")))
10106 return;
10108 /* Check for EVENT WAIT the UNTIL_COUNT. */
10109 if (code->op == EXEC_EVENT_WAIT && code->expr4)
10111 if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
10112 || code->expr4->rank != 0)
10113 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
10114 "expression", &code->expr4->where);
10119 static void
10120 resolve_critical (gfc_code *code)
10122 gfc_symtree *symtree;
10123 gfc_symbol *lock_type;
10124 char name[GFC_MAX_SYMBOL_LEN];
10125 static int serial = 0;
10127 if (flag_coarray != GFC_FCOARRAY_LIB)
10128 return;
10130 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10131 GFC_PREFIX ("lock_type"));
10132 if (symtree)
10133 lock_type = symtree->n.sym;
10134 else
10136 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
10137 false) != 0)
10138 gcc_unreachable ();
10139 lock_type = symtree->n.sym;
10140 lock_type->attr.flavor = FL_DERIVED;
10141 lock_type->attr.zero_comp = 1;
10142 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
10143 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
10146 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
10147 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
10148 gcc_unreachable ();
10150 code->resolved_sym = symtree->n.sym;
10151 symtree->n.sym->attr.flavor = FL_VARIABLE;
10152 symtree->n.sym->attr.referenced = 1;
10153 symtree->n.sym->attr.artificial = 1;
10154 symtree->n.sym->attr.codimension = 1;
10155 symtree->n.sym->ts.type = BT_DERIVED;
10156 symtree->n.sym->ts.u.derived = lock_type;
10157 symtree->n.sym->as = gfc_get_array_spec ();
10158 symtree->n.sym->as->corank = 1;
10159 symtree->n.sym->as->type = AS_EXPLICIT;
10160 symtree->n.sym->as->cotype = AS_EXPLICIT;
10161 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
10162 NULL, 1);
10163 gfc_commit_symbols();
10167 static void
10168 resolve_sync (gfc_code *code)
10170 /* Check imageset. The * case matches expr1 == NULL. */
10171 if (code->expr1)
10173 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
10174 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
10175 "INTEGER expression", &code->expr1->where);
10176 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
10177 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
10178 gfc_error ("Imageset argument at %L must between 1 and num_images()",
10179 &code->expr1->where);
10180 else if (code->expr1->expr_type == EXPR_ARRAY
10181 && gfc_simplify_expr (code->expr1, 0))
10183 gfc_constructor *cons;
10184 cons = gfc_constructor_first (code->expr1->value.constructor);
10185 for (; cons; cons = gfc_constructor_next (cons))
10186 if (cons->expr->expr_type == EXPR_CONSTANT
10187 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
10188 gfc_error ("Imageset argument at %L must between 1 and "
10189 "num_images()", &cons->expr->where);
10193 /* Check STAT. */
10194 gfc_resolve_expr (code->expr2);
10195 if (code->expr2
10196 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10197 || code->expr2->expr_type != EXPR_VARIABLE))
10198 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10199 &code->expr2->where);
10201 /* Check ERRMSG. */
10202 gfc_resolve_expr (code->expr3);
10203 if (code->expr3
10204 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10205 || code->expr3->expr_type != EXPR_VARIABLE))
10206 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10207 &code->expr3->where);
10211 /* Given a branch to a label, see if the branch is conforming.
10212 The code node describes where the branch is located. */
10214 static void
10215 resolve_branch (gfc_st_label *label, gfc_code *code)
10217 code_stack *stack;
10219 if (label == NULL)
10220 return;
10222 /* Step one: is this a valid branching target? */
10224 if (label->defined == ST_LABEL_UNKNOWN)
10226 gfc_error ("Label %d referenced at %L is never defined", label->value,
10227 &code->loc);
10228 return;
10231 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
10233 gfc_error ("Statement at %L is not a valid branch target statement "
10234 "for the branch statement at %L", &label->where, &code->loc);
10235 return;
10238 /* Step two: make sure this branch is not a branch to itself ;-) */
10240 if (code->here == label)
10242 gfc_warning (0,
10243 "Branch at %L may result in an infinite loop", &code->loc);
10244 return;
10247 /* Step three: See if the label is in the same block as the
10248 branching statement. The hard work has been done by setting up
10249 the bitmap reachable_labels. */
10251 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
10253 /* Check now whether there is a CRITICAL construct; if so, check
10254 whether the label is still visible outside of the CRITICAL block,
10255 which is invalid. */
10256 for (stack = cs_base; stack; stack = stack->prev)
10258 if (stack->current->op == EXEC_CRITICAL
10259 && bitmap_bit_p (stack->reachable_labels, label->value))
10260 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
10261 "label at %L", &code->loc, &label->where);
10262 else if (stack->current->op == EXEC_DO_CONCURRENT
10263 && bitmap_bit_p (stack->reachable_labels, label->value))
10264 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
10265 "for label at %L", &code->loc, &label->where);
10268 return;
10271 /* Step four: If we haven't found the label in the bitmap, it may
10272 still be the label of the END of the enclosing block, in which
10273 case we find it by going up the code_stack. */
10275 for (stack = cs_base; stack; stack = stack->prev)
10277 if (stack->current->next && stack->current->next->here == label)
10278 break;
10279 if (stack->current->op == EXEC_CRITICAL)
10281 /* Note: A label at END CRITICAL does not leave the CRITICAL
10282 construct as END CRITICAL is still part of it. */
10283 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
10284 " at %L", &code->loc, &label->where);
10285 return;
10287 else if (stack->current->op == EXEC_DO_CONCURRENT)
10289 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
10290 "label at %L", &code->loc, &label->where);
10291 return;
10295 if (stack)
10297 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
10298 return;
10301 /* The label is not in an enclosing block, so illegal. This was
10302 allowed in Fortran 66, so we allow it as extension. No
10303 further checks are necessary in this case. */
10304 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
10305 "as the GOTO statement at %L", &label->where,
10306 &code->loc);
10307 return;
10311 /* Check whether EXPR1 has the same shape as EXPR2. */
10313 static bool
10314 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
10316 mpz_t shape[GFC_MAX_DIMENSIONS];
10317 mpz_t shape2[GFC_MAX_DIMENSIONS];
10318 bool result = false;
10319 int i;
10321 /* Compare the rank. */
10322 if (expr1->rank != expr2->rank)
10323 return result;
10325 /* Compare the size of each dimension. */
10326 for (i=0; i<expr1->rank; i++)
10328 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
10329 goto ignore;
10331 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
10332 goto ignore;
10334 if (mpz_cmp (shape[i], shape2[i]))
10335 goto over;
10338 /* When either of the two expression is an assumed size array, we
10339 ignore the comparison of dimension sizes. */
10340 ignore:
10341 result = true;
10343 over:
10344 gfc_clear_shape (shape, i);
10345 gfc_clear_shape (shape2, i);
10346 return result;
10350 /* Check whether a WHERE assignment target or a WHERE mask expression
10351 has the same shape as the outmost WHERE mask expression. */
10353 static void
10354 resolve_where (gfc_code *code, gfc_expr *mask)
10356 gfc_code *cblock;
10357 gfc_code *cnext;
10358 gfc_expr *e = NULL;
10360 cblock = code->block;
10362 /* Store the first WHERE mask-expr of the WHERE statement or construct.
10363 In case of nested WHERE, only the outmost one is stored. */
10364 if (mask == NULL) /* outmost WHERE */
10365 e = cblock->expr1;
10366 else /* inner WHERE */
10367 e = mask;
10369 while (cblock)
10371 if (cblock->expr1)
10373 /* Check if the mask-expr has a consistent shape with the
10374 outmost WHERE mask-expr. */
10375 if (!resolve_where_shape (cblock->expr1, e))
10376 gfc_error ("WHERE mask at %L has inconsistent shape",
10377 &cblock->expr1->where);
10380 /* the assignment statement of a WHERE statement, or the first
10381 statement in where-body-construct of a WHERE construct */
10382 cnext = cblock->next;
10383 while (cnext)
10385 switch (cnext->op)
10387 /* WHERE assignment statement */
10388 case EXEC_ASSIGN:
10390 /* Check shape consistent for WHERE assignment target. */
10391 if (e && !resolve_where_shape (cnext->expr1, e))
10392 gfc_error ("WHERE assignment target at %L has "
10393 "inconsistent shape", &cnext->expr1->where);
10394 break;
10397 case EXEC_ASSIGN_CALL:
10398 resolve_call (cnext);
10399 if (!cnext->resolved_sym->attr.elemental)
10400 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10401 &cnext->ext.actual->expr->where);
10402 break;
10404 /* WHERE or WHERE construct is part of a where-body-construct */
10405 case EXEC_WHERE:
10406 resolve_where (cnext, e);
10407 break;
10409 default:
10410 gfc_error ("Unsupported statement inside WHERE at %L",
10411 &cnext->loc);
10413 /* the next statement within the same where-body-construct */
10414 cnext = cnext->next;
10416 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10417 cblock = cblock->block;
10422 /* Resolve assignment in FORALL construct.
10423 NVAR is the number of FORALL index variables, and VAR_EXPR records the
10424 FORALL index variables. */
10426 static void
10427 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
10429 int n;
10431 for (n = 0; n < nvar; n++)
10433 gfc_symbol *forall_index;
10435 forall_index = var_expr[n]->symtree->n.sym;
10437 /* Check whether the assignment target is one of the FORALL index
10438 variable. */
10439 if ((code->expr1->expr_type == EXPR_VARIABLE)
10440 && (code->expr1->symtree->n.sym == forall_index))
10441 gfc_error ("Assignment to a FORALL index variable at %L",
10442 &code->expr1->where);
10443 else
10445 /* If one of the FORALL index variables doesn't appear in the
10446 assignment variable, then there could be a many-to-one
10447 assignment. Emit a warning rather than an error because the
10448 mask could be resolving this problem. */
10449 if (!find_forall_index (code->expr1, forall_index, 0))
10450 gfc_warning (0, "The FORALL with index %qs is not used on the "
10451 "left side of the assignment at %L and so might "
10452 "cause multiple assignment to this object",
10453 var_expr[n]->symtree->name, &code->expr1->where);
10459 /* Resolve WHERE statement in FORALL construct. */
10461 static void
10462 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
10463 gfc_expr **var_expr)
10465 gfc_code *cblock;
10466 gfc_code *cnext;
10468 cblock = code->block;
10469 while (cblock)
10471 /* the assignment statement of a WHERE statement, or the first
10472 statement in where-body-construct of a WHERE construct */
10473 cnext = cblock->next;
10474 while (cnext)
10476 switch (cnext->op)
10478 /* WHERE assignment statement */
10479 case EXEC_ASSIGN:
10480 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
10481 break;
10483 /* WHERE operator assignment statement */
10484 case EXEC_ASSIGN_CALL:
10485 resolve_call (cnext);
10486 if (!cnext->resolved_sym->attr.elemental)
10487 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10488 &cnext->ext.actual->expr->where);
10489 break;
10491 /* WHERE or WHERE construct is part of a where-body-construct */
10492 case EXEC_WHERE:
10493 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
10494 break;
10496 default:
10497 gfc_error ("Unsupported statement inside WHERE at %L",
10498 &cnext->loc);
10500 /* the next statement within the same where-body-construct */
10501 cnext = cnext->next;
10503 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10504 cblock = cblock->block;
10509 /* Traverse the FORALL body to check whether the following errors exist:
10510 1. For assignment, check if a many-to-one assignment happens.
10511 2. For WHERE statement, check the WHERE body to see if there is any
10512 many-to-one assignment. */
10514 static void
10515 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
10517 gfc_code *c;
10519 c = code->block->next;
10520 while (c)
10522 switch (c->op)
10524 case EXEC_ASSIGN:
10525 case EXEC_POINTER_ASSIGN:
10526 gfc_resolve_assign_in_forall (c, nvar, var_expr);
10527 break;
10529 case EXEC_ASSIGN_CALL:
10530 resolve_call (c);
10531 break;
10533 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
10534 there is no need to handle it here. */
10535 case EXEC_FORALL:
10536 break;
10537 case EXEC_WHERE:
10538 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
10539 break;
10540 default:
10541 break;
10543 /* The next statement in the FORALL body. */
10544 c = c->next;
10549 /* Counts the number of iterators needed inside a forall construct, including
10550 nested forall constructs. This is used to allocate the needed memory
10551 in gfc_resolve_forall. */
10553 static int
10554 gfc_count_forall_iterators (gfc_code *code)
10556 int max_iters, sub_iters, current_iters;
10557 gfc_forall_iterator *fa;
10559 gcc_assert(code->op == EXEC_FORALL);
10560 max_iters = 0;
10561 current_iters = 0;
10563 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10564 current_iters ++;
10566 code = code->block->next;
10568 while (code)
10570 if (code->op == EXEC_FORALL)
10572 sub_iters = gfc_count_forall_iterators (code);
10573 if (sub_iters > max_iters)
10574 max_iters = sub_iters;
10576 code = code->next;
10579 return current_iters + max_iters;
10583 /* Given a FORALL construct, first resolve the FORALL iterator, then call
10584 gfc_resolve_forall_body to resolve the FORALL body. */
10586 static void
10587 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
10589 static gfc_expr **var_expr;
10590 static int total_var = 0;
10591 static int nvar = 0;
10592 int i, old_nvar, tmp;
10593 gfc_forall_iterator *fa;
10595 old_nvar = nvar;
10597 if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
10598 return;
10600 /* Start to resolve a FORALL construct */
10601 if (forall_save == 0)
10603 /* Count the total number of FORALL indices in the nested FORALL
10604 construct in order to allocate the VAR_EXPR with proper size. */
10605 total_var = gfc_count_forall_iterators (code);
10607 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
10608 var_expr = XCNEWVEC (gfc_expr *, total_var);
10611 /* The information about FORALL iterator, including FORALL indices start, end
10612 and stride. An outer FORALL indice cannot appear in start, end or stride. */
10613 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10615 /* Fortran 20008: C738 (R753). */
10616 if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
10618 gfc_error ("FORALL index-name at %L must be a scalar variable "
10619 "of type integer", &fa->var->where);
10620 continue;
10623 /* Check if any outer FORALL index name is the same as the current
10624 one. */
10625 for (i = 0; i < nvar; i++)
10627 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
10628 gfc_error ("An outer FORALL construct already has an index "
10629 "with this name %L", &fa->var->where);
10632 /* Record the current FORALL index. */
10633 var_expr[nvar] = gfc_copy_expr (fa->var);
10635 nvar++;
10637 /* No memory leak. */
10638 gcc_assert (nvar <= total_var);
10641 /* Resolve the FORALL body. */
10642 gfc_resolve_forall_body (code, nvar, var_expr);
10644 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
10645 gfc_resolve_blocks (code->block, ns);
10647 tmp = nvar;
10648 nvar = old_nvar;
10649 /* Free only the VAR_EXPRs allocated in this frame. */
10650 for (i = nvar; i < tmp; i++)
10651 gfc_free_expr (var_expr[i]);
10653 if (nvar == 0)
10655 /* We are in the outermost FORALL construct. */
10656 gcc_assert (forall_save == 0);
10658 /* VAR_EXPR is not needed any more. */
10659 free (var_expr);
10660 total_var = 0;
10665 /* Resolve a BLOCK construct statement. */
10667 static void
10668 resolve_block_construct (gfc_code* code)
10670 /* Resolve the BLOCK's namespace. */
10671 gfc_resolve (code->ext.block.ns);
10673 /* For an ASSOCIATE block, the associations (and their targets) are already
10674 resolved during resolve_symbol. */
10678 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
10679 DO code nodes. */
10681 void
10682 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
10684 bool t;
10686 for (; b; b = b->block)
10688 t = gfc_resolve_expr (b->expr1);
10689 if (!gfc_resolve_expr (b->expr2))
10690 t = false;
10692 switch (b->op)
10694 case EXEC_IF:
10695 if (t && b->expr1 != NULL
10696 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
10697 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10698 &b->expr1->where);
10699 break;
10701 case EXEC_WHERE:
10702 if (t
10703 && b->expr1 != NULL
10704 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
10705 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
10706 &b->expr1->where);
10707 break;
10709 case EXEC_GOTO:
10710 resolve_branch (b->label1, b);
10711 break;
10713 case EXEC_BLOCK:
10714 resolve_block_construct (b);
10715 break;
10717 case EXEC_SELECT:
10718 case EXEC_SELECT_TYPE:
10719 case EXEC_SELECT_RANK:
10720 case EXEC_FORALL:
10721 case EXEC_DO:
10722 case EXEC_DO_WHILE:
10723 case EXEC_DO_CONCURRENT:
10724 case EXEC_CRITICAL:
10725 case EXEC_READ:
10726 case EXEC_WRITE:
10727 case EXEC_IOLENGTH:
10728 case EXEC_WAIT:
10729 break;
10731 case EXEC_OMP_ATOMIC:
10732 case EXEC_OACC_ATOMIC:
10734 gfc_omp_atomic_op aop
10735 = (gfc_omp_atomic_op) (b->ext.omp_atomic & GFC_OMP_ATOMIC_MASK);
10737 /* Verify this before calling gfc_resolve_code, which might
10738 change it. */
10739 gcc_assert (b->next && b->next->op == EXEC_ASSIGN);
10740 gcc_assert (((aop != GFC_OMP_ATOMIC_CAPTURE)
10741 && b->next->next == NULL)
10742 || ((aop == GFC_OMP_ATOMIC_CAPTURE)
10743 && b->next->next != NULL
10744 && b->next->next->op == EXEC_ASSIGN
10745 && b->next->next->next == NULL));
10747 break;
10749 case EXEC_OACC_PARALLEL_LOOP:
10750 case EXEC_OACC_PARALLEL:
10751 case EXEC_OACC_KERNELS_LOOP:
10752 case EXEC_OACC_KERNELS:
10753 case EXEC_OACC_SERIAL_LOOP:
10754 case EXEC_OACC_SERIAL:
10755 case EXEC_OACC_DATA:
10756 case EXEC_OACC_HOST_DATA:
10757 case EXEC_OACC_LOOP:
10758 case EXEC_OACC_UPDATE:
10759 case EXEC_OACC_WAIT:
10760 case EXEC_OACC_CACHE:
10761 case EXEC_OACC_ENTER_DATA:
10762 case EXEC_OACC_EXIT_DATA:
10763 case EXEC_OACC_ROUTINE:
10764 case EXEC_OMP_CRITICAL:
10765 case EXEC_OMP_DISTRIBUTE:
10766 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10767 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10768 case EXEC_OMP_DISTRIBUTE_SIMD:
10769 case EXEC_OMP_DO:
10770 case EXEC_OMP_DO_SIMD:
10771 case EXEC_OMP_MASTER:
10772 case EXEC_OMP_ORDERED:
10773 case EXEC_OMP_PARALLEL:
10774 case EXEC_OMP_PARALLEL_DO:
10775 case EXEC_OMP_PARALLEL_DO_SIMD:
10776 case EXEC_OMP_PARALLEL_SECTIONS:
10777 case EXEC_OMP_PARALLEL_WORKSHARE:
10778 case EXEC_OMP_SECTIONS:
10779 case EXEC_OMP_SIMD:
10780 case EXEC_OMP_SINGLE:
10781 case EXEC_OMP_TARGET:
10782 case EXEC_OMP_TARGET_DATA:
10783 case EXEC_OMP_TARGET_ENTER_DATA:
10784 case EXEC_OMP_TARGET_EXIT_DATA:
10785 case EXEC_OMP_TARGET_PARALLEL:
10786 case EXEC_OMP_TARGET_PARALLEL_DO:
10787 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
10788 case EXEC_OMP_TARGET_SIMD:
10789 case EXEC_OMP_TARGET_TEAMS:
10790 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10791 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10792 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10793 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10794 case EXEC_OMP_TARGET_UPDATE:
10795 case EXEC_OMP_TASK:
10796 case EXEC_OMP_TASKGROUP:
10797 case EXEC_OMP_TASKLOOP:
10798 case EXEC_OMP_TASKLOOP_SIMD:
10799 case EXEC_OMP_TASKWAIT:
10800 case EXEC_OMP_TASKYIELD:
10801 case EXEC_OMP_TEAMS:
10802 case EXEC_OMP_TEAMS_DISTRIBUTE:
10803 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10804 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10805 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10806 case EXEC_OMP_WORKSHARE:
10807 break;
10809 default:
10810 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
10813 gfc_resolve_code (b->next, ns);
10818 /* Does everything to resolve an ordinary assignment. Returns true
10819 if this is an interface assignment. */
10820 static bool
10821 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
10823 bool rval = false;
10824 gfc_expr *lhs;
10825 gfc_expr *rhs;
10826 int n;
10827 gfc_ref *ref;
10828 symbol_attribute attr;
10830 if (gfc_extend_assign (code, ns))
10832 gfc_expr** rhsptr;
10834 if (code->op == EXEC_ASSIGN_CALL)
10836 lhs = code->ext.actual->expr;
10837 rhsptr = &code->ext.actual->next->expr;
10839 else
10841 gfc_actual_arglist* args;
10842 gfc_typebound_proc* tbp;
10844 gcc_assert (code->op == EXEC_COMPCALL);
10846 args = code->expr1->value.compcall.actual;
10847 lhs = args->expr;
10848 rhsptr = &args->next->expr;
10850 tbp = code->expr1->value.compcall.tbp;
10851 gcc_assert (!tbp->is_generic);
10854 /* Make a temporary rhs when there is a default initializer
10855 and rhs is the same symbol as the lhs. */
10856 if ((*rhsptr)->expr_type == EXPR_VARIABLE
10857 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
10858 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
10859 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
10860 *rhsptr = gfc_get_parentheses (*rhsptr);
10862 return true;
10865 lhs = code->expr1;
10866 rhs = code->expr2;
10868 if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
10869 && rhs->ts.type == BT_CHARACTER
10870 && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
10872 /* Use of -fdec-char-conversions allows assignment of character data
10873 to non-character variables. This not permited for nonconstant
10874 strings. */
10875 gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
10876 gfc_typename (lhs), &rhs->where);
10877 return false;
10880 /* Handle the case of a BOZ literal on the RHS. */
10881 if (rhs->ts.type == BT_BOZ)
10883 if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
10884 "statement value nor an actual argument of "
10885 "INT/REAL/DBLE/CMPLX intrinsic subprogram",
10886 &rhs->where))
10887 return false;
10889 switch (lhs->ts.type)
10891 case BT_INTEGER:
10892 if (!gfc_boz2int (rhs, lhs->ts.kind))
10893 return false;
10894 break;
10895 case BT_REAL:
10896 if (!gfc_boz2real (rhs, lhs->ts.kind))
10897 return false;
10898 break;
10899 default:
10900 gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
10901 return false;
10905 if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
10907 HOST_WIDE_INT llen = 0, rlen = 0;
10908 if (lhs->ts.u.cl != NULL
10909 && lhs->ts.u.cl->length != NULL
10910 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10911 llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
10913 if (rhs->expr_type == EXPR_CONSTANT)
10914 rlen = rhs->value.character.length;
10916 else if (rhs->ts.u.cl != NULL
10917 && rhs->ts.u.cl->length != NULL
10918 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10919 rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
10921 if (rlen && llen && rlen > llen)
10922 gfc_warning_now (OPT_Wcharacter_truncation,
10923 "CHARACTER expression will be truncated "
10924 "in assignment (%ld/%ld) at %L",
10925 (long) llen, (long) rlen, &code->loc);
10928 /* Ensure that a vector index expression for the lvalue is evaluated
10929 to a temporary if the lvalue symbol is referenced in it. */
10930 if (lhs->rank)
10932 for (ref = lhs->ref; ref; ref= ref->next)
10933 if (ref->type == REF_ARRAY)
10935 for (n = 0; n < ref->u.ar.dimen; n++)
10936 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
10937 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
10938 ref->u.ar.start[n]))
10939 ref->u.ar.start[n]
10940 = gfc_get_parentheses (ref->u.ar.start[n]);
10944 if (gfc_pure (NULL))
10946 if (lhs->ts.type == BT_DERIVED
10947 && lhs->expr_type == EXPR_VARIABLE
10948 && lhs->ts.u.derived->attr.pointer_comp
10949 && rhs->expr_type == EXPR_VARIABLE
10950 && (gfc_impure_variable (rhs->symtree->n.sym)
10951 || gfc_is_coindexed (rhs)))
10953 /* F2008, C1283. */
10954 if (gfc_is_coindexed (rhs))
10955 gfc_error ("Coindexed expression at %L is assigned to "
10956 "a derived type variable with a POINTER "
10957 "component in a PURE procedure",
10958 &rhs->where);
10959 else
10960 /* F2008, C1283 (4). */
10961 gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
10962 "shall not be used as the expr at %L of an intrinsic "
10963 "assignment statement in which the variable is of a "
10964 "derived type if the derived type has a pointer "
10965 "component at any level of component selection.",
10966 &rhs->where);
10967 return rval;
10970 /* Fortran 2008, C1283. */
10971 if (gfc_is_coindexed (lhs))
10973 gfc_error ("Assignment to coindexed variable at %L in a PURE "
10974 "procedure", &rhs->where);
10975 return rval;
10979 if (gfc_implicit_pure (NULL))
10981 if (lhs->expr_type == EXPR_VARIABLE
10982 && lhs->symtree->n.sym != gfc_current_ns->proc_name
10983 && lhs->symtree->n.sym->ns != gfc_current_ns)
10984 gfc_unset_implicit_pure (NULL);
10986 if (lhs->ts.type == BT_DERIVED
10987 && lhs->expr_type == EXPR_VARIABLE
10988 && lhs->ts.u.derived->attr.pointer_comp
10989 && rhs->expr_type == EXPR_VARIABLE
10990 && (gfc_impure_variable (rhs->symtree->n.sym)
10991 || gfc_is_coindexed (rhs)))
10992 gfc_unset_implicit_pure (NULL);
10994 /* Fortran 2008, C1283. */
10995 if (gfc_is_coindexed (lhs))
10996 gfc_unset_implicit_pure (NULL);
10999 /* F2008, 7.2.1.2. */
11000 attr = gfc_expr_attr (lhs);
11001 if (lhs->ts.type == BT_CLASS && attr.allocatable)
11003 if (attr.codimension)
11005 gfc_error ("Assignment to polymorphic coarray at %L is not "
11006 "permitted", &lhs->where);
11007 return false;
11009 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
11010 "polymorphic variable at %L", &lhs->where))
11011 return false;
11012 if (!flag_realloc_lhs)
11014 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
11015 "requires %<-frealloc-lhs%>", &lhs->where);
11016 return false;
11019 else if (lhs->ts.type == BT_CLASS)
11021 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
11022 "assignment at %L - check that there is a matching specific "
11023 "subroutine for '=' operator", &lhs->where);
11024 return false;
11027 bool lhs_coindexed = gfc_is_coindexed (lhs);
11029 /* F2008, Section 7.2.1.2. */
11030 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
11032 gfc_error ("Coindexed variable must not have an allocatable ultimate "
11033 "component in assignment at %L", &lhs->where);
11034 return false;
11037 /* Assign the 'data' of a class object to a derived type. */
11038 if (lhs->ts.type == BT_DERIVED
11039 && rhs->ts.type == BT_CLASS
11040 && rhs->expr_type != EXPR_ARRAY)
11041 gfc_add_data_component (rhs);
11043 /* Make sure there is a vtable and, in particular, a _copy for the
11044 rhs type. */
11045 if (UNLIMITED_POLY (lhs) && lhs->rank && rhs->ts.type != BT_CLASS)
11046 gfc_find_vtab (&rhs->ts);
11048 bool caf_convert_to_send = flag_coarray == GFC_FCOARRAY_LIB
11049 && (lhs_coindexed
11050 || (code->expr2->expr_type == EXPR_FUNCTION
11051 && code->expr2->value.function.isym
11052 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
11053 && (code->expr1->rank == 0 || code->expr2->rank != 0)
11054 && !gfc_expr_attr (rhs).allocatable
11055 && !gfc_has_vector_subscript (rhs)));
11057 gfc_check_assign (lhs, rhs, 1, !caf_convert_to_send);
11059 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
11060 Additionally, insert this code when the RHS is a CAF as we then use the
11061 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
11062 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
11063 noncoindexed array and the RHS is a coindexed scalar, use the normal code
11064 path. */
11065 if (caf_convert_to_send)
11067 if (code->expr2->expr_type == EXPR_FUNCTION
11068 && code->expr2->value.function.isym
11069 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
11070 remove_caf_get_intrinsic (code->expr2);
11071 code->op = EXEC_CALL;
11072 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
11073 code->resolved_sym = code->symtree->n.sym;
11074 code->resolved_sym->attr.flavor = FL_PROCEDURE;
11075 code->resolved_sym->attr.intrinsic = 1;
11076 code->resolved_sym->attr.subroutine = 1;
11077 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
11078 gfc_commit_symbol (code->resolved_sym);
11079 code->ext.actual = gfc_get_actual_arglist ();
11080 code->ext.actual->expr = lhs;
11081 code->ext.actual->next = gfc_get_actual_arglist ();
11082 code->ext.actual->next->expr = rhs;
11083 code->expr1 = NULL;
11084 code->expr2 = NULL;
11087 return false;
11091 /* Add a component reference onto an expression. */
11093 static void
11094 add_comp_ref (gfc_expr *e, gfc_component *c)
11096 gfc_ref **ref;
11097 ref = &(e->ref);
11098 while (*ref)
11099 ref = &((*ref)->next);
11100 *ref = gfc_get_ref ();
11101 (*ref)->type = REF_COMPONENT;
11102 (*ref)->u.c.sym = e->ts.u.derived;
11103 (*ref)->u.c.component = c;
11104 e->ts = c->ts;
11106 /* Add a full array ref, as necessary. */
11107 if (c->as)
11109 gfc_add_full_array_ref (e, c->as);
11110 e->rank = c->as->rank;
11115 /* Build an assignment. Keep the argument 'op' for future use, so that
11116 pointer assignments can be made. */
11118 static gfc_code *
11119 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
11120 gfc_component *comp1, gfc_component *comp2, locus loc)
11122 gfc_code *this_code;
11124 this_code = gfc_get_code (op);
11125 this_code->next = NULL;
11126 this_code->expr1 = gfc_copy_expr (expr1);
11127 this_code->expr2 = gfc_copy_expr (expr2);
11128 this_code->loc = loc;
11129 if (comp1 && comp2)
11131 add_comp_ref (this_code->expr1, comp1);
11132 add_comp_ref (this_code->expr2, comp2);
11135 return this_code;
11139 /* Makes a temporary variable expression based on the characteristics of
11140 a given variable expression. */
11142 static gfc_expr*
11143 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
11145 static int serial = 0;
11146 char name[GFC_MAX_SYMBOL_LEN];
11147 gfc_symtree *tmp;
11148 gfc_array_spec *as;
11149 gfc_array_ref *aref;
11150 gfc_ref *ref;
11152 sprintf (name, GFC_PREFIX("DA%d"), serial++);
11153 gfc_get_sym_tree (name, ns, &tmp, false);
11154 gfc_add_type (tmp->n.sym, &e->ts, NULL);
11156 if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
11157 tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
11158 NULL,
11159 e->value.character.length);
11161 as = NULL;
11162 ref = NULL;
11163 aref = NULL;
11165 /* Obtain the arrayspec for the temporary. */
11166 if (e->rank && e->expr_type != EXPR_ARRAY
11167 && e->expr_type != EXPR_FUNCTION
11168 && e->expr_type != EXPR_OP)
11170 aref = gfc_find_array_ref (e);
11171 if (e->expr_type == EXPR_VARIABLE
11172 && e->symtree->n.sym->as == aref->as)
11173 as = aref->as;
11174 else
11176 for (ref = e->ref; ref; ref = ref->next)
11177 if (ref->type == REF_COMPONENT
11178 && ref->u.c.component->as == aref->as)
11180 as = aref->as;
11181 break;
11186 /* Add the attributes and the arrayspec to the temporary. */
11187 tmp->n.sym->attr = gfc_expr_attr (e);
11188 tmp->n.sym->attr.function = 0;
11189 tmp->n.sym->attr.proc_pointer = 0;
11190 tmp->n.sym->attr.result = 0;
11191 tmp->n.sym->attr.flavor = FL_VARIABLE;
11192 tmp->n.sym->attr.dummy = 0;
11193 tmp->n.sym->attr.use_assoc = 0;
11194 tmp->n.sym->attr.intent = INTENT_UNKNOWN;
11196 if (as)
11198 tmp->n.sym->as = gfc_copy_array_spec (as);
11199 if (!ref)
11200 ref = e->ref;
11201 if (as->type == AS_DEFERRED)
11202 tmp->n.sym->attr.allocatable = 1;
11204 else if (e->rank && (e->expr_type == EXPR_ARRAY
11205 || e->expr_type == EXPR_FUNCTION
11206 || e->expr_type == EXPR_OP))
11208 tmp->n.sym->as = gfc_get_array_spec ();
11209 tmp->n.sym->as->type = AS_DEFERRED;
11210 tmp->n.sym->as->rank = e->rank;
11211 tmp->n.sym->attr.allocatable = 1;
11212 tmp->n.sym->attr.dimension = 1;
11214 else
11215 tmp->n.sym->attr.dimension = 0;
11217 gfc_set_sym_referenced (tmp->n.sym);
11218 gfc_commit_symbol (tmp->n.sym);
11219 e = gfc_lval_expr_from_sym (tmp->n.sym);
11221 /* Should the lhs be a section, use its array ref for the
11222 temporary expression. */
11223 if (aref && aref->type != AR_FULL)
11225 gfc_free_ref_list (e->ref);
11226 e->ref = gfc_copy_ref (ref);
11228 return e;
11232 /* Add one line of code to the code chain, making sure that 'head' and
11233 'tail' are appropriately updated. */
11235 static void
11236 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
11238 gcc_assert (this_code);
11239 if (*head == NULL)
11240 *head = *tail = *this_code;
11241 else
11242 *tail = gfc_append_code (*tail, *this_code);
11243 *this_code = NULL;
11247 /* Counts the potential number of part array references that would
11248 result from resolution of typebound defined assignments. */
11250 static int
11251 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
11253 gfc_component *c;
11254 int c_depth = 0, t_depth;
11256 for (c= derived->components; c; c = c->next)
11258 if ((!gfc_bt_struct (c->ts.type)
11259 || c->attr.pointer
11260 || c->attr.allocatable
11261 || c->attr.proc_pointer_comp
11262 || c->attr.class_pointer
11263 || c->attr.proc_pointer)
11264 && !c->attr.defined_assign_comp)
11265 continue;
11267 if (c->as && c_depth == 0)
11268 c_depth = 1;
11270 if (c->ts.u.derived->attr.defined_assign_comp)
11271 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
11272 c->as ? 1 : 0);
11273 else
11274 t_depth = 0;
11276 c_depth = t_depth > c_depth ? t_depth : c_depth;
11278 return depth + c_depth;
11282 /* Implement 7.2.1.3 of the F08 standard:
11283 "An intrinsic assignment where the variable is of derived type is
11284 performed as if each component of the variable were assigned from the
11285 corresponding component of expr using pointer assignment (7.2.2) for
11286 each pointer component, defined assignment for each nonpointer
11287 nonallocatable component of a type that has a type-bound defined
11288 assignment consistent with the component, intrinsic assignment for
11289 each other nonpointer nonallocatable component, ..."
11291 The pointer assignments are taken care of by the intrinsic
11292 assignment of the structure itself. This function recursively adds
11293 defined assignments where required. The recursion is accomplished
11294 by calling gfc_resolve_code.
11296 When the lhs in a defined assignment has intent INOUT, we need a
11297 temporary for the lhs. In pseudo-code:
11299 ! Only call function lhs once.
11300 if (lhs is not a constant or an variable)
11301 temp_x = expr2
11302 expr2 => temp_x
11303 ! Do the intrinsic assignment
11304 expr1 = expr2
11305 ! Now do the defined assignments
11306 do over components with typebound defined assignment [%cmp]
11307 #if one component's assignment procedure is INOUT
11308 t1 = expr1
11309 #if expr2 non-variable
11310 temp_x = expr2
11311 expr2 => temp_x
11312 # endif
11313 expr1 = expr2
11314 # for each cmp
11315 t1%cmp {defined=} expr2%cmp
11316 expr1%cmp = t1%cmp
11317 #else
11318 expr1 = expr2
11320 # for each cmp
11321 expr1%cmp {defined=} expr2%cmp
11322 #endif
11325 /* The temporary assignments have to be put on top of the additional
11326 code to avoid the result being changed by the intrinsic assignment.
11328 static int component_assignment_level = 0;
11329 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
11331 static void
11332 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
11334 gfc_component *comp1, *comp2;
11335 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
11336 gfc_expr *t1;
11337 int error_count, depth;
11339 gfc_get_errors (NULL, &error_count);
11341 /* Filter out continuing processing after an error. */
11342 if (error_count
11343 || (*code)->expr1->ts.type != BT_DERIVED
11344 || (*code)->expr2->ts.type != BT_DERIVED)
11345 return;
11347 /* TODO: Handle more than one part array reference in assignments. */
11348 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
11349 (*code)->expr1->rank ? 1 : 0);
11350 if (depth > 1)
11352 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
11353 "done because multiple part array references would "
11354 "occur in intermediate expressions.", &(*code)->loc);
11355 return;
11358 component_assignment_level++;
11360 /* Create a temporary so that functions get called only once. */
11361 if ((*code)->expr2->expr_type != EXPR_VARIABLE
11362 && (*code)->expr2->expr_type != EXPR_CONSTANT)
11364 gfc_expr *tmp_expr;
11366 /* Assign the rhs to the temporary. */
11367 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11368 this_code = build_assignment (EXEC_ASSIGN,
11369 tmp_expr, (*code)->expr2,
11370 NULL, NULL, (*code)->loc);
11371 /* Add the code and substitute the rhs expression. */
11372 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
11373 gfc_free_expr ((*code)->expr2);
11374 (*code)->expr2 = tmp_expr;
11377 /* Do the intrinsic assignment. This is not needed if the lhs is one
11378 of the temporaries generated here, since the intrinsic assignment
11379 to the final result already does this. */
11380 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
11382 this_code = build_assignment (EXEC_ASSIGN,
11383 (*code)->expr1, (*code)->expr2,
11384 NULL, NULL, (*code)->loc);
11385 add_code_to_chain (&this_code, &head, &tail);
11388 comp1 = (*code)->expr1->ts.u.derived->components;
11389 comp2 = (*code)->expr2->ts.u.derived->components;
11391 t1 = NULL;
11392 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
11394 bool inout = false;
11396 /* The intrinsic assignment does the right thing for pointers
11397 of all kinds and allocatable components. */
11398 if (!gfc_bt_struct (comp1->ts.type)
11399 || comp1->attr.pointer
11400 || comp1->attr.allocatable
11401 || comp1->attr.proc_pointer_comp
11402 || comp1->attr.class_pointer
11403 || comp1->attr.proc_pointer)
11404 continue;
11406 /* Make an assigment for this component. */
11407 this_code = build_assignment (EXEC_ASSIGN,
11408 (*code)->expr1, (*code)->expr2,
11409 comp1, comp2, (*code)->loc);
11411 /* Convert the assignment if there is a defined assignment for
11412 this type. Otherwise, using the call from gfc_resolve_code,
11413 recurse into its components. */
11414 gfc_resolve_code (this_code, ns);
11416 if (this_code->op == EXEC_ASSIGN_CALL)
11418 gfc_formal_arglist *dummy_args;
11419 gfc_symbol *rsym;
11420 /* Check that there is a typebound defined assignment. If not,
11421 then this must be a module defined assignment. We cannot
11422 use the defined_assign_comp attribute here because it must
11423 be this derived type that has the defined assignment and not
11424 a parent type. */
11425 if (!(comp1->ts.u.derived->f2k_derived
11426 && comp1->ts.u.derived->f2k_derived
11427 ->tb_op[INTRINSIC_ASSIGN]))
11429 gfc_free_statements (this_code);
11430 this_code = NULL;
11431 continue;
11434 /* If the first argument of the subroutine has intent INOUT
11435 a temporary must be generated and used instead. */
11436 rsym = this_code->resolved_sym;
11437 dummy_args = gfc_sym_get_dummy_args (rsym);
11438 if (dummy_args
11439 && dummy_args->sym->attr.intent == INTENT_INOUT)
11441 gfc_code *temp_code;
11442 inout = true;
11444 /* Build the temporary required for the assignment and put
11445 it at the head of the generated code. */
11446 if (!t1)
11448 t1 = get_temp_from_expr ((*code)->expr1, ns);
11449 temp_code = build_assignment (EXEC_ASSIGN,
11450 t1, (*code)->expr1,
11451 NULL, NULL, (*code)->loc);
11453 /* For allocatable LHS, check whether it is allocated. Note
11454 that allocatable components with defined assignment are
11455 not yet support. See PR 57696. */
11456 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
11458 gfc_code *block;
11459 gfc_expr *e =
11460 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11461 block = gfc_get_code (EXEC_IF);
11462 block->block = gfc_get_code (EXEC_IF);
11463 block->block->expr1
11464 = gfc_build_intrinsic_call (ns,
11465 GFC_ISYM_ALLOCATED, "allocated",
11466 (*code)->loc, 1, e);
11467 block->block->next = temp_code;
11468 temp_code = block;
11470 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
11473 /* Replace the first actual arg with the component of the
11474 temporary. */
11475 gfc_free_expr (this_code->ext.actual->expr);
11476 this_code->ext.actual->expr = gfc_copy_expr (t1);
11477 add_comp_ref (this_code->ext.actual->expr, comp1);
11479 /* If the LHS variable is allocatable and wasn't allocated and
11480 the temporary is allocatable, pointer assign the address of
11481 the freshly allocated LHS to the temporary. */
11482 if ((*code)->expr1->symtree->n.sym->attr.allocatable
11483 && gfc_expr_attr ((*code)->expr1).allocatable)
11485 gfc_code *block;
11486 gfc_expr *cond;
11488 cond = gfc_get_expr ();
11489 cond->ts.type = BT_LOGICAL;
11490 cond->ts.kind = gfc_default_logical_kind;
11491 cond->expr_type = EXPR_OP;
11492 cond->where = (*code)->loc;
11493 cond->value.op.op = INTRINSIC_NOT;
11494 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
11495 GFC_ISYM_ALLOCATED, "allocated",
11496 (*code)->loc, 1, gfc_copy_expr (t1));
11497 block = gfc_get_code (EXEC_IF);
11498 block->block = gfc_get_code (EXEC_IF);
11499 block->block->expr1 = cond;
11500 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11501 t1, (*code)->expr1,
11502 NULL, NULL, (*code)->loc);
11503 add_code_to_chain (&block, &head, &tail);
11507 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
11509 /* Don't add intrinsic assignments since they are already
11510 effected by the intrinsic assignment of the structure. */
11511 gfc_free_statements (this_code);
11512 this_code = NULL;
11513 continue;
11516 add_code_to_chain (&this_code, &head, &tail);
11518 if (t1 && inout)
11520 /* Transfer the value to the final result. */
11521 this_code = build_assignment (EXEC_ASSIGN,
11522 (*code)->expr1, t1,
11523 comp1, comp2, (*code)->loc);
11524 add_code_to_chain (&this_code, &head, &tail);
11528 /* Put the temporary assignments at the top of the generated code. */
11529 if (tmp_head && component_assignment_level == 1)
11531 gfc_append_code (tmp_head, head);
11532 head = tmp_head;
11533 tmp_head = tmp_tail = NULL;
11536 // If we did a pointer assignment - thus, we need to ensure that the LHS is
11537 // not accidentally deallocated. Hence, nullify t1.
11538 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
11539 && gfc_expr_attr ((*code)->expr1).allocatable)
11541 gfc_code *block;
11542 gfc_expr *cond;
11543 gfc_expr *e;
11545 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11546 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
11547 (*code)->loc, 2, gfc_copy_expr (t1), e);
11548 block = gfc_get_code (EXEC_IF);
11549 block->block = gfc_get_code (EXEC_IF);
11550 block->block->expr1 = cond;
11551 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11552 t1, gfc_get_null_expr (&(*code)->loc),
11553 NULL, NULL, (*code)->loc);
11554 gfc_append_code (tail, block);
11555 tail = block;
11558 /* Now attach the remaining code chain to the input code. Step on
11559 to the end of the new code since resolution is complete. */
11560 gcc_assert ((*code)->op == EXEC_ASSIGN);
11561 tail->next = (*code)->next;
11562 /* Overwrite 'code' because this would place the intrinsic assignment
11563 before the temporary for the lhs is created. */
11564 gfc_free_expr ((*code)->expr1);
11565 gfc_free_expr ((*code)->expr2);
11566 **code = *head;
11567 if (head != tail)
11568 free (head);
11569 *code = tail;
11571 component_assignment_level--;
11575 /* F2008: Pointer function assignments are of the form:
11576 ptr_fcn (args) = expr
11577 This function breaks these assignments into two statements:
11578 temporary_pointer => ptr_fcn(args)
11579 temporary_pointer = expr */
11581 static bool
11582 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
11584 gfc_expr *tmp_ptr_expr;
11585 gfc_code *this_code;
11586 gfc_component *comp;
11587 gfc_symbol *s;
11589 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
11590 return false;
11592 /* Even if standard does not support this feature, continue to build
11593 the two statements to avoid upsetting frontend_passes.c. */
11594 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
11595 "%L", &(*code)->loc);
11597 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
11599 if (comp)
11600 s = comp->ts.interface;
11601 else
11602 s = (*code)->expr1->symtree->n.sym;
11604 if (s == NULL || !s->result->attr.pointer)
11606 gfc_error ("The function result on the lhs of the assignment at "
11607 "%L must have the pointer attribute.",
11608 &(*code)->expr1->where);
11609 (*code)->op = EXEC_NOP;
11610 return false;
11613 tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
11615 /* get_temp_from_expression is set up for ordinary assignments. To that
11616 end, where array bounds are not known, arrays are made allocatable.
11617 Change the temporary to a pointer here. */
11618 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
11619 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
11620 tmp_ptr_expr->where = (*code)->loc;
11622 this_code = build_assignment (EXEC_ASSIGN,
11623 tmp_ptr_expr, (*code)->expr2,
11624 NULL, NULL, (*code)->loc);
11625 this_code->next = (*code)->next;
11626 (*code)->next = this_code;
11627 (*code)->op = EXEC_POINTER_ASSIGN;
11628 (*code)->expr2 = (*code)->expr1;
11629 (*code)->expr1 = tmp_ptr_expr;
11631 return true;
11635 /* Deferred character length assignments from an operator expression
11636 require a temporary because the character length of the lhs can
11637 change in the course of the assignment. */
11639 static bool
11640 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
11642 gfc_expr *tmp_expr;
11643 gfc_code *this_code;
11645 if (!((*code)->expr1->ts.type == BT_CHARACTER
11646 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
11647 && (*code)->expr2->expr_type == EXPR_OP))
11648 return false;
11650 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
11651 return false;
11653 if (gfc_expr_attr ((*code)->expr1).pointer)
11654 return false;
11656 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11657 tmp_expr->where = (*code)->loc;
11659 /* A new charlen is required to ensure that the variable string
11660 length is different to that of the original lhs. */
11661 tmp_expr->ts.u.cl = gfc_get_charlen();
11662 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
11663 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
11664 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
11666 tmp_expr->symtree->n.sym->ts.deferred = 1;
11668 this_code = build_assignment (EXEC_ASSIGN,
11669 (*code)->expr1,
11670 gfc_copy_expr (tmp_expr),
11671 NULL, NULL, (*code)->loc);
11673 (*code)->expr1 = tmp_expr;
11675 this_code->next = (*code)->next;
11676 (*code)->next = this_code;
11678 return true;
11682 /* Given a block of code, recursively resolve everything pointed to by this
11683 code block. */
11685 void
11686 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
11688 int omp_workshare_save;
11689 int forall_save, do_concurrent_save;
11690 code_stack frame;
11691 bool t;
11693 frame.prev = cs_base;
11694 frame.head = code;
11695 cs_base = &frame;
11697 find_reachable_labels (code);
11699 for (; code; code = code->next)
11701 frame.current = code;
11702 forall_save = forall_flag;
11703 do_concurrent_save = gfc_do_concurrent_flag;
11705 if (code->op == EXEC_FORALL)
11707 forall_flag = 1;
11708 gfc_resolve_forall (code, ns, forall_save);
11709 forall_flag = 2;
11711 else if (code->block)
11713 omp_workshare_save = -1;
11714 switch (code->op)
11716 case EXEC_OACC_PARALLEL_LOOP:
11717 case EXEC_OACC_PARALLEL:
11718 case EXEC_OACC_KERNELS_LOOP:
11719 case EXEC_OACC_KERNELS:
11720 case EXEC_OACC_SERIAL_LOOP:
11721 case EXEC_OACC_SERIAL:
11722 case EXEC_OACC_DATA:
11723 case EXEC_OACC_HOST_DATA:
11724 case EXEC_OACC_LOOP:
11725 gfc_resolve_oacc_blocks (code, ns);
11726 break;
11727 case EXEC_OMP_PARALLEL_WORKSHARE:
11728 omp_workshare_save = omp_workshare_flag;
11729 omp_workshare_flag = 1;
11730 gfc_resolve_omp_parallel_blocks (code, ns);
11731 break;
11732 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
11733 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
11734 case EXEC_OMP_PARALLEL:
11735 case EXEC_OMP_PARALLEL_DO:
11736 case EXEC_OMP_PARALLEL_DO_SIMD:
11737 case EXEC_OMP_PARALLEL_SECTIONS:
11738 case EXEC_OMP_TARGET_PARALLEL:
11739 case EXEC_OMP_TARGET_PARALLEL_DO:
11740 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
11741 case EXEC_OMP_TARGET_TEAMS:
11742 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
11743 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
11744 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11745 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
11746 case EXEC_OMP_TASK:
11747 case EXEC_OMP_TASKLOOP:
11748 case EXEC_OMP_TASKLOOP_SIMD:
11749 case EXEC_OMP_TEAMS:
11750 case EXEC_OMP_TEAMS_DISTRIBUTE:
11751 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
11752 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11753 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
11754 omp_workshare_save = omp_workshare_flag;
11755 omp_workshare_flag = 0;
11756 gfc_resolve_omp_parallel_blocks (code, ns);
11757 break;
11758 case EXEC_OMP_DISTRIBUTE:
11759 case EXEC_OMP_DISTRIBUTE_SIMD:
11760 case EXEC_OMP_DO:
11761 case EXEC_OMP_DO_SIMD:
11762 case EXEC_OMP_SIMD:
11763 case EXEC_OMP_TARGET_SIMD:
11764 gfc_resolve_omp_do_blocks (code, ns);
11765 break;
11766 case EXEC_SELECT_TYPE:
11767 /* Blocks are handled in resolve_select_type because we have
11768 to transform the SELECT TYPE into ASSOCIATE first. */
11769 break;
11770 case EXEC_DO_CONCURRENT:
11771 gfc_do_concurrent_flag = 1;
11772 gfc_resolve_blocks (code->block, ns);
11773 gfc_do_concurrent_flag = 2;
11774 break;
11775 case EXEC_OMP_WORKSHARE:
11776 omp_workshare_save = omp_workshare_flag;
11777 omp_workshare_flag = 1;
11778 /* FALL THROUGH */
11779 default:
11780 gfc_resolve_blocks (code->block, ns);
11781 break;
11784 if (omp_workshare_save != -1)
11785 omp_workshare_flag = omp_workshare_save;
11787 start:
11788 t = true;
11789 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
11790 t = gfc_resolve_expr (code->expr1);
11791 forall_flag = forall_save;
11792 gfc_do_concurrent_flag = do_concurrent_save;
11794 if (!gfc_resolve_expr (code->expr2))
11795 t = false;
11797 if (code->op == EXEC_ALLOCATE
11798 && !gfc_resolve_expr (code->expr3))
11799 t = false;
11801 switch (code->op)
11803 case EXEC_NOP:
11804 case EXEC_END_BLOCK:
11805 case EXEC_END_NESTED_BLOCK:
11806 case EXEC_CYCLE:
11807 case EXEC_PAUSE:
11808 case EXEC_STOP:
11809 case EXEC_ERROR_STOP:
11810 case EXEC_EXIT:
11811 case EXEC_CONTINUE:
11812 case EXEC_DT_END:
11813 case EXEC_ASSIGN_CALL:
11814 break;
11816 case EXEC_CRITICAL:
11817 resolve_critical (code);
11818 break;
11820 case EXEC_SYNC_ALL:
11821 case EXEC_SYNC_IMAGES:
11822 case EXEC_SYNC_MEMORY:
11823 resolve_sync (code);
11824 break;
11826 case EXEC_LOCK:
11827 case EXEC_UNLOCK:
11828 case EXEC_EVENT_POST:
11829 case EXEC_EVENT_WAIT:
11830 resolve_lock_unlock_event (code);
11831 break;
11833 case EXEC_FAIL_IMAGE:
11834 case EXEC_FORM_TEAM:
11835 case EXEC_CHANGE_TEAM:
11836 case EXEC_END_TEAM:
11837 case EXEC_SYNC_TEAM:
11838 break;
11840 case EXEC_ENTRY:
11841 /* Keep track of which entry we are up to. */
11842 current_entry_id = code->ext.entry->id;
11843 break;
11845 case EXEC_WHERE:
11846 resolve_where (code, NULL);
11847 break;
11849 case EXEC_GOTO:
11850 if (code->expr1 != NULL)
11852 if (code->expr1->expr_type != EXPR_VARIABLE
11853 || code->expr1->ts.type != BT_INTEGER
11854 || (code->expr1->ref
11855 && code->expr1->ref->type == REF_ARRAY)
11856 || code->expr1->symtree == NULL
11857 || (code->expr1->symtree->n.sym
11858 && (code->expr1->symtree->n.sym->attr.flavor
11859 == FL_PARAMETER)))
11860 gfc_error ("ASSIGNED GOTO statement at %L requires a "
11861 "scalar INTEGER variable", &code->expr1->where);
11862 else if (code->expr1->symtree->n.sym
11863 && code->expr1->symtree->n.sym->attr.assign != 1)
11864 gfc_error ("Variable %qs has not been assigned a target "
11865 "label at %L", code->expr1->symtree->n.sym->name,
11866 &code->expr1->where);
11868 else
11869 resolve_branch (code->label1, code);
11870 break;
11872 case EXEC_RETURN:
11873 if (code->expr1 != NULL
11874 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
11875 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
11876 "INTEGER return specifier", &code->expr1->where);
11877 break;
11879 case EXEC_INIT_ASSIGN:
11880 case EXEC_END_PROCEDURE:
11881 break;
11883 case EXEC_ASSIGN:
11884 if (!t)
11885 break;
11887 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
11888 the LHS. */
11889 if (code->expr1->expr_type == EXPR_FUNCTION
11890 && code->expr1->value.function.isym
11891 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
11892 remove_caf_get_intrinsic (code->expr1);
11894 /* If this is a pointer function in an lvalue variable context,
11895 the new code will have to be resolved afresh. This is also the
11896 case with an error, where the code is transformed into NOP to
11897 prevent ICEs downstream. */
11898 if (resolve_ptr_fcn_assign (&code, ns)
11899 || code->op == EXEC_NOP)
11900 goto start;
11902 if (!gfc_check_vardef_context (code->expr1, false, false, false,
11903 _("assignment")))
11904 break;
11906 if (resolve_ordinary_assign (code, ns))
11908 if (code->op == EXEC_COMPCALL)
11909 goto compcall;
11910 else
11911 goto call;
11914 /* Check for dependencies in deferred character length array
11915 assignments and generate a temporary, if necessary. */
11916 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
11917 break;
11919 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
11920 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
11921 && code->expr1->ts.u.derived
11922 && code->expr1->ts.u.derived->attr.defined_assign_comp)
11923 generate_component_assignments (&code, ns);
11925 break;
11927 case EXEC_LABEL_ASSIGN:
11928 if (code->label1->defined == ST_LABEL_UNKNOWN)
11929 gfc_error ("Label %d referenced at %L is never defined",
11930 code->label1->value, &code->label1->where);
11931 if (t
11932 && (code->expr1->expr_type != EXPR_VARIABLE
11933 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
11934 || code->expr1->symtree->n.sym->ts.kind
11935 != gfc_default_integer_kind
11936 || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
11937 || code->expr1->symtree->n.sym->as != NULL))
11938 gfc_error ("ASSIGN statement at %L requires a scalar "
11939 "default INTEGER variable", &code->expr1->where);
11940 break;
11942 case EXEC_POINTER_ASSIGN:
11944 gfc_expr* e;
11946 if (!t)
11947 break;
11949 /* This is both a variable definition and pointer assignment
11950 context, so check both of them. For rank remapping, a final
11951 array ref may be present on the LHS and fool gfc_expr_attr
11952 used in gfc_check_vardef_context. Remove it. */
11953 e = remove_last_array_ref (code->expr1);
11954 t = gfc_check_vardef_context (e, true, false, false,
11955 _("pointer assignment"));
11956 if (t)
11957 t = gfc_check_vardef_context (e, false, false, false,
11958 _("pointer assignment"));
11959 gfc_free_expr (e);
11961 t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
11963 if (!t)
11964 break;
11966 /* Assigning a class object always is a regular assign. */
11967 if (code->expr2->ts.type == BT_CLASS
11968 && code->expr1->ts.type == BT_CLASS
11969 && !CLASS_DATA (code->expr2)->attr.dimension
11970 && !(gfc_expr_attr (code->expr1).proc_pointer
11971 && code->expr2->expr_type == EXPR_VARIABLE
11972 && code->expr2->symtree->n.sym->attr.flavor
11973 == FL_PROCEDURE))
11974 code->op = EXEC_ASSIGN;
11975 break;
11978 case EXEC_ARITHMETIC_IF:
11980 gfc_expr *e = code->expr1;
11982 gfc_resolve_expr (e);
11983 if (e->expr_type == EXPR_NULL)
11984 gfc_error ("Invalid NULL at %L", &e->where);
11986 if (t && (e->rank > 0
11987 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
11988 gfc_error ("Arithmetic IF statement at %L requires a scalar "
11989 "REAL or INTEGER expression", &e->where);
11991 resolve_branch (code->label1, code);
11992 resolve_branch (code->label2, code);
11993 resolve_branch (code->label3, code);
11995 break;
11997 case EXEC_IF:
11998 if (t && code->expr1 != NULL
11999 && (code->expr1->ts.type != BT_LOGICAL
12000 || code->expr1->rank != 0))
12001 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
12002 &code->expr1->where);
12003 break;
12005 case EXEC_CALL:
12006 call:
12007 resolve_call (code);
12008 break;
12010 case EXEC_COMPCALL:
12011 compcall:
12012 resolve_typebound_subroutine (code);
12013 break;
12015 case EXEC_CALL_PPC:
12016 resolve_ppc_call (code);
12017 break;
12019 case EXEC_SELECT:
12020 /* Select is complicated. Also, a SELECT construct could be
12021 a transformed computed GOTO. */
12022 resolve_select (code, false);
12023 break;
12025 case EXEC_SELECT_TYPE:
12026 resolve_select_type (code, ns);
12027 break;
12029 case EXEC_SELECT_RANK:
12030 resolve_select_rank (code, ns);
12031 break;
12033 case EXEC_BLOCK:
12034 resolve_block_construct (code);
12035 break;
12037 case EXEC_DO:
12038 if (code->ext.iterator != NULL)
12040 gfc_iterator *iter = code->ext.iterator;
12041 if (gfc_resolve_iterator (iter, true, false))
12042 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
12043 true);
12045 break;
12047 case EXEC_DO_WHILE:
12048 if (code->expr1 == NULL)
12049 gfc_internal_error ("gfc_resolve_code(): No expression on "
12050 "DO WHILE");
12051 if (t
12052 && (code->expr1->rank != 0
12053 || code->expr1->ts.type != BT_LOGICAL))
12054 gfc_error ("Exit condition of DO WHILE loop at %L must be "
12055 "a scalar LOGICAL expression", &code->expr1->where);
12056 break;
12058 case EXEC_ALLOCATE:
12059 if (t)
12060 resolve_allocate_deallocate (code, "ALLOCATE");
12062 break;
12064 case EXEC_DEALLOCATE:
12065 if (t)
12066 resolve_allocate_deallocate (code, "DEALLOCATE");
12068 break;
12070 case EXEC_OPEN:
12071 if (!gfc_resolve_open (code->ext.open, &code->loc))
12072 break;
12074 resolve_branch (code->ext.open->err, code);
12075 break;
12077 case EXEC_CLOSE:
12078 if (!gfc_resolve_close (code->ext.close, &code->loc))
12079 break;
12081 resolve_branch (code->ext.close->err, code);
12082 break;
12084 case EXEC_BACKSPACE:
12085 case EXEC_ENDFILE:
12086 case EXEC_REWIND:
12087 case EXEC_FLUSH:
12088 if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
12089 break;
12091 resolve_branch (code->ext.filepos->err, code);
12092 break;
12094 case EXEC_INQUIRE:
12095 if (!gfc_resolve_inquire (code->ext.inquire))
12096 break;
12098 resolve_branch (code->ext.inquire->err, code);
12099 break;
12101 case EXEC_IOLENGTH:
12102 gcc_assert (code->ext.inquire != NULL);
12103 if (!gfc_resolve_inquire (code->ext.inquire))
12104 break;
12106 resolve_branch (code->ext.inquire->err, code);
12107 break;
12109 case EXEC_WAIT:
12110 if (!gfc_resolve_wait (code->ext.wait))
12111 break;
12113 resolve_branch (code->ext.wait->err, code);
12114 resolve_branch (code->ext.wait->end, code);
12115 resolve_branch (code->ext.wait->eor, code);
12116 break;
12118 case EXEC_READ:
12119 case EXEC_WRITE:
12120 if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
12121 break;
12123 resolve_branch (code->ext.dt->err, code);
12124 resolve_branch (code->ext.dt->end, code);
12125 resolve_branch (code->ext.dt->eor, code);
12126 break;
12128 case EXEC_TRANSFER:
12129 resolve_transfer (code);
12130 break;
12132 case EXEC_DO_CONCURRENT:
12133 case EXEC_FORALL:
12134 resolve_forall_iterators (code->ext.forall_iterator);
12136 if (code->expr1 != NULL
12137 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
12138 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
12139 "expression", &code->expr1->where);
12140 break;
12142 case EXEC_OACC_PARALLEL_LOOP:
12143 case EXEC_OACC_PARALLEL:
12144 case EXEC_OACC_KERNELS_LOOP:
12145 case EXEC_OACC_KERNELS:
12146 case EXEC_OACC_SERIAL_LOOP:
12147 case EXEC_OACC_SERIAL:
12148 case EXEC_OACC_DATA:
12149 case EXEC_OACC_HOST_DATA:
12150 case EXEC_OACC_LOOP:
12151 case EXEC_OACC_UPDATE:
12152 case EXEC_OACC_WAIT:
12153 case EXEC_OACC_CACHE:
12154 case EXEC_OACC_ENTER_DATA:
12155 case EXEC_OACC_EXIT_DATA:
12156 case EXEC_OACC_ATOMIC:
12157 case EXEC_OACC_DECLARE:
12158 gfc_resolve_oacc_directive (code, ns);
12159 break;
12161 case EXEC_OMP_ATOMIC:
12162 case EXEC_OMP_BARRIER:
12163 case EXEC_OMP_CANCEL:
12164 case EXEC_OMP_CANCELLATION_POINT:
12165 case EXEC_OMP_CRITICAL:
12166 case EXEC_OMP_FLUSH:
12167 case EXEC_OMP_DISTRIBUTE:
12168 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12169 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12170 case EXEC_OMP_DISTRIBUTE_SIMD:
12171 case EXEC_OMP_DO:
12172 case EXEC_OMP_DO_SIMD:
12173 case EXEC_OMP_MASTER:
12174 case EXEC_OMP_ORDERED:
12175 case EXEC_OMP_SECTIONS:
12176 case EXEC_OMP_SIMD:
12177 case EXEC_OMP_SINGLE:
12178 case EXEC_OMP_TARGET:
12179 case EXEC_OMP_TARGET_DATA:
12180 case EXEC_OMP_TARGET_ENTER_DATA:
12181 case EXEC_OMP_TARGET_EXIT_DATA:
12182 case EXEC_OMP_TARGET_PARALLEL:
12183 case EXEC_OMP_TARGET_PARALLEL_DO:
12184 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12185 case EXEC_OMP_TARGET_SIMD:
12186 case EXEC_OMP_TARGET_TEAMS:
12187 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12188 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12189 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12190 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12191 case EXEC_OMP_TARGET_UPDATE:
12192 case EXEC_OMP_TASK:
12193 case EXEC_OMP_TASKGROUP:
12194 case EXEC_OMP_TASKLOOP:
12195 case EXEC_OMP_TASKLOOP_SIMD:
12196 case EXEC_OMP_TASKWAIT:
12197 case EXEC_OMP_TASKYIELD:
12198 case EXEC_OMP_TEAMS:
12199 case EXEC_OMP_TEAMS_DISTRIBUTE:
12200 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12201 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12202 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12203 case EXEC_OMP_WORKSHARE:
12204 gfc_resolve_omp_directive (code, ns);
12205 break;
12207 case EXEC_OMP_PARALLEL:
12208 case EXEC_OMP_PARALLEL_DO:
12209 case EXEC_OMP_PARALLEL_DO_SIMD:
12210 case EXEC_OMP_PARALLEL_SECTIONS:
12211 case EXEC_OMP_PARALLEL_WORKSHARE:
12212 omp_workshare_save = omp_workshare_flag;
12213 omp_workshare_flag = 0;
12214 gfc_resolve_omp_directive (code, ns);
12215 omp_workshare_flag = omp_workshare_save;
12216 break;
12218 default:
12219 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
12223 cs_base = frame.prev;
12227 /* Resolve initial values and make sure they are compatible with
12228 the variable. */
12230 static void
12231 resolve_values (gfc_symbol *sym)
12233 bool t;
12235 if (sym->value == NULL)
12236 return;
12238 if (sym->value->expr_type == EXPR_STRUCTURE)
12239 t= resolve_structure_cons (sym->value, 1);
12240 else
12241 t = gfc_resolve_expr (sym->value);
12243 if (!t)
12244 return;
12246 gfc_check_assign_symbol (sym, NULL, sym->value);
12250 /* Verify any BIND(C) derived types in the namespace so we can report errors
12251 for them once, rather than for each variable declared of that type. */
12253 static void
12254 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
12256 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
12257 && derived_sym->attr.is_bind_c == 1)
12258 verify_bind_c_derived_type (derived_sym);
12260 return;
12264 /* Check the interfaces of DTIO procedures associated with derived
12265 type 'sym'. These procedures can either have typebound bindings or
12266 can appear in DTIO generic interfaces. */
12268 static void
12269 gfc_verify_DTIO_procedures (gfc_symbol *sym)
12271 if (!sym || sym->attr.flavor != FL_DERIVED)
12272 return;
12274 gfc_check_dtio_interfaces (sym);
12276 return;
12279 /* Verify that any binding labels used in a given namespace do not collide
12280 with the names or binding labels of any global symbols. Multiple INTERFACE
12281 for the same procedure are permitted. */
12283 static void
12284 gfc_verify_binding_labels (gfc_symbol *sym)
12286 gfc_gsymbol *gsym;
12287 const char *module;
12289 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
12290 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
12291 return;
12293 gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
12295 if (sym->module)
12296 module = sym->module;
12297 else if (sym->ns && sym->ns->proc_name
12298 && sym->ns->proc_name->attr.flavor == FL_MODULE)
12299 module = sym->ns->proc_name->name;
12300 else if (sym->ns && sym->ns->parent
12301 && sym->ns && sym->ns->parent->proc_name
12302 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
12303 module = sym->ns->parent->proc_name->name;
12304 else
12305 module = NULL;
12307 if (!gsym
12308 || (!gsym->defined
12309 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
12311 if (!gsym)
12312 gsym = gfc_get_gsymbol (sym->binding_label, true);
12313 gsym->where = sym->declared_at;
12314 gsym->sym_name = sym->name;
12315 gsym->binding_label = sym->binding_label;
12316 gsym->ns = sym->ns;
12317 gsym->mod_name = module;
12318 if (sym->attr.function)
12319 gsym->type = GSYM_FUNCTION;
12320 else if (sym->attr.subroutine)
12321 gsym->type = GSYM_SUBROUTINE;
12322 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
12323 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
12324 return;
12327 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
12329 gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
12330 "identifier as entity at %L", sym->name,
12331 sym->binding_label, &sym->declared_at, &gsym->where);
12332 /* Clear the binding label to prevent checking multiple times. */
12333 sym->binding_label = NULL;
12334 return;
12337 if (sym->attr.flavor == FL_VARIABLE && module
12338 && (strcmp (module, gsym->mod_name) != 0
12339 || strcmp (sym->name, gsym->sym_name) != 0))
12341 /* This can only happen if the variable is defined in a module - if it
12342 isn't the same module, reject it. */
12343 gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
12344 "uses the same global identifier as entity at %L from module %qs",
12345 sym->name, module, sym->binding_label,
12346 &sym->declared_at, &gsym->where, gsym->mod_name);
12347 sym->binding_label = NULL;
12348 return;
12351 if ((sym->attr.function || sym->attr.subroutine)
12352 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
12353 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
12354 && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
12355 && (module != gsym->mod_name
12356 || strcmp (gsym->sym_name, sym->name) != 0
12357 || (module && strcmp (module, gsym->mod_name) != 0)))
12359 /* Print an error if the procedure is defined multiple times; we have to
12360 exclude references to the same procedure via module association or
12361 multiple checks for the same procedure. */
12362 gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
12363 "global identifier as entity at %L", sym->name,
12364 sym->binding_label, &sym->declared_at, &gsym->where);
12365 sym->binding_label = NULL;
12370 /* Resolve an index expression. */
12372 static bool
12373 resolve_index_expr (gfc_expr *e)
12375 if (!gfc_resolve_expr (e))
12376 return false;
12378 if (!gfc_simplify_expr (e, 0))
12379 return false;
12381 if (!gfc_specification_expr (e))
12382 return false;
12384 return true;
12388 /* Resolve a charlen structure. */
12390 static bool
12391 resolve_charlen (gfc_charlen *cl)
12393 int k;
12394 bool saved_specification_expr;
12396 if (cl->resolved)
12397 return true;
12399 cl->resolved = 1;
12400 saved_specification_expr = specification_expr;
12401 specification_expr = true;
12403 if (cl->length_from_typespec)
12405 if (!gfc_resolve_expr (cl->length))
12407 specification_expr = saved_specification_expr;
12408 return false;
12411 if (!gfc_simplify_expr (cl->length, 0))
12413 specification_expr = saved_specification_expr;
12414 return false;
12417 /* cl->length has been resolved. It should have an integer type. */
12418 if (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0)
12420 gfc_error ("Scalar INTEGER expression expected at %L",
12421 &cl->length->where);
12422 return false;
12425 else
12427 if (!resolve_index_expr (cl->length))
12429 specification_expr = saved_specification_expr;
12430 return false;
12434 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
12435 a negative value, the length of character entities declared is zero. */
12436 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12437 && mpz_sgn (cl->length->value.integer) < 0)
12438 gfc_replace_expr (cl->length,
12439 gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
12441 /* Check that the character length is not too large. */
12442 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
12443 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12444 && cl->length->ts.type == BT_INTEGER
12445 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
12447 gfc_error ("String length at %L is too large", &cl->length->where);
12448 specification_expr = saved_specification_expr;
12449 return false;
12452 specification_expr = saved_specification_expr;
12453 return true;
12457 /* Test for non-constant shape arrays. */
12459 static bool
12460 is_non_constant_shape_array (gfc_symbol *sym)
12462 gfc_expr *e;
12463 int i;
12464 bool not_constant;
12466 not_constant = false;
12467 if (sym->as != NULL)
12469 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
12470 has not been simplified; parameter array references. Do the
12471 simplification now. */
12472 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
12474 if (i == GFC_MAX_DIMENSIONS)
12475 break;
12477 e = sym->as->lower[i];
12478 if (e && (!resolve_index_expr(e)
12479 || !gfc_is_constant_expr (e)))
12480 not_constant = true;
12481 e = sym->as->upper[i];
12482 if (e && (!resolve_index_expr(e)
12483 || !gfc_is_constant_expr (e)))
12484 not_constant = true;
12487 return not_constant;
12490 /* Given a symbol and an initialization expression, add code to initialize
12491 the symbol to the function entry. */
12492 static void
12493 build_init_assign (gfc_symbol *sym, gfc_expr *init)
12495 gfc_expr *lval;
12496 gfc_code *init_st;
12497 gfc_namespace *ns = sym->ns;
12499 /* Search for the function namespace if this is a contained
12500 function without an explicit result. */
12501 if (sym->attr.function && sym == sym->result
12502 && sym->name != sym->ns->proc_name->name)
12504 ns = ns->contained;
12505 for (;ns; ns = ns->sibling)
12506 if (strcmp (ns->proc_name->name, sym->name) == 0)
12507 break;
12510 if (ns == NULL)
12512 gfc_free_expr (init);
12513 return;
12516 /* Build an l-value expression for the result. */
12517 lval = gfc_lval_expr_from_sym (sym);
12519 /* Add the code at scope entry. */
12520 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
12521 init_st->next = ns->code;
12522 ns->code = init_st;
12524 /* Assign the default initializer to the l-value. */
12525 init_st->loc = sym->declared_at;
12526 init_st->expr1 = lval;
12527 init_st->expr2 = init;
12531 /* Whether or not we can generate a default initializer for a symbol. */
12533 static bool
12534 can_generate_init (gfc_symbol *sym)
12536 symbol_attribute *a;
12537 if (!sym)
12538 return false;
12539 a = &sym->attr;
12541 /* These symbols should never have a default initialization. */
12542 return !(
12543 a->allocatable
12544 || a->external
12545 || a->pointer
12546 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
12547 && (CLASS_DATA (sym)->attr.class_pointer
12548 || CLASS_DATA (sym)->attr.proc_pointer))
12549 || a->in_equivalence
12550 || a->in_common
12551 || a->data
12552 || sym->module
12553 || a->cray_pointee
12554 || a->cray_pointer
12555 || sym->assoc
12556 || (!a->referenced && !a->result)
12557 || (a->dummy && a->intent != INTENT_OUT)
12558 || (a->function && sym != sym->result)
12563 /* Assign the default initializer to a derived type variable or result. */
12565 static void
12566 apply_default_init (gfc_symbol *sym)
12568 gfc_expr *init = NULL;
12570 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
12571 return;
12573 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
12574 init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
12576 if (init == NULL && sym->ts.type != BT_CLASS)
12577 return;
12579 build_init_assign (sym, init);
12580 sym->attr.referenced = 1;
12584 /* Build an initializer for a local. Returns null if the symbol should not have
12585 a default initialization. */
12587 static gfc_expr *
12588 build_default_init_expr (gfc_symbol *sym)
12590 /* These symbols should never have a default initialization. */
12591 if (sym->attr.allocatable
12592 || sym->attr.external
12593 || sym->attr.dummy
12594 || sym->attr.pointer
12595 || sym->attr.in_equivalence
12596 || sym->attr.in_common
12597 || sym->attr.data
12598 || sym->module
12599 || sym->attr.cray_pointee
12600 || sym->attr.cray_pointer
12601 || sym->assoc)
12602 return NULL;
12604 /* Get the appropriate init expression. */
12605 return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
12608 /* Add an initialization expression to a local variable. */
12609 static void
12610 apply_default_init_local (gfc_symbol *sym)
12612 gfc_expr *init = NULL;
12614 /* The symbol should be a variable or a function return value. */
12615 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
12616 || (sym->attr.function && sym->result != sym))
12617 return;
12619 /* Try to build the initializer expression. If we can't initialize
12620 this symbol, then init will be NULL. */
12621 init = build_default_init_expr (sym);
12622 if (init == NULL)
12623 return;
12625 /* For saved variables, we don't want to add an initializer at function
12626 entry, so we just add a static initializer. Note that automatic variables
12627 are stack allocated even with -fno-automatic; we have also to exclude
12628 result variable, which are also nonstatic. */
12629 if (!sym->attr.automatic
12630 && (sym->attr.save || sym->ns->save_all
12631 || (flag_max_stack_var_size == 0 && !sym->attr.result
12632 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
12633 && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
12635 /* Don't clobber an existing initializer! */
12636 gcc_assert (sym->value == NULL);
12637 sym->value = init;
12638 return;
12641 build_init_assign (sym, init);
12645 /* Resolution of common features of flavors variable and procedure. */
12647 static bool
12648 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
12650 gfc_array_spec *as;
12652 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
12653 && sym->ts.u.derived && CLASS_DATA (sym))
12654 as = CLASS_DATA (sym)->as;
12655 else
12656 as = sym->as;
12658 /* Constraints on deferred shape variable. */
12659 if (as == NULL || as->type != AS_DEFERRED)
12661 bool pointer, allocatable, dimension;
12663 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
12664 && sym->ts.u.derived && CLASS_DATA (sym))
12666 pointer = CLASS_DATA (sym)->attr.class_pointer;
12667 allocatable = CLASS_DATA (sym)->attr.allocatable;
12668 dimension = CLASS_DATA (sym)->attr.dimension;
12670 else
12672 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
12673 allocatable = sym->attr.allocatable;
12674 dimension = sym->attr.dimension;
12677 if (allocatable)
12679 if (dimension && as->type != AS_ASSUMED_RANK)
12681 gfc_error ("Allocatable array %qs at %L must have a deferred "
12682 "shape or assumed rank", sym->name, &sym->declared_at);
12683 return false;
12685 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
12686 "%qs at %L may not be ALLOCATABLE",
12687 sym->name, &sym->declared_at))
12688 return false;
12691 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
12693 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
12694 "assumed rank", sym->name, &sym->declared_at);
12695 sym->error = 1;
12696 return false;
12699 else
12701 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
12702 && sym->ts.type != BT_CLASS && !sym->assoc)
12704 gfc_error ("Array %qs at %L cannot have a deferred shape",
12705 sym->name, &sym->declared_at);
12706 return false;
12710 /* Constraints on polymorphic variables. */
12711 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
12713 /* F03:C502. */
12714 if (sym->attr.class_ok
12715 && sym->ts.u.derived
12716 && !sym->attr.select_type_temporary
12717 && !UNLIMITED_POLY (sym)
12718 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
12720 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
12721 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
12722 &sym->declared_at);
12723 return false;
12726 /* F03:C509. */
12727 /* Assume that use associated symbols were checked in the module ns.
12728 Class-variables that are associate-names are also something special
12729 and excepted from the test. */
12730 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
12732 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
12733 "or pointer", sym->name, &sym->declared_at);
12734 return false;
12738 return true;
12742 /* Additional checks for symbols with flavor variable and derived
12743 type. To be called from resolve_fl_variable. */
12745 static bool
12746 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
12748 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
12750 /* Check to see if a derived type is blocked from being host
12751 associated by the presence of another class I symbol in the same
12752 namespace. 14.6.1.3 of the standard and the discussion on
12753 comp.lang.fortran. */
12754 if (sym->ts.u.derived
12755 && sym->ns != sym->ts.u.derived->ns
12756 && !sym->ts.u.derived->attr.use_assoc
12757 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
12759 gfc_symbol *s;
12760 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
12761 if (s && s->attr.generic)
12762 s = gfc_find_dt_in_generic (s);
12763 if (s && !gfc_fl_struct (s->attr.flavor))
12765 gfc_error ("The type %qs cannot be host associated at %L "
12766 "because it is blocked by an incompatible object "
12767 "of the same name declared at %L",
12768 sym->ts.u.derived->name, &sym->declared_at,
12769 &s->declared_at);
12770 return false;
12774 /* 4th constraint in section 11.3: "If an object of a type for which
12775 component-initialization is specified (R429) appears in the
12776 specification-part of a module and does not have the ALLOCATABLE
12777 or POINTER attribute, the object shall have the SAVE attribute."
12779 The check for initializers is performed with
12780 gfc_has_default_initializer because gfc_default_initializer generates
12781 a hidden default for allocatable components. */
12782 if (!(sym->value || no_init_flag) && sym->ns->proc_name
12783 && sym->ns->proc_name->attr.flavor == FL_MODULE
12784 && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
12785 && !sym->attr.pointer && !sym->attr.allocatable
12786 && gfc_has_default_initializer (sym->ts.u.derived)
12787 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
12788 "%qs at %L, needed due to the default "
12789 "initialization", sym->name, &sym->declared_at))
12790 return false;
12792 /* Assign default initializer. */
12793 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
12794 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
12795 sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
12797 return true;
12801 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
12802 except in the declaration of an entity or component that has the POINTER
12803 or ALLOCATABLE attribute. */
12805 static bool
12806 deferred_requirements (gfc_symbol *sym)
12808 if (sym->ts.deferred
12809 && !(sym->attr.pointer
12810 || sym->attr.allocatable
12811 || sym->attr.associate_var
12812 || sym->attr.omp_udr_artificial_var))
12814 /* If a function has a result variable, only check the variable. */
12815 if (sym->result && sym->name != sym->result->name)
12816 return true;
12818 gfc_error ("Entity %qs at %L has a deferred type parameter and "
12819 "requires either the POINTER or ALLOCATABLE attribute",
12820 sym->name, &sym->declared_at);
12821 return false;
12823 return true;
12827 /* Resolve symbols with flavor variable. */
12829 static bool
12830 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
12832 const char *auto_save_msg = "Automatic object %qs at %L cannot have the "
12833 "SAVE attribute";
12835 if (!resolve_fl_var_and_proc (sym, mp_flag))
12836 return false;
12838 /* Set this flag to check that variables are parameters of all entries.
12839 This check is effected by the call to gfc_resolve_expr through
12840 is_non_constant_shape_array. */
12841 bool saved_specification_expr = specification_expr;
12842 specification_expr = true;
12844 if (sym->ns->proc_name
12845 && (sym->ns->proc_name->attr.flavor == FL_MODULE
12846 || sym->ns->proc_name->attr.is_main_program)
12847 && !sym->attr.use_assoc
12848 && !sym->attr.allocatable
12849 && !sym->attr.pointer
12850 && is_non_constant_shape_array (sym))
12852 /* F08:C541. The shape of an array defined in a main program or module
12853 * needs to be constant. */
12854 gfc_error ("The module or main program array %qs at %L must "
12855 "have constant shape", sym->name, &sym->declared_at);
12856 specification_expr = saved_specification_expr;
12857 return false;
12860 /* Constraints on deferred type parameter. */
12861 if (!deferred_requirements (sym))
12862 return false;
12864 if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
12866 /* Make sure that character string variables with assumed length are
12867 dummy arguments. */
12868 gfc_expr *e = NULL;
12870 if (sym->ts.u.cl)
12871 e = sym->ts.u.cl->length;
12872 else
12873 return false;
12875 if (e == NULL && !sym->attr.dummy && !sym->attr.result
12876 && !sym->ts.deferred && !sym->attr.select_type_temporary
12877 && !sym->attr.omp_udr_artificial_var)
12879 gfc_error ("Entity with assumed character length at %L must be a "
12880 "dummy argument or a PARAMETER", &sym->declared_at);
12881 specification_expr = saved_specification_expr;
12882 return false;
12885 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
12887 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
12888 specification_expr = saved_specification_expr;
12889 return false;
12892 if (!gfc_is_constant_expr (e)
12893 && !(e->expr_type == EXPR_VARIABLE
12894 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
12896 if (!sym->attr.use_assoc && sym->ns->proc_name
12897 && (sym->ns->proc_name->attr.flavor == FL_MODULE
12898 || sym->ns->proc_name->attr.is_main_program))
12900 gfc_error ("%qs at %L must have constant character length "
12901 "in this context", sym->name, &sym->declared_at);
12902 specification_expr = saved_specification_expr;
12903 return false;
12905 if (sym->attr.in_common)
12907 gfc_error ("COMMON variable %qs at %L must have constant "
12908 "character length", sym->name, &sym->declared_at);
12909 specification_expr = saved_specification_expr;
12910 return false;
12915 if (sym->value == NULL && sym->attr.referenced)
12916 apply_default_init_local (sym); /* Try to apply a default initialization. */
12918 /* Determine if the symbol may not have an initializer. */
12919 int no_init_flag = 0, automatic_flag = 0;
12920 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
12921 || sym->attr.intrinsic || sym->attr.result)
12922 no_init_flag = 1;
12923 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
12924 && is_non_constant_shape_array (sym))
12926 no_init_flag = automatic_flag = 1;
12928 /* Also, they must not have the SAVE attribute.
12929 SAVE_IMPLICIT is checked below. */
12930 if (sym->as && sym->attr.codimension)
12932 int corank = sym->as->corank;
12933 sym->as->corank = 0;
12934 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
12935 sym->as->corank = corank;
12937 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
12939 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
12940 specification_expr = saved_specification_expr;
12941 return false;
12945 /* Ensure that any initializer is simplified. */
12946 if (sym->value)
12947 gfc_simplify_expr (sym->value, 1);
12949 /* Reject illegal initializers. */
12950 if (!sym->mark && sym->value)
12952 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
12953 && CLASS_DATA (sym)->attr.allocatable))
12954 gfc_error ("Allocatable %qs at %L cannot have an initializer",
12955 sym->name, &sym->declared_at);
12956 else if (sym->attr.external)
12957 gfc_error ("External %qs at %L cannot have an initializer",
12958 sym->name, &sym->declared_at);
12959 else if (sym->attr.dummy)
12960 gfc_error ("Dummy %qs at %L cannot have an initializer",
12961 sym->name, &sym->declared_at);
12962 else if (sym->attr.intrinsic)
12963 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
12964 sym->name, &sym->declared_at);
12965 else if (sym->attr.result)
12966 gfc_error ("Function result %qs at %L cannot have an initializer",
12967 sym->name, &sym->declared_at);
12968 else if (automatic_flag)
12969 gfc_error ("Automatic array %qs at %L cannot have an initializer",
12970 sym->name, &sym->declared_at);
12971 else
12972 goto no_init_error;
12973 specification_expr = saved_specification_expr;
12974 return false;
12977 no_init_error:
12978 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
12980 bool res = resolve_fl_variable_derived (sym, no_init_flag);
12981 specification_expr = saved_specification_expr;
12982 return res;
12985 specification_expr = saved_specification_expr;
12986 return true;
12990 /* Compare the dummy characteristics of a module procedure interface
12991 declaration with the corresponding declaration in a submodule. */
12992 static gfc_formal_arglist *new_formal;
12993 static char errmsg[200];
12995 static void
12996 compare_fsyms (gfc_symbol *sym)
12998 gfc_symbol *fsym;
13000 if (sym == NULL || new_formal == NULL)
13001 return;
13003 fsym = new_formal->sym;
13005 if (sym == fsym)
13006 return;
13008 if (strcmp (sym->name, fsym->name) == 0)
13010 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
13011 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
13016 /* Resolve a procedure. */
13018 static bool
13019 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
13021 gfc_formal_arglist *arg;
13023 if (sym->attr.function
13024 && !resolve_fl_var_and_proc (sym, mp_flag))
13025 return false;
13027 /* Constraints on deferred type parameter. */
13028 if (!deferred_requirements (sym))
13029 return false;
13031 if (sym->ts.type == BT_CHARACTER)
13033 gfc_charlen *cl = sym->ts.u.cl;
13035 if (cl && cl->length && gfc_is_constant_expr (cl->length)
13036 && !resolve_charlen (cl))
13037 return false;
13039 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13040 && sym->attr.proc == PROC_ST_FUNCTION)
13042 gfc_error ("Character-valued statement function %qs at %L must "
13043 "have constant length", sym->name, &sym->declared_at);
13044 return false;
13048 /* Ensure that derived type for are not of a private type. Internal
13049 module procedures are excluded by 2.2.3.3 - i.e., they are not
13050 externally accessible and can access all the objects accessible in
13051 the host. */
13052 if (!(sym->ns->parent && sym->ns->parent->proc_name
13053 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
13054 && gfc_check_symbol_access (sym))
13056 gfc_interface *iface;
13058 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
13060 if (arg->sym
13061 && arg->sym->ts.type == BT_DERIVED
13062 && arg->sym->ts.u.derived
13063 && !arg->sym->ts.u.derived->attr.use_assoc
13064 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13065 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
13066 "and cannot be a dummy argument"
13067 " of %qs, which is PUBLIC at %L",
13068 arg->sym->name, sym->name,
13069 &sym->declared_at))
13071 /* Stop this message from recurring. */
13072 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13073 return false;
13077 /* PUBLIC interfaces may expose PRIVATE procedures that take types
13078 PRIVATE to the containing module. */
13079 for (iface = sym->generic; iface; iface = iface->next)
13081 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
13083 if (arg->sym
13084 && arg->sym->ts.type == BT_DERIVED
13085 && !arg->sym->ts.u.derived->attr.use_assoc
13086 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13087 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
13088 "PUBLIC interface %qs at %L "
13089 "takes dummy arguments of %qs which "
13090 "is PRIVATE", iface->sym->name,
13091 sym->name, &iface->sym->declared_at,
13092 gfc_typename(&arg->sym->ts)))
13094 /* Stop this message from recurring. */
13095 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13096 return false;
13102 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
13103 && !sym->attr.proc_pointer)
13105 gfc_error ("Function %qs at %L cannot have an initializer",
13106 sym->name, &sym->declared_at);
13108 /* Make sure no second error is issued for this. */
13109 sym->value->error = 1;
13110 return false;
13113 /* An external symbol may not have an initializer because it is taken to be
13114 a procedure. Exception: Procedure Pointers. */
13115 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
13117 gfc_error ("External object %qs at %L may not have an initializer",
13118 sym->name, &sym->declared_at);
13119 return false;
13122 /* An elemental function is required to return a scalar 12.7.1 */
13123 if (sym->attr.elemental && sym->attr.function
13124 && (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)))
13126 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
13127 "result", sym->name, &sym->declared_at);
13128 /* Reset so that the error only occurs once. */
13129 sym->attr.elemental = 0;
13130 return false;
13133 if (sym->attr.proc == PROC_ST_FUNCTION
13134 && (sym->attr.allocatable || sym->attr.pointer))
13136 gfc_error ("Statement function %qs at %L may not have pointer or "
13137 "allocatable attribute", sym->name, &sym->declared_at);
13138 return false;
13141 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
13142 char-len-param shall not be array-valued, pointer-valued, recursive
13143 or pure. ....snip... A character value of * may only be used in the
13144 following ways: (i) Dummy arg of procedure - dummy associates with
13145 actual length; (ii) To declare a named constant; or (iii) External
13146 function - but length must be declared in calling scoping unit. */
13147 if (sym->attr.function
13148 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
13149 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
13151 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
13152 || (sym->attr.recursive) || (sym->attr.pure))
13154 if (sym->as && sym->as->rank)
13155 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13156 "array-valued", sym->name, &sym->declared_at);
13158 if (sym->attr.pointer)
13159 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13160 "pointer-valued", sym->name, &sym->declared_at);
13162 if (sym->attr.pure)
13163 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13164 "pure", sym->name, &sym->declared_at);
13166 if (sym->attr.recursive)
13167 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13168 "recursive", sym->name, &sym->declared_at);
13170 return false;
13173 /* Appendix B.2 of the standard. Contained functions give an
13174 error anyway. Deferred character length is an F2003 feature.
13175 Don't warn on intrinsic conversion functions, which start
13176 with two underscores. */
13177 if (!sym->attr.contained && !sym->ts.deferred
13178 && (sym->name[0] != '_' || sym->name[1] != '_'))
13179 gfc_notify_std (GFC_STD_F95_OBS,
13180 "CHARACTER(*) function %qs at %L",
13181 sym->name, &sym->declared_at);
13184 /* F2008, C1218. */
13185 if (sym->attr.elemental)
13187 if (sym->attr.proc_pointer)
13189 const char* name = (sym->attr.result ? sym->ns->proc_name->name
13190 : sym->name);
13191 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
13192 name, &sym->declared_at);
13193 return false;
13195 if (sym->attr.dummy)
13197 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
13198 sym->name, &sym->declared_at);
13199 return false;
13203 /* F2018, C15100: "The result of an elemental function shall be scalar,
13204 and shall not have the POINTER or ALLOCATABLE attribute." The scalar
13205 pointer is tested and caught elsewhere. */
13206 if (sym->attr.elemental && sym->result
13207 && (sym->result->attr.allocatable || sym->result->attr.pointer))
13209 gfc_error ("Function result variable %qs at %L of elemental "
13210 "function %qs shall not have an ALLOCATABLE or POINTER "
13211 "attribute", sym->result->name,
13212 &sym->result->declared_at, sym->name);
13213 return false;
13216 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
13218 gfc_formal_arglist *curr_arg;
13219 int has_non_interop_arg = 0;
13221 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13222 sym->common_block))
13224 /* Clear these to prevent looking at them again if there was an
13225 error. */
13226 sym->attr.is_bind_c = 0;
13227 sym->attr.is_c_interop = 0;
13228 sym->ts.is_c_interop = 0;
13230 else
13232 /* So far, no errors have been found. */
13233 sym->attr.is_c_interop = 1;
13234 sym->ts.is_c_interop = 1;
13237 curr_arg = gfc_sym_get_dummy_args (sym);
13238 while (curr_arg != NULL)
13240 /* Skip implicitly typed dummy args here. */
13241 if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
13242 if (!gfc_verify_c_interop_param (curr_arg->sym))
13243 /* If something is found to fail, record the fact so we
13244 can mark the symbol for the procedure as not being
13245 BIND(C) to try and prevent multiple errors being
13246 reported. */
13247 has_non_interop_arg = 1;
13249 curr_arg = curr_arg->next;
13252 /* See if any of the arguments were not interoperable and if so, clear
13253 the procedure symbol to prevent duplicate error messages. */
13254 if (has_non_interop_arg != 0)
13256 sym->attr.is_c_interop = 0;
13257 sym->ts.is_c_interop = 0;
13258 sym->attr.is_bind_c = 0;
13262 if (!sym->attr.proc_pointer)
13264 if (sym->attr.save == SAVE_EXPLICIT)
13266 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
13267 "in %qs at %L", sym->name, &sym->declared_at);
13268 return false;
13270 if (sym->attr.intent)
13272 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
13273 "in %qs at %L", sym->name, &sym->declared_at);
13274 return false;
13276 if (sym->attr.subroutine && sym->attr.result)
13278 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
13279 "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
13280 return false;
13282 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
13283 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
13284 || sym->attr.contained))
13286 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
13287 "in %qs at %L", sym->name, &sym->declared_at);
13288 return false;
13290 if (strcmp ("ppr@", sym->name) == 0)
13292 gfc_error ("Procedure pointer result %qs at %L "
13293 "is missing the pointer attribute",
13294 sym->ns->proc_name->name, &sym->declared_at);
13295 return false;
13299 /* Assume that a procedure whose body is not known has references
13300 to external arrays. */
13301 if (sym->attr.if_source != IFSRC_DECL)
13302 sym->attr.array_outer_dependency = 1;
13304 /* Compare the characteristics of a module procedure with the
13305 interface declaration. Ideally this would be done with
13306 gfc_compare_interfaces but, at present, the formal interface
13307 cannot be copied to the ts.interface. */
13308 if (sym->attr.module_procedure
13309 && sym->attr.if_source == IFSRC_DECL)
13311 gfc_symbol *iface;
13312 char name[2*GFC_MAX_SYMBOL_LEN + 1];
13313 char *module_name;
13314 char *submodule_name;
13315 strcpy (name, sym->ns->proc_name->name);
13316 module_name = strtok (name, ".");
13317 submodule_name = strtok (NULL, ".");
13319 iface = sym->tlink;
13320 sym->tlink = NULL;
13322 /* Make sure that the result uses the correct charlen for deferred
13323 length results. */
13324 if (iface && sym->result
13325 && iface->ts.type == BT_CHARACTER
13326 && iface->ts.deferred)
13327 sym->result->ts.u.cl = iface->ts.u.cl;
13329 if (iface == NULL)
13330 goto check_formal;
13332 /* Check the procedure characteristics. */
13333 if (sym->attr.elemental != iface->attr.elemental)
13335 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
13336 "PROCEDURE at %L and its interface in %s",
13337 &sym->declared_at, module_name);
13338 return false;
13341 if (sym->attr.pure != iface->attr.pure)
13343 gfc_error ("Mismatch in PURE attribute between MODULE "
13344 "PROCEDURE at %L and its interface in %s",
13345 &sym->declared_at, module_name);
13346 return false;
13349 if (sym->attr.recursive != iface->attr.recursive)
13351 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
13352 "PROCEDURE at %L and its interface in %s",
13353 &sym->declared_at, module_name);
13354 return false;
13357 /* Check the result characteristics. */
13358 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
13360 gfc_error ("%s between the MODULE PROCEDURE declaration "
13361 "in MODULE %qs and the declaration at %L in "
13362 "(SUB)MODULE %qs",
13363 errmsg, module_name, &sym->declared_at,
13364 submodule_name ? submodule_name : module_name);
13365 return false;
13368 check_formal:
13369 /* Check the characteristics of the formal arguments. */
13370 if (sym->formal && sym->formal_ns)
13372 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
13374 new_formal = arg;
13375 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
13379 return true;
13383 /* Resolve a list of finalizer procedures. That is, after they have hopefully
13384 been defined and we now know their defined arguments, check that they fulfill
13385 the requirements of the standard for procedures used as finalizers. */
13387 static bool
13388 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
13390 gfc_finalizer* list;
13391 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
13392 bool result = true;
13393 bool seen_scalar = false;
13394 gfc_symbol *vtab;
13395 gfc_component *c;
13396 gfc_symbol *parent = gfc_get_derived_super_type (derived);
13398 if (parent)
13399 gfc_resolve_finalizers (parent, finalizable);
13401 /* Ensure that derived-type components have a their finalizers resolved. */
13402 bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
13403 for (c = derived->components; c; c = c->next)
13404 if (c->ts.type == BT_DERIVED
13405 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
13407 bool has_final2 = false;
13408 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
13409 return false; /* Error. */
13410 has_final = has_final || has_final2;
13412 /* Return early if not finalizable. */
13413 if (!has_final)
13415 if (finalizable)
13416 *finalizable = false;
13417 return true;
13420 /* Walk over the list of finalizer-procedures, check them, and if any one
13421 does not fit in with the standard's definition, print an error and remove
13422 it from the list. */
13423 prev_link = &derived->f2k_derived->finalizers;
13424 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
13426 gfc_formal_arglist *dummy_args;
13427 gfc_symbol* arg;
13428 gfc_finalizer* i;
13429 int my_rank;
13431 /* Skip this finalizer if we already resolved it. */
13432 if (list->proc_tree)
13434 if (list->proc_tree->n.sym->formal->sym->as == NULL
13435 || list->proc_tree->n.sym->formal->sym->as->rank == 0)
13436 seen_scalar = true;
13437 prev_link = &(list->next);
13438 continue;
13441 /* Check this exists and is a SUBROUTINE. */
13442 if (!list->proc_sym->attr.subroutine)
13444 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
13445 list->proc_sym->name, &list->where);
13446 goto error;
13449 /* We should have exactly one argument. */
13450 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
13451 if (!dummy_args || dummy_args->next)
13453 gfc_error ("FINAL procedure at %L must have exactly one argument",
13454 &list->where);
13455 goto error;
13457 arg = dummy_args->sym;
13459 /* This argument must be of our type. */
13460 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
13462 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
13463 &arg->declared_at, derived->name);
13464 goto error;
13467 /* It must neither be a pointer nor allocatable nor optional. */
13468 if (arg->attr.pointer)
13470 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
13471 &arg->declared_at);
13472 goto error;
13474 if (arg->attr.allocatable)
13476 gfc_error ("Argument of FINAL procedure at %L must not be"
13477 " ALLOCATABLE", &arg->declared_at);
13478 goto error;
13480 if (arg->attr.optional)
13482 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
13483 &arg->declared_at);
13484 goto error;
13487 /* It must not be INTENT(OUT). */
13488 if (arg->attr.intent == INTENT_OUT)
13490 gfc_error ("Argument of FINAL procedure at %L must not be"
13491 " INTENT(OUT)", &arg->declared_at);
13492 goto error;
13495 /* Warn if the procedure is non-scalar and not assumed shape. */
13496 if (warn_surprising && arg->as && arg->as->rank != 0
13497 && arg->as->type != AS_ASSUMED_SHAPE)
13498 gfc_warning (OPT_Wsurprising,
13499 "Non-scalar FINAL procedure at %L should have assumed"
13500 " shape argument", &arg->declared_at);
13502 /* Check that it does not match in kind and rank with a FINAL procedure
13503 defined earlier. To really loop over the *earlier* declarations,
13504 we need to walk the tail of the list as new ones were pushed at the
13505 front. */
13506 /* TODO: Handle kind parameters once they are implemented. */
13507 my_rank = (arg->as ? arg->as->rank : 0);
13508 for (i = list->next; i; i = i->next)
13510 gfc_formal_arglist *dummy_args;
13512 /* Argument list might be empty; that is an error signalled earlier,
13513 but we nevertheless continued resolving. */
13514 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
13515 if (dummy_args)
13517 gfc_symbol* i_arg = dummy_args->sym;
13518 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
13519 if (i_rank == my_rank)
13521 gfc_error ("FINAL procedure %qs declared at %L has the same"
13522 " rank (%d) as %qs",
13523 list->proc_sym->name, &list->where, my_rank,
13524 i->proc_sym->name);
13525 goto error;
13530 /* Is this the/a scalar finalizer procedure? */
13531 if (my_rank == 0)
13532 seen_scalar = true;
13534 /* Find the symtree for this procedure. */
13535 gcc_assert (!list->proc_tree);
13536 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
13538 prev_link = &list->next;
13539 continue;
13541 /* Remove wrong nodes immediately from the list so we don't risk any
13542 troubles in the future when they might fail later expectations. */
13543 error:
13544 i = list;
13545 *prev_link = list->next;
13546 gfc_free_finalizer (i);
13547 result = false;
13550 if (result == false)
13551 return false;
13553 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
13554 were nodes in the list, must have been for arrays. It is surely a good
13555 idea to have a scalar version there if there's something to finalize. */
13556 if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
13557 gfc_warning (OPT_Wsurprising,
13558 "Only array FINAL procedures declared for derived type %qs"
13559 " defined at %L, suggest also scalar one",
13560 derived->name, &derived->declared_at);
13562 vtab = gfc_find_derived_vtab (derived);
13563 c = vtab->ts.u.derived->components->next->next->next->next->next;
13564 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
13566 if (finalizable)
13567 *finalizable = true;
13569 return true;
13573 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
13575 static bool
13576 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
13577 const char* generic_name, locus where)
13579 gfc_symbol *sym1, *sym2;
13580 const char *pass1, *pass2;
13581 gfc_formal_arglist *dummy_args;
13583 gcc_assert (t1->specific && t2->specific);
13584 gcc_assert (!t1->specific->is_generic);
13585 gcc_assert (!t2->specific->is_generic);
13586 gcc_assert (t1->is_operator == t2->is_operator);
13588 sym1 = t1->specific->u.specific->n.sym;
13589 sym2 = t2->specific->u.specific->n.sym;
13591 if (sym1 == sym2)
13592 return true;
13594 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
13595 if (sym1->attr.subroutine != sym2->attr.subroutine
13596 || sym1->attr.function != sym2->attr.function)
13598 gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
13599 " GENERIC %qs at %L",
13600 sym1->name, sym2->name, generic_name, &where);
13601 return false;
13604 /* Determine PASS arguments. */
13605 if (t1->specific->nopass)
13606 pass1 = NULL;
13607 else if (t1->specific->pass_arg)
13608 pass1 = t1->specific->pass_arg;
13609 else
13611 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
13612 if (dummy_args)
13613 pass1 = dummy_args->sym->name;
13614 else
13615 pass1 = NULL;
13617 if (t2->specific->nopass)
13618 pass2 = NULL;
13619 else if (t2->specific->pass_arg)
13620 pass2 = t2->specific->pass_arg;
13621 else
13623 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
13624 if (dummy_args)
13625 pass2 = dummy_args->sym->name;
13626 else
13627 pass2 = NULL;
13630 /* Compare the interfaces. */
13631 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
13632 NULL, 0, pass1, pass2))
13634 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
13635 sym1->name, sym2->name, generic_name, &where);
13636 return false;
13639 return true;
13643 /* Worker function for resolving a generic procedure binding; this is used to
13644 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
13646 The difference between those cases is finding possible inherited bindings
13647 that are overridden, as one has to look for them in tb_sym_root,
13648 tb_uop_root or tb_op, respectively. Thus the caller must already find
13649 the super-type and set p->overridden correctly. */
13651 static bool
13652 resolve_tb_generic_targets (gfc_symbol* super_type,
13653 gfc_typebound_proc* p, const char* name)
13655 gfc_tbp_generic* target;
13656 gfc_symtree* first_target;
13657 gfc_symtree* inherited;
13659 gcc_assert (p && p->is_generic);
13661 /* Try to find the specific bindings for the symtrees in our target-list. */
13662 gcc_assert (p->u.generic);
13663 for (target = p->u.generic; target; target = target->next)
13664 if (!target->specific)
13666 gfc_typebound_proc* overridden_tbp;
13667 gfc_tbp_generic* g;
13668 const char* target_name;
13670 target_name = target->specific_st->name;
13672 /* Defined for this type directly. */
13673 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
13675 target->specific = target->specific_st->n.tb;
13676 goto specific_found;
13679 /* Look for an inherited specific binding. */
13680 if (super_type)
13682 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
13683 true, NULL);
13685 if (inherited)
13687 gcc_assert (inherited->n.tb);
13688 target->specific = inherited->n.tb;
13689 goto specific_found;
13693 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
13694 " at %L", target_name, name, &p->where);
13695 return false;
13697 /* Once we've found the specific binding, check it is not ambiguous with
13698 other specifics already found or inherited for the same GENERIC. */
13699 specific_found:
13700 gcc_assert (target->specific);
13702 /* This must really be a specific binding! */
13703 if (target->specific->is_generic)
13705 gfc_error ("GENERIC %qs at %L must target a specific binding,"
13706 " %qs is GENERIC, too", name, &p->where, target_name);
13707 return false;
13710 /* Check those already resolved on this type directly. */
13711 for (g = p->u.generic; g; g = g->next)
13712 if (g != target && g->specific
13713 && !check_generic_tbp_ambiguity (target, g, name, p->where))
13714 return false;
13716 /* Check for ambiguity with inherited specific targets. */
13717 for (overridden_tbp = p->overridden; overridden_tbp;
13718 overridden_tbp = overridden_tbp->overridden)
13719 if (overridden_tbp->is_generic)
13721 for (g = overridden_tbp->u.generic; g; g = g->next)
13723 gcc_assert (g->specific);
13724 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
13725 return false;
13730 /* If we attempt to "overwrite" a specific binding, this is an error. */
13731 if (p->overridden && !p->overridden->is_generic)
13733 gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
13734 " the same name", name, &p->where);
13735 return false;
13738 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
13739 all must have the same attributes here. */
13740 first_target = p->u.generic->specific->u.specific;
13741 gcc_assert (first_target);
13742 p->subroutine = first_target->n.sym->attr.subroutine;
13743 p->function = first_target->n.sym->attr.function;
13745 return true;
13749 /* Resolve a GENERIC procedure binding for a derived type. */
13751 static bool
13752 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
13754 gfc_symbol* super_type;
13756 /* Find the overridden binding if any. */
13757 st->n.tb->overridden = NULL;
13758 super_type = gfc_get_derived_super_type (derived);
13759 if (super_type)
13761 gfc_symtree* overridden;
13762 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
13763 true, NULL);
13765 if (overridden && overridden->n.tb)
13766 st->n.tb->overridden = overridden->n.tb;
13769 /* Resolve using worker function. */
13770 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
13774 /* Retrieve the target-procedure of an operator binding and do some checks in
13775 common for intrinsic and user-defined type-bound operators. */
13777 static gfc_symbol*
13778 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
13780 gfc_symbol* target_proc;
13782 gcc_assert (target->specific && !target->specific->is_generic);
13783 target_proc = target->specific->u.specific->n.sym;
13784 gcc_assert (target_proc);
13786 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
13787 if (target->specific->nopass)
13789 gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
13790 return NULL;
13793 return target_proc;
13797 /* Resolve a type-bound intrinsic operator. */
13799 static bool
13800 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
13801 gfc_typebound_proc* p)
13803 gfc_symbol* super_type;
13804 gfc_tbp_generic* target;
13806 /* If there's already an error here, do nothing (but don't fail again). */
13807 if (p->error)
13808 return true;
13810 /* Operators should always be GENERIC bindings. */
13811 gcc_assert (p->is_generic);
13813 /* Look for an overridden binding. */
13814 super_type = gfc_get_derived_super_type (derived);
13815 if (super_type && super_type->f2k_derived)
13816 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
13817 op, true, NULL);
13818 else
13819 p->overridden = NULL;
13821 /* Resolve general GENERIC properties using worker function. */
13822 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
13823 goto error;
13825 /* Check the targets to be procedures of correct interface. */
13826 for (target = p->u.generic; target; target = target->next)
13828 gfc_symbol* target_proc;
13830 target_proc = get_checked_tb_operator_target (target, p->where);
13831 if (!target_proc)
13832 goto error;
13834 if (!gfc_check_operator_interface (target_proc, op, p->where))
13835 goto error;
13837 /* Add target to non-typebound operator list. */
13838 if (!target->specific->deferred && !derived->attr.use_assoc
13839 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
13841 gfc_interface *head, *intr;
13843 /* Preempt 'gfc_check_new_interface' for submodules, where the
13844 mechanism for handling module procedures winds up resolving
13845 operator interfaces twice and would otherwise cause an error. */
13846 for (intr = derived->ns->op[op]; intr; intr = intr->next)
13847 if (intr->sym == target_proc
13848 && target_proc->attr.used_in_submodule)
13849 return true;
13851 if (!gfc_check_new_interface (derived->ns->op[op],
13852 target_proc, p->where))
13853 return false;
13854 head = derived->ns->op[op];
13855 intr = gfc_get_interface ();
13856 intr->sym = target_proc;
13857 intr->where = p->where;
13858 intr->next = head;
13859 derived->ns->op[op] = intr;
13863 return true;
13865 error:
13866 p->error = 1;
13867 return false;
13871 /* Resolve a type-bound user operator (tree-walker callback). */
13873 static gfc_symbol* resolve_bindings_derived;
13874 static bool resolve_bindings_result;
13876 static bool check_uop_procedure (gfc_symbol* sym, locus where);
13878 static void
13879 resolve_typebound_user_op (gfc_symtree* stree)
13881 gfc_symbol* super_type;
13882 gfc_tbp_generic* target;
13884 gcc_assert (stree && stree->n.tb);
13886 if (stree->n.tb->error)
13887 return;
13889 /* Operators should always be GENERIC bindings. */
13890 gcc_assert (stree->n.tb->is_generic);
13892 /* Find overridden procedure, if any. */
13893 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
13894 if (super_type && super_type->f2k_derived)
13896 gfc_symtree* overridden;
13897 overridden = gfc_find_typebound_user_op (super_type, NULL,
13898 stree->name, true, NULL);
13900 if (overridden && overridden->n.tb)
13901 stree->n.tb->overridden = overridden->n.tb;
13903 else
13904 stree->n.tb->overridden = NULL;
13906 /* Resolve basically using worker function. */
13907 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
13908 goto error;
13910 /* Check the targets to be functions of correct interface. */
13911 for (target = stree->n.tb->u.generic; target; target = target->next)
13913 gfc_symbol* target_proc;
13915 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
13916 if (!target_proc)
13917 goto error;
13919 if (!check_uop_procedure (target_proc, stree->n.tb->where))
13920 goto error;
13923 return;
13925 error:
13926 resolve_bindings_result = false;
13927 stree->n.tb->error = 1;
13931 /* Resolve the type-bound procedures for a derived type. */
13933 static void
13934 resolve_typebound_procedure (gfc_symtree* stree)
13936 gfc_symbol* proc;
13937 locus where;
13938 gfc_symbol* me_arg;
13939 gfc_symbol* super_type;
13940 gfc_component* comp;
13942 gcc_assert (stree);
13944 /* Undefined specific symbol from GENERIC target definition. */
13945 if (!stree->n.tb)
13946 return;
13948 if (stree->n.tb->error)
13949 return;
13951 /* If this is a GENERIC binding, use that routine. */
13952 if (stree->n.tb->is_generic)
13954 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
13955 goto error;
13956 return;
13959 /* Get the target-procedure to check it. */
13960 gcc_assert (!stree->n.tb->is_generic);
13961 gcc_assert (stree->n.tb->u.specific);
13962 proc = stree->n.tb->u.specific->n.sym;
13963 where = stree->n.tb->where;
13965 /* Default access should already be resolved from the parser. */
13966 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
13968 if (stree->n.tb->deferred)
13970 if (!check_proc_interface (proc, &where))
13971 goto error;
13973 else
13975 /* If proc has not been resolved at this point, proc->name may
13976 actually be a USE associated entity. See PR fortran/89647. */
13977 if (!proc->resolve_symbol_called
13978 && proc->attr.function == 0 && proc->attr.subroutine == 0)
13980 gfc_symbol *tmp;
13981 gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
13982 if (tmp && tmp->attr.use_assoc)
13984 proc->module = tmp->module;
13985 proc->attr.proc = tmp->attr.proc;
13986 proc->attr.function = tmp->attr.function;
13987 proc->attr.subroutine = tmp->attr.subroutine;
13988 proc->attr.use_assoc = tmp->attr.use_assoc;
13989 proc->ts = tmp->ts;
13990 proc->result = tmp->result;
13994 /* Check for F08:C465. */
13995 if ((!proc->attr.subroutine && !proc->attr.function)
13996 || (proc->attr.proc != PROC_MODULE
13997 && proc->attr.if_source != IFSRC_IFBODY)
13998 || proc->attr.abstract)
14000 gfc_error ("%qs must be a module procedure or an external "
14001 "procedure with an explicit interface at %L",
14002 proc->name, &where);
14003 goto error;
14007 stree->n.tb->subroutine = proc->attr.subroutine;
14008 stree->n.tb->function = proc->attr.function;
14010 /* Find the super-type of the current derived type. We could do this once and
14011 store in a global if speed is needed, but as long as not I believe this is
14012 more readable and clearer. */
14013 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
14015 /* If PASS, resolve and check arguments if not already resolved / loaded
14016 from a .mod file. */
14017 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
14019 gfc_formal_arglist *dummy_args;
14021 dummy_args = gfc_sym_get_dummy_args (proc);
14022 if (stree->n.tb->pass_arg)
14024 gfc_formal_arglist *i;
14026 /* If an explicit passing argument name is given, walk the arg-list
14027 and look for it. */
14029 me_arg = NULL;
14030 stree->n.tb->pass_arg_num = 1;
14031 for (i = dummy_args; i; i = i->next)
14033 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
14035 me_arg = i->sym;
14036 break;
14038 ++stree->n.tb->pass_arg_num;
14041 if (!me_arg)
14043 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
14044 " argument %qs",
14045 proc->name, stree->n.tb->pass_arg, &where,
14046 stree->n.tb->pass_arg);
14047 goto error;
14050 else
14052 /* Otherwise, take the first one; there should in fact be at least
14053 one. */
14054 stree->n.tb->pass_arg_num = 1;
14055 if (!dummy_args)
14057 gfc_error ("Procedure %qs with PASS at %L must have at"
14058 " least one argument", proc->name, &where);
14059 goto error;
14061 me_arg = dummy_args->sym;
14064 /* Now check that the argument-type matches and the passed-object
14065 dummy argument is generally fine. */
14067 gcc_assert (me_arg);
14069 if (me_arg->ts.type != BT_CLASS)
14071 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14072 " at %L", proc->name, &where);
14073 goto error;
14076 if (CLASS_DATA (me_arg)->ts.u.derived
14077 != resolve_bindings_derived)
14079 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14080 " the derived-type %qs", me_arg->name, proc->name,
14081 me_arg->name, &where, resolve_bindings_derived->name);
14082 goto error;
14085 gcc_assert (me_arg->ts.type == BT_CLASS);
14086 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
14088 gfc_error ("Passed-object dummy argument of %qs at %L must be"
14089 " scalar", proc->name, &where);
14090 goto error;
14092 if (CLASS_DATA (me_arg)->attr.allocatable)
14094 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14095 " be ALLOCATABLE", proc->name, &where);
14096 goto error;
14098 if (CLASS_DATA (me_arg)->attr.class_pointer)
14100 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14101 " be POINTER", proc->name, &where);
14102 goto error;
14106 /* If we are extending some type, check that we don't override a procedure
14107 flagged NON_OVERRIDABLE. */
14108 stree->n.tb->overridden = NULL;
14109 if (super_type)
14111 gfc_symtree* overridden;
14112 overridden = gfc_find_typebound_proc (super_type, NULL,
14113 stree->name, true, NULL);
14115 if (overridden)
14117 if (overridden->n.tb)
14118 stree->n.tb->overridden = overridden->n.tb;
14120 if (!gfc_check_typebound_override (stree, overridden))
14121 goto error;
14125 /* See if there's a name collision with a component directly in this type. */
14126 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
14127 if (!strcmp (comp->name, stree->name))
14129 gfc_error ("Procedure %qs at %L has the same name as a component of"
14130 " %qs",
14131 stree->name, &where, resolve_bindings_derived->name);
14132 goto error;
14135 /* Try to find a name collision with an inherited component. */
14136 if (super_type && gfc_find_component (super_type, stree->name, true, true,
14137 NULL))
14139 gfc_error ("Procedure %qs at %L has the same name as an inherited"
14140 " component of %qs",
14141 stree->name, &where, resolve_bindings_derived->name);
14142 goto error;
14145 stree->n.tb->error = 0;
14146 return;
14148 error:
14149 resolve_bindings_result = false;
14150 stree->n.tb->error = 1;
14154 static bool
14155 resolve_typebound_procedures (gfc_symbol* derived)
14157 int op;
14158 gfc_symbol* super_type;
14160 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
14161 return true;
14163 super_type = gfc_get_derived_super_type (derived);
14164 if (super_type)
14165 resolve_symbol (super_type);
14167 resolve_bindings_derived = derived;
14168 resolve_bindings_result = true;
14170 if (derived->f2k_derived->tb_sym_root)
14171 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
14172 &resolve_typebound_procedure);
14174 if (derived->f2k_derived->tb_uop_root)
14175 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
14176 &resolve_typebound_user_op);
14178 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
14180 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
14181 if (p && !resolve_typebound_intrinsic_op (derived,
14182 (gfc_intrinsic_op)op, p))
14183 resolve_bindings_result = false;
14186 return resolve_bindings_result;
14190 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
14191 to give all identical derived types the same backend_decl. */
14192 static void
14193 add_dt_to_dt_list (gfc_symbol *derived)
14195 if (!derived->dt_next)
14197 if (gfc_derived_types)
14199 derived->dt_next = gfc_derived_types->dt_next;
14200 gfc_derived_types->dt_next = derived;
14202 else
14204 derived->dt_next = derived;
14206 gfc_derived_types = derived;
14211 /* Ensure that a derived-type is really not abstract, meaning that every
14212 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
14214 static bool
14215 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
14217 if (!st)
14218 return true;
14220 if (!ensure_not_abstract_walker (sub, st->left))
14221 return false;
14222 if (!ensure_not_abstract_walker (sub, st->right))
14223 return false;
14225 if (st->n.tb && st->n.tb->deferred)
14227 gfc_symtree* overriding;
14228 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
14229 if (!overriding)
14230 return false;
14231 gcc_assert (overriding->n.tb);
14232 if (overriding->n.tb->deferred)
14234 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
14235 " %qs is DEFERRED and not overridden",
14236 sub->name, &sub->declared_at, st->name);
14237 return false;
14241 return true;
14244 static bool
14245 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
14247 /* The algorithm used here is to recursively travel up the ancestry of sub
14248 and for each ancestor-type, check all bindings. If any of them is
14249 DEFERRED, look it up starting from sub and see if the found (overriding)
14250 binding is not DEFERRED.
14251 This is not the most efficient way to do this, but it should be ok and is
14252 clearer than something sophisticated. */
14254 gcc_assert (ancestor && !sub->attr.abstract);
14256 if (!ancestor->attr.abstract)
14257 return true;
14259 /* Walk bindings of this ancestor. */
14260 if (ancestor->f2k_derived)
14262 bool t;
14263 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
14264 if (!t)
14265 return false;
14268 /* Find next ancestor type and recurse on it. */
14269 ancestor = gfc_get_derived_super_type (ancestor);
14270 if (ancestor)
14271 return ensure_not_abstract (sub, ancestor);
14273 return true;
14277 /* This check for typebound defined assignments is done recursively
14278 since the order in which derived types are resolved is not always in
14279 order of the declarations. */
14281 static void
14282 check_defined_assignments (gfc_symbol *derived)
14284 gfc_component *c;
14286 for (c = derived->components; c; c = c->next)
14288 if (!gfc_bt_struct (c->ts.type)
14289 || c->attr.pointer
14290 || c->attr.allocatable
14291 || c->attr.proc_pointer_comp
14292 || c->attr.class_pointer
14293 || c->attr.proc_pointer)
14294 continue;
14296 if (c->ts.u.derived->attr.defined_assign_comp
14297 || (c->ts.u.derived->f2k_derived
14298 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
14300 derived->attr.defined_assign_comp = 1;
14301 return;
14304 check_defined_assignments (c->ts.u.derived);
14305 if (c->ts.u.derived->attr.defined_assign_comp)
14307 derived->attr.defined_assign_comp = 1;
14308 return;
14314 /* Resolve a single component of a derived type or structure. */
14316 static bool
14317 resolve_component (gfc_component *c, gfc_symbol *sym)
14319 gfc_symbol *super_type;
14320 symbol_attribute *attr;
14322 if (c->attr.artificial)
14323 return true;
14325 /* Do not allow vtype components to be resolved in nameless namespaces
14326 such as block data because the procedure pointers will cause ICEs
14327 and vtables are not needed in these contexts. */
14328 if (sym->attr.vtype && sym->attr.use_assoc
14329 && sym->ns->proc_name == NULL)
14330 return true;
14332 /* F2008, C442. */
14333 if ((!sym->attr.is_class || c != sym->components)
14334 && c->attr.codimension
14335 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
14337 gfc_error ("Coarray component %qs at %L must be allocatable with "
14338 "deferred shape", c->name, &c->loc);
14339 return false;
14342 /* F2008, C443. */
14343 if (c->attr.codimension && c->ts.type == BT_DERIVED
14344 && c->ts.u.derived->ts.is_iso_c)
14346 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14347 "shall not be a coarray", c->name, &c->loc);
14348 return false;
14351 /* F2008, C444. */
14352 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
14353 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
14354 || c->attr.allocatable))
14356 gfc_error ("Component %qs at %L with coarray component "
14357 "shall be a nonpointer, nonallocatable scalar",
14358 c->name, &c->loc);
14359 return false;
14362 /* F2008, C448. */
14363 if (c->ts.type == BT_CLASS)
14365 if (CLASS_DATA (c))
14367 attr = &(CLASS_DATA (c)->attr);
14369 /* Fix up contiguous attribute. */
14370 if (c->attr.contiguous)
14371 attr->contiguous = 1;
14373 else
14374 attr = NULL;
14376 else
14377 attr = &c->attr;
14379 if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
14381 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
14382 "is not an array pointer", c->name, &c->loc);
14383 return false;
14386 /* F2003, 15.2.1 - length has to be one. */
14387 if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
14388 && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
14389 || !gfc_is_constant_expr (c->ts.u.cl->length)
14390 || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
14392 gfc_error ("Component %qs of BIND(C) type at %L must have length one",
14393 c->name, &c->loc);
14394 return false;
14397 if (c->attr.proc_pointer && c->ts.interface)
14399 gfc_symbol *ifc = c->ts.interface;
14401 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
14403 c->tb->error = 1;
14404 return false;
14407 if (ifc->attr.if_source || ifc->attr.intrinsic)
14409 /* Resolve interface and copy attributes. */
14410 if (ifc->formal && !ifc->formal_ns)
14411 resolve_symbol (ifc);
14412 if (ifc->attr.intrinsic)
14413 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
14415 if (ifc->result)
14417 c->ts = ifc->result->ts;
14418 c->attr.allocatable = ifc->result->attr.allocatable;
14419 c->attr.pointer = ifc->result->attr.pointer;
14420 c->attr.dimension = ifc->result->attr.dimension;
14421 c->as = gfc_copy_array_spec (ifc->result->as);
14422 c->attr.class_ok = ifc->result->attr.class_ok;
14424 else
14426 c->ts = ifc->ts;
14427 c->attr.allocatable = ifc->attr.allocatable;
14428 c->attr.pointer = ifc->attr.pointer;
14429 c->attr.dimension = ifc->attr.dimension;
14430 c->as = gfc_copy_array_spec (ifc->as);
14431 c->attr.class_ok = ifc->attr.class_ok;
14433 c->ts.interface = ifc;
14434 c->attr.function = ifc->attr.function;
14435 c->attr.subroutine = ifc->attr.subroutine;
14437 c->attr.pure = ifc->attr.pure;
14438 c->attr.elemental = ifc->attr.elemental;
14439 c->attr.recursive = ifc->attr.recursive;
14440 c->attr.always_explicit = ifc->attr.always_explicit;
14441 c->attr.ext_attr |= ifc->attr.ext_attr;
14442 /* Copy char length. */
14443 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
14445 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
14446 if (cl->length && !cl->resolved
14447 && !gfc_resolve_expr (cl->length))
14449 c->tb->error = 1;
14450 return false;
14452 c->ts.u.cl = cl;
14456 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
14458 /* Since PPCs are not implicitly typed, a PPC without an explicit
14459 interface must be a subroutine. */
14460 gfc_add_subroutine (&c->attr, c->name, &c->loc);
14463 /* Procedure pointer components: Check PASS arg. */
14464 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
14465 && !sym->attr.vtype)
14467 gfc_symbol* me_arg;
14469 if (c->tb->pass_arg)
14471 gfc_formal_arglist* i;
14473 /* If an explicit passing argument name is given, walk the arg-list
14474 and look for it. */
14476 me_arg = NULL;
14477 c->tb->pass_arg_num = 1;
14478 for (i = c->ts.interface->formal; i; i = i->next)
14480 if (!strcmp (i->sym->name, c->tb->pass_arg))
14482 me_arg = i->sym;
14483 break;
14485 c->tb->pass_arg_num++;
14488 if (!me_arg)
14490 gfc_error ("Procedure pointer component %qs with PASS(%s) "
14491 "at %L has no argument %qs", c->name,
14492 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
14493 c->tb->error = 1;
14494 return false;
14497 else
14499 /* Otherwise, take the first one; there should in fact be at least
14500 one. */
14501 c->tb->pass_arg_num = 1;
14502 if (!c->ts.interface->formal)
14504 gfc_error ("Procedure pointer component %qs with PASS at %L "
14505 "must have at least one argument",
14506 c->name, &c->loc);
14507 c->tb->error = 1;
14508 return false;
14510 me_arg = c->ts.interface->formal->sym;
14513 /* Now check that the argument-type matches. */
14514 gcc_assert (me_arg);
14515 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
14516 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
14517 || (me_arg->ts.type == BT_CLASS
14518 && CLASS_DATA (me_arg)->ts.u.derived != sym))
14520 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14521 " the derived type %qs", me_arg->name, c->name,
14522 me_arg->name, &c->loc, sym->name);
14523 c->tb->error = 1;
14524 return false;
14527 /* Check for F03:C453. */
14528 if (CLASS_DATA (me_arg)->attr.dimension)
14530 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14531 "must be scalar", me_arg->name, c->name, me_arg->name,
14532 &c->loc);
14533 c->tb->error = 1;
14534 return false;
14537 if (CLASS_DATA (me_arg)->attr.class_pointer)
14539 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14540 "may not have the POINTER attribute", me_arg->name,
14541 c->name, me_arg->name, &c->loc);
14542 c->tb->error = 1;
14543 return false;
14546 if (CLASS_DATA (me_arg)->attr.allocatable)
14548 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14549 "may not be ALLOCATABLE", me_arg->name, c->name,
14550 me_arg->name, &c->loc);
14551 c->tb->error = 1;
14552 return false;
14555 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
14557 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14558 " at %L", c->name, &c->loc);
14559 return false;
14564 /* Check type-spec if this is not the parent-type component. */
14565 if (((sym->attr.is_class
14566 && (!sym->components->ts.u.derived->attr.extension
14567 || c != sym->components->ts.u.derived->components))
14568 || (!sym->attr.is_class
14569 && (!sym->attr.extension || c != sym->components)))
14570 && !sym->attr.vtype
14571 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
14572 return false;
14574 super_type = gfc_get_derived_super_type (sym);
14576 /* If this type is an extension, set the accessibility of the parent
14577 component. */
14578 if (super_type
14579 && ((sym->attr.is_class
14580 && c == sym->components->ts.u.derived->components)
14581 || (!sym->attr.is_class && c == sym->components))
14582 && strcmp (super_type->name, c->name) == 0)
14583 c->attr.access = super_type->attr.access;
14585 /* If this type is an extension, see if this component has the same name
14586 as an inherited type-bound procedure. */
14587 if (super_type && !sym->attr.is_class
14588 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
14590 gfc_error ("Component %qs of %qs at %L has the same name as an"
14591 " inherited type-bound procedure",
14592 c->name, sym->name, &c->loc);
14593 return false;
14596 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
14597 && !c->ts.deferred)
14599 if (c->ts.u.cl->length == NULL
14600 || (!resolve_charlen(c->ts.u.cl))
14601 || !gfc_is_constant_expr (c->ts.u.cl->length))
14603 gfc_error ("Character length of component %qs needs to "
14604 "be a constant specification expression at %L",
14605 c->name,
14606 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
14607 return false;
14611 if (c->ts.type == BT_CHARACTER && c->ts.deferred
14612 && !c->attr.pointer && !c->attr.allocatable)
14614 gfc_error ("Character component %qs of %qs at %L with deferred "
14615 "length must be a POINTER or ALLOCATABLE",
14616 c->name, sym->name, &c->loc);
14617 return false;
14620 /* Add the hidden deferred length field. */
14621 if (c->ts.type == BT_CHARACTER
14622 && (c->ts.deferred || c->attr.pdt_string)
14623 && !c->attr.function
14624 && !sym->attr.is_class)
14626 char name[GFC_MAX_SYMBOL_LEN+9];
14627 gfc_component *strlen;
14628 sprintf (name, "_%s_length", c->name);
14629 strlen = gfc_find_component (sym, name, true, true, NULL);
14630 if (strlen == NULL)
14632 if (!gfc_add_component (sym, name, &strlen))
14633 return false;
14634 strlen->ts.type = BT_INTEGER;
14635 strlen->ts.kind = gfc_charlen_int_kind;
14636 strlen->attr.access = ACCESS_PRIVATE;
14637 strlen->attr.artificial = 1;
14641 if (c->ts.type == BT_DERIVED
14642 && sym->component_access != ACCESS_PRIVATE
14643 && gfc_check_symbol_access (sym)
14644 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
14645 && !c->ts.u.derived->attr.use_assoc
14646 && !gfc_check_symbol_access (c->ts.u.derived)
14647 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
14648 "PRIVATE type and cannot be a component of "
14649 "%qs, which is PUBLIC at %L", c->name,
14650 sym->name, &sym->declared_at))
14651 return false;
14653 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
14655 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
14656 "type %s", c->name, &c->loc, sym->name);
14657 return false;
14660 if (sym->attr.sequence)
14662 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
14664 gfc_error ("Component %s of SEQUENCE type declared at %L does "
14665 "not have the SEQUENCE attribute",
14666 c->ts.u.derived->name, &sym->declared_at);
14667 return false;
14671 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
14672 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
14673 else if (c->ts.type == BT_CLASS && c->attr.class_ok
14674 && CLASS_DATA (c)->ts.u.derived->attr.generic)
14675 CLASS_DATA (c)->ts.u.derived
14676 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
14678 /* If an allocatable component derived type is of the same type as
14679 the enclosing derived type, we need a vtable generating so that
14680 the __deallocate procedure is created. */
14681 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
14682 && c->ts.u.derived == sym && c->attr.allocatable == 1)
14683 gfc_find_vtab (&c->ts);
14685 /* Ensure that all the derived type components are put on the
14686 derived type list; even in formal namespaces, where derived type
14687 pointer components might not have been declared. */
14688 if (c->ts.type == BT_DERIVED
14689 && c->ts.u.derived
14690 && c->ts.u.derived->components
14691 && c->attr.pointer
14692 && sym != c->ts.u.derived)
14693 add_dt_to_dt_list (c->ts.u.derived);
14695 if (!gfc_resolve_array_spec (c->as,
14696 !(c->attr.pointer || c->attr.proc_pointer
14697 || c->attr.allocatable)))
14698 return false;
14700 if (c->initializer && !sym->attr.vtype
14701 && !c->attr.pdt_kind && !c->attr.pdt_len
14702 && !gfc_check_assign_symbol (sym, c, c->initializer))
14703 return false;
14705 return true;
14709 /* Be nice about the locus for a structure expression - show the locus of the
14710 first non-null sub-expression if we can. */
14712 static locus *
14713 cons_where (gfc_expr *struct_expr)
14715 gfc_constructor *cons;
14717 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
14719 cons = gfc_constructor_first (struct_expr->value.constructor);
14720 for (; cons; cons = gfc_constructor_next (cons))
14722 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
14723 return &cons->expr->where;
14726 return &struct_expr->where;
14729 /* Resolve the components of a structure type. Much less work than derived
14730 types. */
14732 static bool
14733 resolve_fl_struct (gfc_symbol *sym)
14735 gfc_component *c;
14736 gfc_expr *init = NULL;
14737 bool success;
14739 /* Make sure UNIONs do not have overlapping initializers. */
14740 if (sym->attr.flavor == FL_UNION)
14742 for (c = sym->components; c; c = c->next)
14744 if (init && c->initializer)
14746 gfc_error ("Conflicting initializers in union at %L and %L",
14747 cons_where (init), cons_where (c->initializer));
14748 gfc_free_expr (c->initializer);
14749 c->initializer = NULL;
14751 if (init == NULL)
14752 init = c->initializer;
14756 success = true;
14757 for (c = sym->components; c; c = c->next)
14758 if (!resolve_component (c, sym))
14759 success = false;
14761 if (!success)
14762 return false;
14764 if (sym->components)
14765 add_dt_to_dt_list (sym);
14767 return true;
14771 /* Resolve the components of a derived type. This does not have to wait until
14772 resolution stage, but can be done as soon as the dt declaration has been
14773 parsed. */
14775 static bool
14776 resolve_fl_derived0 (gfc_symbol *sym)
14778 gfc_symbol* super_type;
14779 gfc_component *c;
14780 gfc_formal_arglist *f;
14781 bool success;
14783 if (sym->attr.unlimited_polymorphic)
14784 return true;
14786 super_type = gfc_get_derived_super_type (sym);
14788 /* F2008, C432. */
14789 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
14791 gfc_error ("As extending type %qs at %L has a coarray component, "
14792 "parent type %qs shall also have one", sym->name,
14793 &sym->declared_at, super_type->name);
14794 return false;
14797 /* Ensure the extended type gets resolved before we do. */
14798 if (super_type && !resolve_fl_derived0 (super_type))
14799 return false;
14801 /* An ABSTRACT type must be extensible. */
14802 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
14804 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
14805 sym->name, &sym->declared_at);
14806 return false;
14809 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
14810 : sym->components;
14812 success = true;
14813 for ( ; c != NULL; c = c->next)
14814 if (!resolve_component (c, sym))
14815 success = false;
14817 if (!success)
14818 return false;
14820 /* Now add the caf token field, where needed. */
14821 if (flag_coarray != GFC_FCOARRAY_NONE
14822 && !sym->attr.is_class && !sym->attr.vtype)
14824 for (c = sym->components; c; c = c->next)
14825 if (!c->attr.dimension && !c->attr.codimension
14826 && (c->attr.allocatable || c->attr.pointer))
14828 char name[GFC_MAX_SYMBOL_LEN+9];
14829 gfc_component *token;
14830 sprintf (name, "_caf_%s", c->name);
14831 token = gfc_find_component (sym, name, true, true, NULL);
14832 if (token == NULL)
14834 if (!gfc_add_component (sym, name, &token))
14835 return false;
14836 token->ts.type = BT_VOID;
14837 token->ts.kind = gfc_default_integer_kind;
14838 token->attr.access = ACCESS_PRIVATE;
14839 token->attr.artificial = 1;
14840 token->attr.caf_token = 1;
14845 check_defined_assignments (sym);
14847 if (!sym->attr.defined_assign_comp && super_type)
14848 sym->attr.defined_assign_comp
14849 = super_type->attr.defined_assign_comp;
14851 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
14852 all DEFERRED bindings are overridden. */
14853 if (super_type && super_type->attr.abstract && !sym->attr.abstract
14854 && !sym->attr.is_class
14855 && !ensure_not_abstract (sym, super_type))
14856 return false;
14858 /* Check that there is a component for every PDT parameter. */
14859 if (sym->attr.pdt_template)
14861 for (f = sym->formal; f; f = f->next)
14863 if (!f->sym)
14864 continue;
14865 c = gfc_find_component (sym, f->sym->name, true, true, NULL);
14866 if (c == NULL)
14868 gfc_error ("Parameterized type %qs does not have a component "
14869 "corresponding to parameter %qs at %L", sym->name,
14870 f->sym->name, &sym->declared_at);
14871 break;
14876 /* Add derived type to the derived type list. */
14877 add_dt_to_dt_list (sym);
14879 return true;
14883 /* The following procedure does the full resolution of a derived type,
14884 including resolution of all type-bound procedures (if present). In contrast
14885 to 'resolve_fl_derived0' this can only be done after the module has been
14886 parsed completely. */
14888 static bool
14889 resolve_fl_derived (gfc_symbol *sym)
14891 gfc_symbol *gen_dt = NULL;
14893 if (sym->attr.unlimited_polymorphic)
14894 return true;
14896 if (!sym->attr.is_class)
14897 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
14898 if (gen_dt && gen_dt->generic && gen_dt->generic->next
14899 && (!gen_dt->generic->sym->attr.use_assoc
14900 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
14901 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
14902 "%qs at %L being the same name as derived "
14903 "type at %L", sym->name,
14904 gen_dt->generic->sym == sym
14905 ? gen_dt->generic->next->sym->name
14906 : gen_dt->generic->sym->name,
14907 gen_dt->generic->sym == sym
14908 ? &gen_dt->generic->next->sym->declared_at
14909 : &gen_dt->generic->sym->declared_at,
14910 &sym->declared_at))
14911 return false;
14913 if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
14915 gfc_error ("Derived type %qs at %L has not been declared",
14916 sym->name, &sym->declared_at);
14917 return false;
14920 /* Resolve the finalizer procedures. */
14921 if (!gfc_resolve_finalizers (sym, NULL))
14922 return false;
14924 if (sym->attr.is_class && sym->ts.u.derived == NULL)
14926 /* Fix up incomplete CLASS symbols. */
14927 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
14928 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
14930 /* Nothing more to do for unlimited polymorphic entities. */
14931 if (data->ts.u.derived->attr.unlimited_polymorphic)
14932 return true;
14933 else if (vptr->ts.u.derived == NULL)
14935 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
14936 gcc_assert (vtab);
14937 vptr->ts.u.derived = vtab->ts.u.derived;
14938 if (!resolve_fl_derived0 (vptr->ts.u.derived))
14939 return false;
14943 if (!resolve_fl_derived0 (sym))
14944 return false;
14946 /* Resolve the type-bound procedures. */
14947 if (!resolve_typebound_procedures (sym))
14948 return false;
14950 /* Generate module vtables subject to their accessibility and their not
14951 being vtables or pdt templates. If this is not done class declarations
14952 in external procedures wind up with their own version and so SELECT TYPE
14953 fails because the vptrs do not have the same address. */
14954 if (gfc_option.allow_std & GFC_STD_F2003
14955 && sym->ns->proc_name
14956 && sym->ns->proc_name->attr.flavor == FL_MODULE
14957 && sym->attr.access != ACCESS_PRIVATE
14958 && !(sym->attr.use_assoc || sym->attr.vtype || sym->attr.pdt_template))
14960 gfc_symbol *vtab = gfc_find_derived_vtab (sym);
14961 gfc_set_sym_referenced (vtab);
14964 return true;
14968 static bool
14969 resolve_fl_namelist (gfc_symbol *sym)
14971 gfc_namelist *nl;
14972 gfc_symbol *nlsym;
14974 for (nl = sym->namelist; nl; nl = nl->next)
14976 /* Check again, the check in match only works if NAMELIST comes
14977 after the decl. */
14978 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
14980 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
14981 "allowed", nl->sym->name, sym->name, &sym->declared_at);
14982 return false;
14985 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
14986 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
14987 "with assumed shape in namelist %qs at %L",
14988 nl->sym->name, sym->name, &sym->declared_at))
14989 return false;
14991 if (is_non_constant_shape_array (nl->sym)
14992 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
14993 "with nonconstant shape in namelist %qs at %L",
14994 nl->sym->name, sym->name, &sym->declared_at))
14995 return false;
14997 if (nl->sym->ts.type == BT_CHARACTER
14998 && (nl->sym->ts.u.cl->length == NULL
14999 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
15000 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
15001 "nonconstant character length in "
15002 "namelist %qs at %L", nl->sym->name,
15003 sym->name, &sym->declared_at))
15004 return false;
15008 /* Reject PRIVATE objects in a PUBLIC namelist. */
15009 if (gfc_check_symbol_access (sym))
15011 for (nl = sym->namelist; nl; nl = nl->next)
15013 if (!nl->sym->attr.use_assoc
15014 && !is_sym_host_assoc (nl->sym, sym->ns)
15015 && !gfc_check_symbol_access (nl->sym))
15017 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
15018 "cannot be member of PUBLIC namelist %qs at %L",
15019 nl->sym->name, sym->name, &sym->declared_at);
15020 return false;
15023 if (nl->sym->ts.type == BT_DERIVED
15024 && (nl->sym->ts.u.derived->attr.alloc_comp
15025 || nl->sym->ts.u.derived->attr.pointer_comp))
15027 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
15028 "namelist %qs at %L with ALLOCATABLE "
15029 "or POINTER components", nl->sym->name,
15030 sym->name, &sym->declared_at))
15031 return false;
15032 return true;
15035 /* Types with private components that came here by USE-association. */
15036 if (nl->sym->ts.type == BT_DERIVED
15037 && derived_inaccessible (nl->sym->ts.u.derived))
15039 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
15040 "components and cannot be member of namelist %qs at %L",
15041 nl->sym->name, sym->name, &sym->declared_at);
15042 return false;
15045 /* Types with private components that are defined in the same module. */
15046 if (nl->sym->ts.type == BT_DERIVED
15047 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
15048 && nl->sym->ts.u.derived->attr.private_comp)
15050 gfc_error ("NAMELIST object %qs has PRIVATE components and "
15051 "cannot be a member of PUBLIC namelist %qs at %L",
15052 nl->sym->name, sym->name, &sym->declared_at);
15053 return false;
15059 /* 14.1.2 A module or internal procedure represent local entities
15060 of the same type as a namelist member and so are not allowed. */
15061 for (nl = sym->namelist; nl; nl = nl->next)
15063 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
15064 continue;
15066 if (nl->sym->attr.function && nl->sym == nl->sym->result)
15067 if ((nl->sym == sym->ns->proc_name)
15069 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
15070 continue;
15072 nlsym = NULL;
15073 if (nl->sym->name)
15074 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
15075 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
15077 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
15078 "attribute in %qs at %L", nlsym->name,
15079 &sym->declared_at);
15080 return false;
15084 return true;
15088 static bool
15089 resolve_fl_parameter (gfc_symbol *sym)
15091 /* A parameter array's shape needs to be constant. */
15092 if (sym->as != NULL
15093 && (sym->as->type == AS_DEFERRED
15094 || is_non_constant_shape_array (sym)))
15096 gfc_error ("Parameter array %qs at %L cannot be automatic "
15097 "or of deferred shape", sym->name, &sym->declared_at);
15098 return false;
15101 /* Constraints on deferred type parameter. */
15102 if (!deferred_requirements (sym))
15103 return false;
15105 /* Make sure a parameter that has been implicitly typed still
15106 matches the implicit type, since PARAMETER statements can precede
15107 IMPLICIT statements. */
15108 if (sym->attr.implicit_type
15109 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
15110 sym->ns)))
15112 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
15113 "later IMPLICIT type", sym->name, &sym->declared_at);
15114 return false;
15117 /* Make sure the types of derived parameters are consistent. This
15118 type checking is deferred until resolution because the type may
15119 refer to a derived type from the host. */
15120 if (sym->ts.type == BT_DERIVED
15121 && !gfc_compare_types (&sym->ts, &sym->value->ts))
15123 gfc_error ("Incompatible derived type in PARAMETER at %L",
15124 &sym->value->where);
15125 return false;
15128 /* F03:C509,C514. */
15129 if (sym->ts.type == BT_CLASS)
15131 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
15132 sym->name, &sym->declared_at);
15133 return false;
15136 return true;
15140 /* Called by resolve_symbol to check PDTs. */
15142 static void
15143 resolve_pdt (gfc_symbol* sym)
15145 gfc_symbol *derived = NULL;
15146 gfc_actual_arglist *param;
15147 gfc_component *c;
15148 bool const_len_exprs = true;
15149 bool assumed_len_exprs = false;
15150 symbol_attribute *attr;
15152 if (sym->ts.type == BT_DERIVED)
15154 derived = sym->ts.u.derived;
15155 attr = &(sym->attr);
15157 else if (sym->ts.type == BT_CLASS)
15159 derived = CLASS_DATA (sym)->ts.u.derived;
15160 attr = &(CLASS_DATA (sym)->attr);
15162 else
15163 gcc_unreachable ();
15165 gcc_assert (derived->attr.pdt_type);
15167 for (param = sym->param_list; param; param = param->next)
15169 c = gfc_find_component (derived, param->name, false, true, NULL);
15170 gcc_assert (c);
15171 if (c->attr.pdt_kind)
15172 continue;
15174 if (param->expr && !gfc_is_constant_expr (param->expr)
15175 && c->attr.pdt_len)
15176 const_len_exprs = false;
15177 else if (param->spec_type == SPEC_ASSUMED)
15178 assumed_len_exprs = true;
15180 if (param->spec_type == SPEC_DEFERRED
15181 && !attr->allocatable && !attr->pointer)
15182 gfc_error ("The object %qs at %L has a deferred LEN "
15183 "parameter %qs and is neither allocatable "
15184 "nor a pointer", sym->name, &sym->declared_at,
15185 param->name);
15189 if (!const_len_exprs
15190 && (sym->ns->proc_name->attr.is_main_program
15191 || sym->ns->proc_name->attr.flavor == FL_MODULE
15192 || sym->attr.save != SAVE_NONE))
15193 gfc_error ("The AUTOMATIC object %qs at %L must not have the "
15194 "SAVE attribute or be a variable declared in the "
15195 "main program, a module or a submodule(F08/C513)",
15196 sym->name, &sym->declared_at);
15198 if (assumed_len_exprs && !(sym->attr.dummy
15199 || sym->attr.select_type_temporary || sym->attr.associate_var))
15200 gfc_error ("The object %qs at %L with ASSUMED type parameters "
15201 "must be a dummy or a SELECT TYPE selector(F08/4.2)",
15202 sym->name, &sym->declared_at);
15206 /* Do anything necessary to resolve a symbol. Right now, we just
15207 assume that an otherwise unknown symbol is a variable. This sort
15208 of thing commonly happens for symbols in module. */
15210 static void
15211 resolve_symbol (gfc_symbol *sym)
15213 int check_constant, mp_flag;
15214 gfc_symtree *symtree;
15215 gfc_symtree *this_symtree;
15216 gfc_namespace *ns;
15217 gfc_component *c;
15218 symbol_attribute class_attr;
15219 gfc_array_spec *as;
15220 bool saved_specification_expr;
15222 if (sym->resolve_symbol_called >= 1)
15223 return;
15224 sym->resolve_symbol_called = 1;
15226 /* No symbol will ever have union type; only components can be unions.
15227 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
15228 (just like derived type declaration symbols have flavor FL_DERIVED). */
15229 gcc_assert (sym->ts.type != BT_UNION);
15231 /* Coarrayed polymorphic objects with allocatable or pointer components are
15232 yet unsupported for -fcoarray=lib. */
15233 if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
15234 && sym->ts.u.derived && CLASS_DATA (sym)
15235 && CLASS_DATA (sym)->attr.codimension
15236 && CLASS_DATA (sym)->ts.u.derived
15237 && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
15238 || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
15240 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
15241 "type coarrays at %L are unsupported", &sym->declared_at);
15242 return;
15245 if (sym->attr.artificial)
15246 return;
15248 if (sym->attr.unlimited_polymorphic)
15249 return;
15251 if (sym->attr.flavor == FL_UNKNOWN
15252 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
15253 && !sym->attr.generic && !sym->attr.external
15254 && sym->attr.if_source == IFSRC_UNKNOWN
15255 && sym->ts.type == BT_UNKNOWN))
15258 /* If we find that a flavorless symbol is an interface in one of the
15259 parent namespaces, find its symtree in this namespace, free the
15260 symbol and set the symtree to point to the interface symbol. */
15261 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
15263 symtree = gfc_find_symtree (ns->sym_root, sym->name);
15264 if (symtree && (symtree->n.sym->generic ||
15265 (symtree->n.sym->attr.flavor == FL_PROCEDURE
15266 && sym->ns->construct_entities)))
15268 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
15269 sym->name);
15270 if (this_symtree->n.sym == sym)
15272 symtree->n.sym->refs++;
15273 gfc_release_symbol (sym);
15274 this_symtree->n.sym = symtree->n.sym;
15275 return;
15280 /* Otherwise give it a flavor according to such attributes as
15281 it has. */
15282 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
15283 && sym->attr.intrinsic == 0)
15284 sym->attr.flavor = FL_VARIABLE;
15285 else if (sym->attr.flavor == FL_UNKNOWN)
15287 sym->attr.flavor = FL_PROCEDURE;
15288 if (sym->attr.dimension)
15289 sym->attr.function = 1;
15293 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
15294 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
15296 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
15297 && !resolve_procedure_interface (sym))
15298 return;
15300 if (sym->attr.is_protected && !sym->attr.proc_pointer
15301 && (sym->attr.procedure || sym->attr.external))
15303 if (sym->attr.external)
15304 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
15305 "at %L", &sym->declared_at);
15306 else
15307 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
15308 "at %L", &sym->declared_at);
15310 return;
15313 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
15314 return;
15316 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
15317 && !resolve_fl_struct (sym))
15318 return;
15320 /* Symbols that are module procedures with results (functions) have
15321 the types and array specification copied for type checking in
15322 procedures that call them, as well as for saving to a module
15323 file. These symbols can't stand the scrutiny that their results
15324 can. */
15325 mp_flag = (sym->result != NULL && sym->result != sym);
15327 /* Make sure that the intrinsic is consistent with its internal
15328 representation. This needs to be done before assigning a default
15329 type to avoid spurious warnings. */
15330 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
15331 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
15332 return;
15334 /* Resolve associate names. */
15335 if (sym->assoc)
15336 resolve_assoc_var (sym, true);
15338 /* Assign default type to symbols that need one and don't have one. */
15339 if (sym->ts.type == BT_UNKNOWN)
15341 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
15343 gfc_set_default_type (sym, 1, NULL);
15346 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
15347 && !sym->attr.function && !sym->attr.subroutine
15348 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
15349 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
15351 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15353 /* The specific case of an external procedure should emit an error
15354 in the case that there is no implicit type. */
15355 if (!mp_flag)
15357 if (!sym->attr.mixed_entry_master)
15358 gfc_set_default_type (sym, sym->attr.external, NULL);
15360 else
15362 /* Result may be in another namespace. */
15363 resolve_symbol (sym->result);
15365 if (!sym->result->attr.proc_pointer)
15367 sym->ts = sym->result->ts;
15368 sym->as = gfc_copy_array_spec (sym->result->as);
15369 sym->attr.dimension = sym->result->attr.dimension;
15370 sym->attr.pointer = sym->result->attr.pointer;
15371 sym->attr.allocatable = sym->result->attr.allocatable;
15372 sym->attr.contiguous = sym->result->attr.contiguous;
15377 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15379 bool saved_specification_expr = specification_expr;
15380 specification_expr = true;
15381 gfc_resolve_array_spec (sym->result->as, false);
15382 specification_expr = saved_specification_expr;
15385 if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived)
15387 as = CLASS_DATA (sym)->as;
15388 class_attr = CLASS_DATA (sym)->attr;
15389 class_attr.pointer = class_attr.class_pointer;
15391 else
15393 class_attr = sym->attr;
15394 as = sym->as;
15397 /* F2008, C530. */
15398 if (sym->attr.contiguous
15399 && (!class_attr.dimension
15400 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
15401 && !class_attr.pointer)))
15403 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
15404 "array pointer or an assumed-shape or assumed-rank array",
15405 sym->name, &sym->declared_at);
15406 return;
15409 /* Assumed size arrays and assumed shape arrays must be dummy
15410 arguments. Array-spec's of implied-shape should have been resolved to
15411 AS_EXPLICIT already. */
15413 if (as)
15415 /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
15416 specification expression. */
15417 if (as->type == AS_IMPLIED_SHAPE)
15419 int i;
15420 for (i=0; i<as->rank; i++)
15422 if (as->lower[i] != NULL && as->upper[i] == NULL)
15424 gfc_error ("Bad specification for assumed size array at %L",
15425 &as->lower[i]->where);
15426 return;
15429 gcc_unreachable();
15432 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
15433 || as->type == AS_ASSUMED_SHAPE)
15434 && !sym->attr.dummy && !sym->attr.select_type_temporary)
15436 if (as->type == AS_ASSUMED_SIZE)
15437 gfc_error ("Assumed size array at %L must be a dummy argument",
15438 &sym->declared_at);
15439 else
15440 gfc_error ("Assumed shape array at %L must be a dummy argument",
15441 &sym->declared_at);
15442 return;
15444 /* TS 29113, C535a. */
15445 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
15446 && !sym->attr.select_type_temporary
15447 && !(cs_base && cs_base->current
15448 && cs_base->current->op == EXEC_SELECT_RANK))
15450 gfc_error ("Assumed-rank array at %L must be a dummy argument",
15451 &sym->declared_at);
15452 return;
15454 if (as->type == AS_ASSUMED_RANK
15455 && (sym->attr.codimension || sym->attr.value))
15457 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
15458 "CODIMENSION attribute", &sym->declared_at);
15459 return;
15463 /* Make sure symbols with known intent or optional are really dummy
15464 variable. Because of ENTRY statement, this has to be deferred
15465 until resolution time. */
15467 if (!sym->attr.dummy
15468 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
15470 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
15471 return;
15474 if (sym->attr.value && !sym->attr.dummy)
15476 gfc_error ("%qs at %L cannot have the VALUE attribute because "
15477 "it is not a dummy argument", sym->name, &sym->declared_at);
15478 return;
15481 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
15483 gfc_charlen *cl = sym->ts.u.cl;
15484 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
15486 gfc_error ("Character dummy variable %qs at %L with VALUE "
15487 "attribute must have constant length",
15488 sym->name, &sym->declared_at);
15489 return;
15492 if (sym->ts.is_c_interop
15493 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
15495 gfc_error ("C interoperable character dummy variable %qs at %L "
15496 "with VALUE attribute must have length one",
15497 sym->name, &sym->declared_at);
15498 return;
15502 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
15503 && sym->ts.u.derived->attr.generic)
15505 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
15506 if (!sym->ts.u.derived)
15508 gfc_error ("The derived type %qs at %L is of type %qs, "
15509 "which has not been defined", sym->name,
15510 &sym->declared_at, sym->ts.u.derived->name);
15511 sym->ts.type = BT_UNKNOWN;
15512 return;
15516 /* Use the same constraints as TYPE(*), except for the type check
15517 and that only scalars and assumed-size arrays are permitted. */
15518 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
15520 if (!sym->attr.dummy)
15522 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15523 "a dummy argument", sym->name, &sym->declared_at);
15524 return;
15527 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
15528 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
15529 && sym->ts.type != BT_COMPLEX)
15531 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15532 "of type TYPE(*) or of an numeric intrinsic type",
15533 sym->name, &sym->declared_at);
15534 return;
15537 if (sym->attr.allocatable || sym->attr.codimension
15538 || sym->attr.pointer || sym->attr.value)
15540 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15541 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
15542 "attribute", sym->name, &sym->declared_at);
15543 return;
15546 if (sym->attr.intent == INTENT_OUT)
15548 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15549 "have the INTENT(OUT) attribute",
15550 sym->name, &sym->declared_at);
15551 return;
15553 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
15555 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
15556 "either be a scalar or an assumed-size array",
15557 sym->name, &sym->declared_at);
15558 return;
15561 /* Set the type to TYPE(*) and add a dimension(*) to ensure
15562 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
15563 packing. */
15564 sym->ts.type = BT_ASSUMED;
15565 sym->as = gfc_get_array_spec ();
15566 sym->as->type = AS_ASSUMED_SIZE;
15567 sym->as->rank = 1;
15568 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
15570 else if (sym->ts.type == BT_ASSUMED)
15572 /* TS 29113, C407a. */
15573 if (!sym->attr.dummy)
15575 gfc_error ("Assumed type of variable %s at %L is only permitted "
15576 "for dummy variables", sym->name, &sym->declared_at);
15577 return;
15579 if (sym->attr.allocatable || sym->attr.codimension
15580 || sym->attr.pointer || sym->attr.value)
15582 gfc_error ("Assumed-type variable %s at %L may not have the "
15583 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
15584 sym->name, &sym->declared_at);
15585 return;
15587 if (sym->attr.intent == INTENT_OUT)
15589 gfc_error ("Assumed-type variable %s at %L may not have the "
15590 "INTENT(OUT) attribute",
15591 sym->name, &sym->declared_at);
15592 return;
15594 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
15596 gfc_error ("Assumed-type variable %s at %L shall not be an "
15597 "explicit-shape array", sym->name, &sym->declared_at);
15598 return;
15602 /* If the symbol is marked as bind(c), that it is declared at module level
15603 scope and verify its type and kind. Do not do the latter for symbols
15604 that are implicitly typed because that is handled in
15605 gfc_set_default_type. Handle dummy arguments and procedure definitions
15606 separately. Also, anything that is use associated is not handled here
15607 but instead is handled in the module it is declared in. Finally, derived
15608 type definitions are allowed to be BIND(C) since that only implies that
15609 they're interoperable, and they are checked fully for interoperability
15610 when a variable is declared of that type. */
15611 if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
15612 && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
15613 && sym->attr.flavor != FL_DERIVED)
15615 bool t = true;
15617 /* First, make sure the variable is declared at the
15618 module-level scope (J3/04-007, Section 15.3). */
15619 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
15620 sym->attr.in_common == 0)
15622 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
15623 "is neither a COMMON block nor declared at the "
15624 "module level scope", sym->name, &(sym->declared_at));
15625 t = false;
15627 else if (sym->ts.type == BT_CHARACTER
15628 && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
15629 || !gfc_is_constant_expr (sym->ts.u.cl->length)
15630 || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
15632 gfc_error ("BIND(C) Variable %qs at %L must have length one",
15633 sym->name, &sym->declared_at);
15634 t = false;
15636 else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
15638 t = verify_com_block_vars_c_interop (sym->common_head);
15640 else if (sym->attr.implicit_type == 0)
15642 /* If type() declaration, we need to verify that the components
15643 of the given type are all C interoperable, etc. */
15644 if (sym->ts.type == BT_DERIVED &&
15645 sym->ts.u.derived->attr.is_c_interop != 1)
15647 /* Make sure the user marked the derived type as BIND(C). If
15648 not, call the verify routine. This could print an error
15649 for the derived type more than once if multiple variables
15650 of that type are declared. */
15651 if (sym->ts.u.derived->attr.is_bind_c != 1)
15652 verify_bind_c_derived_type (sym->ts.u.derived);
15653 t = false;
15656 /* Verify the variable itself as C interoperable if it
15657 is BIND(C). It is not possible for this to succeed if
15658 the verify_bind_c_derived_type failed, so don't have to handle
15659 any error returned by verify_bind_c_derived_type. */
15660 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
15661 sym->common_block);
15664 if (!t)
15666 /* clear the is_bind_c flag to prevent reporting errors more than
15667 once if something failed. */
15668 sym->attr.is_bind_c = 0;
15669 return;
15673 /* If a derived type symbol has reached this point, without its
15674 type being declared, we have an error. Notice that most
15675 conditions that produce undefined derived types have already
15676 been dealt with. However, the likes of:
15677 implicit type(t) (t) ..... call foo (t) will get us here if
15678 the type is not declared in the scope of the implicit
15679 statement. Change the type to BT_UNKNOWN, both because it is so
15680 and to prevent an ICE. */
15681 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
15682 && sym->ts.u.derived->components == NULL
15683 && !sym->ts.u.derived->attr.zero_comp)
15685 gfc_error ("The derived type %qs at %L is of type %qs, "
15686 "which has not been defined", sym->name,
15687 &sym->declared_at, sym->ts.u.derived->name);
15688 sym->ts.type = BT_UNKNOWN;
15689 return;
15692 /* Make sure that the derived type has been resolved and that the
15693 derived type is visible in the symbol's namespace, if it is a
15694 module function and is not PRIVATE. */
15695 if (sym->ts.type == BT_DERIVED
15696 && sym->ts.u.derived->attr.use_assoc
15697 && sym->ns->proc_name
15698 && sym->ns->proc_name->attr.flavor == FL_MODULE
15699 && !resolve_fl_derived (sym->ts.u.derived))
15700 return;
15702 /* Unless the derived-type declaration is use associated, Fortran 95
15703 does not allow public entries of private derived types.
15704 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
15705 161 in 95-006r3. */
15706 if (sym->ts.type == BT_DERIVED
15707 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
15708 && !sym->ts.u.derived->attr.use_assoc
15709 && gfc_check_symbol_access (sym)
15710 && !gfc_check_symbol_access (sym->ts.u.derived)
15711 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
15712 "derived type %qs",
15713 (sym->attr.flavor == FL_PARAMETER)
15714 ? "parameter" : "variable",
15715 sym->name, &sym->declared_at,
15716 sym->ts.u.derived->name))
15717 return;
15719 /* F2008, C1302. */
15720 if (sym->ts.type == BT_DERIVED
15721 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
15722 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
15723 || sym->ts.u.derived->attr.lock_comp)
15724 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
15726 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
15727 "type LOCK_TYPE must be a coarray", sym->name,
15728 &sym->declared_at);
15729 return;
15732 /* TS18508, C702/C703. */
15733 if (sym->ts.type == BT_DERIVED
15734 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
15735 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
15736 || sym->ts.u.derived->attr.event_comp)
15737 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
15739 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
15740 "type EVENT_TYPE must be a coarray", sym->name,
15741 &sym->declared_at);
15742 return;
15745 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
15746 default initialization is defined (5.1.2.4.4). */
15747 if (sym->ts.type == BT_DERIVED
15748 && sym->attr.dummy
15749 && sym->attr.intent == INTENT_OUT
15750 && sym->as
15751 && sym->as->type == AS_ASSUMED_SIZE)
15753 for (c = sym->ts.u.derived->components; c; c = c->next)
15755 if (c->initializer)
15757 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
15758 "ASSUMED SIZE and so cannot have a default initializer",
15759 sym->name, &sym->declared_at);
15760 return;
15765 /* F2008, C542. */
15766 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
15767 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
15769 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
15770 "INTENT(OUT)", sym->name, &sym->declared_at);
15771 return;
15774 /* TS18508. */
15775 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
15776 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
15778 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
15779 "INTENT(OUT)", sym->name, &sym->declared_at);
15780 return;
15783 /* F2008, C525. */
15784 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15785 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15786 && sym->ts.u.derived && CLASS_DATA (sym)
15787 && CLASS_DATA (sym)->attr.coarray_comp))
15788 || class_attr.codimension)
15789 && (sym->attr.result || sym->result == sym))
15791 gfc_error ("Function result %qs at %L shall not be a coarray or have "
15792 "a coarray component", sym->name, &sym->declared_at);
15793 return;
15796 /* F2008, C524. */
15797 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
15798 && sym->ts.u.derived->ts.is_iso_c)
15800 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
15801 "shall not be a coarray", sym->name, &sym->declared_at);
15802 return;
15805 /* F2008, C525. */
15806 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15807 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15808 && sym->ts.u.derived && CLASS_DATA (sym)
15809 && CLASS_DATA (sym)->attr.coarray_comp))
15810 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
15811 || class_attr.allocatable))
15813 gfc_error ("Variable %qs at %L with coarray component shall be a "
15814 "nonpointer, nonallocatable scalar, which is not a coarray",
15815 sym->name, &sym->declared_at);
15816 return;
15819 /* F2008, C526. The function-result case was handled above. */
15820 if (class_attr.codimension
15821 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
15822 || sym->attr.select_type_temporary
15823 || sym->attr.associate_var
15824 || (sym->ns->save_all && !sym->attr.automatic)
15825 || sym->ns->proc_name->attr.flavor == FL_MODULE
15826 || sym->ns->proc_name->attr.is_main_program
15827 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
15829 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
15830 "nor a dummy argument", sym->name, &sym->declared_at);
15831 return;
15833 /* F2008, C528. */
15834 else if (class_attr.codimension && !sym->attr.select_type_temporary
15835 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
15837 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
15838 "deferred shape", sym->name, &sym->declared_at);
15839 return;
15841 else if (class_attr.codimension && class_attr.allocatable && as
15842 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
15844 gfc_error ("Allocatable coarray variable %qs at %L must have "
15845 "deferred shape", sym->name, &sym->declared_at);
15846 return;
15849 /* F2008, C541. */
15850 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15851 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15852 && sym->ts.u.derived && CLASS_DATA (sym)
15853 && CLASS_DATA (sym)->attr.coarray_comp))
15854 || (class_attr.codimension && class_attr.allocatable))
15855 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
15857 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
15858 "allocatable coarray or have coarray components",
15859 sym->name, &sym->declared_at);
15860 return;
15863 if (class_attr.codimension && sym->attr.dummy
15864 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
15866 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
15867 "procedure %qs", sym->name, &sym->declared_at,
15868 sym->ns->proc_name->name);
15869 return;
15872 if (sym->ts.type == BT_LOGICAL
15873 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
15874 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
15875 && sym->ns->proc_name->attr.is_bind_c)))
15877 int i;
15878 for (i = 0; gfc_logical_kinds[i].kind; i++)
15879 if (gfc_logical_kinds[i].kind == sym->ts.kind)
15880 break;
15881 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
15882 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
15883 "%L with non-C_Bool kind in BIND(C) procedure "
15884 "%qs", sym->name, &sym->declared_at,
15885 sym->ns->proc_name->name))
15886 return;
15887 else if (!gfc_logical_kinds[i].c_bool
15888 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
15889 "%qs at %L with non-C_Bool kind in "
15890 "BIND(C) procedure %qs", sym->name,
15891 &sym->declared_at,
15892 sym->attr.function ? sym->name
15893 : sym->ns->proc_name->name))
15894 return;
15897 switch (sym->attr.flavor)
15899 case FL_VARIABLE:
15900 if (!resolve_fl_variable (sym, mp_flag))
15901 return;
15902 break;
15904 case FL_PROCEDURE:
15905 if (sym->formal && !sym->formal_ns)
15907 /* Check that none of the arguments are a namelist. */
15908 gfc_formal_arglist *formal = sym->formal;
15910 for (; formal; formal = formal->next)
15911 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
15913 gfc_error ("Namelist %qs cannot be an argument to "
15914 "subroutine or function at %L",
15915 formal->sym->name, &sym->declared_at);
15916 return;
15920 if (!resolve_fl_procedure (sym, mp_flag))
15921 return;
15922 break;
15924 case FL_NAMELIST:
15925 if (!resolve_fl_namelist (sym))
15926 return;
15927 break;
15929 case FL_PARAMETER:
15930 if (!resolve_fl_parameter (sym))
15931 return;
15932 break;
15934 default:
15935 break;
15938 /* Resolve array specifier. Check as well some constraints
15939 on COMMON blocks. */
15941 check_constant = sym->attr.in_common && !sym->attr.pointer;
15943 /* Set the formal_arg_flag so that check_conflict will not throw
15944 an error for host associated variables in the specification
15945 expression for an array_valued function. */
15946 if ((sym->attr.function || sym->attr.result) && sym->as)
15947 formal_arg_flag = true;
15949 saved_specification_expr = specification_expr;
15950 specification_expr = true;
15951 gfc_resolve_array_spec (sym->as, check_constant);
15952 specification_expr = saved_specification_expr;
15954 formal_arg_flag = false;
15956 /* Resolve formal namespaces. */
15957 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
15958 && !sym->attr.contained && !sym->attr.intrinsic)
15959 gfc_resolve (sym->formal_ns);
15961 /* Make sure the formal namespace is present. */
15962 if (sym->formal && !sym->formal_ns)
15964 gfc_formal_arglist *formal = sym->formal;
15965 while (formal && !formal->sym)
15966 formal = formal->next;
15968 if (formal)
15970 sym->formal_ns = formal->sym->ns;
15971 if (sym->formal_ns && sym->ns != formal->sym->ns)
15972 sym->formal_ns->refs++;
15976 /* Check threadprivate restrictions. */
15977 if (sym->attr.threadprivate && !sym->attr.save
15978 && !(sym->ns->save_all && !sym->attr.automatic)
15979 && (!sym->attr.in_common
15980 && sym->module == NULL
15981 && (sym->ns->proc_name == NULL
15982 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
15983 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
15985 /* Check omp declare target restrictions. */
15986 if (sym->attr.omp_declare_target
15987 && sym->attr.flavor == FL_VARIABLE
15988 && !sym->attr.save
15989 && !(sym->ns->save_all && !sym->attr.automatic)
15990 && (!sym->attr.in_common
15991 && sym->module == NULL
15992 && (sym->ns->proc_name == NULL
15993 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
15994 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
15995 sym->name, &sym->declared_at);
15997 /* If we have come this far we can apply default-initializers, as
15998 described in 14.7.5, to those variables that have not already
15999 been assigned one. */
16000 if (sym->ts.type == BT_DERIVED
16001 && !sym->value
16002 && !sym->attr.allocatable
16003 && !sym->attr.alloc_comp)
16005 symbol_attribute *a = &sym->attr;
16007 if ((!a->save && !a->dummy && !a->pointer
16008 && !a->in_common && !a->use_assoc
16009 && a->referenced
16010 && !((a->function || a->result)
16011 && (!a->dimension
16012 || sym->ts.u.derived->attr.alloc_comp
16013 || sym->ts.u.derived->attr.pointer_comp))
16014 && !(a->function && sym != sym->result))
16015 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
16016 apply_default_init (sym);
16017 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
16018 && (sym->ts.u.derived->attr.alloc_comp
16019 || sym->ts.u.derived->attr.pointer_comp))
16020 /* Mark the result symbol to be referenced, when it has allocatable
16021 components. */
16022 sym->result->attr.referenced = 1;
16025 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
16026 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
16027 && !CLASS_DATA (sym)->attr.class_pointer
16028 && !CLASS_DATA (sym)->attr.allocatable)
16029 apply_default_init (sym);
16031 /* If this symbol has a type-spec, check it. */
16032 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
16033 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
16034 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
16035 return;
16037 if (sym->param_list)
16038 resolve_pdt (sym);
16042 /************* Resolve DATA statements *************/
16044 static struct
16046 gfc_data_value *vnode;
16047 mpz_t left;
16049 values;
16052 /* Advance the values structure to point to the next value in the data list. */
16054 static bool
16055 next_data_value (void)
16057 while (mpz_cmp_ui (values.left, 0) == 0)
16060 if (values.vnode->next == NULL)
16061 return false;
16063 values.vnode = values.vnode->next;
16064 mpz_set (values.left, values.vnode->repeat);
16067 return true;
16071 static bool
16072 check_data_variable (gfc_data_variable *var, locus *where)
16074 gfc_expr *e;
16075 mpz_t size;
16076 mpz_t offset;
16077 bool t;
16078 ar_type mark = AR_UNKNOWN;
16079 int i;
16080 mpz_t section_index[GFC_MAX_DIMENSIONS];
16081 gfc_ref *ref;
16082 gfc_array_ref *ar;
16083 gfc_symbol *sym;
16084 int has_pointer;
16086 if (!gfc_resolve_expr (var->expr))
16087 return false;
16089 ar = NULL;
16090 mpz_init_set_si (offset, 0);
16091 e = var->expr;
16093 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
16094 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
16095 e = e->value.function.actual->expr;
16097 if (e->expr_type != EXPR_VARIABLE)
16099 gfc_error ("Expecting definable entity near %L", where);
16100 return false;
16103 sym = e->symtree->n.sym;
16105 if (sym->ns->is_block_data && !sym->attr.in_common)
16107 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
16108 sym->name, &sym->declared_at);
16109 return false;
16112 if (e->ref == NULL && sym->as)
16114 gfc_error ("DATA array %qs at %L must be specified in a previous"
16115 " declaration", sym->name, where);
16116 return false;
16119 if (gfc_is_coindexed (e))
16121 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
16122 where);
16123 return false;
16126 has_pointer = sym->attr.pointer;
16128 for (ref = e->ref; ref; ref = ref->next)
16130 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
16131 has_pointer = 1;
16133 if (has_pointer)
16135 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
16137 gfc_error ("DATA element %qs at %L is a pointer and so must "
16138 "be a full array", sym->name, where);
16139 return false;
16142 if (values.vnode->expr->expr_type == EXPR_CONSTANT)
16144 gfc_error ("DATA object near %L has the pointer attribute "
16145 "and the corresponding DATA value is not a valid "
16146 "initial-data-target", where);
16147 return false;
16152 if (e->rank == 0 || has_pointer)
16154 mpz_init_set_ui (size, 1);
16155 ref = NULL;
16157 else
16159 ref = e->ref;
16161 /* Find the array section reference. */
16162 for (ref = e->ref; ref; ref = ref->next)
16164 if (ref->type != REF_ARRAY)
16165 continue;
16166 if (ref->u.ar.type == AR_ELEMENT)
16167 continue;
16168 break;
16170 gcc_assert (ref);
16172 /* Set marks according to the reference pattern. */
16173 switch (ref->u.ar.type)
16175 case AR_FULL:
16176 mark = AR_FULL;
16177 break;
16179 case AR_SECTION:
16180 ar = &ref->u.ar;
16181 /* Get the start position of array section. */
16182 gfc_get_section_index (ar, section_index, &offset);
16183 mark = AR_SECTION;
16184 break;
16186 default:
16187 gcc_unreachable ();
16190 if (!gfc_array_size (e, &size))
16192 gfc_error ("Nonconstant array section at %L in DATA statement",
16193 where);
16194 mpz_clear (offset);
16195 return false;
16199 t = true;
16201 while (mpz_cmp_ui (size, 0) > 0)
16203 if (!next_data_value ())
16205 gfc_error ("DATA statement at %L has more variables than values",
16206 where);
16207 t = false;
16208 break;
16211 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
16212 if (!t)
16213 break;
16215 /* If we have more than one element left in the repeat count,
16216 and we have more than one element left in the target variable,
16217 then create a range assignment. */
16218 /* FIXME: Only done for full arrays for now, since array sections
16219 seem tricky. */
16220 if (mark == AR_FULL && ref && ref->next == NULL
16221 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
16223 mpz_t range;
16225 if (mpz_cmp (size, values.left) >= 0)
16227 mpz_init_set (range, values.left);
16228 mpz_sub (size, size, values.left);
16229 mpz_set_ui (values.left, 0);
16231 else
16233 mpz_init_set (range, size);
16234 mpz_sub (values.left, values.left, size);
16235 mpz_set_ui (size, 0);
16238 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16239 offset, &range);
16241 mpz_add (offset, offset, range);
16242 mpz_clear (range);
16244 if (!t)
16245 break;
16248 /* Assign initial value to symbol. */
16249 else
16251 mpz_sub_ui (values.left, values.left, 1);
16252 mpz_sub_ui (size, size, 1);
16254 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16255 offset, NULL);
16256 if (!t)
16257 break;
16259 if (mark == AR_FULL)
16260 mpz_add_ui (offset, offset, 1);
16262 /* Modify the array section indexes and recalculate the offset
16263 for next element. */
16264 else if (mark == AR_SECTION)
16265 gfc_advance_section (section_index, ar, &offset);
16269 if (mark == AR_SECTION)
16271 for (i = 0; i < ar->dimen; i++)
16272 mpz_clear (section_index[i]);
16275 mpz_clear (size);
16276 mpz_clear (offset);
16278 return t;
16282 static bool traverse_data_var (gfc_data_variable *, locus *);
16284 /* Iterate over a list of elements in a DATA statement. */
16286 static bool
16287 traverse_data_list (gfc_data_variable *var, locus *where)
16289 mpz_t trip;
16290 iterator_stack frame;
16291 gfc_expr *e, *start, *end, *step;
16292 bool retval = true;
16294 mpz_init (frame.value);
16295 mpz_init (trip);
16297 start = gfc_copy_expr (var->iter.start);
16298 end = gfc_copy_expr (var->iter.end);
16299 step = gfc_copy_expr (var->iter.step);
16301 if (!gfc_simplify_expr (start, 1)
16302 || start->expr_type != EXPR_CONSTANT)
16304 gfc_error ("start of implied-do loop at %L could not be "
16305 "simplified to a constant value", &start->where);
16306 retval = false;
16307 goto cleanup;
16309 if (!gfc_simplify_expr (end, 1)
16310 || end->expr_type != EXPR_CONSTANT)
16312 gfc_error ("end of implied-do loop at %L could not be "
16313 "simplified to a constant value", &start->where);
16314 retval = false;
16315 goto cleanup;
16317 if (!gfc_simplify_expr (step, 1)
16318 || step->expr_type != EXPR_CONSTANT)
16320 gfc_error ("step of implied-do loop at %L could not be "
16321 "simplified to a constant value", &start->where);
16322 retval = false;
16323 goto cleanup;
16326 mpz_set (trip, end->value.integer);
16327 mpz_sub (trip, trip, start->value.integer);
16328 mpz_add (trip, trip, step->value.integer);
16330 mpz_div (trip, trip, step->value.integer);
16332 mpz_set (frame.value, start->value.integer);
16334 frame.prev = iter_stack;
16335 frame.variable = var->iter.var->symtree;
16336 iter_stack = &frame;
16338 while (mpz_cmp_ui (trip, 0) > 0)
16340 if (!traverse_data_var (var->list, where))
16342 retval = false;
16343 goto cleanup;
16346 e = gfc_copy_expr (var->expr);
16347 if (!gfc_simplify_expr (e, 1))
16349 gfc_free_expr (e);
16350 retval = false;
16351 goto cleanup;
16354 mpz_add (frame.value, frame.value, step->value.integer);
16356 mpz_sub_ui (trip, trip, 1);
16359 cleanup:
16360 mpz_clear (frame.value);
16361 mpz_clear (trip);
16363 gfc_free_expr (start);
16364 gfc_free_expr (end);
16365 gfc_free_expr (step);
16367 iter_stack = frame.prev;
16368 return retval;
16372 /* Type resolve variables in the variable list of a DATA statement. */
16374 static bool
16375 traverse_data_var (gfc_data_variable *var, locus *where)
16377 bool t;
16379 for (; var; var = var->next)
16381 if (var->expr == NULL)
16382 t = traverse_data_list (var, where);
16383 else
16384 t = check_data_variable (var, where);
16386 if (!t)
16387 return false;
16390 return true;
16394 /* Resolve the expressions and iterators associated with a data statement.
16395 This is separate from the assignment checking because data lists should
16396 only be resolved once. */
16398 static bool
16399 resolve_data_variables (gfc_data_variable *d)
16401 for (; d; d = d->next)
16403 if (d->list == NULL)
16405 if (!gfc_resolve_expr (d->expr))
16406 return false;
16408 else
16410 if (!gfc_resolve_iterator (&d->iter, false, true))
16411 return false;
16413 if (!resolve_data_variables (d->list))
16414 return false;
16418 return true;
16422 /* Resolve a single DATA statement. We implement this by storing a pointer to
16423 the value list into static variables, and then recursively traversing the
16424 variables list, expanding iterators and such. */
16426 static void
16427 resolve_data (gfc_data *d)
16430 if (!resolve_data_variables (d->var))
16431 return;
16433 values.vnode = d->value;
16434 if (d->value == NULL)
16435 mpz_set_ui (values.left, 0);
16436 else
16437 mpz_set (values.left, d->value->repeat);
16439 if (!traverse_data_var (d->var, &d->where))
16440 return;
16442 /* At this point, we better not have any values left. */
16444 if (next_data_value ())
16445 gfc_error ("DATA statement at %L has more values than variables",
16446 &d->where);
16450 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
16451 accessed by host or use association, is a dummy argument to a pure function,
16452 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
16453 is storage associated with any such variable, shall not be used in the
16454 following contexts: (clients of this function). */
16456 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
16457 procedure. Returns zero if assignment is OK, nonzero if there is a
16458 problem. */
16460 gfc_impure_variable (gfc_symbol *sym)
16462 gfc_symbol *proc;
16463 gfc_namespace *ns;
16465 if (sym->attr.use_assoc || sym->attr.in_common)
16466 return 1;
16468 /* Check if the symbol's ns is inside the pure procedure. */
16469 for (ns = gfc_current_ns; ns; ns = ns->parent)
16471 if (ns == sym->ns)
16472 break;
16473 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
16474 return 1;
16477 proc = sym->ns->proc_name;
16478 if (sym->attr.dummy
16479 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
16480 || proc->attr.function))
16481 return 1;
16483 /* TODO: Sort out what can be storage associated, if anything, and include
16484 it here. In principle equivalences should be scanned but it does not
16485 seem to be possible to storage associate an impure variable this way. */
16486 return 0;
16490 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
16491 current namespace is inside a pure procedure. */
16494 gfc_pure (gfc_symbol *sym)
16496 symbol_attribute attr;
16497 gfc_namespace *ns;
16499 if (sym == NULL)
16501 /* Check if the current namespace or one of its parents
16502 belongs to a pure procedure. */
16503 for (ns = gfc_current_ns; ns; ns = ns->parent)
16505 sym = ns->proc_name;
16506 if (sym == NULL)
16507 return 0;
16508 attr = sym->attr;
16509 if (attr.flavor == FL_PROCEDURE && attr.pure)
16510 return 1;
16512 return 0;
16515 attr = sym->attr;
16517 return attr.flavor == FL_PROCEDURE && attr.pure;
16521 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
16522 checks if the current namespace is implicitly pure. Note that this
16523 function returns false for a PURE procedure. */
16526 gfc_implicit_pure (gfc_symbol *sym)
16528 gfc_namespace *ns;
16530 if (sym == NULL)
16532 /* Check if the current procedure is implicit_pure. Walk up
16533 the procedure list until we find a procedure. */
16534 for (ns = gfc_current_ns; ns; ns = ns->parent)
16536 sym = ns->proc_name;
16537 if (sym == NULL)
16538 return 0;
16540 if (sym->attr.flavor == FL_PROCEDURE)
16541 break;
16545 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
16546 && !sym->attr.pure;
16550 void
16551 gfc_unset_implicit_pure (gfc_symbol *sym)
16553 gfc_namespace *ns;
16555 if (sym == NULL)
16557 /* Check if the current procedure is implicit_pure. Walk up
16558 the procedure list until we find a procedure. */
16559 for (ns = gfc_current_ns; ns; ns = ns->parent)
16561 sym = ns->proc_name;
16562 if (sym == NULL)
16563 return;
16565 if (sym->attr.flavor == FL_PROCEDURE)
16566 break;
16570 if (sym->attr.flavor == FL_PROCEDURE)
16571 sym->attr.implicit_pure = 0;
16572 else
16573 sym->attr.pure = 0;
16577 /* Test whether the current procedure is elemental or not. */
16580 gfc_elemental (gfc_symbol *sym)
16582 symbol_attribute attr;
16584 if (sym == NULL)
16585 sym = gfc_current_ns->proc_name;
16586 if (sym == NULL)
16587 return 0;
16588 attr = sym->attr;
16590 return attr.flavor == FL_PROCEDURE && attr.elemental;
16594 /* Warn about unused labels. */
16596 static void
16597 warn_unused_fortran_label (gfc_st_label *label)
16599 if (label == NULL)
16600 return;
16602 warn_unused_fortran_label (label->left);
16604 if (label->defined == ST_LABEL_UNKNOWN)
16605 return;
16607 switch (label->referenced)
16609 case ST_LABEL_UNKNOWN:
16610 gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
16611 label->value, &label->where);
16612 break;
16614 case ST_LABEL_BAD_TARGET:
16615 gfc_warning (OPT_Wunused_label,
16616 "Label %d at %L defined but cannot be used",
16617 label->value, &label->where);
16618 break;
16620 default:
16621 break;
16624 warn_unused_fortran_label (label->right);
16628 /* Returns the sequence type of a symbol or sequence. */
16630 static seq_type
16631 sequence_type (gfc_typespec ts)
16633 seq_type result;
16634 gfc_component *c;
16636 switch (ts.type)
16638 case BT_DERIVED:
16640 if (ts.u.derived->components == NULL)
16641 return SEQ_NONDEFAULT;
16643 result = sequence_type (ts.u.derived->components->ts);
16644 for (c = ts.u.derived->components->next; c; c = c->next)
16645 if (sequence_type (c->ts) != result)
16646 return SEQ_MIXED;
16648 return result;
16650 case BT_CHARACTER:
16651 if (ts.kind != gfc_default_character_kind)
16652 return SEQ_NONDEFAULT;
16654 return SEQ_CHARACTER;
16656 case BT_INTEGER:
16657 if (ts.kind != gfc_default_integer_kind)
16658 return SEQ_NONDEFAULT;
16660 return SEQ_NUMERIC;
16662 case BT_REAL:
16663 if (!(ts.kind == gfc_default_real_kind
16664 || ts.kind == gfc_default_double_kind))
16665 return SEQ_NONDEFAULT;
16667 return SEQ_NUMERIC;
16669 case BT_COMPLEX:
16670 if (ts.kind != gfc_default_complex_kind)
16671 return SEQ_NONDEFAULT;
16673 return SEQ_NUMERIC;
16675 case BT_LOGICAL:
16676 if (ts.kind != gfc_default_logical_kind)
16677 return SEQ_NONDEFAULT;
16679 return SEQ_NUMERIC;
16681 default:
16682 return SEQ_NONDEFAULT;
16687 /* Resolve derived type EQUIVALENCE object. */
16689 static bool
16690 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
16692 gfc_component *c = derived->components;
16694 if (!derived)
16695 return true;
16697 /* Shall not be an object of nonsequence derived type. */
16698 if (!derived->attr.sequence)
16700 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
16701 "attribute to be an EQUIVALENCE object", sym->name,
16702 &e->where);
16703 return false;
16706 /* Shall not have allocatable components. */
16707 if (derived->attr.alloc_comp)
16709 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
16710 "components to be an EQUIVALENCE object",sym->name,
16711 &e->where);
16712 return false;
16715 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
16717 gfc_error ("Derived type variable %qs at %L with default "
16718 "initialization cannot be in EQUIVALENCE with a variable "
16719 "in COMMON", sym->name, &e->where);
16720 return false;
16723 for (; c ; c = c->next)
16725 if (gfc_bt_struct (c->ts.type)
16726 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
16727 return false;
16729 /* Shall not be an object of sequence derived type containing a pointer
16730 in the structure. */
16731 if (c->attr.pointer)
16733 gfc_error ("Derived type variable %qs at %L with pointer "
16734 "component(s) cannot be an EQUIVALENCE object",
16735 sym->name, &e->where);
16736 return false;
16739 return true;
16743 /* Resolve equivalence object.
16744 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
16745 an allocatable array, an object of nonsequence derived type, an object of
16746 sequence derived type containing a pointer at any level of component
16747 selection, an automatic object, a function name, an entry name, a result
16748 name, a named constant, a structure component, or a subobject of any of
16749 the preceding objects. A substring shall not have length zero. A
16750 derived type shall not have components with default initialization nor
16751 shall two objects of an equivalence group be initialized.
16752 Either all or none of the objects shall have an protected attribute.
16753 The simple constraints are done in symbol.c(check_conflict) and the rest
16754 are implemented here. */
16756 static void
16757 resolve_equivalence (gfc_equiv *eq)
16759 gfc_symbol *sym;
16760 gfc_symbol *first_sym;
16761 gfc_expr *e;
16762 gfc_ref *r;
16763 locus *last_where = NULL;
16764 seq_type eq_type, last_eq_type;
16765 gfc_typespec *last_ts;
16766 int object, cnt_protected;
16767 const char *msg;
16769 last_ts = &eq->expr->symtree->n.sym->ts;
16771 first_sym = eq->expr->symtree->n.sym;
16773 cnt_protected = 0;
16775 for (object = 1; eq; eq = eq->eq, object++)
16777 e = eq->expr;
16779 e->ts = e->symtree->n.sym->ts;
16780 /* match_varspec might not know yet if it is seeing
16781 array reference or substring reference, as it doesn't
16782 know the types. */
16783 if (e->ref && e->ref->type == REF_ARRAY)
16785 gfc_ref *ref = e->ref;
16786 sym = e->symtree->n.sym;
16788 if (sym->attr.dimension)
16790 ref->u.ar.as = sym->as;
16791 ref = ref->next;
16794 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
16795 if (e->ts.type == BT_CHARACTER
16796 && ref
16797 && ref->type == REF_ARRAY
16798 && ref->u.ar.dimen == 1
16799 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
16800 && ref->u.ar.stride[0] == NULL)
16802 gfc_expr *start = ref->u.ar.start[0];
16803 gfc_expr *end = ref->u.ar.end[0];
16804 void *mem = NULL;
16806 /* Optimize away the (:) reference. */
16807 if (start == NULL && end == NULL)
16809 if (e->ref == ref)
16810 e->ref = ref->next;
16811 else
16812 e->ref->next = ref->next;
16813 mem = ref;
16815 else
16817 ref->type = REF_SUBSTRING;
16818 if (start == NULL)
16819 start = gfc_get_int_expr (gfc_charlen_int_kind,
16820 NULL, 1);
16821 ref->u.ss.start = start;
16822 if (end == NULL && e->ts.u.cl)
16823 end = gfc_copy_expr (e->ts.u.cl->length);
16824 ref->u.ss.end = end;
16825 ref->u.ss.length = e->ts.u.cl;
16826 e->ts.u.cl = NULL;
16828 ref = ref->next;
16829 free (mem);
16832 /* Any further ref is an error. */
16833 if (ref)
16835 gcc_assert (ref->type == REF_ARRAY);
16836 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
16837 &ref->u.ar.where);
16838 continue;
16842 if (!gfc_resolve_expr (e))
16843 continue;
16845 sym = e->symtree->n.sym;
16847 if (sym->attr.is_protected)
16848 cnt_protected++;
16849 if (cnt_protected > 0 && cnt_protected != object)
16851 gfc_error ("Either all or none of the objects in the "
16852 "EQUIVALENCE set at %L shall have the "
16853 "PROTECTED attribute",
16854 &e->where);
16855 break;
16858 /* Shall not equivalence common block variables in a PURE procedure. */
16859 if (sym->ns->proc_name
16860 && sym->ns->proc_name->attr.pure
16861 && sym->attr.in_common)
16863 /* Need to check for symbols that may have entered the pure
16864 procedure via a USE statement. */
16865 bool saw_sym = false;
16866 if (sym->ns->use_stmts)
16868 gfc_use_rename *r;
16869 for (r = sym->ns->use_stmts->rename; r; r = r->next)
16870 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
16872 else
16873 saw_sym = true;
16875 if (saw_sym)
16876 gfc_error ("COMMON block member %qs at %L cannot be an "
16877 "EQUIVALENCE object in the pure procedure %qs",
16878 sym->name, &e->where, sym->ns->proc_name->name);
16879 break;
16882 /* Shall not be a named constant. */
16883 if (e->expr_type == EXPR_CONSTANT)
16885 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
16886 "object", sym->name, &e->where);
16887 continue;
16890 if (e->ts.type == BT_DERIVED
16891 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
16892 continue;
16894 /* Check that the types correspond correctly:
16895 Note 5.28:
16896 A numeric sequence structure may be equivalenced to another sequence
16897 structure, an object of default integer type, default real type, double
16898 precision real type, default logical type such that components of the
16899 structure ultimately only become associated to objects of the same
16900 kind. A character sequence structure may be equivalenced to an object
16901 of default character kind or another character sequence structure.
16902 Other objects may be equivalenced only to objects of the same type and
16903 kind parameters. */
16905 /* Identical types are unconditionally OK. */
16906 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
16907 goto identical_types;
16909 last_eq_type = sequence_type (*last_ts);
16910 eq_type = sequence_type (sym->ts);
16912 /* Since the pair of objects is not of the same type, mixed or
16913 non-default sequences can be rejected. */
16915 msg = "Sequence %s with mixed components in EQUIVALENCE "
16916 "statement at %L with different type objects";
16917 if ((object ==2
16918 && last_eq_type == SEQ_MIXED
16919 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
16920 || (eq_type == SEQ_MIXED
16921 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
16922 continue;
16924 msg = "Non-default type object or sequence %s in EQUIVALENCE "
16925 "statement at %L with objects of different type";
16926 if ((object ==2
16927 && last_eq_type == SEQ_NONDEFAULT
16928 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
16929 || (eq_type == SEQ_NONDEFAULT
16930 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
16931 continue;
16933 msg ="Non-CHARACTER object %qs in default CHARACTER "
16934 "EQUIVALENCE statement at %L";
16935 if (last_eq_type == SEQ_CHARACTER
16936 && eq_type != SEQ_CHARACTER
16937 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
16938 continue;
16940 msg ="Non-NUMERIC object %qs in default NUMERIC "
16941 "EQUIVALENCE statement at %L";
16942 if (last_eq_type == SEQ_NUMERIC
16943 && eq_type != SEQ_NUMERIC
16944 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
16945 continue;
16947 identical_types:
16949 last_ts =&sym->ts;
16950 last_where = &e->where;
16952 if (!e->ref)
16953 continue;
16955 /* Shall not be an automatic array. */
16956 if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
16958 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
16959 "an EQUIVALENCE object", sym->name, &e->where);
16960 continue;
16963 r = e->ref;
16964 while (r)
16966 /* Shall not be a structure component. */
16967 if (r->type == REF_COMPONENT)
16969 gfc_error ("Structure component %qs at %L cannot be an "
16970 "EQUIVALENCE object",
16971 r->u.c.component->name, &e->where);
16972 break;
16975 /* A substring shall not have length zero. */
16976 if (r->type == REF_SUBSTRING)
16978 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
16980 gfc_error ("Substring at %L has length zero",
16981 &r->u.ss.start->where);
16982 break;
16985 r = r->next;
16991 /* Function called by resolve_fntype to flag other symbols used in the
16992 length type parameter specification of function results. */
16994 static bool
16995 flag_fn_result_spec (gfc_expr *expr,
16996 gfc_symbol *sym,
16997 int *f ATTRIBUTE_UNUSED)
16999 gfc_namespace *ns;
17000 gfc_symbol *s;
17002 if (expr->expr_type == EXPR_VARIABLE)
17004 s = expr->symtree->n.sym;
17005 for (ns = s->ns; ns; ns = ns->parent)
17006 if (!ns->parent)
17007 break;
17009 if (sym == s)
17011 gfc_error ("Self reference in character length expression "
17012 "for %qs at %L", sym->name, &expr->where);
17013 return true;
17016 if (!s->fn_result_spec
17017 && s->attr.flavor == FL_PARAMETER)
17019 /* Function contained in a module.... */
17020 if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
17022 gfc_symtree *st;
17023 s->fn_result_spec = 1;
17024 /* Make sure that this symbol is translated as a module
17025 variable. */
17026 st = gfc_get_unique_symtree (ns);
17027 st->n.sym = s;
17028 s->refs++;
17030 /* ... which is use associated and called. */
17031 else if (s->attr.use_assoc || s->attr.used_in_submodule
17033 /* External function matched with an interface. */
17034 (s->ns->proc_name
17035 && ((s->ns == ns
17036 && s->ns->proc_name->attr.if_source == IFSRC_DECL)
17037 || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
17038 && s->ns->proc_name->attr.function))
17039 s->fn_result_spec = 1;
17042 return false;
17046 /* Resolve function and ENTRY types, issue diagnostics if needed. */
17048 static void
17049 resolve_fntype (gfc_namespace *ns)
17051 gfc_entry_list *el;
17052 gfc_symbol *sym;
17054 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
17055 return;
17057 /* If there are any entries, ns->proc_name is the entry master
17058 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
17059 if (ns->entries)
17060 sym = ns->entries->sym;
17061 else
17062 sym = ns->proc_name;
17063 if (sym->result == sym
17064 && sym->ts.type == BT_UNKNOWN
17065 && !gfc_set_default_type (sym, 0, NULL)
17066 && !sym->attr.untyped)
17068 gfc_error ("Function %qs at %L has no IMPLICIT type",
17069 sym->name, &sym->declared_at);
17070 sym->attr.untyped = 1;
17073 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
17074 && !sym->attr.contained
17075 && !gfc_check_symbol_access (sym->ts.u.derived)
17076 && gfc_check_symbol_access (sym))
17078 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
17079 "%L of PRIVATE type %qs", sym->name,
17080 &sym->declared_at, sym->ts.u.derived->name);
17083 if (ns->entries)
17084 for (el = ns->entries->next; el; el = el->next)
17086 if (el->sym->result == el->sym
17087 && el->sym->ts.type == BT_UNKNOWN
17088 && !gfc_set_default_type (el->sym, 0, NULL)
17089 && !el->sym->attr.untyped)
17091 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
17092 el->sym->name, &el->sym->declared_at);
17093 el->sym->attr.untyped = 1;
17097 if (sym->ts.type == BT_CHARACTER)
17098 gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
17102 /* 12.3.2.1.1 Defined operators. */
17104 static bool
17105 check_uop_procedure (gfc_symbol *sym, locus where)
17107 gfc_formal_arglist *formal;
17109 if (!sym->attr.function)
17111 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
17112 sym->name, &where);
17113 return false;
17116 if (sym->ts.type == BT_CHARACTER
17117 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
17118 && !(sym->result && ((sym->result->ts.u.cl
17119 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
17121 gfc_error ("User operator procedure %qs at %L cannot be assumed "
17122 "character length", sym->name, &where);
17123 return false;
17126 formal = gfc_sym_get_dummy_args (sym);
17127 if (!formal || !formal->sym)
17129 gfc_error ("User operator procedure %qs at %L must have at least "
17130 "one argument", sym->name, &where);
17131 return false;
17134 if (formal->sym->attr.intent != INTENT_IN)
17136 gfc_error ("First argument of operator interface at %L must be "
17137 "INTENT(IN)", &where);
17138 return false;
17141 if (formal->sym->attr.optional)
17143 gfc_error ("First argument of operator interface at %L cannot be "
17144 "optional", &where);
17145 return false;
17148 formal = formal->next;
17149 if (!formal || !formal->sym)
17150 return true;
17152 if (formal->sym->attr.intent != INTENT_IN)
17154 gfc_error ("Second argument of operator interface at %L must be "
17155 "INTENT(IN)", &where);
17156 return false;
17159 if (formal->sym->attr.optional)
17161 gfc_error ("Second argument of operator interface at %L cannot be "
17162 "optional", &where);
17163 return false;
17166 if (formal->next)
17168 gfc_error ("Operator interface at %L must have, at most, two "
17169 "arguments", &where);
17170 return false;
17173 return true;
17176 static void
17177 gfc_resolve_uops (gfc_symtree *symtree)
17179 gfc_interface *itr;
17181 if (symtree == NULL)
17182 return;
17184 gfc_resolve_uops (symtree->left);
17185 gfc_resolve_uops (symtree->right);
17187 for (itr = symtree->n.uop->op; itr; itr = itr->next)
17188 check_uop_procedure (itr->sym, itr->sym->declared_at);
17192 /* Examine all of the expressions associated with a program unit,
17193 assign types to all intermediate expressions, make sure that all
17194 assignments are to compatible types and figure out which names
17195 refer to which functions or subroutines. It doesn't check code
17196 block, which is handled by gfc_resolve_code. */
17198 static void
17199 resolve_types (gfc_namespace *ns)
17201 gfc_namespace *n;
17202 gfc_charlen *cl;
17203 gfc_data *d;
17204 gfc_equiv *eq;
17205 gfc_namespace* old_ns = gfc_current_ns;
17206 bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
17208 if (ns->types_resolved)
17209 return;
17211 /* Check that all IMPLICIT types are ok. */
17212 if (!ns->seen_implicit_none)
17214 unsigned letter;
17215 for (letter = 0; letter != GFC_LETTERS; ++letter)
17216 if (ns->set_flag[letter]
17217 && !resolve_typespec_used (&ns->default_type[letter],
17218 &ns->implicit_loc[letter], NULL))
17219 return;
17222 gfc_current_ns = ns;
17224 resolve_entries (ns);
17226 resolve_common_vars (&ns->blank_common, false);
17227 resolve_common_blocks (ns->common_root);
17229 resolve_contained_functions (ns);
17231 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
17232 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
17233 gfc_resolve_formal_arglist (ns->proc_name);
17235 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
17237 for (cl = ns->cl_list; cl; cl = cl->next)
17238 resolve_charlen (cl);
17240 gfc_traverse_ns (ns, resolve_symbol);
17242 resolve_fntype (ns);
17244 for (n = ns->contained; n; n = n->sibling)
17246 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
17247 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
17248 "also be PURE", n->proc_name->name,
17249 &n->proc_name->declared_at);
17251 resolve_types (n);
17254 forall_flag = 0;
17255 gfc_do_concurrent_flag = 0;
17256 gfc_check_interfaces (ns);
17258 gfc_traverse_ns (ns, resolve_values);
17260 if (ns->save_all || (!flag_automatic && !recursive))
17261 gfc_save_all (ns);
17263 iter_stack = NULL;
17264 for (d = ns->data; d; d = d->next)
17265 resolve_data (d);
17267 iter_stack = NULL;
17268 gfc_traverse_ns (ns, gfc_formalize_init_value);
17270 gfc_traverse_ns (ns, gfc_verify_binding_labels);
17272 for (eq = ns->equiv; eq; eq = eq->next)
17273 resolve_equivalence (eq);
17275 /* Warn about unused labels. */
17276 if (warn_unused_label)
17277 warn_unused_fortran_label (ns->st_labels);
17279 gfc_resolve_uops (ns->uop_root);
17281 gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
17283 gfc_resolve_omp_declare_simd (ns);
17285 gfc_resolve_omp_udrs (ns->omp_udr_root);
17287 ns->types_resolved = 1;
17289 gfc_current_ns = old_ns;
17293 /* Call gfc_resolve_code recursively. */
17295 static void
17296 resolve_codes (gfc_namespace *ns)
17298 gfc_namespace *n;
17299 bitmap_obstack old_obstack;
17301 if (ns->resolved == 1)
17302 return;
17304 for (n = ns->contained; n; n = n->sibling)
17305 resolve_codes (n);
17307 gfc_current_ns = ns;
17309 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
17310 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
17311 cs_base = NULL;
17313 /* Set to an out of range value. */
17314 current_entry_id = -1;
17316 old_obstack = labels_obstack;
17317 bitmap_obstack_initialize (&labels_obstack);
17319 gfc_resolve_oacc_declare (ns);
17320 gfc_resolve_oacc_routines (ns);
17321 gfc_resolve_omp_local_vars (ns);
17322 gfc_resolve_code (ns->code, ns);
17324 bitmap_obstack_release (&labels_obstack);
17325 labels_obstack = old_obstack;
17329 /* This function is called after a complete program unit has been compiled.
17330 Its purpose is to examine all of the expressions associated with a program
17331 unit, assign types to all intermediate expressions, make sure that all
17332 assignments are to compatible types and figure out which names refer to
17333 which functions or subroutines. */
17335 void
17336 gfc_resolve (gfc_namespace *ns)
17338 gfc_namespace *old_ns;
17339 code_stack *old_cs_base;
17340 struct gfc_omp_saved_state old_omp_state;
17342 if (ns->resolved)
17343 return;
17345 ns->resolved = -1;
17346 old_ns = gfc_current_ns;
17347 old_cs_base = cs_base;
17349 /* As gfc_resolve can be called during resolution of an OpenMP construct
17350 body, we should clear any state associated to it, so that say NS's
17351 DO loops are not interpreted as OpenMP loops. */
17352 if (!ns->construct_entities)
17353 gfc_omp_save_and_clear_state (&old_omp_state);
17355 resolve_types (ns);
17356 component_assignment_level = 0;
17357 resolve_codes (ns);
17359 gfc_current_ns = old_ns;
17360 cs_base = old_cs_base;
17361 ns->resolved = 1;
17363 gfc_run_passes (ns);
17365 if (!ns->construct_entities)
17366 gfc_omp_restore_state (&old_omp_state);