tree-nested.c (convert_nonlocal_omp_clauses): Add support for OMP_CLAUSE_{NUM_GANGS...
[official-gcc.git] / gcc / fortran / resolve.c
blobfebf0fa28d62c8a0aee9da77889d1598a2bdc9c6
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2015 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 || sym->attr.flavor == FL_DERIVED || 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);
1121 /* Resolve all of the elements of a structure constructor and make sure that
1122 the types are correct. The 'init' flag indicates that the given
1123 constructor is an initializer. */
1125 static bool
1126 resolve_structure_cons (gfc_expr *expr, int init)
1128 gfc_constructor *cons;
1129 gfc_component *comp;
1130 bool t;
1131 symbol_attribute a;
1133 t = true;
1135 if (expr->ts.type == BT_DERIVED)
1136 resolve_fl_derived0 (expr->ts.u.derived);
1138 cons = gfc_constructor_first (expr->value.constructor);
1140 /* A constructor may have references if it is the result of substituting a
1141 parameter variable. In this case we just pull out the component we
1142 want. */
1143 if (expr->ref)
1144 comp = expr->ref->u.c.sym->components;
1145 else
1146 comp = expr->ts.u.derived->components;
1148 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1150 int rank;
1152 if (!cons->expr)
1153 continue;
1155 if (!gfc_resolve_expr (cons->expr))
1157 t = false;
1158 continue;
1161 rank = comp->as ? comp->as->rank : 0;
1162 if (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->as)
1163 rank = CLASS_DATA (comp)->as->rank;
1165 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1166 && (comp->attr.allocatable || cons->expr->rank))
1168 gfc_error ("The rank of the element in the structure "
1169 "constructor at %L does not match that of the "
1170 "component (%d/%d)", &cons->expr->where,
1171 cons->expr->rank, rank);
1172 t = false;
1175 /* If we don't have the right type, try to convert it. */
1177 if (!comp->attr.proc_pointer &&
1178 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1180 if (strcmp (comp->name, "_extends") == 0)
1182 /* Can afford to be brutal with the _extends initializer.
1183 The derived type can get lost because it is PRIVATE
1184 but it is not usage constrained by the standard. */
1185 cons->expr->ts = comp->ts;
1187 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1189 gfc_error ("The element in the structure constructor at %L, "
1190 "for pointer component %qs, is %s but should be %s",
1191 &cons->expr->where, comp->name,
1192 gfc_basic_typename (cons->expr->ts.type),
1193 gfc_basic_typename (comp->ts.type));
1194 t = false;
1196 else
1198 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1199 if (t)
1200 t = t2;
1204 /* For strings, the length of the constructor should be the same as
1205 the one of the structure, ensure this if the lengths are known at
1206 compile time and when we are dealing with PARAMETER or structure
1207 constructors. */
1208 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1209 && comp->ts.u.cl->length
1210 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1211 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1212 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1213 && cons->expr->rank != 0
1214 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1215 comp->ts.u.cl->length->value.integer) != 0)
1217 if (cons->expr->expr_type == EXPR_VARIABLE
1218 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1220 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1221 to make use of the gfc_resolve_character_array_constructor
1222 machinery. The expression is later simplified away to
1223 an array of string literals. */
1224 gfc_expr *para = cons->expr;
1225 cons->expr = gfc_get_expr ();
1226 cons->expr->ts = para->ts;
1227 cons->expr->where = para->where;
1228 cons->expr->expr_type = EXPR_ARRAY;
1229 cons->expr->rank = para->rank;
1230 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1231 gfc_constructor_append_expr (&cons->expr->value.constructor,
1232 para, &cons->expr->where);
1234 if (cons->expr->expr_type == EXPR_ARRAY)
1236 gfc_constructor *p;
1237 p = gfc_constructor_first (cons->expr->value.constructor);
1238 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1240 gfc_charlen *cl, *cl2;
1242 cl2 = NULL;
1243 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1245 if (cl == cons->expr->ts.u.cl)
1246 break;
1247 cl2 = cl;
1250 gcc_assert (cl);
1252 if (cl2)
1253 cl2->next = cl->next;
1255 gfc_free_expr (cl->length);
1256 free (cl);
1259 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1260 cons->expr->ts.u.cl->length_from_typespec = true;
1261 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1262 gfc_resolve_character_array_constructor (cons->expr);
1266 if (cons->expr->expr_type == EXPR_NULL
1267 && !(comp->attr.pointer || comp->attr.allocatable
1268 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1269 || (comp->ts.type == BT_CLASS
1270 && (CLASS_DATA (comp)->attr.class_pointer
1271 || CLASS_DATA (comp)->attr.allocatable))))
1273 t = false;
1274 gfc_error ("The NULL in the structure constructor at %L is "
1275 "being applied to component %qs, which is neither "
1276 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1277 comp->name);
1280 if (comp->attr.proc_pointer && comp->ts.interface)
1282 /* Check procedure pointer interface. */
1283 gfc_symbol *s2 = NULL;
1284 gfc_component *c2;
1285 const char *name;
1286 char err[200];
1288 c2 = gfc_get_proc_ptr_comp (cons->expr);
1289 if (c2)
1291 s2 = c2->ts.interface;
1292 name = c2->name;
1294 else if (cons->expr->expr_type == EXPR_FUNCTION)
1296 s2 = cons->expr->symtree->n.sym->result;
1297 name = cons->expr->symtree->n.sym->result->name;
1299 else if (cons->expr->expr_type != EXPR_NULL)
1301 s2 = cons->expr->symtree->n.sym;
1302 name = cons->expr->symtree->n.sym->name;
1305 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1306 err, sizeof (err), NULL, NULL))
1308 gfc_error ("Interface mismatch for procedure-pointer component "
1309 "%qs in structure constructor at %L: %s",
1310 comp->name, &cons->expr->where, err);
1311 return false;
1315 if (!comp->attr.pointer || comp->attr.proc_pointer
1316 || cons->expr->expr_type == EXPR_NULL)
1317 continue;
1319 a = gfc_expr_attr (cons->expr);
1321 if (!a.pointer && !a.target)
1323 t = false;
1324 gfc_error ("The element in the structure constructor at %L, "
1325 "for pointer component %qs should be a POINTER or "
1326 "a TARGET", &cons->expr->where, comp->name);
1329 if (init)
1331 /* F08:C461. Additional checks for pointer initialization. */
1332 if (a.allocatable)
1334 t = false;
1335 gfc_error ("Pointer initialization target at %L "
1336 "must not be ALLOCATABLE ", &cons->expr->where);
1338 if (!a.save)
1340 t = false;
1341 gfc_error ("Pointer initialization target at %L "
1342 "must have the SAVE attribute", &cons->expr->where);
1346 /* F2003, C1272 (3). */
1347 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1348 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1349 || gfc_is_coindexed (cons->expr));
1350 if (impure && gfc_pure (NULL))
1352 t = false;
1353 gfc_error ("Invalid expression in the structure constructor for "
1354 "pointer component %qs at %L in PURE procedure",
1355 comp->name, &cons->expr->where);
1358 if (impure)
1359 gfc_unset_implicit_pure (NULL);
1362 return t;
1366 /****************** Expression name resolution ******************/
1368 /* Returns 0 if a symbol was not declared with a type or
1369 attribute declaration statement, nonzero otherwise. */
1371 static int
1372 was_declared (gfc_symbol *sym)
1374 symbol_attribute a;
1376 a = sym->attr;
1378 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1379 return 1;
1381 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1382 || a.optional || a.pointer || a.save || a.target || a.volatile_
1383 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1384 || a.asynchronous || a.codimension)
1385 return 1;
1387 return 0;
1391 /* Determine if a symbol is generic or not. */
1393 static int
1394 generic_sym (gfc_symbol *sym)
1396 gfc_symbol *s;
1398 if (sym->attr.generic ||
1399 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1400 return 1;
1402 if (was_declared (sym) || sym->ns->parent == NULL)
1403 return 0;
1405 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1407 if (s != NULL)
1409 if (s == sym)
1410 return 0;
1411 else
1412 return generic_sym (s);
1415 return 0;
1419 /* Determine if a symbol is specific or not. */
1421 static int
1422 specific_sym (gfc_symbol *sym)
1424 gfc_symbol *s;
1426 if (sym->attr.if_source == IFSRC_IFBODY
1427 || sym->attr.proc == PROC_MODULE
1428 || sym->attr.proc == PROC_INTERNAL
1429 || sym->attr.proc == PROC_ST_FUNCTION
1430 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1431 || sym->attr.external)
1432 return 1;
1434 if (was_declared (sym) || sym->ns->parent == NULL)
1435 return 0;
1437 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1439 return (s == NULL) ? 0 : specific_sym (s);
1443 /* Figure out if the procedure is specific, generic or unknown. */
1445 enum proc_type
1446 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1448 static proc_type
1449 procedure_kind (gfc_symbol *sym)
1451 if (generic_sym (sym))
1452 return PTYPE_GENERIC;
1454 if (specific_sym (sym))
1455 return PTYPE_SPECIFIC;
1457 return PTYPE_UNKNOWN;
1460 /* Check references to assumed size arrays. The flag need_full_assumed_size
1461 is nonzero when matching actual arguments. */
1463 static int need_full_assumed_size = 0;
1465 static bool
1466 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1468 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1469 return false;
1471 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1472 What should it be? */
1473 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1474 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1475 && (e->ref->u.ar.type == AR_FULL))
1477 gfc_error ("The upper bound in the last dimension must "
1478 "appear in the reference to the assumed size "
1479 "array %qs at %L", sym->name, &e->where);
1480 return true;
1482 return false;
1486 /* Look for bad assumed size array references in argument expressions
1487 of elemental and array valued intrinsic procedures. Since this is
1488 called from procedure resolution functions, it only recurses at
1489 operators. */
1491 static bool
1492 resolve_assumed_size_actual (gfc_expr *e)
1494 if (e == NULL)
1495 return false;
1497 switch (e->expr_type)
1499 case EXPR_VARIABLE:
1500 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1501 return true;
1502 break;
1504 case EXPR_OP:
1505 if (resolve_assumed_size_actual (e->value.op.op1)
1506 || resolve_assumed_size_actual (e->value.op.op2))
1507 return true;
1508 break;
1510 default:
1511 break;
1513 return false;
1517 /* Check a generic procedure, passed as an actual argument, to see if
1518 there is a matching specific name. If none, it is an error, and if
1519 more than one, the reference is ambiguous. */
1520 static int
1521 count_specific_procs (gfc_expr *e)
1523 int n;
1524 gfc_interface *p;
1525 gfc_symbol *sym;
1527 n = 0;
1528 sym = e->symtree->n.sym;
1530 for (p = sym->generic; p; p = p->next)
1531 if (strcmp (sym->name, p->sym->name) == 0)
1533 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1534 sym->name);
1535 n++;
1538 if (n > 1)
1539 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1540 &e->where);
1542 if (n == 0)
1543 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1544 "argument at %L", sym->name, &e->where);
1546 return n;
1550 /* See if a call to sym could possibly be a not allowed RECURSION because of
1551 a missing RECURSIVE declaration. This means that either sym is the current
1552 context itself, or sym is the parent of a contained procedure calling its
1553 non-RECURSIVE containing procedure.
1554 This also works if sym is an ENTRY. */
1556 static bool
1557 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1559 gfc_symbol* proc_sym;
1560 gfc_symbol* context_proc;
1561 gfc_namespace* real_context;
1563 if (sym->attr.flavor == FL_PROGRAM
1564 || sym->attr.flavor == FL_DERIVED)
1565 return false;
1567 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1569 /* If we've got an ENTRY, find real procedure. */
1570 if (sym->attr.entry && sym->ns->entries)
1571 proc_sym = sym->ns->entries->sym;
1572 else
1573 proc_sym = sym;
1575 /* If sym is RECURSIVE, all is well of course. */
1576 if (proc_sym->attr.recursive || flag_recursive)
1577 return false;
1579 /* Find the context procedure's "real" symbol if it has entries.
1580 We look for a procedure symbol, so recurse on the parents if we don't
1581 find one (like in case of a BLOCK construct). */
1582 for (real_context = context; ; real_context = real_context->parent)
1584 /* We should find something, eventually! */
1585 gcc_assert (real_context);
1587 context_proc = (real_context->entries ? real_context->entries->sym
1588 : real_context->proc_name);
1590 /* In some special cases, there may not be a proc_name, like for this
1591 invalid code:
1592 real(bad_kind()) function foo () ...
1593 when checking the call to bad_kind ().
1594 In these cases, we simply return here and assume that the
1595 call is ok. */
1596 if (!context_proc)
1597 return false;
1599 if (context_proc->attr.flavor != FL_LABEL)
1600 break;
1603 /* A call from sym's body to itself is recursion, of course. */
1604 if (context_proc == proc_sym)
1605 return true;
1607 /* The same is true if context is a contained procedure and sym the
1608 containing one. */
1609 if (context_proc->attr.contained)
1611 gfc_symbol* parent_proc;
1613 gcc_assert (context->parent);
1614 parent_proc = (context->parent->entries ? context->parent->entries->sym
1615 : context->parent->proc_name);
1617 if (parent_proc == proc_sym)
1618 return true;
1621 return false;
1625 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1626 its typespec and formal argument list. */
1628 bool
1629 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1631 gfc_intrinsic_sym* isym = NULL;
1632 const char* symstd;
1634 if (sym->formal)
1635 return true;
1637 /* Already resolved. */
1638 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1639 return true;
1641 /* We already know this one is an intrinsic, so we don't call
1642 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1643 gfc_find_subroutine directly to check whether it is a function or
1644 subroutine. */
1646 if (sym->intmod_sym_id && sym->attr.subroutine)
1648 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1649 isym = gfc_intrinsic_subroutine_by_id (id);
1651 else if (sym->intmod_sym_id)
1653 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1654 isym = gfc_intrinsic_function_by_id (id);
1656 else if (!sym->attr.subroutine)
1657 isym = gfc_find_function (sym->name);
1659 if (isym && !sym->attr.subroutine)
1661 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1662 && !sym->attr.implicit_type)
1663 gfc_warning (OPT_Wsurprising,
1664 "Type specified for intrinsic function %qs at %L is"
1665 " ignored", sym->name, &sym->declared_at);
1667 if (!sym->attr.function &&
1668 !gfc_add_function(&sym->attr, sym->name, loc))
1669 return false;
1671 sym->ts = isym->ts;
1673 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1675 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1677 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1678 " specifier", sym->name, &sym->declared_at);
1679 return false;
1682 if (!sym->attr.subroutine &&
1683 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1684 return false;
1686 else
1688 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1689 &sym->declared_at);
1690 return false;
1693 gfc_copy_formal_args_intr (sym, isym, NULL);
1695 sym->attr.pure = isym->pure;
1696 sym->attr.elemental = isym->elemental;
1698 /* Check it is actually available in the standard settings. */
1699 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1701 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1702 "available in the current standard settings but %s. Use "
1703 "an appropriate %<-std=*%> option or enable "
1704 "%<-fall-intrinsics%> in order to use it.",
1705 sym->name, &sym->declared_at, symstd);
1706 return false;
1709 return true;
1713 /* Resolve a procedure expression, like passing it to a called procedure or as
1714 RHS for a procedure pointer assignment. */
1716 static bool
1717 resolve_procedure_expression (gfc_expr* expr)
1719 gfc_symbol* sym;
1721 if (expr->expr_type != EXPR_VARIABLE)
1722 return true;
1723 gcc_assert (expr->symtree);
1725 sym = expr->symtree->n.sym;
1727 if (sym->attr.intrinsic)
1728 gfc_resolve_intrinsic (sym, &expr->where);
1730 if (sym->attr.flavor != FL_PROCEDURE
1731 || (sym->attr.function && sym->result == sym))
1732 return true;
1734 /* A non-RECURSIVE procedure that is used as procedure expression within its
1735 own body is in danger of being called recursively. */
1736 if (is_illegal_recursion (sym, gfc_current_ns))
1737 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1738 " itself recursively. Declare it RECURSIVE or use"
1739 " %<-frecursive%>", sym->name, &expr->where);
1741 return true;
1745 /* Resolve an actual argument list. Most of the time, this is just
1746 resolving the expressions in the list.
1747 The exception is that we sometimes have to decide whether arguments
1748 that look like procedure arguments are really simple variable
1749 references. */
1751 static bool
1752 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1753 bool no_formal_args)
1755 gfc_symbol *sym;
1756 gfc_symtree *parent_st;
1757 gfc_expr *e;
1758 gfc_component *comp;
1759 int save_need_full_assumed_size;
1760 bool return_value = false;
1761 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1763 actual_arg = true;
1764 first_actual_arg = true;
1766 for (; arg; arg = arg->next)
1768 e = arg->expr;
1769 if (e == NULL)
1771 /* Check the label is a valid branching target. */
1772 if (arg->label)
1774 if (arg->label->defined == ST_LABEL_UNKNOWN)
1776 gfc_error ("Label %d referenced at %L is never defined",
1777 arg->label->value, &arg->label->where);
1778 goto cleanup;
1781 first_actual_arg = false;
1782 continue;
1785 if (e->expr_type == EXPR_VARIABLE
1786 && e->symtree->n.sym->attr.generic
1787 && no_formal_args
1788 && count_specific_procs (e) != 1)
1789 goto cleanup;
1791 if (e->ts.type != BT_PROCEDURE)
1793 save_need_full_assumed_size = need_full_assumed_size;
1794 if (e->expr_type != EXPR_VARIABLE)
1795 need_full_assumed_size = 0;
1796 if (!gfc_resolve_expr (e))
1797 goto cleanup;
1798 need_full_assumed_size = save_need_full_assumed_size;
1799 goto argument_list;
1802 /* See if the expression node should really be a variable reference. */
1804 sym = e->symtree->n.sym;
1806 if (sym->attr.flavor == FL_PROCEDURE
1807 || sym->attr.intrinsic
1808 || sym->attr.external)
1810 int actual_ok;
1812 /* If a procedure is not already determined to be something else
1813 check if it is intrinsic. */
1814 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1815 sym->attr.intrinsic = 1;
1817 if (sym->attr.proc == PROC_ST_FUNCTION)
1819 gfc_error ("Statement function %qs at %L is not allowed as an "
1820 "actual argument", sym->name, &e->where);
1823 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1824 sym->attr.subroutine);
1825 if (sym->attr.intrinsic && actual_ok == 0)
1827 gfc_error ("Intrinsic %qs at %L is not allowed as an "
1828 "actual argument", sym->name, &e->where);
1831 if (sym->attr.contained && !sym->attr.use_assoc
1832 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1834 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
1835 " used as actual argument at %L",
1836 sym->name, &e->where))
1837 goto cleanup;
1840 if (sym->attr.elemental && !sym->attr.intrinsic)
1842 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
1843 "allowed as an actual argument at %L", sym->name,
1844 &e->where);
1847 /* Check if a generic interface has a specific procedure
1848 with the same name before emitting an error. */
1849 if (sym->attr.generic && count_specific_procs (e) != 1)
1850 goto cleanup;
1852 /* Just in case a specific was found for the expression. */
1853 sym = e->symtree->n.sym;
1855 /* If the symbol is the function that names the current (or
1856 parent) scope, then we really have a variable reference. */
1858 if (gfc_is_function_return_value (sym, sym->ns))
1859 goto got_variable;
1861 /* If all else fails, see if we have a specific intrinsic. */
1862 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1864 gfc_intrinsic_sym *isym;
1866 isym = gfc_find_function (sym->name);
1867 if (isym == NULL || !isym->specific)
1869 gfc_error ("Unable to find a specific INTRINSIC procedure "
1870 "for the reference %qs at %L", sym->name,
1871 &e->where);
1872 goto cleanup;
1874 sym->ts = isym->ts;
1875 sym->attr.intrinsic = 1;
1876 sym->attr.function = 1;
1879 if (!gfc_resolve_expr (e))
1880 goto cleanup;
1881 goto argument_list;
1884 /* See if the name is a module procedure in a parent unit. */
1886 if (was_declared (sym) || sym->ns->parent == NULL)
1887 goto got_variable;
1889 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1891 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
1892 goto cleanup;
1895 if (parent_st == NULL)
1896 goto got_variable;
1898 sym = parent_st->n.sym;
1899 e->symtree = parent_st; /* Point to the right thing. */
1901 if (sym->attr.flavor == FL_PROCEDURE
1902 || sym->attr.intrinsic
1903 || sym->attr.external)
1905 if (!gfc_resolve_expr (e))
1906 goto cleanup;
1907 goto argument_list;
1910 got_variable:
1911 e->expr_type = EXPR_VARIABLE;
1912 e->ts = sym->ts;
1913 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1914 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1915 && CLASS_DATA (sym)->as))
1917 e->rank = sym->ts.type == BT_CLASS
1918 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
1919 e->ref = gfc_get_ref ();
1920 e->ref->type = REF_ARRAY;
1921 e->ref->u.ar.type = AR_FULL;
1922 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1923 ? CLASS_DATA (sym)->as : sym->as;
1926 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1927 primary.c (match_actual_arg). If above code determines that it
1928 is a variable instead, it needs to be resolved as it was not
1929 done at the beginning of this function. */
1930 save_need_full_assumed_size = need_full_assumed_size;
1931 if (e->expr_type != EXPR_VARIABLE)
1932 need_full_assumed_size = 0;
1933 if (!gfc_resolve_expr (e))
1934 goto cleanup;
1935 need_full_assumed_size = save_need_full_assumed_size;
1937 argument_list:
1938 /* Check argument list functions %VAL, %LOC and %REF. There is
1939 nothing to do for %REF. */
1940 if (arg->name && arg->name[0] == '%')
1942 if (strncmp ("%VAL", arg->name, 4) == 0)
1944 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1946 gfc_error ("By-value argument at %L is not of numeric "
1947 "type", &e->where);
1948 goto cleanup;
1951 if (e->rank)
1953 gfc_error ("By-value argument at %L cannot be an array or "
1954 "an array section", &e->where);
1955 goto cleanup;
1958 /* Intrinsics are still PROC_UNKNOWN here. However,
1959 since same file external procedures are not resolvable
1960 in gfortran, it is a good deal easier to leave them to
1961 intrinsic.c. */
1962 if (ptype != PROC_UNKNOWN
1963 && ptype != PROC_DUMMY
1964 && ptype != PROC_EXTERNAL
1965 && ptype != PROC_MODULE)
1967 gfc_error ("By-value argument at %L is not allowed "
1968 "in this context", &e->where);
1969 goto cleanup;
1973 /* Statement functions have already been excluded above. */
1974 else if (strncmp ("%LOC", arg->name, 4) == 0
1975 && e->ts.type == BT_PROCEDURE)
1977 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1979 gfc_error ("Passing internal procedure at %L by location "
1980 "not allowed", &e->where);
1981 goto cleanup;
1986 comp = gfc_get_proc_ptr_comp(e);
1987 if (e->expr_type == EXPR_VARIABLE
1988 && comp && comp->attr.elemental)
1990 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
1991 "allowed as an actual argument at %L", comp->name,
1992 &e->where);
1995 /* Fortran 2008, C1237. */
1996 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1997 && gfc_has_ultimate_pointer (e))
1999 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2000 "component", &e->where);
2001 goto cleanup;
2004 first_actual_arg = false;
2007 return_value = true;
2009 cleanup:
2010 actual_arg = actual_arg_sav;
2011 first_actual_arg = first_actual_arg_sav;
2013 return return_value;
2017 /* Do the checks of the actual argument list that are specific to elemental
2018 procedures. If called with c == NULL, we have a function, otherwise if
2019 expr == NULL, we have a subroutine. */
2021 static bool
2022 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2024 gfc_actual_arglist *arg0;
2025 gfc_actual_arglist *arg;
2026 gfc_symbol *esym = NULL;
2027 gfc_intrinsic_sym *isym = NULL;
2028 gfc_expr *e = NULL;
2029 gfc_intrinsic_arg *iformal = NULL;
2030 gfc_formal_arglist *eformal = NULL;
2031 bool formal_optional = false;
2032 bool set_by_optional = false;
2033 int i;
2034 int rank = 0;
2036 /* Is this an elemental procedure? */
2037 if (expr && expr->value.function.actual != NULL)
2039 if (expr->value.function.esym != NULL
2040 && expr->value.function.esym->attr.elemental)
2042 arg0 = expr->value.function.actual;
2043 esym = expr->value.function.esym;
2045 else if (expr->value.function.isym != NULL
2046 && expr->value.function.isym->elemental)
2048 arg0 = expr->value.function.actual;
2049 isym = expr->value.function.isym;
2051 else
2052 return true;
2054 else if (c && c->ext.actual != NULL)
2056 arg0 = c->ext.actual;
2058 if (c->resolved_sym)
2059 esym = c->resolved_sym;
2060 else
2061 esym = c->symtree->n.sym;
2062 gcc_assert (esym);
2064 if (!esym->attr.elemental)
2065 return true;
2067 else
2068 return true;
2070 /* The rank of an elemental is the rank of its array argument(s). */
2071 for (arg = arg0; arg; arg = arg->next)
2073 if (arg->expr != NULL && arg->expr->rank != 0)
2075 rank = arg->expr->rank;
2076 if (arg->expr->expr_type == EXPR_VARIABLE
2077 && arg->expr->symtree->n.sym->attr.optional)
2078 set_by_optional = true;
2080 /* Function specific; set the result rank and shape. */
2081 if (expr)
2083 expr->rank = rank;
2084 if (!expr->shape && arg->expr->shape)
2086 expr->shape = gfc_get_shape (rank);
2087 for (i = 0; i < rank; i++)
2088 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2091 break;
2095 /* If it is an array, it shall not be supplied as an actual argument
2096 to an elemental procedure unless an array of the same rank is supplied
2097 as an actual argument corresponding to a nonoptional dummy argument of
2098 that elemental procedure(12.4.1.5). */
2099 formal_optional = false;
2100 if (isym)
2101 iformal = isym->formal;
2102 else
2103 eformal = esym->formal;
2105 for (arg = arg0; arg; arg = arg->next)
2107 if (eformal)
2109 if (eformal->sym && eformal->sym->attr.optional)
2110 formal_optional = true;
2111 eformal = eformal->next;
2113 else if (isym && iformal)
2115 if (iformal->optional)
2116 formal_optional = true;
2117 iformal = iformal->next;
2119 else if (isym)
2120 formal_optional = true;
2122 if (pedantic && arg->expr != NULL
2123 && arg->expr->expr_type == EXPR_VARIABLE
2124 && arg->expr->symtree->n.sym->attr.optional
2125 && formal_optional
2126 && arg->expr->rank
2127 && (set_by_optional || arg->expr->rank != rank)
2128 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2130 gfc_warning (0, "%qs at %L is an array and OPTIONAL; IF IT IS "
2131 "MISSING, it cannot be the actual argument of an "
2132 "ELEMENTAL procedure unless there is a non-optional "
2133 "argument with the same rank (12.4.1.5)",
2134 arg->expr->symtree->n.sym->name, &arg->expr->where);
2138 for (arg = arg0; arg; arg = arg->next)
2140 if (arg->expr == NULL || arg->expr->rank == 0)
2141 continue;
2143 /* Being elemental, the last upper bound of an assumed size array
2144 argument must be present. */
2145 if (resolve_assumed_size_actual (arg->expr))
2146 return false;
2148 /* Elemental procedure's array actual arguments must conform. */
2149 if (e != NULL)
2151 if (!gfc_check_conformance (arg->expr, e, "elemental procedure"))
2152 return false;
2154 else
2155 e = arg->expr;
2158 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2159 is an array, the intent inout/out variable needs to be also an array. */
2160 if (rank > 0 && esym && expr == NULL)
2161 for (eformal = esym->formal, arg = arg0; arg && eformal;
2162 arg = arg->next, eformal = eformal->next)
2163 if ((eformal->sym->attr.intent == INTENT_OUT
2164 || eformal->sym->attr.intent == INTENT_INOUT)
2165 && arg->expr && arg->expr->rank == 0)
2167 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2168 "ELEMENTAL subroutine %qs is a scalar, but another "
2169 "actual argument is an array", &arg->expr->where,
2170 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2171 : "INOUT", eformal->sym->name, esym->name);
2172 return false;
2174 return true;
2178 /* This function does the checking of references to global procedures
2179 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2180 77 and 95 standards. It checks for a gsymbol for the name, making
2181 one if it does not already exist. If it already exists, then the
2182 reference being resolved must correspond to the type of gsymbol.
2183 Otherwise, the new symbol is equipped with the attributes of the
2184 reference. The corresponding code that is called in creating
2185 global entities is parse.c.
2187 In addition, for all but -std=legacy, the gsymbols are used to
2188 check the interfaces of external procedures from the same file.
2189 The namespace of the gsymbol is resolved and then, once this is
2190 done the interface is checked. */
2193 static bool
2194 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2196 if (!gsym_ns->proc_name->attr.recursive)
2197 return true;
2199 if (sym->ns == gsym_ns)
2200 return false;
2202 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2203 return false;
2205 return true;
2208 static bool
2209 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2211 if (gsym_ns->entries)
2213 gfc_entry_list *entry = gsym_ns->entries;
2215 for (; entry; entry = entry->next)
2217 if (strcmp (sym->name, entry->sym->name) == 0)
2219 if (strcmp (gsym_ns->proc_name->name,
2220 sym->ns->proc_name->name) == 0)
2221 return false;
2223 if (sym->ns->parent
2224 && strcmp (gsym_ns->proc_name->name,
2225 sym->ns->parent->proc_name->name) == 0)
2226 return false;
2230 return true;
2234 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2236 bool
2237 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2239 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2241 for ( ; arg; arg = arg->next)
2243 if (!arg->sym)
2244 continue;
2246 if (arg->sym->attr.allocatable) /* (2a) */
2248 strncpy (errmsg, _("allocatable argument"), err_len);
2249 return true;
2251 else if (arg->sym->attr.asynchronous)
2253 strncpy (errmsg, _("asynchronous argument"), err_len);
2254 return true;
2256 else if (arg->sym->attr.optional)
2258 strncpy (errmsg, _("optional argument"), err_len);
2259 return true;
2261 else if (arg->sym->attr.pointer)
2263 strncpy (errmsg, _("pointer argument"), err_len);
2264 return true;
2266 else if (arg->sym->attr.target)
2268 strncpy (errmsg, _("target argument"), err_len);
2269 return true;
2271 else if (arg->sym->attr.value)
2273 strncpy (errmsg, _("value argument"), err_len);
2274 return true;
2276 else if (arg->sym->attr.volatile_)
2278 strncpy (errmsg, _("volatile argument"), err_len);
2279 return true;
2281 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2283 strncpy (errmsg, _("assumed-shape argument"), err_len);
2284 return true;
2286 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2288 strncpy (errmsg, _("assumed-rank argument"), err_len);
2289 return true;
2291 else if (arg->sym->attr.codimension) /* (2c) */
2293 strncpy (errmsg, _("coarray argument"), err_len);
2294 return true;
2296 else if (false) /* (2d) TODO: parametrized derived type */
2298 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2299 return true;
2301 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2303 strncpy (errmsg, _("polymorphic argument"), err_len);
2304 return true;
2306 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2308 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2309 return true;
2311 else if (arg->sym->ts.type == BT_ASSUMED)
2313 /* As assumed-type is unlimited polymorphic (cf. above).
2314 See also TS 29113, Note 6.1. */
2315 strncpy (errmsg, _("assumed-type argument"), err_len);
2316 return true;
2320 if (sym->attr.function)
2322 gfc_symbol *res = sym->result ? sym->result : sym;
2324 if (res->attr.dimension) /* (3a) */
2326 strncpy (errmsg, _("array result"), err_len);
2327 return true;
2329 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2331 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2332 return true;
2334 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2335 && res->ts.u.cl->length
2336 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2338 strncpy (errmsg, _("result with non-constant character length"), err_len);
2339 return true;
2343 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2345 strncpy (errmsg, _("elemental procedure"), err_len);
2346 return true;
2348 else if (sym->attr.is_bind_c) /* (5) */
2350 strncpy (errmsg, _("bind(c) procedure"), err_len);
2351 return true;
2354 return false;
2358 static void
2359 resolve_global_procedure (gfc_symbol *sym, locus *where,
2360 gfc_actual_arglist **actual, int sub)
2362 gfc_gsymbol * gsym;
2363 gfc_namespace *ns;
2364 enum gfc_symbol_type type;
2365 char reason[200];
2367 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2369 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name);
2371 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2372 gfc_global_used (gsym, where);
2374 if ((sym->attr.if_source == IFSRC_UNKNOWN
2375 || sym->attr.if_source == IFSRC_IFBODY)
2376 && gsym->type != GSYM_UNKNOWN
2377 && !gsym->binding_label
2378 && gsym->ns
2379 && gsym->ns->resolved != -1
2380 && gsym->ns->proc_name
2381 && not_in_recursive (sym, gsym->ns)
2382 && not_entry_self_reference (sym, gsym->ns))
2384 gfc_symbol *def_sym;
2386 /* Resolve the gsymbol namespace if needed. */
2387 if (!gsym->ns->resolved)
2389 gfc_dt_list *old_dt_list;
2391 /* Stash away derived types so that the backend_decls do not
2392 get mixed up. */
2393 old_dt_list = gfc_derived_types;
2394 gfc_derived_types = NULL;
2396 gfc_resolve (gsym->ns);
2398 /* Store the new derived types with the global namespace. */
2399 if (gfc_derived_types)
2400 gsym->ns->derived_types = gfc_derived_types;
2402 /* Restore the derived types of this namespace. */
2403 gfc_derived_types = old_dt_list;
2406 /* Make sure that translation for the gsymbol occurs before
2407 the procedure currently being resolved. */
2408 ns = gfc_global_ns_list;
2409 for (; ns && ns != gsym->ns; ns = ns->sibling)
2411 if (ns->sibling == gsym->ns)
2413 ns->sibling = gsym->ns->sibling;
2414 gsym->ns->sibling = gfc_global_ns_list;
2415 gfc_global_ns_list = gsym->ns;
2416 break;
2420 def_sym = gsym->ns->proc_name;
2422 /* This can happen if a binding name has been specified. */
2423 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2424 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2426 if (def_sym->attr.entry_master)
2428 gfc_entry_list *entry;
2429 for (entry = gsym->ns->entries; entry; entry = entry->next)
2430 if (strcmp (entry->sym->name, sym->name) == 0)
2432 def_sym = entry->sym;
2433 break;
2437 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2439 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2440 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2441 gfc_typename (&def_sym->ts));
2442 goto done;
2445 if (sym->attr.if_source == IFSRC_UNKNOWN
2446 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2448 gfc_error ("Explicit interface required for %qs at %L: %s",
2449 sym->name, &sym->declared_at, reason);
2450 goto done;
2453 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2454 /* Turn erros into warnings with -std=gnu and -std=legacy. */
2455 gfc_errors_to_warnings (true);
2457 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2458 reason, sizeof(reason), NULL, NULL))
2460 gfc_error ("Interface mismatch in global procedure %qs at %L: %s ",
2461 sym->name, &sym->declared_at, reason);
2462 goto done;
2465 if (!pedantic
2466 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2467 && !(gfc_option.warn_std & GFC_STD_GNU)))
2468 gfc_errors_to_warnings (true);
2470 if (sym->attr.if_source != IFSRC_IFBODY)
2471 gfc_procedure_use (def_sym, actual, where);
2474 done:
2475 gfc_errors_to_warnings (false);
2477 if (gsym->type == GSYM_UNKNOWN)
2479 gsym->type = type;
2480 gsym->where = *where;
2483 gsym->used = 1;
2487 /************* Function resolution *************/
2489 /* Resolve a function call known to be generic.
2490 Section 14.1.2.4.1. */
2492 static match
2493 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2495 gfc_symbol *s;
2497 if (sym->attr.generic)
2499 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2500 if (s != NULL)
2502 expr->value.function.name = s->name;
2503 expr->value.function.esym = s;
2505 if (s->ts.type != BT_UNKNOWN)
2506 expr->ts = s->ts;
2507 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2508 expr->ts = s->result->ts;
2510 if (s->as != NULL)
2511 expr->rank = s->as->rank;
2512 else if (s->result != NULL && s->result->as != NULL)
2513 expr->rank = s->result->as->rank;
2515 gfc_set_sym_referenced (expr->value.function.esym);
2517 return MATCH_YES;
2520 /* TODO: Need to search for elemental references in generic
2521 interface. */
2524 if (sym->attr.intrinsic)
2525 return gfc_intrinsic_func_interface (expr, 0);
2527 return MATCH_NO;
2531 static bool
2532 resolve_generic_f (gfc_expr *expr)
2534 gfc_symbol *sym;
2535 match m;
2536 gfc_interface *intr = NULL;
2538 sym = expr->symtree->n.sym;
2540 for (;;)
2542 m = resolve_generic_f0 (expr, sym);
2543 if (m == MATCH_YES)
2544 return true;
2545 else if (m == MATCH_ERROR)
2546 return false;
2548 generic:
2549 if (!intr)
2550 for (intr = sym->generic; intr; intr = intr->next)
2551 if (intr->sym->attr.flavor == FL_DERIVED)
2552 break;
2554 if (sym->ns->parent == NULL)
2555 break;
2556 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2558 if (sym == NULL)
2559 break;
2560 if (!generic_sym (sym))
2561 goto generic;
2564 /* Last ditch attempt. See if the reference is to an intrinsic
2565 that possesses a matching interface. 14.1.2.4 */
2566 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2568 gfc_error ("There is no specific function for the generic %qs "
2569 "at %L", expr->symtree->n.sym->name, &expr->where);
2570 return false;
2573 if (intr)
2575 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2576 NULL, false))
2577 return false;
2578 return resolve_structure_cons (expr, 0);
2581 m = gfc_intrinsic_func_interface (expr, 0);
2582 if (m == MATCH_YES)
2583 return true;
2585 if (m == MATCH_NO)
2586 gfc_error ("Generic function %qs at %L is not consistent with a "
2587 "specific intrinsic interface", expr->symtree->n.sym->name,
2588 &expr->where);
2590 return false;
2594 /* Resolve a function call known to be specific. */
2596 static match
2597 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2599 match m;
2601 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2603 if (sym->attr.dummy)
2605 sym->attr.proc = PROC_DUMMY;
2606 goto found;
2609 sym->attr.proc = PROC_EXTERNAL;
2610 goto found;
2613 if (sym->attr.proc == PROC_MODULE
2614 || sym->attr.proc == PROC_ST_FUNCTION
2615 || sym->attr.proc == PROC_INTERNAL)
2616 goto found;
2618 if (sym->attr.intrinsic)
2620 m = gfc_intrinsic_func_interface (expr, 1);
2621 if (m == MATCH_YES)
2622 return MATCH_YES;
2623 if (m == MATCH_NO)
2624 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2625 "with an intrinsic", sym->name, &expr->where);
2627 return MATCH_ERROR;
2630 return MATCH_NO;
2632 found:
2633 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2635 if (sym->result)
2636 expr->ts = sym->result->ts;
2637 else
2638 expr->ts = sym->ts;
2639 expr->value.function.name = sym->name;
2640 expr->value.function.esym = sym;
2641 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2642 error(s). */
2643 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2644 return MATCH_ERROR;
2645 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2646 expr->rank = CLASS_DATA (sym)->as->rank;
2647 else if (sym->as != NULL)
2648 expr->rank = sym->as->rank;
2650 return MATCH_YES;
2654 static bool
2655 resolve_specific_f (gfc_expr *expr)
2657 gfc_symbol *sym;
2658 match m;
2660 sym = expr->symtree->n.sym;
2662 for (;;)
2664 m = resolve_specific_f0 (sym, expr);
2665 if (m == MATCH_YES)
2666 return true;
2667 if (m == MATCH_ERROR)
2668 return false;
2670 if (sym->ns->parent == NULL)
2671 break;
2673 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2675 if (sym == NULL)
2676 break;
2679 gfc_error ("Unable to resolve the specific function %qs at %L",
2680 expr->symtree->n.sym->name, &expr->where);
2682 return true;
2686 /* Resolve a procedure call not known to be generic nor specific. */
2688 static bool
2689 resolve_unknown_f (gfc_expr *expr)
2691 gfc_symbol *sym;
2692 gfc_typespec *ts;
2694 sym = expr->symtree->n.sym;
2696 if (sym->attr.dummy)
2698 sym->attr.proc = PROC_DUMMY;
2699 expr->value.function.name = sym->name;
2700 goto set_type;
2703 /* See if we have an intrinsic function reference. */
2705 if (gfc_is_intrinsic (sym, 0, expr->where))
2707 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2708 return true;
2709 return false;
2712 /* The reference is to an external name. */
2714 sym->attr.proc = PROC_EXTERNAL;
2715 expr->value.function.name = sym->name;
2716 expr->value.function.esym = expr->symtree->n.sym;
2718 if (sym->as != NULL)
2719 expr->rank = sym->as->rank;
2721 /* Type of the expression is either the type of the symbol or the
2722 default type of the symbol. */
2724 set_type:
2725 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2727 if (sym->ts.type != BT_UNKNOWN)
2728 expr->ts = sym->ts;
2729 else
2731 ts = gfc_get_default_type (sym->name, sym->ns);
2733 if (ts->type == BT_UNKNOWN)
2735 gfc_error ("Function %qs at %L has no IMPLICIT type",
2736 sym->name, &expr->where);
2737 return false;
2739 else
2740 expr->ts = *ts;
2743 return true;
2747 /* Return true, if the symbol is an external procedure. */
2748 static bool
2749 is_external_proc (gfc_symbol *sym)
2751 if (!sym->attr.dummy && !sym->attr.contained
2752 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2753 && sym->attr.proc != PROC_ST_FUNCTION
2754 && !sym->attr.proc_pointer
2755 && !sym->attr.use_assoc
2756 && sym->name)
2757 return true;
2759 return false;
2763 /* Figure out if a function reference is pure or not. Also set the name
2764 of the function for a potential error message. Return nonzero if the
2765 function is PURE, zero if not. */
2766 static int
2767 pure_stmt_function (gfc_expr *, gfc_symbol *);
2769 static int
2770 pure_function (gfc_expr *e, const char **name)
2772 int pure;
2773 gfc_component *comp;
2775 *name = NULL;
2777 if (e->symtree != NULL
2778 && e->symtree->n.sym != NULL
2779 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2780 return pure_stmt_function (e, e->symtree->n.sym);
2782 comp = gfc_get_proc_ptr_comp (e);
2783 if (comp)
2785 pure = gfc_pure (comp->ts.interface);
2786 *name = comp->name;
2788 else if (e->value.function.esym)
2790 pure = gfc_pure (e->value.function.esym);
2791 *name = e->value.function.esym->name;
2793 else if (e->value.function.isym)
2795 pure = e->value.function.isym->pure
2796 || e->value.function.isym->elemental;
2797 *name = e->value.function.isym->name;
2799 else
2801 /* Implicit functions are not pure. */
2802 pure = 0;
2803 *name = e->value.function.name;
2806 return pure;
2810 static bool
2811 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2812 int *f ATTRIBUTE_UNUSED)
2814 const char *name;
2816 /* Don't bother recursing into other statement functions
2817 since they will be checked individually for purity. */
2818 if (e->expr_type != EXPR_FUNCTION
2819 || !e->symtree
2820 || e->symtree->n.sym == sym
2821 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2822 return false;
2824 return pure_function (e, &name) ? false : true;
2828 static int
2829 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2831 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2835 /* Check if an impure function is allowed in the current context. */
2837 static bool check_pure_function (gfc_expr *e)
2839 const char *name = NULL;
2840 if (!pure_function (e, &name) && name)
2842 if (forall_flag)
2844 gfc_error ("Reference to impure function %qs at %L inside a "
2845 "FORALL %s", name, &e->where,
2846 forall_flag == 2 ? "mask" : "block");
2847 return false;
2849 else if (gfc_do_concurrent_flag)
2851 gfc_error ("Reference to impure function %qs at %L inside a "
2852 "DO CONCURRENT %s", name, &e->where,
2853 gfc_do_concurrent_flag == 2 ? "mask" : "block");
2854 return false;
2856 else if (gfc_pure (NULL))
2858 gfc_error ("Reference to impure function %qs at %L "
2859 "within a PURE procedure", name, &e->where);
2860 return false;
2862 gfc_unset_implicit_pure (NULL);
2864 return true;
2868 /* Update current procedure's array_outer_dependency flag, considering
2869 a call to procedure SYM. */
2871 static void
2872 update_current_proc_array_outer_dependency (gfc_symbol *sym)
2874 /* Check to see if this is a sibling function that has not yet
2875 been resolved. */
2876 gfc_namespace *sibling = gfc_current_ns->sibling;
2877 for (; sibling; sibling = sibling->sibling)
2879 if (sibling->proc_name == sym)
2881 gfc_resolve (sibling);
2882 break;
2886 /* If SYM has references to outer arrays, so has the procedure calling
2887 SYM. If SYM is a procedure pointer, we can assume the worst. */
2888 if (sym->attr.array_outer_dependency
2889 || sym->attr.proc_pointer)
2890 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
2894 /* Resolve a function call, which means resolving the arguments, then figuring
2895 out which entity the name refers to. */
2897 static bool
2898 resolve_function (gfc_expr *expr)
2900 gfc_actual_arglist *arg;
2901 gfc_symbol *sym;
2902 bool t;
2903 int temp;
2904 procedure_type p = PROC_INTRINSIC;
2905 bool no_formal_args;
2907 sym = NULL;
2908 if (expr->symtree)
2909 sym = expr->symtree->n.sym;
2911 /* If this is a procedure pointer component, it has already been resolved. */
2912 if (gfc_is_proc_ptr_comp (expr))
2913 return true;
2915 if (sym && sym->attr.intrinsic
2916 && !gfc_resolve_intrinsic (sym, &expr->where))
2917 return false;
2919 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2921 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
2922 return false;
2925 /* If this ia a deferred TBP with an abstract interface (which may
2926 of course be referenced), expr->value.function.esym will be set. */
2927 if (sym && sym->attr.abstract && !expr->value.function.esym)
2929 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
2930 sym->name, &expr->where);
2931 return false;
2934 /* Switch off assumed size checking and do this again for certain kinds
2935 of procedure, once the procedure itself is resolved. */
2936 need_full_assumed_size++;
2938 if (expr->symtree && expr->symtree->n.sym)
2939 p = expr->symtree->n.sym->attr.proc;
2941 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2942 inquiry_argument = true;
2943 no_formal_args = sym && is_external_proc (sym)
2944 && gfc_sym_get_dummy_args (sym) == NULL;
2946 if (!resolve_actual_arglist (expr->value.function.actual,
2947 p, no_formal_args))
2949 inquiry_argument = false;
2950 return false;
2953 inquiry_argument = false;
2955 /* Resume assumed_size checking. */
2956 need_full_assumed_size--;
2958 /* If the procedure is external, check for usage. */
2959 if (sym && is_external_proc (sym))
2960 resolve_global_procedure (sym, &expr->where,
2961 &expr->value.function.actual, 0);
2963 if (sym && sym->ts.type == BT_CHARACTER
2964 && sym->ts.u.cl
2965 && sym->ts.u.cl->length == NULL
2966 && !sym->attr.dummy
2967 && !sym->ts.deferred
2968 && expr->value.function.esym == NULL
2969 && !sym->attr.contained)
2971 /* Internal procedures are taken care of in resolve_contained_fntype. */
2972 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
2973 "be used at %L since it is not a dummy argument",
2974 sym->name, &expr->where);
2975 return false;
2978 /* See if function is already resolved. */
2980 if (expr->value.function.name != NULL
2981 || expr->value.function.isym != NULL)
2983 if (expr->ts.type == BT_UNKNOWN)
2984 expr->ts = sym->ts;
2985 t = true;
2987 else
2989 /* Apply the rules of section 14.1.2. */
2991 switch (procedure_kind (sym))
2993 case PTYPE_GENERIC:
2994 t = resolve_generic_f (expr);
2995 break;
2997 case PTYPE_SPECIFIC:
2998 t = resolve_specific_f (expr);
2999 break;
3001 case PTYPE_UNKNOWN:
3002 t = resolve_unknown_f (expr);
3003 break;
3005 default:
3006 gfc_internal_error ("resolve_function(): bad function type");
3010 /* If the expression is still a function (it might have simplified),
3011 then we check to see if we are calling an elemental function. */
3013 if (expr->expr_type != EXPR_FUNCTION)
3014 return t;
3016 temp = need_full_assumed_size;
3017 need_full_assumed_size = 0;
3019 if (!resolve_elemental_actual (expr, NULL))
3020 return false;
3022 if (omp_workshare_flag
3023 && expr->value.function.esym
3024 && ! gfc_elemental (expr->value.function.esym))
3026 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3027 "in WORKSHARE construct", expr->value.function.esym->name,
3028 &expr->where);
3029 t = false;
3032 #define GENERIC_ID expr->value.function.isym->id
3033 else if (expr->value.function.actual != NULL
3034 && expr->value.function.isym != NULL
3035 && GENERIC_ID != GFC_ISYM_LBOUND
3036 && GENERIC_ID != GFC_ISYM_LCOBOUND
3037 && GENERIC_ID != GFC_ISYM_UCOBOUND
3038 && GENERIC_ID != GFC_ISYM_LEN
3039 && GENERIC_ID != GFC_ISYM_LOC
3040 && GENERIC_ID != GFC_ISYM_C_LOC
3041 && GENERIC_ID != GFC_ISYM_PRESENT)
3043 /* Array intrinsics must also have the last upper bound of an
3044 assumed size array argument. UBOUND and SIZE have to be
3045 excluded from the check if the second argument is anything
3046 than a constant. */
3048 for (arg = expr->value.function.actual; arg; arg = arg->next)
3050 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3051 && arg == expr->value.function.actual
3052 && arg->next != NULL && arg->next->expr)
3054 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3055 break;
3057 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
3058 break;
3060 if ((int)mpz_get_si (arg->next->expr->value.integer)
3061 < arg->expr->rank)
3062 break;
3065 if (arg->expr != NULL
3066 && arg->expr->rank > 0
3067 && resolve_assumed_size_actual (arg->expr))
3068 return false;
3071 #undef GENERIC_ID
3073 need_full_assumed_size = temp;
3075 if (!check_pure_function(expr))
3076 t = false;
3078 /* Functions without the RECURSIVE attribution are not allowed to
3079 * call themselves. */
3080 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3082 gfc_symbol *esym;
3083 esym = expr->value.function.esym;
3085 if (is_illegal_recursion (esym, gfc_current_ns))
3087 if (esym->attr.entry && esym->ns->entries)
3088 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3089 " function %qs is not RECURSIVE",
3090 esym->name, &expr->where, esym->ns->entries->sym->name);
3091 else
3092 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3093 " is not RECURSIVE", esym->name, &expr->where);
3095 t = false;
3099 /* Character lengths of use associated functions may contains references to
3100 symbols not referenced from the current program unit otherwise. Make sure
3101 those symbols are marked as referenced. */
3103 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3104 && expr->value.function.esym->attr.use_assoc)
3106 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3109 /* Make sure that the expression has a typespec that works. */
3110 if (expr->ts.type == BT_UNKNOWN)
3112 if (expr->symtree->n.sym->result
3113 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3114 && !expr->symtree->n.sym->result->attr.proc_pointer)
3115 expr->ts = expr->symtree->n.sym->result->ts;
3118 if (!expr->ref && !expr->value.function.isym)
3120 if (expr->value.function.esym)
3121 update_current_proc_array_outer_dependency (expr->value.function.esym);
3122 else
3123 update_current_proc_array_outer_dependency (sym);
3125 else if (expr->ref)
3126 /* typebound procedure: Assume the worst. */
3127 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3129 return t;
3133 /************* Subroutine resolution *************/
3135 static bool
3136 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3138 if (gfc_pure (sym))
3139 return true;
3141 if (forall_flag)
3143 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3144 name, loc);
3145 return false;
3147 else if (gfc_do_concurrent_flag)
3149 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3150 "PURE", name, loc);
3151 return false;
3153 else if (gfc_pure (NULL))
3155 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3156 return false;
3159 gfc_unset_implicit_pure (NULL);
3160 return true;
3164 static match
3165 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3167 gfc_symbol *s;
3169 if (sym->attr.generic)
3171 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3172 if (s != NULL)
3174 c->resolved_sym = s;
3175 if (!pure_subroutine (s, s->name, &c->loc))
3176 return MATCH_ERROR;
3177 return MATCH_YES;
3180 /* TODO: Need to search for elemental references in generic interface. */
3183 if (sym->attr.intrinsic)
3184 return gfc_intrinsic_sub_interface (c, 0);
3186 return MATCH_NO;
3190 static bool
3191 resolve_generic_s (gfc_code *c)
3193 gfc_symbol *sym;
3194 match m;
3196 sym = c->symtree->n.sym;
3198 for (;;)
3200 m = resolve_generic_s0 (c, sym);
3201 if (m == MATCH_YES)
3202 return true;
3203 else if (m == MATCH_ERROR)
3204 return false;
3206 generic:
3207 if (sym->ns->parent == NULL)
3208 break;
3209 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3211 if (sym == NULL)
3212 break;
3213 if (!generic_sym (sym))
3214 goto generic;
3217 /* Last ditch attempt. See if the reference is to an intrinsic
3218 that possesses a matching interface. 14.1.2.4 */
3219 sym = c->symtree->n.sym;
3221 if (!gfc_is_intrinsic (sym, 1, c->loc))
3223 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3224 sym->name, &c->loc);
3225 return false;
3228 m = gfc_intrinsic_sub_interface (c, 0);
3229 if (m == MATCH_YES)
3230 return true;
3231 if (m == MATCH_NO)
3232 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3233 "intrinsic subroutine interface", sym->name, &c->loc);
3235 return false;
3239 /* Resolve a subroutine call known to be specific. */
3241 static match
3242 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3244 match m;
3246 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3248 if (sym->attr.dummy)
3250 sym->attr.proc = PROC_DUMMY;
3251 goto found;
3254 sym->attr.proc = PROC_EXTERNAL;
3255 goto found;
3258 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3259 goto found;
3261 if (sym->attr.intrinsic)
3263 m = gfc_intrinsic_sub_interface (c, 1);
3264 if (m == MATCH_YES)
3265 return MATCH_YES;
3266 if (m == MATCH_NO)
3267 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3268 "with an intrinsic", sym->name, &c->loc);
3270 return MATCH_ERROR;
3273 return MATCH_NO;
3275 found:
3276 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3278 c->resolved_sym = sym;
3279 if (!pure_subroutine (sym, sym->name, &c->loc))
3280 return MATCH_ERROR;
3282 return MATCH_YES;
3286 static bool
3287 resolve_specific_s (gfc_code *c)
3289 gfc_symbol *sym;
3290 match m;
3292 sym = c->symtree->n.sym;
3294 for (;;)
3296 m = resolve_specific_s0 (c, sym);
3297 if (m == MATCH_YES)
3298 return true;
3299 if (m == MATCH_ERROR)
3300 return false;
3302 if (sym->ns->parent == NULL)
3303 break;
3305 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3307 if (sym == NULL)
3308 break;
3311 sym = c->symtree->n.sym;
3312 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3313 sym->name, &c->loc);
3315 return false;
3319 /* Resolve a subroutine call not known to be generic nor specific. */
3321 static bool
3322 resolve_unknown_s (gfc_code *c)
3324 gfc_symbol *sym;
3326 sym = c->symtree->n.sym;
3328 if (sym->attr.dummy)
3330 sym->attr.proc = PROC_DUMMY;
3331 goto found;
3334 /* See if we have an intrinsic function reference. */
3336 if (gfc_is_intrinsic (sym, 1, c->loc))
3338 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3339 return true;
3340 return false;
3343 /* The reference is to an external name. */
3345 found:
3346 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3348 c->resolved_sym = sym;
3350 return pure_subroutine (sym, sym->name, &c->loc);
3354 /* Resolve a subroutine call. Although it was tempting to use the same code
3355 for functions, subroutines and functions are stored differently and this
3356 makes things awkward. */
3358 static bool
3359 resolve_call (gfc_code *c)
3361 bool t;
3362 procedure_type ptype = PROC_INTRINSIC;
3363 gfc_symbol *csym, *sym;
3364 bool no_formal_args;
3366 csym = c->symtree ? c->symtree->n.sym : NULL;
3368 if (csym && csym->ts.type != BT_UNKNOWN)
3370 gfc_error ("%qs at %L has a type, which is not consistent with "
3371 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3372 return false;
3375 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3377 gfc_symtree *st;
3378 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3379 sym = st ? st->n.sym : NULL;
3380 if (sym && csym != sym
3381 && sym->ns == gfc_current_ns
3382 && sym->attr.flavor == FL_PROCEDURE
3383 && sym->attr.contained)
3385 sym->refs++;
3386 if (csym->attr.generic)
3387 c->symtree->n.sym = sym;
3388 else
3389 c->symtree = st;
3390 csym = c->symtree->n.sym;
3394 /* If this ia a deferred TBP, c->expr1 will be set. */
3395 if (!c->expr1 && csym)
3397 if (csym->attr.abstract)
3399 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3400 csym->name, &c->loc);
3401 return false;
3404 /* Subroutines without the RECURSIVE attribution are not allowed to
3405 call themselves. */
3406 if (is_illegal_recursion (csym, gfc_current_ns))
3408 if (csym->attr.entry && csym->ns->entries)
3409 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3410 "as subroutine %qs is not RECURSIVE",
3411 csym->name, &c->loc, csym->ns->entries->sym->name);
3412 else
3413 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3414 "as it is not RECURSIVE", csym->name, &c->loc);
3416 t = false;
3420 /* Switch off assumed size checking and do this again for certain kinds
3421 of procedure, once the procedure itself is resolved. */
3422 need_full_assumed_size++;
3424 if (csym)
3425 ptype = csym->attr.proc;
3427 no_formal_args = csym && is_external_proc (csym)
3428 && gfc_sym_get_dummy_args (csym) == NULL;
3429 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3430 return false;
3432 /* Resume assumed_size checking. */
3433 need_full_assumed_size--;
3435 /* If external, check for usage. */
3436 if (csym && is_external_proc (csym))
3437 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3439 t = true;
3440 if (c->resolved_sym == NULL)
3442 c->resolved_isym = NULL;
3443 switch (procedure_kind (csym))
3445 case PTYPE_GENERIC:
3446 t = resolve_generic_s (c);
3447 break;
3449 case PTYPE_SPECIFIC:
3450 t = resolve_specific_s (c);
3451 break;
3453 case PTYPE_UNKNOWN:
3454 t = resolve_unknown_s (c);
3455 break;
3457 default:
3458 gfc_internal_error ("resolve_subroutine(): bad function type");
3462 /* Some checks of elemental subroutine actual arguments. */
3463 if (!resolve_elemental_actual (NULL, c))
3464 return false;
3466 if (!c->expr1)
3467 update_current_proc_array_outer_dependency (csym);
3468 else
3469 /* Typebound procedure: Assume the worst. */
3470 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3472 return t;
3476 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3477 op1->shape and op2->shape are non-NULL return true if their shapes
3478 match. If both op1->shape and op2->shape are non-NULL return false
3479 if their shapes do not match. If either op1->shape or op2->shape is
3480 NULL, return true. */
3482 static bool
3483 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3485 bool t;
3486 int i;
3488 t = true;
3490 if (op1->shape != NULL && op2->shape != NULL)
3492 for (i = 0; i < op1->rank; i++)
3494 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3496 gfc_error ("Shapes for operands at %L and %L are not conformable",
3497 &op1->where, &op2->where);
3498 t = false;
3499 break;
3504 return t;
3508 /* Resolve an operator expression node. This can involve replacing the
3509 operation with a user defined function call. */
3511 static bool
3512 resolve_operator (gfc_expr *e)
3514 gfc_expr *op1, *op2;
3515 char msg[200];
3516 bool dual_locus_error;
3517 bool t;
3519 /* Resolve all subnodes-- give them types. */
3521 switch (e->value.op.op)
3523 default:
3524 if (!gfc_resolve_expr (e->value.op.op2))
3525 return false;
3527 /* Fall through... */
3529 case INTRINSIC_NOT:
3530 case INTRINSIC_UPLUS:
3531 case INTRINSIC_UMINUS:
3532 case INTRINSIC_PARENTHESES:
3533 if (!gfc_resolve_expr (e->value.op.op1))
3534 return false;
3535 break;
3538 /* Typecheck the new node. */
3540 op1 = e->value.op.op1;
3541 op2 = e->value.op.op2;
3542 dual_locus_error = false;
3544 if ((op1 && op1->expr_type == EXPR_NULL)
3545 || (op2 && op2->expr_type == EXPR_NULL))
3547 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3548 goto bad_op;
3551 switch (e->value.op.op)
3553 case INTRINSIC_UPLUS:
3554 case INTRINSIC_UMINUS:
3555 if (op1->ts.type == BT_INTEGER
3556 || op1->ts.type == BT_REAL
3557 || op1->ts.type == BT_COMPLEX)
3559 e->ts = op1->ts;
3560 break;
3563 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3564 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3565 goto bad_op;
3567 case INTRINSIC_PLUS:
3568 case INTRINSIC_MINUS:
3569 case INTRINSIC_TIMES:
3570 case INTRINSIC_DIVIDE:
3571 case INTRINSIC_POWER:
3572 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3574 gfc_type_convert_binary (e, 1);
3575 break;
3578 sprintf (msg,
3579 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3580 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3581 gfc_typename (&op2->ts));
3582 goto bad_op;
3584 case INTRINSIC_CONCAT:
3585 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3586 && op1->ts.kind == op2->ts.kind)
3588 e->ts.type = BT_CHARACTER;
3589 e->ts.kind = op1->ts.kind;
3590 break;
3593 sprintf (msg,
3594 _("Operands of string concatenation operator at %%L are %s/%s"),
3595 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3596 goto bad_op;
3598 case INTRINSIC_AND:
3599 case INTRINSIC_OR:
3600 case INTRINSIC_EQV:
3601 case INTRINSIC_NEQV:
3602 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3604 e->ts.type = BT_LOGICAL;
3605 e->ts.kind = gfc_kind_max (op1, op2);
3606 if (op1->ts.kind < e->ts.kind)
3607 gfc_convert_type (op1, &e->ts, 2);
3608 else if (op2->ts.kind < e->ts.kind)
3609 gfc_convert_type (op2, &e->ts, 2);
3610 break;
3613 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3614 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3615 gfc_typename (&op2->ts));
3617 goto bad_op;
3619 case INTRINSIC_NOT:
3620 if (op1->ts.type == BT_LOGICAL)
3622 e->ts.type = BT_LOGICAL;
3623 e->ts.kind = op1->ts.kind;
3624 break;
3627 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3628 gfc_typename (&op1->ts));
3629 goto bad_op;
3631 case INTRINSIC_GT:
3632 case INTRINSIC_GT_OS:
3633 case INTRINSIC_GE:
3634 case INTRINSIC_GE_OS:
3635 case INTRINSIC_LT:
3636 case INTRINSIC_LT_OS:
3637 case INTRINSIC_LE:
3638 case INTRINSIC_LE_OS:
3639 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3641 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3642 goto bad_op;
3645 /* Fall through... */
3647 case INTRINSIC_EQ:
3648 case INTRINSIC_EQ_OS:
3649 case INTRINSIC_NE:
3650 case INTRINSIC_NE_OS:
3651 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3652 && op1->ts.kind == op2->ts.kind)
3654 e->ts.type = BT_LOGICAL;
3655 e->ts.kind = gfc_default_logical_kind;
3656 break;
3659 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3661 gfc_type_convert_binary (e, 1);
3663 e->ts.type = BT_LOGICAL;
3664 e->ts.kind = gfc_default_logical_kind;
3666 if (warn_compare_reals)
3668 gfc_intrinsic_op op = e->value.op.op;
3670 /* Type conversion has made sure that the types of op1 and op2
3671 agree, so it is only necessary to check the first one. */
3672 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3673 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3674 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3676 const char *msg;
3678 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3679 msg = "Equality comparison for %s at %L";
3680 else
3681 msg = "Inequality comparison for %s at %L";
3683 gfc_warning (0, msg, gfc_typename (&op1->ts), &op1->where);
3687 break;
3690 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3691 sprintf (msg,
3692 _("Logicals at %%L must be compared with %s instead of %s"),
3693 (e->value.op.op == INTRINSIC_EQ
3694 || e->value.op.op == INTRINSIC_EQ_OS)
3695 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3696 else
3697 sprintf (msg,
3698 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3699 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3700 gfc_typename (&op2->ts));
3702 goto bad_op;
3704 case INTRINSIC_USER:
3705 if (e->value.op.uop->op == NULL)
3706 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3707 else if (op2 == NULL)
3708 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3709 e->value.op.uop->name, gfc_typename (&op1->ts));
3710 else
3712 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3713 e->value.op.uop->name, gfc_typename (&op1->ts),
3714 gfc_typename (&op2->ts));
3715 e->value.op.uop->op->sym->attr.referenced = 1;
3718 goto bad_op;
3720 case INTRINSIC_PARENTHESES:
3721 e->ts = op1->ts;
3722 if (e->ts.type == BT_CHARACTER)
3723 e->ts.u.cl = op1->ts.u.cl;
3724 break;
3726 default:
3727 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3730 /* Deal with arrayness of an operand through an operator. */
3732 t = true;
3734 switch (e->value.op.op)
3736 case INTRINSIC_PLUS:
3737 case INTRINSIC_MINUS:
3738 case INTRINSIC_TIMES:
3739 case INTRINSIC_DIVIDE:
3740 case INTRINSIC_POWER:
3741 case INTRINSIC_CONCAT:
3742 case INTRINSIC_AND:
3743 case INTRINSIC_OR:
3744 case INTRINSIC_EQV:
3745 case INTRINSIC_NEQV:
3746 case INTRINSIC_EQ:
3747 case INTRINSIC_EQ_OS:
3748 case INTRINSIC_NE:
3749 case INTRINSIC_NE_OS:
3750 case INTRINSIC_GT:
3751 case INTRINSIC_GT_OS:
3752 case INTRINSIC_GE:
3753 case INTRINSIC_GE_OS:
3754 case INTRINSIC_LT:
3755 case INTRINSIC_LT_OS:
3756 case INTRINSIC_LE:
3757 case INTRINSIC_LE_OS:
3759 if (op1->rank == 0 && op2->rank == 0)
3760 e->rank = 0;
3762 if (op1->rank == 0 && op2->rank != 0)
3764 e->rank = op2->rank;
3766 if (e->shape == NULL)
3767 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3770 if (op1->rank != 0 && op2->rank == 0)
3772 e->rank = op1->rank;
3774 if (e->shape == NULL)
3775 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3778 if (op1->rank != 0 && op2->rank != 0)
3780 if (op1->rank == op2->rank)
3782 e->rank = op1->rank;
3783 if (e->shape == NULL)
3785 t = compare_shapes (op1, op2);
3786 if (!t)
3787 e->shape = NULL;
3788 else
3789 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3792 else
3794 /* Allow higher level expressions to work. */
3795 e->rank = 0;
3797 /* Try user-defined operators, and otherwise throw an error. */
3798 dual_locus_error = true;
3799 sprintf (msg,
3800 _("Inconsistent ranks for operator at %%L and %%L"));
3801 goto bad_op;
3805 break;
3807 case INTRINSIC_PARENTHESES:
3808 case INTRINSIC_NOT:
3809 case INTRINSIC_UPLUS:
3810 case INTRINSIC_UMINUS:
3811 /* Simply copy arrayness attribute */
3812 e->rank = op1->rank;
3814 if (e->shape == NULL)
3815 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3817 break;
3819 default:
3820 break;
3823 /* Attempt to simplify the expression. */
3824 if (t)
3826 t = gfc_simplify_expr (e, 0);
3827 /* Some calls do not succeed in simplification and return false
3828 even though there is no error; e.g. variable references to
3829 PARAMETER arrays. */
3830 if (!gfc_is_constant_expr (e))
3831 t = true;
3833 return t;
3835 bad_op:
3838 match m = gfc_extend_expr (e);
3839 if (m == MATCH_YES)
3840 return true;
3841 if (m == MATCH_ERROR)
3842 return false;
3845 if (dual_locus_error)
3846 gfc_error (msg, &op1->where, &op2->where);
3847 else
3848 gfc_error (msg, &e->where);
3850 return false;
3854 /************** Array resolution subroutines **************/
3856 enum compare_result
3857 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
3859 /* Compare two integer expressions. */
3861 static compare_result
3862 compare_bound (gfc_expr *a, gfc_expr *b)
3864 int i;
3866 if (a == NULL || a->expr_type != EXPR_CONSTANT
3867 || b == NULL || b->expr_type != EXPR_CONSTANT)
3868 return CMP_UNKNOWN;
3870 /* If either of the types isn't INTEGER, we must have
3871 raised an error earlier. */
3873 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3874 return CMP_UNKNOWN;
3876 i = mpz_cmp (a->value.integer, b->value.integer);
3878 if (i < 0)
3879 return CMP_LT;
3880 if (i > 0)
3881 return CMP_GT;
3882 return CMP_EQ;
3886 /* Compare an integer expression with an integer. */
3888 static compare_result
3889 compare_bound_int (gfc_expr *a, int b)
3891 int i;
3893 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3894 return CMP_UNKNOWN;
3896 if (a->ts.type != BT_INTEGER)
3897 gfc_internal_error ("compare_bound_int(): Bad expression");
3899 i = mpz_cmp_si (a->value.integer, b);
3901 if (i < 0)
3902 return CMP_LT;
3903 if (i > 0)
3904 return CMP_GT;
3905 return CMP_EQ;
3909 /* Compare an integer expression with a mpz_t. */
3911 static compare_result
3912 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3914 int i;
3916 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3917 return CMP_UNKNOWN;
3919 if (a->ts.type != BT_INTEGER)
3920 gfc_internal_error ("compare_bound_int(): Bad expression");
3922 i = mpz_cmp (a->value.integer, b);
3924 if (i < 0)
3925 return CMP_LT;
3926 if (i > 0)
3927 return CMP_GT;
3928 return CMP_EQ;
3932 /* Compute the last value of a sequence given by a triplet.
3933 Return 0 if it wasn't able to compute the last value, or if the
3934 sequence if empty, and 1 otherwise. */
3936 static int
3937 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3938 gfc_expr *stride, mpz_t last)
3940 mpz_t rem;
3942 if (start == NULL || start->expr_type != EXPR_CONSTANT
3943 || end == NULL || end->expr_type != EXPR_CONSTANT
3944 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3945 return 0;
3947 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3948 || (stride != NULL && stride->ts.type != BT_INTEGER))
3949 return 0;
3951 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
3953 if (compare_bound (start, end) == CMP_GT)
3954 return 0;
3955 mpz_set (last, end->value.integer);
3956 return 1;
3959 if (compare_bound_int (stride, 0) == CMP_GT)
3961 /* Stride is positive */
3962 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3963 return 0;
3965 else
3967 /* Stride is negative */
3968 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3969 return 0;
3972 mpz_init (rem);
3973 mpz_sub (rem, end->value.integer, start->value.integer);
3974 mpz_tdiv_r (rem, rem, stride->value.integer);
3975 mpz_sub (last, end->value.integer, rem);
3976 mpz_clear (rem);
3978 return 1;
3982 /* Compare a single dimension of an array reference to the array
3983 specification. */
3985 static bool
3986 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3988 mpz_t last_value;
3990 if (ar->dimen_type[i] == DIMEN_STAR)
3992 gcc_assert (ar->stride[i] == NULL);
3993 /* This implies [*] as [*:] and [*:3] are not possible. */
3994 if (ar->start[i] == NULL)
3996 gcc_assert (ar->end[i] == NULL);
3997 return true;
4001 /* Given start, end and stride values, calculate the minimum and
4002 maximum referenced indexes. */
4004 switch (ar->dimen_type[i])
4006 case DIMEN_VECTOR:
4007 case DIMEN_THIS_IMAGE:
4008 break;
4010 case DIMEN_STAR:
4011 case DIMEN_ELEMENT:
4012 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4014 if (i < as->rank)
4015 gfc_warning (0, "Array reference at %L is out of bounds "
4016 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4017 mpz_get_si (ar->start[i]->value.integer),
4018 mpz_get_si (as->lower[i]->value.integer), i+1);
4019 else
4020 gfc_warning (0, "Array reference at %L is out of bounds "
4021 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4022 mpz_get_si (ar->start[i]->value.integer),
4023 mpz_get_si (as->lower[i]->value.integer),
4024 i + 1 - as->rank);
4025 return true;
4027 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4029 if (i < as->rank)
4030 gfc_warning (0, "Array reference at %L is out of bounds "
4031 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4032 mpz_get_si (ar->start[i]->value.integer),
4033 mpz_get_si (as->upper[i]->value.integer), i+1);
4034 else
4035 gfc_warning (0, "Array reference at %L is out of bounds "
4036 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4037 mpz_get_si (ar->start[i]->value.integer),
4038 mpz_get_si (as->upper[i]->value.integer),
4039 i + 1 - as->rank);
4040 return true;
4043 break;
4045 case DIMEN_RANGE:
4047 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4048 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4050 compare_result comp_start_end = compare_bound (AR_START, AR_END);
4052 /* Check for zero stride, which is not allowed. */
4053 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4055 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4056 return false;
4059 /* if start == len || (stride > 0 && start < len)
4060 || (stride < 0 && start > len),
4061 then the array section contains at least one element. In this
4062 case, there is an out-of-bounds access if
4063 (start < lower || start > upper). */
4064 if (compare_bound (AR_START, AR_END) == CMP_EQ
4065 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4066 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4067 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4068 && comp_start_end == CMP_GT))
4070 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4072 gfc_warning (0, "Lower array reference at %L is out of bounds "
4073 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4074 mpz_get_si (AR_START->value.integer),
4075 mpz_get_si (as->lower[i]->value.integer), i+1);
4076 return true;
4078 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4080 gfc_warning (0, "Lower array reference at %L is out of bounds "
4081 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4082 mpz_get_si (AR_START->value.integer),
4083 mpz_get_si (as->upper[i]->value.integer), i+1);
4084 return true;
4088 /* If we can compute the highest index of the array section,
4089 then it also has to be between lower and upper. */
4090 mpz_init (last_value);
4091 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4092 last_value))
4094 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4096 gfc_warning (0, "Upper array reference at %L is out of bounds "
4097 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4098 mpz_get_si (last_value),
4099 mpz_get_si (as->lower[i]->value.integer), i+1);
4100 mpz_clear (last_value);
4101 return true;
4103 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4105 gfc_warning (0, "Upper array reference at %L is out of bounds "
4106 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4107 mpz_get_si (last_value),
4108 mpz_get_si (as->upper[i]->value.integer), i+1);
4109 mpz_clear (last_value);
4110 return true;
4113 mpz_clear (last_value);
4115 #undef AR_START
4116 #undef AR_END
4118 break;
4120 default:
4121 gfc_internal_error ("check_dimension(): Bad array reference");
4124 return true;
4128 /* Compare an array reference with an array specification. */
4130 static bool
4131 compare_spec_to_ref (gfc_array_ref *ar)
4133 gfc_array_spec *as;
4134 int i;
4136 as = ar->as;
4137 i = as->rank - 1;
4138 /* TODO: Full array sections are only allowed as actual parameters. */
4139 if (as->type == AS_ASSUMED_SIZE
4140 && (/*ar->type == AR_FULL
4141 ||*/ (ar->type == AR_SECTION
4142 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4144 gfc_error ("Rightmost upper bound of assumed size array section "
4145 "not specified at %L", &ar->where);
4146 return false;
4149 if (ar->type == AR_FULL)
4150 return true;
4152 if (as->rank != ar->dimen)
4154 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4155 &ar->where, ar->dimen, as->rank);
4156 return false;
4159 /* ar->codimen == 0 is a local array. */
4160 if (as->corank != ar->codimen && ar->codimen != 0)
4162 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4163 &ar->where, ar->codimen, as->corank);
4164 return false;
4167 for (i = 0; i < as->rank; i++)
4168 if (!check_dimension (i, ar, as))
4169 return false;
4171 /* Local access has no coarray spec. */
4172 if (ar->codimen != 0)
4173 for (i = as->rank; i < as->rank + as->corank; i++)
4175 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4176 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4178 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4179 i + 1 - as->rank, &ar->where);
4180 return false;
4182 if (!check_dimension (i, ar, as))
4183 return false;
4186 return true;
4190 /* Resolve one part of an array index. */
4192 static bool
4193 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4194 int force_index_integer_kind)
4196 gfc_typespec ts;
4198 if (index == NULL)
4199 return true;
4201 if (!gfc_resolve_expr (index))
4202 return false;
4204 if (check_scalar && index->rank != 0)
4206 gfc_error ("Array index at %L must be scalar", &index->where);
4207 return false;
4210 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4212 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4213 &index->where, gfc_basic_typename (index->ts.type));
4214 return false;
4217 if (index->ts.type == BT_REAL)
4218 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4219 &index->where))
4220 return false;
4222 if ((index->ts.kind != gfc_index_integer_kind
4223 && force_index_integer_kind)
4224 || index->ts.type != BT_INTEGER)
4226 gfc_clear_ts (&ts);
4227 ts.type = BT_INTEGER;
4228 ts.kind = gfc_index_integer_kind;
4230 gfc_convert_type_warn (index, &ts, 2, 0);
4233 return true;
4236 /* Resolve one part of an array index. */
4238 bool
4239 gfc_resolve_index (gfc_expr *index, int check_scalar)
4241 return gfc_resolve_index_1 (index, check_scalar, 1);
4244 /* Resolve a dim argument to an intrinsic function. */
4246 bool
4247 gfc_resolve_dim_arg (gfc_expr *dim)
4249 if (dim == NULL)
4250 return true;
4252 if (!gfc_resolve_expr (dim))
4253 return false;
4255 if (dim->rank != 0)
4257 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4258 return false;
4262 if (dim->ts.type != BT_INTEGER)
4264 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4265 return false;
4268 if (dim->ts.kind != gfc_index_integer_kind)
4270 gfc_typespec ts;
4272 gfc_clear_ts (&ts);
4273 ts.type = BT_INTEGER;
4274 ts.kind = gfc_index_integer_kind;
4276 gfc_convert_type_warn (dim, &ts, 2, 0);
4279 return true;
4282 /* Given an expression that contains array references, update those array
4283 references to point to the right array specifications. While this is
4284 filled in during matching, this information is difficult to save and load
4285 in a module, so we take care of it here.
4287 The idea here is that the original array reference comes from the
4288 base symbol. We traverse the list of reference structures, setting
4289 the stored reference to references. Component references can
4290 provide an additional array specification. */
4292 static void
4293 find_array_spec (gfc_expr *e)
4295 gfc_array_spec *as;
4296 gfc_component *c;
4297 gfc_ref *ref;
4299 if (e->symtree->n.sym->ts.type == BT_CLASS)
4300 as = CLASS_DATA (e->symtree->n.sym)->as;
4301 else
4302 as = e->symtree->n.sym->as;
4304 for (ref = e->ref; ref; ref = ref->next)
4305 switch (ref->type)
4307 case REF_ARRAY:
4308 if (as == NULL)
4309 gfc_internal_error ("find_array_spec(): Missing spec");
4311 ref->u.ar.as = as;
4312 as = NULL;
4313 break;
4315 case REF_COMPONENT:
4316 c = ref->u.c.component;
4317 if (c->attr.dimension)
4319 if (as != NULL)
4320 gfc_internal_error ("find_array_spec(): unused as(1)");
4321 as = c->as;
4324 break;
4326 case REF_SUBSTRING:
4327 break;
4330 if (as != NULL)
4331 gfc_internal_error ("find_array_spec(): unused as(2)");
4335 /* Resolve an array reference. */
4337 static bool
4338 resolve_array_ref (gfc_array_ref *ar)
4340 int i, check_scalar;
4341 gfc_expr *e;
4343 for (i = 0; i < ar->dimen + ar->codimen; i++)
4345 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4347 /* Do not force gfc_index_integer_kind for the start. We can
4348 do fine with any integer kind. This avoids temporary arrays
4349 created for indexing with a vector. */
4350 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4351 return false;
4352 if (!gfc_resolve_index (ar->end[i], check_scalar))
4353 return false;
4354 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4355 return false;
4357 e = ar->start[i];
4359 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4360 switch (e->rank)
4362 case 0:
4363 ar->dimen_type[i] = DIMEN_ELEMENT;
4364 break;
4366 case 1:
4367 ar->dimen_type[i] = DIMEN_VECTOR;
4368 if (e->expr_type == EXPR_VARIABLE
4369 && e->symtree->n.sym->ts.type == BT_DERIVED)
4370 ar->start[i] = gfc_get_parentheses (e);
4371 break;
4373 default:
4374 gfc_error ("Array index at %L is an array of rank %d",
4375 &ar->c_where[i], e->rank);
4376 return false;
4379 /* Fill in the upper bound, which may be lower than the
4380 specified one for something like a(2:10:5), which is
4381 identical to a(2:7:5). Only relevant for strides not equal
4382 to one. Don't try a division by zero. */
4383 if (ar->dimen_type[i] == DIMEN_RANGE
4384 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4385 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4386 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4388 mpz_t size, end;
4390 if (gfc_ref_dimen_size (ar, i, &size, &end))
4392 if (ar->end[i] == NULL)
4394 ar->end[i] =
4395 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4396 &ar->where);
4397 mpz_set (ar->end[i]->value.integer, end);
4399 else if (ar->end[i]->ts.type == BT_INTEGER
4400 && ar->end[i]->expr_type == EXPR_CONSTANT)
4402 mpz_set (ar->end[i]->value.integer, end);
4404 else
4405 gcc_unreachable ();
4407 mpz_clear (size);
4408 mpz_clear (end);
4413 if (ar->type == AR_FULL)
4415 if (ar->as->rank == 0)
4416 ar->type = AR_ELEMENT;
4418 /* Make sure array is the same as array(:,:), this way
4419 we don't need to special case all the time. */
4420 ar->dimen = ar->as->rank;
4421 for (i = 0; i < ar->dimen; i++)
4423 ar->dimen_type[i] = DIMEN_RANGE;
4425 gcc_assert (ar->start[i] == NULL);
4426 gcc_assert (ar->end[i] == NULL);
4427 gcc_assert (ar->stride[i] == NULL);
4431 /* If the reference type is unknown, figure out what kind it is. */
4433 if (ar->type == AR_UNKNOWN)
4435 ar->type = AR_ELEMENT;
4436 for (i = 0; i < ar->dimen; i++)
4437 if (ar->dimen_type[i] == DIMEN_RANGE
4438 || ar->dimen_type[i] == DIMEN_VECTOR)
4440 ar->type = AR_SECTION;
4441 break;
4445 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4446 return false;
4448 if (ar->as->corank && ar->codimen == 0)
4450 int n;
4451 ar->codimen = ar->as->corank;
4452 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4453 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4456 return true;
4460 static bool
4461 resolve_substring (gfc_ref *ref)
4463 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4465 if (ref->u.ss.start != NULL)
4467 if (!gfc_resolve_expr (ref->u.ss.start))
4468 return false;
4470 if (ref->u.ss.start->ts.type != BT_INTEGER)
4472 gfc_error ("Substring start index at %L must be of type INTEGER",
4473 &ref->u.ss.start->where);
4474 return false;
4477 if (ref->u.ss.start->rank != 0)
4479 gfc_error ("Substring start index at %L must be scalar",
4480 &ref->u.ss.start->where);
4481 return false;
4484 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4485 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4486 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4488 gfc_error ("Substring start index at %L is less than one",
4489 &ref->u.ss.start->where);
4490 return false;
4494 if (ref->u.ss.end != NULL)
4496 if (!gfc_resolve_expr (ref->u.ss.end))
4497 return false;
4499 if (ref->u.ss.end->ts.type != BT_INTEGER)
4501 gfc_error ("Substring end index at %L must be of type INTEGER",
4502 &ref->u.ss.end->where);
4503 return false;
4506 if (ref->u.ss.end->rank != 0)
4508 gfc_error ("Substring end index at %L must be scalar",
4509 &ref->u.ss.end->where);
4510 return false;
4513 if (ref->u.ss.length != NULL
4514 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4515 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4516 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4518 gfc_error ("Substring end index at %L exceeds the string length",
4519 &ref->u.ss.start->where);
4520 return false;
4523 if (compare_bound_mpz_t (ref->u.ss.end,
4524 gfc_integer_kinds[k].huge) == CMP_GT
4525 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4526 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4528 gfc_error ("Substring end index at %L is too large",
4529 &ref->u.ss.end->where);
4530 return false;
4534 return true;
4538 /* This function supplies missing substring charlens. */
4540 void
4541 gfc_resolve_substring_charlen (gfc_expr *e)
4543 gfc_ref *char_ref;
4544 gfc_expr *start, *end;
4545 gfc_typespec *ts = NULL;
4547 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4549 if (char_ref->type == REF_SUBSTRING)
4550 break;
4551 if (char_ref->type == REF_COMPONENT)
4552 ts = &char_ref->u.c.component->ts;
4555 if (!char_ref)
4556 return;
4558 gcc_assert (char_ref->next == NULL);
4560 if (e->ts.u.cl)
4562 if (e->ts.u.cl->length)
4563 gfc_free_expr (e->ts.u.cl->length);
4564 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
4565 return;
4568 e->ts.type = BT_CHARACTER;
4569 e->ts.kind = gfc_default_character_kind;
4571 if (!e->ts.u.cl)
4572 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4574 if (char_ref->u.ss.start)
4575 start = gfc_copy_expr (char_ref->u.ss.start);
4576 else
4577 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4579 if (char_ref->u.ss.end)
4580 end = gfc_copy_expr (char_ref->u.ss.end);
4581 else if (e->expr_type == EXPR_VARIABLE)
4583 if (!ts)
4584 ts = &e->symtree->n.sym->ts;
4585 end = gfc_copy_expr (ts->u.cl->length);
4587 else
4588 end = NULL;
4590 if (!start || !end)
4592 gfc_free_expr (start);
4593 gfc_free_expr (end);
4594 return;
4597 /* Length = (end - start + 1). */
4598 e->ts.u.cl->length = gfc_subtract (end, start);
4599 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4600 gfc_get_int_expr (gfc_default_integer_kind,
4601 NULL, 1));
4603 /* F2008, 6.4.1: Both the starting point and the ending point shall
4604 be within the range 1, 2, ..., n unless the starting point exceeds
4605 the ending point, in which case the substring has length zero. */
4607 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
4608 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
4610 e->ts.u.cl->length->ts.type = BT_INTEGER;
4611 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4613 /* Make sure that the length is simplified. */
4614 gfc_simplify_expr (e->ts.u.cl->length, 1);
4615 gfc_resolve_expr (e->ts.u.cl->length);
4619 /* Resolve subtype references. */
4621 static bool
4622 resolve_ref (gfc_expr *expr)
4624 int current_part_dimension, n_components, seen_part_dimension;
4625 gfc_ref *ref;
4627 for (ref = expr->ref; ref; ref = ref->next)
4628 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4630 find_array_spec (expr);
4631 break;
4634 for (ref = expr->ref; ref; ref = ref->next)
4635 switch (ref->type)
4637 case REF_ARRAY:
4638 if (!resolve_array_ref (&ref->u.ar))
4639 return false;
4640 break;
4642 case REF_COMPONENT:
4643 break;
4645 case REF_SUBSTRING:
4646 if (!resolve_substring (ref))
4647 return false;
4648 break;
4651 /* Check constraints on part references. */
4653 current_part_dimension = 0;
4654 seen_part_dimension = 0;
4655 n_components = 0;
4657 for (ref = expr->ref; ref; ref = ref->next)
4659 switch (ref->type)
4661 case REF_ARRAY:
4662 switch (ref->u.ar.type)
4664 case AR_FULL:
4665 /* Coarray scalar. */
4666 if (ref->u.ar.as->rank == 0)
4668 current_part_dimension = 0;
4669 break;
4671 /* Fall through. */
4672 case AR_SECTION:
4673 current_part_dimension = 1;
4674 break;
4676 case AR_ELEMENT:
4677 current_part_dimension = 0;
4678 break;
4680 case AR_UNKNOWN:
4681 gfc_internal_error ("resolve_ref(): Bad array reference");
4684 break;
4686 case REF_COMPONENT:
4687 if (current_part_dimension || seen_part_dimension)
4689 /* F03:C614. */
4690 if (ref->u.c.component->attr.pointer
4691 || ref->u.c.component->attr.proc_pointer
4692 || (ref->u.c.component->ts.type == BT_CLASS
4693 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4695 gfc_error ("Component to the right of a part reference "
4696 "with nonzero rank must not have the POINTER "
4697 "attribute at %L", &expr->where);
4698 return false;
4700 else if (ref->u.c.component->attr.allocatable
4701 || (ref->u.c.component->ts.type == BT_CLASS
4702 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4705 gfc_error ("Component to the right of a part reference "
4706 "with nonzero rank must not have the ALLOCATABLE "
4707 "attribute at %L", &expr->where);
4708 return false;
4712 n_components++;
4713 break;
4715 case REF_SUBSTRING:
4716 break;
4719 if (((ref->type == REF_COMPONENT && n_components > 1)
4720 || ref->next == NULL)
4721 && current_part_dimension
4722 && seen_part_dimension)
4724 gfc_error ("Two or more part references with nonzero rank must "
4725 "not be specified at %L", &expr->where);
4726 return false;
4729 if (ref->type == REF_COMPONENT)
4731 if (current_part_dimension)
4732 seen_part_dimension = 1;
4734 /* reset to make sure */
4735 current_part_dimension = 0;
4739 return true;
4743 /* Given an expression, determine its shape. This is easier than it sounds.
4744 Leaves the shape array NULL if it is not possible to determine the shape. */
4746 static void
4747 expression_shape (gfc_expr *e)
4749 mpz_t array[GFC_MAX_DIMENSIONS];
4750 int i;
4752 if (e->rank <= 0 || e->shape != NULL)
4753 return;
4755 for (i = 0; i < e->rank; i++)
4756 if (!gfc_array_dimen_size (e, i, &array[i]))
4757 goto fail;
4759 e->shape = gfc_get_shape (e->rank);
4761 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4763 return;
4765 fail:
4766 for (i--; i >= 0; i--)
4767 mpz_clear (array[i]);
4771 /* Given a variable expression node, compute the rank of the expression by
4772 examining the base symbol and any reference structures it may have. */
4774 static void
4775 expression_rank (gfc_expr *e)
4777 gfc_ref *ref;
4778 int i, rank;
4780 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4781 could lead to serious confusion... */
4782 gcc_assert (e->expr_type != EXPR_COMPCALL);
4784 if (e->ref == NULL)
4786 if (e->expr_type == EXPR_ARRAY)
4787 goto done;
4788 /* Constructors can have a rank different from one via RESHAPE(). */
4790 if (e->symtree == NULL)
4792 e->rank = 0;
4793 goto done;
4796 e->rank = (e->symtree->n.sym->as == NULL)
4797 ? 0 : e->symtree->n.sym->as->rank;
4798 goto done;
4801 rank = 0;
4803 for (ref = e->ref; ref; ref = ref->next)
4805 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4806 && ref->u.c.component->attr.function && !ref->next)
4807 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4809 if (ref->type != REF_ARRAY)
4810 continue;
4812 if (ref->u.ar.type == AR_FULL)
4814 rank = ref->u.ar.as->rank;
4815 break;
4818 if (ref->u.ar.type == AR_SECTION)
4820 /* Figure out the rank of the section. */
4821 if (rank != 0)
4822 gfc_internal_error ("expression_rank(): Two array specs");
4824 for (i = 0; i < ref->u.ar.dimen; i++)
4825 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4826 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4827 rank++;
4829 break;
4833 e->rank = rank;
4835 done:
4836 expression_shape (e);
4840 static void
4841 add_caf_get_intrinsic (gfc_expr *e)
4843 gfc_expr *wrapper, *tmp_expr;
4844 gfc_ref *ref;
4845 int n;
4847 for (ref = e->ref; ref; ref = ref->next)
4848 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4849 break;
4850 if (ref == NULL)
4851 return;
4853 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
4854 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
4855 return;
4857 tmp_expr = XCNEW (gfc_expr);
4858 *tmp_expr = *e;
4859 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
4860 "caf_get", tmp_expr->where, 1, tmp_expr);
4861 wrapper->ts = e->ts;
4862 wrapper->rank = e->rank;
4863 if (e->rank)
4864 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
4865 *e = *wrapper;
4866 free (wrapper);
4870 static void
4871 remove_caf_get_intrinsic (gfc_expr *e)
4873 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
4874 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
4875 gfc_expr *e2 = e->value.function.actual->expr;
4876 e->value.function.actual->expr = NULL;
4877 gfc_free_actual_arglist (e->value.function.actual);
4878 gfc_free_shape (&e->shape, e->rank);
4879 *e = *e2;
4880 free (e2);
4884 /* Resolve a variable expression. */
4886 static bool
4887 resolve_variable (gfc_expr *e)
4889 gfc_symbol *sym;
4890 bool t;
4892 t = true;
4894 if (e->symtree == NULL)
4895 return false;
4896 sym = e->symtree->n.sym;
4898 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4899 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4900 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4902 if (!actual_arg || inquiry_argument)
4904 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4905 "be used as actual argument", sym->name, &e->where);
4906 return false;
4909 /* TS 29113, 407b. */
4910 else if (e->ts.type == BT_ASSUMED)
4912 if (!actual_arg)
4914 gfc_error ("Assumed-type variable %s at %L may only be used "
4915 "as actual argument", sym->name, &e->where);
4916 return false;
4918 else if (inquiry_argument && !first_actual_arg)
4920 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4921 for all inquiry functions in resolve_function; the reason is
4922 that the function-name resolution happens too late in that
4923 function. */
4924 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4925 "an inquiry function shall be the first argument",
4926 sym->name, &e->where);
4927 return false;
4930 /* TS 29113, C535b. */
4931 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4932 && CLASS_DATA (sym)->as
4933 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4934 || (sym->ts.type != BT_CLASS && sym->as
4935 && sym->as->type == AS_ASSUMED_RANK))
4937 if (!actual_arg)
4939 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4940 "actual argument", sym->name, &e->where);
4941 return false;
4943 else if (inquiry_argument && !first_actual_arg)
4945 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4946 for all inquiry functions in resolve_function; the reason is
4947 that the function-name resolution happens too late in that
4948 function. */
4949 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4950 "to an inquiry function shall be the first argument",
4951 sym->name, &e->where);
4952 return false;
4956 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
4957 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4958 && e->ref->next == NULL))
4960 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4961 "a subobject reference", sym->name, &e->ref->u.ar.where);
4962 return false;
4964 /* TS 29113, 407b. */
4965 else if (e->ts.type == BT_ASSUMED && e->ref
4966 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4967 && e->ref->next == NULL))
4969 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4970 "reference", sym->name, &e->ref->u.ar.where);
4971 return false;
4974 /* TS 29113, C535b. */
4975 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4976 && CLASS_DATA (sym)->as
4977 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4978 || (sym->ts.type != BT_CLASS && sym->as
4979 && sym->as->type == AS_ASSUMED_RANK))
4980 && e->ref
4981 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4982 && e->ref->next == NULL))
4984 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
4985 "reference", sym->name, &e->ref->u.ar.where);
4986 return false;
4989 /* For variables that are used in an associate (target => object) where
4990 the object's basetype is array valued while the target is scalar,
4991 the ts' type of the component refs is still array valued, which
4992 can't be translated that way. */
4993 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
4994 && sym->assoc->target->ts.type == BT_CLASS
4995 && CLASS_DATA (sym->assoc->target)->as)
4997 gfc_ref *ref = e->ref;
4998 while (ref)
5000 switch (ref->type)
5002 case REF_COMPONENT:
5003 ref->u.c.sym = sym->ts.u.derived;
5004 /* Stop the loop. */
5005 ref = NULL;
5006 break;
5007 default:
5008 ref = ref->next;
5009 break;
5014 /* If this is an associate-name, it may be parsed with an array reference
5015 in error even though the target is scalar. Fail directly in this case.
5016 TODO Understand why class scalar expressions must be excluded. */
5017 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5019 if (sym->ts.type == BT_CLASS)
5020 gfc_fix_class_refs (e);
5021 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5022 return false;
5025 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5026 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5028 /* On the other hand, the parser may not have known this is an array;
5029 in this case, we have to add a FULL reference. */
5030 if (sym->assoc && sym->attr.dimension && !e->ref)
5032 e->ref = gfc_get_ref ();
5033 e->ref->type = REF_ARRAY;
5034 e->ref->u.ar.type = AR_FULL;
5035 e->ref->u.ar.dimen = 0;
5038 /* Like above, but for class types, where the checking whether an array
5039 ref is present is more complicated. Furthermore make sure not to add
5040 the full array ref to _vptr or _len refs. */
5041 if (sym->assoc && sym->ts.type == BT_CLASS
5042 && CLASS_DATA (sym)->attr.dimension
5043 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
5045 gfc_ref *ref, *newref;
5047 newref = gfc_get_ref ();
5048 newref->type = REF_ARRAY;
5049 newref->u.ar.type = AR_FULL;
5050 newref->u.ar.dimen = 0;
5051 /* Because this is an associate var and the first ref either is a ref to
5052 the _data component or not, no traversal of the ref chain is
5053 needed. The array ref needs to be inserted after the _data ref,
5054 or when that is not present, which may happend for polymorphic
5055 types, then at the first position. */
5056 ref = e->ref;
5057 if (!ref)
5058 e->ref = newref;
5059 else if (ref->type == REF_COMPONENT
5060 && strcmp ("_data", ref->u.c.component->name) == 0)
5062 if (!ref->next || ref->next->type != REF_ARRAY)
5064 newref->next = ref->next;
5065 ref->next = newref;
5067 else
5068 /* Array ref present already. */
5069 gfc_free_ref_list (newref);
5071 else if (ref->type == REF_ARRAY)
5072 /* Array ref present already. */
5073 gfc_free_ref_list (newref);
5074 else
5076 newref->next = ref;
5077 e->ref = newref;
5081 if (e->ref && !resolve_ref (e))
5082 return false;
5084 if (sym->attr.flavor == FL_PROCEDURE
5085 && (!sym->attr.function
5086 || (sym->attr.function && sym->result
5087 && sym->result->attr.proc_pointer
5088 && !sym->result->attr.function)))
5090 e->ts.type = BT_PROCEDURE;
5091 goto resolve_procedure;
5094 if (sym->ts.type != BT_UNKNOWN)
5095 gfc_variable_attr (e, &e->ts);
5096 else
5098 /* Must be a simple variable reference. */
5099 if (!gfc_set_default_type (sym, 1, sym->ns))
5100 return false;
5101 e->ts = sym->ts;
5104 if (check_assumed_size_reference (sym, e))
5105 return false;
5107 /* Deal with forward references to entries during gfc_resolve_code, to
5108 satisfy, at least partially, 12.5.2.5. */
5109 if (gfc_current_ns->entries
5110 && current_entry_id == sym->entry_id
5111 && cs_base
5112 && cs_base->current
5113 && cs_base->current->op != EXEC_ENTRY)
5115 gfc_entry_list *entry;
5116 gfc_formal_arglist *formal;
5117 int n;
5118 bool seen, saved_specification_expr;
5120 /* If the symbol is a dummy... */
5121 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5123 entry = gfc_current_ns->entries;
5124 seen = false;
5126 /* ...test if the symbol is a parameter of previous entries. */
5127 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5128 for (formal = entry->sym->formal; formal; formal = formal->next)
5130 if (formal->sym && sym->name == formal->sym->name)
5132 seen = true;
5133 break;
5137 /* If it has not been seen as a dummy, this is an error. */
5138 if (!seen)
5140 if (specification_expr)
5141 gfc_error ("Variable %qs, used in a specification expression"
5142 ", is referenced at %L before the ENTRY statement "
5143 "in which it is a parameter",
5144 sym->name, &cs_base->current->loc);
5145 else
5146 gfc_error ("Variable %qs is used at %L before the ENTRY "
5147 "statement in which it is a parameter",
5148 sym->name, &cs_base->current->loc);
5149 t = false;
5153 /* Now do the same check on the specification expressions. */
5154 saved_specification_expr = specification_expr;
5155 specification_expr = true;
5156 if (sym->ts.type == BT_CHARACTER
5157 && !gfc_resolve_expr (sym->ts.u.cl->length))
5158 t = false;
5160 if (sym->as)
5161 for (n = 0; n < sym->as->rank; n++)
5163 if (!gfc_resolve_expr (sym->as->lower[n]))
5164 t = false;
5165 if (!gfc_resolve_expr (sym->as->upper[n]))
5166 t = false;
5168 specification_expr = saved_specification_expr;
5170 if (t)
5171 /* Update the symbol's entry level. */
5172 sym->entry_id = current_entry_id + 1;
5175 /* If a symbol has been host_associated mark it. This is used latter,
5176 to identify if aliasing is possible via host association. */
5177 if (sym->attr.flavor == FL_VARIABLE
5178 && gfc_current_ns->parent
5179 && (gfc_current_ns->parent == sym->ns
5180 || (gfc_current_ns->parent->parent
5181 && gfc_current_ns->parent->parent == sym->ns)))
5182 sym->attr.host_assoc = 1;
5184 if (gfc_current_ns->proc_name
5185 && sym->attr.dimension
5186 && (sym->ns != gfc_current_ns
5187 || sym->attr.use_assoc
5188 || sym->attr.in_common))
5189 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
5191 resolve_procedure:
5192 if (t && !resolve_procedure_expression (e))
5193 t = false;
5195 /* F2008, C617 and C1229. */
5196 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5197 && gfc_is_coindexed (e))
5199 gfc_ref *ref, *ref2 = NULL;
5201 for (ref = e->ref; ref; ref = ref->next)
5203 if (ref->type == REF_COMPONENT)
5204 ref2 = ref;
5205 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5206 break;
5209 for ( ; ref; ref = ref->next)
5210 if (ref->type == REF_COMPONENT)
5211 break;
5213 /* Expression itself is not coindexed object. */
5214 if (ref && e->ts.type == BT_CLASS)
5216 gfc_error ("Polymorphic subobject of coindexed object at %L",
5217 &e->where);
5218 t = false;
5221 /* Expression itself is coindexed object. */
5222 if (ref == NULL)
5224 gfc_component *c;
5225 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5226 for ( ; c; c = c->next)
5227 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5229 gfc_error ("Coindexed object with polymorphic allocatable "
5230 "subcomponent at %L", &e->where);
5231 t = false;
5232 break;
5237 if (t)
5238 expression_rank (e);
5240 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
5241 add_caf_get_intrinsic (e);
5243 return t;
5247 /* Checks to see that the correct symbol has been host associated.
5248 The only situation where this arises is that in which a twice
5249 contained function is parsed after the host association is made.
5250 Therefore, on detecting this, change the symbol in the expression
5251 and convert the array reference into an actual arglist if the old
5252 symbol is a variable. */
5253 static bool
5254 check_host_association (gfc_expr *e)
5256 gfc_symbol *sym, *old_sym;
5257 gfc_symtree *st;
5258 int n;
5259 gfc_ref *ref;
5260 gfc_actual_arglist *arg, *tail = NULL;
5261 bool retval = e->expr_type == EXPR_FUNCTION;
5263 /* If the expression is the result of substitution in
5264 interface.c(gfc_extend_expr) because there is no way in
5265 which the host association can be wrong. */
5266 if (e->symtree == NULL
5267 || e->symtree->n.sym == NULL
5268 || e->user_operator)
5269 return retval;
5271 old_sym = e->symtree->n.sym;
5273 if (gfc_current_ns->parent
5274 && old_sym->ns != gfc_current_ns)
5276 /* Use the 'USE' name so that renamed module symbols are
5277 correctly handled. */
5278 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5280 if (sym && old_sym != sym
5281 && sym->ts.type == old_sym->ts.type
5282 && sym->attr.flavor == FL_PROCEDURE
5283 && sym->attr.contained)
5285 /* Clear the shape, since it might not be valid. */
5286 gfc_free_shape (&e->shape, e->rank);
5288 /* Give the expression the right symtree! */
5289 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5290 gcc_assert (st != NULL);
5292 if (old_sym->attr.flavor == FL_PROCEDURE
5293 || e->expr_type == EXPR_FUNCTION)
5295 /* Original was function so point to the new symbol, since
5296 the actual argument list is already attached to the
5297 expression. */
5298 e->value.function.esym = NULL;
5299 e->symtree = st;
5301 else
5303 /* Original was variable so convert array references into
5304 an actual arglist. This does not need any checking now
5305 since resolve_function will take care of it. */
5306 e->value.function.actual = NULL;
5307 e->expr_type = EXPR_FUNCTION;
5308 e->symtree = st;
5310 /* Ambiguity will not arise if the array reference is not
5311 the last reference. */
5312 for (ref = e->ref; ref; ref = ref->next)
5313 if (ref->type == REF_ARRAY && ref->next == NULL)
5314 break;
5316 gcc_assert (ref->type == REF_ARRAY);
5318 /* Grab the start expressions from the array ref and
5319 copy them into actual arguments. */
5320 for (n = 0; n < ref->u.ar.dimen; n++)
5322 arg = gfc_get_actual_arglist ();
5323 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5324 if (e->value.function.actual == NULL)
5325 tail = e->value.function.actual = arg;
5326 else
5328 tail->next = arg;
5329 tail = arg;
5333 /* Dump the reference list and set the rank. */
5334 gfc_free_ref_list (e->ref);
5335 e->ref = NULL;
5336 e->rank = sym->as ? sym->as->rank : 0;
5339 gfc_resolve_expr (e);
5340 sym->refs++;
5343 /* This might have changed! */
5344 return e->expr_type == EXPR_FUNCTION;
5348 static void
5349 gfc_resolve_character_operator (gfc_expr *e)
5351 gfc_expr *op1 = e->value.op.op1;
5352 gfc_expr *op2 = e->value.op.op2;
5353 gfc_expr *e1 = NULL;
5354 gfc_expr *e2 = NULL;
5356 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5358 if (op1->ts.u.cl && op1->ts.u.cl->length)
5359 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5360 else if (op1->expr_type == EXPR_CONSTANT)
5361 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5362 op1->value.character.length);
5364 if (op2->ts.u.cl && op2->ts.u.cl->length)
5365 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5366 else if (op2->expr_type == EXPR_CONSTANT)
5367 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5368 op2->value.character.length);
5370 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5372 if (!e1 || !e2)
5374 gfc_free_expr (e1);
5375 gfc_free_expr (e2);
5377 return;
5380 e->ts.u.cl->length = gfc_add (e1, e2);
5381 e->ts.u.cl->length->ts.type = BT_INTEGER;
5382 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5383 gfc_simplify_expr (e->ts.u.cl->length, 0);
5384 gfc_resolve_expr (e->ts.u.cl->length);
5386 return;
5390 /* Ensure that an character expression has a charlen and, if possible, a
5391 length expression. */
5393 static void
5394 fixup_charlen (gfc_expr *e)
5396 /* The cases fall through so that changes in expression type and the need
5397 for multiple fixes are picked up. In all circumstances, a charlen should
5398 be available for the middle end to hang a backend_decl on. */
5399 switch (e->expr_type)
5401 case EXPR_OP:
5402 gfc_resolve_character_operator (e);
5404 case EXPR_ARRAY:
5405 if (e->expr_type == EXPR_ARRAY)
5406 gfc_resolve_character_array_constructor (e);
5408 case EXPR_SUBSTRING:
5409 if (!e->ts.u.cl && e->ref)
5410 gfc_resolve_substring_charlen (e);
5412 default:
5413 if (!e->ts.u.cl)
5414 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5416 break;
5421 /* Update an actual argument to include the passed-object for type-bound
5422 procedures at the right position. */
5424 static gfc_actual_arglist*
5425 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5426 const char *name)
5428 gcc_assert (argpos > 0);
5430 if (argpos == 1)
5432 gfc_actual_arglist* result;
5434 result = gfc_get_actual_arglist ();
5435 result->expr = po;
5436 result->next = lst;
5437 if (name)
5438 result->name = name;
5440 return result;
5443 if (lst)
5444 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5445 else
5446 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5447 return lst;
5451 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5453 static gfc_expr*
5454 extract_compcall_passed_object (gfc_expr* e)
5456 gfc_expr* po;
5458 gcc_assert (e->expr_type == EXPR_COMPCALL);
5460 if (e->value.compcall.base_object)
5461 po = gfc_copy_expr (e->value.compcall.base_object);
5462 else
5464 po = gfc_get_expr ();
5465 po->expr_type = EXPR_VARIABLE;
5466 po->symtree = e->symtree;
5467 po->ref = gfc_copy_ref (e->ref);
5468 po->where = e->where;
5471 if (!gfc_resolve_expr (po))
5472 return NULL;
5474 return po;
5478 /* Update the arglist of an EXPR_COMPCALL expression to include the
5479 passed-object. */
5481 static bool
5482 update_compcall_arglist (gfc_expr* e)
5484 gfc_expr* po;
5485 gfc_typebound_proc* tbp;
5487 tbp = e->value.compcall.tbp;
5489 if (tbp->error)
5490 return false;
5492 po = extract_compcall_passed_object (e);
5493 if (!po)
5494 return false;
5496 if (tbp->nopass || e->value.compcall.ignore_pass)
5498 gfc_free_expr (po);
5499 return true;
5502 gcc_assert (tbp->pass_arg_num > 0);
5503 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5504 tbp->pass_arg_num,
5505 tbp->pass_arg);
5507 return true;
5511 /* Extract the passed object from a PPC call (a copy of it). */
5513 static gfc_expr*
5514 extract_ppc_passed_object (gfc_expr *e)
5516 gfc_expr *po;
5517 gfc_ref **ref;
5519 po = gfc_get_expr ();
5520 po->expr_type = EXPR_VARIABLE;
5521 po->symtree = e->symtree;
5522 po->ref = gfc_copy_ref (e->ref);
5523 po->where = e->where;
5525 /* Remove PPC reference. */
5526 ref = &po->ref;
5527 while ((*ref)->next)
5528 ref = &(*ref)->next;
5529 gfc_free_ref_list (*ref);
5530 *ref = NULL;
5532 if (!gfc_resolve_expr (po))
5533 return NULL;
5535 return po;
5539 /* Update the actual arglist of a procedure pointer component to include the
5540 passed-object. */
5542 static bool
5543 update_ppc_arglist (gfc_expr* e)
5545 gfc_expr* po;
5546 gfc_component *ppc;
5547 gfc_typebound_proc* tb;
5549 ppc = gfc_get_proc_ptr_comp (e);
5550 if (!ppc)
5551 return false;
5553 tb = ppc->tb;
5555 if (tb->error)
5556 return false;
5557 else if (tb->nopass)
5558 return true;
5560 po = extract_ppc_passed_object (e);
5561 if (!po)
5562 return false;
5564 /* F08:R739. */
5565 if (po->rank != 0)
5567 gfc_error ("Passed-object at %L must be scalar", &e->where);
5568 return false;
5571 /* F08:C611. */
5572 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5574 gfc_error ("Base object for procedure-pointer component call at %L is of"
5575 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
5576 return false;
5579 gcc_assert (tb->pass_arg_num > 0);
5580 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5581 tb->pass_arg_num,
5582 tb->pass_arg);
5584 return true;
5588 /* Check that the object a TBP is called on is valid, i.e. it must not be
5589 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5591 static bool
5592 check_typebound_baseobject (gfc_expr* e)
5594 gfc_expr* base;
5595 bool return_value = false;
5597 base = extract_compcall_passed_object (e);
5598 if (!base)
5599 return false;
5601 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5603 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
5604 return false;
5606 /* F08:C611. */
5607 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5609 gfc_error ("Base object for type-bound procedure call at %L is of"
5610 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
5611 goto cleanup;
5614 /* F08:C1230. If the procedure called is NOPASS,
5615 the base object must be scalar. */
5616 if (e->value.compcall.tbp->nopass && base->rank != 0)
5618 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5619 " be scalar", &e->where);
5620 goto cleanup;
5623 return_value = true;
5625 cleanup:
5626 gfc_free_expr (base);
5627 return return_value;
5631 /* Resolve a call to a type-bound procedure, either function or subroutine,
5632 statically from the data in an EXPR_COMPCALL expression. The adapted
5633 arglist and the target-procedure symtree are returned. */
5635 static bool
5636 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5637 gfc_actual_arglist** actual)
5639 gcc_assert (e->expr_type == EXPR_COMPCALL);
5640 gcc_assert (!e->value.compcall.tbp->is_generic);
5642 /* Update the actual arglist for PASS. */
5643 if (!update_compcall_arglist (e))
5644 return false;
5646 *actual = e->value.compcall.actual;
5647 *target = e->value.compcall.tbp->u.specific;
5649 gfc_free_ref_list (e->ref);
5650 e->ref = NULL;
5651 e->value.compcall.actual = NULL;
5653 /* If we find a deferred typebound procedure, check for derived types
5654 that an overriding typebound procedure has not been missed. */
5655 if (e->value.compcall.name
5656 && !e->value.compcall.tbp->non_overridable
5657 && e->value.compcall.base_object
5658 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5660 gfc_symtree *st;
5661 gfc_symbol *derived;
5663 /* Use the derived type of the base_object. */
5664 derived = e->value.compcall.base_object->ts.u.derived;
5665 st = NULL;
5667 /* If necessary, go through the inheritance chain. */
5668 while (!st && derived)
5670 /* Look for the typebound procedure 'name'. */
5671 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5672 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5673 e->value.compcall.name);
5674 if (!st)
5675 derived = gfc_get_derived_super_type (derived);
5678 /* Now find the specific name in the derived type namespace. */
5679 if (st && st->n.tb && st->n.tb->u.specific)
5680 gfc_find_sym_tree (st->n.tb->u.specific->name,
5681 derived->ns, 1, &st);
5682 if (st)
5683 *target = st;
5685 return true;
5689 /* Get the ultimate declared type from an expression. In addition,
5690 return the last class/derived type reference and the copy of the
5691 reference list. If check_types is set true, derived types are
5692 identified as well as class references. */
5693 static gfc_symbol*
5694 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5695 gfc_expr *e, bool check_types)
5697 gfc_symbol *declared;
5698 gfc_ref *ref;
5700 declared = NULL;
5701 if (class_ref)
5702 *class_ref = NULL;
5703 if (new_ref)
5704 *new_ref = gfc_copy_ref (e->ref);
5706 for (ref = e->ref; ref; ref = ref->next)
5708 if (ref->type != REF_COMPONENT)
5709 continue;
5711 if ((ref->u.c.component->ts.type == BT_CLASS
5712 || (check_types && ref->u.c.component->ts.type == BT_DERIVED))
5713 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5715 declared = ref->u.c.component->ts.u.derived;
5716 if (class_ref)
5717 *class_ref = ref;
5721 if (declared == NULL)
5722 declared = e->symtree->n.sym->ts.u.derived;
5724 return declared;
5728 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5729 which of the specific bindings (if any) matches the arglist and transform
5730 the expression into a call of that binding. */
5732 static bool
5733 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5735 gfc_typebound_proc* genproc;
5736 const char* genname;
5737 gfc_symtree *st;
5738 gfc_symbol *derived;
5740 gcc_assert (e->expr_type == EXPR_COMPCALL);
5741 genname = e->value.compcall.name;
5742 genproc = e->value.compcall.tbp;
5744 if (!genproc->is_generic)
5745 return true;
5747 /* Try the bindings on this type and in the inheritance hierarchy. */
5748 for (; genproc; genproc = genproc->overridden)
5750 gfc_tbp_generic* g;
5752 gcc_assert (genproc->is_generic);
5753 for (g = genproc->u.generic; g; g = g->next)
5755 gfc_symbol* target;
5756 gfc_actual_arglist* args;
5757 bool matches;
5759 gcc_assert (g->specific);
5761 if (g->specific->error)
5762 continue;
5764 target = g->specific->u.specific->n.sym;
5766 /* Get the right arglist by handling PASS/NOPASS. */
5767 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5768 if (!g->specific->nopass)
5770 gfc_expr* po;
5771 po = extract_compcall_passed_object (e);
5772 if (!po)
5774 gfc_free_actual_arglist (args);
5775 return false;
5778 gcc_assert (g->specific->pass_arg_num > 0);
5779 gcc_assert (!g->specific->error);
5780 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5781 g->specific->pass_arg);
5783 resolve_actual_arglist (args, target->attr.proc,
5784 is_external_proc (target)
5785 && gfc_sym_get_dummy_args (target) == NULL);
5787 /* Check if this arglist matches the formal. */
5788 matches = gfc_arglist_matches_symbol (&args, target);
5790 /* Clean up and break out of the loop if we've found it. */
5791 gfc_free_actual_arglist (args);
5792 if (matches)
5794 e->value.compcall.tbp = g->specific;
5795 genname = g->specific_st->name;
5796 /* Pass along the name for CLASS methods, where the vtab
5797 procedure pointer component has to be referenced. */
5798 if (name)
5799 *name = genname;
5800 goto success;
5805 /* Nothing matching found! */
5806 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5807 " %qs at %L", genname, &e->where);
5808 return false;
5810 success:
5811 /* Make sure that we have the right specific instance for the name. */
5812 derived = get_declared_from_expr (NULL, NULL, e, true);
5814 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5815 if (st)
5816 e->value.compcall.tbp = st->n.tb;
5818 return true;
5822 /* Resolve a call to a type-bound subroutine. */
5824 static bool
5825 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
5827 gfc_actual_arglist* newactual;
5828 gfc_symtree* target;
5830 /* Check that's really a SUBROUTINE. */
5831 if (!c->expr1->value.compcall.tbp->subroutine)
5833 gfc_error ("%qs at %L should be a SUBROUTINE",
5834 c->expr1->value.compcall.name, &c->loc);
5835 return false;
5838 if (!check_typebound_baseobject (c->expr1))
5839 return false;
5841 /* Pass along the name for CLASS methods, where the vtab
5842 procedure pointer component has to be referenced. */
5843 if (name)
5844 *name = c->expr1->value.compcall.name;
5846 if (!resolve_typebound_generic_call (c->expr1, name))
5847 return false;
5849 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
5850 if (overridable)
5851 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
5853 /* Transform into an ordinary EXEC_CALL for now. */
5855 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5856 return false;
5858 c->ext.actual = newactual;
5859 c->symtree = target;
5860 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5862 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5864 gfc_free_expr (c->expr1);
5865 c->expr1 = gfc_get_expr ();
5866 c->expr1->expr_type = EXPR_FUNCTION;
5867 c->expr1->symtree = target;
5868 c->expr1->where = c->loc;
5870 return resolve_call (c);
5874 /* Resolve a component-call expression. */
5875 static bool
5876 resolve_compcall (gfc_expr* e, const char **name)
5878 gfc_actual_arglist* newactual;
5879 gfc_symtree* target;
5881 /* Check that's really a FUNCTION. */
5882 if (!e->value.compcall.tbp->function)
5884 gfc_error ("%qs at %L should be a FUNCTION",
5885 e->value.compcall.name, &e->where);
5886 return false;
5889 /* These must not be assign-calls! */
5890 gcc_assert (!e->value.compcall.assign);
5892 if (!check_typebound_baseobject (e))
5893 return false;
5895 /* Pass along the name for CLASS methods, where the vtab
5896 procedure pointer component has to be referenced. */
5897 if (name)
5898 *name = e->value.compcall.name;
5900 if (!resolve_typebound_generic_call (e, name))
5901 return false;
5902 gcc_assert (!e->value.compcall.tbp->is_generic);
5904 /* Take the rank from the function's symbol. */
5905 if (e->value.compcall.tbp->u.specific->n.sym->as)
5906 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5908 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5909 arglist to the TBP's binding target. */
5911 if (!resolve_typebound_static (e, &target, &newactual))
5912 return false;
5914 e->value.function.actual = newactual;
5915 e->value.function.name = NULL;
5916 e->value.function.esym = target->n.sym;
5917 e->value.function.isym = NULL;
5918 e->symtree = target;
5919 e->ts = target->n.sym->ts;
5920 e->expr_type = EXPR_FUNCTION;
5922 /* Resolution is not necessary if this is a class subroutine; this
5923 function only has to identify the specific proc. Resolution of
5924 the call will be done next in resolve_typebound_call. */
5925 return gfc_resolve_expr (e);
5929 static bool resolve_fl_derived (gfc_symbol *sym);
5932 /* Resolve a typebound function, or 'method'. First separate all
5933 the non-CLASS references by calling resolve_compcall directly. */
5935 static bool
5936 resolve_typebound_function (gfc_expr* e)
5938 gfc_symbol *declared;
5939 gfc_component *c;
5940 gfc_ref *new_ref;
5941 gfc_ref *class_ref;
5942 gfc_symtree *st;
5943 const char *name;
5944 gfc_typespec ts;
5945 gfc_expr *expr;
5946 bool overridable;
5948 st = e->symtree;
5950 /* Deal with typebound operators for CLASS objects. */
5951 expr = e->value.compcall.base_object;
5952 overridable = !e->value.compcall.tbp->non_overridable;
5953 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5955 /* If the base_object is not a variable, the corresponding actual
5956 argument expression must be stored in e->base_expression so
5957 that the corresponding tree temporary can be used as the base
5958 object in gfc_conv_procedure_call. */
5959 if (expr->expr_type != EXPR_VARIABLE)
5961 gfc_actual_arglist *args;
5963 for (args= e->value.function.actual; args; args = args->next)
5965 if (expr == args->expr)
5966 expr = args->expr;
5970 /* Since the typebound operators are generic, we have to ensure
5971 that any delays in resolution are corrected and that the vtab
5972 is present. */
5973 ts = expr->ts;
5974 declared = ts.u.derived;
5975 c = gfc_find_component (declared, "_vptr", true, true);
5976 if (c->ts.u.derived == NULL)
5977 c->ts.u.derived = gfc_find_derived_vtab (declared);
5979 if (!resolve_compcall (e, &name))
5980 return false;
5982 /* Use the generic name if it is there. */
5983 name = name ? name : e->value.function.esym->name;
5984 e->symtree = expr->symtree;
5985 e->ref = gfc_copy_ref (expr->ref);
5986 get_declared_from_expr (&class_ref, NULL, e, false);
5988 /* Trim away the extraneous references that emerge from nested
5989 use of interface.c (extend_expr). */
5990 if (class_ref && class_ref->next)
5992 gfc_free_ref_list (class_ref->next);
5993 class_ref->next = NULL;
5995 else if (e->ref && !class_ref)
5997 gfc_free_ref_list (e->ref);
5998 e->ref = NULL;
6001 gfc_add_vptr_component (e);
6002 gfc_add_component_ref (e, name);
6003 e->value.function.esym = NULL;
6004 if (expr->expr_type != EXPR_VARIABLE)
6005 e->base_expr = expr;
6006 return true;
6009 if (st == NULL)
6010 return resolve_compcall (e, NULL);
6012 if (!resolve_ref (e))
6013 return false;
6015 /* Get the CLASS declared type. */
6016 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6018 if (!resolve_fl_derived (declared))
6019 return false;
6021 /* Weed out cases of the ultimate component being a derived type. */
6022 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
6023 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6025 gfc_free_ref_list (new_ref);
6026 return resolve_compcall (e, NULL);
6029 c = gfc_find_component (declared, "_data", true, true);
6030 declared = c->ts.u.derived;
6032 /* Treat the call as if it is a typebound procedure, in order to roll
6033 out the correct name for the specific function. */
6034 if (!resolve_compcall (e, &name))
6036 gfc_free_ref_list (new_ref);
6037 return false;
6039 ts = e->ts;
6041 if (overridable)
6043 /* Convert the expression to a procedure pointer component call. */
6044 e->value.function.esym = NULL;
6045 e->symtree = st;
6047 if (new_ref)
6048 e->ref = new_ref;
6050 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6051 gfc_add_vptr_component (e);
6052 gfc_add_component_ref (e, name);
6054 /* Recover the typespec for the expression. This is really only
6055 necessary for generic procedures, where the additional call
6056 to gfc_add_component_ref seems to throw the collection of the
6057 correct typespec. */
6058 e->ts = ts;
6060 else if (new_ref)
6061 gfc_free_ref_list (new_ref);
6063 return true;
6066 /* Resolve a typebound subroutine, or 'method'. First separate all
6067 the non-CLASS references by calling resolve_typebound_call
6068 directly. */
6070 static bool
6071 resolve_typebound_subroutine (gfc_code *code)
6073 gfc_symbol *declared;
6074 gfc_component *c;
6075 gfc_ref *new_ref;
6076 gfc_ref *class_ref;
6077 gfc_symtree *st;
6078 const char *name;
6079 gfc_typespec ts;
6080 gfc_expr *expr;
6081 bool overridable;
6083 st = code->expr1->symtree;
6085 /* Deal with typebound operators for CLASS objects. */
6086 expr = code->expr1->value.compcall.base_object;
6087 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6088 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6090 /* If the base_object is not a variable, the corresponding actual
6091 argument expression must be stored in e->base_expression so
6092 that the corresponding tree temporary can be used as the base
6093 object in gfc_conv_procedure_call. */
6094 if (expr->expr_type != EXPR_VARIABLE)
6096 gfc_actual_arglist *args;
6098 args= code->expr1->value.function.actual;
6099 for (; args; args = args->next)
6100 if (expr == args->expr)
6101 expr = args->expr;
6104 /* Since the typebound operators are generic, we have to ensure
6105 that any delays in resolution are corrected and that the vtab
6106 is present. */
6107 declared = expr->ts.u.derived;
6108 c = gfc_find_component (declared, "_vptr", true, true);
6109 if (c->ts.u.derived == NULL)
6110 c->ts.u.derived = gfc_find_derived_vtab (declared);
6112 if (!resolve_typebound_call (code, &name, NULL))
6113 return false;
6115 /* Use the generic name if it is there. */
6116 name = name ? name : code->expr1->value.function.esym->name;
6117 code->expr1->symtree = expr->symtree;
6118 code->expr1->ref = gfc_copy_ref (expr->ref);
6120 /* Trim away the extraneous references that emerge from nested
6121 use of interface.c (extend_expr). */
6122 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6123 if (class_ref && class_ref->next)
6125 gfc_free_ref_list (class_ref->next);
6126 class_ref->next = NULL;
6128 else if (code->expr1->ref && !class_ref)
6130 gfc_free_ref_list (code->expr1->ref);
6131 code->expr1->ref = NULL;
6134 /* Now use the procedure in the vtable. */
6135 gfc_add_vptr_component (code->expr1);
6136 gfc_add_component_ref (code->expr1, name);
6137 code->expr1->value.function.esym = NULL;
6138 if (expr->expr_type != EXPR_VARIABLE)
6139 code->expr1->base_expr = expr;
6140 return true;
6143 if (st == NULL)
6144 return resolve_typebound_call (code, NULL, NULL);
6146 if (!resolve_ref (code->expr1))
6147 return false;
6149 /* Get the CLASS declared type. */
6150 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
6152 /* Weed out cases of the ultimate component being a derived type. */
6153 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
6154 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6156 gfc_free_ref_list (new_ref);
6157 return resolve_typebound_call (code, NULL, NULL);
6160 if (!resolve_typebound_call (code, &name, &overridable))
6162 gfc_free_ref_list (new_ref);
6163 return false;
6165 ts = code->expr1->ts;
6167 if (overridable)
6169 /* Convert the expression to a procedure pointer component call. */
6170 code->expr1->value.function.esym = NULL;
6171 code->expr1->symtree = st;
6173 if (new_ref)
6174 code->expr1->ref = new_ref;
6176 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6177 gfc_add_vptr_component (code->expr1);
6178 gfc_add_component_ref (code->expr1, name);
6180 /* Recover the typespec for the expression. This is really only
6181 necessary for generic procedures, where the additional call
6182 to gfc_add_component_ref seems to throw the collection of the
6183 correct typespec. */
6184 code->expr1->ts = ts;
6186 else if (new_ref)
6187 gfc_free_ref_list (new_ref);
6189 return true;
6193 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6195 static bool
6196 resolve_ppc_call (gfc_code* c)
6198 gfc_component *comp;
6200 comp = gfc_get_proc_ptr_comp (c->expr1);
6201 gcc_assert (comp != NULL);
6203 c->resolved_sym = c->expr1->symtree->n.sym;
6204 c->expr1->expr_type = EXPR_VARIABLE;
6206 if (!comp->attr.subroutine)
6207 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6209 if (!resolve_ref (c->expr1))
6210 return false;
6212 if (!update_ppc_arglist (c->expr1))
6213 return false;
6215 c->ext.actual = c->expr1->value.compcall.actual;
6217 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6218 !(comp->ts.interface
6219 && comp->ts.interface->formal)))
6220 return false;
6222 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
6223 return false;
6225 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6227 return true;
6231 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6233 static bool
6234 resolve_expr_ppc (gfc_expr* e)
6236 gfc_component *comp;
6238 comp = gfc_get_proc_ptr_comp (e);
6239 gcc_assert (comp != NULL);
6241 /* Convert to EXPR_FUNCTION. */
6242 e->expr_type = EXPR_FUNCTION;
6243 e->value.function.isym = NULL;
6244 e->value.function.actual = e->value.compcall.actual;
6245 e->ts = comp->ts;
6246 if (comp->as != NULL)
6247 e->rank = comp->as->rank;
6249 if (!comp->attr.function)
6250 gfc_add_function (&comp->attr, comp->name, &e->where);
6252 if (!resolve_ref (e))
6253 return false;
6255 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6256 !(comp->ts.interface
6257 && comp->ts.interface->formal)))
6258 return false;
6260 if (!update_ppc_arglist (e))
6261 return false;
6263 if (!check_pure_function(e))
6264 return false;
6266 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6268 return true;
6272 static bool
6273 gfc_is_expandable_expr (gfc_expr *e)
6275 gfc_constructor *con;
6277 if (e->expr_type == EXPR_ARRAY)
6279 /* Traverse the constructor looking for variables that are flavor
6280 parameter. Parameters must be expanded since they are fully used at
6281 compile time. */
6282 con = gfc_constructor_first (e->value.constructor);
6283 for (; con; con = gfc_constructor_next (con))
6285 if (con->expr->expr_type == EXPR_VARIABLE
6286 && con->expr->symtree
6287 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6288 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6289 return true;
6290 if (con->expr->expr_type == EXPR_ARRAY
6291 && gfc_is_expandable_expr (con->expr))
6292 return true;
6296 return false;
6299 /* Resolve an expression. That is, make sure that types of operands agree
6300 with their operators, intrinsic operators are converted to function calls
6301 for overloaded types and unresolved function references are resolved. */
6303 bool
6304 gfc_resolve_expr (gfc_expr *e)
6306 bool t;
6307 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6309 if (e == NULL)
6310 return true;
6312 /* inquiry_argument only applies to variables. */
6313 inquiry_save = inquiry_argument;
6314 actual_arg_save = actual_arg;
6315 first_actual_arg_save = first_actual_arg;
6317 if (e->expr_type != EXPR_VARIABLE)
6319 inquiry_argument = false;
6320 actual_arg = false;
6321 first_actual_arg = false;
6324 switch (e->expr_type)
6326 case EXPR_OP:
6327 t = resolve_operator (e);
6328 break;
6330 case EXPR_FUNCTION:
6331 case EXPR_VARIABLE:
6333 if (check_host_association (e))
6334 t = resolve_function (e);
6335 else
6336 t = resolve_variable (e);
6338 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6339 && e->ref->type != REF_SUBSTRING)
6340 gfc_resolve_substring_charlen (e);
6342 break;
6344 case EXPR_COMPCALL:
6345 t = resolve_typebound_function (e);
6346 break;
6348 case EXPR_SUBSTRING:
6349 t = resolve_ref (e);
6350 break;
6352 case EXPR_CONSTANT:
6353 case EXPR_NULL:
6354 t = true;
6355 break;
6357 case EXPR_PPC:
6358 t = resolve_expr_ppc (e);
6359 break;
6361 case EXPR_ARRAY:
6362 t = false;
6363 if (!resolve_ref (e))
6364 break;
6366 t = gfc_resolve_array_constructor (e);
6367 /* Also try to expand a constructor. */
6368 if (t)
6370 expression_rank (e);
6371 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6372 gfc_expand_constructor (e, false);
6375 /* This provides the opportunity for the length of constructors with
6376 character valued function elements to propagate the string length
6377 to the expression. */
6378 if (t && e->ts.type == BT_CHARACTER)
6380 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6381 here rather then add a duplicate test for it above. */
6382 gfc_expand_constructor (e, false);
6383 t = gfc_resolve_character_array_constructor (e);
6386 break;
6388 case EXPR_STRUCTURE:
6389 t = resolve_ref (e);
6390 if (!t)
6391 break;
6393 t = resolve_structure_cons (e, 0);
6394 if (!t)
6395 break;
6397 t = gfc_simplify_expr (e, 0);
6398 break;
6400 default:
6401 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6404 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
6405 fixup_charlen (e);
6407 inquiry_argument = inquiry_save;
6408 actual_arg = actual_arg_save;
6409 first_actual_arg = first_actual_arg_save;
6411 return t;
6415 /* Resolve an expression from an iterator. They must be scalar and have
6416 INTEGER or (optionally) REAL type. */
6418 static bool
6419 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6420 const char *name_msgid)
6422 if (!gfc_resolve_expr (expr))
6423 return false;
6425 if (expr->rank != 0)
6427 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6428 return false;
6431 if (expr->ts.type != BT_INTEGER)
6433 if (expr->ts.type == BT_REAL)
6435 if (real_ok)
6436 return gfc_notify_std (GFC_STD_F95_DEL,
6437 "%s at %L must be integer",
6438 _(name_msgid), &expr->where);
6439 else
6441 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6442 &expr->where);
6443 return false;
6446 else
6448 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6449 return false;
6452 return true;
6456 /* Resolve the expressions in an iterator structure. If REAL_OK is
6457 false allow only INTEGER type iterators, otherwise allow REAL types.
6458 Set own_scope to true for ac-implied-do and data-implied-do as those
6459 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
6461 bool
6462 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
6464 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6465 return false;
6467 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6468 _("iterator variable")))
6469 return false;
6471 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6472 "Start expression in DO loop"))
6473 return false;
6475 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6476 "End expression in DO loop"))
6477 return false;
6479 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6480 "Step expression in DO loop"))
6481 return false;
6483 if (iter->step->expr_type == EXPR_CONSTANT)
6485 if ((iter->step->ts.type == BT_INTEGER
6486 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6487 || (iter->step->ts.type == BT_REAL
6488 && mpfr_sgn (iter->step->value.real) == 0))
6490 gfc_error ("Step expression in DO loop at %L cannot be zero",
6491 &iter->step->where);
6492 return false;
6496 /* Convert start, end, and step to the same type as var. */
6497 if (iter->start->ts.kind != iter->var->ts.kind
6498 || iter->start->ts.type != iter->var->ts.type)
6499 gfc_convert_type (iter->start, &iter->var->ts, 2);
6501 if (iter->end->ts.kind != iter->var->ts.kind
6502 || iter->end->ts.type != iter->var->ts.type)
6503 gfc_convert_type (iter->end, &iter->var->ts, 2);
6505 if (iter->step->ts.kind != iter->var->ts.kind
6506 || iter->step->ts.type != iter->var->ts.type)
6507 gfc_convert_type (iter->step, &iter->var->ts, 2);
6509 if (iter->start->expr_type == EXPR_CONSTANT
6510 && iter->end->expr_type == EXPR_CONSTANT
6511 && iter->step->expr_type == EXPR_CONSTANT)
6513 int sgn, cmp;
6514 if (iter->start->ts.type == BT_INTEGER)
6516 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6517 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6519 else
6521 sgn = mpfr_sgn (iter->step->value.real);
6522 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6524 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
6525 gfc_warning (OPT_Wzerotrip,
6526 "DO loop at %L will be executed zero times",
6527 &iter->step->where);
6530 return true;
6534 /* Traversal function for find_forall_index. f == 2 signals that
6535 that variable itself is not to be checked - only the references. */
6537 static bool
6538 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6540 if (expr->expr_type != EXPR_VARIABLE)
6541 return false;
6543 /* A scalar assignment */
6544 if (!expr->ref || *f == 1)
6546 if (expr->symtree->n.sym == sym)
6547 return true;
6548 else
6549 return false;
6552 if (*f == 2)
6553 *f = 1;
6554 return false;
6558 /* Check whether the FORALL index appears in the expression or not.
6559 Returns true if SYM is found in EXPR. */
6561 bool
6562 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6564 if (gfc_traverse_expr (expr, sym, forall_index, f))
6565 return true;
6566 else
6567 return false;
6571 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6572 to be a scalar INTEGER variable. The subscripts and stride are scalar
6573 INTEGERs, and if stride is a constant it must be nonzero.
6574 Furthermore "A subscript or stride in a forall-triplet-spec shall
6575 not contain a reference to any index-name in the
6576 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6578 static void
6579 resolve_forall_iterators (gfc_forall_iterator *it)
6581 gfc_forall_iterator *iter, *iter2;
6583 for (iter = it; iter; iter = iter->next)
6585 if (gfc_resolve_expr (iter->var)
6586 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6587 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6588 &iter->var->where);
6590 if (gfc_resolve_expr (iter->start)
6591 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6592 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6593 &iter->start->where);
6594 if (iter->var->ts.kind != iter->start->ts.kind)
6595 gfc_convert_type (iter->start, &iter->var->ts, 1);
6597 if (gfc_resolve_expr (iter->end)
6598 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6599 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6600 &iter->end->where);
6601 if (iter->var->ts.kind != iter->end->ts.kind)
6602 gfc_convert_type (iter->end, &iter->var->ts, 1);
6604 if (gfc_resolve_expr (iter->stride))
6606 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6607 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6608 &iter->stride->where, "INTEGER");
6610 if (iter->stride->expr_type == EXPR_CONSTANT
6611 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
6612 gfc_error ("FORALL stride expression at %L cannot be zero",
6613 &iter->stride->where);
6615 if (iter->var->ts.kind != iter->stride->ts.kind)
6616 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6619 for (iter = it; iter; iter = iter->next)
6620 for (iter2 = iter; iter2; iter2 = iter2->next)
6622 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6623 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6624 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
6625 gfc_error ("FORALL index %qs may not appear in triplet "
6626 "specification at %L", iter->var->symtree->name,
6627 &iter2->start->where);
6632 /* Given a pointer to a symbol that is a derived type, see if it's
6633 inaccessible, i.e. if it's defined in another module and the components are
6634 PRIVATE. The search is recursive if necessary. Returns zero if no
6635 inaccessible components are found, nonzero otherwise. */
6637 static int
6638 derived_inaccessible (gfc_symbol *sym)
6640 gfc_component *c;
6642 if (sym->attr.use_assoc && sym->attr.private_comp)
6643 return 1;
6645 for (c = sym->components; c; c = c->next)
6647 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6648 return 1;
6651 return 0;
6655 /* Resolve the argument of a deallocate expression. The expression must be
6656 a pointer or a full array. */
6658 static bool
6659 resolve_deallocate_expr (gfc_expr *e)
6661 symbol_attribute attr;
6662 int allocatable, pointer;
6663 gfc_ref *ref;
6664 gfc_symbol *sym;
6665 gfc_component *c;
6666 bool unlimited;
6668 if (!gfc_resolve_expr (e))
6669 return false;
6671 if (e->expr_type != EXPR_VARIABLE)
6672 goto bad;
6674 sym = e->symtree->n.sym;
6675 unlimited = UNLIMITED_POLY(sym);
6677 if (sym->ts.type == BT_CLASS)
6679 allocatable = CLASS_DATA (sym)->attr.allocatable;
6680 pointer = CLASS_DATA (sym)->attr.class_pointer;
6682 else
6684 allocatable = sym->attr.allocatable;
6685 pointer = sym->attr.pointer;
6687 for (ref = e->ref; ref; ref = ref->next)
6689 switch (ref->type)
6691 case REF_ARRAY:
6692 if (ref->u.ar.type != AR_FULL
6693 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6694 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6695 allocatable = 0;
6696 break;
6698 case REF_COMPONENT:
6699 c = ref->u.c.component;
6700 if (c->ts.type == BT_CLASS)
6702 allocatable = CLASS_DATA (c)->attr.allocatable;
6703 pointer = CLASS_DATA (c)->attr.class_pointer;
6705 else
6707 allocatable = c->attr.allocatable;
6708 pointer = c->attr.pointer;
6710 break;
6712 case REF_SUBSTRING:
6713 allocatable = 0;
6714 break;
6718 attr = gfc_expr_attr (e);
6720 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
6722 bad:
6723 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6724 &e->where);
6725 return false;
6728 /* F2008, C644. */
6729 if (gfc_is_coindexed (e))
6731 gfc_error ("Coindexed allocatable object at %L", &e->where);
6732 return false;
6735 if (pointer
6736 && !gfc_check_vardef_context (e, true, true, false,
6737 _("DEALLOCATE object")))
6738 return false;
6739 if (!gfc_check_vardef_context (e, false, true, false,
6740 _("DEALLOCATE object")))
6741 return false;
6743 return true;
6747 /* Returns true if the expression e contains a reference to the symbol sym. */
6748 static bool
6749 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6751 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6752 return true;
6754 return false;
6757 bool
6758 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6760 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6764 /* Given the expression node e for an allocatable/pointer of derived type to be
6765 allocated, get the expression node to be initialized afterwards (needed for
6766 derived types with default initializers, and derived types with allocatable
6767 components that need nullification.) */
6769 gfc_expr *
6770 gfc_expr_to_initialize (gfc_expr *e)
6772 gfc_expr *result;
6773 gfc_ref *ref;
6774 int i;
6776 result = gfc_copy_expr (e);
6778 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6779 for (ref = result->ref; ref; ref = ref->next)
6780 if (ref->type == REF_ARRAY && ref->next == NULL)
6782 ref->u.ar.type = AR_FULL;
6784 for (i = 0; i < ref->u.ar.dimen; i++)
6785 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6787 break;
6790 gfc_free_shape (&result->shape, result->rank);
6792 /* Recalculate rank, shape, etc. */
6793 gfc_resolve_expr (result);
6794 return result;
6798 /* If the last ref of an expression is an array ref, return a copy of the
6799 expression with that one removed. Otherwise, a copy of the original
6800 expression. This is used for allocate-expressions and pointer assignment
6801 LHS, where there may be an array specification that needs to be stripped
6802 off when using gfc_check_vardef_context. */
6804 static gfc_expr*
6805 remove_last_array_ref (gfc_expr* e)
6807 gfc_expr* e2;
6808 gfc_ref** r;
6810 e2 = gfc_copy_expr (e);
6811 for (r = &e2->ref; *r; r = &(*r)->next)
6812 if ((*r)->type == REF_ARRAY && !(*r)->next)
6814 gfc_free_ref_list (*r);
6815 *r = NULL;
6816 break;
6819 return e2;
6823 /* Used in resolve_allocate_expr to check that a allocation-object and
6824 a source-expr are conformable. This does not catch all possible
6825 cases; in particular a runtime checking is needed. */
6827 static bool
6828 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6830 gfc_ref *tail;
6831 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6833 /* First compare rank. */
6834 if ((tail && e1->rank != tail->u.ar.as->rank)
6835 || (!tail && e1->rank != e2->rank))
6837 gfc_error ("Source-expr at %L must be scalar or have the "
6838 "same rank as the allocate-object at %L",
6839 &e1->where, &e2->where);
6840 return false;
6843 if (e1->shape)
6845 int i;
6846 mpz_t s;
6848 mpz_init (s);
6850 for (i = 0; i < e1->rank; i++)
6852 if (tail->u.ar.start[i] == NULL)
6853 break;
6855 if (tail->u.ar.end[i])
6857 mpz_set (s, tail->u.ar.end[i]->value.integer);
6858 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6859 mpz_add_ui (s, s, 1);
6861 else
6863 mpz_set (s, tail->u.ar.start[i]->value.integer);
6866 if (mpz_cmp (e1->shape[i], s) != 0)
6868 gfc_error ("Source-expr at %L and allocate-object at %L must "
6869 "have the same shape", &e1->where, &e2->where);
6870 mpz_clear (s);
6871 return false;
6875 mpz_clear (s);
6878 return true;
6882 /* Resolve the expression in an ALLOCATE statement, doing the additional
6883 checks to see whether the expression is OK or not. The expression must
6884 have a trailing array reference that gives the size of the array. */
6886 static bool
6887 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
6889 int i, pointer, allocatable, dimension, is_abstract;
6890 int codimension;
6891 bool coindexed;
6892 bool unlimited;
6893 symbol_attribute attr;
6894 gfc_ref *ref, *ref2;
6895 gfc_expr *e2;
6896 gfc_array_ref *ar;
6897 gfc_symbol *sym = NULL;
6898 gfc_alloc *a;
6899 gfc_component *c;
6900 bool t;
6902 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
6903 checking of coarrays. */
6904 for (ref = e->ref; ref; ref = ref->next)
6905 if (ref->next == NULL)
6906 break;
6908 if (ref && ref->type == REF_ARRAY)
6909 ref->u.ar.in_allocate = true;
6911 if (!gfc_resolve_expr (e))
6912 goto failure;
6914 /* Make sure the expression is allocatable or a pointer. If it is
6915 pointer, the next-to-last reference must be a pointer. */
6917 ref2 = NULL;
6918 if (e->symtree)
6919 sym = e->symtree->n.sym;
6921 /* Check whether ultimate component is abstract and CLASS. */
6922 is_abstract = 0;
6924 /* Is the allocate-object unlimited polymorphic? */
6925 unlimited = UNLIMITED_POLY(e);
6927 if (e->expr_type != EXPR_VARIABLE)
6929 allocatable = 0;
6930 attr = gfc_expr_attr (e);
6931 pointer = attr.pointer;
6932 dimension = attr.dimension;
6933 codimension = attr.codimension;
6935 else
6937 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
6939 allocatable = CLASS_DATA (sym)->attr.allocatable;
6940 pointer = CLASS_DATA (sym)->attr.class_pointer;
6941 dimension = CLASS_DATA (sym)->attr.dimension;
6942 codimension = CLASS_DATA (sym)->attr.codimension;
6943 is_abstract = CLASS_DATA (sym)->attr.abstract;
6945 else
6947 allocatable = sym->attr.allocatable;
6948 pointer = sym->attr.pointer;
6949 dimension = sym->attr.dimension;
6950 codimension = sym->attr.codimension;
6953 coindexed = false;
6955 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6957 switch (ref->type)
6959 case REF_ARRAY:
6960 if (ref->u.ar.codimen > 0)
6962 int n;
6963 for (n = ref->u.ar.dimen;
6964 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6965 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6967 coindexed = true;
6968 break;
6972 if (ref->next != NULL)
6973 pointer = 0;
6974 break;
6976 case REF_COMPONENT:
6977 /* F2008, C644. */
6978 if (coindexed)
6980 gfc_error ("Coindexed allocatable object at %L",
6981 &e->where);
6982 goto failure;
6985 c = ref->u.c.component;
6986 if (c->ts.type == BT_CLASS)
6988 allocatable = CLASS_DATA (c)->attr.allocatable;
6989 pointer = CLASS_DATA (c)->attr.class_pointer;
6990 dimension = CLASS_DATA (c)->attr.dimension;
6991 codimension = CLASS_DATA (c)->attr.codimension;
6992 is_abstract = CLASS_DATA (c)->attr.abstract;
6994 else
6996 allocatable = c->attr.allocatable;
6997 pointer = c->attr.pointer;
6998 dimension = c->attr.dimension;
6999 codimension = c->attr.codimension;
7000 is_abstract = c->attr.abstract;
7002 break;
7004 case REF_SUBSTRING:
7005 allocatable = 0;
7006 pointer = 0;
7007 break;
7012 /* Check for F08:C628. */
7013 if (allocatable == 0 && pointer == 0 && !unlimited)
7015 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7016 &e->where);
7017 goto failure;
7020 /* Some checks for the SOURCE tag. */
7021 if (code->expr3)
7023 /* Check F03:C631. */
7024 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7026 gfc_error ("Type of entity at %L is type incompatible with "
7027 "source-expr at %L", &e->where, &code->expr3->where);
7028 goto failure;
7031 /* Check F03:C632 and restriction following Note 6.18. */
7032 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
7033 goto failure;
7035 /* Check F03:C633. */
7036 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
7038 gfc_error ("The allocate-object at %L and the source-expr at %L "
7039 "shall have the same kind type parameter",
7040 &e->where, &code->expr3->where);
7041 goto failure;
7044 /* Check F2008, C642. */
7045 if (code->expr3->ts.type == BT_DERIVED
7046 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7047 || (code->expr3->ts.u.derived->from_intmod
7048 == INTMOD_ISO_FORTRAN_ENV
7049 && code->expr3->ts.u.derived->intmod_sym_id
7050 == ISOFORTRAN_LOCK_TYPE)))
7052 gfc_error ("The source-expr at %L shall neither be of type "
7053 "LOCK_TYPE nor have a LOCK_TYPE component if "
7054 "allocate-object at %L is a coarray",
7055 &code->expr3->where, &e->where);
7056 goto failure;
7060 /* Check F08:C629. */
7061 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7062 && !code->expr3)
7064 gcc_assert (e->ts.type == BT_CLASS);
7065 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7066 "type-spec or source-expr", sym->name, &e->where);
7067 goto failure;
7070 /* Check F08:C632. */
7071 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
7072 && !UNLIMITED_POLY (e))
7074 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7075 code->ext.alloc.ts.u.cl->length);
7076 if (cmp == 1 || cmp == -1 || cmp == -3)
7078 gfc_error ("Allocating %s at %L with type-spec requires the same "
7079 "character-length parameter as in the declaration",
7080 sym->name, &e->where);
7081 goto failure;
7085 /* In the variable definition context checks, gfc_expr_attr is used
7086 on the expression. This is fooled by the array specification
7087 present in e, thus we have to eliminate that one temporarily. */
7088 e2 = remove_last_array_ref (e);
7089 t = true;
7090 if (t && pointer)
7091 t = gfc_check_vardef_context (e2, true, true, false,
7092 _("ALLOCATE object"));
7093 if (t)
7094 t = gfc_check_vardef_context (e2, false, true, false,
7095 _("ALLOCATE object"));
7096 gfc_free_expr (e2);
7097 if (!t)
7098 goto failure;
7100 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7101 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7103 /* For class arrays, the initialization with SOURCE is done
7104 using _copy and trans_call. It is convenient to exploit that
7105 when the allocated type is different from the declared type but
7106 no SOURCE exists by setting expr3. */
7107 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
7109 else if (!code->expr3)
7111 /* Set up default initializer if needed. */
7112 gfc_typespec ts;
7113 gfc_expr *init_e;
7115 if (code->ext.alloc.ts.type == BT_DERIVED)
7116 ts = code->ext.alloc.ts;
7117 else
7118 ts = e->ts;
7120 if (ts.type == BT_CLASS)
7121 ts = ts.u.derived->components->ts;
7123 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
7125 gfc_code *init_st = gfc_get_code (EXEC_INIT_ASSIGN);
7126 init_st->loc = code->loc;
7127 init_st->expr1 = gfc_expr_to_initialize (e);
7128 init_st->expr2 = init_e;
7129 init_st->next = code->next;
7130 code->next = init_st;
7133 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
7135 /* Default initialization via MOLD (non-polymorphic). */
7136 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
7137 if (rhs != NULL)
7139 gfc_resolve_expr (rhs);
7140 gfc_free_expr (code->expr3);
7141 code->expr3 = rhs;
7145 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
7147 /* Make sure the vtab symbol is present when
7148 the module variables are generated. */
7149 gfc_typespec ts = e->ts;
7150 if (code->expr3)
7151 ts = code->expr3->ts;
7152 else if (code->ext.alloc.ts.type == BT_DERIVED)
7153 ts = code->ext.alloc.ts;
7155 gfc_find_derived_vtab (ts.u.derived);
7157 if (dimension)
7158 e = gfc_expr_to_initialize (e);
7160 else if (unlimited && !UNLIMITED_POLY (code->expr3))
7162 /* Again, make sure the vtab symbol is present when
7163 the module variables are generated. */
7164 gfc_typespec *ts = NULL;
7165 if (code->expr3)
7166 ts = &code->expr3->ts;
7167 else
7168 ts = &code->ext.alloc.ts;
7170 gcc_assert (ts);
7172 gfc_find_vtab (ts);
7174 if (dimension)
7175 e = gfc_expr_to_initialize (e);
7178 if (dimension == 0 && codimension == 0)
7179 goto success;
7181 /* Make sure the last reference node is an array specification. */
7183 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
7184 || (dimension && ref2->u.ar.dimen == 0))
7186 /* F08:C633. */
7187 if (code->expr3)
7189 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
7190 "in ALLOCATE statement at %L", &e->where))
7191 goto failure;
7192 *array_alloc_wo_spec = true;
7194 else
7196 gfc_error ("Array specification required in ALLOCATE statement "
7197 "at %L", &e->where);
7198 goto failure;
7202 /* Make sure that the array section reference makes sense in the
7203 context of an ALLOCATE specification. */
7205 ar = &ref2->u.ar;
7207 if (codimension)
7208 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
7209 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
7211 gfc_error ("Coarray specification required in ALLOCATE statement "
7212 "at %L", &e->where);
7213 goto failure;
7216 for (i = 0; i < ar->dimen; i++)
7218 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
7219 goto check_symbols;
7221 switch (ar->dimen_type[i])
7223 case DIMEN_ELEMENT:
7224 break;
7226 case DIMEN_RANGE:
7227 if (ar->start[i] != NULL
7228 && ar->end[i] != NULL
7229 && ar->stride[i] == NULL)
7230 break;
7232 /* Fall Through... */
7234 case DIMEN_UNKNOWN:
7235 case DIMEN_VECTOR:
7236 case DIMEN_STAR:
7237 case DIMEN_THIS_IMAGE:
7238 gfc_error ("Bad array specification in ALLOCATE statement at %L",
7239 &e->where);
7240 goto failure;
7243 check_symbols:
7244 for (a = code->ext.alloc.list; a; a = a->next)
7246 sym = a->expr->symtree->n.sym;
7248 /* TODO - check derived type components. */
7249 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
7250 continue;
7252 if ((ar->start[i] != NULL
7253 && gfc_find_sym_in_expr (sym, ar->start[i]))
7254 || (ar->end[i] != NULL
7255 && gfc_find_sym_in_expr (sym, ar->end[i])))
7257 gfc_error ("%qs must not appear in the array specification at "
7258 "%L in the same ALLOCATE statement where it is "
7259 "itself allocated", sym->name, &ar->where);
7260 goto failure;
7265 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7267 if (ar->dimen_type[i] == DIMEN_ELEMENT
7268 || ar->dimen_type[i] == DIMEN_RANGE)
7270 if (i == (ar->dimen + ar->codimen - 1))
7272 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7273 "statement at %L", &e->where);
7274 goto failure;
7276 continue;
7279 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7280 && ar->stride[i] == NULL)
7281 break;
7283 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7284 &e->where);
7285 goto failure;
7288 success:
7289 return true;
7291 failure:
7292 return false;
7296 static void
7297 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7299 gfc_expr *stat, *errmsg, *pe, *qe;
7300 gfc_alloc *a, *p, *q;
7302 stat = code->expr1;
7303 errmsg = code->expr2;
7305 /* Check the stat variable. */
7306 if (stat)
7308 gfc_check_vardef_context (stat, false, false, false,
7309 _("STAT variable"));
7311 if ((stat->ts.type != BT_INTEGER
7312 && !(stat->ref && (stat->ref->type == REF_ARRAY
7313 || stat->ref->type == REF_COMPONENT)))
7314 || stat->rank > 0)
7315 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7316 "variable", &stat->where);
7318 for (p = code->ext.alloc.list; p; p = p->next)
7319 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7321 gfc_ref *ref1, *ref2;
7322 bool found = true;
7324 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7325 ref1 = ref1->next, ref2 = ref2->next)
7327 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7328 continue;
7329 if (ref1->u.c.component->name != ref2->u.c.component->name)
7331 found = false;
7332 break;
7336 if (found)
7338 gfc_error ("Stat-variable at %L shall not be %sd within "
7339 "the same %s statement", &stat->where, fcn, fcn);
7340 break;
7345 /* Check the errmsg variable. */
7346 if (errmsg)
7348 if (!stat)
7349 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
7350 &errmsg->where);
7352 gfc_check_vardef_context (errmsg, false, false, false,
7353 _("ERRMSG variable"));
7355 if ((errmsg->ts.type != BT_CHARACTER
7356 && !(errmsg->ref
7357 && (errmsg->ref->type == REF_ARRAY
7358 || errmsg->ref->type == REF_COMPONENT)))
7359 || errmsg->rank > 0 )
7360 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7361 "variable", &errmsg->where);
7363 for (p = code->ext.alloc.list; p; p = p->next)
7364 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7366 gfc_ref *ref1, *ref2;
7367 bool found = true;
7369 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7370 ref1 = ref1->next, ref2 = ref2->next)
7372 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7373 continue;
7374 if (ref1->u.c.component->name != ref2->u.c.component->name)
7376 found = false;
7377 break;
7381 if (found)
7383 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7384 "the same %s statement", &errmsg->where, fcn, fcn);
7385 break;
7390 /* Check that an allocate-object appears only once in the statement. */
7392 for (p = code->ext.alloc.list; p; p = p->next)
7394 pe = p->expr;
7395 for (q = p->next; q; q = q->next)
7397 qe = q->expr;
7398 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7400 /* This is a potential collision. */
7401 gfc_ref *pr = pe->ref;
7402 gfc_ref *qr = qe->ref;
7404 /* Follow the references until
7405 a) They start to differ, in which case there is no error;
7406 you can deallocate a%b and a%c in a single statement
7407 b) Both of them stop, which is an error
7408 c) One of them stops, which is also an error. */
7409 while (1)
7411 if (pr == NULL && qr == NULL)
7413 gfc_error ("Allocate-object at %L also appears at %L",
7414 &pe->where, &qe->where);
7415 break;
7417 else if (pr != NULL && qr == NULL)
7419 gfc_error ("Allocate-object at %L is subobject of"
7420 " object at %L", &pe->where, &qe->where);
7421 break;
7423 else if (pr == NULL && qr != NULL)
7425 gfc_error ("Allocate-object at %L is subobject of"
7426 " object at %L", &qe->where, &pe->where);
7427 break;
7429 /* Here, pr != NULL && qr != NULL */
7430 gcc_assert(pr->type == qr->type);
7431 if (pr->type == REF_ARRAY)
7433 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7434 which are legal. */
7435 gcc_assert (qr->type == REF_ARRAY);
7437 if (pr->next && qr->next)
7439 int i;
7440 gfc_array_ref *par = &(pr->u.ar);
7441 gfc_array_ref *qar = &(qr->u.ar);
7443 for (i=0; i<par->dimen; i++)
7445 if ((par->start[i] != NULL
7446 || qar->start[i] != NULL)
7447 && gfc_dep_compare_expr (par->start[i],
7448 qar->start[i]) != 0)
7449 goto break_label;
7453 else
7455 if (pr->u.c.component->name != qr->u.c.component->name)
7456 break;
7459 pr = pr->next;
7460 qr = qr->next;
7462 break_label:
7468 if (strcmp (fcn, "ALLOCATE") == 0)
7470 bool arr_alloc_wo_spec = false;
7471 for (a = code->ext.alloc.list; a; a = a->next)
7472 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
7474 if (arr_alloc_wo_spec && code->expr3)
7476 /* Mark the allocate to have to take the array specification
7477 from the expr3. */
7478 code->ext.alloc.arr_spec_from_expr3 = 1;
7481 else
7483 for (a = code->ext.alloc.list; a; a = a->next)
7484 resolve_deallocate_expr (a->expr);
7489 /************ SELECT CASE resolution subroutines ************/
7491 /* Callback function for our mergesort variant. Determines interval
7492 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7493 op1 > op2. Assumes we're not dealing with the default case.
7494 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7495 There are nine situations to check. */
7497 static int
7498 compare_cases (const gfc_case *op1, const gfc_case *op2)
7500 int retval;
7502 if (op1->low == NULL) /* op1 = (:L) */
7504 /* op2 = (:N), so overlap. */
7505 retval = 0;
7506 /* op2 = (M:) or (M:N), L < M */
7507 if (op2->low != NULL
7508 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7509 retval = -1;
7511 else if (op1->high == NULL) /* op1 = (K:) */
7513 /* op2 = (M:), so overlap. */
7514 retval = 0;
7515 /* op2 = (:N) or (M:N), K > N */
7516 if (op2->high != NULL
7517 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7518 retval = 1;
7520 else /* op1 = (K:L) */
7522 if (op2->low == NULL) /* op2 = (:N), K > N */
7523 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7524 ? 1 : 0;
7525 else if (op2->high == NULL) /* op2 = (M:), L < M */
7526 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7527 ? -1 : 0;
7528 else /* op2 = (M:N) */
7530 retval = 0;
7531 /* L < M */
7532 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7533 retval = -1;
7534 /* K > N */
7535 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7536 retval = 1;
7540 return retval;
7544 /* Merge-sort a double linked case list, detecting overlap in the
7545 process. LIST is the head of the double linked case list before it
7546 is sorted. Returns the head of the sorted list if we don't see any
7547 overlap, or NULL otherwise. */
7549 static gfc_case *
7550 check_case_overlap (gfc_case *list)
7552 gfc_case *p, *q, *e, *tail;
7553 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7555 /* If the passed list was empty, return immediately. */
7556 if (!list)
7557 return NULL;
7559 overlap_seen = 0;
7560 insize = 1;
7562 /* Loop unconditionally. The only exit from this loop is a return
7563 statement, when we've finished sorting the case list. */
7564 for (;;)
7566 p = list;
7567 list = NULL;
7568 tail = NULL;
7570 /* Count the number of merges we do in this pass. */
7571 nmerges = 0;
7573 /* Loop while there exists a merge to be done. */
7574 while (p)
7576 int i;
7578 /* Count this merge. */
7579 nmerges++;
7581 /* Cut the list in two pieces by stepping INSIZE places
7582 forward in the list, starting from P. */
7583 psize = 0;
7584 q = p;
7585 for (i = 0; i < insize; i++)
7587 psize++;
7588 q = q->right;
7589 if (!q)
7590 break;
7592 qsize = insize;
7594 /* Now we have two lists. Merge them! */
7595 while (psize > 0 || (qsize > 0 && q != NULL))
7597 /* See from which the next case to merge comes from. */
7598 if (psize == 0)
7600 /* P is empty so the next case must come from Q. */
7601 e = q;
7602 q = q->right;
7603 qsize--;
7605 else if (qsize == 0 || q == NULL)
7607 /* Q is empty. */
7608 e = p;
7609 p = p->right;
7610 psize--;
7612 else
7614 cmp = compare_cases (p, q);
7615 if (cmp < 0)
7617 /* The whole case range for P is less than the
7618 one for Q. */
7619 e = p;
7620 p = p->right;
7621 psize--;
7623 else if (cmp > 0)
7625 /* The whole case range for Q is greater than
7626 the case range for P. */
7627 e = q;
7628 q = q->right;
7629 qsize--;
7631 else
7633 /* The cases overlap, or they are the same
7634 element in the list. Either way, we must
7635 issue an error and get the next case from P. */
7636 /* FIXME: Sort P and Q by line number. */
7637 gfc_error ("CASE label at %L overlaps with CASE "
7638 "label at %L", &p->where, &q->where);
7639 overlap_seen = 1;
7640 e = p;
7641 p = p->right;
7642 psize--;
7646 /* Add the next element to the merged list. */
7647 if (tail)
7648 tail->right = e;
7649 else
7650 list = e;
7651 e->left = tail;
7652 tail = e;
7655 /* P has now stepped INSIZE places along, and so has Q. So
7656 they're the same. */
7657 p = q;
7659 tail->right = NULL;
7661 /* If we have done only one merge or none at all, we've
7662 finished sorting the cases. */
7663 if (nmerges <= 1)
7665 if (!overlap_seen)
7666 return list;
7667 else
7668 return NULL;
7671 /* Otherwise repeat, merging lists twice the size. */
7672 insize *= 2;
7677 /* Check to see if an expression is suitable for use in a CASE statement.
7678 Makes sure that all case expressions are scalar constants of the same
7679 type. Return false if anything is wrong. */
7681 static bool
7682 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7684 if (e == NULL) return true;
7686 if (e->ts.type != case_expr->ts.type)
7688 gfc_error ("Expression in CASE statement at %L must be of type %s",
7689 &e->where, gfc_basic_typename (case_expr->ts.type));
7690 return false;
7693 /* C805 (R808) For a given case-construct, each case-value shall be of
7694 the same type as case-expr. For character type, length differences
7695 are allowed, but the kind type parameters shall be the same. */
7697 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7699 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7700 &e->where, case_expr->ts.kind);
7701 return false;
7704 /* Convert the case value kind to that of case expression kind,
7705 if needed */
7707 if (e->ts.kind != case_expr->ts.kind)
7708 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7710 if (e->rank != 0)
7712 gfc_error ("Expression in CASE statement at %L must be scalar",
7713 &e->where);
7714 return false;
7717 return true;
7721 /* Given a completely parsed select statement, we:
7723 - Validate all expressions and code within the SELECT.
7724 - Make sure that the selection expression is not of the wrong type.
7725 - Make sure that no case ranges overlap.
7726 - Eliminate unreachable cases and unreachable code resulting from
7727 removing case labels.
7729 The standard does allow unreachable cases, e.g. CASE (5:3). But
7730 they are a hassle for code generation, and to prevent that, we just
7731 cut them out here. This is not necessary for overlapping cases
7732 because they are illegal and we never even try to generate code.
7734 We have the additional caveat that a SELECT construct could have
7735 been a computed GOTO in the source code. Fortunately we can fairly
7736 easily work around that here: The case_expr for a "real" SELECT CASE
7737 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7738 we have to do is make sure that the case_expr is a scalar integer
7739 expression. */
7741 static void
7742 resolve_select (gfc_code *code, bool select_type)
7744 gfc_code *body;
7745 gfc_expr *case_expr;
7746 gfc_case *cp, *default_case, *tail, *head;
7747 int seen_unreachable;
7748 int seen_logical;
7749 int ncases;
7750 bt type;
7751 bool t;
7753 if (code->expr1 == NULL)
7755 /* This was actually a computed GOTO statement. */
7756 case_expr = code->expr2;
7757 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7758 gfc_error ("Selection expression in computed GOTO statement "
7759 "at %L must be a scalar integer expression",
7760 &case_expr->where);
7762 /* Further checking is not necessary because this SELECT was built
7763 by the compiler, so it should always be OK. Just move the
7764 case_expr from expr2 to expr so that we can handle computed
7765 GOTOs as normal SELECTs from here on. */
7766 code->expr1 = code->expr2;
7767 code->expr2 = NULL;
7768 return;
7771 case_expr = code->expr1;
7772 type = case_expr->ts.type;
7774 /* F08:C830. */
7775 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7777 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7778 &case_expr->where, gfc_typename (&case_expr->ts));
7780 /* Punt. Going on here just produce more garbage error messages. */
7781 return;
7784 /* F08:R842. */
7785 if (!select_type && case_expr->rank != 0)
7787 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7788 "expression", &case_expr->where);
7790 /* Punt. */
7791 return;
7794 /* Raise a warning if an INTEGER case value exceeds the range of
7795 the case-expr. Later, all expressions will be promoted to the
7796 largest kind of all case-labels. */
7798 if (type == BT_INTEGER)
7799 for (body = code->block; body; body = body->block)
7800 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7802 if (cp->low
7803 && gfc_check_integer_range (cp->low->value.integer,
7804 case_expr->ts.kind) != ARITH_OK)
7805 gfc_warning (0, "Expression in CASE statement at %L is "
7806 "not in the range of %s", &cp->low->where,
7807 gfc_typename (&case_expr->ts));
7809 if (cp->high
7810 && cp->low != cp->high
7811 && gfc_check_integer_range (cp->high->value.integer,
7812 case_expr->ts.kind) != ARITH_OK)
7813 gfc_warning (0, "Expression in CASE statement at %L is "
7814 "not in the range of %s", &cp->high->where,
7815 gfc_typename (&case_expr->ts));
7818 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7819 of the SELECT CASE expression and its CASE values. Walk the lists
7820 of case values, and if we find a mismatch, promote case_expr to
7821 the appropriate kind. */
7823 if (type == BT_LOGICAL || type == BT_INTEGER)
7825 for (body = code->block; body; body = body->block)
7827 /* Walk the case label list. */
7828 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7830 /* Intercept the DEFAULT case. It does not have a kind. */
7831 if (cp->low == NULL && cp->high == NULL)
7832 continue;
7834 /* Unreachable case ranges are discarded, so ignore. */
7835 if (cp->low != NULL && cp->high != NULL
7836 && cp->low != cp->high
7837 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7838 continue;
7840 if (cp->low != NULL
7841 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7842 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7844 if (cp->high != NULL
7845 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7846 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7851 /* Assume there is no DEFAULT case. */
7852 default_case = NULL;
7853 head = tail = NULL;
7854 ncases = 0;
7855 seen_logical = 0;
7857 for (body = code->block; body; body = body->block)
7859 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7860 t = true;
7861 seen_unreachable = 0;
7863 /* Walk the case label list, making sure that all case labels
7864 are legal. */
7865 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7867 /* Count the number of cases in the whole construct. */
7868 ncases++;
7870 /* Intercept the DEFAULT case. */
7871 if (cp->low == NULL && cp->high == NULL)
7873 if (default_case != NULL)
7875 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7876 "by a second DEFAULT CASE at %L",
7877 &default_case->where, &cp->where);
7878 t = false;
7879 break;
7881 else
7883 default_case = cp;
7884 continue;
7888 /* Deal with single value cases and case ranges. Errors are
7889 issued from the validation function. */
7890 if (!validate_case_label_expr (cp->low, case_expr)
7891 || !validate_case_label_expr (cp->high, case_expr))
7893 t = false;
7894 break;
7897 if (type == BT_LOGICAL
7898 && ((cp->low == NULL || cp->high == NULL)
7899 || cp->low != cp->high))
7901 gfc_error ("Logical range in CASE statement at %L is not "
7902 "allowed", &cp->low->where);
7903 t = false;
7904 break;
7907 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7909 int value;
7910 value = cp->low->value.logical == 0 ? 2 : 1;
7911 if (value & seen_logical)
7913 gfc_error ("Constant logical value in CASE statement "
7914 "is repeated at %L",
7915 &cp->low->where);
7916 t = false;
7917 break;
7919 seen_logical |= value;
7922 if (cp->low != NULL && cp->high != NULL
7923 && cp->low != cp->high
7924 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7926 if (warn_surprising)
7927 gfc_warning (OPT_Wsurprising,
7928 "Range specification at %L can never be matched",
7929 &cp->where);
7931 cp->unreachable = 1;
7932 seen_unreachable = 1;
7934 else
7936 /* If the case range can be matched, it can also overlap with
7937 other cases. To make sure it does not, we put it in a
7938 double linked list here. We sort that with a merge sort
7939 later on to detect any overlapping cases. */
7940 if (!head)
7942 head = tail = cp;
7943 head->right = head->left = NULL;
7945 else
7947 tail->right = cp;
7948 tail->right->left = tail;
7949 tail = tail->right;
7950 tail->right = NULL;
7955 /* It there was a failure in the previous case label, give up
7956 for this case label list. Continue with the next block. */
7957 if (!t)
7958 continue;
7960 /* See if any case labels that are unreachable have been seen.
7961 If so, we eliminate them. This is a bit of a kludge because
7962 the case lists for a single case statement (label) is a
7963 single forward linked lists. */
7964 if (seen_unreachable)
7966 /* Advance until the first case in the list is reachable. */
7967 while (body->ext.block.case_list != NULL
7968 && body->ext.block.case_list->unreachable)
7970 gfc_case *n = body->ext.block.case_list;
7971 body->ext.block.case_list = body->ext.block.case_list->next;
7972 n->next = NULL;
7973 gfc_free_case_list (n);
7976 /* Strip all other unreachable cases. */
7977 if (body->ext.block.case_list)
7979 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
7981 if (cp->next->unreachable)
7983 gfc_case *n = cp->next;
7984 cp->next = cp->next->next;
7985 n->next = NULL;
7986 gfc_free_case_list (n);
7993 /* See if there were overlapping cases. If the check returns NULL,
7994 there was overlap. In that case we don't do anything. If head
7995 is non-NULL, we prepend the DEFAULT case. The sorted list can
7996 then used during code generation for SELECT CASE constructs with
7997 a case expression of a CHARACTER type. */
7998 if (head)
8000 head = check_case_overlap (head);
8002 /* Prepend the default_case if it is there. */
8003 if (head != NULL && default_case)
8005 default_case->left = NULL;
8006 default_case->right = head;
8007 head->left = default_case;
8011 /* Eliminate dead blocks that may be the result if we've seen
8012 unreachable case labels for a block. */
8013 for (body = code; body && body->block; body = body->block)
8015 if (body->block->ext.block.case_list == NULL)
8017 /* Cut the unreachable block from the code chain. */
8018 gfc_code *c = body->block;
8019 body->block = c->block;
8021 /* Kill the dead block, but not the blocks below it. */
8022 c->block = NULL;
8023 gfc_free_statements (c);
8027 /* More than two cases is legal but insane for logical selects.
8028 Issue a warning for it. */
8029 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
8030 gfc_warning (OPT_Wsurprising,
8031 "Logical SELECT CASE block at %L has more that two cases",
8032 &code->loc);
8036 /* Check if a derived type is extensible. */
8038 bool
8039 gfc_type_is_extensible (gfc_symbol *sym)
8041 return !(sym->attr.is_bind_c || sym->attr.sequence
8042 || (sym->attr.is_class
8043 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
8047 static void
8048 resolve_types (gfc_namespace *ns);
8050 /* Resolve an associate-name: Resolve target and ensure the type-spec is
8051 correct as well as possibly the array-spec. */
8053 static void
8054 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
8056 gfc_expr* target;
8058 gcc_assert (sym->assoc);
8059 gcc_assert (sym->attr.flavor == FL_VARIABLE);
8061 /* If this is for SELECT TYPE, the target may not yet be set. In that
8062 case, return. Resolution will be called later manually again when
8063 this is done. */
8064 target = sym->assoc->target;
8065 if (!target)
8066 return;
8067 gcc_assert (!sym->assoc->dangling);
8069 if (resolve_target && !gfc_resolve_expr (target))
8070 return;
8072 /* For variable targets, we get some attributes from the target. */
8073 if (target->expr_type == EXPR_VARIABLE)
8075 gfc_symbol* tsym;
8077 gcc_assert (target->symtree);
8078 tsym = target->symtree->n.sym;
8080 sym->attr.asynchronous = tsym->attr.asynchronous;
8081 sym->attr.volatile_ = tsym->attr.volatile_;
8083 sym->attr.target = tsym->attr.target
8084 || gfc_expr_attr (target).pointer;
8085 if (is_subref_array (target))
8086 sym->attr.subref_array_pointer = 1;
8089 /* Get type if this was not already set. Note that it can be
8090 some other type than the target in case this is a SELECT TYPE
8091 selector! So we must not update when the type is already there. */
8092 if (sym->ts.type == BT_UNKNOWN)
8093 sym->ts = target->ts;
8094 gcc_assert (sym->ts.type != BT_UNKNOWN);
8096 /* See if this is a valid association-to-variable. */
8097 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
8098 && !gfc_has_vector_subscript (target));
8100 /* Finally resolve if this is an array or not. */
8101 if (sym->attr.dimension && target->rank == 0)
8103 /* primary.c makes the assumption that a reference to an associate
8104 name followed by a left parenthesis is an array reference. */
8105 if (sym->ts.type != BT_CHARACTER)
8106 gfc_error ("Associate-name %qs at %L is used as array",
8107 sym->name, &sym->declared_at);
8108 sym->attr.dimension = 0;
8109 return;
8113 /* We cannot deal with class selectors that need temporaries. */
8114 if (target->ts.type == BT_CLASS
8115 && gfc_ref_needs_temporary_p (target->ref))
8117 gfc_error ("CLASS selector at %L needs a temporary which is not "
8118 "yet implemented", &target->where);
8119 return;
8122 if (target->ts.type == BT_CLASS)
8123 gfc_fix_class_refs (target);
8125 if (target->rank != 0)
8127 gfc_array_spec *as;
8128 if (sym->ts.type != BT_CLASS && !sym->as)
8130 as = gfc_get_array_spec ();
8131 as->rank = target->rank;
8132 as->type = AS_DEFERRED;
8133 as->corank = gfc_get_corank (target);
8134 sym->attr.dimension = 1;
8135 if (as->corank != 0)
8136 sym->attr.codimension = 1;
8137 sym->as = as;
8140 else
8142 /* target's rank is 0, but the type of the sym is still array valued,
8143 which has to be corrected. */
8144 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
8146 gfc_array_spec *as;
8147 symbol_attribute attr;
8148 /* The associated variable's type is still the array type
8149 correct this now. */
8150 gfc_typespec *ts = &target->ts;
8151 gfc_ref *ref;
8152 gfc_component *c;
8153 for (ref = target->ref; ref != NULL; ref = ref->next)
8155 switch (ref->type)
8157 case REF_COMPONENT:
8158 ts = &ref->u.c.component->ts;
8159 break;
8160 case REF_ARRAY:
8161 if (ts->type == BT_CLASS)
8162 ts = &ts->u.derived->components->ts;
8163 break;
8164 default:
8165 break;
8168 /* Create a scalar instance of the current class type. Because the
8169 rank of a class array goes into its name, the type has to be
8170 rebuild. The alternative of (re-)setting just the attributes
8171 and as in the current type, destroys the type also in other
8172 places. */
8173 as = NULL;
8174 sym->ts = *ts;
8175 sym->ts.type = BT_CLASS;
8176 attr = CLASS_DATA (sym)->attr;
8177 attr.class_ok = 0;
8178 attr.associate_var = 1;
8179 attr.dimension = attr.codimension = 0;
8180 attr.class_pointer = 1;
8181 if (!gfc_build_class_symbol (&sym->ts, &attr, &as))
8182 gcc_unreachable ();
8183 /* Make sure the _vptr is set. */
8184 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true);
8185 if (c->ts.u.derived == NULL)
8186 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived);
8187 CLASS_DATA (sym)->attr.pointer = 1;
8188 CLASS_DATA (sym)->attr.class_pointer = 1;
8189 gfc_set_sym_referenced (sym->ts.u.derived);
8190 gfc_commit_symbol (sym->ts.u.derived);
8191 /* _vptr now has the _vtab in it, change it to the _vtype. */
8192 if (c->ts.u.derived->attr.vtab)
8193 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
8194 c->ts.u.derived->ns->types_resolved = 0;
8195 resolve_types (c->ts.u.derived->ns);
8199 /* Mark this as an associate variable. */
8200 sym->attr.associate_var = 1;
8202 /* If the target is a good class object, so is the associate variable. */
8203 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
8204 sym->attr.class_ok = 1;
8208 /* Resolve a SELECT TYPE statement. */
8210 static void
8211 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
8213 gfc_symbol *selector_type;
8214 gfc_code *body, *new_st, *if_st, *tail;
8215 gfc_code *class_is = NULL, *default_case = NULL;
8216 gfc_case *c;
8217 gfc_symtree *st;
8218 char name[GFC_MAX_SYMBOL_LEN];
8219 gfc_namespace *ns;
8220 int error = 0;
8221 int charlen = 0;
8223 ns = code->ext.block.ns;
8224 gfc_resolve (ns);
8226 /* Check for F03:C813. */
8227 if (code->expr1->ts.type != BT_CLASS
8228 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
8230 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
8231 "at %L", &code->loc);
8232 return;
8235 if (!code->expr1->symtree->n.sym->attr.class_ok)
8236 return;
8238 if (code->expr2)
8240 if (code->expr1->symtree->n.sym->attr.untyped)
8241 code->expr1->symtree->n.sym->ts = code->expr2->ts;
8242 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
8244 /* F2008: C803 The selector expression must not be coindexed. */
8245 if (gfc_is_coindexed (code->expr2))
8247 gfc_error ("Selector at %L must not be coindexed",
8248 &code->expr2->where);
8249 return;
8253 else
8255 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
8257 if (gfc_is_coindexed (code->expr1))
8259 gfc_error ("Selector at %L must not be coindexed",
8260 &code->expr1->where);
8261 return;
8265 /* Loop over TYPE IS / CLASS IS cases. */
8266 for (body = code->block; body; body = body->block)
8268 c = body->ext.block.case_list;
8270 /* Check F03:C815. */
8271 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8272 && !selector_type->attr.unlimited_polymorphic
8273 && !gfc_type_is_extensible (c->ts.u.derived))
8275 gfc_error ("Derived type %qs at %L must be extensible",
8276 c->ts.u.derived->name, &c->where);
8277 error++;
8278 continue;
8281 /* Check F03:C816. */
8282 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
8283 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
8284 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
8286 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8287 gfc_error ("Derived type %qs at %L must be an extension of %qs",
8288 c->ts.u.derived->name, &c->where, selector_type->name);
8289 else
8290 gfc_error ("Unexpected intrinsic type %qs at %L",
8291 gfc_basic_typename (c->ts.type), &c->where);
8292 error++;
8293 continue;
8296 /* Check F03:C814. */
8297 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
8299 gfc_error ("The type-spec at %L shall specify that each length "
8300 "type parameter is assumed", &c->where);
8301 error++;
8302 continue;
8305 /* Intercept the DEFAULT case. */
8306 if (c->ts.type == BT_UNKNOWN)
8308 /* Check F03:C818. */
8309 if (default_case)
8311 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8312 "by a second DEFAULT CASE at %L",
8313 &default_case->ext.block.case_list->where, &c->where);
8314 error++;
8315 continue;
8318 default_case = body;
8322 if (error > 0)
8323 return;
8325 /* Transform SELECT TYPE statement to BLOCK and associate selector to
8326 target if present. If there are any EXIT statements referring to the
8327 SELECT TYPE construct, this is no problem because the gfc_code
8328 reference stays the same and EXIT is equally possible from the BLOCK
8329 it is changed to. */
8330 code->op = EXEC_BLOCK;
8331 if (code->expr2)
8333 gfc_association_list* assoc;
8335 assoc = gfc_get_association_list ();
8336 assoc->st = code->expr1->symtree;
8337 assoc->target = gfc_copy_expr (code->expr2);
8338 assoc->target->where = code->expr2->where;
8339 /* assoc->variable will be set by resolve_assoc_var. */
8341 code->ext.block.assoc = assoc;
8342 code->expr1->symtree->n.sym->assoc = assoc;
8344 resolve_assoc_var (code->expr1->symtree->n.sym, false);
8346 else
8347 code->ext.block.assoc = NULL;
8349 /* Add EXEC_SELECT to switch on type. */
8350 new_st = gfc_get_code (code->op);
8351 new_st->expr1 = code->expr1;
8352 new_st->expr2 = code->expr2;
8353 new_st->block = code->block;
8354 code->expr1 = code->expr2 = NULL;
8355 code->block = NULL;
8356 if (!ns->code)
8357 ns->code = new_st;
8358 else
8359 ns->code->next = new_st;
8360 code = new_st;
8361 code->op = EXEC_SELECT;
8363 gfc_add_vptr_component (code->expr1);
8364 gfc_add_hash_component (code->expr1);
8366 /* Loop over TYPE IS / CLASS IS cases. */
8367 for (body = code->block; body; body = body->block)
8369 c = body->ext.block.case_list;
8371 if (c->ts.type == BT_DERIVED)
8372 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8373 c->ts.u.derived->hash_value);
8374 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8376 gfc_symbol *ivtab;
8377 gfc_expr *e;
8379 ivtab = gfc_find_vtab (&c->ts);
8380 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
8381 e = CLASS_DATA (ivtab)->initializer;
8382 c->low = c->high = gfc_copy_expr (e);
8385 else if (c->ts.type == BT_UNKNOWN)
8386 continue;
8388 /* Associate temporary to selector. This should only be done
8389 when this case is actually true, so build a new ASSOCIATE
8390 that does precisely this here (instead of using the
8391 'global' one). */
8393 if (c->ts.type == BT_CLASS)
8394 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8395 else if (c->ts.type == BT_DERIVED)
8396 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8397 else if (c->ts.type == BT_CHARACTER)
8399 if (c->ts.u.cl && c->ts.u.cl->length
8400 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8401 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8402 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8403 charlen, c->ts.kind);
8405 else
8406 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8407 c->ts.kind);
8409 st = gfc_find_symtree (ns->sym_root, name);
8410 gcc_assert (st->n.sym->assoc);
8411 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
8412 st->n.sym->assoc->target->where = code->expr1->where;
8413 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8414 gfc_add_data_component (st->n.sym->assoc->target);
8416 new_st = gfc_get_code (EXEC_BLOCK);
8417 new_st->ext.block.ns = gfc_build_block_ns (ns);
8418 new_st->ext.block.ns->code = body->next;
8419 body->next = new_st;
8421 /* Chain in the new list only if it is marked as dangling. Otherwise
8422 there is a CASE label overlap and this is already used. Just ignore,
8423 the error is diagnosed elsewhere. */
8424 if (st->n.sym->assoc->dangling)
8426 new_st->ext.block.assoc = st->n.sym->assoc;
8427 st->n.sym->assoc->dangling = 0;
8430 resolve_assoc_var (st->n.sym, false);
8433 /* Take out CLASS IS cases for separate treatment. */
8434 body = code;
8435 while (body && body->block)
8437 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8439 /* Add to class_is list. */
8440 if (class_is == NULL)
8442 class_is = body->block;
8443 tail = class_is;
8445 else
8447 for (tail = class_is; tail->block; tail = tail->block) ;
8448 tail->block = body->block;
8449 tail = tail->block;
8451 /* Remove from EXEC_SELECT list. */
8452 body->block = body->block->block;
8453 tail->block = NULL;
8455 else
8456 body = body->block;
8459 if (class_is)
8461 gfc_symbol *vtab;
8463 if (!default_case)
8465 /* Add a default case to hold the CLASS IS cases. */
8466 for (tail = code; tail->block; tail = tail->block) ;
8467 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
8468 tail = tail->block;
8469 tail->ext.block.case_list = gfc_get_case ();
8470 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8471 tail->next = NULL;
8472 default_case = tail;
8475 /* More than one CLASS IS block? */
8476 if (class_is->block)
8478 gfc_code **c1,*c2;
8479 bool swapped;
8480 /* Sort CLASS IS blocks by extension level. */
8483 swapped = false;
8484 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8486 c2 = (*c1)->block;
8487 /* F03:C817 (check for doubles). */
8488 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8489 == c2->ext.block.case_list->ts.u.derived->hash_value)
8491 gfc_error ("Double CLASS IS block in SELECT TYPE "
8492 "statement at %L",
8493 &c2->ext.block.case_list->where);
8494 return;
8496 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8497 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8499 /* Swap. */
8500 (*c1)->block = c2->block;
8501 c2->block = *c1;
8502 *c1 = c2;
8503 swapped = true;
8507 while (swapped);
8510 /* Generate IF chain. */
8511 if_st = gfc_get_code (EXEC_IF);
8512 new_st = if_st;
8513 for (body = class_is; body; body = body->block)
8515 new_st->block = gfc_get_code (EXEC_IF);
8516 new_st = new_st->block;
8517 /* Set up IF condition: Call _gfortran_is_extension_of. */
8518 new_st->expr1 = gfc_get_expr ();
8519 new_st->expr1->expr_type = EXPR_FUNCTION;
8520 new_st->expr1->ts.type = BT_LOGICAL;
8521 new_st->expr1->ts.kind = 4;
8522 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8523 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8524 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8525 /* Set up arguments. */
8526 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8527 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8528 new_st->expr1->value.function.actual->expr->where = code->loc;
8529 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8530 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8531 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8532 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8533 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8534 new_st->next = body->next;
8536 if (default_case->next)
8538 new_st->block = gfc_get_code (EXEC_IF);
8539 new_st = new_st->block;
8540 new_st->next = default_case->next;
8543 /* Replace CLASS DEFAULT code by the IF chain. */
8544 default_case->next = if_st;
8547 /* Resolve the internal code. This can not be done earlier because
8548 it requires that the sym->assoc of selectors is set already. */
8549 gfc_current_ns = ns;
8550 gfc_resolve_blocks (code->block, gfc_current_ns);
8551 gfc_current_ns = old_ns;
8553 resolve_select (code, true);
8557 /* Resolve a transfer statement. This is making sure that:
8558 -- a derived type being transferred has only non-pointer components
8559 -- a derived type being transferred doesn't have private components, unless
8560 it's being transferred from the module where the type was defined
8561 -- we're not trying to transfer a whole assumed size array. */
8563 static void
8564 resolve_transfer (gfc_code *code)
8566 gfc_typespec *ts;
8567 gfc_symbol *sym;
8568 gfc_ref *ref;
8569 gfc_expr *exp;
8571 exp = code->expr1;
8573 while (exp != NULL && exp->expr_type == EXPR_OP
8574 && exp->value.op.op == INTRINSIC_PARENTHESES)
8575 exp = exp->value.op.op1;
8577 if (exp && exp->expr_type == EXPR_NULL
8578 && code->ext.dt)
8580 gfc_error ("Invalid context for NULL () intrinsic at %L",
8581 &exp->where);
8582 return;
8585 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8586 && exp->expr_type != EXPR_FUNCTION
8587 && exp->expr_type != EXPR_STRUCTURE))
8588 return;
8590 /* If we are reading, the variable will be changed. Note that
8591 code->ext.dt may be NULL if the TRANSFER is related to
8592 an INQUIRE statement -- but in this case, we are not reading, either. */
8593 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8594 && !gfc_check_vardef_context (exp, false, false, false,
8595 _("item in READ")))
8596 return;
8598 ts = exp->expr_type == EXPR_STRUCTURE ? &exp->ts : &exp->symtree->n.sym->ts;
8600 /* Go to actual component transferred. */
8601 for (ref = exp->ref; ref; ref = ref->next)
8602 if (ref->type == REF_COMPONENT)
8603 ts = &ref->u.c.component->ts;
8605 if (ts->type == BT_CLASS)
8607 /* FIXME: Test for defined input/output. */
8608 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8609 "it is processed by a defined input/output procedure",
8610 &code->loc);
8611 return;
8614 if (ts->type == BT_DERIVED)
8616 /* Check that transferred derived type doesn't contain POINTER
8617 components. */
8618 if (ts->u.derived->attr.pointer_comp)
8620 gfc_error ("Data transfer element at %L cannot have POINTER "
8621 "components unless it is processed by a defined "
8622 "input/output procedure", &code->loc);
8623 return;
8626 /* F08:C935. */
8627 if (ts->u.derived->attr.proc_pointer_comp)
8629 gfc_error ("Data transfer element at %L cannot have "
8630 "procedure pointer components", &code->loc);
8631 return;
8634 if (ts->u.derived->attr.alloc_comp)
8636 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8637 "components unless it is processed by a defined "
8638 "input/output procedure", &code->loc);
8639 return;
8642 /* C_PTR and C_FUNPTR have private components which means they can not
8643 be printed. However, if -std=gnu and not -pedantic, allow
8644 the component to be printed to help debugging. */
8645 if (ts->u.derived->ts.f90_type == BT_VOID)
8647 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8648 "cannot have PRIVATE components", &code->loc))
8649 return;
8651 else if (derived_inaccessible (ts->u.derived))
8653 gfc_error ("Data transfer element at %L cannot have "
8654 "PRIVATE components",&code->loc);
8655 return;
8659 if (exp->expr_type == EXPR_STRUCTURE)
8660 return;
8662 sym = exp->symtree->n.sym;
8664 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8665 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8667 gfc_error ("Data transfer element at %L cannot be a full reference to "
8668 "an assumed-size array", &code->loc);
8669 return;
8674 /*********** Toplevel code resolution subroutines ***********/
8676 /* Find the set of labels that are reachable from this block. We also
8677 record the last statement in each block. */
8679 static void
8680 find_reachable_labels (gfc_code *block)
8682 gfc_code *c;
8684 if (!block)
8685 return;
8687 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8689 /* Collect labels in this block. We don't keep those corresponding
8690 to END {IF|SELECT}, these are checked in resolve_branch by going
8691 up through the code_stack. */
8692 for (c = block; c; c = c->next)
8694 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8695 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8698 /* Merge with labels from parent block. */
8699 if (cs_base->prev)
8701 gcc_assert (cs_base->prev->reachable_labels);
8702 bitmap_ior_into (cs_base->reachable_labels,
8703 cs_base->prev->reachable_labels);
8708 static void
8709 resolve_lock_unlock (gfc_code *code)
8711 if (code->expr1->expr_type == EXPR_FUNCTION
8712 && code->expr1->value.function.isym
8713 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
8714 remove_caf_get_intrinsic (code->expr1);
8716 if (code->expr1->ts.type != BT_DERIVED
8717 || code->expr1->expr_type != EXPR_VARIABLE
8718 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8719 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8720 || code->expr1->rank != 0
8721 || (!gfc_is_coarray (code->expr1) && !gfc_is_coindexed (code->expr1)))
8722 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8723 &code->expr1->where);
8725 /* Check STAT. */
8726 if (code->expr2
8727 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8728 || code->expr2->expr_type != EXPR_VARIABLE))
8729 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8730 &code->expr2->where);
8732 if (code->expr2
8733 && !gfc_check_vardef_context (code->expr2, false, false, false,
8734 _("STAT variable")))
8735 return;
8737 /* Check ERRMSG. */
8738 if (code->expr3
8739 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8740 || code->expr3->expr_type != EXPR_VARIABLE))
8741 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8742 &code->expr3->where);
8744 if (code->expr3
8745 && !gfc_check_vardef_context (code->expr3, false, false, false,
8746 _("ERRMSG variable")))
8747 return;
8749 /* Check ACQUIRED_LOCK. */
8750 if (code->expr4
8751 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8752 || code->expr4->expr_type != EXPR_VARIABLE))
8753 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8754 "variable", &code->expr4->where);
8756 if (code->expr4
8757 && !gfc_check_vardef_context (code->expr4, false, false, false,
8758 _("ACQUIRED_LOCK variable")))
8759 return;
8763 static void
8764 resolve_critical (gfc_code *code)
8766 gfc_symtree *symtree;
8767 gfc_symbol *lock_type;
8768 char name[GFC_MAX_SYMBOL_LEN];
8769 static int serial = 0;
8771 if (flag_coarray != GFC_FCOARRAY_LIB)
8772 return;
8774 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
8775 GFC_PREFIX ("lock_type"));
8776 if (symtree)
8777 lock_type = symtree->n.sym;
8778 else
8780 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
8781 false) != 0)
8782 gcc_unreachable ();
8783 lock_type = symtree->n.sym;
8784 lock_type->attr.flavor = FL_DERIVED;
8785 lock_type->attr.zero_comp = 1;
8786 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
8787 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
8790 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
8791 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
8792 gcc_unreachable ();
8794 code->resolved_sym = symtree->n.sym;
8795 symtree->n.sym->attr.flavor = FL_VARIABLE;
8796 symtree->n.sym->attr.referenced = 1;
8797 symtree->n.sym->attr.artificial = 1;
8798 symtree->n.sym->attr.codimension = 1;
8799 symtree->n.sym->ts.type = BT_DERIVED;
8800 symtree->n.sym->ts.u.derived = lock_type;
8801 symtree->n.sym->as = gfc_get_array_spec ();
8802 symtree->n.sym->as->corank = 1;
8803 symtree->n.sym->as->type = AS_EXPLICIT;
8804 symtree->n.sym->as->cotype = AS_EXPLICIT;
8805 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
8806 NULL, 1);
8810 static void
8811 resolve_sync (gfc_code *code)
8813 /* Check imageset. The * case matches expr1 == NULL. */
8814 if (code->expr1)
8816 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8817 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8818 "INTEGER expression", &code->expr1->where);
8819 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8820 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8821 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8822 &code->expr1->where);
8823 else if (code->expr1->expr_type == EXPR_ARRAY
8824 && gfc_simplify_expr (code->expr1, 0))
8826 gfc_constructor *cons;
8827 cons = gfc_constructor_first (code->expr1->value.constructor);
8828 for (; cons; cons = gfc_constructor_next (cons))
8829 if (cons->expr->expr_type == EXPR_CONSTANT
8830 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8831 gfc_error ("Imageset argument at %L must between 1 and "
8832 "num_images()", &cons->expr->where);
8836 /* Check STAT. */
8837 if (code->expr2
8838 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8839 || code->expr2->expr_type != EXPR_VARIABLE))
8840 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8841 &code->expr2->where);
8843 /* Check ERRMSG. */
8844 if (code->expr3
8845 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8846 || code->expr3->expr_type != EXPR_VARIABLE))
8847 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8848 &code->expr3->where);
8852 /* Given a branch to a label, see if the branch is conforming.
8853 The code node describes where the branch is located. */
8855 static void
8856 resolve_branch (gfc_st_label *label, gfc_code *code)
8858 code_stack *stack;
8860 if (label == NULL)
8861 return;
8863 /* Step one: is this a valid branching target? */
8865 if (label->defined == ST_LABEL_UNKNOWN)
8867 gfc_error ("Label %d referenced at %L is never defined", label->value,
8868 &label->where);
8869 return;
8872 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
8874 gfc_error ("Statement at %L is not a valid branch target statement "
8875 "for the branch statement at %L", &label->where, &code->loc);
8876 return;
8879 /* Step two: make sure this branch is not a branch to itself ;-) */
8881 if (code->here == label)
8883 gfc_warning (0,
8884 "Branch at %L may result in an infinite loop", &code->loc);
8885 return;
8888 /* Step three: See if the label is in the same block as the
8889 branching statement. The hard work has been done by setting up
8890 the bitmap reachable_labels. */
8892 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8894 /* Check now whether there is a CRITICAL construct; if so, check
8895 whether the label is still visible outside of the CRITICAL block,
8896 which is invalid. */
8897 for (stack = cs_base; stack; stack = stack->prev)
8899 if (stack->current->op == EXEC_CRITICAL
8900 && bitmap_bit_p (stack->reachable_labels, label->value))
8901 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8902 "label at %L", &code->loc, &label->where);
8903 else if (stack->current->op == EXEC_DO_CONCURRENT
8904 && bitmap_bit_p (stack->reachable_labels, label->value))
8905 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8906 "for label at %L", &code->loc, &label->where);
8909 return;
8912 /* Step four: If we haven't found the label in the bitmap, it may
8913 still be the label of the END of the enclosing block, in which
8914 case we find it by going up the code_stack. */
8916 for (stack = cs_base; stack; stack = stack->prev)
8918 if (stack->current->next && stack->current->next->here == label)
8919 break;
8920 if (stack->current->op == EXEC_CRITICAL)
8922 /* Note: A label at END CRITICAL does not leave the CRITICAL
8923 construct as END CRITICAL is still part of it. */
8924 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8925 " at %L", &code->loc, &label->where);
8926 return;
8928 else if (stack->current->op == EXEC_DO_CONCURRENT)
8930 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
8931 "label at %L", &code->loc, &label->where);
8932 return;
8936 if (stack)
8938 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
8939 return;
8942 /* The label is not in an enclosing block, so illegal. This was
8943 allowed in Fortran 66, so we allow it as extension. No
8944 further checks are necessary in this case. */
8945 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8946 "as the GOTO statement at %L", &label->where,
8947 &code->loc);
8948 return;
8952 /* Check whether EXPR1 has the same shape as EXPR2. */
8954 static bool
8955 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8957 mpz_t shape[GFC_MAX_DIMENSIONS];
8958 mpz_t shape2[GFC_MAX_DIMENSIONS];
8959 bool result = false;
8960 int i;
8962 /* Compare the rank. */
8963 if (expr1->rank != expr2->rank)
8964 return result;
8966 /* Compare the size of each dimension. */
8967 for (i=0; i<expr1->rank; i++)
8969 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
8970 goto ignore;
8972 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
8973 goto ignore;
8975 if (mpz_cmp (shape[i], shape2[i]))
8976 goto over;
8979 /* When either of the two expression is an assumed size array, we
8980 ignore the comparison of dimension sizes. */
8981 ignore:
8982 result = true;
8984 over:
8985 gfc_clear_shape (shape, i);
8986 gfc_clear_shape (shape2, i);
8987 return result;
8991 /* Check whether a WHERE assignment target or a WHERE mask expression
8992 has the same shape as the outmost WHERE mask expression. */
8994 static void
8995 resolve_where (gfc_code *code, gfc_expr *mask)
8997 gfc_code *cblock;
8998 gfc_code *cnext;
8999 gfc_expr *e = NULL;
9001 cblock = code->block;
9003 /* Store the first WHERE mask-expr of the WHERE statement or construct.
9004 In case of nested WHERE, only the outmost one is stored. */
9005 if (mask == NULL) /* outmost WHERE */
9006 e = cblock->expr1;
9007 else /* inner WHERE */
9008 e = mask;
9010 while (cblock)
9012 if (cblock->expr1)
9014 /* Check if the mask-expr has a consistent shape with the
9015 outmost WHERE mask-expr. */
9016 if (!resolve_where_shape (cblock->expr1, e))
9017 gfc_error ("WHERE mask at %L has inconsistent shape",
9018 &cblock->expr1->where);
9021 /* the assignment statement of a WHERE statement, or the first
9022 statement in where-body-construct of a WHERE construct */
9023 cnext = cblock->next;
9024 while (cnext)
9026 switch (cnext->op)
9028 /* WHERE assignment statement */
9029 case EXEC_ASSIGN:
9031 /* Check shape consistent for WHERE assignment target. */
9032 if (e && !resolve_where_shape (cnext->expr1, e))
9033 gfc_error ("WHERE assignment target at %L has "
9034 "inconsistent shape", &cnext->expr1->where);
9035 break;
9038 case EXEC_ASSIGN_CALL:
9039 resolve_call (cnext);
9040 if (!cnext->resolved_sym->attr.elemental)
9041 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
9042 &cnext->ext.actual->expr->where);
9043 break;
9045 /* WHERE or WHERE construct is part of a where-body-construct */
9046 case EXEC_WHERE:
9047 resolve_where (cnext, e);
9048 break;
9050 default:
9051 gfc_error ("Unsupported statement inside WHERE at %L",
9052 &cnext->loc);
9054 /* the next statement within the same where-body-construct */
9055 cnext = cnext->next;
9057 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
9058 cblock = cblock->block;
9063 /* Resolve assignment in FORALL construct.
9064 NVAR is the number of FORALL index variables, and VAR_EXPR records the
9065 FORALL index variables. */
9067 static void
9068 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
9070 int n;
9072 for (n = 0; n < nvar; n++)
9074 gfc_symbol *forall_index;
9076 forall_index = var_expr[n]->symtree->n.sym;
9078 /* Check whether the assignment target is one of the FORALL index
9079 variable. */
9080 if ((code->expr1->expr_type == EXPR_VARIABLE)
9081 && (code->expr1->symtree->n.sym == forall_index))
9082 gfc_error ("Assignment to a FORALL index variable at %L",
9083 &code->expr1->where);
9084 else
9086 /* If one of the FORALL index variables doesn't appear in the
9087 assignment variable, then there could be a many-to-one
9088 assignment. Emit a warning rather than an error because the
9089 mask could be resolving this problem. */
9090 if (!find_forall_index (code->expr1, forall_index, 0))
9091 gfc_warning (0, "The FORALL with index %qs is not used on the "
9092 "left side of the assignment at %L and so might "
9093 "cause multiple assignment to this object",
9094 var_expr[n]->symtree->name, &code->expr1->where);
9100 /* Resolve WHERE statement in FORALL construct. */
9102 static void
9103 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
9104 gfc_expr **var_expr)
9106 gfc_code *cblock;
9107 gfc_code *cnext;
9109 cblock = code->block;
9110 while (cblock)
9112 /* the assignment statement of a WHERE statement, or the first
9113 statement in where-body-construct of a WHERE construct */
9114 cnext = cblock->next;
9115 while (cnext)
9117 switch (cnext->op)
9119 /* WHERE assignment statement */
9120 case EXEC_ASSIGN:
9121 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
9122 break;
9124 /* WHERE operator assignment statement */
9125 case EXEC_ASSIGN_CALL:
9126 resolve_call (cnext);
9127 if (!cnext->resolved_sym->attr.elemental)
9128 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
9129 &cnext->ext.actual->expr->where);
9130 break;
9132 /* WHERE or WHERE construct is part of a where-body-construct */
9133 case EXEC_WHERE:
9134 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
9135 break;
9137 default:
9138 gfc_error ("Unsupported statement inside WHERE at %L",
9139 &cnext->loc);
9141 /* the next statement within the same where-body-construct */
9142 cnext = cnext->next;
9144 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
9145 cblock = cblock->block;
9150 /* Traverse the FORALL body to check whether the following errors exist:
9151 1. For assignment, check if a many-to-one assignment happens.
9152 2. For WHERE statement, check the WHERE body to see if there is any
9153 many-to-one assignment. */
9155 static void
9156 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
9158 gfc_code *c;
9160 c = code->block->next;
9161 while (c)
9163 switch (c->op)
9165 case EXEC_ASSIGN:
9166 case EXEC_POINTER_ASSIGN:
9167 gfc_resolve_assign_in_forall (c, nvar, var_expr);
9168 break;
9170 case EXEC_ASSIGN_CALL:
9171 resolve_call (c);
9172 break;
9174 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
9175 there is no need to handle it here. */
9176 case EXEC_FORALL:
9177 break;
9178 case EXEC_WHERE:
9179 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
9180 break;
9181 default:
9182 break;
9184 /* The next statement in the FORALL body. */
9185 c = c->next;
9190 /* Counts the number of iterators needed inside a forall construct, including
9191 nested forall constructs. This is used to allocate the needed memory
9192 in gfc_resolve_forall. */
9194 static int
9195 gfc_count_forall_iterators (gfc_code *code)
9197 int max_iters, sub_iters, current_iters;
9198 gfc_forall_iterator *fa;
9200 gcc_assert(code->op == EXEC_FORALL);
9201 max_iters = 0;
9202 current_iters = 0;
9204 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9205 current_iters ++;
9207 code = code->block->next;
9209 while (code)
9211 if (code->op == EXEC_FORALL)
9213 sub_iters = gfc_count_forall_iterators (code);
9214 if (sub_iters > max_iters)
9215 max_iters = sub_iters;
9217 code = code->next;
9220 return current_iters + max_iters;
9224 /* Given a FORALL construct, first resolve the FORALL iterator, then call
9225 gfc_resolve_forall_body to resolve the FORALL body. */
9227 static void
9228 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
9230 static gfc_expr **var_expr;
9231 static int total_var = 0;
9232 static int nvar = 0;
9233 int old_nvar, tmp;
9234 gfc_forall_iterator *fa;
9235 int i;
9237 old_nvar = nvar;
9239 /* Start to resolve a FORALL construct */
9240 if (forall_save == 0)
9242 /* Count the total number of FORALL index in the nested FORALL
9243 construct in order to allocate the VAR_EXPR with proper size. */
9244 total_var = gfc_count_forall_iterators (code);
9246 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
9247 var_expr = XCNEWVEC (gfc_expr *, total_var);
9250 /* The information about FORALL iterator, including FORALL index start, end
9251 and stride. The FORALL index can not appear in start, end or stride. */
9252 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9254 /* Check if any outer FORALL index name is the same as the current
9255 one. */
9256 for (i = 0; i < nvar; i++)
9258 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
9260 gfc_error ("An outer FORALL construct already has an index "
9261 "with this name %L", &fa->var->where);
9265 /* Record the current FORALL index. */
9266 var_expr[nvar] = gfc_copy_expr (fa->var);
9268 nvar++;
9270 /* No memory leak. */
9271 gcc_assert (nvar <= total_var);
9274 /* Resolve the FORALL body. */
9275 gfc_resolve_forall_body (code, nvar, var_expr);
9277 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
9278 gfc_resolve_blocks (code->block, ns);
9280 tmp = nvar;
9281 nvar = old_nvar;
9282 /* Free only the VAR_EXPRs allocated in this frame. */
9283 for (i = nvar; i < tmp; i++)
9284 gfc_free_expr (var_expr[i]);
9286 if (nvar == 0)
9288 /* We are in the outermost FORALL construct. */
9289 gcc_assert (forall_save == 0);
9291 /* VAR_EXPR is not needed any more. */
9292 free (var_expr);
9293 total_var = 0;
9298 /* Resolve a BLOCK construct statement. */
9300 static void
9301 resolve_block_construct (gfc_code* code)
9303 /* Resolve the BLOCK's namespace. */
9304 gfc_resolve (code->ext.block.ns);
9306 /* For an ASSOCIATE block, the associations (and their targets) are already
9307 resolved during resolve_symbol. */
9311 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
9312 DO code nodes. */
9314 void
9315 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
9317 bool t;
9319 for (; b; b = b->block)
9321 t = gfc_resolve_expr (b->expr1);
9322 if (!gfc_resolve_expr (b->expr2))
9323 t = false;
9325 switch (b->op)
9327 case EXEC_IF:
9328 if (t && b->expr1 != NULL
9329 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
9330 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9331 &b->expr1->where);
9332 break;
9334 case EXEC_WHERE:
9335 if (t
9336 && b->expr1 != NULL
9337 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
9338 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
9339 &b->expr1->where);
9340 break;
9342 case EXEC_GOTO:
9343 resolve_branch (b->label1, b);
9344 break;
9346 case EXEC_BLOCK:
9347 resolve_block_construct (b);
9348 break;
9350 case EXEC_SELECT:
9351 case EXEC_SELECT_TYPE:
9352 case EXEC_FORALL:
9353 case EXEC_DO:
9354 case EXEC_DO_WHILE:
9355 case EXEC_DO_CONCURRENT:
9356 case EXEC_CRITICAL:
9357 case EXEC_READ:
9358 case EXEC_WRITE:
9359 case EXEC_IOLENGTH:
9360 case EXEC_WAIT:
9361 break;
9363 case EXEC_OACC_PARALLEL_LOOP:
9364 case EXEC_OACC_PARALLEL:
9365 case EXEC_OACC_KERNELS_LOOP:
9366 case EXEC_OACC_KERNELS:
9367 case EXEC_OACC_DATA:
9368 case EXEC_OACC_HOST_DATA:
9369 case EXEC_OACC_LOOP:
9370 case EXEC_OACC_UPDATE:
9371 case EXEC_OACC_WAIT:
9372 case EXEC_OACC_CACHE:
9373 case EXEC_OACC_ENTER_DATA:
9374 case EXEC_OACC_EXIT_DATA:
9375 case EXEC_OACC_ATOMIC:
9376 case EXEC_OACC_ROUTINE:
9377 case EXEC_OMP_ATOMIC:
9378 case EXEC_OMP_CRITICAL:
9379 case EXEC_OMP_DISTRIBUTE:
9380 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
9381 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
9382 case EXEC_OMP_DISTRIBUTE_SIMD:
9383 case EXEC_OMP_DO:
9384 case EXEC_OMP_DO_SIMD:
9385 case EXEC_OMP_MASTER:
9386 case EXEC_OMP_ORDERED:
9387 case EXEC_OMP_PARALLEL:
9388 case EXEC_OMP_PARALLEL_DO:
9389 case EXEC_OMP_PARALLEL_DO_SIMD:
9390 case EXEC_OMP_PARALLEL_SECTIONS:
9391 case EXEC_OMP_PARALLEL_WORKSHARE:
9392 case EXEC_OMP_SECTIONS:
9393 case EXEC_OMP_SIMD:
9394 case EXEC_OMP_SINGLE:
9395 case EXEC_OMP_TARGET:
9396 case EXEC_OMP_TARGET_DATA:
9397 case EXEC_OMP_TARGET_TEAMS:
9398 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
9399 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
9400 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9401 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
9402 case EXEC_OMP_TARGET_UPDATE:
9403 case EXEC_OMP_TASK:
9404 case EXEC_OMP_TASKGROUP:
9405 case EXEC_OMP_TASKWAIT:
9406 case EXEC_OMP_TASKYIELD:
9407 case EXEC_OMP_TEAMS:
9408 case EXEC_OMP_TEAMS_DISTRIBUTE:
9409 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
9410 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9411 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
9412 case EXEC_OMP_WORKSHARE:
9413 break;
9415 default:
9416 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
9419 gfc_resolve_code (b->next, ns);
9424 /* Does everything to resolve an ordinary assignment. Returns true
9425 if this is an interface assignment. */
9426 static bool
9427 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9429 bool rval = false;
9430 gfc_expr *lhs;
9431 gfc_expr *rhs;
9432 int llen = 0;
9433 int rlen = 0;
9434 int n;
9435 gfc_ref *ref;
9436 symbol_attribute attr;
9438 if (gfc_extend_assign (code, ns))
9440 gfc_expr** rhsptr;
9442 if (code->op == EXEC_ASSIGN_CALL)
9444 lhs = code->ext.actual->expr;
9445 rhsptr = &code->ext.actual->next->expr;
9447 else
9449 gfc_actual_arglist* args;
9450 gfc_typebound_proc* tbp;
9452 gcc_assert (code->op == EXEC_COMPCALL);
9454 args = code->expr1->value.compcall.actual;
9455 lhs = args->expr;
9456 rhsptr = &args->next->expr;
9458 tbp = code->expr1->value.compcall.tbp;
9459 gcc_assert (!tbp->is_generic);
9462 /* Make a temporary rhs when there is a default initializer
9463 and rhs is the same symbol as the lhs. */
9464 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9465 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9466 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9467 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9468 *rhsptr = gfc_get_parentheses (*rhsptr);
9470 return true;
9473 lhs = code->expr1;
9474 rhs = code->expr2;
9476 if (rhs->is_boz
9477 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9478 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9479 &code->loc))
9480 return false;
9482 /* Handle the case of a BOZ literal on the RHS. */
9483 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9485 int rc;
9486 if (warn_surprising)
9487 gfc_warning (OPT_Wsurprising,
9488 "BOZ literal at %L is bitwise transferred "
9489 "non-integer symbol %qs", &code->loc,
9490 lhs->symtree->n.sym->name);
9492 if (!gfc_convert_boz (rhs, &lhs->ts))
9493 return false;
9494 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9496 if (rc == ARITH_UNDERFLOW)
9497 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9498 ". This check can be disabled with the option "
9499 "%<-fno-range-check%>", &rhs->where);
9500 else if (rc == ARITH_OVERFLOW)
9501 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9502 ". This check can be disabled with the option "
9503 "%<-fno-range-check%>", &rhs->where);
9504 else if (rc == ARITH_NAN)
9505 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9506 ". This check can be disabled with the option "
9507 "%<-fno-range-check%>", &rhs->where);
9508 return false;
9512 if (lhs->ts.type == BT_CHARACTER
9513 && warn_character_truncation)
9515 if (lhs->ts.u.cl != NULL
9516 && lhs->ts.u.cl->length != NULL
9517 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9518 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9520 if (rhs->expr_type == EXPR_CONSTANT)
9521 rlen = rhs->value.character.length;
9523 else if (rhs->ts.u.cl != NULL
9524 && rhs->ts.u.cl->length != NULL
9525 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9526 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9528 if (rlen && llen && rlen > llen)
9529 gfc_warning_now (OPT_Wcharacter_truncation,
9530 "CHARACTER expression will be truncated "
9531 "in assignment (%d/%d) at %L",
9532 llen, rlen, &code->loc);
9535 /* Ensure that a vector index expression for the lvalue is evaluated
9536 to a temporary if the lvalue symbol is referenced in it. */
9537 if (lhs->rank)
9539 for (ref = lhs->ref; ref; ref= ref->next)
9540 if (ref->type == REF_ARRAY)
9542 for (n = 0; n < ref->u.ar.dimen; n++)
9543 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9544 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9545 ref->u.ar.start[n]))
9546 ref->u.ar.start[n]
9547 = gfc_get_parentheses (ref->u.ar.start[n]);
9551 if (gfc_pure (NULL))
9553 if (lhs->ts.type == BT_DERIVED
9554 && lhs->expr_type == EXPR_VARIABLE
9555 && lhs->ts.u.derived->attr.pointer_comp
9556 && rhs->expr_type == EXPR_VARIABLE
9557 && (gfc_impure_variable (rhs->symtree->n.sym)
9558 || gfc_is_coindexed (rhs)))
9560 /* F2008, C1283. */
9561 if (gfc_is_coindexed (rhs))
9562 gfc_error ("Coindexed expression at %L is assigned to "
9563 "a derived type variable with a POINTER "
9564 "component in a PURE procedure",
9565 &rhs->where);
9566 else
9567 gfc_error ("The impure variable at %L is assigned to "
9568 "a derived type variable with a POINTER "
9569 "component in a PURE procedure (12.6)",
9570 &rhs->where);
9571 return rval;
9574 /* Fortran 2008, C1283. */
9575 if (gfc_is_coindexed (lhs))
9577 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9578 "procedure", &rhs->where);
9579 return rval;
9583 if (gfc_implicit_pure (NULL))
9585 if (lhs->expr_type == EXPR_VARIABLE
9586 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9587 && lhs->symtree->n.sym->ns != gfc_current_ns)
9588 gfc_unset_implicit_pure (NULL);
9590 if (lhs->ts.type == BT_DERIVED
9591 && lhs->expr_type == EXPR_VARIABLE
9592 && lhs->ts.u.derived->attr.pointer_comp
9593 && rhs->expr_type == EXPR_VARIABLE
9594 && (gfc_impure_variable (rhs->symtree->n.sym)
9595 || gfc_is_coindexed (rhs)))
9596 gfc_unset_implicit_pure (NULL);
9598 /* Fortran 2008, C1283. */
9599 if (gfc_is_coindexed (lhs))
9600 gfc_unset_implicit_pure (NULL);
9603 /* F2008, 7.2.1.2. */
9604 attr = gfc_expr_attr (lhs);
9605 if (lhs->ts.type == BT_CLASS && attr.allocatable)
9607 if (attr.codimension)
9609 gfc_error ("Assignment to polymorphic coarray at %L is not "
9610 "permitted", &lhs->where);
9611 return false;
9613 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
9614 "polymorphic variable at %L", &lhs->where))
9615 return false;
9616 if (!flag_realloc_lhs)
9618 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9619 "requires %<-frealloc-lhs%>", &lhs->where);
9620 return false;
9622 /* See PR 43366. */
9623 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9624 "is not yet supported", &lhs->where);
9625 return false;
9627 else if (lhs->ts.type == BT_CLASS)
9629 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
9630 "assignment at %L - check that there is a matching specific "
9631 "subroutine for '=' operator", &lhs->where);
9632 return false;
9635 bool lhs_coindexed = gfc_is_coindexed (lhs);
9637 /* F2008, Section 7.2.1.2. */
9638 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
9640 gfc_error ("Coindexed variable must not have an allocatable ultimate "
9641 "component in assignment at %L", &lhs->where);
9642 return false;
9645 gfc_check_assign (lhs, rhs, 1);
9647 /* Assign the 'data' of a class object to a derived type. */
9648 if (lhs->ts.type == BT_DERIVED
9649 && rhs->ts.type == BT_CLASS)
9650 gfc_add_data_component (rhs);
9652 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
9653 Additionally, insert this code when the RHS is a CAF as we then use the
9654 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
9655 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
9656 noncoindexed array and the RHS is a coindexed scalar, use the normal code
9657 path. */
9658 if (flag_coarray == GFC_FCOARRAY_LIB
9659 && (lhs_coindexed
9660 || (code->expr2->expr_type == EXPR_FUNCTION
9661 && code->expr2->value.function.isym
9662 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
9663 && (code->expr1->rank == 0 || code->expr2->rank != 0)
9664 && !gfc_expr_attr (rhs).allocatable
9665 && !gfc_has_vector_subscript (rhs))))
9667 if (code->expr2->expr_type == EXPR_FUNCTION
9668 && code->expr2->value.function.isym
9669 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
9670 remove_caf_get_intrinsic (code->expr2);
9671 code->op = EXEC_CALL;
9672 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
9673 code->resolved_sym = code->symtree->n.sym;
9674 code->resolved_sym->attr.flavor = FL_PROCEDURE;
9675 code->resolved_sym->attr.intrinsic = 1;
9676 code->resolved_sym->attr.subroutine = 1;
9677 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
9678 gfc_commit_symbol (code->resolved_sym);
9679 code->ext.actual = gfc_get_actual_arglist ();
9680 code->ext.actual->expr = lhs;
9681 code->ext.actual->next = gfc_get_actual_arglist ();
9682 code->ext.actual->next->expr = rhs;
9683 code->expr1 = NULL;
9684 code->expr2 = NULL;
9687 return false;
9691 /* Add a component reference onto an expression. */
9693 static void
9694 add_comp_ref (gfc_expr *e, gfc_component *c)
9696 gfc_ref **ref;
9697 ref = &(e->ref);
9698 while (*ref)
9699 ref = &((*ref)->next);
9700 *ref = gfc_get_ref ();
9701 (*ref)->type = REF_COMPONENT;
9702 (*ref)->u.c.sym = e->ts.u.derived;
9703 (*ref)->u.c.component = c;
9704 e->ts = c->ts;
9706 /* Add a full array ref, as necessary. */
9707 if (c->as)
9709 gfc_add_full_array_ref (e, c->as);
9710 e->rank = c->as->rank;
9715 /* Build an assignment. Keep the argument 'op' for future use, so that
9716 pointer assignments can be made. */
9718 static gfc_code *
9719 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9720 gfc_component *comp1, gfc_component *comp2, locus loc)
9722 gfc_code *this_code;
9724 this_code = gfc_get_code (op);
9725 this_code->next = NULL;
9726 this_code->expr1 = gfc_copy_expr (expr1);
9727 this_code->expr2 = gfc_copy_expr (expr2);
9728 this_code->loc = loc;
9729 if (comp1 && comp2)
9731 add_comp_ref (this_code->expr1, comp1);
9732 add_comp_ref (this_code->expr2, comp2);
9735 return this_code;
9739 /* Makes a temporary variable expression based on the characteristics of
9740 a given variable expression. */
9742 static gfc_expr*
9743 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9745 static int serial = 0;
9746 char name[GFC_MAX_SYMBOL_LEN];
9747 gfc_symtree *tmp;
9748 gfc_array_spec *as;
9749 gfc_array_ref *aref;
9750 gfc_ref *ref;
9752 sprintf (name, GFC_PREFIX("DA%d"), serial++);
9753 gfc_get_sym_tree (name, ns, &tmp, false);
9754 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9756 as = NULL;
9757 ref = NULL;
9758 aref = NULL;
9760 /* Obtain the arrayspec for the temporary. */
9761 if (e->rank && e->expr_type != EXPR_ARRAY
9762 && e->expr_type != EXPR_FUNCTION
9763 && e->expr_type != EXPR_OP)
9765 aref = gfc_find_array_ref (e);
9766 if (e->expr_type == EXPR_VARIABLE
9767 && e->symtree->n.sym->as == aref->as)
9768 as = aref->as;
9769 else
9771 for (ref = e->ref; ref; ref = ref->next)
9772 if (ref->type == REF_COMPONENT
9773 && ref->u.c.component->as == aref->as)
9775 as = aref->as;
9776 break;
9781 /* Add the attributes and the arrayspec to the temporary. */
9782 tmp->n.sym->attr = gfc_expr_attr (e);
9783 tmp->n.sym->attr.function = 0;
9784 tmp->n.sym->attr.result = 0;
9785 tmp->n.sym->attr.flavor = FL_VARIABLE;
9787 if (as)
9789 tmp->n.sym->as = gfc_copy_array_spec (as);
9790 if (!ref)
9791 ref = e->ref;
9792 if (as->type == AS_DEFERRED)
9793 tmp->n.sym->attr.allocatable = 1;
9795 else if (e->rank && (e->expr_type == EXPR_ARRAY
9796 || e->expr_type == EXPR_FUNCTION
9797 || e->expr_type == EXPR_OP))
9799 tmp->n.sym->as = gfc_get_array_spec ();
9800 tmp->n.sym->as->type = AS_DEFERRED;
9801 tmp->n.sym->as->rank = e->rank;
9802 tmp->n.sym->attr.allocatable = 1;
9803 tmp->n.sym->attr.dimension = 1;
9805 else
9806 tmp->n.sym->attr.dimension = 0;
9808 gfc_set_sym_referenced (tmp->n.sym);
9809 gfc_commit_symbol (tmp->n.sym);
9810 e = gfc_lval_expr_from_sym (tmp->n.sym);
9812 /* Should the lhs be a section, use its array ref for the
9813 temporary expression. */
9814 if (aref && aref->type != AR_FULL)
9816 gfc_free_ref_list (e->ref);
9817 e->ref = gfc_copy_ref (ref);
9819 return e;
9823 /* Add one line of code to the code chain, making sure that 'head' and
9824 'tail' are appropriately updated. */
9826 static void
9827 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9829 gcc_assert (this_code);
9830 if (*head == NULL)
9831 *head = *tail = *this_code;
9832 else
9833 *tail = gfc_append_code (*tail, *this_code);
9834 *this_code = NULL;
9838 /* Counts the potential number of part array references that would
9839 result from resolution of typebound defined assignments. */
9841 static int
9842 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9844 gfc_component *c;
9845 int c_depth = 0, t_depth;
9847 for (c= derived->components; c; c = c->next)
9849 if ((c->ts.type != BT_DERIVED
9850 || c->attr.pointer
9851 || c->attr.allocatable
9852 || c->attr.proc_pointer_comp
9853 || c->attr.class_pointer
9854 || c->attr.proc_pointer)
9855 && !c->attr.defined_assign_comp)
9856 continue;
9858 if (c->as && c_depth == 0)
9859 c_depth = 1;
9861 if (c->ts.u.derived->attr.defined_assign_comp)
9862 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9863 c->as ? 1 : 0);
9864 else
9865 t_depth = 0;
9867 c_depth = t_depth > c_depth ? t_depth : c_depth;
9869 return depth + c_depth;
9873 /* Implement 7.2.1.3 of the F08 standard:
9874 "An intrinsic assignment where the variable is of derived type is
9875 performed as if each component of the variable were assigned from the
9876 corresponding component of expr using pointer assignment (7.2.2) for
9877 each pointer component, defined assignment for each nonpointer
9878 nonallocatable component of a type that has a type-bound defined
9879 assignment consistent with the component, intrinsic assignment for
9880 each other nonpointer nonallocatable component, ..."
9882 The pointer assignments are taken care of by the intrinsic
9883 assignment of the structure itself. This function recursively adds
9884 defined assignments where required. The recursion is accomplished
9885 by calling gfc_resolve_code.
9887 When the lhs in a defined assignment has intent INOUT, we need a
9888 temporary for the lhs. In pseudo-code:
9890 ! Only call function lhs once.
9891 if (lhs is not a constant or an variable)
9892 temp_x = expr2
9893 expr2 => temp_x
9894 ! Do the intrinsic assignment
9895 expr1 = expr2
9896 ! Now do the defined assignments
9897 do over components with typebound defined assignment [%cmp]
9898 #if one component's assignment procedure is INOUT
9899 t1 = expr1
9900 #if expr2 non-variable
9901 temp_x = expr2
9902 expr2 => temp_x
9903 # endif
9904 expr1 = expr2
9905 # for each cmp
9906 t1%cmp {defined=} expr2%cmp
9907 expr1%cmp = t1%cmp
9908 #else
9909 expr1 = expr2
9911 # for each cmp
9912 expr1%cmp {defined=} expr2%cmp
9913 #endif
9916 /* The temporary assignments have to be put on top of the additional
9917 code to avoid the result being changed by the intrinsic assignment.
9919 static int component_assignment_level = 0;
9920 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
9922 static void
9923 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
9925 gfc_component *comp1, *comp2;
9926 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
9927 gfc_expr *t1;
9928 int error_count, depth;
9930 gfc_get_errors (NULL, &error_count);
9932 /* Filter out continuing processing after an error. */
9933 if (error_count
9934 || (*code)->expr1->ts.type != BT_DERIVED
9935 || (*code)->expr2->ts.type != BT_DERIVED)
9936 return;
9938 /* TODO: Handle more than one part array reference in assignments. */
9939 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
9940 (*code)->expr1->rank ? 1 : 0);
9941 if (depth > 1)
9943 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
9944 "done because multiple part array references would "
9945 "occur in intermediate expressions.", &(*code)->loc);
9946 return;
9949 component_assignment_level++;
9951 /* Create a temporary so that functions get called only once. */
9952 if ((*code)->expr2->expr_type != EXPR_VARIABLE
9953 && (*code)->expr2->expr_type != EXPR_CONSTANT)
9955 gfc_expr *tmp_expr;
9957 /* Assign the rhs to the temporary. */
9958 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
9959 this_code = build_assignment (EXEC_ASSIGN,
9960 tmp_expr, (*code)->expr2,
9961 NULL, NULL, (*code)->loc);
9962 /* Add the code and substitute the rhs expression. */
9963 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
9964 gfc_free_expr ((*code)->expr2);
9965 (*code)->expr2 = tmp_expr;
9968 /* Do the intrinsic assignment. This is not needed if the lhs is one
9969 of the temporaries generated here, since the intrinsic assignment
9970 to the final result already does this. */
9971 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
9973 this_code = build_assignment (EXEC_ASSIGN,
9974 (*code)->expr1, (*code)->expr2,
9975 NULL, NULL, (*code)->loc);
9976 add_code_to_chain (&this_code, &head, &tail);
9979 comp1 = (*code)->expr1->ts.u.derived->components;
9980 comp2 = (*code)->expr2->ts.u.derived->components;
9982 t1 = NULL;
9983 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
9985 bool inout = false;
9987 /* The intrinsic assignment does the right thing for pointers
9988 of all kinds and allocatable components. */
9989 if (comp1->ts.type != BT_DERIVED
9990 || comp1->attr.pointer
9991 || comp1->attr.allocatable
9992 || comp1->attr.proc_pointer_comp
9993 || comp1->attr.class_pointer
9994 || comp1->attr.proc_pointer)
9995 continue;
9997 /* Make an assigment for this component. */
9998 this_code = build_assignment (EXEC_ASSIGN,
9999 (*code)->expr1, (*code)->expr2,
10000 comp1, comp2, (*code)->loc);
10002 /* Convert the assignment if there is a defined assignment for
10003 this type. Otherwise, using the call from gfc_resolve_code,
10004 recurse into its components. */
10005 gfc_resolve_code (this_code, ns);
10007 if (this_code->op == EXEC_ASSIGN_CALL)
10009 gfc_formal_arglist *dummy_args;
10010 gfc_symbol *rsym;
10011 /* Check that there is a typebound defined assignment. If not,
10012 then this must be a module defined assignment. We cannot
10013 use the defined_assign_comp attribute here because it must
10014 be this derived type that has the defined assignment and not
10015 a parent type. */
10016 if (!(comp1->ts.u.derived->f2k_derived
10017 && comp1->ts.u.derived->f2k_derived
10018 ->tb_op[INTRINSIC_ASSIGN]))
10020 gfc_free_statements (this_code);
10021 this_code = NULL;
10022 continue;
10025 /* If the first argument of the subroutine has intent INOUT
10026 a temporary must be generated and used instead. */
10027 rsym = this_code->resolved_sym;
10028 dummy_args = gfc_sym_get_dummy_args (rsym);
10029 if (dummy_args
10030 && dummy_args->sym->attr.intent == INTENT_INOUT)
10032 gfc_code *temp_code;
10033 inout = true;
10035 /* Build the temporary required for the assignment and put
10036 it at the head of the generated code. */
10037 if (!t1)
10039 t1 = get_temp_from_expr ((*code)->expr1, ns);
10040 temp_code = build_assignment (EXEC_ASSIGN,
10041 t1, (*code)->expr1,
10042 NULL, NULL, (*code)->loc);
10044 /* For allocatable LHS, check whether it is allocated. Note
10045 that allocatable components with defined assignment are
10046 not yet support. See PR 57696. */
10047 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
10049 gfc_code *block;
10050 gfc_expr *e =
10051 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10052 block = gfc_get_code (EXEC_IF);
10053 block->block = gfc_get_code (EXEC_IF);
10054 block->block->expr1
10055 = gfc_build_intrinsic_call (ns,
10056 GFC_ISYM_ALLOCATED, "allocated",
10057 (*code)->loc, 1, e);
10058 block->block->next = temp_code;
10059 temp_code = block;
10061 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
10064 /* Replace the first actual arg with the component of the
10065 temporary. */
10066 gfc_free_expr (this_code->ext.actual->expr);
10067 this_code->ext.actual->expr = gfc_copy_expr (t1);
10068 add_comp_ref (this_code->ext.actual->expr, comp1);
10070 /* If the LHS variable is allocatable and wasn't allocated and
10071 the temporary is allocatable, pointer assign the address of
10072 the freshly allocated LHS to the temporary. */
10073 if ((*code)->expr1->symtree->n.sym->attr.allocatable
10074 && gfc_expr_attr ((*code)->expr1).allocatable)
10076 gfc_code *block;
10077 gfc_expr *cond;
10079 cond = gfc_get_expr ();
10080 cond->ts.type = BT_LOGICAL;
10081 cond->ts.kind = gfc_default_logical_kind;
10082 cond->expr_type = EXPR_OP;
10083 cond->where = (*code)->loc;
10084 cond->value.op.op = INTRINSIC_NOT;
10085 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
10086 GFC_ISYM_ALLOCATED, "allocated",
10087 (*code)->loc, 1, gfc_copy_expr (t1));
10088 block = gfc_get_code (EXEC_IF);
10089 block->block = gfc_get_code (EXEC_IF);
10090 block->block->expr1 = cond;
10091 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10092 t1, (*code)->expr1,
10093 NULL, NULL, (*code)->loc);
10094 add_code_to_chain (&block, &head, &tail);
10098 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
10100 /* Don't add intrinsic assignments since they are already
10101 effected by the intrinsic assignment of the structure. */
10102 gfc_free_statements (this_code);
10103 this_code = NULL;
10104 continue;
10107 add_code_to_chain (&this_code, &head, &tail);
10109 if (t1 && inout)
10111 /* Transfer the value to the final result. */
10112 this_code = build_assignment (EXEC_ASSIGN,
10113 (*code)->expr1, t1,
10114 comp1, comp2, (*code)->loc);
10115 add_code_to_chain (&this_code, &head, &tail);
10119 /* Put the temporary assignments at the top of the generated code. */
10120 if (tmp_head && component_assignment_level == 1)
10122 gfc_append_code (tmp_head, head);
10123 head = tmp_head;
10124 tmp_head = tmp_tail = NULL;
10127 // If we did a pointer assignment - thus, we need to ensure that the LHS is
10128 // not accidentally deallocated. Hence, nullify t1.
10129 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
10130 && gfc_expr_attr ((*code)->expr1).allocatable)
10132 gfc_code *block;
10133 gfc_expr *cond;
10134 gfc_expr *e;
10136 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10137 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
10138 (*code)->loc, 2, gfc_copy_expr (t1), e);
10139 block = gfc_get_code (EXEC_IF);
10140 block->block = gfc_get_code (EXEC_IF);
10141 block->block->expr1 = cond;
10142 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10143 t1, gfc_get_null_expr (&(*code)->loc),
10144 NULL, NULL, (*code)->loc);
10145 gfc_append_code (tail, block);
10146 tail = block;
10149 /* Now attach the remaining code chain to the input code. Step on
10150 to the end of the new code since resolution is complete. */
10151 gcc_assert ((*code)->op == EXEC_ASSIGN);
10152 tail->next = (*code)->next;
10153 /* Overwrite 'code' because this would place the intrinsic assignment
10154 before the temporary for the lhs is created. */
10155 gfc_free_expr ((*code)->expr1);
10156 gfc_free_expr ((*code)->expr2);
10157 **code = *head;
10158 if (head != tail)
10159 free (head);
10160 *code = tail;
10162 component_assignment_level--;
10166 /* F2008: Pointer function assignments are of the form:
10167 ptr_fcn (args) = expr
10168 This function breaks these assignments into two statements:
10169 temporary_pointer => ptr_fcn(args)
10170 temporary_pointer = expr */
10172 static bool
10173 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
10175 gfc_expr *tmp_ptr_expr;
10176 gfc_code *this_code;
10177 gfc_component *comp;
10178 gfc_symbol *s;
10180 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
10181 return false;
10183 /* Even if standard does not support this feature, continue to build
10184 the two statements to avoid upsetting frontend_passes.c. */
10185 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
10186 "%L", &(*code)->loc);
10188 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
10190 if (comp)
10191 s = comp->ts.interface;
10192 else
10193 s = (*code)->expr1->symtree->n.sym;
10195 if (s == NULL || !s->result->attr.pointer)
10197 gfc_error ("The function result on the lhs of the assignment at "
10198 "%L must have the pointer attribute.",
10199 &(*code)->expr1->where);
10200 (*code)->op = EXEC_NOP;
10201 return false;
10204 tmp_ptr_expr = get_temp_from_expr ((*code)->expr2, ns);
10206 /* get_temp_from_expression is set up for ordinary assignments. To that
10207 end, where array bounds are not known, arrays are made allocatable.
10208 Change the temporary to a pointer here. */
10209 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
10210 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
10211 tmp_ptr_expr->where = (*code)->loc;
10213 this_code = build_assignment (EXEC_ASSIGN,
10214 tmp_ptr_expr, (*code)->expr2,
10215 NULL, NULL, (*code)->loc);
10216 this_code->next = (*code)->next;
10217 (*code)->next = this_code;
10218 (*code)->op = EXEC_POINTER_ASSIGN;
10219 (*code)->expr2 = (*code)->expr1;
10220 (*code)->expr1 = tmp_ptr_expr;
10222 return true;
10226 /* Deferred character length assignments from an operator expression
10227 require a temporary because the character length of the lhs can
10228 change in the course of the assignment. */
10230 static bool
10231 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
10233 gfc_expr *tmp_expr;
10234 gfc_code *this_code;
10236 if (!((*code)->expr1->ts.type == BT_CHARACTER
10237 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
10238 && (*code)->expr2->expr_type == EXPR_OP))
10239 return false;
10241 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
10242 return false;
10244 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
10245 tmp_expr->where = (*code)->loc;
10247 /* A new charlen is required to ensure that the variable string
10248 length is different to that of the original lhs. */
10249 tmp_expr->ts.u.cl = gfc_get_charlen();
10250 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
10251 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
10252 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
10254 tmp_expr->symtree->n.sym->ts.deferred = 1;
10256 this_code = build_assignment (EXEC_ASSIGN,
10257 (*code)->expr1,
10258 gfc_copy_expr (tmp_expr),
10259 NULL, NULL, (*code)->loc);
10261 (*code)->expr1 = tmp_expr;
10263 this_code->next = (*code)->next;
10264 (*code)->next = this_code;
10266 return true;
10270 /* Given a block of code, recursively resolve everything pointed to by this
10271 code block. */
10273 void
10274 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
10276 int omp_workshare_save;
10277 int forall_save, do_concurrent_save;
10278 code_stack frame;
10279 bool t;
10281 frame.prev = cs_base;
10282 frame.head = code;
10283 cs_base = &frame;
10285 find_reachable_labels (code);
10287 for (; code; code = code->next)
10289 frame.current = code;
10290 forall_save = forall_flag;
10291 do_concurrent_save = gfc_do_concurrent_flag;
10293 if (code->op == EXEC_FORALL)
10295 forall_flag = 1;
10296 gfc_resolve_forall (code, ns, forall_save);
10297 forall_flag = 2;
10299 else if (code->block)
10301 omp_workshare_save = -1;
10302 switch (code->op)
10304 case EXEC_OACC_PARALLEL_LOOP:
10305 case EXEC_OACC_PARALLEL:
10306 case EXEC_OACC_KERNELS_LOOP:
10307 case EXEC_OACC_KERNELS:
10308 case EXEC_OACC_DATA:
10309 case EXEC_OACC_HOST_DATA:
10310 case EXEC_OACC_LOOP:
10311 gfc_resolve_oacc_blocks (code, ns);
10312 break;
10313 case EXEC_OMP_PARALLEL_WORKSHARE:
10314 omp_workshare_save = omp_workshare_flag;
10315 omp_workshare_flag = 1;
10316 gfc_resolve_omp_parallel_blocks (code, ns);
10317 break;
10318 case EXEC_OMP_PARALLEL:
10319 case EXEC_OMP_PARALLEL_DO:
10320 case EXEC_OMP_PARALLEL_DO_SIMD:
10321 case EXEC_OMP_PARALLEL_SECTIONS:
10322 case EXEC_OMP_TARGET_TEAMS:
10323 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10324 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10325 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10326 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10327 case EXEC_OMP_TASK:
10328 case EXEC_OMP_TEAMS:
10329 case EXEC_OMP_TEAMS_DISTRIBUTE:
10330 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10331 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10332 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10333 omp_workshare_save = omp_workshare_flag;
10334 omp_workshare_flag = 0;
10335 gfc_resolve_omp_parallel_blocks (code, ns);
10336 break;
10337 case EXEC_OMP_DISTRIBUTE:
10338 case EXEC_OMP_DISTRIBUTE_SIMD:
10339 case EXEC_OMP_DO:
10340 case EXEC_OMP_DO_SIMD:
10341 case EXEC_OMP_SIMD:
10342 gfc_resolve_omp_do_blocks (code, ns);
10343 break;
10344 case EXEC_SELECT_TYPE:
10345 /* Blocks are handled in resolve_select_type because we have
10346 to transform the SELECT TYPE into ASSOCIATE first. */
10347 break;
10348 case EXEC_DO_CONCURRENT:
10349 gfc_do_concurrent_flag = 1;
10350 gfc_resolve_blocks (code->block, ns);
10351 gfc_do_concurrent_flag = 2;
10352 break;
10353 case EXEC_OMP_WORKSHARE:
10354 omp_workshare_save = omp_workshare_flag;
10355 omp_workshare_flag = 1;
10356 /* FALL THROUGH */
10357 default:
10358 gfc_resolve_blocks (code->block, ns);
10359 break;
10362 if (omp_workshare_save != -1)
10363 omp_workshare_flag = omp_workshare_save;
10365 start:
10366 t = true;
10367 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
10368 t = gfc_resolve_expr (code->expr1);
10369 forall_flag = forall_save;
10370 gfc_do_concurrent_flag = do_concurrent_save;
10372 if (!gfc_resolve_expr (code->expr2))
10373 t = false;
10375 if (code->op == EXEC_ALLOCATE
10376 && !gfc_resolve_expr (code->expr3))
10377 t = false;
10379 switch (code->op)
10381 case EXEC_NOP:
10382 case EXEC_END_BLOCK:
10383 case EXEC_END_NESTED_BLOCK:
10384 case EXEC_CYCLE:
10385 case EXEC_PAUSE:
10386 case EXEC_STOP:
10387 case EXEC_ERROR_STOP:
10388 case EXEC_EXIT:
10389 case EXEC_CONTINUE:
10390 case EXEC_DT_END:
10391 case EXEC_ASSIGN_CALL:
10392 break;
10394 case EXEC_CRITICAL:
10395 resolve_critical (code);
10396 break;
10398 case EXEC_SYNC_ALL:
10399 case EXEC_SYNC_IMAGES:
10400 case EXEC_SYNC_MEMORY:
10401 resolve_sync (code);
10402 break;
10404 case EXEC_LOCK:
10405 case EXEC_UNLOCK:
10406 resolve_lock_unlock (code);
10407 break;
10409 case EXEC_ENTRY:
10410 /* Keep track of which entry we are up to. */
10411 current_entry_id = code->ext.entry->id;
10412 break;
10414 case EXEC_WHERE:
10415 resolve_where (code, NULL);
10416 break;
10418 case EXEC_GOTO:
10419 if (code->expr1 != NULL)
10421 if (code->expr1->ts.type != BT_INTEGER)
10422 gfc_error ("ASSIGNED GOTO statement at %L requires an "
10423 "INTEGER variable", &code->expr1->where);
10424 else if (code->expr1->symtree->n.sym->attr.assign != 1)
10425 gfc_error ("Variable %qs has not been assigned a target "
10426 "label at %L", code->expr1->symtree->n.sym->name,
10427 &code->expr1->where);
10429 else
10430 resolve_branch (code->label1, code);
10431 break;
10433 case EXEC_RETURN:
10434 if (code->expr1 != NULL
10435 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
10436 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
10437 "INTEGER return specifier", &code->expr1->where);
10438 break;
10440 case EXEC_INIT_ASSIGN:
10441 case EXEC_END_PROCEDURE:
10442 break;
10444 case EXEC_ASSIGN:
10445 if (!t)
10446 break;
10448 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
10449 the LHS. */
10450 if (code->expr1->expr_type == EXPR_FUNCTION
10451 && code->expr1->value.function.isym
10452 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10453 remove_caf_get_intrinsic (code->expr1);
10455 /* If this is a pointer function in an lvalue variable context,
10456 the new code will have to be resolved afresh. This is also the
10457 case with an error, where the code is transformed into NOP to
10458 prevent ICEs downstream. */
10459 if (resolve_ptr_fcn_assign (&code, ns)
10460 || code->op == EXEC_NOP)
10461 goto start;
10463 if (!gfc_check_vardef_context (code->expr1, false, false, false,
10464 _("assignment")))
10465 break;
10467 if (resolve_ordinary_assign (code, ns))
10469 if (code->op == EXEC_COMPCALL)
10470 goto compcall;
10471 else
10472 goto call;
10475 /* Check for dependencies in deferred character length array
10476 assignments and generate a temporary, if necessary. */
10477 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
10478 break;
10480 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
10481 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
10482 && code->expr1->ts.u.derived
10483 && code->expr1->ts.u.derived->attr.defined_assign_comp)
10484 generate_component_assignments (&code, ns);
10486 break;
10488 case EXEC_LABEL_ASSIGN:
10489 if (code->label1->defined == ST_LABEL_UNKNOWN)
10490 gfc_error ("Label %d referenced at %L is never defined",
10491 code->label1->value, &code->label1->where);
10492 if (t
10493 && (code->expr1->expr_type != EXPR_VARIABLE
10494 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
10495 || code->expr1->symtree->n.sym->ts.kind
10496 != gfc_default_integer_kind
10497 || code->expr1->symtree->n.sym->as != NULL))
10498 gfc_error ("ASSIGN statement at %L requires a scalar "
10499 "default INTEGER variable", &code->expr1->where);
10500 break;
10502 case EXEC_POINTER_ASSIGN:
10504 gfc_expr* e;
10506 if (!t)
10507 break;
10509 /* This is both a variable definition and pointer assignment
10510 context, so check both of them. For rank remapping, a final
10511 array ref may be present on the LHS and fool gfc_expr_attr
10512 used in gfc_check_vardef_context. Remove it. */
10513 e = remove_last_array_ref (code->expr1);
10514 t = gfc_check_vardef_context (e, true, false, false,
10515 _("pointer assignment"));
10516 if (t)
10517 t = gfc_check_vardef_context (e, false, false, false,
10518 _("pointer assignment"));
10519 gfc_free_expr (e);
10520 if (!t)
10521 break;
10523 gfc_check_pointer_assign (code->expr1, code->expr2);
10524 break;
10527 case EXEC_ARITHMETIC_IF:
10529 gfc_expr *e = code->expr1;
10531 gfc_resolve_expr (e);
10532 if (e->expr_type == EXPR_NULL)
10533 gfc_error ("Invalid NULL at %L", &e->where);
10535 if (t && (e->rank > 0
10536 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
10537 gfc_error ("Arithmetic IF statement at %L requires a scalar "
10538 "REAL or INTEGER expression", &e->where);
10540 resolve_branch (code->label1, code);
10541 resolve_branch (code->label2, code);
10542 resolve_branch (code->label3, code);
10544 break;
10546 case EXEC_IF:
10547 if (t && code->expr1 != NULL
10548 && (code->expr1->ts.type != BT_LOGICAL
10549 || code->expr1->rank != 0))
10550 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10551 &code->expr1->where);
10552 break;
10554 case EXEC_CALL:
10555 call:
10556 resolve_call (code);
10557 break;
10559 case EXEC_COMPCALL:
10560 compcall:
10561 resolve_typebound_subroutine (code);
10562 break;
10564 case EXEC_CALL_PPC:
10565 resolve_ppc_call (code);
10566 break;
10568 case EXEC_SELECT:
10569 /* Select is complicated. Also, a SELECT construct could be
10570 a transformed computed GOTO. */
10571 resolve_select (code, false);
10572 break;
10574 case EXEC_SELECT_TYPE:
10575 resolve_select_type (code, ns);
10576 break;
10578 case EXEC_BLOCK:
10579 resolve_block_construct (code);
10580 break;
10582 case EXEC_DO:
10583 if (code->ext.iterator != NULL)
10585 gfc_iterator *iter = code->ext.iterator;
10586 if (gfc_resolve_iterator (iter, true, false))
10587 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
10589 break;
10591 case EXEC_DO_WHILE:
10592 if (code->expr1 == NULL)
10593 gfc_internal_error ("gfc_resolve_code(): No expression on "
10594 "DO WHILE");
10595 if (t
10596 && (code->expr1->rank != 0
10597 || code->expr1->ts.type != BT_LOGICAL))
10598 gfc_error ("Exit condition of DO WHILE loop at %L must be "
10599 "a scalar LOGICAL expression", &code->expr1->where);
10600 break;
10602 case EXEC_ALLOCATE:
10603 if (t)
10604 resolve_allocate_deallocate (code, "ALLOCATE");
10606 break;
10608 case EXEC_DEALLOCATE:
10609 if (t)
10610 resolve_allocate_deallocate (code, "DEALLOCATE");
10612 break;
10614 case EXEC_OPEN:
10615 if (!gfc_resolve_open (code->ext.open))
10616 break;
10618 resolve_branch (code->ext.open->err, code);
10619 break;
10621 case EXEC_CLOSE:
10622 if (!gfc_resolve_close (code->ext.close))
10623 break;
10625 resolve_branch (code->ext.close->err, code);
10626 break;
10628 case EXEC_BACKSPACE:
10629 case EXEC_ENDFILE:
10630 case EXEC_REWIND:
10631 case EXEC_FLUSH:
10632 if (!gfc_resolve_filepos (code->ext.filepos))
10633 break;
10635 resolve_branch (code->ext.filepos->err, code);
10636 break;
10638 case EXEC_INQUIRE:
10639 if (!gfc_resolve_inquire (code->ext.inquire))
10640 break;
10642 resolve_branch (code->ext.inquire->err, code);
10643 break;
10645 case EXEC_IOLENGTH:
10646 gcc_assert (code->ext.inquire != NULL);
10647 if (!gfc_resolve_inquire (code->ext.inquire))
10648 break;
10650 resolve_branch (code->ext.inquire->err, code);
10651 break;
10653 case EXEC_WAIT:
10654 if (!gfc_resolve_wait (code->ext.wait))
10655 break;
10657 resolve_branch (code->ext.wait->err, code);
10658 resolve_branch (code->ext.wait->end, code);
10659 resolve_branch (code->ext.wait->eor, code);
10660 break;
10662 case EXEC_READ:
10663 case EXEC_WRITE:
10664 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
10665 break;
10667 resolve_branch (code->ext.dt->err, code);
10668 resolve_branch (code->ext.dt->end, code);
10669 resolve_branch (code->ext.dt->eor, code);
10670 break;
10672 case EXEC_TRANSFER:
10673 resolve_transfer (code);
10674 break;
10676 case EXEC_DO_CONCURRENT:
10677 case EXEC_FORALL:
10678 resolve_forall_iterators (code->ext.forall_iterator);
10680 if (code->expr1 != NULL
10681 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
10682 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
10683 "expression", &code->expr1->where);
10684 break;
10686 case EXEC_OACC_PARALLEL_LOOP:
10687 case EXEC_OACC_PARALLEL:
10688 case EXEC_OACC_KERNELS_LOOP:
10689 case EXEC_OACC_KERNELS:
10690 case EXEC_OACC_DATA:
10691 case EXEC_OACC_HOST_DATA:
10692 case EXEC_OACC_LOOP:
10693 case EXEC_OACC_UPDATE:
10694 case EXEC_OACC_WAIT:
10695 case EXEC_OACC_CACHE:
10696 case EXEC_OACC_ENTER_DATA:
10697 case EXEC_OACC_EXIT_DATA:
10698 case EXEC_OACC_ATOMIC:
10699 case EXEC_OACC_DECLARE:
10700 gfc_resolve_oacc_directive (code, ns);
10701 break;
10703 case EXEC_OMP_ATOMIC:
10704 case EXEC_OMP_BARRIER:
10705 case EXEC_OMP_CANCEL:
10706 case EXEC_OMP_CANCELLATION_POINT:
10707 case EXEC_OMP_CRITICAL:
10708 case EXEC_OMP_FLUSH:
10709 case EXEC_OMP_DISTRIBUTE:
10710 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10711 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10712 case EXEC_OMP_DISTRIBUTE_SIMD:
10713 case EXEC_OMP_DO:
10714 case EXEC_OMP_DO_SIMD:
10715 case EXEC_OMP_MASTER:
10716 case EXEC_OMP_ORDERED:
10717 case EXEC_OMP_SECTIONS:
10718 case EXEC_OMP_SIMD:
10719 case EXEC_OMP_SINGLE:
10720 case EXEC_OMP_TARGET:
10721 case EXEC_OMP_TARGET_DATA:
10722 case EXEC_OMP_TARGET_TEAMS:
10723 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10724 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10725 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10726 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10727 case EXEC_OMP_TARGET_UPDATE:
10728 case EXEC_OMP_TASK:
10729 case EXEC_OMP_TASKGROUP:
10730 case EXEC_OMP_TASKWAIT:
10731 case EXEC_OMP_TASKYIELD:
10732 case EXEC_OMP_TEAMS:
10733 case EXEC_OMP_TEAMS_DISTRIBUTE:
10734 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10735 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10736 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10737 case EXEC_OMP_WORKSHARE:
10738 gfc_resolve_omp_directive (code, ns);
10739 break;
10741 case EXEC_OMP_PARALLEL:
10742 case EXEC_OMP_PARALLEL_DO:
10743 case EXEC_OMP_PARALLEL_DO_SIMD:
10744 case EXEC_OMP_PARALLEL_SECTIONS:
10745 case EXEC_OMP_PARALLEL_WORKSHARE:
10746 omp_workshare_save = omp_workshare_flag;
10747 omp_workshare_flag = 0;
10748 gfc_resolve_omp_directive (code, ns);
10749 omp_workshare_flag = omp_workshare_save;
10750 break;
10752 default:
10753 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
10757 cs_base = frame.prev;
10761 /* Resolve initial values and make sure they are compatible with
10762 the variable. */
10764 static void
10765 resolve_values (gfc_symbol *sym)
10767 bool t;
10769 if (sym->value == NULL)
10770 return;
10772 if (sym->value->expr_type == EXPR_STRUCTURE)
10773 t= resolve_structure_cons (sym->value, 1);
10774 else
10775 t = gfc_resolve_expr (sym->value);
10777 if (!t)
10778 return;
10780 gfc_check_assign_symbol (sym, NULL, sym->value);
10784 /* Verify any BIND(C) derived types in the namespace so we can report errors
10785 for them once, rather than for each variable declared of that type. */
10787 static void
10788 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10790 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10791 && derived_sym->attr.is_bind_c == 1)
10792 verify_bind_c_derived_type (derived_sym);
10794 return;
10798 /* Verify that any binding labels used in a given namespace do not collide
10799 with the names or binding labels of any global symbols. Multiple INTERFACE
10800 for the same procedure are permitted. */
10802 static void
10803 gfc_verify_binding_labels (gfc_symbol *sym)
10805 gfc_gsymbol *gsym;
10806 const char *module;
10808 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
10809 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
10810 return;
10812 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10814 if (sym->module)
10815 module = sym->module;
10816 else if (sym->ns && sym->ns->proc_name
10817 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10818 module = sym->ns->proc_name->name;
10819 else if (sym->ns && sym->ns->parent
10820 && sym->ns && sym->ns->parent->proc_name
10821 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10822 module = sym->ns->parent->proc_name->name;
10823 else
10824 module = NULL;
10826 if (!gsym
10827 || (!gsym->defined
10828 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
10830 if (!gsym)
10831 gsym = gfc_get_gsymbol (sym->binding_label);
10832 gsym->where = sym->declared_at;
10833 gsym->sym_name = sym->name;
10834 gsym->binding_label = sym->binding_label;
10835 gsym->ns = sym->ns;
10836 gsym->mod_name = module;
10837 if (sym->attr.function)
10838 gsym->type = GSYM_FUNCTION;
10839 else if (sym->attr.subroutine)
10840 gsym->type = GSYM_SUBROUTINE;
10841 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
10842 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
10843 return;
10846 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
10848 gfc_error ("Variable %s with binding label %s at %L uses the same global "
10849 "identifier as entity at %L", sym->name,
10850 sym->binding_label, &sym->declared_at, &gsym->where);
10851 /* Clear the binding label to prevent checking multiple times. */
10852 sym->binding_label = NULL;
10855 else if (sym->attr.flavor == FL_VARIABLE && module
10856 && (strcmp (module, gsym->mod_name) != 0
10857 || strcmp (sym->name, gsym->sym_name) != 0))
10859 /* This can only happen if the variable is defined in a module - if it
10860 isn't the same module, reject it. */
10861 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
10862 "the same global identifier as entity at %L from module %s",
10863 sym->name, module, sym->binding_label,
10864 &sym->declared_at, &gsym->where, gsym->mod_name);
10865 sym->binding_label = NULL;
10867 else if ((sym->attr.function || sym->attr.subroutine)
10868 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
10869 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
10870 && sym != gsym->ns->proc_name
10871 && (module != gsym->mod_name
10872 || strcmp (gsym->sym_name, sym->name) != 0
10873 || (module && strcmp (module, gsym->mod_name) != 0)))
10875 /* Print an error if the procedure is defined multiple times; we have to
10876 exclude references to the same procedure via module association or
10877 multiple checks for the same procedure. */
10878 gfc_error ("Procedure %s with binding label %s at %L uses the same "
10879 "global identifier as entity at %L", sym->name,
10880 sym->binding_label, &sym->declared_at, &gsym->where);
10881 sym->binding_label = NULL;
10886 /* Resolve an index expression. */
10888 static bool
10889 resolve_index_expr (gfc_expr *e)
10891 if (!gfc_resolve_expr (e))
10892 return false;
10894 if (!gfc_simplify_expr (e, 0))
10895 return false;
10897 if (!gfc_specification_expr (e))
10898 return false;
10900 return true;
10904 /* Resolve a charlen structure. */
10906 static bool
10907 resolve_charlen (gfc_charlen *cl)
10909 int i, k;
10910 bool saved_specification_expr;
10912 if (cl->resolved)
10913 return true;
10915 cl->resolved = 1;
10916 saved_specification_expr = specification_expr;
10917 specification_expr = true;
10919 if (cl->length_from_typespec)
10921 if (!gfc_resolve_expr (cl->length))
10923 specification_expr = saved_specification_expr;
10924 return false;
10927 if (!gfc_simplify_expr (cl->length, 0))
10929 specification_expr = saved_specification_expr;
10930 return false;
10933 else
10936 if (!resolve_index_expr (cl->length))
10938 specification_expr = saved_specification_expr;
10939 return false;
10943 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
10944 a negative value, the length of character entities declared is zero. */
10945 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
10946 gfc_replace_expr (cl->length,
10947 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
10949 /* Check that the character length is not too large. */
10950 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
10951 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
10952 && cl->length->ts.type == BT_INTEGER
10953 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
10955 gfc_error ("String length at %L is too large", &cl->length->where);
10956 specification_expr = saved_specification_expr;
10957 return false;
10960 specification_expr = saved_specification_expr;
10961 return true;
10965 /* Test for non-constant shape arrays. */
10967 static bool
10968 is_non_constant_shape_array (gfc_symbol *sym)
10970 gfc_expr *e;
10971 int i;
10972 bool not_constant;
10974 not_constant = false;
10975 if (sym->as != NULL)
10977 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
10978 has not been simplified; parameter array references. Do the
10979 simplification now. */
10980 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
10982 e = sym->as->lower[i];
10983 if (e && (!resolve_index_expr(e)
10984 || !gfc_is_constant_expr (e)))
10985 not_constant = true;
10986 e = sym->as->upper[i];
10987 if (e && (!resolve_index_expr(e)
10988 || !gfc_is_constant_expr (e)))
10989 not_constant = true;
10992 return not_constant;
10995 /* Given a symbol and an initialization expression, add code to initialize
10996 the symbol to the function entry. */
10997 static void
10998 build_init_assign (gfc_symbol *sym, gfc_expr *init)
11000 gfc_expr *lval;
11001 gfc_code *init_st;
11002 gfc_namespace *ns = sym->ns;
11004 /* Search for the function namespace if this is a contained
11005 function without an explicit result. */
11006 if (sym->attr.function && sym == sym->result
11007 && sym->name != sym->ns->proc_name->name)
11009 ns = ns->contained;
11010 for (;ns; ns = ns->sibling)
11011 if (strcmp (ns->proc_name->name, sym->name) == 0)
11012 break;
11015 if (ns == NULL)
11017 gfc_free_expr (init);
11018 return;
11021 /* Build an l-value expression for the result. */
11022 lval = gfc_lval_expr_from_sym (sym);
11024 /* Add the code at scope entry. */
11025 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
11026 init_st->next = ns->code;
11027 ns->code = init_st;
11029 /* Assign the default initializer to the l-value. */
11030 init_st->loc = sym->declared_at;
11031 init_st->expr1 = lval;
11032 init_st->expr2 = init;
11035 /* Assign the default initializer to a derived type variable or result. */
11037 static void
11038 apply_default_init (gfc_symbol *sym)
11040 gfc_expr *init = NULL;
11042 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11043 return;
11045 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
11046 init = gfc_default_initializer (&sym->ts);
11048 if (init == NULL && sym->ts.type != BT_CLASS)
11049 return;
11051 build_init_assign (sym, init);
11052 sym->attr.referenced = 1;
11055 /* Build an initializer for a local integer, real, complex, logical, or
11056 character variable, based on the command line flags finit-local-zero,
11057 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
11058 null if the symbol should not have a default initialization. */
11059 static gfc_expr *
11060 build_default_init_expr (gfc_symbol *sym)
11062 int char_len;
11063 gfc_expr *init_expr;
11064 int i;
11066 /* These symbols should never have a default initialization. */
11067 if (sym->attr.allocatable
11068 || sym->attr.external
11069 || sym->attr.dummy
11070 || sym->attr.pointer
11071 || sym->attr.in_equivalence
11072 || sym->attr.in_common
11073 || sym->attr.data
11074 || sym->module
11075 || sym->attr.cray_pointee
11076 || sym->attr.cray_pointer
11077 || sym->assoc)
11078 return NULL;
11080 /* Now we'll try to build an initializer expression. */
11081 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
11082 &sym->declared_at);
11084 /* We will only initialize integers, reals, complex, logicals, and
11085 characters, and only if the corresponding command-line flags
11086 were set. Otherwise, we free init_expr and return null. */
11087 switch (sym->ts.type)
11089 case BT_INTEGER:
11090 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
11091 mpz_set_si (init_expr->value.integer,
11092 gfc_option.flag_init_integer_value);
11093 else
11095 gfc_free_expr (init_expr);
11096 init_expr = NULL;
11098 break;
11100 case BT_REAL:
11101 switch (flag_init_real)
11103 case GFC_INIT_REAL_SNAN:
11104 init_expr->is_snan = 1;
11105 /* Fall through. */
11106 case GFC_INIT_REAL_NAN:
11107 mpfr_set_nan (init_expr->value.real);
11108 break;
11110 case GFC_INIT_REAL_INF:
11111 mpfr_set_inf (init_expr->value.real, 1);
11112 break;
11114 case GFC_INIT_REAL_NEG_INF:
11115 mpfr_set_inf (init_expr->value.real, -1);
11116 break;
11118 case GFC_INIT_REAL_ZERO:
11119 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
11120 break;
11122 default:
11123 gfc_free_expr (init_expr);
11124 init_expr = NULL;
11125 break;
11127 break;
11129 case BT_COMPLEX:
11130 switch (flag_init_real)
11132 case GFC_INIT_REAL_SNAN:
11133 init_expr->is_snan = 1;
11134 /* Fall through. */
11135 case GFC_INIT_REAL_NAN:
11136 mpfr_set_nan (mpc_realref (init_expr->value.complex));
11137 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
11138 break;
11140 case GFC_INIT_REAL_INF:
11141 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
11142 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
11143 break;
11145 case GFC_INIT_REAL_NEG_INF:
11146 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
11147 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
11148 break;
11150 case GFC_INIT_REAL_ZERO:
11151 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
11152 break;
11154 default:
11155 gfc_free_expr (init_expr);
11156 init_expr = NULL;
11157 break;
11159 break;
11161 case BT_LOGICAL:
11162 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
11163 init_expr->value.logical = 0;
11164 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
11165 init_expr->value.logical = 1;
11166 else
11168 gfc_free_expr (init_expr);
11169 init_expr = NULL;
11171 break;
11173 case BT_CHARACTER:
11174 /* For characters, the length must be constant in order to
11175 create a default initializer. */
11176 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
11177 && sym->ts.u.cl->length
11178 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11180 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
11181 init_expr->value.character.length = char_len;
11182 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
11183 for (i = 0; i < char_len; i++)
11184 init_expr->value.character.string[i]
11185 = (unsigned char) gfc_option.flag_init_character_value;
11187 else
11189 gfc_free_expr (init_expr);
11190 init_expr = NULL;
11192 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
11193 && sym->ts.u.cl->length && flag_max_stack_var_size != 0)
11195 gfc_actual_arglist *arg;
11196 init_expr = gfc_get_expr ();
11197 init_expr->where = sym->declared_at;
11198 init_expr->ts = sym->ts;
11199 init_expr->expr_type = EXPR_FUNCTION;
11200 init_expr->value.function.isym =
11201 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
11202 init_expr->value.function.name = "repeat";
11203 arg = gfc_get_actual_arglist ();
11204 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
11205 NULL, 1);
11206 arg->expr->value.character.string[0]
11207 = gfc_option.flag_init_character_value;
11208 arg->next = gfc_get_actual_arglist ();
11209 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
11210 init_expr->value.function.actual = arg;
11212 break;
11214 default:
11215 gfc_free_expr (init_expr);
11216 init_expr = NULL;
11218 return init_expr;
11221 /* Add an initialization expression to a local variable. */
11222 static void
11223 apply_default_init_local (gfc_symbol *sym)
11225 gfc_expr *init = NULL;
11227 /* The symbol should be a variable or a function return value. */
11228 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11229 || (sym->attr.function && sym->result != sym))
11230 return;
11232 /* Try to build the initializer expression. If we can't initialize
11233 this symbol, then init will be NULL. */
11234 init = build_default_init_expr (sym);
11235 if (init == NULL)
11236 return;
11238 /* For saved variables, we don't want to add an initializer at function
11239 entry, so we just add a static initializer. Note that automatic variables
11240 are stack allocated even with -fno-automatic; we have also to exclude
11241 result variable, which are also nonstatic. */
11242 if (sym->attr.save || sym->ns->save_all
11243 || (flag_max_stack_var_size == 0 && !sym->attr.result
11244 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
11245 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
11247 /* Don't clobber an existing initializer! */
11248 gcc_assert (sym->value == NULL);
11249 sym->value = init;
11250 return;
11253 build_init_assign (sym, init);
11257 /* Resolution of common features of flavors variable and procedure. */
11259 static bool
11260 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
11262 gfc_array_spec *as;
11264 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11265 as = CLASS_DATA (sym)->as;
11266 else
11267 as = sym->as;
11269 /* Constraints on deferred shape variable. */
11270 if (as == NULL || as->type != AS_DEFERRED)
11272 bool pointer, allocatable, dimension;
11274 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11276 pointer = CLASS_DATA (sym)->attr.class_pointer;
11277 allocatable = CLASS_DATA (sym)->attr.allocatable;
11278 dimension = CLASS_DATA (sym)->attr.dimension;
11280 else
11282 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
11283 allocatable = sym->attr.allocatable;
11284 dimension = sym->attr.dimension;
11287 if (allocatable)
11289 if (dimension && as->type != AS_ASSUMED_RANK)
11291 gfc_error ("Allocatable array %qs at %L must have a deferred "
11292 "shape or assumed rank", sym->name, &sym->declared_at);
11293 return false;
11295 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
11296 "%qs at %L may not be ALLOCATABLE",
11297 sym->name, &sym->declared_at))
11298 return false;
11301 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
11303 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
11304 "assumed rank", sym->name, &sym->declared_at);
11305 return false;
11308 else
11310 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
11311 && sym->ts.type != BT_CLASS && !sym->assoc)
11313 gfc_error ("Array %qs at %L cannot have a deferred shape",
11314 sym->name, &sym->declared_at);
11315 return false;
11319 /* Constraints on polymorphic variables. */
11320 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
11322 /* F03:C502. */
11323 if (sym->attr.class_ok
11324 && !sym->attr.select_type_temporary
11325 && !UNLIMITED_POLY (sym)
11326 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
11328 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
11329 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
11330 &sym->declared_at);
11331 return false;
11334 /* F03:C509. */
11335 /* Assume that use associated symbols were checked in the module ns.
11336 Class-variables that are associate-names are also something special
11337 and excepted from the test. */
11338 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
11340 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
11341 "or pointer", sym->name, &sym->declared_at);
11342 return false;
11346 return true;
11350 /* Additional checks for symbols with flavor variable and derived
11351 type. To be called from resolve_fl_variable. */
11353 static bool
11354 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
11356 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
11358 /* Check to see if a derived type is blocked from being host
11359 associated by the presence of another class I symbol in the same
11360 namespace. 14.6.1.3 of the standard and the discussion on
11361 comp.lang.fortran. */
11362 if (sym->ns != sym->ts.u.derived->ns
11363 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
11365 gfc_symbol *s;
11366 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
11367 if (s && s->attr.generic)
11368 s = gfc_find_dt_in_generic (s);
11369 if (s && s->attr.flavor != FL_DERIVED)
11371 gfc_error ("The type %qs cannot be host associated at %L "
11372 "because it is blocked by an incompatible object "
11373 "of the same name declared at %L",
11374 sym->ts.u.derived->name, &sym->declared_at,
11375 &s->declared_at);
11376 return false;
11380 /* 4th constraint in section 11.3: "If an object of a type for which
11381 component-initialization is specified (R429) appears in the
11382 specification-part of a module and does not have the ALLOCATABLE
11383 or POINTER attribute, the object shall have the SAVE attribute."
11385 The check for initializers is performed with
11386 gfc_has_default_initializer because gfc_default_initializer generates
11387 a hidden default for allocatable components. */
11388 if (!(sym->value || no_init_flag) && sym->ns->proc_name
11389 && sym->ns->proc_name->attr.flavor == FL_MODULE
11390 && !sym->ns->save_all && !sym->attr.save
11391 && !sym->attr.pointer && !sym->attr.allocatable
11392 && gfc_has_default_initializer (sym->ts.u.derived)
11393 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
11394 "%qs at %L, needed due to the default "
11395 "initialization", sym->name, &sym->declared_at))
11396 return false;
11398 /* Assign default initializer. */
11399 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
11400 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
11402 sym->value = gfc_default_initializer (&sym->ts);
11405 return true;
11409 /* Resolve symbols with flavor variable. */
11411 static bool
11412 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
11414 int no_init_flag, automatic_flag;
11415 gfc_expr *e;
11416 const char *auto_save_msg;
11417 bool saved_specification_expr;
11419 auto_save_msg = "Automatic object %qs at %L cannot have the "
11420 "SAVE attribute";
11422 if (!resolve_fl_var_and_proc (sym, mp_flag))
11423 return false;
11425 /* Set this flag to check that variables are parameters of all entries.
11426 This check is effected by the call to gfc_resolve_expr through
11427 is_non_constant_shape_array. */
11428 saved_specification_expr = specification_expr;
11429 specification_expr = true;
11431 if (sym->ns->proc_name
11432 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11433 || sym->ns->proc_name->attr.is_main_program)
11434 && !sym->attr.use_assoc
11435 && !sym->attr.allocatable
11436 && !sym->attr.pointer
11437 && is_non_constant_shape_array (sym))
11439 /* The shape of a main program or module array needs to be
11440 constant. */
11441 gfc_error ("The module or main program array %qs at %L must "
11442 "have constant shape", sym->name, &sym->declared_at);
11443 specification_expr = saved_specification_expr;
11444 return false;
11447 /* Constraints on deferred type parameter. */
11448 if (sym->ts.deferred
11449 && !(sym->attr.pointer
11450 || sym->attr.allocatable
11451 || sym->attr.omp_udr_artificial_var))
11453 gfc_error ("Entity %qs at %L has a deferred type parameter and "
11454 "requires either the pointer or allocatable attribute",
11455 sym->name, &sym->declared_at);
11456 specification_expr = saved_specification_expr;
11457 return false;
11460 if (sym->ts.type == BT_CHARACTER)
11462 /* Make sure that character string variables with assumed length are
11463 dummy arguments. */
11464 e = sym->ts.u.cl->length;
11465 if (e == NULL && !sym->attr.dummy && !sym->attr.result
11466 && !sym->ts.deferred && !sym->attr.select_type_temporary
11467 && !sym->attr.omp_udr_artificial_var)
11469 gfc_error ("Entity with assumed character length at %L must be a "
11470 "dummy argument or a PARAMETER", &sym->declared_at);
11471 specification_expr = saved_specification_expr;
11472 return false;
11475 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
11477 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
11478 specification_expr = saved_specification_expr;
11479 return false;
11482 if (!gfc_is_constant_expr (e)
11483 && !(e->expr_type == EXPR_VARIABLE
11484 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
11486 if (!sym->attr.use_assoc && sym->ns->proc_name
11487 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11488 || sym->ns->proc_name->attr.is_main_program))
11490 gfc_error ("%qs at %L must have constant character length "
11491 "in this context", sym->name, &sym->declared_at);
11492 specification_expr = saved_specification_expr;
11493 return false;
11495 if (sym->attr.in_common)
11497 gfc_error ("COMMON variable %qs at %L must have constant "
11498 "character length", sym->name, &sym->declared_at);
11499 specification_expr = saved_specification_expr;
11500 return false;
11505 if (sym->value == NULL && sym->attr.referenced)
11506 apply_default_init_local (sym); /* Try to apply a default initialization. */
11508 /* Determine if the symbol may not have an initializer. */
11509 no_init_flag = automatic_flag = 0;
11510 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
11511 || sym->attr.intrinsic || sym->attr.result)
11512 no_init_flag = 1;
11513 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
11514 && is_non_constant_shape_array (sym))
11516 no_init_flag = automatic_flag = 1;
11518 /* Also, they must not have the SAVE attribute.
11519 SAVE_IMPLICIT is checked below. */
11520 if (sym->as && sym->attr.codimension)
11522 int corank = sym->as->corank;
11523 sym->as->corank = 0;
11524 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
11525 sym->as->corank = corank;
11527 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
11529 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
11530 specification_expr = saved_specification_expr;
11531 return false;
11535 /* Ensure that any initializer is simplified. */
11536 if (sym->value)
11537 gfc_simplify_expr (sym->value, 1);
11539 /* Reject illegal initializers. */
11540 if (!sym->mark && sym->value)
11542 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
11543 && CLASS_DATA (sym)->attr.allocatable))
11544 gfc_error ("Allocatable %qs at %L cannot have an initializer",
11545 sym->name, &sym->declared_at);
11546 else if (sym->attr.external)
11547 gfc_error ("External %qs at %L cannot have an initializer",
11548 sym->name, &sym->declared_at);
11549 else if (sym->attr.dummy
11550 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
11551 gfc_error ("Dummy %qs at %L cannot have an initializer",
11552 sym->name, &sym->declared_at);
11553 else if (sym->attr.intrinsic)
11554 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
11555 sym->name, &sym->declared_at);
11556 else if (sym->attr.result)
11557 gfc_error ("Function result %qs at %L cannot have an initializer",
11558 sym->name, &sym->declared_at);
11559 else if (automatic_flag)
11560 gfc_error ("Automatic array %qs at %L cannot have an initializer",
11561 sym->name, &sym->declared_at);
11562 else
11563 goto no_init_error;
11564 specification_expr = saved_specification_expr;
11565 return false;
11568 no_init_error:
11569 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
11571 bool res = resolve_fl_variable_derived (sym, no_init_flag);
11572 specification_expr = saved_specification_expr;
11573 return res;
11576 specification_expr = saved_specification_expr;
11577 return true;
11581 /* Compare the dummy characteristics of a module procedure interface
11582 declaration with the corresponding declaration in a submodule. */
11583 static gfc_formal_arglist *new_formal;
11584 static char errmsg[200];
11586 static void
11587 compare_fsyms (gfc_symbol *sym)
11589 gfc_symbol *fsym;
11591 if (sym == NULL || new_formal == NULL)
11592 return;
11594 fsym = new_formal->sym;
11596 if (sym == fsym)
11597 return;
11599 if (strcmp (sym->name, fsym->name) == 0)
11601 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
11602 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
11607 /* Resolve a procedure. */
11609 static bool
11610 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
11612 gfc_formal_arglist *arg;
11614 if (sym->attr.function
11615 && !resolve_fl_var_and_proc (sym, mp_flag))
11616 return false;
11618 if (sym->ts.type == BT_CHARACTER)
11620 gfc_charlen *cl = sym->ts.u.cl;
11622 if (cl && cl->length && gfc_is_constant_expr (cl->length)
11623 && !resolve_charlen (cl))
11624 return false;
11626 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11627 && sym->attr.proc == PROC_ST_FUNCTION)
11629 gfc_error ("Character-valued statement function %qs at %L must "
11630 "have constant length", sym->name, &sym->declared_at);
11631 return false;
11635 /* Ensure that derived type for are not of a private type. Internal
11636 module procedures are excluded by 2.2.3.3 - i.e., they are not
11637 externally accessible and can access all the objects accessible in
11638 the host. */
11639 if (!(sym->ns->parent
11640 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
11641 && gfc_check_symbol_access (sym))
11643 gfc_interface *iface;
11645 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
11647 if (arg->sym
11648 && arg->sym->ts.type == BT_DERIVED
11649 && !arg->sym->ts.u.derived->attr.use_assoc
11650 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11651 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
11652 "and cannot be a dummy argument"
11653 " of %qs, which is PUBLIC at %L",
11654 arg->sym->name, sym->name,
11655 &sym->declared_at))
11657 /* Stop this message from recurring. */
11658 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11659 return false;
11663 /* PUBLIC interfaces may expose PRIVATE procedures that take types
11664 PRIVATE to the containing module. */
11665 for (iface = sym->generic; iface; iface = iface->next)
11667 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
11669 if (arg->sym
11670 && arg->sym->ts.type == BT_DERIVED
11671 && !arg->sym->ts.u.derived->attr.use_assoc
11672 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11673 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
11674 "PUBLIC interface %qs at %L "
11675 "takes dummy arguments of %qs which "
11676 "is PRIVATE", iface->sym->name,
11677 sym->name, &iface->sym->declared_at,
11678 gfc_typename(&arg->sym->ts)))
11680 /* Stop this message from recurring. */
11681 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11682 return false;
11688 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
11689 && !sym->attr.proc_pointer)
11691 gfc_error ("Function %qs at %L cannot have an initializer",
11692 sym->name, &sym->declared_at);
11693 return false;
11696 /* An external symbol may not have an initializer because it is taken to be
11697 a procedure. Exception: Procedure Pointers. */
11698 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
11700 gfc_error ("External object %qs at %L may not have an initializer",
11701 sym->name, &sym->declared_at);
11702 return false;
11705 /* An elemental function is required to return a scalar 12.7.1 */
11706 if (sym->attr.elemental && sym->attr.function && sym->as)
11708 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
11709 "result", sym->name, &sym->declared_at);
11710 /* Reset so that the error only occurs once. */
11711 sym->attr.elemental = 0;
11712 return false;
11715 if (sym->attr.proc == PROC_ST_FUNCTION
11716 && (sym->attr.allocatable || sym->attr.pointer))
11718 gfc_error ("Statement function %qs at %L may not have pointer or "
11719 "allocatable attribute", sym->name, &sym->declared_at);
11720 return false;
11723 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
11724 char-len-param shall not be array-valued, pointer-valued, recursive
11725 or pure. ....snip... A character value of * may only be used in the
11726 following ways: (i) Dummy arg of procedure - dummy associates with
11727 actual length; (ii) To declare a named constant; or (iii) External
11728 function - but length must be declared in calling scoping unit. */
11729 if (sym->attr.function
11730 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
11731 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
11733 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
11734 || (sym->attr.recursive) || (sym->attr.pure))
11736 if (sym->as && sym->as->rank)
11737 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11738 "array-valued", sym->name, &sym->declared_at);
11740 if (sym->attr.pointer)
11741 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11742 "pointer-valued", sym->name, &sym->declared_at);
11744 if (sym->attr.pure)
11745 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11746 "pure", sym->name, &sym->declared_at);
11748 if (sym->attr.recursive)
11749 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11750 "recursive", sym->name, &sym->declared_at);
11752 return false;
11755 /* Appendix B.2 of the standard. Contained functions give an
11756 error anyway. Deferred character length is an F2003 feature.
11757 Don't warn on intrinsic conversion functions, which start
11758 with two underscores. */
11759 if (!sym->attr.contained && !sym->ts.deferred
11760 && (sym->name[0] != '_' || sym->name[1] != '_'))
11761 gfc_notify_std (GFC_STD_F95_OBS,
11762 "CHARACTER(*) function %qs at %L",
11763 sym->name, &sym->declared_at);
11766 /* F2008, C1218. */
11767 if (sym->attr.elemental)
11769 if (sym->attr.proc_pointer)
11771 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
11772 sym->name, &sym->declared_at);
11773 return false;
11775 if (sym->attr.dummy)
11777 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
11778 sym->name, &sym->declared_at);
11779 return false;
11783 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11785 gfc_formal_arglist *curr_arg;
11786 int has_non_interop_arg = 0;
11788 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11789 sym->common_block))
11791 /* Clear these to prevent looking at them again if there was an
11792 error. */
11793 sym->attr.is_bind_c = 0;
11794 sym->attr.is_c_interop = 0;
11795 sym->ts.is_c_interop = 0;
11797 else
11799 /* So far, no errors have been found. */
11800 sym->attr.is_c_interop = 1;
11801 sym->ts.is_c_interop = 1;
11804 curr_arg = gfc_sym_get_dummy_args (sym);
11805 while (curr_arg != NULL)
11807 /* Skip implicitly typed dummy args here. */
11808 if (curr_arg->sym->attr.implicit_type == 0)
11809 if (!gfc_verify_c_interop_param (curr_arg->sym))
11810 /* If something is found to fail, record the fact so we
11811 can mark the symbol for the procedure as not being
11812 BIND(C) to try and prevent multiple errors being
11813 reported. */
11814 has_non_interop_arg = 1;
11816 curr_arg = curr_arg->next;
11819 /* See if any of the arguments were not interoperable and if so, clear
11820 the procedure symbol to prevent duplicate error messages. */
11821 if (has_non_interop_arg != 0)
11823 sym->attr.is_c_interop = 0;
11824 sym->ts.is_c_interop = 0;
11825 sym->attr.is_bind_c = 0;
11829 if (!sym->attr.proc_pointer)
11831 if (sym->attr.save == SAVE_EXPLICIT)
11833 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11834 "in %qs at %L", sym->name, &sym->declared_at);
11835 return false;
11837 if (sym->attr.intent)
11839 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11840 "in %qs at %L", sym->name, &sym->declared_at);
11841 return false;
11843 if (sym->attr.subroutine && sym->attr.result)
11845 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11846 "in %qs at %L", sym->name, &sym->declared_at);
11847 return false;
11849 if (sym->attr.external && sym->attr.function
11850 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11851 || sym->attr.contained))
11853 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11854 "in %qs at %L", sym->name, &sym->declared_at);
11855 return false;
11857 if (strcmp ("ppr@", sym->name) == 0)
11859 gfc_error ("Procedure pointer result %qs at %L "
11860 "is missing the pointer attribute",
11861 sym->ns->proc_name->name, &sym->declared_at);
11862 return false;
11866 /* Assume that a procedure whose body is not known has references
11867 to external arrays. */
11868 if (sym->attr.if_source != IFSRC_DECL)
11869 sym->attr.array_outer_dependency = 1;
11871 /* Compare the characteristics of a module procedure with the
11872 interface declaration. Ideally this would be done with
11873 gfc_compare_interfaces but, at present, the formal interface
11874 cannot be copied to the ts.interface. */
11875 if (sym->attr.module_procedure
11876 && sym->attr.if_source == IFSRC_DECL)
11878 gfc_symbol *iface;
11879 char name[2*GFC_MAX_SYMBOL_LEN + 1];
11880 char *module_name;
11881 char *submodule_name;
11882 strcpy (name, sym->ns->proc_name->name);
11883 module_name = strtok (name, ".");
11884 submodule_name = strtok (NULL, ".");
11886 /* Stop the dummy characteristics test from using the interface
11887 symbol instead of 'sym'. */
11888 iface = sym->ts.interface;
11889 sym->ts.interface = NULL;
11891 if (iface == NULL)
11892 goto check_formal;
11894 /* Check the procedure characteristics. */
11895 if (sym->attr.pure != iface->attr.pure)
11897 gfc_error ("Mismatch in PURE attribute between MODULE "
11898 "PROCEDURE at %L and its interface in %s",
11899 &sym->declared_at, module_name);
11900 return false;
11903 if (sym->attr.elemental != iface->attr.elemental)
11905 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
11906 "PROCEDURE at %L and its interface in %s",
11907 &sym->declared_at, module_name);
11908 return false;
11911 if (sym->attr.recursive != iface->attr.recursive)
11913 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
11914 "PROCEDURE at %L and its interface in %s",
11915 &sym->declared_at, module_name);
11916 return false;
11919 /* Check the result characteristics. */
11920 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
11922 gfc_error ("%s between the MODULE PROCEDURE declaration "
11923 "in module %s and the declaration at %L in "
11924 "SUBMODULE %s", errmsg, module_name,
11925 &sym->declared_at, submodule_name);
11926 return false;
11929 check_formal:
11930 /* Check the charcateristics of the formal arguments. */
11931 if (sym->formal && sym->formal_ns)
11933 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
11935 new_formal = arg;
11936 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
11940 sym->ts.interface = iface;
11942 return true;
11946 /* Resolve a list of finalizer procedures. That is, after they have hopefully
11947 been defined and we now know their defined arguments, check that they fulfill
11948 the requirements of the standard for procedures used as finalizers. */
11950 static bool
11951 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
11953 gfc_finalizer* list;
11954 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
11955 bool result = true;
11956 bool seen_scalar = false;
11957 gfc_symbol *vtab;
11958 gfc_component *c;
11959 gfc_symbol *parent = gfc_get_derived_super_type (derived);
11961 if (parent)
11962 gfc_resolve_finalizers (parent, finalizable);
11964 /* Return early when not finalizable. Additionally, ensure that derived-type
11965 components have a their finalizables resolved. */
11966 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
11968 bool has_final = false;
11969 for (c = derived->components; c; c = c->next)
11970 if (c->ts.type == BT_DERIVED
11971 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
11973 bool has_final2 = false;
11974 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final))
11975 return false; /* Error. */
11976 has_final = has_final || has_final2;
11978 if (!has_final)
11980 if (finalizable)
11981 *finalizable = false;
11982 return true;
11986 /* Walk over the list of finalizer-procedures, check them, and if any one
11987 does not fit in with the standard's definition, print an error and remove
11988 it from the list. */
11989 prev_link = &derived->f2k_derived->finalizers;
11990 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
11992 gfc_formal_arglist *dummy_args;
11993 gfc_symbol* arg;
11994 gfc_finalizer* i;
11995 int my_rank;
11997 /* Skip this finalizer if we already resolved it. */
11998 if (list->proc_tree)
12000 prev_link = &(list->next);
12001 continue;
12004 /* Check this exists and is a SUBROUTINE. */
12005 if (!list->proc_sym->attr.subroutine)
12007 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
12008 list->proc_sym->name, &list->where);
12009 goto error;
12012 /* We should have exactly one argument. */
12013 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
12014 if (!dummy_args || dummy_args->next)
12016 gfc_error ("FINAL procedure at %L must have exactly one argument",
12017 &list->where);
12018 goto error;
12020 arg = dummy_args->sym;
12022 /* This argument must be of our type. */
12023 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
12025 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
12026 &arg->declared_at, derived->name);
12027 goto error;
12030 /* It must neither be a pointer nor allocatable nor optional. */
12031 if (arg->attr.pointer)
12033 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
12034 &arg->declared_at);
12035 goto error;
12037 if (arg->attr.allocatable)
12039 gfc_error ("Argument of FINAL procedure at %L must not be"
12040 " ALLOCATABLE", &arg->declared_at);
12041 goto error;
12043 if (arg->attr.optional)
12045 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
12046 &arg->declared_at);
12047 goto error;
12050 /* It must not be INTENT(OUT). */
12051 if (arg->attr.intent == INTENT_OUT)
12053 gfc_error ("Argument of FINAL procedure at %L must not be"
12054 " INTENT(OUT)", &arg->declared_at);
12055 goto error;
12058 /* Warn if the procedure is non-scalar and not assumed shape. */
12059 if (warn_surprising && arg->as && arg->as->rank != 0
12060 && arg->as->type != AS_ASSUMED_SHAPE)
12061 gfc_warning (OPT_Wsurprising,
12062 "Non-scalar FINAL procedure at %L should have assumed"
12063 " shape argument", &arg->declared_at);
12065 /* Check that it does not match in kind and rank with a FINAL procedure
12066 defined earlier. To really loop over the *earlier* declarations,
12067 we need to walk the tail of the list as new ones were pushed at the
12068 front. */
12069 /* TODO: Handle kind parameters once they are implemented. */
12070 my_rank = (arg->as ? arg->as->rank : 0);
12071 for (i = list->next; i; i = i->next)
12073 gfc_formal_arglist *dummy_args;
12075 /* Argument list might be empty; that is an error signalled earlier,
12076 but we nevertheless continued resolving. */
12077 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
12078 if (dummy_args)
12080 gfc_symbol* i_arg = dummy_args->sym;
12081 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
12082 if (i_rank == my_rank)
12084 gfc_error ("FINAL procedure %qs declared at %L has the same"
12085 " rank (%d) as %qs",
12086 list->proc_sym->name, &list->where, my_rank,
12087 i->proc_sym->name);
12088 goto error;
12093 /* Is this the/a scalar finalizer procedure? */
12094 if (!arg->as || arg->as->rank == 0)
12095 seen_scalar = true;
12097 /* Find the symtree for this procedure. */
12098 gcc_assert (!list->proc_tree);
12099 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
12101 prev_link = &list->next;
12102 continue;
12104 /* Remove wrong nodes immediately from the list so we don't risk any
12105 troubles in the future when they might fail later expectations. */
12106 error:
12107 i = list;
12108 *prev_link = list->next;
12109 gfc_free_finalizer (i);
12110 result = false;
12113 if (result == false)
12114 return false;
12116 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
12117 were nodes in the list, must have been for arrays. It is surely a good
12118 idea to have a scalar version there if there's something to finalize. */
12119 if (warn_surprising && result && !seen_scalar)
12120 gfc_warning (OPT_Wsurprising,
12121 "Only array FINAL procedures declared for derived type %qs"
12122 " defined at %L, suggest also scalar one",
12123 derived->name, &derived->declared_at);
12125 vtab = gfc_find_derived_vtab (derived);
12126 c = vtab->ts.u.derived->components->next->next->next->next->next;
12127 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
12129 if (finalizable)
12130 *finalizable = true;
12132 return true;
12136 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
12138 static bool
12139 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
12140 const char* generic_name, locus where)
12142 gfc_symbol *sym1, *sym2;
12143 const char *pass1, *pass2;
12144 gfc_formal_arglist *dummy_args;
12146 gcc_assert (t1->specific && t2->specific);
12147 gcc_assert (!t1->specific->is_generic);
12148 gcc_assert (!t2->specific->is_generic);
12149 gcc_assert (t1->is_operator == t2->is_operator);
12151 sym1 = t1->specific->u.specific->n.sym;
12152 sym2 = t2->specific->u.specific->n.sym;
12154 if (sym1 == sym2)
12155 return true;
12157 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
12158 if (sym1->attr.subroutine != sym2->attr.subroutine
12159 || sym1->attr.function != sym2->attr.function)
12161 gfc_error ("%qs and %qs can't be mixed FUNCTION/SUBROUTINE for"
12162 " GENERIC %qs at %L",
12163 sym1->name, sym2->name, generic_name, &where);
12164 return false;
12167 /* Determine PASS arguments. */
12168 if (t1->specific->nopass)
12169 pass1 = NULL;
12170 else if (t1->specific->pass_arg)
12171 pass1 = t1->specific->pass_arg;
12172 else
12174 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
12175 if (dummy_args)
12176 pass1 = dummy_args->sym->name;
12177 else
12178 pass1 = NULL;
12180 if (t2->specific->nopass)
12181 pass2 = NULL;
12182 else if (t2->specific->pass_arg)
12183 pass2 = t2->specific->pass_arg;
12184 else
12186 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
12187 if (dummy_args)
12188 pass2 = dummy_args->sym->name;
12189 else
12190 pass2 = NULL;
12193 /* Compare the interfaces. */
12194 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
12195 NULL, 0, pass1, pass2))
12197 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
12198 sym1->name, sym2->name, generic_name, &where);
12199 return false;
12202 return true;
12206 /* Worker function for resolving a generic procedure binding; this is used to
12207 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
12209 The difference between those cases is finding possible inherited bindings
12210 that are overridden, as one has to look for them in tb_sym_root,
12211 tb_uop_root or tb_op, respectively. Thus the caller must already find
12212 the super-type and set p->overridden correctly. */
12214 static bool
12215 resolve_tb_generic_targets (gfc_symbol* super_type,
12216 gfc_typebound_proc* p, const char* name)
12218 gfc_tbp_generic* target;
12219 gfc_symtree* first_target;
12220 gfc_symtree* inherited;
12222 gcc_assert (p && p->is_generic);
12224 /* Try to find the specific bindings for the symtrees in our target-list. */
12225 gcc_assert (p->u.generic);
12226 for (target = p->u.generic; target; target = target->next)
12227 if (!target->specific)
12229 gfc_typebound_proc* overridden_tbp;
12230 gfc_tbp_generic* g;
12231 const char* target_name;
12233 target_name = target->specific_st->name;
12235 /* Defined for this type directly. */
12236 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
12238 target->specific = target->specific_st->n.tb;
12239 goto specific_found;
12242 /* Look for an inherited specific binding. */
12243 if (super_type)
12245 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
12246 true, NULL);
12248 if (inherited)
12250 gcc_assert (inherited->n.tb);
12251 target->specific = inherited->n.tb;
12252 goto specific_found;
12256 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
12257 " at %L", target_name, name, &p->where);
12258 return false;
12260 /* Once we've found the specific binding, check it is not ambiguous with
12261 other specifics already found or inherited for the same GENERIC. */
12262 specific_found:
12263 gcc_assert (target->specific);
12265 /* This must really be a specific binding! */
12266 if (target->specific->is_generic)
12268 gfc_error ("GENERIC %qs at %L must target a specific binding,"
12269 " %qs is GENERIC, too", name, &p->where, target_name);
12270 return false;
12273 /* Check those already resolved on this type directly. */
12274 for (g = p->u.generic; g; g = g->next)
12275 if (g != target && g->specific
12276 && !check_generic_tbp_ambiguity (target, g, name, p->where))
12277 return false;
12279 /* Check for ambiguity with inherited specific targets. */
12280 for (overridden_tbp = p->overridden; overridden_tbp;
12281 overridden_tbp = overridden_tbp->overridden)
12282 if (overridden_tbp->is_generic)
12284 for (g = overridden_tbp->u.generic; g; g = g->next)
12286 gcc_assert (g->specific);
12287 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
12288 return false;
12293 /* If we attempt to "overwrite" a specific binding, this is an error. */
12294 if (p->overridden && !p->overridden->is_generic)
12296 gfc_error ("GENERIC %qs at %L can't overwrite specific binding with"
12297 " the same name", name, &p->where);
12298 return false;
12301 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
12302 all must have the same attributes here. */
12303 first_target = p->u.generic->specific->u.specific;
12304 gcc_assert (first_target);
12305 p->subroutine = first_target->n.sym->attr.subroutine;
12306 p->function = first_target->n.sym->attr.function;
12308 return true;
12312 /* Resolve a GENERIC procedure binding for a derived type. */
12314 static bool
12315 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
12317 gfc_symbol* super_type;
12319 /* Find the overridden binding if any. */
12320 st->n.tb->overridden = NULL;
12321 super_type = gfc_get_derived_super_type (derived);
12322 if (super_type)
12324 gfc_symtree* overridden;
12325 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
12326 true, NULL);
12328 if (overridden && overridden->n.tb)
12329 st->n.tb->overridden = overridden->n.tb;
12332 /* Resolve using worker function. */
12333 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
12337 /* Retrieve the target-procedure of an operator binding and do some checks in
12338 common for intrinsic and user-defined type-bound operators. */
12340 static gfc_symbol*
12341 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
12343 gfc_symbol* target_proc;
12345 gcc_assert (target->specific && !target->specific->is_generic);
12346 target_proc = target->specific->u.specific->n.sym;
12347 gcc_assert (target_proc);
12349 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
12350 if (target->specific->nopass)
12352 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
12353 return NULL;
12356 return target_proc;
12360 /* Resolve a type-bound intrinsic operator. */
12362 static bool
12363 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
12364 gfc_typebound_proc* p)
12366 gfc_symbol* super_type;
12367 gfc_tbp_generic* target;
12369 /* If there's already an error here, do nothing (but don't fail again). */
12370 if (p->error)
12371 return true;
12373 /* Operators should always be GENERIC bindings. */
12374 gcc_assert (p->is_generic);
12376 /* Look for an overridden binding. */
12377 super_type = gfc_get_derived_super_type (derived);
12378 if (super_type && super_type->f2k_derived)
12379 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
12380 op, true, NULL);
12381 else
12382 p->overridden = NULL;
12384 /* Resolve general GENERIC properties using worker function. */
12385 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
12386 goto error;
12388 /* Check the targets to be procedures of correct interface. */
12389 for (target = p->u.generic; target; target = target->next)
12391 gfc_symbol* target_proc;
12393 target_proc = get_checked_tb_operator_target (target, p->where);
12394 if (!target_proc)
12395 goto error;
12397 if (!gfc_check_operator_interface (target_proc, op, p->where))
12398 goto error;
12400 /* Add target to non-typebound operator list. */
12401 if (!target->specific->deferred && !derived->attr.use_assoc
12402 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
12404 gfc_interface *head, *intr;
12405 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
12406 return false;
12407 head = derived->ns->op[op];
12408 intr = gfc_get_interface ();
12409 intr->sym = target_proc;
12410 intr->where = p->where;
12411 intr->next = head;
12412 derived->ns->op[op] = intr;
12416 return true;
12418 error:
12419 p->error = 1;
12420 return false;
12424 /* Resolve a type-bound user operator (tree-walker callback). */
12426 static gfc_symbol* resolve_bindings_derived;
12427 static bool resolve_bindings_result;
12429 static bool check_uop_procedure (gfc_symbol* sym, locus where);
12431 static void
12432 resolve_typebound_user_op (gfc_symtree* stree)
12434 gfc_symbol* super_type;
12435 gfc_tbp_generic* target;
12437 gcc_assert (stree && stree->n.tb);
12439 if (stree->n.tb->error)
12440 return;
12442 /* Operators should always be GENERIC bindings. */
12443 gcc_assert (stree->n.tb->is_generic);
12445 /* Find overridden procedure, if any. */
12446 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12447 if (super_type && super_type->f2k_derived)
12449 gfc_symtree* overridden;
12450 overridden = gfc_find_typebound_user_op (super_type, NULL,
12451 stree->name, true, NULL);
12453 if (overridden && overridden->n.tb)
12454 stree->n.tb->overridden = overridden->n.tb;
12456 else
12457 stree->n.tb->overridden = NULL;
12459 /* Resolve basically using worker function. */
12460 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
12461 goto error;
12463 /* Check the targets to be functions of correct interface. */
12464 for (target = stree->n.tb->u.generic; target; target = target->next)
12466 gfc_symbol* target_proc;
12468 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
12469 if (!target_proc)
12470 goto error;
12472 if (!check_uop_procedure (target_proc, stree->n.tb->where))
12473 goto error;
12476 return;
12478 error:
12479 resolve_bindings_result = false;
12480 stree->n.tb->error = 1;
12484 /* Resolve the type-bound procedures for a derived type. */
12486 static void
12487 resolve_typebound_procedure (gfc_symtree* stree)
12489 gfc_symbol* proc;
12490 locus where;
12491 gfc_symbol* me_arg;
12492 gfc_symbol* super_type;
12493 gfc_component* comp;
12495 gcc_assert (stree);
12497 /* Undefined specific symbol from GENERIC target definition. */
12498 if (!stree->n.tb)
12499 return;
12501 if (stree->n.tb->error)
12502 return;
12504 /* If this is a GENERIC binding, use that routine. */
12505 if (stree->n.tb->is_generic)
12507 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
12508 goto error;
12509 return;
12512 /* Get the target-procedure to check it. */
12513 gcc_assert (!stree->n.tb->is_generic);
12514 gcc_assert (stree->n.tb->u.specific);
12515 proc = stree->n.tb->u.specific->n.sym;
12516 where = stree->n.tb->where;
12518 /* Default access should already be resolved from the parser. */
12519 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
12521 if (stree->n.tb->deferred)
12523 if (!check_proc_interface (proc, &where))
12524 goto error;
12526 else
12528 /* Check for F08:C465. */
12529 if ((!proc->attr.subroutine && !proc->attr.function)
12530 || (proc->attr.proc != PROC_MODULE
12531 && proc->attr.if_source != IFSRC_IFBODY)
12532 || proc->attr.abstract)
12534 gfc_error ("%qs must be a module procedure or an external procedure with"
12535 " an explicit interface at %L", proc->name, &where);
12536 goto error;
12540 stree->n.tb->subroutine = proc->attr.subroutine;
12541 stree->n.tb->function = proc->attr.function;
12543 /* Find the super-type of the current derived type. We could do this once and
12544 store in a global if speed is needed, but as long as not I believe this is
12545 more readable and clearer. */
12546 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12548 /* If PASS, resolve and check arguments if not already resolved / loaded
12549 from a .mod file. */
12550 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
12552 gfc_formal_arglist *dummy_args;
12554 dummy_args = gfc_sym_get_dummy_args (proc);
12555 if (stree->n.tb->pass_arg)
12557 gfc_formal_arglist *i;
12559 /* If an explicit passing argument name is given, walk the arg-list
12560 and look for it. */
12562 me_arg = NULL;
12563 stree->n.tb->pass_arg_num = 1;
12564 for (i = dummy_args; i; i = i->next)
12566 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
12568 me_arg = i->sym;
12569 break;
12571 ++stree->n.tb->pass_arg_num;
12574 if (!me_arg)
12576 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
12577 " argument %qs",
12578 proc->name, stree->n.tb->pass_arg, &where,
12579 stree->n.tb->pass_arg);
12580 goto error;
12583 else
12585 /* Otherwise, take the first one; there should in fact be at least
12586 one. */
12587 stree->n.tb->pass_arg_num = 1;
12588 if (!dummy_args)
12590 gfc_error ("Procedure %qs with PASS at %L must have at"
12591 " least one argument", proc->name, &where);
12592 goto error;
12594 me_arg = dummy_args->sym;
12597 /* Now check that the argument-type matches and the passed-object
12598 dummy argument is generally fine. */
12600 gcc_assert (me_arg);
12602 if (me_arg->ts.type != BT_CLASS)
12604 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
12605 " at %L", proc->name, &where);
12606 goto error;
12609 if (CLASS_DATA (me_arg)->ts.u.derived
12610 != resolve_bindings_derived)
12612 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
12613 " the derived-type %qs", me_arg->name, proc->name,
12614 me_arg->name, &where, resolve_bindings_derived->name);
12615 goto error;
12618 gcc_assert (me_arg->ts.type == BT_CLASS);
12619 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
12621 gfc_error ("Passed-object dummy argument of %qs at %L must be"
12622 " scalar", proc->name, &where);
12623 goto error;
12625 if (CLASS_DATA (me_arg)->attr.allocatable)
12627 gfc_error ("Passed-object dummy argument of %qs at %L must not"
12628 " be ALLOCATABLE", proc->name, &where);
12629 goto error;
12631 if (CLASS_DATA (me_arg)->attr.class_pointer)
12633 gfc_error ("Passed-object dummy argument of %qs at %L must not"
12634 " be POINTER", proc->name, &where);
12635 goto error;
12639 /* If we are extending some type, check that we don't override a procedure
12640 flagged NON_OVERRIDABLE. */
12641 stree->n.tb->overridden = NULL;
12642 if (super_type)
12644 gfc_symtree* overridden;
12645 overridden = gfc_find_typebound_proc (super_type, NULL,
12646 stree->name, true, NULL);
12648 if (overridden)
12650 if (overridden->n.tb)
12651 stree->n.tb->overridden = overridden->n.tb;
12653 if (!gfc_check_typebound_override (stree, overridden))
12654 goto error;
12658 /* See if there's a name collision with a component directly in this type. */
12659 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
12660 if (!strcmp (comp->name, stree->name))
12662 gfc_error ("Procedure %qs at %L has the same name as a component of"
12663 " %qs",
12664 stree->name, &where, resolve_bindings_derived->name);
12665 goto error;
12668 /* Try to find a name collision with an inherited component. */
12669 if (super_type && gfc_find_component (super_type, stree->name, true, true))
12671 gfc_error ("Procedure %qs at %L has the same name as an inherited"
12672 " component of %qs",
12673 stree->name, &where, resolve_bindings_derived->name);
12674 goto error;
12677 stree->n.tb->error = 0;
12678 return;
12680 error:
12681 resolve_bindings_result = false;
12682 stree->n.tb->error = 1;
12686 static bool
12687 resolve_typebound_procedures (gfc_symbol* derived)
12689 int op;
12690 gfc_symbol* super_type;
12692 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
12693 return true;
12695 super_type = gfc_get_derived_super_type (derived);
12696 if (super_type)
12697 resolve_symbol (super_type);
12699 resolve_bindings_derived = derived;
12700 resolve_bindings_result = true;
12702 if (derived->f2k_derived->tb_sym_root)
12703 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
12704 &resolve_typebound_procedure);
12706 if (derived->f2k_derived->tb_uop_root)
12707 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
12708 &resolve_typebound_user_op);
12710 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
12712 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
12713 if (p && !resolve_typebound_intrinsic_op (derived,
12714 (gfc_intrinsic_op)op, p))
12715 resolve_bindings_result = false;
12718 return resolve_bindings_result;
12722 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
12723 to give all identical derived types the same backend_decl. */
12724 static void
12725 add_dt_to_dt_list (gfc_symbol *derived)
12727 gfc_dt_list *dt_list;
12729 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
12730 if (derived == dt_list->derived)
12731 return;
12733 dt_list = gfc_get_dt_list ();
12734 dt_list->next = gfc_derived_types;
12735 dt_list->derived = derived;
12736 gfc_derived_types = dt_list;
12740 /* Ensure that a derived-type is really not abstract, meaning that every
12741 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
12743 static bool
12744 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
12746 if (!st)
12747 return true;
12749 if (!ensure_not_abstract_walker (sub, st->left))
12750 return false;
12751 if (!ensure_not_abstract_walker (sub, st->right))
12752 return false;
12754 if (st->n.tb && st->n.tb->deferred)
12756 gfc_symtree* overriding;
12757 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
12758 if (!overriding)
12759 return false;
12760 gcc_assert (overriding->n.tb);
12761 if (overriding->n.tb->deferred)
12763 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
12764 " %qs is DEFERRED and not overridden",
12765 sub->name, &sub->declared_at, st->name);
12766 return false;
12770 return true;
12773 static bool
12774 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
12776 /* The algorithm used here is to recursively travel up the ancestry of sub
12777 and for each ancestor-type, check all bindings. If any of them is
12778 DEFERRED, look it up starting from sub and see if the found (overriding)
12779 binding is not DEFERRED.
12780 This is not the most efficient way to do this, but it should be ok and is
12781 clearer than something sophisticated. */
12783 gcc_assert (ancestor && !sub->attr.abstract);
12785 if (!ancestor->attr.abstract)
12786 return true;
12788 /* Walk bindings of this ancestor. */
12789 if (ancestor->f2k_derived)
12791 bool t;
12792 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
12793 if (!t)
12794 return false;
12797 /* Find next ancestor type and recurse on it. */
12798 ancestor = gfc_get_derived_super_type (ancestor);
12799 if (ancestor)
12800 return ensure_not_abstract (sub, ancestor);
12802 return true;
12806 /* This check for typebound defined assignments is done recursively
12807 since the order in which derived types are resolved is not always in
12808 order of the declarations. */
12810 static void
12811 check_defined_assignments (gfc_symbol *derived)
12813 gfc_component *c;
12815 for (c = derived->components; c; c = c->next)
12817 if (c->ts.type != BT_DERIVED
12818 || c->attr.pointer
12819 || c->attr.allocatable
12820 || c->attr.proc_pointer_comp
12821 || c->attr.class_pointer
12822 || c->attr.proc_pointer)
12823 continue;
12825 if (c->ts.u.derived->attr.defined_assign_comp
12826 || (c->ts.u.derived->f2k_derived
12827 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
12829 derived->attr.defined_assign_comp = 1;
12830 return;
12833 check_defined_assignments (c->ts.u.derived);
12834 if (c->ts.u.derived->attr.defined_assign_comp)
12836 derived->attr.defined_assign_comp = 1;
12837 return;
12843 /* Resolve the components of a derived type. This does not have to wait until
12844 resolution stage, but can be done as soon as the dt declaration has been
12845 parsed. */
12847 static bool
12848 resolve_fl_derived0 (gfc_symbol *sym)
12850 gfc_symbol* super_type;
12851 gfc_component *c;
12853 if (sym->attr.unlimited_polymorphic)
12854 return true;
12856 super_type = gfc_get_derived_super_type (sym);
12858 /* F2008, C432. */
12859 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
12861 gfc_error ("As extending type %qs at %L has a coarray component, "
12862 "parent type %qs shall also have one", sym->name,
12863 &sym->declared_at, super_type->name);
12864 return false;
12867 /* Ensure the extended type gets resolved before we do. */
12868 if (super_type && !resolve_fl_derived0 (super_type))
12869 return false;
12871 /* An ABSTRACT type must be extensible. */
12872 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
12874 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
12875 sym->name, &sym->declared_at);
12876 return false;
12879 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
12880 : sym->components;
12882 bool success = true;
12884 for ( ; c != NULL; c = c->next)
12886 if (c->attr.artificial)
12887 continue;
12889 /* F2008, C442. */
12890 if ((!sym->attr.is_class || c != sym->components)
12891 && c->attr.codimension
12892 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
12894 gfc_error ("Coarray component %qs at %L must be allocatable with "
12895 "deferred shape", c->name, &c->loc);
12896 success = false;
12897 continue;
12900 /* F2008, C443. */
12901 if (c->attr.codimension && c->ts.type == BT_DERIVED
12902 && c->ts.u.derived->ts.is_iso_c)
12904 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12905 "shall not be a coarray", c->name, &c->loc);
12906 success = false;
12907 continue;
12910 /* F2008, C444. */
12911 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
12912 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12913 || c->attr.allocatable))
12915 gfc_error ("Component %qs at %L with coarray component "
12916 "shall be a nonpointer, nonallocatable scalar",
12917 c->name, &c->loc);
12918 success = false;
12919 continue;
12922 /* F2008, C448. */
12923 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12925 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
12926 "is not an array pointer", c->name, &c->loc);
12927 success = false;
12928 continue;
12931 if (c->attr.proc_pointer && c->ts.interface)
12933 gfc_symbol *ifc = c->ts.interface;
12935 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
12937 c->tb->error = 1;
12938 success = false;
12939 continue;
12942 if (ifc->attr.if_source || ifc->attr.intrinsic)
12944 /* Resolve interface and copy attributes. */
12945 if (ifc->formal && !ifc->formal_ns)
12946 resolve_symbol (ifc);
12947 if (ifc->attr.intrinsic)
12948 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
12950 if (ifc->result)
12952 c->ts = ifc->result->ts;
12953 c->attr.allocatable = ifc->result->attr.allocatable;
12954 c->attr.pointer = ifc->result->attr.pointer;
12955 c->attr.dimension = ifc->result->attr.dimension;
12956 c->as = gfc_copy_array_spec (ifc->result->as);
12957 c->attr.class_ok = ifc->result->attr.class_ok;
12959 else
12961 c->ts = ifc->ts;
12962 c->attr.allocatable = ifc->attr.allocatable;
12963 c->attr.pointer = ifc->attr.pointer;
12964 c->attr.dimension = ifc->attr.dimension;
12965 c->as = gfc_copy_array_spec (ifc->as);
12966 c->attr.class_ok = ifc->attr.class_ok;
12968 c->ts.interface = ifc;
12969 c->attr.function = ifc->attr.function;
12970 c->attr.subroutine = ifc->attr.subroutine;
12972 c->attr.pure = ifc->attr.pure;
12973 c->attr.elemental = ifc->attr.elemental;
12974 c->attr.recursive = ifc->attr.recursive;
12975 c->attr.always_explicit = ifc->attr.always_explicit;
12976 c->attr.ext_attr |= ifc->attr.ext_attr;
12977 /* Copy char length. */
12978 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
12980 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
12981 if (cl->length && !cl->resolved
12982 && !gfc_resolve_expr (cl->length))
12984 c->tb->error = 1;
12985 success = false;
12986 continue;
12988 c->ts.u.cl = cl;
12992 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
12994 /* Since PPCs are not implicitly typed, a PPC without an explicit
12995 interface must be a subroutine. */
12996 gfc_add_subroutine (&c->attr, c->name, &c->loc);
12999 /* Procedure pointer components: Check PASS arg. */
13000 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
13001 && !sym->attr.vtype)
13003 gfc_symbol* me_arg;
13005 if (c->tb->pass_arg)
13007 gfc_formal_arglist* i;
13009 /* If an explicit passing argument name is given, walk the arg-list
13010 and look for it. */
13012 me_arg = NULL;
13013 c->tb->pass_arg_num = 1;
13014 for (i = c->ts.interface->formal; i; i = i->next)
13016 if (!strcmp (i->sym->name, c->tb->pass_arg))
13018 me_arg = i->sym;
13019 break;
13021 c->tb->pass_arg_num++;
13024 if (!me_arg)
13026 gfc_error ("Procedure pointer component %qs with PASS(%s) "
13027 "at %L has no argument %qs", c->name,
13028 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
13029 c->tb->error = 1;
13030 success = false;
13031 continue;
13034 else
13036 /* Otherwise, take the first one; there should in fact be at least
13037 one. */
13038 c->tb->pass_arg_num = 1;
13039 if (!c->ts.interface->formal)
13041 gfc_error ("Procedure pointer component %qs with PASS at %L "
13042 "must have at least one argument",
13043 c->name, &c->loc);
13044 c->tb->error = 1;
13045 success = false;
13046 continue;
13048 me_arg = c->ts.interface->formal->sym;
13051 /* Now check that the argument-type matches. */
13052 gcc_assert (me_arg);
13053 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
13054 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
13055 || (me_arg->ts.type == BT_CLASS
13056 && CLASS_DATA (me_arg)->ts.u.derived != sym))
13058 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
13059 " the derived type %qs", me_arg->name, c->name,
13060 me_arg->name, &c->loc, sym->name);
13061 c->tb->error = 1;
13062 success = false;
13063 continue;
13066 /* Check for C453. */
13067 if (me_arg->attr.dimension)
13069 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13070 "must be scalar", me_arg->name, c->name, me_arg->name,
13071 &c->loc);
13072 c->tb->error = 1;
13073 success = false;
13074 continue;
13077 if (me_arg->attr.pointer)
13079 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13080 "may not have the POINTER attribute", me_arg->name,
13081 c->name, me_arg->name, &c->loc);
13082 c->tb->error = 1;
13083 success = false;
13084 continue;
13087 if (me_arg->attr.allocatable)
13089 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13090 "may not be ALLOCATABLE", me_arg->name, c->name,
13091 me_arg->name, &c->loc);
13092 c->tb->error = 1;
13093 success = false;
13094 continue;
13097 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
13099 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
13100 " at %L", c->name, &c->loc);
13101 success = false;
13102 continue;
13107 /* Check type-spec if this is not the parent-type component. */
13108 if (((sym->attr.is_class
13109 && (!sym->components->ts.u.derived->attr.extension
13110 || c != sym->components->ts.u.derived->components))
13111 || (!sym->attr.is_class
13112 && (!sym->attr.extension || c != sym->components)))
13113 && !sym->attr.vtype
13114 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
13115 return false;
13117 /* If this type is an extension, set the accessibility of the parent
13118 component. */
13119 if (super_type
13120 && ((sym->attr.is_class
13121 && c == sym->components->ts.u.derived->components)
13122 || (!sym->attr.is_class && c == sym->components))
13123 && strcmp (super_type->name, c->name) == 0)
13124 c->attr.access = super_type->attr.access;
13126 /* If this type is an extension, see if this component has the same name
13127 as an inherited type-bound procedure. */
13128 if (super_type && !sym->attr.is_class
13129 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
13131 gfc_error ("Component %qs of %qs at %L has the same name as an"
13132 " inherited type-bound procedure",
13133 c->name, sym->name, &c->loc);
13134 return false;
13137 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
13138 && !c->ts.deferred)
13140 if (c->ts.u.cl->length == NULL
13141 || (!resolve_charlen(c->ts.u.cl))
13142 || !gfc_is_constant_expr (c->ts.u.cl->length))
13144 gfc_error ("Character length of component %qs needs to "
13145 "be a constant specification expression at %L",
13146 c->name,
13147 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
13148 return false;
13152 if (c->ts.type == BT_CHARACTER && c->ts.deferred
13153 && !c->attr.pointer && !c->attr.allocatable)
13155 gfc_error ("Character component %qs of %qs at %L with deferred "
13156 "length must be a POINTER or ALLOCATABLE",
13157 c->name, sym->name, &c->loc);
13158 return false;
13161 /* Add the hidden deferred length field. */
13162 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function
13163 && !sym->attr.is_class)
13165 char name[GFC_MAX_SYMBOL_LEN+9];
13166 gfc_component *strlen;
13167 sprintf (name, "_%s_length", c->name);
13168 strlen = gfc_find_component (sym, name, true, true);
13169 if (strlen == NULL)
13171 if (!gfc_add_component (sym, name, &strlen))
13172 return false;
13173 strlen->ts.type = BT_INTEGER;
13174 strlen->ts.kind = gfc_charlen_int_kind;
13175 strlen->attr.access = ACCESS_PRIVATE;
13176 strlen->attr.artificial = 1;
13180 if (c->ts.type == BT_DERIVED
13181 && sym->component_access != ACCESS_PRIVATE
13182 && gfc_check_symbol_access (sym)
13183 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
13184 && !c->ts.u.derived->attr.use_assoc
13185 && !gfc_check_symbol_access (c->ts.u.derived)
13186 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
13187 "PRIVATE type and cannot be a component of "
13188 "%qs, which is PUBLIC at %L", c->name,
13189 sym->name, &sym->declared_at))
13190 return false;
13192 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
13194 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
13195 "type %s", c->name, &c->loc, sym->name);
13196 return false;
13199 if (sym->attr.sequence)
13201 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
13203 gfc_error ("Component %s of SEQUENCE type declared at %L does "
13204 "not have the SEQUENCE attribute",
13205 c->ts.u.derived->name, &sym->declared_at);
13206 return false;
13210 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
13211 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
13212 else if (c->ts.type == BT_CLASS && c->attr.class_ok
13213 && CLASS_DATA (c)->ts.u.derived->attr.generic)
13214 CLASS_DATA (c)->ts.u.derived
13215 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
13217 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
13218 && c->attr.pointer && c->ts.u.derived->components == NULL
13219 && !c->ts.u.derived->attr.zero_comp)
13221 gfc_error ("The pointer component %qs of %qs at %L is a type "
13222 "that has not been declared", c->name, sym->name,
13223 &c->loc);
13224 return false;
13227 if (c->ts.type == BT_CLASS && c->attr.class_ok
13228 && CLASS_DATA (c)->attr.class_pointer
13229 && CLASS_DATA (c)->ts.u.derived->components == NULL
13230 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
13231 && !UNLIMITED_POLY (c))
13233 gfc_error ("The pointer component %qs of %qs at %L is a type "
13234 "that has not been declared", c->name, sym->name,
13235 &c->loc);
13236 return false;
13239 /* C437. */
13240 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
13241 && (!c->attr.class_ok
13242 || !(CLASS_DATA (c)->attr.class_pointer
13243 || CLASS_DATA (c)->attr.allocatable)))
13245 gfc_error ("Component %qs with CLASS at %L must be allocatable "
13246 "or pointer", c->name, &c->loc);
13247 /* Prevent a recurrence of the error. */
13248 c->ts.type = BT_UNKNOWN;
13249 return false;
13252 /* Ensure that all the derived type components are put on the
13253 derived type list; even in formal namespaces, where derived type
13254 pointer components might not have been declared. */
13255 if (c->ts.type == BT_DERIVED
13256 && c->ts.u.derived
13257 && c->ts.u.derived->components
13258 && c->attr.pointer
13259 && sym != c->ts.u.derived)
13260 add_dt_to_dt_list (c->ts.u.derived);
13262 if (!gfc_resolve_array_spec (c->as,
13263 !(c->attr.pointer || c->attr.proc_pointer
13264 || c->attr.allocatable)))
13265 return false;
13267 if (c->initializer && !sym->attr.vtype
13268 && !gfc_check_assign_symbol (sym, c, c->initializer))
13269 return false;
13272 if (!success)
13273 return false;
13275 check_defined_assignments (sym);
13277 if (!sym->attr.defined_assign_comp && super_type)
13278 sym->attr.defined_assign_comp
13279 = super_type->attr.defined_assign_comp;
13281 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
13282 all DEFERRED bindings are overridden. */
13283 if (super_type && super_type->attr.abstract && !sym->attr.abstract
13284 && !sym->attr.is_class
13285 && !ensure_not_abstract (sym, super_type))
13286 return false;
13288 /* Add derived type to the derived type list. */
13289 add_dt_to_dt_list (sym);
13291 return true;
13295 /* The following procedure does the full resolution of a derived type,
13296 including resolution of all type-bound procedures (if present). In contrast
13297 to 'resolve_fl_derived0' this can only be done after the module has been
13298 parsed completely. */
13300 static bool
13301 resolve_fl_derived (gfc_symbol *sym)
13303 gfc_symbol *gen_dt = NULL;
13305 if (sym->attr.unlimited_polymorphic)
13306 return true;
13308 if (!sym->attr.is_class)
13309 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
13310 if (gen_dt && gen_dt->generic && gen_dt->generic->next
13311 && (!gen_dt->generic->sym->attr.use_assoc
13312 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
13313 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
13314 "%qs at %L being the same name as derived "
13315 "type at %L", sym->name,
13316 gen_dt->generic->sym == sym
13317 ? gen_dt->generic->next->sym->name
13318 : gen_dt->generic->sym->name,
13319 gen_dt->generic->sym == sym
13320 ? &gen_dt->generic->next->sym->declared_at
13321 : &gen_dt->generic->sym->declared_at,
13322 &sym->declared_at))
13323 return false;
13325 /* Resolve the finalizer procedures. */
13326 if (!gfc_resolve_finalizers (sym, NULL))
13327 return false;
13329 if (sym->attr.is_class && sym->ts.u.derived == NULL)
13331 /* Fix up incomplete CLASS symbols. */
13332 gfc_component *data = gfc_find_component (sym, "_data", true, true);
13333 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
13335 /* Nothing more to do for unlimited polymorphic entities. */
13336 if (data->ts.u.derived->attr.unlimited_polymorphic)
13337 return true;
13338 else if (vptr->ts.u.derived == NULL)
13340 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
13341 gcc_assert (vtab);
13342 vptr->ts.u.derived = vtab->ts.u.derived;
13346 if (!resolve_fl_derived0 (sym))
13347 return false;
13349 /* Resolve the type-bound procedures. */
13350 if (!resolve_typebound_procedures (sym))
13351 return false;
13353 return true;
13357 static bool
13358 resolve_fl_namelist (gfc_symbol *sym)
13360 gfc_namelist *nl;
13361 gfc_symbol *nlsym;
13363 for (nl = sym->namelist; nl; nl = nl->next)
13365 /* Check again, the check in match only works if NAMELIST comes
13366 after the decl. */
13367 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
13369 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
13370 "allowed", nl->sym->name, sym->name, &sym->declared_at);
13371 return false;
13374 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
13375 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13376 "with assumed shape in namelist %qs at %L",
13377 nl->sym->name, sym->name, &sym->declared_at))
13378 return false;
13380 if (is_non_constant_shape_array (nl->sym)
13381 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13382 "with nonconstant shape in namelist %qs at %L",
13383 nl->sym->name, sym->name, &sym->declared_at))
13384 return false;
13386 if (nl->sym->ts.type == BT_CHARACTER
13387 && (nl->sym->ts.u.cl->length == NULL
13388 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
13389 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
13390 "nonconstant character length in "
13391 "namelist %qs at %L", nl->sym->name,
13392 sym->name, &sym->declared_at))
13393 return false;
13395 /* FIXME: Once UDDTIO is implemented, the following can be
13396 removed. */
13397 if (nl->sym->ts.type == BT_CLASS)
13399 gfc_error ("NAMELIST object %qs in namelist %qs at %L is "
13400 "polymorphic and requires a defined input/output "
13401 "procedure", nl->sym->name, sym->name, &sym->declared_at);
13402 return false;
13405 if (nl->sym->ts.type == BT_DERIVED
13406 && (nl->sym->ts.u.derived->attr.alloc_comp
13407 || nl->sym->ts.u.derived->attr.pointer_comp))
13409 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
13410 "namelist %qs at %L with ALLOCATABLE "
13411 "or POINTER components", nl->sym->name,
13412 sym->name, &sym->declared_at))
13413 return false;
13415 /* FIXME: Once UDDTIO is implemented, the following can be
13416 removed. */
13417 gfc_error ("NAMELIST object %qs in namelist %qs at %L has "
13418 "ALLOCATABLE or POINTER components and thus requires "
13419 "a defined input/output procedure", nl->sym->name,
13420 sym->name, &sym->declared_at);
13421 return false;
13425 /* Reject PRIVATE objects in a PUBLIC namelist. */
13426 if (gfc_check_symbol_access (sym))
13428 for (nl = sym->namelist; nl; nl = nl->next)
13430 if (!nl->sym->attr.use_assoc
13431 && !is_sym_host_assoc (nl->sym, sym->ns)
13432 && !gfc_check_symbol_access (nl->sym))
13434 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
13435 "cannot be member of PUBLIC namelist %qs at %L",
13436 nl->sym->name, sym->name, &sym->declared_at);
13437 return false;
13440 /* Types with private components that came here by USE-association. */
13441 if (nl->sym->ts.type == BT_DERIVED
13442 && derived_inaccessible (nl->sym->ts.u.derived))
13444 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
13445 "components and cannot be member of namelist %qs at %L",
13446 nl->sym->name, sym->name, &sym->declared_at);
13447 return false;
13450 /* Types with private components that are defined in the same module. */
13451 if (nl->sym->ts.type == BT_DERIVED
13452 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
13453 && nl->sym->ts.u.derived->attr.private_comp)
13455 gfc_error ("NAMELIST object %qs has PRIVATE components and "
13456 "cannot be a member of PUBLIC namelist %qs at %L",
13457 nl->sym->name, sym->name, &sym->declared_at);
13458 return false;
13464 /* 14.1.2 A module or internal procedure represent local entities
13465 of the same type as a namelist member and so are not allowed. */
13466 for (nl = sym->namelist; nl; nl = nl->next)
13468 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
13469 continue;
13471 if (nl->sym->attr.function && nl->sym == nl->sym->result)
13472 if ((nl->sym == sym->ns->proc_name)
13474 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
13475 continue;
13477 nlsym = NULL;
13478 if (nl->sym->name)
13479 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
13480 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
13482 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
13483 "attribute in %qs at %L", nlsym->name,
13484 &sym->declared_at);
13485 return false;
13489 return true;
13493 static bool
13494 resolve_fl_parameter (gfc_symbol *sym)
13496 /* A parameter array's shape needs to be constant. */
13497 if (sym->as != NULL
13498 && (sym->as->type == AS_DEFERRED
13499 || is_non_constant_shape_array (sym)))
13501 gfc_error ("Parameter array %qs at %L cannot be automatic "
13502 "or of deferred shape", sym->name, &sym->declared_at);
13503 return false;
13506 /* Make sure a parameter that has been implicitly typed still
13507 matches the implicit type, since PARAMETER statements can precede
13508 IMPLICIT statements. */
13509 if (sym->attr.implicit_type
13510 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
13511 sym->ns)))
13513 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
13514 "later IMPLICIT type", sym->name, &sym->declared_at);
13515 return false;
13518 /* Make sure the types of derived parameters are consistent. This
13519 type checking is deferred until resolution because the type may
13520 refer to a derived type from the host. */
13521 if (sym->ts.type == BT_DERIVED
13522 && !gfc_compare_types (&sym->ts, &sym->value->ts))
13524 gfc_error ("Incompatible derived type in PARAMETER at %L",
13525 &sym->value->where);
13526 return false;
13528 return true;
13532 /* Do anything necessary to resolve a symbol. Right now, we just
13533 assume that an otherwise unknown symbol is a variable. This sort
13534 of thing commonly happens for symbols in module. */
13536 static void
13537 resolve_symbol (gfc_symbol *sym)
13539 int check_constant, mp_flag;
13540 gfc_symtree *symtree;
13541 gfc_symtree *this_symtree;
13542 gfc_namespace *ns;
13543 gfc_component *c;
13544 symbol_attribute class_attr;
13545 gfc_array_spec *as;
13546 bool saved_specification_expr;
13548 if (sym->resolved)
13549 return;
13550 sym->resolved = 1;
13552 if (sym->attr.artificial)
13553 return;
13555 if (sym->attr.unlimited_polymorphic)
13556 return;
13558 if (sym->attr.flavor == FL_UNKNOWN
13559 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
13560 && !sym->attr.generic && !sym->attr.external
13561 && sym->attr.if_source == IFSRC_UNKNOWN
13562 && sym->ts.type == BT_UNKNOWN))
13565 /* If we find that a flavorless symbol is an interface in one of the
13566 parent namespaces, find its symtree in this namespace, free the
13567 symbol and set the symtree to point to the interface symbol. */
13568 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
13570 symtree = gfc_find_symtree (ns->sym_root, sym->name);
13571 if (symtree && (symtree->n.sym->generic ||
13572 (symtree->n.sym->attr.flavor == FL_PROCEDURE
13573 && sym->ns->construct_entities)))
13575 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
13576 sym->name);
13577 if (this_symtree->n.sym == sym)
13579 symtree->n.sym->refs++;
13580 gfc_release_symbol (sym);
13581 this_symtree->n.sym = symtree->n.sym;
13582 return;
13587 /* Otherwise give it a flavor according to such attributes as
13588 it has. */
13589 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
13590 && sym->attr.intrinsic == 0)
13591 sym->attr.flavor = FL_VARIABLE;
13592 else if (sym->attr.flavor == FL_UNKNOWN)
13594 sym->attr.flavor = FL_PROCEDURE;
13595 if (sym->attr.dimension)
13596 sym->attr.function = 1;
13600 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
13601 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
13603 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
13604 && !resolve_procedure_interface (sym))
13605 return;
13607 if (sym->attr.is_protected && !sym->attr.proc_pointer
13608 && (sym->attr.procedure || sym->attr.external))
13610 if (sym->attr.external)
13611 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
13612 "at %L", &sym->declared_at);
13613 else
13614 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
13615 "at %L", &sym->declared_at);
13617 return;
13620 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
13621 return;
13623 /* Symbols that are module procedures with results (functions) have
13624 the types and array specification copied for type checking in
13625 procedures that call them, as well as for saving to a module
13626 file. These symbols can't stand the scrutiny that their results
13627 can. */
13628 mp_flag = (sym->result != NULL && sym->result != sym);
13630 /* Make sure that the intrinsic is consistent with its internal
13631 representation. This needs to be done before assigning a default
13632 type to avoid spurious warnings. */
13633 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
13634 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
13635 return;
13637 /* Resolve associate names. */
13638 if (sym->assoc)
13639 resolve_assoc_var (sym, true);
13641 /* Assign default type to symbols that need one and don't have one. */
13642 if (sym->ts.type == BT_UNKNOWN)
13644 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
13646 gfc_set_default_type (sym, 1, NULL);
13649 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
13650 && !sym->attr.function && !sym->attr.subroutine
13651 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
13652 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
13654 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
13656 /* The specific case of an external procedure should emit an error
13657 in the case that there is no implicit type. */
13658 if (!mp_flag)
13659 gfc_set_default_type (sym, sym->attr.external, NULL);
13660 else
13662 /* Result may be in another namespace. */
13663 resolve_symbol (sym->result);
13665 if (!sym->result->attr.proc_pointer)
13667 sym->ts = sym->result->ts;
13668 sym->as = gfc_copy_array_spec (sym->result->as);
13669 sym->attr.dimension = sym->result->attr.dimension;
13670 sym->attr.pointer = sym->result->attr.pointer;
13671 sym->attr.allocatable = sym->result->attr.allocatable;
13672 sym->attr.contiguous = sym->result->attr.contiguous;
13677 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
13679 bool saved_specification_expr = specification_expr;
13680 specification_expr = true;
13681 gfc_resolve_array_spec (sym->result->as, false);
13682 specification_expr = saved_specification_expr;
13685 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
13687 as = CLASS_DATA (sym)->as;
13688 class_attr = CLASS_DATA (sym)->attr;
13689 class_attr.pointer = class_attr.class_pointer;
13691 else
13693 class_attr = sym->attr;
13694 as = sym->as;
13697 /* F2008, C530. */
13698 if (sym->attr.contiguous
13699 && (!class_attr.dimension
13700 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
13701 && !class_attr.pointer)))
13703 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
13704 "array pointer or an assumed-shape or assumed-rank array",
13705 sym->name, &sym->declared_at);
13706 return;
13709 /* Assumed size arrays and assumed shape arrays must be dummy
13710 arguments. Array-spec's of implied-shape should have been resolved to
13711 AS_EXPLICIT already. */
13713 if (as)
13715 gcc_assert (as->type != AS_IMPLIED_SHAPE);
13716 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
13717 || as->type == AS_ASSUMED_SHAPE)
13718 && !sym->attr.dummy && !sym->attr.select_type_temporary)
13720 if (as->type == AS_ASSUMED_SIZE)
13721 gfc_error ("Assumed size array at %L must be a dummy argument",
13722 &sym->declared_at);
13723 else
13724 gfc_error ("Assumed shape array at %L must be a dummy argument",
13725 &sym->declared_at);
13726 return;
13728 /* TS 29113, C535a. */
13729 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
13730 && !sym->attr.select_type_temporary)
13732 gfc_error ("Assumed-rank array at %L must be a dummy argument",
13733 &sym->declared_at);
13734 return;
13736 if (as->type == AS_ASSUMED_RANK
13737 && (sym->attr.codimension || sym->attr.value))
13739 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
13740 "CODIMENSION attribute", &sym->declared_at);
13741 return;
13745 /* Make sure symbols with known intent or optional are really dummy
13746 variable. Because of ENTRY statement, this has to be deferred
13747 until resolution time. */
13749 if (!sym->attr.dummy
13750 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
13752 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
13753 return;
13756 if (sym->attr.value && !sym->attr.dummy)
13758 gfc_error ("%qs at %L cannot have the VALUE attribute because "
13759 "it is not a dummy argument", sym->name, &sym->declared_at);
13760 return;
13763 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
13765 gfc_charlen *cl = sym->ts.u.cl;
13766 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13768 gfc_error ("Character dummy variable %qs at %L with VALUE "
13769 "attribute must have constant length",
13770 sym->name, &sym->declared_at);
13771 return;
13774 if (sym->ts.is_c_interop
13775 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
13777 gfc_error ("C interoperable character dummy variable %qs at %L "
13778 "with VALUE attribute must have length one",
13779 sym->name, &sym->declared_at);
13780 return;
13784 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13785 && sym->ts.u.derived->attr.generic)
13787 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
13788 if (!sym->ts.u.derived)
13790 gfc_error ("The derived type %qs at %L is of type %qs, "
13791 "which has not been defined", sym->name,
13792 &sym->declared_at, sym->ts.u.derived->name);
13793 sym->ts.type = BT_UNKNOWN;
13794 return;
13798 /* Use the same constraints as TYPE(*), except for the type check
13799 and that only scalars and assumed-size arrays are permitted. */
13800 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
13802 if (!sym->attr.dummy)
13804 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13805 "a dummy argument", sym->name, &sym->declared_at);
13806 return;
13809 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
13810 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
13811 && sym->ts.type != BT_COMPLEX)
13813 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13814 "of type TYPE(*) or of an numeric intrinsic type",
13815 sym->name, &sym->declared_at);
13816 return;
13819 if (sym->attr.allocatable || sym->attr.codimension
13820 || sym->attr.pointer || sym->attr.value)
13822 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13823 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
13824 "attribute", sym->name, &sym->declared_at);
13825 return;
13828 if (sym->attr.intent == INTENT_OUT)
13830 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13831 "have the INTENT(OUT) attribute",
13832 sym->name, &sym->declared_at);
13833 return;
13835 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
13837 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
13838 "either be a scalar or an assumed-size array",
13839 sym->name, &sym->declared_at);
13840 return;
13843 /* Set the type to TYPE(*) and add a dimension(*) to ensure
13844 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
13845 packing. */
13846 sym->ts.type = BT_ASSUMED;
13847 sym->as = gfc_get_array_spec ();
13848 sym->as->type = AS_ASSUMED_SIZE;
13849 sym->as->rank = 1;
13850 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
13852 else if (sym->ts.type == BT_ASSUMED)
13854 /* TS 29113, C407a. */
13855 if (!sym->attr.dummy)
13857 gfc_error ("Assumed type of variable %s at %L is only permitted "
13858 "for dummy variables", sym->name, &sym->declared_at);
13859 return;
13861 if (sym->attr.allocatable || sym->attr.codimension
13862 || sym->attr.pointer || sym->attr.value)
13864 gfc_error ("Assumed-type variable %s at %L may not have the "
13865 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
13866 sym->name, &sym->declared_at);
13867 return;
13869 if (sym->attr.intent == INTENT_OUT)
13871 gfc_error ("Assumed-type variable %s at %L may not have the "
13872 "INTENT(OUT) attribute",
13873 sym->name, &sym->declared_at);
13874 return;
13876 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
13878 gfc_error ("Assumed-type variable %s at %L shall not be an "
13879 "explicit-shape array", sym->name, &sym->declared_at);
13880 return;
13884 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
13885 do this for something that was implicitly typed because that is handled
13886 in gfc_set_default_type. Handle dummy arguments and procedure
13887 definitions separately. Also, anything that is use associated is not
13888 handled here but instead is handled in the module it is declared in.
13889 Finally, derived type definitions are allowed to be BIND(C) since that
13890 only implies that they're interoperable, and they are checked fully for
13891 interoperability when a variable is declared of that type. */
13892 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
13893 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
13894 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
13896 bool t = true;
13898 /* First, make sure the variable is declared at the
13899 module-level scope (J3/04-007, Section 15.3). */
13900 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
13901 sym->attr.in_common == 0)
13903 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
13904 "is neither a COMMON block nor declared at the "
13905 "module level scope", sym->name, &(sym->declared_at));
13906 t = false;
13908 else if (sym->common_head != NULL)
13910 t = verify_com_block_vars_c_interop (sym->common_head);
13912 else
13914 /* If type() declaration, we need to verify that the components
13915 of the given type are all C interoperable, etc. */
13916 if (sym->ts.type == BT_DERIVED &&
13917 sym->ts.u.derived->attr.is_c_interop != 1)
13919 /* Make sure the user marked the derived type as BIND(C). If
13920 not, call the verify routine. This could print an error
13921 for the derived type more than once if multiple variables
13922 of that type are declared. */
13923 if (sym->ts.u.derived->attr.is_bind_c != 1)
13924 verify_bind_c_derived_type (sym->ts.u.derived);
13925 t = false;
13928 /* Verify the variable itself as C interoperable if it
13929 is BIND(C). It is not possible for this to succeed if
13930 the verify_bind_c_derived_type failed, so don't have to handle
13931 any error returned by verify_bind_c_derived_type. */
13932 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13933 sym->common_block);
13936 if (!t)
13938 /* clear the is_bind_c flag to prevent reporting errors more than
13939 once if something failed. */
13940 sym->attr.is_bind_c = 0;
13941 return;
13945 /* If a derived type symbol has reached this point, without its
13946 type being declared, we have an error. Notice that most
13947 conditions that produce undefined derived types have already
13948 been dealt with. However, the likes of:
13949 implicit type(t) (t) ..... call foo (t) will get us here if
13950 the type is not declared in the scope of the implicit
13951 statement. Change the type to BT_UNKNOWN, both because it is so
13952 and to prevent an ICE. */
13953 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13954 && sym->ts.u.derived->components == NULL
13955 && !sym->ts.u.derived->attr.zero_comp)
13957 gfc_error ("The derived type %qs at %L is of type %qs, "
13958 "which has not been defined", sym->name,
13959 &sym->declared_at, sym->ts.u.derived->name);
13960 sym->ts.type = BT_UNKNOWN;
13961 return;
13964 /* Make sure that the derived type has been resolved and that the
13965 derived type is visible in the symbol's namespace, if it is a
13966 module function and is not PRIVATE. */
13967 if (sym->ts.type == BT_DERIVED
13968 && sym->ts.u.derived->attr.use_assoc
13969 && sym->ns->proc_name
13970 && sym->ns->proc_name->attr.flavor == FL_MODULE
13971 && !resolve_fl_derived (sym->ts.u.derived))
13972 return;
13974 /* Unless the derived-type declaration is use associated, Fortran 95
13975 does not allow public entries of private derived types.
13976 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
13977 161 in 95-006r3. */
13978 if (sym->ts.type == BT_DERIVED
13979 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
13980 && !sym->ts.u.derived->attr.use_assoc
13981 && gfc_check_symbol_access (sym)
13982 && !gfc_check_symbol_access (sym->ts.u.derived)
13983 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
13984 "derived type %qs",
13985 (sym->attr.flavor == FL_PARAMETER)
13986 ? "parameter" : "variable",
13987 sym->name, &sym->declared_at,
13988 sym->ts.u.derived->name))
13989 return;
13991 /* F2008, C1302. */
13992 if (sym->ts.type == BT_DERIVED
13993 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
13994 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
13995 || sym->ts.u.derived->attr.lock_comp)
13996 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
13998 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
13999 "type LOCK_TYPE must be a coarray", sym->name,
14000 &sym->declared_at);
14001 return;
14004 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
14005 default initialization is defined (5.1.2.4.4). */
14006 if (sym->ts.type == BT_DERIVED
14007 && sym->attr.dummy
14008 && sym->attr.intent == INTENT_OUT
14009 && sym->as
14010 && sym->as->type == AS_ASSUMED_SIZE)
14012 for (c = sym->ts.u.derived->components; c; c = c->next)
14014 if (c->initializer)
14016 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
14017 "ASSUMED SIZE and so cannot have a default initializer",
14018 sym->name, &sym->declared_at);
14019 return;
14024 /* F2008, C542. */
14025 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
14026 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
14028 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
14029 "INTENT(OUT)", sym->name, &sym->declared_at);
14030 return;
14033 /* F2008, C525. */
14034 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14035 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14036 && CLASS_DATA (sym)->attr.coarray_comp))
14037 || class_attr.codimension)
14038 && (sym->attr.result || sym->result == sym))
14040 gfc_error ("Function result %qs at %L shall not be a coarray or have "
14041 "a coarray component", sym->name, &sym->declared_at);
14042 return;
14045 /* F2008, C524. */
14046 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
14047 && sym->ts.u.derived->ts.is_iso_c)
14049 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14050 "shall not be a coarray", sym->name, &sym->declared_at);
14051 return;
14054 /* F2008, C525. */
14055 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14056 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14057 && CLASS_DATA (sym)->attr.coarray_comp))
14058 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
14059 || class_attr.allocatable))
14061 gfc_error ("Variable %qs at %L with coarray component shall be a "
14062 "nonpointer, nonallocatable scalar, which is not a coarray",
14063 sym->name, &sym->declared_at);
14064 return;
14067 /* F2008, C526. The function-result case was handled above. */
14068 if (class_attr.codimension
14069 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
14070 || sym->attr.select_type_temporary
14071 || sym->ns->save_all
14072 || sym->ns->proc_name->attr.flavor == FL_MODULE
14073 || sym->ns->proc_name->attr.is_main_program
14074 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
14076 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
14077 "nor a dummy argument", sym->name, &sym->declared_at);
14078 return;
14080 /* F2008, C528. */
14081 else if (class_attr.codimension && !sym->attr.select_type_temporary
14082 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
14084 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
14085 "deferred shape", sym->name, &sym->declared_at);
14086 return;
14088 else if (class_attr.codimension && class_attr.allocatable && as
14089 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
14091 gfc_error ("Allocatable coarray variable %qs at %L must have "
14092 "deferred shape", sym->name, &sym->declared_at);
14093 return;
14096 /* F2008, C541. */
14097 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14098 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14099 && CLASS_DATA (sym)->attr.coarray_comp))
14100 || (class_attr.codimension && class_attr.allocatable))
14101 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
14103 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
14104 "allocatable coarray or have coarray components",
14105 sym->name, &sym->declared_at);
14106 return;
14109 if (class_attr.codimension && sym->attr.dummy
14110 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
14112 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
14113 "procedure %qs", sym->name, &sym->declared_at,
14114 sym->ns->proc_name->name);
14115 return;
14118 if (sym->ts.type == BT_LOGICAL
14119 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
14120 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
14121 && sym->ns->proc_name->attr.is_bind_c)))
14123 int i;
14124 for (i = 0; gfc_logical_kinds[i].kind; i++)
14125 if (gfc_logical_kinds[i].kind == sym->ts.kind)
14126 break;
14127 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
14128 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
14129 "%L with non-C_Bool kind in BIND(C) procedure "
14130 "%qs", sym->name, &sym->declared_at,
14131 sym->ns->proc_name->name))
14132 return;
14133 else if (!gfc_logical_kinds[i].c_bool
14134 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
14135 "%qs at %L with non-C_Bool kind in "
14136 "BIND(C) procedure %qs", sym->name,
14137 &sym->declared_at,
14138 sym->attr.function ? sym->name
14139 : sym->ns->proc_name->name))
14140 return;
14143 switch (sym->attr.flavor)
14145 case FL_VARIABLE:
14146 if (!resolve_fl_variable (sym, mp_flag))
14147 return;
14148 break;
14150 case FL_PROCEDURE:
14151 if (!resolve_fl_procedure (sym, mp_flag))
14152 return;
14153 break;
14155 case FL_NAMELIST:
14156 if (!resolve_fl_namelist (sym))
14157 return;
14158 break;
14160 case FL_PARAMETER:
14161 if (!resolve_fl_parameter (sym))
14162 return;
14163 break;
14165 default:
14166 break;
14169 /* Resolve array specifier. Check as well some constraints
14170 on COMMON blocks. */
14172 check_constant = sym->attr.in_common && !sym->attr.pointer;
14174 /* Set the formal_arg_flag so that check_conflict will not throw
14175 an error for host associated variables in the specification
14176 expression for an array_valued function. */
14177 if (sym->attr.function && sym->as)
14178 formal_arg_flag = 1;
14180 saved_specification_expr = specification_expr;
14181 specification_expr = true;
14182 gfc_resolve_array_spec (sym->as, check_constant);
14183 specification_expr = saved_specification_expr;
14185 formal_arg_flag = 0;
14187 /* Resolve formal namespaces. */
14188 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
14189 && !sym->attr.contained && !sym->attr.intrinsic)
14190 gfc_resolve (sym->formal_ns);
14192 /* Make sure the formal namespace is present. */
14193 if (sym->formal && !sym->formal_ns)
14195 gfc_formal_arglist *formal = sym->formal;
14196 while (formal && !formal->sym)
14197 formal = formal->next;
14199 if (formal)
14201 sym->formal_ns = formal->sym->ns;
14202 if (sym->ns != formal->sym->ns)
14203 sym->formal_ns->refs++;
14207 /* Check threadprivate restrictions. */
14208 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
14209 && (!sym->attr.in_common
14210 && sym->module == NULL
14211 && (sym->ns->proc_name == NULL
14212 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
14213 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
14215 /* Check omp declare target restrictions. */
14216 if (sym->attr.omp_declare_target
14217 && sym->attr.flavor == FL_VARIABLE
14218 && !sym->attr.save
14219 && !sym->ns->save_all
14220 && (!sym->attr.in_common
14221 && sym->module == NULL
14222 && (sym->ns->proc_name == NULL
14223 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
14224 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
14225 sym->name, &sym->declared_at);
14227 /* If we have come this far we can apply default-initializers, as
14228 described in 14.7.5, to those variables that have not already
14229 been assigned one. */
14230 if (sym->ts.type == BT_DERIVED
14231 && !sym->value
14232 && !sym->attr.allocatable
14233 && !sym->attr.alloc_comp)
14235 symbol_attribute *a = &sym->attr;
14237 if ((!a->save && !a->dummy && !a->pointer
14238 && !a->in_common && !a->use_assoc
14239 && !a->result && !a->function)
14240 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
14241 apply_default_init (sym);
14242 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
14243 && (sym->ts.u.derived->attr.alloc_comp
14244 || sym->ts.u.derived->attr.pointer_comp))
14245 /* Mark the result symbol to be referenced, when it has allocatable
14246 components. */
14247 sym->result->attr.referenced = 1;
14250 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
14251 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
14252 && !CLASS_DATA (sym)->attr.class_pointer
14253 && !CLASS_DATA (sym)->attr.allocatable)
14254 apply_default_init (sym);
14256 /* If this symbol has a type-spec, check it. */
14257 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
14258 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
14259 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
14260 return;
14264 /************* Resolve DATA statements *************/
14266 static struct
14268 gfc_data_value *vnode;
14269 mpz_t left;
14271 values;
14274 /* Advance the values structure to point to the next value in the data list. */
14276 static bool
14277 next_data_value (void)
14279 while (mpz_cmp_ui (values.left, 0) == 0)
14282 if (values.vnode->next == NULL)
14283 return false;
14285 values.vnode = values.vnode->next;
14286 mpz_set (values.left, values.vnode->repeat);
14289 return true;
14293 static bool
14294 check_data_variable (gfc_data_variable *var, locus *where)
14296 gfc_expr *e;
14297 mpz_t size;
14298 mpz_t offset;
14299 bool t;
14300 ar_type mark = AR_UNKNOWN;
14301 int i;
14302 mpz_t section_index[GFC_MAX_DIMENSIONS];
14303 gfc_ref *ref;
14304 gfc_array_ref *ar;
14305 gfc_symbol *sym;
14306 int has_pointer;
14308 if (!gfc_resolve_expr (var->expr))
14309 return false;
14311 ar = NULL;
14312 mpz_init_set_si (offset, 0);
14313 e = var->expr;
14315 if (e->expr_type != EXPR_VARIABLE)
14316 gfc_internal_error ("check_data_variable(): Bad expression");
14318 sym = e->symtree->n.sym;
14320 if (sym->ns->is_block_data && !sym->attr.in_common)
14322 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
14323 sym->name, &sym->declared_at);
14326 if (e->ref == NULL && sym->as)
14328 gfc_error ("DATA array %qs at %L must be specified in a previous"
14329 " declaration", sym->name, where);
14330 return false;
14333 has_pointer = sym->attr.pointer;
14335 if (gfc_is_coindexed (e))
14337 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
14338 where);
14339 return false;
14342 for (ref = e->ref; ref; ref = ref->next)
14344 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
14345 has_pointer = 1;
14347 if (has_pointer
14348 && ref->type == REF_ARRAY
14349 && ref->u.ar.type != AR_FULL)
14351 gfc_error ("DATA element %qs at %L is a pointer and so must "
14352 "be a full array", sym->name, where);
14353 return false;
14357 if (e->rank == 0 || has_pointer)
14359 mpz_init_set_ui (size, 1);
14360 ref = NULL;
14362 else
14364 ref = e->ref;
14366 /* Find the array section reference. */
14367 for (ref = e->ref; ref; ref = ref->next)
14369 if (ref->type != REF_ARRAY)
14370 continue;
14371 if (ref->u.ar.type == AR_ELEMENT)
14372 continue;
14373 break;
14375 gcc_assert (ref);
14377 /* Set marks according to the reference pattern. */
14378 switch (ref->u.ar.type)
14380 case AR_FULL:
14381 mark = AR_FULL;
14382 break;
14384 case AR_SECTION:
14385 ar = &ref->u.ar;
14386 /* Get the start position of array section. */
14387 gfc_get_section_index (ar, section_index, &offset);
14388 mark = AR_SECTION;
14389 break;
14391 default:
14392 gcc_unreachable ();
14395 if (!gfc_array_size (e, &size))
14397 gfc_error ("Nonconstant array section at %L in DATA statement",
14398 &e->where);
14399 mpz_clear (offset);
14400 return false;
14404 t = true;
14406 while (mpz_cmp_ui (size, 0) > 0)
14408 if (!next_data_value ())
14410 gfc_error ("DATA statement at %L has more variables than values",
14411 where);
14412 t = false;
14413 break;
14416 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
14417 if (!t)
14418 break;
14420 /* If we have more than one element left in the repeat count,
14421 and we have more than one element left in the target variable,
14422 then create a range assignment. */
14423 /* FIXME: Only done for full arrays for now, since array sections
14424 seem tricky. */
14425 if (mark == AR_FULL && ref && ref->next == NULL
14426 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
14428 mpz_t range;
14430 if (mpz_cmp (size, values.left) >= 0)
14432 mpz_init_set (range, values.left);
14433 mpz_sub (size, size, values.left);
14434 mpz_set_ui (values.left, 0);
14436 else
14438 mpz_init_set (range, size);
14439 mpz_sub (values.left, values.left, size);
14440 mpz_set_ui (size, 0);
14443 t = gfc_assign_data_value (var->expr, values.vnode->expr,
14444 offset, &range);
14446 mpz_add (offset, offset, range);
14447 mpz_clear (range);
14449 if (!t)
14450 break;
14453 /* Assign initial value to symbol. */
14454 else
14456 mpz_sub_ui (values.left, values.left, 1);
14457 mpz_sub_ui (size, size, 1);
14459 t = gfc_assign_data_value (var->expr, values.vnode->expr,
14460 offset, NULL);
14461 if (!t)
14462 break;
14464 if (mark == AR_FULL)
14465 mpz_add_ui (offset, offset, 1);
14467 /* Modify the array section indexes and recalculate the offset
14468 for next element. */
14469 else if (mark == AR_SECTION)
14470 gfc_advance_section (section_index, ar, &offset);
14474 if (mark == AR_SECTION)
14476 for (i = 0; i < ar->dimen; i++)
14477 mpz_clear (section_index[i]);
14480 mpz_clear (size);
14481 mpz_clear (offset);
14483 return t;
14487 static bool traverse_data_var (gfc_data_variable *, locus *);
14489 /* Iterate over a list of elements in a DATA statement. */
14491 static bool
14492 traverse_data_list (gfc_data_variable *var, locus *where)
14494 mpz_t trip;
14495 iterator_stack frame;
14496 gfc_expr *e, *start, *end, *step;
14497 bool retval = true;
14499 mpz_init (frame.value);
14500 mpz_init (trip);
14502 start = gfc_copy_expr (var->iter.start);
14503 end = gfc_copy_expr (var->iter.end);
14504 step = gfc_copy_expr (var->iter.step);
14506 if (!gfc_simplify_expr (start, 1)
14507 || start->expr_type != EXPR_CONSTANT)
14509 gfc_error ("start of implied-do loop at %L could not be "
14510 "simplified to a constant value", &start->where);
14511 retval = false;
14512 goto cleanup;
14514 if (!gfc_simplify_expr (end, 1)
14515 || end->expr_type != EXPR_CONSTANT)
14517 gfc_error ("end of implied-do loop at %L could not be "
14518 "simplified to a constant value", &start->where);
14519 retval = false;
14520 goto cleanup;
14522 if (!gfc_simplify_expr (step, 1)
14523 || step->expr_type != EXPR_CONSTANT)
14525 gfc_error ("step of implied-do loop at %L could not be "
14526 "simplified to a constant value", &start->where);
14527 retval = false;
14528 goto cleanup;
14531 mpz_set (trip, end->value.integer);
14532 mpz_sub (trip, trip, start->value.integer);
14533 mpz_add (trip, trip, step->value.integer);
14535 mpz_div (trip, trip, step->value.integer);
14537 mpz_set (frame.value, start->value.integer);
14539 frame.prev = iter_stack;
14540 frame.variable = var->iter.var->symtree;
14541 iter_stack = &frame;
14543 while (mpz_cmp_ui (trip, 0) > 0)
14545 if (!traverse_data_var (var->list, where))
14547 retval = false;
14548 goto cleanup;
14551 e = gfc_copy_expr (var->expr);
14552 if (!gfc_simplify_expr (e, 1))
14554 gfc_free_expr (e);
14555 retval = false;
14556 goto cleanup;
14559 mpz_add (frame.value, frame.value, step->value.integer);
14561 mpz_sub_ui (trip, trip, 1);
14564 cleanup:
14565 mpz_clear (frame.value);
14566 mpz_clear (trip);
14568 gfc_free_expr (start);
14569 gfc_free_expr (end);
14570 gfc_free_expr (step);
14572 iter_stack = frame.prev;
14573 return retval;
14577 /* Type resolve variables in the variable list of a DATA statement. */
14579 static bool
14580 traverse_data_var (gfc_data_variable *var, locus *where)
14582 bool t;
14584 for (; var; var = var->next)
14586 if (var->expr == NULL)
14587 t = traverse_data_list (var, where);
14588 else
14589 t = check_data_variable (var, where);
14591 if (!t)
14592 return false;
14595 return true;
14599 /* Resolve the expressions and iterators associated with a data statement.
14600 This is separate from the assignment checking because data lists should
14601 only be resolved once. */
14603 static bool
14604 resolve_data_variables (gfc_data_variable *d)
14606 for (; d; d = d->next)
14608 if (d->list == NULL)
14610 if (!gfc_resolve_expr (d->expr))
14611 return false;
14613 else
14615 if (!gfc_resolve_iterator (&d->iter, false, true))
14616 return false;
14618 if (!resolve_data_variables (d->list))
14619 return false;
14623 return true;
14627 /* Resolve a single DATA statement. We implement this by storing a pointer to
14628 the value list into static variables, and then recursively traversing the
14629 variables list, expanding iterators and such. */
14631 static void
14632 resolve_data (gfc_data *d)
14635 if (!resolve_data_variables (d->var))
14636 return;
14638 values.vnode = d->value;
14639 if (d->value == NULL)
14640 mpz_set_ui (values.left, 0);
14641 else
14642 mpz_set (values.left, d->value->repeat);
14644 if (!traverse_data_var (d->var, &d->where))
14645 return;
14647 /* At this point, we better not have any values left. */
14649 if (next_data_value ())
14650 gfc_error ("DATA statement at %L has more values than variables",
14651 &d->where);
14655 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
14656 accessed by host or use association, is a dummy argument to a pure function,
14657 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
14658 is storage associated with any such variable, shall not be used in the
14659 following contexts: (clients of this function). */
14661 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
14662 procedure. Returns zero if assignment is OK, nonzero if there is a
14663 problem. */
14665 gfc_impure_variable (gfc_symbol *sym)
14667 gfc_symbol *proc;
14668 gfc_namespace *ns;
14670 if (sym->attr.use_assoc || sym->attr.in_common)
14671 return 1;
14673 /* Check if the symbol's ns is inside the pure procedure. */
14674 for (ns = gfc_current_ns; ns; ns = ns->parent)
14676 if (ns == sym->ns)
14677 break;
14678 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
14679 return 1;
14682 proc = sym->ns->proc_name;
14683 if (sym->attr.dummy
14684 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
14685 || proc->attr.function))
14686 return 1;
14688 /* TODO: Sort out what can be storage associated, if anything, and include
14689 it here. In principle equivalences should be scanned but it does not
14690 seem to be possible to storage associate an impure variable this way. */
14691 return 0;
14695 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
14696 current namespace is inside a pure procedure. */
14699 gfc_pure (gfc_symbol *sym)
14701 symbol_attribute attr;
14702 gfc_namespace *ns;
14704 if (sym == NULL)
14706 /* Check if the current namespace or one of its parents
14707 belongs to a pure procedure. */
14708 for (ns = gfc_current_ns; ns; ns = ns->parent)
14710 sym = ns->proc_name;
14711 if (sym == NULL)
14712 return 0;
14713 attr = sym->attr;
14714 if (attr.flavor == FL_PROCEDURE && attr.pure)
14715 return 1;
14717 return 0;
14720 attr = sym->attr;
14722 return attr.flavor == FL_PROCEDURE && attr.pure;
14726 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
14727 checks if the current namespace is implicitly pure. Note that this
14728 function returns false for a PURE procedure. */
14731 gfc_implicit_pure (gfc_symbol *sym)
14733 gfc_namespace *ns;
14735 if (sym == NULL)
14737 /* Check if the current procedure is implicit_pure. Walk up
14738 the procedure list until we find a procedure. */
14739 for (ns = gfc_current_ns; ns; ns = ns->parent)
14741 sym = ns->proc_name;
14742 if (sym == NULL)
14743 return 0;
14745 if (sym->attr.flavor == FL_PROCEDURE)
14746 break;
14750 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
14751 && !sym->attr.pure;
14755 void
14756 gfc_unset_implicit_pure (gfc_symbol *sym)
14758 gfc_namespace *ns;
14760 if (sym == NULL)
14762 /* Check if the current procedure is implicit_pure. Walk up
14763 the procedure list until we find a procedure. */
14764 for (ns = gfc_current_ns; ns; ns = ns->parent)
14766 sym = ns->proc_name;
14767 if (sym == NULL)
14768 return;
14770 if (sym->attr.flavor == FL_PROCEDURE)
14771 break;
14775 if (sym->attr.flavor == FL_PROCEDURE)
14776 sym->attr.implicit_pure = 0;
14777 else
14778 sym->attr.pure = 0;
14782 /* Test whether the current procedure is elemental or not. */
14785 gfc_elemental (gfc_symbol *sym)
14787 symbol_attribute attr;
14789 if (sym == NULL)
14790 sym = gfc_current_ns->proc_name;
14791 if (sym == NULL)
14792 return 0;
14793 attr = sym->attr;
14795 return attr.flavor == FL_PROCEDURE && attr.elemental;
14799 /* Warn about unused labels. */
14801 static void
14802 warn_unused_fortran_label (gfc_st_label *label)
14804 if (label == NULL)
14805 return;
14807 warn_unused_fortran_label (label->left);
14809 if (label->defined == ST_LABEL_UNKNOWN)
14810 return;
14812 switch (label->referenced)
14814 case ST_LABEL_UNKNOWN:
14815 gfc_warning (0, "Label %d at %L defined but not used", label->value,
14816 &label->where);
14817 break;
14819 case ST_LABEL_BAD_TARGET:
14820 gfc_warning (0, "Label %d at %L defined but cannot be used",
14821 label->value, &label->where);
14822 break;
14824 default:
14825 break;
14828 warn_unused_fortran_label (label->right);
14832 /* Returns the sequence type of a symbol or sequence. */
14834 static seq_type
14835 sequence_type (gfc_typespec ts)
14837 seq_type result;
14838 gfc_component *c;
14840 switch (ts.type)
14842 case BT_DERIVED:
14844 if (ts.u.derived->components == NULL)
14845 return SEQ_NONDEFAULT;
14847 result = sequence_type (ts.u.derived->components->ts);
14848 for (c = ts.u.derived->components->next; c; c = c->next)
14849 if (sequence_type (c->ts) != result)
14850 return SEQ_MIXED;
14852 return result;
14854 case BT_CHARACTER:
14855 if (ts.kind != gfc_default_character_kind)
14856 return SEQ_NONDEFAULT;
14858 return SEQ_CHARACTER;
14860 case BT_INTEGER:
14861 if (ts.kind != gfc_default_integer_kind)
14862 return SEQ_NONDEFAULT;
14864 return SEQ_NUMERIC;
14866 case BT_REAL:
14867 if (!(ts.kind == gfc_default_real_kind
14868 || ts.kind == gfc_default_double_kind))
14869 return SEQ_NONDEFAULT;
14871 return SEQ_NUMERIC;
14873 case BT_COMPLEX:
14874 if (ts.kind != gfc_default_complex_kind)
14875 return SEQ_NONDEFAULT;
14877 return SEQ_NUMERIC;
14879 case BT_LOGICAL:
14880 if (ts.kind != gfc_default_logical_kind)
14881 return SEQ_NONDEFAULT;
14883 return SEQ_NUMERIC;
14885 default:
14886 return SEQ_NONDEFAULT;
14891 /* Resolve derived type EQUIVALENCE object. */
14893 static bool
14894 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
14896 gfc_component *c = derived->components;
14898 if (!derived)
14899 return true;
14901 /* Shall not be an object of nonsequence derived type. */
14902 if (!derived->attr.sequence)
14904 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
14905 "attribute to be an EQUIVALENCE object", sym->name,
14906 &e->where);
14907 return false;
14910 /* Shall not have allocatable components. */
14911 if (derived->attr.alloc_comp)
14913 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
14914 "components to be an EQUIVALENCE object",sym->name,
14915 &e->where);
14916 return false;
14919 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
14921 gfc_error ("Derived type variable %qs at %L with default "
14922 "initialization cannot be in EQUIVALENCE with a variable "
14923 "in COMMON", sym->name, &e->where);
14924 return false;
14927 for (; c ; c = c->next)
14929 if (c->ts.type == BT_DERIVED
14930 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
14931 return false;
14933 /* Shall not be an object of sequence derived type containing a pointer
14934 in the structure. */
14935 if (c->attr.pointer)
14937 gfc_error ("Derived type variable %qs at %L with pointer "
14938 "component(s) cannot be an EQUIVALENCE object",
14939 sym->name, &e->where);
14940 return false;
14943 return true;
14947 /* Resolve equivalence object.
14948 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
14949 an allocatable array, an object of nonsequence derived type, an object of
14950 sequence derived type containing a pointer at any level of component
14951 selection, an automatic object, a function name, an entry name, a result
14952 name, a named constant, a structure component, or a subobject of any of
14953 the preceding objects. A substring shall not have length zero. A
14954 derived type shall not have components with default initialization nor
14955 shall two objects of an equivalence group be initialized.
14956 Either all or none of the objects shall have an protected attribute.
14957 The simple constraints are done in symbol.c(check_conflict) and the rest
14958 are implemented here. */
14960 static void
14961 resolve_equivalence (gfc_equiv *eq)
14963 gfc_symbol *sym;
14964 gfc_symbol *first_sym;
14965 gfc_expr *e;
14966 gfc_ref *r;
14967 locus *last_where = NULL;
14968 seq_type eq_type, last_eq_type;
14969 gfc_typespec *last_ts;
14970 int object, cnt_protected;
14971 const char *msg;
14973 last_ts = &eq->expr->symtree->n.sym->ts;
14975 first_sym = eq->expr->symtree->n.sym;
14977 cnt_protected = 0;
14979 for (object = 1; eq; eq = eq->eq, object++)
14981 e = eq->expr;
14983 e->ts = e->symtree->n.sym->ts;
14984 /* match_varspec might not know yet if it is seeing
14985 array reference or substring reference, as it doesn't
14986 know the types. */
14987 if (e->ref && e->ref->type == REF_ARRAY)
14989 gfc_ref *ref = e->ref;
14990 sym = e->symtree->n.sym;
14992 if (sym->attr.dimension)
14994 ref->u.ar.as = sym->as;
14995 ref = ref->next;
14998 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
14999 if (e->ts.type == BT_CHARACTER
15000 && ref
15001 && ref->type == REF_ARRAY
15002 && ref->u.ar.dimen == 1
15003 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
15004 && ref->u.ar.stride[0] == NULL)
15006 gfc_expr *start = ref->u.ar.start[0];
15007 gfc_expr *end = ref->u.ar.end[0];
15008 void *mem = NULL;
15010 /* Optimize away the (:) reference. */
15011 if (start == NULL && end == NULL)
15013 if (e->ref == ref)
15014 e->ref = ref->next;
15015 else
15016 e->ref->next = ref->next;
15017 mem = ref;
15019 else
15021 ref->type = REF_SUBSTRING;
15022 if (start == NULL)
15023 start = gfc_get_int_expr (gfc_default_integer_kind,
15024 NULL, 1);
15025 ref->u.ss.start = start;
15026 if (end == NULL && e->ts.u.cl)
15027 end = gfc_copy_expr (e->ts.u.cl->length);
15028 ref->u.ss.end = end;
15029 ref->u.ss.length = e->ts.u.cl;
15030 e->ts.u.cl = NULL;
15032 ref = ref->next;
15033 free (mem);
15036 /* Any further ref is an error. */
15037 if (ref)
15039 gcc_assert (ref->type == REF_ARRAY);
15040 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
15041 &ref->u.ar.where);
15042 continue;
15046 if (!gfc_resolve_expr (e))
15047 continue;
15049 sym = e->symtree->n.sym;
15051 if (sym->attr.is_protected)
15052 cnt_protected++;
15053 if (cnt_protected > 0 && cnt_protected != object)
15055 gfc_error ("Either all or none of the objects in the "
15056 "EQUIVALENCE set at %L shall have the "
15057 "PROTECTED attribute",
15058 &e->where);
15059 break;
15062 /* Shall not equivalence common block variables in a PURE procedure. */
15063 if (sym->ns->proc_name
15064 && sym->ns->proc_name->attr.pure
15065 && sym->attr.in_common)
15067 gfc_error ("Common block member %qs at %L cannot be an EQUIVALENCE "
15068 "object in the pure procedure %qs",
15069 sym->name, &e->where, sym->ns->proc_name->name);
15070 break;
15073 /* Shall not be a named constant. */
15074 if (e->expr_type == EXPR_CONSTANT)
15076 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
15077 "object", sym->name, &e->where);
15078 continue;
15081 if (e->ts.type == BT_DERIVED
15082 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
15083 continue;
15085 /* Check that the types correspond correctly:
15086 Note 5.28:
15087 A numeric sequence structure may be equivalenced to another sequence
15088 structure, an object of default integer type, default real type, double
15089 precision real type, default logical type such that components of the
15090 structure ultimately only become associated to objects of the same
15091 kind. A character sequence structure may be equivalenced to an object
15092 of default character kind or another character sequence structure.
15093 Other objects may be equivalenced only to objects of the same type and
15094 kind parameters. */
15096 /* Identical types are unconditionally OK. */
15097 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
15098 goto identical_types;
15100 last_eq_type = sequence_type (*last_ts);
15101 eq_type = sequence_type (sym->ts);
15103 /* Since the pair of objects is not of the same type, mixed or
15104 non-default sequences can be rejected. */
15106 msg = "Sequence %s with mixed components in EQUIVALENCE "
15107 "statement at %L with different type objects";
15108 if ((object ==2
15109 && last_eq_type == SEQ_MIXED
15110 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
15111 || (eq_type == SEQ_MIXED
15112 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
15113 continue;
15115 msg = "Non-default type object or sequence %s in EQUIVALENCE "
15116 "statement at %L with objects of different type";
15117 if ((object ==2
15118 && last_eq_type == SEQ_NONDEFAULT
15119 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
15120 || (eq_type == SEQ_NONDEFAULT
15121 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
15122 continue;
15124 msg ="Non-CHARACTER object %qs in default CHARACTER "
15125 "EQUIVALENCE statement at %L";
15126 if (last_eq_type == SEQ_CHARACTER
15127 && eq_type != SEQ_CHARACTER
15128 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
15129 continue;
15131 msg ="Non-NUMERIC object %qs in default NUMERIC "
15132 "EQUIVALENCE statement at %L";
15133 if (last_eq_type == SEQ_NUMERIC
15134 && eq_type != SEQ_NUMERIC
15135 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
15136 continue;
15138 identical_types:
15139 last_ts =&sym->ts;
15140 last_where = &e->where;
15142 if (!e->ref)
15143 continue;
15145 /* Shall not be an automatic array. */
15146 if (e->ref->type == REF_ARRAY
15147 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
15149 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
15150 "an EQUIVALENCE object", sym->name, &e->where);
15151 continue;
15154 r = e->ref;
15155 while (r)
15157 /* Shall not be a structure component. */
15158 if (r->type == REF_COMPONENT)
15160 gfc_error ("Structure component %qs at %L cannot be an "
15161 "EQUIVALENCE object",
15162 r->u.c.component->name, &e->where);
15163 break;
15166 /* A substring shall not have length zero. */
15167 if (r->type == REF_SUBSTRING)
15169 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
15171 gfc_error ("Substring at %L has length zero",
15172 &r->u.ss.start->where);
15173 break;
15176 r = r->next;
15182 /* Resolve function and ENTRY types, issue diagnostics if needed. */
15184 static void
15185 resolve_fntype (gfc_namespace *ns)
15187 gfc_entry_list *el;
15188 gfc_symbol *sym;
15190 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
15191 return;
15193 /* If there are any entries, ns->proc_name is the entry master
15194 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
15195 if (ns->entries)
15196 sym = ns->entries->sym;
15197 else
15198 sym = ns->proc_name;
15199 if (sym->result == sym
15200 && sym->ts.type == BT_UNKNOWN
15201 && !gfc_set_default_type (sym, 0, NULL)
15202 && !sym->attr.untyped)
15204 gfc_error ("Function %qs at %L has no IMPLICIT type",
15205 sym->name, &sym->declared_at);
15206 sym->attr.untyped = 1;
15209 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
15210 && !sym->attr.contained
15211 && !gfc_check_symbol_access (sym->ts.u.derived)
15212 && gfc_check_symbol_access (sym))
15214 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
15215 "%L of PRIVATE type %qs", sym->name,
15216 &sym->declared_at, sym->ts.u.derived->name);
15219 if (ns->entries)
15220 for (el = ns->entries->next; el; el = el->next)
15222 if (el->sym->result == el->sym
15223 && el->sym->ts.type == BT_UNKNOWN
15224 && !gfc_set_default_type (el->sym, 0, NULL)
15225 && !el->sym->attr.untyped)
15227 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
15228 el->sym->name, &el->sym->declared_at);
15229 el->sym->attr.untyped = 1;
15235 /* 12.3.2.1.1 Defined operators. */
15237 static bool
15238 check_uop_procedure (gfc_symbol *sym, locus where)
15240 gfc_formal_arglist *formal;
15242 if (!sym->attr.function)
15244 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
15245 sym->name, &where);
15246 return false;
15249 if (sym->ts.type == BT_CHARACTER
15250 && !(sym->ts.u.cl && sym->ts.u.cl->length)
15251 && !(sym->result && sym->result->ts.u.cl
15252 && sym->result->ts.u.cl->length))
15254 gfc_error ("User operator procedure %qs at %L cannot be assumed "
15255 "character length", sym->name, &where);
15256 return false;
15259 formal = gfc_sym_get_dummy_args (sym);
15260 if (!formal || !formal->sym)
15262 gfc_error ("User operator procedure %qs at %L must have at least "
15263 "one argument", sym->name, &where);
15264 return false;
15267 if (formal->sym->attr.intent != INTENT_IN)
15269 gfc_error ("First argument of operator interface at %L must be "
15270 "INTENT(IN)", &where);
15271 return false;
15274 if (formal->sym->attr.optional)
15276 gfc_error ("First argument of operator interface at %L cannot be "
15277 "optional", &where);
15278 return false;
15281 formal = formal->next;
15282 if (!formal || !formal->sym)
15283 return true;
15285 if (formal->sym->attr.intent != INTENT_IN)
15287 gfc_error ("Second argument of operator interface at %L must be "
15288 "INTENT(IN)", &where);
15289 return false;
15292 if (formal->sym->attr.optional)
15294 gfc_error ("Second argument of operator interface at %L cannot be "
15295 "optional", &where);
15296 return false;
15299 if (formal->next)
15301 gfc_error ("Operator interface at %L must have, at most, two "
15302 "arguments", &where);
15303 return false;
15306 return true;
15309 static void
15310 gfc_resolve_uops (gfc_symtree *symtree)
15312 gfc_interface *itr;
15314 if (symtree == NULL)
15315 return;
15317 gfc_resolve_uops (symtree->left);
15318 gfc_resolve_uops (symtree->right);
15320 for (itr = symtree->n.uop->op; itr; itr = itr->next)
15321 check_uop_procedure (itr->sym, itr->sym->declared_at);
15325 /* Examine all of the expressions associated with a program unit,
15326 assign types to all intermediate expressions, make sure that all
15327 assignments are to compatible types and figure out which names
15328 refer to which functions or subroutines. It doesn't check code
15329 block, which is handled by gfc_resolve_code. */
15331 static void
15332 resolve_types (gfc_namespace *ns)
15334 gfc_namespace *n;
15335 gfc_charlen *cl;
15336 gfc_data *d;
15337 gfc_equiv *eq;
15338 gfc_namespace* old_ns = gfc_current_ns;
15340 if (ns->types_resolved)
15341 return;
15343 /* Check that all IMPLICIT types are ok. */
15344 if (!ns->seen_implicit_none)
15346 unsigned letter;
15347 for (letter = 0; letter != GFC_LETTERS; ++letter)
15348 if (ns->set_flag[letter]
15349 && !resolve_typespec_used (&ns->default_type[letter],
15350 &ns->implicit_loc[letter], NULL))
15351 return;
15354 gfc_current_ns = ns;
15356 resolve_entries (ns);
15358 resolve_common_vars (&ns->blank_common, false);
15359 resolve_common_blocks (ns->common_root);
15361 resolve_contained_functions (ns);
15363 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
15364 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
15365 resolve_formal_arglist (ns->proc_name);
15367 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
15369 for (cl = ns->cl_list; cl; cl = cl->next)
15370 resolve_charlen (cl);
15372 gfc_traverse_ns (ns, resolve_symbol);
15374 resolve_fntype (ns);
15376 for (n = ns->contained; n; n = n->sibling)
15378 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
15379 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
15380 "also be PURE", n->proc_name->name,
15381 &n->proc_name->declared_at);
15383 resolve_types (n);
15386 forall_flag = 0;
15387 gfc_do_concurrent_flag = 0;
15388 gfc_check_interfaces (ns);
15390 gfc_traverse_ns (ns, resolve_values);
15392 if (ns->save_all)
15393 gfc_save_all (ns);
15395 iter_stack = NULL;
15396 for (d = ns->data; d; d = d->next)
15397 resolve_data (d);
15399 iter_stack = NULL;
15400 gfc_traverse_ns (ns, gfc_formalize_init_value);
15402 gfc_traverse_ns (ns, gfc_verify_binding_labels);
15404 for (eq = ns->equiv; eq; eq = eq->next)
15405 resolve_equivalence (eq);
15407 /* Warn about unused labels. */
15408 if (warn_unused_label)
15409 warn_unused_fortran_label (ns->st_labels);
15411 gfc_resolve_uops (ns->uop_root);
15413 gfc_resolve_omp_declare_simd (ns);
15415 gfc_resolve_omp_udrs (ns->omp_udr_root);
15417 ns->types_resolved = 1;
15419 gfc_current_ns = old_ns;
15423 /* Call gfc_resolve_code recursively. */
15425 static void
15426 resolve_codes (gfc_namespace *ns)
15428 gfc_namespace *n;
15429 bitmap_obstack old_obstack;
15431 if (ns->resolved == 1)
15432 return;
15434 for (n = ns->contained; n; n = n->sibling)
15435 resolve_codes (n);
15437 gfc_current_ns = ns;
15439 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
15440 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
15441 cs_base = NULL;
15443 /* Set to an out of range value. */
15444 current_entry_id = -1;
15446 old_obstack = labels_obstack;
15447 bitmap_obstack_initialize (&labels_obstack);
15449 gfc_resolve_oacc_declare (ns);
15450 gfc_resolve_code (ns->code, ns);
15452 bitmap_obstack_release (&labels_obstack);
15453 labels_obstack = old_obstack;
15457 /* This function is called after a complete program unit has been compiled.
15458 Its purpose is to examine all of the expressions associated with a program
15459 unit, assign types to all intermediate expressions, make sure that all
15460 assignments are to compatible types and figure out which names refer to
15461 which functions or subroutines. */
15463 void
15464 gfc_resolve (gfc_namespace *ns)
15466 gfc_namespace *old_ns;
15467 code_stack *old_cs_base;
15468 struct gfc_omp_saved_state old_omp_state;
15470 if (ns->resolved)
15471 return;
15473 ns->resolved = -1;
15474 old_ns = gfc_current_ns;
15475 old_cs_base = cs_base;
15477 /* As gfc_resolve can be called during resolution of an OpenMP construct
15478 body, we should clear any state associated to it, so that say NS's
15479 DO loops are not interpreted as OpenMP loops. */
15480 gfc_omp_save_and_clear_state (&old_omp_state);
15482 resolve_types (ns);
15483 component_assignment_level = 0;
15484 resolve_codes (ns);
15486 gfc_current_ns = old_ns;
15487 cs_base = old_cs_base;
15488 ns->resolved = 1;
15490 gfc_run_passes (ns);
15492 gfc_omp_restore_state (&old_omp_state);