Fix missing ChangeLog entry for Graphite head files fix.
[official-gcc.git] / gcc / fortran / resolve.c
blob685e3f540079464f9cebbac2c355f82e512ac0ec
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_OMP_ATOMIC:
9377 case EXEC_OMP_CRITICAL:
9378 case EXEC_OMP_DISTRIBUTE:
9379 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
9380 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
9381 case EXEC_OMP_DISTRIBUTE_SIMD:
9382 case EXEC_OMP_DO:
9383 case EXEC_OMP_DO_SIMD:
9384 case EXEC_OMP_MASTER:
9385 case EXEC_OMP_ORDERED:
9386 case EXEC_OMP_PARALLEL:
9387 case EXEC_OMP_PARALLEL_DO:
9388 case EXEC_OMP_PARALLEL_DO_SIMD:
9389 case EXEC_OMP_PARALLEL_SECTIONS:
9390 case EXEC_OMP_PARALLEL_WORKSHARE:
9391 case EXEC_OMP_SECTIONS:
9392 case EXEC_OMP_SIMD:
9393 case EXEC_OMP_SINGLE:
9394 case EXEC_OMP_TARGET:
9395 case EXEC_OMP_TARGET_DATA:
9396 case EXEC_OMP_TARGET_TEAMS:
9397 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
9398 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
9399 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9400 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
9401 case EXEC_OMP_TARGET_UPDATE:
9402 case EXEC_OMP_TASK:
9403 case EXEC_OMP_TASKGROUP:
9404 case EXEC_OMP_TASKWAIT:
9405 case EXEC_OMP_TASKYIELD:
9406 case EXEC_OMP_TEAMS:
9407 case EXEC_OMP_TEAMS_DISTRIBUTE:
9408 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
9409 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9410 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
9411 case EXEC_OMP_WORKSHARE:
9412 break;
9414 default:
9415 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
9418 gfc_resolve_code (b->next, ns);
9423 /* Does everything to resolve an ordinary assignment. Returns true
9424 if this is an interface assignment. */
9425 static bool
9426 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9428 bool rval = false;
9429 gfc_expr *lhs;
9430 gfc_expr *rhs;
9431 int llen = 0;
9432 int rlen = 0;
9433 int n;
9434 gfc_ref *ref;
9435 symbol_attribute attr;
9437 if (gfc_extend_assign (code, ns))
9439 gfc_expr** rhsptr;
9441 if (code->op == EXEC_ASSIGN_CALL)
9443 lhs = code->ext.actual->expr;
9444 rhsptr = &code->ext.actual->next->expr;
9446 else
9448 gfc_actual_arglist* args;
9449 gfc_typebound_proc* tbp;
9451 gcc_assert (code->op == EXEC_COMPCALL);
9453 args = code->expr1->value.compcall.actual;
9454 lhs = args->expr;
9455 rhsptr = &args->next->expr;
9457 tbp = code->expr1->value.compcall.tbp;
9458 gcc_assert (!tbp->is_generic);
9461 /* Make a temporary rhs when there is a default initializer
9462 and rhs is the same symbol as the lhs. */
9463 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9464 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9465 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9466 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9467 *rhsptr = gfc_get_parentheses (*rhsptr);
9469 return true;
9472 lhs = code->expr1;
9473 rhs = code->expr2;
9475 if (rhs->is_boz
9476 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9477 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9478 &code->loc))
9479 return false;
9481 /* Handle the case of a BOZ literal on the RHS. */
9482 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9484 int rc;
9485 if (warn_surprising)
9486 gfc_warning (OPT_Wsurprising,
9487 "BOZ literal at %L is bitwise transferred "
9488 "non-integer symbol %qs", &code->loc,
9489 lhs->symtree->n.sym->name);
9491 if (!gfc_convert_boz (rhs, &lhs->ts))
9492 return false;
9493 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9495 if (rc == ARITH_UNDERFLOW)
9496 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9497 ". This check can be disabled with the option "
9498 "%<-fno-range-check%>", &rhs->where);
9499 else if (rc == ARITH_OVERFLOW)
9500 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9501 ". This check can be disabled with the option "
9502 "%<-fno-range-check%>", &rhs->where);
9503 else if (rc == ARITH_NAN)
9504 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9505 ". This check can be disabled with the option "
9506 "%<-fno-range-check%>", &rhs->where);
9507 return false;
9511 if (lhs->ts.type == BT_CHARACTER
9512 && warn_character_truncation)
9514 if (lhs->ts.u.cl != NULL
9515 && lhs->ts.u.cl->length != NULL
9516 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9517 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9519 if (rhs->expr_type == EXPR_CONSTANT)
9520 rlen = rhs->value.character.length;
9522 else if (rhs->ts.u.cl != NULL
9523 && rhs->ts.u.cl->length != NULL
9524 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9525 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9527 if (rlen && llen && rlen > llen)
9528 gfc_warning_now (OPT_Wcharacter_truncation,
9529 "CHARACTER expression will be truncated "
9530 "in assignment (%d/%d) at %L",
9531 llen, rlen, &code->loc);
9534 /* Ensure that a vector index expression for the lvalue is evaluated
9535 to a temporary if the lvalue symbol is referenced in it. */
9536 if (lhs->rank)
9538 for (ref = lhs->ref; ref; ref= ref->next)
9539 if (ref->type == REF_ARRAY)
9541 for (n = 0; n < ref->u.ar.dimen; n++)
9542 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9543 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9544 ref->u.ar.start[n]))
9545 ref->u.ar.start[n]
9546 = gfc_get_parentheses (ref->u.ar.start[n]);
9550 if (gfc_pure (NULL))
9552 if (lhs->ts.type == BT_DERIVED
9553 && lhs->expr_type == EXPR_VARIABLE
9554 && lhs->ts.u.derived->attr.pointer_comp
9555 && rhs->expr_type == EXPR_VARIABLE
9556 && (gfc_impure_variable (rhs->symtree->n.sym)
9557 || gfc_is_coindexed (rhs)))
9559 /* F2008, C1283. */
9560 if (gfc_is_coindexed (rhs))
9561 gfc_error ("Coindexed expression at %L is assigned to "
9562 "a derived type variable with a POINTER "
9563 "component in a PURE procedure",
9564 &rhs->where);
9565 else
9566 gfc_error ("The impure variable at %L is assigned to "
9567 "a derived type variable with a POINTER "
9568 "component in a PURE procedure (12.6)",
9569 &rhs->where);
9570 return rval;
9573 /* Fortran 2008, C1283. */
9574 if (gfc_is_coindexed (lhs))
9576 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9577 "procedure", &rhs->where);
9578 return rval;
9582 if (gfc_implicit_pure (NULL))
9584 if (lhs->expr_type == EXPR_VARIABLE
9585 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9586 && lhs->symtree->n.sym->ns != gfc_current_ns)
9587 gfc_unset_implicit_pure (NULL);
9589 if (lhs->ts.type == BT_DERIVED
9590 && lhs->expr_type == EXPR_VARIABLE
9591 && lhs->ts.u.derived->attr.pointer_comp
9592 && rhs->expr_type == EXPR_VARIABLE
9593 && (gfc_impure_variable (rhs->symtree->n.sym)
9594 || gfc_is_coindexed (rhs)))
9595 gfc_unset_implicit_pure (NULL);
9597 /* Fortran 2008, C1283. */
9598 if (gfc_is_coindexed (lhs))
9599 gfc_unset_implicit_pure (NULL);
9602 /* F2008, 7.2.1.2. */
9603 attr = gfc_expr_attr (lhs);
9604 if (lhs->ts.type == BT_CLASS && attr.allocatable)
9606 if (attr.codimension)
9608 gfc_error ("Assignment to polymorphic coarray at %L is not "
9609 "permitted", &lhs->where);
9610 return false;
9612 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
9613 "polymorphic variable at %L", &lhs->where))
9614 return false;
9615 if (!flag_realloc_lhs)
9617 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9618 "requires %<-frealloc-lhs%>", &lhs->where);
9619 return false;
9621 /* See PR 43366. */
9622 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9623 "is not yet supported", &lhs->where);
9624 return false;
9626 else if (lhs->ts.type == BT_CLASS)
9628 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
9629 "assignment at %L - check that there is a matching specific "
9630 "subroutine for '=' operator", &lhs->where);
9631 return false;
9634 bool lhs_coindexed = gfc_is_coindexed (lhs);
9636 /* F2008, Section 7.2.1.2. */
9637 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
9639 gfc_error ("Coindexed variable must not have an allocatable ultimate "
9640 "component in assignment at %L", &lhs->where);
9641 return false;
9644 gfc_check_assign (lhs, rhs, 1);
9646 /* Assign the 'data' of a class object to a derived type. */
9647 if (lhs->ts.type == BT_DERIVED
9648 && rhs->ts.type == BT_CLASS)
9649 gfc_add_data_component (rhs);
9651 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
9652 Additionally, insert this code when the RHS is a CAF as we then use the
9653 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
9654 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
9655 noncoindexed array and the RHS is a coindexed scalar, use the normal code
9656 path. */
9657 if (flag_coarray == GFC_FCOARRAY_LIB
9658 && (lhs_coindexed
9659 || (code->expr2->expr_type == EXPR_FUNCTION
9660 && code->expr2->value.function.isym
9661 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
9662 && (code->expr1->rank == 0 || code->expr2->rank != 0)
9663 && !gfc_expr_attr (rhs).allocatable
9664 && !gfc_has_vector_subscript (rhs))))
9666 if (code->expr2->expr_type == EXPR_FUNCTION
9667 && code->expr2->value.function.isym
9668 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
9669 remove_caf_get_intrinsic (code->expr2);
9670 code->op = EXEC_CALL;
9671 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
9672 code->resolved_sym = code->symtree->n.sym;
9673 code->resolved_sym->attr.flavor = FL_PROCEDURE;
9674 code->resolved_sym->attr.intrinsic = 1;
9675 code->resolved_sym->attr.subroutine = 1;
9676 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
9677 gfc_commit_symbol (code->resolved_sym);
9678 code->ext.actual = gfc_get_actual_arglist ();
9679 code->ext.actual->expr = lhs;
9680 code->ext.actual->next = gfc_get_actual_arglist ();
9681 code->ext.actual->next->expr = rhs;
9682 code->expr1 = NULL;
9683 code->expr2 = NULL;
9686 return false;
9690 /* Add a component reference onto an expression. */
9692 static void
9693 add_comp_ref (gfc_expr *e, gfc_component *c)
9695 gfc_ref **ref;
9696 ref = &(e->ref);
9697 while (*ref)
9698 ref = &((*ref)->next);
9699 *ref = gfc_get_ref ();
9700 (*ref)->type = REF_COMPONENT;
9701 (*ref)->u.c.sym = e->ts.u.derived;
9702 (*ref)->u.c.component = c;
9703 e->ts = c->ts;
9705 /* Add a full array ref, as necessary. */
9706 if (c->as)
9708 gfc_add_full_array_ref (e, c->as);
9709 e->rank = c->as->rank;
9714 /* Build an assignment. Keep the argument 'op' for future use, so that
9715 pointer assignments can be made. */
9717 static gfc_code *
9718 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9719 gfc_component *comp1, gfc_component *comp2, locus loc)
9721 gfc_code *this_code;
9723 this_code = gfc_get_code (op);
9724 this_code->next = NULL;
9725 this_code->expr1 = gfc_copy_expr (expr1);
9726 this_code->expr2 = gfc_copy_expr (expr2);
9727 this_code->loc = loc;
9728 if (comp1 && comp2)
9730 add_comp_ref (this_code->expr1, comp1);
9731 add_comp_ref (this_code->expr2, comp2);
9734 return this_code;
9738 /* Makes a temporary variable expression based on the characteristics of
9739 a given variable expression. */
9741 static gfc_expr*
9742 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9744 static int serial = 0;
9745 char name[GFC_MAX_SYMBOL_LEN];
9746 gfc_symtree *tmp;
9747 gfc_array_spec *as;
9748 gfc_array_ref *aref;
9749 gfc_ref *ref;
9751 sprintf (name, GFC_PREFIX("DA%d"), serial++);
9752 gfc_get_sym_tree (name, ns, &tmp, false);
9753 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9755 as = NULL;
9756 ref = NULL;
9757 aref = NULL;
9759 /* Obtain the arrayspec for the temporary. */
9760 if (e->rank && e->expr_type != EXPR_ARRAY
9761 && e->expr_type != EXPR_FUNCTION
9762 && e->expr_type != EXPR_OP)
9764 aref = gfc_find_array_ref (e);
9765 if (e->expr_type == EXPR_VARIABLE
9766 && e->symtree->n.sym->as == aref->as)
9767 as = aref->as;
9768 else
9770 for (ref = e->ref; ref; ref = ref->next)
9771 if (ref->type == REF_COMPONENT
9772 && ref->u.c.component->as == aref->as)
9774 as = aref->as;
9775 break;
9780 /* Add the attributes and the arrayspec to the temporary. */
9781 tmp->n.sym->attr = gfc_expr_attr (e);
9782 tmp->n.sym->attr.function = 0;
9783 tmp->n.sym->attr.result = 0;
9784 tmp->n.sym->attr.flavor = FL_VARIABLE;
9786 if (as)
9788 tmp->n.sym->as = gfc_copy_array_spec (as);
9789 if (!ref)
9790 ref = e->ref;
9791 if (as->type == AS_DEFERRED)
9792 tmp->n.sym->attr.allocatable = 1;
9794 else if (e->rank && (e->expr_type == EXPR_ARRAY
9795 || e->expr_type == EXPR_FUNCTION
9796 || e->expr_type == EXPR_OP))
9798 tmp->n.sym->as = gfc_get_array_spec ();
9799 tmp->n.sym->as->type = AS_DEFERRED;
9800 tmp->n.sym->as->rank = e->rank;
9801 tmp->n.sym->attr.allocatable = 1;
9802 tmp->n.sym->attr.dimension = 1;
9804 else
9805 tmp->n.sym->attr.dimension = 0;
9807 gfc_set_sym_referenced (tmp->n.sym);
9808 gfc_commit_symbol (tmp->n.sym);
9809 e = gfc_lval_expr_from_sym (tmp->n.sym);
9811 /* Should the lhs be a section, use its array ref for the
9812 temporary expression. */
9813 if (aref && aref->type != AR_FULL)
9815 gfc_free_ref_list (e->ref);
9816 e->ref = gfc_copy_ref (ref);
9818 return e;
9822 /* Add one line of code to the code chain, making sure that 'head' and
9823 'tail' are appropriately updated. */
9825 static void
9826 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9828 gcc_assert (this_code);
9829 if (*head == NULL)
9830 *head = *tail = *this_code;
9831 else
9832 *tail = gfc_append_code (*tail, *this_code);
9833 *this_code = NULL;
9837 /* Counts the potential number of part array references that would
9838 result from resolution of typebound defined assignments. */
9840 static int
9841 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9843 gfc_component *c;
9844 int c_depth = 0, t_depth;
9846 for (c= derived->components; c; c = c->next)
9848 if ((c->ts.type != BT_DERIVED
9849 || c->attr.pointer
9850 || c->attr.allocatable
9851 || c->attr.proc_pointer_comp
9852 || c->attr.class_pointer
9853 || c->attr.proc_pointer)
9854 && !c->attr.defined_assign_comp)
9855 continue;
9857 if (c->as && c_depth == 0)
9858 c_depth = 1;
9860 if (c->ts.u.derived->attr.defined_assign_comp)
9861 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9862 c->as ? 1 : 0);
9863 else
9864 t_depth = 0;
9866 c_depth = t_depth > c_depth ? t_depth : c_depth;
9868 return depth + c_depth;
9872 /* Implement 7.2.1.3 of the F08 standard:
9873 "An intrinsic assignment where the variable is of derived type is
9874 performed as if each component of the variable were assigned from the
9875 corresponding component of expr using pointer assignment (7.2.2) for
9876 each pointer component, defined assignment for each nonpointer
9877 nonallocatable component of a type that has a type-bound defined
9878 assignment consistent with the component, intrinsic assignment for
9879 each other nonpointer nonallocatable component, ..."
9881 The pointer assignments are taken care of by the intrinsic
9882 assignment of the structure itself. This function recursively adds
9883 defined assignments where required. The recursion is accomplished
9884 by calling gfc_resolve_code.
9886 When the lhs in a defined assignment has intent INOUT, we need a
9887 temporary for the lhs. In pseudo-code:
9889 ! Only call function lhs once.
9890 if (lhs is not a constant or an variable)
9891 temp_x = expr2
9892 expr2 => temp_x
9893 ! Do the intrinsic assignment
9894 expr1 = expr2
9895 ! Now do the defined assignments
9896 do over components with typebound defined assignment [%cmp]
9897 #if one component's assignment procedure is INOUT
9898 t1 = expr1
9899 #if expr2 non-variable
9900 temp_x = expr2
9901 expr2 => temp_x
9902 # endif
9903 expr1 = expr2
9904 # for each cmp
9905 t1%cmp {defined=} expr2%cmp
9906 expr1%cmp = t1%cmp
9907 #else
9908 expr1 = expr2
9910 # for each cmp
9911 expr1%cmp {defined=} expr2%cmp
9912 #endif
9915 /* The temporary assignments have to be put on top of the additional
9916 code to avoid the result being changed by the intrinsic assignment.
9918 static int component_assignment_level = 0;
9919 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
9921 static void
9922 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
9924 gfc_component *comp1, *comp2;
9925 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
9926 gfc_expr *t1;
9927 int error_count, depth;
9929 gfc_get_errors (NULL, &error_count);
9931 /* Filter out continuing processing after an error. */
9932 if (error_count
9933 || (*code)->expr1->ts.type != BT_DERIVED
9934 || (*code)->expr2->ts.type != BT_DERIVED)
9935 return;
9937 /* TODO: Handle more than one part array reference in assignments. */
9938 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
9939 (*code)->expr1->rank ? 1 : 0);
9940 if (depth > 1)
9942 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
9943 "done because multiple part array references would "
9944 "occur in intermediate expressions.", &(*code)->loc);
9945 return;
9948 component_assignment_level++;
9950 /* Create a temporary so that functions get called only once. */
9951 if ((*code)->expr2->expr_type != EXPR_VARIABLE
9952 && (*code)->expr2->expr_type != EXPR_CONSTANT)
9954 gfc_expr *tmp_expr;
9956 /* Assign the rhs to the temporary. */
9957 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
9958 this_code = build_assignment (EXEC_ASSIGN,
9959 tmp_expr, (*code)->expr2,
9960 NULL, NULL, (*code)->loc);
9961 /* Add the code and substitute the rhs expression. */
9962 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
9963 gfc_free_expr ((*code)->expr2);
9964 (*code)->expr2 = tmp_expr;
9967 /* Do the intrinsic assignment. This is not needed if the lhs is one
9968 of the temporaries generated here, since the intrinsic assignment
9969 to the final result already does this. */
9970 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
9972 this_code = build_assignment (EXEC_ASSIGN,
9973 (*code)->expr1, (*code)->expr2,
9974 NULL, NULL, (*code)->loc);
9975 add_code_to_chain (&this_code, &head, &tail);
9978 comp1 = (*code)->expr1->ts.u.derived->components;
9979 comp2 = (*code)->expr2->ts.u.derived->components;
9981 t1 = NULL;
9982 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
9984 bool inout = false;
9986 /* The intrinsic assignment does the right thing for pointers
9987 of all kinds and allocatable components. */
9988 if (comp1->ts.type != BT_DERIVED
9989 || comp1->attr.pointer
9990 || comp1->attr.allocatable
9991 || comp1->attr.proc_pointer_comp
9992 || comp1->attr.class_pointer
9993 || comp1->attr.proc_pointer)
9994 continue;
9996 /* Make an assigment for this component. */
9997 this_code = build_assignment (EXEC_ASSIGN,
9998 (*code)->expr1, (*code)->expr2,
9999 comp1, comp2, (*code)->loc);
10001 /* Convert the assignment if there is a defined assignment for
10002 this type. Otherwise, using the call from gfc_resolve_code,
10003 recurse into its components. */
10004 gfc_resolve_code (this_code, ns);
10006 if (this_code->op == EXEC_ASSIGN_CALL)
10008 gfc_formal_arglist *dummy_args;
10009 gfc_symbol *rsym;
10010 /* Check that there is a typebound defined assignment. If not,
10011 then this must be a module defined assignment. We cannot
10012 use the defined_assign_comp attribute here because it must
10013 be this derived type that has the defined assignment and not
10014 a parent type. */
10015 if (!(comp1->ts.u.derived->f2k_derived
10016 && comp1->ts.u.derived->f2k_derived
10017 ->tb_op[INTRINSIC_ASSIGN]))
10019 gfc_free_statements (this_code);
10020 this_code = NULL;
10021 continue;
10024 /* If the first argument of the subroutine has intent INOUT
10025 a temporary must be generated and used instead. */
10026 rsym = this_code->resolved_sym;
10027 dummy_args = gfc_sym_get_dummy_args (rsym);
10028 if (dummy_args
10029 && dummy_args->sym->attr.intent == INTENT_INOUT)
10031 gfc_code *temp_code;
10032 inout = true;
10034 /* Build the temporary required for the assignment and put
10035 it at the head of the generated code. */
10036 if (!t1)
10038 t1 = get_temp_from_expr ((*code)->expr1, ns);
10039 temp_code = build_assignment (EXEC_ASSIGN,
10040 t1, (*code)->expr1,
10041 NULL, NULL, (*code)->loc);
10043 /* For allocatable LHS, check whether it is allocated. Note
10044 that allocatable components with defined assignment are
10045 not yet support. See PR 57696. */
10046 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
10048 gfc_code *block;
10049 gfc_expr *e =
10050 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10051 block = gfc_get_code (EXEC_IF);
10052 block->block = gfc_get_code (EXEC_IF);
10053 block->block->expr1
10054 = gfc_build_intrinsic_call (ns,
10055 GFC_ISYM_ALLOCATED, "allocated",
10056 (*code)->loc, 1, e);
10057 block->block->next = temp_code;
10058 temp_code = block;
10060 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
10063 /* Replace the first actual arg with the component of the
10064 temporary. */
10065 gfc_free_expr (this_code->ext.actual->expr);
10066 this_code->ext.actual->expr = gfc_copy_expr (t1);
10067 add_comp_ref (this_code->ext.actual->expr, comp1);
10069 /* If the LHS variable is allocatable and wasn't allocated and
10070 the temporary is allocatable, pointer assign the address of
10071 the freshly allocated LHS to the temporary. */
10072 if ((*code)->expr1->symtree->n.sym->attr.allocatable
10073 && gfc_expr_attr ((*code)->expr1).allocatable)
10075 gfc_code *block;
10076 gfc_expr *cond;
10078 cond = gfc_get_expr ();
10079 cond->ts.type = BT_LOGICAL;
10080 cond->ts.kind = gfc_default_logical_kind;
10081 cond->expr_type = EXPR_OP;
10082 cond->where = (*code)->loc;
10083 cond->value.op.op = INTRINSIC_NOT;
10084 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
10085 GFC_ISYM_ALLOCATED, "allocated",
10086 (*code)->loc, 1, gfc_copy_expr (t1));
10087 block = gfc_get_code (EXEC_IF);
10088 block->block = gfc_get_code (EXEC_IF);
10089 block->block->expr1 = cond;
10090 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10091 t1, (*code)->expr1,
10092 NULL, NULL, (*code)->loc);
10093 add_code_to_chain (&block, &head, &tail);
10097 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
10099 /* Don't add intrinsic assignments since they are already
10100 effected by the intrinsic assignment of the structure. */
10101 gfc_free_statements (this_code);
10102 this_code = NULL;
10103 continue;
10106 add_code_to_chain (&this_code, &head, &tail);
10108 if (t1 && inout)
10110 /* Transfer the value to the final result. */
10111 this_code = build_assignment (EXEC_ASSIGN,
10112 (*code)->expr1, t1,
10113 comp1, comp2, (*code)->loc);
10114 add_code_to_chain (&this_code, &head, &tail);
10118 /* Put the temporary assignments at the top of the generated code. */
10119 if (tmp_head && component_assignment_level == 1)
10121 gfc_append_code (tmp_head, head);
10122 head = tmp_head;
10123 tmp_head = tmp_tail = NULL;
10126 // If we did a pointer assignment - thus, we need to ensure that the LHS is
10127 // not accidentally deallocated. Hence, nullify t1.
10128 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
10129 && gfc_expr_attr ((*code)->expr1).allocatable)
10131 gfc_code *block;
10132 gfc_expr *cond;
10133 gfc_expr *e;
10135 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10136 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
10137 (*code)->loc, 2, gfc_copy_expr (t1), e);
10138 block = gfc_get_code (EXEC_IF);
10139 block->block = gfc_get_code (EXEC_IF);
10140 block->block->expr1 = cond;
10141 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10142 t1, gfc_get_null_expr (&(*code)->loc),
10143 NULL, NULL, (*code)->loc);
10144 gfc_append_code (tail, block);
10145 tail = block;
10148 /* Now attach the remaining code chain to the input code. Step on
10149 to the end of the new code since resolution is complete. */
10150 gcc_assert ((*code)->op == EXEC_ASSIGN);
10151 tail->next = (*code)->next;
10152 /* Overwrite 'code' because this would place the intrinsic assignment
10153 before the temporary for the lhs is created. */
10154 gfc_free_expr ((*code)->expr1);
10155 gfc_free_expr ((*code)->expr2);
10156 **code = *head;
10157 if (head != tail)
10158 free (head);
10159 *code = tail;
10161 component_assignment_level--;
10165 /* F2008: Pointer function assignments are of the form:
10166 ptr_fcn (args) = expr
10167 This function breaks these assignments into two statements:
10168 temporary_pointer => ptr_fcn(args)
10169 temporary_pointer = expr */
10171 static bool
10172 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
10174 gfc_expr *tmp_ptr_expr;
10175 gfc_code *this_code;
10176 gfc_component *comp;
10177 gfc_symbol *s;
10179 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
10180 return false;
10182 /* Even if standard does not support this feature, continue to build
10183 the two statements to avoid upsetting frontend_passes.c. */
10184 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
10185 "%L", &(*code)->loc);
10187 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
10189 if (comp)
10190 s = comp->ts.interface;
10191 else
10192 s = (*code)->expr1->symtree->n.sym;
10194 if (s == NULL || !s->result->attr.pointer)
10196 gfc_error ("The function result on the lhs of the assignment at "
10197 "%L must have the pointer attribute.",
10198 &(*code)->expr1->where);
10199 (*code)->op = EXEC_NOP;
10200 return false;
10203 tmp_ptr_expr = get_temp_from_expr ((*code)->expr2, ns);
10205 /* get_temp_from_expression is set up for ordinary assignments. To that
10206 end, where array bounds are not known, arrays are made allocatable.
10207 Change the temporary to a pointer here. */
10208 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
10209 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
10210 tmp_ptr_expr->where = (*code)->loc;
10212 this_code = build_assignment (EXEC_ASSIGN,
10213 tmp_ptr_expr, (*code)->expr2,
10214 NULL, NULL, (*code)->loc);
10215 this_code->next = (*code)->next;
10216 (*code)->next = this_code;
10217 (*code)->op = EXEC_POINTER_ASSIGN;
10218 (*code)->expr2 = (*code)->expr1;
10219 (*code)->expr1 = tmp_ptr_expr;
10221 return true;
10225 /* Deferred character length assignments from an operator expression
10226 require a temporary because the character length of the lhs can
10227 change in the course of the assignment. */
10229 static bool
10230 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
10232 gfc_expr *tmp_expr;
10233 gfc_code *this_code;
10235 if (!((*code)->expr1->ts.type == BT_CHARACTER
10236 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
10237 && (*code)->expr2->expr_type == EXPR_OP))
10238 return false;
10240 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
10241 return false;
10243 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
10244 tmp_expr->where = (*code)->loc;
10246 /* A new charlen is required to ensure that the variable string
10247 length is different to that of the original lhs. */
10248 tmp_expr->ts.u.cl = gfc_get_charlen();
10249 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
10250 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
10251 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
10253 tmp_expr->symtree->n.sym->ts.deferred = 1;
10255 this_code = build_assignment (EXEC_ASSIGN,
10256 (*code)->expr1,
10257 gfc_copy_expr (tmp_expr),
10258 NULL, NULL, (*code)->loc);
10260 (*code)->expr1 = tmp_expr;
10262 this_code->next = (*code)->next;
10263 (*code)->next = this_code;
10265 return true;
10269 /* Given a block of code, recursively resolve everything pointed to by this
10270 code block. */
10272 void
10273 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
10275 int omp_workshare_save;
10276 int forall_save, do_concurrent_save;
10277 code_stack frame;
10278 bool t;
10280 frame.prev = cs_base;
10281 frame.head = code;
10282 cs_base = &frame;
10284 find_reachable_labels (code);
10286 for (; code; code = code->next)
10288 frame.current = code;
10289 forall_save = forall_flag;
10290 do_concurrent_save = gfc_do_concurrent_flag;
10292 if (code->op == EXEC_FORALL)
10294 forall_flag = 1;
10295 gfc_resolve_forall (code, ns, forall_save);
10296 forall_flag = 2;
10298 else if (code->block)
10300 omp_workshare_save = -1;
10301 switch (code->op)
10303 case EXEC_OACC_PARALLEL_LOOP:
10304 case EXEC_OACC_PARALLEL:
10305 case EXEC_OACC_KERNELS_LOOP:
10306 case EXEC_OACC_KERNELS:
10307 case EXEC_OACC_DATA:
10308 case EXEC_OACC_HOST_DATA:
10309 case EXEC_OACC_LOOP:
10310 gfc_resolve_oacc_blocks (code, ns);
10311 break;
10312 case EXEC_OMP_PARALLEL_WORKSHARE:
10313 omp_workshare_save = omp_workshare_flag;
10314 omp_workshare_flag = 1;
10315 gfc_resolve_omp_parallel_blocks (code, ns);
10316 break;
10317 case EXEC_OMP_PARALLEL:
10318 case EXEC_OMP_PARALLEL_DO:
10319 case EXEC_OMP_PARALLEL_DO_SIMD:
10320 case EXEC_OMP_PARALLEL_SECTIONS:
10321 case EXEC_OMP_TARGET_TEAMS:
10322 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10323 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10324 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10325 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10326 case EXEC_OMP_TASK:
10327 case EXEC_OMP_TEAMS:
10328 case EXEC_OMP_TEAMS_DISTRIBUTE:
10329 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10330 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10331 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10332 omp_workshare_save = omp_workshare_flag;
10333 omp_workshare_flag = 0;
10334 gfc_resolve_omp_parallel_blocks (code, ns);
10335 break;
10336 case EXEC_OMP_DISTRIBUTE:
10337 case EXEC_OMP_DISTRIBUTE_SIMD:
10338 case EXEC_OMP_DO:
10339 case EXEC_OMP_DO_SIMD:
10340 case EXEC_OMP_SIMD:
10341 gfc_resolve_omp_do_blocks (code, ns);
10342 break;
10343 case EXEC_SELECT_TYPE:
10344 /* Blocks are handled in resolve_select_type because we have
10345 to transform the SELECT TYPE into ASSOCIATE first. */
10346 break;
10347 case EXEC_DO_CONCURRENT:
10348 gfc_do_concurrent_flag = 1;
10349 gfc_resolve_blocks (code->block, ns);
10350 gfc_do_concurrent_flag = 2;
10351 break;
10352 case EXEC_OMP_WORKSHARE:
10353 omp_workshare_save = omp_workshare_flag;
10354 omp_workshare_flag = 1;
10355 /* FALL THROUGH */
10356 default:
10357 gfc_resolve_blocks (code->block, ns);
10358 break;
10361 if (omp_workshare_save != -1)
10362 omp_workshare_flag = omp_workshare_save;
10364 start:
10365 t = true;
10366 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
10367 t = gfc_resolve_expr (code->expr1);
10368 forall_flag = forall_save;
10369 gfc_do_concurrent_flag = do_concurrent_save;
10371 if (!gfc_resolve_expr (code->expr2))
10372 t = false;
10374 if (code->op == EXEC_ALLOCATE
10375 && !gfc_resolve_expr (code->expr3))
10376 t = false;
10378 switch (code->op)
10380 case EXEC_NOP:
10381 case EXEC_END_BLOCK:
10382 case EXEC_END_NESTED_BLOCK:
10383 case EXEC_CYCLE:
10384 case EXEC_PAUSE:
10385 case EXEC_STOP:
10386 case EXEC_ERROR_STOP:
10387 case EXEC_EXIT:
10388 case EXEC_CONTINUE:
10389 case EXEC_DT_END:
10390 case EXEC_ASSIGN_CALL:
10391 break;
10393 case EXEC_CRITICAL:
10394 resolve_critical (code);
10395 break;
10397 case EXEC_SYNC_ALL:
10398 case EXEC_SYNC_IMAGES:
10399 case EXEC_SYNC_MEMORY:
10400 resolve_sync (code);
10401 break;
10403 case EXEC_LOCK:
10404 case EXEC_UNLOCK:
10405 resolve_lock_unlock (code);
10406 break;
10408 case EXEC_ENTRY:
10409 /* Keep track of which entry we are up to. */
10410 current_entry_id = code->ext.entry->id;
10411 break;
10413 case EXEC_WHERE:
10414 resolve_where (code, NULL);
10415 break;
10417 case EXEC_GOTO:
10418 if (code->expr1 != NULL)
10420 if (code->expr1->ts.type != BT_INTEGER)
10421 gfc_error ("ASSIGNED GOTO statement at %L requires an "
10422 "INTEGER variable", &code->expr1->where);
10423 else if (code->expr1->symtree->n.sym->attr.assign != 1)
10424 gfc_error ("Variable %qs has not been assigned a target "
10425 "label at %L", code->expr1->symtree->n.sym->name,
10426 &code->expr1->where);
10428 else
10429 resolve_branch (code->label1, code);
10430 break;
10432 case EXEC_RETURN:
10433 if (code->expr1 != NULL
10434 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
10435 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
10436 "INTEGER return specifier", &code->expr1->where);
10437 break;
10439 case EXEC_INIT_ASSIGN:
10440 case EXEC_END_PROCEDURE:
10441 break;
10443 case EXEC_ASSIGN:
10444 if (!t)
10445 break;
10447 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
10448 the LHS. */
10449 if (code->expr1->expr_type == EXPR_FUNCTION
10450 && code->expr1->value.function.isym
10451 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10452 remove_caf_get_intrinsic (code->expr1);
10454 /* If this is a pointer function in an lvalue variable context,
10455 the new code will have to be resolved afresh. This is also the
10456 case with an error, where the code is transformed into NOP to
10457 prevent ICEs downstream. */
10458 if (resolve_ptr_fcn_assign (&code, ns)
10459 || code->op == EXEC_NOP)
10460 goto start;
10462 if (!gfc_check_vardef_context (code->expr1, false, false, false,
10463 _("assignment")))
10464 break;
10466 if (resolve_ordinary_assign (code, ns))
10468 if (code->op == EXEC_COMPCALL)
10469 goto compcall;
10470 else
10471 goto call;
10474 /* Check for dependencies in deferred character length array
10475 assignments and generate a temporary, if necessary. */
10476 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
10477 break;
10479 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
10480 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
10481 && code->expr1->ts.u.derived
10482 && code->expr1->ts.u.derived->attr.defined_assign_comp)
10483 generate_component_assignments (&code, ns);
10485 break;
10487 case EXEC_LABEL_ASSIGN:
10488 if (code->label1->defined == ST_LABEL_UNKNOWN)
10489 gfc_error ("Label %d referenced at %L is never defined",
10490 code->label1->value, &code->label1->where);
10491 if (t
10492 && (code->expr1->expr_type != EXPR_VARIABLE
10493 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
10494 || code->expr1->symtree->n.sym->ts.kind
10495 != gfc_default_integer_kind
10496 || code->expr1->symtree->n.sym->as != NULL))
10497 gfc_error ("ASSIGN statement at %L requires a scalar "
10498 "default INTEGER variable", &code->expr1->where);
10499 break;
10501 case EXEC_POINTER_ASSIGN:
10503 gfc_expr* e;
10505 if (!t)
10506 break;
10508 /* This is both a variable definition and pointer assignment
10509 context, so check both of them. For rank remapping, a final
10510 array ref may be present on the LHS and fool gfc_expr_attr
10511 used in gfc_check_vardef_context. Remove it. */
10512 e = remove_last_array_ref (code->expr1);
10513 t = gfc_check_vardef_context (e, true, false, false,
10514 _("pointer assignment"));
10515 if (t)
10516 t = gfc_check_vardef_context (e, false, false, false,
10517 _("pointer assignment"));
10518 gfc_free_expr (e);
10519 if (!t)
10520 break;
10522 gfc_check_pointer_assign (code->expr1, code->expr2);
10523 break;
10526 case EXEC_ARITHMETIC_IF:
10528 gfc_expr *e = code->expr1;
10530 gfc_resolve_expr (e);
10531 if (e->expr_type == EXPR_NULL)
10532 gfc_error ("Invalid NULL at %L", &e->where);
10534 if (t && (e->rank > 0
10535 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
10536 gfc_error ("Arithmetic IF statement at %L requires a scalar "
10537 "REAL or INTEGER expression", &e->where);
10539 resolve_branch (code->label1, code);
10540 resolve_branch (code->label2, code);
10541 resolve_branch (code->label3, code);
10543 break;
10545 case EXEC_IF:
10546 if (t && code->expr1 != NULL
10547 && (code->expr1->ts.type != BT_LOGICAL
10548 || code->expr1->rank != 0))
10549 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10550 &code->expr1->where);
10551 break;
10553 case EXEC_CALL:
10554 call:
10555 resolve_call (code);
10556 break;
10558 case EXEC_COMPCALL:
10559 compcall:
10560 resolve_typebound_subroutine (code);
10561 break;
10563 case EXEC_CALL_PPC:
10564 resolve_ppc_call (code);
10565 break;
10567 case EXEC_SELECT:
10568 /* Select is complicated. Also, a SELECT construct could be
10569 a transformed computed GOTO. */
10570 resolve_select (code, false);
10571 break;
10573 case EXEC_SELECT_TYPE:
10574 resolve_select_type (code, ns);
10575 break;
10577 case EXEC_BLOCK:
10578 resolve_block_construct (code);
10579 break;
10581 case EXEC_DO:
10582 if (code->ext.iterator != NULL)
10584 gfc_iterator *iter = code->ext.iterator;
10585 if (gfc_resolve_iterator (iter, true, false))
10586 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
10588 break;
10590 case EXEC_DO_WHILE:
10591 if (code->expr1 == NULL)
10592 gfc_internal_error ("gfc_resolve_code(): No expression on "
10593 "DO WHILE");
10594 if (t
10595 && (code->expr1->rank != 0
10596 || code->expr1->ts.type != BT_LOGICAL))
10597 gfc_error ("Exit condition of DO WHILE loop at %L must be "
10598 "a scalar LOGICAL expression", &code->expr1->where);
10599 break;
10601 case EXEC_ALLOCATE:
10602 if (t)
10603 resolve_allocate_deallocate (code, "ALLOCATE");
10605 break;
10607 case EXEC_DEALLOCATE:
10608 if (t)
10609 resolve_allocate_deallocate (code, "DEALLOCATE");
10611 break;
10613 case EXEC_OPEN:
10614 if (!gfc_resolve_open (code->ext.open))
10615 break;
10617 resolve_branch (code->ext.open->err, code);
10618 break;
10620 case EXEC_CLOSE:
10621 if (!gfc_resolve_close (code->ext.close))
10622 break;
10624 resolve_branch (code->ext.close->err, code);
10625 break;
10627 case EXEC_BACKSPACE:
10628 case EXEC_ENDFILE:
10629 case EXEC_REWIND:
10630 case EXEC_FLUSH:
10631 if (!gfc_resolve_filepos (code->ext.filepos))
10632 break;
10634 resolve_branch (code->ext.filepos->err, code);
10635 break;
10637 case EXEC_INQUIRE:
10638 if (!gfc_resolve_inquire (code->ext.inquire))
10639 break;
10641 resolve_branch (code->ext.inquire->err, code);
10642 break;
10644 case EXEC_IOLENGTH:
10645 gcc_assert (code->ext.inquire != NULL);
10646 if (!gfc_resolve_inquire (code->ext.inquire))
10647 break;
10649 resolve_branch (code->ext.inquire->err, code);
10650 break;
10652 case EXEC_WAIT:
10653 if (!gfc_resolve_wait (code->ext.wait))
10654 break;
10656 resolve_branch (code->ext.wait->err, code);
10657 resolve_branch (code->ext.wait->end, code);
10658 resolve_branch (code->ext.wait->eor, code);
10659 break;
10661 case EXEC_READ:
10662 case EXEC_WRITE:
10663 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
10664 break;
10666 resolve_branch (code->ext.dt->err, code);
10667 resolve_branch (code->ext.dt->end, code);
10668 resolve_branch (code->ext.dt->eor, code);
10669 break;
10671 case EXEC_TRANSFER:
10672 resolve_transfer (code);
10673 break;
10675 case EXEC_DO_CONCURRENT:
10676 case EXEC_FORALL:
10677 resolve_forall_iterators (code->ext.forall_iterator);
10679 if (code->expr1 != NULL
10680 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
10681 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
10682 "expression", &code->expr1->where);
10683 break;
10685 case EXEC_OACC_PARALLEL_LOOP:
10686 case EXEC_OACC_PARALLEL:
10687 case EXEC_OACC_KERNELS_LOOP:
10688 case EXEC_OACC_KERNELS:
10689 case EXEC_OACC_DATA:
10690 case EXEC_OACC_HOST_DATA:
10691 case EXEC_OACC_LOOP:
10692 case EXEC_OACC_UPDATE:
10693 case EXEC_OACC_WAIT:
10694 case EXEC_OACC_CACHE:
10695 case EXEC_OACC_ENTER_DATA:
10696 case EXEC_OACC_EXIT_DATA:
10697 case EXEC_OACC_ATOMIC:
10698 case EXEC_OACC_DECLARE:
10699 gfc_resolve_oacc_directive (code, ns);
10700 break;
10702 case EXEC_OMP_ATOMIC:
10703 case EXEC_OMP_BARRIER:
10704 case EXEC_OMP_CANCEL:
10705 case EXEC_OMP_CANCELLATION_POINT:
10706 case EXEC_OMP_CRITICAL:
10707 case EXEC_OMP_FLUSH:
10708 case EXEC_OMP_DISTRIBUTE:
10709 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10710 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10711 case EXEC_OMP_DISTRIBUTE_SIMD:
10712 case EXEC_OMP_DO:
10713 case EXEC_OMP_DO_SIMD:
10714 case EXEC_OMP_MASTER:
10715 case EXEC_OMP_ORDERED:
10716 case EXEC_OMP_SECTIONS:
10717 case EXEC_OMP_SIMD:
10718 case EXEC_OMP_SINGLE:
10719 case EXEC_OMP_TARGET:
10720 case EXEC_OMP_TARGET_DATA:
10721 case EXEC_OMP_TARGET_TEAMS:
10722 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10723 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10724 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10725 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10726 case EXEC_OMP_TARGET_UPDATE:
10727 case EXEC_OMP_TASK:
10728 case EXEC_OMP_TASKGROUP:
10729 case EXEC_OMP_TASKWAIT:
10730 case EXEC_OMP_TASKYIELD:
10731 case EXEC_OMP_TEAMS:
10732 case EXEC_OMP_TEAMS_DISTRIBUTE:
10733 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10734 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10735 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10736 case EXEC_OMP_WORKSHARE:
10737 gfc_resolve_omp_directive (code, ns);
10738 break;
10740 case EXEC_OMP_PARALLEL:
10741 case EXEC_OMP_PARALLEL_DO:
10742 case EXEC_OMP_PARALLEL_DO_SIMD:
10743 case EXEC_OMP_PARALLEL_SECTIONS:
10744 case EXEC_OMP_PARALLEL_WORKSHARE:
10745 omp_workshare_save = omp_workshare_flag;
10746 omp_workshare_flag = 0;
10747 gfc_resolve_omp_directive (code, ns);
10748 omp_workshare_flag = omp_workshare_save;
10749 break;
10751 default:
10752 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
10756 cs_base = frame.prev;
10760 /* Resolve initial values and make sure they are compatible with
10761 the variable. */
10763 static void
10764 resolve_values (gfc_symbol *sym)
10766 bool t;
10768 if (sym->value == NULL)
10769 return;
10771 if (sym->value->expr_type == EXPR_STRUCTURE)
10772 t= resolve_structure_cons (sym->value, 1);
10773 else
10774 t = gfc_resolve_expr (sym->value);
10776 if (!t)
10777 return;
10779 gfc_check_assign_symbol (sym, NULL, sym->value);
10783 /* Verify any BIND(C) derived types in the namespace so we can report errors
10784 for them once, rather than for each variable declared of that type. */
10786 static void
10787 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10789 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10790 && derived_sym->attr.is_bind_c == 1)
10791 verify_bind_c_derived_type (derived_sym);
10793 return;
10797 /* Verify that any binding labels used in a given namespace do not collide
10798 with the names or binding labels of any global symbols. Multiple INTERFACE
10799 for the same procedure are permitted. */
10801 static void
10802 gfc_verify_binding_labels (gfc_symbol *sym)
10804 gfc_gsymbol *gsym;
10805 const char *module;
10807 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
10808 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
10809 return;
10811 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10813 if (sym->module)
10814 module = sym->module;
10815 else if (sym->ns && sym->ns->proc_name
10816 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10817 module = sym->ns->proc_name->name;
10818 else if (sym->ns && sym->ns->parent
10819 && sym->ns && sym->ns->parent->proc_name
10820 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10821 module = sym->ns->parent->proc_name->name;
10822 else
10823 module = NULL;
10825 if (!gsym
10826 || (!gsym->defined
10827 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
10829 if (!gsym)
10830 gsym = gfc_get_gsymbol (sym->binding_label);
10831 gsym->where = sym->declared_at;
10832 gsym->sym_name = sym->name;
10833 gsym->binding_label = sym->binding_label;
10834 gsym->ns = sym->ns;
10835 gsym->mod_name = module;
10836 if (sym->attr.function)
10837 gsym->type = GSYM_FUNCTION;
10838 else if (sym->attr.subroutine)
10839 gsym->type = GSYM_SUBROUTINE;
10840 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
10841 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
10842 return;
10845 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
10847 gfc_error ("Variable %s with binding label %s at %L uses the same global "
10848 "identifier as entity at %L", sym->name,
10849 sym->binding_label, &sym->declared_at, &gsym->where);
10850 /* Clear the binding label to prevent checking multiple times. */
10851 sym->binding_label = NULL;
10854 else if (sym->attr.flavor == FL_VARIABLE && module
10855 && (strcmp (module, gsym->mod_name) != 0
10856 || strcmp (sym->name, gsym->sym_name) != 0))
10858 /* This can only happen if the variable is defined in a module - if it
10859 isn't the same module, reject it. */
10860 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
10861 "the same global identifier as entity at %L from module %s",
10862 sym->name, module, sym->binding_label,
10863 &sym->declared_at, &gsym->where, gsym->mod_name);
10864 sym->binding_label = NULL;
10866 else if ((sym->attr.function || sym->attr.subroutine)
10867 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
10868 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
10869 && sym != gsym->ns->proc_name
10870 && (module != gsym->mod_name
10871 || strcmp (gsym->sym_name, sym->name) != 0
10872 || (module && strcmp (module, gsym->mod_name) != 0)))
10874 /* Print an error if the procedure is defined multiple times; we have to
10875 exclude references to the same procedure via module association or
10876 multiple checks for the same procedure. */
10877 gfc_error ("Procedure %s with binding label %s at %L uses the same "
10878 "global identifier as entity at %L", sym->name,
10879 sym->binding_label, &sym->declared_at, &gsym->where);
10880 sym->binding_label = NULL;
10885 /* Resolve an index expression. */
10887 static bool
10888 resolve_index_expr (gfc_expr *e)
10890 if (!gfc_resolve_expr (e))
10891 return false;
10893 if (!gfc_simplify_expr (e, 0))
10894 return false;
10896 if (!gfc_specification_expr (e))
10897 return false;
10899 return true;
10903 /* Resolve a charlen structure. */
10905 static bool
10906 resolve_charlen (gfc_charlen *cl)
10908 int i, k;
10909 bool saved_specification_expr;
10911 if (cl->resolved)
10912 return true;
10914 cl->resolved = 1;
10915 saved_specification_expr = specification_expr;
10916 specification_expr = true;
10918 if (cl->length_from_typespec)
10920 if (!gfc_resolve_expr (cl->length))
10922 specification_expr = saved_specification_expr;
10923 return false;
10926 if (!gfc_simplify_expr (cl->length, 0))
10928 specification_expr = saved_specification_expr;
10929 return false;
10932 else
10935 if (!resolve_index_expr (cl->length))
10937 specification_expr = saved_specification_expr;
10938 return false;
10942 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
10943 a negative value, the length of character entities declared is zero. */
10944 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
10945 gfc_replace_expr (cl->length,
10946 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
10948 /* Check that the character length is not too large. */
10949 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
10950 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
10951 && cl->length->ts.type == BT_INTEGER
10952 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
10954 gfc_error ("String length at %L is too large", &cl->length->where);
10955 specification_expr = saved_specification_expr;
10956 return false;
10959 specification_expr = saved_specification_expr;
10960 return true;
10964 /* Test for non-constant shape arrays. */
10966 static bool
10967 is_non_constant_shape_array (gfc_symbol *sym)
10969 gfc_expr *e;
10970 int i;
10971 bool not_constant;
10973 not_constant = false;
10974 if (sym->as != NULL)
10976 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
10977 has not been simplified; parameter array references. Do the
10978 simplification now. */
10979 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
10981 e = sym->as->lower[i];
10982 if (e && (!resolve_index_expr(e)
10983 || !gfc_is_constant_expr (e)))
10984 not_constant = true;
10985 e = sym->as->upper[i];
10986 if (e && (!resolve_index_expr(e)
10987 || !gfc_is_constant_expr (e)))
10988 not_constant = true;
10991 return not_constant;
10994 /* Given a symbol and an initialization expression, add code to initialize
10995 the symbol to the function entry. */
10996 static void
10997 build_init_assign (gfc_symbol *sym, gfc_expr *init)
10999 gfc_expr *lval;
11000 gfc_code *init_st;
11001 gfc_namespace *ns = sym->ns;
11003 /* Search for the function namespace if this is a contained
11004 function without an explicit result. */
11005 if (sym->attr.function && sym == sym->result
11006 && sym->name != sym->ns->proc_name->name)
11008 ns = ns->contained;
11009 for (;ns; ns = ns->sibling)
11010 if (strcmp (ns->proc_name->name, sym->name) == 0)
11011 break;
11014 if (ns == NULL)
11016 gfc_free_expr (init);
11017 return;
11020 /* Build an l-value expression for the result. */
11021 lval = gfc_lval_expr_from_sym (sym);
11023 /* Add the code at scope entry. */
11024 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
11025 init_st->next = ns->code;
11026 ns->code = init_st;
11028 /* Assign the default initializer to the l-value. */
11029 init_st->loc = sym->declared_at;
11030 init_st->expr1 = lval;
11031 init_st->expr2 = init;
11034 /* Assign the default initializer to a derived type variable or result. */
11036 static void
11037 apply_default_init (gfc_symbol *sym)
11039 gfc_expr *init = NULL;
11041 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11042 return;
11044 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
11045 init = gfc_default_initializer (&sym->ts);
11047 if (init == NULL && sym->ts.type != BT_CLASS)
11048 return;
11050 build_init_assign (sym, init);
11051 sym->attr.referenced = 1;
11054 /* Build an initializer for a local integer, real, complex, logical, or
11055 character variable, based on the command line flags finit-local-zero,
11056 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
11057 null if the symbol should not have a default initialization. */
11058 static gfc_expr *
11059 build_default_init_expr (gfc_symbol *sym)
11061 int char_len;
11062 gfc_expr *init_expr;
11063 int i;
11065 /* These symbols should never have a default initialization. */
11066 if (sym->attr.allocatable
11067 || sym->attr.external
11068 || sym->attr.dummy
11069 || sym->attr.pointer
11070 || sym->attr.in_equivalence
11071 || sym->attr.in_common
11072 || sym->attr.data
11073 || sym->module
11074 || sym->attr.cray_pointee
11075 || sym->attr.cray_pointer
11076 || sym->assoc)
11077 return NULL;
11079 /* Now we'll try to build an initializer expression. */
11080 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
11081 &sym->declared_at);
11083 /* We will only initialize integers, reals, complex, logicals, and
11084 characters, and only if the corresponding command-line flags
11085 were set. Otherwise, we free init_expr and return null. */
11086 switch (sym->ts.type)
11088 case BT_INTEGER:
11089 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
11090 mpz_set_si (init_expr->value.integer,
11091 gfc_option.flag_init_integer_value);
11092 else
11094 gfc_free_expr (init_expr);
11095 init_expr = NULL;
11097 break;
11099 case BT_REAL:
11100 switch (flag_init_real)
11102 case GFC_INIT_REAL_SNAN:
11103 init_expr->is_snan = 1;
11104 /* Fall through. */
11105 case GFC_INIT_REAL_NAN:
11106 mpfr_set_nan (init_expr->value.real);
11107 break;
11109 case GFC_INIT_REAL_INF:
11110 mpfr_set_inf (init_expr->value.real, 1);
11111 break;
11113 case GFC_INIT_REAL_NEG_INF:
11114 mpfr_set_inf (init_expr->value.real, -1);
11115 break;
11117 case GFC_INIT_REAL_ZERO:
11118 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
11119 break;
11121 default:
11122 gfc_free_expr (init_expr);
11123 init_expr = NULL;
11124 break;
11126 break;
11128 case BT_COMPLEX:
11129 switch (flag_init_real)
11131 case GFC_INIT_REAL_SNAN:
11132 init_expr->is_snan = 1;
11133 /* Fall through. */
11134 case GFC_INIT_REAL_NAN:
11135 mpfr_set_nan (mpc_realref (init_expr->value.complex));
11136 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
11137 break;
11139 case GFC_INIT_REAL_INF:
11140 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
11141 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
11142 break;
11144 case GFC_INIT_REAL_NEG_INF:
11145 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
11146 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
11147 break;
11149 case GFC_INIT_REAL_ZERO:
11150 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
11151 break;
11153 default:
11154 gfc_free_expr (init_expr);
11155 init_expr = NULL;
11156 break;
11158 break;
11160 case BT_LOGICAL:
11161 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
11162 init_expr->value.logical = 0;
11163 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
11164 init_expr->value.logical = 1;
11165 else
11167 gfc_free_expr (init_expr);
11168 init_expr = NULL;
11170 break;
11172 case BT_CHARACTER:
11173 /* For characters, the length must be constant in order to
11174 create a default initializer. */
11175 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
11176 && sym->ts.u.cl->length
11177 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11179 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
11180 init_expr->value.character.length = char_len;
11181 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
11182 for (i = 0; i < char_len; i++)
11183 init_expr->value.character.string[i]
11184 = (unsigned char) gfc_option.flag_init_character_value;
11186 else
11188 gfc_free_expr (init_expr);
11189 init_expr = NULL;
11191 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
11192 && sym->ts.u.cl->length && flag_max_stack_var_size != 0)
11194 gfc_actual_arglist *arg;
11195 init_expr = gfc_get_expr ();
11196 init_expr->where = sym->declared_at;
11197 init_expr->ts = sym->ts;
11198 init_expr->expr_type = EXPR_FUNCTION;
11199 init_expr->value.function.isym =
11200 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
11201 init_expr->value.function.name = "repeat";
11202 arg = gfc_get_actual_arglist ();
11203 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
11204 NULL, 1);
11205 arg->expr->value.character.string[0]
11206 = gfc_option.flag_init_character_value;
11207 arg->next = gfc_get_actual_arglist ();
11208 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
11209 init_expr->value.function.actual = arg;
11211 break;
11213 default:
11214 gfc_free_expr (init_expr);
11215 init_expr = NULL;
11217 return init_expr;
11220 /* Add an initialization expression to a local variable. */
11221 static void
11222 apply_default_init_local (gfc_symbol *sym)
11224 gfc_expr *init = NULL;
11226 /* The symbol should be a variable or a function return value. */
11227 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11228 || (sym->attr.function && sym->result != sym))
11229 return;
11231 /* Try to build the initializer expression. If we can't initialize
11232 this symbol, then init will be NULL. */
11233 init = build_default_init_expr (sym);
11234 if (init == NULL)
11235 return;
11237 /* For saved variables, we don't want to add an initializer at function
11238 entry, so we just add a static initializer. Note that automatic variables
11239 are stack allocated even with -fno-automatic; we have also to exclude
11240 result variable, which are also nonstatic. */
11241 if (sym->attr.save || sym->ns->save_all
11242 || (flag_max_stack_var_size == 0 && !sym->attr.result
11243 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
11244 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
11246 /* Don't clobber an existing initializer! */
11247 gcc_assert (sym->value == NULL);
11248 sym->value = init;
11249 return;
11252 build_init_assign (sym, init);
11256 /* Resolution of common features of flavors variable and procedure. */
11258 static bool
11259 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
11261 gfc_array_spec *as;
11263 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11264 as = CLASS_DATA (sym)->as;
11265 else
11266 as = sym->as;
11268 /* Constraints on deferred shape variable. */
11269 if (as == NULL || as->type != AS_DEFERRED)
11271 bool pointer, allocatable, dimension;
11273 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11275 pointer = CLASS_DATA (sym)->attr.class_pointer;
11276 allocatable = CLASS_DATA (sym)->attr.allocatable;
11277 dimension = CLASS_DATA (sym)->attr.dimension;
11279 else
11281 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
11282 allocatable = sym->attr.allocatable;
11283 dimension = sym->attr.dimension;
11286 if (allocatable)
11288 if (dimension && as->type != AS_ASSUMED_RANK)
11290 gfc_error ("Allocatable array %qs at %L must have a deferred "
11291 "shape or assumed rank", sym->name, &sym->declared_at);
11292 return false;
11294 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
11295 "%qs at %L may not be ALLOCATABLE",
11296 sym->name, &sym->declared_at))
11297 return false;
11300 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
11302 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
11303 "assumed rank", sym->name, &sym->declared_at);
11304 return false;
11307 else
11309 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
11310 && sym->ts.type != BT_CLASS && !sym->assoc)
11312 gfc_error ("Array %qs at %L cannot have a deferred shape",
11313 sym->name, &sym->declared_at);
11314 return false;
11318 /* Constraints on polymorphic variables. */
11319 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
11321 /* F03:C502. */
11322 if (sym->attr.class_ok
11323 && !sym->attr.select_type_temporary
11324 && !UNLIMITED_POLY (sym)
11325 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
11327 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
11328 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
11329 &sym->declared_at);
11330 return false;
11333 /* F03:C509. */
11334 /* Assume that use associated symbols were checked in the module ns.
11335 Class-variables that are associate-names are also something special
11336 and excepted from the test. */
11337 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
11339 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
11340 "or pointer", sym->name, &sym->declared_at);
11341 return false;
11345 return true;
11349 /* Additional checks for symbols with flavor variable and derived
11350 type. To be called from resolve_fl_variable. */
11352 static bool
11353 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
11355 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
11357 /* Check to see if a derived type is blocked from being host
11358 associated by the presence of another class I symbol in the same
11359 namespace. 14.6.1.3 of the standard and the discussion on
11360 comp.lang.fortran. */
11361 if (sym->ns != sym->ts.u.derived->ns
11362 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
11364 gfc_symbol *s;
11365 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
11366 if (s && s->attr.generic)
11367 s = gfc_find_dt_in_generic (s);
11368 if (s && s->attr.flavor != FL_DERIVED)
11370 gfc_error ("The type %qs cannot be host associated at %L "
11371 "because it is blocked by an incompatible object "
11372 "of the same name declared at %L",
11373 sym->ts.u.derived->name, &sym->declared_at,
11374 &s->declared_at);
11375 return false;
11379 /* 4th constraint in section 11.3: "If an object of a type for which
11380 component-initialization is specified (R429) appears in the
11381 specification-part of a module and does not have the ALLOCATABLE
11382 or POINTER attribute, the object shall have the SAVE attribute."
11384 The check for initializers is performed with
11385 gfc_has_default_initializer because gfc_default_initializer generates
11386 a hidden default for allocatable components. */
11387 if (!(sym->value || no_init_flag) && sym->ns->proc_name
11388 && sym->ns->proc_name->attr.flavor == FL_MODULE
11389 && !sym->ns->save_all && !sym->attr.save
11390 && !sym->attr.pointer && !sym->attr.allocatable
11391 && gfc_has_default_initializer (sym->ts.u.derived)
11392 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
11393 "%qs at %L, needed due to the default "
11394 "initialization", sym->name, &sym->declared_at))
11395 return false;
11397 /* Assign default initializer. */
11398 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
11399 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
11401 sym->value = gfc_default_initializer (&sym->ts);
11404 return true;
11408 /* Resolve symbols with flavor variable. */
11410 static bool
11411 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
11413 int no_init_flag, automatic_flag;
11414 gfc_expr *e;
11415 const char *auto_save_msg;
11416 bool saved_specification_expr;
11418 auto_save_msg = "Automatic object %qs at %L cannot have the "
11419 "SAVE attribute";
11421 if (!resolve_fl_var_and_proc (sym, mp_flag))
11422 return false;
11424 /* Set this flag to check that variables are parameters of all entries.
11425 This check is effected by the call to gfc_resolve_expr through
11426 is_non_constant_shape_array. */
11427 saved_specification_expr = specification_expr;
11428 specification_expr = true;
11430 if (sym->ns->proc_name
11431 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11432 || sym->ns->proc_name->attr.is_main_program)
11433 && !sym->attr.use_assoc
11434 && !sym->attr.allocatable
11435 && !sym->attr.pointer
11436 && is_non_constant_shape_array (sym))
11438 /* The shape of a main program or module array needs to be
11439 constant. */
11440 gfc_error ("The module or main program array %qs at %L must "
11441 "have constant shape", sym->name, &sym->declared_at);
11442 specification_expr = saved_specification_expr;
11443 return false;
11446 /* Constraints on deferred type parameter. */
11447 if (sym->ts.deferred
11448 && !(sym->attr.pointer
11449 || sym->attr.allocatable
11450 || sym->attr.omp_udr_artificial_var))
11452 gfc_error ("Entity %qs at %L has a deferred type parameter and "
11453 "requires either the pointer or allocatable attribute",
11454 sym->name, &sym->declared_at);
11455 specification_expr = saved_specification_expr;
11456 return false;
11459 if (sym->ts.type == BT_CHARACTER)
11461 /* Make sure that character string variables with assumed length are
11462 dummy arguments. */
11463 e = sym->ts.u.cl->length;
11464 if (e == NULL && !sym->attr.dummy && !sym->attr.result
11465 && !sym->ts.deferred && !sym->attr.select_type_temporary
11466 && !sym->attr.omp_udr_artificial_var)
11468 gfc_error ("Entity with assumed character length at %L must be a "
11469 "dummy argument or a PARAMETER", &sym->declared_at);
11470 specification_expr = saved_specification_expr;
11471 return false;
11474 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
11476 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
11477 specification_expr = saved_specification_expr;
11478 return false;
11481 if (!gfc_is_constant_expr (e)
11482 && !(e->expr_type == EXPR_VARIABLE
11483 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
11485 if (!sym->attr.use_assoc && sym->ns->proc_name
11486 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11487 || sym->ns->proc_name->attr.is_main_program))
11489 gfc_error ("%qs at %L must have constant character length "
11490 "in this context", sym->name, &sym->declared_at);
11491 specification_expr = saved_specification_expr;
11492 return false;
11494 if (sym->attr.in_common)
11496 gfc_error ("COMMON variable %qs at %L must have constant "
11497 "character length", sym->name, &sym->declared_at);
11498 specification_expr = saved_specification_expr;
11499 return false;
11504 if (sym->value == NULL && sym->attr.referenced)
11505 apply_default_init_local (sym); /* Try to apply a default initialization. */
11507 /* Determine if the symbol may not have an initializer. */
11508 no_init_flag = automatic_flag = 0;
11509 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
11510 || sym->attr.intrinsic || sym->attr.result)
11511 no_init_flag = 1;
11512 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
11513 && is_non_constant_shape_array (sym))
11515 no_init_flag = automatic_flag = 1;
11517 /* Also, they must not have the SAVE attribute.
11518 SAVE_IMPLICIT is checked below. */
11519 if (sym->as && sym->attr.codimension)
11521 int corank = sym->as->corank;
11522 sym->as->corank = 0;
11523 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
11524 sym->as->corank = corank;
11526 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
11528 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
11529 specification_expr = saved_specification_expr;
11530 return false;
11534 /* Ensure that any initializer is simplified. */
11535 if (sym->value)
11536 gfc_simplify_expr (sym->value, 1);
11538 /* Reject illegal initializers. */
11539 if (!sym->mark && sym->value)
11541 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
11542 && CLASS_DATA (sym)->attr.allocatable))
11543 gfc_error ("Allocatable %qs at %L cannot have an initializer",
11544 sym->name, &sym->declared_at);
11545 else if (sym->attr.external)
11546 gfc_error ("External %qs at %L cannot have an initializer",
11547 sym->name, &sym->declared_at);
11548 else if (sym->attr.dummy
11549 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
11550 gfc_error ("Dummy %qs at %L cannot have an initializer",
11551 sym->name, &sym->declared_at);
11552 else if (sym->attr.intrinsic)
11553 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
11554 sym->name, &sym->declared_at);
11555 else if (sym->attr.result)
11556 gfc_error ("Function result %qs at %L cannot have an initializer",
11557 sym->name, &sym->declared_at);
11558 else if (automatic_flag)
11559 gfc_error ("Automatic array %qs at %L cannot have an initializer",
11560 sym->name, &sym->declared_at);
11561 else
11562 goto no_init_error;
11563 specification_expr = saved_specification_expr;
11564 return false;
11567 no_init_error:
11568 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
11570 bool res = resolve_fl_variable_derived (sym, no_init_flag);
11571 specification_expr = saved_specification_expr;
11572 return res;
11575 specification_expr = saved_specification_expr;
11576 return true;
11580 /* Compare the dummy characteristics of a module procedure interface
11581 declaration with the corresponding declaration in a submodule. */
11582 static gfc_formal_arglist *new_formal;
11583 static char errmsg[200];
11585 static void
11586 compare_fsyms (gfc_symbol *sym)
11588 gfc_symbol *fsym;
11590 if (sym == NULL || new_formal == NULL)
11591 return;
11593 fsym = new_formal->sym;
11595 if (sym == fsym)
11596 return;
11598 if (strcmp (sym->name, fsym->name) == 0)
11600 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
11601 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
11606 /* Resolve a procedure. */
11608 static bool
11609 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
11611 gfc_formal_arglist *arg;
11613 if (sym->attr.function
11614 && !resolve_fl_var_and_proc (sym, mp_flag))
11615 return false;
11617 if (sym->ts.type == BT_CHARACTER)
11619 gfc_charlen *cl = sym->ts.u.cl;
11621 if (cl && cl->length && gfc_is_constant_expr (cl->length)
11622 && !resolve_charlen (cl))
11623 return false;
11625 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11626 && sym->attr.proc == PROC_ST_FUNCTION)
11628 gfc_error ("Character-valued statement function %qs at %L must "
11629 "have constant length", sym->name, &sym->declared_at);
11630 return false;
11634 /* Ensure that derived type for are not of a private type. Internal
11635 module procedures are excluded by 2.2.3.3 - i.e., they are not
11636 externally accessible and can access all the objects accessible in
11637 the host. */
11638 if (!(sym->ns->parent
11639 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
11640 && gfc_check_symbol_access (sym))
11642 gfc_interface *iface;
11644 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
11646 if (arg->sym
11647 && arg->sym->ts.type == BT_DERIVED
11648 && !arg->sym->ts.u.derived->attr.use_assoc
11649 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11650 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
11651 "and cannot be a dummy argument"
11652 " of %qs, which is PUBLIC at %L",
11653 arg->sym->name, sym->name,
11654 &sym->declared_at))
11656 /* Stop this message from recurring. */
11657 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11658 return false;
11662 /* PUBLIC interfaces may expose PRIVATE procedures that take types
11663 PRIVATE to the containing module. */
11664 for (iface = sym->generic; iface; iface = iface->next)
11666 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
11668 if (arg->sym
11669 && arg->sym->ts.type == BT_DERIVED
11670 && !arg->sym->ts.u.derived->attr.use_assoc
11671 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11672 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
11673 "PUBLIC interface %qs at %L "
11674 "takes dummy arguments of %qs which "
11675 "is PRIVATE", iface->sym->name,
11676 sym->name, &iface->sym->declared_at,
11677 gfc_typename(&arg->sym->ts)))
11679 /* Stop this message from recurring. */
11680 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11681 return false;
11687 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
11688 && !sym->attr.proc_pointer)
11690 gfc_error ("Function %qs at %L cannot have an initializer",
11691 sym->name, &sym->declared_at);
11692 return false;
11695 /* An external symbol may not have an initializer because it is taken to be
11696 a procedure. Exception: Procedure Pointers. */
11697 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
11699 gfc_error ("External object %qs at %L may not have an initializer",
11700 sym->name, &sym->declared_at);
11701 return false;
11704 /* An elemental function is required to return a scalar 12.7.1 */
11705 if (sym->attr.elemental && sym->attr.function && sym->as)
11707 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
11708 "result", sym->name, &sym->declared_at);
11709 /* Reset so that the error only occurs once. */
11710 sym->attr.elemental = 0;
11711 return false;
11714 if (sym->attr.proc == PROC_ST_FUNCTION
11715 && (sym->attr.allocatable || sym->attr.pointer))
11717 gfc_error ("Statement function %qs at %L may not have pointer or "
11718 "allocatable attribute", sym->name, &sym->declared_at);
11719 return false;
11722 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
11723 char-len-param shall not be array-valued, pointer-valued, recursive
11724 or pure. ....snip... A character value of * may only be used in the
11725 following ways: (i) Dummy arg of procedure - dummy associates with
11726 actual length; (ii) To declare a named constant; or (iii) External
11727 function - but length must be declared in calling scoping unit. */
11728 if (sym->attr.function
11729 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
11730 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
11732 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
11733 || (sym->attr.recursive) || (sym->attr.pure))
11735 if (sym->as && sym->as->rank)
11736 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11737 "array-valued", sym->name, &sym->declared_at);
11739 if (sym->attr.pointer)
11740 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11741 "pointer-valued", sym->name, &sym->declared_at);
11743 if (sym->attr.pure)
11744 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11745 "pure", sym->name, &sym->declared_at);
11747 if (sym->attr.recursive)
11748 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
11749 "recursive", sym->name, &sym->declared_at);
11751 return false;
11754 /* Appendix B.2 of the standard. Contained functions give an
11755 error anyway. Deferred character length is an F2003 feature.
11756 Don't warn on intrinsic conversion functions, which start
11757 with two underscores. */
11758 if (!sym->attr.contained && !sym->ts.deferred
11759 && (sym->name[0] != '_' || sym->name[1] != '_'))
11760 gfc_notify_std (GFC_STD_F95_OBS,
11761 "CHARACTER(*) function %qs at %L",
11762 sym->name, &sym->declared_at);
11765 /* F2008, C1218. */
11766 if (sym->attr.elemental)
11768 if (sym->attr.proc_pointer)
11770 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
11771 sym->name, &sym->declared_at);
11772 return false;
11774 if (sym->attr.dummy)
11776 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
11777 sym->name, &sym->declared_at);
11778 return false;
11782 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11784 gfc_formal_arglist *curr_arg;
11785 int has_non_interop_arg = 0;
11787 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11788 sym->common_block))
11790 /* Clear these to prevent looking at them again if there was an
11791 error. */
11792 sym->attr.is_bind_c = 0;
11793 sym->attr.is_c_interop = 0;
11794 sym->ts.is_c_interop = 0;
11796 else
11798 /* So far, no errors have been found. */
11799 sym->attr.is_c_interop = 1;
11800 sym->ts.is_c_interop = 1;
11803 curr_arg = gfc_sym_get_dummy_args (sym);
11804 while (curr_arg != NULL)
11806 /* Skip implicitly typed dummy args here. */
11807 if (curr_arg->sym->attr.implicit_type == 0)
11808 if (!gfc_verify_c_interop_param (curr_arg->sym))
11809 /* If something is found to fail, record the fact so we
11810 can mark the symbol for the procedure as not being
11811 BIND(C) to try and prevent multiple errors being
11812 reported. */
11813 has_non_interop_arg = 1;
11815 curr_arg = curr_arg->next;
11818 /* See if any of the arguments were not interoperable and if so, clear
11819 the procedure symbol to prevent duplicate error messages. */
11820 if (has_non_interop_arg != 0)
11822 sym->attr.is_c_interop = 0;
11823 sym->ts.is_c_interop = 0;
11824 sym->attr.is_bind_c = 0;
11828 if (!sym->attr.proc_pointer)
11830 if (sym->attr.save == SAVE_EXPLICIT)
11832 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11833 "in %qs at %L", sym->name, &sym->declared_at);
11834 return false;
11836 if (sym->attr.intent)
11838 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11839 "in %qs at %L", sym->name, &sym->declared_at);
11840 return false;
11842 if (sym->attr.subroutine && sym->attr.result)
11844 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11845 "in %qs at %L", sym->name, &sym->declared_at);
11846 return false;
11848 if (sym->attr.external && sym->attr.function
11849 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11850 || sym->attr.contained))
11852 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11853 "in %qs at %L", sym->name, &sym->declared_at);
11854 return false;
11856 if (strcmp ("ppr@", sym->name) == 0)
11858 gfc_error ("Procedure pointer result %qs at %L "
11859 "is missing the pointer attribute",
11860 sym->ns->proc_name->name, &sym->declared_at);
11861 return false;
11865 /* Assume that a procedure whose body is not known has references
11866 to external arrays. */
11867 if (sym->attr.if_source != IFSRC_DECL)
11868 sym->attr.array_outer_dependency = 1;
11870 /* Compare the characteristics of a module procedure with the
11871 interface declaration. Ideally this would be done with
11872 gfc_compare_interfaces but, at present, the formal interface
11873 cannot be copied to the ts.interface. */
11874 if (sym->attr.module_procedure
11875 && sym->attr.if_source == IFSRC_DECL)
11877 gfc_symbol *iface;
11878 char name[2*GFC_MAX_SYMBOL_LEN + 1];
11879 char *module_name;
11880 char *submodule_name;
11881 strcpy (name, sym->ns->proc_name->name);
11882 module_name = strtok (name, ".");
11883 submodule_name = strtok (NULL, ".");
11885 /* Stop the dummy characteristics test from using the interface
11886 symbol instead of 'sym'. */
11887 iface = sym->ts.interface;
11888 sym->ts.interface = NULL;
11890 if (iface == NULL)
11891 goto check_formal;
11893 /* Check the procedure characteristics. */
11894 if (sym->attr.pure != iface->attr.pure)
11896 gfc_error ("Mismatch in PURE attribute between MODULE "
11897 "PROCEDURE at %L and its interface in %s",
11898 &sym->declared_at, module_name);
11899 return false;
11902 if (sym->attr.elemental != iface->attr.elemental)
11904 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
11905 "PROCEDURE at %L and its interface in %s",
11906 &sym->declared_at, module_name);
11907 return false;
11910 if (sym->attr.recursive != iface->attr.recursive)
11912 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
11913 "PROCEDURE at %L and its interface in %s",
11914 &sym->declared_at, module_name);
11915 return false;
11918 /* Check the result characteristics. */
11919 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
11921 gfc_error ("%s between the MODULE PROCEDURE declaration "
11922 "in module %s and the declaration at %L in "
11923 "SUBMODULE %s", errmsg, module_name,
11924 &sym->declared_at, submodule_name);
11925 return false;
11928 check_formal:
11929 /* Check the charcateristics of the formal arguments. */
11930 if (sym->formal && sym->formal_ns)
11932 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
11934 new_formal = arg;
11935 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
11939 sym->ts.interface = iface;
11941 return true;
11945 /* Resolve a list of finalizer procedures. That is, after they have hopefully
11946 been defined and we now know their defined arguments, check that they fulfill
11947 the requirements of the standard for procedures used as finalizers. */
11949 static bool
11950 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
11952 gfc_finalizer* list;
11953 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
11954 bool result = true;
11955 bool seen_scalar = false;
11956 gfc_symbol *vtab;
11957 gfc_component *c;
11958 gfc_symbol *parent = gfc_get_derived_super_type (derived);
11960 if (parent)
11961 gfc_resolve_finalizers (parent, finalizable);
11963 /* Return early when not finalizable. Additionally, ensure that derived-type
11964 components have a their finalizables resolved. */
11965 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
11967 bool has_final = false;
11968 for (c = derived->components; c; c = c->next)
11969 if (c->ts.type == BT_DERIVED
11970 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
11972 bool has_final2 = false;
11973 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final))
11974 return false; /* Error. */
11975 has_final = has_final || has_final2;
11977 if (!has_final)
11979 if (finalizable)
11980 *finalizable = false;
11981 return true;
11985 /* Walk over the list of finalizer-procedures, check them, and if any one
11986 does not fit in with the standard's definition, print an error and remove
11987 it from the list. */
11988 prev_link = &derived->f2k_derived->finalizers;
11989 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
11991 gfc_formal_arglist *dummy_args;
11992 gfc_symbol* arg;
11993 gfc_finalizer* i;
11994 int my_rank;
11996 /* Skip this finalizer if we already resolved it. */
11997 if (list->proc_tree)
11999 prev_link = &(list->next);
12000 continue;
12003 /* Check this exists and is a SUBROUTINE. */
12004 if (!list->proc_sym->attr.subroutine)
12006 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
12007 list->proc_sym->name, &list->where);
12008 goto error;
12011 /* We should have exactly one argument. */
12012 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
12013 if (!dummy_args || dummy_args->next)
12015 gfc_error ("FINAL procedure at %L must have exactly one argument",
12016 &list->where);
12017 goto error;
12019 arg = dummy_args->sym;
12021 /* This argument must be of our type. */
12022 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
12024 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
12025 &arg->declared_at, derived->name);
12026 goto error;
12029 /* It must neither be a pointer nor allocatable nor optional. */
12030 if (arg->attr.pointer)
12032 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
12033 &arg->declared_at);
12034 goto error;
12036 if (arg->attr.allocatable)
12038 gfc_error ("Argument of FINAL procedure at %L must not be"
12039 " ALLOCATABLE", &arg->declared_at);
12040 goto error;
12042 if (arg->attr.optional)
12044 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
12045 &arg->declared_at);
12046 goto error;
12049 /* It must not be INTENT(OUT). */
12050 if (arg->attr.intent == INTENT_OUT)
12052 gfc_error ("Argument of FINAL procedure at %L must not be"
12053 " INTENT(OUT)", &arg->declared_at);
12054 goto error;
12057 /* Warn if the procedure is non-scalar and not assumed shape. */
12058 if (warn_surprising && arg->as && arg->as->rank != 0
12059 && arg->as->type != AS_ASSUMED_SHAPE)
12060 gfc_warning (OPT_Wsurprising,
12061 "Non-scalar FINAL procedure at %L should have assumed"
12062 " shape argument", &arg->declared_at);
12064 /* Check that it does not match in kind and rank with a FINAL procedure
12065 defined earlier. To really loop over the *earlier* declarations,
12066 we need to walk the tail of the list as new ones were pushed at the
12067 front. */
12068 /* TODO: Handle kind parameters once they are implemented. */
12069 my_rank = (arg->as ? arg->as->rank : 0);
12070 for (i = list->next; i; i = i->next)
12072 gfc_formal_arglist *dummy_args;
12074 /* Argument list might be empty; that is an error signalled earlier,
12075 but we nevertheless continued resolving. */
12076 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
12077 if (dummy_args)
12079 gfc_symbol* i_arg = dummy_args->sym;
12080 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
12081 if (i_rank == my_rank)
12083 gfc_error ("FINAL procedure %qs declared at %L has the same"
12084 " rank (%d) as %qs",
12085 list->proc_sym->name, &list->where, my_rank,
12086 i->proc_sym->name);
12087 goto error;
12092 /* Is this the/a scalar finalizer procedure? */
12093 if (!arg->as || arg->as->rank == 0)
12094 seen_scalar = true;
12096 /* Find the symtree for this procedure. */
12097 gcc_assert (!list->proc_tree);
12098 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
12100 prev_link = &list->next;
12101 continue;
12103 /* Remove wrong nodes immediately from the list so we don't risk any
12104 troubles in the future when they might fail later expectations. */
12105 error:
12106 i = list;
12107 *prev_link = list->next;
12108 gfc_free_finalizer (i);
12109 result = false;
12112 if (result == false)
12113 return false;
12115 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
12116 were nodes in the list, must have been for arrays. It is surely a good
12117 idea to have a scalar version there if there's something to finalize. */
12118 if (warn_surprising && result && !seen_scalar)
12119 gfc_warning (OPT_Wsurprising,
12120 "Only array FINAL procedures declared for derived type %qs"
12121 " defined at %L, suggest also scalar one",
12122 derived->name, &derived->declared_at);
12124 vtab = gfc_find_derived_vtab (derived);
12125 c = vtab->ts.u.derived->components->next->next->next->next->next;
12126 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
12128 if (finalizable)
12129 *finalizable = true;
12131 return true;
12135 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
12137 static bool
12138 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
12139 const char* generic_name, locus where)
12141 gfc_symbol *sym1, *sym2;
12142 const char *pass1, *pass2;
12143 gfc_formal_arglist *dummy_args;
12145 gcc_assert (t1->specific && t2->specific);
12146 gcc_assert (!t1->specific->is_generic);
12147 gcc_assert (!t2->specific->is_generic);
12148 gcc_assert (t1->is_operator == t2->is_operator);
12150 sym1 = t1->specific->u.specific->n.sym;
12151 sym2 = t2->specific->u.specific->n.sym;
12153 if (sym1 == sym2)
12154 return true;
12156 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
12157 if (sym1->attr.subroutine != sym2->attr.subroutine
12158 || sym1->attr.function != sym2->attr.function)
12160 gfc_error ("%qs and %qs can't be mixed FUNCTION/SUBROUTINE for"
12161 " GENERIC %qs at %L",
12162 sym1->name, sym2->name, generic_name, &where);
12163 return false;
12166 /* Determine PASS arguments. */
12167 if (t1->specific->nopass)
12168 pass1 = NULL;
12169 else if (t1->specific->pass_arg)
12170 pass1 = t1->specific->pass_arg;
12171 else
12173 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
12174 if (dummy_args)
12175 pass1 = dummy_args->sym->name;
12176 else
12177 pass1 = NULL;
12179 if (t2->specific->nopass)
12180 pass2 = NULL;
12181 else if (t2->specific->pass_arg)
12182 pass2 = t2->specific->pass_arg;
12183 else
12185 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
12186 if (dummy_args)
12187 pass2 = dummy_args->sym->name;
12188 else
12189 pass2 = NULL;
12192 /* Compare the interfaces. */
12193 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
12194 NULL, 0, pass1, pass2))
12196 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
12197 sym1->name, sym2->name, generic_name, &where);
12198 return false;
12201 return true;
12205 /* Worker function for resolving a generic procedure binding; this is used to
12206 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
12208 The difference between those cases is finding possible inherited bindings
12209 that are overridden, as one has to look for them in tb_sym_root,
12210 tb_uop_root or tb_op, respectively. Thus the caller must already find
12211 the super-type and set p->overridden correctly. */
12213 static bool
12214 resolve_tb_generic_targets (gfc_symbol* super_type,
12215 gfc_typebound_proc* p, const char* name)
12217 gfc_tbp_generic* target;
12218 gfc_symtree* first_target;
12219 gfc_symtree* inherited;
12221 gcc_assert (p && p->is_generic);
12223 /* Try to find the specific bindings for the symtrees in our target-list. */
12224 gcc_assert (p->u.generic);
12225 for (target = p->u.generic; target; target = target->next)
12226 if (!target->specific)
12228 gfc_typebound_proc* overridden_tbp;
12229 gfc_tbp_generic* g;
12230 const char* target_name;
12232 target_name = target->specific_st->name;
12234 /* Defined for this type directly. */
12235 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
12237 target->specific = target->specific_st->n.tb;
12238 goto specific_found;
12241 /* Look for an inherited specific binding. */
12242 if (super_type)
12244 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
12245 true, NULL);
12247 if (inherited)
12249 gcc_assert (inherited->n.tb);
12250 target->specific = inherited->n.tb;
12251 goto specific_found;
12255 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
12256 " at %L", target_name, name, &p->where);
12257 return false;
12259 /* Once we've found the specific binding, check it is not ambiguous with
12260 other specifics already found or inherited for the same GENERIC. */
12261 specific_found:
12262 gcc_assert (target->specific);
12264 /* This must really be a specific binding! */
12265 if (target->specific->is_generic)
12267 gfc_error ("GENERIC %qs at %L must target a specific binding,"
12268 " %qs is GENERIC, too", name, &p->where, target_name);
12269 return false;
12272 /* Check those already resolved on this type directly. */
12273 for (g = p->u.generic; g; g = g->next)
12274 if (g != target && g->specific
12275 && !check_generic_tbp_ambiguity (target, g, name, p->where))
12276 return false;
12278 /* Check for ambiguity with inherited specific targets. */
12279 for (overridden_tbp = p->overridden; overridden_tbp;
12280 overridden_tbp = overridden_tbp->overridden)
12281 if (overridden_tbp->is_generic)
12283 for (g = overridden_tbp->u.generic; g; g = g->next)
12285 gcc_assert (g->specific);
12286 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
12287 return false;
12292 /* If we attempt to "overwrite" a specific binding, this is an error. */
12293 if (p->overridden && !p->overridden->is_generic)
12295 gfc_error ("GENERIC %qs at %L can't overwrite specific binding with"
12296 " the same name", name, &p->where);
12297 return false;
12300 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
12301 all must have the same attributes here. */
12302 first_target = p->u.generic->specific->u.specific;
12303 gcc_assert (first_target);
12304 p->subroutine = first_target->n.sym->attr.subroutine;
12305 p->function = first_target->n.sym->attr.function;
12307 return true;
12311 /* Resolve a GENERIC procedure binding for a derived type. */
12313 static bool
12314 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
12316 gfc_symbol* super_type;
12318 /* Find the overridden binding if any. */
12319 st->n.tb->overridden = NULL;
12320 super_type = gfc_get_derived_super_type (derived);
12321 if (super_type)
12323 gfc_symtree* overridden;
12324 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
12325 true, NULL);
12327 if (overridden && overridden->n.tb)
12328 st->n.tb->overridden = overridden->n.tb;
12331 /* Resolve using worker function. */
12332 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
12336 /* Retrieve the target-procedure of an operator binding and do some checks in
12337 common for intrinsic and user-defined type-bound operators. */
12339 static gfc_symbol*
12340 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
12342 gfc_symbol* target_proc;
12344 gcc_assert (target->specific && !target->specific->is_generic);
12345 target_proc = target->specific->u.specific->n.sym;
12346 gcc_assert (target_proc);
12348 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
12349 if (target->specific->nopass)
12351 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
12352 return NULL;
12355 return target_proc;
12359 /* Resolve a type-bound intrinsic operator. */
12361 static bool
12362 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
12363 gfc_typebound_proc* p)
12365 gfc_symbol* super_type;
12366 gfc_tbp_generic* target;
12368 /* If there's already an error here, do nothing (but don't fail again). */
12369 if (p->error)
12370 return true;
12372 /* Operators should always be GENERIC bindings. */
12373 gcc_assert (p->is_generic);
12375 /* Look for an overridden binding. */
12376 super_type = gfc_get_derived_super_type (derived);
12377 if (super_type && super_type->f2k_derived)
12378 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
12379 op, true, NULL);
12380 else
12381 p->overridden = NULL;
12383 /* Resolve general GENERIC properties using worker function. */
12384 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
12385 goto error;
12387 /* Check the targets to be procedures of correct interface. */
12388 for (target = p->u.generic; target; target = target->next)
12390 gfc_symbol* target_proc;
12392 target_proc = get_checked_tb_operator_target (target, p->where);
12393 if (!target_proc)
12394 goto error;
12396 if (!gfc_check_operator_interface (target_proc, op, p->where))
12397 goto error;
12399 /* Add target to non-typebound operator list. */
12400 if (!target->specific->deferred && !derived->attr.use_assoc
12401 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
12403 gfc_interface *head, *intr;
12404 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
12405 return false;
12406 head = derived->ns->op[op];
12407 intr = gfc_get_interface ();
12408 intr->sym = target_proc;
12409 intr->where = p->where;
12410 intr->next = head;
12411 derived->ns->op[op] = intr;
12415 return true;
12417 error:
12418 p->error = 1;
12419 return false;
12423 /* Resolve a type-bound user operator (tree-walker callback). */
12425 static gfc_symbol* resolve_bindings_derived;
12426 static bool resolve_bindings_result;
12428 static bool check_uop_procedure (gfc_symbol* sym, locus where);
12430 static void
12431 resolve_typebound_user_op (gfc_symtree* stree)
12433 gfc_symbol* super_type;
12434 gfc_tbp_generic* target;
12436 gcc_assert (stree && stree->n.tb);
12438 if (stree->n.tb->error)
12439 return;
12441 /* Operators should always be GENERIC bindings. */
12442 gcc_assert (stree->n.tb->is_generic);
12444 /* Find overridden procedure, if any. */
12445 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12446 if (super_type && super_type->f2k_derived)
12448 gfc_symtree* overridden;
12449 overridden = gfc_find_typebound_user_op (super_type, NULL,
12450 stree->name, true, NULL);
12452 if (overridden && overridden->n.tb)
12453 stree->n.tb->overridden = overridden->n.tb;
12455 else
12456 stree->n.tb->overridden = NULL;
12458 /* Resolve basically using worker function. */
12459 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
12460 goto error;
12462 /* Check the targets to be functions of correct interface. */
12463 for (target = stree->n.tb->u.generic; target; target = target->next)
12465 gfc_symbol* target_proc;
12467 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
12468 if (!target_proc)
12469 goto error;
12471 if (!check_uop_procedure (target_proc, stree->n.tb->where))
12472 goto error;
12475 return;
12477 error:
12478 resolve_bindings_result = false;
12479 stree->n.tb->error = 1;
12483 /* Resolve the type-bound procedures for a derived type. */
12485 static void
12486 resolve_typebound_procedure (gfc_symtree* stree)
12488 gfc_symbol* proc;
12489 locus where;
12490 gfc_symbol* me_arg;
12491 gfc_symbol* super_type;
12492 gfc_component* comp;
12494 gcc_assert (stree);
12496 /* Undefined specific symbol from GENERIC target definition. */
12497 if (!stree->n.tb)
12498 return;
12500 if (stree->n.tb->error)
12501 return;
12503 /* If this is a GENERIC binding, use that routine. */
12504 if (stree->n.tb->is_generic)
12506 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
12507 goto error;
12508 return;
12511 /* Get the target-procedure to check it. */
12512 gcc_assert (!stree->n.tb->is_generic);
12513 gcc_assert (stree->n.tb->u.specific);
12514 proc = stree->n.tb->u.specific->n.sym;
12515 where = stree->n.tb->where;
12517 /* Default access should already be resolved from the parser. */
12518 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
12520 if (stree->n.tb->deferred)
12522 if (!check_proc_interface (proc, &where))
12523 goto error;
12525 else
12527 /* Check for F08:C465. */
12528 if ((!proc->attr.subroutine && !proc->attr.function)
12529 || (proc->attr.proc != PROC_MODULE
12530 && proc->attr.if_source != IFSRC_IFBODY)
12531 || proc->attr.abstract)
12533 gfc_error ("%qs must be a module procedure or an external procedure with"
12534 " an explicit interface at %L", proc->name, &where);
12535 goto error;
12539 stree->n.tb->subroutine = proc->attr.subroutine;
12540 stree->n.tb->function = proc->attr.function;
12542 /* Find the super-type of the current derived type. We could do this once and
12543 store in a global if speed is needed, but as long as not I believe this is
12544 more readable and clearer. */
12545 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12547 /* If PASS, resolve and check arguments if not already resolved / loaded
12548 from a .mod file. */
12549 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
12551 gfc_formal_arglist *dummy_args;
12553 dummy_args = gfc_sym_get_dummy_args (proc);
12554 if (stree->n.tb->pass_arg)
12556 gfc_formal_arglist *i;
12558 /* If an explicit passing argument name is given, walk the arg-list
12559 and look for it. */
12561 me_arg = NULL;
12562 stree->n.tb->pass_arg_num = 1;
12563 for (i = dummy_args; i; i = i->next)
12565 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
12567 me_arg = i->sym;
12568 break;
12570 ++stree->n.tb->pass_arg_num;
12573 if (!me_arg)
12575 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
12576 " argument %qs",
12577 proc->name, stree->n.tb->pass_arg, &where,
12578 stree->n.tb->pass_arg);
12579 goto error;
12582 else
12584 /* Otherwise, take the first one; there should in fact be at least
12585 one. */
12586 stree->n.tb->pass_arg_num = 1;
12587 if (!dummy_args)
12589 gfc_error ("Procedure %qs with PASS at %L must have at"
12590 " least one argument", proc->name, &where);
12591 goto error;
12593 me_arg = dummy_args->sym;
12596 /* Now check that the argument-type matches and the passed-object
12597 dummy argument is generally fine. */
12599 gcc_assert (me_arg);
12601 if (me_arg->ts.type != BT_CLASS)
12603 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
12604 " at %L", proc->name, &where);
12605 goto error;
12608 if (CLASS_DATA (me_arg)->ts.u.derived
12609 != resolve_bindings_derived)
12611 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
12612 " the derived-type %qs", me_arg->name, proc->name,
12613 me_arg->name, &where, resolve_bindings_derived->name);
12614 goto error;
12617 gcc_assert (me_arg->ts.type == BT_CLASS);
12618 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
12620 gfc_error ("Passed-object dummy argument of %qs at %L must be"
12621 " scalar", proc->name, &where);
12622 goto error;
12624 if (CLASS_DATA (me_arg)->attr.allocatable)
12626 gfc_error ("Passed-object dummy argument of %qs at %L must not"
12627 " be ALLOCATABLE", proc->name, &where);
12628 goto error;
12630 if (CLASS_DATA (me_arg)->attr.class_pointer)
12632 gfc_error ("Passed-object dummy argument of %qs at %L must not"
12633 " be POINTER", proc->name, &where);
12634 goto error;
12638 /* If we are extending some type, check that we don't override a procedure
12639 flagged NON_OVERRIDABLE. */
12640 stree->n.tb->overridden = NULL;
12641 if (super_type)
12643 gfc_symtree* overridden;
12644 overridden = gfc_find_typebound_proc (super_type, NULL,
12645 stree->name, true, NULL);
12647 if (overridden)
12649 if (overridden->n.tb)
12650 stree->n.tb->overridden = overridden->n.tb;
12652 if (!gfc_check_typebound_override (stree, overridden))
12653 goto error;
12657 /* See if there's a name collision with a component directly in this type. */
12658 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
12659 if (!strcmp (comp->name, stree->name))
12661 gfc_error ("Procedure %qs at %L has the same name as a component of"
12662 " %qs",
12663 stree->name, &where, resolve_bindings_derived->name);
12664 goto error;
12667 /* Try to find a name collision with an inherited component. */
12668 if (super_type && gfc_find_component (super_type, stree->name, true, true))
12670 gfc_error ("Procedure %qs at %L has the same name as an inherited"
12671 " component of %qs",
12672 stree->name, &where, resolve_bindings_derived->name);
12673 goto error;
12676 stree->n.tb->error = 0;
12677 return;
12679 error:
12680 resolve_bindings_result = false;
12681 stree->n.tb->error = 1;
12685 static bool
12686 resolve_typebound_procedures (gfc_symbol* derived)
12688 int op;
12689 gfc_symbol* super_type;
12691 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
12692 return true;
12694 super_type = gfc_get_derived_super_type (derived);
12695 if (super_type)
12696 resolve_symbol (super_type);
12698 resolve_bindings_derived = derived;
12699 resolve_bindings_result = true;
12701 if (derived->f2k_derived->tb_sym_root)
12702 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
12703 &resolve_typebound_procedure);
12705 if (derived->f2k_derived->tb_uop_root)
12706 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
12707 &resolve_typebound_user_op);
12709 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
12711 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
12712 if (p && !resolve_typebound_intrinsic_op (derived,
12713 (gfc_intrinsic_op)op, p))
12714 resolve_bindings_result = false;
12717 return resolve_bindings_result;
12721 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
12722 to give all identical derived types the same backend_decl. */
12723 static void
12724 add_dt_to_dt_list (gfc_symbol *derived)
12726 gfc_dt_list *dt_list;
12728 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
12729 if (derived == dt_list->derived)
12730 return;
12732 dt_list = gfc_get_dt_list ();
12733 dt_list->next = gfc_derived_types;
12734 dt_list->derived = derived;
12735 gfc_derived_types = dt_list;
12739 /* Ensure that a derived-type is really not abstract, meaning that every
12740 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
12742 static bool
12743 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
12745 if (!st)
12746 return true;
12748 if (!ensure_not_abstract_walker (sub, st->left))
12749 return false;
12750 if (!ensure_not_abstract_walker (sub, st->right))
12751 return false;
12753 if (st->n.tb && st->n.tb->deferred)
12755 gfc_symtree* overriding;
12756 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
12757 if (!overriding)
12758 return false;
12759 gcc_assert (overriding->n.tb);
12760 if (overriding->n.tb->deferred)
12762 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
12763 " %qs is DEFERRED and not overridden",
12764 sub->name, &sub->declared_at, st->name);
12765 return false;
12769 return true;
12772 static bool
12773 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
12775 /* The algorithm used here is to recursively travel up the ancestry of sub
12776 and for each ancestor-type, check all bindings. If any of them is
12777 DEFERRED, look it up starting from sub and see if the found (overriding)
12778 binding is not DEFERRED.
12779 This is not the most efficient way to do this, but it should be ok and is
12780 clearer than something sophisticated. */
12782 gcc_assert (ancestor && !sub->attr.abstract);
12784 if (!ancestor->attr.abstract)
12785 return true;
12787 /* Walk bindings of this ancestor. */
12788 if (ancestor->f2k_derived)
12790 bool t;
12791 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
12792 if (!t)
12793 return false;
12796 /* Find next ancestor type and recurse on it. */
12797 ancestor = gfc_get_derived_super_type (ancestor);
12798 if (ancestor)
12799 return ensure_not_abstract (sub, ancestor);
12801 return true;
12805 /* This check for typebound defined assignments is done recursively
12806 since the order in which derived types are resolved is not always in
12807 order of the declarations. */
12809 static void
12810 check_defined_assignments (gfc_symbol *derived)
12812 gfc_component *c;
12814 for (c = derived->components; c; c = c->next)
12816 if (c->ts.type != BT_DERIVED
12817 || c->attr.pointer
12818 || c->attr.allocatable
12819 || c->attr.proc_pointer_comp
12820 || c->attr.class_pointer
12821 || c->attr.proc_pointer)
12822 continue;
12824 if (c->ts.u.derived->attr.defined_assign_comp
12825 || (c->ts.u.derived->f2k_derived
12826 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
12828 derived->attr.defined_assign_comp = 1;
12829 return;
12832 check_defined_assignments (c->ts.u.derived);
12833 if (c->ts.u.derived->attr.defined_assign_comp)
12835 derived->attr.defined_assign_comp = 1;
12836 return;
12842 /* Resolve the components of a derived type. This does not have to wait until
12843 resolution stage, but can be done as soon as the dt declaration has been
12844 parsed. */
12846 static bool
12847 resolve_fl_derived0 (gfc_symbol *sym)
12849 gfc_symbol* super_type;
12850 gfc_component *c;
12852 if (sym->attr.unlimited_polymorphic)
12853 return true;
12855 super_type = gfc_get_derived_super_type (sym);
12857 /* F2008, C432. */
12858 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
12860 gfc_error ("As extending type %qs at %L has a coarray component, "
12861 "parent type %qs shall also have one", sym->name,
12862 &sym->declared_at, super_type->name);
12863 return false;
12866 /* Ensure the extended type gets resolved before we do. */
12867 if (super_type && !resolve_fl_derived0 (super_type))
12868 return false;
12870 /* An ABSTRACT type must be extensible. */
12871 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
12873 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
12874 sym->name, &sym->declared_at);
12875 return false;
12878 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
12879 : sym->components;
12881 bool success = true;
12883 for ( ; c != NULL; c = c->next)
12885 if (c->attr.artificial)
12886 continue;
12888 /* F2008, C442. */
12889 if ((!sym->attr.is_class || c != sym->components)
12890 && c->attr.codimension
12891 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
12893 gfc_error ("Coarray component %qs at %L must be allocatable with "
12894 "deferred shape", c->name, &c->loc);
12895 success = false;
12896 continue;
12899 /* F2008, C443. */
12900 if (c->attr.codimension && c->ts.type == BT_DERIVED
12901 && c->ts.u.derived->ts.is_iso_c)
12903 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12904 "shall not be a coarray", c->name, &c->loc);
12905 success = false;
12906 continue;
12909 /* F2008, C444. */
12910 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
12911 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12912 || c->attr.allocatable))
12914 gfc_error ("Component %qs at %L with coarray component "
12915 "shall be a nonpointer, nonallocatable scalar",
12916 c->name, &c->loc);
12917 success = false;
12918 continue;
12921 /* F2008, C448. */
12922 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12924 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
12925 "is not an array pointer", c->name, &c->loc);
12926 success = false;
12927 continue;
12930 if (c->attr.proc_pointer && c->ts.interface)
12932 gfc_symbol *ifc = c->ts.interface;
12934 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
12936 c->tb->error = 1;
12937 success = false;
12938 continue;
12941 if (ifc->attr.if_source || ifc->attr.intrinsic)
12943 /* Resolve interface and copy attributes. */
12944 if (ifc->formal && !ifc->formal_ns)
12945 resolve_symbol (ifc);
12946 if (ifc->attr.intrinsic)
12947 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
12949 if (ifc->result)
12951 c->ts = ifc->result->ts;
12952 c->attr.allocatable = ifc->result->attr.allocatable;
12953 c->attr.pointer = ifc->result->attr.pointer;
12954 c->attr.dimension = ifc->result->attr.dimension;
12955 c->as = gfc_copy_array_spec (ifc->result->as);
12956 c->attr.class_ok = ifc->result->attr.class_ok;
12958 else
12960 c->ts = ifc->ts;
12961 c->attr.allocatable = ifc->attr.allocatable;
12962 c->attr.pointer = ifc->attr.pointer;
12963 c->attr.dimension = ifc->attr.dimension;
12964 c->as = gfc_copy_array_spec (ifc->as);
12965 c->attr.class_ok = ifc->attr.class_ok;
12967 c->ts.interface = ifc;
12968 c->attr.function = ifc->attr.function;
12969 c->attr.subroutine = ifc->attr.subroutine;
12971 c->attr.pure = ifc->attr.pure;
12972 c->attr.elemental = ifc->attr.elemental;
12973 c->attr.recursive = ifc->attr.recursive;
12974 c->attr.always_explicit = ifc->attr.always_explicit;
12975 c->attr.ext_attr |= ifc->attr.ext_attr;
12976 /* Copy char length. */
12977 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
12979 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
12980 if (cl->length && !cl->resolved
12981 && !gfc_resolve_expr (cl->length))
12983 c->tb->error = 1;
12984 success = false;
12985 continue;
12987 c->ts.u.cl = cl;
12991 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
12993 /* Since PPCs are not implicitly typed, a PPC without an explicit
12994 interface must be a subroutine. */
12995 gfc_add_subroutine (&c->attr, c->name, &c->loc);
12998 /* Procedure pointer components: Check PASS arg. */
12999 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
13000 && !sym->attr.vtype)
13002 gfc_symbol* me_arg;
13004 if (c->tb->pass_arg)
13006 gfc_formal_arglist* i;
13008 /* If an explicit passing argument name is given, walk the arg-list
13009 and look for it. */
13011 me_arg = NULL;
13012 c->tb->pass_arg_num = 1;
13013 for (i = c->ts.interface->formal; i; i = i->next)
13015 if (!strcmp (i->sym->name, c->tb->pass_arg))
13017 me_arg = i->sym;
13018 break;
13020 c->tb->pass_arg_num++;
13023 if (!me_arg)
13025 gfc_error ("Procedure pointer component %qs with PASS(%s) "
13026 "at %L has no argument %qs", c->name,
13027 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
13028 c->tb->error = 1;
13029 success = false;
13030 continue;
13033 else
13035 /* Otherwise, take the first one; there should in fact be at least
13036 one. */
13037 c->tb->pass_arg_num = 1;
13038 if (!c->ts.interface->formal)
13040 gfc_error ("Procedure pointer component %qs with PASS at %L "
13041 "must have at least one argument",
13042 c->name, &c->loc);
13043 c->tb->error = 1;
13044 success = false;
13045 continue;
13047 me_arg = c->ts.interface->formal->sym;
13050 /* Now check that the argument-type matches. */
13051 gcc_assert (me_arg);
13052 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
13053 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
13054 || (me_arg->ts.type == BT_CLASS
13055 && CLASS_DATA (me_arg)->ts.u.derived != sym))
13057 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
13058 " the derived type %qs", me_arg->name, c->name,
13059 me_arg->name, &c->loc, sym->name);
13060 c->tb->error = 1;
13061 success = false;
13062 continue;
13065 /* Check for C453. */
13066 if (me_arg->attr.dimension)
13068 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13069 "must be scalar", me_arg->name, c->name, me_arg->name,
13070 &c->loc);
13071 c->tb->error = 1;
13072 success = false;
13073 continue;
13076 if (me_arg->attr.pointer)
13078 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13079 "may not have the POINTER attribute", me_arg->name,
13080 c->name, me_arg->name, &c->loc);
13081 c->tb->error = 1;
13082 success = false;
13083 continue;
13086 if (me_arg->attr.allocatable)
13088 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13089 "may not be ALLOCATABLE", me_arg->name, c->name,
13090 me_arg->name, &c->loc);
13091 c->tb->error = 1;
13092 success = false;
13093 continue;
13096 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
13098 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
13099 " at %L", c->name, &c->loc);
13100 success = false;
13101 continue;
13106 /* Check type-spec if this is not the parent-type component. */
13107 if (((sym->attr.is_class
13108 && (!sym->components->ts.u.derived->attr.extension
13109 || c != sym->components->ts.u.derived->components))
13110 || (!sym->attr.is_class
13111 && (!sym->attr.extension || c != sym->components)))
13112 && !sym->attr.vtype
13113 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
13114 return false;
13116 /* If this type is an extension, set the accessibility of the parent
13117 component. */
13118 if (super_type
13119 && ((sym->attr.is_class
13120 && c == sym->components->ts.u.derived->components)
13121 || (!sym->attr.is_class && c == sym->components))
13122 && strcmp (super_type->name, c->name) == 0)
13123 c->attr.access = super_type->attr.access;
13125 /* If this type is an extension, see if this component has the same name
13126 as an inherited type-bound procedure. */
13127 if (super_type && !sym->attr.is_class
13128 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
13130 gfc_error ("Component %qs of %qs at %L has the same name as an"
13131 " inherited type-bound procedure",
13132 c->name, sym->name, &c->loc);
13133 return false;
13136 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
13137 && !c->ts.deferred)
13139 if (c->ts.u.cl->length == NULL
13140 || (!resolve_charlen(c->ts.u.cl))
13141 || !gfc_is_constant_expr (c->ts.u.cl->length))
13143 gfc_error ("Character length of component %qs needs to "
13144 "be a constant specification expression at %L",
13145 c->name,
13146 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
13147 return false;
13151 if (c->ts.type == BT_CHARACTER && c->ts.deferred
13152 && !c->attr.pointer && !c->attr.allocatable)
13154 gfc_error ("Character component %qs of %qs at %L with deferred "
13155 "length must be a POINTER or ALLOCATABLE",
13156 c->name, sym->name, &c->loc);
13157 return false;
13160 /* Add the hidden deferred length field. */
13161 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function
13162 && !sym->attr.is_class)
13164 char name[GFC_MAX_SYMBOL_LEN+9];
13165 gfc_component *strlen;
13166 sprintf (name, "_%s_length", c->name);
13167 strlen = gfc_find_component (sym, name, true, true);
13168 if (strlen == NULL)
13170 if (!gfc_add_component (sym, name, &strlen))
13171 return false;
13172 strlen->ts.type = BT_INTEGER;
13173 strlen->ts.kind = gfc_charlen_int_kind;
13174 strlen->attr.access = ACCESS_PRIVATE;
13175 strlen->attr.artificial = 1;
13179 if (c->ts.type == BT_DERIVED
13180 && sym->component_access != ACCESS_PRIVATE
13181 && gfc_check_symbol_access (sym)
13182 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
13183 && !c->ts.u.derived->attr.use_assoc
13184 && !gfc_check_symbol_access (c->ts.u.derived)
13185 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
13186 "PRIVATE type and cannot be a component of "
13187 "%qs, which is PUBLIC at %L", c->name,
13188 sym->name, &sym->declared_at))
13189 return false;
13191 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
13193 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
13194 "type %s", c->name, &c->loc, sym->name);
13195 return false;
13198 if (sym->attr.sequence)
13200 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
13202 gfc_error ("Component %s of SEQUENCE type declared at %L does "
13203 "not have the SEQUENCE attribute",
13204 c->ts.u.derived->name, &sym->declared_at);
13205 return false;
13209 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
13210 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
13211 else if (c->ts.type == BT_CLASS && c->attr.class_ok
13212 && CLASS_DATA (c)->ts.u.derived->attr.generic)
13213 CLASS_DATA (c)->ts.u.derived
13214 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
13216 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
13217 && c->attr.pointer && c->ts.u.derived->components == NULL
13218 && !c->ts.u.derived->attr.zero_comp)
13220 gfc_error ("The pointer component %qs of %qs at %L is a type "
13221 "that has not been declared", c->name, sym->name,
13222 &c->loc);
13223 return false;
13226 if (c->ts.type == BT_CLASS && c->attr.class_ok
13227 && CLASS_DATA (c)->attr.class_pointer
13228 && CLASS_DATA (c)->ts.u.derived->components == NULL
13229 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
13230 && !UNLIMITED_POLY (c))
13232 gfc_error ("The pointer component %qs of %qs at %L is a type "
13233 "that has not been declared", c->name, sym->name,
13234 &c->loc);
13235 return false;
13238 /* C437. */
13239 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
13240 && (!c->attr.class_ok
13241 || !(CLASS_DATA (c)->attr.class_pointer
13242 || CLASS_DATA (c)->attr.allocatable)))
13244 gfc_error ("Component %qs with CLASS at %L must be allocatable "
13245 "or pointer", c->name, &c->loc);
13246 /* Prevent a recurrence of the error. */
13247 c->ts.type = BT_UNKNOWN;
13248 return false;
13251 /* Ensure that all the derived type components are put on the
13252 derived type list; even in formal namespaces, where derived type
13253 pointer components might not have been declared. */
13254 if (c->ts.type == BT_DERIVED
13255 && c->ts.u.derived
13256 && c->ts.u.derived->components
13257 && c->attr.pointer
13258 && sym != c->ts.u.derived)
13259 add_dt_to_dt_list (c->ts.u.derived);
13261 if (!gfc_resolve_array_spec (c->as,
13262 !(c->attr.pointer || c->attr.proc_pointer
13263 || c->attr.allocatable)))
13264 return false;
13266 if (c->initializer && !sym->attr.vtype
13267 && !gfc_check_assign_symbol (sym, c, c->initializer))
13268 return false;
13271 if (!success)
13272 return false;
13274 check_defined_assignments (sym);
13276 if (!sym->attr.defined_assign_comp && super_type)
13277 sym->attr.defined_assign_comp
13278 = super_type->attr.defined_assign_comp;
13280 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
13281 all DEFERRED bindings are overridden. */
13282 if (super_type && super_type->attr.abstract && !sym->attr.abstract
13283 && !sym->attr.is_class
13284 && !ensure_not_abstract (sym, super_type))
13285 return false;
13287 /* Add derived type to the derived type list. */
13288 add_dt_to_dt_list (sym);
13290 return true;
13294 /* The following procedure does the full resolution of a derived type,
13295 including resolution of all type-bound procedures (if present). In contrast
13296 to 'resolve_fl_derived0' this can only be done after the module has been
13297 parsed completely. */
13299 static bool
13300 resolve_fl_derived (gfc_symbol *sym)
13302 gfc_symbol *gen_dt = NULL;
13304 if (sym->attr.unlimited_polymorphic)
13305 return true;
13307 if (!sym->attr.is_class)
13308 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
13309 if (gen_dt && gen_dt->generic && gen_dt->generic->next
13310 && (!gen_dt->generic->sym->attr.use_assoc
13311 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
13312 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
13313 "%qs at %L being the same name as derived "
13314 "type at %L", sym->name,
13315 gen_dt->generic->sym == sym
13316 ? gen_dt->generic->next->sym->name
13317 : gen_dt->generic->sym->name,
13318 gen_dt->generic->sym == sym
13319 ? &gen_dt->generic->next->sym->declared_at
13320 : &gen_dt->generic->sym->declared_at,
13321 &sym->declared_at))
13322 return false;
13324 /* Resolve the finalizer procedures. */
13325 if (!gfc_resolve_finalizers (sym, NULL))
13326 return false;
13328 if (sym->attr.is_class && sym->ts.u.derived == NULL)
13330 /* Fix up incomplete CLASS symbols. */
13331 gfc_component *data = gfc_find_component (sym, "_data", true, true);
13332 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
13334 /* Nothing more to do for unlimited polymorphic entities. */
13335 if (data->ts.u.derived->attr.unlimited_polymorphic)
13336 return true;
13337 else if (vptr->ts.u.derived == NULL)
13339 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
13340 gcc_assert (vtab);
13341 vptr->ts.u.derived = vtab->ts.u.derived;
13345 if (!resolve_fl_derived0 (sym))
13346 return false;
13348 /* Resolve the type-bound procedures. */
13349 if (!resolve_typebound_procedures (sym))
13350 return false;
13352 return true;
13356 static bool
13357 resolve_fl_namelist (gfc_symbol *sym)
13359 gfc_namelist *nl;
13360 gfc_symbol *nlsym;
13362 for (nl = sym->namelist; nl; nl = nl->next)
13364 /* Check again, the check in match only works if NAMELIST comes
13365 after the decl. */
13366 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
13368 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
13369 "allowed", nl->sym->name, sym->name, &sym->declared_at);
13370 return false;
13373 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
13374 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13375 "with assumed shape in namelist %qs at %L",
13376 nl->sym->name, sym->name, &sym->declared_at))
13377 return false;
13379 if (is_non_constant_shape_array (nl->sym)
13380 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13381 "with nonconstant shape in namelist %qs at %L",
13382 nl->sym->name, sym->name, &sym->declared_at))
13383 return false;
13385 if (nl->sym->ts.type == BT_CHARACTER
13386 && (nl->sym->ts.u.cl->length == NULL
13387 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
13388 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
13389 "nonconstant character length in "
13390 "namelist %qs at %L", nl->sym->name,
13391 sym->name, &sym->declared_at))
13392 return false;
13394 /* FIXME: Once UDDTIO is implemented, the following can be
13395 removed. */
13396 if (nl->sym->ts.type == BT_CLASS)
13398 gfc_error ("NAMELIST object %qs in namelist %qs at %L is "
13399 "polymorphic and requires a defined input/output "
13400 "procedure", nl->sym->name, sym->name, &sym->declared_at);
13401 return false;
13404 if (nl->sym->ts.type == BT_DERIVED
13405 && (nl->sym->ts.u.derived->attr.alloc_comp
13406 || nl->sym->ts.u.derived->attr.pointer_comp))
13408 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
13409 "namelist %qs at %L with ALLOCATABLE "
13410 "or POINTER components", nl->sym->name,
13411 sym->name, &sym->declared_at))
13412 return false;
13414 /* FIXME: Once UDDTIO is implemented, the following can be
13415 removed. */
13416 gfc_error ("NAMELIST object %qs in namelist %qs at %L has "
13417 "ALLOCATABLE or POINTER components and thus requires "
13418 "a defined input/output procedure", nl->sym->name,
13419 sym->name, &sym->declared_at);
13420 return false;
13424 /* Reject PRIVATE objects in a PUBLIC namelist. */
13425 if (gfc_check_symbol_access (sym))
13427 for (nl = sym->namelist; nl; nl = nl->next)
13429 if (!nl->sym->attr.use_assoc
13430 && !is_sym_host_assoc (nl->sym, sym->ns)
13431 && !gfc_check_symbol_access (nl->sym))
13433 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
13434 "cannot be member of PUBLIC namelist %qs at %L",
13435 nl->sym->name, sym->name, &sym->declared_at);
13436 return false;
13439 /* Types with private components that came here by USE-association. */
13440 if (nl->sym->ts.type == BT_DERIVED
13441 && derived_inaccessible (nl->sym->ts.u.derived))
13443 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
13444 "components and cannot be member of namelist %qs at %L",
13445 nl->sym->name, sym->name, &sym->declared_at);
13446 return false;
13449 /* Types with private components that are defined in the same module. */
13450 if (nl->sym->ts.type == BT_DERIVED
13451 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
13452 && nl->sym->ts.u.derived->attr.private_comp)
13454 gfc_error ("NAMELIST object %qs has PRIVATE components and "
13455 "cannot be a member of PUBLIC namelist %qs at %L",
13456 nl->sym->name, sym->name, &sym->declared_at);
13457 return false;
13463 /* 14.1.2 A module or internal procedure represent local entities
13464 of the same type as a namelist member and so are not allowed. */
13465 for (nl = sym->namelist; nl; nl = nl->next)
13467 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
13468 continue;
13470 if (nl->sym->attr.function && nl->sym == nl->sym->result)
13471 if ((nl->sym == sym->ns->proc_name)
13473 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
13474 continue;
13476 nlsym = NULL;
13477 if (nl->sym->name)
13478 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
13479 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
13481 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
13482 "attribute in %qs at %L", nlsym->name,
13483 &sym->declared_at);
13484 return false;
13488 return true;
13492 static bool
13493 resolve_fl_parameter (gfc_symbol *sym)
13495 /* A parameter array's shape needs to be constant. */
13496 if (sym->as != NULL
13497 && (sym->as->type == AS_DEFERRED
13498 || is_non_constant_shape_array (sym)))
13500 gfc_error ("Parameter array %qs at %L cannot be automatic "
13501 "or of deferred shape", sym->name, &sym->declared_at);
13502 return false;
13505 /* Make sure a parameter that has been implicitly typed still
13506 matches the implicit type, since PARAMETER statements can precede
13507 IMPLICIT statements. */
13508 if (sym->attr.implicit_type
13509 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
13510 sym->ns)))
13512 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
13513 "later IMPLICIT type", sym->name, &sym->declared_at);
13514 return false;
13517 /* Make sure the types of derived parameters are consistent. This
13518 type checking is deferred until resolution because the type may
13519 refer to a derived type from the host. */
13520 if (sym->ts.type == BT_DERIVED
13521 && !gfc_compare_types (&sym->ts, &sym->value->ts))
13523 gfc_error ("Incompatible derived type in PARAMETER at %L",
13524 &sym->value->where);
13525 return false;
13527 return true;
13531 /* Do anything necessary to resolve a symbol. Right now, we just
13532 assume that an otherwise unknown symbol is a variable. This sort
13533 of thing commonly happens for symbols in module. */
13535 static void
13536 resolve_symbol (gfc_symbol *sym)
13538 int check_constant, mp_flag;
13539 gfc_symtree *symtree;
13540 gfc_symtree *this_symtree;
13541 gfc_namespace *ns;
13542 gfc_component *c;
13543 symbol_attribute class_attr;
13544 gfc_array_spec *as;
13545 bool saved_specification_expr;
13547 if (sym->resolved)
13548 return;
13549 sym->resolved = 1;
13551 if (sym->attr.artificial)
13552 return;
13554 if (sym->attr.unlimited_polymorphic)
13555 return;
13557 if (sym->attr.flavor == FL_UNKNOWN
13558 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
13559 && !sym->attr.generic && !sym->attr.external
13560 && sym->attr.if_source == IFSRC_UNKNOWN
13561 && sym->ts.type == BT_UNKNOWN))
13564 /* If we find that a flavorless symbol is an interface in one of the
13565 parent namespaces, find its symtree in this namespace, free the
13566 symbol and set the symtree to point to the interface symbol. */
13567 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
13569 symtree = gfc_find_symtree (ns->sym_root, sym->name);
13570 if (symtree && (symtree->n.sym->generic ||
13571 (symtree->n.sym->attr.flavor == FL_PROCEDURE
13572 && sym->ns->construct_entities)))
13574 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
13575 sym->name);
13576 if (this_symtree->n.sym == sym)
13578 symtree->n.sym->refs++;
13579 gfc_release_symbol (sym);
13580 this_symtree->n.sym = symtree->n.sym;
13581 return;
13586 /* Otherwise give it a flavor according to such attributes as
13587 it has. */
13588 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
13589 && sym->attr.intrinsic == 0)
13590 sym->attr.flavor = FL_VARIABLE;
13591 else if (sym->attr.flavor == FL_UNKNOWN)
13593 sym->attr.flavor = FL_PROCEDURE;
13594 if (sym->attr.dimension)
13595 sym->attr.function = 1;
13599 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
13600 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
13602 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
13603 && !resolve_procedure_interface (sym))
13604 return;
13606 if (sym->attr.is_protected && !sym->attr.proc_pointer
13607 && (sym->attr.procedure || sym->attr.external))
13609 if (sym->attr.external)
13610 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
13611 "at %L", &sym->declared_at);
13612 else
13613 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
13614 "at %L", &sym->declared_at);
13616 return;
13619 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
13620 return;
13622 /* Symbols that are module procedures with results (functions) have
13623 the types and array specification copied for type checking in
13624 procedures that call them, as well as for saving to a module
13625 file. These symbols can't stand the scrutiny that their results
13626 can. */
13627 mp_flag = (sym->result != NULL && sym->result != sym);
13629 /* Make sure that the intrinsic is consistent with its internal
13630 representation. This needs to be done before assigning a default
13631 type to avoid spurious warnings. */
13632 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
13633 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
13634 return;
13636 /* Resolve associate names. */
13637 if (sym->assoc)
13638 resolve_assoc_var (sym, true);
13640 /* Assign default type to symbols that need one and don't have one. */
13641 if (sym->ts.type == BT_UNKNOWN)
13643 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
13645 gfc_set_default_type (sym, 1, NULL);
13648 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
13649 && !sym->attr.function && !sym->attr.subroutine
13650 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
13651 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
13653 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
13655 /* The specific case of an external procedure should emit an error
13656 in the case that there is no implicit type. */
13657 if (!mp_flag)
13658 gfc_set_default_type (sym, sym->attr.external, NULL);
13659 else
13661 /* Result may be in another namespace. */
13662 resolve_symbol (sym->result);
13664 if (!sym->result->attr.proc_pointer)
13666 sym->ts = sym->result->ts;
13667 sym->as = gfc_copy_array_spec (sym->result->as);
13668 sym->attr.dimension = sym->result->attr.dimension;
13669 sym->attr.pointer = sym->result->attr.pointer;
13670 sym->attr.allocatable = sym->result->attr.allocatable;
13671 sym->attr.contiguous = sym->result->attr.contiguous;
13676 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
13678 bool saved_specification_expr = specification_expr;
13679 specification_expr = true;
13680 gfc_resolve_array_spec (sym->result->as, false);
13681 specification_expr = saved_specification_expr;
13684 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
13686 as = CLASS_DATA (sym)->as;
13687 class_attr = CLASS_DATA (sym)->attr;
13688 class_attr.pointer = class_attr.class_pointer;
13690 else
13692 class_attr = sym->attr;
13693 as = sym->as;
13696 /* F2008, C530. */
13697 if (sym->attr.contiguous
13698 && (!class_attr.dimension
13699 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
13700 && !class_attr.pointer)))
13702 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
13703 "array pointer or an assumed-shape or assumed-rank array",
13704 sym->name, &sym->declared_at);
13705 return;
13708 /* Assumed size arrays and assumed shape arrays must be dummy
13709 arguments. Array-spec's of implied-shape should have been resolved to
13710 AS_EXPLICIT already. */
13712 if (as)
13714 gcc_assert (as->type != AS_IMPLIED_SHAPE);
13715 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
13716 || as->type == AS_ASSUMED_SHAPE)
13717 && !sym->attr.dummy && !sym->attr.select_type_temporary)
13719 if (as->type == AS_ASSUMED_SIZE)
13720 gfc_error ("Assumed size array at %L must be a dummy argument",
13721 &sym->declared_at);
13722 else
13723 gfc_error ("Assumed shape array at %L must be a dummy argument",
13724 &sym->declared_at);
13725 return;
13727 /* TS 29113, C535a. */
13728 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
13729 && !sym->attr.select_type_temporary)
13731 gfc_error ("Assumed-rank array at %L must be a dummy argument",
13732 &sym->declared_at);
13733 return;
13735 if (as->type == AS_ASSUMED_RANK
13736 && (sym->attr.codimension || sym->attr.value))
13738 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
13739 "CODIMENSION attribute", &sym->declared_at);
13740 return;
13744 /* Make sure symbols with known intent or optional are really dummy
13745 variable. Because of ENTRY statement, this has to be deferred
13746 until resolution time. */
13748 if (!sym->attr.dummy
13749 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
13751 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
13752 return;
13755 if (sym->attr.value && !sym->attr.dummy)
13757 gfc_error ("%qs at %L cannot have the VALUE attribute because "
13758 "it is not a dummy argument", sym->name, &sym->declared_at);
13759 return;
13762 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
13764 gfc_charlen *cl = sym->ts.u.cl;
13765 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13767 gfc_error ("Character dummy variable %qs at %L with VALUE "
13768 "attribute must have constant length",
13769 sym->name, &sym->declared_at);
13770 return;
13773 if (sym->ts.is_c_interop
13774 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
13776 gfc_error ("C interoperable character dummy variable %qs at %L "
13777 "with VALUE attribute must have length one",
13778 sym->name, &sym->declared_at);
13779 return;
13783 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13784 && sym->ts.u.derived->attr.generic)
13786 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
13787 if (!sym->ts.u.derived)
13789 gfc_error ("The derived type %qs at %L is of type %qs, "
13790 "which has not been defined", sym->name,
13791 &sym->declared_at, sym->ts.u.derived->name);
13792 sym->ts.type = BT_UNKNOWN;
13793 return;
13797 /* Use the same constraints as TYPE(*), except for the type check
13798 and that only scalars and assumed-size arrays are permitted. */
13799 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
13801 if (!sym->attr.dummy)
13803 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13804 "a dummy argument", sym->name, &sym->declared_at);
13805 return;
13808 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
13809 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
13810 && sym->ts.type != BT_COMPLEX)
13812 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13813 "of type TYPE(*) or of an numeric intrinsic type",
13814 sym->name, &sym->declared_at);
13815 return;
13818 if (sym->attr.allocatable || sym->attr.codimension
13819 || sym->attr.pointer || sym->attr.value)
13821 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13822 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
13823 "attribute", sym->name, &sym->declared_at);
13824 return;
13827 if (sym->attr.intent == INTENT_OUT)
13829 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13830 "have the INTENT(OUT) attribute",
13831 sym->name, &sym->declared_at);
13832 return;
13834 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
13836 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
13837 "either be a scalar or an assumed-size array",
13838 sym->name, &sym->declared_at);
13839 return;
13842 /* Set the type to TYPE(*) and add a dimension(*) to ensure
13843 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
13844 packing. */
13845 sym->ts.type = BT_ASSUMED;
13846 sym->as = gfc_get_array_spec ();
13847 sym->as->type = AS_ASSUMED_SIZE;
13848 sym->as->rank = 1;
13849 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
13851 else if (sym->ts.type == BT_ASSUMED)
13853 /* TS 29113, C407a. */
13854 if (!sym->attr.dummy)
13856 gfc_error ("Assumed type of variable %s at %L is only permitted "
13857 "for dummy variables", sym->name, &sym->declared_at);
13858 return;
13860 if (sym->attr.allocatable || sym->attr.codimension
13861 || sym->attr.pointer || sym->attr.value)
13863 gfc_error ("Assumed-type variable %s at %L may not have the "
13864 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
13865 sym->name, &sym->declared_at);
13866 return;
13868 if (sym->attr.intent == INTENT_OUT)
13870 gfc_error ("Assumed-type variable %s at %L may not have the "
13871 "INTENT(OUT) attribute",
13872 sym->name, &sym->declared_at);
13873 return;
13875 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
13877 gfc_error ("Assumed-type variable %s at %L shall not be an "
13878 "explicit-shape array", sym->name, &sym->declared_at);
13879 return;
13883 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
13884 do this for something that was implicitly typed because that is handled
13885 in gfc_set_default_type. Handle dummy arguments and procedure
13886 definitions separately. Also, anything that is use associated is not
13887 handled here but instead is handled in the module it is declared in.
13888 Finally, derived type definitions are allowed to be BIND(C) since that
13889 only implies that they're interoperable, and they are checked fully for
13890 interoperability when a variable is declared of that type. */
13891 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
13892 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
13893 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
13895 bool t = true;
13897 /* First, make sure the variable is declared at the
13898 module-level scope (J3/04-007, Section 15.3). */
13899 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
13900 sym->attr.in_common == 0)
13902 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
13903 "is neither a COMMON block nor declared at the "
13904 "module level scope", sym->name, &(sym->declared_at));
13905 t = false;
13907 else if (sym->common_head != NULL)
13909 t = verify_com_block_vars_c_interop (sym->common_head);
13911 else
13913 /* If type() declaration, we need to verify that the components
13914 of the given type are all C interoperable, etc. */
13915 if (sym->ts.type == BT_DERIVED &&
13916 sym->ts.u.derived->attr.is_c_interop != 1)
13918 /* Make sure the user marked the derived type as BIND(C). If
13919 not, call the verify routine. This could print an error
13920 for the derived type more than once if multiple variables
13921 of that type are declared. */
13922 if (sym->ts.u.derived->attr.is_bind_c != 1)
13923 verify_bind_c_derived_type (sym->ts.u.derived);
13924 t = false;
13927 /* Verify the variable itself as C interoperable if it
13928 is BIND(C). It is not possible for this to succeed if
13929 the verify_bind_c_derived_type failed, so don't have to handle
13930 any error returned by verify_bind_c_derived_type. */
13931 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13932 sym->common_block);
13935 if (!t)
13937 /* clear the is_bind_c flag to prevent reporting errors more than
13938 once if something failed. */
13939 sym->attr.is_bind_c = 0;
13940 return;
13944 /* If a derived type symbol has reached this point, without its
13945 type being declared, we have an error. Notice that most
13946 conditions that produce undefined derived types have already
13947 been dealt with. However, the likes of:
13948 implicit type(t) (t) ..... call foo (t) will get us here if
13949 the type is not declared in the scope of the implicit
13950 statement. Change the type to BT_UNKNOWN, both because it is so
13951 and to prevent an ICE. */
13952 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13953 && sym->ts.u.derived->components == NULL
13954 && !sym->ts.u.derived->attr.zero_comp)
13956 gfc_error ("The derived type %qs at %L is of type %qs, "
13957 "which has not been defined", sym->name,
13958 &sym->declared_at, sym->ts.u.derived->name);
13959 sym->ts.type = BT_UNKNOWN;
13960 return;
13963 /* Make sure that the derived type has been resolved and that the
13964 derived type is visible in the symbol's namespace, if it is a
13965 module function and is not PRIVATE. */
13966 if (sym->ts.type == BT_DERIVED
13967 && sym->ts.u.derived->attr.use_assoc
13968 && sym->ns->proc_name
13969 && sym->ns->proc_name->attr.flavor == FL_MODULE
13970 && !resolve_fl_derived (sym->ts.u.derived))
13971 return;
13973 /* Unless the derived-type declaration is use associated, Fortran 95
13974 does not allow public entries of private derived types.
13975 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
13976 161 in 95-006r3. */
13977 if (sym->ts.type == BT_DERIVED
13978 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
13979 && !sym->ts.u.derived->attr.use_assoc
13980 && gfc_check_symbol_access (sym)
13981 && !gfc_check_symbol_access (sym->ts.u.derived)
13982 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
13983 "derived type %qs",
13984 (sym->attr.flavor == FL_PARAMETER)
13985 ? "parameter" : "variable",
13986 sym->name, &sym->declared_at,
13987 sym->ts.u.derived->name))
13988 return;
13990 /* F2008, C1302. */
13991 if (sym->ts.type == BT_DERIVED
13992 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
13993 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
13994 || sym->ts.u.derived->attr.lock_comp)
13995 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
13997 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
13998 "type LOCK_TYPE must be a coarray", sym->name,
13999 &sym->declared_at);
14000 return;
14003 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
14004 default initialization is defined (5.1.2.4.4). */
14005 if (sym->ts.type == BT_DERIVED
14006 && sym->attr.dummy
14007 && sym->attr.intent == INTENT_OUT
14008 && sym->as
14009 && sym->as->type == AS_ASSUMED_SIZE)
14011 for (c = sym->ts.u.derived->components; c; c = c->next)
14013 if (c->initializer)
14015 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
14016 "ASSUMED SIZE and so cannot have a default initializer",
14017 sym->name, &sym->declared_at);
14018 return;
14023 /* F2008, C542. */
14024 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
14025 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
14027 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
14028 "INTENT(OUT)", sym->name, &sym->declared_at);
14029 return;
14032 /* F2008, C525. */
14033 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14034 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14035 && CLASS_DATA (sym)->attr.coarray_comp))
14036 || class_attr.codimension)
14037 && (sym->attr.result || sym->result == sym))
14039 gfc_error ("Function result %qs at %L shall not be a coarray or have "
14040 "a coarray component", sym->name, &sym->declared_at);
14041 return;
14044 /* F2008, C524. */
14045 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
14046 && sym->ts.u.derived->ts.is_iso_c)
14048 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14049 "shall not be a coarray", sym->name, &sym->declared_at);
14050 return;
14053 /* F2008, C525. */
14054 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14055 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14056 && CLASS_DATA (sym)->attr.coarray_comp))
14057 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
14058 || class_attr.allocatable))
14060 gfc_error ("Variable %qs at %L with coarray component shall be a "
14061 "nonpointer, nonallocatable scalar, which is not a coarray",
14062 sym->name, &sym->declared_at);
14063 return;
14066 /* F2008, C526. The function-result case was handled above. */
14067 if (class_attr.codimension
14068 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
14069 || sym->attr.select_type_temporary
14070 || sym->ns->save_all
14071 || sym->ns->proc_name->attr.flavor == FL_MODULE
14072 || sym->ns->proc_name->attr.is_main_program
14073 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
14075 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
14076 "nor a dummy argument", sym->name, &sym->declared_at);
14077 return;
14079 /* F2008, C528. */
14080 else if (class_attr.codimension && !sym->attr.select_type_temporary
14081 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
14083 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
14084 "deferred shape", sym->name, &sym->declared_at);
14085 return;
14087 else if (class_attr.codimension && class_attr.allocatable && as
14088 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
14090 gfc_error ("Allocatable coarray variable %qs at %L must have "
14091 "deferred shape", sym->name, &sym->declared_at);
14092 return;
14095 /* F2008, C541. */
14096 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14097 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14098 && CLASS_DATA (sym)->attr.coarray_comp))
14099 || (class_attr.codimension && class_attr.allocatable))
14100 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
14102 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
14103 "allocatable coarray or have coarray components",
14104 sym->name, &sym->declared_at);
14105 return;
14108 if (class_attr.codimension && sym->attr.dummy
14109 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
14111 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
14112 "procedure %qs", sym->name, &sym->declared_at,
14113 sym->ns->proc_name->name);
14114 return;
14117 if (sym->ts.type == BT_LOGICAL
14118 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
14119 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
14120 && sym->ns->proc_name->attr.is_bind_c)))
14122 int i;
14123 for (i = 0; gfc_logical_kinds[i].kind; i++)
14124 if (gfc_logical_kinds[i].kind == sym->ts.kind)
14125 break;
14126 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
14127 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
14128 "%L with non-C_Bool kind in BIND(C) procedure "
14129 "%qs", sym->name, &sym->declared_at,
14130 sym->ns->proc_name->name))
14131 return;
14132 else if (!gfc_logical_kinds[i].c_bool
14133 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
14134 "%qs at %L with non-C_Bool kind in "
14135 "BIND(C) procedure %qs", sym->name,
14136 &sym->declared_at,
14137 sym->attr.function ? sym->name
14138 : sym->ns->proc_name->name))
14139 return;
14142 switch (sym->attr.flavor)
14144 case FL_VARIABLE:
14145 if (!resolve_fl_variable (sym, mp_flag))
14146 return;
14147 break;
14149 case FL_PROCEDURE:
14150 if (!resolve_fl_procedure (sym, mp_flag))
14151 return;
14152 break;
14154 case FL_NAMELIST:
14155 if (!resolve_fl_namelist (sym))
14156 return;
14157 break;
14159 case FL_PARAMETER:
14160 if (!resolve_fl_parameter (sym))
14161 return;
14162 break;
14164 default:
14165 break;
14168 /* Resolve array specifier. Check as well some constraints
14169 on COMMON blocks. */
14171 check_constant = sym->attr.in_common && !sym->attr.pointer;
14173 /* Set the formal_arg_flag so that check_conflict will not throw
14174 an error for host associated variables in the specification
14175 expression for an array_valued function. */
14176 if (sym->attr.function && sym->as)
14177 formal_arg_flag = 1;
14179 saved_specification_expr = specification_expr;
14180 specification_expr = true;
14181 gfc_resolve_array_spec (sym->as, check_constant);
14182 specification_expr = saved_specification_expr;
14184 formal_arg_flag = 0;
14186 /* Resolve formal namespaces. */
14187 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
14188 && !sym->attr.contained && !sym->attr.intrinsic)
14189 gfc_resolve (sym->formal_ns);
14191 /* Make sure the formal namespace is present. */
14192 if (sym->formal && !sym->formal_ns)
14194 gfc_formal_arglist *formal = sym->formal;
14195 while (formal && !formal->sym)
14196 formal = formal->next;
14198 if (formal)
14200 sym->formal_ns = formal->sym->ns;
14201 if (sym->ns != formal->sym->ns)
14202 sym->formal_ns->refs++;
14206 /* Check threadprivate restrictions. */
14207 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
14208 && (!sym->attr.in_common
14209 && sym->module == NULL
14210 && (sym->ns->proc_name == NULL
14211 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
14212 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
14214 /* Check omp declare target restrictions. */
14215 if (sym->attr.omp_declare_target
14216 && sym->attr.flavor == FL_VARIABLE
14217 && !sym->attr.save
14218 && !sym->ns->save_all
14219 && (!sym->attr.in_common
14220 && sym->module == NULL
14221 && (sym->ns->proc_name == NULL
14222 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
14223 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
14224 sym->name, &sym->declared_at);
14226 /* If we have come this far we can apply default-initializers, as
14227 described in 14.7.5, to those variables that have not already
14228 been assigned one. */
14229 if (sym->ts.type == BT_DERIVED
14230 && !sym->value
14231 && !sym->attr.allocatable
14232 && !sym->attr.alloc_comp)
14234 symbol_attribute *a = &sym->attr;
14236 if ((!a->save && !a->dummy && !a->pointer
14237 && !a->in_common && !a->use_assoc
14238 && !a->result && !a->function)
14239 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
14240 apply_default_init (sym);
14241 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
14242 && (sym->ts.u.derived->attr.alloc_comp
14243 || sym->ts.u.derived->attr.pointer_comp))
14244 /* Mark the result symbol to be referenced, when it has allocatable
14245 components. */
14246 sym->result->attr.referenced = 1;
14249 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
14250 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
14251 && !CLASS_DATA (sym)->attr.class_pointer
14252 && !CLASS_DATA (sym)->attr.allocatable)
14253 apply_default_init (sym);
14255 /* If this symbol has a type-spec, check it. */
14256 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
14257 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
14258 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
14259 return;
14263 /************* Resolve DATA statements *************/
14265 static struct
14267 gfc_data_value *vnode;
14268 mpz_t left;
14270 values;
14273 /* Advance the values structure to point to the next value in the data list. */
14275 static bool
14276 next_data_value (void)
14278 while (mpz_cmp_ui (values.left, 0) == 0)
14281 if (values.vnode->next == NULL)
14282 return false;
14284 values.vnode = values.vnode->next;
14285 mpz_set (values.left, values.vnode->repeat);
14288 return true;
14292 static bool
14293 check_data_variable (gfc_data_variable *var, locus *where)
14295 gfc_expr *e;
14296 mpz_t size;
14297 mpz_t offset;
14298 bool t;
14299 ar_type mark = AR_UNKNOWN;
14300 int i;
14301 mpz_t section_index[GFC_MAX_DIMENSIONS];
14302 gfc_ref *ref;
14303 gfc_array_ref *ar;
14304 gfc_symbol *sym;
14305 int has_pointer;
14307 if (!gfc_resolve_expr (var->expr))
14308 return false;
14310 ar = NULL;
14311 mpz_init_set_si (offset, 0);
14312 e = var->expr;
14314 if (e->expr_type != EXPR_VARIABLE)
14315 gfc_internal_error ("check_data_variable(): Bad expression");
14317 sym = e->symtree->n.sym;
14319 if (sym->ns->is_block_data && !sym->attr.in_common)
14321 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
14322 sym->name, &sym->declared_at);
14325 if (e->ref == NULL && sym->as)
14327 gfc_error ("DATA array %qs at %L must be specified in a previous"
14328 " declaration", sym->name, where);
14329 return false;
14332 has_pointer = sym->attr.pointer;
14334 if (gfc_is_coindexed (e))
14336 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
14337 where);
14338 return false;
14341 for (ref = e->ref; ref; ref = ref->next)
14343 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
14344 has_pointer = 1;
14346 if (has_pointer
14347 && ref->type == REF_ARRAY
14348 && ref->u.ar.type != AR_FULL)
14350 gfc_error ("DATA element %qs at %L is a pointer and so must "
14351 "be a full array", sym->name, where);
14352 return false;
14356 if (e->rank == 0 || has_pointer)
14358 mpz_init_set_ui (size, 1);
14359 ref = NULL;
14361 else
14363 ref = e->ref;
14365 /* Find the array section reference. */
14366 for (ref = e->ref; ref; ref = ref->next)
14368 if (ref->type != REF_ARRAY)
14369 continue;
14370 if (ref->u.ar.type == AR_ELEMENT)
14371 continue;
14372 break;
14374 gcc_assert (ref);
14376 /* Set marks according to the reference pattern. */
14377 switch (ref->u.ar.type)
14379 case AR_FULL:
14380 mark = AR_FULL;
14381 break;
14383 case AR_SECTION:
14384 ar = &ref->u.ar;
14385 /* Get the start position of array section. */
14386 gfc_get_section_index (ar, section_index, &offset);
14387 mark = AR_SECTION;
14388 break;
14390 default:
14391 gcc_unreachable ();
14394 if (!gfc_array_size (e, &size))
14396 gfc_error ("Nonconstant array section at %L in DATA statement",
14397 &e->where);
14398 mpz_clear (offset);
14399 return false;
14403 t = true;
14405 while (mpz_cmp_ui (size, 0) > 0)
14407 if (!next_data_value ())
14409 gfc_error ("DATA statement at %L has more variables than values",
14410 where);
14411 t = false;
14412 break;
14415 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
14416 if (!t)
14417 break;
14419 /* If we have more than one element left in the repeat count,
14420 and we have more than one element left in the target variable,
14421 then create a range assignment. */
14422 /* FIXME: Only done for full arrays for now, since array sections
14423 seem tricky. */
14424 if (mark == AR_FULL && ref && ref->next == NULL
14425 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
14427 mpz_t range;
14429 if (mpz_cmp (size, values.left) >= 0)
14431 mpz_init_set (range, values.left);
14432 mpz_sub (size, size, values.left);
14433 mpz_set_ui (values.left, 0);
14435 else
14437 mpz_init_set (range, size);
14438 mpz_sub (values.left, values.left, size);
14439 mpz_set_ui (size, 0);
14442 t = gfc_assign_data_value (var->expr, values.vnode->expr,
14443 offset, &range);
14445 mpz_add (offset, offset, range);
14446 mpz_clear (range);
14448 if (!t)
14449 break;
14452 /* Assign initial value to symbol. */
14453 else
14455 mpz_sub_ui (values.left, values.left, 1);
14456 mpz_sub_ui (size, size, 1);
14458 t = gfc_assign_data_value (var->expr, values.vnode->expr,
14459 offset, NULL);
14460 if (!t)
14461 break;
14463 if (mark == AR_FULL)
14464 mpz_add_ui (offset, offset, 1);
14466 /* Modify the array section indexes and recalculate the offset
14467 for next element. */
14468 else if (mark == AR_SECTION)
14469 gfc_advance_section (section_index, ar, &offset);
14473 if (mark == AR_SECTION)
14475 for (i = 0; i < ar->dimen; i++)
14476 mpz_clear (section_index[i]);
14479 mpz_clear (size);
14480 mpz_clear (offset);
14482 return t;
14486 static bool traverse_data_var (gfc_data_variable *, locus *);
14488 /* Iterate over a list of elements in a DATA statement. */
14490 static bool
14491 traverse_data_list (gfc_data_variable *var, locus *where)
14493 mpz_t trip;
14494 iterator_stack frame;
14495 gfc_expr *e, *start, *end, *step;
14496 bool retval = true;
14498 mpz_init (frame.value);
14499 mpz_init (trip);
14501 start = gfc_copy_expr (var->iter.start);
14502 end = gfc_copy_expr (var->iter.end);
14503 step = gfc_copy_expr (var->iter.step);
14505 if (!gfc_simplify_expr (start, 1)
14506 || start->expr_type != EXPR_CONSTANT)
14508 gfc_error ("start of implied-do loop at %L could not be "
14509 "simplified to a constant value", &start->where);
14510 retval = false;
14511 goto cleanup;
14513 if (!gfc_simplify_expr (end, 1)
14514 || end->expr_type != EXPR_CONSTANT)
14516 gfc_error ("end of implied-do loop at %L could not be "
14517 "simplified to a constant value", &start->where);
14518 retval = false;
14519 goto cleanup;
14521 if (!gfc_simplify_expr (step, 1)
14522 || step->expr_type != EXPR_CONSTANT)
14524 gfc_error ("step of implied-do loop at %L could not be "
14525 "simplified to a constant value", &start->where);
14526 retval = false;
14527 goto cleanup;
14530 mpz_set (trip, end->value.integer);
14531 mpz_sub (trip, trip, start->value.integer);
14532 mpz_add (trip, trip, step->value.integer);
14534 mpz_div (trip, trip, step->value.integer);
14536 mpz_set (frame.value, start->value.integer);
14538 frame.prev = iter_stack;
14539 frame.variable = var->iter.var->symtree;
14540 iter_stack = &frame;
14542 while (mpz_cmp_ui (trip, 0) > 0)
14544 if (!traverse_data_var (var->list, where))
14546 retval = false;
14547 goto cleanup;
14550 e = gfc_copy_expr (var->expr);
14551 if (!gfc_simplify_expr (e, 1))
14553 gfc_free_expr (e);
14554 retval = false;
14555 goto cleanup;
14558 mpz_add (frame.value, frame.value, step->value.integer);
14560 mpz_sub_ui (trip, trip, 1);
14563 cleanup:
14564 mpz_clear (frame.value);
14565 mpz_clear (trip);
14567 gfc_free_expr (start);
14568 gfc_free_expr (end);
14569 gfc_free_expr (step);
14571 iter_stack = frame.prev;
14572 return retval;
14576 /* Type resolve variables in the variable list of a DATA statement. */
14578 static bool
14579 traverse_data_var (gfc_data_variable *var, locus *where)
14581 bool t;
14583 for (; var; var = var->next)
14585 if (var->expr == NULL)
14586 t = traverse_data_list (var, where);
14587 else
14588 t = check_data_variable (var, where);
14590 if (!t)
14591 return false;
14594 return true;
14598 /* Resolve the expressions and iterators associated with a data statement.
14599 This is separate from the assignment checking because data lists should
14600 only be resolved once. */
14602 static bool
14603 resolve_data_variables (gfc_data_variable *d)
14605 for (; d; d = d->next)
14607 if (d->list == NULL)
14609 if (!gfc_resolve_expr (d->expr))
14610 return false;
14612 else
14614 if (!gfc_resolve_iterator (&d->iter, false, true))
14615 return false;
14617 if (!resolve_data_variables (d->list))
14618 return false;
14622 return true;
14626 /* Resolve a single DATA statement. We implement this by storing a pointer to
14627 the value list into static variables, and then recursively traversing the
14628 variables list, expanding iterators and such. */
14630 static void
14631 resolve_data (gfc_data *d)
14634 if (!resolve_data_variables (d->var))
14635 return;
14637 values.vnode = d->value;
14638 if (d->value == NULL)
14639 mpz_set_ui (values.left, 0);
14640 else
14641 mpz_set (values.left, d->value->repeat);
14643 if (!traverse_data_var (d->var, &d->where))
14644 return;
14646 /* At this point, we better not have any values left. */
14648 if (next_data_value ())
14649 gfc_error ("DATA statement at %L has more values than variables",
14650 &d->where);
14654 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
14655 accessed by host or use association, is a dummy argument to a pure function,
14656 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
14657 is storage associated with any such variable, shall not be used in the
14658 following contexts: (clients of this function). */
14660 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
14661 procedure. Returns zero if assignment is OK, nonzero if there is a
14662 problem. */
14664 gfc_impure_variable (gfc_symbol *sym)
14666 gfc_symbol *proc;
14667 gfc_namespace *ns;
14669 if (sym->attr.use_assoc || sym->attr.in_common)
14670 return 1;
14672 /* Check if the symbol's ns is inside the pure procedure. */
14673 for (ns = gfc_current_ns; ns; ns = ns->parent)
14675 if (ns == sym->ns)
14676 break;
14677 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
14678 return 1;
14681 proc = sym->ns->proc_name;
14682 if (sym->attr.dummy
14683 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
14684 || proc->attr.function))
14685 return 1;
14687 /* TODO: Sort out what can be storage associated, if anything, and include
14688 it here. In principle equivalences should be scanned but it does not
14689 seem to be possible to storage associate an impure variable this way. */
14690 return 0;
14694 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
14695 current namespace is inside a pure procedure. */
14698 gfc_pure (gfc_symbol *sym)
14700 symbol_attribute attr;
14701 gfc_namespace *ns;
14703 if (sym == NULL)
14705 /* Check if the current namespace or one of its parents
14706 belongs to a pure procedure. */
14707 for (ns = gfc_current_ns; ns; ns = ns->parent)
14709 sym = ns->proc_name;
14710 if (sym == NULL)
14711 return 0;
14712 attr = sym->attr;
14713 if (attr.flavor == FL_PROCEDURE && attr.pure)
14714 return 1;
14716 return 0;
14719 attr = sym->attr;
14721 return attr.flavor == FL_PROCEDURE && attr.pure;
14725 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
14726 checks if the current namespace is implicitly pure. Note that this
14727 function returns false for a PURE procedure. */
14730 gfc_implicit_pure (gfc_symbol *sym)
14732 gfc_namespace *ns;
14734 if (sym == NULL)
14736 /* Check if the current procedure is implicit_pure. Walk up
14737 the procedure list until we find a procedure. */
14738 for (ns = gfc_current_ns; ns; ns = ns->parent)
14740 sym = ns->proc_name;
14741 if (sym == NULL)
14742 return 0;
14744 if (sym->attr.flavor == FL_PROCEDURE)
14745 break;
14749 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
14750 && !sym->attr.pure;
14754 void
14755 gfc_unset_implicit_pure (gfc_symbol *sym)
14757 gfc_namespace *ns;
14759 if (sym == NULL)
14761 /* Check if the current procedure is implicit_pure. Walk up
14762 the procedure list until we find a procedure. */
14763 for (ns = gfc_current_ns; ns; ns = ns->parent)
14765 sym = ns->proc_name;
14766 if (sym == NULL)
14767 return;
14769 if (sym->attr.flavor == FL_PROCEDURE)
14770 break;
14774 if (sym->attr.flavor == FL_PROCEDURE)
14775 sym->attr.implicit_pure = 0;
14776 else
14777 sym->attr.pure = 0;
14781 /* Test whether the current procedure is elemental or not. */
14784 gfc_elemental (gfc_symbol *sym)
14786 symbol_attribute attr;
14788 if (sym == NULL)
14789 sym = gfc_current_ns->proc_name;
14790 if (sym == NULL)
14791 return 0;
14792 attr = sym->attr;
14794 return attr.flavor == FL_PROCEDURE && attr.elemental;
14798 /* Warn about unused labels. */
14800 static void
14801 warn_unused_fortran_label (gfc_st_label *label)
14803 if (label == NULL)
14804 return;
14806 warn_unused_fortran_label (label->left);
14808 if (label->defined == ST_LABEL_UNKNOWN)
14809 return;
14811 switch (label->referenced)
14813 case ST_LABEL_UNKNOWN:
14814 gfc_warning (0, "Label %d at %L defined but not used", label->value,
14815 &label->where);
14816 break;
14818 case ST_LABEL_BAD_TARGET:
14819 gfc_warning (0, "Label %d at %L defined but cannot be used",
14820 label->value, &label->where);
14821 break;
14823 default:
14824 break;
14827 warn_unused_fortran_label (label->right);
14831 /* Returns the sequence type of a symbol or sequence. */
14833 static seq_type
14834 sequence_type (gfc_typespec ts)
14836 seq_type result;
14837 gfc_component *c;
14839 switch (ts.type)
14841 case BT_DERIVED:
14843 if (ts.u.derived->components == NULL)
14844 return SEQ_NONDEFAULT;
14846 result = sequence_type (ts.u.derived->components->ts);
14847 for (c = ts.u.derived->components->next; c; c = c->next)
14848 if (sequence_type (c->ts) != result)
14849 return SEQ_MIXED;
14851 return result;
14853 case BT_CHARACTER:
14854 if (ts.kind != gfc_default_character_kind)
14855 return SEQ_NONDEFAULT;
14857 return SEQ_CHARACTER;
14859 case BT_INTEGER:
14860 if (ts.kind != gfc_default_integer_kind)
14861 return SEQ_NONDEFAULT;
14863 return SEQ_NUMERIC;
14865 case BT_REAL:
14866 if (!(ts.kind == gfc_default_real_kind
14867 || ts.kind == gfc_default_double_kind))
14868 return SEQ_NONDEFAULT;
14870 return SEQ_NUMERIC;
14872 case BT_COMPLEX:
14873 if (ts.kind != gfc_default_complex_kind)
14874 return SEQ_NONDEFAULT;
14876 return SEQ_NUMERIC;
14878 case BT_LOGICAL:
14879 if (ts.kind != gfc_default_logical_kind)
14880 return SEQ_NONDEFAULT;
14882 return SEQ_NUMERIC;
14884 default:
14885 return SEQ_NONDEFAULT;
14890 /* Resolve derived type EQUIVALENCE object. */
14892 static bool
14893 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
14895 gfc_component *c = derived->components;
14897 if (!derived)
14898 return true;
14900 /* Shall not be an object of nonsequence derived type. */
14901 if (!derived->attr.sequence)
14903 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
14904 "attribute to be an EQUIVALENCE object", sym->name,
14905 &e->where);
14906 return false;
14909 /* Shall not have allocatable components. */
14910 if (derived->attr.alloc_comp)
14912 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
14913 "components to be an EQUIVALENCE object",sym->name,
14914 &e->where);
14915 return false;
14918 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
14920 gfc_error ("Derived type variable %qs at %L with default "
14921 "initialization cannot be in EQUIVALENCE with a variable "
14922 "in COMMON", sym->name, &e->where);
14923 return false;
14926 for (; c ; c = c->next)
14928 if (c->ts.type == BT_DERIVED
14929 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
14930 return false;
14932 /* Shall not be an object of sequence derived type containing a pointer
14933 in the structure. */
14934 if (c->attr.pointer)
14936 gfc_error ("Derived type variable %qs at %L with pointer "
14937 "component(s) cannot be an EQUIVALENCE object",
14938 sym->name, &e->where);
14939 return false;
14942 return true;
14946 /* Resolve equivalence object.
14947 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
14948 an allocatable array, an object of nonsequence derived type, an object of
14949 sequence derived type containing a pointer at any level of component
14950 selection, an automatic object, a function name, an entry name, a result
14951 name, a named constant, a structure component, or a subobject of any of
14952 the preceding objects. A substring shall not have length zero. A
14953 derived type shall not have components with default initialization nor
14954 shall two objects of an equivalence group be initialized.
14955 Either all or none of the objects shall have an protected attribute.
14956 The simple constraints are done in symbol.c(check_conflict) and the rest
14957 are implemented here. */
14959 static void
14960 resolve_equivalence (gfc_equiv *eq)
14962 gfc_symbol *sym;
14963 gfc_symbol *first_sym;
14964 gfc_expr *e;
14965 gfc_ref *r;
14966 locus *last_where = NULL;
14967 seq_type eq_type, last_eq_type;
14968 gfc_typespec *last_ts;
14969 int object, cnt_protected;
14970 const char *msg;
14972 last_ts = &eq->expr->symtree->n.sym->ts;
14974 first_sym = eq->expr->symtree->n.sym;
14976 cnt_protected = 0;
14978 for (object = 1; eq; eq = eq->eq, object++)
14980 e = eq->expr;
14982 e->ts = e->symtree->n.sym->ts;
14983 /* match_varspec might not know yet if it is seeing
14984 array reference or substring reference, as it doesn't
14985 know the types. */
14986 if (e->ref && e->ref->type == REF_ARRAY)
14988 gfc_ref *ref = e->ref;
14989 sym = e->symtree->n.sym;
14991 if (sym->attr.dimension)
14993 ref->u.ar.as = sym->as;
14994 ref = ref->next;
14997 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
14998 if (e->ts.type == BT_CHARACTER
14999 && ref
15000 && ref->type == REF_ARRAY
15001 && ref->u.ar.dimen == 1
15002 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
15003 && ref->u.ar.stride[0] == NULL)
15005 gfc_expr *start = ref->u.ar.start[0];
15006 gfc_expr *end = ref->u.ar.end[0];
15007 void *mem = NULL;
15009 /* Optimize away the (:) reference. */
15010 if (start == NULL && end == NULL)
15012 if (e->ref == ref)
15013 e->ref = ref->next;
15014 else
15015 e->ref->next = ref->next;
15016 mem = ref;
15018 else
15020 ref->type = REF_SUBSTRING;
15021 if (start == NULL)
15022 start = gfc_get_int_expr (gfc_default_integer_kind,
15023 NULL, 1);
15024 ref->u.ss.start = start;
15025 if (end == NULL && e->ts.u.cl)
15026 end = gfc_copy_expr (e->ts.u.cl->length);
15027 ref->u.ss.end = end;
15028 ref->u.ss.length = e->ts.u.cl;
15029 e->ts.u.cl = NULL;
15031 ref = ref->next;
15032 free (mem);
15035 /* Any further ref is an error. */
15036 if (ref)
15038 gcc_assert (ref->type == REF_ARRAY);
15039 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
15040 &ref->u.ar.where);
15041 continue;
15045 if (!gfc_resolve_expr (e))
15046 continue;
15048 sym = e->symtree->n.sym;
15050 if (sym->attr.is_protected)
15051 cnt_protected++;
15052 if (cnt_protected > 0 && cnt_protected != object)
15054 gfc_error ("Either all or none of the objects in the "
15055 "EQUIVALENCE set at %L shall have the "
15056 "PROTECTED attribute",
15057 &e->where);
15058 break;
15061 /* Shall not equivalence common block variables in a PURE procedure. */
15062 if (sym->ns->proc_name
15063 && sym->ns->proc_name->attr.pure
15064 && sym->attr.in_common)
15066 gfc_error ("Common block member %qs at %L cannot be an EQUIVALENCE "
15067 "object in the pure procedure %qs",
15068 sym->name, &e->where, sym->ns->proc_name->name);
15069 break;
15072 /* Shall not be a named constant. */
15073 if (e->expr_type == EXPR_CONSTANT)
15075 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
15076 "object", sym->name, &e->where);
15077 continue;
15080 if (e->ts.type == BT_DERIVED
15081 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
15082 continue;
15084 /* Check that the types correspond correctly:
15085 Note 5.28:
15086 A numeric sequence structure may be equivalenced to another sequence
15087 structure, an object of default integer type, default real type, double
15088 precision real type, default logical type such that components of the
15089 structure ultimately only become associated to objects of the same
15090 kind. A character sequence structure may be equivalenced to an object
15091 of default character kind or another character sequence structure.
15092 Other objects may be equivalenced only to objects of the same type and
15093 kind parameters. */
15095 /* Identical types are unconditionally OK. */
15096 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
15097 goto identical_types;
15099 last_eq_type = sequence_type (*last_ts);
15100 eq_type = sequence_type (sym->ts);
15102 /* Since the pair of objects is not of the same type, mixed or
15103 non-default sequences can be rejected. */
15105 msg = "Sequence %s with mixed components in EQUIVALENCE "
15106 "statement at %L with different type objects";
15107 if ((object ==2
15108 && last_eq_type == SEQ_MIXED
15109 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
15110 || (eq_type == SEQ_MIXED
15111 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
15112 continue;
15114 msg = "Non-default type object or sequence %s in EQUIVALENCE "
15115 "statement at %L with objects of different type";
15116 if ((object ==2
15117 && last_eq_type == SEQ_NONDEFAULT
15118 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
15119 || (eq_type == SEQ_NONDEFAULT
15120 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
15121 continue;
15123 msg ="Non-CHARACTER object %qs in default CHARACTER "
15124 "EQUIVALENCE statement at %L";
15125 if (last_eq_type == SEQ_CHARACTER
15126 && eq_type != SEQ_CHARACTER
15127 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
15128 continue;
15130 msg ="Non-NUMERIC object %qs in default NUMERIC "
15131 "EQUIVALENCE statement at %L";
15132 if (last_eq_type == SEQ_NUMERIC
15133 && eq_type != SEQ_NUMERIC
15134 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
15135 continue;
15137 identical_types:
15138 last_ts =&sym->ts;
15139 last_where = &e->where;
15141 if (!e->ref)
15142 continue;
15144 /* Shall not be an automatic array. */
15145 if (e->ref->type == REF_ARRAY
15146 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
15148 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
15149 "an EQUIVALENCE object", sym->name, &e->where);
15150 continue;
15153 r = e->ref;
15154 while (r)
15156 /* Shall not be a structure component. */
15157 if (r->type == REF_COMPONENT)
15159 gfc_error ("Structure component %qs at %L cannot be an "
15160 "EQUIVALENCE object",
15161 r->u.c.component->name, &e->where);
15162 break;
15165 /* A substring shall not have length zero. */
15166 if (r->type == REF_SUBSTRING)
15168 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
15170 gfc_error ("Substring at %L has length zero",
15171 &r->u.ss.start->where);
15172 break;
15175 r = r->next;
15181 /* Resolve function and ENTRY types, issue diagnostics if needed. */
15183 static void
15184 resolve_fntype (gfc_namespace *ns)
15186 gfc_entry_list *el;
15187 gfc_symbol *sym;
15189 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
15190 return;
15192 /* If there are any entries, ns->proc_name is the entry master
15193 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
15194 if (ns->entries)
15195 sym = ns->entries->sym;
15196 else
15197 sym = ns->proc_name;
15198 if (sym->result == sym
15199 && sym->ts.type == BT_UNKNOWN
15200 && !gfc_set_default_type (sym, 0, NULL)
15201 && !sym->attr.untyped)
15203 gfc_error ("Function %qs at %L has no IMPLICIT type",
15204 sym->name, &sym->declared_at);
15205 sym->attr.untyped = 1;
15208 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
15209 && !sym->attr.contained
15210 && !gfc_check_symbol_access (sym->ts.u.derived)
15211 && gfc_check_symbol_access (sym))
15213 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
15214 "%L of PRIVATE type %qs", sym->name,
15215 &sym->declared_at, sym->ts.u.derived->name);
15218 if (ns->entries)
15219 for (el = ns->entries->next; el; el = el->next)
15221 if (el->sym->result == el->sym
15222 && el->sym->ts.type == BT_UNKNOWN
15223 && !gfc_set_default_type (el->sym, 0, NULL)
15224 && !el->sym->attr.untyped)
15226 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
15227 el->sym->name, &el->sym->declared_at);
15228 el->sym->attr.untyped = 1;
15234 /* 12.3.2.1.1 Defined operators. */
15236 static bool
15237 check_uop_procedure (gfc_symbol *sym, locus where)
15239 gfc_formal_arglist *formal;
15241 if (!sym->attr.function)
15243 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
15244 sym->name, &where);
15245 return false;
15248 if (sym->ts.type == BT_CHARACTER
15249 && !(sym->ts.u.cl && sym->ts.u.cl->length)
15250 && !(sym->result && sym->result->ts.u.cl
15251 && sym->result->ts.u.cl->length))
15253 gfc_error ("User operator procedure %qs at %L cannot be assumed "
15254 "character length", sym->name, &where);
15255 return false;
15258 formal = gfc_sym_get_dummy_args (sym);
15259 if (!formal || !formal->sym)
15261 gfc_error ("User operator procedure %qs at %L must have at least "
15262 "one argument", sym->name, &where);
15263 return false;
15266 if (formal->sym->attr.intent != INTENT_IN)
15268 gfc_error ("First argument of operator interface at %L must be "
15269 "INTENT(IN)", &where);
15270 return false;
15273 if (formal->sym->attr.optional)
15275 gfc_error ("First argument of operator interface at %L cannot be "
15276 "optional", &where);
15277 return false;
15280 formal = formal->next;
15281 if (!formal || !formal->sym)
15282 return true;
15284 if (formal->sym->attr.intent != INTENT_IN)
15286 gfc_error ("Second argument of operator interface at %L must be "
15287 "INTENT(IN)", &where);
15288 return false;
15291 if (formal->sym->attr.optional)
15293 gfc_error ("Second argument of operator interface at %L cannot be "
15294 "optional", &where);
15295 return false;
15298 if (formal->next)
15300 gfc_error ("Operator interface at %L must have, at most, two "
15301 "arguments", &where);
15302 return false;
15305 return true;
15308 static void
15309 gfc_resolve_uops (gfc_symtree *symtree)
15311 gfc_interface *itr;
15313 if (symtree == NULL)
15314 return;
15316 gfc_resolve_uops (symtree->left);
15317 gfc_resolve_uops (symtree->right);
15319 for (itr = symtree->n.uop->op; itr; itr = itr->next)
15320 check_uop_procedure (itr->sym, itr->sym->declared_at);
15324 /* Examine all of the expressions associated with a program unit,
15325 assign types to all intermediate expressions, make sure that all
15326 assignments are to compatible types and figure out which names
15327 refer to which functions or subroutines. It doesn't check code
15328 block, which is handled by gfc_resolve_code. */
15330 static void
15331 resolve_types (gfc_namespace *ns)
15333 gfc_namespace *n;
15334 gfc_charlen *cl;
15335 gfc_data *d;
15336 gfc_equiv *eq;
15337 gfc_namespace* old_ns = gfc_current_ns;
15339 if (ns->types_resolved)
15340 return;
15342 /* Check that all IMPLICIT types are ok. */
15343 if (!ns->seen_implicit_none)
15345 unsigned letter;
15346 for (letter = 0; letter != GFC_LETTERS; ++letter)
15347 if (ns->set_flag[letter]
15348 && !resolve_typespec_used (&ns->default_type[letter],
15349 &ns->implicit_loc[letter], NULL))
15350 return;
15353 gfc_current_ns = ns;
15355 resolve_entries (ns);
15357 resolve_common_vars (&ns->blank_common, false);
15358 resolve_common_blocks (ns->common_root);
15360 resolve_contained_functions (ns);
15362 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
15363 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
15364 resolve_formal_arglist (ns->proc_name);
15366 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
15368 for (cl = ns->cl_list; cl; cl = cl->next)
15369 resolve_charlen (cl);
15371 gfc_traverse_ns (ns, resolve_symbol);
15373 resolve_fntype (ns);
15375 for (n = ns->contained; n; n = n->sibling)
15377 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
15378 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
15379 "also be PURE", n->proc_name->name,
15380 &n->proc_name->declared_at);
15382 resolve_types (n);
15385 forall_flag = 0;
15386 gfc_do_concurrent_flag = 0;
15387 gfc_check_interfaces (ns);
15389 gfc_traverse_ns (ns, resolve_values);
15391 if (ns->save_all)
15392 gfc_save_all (ns);
15394 iter_stack = NULL;
15395 for (d = ns->data; d; d = d->next)
15396 resolve_data (d);
15398 iter_stack = NULL;
15399 gfc_traverse_ns (ns, gfc_formalize_init_value);
15401 gfc_traverse_ns (ns, gfc_verify_binding_labels);
15403 for (eq = ns->equiv; eq; eq = eq->next)
15404 resolve_equivalence (eq);
15406 /* Warn about unused labels. */
15407 if (warn_unused_label)
15408 warn_unused_fortran_label (ns->st_labels);
15410 gfc_resolve_uops (ns->uop_root);
15412 gfc_resolve_omp_declare_simd (ns);
15414 gfc_resolve_omp_udrs (ns->omp_udr_root);
15416 ns->types_resolved = 1;
15418 gfc_current_ns = old_ns;
15422 /* Call gfc_resolve_code recursively. */
15424 static void
15425 resolve_codes (gfc_namespace *ns)
15427 gfc_namespace *n;
15428 bitmap_obstack old_obstack;
15430 if (ns->resolved == 1)
15431 return;
15433 for (n = ns->contained; n; n = n->sibling)
15434 resolve_codes (n);
15436 gfc_current_ns = ns;
15438 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
15439 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
15440 cs_base = NULL;
15442 /* Set to an out of range value. */
15443 current_entry_id = -1;
15445 old_obstack = labels_obstack;
15446 bitmap_obstack_initialize (&labels_obstack);
15448 gfc_resolve_oacc_declare (ns);
15449 gfc_resolve_code (ns->code, ns);
15451 bitmap_obstack_release (&labels_obstack);
15452 labels_obstack = old_obstack;
15456 /* This function is called after a complete program unit has been compiled.
15457 Its purpose is to examine all of the expressions associated with a program
15458 unit, assign types to all intermediate expressions, make sure that all
15459 assignments are to compatible types and figure out which names refer to
15460 which functions or subroutines. */
15462 void
15463 gfc_resolve (gfc_namespace *ns)
15465 gfc_namespace *old_ns;
15466 code_stack *old_cs_base;
15467 struct gfc_omp_saved_state old_omp_state;
15469 if (ns->resolved)
15470 return;
15472 ns->resolved = -1;
15473 old_ns = gfc_current_ns;
15474 old_cs_base = cs_base;
15476 /* As gfc_resolve can be called during resolution of an OpenMP construct
15477 body, we should clear any state associated to it, so that say NS's
15478 DO loops are not interpreted as OpenMP loops. */
15479 gfc_omp_save_and_clear_state (&old_omp_state);
15481 resolve_types (ns);
15482 component_assignment_level = 0;
15483 resolve_codes (ns);
15485 gfc_current_ns = old_ns;
15486 cs_base = old_cs_base;
15487 ns->resolved = 1;
15489 gfc_run_passes (ns);
15491 gfc_omp_restore_state (&old_omp_state);