2016-07-28 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / resolve.c
blob23da9ac444443391e7b1e64e440a4ba4d2d7bfbf
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2016 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "bitmap.h"
26 #include "gfortran.h"
27 #include "arith.h" /* For gfc_compare_expr(). */
28 #include "dependency.h"
29 #include "data.h"
30 #include "target-memory.h" /* for gfc_simplify_transfer */
31 #include "constructor.h"
33 /* Types used in equivalence statements. */
35 enum seq_type
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
40 /* Stack to keep track of the nesting of blocks as we move through the
41 code. See resolve_branch() and gfc_resolve_code(). */
43 typedef struct code_stack
45 struct gfc_code *head, *current;
46 struct code_stack *prev;
48 /* This bitmap keeps track of the targets valid for a branch from
49 inside this block except for END {IF|SELECT}s of enclosing
50 blocks. */
51 bitmap reachable_labels;
53 code_stack;
55 static code_stack *cs_base = NULL;
58 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
60 static int forall_flag;
61 int gfc_do_concurrent_flag;
63 /* True when we are resolving an expression that is an actual argument to
64 a procedure. */
65 static bool actual_arg = false;
66 /* True when we are resolving an expression that is the first actual argument
67 to a procedure. */
68 static bool first_actual_arg = false;
71 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
73 static int omp_workshare_flag;
75 /* Nonzero if we are processing a formal arglist. The corresponding function
76 resets the flag each time that it is read. */
77 static int formal_arg_flag = 0;
79 /* True if we are resolving a specification expression. */
80 static bool specification_expr = false;
82 /* The id of the last entry seen. */
83 static int current_entry_id;
85 /* We use bitmaps to determine if a branch target is valid. */
86 static bitmap_obstack labels_obstack;
88 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
89 static bool inquiry_argument = false;
92 int
93 gfc_is_formal_arg (void)
95 return formal_arg_flag;
98 /* Is the symbol host associated? */
99 static bool
100 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
102 for (ns = ns->parent; ns; ns = ns->parent)
104 if (sym->ns == ns)
105 return true;
108 return false;
111 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
112 an ABSTRACT derived-type. If where is not NULL, an error message with that
113 locus is printed, optionally using name. */
115 static bool
116 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
118 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
120 if (where)
122 if (name)
123 gfc_error ("%qs at %L is of the ABSTRACT type %qs",
124 name, where, ts->u.derived->name);
125 else
126 gfc_error ("ABSTRACT type %qs used at %L",
127 ts->u.derived->name, where);
130 return false;
133 return true;
137 static bool
138 check_proc_interface (gfc_symbol *ifc, locus *where)
140 /* Several checks for F08:C1216. */
141 if (ifc->attr.procedure)
143 gfc_error ("Interface %qs at %L is declared "
144 "in a later PROCEDURE statement", ifc->name, where);
145 return false;
147 if (ifc->generic)
149 /* For generic interfaces, check if there is
150 a specific procedure with the same name. */
151 gfc_interface *gen = ifc->generic;
152 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
153 gen = gen->next;
154 if (!gen)
156 gfc_error ("Interface %qs at %L may not be generic",
157 ifc->name, where);
158 return false;
161 if (ifc->attr.proc == PROC_ST_FUNCTION)
163 gfc_error ("Interface %qs at %L may not be a statement function",
164 ifc->name, where);
165 return false;
167 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
168 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
169 ifc->attr.intrinsic = 1;
170 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
172 gfc_error ("Intrinsic procedure %qs not allowed in "
173 "PROCEDURE statement at %L", ifc->name, where);
174 return false;
176 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
178 gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
179 return false;
181 return true;
185 static void resolve_symbol (gfc_symbol *sym);
188 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
190 static bool
191 resolve_procedure_interface (gfc_symbol *sym)
193 gfc_symbol *ifc = sym->ts.interface;
195 if (!ifc)
196 return true;
198 if (ifc == sym)
200 gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
201 sym->name, &sym->declared_at);
202 return false;
204 if (!check_proc_interface (ifc, &sym->declared_at))
205 return false;
207 if (ifc->attr.if_source || ifc->attr.intrinsic)
209 /* Resolve interface and copy attributes. */
210 resolve_symbol (ifc);
211 if (ifc->attr.intrinsic)
212 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
214 if (ifc->result)
216 sym->ts = ifc->result->ts;
217 sym->result = sym;
219 else
220 sym->ts = ifc->ts;
221 sym->ts.interface = ifc;
222 sym->attr.function = ifc->attr.function;
223 sym->attr.subroutine = ifc->attr.subroutine;
225 sym->attr.allocatable = ifc->attr.allocatable;
226 sym->attr.pointer = ifc->attr.pointer;
227 sym->attr.pure = ifc->attr.pure;
228 sym->attr.elemental = ifc->attr.elemental;
229 sym->attr.dimension = ifc->attr.dimension;
230 sym->attr.contiguous = ifc->attr.contiguous;
231 sym->attr.recursive = ifc->attr.recursive;
232 sym->attr.always_explicit = ifc->attr.always_explicit;
233 sym->attr.ext_attr |= ifc->attr.ext_attr;
234 sym->attr.is_bind_c = ifc->attr.is_bind_c;
235 sym->attr.class_ok = ifc->attr.class_ok;
236 /* Copy array spec. */
237 sym->as = gfc_copy_array_spec (ifc->as);
238 /* Copy char length. */
239 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
241 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
242 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
243 && !gfc_resolve_expr (sym->ts.u.cl->length))
244 return false;
248 return true;
252 /* Resolve types of formal argument lists. These have to be done early so that
253 the formal argument lists of module procedures can be copied to the
254 containing module before the individual procedures are resolved
255 individually. We also resolve argument lists of procedures in interface
256 blocks because they are self-contained scoping units.
258 Since a dummy argument cannot be a non-dummy procedure, the only
259 resort left for untyped names are the IMPLICIT types. */
261 static void
262 resolve_formal_arglist (gfc_symbol *proc)
264 gfc_formal_arglist *f;
265 gfc_symbol *sym;
266 bool saved_specification_expr;
267 int i;
269 if (proc->result != NULL)
270 sym = proc->result;
271 else
272 sym = proc;
274 if (gfc_elemental (proc)
275 || sym->attr.pointer || sym->attr.allocatable
276 || (sym->as && sym->as->rank != 0))
278 proc->attr.always_explicit = 1;
279 sym->attr.always_explicit = 1;
282 formal_arg_flag = 1;
284 for (f = proc->formal; f; f = f->next)
286 gfc_array_spec *as;
288 sym = f->sym;
290 if (sym == NULL)
292 /* Alternate return placeholder. */
293 if (gfc_elemental (proc))
294 gfc_error ("Alternate return specifier in elemental subroutine "
295 "%qs at %L is not allowed", proc->name,
296 &proc->declared_at);
297 if (proc->attr.function)
298 gfc_error ("Alternate return specifier in function "
299 "%qs at %L is not allowed", proc->name,
300 &proc->declared_at);
301 continue;
303 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
304 && !resolve_procedure_interface (sym))
305 return;
307 if (strcmp (proc->name, sym->name) == 0)
309 gfc_error ("Self-referential argument "
310 "%qs at %L is not allowed", sym->name,
311 &proc->declared_at);
312 return;
315 if (sym->attr.if_source != IFSRC_UNKNOWN)
316 resolve_formal_arglist (sym);
318 if (sym->attr.subroutine || sym->attr.external)
320 if (sym->attr.flavor == FL_UNKNOWN)
321 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
323 else
325 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
326 && (!sym->attr.function || sym->result == sym))
327 gfc_set_default_type (sym, 1, sym->ns);
330 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
331 ? CLASS_DATA (sym)->as : sym->as;
333 saved_specification_expr = specification_expr;
334 specification_expr = true;
335 gfc_resolve_array_spec (as, 0);
336 specification_expr = saved_specification_expr;
338 /* We can't tell if an array with dimension (:) is assumed or deferred
339 shape until we know if it has the pointer or allocatable attributes.
341 if (as && as->rank > 0 && as->type == AS_DEFERRED
342 && ((sym->ts.type != BT_CLASS
343 && !(sym->attr.pointer || sym->attr.allocatable))
344 || (sym->ts.type == BT_CLASS
345 && !(CLASS_DATA (sym)->attr.class_pointer
346 || CLASS_DATA (sym)->attr.allocatable)))
347 && sym->attr.flavor != FL_PROCEDURE)
349 as->type = AS_ASSUMED_SHAPE;
350 for (i = 0; i < as->rank; i++)
351 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
354 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
355 || (as && as->type == AS_ASSUMED_RANK)
356 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
357 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
358 && (CLASS_DATA (sym)->attr.class_pointer
359 || CLASS_DATA (sym)->attr.allocatable
360 || CLASS_DATA (sym)->attr.target))
361 || sym->attr.optional)
363 proc->attr.always_explicit = 1;
364 if (proc->result)
365 proc->result->attr.always_explicit = 1;
368 /* If the flavor is unknown at this point, it has to be a variable.
369 A procedure specification would have already set the type. */
371 if (sym->attr.flavor == FL_UNKNOWN)
372 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
374 if (gfc_pure (proc))
376 if (sym->attr.flavor == FL_PROCEDURE)
378 /* F08:C1279. */
379 if (!gfc_pure (sym))
381 gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
382 "also be PURE", sym->name, &sym->declared_at);
383 continue;
386 else if (!sym->attr.pointer)
388 if (proc->attr.function && sym->attr.intent != INTENT_IN)
390 if (sym->attr.value)
391 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
392 " of pure function %qs at %L with VALUE "
393 "attribute but without INTENT(IN)",
394 sym->name, proc->name, &sym->declared_at);
395 else
396 gfc_error ("Argument %qs of pure function %qs at %L must "
397 "be INTENT(IN) or VALUE", sym->name, proc->name,
398 &sym->declared_at);
401 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
403 if (sym->attr.value)
404 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
405 " of pure subroutine %qs at %L with VALUE "
406 "attribute but without INTENT", sym->name,
407 proc->name, &sym->declared_at);
408 else
409 gfc_error ("Argument %qs of pure subroutine %qs at %L "
410 "must have its INTENT specified or have the "
411 "VALUE attribute", sym->name, proc->name,
412 &sym->declared_at);
416 /* F08:C1278a. */
417 if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
419 gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
420 " may not be polymorphic", sym->name, proc->name,
421 &sym->declared_at);
422 continue;
426 if (proc->attr.implicit_pure)
428 if (sym->attr.flavor == FL_PROCEDURE)
430 if (!gfc_pure (sym))
431 proc->attr.implicit_pure = 0;
433 else if (!sym->attr.pointer)
435 if (proc->attr.function && sym->attr.intent != INTENT_IN
436 && !sym->value)
437 proc->attr.implicit_pure = 0;
439 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
440 && !sym->value)
441 proc->attr.implicit_pure = 0;
445 if (gfc_elemental (proc))
447 /* F08:C1289. */
448 if (sym->attr.codimension
449 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
450 && CLASS_DATA (sym)->attr.codimension))
452 gfc_error ("Coarray dummy argument %qs at %L to elemental "
453 "procedure", sym->name, &sym->declared_at);
454 continue;
457 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
458 && CLASS_DATA (sym)->as))
460 gfc_error ("Argument %qs of elemental procedure at %L must "
461 "be scalar", sym->name, &sym->declared_at);
462 continue;
465 if (sym->attr.allocatable
466 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
467 && CLASS_DATA (sym)->attr.allocatable))
469 gfc_error ("Argument %qs of elemental procedure at %L cannot "
470 "have the ALLOCATABLE attribute", sym->name,
471 &sym->declared_at);
472 continue;
475 if (sym->attr.pointer
476 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
477 && CLASS_DATA (sym)->attr.class_pointer))
479 gfc_error ("Argument %qs of elemental procedure at %L cannot "
480 "have the POINTER attribute", sym->name,
481 &sym->declared_at);
482 continue;
485 if (sym->attr.flavor == FL_PROCEDURE)
487 gfc_error ("Dummy procedure %qs not allowed in elemental "
488 "procedure %qs at %L", sym->name, proc->name,
489 &sym->declared_at);
490 continue;
493 /* Fortran 2008 Corrigendum 1, C1290a. */
494 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
496 gfc_error ("Argument %qs of elemental procedure %qs at %L must "
497 "have its INTENT specified or have the VALUE "
498 "attribute", sym->name, proc->name,
499 &sym->declared_at);
500 continue;
504 /* Each dummy shall be specified to be scalar. */
505 if (proc->attr.proc == PROC_ST_FUNCTION)
507 if (sym->as != NULL)
509 gfc_error ("Argument %qs of statement function at %L must "
510 "be scalar", sym->name, &sym->declared_at);
511 continue;
514 if (sym->ts.type == BT_CHARACTER)
516 gfc_charlen *cl = sym->ts.u.cl;
517 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
519 gfc_error ("Character-valued argument %qs of statement "
520 "function at %L must have constant length",
521 sym->name, &sym->declared_at);
522 continue;
527 formal_arg_flag = 0;
531 /* Work function called when searching for symbols that have argument lists
532 associated with them. */
534 static void
535 find_arglists (gfc_symbol *sym)
537 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
538 || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
539 return;
541 resolve_formal_arglist (sym);
545 /* Given a namespace, resolve all formal argument lists within the namespace.
548 static void
549 resolve_formal_arglists (gfc_namespace *ns)
551 if (ns == NULL)
552 return;
554 gfc_traverse_ns (ns, find_arglists);
558 static void
559 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
561 bool t;
563 /* If this namespace is not a function or an entry master function,
564 ignore it. */
565 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
566 || sym->attr.entry_master)
567 return;
569 /* Try to find out of what the return type is. */
570 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
572 t = gfc_set_default_type (sym->result, 0, ns);
574 if (!t && !sym->result->attr.untyped)
576 if (sym->result == sym)
577 gfc_error ("Contained function %qs at %L has no IMPLICIT type",
578 sym->name, &sym->declared_at);
579 else if (!sym->result->attr.proc_pointer)
580 gfc_error ("Result %qs of contained function %qs at %L has "
581 "no IMPLICIT type", sym->result->name, sym->name,
582 &sym->result->declared_at);
583 sym->result->attr.untyped = 1;
587 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
588 type, lists the only ways a character length value of * can be used:
589 dummy arguments of procedures, named constants, and function results
590 in external functions. Internal function results and results of module
591 procedures are not on this list, ergo, not permitted. */
593 if (sym->result->ts.type == BT_CHARACTER)
595 gfc_charlen *cl = sym->result->ts.u.cl;
596 if ((!cl || !cl->length) && !sym->result->ts.deferred)
598 /* See if this is a module-procedure and adapt error message
599 accordingly. */
600 bool module_proc;
601 gcc_assert (ns->parent && ns->parent->proc_name);
602 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
604 gfc_error ("Character-valued %s %qs at %L must not be"
605 " assumed length",
606 module_proc ? _("module procedure")
607 : _("internal function"),
608 sym->name, &sym->declared_at);
614 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
615 introduce duplicates. */
617 static void
618 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
620 gfc_formal_arglist *f, *new_arglist;
621 gfc_symbol *new_sym;
623 for (; new_args != NULL; new_args = new_args->next)
625 new_sym = new_args->sym;
626 /* See if this arg is already in the formal argument list. */
627 for (f = proc->formal; f; f = f->next)
629 if (new_sym == f->sym)
630 break;
633 if (f)
634 continue;
636 /* Add a new argument. Argument order is not important. */
637 new_arglist = gfc_get_formal_arglist ();
638 new_arglist->sym = new_sym;
639 new_arglist->next = proc->formal;
640 proc->formal = new_arglist;
645 /* Flag the arguments that are not present in all entries. */
647 static void
648 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
650 gfc_formal_arglist *f, *head;
651 head = new_args;
653 for (f = proc->formal; f; f = f->next)
655 if (f->sym == NULL)
656 continue;
658 for (new_args = head; new_args; new_args = new_args->next)
660 if (new_args->sym == f->sym)
661 break;
664 if (new_args)
665 continue;
667 f->sym->attr.not_always_present = 1;
672 /* Resolve alternate entry points. If a symbol has multiple entry points we
673 create a new master symbol for the main routine, and turn the existing
674 symbol into an entry point. */
676 static void
677 resolve_entries (gfc_namespace *ns)
679 gfc_namespace *old_ns;
680 gfc_code *c;
681 gfc_symbol *proc;
682 gfc_entry_list *el;
683 char name[GFC_MAX_SYMBOL_LEN + 1];
684 static int master_count = 0;
686 if (ns->proc_name == NULL)
687 return;
689 /* No need to do anything if this procedure doesn't have alternate entry
690 points. */
691 if (!ns->entries)
692 return;
694 /* We may already have resolved alternate entry points. */
695 if (ns->proc_name->attr.entry_master)
696 return;
698 /* If this isn't a procedure something has gone horribly wrong. */
699 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
701 /* Remember the current namespace. */
702 old_ns = gfc_current_ns;
704 gfc_current_ns = ns;
706 /* Add the main entry point to the list of entry points. */
707 el = gfc_get_entry_list ();
708 el->sym = ns->proc_name;
709 el->id = 0;
710 el->next = ns->entries;
711 ns->entries = el;
712 ns->proc_name->attr.entry = 1;
714 /* If it is a module function, it needs to be in the right namespace
715 so that gfc_get_fake_result_decl can gather up the results. The
716 need for this arose in get_proc_name, where these beasts were
717 left in their own namespace, to keep prior references linked to
718 the entry declaration.*/
719 if (ns->proc_name->attr.function
720 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
721 el->sym->ns = ns;
723 /* Do the same for entries where the master is not a module
724 procedure. These are retained in the module namespace because
725 of the module procedure declaration. */
726 for (el = el->next; el; el = el->next)
727 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
728 && el->sym->attr.mod_proc)
729 el->sym->ns = ns;
730 el = ns->entries;
732 /* Add an entry statement for it. */
733 c = gfc_get_code (EXEC_ENTRY);
734 c->ext.entry = el;
735 c->next = ns->code;
736 ns->code = c;
738 /* Create a new symbol for the master function. */
739 /* Give the internal function a unique name (within this file).
740 Also include the function name so the user has some hope of figuring
741 out what is going on. */
742 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
743 master_count++, ns->proc_name->name);
744 gfc_get_ha_symbol (name, &proc);
745 gcc_assert (proc != NULL);
747 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
748 if (ns->proc_name->attr.subroutine)
749 gfc_add_subroutine (&proc->attr, proc->name, NULL);
750 else
752 gfc_symbol *sym;
753 gfc_typespec *ts, *fts;
754 gfc_array_spec *as, *fas;
755 gfc_add_function (&proc->attr, proc->name, NULL);
756 proc->result = proc;
757 fas = ns->entries->sym->as;
758 fas = fas ? fas : ns->entries->sym->result->as;
759 fts = &ns->entries->sym->result->ts;
760 if (fts->type == BT_UNKNOWN)
761 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
762 for (el = ns->entries->next; el; el = el->next)
764 ts = &el->sym->result->ts;
765 as = el->sym->as;
766 as = as ? as : el->sym->result->as;
767 if (ts->type == BT_UNKNOWN)
768 ts = gfc_get_default_type (el->sym->result->name, NULL);
770 if (! gfc_compare_types (ts, fts)
771 || (el->sym->result->attr.dimension
772 != ns->entries->sym->result->attr.dimension)
773 || (el->sym->result->attr.pointer
774 != ns->entries->sym->result->attr.pointer))
775 break;
776 else if (as && fas && ns->entries->sym->result != el->sym->result
777 && gfc_compare_array_spec (as, fas) == 0)
778 gfc_error ("Function %s at %L has entries with mismatched "
779 "array specifications", ns->entries->sym->name,
780 &ns->entries->sym->declared_at);
781 /* The characteristics need to match and thus both need to have
782 the same string length, i.e. both len=*, or both len=4.
783 Having both len=<variable> is also possible, but difficult to
784 check at compile time. */
785 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
786 && (((ts->u.cl->length && !fts->u.cl->length)
787 ||(!ts->u.cl->length && fts->u.cl->length))
788 || (ts->u.cl->length
789 && ts->u.cl->length->expr_type
790 != fts->u.cl->length->expr_type)
791 || (ts->u.cl->length
792 && ts->u.cl->length->expr_type == EXPR_CONSTANT
793 && mpz_cmp (ts->u.cl->length->value.integer,
794 fts->u.cl->length->value.integer) != 0)))
795 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
796 "entries returning variables of different "
797 "string lengths", ns->entries->sym->name,
798 &ns->entries->sym->declared_at);
801 if (el == NULL)
803 sym = ns->entries->sym->result;
804 /* All result types the same. */
805 proc->ts = *fts;
806 if (sym->attr.dimension)
807 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
808 if (sym->attr.pointer)
809 gfc_add_pointer (&proc->attr, NULL);
811 else
813 /* Otherwise the result will be passed through a union by
814 reference. */
815 proc->attr.mixed_entry_master = 1;
816 for (el = ns->entries; el; el = el->next)
818 sym = el->sym->result;
819 if (sym->attr.dimension)
821 if (el == ns->entries)
822 gfc_error ("FUNCTION result %s can't be an array in "
823 "FUNCTION %s at %L", sym->name,
824 ns->entries->sym->name, &sym->declared_at);
825 else
826 gfc_error ("ENTRY result %s can't be an array in "
827 "FUNCTION %s at %L", sym->name,
828 ns->entries->sym->name, &sym->declared_at);
830 else if (sym->attr.pointer)
832 if (el == ns->entries)
833 gfc_error ("FUNCTION result %s can't be a POINTER in "
834 "FUNCTION %s at %L", sym->name,
835 ns->entries->sym->name, &sym->declared_at);
836 else
837 gfc_error ("ENTRY result %s can't be a POINTER in "
838 "FUNCTION %s at %L", sym->name,
839 ns->entries->sym->name, &sym->declared_at);
841 else
843 ts = &sym->ts;
844 if (ts->type == BT_UNKNOWN)
845 ts = gfc_get_default_type (sym->name, NULL);
846 switch (ts->type)
848 case BT_INTEGER:
849 if (ts->kind == gfc_default_integer_kind)
850 sym = NULL;
851 break;
852 case BT_REAL:
853 if (ts->kind == gfc_default_real_kind
854 || ts->kind == gfc_default_double_kind)
855 sym = NULL;
856 break;
857 case BT_COMPLEX:
858 if (ts->kind == gfc_default_complex_kind)
859 sym = NULL;
860 break;
861 case BT_LOGICAL:
862 if (ts->kind == gfc_default_logical_kind)
863 sym = NULL;
864 break;
865 case BT_UNKNOWN:
866 /* We will issue error elsewhere. */
867 sym = NULL;
868 break;
869 default:
870 break;
872 if (sym)
874 if (el == ns->entries)
875 gfc_error ("FUNCTION result %s can't be of type %s "
876 "in FUNCTION %s at %L", sym->name,
877 gfc_typename (ts), ns->entries->sym->name,
878 &sym->declared_at);
879 else
880 gfc_error ("ENTRY result %s can't be of type %s "
881 "in FUNCTION %s at %L", sym->name,
882 gfc_typename (ts), ns->entries->sym->name,
883 &sym->declared_at);
889 proc->attr.access = ACCESS_PRIVATE;
890 proc->attr.entry_master = 1;
892 /* Merge all the entry point arguments. */
893 for (el = ns->entries; el; el = el->next)
894 merge_argument_lists (proc, el->sym->formal);
896 /* Check the master formal arguments for any that are not
897 present in all entry points. */
898 for (el = ns->entries; el; el = el->next)
899 check_argument_lists (proc, el->sym->formal);
901 /* Use the master function for the function body. */
902 ns->proc_name = proc;
904 /* Finalize the new symbols. */
905 gfc_commit_symbols ();
907 /* Restore the original namespace. */
908 gfc_current_ns = old_ns;
912 /* Resolve common variables. */
913 static void
914 resolve_common_vars (gfc_common_head *common_block, bool named_common)
916 gfc_symbol *csym = common_block->head;
918 for (; csym; csym = csym->common_next)
920 /* gfc_add_in_common may have been called before, but the reported errors
921 have been ignored to continue parsing.
922 We do the checks again here. */
923 if (!csym->attr.use_assoc)
924 gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
926 if (csym->value || csym->attr.data)
928 if (!csym->ns->is_block_data)
929 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
930 "but only in BLOCK DATA initialization is "
931 "allowed", csym->name, &csym->declared_at);
932 else if (!named_common)
933 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
934 "in a blank COMMON but initialization is only "
935 "allowed in named common blocks", csym->name,
936 &csym->declared_at);
939 if (UNLIMITED_POLY (csym))
940 gfc_error_now ("%qs in cannot appear in COMMON at %L "
941 "[F2008:C5100]", csym->name, &csym->declared_at);
943 if (csym->ts.type != BT_DERIVED)
944 continue;
946 if (!(csym->ts.u.derived->attr.sequence
947 || csym->ts.u.derived->attr.is_bind_c))
948 gfc_error_now ("Derived type variable %qs in COMMON at %L "
949 "has neither the SEQUENCE nor the BIND(C) "
950 "attribute", csym->name, &csym->declared_at);
951 if (csym->ts.u.derived->attr.alloc_comp)
952 gfc_error_now ("Derived type variable %qs in COMMON at %L "
953 "has an ultimate component that is "
954 "allocatable", csym->name, &csym->declared_at);
955 if (gfc_has_default_initializer (csym->ts.u.derived))
956 gfc_error_now ("Derived type variable %qs in COMMON at %L "
957 "may not have default initializer", csym->name,
958 &csym->declared_at);
960 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
961 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
965 /* Resolve common blocks. */
966 static void
967 resolve_common_blocks (gfc_symtree *common_root)
969 gfc_symbol *sym;
970 gfc_gsymbol * gsym;
972 if (common_root == NULL)
973 return;
975 if (common_root->left)
976 resolve_common_blocks (common_root->left);
977 if (common_root->right)
978 resolve_common_blocks (common_root->right);
980 resolve_common_vars (common_root->n.common, true);
982 /* The common name is a global name - in Fortran 2003 also if it has a
983 C binding name, since Fortran 2008 only the C binding name is a global
984 identifier. */
985 if (!common_root->n.common->binding_label
986 || gfc_notification_std (GFC_STD_F2008))
988 gsym = gfc_find_gsymbol (gfc_gsym_root,
989 common_root->n.common->name);
991 if (gsym && gfc_notification_std (GFC_STD_F2008)
992 && gsym->type == GSYM_COMMON
993 && ((common_root->n.common->binding_label
994 && (!gsym->binding_label
995 || strcmp (common_root->n.common->binding_label,
996 gsym->binding_label) != 0))
997 || (!common_root->n.common->binding_label
998 && gsym->binding_label)))
1000 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1001 "identifier and must thus have the same binding name "
1002 "as the same-named COMMON block at %L: %s vs %s",
1003 common_root->n.common->name, &common_root->n.common->where,
1004 &gsym->where,
1005 common_root->n.common->binding_label
1006 ? common_root->n.common->binding_label : "(blank)",
1007 gsym->binding_label ? gsym->binding_label : "(blank)");
1008 return;
1011 if (gsym && gsym->type != GSYM_COMMON
1012 && !common_root->n.common->binding_label)
1014 gfc_error ("COMMON block %qs at %L uses the same global identifier "
1015 "as entity at %L",
1016 common_root->n.common->name, &common_root->n.common->where,
1017 &gsym->where);
1018 return;
1020 if (gsym && gsym->type != GSYM_COMMON)
1022 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1023 "%L sharing the identifier with global non-COMMON-block "
1024 "entity at %L", common_root->n.common->name,
1025 &common_root->n.common->where, &gsym->where);
1026 return;
1028 if (!gsym)
1030 gsym = gfc_get_gsymbol (common_root->n.common->name);
1031 gsym->type = GSYM_COMMON;
1032 gsym->where = common_root->n.common->where;
1033 gsym->defined = 1;
1035 gsym->used = 1;
1038 if (common_root->n.common->binding_label)
1040 gsym = gfc_find_gsymbol (gfc_gsym_root,
1041 common_root->n.common->binding_label);
1042 if (gsym && gsym->type != GSYM_COMMON)
1044 gfc_error ("COMMON block at %L with binding label %s uses the same "
1045 "global identifier as entity at %L",
1046 &common_root->n.common->where,
1047 common_root->n.common->binding_label, &gsym->where);
1048 return;
1050 if (!gsym)
1052 gsym = gfc_get_gsymbol (common_root->n.common->binding_label);
1053 gsym->type = GSYM_COMMON;
1054 gsym->where = common_root->n.common->where;
1055 gsym->defined = 1;
1057 gsym->used = 1;
1060 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1061 if (sym == NULL)
1062 return;
1064 if (sym->attr.flavor == FL_PARAMETER)
1065 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1066 sym->name, &common_root->n.common->where, &sym->declared_at);
1068 if (sym->attr.external)
1069 gfc_error ("COMMON block %qs at %L can not have the EXTERNAL attribute",
1070 sym->name, &common_root->n.common->where);
1072 if (sym->attr.intrinsic)
1073 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1074 sym->name, &common_root->n.common->where);
1075 else if (sym->attr.result
1076 || gfc_is_function_return_value (sym, gfc_current_ns))
1077 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1078 "that is also a function result", sym->name,
1079 &common_root->n.common->where);
1080 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1081 && sym->attr.proc != PROC_ST_FUNCTION)
1082 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1083 "that is also a global procedure", sym->name,
1084 &common_root->n.common->where);
1088 /* Resolve contained function types. Because contained functions can call one
1089 another, they have to be worked out before any of the contained procedures
1090 can be resolved.
1092 The good news is that if a function doesn't already have a type, the only
1093 way it can get one is through an IMPLICIT type or a RESULT variable, because
1094 by definition contained functions are contained namespace they're contained
1095 in, not in a sibling or parent namespace. */
1097 static void
1098 resolve_contained_functions (gfc_namespace *ns)
1100 gfc_namespace *child;
1101 gfc_entry_list *el;
1103 resolve_formal_arglists (ns);
1105 for (child = ns->contained; child; child = child->sibling)
1107 /* Resolve alternate entry points first. */
1108 resolve_entries (child);
1110 /* Then check function return types. */
1111 resolve_contained_fntype (child->proc_name, child);
1112 for (el = child->entries; el; el = el->next)
1113 resolve_contained_fntype (el->sym, child);
1118 static bool resolve_fl_derived0 (gfc_symbol *sym);
1119 static bool resolve_fl_struct (gfc_symbol *sym);
1122 /* Resolve all of the elements of a structure constructor and make sure that
1123 the types are correct. The 'init' flag indicates that the given
1124 constructor is an initializer. */
1126 static bool
1127 resolve_structure_cons (gfc_expr *expr, int init)
1129 gfc_constructor *cons;
1130 gfc_component *comp;
1131 bool t;
1132 symbol_attribute a;
1134 t = true;
1136 if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1138 if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1139 resolve_fl_derived0 (expr->ts.u.derived);
1140 else
1141 resolve_fl_struct (expr->ts.u.derived);
1144 cons = gfc_constructor_first (expr->value.constructor);
1146 /* A constructor may have references if it is the result of substituting a
1147 parameter variable. In this case we just pull out the component we
1148 want. */
1149 if (expr->ref)
1150 comp = expr->ref->u.c.sym->components;
1151 else
1152 comp = expr->ts.u.derived->components;
1154 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1156 int rank;
1158 if (!cons->expr)
1159 continue;
1161 if (!gfc_resolve_expr (cons->expr))
1163 t = false;
1164 continue;
1167 rank = comp->as ? comp->as->rank : 0;
1168 if (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->as)
1169 rank = CLASS_DATA (comp)->as->rank;
1171 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1172 && (comp->attr.allocatable || cons->expr->rank))
1174 gfc_error ("The rank of the element in the structure "
1175 "constructor at %L does not match that of the "
1176 "component (%d/%d)", &cons->expr->where,
1177 cons->expr->rank, rank);
1178 t = false;
1181 /* If we don't have the right type, try to convert it. */
1183 if (!comp->attr.proc_pointer &&
1184 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1186 if (strcmp (comp->name, "_extends") == 0)
1188 /* Can afford to be brutal with the _extends initializer.
1189 The derived type can get lost because it is PRIVATE
1190 but it is not usage constrained by the standard. */
1191 cons->expr->ts = comp->ts;
1193 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1195 gfc_error ("The element in the structure constructor at %L, "
1196 "for pointer component %qs, is %s but should be %s",
1197 &cons->expr->where, comp->name,
1198 gfc_basic_typename (cons->expr->ts.type),
1199 gfc_basic_typename (comp->ts.type));
1200 t = false;
1202 else
1204 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1205 if (t)
1206 t = t2;
1210 /* For strings, the length of the constructor should be the same as
1211 the one of the structure, ensure this if the lengths are known at
1212 compile time and when we are dealing with PARAMETER or structure
1213 constructors. */
1214 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1215 && comp->ts.u.cl->length
1216 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1217 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1218 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1219 && cons->expr->rank != 0
1220 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1221 comp->ts.u.cl->length->value.integer) != 0)
1223 if (cons->expr->expr_type == EXPR_VARIABLE
1224 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1226 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1227 to make use of the gfc_resolve_character_array_constructor
1228 machinery. The expression is later simplified away to
1229 an array of string literals. */
1230 gfc_expr *para = cons->expr;
1231 cons->expr = gfc_get_expr ();
1232 cons->expr->ts = para->ts;
1233 cons->expr->where = para->where;
1234 cons->expr->expr_type = EXPR_ARRAY;
1235 cons->expr->rank = para->rank;
1236 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1237 gfc_constructor_append_expr (&cons->expr->value.constructor,
1238 para, &cons->expr->where);
1240 if (cons->expr->expr_type == EXPR_ARRAY)
1242 gfc_constructor *p;
1243 p = gfc_constructor_first (cons->expr->value.constructor);
1244 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1246 gfc_charlen *cl, *cl2;
1248 cl2 = NULL;
1249 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1251 if (cl == cons->expr->ts.u.cl)
1252 break;
1253 cl2 = cl;
1256 gcc_assert (cl);
1258 if (cl2)
1259 cl2->next = cl->next;
1261 gfc_free_expr (cl->length);
1262 free (cl);
1265 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1266 cons->expr->ts.u.cl->length_from_typespec = true;
1267 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1268 gfc_resolve_character_array_constructor (cons->expr);
1272 if (cons->expr->expr_type == EXPR_NULL
1273 && !(comp->attr.pointer || comp->attr.allocatable
1274 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1275 || (comp->ts.type == BT_CLASS
1276 && (CLASS_DATA (comp)->attr.class_pointer
1277 || CLASS_DATA (comp)->attr.allocatable))))
1279 t = false;
1280 gfc_error ("The NULL in the structure constructor at %L is "
1281 "being applied to component %qs, which is neither "
1282 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1283 comp->name);
1286 if (comp->attr.proc_pointer && comp->ts.interface)
1288 /* Check procedure pointer interface. */
1289 gfc_symbol *s2 = NULL;
1290 gfc_component *c2;
1291 const char *name;
1292 char err[200];
1294 c2 = gfc_get_proc_ptr_comp (cons->expr);
1295 if (c2)
1297 s2 = c2->ts.interface;
1298 name = c2->name;
1300 else if (cons->expr->expr_type == EXPR_FUNCTION)
1302 s2 = cons->expr->symtree->n.sym->result;
1303 name = cons->expr->symtree->n.sym->result->name;
1305 else if (cons->expr->expr_type != EXPR_NULL)
1307 s2 = cons->expr->symtree->n.sym;
1308 name = cons->expr->symtree->n.sym->name;
1311 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1312 err, sizeof (err), NULL, NULL))
1314 gfc_error ("Interface mismatch for procedure-pointer component "
1315 "%qs in structure constructor at %L: %s",
1316 comp->name, &cons->expr->where, err);
1317 return false;
1321 if (!comp->attr.pointer || comp->attr.proc_pointer
1322 || cons->expr->expr_type == EXPR_NULL)
1323 continue;
1325 a = gfc_expr_attr (cons->expr);
1327 if (!a.pointer && !a.target)
1329 t = false;
1330 gfc_error ("The element in the structure constructor at %L, "
1331 "for pointer component %qs should be a POINTER or "
1332 "a TARGET", &cons->expr->where, comp->name);
1335 if (init)
1337 /* F08:C461. Additional checks for pointer initialization. */
1338 if (a.allocatable)
1340 t = false;
1341 gfc_error ("Pointer initialization target at %L "
1342 "must not be ALLOCATABLE ", &cons->expr->where);
1344 if (!a.save)
1346 t = false;
1347 gfc_error ("Pointer initialization target at %L "
1348 "must have the SAVE attribute", &cons->expr->where);
1352 /* F2003, C1272 (3). */
1353 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1354 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1355 || gfc_is_coindexed (cons->expr));
1356 if (impure && gfc_pure (NULL))
1358 t = false;
1359 gfc_error ("Invalid expression in the structure constructor for "
1360 "pointer component %qs at %L in PURE procedure",
1361 comp->name, &cons->expr->where);
1364 if (impure)
1365 gfc_unset_implicit_pure (NULL);
1368 return t;
1372 /****************** Expression name resolution ******************/
1374 /* Returns 0 if a symbol was not declared with a type or
1375 attribute declaration statement, nonzero otherwise. */
1377 static int
1378 was_declared (gfc_symbol *sym)
1380 symbol_attribute a;
1382 a = sym->attr;
1384 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1385 return 1;
1387 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1388 || a.optional || a.pointer || a.save || a.target || a.volatile_
1389 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1390 || a.asynchronous || a.codimension)
1391 return 1;
1393 return 0;
1397 /* Determine if a symbol is generic or not. */
1399 static int
1400 generic_sym (gfc_symbol *sym)
1402 gfc_symbol *s;
1404 if (sym->attr.generic ||
1405 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1406 return 1;
1408 if (was_declared (sym) || sym->ns->parent == NULL)
1409 return 0;
1411 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1413 if (s != NULL)
1415 if (s == sym)
1416 return 0;
1417 else
1418 return generic_sym (s);
1421 return 0;
1425 /* Determine if a symbol is specific or not. */
1427 static int
1428 specific_sym (gfc_symbol *sym)
1430 gfc_symbol *s;
1432 if (sym->attr.if_source == IFSRC_IFBODY
1433 || sym->attr.proc == PROC_MODULE
1434 || sym->attr.proc == PROC_INTERNAL
1435 || sym->attr.proc == PROC_ST_FUNCTION
1436 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1437 || sym->attr.external)
1438 return 1;
1440 if (was_declared (sym) || sym->ns->parent == NULL)
1441 return 0;
1443 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1445 return (s == NULL) ? 0 : specific_sym (s);
1449 /* Figure out if the procedure is specific, generic or unknown. */
1451 enum proc_type
1452 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1454 static proc_type
1455 procedure_kind (gfc_symbol *sym)
1457 if (generic_sym (sym))
1458 return PTYPE_GENERIC;
1460 if (specific_sym (sym))
1461 return PTYPE_SPECIFIC;
1463 return PTYPE_UNKNOWN;
1466 /* Check references to assumed size arrays. The flag need_full_assumed_size
1467 is nonzero when matching actual arguments. */
1469 static int need_full_assumed_size = 0;
1471 static bool
1472 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1474 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1475 return false;
1477 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1478 What should it be? */
1479 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1480 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1481 && (e->ref->u.ar.type == AR_FULL))
1483 gfc_error ("The upper bound in the last dimension must "
1484 "appear in the reference to the assumed size "
1485 "array %qs at %L", sym->name, &e->where);
1486 return true;
1488 return false;
1492 /* Look for bad assumed size array references in argument expressions
1493 of elemental and array valued intrinsic procedures. Since this is
1494 called from procedure resolution functions, it only recurses at
1495 operators. */
1497 static bool
1498 resolve_assumed_size_actual (gfc_expr *e)
1500 if (e == NULL)
1501 return false;
1503 switch (e->expr_type)
1505 case EXPR_VARIABLE:
1506 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1507 return true;
1508 break;
1510 case EXPR_OP:
1511 if (resolve_assumed_size_actual (e->value.op.op1)
1512 || resolve_assumed_size_actual (e->value.op.op2))
1513 return true;
1514 break;
1516 default:
1517 break;
1519 return false;
1523 /* Check a generic procedure, passed as an actual argument, to see if
1524 there is a matching specific name. If none, it is an error, and if
1525 more than one, the reference is ambiguous. */
1526 static int
1527 count_specific_procs (gfc_expr *e)
1529 int n;
1530 gfc_interface *p;
1531 gfc_symbol *sym;
1533 n = 0;
1534 sym = e->symtree->n.sym;
1536 for (p = sym->generic; p; p = p->next)
1537 if (strcmp (sym->name, p->sym->name) == 0)
1539 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1540 sym->name);
1541 n++;
1544 if (n > 1)
1545 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1546 &e->where);
1548 if (n == 0)
1549 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1550 "argument at %L", sym->name, &e->where);
1552 return n;
1556 /* See if a call to sym could possibly be a not allowed RECURSION because of
1557 a missing RECURSIVE declaration. This means that either sym is the current
1558 context itself, or sym is the parent of a contained procedure calling its
1559 non-RECURSIVE containing procedure.
1560 This also works if sym is an ENTRY. */
1562 static bool
1563 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1565 gfc_symbol* proc_sym;
1566 gfc_symbol* context_proc;
1567 gfc_namespace* real_context;
1569 if (sym->attr.flavor == FL_PROGRAM
1570 || gfc_fl_struct (sym->attr.flavor))
1571 return false;
1573 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1575 /* If we've got an ENTRY, find real procedure. */
1576 if (sym->attr.entry && sym->ns->entries)
1577 proc_sym = sym->ns->entries->sym;
1578 else
1579 proc_sym = sym;
1581 /* If sym is RECURSIVE, all is well of course. */
1582 if (proc_sym->attr.recursive || flag_recursive)
1583 return false;
1585 /* Find the context procedure's "real" symbol if it has entries.
1586 We look for a procedure symbol, so recurse on the parents if we don't
1587 find one (like in case of a BLOCK construct). */
1588 for (real_context = context; ; real_context = real_context->parent)
1590 /* We should find something, eventually! */
1591 gcc_assert (real_context);
1593 context_proc = (real_context->entries ? real_context->entries->sym
1594 : real_context->proc_name);
1596 /* In some special cases, there may not be a proc_name, like for this
1597 invalid code:
1598 real(bad_kind()) function foo () ...
1599 when checking the call to bad_kind ().
1600 In these cases, we simply return here and assume that the
1601 call is ok. */
1602 if (!context_proc)
1603 return false;
1605 if (context_proc->attr.flavor != FL_LABEL)
1606 break;
1609 /* A call from sym's body to itself is recursion, of course. */
1610 if (context_proc == proc_sym)
1611 return true;
1613 /* The same is true if context is a contained procedure and sym the
1614 containing one. */
1615 if (context_proc->attr.contained)
1617 gfc_symbol* parent_proc;
1619 gcc_assert (context->parent);
1620 parent_proc = (context->parent->entries ? context->parent->entries->sym
1621 : context->parent->proc_name);
1623 if (parent_proc == proc_sym)
1624 return true;
1627 return false;
1631 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1632 its typespec and formal argument list. */
1634 bool
1635 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1637 gfc_intrinsic_sym* isym = NULL;
1638 const char* symstd;
1640 if (sym->formal)
1641 return true;
1643 /* Already resolved. */
1644 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1645 return true;
1647 /* We already know this one is an intrinsic, so we don't call
1648 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1649 gfc_find_subroutine directly to check whether it is a function or
1650 subroutine. */
1652 if (sym->intmod_sym_id && sym->attr.subroutine)
1654 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1655 isym = gfc_intrinsic_subroutine_by_id (id);
1657 else if (sym->intmod_sym_id)
1659 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1660 isym = gfc_intrinsic_function_by_id (id);
1662 else if (!sym->attr.subroutine)
1663 isym = gfc_find_function (sym->name);
1665 if (isym && !sym->attr.subroutine)
1667 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1668 && !sym->attr.implicit_type)
1669 gfc_warning (OPT_Wsurprising,
1670 "Type specified for intrinsic function %qs at %L is"
1671 " ignored", sym->name, &sym->declared_at);
1673 if (!sym->attr.function &&
1674 !gfc_add_function(&sym->attr, sym->name, loc))
1675 return false;
1677 sym->ts = isym->ts;
1679 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1681 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1683 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1684 " specifier", sym->name, &sym->declared_at);
1685 return false;
1688 if (!sym->attr.subroutine &&
1689 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1690 return false;
1692 else
1694 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1695 &sym->declared_at);
1696 return false;
1699 gfc_copy_formal_args_intr (sym, isym, NULL);
1701 sym->attr.pure = isym->pure;
1702 sym->attr.elemental = isym->elemental;
1704 /* Check it is actually available in the standard settings. */
1705 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1707 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1708 "available in the current standard settings but %s. Use "
1709 "an appropriate %<-std=*%> option or enable "
1710 "%<-fall-intrinsics%> in order to use it.",
1711 sym->name, &sym->declared_at, symstd);
1712 return false;
1715 return true;
1719 /* Resolve a procedure expression, like passing it to a called procedure or as
1720 RHS for a procedure pointer assignment. */
1722 static bool
1723 resolve_procedure_expression (gfc_expr* expr)
1725 gfc_symbol* sym;
1727 if (expr->expr_type != EXPR_VARIABLE)
1728 return true;
1729 gcc_assert (expr->symtree);
1731 sym = expr->symtree->n.sym;
1733 if (sym->attr.intrinsic)
1734 gfc_resolve_intrinsic (sym, &expr->where);
1736 if (sym->attr.flavor != FL_PROCEDURE
1737 || (sym->attr.function && sym->result == sym))
1738 return true;
1740 /* A non-RECURSIVE procedure that is used as procedure expression within its
1741 own body is in danger of being called recursively. */
1742 if (is_illegal_recursion (sym, gfc_current_ns))
1743 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1744 " itself recursively. Declare it RECURSIVE or use"
1745 " %<-frecursive%>", sym->name, &expr->where);
1747 return true;
1751 /* Resolve an actual argument list. Most of the time, this is just
1752 resolving the expressions in the list.
1753 The exception is that we sometimes have to decide whether arguments
1754 that look like procedure arguments are really simple variable
1755 references. */
1757 static bool
1758 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1759 bool no_formal_args)
1761 gfc_symbol *sym;
1762 gfc_symtree *parent_st;
1763 gfc_expr *e;
1764 gfc_component *comp;
1765 int save_need_full_assumed_size;
1766 bool return_value = false;
1767 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1769 actual_arg = true;
1770 first_actual_arg = true;
1772 for (; arg; arg = arg->next)
1774 e = arg->expr;
1775 if (e == NULL)
1777 /* Check the label is a valid branching target. */
1778 if (arg->label)
1780 if (arg->label->defined == ST_LABEL_UNKNOWN)
1782 gfc_error ("Label %d referenced at %L is never defined",
1783 arg->label->value, &arg->label->where);
1784 goto cleanup;
1787 first_actual_arg = false;
1788 continue;
1791 if (e->expr_type == EXPR_VARIABLE
1792 && e->symtree->n.sym->attr.generic
1793 && no_formal_args
1794 && count_specific_procs (e) != 1)
1795 goto cleanup;
1797 if (e->ts.type != BT_PROCEDURE)
1799 save_need_full_assumed_size = need_full_assumed_size;
1800 if (e->expr_type != EXPR_VARIABLE)
1801 need_full_assumed_size = 0;
1802 if (!gfc_resolve_expr (e))
1803 goto cleanup;
1804 need_full_assumed_size = save_need_full_assumed_size;
1805 goto argument_list;
1808 /* See if the expression node should really be a variable reference. */
1810 sym = e->symtree->n.sym;
1812 if (sym->attr.flavor == FL_PROCEDURE
1813 || sym->attr.intrinsic
1814 || sym->attr.external)
1816 int actual_ok;
1818 /* If a procedure is not already determined to be something else
1819 check if it is intrinsic. */
1820 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1821 sym->attr.intrinsic = 1;
1823 if (sym->attr.proc == PROC_ST_FUNCTION)
1825 gfc_error ("Statement function %qs at %L is not allowed as an "
1826 "actual argument", sym->name, &e->where);
1829 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1830 sym->attr.subroutine);
1831 if (sym->attr.intrinsic && actual_ok == 0)
1833 gfc_error ("Intrinsic %qs at %L is not allowed as an "
1834 "actual argument", sym->name, &e->where);
1837 if (sym->attr.contained && !sym->attr.use_assoc
1838 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1840 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
1841 " used as actual argument at %L",
1842 sym->name, &e->where))
1843 goto cleanup;
1846 if (sym->attr.elemental && !sym->attr.intrinsic)
1848 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
1849 "allowed as an actual argument at %L", sym->name,
1850 &e->where);
1853 /* Check if a generic interface has a specific procedure
1854 with the same name before emitting an error. */
1855 if (sym->attr.generic && count_specific_procs (e) != 1)
1856 goto cleanup;
1858 /* Just in case a specific was found for the expression. */
1859 sym = e->symtree->n.sym;
1861 /* If the symbol is the function that names the current (or
1862 parent) scope, then we really have a variable reference. */
1864 if (gfc_is_function_return_value (sym, sym->ns))
1865 goto got_variable;
1867 /* If all else fails, see if we have a specific intrinsic. */
1868 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1870 gfc_intrinsic_sym *isym;
1872 isym = gfc_find_function (sym->name);
1873 if (isym == NULL || !isym->specific)
1875 gfc_error ("Unable to find a specific INTRINSIC procedure "
1876 "for the reference %qs at %L", sym->name,
1877 &e->where);
1878 goto cleanup;
1880 sym->ts = isym->ts;
1881 sym->attr.intrinsic = 1;
1882 sym->attr.function = 1;
1885 if (!gfc_resolve_expr (e))
1886 goto cleanup;
1887 goto argument_list;
1890 /* See if the name is a module procedure in a parent unit. */
1892 if (was_declared (sym) || sym->ns->parent == NULL)
1893 goto got_variable;
1895 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1897 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
1898 goto cleanup;
1901 if (parent_st == NULL)
1902 goto got_variable;
1904 sym = parent_st->n.sym;
1905 e->symtree = parent_st; /* Point to the right thing. */
1907 if (sym->attr.flavor == FL_PROCEDURE
1908 || sym->attr.intrinsic
1909 || sym->attr.external)
1911 if (!gfc_resolve_expr (e))
1912 goto cleanup;
1913 goto argument_list;
1916 got_variable:
1917 e->expr_type = EXPR_VARIABLE;
1918 e->ts = sym->ts;
1919 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1920 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1921 && CLASS_DATA (sym)->as))
1923 e->rank = sym->ts.type == BT_CLASS
1924 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
1925 e->ref = gfc_get_ref ();
1926 e->ref->type = REF_ARRAY;
1927 e->ref->u.ar.type = AR_FULL;
1928 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1929 ? CLASS_DATA (sym)->as : sym->as;
1932 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1933 primary.c (match_actual_arg). If above code determines that it
1934 is a variable instead, it needs to be resolved as it was not
1935 done at the beginning of this function. */
1936 save_need_full_assumed_size = need_full_assumed_size;
1937 if (e->expr_type != EXPR_VARIABLE)
1938 need_full_assumed_size = 0;
1939 if (!gfc_resolve_expr (e))
1940 goto cleanup;
1941 need_full_assumed_size = save_need_full_assumed_size;
1943 argument_list:
1944 /* Check argument list functions %VAL, %LOC and %REF. There is
1945 nothing to do for %REF. */
1946 if (arg->name && arg->name[0] == '%')
1948 if (strncmp ("%VAL", arg->name, 4) == 0)
1950 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1952 gfc_error ("By-value argument at %L is not of numeric "
1953 "type", &e->where);
1954 goto cleanup;
1957 if (e->rank)
1959 gfc_error ("By-value argument at %L cannot be an array or "
1960 "an array section", &e->where);
1961 goto cleanup;
1964 /* Intrinsics are still PROC_UNKNOWN here. However,
1965 since same file external procedures are not resolvable
1966 in gfortran, it is a good deal easier to leave them to
1967 intrinsic.c. */
1968 if (ptype != PROC_UNKNOWN
1969 && ptype != PROC_DUMMY
1970 && ptype != PROC_EXTERNAL
1971 && ptype != PROC_MODULE)
1973 gfc_error ("By-value argument at %L is not allowed "
1974 "in this context", &e->where);
1975 goto cleanup;
1979 /* Statement functions have already been excluded above. */
1980 else if (strncmp ("%LOC", arg->name, 4) == 0
1981 && e->ts.type == BT_PROCEDURE)
1983 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1985 gfc_error ("Passing internal procedure at %L by location "
1986 "not allowed", &e->where);
1987 goto cleanup;
1992 comp = gfc_get_proc_ptr_comp(e);
1993 if (e->expr_type == EXPR_VARIABLE
1994 && comp && comp->attr.elemental)
1996 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
1997 "allowed as an actual argument at %L", comp->name,
1998 &e->where);
2001 /* Fortran 2008, C1237. */
2002 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2003 && gfc_has_ultimate_pointer (e))
2005 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2006 "component", &e->where);
2007 goto cleanup;
2010 first_actual_arg = false;
2013 return_value = true;
2015 cleanup:
2016 actual_arg = actual_arg_sav;
2017 first_actual_arg = first_actual_arg_sav;
2019 return return_value;
2023 /* Do the checks of the actual argument list that are specific to elemental
2024 procedures. If called with c == NULL, we have a function, otherwise if
2025 expr == NULL, we have a subroutine. */
2027 static bool
2028 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2030 gfc_actual_arglist *arg0;
2031 gfc_actual_arglist *arg;
2032 gfc_symbol *esym = NULL;
2033 gfc_intrinsic_sym *isym = NULL;
2034 gfc_expr *e = NULL;
2035 gfc_intrinsic_arg *iformal = NULL;
2036 gfc_formal_arglist *eformal = NULL;
2037 bool formal_optional = false;
2038 bool set_by_optional = false;
2039 int i;
2040 int rank = 0;
2042 /* Is this an elemental procedure? */
2043 if (expr && expr->value.function.actual != NULL)
2045 if (expr->value.function.esym != NULL
2046 && expr->value.function.esym->attr.elemental)
2048 arg0 = expr->value.function.actual;
2049 esym = expr->value.function.esym;
2051 else if (expr->value.function.isym != NULL
2052 && expr->value.function.isym->elemental)
2054 arg0 = expr->value.function.actual;
2055 isym = expr->value.function.isym;
2057 else
2058 return true;
2060 else if (c && c->ext.actual != NULL)
2062 arg0 = c->ext.actual;
2064 if (c->resolved_sym)
2065 esym = c->resolved_sym;
2066 else
2067 esym = c->symtree->n.sym;
2068 gcc_assert (esym);
2070 if (!esym->attr.elemental)
2071 return true;
2073 else
2074 return true;
2076 /* The rank of an elemental is the rank of its array argument(s). */
2077 for (arg = arg0; arg; arg = arg->next)
2079 if (arg->expr != NULL && arg->expr->rank != 0)
2081 rank = arg->expr->rank;
2082 if (arg->expr->expr_type == EXPR_VARIABLE
2083 && arg->expr->symtree->n.sym->attr.optional)
2084 set_by_optional = true;
2086 /* Function specific; set the result rank and shape. */
2087 if (expr)
2089 expr->rank = rank;
2090 if (!expr->shape && arg->expr->shape)
2092 expr->shape = gfc_get_shape (rank);
2093 for (i = 0; i < rank; i++)
2094 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2097 break;
2101 /* If it is an array, it shall not be supplied as an actual argument
2102 to an elemental procedure unless an array of the same rank is supplied
2103 as an actual argument corresponding to a nonoptional dummy argument of
2104 that elemental procedure(12.4.1.5). */
2105 formal_optional = false;
2106 if (isym)
2107 iformal = isym->formal;
2108 else
2109 eformal = esym->formal;
2111 for (arg = arg0; arg; arg = arg->next)
2113 if (eformal)
2115 if (eformal->sym && eformal->sym->attr.optional)
2116 formal_optional = true;
2117 eformal = eformal->next;
2119 else if (isym && iformal)
2121 if (iformal->optional)
2122 formal_optional = true;
2123 iformal = iformal->next;
2125 else if (isym)
2126 formal_optional = true;
2128 if (pedantic && arg->expr != NULL
2129 && arg->expr->expr_type == EXPR_VARIABLE
2130 && arg->expr->symtree->n.sym->attr.optional
2131 && formal_optional
2132 && arg->expr->rank
2133 && (set_by_optional || arg->expr->rank != rank)
2134 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2136 gfc_warning (0, "%qs at %L is an array and OPTIONAL; IF IT IS "
2137 "MISSING, it cannot be the actual argument of an "
2138 "ELEMENTAL procedure unless there is a non-optional "
2139 "argument with the same rank (12.4.1.5)",
2140 arg->expr->symtree->n.sym->name, &arg->expr->where);
2144 for (arg = arg0; arg; arg = arg->next)
2146 if (arg->expr == NULL || arg->expr->rank == 0)
2147 continue;
2149 /* Being elemental, the last upper bound of an assumed size array
2150 argument must be present. */
2151 if (resolve_assumed_size_actual (arg->expr))
2152 return false;
2154 /* Elemental procedure's array actual arguments must conform. */
2155 if (e != NULL)
2157 if (!gfc_check_conformance (arg->expr, e, "elemental procedure"))
2158 return false;
2160 else
2161 e = arg->expr;
2164 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2165 is an array, the intent inout/out variable needs to be also an array. */
2166 if (rank > 0 && esym && expr == NULL)
2167 for (eformal = esym->formal, arg = arg0; arg && eformal;
2168 arg = arg->next, eformal = eformal->next)
2169 if ((eformal->sym->attr.intent == INTENT_OUT
2170 || eformal->sym->attr.intent == INTENT_INOUT)
2171 && arg->expr && arg->expr->rank == 0)
2173 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2174 "ELEMENTAL subroutine %qs is a scalar, but another "
2175 "actual argument is an array", &arg->expr->where,
2176 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2177 : "INOUT", eformal->sym->name, esym->name);
2178 return false;
2180 return true;
2184 /* This function does the checking of references to global procedures
2185 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2186 77 and 95 standards. It checks for a gsymbol for the name, making
2187 one if it does not already exist. If it already exists, then the
2188 reference being resolved must correspond to the type of gsymbol.
2189 Otherwise, the new symbol is equipped with the attributes of the
2190 reference. The corresponding code that is called in creating
2191 global entities is parse.c.
2193 In addition, for all but -std=legacy, the gsymbols are used to
2194 check the interfaces of external procedures from the same file.
2195 The namespace of the gsymbol is resolved and then, once this is
2196 done the interface is checked. */
2199 static bool
2200 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2202 if (!gsym_ns->proc_name->attr.recursive)
2203 return true;
2205 if (sym->ns == gsym_ns)
2206 return false;
2208 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2209 return false;
2211 return true;
2214 static bool
2215 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2217 if (gsym_ns->entries)
2219 gfc_entry_list *entry = gsym_ns->entries;
2221 for (; entry; entry = entry->next)
2223 if (strcmp (sym->name, entry->sym->name) == 0)
2225 if (strcmp (gsym_ns->proc_name->name,
2226 sym->ns->proc_name->name) == 0)
2227 return false;
2229 if (sym->ns->parent
2230 && strcmp (gsym_ns->proc_name->name,
2231 sym->ns->parent->proc_name->name) == 0)
2232 return false;
2236 return true;
2240 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2242 bool
2243 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2245 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2247 for ( ; arg; arg = arg->next)
2249 if (!arg->sym)
2250 continue;
2252 if (arg->sym->attr.allocatable) /* (2a) */
2254 strncpy (errmsg, _("allocatable argument"), err_len);
2255 return true;
2257 else if (arg->sym->attr.asynchronous)
2259 strncpy (errmsg, _("asynchronous argument"), err_len);
2260 return true;
2262 else if (arg->sym->attr.optional)
2264 strncpy (errmsg, _("optional argument"), err_len);
2265 return true;
2267 else if (arg->sym->attr.pointer)
2269 strncpy (errmsg, _("pointer argument"), err_len);
2270 return true;
2272 else if (arg->sym->attr.target)
2274 strncpy (errmsg, _("target argument"), err_len);
2275 return true;
2277 else if (arg->sym->attr.value)
2279 strncpy (errmsg, _("value argument"), err_len);
2280 return true;
2282 else if (arg->sym->attr.volatile_)
2284 strncpy (errmsg, _("volatile argument"), err_len);
2285 return true;
2287 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2289 strncpy (errmsg, _("assumed-shape argument"), err_len);
2290 return true;
2292 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2294 strncpy (errmsg, _("assumed-rank argument"), err_len);
2295 return true;
2297 else if (arg->sym->attr.codimension) /* (2c) */
2299 strncpy (errmsg, _("coarray argument"), err_len);
2300 return true;
2302 else if (false) /* (2d) TODO: parametrized derived type */
2304 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2305 return true;
2307 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2309 strncpy (errmsg, _("polymorphic argument"), err_len);
2310 return true;
2312 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2314 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2315 return true;
2317 else if (arg->sym->ts.type == BT_ASSUMED)
2319 /* As assumed-type is unlimited polymorphic (cf. above).
2320 See also TS 29113, Note 6.1. */
2321 strncpy (errmsg, _("assumed-type argument"), err_len);
2322 return true;
2326 if (sym->attr.function)
2328 gfc_symbol *res = sym->result ? sym->result : sym;
2330 if (res->attr.dimension) /* (3a) */
2332 strncpy (errmsg, _("array result"), err_len);
2333 return true;
2335 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2337 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2338 return true;
2340 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2341 && res->ts.u.cl->length
2342 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2344 strncpy (errmsg, _("result with non-constant character length"), err_len);
2345 return true;
2349 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2351 strncpy (errmsg, _("elemental procedure"), err_len);
2352 return true;
2354 else if (sym->attr.is_bind_c) /* (5) */
2356 strncpy (errmsg, _("bind(c) procedure"), err_len);
2357 return true;
2360 return false;
2364 static void
2365 resolve_global_procedure (gfc_symbol *sym, locus *where,
2366 gfc_actual_arglist **actual, int sub)
2368 gfc_gsymbol * gsym;
2369 gfc_namespace *ns;
2370 enum gfc_symbol_type type;
2371 char reason[200];
2373 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2375 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name);
2377 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2378 gfc_global_used (gsym, where);
2380 if ((sym->attr.if_source == IFSRC_UNKNOWN
2381 || sym->attr.if_source == IFSRC_IFBODY)
2382 && gsym->type != GSYM_UNKNOWN
2383 && !gsym->binding_label
2384 && gsym->ns
2385 && gsym->ns->resolved != -1
2386 && gsym->ns->proc_name
2387 && not_in_recursive (sym, gsym->ns)
2388 && not_entry_self_reference (sym, gsym->ns))
2390 gfc_symbol *def_sym;
2392 /* Resolve the gsymbol namespace if needed. */
2393 if (!gsym->ns->resolved)
2395 gfc_dt_list *old_dt_list;
2397 /* Stash away derived types so that the backend_decls do not
2398 get mixed up. */
2399 old_dt_list = gfc_derived_types;
2400 gfc_derived_types = NULL;
2402 gfc_resolve (gsym->ns);
2404 /* Store the new derived types with the global namespace. */
2405 if (gfc_derived_types)
2406 gsym->ns->derived_types = gfc_derived_types;
2408 /* Restore the derived types of this namespace. */
2409 gfc_derived_types = old_dt_list;
2412 /* Make sure that translation for the gsymbol occurs before
2413 the procedure currently being resolved. */
2414 ns = gfc_global_ns_list;
2415 for (; ns && ns != gsym->ns; ns = ns->sibling)
2417 if (ns->sibling == gsym->ns)
2419 ns->sibling = gsym->ns->sibling;
2420 gsym->ns->sibling = gfc_global_ns_list;
2421 gfc_global_ns_list = gsym->ns;
2422 break;
2426 def_sym = gsym->ns->proc_name;
2428 /* This can happen if a binding name has been specified. */
2429 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2430 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2432 if (def_sym->attr.entry_master)
2434 gfc_entry_list *entry;
2435 for (entry = gsym->ns->entries; entry; entry = entry->next)
2436 if (strcmp (entry->sym->name, sym->name) == 0)
2438 def_sym = entry->sym;
2439 break;
2443 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2445 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2446 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2447 gfc_typename (&def_sym->ts));
2448 goto done;
2451 if (sym->attr.if_source == IFSRC_UNKNOWN
2452 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2454 gfc_error ("Explicit interface required for %qs at %L: %s",
2455 sym->name, &sym->declared_at, reason);
2456 goto done;
2459 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2460 /* Turn erros into warnings with -std=gnu and -std=legacy. */
2461 gfc_errors_to_warnings (true);
2463 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2464 reason, sizeof(reason), NULL, NULL))
2466 gfc_error ("Interface mismatch in global procedure %qs at %L: %s ",
2467 sym->name, &sym->declared_at, reason);
2468 goto done;
2471 if (!pedantic
2472 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2473 && !(gfc_option.warn_std & GFC_STD_GNU)))
2474 gfc_errors_to_warnings (true);
2476 if (sym->attr.if_source != IFSRC_IFBODY)
2477 gfc_procedure_use (def_sym, actual, where);
2480 done:
2481 gfc_errors_to_warnings (false);
2483 if (gsym->type == GSYM_UNKNOWN)
2485 gsym->type = type;
2486 gsym->where = *where;
2489 gsym->used = 1;
2493 /************* Function resolution *************/
2495 /* Resolve a function call known to be generic.
2496 Section 14.1.2.4.1. */
2498 static match
2499 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2501 gfc_symbol *s;
2503 if (sym->attr.generic)
2505 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2506 if (s != NULL)
2508 expr->value.function.name = s->name;
2509 expr->value.function.esym = s;
2511 if (s->ts.type != BT_UNKNOWN)
2512 expr->ts = s->ts;
2513 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2514 expr->ts = s->result->ts;
2516 if (s->as != NULL)
2517 expr->rank = s->as->rank;
2518 else if (s->result != NULL && s->result->as != NULL)
2519 expr->rank = s->result->as->rank;
2521 gfc_set_sym_referenced (expr->value.function.esym);
2523 return MATCH_YES;
2526 /* TODO: Need to search for elemental references in generic
2527 interface. */
2530 if (sym->attr.intrinsic)
2531 return gfc_intrinsic_func_interface (expr, 0);
2533 return MATCH_NO;
2537 static bool
2538 resolve_generic_f (gfc_expr *expr)
2540 gfc_symbol *sym;
2541 match m;
2542 gfc_interface *intr = NULL;
2544 sym = expr->symtree->n.sym;
2546 for (;;)
2548 m = resolve_generic_f0 (expr, sym);
2549 if (m == MATCH_YES)
2550 return true;
2551 else if (m == MATCH_ERROR)
2552 return false;
2554 generic:
2555 if (!intr)
2556 for (intr = sym->generic; intr; intr = intr->next)
2557 if (gfc_fl_struct (intr->sym->attr.flavor))
2558 break;
2560 if (sym->ns->parent == NULL)
2561 break;
2562 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2564 if (sym == NULL)
2565 break;
2566 if (!generic_sym (sym))
2567 goto generic;
2570 /* Last ditch attempt. See if the reference is to an intrinsic
2571 that possesses a matching interface. 14.1.2.4 */
2572 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2574 if (gfc_init_expr_flag)
2575 gfc_error ("Function %qs in initialization expression at %L "
2576 "must be an intrinsic function",
2577 expr->symtree->n.sym->name, &expr->where);
2578 else
2579 gfc_error ("There is no specific function for the generic %qs "
2580 "at %L", expr->symtree->n.sym->name, &expr->where);
2581 return false;
2584 if (intr)
2586 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2587 NULL, false))
2588 return false;
2589 return resolve_structure_cons (expr, 0);
2592 m = gfc_intrinsic_func_interface (expr, 0);
2593 if (m == MATCH_YES)
2594 return true;
2596 if (m == MATCH_NO)
2597 gfc_error ("Generic function %qs at %L is not consistent with a "
2598 "specific intrinsic interface", expr->symtree->n.sym->name,
2599 &expr->where);
2601 return false;
2605 /* Resolve a function call known to be specific. */
2607 static match
2608 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2610 match m;
2612 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2614 if (sym->attr.dummy)
2616 sym->attr.proc = PROC_DUMMY;
2617 goto found;
2620 sym->attr.proc = PROC_EXTERNAL;
2621 goto found;
2624 if (sym->attr.proc == PROC_MODULE
2625 || sym->attr.proc == PROC_ST_FUNCTION
2626 || sym->attr.proc == PROC_INTERNAL)
2627 goto found;
2629 if (sym->attr.intrinsic)
2631 m = gfc_intrinsic_func_interface (expr, 1);
2632 if (m == MATCH_YES)
2633 return MATCH_YES;
2634 if (m == MATCH_NO)
2635 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2636 "with an intrinsic", sym->name, &expr->where);
2638 return MATCH_ERROR;
2641 return MATCH_NO;
2643 found:
2644 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2646 if (sym->result)
2647 expr->ts = sym->result->ts;
2648 else
2649 expr->ts = sym->ts;
2650 expr->value.function.name = sym->name;
2651 expr->value.function.esym = sym;
2652 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2653 error(s). */
2654 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2655 return MATCH_ERROR;
2656 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2657 expr->rank = CLASS_DATA (sym)->as->rank;
2658 else if (sym->as != NULL)
2659 expr->rank = sym->as->rank;
2661 return MATCH_YES;
2665 static bool
2666 resolve_specific_f (gfc_expr *expr)
2668 gfc_symbol *sym;
2669 match m;
2671 sym = expr->symtree->n.sym;
2673 for (;;)
2675 m = resolve_specific_f0 (sym, expr);
2676 if (m == MATCH_YES)
2677 return true;
2678 if (m == MATCH_ERROR)
2679 return false;
2681 if (sym->ns->parent == NULL)
2682 break;
2684 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2686 if (sym == NULL)
2687 break;
2690 gfc_error ("Unable to resolve the specific function %qs at %L",
2691 expr->symtree->n.sym->name, &expr->where);
2693 return true;
2697 /* Resolve a procedure call not known to be generic nor specific. */
2699 static bool
2700 resolve_unknown_f (gfc_expr *expr)
2702 gfc_symbol *sym;
2703 gfc_typespec *ts;
2705 sym = expr->symtree->n.sym;
2707 if (sym->attr.dummy)
2709 sym->attr.proc = PROC_DUMMY;
2710 expr->value.function.name = sym->name;
2711 goto set_type;
2714 /* See if we have an intrinsic function reference. */
2716 if (gfc_is_intrinsic (sym, 0, expr->where))
2718 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2719 return true;
2720 return false;
2723 /* The reference is to an external name. */
2725 sym->attr.proc = PROC_EXTERNAL;
2726 expr->value.function.name = sym->name;
2727 expr->value.function.esym = expr->symtree->n.sym;
2729 if (sym->as != NULL)
2730 expr->rank = sym->as->rank;
2732 /* Type of the expression is either the type of the symbol or the
2733 default type of the symbol. */
2735 set_type:
2736 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2738 if (sym->ts.type != BT_UNKNOWN)
2739 expr->ts = sym->ts;
2740 else
2742 ts = gfc_get_default_type (sym->name, sym->ns);
2744 if (ts->type == BT_UNKNOWN)
2746 gfc_error ("Function %qs at %L has no IMPLICIT type",
2747 sym->name, &expr->where);
2748 return false;
2750 else
2751 expr->ts = *ts;
2754 return true;
2758 /* Return true, if the symbol is an external procedure. */
2759 static bool
2760 is_external_proc (gfc_symbol *sym)
2762 if (!sym->attr.dummy && !sym->attr.contained
2763 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2764 && sym->attr.proc != PROC_ST_FUNCTION
2765 && !sym->attr.proc_pointer
2766 && !sym->attr.use_assoc
2767 && sym->name)
2768 return true;
2770 return false;
2774 /* Figure out if a function reference is pure or not. Also set the name
2775 of the function for a potential error message. Return nonzero if the
2776 function is PURE, zero if not. */
2777 static int
2778 pure_stmt_function (gfc_expr *, gfc_symbol *);
2780 static int
2781 pure_function (gfc_expr *e, const char **name)
2783 int pure;
2784 gfc_component *comp;
2786 *name = NULL;
2788 if (e->symtree != NULL
2789 && e->symtree->n.sym != NULL
2790 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2791 return pure_stmt_function (e, e->symtree->n.sym);
2793 comp = gfc_get_proc_ptr_comp (e);
2794 if (comp)
2796 pure = gfc_pure (comp->ts.interface);
2797 *name = comp->name;
2799 else if (e->value.function.esym)
2801 pure = gfc_pure (e->value.function.esym);
2802 *name = e->value.function.esym->name;
2804 else if (e->value.function.isym)
2806 pure = e->value.function.isym->pure
2807 || e->value.function.isym->elemental;
2808 *name = e->value.function.isym->name;
2810 else
2812 /* Implicit functions are not pure. */
2813 pure = 0;
2814 *name = e->value.function.name;
2817 return pure;
2821 static bool
2822 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2823 int *f ATTRIBUTE_UNUSED)
2825 const char *name;
2827 /* Don't bother recursing into other statement functions
2828 since they will be checked individually for purity. */
2829 if (e->expr_type != EXPR_FUNCTION
2830 || !e->symtree
2831 || e->symtree->n.sym == sym
2832 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2833 return false;
2835 return pure_function (e, &name) ? false : true;
2839 static int
2840 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2842 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2846 /* Check if an impure function is allowed in the current context. */
2848 static bool check_pure_function (gfc_expr *e)
2850 const char *name = NULL;
2851 if (!pure_function (e, &name) && name)
2853 if (forall_flag)
2855 gfc_error ("Reference to impure function %qs at %L inside a "
2856 "FORALL %s", name, &e->where,
2857 forall_flag == 2 ? "mask" : "block");
2858 return false;
2860 else if (gfc_do_concurrent_flag)
2862 gfc_error ("Reference to impure function %qs at %L inside a "
2863 "DO CONCURRENT %s", name, &e->where,
2864 gfc_do_concurrent_flag == 2 ? "mask" : "block");
2865 return false;
2867 else if (gfc_pure (NULL))
2869 gfc_error ("Reference to impure function %qs at %L "
2870 "within a PURE procedure", name, &e->where);
2871 return false;
2873 gfc_unset_implicit_pure (NULL);
2875 return true;
2879 /* Update current procedure's array_outer_dependency flag, considering
2880 a call to procedure SYM. */
2882 static void
2883 update_current_proc_array_outer_dependency (gfc_symbol *sym)
2885 /* Check to see if this is a sibling function that has not yet
2886 been resolved. */
2887 gfc_namespace *sibling = gfc_current_ns->sibling;
2888 for (; sibling; sibling = sibling->sibling)
2890 if (sibling->proc_name == sym)
2892 gfc_resolve (sibling);
2893 break;
2897 /* If SYM has references to outer arrays, so has the procedure calling
2898 SYM. If SYM is a procedure pointer, we can assume the worst. */
2899 if (sym->attr.array_outer_dependency
2900 || sym->attr.proc_pointer)
2901 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
2905 /* Resolve a function call, which means resolving the arguments, then figuring
2906 out which entity the name refers to. */
2908 static bool
2909 resolve_function (gfc_expr *expr)
2911 gfc_actual_arglist *arg;
2912 gfc_symbol *sym;
2913 bool t;
2914 int temp;
2915 procedure_type p = PROC_INTRINSIC;
2916 bool no_formal_args;
2918 sym = NULL;
2919 if (expr->symtree)
2920 sym = expr->symtree->n.sym;
2922 /* If this is a procedure pointer component, it has already been resolved. */
2923 if (gfc_is_proc_ptr_comp (expr))
2924 return true;
2926 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
2927 another caf_get. */
2928 if (sym && sym->attr.intrinsic
2929 && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
2930 || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
2931 return true;
2933 if (sym && sym->attr.intrinsic
2934 && !gfc_resolve_intrinsic (sym, &expr->where))
2935 return false;
2937 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2939 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
2940 return false;
2943 /* If this ia a deferred TBP with an abstract interface (which may
2944 of course be referenced), expr->value.function.esym will be set. */
2945 if (sym && sym->attr.abstract && !expr->value.function.esym)
2947 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
2948 sym->name, &expr->where);
2949 return false;
2952 /* Switch off assumed size checking and do this again for certain kinds
2953 of procedure, once the procedure itself is resolved. */
2954 need_full_assumed_size++;
2956 if (expr->symtree && expr->symtree->n.sym)
2957 p = expr->symtree->n.sym->attr.proc;
2959 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2960 inquiry_argument = true;
2961 no_formal_args = sym && is_external_proc (sym)
2962 && gfc_sym_get_dummy_args (sym) == NULL;
2964 if (!resolve_actual_arglist (expr->value.function.actual,
2965 p, no_formal_args))
2967 inquiry_argument = false;
2968 return false;
2971 inquiry_argument = false;
2973 /* Resume assumed_size checking. */
2974 need_full_assumed_size--;
2976 /* If the procedure is external, check for usage. */
2977 if (sym && is_external_proc (sym))
2978 resolve_global_procedure (sym, &expr->where,
2979 &expr->value.function.actual, 0);
2981 if (sym && sym->ts.type == BT_CHARACTER
2982 && sym->ts.u.cl
2983 && sym->ts.u.cl->length == NULL
2984 && !sym->attr.dummy
2985 && !sym->ts.deferred
2986 && expr->value.function.esym == NULL
2987 && !sym->attr.contained)
2989 /* Internal procedures are taken care of in resolve_contained_fntype. */
2990 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
2991 "be used at %L since it is not a dummy argument",
2992 sym->name, &expr->where);
2993 return false;
2996 /* See if function is already resolved. */
2998 if (expr->value.function.name != NULL
2999 || expr->value.function.isym != NULL)
3001 if (expr->ts.type == BT_UNKNOWN)
3002 expr->ts = sym->ts;
3003 t = true;
3005 else
3007 /* Apply the rules of section 14.1.2. */
3009 switch (procedure_kind (sym))
3011 case PTYPE_GENERIC:
3012 t = resolve_generic_f (expr);
3013 break;
3015 case PTYPE_SPECIFIC:
3016 t = resolve_specific_f (expr);
3017 break;
3019 case PTYPE_UNKNOWN:
3020 t = resolve_unknown_f (expr);
3021 break;
3023 default:
3024 gfc_internal_error ("resolve_function(): bad function type");
3028 /* If the expression is still a function (it might have simplified),
3029 then we check to see if we are calling an elemental function. */
3031 if (expr->expr_type != EXPR_FUNCTION)
3032 return t;
3034 temp = need_full_assumed_size;
3035 need_full_assumed_size = 0;
3037 if (!resolve_elemental_actual (expr, NULL))
3038 return false;
3040 if (omp_workshare_flag
3041 && expr->value.function.esym
3042 && ! gfc_elemental (expr->value.function.esym))
3044 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3045 "in WORKSHARE construct", expr->value.function.esym->name,
3046 &expr->where);
3047 t = false;
3050 #define GENERIC_ID expr->value.function.isym->id
3051 else if (expr->value.function.actual != NULL
3052 && expr->value.function.isym != NULL
3053 && GENERIC_ID != GFC_ISYM_LBOUND
3054 && GENERIC_ID != GFC_ISYM_LCOBOUND
3055 && GENERIC_ID != GFC_ISYM_UCOBOUND
3056 && GENERIC_ID != GFC_ISYM_LEN
3057 && GENERIC_ID != GFC_ISYM_LOC
3058 && GENERIC_ID != GFC_ISYM_C_LOC
3059 && GENERIC_ID != GFC_ISYM_PRESENT)
3061 /* Array intrinsics must also have the last upper bound of an
3062 assumed size array argument. UBOUND and SIZE have to be
3063 excluded from the check if the second argument is anything
3064 than a constant. */
3066 for (arg = expr->value.function.actual; arg; arg = arg->next)
3068 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3069 && arg == expr->value.function.actual
3070 && arg->next != NULL && arg->next->expr)
3072 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3073 break;
3075 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
3076 break;
3078 if ((int)mpz_get_si (arg->next->expr->value.integer)
3079 < arg->expr->rank)
3080 break;
3083 if (arg->expr != NULL
3084 && arg->expr->rank > 0
3085 && resolve_assumed_size_actual (arg->expr))
3086 return false;
3089 #undef GENERIC_ID
3091 need_full_assumed_size = temp;
3093 if (!check_pure_function(expr))
3094 t = false;
3096 /* Functions without the RECURSIVE attribution are not allowed to
3097 * call themselves. */
3098 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3100 gfc_symbol *esym;
3101 esym = expr->value.function.esym;
3103 if (is_illegal_recursion (esym, gfc_current_ns))
3105 if (esym->attr.entry && esym->ns->entries)
3106 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3107 " function %qs is not RECURSIVE",
3108 esym->name, &expr->where, esym->ns->entries->sym->name);
3109 else
3110 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3111 " is not RECURSIVE", esym->name, &expr->where);
3113 t = false;
3117 /* Character lengths of use associated functions may contains references to
3118 symbols not referenced from the current program unit otherwise. Make sure
3119 those symbols are marked as referenced. */
3121 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3122 && expr->value.function.esym->attr.use_assoc)
3124 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3127 /* Make sure that the expression has a typespec that works. */
3128 if (expr->ts.type == BT_UNKNOWN)
3130 if (expr->symtree->n.sym->result
3131 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3132 && !expr->symtree->n.sym->result->attr.proc_pointer)
3133 expr->ts = expr->symtree->n.sym->result->ts;
3136 if (!expr->ref && !expr->value.function.isym)
3138 if (expr->value.function.esym)
3139 update_current_proc_array_outer_dependency (expr->value.function.esym);
3140 else
3141 update_current_proc_array_outer_dependency (sym);
3143 else if (expr->ref)
3144 /* typebound procedure: Assume the worst. */
3145 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3147 return t;
3151 /************* Subroutine resolution *************/
3153 static bool
3154 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3156 if (gfc_pure (sym))
3157 return true;
3159 if (forall_flag)
3161 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3162 name, loc);
3163 return false;
3165 else if (gfc_do_concurrent_flag)
3167 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3168 "PURE", name, loc);
3169 return false;
3171 else if (gfc_pure (NULL))
3173 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3174 return false;
3177 gfc_unset_implicit_pure (NULL);
3178 return true;
3182 static match
3183 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3185 gfc_symbol *s;
3187 if (sym->attr.generic)
3189 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3190 if (s != NULL)
3192 c->resolved_sym = s;
3193 if (!pure_subroutine (s, s->name, &c->loc))
3194 return MATCH_ERROR;
3195 return MATCH_YES;
3198 /* TODO: Need to search for elemental references in generic interface. */
3201 if (sym->attr.intrinsic)
3202 return gfc_intrinsic_sub_interface (c, 0);
3204 return MATCH_NO;
3208 static bool
3209 resolve_generic_s (gfc_code *c)
3211 gfc_symbol *sym;
3212 match m;
3214 sym = c->symtree->n.sym;
3216 for (;;)
3218 m = resolve_generic_s0 (c, sym);
3219 if (m == MATCH_YES)
3220 return true;
3221 else if (m == MATCH_ERROR)
3222 return false;
3224 generic:
3225 if (sym->ns->parent == NULL)
3226 break;
3227 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3229 if (sym == NULL)
3230 break;
3231 if (!generic_sym (sym))
3232 goto generic;
3235 /* Last ditch attempt. See if the reference is to an intrinsic
3236 that possesses a matching interface. 14.1.2.4 */
3237 sym = c->symtree->n.sym;
3239 if (!gfc_is_intrinsic (sym, 1, c->loc))
3241 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3242 sym->name, &c->loc);
3243 return false;
3246 m = gfc_intrinsic_sub_interface (c, 0);
3247 if (m == MATCH_YES)
3248 return true;
3249 if (m == MATCH_NO)
3250 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3251 "intrinsic subroutine interface", sym->name, &c->loc);
3253 return false;
3257 /* Resolve a subroutine call known to be specific. */
3259 static match
3260 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3262 match m;
3264 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3266 if (sym->attr.dummy)
3268 sym->attr.proc = PROC_DUMMY;
3269 goto found;
3272 sym->attr.proc = PROC_EXTERNAL;
3273 goto found;
3276 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3277 goto found;
3279 if (sym->attr.intrinsic)
3281 m = gfc_intrinsic_sub_interface (c, 1);
3282 if (m == MATCH_YES)
3283 return MATCH_YES;
3284 if (m == MATCH_NO)
3285 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3286 "with an intrinsic", sym->name, &c->loc);
3288 return MATCH_ERROR;
3291 return MATCH_NO;
3293 found:
3294 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3296 c->resolved_sym = sym;
3297 if (!pure_subroutine (sym, sym->name, &c->loc))
3298 return MATCH_ERROR;
3300 return MATCH_YES;
3304 static bool
3305 resolve_specific_s (gfc_code *c)
3307 gfc_symbol *sym;
3308 match m;
3310 sym = c->symtree->n.sym;
3312 for (;;)
3314 m = resolve_specific_s0 (c, sym);
3315 if (m == MATCH_YES)
3316 return true;
3317 if (m == MATCH_ERROR)
3318 return false;
3320 if (sym->ns->parent == NULL)
3321 break;
3323 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3325 if (sym == NULL)
3326 break;
3329 sym = c->symtree->n.sym;
3330 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3331 sym->name, &c->loc);
3333 return false;
3337 /* Resolve a subroutine call not known to be generic nor specific. */
3339 static bool
3340 resolve_unknown_s (gfc_code *c)
3342 gfc_symbol *sym;
3344 sym = c->symtree->n.sym;
3346 if (sym->attr.dummy)
3348 sym->attr.proc = PROC_DUMMY;
3349 goto found;
3352 /* See if we have an intrinsic function reference. */
3354 if (gfc_is_intrinsic (sym, 1, c->loc))
3356 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3357 return true;
3358 return false;
3361 /* The reference is to an external name. */
3363 found:
3364 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3366 c->resolved_sym = sym;
3368 return pure_subroutine (sym, sym->name, &c->loc);
3372 /* Resolve a subroutine call. Although it was tempting to use the same code
3373 for functions, subroutines and functions are stored differently and this
3374 makes things awkward. */
3376 static bool
3377 resolve_call (gfc_code *c)
3379 bool t;
3380 procedure_type ptype = PROC_INTRINSIC;
3381 gfc_symbol *csym, *sym;
3382 bool no_formal_args;
3384 csym = c->symtree ? c->symtree->n.sym : NULL;
3386 if (csym && csym->ts.type != BT_UNKNOWN)
3388 gfc_error ("%qs at %L has a type, which is not consistent with "
3389 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3390 return false;
3393 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3395 gfc_symtree *st;
3396 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3397 sym = st ? st->n.sym : NULL;
3398 if (sym && csym != sym
3399 && sym->ns == gfc_current_ns
3400 && sym->attr.flavor == FL_PROCEDURE
3401 && sym->attr.contained)
3403 sym->refs++;
3404 if (csym->attr.generic)
3405 c->symtree->n.sym = sym;
3406 else
3407 c->symtree = st;
3408 csym = c->symtree->n.sym;
3412 /* If this ia a deferred TBP, c->expr1 will be set. */
3413 if (!c->expr1 && csym)
3415 if (csym->attr.abstract)
3417 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3418 csym->name, &c->loc);
3419 return false;
3422 /* Subroutines without the RECURSIVE attribution are not allowed to
3423 call themselves. */
3424 if (is_illegal_recursion (csym, gfc_current_ns))
3426 if (csym->attr.entry && csym->ns->entries)
3427 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3428 "as subroutine %qs is not RECURSIVE",
3429 csym->name, &c->loc, csym->ns->entries->sym->name);
3430 else
3431 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3432 "as it is not RECURSIVE", csym->name, &c->loc);
3434 t = false;
3438 /* Switch off assumed size checking and do this again for certain kinds
3439 of procedure, once the procedure itself is resolved. */
3440 need_full_assumed_size++;
3442 if (csym)
3443 ptype = csym->attr.proc;
3445 no_formal_args = csym && is_external_proc (csym)
3446 && gfc_sym_get_dummy_args (csym) == NULL;
3447 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3448 return false;
3450 /* Resume assumed_size checking. */
3451 need_full_assumed_size--;
3453 /* If external, check for usage. */
3454 if (csym && is_external_proc (csym))
3455 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3457 t = true;
3458 if (c->resolved_sym == NULL)
3460 c->resolved_isym = NULL;
3461 switch (procedure_kind (csym))
3463 case PTYPE_GENERIC:
3464 t = resolve_generic_s (c);
3465 break;
3467 case PTYPE_SPECIFIC:
3468 t = resolve_specific_s (c);
3469 break;
3471 case PTYPE_UNKNOWN:
3472 t = resolve_unknown_s (c);
3473 break;
3475 default:
3476 gfc_internal_error ("resolve_subroutine(): bad function type");
3480 /* Some checks of elemental subroutine actual arguments. */
3481 if (!resolve_elemental_actual (NULL, c))
3482 return false;
3484 if (!c->expr1)
3485 update_current_proc_array_outer_dependency (csym);
3486 else
3487 /* Typebound procedure: Assume the worst. */
3488 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3490 return t;
3494 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3495 op1->shape and op2->shape are non-NULL return true if their shapes
3496 match. If both op1->shape and op2->shape are non-NULL return false
3497 if their shapes do not match. If either op1->shape or op2->shape is
3498 NULL, return true. */
3500 static bool
3501 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3503 bool t;
3504 int i;
3506 t = true;
3508 if (op1->shape != NULL && op2->shape != NULL)
3510 for (i = 0; i < op1->rank; i++)
3512 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3514 gfc_error ("Shapes for operands at %L and %L are not conformable",
3515 &op1->where, &op2->where);
3516 t = false;
3517 break;
3522 return t;
3526 /* Resolve an operator expression node. This can involve replacing the
3527 operation with a user defined function call. */
3529 static bool
3530 resolve_operator (gfc_expr *e)
3532 gfc_expr *op1, *op2;
3533 char msg[200];
3534 bool dual_locus_error;
3535 bool t;
3537 /* Resolve all subnodes-- give them types. */
3539 switch (e->value.op.op)
3541 default:
3542 if (!gfc_resolve_expr (e->value.op.op2))
3543 return false;
3545 /* Fall through... */
3547 case INTRINSIC_NOT:
3548 case INTRINSIC_UPLUS:
3549 case INTRINSIC_UMINUS:
3550 case INTRINSIC_PARENTHESES:
3551 if (!gfc_resolve_expr (e->value.op.op1))
3552 return false;
3553 break;
3556 /* Typecheck the new node. */
3558 op1 = e->value.op.op1;
3559 op2 = e->value.op.op2;
3560 dual_locus_error = false;
3562 if ((op1 && op1->expr_type == EXPR_NULL)
3563 || (op2 && op2->expr_type == EXPR_NULL))
3565 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3566 goto bad_op;
3569 switch (e->value.op.op)
3571 case INTRINSIC_UPLUS:
3572 case INTRINSIC_UMINUS:
3573 if (op1->ts.type == BT_INTEGER
3574 || op1->ts.type == BT_REAL
3575 || op1->ts.type == BT_COMPLEX)
3577 e->ts = op1->ts;
3578 break;
3581 sprintf (msg, _("Operand of unary numeric operator %%<%s%%> at %%L is %s"),
3582 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3583 goto bad_op;
3585 case INTRINSIC_PLUS:
3586 case INTRINSIC_MINUS:
3587 case INTRINSIC_TIMES:
3588 case INTRINSIC_DIVIDE:
3589 case INTRINSIC_POWER:
3590 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3592 gfc_type_convert_binary (e, 1);
3593 break;
3596 sprintf (msg,
3597 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"),
3598 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3599 gfc_typename (&op2->ts));
3600 goto bad_op;
3602 case INTRINSIC_CONCAT:
3603 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3604 && op1->ts.kind == op2->ts.kind)
3606 e->ts.type = BT_CHARACTER;
3607 e->ts.kind = op1->ts.kind;
3608 break;
3611 sprintf (msg,
3612 _("Operands of string concatenation operator at %%L are %s/%s"),
3613 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3614 goto bad_op;
3616 case INTRINSIC_AND:
3617 case INTRINSIC_OR:
3618 case INTRINSIC_EQV:
3619 case INTRINSIC_NEQV:
3620 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3622 e->ts.type = BT_LOGICAL;
3623 e->ts.kind = gfc_kind_max (op1, op2);
3624 if (op1->ts.kind < e->ts.kind)
3625 gfc_convert_type (op1, &e->ts, 2);
3626 else if (op2->ts.kind < e->ts.kind)
3627 gfc_convert_type (op2, &e->ts, 2);
3628 break;
3631 sprintf (msg, _("Operands of logical operator %%<%s%%> at %%L are %s/%s"),
3632 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3633 gfc_typename (&op2->ts));
3635 goto bad_op;
3637 case INTRINSIC_NOT:
3638 if (op1->ts.type == BT_LOGICAL)
3640 e->ts.type = BT_LOGICAL;
3641 e->ts.kind = op1->ts.kind;
3642 break;
3645 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3646 gfc_typename (&op1->ts));
3647 goto bad_op;
3649 case INTRINSIC_GT:
3650 case INTRINSIC_GT_OS:
3651 case INTRINSIC_GE:
3652 case INTRINSIC_GE_OS:
3653 case INTRINSIC_LT:
3654 case INTRINSIC_LT_OS:
3655 case INTRINSIC_LE:
3656 case INTRINSIC_LE_OS:
3657 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3659 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3660 goto bad_op;
3663 /* Fall through... */
3665 case INTRINSIC_EQ:
3666 case INTRINSIC_EQ_OS:
3667 case INTRINSIC_NE:
3668 case INTRINSIC_NE_OS:
3669 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3670 && op1->ts.kind == op2->ts.kind)
3672 e->ts.type = BT_LOGICAL;
3673 e->ts.kind = gfc_default_logical_kind;
3674 break;
3677 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3679 gfc_type_convert_binary (e, 1);
3681 e->ts.type = BT_LOGICAL;
3682 e->ts.kind = gfc_default_logical_kind;
3684 if (warn_compare_reals)
3686 gfc_intrinsic_op op = e->value.op.op;
3688 /* Type conversion has made sure that the types of op1 and op2
3689 agree, so it is only necessary to check the first one. */
3690 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3691 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3692 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3694 const char *msg;
3696 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3697 msg = "Equality comparison for %s at %L";
3698 else
3699 msg = "Inequality comparison for %s at %L";
3701 gfc_warning (0, msg, gfc_typename (&op1->ts), &op1->where);
3705 break;
3708 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3709 sprintf (msg,
3710 _("Logicals at %%L must be compared with %s instead of %s"),
3711 (e->value.op.op == INTRINSIC_EQ
3712 || e->value.op.op == INTRINSIC_EQ_OS)
3713 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3714 else
3715 sprintf (msg,
3716 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"),
3717 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3718 gfc_typename (&op2->ts));
3720 goto bad_op;
3722 case INTRINSIC_USER:
3723 if (e->value.op.uop->op == NULL)
3724 sprintf (msg, _("Unknown operator %%<%s%%> at %%L"),
3725 e->value.op.uop->name);
3726 else if (op2 == NULL)
3727 sprintf (msg, _("Operand of user operator %%<%s%%> at %%L is %s"),
3728 e->value.op.uop->name, gfc_typename (&op1->ts));
3729 else
3731 sprintf (msg, _("Operands of user operator %%<%s%%> at %%L are %s/%s"),
3732 e->value.op.uop->name, gfc_typename (&op1->ts),
3733 gfc_typename (&op2->ts));
3734 e->value.op.uop->op->sym->attr.referenced = 1;
3737 goto bad_op;
3739 case INTRINSIC_PARENTHESES:
3740 e->ts = op1->ts;
3741 if (e->ts.type == BT_CHARACTER)
3742 e->ts.u.cl = op1->ts.u.cl;
3743 break;
3745 default:
3746 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3749 /* Deal with arrayness of an operand through an operator. */
3751 t = true;
3753 switch (e->value.op.op)
3755 case INTRINSIC_PLUS:
3756 case INTRINSIC_MINUS:
3757 case INTRINSIC_TIMES:
3758 case INTRINSIC_DIVIDE:
3759 case INTRINSIC_POWER:
3760 case INTRINSIC_CONCAT:
3761 case INTRINSIC_AND:
3762 case INTRINSIC_OR:
3763 case INTRINSIC_EQV:
3764 case INTRINSIC_NEQV:
3765 case INTRINSIC_EQ:
3766 case INTRINSIC_EQ_OS:
3767 case INTRINSIC_NE:
3768 case INTRINSIC_NE_OS:
3769 case INTRINSIC_GT:
3770 case INTRINSIC_GT_OS:
3771 case INTRINSIC_GE:
3772 case INTRINSIC_GE_OS:
3773 case INTRINSIC_LT:
3774 case INTRINSIC_LT_OS:
3775 case INTRINSIC_LE:
3776 case INTRINSIC_LE_OS:
3778 if (op1->rank == 0 && op2->rank == 0)
3779 e->rank = 0;
3781 if (op1->rank == 0 && op2->rank != 0)
3783 e->rank = op2->rank;
3785 if (e->shape == NULL)
3786 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3789 if (op1->rank != 0 && op2->rank == 0)
3791 e->rank = op1->rank;
3793 if (e->shape == NULL)
3794 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3797 if (op1->rank != 0 && op2->rank != 0)
3799 if (op1->rank == op2->rank)
3801 e->rank = op1->rank;
3802 if (e->shape == NULL)
3804 t = compare_shapes (op1, op2);
3805 if (!t)
3806 e->shape = NULL;
3807 else
3808 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3811 else
3813 /* Allow higher level expressions to work. */
3814 e->rank = 0;
3816 /* Try user-defined operators, and otherwise throw an error. */
3817 dual_locus_error = true;
3818 sprintf (msg,
3819 _("Inconsistent ranks for operator at %%L and %%L"));
3820 goto bad_op;
3824 break;
3826 case INTRINSIC_PARENTHESES:
3827 case INTRINSIC_NOT:
3828 case INTRINSIC_UPLUS:
3829 case INTRINSIC_UMINUS:
3830 /* Simply copy arrayness attribute */
3831 e->rank = op1->rank;
3833 if (e->shape == NULL)
3834 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3836 break;
3838 default:
3839 break;
3842 /* Attempt to simplify the expression. */
3843 if (t)
3845 t = gfc_simplify_expr (e, 0);
3846 /* Some calls do not succeed in simplification and return false
3847 even though there is no error; e.g. variable references to
3848 PARAMETER arrays. */
3849 if (!gfc_is_constant_expr (e))
3850 t = true;
3852 return t;
3854 bad_op:
3857 match m = gfc_extend_expr (e);
3858 if (m == MATCH_YES)
3859 return true;
3860 if (m == MATCH_ERROR)
3861 return false;
3864 if (dual_locus_error)
3865 gfc_error (msg, &op1->where, &op2->where);
3866 else
3867 gfc_error (msg, &e->where);
3869 return false;
3873 /************** Array resolution subroutines **************/
3875 enum compare_result
3876 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
3878 /* Compare two integer expressions. */
3880 static compare_result
3881 compare_bound (gfc_expr *a, gfc_expr *b)
3883 int i;
3885 if (a == NULL || a->expr_type != EXPR_CONSTANT
3886 || b == NULL || b->expr_type != EXPR_CONSTANT)
3887 return CMP_UNKNOWN;
3889 /* If either of the types isn't INTEGER, we must have
3890 raised an error earlier. */
3892 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3893 return CMP_UNKNOWN;
3895 i = mpz_cmp (a->value.integer, b->value.integer);
3897 if (i < 0)
3898 return CMP_LT;
3899 if (i > 0)
3900 return CMP_GT;
3901 return CMP_EQ;
3905 /* Compare an integer expression with an integer. */
3907 static compare_result
3908 compare_bound_int (gfc_expr *a, int b)
3910 int i;
3912 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3913 return CMP_UNKNOWN;
3915 if (a->ts.type != BT_INTEGER)
3916 gfc_internal_error ("compare_bound_int(): Bad expression");
3918 i = mpz_cmp_si (a->value.integer, b);
3920 if (i < 0)
3921 return CMP_LT;
3922 if (i > 0)
3923 return CMP_GT;
3924 return CMP_EQ;
3928 /* Compare an integer expression with a mpz_t. */
3930 static compare_result
3931 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3933 int i;
3935 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3936 return CMP_UNKNOWN;
3938 if (a->ts.type != BT_INTEGER)
3939 gfc_internal_error ("compare_bound_int(): Bad expression");
3941 i = mpz_cmp (a->value.integer, b);
3943 if (i < 0)
3944 return CMP_LT;
3945 if (i > 0)
3946 return CMP_GT;
3947 return CMP_EQ;
3951 /* Compute the last value of a sequence given by a triplet.
3952 Return 0 if it wasn't able to compute the last value, or if the
3953 sequence if empty, and 1 otherwise. */
3955 static int
3956 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3957 gfc_expr *stride, mpz_t last)
3959 mpz_t rem;
3961 if (start == NULL || start->expr_type != EXPR_CONSTANT
3962 || end == NULL || end->expr_type != EXPR_CONSTANT
3963 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3964 return 0;
3966 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3967 || (stride != NULL && stride->ts.type != BT_INTEGER))
3968 return 0;
3970 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
3972 if (compare_bound (start, end) == CMP_GT)
3973 return 0;
3974 mpz_set (last, end->value.integer);
3975 return 1;
3978 if (compare_bound_int (stride, 0) == CMP_GT)
3980 /* Stride is positive */
3981 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3982 return 0;
3984 else
3986 /* Stride is negative */
3987 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3988 return 0;
3991 mpz_init (rem);
3992 mpz_sub (rem, end->value.integer, start->value.integer);
3993 mpz_tdiv_r (rem, rem, stride->value.integer);
3994 mpz_sub (last, end->value.integer, rem);
3995 mpz_clear (rem);
3997 return 1;
4001 /* Compare a single dimension of an array reference to the array
4002 specification. */
4004 static bool
4005 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4007 mpz_t last_value;
4009 if (ar->dimen_type[i] == DIMEN_STAR)
4011 gcc_assert (ar->stride[i] == NULL);
4012 /* This implies [*] as [*:] and [*:3] are not possible. */
4013 if (ar->start[i] == NULL)
4015 gcc_assert (ar->end[i] == NULL);
4016 return true;
4020 /* Given start, end and stride values, calculate the minimum and
4021 maximum referenced indexes. */
4023 switch (ar->dimen_type[i])
4025 case DIMEN_VECTOR:
4026 case DIMEN_THIS_IMAGE:
4027 break;
4029 case DIMEN_STAR:
4030 case DIMEN_ELEMENT:
4031 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4033 if (i < as->rank)
4034 gfc_warning (0, "Array reference at %L is out of bounds "
4035 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4036 mpz_get_si (ar->start[i]->value.integer),
4037 mpz_get_si (as->lower[i]->value.integer), i+1);
4038 else
4039 gfc_warning (0, "Array reference at %L is out of bounds "
4040 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4041 mpz_get_si (ar->start[i]->value.integer),
4042 mpz_get_si (as->lower[i]->value.integer),
4043 i + 1 - as->rank);
4044 return true;
4046 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4048 if (i < as->rank)
4049 gfc_warning (0, "Array reference at %L is out of bounds "
4050 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4051 mpz_get_si (ar->start[i]->value.integer),
4052 mpz_get_si (as->upper[i]->value.integer), i+1);
4053 else
4054 gfc_warning (0, "Array reference at %L is out of bounds "
4055 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4056 mpz_get_si (ar->start[i]->value.integer),
4057 mpz_get_si (as->upper[i]->value.integer),
4058 i + 1 - as->rank);
4059 return true;
4062 break;
4064 case DIMEN_RANGE:
4066 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4067 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4069 compare_result comp_start_end = compare_bound (AR_START, AR_END);
4071 /* Check for zero stride, which is not allowed. */
4072 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4074 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4075 return false;
4078 /* if start == len || (stride > 0 && start < len)
4079 || (stride < 0 && start > len),
4080 then the array section contains at least one element. In this
4081 case, there is an out-of-bounds access if
4082 (start < lower || start > upper). */
4083 if (compare_bound (AR_START, AR_END) == CMP_EQ
4084 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4085 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4086 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4087 && comp_start_end == CMP_GT))
4089 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4091 gfc_warning (0, "Lower array reference at %L is out of bounds "
4092 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4093 mpz_get_si (AR_START->value.integer),
4094 mpz_get_si (as->lower[i]->value.integer), i+1);
4095 return true;
4097 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4099 gfc_warning (0, "Lower array reference at %L is out of bounds "
4100 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4101 mpz_get_si (AR_START->value.integer),
4102 mpz_get_si (as->upper[i]->value.integer), i+1);
4103 return true;
4107 /* If we can compute the highest index of the array section,
4108 then it also has to be between lower and upper. */
4109 mpz_init (last_value);
4110 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4111 last_value))
4113 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4115 gfc_warning (0, "Upper array reference at %L is out of bounds "
4116 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4117 mpz_get_si (last_value),
4118 mpz_get_si (as->lower[i]->value.integer), i+1);
4119 mpz_clear (last_value);
4120 return true;
4122 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4124 gfc_warning (0, "Upper array reference at %L is out of bounds "
4125 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4126 mpz_get_si (last_value),
4127 mpz_get_si (as->upper[i]->value.integer), i+1);
4128 mpz_clear (last_value);
4129 return true;
4132 mpz_clear (last_value);
4134 #undef AR_START
4135 #undef AR_END
4137 break;
4139 default:
4140 gfc_internal_error ("check_dimension(): Bad array reference");
4143 return true;
4147 /* Compare an array reference with an array specification. */
4149 static bool
4150 compare_spec_to_ref (gfc_array_ref *ar)
4152 gfc_array_spec *as;
4153 int i;
4155 as = ar->as;
4156 i = as->rank - 1;
4157 /* TODO: Full array sections are only allowed as actual parameters. */
4158 if (as->type == AS_ASSUMED_SIZE
4159 && (/*ar->type == AR_FULL
4160 ||*/ (ar->type == AR_SECTION
4161 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4163 gfc_error ("Rightmost upper bound of assumed size array section "
4164 "not specified at %L", &ar->where);
4165 return false;
4168 if (ar->type == AR_FULL)
4169 return true;
4171 if (as->rank != ar->dimen)
4173 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4174 &ar->where, ar->dimen, as->rank);
4175 return false;
4178 /* ar->codimen == 0 is a local array. */
4179 if (as->corank != ar->codimen && ar->codimen != 0)
4181 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4182 &ar->where, ar->codimen, as->corank);
4183 return false;
4186 for (i = 0; i < as->rank; i++)
4187 if (!check_dimension (i, ar, as))
4188 return false;
4190 /* Local access has no coarray spec. */
4191 if (ar->codimen != 0)
4192 for (i = as->rank; i < as->rank + as->corank; i++)
4194 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4195 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4197 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4198 i + 1 - as->rank, &ar->where);
4199 return false;
4201 if (!check_dimension (i, ar, as))
4202 return false;
4205 return true;
4209 /* Resolve one part of an array index. */
4211 static bool
4212 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4213 int force_index_integer_kind)
4215 gfc_typespec ts;
4217 if (index == NULL)
4218 return true;
4220 if (!gfc_resolve_expr (index))
4221 return false;
4223 if (check_scalar && index->rank != 0)
4225 gfc_error ("Array index at %L must be scalar", &index->where);
4226 return false;
4229 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4231 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4232 &index->where, gfc_basic_typename (index->ts.type));
4233 return false;
4236 if (index->ts.type == BT_REAL)
4237 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4238 &index->where))
4239 return false;
4241 if ((index->ts.kind != gfc_index_integer_kind
4242 && force_index_integer_kind)
4243 || index->ts.type != BT_INTEGER)
4245 gfc_clear_ts (&ts);
4246 ts.type = BT_INTEGER;
4247 ts.kind = gfc_index_integer_kind;
4249 gfc_convert_type_warn (index, &ts, 2, 0);
4252 return true;
4255 /* Resolve one part of an array index. */
4257 bool
4258 gfc_resolve_index (gfc_expr *index, int check_scalar)
4260 return gfc_resolve_index_1 (index, check_scalar, 1);
4263 /* Resolve a dim argument to an intrinsic function. */
4265 bool
4266 gfc_resolve_dim_arg (gfc_expr *dim)
4268 if (dim == NULL)
4269 return true;
4271 if (!gfc_resolve_expr (dim))
4272 return false;
4274 if (dim->rank != 0)
4276 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4277 return false;
4281 if (dim->ts.type != BT_INTEGER)
4283 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4284 return false;
4287 if (dim->ts.kind != gfc_index_integer_kind)
4289 gfc_typespec ts;
4291 gfc_clear_ts (&ts);
4292 ts.type = BT_INTEGER;
4293 ts.kind = gfc_index_integer_kind;
4295 gfc_convert_type_warn (dim, &ts, 2, 0);
4298 return true;
4301 /* Given an expression that contains array references, update those array
4302 references to point to the right array specifications. While this is
4303 filled in during matching, this information is difficult to save and load
4304 in a module, so we take care of it here.
4306 The idea here is that the original array reference comes from the
4307 base symbol. We traverse the list of reference structures, setting
4308 the stored reference to references. Component references can
4309 provide an additional array specification. */
4311 static void
4312 find_array_spec (gfc_expr *e)
4314 gfc_array_spec *as;
4315 gfc_component *c;
4316 gfc_ref *ref;
4318 if (e->symtree->n.sym->ts.type == BT_CLASS)
4319 as = CLASS_DATA (e->symtree->n.sym)->as;
4320 else
4321 as = e->symtree->n.sym->as;
4323 for (ref = e->ref; ref; ref = ref->next)
4324 switch (ref->type)
4326 case REF_ARRAY:
4327 if (as == NULL)
4328 gfc_internal_error ("find_array_spec(): Missing spec");
4330 ref->u.ar.as = as;
4331 as = NULL;
4332 break;
4334 case REF_COMPONENT:
4335 c = ref->u.c.component;
4336 if (c->attr.dimension)
4338 if (as != NULL)
4339 gfc_internal_error ("find_array_spec(): unused as(1)");
4340 as = c->as;
4343 break;
4345 case REF_SUBSTRING:
4346 break;
4349 if (as != NULL)
4350 gfc_internal_error ("find_array_spec(): unused as(2)");
4354 /* Resolve an array reference. */
4356 static bool
4357 resolve_array_ref (gfc_array_ref *ar)
4359 int i, check_scalar;
4360 gfc_expr *e;
4362 for (i = 0; i < ar->dimen + ar->codimen; i++)
4364 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4366 /* Do not force gfc_index_integer_kind for the start. We can
4367 do fine with any integer kind. This avoids temporary arrays
4368 created for indexing with a vector. */
4369 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4370 return false;
4371 if (!gfc_resolve_index (ar->end[i], check_scalar))
4372 return false;
4373 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4374 return false;
4376 e = ar->start[i];
4378 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4379 switch (e->rank)
4381 case 0:
4382 ar->dimen_type[i] = DIMEN_ELEMENT;
4383 break;
4385 case 1:
4386 ar->dimen_type[i] = DIMEN_VECTOR;
4387 if (e->expr_type == EXPR_VARIABLE
4388 && e->symtree->n.sym->ts.type == BT_DERIVED)
4389 ar->start[i] = gfc_get_parentheses (e);
4390 break;
4392 default:
4393 gfc_error ("Array index at %L is an array of rank %d",
4394 &ar->c_where[i], e->rank);
4395 return false;
4398 /* Fill in the upper bound, which may be lower than the
4399 specified one for something like a(2:10:5), which is
4400 identical to a(2:7:5). Only relevant for strides not equal
4401 to one. Don't try a division by zero. */
4402 if (ar->dimen_type[i] == DIMEN_RANGE
4403 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4404 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4405 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4407 mpz_t size, end;
4409 if (gfc_ref_dimen_size (ar, i, &size, &end))
4411 if (ar->end[i] == NULL)
4413 ar->end[i] =
4414 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4415 &ar->where);
4416 mpz_set (ar->end[i]->value.integer, end);
4418 else if (ar->end[i]->ts.type == BT_INTEGER
4419 && ar->end[i]->expr_type == EXPR_CONSTANT)
4421 mpz_set (ar->end[i]->value.integer, end);
4423 else
4424 gcc_unreachable ();
4426 mpz_clear (size);
4427 mpz_clear (end);
4432 if (ar->type == AR_FULL)
4434 if (ar->as->rank == 0)
4435 ar->type = AR_ELEMENT;
4437 /* Make sure array is the same as array(:,:), this way
4438 we don't need to special case all the time. */
4439 ar->dimen = ar->as->rank;
4440 for (i = 0; i < ar->dimen; i++)
4442 ar->dimen_type[i] = DIMEN_RANGE;
4444 gcc_assert (ar->start[i] == NULL);
4445 gcc_assert (ar->end[i] == NULL);
4446 gcc_assert (ar->stride[i] == NULL);
4450 /* If the reference type is unknown, figure out what kind it is. */
4452 if (ar->type == AR_UNKNOWN)
4454 ar->type = AR_ELEMENT;
4455 for (i = 0; i < ar->dimen; i++)
4456 if (ar->dimen_type[i] == DIMEN_RANGE
4457 || ar->dimen_type[i] == DIMEN_VECTOR)
4459 ar->type = AR_SECTION;
4460 break;
4464 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4465 return false;
4467 if (ar->as->corank && ar->codimen == 0)
4469 int n;
4470 ar->codimen = ar->as->corank;
4471 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4472 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4475 return true;
4479 static bool
4480 resolve_substring (gfc_ref *ref)
4482 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4484 if (ref->u.ss.start != NULL)
4486 if (!gfc_resolve_expr (ref->u.ss.start))
4487 return false;
4489 if (ref->u.ss.start->ts.type != BT_INTEGER)
4491 gfc_error ("Substring start index at %L must be of type INTEGER",
4492 &ref->u.ss.start->where);
4493 return false;
4496 if (ref->u.ss.start->rank != 0)
4498 gfc_error ("Substring start index at %L must be scalar",
4499 &ref->u.ss.start->where);
4500 return false;
4503 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4504 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4505 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4507 gfc_error ("Substring start index at %L is less than one",
4508 &ref->u.ss.start->where);
4509 return false;
4513 if (ref->u.ss.end != NULL)
4515 if (!gfc_resolve_expr (ref->u.ss.end))
4516 return false;
4518 if (ref->u.ss.end->ts.type != BT_INTEGER)
4520 gfc_error ("Substring end index at %L must be of type INTEGER",
4521 &ref->u.ss.end->where);
4522 return false;
4525 if (ref->u.ss.end->rank != 0)
4527 gfc_error ("Substring end index at %L must be scalar",
4528 &ref->u.ss.end->where);
4529 return false;
4532 if (ref->u.ss.length != NULL
4533 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4534 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4535 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4537 gfc_error ("Substring end index at %L exceeds the string length",
4538 &ref->u.ss.start->where);
4539 return false;
4542 if (compare_bound_mpz_t (ref->u.ss.end,
4543 gfc_integer_kinds[k].huge) == CMP_GT
4544 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4545 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4547 gfc_error ("Substring end index at %L is too large",
4548 &ref->u.ss.end->where);
4549 return false;
4553 return true;
4557 /* This function supplies missing substring charlens. */
4559 void
4560 gfc_resolve_substring_charlen (gfc_expr *e)
4562 gfc_ref *char_ref;
4563 gfc_expr *start, *end;
4564 gfc_typespec *ts = NULL;
4566 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4568 if (char_ref->type == REF_SUBSTRING)
4569 break;
4570 if (char_ref->type == REF_COMPONENT)
4571 ts = &char_ref->u.c.component->ts;
4574 if (!char_ref)
4575 return;
4577 gcc_assert (char_ref->next == NULL);
4579 if (e->ts.u.cl)
4581 if (e->ts.u.cl->length)
4582 gfc_free_expr (e->ts.u.cl->length);
4583 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
4584 return;
4587 e->ts.type = BT_CHARACTER;
4588 e->ts.kind = gfc_default_character_kind;
4590 if (!e->ts.u.cl)
4591 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4593 if (char_ref->u.ss.start)
4594 start = gfc_copy_expr (char_ref->u.ss.start);
4595 else
4596 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4598 if (char_ref->u.ss.end)
4599 end = gfc_copy_expr (char_ref->u.ss.end);
4600 else if (e->expr_type == EXPR_VARIABLE)
4602 if (!ts)
4603 ts = &e->symtree->n.sym->ts;
4604 end = gfc_copy_expr (ts->u.cl->length);
4606 else
4607 end = NULL;
4609 if (!start || !end)
4611 gfc_free_expr (start);
4612 gfc_free_expr (end);
4613 return;
4616 /* Length = (end - start + 1). */
4617 e->ts.u.cl->length = gfc_subtract (end, start);
4618 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4619 gfc_get_int_expr (gfc_default_integer_kind,
4620 NULL, 1));
4622 /* F2008, 6.4.1: Both the starting point and the ending point shall
4623 be within the range 1, 2, ..., n unless the starting point exceeds
4624 the ending point, in which case the substring has length zero. */
4626 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
4627 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
4629 e->ts.u.cl->length->ts.type = BT_INTEGER;
4630 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4632 /* Make sure that the length is simplified. */
4633 gfc_simplify_expr (e->ts.u.cl->length, 1);
4634 gfc_resolve_expr (e->ts.u.cl->length);
4638 /* Resolve subtype references. */
4640 static bool
4641 resolve_ref (gfc_expr *expr)
4643 int current_part_dimension, n_components, seen_part_dimension;
4644 gfc_ref *ref;
4646 for (ref = expr->ref; ref; ref = ref->next)
4647 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4649 find_array_spec (expr);
4650 break;
4653 for (ref = expr->ref; ref; ref = ref->next)
4654 switch (ref->type)
4656 case REF_ARRAY:
4657 if (!resolve_array_ref (&ref->u.ar))
4658 return false;
4659 break;
4661 case REF_COMPONENT:
4662 break;
4664 case REF_SUBSTRING:
4665 if (!resolve_substring (ref))
4666 return false;
4667 break;
4670 /* Check constraints on part references. */
4672 current_part_dimension = 0;
4673 seen_part_dimension = 0;
4674 n_components = 0;
4676 for (ref = expr->ref; ref; ref = ref->next)
4678 switch (ref->type)
4680 case REF_ARRAY:
4681 switch (ref->u.ar.type)
4683 case AR_FULL:
4684 /* Coarray scalar. */
4685 if (ref->u.ar.as->rank == 0)
4687 current_part_dimension = 0;
4688 break;
4690 /* Fall through. */
4691 case AR_SECTION:
4692 current_part_dimension = 1;
4693 break;
4695 case AR_ELEMENT:
4696 current_part_dimension = 0;
4697 break;
4699 case AR_UNKNOWN:
4700 gfc_internal_error ("resolve_ref(): Bad array reference");
4703 break;
4705 case REF_COMPONENT:
4706 if (current_part_dimension || seen_part_dimension)
4708 /* F03:C614. */
4709 if (ref->u.c.component->attr.pointer
4710 || ref->u.c.component->attr.proc_pointer
4711 || (ref->u.c.component->ts.type == BT_CLASS
4712 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4714 gfc_error ("Component to the right of a part reference "
4715 "with nonzero rank must not have the POINTER "
4716 "attribute at %L", &expr->where);
4717 return false;
4719 else if (ref->u.c.component->attr.allocatable
4720 || (ref->u.c.component->ts.type == BT_CLASS
4721 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4724 gfc_error ("Component to the right of a part reference "
4725 "with nonzero rank must not have the ALLOCATABLE "
4726 "attribute at %L", &expr->where);
4727 return false;
4731 n_components++;
4732 break;
4734 case REF_SUBSTRING:
4735 break;
4738 if (((ref->type == REF_COMPONENT && n_components > 1)
4739 || ref->next == NULL)
4740 && current_part_dimension
4741 && seen_part_dimension)
4743 gfc_error ("Two or more part references with nonzero rank must "
4744 "not be specified at %L", &expr->where);
4745 return false;
4748 if (ref->type == REF_COMPONENT)
4750 if (current_part_dimension)
4751 seen_part_dimension = 1;
4753 /* reset to make sure */
4754 current_part_dimension = 0;
4758 return true;
4762 /* Given an expression, determine its shape. This is easier than it sounds.
4763 Leaves the shape array NULL if it is not possible to determine the shape. */
4765 static void
4766 expression_shape (gfc_expr *e)
4768 mpz_t array[GFC_MAX_DIMENSIONS];
4769 int i;
4771 if (e->rank <= 0 || e->shape != NULL)
4772 return;
4774 for (i = 0; i < e->rank; i++)
4775 if (!gfc_array_dimen_size (e, i, &array[i]))
4776 goto fail;
4778 e->shape = gfc_get_shape (e->rank);
4780 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4782 return;
4784 fail:
4785 for (i--; i >= 0; i--)
4786 mpz_clear (array[i]);
4790 /* Given a variable expression node, compute the rank of the expression by
4791 examining the base symbol and any reference structures it may have. */
4793 void
4794 expression_rank (gfc_expr *e)
4796 gfc_ref *ref;
4797 int i, rank;
4799 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4800 could lead to serious confusion... */
4801 gcc_assert (e->expr_type != EXPR_COMPCALL);
4803 if (e->ref == NULL)
4805 if (e->expr_type == EXPR_ARRAY)
4806 goto done;
4807 /* Constructors can have a rank different from one via RESHAPE(). */
4809 if (e->symtree == NULL)
4811 e->rank = 0;
4812 goto done;
4815 e->rank = (e->symtree->n.sym->as == NULL)
4816 ? 0 : e->symtree->n.sym->as->rank;
4817 goto done;
4820 rank = 0;
4822 for (ref = e->ref; ref; ref = ref->next)
4824 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4825 && ref->u.c.component->attr.function && !ref->next)
4826 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4828 if (ref->type != REF_ARRAY)
4829 continue;
4831 if (ref->u.ar.type == AR_FULL)
4833 rank = ref->u.ar.as->rank;
4834 break;
4837 if (ref->u.ar.type == AR_SECTION)
4839 /* Figure out the rank of the section. */
4840 if (rank != 0)
4841 gfc_internal_error ("expression_rank(): Two array specs");
4843 for (i = 0; i < ref->u.ar.dimen; i++)
4844 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4845 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4846 rank++;
4848 break;
4852 e->rank = rank;
4854 done:
4855 expression_shape (e);
4859 static void
4860 add_caf_get_intrinsic (gfc_expr *e)
4862 gfc_expr *wrapper, *tmp_expr;
4863 gfc_ref *ref;
4864 int n;
4866 for (ref = e->ref; ref; ref = ref->next)
4867 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4868 break;
4869 if (ref == NULL)
4870 return;
4872 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
4873 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
4874 return;
4876 tmp_expr = XCNEW (gfc_expr);
4877 *tmp_expr = *e;
4878 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
4879 "caf_get", tmp_expr->where, 1, tmp_expr);
4880 wrapper->ts = e->ts;
4881 wrapper->rank = e->rank;
4882 if (e->rank)
4883 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
4884 *e = *wrapper;
4885 free (wrapper);
4889 static void
4890 remove_caf_get_intrinsic (gfc_expr *e)
4892 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
4893 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
4894 gfc_expr *e2 = e->value.function.actual->expr;
4895 e->value.function.actual->expr = NULL;
4896 gfc_free_actual_arglist (e->value.function.actual);
4897 gfc_free_shape (&e->shape, e->rank);
4898 *e = *e2;
4899 free (e2);
4903 /* Resolve a variable expression. */
4905 static bool
4906 resolve_variable (gfc_expr *e)
4908 gfc_symbol *sym;
4909 bool t;
4911 t = true;
4913 if (e->symtree == NULL)
4914 return false;
4915 sym = e->symtree->n.sym;
4917 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4918 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4919 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4921 if (!actual_arg || inquiry_argument)
4923 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4924 "be used as actual argument", sym->name, &e->where);
4925 return false;
4928 /* TS 29113, 407b. */
4929 else if (e->ts.type == BT_ASSUMED)
4931 if (!actual_arg)
4933 gfc_error ("Assumed-type variable %s at %L may only be used "
4934 "as actual argument", sym->name, &e->where);
4935 return false;
4937 else if (inquiry_argument && !first_actual_arg)
4939 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4940 for all inquiry functions in resolve_function; the reason is
4941 that the function-name resolution happens too late in that
4942 function. */
4943 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4944 "an inquiry function shall be the first argument",
4945 sym->name, &e->where);
4946 return false;
4949 /* TS 29113, C535b. */
4950 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4951 && CLASS_DATA (sym)->as
4952 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4953 || (sym->ts.type != BT_CLASS && sym->as
4954 && sym->as->type == AS_ASSUMED_RANK))
4956 if (!actual_arg)
4958 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4959 "actual argument", sym->name, &e->where);
4960 return false;
4962 else if (inquiry_argument && !first_actual_arg)
4964 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4965 for all inquiry functions in resolve_function; the reason is
4966 that the function-name resolution happens too late in that
4967 function. */
4968 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4969 "to an inquiry function shall be the first argument",
4970 sym->name, &e->where);
4971 return false;
4975 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
4976 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4977 && e->ref->next == NULL))
4979 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4980 "a subobject reference", sym->name, &e->ref->u.ar.where);
4981 return false;
4983 /* TS 29113, 407b. */
4984 else if (e->ts.type == BT_ASSUMED && e->ref
4985 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4986 && e->ref->next == NULL))
4988 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4989 "reference", sym->name, &e->ref->u.ar.where);
4990 return false;
4993 /* TS 29113, C535b. */
4994 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4995 && CLASS_DATA (sym)->as
4996 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4997 || (sym->ts.type != BT_CLASS && sym->as
4998 && sym->as->type == AS_ASSUMED_RANK))
4999 && e->ref
5000 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5001 && e->ref->next == NULL))
5003 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
5004 "reference", sym->name, &e->ref->u.ar.where);
5005 return false;
5008 /* For variables that are used in an associate (target => object) where
5009 the object's basetype is array valued while the target is scalar,
5010 the ts' type of the component refs is still array valued, which
5011 can't be translated that way. */
5012 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
5013 && sym->assoc->target->ts.type == BT_CLASS
5014 && CLASS_DATA (sym->assoc->target)->as)
5016 gfc_ref *ref = e->ref;
5017 while (ref)
5019 switch (ref->type)
5021 case REF_COMPONENT:
5022 ref->u.c.sym = sym->ts.u.derived;
5023 /* Stop the loop. */
5024 ref = NULL;
5025 break;
5026 default:
5027 ref = ref->next;
5028 break;
5033 /* If this is an associate-name, it may be parsed with an array reference
5034 in error even though the target is scalar. Fail directly in this case.
5035 TODO Understand why class scalar expressions must be excluded. */
5036 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5038 if (sym->ts.type == BT_CLASS)
5039 gfc_fix_class_refs (e);
5040 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5041 return false;
5044 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5045 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5047 /* On the other hand, the parser may not have known this is an array;
5048 in this case, we have to add a FULL reference. */
5049 if (sym->assoc && sym->attr.dimension && !e->ref)
5051 e->ref = gfc_get_ref ();
5052 e->ref->type = REF_ARRAY;
5053 e->ref->u.ar.type = AR_FULL;
5054 e->ref->u.ar.dimen = 0;
5057 /* Like above, but for class types, where the checking whether an array
5058 ref is present is more complicated. Furthermore make sure not to add
5059 the full array ref to _vptr or _len refs. */
5060 if (sym->assoc && sym->ts.type == BT_CLASS
5061 && CLASS_DATA (sym)->attr.dimension
5062 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
5064 gfc_ref *ref, *newref;
5066 newref = gfc_get_ref ();
5067 newref->type = REF_ARRAY;
5068 newref->u.ar.type = AR_FULL;
5069 newref->u.ar.dimen = 0;
5070 /* Because this is an associate var and the first ref either is a ref to
5071 the _data component or not, no traversal of the ref chain is
5072 needed. The array ref needs to be inserted after the _data ref,
5073 or when that is not present, which may happend for polymorphic
5074 types, then at the first position. */
5075 ref = e->ref;
5076 if (!ref)
5077 e->ref = newref;
5078 else if (ref->type == REF_COMPONENT
5079 && strcmp ("_data", ref->u.c.component->name) == 0)
5081 if (!ref->next || ref->next->type != REF_ARRAY)
5083 newref->next = ref->next;
5084 ref->next = newref;
5086 else
5087 /* Array ref present already. */
5088 gfc_free_ref_list (newref);
5090 else if (ref->type == REF_ARRAY)
5091 /* Array ref present already. */
5092 gfc_free_ref_list (newref);
5093 else
5095 newref->next = ref;
5096 e->ref = newref;
5100 if (e->ref && !resolve_ref (e))
5101 return false;
5103 if (sym->attr.flavor == FL_PROCEDURE
5104 && (!sym->attr.function
5105 || (sym->attr.function && sym->result
5106 && sym->result->attr.proc_pointer
5107 && !sym->result->attr.function)))
5109 e->ts.type = BT_PROCEDURE;
5110 goto resolve_procedure;
5113 if (sym->ts.type != BT_UNKNOWN)
5114 gfc_variable_attr (e, &e->ts);
5115 else
5117 /* Must be a simple variable reference. */
5118 if (!gfc_set_default_type (sym, 1, sym->ns))
5119 return false;
5120 e->ts = sym->ts;
5123 if (check_assumed_size_reference (sym, e))
5124 return false;
5126 /* Deal with forward references to entries during gfc_resolve_code, to
5127 satisfy, at least partially, 12.5.2.5. */
5128 if (gfc_current_ns->entries
5129 && current_entry_id == sym->entry_id
5130 && cs_base
5131 && cs_base->current
5132 && cs_base->current->op != EXEC_ENTRY)
5134 gfc_entry_list *entry;
5135 gfc_formal_arglist *formal;
5136 int n;
5137 bool seen, saved_specification_expr;
5139 /* If the symbol is a dummy... */
5140 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5142 entry = gfc_current_ns->entries;
5143 seen = false;
5145 /* ...test if the symbol is a parameter of previous entries. */
5146 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5147 for (formal = entry->sym->formal; formal; formal = formal->next)
5149 if (formal->sym && sym->name == formal->sym->name)
5151 seen = true;
5152 break;
5156 /* If it has not been seen as a dummy, this is an error. */
5157 if (!seen)
5159 if (specification_expr)
5160 gfc_error ("Variable %qs, used in a specification expression"
5161 ", is referenced at %L before the ENTRY statement "
5162 "in which it is a parameter",
5163 sym->name, &cs_base->current->loc);
5164 else
5165 gfc_error ("Variable %qs is used at %L before the ENTRY "
5166 "statement in which it is a parameter",
5167 sym->name, &cs_base->current->loc);
5168 t = false;
5172 /* Now do the same check on the specification expressions. */
5173 saved_specification_expr = specification_expr;
5174 specification_expr = true;
5175 if (sym->ts.type == BT_CHARACTER
5176 && !gfc_resolve_expr (sym->ts.u.cl->length))
5177 t = false;
5179 if (sym->as)
5180 for (n = 0; n < sym->as->rank; n++)
5182 if (!gfc_resolve_expr (sym->as->lower[n]))
5183 t = false;
5184 if (!gfc_resolve_expr (sym->as->upper[n]))
5185 t = false;
5187 specification_expr = saved_specification_expr;
5189 if (t)
5190 /* Update the symbol's entry level. */
5191 sym->entry_id = current_entry_id + 1;
5194 /* If a symbol has been host_associated mark it. This is used latter,
5195 to identify if aliasing is possible via host association. */
5196 if (sym->attr.flavor == FL_VARIABLE
5197 && gfc_current_ns->parent
5198 && (gfc_current_ns->parent == sym->ns
5199 || (gfc_current_ns->parent->parent
5200 && gfc_current_ns->parent->parent == sym->ns)))
5201 sym->attr.host_assoc = 1;
5203 if (gfc_current_ns->proc_name
5204 && sym->attr.dimension
5205 && (sym->ns != gfc_current_ns
5206 || sym->attr.use_assoc
5207 || sym->attr.in_common))
5208 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
5210 resolve_procedure:
5211 if (t && !resolve_procedure_expression (e))
5212 t = false;
5214 /* F2008, C617 and C1229. */
5215 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5216 && gfc_is_coindexed (e))
5218 gfc_ref *ref, *ref2 = NULL;
5220 for (ref = e->ref; ref; ref = ref->next)
5222 if (ref->type == REF_COMPONENT)
5223 ref2 = ref;
5224 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5225 break;
5228 for ( ; ref; ref = ref->next)
5229 if (ref->type == REF_COMPONENT)
5230 break;
5232 /* Expression itself is not coindexed object. */
5233 if (ref && e->ts.type == BT_CLASS)
5235 gfc_error ("Polymorphic subobject of coindexed object at %L",
5236 &e->where);
5237 t = false;
5240 /* Expression itself is coindexed object. */
5241 if (ref == NULL)
5243 gfc_component *c;
5244 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5245 for ( ; c; c = c->next)
5246 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5248 gfc_error ("Coindexed object with polymorphic allocatable "
5249 "subcomponent at %L", &e->where);
5250 t = false;
5251 break;
5256 if (t)
5257 expression_rank (e);
5259 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
5260 add_caf_get_intrinsic (e);
5262 return t;
5266 /* Checks to see that the correct symbol has been host associated.
5267 The only situation where this arises is that in which a twice
5268 contained function is parsed after the host association is made.
5269 Therefore, on detecting this, change the symbol in the expression
5270 and convert the array reference into an actual arglist if the old
5271 symbol is a variable. */
5272 static bool
5273 check_host_association (gfc_expr *e)
5275 gfc_symbol *sym, *old_sym;
5276 gfc_symtree *st;
5277 int n;
5278 gfc_ref *ref;
5279 gfc_actual_arglist *arg, *tail = NULL;
5280 bool retval = e->expr_type == EXPR_FUNCTION;
5282 /* If the expression is the result of substitution in
5283 interface.c(gfc_extend_expr) because there is no way in
5284 which the host association can be wrong. */
5285 if (e->symtree == NULL
5286 || e->symtree->n.sym == NULL
5287 || e->user_operator)
5288 return retval;
5290 old_sym = e->symtree->n.sym;
5292 if (gfc_current_ns->parent
5293 && old_sym->ns != gfc_current_ns)
5295 /* Use the 'USE' name so that renamed module symbols are
5296 correctly handled. */
5297 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5299 if (sym && old_sym != sym
5300 && sym->ts.type == old_sym->ts.type
5301 && sym->attr.flavor == FL_PROCEDURE
5302 && sym->attr.contained)
5304 /* Clear the shape, since it might not be valid. */
5305 gfc_free_shape (&e->shape, e->rank);
5307 /* Give the expression the right symtree! */
5308 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5309 gcc_assert (st != NULL);
5311 if (old_sym->attr.flavor == FL_PROCEDURE
5312 || e->expr_type == EXPR_FUNCTION)
5314 /* Original was function so point to the new symbol, since
5315 the actual argument list is already attached to the
5316 expression. */
5317 e->value.function.esym = NULL;
5318 e->symtree = st;
5320 else
5322 /* Original was variable so convert array references into
5323 an actual arglist. This does not need any checking now
5324 since resolve_function will take care of it. */
5325 e->value.function.actual = NULL;
5326 e->expr_type = EXPR_FUNCTION;
5327 e->symtree = st;
5329 /* Ambiguity will not arise if the array reference is not
5330 the last reference. */
5331 for (ref = e->ref; ref; ref = ref->next)
5332 if (ref->type == REF_ARRAY && ref->next == NULL)
5333 break;
5335 gcc_assert (ref->type == REF_ARRAY);
5337 /* Grab the start expressions from the array ref and
5338 copy them into actual arguments. */
5339 for (n = 0; n < ref->u.ar.dimen; n++)
5341 arg = gfc_get_actual_arglist ();
5342 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5343 if (e->value.function.actual == NULL)
5344 tail = e->value.function.actual = arg;
5345 else
5347 tail->next = arg;
5348 tail = arg;
5352 /* Dump the reference list and set the rank. */
5353 gfc_free_ref_list (e->ref);
5354 e->ref = NULL;
5355 e->rank = sym->as ? sym->as->rank : 0;
5358 gfc_resolve_expr (e);
5359 sym->refs++;
5362 /* This might have changed! */
5363 return e->expr_type == EXPR_FUNCTION;
5367 static void
5368 gfc_resolve_character_operator (gfc_expr *e)
5370 gfc_expr *op1 = e->value.op.op1;
5371 gfc_expr *op2 = e->value.op.op2;
5372 gfc_expr *e1 = NULL;
5373 gfc_expr *e2 = NULL;
5375 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5377 if (op1->ts.u.cl && op1->ts.u.cl->length)
5378 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5379 else if (op1->expr_type == EXPR_CONSTANT)
5380 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5381 op1->value.character.length);
5383 if (op2->ts.u.cl && op2->ts.u.cl->length)
5384 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5385 else if (op2->expr_type == EXPR_CONSTANT)
5386 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5387 op2->value.character.length);
5389 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5391 if (!e1 || !e2)
5393 gfc_free_expr (e1);
5394 gfc_free_expr (e2);
5396 return;
5399 e->ts.u.cl->length = gfc_add (e1, e2);
5400 e->ts.u.cl->length->ts.type = BT_INTEGER;
5401 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5402 gfc_simplify_expr (e->ts.u.cl->length, 0);
5403 gfc_resolve_expr (e->ts.u.cl->length);
5405 return;
5409 /* Ensure that an character expression has a charlen and, if possible, a
5410 length expression. */
5412 static void
5413 fixup_charlen (gfc_expr *e)
5415 /* The cases fall through so that changes in expression type and the need
5416 for multiple fixes are picked up. In all circumstances, a charlen should
5417 be available for the middle end to hang a backend_decl on. */
5418 switch (e->expr_type)
5420 case EXPR_OP:
5421 gfc_resolve_character_operator (e);
5423 case EXPR_ARRAY:
5424 if (e->expr_type == EXPR_ARRAY)
5425 gfc_resolve_character_array_constructor (e);
5427 case EXPR_SUBSTRING:
5428 if (!e->ts.u.cl && e->ref)
5429 gfc_resolve_substring_charlen (e);
5431 default:
5432 if (!e->ts.u.cl)
5433 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5435 break;
5440 /* Update an actual argument to include the passed-object for type-bound
5441 procedures at the right position. */
5443 static gfc_actual_arglist*
5444 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5445 const char *name)
5447 gcc_assert (argpos > 0);
5449 if (argpos == 1)
5451 gfc_actual_arglist* result;
5453 result = gfc_get_actual_arglist ();
5454 result->expr = po;
5455 result->next = lst;
5456 if (name)
5457 result->name = name;
5459 return result;
5462 if (lst)
5463 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5464 else
5465 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5466 return lst;
5470 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5472 static gfc_expr*
5473 extract_compcall_passed_object (gfc_expr* e)
5475 gfc_expr* po;
5477 gcc_assert (e->expr_type == EXPR_COMPCALL);
5479 if (e->value.compcall.base_object)
5480 po = gfc_copy_expr (e->value.compcall.base_object);
5481 else
5483 po = gfc_get_expr ();
5484 po->expr_type = EXPR_VARIABLE;
5485 po->symtree = e->symtree;
5486 po->ref = gfc_copy_ref (e->ref);
5487 po->where = e->where;
5490 if (!gfc_resolve_expr (po))
5491 return NULL;
5493 return po;
5497 /* Update the arglist of an EXPR_COMPCALL expression to include the
5498 passed-object. */
5500 static bool
5501 update_compcall_arglist (gfc_expr* e)
5503 gfc_expr* po;
5504 gfc_typebound_proc* tbp;
5506 tbp = e->value.compcall.tbp;
5508 if (tbp->error)
5509 return false;
5511 po = extract_compcall_passed_object (e);
5512 if (!po)
5513 return false;
5515 if (tbp->nopass || e->value.compcall.ignore_pass)
5517 gfc_free_expr (po);
5518 return true;
5521 gcc_assert (tbp->pass_arg_num > 0);
5522 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5523 tbp->pass_arg_num,
5524 tbp->pass_arg);
5526 return true;
5530 /* Extract the passed object from a PPC call (a copy of it). */
5532 static gfc_expr*
5533 extract_ppc_passed_object (gfc_expr *e)
5535 gfc_expr *po;
5536 gfc_ref **ref;
5538 po = gfc_get_expr ();
5539 po->expr_type = EXPR_VARIABLE;
5540 po->symtree = e->symtree;
5541 po->ref = gfc_copy_ref (e->ref);
5542 po->where = e->where;
5544 /* Remove PPC reference. */
5545 ref = &po->ref;
5546 while ((*ref)->next)
5547 ref = &(*ref)->next;
5548 gfc_free_ref_list (*ref);
5549 *ref = NULL;
5551 if (!gfc_resolve_expr (po))
5552 return NULL;
5554 return po;
5558 /* Update the actual arglist of a procedure pointer component to include the
5559 passed-object. */
5561 static bool
5562 update_ppc_arglist (gfc_expr* e)
5564 gfc_expr* po;
5565 gfc_component *ppc;
5566 gfc_typebound_proc* tb;
5568 ppc = gfc_get_proc_ptr_comp (e);
5569 if (!ppc)
5570 return false;
5572 tb = ppc->tb;
5574 if (tb->error)
5575 return false;
5576 else if (tb->nopass)
5577 return true;
5579 po = extract_ppc_passed_object (e);
5580 if (!po)
5581 return false;
5583 /* F08:R739. */
5584 if (po->rank != 0)
5586 gfc_error ("Passed-object at %L must be scalar", &e->where);
5587 return false;
5590 /* F08:C611. */
5591 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5593 gfc_error ("Base object for procedure-pointer component call at %L is of"
5594 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
5595 return false;
5598 gcc_assert (tb->pass_arg_num > 0);
5599 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5600 tb->pass_arg_num,
5601 tb->pass_arg);
5603 return true;
5607 /* Check that the object a TBP is called on is valid, i.e. it must not be
5608 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5610 static bool
5611 check_typebound_baseobject (gfc_expr* e)
5613 gfc_expr* base;
5614 bool return_value = false;
5616 base = extract_compcall_passed_object (e);
5617 if (!base)
5618 return false;
5620 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5622 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
5623 return false;
5625 /* F08:C611. */
5626 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5628 gfc_error ("Base object for type-bound procedure call at %L is of"
5629 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
5630 goto cleanup;
5633 /* F08:C1230. If the procedure called is NOPASS,
5634 the base object must be scalar. */
5635 if (e->value.compcall.tbp->nopass && base->rank != 0)
5637 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5638 " be scalar", &e->where);
5639 goto cleanup;
5642 return_value = true;
5644 cleanup:
5645 gfc_free_expr (base);
5646 return return_value;
5650 /* Resolve a call to a type-bound procedure, either function or subroutine,
5651 statically from the data in an EXPR_COMPCALL expression. The adapted
5652 arglist and the target-procedure symtree are returned. */
5654 static bool
5655 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5656 gfc_actual_arglist** actual)
5658 gcc_assert (e->expr_type == EXPR_COMPCALL);
5659 gcc_assert (!e->value.compcall.tbp->is_generic);
5661 /* Update the actual arglist for PASS. */
5662 if (!update_compcall_arglist (e))
5663 return false;
5665 *actual = e->value.compcall.actual;
5666 *target = e->value.compcall.tbp->u.specific;
5668 gfc_free_ref_list (e->ref);
5669 e->ref = NULL;
5670 e->value.compcall.actual = NULL;
5672 /* If we find a deferred typebound procedure, check for derived types
5673 that an overriding typebound procedure has not been missed. */
5674 if (e->value.compcall.name
5675 && !e->value.compcall.tbp->non_overridable
5676 && e->value.compcall.base_object
5677 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5679 gfc_symtree *st;
5680 gfc_symbol *derived;
5682 /* Use the derived type of the base_object. */
5683 derived = e->value.compcall.base_object->ts.u.derived;
5684 st = NULL;
5686 /* If necessary, go through the inheritance chain. */
5687 while (!st && derived)
5689 /* Look for the typebound procedure 'name'. */
5690 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5691 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5692 e->value.compcall.name);
5693 if (!st)
5694 derived = gfc_get_derived_super_type (derived);
5697 /* Now find the specific name in the derived type namespace. */
5698 if (st && st->n.tb && st->n.tb->u.specific)
5699 gfc_find_sym_tree (st->n.tb->u.specific->name,
5700 derived->ns, 1, &st);
5701 if (st)
5702 *target = st;
5704 return true;
5708 /* Get the ultimate declared type from an expression. In addition,
5709 return the last class/derived type reference and the copy of the
5710 reference list. If check_types is set true, derived types are
5711 identified as well as class references. */
5712 static gfc_symbol*
5713 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5714 gfc_expr *e, bool check_types)
5716 gfc_symbol *declared;
5717 gfc_ref *ref;
5719 declared = NULL;
5720 if (class_ref)
5721 *class_ref = NULL;
5722 if (new_ref)
5723 *new_ref = gfc_copy_ref (e->ref);
5725 for (ref = e->ref; ref; ref = ref->next)
5727 if (ref->type != REF_COMPONENT)
5728 continue;
5730 if ((ref->u.c.component->ts.type == BT_CLASS
5731 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
5732 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5734 declared = ref->u.c.component->ts.u.derived;
5735 if (class_ref)
5736 *class_ref = ref;
5740 if (declared == NULL)
5741 declared = e->symtree->n.sym->ts.u.derived;
5743 return declared;
5747 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5748 which of the specific bindings (if any) matches the arglist and transform
5749 the expression into a call of that binding. */
5751 static bool
5752 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5754 gfc_typebound_proc* genproc;
5755 const char* genname;
5756 gfc_symtree *st;
5757 gfc_symbol *derived;
5759 gcc_assert (e->expr_type == EXPR_COMPCALL);
5760 genname = e->value.compcall.name;
5761 genproc = e->value.compcall.tbp;
5763 if (!genproc->is_generic)
5764 return true;
5766 /* Try the bindings on this type and in the inheritance hierarchy. */
5767 for (; genproc; genproc = genproc->overridden)
5769 gfc_tbp_generic* g;
5771 gcc_assert (genproc->is_generic);
5772 for (g = genproc->u.generic; g; g = g->next)
5774 gfc_symbol* target;
5775 gfc_actual_arglist* args;
5776 bool matches;
5778 gcc_assert (g->specific);
5780 if (g->specific->error)
5781 continue;
5783 target = g->specific->u.specific->n.sym;
5785 /* Get the right arglist by handling PASS/NOPASS. */
5786 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5787 if (!g->specific->nopass)
5789 gfc_expr* po;
5790 po = extract_compcall_passed_object (e);
5791 if (!po)
5793 gfc_free_actual_arglist (args);
5794 return false;
5797 gcc_assert (g->specific->pass_arg_num > 0);
5798 gcc_assert (!g->specific->error);
5799 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5800 g->specific->pass_arg);
5802 resolve_actual_arglist (args, target->attr.proc,
5803 is_external_proc (target)
5804 && gfc_sym_get_dummy_args (target) == NULL);
5806 /* Check if this arglist matches the formal. */
5807 matches = gfc_arglist_matches_symbol (&args, target);
5809 /* Clean up and break out of the loop if we've found it. */
5810 gfc_free_actual_arglist (args);
5811 if (matches)
5813 e->value.compcall.tbp = g->specific;
5814 genname = g->specific_st->name;
5815 /* Pass along the name for CLASS methods, where the vtab
5816 procedure pointer component has to be referenced. */
5817 if (name)
5818 *name = genname;
5819 goto success;
5824 /* Nothing matching found! */
5825 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5826 " %qs at %L", genname, &e->where);
5827 return false;
5829 success:
5830 /* Make sure that we have the right specific instance for the name. */
5831 derived = get_declared_from_expr (NULL, NULL, e, true);
5833 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5834 if (st)
5835 e->value.compcall.tbp = st->n.tb;
5837 return true;
5841 /* Resolve a call to a type-bound subroutine. */
5843 static bool
5844 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
5846 gfc_actual_arglist* newactual;
5847 gfc_symtree* target;
5849 /* Check that's really a SUBROUTINE. */
5850 if (!c->expr1->value.compcall.tbp->subroutine)
5852 gfc_error ("%qs at %L should be a SUBROUTINE",
5853 c->expr1->value.compcall.name, &c->loc);
5854 return false;
5857 if (!check_typebound_baseobject (c->expr1))
5858 return false;
5860 /* Pass along the name for CLASS methods, where the vtab
5861 procedure pointer component has to be referenced. */
5862 if (name)
5863 *name = c->expr1->value.compcall.name;
5865 if (!resolve_typebound_generic_call (c->expr1, name))
5866 return false;
5868 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
5869 if (overridable)
5870 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
5872 /* Transform into an ordinary EXEC_CALL for now. */
5874 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5875 return false;
5877 c->ext.actual = newactual;
5878 c->symtree = target;
5879 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5881 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5883 gfc_free_expr (c->expr1);
5884 c->expr1 = gfc_get_expr ();
5885 c->expr1->expr_type = EXPR_FUNCTION;
5886 c->expr1->symtree = target;
5887 c->expr1->where = c->loc;
5889 return resolve_call (c);
5893 /* Resolve a component-call expression. */
5894 static bool
5895 resolve_compcall (gfc_expr* e, const char **name)
5897 gfc_actual_arglist* newactual;
5898 gfc_symtree* target;
5900 /* Check that's really a FUNCTION. */
5901 if (!e->value.compcall.tbp->function)
5903 gfc_error ("%qs at %L should be a FUNCTION",
5904 e->value.compcall.name, &e->where);
5905 return false;
5908 /* These must not be assign-calls! */
5909 gcc_assert (!e->value.compcall.assign);
5911 if (!check_typebound_baseobject (e))
5912 return false;
5914 /* Pass along the name for CLASS methods, where the vtab
5915 procedure pointer component has to be referenced. */
5916 if (name)
5917 *name = e->value.compcall.name;
5919 if (!resolve_typebound_generic_call (e, name))
5920 return false;
5921 gcc_assert (!e->value.compcall.tbp->is_generic);
5923 /* Take the rank from the function's symbol. */
5924 if (e->value.compcall.tbp->u.specific->n.sym->as)
5925 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5927 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5928 arglist to the TBP's binding target. */
5930 if (!resolve_typebound_static (e, &target, &newactual))
5931 return false;
5933 e->value.function.actual = newactual;
5934 e->value.function.name = NULL;
5935 e->value.function.esym = target->n.sym;
5936 e->value.function.isym = NULL;
5937 e->symtree = target;
5938 e->ts = target->n.sym->ts;
5939 e->expr_type = EXPR_FUNCTION;
5941 /* Resolution is not necessary if this is a class subroutine; this
5942 function only has to identify the specific proc. Resolution of
5943 the call will be done next in resolve_typebound_call. */
5944 return gfc_resolve_expr (e);
5948 static bool resolve_fl_derived (gfc_symbol *sym);
5951 /* Resolve a typebound function, or 'method'. First separate all
5952 the non-CLASS references by calling resolve_compcall directly. */
5954 static bool
5955 resolve_typebound_function (gfc_expr* e)
5957 gfc_symbol *declared;
5958 gfc_component *c;
5959 gfc_ref *new_ref;
5960 gfc_ref *class_ref;
5961 gfc_symtree *st;
5962 const char *name;
5963 gfc_typespec ts;
5964 gfc_expr *expr;
5965 bool overridable;
5967 st = e->symtree;
5969 /* Deal with typebound operators for CLASS objects. */
5970 expr = e->value.compcall.base_object;
5971 overridable = !e->value.compcall.tbp->non_overridable;
5972 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5974 /* If the base_object is not a variable, the corresponding actual
5975 argument expression must be stored in e->base_expression so
5976 that the corresponding tree temporary can be used as the base
5977 object in gfc_conv_procedure_call. */
5978 if (expr->expr_type != EXPR_VARIABLE)
5980 gfc_actual_arglist *args;
5982 for (args= e->value.function.actual; args; args = args->next)
5984 if (expr == args->expr)
5985 expr = args->expr;
5989 /* Since the typebound operators are generic, we have to ensure
5990 that any delays in resolution are corrected and that the vtab
5991 is present. */
5992 ts = expr->ts;
5993 declared = ts.u.derived;
5994 c = gfc_find_component (declared, "_vptr", true, true, NULL);
5995 if (c->ts.u.derived == NULL)
5996 c->ts.u.derived = gfc_find_derived_vtab (declared);
5998 if (!resolve_compcall (e, &name))
5999 return false;
6001 /* Use the generic name if it is there. */
6002 name = name ? name : e->value.function.esym->name;
6003 e->symtree = expr->symtree;
6004 e->ref = gfc_copy_ref (expr->ref);
6005 get_declared_from_expr (&class_ref, NULL, e, false);
6007 /* Trim away the extraneous references that emerge from nested
6008 use of interface.c (extend_expr). */
6009 if (class_ref && class_ref->next)
6011 gfc_free_ref_list (class_ref->next);
6012 class_ref->next = NULL;
6014 else if (e->ref && !class_ref)
6016 gfc_free_ref_list (e->ref);
6017 e->ref = NULL;
6020 gfc_add_vptr_component (e);
6021 gfc_add_component_ref (e, name);
6022 e->value.function.esym = NULL;
6023 if (expr->expr_type != EXPR_VARIABLE)
6024 e->base_expr = expr;
6025 return true;
6028 if (st == NULL)
6029 return resolve_compcall (e, NULL);
6031 if (!resolve_ref (e))
6032 return false;
6034 /* Get the CLASS declared type. */
6035 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6037 if (!resolve_fl_derived (declared))
6038 return false;
6040 /* Weed out cases of the ultimate component being a derived type. */
6041 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6042 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6044 gfc_free_ref_list (new_ref);
6045 return resolve_compcall (e, NULL);
6048 c = gfc_find_component (declared, "_data", true, true, NULL);
6049 declared = c->ts.u.derived;
6051 /* Treat the call as if it is a typebound procedure, in order to roll
6052 out the correct name for the specific function. */
6053 if (!resolve_compcall (e, &name))
6055 gfc_free_ref_list (new_ref);
6056 return false;
6058 ts = e->ts;
6060 if (overridable)
6062 /* Convert the expression to a procedure pointer component call. */
6063 e->value.function.esym = NULL;
6064 e->symtree = st;
6066 if (new_ref)
6067 e->ref = new_ref;
6069 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6070 gfc_add_vptr_component (e);
6071 gfc_add_component_ref (e, name);
6073 /* Recover the typespec for the expression. This is really only
6074 necessary for generic procedures, where the additional call
6075 to gfc_add_component_ref seems to throw the collection of the
6076 correct typespec. */
6077 e->ts = ts;
6079 else if (new_ref)
6080 gfc_free_ref_list (new_ref);
6082 return true;
6085 /* Resolve a typebound subroutine, or 'method'. First separate all
6086 the non-CLASS references by calling resolve_typebound_call
6087 directly. */
6089 static bool
6090 resolve_typebound_subroutine (gfc_code *code)
6092 gfc_symbol *declared;
6093 gfc_component *c;
6094 gfc_ref *new_ref;
6095 gfc_ref *class_ref;
6096 gfc_symtree *st;
6097 const char *name;
6098 gfc_typespec ts;
6099 gfc_expr *expr;
6100 bool overridable;
6102 st = code->expr1->symtree;
6104 /* Deal with typebound operators for CLASS objects. */
6105 expr = code->expr1->value.compcall.base_object;
6106 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6107 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6109 /* If the base_object is not a variable, the corresponding actual
6110 argument expression must be stored in e->base_expression so
6111 that the corresponding tree temporary can be used as the base
6112 object in gfc_conv_procedure_call. */
6113 if (expr->expr_type != EXPR_VARIABLE)
6115 gfc_actual_arglist *args;
6117 args= code->expr1->value.function.actual;
6118 for (; args; args = args->next)
6119 if (expr == args->expr)
6120 expr = args->expr;
6123 /* Since the typebound operators are generic, we have to ensure
6124 that any delays in resolution are corrected and that the vtab
6125 is present. */
6126 declared = expr->ts.u.derived;
6127 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6128 if (c->ts.u.derived == NULL)
6129 c->ts.u.derived = gfc_find_derived_vtab (declared);
6131 if (!resolve_typebound_call (code, &name, NULL))
6132 return false;
6134 /* Use the generic name if it is there. */
6135 name = name ? name : code->expr1->value.function.esym->name;
6136 code->expr1->symtree = expr->symtree;
6137 code->expr1->ref = gfc_copy_ref (expr->ref);
6139 /* Trim away the extraneous references that emerge from nested
6140 use of interface.c (extend_expr). */
6141 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6142 if (class_ref && class_ref->next)
6144 gfc_free_ref_list (class_ref->next);
6145 class_ref->next = NULL;
6147 else if (code->expr1->ref && !class_ref)
6149 gfc_free_ref_list (code->expr1->ref);
6150 code->expr1->ref = NULL;
6153 /* Now use the procedure in the vtable. */
6154 gfc_add_vptr_component (code->expr1);
6155 gfc_add_component_ref (code->expr1, name);
6156 code->expr1->value.function.esym = NULL;
6157 if (expr->expr_type != EXPR_VARIABLE)
6158 code->expr1->base_expr = expr;
6159 return true;
6162 if (st == NULL)
6163 return resolve_typebound_call (code, NULL, NULL);
6165 if (!resolve_ref (code->expr1))
6166 return false;
6168 /* Get the CLASS declared type. */
6169 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
6171 /* Weed out cases of the ultimate component being a derived type. */
6172 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6173 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6175 gfc_free_ref_list (new_ref);
6176 return resolve_typebound_call (code, NULL, NULL);
6179 if (!resolve_typebound_call (code, &name, &overridable))
6181 gfc_free_ref_list (new_ref);
6182 return false;
6184 ts = code->expr1->ts;
6186 if (overridable)
6188 /* Convert the expression to a procedure pointer component call. */
6189 code->expr1->value.function.esym = NULL;
6190 code->expr1->symtree = st;
6192 if (new_ref)
6193 code->expr1->ref = new_ref;
6195 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6196 gfc_add_vptr_component (code->expr1);
6197 gfc_add_component_ref (code->expr1, name);
6199 /* Recover the typespec for the expression. This is really only
6200 necessary for generic procedures, where the additional call
6201 to gfc_add_component_ref seems to throw the collection of the
6202 correct typespec. */
6203 code->expr1->ts = ts;
6205 else if (new_ref)
6206 gfc_free_ref_list (new_ref);
6208 return true;
6212 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6214 static bool
6215 resolve_ppc_call (gfc_code* c)
6217 gfc_component *comp;
6219 comp = gfc_get_proc_ptr_comp (c->expr1);
6220 gcc_assert (comp != NULL);
6222 c->resolved_sym = c->expr1->symtree->n.sym;
6223 c->expr1->expr_type = EXPR_VARIABLE;
6225 if (!comp->attr.subroutine)
6226 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6228 if (!resolve_ref (c->expr1))
6229 return false;
6231 if (!update_ppc_arglist (c->expr1))
6232 return false;
6234 c->ext.actual = c->expr1->value.compcall.actual;
6236 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6237 !(comp->ts.interface
6238 && comp->ts.interface->formal)))
6239 return false;
6241 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
6242 return false;
6244 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6246 return true;
6250 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6252 static bool
6253 resolve_expr_ppc (gfc_expr* e)
6255 gfc_component *comp;
6257 comp = gfc_get_proc_ptr_comp (e);
6258 gcc_assert (comp != NULL);
6260 /* Convert to EXPR_FUNCTION. */
6261 e->expr_type = EXPR_FUNCTION;
6262 e->value.function.isym = NULL;
6263 e->value.function.actual = e->value.compcall.actual;
6264 e->ts = comp->ts;
6265 if (comp->as != NULL)
6266 e->rank = comp->as->rank;
6268 if (!comp->attr.function)
6269 gfc_add_function (&comp->attr, comp->name, &e->where);
6271 if (!resolve_ref (e))
6272 return false;
6274 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6275 !(comp->ts.interface
6276 && comp->ts.interface->formal)))
6277 return false;
6279 if (!update_ppc_arglist (e))
6280 return false;
6282 if (!check_pure_function(e))
6283 return false;
6285 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6287 return true;
6291 static bool
6292 gfc_is_expandable_expr (gfc_expr *e)
6294 gfc_constructor *con;
6296 if (e->expr_type == EXPR_ARRAY)
6298 /* Traverse the constructor looking for variables that are flavor
6299 parameter. Parameters must be expanded since they are fully used at
6300 compile time. */
6301 con = gfc_constructor_first (e->value.constructor);
6302 for (; con; con = gfc_constructor_next (con))
6304 if (con->expr->expr_type == EXPR_VARIABLE
6305 && con->expr->symtree
6306 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6307 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6308 return true;
6309 if (con->expr->expr_type == EXPR_ARRAY
6310 && gfc_is_expandable_expr (con->expr))
6311 return true;
6315 return false;
6318 /* Resolve an expression. That is, make sure that types of operands agree
6319 with their operators, intrinsic operators are converted to function calls
6320 for overloaded types and unresolved function references are resolved. */
6322 bool
6323 gfc_resolve_expr (gfc_expr *e)
6325 bool t;
6326 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6328 if (e == NULL)
6329 return true;
6331 /* inquiry_argument only applies to variables. */
6332 inquiry_save = inquiry_argument;
6333 actual_arg_save = actual_arg;
6334 first_actual_arg_save = first_actual_arg;
6336 if (e->expr_type != EXPR_VARIABLE)
6338 inquiry_argument = false;
6339 actual_arg = false;
6340 first_actual_arg = false;
6343 switch (e->expr_type)
6345 case EXPR_OP:
6346 t = resolve_operator (e);
6347 break;
6349 case EXPR_FUNCTION:
6350 case EXPR_VARIABLE:
6352 if (check_host_association (e))
6353 t = resolve_function (e);
6354 else
6355 t = resolve_variable (e);
6357 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6358 && e->ref->type != REF_SUBSTRING)
6359 gfc_resolve_substring_charlen (e);
6361 break;
6363 case EXPR_COMPCALL:
6364 t = resolve_typebound_function (e);
6365 break;
6367 case EXPR_SUBSTRING:
6368 t = resolve_ref (e);
6369 break;
6371 case EXPR_CONSTANT:
6372 case EXPR_NULL:
6373 t = true;
6374 break;
6376 case EXPR_PPC:
6377 t = resolve_expr_ppc (e);
6378 break;
6380 case EXPR_ARRAY:
6381 t = false;
6382 if (!resolve_ref (e))
6383 break;
6385 t = gfc_resolve_array_constructor (e);
6386 /* Also try to expand a constructor. */
6387 if (t)
6389 expression_rank (e);
6390 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6391 gfc_expand_constructor (e, false);
6394 /* This provides the opportunity for the length of constructors with
6395 character valued function elements to propagate the string length
6396 to the expression. */
6397 if (t && e->ts.type == BT_CHARACTER)
6399 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6400 here rather then add a duplicate test for it above. */
6401 gfc_expand_constructor (e, false);
6402 t = gfc_resolve_character_array_constructor (e);
6405 break;
6407 case EXPR_STRUCTURE:
6408 t = resolve_ref (e);
6409 if (!t)
6410 break;
6412 t = resolve_structure_cons (e, 0);
6413 if (!t)
6414 break;
6416 t = gfc_simplify_expr (e, 0);
6417 break;
6419 default:
6420 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6423 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
6424 fixup_charlen (e);
6426 inquiry_argument = inquiry_save;
6427 actual_arg = actual_arg_save;
6428 first_actual_arg = first_actual_arg_save;
6430 return t;
6434 /* Resolve an expression from an iterator. They must be scalar and have
6435 INTEGER or (optionally) REAL type. */
6437 static bool
6438 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6439 const char *name_msgid)
6441 if (!gfc_resolve_expr (expr))
6442 return false;
6444 if (expr->rank != 0)
6446 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6447 return false;
6450 if (expr->ts.type != BT_INTEGER)
6452 if (expr->ts.type == BT_REAL)
6454 if (real_ok)
6455 return gfc_notify_std (GFC_STD_F95_DEL,
6456 "%s at %L must be integer",
6457 _(name_msgid), &expr->where);
6458 else
6460 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6461 &expr->where);
6462 return false;
6465 else
6467 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6468 return false;
6471 return true;
6475 /* Resolve the expressions in an iterator structure. If REAL_OK is
6476 false allow only INTEGER type iterators, otherwise allow REAL types.
6477 Set own_scope to true for ac-implied-do and data-implied-do as those
6478 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
6480 bool
6481 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
6483 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6484 return false;
6486 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6487 _("iterator variable")))
6488 return false;
6490 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6491 "Start expression in DO loop"))
6492 return false;
6494 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6495 "End expression in DO loop"))
6496 return false;
6498 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6499 "Step expression in DO loop"))
6500 return false;
6502 if (iter->step->expr_type == EXPR_CONSTANT)
6504 if ((iter->step->ts.type == BT_INTEGER
6505 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6506 || (iter->step->ts.type == BT_REAL
6507 && mpfr_sgn (iter->step->value.real) == 0))
6509 gfc_error ("Step expression in DO loop at %L cannot be zero",
6510 &iter->step->where);
6511 return false;
6515 /* Convert start, end, and step to the same type as var. */
6516 if (iter->start->ts.kind != iter->var->ts.kind
6517 || iter->start->ts.type != iter->var->ts.type)
6518 gfc_convert_type (iter->start, &iter->var->ts, 1);
6520 if (iter->end->ts.kind != iter->var->ts.kind
6521 || iter->end->ts.type != iter->var->ts.type)
6522 gfc_convert_type (iter->end, &iter->var->ts, 1);
6524 if (iter->step->ts.kind != iter->var->ts.kind
6525 || iter->step->ts.type != iter->var->ts.type)
6526 gfc_convert_type (iter->step, &iter->var->ts, 1);
6528 if (iter->start->expr_type == EXPR_CONSTANT
6529 && iter->end->expr_type == EXPR_CONSTANT
6530 && iter->step->expr_type == EXPR_CONSTANT)
6532 int sgn, cmp;
6533 if (iter->start->ts.type == BT_INTEGER)
6535 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6536 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6538 else
6540 sgn = mpfr_sgn (iter->step->value.real);
6541 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6543 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
6544 gfc_warning (OPT_Wzerotrip,
6545 "DO loop at %L will be executed zero times",
6546 &iter->step->where);
6549 if (iter->end->expr_type == EXPR_CONSTANT
6550 && iter->end->ts.type == BT_INTEGER
6551 && iter->step->expr_type == EXPR_CONSTANT
6552 && iter->step->ts.type == BT_INTEGER
6553 && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
6554 || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
6556 bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
6557 int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
6559 if (is_step_positive
6560 && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
6561 gfc_warning (OPT_Wundefined_do_loop,
6562 "DO loop at %L is undefined as it overflows",
6563 &iter->step->where);
6564 else if (!is_step_positive
6565 && mpz_cmp (iter->end->value.integer,
6566 gfc_integer_kinds[k].min_int) == 0)
6567 gfc_warning (OPT_Wundefined_do_loop,
6568 "DO loop at %L is undefined as it underflows",
6569 &iter->step->where);
6572 return true;
6576 /* Traversal function for find_forall_index. f == 2 signals that
6577 that variable itself is not to be checked - only the references. */
6579 static bool
6580 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6582 if (expr->expr_type != EXPR_VARIABLE)
6583 return false;
6585 /* A scalar assignment */
6586 if (!expr->ref || *f == 1)
6588 if (expr->symtree->n.sym == sym)
6589 return true;
6590 else
6591 return false;
6594 if (*f == 2)
6595 *f = 1;
6596 return false;
6600 /* Check whether the FORALL index appears in the expression or not.
6601 Returns true if SYM is found in EXPR. */
6603 bool
6604 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6606 if (gfc_traverse_expr (expr, sym, forall_index, f))
6607 return true;
6608 else
6609 return false;
6613 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6614 to be a scalar INTEGER variable. The subscripts and stride are scalar
6615 INTEGERs, and if stride is a constant it must be nonzero.
6616 Furthermore "A subscript or stride in a forall-triplet-spec shall
6617 not contain a reference to any index-name in the
6618 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6620 static void
6621 resolve_forall_iterators (gfc_forall_iterator *it)
6623 gfc_forall_iterator *iter, *iter2;
6625 for (iter = it; iter; iter = iter->next)
6627 if (gfc_resolve_expr (iter->var)
6628 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6629 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6630 &iter->var->where);
6632 if (gfc_resolve_expr (iter->start)
6633 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6634 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6635 &iter->start->where);
6636 if (iter->var->ts.kind != iter->start->ts.kind)
6637 gfc_convert_type (iter->start, &iter->var->ts, 1);
6639 if (gfc_resolve_expr (iter->end)
6640 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6641 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6642 &iter->end->where);
6643 if (iter->var->ts.kind != iter->end->ts.kind)
6644 gfc_convert_type (iter->end, &iter->var->ts, 1);
6646 if (gfc_resolve_expr (iter->stride))
6648 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6649 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6650 &iter->stride->where, "INTEGER");
6652 if (iter->stride->expr_type == EXPR_CONSTANT
6653 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
6654 gfc_error ("FORALL stride expression at %L cannot be zero",
6655 &iter->stride->where);
6657 if (iter->var->ts.kind != iter->stride->ts.kind)
6658 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6661 for (iter = it; iter; iter = iter->next)
6662 for (iter2 = iter; iter2; iter2 = iter2->next)
6664 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6665 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6666 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
6667 gfc_error ("FORALL index %qs may not appear in triplet "
6668 "specification at %L", iter->var->symtree->name,
6669 &iter2->start->where);
6674 /* Given a pointer to a symbol that is a derived type, see if it's
6675 inaccessible, i.e. if it's defined in another module and the components are
6676 PRIVATE. The search is recursive if necessary. Returns zero if no
6677 inaccessible components are found, nonzero otherwise. */
6679 static int
6680 derived_inaccessible (gfc_symbol *sym)
6682 gfc_component *c;
6684 if (sym->attr.use_assoc && sym->attr.private_comp)
6685 return 1;
6687 for (c = sym->components; c; c = c->next)
6689 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6690 return 1;
6693 return 0;
6697 /* Resolve the argument of a deallocate expression. The expression must be
6698 a pointer or a full array. */
6700 static bool
6701 resolve_deallocate_expr (gfc_expr *e)
6703 symbol_attribute attr;
6704 int allocatable, pointer;
6705 gfc_ref *ref;
6706 gfc_symbol *sym;
6707 gfc_component *c;
6708 bool unlimited;
6710 if (!gfc_resolve_expr (e))
6711 return false;
6713 if (e->expr_type != EXPR_VARIABLE)
6714 goto bad;
6716 sym = e->symtree->n.sym;
6717 unlimited = UNLIMITED_POLY(sym);
6719 if (sym->ts.type == BT_CLASS)
6721 allocatable = CLASS_DATA (sym)->attr.allocatable;
6722 pointer = CLASS_DATA (sym)->attr.class_pointer;
6724 else
6726 allocatable = sym->attr.allocatable;
6727 pointer = sym->attr.pointer;
6729 for (ref = e->ref; ref; ref = ref->next)
6731 switch (ref->type)
6733 case REF_ARRAY:
6734 if (ref->u.ar.type != AR_FULL
6735 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6736 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6737 allocatable = 0;
6738 break;
6740 case REF_COMPONENT:
6741 c = ref->u.c.component;
6742 if (c->ts.type == BT_CLASS)
6744 allocatable = CLASS_DATA (c)->attr.allocatable;
6745 pointer = CLASS_DATA (c)->attr.class_pointer;
6747 else
6749 allocatable = c->attr.allocatable;
6750 pointer = c->attr.pointer;
6752 break;
6754 case REF_SUBSTRING:
6755 allocatable = 0;
6756 break;
6760 attr = gfc_expr_attr (e);
6762 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
6764 bad:
6765 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6766 &e->where);
6767 return false;
6770 /* F2008, C644. */
6771 if (gfc_is_coindexed (e))
6773 gfc_error ("Coindexed allocatable object at %L", &e->where);
6774 return false;
6777 if (pointer
6778 && !gfc_check_vardef_context (e, true, true, false,
6779 _("DEALLOCATE object")))
6780 return false;
6781 if (!gfc_check_vardef_context (e, false, true, false,
6782 _("DEALLOCATE object")))
6783 return false;
6785 return true;
6789 /* Returns true if the expression e contains a reference to the symbol sym. */
6790 static bool
6791 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6793 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6794 return true;
6796 return false;
6799 bool
6800 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6802 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6806 /* Given the expression node e for an allocatable/pointer of derived type to be
6807 allocated, get the expression node to be initialized afterwards (needed for
6808 derived types with default initializers, and derived types with allocatable
6809 components that need nullification.) */
6811 gfc_expr *
6812 gfc_expr_to_initialize (gfc_expr *e)
6814 gfc_expr *result;
6815 gfc_ref *ref;
6816 int i;
6818 result = gfc_copy_expr (e);
6820 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6821 for (ref = result->ref; ref; ref = ref->next)
6822 if (ref->type == REF_ARRAY && ref->next == NULL)
6824 ref->u.ar.type = AR_FULL;
6826 for (i = 0; i < ref->u.ar.dimen; i++)
6827 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6829 break;
6832 gfc_free_shape (&result->shape, result->rank);
6834 /* Recalculate rank, shape, etc. */
6835 gfc_resolve_expr (result);
6836 return result;
6840 /* If the last ref of an expression is an array ref, return a copy of the
6841 expression with that one removed. Otherwise, a copy of the original
6842 expression. This is used for allocate-expressions and pointer assignment
6843 LHS, where there may be an array specification that needs to be stripped
6844 off when using gfc_check_vardef_context. */
6846 static gfc_expr*
6847 remove_last_array_ref (gfc_expr* e)
6849 gfc_expr* e2;
6850 gfc_ref** r;
6852 e2 = gfc_copy_expr (e);
6853 for (r = &e2->ref; *r; r = &(*r)->next)
6854 if ((*r)->type == REF_ARRAY && !(*r)->next)
6856 gfc_free_ref_list (*r);
6857 *r = NULL;
6858 break;
6861 return e2;
6865 /* Used in resolve_allocate_expr to check that a allocation-object and
6866 a source-expr are conformable. This does not catch all possible
6867 cases; in particular a runtime checking is needed. */
6869 static bool
6870 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6872 gfc_ref *tail;
6873 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6875 /* First compare rank. */
6876 if ((tail && e1->rank != tail->u.ar.as->rank)
6877 || (!tail && e1->rank != e2->rank))
6879 gfc_error ("Source-expr at %L must be scalar or have the "
6880 "same rank as the allocate-object at %L",
6881 &e1->where, &e2->where);
6882 return false;
6885 if (e1->shape)
6887 int i;
6888 mpz_t s;
6890 mpz_init (s);
6892 for (i = 0; i < e1->rank; i++)
6894 if (tail->u.ar.start[i] == NULL)
6895 break;
6897 if (tail->u.ar.end[i])
6899 mpz_set (s, tail->u.ar.end[i]->value.integer);
6900 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6901 mpz_add_ui (s, s, 1);
6903 else
6905 mpz_set (s, tail->u.ar.start[i]->value.integer);
6908 if (mpz_cmp (e1->shape[i], s) != 0)
6910 gfc_error ("Source-expr at %L and allocate-object at %L must "
6911 "have the same shape", &e1->where, &e2->where);
6912 mpz_clear (s);
6913 return false;
6917 mpz_clear (s);
6920 return true;
6924 /* Resolve the expression in an ALLOCATE statement, doing the additional
6925 checks to see whether the expression is OK or not. The expression must
6926 have a trailing array reference that gives the size of the array. */
6928 static bool
6929 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
6931 int i, pointer, allocatable, dimension, is_abstract;
6932 int codimension;
6933 bool coindexed;
6934 bool unlimited;
6935 symbol_attribute attr;
6936 gfc_ref *ref, *ref2;
6937 gfc_expr *e2;
6938 gfc_array_ref *ar;
6939 gfc_symbol *sym = NULL;
6940 gfc_alloc *a;
6941 gfc_component *c;
6942 bool t;
6944 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
6945 checking of coarrays. */
6946 for (ref = e->ref; ref; ref = ref->next)
6947 if (ref->next == NULL)
6948 break;
6950 if (ref && ref->type == REF_ARRAY)
6951 ref->u.ar.in_allocate = true;
6953 if (!gfc_resolve_expr (e))
6954 goto failure;
6956 /* Make sure the expression is allocatable or a pointer. If it is
6957 pointer, the next-to-last reference must be a pointer. */
6959 ref2 = NULL;
6960 if (e->symtree)
6961 sym = e->symtree->n.sym;
6963 /* Check whether ultimate component is abstract and CLASS. */
6964 is_abstract = 0;
6966 /* Is the allocate-object unlimited polymorphic? */
6967 unlimited = UNLIMITED_POLY(e);
6969 if (e->expr_type != EXPR_VARIABLE)
6971 allocatable = 0;
6972 attr = gfc_expr_attr (e);
6973 pointer = attr.pointer;
6974 dimension = attr.dimension;
6975 codimension = attr.codimension;
6977 else
6979 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
6981 allocatable = CLASS_DATA (sym)->attr.allocatable;
6982 pointer = CLASS_DATA (sym)->attr.class_pointer;
6983 dimension = CLASS_DATA (sym)->attr.dimension;
6984 codimension = CLASS_DATA (sym)->attr.codimension;
6985 is_abstract = CLASS_DATA (sym)->attr.abstract;
6987 else
6989 allocatable = sym->attr.allocatable;
6990 pointer = sym->attr.pointer;
6991 dimension = sym->attr.dimension;
6992 codimension = sym->attr.codimension;
6995 coindexed = false;
6997 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6999 switch (ref->type)
7001 case REF_ARRAY:
7002 if (ref->u.ar.codimen > 0)
7004 int n;
7005 for (n = ref->u.ar.dimen;
7006 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
7007 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
7009 coindexed = true;
7010 break;
7014 if (ref->next != NULL)
7015 pointer = 0;
7016 break;
7018 case REF_COMPONENT:
7019 /* F2008, C644. */
7020 if (coindexed)
7022 gfc_error ("Coindexed allocatable object at %L",
7023 &e->where);
7024 goto failure;
7027 c = ref->u.c.component;
7028 if (c->ts.type == BT_CLASS)
7030 allocatable = CLASS_DATA (c)->attr.allocatable;
7031 pointer = CLASS_DATA (c)->attr.class_pointer;
7032 dimension = CLASS_DATA (c)->attr.dimension;
7033 codimension = CLASS_DATA (c)->attr.codimension;
7034 is_abstract = CLASS_DATA (c)->attr.abstract;
7036 else
7038 allocatable = c->attr.allocatable;
7039 pointer = c->attr.pointer;
7040 dimension = c->attr.dimension;
7041 codimension = c->attr.codimension;
7042 is_abstract = c->attr.abstract;
7044 break;
7046 case REF_SUBSTRING:
7047 allocatable = 0;
7048 pointer = 0;
7049 break;
7054 /* Check for F08:C628. */
7055 if (allocatable == 0 && pointer == 0 && !unlimited)
7057 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7058 &e->where);
7059 goto failure;
7062 /* Some checks for the SOURCE tag. */
7063 if (code->expr3)
7065 /* Check F03:C631. */
7066 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7068 gfc_error ("Type of entity at %L is type incompatible with "
7069 "source-expr at %L", &e->where, &code->expr3->where);
7070 goto failure;
7073 /* Check F03:C632 and restriction following Note 6.18. */
7074 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
7075 goto failure;
7077 /* Check F03:C633. */
7078 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
7080 gfc_error ("The allocate-object at %L and the source-expr at %L "
7081 "shall have the same kind type parameter",
7082 &e->where, &code->expr3->where);
7083 goto failure;
7086 /* Check F2008, C642. */
7087 if (code->expr3->ts.type == BT_DERIVED
7088 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7089 || (code->expr3->ts.u.derived->from_intmod
7090 == INTMOD_ISO_FORTRAN_ENV
7091 && code->expr3->ts.u.derived->intmod_sym_id
7092 == ISOFORTRAN_LOCK_TYPE)))
7094 gfc_error ("The source-expr at %L shall neither be of type "
7095 "LOCK_TYPE nor have a LOCK_TYPE component if "
7096 "allocate-object at %L is a coarray",
7097 &code->expr3->where, &e->where);
7098 goto failure;
7101 /* Check TS18508, C702/C703. */
7102 if (code->expr3->ts.type == BT_DERIVED
7103 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
7104 || (code->expr3->ts.u.derived->from_intmod
7105 == INTMOD_ISO_FORTRAN_ENV
7106 && code->expr3->ts.u.derived->intmod_sym_id
7107 == ISOFORTRAN_EVENT_TYPE)))
7109 gfc_error ("The source-expr at %L shall neither be of type "
7110 "EVENT_TYPE nor have a EVENT_TYPE component if "
7111 "allocate-object at %L is a coarray",
7112 &code->expr3->where, &e->where);
7113 goto failure;
7117 /* Check F08:C629. */
7118 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7119 && !code->expr3)
7121 gcc_assert (e->ts.type == BT_CLASS);
7122 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7123 "type-spec or source-expr", sym->name, &e->where);
7124 goto failure;
7127 /* Check F08:C632. */
7128 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
7129 && !UNLIMITED_POLY (e))
7131 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7132 code->ext.alloc.ts.u.cl->length);
7133 if (cmp == 1 || cmp == -1 || cmp == -3)
7135 gfc_error ("Allocating %s at %L with type-spec requires the same "
7136 "character-length parameter as in the declaration",
7137 sym->name, &e->where);
7138 goto failure;
7142 /* In the variable definition context checks, gfc_expr_attr is used
7143 on the expression. This is fooled by the array specification
7144 present in e, thus we have to eliminate that one temporarily. */
7145 e2 = remove_last_array_ref (e);
7146 t = true;
7147 if (t && pointer)
7148 t = gfc_check_vardef_context (e2, true, true, false,
7149 _("ALLOCATE object"));
7150 if (t)
7151 t = gfc_check_vardef_context (e2, false, true, false,
7152 _("ALLOCATE object"));
7153 gfc_free_expr (e2);
7154 if (!t)
7155 goto failure;
7157 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7158 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7160 /* For class arrays, the initialization with SOURCE is done
7161 using _copy and trans_call. It is convenient to exploit that
7162 when the allocated type is different from the declared type but
7163 no SOURCE exists by setting expr3. */
7164 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
7166 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
7167 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
7168 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
7170 /* We have to zero initialize the integer variable. */
7171 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
7173 else if (!code->expr3)
7175 /* Set up default initializer if needed. */
7176 gfc_typespec ts;
7177 gfc_expr *init_e;
7179 if (gfc_bt_struct (code->ext.alloc.ts.type))
7180 ts = code->ext.alloc.ts;
7181 else
7182 ts = e->ts;
7184 if (ts.type == BT_CLASS)
7185 ts = ts.u.derived->components->ts;
7187 if (gfc_bt_struct (ts.type) && (init_e = gfc_default_initializer (&ts)))
7189 gfc_code *init_st = gfc_get_code (EXEC_INIT_ASSIGN);
7190 init_st->loc = code->loc;
7191 init_st->expr1 = gfc_expr_to_initialize (e);
7192 init_st->expr2 = init_e;
7193 init_st->next = code->next;
7194 code->next = init_st;
7197 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
7199 /* Default initialization via MOLD (non-polymorphic). */
7200 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
7201 if (rhs != NULL)
7203 gfc_resolve_expr (rhs);
7204 gfc_free_expr (code->expr3);
7205 code->expr3 = rhs;
7209 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
7211 /* Make sure the vtab symbol is present when
7212 the module variables are generated. */
7213 gfc_typespec ts = e->ts;
7214 if (code->expr3)
7215 ts = code->expr3->ts;
7216 else if (code->ext.alloc.ts.type == BT_DERIVED)
7217 ts = code->ext.alloc.ts;
7219 gfc_find_derived_vtab (ts.u.derived);
7221 if (dimension)
7222 e = gfc_expr_to_initialize (e);
7224 else if (unlimited && !UNLIMITED_POLY (code->expr3))
7226 /* Again, make sure the vtab symbol is present when
7227 the module variables are generated. */
7228 gfc_typespec *ts = NULL;
7229 if (code->expr3)
7230 ts = &code->expr3->ts;
7231 else
7232 ts = &code->ext.alloc.ts;
7234 gcc_assert (ts);
7236 gfc_find_vtab (ts);
7238 if (dimension)
7239 e = gfc_expr_to_initialize (e);
7242 if (dimension == 0 && codimension == 0)
7243 goto success;
7245 /* Make sure the last reference node is an array specification. */
7247 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
7248 || (dimension && ref2->u.ar.dimen == 0))
7250 /* F08:C633. */
7251 if (code->expr3)
7253 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
7254 "in ALLOCATE statement at %L", &e->where))
7255 goto failure;
7256 if (code->expr3->rank != 0)
7257 *array_alloc_wo_spec = true;
7258 else
7260 gfc_error ("Array specification or array-valued SOURCE= "
7261 "expression required in ALLOCATE statement at %L",
7262 &e->where);
7263 goto failure;
7266 else
7268 gfc_error ("Array specification required in ALLOCATE statement "
7269 "at %L", &e->where);
7270 goto failure;
7274 /* Make sure that the array section reference makes sense in the
7275 context of an ALLOCATE specification. */
7277 ar = &ref2->u.ar;
7279 if (codimension)
7280 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
7281 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
7283 gfc_error ("Coarray specification required in ALLOCATE statement "
7284 "at %L", &e->where);
7285 goto failure;
7288 for (i = 0; i < ar->dimen; i++)
7290 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
7291 goto check_symbols;
7293 switch (ar->dimen_type[i])
7295 case DIMEN_ELEMENT:
7296 break;
7298 case DIMEN_RANGE:
7299 if (ar->start[i] != NULL
7300 && ar->end[i] != NULL
7301 && ar->stride[i] == NULL)
7302 break;
7304 /* Fall Through... */
7306 case DIMEN_UNKNOWN:
7307 case DIMEN_VECTOR:
7308 case DIMEN_STAR:
7309 case DIMEN_THIS_IMAGE:
7310 gfc_error ("Bad array specification in ALLOCATE statement at %L",
7311 &e->where);
7312 goto failure;
7315 check_symbols:
7316 for (a = code->ext.alloc.list; a; a = a->next)
7318 sym = a->expr->symtree->n.sym;
7320 /* TODO - check derived type components. */
7321 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
7322 continue;
7324 if ((ar->start[i] != NULL
7325 && gfc_find_sym_in_expr (sym, ar->start[i]))
7326 || (ar->end[i] != NULL
7327 && gfc_find_sym_in_expr (sym, ar->end[i])))
7329 gfc_error ("%qs must not appear in the array specification at "
7330 "%L in the same ALLOCATE statement where it is "
7331 "itself allocated", sym->name, &ar->where);
7332 goto failure;
7337 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7339 if (ar->dimen_type[i] == DIMEN_ELEMENT
7340 || ar->dimen_type[i] == DIMEN_RANGE)
7342 if (i == (ar->dimen + ar->codimen - 1))
7344 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7345 "statement at %L", &e->where);
7346 goto failure;
7348 continue;
7351 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7352 && ar->stride[i] == NULL)
7353 break;
7355 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7356 &e->where);
7357 goto failure;
7360 success:
7361 return true;
7363 failure:
7364 return false;
7368 static void
7369 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7371 gfc_expr *stat, *errmsg, *pe, *qe;
7372 gfc_alloc *a, *p, *q;
7374 stat = code->expr1;
7375 errmsg = code->expr2;
7377 /* Check the stat variable. */
7378 if (stat)
7380 gfc_check_vardef_context (stat, false, false, false,
7381 _("STAT variable"));
7383 if ((stat->ts.type != BT_INTEGER
7384 && !(stat->ref && (stat->ref->type == REF_ARRAY
7385 || stat->ref->type == REF_COMPONENT)))
7386 || stat->rank > 0)
7387 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7388 "variable", &stat->where);
7390 for (p = code->ext.alloc.list; p; p = p->next)
7391 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7393 gfc_ref *ref1, *ref2;
7394 bool found = true;
7396 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7397 ref1 = ref1->next, ref2 = ref2->next)
7399 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7400 continue;
7401 if (ref1->u.c.component->name != ref2->u.c.component->name)
7403 found = false;
7404 break;
7408 if (found)
7410 gfc_error ("Stat-variable at %L shall not be %sd within "
7411 "the same %s statement", &stat->where, fcn, fcn);
7412 break;
7417 /* Check the errmsg variable. */
7418 if (errmsg)
7420 if (!stat)
7421 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
7422 &errmsg->where);
7424 gfc_check_vardef_context (errmsg, false, false, false,
7425 _("ERRMSG variable"));
7427 if ((errmsg->ts.type != BT_CHARACTER
7428 && !(errmsg->ref
7429 && (errmsg->ref->type == REF_ARRAY
7430 || errmsg->ref->type == REF_COMPONENT)))
7431 || errmsg->rank > 0 )
7432 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7433 "variable", &errmsg->where);
7435 for (p = code->ext.alloc.list; p; p = p->next)
7436 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7438 gfc_ref *ref1, *ref2;
7439 bool found = true;
7441 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7442 ref1 = ref1->next, ref2 = ref2->next)
7444 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7445 continue;
7446 if (ref1->u.c.component->name != ref2->u.c.component->name)
7448 found = false;
7449 break;
7453 if (found)
7455 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7456 "the same %s statement", &errmsg->where, fcn, fcn);
7457 break;
7462 /* Check that an allocate-object appears only once in the statement. */
7464 for (p = code->ext.alloc.list; p; p = p->next)
7466 pe = p->expr;
7467 for (q = p->next; q; q = q->next)
7469 qe = q->expr;
7470 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7472 /* This is a potential collision. */
7473 gfc_ref *pr = pe->ref;
7474 gfc_ref *qr = qe->ref;
7476 /* Follow the references until
7477 a) They start to differ, in which case there is no error;
7478 you can deallocate a%b and a%c in a single statement
7479 b) Both of them stop, which is an error
7480 c) One of them stops, which is also an error. */
7481 while (1)
7483 if (pr == NULL && qr == NULL)
7485 gfc_error ("Allocate-object at %L also appears at %L",
7486 &pe->where, &qe->where);
7487 break;
7489 else if (pr != NULL && qr == NULL)
7491 gfc_error ("Allocate-object at %L is subobject of"
7492 " object at %L", &pe->where, &qe->where);
7493 break;
7495 else if (pr == NULL && qr != NULL)
7497 gfc_error ("Allocate-object at %L is subobject of"
7498 " object at %L", &qe->where, &pe->where);
7499 break;
7501 /* Here, pr != NULL && qr != NULL */
7502 gcc_assert(pr->type == qr->type);
7503 if (pr->type == REF_ARRAY)
7505 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7506 which are legal. */
7507 gcc_assert (qr->type == REF_ARRAY);
7509 if (pr->next && qr->next)
7511 int i;
7512 gfc_array_ref *par = &(pr->u.ar);
7513 gfc_array_ref *qar = &(qr->u.ar);
7515 for (i=0; i<par->dimen; i++)
7517 if ((par->start[i] != NULL
7518 || qar->start[i] != NULL)
7519 && gfc_dep_compare_expr (par->start[i],
7520 qar->start[i]) != 0)
7521 goto break_label;
7525 else
7527 if (pr->u.c.component->name != qr->u.c.component->name)
7528 break;
7531 pr = pr->next;
7532 qr = qr->next;
7534 break_label:
7540 if (strcmp (fcn, "ALLOCATE") == 0)
7542 bool arr_alloc_wo_spec = false;
7543 for (a = code->ext.alloc.list; a; a = a->next)
7544 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
7546 if (arr_alloc_wo_spec && code->expr3)
7548 /* Mark the allocate to have to take the array specification
7549 from the expr3. */
7550 code->ext.alloc.arr_spec_from_expr3 = 1;
7553 else
7555 for (a = code->ext.alloc.list; a; a = a->next)
7556 resolve_deallocate_expr (a->expr);
7561 /************ SELECT CASE resolution subroutines ************/
7563 /* Callback function for our mergesort variant. Determines interval
7564 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7565 op1 > op2. Assumes we're not dealing with the default case.
7566 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7567 There are nine situations to check. */
7569 static int
7570 compare_cases (const gfc_case *op1, const gfc_case *op2)
7572 int retval;
7574 if (op1->low == NULL) /* op1 = (:L) */
7576 /* op2 = (:N), so overlap. */
7577 retval = 0;
7578 /* op2 = (M:) or (M:N), L < M */
7579 if (op2->low != NULL
7580 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7581 retval = -1;
7583 else if (op1->high == NULL) /* op1 = (K:) */
7585 /* op2 = (M:), so overlap. */
7586 retval = 0;
7587 /* op2 = (:N) or (M:N), K > N */
7588 if (op2->high != NULL
7589 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7590 retval = 1;
7592 else /* op1 = (K:L) */
7594 if (op2->low == NULL) /* op2 = (:N), K > N */
7595 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7596 ? 1 : 0;
7597 else if (op2->high == NULL) /* op2 = (M:), L < M */
7598 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7599 ? -1 : 0;
7600 else /* op2 = (M:N) */
7602 retval = 0;
7603 /* L < M */
7604 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7605 retval = -1;
7606 /* K > N */
7607 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7608 retval = 1;
7612 return retval;
7616 /* Merge-sort a double linked case list, detecting overlap in the
7617 process. LIST is the head of the double linked case list before it
7618 is sorted. Returns the head of the sorted list if we don't see any
7619 overlap, or NULL otherwise. */
7621 static gfc_case *
7622 check_case_overlap (gfc_case *list)
7624 gfc_case *p, *q, *e, *tail;
7625 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7627 /* If the passed list was empty, return immediately. */
7628 if (!list)
7629 return NULL;
7631 overlap_seen = 0;
7632 insize = 1;
7634 /* Loop unconditionally. The only exit from this loop is a return
7635 statement, when we've finished sorting the case list. */
7636 for (;;)
7638 p = list;
7639 list = NULL;
7640 tail = NULL;
7642 /* Count the number of merges we do in this pass. */
7643 nmerges = 0;
7645 /* Loop while there exists a merge to be done. */
7646 while (p)
7648 int i;
7650 /* Count this merge. */
7651 nmerges++;
7653 /* Cut the list in two pieces by stepping INSIZE places
7654 forward in the list, starting from P. */
7655 psize = 0;
7656 q = p;
7657 for (i = 0; i < insize; i++)
7659 psize++;
7660 q = q->right;
7661 if (!q)
7662 break;
7664 qsize = insize;
7666 /* Now we have two lists. Merge them! */
7667 while (psize > 0 || (qsize > 0 && q != NULL))
7669 /* See from which the next case to merge comes from. */
7670 if (psize == 0)
7672 /* P is empty so the next case must come from Q. */
7673 e = q;
7674 q = q->right;
7675 qsize--;
7677 else if (qsize == 0 || q == NULL)
7679 /* Q is empty. */
7680 e = p;
7681 p = p->right;
7682 psize--;
7684 else
7686 cmp = compare_cases (p, q);
7687 if (cmp < 0)
7689 /* The whole case range for P is less than the
7690 one for Q. */
7691 e = p;
7692 p = p->right;
7693 psize--;
7695 else if (cmp > 0)
7697 /* The whole case range for Q is greater than
7698 the case range for P. */
7699 e = q;
7700 q = q->right;
7701 qsize--;
7703 else
7705 /* The cases overlap, or they are the same
7706 element in the list. Either way, we must
7707 issue an error and get the next case from P. */
7708 /* FIXME: Sort P and Q by line number. */
7709 gfc_error ("CASE label at %L overlaps with CASE "
7710 "label at %L", &p->where, &q->where);
7711 overlap_seen = 1;
7712 e = p;
7713 p = p->right;
7714 psize--;
7718 /* Add the next element to the merged list. */
7719 if (tail)
7720 tail->right = e;
7721 else
7722 list = e;
7723 e->left = tail;
7724 tail = e;
7727 /* P has now stepped INSIZE places along, and so has Q. So
7728 they're the same. */
7729 p = q;
7731 tail->right = NULL;
7733 /* If we have done only one merge or none at all, we've
7734 finished sorting the cases. */
7735 if (nmerges <= 1)
7737 if (!overlap_seen)
7738 return list;
7739 else
7740 return NULL;
7743 /* Otherwise repeat, merging lists twice the size. */
7744 insize *= 2;
7749 /* Check to see if an expression is suitable for use in a CASE statement.
7750 Makes sure that all case expressions are scalar constants of the same
7751 type. Return false if anything is wrong. */
7753 static bool
7754 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7756 if (e == NULL) return true;
7758 if (e->ts.type != case_expr->ts.type)
7760 gfc_error ("Expression in CASE statement at %L must be of type %s",
7761 &e->where, gfc_basic_typename (case_expr->ts.type));
7762 return false;
7765 /* C805 (R808) For a given case-construct, each case-value shall be of
7766 the same type as case-expr. For character type, length differences
7767 are allowed, but the kind type parameters shall be the same. */
7769 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7771 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7772 &e->where, case_expr->ts.kind);
7773 return false;
7776 /* Convert the case value kind to that of case expression kind,
7777 if needed */
7779 if (e->ts.kind != case_expr->ts.kind)
7780 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7782 if (e->rank != 0)
7784 gfc_error ("Expression in CASE statement at %L must be scalar",
7785 &e->where);
7786 return false;
7789 return true;
7793 /* Given a completely parsed select statement, we:
7795 - Validate all expressions and code within the SELECT.
7796 - Make sure that the selection expression is not of the wrong type.
7797 - Make sure that no case ranges overlap.
7798 - Eliminate unreachable cases and unreachable code resulting from
7799 removing case labels.
7801 The standard does allow unreachable cases, e.g. CASE (5:3). But
7802 they are a hassle for code generation, and to prevent that, we just
7803 cut them out here. This is not necessary for overlapping cases
7804 because they are illegal and we never even try to generate code.
7806 We have the additional caveat that a SELECT construct could have
7807 been a computed GOTO in the source code. Fortunately we can fairly
7808 easily work around that here: The case_expr for a "real" SELECT CASE
7809 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7810 we have to do is make sure that the case_expr is a scalar integer
7811 expression. */
7813 static void
7814 resolve_select (gfc_code *code, bool select_type)
7816 gfc_code *body;
7817 gfc_expr *case_expr;
7818 gfc_case *cp, *default_case, *tail, *head;
7819 int seen_unreachable;
7820 int seen_logical;
7821 int ncases;
7822 bt type;
7823 bool t;
7825 if (code->expr1 == NULL)
7827 /* This was actually a computed GOTO statement. */
7828 case_expr = code->expr2;
7829 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7830 gfc_error ("Selection expression in computed GOTO statement "
7831 "at %L must be a scalar integer expression",
7832 &case_expr->where);
7834 /* Further checking is not necessary because this SELECT was built
7835 by the compiler, so it should always be OK. Just move the
7836 case_expr from expr2 to expr so that we can handle computed
7837 GOTOs as normal SELECTs from here on. */
7838 code->expr1 = code->expr2;
7839 code->expr2 = NULL;
7840 return;
7843 case_expr = code->expr1;
7844 type = case_expr->ts.type;
7846 /* F08:C830. */
7847 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7849 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7850 &case_expr->where, gfc_typename (&case_expr->ts));
7852 /* Punt. Going on here just produce more garbage error messages. */
7853 return;
7856 /* F08:R842. */
7857 if (!select_type && case_expr->rank != 0)
7859 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7860 "expression", &case_expr->where);
7862 /* Punt. */
7863 return;
7866 /* Raise a warning if an INTEGER case value exceeds the range of
7867 the case-expr. Later, all expressions will be promoted to the
7868 largest kind of all case-labels. */
7870 if (type == BT_INTEGER)
7871 for (body = code->block; body; body = body->block)
7872 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7874 if (cp->low
7875 && gfc_check_integer_range (cp->low->value.integer,
7876 case_expr->ts.kind) != ARITH_OK)
7877 gfc_warning (0, "Expression in CASE statement at %L is "
7878 "not in the range of %s", &cp->low->where,
7879 gfc_typename (&case_expr->ts));
7881 if (cp->high
7882 && cp->low != cp->high
7883 && gfc_check_integer_range (cp->high->value.integer,
7884 case_expr->ts.kind) != ARITH_OK)
7885 gfc_warning (0, "Expression in CASE statement at %L is "
7886 "not in the range of %s", &cp->high->where,
7887 gfc_typename (&case_expr->ts));
7890 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7891 of the SELECT CASE expression and its CASE values. Walk the lists
7892 of case values, and if we find a mismatch, promote case_expr to
7893 the appropriate kind. */
7895 if (type == BT_LOGICAL || type == BT_INTEGER)
7897 for (body = code->block; body; body = body->block)
7899 /* Walk the case label list. */
7900 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7902 /* Intercept the DEFAULT case. It does not have a kind. */
7903 if (cp->low == NULL && cp->high == NULL)
7904 continue;
7906 /* Unreachable case ranges are discarded, so ignore. */
7907 if (cp->low != NULL && cp->high != NULL
7908 && cp->low != cp->high
7909 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7910 continue;
7912 if (cp->low != NULL
7913 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7914 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7916 if (cp->high != NULL
7917 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7918 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7923 /* Assume there is no DEFAULT case. */
7924 default_case = NULL;
7925 head = tail = NULL;
7926 ncases = 0;
7927 seen_logical = 0;
7929 for (body = code->block; body; body = body->block)
7931 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7932 t = true;
7933 seen_unreachable = 0;
7935 /* Walk the case label list, making sure that all case labels
7936 are legal. */
7937 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7939 /* Count the number of cases in the whole construct. */
7940 ncases++;
7942 /* Intercept the DEFAULT case. */
7943 if (cp->low == NULL && cp->high == NULL)
7945 if (default_case != NULL)
7947 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7948 "by a second DEFAULT CASE at %L",
7949 &default_case->where, &cp->where);
7950 t = false;
7951 break;
7953 else
7955 default_case = cp;
7956 continue;
7960 /* Deal with single value cases and case ranges. Errors are
7961 issued from the validation function. */
7962 if (!validate_case_label_expr (cp->low, case_expr)
7963 || !validate_case_label_expr (cp->high, case_expr))
7965 t = false;
7966 break;
7969 if (type == BT_LOGICAL
7970 && ((cp->low == NULL || cp->high == NULL)
7971 || cp->low != cp->high))
7973 gfc_error ("Logical range in CASE statement at %L is not "
7974 "allowed", &cp->low->where);
7975 t = false;
7976 break;
7979 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7981 int value;
7982 value = cp->low->value.logical == 0 ? 2 : 1;
7983 if (value & seen_logical)
7985 gfc_error ("Constant logical value in CASE statement "
7986 "is repeated at %L",
7987 &cp->low->where);
7988 t = false;
7989 break;
7991 seen_logical |= value;
7994 if (cp->low != NULL && cp->high != NULL
7995 && cp->low != cp->high
7996 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7998 if (warn_surprising)
7999 gfc_warning (OPT_Wsurprising,
8000 "Range specification at %L can never be matched",
8001 &cp->where);
8003 cp->unreachable = 1;
8004 seen_unreachable = 1;
8006 else
8008 /* If the case range can be matched, it can also overlap with
8009 other cases. To make sure it does not, we put it in a
8010 double linked list here. We sort that with a merge sort
8011 later on to detect any overlapping cases. */
8012 if (!head)
8014 head = tail = cp;
8015 head->right = head->left = NULL;
8017 else
8019 tail->right = cp;
8020 tail->right->left = tail;
8021 tail = tail->right;
8022 tail->right = NULL;
8027 /* It there was a failure in the previous case label, give up
8028 for this case label list. Continue with the next block. */
8029 if (!t)
8030 continue;
8032 /* See if any case labels that are unreachable have been seen.
8033 If so, we eliminate them. This is a bit of a kludge because
8034 the case lists for a single case statement (label) is a
8035 single forward linked lists. */
8036 if (seen_unreachable)
8038 /* Advance until the first case in the list is reachable. */
8039 while (body->ext.block.case_list != NULL
8040 && body->ext.block.case_list->unreachable)
8042 gfc_case *n = body->ext.block.case_list;
8043 body->ext.block.case_list = body->ext.block.case_list->next;
8044 n->next = NULL;
8045 gfc_free_case_list (n);
8048 /* Strip all other unreachable cases. */
8049 if (body->ext.block.case_list)
8051 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
8053 if (cp->next->unreachable)
8055 gfc_case *n = cp->next;
8056 cp->next = cp->next->next;
8057 n->next = NULL;
8058 gfc_free_case_list (n);
8065 /* See if there were overlapping cases. If the check returns NULL,
8066 there was overlap. In that case we don't do anything. If head
8067 is non-NULL, we prepend the DEFAULT case. The sorted list can
8068 then used during code generation for SELECT CASE constructs with
8069 a case expression of a CHARACTER type. */
8070 if (head)
8072 head = check_case_overlap (head);
8074 /* Prepend the default_case if it is there. */
8075 if (head != NULL && default_case)
8077 default_case->left = NULL;
8078 default_case->right = head;
8079 head->left = default_case;
8083 /* Eliminate dead blocks that may be the result if we've seen
8084 unreachable case labels for a block. */
8085 for (body = code; body && body->block; body = body->block)
8087 if (body->block->ext.block.case_list == NULL)
8089 /* Cut the unreachable block from the code chain. */
8090 gfc_code *c = body->block;
8091 body->block = c->block;
8093 /* Kill the dead block, but not the blocks below it. */
8094 c->block = NULL;
8095 gfc_free_statements (c);
8099 /* More than two cases is legal but insane for logical selects.
8100 Issue a warning for it. */
8101 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
8102 gfc_warning (OPT_Wsurprising,
8103 "Logical SELECT CASE block at %L has more that two cases",
8104 &code->loc);
8108 /* Check if a derived type is extensible. */
8110 bool
8111 gfc_type_is_extensible (gfc_symbol *sym)
8113 return !(sym->attr.is_bind_c || sym->attr.sequence
8114 || (sym->attr.is_class
8115 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
8119 static void
8120 resolve_types (gfc_namespace *ns);
8122 /* Resolve an associate-name: Resolve target and ensure the type-spec is
8123 correct as well as possibly the array-spec. */
8125 static void
8126 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
8128 gfc_expr* target;
8130 gcc_assert (sym->assoc);
8131 gcc_assert (sym->attr.flavor == FL_VARIABLE);
8133 /* If this is for SELECT TYPE, the target may not yet be set. In that
8134 case, return. Resolution will be called later manually again when
8135 this is done. */
8136 target = sym->assoc->target;
8137 if (!target)
8138 return;
8139 gcc_assert (!sym->assoc->dangling);
8141 if (resolve_target && !gfc_resolve_expr (target))
8142 return;
8144 /* For variable targets, we get some attributes from the target. */
8145 if (target->expr_type == EXPR_VARIABLE)
8147 gfc_symbol* tsym;
8149 gcc_assert (target->symtree);
8150 tsym = target->symtree->n.sym;
8152 sym->attr.asynchronous = tsym->attr.asynchronous;
8153 sym->attr.volatile_ = tsym->attr.volatile_;
8155 sym->attr.target = tsym->attr.target
8156 || gfc_expr_attr (target).pointer;
8157 if (is_subref_array (target))
8158 sym->attr.subref_array_pointer = 1;
8161 /* Get type if this was not already set. Note that it can be
8162 some other type than the target in case this is a SELECT TYPE
8163 selector! So we must not update when the type is already there. */
8164 if (sym->ts.type == BT_UNKNOWN)
8165 sym->ts = target->ts;
8166 gcc_assert (sym->ts.type != BT_UNKNOWN);
8168 /* See if this is a valid association-to-variable. */
8169 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
8170 && !gfc_has_vector_subscript (target));
8172 /* Finally resolve if this is an array or not. */
8173 if (sym->attr.dimension && target->rank == 0)
8175 /* primary.c makes the assumption that a reference to an associate
8176 name followed by a left parenthesis is an array reference. */
8177 if (sym->ts.type != BT_CHARACTER)
8178 gfc_error ("Associate-name %qs at %L is used as array",
8179 sym->name, &sym->declared_at);
8180 sym->attr.dimension = 0;
8181 return;
8185 /* We cannot deal with class selectors that need temporaries. */
8186 if (target->ts.type == BT_CLASS
8187 && gfc_ref_needs_temporary_p (target->ref))
8189 gfc_error ("CLASS selector at %L needs a temporary which is not "
8190 "yet implemented", &target->where);
8191 return;
8194 if (target->ts.type == BT_CLASS)
8195 gfc_fix_class_refs (target);
8197 if (target->rank != 0)
8199 gfc_array_spec *as;
8200 /* The rank may be incorrectly guessed at parsing, therefore make sure
8201 it is corrected now. */
8202 if (sym->ts.type != BT_CLASS && (!sym->as || sym->assoc->rankguessed))
8204 if (!sym->as)
8205 sym->as = gfc_get_array_spec ();
8206 as = sym->as;
8207 as->rank = target->rank;
8208 as->type = AS_DEFERRED;
8209 as->corank = gfc_get_corank (target);
8210 sym->attr.dimension = 1;
8211 if (as->corank != 0)
8212 sym->attr.codimension = 1;
8215 else
8217 /* target's rank is 0, but the type of the sym is still array valued,
8218 which has to be corrected. */
8219 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
8221 gfc_array_spec *as;
8222 symbol_attribute attr;
8223 /* The associated variable's type is still the array type
8224 correct this now. */
8225 gfc_typespec *ts = &target->ts;
8226 gfc_ref *ref;
8227 gfc_component *c;
8228 for (ref = target->ref; ref != NULL; ref = ref->next)
8230 switch (ref->type)
8232 case REF_COMPONENT:
8233 ts = &ref->u.c.component->ts;
8234 break;
8235 case REF_ARRAY:
8236 if (ts->type == BT_CLASS)
8237 ts = &ts->u.derived->components->ts;
8238 break;
8239 default:
8240 break;
8243 /* Create a scalar instance of the current class type. Because the
8244 rank of a class array goes into its name, the type has to be
8245 rebuild. The alternative of (re-)setting just the attributes
8246 and as in the current type, destroys the type also in other
8247 places. */
8248 as = NULL;
8249 sym->ts = *ts;
8250 sym->ts.type = BT_CLASS;
8251 attr = CLASS_DATA (sym)->attr;
8252 attr.class_ok = 0;
8253 attr.associate_var = 1;
8254 attr.dimension = attr.codimension = 0;
8255 attr.class_pointer = 1;
8256 if (!gfc_build_class_symbol (&sym->ts, &attr, &as))
8257 gcc_unreachable ();
8258 /* Make sure the _vptr is set. */
8259 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true, NULL);
8260 if (c->ts.u.derived == NULL)
8261 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived);
8262 CLASS_DATA (sym)->attr.pointer = 1;
8263 CLASS_DATA (sym)->attr.class_pointer = 1;
8264 gfc_set_sym_referenced (sym->ts.u.derived);
8265 gfc_commit_symbol (sym->ts.u.derived);
8266 /* _vptr now has the _vtab in it, change it to the _vtype. */
8267 if (c->ts.u.derived->attr.vtab)
8268 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
8269 c->ts.u.derived->ns->types_resolved = 0;
8270 resolve_types (c->ts.u.derived->ns);
8274 /* Mark this as an associate variable. */
8275 sym->attr.associate_var = 1;
8277 /* If the target is a good class object, so is the associate variable. */
8278 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
8279 sym->attr.class_ok = 1;
8283 /* Resolve a SELECT TYPE statement. */
8285 static void
8286 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
8288 gfc_symbol *selector_type;
8289 gfc_code *body, *new_st, *if_st, *tail;
8290 gfc_code *class_is = NULL, *default_case = NULL;
8291 gfc_case *c;
8292 gfc_symtree *st;
8293 char name[GFC_MAX_SYMBOL_LEN];
8294 gfc_namespace *ns;
8295 int error = 0;
8296 int charlen = 0;
8298 ns = code->ext.block.ns;
8299 gfc_resolve (ns);
8301 /* Check for F03:C813. */
8302 if (code->expr1->ts.type != BT_CLASS
8303 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
8305 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
8306 "at %L", &code->loc);
8307 return;
8310 if (!code->expr1->symtree->n.sym->attr.class_ok)
8311 return;
8313 if (code->expr2)
8315 if (code->expr1->symtree->n.sym->attr.untyped)
8316 code->expr1->symtree->n.sym->ts = code->expr2->ts;
8317 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
8319 /* F2008: C803 The selector expression must not be coindexed. */
8320 if (gfc_is_coindexed (code->expr2))
8322 gfc_error ("Selector at %L must not be coindexed",
8323 &code->expr2->where);
8324 return;
8328 else
8330 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
8332 if (gfc_is_coindexed (code->expr1))
8334 gfc_error ("Selector at %L must not be coindexed",
8335 &code->expr1->where);
8336 return;
8340 /* Loop over TYPE IS / CLASS IS cases. */
8341 for (body = code->block; body; body = body->block)
8343 c = body->ext.block.case_list;
8345 /* Check F03:C815. */
8346 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8347 && !selector_type->attr.unlimited_polymorphic
8348 && !gfc_type_is_extensible (c->ts.u.derived))
8350 gfc_error ("Derived type %qs at %L must be extensible",
8351 c->ts.u.derived->name, &c->where);
8352 error++;
8353 continue;
8356 /* Check F03:C816. */
8357 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
8358 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
8359 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
8361 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8362 gfc_error ("Derived type %qs at %L must be an extension of %qs",
8363 c->ts.u.derived->name, &c->where, selector_type->name);
8364 else
8365 gfc_error ("Unexpected intrinsic type %qs at %L",
8366 gfc_basic_typename (c->ts.type), &c->where);
8367 error++;
8368 continue;
8371 /* Check F03:C814. */
8372 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
8374 gfc_error ("The type-spec at %L shall specify that each length "
8375 "type parameter is assumed", &c->where);
8376 error++;
8377 continue;
8380 /* Intercept the DEFAULT case. */
8381 if (c->ts.type == BT_UNKNOWN)
8383 /* Check F03:C818. */
8384 if (default_case)
8386 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8387 "by a second DEFAULT CASE at %L",
8388 &default_case->ext.block.case_list->where, &c->where);
8389 error++;
8390 continue;
8393 default_case = body;
8397 if (error > 0)
8398 return;
8400 /* Transform SELECT TYPE statement to BLOCK and associate selector to
8401 target if present. If there are any EXIT statements referring to the
8402 SELECT TYPE construct, this is no problem because the gfc_code
8403 reference stays the same and EXIT is equally possible from the BLOCK
8404 it is changed to. */
8405 code->op = EXEC_BLOCK;
8406 if (code->expr2)
8408 gfc_association_list* assoc;
8410 assoc = gfc_get_association_list ();
8411 assoc->st = code->expr1->symtree;
8412 assoc->target = gfc_copy_expr (code->expr2);
8413 assoc->target->where = code->expr2->where;
8414 /* assoc->variable will be set by resolve_assoc_var. */
8416 code->ext.block.assoc = assoc;
8417 code->expr1->symtree->n.sym->assoc = assoc;
8419 resolve_assoc_var (code->expr1->symtree->n.sym, false);
8421 else
8422 code->ext.block.assoc = NULL;
8424 /* Add EXEC_SELECT to switch on type. */
8425 new_st = gfc_get_code (code->op);
8426 new_st->expr1 = code->expr1;
8427 new_st->expr2 = code->expr2;
8428 new_st->block = code->block;
8429 code->expr1 = code->expr2 = NULL;
8430 code->block = NULL;
8431 if (!ns->code)
8432 ns->code = new_st;
8433 else
8434 ns->code->next = new_st;
8435 code = new_st;
8436 code->op = EXEC_SELECT;
8438 gfc_add_vptr_component (code->expr1);
8439 gfc_add_hash_component (code->expr1);
8441 /* Loop over TYPE IS / CLASS IS cases. */
8442 for (body = code->block; body; body = body->block)
8444 c = body->ext.block.case_list;
8446 if (c->ts.type == BT_DERIVED)
8447 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8448 c->ts.u.derived->hash_value);
8449 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8451 gfc_symbol *ivtab;
8452 gfc_expr *e;
8454 ivtab = gfc_find_vtab (&c->ts);
8455 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
8456 e = CLASS_DATA (ivtab)->initializer;
8457 c->low = c->high = gfc_copy_expr (e);
8460 else if (c->ts.type == BT_UNKNOWN)
8461 continue;
8463 /* Associate temporary to selector. This should only be done
8464 when this case is actually true, so build a new ASSOCIATE
8465 that does precisely this here (instead of using the
8466 'global' one). */
8468 if (c->ts.type == BT_CLASS)
8469 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8470 else if (c->ts.type == BT_DERIVED)
8471 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8472 else if (c->ts.type == BT_CHARACTER)
8474 if (c->ts.u.cl && c->ts.u.cl->length
8475 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8476 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8477 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8478 charlen, c->ts.kind);
8480 else
8481 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8482 c->ts.kind);
8484 st = gfc_find_symtree (ns->sym_root, name);
8485 gcc_assert (st->n.sym->assoc);
8486 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
8487 st->n.sym->assoc->target->where = code->expr1->where;
8488 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8489 gfc_add_data_component (st->n.sym->assoc->target);
8491 new_st = gfc_get_code (EXEC_BLOCK);
8492 new_st->ext.block.ns = gfc_build_block_ns (ns);
8493 new_st->ext.block.ns->code = body->next;
8494 body->next = new_st;
8496 /* Chain in the new list only if it is marked as dangling. Otherwise
8497 there is a CASE label overlap and this is already used. Just ignore,
8498 the error is diagnosed elsewhere. */
8499 if (st->n.sym->assoc->dangling)
8501 new_st->ext.block.assoc = st->n.sym->assoc;
8502 st->n.sym->assoc->dangling = 0;
8505 resolve_assoc_var (st->n.sym, false);
8508 /* Take out CLASS IS cases for separate treatment. */
8509 body = code;
8510 while (body && body->block)
8512 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8514 /* Add to class_is list. */
8515 if (class_is == NULL)
8517 class_is = body->block;
8518 tail = class_is;
8520 else
8522 for (tail = class_is; tail->block; tail = tail->block) ;
8523 tail->block = body->block;
8524 tail = tail->block;
8526 /* Remove from EXEC_SELECT list. */
8527 body->block = body->block->block;
8528 tail->block = NULL;
8530 else
8531 body = body->block;
8534 if (class_is)
8536 gfc_symbol *vtab;
8538 if (!default_case)
8540 /* Add a default case to hold the CLASS IS cases. */
8541 for (tail = code; tail->block; tail = tail->block) ;
8542 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
8543 tail = tail->block;
8544 tail->ext.block.case_list = gfc_get_case ();
8545 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8546 tail->next = NULL;
8547 default_case = tail;
8550 /* More than one CLASS IS block? */
8551 if (class_is->block)
8553 gfc_code **c1,*c2;
8554 bool swapped;
8555 /* Sort CLASS IS blocks by extension level. */
8558 swapped = false;
8559 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8561 c2 = (*c1)->block;
8562 /* F03:C817 (check for doubles). */
8563 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8564 == c2->ext.block.case_list->ts.u.derived->hash_value)
8566 gfc_error ("Double CLASS IS block in SELECT TYPE "
8567 "statement at %L",
8568 &c2->ext.block.case_list->where);
8569 return;
8571 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8572 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8574 /* Swap. */
8575 (*c1)->block = c2->block;
8576 c2->block = *c1;
8577 *c1 = c2;
8578 swapped = true;
8582 while (swapped);
8585 /* Generate IF chain. */
8586 if_st = gfc_get_code (EXEC_IF);
8587 new_st = if_st;
8588 for (body = class_is; body; body = body->block)
8590 new_st->block = gfc_get_code (EXEC_IF);
8591 new_st = new_st->block;
8592 /* Set up IF condition: Call _gfortran_is_extension_of. */
8593 new_st->expr1 = gfc_get_expr ();
8594 new_st->expr1->expr_type = EXPR_FUNCTION;
8595 new_st->expr1->ts.type = BT_LOGICAL;
8596 new_st->expr1->ts.kind = 4;
8597 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8598 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8599 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8600 /* Set up arguments. */
8601 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8602 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8603 new_st->expr1->value.function.actual->expr->where = code->loc;
8604 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8605 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8606 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8607 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8608 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8609 new_st->next = body->next;
8611 if (default_case->next)
8613 new_st->block = gfc_get_code (EXEC_IF);
8614 new_st = new_st->block;
8615 new_st->next = default_case->next;
8618 /* Replace CLASS DEFAULT code by the IF chain. */
8619 default_case->next = if_st;
8622 /* Resolve the internal code. This can not be done earlier because
8623 it requires that the sym->assoc of selectors is set already. */
8624 gfc_current_ns = ns;
8625 gfc_resolve_blocks (code->block, gfc_current_ns);
8626 gfc_current_ns = old_ns;
8628 resolve_select (code, true);
8632 /* Resolve a transfer statement. This is making sure that:
8633 -- a derived type being transferred has only non-pointer components
8634 -- a derived type being transferred doesn't have private components, unless
8635 it's being transferred from the module where the type was defined
8636 -- we're not trying to transfer a whole assumed size array. */
8638 static void
8639 resolve_transfer (gfc_code *code)
8641 gfc_typespec *ts;
8642 gfc_symbol *sym;
8643 gfc_ref *ref;
8644 gfc_expr *exp;
8646 exp = code->expr1;
8648 while (exp != NULL && exp->expr_type == EXPR_OP
8649 && exp->value.op.op == INTRINSIC_PARENTHESES)
8650 exp = exp->value.op.op1;
8652 if (exp && exp->expr_type == EXPR_NULL
8653 && code->ext.dt)
8655 gfc_error ("Invalid context for NULL () intrinsic at %L",
8656 &exp->where);
8657 return;
8660 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8661 && exp->expr_type != EXPR_FUNCTION
8662 && exp->expr_type != EXPR_STRUCTURE))
8663 return;
8665 /* If we are reading, the variable will be changed. Note that
8666 code->ext.dt may be NULL if the TRANSFER is related to
8667 an INQUIRE statement -- but in this case, we are not reading, either. */
8668 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8669 && !gfc_check_vardef_context (exp, false, false, false,
8670 _("item in READ")))
8671 return;
8673 ts = exp->expr_type == EXPR_STRUCTURE ? &exp->ts : &exp->symtree->n.sym->ts;
8675 /* Go to actual component transferred. */
8676 for (ref = exp->ref; ref; ref = ref->next)
8677 if (ref->type == REF_COMPONENT)
8678 ts = &ref->u.c.component->ts;
8680 if (ts->type == BT_CLASS)
8682 /* FIXME: Test for defined input/output. */
8683 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8684 "it is processed by a defined input/output procedure",
8685 &code->loc);
8686 return;
8689 if (ts->type == BT_DERIVED)
8691 /* Check that transferred derived type doesn't contain POINTER
8692 components. */
8693 if (ts->u.derived->attr.pointer_comp)
8695 gfc_error ("Data transfer element at %L cannot have POINTER "
8696 "components unless it is processed by a defined "
8697 "input/output procedure", &code->loc);
8698 return;
8701 /* F08:C935. */
8702 if (ts->u.derived->attr.proc_pointer_comp)
8704 gfc_error ("Data transfer element at %L cannot have "
8705 "procedure pointer components", &code->loc);
8706 return;
8709 if (ts->u.derived->attr.alloc_comp)
8711 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8712 "components unless it is processed by a defined "
8713 "input/output procedure", &code->loc);
8714 return;
8717 /* C_PTR and C_FUNPTR have private components which means they can not
8718 be printed. However, if -std=gnu and not -pedantic, allow
8719 the component to be printed to help debugging. */
8720 if (ts->u.derived->ts.f90_type == BT_VOID)
8722 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8723 "cannot have PRIVATE components", &code->loc))
8724 return;
8726 else if (derived_inaccessible (ts->u.derived))
8728 gfc_error ("Data transfer element at %L cannot have "
8729 "PRIVATE components",&code->loc);
8730 return;
8734 if (exp->expr_type == EXPR_STRUCTURE)
8735 return;
8737 sym = exp->symtree->n.sym;
8739 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8740 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8742 gfc_error ("Data transfer element at %L cannot be a full reference to "
8743 "an assumed-size array", &code->loc);
8744 return;
8749 /*********** Toplevel code resolution subroutines ***********/
8751 /* Find the set of labels that are reachable from this block. We also
8752 record the last statement in each block. */
8754 static void
8755 find_reachable_labels (gfc_code *block)
8757 gfc_code *c;
8759 if (!block)
8760 return;
8762 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8764 /* Collect labels in this block. We don't keep those corresponding
8765 to END {IF|SELECT}, these are checked in resolve_branch by going
8766 up through the code_stack. */
8767 for (c = block; c; c = c->next)
8769 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8770 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8773 /* Merge with labels from parent block. */
8774 if (cs_base->prev)
8776 gcc_assert (cs_base->prev->reachable_labels);
8777 bitmap_ior_into (cs_base->reachable_labels,
8778 cs_base->prev->reachable_labels);
8783 static void
8784 resolve_lock_unlock_event (gfc_code *code)
8786 if (code->expr1->expr_type == EXPR_FUNCTION
8787 && code->expr1->value.function.isym
8788 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
8789 remove_caf_get_intrinsic (code->expr1);
8791 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
8792 && (code->expr1->ts.type != BT_DERIVED
8793 || code->expr1->expr_type != EXPR_VARIABLE
8794 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8795 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8796 || code->expr1->rank != 0
8797 || (!gfc_is_coarray (code->expr1) &&
8798 !gfc_is_coindexed (code->expr1))))
8799 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8800 &code->expr1->where);
8801 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
8802 && (code->expr1->ts.type != BT_DERIVED
8803 || code->expr1->expr_type != EXPR_VARIABLE
8804 || code->expr1->ts.u.derived->from_intmod
8805 != INTMOD_ISO_FORTRAN_ENV
8806 || code->expr1->ts.u.derived->intmod_sym_id
8807 != ISOFORTRAN_EVENT_TYPE
8808 || code->expr1->rank != 0))
8809 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
8810 &code->expr1->where);
8811 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
8812 && !gfc_is_coindexed (code->expr1))
8813 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
8814 &code->expr1->where);
8815 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
8816 gfc_error ("Event variable argument at %L must be a coarray but not "
8817 "coindexed", &code->expr1->where);
8819 /* Check STAT. */
8820 if (code->expr2
8821 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8822 || code->expr2->expr_type != EXPR_VARIABLE))
8823 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8824 &code->expr2->where);
8826 if (code->expr2
8827 && !gfc_check_vardef_context (code->expr2, false, false, false,
8828 _("STAT variable")))
8829 return;
8831 /* Check ERRMSG. */
8832 if (code->expr3
8833 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8834 || code->expr3->expr_type != EXPR_VARIABLE))
8835 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8836 &code->expr3->where);
8838 if (code->expr3
8839 && !gfc_check_vardef_context (code->expr3, false, false, false,
8840 _("ERRMSG variable")))
8841 return;
8843 /* Check for LOCK the ACQUIRED_LOCK. */
8844 if (code->op != EXEC_EVENT_WAIT && code->expr4
8845 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8846 || code->expr4->expr_type != EXPR_VARIABLE))
8847 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8848 "variable", &code->expr4->where);
8850 if (code->op != EXEC_EVENT_WAIT && code->expr4
8851 && !gfc_check_vardef_context (code->expr4, false, false, false,
8852 _("ACQUIRED_LOCK variable")))
8853 return;
8855 /* Check for EVENT WAIT the UNTIL_COUNT. */
8856 if (code->op == EXEC_EVENT_WAIT && code->expr4
8857 && (code->expr4->ts.type != BT_INTEGER || code->expr4->rank != 0))
8858 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
8859 "expression", &code->expr4->where);
8863 static void
8864 resolve_critical (gfc_code *code)
8866 gfc_symtree *symtree;
8867 gfc_symbol *lock_type;
8868 char name[GFC_MAX_SYMBOL_LEN];
8869 static int serial = 0;
8871 if (flag_coarray != GFC_FCOARRAY_LIB)
8872 return;
8874 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
8875 GFC_PREFIX ("lock_type"));
8876 if (symtree)
8877 lock_type = symtree->n.sym;
8878 else
8880 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
8881 false) != 0)
8882 gcc_unreachable ();
8883 lock_type = symtree->n.sym;
8884 lock_type->attr.flavor = FL_DERIVED;
8885 lock_type->attr.zero_comp = 1;
8886 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
8887 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
8890 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
8891 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
8892 gcc_unreachable ();
8894 code->resolved_sym = symtree->n.sym;
8895 symtree->n.sym->attr.flavor = FL_VARIABLE;
8896 symtree->n.sym->attr.referenced = 1;
8897 symtree->n.sym->attr.artificial = 1;
8898 symtree->n.sym->attr.codimension = 1;
8899 symtree->n.sym->ts.type = BT_DERIVED;
8900 symtree->n.sym->ts.u.derived = lock_type;
8901 symtree->n.sym->as = gfc_get_array_spec ();
8902 symtree->n.sym->as->corank = 1;
8903 symtree->n.sym->as->type = AS_EXPLICIT;
8904 symtree->n.sym->as->cotype = AS_EXPLICIT;
8905 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
8906 NULL, 1);
8907 gfc_commit_symbols();
8911 static void
8912 resolve_sync (gfc_code *code)
8914 /* Check imageset. The * case matches expr1 == NULL. */
8915 if (code->expr1)
8917 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8918 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8919 "INTEGER expression", &code->expr1->where);
8920 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8921 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8922 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8923 &code->expr1->where);
8924 else if (code->expr1->expr_type == EXPR_ARRAY
8925 && gfc_simplify_expr (code->expr1, 0))
8927 gfc_constructor *cons;
8928 cons = gfc_constructor_first (code->expr1->value.constructor);
8929 for (; cons; cons = gfc_constructor_next (cons))
8930 if (cons->expr->expr_type == EXPR_CONSTANT
8931 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8932 gfc_error ("Imageset argument at %L must between 1 and "
8933 "num_images()", &cons->expr->where);
8937 /* Check STAT. */
8938 if (code->expr2
8939 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8940 || code->expr2->expr_type != EXPR_VARIABLE))
8941 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8942 &code->expr2->where);
8944 /* Check ERRMSG. */
8945 if (code->expr3
8946 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8947 || code->expr3->expr_type != EXPR_VARIABLE))
8948 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8949 &code->expr3->where);
8953 /* Given a branch to a label, see if the branch is conforming.
8954 The code node describes where the branch is located. */
8956 static void
8957 resolve_branch (gfc_st_label *label, gfc_code *code)
8959 code_stack *stack;
8961 if (label == NULL)
8962 return;
8964 /* Step one: is this a valid branching target? */
8966 if (label->defined == ST_LABEL_UNKNOWN)
8968 gfc_error ("Label %d referenced at %L is never defined", label->value,
8969 &label->where);
8970 return;
8973 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
8975 gfc_error ("Statement at %L is not a valid branch target statement "
8976 "for the branch statement at %L", &label->where, &code->loc);
8977 return;
8980 /* Step two: make sure this branch is not a branch to itself ;-) */
8982 if (code->here == label)
8984 gfc_warning (0,
8985 "Branch at %L may result in an infinite loop", &code->loc);
8986 return;
8989 /* Step three: See if the label is in the same block as the
8990 branching statement. The hard work has been done by setting up
8991 the bitmap reachable_labels. */
8993 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8995 /* Check now whether there is a CRITICAL construct; if so, check
8996 whether the label is still visible outside of the CRITICAL block,
8997 which is invalid. */
8998 for (stack = cs_base; stack; stack = stack->prev)
9000 if (stack->current->op == EXEC_CRITICAL
9001 && bitmap_bit_p (stack->reachable_labels, label->value))
9002 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
9003 "label at %L", &code->loc, &label->where);
9004 else if (stack->current->op == EXEC_DO_CONCURRENT
9005 && bitmap_bit_p (stack->reachable_labels, label->value))
9006 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
9007 "for label at %L", &code->loc, &label->where);
9010 return;
9013 /* Step four: If we haven't found the label in the bitmap, it may
9014 still be the label of the END of the enclosing block, in which
9015 case we find it by going up the code_stack. */
9017 for (stack = cs_base; stack; stack = stack->prev)
9019 if (stack->current->next && stack->current->next->here == label)
9020 break;
9021 if (stack->current->op == EXEC_CRITICAL)
9023 /* Note: A label at END CRITICAL does not leave the CRITICAL
9024 construct as END CRITICAL is still part of it. */
9025 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
9026 " at %L", &code->loc, &label->where);
9027 return;
9029 else if (stack->current->op == EXEC_DO_CONCURRENT)
9031 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
9032 "label at %L", &code->loc, &label->where);
9033 return;
9037 if (stack)
9039 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
9040 return;
9043 /* The label is not in an enclosing block, so illegal. This was
9044 allowed in Fortran 66, so we allow it as extension. No
9045 further checks are necessary in this case. */
9046 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
9047 "as the GOTO statement at %L", &label->where,
9048 &code->loc);
9049 return;
9053 /* Check whether EXPR1 has the same shape as EXPR2. */
9055 static bool
9056 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
9058 mpz_t shape[GFC_MAX_DIMENSIONS];
9059 mpz_t shape2[GFC_MAX_DIMENSIONS];
9060 bool result = false;
9061 int i;
9063 /* Compare the rank. */
9064 if (expr1->rank != expr2->rank)
9065 return result;
9067 /* Compare the size of each dimension. */
9068 for (i=0; i<expr1->rank; i++)
9070 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
9071 goto ignore;
9073 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
9074 goto ignore;
9076 if (mpz_cmp (shape[i], shape2[i]))
9077 goto over;
9080 /* When either of the two expression is an assumed size array, we
9081 ignore the comparison of dimension sizes. */
9082 ignore:
9083 result = true;
9085 over:
9086 gfc_clear_shape (shape, i);
9087 gfc_clear_shape (shape2, i);
9088 return result;
9092 /* Check whether a WHERE assignment target or a WHERE mask expression
9093 has the same shape as the outmost WHERE mask expression. */
9095 static void
9096 resolve_where (gfc_code *code, gfc_expr *mask)
9098 gfc_code *cblock;
9099 gfc_code *cnext;
9100 gfc_expr *e = NULL;
9102 cblock = code->block;
9104 /* Store the first WHERE mask-expr of the WHERE statement or construct.
9105 In case of nested WHERE, only the outmost one is stored. */
9106 if (mask == NULL) /* outmost WHERE */
9107 e = cblock->expr1;
9108 else /* inner WHERE */
9109 e = mask;
9111 while (cblock)
9113 if (cblock->expr1)
9115 /* Check if the mask-expr has a consistent shape with the
9116 outmost WHERE mask-expr. */
9117 if (!resolve_where_shape (cblock->expr1, e))
9118 gfc_error ("WHERE mask at %L has inconsistent shape",
9119 &cblock->expr1->where);
9122 /* the assignment statement of a WHERE statement, or the first
9123 statement in where-body-construct of a WHERE construct */
9124 cnext = cblock->next;
9125 while (cnext)
9127 switch (cnext->op)
9129 /* WHERE assignment statement */
9130 case EXEC_ASSIGN:
9132 /* Check shape consistent for WHERE assignment target. */
9133 if (e && !resolve_where_shape (cnext->expr1, e))
9134 gfc_error ("WHERE assignment target at %L has "
9135 "inconsistent shape", &cnext->expr1->where);
9136 break;
9139 case EXEC_ASSIGN_CALL:
9140 resolve_call (cnext);
9141 if (!cnext->resolved_sym->attr.elemental)
9142 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
9143 &cnext->ext.actual->expr->where);
9144 break;
9146 /* WHERE or WHERE construct is part of a where-body-construct */
9147 case EXEC_WHERE:
9148 resolve_where (cnext, e);
9149 break;
9151 default:
9152 gfc_error ("Unsupported statement inside WHERE at %L",
9153 &cnext->loc);
9155 /* the next statement within the same where-body-construct */
9156 cnext = cnext->next;
9158 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
9159 cblock = cblock->block;
9164 /* Resolve assignment in FORALL construct.
9165 NVAR is the number of FORALL index variables, and VAR_EXPR records the
9166 FORALL index variables. */
9168 static void
9169 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
9171 int n;
9173 for (n = 0; n < nvar; n++)
9175 gfc_symbol *forall_index;
9177 forall_index = var_expr[n]->symtree->n.sym;
9179 /* Check whether the assignment target is one of the FORALL index
9180 variable. */
9181 if ((code->expr1->expr_type == EXPR_VARIABLE)
9182 && (code->expr1->symtree->n.sym == forall_index))
9183 gfc_error ("Assignment to a FORALL index variable at %L",
9184 &code->expr1->where);
9185 else
9187 /* If one of the FORALL index variables doesn't appear in the
9188 assignment variable, then there could be a many-to-one
9189 assignment. Emit a warning rather than an error because the
9190 mask could be resolving this problem. */
9191 if (!find_forall_index (code->expr1, forall_index, 0))
9192 gfc_warning (0, "The FORALL with index %qs is not used on the "
9193 "left side of the assignment at %L and so might "
9194 "cause multiple assignment to this object",
9195 var_expr[n]->symtree->name, &code->expr1->where);
9201 /* Resolve WHERE statement in FORALL construct. */
9203 static void
9204 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
9205 gfc_expr **var_expr)
9207 gfc_code *cblock;
9208 gfc_code *cnext;
9210 cblock = code->block;
9211 while (cblock)
9213 /* the assignment statement of a WHERE statement, or the first
9214 statement in where-body-construct of a WHERE construct */
9215 cnext = cblock->next;
9216 while (cnext)
9218 switch (cnext->op)
9220 /* WHERE assignment statement */
9221 case EXEC_ASSIGN:
9222 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
9223 break;
9225 /* WHERE operator assignment statement */
9226 case EXEC_ASSIGN_CALL:
9227 resolve_call (cnext);
9228 if (!cnext->resolved_sym->attr.elemental)
9229 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
9230 &cnext->ext.actual->expr->where);
9231 break;
9233 /* WHERE or WHERE construct is part of a where-body-construct */
9234 case EXEC_WHERE:
9235 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
9236 break;
9238 default:
9239 gfc_error ("Unsupported statement inside WHERE at %L",
9240 &cnext->loc);
9242 /* the next statement within the same where-body-construct */
9243 cnext = cnext->next;
9245 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
9246 cblock = cblock->block;
9251 /* Traverse the FORALL body to check whether the following errors exist:
9252 1. For assignment, check if a many-to-one assignment happens.
9253 2. For WHERE statement, check the WHERE body to see if there is any
9254 many-to-one assignment. */
9256 static void
9257 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
9259 gfc_code *c;
9261 c = code->block->next;
9262 while (c)
9264 switch (c->op)
9266 case EXEC_ASSIGN:
9267 case EXEC_POINTER_ASSIGN:
9268 gfc_resolve_assign_in_forall (c, nvar, var_expr);
9269 break;
9271 case EXEC_ASSIGN_CALL:
9272 resolve_call (c);
9273 break;
9275 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
9276 there is no need to handle it here. */
9277 case EXEC_FORALL:
9278 break;
9279 case EXEC_WHERE:
9280 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
9281 break;
9282 default:
9283 break;
9285 /* The next statement in the FORALL body. */
9286 c = c->next;
9291 /* Counts the number of iterators needed inside a forall construct, including
9292 nested forall constructs. This is used to allocate the needed memory
9293 in gfc_resolve_forall. */
9295 static int
9296 gfc_count_forall_iterators (gfc_code *code)
9298 int max_iters, sub_iters, current_iters;
9299 gfc_forall_iterator *fa;
9301 gcc_assert(code->op == EXEC_FORALL);
9302 max_iters = 0;
9303 current_iters = 0;
9305 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9306 current_iters ++;
9308 code = code->block->next;
9310 while (code)
9312 if (code->op == EXEC_FORALL)
9314 sub_iters = gfc_count_forall_iterators (code);
9315 if (sub_iters > max_iters)
9316 max_iters = sub_iters;
9318 code = code->next;
9321 return current_iters + max_iters;
9325 /* Given a FORALL construct, first resolve the FORALL iterator, then call
9326 gfc_resolve_forall_body to resolve the FORALL body. */
9328 static void
9329 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
9331 static gfc_expr **var_expr;
9332 static int total_var = 0;
9333 static int nvar = 0;
9334 int old_nvar, tmp;
9335 gfc_forall_iterator *fa;
9336 int i;
9338 old_nvar = nvar;
9340 /* Start to resolve a FORALL construct */
9341 if (forall_save == 0)
9343 /* Count the total number of FORALL index in the nested FORALL
9344 construct in order to allocate the VAR_EXPR with proper size. */
9345 total_var = gfc_count_forall_iterators (code);
9347 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
9348 var_expr = XCNEWVEC (gfc_expr *, total_var);
9351 /* The information about FORALL iterator, including FORALL index start, end
9352 and stride. The FORALL index can not appear in start, end or stride. */
9353 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9355 /* Check if any outer FORALL index name is the same as the current
9356 one. */
9357 for (i = 0; i < nvar; i++)
9359 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
9361 gfc_error ("An outer FORALL construct already has an index "
9362 "with this name %L", &fa->var->where);
9366 /* Record the current FORALL index. */
9367 var_expr[nvar] = gfc_copy_expr (fa->var);
9369 nvar++;
9371 /* No memory leak. */
9372 gcc_assert (nvar <= total_var);
9375 /* Resolve the FORALL body. */
9376 gfc_resolve_forall_body (code, nvar, var_expr);
9378 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
9379 gfc_resolve_blocks (code->block, ns);
9381 tmp = nvar;
9382 nvar = old_nvar;
9383 /* Free only the VAR_EXPRs allocated in this frame. */
9384 for (i = nvar; i < tmp; i++)
9385 gfc_free_expr (var_expr[i]);
9387 if (nvar == 0)
9389 /* We are in the outermost FORALL construct. */
9390 gcc_assert (forall_save == 0);
9392 /* VAR_EXPR is not needed any more. */
9393 free (var_expr);
9394 total_var = 0;
9399 /* Resolve a BLOCK construct statement. */
9401 static void
9402 resolve_block_construct (gfc_code* code)
9404 /* Resolve the BLOCK's namespace. */
9405 gfc_resolve (code->ext.block.ns);
9407 /* For an ASSOCIATE block, the associations (and their targets) are already
9408 resolved during resolve_symbol. */
9412 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
9413 DO code nodes. */
9415 void
9416 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
9418 bool t;
9420 for (; b; b = b->block)
9422 t = gfc_resolve_expr (b->expr1);
9423 if (!gfc_resolve_expr (b->expr2))
9424 t = false;
9426 switch (b->op)
9428 case EXEC_IF:
9429 if (t && b->expr1 != NULL
9430 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
9431 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9432 &b->expr1->where);
9433 break;
9435 case EXEC_WHERE:
9436 if (t
9437 && b->expr1 != NULL
9438 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
9439 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
9440 &b->expr1->where);
9441 break;
9443 case EXEC_GOTO:
9444 resolve_branch (b->label1, b);
9445 break;
9447 case EXEC_BLOCK:
9448 resolve_block_construct (b);
9449 break;
9451 case EXEC_SELECT:
9452 case EXEC_SELECT_TYPE:
9453 case EXEC_FORALL:
9454 case EXEC_DO:
9455 case EXEC_DO_WHILE:
9456 case EXEC_DO_CONCURRENT:
9457 case EXEC_CRITICAL:
9458 case EXEC_READ:
9459 case EXEC_WRITE:
9460 case EXEC_IOLENGTH:
9461 case EXEC_WAIT:
9462 break;
9464 case EXEC_OACC_PARALLEL_LOOP:
9465 case EXEC_OACC_PARALLEL:
9466 case EXEC_OACC_KERNELS_LOOP:
9467 case EXEC_OACC_KERNELS:
9468 case EXEC_OACC_DATA:
9469 case EXEC_OACC_HOST_DATA:
9470 case EXEC_OACC_LOOP:
9471 case EXEC_OACC_UPDATE:
9472 case EXEC_OACC_WAIT:
9473 case EXEC_OACC_CACHE:
9474 case EXEC_OACC_ENTER_DATA:
9475 case EXEC_OACC_EXIT_DATA:
9476 case EXEC_OACC_ATOMIC:
9477 case EXEC_OACC_ROUTINE:
9478 case EXEC_OMP_ATOMIC:
9479 case EXEC_OMP_CRITICAL:
9480 case EXEC_OMP_DISTRIBUTE:
9481 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
9482 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
9483 case EXEC_OMP_DISTRIBUTE_SIMD:
9484 case EXEC_OMP_DO:
9485 case EXEC_OMP_DO_SIMD:
9486 case EXEC_OMP_MASTER:
9487 case EXEC_OMP_ORDERED:
9488 case EXEC_OMP_PARALLEL:
9489 case EXEC_OMP_PARALLEL_DO:
9490 case EXEC_OMP_PARALLEL_DO_SIMD:
9491 case EXEC_OMP_PARALLEL_SECTIONS:
9492 case EXEC_OMP_PARALLEL_WORKSHARE:
9493 case EXEC_OMP_SECTIONS:
9494 case EXEC_OMP_SIMD:
9495 case EXEC_OMP_SINGLE:
9496 case EXEC_OMP_TARGET:
9497 case EXEC_OMP_TARGET_DATA:
9498 case EXEC_OMP_TARGET_TEAMS:
9499 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
9500 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
9501 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9502 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
9503 case EXEC_OMP_TARGET_UPDATE:
9504 case EXEC_OMP_TASK:
9505 case EXEC_OMP_TASKGROUP:
9506 case EXEC_OMP_TASKWAIT:
9507 case EXEC_OMP_TASKYIELD:
9508 case EXEC_OMP_TEAMS:
9509 case EXEC_OMP_TEAMS_DISTRIBUTE:
9510 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
9511 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9512 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
9513 case EXEC_OMP_WORKSHARE:
9514 break;
9516 default:
9517 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
9520 gfc_resolve_code (b->next, ns);
9525 /* Does everything to resolve an ordinary assignment. Returns true
9526 if this is an interface assignment. */
9527 static bool
9528 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9530 bool rval = false;
9531 gfc_expr *lhs;
9532 gfc_expr *rhs;
9533 int llen = 0;
9534 int rlen = 0;
9535 int n;
9536 gfc_ref *ref;
9537 symbol_attribute attr;
9539 if (gfc_extend_assign (code, ns))
9541 gfc_expr** rhsptr;
9543 if (code->op == EXEC_ASSIGN_CALL)
9545 lhs = code->ext.actual->expr;
9546 rhsptr = &code->ext.actual->next->expr;
9548 else
9550 gfc_actual_arglist* args;
9551 gfc_typebound_proc* tbp;
9553 gcc_assert (code->op == EXEC_COMPCALL);
9555 args = code->expr1->value.compcall.actual;
9556 lhs = args->expr;
9557 rhsptr = &args->next->expr;
9559 tbp = code->expr1->value.compcall.tbp;
9560 gcc_assert (!tbp->is_generic);
9563 /* Make a temporary rhs when there is a default initializer
9564 and rhs is the same symbol as the lhs. */
9565 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9566 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9567 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9568 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9569 *rhsptr = gfc_get_parentheses (*rhsptr);
9571 return true;
9574 lhs = code->expr1;
9575 rhs = code->expr2;
9577 if (rhs->is_boz
9578 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9579 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9580 &code->loc))
9581 return false;
9583 /* Handle the case of a BOZ literal on the RHS. */
9584 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9586 int rc;
9587 if (warn_surprising)
9588 gfc_warning (OPT_Wsurprising,
9589 "BOZ literal at %L is bitwise transferred "
9590 "non-integer symbol %qs", &code->loc,
9591 lhs->symtree->n.sym->name);
9593 if (!gfc_convert_boz (rhs, &lhs->ts))
9594 return false;
9595 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9597 if (rc == ARITH_UNDERFLOW)
9598 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9599 ". This check can be disabled with the option "
9600 "%<-fno-range-check%>", &rhs->where);
9601 else if (rc == ARITH_OVERFLOW)
9602 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9603 ". This check can be disabled with the option "
9604 "%<-fno-range-check%>", &rhs->where);
9605 else if (rc == ARITH_NAN)
9606 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9607 ". This check can be disabled with the option "
9608 "%<-fno-range-check%>", &rhs->where);
9609 return false;
9613 if (lhs->ts.type == BT_CHARACTER
9614 && warn_character_truncation)
9616 if (lhs->ts.u.cl != NULL
9617 && lhs->ts.u.cl->length != NULL
9618 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9619 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9621 if (rhs->expr_type == EXPR_CONSTANT)
9622 rlen = rhs->value.character.length;
9624 else if (rhs->ts.u.cl != NULL
9625 && rhs->ts.u.cl->length != NULL
9626 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9627 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9629 if (rlen && llen && rlen > llen)
9630 gfc_warning_now (OPT_Wcharacter_truncation,
9631 "CHARACTER expression will be truncated "
9632 "in assignment (%d/%d) at %L",
9633 llen, rlen, &code->loc);
9636 /* Ensure that a vector index expression for the lvalue is evaluated
9637 to a temporary if the lvalue symbol is referenced in it. */
9638 if (lhs->rank)
9640 for (ref = lhs->ref; ref; ref= ref->next)
9641 if (ref->type == REF_ARRAY)
9643 for (n = 0; n < ref->u.ar.dimen; n++)
9644 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9645 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9646 ref->u.ar.start[n]))
9647 ref->u.ar.start[n]
9648 = gfc_get_parentheses (ref->u.ar.start[n]);
9652 if (gfc_pure (NULL))
9654 if (lhs->ts.type == BT_DERIVED
9655 && lhs->expr_type == EXPR_VARIABLE
9656 && lhs->ts.u.derived->attr.pointer_comp
9657 && rhs->expr_type == EXPR_VARIABLE
9658 && (gfc_impure_variable (rhs->symtree->n.sym)
9659 || gfc_is_coindexed (rhs)))
9661 /* F2008, C1283. */
9662 if (gfc_is_coindexed (rhs))
9663 gfc_error ("Coindexed expression at %L is assigned to "
9664 "a derived type variable with a POINTER "
9665 "component in a PURE procedure",
9666 &rhs->where);
9667 else
9668 gfc_error ("The impure variable at %L is assigned to "
9669 "a derived type variable with a POINTER "
9670 "component in a PURE procedure (12.6)",
9671 &rhs->where);
9672 return rval;
9675 /* Fortran 2008, C1283. */
9676 if (gfc_is_coindexed (lhs))
9678 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9679 "procedure", &rhs->where);
9680 return rval;
9684 if (gfc_implicit_pure (NULL))
9686 if (lhs->expr_type == EXPR_VARIABLE
9687 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9688 && lhs->symtree->n.sym->ns != gfc_current_ns)
9689 gfc_unset_implicit_pure (NULL);
9691 if (lhs->ts.type == BT_DERIVED
9692 && lhs->expr_type == EXPR_VARIABLE
9693 && lhs->ts.u.derived->attr.pointer_comp
9694 && rhs->expr_type == EXPR_VARIABLE
9695 && (gfc_impure_variable (rhs->symtree->n.sym)
9696 || gfc_is_coindexed (rhs)))
9697 gfc_unset_implicit_pure (NULL);
9699 /* Fortran 2008, C1283. */
9700 if (gfc_is_coindexed (lhs))
9701 gfc_unset_implicit_pure (NULL);
9704 /* F2008, 7.2.1.2. */
9705 attr = gfc_expr_attr (lhs);
9706 if (lhs->ts.type == BT_CLASS && attr.allocatable)
9708 if (attr.codimension)
9710 gfc_error ("Assignment to polymorphic coarray at %L is not "
9711 "permitted", &lhs->where);
9712 return false;
9714 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
9715 "polymorphic variable at %L", &lhs->where))
9716 return false;
9717 if (!flag_realloc_lhs)
9719 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9720 "requires %<-frealloc-lhs%>", &lhs->where);
9721 return false;
9723 /* See PR 43366. */
9724 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9725 "is not yet supported", &lhs->where);
9726 return false;
9728 else if (lhs->ts.type == BT_CLASS)
9730 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
9731 "assignment at %L - check that there is a matching specific "
9732 "subroutine for '=' operator", &lhs->where);
9733 return false;
9736 bool lhs_coindexed = gfc_is_coindexed (lhs);
9738 /* F2008, Section 7.2.1.2. */
9739 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
9741 gfc_error ("Coindexed variable must not have an allocatable ultimate "
9742 "component in assignment at %L", &lhs->where);
9743 return false;
9746 gfc_check_assign (lhs, rhs, 1);
9748 /* Assign the 'data' of a class object to a derived type. */
9749 if (lhs->ts.type == BT_DERIVED
9750 && rhs->ts.type == BT_CLASS)
9751 gfc_add_data_component (rhs);
9753 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
9754 Additionally, insert this code when the RHS is a CAF as we then use the
9755 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
9756 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
9757 noncoindexed array and the RHS is a coindexed scalar, use the normal code
9758 path. */
9759 if (flag_coarray == GFC_FCOARRAY_LIB
9760 && (lhs_coindexed
9761 || (code->expr2->expr_type == EXPR_FUNCTION
9762 && code->expr2->value.function.isym
9763 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
9764 && (code->expr1->rank == 0 || code->expr2->rank != 0)
9765 && !gfc_expr_attr (rhs).allocatable
9766 && !gfc_has_vector_subscript (rhs))))
9768 if (code->expr2->expr_type == EXPR_FUNCTION
9769 && code->expr2->value.function.isym
9770 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
9771 remove_caf_get_intrinsic (code->expr2);
9772 code->op = EXEC_CALL;
9773 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
9774 code->resolved_sym = code->symtree->n.sym;
9775 code->resolved_sym->attr.flavor = FL_PROCEDURE;
9776 code->resolved_sym->attr.intrinsic = 1;
9777 code->resolved_sym->attr.subroutine = 1;
9778 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
9779 gfc_commit_symbol (code->resolved_sym);
9780 code->ext.actual = gfc_get_actual_arglist ();
9781 code->ext.actual->expr = lhs;
9782 code->ext.actual->next = gfc_get_actual_arglist ();
9783 code->ext.actual->next->expr = rhs;
9784 code->expr1 = NULL;
9785 code->expr2 = NULL;
9788 return false;
9792 /* Add a component reference onto an expression. */
9794 static void
9795 add_comp_ref (gfc_expr *e, gfc_component *c)
9797 gfc_ref **ref;
9798 ref = &(e->ref);
9799 while (*ref)
9800 ref = &((*ref)->next);
9801 *ref = gfc_get_ref ();
9802 (*ref)->type = REF_COMPONENT;
9803 (*ref)->u.c.sym = e->ts.u.derived;
9804 (*ref)->u.c.component = c;
9805 e->ts = c->ts;
9807 /* Add a full array ref, as necessary. */
9808 if (c->as)
9810 gfc_add_full_array_ref (e, c->as);
9811 e->rank = c->as->rank;
9816 /* Build an assignment. Keep the argument 'op' for future use, so that
9817 pointer assignments can be made. */
9819 static gfc_code *
9820 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9821 gfc_component *comp1, gfc_component *comp2, locus loc)
9823 gfc_code *this_code;
9825 this_code = gfc_get_code (op);
9826 this_code->next = NULL;
9827 this_code->expr1 = gfc_copy_expr (expr1);
9828 this_code->expr2 = gfc_copy_expr (expr2);
9829 this_code->loc = loc;
9830 if (comp1 && comp2)
9832 add_comp_ref (this_code->expr1, comp1);
9833 add_comp_ref (this_code->expr2, comp2);
9836 return this_code;
9840 /* Makes a temporary variable expression based on the characteristics of
9841 a given variable expression. */
9843 static gfc_expr*
9844 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9846 static int serial = 0;
9847 char name[GFC_MAX_SYMBOL_LEN];
9848 gfc_symtree *tmp;
9849 gfc_array_spec *as;
9850 gfc_array_ref *aref;
9851 gfc_ref *ref;
9853 sprintf (name, GFC_PREFIX("DA%d"), serial++);
9854 gfc_get_sym_tree (name, ns, &tmp, false);
9855 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9857 as = NULL;
9858 ref = NULL;
9859 aref = NULL;
9861 /* Obtain the arrayspec for the temporary. */
9862 if (e->rank && e->expr_type != EXPR_ARRAY
9863 && e->expr_type != EXPR_FUNCTION
9864 && e->expr_type != EXPR_OP)
9866 aref = gfc_find_array_ref (e);
9867 if (e->expr_type == EXPR_VARIABLE
9868 && e->symtree->n.sym->as == aref->as)
9869 as = aref->as;
9870 else
9872 for (ref = e->ref; ref; ref = ref->next)
9873 if (ref->type == REF_COMPONENT
9874 && ref->u.c.component->as == aref->as)
9876 as = aref->as;
9877 break;
9882 /* Add the attributes and the arrayspec to the temporary. */
9883 tmp->n.sym->attr = gfc_expr_attr (e);
9884 tmp->n.sym->attr.function = 0;
9885 tmp->n.sym->attr.result = 0;
9886 tmp->n.sym->attr.flavor = FL_VARIABLE;
9888 if (as)
9890 tmp->n.sym->as = gfc_copy_array_spec (as);
9891 if (!ref)
9892 ref = e->ref;
9893 if (as->type == AS_DEFERRED)
9894 tmp->n.sym->attr.allocatable = 1;
9896 else if (e->rank && (e->expr_type == EXPR_ARRAY
9897 || e->expr_type == EXPR_FUNCTION
9898 || e->expr_type == EXPR_OP))
9900 tmp->n.sym->as = gfc_get_array_spec ();
9901 tmp->n.sym->as->type = AS_DEFERRED;
9902 tmp->n.sym->as->rank = e->rank;
9903 tmp->n.sym->attr.allocatable = 1;
9904 tmp->n.sym->attr.dimension = 1;
9906 else
9907 tmp->n.sym->attr.dimension = 0;
9909 gfc_set_sym_referenced (tmp->n.sym);
9910 gfc_commit_symbol (tmp->n.sym);
9911 e = gfc_lval_expr_from_sym (tmp->n.sym);
9913 /* Should the lhs be a section, use its array ref for the
9914 temporary expression. */
9915 if (aref && aref->type != AR_FULL)
9917 gfc_free_ref_list (e->ref);
9918 e->ref = gfc_copy_ref (ref);
9920 return e;
9924 /* Add one line of code to the code chain, making sure that 'head' and
9925 'tail' are appropriately updated. */
9927 static void
9928 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9930 gcc_assert (this_code);
9931 if (*head == NULL)
9932 *head = *tail = *this_code;
9933 else
9934 *tail = gfc_append_code (*tail, *this_code);
9935 *this_code = NULL;
9939 /* Counts the potential number of part array references that would
9940 result from resolution of typebound defined assignments. */
9942 static int
9943 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9945 gfc_component *c;
9946 int c_depth = 0, t_depth;
9948 for (c= derived->components; c; c = c->next)
9950 if ((!gfc_bt_struct (c->ts.type)
9951 || c->attr.pointer
9952 || c->attr.allocatable
9953 || c->attr.proc_pointer_comp
9954 || c->attr.class_pointer
9955 || c->attr.proc_pointer)
9956 && !c->attr.defined_assign_comp)
9957 continue;
9959 if (c->as && c_depth == 0)
9960 c_depth = 1;
9962 if (c->ts.u.derived->attr.defined_assign_comp)
9963 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9964 c->as ? 1 : 0);
9965 else
9966 t_depth = 0;
9968 c_depth = t_depth > c_depth ? t_depth : c_depth;
9970 return depth + c_depth;
9974 /* Implement 7.2.1.3 of the F08 standard:
9975 "An intrinsic assignment where the variable is of derived type is
9976 performed as if each component of the variable were assigned from the
9977 corresponding component of expr using pointer assignment (7.2.2) for
9978 each pointer component, defined assignment for each nonpointer
9979 nonallocatable component of a type that has a type-bound defined
9980 assignment consistent with the component, intrinsic assignment for
9981 each other nonpointer nonallocatable component, ..."
9983 The pointer assignments are taken care of by the intrinsic
9984 assignment of the structure itself. This function recursively adds
9985 defined assignments where required. The recursion is accomplished
9986 by calling gfc_resolve_code.
9988 When the lhs in a defined assignment has intent INOUT, we need a
9989 temporary for the lhs. In pseudo-code:
9991 ! Only call function lhs once.
9992 if (lhs is not a constant or an variable)
9993 temp_x = expr2
9994 expr2 => temp_x
9995 ! Do the intrinsic assignment
9996 expr1 = expr2
9997 ! Now do the defined assignments
9998 do over components with typebound defined assignment [%cmp]
9999 #if one component's assignment procedure is INOUT
10000 t1 = expr1
10001 #if expr2 non-variable
10002 temp_x = expr2
10003 expr2 => temp_x
10004 # endif
10005 expr1 = expr2
10006 # for each cmp
10007 t1%cmp {defined=} expr2%cmp
10008 expr1%cmp = t1%cmp
10009 #else
10010 expr1 = expr2
10012 # for each cmp
10013 expr1%cmp {defined=} expr2%cmp
10014 #endif
10017 /* The temporary assignments have to be put on top of the additional
10018 code to avoid the result being changed by the intrinsic assignment.
10020 static int component_assignment_level = 0;
10021 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
10023 static void
10024 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
10026 gfc_component *comp1, *comp2;
10027 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
10028 gfc_expr *t1;
10029 int error_count, depth;
10031 gfc_get_errors (NULL, &error_count);
10033 /* Filter out continuing processing after an error. */
10034 if (error_count
10035 || (*code)->expr1->ts.type != BT_DERIVED
10036 || (*code)->expr2->ts.type != BT_DERIVED)
10037 return;
10039 /* TODO: Handle more than one part array reference in assignments. */
10040 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
10041 (*code)->expr1->rank ? 1 : 0);
10042 if (depth > 1)
10044 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
10045 "done because multiple part array references would "
10046 "occur in intermediate expressions.", &(*code)->loc);
10047 return;
10050 component_assignment_level++;
10052 /* Create a temporary so that functions get called only once. */
10053 if ((*code)->expr2->expr_type != EXPR_VARIABLE
10054 && (*code)->expr2->expr_type != EXPR_CONSTANT)
10056 gfc_expr *tmp_expr;
10058 /* Assign the rhs to the temporary. */
10059 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
10060 this_code = build_assignment (EXEC_ASSIGN,
10061 tmp_expr, (*code)->expr2,
10062 NULL, NULL, (*code)->loc);
10063 /* Add the code and substitute the rhs expression. */
10064 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
10065 gfc_free_expr ((*code)->expr2);
10066 (*code)->expr2 = tmp_expr;
10069 /* Do the intrinsic assignment. This is not needed if the lhs is one
10070 of the temporaries generated here, since the intrinsic assignment
10071 to the final result already does this. */
10072 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
10074 this_code = build_assignment (EXEC_ASSIGN,
10075 (*code)->expr1, (*code)->expr2,
10076 NULL, NULL, (*code)->loc);
10077 add_code_to_chain (&this_code, &head, &tail);
10080 comp1 = (*code)->expr1->ts.u.derived->components;
10081 comp2 = (*code)->expr2->ts.u.derived->components;
10083 t1 = NULL;
10084 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
10086 bool inout = false;
10088 /* The intrinsic assignment does the right thing for pointers
10089 of all kinds and allocatable components. */
10090 if (!gfc_bt_struct (comp1->ts.type)
10091 || comp1->attr.pointer
10092 || comp1->attr.allocatable
10093 || comp1->attr.proc_pointer_comp
10094 || comp1->attr.class_pointer
10095 || comp1->attr.proc_pointer)
10096 continue;
10098 /* Make an assigment for this component. */
10099 this_code = build_assignment (EXEC_ASSIGN,
10100 (*code)->expr1, (*code)->expr2,
10101 comp1, comp2, (*code)->loc);
10103 /* Convert the assignment if there is a defined assignment for
10104 this type. Otherwise, using the call from gfc_resolve_code,
10105 recurse into its components. */
10106 gfc_resolve_code (this_code, ns);
10108 if (this_code->op == EXEC_ASSIGN_CALL)
10110 gfc_formal_arglist *dummy_args;
10111 gfc_symbol *rsym;
10112 /* Check that there is a typebound defined assignment. If not,
10113 then this must be a module defined assignment. We cannot
10114 use the defined_assign_comp attribute here because it must
10115 be this derived type that has the defined assignment and not
10116 a parent type. */
10117 if (!(comp1->ts.u.derived->f2k_derived
10118 && comp1->ts.u.derived->f2k_derived
10119 ->tb_op[INTRINSIC_ASSIGN]))
10121 gfc_free_statements (this_code);
10122 this_code = NULL;
10123 continue;
10126 /* If the first argument of the subroutine has intent INOUT
10127 a temporary must be generated and used instead. */
10128 rsym = this_code->resolved_sym;
10129 dummy_args = gfc_sym_get_dummy_args (rsym);
10130 if (dummy_args
10131 && dummy_args->sym->attr.intent == INTENT_INOUT)
10133 gfc_code *temp_code;
10134 inout = true;
10136 /* Build the temporary required for the assignment and put
10137 it at the head of the generated code. */
10138 if (!t1)
10140 t1 = get_temp_from_expr ((*code)->expr1, ns);
10141 temp_code = build_assignment (EXEC_ASSIGN,
10142 t1, (*code)->expr1,
10143 NULL, NULL, (*code)->loc);
10145 /* For allocatable LHS, check whether it is allocated. Note
10146 that allocatable components with defined assignment are
10147 not yet support. See PR 57696. */
10148 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
10150 gfc_code *block;
10151 gfc_expr *e =
10152 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10153 block = gfc_get_code (EXEC_IF);
10154 block->block = gfc_get_code (EXEC_IF);
10155 block->block->expr1
10156 = gfc_build_intrinsic_call (ns,
10157 GFC_ISYM_ALLOCATED, "allocated",
10158 (*code)->loc, 1, e);
10159 block->block->next = temp_code;
10160 temp_code = block;
10162 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
10165 /* Replace the first actual arg with the component of the
10166 temporary. */
10167 gfc_free_expr (this_code->ext.actual->expr);
10168 this_code->ext.actual->expr = gfc_copy_expr (t1);
10169 add_comp_ref (this_code->ext.actual->expr, comp1);
10171 /* If the LHS variable is allocatable and wasn't allocated and
10172 the temporary is allocatable, pointer assign the address of
10173 the freshly allocated LHS to the temporary. */
10174 if ((*code)->expr1->symtree->n.sym->attr.allocatable
10175 && gfc_expr_attr ((*code)->expr1).allocatable)
10177 gfc_code *block;
10178 gfc_expr *cond;
10180 cond = gfc_get_expr ();
10181 cond->ts.type = BT_LOGICAL;
10182 cond->ts.kind = gfc_default_logical_kind;
10183 cond->expr_type = EXPR_OP;
10184 cond->where = (*code)->loc;
10185 cond->value.op.op = INTRINSIC_NOT;
10186 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
10187 GFC_ISYM_ALLOCATED, "allocated",
10188 (*code)->loc, 1, gfc_copy_expr (t1));
10189 block = gfc_get_code (EXEC_IF);
10190 block->block = gfc_get_code (EXEC_IF);
10191 block->block->expr1 = cond;
10192 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10193 t1, (*code)->expr1,
10194 NULL, NULL, (*code)->loc);
10195 add_code_to_chain (&block, &head, &tail);
10199 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
10201 /* Don't add intrinsic assignments since they are already
10202 effected by the intrinsic assignment of the structure. */
10203 gfc_free_statements (this_code);
10204 this_code = NULL;
10205 continue;
10208 add_code_to_chain (&this_code, &head, &tail);
10210 if (t1 && inout)
10212 /* Transfer the value to the final result. */
10213 this_code = build_assignment (EXEC_ASSIGN,
10214 (*code)->expr1, t1,
10215 comp1, comp2, (*code)->loc);
10216 add_code_to_chain (&this_code, &head, &tail);
10220 /* Put the temporary assignments at the top of the generated code. */
10221 if (tmp_head && component_assignment_level == 1)
10223 gfc_append_code (tmp_head, head);
10224 head = tmp_head;
10225 tmp_head = tmp_tail = NULL;
10228 // If we did a pointer assignment - thus, we need to ensure that the LHS is
10229 // not accidentally deallocated. Hence, nullify t1.
10230 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
10231 && gfc_expr_attr ((*code)->expr1).allocatable)
10233 gfc_code *block;
10234 gfc_expr *cond;
10235 gfc_expr *e;
10237 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10238 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
10239 (*code)->loc, 2, gfc_copy_expr (t1), e);
10240 block = gfc_get_code (EXEC_IF);
10241 block->block = gfc_get_code (EXEC_IF);
10242 block->block->expr1 = cond;
10243 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10244 t1, gfc_get_null_expr (&(*code)->loc),
10245 NULL, NULL, (*code)->loc);
10246 gfc_append_code (tail, block);
10247 tail = block;
10250 /* Now attach the remaining code chain to the input code. Step on
10251 to the end of the new code since resolution is complete. */
10252 gcc_assert ((*code)->op == EXEC_ASSIGN);
10253 tail->next = (*code)->next;
10254 /* Overwrite 'code' because this would place the intrinsic assignment
10255 before the temporary for the lhs is created. */
10256 gfc_free_expr ((*code)->expr1);
10257 gfc_free_expr ((*code)->expr2);
10258 **code = *head;
10259 if (head != tail)
10260 free (head);
10261 *code = tail;
10263 component_assignment_level--;
10267 /* F2008: Pointer function assignments are of the form:
10268 ptr_fcn (args) = expr
10269 This function breaks these assignments into two statements:
10270 temporary_pointer => ptr_fcn(args)
10271 temporary_pointer = expr */
10273 static bool
10274 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
10276 gfc_expr *tmp_ptr_expr;
10277 gfc_code *this_code;
10278 gfc_component *comp;
10279 gfc_symbol *s;
10281 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
10282 return false;
10284 /* Even if standard does not support this feature, continue to build
10285 the two statements to avoid upsetting frontend_passes.c. */
10286 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
10287 "%L", &(*code)->loc);
10289 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
10291 if (comp)
10292 s = comp->ts.interface;
10293 else
10294 s = (*code)->expr1->symtree->n.sym;
10296 if (s == NULL || !s->result->attr.pointer)
10298 gfc_error ("The function result on the lhs of the assignment at "
10299 "%L must have the pointer attribute.",
10300 &(*code)->expr1->where);
10301 (*code)->op = EXEC_NOP;
10302 return false;
10305 tmp_ptr_expr = get_temp_from_expr ((*code)->expr2, ns);
10307 /* get_temp_from_expression is set up for ordinary assignments. To that
10308 end, where array bounds are not known, arrays are made allocatable.
10309 Change the temporary to a pointer here. */
10310 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
10311 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
10312 tmp_ptr_expr->where = (*code)->loc;
10314 this_code = build_assignment (EXEC_ASSIGN,
10315 tmp_ptr_expr, (*code)->expr2,
10316 NULL, NULL, (*code)->loc);
10317 this_code->next = (*code)->next;
10318 (*code)->next = this_code;
10319 (*code)->op = EXEC_POINTER_ASSIGN;
10320 (*code)->expr2 = (*code)->expr1;
10321 (*code)->expr1 = tmp_ptr_expr;
10323 return true;
10327 /* Deferred character length assignments from an operator expression
10328 require a temporary because the character length of the lhs can
10329 change in the course of the assignment. */
10331 static bool
10332 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
10334 gfc_expr *tmp_expr;
10335 gfc_code *this_code;
10337 if (!((*code)->expr1->ts.type == BT_CHARACTER
10338 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
10339 && (*code)->expr2->expr_type == EXPR_OP))
10340 return false;
10342 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
10343 return false;
10345 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
10346 tmp_expr->where = (*code)->loc;
10348 /* A new charlen is required to ensure that the variable string
10349 length is different to that of the original lhs. */
10350 tmp_expr->ts.u.cl = gfc_get_charlen();
10351 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
10352 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
10353 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
10355 tmp_expr->symtree->n.sym->ts.deferred = 1;
10357 this_code = build_assignment (EXEC_ASSIGN,
10358 (*code)->expr1,
10359 gfc_copy_expr (tmp_expr),
10360 NULL, NULL, (*code)->loc);
10362 (*code)->expr1 = tmp_expr;
10364 this_code->next = (*code)->next;
10365 (*code)->next = this_code;
10367 return true;
10371 /* Given a block of code, recursively resolve everything pointed to by this
10372 code block. */
10374 void
10375 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
10377 int omp_workshare_save;
10378 int forall_save, do_concurrent_save;
10379 code_stack frame;
10380 bool t;
10382 frame.prev = cs_base;
10383 frame.head = code;
10384 cs_base = &frame;
10386 find_reachable_labels (code);
10388 for (; code; code = code->next)
10390 frame.current = code;
10391 forall_save = forall_flag;
10392 do_concurrent_save = gfc_do_concurrent_flag;
10394 if (code->op == EXEC_FORALL)
10396 forall_flag = 1;
10397 gfc_resolve_forall (code, ns, forall_save);
10398 forall_flag = 2;
10400 else if (code->block)
10402 omp_workshare_save = -1;
10403 switch (code->op)
10405 case EXEC_OACC_PARALLEL_LOOP:
10406 case EXEC_OACC_PARALLEL:
10407 case EXEC_OACC_KERNELS_LOOP:
10408 case EXEC_OACC_KERNELS:
10409 case EXEC_OACC_DATA:
10410 case EXEC_OACC_HOST_DATA:
10411 case EXEC_OACC_LOOP:
10412 gfc_resolve_oacc_blocks (code, ns);
10413 break;
10414 case EXEC_OMP_PARALLEL_WORKSHARE:
10415 omp_workshare_save = omp_workshare_flag;
10416 omp_workshare_flag = 1;
10417 gfc_resolve_omp_parallel_blocks (code, ns);
10418 break;
10419 case EXEC_OMP_PARALLEL:
10420 case EXEC_OMP_PARALLEL_DO:
10421 case EXEC_OMP_PARALLEL_DO_SIMD:
10422 case EXEC_OMP_PARALLEL_SECTIONS:
10423 case EXEC_OMP_TARGET_TEAMS:
10424 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10425 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10426 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10427 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10428 case EXEC_OMP_TASK:
10429 case EXEC_OMP_TEAMS:
10430 case EXEC_OMP_TEAMS_DISTRIBUTE:
10431 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10432 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10433 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10434 omp_workshare_save = omp_workshare_flag;
10435 omp_workshare_flag = 0;
10436 gfc_resolve_omp_parallel_blocks (code, ns);
10437 break;
10438 case EXEC_OMP_DISTRIBUTE:
10439 case EXEC_OMP_DISTRIBUTE_SIMD:
10440 case EXEC_OMP_DO:
10441 case EXEC_OMP_DO_SIMD:
10442 case EXEC_OMP_SIMD:
10443 gfc_resolve_omp_do_blocks (code, ns);
10444 break;
10445 case EXEC_SELECT_TYPE:
10446 /* Blocks are handled in resolve_select_type because we have
10447 to transform the SELECT TYPE into ASSOCIATE first. */
10448 break;
10449 case EXEC_DO_CONCURRENT:
10450 gfc_do_concurrent_flag = 1;
10451 gfc_resolve_blocks (code->block, ns);
10452 gfc_do_concurrent_flag = 2;
10453 break;
10454 case EXEC_OMP_WORKSHARE:
10455 omp_workshare_save = omp_workshare_flag;
10456 omp_workshare_flag = 1;
10457 /* FALL THROUGH */
10458 default:
10459 gfc_resolve_blocks (code->block, ns);
10460 break;
10463 if (omp_workshare_save != -1)
10464 omp_workshare_flag = omp_workshare_save;
10466 start:
10467 t = true;
10468 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
10469 t = gfc_resolve_expr (code->expr1);
10470 forall_flag = forall_save;
10471 gfc_do_concurrent_flag = do_concurrent_save;
10473 if (!gfc_resolve_expr (code->expr2))
10474 t = false;
10476 if (code->op == EXEC_ALLOCATE
10477 && !gfc_resolve_expr (code->expr3))
10478 t = false;
10480 switch (code->op)
10482 case EXEC_NOP:
10483 case EXEC_END_BLOCK:
10484 case EXEC_END_NESTED_BLOCK:
10485 case EXEC_CYCLE:
10486 case EXEC_PAUSE:
10487 case EXEC_STOP:
10488 case EXEC_ERROR_STOP:
10489 case EXEC_EXIT:
10490 case EXEC_CONTINUE:
10491 case EXEC_DT_END:
10492 case EXEC_ASSIGN_CALL:
10493 break;
10495 case EXEC_CRITICAL:
10496 resolve_critical (code);
10497 break;
10499 case EXEC_SYNC_ALL:
10500 case EXEC_SYNC_IMAGES:
10501 case EXEC_SYNC_MEMORY:
10502 resolve_sync (code);
10503 break;
10505 case EXEC_LOCK:
10506 case EXEC_UNLOCK:
10507 case EXEC_EVENT_POST:
10508 case EXEC_EVENT_WAIT:
10509 resolve_lock_unlock_event (code);
10510 break;
10512 case EXEC_ENTRY:
10513 /* Keep track of which entry we are up to. */
10514 current_entry_id = code->ext.entry->id;
10515 break;
10517 case EXEC_WHERE:
10518 resolve_where (code, NULL);
10519 break;
10521 case EXEC_GOTO:
10522 if (code->expr1 != NULL)
10524 if (code->expr1->ts.type != BT_INTEGER)
10525 gfc_error ("ASSIGNED GOTO statement at %L requires an "
10526 "INTEGER variable", &code->expr1->where);
10527 else if (code->expr1->symtree->n.sym->attr.assign != 1)
10528 gfc_error ("Variable %qs has not been assigned a target "
10529 "label at %L", code->expr1->symtree->n.sym->name,
10530 &code->expr1->where);
10532 else
10533 resolve_branch (code->label1, code);
10534 break;
10536 case EXEC_RETURN:
10537 if (code->expr1 != NULL
10538 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
10539 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
10540 "INTEGER return specifier", &code->expr1->where);
10541 break;
10543 case EXEC_INIT_ASSIGN:
10544 case EXEC_END_PROCEDURE:
10545 break;
10547 case EXEC_ASSIGN:
10548 if (!t)
10549 break;
10551 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
10552 the LHS. */
10553 if (code->expr1->expr_type == EXPR_FUNCTION
10554 && code->expr1->value.function.isym
10555 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10556 remove_caf_get_intrinsic (code->expr1);
10558 /* If this is a pointer function in an lvalue variable context,
10559 the new code will have to be resolved afresh. This is also the
10560 case with an error, where the code is transformed into NOP to
10561 prevent ICEs downstream. */
10562 if (resolve_ptr_fcn_assign (&code, ns)
10563 || code->op == EXEC_NOP)
10564 goto start;
10566 if (!gfc_check_vardef_context (code->expr1, false, false, false,
10567 _("assignment")))
10568 break;
10570 if (resolve_ordinary_assign (code, ns))
10572 if (code->op == EXEC_COMPCALL)
10573 goto compcall;
10574 else
10575 goto call;
10578 /* Check for dependencies in deferred character length array
10579 assignments and generate a temporary, if necessary. */
10580 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
10581 break;
10583 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
10584 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
10585 && code->expr1->ts.u.derived
10586 && code->expr1->ts.u.derived->attr.defined_assign_comp)
10587 generate_component_assignments (&code, ns);
10589 break;
10591 case EXEC_LABEL_ASSIGN:
10592 if (code->label1->defined == ST_LABEL_UNKNOWN)
10593 gfc_error ("Label %d referenced at %L is never defined",
10594 code->label1->value, &code->label1->where);
10595 if (t
10596 && (code->expr1->expr_type != EXPR_VARIABLE
10597 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
10598 || code->expr1->symtree->n.sym->ts.kind
10599 != gfc_default_integer_kind
10600 || code->expr1->symtree->n.sym->as != NULL))
10601 gfc_error ("ASSIGN statement at %L requires a scalar "
10602 "default INTEGER variable", &code->expr1->where);
10603 break;
10605 case EXEC_POINTER_ASSIGN:
10607 gfc_expr* e;
10609 if (!t)
10610 break;
10612 /* This is both a variable definition and pointer assignment
10613 context, so check both of them. For rank remapping, a final
10614 array ref may be present on the LHS and fool gfc_expr_attr
10615 used in gfc_check_vardef_context. Remove it. */
10616 e = remove_last_array_ref (code->expr1);
10617 t = gfc_check_vardef_context (e, true, false, false,
10618 _("pointer assignment"));
10619 if (t)
10620 t = gfc_check_vardef_context (e, false, false, false,
10621 _("pointer assignment"));
10622 gfc_free_expr (e);
10623 if (!t)
10624 break;
10626 gfc_check_pointer_assign (code->expr1, code->expr2);
10627 break;
10630 case EXEC_ARITHMETIC_IF:
10632 gfc_expr *e = code->expr1;
10634 gfc_resolve_expr (e);
10635 if (e->expr_type == EXPR_NULL)
10636 gfc_error ("Invalid NULL at %L", &e->where);
10638 if (t && (e->rank > 0
10639 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
10640 gfc_error ("Arithmetic IF statement at %L requires a scalar "
10641 "REAL or INTEGER expression", &e->where);
10643 resolve_branch (code->label1, code);
10644 resolve_branch (code->label2, code);
10645 resolve_branch (code->label3, code);
10647 break;
10649 case EXEC_IF:
10650 if (t && code->expr1 != NULL
10651 && (code->expr1->ts.type != BT_LOGICAL
10652 || code->expr1->rank != 0))
10653 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10654 &code->expr1->where);
10655 break;
10657 case EXEC_CALL:
10658 call:
10659 resolve_call (code);
10660 break;
10662 case EXEC_COMPCALL:
10663 compcall:
10664 resolve_typebound_subroutine (code);
10665 break;
10667 case EXEC_CALL_PPC:
10668 resolve_ppc_call (code);
10669 break;
10671 case EXEC_SELECT:
10672 /* Select is complicated. Also, a SELECT construct could be
10673 a transformed computed GOTO. */
10674 resolve_select (code, false);
10675 break;
10677 case EXEC_SELECT_TYPE:
10678 resolve_select_type (code, ns);
10679 break;
10681 case EXEC_BLOCK:
10682 resolve_block_construct (code);
10683 break;
10685 case EXEC_DO:
10686 if (code->ext.iterator != NULL)
10688 gfc_iterator *iter = code->ext.iterator;
10689 if (gfc_resolve_iterator (iter, true, false))
10690 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
10692 break;
10694 case EXEC_DO_WHILE:
10695 if (code->expr1 == NULL)
10696 gfc_internal_error ("gfc_resolve_code(): No expression on "
10697 "DO WHILE");
10698 if (t
10699 && (code->expr1->rank != 0
10700 || code->expr1->ts.type != BT_LOGICAL))
10701 gfc_error ("Exit condition of DO WHILE loop at %L must be "
10702 "a scalar LOGICAL expression", &code->expr1->where);
10703 break;
10705 case EXEC_ALLOCATE:
10706 if (t)
10707 resolve_allocate_deallocate (code, "ALLOCATE");
10709 break;
10711 case EXEC_DEALLOCATE:
10712 if (t)
10713 resolve_allocate_deallocate (code, "DEALLOCATE");
10715 break;
10717 case EXEC_OPEN:
10718 if (!gfc_resolve_open (code->ext.open))
10719 break;
10721 resolve_branch (code->ext.open->err, code);
10722 break;
10724 case EXEC_CLOSE:
10725 if (!gfc_resolve_close (code->ext.close))
10726 break;
10728 resolve_branch (code->ext.close->err, code);
10729 break;
10731 case EXEC_BACKSPACE:
10732 case EXEC_ENDFILE:
10733 case EXEC_REWIND:
10734 case EXEC_FLUSH:
10735 if (!gfc_resolve_filepos (code->ext.filepos))
10736 break;
10738 resolve_branch (code->ext.filepos->err, code);
10739 break;
10741 case EXEC_INQUIRE:
10742 if (!gfc_resolve_inquire (code->ext.inquire))
10743 break;
10745 resolve_branch (code->ext.inquire->err, code);
10746 break;
10748 case EXEC_IOLENGTH:
10749 gcc_assert (code->ext.inquire != NULL);
10750 if (!gfc_resolve_inquire (code->ext.inquire))
10751 break;
10753 resolve_branch (code->ext.inquire->err, code);
10754 break;
10756 case EXEC_WAIT:
10757 if (!gfc_resolve_wait (code->ext.wait))
10758 break;
10760 resolve_branch (code->ext.wait->err, code);
10761 resolve_branch (code->ext.wait->end, code);
10762 resolve_branch (code->ext.wait->eor, code);
10763 break;
10765 case EXEC_READ:
10766 case EXEC_WRITE:
10767 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
10768 break;
10770 resolve_branch (code->ext.dt->err, code);
10771 resolve_branch (code->ext.dt->end, code);
10772 resolve_branch (code->ext.dt->eor, code);
10773 break;
10775 case EXEC_TRANSFER:
10776 resolve_transfer (code);
10777 break;
10779 case EXEC_DO_CONCURRENT:
10780 case EXEC_FORALL:
10781 resolve_forall_iterators (code->ext.forall_iterator);
10783 if (code->expr1 != NULL
10784 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
10785 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
10786 "expression", &code->expr1->where);
10787 break;
10789 case EXEC_OACC_PARALLEL_LOOP:
10790 case EXEC_OACC_PARALLEL:
10791 case EXEC_OACC_KERNELS_LOOP:
10792 case EXEC_OACC_KERNELS:
10793 case EXEC_OACC_DATA:
10794 case EXEC_OACC_HOST_DATA:
10795 case EXEC_OACC_LOOP:
10796 case EXEC_OACC_UPDATE:
10797 case EXEC_OACC_WAIT:
10798 case EXEC_OACC_CACHE:
10799 case EXEC_OACC_ENTER_DATA:
10800 case EXEC_OACC_EXIT_DATA:
10801 case EXEC_OACC_ATOMIC:
10802 case EXEC_OACC_DECLARE:
10803 gfc_resolve_oacc_directive (code, ns);
10804 break;
10806 case EXEC_OMP_ATOMIC:
10807 case EXEC_OMP_BARRIER:
10808 case EXEC_OMP_CANCEL:
10809 case EXEC_OMP_CANCELLATION_POINT:
10810 case EXEC_OMP_CRITICAL:
10811 case EXEC_OMP_FLUSH:
10812 case EXEC_OMP_DISTRIBUTE:
10813 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10814 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10815 case EXEC_OMP_DISTRIBUTE_SIMD:
10816 case EXEC_OMP_DO:
10817 case EXEC_OMP_DO_SIMD:
10818 case EXEC_OMP_MASTER:
10819 case EXEC_OMP_ORDERED:
10820 case EXEC_OMP_SECTIONS:
10821 case EXEC_OMP_SIMD:
10822 case EXEC_OMP_SINGLE:
10823 case EXEC_OMP_TARGET:
10824 case EXEC_OMP_TARGET_DATA:
10825 case EXEC_OMP_TARGET_TEAMS:
10826 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10827 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10828 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10829 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10830 case EXEC_OMP_TARGET_UPDATE:
10831 case EXEC_OMP_TASK:
10832 case EXEC_OMP_TASKGROUP:
10833 case EXEC_OMP_TASKWAIT:
10834 case EXEC_OMP_TASKYIELD:
10835 case EXEC_OMP_TEAMS:
10836 case EXEC_OMP_TEAMS_DISTRIBUTE:
10837 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10838 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10839 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10840 case EXEC_OMP_WORKSHARE:
10841 gfc_resolve_omp_directive (code, ns);
10842 break;
10844 case EXEC_OMP_PARALLEL:
10845 case EXEC_OMP_PARALLEL_DO:
10846 case EXEC_OMP_PARALLEL_DO_SIMD:
10847 case EXEC_OMP_PARALLEL_SECTIONS:
10848 case EXEC_OMP_PARALLEL_WORKSHARE:
10849 omp_workshare_save = omp_workshare_flag;
10850 omp_workshare_flag = 0;
10851 gfc_resolve_omp_directive (code, ns);
10852 omp_workshare_flag = omp_workshare_save;
10853 break;
10855 default:
10856 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
10860 cs_base = frame.prev;
10864 /* Resolve initial values and make sure they are compatible with
10865 the variable. */
10867 static void
10868 resolve_values (gfc_symbol *sym)
10870 bool t;
10872 if (sym->value == NULL)
10873 return;
10875 if (sym->value->expr_type == EXPR_STRUCTURE)
10876 t= resolve_structure_cons (sym->value, 1);
10877 else
10878 t = gfc_resolve_expr (sym->value);
10880 if (!t)
10881 return;
10883 gfc_check_assign_symbol (sym, NULL, sym->value);
10887 /* Verify any BIND(C) derived types in the namespace so we can report errors
10888 for them once, rather than for each variable declared of that type. */
10890 static void
10891 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10893 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10894 && derived_sym->attr.is_bind_c == 1)
10895 verify_bind_c_derived_type (derived_sym);
10897 return;
10901 /* Verify that any binding labels used in a given namespace do not collide
10902 with the names or binding labels of any global symbols. Multiple INTERFACE
10903 for the same procedure are permitted. */
10905 static void
10906 gfc_verify_binding_labels (gfc_symbol *sym)
10908 gfc_gsymbol *gsym;
10909 const char *module;
10911 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
10912 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
10913 return;
10915 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10917 if (sym->module)
10918 module = sym->module;
10919 else if (sym->ns && sym->ns->proc_name
10920 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10921 module = sym->ns->proc_name->name;
10922 else if (sym->ns && sym->ns->parent
10923 && sym->ns && sym->ns->parent->proc_name
10924 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10925 module = sym->ns->parent->proc_name->name;
10926 else
10927 module = NULL;
10929 if (!gsym
10930 || (!gsym->defined
10931 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
10933 if (!gsym)
10934 gsym = gfc_get_gsymbol (sym->binding_label);
10935 gsym->where = sym->declared_at;
10936 gsym->sym_name = sym->name;
10937 gsym->binding_label = sym->binding_label;
10938 gsym->ns = sym->ns;
10939 gsym->mod_name = module;
10940 if (sym->attr.function)
10941 gsym->type = GSYM_FUNCTION;
10942 else if (sym->attr.subroutine)
10943 gsym->type = GSYM_SUBROUTINE;
10944 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
10945 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
10946 return;
10949 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
10951 gfc_error ("Variable %s with binding label %s at %L uses the same global "
10952 "identifier as entity at %L", sym->name,
10953 sym->binding_label, &sym->declared_at, &gsym->where);
10954 /* Clear the binding label to prevent checking multiple times. */
10955 sym->binding_label = NULL;
10958 else if (sym->attr.flavor == FL_VARIABLE && module
10959 && (strcmp (module, gsym->mod_name) != 0
10960 || strcmp (sym->name, gsym->sym_name) != 0))
10962 /* This can only happen if the variable is defined in a module - if it
10963 isn't the same module, reject it. */
10964 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
10965 "the same global identifier as entity at %L from module %s",
10966 sym->name, module, sym->binding_label,
10967 &sym->declared_at, &gsym->where, gsym->mod_name);
10968 sym->binding_label = NULL;
10970 else if ((sym->attr.function || sym->attr.subroutine)
10971 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
10972 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
10973 && sym != gsym->ns->proc_name
10974 && (module != gsym->mod_name
10975 || strcmp (gsym->sym_name, sym->name) != 0
10976 || (module && strcmp (module, gsym->mod_name) != 0)))
10978 /* Print an error if the procedure is defined multiple times; we have to
10979 exclude references to the same procedure via module association or
10980 multiple checks for the same procedure. */
10981 gfc_error ("Procedure %s with binding label %s at %L uses the same "
10982 "global identifier as entity at %L", sym->name,
10983 sym->binding_label, &sym->declared_at, &gsym->where);
10984 sym->binding_label = NULL;
10989 /* Resolve an index expression. */
10991 static bool
10992 resolve_index_expr (gfc_expr *e)
10994 if (!gfc_resolve_expr (e))
10995 return false;
10997 if (!gfc_simplify_expr (e, 0))
10998 return false;
11000 if (!gfc_specification_expr (e))
11001 return false;
11003 return true;
11007 /* Resolve a charlen structure. */
11009 static bool
11010 resolve_charlen (gfc_charlen *cl)
11012 int i, k;
11013 bool saved_specification_expr;
11015 if (cl->resolved)
11016 return true;
11018 cl->resolved = 1;
11019 saved_specification_expr = specification_expr;
11020 specification_expr = true;
11022 if (cl->length_from_typespec)
11024 if (!gfc_resolve_expr (cl->length))
11026 specification_expr = saved_specification_expr;
11027 return false;
11030 if (!gfc_simplify_expr (cl->length, 0))
11032 specification_expr = saved_specification_expr;
11033 return false;
11036 else
11039 if (!resolve_index_expr (cl->length))
11041 specification_expr = saved_specification_expr;
11042 return false;
11046 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
11047 a negative value, the length of character entities declared is zero. */
11048 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
11049 gfc_replace_expr (cl->length,
11050 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
11052 /* Check that the character length is not too large. */
11053 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
11054 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
11055 && cl->length->ts.type == BT_INTEGER
11056 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
11058 gfc_error ("String length at %L is too large", &cl->length->where);
11059 specification_expr = saved_specification_expr;
11060 return false;
11063 specification_expr = saved_specification_expr;
11064 return true;
11068 /* Test for non-constant shape arrays. */
11070 static bool
11071 is_non_constant_shape_array (gfc_symbol *sym)
11073 gfc_expr *e;
11074 int i;
11075 bool not_constant;
11077 not_constant = false;
11078 if (sym->as != NULL)
11080 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
11081 has not been simplified; parameter array references. Do the
11082 simplification now. */
11083 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
11085 e = sym->as->lower[i];
11086 if (e && (!resolve_index_expr(e)
11087 || !gfc_is_constant_expr (e)))
11088 not_constant = true;
11089 e = sym->as->upper[i];
11090 if (e && (!resolve_index_expr(e)
11091 || !gfc_is_constant_expr (e)))
11092 not_constant = true;
11095 return not_constant;
11098 /* Given a symbol and an initialization expression, add code to initialize
11099 the symbol to the function entry. */
11100 static void
11101 build_init_assign (gfc_symbol *sym, gfc_expr *init)
11103 gfc_expr *lval;
11104 gfc_code *init_st;
11105 gfc_namespace *ns = sym->ns;
11107 /* Search for the function namespace if this is a contained
11108 function without an explicit result. */
11109 if (sym->attr.function && sym == sym->result
11110 && sym->name != sym->ns->proc_name->name)
11112 ns = ns->contained;
11113 for (;ns; ns = ns->sibling)
11114 if (strcmp (ns->proc_name->name, sym->name) == 0)
11115 break;
11118 if (ns == NULL)
11120 gfc_free_expr (init);
11121 return;
11124 /* Build an l-value expression for the result. */
11125 lval = gfc_lval_expr_from_sym (sym);
11127 /* Add the code at scope entry. */
11128 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
11129 init_st->next = ns->code;
11130 ns->code = init_st;
11132 /* Assign the default initializer to the l-value. */
11133 init_st->loc = sym->declared_at;
11134 init_st->expr1 = lval;
11135 init_st->expr2 = init;
11138 /* Assign the default initializer to a derived type variable or result. */
11140 static void
11141 apply_default_init (gfc_symbol *sym)
11143 gfc_expr *init = NULL;
11145 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11146 return;
11148 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
11149 init = gfc_default_initializer (&sym->ts);
11151 if (init == NULL && sym->ts.type != BT_CLASS)
11152 return;
11154 build_init_assign (sym, init);
11155 sym->attr.referenced = 1;
11158 /* Build an initializer for a local integer, real, complex, logical, or
11159 character variable, based on the command line flags finit-local-zero,
11160 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
11161 null if the symbol should not have a default initialization. */
11162 static gfc_expr *
11163 build_default_init_expr (gfc_symbol *sym)
11165 int char_len;
11166 gfc_expr *init_expr;
11167 int i;
11169 /* These symbols should never have a default initialization. */
11170 if (sym->attr.allocatable
11171 || sym->attr.external
11172 || sym->attr.dummy
11173 || sym->attr.pointer
11174 || sym->attr.in_equivalence
11175 || sym->attr.in_common
11176 || sym->attr.data
11177 || sym->module
11178 || sym->attr.cray_pointee
11179 || sym->attr.cray_pointer
11180 || sym->assoc)
11181 return NULL;
11183 /* Now we'll try to build an initializer expression. */
11184 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
11185 &sym->declared_at);
11187 /* We will only initialize integers, reals, complex, logicals, and
11188 characters, and only if the corresponding command-line flags
11189 were set. Otherwise, we free init_expr and return null. */
11190 switch (sym->ts.type)
11192 case BT_INTEGER:
11193 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
11194 mpz_set_si (init_expr->value.integer,
11195 gfc_option.flag_init_integer_value);
11196 else
11198 gfc_free_expr (init_expr);
11199 init_expr = NULL;
11201 break;
11203 case BT_REAL:
11204 switch (flag_init_real)
11206 case GFC_INIT_REAL_SNAN:
11207 init_expr->is_snan = 1;
11208 /* Fall through. */
11209 case GFC_INIT_REAL_NAN:
11210 mpfr_set_nan (init_expr->value.real);
11211 break;
11213 case GFC_INIT_REAL_INF:
11214 mpfr_set_inf (init_expr->value.real, 1);
11215 break;
11217 case GFC_INIT_REAL_NEG_INF:
11218 mpfr_set_inf (init_expr->value.real, -1);
11219 break;
11221 case GFC_INIT_REAL_ZERO:
11222 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
11223 break;
11225 default:
11226 gfc_free_expr (init_expr);
11227 init_expr = NULL;
11228 break;
11230 break;
11232 case BT_COMPLEX:
11233 switch (flag_init_real)
11235 case GFC_INIT_REAL_SNAN:
11236 init_expr->is_snan = 1;
11237 /* Fall through. */
11238 case GFC_INIT_REAL_NAN:
11239 mpfr_set_nan (mpc_realref (init_expr->value.complex));
11240 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
11241 break;
11243 case GFC_INIT_REAL_INF:
11244 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
11245 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
11246 break;
11248 case GFC_INIT_REAL_NEG_INF:
11249 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
11250 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
11251 break;
11253 case GFC_INIT_REAL_ZERO:
11254 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
11255 break;
11257 default:
11258 gfc_free_expr (init_expr);
11259 init_expr = NULL;
11260 break;
11262 break;
11264 case BT_LOGICAL:
11265 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
11266 init_expr->value.logical = 0;
11267 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
11268 init_expr->value.logical = 1;
11269 else
11271 gfc_free_expr (init_expr);
11272 init_expr = NULL;
11274 break;
11276 case BT_CHARACTER:
11277 /* For characters, the length must be constant in order to
11278 create a default initializer. */
11279 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
11280 && sym->ts.u.cl->length
11281 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11283 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
11284 init_expr->value.character.length = char_len;
11285 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
11286 for (i = 0; i < char_len; i++)
11287 init_expr->value.character.string[i]
11288 = (unsigned char) gfc_option.flag_init_character_value;
11290 else
11292 gfc_free_expr (init_expr);
11293 init_expr = NULL;
11295 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
11296 && sym->ts.u.cl->length && flag_max_stack_var_size != 0)
11298 gfc_actual_arglist *arg;
11299 init_expr = gfc_get_expr ();
11300 init_expr->where = sym->declared_at;
11301 init_expr->ts = sym->ts;
11302 init_expr->expr_type = EXPR_FUNCTION;
11303 init_expr->value.function.isym =
11304 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
11305 init_expr->value.function.name = "repeat";
11306 arg = gfc_get_actual_arglist ();
11307 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
11308 NULL, 1);
11309 arg->expr->value.character.string[0]
11310 = gfc_option.flag_init_character_value;
11311 arg->next = gfc_get_actual_arglist ();
11312 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
11313 init_expr->value.function.actual = arg;
11315 break;
11317 default:
11318 gfc_free_expr (init_expr);
11319 init_expr = NULL;
11321 return init_expr;
11324 /* Add an initialization expression to a local variable. */
11325 static void
11326 apply_default_init_local (gfc_symbol *sym)
11328 gfc_expr *init = NULL;
11330 /* The symbol should be a variable or a function return value. */
11331 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11332 || (sym->attr.function && sym->result != sym))
11333 return;
11335 /* Try to build the initializer expression. If we can't initialize
11336 this symbol, then init will be NULL. */
11337 init = build_default_init_expr (sym);
11338 if (init == NULL)
11339 return;
11341 /* For saved variables, we don't want to add an initializer at function
11342 entry, so we just add a static initializer. Note that automatic variables
11343 are stack allocated even with -fno-automatic; we have also to exclude
11344 result variable, which are also nonstatic. */
11345 if (sym->attr.save || sym->ns->save_all
11346 || (flag_max_stack_var_size == 0 && !sym->attr.result
11347 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
11348 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
11350 /* Don't clobber an existing initializer! */
11351 gcc_assert (sym->value == NULL);
11352 sym->value = init;
11353 return;
11356 build_init_assign (sym, init);
11360 /* Resolution of common features of flavors variable and procedure. */
11362 static bool
11363 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
11365 gfc_array_spec *as;
11367 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11368 as = CLASS_DATA (sym)->as;
11369 else
11370 as = sym->as;
11372 /* Constraints on deferred shape variable. */
11373 if (as == NULL || as->type != AS_DEFERRED)
11375 bool pointer, allocatable, dimension;
11377 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11379 pointer = CLASS_DATA (sym)->attr.class_pointer;
11380 allocatable = CLASS_DATA (sym)->attr.allocatable;
11381 dimension = CLASS_DATA (sym)->attr.dimension;
11383 else
11385 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
11386 allocatable = sym->attr.allocatable;
11387 dimension = sym->attr.dimension;
11390 if (allocatable)
11392 if (dimension && as->type != AS_ASSUMED_RANK)
11394 gfc_error ("Allocatable array %qs at %L must have a deferred "
11395 "shape or assumed rank", sym->name, &sym->declared_at);
11396 return false;
11398 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
11399 "%qs at %L may not be ALLOCATABLE",
11400 sym->name, &sym->declared_at))
11401 return false;
11404 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
11406 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
11407 "assumed rank", sym->name, &sym->declared_at);
11408 return false;
11411 else
11413 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
11414 && sym->ts.type != BT_CLASS && !sym->assoc)
11416 gfc_error ("Array %qs at %L cannot have a deferred shape",
11417 sym->name, &sym->declared_at);
11418 return false;
11422 /* Constraints on polymorphic variables. */
11423 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
11425 /* F03:C502. */
11426 if (sym->attr.class_ok
11427 && !sym->attr.select_type_temporary
11428 && !UNLIMITED_POLY (sym)
11429 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
11431 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
11432 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
11433 &sym->declared_at);
11434 return false;
11437 /* F03:C509. */
11438 /* Assume that use associated symbols were checked in the module ns.
11439 Class-variables that are associate-names are also something special
11440 and excepted from the test. */
11441 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
11443 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
11444 "or pointer", sym->name, &sym->declared_at);
11445 return false;
11449 return true;
11453 /* Additional checks for symbols with flavor variable and derived
11454 type. To be called from resolve_fl_variable. */
11456 static bool
11457 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
11459 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
11461 /* Check to see if a derived type is blocked from being host
11462 associated by the presence of another class I symbol in the same
11463 namespace. 14.6.1.3 of the standard and the discussion on
11464 comp.lang.fortran. */
11465 if (sym->ns != sym->ts.u.derived->ns
11466 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
11468 gfc_symbol *s;
11469 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
11470 if (s && s->attr.generic)
11471 s = gfc_find_dt_in_generic (s);
11472 if (s && !gfc_fl_struct (s->attr.flavor))
11474 gfc_error ("The type %qs cannot be host associated at %L "
11475 "because it is blocked by an incompatible object "
11476 "of the same name declared at %L",
11477 sym->ts.u.derived->name, &sym->declared_at,
11478 &s->declared_at);
11479 return false;
11483 /* 4th constraint in section 11.3: "If an object of a type for which
11484 component-initialization is specified (R429) appears in the
11485 specification-part of a module and does not have the ALLOCATABLE
11486 or POINTER attribute, the object shall have the SAVE attribute."
11488 The check for initializers is performed with
11489 gfc_has_default_initializer because gfc_default_initializer generates
11490 a hidden default for allocatable components. */
11491 if (!(sym->value || no_init_flag) && sym->ns->proc_name
11492 && sym->ns->proc_name->attr.flavor == FL_MODULE
11493 && !sym->ns->save_all && !sym->attr.save
11494 && !sym->attr.pointer && !sym->attr.allocatable
11495 && gfc_has_default_initializer (sym->ts.u.derived)
11496 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
11497 "%qs at %L, needed due to the default "
11498 "initialization", sym->name, &sym->declared_at))
11499 return false;
11501 /* Assign default initializer. */
11502 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
11503 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
11505 sym->value = gfc_default_initializer (&sym->ts);
11508 return true;
11512 /* Resolve symbols with flavor variable. */
11514 static bool
11515 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
11517 int no_init_flag, automatic_flag;
11518 gfc_expr *e;
11519 const char *auto_save_msg;
11520 bool saved_specification_expr;
11522 auto_save_msg = "Automatic object %qs at %L cannot have the "
11523 "SAVE attribute";
11525 if (!resolve_fl_var_and_proc (sym, mp_flag))
11526 return false;
11528 /* Set this flag to check that variables are parameters of all entries.
11529 This check is effected by the call to gfc_resolve_expr through
11530 is_non_constant_shape_array. */
11531 saved_specification_expr = specification_expr;
11532 specification_expr = true;
11534 if (sym->ns->proc_name
11535 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11536 || sym->ns->proc_name->attr.is_main_program)
11537 && !sym->attr.use_assoc
11538 && !sym->attr.allocatable
11539 && !sym->attr.pointer
11540 && is_non_constant_shape_array (sym))
11542 /* The shape of a main program or module array needs to be
11543 constant. */
11544 gfc_error ("The module or main program array %qs at %L must "
11545 "have constant shape", sym->name, &sym->declared_at);
11546 specification_expr = saved_specification_expr;
11547 return false;
11550 /* Constraints on deferred type parameter. */
11551 if (sym->ts.deferred
11552 && !(sym->attr.pointer
11553 || sym->attr.allocatable
11554 || sym->attr.omp_udr_artificial_var))
11556 gfc_error ("Entity %qs at %L has a deferred type parameter and "
11557 "requires either the pointer or allocatable attribute",
11558 sym->name, &sym->declared_at);
11559 specification_expr = saved_specification_expr;
11560 return false;
11563 if (sym->ts.type == BT_CHARACTER)
11565 /* Make sure that character string variables with assumed length are
11566 dummy arguments. */
11567 e = sym->ts.u.cl->length;
11568 if (e == NULL && !sym->attr.dummy && !sym->attr.result
11569 && !sym->ts.deferred && !sym->attr.select_type_temporary
11570 && !sym->attr.omp_udr_artificial_var)
11572 gfc_error ("Entity with assumed character length at %L must be a "
11573 "dummy argument or a PARAMETER", &sym->declared_at);
11574 specification_expr = saved_specification_expr;
11575 return false;
11578 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
11580 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
11581 specification_expr = saved_specification_expr;
11582 return false;
11585 if (!gfc_is_constant_expr (e)
11586 && !(e->expr_type == EXPR_VARIABLE
11587 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
11589 if (!sym->attr.use_assoc && sym->ns->proc_name
11590 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11591 || sym->ns->proc_name->attr.is_main_program))
11593 gfc_error ("%qs at %L must have constant character length "
11594 "in this context", sym->name, &sym->declared_at);
11595 specification_expr = saved_specification_expr;
11596 return false;
11598 if (sym->attr.in_common)
11600 gfc_error ("COMMON variable %qs at %L must have constant "
11601 "character length", sym->name, &sym->declared_at);
11602 specification_expr = saved_specification_expr;
11603 return false;
11608 if (sym->value == NULL && sym->attr.referenced)
11609 apply_default_init_local (sym); /* Try to apply a default initialization. */
11611 /* Determine if the symbol may not have an initializer. */
11612 no_init_flag = automatic_flag = 0;
11613 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
11614 || sym->attr.intrinsic || sym->attr.result)
11615 no_init_flag = 1;
11616 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
11617 && is_non_constant_shape_array (sym))
11619 no_init_flag = automatic_flag = 1;
11621 /* Also, they must not have the SAVE attribute.
11622 SAVE_IMPLICIT is checked below. */
11623 if (sym->as && sym->attr.codimension)
11625 int corank = sym->as->corank;
11626 sym->as->corank = 0;
11627 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
11628 sym->as->corank = corank;
11630 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
11632 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
11633 specification_expr = saved_specification_expr;
11634 return false;
11638 /* Ensure that any initializer is simplified. */
11639 if (sym->value)
11640 gfc_simplify_expr (sym->value, 1);
11642 /* Reject illegal initializers. */
11643 if (!sym->mark && sym->value)
11645 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
11646 && CLASS_DATA (sym)->attr.allocatable))
11647 gfc_error ("Allocatable %qs at %L cannot have an initializer",
11648 sym->name, &sym->declared_at);
11649 else if (sym->attr.external)
11650 gfc_error ("External %qs at %L cannot have an initializer",
11651 sym->name, &sym->declared_at);
11652 else if (sym->attr.dummy
11653 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
11654 gfc_error ("Dummy %qs at %L cannot have an initializer",
11655 sym->name, &sym->declared_at);
11656 else if (sym->attr.intrinsic)
11657 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
11658 sym->name, &sym->declared_at);
11659 else if (sym->attr.result)
11660 gfc_error ("Function result %qs at %L cannot have an initializer",
11661 sym->name, &sym->declared_at);
11662 else if (automatic_flag)
11663 gfc_error ("Automatic array %qs at %L cannot have an initializer",
11664 sym->name, &sym->declared_at);
11665 else
11666 goto no_init_error;
11667 specification_expr = saved_specification_expr;
11668 return false;
11671 no_init_error:
11672 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
11674 bool res = resolve_fl_variable_derived (sym, no_init_flag);
11675 specification_expr = saved_specification_expr;
11676 return res;
11679 specification_expr = saved_specification_expr;
11680 return true;
11684 /* Compare the dummy characteristics of a module procedure interface
11685 declaration with the corresponding declaration in a submodule. */
11686 static gfc_formal_arglist *new_formal;
11687 static char errmsg[200];
11689 static void
11690 compare_fsyms (gfc_symbol *sym)
11692 gfc_symbol *fsym;
11694 if (sym == NULL || new_formal == NULL)
11695 return;
11697 fsym = new_formal->sym;
11699 if (sym == fsym)
11700 return;
11702 if (strcmp (sym->name, fsym->name) == 0)
11704 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
11705 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
11710 /* Resolve a procedure. */
11712 static bool
11713 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
11715 gfc_formal_arglist *arg;
11717 if (sym->attr.function
11718 && !resolve_fl_var_and_proc (sym, mp_flag))
11719 return false;
11721 if (sym->ts.type == BT_CHARACTER)
11723 gfc_charlen *cl = sym->ts.u.cl;
11725 if (cl && cl->length && gfc_is_constant_expr (cl->length)
11726 && !resolve_charlen (cl))
11727 return false;
11729 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11730 && sym->attr.proc == PROC_ST_FUNCTION)
11732 gfc_error ("Character-valued statement function %qs at %L must "
11733 "have constant length", sym->name, &sym->declared_at);
11734 return false;
11738 /* Ensure that derived type for are not of a private type. Internal
11739 module procedures are excluded by 2.2.3.3 - i.e., they are not
11740 externally accessible and can access all the objects accessible in
11741 the host. */
11742 if (!(sym->ns->parent
11743 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
11744 && gfc_check_symbol_access (sym))
11746 gfc_interface *iface;
11748 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
11750 if (arg->sym
11751 && arg->sym->ts.type == BT_DERIVED
11752 && !arg->sym->ts.u.derived->attr.use_assoc
11753 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11754 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
11755 "and cannot be a dummy argument"
11756 " of %qs, which is PUBLIC at %L",
11757 arg->sym->name, sym->name,
11758 &sym->declared_at))
11760 /* Stop this message from recurring. */
11761 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11762 return false;
11766 /* PUBLIC interfaces may expose PRIVATE procedures that take types
11767 PRIVATE to the containing module. */
11768 for (iface = sym->generic; iface; iface = iface->next)
11770 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
11772 if (arg->sym
11773 && arg->sym->ts.type == BT_DERIVED
11774 && !arg->sym->ts.u.derived->attr.use_assoc
11775 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11776 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
11777 "PUBLIC interface %qs at %L "
11778 "takes dummy arguments of %qs which "
11779 "is PRIVATE", iface->sym->name,
11780 sym->name, &iface->sym->declared_at,
11781 gfc_typename(&arg->sym->ts)))
11783 /* Stop this message from recurring. */
11784 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11785 return false;
11791 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
11792 && !sym->attr.proc_pointer)
11794 gfc_error ("Function %qs at %L cannot have an initializer",
11795 sym->name, &sym->declared_at);
11796 return false;
11799 /* An external symbol may not have an initializer because it is taken to be
11800 a procedure. Exception: Procedure Pointers. */
11801 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
11803 gfc_error ("External object %qs at %L may not have an initializer",
11804 sym->name, &sym->declared_at);
11805 return false;
11808 /* An elemental function is required to return a scalar 12.7.1 */
11809 if (sym->attr.elemental && sym->attr.function && sym->as)
11811 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
11812 "result", sym->name, &sym->declared_at);
11813 /* Reset so that the error only occurs once. */
11814 sym->attr.elemental = 0;
11815 return false;
11818 if (sym->attr.proc == PROC_ST_FUNCTION
11819 && (sym->attr.allocatable || sym->attr.pointer))
11821 gfc_error ("Statement function %qs at %L may not have pointer or "
11822 "allocatable attribute", sym->name, &sym->declared_at);
11823 return false;
11826 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
11827 char-len-param shall not be array-valued, pointer-valued, recursive
11828 or pure. ....snip... A character value of * may only be used in the
11829 following ways: (i) Dummy arg of procedure - dummy associates with
11830 actual length; (ii) To declare a named constant; or (iii) External
11831 function - but length must be declared in calling scoping unit. */
11832 if (sym->attr.function
11833 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
11834 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
11836 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
11837 || (sym->attr.recursive) || (sym->attr.pure))
11839 if (sym->as && sym->as->rank)
11840 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11841 "array-valued", sym->name, &sym->declared_at);
11843 if (sym->attr.pointer)
11844 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11845 "pointer-valued", sym->name, &sym->declared_at);
11847 if (sym->attr.pure)
11848 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11849 "pure", sym->name, &sym->declared_at);
11851 if (sym->attr.recursive)
11852 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11853 "recursive", sym->name, &sym->declared_at);
11855 return false;
11858 /* Appendix B.2 of the standard. Contained functions give an
11859 error anyway. Deferred character length is an F2003 feature.
11860 Don't warn on intrinsic conversion functions, which start
11861 with two underscores. */
11862 if (!sym->attr.contained && !sym->ts.deferred
11863 && (sym->name[0] != '_' || sym->name[1] != '_'))
11864 gfc_notify_std (GFC_STD_F95_OBS,
11865 "CHARACTER(*) function %qs at %L",
11866 sym->name, &sym->declared_at);
11869 /* F2008, C1218. */
11870 if (sym->attr.elemental)
11872 if (sym->attr.proc_pointer)
11874 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
11875 sym->name, &sym->declared_at);
11876 return false;
11878 if (sym->attr.dummy)
11880 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
11881 sym->name, &sym->declared_at);
11882 return false;
11886 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11888 gfc_formal_arglist *curr_arg;
11889 int has_non_interop_arg = 0;
11891 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11892 sym->common_block))
11894 /* Clear these to prevent looking at them again if there was an
11895 error. */
11896 sym->attr.is_bind_c = 0;
11897 sym->attr.is_c_interop = 0;
11898 sym->ts.is_c_interop = 0;
11900 else
11902 /* So far, no errors have been found. */
11903 sym->attr.is_c_interop = 1;
11904 sym->ts.is_c_interop = 1;
11907 curr_arg = gfc_sym_get_dummy_args (sym);
11908 while (curr_arg != NULL)
11910 /* Skip implicitly typed dummy args here. */
11911 if (curr_arg->sym->attr.implicit_type == 0)
11912 if (!gfc_verify_c_interop_param (curr_arg->sym))
11913 /* If something is found to fail, record the fact so we
11914 can mark the symbol for the procedure as not being
11915 BIND(C) to try and prevent multiple errors being
11916 reported. */
11917 has_non_interop_arg = 1;
11919 curr_arg = curr_arg->next;
11922 /* See if any of the arguments were not interoperable and if so, clear
11923 the procedure symbol to prevent duplicate error messages. */
11924 if (has_non_interop_arg != 0)
11926 sym->attr.is_c_interop = 0;
11927 sym->ts.is_c_interop = 0;
11928 sym->attr.is_bind_c = 0;
11932 if (!sym->attr.proc_pointer)
11934 if (sym->attr.save == SAVE_EXPLICIT)
11936 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11937 "in %qs at %L", sym->name, &sym->declared_at);
11938 return false;
11940 if (sym->attr.intent)
11942 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11943 "in %qs at %L", sym->name, &sym->declared_at);
11944 return false;
11946 if (sym->attr.subroutine && sym->attr.result)
11948 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11949 "in %qs at %L", sym->name, &sym->declared_at);
11950 return false;
11952 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
11953 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11954 || sym->attr.contained))
11956 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11957 "in %qs at %L", sym->name, &sym->declared_at);
11958 return false;
11960 if (strcmp ("ppr@", sym->name) == 0)
11962 gfc_error ("Procedure pointer result %qs at %L "
11963 "is missing the pointer attribute",
11964 sym->ns->proc_name->name, &sym->declared_at);
11965 return false;
11969 /* Assume that a procedure whose body is not known has references
11970 to external arrays. */
11971 if (sym->attr.if_source != IFSRC_DECL)
11972 sym->attr.array_outer_dependency = 1;
11974 /* Compare the characteristics of a module procedure with the
11975 interface declaration. Ideally this would be done with
11976 gfc_compare_interfaces but, at present, the formal interface
11977 cannot be copied to the ts.interface. */
11978 if (sym->attr.module_procedure
11979 && sym->attr.if_source == IFSRC_DECL)
11981 gfc_symbol *iface;
11982 char name[2*GFC_MAX_SYMBOL_LEN + 1];
11983 char *module_name;
11984 char *submodule_name;
11985 strcpy (name, sym->ns->proc_name->name);
11986 module_name = strtok (name, ".");
11987 submodule_name = strtok (NULL, ".");
11989 /* Stop the dummy characteristics test from using the interface
11990 symbol instead of 'sym'. */
11991 iface = sym->ts.interface;
11992 sym->ts.interface = NULL;
11994 if (iface == NULL)
11995 goto check_formal;
11997 /* Check the procedure characteristics. */
11998 if (sym->attr.elemental != iface->attr.elemental)
12000 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
12001 "PROCEDURE at %L and its interface in %s",
12002 &sym->declared_at, module_name);
12003 return false;
12006 if (sym->attr.pure != iface->attr.pure)
12008 gfc_error ("Mismatch in PURE attribute between MODULE "
12009 "PROCEDURE at %L and its interface in %s",
12010 &sym->declared_at, module_name);
12011 return false;
12014 if (sym->attr.recursive != iface->attr.recursive)
12016 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
12017 "PROCEDURE at %L and its interface in %s",
12018 &sym->declared_at, module_name);
12019 return false;
12022 /* Check the result characteristics. */
12023 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
12025 gfc_error ("%s between the MODULE PROCEDURE declaration "
12026 "in module %s and the declaration at %L in "
12027 "SUBMODULE %s", errmsg, module_name,
12028 &sym->declared_at, submodule_name);
12029 return false;
12032 check_formal:
12033 /* Check the charcateristics of the formal arguments. */
12034 if (sym->formal && sym->formal_ns)
12036 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
12038 new_formal = arg;
12039 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
12043 sym->ts.interface = iface;
12045 return true;
12049 /* Resolve a list of finalizer procedures. That is, after they have hopefully
12050 been defined and we now know their defined arguments, check that they fulfill
12051 the requirements of the standard for procedures used as finalizers. */
12053 static bool
12054 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
12056 gfc_finalizer* list;
12057 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
12058 bool result = true;
12059 bool seen_scalar = false;
12060 gfc_symbol *vtab;
12061 gfc_component *c;
12062 gfc_symbol *parent = gfc_get_derived_super_type (derived);
12064 if (parent)
12065 gfc_resolve_finalizers (parent, finalizable);
12067 /* Return early when not finalizable. Additionally, ensure that derived-type
12068 components have a their finalizables resolved. */
12069 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
12071 bool has_final = false;
12072 for (c = derived->components; c; c = c->next)
12073 if (c->ts.type == BT_DERIVED
12074 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
12076 bool has_final2 = false;
12077 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final))
12078 return false; /* Error. */
12079 has_final = has_final || has_final2;
12081 if (!has_final)
12083 if (finalizable)
12084 *finalizable = false;
12085 return true;
12089 /* Walk over the list of finalizer-procedures, check them, and if any one
12090 does not fit in with the standard's definition, print an error and remove
12091 it from the list. */
12092 prev_link = &derived->f2k_derived->finalizers;
12093 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
12095 gfc_formal_arglist *dummy_args;
12096 gfc_symbol* arg;
12097 gfc_finalizer* i;
12098 int my_rank;
12100 /* Skip this finalizer if we already resolved it. */
12101 if (list->proc_tree)
12103 prev_link = &(list->next);
12104 continue;
12107 /* Check this exists and is a SUBROUTINE. */
12108 if (!list->proc_sym->attr.subroutine)
12110 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
12111 list->proc_sym->name, &list->where);
12112 goto error;
12115 /* We should have exactly one argument. */
12116 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
12117 if (!dummy_args || dummy_args->next)
12119 gfc_error ("FINAL procedure at %L must have exactly one argument",
12120 &list->where);
12121 goto error;
12123 arg = dummy_args->sym;
12125 /* This argument must be of our type. */
12126 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
12128 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
12129 &arg->declared_at, derived->name);
12130 goto error;
12133 /* It must neither be a pointer nor allocatable nor optional. */
12134 if (arg->attr.pointer)
12136 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
12137 &arg->declared_at);
12138 goto error;
12140 if (arg->attr.allocatable)
12142 gfc_error ("Argument of FINAL procedure at %L must not be"
12143 " ALLOCATABLE", &arg->declared_at);
12144 goto error;
12146 if (arg->attr.optional)
12148 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
12149 &arg->declared_at);
12150 goto error;
12153 /* It must not be INTENT(OUT). */
12154 if (arg->attr.intent == INTENT_OUT)
12156 gfc_error ("Argument of FINAL procedure at %L must not be"
12157 " INTENT(OUT)", &arg->declared_at);
12158 goto error;
12161 /* Warn if the procedure is non-scalar and not assumed shape. */
12162 if (warn_surprising && arg->as && arg->as->rank != 0
12163 && arg->as->type != AS_ASSUMED_SHAPE)
12164 gfc_warning (OPT_Wsurprising,
12165 "Non-scalar FINAL procedure at %L should have assumed"
12166 " shape argument", &arg->declared_at);
12168 /* Check that it does not match in kind and rank with a FINAL procedure
12169 defined earlier. To really loop over the *earlier* declarations,
12170 we need to walk the tail of the list as new ones were pushed at the
12171 front. */
12172 /* TODO: Handle kind parameters once they are implemented. */
12173 my_rank = (arg->as ? arg->as->rank : 0);
12174 for (i = list->next; i; i = i->next)
12176 gfc_formal_arglist *dummy_args;
12178 /* Argument list might be empty; that is an error signalled earlier,
12179 but we nevertheless continued resolving. */
12180 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
12181 if (dummy_args)
12183 gfc_symbol* i_arg = dummy_args->sym;
12184 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
12185 if (i_rank == my_rank)
12187 gfc_error ("FINAL procedure %qs declared at %L has the same"
12188 " rank (%d) as %qs",
12189 list->proc_sym->name, &list->where, my_rank,
12190 i->proc_sym->name);
12191 goto error;
12196 /* Is this the/a scalar finalizer procedure? */
12197 if (!arg->as || arg->as->rank == 0)
12198 seen_scalar = true;
12200 /* Find the symtree for this procedure. */
12201 gcc_assert (!list->proc_tree);
12202 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
12204 prev_link = &list->next;
12205 continue;
12207 /* Remove wrong nodes immediately from the list so we don't risk any
12208 troubles in the future when they might fail later expectations. */
12209 error:
12210 i = list;
12211 *prev_link = list->next;
12212 gfc_free_finalizer (i);
12213 result = false;
12216 if (result == false)
12217 return false;
12219 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
12220 were nodes in the list, must have been for arrays. It is surely a good
12221 idea to have a scalar version there if there's something to finalize. */
12222 if (warn_surprising && result && !seen_scalar)
12223 gfc_warning (OPT_Wsurprising,
12224 "Only array FINAL procedures declared for derived type %qs"
12225 " defined at %L, suggest also scalar one",
12226 derived->name, &derived->declared_at);
12228 vtab = gfc_find_derived_vtab (derived);
12229 c = vtab->ts.u.derived->components->next->next->next->next->next;
12230 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
12232 if (finalizable)
12233 *finalizable = true;
12235 return true;
12239 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
12241 static bool
12242 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
12243 const char* generic_name, locus where)
12245 gfc_symbol *sym1, *sym2;
12246 const char *pass1, *pass2;
12247 gfc_formal_arglist *dummy_args;
12249 gcc_assert (t1->specific && t2->specific);
12250 gcc_assert (!t1->specific->is_generic);
12251 gcc_assert (!t2->specific->is_generic);
12252 gcc_assert (t1->is_operator == t2->is_operator);
12254 sym1 = t1->specific->u.specific->n.sym;
12255 sym2 = t2->specific->u.specific->n.sym;
12257 if (sym1 == sym2)
12258 return true;
12260 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
12261 if (sym1->attr.subroutine != sym2->attr.subroutine
12262 || sym1->attr.function != sym2->attr.function)
12264 gfc_error ("%qs and %qs can't be mixed FUNCTION/SUBROUTINE for"
12265 " GENERIC %qs at %L",
12266 sym1->name, sym2->name, generic_name, &where);
12267 return false;
12270 /* Determine PASS arguments. */
12271 if (t1->specific->nopass)
12272 pass1 = NULL;
12273 else if (t1->specific->pass_arg)
12274 pass1 = t1->specific->pass_arg;
12275 else
12277 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
12278 if (dummy_args)
12279 pass1 = dummy_args->sym->name;
12280 else
12281 pass1 = NULL;
12283 if (t2->specific->nopass)
12284 pass2 = NULL;
12285 else if (t2->specific->pass_arg)
12286 pass2 = t2->specific->pass_arg;
12287 else
12289 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
12290 if (dummy_args)
12291 pass2 = dummy_args->sym->name;
12292 else
12293 pass2 = NULL;
12296 /* Compare the interfaces. */
12297 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
12298 NULL, 0, pass1, pass2))
12300 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
12301 sym1->name, sym2->name, generic_name, &where);
12302 return false;
12305 return true;
12309 /* Worker function for resolving a generic procedure binding; this is used to
12310 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
12312 The difference between those cases is finding possible inherited bindings
12313 that are overridden, as one has to look for them in tb_sym_root,
12314 tb_uop_root or tb_op, respectively. Thus the caller must already find
12315 the super-type and set p->overridden correctly. */
12317 static bool
12318 resolve_tb_generic_targets (gfc_symbol* super_type,
12319 gfc_typebound_proc* p, const char* name)
12321 gfc_tbp_generic* target;
12322 gfc_symtree* first_target;
12323 gfc_symtree* inherited;
12325 gcc_assert (p && p->is_generic);
12327 /* Try to find the specific bindings for the symtrees in our target-list. */
12328 gcc_assert (p->u.generic);
12329 for (target = p->u.generic; target; target = target->next)
12330 if (!target->specific)
12332 gfc_typebound_proc* overridden_tbp;
12333 gfc_tbp_generic* g;
12334 const char* target_name;
12336 target_name = target->specific_st->name;
12338 /* Defined for this type directly. */
12339 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
12341 target->specific = target->specific_st->n.tb;
12342 goto specific_found;
12345 /* Look for an inherited specific binding. */
12346 if (super_type)
12348 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
12349 true, NULL);
12351 if (inherited)
12353 gcc_assert (inherited->n.tb);
12354 target->specific = inherited->n.tb;
12355 goto specific_found;
12359 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
12360 " at %L", target_name, name, &p->where);
12361 return false;
12363 /* Once we've found the specific binding, check it is not ambiguous with
12364 other specifics already found or inherited for the same GENERIC. */
12365 specific_found:
12366 gcc_assert (target->specific);
12368 /* This must really be a specific binding! */
12369 if (target->specific->is_generic)
12371 gfc_error ("GENERIC %qs at %L must target a specific binding,"
12372 " %qs is GENERIC, too", name, &p->where, target_name);
12373 return false;
12376 /* Check those already resolved on this type directly. */
12377 for (g = p->u.generic; g; g = g->next)
12378 if (g != target && g->specific
12379 && !check_generic_tbp_ambiguity (target, g, name, p->where))
12380 return false;
12382 /* Check for ambiguity with inherited specific targets. */
12383 for (overridden_tbp = p->overridden; overridden_tbp;
12384 overridden_tbp = overridden_tbp->overridden)
12385 if (overridden_tbp->is_generic)
12387 for (g = overridden_tbp->u.generic; g; g = g->next)
12389 gcc_assert (g->specific);
12390 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
12391 return false;
12396 /* If we attempt to "overwrite" a specific binding, this is an error. */
12397 if (p->overridden && !p->overridden->is_generic)
12399 gfc_error ("GENERIC %qs at %L can't overwrite specific binding with"
12400 " the same name", name, &p->where);
12401 return false;
12404 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
12405 all must have the same attributes here. */
12406 first_target = p->u.generic->specific->u.specific;
12407 gcc_assert (first_target);
12408 p->subroutine = first_target->n.sym->attr.subroutine;
12409 p->function = first_target->n.sym->attr.function;
12411 return true;
12415 /* Resolve a GENERIC procedure binding for a derived type. */
12417 static bool
12418 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
12420 gfc_symbol* super_type;
12422 /* Find the overridden binding if any. */
12423 st->n.tb->overridden = NULL;
12424 super_type = gfc_get_derived_super_type (derived);
12425 if (super_type)
12427 gfc_symtree* overridden;
12428 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
12429 true, NULL);
12431 if (overridden && overridden->n.tb)
12432 st->n.tb->overridden = overridden->n.tb;
12435 /* Resolve using worker function. */
12436 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
12440 /* Retrieve the target-procedure of an operator binding and do some checks in
12441 common for intrinsic and user-defined type-bound operators. */
12443 static gfc_symbol*
12444 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
12446 gfc_symbol* target_proc;
12448 gcc_assert (target->specific && !target->specific->is_generic);
12449 target_proc = target->specific->u.specific->n.sym;
12450 gcc_assert (target_proc);
12452 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
12453 if (target->specific->nopass)
12455 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
12456 return NULL;
12459 return target_proc;
12463 /* Resolve a type-bound intrinsic operator. */
12465 static bool
12466 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
12467 gfc_typebound_proc* p)
12469 gfc_symbol* super_type;
12470 gfc_tbp_generic* target;
12472 /* If there's already an error here, do nothing (but don't fail again). */
12473 if (p->error)
12474 return true;
12476 /* Operators should always be GENERIC bindings. */
12477 gcc_assert (p->is_generic);
12479 /* Look for an overridden binding. */
12480 super_type = gfc_get_derived_super_type (derived);
12481 if (super_type && super_type->f2k_derived)
12482 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
12483 op, true, NULL);
12484 else
12485 p->overridden = NULL;
12487 /* Resolve general GENERIC properties using worker function. */
12488 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
12489 goto error;
12491 /* Check the targets to be procedures of correct interface. */
12492 for (target = p->u.generic; target; target = target->next)
12494 gfc_symbol* target_proc;
12496 target_proc = get_checked_tb_operator_target (target, p->where);
12497 if (!target_proc)
12498 goto error;
12500 if (!gfc_check_operator_interface (target_proc, op, p->where))
12501 goto error;
12503 /* Add target to non-typebound operator list. */
12504 if (!target->specific->deferred && !derived->attr.use_assoc
12505 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
12507 gfc_interface *head, *intr;
12508 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
12509 return false;
12510 head = derived->ns->op[op];
12511 intr = gfc_get_interface ();
12512 intr->sym = target_proc;
12513 intr->where = p->where;
12514 intr->next = head;
12515 derived->ns->op[op] = intr;
12519 return true;
12521 error:
12522 p->error = 1;
12523 return false;
12527 /* Resolve a type-bound user operator (tree-walker callback). */
12529 static gfc_symbol* resolve_bindings_derived;
12530 static bool resolve_bindings_result;
12532 static bool check_uop_procedure (gfc_symbol* sym, locus where);
12534 static void
12535 resolve_typebound_user_op (gfc_symtree* stree)
12537 gfc_symbol* super_type;
12538 gfc_tbp_generic* target;
12540 gcc_assert (stree && stree->n.tb);
12542 if (stree->n.tb->error)
12543 return;
12545 /* Operators should always be GENERIC bindings. */
12546 gcc_assert (stree->n.tb->is_generic);
12548 /* Find overridden procedure, if any. */
12549 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12550 if (super_type && super_type->f2k_derived)
12552 gfc_symtree* overridden;
12553 overridden = gfc_find_typebound_user_op (super_type, NULL,
12554 stree->name, true, NULL);
12556 if (overridden && overridden->n.tb)
12557 stree->n.tb->overridden = overridden->n.tb;
12559 else
12560 stree->n.tb->overridden = NULL;
12562 /* Resolve basically using worker function. */
12563 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
12564 goto error;
12566 /* Check the targets to be functions of correct interface. */
12567 for (target = stree->n.tb->u.generic; target; target = target->next)
12569 gfc_symbol* target_proc;
12571 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
12572 if (!target_proc)
12573 goto error;
12575 if (!check_uop_procedure (target_proc, stree->n.tb->where))
12576 goto error;
12579 return;
12581 error:
12582 resolve_bindings_result = false;
12583 stree->n.tb->error = 1;
12587 /* Resolve the type-bound procedures for a derived type. */
12589 static void
12590 resolve_typebound_procedure (gfc_symtree* stree)
12592 gfc_symbol* proc;
12593 locus where;
12594 gfc_symbol* me_arg;
12595 gfc_symbol* super_type;
12596 gfc_component* comp;
12598 gcc_assert (stree);
12600 /* Undefined specific symbol from GENERIC target definition. */
12601 if (!stree->n.tb)
12602 return;
12604 if (stree->n.tb->error)
12605 return;
12607 /* If this is a GENERIC binding, use that routine. */
12608 if (stree->n.tb->is_generic)
12610 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
12611 goto error;
12612 return;
12615 /* Get the target-procedure to check it. */
12616 gcc_assert (!stree->n.tb->is_generic);
12617 gcc_assert (stree->n.tb->u.specific);
12618 proc = stree->n.tb->u.specific->n.sym;
12619 where = stree->n.tb->where;
12621 /* Default access should already be resolved from the parser. */
12622 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
12624 if (stree->n.tb->deferred)
12626 if (!check_proc_interface (proc, &where))
12627 goto error;
12629 else
12631 /* Check for F08:C465. */
12632 if ((!proc->attr.subroutine && !proc->attr.function)
12633 || (proc->attr.proc != PROC_MODULE
12634 && proc->attr.if_source != IFSRC_IFBODY)
12635 || proc->attr.abstract)
12637 gfc_error ("%qs must be a module procedure or an external procedure with"
12638 " an explicit interface at %L", proc->name, &where);
12639 goto error;
12643 stree->n.tb->subroutine = proc->attr.subroutine;
12644 stree->n.tb->function = proc->attr.function;
12646 /* Find the super-type of the current derived type. We could do this once and
12647 store in a global if speed is needed, but as long as not I believe this is
12648 more readable and clearer. */
12649 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12651 /* If PASS, resolve and check arguments if not already resolved / loaded
12652 from a .mod file. */
12653 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
12655 gfc_formal_arglist *dummy_args;
12657 dummy_args = gfc_sym_get_dummy_args (proc);
12658 if (stree->n.tb->pass_arg)
12660 gfc_formal_arglist *i;
12662 /* If an explicit passing argument name is given, walk the arg-list
12663 and look for it. */
12665 me_arg = NULL;
12666 stree->n.tb->pass_arg_num = 1;
12667 for (i = dummy_args; i; i = i->next)
12669 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
12671 me_arg = i->sym;
12672 break;
12674 ++stree->n.tb->pass_arg_num;
12677 if (!me_arg)
12679 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
12680 " argument %qs",
12681 proc->name, stree->n.tb->pass_arg, &where,
12682 stree->n.tb->pass_arg);
12683 goto error;
12686 else
12688 /* Otherwise, take the first one; there should in fact be at least
12689 one. */
12690 stree->n.tb->pass_arg_num = 1;
12691 if (!dummy_args)
12693 gfc_error ("Procedure %qs with PASS at %L must have at"
12694 " least one argument", proc->name, &where);
12695 goto error;
12697 me_arg = dummy_args->sym;
12700 /* Now check that the argument-type matches and the passed-object
12701 dummy argument is generally fine. */
12703 gcc_assert (me_arg);
12705 if (me_arg->ts.type != BT_CLASS)
12707 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
12708 " at %L", proc->name, &where);
12709 goto error;
12712 if (CLASS_DATA (me_arg)->ts.u.derived
12713 != resolve_bindings_derived)
12715 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
12716 " the derived-type %qs", me_arg->name, proc->name,
12717 me_arg->name, &where, resolve_bindings_derived->name);
12718 goto error;
12721 gcc_assert (me_arg->ts.type == BT_CLASS);
12722 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
12724 gfc_error ("Passed-object dummy argument of %qs at %L must be"
12725 " scalar", proc->name, &where);
12726 goto error;
12728 if (CLASS_DATA (me_arg)->attr.allocatable)
12730 gfc_error ("Passed-object dummy argument of %qs at %L must not"
12731 " be ALLOCATABLE", proc->name, &where);
12732 goto error;
12734 if (CLASS_DATA (me_arg)->attr.class_pointer)
12736 gfc_error ("Passed-object dummy argument of %qs at %L must not"
12737 " be POINTER", proc->name, &where);
12738 goto error;
12742 /* If we are extending some type, check that we don't override a procedure
12743 flagged NON_OVERRIDABLE. */
12744 stree->n.tb->overridden = NULL;
12745 if (super_type)
12747 gfc_symtree* overridden;
12748 overridden = gfc_find_typebound_proc (super_type, NULL,
12749 stree->name, true, NULL);
12751 if (overridden)
12753 if (overridden->n.tb)
12754 stree->n.tb->overridden = overridden->n.tb;
12756 if (!gfc_check_typebound_override (stree, overridden))
12757 goto error;
12761 /* See if there's a name collision with a component directly in this type. */
12762 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
12763 if (!strcmp (comp->name, stree->name))
12765 gfc_error ("Procedure %qs at %L has the same name as a component of"
12766 " %qs",
12767 stree->name, &where, resolve_bindings_derived->name);
12768 goto error;
12771 /* Try to find a name collision with an inherited component. */
12772 if (super_type && gfc_find_component (super_type, stree->name, true, true,
12773 NULL))
12775 gfc_error ("Procedure %qs at %L has the same name as an inherited"
12776 " component of %qs",
12777 stree->name, &where, resolve_bindings_derived->name);
12778 goto error;
12781 stree->n.tb->error = 0;
12782 return;
12784 error:
12785 resolve_bindings_result = false;
12786 stree->n.tb->error = 1;
12790 static bool
12791 resolve_typebound_procedures (gfc_symbol* derived)
12793 int op;
12794 gfc_symbol* super_type;
12796 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
12797 return true;
12799 super_type = gfc_get_derived_super_type (derived);
12800 if (super_type)
12801 resolve_symbol (super_type);
12803 resolve_bindings_derived = derived;
12804 resolve_bindings_result = true;
12806 if (derived->f2k_derived->tb_sym_root)
12807 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
12808 &resolve_typebound_procedure);
12810 if (derived->f2k_derived->tb_uop_root)
12811 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
12812 &resolve_typebound_user_op);
12814 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
12816 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
12817 if (p && !resolve_typebound_intrinsic_op (derived,
12818 (gfc_intrinsic_op)op, p))
12819 resolve_bindings_result = false;
12822 return resolve_bindings_result;
12826 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
12827 to give all identical derived types the same backend_decl. */
12828 static void
12829 add_dt_to_dt_list (gfc_symbol *derived)
12831 gfc_dt_list *dt_list;
12833 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
12834 if (derived == dt_list->derived)
12835 return;
12837 dt_list = gfc_get_dt_list ();
12838 dt_list->next = gfc_derived_types;
12839 dt_list->derived = derived;
12840 gfc_derived_types = dt_list;
12844 /* Ensure that a derived-type is really not abstract, meaning that every
12845 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
12847 static bool
12848 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
12850 if (!st)
12851 return true;
12853 if (!ensure_not_abstract_walker (sub, st->left))
12854 return false;
12855 if (!ensure_not_abstract_walker (sub, st->right))
12856 return false;
12858 if (st->n.tb && st->n.tb->deferred)
12860 gfc_symtree* overriding;
12861 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
12862 if (!overriding)
12863 return false;
12864 gcc_assert (overriding->n.tb);
12865 if (overriding->n.tb->deferred)
12867 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
12868 " %qs is DEFERRED and not overridden",
12869 sub->name, &sub->declared_at, st->name);
12870 return false;
12874 return true;
12877 static bool
12878 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
12880 /* The algorithm used here is to recursively travel up the ancestry of sub
12881 and for each ancestor-type, check all bindings. If any of them is
12882 DEFERRED, look it up starting from sub and see if the found (overriding)
12883 binding is not DEFERRED.
12884 This is not the most efficient way to do this, but it should be ok and is
12885 clearer than something sophisticated. */
12887 gcc_assert (ancestor && !sub->attr.abstract);
12889 if (!ancestor->attr.abstract)
12890 return true;
12892 /* Walk bindings of this ancestor. */
12893 if (ancestor->f2k_derived)
12895 bool t;
12896 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
12897 if (!t)
12898 return false;
12901 /* Find next ancestor type and recurse on it. */
12902 ancestor = gfc_get_derived_super_type (ancestor);
12903 if (ancestor)
12904 return ensure_not_abstract (sub, ancestor);
12906 return true;
12910 /* This check for typebound defined assignments is done recursively
12911 since the order in which derived types are resolved is not always in
12912 order of the declarations. */
12914 static void
12915 check_defined_assignments (gfc_symbol *derived)
12917 gfc_component *c;
12919 for (c = derived->components; c; c = c->next)
12921 if (!gfc_bt_struct (c->ts.type)
12922 || c->attr.pointer
12923 || c->attr.allocatable
12924 || c->attr.proc_pointer_comp
12925 || c->attr.class_pointer
12926 || c->attr.proc_pointer)
12927 continue;
12929 if (c->ts.u.derived->attr.defined_assign_comp
12930 || (c->ts.u.derived->f2k_derived
12931 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
12933 derived->attr.defined_assign_comp = 1;
12934 return;
12937 check_defined_assignments (c->ts.u.derived);
12938 if (c->ts.u.derived->attr.defined_assign_comp)
12940 derived->attr.defined_assign_comp = 1;
12941 return;
12947 /* Resolve a single component of a derived type or structure. */
12949 static bool
12950 resolve_component (gfc_component *c, gfc_symbol *sym)
12952 gfc_symbol *super_type;
12954 if (c->attr.artificial)
12955 return true;
12957 /* F2008, C442. */
12958 if ((!sym->attr.is_class || c != sym->components)
12959 && c->attr.codimension
12960 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
12962 gfc_error ("Coarray component %qs at %L must be allocatable with "
12963 "deferred shape", c->name, &c->loc);
12964 return false;
12967 /* F2008, C443. */
12968 if (c->attr.codimension && c->ts.type == BT_DERIVED
12969 && c->ts.u.derived->ts.is_iso_c)
12971 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12972 "shall not be a coarray", c->name, &c->loc);
12973 return false;
12976 /* F2008, C444. */
12977 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
12978 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12979 || c->attr.allocatable))
12981 gfc_error ("Component %qs at %L with coarray component "
12982 "shall be a nonpointer, nonallocatable scalar",
12983 c->name, &c->loc);
12984 return false;
12987 /* F2008, C448. */
12988 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12990 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
12991 "is not an array pointer", c->name, &c->loc);
12992 return false;
12995 if (c->attr.proc_pointer && c->ts.interface)
12997 gfc_symbol *ifc = c->ts.interface;
12999 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
13001 c->tb->error = 1;
13002 return false;
13005 if (ifc->attr.if_source || ifc->attr.intrinsic)
13007 /* Resolve interface and copy attributes. */
13008 if (ifc->formal && !ifc->formal_ns)
13009 resolve_symbol (ifc);
13010 if (ifc->attr.intrinsic)
13011 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
13013 if (ifc->result)
13015 c->ts = ifc->result->ts;
13016 c->attr.allocatable = ifc->result->attr.allocatable;
13017 c->attr.pointer = ifc->result->attr.pointer;
13018 c->attr.dimension = ifc->result->attr.dimension;
13019 c->as = gfc_copy_array_spec (ifc->result->as);
13020 c->attr.class_ok = ifc->result->attr.class_ok;
13022 else
13024 c->ts = ifc->ts;
13025 c->attr.allocatable = ifc->attr.allocatable;
13026 c->attr.pointer = ifc->attr.pointer;
13027 c->attr.dimension = ifc->attr.dimension;
13028 c->as = gfc_copy_array_spec (ifc->as);
13029 c->attr.class_ok = ifc->attr.class_ok;
13031 c->ts.interface = ifc;
13032 c->attr.function = ifc->attr.function;
13033 c->attr.subroutine = ifc->attr.subroutine;
13035 c->attr.pure = ifc->attr.pure;
13036 c->attr.elemental = ifc->attr.elemental;
13037 c->attr.recursive = ifc->attr.recursive;
13038 c->attr.always_explicit = ifc->attr.always_explicit;
13039 c->attr.ext_attr |= ifc->attr.ext_attr;
13040 /* Copy char length. */
13041 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
13043 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
13044 if (cl->length && !cl->resolved
13045 && !gfc_resolve_expr (cl->length))
13047 c->tb->error = 1;
13048 return false;
13050 c->ts.u.cl = cl;
13054 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
13056 /* Since PPCs are not implicitly typed, a PPC without an explicit
13057 interface must be a subroutine. */
13058 gfc_add_subroutine (&c->attr, c->name, &c->loc);
13061 /* Procedure pointer components: Check PASS arg. */
13062 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
13063 && !sym->attr.vtype)
13065 gfc_symbol* me_arg;
13067 if (c->tb->pass_arg)
13069 gfc_formal_arglist* i;
13071 /* If an explicit passing argument name is given, walk the arg-list
13072 and look for it. */
13074 me_arg = NULL;
13075 c->tb->pass_arg_num = 1;
13076 for (i = c->ts.interface->formal; i; i = i->next)
13078 if (!strcmp (i->sym->name, c->tb->pass_arg))
13080 me_arg = i->sym;
13081 break;
13083 c->tb->pass_arg_num++;
13086 if (!me_arg)
13088 gfc_error ("Procedure pointer component %qs with PASS(%s) "
13089 "at %L has no argument %qs", c->name,
13090 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
13091 c->tb->error = 1;
13092 return false;
13095 else
13097 /* Otherwise, take the first one; there should in fact be at least
13098 one. */
13099 c->tb->pass_arg_num = 1;
13100 if (!c->ts.interface->formal)
13102 gfc_error ("Procedure pointer component %qs with PASS at %L "
13103 "must have at least one argument",
13104 c->name, &c->loc);
13105 c->tb->error = 1;
13106 return false;
13108 me_arg = c->ts.interface->formal->sym;
13111 /* Now check that the argument-type matches. */
13112 gcc_assert (me_arg);
13113 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
13114 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
13115 || (me_arg->ts.type == BT_CLASS
13116 && CLASS_DATA (me_arg)->ts.u.derived != sym))
13118 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
13119 " the derived type %qs", me_arg->name, c->name,
13120 me_arg->name, &c->loc, sym->name);
13121 c->tb->error = 1;
13122 return false;
13125 /* Check for C453. */
13126 if (me_arg->attr.dimension)
13128 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13129 "must be scalar", me_arg->name, c->name, me_arg->name,
13130 &c->loc);
13131 c->tb->error = 1;
13132 return false;
13135 if (me_arg->attr.pointer)
13137 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13138 "may not have the POINTER attribute", me_arg->name,
13139 c->name, me_arg->name, &c->loc);
13140 c->tb->error = 1;
13141 return false;
13144 if (me_arg->attr.allocatable)
13146 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13147 "may not be ALLOCATABLE", me_arg->name, c->name,
13148 me_arg->name, &c->loc);
13149 c->tb->error = 1;
13150 return false;
13153 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
13155 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
13156 " at %L", c->name, &c->loc);
13157 return false;
13162 /* Check type-spec if this is not the parent-type component. */
13163 if (((sym->attr.is_class
13164 && (!sym->components->ts.u.derived->attr.extension
13165 || c != sym->components->ts.u.derived->components))
13166 || (!sym->attr.is_class
13167 && (!sym->attr.extension || c != sym->components)))
13168 && !sym->attr.vtype
13169 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
13170 return false;
13172 super_type = gfc_get_derived_super_type (sym);
13174 /* If this type is an extension, set the accessibility of the parent
13175 component. */
13176 if (super_type
13177 && ((sym->attr.is_class
13178 && c == sym->components->ts.u.derived->components)
13179 || (!sym->attr.is_class && c == sym->components))
13180 && strcmp (super_type->name, c->name) == 0)
13181 c->attr.access = super_type->attr.access;
13183 /* If this type is an extension, see if this component has the same name
13184 as an inherited type-bound procedure. */
13185 if (super_type && !sym->attr.is_class
13186 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
13188 gfc_error ("Component %qs of %qs at %L has the same name as an"
13189 " inherited type-bound procedure",
13190 c->name, sym->name, &c->loc);
13191 return false;
13194 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
13195 && !c->ts.deferred)
13197 if (c->ts.u.cl->length == NULL
13198 || (!resolve_charlen(c->ts.u.cl))
13199 || !gfc_is_constant_expr (c->ts.u.cl->length))
13201 gfc_error ("Character length of component %qs needs to "
13202 "be a constant specification expression at %L",
13203 c->name,
13204 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
13205 return false;
13209 if (c->ts.type == BT_CHARACTER && c->ts.deferred
13210 && !c->attr.pointer && !c->attr.allocatable)
13212 gfc_error ("Character component %qs of %qs at %L with deferred "
13213 "length must be a POINTER or ALLOCATABLE",
13214 c->name, sym->name, &c->loc);
13215 return false;
13218 /* Add the hidden deferred length field. */
13219 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function
13220 && !sym->attr.is_class)
13222 char name[GFC_MAX_SYMBOL_LEN+9];
13223 gfc_component *strlen;
13224 sprintf (name, "_%s_length", c->name);
13225 strlen = gfc_find_component (sym, name, true, true, NULL);
13226 if (strlen == NULL)
13228 if (!gfc_add_component (sym, name, &strlen))
13229 return false;
13230 strlen->ts.type = BT_INTEGER;
13231 strlen->ts.kind = gfc_charlen_int_kind;
13232 strlen->attr.access = ACCESS_PRIVATE;
13233 strlen->attr.artificial = 1;
13237 if (c->ts.type == BT_DERIVED
13238 && sym->component_access != ACCESS_PRIVATE
13239 && gfc_check_symbol_access (sym)
13240 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
13241 && !c->ts.u.derived->attr.use_assoc
13242 && !gfc_check_symbol_access (c->ts.u.derived)
13243 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
13244 "PRIVATE type and cannot be a component of "
13245 "%qs, which is PUBLIC at %L", c->name,
13246 sym->name, &sym->declared_at))
13247 return false;
13249 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
13251 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
13252 "type %s", c->name, &c->loc, sym->name);
13253 return false;
13256 if (sym->attr.sequence)
13258 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
13260 gfc_error ("Component %s of SEQUENCE type declared at %L does "
13261 "not have the SEQUENCE attribute",
13262 c->ts.u.derived->name, &sym->declared_at);
13263 return false;
13267 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
13268 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
13269 else if (c->ts.type == BT_CLASS && c->attr.class_ok
13270 && CLASS_DATA (c)->ts.u.derived->attr.generic)
13271 CLASS_DATA (c)->ts.u.derived
13272 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
13274 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
13275 && c->attr.pointer && c->ts.u.derived->components == NULL
13276 && !c->ts.u.derived->attr.zero_comp)
13278 gfc_error ("The pointer component %qs of %qs at %L is a type "
13279 "that has not been declared", c->name, sym->name,
13280 &c->loc);
13281 return false;
13284 if (c->ts.type == BT_CLASS && c->attr.class_ok
13285 && CLASS_DATA (c)->attr.class_pointer
13286 && CLASS_DATA (c)->ts.u.derived->components == NULL
13287 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
13288 && !UNLIMITED_POLY (c))
13290 gfc_error ("The pointer component %qs of %qs at %L is a type "
13291 "that has not been declared", c->name, sym->name,
13292 &c->loc);
13293 return false;
13296 /* C437. */
13297 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
13298 && (!c->attr.class_ok
13299 || !(CLASS_DATA (c)->attr.class_pointer
13300 || CLASS_DATA (c)->attr.allocatable)))
13302 gfc_error ("Component %qs with CLASS at %L must be allocatable "
13303 "or pointer", c->name, &c->loc);
13304 /* Prevent a recurrence of the error. */
13305 c->ts.type = BT_UNKNOWN;
13306 return false;
13309 /* Ensure that all the derived type components are put on the
13310 derived type list; even in formal namespaces, where derived type
13311 pointer components might not have been declared. */
13312 if (c->ts.type == BT_DERIVED
13313 && c->ts.u.derived
13314 && c->ts.u.derived->components
13315 && c->attr.pointer
13316 && sym != c->ts.u.derived)
13317 add_dt_to_dt_list (c->ts.u.derived);
13319 if (!gfc_resolve_array_spec (c->as,
13320 !(c->attr.pointer || c->attr.proc_pointer
13321 || c->attr.allocatable)))
13322 return false;
13324 if (c->initializer && !sym->attr.vtype
13325 && !gfc_check_assign_symbol (sym, c, c->initializer))
13326 return false;
13328 return true;
13332 /* Be nice about the locus for a structure expression - show the locus of the
13333 first non-null sub-expression if we can. */
13335 static locus *
13336 cons_where (gfc_expr *struct_expr)
13338 gfc_constructor *cons;
13340 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
13342 cons = gfc_constructor_first (struct_expr->value.constructor);
13343 for (; cons; cons = gfc_constructor_next (cons))
13345 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
13346 return &cons->expr->where;
13349 return &struct_expr->where;
13352 /* Resolve the components of a structure type. Much less work than derived
13353 types. */
13355 static bool
13356 resolve_fl_struct (gfc_symbol *sym)
13358 gfc_component *c;
13359 gfc_expr *init = NULL;
13360 bool success;
13362 /* Make sure UNIONs do not have overlapping initializers. */
13363 if (sym->attr.flavor == FL_UNION)
13365 for (c = sym->components; c; c = c->next)
13367 if (init && c->initializer)
13369 gfc_error ("Conflicting initializers in union at %L and %L",
13370 cons_where (init), cons_where (c->initializer));
13371 gfc_free_expr (c->initializer);
13372 c->initializer = NULL;
13374 if (init == NULL)
13375 init = c->initializer;
13379 success = true;
13380 for (c = sym->components; c; c = c->next)
13381 if (!resolve_component (c, sym))
13382 success = false;
13384 if (!success)
13385 return false;
13387 if (sym->components)
13388 add_dt_to_dt_list (sym);
13390 return true;
13394 /* Resolve the components of a derived type. This does not have to wait until
13395 resolution stage, but can be done as soon as the dt declaration has been
13396 parsed. */
13398 static bool
13399 resolve_fl_derived0 (gfc_symbol *sym)
13401 gfc_symbol* super_type;
13402 gfc_component *c;
13403 bool success;
13405 if (sym->attr.unlimited_polymorphic)
13406 return true;
13408 super_type = gfc_get_derived_super_type (sym);
13410 /* F2008, C432. */
13411 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
13413 gfc_error ("As extending type %qs at %L has a coarray component, "
13414 "parent type %qs shall also have one", sym->name,
13415 &sym->declared_at, super_type->name);
13416 return false;
13419 /* Ensure the extended type gets resolved before we do. */
13420 if (super_type && !resolve_fl_derived0 (super_type))
13421 return false;
13423 /* An ABSTRACT type must be extensible. */
13424 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
13426 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
13427 sym->name, &sym->declared_at);
13428 return false;
13431 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
13432 : sym->components;
13434 success = true;
13435 for ( ; c != NULL; c = c->next)
13436 if (!resolve_component (c, sym))
13437 success = false;
13439 if (!success)
13440 return false;
13442 check_defined_assignments (sym);
13444 if (!sym->attr.defined_assign_comp && super_type)
13445 sym->attr.defined_assign_comp
13446 = super_type->attr.defined_assign_comp;
13448 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
13449 all DEFERRED bindings are overridden. */
13450 if (super_type && super_type->attr.abstract && !sym->attr.abstract
13451 && !sym->attr.is_class
13452 && !ensure_not_abstract (sym, super_type))
13453 return false;
13455 /* Add derived type to the derived type list. */
13456 add_dt_to_dt_list (sym);
13458 return true;
13462 /* The following procedure does the full resolution of a derived type,
13463 including resolution of all type-bound procedures (if present). In contrast
13464 to 'resolve_fl_derived0' this can only be done after the module has been
13465 parsed completely. */
13467 static bool
13468 resolve_fl_derived (gfc_symbol *sym)
13470 gfc_symbol *gen_dt = NULL;
13472 if (sym->attr.unlimited_polymorphic)
13473 return true;
13475 if (!sym->attr.is_class)
13476 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
13477 if (gen_dt && gen_dt->generic && gen_dt->generic->next
13478 && (!gen_dt->generic->sym->attr.use_assoc
13479 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
13480 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
13481 "%qs at %L being the same name as derived "
13482 "type at %L", sym->name,
13483 gen_dt->generic->sym == sym
13484 ? gen_dt->generic->next->sym->name
13485 : gen_dt->generic->sym->name,
13486 gen_dt->generic->sym == sym
13487 ? &gen_dt->generic->next->sym->declared_at
13488 : &gen_dt->generic->sym->declared_at,
13489 &sym->declared_at))
13490 return false;
13492 /* Resolve the finalizer procedures. */
13493 if (!gfc_resolve_finalizers (sym, NULL))
13494 return false;
13496 if (sym->attr.is_class && sym->ts.u.derived == NULL)
13498 /* Fix up incomplete CLASS symbols. */
13499 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
13500 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
13502 /* Nothing more to do for unlimited polymorphic entities. */
13503 if (data->ts.u.derived->attr.unlimited_polymorphic)
13504 return true;
13505 else if (vptr->ts.u.derived == NULL)
13507 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
13508 gcc_assert (vtab);
13509 vptr->ts.u.derived = vtab->ts.u.derived;
13513 if (!resolve_fl_derived0 (sym))
13514 return false;
13516 /* Resolve the type-bound procedures. */
13517 if (!resolve_typebound_procedures (sym))
13518 return false;
13520 return true;
13524 static bool
13525 resolve_fl_namelist (gfc_symbol *sym)
13527 gfc_namelist *nl;
13528 gfc_symbol *nlsym;
13530 for (nl = sym->namelist; nl; nl = nl->next)
13532 /* Check again, the check in match only works if NAMELIST comes
13533 after the decl. */
13534 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
13536 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
13537 "allowed", nl->sym->name, sym->name, &sym->declared_at);
13538 return false;
13541 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
13542 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13543 "with assumed shape in namelist %qs at %L",
13544 nl->sym->name, sym->name, &sym->declared_at))
13545 return false;
13547 if (is_non_constant_shape_array (nl->sym)
13548 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13549 "with nonconstant shape in namelist %qs at %L",
13550 nl->sym->name, sym->name, &sym->declared_at))
13551 return false;
13553 if (nl->sym->ts.type == BT_CHARACTER
13554 && (nl->sym->ts.u.cl->length == NULL
13555 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
13556 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
13557 "nonconstant character length in "
13558 "namelist %qs at %L", nl->sym->name,
13559 sym->name, &sym->declared_at))
13560 return false;
13562 /* FIXME: Once UDDTIO is implemented, the following can be
13563 removed. */
13564 if (nl->sym->ts.type == BT_CLASS)
13566 gfc_error ("NAMELIST object %qs in namelist %qs at %L is "
13567 "polymorphic and requires a defined input/output "
13568 "procedure", nl->sym->name, sym->name, &sym->declared_at);
13569 return false;
13572 if (nl->sym->ts.type == BT_DERIVED
13573 && (nl->sym->ts.u.derived->attr.alloc_comp
13574 || nl->sym->ts.u.derived->attr.pointer_comp))
13576 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
13577 "namelist %qs at %L with ALLOCATABLE "
13578 "or POINTER components", nl->sym->name,
13579 sym->name, &sym->declared_at))
13580 return false;
13582 /* FIXME: Once UDDTIO is implemented, the following can be
13583 removed. */
13584 gfc_error ("NAMELIST object %qs in namelist %qs at %L has "
13585 "ALLOCATABLE or POINTER components and thus requires "
13586 "a defined input/output procedure", nl->sym->name,
13587 sym->name, &sym->declared_at);
13588 return false;
13592 /* Reject PRIVATE objects in a PUBLIC namelist. */
13593 if (gfc_check_symbol_access (sym))
13595 for (nl = sym->namelist; nl; nl = nl->next)
13597 if (!nl->sym->attr.use_assoc
13598 && !is_sym_host_assoc (nl->sym, sym->ns)
13599 && !gfc_check_symbol_access (nl->sym))
13601 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
13602 "cannot be member of PUBLIC namelist %qs at %L",
13603 nl->sym->name, sym->name, &sym->declared_at);
13604 return false;
13607 /* Types with private components that came here by USE-association. */
13608 if (nl->sym->ts.type == BT_DERIVED
13609 && derived_inaccessible (nl->sym->ts.u.derived))
13611 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
13612 "components and cannot be member of namelist %qs at %L",
13613 nl->sym->name, sym->name, &sym->declared_at);
13614 return false;
13617 /* Types with private components that are defined in the same module. */
13618 if (nl->sym->ts.type == BT_DERIVED
13619 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
13620 && nl->sym->ts.u.derived->attr.private_comp)
13622 gfc_error ("NAMELIST object %qs has PRIVATE components and "
13623 "cannot be a member of PUBLIC namelist %qs at %L",
13624 nl->sym->name, sym->name, &sym->declared_at);
13625 return false;
13631 /* 14.1.2 A module or internal procedure represent local entities
13632 of the same type as a namelist member and so are not allowed. */
13633 for (nl = sym->namelist; nl; nl = nl->next)
13635 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
13636 continue;
13638 if (nl->sym->attr.function && nl->sym == nl->sym->result)
13639 if ((nl->sym == sym->ns->proc_name)
13641 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
13642 continue;
13644 nlsym = NULL;
13645 if (nl->sym->name)
13646 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
13647 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
13649 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
13650 "attribute in %qs at %L", nlsym->name,
13651 &sym->declared_at);
13652 return false;
13656 return true;
13660 static bool
13661 resolve_fl_parameter (gfc_symbol *sym)
13663 /* A parameter array's shape needs to be constant. */
13664 if (sym->as != NULL
13665 && (sym->as->type == AS_DEFERRED
13666 || is_non_constant_shape_array (sym)))
13668 gfc_error ("Parameter array %qs at %L cannot be automatic "
13669 "or of deferred shape", sym->name, &sym->declared_at);
13670 return false;
13673 /* Make sure a parameter that has been implicitly typed still
13674 matches the implicit type, since PARAMETER statements can precede
13675 IMPLICIT statements. */
13676 if (sym->attr.implicit_type
13677 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
13678 sym->ns)))
13680 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
13681 "later IMPLICIT type", sym->name, &sym->declared_at);
13682 return false;
13685 /* Make sure the types of derived parameters are consistent. This
13686 type checking is deferred until resolution because the type may
13687 refer to a derived type from the host. */
13688 if (sym->ts.type == BT_DERIVED
13689 && !gfc_compare_types (&sym->ts, &sym->value->ts))
13691 gfc_error ("Incompatible derived type in PARAMETER at %L",
13692 &sym->value->where);
13693 return false;
13695 return true;
13699 /* Do anything necessary to resolve a symbol. Right now, we just
13700 assume that an otherwise unknown symbol is a variable. This sort
13701 of thing commonly happens for symbols in module. */
13703 static void
13704 resolve_symbol (gfc_symbol *sym)
13706 int check_constant, mp_flag;
13707 gfc_symtree *symtree;
13708 gfc_symtree *this_symtree;
13709 gfc_namespace *ns;
13710 gfc_component *c;
13711 symbol_attribute class_attr;
13712 gfc_array_spec *as;
13713 bool saved_specification_expr;
13715 if (sym->resolved)
13716 return;
13717 sym->resolved = 1;
13719 /* No symbol will ever have union type; only components can be unions.
13720 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
13721 (just like derived type declaration symbols have flavor FL_DERIVED). */
13722 gcc_assert (sym->ts.type != BT_UNION);
13724 if (sym->attr.artificial)
13725 return;
13727 if (sym->attr.unlimited_polymorphic)
13728 return;
13730 if (sym->attr.flavor == FL_UNKNOWN
13731 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
13732 && !sym->attr.generic && !sym->attr.external
13733 && sym->attr.if_source == IFSRC_UNKNOWN
13734 && sym->ts.type == BT_UNKNOWN))
13737 /* If we find that a flavorless symbol is an interface in one of the
13738 parent namespaces, find its symtree in this namespace, free the
13739 symbol and set the symtree to point to the interface symbol. */
13740 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
13742 symtree = gfc_find_symtree (ns->sym_root, sym->name);
13743 if (symtree && (symtree->n.sym->generic ||
13744 (symtree->n.sym->attr.flavor == FL_PROCEDURE
13745 && sym->ns->construct_entities)))
13747 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
13748 sym->name);
13749 if (this_symtree->n.sym == sym)
13751 symtree->n.sym->refs++;
13752 gfc_release_symbol (sym);
13753 this_symtree->n.sym = symtree->n.sym;
13754 return;
13759 /* Otherwise give it a flavor according to such attributes as
13760 it has. */
13761 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
13762 && sym->attr.intrinsic == 0)
13763 sym->attr.flavor = FL_VARIABLE;
13764 else if (sym->attr.flavor == FL_UNKNOWN)
13766 sym->attr.flavor = FL_PROCEDURE;
13767 if (sym->attr.dimension)
13768 sym->attr.function = 1;
13772 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
13773 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
13775 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
13776 && !resolve_procedure_interface (sym))
13777 return;
13779 if (sym->attr.is_protected && !sym->attr.proc_pointer
13780 && (sym->attr.procedure || sym->attr.external))
13782 if (sym->attr.external)
13783 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
13784 "at %L", &sym->declared_at);
13785 else
13786 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
13787 "at %L", &sym->declared_at);
13789 return;
13792 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
13793 return;
13795 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
13796 && !resolve_fl_struct (sym))
13797 return;
13799 /* Symbols that are module procedures with results (functions) have
13800 the types and array specification copied for type checking in
13801 procedures that call them, as well as for saving to a module
13802 file. These symbols can't stand the scrutiny that their results
13803 can. */
13804 mp_flag = (sym->result != NULL && sym->result != sym);
13806 /* Make sure that the intrinsic is consistent with its internal
13807 representation. This needs to be done before assigning a default
13808 type to avoid spurious warnings. */
13809 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
13810 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
13811 return;
13813 /* Resolve associate names. */
13814 if (sym->assoc)
13815 resolve_assoc_var (sym, true);
13817 /* Assign default type to symbols that need one and don't have one. */
13818 if (sym->ts.type == BT_UNKNOWN)
13820 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
13822 gfc_set_default_type (sym, 1, NULL);
13825 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
13826 && !sym->attr.function && !sym->attr.subroutine
13827 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
13828 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
13830 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
13832 /* The specific case of an external procedure should emit an error
13833 in the case that there is no implicit type. */
13834 if (!mp_flag)
13835 gfc_set_default_type (sym, sym->attr.external, NULL);
13836 else
13838 /* Result may be in another namespace. */
13839 resolve_symbol (sym->result);
13841 if (!sym->result->attr.proc_pointer)
13843 sym->ts = sym->result->ts;
13844 sym->as = gfc_copy_array_spec (sym->result->as);
13845 sym->attr.dimension = sym->result->attr.dimension;
13846 sym->attr.pointer = sym->result->attr.pointer;
13847 sym->attr.allocatable = sym->result->attr.allocatable;
13848 sym->attr.contiguous = sym->result->attr.contiguous;
13853 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
13855 bool saved_specification_expr = specification_expr;
13856 specification_expr = true;
13857 gfc_resolve_array_spec (sym->result->as, false);
13858 specification_expr = saved_specification_expr;
13861 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
13863 as = CLASS_DATA (sym)->as;
13864 class_attr = CLASS_DATA (sym)->attr;
13865 class_attr.pointer = class_attr.class_pointer;
13867 else
13869 class_attr = sym->attr;
13870 as = sym->as;
13873 /* F2008, C530. */
13874 if (sym->attr.contiguous
13875 && (!class_attr.dimension
13876 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
13877 && !class_attr.pointer)))
13879 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
13880 "array pointer or an assumed-shape or assumed-rank array",
13881 sym->name, &sym->declared_at);
13882 return;
13885 /* Assumed size arrays and assumed shape arrays must be dummy
13886 arguments. Array-spec's of implied-shape should have been resolved to
13887 AS_EXPLICIT already. */
13889 if (as)
13891 gcc_assert (as->type != AS_IMPLIED_SHAPE);
13892 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
13893 || as->type == AS_ASSUMED_SHAPE)
13894 && !sym->attr.dummy && !sym->attr.select_type_temporary)
13896 if (as->type == AS_ASSUMED_SIZE)
13897 gfc_error ("Assumed size array at %L must be a dummy argument",
13898 &sym->declared_at);
13899 else
13900 gfc_error ("Assumed shape array at %L must be a dummy argument",
13901 &sym->declared_at);
13902 return;
13904 /* TS 29113, C535a. */
13905 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
13906 && !sym->attr.select_type_temporary)
13908 gfc_error ("Assumed-rank array at %L must be a dummy argument",
13909 &sym->declared_at);
13910 return;
13912 if (as->type == AS_ASSUMED_RANK
13913 && (sym->attr.codimension || sym->attr.value))
13915 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
13916 "CODIMENSION attribute", &sym->declared_at);
13917 return;
13921 /* Make sure symbols with known intent or optional are really dummy
13922 variable. Because of ENTRY statement, this has to be deferred
13923 until resolution time. */
13925 if (!sym->attr.dummy
13926 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
13928 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
13929 return;
13932 if (sym->attr.value && !sym->attr.dummy)
13934 gfc_error ("%qs at %L cannot have the VALUE attribute because "
13935 "it is not a dummy argument", sym->name, &sym->declared_at);
13936 return;
13939 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
13941 gfc_charlen *cl = sym->ts.u.cl;
13942 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13944 gfc_error ("Character dummy variable %qs at %L with VALUE "
13945 "attribute must have constant length",
13946 sym->name, &sym->declared_at);
13947 return;
13950 if (sym->ts.is_c_interop
13951 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
13953 gfc_error ("C interoperable character dummy variable %qs at %L "
13954 "with VALUE attribute must have length one",
13955 sym->name, &sym->declared_at);
13956 return;
13960 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13961 && sym->ts.u.derived->attr.generic)
13963 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
13964 if (!sym->ts.u.derived)
13966 gfc_error ("The derived type %qs at %L is of type %qs, "
13967 "which has not been defined", sym->name,
13968 &sym->declared_at, sym->ts.u.derived->name);
13969 sym->ts.type = BT_UNKNOWN;
13970 return;
13974 /* Use the same constraints as TYPE(*), except for the type check
13975 and that only scalars and assumed-size arrays are permitted. */
13976 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
13978 if (!sym->attr.dummy)
13980 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13981 "a dummy argument", sym->name, &sym->declared_at);
13982 return;
13985 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
13986 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
13987 && sym->ts.type != BT_COMPLEX)
13989 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13990 "of type TYPE(*) or of an numeric intrinsic type",
13991 sym->name, &sym->declared_at);
13992 return;
13995 if (sym->attr.allocatable || sym->attr.codimension
13996 || sym->attr.pointer || sym->attr.value)
13998 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13999 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
14000 "attribute", sym->name, &sym->declared_at);
14001 return;
14004 if (sym->attr.intent == INTENT_OUT)
14006 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
14007 "have the INTENT(OUT) attribute",
14008 sym->name, &sym->declared_at);
14009 return;
14011 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
14013 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
14014 "either be a scalar or an assumed-size array",
14015 sym->name, &sym->declared_at);
14016 return;
14019 /* Set the type to TYPE(*) and add a dimension(*) to ensure
14020 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
14021 packing. */
14022 sym->ts.type = BT_ASSUMED;
14023 sym->as = gfc_get_array_spec ();
14024 sym->as->type = AS_ASSUMED_SIZE;
14025 sym->as->rank = 1;
14026 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
14028 else if (sym->ts.type == BT_ASSUMED)
14030 /* TS 29113, C407a. */
14031 if (!sym->attr.dummy)
14033 gfc_error ("Assumed type of variable %s at %L is only permitted "
14034 "for dummy variables", sym->name, &sym->declared_at);
14035 return;
14037 if (sym->attr.allocatable || sym->attr.codimension
14038 || sym->attr.pointer || sym->attr.value)
14040 gfc_error ("Assumed-type variable %s at %L may not have the "
14041 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
14042 sym->name, &sym->declared_at);
14043 return;
14045 if (sym->attr.intent == INTENT_OUT)
14047 gfc_error ("Assumed-type variable %s at %L may not have the "
14048 "INTENT(OUT) attribute",
14049 sym->name, &sym->declared_at);
14050 return;
14052 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
14054 gfc_error ("Assumed-type variable %s at %L shall not be an "
14055 "explicit-shape array", sym->name, &sym->declared_at);
14056 return;
14060 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
14061 do this for something that was implicitly typed because that is handled
14062 in gfc_set_default_type. Handle dummy arguments and procedure
14063 definitions separately. Also, anything that is use associated is not
14064 handled here but instead is handled in the module it is declared in.
14065 Finally, derived type definitions are allowed to be BIND(C) since that
14066 only implies that they're interoperable, and they are checked fully for
14067 interoperability when a variable is declared of that type. */
14068 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
14069 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
14070 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
14072 bool t = true;
14074 /* First, make sure the variable is declared at the
14075 module-level scope (J3/04-007, Section 15.3). */
14076 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
14077 sym->attr.in_common == 0)
14079 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
14080 "is neither a COMMON block nor declared at the "
14081 "module level scope", sym->name, &(sym->declared_at));
14082 t = false;
14084 else if (sym->common_head != NULL)
14086 t = verify_com_block_vars_c_interop (sym->common_head);
14088 else
14090 /* If type() declaration, we need to verify that the components
14091 of the given type are all C interoperable, etc. */
14092 if (sym->ts.type == BT_DERIVED &&
14093 sym->ts.u.derived->attr.is_c_interop != 1)
14095 /* Make sure the user marked the derived type as BIND(C). If
14096 not, call the verify routine. This could print an error
14097 for the derived type more than once if multiple variables
14098 of that type are declared. */
14099 if (sym->ts.u.derived->attr.is_bind_c != 1)
14100 verify_bind_c_derived_type (sym->ts.u.derived);
14101 t = false;
14104 /* Verify the variable itself as C interoperable if it
14105 is BIND(C). It is not possible for this to succeed if
14106 the verify_bind_c_derived_type failed, so don't have to handle
14107 any error returned by verify_bind_c_derived_type. */
14108 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
14109 sym->common_block);
14112 if (!t)
14114 /* clear the is_bind_c flag to prevent reporting errors more than
14115 once if something failed. */
14116 sym->attr.is_bind_c = 0;
14117 return;
14121 /* If a derived type symbol has reached this point, without its
14122 type being declared, we have an error. Notice that most
14123 conditions that produce undefined derived types have already
14124 been dealt with. However, the likes of:
14125 implicit type(t) (t) ..... call foo (t) will get us here if
14126 the type is not declared in the scope of the implicit
14127 statement. Change the type to BT_UNKNOWN, both because it is so
14128 and to prevent an ICE. */
14129 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
14130 && sym->ts.u.derived->components == NULL
14131 && !sym->ts.u.derived->attr.zero_comp)
14133 gfc_error ("The derived type %qs at %L is of type %qs, "
14134 "which has not been defined", sym->name,
14135 &sym->declared_at, sym->ts.u.derived->name);
14136 sym->ts.type = BT_UNKNOWN;
14137 return;
14140 /* Make sure that the derived type has been resolved and that the
14141 derived type is visible in the symbol's namespace, if it is a
14142 module function and is not PRIVATE. */
14143 if (sym->ts.type == BT_DERIVED
14144 && sym->ts.u.derived->attr.use_assoc
14145 && sym->ns->proc_name
14146 && sym->ns->proc_name->attr.flavor == FL_MODULE
14147 && !resolve_fl_derived (sym->ts.u.derived))
14148 return;
14150 /* Unless the derived-type declaration is use associated, Fortran 95
14151 does not allow public entries of private derived types.
14152 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
14153 161 in 95-006r3. */
14154 if (sym->ts.type == BT_DERIVED
14155 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
14156 && !sym->ts.u.derived->attr.use_assoc
14157 && gfc_check_symbol_access (sym)
14158 && !gfc_check_symbol_access (sym->ts.u.derived)
14159 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
14160 "derived type %qs",
14161 (sym->attr.flavor == FL_PARAMETER)
14162 ? "parameter" : "variable",
14163 sym->name, &sym->declared_at,
14164 sym->ts.u.derived->name))
14165 return;
14167 /* F2008, C1302. */
14168 if (sym->ts.type == BT_DERIVED
14169 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
14170 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
14171 || sym->ts.u.derived->attr.lock_comp)
14172 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
14174 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
14175 "type LOCK_TYPE must be a coarray", sym->name,
14176 &sym->declared_at);
14177 return;
14180 /* TS18508, C702/C703. */
14181 if (sym->ts.type == BT_DERIVED
14182 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
14183 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
14184 || sym->ts.u.derived->attr.event_comp)
14185 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
14187 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
14188 "type LOCK_TYPE must be a coarray", sym->name,
14189 &sym->declared_at);
14190 return;
14193 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
14194 default initialization is defined (5.1.2.4.4). */
14195 if (sym->ts.type == BT_DERIVED
14196 && sym->attr.dummy
14197 && sym->attr.intent == INTENT_OUT
14198 && sym->as
14199 && sym->as->type == AS_ASSUMED_SIZE)
14201 for (c = sym->ts.u.derived->components; c; c = c->next)
14203 if (c->initializer)
14205 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
14206 "ASSUMED SIZE and so cannot have a default initializer",
14207 sym->name, &sym->declared_at);
14208 return;
14213 /* F2008, C542. */
14214 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
14215 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
14217 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
14218 "INTENT(OUT)", sym->name, &sym->declared_at);
14219 return;
14222 /* TS18508. */
14223 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
14224 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
14226 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
14227 "INTENT(OUT)", sym->name, &sym->declared_at);
14228 return;
14231 /* F2008, C525. */
14232 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14233 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14234 && CLASS_DATA (sym)->attr.coarray_comp))
14235 || class_attr.codimension)
14236 && (sym->attr.result || sym->result == sym))
14238 gfc_error ("Function result %qs at %L shall not be a coarray or have "
14239 "a coarray component", sym->name, &sym->declared_at);
14240 return;
14243 /* F2008, C524. */
14244 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
14245 && sym->ts.u.derived->ts.is_iso_c)
14247 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14248 "shall not be a coarray", sym->name, &sym->declared_at);
14249 return;
14252 /* F2008, C525. */
14253 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14254 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14255 && CLASS_DATA (sym)->attr.coarray_comp))
14256 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
14257 || class_attr.allocatable))
14259 gfc_error ("Variable %qs at %L with coarray component shall be a "
14260 "nonpointer, nonallocatable scalar, which is not a coarray",
14261 sym->name, &sym->declared_at);
14262 return;
14265 /* F2008, C526. The function-result case was handled above. */
14266 if (class_attr.codimension
14267 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
14268 || sym->attr.select_type_temporary
14269 || sym->ns->save_all
14270 || sym->ns->proc_name->attr.flavor == FL_MODULE
14271 || sym->ns->proc_name->attr.is_main_program
14272 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
14274 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
14275 "nor a dummy argument", sym->name, &sym->declared_at);
14276 return;
14278 /* F2008, C528. */
14279 else if (class_attr.codimension && !sym->attr.select_type_temporary
14280 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
14282 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
14283 "deferred shape", sym->name, &sym->declared_at);
14284 return;
14286 else if (class_attr.codimension && class_attr.allocatable && as
14287 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
14289 gfc_error ("Allocatable coarray variable %qs at %L must have "
14290 "deferred shape", sym->name, &sym->declared_at);
14291 return;
14294 /* F2008, C541. */
14295 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14296 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14297 && CLASS_DATA (sym)->attr.coarray_comp))
14298 || (class_attr.codimension && class_attr.allocatable))
14299 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
14301 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
14302 "allocatable coarray or have coarray components",
14303 sym->name, &sym->declared_at);
14304 return;
14307 if (class_attr.codimension && sym->attr.dummy
14308 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
14310 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
14311 "procedure %qs", sym->name, &sym->declared_at,
14312 sym->ns->proc_name->name);
14313 return;
14316 if (sym->ts.type == BT_LOGICAL
14317 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
14318 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
14319 && sym->ns->proc_name->attr.is_bind_c)))
14321 int i;
14322 for (i = 0; gfc_logical_kinds[i].kind; i++)
14323 if (gfc_logical_kinds[i].kind == sym->ts.kind)
14324 break;
14325 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
14326 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
14327 "%L with non-C_Bool kind in BIND(C) procedure "
14328 "%qs", sym->name, &sym->declared_at,
14329 sym->ns->proc_name->name))
14330 return;
14331 else if (!gfc_logical_kinds[i].c_bool
14332 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
14333 "%qs at %L with non-C_Bool kind in "
14334 "BIND(C) procedure %qs", sym->name,
14335 &sym->declared_at,
14336 sym->attr.function ? sym->name
14337 : sym->ns->proc_name->name))
14338 return;
14341 switch (sym->attr.flavor)
14343 case FL_VARIABLE:
14344 if (!resolve_fl_variable (sym, mp_flag))
14345 return;
14346 break;
14348 case FL_PROCEDURE:
14349 if (sym->formal && !sym->formal_ns)
14351 /* Check that none of the arguments are a namelist. */
14352 gfc_formal_arglist *formal = sym->formal;
14354 for (; formal; formal = formal->next)
14355 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
14357 gfc_error ("Namelist '%s' can not be an argument to "
14358 "subroutine or function at %L",
14359 formal->sym->name, &sym->declared_at);
14360 return;
14364 if (!resolve_fl_procedure (sym, mp_flag))
14365 return;
14366 break;
14368 case FL_NAMELIST:
14369 if (!resolve_fl_namelist (sym))
14370 return;
14371 break;
14373 case FL_PARAMETER:
14374 if (!resolve_fl_parameter (sym))
14375 return;
14376 break;
14378 default:
14379 break;
14382 /* Resolve array specifier. Check as well some constraints
14383 on COMMON blocks. */
14385 check_constant = sym->attr.in_common && !sym->attr.pointer;
14387 /* Set the formal_arg_flag so that check_conflict will not throw
14388 an error for host associated variables in the specification
14389 expression for an array_valued function. */
14390 if (sym->attr.function && sym->as)
14391 formal_arg_flag = 1;
14393 saved_specification_expr = specification_expr;
14394 specification_expr = true;
14395 gfc_resolve_array_spec (sym->as, check_constant);
14396 specification_expr = saved_specification_expr;
14398 formal_arg_flag = 0;
14400 /* Resolve formal namespaces. */
14401 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
14402 && !sym->attr.contained && !sym->attr.intrinsic)
14403 gfc_resolve (sym->formal_ns);
14405 /* Make sure the formal namespace is present. */
14406 if (sym->formal && !sym->formal_ns)
14408 gfc_formal_arglist *formal = sym->formal;
14409 while (formal && !formal->sym)
14410 formal = formal->next;
14412 if (formal)
14414 sym->formal_ns = formal->sym->ns;
14415 if (sym->ns != formal->sym->ns)
14416 sym->formal_ns->refs++;
14420 /* Check threadprivate restrictions. */
14421 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
14422 && (!sym->attr.in_common
14423 && sym->module == NULL
14424 && (sym->ns->proc_name == NULL
14425 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
14426 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
14428 /* Check omp declare target restrictions. */
14429 if (sym->attr.omp_declare_target
14430 && sym->attr.flavor == FL_VARIABLE
14431 && !sym->attr.save
14432 && !sym->ns->save_all
14433 && (!sym->attr.in_common
14434 && sym->module == NULL
14435 && (sym->ns->proc_name == NULL
14436 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
14437 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
14438 sym->name, &sym->declared_at);
14440 /* If we have come this far we can apply default-initializers, as
14441 described in 14.7.5, to those variables that have not already
14442 been assigned one. */
14443 if (sym->ts.type == BT_DERIVED
14444 && !sym->value
14445 && !sym->attr.allocatable
14446 && !sym->attr.alloc_comp)
14448 symbol_attribute *a = &sym->attr;
14450 if ((!a->save && !a->dummy && !a->pointer
14451 && !a->in_common && !a->use_assoc
14452 && !a->result && !a->function)
14453 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
14454 apply_default_init (sym);
14455 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
14456 && (sym->ts.u.derived->attr.alloc_comp
14457 || sym->ts.u.derived->attr.pointer_comp))
14458 /* Mark the result symbol to be referenced, when it has allocatable
14459 components. */
14460 sym->result->attr.referenced = 1;
14463 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
14464 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
14465 && !CLASS_DATA (sym)->attr.class_pointer
14466 && !CLASS_DATA (sym)->attr.allocatable)
14467 apply_default_init (sym);
14469 /* If this symbol has a type-spec, check it. */
14470 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
14471 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
14472 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
14473 return;
14477 /************* Resolve DATA statements *************/
14479 static struct
14481 gfc_data_value *vnode;
14482 mpz_t left;
14484 values;
14487 /* Advance the values structure to point to the next value in the data list. */
14489 static bool
14490 next_data_value (void)
14492 while (mpz_cmp_ui (values.left, 0) == 0)
14495 if (values.vnode->next == NULL)
14496 return false;
14498 values.vnode = values.vnode->next;
14499 mpz_set (values.left, values.vnode->repeat);
14502 return true;
14506 static bool
14507 check_data_variable (gfc_data_variable *var, locus *where)
14509 gfc_expr *e;
14510 mpz_t size;
14511 mpz_t offset;
14512 bool t;
14513 ar_type mark = AR_UNKNOWN;
14514 int i;
14515 mpz_t section_index[GFC_MAX_DIMENSIONS];
14516 gfc_ref *ref;
14517 gfc_array_ref *ar;
14518 gfc_symbol *sym;
14519 int has_pointer;
14521 if (!gfc_resolve_expr (var->expr))
14522 return false;
14524 ar = NULL;
14525 mpz_init_set_si (offset, 0);
14526 e = var->expr;
14528 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
14529 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
14530 e = e->value.function.actual->expr;
14532 if (e->expr_type != EXPR_VARIABLE)
14533 gfc_internal_error ("check_data_variable(): Bad expression");
14535 sym = e->symtree->n.sym;
14537 if (sym->ns->is_block_data && !sym->attr.in_common)
14539 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
14540 sym->name, &sym->declared_at);
14543 if (e->ref == NULL && sym->as)
14545 gfc_error ("DATA array %qs at %L must be specified in a previous"
14546 " declaration", sym->name, where);
14547 return false;
14550 has_pointer = sym->attr.pointer;
14552 if (gfc_is_coindexed (e))
14554 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
14555 where);
14556 return false;
14559 for (ref = e->ref; ref; ref = ref->next)
14561 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
14562 has_pointer = 1;
14564 if (has_pointer
14565 && ref->type == REF_ARRAY
14566 && ref->u.ar.type != AR_FULL)
14568 gfc_error ("DATA element %qs at %L is a pointer and so must "
14569 "be a full array", sym->name, where);
14570 return false;
14574 if (e->rank == 0 || has_pointer)
14576 mpz_init_set_ui (size, 1);
14577 ref = NULL;
14579 else
14581 ref = e->ref;
14583 /* Find the array section reference. */
14584 for (ref = e->ref; ref; ref = ref->next)
14586 if (ref->type != REF_ARRAY)
14587 continue;
14588 if (ref->u.ar.type == AR_ELEMENT)
14589 continue;
14590 break;
14592 gcc_assert (ref);
14594 /* Set marks according to the reference pattern. */
14595 switch (ref->u.ar.type)
14597 case AR_FULL:
14598 mark = AR_FULL;
14599 break;
14601 case AR_SECTION:
14602 ar = &ref->u.ar;
14603 /* Get the start position of array section. */
14604 gfc_get_section_index (ar, section_index, &offset);
14605 mark = AR_SECTION;
14606 break;
14608 default:
14609 gcc_unreachable ();
14612 if (!gfc_array_size (e, &size))
14614 gfc_error ("Nonconstant array section at %L in DATA statement",
14615 &e->where);
14616 mpz_clear (offset);
14617 return false;
14621 t = true;
14623 while (mpz_cmp_ui (size, 0) > 0)
14625 if (!next_data_value ())
14627 gfc_error ("DATA statement at %L has more variables than values",
14628 where);
14629 t = false;
14630 break;
14633 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
14634 if (!t)
14635 break;
14637 /* If we have more than one element left in the repeat count,
14638 and we have more than one element left in the target variable,
14639 then create a range assignment. */
14640 /* FIXME: Only done for full arrays for now, since array sections
14641 seem tricky. */
14642 if (mark == AR_FULL && ref && ref->next == NULL
14643 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
14645 mpz_t range;
14647 if (mpz_cmp (size, values.left) >= 0)
14649 mpz_init_set (range, values.left);
14650 mpz_sub (size, size, values.left);
14651 mpz_set_ui (values.left, 0);
14653 else
14655 mpz_init_set (range, size);
14656 mpz_sub (values.left, values.left, size);
14657 mpz_set_ui (size, 0);
14660 t = gfc_assign_data_value (var->expr, values.vnode->expr,
14661 offset, &range);
14663 mpz_add (offset, offset, range);
14664 mpz_clear (range);
14666 if (!t)
14667 break;
14670 /* Assign initial value to symbol. */
14671 else
14673 mpz_sub_ui (values.left, values.left, 1);
14674 mpz_sub_ui (size, size, 1);
14676 t = gfc_assign_data_value (var->expr, values.vnode->expr,
14677 offset, NULL);
14678 if (!t)
14679 break;
14681 if (mark == AR_FULL)
14682 mpz_add_ui (offset, offset, 1);
14684 /* Modify the array section indexes and recalculate the offset
14685 for next element. */
14686 else if (mark == AR_SECTION)
14687 gfc_advance_section (section_index, ar, &offset);
14691 if (mark == AR_SECTION)
14693 for (i = 0; i < ar->dimen; i++)
14694 mpz_clear (section_index[i]);
14697 mpz_clear (size);
14698 mpz_clear (offset);
14700 return t;
14704 static bool traverse_data_var (gfc_data_variable *, locus *);
14706 /* Iterate over a list of elements in a DATA statement. */
14708 static bool
14709 traverse_data_list (gfc_data_variable *var, locus *where)
14711 mpz_t trip;
14712 iterator_stack frame;
14713 gfc_expr *e, *start, *end, *step;
14714 bool retval = true;
14716 mpz_init (frame.value);
14717 mpz_init (trip);
14719 start = gfc_copy_expr (var->iter.start);
14720 end = gfc_copy_expr (var->iter.end);
14721 step = gfc_copy_expr (var->iter.step);
14723 if (!gfc_simplify_expr (start, 1)
14724 || start->expr_type != EXPR_CONSTANT)
14726 gfc_error ("start of implied-do loop at %L could not be "
14727 "simplified to a constant value", &start->where);
14728 retval = false;
14729 goto cleanup;
14731 if (!gfc_simplify_expr (end, 1)
14732 || end->expr_type != EXPR_CONSTANT)
14734 gfc_error ("end of implied-do loop at %L could not be "
14735 "simplified to a constant value", &start->where);
14736 retval = false;
14737 goto cleanup;
14739 if (!gfc_simplify_expr (step, 1)
14740 || step->expr_type != EXPR_CONSTANT)
14742 gfc_error ("step of implied-do loop at %L could not be "
14743 "simplified to a constant value", &start->where);
14744 retval = false;
14745 goto cleanup;
14748 mpz_set (trip, end->value.integer);
14749 mpz_sub (trip, trip, start->value.integer);
14750 mpz_add (trip, trip, step->value.integer);
14752 mpz_div (trip, trip, step->value.integer);
14754 mpz_set (frame.value, start->value.integer);
14756 frame.prev = iter_stack;
14757 frame.variable = var->iter.var->symtree;
14758 iter_stack = &frame;
14760 while (mpz_cmp_ui (trip, 0) > 0)
14762 if (!traverse_data_var (var->list, where))
14764 retval = false;
14765 goto cleanup;
14768 e = gfc_copy_expr (var->expr);
14769 if (!gfc_simplify_expr (e, 1))
14771 gfc_free_expr (e);
14772 retval = false;
14773 goto cleanup;
14776 mpz_add (frame.value, frame.value, step->value.integer);
14778 mpz_sub_ui (trip, trip, 1);
14781 cleanup:
14782 mpz_clear (frame.value);
14783 mpz_clear (trip);
14785 gfc_free_expr (start);
14786 gfc_free_expr (end);
14787 gfc_free_expr (step);
14789 iter_stack = frame.prev;
14790 return retval;
14794 /* Type resolve variables in the variable list of a DATA statement. */
14796 static bool
14797 traverse_data_var (gfc_data_variable *var, locus *where)
14799 bool t;
14801 for (; var; var = var->next)
14803 if (var->expr == NULL)
14804 t = traverse_data_list (var, where);
14805 else
14806 t = check_data_variable (var, where);
14808 if (!t)
14809 return false;
14812 return true;
14816 /* Resolve the expressions and iterators associated with a data statement.
14817 This is separate from the assignment checking because data lists should
14818 only be resolved once. */
14820 static bool
14821 resolve_data_variables (gfc_data_variable *d)
14823 for (; d; d = d->next)
14825 if (d->list == NULL)
14827 if (!gfc_resolve_expr (d->expr))
14828 return false;
14830 else
14832 if (!gfc_resolve_iterator (&d->iter, false, true))
14833 return false;
14835 if (!resolve_data_variables (d->list))
14836 return false;
14840 return true;
14844 /* Resolve a single DATA statement. We implement this by storing a pointer to
14845 the value list into static variables, and then recursively traversing the
14846 variables list, expanding iterators and such. */
14848 static void
14849 resolve_data (gfc_data *d)
14852 if (!resolve_data_variables (d->var))
14853 return;
14855 values.vnode = d->value;
14856 if (d->value == NULL)
14857 mpz_set_ui (values.left, 0);
14858 else
14859 mpz_set (values.left, d->value->repeat);
14861 if (!traverse_data_var (d->var, &d->where))
14862 return;
14864 /* At this point, we better not have any values left. */
14866 if (next_data_value ())
14867 gfc_error ("DATA statement at %L has more values than variables",
14868 &d->where);
14872 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
14873 accessed by host or use association, is a dummy argument to a pure function,
14874 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
14875 is storage associated with any such variable, shall not be used in the
14876 following contexts: (clients of this function). */
14878 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
14879 procedure. Returns zero if assignment is OK, nonzero if there is a
14880 problem. */
14882 gfc_impure_variable (gfc_symbol *sym)
14884 gfc_symbol *proc;
14885 gfc_namespace *ns;
14887 if (sym->attr.use_assoc || sym->attr.in_common)
14888 return 1;
14890 /* Check if the symbol's ns is inside the pure procedure. */
14891 for (ns = gfc_current_ns; ns; ns = ns->parent)
14893 if (ns == sym->ns)
14894 break;
14895 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
14896 return 1;
14899 proc = sym->ns->proc_name;
14900 if (sym->attr.dummy
14901 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
14902 || proc->attr.function))
14903 return 1;
14905 /* TODO: Sort out what can be storage associated, if anything, and include
14906 it here. In principle equivalences should be scanned but it does not
14907 seem to be possible to storage associate an impure variable this way. */
14908 return 0;
14912 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
14913 current namespace is inside a pure procedure. */
14916 gfc_pure (gfc_symbol *sym)
14918 symbol_attribute attr;
14919 gfc_namespace *ns;
14921 if (sym == NULL)
14923 /* Check if the current namespace or one of its parents
14924 belongs to a pure procedure. */
14925 for (ns = gfc_current_ns; ns; ns = ns->parent)
14927 sym = ns->proc_name;
14928 if (sym == NULL)
14929 return 0;
14930 attr = sym->attr;
14931 if (attr.flavor == FL_PROCEDURE && attr.pure)
14932 return 1;
14934 return 0;
14937 attr = sym->attr;
14939 return attr.flavor == FL_PROCEDURE && attr.pure;
14943 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
14944 checks if the current namespace is implicitly pure. Note that this
14945 function returns false for a PURE procedure. */
14948 gfc_implicit_pure (gfc_symbol *sym)
14950 gfc_namespace *ns;
14952 if (sym == NULL)
14954 /* Check if the current procedure is implicit_pure. Walk up
14955 the procedure list until we find a procedure. */
14956 for (ns = gfc_current_ns; ns; ns = ns->parent)
14958 sym = ns->proc_name;
14959 if (sym == NULL)
14960 return 0;
14962 if (sym->attr.flavor == FL_PROCEDURE)
14963 break;
14967 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
14968 && !sym->attr.pure;
14972 void
14973 gfc_unset_implicit_pure (gfc_symbol *sym)
14975 gfc_namespace *ns;
14977 if (sym == NULL)
14979 /* Check if the current procedure is implicit_pure. Walk up
14980 the procedure list until we find a procedure. */
14981 for (ns = gfc_current_ns; ns; ns = ns->parent)
14983 sym = ns->proc_name;
14984 if (sym == NULL)
14985 return;
14987 if (sym->attr.flavor == FL_PROCEDURE)
14988 break;
14992 if (sym->attr.flavor == FL_PROCEDURE)
14993 sym->attr.implicit_pure = 0;
14994 else
14995 sym->attr.pure = 0;
14999 /* Test whether the current procedure is elemental or not. */
15002 gfc_elemental (gfc_symbol *sym)
15004 symbol_attribute attr;
15006 if (sym == NULL)
15007 sym = gfc_current_ns->proc_name;
15008 if (sym == NULL)
15009 return 0;
15010 attr = sym->attr;
15012 return attr.flavor == FL_PROCEDURE && attr.elemental;
15016 /* Warn about unused labels. */
15018 static void
15019 warn_unused_fortran_label (gfc_st_label *label)
15021 if (label == NULL)
15022 return;
15024 warn_unused_fortran_label (label->left);
15026 if (label->defined == ST_LABEL_UNKNOWN)
15027 return;
15029 switch (label->referenced)
15031 case ST_LABEL_UNKNOWN:
15032 gfc_warning (0, "Label %d at %L defined but not used", label->value,
15033 &label->where);
15034 break;
15036 case ST_LABEL_BAD_TARGET:
15037 gfc_warning (0, "Label %d at %L defined but cannot be used",
15038 label->value, &label->where);
15039 break;
15041 default:
15042 break;
15045 warn_unused_fortran_label (label->right);
15049 /* Returns the sequence type of a symbol or sequence. */
15051 static seq_type
15052 sequence_type (gfc_typespec ts)
15054 seq_type result;
15055 gfc_component *c;
15057 switch (ts.type)
15059 case BT_DERIVED:
15061 if (ts.u.derived->components == NULL)
15062 return SEQ_NONDEFAULT;
15064 result = sequence_type (ts.u.derived->components->ts);
15065 for (c = ts.u.derived->components->next; c; c = c->next)
15066 if (sequence_type (c->ts) != result)
15067 return SEQ_MIXED;
15069 return result;
15071 case BT_CHARACTER:
15072 if (ts.kind != gfc_default_character_kind)
15073 return SEQ_NONDEFAULT;
15075 return SEQ_CHARACTER;
15077 case BT_INTEGER:
15078 if (ts.kind != gfc_default_integer_kind)
15079 return SEQ_NONDEFAULT;
15081 return SEQ_NUMERIC;
15083 case BT_REAL:
15084 if (!(ts.kind == gfc_default_real_kind
15085 || ts.kind == gfc_default_double_kind))
15086 return SEQ_NONDEFAULT;
15088 return SEQ_NUMERIC;
15090 case BT_COMPLEX:
15091 if (ts.kind != gfc_default_complex_kind)
15092 return SEQ_NONDEFAULT;
15094 return SEQ_NUMERIC;
15096 case BT_LOGICAL:
15097 if (ts.kind != gfc_default_logical_kind)
15098 return SEQ_NONDEFAULT;
15100 return SEQ_NUMERIC;
15102 default:
15103 return SEQ_NONDEFAULT;
15108 /* Resolve derived type EQUIVALENCE object. */
15110 static bool
15111 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
15113 gfc_component *c = derived->components;
15115 if (!derived)
15116 return true;
15118 /* Shall not be an object of nonsequence derived type. */
15119 if (!derived->attr.sequence)
15121 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
15122 "attribute to be an EQUIVALENCE object", sym->name,
15123 &e->where);
15124 return false;
15127 /* Shall not have allocatable components. */
15128 if (derived->attr.alloc_comp)
15130 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
15131 "components to be an EQUIVALENCE object",sym->name,
15132 &e->where);
15133 return false;
15136 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
15138 gfc_error ("Derived type variable %qs at %L with default "
15139 "initialization cannot be in EQUIVALENCE with a variable "
15140 "in COMMON", sym->name, &e->where);
15141 return false;
15144 for (; c ; c = c->next)
15146 if (gfc_bt_struct (c->ts.type)
15147 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
15148 return false;
15150 /* Shall not be an object of sequence derived type containing a pointer
15151 in the structure. */
15152 if (c->attr.pointer)
15154 gfc_error ("Derived type variable %qs at %L with pointer "
15155 "component(s) cannot be an EQUIVALENCE object",
15156 sym->name, &e->where);
15157 return false;
15160 return true;
15164 /* Resolve equivalence object.
15165 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
15166 an allocatable array, an object of nonsequence derived type, an object of
15167 sequence derived type containing a pointer at any level of component
15168 selection, an automatic object, a function name, an entry name, a result
15169 name, a named constant, a structure component, or a subobject of any of
15170 the preceding objects. A substring shall not have length zero. A
15171 derived type shall not have components with default initialization nor
15172 shall two objects of an equivalence group be initialized.
15173 Either all or none of the objects shall have an protected attribute.
15174 The simple constraints are done in symbol.c(check_conflict) and the rest
15175 are implemented here. */
15177 static void
15178 resolve_equivalence (gfc_equiv *eq)
15180 gfc_symbol *sym;
15181 gfc_symbol *first_sym;
15182 gfc_expr *e;
15183 gfc_ref *r;
15184 locus *last_where = NULL;
15185 seq_type eq_type, last_eq_type;
15186 gfc_typespec *last_ts;
15187 int object, cnt_protected;
15188 const char *msg;
15190 last_ts = &eq->expr->symtree->n.sym->ts;
15192 first_sym = eq->expr->symtree->n.sym;
15194 cnt_protected = 0;
15196 for (object = 1; eq; eq = eq->eq, object++)
15198 e = eq->expr;
15200 e->ts = e->symtree->n.sym->ts;
15201 /* match_varspec might not know yet if it is seeing
15202 array reference or substring reference, as it doesn't
15203 know the types. */
15204 if (e->ref && e->ref->type == REF_ARRAY)
15206 gfc_ref *ref = e->ref;
15207 sym = e->symtree->n.sym;
15209 if (sym->attr.dimension)
15211 ref->u.ar.as = sym->as;
15212 ref = ref->next;
15215 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
15216 if (e->ts.type == BT_CHARACTER
15217 && ref
15218 && ref->type == REF_ARRAY
15219 && ref->u.ar.dimen == 1
15220 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
15221 && ref->u.ar.stride[0] == NULL)
15223 gfc_expr *start = ref->u.ar.start[0];
15224 gfc_expr *end = ref->u.ar.end[0];
15225 void *mem = NULL;
15227 /* Optimize away the (:) reference. */
15228 if (start == NULL && end == NULL)
15230 if (e->ref == ref)
15231 e->ref = ref->next;
15232 else
15233 e->ref->next = ref->next;
15234 mem = ref;
15236 else
15238 ref->type = REF_SUBSTRING;
15239 if (start == NULL)
15240 start = gfc_get_int_expr (gfc_default_integer_kind,
15241 NULL, 1);
15242 ref->u.ss.start = start;
15243 if (end == NULL && e->ts.u.cl)
15244 end = gfc_copy_expr (e->ts.u.cl->length);
15245 ref->u.ss.end = end;
15246 ref->u.ss.length = e->ts.u.cl;
15247 e->ts.u.cl = NULL;
15249 ref = ref->next;
15250 free (mem);
15253 /* Any further ref is an error. */
15254 if (ref)
15256 gcc_assert (ref->type == REF_ARRAY);
15257 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
15258 &ref->u.ar.where);
15259 continue;
15263 if (!gfc_resolve_expr (e))
15264 continue;
15266 sym = e->symtree->n.sym;
15268 if (sym->attr.is_protected)
15269 cnt_protected++;
15270 if (cnt_protected > 0 && cnt_protected != object)
15272 gfc_error ("Either all or none of the objects in the "
15273 "EQUIVALENCE set at %L shall have the "
15274 "PROTECTED attribute",
15275 &e->where);
15276 break;
15279 /* Shall not equivalence common block variables in a PURE procedure. */
15280 if (sym->ns->proc_name
15281 && sym->ns->proc_name->attr.pure
15282 && sym->attr.in_common)
15284 gfc_error ("Common block member %qs at %L cannot be an EQUIVALENCE "
15285 "object in the pure procedure %qs",
15286 sym->name, &e->where, sym->ns->proc_name->name);
15287 break;
15290 /* Shall not be a named constant. */
15291 if (e->expr_type == EXPR_CONSTANT)
15293 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
15294 "object", sym->name, &e->where);
15295 continue;
15298 if (e->ts.type == BT_DERIVED
15299 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
15300 continue;
15302 /* Check that the types correspond correctly:
15303 Note 5.28:
15304 A numeric sequence structure may be equivalenced to another sequence
15305 structure, an object of default integer type, default real type, double
15306 precision real type, default logical type such that components of the
15307 structure ultimately only become associated to objects of the same
15308 kind. A character sequence structure may be equivalenced to an object
15309 of default character kind or another character sequence structure.
15310 Other objects may be equivalenced only to objects of the same type and
15311 kind parameters. */
15313 /* Identical types are unconditionally OK. */
15314 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
15315 goto identical_types;
15317 last_eq_type = sequence_type (*last_ts);
15318 eq_type = sequence_type (sym->ts);
15320 /* Since the pair of objects is not of the same type, mixed or
15321 non-default sequences can be rejected. */
15323 msg = "Sequence %s with mixed components in EQUIVALENCE "
15324 "statement at %L with different type objects";
15325 if ((object ==2
15326 && last_eq_type == SEQ_MIXED
15327 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
15328 || (eq_type == SEQ_MIXED
15329 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
15330 continue;
15332 msg = "Non-default type object or sequence %s in EQUIVALENCE "
15333 "statement at %L with objects of different type";
15334 if ((object ==2
15335 && last_eq_type == SEQ_NONDEFAULT
15336 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
15337 || (eq_type == SEQ_NONDEFAULT
15338 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
15339 continue;
15341 msg ="Non-CHARACTER object %qs in default CHARACTER "
15342 "EQUIVALENCE statement at %L";
15343 if (last_eq_type == SEQ_CHARACTER
15344 && eq_type != SEQ_CHARACTER
15345 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
15346 continue;
15348 msg ="Non-NUMERIC object %qs in default NUMERIC "
15349 "EQUIVALENCE statement at %L";
15350 if (last_eq_type == SEQ_NUMERIC
15351 && eq_type != SEQ_NUMERIC
15352 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
15353 continue;
15355 identical_types:
15356 last_ts =&sym->ts;
15357 last_where = &e->where;
15359 if (!e->ref)
15360 continue;
15362 /* Shall not be an automatic array. */
15363 if (e->ref->type == REF_ARRAY
15364 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
15366 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
15367 "an EQUIVALENCE object", sym->name, &e->where);
15368 continue;
15371 r = e->ref;
15372 while (r)
15374 /* Shall not be a structure component. */
15375 if (r->type == REF_COMPONENT)
15377 gfc_error ("Structure component %qs at %L cannot be an "
15378 "EQUIVALENCE object",
15379 r->u.c.component->name, &e->where);
15380 break;
15383 /* A substring shall not have length zero. */
15384 if (r->type == REF_SUBSTRING)
15386 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
15388 gfc_error ("Substring at %L has length zero",
15389 &r->u.ss.start->where);
15390 break;
15393 r = r->next;
15399 /* Resolve function and ENTRY types, issue diagnostics if needed. */
15401 static void
15402 resolve_fntype (gfc_namespace *ns)
15404 gfc_entry_list *el;
15405 gfc_symbol *sym;
15407 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
15408 return;
15410 /* If there are any entries, ns->proc_name is the entry master
15411 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
15412 if (ns->entries)
15413 sym = ns->entries->sym;
15414 else
15415 sym = ns->proc_name;
15416 if (sym->result == sym
15417 && sym->ts.type == BT_UNKNOWN
15418 && !gfc_set_default_type (sym, 0, NULL)
15419 && !sym->attr.untyped)
15421 gfc_error ("Function %qs at %L has no IMPLICIT type",
15422 sym->name, &sym->declared_at);
15423 sym->attr.untyped = 1;
15426 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
15427 && !sym->attr.contained
15428 && !gfc_check_symbol_access (sym->ts.u.derived)
15429 && gfc_check_symbol_access (sym))
15431 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
15432 "%L of PRIVATE type %qs", sym->name,
15433 &sym->declared_at, sym->ts.u.derived->name);
15436 if (ns->entries)
15437 for (el = ns->entries->next; el; el = el->next)
15439 if (el->sym->result == el->sym
15440 && el->sym->ts.type == BT_UNKNOWN
15441 && !gfc_set_default_type (el->sym, 0, NULL)
15442 && !el->sym->attr.untyped)
15444 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
15445 el->sym->name, &el->sym->declared_at);
15446 el->sym->attr.untyped = 1;
15452 /* 12.3.2.1.1 Defined operators. */
15454 static bool
15455 check_uop_procedure (gfc_symbol *sym, locus where)
15457 gfc_formal_arglist *formal;
15459 if (!sym->attr.function)
15461 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
15462 sym->name, &where);
15463 return false;
15466 if (sym->ts.type == BT_CHARACTER
15467 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
15468 && !(sym->result && ((sym->result->ts.u.cl
15469 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
15471 gfc_error ("User operator procedure %qs at %L cannot be assumed "
15472 "character length", sym->name, &where);
15473 return false;
15476 formal = gfc_sym_get_dummy_args (sym);
15477 if (!formal || !formal->sym)
15479 gfc_error ("User operator procedure %qs at %L must have at least "
15480 "one argument", sym->name, &where);
15481 return false;
15484 if (formal->sym->attr.intent != INTENT_IN)
15486 gfc_error ("First argument of operator interface at %L must be "
15487 "INTENT(IN)", &where);
15488 return false;
15491 if (formal->sym->attr.optional)
15493 gfc_error ("First argument of operator interface at %L cannot be "
15494 "optional", &where);
15495 return false;
15498 formal = formal->next;
15499 if (!formal || !formal->sym)
15500 return true;
15502 if (formal->sym->attr.intent != INTENT_IN)
15504 gfc_error ("Second argument of operator interface at %L must be "
15505 "INTENT(IN)", &where);
15506 return false;
15509 if (formal->sym->attr.optional)
15511 gfc_error ("Second argument of operator interface at %L cannot be "
15512 "optional", &where);
15513 return false;
15516 if (formal->next)
15518 gfc_error ("Operator interface at %L must have, at most, two "
15519 "arguments", &where);
15520 return false;
15523 return true;
15526 static void
15527 gfc_resolve_uops (gfc_symtree *symtree)
15529 gfc_interface *itr;
15531 if (symtree == NULL)
15532 return;
15534 gfc_resolve_uops (symtree->left);
15535 gfc_resolve_uops (symtree->right);
15537 for (itr = symtree->n.uop->op; itr; itr = itr->next)
15538 check_uop_procedure (itr->sym, itr->sym->declared_at);
15542 /* Examine all of the expressions associated with a program unit,
15543 assign types to all intermediate expressions, make sure that all
15544 assignments are to compatible types and figure out which names
15545 refer to which functions or subroutines. It doesn't check code
15546 block, which is handled by gfc_resolve_code. */
15548 static void
15549 resolve_types (gfc_namespace *ns)
15551 gfc_namespace *n;
15552 gfc_charlen *cl;
15553 gfc_data *d;
15554 gfc_equiv *eq;
15555 gfc_namespace* old_ns = gfc_current_ns;
15557 if (ns->types_resolved)
15558 return;
15560 /* Check that all IMPLICIT types are ok. */
15561 if (!ns->seen_implicit_none)
15563 unsigned letter;
15564 for (letter = 0; letter != GFC_LETTERS; ++letter)
15565 if (ns->set_flag[letter]
15566 && !resolve_typespec_used (&ns->default_type[letter],
15567 &ns->implicit_loc[letter], NULL))
15568 return;
15571 gfc_current_ns = ns;
15573 resolve_entries (ns);
15575 resolve_common_vars (&ns->blank_common, false);
15576 resolve_common_blocks (ns->common_root);
15578 resolve_contained_functions (ns);
15580 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
15581 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
15582 resolve_formal_arglist (ns->proc_name);
15584 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
15586 for (cl = ns->cl_list; cl; cl = cl->next)
15587 resolve_charlen (cl);
15589 gfc_traverse_ns (ns, resolve_symbol);
15591 resolve_fntype (ns);
15593 for (n = ns->contained; n; n = n->sibling)
15595 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
15596 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
15597 "also be PURE", n->proc_name->name,
15598 &n->proc_name->declared_at);
15600 resolve_types (n);
15603 forall_flag = 0;
15604 gfc_do_concurrent_flag = 0;
15605 gfc_check_interfaces (ns);
15607 gfc_traverse_ns (ns, resolve_values);
15609 if (ns->save_all)
15610 gfc_save_all (ns);
15612 iter_stack = NULL;
15613 for (d = ns->data; d; d = d->next)
15614 resolve_data (d);
15616 iter_stack = NULL;
15617 gfc_traverse_ns (ns, gfc_formalize_init_value);
15619 gfc_traverse_ns (ns, gfc_verify_binding_labels);
15621 for (eq = ns->equiv; eq; eq = eq->next)
15622 resolve_equivalence (eq);
15624 /* Warn about unused labels. */
15625 if (warn_unused_label)
15626 warn_unused_fortran_label (ns->st_labels);
15628 gfc_resolve_uops (ns->uop_root);
15630 gfc_resolve_omp_declare_simd (ns);
15632 gfc_resolve_omp_udrs (ns->omp_udr_root);
15634 ns->types_resolved = 1;
15636 gfc_current_ns = old_ns;
15640 /* Call gfc_resolve_code recursively. */
15642 static void
15643 resolve_codes (gfc_namespace *ns)
15645 gfc_namespace *n;
15646 bitmap_obstack old_obstack;
15648 if (ns->resolved == 1)
15649 return;
15651 for (n = ns->contained; n; n = n->sibling)
15652 resolve_codes (n);
15654 gfc_current_ns = ns;
15656 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
15657 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
15658 cs_base = NULL;
15660 /* Set to an out of range value. */
15661 current_entry_id = -1;
15663 old_obstack = labels_obstack;
15664 bitmap_obstack_initialize (&labels_obstack);
15666 gfc_resolve_oacc_declare (ns);
15667 gfc_resolve_code (ns->code, ns);
15669 bitmap_obstack_release (&labels_obstack);
15670 labels_obstack = old_obstack;
15674 /* This function is called after a complete program unit has been compiled.
15675 Its purpose is to examine all of the expressions associated with a program
15676 unit, assign types to all intermediate expressions, make sure that all
15677 assignments are to compatible types and figure out which names refer to
15678 which functions or subroutines. */
15680 void
15681 gfc_resolve (gfc_namespace *ns)
15683 gfc_namespace *old_ns;
15684 code_stack *old_cs_base;
15685 struct gfc_omp_saved_state old_omp_state;
15687 if (ns->resolved)
15688 return;
15690 ns->resolved = -1;
15691 old_ns = gfc_current_ns;
15692 old_cs_base = cs_base;
15694 /* As gfc_resolve can be called during resolution of an OpenMP construct
15695 body, we should clear any state associated to it, so that say NS's
15696 DO loops are not interpreted as OpenMP loops. */
15697 gfc_omp_save_and_clear_state (&old_omp_state);
15699 resolve_types (ns);
15700 component_assignment_level = 0;
15701 resolve_codes (ns);
15703 gfc_current_ns = old_ns;
15704 cs_base = old_cs_base;
15705 ns->resolved = 1;
15707 gfc_run_passes (ns);
15709 gfc_omp_restore_state (&old_omp_state);