2017-10-07 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / resolve.c
blobbd316344813c6fd783c6ac30e5f0847fca5b114b
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2017 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "bitmap.h"
26 #include "gfortran.h"
27 #include "arith.h" /* For gfc_compare_expr(). */
28 #include "dependency.h"
29 #include "data.h"
30 #include "target-memory.h" /* for gfc_simplify_transfer */
31 #include "constructor.h"
33 /* Types used in equivalence statements. */
35 enum seq_type
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
40 /* Stack to keep track of the nesting of blocks as we move through the
41 code. See resolve_branch() and gfc_resolve_code(). */
43 typedef struct code_stack
45 struct gfc_code *head, *current;
46 struct code_stack *prev;
48 /* This bitmap keeps track of the targets valid for a branch from
49 inside this block except for END {IF|SELECT}s of enclosing
50 blocks. */
51 bitmap reachable_labels;
53 code_stack;
55 static code_stack *cs_base = NULL;
58 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
60 static int forall_flag;
61 int gfc_do_concurrent_flag;
63 /* True when we are resolving an expression that is an actual argument to
64 a procedure. */
65 static bool actual_arg = false;
66 /* True when we are resolving an expression that is the first actual argument
67 to a procedure. */
68 static bool first_actual_arg = false;
71 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
73 static int omp_workshare_flag;
75 /* True if we are processing a formal arglist. The corresponding function
76 resets the flag each time that it is read. */
77 static bool formal_arg_flag = false;
79 /* True if we are resolving a specification expression. */
80 static bool specification_expr = false;
82 /* The id of the last entry seen. */
83 static int current_entry_id;
85 /* We use bitmaps to determine if a branch target is valid. */
86 static bitmap_obstack labels_obstack;
88 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
89 static bool inquiry_argument = false;
92 bool
93 gfc_is_formal_arg (void)
95 return formal_arg_flag;
98 /* Is the symbol host associated? */
99 static bool
100 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
102 for (ns = ns->parent; ns; ns = ns->parent)
104 if (sym->ns == ns)
105 return true;
108 return false;
111 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
112 an ABSTRACT derived-type. If where is not NULL, an error message with that
113 locus is printed, optionally using name. */
115 static bool
116 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
118 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
120 if (where)
122 if (name)
123 gfc_error ("%qs at %L is of the ABSTRACT type %qs",
124 name, where, ts->u.derived->name);
125 else
126 gfc_error ("ABSTRACT type %qs used at %L",
127 ts->u.derived->name, where);
130 return false;
133 return true;
137 static bool
138 check_proc_interface (gfc_symbol *ifc, locus *where)
140 /* Several checks for F08:C1216. */
141 if (ifc->attr.procedure)
143 gfc_error ("Interface %qs at %L is declared "
144 "in a later PROCEDURE statement", ifc->name, where);
145 return false;
147 if (ifc->generic)
149 /* For generic interfaces, check if there is
150 a specific procedure with the same name. */
151 gfc_interface *gen = ifc->generic;
152 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
153 gen = gen->next;
154 if (!gen)
156 gfc_error ("Interface %qs at %L may not be generic",
157 ifc->name, where);
158 return false;
161 if (ifc->attr.proc == PROC_ST_FUNCTION)
163 gfc_error ("Interface %qs at %L may not be a statement function",
164 ifc->name, where);
165 return false;
167 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
168 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
169 ifc->attr.intrinsic = 1;
170 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
172 gfc_error ("Intrinsic procedure %qs not allowed in "
173 "PROCEDURE statement at %L", ifc->name, where);
174 return false;
176 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
178 gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
179 return false;
181 return true;
185 static void resolve_symbol (gfc_symbol *sym);
188 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
190 static bool
191 resolve_procedure_interface (gfc_symbol *sym)
193 gfc_symbol *ifc = sym->ts.interface;
195 if (!ifc)
196 return true;
198 if (ifc == sym)
200 gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
201 sym->name, &sym->declared_at);
202 return false;
204 if (!check_proc_interface (ifc, &sym->declared_at))
205 return false;
207 if (ifc->attr.if_source || ifc->attr.intrinsic)
209 /* Resolve interface and copy attributes. */
210 resolve_symbol (ifc);
211 if (ifc->attr.intrinsic)
212 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
214 if (ifc->result)
216 sym->ts = ifc->result->ts;
217 sym->attr.allocatable = ifc->result->attr.allocatable;
218 sym->attr.pointer = ifc->result->attr.pointer;
219 sym->attr.dimension = ifc->result->attr.dimension;
220 sym->attr.class_ok = ifc->result->attr.class_ok;
221 sym->as = gfc_copy_array_spec (ifc->result->as);
222 sym->result = sym;
224 else
226 sym->ts = ifc->ts;
227 sym->attr.allocatable = ifc->attr.allocatable;
228 sym->attr.pointer = ifc->attr.pointer;
229 sym->attr.dimension = ifc->attr.dimension;
230 sym->attr.class_ok = ifc->attr.class_ok;
231 sym->as = gfc_copy_array_spec (ifc->as);
233 sym->ts.interface = ifc;
234 sym->attr.function = ifc->attr.function;
235 sym->attr.subroutine = ifc->attr.subroutine;
237 sym->attr.pure = ifc->attr.pure;
238 sym->attr.elemental = ifc->attr.elemental;
239 sym->attr.contiguous = ifc->attr.contiguous;
240 sym->attr.recursive = ifc->attr.recursive;
241 sym->attr.always_explicit = ifc->attr.always_explicit;
242 sym->attr.ext_attr |= ifc->attr.ext_attr;
243 sym->attr.is_bind_c = ifc->attr.is_bind_c;
244 /* Copy char length. */
245 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
247 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
248 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
249 && !gfc_resolve_expr (sym->ts.u.cl->length))
250 return false;
254 return true;
258 /* Resolve types of formal argument lists. These have to be done early so that
259 the formal argument lists of module procedures can be copied to the
260 containing module before the individual procedures are resolved
261 individually. We also resolve argument lists of procedures in interface
262 blocks because they are self-contained scoping units.
264 Since a dummy argument cannot be a non-dummy procedure, the only
265 resort left for untyped names are the IMPLICIT types. */
267 static void
268 resolve_formal_arglist (gfc_symbol *proc)
270 gfc_formal_arglist *f;
271 gfc_symbol *sym;
272 bool saved_specification_expr;
273 int i;
275 if (proc->result != NULL)
276 sym = proc->result;
277 else
278 sym = proc;
280 if (gfc_elemental (proc)
281 || sym->attr.pointer || sym->attr.allocatable
282 || (sym->as && sym->as->rank != 0))
284 proc->attr.always_explicit = 1;
285 sym->attr.always_explicit = 1;
288 formal_arg_flag = true;
290 for (f = proc->formal; f; f = f->next)
292 gfc_array_spec *as;
294 sym = f->sym;
296 if (sym == NULL)
298 /* Alternate return placeholder. */
299 if (gfc_elemental (proc))
300 gfc_error ("Alternate return specifier in elemental subroutine "
301 "%qs at %L is not allowed", proc->name,
302 &proc->declared_at);
303 if (proc->attr.function)
304 gfc_error ("Alternate return specifier in function "
305 "%qs at %L is not allowed", proc->name,
306 &proc->declared_at);
307 continue;
309 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
310 && !resolve_procedure_interface (sym))
311 return;
313 if (strcmp (proc->name, sym->name) == 0)
315 gfc_error ("Self-referential argument "
316 "%qs at %L is not allowed", sym->name,
317 &proc->declared_at);
318 return;
321 if (sym->attr.if_source != IFSRC_UNKNOWN)
322 resolve_formal_arglist (sym);
324 if (sym->attr.subroutine || sym->attr.external)
326 if (sym->attr.flavor == FL_UNKNOWN)
327 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
329 else
331 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
332 && (!sym->attr.function || sym->result == sym))
333 gfc_set_default_type (sym, 1, sym->ns);
336 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
337 ? CLASS_DATA (sym)->as : sym->as;
339 saved_specification_expr = specification_expr;
340 specification_expr = true;
341 gfc_resolve_array_spec (as, 0);
342 specification_expr = saved_specification_expr;
344 /* We can't tell if an array with dimension (:) is assumed or deferred
345 shape until we know if it has the pointer or allocatable attributes.
347 if (as && as->rank > 0 && as->type == AS_DEFERRED
348 && ((sym->ts.type != BT_CLASS
349 && !(sym->attr.pointer || sym->attr.allocatable))
350 || (sym->ts.type == BT_CLASS
351 && !(CLASS_DATA (sym)->attr.class_pointer
352 || CLASS_DATA (sym)->attr.allocatable)))
353 && sym->attr.flavor != FL_PROCEDURE)
355 as->type = AS_ASSUMED_SHAPE;
356 for (i = 0; i < as->rank; i++)
357 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
360 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
361 || (as && as->type == AS_ASSUMED_RANK)
362 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
363 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
364 && (CLASS_DATA (sym)->attr.class_pointer
365 || CLASS_DATA (sym)->attr.allocatable
366 || CLASS_DATA (sym)->attr.target))
367 || sym->attr.optional)
369 proc->attr.always_explicit = 1;
370 if (proc->result)
371 proc->result->attr.always_explicit = 1;
374 /* If the flavor is unknown at this point, it has to be a variable.
375 A procedure specification would have already set the type. */
377 if (sym->attr.flavor == FL_UNKNOWN)
378 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
380 if (gfc_pure (proc))
382 if (sym->attr.flavor == FL_PROCEDURE)
384 /* F08:C1279. */
385 if (!gfc_pure (sym))
387 gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
388 "also be PURE", sym->name, &sym->declared_at);
389 continue;
392 else if (!sym->attr.pointer)
394 if (proc->attr.function && sym->attr.intent != INTENT_IN)
396 if (sym->attr.value)
397 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
398 " of pure function %qs at %L with VALUE "
399 "attribute but without INTENT(IN)",
400 sym->name, proc->name, &sym->declared_at);
401 else
402 gfc_error ("Argument %qs of pure function %qs at %L must "
403 "be INTENT(IN) or VALUE", sym->name, proc->name,
404 &sym->declared_at);
407 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
409 if (sym->attr.value)
410 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
411 " of pure subroutine %qs at %L with VALUE "
412 "attribute but without INTENT", sym->name,
413 proc->name, &sym->declared_at);
414 else
415 gfc_error ("Argument %qs of pure subroutine %qs at %L "
416 "must have its INTENT specified or have the "
417 "VALUE attribute", sym->name, proc->name,
418 &sym->declared_at);
422 /* F08:C1278a. */
423 if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
425 gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
426 " may not be polymorphic", sym->name, proc->name,
427 &sym->declared_at);
428 continue;
432 if (proc->attr.implicit_pure)
434 if (sym->attr.flavor == FL_PROCEDURE)
436 if (!gfc_pure (sym))
437 proc->attr.implicit_pure = 0;
439 else if (!sym->attr.pointer)
441 if (proc->attr.function && sym->attr.intent != INTENT_IN
442 && !sym->value)
443 proc->attr.implicit_pure = 0;
445 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
446 && !sym->value)
447 proc->attr.implicit_pure = 0;
451 if (gfc_elemental (proc))
453 /* F08:C1289. */
454 if (sym->attr.codimension
455 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
456 && CLASS_DATA (sym)->attr.codimension))
458 gfc_error ("Coarray dummy argument %qs at %L to elemental "
459 "procedure", sym->name, &sym->declared_at);
460 continue;
463 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
464 && CLASS_DATA (sym)->as))
466 gfc_error ("Argument %qs of elemental procedure at %L must "
467 "be scalar", sym->name, &sym->declared_at);
468 continue;
471 if (sym->attr.allocatable
472 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
473 && CLASS_DATA (sym)->attr.allocatable))
475 gfc_error ("Argument %qs of elemental procedure at %L cannot "
476 "have the ALLOCATABLE attribute", sym->name,
477 &sym->declared_at);
478 continue;
481 if (sym->attr.pointer
482 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
483 && CLASS_DATA (sym)->attr.class_pointer))
485 gfc_error ("Argument %qs of elemental procedure at %L cannot "
486 "have the POINTER attribute", sym->name,
487 &sym->declared_at);
488 continue;
491 if (sym->attr.flavor == FL_PROCEDURE)
493 gfc_error ("Dummy procedure %qs not allowed in elemental "
494 "procedure %qs at %L", sym->name, proc->name,
495 &sym->declared_at);
496 continue;
499 /* Fortran 2008 Corrigendum 1, C1290a. */
500 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
502 gfc_error ("Argument %qs of elemental procedure %qs at %L must "
503 "have its INTENT specified or have the VALUE "
504 "attribute", sym->name, proc->name,
505 &sym->declared_at);
506 continue;
510 /* Each dummy shall be specified to be scalar. */
511 if (proc->attr.proc == PROC_ST_FUNCTION)
513 if (sym->as != NULL)
515 gfc_error ("Argument %qs of statement function at %L must "
516 "be scalar", sym->name, &sym->declared_at);
517 continue;
520 if (sym->ts.type == BT_CHARACTER)
522 gfc_charlen *cl = sym->ts.u.cl;
523 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
525 gfc_error ("Character-valued argument %qs of statement "
526 "function at %L must have constant length",
527 sym->name, &sym->declared_at);
528 continue;
533 formal_arg_flag = false;
537 /* Work function called when searching for symbols that have argument lists
538 associated with them. */
540 static void
541 find_arglists (gfc_symbol *sym)
543 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
544 || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
545 return;
547 resolve_formal_arglist (sym);
551 /* Given a namespace, resolve all formal argument lists within the namespace.
554 static void
555 resolve_formal_arglists (gfc_namespace *ns)
557 if (ns == NULL)
558 return;
560 gfc_traverse_ns (ns, find_arglists);
564 static void
565 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
567 bool t;
569 if (sym && sym->attr.flavor == FL_PROCEDURE
570 && sym->ns->parent
571 && sym->ns->parent->proc_name
572 && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
573 && !strcmp (sym->name, sym->ns->parent->proc_name->name))
574 gfc_error ("Contained procedure %qs at %L has the same name as its "
575 "encompassing procedure", sym->name, &sym->declared_at);
577 /* If this namespace is not a function or an entry master function,
578 ignore it. */
579 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
580 || sym->attr.entry_master)
581 return;
583 /* Try to find out of what the return type is. */
584 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
586 t = gfc_set_default_type (sym->result, 0, ns);
588 if (!t && !sym->result->attr.untyped)
590 if (sym->result == sym)
591 gfc_error ("Contained function %qs at %L has no IMPLICIT type",
592 sym->name, &sym->declared_at);
593 else if (!sym->result->attr.proc_pointer)
594 gfc_error ("Result %qs of contained function %qs at %L has "
595 "no IMPLICIT type", sym->result->name, sym->name,
596 &sym->result->declared_at);
597 sym->result->attr.untyped = 1;
601 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
602 type, lists the only ways a character length value of * can be used:
603 dummy arguments of procedures, named constants, and function results
604 in external functions. Internal function results and results of module
605 procedures are not on this list, ergo, not permitted. */
607 if (sym->result->ts.type == BT_CHARACTER)
609 gfc_charlen *cl = sym->result->ts.u.cl;
610 if ((!cl || !cl->length) && !sym->result->ts.deferred)
612 /* See if this is a module-procedure and adapt error message
613 accordingly. */
614 bool module_proc;
615 gcc_assert (ns->parent && ns->parent->proc_name);
616 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
618 gfc_error (module_proc
619 ? G_("Character-valued module procedure %qs at %L"
620 " must not be assumed length")
621 : G_("Character-valued internal function %qs at %L"
622 " must not be assumed length"),
623 sym->name, &sym->declared_at);
629 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
630 introduce duplicates. */
632 static void
633 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
635 gfc_formal_arglist *f, *new_arglist;
636 gfc_symbol *new_sym;
638 for (; new_args != NULL; new_args = new_args->next)
640 new_sym = new_args->sym;
641 /* See if this arg is already in the formal argument list. */
642 for (f = proc->formal; f; f = f->next)
644 if (new_sym == f->sym)
645 break;
648 if (f)
649 continue;
651 /* Add a new argument. Argument order is not important. */
652 new_arglist = gfc_get_formal_arglist ();
653 new_arglist->sym = new_sym;
654 new_arglist->next = proc->formal;
655 proc->formal = new_arglist;
660 /* Flag the arguments that are not present in all entries. */
662 static void
663 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
665 gfc_formal_arglist *f, *head;
666 head = new_args;
668 for (f = proc->formal; f; f = f->next)
670 if (f->sym == NULL)
671 continue;
673 for (new_args = head; new_args; new_args = new_args->next)
675 if (new_args->sym == f->sym)
676 break;
679 if (new_args)
680 continue;
682 f->sym->attr.not_always_present = 1;
687 /* Resolve alternate entry points. If a symbol has multiple entry points we
688 create a new master symbol for the main routine, and turn the existing
689 symbol into an entry point. */
691 static void
692 resolve_entries (gfc_namespace *ns)
694 gfc_namespace *old_ns;
695 gfc_code *c;
696 gfc_symbol *proc;
697 gfc_entry_list *el;
698 char name[GFC_MAX_SYMBOL_LEN + 1];
699 static int master_count = 0;
701 if (ns->proc_name == NULL)
702 return;
704 /* No need to do anything if this procedure doesn't have alternate entry
705 points. */
706 if (!ns->entries)
707 return;
709 /* We may already have resolved alternate entry points. */
710 if (ns->proc_name->attr.entry_master)
711 return;
713 /* If this isn't a procedure something has gone horribly wrong. */
714 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
716 /* Remember the current namespace. */
717 old_ns = gfc_current_ns;
719 gfc_current_ns = ns;
721 /* Add the main entry point to the list of entry points. */
722 el = gfc_get_entry_list ();
723 el->sym = ns->proc_name;
724 el->id = 0;
725 el->next = ns->entries;
726 ns->entries = el;
727 ns->proc_name->attr.entry = 1;
729 /* If it is a module function, it needs to be in the right namespace
730 so that gfc_get_fake_result_decl can gather up the results. The
731 need for this arose in get_proc_name, where these beasts were
732 left in their own namespace, to keep prior references linked to
733 the entry declaration.*/
734 if (ns->proc_name->attr.function
735 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
736 el->sym->ns = ns;
738 /* Do the same for entries where the master is not a module
739 procedure. These are retained in the module namespace because
740 of the module procedure declaration. */
741 for (el = el->next; el; el = el->next)
742 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
743 && el->sym->attr.mod_proc)
744 el->sym->ns = ns;
745 el = ns->entries;
747 /* Add an entry statement for it. */
748 c = gfc_get_code (EXEC_ENTRY);
749 c->ext.entry = el;
750 c->next = ns->code;
751 ns->code = c;
753 /* Create a new symbol for the master function. */
754 /* Give the internal function a unique name (within this file).
755 Also include the function name so the user has some hope of figuring
756 out what is going on. */
757 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
758 master_count++, ns->proc_name->name);
759 gfc_get_ha_symbol (name, &proc);
760 gcc_assert (proc != NULL);
762 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
763 if (ns->proc_name->attr.subroutine)
764 gfc_add_subroutine (&proc->attr, proc->name, NULL);
765 else
767 gfc_symbol *sym;
768 gfc_typespec *ts, *fts;
769 gfc_array_spec *as, *fas;
770 gfc_add_function (&proc->attr, proc->name, NULL);
771 proc->result = proc;
772 fas = ns->entries->sym->as;
773 fas = fas ? fas : ns->entries->sym->result->as;
774 fts = &ns->entries->sym->result->ts;
775 if (fts->type == BT_UNKNOWN)
776 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
777 for (el = ns->entries->next; el; el = el->next)
779 ts = &el->sym->result->ts;
780 as = el->sym->as;
781 as = as ? as : el->sym->result->as;
782 if (ts->type == BT_UNKNOWN)
783 ts = gfc_get_default_type (el->sym->result->name, NULL);
785 if (! gfc_compare_types (ts, fts)
786 || (el->sym->result->attr.dimension
787 != ns->entries->sym->result->attr.dimension)
788 || (el->sym->result->attr.pointer
789 != ns->entries->sym->result->attr.pointer))
790 break;
791 else if (as && fas && ns->entries->sym->result != el->sym->result
792 && gfc_compare_array_spec (as, fas) == 0)
793 gfc_error ("Function %s at %L has entries with mismatched "
794 "array specifications", ns->entries->sym->name,
795 &ns->entries->sym->declared_at);
796 /* The characteristics need to match and thus both need to have
797 the same string length, i.e. both len=*, or both len=4.
798 Having both len=<variable> is also possible, but difficult to
799 check at compile time. */
800 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
801 && (((ts->u.cl->length && !fts->u.cl->length)
802 ||(!ts->u.cl->length && fts->u.cl->length))
803 || (ts->u.cl->length
804 && ts->u.cl->length->expr_type
805 != fts->u.cl->length->expr_type)
806 || (ts->u.cl->length
807 && ts->u.cl->length->expr_type == EXPR_CONSTANT
808 && mpz_cmp (ts->u.cl->length->value.integer,
809 fts->u.cl->length->value.integer) != 0)))
810 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
811 "entries returning variables of different "
812 "string lengths", ns->entries->sym->name,
813 &ns->entries->sym->declared_at);
816 if (el == NULL)
818 sym = ns->entries->sym->result;
819 /* All result types the same. */
820 proc->ts = *fts;
821 if (sym->attr.dimension)
822 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
823 if (sym->attr.pointer)
824 gfc_add_pointer (&proc->attr, NULL);
826 else
828 /* Otherwise the result will be passed through a union by
829 reference. */
830 proc->attr.mixed_entry_master = 1;
831 for (el = ns->entries; el; el = el->next)
833 sym = el->sym->result;
834 if (sym->attr.dimension)
836 if (el == ns->entries)
837 gfc_error ("FUNCTION result %s can't be an array in "
838 "FUNCTION %s at %L", sym->name,
839 ns->entries->sym->name, &sym->declared_at);
840 else
841 gfc_error ("ENTRY result %s can't be an array in "
842 "FUNCTION %s at %L", sym->name,
843 ns->entries->sym->name, &sym->declared_at);
845 else if (sym->attr.pointer)
847 if (el == ns->entries)
848 gfc_error ("FUNCTION result %s can't be a POINTER in "
849 "FUNCTION %s at %L", sym->name,
850 ns->entries->sym->name, &sym->declared_at);
851 else
852 gfc_error ("ENTRY result %s can't be a POINTER in "
853 "FUNCTION %s at %L", sym->name,
854 ns->entries->sym->name, &sym->declared_at);
856 else
858 ts = &sym->ts;
859 if (ts->type == BT_UNKNOWN)
860 ts = gfc_get_default_type (sym->name, NULL);
861 switch (ts->type)
863 case BT_INTEGER:
864 if (ts->kind == gfc_default_integer_kind)
865 sym = NULL;
866 break;
867 case BT_REAL:
868 if (ts->kind == gfc_default_real_kind
869 || ts->kind == gfc_default_double_kind)
870 sym = NULL;
871 break;
872 case BT_COMPLEX:
873 if (ts->kind == gfc_default_complex_kind)
874 sym = NULL;
875 break;
876 case BT_LOGICAL:
877 if (ts->kind == gfc_default_logical_kind)
878 sym = NULL;
879 break;
880 case BT_UNKNOWN:
881 /* We will issue error elsewhere. */
882 sym = NULL;
883 break;
884 default:
885 break;
887 if (sym)
889 if (el == ns->entries)
890 gfc_error ("FUNCTION result %s can't be of type %s "
891 "in FUNCTION %s at %L", sym->name,
892 gfc_typename (ts), ns->entries->sym->name,
893 &sym->declared_at);
894 else
895 gfc_error ("ENTRY result %s can't be of type %s "
896 "in FUNCTION %s at %L", sym->name,
897 gfc_typename (ts), ns->entries->sym->name,
898 &sym->declared_at);
904 proc->attr.access = ACCESS_PRIVATE;
905 proc->attr.entry_master = 1;
907 /* Merge all the entry point arguments. */
908 for (el = ns->entries; el; el = el->next)
909 merge_argument_lists (proc, el->sym->formal);
911 /* Check the master formal arguments for any that are not
912 present in all entry points. */
913 for (el = ns->entries; el; el = el->next)
914 check_argument_lists (proc, el->sym->formal);
916 /* Use the master function for the function body. */
917 ns->proc_name = proc;
919 /* Finalize the new symbols. */
920 gfc_commit_symbols ();
922 /* Restore the original namespace. */
923 gfc_current_ns = old_ns;
927 /* Resolve common variables. */
928 static void
929 resolve_common_vars (gfc_common_head *common_block, bool named_common)
931 gfc_symbol *csym = common_block->head;
933 for (; csym; csym = csym->common_next)
935 /* gfc_add_in_common may have been called before, but the reported errors
936 have been ignored to continue parsing.
937 We do the checks again here. */
938 if (!csym->attr.use_assoc)
939 gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
941 if (csym->value || csym->attr.data)
943 if (!csym->ns->is_block_data)
944 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
945 "but only in BLOCK DATA initialization is "
946 "allowed", csym->name, &csym->declared_at);
947 else if (!named_common)
948 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
949 "in a blank COMMON but initialization is only "
950 "allowed in named common blocks", csym->name,
951 &csym->declared_at);
954 if (UNLIMITED_POLY (csym))
955 gfc_error_now ("%qs in cannot appear in COMMON at %L "
956 "[F2008:C5100]", csym->name, &csym->declared_at);
958 if (csym->ts.type != BT_DERIVED)
959 continue;
961 if (!(csym->ts.u.derived->attr.sequence
962 || csym->ts.u.derived->attr.is_bind_c))
963 gfc_error_now ("Derived type variable %qs in COMMON at %L "
964 "has neither the SEQUENCE nor the BIND(C) "
965 "attribute", csym->name, &csym->declared_at);
966 if (csym->ts.u.derived->attr.alloc_comp)
967 gfc_error_now ("Derived type variable %qs in COMMON at %L "
968 "has an ultimate component that is "
969 "allocatable", csym->name, &csym->declared_at);
970 if (gfc_has_default_initializer (csym->ts.u.derived))
971 gfc_error_now ("Derived type variable %qs in COMMON at %L "
972 "may not have default initializer", csym->name,
973 &csym->declared_at);
975 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
976 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
980 /* Resolve common blocks. */
981 static void
982 resolve_common_blocks (gfc_symtree *common_root)
984 gfc_symbol *sym;
985 gfc_gsymbol * gsym;
987 if (common_root == NULL)
988 return;
990 if (common_root->left)
991 resolve_common_blocks (common_root->left);
992 if (common_root->right)
993 resolve_common_blocks (common_root->right);
995 resolve_common_vars (common_root->n.common, true);
997 /* The common name is a global name - in Fortran 2003 also if it has a
998 C binding name, since Fortran 2008 only the C binding name is a global
999 identifier. */
1000 if (!common_root->n.common->binding_label
1001 || gfc_notification_std (GFC_STD_F2008))
1003 gsym = gfc_find_gsymbol (gfc_gsym_root,
1004 common_root->n.common->name);
1006 if (gsym && gfc_notification_std (GFC_STD_F2008)
1007 && gsym->type == GSYM_COMMON
1008 && ((common_root->n.common->binding_label
1009 && (!gsym->binding_label
1010 || strcmp (common_root->n.common->binding_label,
1011 gsym->binding_label) != 0))
1012 || (!common_root->n.common->binding_label
1013 && gsym->binding_label)))
1015 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1016 "identifier and must thus have the same binding name "
1017 "as the same-named COMMON block at %L: %s vs %s",
1018 common_root->n.common->name, &common_root->n.common->where,
1019 &gsym->where,
1020 common_root->n.common->binding_label
1021 ? common_root->n.common->binding_label : "(blank)",
1022 gsym->binding_label ? gsym->binding_label : "(blank)");
1023 return;
1026 if (gsym && gsym->type != GSYM_COMMON
1027 && !common_root->n.common->binding_label)
1029 gfc_error ("COMMON block %qs at %L uses the same global identifier "
1030 "as entity at %L",
1031 common_root->n.common->name, &common_root->n.common->where,
1032 &gsym->where);
1033 return;
1035 if (gsym && gsym->type != GSYM_COMMON)
1037 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1038 "%L sharing the identifier with global non-COMMON-block "
1039 "entity at %L", common_root->n.common->name,
1040 &common_root->n.common->where, &gsym->where);
1041 return;
1043 if (!gsym)
1045 gsym = gfc_get_gsymbol (common_root->n.common->name);
1046 gsym->type = GSYM_COMMON;
1047 gsym->where = common_root->n.common->where;
1048 gsym->defined = 1;
1050 gsym->used = 1;
1053 if (common_root->n.common->binding_label)
1055 gsym = gfc_find_gsymbol (gfc_gsym_root,
1056 common_root->n.common->binding_label);
1057 if (gsym && gsym->type != GSYM_COMMON)
1059 gfc_error ("COMMON block at %L with binding label %s uses the same "
1060 "global identifier as entity at %L",
1061 &common_root->n.common->where,
1062 common_root->n.common->binding_label, &gsym->where);
1063 return;
1065 if (!gsym)
1067 gsym = gfc_get_gsymbol (common_root->n.common->binding_label);
1068 gsym->type = GSYM_COMMON;
1069 gsym->where = common_root->n.common->where;
1070 gsym->defined = 1;
1072 gsym->used = 1;
1075 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1076 if (sym == NULL)
1077 return;
1079 if (sym->attr.flavor == FL_PARAMETER)
1080 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1081 sym->name, &common_root->n.common->where, &sym->declared_at);
1083 if (sym->attr.external)
1084 gfc_error ("COMMON block %qs at %L can not have the EXTERNAL attribute",
1085 sym->name, &common_root->n.common->where);
1087 if (sym->attr.intrinsic)
1088 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1089 sym->name, &common_root->n.common->where);
1090 else if (sym->attr.result
1091 || gfc_is_function_return_value (sym, gfc_current_ns))
1092 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1093 "that is also a function result", sym->name,
1094 &common_root->n.common->where);
1095 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1096 && sym->attr.proc != PROC_ST_FUNCTION)
1097 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1098 "that is also a global procedure", sym->name,
1099 &common_root->n.common->where);
1103 /* Resolve contained function types. Because contained functions can call one
1104 another, they have to be worked out before any of the contained procedures
1105 can be resolved.
1107 The good news is that if a function doesn't already have a type, the only
1108 way it can get one is through an IMPLICIT type or a RESULT variable, because
1109 by definition contained functions are contained namespace they're contained
1110 in, not in a sibling or parent namespace. */
1112 static void
1113 resolve_contained_functions (gfc_namespace *ns)
1115 gfc_namespace *child;
1116 gfc_entry_list *el;
1118 resolve_formal_arglists (ns);
1120 for (child = ns->contained; child; child = child->sibling)
1122 /* Resolve alternate entry points first. */
1123 resolve_entries (child);
1125 /* Then check function return types. */
1126 resolve_contained_fntype (child->proc_name, child);
1127 for (el = child->entries; el; el = el->next)
1128 resolve_contained_fntype (el->sym, child);
1134 /* A Parameterized Derived Type constructor must contain values for
1135 the PDT KIND parameters or they must have a default initializer.
1136 Go through the constructor picking out the KIND expressions,
1137 storing them in 'param_list' and then call gfc_get_pdt_instance
1138 to obtain the PDT instance. */
1140 static gfc_actual_arglist *param_list, *param_tail, *param;
1142 static bool
1143 get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
1145 param = gfc_get_actual_arglist ();
1146 if (!param_list)
1147 param_list = param_tail = param;
1148 else
1150 param_tail->next = param;
1151 param_tail = param_tail->next;
1154 param_tail->name = c->name;
1155 if (expr)
1156 param_tail->expr = gfc_copy_expr (expr);
1157 else if (c->initializer)
1158 param_tail->expr = gfc_copy_expr (c->initializer);
1159 else
1161 param_tail->spec_type = SPEC_ASSUMED;
1162 if (c->attr.pdt_kind)
1164 gfc_error ("The KIND parameter %qs in the PDT constructor "
1165 "at %C has no value", param->name);
1166 return false;
1170 return true;
1173 static bool
1174 get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
1175 gfc_symbol *derived)
1177 gfc_constructor *cons;
1178 gfc_component *comp;
1179 bool t = true;
1181 if (expr && expr->expr_type == EXPR_STRUCTURE)
1182 cons = gfc_constructor_first (expr->value.constructor);
1183 else if (constr)
1184 cons = *constr;
1185 gcc_assert (cons);
1187 comp = derived->components;
1189 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1191 if (cons->expr
1192 && cons->expr->expr_type == EXPR_STRUCTURE
1193 && comp->ts.type == BT_DERIVED)
1195 t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
1196 if (!t)
1197 return t;
1199 else if (comp->ts.type == BT_DERIVED)
1201 t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
1202 if (!t)
1203 return t;
1205 else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
1206 && derived->attr.pdt_template)
1208 t = get_pdt_spec_expr (comp, cons->expr);
1209 if (!t)
1210 return t;
1213 return t;
1217 static bool resolve_fl_derived0 (gfc_symbol *sym);
1218 static bool resolve_fl_struct (gfc_symbol *sym);
1221 /* Resolve all of the elements of a structure constructor and make sure that
1222 the types are correct. The 'init' flag indicates that the given
1223 constructor is an initializer. */
1225 static bool
1226 resolve_structure_cons (gfc_expr *expr, int init)
1228 gfc_constructor *cons;
1229 gfc_component *comp;
1230 bool t;
1231 symbol_attribute a;
1233 t = true;
1235 if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1237 if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1238 resolve_fl_derived0 (expr->ts.u.derived);
1239 else
1240 resolve_fl_struct (expr->ts.u.derived);
1242 /* If this is a Parameterized Derived Type template, find the
1243 instance corresponding to the PDT kind parameters. */
1244 if (expr->ts.u.derived->attr.pdt_template)
1246 param_list = NULL;
1247 t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
1248 if (!t)
1249 return t;
1250 gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
1252 expr->param_list = gfc_copy_actual_arglist (param_list);
1254 if (param_list)
1255 gfc_free_actual_arglist (param_list);
1257 if (!expr->ts.u.derived->attr.pdt_type)
1258 return false;
1262 cons = gfc_constructor_first (expr->value.constructor);
1264 /* A constructor may have references if it is the result of substituting a
1265 parameter variable. In this case we just pull out the component we
1266 want. */
1267 if (expr->ref)
1268 comp = expr->ref->u.c.sym->components;
1269 else
1270 comp = expr->ts.u.derived->components;
1272 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1274 int rank;
1276 if (!cons->expr)
1277 continue;
1279 /* Unions use an EXPR_NULL contrived expression to tell the translation
1280 phase to generate an initializer of the appropriate length.
1281 Ignore it here. */
1282 if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1283 continue;
1285 if (!gfc_resolve_expr (cons->expr))
1287 t = false;
1288 continue;
1291 rank = comp->as ? comp->as->rank : 0;
1292 if (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->as)
1293 rank = CLASS_DATA (comp)->as->rank;
1295 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1296 && (comp->attr.allocatable || cons->expr->rank))
1298 gfc_error ("The rank of the element in the structure "
1299 "constructor at %L does not match that of the "
1300 "component (%d/%d)", &cons->expr->where,
1301 cons->expr->rank, rank);
1302 t = false;
1305 /* If we don't have the right type, try to convert it. */
1307 if (!comp->attr.proc_pointer &&
1308 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1310 if (strcmp (comp->name, "_extends") == 0)
1312 /* Can afford to be brutal with the _extends initializer.
1313 The derived type can get lost because it is PRIVATE
1314 but it is not usage constrained by the standard. */
1315 cons->expr->ts = comp->ts;
1317 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1319 gfc_error ("The element in the structure constructor at %L, "
1320 "for pointer component %qs, is %s but should be %s",
1321 &cons->expr->where, comp->name,
1322 gfc_basic_typename (cons->expr->ts.type),
1323 gfc_basic_typename (comp->ts.type));
1324 t = false;
1326 else
1328 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1329 if (t)
1330 t = t2;
1334 /* For strings, the length of the constructor should be the same as
1335 the one of the structure, ensure this if the lengths are known at
1336 compile time and when we are dealing with PARAMETER or structure
1337 constructors. */
1338 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1339 && comp->ts.u.cl->length
1340 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1341 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1342 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1343 && cons->expr->rank != 0
1344 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1345 comp->ts.u.cl->length->value.integer) != 0)
1347 if (cons->expr->expr_type == EXPR_VARIABLE
1348 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1350 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1351 to make use of the gfc_resolve_character_array_constructor
1352 machinery. The expression is later simplified away to
1353 an array of string literals. */
1354 gfc_expr *para = cons->expr;
1355 cons->expr = gfc_get_expr ();
1356 cons->expr->ts = para->ts;
1357 cons->expr->where = para->where;
1358 cons->expr->expr_type = EXPR_ARRAY;
1359 cons->expr->rank = para->rank;
1360 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1361 gfc_constructor_append_expr (&cons->expr->value.constructor,
1362 para, &cons->expr->where);
1365 if (cons->expr->expr_type == EXPR_ARRAY)
1367 /* Rely on the cleanup of the namespace to deal correctly with
1368 the old charlen. (There was a block here that attempted to
1369 remove the charlen but broke the chain in so doing.) */
1370 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1371 cons->expr->ts.u.cl->length_from_typespec = true;
1372 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1373 gfc_resolve_character_array_constructor (cons->expr);
1377 if (cons->expr->expr_type == EXPR_NULL
1378 && !(comp->attr.pointer || comp->attr.allocatable
1379 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1380 || (comp->ts.type == BT_CLASS
1381 && (CLASS_DATA (comp)->attr.class_pointer
1382 || CLASS_DATA (comp)->attr.allocatable))))
1384 t = false;
1385 gfc_error ("The NULL in the structure constructor at %L is "
1386 "being applied to component %qs, which is neither "
1387 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1388 comp->name);
1391 if (comp->attr.proc_pointer && comp->ts.interface)
1393 /* Check procedure pointer interface. */
1394 gfc_symbol *s2 = NULL;
1395 gfc_component *c2;
1396 const char *name;
1397 char err[200];
1399 c2 = gfc_get_proc_ptr_comp (cons->expr);
1400 if (c2)
1402 s2 = c2->ts.interface;
1403 name = c2->name;
1405 else if (cons->expr->expr_type == EXPR_FUNCTION)
1407 s2 = cons->expr->symtree->n.sym->result;
1408 name = cons->expr->symtree->n.sym->result->name;
1410 else if (cons->expr->expr_type != EXPR_NULL)
1412 s2 = cons->expr->symtree->n.sym;
1413 name = cons->expr->symtree->n.sym->name;
1416 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1417 err, sizeof (err), NULL, NULL))
1419 gfc_error_opt (OPT_Wargument_mismatch,
1420 "Interface mismatch for procedure-pointer "
1421 "component %qs in structure constructor at %L:"
1422 " %s", comp->name, &cons->expr->where, err);
1423 return false;
1427 if (!comp->attr.pointer || comp->attr.proc_pointer
1428 || cons->expr->expr_type == EXPR_NULL)
1429 continue;
1431 a = gfc_expr_attr (cons->expr);
1433 if (!a.pointer && !a.target)
1435 t = false;
1436 gfc_error ("The element in the structure constructor at %L, "
1437 "for pointer component %qs should be a POINTER or "
1438 "a TARGET", &cons->expr->where, comp->name);
1441 if (init)
1443 /* F08:C461. Additional checks for pointer initialization. */
1444 if (a.allocatable)
1446 t = false;
1447 gfc_error ("Pointer initialization target at %L "
1448 "must not be ALLOCATABLE", &cons->expr->where);
1450 if (!a.save)
1452 t = false;
1453 gfc_error ("Pointer initialization target at %L "
1454 "must have the SAVE attribute", &cons->expr->where);
1458 /* F2003, C1272 (3). */
1459 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1460 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1461 || gfc_is_coindexed (cons->expr));
1462 if (impure && gfc_pure (NULL))
1464 t = false;
1465 gfc_error ("Invalid expression in the structure constructor for "
1466 "pointer component %qs at %L in PURE procedure",
1467 comp->name, &cons->expr->where);
1470 if (impure)
1471 gfc_unset_implicit_pure (NULL);
1474 return t;
1478 /****************** Expression name resolution ******************/
1480 /* Returns 0 if a symbol was not declared with a type or
1481 attribute declaration statement, nonzero otherwise. */
1483 static int
1484 was_declared (gfc_symbol *sym)
1486 symbol_attribute a;
1488 a = sym->attr;
1490 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1491 return 1;
1493 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1494 || a.optional || a.pointer || a.save || a.target || a.volatile_
1495 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1496 || a.asynchronous || a.codimension)
1497 return 1;
1499 return 0;
1503 /* Determine if a symbol is generic or not. */
1505 static int
1506 generic_sym (gfc_symbol *sym)
1508 gfc_symbol *s;
1510 if (sym->attr.generic ||
1511 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1512 return 1;
1514 if (was_declared (sym) || sym->ns->parent == NULL)
1515 return 0;
1517 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1519 if (s != NULL)
1521 if (s == sym)
1522 return 0;
1523 else
1524 return generic_sym (s);
1527 return 0;
1531 /* Determine if a symbol is specific or not. */
1533 static int
1534 specific_sym (gfc_symbol *sym)
1536 gfc_symbol *s;
1538 if (sym->attr.if_source == IFSRC_IFBODY
1539 || sym->attr.proc == PROC_MODULE
1540 || sym->attr.proc == PROC_INTERNAL
1541 || sym->attr.proc == PROC_ST_FUNCTION
1542 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1543 || sym->attr.external)
1544 return 1;
1546 if (was_declared (sym) || sym->ns->parent == NULL)
1547 return 0;
1549 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1551 return (s == NULL) ? 0 : specific_sym (s);
1555 /* Figure out if the procedure is specific, generic or unknown. */
1557 enum proc_type
1558 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1560 static proc_type
1561 procedure_kind (gfc_symbol *sym)
1563 if (generic_sym (sym))
1564 return PTYPE_GENERIC;
1566 if (specific_sym (sym))
1567 return PTYPE_SPECIFIC;
1569 return PTYPE_UNKNOWN;
1572 /* Check references to assumed size arrays. The flag need_full_assumed_size
1573 is nonzero when matching actual arguments. */
1575 static int need_full_assumed_size = 0;
1577 static bool
1578 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1580 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1581 return false;
1583 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1584 What should it be? */
1585 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1586 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1587 && (e->ref->u.ar.type == AR_FULL))
1589 gfc_error ("The upper bound in the last dimension must "
1590 "appear in the reference to the assumed size "
1591 "array %qs at %L", sym->name, &e->where);
1592 return true;
1594 return false;
1598 /* Look for bad assumed size array references in argument expressions
1599 of elemental and array valued intrinsic procedures. Since this is
1600 called from procedure resolution functions, it only recurses at
1601 operators. */
1603 static bool
1604 resolve_assumed_size_actual (gfc_expr *e)
1606 if (e == NULL)
1607 return false;
1609 switch (e->expr_type)
1611 case EXPR_VARIABLE:
1612 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1613 return true;
1614 break;
1616 case EXPR_OP:
1617 if (resolve_assumed_size_actual (e->value.op.op1)
1618 || resolve_assumed_size_actual (e->value.op.op2))
1619 return true;
1620 break;
1622 default:
1623 break;
1625 return false;
1629 /* Check a generic procedure, passed as an actual argument, to see if
1630 there is a matching specific name. If none, it is an error, and if
1631 more than one, the reference is ambiguous. */
1632 static int
1633 count_specific_procs (gfc_expr *e)
1635 int n;
1636 gfc_interface *p;
1637 gfc_symbol *sym;
1639 n = 0;
1640 sym = e->symtree->n.sym;
1642 for (p = sym->generic; p; p = p->next)
1643 if (strcmp (sym->name, p->sym->name) == 0)
1645 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1646 sym->name);
1647 n++;
1650 if (n > 1)
1651 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1652 &e->where);
1654 if (n == 0)
1655 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1656 "argument at %L", sym->name, &e->where);
1658 return n;
1662 /* See if a call to sym could possibly be a not allowed RECURSION because of
1663 a missing RECURSIVE declaration. This means that either sym is the current
1664 context itself, or sym is the parent of a contained procedure calling its
1665 non-RECURSIVE containing procedure.
1666 This also works if sym is an ENTRY. */
1668 static bool
1669 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1671 gfc_symbol* proc_sym;
1672 gfc_symbol* context_proc;
1673 gfc_namespace* real_context;
1675 if (sym->attr.flavor == FL_PROGRAM
1676 || gfc_fl_struct (sym->attr.flavor))
1677 return false;
1679 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1681 /* If we've got an ENTRY, find real procedure. */
1682 if (sym->attr.entry && sym->ns->entries)
1683 proc_sym = sym->ns->entries->sym;
1684 else
1685 proc_sym = sym;
1687 /* If sym is RECURSIVE, all is well of course. */
1688 if (proc_sym->attr.recursive || flag_recursive)
1689 return false;
1691 /* Find the context procedure's "real" symbol if it has entries.
1692 We look for a procedure symbol, so recurse on the parents if we don't
1693 find one (like in case of a BLOCK construct). */
1694 for (real_context = context; ; real_context = real_context->parent)
1696 /* We should find something, eventually! */
1697 gcc_assert (real_context);
1699 context_proc = (real_context->entries ? real_context->entries->sym
1700 : real_context->proc_name);
1702 /* In some special cases, there may not be a proc_name, like for this
1703 invalid code:
1704 real(bad_kind()) function foo () ...
1705 when checking the call to bad_kind ().
1706 In these cases, we simply return here and assume that the
1707 call is ok. */
1708 if (!context_proc)
1709 return false;
1711 if (context_proc->attr.flavor != FL_LABEL)
1712 break;
1715 /* A call from sym's body to itself is recursion, of course. */
1716 if (context_proc == proc_sym)
1717 return true;
1719 /* The same is true if context is a contained procedure and sym the
1720 containing one. */
1721 if (context_proc->attr.contained)
1723 gfc_symbol* parent_proc;
1725 gcc_assert (context->parent);
1726 parent_proc = (context->parent->entries ? context->parent->entries->sym
1727 : context->parent->proc_name);
1729 if (parent_proc == proc_sym)
1730 return true;
1733 return false;
1737 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1738 its typespec and formal argument list. */
1740 bool
1741 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1743 gfc_intrinsic_sym* isym = NULL;
1744 const char* symstd;
1746 if (sym->formal)
1747 return true;
1749 /* Already resolved. */
1750 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1751 return true;
1753 /* We already know this one is an intrinsic, so we don't call
1754 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1755 gfc_find_subroutine directly to check whether it is a function or
1756 subroutine. */
1758 if (sym->intmod_sym_id && sym->attr.subroutine)
1760 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1761 isym = gfc_intrinsic_subroutine_by_id (id);
1763 else if (sym->intmod_sym_id)
1765 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1766 isym = gfc_intrinsic_function_by_id (id);
1768 else if (!sym->attr.subroutine)
1769 isym = gfc_find_function (sym->name);
1771 if (isym && !sym->attr.subroutine)
1773 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1774 && !sym->attr.implicit_type)
1775 gfc_warning (OPT_Wsurprising,
1776 "Type specified for intrinsic function %qs at %L is"
1777 " ignored", sym->name, &sym->declared_at);
1779 if (!sym->attr.function &&
1780 !gfc_add_function(&sym->attr, sym->name, loc))
1781 return false;
1783 sym->ts = isym->ts;
1785 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1787 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1789 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1790 " specifier", sym->name, &sym->declared_at);
1791 return false;
1794 if (!sym->attr.subroutine &&
1795 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1796 return false;
1798 else
1800 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1801 &sym->declared_at);
1802 return false;
1805 gfc_copy_formal_args_intr (sym, isym, NULL);
1807 sym->attr.pure = isym->pure;
1808 sym->attr.elemental = isym->elemental;
1810 /* Check it is actually available in the standard settings. */
1811 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1813 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1814 "available in the current standard settings but %s. Use "
1815 "an appropriate %<-std=*%> option or enable "
1816 "%<-fall-intrinsics%> in order to use it.",
1817 sym->name, &sym->declared_at, symstd);
1818 return false;
1821 return true;
1825 /* Resolve a procedure expression, like passing it to a called procedure or as
1826 RHS for a procedure pointer assignment. */
1828 static bool
1829 resolve_procedure_expression (gfc_expr* expr)
1831 gfc_symbol* sym;
1833 if (expr->expr_type != EXPR_VARIABLE)
1834 return true;
1835 gcc_assert (expr->symtree);
1837 sym = expr->symtree->n.sym;
1839 if (sym->attr.intrinsic)
1840 gfc_resolve_intrinsic (sym, &expr->where);
1842 if (sym->attr.flavor != FL_PROCEDURE
1843 || (sym->attr.function && sym->result == sym))
1844 return true;
1846 /* A non-RECURSIVE procedure that is used as procedure expression within its
1847 own body is in danger of being called recursively. */
1848 if (is_illegal_recursion (sym, gfc_current_ns))
1849 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1850 " itself recursively. Declare it RECURSIVE or use"
1851 " %<-frecursive%>", sym->name, &expr->where);
1853 return true;
1857 /* Resolve an actual argument list. Most of the time, this is just
1858 resolving the expressions in the list.
1859 The exception is that we sometimes have to decide whether arguments
1860 that look like procedure arguments are really simple variable
1861 references. */
1863 static bool
1864 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1865 bool no_formal_args)
1867 gfc_symbol *sym;
1868 gfc_symtree *parent_st;
1869 gfc_expr *e;
1870 gfc_component *comp;
1871 int save_need_full_assumed_size;
1872 bool return_value = false;
1873 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1875 actual_arg = true;
1876 first_actual_arg = true;
1878 for (; arg; arg = arg->next)
1880 e = arg->expr;
1881 if (e == NULL)
1883 /* Check the label is a valid branching target. */
1884 if (arg->label)
1886 if (arg->label->defined == ST_LABEL_UNKNOWN)
1888 gfc_error ("Label %d referenced at %L is never defined",
1889 arg->label->value, &arg->label->where);
1890 goto cleanup;
1893 first_actual_arg = false;
1894 continue;
1897 if (e->expr_type == EXPR_VARIABLE
1898 && e->symtree->n.sym->attr.generic
1899 && no_formal_args
1900 && count_specific_procs (e) != 1)
1901 goto cleanup;
1903 if (e->ts.type != BT_PROCEDURE)
1905 save_need_full_assumed_size = need_full_assumed_size;
1906 if (e->expr_type != EXPR_VARIABLE)
1907 need_full_assumed_size = 0;
1908 if (!gfc_resolve_expr (e))
1909 goto cleanup;
1910 need_full_assumed_size = save_need_full_assumed_size;
1911 goto argument_list;
1914 /* See if the expression node should really be a variable reference. */
1916 sym = e->symtree->n.sym;
1918 if (sym->attr.flavor == FL_PROCEDURE
1919 || sym->attr.intrinsic
1920 || sym->attr.external)
1922 int actual_ok;
1924 /* If a procedure is not already determined to be something else
1925 check if it is intrinsic. */
1926 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1927 sym->attr.intrinsic = 1;
1929 if (sym->attr.proc == PROC_ST_FUNCTION)
1931 gfc_error ("Statement function %qs at %L is not allowed as an "
1932 "actual argument", sym->name, &e->where);
1935 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1936 sym->attr.subroutine);
1937 if (sym->attr.intrinsic && actual_ok == 0)
1939 gfc_error ("Intrinsic %qs at %L is not allowed as an "
1940 "actual argument", sym->name, &e->where);
1943 if (sym->attr.contained && !sym->attr.use_assoc
1944 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1946 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
1947 " used as actual argument at %L",
1948 sym->name, &e->where))
1949 goto cleanup;
1952 if (sym->attr.elemental && !sym->attr.intrinsic)
1954 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
1955 "allowed as an actual argument at %L", sym->name,
1956 &e->where);
1959 /* Check if a generic interface has a specific procedure
1960 with the same name before emitting an error. */
1961 if (sym->attr.generic && count_specific_procs (e) != 1)
1962 goto cleanup;
1964 /* Just in case a specific was found for the expression. */
1965 sym = e->symtree->n.sym;
1967 /* If the symbol is the function that names the current (or
1968 parent) scope, then we really have a variable reference. */
1970 if (gfc_is_function_return_value (sym, sym->ns))
1971 goto got_variable;
1973 /* If all else fails, see if we have a specific intrinsic. */
1974 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1976 gfc_intrinsic_sym *isym;
1978 isym = gfc_find_function (sym->name);
1979 if (isym == NULL || !isym->specific)
1981 gfc_error ("Unable to find a specific INTRINSIC procedure "
1982 "for the reference %qs at %L", sym->name,
1983 &e->where);
1984 goto cleanup;
1986 sym->ts = isym->ts;
1987 sym->attr.intrinsic = 1;
1988 sym->attr.function = 1;
1991 if (!gfc_resolve_expr (e))
1992 goto cleanup;
1993 goto argument_list;
1996 /* See if the name is a module procedure in a parent unit. */
1998 if (was_declared (sym) || sym->ns->parent == NULL)
1999 goto got_variable;
2001 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
2003 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
2004 goto cleanup;
2007 if (parent_st == NULL)
2008 goto got_variable;
2010 sym = parent_st->n.sym;
2011 e->symtree = parent_st; /* Point to the right thing. */
2013 if (sym->attr.flavor == FL_PROCEDURE
2014 || sym->attr.intrinsic
2015 || sym->attr.external)
2017 if (!gfc_resolve_expr (e))
2018 goto cleanup;
2019 goto argument_list;
2022 got_variable:
2023 e->expr_type = EXPR_VARIABLE;
2024 e->ts = sym->ts;
2025 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2026 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2027 && CLASS_DATA (sym)->as))
2029 e->rank = sym->ts.type == BT_CLASS
2030 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
2031 e->ref = gfc_get_ref ();
2032 e->ref->type = REF_ARRAY;
2033 e->ref->u.ar.type = AR_FULL;
2034 e->ref->u.ar.as = sym->ts.type == BT_CLASS
2035 ? CLASS_DATA (sym)->as : sym->as;
2038 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2039 primary.c (match_actual_arg). If above code determines that it
2040 is a variable instead, it needs to be resolved as it was not
2041 done at the beginning of this function. */
2042 save_need_full_assumed_size = need_full_assumed_size;
2043 if (e->expr_type != EXPR_VARIABLE)
2044 need_full_assumed_size = 0;
2045 if (!gfc_resolve_expr (e))
2046 goto cleanup;
2047 need_full_assumed_size = save_need_full_assumed_size;
2049 argument_list:
2050 /* Check argument list functions %VAL, %LOC and %REF. There is
2051 nothing to do for %REF. */
2052 if (arg->name && arg->name[0] == '%')
2054 if (strncmp ("%VAL", arg->name, 4) == 0)
2056 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
2058 gfc_error ("By-value argument at %L is not of numeric "
2059 "type", &e->where);
2060 goto cleanup;
2063 if (e->rank)
2065 gfc_error ("By-value argument at %L cannot be an array or "
2066 "an array section", &e->where);
2067 goto cleanup;
2070 /* Intrinsics are still PROC_UNKNOWN here. However,
2071 since same file external procedures are not resolvable
2072 in gfortran, it is a good deal easier to leave them to
2073 intrinsic.c. */
2074 if (ptype != PROC_UNKNOWN
2075 && ptype != PROC_DUMMY
2076 && ptype != PROC_EXTERNAL
2077 && ptype != PROC_MODULE)
2079 gfc_error ("By-value argument at %L is not allowed "
2080 "in this context", &e->where);
2081 goto cleanup;
2085 /* Statement functions have already been excluded above. */
2086 else if (strncmp ("%LOC", arg->name, 4) == 0
2087 && e->ts.type == BT_PROCEDURE)
2089 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
2091 gfc_error ("Passing internal procedure at %L by location "
2092 "not allowed", &e->where);
2093 goto cleanup;
2098 comp = gfc_get_proc_ptr_comp(e);
2099 if (e->expr_type == EXPR_VARIABLE
2100 && comp && comp->attr.elemental)
2102 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2103 "allowed as an actual argument at %L", comp->name,
2104 &e->where);
2107 /* Fortran 2008, C1237. */
2108 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2109 && gfc_has_ultimate_pointer (e))
2111 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2112 "component", &e->where);
2113 goto cleanup;
2116 first_actual_arg = false;
2119 return_value = true;
2121 cleanup:
2122 actual_arg = actual_arg_sav;
2123 first_actual_arg = first_actual_arg_sav;
2125 return return_value;
2129 /* Do the checks of the actual argument list that are specific to elemental
2130 procedures. If called with c == NULL, we have a function, otherwise if
2131 expr == NULL, we have a subroutine. */
2133 static bool
2134 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2136 gfc_actual_arglist *arg0;
2137 gfc_actual_arglist *arg;
2138 gfc_symbol *esym = NULL;
2139 gfc_intrinsic_sym *isym = NULL;
2140 gfc_expr *e = NULL;
2141 gfc_intrinsic_arg *iformal = NULL;
2142 gfc_formal_arglist *eformal = NULL;
2143 bool formal_optional = false;
2144 bool set_by_optional = false;
2145 int i;
2146 int rank = 0;
2148 /* Is this an elemental procedure? */
2149 if (expr && expr->value.function.actual != NULL)
2151 if (expr->value.function.esym != NULL
2152 && expr->value.function.esym->attr.elemental)
2154 arg0 = expr->value.function.actual;
2155 esym = expr->value.function.esym;
2157 else if (expr->value.function.isym != NULL
2158 && expr->value.function.isym->elemental)
2160 arg0 = expr->value.function.actual;
2161 isym = expr->value.function.isym;
2163 else
2164 return true;
2166 else if (c && c->ext.actual != NULL)
2168 arg0 = c->ext.actual;
2170 if (c->resolved_sym)
2171 esym = c->resolved_sym;
2172 else
2173 esym = c->symtree->n.sym;
2174 gcc_assert (esym);
2176 if (!esym->attr.elemental)
2177 return true;
2179 else
2180 return true;
2182 /* The rank of an elemental is the rank of its array argument(s). */
2183 for (arg = arg0; arg; arg = arg->next)
2185 if (arg->expr != NULL && arg->expr->rank != 0)
2187 rank = arg->expr->rank;
2188 if (arg->expr->expr_type == EXPR_VARIABLE
2189 && arg->expr->symtree->n.sym->attr.optional)
2190 set_by_optional = true;
2192 /* Function specific; set the result rank and shape. */
2193 if (expr)
2195 expr->rank = rank;
2196 if (!expr->shape && arg->expr->shape)
2198 expr->shape = gfc_get_shape (rank);
2199 for (i = 0; i < rank; i++)
2200 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2203 break;
2207 /* If it is an array, it shall not be supplied as an actual argument
2208 to an elemental procedure unless an array of the same rank is supplied
2209 as an actual argument corresponding to a nonoptional dummy argument of
2210 that elemental procedure(12.4.1.5). */
2211 formal_optional = false;
2212 if (isym)
2213 iformal = isym->formal;
2214 else
2215 eformal = esym->formal;
2217 for (arg = arg0; arg; arg = arg->next)
2219 if (eformal)
2221 if (eformal->sym && eformal->sym->attr.optional)
2222 formal_optional = true;
2223 eformal = eformal->next;
2225 else if (isym && iformal)
2227 if (iformal->optional)
2228 formal_optional = true;
2229 iformal = iformal->next;
2231 else if (isym)
2232 formal_optional = true;
2234 if (pedantic && arg->expr != NULL
2235 && arg->expr->expr_type == EXPR_VARIABLE
2236 && arg->expr->symtree->n.sym->attr.optional
2237 && formal_optional
2238 && arg->expr->rank
2239 && (set_by_optional || arg->expr->rank != rank)
2240 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2242 gfc_warning (OPT_Wpedantic,
2243 "%qs at %L is an array and OPTIONAL; IF IT IS "
2244 "MISSING, it cannot be the actual argument of an "
2245 "ELEMENTAL procedure unless there is a non-optional "
2246 "argument with the same rank (12.4.1.5)",
2247 arg->expr->symtree->n.sym->name, &arg->expr->where);
2251 for (arg = arg0; arg; arg = arg->next)
2253 if (arg->expr == NULL || arg->expr->rank == 0)
2254 continue;
2256 /* Being elemental, the last upper bound of an assumed size array
2257 argument must be present. */
2258 if (resolve_assumed_size_actual (arg->expr))
2259 return false;
2261 /* Elemental procedure's array actual arguments must conform. */
2262 if (e != NULL)
2264 if (!gfc_check_conformance (arg->expr, e, "elemental procedure"))
2265 return false;
2267 else
2268 e = arg->expr;
2271 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2272 is an array, the intent inout/out variable needs to be also an array. */
2273 if (rank > 0 && esym && expr == NULL)
2274 for (eformal = esym->formal, arg = arg0; arg && eformal;
2275 arg = arg->next, eformal = eformal->next)
2276 if ((eformal->sym->attr.intent == INTENT_OUT
2277 || eformal->sym->attr.intent == INTENT_INOUT)
2278 && arg->expr && arg->expr->rank == 0)
2280 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2281 "ELEMENTAL subroutine %qs is a scalar, but another "
2282 "actual argument is an array", &arg->expr->where,
2283 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2284 : "INOUT", eformal->sym->name, esym->name);
2285 return false;
2287 return true;
2291 /* This function does the checking of references to global procedures
2292 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2293 77 and 95 standards. It checks for a gsymbol for the name, making
2294 one if it does not already exist. If it already exists, then the
2295 reference being resolved must correspond to the type of gsymbol.
2296 Otherwise, the new symbol is equipped with the attributes of the
2297 reference. The corresponding code that is called in creating
2298 global entities is parse.c.
2300 In addition, for all but -std=legacy, the gsymbols are used to
2301 check the interfaces of external procedures from the same file.
2302 The namespace of the gsymbol is resolved and then, once this is
2303 done the interface is checked. */
2306 static bool
2307 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2309 if (!gsym_ns->proc_name->attr.recursive)
2310 return true;
2312 if (sym->ns == gsym_ns)
2313 return false;
2315 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2316 return false;
2318 return true;
2321 static bool
2322 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2324 if (gsym_ns->entries)
2326 gfc_entry_list *entry = gsym_ns->entries;
2328 for (; entry; entry = entry->next)
2330 if (strcmp (sym->name, entry->sym->name) == 0)
2332 if (strcmp (gsym_ns->proc_name->name,
2333 sym->ns->proc_name->name) == 0)
2334 return false;
2336 if (sym->ns->parent
2337 && strcmp (gsym_ns->proc_name->name,
2338 sym->ns->parent->proc_name->name) == 0)
2339 return false;
2343 return true;
2347 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2349 bool
2350 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2352 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2354 for ( ; arg; arg = arg->next)
2356 if (!arg->sym)
2357 continue;
2359 if (arg->sym->attr.allocatable) /* (2a) */
2361 strncpy (errmsg, _("allocatable argument"), err_len);
2362 return true;
2364 else if (arg->sym->attr.asynchronous)
2366 strncpy (errmsg, _("asynchronous argument"), err_len);
2367 return true;
2369 else if (arg->sym->attr.optional)
2371 strncpy (errmsg, _("optional argument"), err_len);
2372 return true;
2374 else if (arg->sym->attr.pointer)
2376 strncpy (errmsg, _("pointer argument"), err_len);
2377 return true;
2379 else if (arg->sym->attr.target)
2381 strncpy (errmsg, _("target argument"), err_len);
2382 return true;
2384 else if (arg->sym->attr.value)
2386 strncpy (errmsg, _("value argument"), err_len);
2387 return true;
2389 else if (arg->sym->attr.volatile_)
2391 strncpy (errmsg, _("volatile argument"), err_len);
2392 return true;
2394 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2396 strncpy (errmsg, _("assumed-shape argument"), err_len);
2397 return true;
2399 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2401 strncpy (errmsg, _("assumed-rank argument"), err_len);
2402 return true;
2404 else if (arg->sym->attr.codimension) /* (2c) */
2406 strncpy (errmsg, _("coarray argument"), err_len);
2407 return true;
2409 else if (false) /* (2d) TODO: parametrized derived type */
2411 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2412 return true;
2414 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2416 strncpy (errmsg, _("polymorphic argument"), err_len);
2417 return true;
2419 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2421 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2422 return true;
2424 else if (arg->sym->ts.type == BT_ASSUMED)
2426 /* As assumed-type is unlimited polymorphic (cf. above).
2427 See also TS 29113, Note 6.1. */
2428 strncpy (errmsg, _("assumed-type argument"), err_len);
2429 return true;
2433 if (sym->attr.function)
2435 gfc_symbol *res = sym->result ? sym->result : sym;
2437 if (res->attr.dimension) /* (3a) */
2439 strncpy (errmsg, _("array result"), err_len);
2440 return true;
2442 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2444 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2445 return true;
2447 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2448 && res->ts.u.cl->length
2449 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2451 strncpy (errmsg, _("result with non-constant character length"), err_len);
2452 return true;
2456 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2458 strncpy (errmsg, _("elemental procedure"), err_len);
2459 return true;
2461 else if (sym->attr.is_bind_c) /* (5) */
2463 strncpy (errmsg, _("bind(c) procedure"), err_len);
2464 return true;
2467 return false;
2471 static void
2472 resolve_global_procedure (gfc_symbol *sym, locus *where,
2473 gfc_actual_arglist **actual, int sub)
2475 gfc_gsymbol * gsym;
2476 gfc_namespace *ns;
2477 enum gfc_symbol_type type;
2478 char reason[200];
2480 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2482 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name);
2484 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2485 gfc_global_used (gsym, where);
2487 if ((sym->attr.if_source == IFSRC_UNKNOWN
2488 || sym->attr.if_source == IFSRC_IFBODY)
2489 && gsym->type != GSYM_UNKNOWN
2490 && !gsym->binding_label
2491 && gsym->ns
2492 && gsym->ns->resolved != -1
2493 && gsym->ns->proc_name
2494 && not_in_recursive (sym, gsym->ns)
2495 && not_entry_self_reference (sym, gsym->ns))
2497 gfc_symbol *def_sym;
2499 /* Resolve the gsymbol namespace if needed. */
2500 if (!gsym->ns->resolved)
2502 gfc_dt_list *old_dt_list;
2504 /* Stash away derived types so that the backend_decls do not
2505 get mixed up. */
2506 old_dt_list = gfc_derived_types;
2507 gfc_derived_types = NULL;
2509 gfc_resolve (gsym->ns);
2511 /* Store the new derived types with the global namespace. */
2512 if (gfc_derived_types)
2513 gsym->ns->derived_types = gfc_derived_types;
2515 /* Restore the derived types of this namespace. */
2516 gfc_derived_types = old_dt_list;
2519 /* Make sure that translation for the gsymbol occurs before
2520 the procedure currently being resolved. */
2521 ns = gfc_global_ns_list;
2522 for (; ns && ns != gsym->ns; ns = ns->sibling)
2524 if (ns->sibling == gsym->ns)
2526 ns->sibling = gsym->ns->sibling;
2527 gsym->ns->sibling = gfc_global_ns_list;
2528 gfc_global_ns_list = gsym->ns;
2529 break;
2533 def_sym = gsym->ns->proc_name;
2535 /* This can happen if a binding name has been specified. */
2536 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2537 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2539 if (def_sym->attr.entry_master)
2541 gfc_entry_list *entry;
2542 for (entry = gsym->ns->entries; entry; entry = entry->next)
2543 if (strcmp (entry->sym->name, sym->name) == 0)
2545 def_sym = entry->sym;
2546 break;
2550 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2552 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2553 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2554 gfc_typename (&def_sym->ts));
2555 goto done;
2558 if (sym->attr.if_source == IFSRC_UNKNOWN
2559 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2561 gfc_error ("Explicit interface required for %qs at %L: %s",
2562 sym->name, &sym->declared_at, reason);
2563 goto done;
2566 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2567 /* Turn erros into warnings with -std=gnu and -std=legacy. */
2568 gfc_errors_to_warnings (true);
2570 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2571 reason, sizeof(reason), NULL, NULL))
2573 gfc_error_opt (OPT_Wargument_mismatch,
2574 "Interface mismatch in global procedure %qs at %L:"
2575 " %s", sym->name, &sym->declared_at, reason);
2576 goto done;
2579 if (!pedantic
2580 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2581 && !(gfc_option.warn_std & GFC_STD_GNU)))
2582 gfc_errors_to_warnings (true);
2584 if (sym->attr.if_source != IFSRC_IFBODY)
2585 gfc_procedure_use (def_sym, actual, where);
2588 done:
2589 gfc_errors_to_warnings (false);
2591 if (gsym->type == GSYM_UNKNOWN)
2593 gsym->type = type;
2594 gsym->where = *where;
2597 gsym->used = 1;
2601 /************* Function resolution *************/
2603 /* Resolve a function call known to be generic.
2604 Section 14.1.2.4.1. */
2606 static match
2607 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2609 gfc_symbol *s;
2611 if (sym->attr.generic)
2613 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2614 if (s != NULL)
2616 expr->value.function.name = s->name;
2617 expr->value.function.esym = s;
2619 if (s->ts.type != BT_UNKNOWN)
2620 expr->ts = s->ts;
2621 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2622 expr->ts = s->result->ts;
2624 if (s->as != NULL)
2625 expr->rank = s->as->rank;
2626 else if (s->result != NULL && s->result->as != NULL)
2627 expr->rank = s->result->as->rank;
2629 gfc_set_sym_referenced (expr->value.function.esym);
2631 return MATCH_YES;
2634 /* TODO: Need to search for elemental references in generic
2635 interface. */
2638 if (sym->attr.intrinsic)
2639 return gfc_intrinsic_func_interface (expr, 0);
2641 return MATCH_NO;
2645 static bool
2646 resolve_generic_f (gfc_expr *expr)
2648 gfc_symbol *sym;
2649 match m;
2650 gfc_interface *intr = NULL;
2652 sym = expr->symtree->n.sym;
2654 for (;;)
2656 m = resolve_generic_f0 (expr, sym);
2657 if (m == MATCH_YES)
2658 return true;
2659 else if (m == MATCH_ERROR)
2660 return false;
2662 generic:
2663 if (!intr)
2664 for (intr = sym->generic; intr; intr = intr->next)
2665 if (gfc_fl_struct (intr->sym->attr.flavor))
2666 break;
2668 if (sym->ns->parent == NULL)
2669 break;
2670 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2672 if (sym == NULL)
2673 break;
2674 if (!generic_sym (sym))
2675 goto generic;
2678 /* Last ditch attempt. See if the reference is to an intrinsic
2679 that possesses a matching interface. 14.1.2.4 */
2680 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2682 if (gfc_init_expr_flag)
2683 gfc_error ("Function %qs in initialization expression at %L "
2684 "must be an intrinsic function",
2685 expr->symtree->n.sym->name, &expr->where);
2686 else
2687 gfc_error ("There is no specific function for the generic %qs "
2688 "at %L", expr->symtree->n.sym->name, &expr->where);
2689 return false;
2692 if (intr)
2694 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2695 NULL, false))
2696 return false;
2697 return resolve_structure_cons (expr, 0);
2700 m = gfc_intrinsic_func_interface (expr, 0);
2701 if (m == MATCH_YES)
2702 return true;
2704 if (m == MATCH_NO)
2705 gfc_error ("Generic function %qs at %L is not consistent with a "
2706 "specific intrinsic interface", expr->symtree->n.sym->name,
2707 &expr->where);
2709 return false;
2713 /* Resolve a function call known to be specific. */
2715 static match
2716 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2718 match m;
2720 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2722 if (sym->attr.dummy)
2724 sym->attr.proc = PROC_DUMMY;
2725 goto found;
2728 sym->attr.proc = PROC_EXTERNAL;
2729 goto found;
2732 if (sym->attr.proc == PROC_MODULE
2733 || sym->attr.proc == PROC_ST_FUNCTION
2734 || sym->attr.proc == PROC_INTERNAL)
2735 goto found;
2737 if (sym->attr.intrinsic)
2739 m = gfc_intrinsic_func_interface (expr, 1);
2740 if (m == MATCH_YES)
2741 return MATCH_YES;
2742 if (m == MATCH_NO)
2743 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2744 "with an intrinsic", sym->name, &expr->where);
2746 return MATCH_ERROR;
2749 return MATCH_NO;
2751 found:
2752 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2754 if (sym->result)
2755 expr->ts = sym->result->ts;
2756 else
2757 expr->ts = sym->ts;
2758 expr->value.function.name = sym->name;
2759 expr->value.function.esym = sym;
2760 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2761 error(s). */
2762 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2763 return MATCH_ERROR;
2764 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2765 expr->rank = CLASS_DATA (sym)->as->rank;
2766 else if (sym->as != NULL)
2767 expr->rank = sym->as->rank;
2769 return MATCH_YES;
2773 static bool
2774 resolve_specific_f (gfc_expr *expr)
2776 gfc_symbol *sym;
2777 match m;
2779 sym = expr->symtree->n.sym;
2781 for (;;)
2783 m = resolve_specific_f0 (sym, expr);
2784 if (m == MATCH_YES)
2785 return true;
2786 if (m == MATCH_ERROR)
2787 return false;
2789 if (sym->ns->parent == NULL)
2790 break;
2792 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2794 if (sym == NULL)
2795 break;
2798 gfc_error ("Unable to resolve the specific function %qs at %L",
2799 expr->symtree->n.sym->name, &expr->where);
2801 return true;
2805 /* Resolve a procedure call not known to be generic nor specific. */
2807 static bool
2808 resolve_unknown_f (gfc_expr *expr)
2810 gfc_symbol *sym;
2811 gfc_typespec *ts;
2813 sym = expr->symtree->n.sym;
2815 if (sym->attr.dummy)
2817 sym->attr.proc = PROC_DUMMY;
2818 expr->value.function.name = sym->name;
2819 goto set_type;
2822 /* See if we have an intrinsic function reference. */
2824 if (gfc_is_intrinsic (sym, 0, expr->where))
2826 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2827 return true;
2828 return false;
2831 /* The reference is to an external name. */
2833 sym->attr.proc = PROC_EXTERNAL;
2834 expr->value.function.name = sym->name;
2835 expr->value.function.esym = expr->symtree->n.sym;
2837 if (sym->as != NULL)
2838 expr->rank = sym->as->rank;
2840 /* Type of the expression is either the type of the symbol or the
2841 default type of the symbol. */
2843 set_type:
2844 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2846 if (sym->ts.type != BT_UNKNOWN)
2847 expr->ts = sym->ts;
2848 else
2850 ts = gfc_get_default_type (sym->name, sym->ns);
2852 if (ts->type == BT_UNKNOWN)
2854 gfc_error ("Function %qs at %L has no IMPLICIT type",
2855 sym->name, &expr->where);
2856 return false;
2858 else
2859 expr->ts = *ts;
2862 return true;
2866 /* Return true, if the symbol is an external procedure. */
2867 static bool
2868 is_external_proc (gfc_symbol *sym)
2870 if (!sym->attr.dummy && !sym->attr.contained
2871 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2872 && sym->attr.proc != PROC_ST_FUNCTION
2873 && !sym->attr.proc_pointer
2874 && !sym->attr.use_assoc
2875 && sym->name)
2876 return true;
2878 return false;
2882 /* Figure out if a function reference is pure or not. Also set the name
2883 of the function for a potential error message. Return nonzero if the
2884 function is PURE, zero if not. */
2885 static int
2886 pure_stmt_function (gfc_expr *, gfc_symbol *);
2888 static int
2889 pure_function (gfc_expr *e, const char **name)
2891 int pure;
2892 gfc_component *comp;
2894 *name = NULL;
2896 if (e->symtree != NULL
2897 && e->symtree->n.sym != NULL
2898 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2899 return pure_stmt_function (e, e->symtree->n.sym);
2901 comp = gfc_get_proc_ptr_comp (e);
2902 if (comp)
2904 pure = gfc_pure (comp->ts.interface);
2905 *name = comp->name;
2907 else if (e->value.function.esym)
2909 pure = gfc_pure (e->value.function.esym);
2910 *name = e->value.function.esym->name;
2912 else if (e->value.function.isym)
2914 pure = e->value.function.isym->pure
2915 || e->value.function.isym->elemental;
2916 *name = e->value.function.isym->name;
2918 else
2920 /* Implicit functions are not pure. */
2921 pure = 0;
2922 *name = e->value.function.name;
2925 return pure;
2929 static bool
2930 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2931 int *f ATTRIBUTE_UNUSED)
2933 const char *name;
2935 /* Don't bother recursing into other statement functions
2936 since they will be checked individually for purity. */
2937 if (e->expr_type != EXPR_FUNCTION
2938 || !e->symtree
2939 || e->symtree->n.sym == sym
2940 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2941 return false;
2943 return pure_function (e, &name) ? false : true;
2947 static int
2948 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2950 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2954 /* Check if an impure function is allowed in the current context. */
2956 static bool check_pure_function (gfc_expr *e)
2958 const char *name = NULL;
2959 if (!pure_function (e, &name) && name)
2961 if (forall_flag)
2963 gfc_error ("Reference to impure function %qs at %L inside a "
2964 "FORALL %s", name, &e->where,
2965 forall_flag == 2 ? "mask" : "block");
2966 return false;
2968 else if (gfc_do_concurrent_flag)
2970 gfc_error ("Reference to impure function %qs at %L inside a "
2971 "DO CONCURRENT %s", name, &e->where,
2972 gfc_do_concurrent_flag == 2 ? "mask" : "block");
2973 return false;
2975 else if (gfc_pure (NULL))
2977 gfc_error ("Reference to impure function %qs at %L "
2978 "within a PURE procedure", name, &e->where);
2979 return false;
2981 gfc_unset_implicit_pure (NULL);
2983 return true;
2987 /* Update current procedure's array_outer_dependency flag, considering
2988 a call to procedure SYM. */
2990 static void
2991 update_current_proc_array_outer_dependency (gfc_symbol *sym)
2993 /* Check to see if this is a sibling function that has not yet
2994 been resolved. */
2995 gfc_namespace *sibling = gfc_current_ns->sibling;
2996 for (; sibling; sibling = sibling->sibling)
2998 if (sibling->proc_name == sym)
3000 gfc_resolve (sibling);
3001 break;
3005 /* If SYM has references to outer arrays, so has the procedure calling
3006 SYM. If SYM is a procedure pointer, we can assume the worst. */
3007 if (sym->attr.array_outer_dependency
3008 || sym->attr.proc_pointer)
3009 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3013 /* Resolve a function call, which means resolving the arguments, then figuring
3014 out which entity the name refers to. */
3016 static bool
3017 resolve_function (gfc_expr *expr)
3019 gfc_actual_arglist *arg;
3020 gfc_symbol *sym;
3021 bool t;
3022 int temp;
3023 procedure_type p = PROC_INTRINSIC;
3024 bool no_formal_args;
3026 sym = NULL;
3027 if (expr->symtree)
3028 sym = expr->symtree->n.sym;
3030 /* If this is a procedure pointer component, it has already been resolved. */
3031 if (gfc_is_proc_ptr_comp (expr))
3032 return true;
3034 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3035 another caf_get. */
3036 if (sym && sym->attr.intrinsic
3037 && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
3038 || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
3039 return true;
3041 if (sym && sym->attr.intrinsic
3042 && !gfc_resolve_intrinsic (sym, &expr->where))
3043 return false;
3045 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3047 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
3048 return false;
3051 /* If this ia a deferred TBP with an abstract interface (which may
3052 of course be referenced), expr->value.function.esym will be set. */
3053 if (sym && sym->attr.abstract && !expr->value.function.esym)
3055 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3056 sym->name, &expr->where);
3057 return false;
3060 /* Switch off assumed size checking and do this again for certain kinds
3061 of procedure, once the procedure itself is resolved. */
3062 need_full_assumed_size++;
3064 if (expr->symtree && expr->symtree->n.sym)
3065 p = expr->symtree->n.sym->attr.proc;
3067 if (expr->value.function.isym && expr->value.function.isym->inquiry)
3068 inquiry_argument = true;
3069 no_formal_args = sym && is_external_proc (sym)
3070 && gfc_sym_get_dummy_args (sym) == NULL;
3072 if (!resolve_actual_arglist (expr->value.function.actual,
3073 p, no_formal_args))
3075 inquiry_argument = false;
3076 return false;
3079 inquiry_argument = false;
3081 /* Resume assumed_size checking. */
3082 need_full_assumed_size--;
3084 /* If the procedure is external, check for usage. */
3085 if (sym && is_external_proc (sym))
3086 resolve_global_procedure (sym, &expr->where,
3087 &expr->value.function.actual, 0);
3089 if (sym && sym->ts.type == BT_CHARACTER
3090 && sym->ts.u.cl
3091 && sym->ts.u.cl->length == NULL
3092 && !sym->attr.dummy
3093 && !sym->ts.deferred
3094 && expr->value.function.esym == NULL
3095 && !sym->attr.contained)
3097 /* Internal procedures are taken care of in resolve_contained_fntype. */
3098 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3099 "be used at %L since it is not a dummy argument",
3100 sym->name, &expr->where);
3101 return false;
3104 /* See if function is already resolved. */
3106 if (expr->value.function.name != NULL
3107 || expr->value.function.isym != NULL)
3109 if (expr->ts.type == BT_UNKNOWN)
3110 expr->ts = sym->ts;
3111 t = true;
3113 else
3115 /* Apply the rules of section 14.1.2. */
3117 switch (procedure_kind (sym))
3119 case PTYPE_GENERIC:
3120 t = resolve_generic_f (expr);
3121 break;
3123 case PTYPE_SPECIFIC:
3124 t = resolve_specific_f (expr);
3125 break;
3127 case PTYPE_UNKNOWN:
3128 t = resolve_unknown_f (expr);
3129 break;
3131 default:
3132 gfc_internal_error ("resolve_function(): bad function type");
3136 /* If the expression is still a function (it might have simplified),
3137 then we check to see if we are calling an elemental function. */
3139 if (expr->expr_type != EXPR_FUNCTION)
3140 return t;
3142 temp = need_full_assumed_size;
3143 need_full_assumed_size = 0;
3145 if (!resolve_elemental_actual (expr, NULL))
3146 return false;
3148 if (omp_workshare_flag
3149 && expr->value.function.esym
3150 && ! gfc_elemental (expr->value.function.esym))
3152 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3153 "in WORKSHARE construct", expr->value.function.esym->name,
3154 &expr->where);
3155 t = false;
3158 #define GENERIC_ID expr->value.function.isym->id
3159 else if (expr->value.function.actual != NULL
3160 && expr->value.function.isym != NULL
3161 && GENERIC_ID != GFC_ISYM_LBOUND
3162 && GENERIC_ID != GFC_ISYM_LCOBOUND
3163 && GENERIC_ID != GFC_ISYM_UCOBOUND
3164 && GENERIC_ID != GFC_ISYM_LEN
3165 && GENERIC_ID != GFC_ISYM_LOC
3166 && GENERIC_ID != GFC_ISYM_C_LOC
3167 && GENERIC_ID != GFC_ISYM_PRESENT)
3169 /* Array intrinsics must also have the last upper bound of an
3170 assumed size array argument. UBOUND and SIZE have to be
3171 excluded from the check if the second argument is anything
3172 than a constant. */
3174 for (arg = expr->value.function.actual; arg; arg = arg->next)
3176 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3177 && arg == expr->value.function.actual
3178 && arg->next != NULL && arg->next->expr)
3180 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3181 break;
3183 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
3184 break;
3186 if ((int)mpz_get_si (arg->next->expr->value.integer)
3187 < arg->expr->rank)
3188 break;
3191 if (arg->expr != NULL
3192 && arg->expr->rank > 0
3193 && resolve_assumed_size_actual (arg->expr))
3194 return false;
3197 #undef GENERIC_ID
3199 need_full_assumed_size = temp;
3201 if (!check_pure_function(expr))
3202 t = false;
3204 /* Functions without the RECURSIVE attribution are not allowed to
3205 * call themselves. */
3206 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3208 gfc_symbol *esym;
3209 esym = expr->value.function.esym;
3211 if (is_illegal_recursion (esym, gfc_current_ns))
3213 if (esym->attr.entry && esym->ns->entries)
3214 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3215 " function %qs is not RECURSIVE",
3216 esym->name, &expr->where, esym->ns->entries->sym->name);
3217 else
3218 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3219 " is not RECURSIVE", esym->name, &expr->where);
3221 t = false;
3225 /* Character lengths of use associated functions may contains references to
3226 symbols not referenced from the current program unit otherwise. Make sure
3227 those symbols are marked as referenced. */
3229 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3230 && expr->value.function.esym->attr.use_assoc)
3232 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3235 /* Make sure that the expression has a typespec that works. */
3236 if (expr->ts.type == BT_UNKNOWN)
3238 if (expr->symtree->n.sym->result
3239 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3240 && !expr->symtree->n.sym->result->attr.proc_pointer)
3241 expr->ts = expr->symtree->n.sym->result->ts;
3244 if (!expr->ref && !expr->value.function.isym)
3246 if (expr->value.function.esym)
3247 update_current_proc_array_outer_dependency (expr->value.function.esym);
3248 else
3249 update_current_proc_array_outer_dependency (sym);
3251 else if (expr->ref)
3252 /* typebound procedure: Assume the worst. */
3253 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3255 return t;
3259 /************* Subroutine resolution *************/
3261 static bool
3262 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3264 if (gfc_pure (sym))
3265 return true;
3267 if (forall_flag)
3269 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3270 name, loc);
3271 return false;
3273 else if (gfc_do_concurrent_flag)
3275 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3276 "PURE", name, loc);
3277 return false;
3279 else if (gfc_pure (NULL))
3281 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3282 return false;
3285 gfc_unset_implicit_pure (NULL);
3286 return true;
3290 static match
3291 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3293 gfc_symbol *s;
3295 if (sym->attr.generic)
3297 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3298 if (s != NULL)
3300 c->resolved_sym = s;
3301 if (!pure_subroutine (s, s->name, &c->loc))
3302 return MATCH_ERROR;
3303 return MATCH_YES;
3306 /* TODO: Need to search for elemental references in generic interface. */
3309 if (sym->attr.intrinsic)
3310 return gfc_intrinsic_sub_interface (c, 0);
3312 return MATCH_NO;
3316 static bool
3317 resolve_generic_s (gfc_code *c)
3319 gfc_symbol *sym;
3320 match m;
3322 sym = c->symtree->n.sym;
3324 for (;;)
3326 m = resolve_generic_s0 (c, sym);
3327 if (m == MATCH_YES)
3328 return true;
3329 else if (m == MATCH_ERROR)
3330 return false;
3332 generic:
3333 if (sym->ns->parent == NULL)
3334 break;
3335 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3337 if (sym == NULL)
3338 break;
3339 if (!generic_sym (sym))
3340 goto generic;
3343 /* Last ditch attempt. See if the reference is to an intrinsic
3344 that possesses a matching interface. 14.1.2.4 */
3345 sym = c->symtree->n.sym;
3347 if (!gfc_is_intrinsic (sym, 1, c->loc))
3349 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3350 sym->name, &c->loc);
3351 return false;
3354 m = gfc_intrinsic_sub_interface (c, 0);
3355 if (m == MATCH_YES)
3356 return true;
3357 if (m == MATCH_NO)
3358 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3359 "intrinsic subroutine interface", sym->name, &c->loc);
3361 return false;
3365 /* Resolve a subroutine call known to be specific. */
3367 static match
3368 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3370 match m;
3372 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3374 if (sym->attr.dummy)
3376 sym->attr.proc = PROC_DUMMY;
3377 goto found;
3380 sym->attr.proc = PROC_EXTERNAL;
3381 goto found;
3384 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3385 goto found;
3387 if (sym->attr.intrinsic)
3389 m = gfc_intrinsic_sub_interface (c, 1);
3390 if (m == MATCH_YES)
3391 return MATCH_YES;
3392 if (m == MATCH_NO)
3393 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3394 "with an intrinsic", sym->name, &c->loc);
3396 return MATCH_ERROR;
3399 return MATCH_NO;
3401 found:
3402 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3404 c->resolved_sym = sym;
3405 if (!pure_subroutine (sym, sym->name, &c->loc))
3406 return MATCH_ERROR;
3408 return MATCH_YES;
3412 static bool
3413 resolve_specific_s (gfc_code *c)
3415 gfc_symbol *sym;
3416 match m;
3418 sym = c->symtree->n.sym;
3420 for (;;)
3422 m = resolve_specific_s0 (c, sym);
3423 if (m == MATCH_YES)
3424 return true;
3425 if (m == MATCH_ERROR)
3426 return false;
3428 if (sym->ns->parent == NULL)
3429 break;
3431 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3433 if (sym == NULL)
3434 break;
3437 sym = c->symtree->n.sym;
3438 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3439 sym->name, &c->loc);
3441 return false;
3445 /* Resolve a subroutine call not known to be generic nor specific. */
3447 static bool
3448 resolve_unknown_s (gfc_code *c)
3450 gfc_symbol *sym;
3452 sym = c->symtree->n.sym;
3454 if (sym->attr.dummy)
3456 sym->attr.proc = PROC_DUMMY;
3457 goto found;
3460 /* See if we have an intrinsic function reference. */
3462 if (gfc_is_intrinsic (sym, 1, c->loc))
3464 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3465 return true;
3466 return false;
3469 /* The reference is to an external name. */
3471 found:
3472 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3474 c->resolved_sym = sym;
3476 return pure_subroutine (sym, sym->name, &c->loc);
3480 /* Resolve a subroutine call. Although it was tempting to use the same code
3481 for functions, subroutines and functions are stored differently and this
3482 makes things awkward. */
3484 static bool
3485 resolve_call (gfc_code *c)
3487 bool t;
3488 procedure_type ptype = PROC_INTRINSIC;
3489 gfc_symbol *csym, *sym;
3490 bool no_formal_args;
3492 csym = c->symtree ? c->symtree->n.sym : NULL;
3494 if (csym && csym->ts.type != BT_UNKNOWN)
3496 gfc_error ("%qs at %L has a type, which is not consistent with "
3497 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3498 return false;
3501 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3503 gfc_symtree *st;
3504 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3505 sym = st ? st->n.sym : NULL;
3506 if (sym && csym != sym
3507 && sym->ns == gfc_current_ns
3508 && sym->attr.flavor == FL_PROCEDURE
3509 && sym->attr.contained)
3511 sym->refs++;
3512 if (csym->attr.generic)
3513 c->symtree->n.sym = sym;
3514 else
3515 c->symtree = st;
3516 csym = c->symtree->n.sym;
3520 /* If this ia a deferred TBP, c->expr1 will be set. */
3521 if (!c->expr1 && csym)
3523 if (csym->attr.abstract)
3525 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3526 csym->name, &c->loc);
3527 return false;
3530 /* Subroutines without the RECURSIVE attribution are not allowed to
3531 call themselves. */
3532 if (is_illegal_recursion (csym, gfc_current_ns))
3534 if (csym->attr.entry && csym->ns->entries)
3535 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3536 "as subroutine %qs is not RECURSIVE",
3537 csym->name, &c->loc, csym->ns->entries->sym->name);
3538 else
3539 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3540 "as it is not RECURSIVE", csym->name, &c->loc);
3542 t = false;
3546 /* Switch off assumed size checking and do this again for certain kinds
3547 of procedure, once the procedure itself is resolved. */
3548 need_full_assumed_size++;
3550 if (csym)
3551 ptype = csym->attr.proc;
3553 no_formal_args = csym && is_external_proc (csym)
3554 && gfc_sym_get_dummy_args (csym) == NULL;
3555 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3556 return false;
3558 /* Resume assumed_size checking. */
3559 need_full_assumed_size--;
3561 /* If external, check for usage. */
3562 if (csym && is_external_proc (csym))
3563 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3565 t = true;
3566 if (c->resolved_sym == NULL)
3568 c->resolved_isym = NULL;
3569 switch (procedure_kind (csym))
3571 case PTYPE_GENERIC:
3572 t = resolve_generic_s (c);
3573 break;
3575 case PTYPE_SPECIFIC:
3576 t = resolve_specific_s (c);
3577 break;
3579 case PTYPE_UNKNOWN:
3580 t = resolve_unknown_s (c);
3581 break;
3583 default:
3584 gfc_internal_error ("resolve_subroutine(): bad function type");
3588 /* Some checks of elemental subroutine actual arguments. */
3589 if (!resolve_elemental_actual (NULL, c))
3590 return false;
3592 if (!c->expr1)
3593 update_current_proc_array_outer_dependency (csym);
3594 else
3595 /* Typebound procedure: Assume the worst. */
3596 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3598 return t;
3602 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3603 op1->shape and op2->shape are non-NULL return true if their shapes
3604 match. If both op1->shape and op2->shape are non-NULL return false
3605 if their shapes do not match. If either op1->shape or op2->shape is
3606 NULL, return true. */
3608 static bool
3609 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3611 bool t;
3612 int i;
3614 t = true;
3616 if (op1->shape != NULL && op2->shape != NULL)
3618 for (i = 0; i < op1->rank; i++)
3620 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3622 gfc_error ("Shapes for operands at %L and %L are not conformable",
3623 &op1->where, &op2->where);
3624 t = false;
3625 break;
3630 return t;
3633 /* Convert a logical operator to the corresponding bitwise intrinsic call.
3634 For example A .AND. B becomes IAND(A, B). */
3635 static gfc_expr *
3636 logical_to_bitwise (gfc_expr *e)
3638 gfc_expr *tmp, *op1, *op2;
3639 gfc_isym_id isym;
3640 gfc_actual_arglist *args = NULL;
3642 gcc_assert (e->expr_type == EXPR_OP);
3644 isym = GFC_ISYM_NONE;
3645 op1 = e->value.op.op1;
3646 op2 = e->value.op.op2;
3648 switch (e->value.op.op)
3650 case INTRINSIC_NOT:
3651 isym = GFC_ISYM_NOT;
3652 break;
3653 case INTRINSIC_AND:
3654 isym = GFC_ISYM_IAND;
3655 break;
3656 case INTRINSIC_OR:
3657 isym = GFC_ISYM_IOR;
3658 break;
3659 case INTRINSIC_NEQV:
3660 isym = GFC_ISYM_IEOR;
3661 break;
3662 case INTRINSIC_EQV:
3663 /* "Bitwise eqv" is just the complement of NEQV === IEOR.
3664 Change the old expression to NEQV, which will get replaced by IEOR,
3665 and wrap it in NOT. */
3666 tmp = gfc_copy_expr (e);
3667 tmp->value.op.op = INTRINSIC_NEQV;
3668 tmp = logical_to_bitwise (tmp);
3669 isym = GFC_ISYM_NOT;
3670 op1 = tmp;
3671 op2 = NULL;
3672 break;
3673 default:
3674 gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
3677 /* Inherit the original operation's operands as arguments. */
3678 args = gfc_get_actual_arglist ();
3679 args->expr = op1;
3680 if (op2)
3682 args->next = gfc_get_actual_arglist ();
3683 args->next->expr = op2;
3686 /* Convert the expression to a function call. */
3687 e->expr_type = EXPR_FUNCTION;
3688 e->value.function.actual = args;
3689 e->value.function.isym = gfc_intrinsic_function_by_id (isym);
3690 e->value.function.name = e->value.function.isym->name;
3691 e->value.function.esym = NULL;
3693 /* Make up a pre-resolved function call symtree if we need to. */
3694 if (!e->symtree || !e->symtree->n.sym)
3696 gfc_symbol *sym;
3697 gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
3698 sym = e->symtree->n.sym;
3699 sym->result = sym;
3700 sym->attr.flavor = FL_PROCEDURE;
3701 sym->attr.function = 1;
3702 sym->attr.elemental = 1;
3703 sym->attr.pure = 1;
3704 sym->attr.referenced = 1;
3705 gfc_intrinsic_symbol (sym);
3706 gfc_commit_symbol (sym);
3709 args->name = e->value.function.isym->formal->name;
3710 if (e->value.function.isym->formal->next)
3711 args->next->name = e->value.function.isym->formal->next->name;
3713 return e;
3716 /* Resolve an operator expression node. This can involve replacing the
3717 operation with a user defined function call. */
3719 static bool
3720 resolve_operator (gfc_expr *e)
3722 gfc_expr *op1, *op2;
3723 char msg[200];
3724 bool dual_locus_error;
3725 bool t;
3727 /* Resolve all subnodes-- give them types. */
3729 switch (e->value.op.op)
3731 default:
3732 if (!gfc_resolve_expr (e->value.op.op2))
3733 return false;
3735 /* Fall through. */
3737 case INTRINSIC_NOT:
3738 case INTRINSIC_UPLUS:
3739 case INTRINSIC_UMINUS:
3740 case INTRINSIC_PARENTHESES:
3741 if (!gfc_resolve_expr (e->value.op.op1))
3742 return false;
3743 break;
3746 /* Typecheck the new node. */
3748 op1 = e->value.op.op1;
3749 op2 = e->value.op.op2;
3750 dual_locus_error = false;
3752 if ((op1 && op1->expr_type == EXPR_NULL)
3753 || (op2 && op2->expr_type == EXPR_NULL))
3755 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3756 goto bad_op;
3759 switch (e->value.op.op)
3761 case INTRINSIC_UPLUS:
3762 case INTRINSIC_UMINUS:
3763 if (op1->ts.type == BT_INTEGER
3764 || op1->ts.type == BT_REAL
3765 || op1->ts.type == BT_COMPLEX)
3767 e->ts = op1->ts;
3768 break;
3771 sprintf (msg, _("Operand of unary numeric operator %%<%s%%> at %%L is %s"),
3772 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3773 goto bad_op;
3775 case INTRINSIC_PLUS:
3776 case INTRINSIC_MINUS:
3777 case INTRINSIC_TIMES:
3778 case INTRINSIC_DIVIDE:
3779 case INTRINSIC_POWER:
3780 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3782 gfc_type_convert_binary (e, 1);
3783 break;
3786 sprintf (msg,
3787 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"),
3788 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3789 gfc_typename (&op2->ts));
3790 goto bad_op;
3792 case INTRINSIC_CONCAT:
3793 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3794 && op1->ts.kind == op2->ts.kind)
3796 e->ts.type = BT_CHARACTER;
3797 e->ts.kind = op1->ts.kind;
3798 break;
3801 sprintf (msg,
3802 _("Operands of string concatenation operator at %%L are %s/%s"),
3803 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3804 goto bad_op;
3806 case INTRINSIC_AND:
3807 case INTRINSIC_OR:
3808 case INTRINSIC_EQV:
3809 case INTRINSIC_NEQV:
3810 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3812 e->ts.type = BT_LOGICAL;
3813 e->ts.kind = gfc_kind_max (op1, op2);
3814 if (op1->ts.kind < e->ts.kind)
3815 gfc_convert_type (op1, &e->ts, 2);
3816 else if (op2->ts.kind < e->ts.kind)
3817 gfc_convert_type (op2, &e->ts, 2);
3818 break;
3821 /* Logical ops on integers become bitwise ops with -fdec. */
3822 else if (flag_dec
3823 && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
3825 e->ts.type = BT_INTEGER;
3826 e->ts.kind = gfc_kind_max (op1, op2);
3827 if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
3828 gfc_convert_type (op1, &e->ts, 1);
3829 if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
3830 gfc_convert_type (op2, &e->ts, 1);
3831 e = logical_to_bitwise (e);
3832 return resolve_function (e);
3835 sprintf (msg, _("Operands of logical operator %%<%s%%> at %%L are %s/%s"),
3836 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3837 gfc_typename (&op2->ts));
3839 goto bad_op;
3841 case INTRINSIC_NOT:
3842 /* Logical ops on integers become bitwise ops with -fdec. */
3843 if (flag_dec && op1->ts.type == BT_INTEGER)
3845 e->ts.type = BT_INTEGER;
3846 e->ts.kind = op1->ts.kind;
3847 e = logical_to_bitwise (e);
3848 return resolve_function (e);
3851 if (op1->ts.type == BT_LOGICAL)
3853 e->ts.type = BT_LOGICAL;
3854 e->ts.kind = op1->ts.kind;
3855 break;
3858 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3859 gfc_typename (&op1->ts));
3860 goto bad_op;
3862 case INTRINSIC_GT:
3863 case INTRINSIC_GT_OS:
3864 case INTRINSIC_GE:
3865 case INTRINSIC_GE_OS:
3866 case INTRINSIC_LT:
3867 case INTRINSIC_LT_OS:
3868 case INTRINSIC_LE:
3869 case INTRINSIC_LE_OS:
3870 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3872 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3873 goto bad_op;
3876 /* Fall through. */
3878 case INTRINSIC_EQ:
3879 case INTRINSIC_EQ_OS:
3880 case INTRINSIC_NE:
3881 case INTRINSIC_NE_OS:
3882 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3883 && op1->ts.kind == op2->ts.kind)
3885 e->ts.type = BT_LOGICAL;
3886 e->ts.kind = gfc_default_logical_kind;
3887 break;
3890 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3892 gfc_type_convert_binary (e, 1);
3894 e->ts.type = BT_LOGICAL;
3895 e->ts.kind = gfc_default_logical_kind;
3897 if (warn_compare_reals)
3899 gfc_intrinsic_op op = e->value.op.op;
3901 /* Type conversion has made sure that the types of op1 and op2
3902 agree, so it is only necessary to check the first one. */
3903 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3904 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3905 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3907 const char *msg;
3909 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3910 msg = "Equality comparison for %s at %L";
3911 else
3912 msg = "Inequality comparison for %s at %L";
3914 gfc_warning (OPT_Wcompare_reals, msg,
3915 gfc_typename (&op1->ts), &op1->where);
3919 break;
3922 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3923 sprintf (msg,
3924 _("Logicals at %%L must be compared with %s instead of %s"),
3925 (e->value.op.op == INTRINSIC_EQ
3926 || e->value.op.op == INTRINSIC_EQ_OS)
3927 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3928 else
3929 sprintf (msg,
3930 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"),
3931 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3932 gfc_typename (&op2->ts));
3934 goto bad_op;
3936 case INTRINSIC_USER:
3937 if (e->value.op.uop->op == NULL)
3938 sprintf (msg, _("Unknown operator %%<%s%%> at %%L"),
3939 e->value.op.uop->name);
3940 else if (op2 == NULL)
3941 sprintf (msg, _("Operand of user operator %%<%s%%> at %%L is %s"),
3942 e->value.op.uop->name, gfc_typename (&op1->ts));
3943 else
3945 sprintf (msg, _("Operands of user operator %%<%s%%> at %%L are %s/%s"),
3946 e->value.op.uop->name, gfc_typename (&op1->ts),
3947 gfc_typename (&op2->ts));
3948 e->value.op.uop->op->sym->attr.referenced = 1;
3951 goto bad_op;
3953 case INTRINSIC_PARENTHESES:
3954 e->ts = op1->ts;
3955 if (e->ts.type == BT_CHARACTER)
3956 e->ts.u.cl = op1->ts.u.cl;
3957 break;
3959 default:
3960 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3963 /* Deal with arrayness of an operand through an operator. */
3965 t = true;
3967 switch (e->value.op.op)
3969 case INTRINSIC_PLUS:
3970 case INTRINSIC_MINUS:
3971 case INTRINSIC_TIMES:
3972 case INTRINSIC_DIVIDE:
3973 case INTRINSIC_POWER:
3974 case INTRINSIC_CONCAT:
3975 case INTRINSIC_AND:
3976 case INTRINSIC_OR:
3977 case INTRINSIC_EQV:
3978 case INTRINSIC_NEQV:
3979 case INTRINSIC_EQ:
3980 case INTRINSIC_EQ_OS:
3981 case INTRINSIC_NE:
3982 case INTRINSIC_NE_OS:
3983 case INTRINSIC_GT:
3984 case INTRINSIC_GT_OS:
3985 case INTRINSIC_GE:
3986 case INTRINSIC_GE_OS:
3987 case INTRINSIC_LT:
3988 case INTRINSIC_LT_OS:
3989 case INTRINSIC_LE:
3990 case INTRINSIC_LE_OS:
3992 if (op1->rank == 0 && op2->rank == 0)
3993 e->rank = 0;
3995 if (op1->rank == 0 && op2->rank != 0)
3997 e->rank = op2->rank;
3999 if (e->shape == NULL)
4000 e->shape = gfc_copy_shape (op2->shape, op2->rank);
4003 if (op1->rank != 0 && op2->rank == 0)
4005 e->rank = op1->rank;
4007 if (e->shape == NULL)
4008 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4011 if (op1->rank != 0 && op2->rank != 0)
4013 if (op1->rank == op2->rank)
4015 e->rank = op1->rank;
4016 if (e->shape == NULL)
4018 t = compare_shapes (op1, op2);
4019 if (!t)
4020 e->shape = NULL;
4021 else
4022 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4025 else
4027 /* Allow higher level expressions to work. */
4028 e->rank = 0;
4030 /* Try user-defined operators, and otherwise throw an error. */
4031 dual_locus_error = true;
4032 sprintf (msg,
4033 _("Inconsistent ranks for operator at %%L and %%L"));
4034 goto bad_op;
4038 break;
4040 case INTRINSIC_PARENTHESES:
4041 case INTRINSIC_NOT:
4042 case INTRINSIC_UPLUS:
4043 case INTRINSIC_UMINUS:
4044 /* Simply copy arrayness attribute */
4045 e->rank = op1->rank;
4047 if (e->shape == NULL)
4048 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4050 break;
4052 default:
4053 break;
4056 /* Attempt to simplify the expression. */
4057 if (t)
4059 t = gfc_simplify_expr (e, 0);
4060 /* Some calls do not succeed in simplification and return false
4061 even though there is no error; e.g. variable references to
4062 PARAMETER arrays. */
4063 if (!gfc_is_constant_expr (e))
4064 t = true;
4066 return t;
4068 bad_op:
4071 match m = gfc_extend_expr (e);
4072 if (m == MATCH_YES)
4073 return true;
4074 if (m == MATCH_ERROR)
4075 return false;
4078 if (dual_locus_error)
4079 gfc_error (msg, &op1->where, &op2->where);
4080 else
4081 gfc_error (msg, &e->where);
4083 return false;
4087 /************** Array resolution subroutines **************/
4089 enum compare_result
4090 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
4092 /* Compare two integer expressions. */
4094 static compare_result
4095 compare_bound (gfc_expr *a, gfc_expr *b)
4097 int i;
4099 if (a == NULL || a->expr_type != EXPR_CONSTANT
4100 || b == NULL || b->expr_type != EXPR_CONSTANT)
4101 return CMP_UNKNOWN;
4103 /* If either of the types isn't INTEGER, we must have
4104 raised an error earlier. */
4106 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4107 return CMP_UNKNOWN;
4109 i = mpz_cmp (a->value.integer, b->value.integer);
4111 if (i < 0)
4112 return CMP_LT;
4113 if (i > 0)
4114 return CMP_GT;
4115 return CMP_EQ;
4119 /* Compare an integer expression with an integer. */
4121 static compare_result
4122 compare_bound_int (gfc_expr *a, int b)
4124 int i;
4126 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4127 return CMP_UNKNOWN;
4129 if (a->ts.type != BT_INTEGER)
4130 gfc_internal_error ("compare_bound_int(): Bad expression");
4132 i = mpz_cmp_si (a->value.integer, b);
4134 if (i < 0)
4135 return CMP_LT;
4136 if (i > 0)
4137 return CMP_GT;
4138 return CMP_EQ;
4142 /* Compare an integer expression with a mpz_t. */
4144 static compare_result
4145 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4147 int i;
4149 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4150 return CMP_UNKNOWN;
4152 if (a->ts.type != BT_INTEGER)
4153 gfc_internal_error ("compare_bound_int(): Bad expression");
4155 i = mpz_cmp (a->value.integer, b);
4157 if (i < 0)
4158 return CMP_LT;
4159 if (i > 0)
4160 return CMP_GT;
4161 return CMP_EQ;
4165 /* Compute the last value of a sequence given by a triplet.
4166 Return 0 if it wasn't able to compute the last value, or if the
4167 sequence if empty, and 1 otherwise. */
4169 static int
4170 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4171 gfc_expr *stride, mpz_t last)
4173 mpz_t rem;
4175 if (start == NULL || start->expr_type != EXPR_CONSTANT
4176 || end == NULL || end->expr_type != EXPR_CONSTANT
4177 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4178 return 0;
4180 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4181 || (stride != NULL && stride->ts.type != BT_INTEGER))
4182 return 0;
4184 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
4186 if (compare_bound (start, end) == CMP_GT)
4187 return 0;
4188 mpz_set (last, end->value.integer);
4189 return 1;
4192 if (compare_bound_int (stride, 0) == CMP_GT)
4194 /* Stride is positive */
4195 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4196 return 0;
4198 else
4200 /* Stride is negative */
4201 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4202 return 0;
4205 mpz_init (rem);
4206 mpz_sub (rem, end->value.integer, start->value.integer);
4207 mpz_tdiv_r (rem, rem, stride->value.integer);
4208 mpz_sub (last, end->value.integer, rem);
4209 mpz_clear (rem);
4211 return 1;
4215 /* Compare a single dimension of an array reference to the array
4216 specification. */
4218 static bool
4219 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4221 mpz_t last_value;
4223 if (ar->dimen_type[i] == DIMEN_STAR)
4225 gcc_assert (ar->stride[i] == NULL);
4226 /* This implies [*] as [*:] and [*:3] are not possible. */
4227 if (ar->start[i] == NULL)
4229 gcc_assert (ar->end[i] == NULL);
4230 return true;
4234 /* Given start, end and stride values, calculate the minimum and
4235 maximum referenced indexes. */
4237 switch (ar->dimen_type[i])
4239 case DIMEN_VECTOR:
4240 case DIMEN_THIS_IMAGE:
4241 break;
4243 case DIMEN_STAR:
4244 case DIMEN_ELEMENT:
4245 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4247 if (i < as->rank)
4248 gfc_warning (0, "Array reference at %L is out of bounds "
4249 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4250 mpz_get_si (ar->start[i]->value.integer),
4251 mpz_get_si (as->lower[i]->value.integer), i+1);
4252 else
4253 gfc_warning (0, "Array reference at %L is out of bounds "
4254 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4255 mpz_get_si (ar->start[i]->value.integer),
4256 mpz_get_si (as->lower[i]->value.integer),
4257 i + 1 - as->rank);
4258 return true;
4260 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4262 if (i < as->rank)
4263 gfc_warning (0, "Array reference at %L is out of bounds "
4264 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4265 mpz_get_si (ar->start[i]->value.integer),
4266 mpz_get_si (as->upper[i]->value.integer), i+1);
4267 else
4268 gfc_warning (0, "Array reference at %L is out of bounds "
4269 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4270 mpz_get_si (ar->start[i]->value.integer),
4271 mpz_get_si (as->upper[i]->value.integer),
4272 i + 1 - as->rank);
4273 return true;
4276 break;
4278 case DIMEN_RANGE:
4280 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4281 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4283 compare_result comp_start_end = compare_bound (AR_START, AR_END);
4285 /* Check for zero stride, which is not allowed. */
4286 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4288 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4289 return false;
4292 /* if start == len || (stride > 0 && start < len)
4293 || (stride < 0 && start > len),
4294 then the array section contains at least one element. In this
4295 case, there is an out-of-bounds access if
4296 (start < lower || start > upper). */
4297 if (compare_bound (AR_START, AR_END) == CMP_EQ
4298 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4299 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4300 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4301 && comp_start_end == CMP_GT))
4303 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4305 gfc_warning (0, "Lower array reference at %L is out of bounds "
4306 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4307 mpz_get_si (AR_START->value.integer),
4308 mpz_get_si (as->lower[i]->value.integer), i+1);
4309 return true;
4311 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4313 gfc_warning (0, "Lower array reference at %L is out of bounds "
4314 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4315 mpz_get_si (AR_START->value.integer),
4316 mpz_get_si (as->upper[i]->value.integer), i+1);
4317 return true;
4321 /* If we can compute the highest index of the array section,
4322 then it also has to be between lower and upper. */
4323 mpz_init (last_value);
4324 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4325 last_value))
4327 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4329 gfc_warning (0, "Upper array reference at %L is out of bounds "
4330 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4331 mpz_get_si (last_value),
4332 mpz_get_si (as->lower[i]->value.integer), i+1);
4333 mpz_clear (last_value);
4334 return true;
4336 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4338 gfc_warning (0, "Upper array reference at %L is out of bounds "
4339 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4340 mpz_get_si (last_value),
4341 mpz_get_si (as->upper[i]->value.integer), i+1);
4342 mpz_clear (last_value);
4343 return true;
4346 mpz_clear (last_value);
4348 #undef AR_START
4349 #undef AR_END
4351 break;
4353 default:
4354 gfc_internal_error ("check_dimension(): Bad array reference");
4357 return true;
4361 /* Compare an array reference with an array specification. */
4363 static bool
4364 compare_spec_to_ref (gfc_array_ref *ar)
4366 gfc_array_spec *as;
4367 int i;
4369 as = ar->as;
4370 i = as->rank - 1;
4371 /* TODO: Full array sections are only allowed as actual parameters. */
4372 if (as->type == AS_ASSUMED_SIZE
4373 && (/*ar->type == AR_FULL
4374 ||*/ (ar->type == AR_SECTION
4375 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4377 gfc_error ("Rightmost upper bound of assumed size array section "
4378 "not specified at %L", &ar->where);
4379 return false;
4382 if (ar->type == AR_FULL)
4383 return true;
4385 if (as->rank != ar->dimen)
4387 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4388 &ar->where, ar->dimen, as->rank);
4389 return false;
4392 /* ar->codimen == 0 is a local array. */
4393 if (as->corank != ar->codimen && ar->codimen != 0)
4395 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4396 &ar->where, ar->codimen, as->corank);
4397 return false;
4400 for (i = 0; i < as->rank; i++)
4401 if (!check_dimension (i, ar, as))
4402 return false;
4404 /* Local access has no coarray spec. */
4405 if (ar->codimen != 0)
4406 for (i = as->rank; i < as->rank + as->corank; i++)
4408 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4409 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4411 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4412 i + 1 - as->rank, &ar->where);
4413 return false;
4415 if (!check_dimension (i, ar, as))
4416 return false;
4419 return true;
4423 /* Resolve one part of an array index. */
4425 static bool
4426 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4427 int force_index_integer_kind)
4429 gfc_typespec ts;
4431 if (index == NULL)
4432 return true;
4434 if (!gfc_resolve_expr (index))
4435 return false;
4437 if (check_scalar && index->rank != 0)
4439 gfc_error ("Array index at %L must be scalar", &index->where);
4440 return false;
4443 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4445 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4446 &index->where, gfc_basic_typename (index->ts.type));
4447 return false;
4450 if (index->ts.type == BT_REAL)
4451 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4452 &index->where))
4453 return false;
4455 if ((index->ts.kind != gfc_index_integer_kind
4456 && force_index_integer_kind)
4457 || index->ts.type != BT_INTEGER)
4459 gfc_clear_ts (&ts);
4460 ts.type = BT_INTEGER;
4461 ts.kind = gfc_index_integer_kind;
4463 gfc_convert_type_warn (index, &ts, 2, 0);
4466 return true;
4469 /* Resolve one part of an array index. */
4471 bool
4472 gfc_resolve_index (gfc_expr *index, int check_scalar)
4474 return gfc_resolve_index_1 (index, check_scalar, 1);
4477 /* Resolve a dim argument to an intrinsic function. */
4479 bool
4480 gfc_resolve_dim_arg (gfc_expr *dim)
4482 if (dim == NULL)
4483 return true;
4485 if (!gfc_resolve_expr (dim))
4486 return false;
4488 if (dim->rank != 0)
4490 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4491 return false;
4495 if (dim->ts.type != BT_INTEGER)
4497 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4498 return false;
4501 if (dim->ts.kind != gfc_index_integer_kind)
4503 gfc_typespec ts;
4505 gfc_clear_ts (&ts);
4506 ts.type = BT_INTEGER;
4507 ts.kind = gfc_index_integer_kind;
4509 gfc_convert_type_warn (dim, &ts, 2, 0);
4512 return true;
4515 /* Given an expression that contains array references, update those array
4516 references to point to the right array specifications. While this is
4517 filled in during matching, this information is difficult to save and load
4518 in a module, so we take care of it here.
4520 The idea here is that the original array reference comes from the
4521 base symbol. We traverse the list of reference structures, setting
4522 the stored reference to references. Component references can
4523 provide an additional array specification. */
4525 static void
4526 find_array_spec (gfc_expr *e)
4528 gfc_array_spec *as;
4529 gfc_component *c;
4530 gfc_ref *ref;
4532 if (e->symtree->n.sym->ts.type == BT_CLASS)
4533 as = CLASS_DATA (e->symtree->n.sym)->as;
4534 else
4535 as = e->symtree->n.sym->as;
4537 for (ref = e->ref; ref; ref = ref->next)
4538 switch (ref->type)
4540 case REF_ARRAY:
4541 if (as == NULL)
4542 gfc_internal_error ("find_array_spec(): Missing spec");
4544 ref->u.ar.as = as;
4545 as = NULL;
4546 break;
4548 case REF_COMPONENT:
4549 c = ref->u.c.component;
4550 if (c->attr.dimension)
4552 if (as != NULL)
4553 gfc_internal_error ("find_array_spec(): unused as(1)");
4554 as = c->as;
4557 break;
4559 case REF_SUBSTRING:
4560 break;
4563 if (as != NULL)
4564 gfc_internal_error ("find_array_spec(): unused as(2)");
4568 /* Resolve an array reference. */
4570 static bool
4571 resolve_array_ref (gfc_array_ref *ar)
4573 int i, check_scalar;
4574 gfc_expr *e;
4576 for (i = 0; i < ar->dimen + ar->codimen; i++)
4578 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4580 /* Do not force gfc_index_integer_kind for the start. We can
4581 do fine with any integer kind. This avoids temporary arrays
4582 created for indexing with a vector. */
4583 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4584 return false;
4585 if (!gfc_resolve_index (ar->end[i], check_scalar))
4586 return false;
4587 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4588 return false;
4590 e = ar->start[i];
4592 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4593 switch (e->rank)
4595 case 0:
4596 ar->dimen_type[i] = DIMEN_ELEMENT;
4597 break;
4599 case 1:
4600 ar->dimen_type[i] = DIMEN_VECTOR;
4601 if (e->expr_type == EXPR_VARIABLE
4602 && e->symtree->n.sym->ts.type == BT_DERIVED)
4603 ar->start[i] = gfc_get_parentheses (e);
4604 break;
4606 default:
4607 gfc_error ("Array index at %L is an array of rank %d",
4608 &ar->c_where[i], e->rank);
4609 return false;
4612 /* Fill in the upper bound, which may be lower than the
4613 specified one for something like a(2:10:5), which is
4614 identical to a(2:7:5). Only relevant for strides not equal
4615 to one. Don't try a division by zero. */
4616 if (ar->dimen_type[i] == DIMEN_RANGE
4617 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4618 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4619 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4621 mpz_t size, end;
4623 if (gfc_ref_dimen_size (ar, i, &size, &end))
4625 if (ar->end[i] == NULL)
4627 ar->end[i] =
4628 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4629 &ar->where);
4630 mpz_set (ar->end[i]->value.integer, end);
4632 else if (ar->end[i]->ts.type == BT_INTEGER
4633 && ar->end[i]->expr_type == EXPR_CONSTANT)
4635 mpz_set (ar->end[i]->value.integer, end);
4637 else
4638 gcc_unreachable ();
4640 mpz_clear (size);
4641 mpz_clear (end);
4646 if (ar->type == AR_FULL)
4648 if (ar->as->rank == 0)
4649 ar->type = AR_ELEMENT;
4651 /* Make sure array is the same as array(:,:), this way
4652 we don't need to special case all the time. */
4653 ar->dimen = ar->as->rank;
4654 for (i = 0; i < ar->dimen; i++)
4656 ar->dimen_type[i] = DIMEN_RANGE;
4658 gcc_assert (ar->start[i] == NULL);
4659 gcc_assert (ar->end[i] == NULL);
4660 gcc_assert (ar->stride[i] == NULL);
4664 /* If the reference type is unknown, figure out what kind it is. */
4666 if (ar->type == AR_UNKNOWN)
4668 ar->type = AR_ELEMENT;
4669 for (i = 0; i < ar->dimen; i++)
4670 if (ar->dimen_type[i] == DIMEN_RANGE
4671 || ar->dimen_type[i] == DIMEN_VECTOR)
4673 ar->type = AR_SECTION;
4674 break;
4678 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4679 return false;
4681 if (ar->as->corank && ar->codimen == 0)
4683 int n;
4684 ar->codimen = ar->as->corank;
4685 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4686 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4689 return true;
4693 static bool
4694 resolve_substring (gfc_ref *ref)
4696 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4698 if (ref->u.ss.start != NULL)
4700 if (!gfc_resolve_expr (ref->u.ss.start))
4701 return false;
4703 if (ref->u.ss.start->ts.type != BT_INTEGER)
4705 gfc_error ("Substring start index at %L must be of type INTEGER",
4706 &ref->u.ss.start->where);
4707 return false;
4710 if (ref->u.ss.start->rank != 0)
4712 gfc_error ("Substring start index at %L must be scalar",
4713 &ref->u.ss.start->where);
4714 return false;
4717 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4718 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4719 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4721 gfc_error ("Substring start index at %L is less than one",
4722 &ref->u.ss.start->where);
4723 return false;
4727 if (ref->u.ss.end != NULL)
4729 if (!gfc_resolve_expr (ref->u.ss.end))
4730 return false;
4732 if (ref->u.ss.end->ts.type != BT_INTEGER)
4734 gfc_error ("Substring end index at %L must be of type INTEGER",
4735 &ref->u.ss.end->where);
4736 return false;
4739 if (ref->u.ss.end->rank != 0)
4741 gfc_error ("Substring end index at %L must be scalar",
4742 &ref->u.ss.end->where);
4743 return false;
4746 if (ref->u.ss.length != NULL
4747 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4748 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4749 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4751 gfc_error ("Substring end index at %L exceeds the string length",
4752 &ref->u.ss.start->where);
4753 return false;
4756 if (compare_bound_mpz_t (ref->u.ss.end,
4757 gfc_integer_kinds[k].huge) == CMP_GT
4758 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4759 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4761 gfc_error ("Substring end index at %L is too large",
4762 &ref->u.ss.end->where);
4763 return false;
4767 return true;
4771 /* This function supplies missing substring charlens. */
4773 void
4774 gfc_resolve_substring_charlen (gfc_expr *e)
4776 gfc_ref *char_ref;
4777 gfc_expr *start, *end;
4778 gfc_typespec *ts = NULL;
4780 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4782 if (char_ref->type == REF_SUBSTRING)
4783 break;
4784 if (char_ref->type == REF_COMPONENT)
4785 ts = &char_ref->u.c.component->ts;
4788 if (!char_ref)
4789 return;
4791 gcc_assert (char_ref->next == NULL);
4793 if (e->ts.u.cl)
4795 if (e->ts.u.cl->length)
4796 gfc_free_expr (e->ts.u.cl->length);
4797 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
4798 return;
4801 e->ts.type = BT_CHARACTER;
4802 e->ts.kind = gfc_default_character_kind;
4804 if (!e->ts.u.cl)
4805 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4807 if (char_ref->u.ss.start)
4808 start = gfc_copy_expr (char_ref->u.ss.start);
4809 else
4810 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4812 if (char_ref->u.ss.end)
4813 end = gfc_copy_expr (char_ref->u.ss.end);
4814 else if (e->expr_type == EXPR_VARIABLE)
4816 if (!ts)
4817 ts = &e->symtree->n.sym->ts;
4818 end = gfc_copy_expr (ts->u.cl->length);
4820 else
4821 end = NULL;
4823 if (!start || !end)
4825 gfc_free_expr (start);
4826 gfc_free_expr (end);
4827 return;
4830 /* Length = (end - start + 1). */
4831 e->ts.u.cl->length = gfc_subtract (end, start);
4832 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4833 gfc_get_int_expr (gfc_default_integer_kind,
4834 NULL, 1));
4836 /* F2008, 6.4.1: Both the starting point and the ending point shall
4837 be within the range 1, 2, ..., n unless the starting point exceeds
4838 the ending point, in which case the substring has length zero. */
4840 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
4841 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
4843 e->ts.u.cl->length->ts.type = BT_INTEGER;
4844 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4846 /* Make sure that the length is simplified. */
4847 gfc_simplify_expr (e->ts.u.cl->length, 1);
4848 gfc_resolve_expr (e->ts.u.cl->length);
4852 /* Resolve subtype references. */
4854 static bool
4855 resolve_ref (gfc_expr *expr)
4857 int current_part_dimension, n_components, seen_part_dimension;
4858 gfc_ref *ref;
4860 for (ref = expr->ref; ref; ref = ref->next)
4861 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4863 find_array_spec (expr);
4864 break;
4867 for (ref = expr->ref; ref; ref = ref->next)
4868 switch (ref->type)
4870 case REF_ARRAY:
4871 if (!resolve_array_ref (&ref->u.ar))
4872 return false;
4873 break;
4875 case REF_COMPONENT:
4876 break;
4878 case REF_SUBSTRING:
4879 if (!resolve_substring (ref))
4880 return false;
4881 break;
4884 /* Check constraints on part references. */
4886 current_part_dimension = 0;
4887 seen_part_dimension = 0;
4888 n_components = 0;
4890 for (ref = expr->ref; ref; ref = ref->next)
4892 switch (ref->type)
4894 case REF_ARRAY:
4895 switch (ref->u.ar.type)
4897 case AR_FULL:
4898 /* Coarray scalar. */
4899 if (ref->u.ar.as->rank == 0)
4901 current_part_dimension = 0;
4902 break;
4904 /* Fall through. */
4905 case AR_SECTION:
4906 current_part_dimension = 1;
4907 break;
4909 case AR_ELEMENT:
4910 current_part_dimension = 0;
4911 break;
4913 case AR_UNKNOWN:
4914 gfc_internal_error ("resolve_ref(): Bad array reference");
4917 break;
4919 case REF_COMPONENT:
4920 if (current_part_dimension || seen_part_dimension)
4922 /* F03:C614. */
4923 if (ref->u.c.component->attr.pointer
4924 || ref->u.c.component->attr.proc_pointer
4925 || (ref->u.c.component->ts.type == BT_CLASS
4926 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4928 gfc_error ("Component to the right of a part reference "
4929 "with nonzero rank must not have the POINTER "
4930 "attribute at %L", &expr->where);
4931 return false;
4933 else if (ref->u.c.component->attr.allocatable
4934 || (ref->u.c.component->ts.type == BT_CLASS
4935 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4938 gfc_error ("Component to the right of a part reference "
4939 "with nonzero rank must not have the ALLOCATABLE "
4940 "attribute at %L", &expr->where);
4941 return false;
4945 n_components++;
4946 break;
4948 case REF_SUBSTRING:
4949 break;
4952 if (((ref->type == REF_COMPONENT && n_components > 1)
4953 || ref->next == NULL)
4954 && current_part_dimension
4955 && seen_part_dimension)
4957 gfc_error ("Two or more part references with nonzero rank must "
4958 "not be specified at %L", &expr->where);
4959 return false;
4962 if (ref->type == REF_COMPONENT)
4964 if (current_part_dimension)
4965 seen_part_dimension = 1;
4967 /* reset to make sure */
4968 current_part_dimension = 0;
4972 return true;
4976 /* Given an expression, determine its shape. This is easier than it sounds.
4977 Leaves the shape array NULL if it is not possible to determine the shape. */
4979 static void
4980 expression_shape (gfc_expr *e)
4982 mpz_t array[GFC_MAX_DIMENSIONS];
4983 int i;
4985 if (e->rank <= 0 || e->shape != NULL)
4986 return;
4988 for (i = 0; i < e->rank; i++)
4989 if (!gfc_array_dimen_size (e, i, &array[i]))
4990 goto fail;
4992 e->shape = gfc_get_shape (e->rank);
4994 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4996 return;
4998 fail:
4999 for (i--; i >= 0; i--)
5000 mpz_clear (array[i]);
5004 /* Given a variable expression node, compute the rank of the expression by
5005 examining the base symbol and any reference structures it may have. */
5007 void
5008 expression_rank (gfc_expr *e)
5010 gfc_ref *ref;
5011 int i, rank;
5013 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
5014 could lead to serious confusion... */
5015 gcc_assert (e->expr_type != EXPR_COMPCALL);
5017 if (e->ref == NULL)
5019 if (e->expr_type == EXPR_ARRAY)
5020 goto done;
5021 /* Constructors can have a rank different from one via RESHAPE(). */
5023 if (e->symtree == NULL)
5025 e->rank = 0;
5026 goto done;
5029 e->rank = (e->symtree->n.sym->as == NULL)
5030 ? 0 : e->symtree->n.sym->as->rank;
5031 goto done;
5034 rank = 0;
5036 for (ref = e->ref; ref; ref = ref->next)
5038 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
5039 && ref->u.c.component->attr.function && !ref->next)
5040 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
5042 if (ref->type != REF_ARRAY)
5043 continue;
5045 if (ref->u.ar.type == AR_FULL)
5047 rank = ref->u.ar.as->rank;
5048 break;
5051 if (ref->u.ar.type == AR_SECTION)
5053 /* Figure out the rank of the section. */
5054 if (rank != 0)
5055 gfc_internal_error ("expression_rank(): Two array specs");
5057 for (i = 0; i < ref->u.ar.dimen; i++)
5058 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5059 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
5060 rank++;
5062 break;
5066 e->rank = rank;
5068 done:
5069 expression_shape (e);
5073 static void
5074 add_caf_get_intrinsic (gfc_expr *e)
5076 gfc_expr *wrapper, *tmp_expr;
5077 gfc_ref *ref;
5078 int n;
5080 for (ref = e->ref; ref; ref = ref->next)
5081 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5082 break;
5083 if (ref == NULL)
5084 return;
5086 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
5087 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
5088 return;
5090 tmp_expr = XCNEW (gfc_expr);
5091 *tmp_expr = *e;
5092 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
5093 "caf_get", tmp_expr->where, 1, tmp_expr);
5094 wrapper->ts = e->ts;
5095 wrapper->rank = e->rank;
5096 if (e->rank)
5097 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
5098 *e = *wrapper;
5099 free (wrapper);
5103 static void
5104 remove_caf_get_intrinsic (gfc_expr *e)
5106 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
5107 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
5108 gfc_expr *e2 = e->value.function.actual->expr;
5109 e->value.function.actual->expr = NULL;
5110 gfc_free_actual_arglist (e->value.function.actual);
5111 gfc_free_shape (&e->shape, e->rank);
5112 *e = *e2;
5113 free (e2);
5117 /* Resolve a variable expression. */
5119 static bool
5120 resolve_variable (gfc_expr *e)
5122 gfc_symbol *sym;
5123 bool t;
5125 t = true;
5127 if (e->symtree == NULL)
5128 return false;
5129 sym = e->symtree->n.sym;
5131 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
5132 as ts.type is set to BT_ASSUMED in resolve_symbol. */
5133 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
5135 if (!actual_arg || inquiry_argument)
5137 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
5138 "be used as actual argument", sym->name, &e->where);
5139 return false;
5142 /* TS 29113, 407b. */
5143 else if (e->ts.type == BT_ASSUMED)
5145 if (!actual_arg)
5147 gfc_error ("Assumed-type variable %s at %L may only be used "
5148 "as actual argument", sym->name, &e->where);
5149 return false;
5151 else if (inquiry_argument && !first_actual_arg)
5153 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5154 for all inquiry functions in resolve_function; the reason is
5155 that the function-name resolution happens too late in that
5156 function. */
5157 gfc_error ("Assumed-type variable %s at %L as actual argument to "
5158 "an inquiry function shall be the first argument",
5159 sym->name, &e->where);
5160 return false;
5163 /* TS 29113, C535b. */
5164 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
5165 && CLASS_DATA (sym)->as
5166 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5167 || (sym->ts.type != BT_CLASS && sym->as
5168 && sym->as->type == AS_ASSUMED_RANK))
5170 if (!actual_arg)
5172 gfc_error ("Assumed-rank variable %s at %L may only be used as "
5173 "actual argument", sym->name, &e->where);
5174 return false;
5176 else if (inquiry_argument && !first_actual_arg)
5178 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5179 for all inquiry functions in resolve_function; the reason is
5180 that the function-name resolution happens too late in that
5181 function. */
5182 gfc_error ("Assumed-rank variable %s at %L as actual argument "
5183 "to an inquiry function shall be the first argument",
5184 sym->name, &e->where);
5185 return false;
5189 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
5190 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5191 && e->ref->next == NULL))
5193 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
5194 "a subobject reference", sym->name, &e->ref->u.ar.where);
5195 return false;
5197 /* TS 29113, 407b. */
5198 else if (e->ts.type == BT_ASSUMED && e->ref
5199 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5200 && e->ref->next == NULL))
5202 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
5203 "reference", sym->name, &e->ref->u.ar.where);
5204 return false;
5207 /* TS 29113, C535b. */
5208 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5209 && CLASS_DATA (sym)->as
5210 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5211 || (sym->ts.type != BT_CLASS && sym->as
5212 && sym->as->type == AS_ASSUMED_RANK))
5213 && e->ref
5214 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5215 && e->ref->next == NULL))
5217 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
5218 "reference", sym->name, &e->ref->u.ar.where);
5219 return false;
5222 /* For variables that are used in an associate (target => object) where
5223 the object's basetype is array valued while the target is scalar,
5224 the ts' type of the component refs is still array valued, which
5225 can't be translated that way. */
5226 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
5227 && sym->assoc->target->ts.type == BT_CLASS
5228 && CLASS_DATA (sym->assoc->target)->as)
5230 gfc_ref *ref = e->ref;
5231 while (ref)
5233 switch (ref->type)
5235 case REF_COMPONENT:
5236 ref->u.c.sym = sym->ts.u.derived;
5237 /* Stop the loop. */
5238 ref = NULL;
5239 break;
5240 default:
5241 ref = ref->next;
5242 break;
5247 /* If this is an associate-name, it may be parsed with an array reference
5248 in error even though the target is scalar. Fail directly in this case.
5249 TODO Understand why class scalar expressions must be excluded. */
5250 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5252 if (sym->ts.type == BT_CLASS)
5253 gfc_fix_class_refs (e);
5254 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5255 return false;
5258 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5259 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5261 /* On the other hand, the parser may not have known this is an array;
5262 in this case, we have to add a FULL reference. */
5263 if (sym->assoc && sym->attr.dimension && !e->ref)
5265 e->ref = gfc_get_ref ();
5266 e->ref->type = REF_ARRAY;
5267 e->ref->u.ar.type = AR_FULL;
5268 e->ref->u.ar.dimen = 0;
5271 /* Like above, but for class types, where the checking whether an array
5272 ref is present is more complicated. Furthermore make sure not to add
5273 the full array ref to _vptr or _len refs. */
5274 if (sym->assoc && sym->ts.type == BT_CLASS
5275 && CLASS_DATA (sym)->attr.dimension
5276 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
5278 gfc_ref *ref, *newref;
5280 newref = gfc_get_ref ();
5281 newref->type = REF_ARRAY;
5282 newref->u.ar.type = AR_FULL;
5283 newref->u.ar.dimen = 0;
5284 /* Because this is an associate var and the first ref either is a ref to
5285 the _data component or not, no traversal of the ref chain is
5286 needed. The array ref needs to be inserted after the _data ref,
5287 or when that is not present, which may happend for polymorphic
5288 types, then at the first position. */
5289 ref = e->ref;
5290 if (!ref)
5291 e->ref = newref;
5292 else if (ref->type == REF_COMPONENT
5293 && strcmp ("_data", ref->u.c.component->name) == 0)
5295 if (!ref->next || ref->next->type != REF_ARRAY)
5297 newref->next = ref->next;
5298 ref->next = newref;
5300 else
5301 /* Array ref present already. */
5302 gfc_free_ref_list (newref);
5304 else if (ref->type == REF_ARRAY)
5305 /* Array ref present already. */
5306 gfc_free_ref_list (newref);
5307 else
5309 newref->next = ref;
5310 e->ref = newref;
5314 if (e->ref && !resolve_ref (e))
5315 return false;
5317 if (sym->attr.flavor == FL_PROCEDURE
5318 && (!sym->attr.function
5319 || (sym->attr.function && sym->result
5320 && sym->result->attr.proc_pointer
5321 && !sym->result->attr.function)))
5323 e->ts.type = BT_PROCEDURE;
5324 goto resolve_procedure;
5327 if (sym->ts.type != BT_UNKNOWN)
5328 gfc_variable_attr (e, &e->ts);
5329 else if (sym->attr.flavor == FL_PROCEDURE
5330 && sym->attr.function && sym->result
5331 && sym->result->ts.type != BT_UNKNOWN
5332 && sym->result->attr.proc_pointer)
5333 e->ts = sym->result->ts;
5334 else
5336 /* Must be a simple variable reference. */
5337 if (!gfc_set_default_type (sym, 1, sym->ns))
5338 return false;
5339 e->ts = sym->ts;
5342 if (check_assumed_size_reference (sym, e))
5343 return false;
5345 /* Deal with forward references to entries during gfc_resolve_code, to
5346 satisfy, at least partially, 12.5.2.5. */
5347 if (gfc_current_ns->entries
5348 && current_entry_id == sym->entry_id
5349 && cs_base
5350 && cs_base->current
5351 && cs_base->current->op != EXEC_ENTRY)
5353 gfc_entry_list *entry;
5354 gfc_formal_arglist *formal;
5355 int n;
5356 bool seen, saved_specification_expr;
5358 /* If the symbol is a dummy... */
5359 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5361 entry = gfc_current_ns->entries;
5362 seen = false;
5364 /* ...test if the symbol is a parameter of previous entries. */
5365 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5366 for (formal = entry->sym->formal; formal; formal = formal->next)
5368 if (formal->sym && sym->name == formal->sym->name)
5370 seen = true;
5371 break;
5375 /* If it has not been seen as a dummy, this is an error. */
5376 if (!seen)
5378 if (specification_expr)
5379 gfc_error ("Variable %qs, used in a specification expression"
5380 ", is referenced at %L before the ENTRY statement "
5381 "in which it is a parameter",
5382 sym->name, &cs_base->current->loc);
5383 else
5384 gfc_error ("Variable %qs is used at %L before the ENTRY "
5385 "statement in which it is a parameter",
5386 sym->name, &cs_base->current->loc);
5387 t = false;
5391 /* Now do the same check on the specification expressions. */
5392 saved_specification_expr = specification_expr;
5393 specification_expr = true;
5394 if (sym->ts.type == BT_CHARACTER
5395 && !gfc_resolve_expr (sym->ts.u.cl->length))
5396 t = false;
5398 if (sym->as)
5399 for (n = 0; n < sym->as->rank; n++)
5401 if (!gfc_resolve_expr (sym->as->lower[n]))
5402 t = false;
5403 if (!gfc_resolve_expr (sym->as->upper[n]))
5404 t = false;
5406 specification_expr = saved_specification_expr;
5408 if (t)
5409 /* Update the symbol's entry level. */
5410 sym->entry_id = current_entry_id + 1;
5413 /* If a symbol has been host_associated mark it. This is used latter,
5414 to identify if aliasing is possible via host association. */
5415 if (sym->attr.flavor == FL_VARIABLE
5416 && gfc_current_ns->parent
5417 && (gfc_current_ns->parent == sym->ns
5418 || (gfc_current_ns->parent->parent
5419 && gfc_current_ns->parent->parent == sym->ns)))
5420 sym->attr.host_assoc = 1;
5422 if (gfc_current_ns->proc_name
5423 && sym->attr.dimension
5424 && (sym->ns != gfc_current_ns
5425 || sym->attr.use_assoc
5426 || sym->attr.in_common))
5427 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
5429 resolve_procedure:
5430 if (t && !resolve_procedure_expression (e))
5431 t = false;
5433 /* F2008, C617 and C1229. */
5434 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5435 && gfc_is_coindexed (e))
5437 gfc_ref *ref, *ref2 = NULL;
5439 for (ref = e->ref; ref; ref = ref->next)
5441 if (ref->type == REF_COMPONENT)
5442 ref2 = ref;
5443 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5444 break;
5447 for ( ; ref; ref = ref->next)
5448 if (ref->type == REF_COMPONENT)
5449 break;
5451 /* Expression itself is not coindexed object. */
5452 if (ref && e->ts.type == BT_CLASS)
5454 gfc_error ("Polymorphic subobject of coindexed object at %L",
5455 &e->where);
5456 t = false;
5459 /* Expression itself is coindexed object. */
5460 if (ref == NULL)
5462 gfc_component *c;
5463 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5464 for ( ; c; c = c->next)
5465 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5467 gfc_error ("Coindexed object with polymorphic allocatable "
5468 "subcomponent at %L", &e->where);
5469 t = false;
5470 break;
5475 if (t)
5476 expression_rank (e);
5478 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
5479 add_caf_get_intrinsic (e);
5481 return t;
5485 /* Checks to see that the correct symbol has been host associated.
5486 The only situation where this arises is that in which a twice
5487 contained function is parsed after the host association is made.
5488 Therefore, on detecting this, change the symbol in the expression
5489 and convert the array reference into an actual arglist if the old
5490 symbol is a variable. */
5491 static bool
5492 check_host_association (gfc_expr *e)
5494 gfc_symbol *sym, *old_sym;
5495 gfc_symtree *st;
5496 int n;
5497 gfc_ref *ref;
5498 gfc_actual_arglist *arg, *tail = NULL;
5499 bool retval = e->expr_type == EXPR_FUNCTION;
5501 /* If the expression is the result of substitution in
5502 interface.c(gfc_extend_expr) because there is no way in
5503 which the host association can be wrong. */
5504 if (e->symtree == NULL
5505 || e->symtree->n.sym == NULL
5506 || e->user_operator)
5507 return retval;
5509 old_sym = e->symtree->n.sym;
5511 if (gfc_current_ns->parent
5512 && old_sym->ns != gfc_current_ns)
5514 /* Use the 'USE' name so that renamed module symbols are
5515 correctly handled. */
5516 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5518 if (sym && old_sym != sym
5519 && sym->ts.type == old_sym->ts.type
5520 && sym->attr.flavor == FL_PROCEDURE
5521 && sym->attr.contained)
5523 /* Clear the shape, since it might not be valid. */
5524 gfc_free_shape (&e->shape, e->rank);
5526 /* Give the expression the right symtree! */
5527 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5528 gcc_assert (st != NULL);
5530 if (old_sym->attr.flavor == FL_PROCEDURE
5531 || e->expr_type == EXPR_FUNCTION)
5533 /* Original was function so point to the new symbol, since
5534 the actual argument list is already attached to the
5535 expression. */
5536 e->value.function.esym = NULL;
5537 e->symtree = st;
5539 else
5541 /* Original was variable so convert array references into
5542 an actual arglist. This does not need any checking now
5543 since resolve_function will take care of it. */
5544 e->value.function.actual = NULL;
5545 e->expr_type = EXPR_FUNCTION;
5546 e->symtree = st;
5548 /* Ambiguity will not arise if the array reference is not
5549 the last reference. */
5550 for (ref = e->ref; ref; ref = ref->next)
5551 if (ref->type == REF_ARRAY && ref->next == NULL)
5552 break;
5554 gcc_assert (ref->type == REF_ARRAY);
5556 /* Grab the start expressions from the array ref and
5557 copy them into actual arguments. */
5558 for (n = 0; n < ref->u.ar.dimen; n++)
5560 arg = gfc_get_actual_arglist ();
5561 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5562 if (e->value.function.actual == NULL)
5563 tail = e->value.function.actual = arg;
5564 else
5566 tail->next = arg;
5567 tail = arg;
5571 /* Dump the reference list and set the rank. */
5572 gfc_free_ref_list (e->ref);
5573 e->ref = NULL;
5574 e->rank = sym->as ? sym->as->rank : 0;
5577 gfc_resolve_expr (e);
5578 sym->refs++;
5581 /* This might have changed! */
5582 return e->expr_type == EXPR_FUNCTION;
5586 static void
5587 gfc_resolve_character_operator (gfc_expr *e)
5589 gfc_expr *op1 = e->value.op.op1;
5590 gfc_expr *op2 = e->value.op.op2;
5591 gfc_expr *e1 = NULL;
5592 gfc_expr *e2 = NULL;
5594 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5596 if (op1->ts.u.cl && op1->ts.u.cl->length)
5597 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5598 else if (op1->expr_type == EXPR_CONSTANT)
5599 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5600 op1->value.character.length);
5602 if (op2->ts.u.cl && op2->ts.u.cl->length)
5603 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5604 else if (op2->expr_type == EXPR_CONSTANT)
5605 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5606 op2->value.character.length);
5608 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5610 if (!e1 || !e2)
5612 gfc_free_expr (e1);
5613 gfc_free_expr (e2);
5615 return;
5618 e->ts.u.cl->length = gfc_add (e1, e2);
5619 e->ts.u.cl->length->ts.type = BT_INTEGER;
5620 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5621 gfc_simplify_expr (e->ts.u.cl->length, 0);
5622 gfc_resolve_expr (e->ts.u.cl->length);
5624 return;
5628 /* Ensure that an character expression has a charlen and, if possible, a
5629 length expression. */
5631 static void
5632 fixup_charlen (gfc_expr *e)
5634 /* The cases fall through so that changes in expression type and the need
5635 for multiple fixes are picked up. In all circumstances, a charlen should
5636 be available for the middle end to hang a backend_decl on. */
5637 switch (e->expr_type)
5639 case EXPR_OP:
5640 gfc_resolve_character_operator (e);
5641 /* FALLTHRU */
5643 case EXPR_ARRAY:
5644 if (e->expr_type == EXPR_ARRAY)
5645 gfc_resolve_character_array_constructor (e);
5646 /* FALLTHRU */
5648 case EXPR_SUBSTRING:
5649 if (!e->ts.u.cl && e->ref)
5650 gfc_resolve_substring_charlen (e);
5651 /* FALLTHRU */
5653 default:
5654 if (!e->ts.u.cl)
5655 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5657 break;
5662 /* Update an actual argument to include the passed-object for type-bound
5663 procedures at the right position. */
5665 static gfc_actual_arglist*
5666 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5667 const char *name)
5669 gcc_assert (argpos > 0);
5671 if (argpos == 1)
5673 gfc_actual_arglist* result;
5675 result = gfc_get_actual_arglist ();
5676 result->expr = po;
5677 result->next = lst;
5678 if (name)
5679 result->name = name;
5681 return result;
5684 if (lst)
5685 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5686 else
5687 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5688 return lst;
5692 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5694 static gfc_expr*
5695 extract_compcall_passed_object (gfc_expr* e)
5697 gfc_expr* po;
5699 gcc_assert (e->expr_type == EXPR_COMPCALL);
5701 if (e->value.compcall.base_object)
5702 po = gfc_copy_expr (e->value.compcall.base_object);
5703 else
5705 po = gfc_get_expr ();
5706 po->expr_type = EXPR_VARIABLE;
5707 po->symtree = e->symtree;
5708 po->ref = gfc_copy_ref (e->ref);
5709 po->where = e->where;
5712 if (!gfc_resolve_expr (po))
5713 return NULL;
5715 return po;
5719 /* Update the arglist of an EXPR_COMPCALL expression to include the
5720 passed-object. */
5722 static bool
5723 update_compcall_arglist (gfc_expr* e)
5725 gfc_expr* po;
5726 gfc_typebound_proc* tbp;
5728 tbp = e->value.compcall.tbp;
5730 if (tbp->error)
5731 return false;
5733 po = extract_compcall_passed_object (e);
5734 if (!po)
5735 return false;
5737 if (tbp->nopass || e->value.compcall.ignore_pass)
5739 gfc_free_expr (po);
5740 return true;
5743 gcc_assert (tbp->pass_arg_num > 0);
5744 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5745 tbp->pass_arg_num,
5746 tbp->pass_arg);
5748 return true;
5752 /* Extract the passed object from a PPC call (a copy of it). */
5754 static gfc_expr*
5755 extract_ppc_passed_object (gfc_expr *e)
5757 gfc_expr *po;
5758 gfc_ref **ref;
5760 po = gfc_get_expr ();
5761 po->expr_type = EXPR_VARIABLE;
5762 po->symtree = e->symtree;
5763 po->ref = gfc_copy_ref (e->ref);
5764 po->where = e->where;
5766 /* Remove PPC reference. */
5767 ref = &po->ref;
5768 while ((*ref)->next)
5769 ref = &(*ref)->next;
5770 gfc_free_ref_list (*ref);
5771 *ref = NULL;
5773 if (!gfc_resolve_expr (po))
5774 return NULL;
5776 return po;
5780 /* Update the actual arglist of a procedure pointer component to include the
5781 passed-object. */
5783 static bool
5784 update_ppc_arglist (gfc_expr* e)
5786 gfc_expr* po;
5787 gfc_component *ppc;
5788 gfc_typebound_proc* tb;
5790 ppc = gfc_get_proc_ptr_comp (e);
5791 if (!ppc)
5792 return false;
5794 tb = ppc->tb;
5796 if (tb->error)
5797 return false;
5798 else if (tb->nopass)
5799 return true;
5801 po = extract_ppc_passed_object (e);
5802 if (!po)
5803 return false;
5805 /* F08:R739. */
5806 if (po->rank != 0)
5808 gfc_error ("Passed-object at %L must be scalar", &e->where);
5809 return false;
5812 /* F08:C611. */
5813 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5815 gfc_error ("Base object for procedure-pointer component call at %L is of"
5816 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
5817 return false;
5820 gcc_assert (tb->pass_arg_num > 0);
5821 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5822 tb->pass_arg_num,
5823 tb->pass_arg);
5825 return true;
5829 /* Check that the object a TBP is called on is valid, i.e. it must not be
5830 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5832 static bool
5833 check_typebound_baseobject (gfc_expr* e)
5835 gfc_expr* base;
5836 bool return_value = false;
5838 base = extract_compcall_passed_object (e);
5839 if (!base)
5840 return false;
5842 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5844 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
5845 return false;
5847 /* F08:C611. */
5848 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5850 gfc_error ("Base object for type-bound procedure call at %L is of"
5851 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
5852 goto cleanup;
5855 /* F08:C1230. If the procedure called is NOPASS,
5856 the base object must be scalar. */
5857 if (e->value.compcall.tbp->nopass && base->rank != 0)
5859 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5860 " be scalar", &e->where);
5861 goto cleanup;
5864 return_value = true;
5866 cleanup:
5867 gfc_free_expr (base);
5868 return return_value;
5872 /* Resolve a call to a type-bound procedure, either function or subroutine,
5873 statically from the data in an EXPR_COMPCALL expression. The adapted
5874 arglist and the target-procedure symtree are returned. */
5876 static bool
5877 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5878 gfc_actual_arglist** actual)
5880 gcc_assert (e->expr_type == EXPR_COMPCALL);
5881 gcc_assert (!e->value.compcall.tbp->is_generic);
5883 /* Update the actual arglist for PASS. */
5884 if (!update_compcall_arglist (e))
5885 return false;
5887 *actual = e->value.compcall.actual;
5888 *target = e->value.compcall.tbp->u.specific;
5890 gfc_free_ref_list (e->ref);
5891 e->ref = NULL;
5892 e->value.compcall.actual = NULL;
5894 /* If we find a deferred typebound procedure, check for derived types
5895 that an overriding typebound procedure has not been missed. */
5896 if (e->value.compcall.name
5897 && !e->value.compcall.tbp->non_overridable
5898 && e->value.compcall.base_object
5899 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5901 gfc_symtree *st;
5902 gfc_symbol *derived;
5904 /* Use the derived type of the base_object. */
5905 derived = e->value.compcall.base_object->ts.u.derived;
5906 st = NULL;
5908 /* If necessary, go through the inheritance chain. */
5909 while (!st && derived)
5911 /* Look for the typebound procedure 'name'. */
5912 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5913 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5914 e->value.compcall.name);
5915 if (!st)
5916 derived = gfc_get_derived_super_type (derived);
5919 /* Now find the specific name in the derived type namespace. */
5920 if (st && st->n.tb && st->n.tb->u.specific)
5921 gfc_find_sym_tree (st->n.tb->u.specific->name,
5922 derived->ns, 1, &st);
5923 if (st)
5924 *target = st;
5926 return true;
5930 /* Get the ultimate declared type from an expression. In addition,
5931 return the last class/derived type reference and the copy of the
5932 reference list. If check_types is set true, derived types are
5933 identified as well as class references. */
5934 static gfc_symbol*
5935 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5936 gfc_expr *e, bool check_types)
5938 gfc_symbol *declared;
5939 gfc_ref *ref;
5941 declared = NULL;
5942 if (class_ref)
5943 *class_ref = NULL;
5944 if (new_ref)
5945 *new_ref = gfc_copy_ref (e->ref);
5947 for (ref = e->ref; ref; ref = ref->next)
5949 if (ref->type != REF_COMPONENT)
5950 continue;
5952 if ((ref->u.c.component->ts.type == BT_CLASS
5953 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
5954 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5956 declared = ref->u.c.component->ts.u.derived;
5957 if (class_ref)
5958 *class_ref = ref;
5962 if (declared == NULL)
5963 declared = e->symtree->n.sym->ts.u.derived;
5965 return declared;
5969 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5970 which of the specific bindings (if any) matches the arglist and transform
5971 the expression into a call of that binding. */
5973 static bool
5974 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5976 gfc_typebound_proc* genproc;
5977 const char* genname;
5978 gfc_symtree *st;
5979 gfc_symbol *derived;
5981 gcc_assert (e->expr_type == EXPR_COMPCALL);
5982 genname = e->value.compcall.name;
5983 genproc = e->value.compcall.tbp;
5985 if (!genproc->is_generic)
5986 return true;
5988 /* Try the bindings on this type and in the inheritance hierarchy. */
5989 for (; genproc; genproc = genproc->overridden)
5991 gfc_tbp_generic* g;
5993 gcc_assert (genproc->is_generic);
5994 for (g = genproc->u.generic; g; g = g->next)
5996 gfc_symbol* target;
5997 gfc_actual_arglist* args;
5998 bool matches;
6000 gcc_assert (g->specific);
6002 if (g->specific->error)
6003 continue;
6005 target = g->specific->u.specific->n.sym;
6007 /* Get the right arglist by handling PASS/NOPASS. */
6008 args = gfc_copy_actual_arglist (e->value.compcall.actual);
6009 if (!g->specific->nopass)
6011 gfc_expr* po;
6012 po = extract_compcall_passed_object (e);
6013 if (!po)
6015 gfc_free_actual_arglist (args);
6016 return false;
6019 gcc_assert (g->specific->pass_arg_num > 0);
6020 gcc_assert (!g->specific->error);
6021 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
6022 g->specific->pass_arg);
6024 resolve_actual_arglist (args, target->attr.proc,
6025 is_external_proc (target)
6026 && gfc_sym_get_dummy_args (target) == NULL);
6028 /* Check if this arglist matches the formal. */
6029 matches = gfc_arglist_matches_symbol (&args, target);
6031 /* Clean up and break out of the loop if we've found it. */
6032 gfc_free_actual_arglist (args);
6033 if (matches)
6035 e->value.compcall.tbp = g->specific;
6036 genname = g->specific_st->name;
6037 /* Pass along the name for CLASS methods, where the vtab
6038 procedure pointer component has to be referenced. */
6039 if (name)
6040 *name = genname;
6041 goto success;
6046 /* Nothing matching found! */
6047 gfc_error ("Found no matching specific binding for the call to the GENERIC"
6048 " %qs at %L", genname, &e->where);
6049 return false;
6051 success:
6052 /* Make sure that we have the right specific instance for the name. */
6053 derived = get_declared_from_expr (NULL, NULL, e, true);
6055 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
6056 if (st)
6057 e->value.compcall.tbp = st->n.tb;
6059 return true;
6063 /* Resolve a call to a type-bound subroutine. */
6065 static bool
6066 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
6068 gfc_actual_arglist* newactual;
6069 gfc_symtree* target;
6071 /* Check that's really a SUBROUTINE. */
6072 if (!c->expr1->value.compcall.tbp->subroutine)
6074 gfc_error ("%qs at %L should be a SUBROUTINE",
6075 c->expr1->value.compcall.name, &c->loc);
6076 return false;
6079 if (!check_typebound_baseobject (c->expr1))
6080 return false;
6082 /* Pass along the name for CLASS methods, where the vtab
6083 procedure pointer component has to be referenced. */
6084 if (name)
6085 *name = c->expr1->value.compcall.name;
6087 if (!resolve_typebound_generic_call (c->expr1, name))
6088 return false;
6090 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
6091 if (overridable)
6092 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
6094 /* Transform into an ordinary EXEC_CALL for now. */
6096 if (!resolve_typebound_static (c->expr1, &target, &newactual))
6097 return false;
6099 c->ext.actual = newactual;
6100 c->symtree = target;
6101 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
6103 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
6105 gfc_free_expr (c->expr1);
6106 c->expr1 = gfc_get_expr ();
6107 c->expr1->expr_type = EXPR_FUNCTION;
6108 c->expr1->symtree = target;
6109 c->expr1->where = c->loc;
6111 return resolve_call (c);
6115 /* Resolve a component-call expression. */
6116 static bool
6117 resolve_compcall (gfc_expr* e, const char **name)
6119 gfc_actual_arglist* newactual;
6120 gfc_symtree* target;
6122 /* Check that's really a FUNCTION. */
6123 if (!e->value.compcall.tbp->function)
6125 gfc_error ("%qs at %L should be a FUNCTION",
6126 e->value.compcall.name, &e->where);
6127 return false;
6130 /* These must not be assign-calls! */
6131 gcc_assert (!e->value.compcall.assign);
6133 if (!check_typebound_baseobject (e))
6134 return false;
6136 /* Pass along the name for CLASS methods, where the vtab
6137 procedure pointer component has to be referenced. */
6138 if (name)
6139 *name = e->value.compcall.name;
6141 if (!resolve_typebound_generic_call (e, name))
6142 return false;
6143 gcc_assert (!e->value.compcall.tbp->is_generic);
6145 /* Take the rank from the function's symbol. */
6146 if (e->value.compcall.tbp->u.specific->n.sym->as)
6147 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
6149 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
6150 arglist to the TBP's binding target. */
6152 if (!resolve_typebound_static (e, &target, &newactual))
6153 return false;
6155 e->value.function.actual = newactual;
6156 e->value.function.name = NULL;
6157 e->value.function.esym = target->n.sym;
6158 e->value.function.isym = NULL;
6159 e->symtree = target;
6160 e->ts = target->n.sym->ts;
6161 e->expr_type = EXPR_FUNCTION;
6163 /* Resolution is not necessary if this is a class subroutine; this
6164 function only has to identify the specific proc. Resolution of
6165 the call will be done next in resolve_typebound_call. */
6166 return gfc_resolve_expr (e);
6170 static bool resolve_fl_derived (gfc_symbol *sym);
6173 /* Resolve a typebound function, or 'method'. First separate all
6174 the non-CLASS references by calling resolve_compcall directly. */
6176 static bool
6177 resolve_typebound_function (gfc_expr* e)
6179 gfc_symbol *declared;
6180 gfc_component *c;
6181 gfc_ref *new_ref;
6182 gfc_ref *class_ref;
6183 gfc_symtree *st;
6184 const char *name;
6185 gfc_typespec ts;
6186 gfc_expr *expr;
6187 bool overridable;
6189 st = e->symtree;
6191 /* Deal with typebound operators for CLASS objects. */
6192 expr = e->value.compcall.base_object;
6193 overridable = !e->value.compcall.tbp->non_overridable;
6194 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
6196 /* If the base_object is not a variable, the corresponding actual
6197 argument expression must be stored in e->base_expression so
6198 that the corresponding tree temporary can be used as the base
6199 object in gfc_conv_procedure_call. */
6200 if (expr->expr_type != EXPR_VARIABLE)
6202 gfc_actual_arglist *args;
6204 for (args= e->value.function.actual; args; args = args->next)
6206 if (expr == args->expr)
6207 expr = args->expr;
6211 /* Since the typebound operators are generic, we have to ensure
6212 that any delays in resolution are corrected and that the vtab
6213 is present. */
6214 ts = expr->ts;
6215 declared = ts.u.derived;
6216 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6217 if (c->ts.u.derived == NULL)
6218 c->ts.u.derived = gfc_find_derived_vtab (declared);
6220 if (!resolve_compcall (e, &name))
6221 return false;
6223 /* Use the generic name if it is there. */
6224 name = name ? name : e->value.function.esym->name;
6225 e->symtree = expr->symtree;
6226 e->ref = gfc_copy_ref (expr->ref);
6227 get_declared_from_expr (&class_ref, NULL, e, false);
6229 /* Trim away the extraneous references that emerge from nested
6230 use of interface.c (extend_expr). */
6231 if (class_ref && class_ref->next)
6233 gfc_free_ref_list (class_ref->next);
6234 class_ref->next = NULL;
6236 else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
6238 gfc_free_ref_list (e->ref);
6239 e->ref = NULL;
6242 gfc_add_vptr_component (e);
6243 gfc_add_component_ref (e, name);
6244 e->value.function.esym = NULL;
6245 if (expr->expr_type != EXPR_VARIABLE)
6246 e->base_expr = expr;
6247 return true;
6250 if (st == NULL)
6251 return resolve_compcall (e, NULL);
6253 if (!resolve_ref (e))
6254 return false;
6256 /* Get the CLASS declared type. */
6257 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6259 if (!resolve_fl_derived (declared))
6260 return false;
6262 /* Weed out cases of the ultimate component being a derived type. */
6263 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6264 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6266 gfc_free_ref_list (new_ref);
6267 return resolve_compcall (e, NULL);
6270 c = gfc_find_component (declared, "_data", true, true, NULL);
6271 declared = c->ts.u.derived;
6273 /* Treat the call as if it is a typebound procedure, in order to roll
6274 out the correct name for the specific function. */
6275 if (!resolve_compcall (e, &name))
6277 gfc_free_ref_list (new_ref);
6278 return false;
6280 ts = e->ts;
6282 if (overridable)
6284 /* Convert the expression to a procedure pointer component call. */
6285 e->value.function.esym = NULL;
6286 e->symtree = st;
6288 if (new_ref)
6289 e->ref = new_ref;
6291 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6292 gfc_add_vptr_component (e);
6293 gfc_add_component_ref (e, name);
6295 /* Recover the typespec for the expression. This is really only
6296 necessary for generic procedures, where the additional call
6297 to gfc_add_component_ref seems to throw the collection of the
6298 correct typespec. */
6299 e->ts = ts;
6301 else if (new_ref)
6302 gfc_free_ref_list (new_ref);
6304 return true;
6307 /* Resolve a typebound subroutine, or 'method'. First separate all
6308 the non-CLASS references by calling resolve_typebound_call
6309 directly. */
6311 static bool
6312 resolve_typebound_subroutine (gfc_code *code)
6314 gfc_symbol *declared;
6315 gfc_component *c;
6316 gfc_ref *new_ref;
6317 gfc_ref *class_ref;
6318 gfc_symtree *st;
6319 const char *name;
6320 gfc_typespec ts;
6321 gfc_expr *expr;
6322 bool overridable;
6324 st = code->expr1->symtree;
6326 /* Deal with typebound operators for CLASS objects. */
6327 expr = code->expr1->value.compcall.base_object;
6328 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6329 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6331 /* If the base_object is not a variable, the corresponding actual
6332 argument expression must be stored in e->base_expression so
6333 that the corresponding tree temporary can be used as the base
6334 object in gfc_conv_procedure_call. */
6335 if (expr->expr_type != EXPR_VARIABLE)
6337 gfc_actual_arglist *args;
6339 args= code->expr1->value.function.actual;
6340 for (; args; args = args->next)
6341 if (expr == args->expr)
6342 expr = args->expr;
6345 /* Since the typebound operators are generic, we have to ensure
6346 that any delays in resolution are corrected and that the vtab
6347 is present. */
6348 declared = expr->ts.u.derived;
6349 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6350 if (c->ts.u.derived == NULL)
6351 c->ts.u.derived = gfc_find_derived_vtab (declared);
6353 if (!resolve_typebound_call (code, &name, NULL))
6354 return false;
6356 /* Use the generic name if it is there. */
6357 name = name ? name : code->expr1->value.function.esym->name;
6358 code->expr1->symtree = expr->symtree;
6359 code->expr1->ref = gfc_copy_ref (expr->ref);
6361 /* Trim away the extraneous references that emerge from nested
6362 use of interface.c (extend_expr). */
6363 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6364 if (class_ref && class_ref->next)
6366 gfc_free_ref_list (class_ref->next);
6367 class_ref->next = NULL;
6369 else if (code->expr1->ref && !class_ref)
6371 gfc_free_ref_list (code->expr1->ref);
6372 code->expr1->ref = NULL;
6375 /* Now use the procedure in the vtable. */
6376 gfc_add_vptr_component (code->expr1);
6377 gfc_add_component_ref (code->expr1, name);
6378 code->expr1->value.function.esym = NULL;
6379 if (expr->expr_type != EXPR_VARIABLE)
6380 code->expr1->base_expr = expr;
6381 return true;
6384 if (st == NULL)
6385 return resolve_typebound_call (code, NULL, NULL);
6387 if (!resolve_ref (code->expr1))
6388 return false;
6390 /* Get the CLASS declared type. */
6391 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
6393 /* Weed out cases of the ultimate component being a derived type. */
6394 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6395 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6397 gfc_free_ref_list (new_ref);
6398 return resolve_typebound_call (code, NULL, NULL);
6401 if (!resolve_typebound_call (code, &name, &overridable))
6403 gfc_free_ref_list (new_ref);
6404 return false;
6406 ts = code->expr1->ts;
6408 if (overridable)
6410 /* Convert the expression to a procedure pointer component call. */
6411 code->expr1->value.function.esym = NULL;
6412 code->expr1->symtree = st;
6414 if (new_ref)
6415 code->expr1->ref = new_ref;
6417 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6418 gfc_add_vptr_component (code->expr1);
6419 gfc_add_component_ref (code->expr1, name);
6421 /* Recover the typespec for the expression. This is really only
6422 necessary for generic procedures, where the additional call
6423 to gfc_add_component_ref seems to throw the collection of the
6424 correct typespec. */
6425 code->expr1->ts = ts;
6427 else if (new_ref)
6428 gfc_free_ref_list (new_ref);
6430 return true;
6434 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6436 static bool
6437 resolve_ppc_call (gfc_code* c)
6439 gfc_component *comp;
6441 comp = gfc_get_proc_ptr_comp (c->expr1);
6442 gcc_assert (comp != NULL);
6444 c->resolved_sym = c->expr1->symtree->n.sym;
6445 c->expr1->expr_type = EXPR_VARIABLE;
6447 if (!comp->attr.subroutine)
6448 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6450 if (!resolve_ref (c->expr1))
6451 return false;
6453 if (!update_ppc_arglist (c->expr1))
6454 return false;
6456 c->ext.actual = c->expr1->value.compcall.actual;
6458 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6459 !(comp->ts.interface
6460 && comp->ts.interface->formal)))
6461 return false;
6463 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
6464 return false;
6466 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6468 return true;
6472 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6474 static bool
6475 resolve_expr_ppc (gfc_expr* e)
6477 gfc_component *comp;
6479 comp = gfc_get_proc_ptr_comp (e);
6480 gcc_assert (comp != NULL);
6482 /* Convert to EXPR_FUNCTION. */
6483 e->expr_type = EXPR_FUNCTION;
6484 e->value.function.isym = NULL;
6485 e->value.function.actual = e->value.compcall.actual;
6486 e->ts = comp->ts;
6487 if (comp->as != NULL)
6488 e->rank = comp->as->rank;
6490 if (!comp->attr.function)
6491 gfc_add_function (&comp->attr, comp->name, &e->where);
6493 if (!resolve_ref (e))
6494 return false;
6496 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6497 !(comp->ts.interface
6498 && comp->ts.interface->formal)))
6499 return false;
6501 if (!update_ppc_arglist (e))
6502 return false;
6504 if (!check_pure_function(e))
6505 return false;
6507 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6509 return true;
6513 static bool
6514 gfc_is_expandable_expr (gfc_expr *e)
6516 gfc_constructor *con;
6518 if (e->expr_type == EXPR_ARRAY)
6520 /* Traverse the constructor looking for variables that are flavor
6521 parameter. Parameters must be expanded since they are fully used at
6522 compile time. */
6523 con = gfc_constructor_first (e->value.constructor);
6524 for (; con; con = gfc_constructor_next (con))
6526 if (con->expr->expr_type == EXPR_VARIABLE
6527 && con->expr->symtree
6528 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6529 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6530 return true;
6531 if (con->expr->expr_type == EXPR_ARRAY
6532 && gfc_is_expandable_expr (con->expr))
6533 return true;
6537 return false;
6541 /* Sometimes variables in specification expressions of the result
6542 of module procedures in submodules wind up not being the 'real'
6543 dummy. Find this, if possible, in the namespace of the first
6544 formal argument. */
6546 static void
6547 fixup_unique_dummy (gfc_expr *e)
6549 gfc_symtree *st = NULL;
6550 gfc_symbol *s = NULL;
6552 if (e->symtree->n.sym->ns->proc_name
6553 && e->symtree->n.sym->ns->proc_name->formal)
6554 s = e->symtree->n.sym->ns->proc_name->formal->sym;
6556 if (s != NULL)
6557 st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
6559 if (st != NULL
6560 && st->n.sym != NULL
6561 && st->n.sym->attr.dummy)
6562 e->symtree = st;
6565 /* Resolve an expression. That is, make sure that types of operands agree
6566 with their operators, intrinsic operators are converted to function calls
6567 for overloaded types and unresolved function references are resolved. */
6569 bool
6570 gfc_resolve_expr (gfc_expr *e)
6572 bool t;
6573 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6575 if (e == NULL)
6576 return true;
6578 /* inquiry_argument only applies to variables. */
6579 inquiry_save = inquiry_argument;
6580 actual_arg_save = actual_arg;
6581 first_actual_arg_save = first_actual_arg;
6583 if (e->expr_type != EXPR_VARIABLE)
6585 inquiry_argument = false;
6586 actual_arg = false;
6587 first_actual_arg = false;
6589 else if (e->symtree != NULL
6590 && *e->symtree->name == '@'
6591 && e->symtree->n.sym->attr.dummy)
6593 /* Deal with submodule specification expressions that are not
6594 found to be referenced in module.c(read_cleanup). */
6595 fixup_unique_dummy (e);
6598 switch (e->expr_type)
6600 case EXPR_OP:
6601 t = resolve_operator (e);
6602 break;
6604 case EXPR_FUNCTION:
6605 case EXPR_VARIABLE:
6607 if (check_host_association (e))
6608 t = resolve_function (e);
6609 else
6610 t = resolve_variable (e);
6612 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6613 && e->ref->type != REF_SUBSTRING)
6614 gfc_resolve_substring_charlen (e);
6616 break;
6618 case EXPR_COMPCALL:
6619 t = resolve_typebound_function (e);
6620 break;
6622 case EXPR_SUBSTRING:
6623 t = resolve_ref (e);
6624 break;
6626 case EXPR_CONSTANT:
6627 case EXPR_NULL:
6628 t = true;
6629 break;
6631 case EXPR_PPC:
6632 t = resolve_expr_ppc (e);
6633 break;
6635 case EXPR_ARRAY:
6636 t = false;
6637 if (!resolve_ref (e))
6638 break;
6640 t = gfc_resolve_array_constructor (e);
6641 /* Also try to expand a constructor. */
6642 if (t)
6644 expression_rank (e);
6645 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6646 gfc_expand_constructor (e, false);
6649 /* This provides the opportunity for the length of constructors with
6650 character valued function elements to propagate the string length
6651 to the expression. */
6652 if (t && e->ts.type == BT_CHARACTER)
6654 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6655 here rather then add a duplicate test for it above. */
6656 gfc_expand_constructor (e, false);
6657 t = gfc_resolve_character_array_constructor (e);
6660 break;
6662 case EXPR_STRUCTURE:
6663 t = resolve_ref (e);
6664 if (!t)
6665 break;
6667 t = resolve_structure_cons (e, 0);
6668 if (!t)
6669 break;
6671 t = gfc_simplify_expr (e, 0);
6672 break;
6674 default:
6675 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6678 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
6679 fixup_charlen (e);
6681 inquiry_argument = inquiry_save;
6682 actual_arg = actual_arg_save;
6683 first_actual_arg = first_actual_arg_save;
6685 return t;
6689 /* Resolve an expression from an iterator. They must be scalar and have
6690 INTEGER or (optionally) REAL type. */
6692 static bool
6693 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6694 const char *name_msgid)
6696 if (!gfc_resolve_expr (expr))
6697 return false;
6699 if (expr->rank != 0)
6701 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6702 return false;
6705 if (expr->ts.type != BT_INTEGER)
6707 if (expr->ts.type == BT_REAL)
6709 if (real_ok)
6710 return gfc_notify_std (GFC_STD_F95_DEL,
6711 "%s at %L must be integer",
6712 _(name_msgid), &expr->where);
6713 else
6715 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6716 &expr->where);
6717 return false;
6720 else
6722 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6723 return false;
6726 return true;
6730 /* Resolve the expressions in an iterator structure. If REAL_OK is
6731 false allow only INTEGER type iterators, otherwise allow REAL types.
6732 Set own_scope to true for ac-implied-do and data-implied-do as those
6733 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
6735 bool
6736 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
6738 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6739 return false;
6741 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6742 _("iterator variable")))
6743 return false;
6745 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6746 "Start expression in DO loop"))
6747 return false;
6749 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6750 "End expression in DO loop"))
6751 return false;
6753 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6754 "Step expression in DO loop"))
6755 return false;
6757 if (iter->step->expr_type == EXPR_CONSTANT)
6759 if ((iter->step->ts.type == BT_INTEGER
6760 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6761 || (iter->step->ts.type == BT_REAL
6762 && mpfr_sgn (iter->step->value.real) == 0))
6764 gfc_error ("Step expression in DO loop at %L cannot be zero",
6765 &iter->step->where);
6766 return false;
6770 /* Convert start, end, and step to the same type as var. */
6771 if (iter->start->ts.kind != iter->var->ts.kind
6772 || iter->start->ts.type != iter->var->ts.type)
6773 gfc_convert_type (iter->start, &iter->var->ts, 1);
6775 if (iter->end->ts.kind != iter->var->ts.kind
6776 || iter->end->ts.type != iter->var->ts.type)
6777 gfc_convert_type (iter->end, &iter->var->ts, 1);
6779 if (iter->step->ts.kind != iter->var->ts.kind
6780 || iter->step->ts.type != iter->var->ts.type)
6781 gfc_convert_type (iter->step, &iter->var->ts, 1);
6783 if (iter->start->expr_type == EXPR_CONSTANT
6784 && iter->end->expr_type == EXPR_CONSTANT
6785 && iter->step->expr_type == EXPR_CONSTANT)
6787 int sgn, cmp;
6788 if (iter->start->ts.type == BT_INTEGER)
6790 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6791 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6793 else
6795 sgn = mpfr_sgn (iter->step->value.real);
6796 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6798 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
6799 gfc_warning (OPT_Wzerotrip,
6800 "DO loop at %L will be executed zero times",
6801 &iter->step->where);
6804 if (iter->end->expr_type == EXPR_CONSTANT
6805 && iter->end->ts.type == BT_INTEGER
6806 && iter->step->expr_type == EXPR_CONSTANT
6807 && iter->step->ts.type == BT_INTEGER
6808 && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
6809 || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
6811 bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
6812 int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
6814 if (is_step_positive
6815 && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
6816 gfc_warning (OPT_Wundefined_do_loop,
6817 "DO loop at %L is undefined as it overflows",
6818 &iter->step->where);
6819 else if (!is_step_positive
6820 && mpz_cmp (iter->end->value.integer,
6821 gfc_integer_kinds[k].min_int) == 0)
6822 gfc_warning (OPT_Wundefined_do_loop,
6823 "DO loop at %L is undefined as it underflows",
6824 &iter->step->where);
6827 return true;
6831 /* Traversal function for find_forall_index. f == 2 signals that
6832 that variable itself is not to be checked - only the references. */
6834 static bool
6835 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6837 if (expr->expr_type != EXPR_VARIABLE)
6838 return false;
6840 /* A scalar assignment */
6841 if (!expr->ref || *f == 1)
6843 if (expr->symtree->n.sym == sym)
6844 return true;
6845 else
6846 return false;
6849 if (*f == 2)
6850 *f = 1;
6851 return false;
6855 /* Check whether the FORALL index appears in the expression or not.
6856 Returns true if SYM is found in EXPR. */
6858 bool
6859 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6861 if (gfc_traverse_expr (expr, sym, forall_index, f))
6862 return true;
6863 else
6864 return false;
6868 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6869 to be a scalar INTEGER variable. The subscripts and stride are scalar
6870 INTEGERs, and if stride is a constant it must be nonzero.
6871 Furthermore "A subscript or stride in a forall-triplet-spec shall
6872 not contain a reference to any index-name in the
6873 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6875 static void
6876 resolve_forall_iterators (gfc_forall_iterator *it)
6878 gfc_forall_iterator *iter, *iter2;
6880 for (iter = it; iter; iter = iter->next)
6882 if (gfc_resolve_expr (iter->var)
6883 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6884 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6885 &iter->var->where);
6887 if (gfc_resolve_expr (iter->start)
6888 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6889 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6890 &iter->start->where);
6891 if (iter->var->ts.kind != iter->start->ts.kind)
6892 gfc_convert_type (iter->start, &iter->var->ts, 1);
6894 if (gfc_resolve_expr (iter->end)
6895 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6896 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6897 &iter->end->where);
6898 if (iter->var->ts.kind != iter->end->ts.kind)
6899 gfc_convert_type (iter->end, &iter->var->ts, 1);
6901 if (gfc_resolve_expr (iter->stride))
6903 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6904 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6905 &iter->stride->where, "INTEGER");
6907 if (iter->stride->expr_type == EXPR_CONSTANT
6908 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
6909 gfc_error ("FORALL stride expression at %L cannot be zero",
6910 &iter->stride->where);
6912 if (iter->var->ts.kind != iter->stride->ts.kind)
6913 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6916 for (iter = it; iter; iter = iter->next)
6917 for (iter2 = iter; iter2; iter2 = iter2->next)
6919 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6920 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6921 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
6922 gfc_error ("FORALL index %qs may not appear in triplet "
6923 "specification at %L", iter->var->symtree->name,
6924 &iter2->start->where);
6929 /* Given a pointer to a symbol that is a derived type, see if it's
6930 inaccessible, i.e. if it's defined in another module and the components are
6931 PRIVATE. The search is recursive if necessary. Returns zero if no
6932 inaccessible components are found, nonzero otherwise. */
6934 static int
6935 derived_inaccessible (gfc_symbol *sym)
6937 gfc_component *c;
6939 if (sym->attr.use_assoc && sym->attr.private_comp)
6940 return 1;
6942 for (c = sym->components; c; c = c->next)
6944 /* Prevent an infinite loop through this function. */
6945 if (c->ts.type == BT_DERIVED && c->attr.pointer
6946 && sym == c->ts.u.derived)
6947 continue;
6949 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6950 return 1;
6953 return 0;
6957 /* Resolve the argument of a deallocate expression. The expression must be
6958 a pointer or a full array. */
6960 static bool
6961 resolve_deallocate_expr (gfc_expr *e)
6963 symbol_attribute attr;
6964 int allocatable, pointer;
6965 gfc_ref *ref;
6966 gfc_symbol *sym;
6967 gfc_component *c;
6968 bool unlimited;
6970 if (!gfc_resolve_expr (e))
6971 return false;
6973 if (e->expr_type != EXPR_VARIABLE)
6974 goto bad;
6976 sym = e->symtree->n.sym;
6977 unlimited = UNLIMITED_POLY(sym);
6979 if (sym->ts.type == BT_CLASS)
6981 allocatable = CLASS_DATA (sym)->attr.allocatable;
6982 pointer = CLASS_DATA (sym)->attr.class_pointer;
6984 else
6986 allocatable = sym->attr.allocatable;
6987 pointer = sym->attr.pointer;
6989 for (ref = e->ref; ref; ref = ref->next)
6991 switch (ref->type)
6993 case REF_ARRAY:
6994 if (ref->u.ar.type != AR_FULL
6995 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6996 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6997 allocatable = 0;
6998 break;
7000 case REF_COMPONENT:
7001 c = ref->u.c.component;
7002 if (c->ts.type == BT_CLASS)
7004 allocatable = CLASS_DATA (c)->attr.allocatable;
7005 pointer = CLASS_DATA (c)->attr.class_pointer;
7007 else
7009 allocatable = c->attr.allocatable;
7010 pointer = c->attr.pointer;
7012 break;
7014 case REF_SUBSTRING:
7015 allocatable = 0;
7016 break;
7020 attr = gfc_expr_attr (e);
7022 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
7024 bad:
7025 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7026 &e->where);
7027 return false;
7030 /* F2008, C644. */
7031 if (gfc_is_coindexed (e))
7033 gfc_error ("Coindexed allocatable object at %L", &e->where);
7034 return false;
7037 if (pointer
7038 && !gfc_check_vardef_context (e, true, true, false,
7039 _("DEALLOCATE object")))
7040 return false;
7041 if (!gfc_check_vardef_context (e, false, true, false,
7042 _("DEALLOCATE object")))
7043 return false;
7045 return true;
7049 /* Returns true if the expression e contains a reference to the symbol sym. */
7050 static bool
7051 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
7053 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
7054 return true;
7056 return false;
7059 bool
7060 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
7062 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
7066 /* Given the expression node e for an allocatable/pointer of derived type to be
7067 allocated, get the expression node to be initialized afterwards (needed for
7068 derived types with default initializers, and derived types with allocatable
7069 components that need nullification.) */
7071 gfc_expr *
7072 gfc_expr_to_initialize (gfc_expr *e)
7074 gfc_expr *result;
7075 gfc_ref *ref;
7076 int i;
7078 result = gfc_copy_expr (e);
7080 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
7081 for (ref = result->ref; ref; ref = ref->next)
7082 if (ref->type == REF_ARRAY && ref->next == NULL)
7084 ref->u.ar.type = AR_FULL;
7086 for (i = 0; i < ref->u.ar.dimen; i++)
7087 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
7089 break;
7092 gfc_free_shape (&result->shape, result->rank);
7094 /* Recalculate rank, shape, etc. */
7095 gfc_resolve_expr (result);
7096 return result;
7100 /* If the last ref of an expression is an array ref, return a copy of the
7101 expression with that one removed. Otherwise, a copy of the original
7102 expression. This is used for allocate-expressions and pointer assignment
7103 LHS, where there may be an array specification that needs to be stripped
7104 off when using gfc_check_vardef_context. */
7106 static gfc_expr*
7107 remove_last_array_ref (gfc_expr* e)
7109 gfc_expr* e2;
7110 gfc_ref** r;
7112 e2 = gfc_copy_expr (e);
7113 for (r = &e2->ref; *r; r = &(*r)->next)
7114 if ((*r)->type == REF_ARRAY && !(*r)->next)
7116 gfc_free_ref_list (*r);
7117 *r = NULL;
7118 break;
7121 return e2;
7125 /* Used in resolve_allocate_expr to check that a allocation-object and
7126 a source-expr are conformable. This does not catch all possible
7127 cases; in particular a runtime checking is needed. */
7129 static bool
7130 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
7132 gfc_ref *tail;
7133 for (tail = e2->ref; tail && tail->next; tail = tail->next);
7135 /* First compare rank. */
7136 if ((tail && e1->rank != tail->u.ar.as->rank)
7137 || (!tail && e1->rank != e2->rank))
7139 gfc_error ("Source-expr at %L must be scalar or have the "
7140 "same rank as the allocate-object at %L",
7141 &e1->where, &e2->where);
7142 return false;
7145 if (e1->shape)
7147 int i;
7148 mpz_t s;
7150 mpz_init (s);
7152 for (i = 0; i < e1->rank; i++)
7154 if (tail->u.ar.start[i] == NULL)
7155 break;
7157 if (tail->u.ar.end[i])
7159 mpz_set (s, tail->u.ar.end[i]->value.integer);
7160 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
7161 mpz_add_ui (s, s, 1);
7163 else
7165 mpz_set (s, tail->u.ar.start[i]->value.integer);
7168 if (mpz_cmp (e1->shape[i], s) != 0)
7170 gfc_error ("Source-expr at %L and allocate-object at %L must "
7171 "have the same shape", &e1->where, &e2->where);
7172 mpz_clear (s);
7173 return false;
7177 mpz_clear (s);
7180 return true;
7184 /* Resolve the expression in an ALLOCATE statement, doing the additional
7185 checks to see whether the expression is OK or not. The expression must
7186 have a trailing array reference that gives the size of the array. */
7188 static bool
7189 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
7191 int i, pointer, allocatable, dimension, is_abstract;
7192 int codimension;
7193 bool coindexed;
7194 bool unlimited;
7195 symbol_attribute attr;
7196 gfc_ref *ref, *ref2;
7197 gfc_expr *e2;
7198 gfc_array_ref *ar;
7199 gfc_symbol *sym = NULL;
7200 gfc_alloc *a;
7201 gfc_component *c;
7202 bool t;
7204 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
7205 checking of coarrays. */
7206 for (ref = e->ref; ref; ref = ref->next)
7207 if (ref->next == NULL)
7208 break;
7210 if (ref && ref->type == REF_ARRAY)
7211 ref->u.ar.in_allocate = true;
7213 if (!gfc_resolve_expr (e))
7214 goto failure;
7216 /* Make sure the expression is allocatable or a pointer. If it is
7217 pointer, the next-to-last reference must be a pointer. */
7219 ref2 = NULL;
7220 if (e->symtree)
7221 sym = e->symtree->n.sym;
7223 /* Check whether ultimate component is abstract and CLASS. */
7224 is_abstract = 0;
7226 /* Is the allocate-object unlimited polymorphic? */
7227 unlimited = UNLIMITED_POLY(e);
7229 if (e->expr_type != EXPR_VARIABLE)
7231 allocatable = 0;
7232 attr = gfc_expr_attr (e);
7233 pointer = attr.pointer;
7234 dimension = attr.dimension;
7235 codimension = attr.codimension;
7237 else
7239 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
7241 allocatable = CLASS_DATA (sym)->attr.allocatable;
7242 pointer = CLASS_DATA (sym)->attr.class_pointer;
7243 dimension = CLASS_DATA (sym)->attr.dimension;
7244 codimension = CLASS_DATA (sym)->attr.codimension;
7245 is_abstract = CLASS_DATA (sym)->attr.abstract;
7247 else
7249 allocatable = sym->attr.allocatable;
7250 pointer = sym->attr.pointer;
7251 dimension = sym->attr.dimension;
7252 codimension = sym->attr.codimension;
7255 coindexed = false;
7257 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
7259 switch (ref->type)
7261 case REF_ARRAY:
7262 if (ref->u.ar.codimen > 0)
7264 int n;
7265 for (n = ref->u.ar.dimen;
7266 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
7267 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
7269 coindexed = true;
7270 break;
7274 if (ref->next != NULL)
7275 pointer = 0;
7276 break;
7278 case REF_COMPONENT:
7279 /* F2008, C644. */
7280 if (coindexed)
7282 gfc_error ("Coindexed allocatable object at %L",
7283 &e->where);
7284 goto failure;
7287 c = ref->u.c.component;
7288 if (c->ts.type == BT_CLASS)
7290 allocatable = CLASS_DATA (c)->attr.allocatable;
7291 pointer = CLASS_DATA (c)->attr.class_pointer;
7292 dimension = CLASS_DATA (c)->attr.dimension;
7293 codimension = CLASS_DATA (c)->attr.codimension;
7294 is_abstract = CLASS_DATA (c)->attr.abstract;
7296 else
7298 allocatable = c->attr.allocatable;
7299 pointer = c->attr.pointer;
7300 dimension = c->attr.dimension;
7301 codimension = c->attr.codimension;
7302 is_abstract = c->attr.abstract;
7304 break;
7306 case REF_SUBSTRING:
7307 allocatable = 0;
7308 pointer = 0;
7309 break;
7314 /* Check for F08:C628. */
7315 if (allocatable == 0 && pointer == 0 && !unlimited)
7317 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7318 &e->where);
7319 goto failure;
7322 /* Some checks for the SOURCE tag. */
7323 if (code->expr3)
7325 /* Check F03:C631. */
7326 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7328 gfc_error ("Type of entity at %L is type incompatible with "
7329 "source-expr at %L", &e->where, &code->expr3->where);
7330 goto failure;
7333 /* Check F03:C632 and restriction following Note 6.18. */
7334 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
7335 goto failure;
7337 /* Check F03:C633. */
7338 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
7340 gfc_error ("The allocate-object at %L and the source-expr at %L "
7341 "shall have the same kind type parameter",
7342 &e->where, &code->expr3->where);
7343 goto failure;
7346 /* Check F2008, C642. */
7347 if (code->expr3->ts.type == BT_DERIVED
7348 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7349 || (code->expr3->ts.u.derived->from_intmod
7350 == INTMOD_ISO_FORTRAN_ENV
7351 && code->expr3->ts.u.derived->intmod_sym_id
7352 == ISOFORTRAN_LOCK_TYPE)))
7354 gfc_error ("The source-expr at %L shall neither be of type "
7355 "LOCK_TYPE nor have a LOCK_TYPE component if "
7356 "allocate-object at %L is a coarray",
7357 &code->expr3->where, &e->where);
7358 goto failure;
7361 /* Check TS18508, C702/C703. */
7362 if (code->expr3->ts.type == BT_DERIVED
7363 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
7364 || (code->expr3->ts.u.derived->from_intmod
7365 == INTMOD_ISO_FORTRAN_ENV
7366 && code->expr3->ts.u.derived->intmod_sym_id
7367 == ISOFORTRAN_EVENT_TYPE)))
7369 gfc_error ("The source-expr at %L shall neither be of type "
7370 "EVENT_TYPE nor have a EVENT_TYPE component if "
7371 "allocate-object at %L is a coarray",
7372 &code->expr3->where, &e->where);
7373 goto failure;
7377 /* Check F08:C629. */
7378 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7379 && !code->expr3)
7381 gcc_assert (e->ts.type == BT_CLASS);
7382 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7383 "type-spec or source-expr", sym->name, &e->where);
7384 goto failure;
7387 /* Check F08:C632. */
7388 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
7389 && !UNLIMITED_POLY (e))
7391 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7392 code->ext.alloc.ts.u.cl->length);
7393 if (cmp == 1 || cmp == -1 || cmp == -3)
7395 gfc_error ("Allocating %s at %L with type-spec requires the same "
7396 "character-length parameter as in the declaration",
7397 sym->name, &e->where);
7398 goto failure;
7402 /* In the variable definition context checks, gfc_expr_attr is used
7403 on the expression. This is fooled by the array specification
7404 present in e, thus we have to eliminate that one temporarily. */
7405 e2 = remove_last_array_ref (e);
7406 t = true;
7407 if (t && pointer)
7408 t = gfc_check_vardef_context (e2, true, true, false,
7409 _("ALLOCATE object"));
7410 if (t)
7411 t = gfc_check_vardef_context (e2, false, true, false,
7412 _("ALLOCATE object"));
7413 gfc_free_expr (e2);
7414 if (!t)
7415 goto failure;
7417 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7418 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7420 /* For class arrays, the initialization with SOURCE is done
7421 using _copy and trans_call. It is convenient to exploit that
7422 when the allocated type is different from the declared type but
7423 no SOURCE exists by setting expr3. */
7424 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
7426 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
7427 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
7428 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
7430 /* We have to zero initialize the integer variable. */
7431 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
7434 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
7436 /* Make sure the vtab symbol is present when
7437 the module variables are generated. */
7438 gfc_typespec ts = e->ts;
7439 if (code->expr3)
7440 ts = code->expr3->ts;
7441 else if (code->ext.alloc.ts.type == BT_DERIVED)
7442 ts = code->ext.alloc.ts;
7444 /* Finding the vtab also publishes the type's symbol. Therefore this
7445 statement is necessary. */
7446 gfc_find_derived_vtab (ts.u.derived);
7448 else if (unlimited && !UNLIMITED_POLY (code->expr3))
7450 /* Again, make sure the vtab symbol is present when
7451 the module variables are generated. */
7452 gfc_typespec *ts = NULL;
7453 if (code->expr3)
7454 ts = &code->expr3->ts;
7455 else
7456 ts = &code->ext.alloc.ts;
7458 gcc_assert (ts);
7460 /* Finding the vtab also publishes the type's symbol. Therefore this
7461 statement is necessary. */
7462 gfc_find_vtab (ts);
7465 if (dimension == 0 && codimension == 0)
7466 goto success;
7468 /* Make sure the last reference node is an array specification. */
7470 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
7471 || (dimension && ref2->u.ar.dimen == 0))
7473 /* F08:C633. */
7474 if (code->expr3)
7476 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
7477 "in ALLOCATE statement at %L", &e->where))
7478 goto failure;
7479 if (code->expr3->rank != 0)
7480 *array_alloc_wo_spec = true;
7481 else
7483 gfc_error ("Array specification or array-valued SOURCE= "
7484 "expression required in ALLOCATE statement at %L",
7485 &e->where);
7486 goto failure;
7489 else
7491 gfc_error ("Array specification required in ALLOCATE statement "
7492 "at %L", &e->where);
7493 goto failure;
7497 /* Make sure that the array section reference makes sense in the
7498 context of an ALLOCATE specification. */
7500 ar = &ref2->u.ar;
7502 if (codimension)
7503 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
7504 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
7506 gfc_error ("Coarray specification required in ALLOCATE statement "
7507 "at %L", &e->where);
7508 goto failure;
7511 for (i = 0; i < ar->dimen; i++)
7513 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
7514 goto check_symbols;
7516 switch (ar->dimen_type[i])
7518 case DIMEN_ELEMENT:
7519 break;
7521 case DIMEN_RANGE:
7522 if (ar->start[i] != NULL
7523 && ar->end[i] != NULL
7524 && ar->stride[i] == NULL)
7525 break;
7527 /* Fall through. */
7529 case DIMEN_UNKNOWN:
7530 case DIMEN_VECTOR:
7531 case DIMEN_STAR:
7532 case DIMEN_THIS_IMAGE:
7533 gfc_error ("Bad array specification in ALLOCATE statement at %L",
7534 &e->where);
7535 goto failure;
7538 check_symbols:
7539 for (a = code->ext.alloc.list; a; a = a->next)
7541 sym = a->expr->symtree->n.sym;
7543 /* TODO - check derived type components. */
7544 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
7545 continue;
7547 if ((ar->start[i] != NULL
7548 && gfc_find_sym_in_expr (sym, ar->start[i]))
7549 || (ar->end[i] != NULL
7550 && gfc_find_sym_in_expr (sym, ar->end[i])))
7552 gfc_error ("%qs must not appear in the array specification at "
7553 "%L in the same ALLOCATE statement where it is "
7554 "itself allocated", sym->name, &ar->where);
7555 goto failure;
7560 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7562 if (ar->dimen_type[i] == DIMEN_ELEMENT
7563 || ar->dimen_type[i] == DIMEN_RANGE)
7565 if (i == (ar->dimen + ar->codimen - 1))
7567 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7568 "statement at %L", &e->where);
7569 goto failure;
7571 continue;
7574 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7575 && ar->stride[i] == NULL)
7576 break;
7578 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7579 &e->where);
7580 goto failure;
7583 success:
7584 return true;
7586 failure:
7587 return false;
7591 static void
7592 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7594 gfc_expr *stat, *errmsg, *pe, *qe;
7595 gfc_alloc *a, *p, *q;
7597 stat = code->expr1;
7598 errmsg = code->expr2;
7600 /* Check the stat variable. */
7601 if (stat)
7603 gfc_check_vardef_context (stat, false, false, false,
7604 _("STAT variable"));
7606 if ((stat->ts.type != BT_INTEGER
7607 && !(stat->ref && (stat->ref->type == REF_ARRAY
7608 || stat->ref->type == REF_COMPONENT)))
7609 || stat->rank > 0)
7610 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7611 "variable", &stat->where);
7613 for (p = code->ext.alloc.list; p; p = p->next)
7614 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7616 gfc_ref *ref1, *ref2;
7617 bool found = true;
7619 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7620 ref1 = ref1->next, ref2 = ref2->next)
7622 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7623 continue;
7624 if (ref1->u.c.component->name != ref2->u.c.component->name)
7626 found = false;
7627 break;
7631 if (found)
7633 gfc_error ("Stat-variable at %L shall not be %sd within "
7634 "the same %s statement", &stat->where, fcn, fcn);
7635 break;
7640 /* Check the errmsg variable. */
7641 if (errmsg)
7643 if (!stat)
7644 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
7645 &errmsg->where);
7647 gfc_check_vardef_context (errmsg, false, false, false,
7648 _("ERRMSG variable"));
7650 if ((errmsg->ts.type != BT_CHARACTER
7651 && !(errmsg->ref
7652 && (errmsg->ref->type == REF_ARRAY
7653 || errmsg->ref->type == REF_COMPONENT)))
7654 || errmsg->rank > 0 )
7655 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7656 "variable", &errmsg->where);
7658 for (p = code->ext.alloc.list; p; p = p->next)
7659 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7661 gfc_ref *ref1, *ref2;
7662 bool found = true;
7664 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7665 ref1 = ref1->next, ref2 = ref2->next)
7667 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7668 continue;
7669 if (ref1->u.c.component->name != ref2->u.c.component->name)
7671 found = false;
7672 break;
7676 if (found)
7678 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7679 "the same %s statement", &errmsg->where, fcn, fcn);
7680 break;
7685 /* Check that an allocate-object appears only once in the statement. */
7687 for (p = code->ext.alloc.list; p; p = p->next)
7689 pe = p->expr;
7690 for (q = p->next; q; q = q->next)
7692 qe = q->expr;
7693 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7695 /* This is a potential collision. */
7696 gfc_ref *pr = pe->ref;
7697 gfc_ref *qr = qe->ref;
7699 /* Follow the references until
7700 a) They start to differ, in which case there is no error;
7701 you can deallocate a%b and a%c in a single statement
7702 b) Both of them stop, which is an error
7703 c) One of them stops, which is also an error. */
7704 while (1)
7706 if (pr == NULL && qr == NULL)
7708 gfc_error ("Allocate-object at %L also appears at %L",
7709 &pe->where, &qe->where);
7710 break;
7712 else if (pr != NULL && qr == NULL)
7714 gfc_error ("Allocate-object at %L is subobject of"
7715 " object at %L", &pe->where, &qe->where);
7716 break;
7718 else if (pr == NULL && qr != NULL)
7720 gfc_error ("Allocate-object at %L is subobject of"
7721 " object at %L", &qe->where, &pe->where);
7722 break;
7724 /* Here, pr != NULL && qr != NULL */
7725 gcc_assert(pr->type == qr->type);
7726 if (pr->type == REF_ARRAY)
7728 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7729 which are legal. */
7730 gcc_assert (qr->type == REF_ARRAY);
7732 if (pr->next && qr->next)
7734 int i;
7735 gfc_array_ref *par = &(pr->u.ar);
7736 gfc_array_ref *qar = &(qr->u.ar);
7738 for (i=0; i<par->dimen; i++)
7740 if ((par->start[i] != NULL
7741 || qar->start[i] != NULL)
7742 && gfc_dep_compare_expr (par->start[i],
7743 qar->start[i]) != 0)
7744 goto break_label;
7748 else
7750 if (pr->u.c.component->name != qr->u.c.component->name)
7751 break;
7754 pr = pr->next;
7755 qr = qr->next;
7757 break_label:
7763 if (strcmp (fcn, "ALLOCATE") == 0)
7765 bool arr_alloc_wo_spec = false;
7767 /* Resolving the expr3 in the loop over all objects to allocate would
7768 execute loop invariant code for each loop item. Therefore do it just
7769 once here. */
7770 if (code->expr3 && code->expr3->mold
7771 && code->expr3->ts.type == BT_DERIVED)
7773 /* Default initialization via MOLD (non-polymorphic). */
7774 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
7775 if (rhs != NULL)
7777 gfc_resolve_expr (rhs);
7778 gfc_free_expr (code->expr3);
7779 code->expr3 = rhs;
7782 for (a = code->ext.alloc.list; a; a = a->next)
7783 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
7785 if (arr_alloc_wo_spec && code->expr3)
7787 /* Mark the allocate to have to take the array specification
7788 from the expr3. */
7789 code->ext.alloc.arr_spec_from_expr3 = 1;
7792 else
7794 for (a = code->ext.alloc.list; a; a = a->next)
7795 resolve_deallocate_expr (a->expr);
7800 /************ SELECT CASE resolution subroutines ************/
7802 /* Callback function for our mergesort variant. Determines interval
7803 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7804 op1 > op2. Assumes we're not dealing with the default case.
7805 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7806 There are nine situations to check. */
7808 static int
7809 compare_cases (const gfc_case *op1, const gfc_case *op2)
7811 int retval;
7813 if (op1->low == NULL) /* op1 = (:L) */
7815 /* op2 = (:N), so overlap. */
7816 retval = 0;
7817 /* op2 = (M:) or (M:N), L < M */
7818 if (op2->low != NULL
7819 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7820 retval = -1;
7822 else if (op1->high == NULL) /* op1 = (K:) */
7824 /* op2 = (M:), so overlap. */
7825 retval = 0;
7826 /* op2 = (:N) or (M:N), K > N */
7827 if (op2->high != NULL
7828 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7829 retval = 1;
7831 else /* op1 = (K:L) */
7833 if (op2->low == NULL) /* op2 = (:N), K > N */
7834 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7835 ? 1 : 0;
7836 else if (op2->high == NULL) /* op2 = (M:), L < M */
7837 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7838 ? -1 : 0;
7839 else /* op2 = (M:N) */
7841 retval = 0;
7842 /* L < M */
7843 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7844 retval = -1;
7845 /* K > N */
7846 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7847 retval = 1;
7851 return retval;
7855 /* Merge-sort a double linked case list, detecting overlap in the
7856 process. LIST is the head of the double linked case list before it
7857 is sorted. Returns the head of the sorted list if we don't see any
7858 overlap, or NULL otherwise. */
7860 static gfc_case *
7861 check_case_overlap (gfc_case *list)
7863 gfc_case *p, *q, *e, *tail;
7864 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7866 /* If the passed list was empty, return immediately. */
7867 if (!list)
7868 return NULL;
7870 overlap_seen = 0;
7871 insize = 1;
7873 /* Loop unconditionally. The only exit from this loop is a return
7874 statement, when we've finished sorting the case list. */
7875 for (;;)
7877 p = list;
7878 list = NULL;
7879 tail = NULL;
7881 /* Count the number of merges we do in this pass. */
7882 nmerges = 0;
7884 /* Loop while there exists a merge to be done. */
7885 while (p)
7887 int i;
7889 /* Count this merge. */
7890 nmerges++;
7892 /* Cut the list in two pieces by stepping INSIZE places
7893 forward in the list, starting from P. */
7894 psize = 0;
7895 q = p;
7896 for (i = 0; i < insize; i++)
7898 psize++;
7899 q = q->right;
7900 if (!q)
7901 break;
7903 qsize = insize;
7905 /* Now we have two lists. Merge them! */
7906 while (psize > 0 || (qsize > 0 && q != NULL))
7908 /* See from which the next case to merge comes from. */
7909 if (psize == 0)
7911 /* P is empty so the next case must come from Q. */
7912 e = q;
7913 q = q->right;
7914 qsize--;
7916 else if (qsize == 0 || q == NULL)
7918 /* Q is empty. */
7919 e = p;
7920 p = p->right;
7921 psize--;
7923 else
7925 cmp = compare_cases (p, q);
7926 if (cmp < 0)
7928 /* The whole case range for P is less than the
7929 one for Q. */
7930 e = p;
7931 p = p->right;
7932 psize--;
7934 else if (cmp > 0)
7936 /* The whole case range for Q is greater than
7937 the case range for P. */
7938 e = q;
7939 q = q->right;
7940 qsize--;
7942 else
7944 /* The cases overlap, or they are the same
7945 element in the list. Either way, we must
7946 issue an error and get the next case from P. */
7947 /* FIXME: Sort P and Q by line number. */
7948 gfc_error ("CASE label at %L overlaps with CASE "
7949 "label at %L", &p->where, &q->where);
7950 overlap_seen = 1;
7951 e = p;
7952 p = p->right;
7953 psize--;
7957 /* Add the next element to the merged list. */
7958 if (tail)
7959 tail->right = e;
7960 else
7961 list = e;
7962 e->left = tail;
7963 tail = e;
7966 /* P has now stepped INSIZE places along, and so has Q. So
7967 they're the same. */
7968 p = q;
7970 tail->right = NULL;
7972 /* If we have done only one merge or none at all, we've
7973 finished sorting the cases. */
7974 if (nmerges <= 1)
7976 if (!overlap_seen)
7977 return list;
7978 else
7979 return NULL;
7982 /* Otherwise repeat, merging lists twice the size. */
7983 insize *= 2;
7988 /* Check to see if an expression is suitable for use in a CASE statement.
7989 Makes sure that all case expressions are scalar constants of the same
7990 type. Return false if anything is wrong. */
7992 static bool
7993 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7995 if (e == NULL) return true;
7997 if (e->ts.type != case_expr->ts.type)
7999 gfc_error ("Expression in CASE statement at %L must be of type %s",
8000 &e->where, gfc_basic_typename (case_expr->ts.type));
8001 return false;
8004 /* C805 (R808) For a given case-construct, each case-value shall be of
8005 the same type as case-expr. For character type, length differences
8006 are allowed, but the kind type parameters shall be the same. */
8008 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
8010 gfc_error ("Expression in CASE statement at %L must be of kind %d",
8011 &e->where, case_expr->ts.kind);
8012 return false;
8015 /* Convert the case value kind to that of case expression kind,
8016 if needed */
8018 if (e->ts.kind != case_expr->ts.kind)
8019 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
8021 if (e->rank != 0)
8023 gfc_error ("Expression in CASE statement at %L must be scalar",
8024 &e->where);
8025 return false;
8028 return true;
8032 /* Given a completely parsed select statement, we:
8034 - Validate all expressions and code within the SELECT.
8035 - Make sure that the selection expression is not of the wrong type.
8036 - Make sure that no case ranges overlap.
8037 - Eliminate unreachable cases and unreachable code resulting from
8038 removing case labels.
8040 The standard does allow unreachable cases, e.g. CASE (5:3). But
8041 they are a hassle for code generation, and to prevent that, we just
8042 cut them out here. This is not necessary for overlapping cases
8043 because they are illegal and we never even try to generate code.
8045 We have the additional caveat that a SELECT construct could have
8046 been a computed GOTO in the source code. Fortunately we can fairly
8047 easily work around that here: The case_expr for a "real" SELECT CASE
8048 is in code->expr1, but for a computed GOTO it is in code->expr2. All
8049 we have to do is make sure that the case_expr is a scalar integer
8050 expression. */
8052 static void
8053 resolve_select (gfc_code *code, bool select_type)
8055 gfc_code *body;
8056 gfc_expr *case_expr;
8057 gfc_case *cp, *default_case, *tail, *head;
8058 int seen_unreachable;
8059 int seen_logical;
8060 int ncases;
8061 bt type;
8062 bool t;
8064 if (code->expr1 == NULL)
8066 /* This was actually a computed GOTO statement. */
8067 case_expr = code->expr2;
8068 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
8069 gfc_error ("Selection expression in computed GOTO statement "
8070 "at %L must be a scalar integer expression",
8071 &case_expr->where);
8073 /* Further checking is not necessary because this SELECT was built
8074 by the compiler, so it should always be OK. Just move the
8075 case_expr from expr2 to expr so that we can handle computed
8076 GOTOs as normal SELECTs from here on. */
8077 code->expr1 = code->expr2;
8078 code->expr2 = NULL;
8079 return;
8082 case_expr = code->expr1;
8083 type = case_expr->ts.type;
8085 /* F08:C830. */
8086 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
8088 gfc_error ("Argument of SELECT statement at %L cannot be %s",
8089 &case_expr->where, gfc_typename (&case_expr->ts));
8091 /* Punt. Going on here just produce more garbage error messages. */
8092 return;
8095 /* F08:R842. */
8096 if (!select_type && case_expr->rank != 0)
8098 gfc_error ("Argument of SELECT statement at %L must be a scalar "
8099 "expression", &case_expr->where);
8101 /* Punt. */
8102 return;
8105 /* Raise a warning if an INTEGER case value exceeds the range of
8106 the case-expr. Later, all expressions will be promoted to the
8107 largest kind of all case-labels. */
8109 if (type == BT_INTEGER)
8110 for (body = code->block; body; body = body->block)
8111 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8113 if (cp->low
8114 && gfc_check_integer_range (cp->low->value.integer,
8115 case_expr->ts.kind) != ARITH_OK)
8116 gfc_warning (0, "Expression in CASE statement at %L is "
8117 "not in the range of %s", &cp->low->where,
8118 gfc_typename (&case_expr->ts));
8120 if (cp->high
8121 && cp->low != cp->high
8122 && gfc_check_integer_range (cp->high->value.integer,
8123 case_expr->ts.kind) != ARITH_OK)
8124 gfc_warning (0, "Expression in CASE statement at %L is "
8125 "not in the range of %s", &cp->high->where,
8126 gfc_typename (&case_expr->ts));
8129 /* PR 19168 has a long discussion concerning a mismatch of the kinds
8130 of the SELECT CASE expression and its CASE values. Walk the lists
8131 of case values, and if we find a mismatch, promote case_expr to
8132 the appropriate kind. */
8134 if (type == BT_LOGICAL || type == BT_INTEGER)
8136 for (body = code->block; body; body = body->block)
8138 /* Walk the case label list. */
8139 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8141 /* Intercept the DEFAULT case. It does not have a kind. */
8142 if (cp->low == NULL && cp->high == NULL)
8143 continue;
8145 /* Unreachable case ranges are discarded, so ignore. */
8146 if (cp->low != NULL && cp->high != NULL
8147 && cp->low != cp->high
8148 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8149 continue;
8151 if (cp->low != NULL
8152 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
8153 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
8155 if (cp->high != NULL
8156 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
8157 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
8162 /* Assume there is no DEFAULT case. */
8163 default_case = NULL;
8164 head = tail = NULL;
8165 ncases = 0;
8166 seen_logical = 0;
8168 for (body = code->block; body; body = body->block)
8170 /* Assume the CASE list is OK, and all CASE labels can be matched. */
8171 t = true;
8172 seen_unreachable = 0;
8174 /* Walk the case label list, making sure that all case labels
8175 are legal. */
8176 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8178 /* Count the number of cases in the whole construct. */
8179 ncases++;
8181 /* Intercept the DEFAULT case. */
8182 if (cp->low == NULL && cp->high == NULL)
8184 if (default_case != NULL)
8186 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8187 "by a second DEFAULT CASE at %L",
8188 &default_case->where, &cp->where);
8189 t = false;
8190 break;
8192 else
8194 default_case = cp;
8195 continue;
8199 /* Deal with single value cases and case ranges. Errors are
8200 issued from the validation function. */
8201 if (!validate_case_label_expr (cp->low, case_expr)
8202 || !validate_case_label_expr (cp->high, case_expr))
8204 t = false;
8205 break;
8208 if (type == BT_LOGICAL
8209 && ((cp->low == NULL || cp->high == NULL)
8210 || cp->low != cp->high))
8212 gfc_error ("Logical range in CASE statement at %L is not "
8213 "allowed", &cp->low->where);
8214 t = false;
8215 break;
8218 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
8220 int value;
8221 value = cp->low->value.logical == 0 ? 2 : 1;
8222 if (value & seen_logical)
8224 gfc_error ("Constant logical value in CASE statement "
8225 "is repeated at %L",
8226 &cp->low->where);
8227 t = false;
8228 break;
8230 seen_logical |= value;
8233 if (cp->low != NULL && cp->high != NULL
8234 && cp->low != cp->high
8235 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8237 if (warn_surprising)
8238 gfc_warning (OPT_Wsurprising,
8239 "Range specification at %L can never be matched",
8240 &cp->where);
8242 cp->unreachable = 1;
8243 seen_unreachable = 1;
8245 else
8247 /* If the case range can be matched, it can also overlap with
8248 other cases. To make sure it does not, we put it in a
8249 double linked list here. We sort that with a merge sort
8250 later on to detect any overlapping cases. */
8251 if (!head)
8253 head = tail = cp;
8254 head->right = head->left = NULL;
8256 else
8258 tail->right = cp;
8259 tail->right->left = tail;
8260 tail = tail->right;
8261 tail->right = NULL;
8266 /* It there was a failure in the previous case label, give up
8267 for this case label list. Continue with the next block. */
8268 if (!t)
8269 continue;
8271 /* See if any case labels that are unreachable have been seen.
8272 If so, we eliminate them. This is a bit of a kludge because
8273 the case lists for a single case statement (label) is a
8274 single forward linked lists. */
8275 if (seen_unreachable)
8277 /* Advance until the first case in the list is reachable. */
8278 while (body->ext.block.case_list != NULL
8279 && body->ext.block.case_list->unreachable)
8281 gfc_case *n = body->ext.block.case_list;
8282 body->ext.block.case_list = body->ext.block.case_list->next;
8283 n->next = NULL;
8284 gfc_free_case_list (n);
8287 /* Strip all other unreachable cases. */
8288 if (body->ext.block.case_list)
8290 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
8292 if (cp->next->unreachable)
8294 gfc_case *n = cp->next;
8295 cp->next = cp->next->next;
8296 n->next = NULL;
8297 gfc_free_case_list (n);
8304 /* See if there were overlapping cases. If the check returns NULL,
8305 there was overlap. In that case we don't do anything. If head
8306 is non-NULL, we prepend the DEFAULT case. The sorted list can
8307 then used during code generation for SELECT CASE constructs with
8308 a case expression of a CHARACTER type. */
8309 if (head)
8311 head = check_case_overlap (head);
8313 /* Prepend the default_case if it is there. */
8314 if (head != NULL && default_case)
8316 default_case->left = NULL;
8317 default_case->right = head;
8318 head->left = default_case;
8322 /* Eliminate dead blocks that may be the result if we've seen
8323 unreachable case labels for a block. */
8324 for (body = code; body && body->block; body = body->block)
8326 if (body->block->ext.block.case_list == NULL)
8328 /* Cut the unreachable block from the code chain. */
8329 gfc_code *c = body->block;
8330 body->block = c->block;
8332 /* Kill the dead block, but not the blocks below it. */
8333 c->block = NULL;
8334 gfc_free_statements (c);
8338 /* More than two cases is legal but insane for logical selects.
8339 Issue a warning for it. */
8340 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
8341 gfc_warning (OPT_Wsurprising,
8342 "Logical SELECT CASE block at %L has more that two cases",
8343 &code->loc);
8347 /* Check if a derived type is extensible. */
8349 bool
8350 gfc_type_is_extensible (gfc_symbol *sym)
8352 return !(sym->attr.is_bind_c || sym->attr.sequence
8353 || (sym->attr.is_class
8354 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
8358 static void
8359 resolve_types (gfc_namespace *ns);
8361 /* Resolve an associate-name: Resolve target and ensure the type-spec is
8362 correct as well as possibly the array-spec. */
8364 static void
8365 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
8367 gfc_expr* target;
8369 gcc_assert (sym->assoc);
8370 gcc_assert (sym->attr.flavor == FL_VARIABLE);
8372 /* If this is for SELECT TYPE, the target may not yet be set. In that
8373 case, return. Resolution will be called later manually again when
8374 this is done. */
8375 target = sym->assoc->target;
8376 if (!target)
8377 return;
8378 gcc_assert (!sym->assoc->dangling);
8380 if (resolve_target && !gfc_resolve_expr (target))
8381 return;
8383 /* For variable targets, we get some attributes from the target. */
8384 if (target->expr_type == EXPR_VARIABLE)
8386 gfc_symbol* tsym;
8388 gcc_assert (target->symtree);
8389 tsym = target->symtree->n.sym;
8391 sym->attr.asynchronous = tsym->attr.asynchronous;
8392 sym->attr.volatile_ = tsym->attr.volatile_;
8394 sym->attr.target = tsym->attr.target
8395 || gfc_expr_attr (target).pointer;
8396 if (is_subref_array (target))
8397 sym->attr.subref_array_pointer = 1;
8400 if (target->expr_type == EXPR_NULL)
8402 gfc_error ("Selector at %L cannot be NULL()", &target->where);
8403 return;
8405 else if (target->ts.type == BT_UNKNOWN)
8407 gfc_error ("Selector at %L has no type", &target->where);
8408 return;
8411 /* Get type if this was not already set. Note that it can be
8412 some other type than the target in case this is a SELECT TYPE
8413 selector! So we must not update when the type is already there. */
8414 if (sym->ts.type == BT_UNKNOWN)
8415 sym->ts = target->ts;
8417 gcc_assert (sym->ts.type != BT_UNKNOWN);
8419 /* See if this is a valid association-to-variable. */
8420 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
8421 && !gfc_has_vector_subscript (target));
8423 /* Finally resolve if this is an array or not. */
8424 if (sym->attr.dimension && target->rank == 0)
8426 /* primary.c makes the assumption that a reference to an associate
8427 name followed by a left parenthesis is an array reference. */
8428 if (sym->ts.type != BT_CHARACTER)
8429 gfc_error ("Associate-name %qs at %L is used as array",
8430 sym->name, &sym->declared_at);
8431 sym->attr.dimension = 0;
8432 return;
8436 /* We cannot deal with class selectors that need temporaries. */
8437 if (target->ts.type == BT_CLASS
8438 && gfc_ref_needs_temporary_p (target->ref))
8440 gfc_error ("CLASS selector at %L needs a temporary which is not "
8441 "yet implemented", &target->where);
8442 return;
8445 if (target->ts.type == BT_CLASS)
8446 gfc_fix_class_refs (target);
8448 if (target->rank != 0)
8450 gfc_array_spec *as;
8451 /* The rank may be incorrectly guessed at parsing, therefore make sure
8452 it is corrected now. */
8453 if (sym->ts.type != BT_CLASS && (!sym->as || sym->assoc->rankguessed))
8455 if (!sym->as)
8456 sym->as = gfc_get_array_spec ();
8457 as = sym->as;
8458 as->rank = target->rank;
8459 as->type = AS_DEFERRED;
8460 as->corank = gfc_get_corank (target);
8461 sym->attr.dimension = 1;
8462 if (as->corank != 0)
8463 sym->attr.codimension = 1;
8466 else
8468 /* target's rank is 0, but the type of the sym is still array valued,
8469 which has to be corrected. */
8470 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
8472 gfc_array_spec *as;
8473 symbol_attribute attr;
8474 /* The associated variable's type is still the array type
8475 correct this now. */
8476 gfc_typespec *ts = &target->ts;
8477 gfc_ref *ref;
8478 gfc_component *c;
8479 for (ref = target->ref; ref != NULL; ref = ref->next)
8481 switch (ref->type)
8483 case REF_COMPONENT:
8484 ts = &ref->u.c.component->ts;
8485 break;
8486 case REF_ARRAY:
8487 if (ts->type == BT_CLASS)
8488 ts = &ts->u.derived->components->ts;
8489 break;
8490 default:
8491 break;
8494 /* Create a scalar instance of the current class type. Because the
8495 rank of a class array goes into its name, the type has to be
8496 rebuild. The alternative of (re-)setting just the attributes
8497 and as in the current type, destroys the type also in other
8498 places. */
8499 as = NULL;
8500 sym->ts = *ts;
8501 sym->ts.type = BT_CLASS;
8502 attr = CLASS_DATA (sym)->attr;
8503 attr.class_ok = 0;
8504 attr.associate_var = 1;
8505 attr.dimension = attr.codimension = 0;
8506 attr.class_pointer = 1;
8507 if (!gfc_build_class_symbol (&sym->ts, &attr, &as))
8508 gcc_unreachable ();
8509 /* Make sure the _vptr is set. */
8510 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true, NULL);
8511 if (c->ts.u.derived == NULL)
8512 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived);
8513 CLASS_DATA (sym)->attr.pointer = 1;
8514 CLASS_DATA (sym)->attr.class_pointer = 1;
8515 gfc_set_sym_referenced (sym->ts.u.derived);
8516 gfc_commit_symbol (sym->ts.u.derived);
8517 /* _vptr now has the _vtab in it, change it to the _vtype. */
8518 if (c->ts.u.derived->attr.vtab)
8519 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
8520 c->ts.u.derived->ns->types_resolved = 0;
8521 resolve_types (c->ts.u.derived->ns);
8525 /* Mark this as an associate variable. */
8526 sym->attr.associate_var = 1;
8528 /* Fix up the type-spec for CHARACTER types. */
8529 if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
8531 if (!sym->ts.u.cl)
8532 sym->ts.u.cl = target->ts.u.cl;
8534 if (!sym->ts.u.cl->length && !sym->ts.deferred)
8535 sym->ts.u.cl->length
8536 = gfc_get_int_expr (gfc_default_integer_kind,
8537 NULL, target->value.character.length);
8540 /* If the target is a good class object, so is the associate variable. */
8541 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
8542 sym->attr.class_ok = 1;
8546 /* Ensure that SELECT TYPE expressions have the correct rank and a full
8547 array reference, where necessary. The symbols are artificial and so
8548 the dimension attribute and arrayspec can also be set. In addition,
8549 sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
8550 This is corrected here as well.*/
8552 static void
8553 fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2,
8554 int rank, gfc_ref *ref)
8556 gfc_ref *nref = (*expr1)->ref;
8557 gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
8558 gfc_symbol *sym2 = expr2 ? expr2->symtree->n.sym : NULL;
8559 (*expr1)->rank = rank;
8560 if (sym1->ts.type == BT_CLASS)
8562 if ((*expr1)->ts.type != BT_CLASS)
8563 (*expr1)->ts = sym1->ts;
8565 CLASS_DATA (sym1)->attr.dimension = 1;
8566 if (CLASS_DATA (sym1)->as == NULL && sym2)
8567 CLASS_DATA (sym1)->as
8568 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
8570 else
8572 sym1->attr.dimension = 1;
8573 if (sym1->as == NULL && sym2)
8574 sym1->as = gfc_copy_array_spec (sym2->as);
8577 for (; nref; nref = nref->next)
8578 if (nref->next == NULL)
8579 break;
8581 if (ref && nref && nref->type != REF_ARRAY)
8582 nref->next = gfc_copy_ref (ref);
8583 else if (ref && !nref)
8584 (*expr1)->ref = gfc_copy_ref (ref);
8588 static gfc_expr *
8589 build_loc_call (gfc_expr *sym_expr)
8591 gfc_expr *loc_call;
8592 loc_call = gfc_get_expr ();
8593 loc_call->expr_type = EXPR_FUNCTION;
8594 gfc_get_sym_tree ("loc", gfc_current_ns, &loc_call->symtree, false);
8595 loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
8596 loc_call->symtree->n.sym->attr.intrinsic = 1;
8597 loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
8598 gfc_commit_symbol (loc_call->symtree->n.sym);
8599 loc_call->ts.type = BT_INTEGER;
8600 loc_call->ts.kind = gfc_index_integer_kind;
8601 loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
8602 loc_call->value.function.actual = gfc_get_actual_arglist ();
8603 loc_call->value.function.actual->expr = sym_expr;
8604 loc_call->where = sym_expr->where;
8605 return loc_call;
8608 /* Resolve a SELECT TYPE statement. */
8610 static void
8611 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
8613 gfc_symbol *selector_type;
8614 gfc_code *body, *new_st, *if_st, *tail;
8615 gfc_code *class_is = NULL, *default_case = NULL;
8616 gfc_case *c;
8617 gfc_symtree *st;
8618 char name[GFC_MAX_SYMBOL_LEN];
8619 gfc_namespace *ns;
8620 int error = 0;
8621 int charlen = 0;
8622 int rank = 0;
8623 gfc_ref* ref = NULL;
8624 gfc_expr *selector_expr = NULL;
8626 ns = code->ext.block.ns;
8627 gfc_resolve (ns);
8629 /* Check for F03:C813. */
8630 if (code->expr1->ts.type != BT_CLASS
8631 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
8633 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
8634 "at %L", &code->loc);
8635 return;
8638 if (!code->expr1->symtree->n.sym->attr.class_ok)
8639 return;
8641 if (code->expr2)
8643 if (code->expr1->symtree->n.sym->attr.untyped)
8644 code->expr1->symtree->n.sym->ts = code->expr2->ts;
8645 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
8647 /* F2008: C803 The selector expression must not be coindexed. */
8648 if (gfc_is_coindexed (code->expr2))
8650 gfc_error ("Selector at %L must not be coindexed",
8651 &code->expr2->where);
8652 return;
8656 else
8658 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
8660 if (gfc_is_coindexed (code->expr1))
8662 gfc_error ("Selector at %L must not be coindexed",
8663 &code->expr1->where);
8664 return;
8668 /* Loop over TYPE IS / CLASS IS cases. */
8669 for (body = code->block; body; body = body->block)
8671 c = body->ext.block.case_list;
8673 if (!error)
8675 /* Check for repeated cases. */
8676 for (tail = code->block; tail; tail = tail->block)
8678 gfc_case *d = tail->ext.block.case_list;
8679 if (tail == body)
8680 break;
8682 if (c->ts.type == d->ts.type
8683 && ((c->ts.type == BT_DERIVED
8684 && c->ts.u.derived && d->ts.u.derived
8685 && !strcmp (c->ts.u.derived->name,
8686 d->ts.u.derived->name))
8687 || c->ts.type == BT_UNKNOWN
8688 || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8689 && c->ts.kind == d->ts.kind)))
8691 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
8692 &c->where, &d->where);
8693 return;
8698 /* Check F03:C815. */
8699 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8700 && !selector_type->attr.unlimited_polymorphic
8701 && !gfc_type_is_extensible (c->ts.u.derived))
8703 gfc_error ("Derived type %qs at %L must be extensible",
8704 c->ts.u.derived->name, &c->where);
8705 error++;
8706 continue;
8709 /* Check F03:C816. */
8710 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
8711 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
8712 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
8714 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8715 gfc_error ("Derived type %qs at %L must be an extension of %qs",
8716 c->ts.u.derived->name, &c->where, selector_type->name);
8717 else
8718 gfc_error ("Unexpected intrinsic type %qs at %L",
8719 gfc_basic_typename (c->ts.type), &c->where);
8720 error++;
8721 continue;
8724 /* Check F03:C814. */
8725 if (c->ts.type == BT_CHARACTER
8726 && (c->ts.u.cl->length != NULL || c->ts.deferred))
8728 gfc_error ("The type-spec at %L shall specify that each length "
8729 "type parameter is assumed", &c->where);
8730 error++;
8731 continue;
8734 /* Intercept the DEFAULT case. */
8735 if (c->ts.type == BT_UNKNOWN)
8737 /* Check F03:C818. */
8738 if (default_case)
8740 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8741 "by a second DEFAULT CASE at %L",
8742 &default_case->ext.block.case_list->where, &c->where);
8743 error++;
8744 continue;
8747 default_case = body;
8751 if (error > 0)
8752 return;
8754 /* Transform SELECT TYPE statement to BLOCK and associate selector to
8755 target if present. If there are any EXIT statements referring to the
8756 SELECT TYPE construct, this is no problem because the gfc_code
8757 reference stays the same and EXIT is equally possible from the BLOCK
8758 it is changed to. */
8759 code->op = EXEC_BLOCK;
8760 if (code->expr2)
8762 gfc_association_list* assoc;
8764 assoc = gfc_get_association_list ();
8765 assoc->st = code->expr1->symtree;
8766 assoc->target = gfc_copy_expr (code->expr2);
8767 assoc->target->where = code->expr2->where;
8768 /* assoc->variable will be set by resolve_assoc_var. */
8770 code->ext.block.assoc = assoc;
8771 code->expr1->symtree->n.sym->assoc = assoc;
8773 resolve_assoc_var (code->expr1->symtree->n.sym, false);
8775 else
8776 code->ext.block.assoc = NULL;
8778 /* Ensure that the selector rank and arrayspec are available to
8779 correct expressions in which they might be missing. */
8780 if (code->expr2 && code->expr2->rank)
8782 rank = code->expr2->rank;
8783 for (ref = code->expr2->ref; ref; ref = ref->next)
8784 if (ref->next == NULL)
8785 break;
8786 if (ref && ref->type == REF_ARRAY)
8787 ref = gfc_copy_ref (ref);
8789 /* Fixup expr1 if necessary. */
8790 if (rank)
8791 fixup_array_ref (&code->expr1, code->expr2, rank, ref);
8793 else if (code->expr1->rank)
8795 rank = code->expr1->rank;
8796 for (ref = code->expr1->ref; ref; ref = ref->next)
8797 if (ref->next == NULL)
8798 break;
8799 if (ref && ref->type == REF_ARRAY)
8800 ref = gfc_copy_ref (ref);
8803 /* Add EXEC_SELECT to switch on type. */
8804 new_st = gfc_get_code (code->op);
8805 new_st->expr1 = code->expr1;
8806 new_st->expr2 = code->expr2;
8807 new_st->block = code->block;
8808 code->expr1 = code->expr2 = NULL;
8809 code->block = NULL;
8810 if (!ns->code)
8811 ns->code = new_st;
8812 else
8813 ns->code->next = new_st;
8814 code = new_st;
8815 code->op = EXEC_SELECT_TYPE;
8817 /* Use the intrinsic LOC function to generate an integer expression
8818 for the vtable of the selector. Note that the rank of the selector
8819 expression has to be set to zero. */
8820 gfc_add_vptr_component (code->expr1);
8821 code->expr1->rank = 0;
8822 code->expr1 = build_loc_call (code->expr1);
8823 selector_expr = code->expr1->value.function.actual->expr;
8825 /* Loop over TYPE IS / CLASS IS cases. */
8826 for (body = code->block; body; body = body->block)
8828 gfc_symbol *vtab;
8829 gfc_expr *e;
8830 c = body->ext.block.case_list;
8832 /* Generate an index integer expression for address of the
8833 TYPE/CLASS vtable and store it in c->low. The hash expression
8834 is stored in c->high and is used to resolve intrinsic cases. */
8835 if (c->ts.type != BT_UNKNOWN)
8837 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8839 vtab = gfc_find_derived_vtab (c->ts.u.derived);
8840 gcc_assert (vtab);
8841 c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8842 c->ts.u.derived->hash_value);
8844 else
8846 vtab = gfc_find_vtab (&c->ts);
8847 gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
8848 e = CLASS_DATA (vtab)->initializer;
8849 c->high = gfc_copy_expr (e);
8852 e = gfc_lval_expr_from_sym (vtab);
8853 c->low = build_loc_call (e);
8855 else
8856 continue;
8858 /* Associate temporary to selector. This should only be done
8859 when this case is actually true, so build a new ASSOCIATE
8860 that does precisely this here (instead of using the
8861 'global' one). */
8863 if (c->ts.type == BT_CLASS)
8864 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8865 else if (c->ts.type == BT_DERIVED)
8866 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8867 else if (c->ts.type == BT_CHARACTER)
8869 if (c->ts.u.cl && c->ts.u.cl->length
8870 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8871 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8872 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8873 charlen, c->ts.kind);
8875 else
8876 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8877 c->ts.kind);
8879 st = gfc_find_symtree (ns->sym_root, name);
8880 gcc_assert (st->n.sym->assoc);
8881 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
8882 st->n.sym->assoc->target->where = selector_expr->where;
8883 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8885 gfc_add_data_component (st->n.sym->assoc->target);
8886 /* Fixup the target expression if necessary. */
8887 if (rank)
8888 fixup_array_ref (&st->n.sym->assoc->target, NULL, rank, ref);
8891 new_st = gfc_get_code (EXEC_BLOCK);
8892 new_st->ext.block.ns = gfc_build_block_ns (ns);
8893 new_st->ext.block.ns->code = body->next;
8894 body->next = new_st;
8896 /* Chain in the new list only if it is marked as dangling. Otherwise
8897 there is a CASE label overlap and this is already used. Just ignore,
8898 the error is diagnosed elsewhere. */
8899 if (st->n.sym->assoc->dangling)
8901 new_st->ext.block.assoc = st->n.sym->assoc;
8902 st->n.sym->assoc->dangling = 0;
8905 resolve_assoc_var (st->n.sym, false);
8908 /* Take out CLASS IS cases for separate treatment. */
8909 body = code;
8910 while (body && body->block)
8912 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8914 /* Add to class_is list. */
8915 if (class_is == NULL)
8917 class_is = body->block;
8918 tail = class_is;
8920 else
8922 for (tail = class_is; tail->block; tail = tail->block) ;
8923 tail->block = body->block;
8924 tail = tail->block;
8926 /* Remove from EXEC_SELECT list. */
8927 body->block = body->block->block;
8928 tail->block = NULL;
8930 else
8931 body = body->block;
8934 if (class_is)
8936 gfc_symbol *vtab;
8938 if (!default_case)
8940 /* Add a default case to hold the CLASS IS cases. */
8941 for (tail = code; tail->block; tail = tail->block) ;
8942 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
8943 tail = tail->block;
8944 tail->ext.block.case_list = gfc_get_case ();
8945 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8946 tail->next = NULL;
8947 default_case = tail;
8950 /* More than one CLASS IS block? */
8951 if (class_is->block)
8953 gfc_code **c1,*c2;
8954 bool swapped;
8955 /* Sort CLASS IS blocks by extension level. */
8958 swapped = false;
8959 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8961 c2 = (*c1)->block;
8962 /* F03:C817 (check for doubles). */
8963 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8964 == c2->ext.block.case_list->ts.u.derived->hash_value)
8966 gfc_error ("Double CLASS IS block in SELECT TYPE "
8967 "statement at %L",
8968 &c2->ext.block.case_list->where);
8969 return;
8971 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8972 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8974 /* Swap. */
8975 (*c1)->block = c2->block;
8976 c2->block = *c1;
8977 *c1 = c2;
8978 swapped = true;
8982 while (swapped);
8985 /* Generate IF chain. */
8986 if_st = gfc_get_code (EXEC_IF);
8987 new_st = if_st;
8988 for (body = class_is; body; body = body->block)
8990 new_st->block = gfc_get_code (EXEC_IF);
8991 new_st = new_st->block;
8992 /* Set up IF condition: Call _gfortran_is_extension_of. */
8993 new_st->expr1 = gfc_get_expr ();
8994 new_st->expr1->expr_type = EXPR_FUNCTION;
8995 new_st->expr1->ts.type = BT_LOGICAL;
8996 new_st->expr1->ts.kind = 4;
8997 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8998 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8999 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
9000 /* Set up arguments. */
9001 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
9002 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
9003 new_st->expr1->value.function.actual->expr->where = code->loc;
9004 new_st->expr1->where = code->loc;
9005 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
9006 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
9007 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
9008 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
9009 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
9010 new_st->expr1->value.function.actual->next->expr->where = code->loc;
9011 new_st->next = body->next;
9013 if (default_case->next)
9015 new_st->block = gfc_get_code (EXEC_IF);
9016 new_st = new_st->block;
9017 new_st->next = default_case->next;
9020 /* Replace CLASS DEFAULT code by the IF chain. */
9021 default_case->next = if_st;
9024 /* Resolve the internal code. This can not be done earlier because
9025 it requires that the sym->assoc of selectors is set already. */
9026 gfc_current_ns = ns;
9027 gfc_resolve_blocks (code->block, gfc_current_ns);
9028 gfc_current_ns = old_ns;
9030 if (ref)
9031 free (ref);
9035 /* Resolve a transfer statement. This is making sure that:
9036 -- a derived type being transferred has only non-pointer components
9037 -- a derived type being transferred doesn't have private components, unless
9038 it's being transferred from the module where the type was defined
9039 -- we're not trying to transfer a whole assumed size array. */
9041 static void
9042 resolve_transfer (gfc_code *code)
9044 gfc_typespec *ts;
9045 gfc_symbol *sym, *derived;
9046 gfc_ref *ref;
9047 gfc_expr *exp;
9048 bool write = false;
9049 bool formatted = false;
9050 gfc_dt *dt = code->ext.dt;
9051 gfc_symbol *dtio_sub = NULL;
9053 exp = code->expr1;
9055 while (exp != NULL && exp->expr_type == EXPR_OP
9056 && exp->value.op.op == INTRINSIC_PARENTHESES)
9057 exp = exp->value.op.op1;
9059 if (exp && exp->expr_type == EXPR_NULL
9060 && code->ext.dt)
9062 gfc_error ("Invalid context for NULL () intrinsic at %L",
9063 &exp->where);
9064 return;
9067 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
9068 && exp->expr_type != EXPR_FUNCTION
9069 && exp->expr_type != EXPR_STRUCTURE))
9070 return;
9072 /* If we are reading, the variable will be changed. Note that
9073 code->ext.dt may be NULL if the TRANSFER is related to
9074 an INQUIRE statement -- but in this case, we are not reading, either. */
9075 if (dt && dt->dt_io_kind->value.iokind == M_READ
9076 && !gfc_check_vardef_context (exp, false, false, false,
9077 _("item in READ")))
9078 return;
9080 ts = exp->expr_type == EXPR_STRUCTURE ? &exp->ts : &exp->symtree->n.sym->ts;
9082 /* Go to actual component transferred. */
9083 for (ref = exp->ref; ref; ref = ref->next)
9084 if (ref->type == REF_COMPONENT)
9085 ts = &ref->u.c.component->ts;
9087 if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
9088 && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
9090 if (ts->type == BT_DERIVED)
9091 derived = ts->u.derived;
9092 else
9093 derived = ts->u.derived->components->ts.u.derived;
9095 if (dt->format_expr)
9097 char *fmt;
9098 fmt = gfc_widechar_to_char (dt->format_expr->value.character.string,
9099 -1);
9100 if (strtok (fmt, "DT") != NULL)
9101 formatted = true;
9103 else if (dt->format_label == &format_asterisk)
9105 /* List directed io must call the formatted DTIO procedure. */
9106 formatted = true;
9109 write = dt->dt_io_kind->value.iokind == M_WRITE
9110 || dt->dt_io_kind->value.iokind == M_PRINT;
9111 dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
9113 if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
9115 dt->udtio = exp;
9116 sym = exp->symtree->n.sym->ns->proc_name;
9117 /* Check to see if this is a nested DTIO call, with the
9118 dummy as the io-list object. */
9119 if (sym && sym == dtio_sub && sym->formal
9120 && sym->formal->sym == exp->symtree->n.sym
9121 && exp->ref == NULL)
9123 if (!sym->attr.recursive)
9125 gfc_error ("DTIO %s procedure at %L must be recursive",
9126 sym->name, &sym->declared_at);
9127 return;
9133 if (ts->type == BT_CLASS && dtio_sub == NULL)
9135 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
9136 "it is processed by a defined input/output procedure",
9137 &code->loc);
9138 return;
9141 if (ts->type == BT_DERIVED)
9143 /* Check that transferred derived type doesn't contain POINTER
9144 components unless it is processed by a defined input/output
9145 procedure". */
9146 if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
9148 gfc_error ("Data transfer element at %L cannot have POINTER "
9149 "components unless it is processed by a defined "
9150 "input/output procedure", &code->loc);
9151 return;
9154 /* F08:C935. */
9155 if (ts->u.derived->attr.proc_pointer_comp)
9157 gfc_error ("Data transfer element at %L cannot have "
9158 "procedure pointer components", &code->loc);
9159 return;
9162 if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
9164 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
9165 "components unless it is processed by a defined "
9166 "input/output procedure", &code->loc);
9167 return;
9170 /* C_PTR and C_FUNPTR have private components which means they can not
9171 be printed. However, if -std=gnu and not -pedantic, allow
9172 the component to be printed to help debugging. */
9173 if (ts->u.derived->ts.f90_type == BT_VOID)
9175 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
9176 "cannot have PRIVATE components", &code->loc))
9177 return;
9179 else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
9181 gfc_error ("Data transfer element at %L cannot have "
9182 "PRIVATE components unless it is processed by "
9183 "a defined input/output procedure", &code->loc);
9184 return;
9188 if (exp->expr_type == EXPR_STRUCTURE)
9189 return;
9191 sym = exp->symtree->n.sym;
9193 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
9194 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
9196 gfc_error ("Data transfer element at %L cannot be a full reference to "
9197 "an assumed-size array", &code->loc);
9198 return;
9201 if (async_io_dt && exp->expr_type == EXPR_VARIABLE)
9202 exp->symtree->n.sym->attr.asynchronous = 1;
9206 /*********** Toplevel code resolution subroutines ***********/
9208 /* Find the set of labels that are reachable from this block. We also
9209 record the last statement in each block. */
9211 static void
9212 find_reachable_labels (gfc_code *block)
9214 gfc_code *c;
9216 if (!block)
9217 return;
9219 cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
9221 /* Collect labels in this block. We don't keep those corresponding
9222 to END {IF|SELECT}, these are checked in resolve_branch by going
9223 up through the code_stack. */
9224 for (c = block; c; c = c->next)
9226 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
9227 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
9230 /* Merge with labels from parent block. */
9231 if (cs_base->prev)
9233 gcc_assert (cs_base->prev->reachable_labels);
9234 bitmap_ior_into (cs_base->reachable_labels,
9235 cs_base->prev->reachable_labels);
9240 static void
9241 resolve_lock_unlock_event (gfc_code *code)
9243 if (code->expr1->expr_type == EXPR_FUNCTION
9244 && code->expr1->value.function.isym
9245 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
9246 remove_caf_get_intrinsic (code->expr1);
9248 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
9249 && (code->expr1->ts.type != BT_DERIVED
9250 || code->expr1->expr_type != EXPR_VARIABLE
9251 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
9252 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
9253 || code->expr1->rank != 0
9254 || (!gfc_is_coarray (code->expr1) &&
9255 !gfc_is_coindexed (code->expr1))))
9256 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
9257 &code->expr1->where);
9258 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
9259 && (code->expr1->ts.type != BT_DERIVED
9260 || code->expr1->expr_type != EXPR_VARIABLE
9261 || code->expr1->ts.u.derived->from_intmod
9262 != INTMOD_ISO_FORTRAN_ENV
9263 || code->expr1->ts.u.derived->intmod_sym_id
9264 != ISOFORTRAN_EVENT_TYPE
9265 || code->expr1->rank != 0))
9266 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
9267 &code->expr1->where);
9268 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
9269 && !gfc_is_coindexed (code->expr1))
9270 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
9271 &code->expr1->where);
9272 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
9273 gfc_error ("Event variable argument at %L must be a coarray but not "
9274 "coindexed", &code->expr1->where);
9276 /* Check STAT. */
9277 if (code->expr2
9278 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
9279 || code->expr2->expr_type != EXPR_VARIABLE))
9280 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
9281 &code->expr2->where);
9283 if (code->expr2
9284 && !gfc_check_vardef_context (code->expr2, false, false, false,
9285 _("STAT variable")))
9286 return;
9288 /* Check ERRMSG. */
9289 if (code->expr3
9290 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
9291 || code->expr3->expr_type != EXPR_VARIABLE))
9292 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
9293 &code->expr3->where);
9295 if (code->expr3
9296 && !gfc_check_vardef_context (code->expr3, false, false, false,
9297 _("ERRMSG variable")))
9298 return;
9300 /* Check for LOCK the ACQUIRED_LOCK. */
9301 if (code->op != EXEC_EVENT_WAIT && code->expr4
9302 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
9303 || code->expr4->expr_type != EXPR_VARIABLE))
9304 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
9305 "variable", &code->expr4->where);
9307 if (code->op != EXEC_EVENT_WAIT && code->expr4
9308 && !gfc_check_vardef_context (code->expr4, false, false, false,
9309 _("ACQUIRED_LOCK variable")))
9310 return;
9312 /* Check for EVENT WAIT the UNTIL_COUNT. */
9313 if (code->op == EXEC_EVENT_WAIT && code->expr4)
9315 if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
9316 || code->expr4->rank != 0)
9317 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
9318 "expression", &code->expr4->where);
9323 static void
9324 resolve_critical (gfc_code *code)
9326 gfc_symtree *symtree;
9327 gfc_symbol *lock_type;
9328 char name[GFC_MAX_SYMBOL_LEN];
9329 static int serial = 0;
9331 if (flag_coarray != GFC_FCOARRAY_LIB)
9332 return;
9334 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
9335 GFC_PREFIX ("lock_type"));
9336 if (symtree)
9337 lock_type = symtree->n.sym;
9338 else
9340 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
9341 false) != 0)
9342 gcc_unreachable ();
9343 lock_type = symtree->n.sym;
9344 lock_type->attr.flavor = FL_DERIVED;
9345 lock_type->attr.zero_comp = 1;
9346 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
9347 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
9350 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
9351 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
9352 gcc_unreachable ();
9354 code->resolved_sym = symtree->n.sym;
9355 symtree->n.sym->attr.flavor = FL_VARIABLE;
9356 symtree->n.sym->attr.referenced = 1;
9357 symtree->n.sym->attr.artificial = 1;
9358 symtree->n.sym->attr.codimension = 1;
9359 symtree->n.sym->ts.type = BT_DERIVED;
9360 symtree->n.sym->ts.u.derived = lock_type;
9361 symtree->n.sym->as = gfc_get_array_spec ();
9362 symtree->n.sym->as->corank = 1;
9363 symtree->n.sym->as->type = AS_EXPLICIT;
9364 symtree->n.sym->as->cotype = AS_EXPLICIT;
9365 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
9366 NULL, 1);
9367 gfc_commit_symbols();
9371 static void
9372 resolve_sync (gfc_code *code)
9374 /* Check imageset. The * case matches expr1 == NULL. */
9375 if (code->expr1)
9377 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
9378 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
9379 "INTEGER expression", &code->expr1->where);
9380 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
9381 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
9382 gfc_error ("Imageset argument at %L must between 1 and num_images()",
9383 &code->expr1->where);
9384 else if (code->expr1->expr_type == EXPR_ARRAY
9385 && gfc_simplify_expr (code->expr1, 0))
9387 gfc_constructor *cons;
9388 cons = gfc_constructor_first (code->expr1->value.constructor);
9389 for (; cons; cons = gfc_constructor_next (cons))
9390 if (cons->expr->expr_type == EXPR_CONSTANT
9391 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
9392 gfc_error ("Imageset argument at %L must between 1 and "
9393 "num_images()", &cons->expr->where);
9397 /* Check STAT. */
9398 if (code->expr2
9399 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
9400 || code->expr2->expr_type != EXPR_VARIABLE))
9401 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
9402 &code->expr2->where);
9404 /* Check ERRMSG. */
9405 if (code->expr3
9406 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
9407 || code->expr3->expr_type != EXPR_VARIABLE))
9408 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
9409 &code->expr3->where);
9413 /* Given a branch to a label, see if the branch is conforming.
9414 The code node describes where the branch is located. */
9416 static void
9417 resolve_branch (gfc_st_label *label, gfc_code *code)
9419 code_stack *stack;
9421 if (label == NULL)
9422 return;
9424 /* Step one: is this a valid branching target? */
9426 if (label->defined == ST_LABEL_UNKNOWN)
9428 gfc_error ("Label %d referenced at %L is never defined", label->value,
9429 &code->loc);
9430 return;
9433 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
9435 gfc_error ("Statement at %L is not a valid branch target statement "
9436 "for the branch statement at %L", &label->where, &code->loc);
9437 return;
9440 /* Step two: make sure this branch is not a branch to itself ;-) */
9442 if (code->here == label)
9444 gfc_warning (0,
9445 "Branch at %L may result in an infinite loop", &code->loc);
9446 return;
9449 /* Step three: See if the label is in the same block as the
9450 branching statement. The hard work has been done by setting up
9451 the bitmap reachable_labels. */
9453 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
9455 /* Check now whether there is a CRITICAL construct; if so, check
9456 whether the label is still visible outside of the CRITICAL block,
9457 which is invalid. */
9458 for (stack = cs_base; stack; stack = stack->prev)
9460 if (stack->current->op == EXEC_CRITICAL
9461 && bitmap_bit_p (stack->reachable_labels, label->value))
9462 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
9463 "label at %L", &code->loc, &label->where);
9464 else if (stack->current->op == EXEC_DO_CONCURRENT
9465 && bitmap_bit_p (stack->reachable_labels, label->value))
9466 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
9467 "for label at %L", &code->loc, &label->where);
9470 return;
9473 /* Step four: If we haven't found the label in the bitmap, it may
9474 still be the label of the END of the enclosing block, in which
9475 case we find it by going up the code_stack. */
9477 for (stack = cs_base; stack; stack = stack->prev)
9479 if (stack->current->next && stack->current->next->here == label)
9480 break;
9481 if (stack->current->op == EXEC_CRITICAL)
9483 /* Note: A label at END CRITICAL does not leave the CRITICAL
9484 construct as END CRITICAL is still part of it. */
9485 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
9486 " at %L", &code->loc, &label->where);
9487 return;
9489 else if (stack->current->op == EXEC_DO_CONCURRENT)
9491 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
9492 "label at %L", &code->loc, &label->where);
9493 return;
9497 if (stack)
9499 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
9500 return;
9503 /* The label is not in an enclosing block, so illegal. This was
9504 allowed in Fortran 66, so we allow it as extension. No
9505 further checks are necessary in this case. */
9506 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
9507 "as the GOTO statement at %L", &label->where,
9508 &code->loc);
9509 return;
9513 /* Check whether EXPR1 has the same shape as EXPR2. */
9515 static bool
9516 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
9518 mpz_t shape[GFC_MAX_DIMENSIONS];
9519 mpz_t shape2[GFC_MAX_DIMENSIONS];
9520 bool result = false;
9521 int i;
9523 /* Compare the rank. */
9524 if (expr1->rank != expr2->rank)
9525 return result;
9527 /* Compare the size of each dimension. */
9528 for (i=0; i<expr1->rank; i++)
9530 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
9531 goto ignore;
9533 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
9534 goto ignore;
9536 if (mpz_cmp (shape[i], shape2[i]))
9537 goto over;
9540 /* When either of the two expression is an assumed size array, we
9541 ignore the comparison of dimension sizes. */
9542 ignore:
9543 result = true;
9545 over:
9546 gfc_clear_shape (shape, i);
9547 gfc_clear_shape (shape2, i);
9548 return result;
9552 /* Check whether a WHERE assignment target or a WHERE mask expression
9553 has the same shape as the outmost WHERE mask expression. */
9555 static void
9556 resolve_where (gfc_code *code, gfc_expr *mask)
9558 gfc_code *cblock;
9559 gfc_code *cnext;
9560 gfc_expr *e = NULL;
9562 cblock = code->block;
9564 /* Store the first WHERE mask-expr of the WHERE statement or construct.
9565 In case of nested WHERE, only the outmost one is stored. */
9566 if (mask == NULL) /* outmost WHERE */
9567 e = cblock->expr1;
9568 else /* inner WHERE */
9569 e = mask;
9571 while (cblock)
9573 if (cblock->expr1)
9575 /* Check if the mask-expr has a consistent shape with the
9576 outmost WHERE mask-expr. */
9577 if (!resolve_where_shape (cblock->expr1, e))
9578 gfc_error ("WHERE mask at %L has inconsistent shape",
9579 &cblock->expr1->where);
9582 /* the assignment statement of a WHERE statement, or the first
9583 statement in where-body-construct of a WHERE construct */
9584 cnext = cblock->next;
9585 while (cnext)
9587 switch (cnext->op)
9589 /* WHERE assignment statement */
9590 case EXEC_ASSIGN:
9592 /* Check shape consistent for WHERE assignment target. */
9593 if (e && !resolve_where_shape (cnext->expr1, e))
9594 gfc_error ("WHERE assignment target at %L has "
9595 "inconsistent shape", &cnext->expr1->where);
9596 break;
9599 case EXEC_ASSIGN_CALL:
9600 resolve_call (cnext);
9601 if (!cnext->resolved_sym->attr.elemental)
9602 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
9603 &cnext->ext.actual->expr->where);
9604 break;
9606 /* WHERE or WHERE construct is part of a where-body-construct */
9607 case EXEC_WHERE:
9608 resolve_where (cnext, e);
9609 break;
9611 default:
9612 gfc_error ("Unsupported statement inside WHERE at %L",
9613 &cnext->loc);
9615 /* the next statement within the same where-body-construct */
9616 cnext = cnext->next;
9618 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
9619 cblock = cblock->block;
9624 /* Resolve assignment in FORALL construct.
9625 NVAR is the number of FORALL index variables, and VAR_EXPR records the
9626 FORALL index variables. */
9628 static void
9629 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
9631 int n;
9633 for (n = 0; n < nvar; n++)
9635 gfc_symbol *forall_index;
9637 forall_index = var_expr[n]->symtree->n.sym;
9639 /* Check whether the assignment target is one of the FORALL index
9640 variable. */
9641 if ((code->expr1->expr_type == EXPR_VARIABLE)
9642 && (code->expr1->symtree->n.sym == forall_index))
9643 gfc_error ("Assignment to a FORALL index variable at %L",
9644 &code->expr1->where);
9645 else
9647 /* If one of the FORALL index variables doesn't appear in the
9648 assignment variable, then there could be a many-to-one
9649 assignment. Emit a warning rather than an error because the
9650 mask could be resolving this problem. */
9651 if (!find_forall_index (code->expr1, forall_index, 0))
9652 gfc_warning (0, "The FORALL with index %qs is not used on the "
9653 "left side of the assignment at %L and so might "
9654 "cause multiple assignment to this object",
9655 var_expr[n]->symtree->name, &code->expr1->where);
9661 /* Resolve WHERE statement in FORALL construct. */
9663 static void
9664 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
9665 gfc_expr **var_expr)
9667 gfc_code *cblock;
9668 gfc_code *cnext;
9670 cblock = code->block;
9671 while (cblock)
9673 /* the assignment statement of a WHERE statement, or the first
9674 statement in where-body-construct of a WHERE construct */
9675 cnext = cblock->next;
9676 while (cnext)
9678 switch (cnext->op)
9680 /* WHERE assignment statement */
9681 case EXEC_ASSIGN:
9682 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
9683 break;
9685 /* WHERE operator assignment statement */
9686 case EXEC_ASSIGN_CALL:
9687 resolve_call (cnext);
9688 if (!cnext->resolved_sym->attr.elemental)
9689 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
9690 &cnext->ext.actual->expr->where);
9691 break;
9693 /* WHERE or WHERE construct is part of a where-body-construct */
9694 case EXEC_WHERE:
9695 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
9696 break;
9698 default:
9699 gfc_error ("Unsupported statement inside WHERE at %L",
9700 &cnext->loc);
9702 /* the next statement within the same where-body-construct */
9703 cnext = cnext->next;
9705 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
9706 cblock = cblock->block;
9711 /* Traverse the FORALL body to check whether the following errors exist:
9712 1. For assignment, check if a many-to-one assignment happens.
9713 2. For WHERE statement, check the WHERE body to see if there is any
9714 many-to-one assignment. */
9716 static void
9717 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
9719 gfc_code *c;
9721 c = code->block->next;
9722 while (c)
9724 switch (c->op)
9726 case EXEC_ASSIGN:
9727 case EXEC_POINTER_ASSIGN:
9728 gfc_resolve_assign_in_forall (c, nvar, var_expr);
9729 break;
9731 case EXEC_ASSIGN_CALL:
9732 resolve_call (c);
9733 break;
9735 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
9736 there is no need to handle it here. */
9737 case EXEC_FORALL:
9738 break;
9739 case EXEC_WHERE:
9740 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
9741 break;
9742 default:
9743 break;
9745 /* The next statement in the FORALL body. */
9746 c = c->next;
9751 /* Counts the number of iterators needed inside a forall construct, including
9752 nested forall constructs. This is used to allocate the needed memory
9753 in gfc_resolve_forall. */
9755 static int
9756 gfc_count_forall_iterators (gfc_code *code)
9758 int max_iters, sub_iters, current_iters;
9759 gfc_forall_iterator *fa;
9761 gcc_assert(code->op == EXEC_FORALL);
9762 max_iters = 0;
9763 current_iters = 0;
9765 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9766 current_iters ++;
9768 code = code->block->next;
9770 while (code)
9772 if (code->op == EXEC_FORALL)
9774 sub_iters = gfc_count_forall_iterators (code);
9775 if (sub_iters > max_iters)
9776 max_iters = sub_iters;
9778 code = code->next;
9781 return current_iters + max_iters;
9785 /* Given a FORALL construct, first resolve the FORALL iterator, then call
9786 gfc_resolve_forall_body to resolve the FORALL body. */
9788 static void
9789 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
9791 static gfc_expr **var_expr;
9792 static int total_var = 0;
9793 static int nvar = 0;
9794 int i, old_nvar, tmp;
9795 gfc_forall_iterator *fa;
9797 old_nvar = nvar;
9799 /* Start to resolve a FORALL construct */
9800 if (forall_save == 0)
9802 /* Count the total number of FORALL indices in the nested FORALL
9803 construct in order to allocate the VAR_EXPR with proper size. */
9804 total_var = gfc_count_forall_iterators (code);
9806 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
9807 var_expr = XCNEWVEC (gfc_expr *, total_var);
9810 /* The information about FORALL iterator, including FORALL indices start, end
9811 and stride. An outer FORALL indice cannot appear in start, end or stride. */
9812 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9814 /* Fortran 20008: C738 (R753). */
9815 if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
9817 gfc_error ("FORALL index-name at %L must be a scalar variable "
9818 "of type integer", &fa->var->where);
9819 continue;
9822 /* Check if any outer FORALL index name is the same as the current
9823 one. */
9824 for (i = 0; i < nvar; i++)
9826 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
9827 gfc_error ("An outer FORALL construct already has an index "
9828 "with this name %L", &fa->var->where);
9831 /* Record the current FORALL index. */
9832 var_expr[nvar] = gfc_copy_expr (fa->var);
9834 nvar++;
9836 /* No memory leak. */
9837 gcc_assert (nvar <= total_var);
9840 /* Resolve the FORALL body. */
9841 gfc_resolve_forall_body (code, nvar, var_expr);
9843 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
9844 gfc_resolve_blocks (code->block, ns);
9846 tmp = nvar;
9847 nvar = old_nvar;
9848 /* Free only the VAR_EXPRs allocated in this frame. */
9849 for (i = nvar; i < tmp; i++)
9850 gfc_free_expr (var_expr[i]);
9852 if (nvar == 0)
9854 /* We are in the outermost FORALL construct. */
9855 gcc_assert (forall_save == 0);
9857 /* VAR_EXPR is not needed any more. */
9858 free (var_expr);
9859 total_var = 0;
9864 /* Resolve a BLOCK construct statement. */
9866 static void
9867 resolve_block_construct (gfc_code* code)
9869 /* Resolve the BLOCK's namespace. */
9870 gfc_resolve (code->ext.block.ns);
9872 /* For an ASSOCIATE block, the associations (and their targets) are already
9873 resolved during resolve_symbol. */
9877 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
9878 DO code nodes. */
9880 void
9881 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
9883 bool t;
9885 for (; b; b = b->block)
9887 t = gfc_resolve_expr (b->expr1);
9888 if (!gfc_resolve_expr (b->expr2))
9889 t = false;
9891 switch (b->op)
9893 case EXEC_IF:
9894 if (t && b->expr1 != NULL
9895 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
9896 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9897 &b->expr1->where);
9898 break;
9900 case EXEC_WHERE:
9901 if (t
9902 && b->expr1 != NULL
9903 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
9904 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
9905 &b->expr1->where);
9906 break;
9908 case EXEC_GOTO:
9909 resolve_branch (b->label1, b);
9910 break;
9912 case EXEC_BLOCK:
9913 resolve_block_construct (b);
9914 break;
9916 case EXEC_SELECT:
9917 case EXEC_SELECT_TYPE:
9918 case EXEC_FORALL:
9919 case EXEC_DO:
9920 case EXEC_DO_WHILE:
9921 case EXEC_DO_CONCURRENT:
9922 case EXEC_CRITICAL:
9923 case EXEC_READ:
9924 case EXEC_WRITE:
9925 case EXEC_IOLENGTH:
9926 case EXEC_WAIT:
9927 break;
9929 case EXEC_OMP_ATOMIC:
9930 case EXEC_OACC_ATOMIC:
9932 gfc_omp_atomic_op aop
9933 = (gfc_omp_atomic_op) (b->ext.omp_atomic & GFC_OMP_ATOMIC_MASK);
9935 /* Verify this before calling gfc_resolve_code, which might
9936 change it. */
9937 gcc_assert (b->next && b->next->op == EXEC_ASSIGN);
9938 gcc_assert (((aop != GFC_OMP_ATOMIC_CAPTURE)
9939 && b->next->next == NULL)
9940 || ((aop == GFC_OMP_ATOMIC_CAPTURE)
9941 && b->next->next != NULL
9942 && b->next->next->op == EXEC_ASSIGN
9943 && b->next->next->next == NULL));
9945 break;
9947 case EXEC_OACC_PARALLEL_LOOP:
9948 case EXEC_OACC_PARALLEL:
9949 case EXEC_OACC_KERNELS_LOOP:
9950 case EXEC_OACC_KERNELS:
9951 case EXEC_OACC_DATA:
9952 case EXEC_OACC_HOST_DATA:
9953 case EXEC_OACC_LOOP:
9954 case EXEC_OACC_UPDATE:
9955 case EXEC_OACC_WAIT:
9956 case EXEC_OACC_CACHE:
9957 case EXEC_OACC_ENTER_DATA:
9958 case EXEC_OACC_EXIT_DATA:
9959 case EXEC_OACC_ROUTINE:
9960 case EXEC_OMP_CRITICAL:
9961 case EXEC_OMP_DISTRIBUTE:
9962 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
9963 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
9964 case EXEC_OMP_DISTRIBUTE_SIMD:
9965 case EXEC_OMP_DO:
9966 case EXEC_OMP_DO_SIMD:
9967 case EXEC_OMP_MASTER:
9968 case EXEC_OMP_ORDERED:
9969 case EXEC_OMP_PARALLEL:
9970 case EXEC_OMP_PARALLEL_DO:
9971 case EXEC_OMP_PARALLEL_DO_SIMD:
9972 case EXEC_OMP_PARALLEL_SECTIONS:
9973 case EXEC_OMP_PARALLEL_WORKSHARE:
9974 case EXEC_OMP_SECTIONS:
9975 case EXEC_OMP_SIMD:
9976 case EXEC_OMP_SINGLE:
9977 case EXEC_OMP_TARGET:
9978 case EXEC_OMP_TARGET_DATA:
9979 case EXEC_OMP_TARGET_ENTER_DATA:
9980 case EXEC_OMP_TARGET_EXIT_DATA:
9981 case EXEC_OMP_TARGET_PARALLEL:
9982 case EXEC_OMP_TARGET_PARALLEL_DO:
9983 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
9984 case EXEC_OMP_TARGET_SIMD:
9985 case EXEC_OMP_TARGET_TEAMS:
9986 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
9987 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
9988 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9989 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
9990 case EXEC_OMP_TARGET_UPDATE:
9991 case EXEC_OMP_TASK:
9992 case EXEC_OMP_TASKGROUP:
9993 case EXEC_OMP_TASKLOOP:
9994 case EXEC_OMP_TASKLOOP_SIMD:
9995 case EXEC_OMP_TASKWAIT:
9996 case EXEC_OMP_TASKYIELD:
9997 case EXEC_OMP_TEAMS:
9998 case EXEC_OMP_TEAMS_DISTRIBUTE:
9999 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10000 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10001 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10002 case EXEC_OMP_WORKSHARE:
10003 break;
10005 default:
10006 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
10009 gfc_resolve_code (b->next, ns);
10014 /* Does everything to resolve an ordinary assignment. Returns true
10015 if this is an interface assignment. */
10016 static bool
10017 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
10019 bool rval = false;
10020 gfc_expr *lhs;
10021 gfc_expr *rhs;
10022 int llen = 0;
10023 int rlen = 0;
10024 int n;
10025 gfc_ref *ref;
10026 symbol_attribute attr;
10028 if (gfc_extend_assign (code, ns))
10030 gfc_expr** rhsptr;
10032 if (code->op == EXEC_ASSIGN_CALL)
10034 lhs = code->ext.actual->expr;
10035 rhsptr = &code->ext.actual->next->expr;
10037 else
10039 gfc_actual_arglist* args;
10040 gfc_typebound_proc* tbp;
10042 gcc_assert (code->op == EXEC_COMPCALL);
10044 args = code->expr1->value.compcall.actual;
10045 lhs = args->expr;
10046 rhsptr = &args->next->expr;
10048 tbp = code->expr1->value.compcall.tbp;
10049 gcc_assert (!tbp->is_generic);
10052 /* Make a temporary rhs when there is a default initializer
10053 and rhs is the same symbol as the lhs. */
10054 if ((*rhsptr)->expr_type == EXPR_VARIABLE
10055 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
10056 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
10057 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
10058 *rhsptr = gfc_get_parentheses (*rhsptr);
10060 return true;
10063 lhs = code->expr1;
10064 rhs = code->expr2;
10066 if (rhs->is_boz
10067 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
10068 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
10069 &code->loc))
10070 return false;
10072 /* Handle the case of a BOZ literal on the RHS. */
10073 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
10075 int rc;
10076 if (warn_surprising)
10077 gfc_warning (OPT_Wsurprising,
10078 "BOZ literal at %L is bitwise transferred "
10079 "non-integer symbol %qs", &code->loc,
10080 lhs->symtree->n.sym->name);
10082 if (!gfc_convert_boz (rhs, &lhs->ts))
10083 return false;
10084 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
10086 if (rc == ARITH_UNDERFLOW)
10087 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
10088 ". This check can be disabled with the option "
10089 "%<-fno-range-check%>", &rhs->where);
10090 else if (rc == ARITH_OVERFLOW)
10091 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
10092 ". This check can be disabled with the option "
10093 "%<-fno-range-check%>", &rhs->where);
10094 else if (rc == ARITH_NAN)
10095 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
10096 ". This check can be disabled with the option "
10097 "%<-fno-range-check%>", &rhs->where);
10098 return false;
10102 if (lhs->ts.type == BT_CHARACTER
10103 && warn_character_truncation)
10105 if (lhs->ts.u.cl != NULL
10106 && lhs->ts.u.cl->length != NULL
10107 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10108 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
10110 if (rhs->expr_type == EXPR_CONSTANT)
10111 rlen = rhs->value.character.length;
10113 else if (rhs->ts.u.cl != NULL
10114 && rhs->ts.u.cl->length != NULL
10115 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10116 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
10118 if (rlen && llen && rlen > llen)
10119 gfc_warning_now (OPT_Wcharacter_truncation,
10120 "CHARACTER expression will be truncated "
10121 "in assignment (%d/%d) at %L",
10122 llen, rlen, &code->loc);
10125 /* Ensure that a vector index expression for the lvalue is evaluated
10126 to a temporary if the lvalue symbol is referenced in it. */
10127 if (lhs->rank)
10129 for (ref = lhs->ref; ref; ref= ref->next)
10130 if (ref->type == REF_ARRAY)
10132 for (n = 0; n < ref->u.ar.dimen; n++)
10133 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
10134 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
10135 ref->u.ar.start[n]))
10136 ref->u.ar.start[n]
10137 = gfc_get_parentheses (ref->u.ar.start[n]);
10141 if (gfc_pure (NULL))
10143 if (lhs->ts.type == BT_DERIVED
10144 && lhs->expr_type == EXPR_VARIABLE
10145 && lhs->ts.u.derived->attr.pointer_comp
10146 && rhs->expr_type == EXPR_VARIABLE
10147 && (gfc_impure_variable (rhs->symtree->n.sym)
10148 || gfc_is_coindexed (rhs)))
10150 /* F2008, C1283. */
10151 if (gfc_is_coindexed (rhs))
10152 gfc_error ("Coindexed expression at %L is assigned to "
10153 "a derived type variable with a POINTER "
10154 "component in a PURE procedure",
10155 &rhs->where);
10156 else
10157 gfc_error ("The impure variable at %L is assigned to "
10158 "a derived type variable with a POINTER "
10159 "component in a PURE procedure (12.6)",
10160 &rhs->where);
10161 return rval;
10164 /* Fortran 2008, C1283. */
10165 if (gfc_is_coindexed (lhs))
10167 gfc_error ("Assignment to coindexed variable at %L in a PURE "
10168 "procedure", &rhs->where);
10169 return rval;
10173 if (gfc_implicit_pure (NULL))
10175 if (lhs->expr_type == EXPR_VARIABLE
10176 && lhs->symtree->n.sym != gfc_current_ns->proc_name
10177 && lhs->symtree->n.sym->ns != gfc_current_ns)
10178 gfc_unset_implicit_pure (NULL);
10180 if (lhs->ts.type == BT_DERIVED
10181 && lhs->expr_type == EXPR_VARIABLE
10182 && lhs->ts.u.derived->attr.pointer_comp
10183 && rhs->expr_type == EXPR_VARIABLE
10184 && (gfc_impure_variable (rhs->symtree->n.sym)
10185 || gfc_is_coindexed (rhs)))
10186 gfc_unset_implicit_pure (NULL);
10188 /* Fortran 2008, C1283. */
10189 if (gfc_is_coindexed (lhs))
10190 gfc_unset_implicit_pure (NULL);
10193 /* F2008, 7.2.1.2. */
10194 attr = gfc_expr_attr (lhs);
10195 if (lhs->ts.type == BT_CLASS && attr.allocatable)
10197 if (attr.codimension)
10199 gfc_error ("Assignment to polymorphic coarray at %L is not "
10200 "permitted", &lhs->where);
10201 return false;
10203 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
10204 "polymorphic variable at %L", &lhs->where))
10205 return false;
10206 if (!flag_realloc_lhs)
10208 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
10209 "requires %<-frealloc-lhs%>", &lhs->where);
10210 return false;
10213 else if (lhs->ts.type == BT_CLASS)
10215 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
10216 "assignment at %L - check that there is a matching specific "
10217 "subroutine for '=' operator", &lhs->where);
10218 return false;
10221 bool lhs_coindexed = gfc_is_coindexed (lhs);
10223 /* F2008, Section 7.2.1.2. */
10224 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
10226 gfc_error ("Coindexed variable must not have an allocatable ultimate "
10227 "component in assignment at %L", &lhs->where);
10228 return false;
10231 /* Assign the 'data' of a class object to a derived type. */
10232 if (lhs->ts.type == BT_DERIVED
10233 && rhs->ts.type == BT_CLASS)
10234 gfc_add_data_component (rhs);
10236 bool caf_convert_to_send = flag_coarray == GFC_FCOARRAY_LIB
10237 && (lhs_coindexed
10238 || (code->expr2->expr_type == EXPR_FUNCTION
10239 && code->expr2->value.function.isym
10240 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
10241 && (code->expr1->rank == 0 || code->expr2->rank != 0)
10242 && !gfc_expr_attr (rhs).allocatable
10243 && !gfc_has_vector_subscript (rhs)));
10245 gfc_check_assign (lhs, rhs, 1, !caf_convert_to_send);
10247 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
10248 Additionally, insert this code when the RHS is a CAF as we then use the
10249 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
10250 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
10251 noncoindexed array and the RHS is a coindexed scalar, use the normal code
10252 path. */
10253 if (caf_convert_to_send)
10255 if (code->expr2->expr_type == EXPR_FUNCTION
10256 && code->expr2->value.function.isym
10257 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
10258 remove_caf_get_intrinsic (code->expr2);
10259 code->op = EXEC_CALL;
10260 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
10261 code->resolved_sym = code->symtree->n.sym;
10262 code->resolved_sym->attr.flavor = FL_PROCEDURE;
10263 code->resolved_sym->attr.intrinsic = 1;
10264 code->resolved_sym->attr.subroutine = 1;
10265 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
10266 gfc_commit_symbol (code->resolved_sym);
10267 code->ext.actual = gfc_get_actual_arglist ();
10268 code->ext.actual->expr = lhs;
10269 code->ext.actual->next = gfc_get_actual_arglist ();
10270 code->ext.actual->next->expr = rhs;
10271 code->expr1 = NULL;
10272 code->expr2 = NULL;
10275 return false;
10279 /* Add a component reference onto an expression. */
10281 static void
10282 add_comp_ref (gfc_expr *e, gfc_component *c)
10284 gfc_ref **ref;
10285 ref = &(e->ref);
10286 while (*ref)
10287 ref = &((*ref)->next);
10288 *ref = gfc_get_ref ();
10289 (*ref)->type = REF_COMPONENT;
10290 (*ref)->u.c.sym = e->ts.u.derived;
10291 (*ref)->u.c.component = c;
10292 e->ts = c->ts;
10294 /* Add a full array ref, as necessary. */
10295 if (c->as)
10297 gfc_add_full_array_ref (e, c->as);
10298 e->rank = c->as->rank;
10303 /* Build an assignment. Keep the argument 'op' for future use, so that
10304 pointer assignments can be made. */
10306 static gfc_code *
10307 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
10308 gfc_component *comp1, gfc_component *comp2, locus loc)
10310 gfc_code *this_code;
10312 this_code = gfc_get_code (op);
10313 this_code->next = NULL;
10314 this_code->expr1 = gfc_copy_expr (expr1);
10315 this_code->expr2 = gfc_copy_expr (expr2);
10316 this_code->loc = loc;
10317 if (comp1 && comp2)
10319 add_comp_ref (this_code->expr1, comp1);
10320 add_comp_ref (this_code->expr2, comp2);
10323 return this_code;
10327 /* Makes a temporary variable expression based on the characteristics of
10328 a given variable expression. */
10330 static gfc_expr*
10331 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
10333 static int serial = 0;
10334 char name[GFC_MAX_SYMBOL_LEN];
10335 gfc_symtree *tmp;
10336 gfc_array_spec *as;
10337 gfc_array_ref *aref;
10338 gfc_ref *ref;
10340 sprintf (name, GFC_PREFIX("DA%d"), serial++);
10341 gfc_get_sym_tree (name, ns, &tmp, false);
10342 gfc_add_type (tmp->n.sym, &e->ts, NULL);
10344 as = NULL;
10345 ref = NULL;
10346 aref = NULL;
10348 /* Obtain the arrayspec for the temporary. */
10349 if (e->rank && e->expr_type != EXPR_ARRAY
10350 && e->expr_type != EXPR_FUNCTION
10351 && e->expr_type != EXPR_OP)
10353 aref = gfc_find_array_ref (e);
10354 if (e->expr_type == EXPR_VARIABLE
10355 && e->symtree->n.sym->as == aref->as)
10356 as = aref->as;
10357 else
10359 for (ref = e->ref; ref; ref = ref->next)
10360 if (ref->type == REF_COMPONENT
10361 && ref->u.c.component->as == aref->as)
10363 as = aref->as;
10364 break;
10369 /* Add the attributes and the arrayspec to the temporary. */
10370 tmp->n.sym->attr = gfc_expr_attr (e);
10371 tmp->n.sym->attr.function = 0;
10372 tmp->n.sym->attr.result = 0;
10373 tmp->n.sym->attr.flavor = FL_VARIABLE;
10375 if (as)
10377 tmp->n.sym->as = gfc_copy_array_spec (as);
10378 if (!ref)
10379 ref = e->ref;
10380 if (as->type == AS_DEFERRED)
10381 tmp->n.sym->attr.allocatable = 1;
10383 else if (e->rank && (e->expr_type == EXPR_ARRAY
10384 || e->expr_type == EXPR_FUNCTION
10385 || e->expr_type == EXPR_OP))
10387 tmp->n.sym->as = gfc_get_array_spec ();
10388 tmp->n.sym->as->type = AS_DEFERRED;
10389 tmp->n.sym->as->rank = e->rank;
10390 tmp->n.sym->attr.allocatable = 1;
10391 tmp->n.sym->attr.dimension = 1;
10393 else
10394 tmp->n.sym->attr.dimension = 0;
10396 gfc_set_sym_referenced (tmp->n.sym);
10397 gfc_commit_symbol (tmp->n.sym);
10398 e = gfc_lval_expr_from_sym (tmp->n.sym);
10400 /* Should the lhs be a section, use its array ref for the
10401 temporary expression. */
10402 if (aref && aref->type != AR_FULL)
10404 gfc_free_ref_list (e->ref);
10405 e->ref = gfc_copy_ref (ref);
10407 return e;
10411 /* Add one line of code to the code chain, making sure that 'head' and
10412 'tail' are appropriately updated. */
10414 static void
10415 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
10417 gcc_assert (this_code);
10418 if (*head == NULL)
10419 *head = *tail = *this_code;
10420 else
10421 *tail = gfc_append_code (*tail, *this_code);
10422 *this_code = NULL;
10426 /* Counts the potential number of part array references that would
10427 result from resolution of typebound defined assignments. */
10429 static int
10430 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
10432 gfc_component *c;
10433 int c_depth = 0, t_depth;
10435 for (c= derived->components; c; c = c->next)
10437 if ((!gfc_bt_struct (c->ts.type)
10438 || c->attr.pointer
10439 || c->attr.allocatable
10440 || c->attr.proc_pointer_comp
10441 || c->attr.class_pointer
10442 || c->attr.proc_pointer)
10443 && !c->attr.defined_assign_comp)
10444 continue;
10446 if (c->as && c_depth == 0)
10447 c_depth = 1;
10449 if (c->ts.u.derived->attr.defined_assign_comp)
10450 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
10451 c->as ? 1 : 0);
10452 else
10453 t_depth = 0;
10455 c_depth = t_depth > c_depth ? t_depth : c_depth;
10457 return depth + c_depth;
10461 /* Implement 7.2.1.3 of the F08 standard:
10462 "An intrinsic assignment where the variable is of derived type is
10463 performed as if each component of the variable were assigned from the
10464 corresponding component of expr using pointer assignment (7.2.2) for
10465 each pointer component, defined assignment for each nonpointer
10466 nonallocatable component of a type that has a type-bound defined
10467 assignment consistent with the component, intrinsic assignment for
10468 each other nonpointer nonallocatable component, ..."
10470 The pointer assignments are taken care of by the intrinsic
10471 assignment of the structure itself. This function recursively adds
10472 defined assignments where required. The recursion is accomplished
10473 by calling gfc_resolve_code.
10475 When the lhs in a defined assignment has intent INOUT, we need a
10476 temporary for the lhs. In pseudo-code:
10478 ! Only call function lhs once.
10479 if (lhs is not a constant or an variable)
10480 temp_x = expr2
10481 expr2 => temp_x
10482 ! Do the intrinsic assignment
10483 expr1 = expr2
10484 ! Now do the defined assignments
10485 do over components with typebound defined assignment [%cmp]
10486 #if one component's assignment procedure is INOUT
10487 t1 = expr1
10488 #if expr2 non-variable
10489 temp_x = expr2
10490 expr2 => temp_x
10491 # endif
10492 expr1 = expr2
10493 # for each cmp
10494 t1%cmp {defined=} expr2%cmp
10495 expr1%cmp = t1%cmp
10496 #else
10497 expr1 = expr2
10499 # for each cmp
10500 expr1%cmp {defined=} expr2%cmp
10501 #endif
10504 /* The temporary assignments have to be put on top of the additional
10505 code to avoid the result being changed by the intrinsic assignment.
10507 static int component_assignment_level = 0;
10508 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
10510 static void
10511 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
10513 gfc_component *comp1, *comp2;
10514 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
10515 gfc_expr *t1;
10516 int error_count, depth;
10518 gfc_get_errors (NULL, &error_count);
10520 /* Filter out continuing processing after an error. */
10521 if (error_count
10522 || (*code)->expr1->ts.type != BT_DERIVED
10523 || (*code)->expr2->ts.type != BT_DERIVED)
10524 return;
10526 /* TODO: Handle more than one part array reference in assignments. */
10527 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
10528 (*code)->expr1->rank ? 1 : 0);
10529 if (depth > 1)
10531 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
10532 "done because multiple part array references would "
10533 "occur in intermediate expressions.", &(*code)->loc);
10534 return;
10537 component_assignment_level++;
10539 /* Create a temporary so that functions get called only once. */
10540 if ((*code)->expr2->expr_type != EXPR_VARIABLE
10541 && (*code)->expr2->expr_type != EXPR_CONSTANT)
10543 gfc_expr *tmp_expr;
10545 /* Assign the rhs to the temporary. */
10546 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
10547 this_code = build_assignment (EXEC_ASSIGN,
10548 tmp_expr, (*code)->expr2,
10549 NULL, NULL, (*code)->loc);
10550 /* Add the code and substitute the rhs expression. */
10551 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
10552 gfc_free_expr ((*code)->expr2);
10553 (*code)->expr2 = tmp_expr;
10556 /* Do the intrinsic assignment. This is not needed if the lhs is one
10557 of the temporaries generated here, since the intrinsic assignment
10558 to the final result already does this. */
10559 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
10561 this_code = build_assignment (EXEC_ASSIGN,
10562 (*code)->expr1, (*code)->expr2,
10563 NULL, NULL, (*code)->loc);
10564 add_code_to_chain (&this_code, &head, &tail);
10567 comp1 = (*code)->expr1->ts.u.derived->components;
10568 comp2 = (*code)->expr2->ts.u.derived->components;
10570 t1 = NULL;
10571 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
10573 bool inout = false;
10575 /* The intrinsic assignment does the right thing for pointers
10576 of all kinds and allocatable components. */
10577 if (!gfc_bt_struct (comp1->ts.type)
10578 || comp1->attr.pointer
10579 || comp1->attr.allocatable
10580 || comp1->attr.proc_pointer_comp
10581 || comp1->attr.class_pointer
10582 || comp1->attr.proc_pointer)
10583 continue;
10585 /* Make an assigment for this component. */
10586 this_code = build_assignment (EXEC_ASSIGN,
10587 (*code)->expr1, (*code)->expr2,
10588 comp1, comp2, (*code)->loc);
10590 /* Convert the assignment if there is a defined assignment for
10591 this type. Otherwise, using the call from gfc_resolve_code,
10592 recurse into its components. */
10593 gfc_resolve_code (this_code, ns);
10595 if (this_code->op == EXEC_ASSIGN_CALL)
10597 gfc_formal_arglist *dummy_args;
10598 gfc_symbol *rsym;
10599 /* Check that there is a typebound defined assignment. If not,
10600 then this must be a module defined assignment. We cannot
10601 use the defined_assign_comp attribute here because it must
10602 be this derived type that has the defined assignment and not
10603 a parent type. */
10604 if (!(comp1->ts.u.derived->f2k_derived
10605 && comp1->ts.u.derived->f2k_derived
10606 ->tb_op[INTRINSIC_ASSIGN]))
10608 gfc_free_statements (this_code);
10609 this_code = NULL;
10610 continue;
10613 /* If the first argument of the subroutine has intent INOUT
10614 a temporary must be generated and used instead. */
10615 rsym = this_code->resolved_sym;
10616 dummy_args = gfc_sym_get_dummy_args (rsym);
10617 if (dummy_args
10618 && dummy_args->sym->attr.intent == INTENT_INOUT)
10620 gfc_code *temp_code;
10621 inout = true;
10623 /* Build the temporary required for the assignment and put
10624 it at the head of the generated code. */
10625 if (!t1)
10627 t1 = get_temp_from_expr ((*code)->expr1, ns);
10628 temp_code = build_assignment (EXEC_ASSIGN,
10629 t1, (*code)->expr1,
10630 NULL, NULL, (*code)->loc);
10632 /* For allocatable LHS, check whether it is allocated. Note
10633 that allocatable components with defined assignment are
10634 not yet support. See PR 57696. */
10635 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
10637 gfc_code *block;
10638 gfc_expr *e =
10639 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10640 block = gfc_get_code (EXEC_IF);
10641 block->block = gfc_get_code (EXEC_IF);
10642 block->block->expr1
10643 = gfc_build_intrinsic_call (ns,
10644 GFC_ISYM_ALLOCATED, "allocated",
10645 (*code)->loc, 1, e);
10646 block->block->next = temp_code;
10647 temp_code = block;
10649 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
10652 /* Replace the first actual arg with the component of the
10653 temporary. */
10654 gfc_free_expr (this_code->ext.actual->expr);
10655 this_code->ext.actual->expr = gfc_copy_expr (t1);
10656 add_comp_ref (this_code->ext.actual->expr, comp1);
10658 /* If the LHS variable is allocatable and wasn't allocated and
10659 the temporary is allocatable, pointer assign the address of
10660 the freshly allocated LHS to the temporary. */
10661 if ((*code)->expr1->symtree->n.sym->attr.allocatable
10662 && gfc_expr_attr ((*code)->expr1).allocatable)
10664 gfc_code *block;
10665 gfc_expr *cond;
10667 cond = gfc_get_expr ();
10668 cond->ts.type = BT_LOGICAL;
10669 cond->ts.kind = gfc_default_logical_kind;
10670 cond->expr_type = EXPR_OP;
10671 cond->where = (*code)->loc;
10672 cond->value.op.op = INTRINSIC_NOT;
10673 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
10674 GFC_ISYM_ALLOCATED, "allocated",
10675 (*code)->loc, 1, gfc_copy_expr (t1));
10676 block = gfc_get_code (EXEC_IF);
10677 block->block = gfc_get_code (EXEC_IF);
10678 block->block->expr1 = cond;
10679 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10680 t1, (*code)->expr1,
10681 NULL, NULL, (*code)->loc);
10682 add_code_to_chain (&block, &head, &tail);
10686 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
10688 /* Don't add intrinsic assignments since they are already
10689 effected by the intrinsic assignment of the structure. */
10690 gfc_free_statements (this_code);
10691 this_code = NULL;
10692 continue;
10695 add_code_to_chain (&this_code, &head, &tail);
10697 if (t1 && inout)
10699 /* Transfer the value to the final result. */
10700 this_code = build_assignment (EXEC_ASSIGN,
10701 (*code)->expr1, t1,
10702 comp1, comp2, (*code)->loc);
10703 add_code_to_chain (&this_code, &head, &tail);
10707 /* Put the temporary assignments at the top of the generated code. */
10708 if (tmp_head && component_assignment_level == 1)
10710 gfc_append_code (tmp_head, head);
10711 head = tmp_head;
10712 tmp_head = tmp_tail = NULL;
10715 // If we did a pointer assignment - thus, we need to ensure that the LHS is
10716 // not accidentally deallocated. Hence, nullify t1.
10717 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
10718 && gfc_expr_attr ((*code)->expr1).allocatable)
10720 gfc_code *block;
10721 gfc_expr *cond;
10722 gfc_expr *e;
10724 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10725 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
10726 (*code)->loc, 2, gfc_copy_expr (t1), e);
10727 block = gfc_get_code (EXEC_IF);
10728 block->block = gfc_get_code (EXEC_IF);
10729 block->block->expr1 = cond;
10730 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10731 t1, gfc_get_null_expr (&(*code)->loc),
10732 NULL, NULL, (*code)->loc);
10733 gfc_append_code (tail, block);
10734 tail = block;
10737 /* Now attach the remaining code chain to the input code. Step on
10738 to the end of the new code since resolution is complete. */
10739 gcc_assert ((*code)->op == EXEC_ASSIGN);
10740 tail->next = (*code)->next;
10741 /* Overwrite 'code' because this would place the intrinsic assignment
10742 before the temporary for the lhs is created. */
10743 gfc_free_expr ((*code)->expr1);
10744 gfc_free_expr ((*code)->expr2);
10745 **code = *head;
10746 if (head != tail)
10747 free (head);
10748 *code = tail;
10750 component_assignment_level--;
10754 /* F2008: Pointer function assignments are of the form:
10755 ptr_fcn (args) = expr
10756 This function breaks these assignments into two statements:
10757 temporary_pointer => ptr_fcn(args)
10758 temporary_pointer = expr */
10760 static bool
10761 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
10763 gfc_expr *tmp_ptr_expr;
10764 gfc_code *this_code;
10765 gfc_component *comp;
10766 gfc_symbol *s;
10768 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
10769 return false;
10771 /* Even if standard does not support this feature, continue to build
10772 the two statements to avoid upsetting frontend_passes.c. */
10773 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
10774 "%L", &(*code)->loc);
10776 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
10778 if (comp)
10779 s = comp->ts.interface;
10780 else
10781 s = (*code)->expr1->symtree->n.sym;
10783 if (s == NULL || !s->result->attr.pointer)
10785 gfc_error ("The function result on the lhs of the assignment at "
10786 "%L must have the pointer attribute.",
10787 &(*code)->expr1->where);
10788 (*code)->op = EXEC_NOP;
10789 return false;
10792 tmp_ptr_expr = get_temp_from_expr ((*code)->expr2, ns);
10794 /* get_temp_from_expression is set up for ordinary assignments. To that
10795 end, where array bounds are not known, arrays are made allocatable.
10796 Change the temporary to a pointer here. */
10797 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
10798 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
10799 tmp_ptr_expr->where = (*code)->loc;
10801 this_code = build_assignment (EXEC_ASSIGN,
10802 tmp_ptr_expr, (*code)->expr2,
10803 NULL, NULL, (*code)->loc);
10804 this_code->next = (*code)->next;
10805 (*code)->next = this_code;
10806 (*code)->op = EXEC_POINTER_ASSIGN;
10807 (*code)->expr2 = (*code)->expr1;
10808 (*code)->expr1 = tmp_ptr_expr;
10810 return true;
10814 /* Deferred character length assignments from an operator expression
10815 require a temporary because the character length of the lhs can
10816 change in the course of the assignment. */
10818 static bool
10819 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
10821 gfc_expr *tmp_expr;
10822 gfc_code *this_code;
10824 if (!((*code)->expr1->ts.type == BT_CHARACTER
10825 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
10826 && (*code)->expr2->expr_type == EXPR_OP))
10827 return false;
10829 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
10830 return false;
10832 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
10833 tmp_expr->where = (*code)->loc;
10835 /* A new charlen is required to ensure that the variable string
10836 length is different to that of the original lhs. */
10837 tmp_expr->ts.u.cl = gfc_get_charlen();
10838 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
10839 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
10840 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
10842 tmp_expr->symtree->n.sym->ts.deferred = 1;
10844 this_code = build_assignment (EXEC_ASSIGN,
10845 (*code)->expr1,
10846 gfc_copy_expr (tmp_expr),
10847 NULL, NULL, (*code)->loc);
10849 (*code)->expr1 = tmp_expr;
10851 this_code->next = (*code)->next;
10852 (*code)->next = this_code;
10854 return true;
10858 /* Given a block of code, recursively resolve everything pointed to by this
10859 code block. */
10861 void
10862 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
10864 int omp_workshare_save;
10865 int forall_save, do_concurrent_save;
10866 code_stack frame;
10867 bool t;
10869 frame.prev = cs_base;
10870 frame.head = code;
10871 cs_base = &frame;
10873 find_reachable_labels (code);
10875 for (; code; code = code->next)
10877 frame.current = code;
10878 forall_save = forall_flag;
10879 do_concurrent_save = gfc_do_concurrent_flag;
10881 if (code->op == EXEC_FORALL)
10883 forall_flag = 1;
10884 gfc_resolve_forall (code, ns, forall_save);
10885 forall_flag = 2;
10887 else if (code->block)
10889 omp_workshare_save = -1;
10890 switch (code->op)
10892 case EXEC_OACC_PARALLEL_LOOP:
10893 case EXEC_OACC_PARALLEL:
10894 case EXEC_OACC_KERNELS_LOOP:
10895 case EXEC_OACC_KERNELS:
10896 case EXEC_OACC_DATA:
10897 case EXEC_OACC_HOST_DATA:
10898 case EXEC_OACC_LOOP:
10899 gfc_resolve_oacc_blocks (code, ns);
10900 break;
10901 case EXEC_OMP_PARALLEL_WORKSHARE:
10902 omp_workshare_save = omp_workshare_flag;
10903 omp_workshare_flag = 1;
10904 gfc_resolve_omp_parallel_blocks (code, ns);
10905 break;
10906 case EXEC_OMP_PARALLEL:
10907 case EXEC_OMP_PARALLEL_DO:
10908 case EXEC_OMP_PARALLEL_DO_SIMD:
10909 case EXEC_OMP_PARALLEL_SECTIONS:
10910 case EXEC_OMP_TARGET_PARALLEL:
10911 case EXEC_OMP_TARGET_PARALLEL_DO:
10912 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
10913 case EXEC_OMP_TARGET_TEAMS:
10914 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10915 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10916 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10917 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10918 case EXEC_OMP_TASK:
10919 case EXEC_OMP_TEAMS:
10920 case EXEC_OMP_TEAMS_DISTRIBUTE:
10921 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10922 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10923 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10924 omp_workshare_save = omp_workshare_flag;
10925 omp_workshare_flag = 0;
10926 gfc_resolve_omp_parallel_blocks (code, ns);
10927 break;
10928 case EXEC_OMP_DISTRIBUTE:
10929 case EXEC_OMP_DISTRIBUTE_SIMD:
10930 case EXEC_OMP_DO:
10931 case EXEC_OMP_DO_SIMD:
10932 case EXEC_OMP_SIMD:
10933 case EXEC_OMP_TARGET_SIMD:
10934 case EXEC_OMP_TASKLOOP:
10935 case EXEC_OMP_TASKLOOP_SIMD:
10936 gfc_resolve_omp_do_blocks (code, ns);
10937 break;
10938 case EXEC_SELECT_TYPE:
10939 /* Blocks are handled in resolve_select_type because we have
10940 to transform the SELECT TYPE into ASSOCIATE first. */
10941 break;
10942 case EXEC_DO_CONCURRENT:
10943 gfc_do_concurrent_flag = 1;
10944 gfc_resolve_blocks (code->block, ns);
10945 gfc_do_concurrent_flag = 2;
10946 break;
10947 case EXEC_OMP_WORKSHARE:
10948 omp_workshare_save = omp_workshare_flag;
10949 omp_workshare_flag = 1;
10950 /* FALL THROUGH */
10951 default:
10952 gfc_resolve_blocks (code->block, ns);
10953 break;
10956 if (omp_workshare_save != -1)
10957 omp_workshare_flag = omp_workshare_save;
10959 start:
10960 t = true;
10961 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
10962 t = gfc_resolve_expr (code->expr1);
10963 forall_flag = forall_save;
10964 gfc_do_concurrent_flag = do_concurrent_save;
10966 if (!gfc_resolve_expr (code->expr2))
10967 t = false;
10969 if (code->op == EXEC_ALLOCATE
10970 && !gfc_resolve_expr (code->expr3))
10971 t = false;
10973 switch (code->op)
10975 case EXEC_NOP:
10976 case EXEC_END_BLOCK:
10977 case EXEC_END_NESTED_BLOCK:
10978 case EXEC_CYCLE:
10979 case EXEC_PAUSE:
10980 case EXEC_STOP:
10981 case EXEC_ERROR_STOP:
10982 case EXEC_EXIT:
10983 case EXEC_CONTINUE:
10984 case EXEC_DT_END:
10985 case EXEC_ASSIGN_CALL:
10986 break;
10988 case EXEC_CRITICAL:
10989 resolve_critical (code);
10990 break;
10992 case EXEC_SYNC_ALL:
10993 case EXEC_SYNC_IMAGES:
10994 case EXEC_SYNC_MEMORY:
10995 resolve_sync (code);
10996 break;
10998 case EXEC_LOCK:
10999 case EXEC_UNLOCK:
11000 case EXEC_EVENT_POST:
11001 case EXEC_EVENT_WAIT:
11002 resolve_lock_unlock_event (code);
11003 break;
11005 case EXEC_FAIL_IMAGE:
11006 break;
11008 case EXEC_ENTRY:
11009 /* Keep track of which entry we are up to. */
11010 current_entry_id = code->ext.entry->id;
11011 break;
11013 case EXEC_WHERE:
11014 resolve_where (code, NULL);
11015 break;
11017 case EXEC_GOTO:
11018 if (code->expr1 != NULL)
11020 if (code->expr1->ts.type != BT_INTEGER)
11021 gfc_error ("ASSIGNED GOTO statement at %L requires an "
11022 "INTEGER variable", &code->expr1->where);
11023 else if (code->expr1->symtree->n.sym->attr.assign != 1)
11024 gfc_error ("Variable %qs has not been assigned a target "
11025 "label at %L", code->expr1->symtree->n.sym->name,
11026 &code->expr1->where);
11028 else
11029 resolve_branch (code->label1, code);
11030 break;
11032 case EXEC_RETURN:
11033 if (code->expr1 != NULL
11034 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
11035 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
11036 "INTEGER return specifier", &code->expr1->where);
11037 break;
11039 case EXEC_INIT_ASSIGN:
11040 case EXEC_END_PROCEDURE:
11041 break;
11043 case EXEC_ASSIGN:
11044 if (!t)
11045 break;
11047 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
11048 the LHS. */
11049 if (code->expr1->expr_type == EXPR_FUNCTION
11050 && code->expr1->value.function.isym
11051 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
11052 remove_caf_get_intrinsic (code->expr1);
11054 /* If this is a pointer function in an lvalue variable context,
11055 the new code will have to be resolved afresh. This is also the
11056 case with an error, where the code is transformed into NOP to
11057 prevent ICEs downstream. */
11058 if (resolve_ptr_fcn_assign (&code, ns)
11059 || code->op == EXEC_NOP)
11060 goto start;
11062 if (!gfc_check_vardef_context (code->expr1, false, false, false,
11063 _("assignment")))
11064 break;
11066 if (resolve_ordinary_assign (code, ns))
11068 if (code->op == EXEC_COMPCALL)
11069 goto compcall;
11070 else
11071 goto call;
11074 /* Check for dependencies in deferred character length array
11075 assignments and generate a temporary, if necessary. */
11076 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
11077 break;
11079 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
11080 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
11081 && code->expr1->ts.u.derived
11082 && code->expr1->ts.u.derived->attr.defined_assign_comp)
11083 generate_component_assignments (&code, ns);
11085 break;
11087 case EXEC_LABEL_ASSIGN:
11088 if (code->label1->defined == ST_LABEL_UNKNOWN)
11089 gfc_error ("Label %d referenced at %L is never defined",
11090 code->label1->value, &code->label1->where);
11091 if (t
11092 && (code->expr1->expr_type != EXPR_VARIABLE
11093 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
11094 || code->expr1->symtree->n.sym->ts.kind
11095 != gfc_default_integer_kind
11096 || code->expr1->symtree->n.sym->as != NULL))
11097 gfc_error ("ASSIGN statement at %L requires a scalar "
11098 "default INTEGER variable", &code->expr1->where);
11099 break;
11101 case EXEC_POINTER_ASSIGN:
11103 gfc_expr* e;
11105 if (!t)
11106 break;
11108 /* This is both a variable definition and pointer assignment
11109 context, so check both of them. For rank remapping, a final
11110 array ref may be present on the LHS and fool gfc_expr_attr
11111 used in gfc_check_vardef_context. Remove it. */
11112 e = remove_last_array_ref (code->expr1);
11113 t = gfc_check_vardef_context (e, true, false, false,
11114 _("pointer assignment"));
11115 if (t)
11116 t = gfc_check_vardef_context (e, false, false, false,
11117 _("pointer assignment"));
11118 gfc_free_expr (e);
11119 if (!t)
11120 break;
11122 gfc_check_pointer_assign (code->expr1, code->expr2);
11124 /* Assigning a class object always is a regular assign. */
11125 if (code->expr2->ts.type == BT_CLASS
11126 && code->expr1->ts.type == BT_CLASS
11127 && !CLASS_DATA (code->expr2)->attr.dimension
11128 && !(gfc_expr_attr (code->expr1).proc_pointer
11129 && code->expr2->expr_type == EXPR_VARIABLE
11130 && code->expr2->symtree->n.sym->attr.flavor
11131 == FL_PROCEDURE))
11132 code->op = EXEC_ASSIGN;
11133 break;
11136 case EXEC_ARITHMETIC_IF:
11138 gfc_expr *e = code->expr1;
11140 gfc_resolve_expr (e);
11141 if (e->expr_type == EXPR_NULL)
11142 gfc_error ("Invalid NULL at %L", &e->where);
11144 if (t && (e->rank > 0
11145 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
11146 gfc_error ("Arithmetic IF statement at %L requires a scalar "
11147 "REAL or INTEGER expression", &e->where);
11149 resolve_branch (code->label1, code);
11150 resolve_branch (code->label2, code);
11151 resolve_branch (code->label3, code);
11153 break;
11155 case EXEC_IF:
11156 if (t && code->expr1 != NULL
11157 && (code->expr1->ts.type != BT_LOGICAL
11158 || code->expr1->rank != 0))
11159 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
11160 &code->expr1->where);
11161 break;
11163 case EXEC_CALL:
11164 call:
11165 resolve_call (code);
11166 break;
11168 case EXEC_COMPCALL:
11169 compcall:
11170 resolve_typebound_subroutine (code);
11171 break;
11173 case EXEC_CALL_PPC:
11174 resolve_ppc_call (code);
11175 break;
11177 case EXEC_SELECT:
11178 /* Select is complicated. Also, a SELECT construct could be
11179 a transformed computed GOTO. */
11180 resolve_select (code, false);
11181 break;
11183 case EXEC_SELECT_TYPE:
11184 resolve_select_type (code, ns);
11185 break;
11187 case EXEC_BLOCK:
11188 resolve_block_construct (code);
11189 break;
11191 case EXEC_DO:
11192 if (code->ext.iterator != NULL)
11194 gfc_iterator *iter = code->ext.iterator;
11195 if (gfc_resolve_iterator (iter, true, false))
11196 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
11198 break;
11200 case EXEC_DO_WHILE:
11201 if (code->expr1 == NULL)
11202 gfc_internal_error ("gfc_resolve_code(): No expression on "
11203 "DO WHILE");
11204 if (t
11205 && (code->expr1->rank != 0
11206 || code->expr1->ts.type != BT_LOGICAL))
11207 gfc_error ("Exit condition of DO WHILE loop at %L must be "
11208 "a scalar LOGICAL expression", &code->expr1->where);
11209 break;
11211 case EXEC_ALLOCATE:
11212 if (t)
11213 resolve_allocate_deallocate (code, "ALLOCATE");
11215 break;
11217 case EXEC_DEALLOCATE:
11218 if (t)
11219 resolve_allocate_deallocate (code, "DEALLOCATE");
11221 break;
11223 case EXEC_OPEN:
11224 if (!gfc_resolve_open (code->ext.open))
11225 break;
11227 resolve_branch (code->ext.open->err, code);
11228 break;
11230 case EXEC_CLOSE:
11231 if (!gfc_resolve_close (code->ext.close))
11232 break;
11234 resolve_branch (code->ext.close->err, code);
11235 break;
11237 case EXEC_BACKSPACE:
11238 case EXEC_ENDFILE:
11239 case EXEC_REWIND:
11240 case EXEC_FLUSH:
11241 if (!gfc_resolve_filepos (code->ext.filepos))
11242 break;
11244 resolve_branch (code->ext.filepos->err, code);
11245 break;
11247 case EXEC_INQUIRE:
11248 if (!gfc_resolve_inquire (code->ext.inquire))
11249 break;
11251 resolve_branch (code->ext.inquire->err, code);
11252 break;
11254 case EXEC_IOLENGTH:
11255 gcc_assert (code->ext.inquire != NULL);
11256 if (!gfc_resolve_inquire (code->ext.inquire))
11257 break;
11259 resolve_branch (code->ext.inquire->err, code);
11260 break;
11262 case EXEC_WAIT:
11263 if (!gfc_resolve_wait (code->ext.wait))
11264 break;
11266 resolve_branch (code->ext.wait->err, code);
11267 resolve_branch (code->ext.wait->end, code);
11268 resolve_branch (code->ext.wait->eor, code);
11269 break;
11271 case EXEC_READ:
11272 case EXEC_WRITE:
11273 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
11274 break;
11276 resolve_branch (code->ext.dt->err, code);
11277 resolve_branch (code->ext.dt->end, code);
11278 resolve_branch (code->ext.dt->eor, code);
11279 break;
11281 case EXEC_TRANSFER:
11282 resolve_transfer (code);
11283 break;
11285 case EXEC_DO_CONCURRENT:
11286 case EXEC_FORALL:
11287 resolve_forall_iterators (code->ext.forall_iterator);
11289 if (code->expr1 != NULL
11290 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
11291 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
11292 "expression", &code->expr1->where);
11293 break;
11295 case EXEC_OACC_PARALLEL_LOOP:
11296 case EXEC_OACC_PARALLEL:
11297 case EXEC_OACC_KERNELS_LOOP:
11298 case EXEC_OACC_KERNELS:
11299 case EXEC_OACC_DATA:
11300 case EXEC_OACC_HOST_DATA:
11301 case EXEC_OACC_LOOP:
11302 case EXEC_OACC_UPDATE:
11303 case EXEC_OACC_WAIT:
11304 case EXEC_OACC_CACHE:
11305 case EXEC_OACC_ENTER_DATA:
11306 case EXEC_OACC_EXIT_DATA:
11307 case EXEC_OACC_ATOMIC:
11308 case EXEC_OACC_DECLARE:
11309 gfc_resolve_oacc_directive (code, ns);
11310 break;
11312 case EXEC_OMP_ATOMIC:
11313 case EXEC_OMP_BARRIER:
11314 case EXEC_OMP_CANCEL:
11315 case EXEC_OMP_CANCELLATION_POINT:
11316 case EXEC_OMP_CRITICAL:
11317 case EXEC_OMP_FLUSH:
11318 case EXEC_OMP_DISTRIBUTE:
11319 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
11320 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
11321 case EXEC_OMP_DISTRIBUTE_SIMD:
11322 case EXEC_OMP_DO:
11323 case EXEC_OMP_DO_SIMD:
11324 case EXEC_OMP_MASTER:
11325 case EXEC_OMP_ORDERED:
11326 case EXEC_OMP_SECTIONS:
11327 case EXEC_OMP_SIMD:
11328 case EXEC_OMP_SINGLE:
11329 case EXEC_OMP_TARGET:
11330 case EXEC_OMP_TARGET_DATA:
11331 case EXEC_OMP_TARGET_ENTER_DATA:
11332 case EXEC_OMP_TARGET_EXIT_DATA:
11333 case EXEC_OMP_TARGET_PARALLEL:
11334 case EXEC_OMP_TARGET_PARALLEL_DO:
11335 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
11336 case EXEC_OMP_TARGET_SIMD:
11337 case EXEC_OMP_TARGET_TEAMS:
11338 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
11339 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
11340 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11341 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
11342 case EXEC_OMP_TARGET_UPDATE:
11343 case EXEC_OMP_TASK:
11344 case EXEC_OMP_TASKGROUP:
11345 case EXEC_OMP_TASKLOOP:
11346 case EXEC_OMP_TASKLOOP_SIMD:
11347 case EXEC_OMP_TASKWAIT:
11348 case EXEC_OMP_TASKYIELD:
11349 case EXEC_OMP_TEAMS:
11350 case EXEC_OMP_TEAMS_DISTRIBUTE:
11351 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
11352 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11353 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
11354 case EXEC_OMP_WORKSHARE:
11355 gfc_resolve_omp_directive (code, ns);
11356 break;
11358 case EXEC_OMP_PARALLEL:
11359 case EXEC_OMP_PARALLEL_DO:
11360 case EXEC_OMP_PARALLEL_DO_SIMD:
11361 case EXEC_OMP_PARALLEL_SECTIONS:
11362 case EXEC_OMP_PARALLEL_WORKSHARE:
11363 omp_workshare_save = omp_workshare_flag;
11364 omp_workshare_flag = 0;
11365 gfc_resolve_omp_directive (code, ns);
11366 omp_workshare_flag = omp_workshare_save;
11367 break;
11369 default:
11370 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
11374 cs_base = frame.prev;
11378 /* Resolve initial values and make sure they are compatible with
11379 the variable. */
11381 static void
11382 resolve_values (gfc_symbol *sym)
11384 bool t;
11386 if (sym->value == NULL)
11387 return;
11389 if (sym->value->expr_type == EXPR_STRUCTURE)
11390 t= resolve_structure_cons (sym->value, 1);
11391 else
11392 t = gfc_resolve_expr (sym->value);
11394 if (!t)
11395 return;
11397 gfc_check_assign_symbol (sym, NULL, sym->value);
11401 /* Verify any BIND(C) derived types in the namespace so we can report errors
11402 for them once, rather than for each variable declared of that type. */
11404 static void
11405 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
11407 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
11408 && derived_sym->attr.is_bind_c == 1)
11409 verify_bind_c_derived_type (derived_sym);
11411 return;
11415 /* Check the interfaces of DTIO procedures associated with derived
11416 type 'sym'. These procedures can either have typebound bindings or
11417 can appear in DTIO generic interfaces. */
11419 static void
11420 gfc_verify_DTIO_procedures (gfc_symbol *sym)
11422 if (!sym || sym->attr.flavor != FL_DERIVED)
11423 return;
11425 gfc_check_dtio_interfaces (sym);
11427 return;
11430 /* Verify that any binding labels used in a given namespace do not collide
11431 with the names or binding labels of any global symbols. Multiple INTERFACE
11432 for the same procedure are permitted. */
11434 static void
11435 gfc_verify_binding_labels (gfc_symbol *sym)
11437 gfc_gsymbol *gsym;
11438 const char *module;
11440 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
11441 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
11442 return;
11444 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
11446 if (sym->module)
11447 module = sym->module;
11448 else if (sym->ns && sym->ns->proc_name
11449 && sym->ns->proc_name->attr.flavor == FL_MODULE)
11450 module = sym->ns->proc_name->name;
11451 else if (sym->ns && sym->ns->parent
11452 && sym->ns && sym->ns->parent->proc_name
11453 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
11454 module = sym->ns->parent->proc_name->name;
11455 else
11456 module = NULL;
11458 if (!gsym
11459 || (!gsym->defined
11460 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
11462 if (!gsym)
11463 gsym = gfc_get_gsymbol (sym->binding_label);
11464 gsym->where = sym->declared_at;
11465 gsym->sym_name = sym->name;
11466 gsym->binding_label = sym->binding_label;
11467 gsym->ns = sym->ns;
11468 gsym->mod_name = module;
11469 if (sym->attr.function)
11470 gsym->type = GSYM_FUNCTION;
11471 else if (sym->attr.subroutine)
11472 gsym->type = GSYM_SUBROUTINE;
11473 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
11474 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
11475 return;
11478 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
11480 gfc_error ("Variable %s with binding label %s at %L uses the same global "
11481 "identifier as entity at %L", sym->name,
11482 sym->binding_label, &sym->declared_at, &gsym->where);
11483 /* Clear the binding label to prevent checking multiple times. */
11484 sym->binding_label = NULL;
11487 else if (sym->attr.flavor == FL_VARIABLE && module
11488 && (strcmp (module, gsym->mod_name) != 0
11489 || strcmp (sym->name, gsym->sym_name) != 0))
11491 /* This can only happen if the variable is defined in a module - if it
11492 isn't the same module, reject it. */
11493 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
11494 "the same global identifier as entity at %L from module %s",
11495 sym->name, module, sym->binding_label,
11496 &sym->declared_at, &gsym->where, gsym->mod_name);
11497 sym->binding_label = NULL;
11499 else if ((sym->attr.function || sym->attr.subroutine)
11500 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
11501 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
11502 && sym != gsym->ns->proc_name
11503 && (module != gsym->mod_name
11504 || strcmp (gsym->sym_name, sym->name) != 0
11505 || (module && strcmp (module, gsym->mod_name) != 0)))
11507 /* Print an error if the procedure is defined multiple times; we have to
11508 exclude references to the same procedure via module association or
11509 multiple checks for the same procedure. */
11510 gfc_error ("Procedure %s with binding label %s at %L uses the same "
11511 "global identifier as entity at %L", sym->name,
11512 sym->binding_label, &sym->declared_at, &gsym->where);
11513 sym->binding_label = NULL;
11518 /* Resolve an index expression. */
11520 static bool
11521 resolve_index_expr (gfc_expr *e)
11523 if (!gfc_resolve_expr (e))
11524 return false;
11526 if (!gfc_simplify_expr (e, 0))
11527 return false;
11529 if (!gfc_specification_expr (e))
11530 return false;
11532 return true;
11536 /* Resolve a charlen structure. */
11538 static bool
11539 resolve_charlen (gfc_charlen *cl)
11541 int i, k;
11542 bool saved_specification_expr;
11544 if (cl->resolved)
11545 return true;
11547 cl->resolved = 1;
11548 saved_specification_expr = specification_expr;
11549 specification_expr = true;
11551 if (cl->length_from_typespec)
11553 if (!gfc_resolve_expr (cl->length))
11555 specification_expr = saved_specification_expr;
11556 return false;
11559 if (!gfc_simplify_expr (cl->length, 0))
11561 specification_expr = saved_specification_expr;
11562 return false;
11565 else
11568 if (!resolve_index_expr (cl->length))
11570 specification_expr = saved_specification_expr;
11571 return false;
11575 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
11576 a negative value, the length of character entities declared is zero. */
11577 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
11578 gfc_replace_expr (cl->length,
11579 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
11581 /* Check that the character length is not too large. */
11582 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
11583 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
11584 && cl->length->ts.type == BT_INTEGER
11585 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
11587 gfc_error ("String length at %L is too large", &cl->length->where);
11588 specification_expr = saved_specification_expr;
11589 return false;
11592 specification_expr = saved_specification_expr;
11593 return true;
11597 /* Test for non-constant shape arrays. */
11599 static bool
11600 is_non_constant_shape_array (gfc_symbol *sym)
11602 gfc_expr *e;
11603 int i;
11604 bool not_constant;
11606 not_constant = false;
11607 if (sym->as != NULL)
11609 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
11610 has not been simplified; parameter array references. Do the
11611 simplification now. */
11612 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
11614 e = sym->as->lower[i];
11615 if (e && (!resolve_index_expr(e)
11616 || !gfc_is_constant_expr (e)))
11617 not_constant = true;
11618 e = sym->as->upper[i];
11619 if (e && (!resolve_index_expr(e)
11620 || !gfc_is_constant_expr (e)))
11621 not_constant = true;
11624 return not_constant;
11627 /* Given a symbol and an initialization expression, add code to initialize
11628 the symbol to the function entry. */
11629 static void
11630 build_init_assign (gfc_symbol *sym, gfc_expr *init)
11632 gfc_expr *lval;
11633 gfc_code *init_st;
11634 gfc_namespace *ns = sym->ns;
11636 /* Search for the function namespace if this is a contained
11637 function without an explicit result. */
11638 if (sym->attr.function && sym == sym->result
11639 && sym->name != sym->ns->proc_name->name)
11641 ns = ns->contained;
11642 for (;ns; ns = ns->sibling)
11643 if (strcmp (ns->proc_name->name, sym->name) == 0)
11644 break;
11647 if (ns == NULL)
11649 gfc_free_expr (init);
11650 return;
11653 /* Build an l-value expression for the result. */
11654 lval = gfc_lval_expr_from_sym (sym);
11656 /* Add the code at scope entry. */
11657 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
11658 init_st->next = ns->code;
11659 ns->code = init_st;
11661 /* Assign the default initializer to the l-value. */
11662 init_st->loc = sym->declared_at;
11663 init_st->expr1 = lval;
11664 init_st->expr2 = init;
11668 /* Whether or not we can generate a default initializer for a symbol. */
11670 static bool
11671 can_generate_init (gfc_symbol *sym)
11673 symbol_attribute *a;
11674 if (!sym)
11675 return false;
11676 a = &sym->attr;
11678 /* These symbols should never have a default initialization. */
11679 return !(
11680 a->allocatable
11681 || a->external
11682 || a->pointer
11683 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
11684 && (CLASS_DATA (sym)->attr.class_pointer
11685 || CLASS_DATA (sym)->attr.proc_pointer))
11686 || a->in_equivalence
11687 || a->in_common
11688 || a->data
11689 || sym->module
11690 || a->cray_pointee
11691 || a->cray_pointer
11692 || sym->assoc
11693 || (!a->referenced && !a->result)
11694 || (a->dummy && a->intent != INTENT_OUT)
11695 || (a->function && sym != sym->result)
11700 /* Assign the default initializer to a derived type variable or result. */
11702 static void
11703 apply_default_init (gfc_symbol *sym)
11705 gfc_expr *init = NULL;
11707 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11708 return;
11710 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
11711 init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
11713 if (init == NULL && sym->ts.type != BT_CLASS)
11714 return;
11716 build_init_assign (sym, init);
11717 sym->attr.referenced = 1;
11721 /* Build an initializer for a local. Returns null if the symbol should not have
11722 a default initialization. */
11724 static gfc_expr *
11725 build_default_init_expr (gfc_symbol *sym)
11727 /* These symbols should never have a default initialization. */
11728 if (sym->attr.allocatable
11729 || sym->attr.external
11730 || sym->attr.dummy
11731 || sym->attr.pointer
11732 || sym->attr.in_equivalence
11733 || sym->attr.in_common
11734 || sym->attr.data
11735 || sym->module
11736 || sym->attr.cray_pointee
11737 || sym->attr.cray_pointer
11738 || sym->assoc)
11739 return NULL;
11741 /* Get the appropriate init expression. */
11742 return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
11745 /* Add an initialization expression to a local variable. */
11746 static void
11747 apply_default_init_local (gfc_symbol *sym)
11749 gfc_expr *init = NULL;
11751 /* The symbol should be a variable or a function return value. */
11752 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11753 || (sym->attr.function && sym->result != sym))
11754 return;
11756 /* Try to build the initializer expression. If we can't initialize
11757 this symbol, then init will be NULL. */
11758 init = build_default_init_expr (sym);
11759 if (init == NULL)
11760 return;
11762 /* For saved variables, we don't want to add an initializer at function
11763 entry, so we just add a static initializer. Note that automatic variables
11764 are stack allocated even with -fno-automatic; we have also to exclude
11765 result variable, which are also nonstatic. */
11766 if (!sym->attr.automatic
11767 && (sym->attr.save || sym->ns->save_all
11768 || (flag_max_stack_var_size == 0 && !sym->attr.result
11769 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
11770 && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
11772 /* Don't clobber an existing initializer! */
11773 gcc_assert (sym->value == NULL);
11774 sym->value = init;
11775 return;
11778 build_init_assign (sym, init);
11782 /* Resolution of common features of flavors variable and procedure. */
11784 static bool
11785 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
11787 gfc_array_spec *as;
11789 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11790 as = CLASS_DATA (sym)->as;
11791 else
11792 as = sym->as;
11794 /* Constraints on deferred shape variable. */
11795 if (as == NULL || as->type != AS_DEFERRED)
11797 bool pointer, allocatable, dimension;
11799 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11801 pointer = CLASS_DATA (sym)->attr.class_pointer;
11802 allocatable = CLASS_DATA (sym)->attr.allocatable;
11803 dimension = CLASS_DATA (sym)->attr.dimension;
11805 else
11807 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
11808 allocatable = sym->attr.allocatable;
11809 dimension = sym->attr.dimension;
11812 if (allocatable)
11814 if (dimension && as->type != AS_ASSUMED_RANK)
11816 gfc_error ("Allocatable array %qs at %L must have a deferred "
11817 "shape or assumed rank", sym->name, &sym->declared_at);
11818 return false;
11820 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
11821 "%qs at %L may not be ALLOCATABLE",
11822 sym->name, &sym->declared_at))
11823 return false;
11826 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
11828 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
11829 "assumed rank", sym->name, &sym->declared_at);
11830 return false;
11833 else
11835 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
11836 && sym->ts.type != BT_CLASS && !sym->assoc)
11838 gfc_error ("Array %qs at %L cannot have a deferred shape",
11839 sym->name, &sym->declared_at);
11840 return false;
11844 /* Constraints on polymorphic variables. */
11845 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
11847 /* F03:C502. */
11848 if (sym->attr.class_ok
11849 && !sym->attr.select_type_temporary
11850 && !UNLIMITED_POLY (sym)
11851 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
11853 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
11854 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
11855 &sym->declared_at);
11856 return false;
11859 /* F03:C509. */
11860 /* Assume that use associated symbols were checked in the module ns.
11861 Class-variables that are associate-names are also something special
11862 and excepted from the test. */
11863 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
11865 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
11866 "or pointer", sym->name, &sym->declared_at);
11867 return false;
11871 return true;
11875 /* Additional checks for symbols with flavor variable and derived
11876 type. To be called from resolve_fl_variable. */
11878 static bool
11879 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
11881 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
11883 /* Check to see if a derived type is blocked from being host
11884 associated by the presence of another class I symbol in the same
11885 namespace. 14.6.1.3 of the standard and the discussion on
11886 comp.lang.fortran. */
11887 if (sym->ns != sym->ts.u.derived->ns
11888 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
11890 gfc_symbol *s;
11891 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
11892 if (s && s->attr.generic)
11893 s = gfc_find_dt_in_generic (s);
11894 if (s && !gfc_fl_struct (s->attr.flavor))
11896 gfc_error ("The type %qs cannot be host associated at %L "
11897 "because it is blocked by an incompatible object "
11898 "of the same name declared at %L",
11899 sym->ts.u.derived->name, &sym->declared_at,
11900 &s->declared_at);
11901 return false;
11905 /* 4th constraint in section 11.3: "If an object of a type for which
11906 component-initialization is specified (R429) appears in the
11907 specification-part of a module and does not have the ALLOCATABLE
11908 or POINTER attribute, the object shall have the SAVE attribute."
11910 The check for initializers is performed with
11911 gfc_has_default_initializer because gfc_default_initializer generates
11912 a hidden default for allocatable components. */
11913 if (!(sym->value || no_init_flag) && sym->ns->proc_name
11914 && sym->ns->proc_name->attr.flavor == FL_MODULE
11915 && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
11916 && !sym->attr.pointer && !sym->attr.allocatable
11917 && gfc_has_default_initializer (sym->ts.u.derived)
11918 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
11919 "%qs at %L, needed due to the default "
11920 "initialization", sym->name, &sym->declared_at))
11921 return false;
11923 /* Assign default initializer. */
11924 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
11925 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
11926 sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
11928 return true;
11932 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
11933 except in the declaration of an entity or component that has the POINTER
11934 or ALLOCATABLE attribute. */
11936 static bool
11937 deferred_requirements (gfc_symbol *sym)
11939 if (sym->ts.deferred
11940 && !(sym->attr.pointer
11941 || sym->attr.allocatable
11942 || sym->attr.associate_var
11943 || sym->attr.omp_udr_artificial_var))
11945 gfc_error ("Entity %qs at %L has a deferred type parameter and "
11946 "requires either the POINTER or ALLOCATABLE attribute",
11947 sym->name, &sym->declared_at);
11948 return false;
11950 return true;
11954 /* Resolve symbols with flavor variable. */
11956 static bool
11957 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
11959 int no_init_flag, automatic_flag;
11960 gfc_expr *e;
11961 const char *auto_save_msg;
11962 bool saved_specification_expr;
11964 auto_save_msg = "Automatic object %qs at %L cannot have the "
11965 "SAVE attribute";
11967 if (!resolve_fl_var_and_proc (sym, mp_flag))
11968 return false;
11970 /* Set this flag to check that variables are parameters of all entries.
11971 This check is effected by the call to gfc_resolve_expr through
11972 is_non_constant_shape_array. */
11973 saved_specification_expr = specification_expr;
11974 specification_expr = true;
11976 if (sym->ns->proc_name
11977 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11978 || sym->ns->proc_name->attr.is_main_program)
11979 && !sym->attr.use_assoc
11980 && !sym->attr.allocatable
11981 && !sym->attr.pointer
11982 && is_non_constant_shape_array (sym))
11984 /* F08:C541. The shape of an array defined in a main program or module
11985 * needs to be constant. */
11986 gfc_error ("The module or main program array %qs at %L must "
11987 "have constant shape", sym->name, &sym->declared_at);
11988 specification_expr = saved_specification_expr;
11989 return false;
11992 /* Constraints on deferred type parameter. */
11993 if (!deferred_requirements (sym))
11994 return false;
11996 if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
11998 /* Make sure that character string variables with assumed length are
11999 dummy arguments. */
12000 e = sym->ts.u.cl->length;
12001 if (e == NULL && !sym->attr.dummy && !sym->attr.result
12002 && !sym->ts.deferred && !sym->attr.select_type_temporary
12003 && !sym->attr.omp_udr_artificial_var)
12005 gfc_error ("Entity with assumed character length at %L must be a "
12006 "dummy argument or a PARAMETER", &sym->declared_at);
12007 specification_expr = saved_specification_expr;
12008 return false;
12011 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
12013 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
12014 specification_expr = saved_specification_expr;
12015 return false;
12018 if (!gfc_is_constant_expr (e)
12019 && !(e->expr_type == EXPR_VARIABLE
12020 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
12022 if (!sym->attr.use_assoc && sym->ns->proc_name
12023 && (sym->ns->proc_name->attr.flavor == FL_MODULE
12024 || sym->ns->proc_name->attr.is_main_program))
12026 gfc_error ("%qs at %L must have constant character length "
12027 "in this context", sym->name, &sym->declared_at);
12028 specification_expr = saved_specification_expr;
12029 return false;
12031 if (sym->attr.in_common)
12033 gfc_error ("COMMON variable %qs at %L must have constant "
12034 "character length", sym->name, &sym->declared_at);
12035 specification_expr = saved_specification_expr;
12036 return false;
12041 if (sym->value == NULL && sym->attr.referenced)
12042 apply_default_init_local (sym); /* Try to apply a default initialization. */
12044 /* Determine if the symbol may not have an initializer. */
12045 no_init_flag = automatic_flag = 0;
12046 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
12047 || sym->attr.intrinsic || sym->attr.result)
12048 no_init_flag = 1;
12049 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
12050 && is_non_constant_shape_array (sym))
12052 no_init_flag = automatic_flag = 1;
12054 /* Also, they must not have the SAVE attribute.
12055 SAVE_IMPLICIT is checked below. */
12056 if (sym->as && sym->attr.codimension)
12058 int corank = sym->as->corank;
12059 sym->as->corank = 0;
12060 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
12061 sym->as->corank = corank;
12063 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
12065 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
12066 specification_expr = saved_specification_expr;
12067 return false;
12071 /* Ensure that any initializer is simplified. */
12072 if (sym->value)
12073 gfc_simplify_expr (sym->value, 1);
12075 /* Reject illegal initializers. */
12076 if (!sym->mark && sym->value)
12078 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
12079 && CLASS_DATA (sym)->attr.allocatable))
12080 gfc_error ("Allocatable %qs at %L cannot have an initializer",
12081 sym->name, &sym->declared_at);
12082 else if (sym->attr.external)
12083 gfc_error ("External %qs at %L cannot have an initializer",
12084 sym->name, &sym->declared_at);
12085 else if (sym->attr.dummy
12086 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
12087 gfc_error ("Dummy %qs at %L cannot have an initializer",
12088 sym->name, &sym->declared_at);
12089 else if (sym->attr.intrinsic)
12090 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
12091 sym->name, &sym->declared_at);
12092 else if (sym->attr.result)
12093 gfc_error ("Function result %qs at %L cannot have an initializer",
12094 sym->name, &sym->declared_at);
12095 else if (automatic_flag)
12096 gfc_error ("Automatic array %qs at %L cannot have an initializer",
12097 sym->name, &sym->declared_at);
12098 else
12099 goto no_init_error;
12100 specification_expr = saved_specification_expr;
12101 return false;
12104 no_init_error:
12105 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
12107 bool res = resolve_fl_variable_derived (sym, no_init_flag);
12108 specification_expr = saved_specification_expr;
12109 return res;
12112 specification_expr = saved_specification_expr;
12113 return true;
12117 /* Compare the dummy characteristics of a module procedure interface
12118 declaration with the corresponding declaration in a submodule. */
12119 static gfc_formal_arglist *new_formal;
12120 static char errmsg[200];
12122 static void
12123 compare_fsyms (gfc_symbol *sym)
12125 gfc_symbol *fsym;
12127 if (sym == NULL || new_formal == NULL)
12128 return;
12130 fsym = new_formal->sym;
12132 if (sym == fsym)
12133 return;
12135 if (strcmp (sym->name, fsym->name) == 0)
12137 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
12138 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
12143 /* Resolve a procedure. */
12145 static bool
12146 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
12148 gfc_formal_arglist *arg;
12150 if (sym->attr.function
12151 && !resolve_fl_var_and_proc (sym, mp_flag))
12152 return false;
12154 if (sym->ts.type == BT_CHARACTER)
12156 gfc_charlen *cl = sym->ts.u.cl;
12158 if (cl && cl->length && gfc_is_constant_expr (cl->length)
12159 && !resolve_charlen (cl))
12160 return false;
12162 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12163 && sym->attr.proc == PROC_ST_FUNCTION)
12165 gfc_error ("Character-valued statement function %qs at %L must "
12166 "have constant length", sym->name, &sym->declared_at);
12167 return false;
12171 /* Ensure that derived type for are not of a private type. Internal
12172 module procedures are excluded by 2.2.3.3 - i.e., they are not
12173 externally accessible and can access all the objects accessible in
12174 the host. */
12175 if (!(sym->ns->parent
12176 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
12177 && gfc_check_symbol_access (sym))
12179 gfc_interface *iface;
12181 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
12183 if (arg->sym
12184 && arg->sym->ts.type == BT_DERIVED
12185 && !arg->sym->ts.u.derived->attr.use_assoc
12186 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
12187 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
12188 "and cannot be a dummy argument"
12189 " of %qs, which is PUBLIC at %L",
12190 arg->sym->name, sym->name,
12191 &sym->declared_at))
12193 /* Stop this message from recurring. */
12194 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
12195 return false;
12199 /* PUBLIC interfaces may expose PRIVATE procedures that take types
12200 PRIVATE to the containing module. */
12201 for (iface = sym->generic; iface; iface = iface->next)
12203 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
12205 if (arg->sym
12206 && arg->sym->ts.type == BT_DERIVED
12207 && !arg->sym->ts.u.derived->attr.use_assoc
12208 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
12209 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
12210 "PUBLIC interface %qs at %L "
12211 "takes dummy arguments of %qs which "
12212 "is PRIVATE", iface->sym->name,
12213 sym->name, &iface->sym->declared_at,
12214 gfc_typename(&arg->sym->ts)))
12216 /* Stop this message from recurring. */
12217 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
12218 return false;
12224 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
12225 && !sym->attr.proc_pointer)
12227 gfc_error ("Function %qs at %L cannot have an initializer",
12228 sym->name, &sym->declared_at);
12229 return false;
12232 /* An external symbol may not have an initializer because it is taken to be
12233 a procedure. Exception: Procedure Pointers. */
12234 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
12236 gfc_error ("External object %qs at %L may not have an initializer",
12237 sym->name, &sym->declared_at);
12238 return false;
12241 /* An elemental function is required to return a scalar 12.7.1 */
12242 if (sym->attr.elemental && sym->attr.function && sym->as)
12244 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
12245 "result", sym->name, &sym->declared_at);
12246 /* Reset so that the error only occurs once. */
12247 sym->attr.elemental = 0;
12248 return false;
12251 if (sym->attr.proc == PROC_ST_FUNCTION
12252 && (sym->attr.allocatable || sym->attr.pointer))
12254 gfc_error ("Statement function %qs at %L may not have pointer or "
12255 "allocatable attribute", sym->name, &sym->declared_at);
12256 return false;
12259 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
12260 char-len-param shall not be array-valued, pointer-valued, recursive
12261 or pure. ....snip... A character value of * may only be used in the
12262 following ways: (i) Dummy arg of procedure - dummy associates with
12263 actual length; (ii) To declare a named constant; or (iii) External
12264 function - but length must be declared in calling scoping unit. */
12265 if (sym->attr.function
12266 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
12267 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
12269 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
12270 || (sym->attr.recursive) || (sym->attr.pure))
12272 if (sym->as && sym->as->rank)
12273 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
12274 "array-valued", sym->name, &sym->declared_at);
12276 if (sym->attr.pointer)
12277 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
12278 "pointer-valued", sym->name, &sym->declared_at);
12280 if (sym->attr.pure)
12281 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
12282 "pure", sym->name, &sym->declared_at);
12284 if (sym->attr.recursive)
12285 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
12286 "recursive", sym->name, &sym->declared_at);
12288 return false;
12291 /* Appendix B.2 of the standard. Contained functions give an
12292 error anyway. Deferred character length is an F2003 feature.
12293 Don't warn on intrinsic conversion functions, which start
12294 with two underscores. */
12295 if (!sym->attr.contained && !sym->ts.deferred
12296 && (sym->name[0] != '_' || sym->name[1] != '_'))
12297 gfc_notify_std (GFC_STD_F95_OBS,
12298 "CHARACTER(*) function %qs at %L",
12299 sym->name, &sym->declared_at);
12302 /* F2008, C1218. */
12303 if (sym->attr.elemental)
12305 if (sym->attr.proc_pointer)
12307 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
12308 sym->name, &sym->declared_at);
12309 return false;
12311 if (sym->attr.dummy)
12313 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
12314 sym->name, &sym->declared_at);
12315 return false;
12319 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
12321 gfc_formal_arglist *curr_arg;
12322 int has_non_interop_arg = 0;
12324 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
12325 sym->common_block))
12327 /* Clear these to prevent looking at them again if there was an
12328 error. */
12329 sym->attr.is_bind_c = 0;
12330 sym->attr.is_c_interop = 0;
12331 sym->ts.is_c_interop = 0;
12333 else
12335 /* So far, no errors have been found. */
12336 sym->attr.is_c_interop = 1;
12337 sym->ts.is_c_interop = 1;
12340 curr_arg = gfc_sym_get_dummy_args (sym);
12341 while (curr_arg != NULL)
12343 /* Skip implicitly typed dummy args here. */
12344 if (curr_arg->sym->attr.implicit_type == 0)
12345 if (!gfc_verify_c_interop_param (curr_arg->sym))
12346 /* If something is found to fail, record the fact so we
12347 can mark the symbol for the procedure as not being
12348 BIND(C) to try and prevent multiple errors being
12349 reported. */
12350 has_non_interop_arg = 1;
12352 curr_arg = curr_arg->next;
12355 /* See if any of the arguments were not interoperable and if so, clear
12356 the procedure symbol to prevent duplicate error messages. */
12357 if (has_non_interop_arg != 0)
12359 sym->attr.is_c_interop = 0;
12360 sym->ts.is_c_interop = 0;
12361 sym->attr.is_bind_c = 0;
12365 if (!sym->attr.proc_pointer)
12367 if (sym->attr.save == SAVE_EXPLICIT)
12369 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
12370 "in %qs at %L", sym->name, &sym->declared_at);
12371 return false;
12373 if (sym->attr.intent)
12375 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
12376 "in %qs at %L", sym->name, &sym->declared_at);
12377 return false;
12379 if (sym->attr.subroutine && sym->attr.result)
12381 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
12382 "in %qs at %L", sym->name, &sym->declared_at);
12383 return false;
12385 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
12386 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
12387 || sym->attr.contained))
12389 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
12390 "in %qs at %L", sym->name, &sym->declared_at);
12391 return false;
12393 if (strcmp ("ppr@", sym->name) == 0)
12395 gfc_error ("Procedure pointer result %qs at %L "
12396 "is missing the pointer attribute",
12397 sym->ns->proc_name->name, &sym->declared_at);
12398 return false;
12402 /* Assume that a procedure whose body is not known has references
12403 to external arrays. */
12404 if (sym->attr.if_source != IFSRC_DECL)
12405 sym->attr.array_outer_dependency = 1;
12407 /* Compare the characteristics of a module procedure with the
12408 interface declaration. Ideally this would be done with
12409 gfc_compare_interfaces but, at present, the formal interface
12410 cannot be copied to the ts.interface. */
12411 if (sym->attr.module_procedure
12412 && sym->attr.if_source == IFSRC_DECL)
12414 gfc_symbol *iface;
12415 char name[2*GFC_MAX_SYMBOL_LEN + 1];
12416 char *module_name;
12417 char *submodule_name;
12418 strcpy (name, sym->ns->proc_name->name);
12419 module_name = strtok (name, ".");
12420 submodule_name = strtok (NULL, ".");
12422 iface = sym->tlink;
12423 sym->tlink = NULL;
12425 /* Make sure that the result uses the correct charlen for deferred
12426 length results. */
12427 if (iface && sym->result
12428 && iface->ts.type == BT_CHARACTER
12429 && iface->ts.deferred)
12430 sym->result->ts.u.cl = iface->ts.u.cl;
12432 if (iface == NULL)
12433 goto check_formal;
12435 /* Check the procedure characteristics. */
12436 if (sym->attr.elemental != iface->attr.elemental)
12438 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
12439 "PROCEDURE at %L and its interface in %s",
12440 &sym->declared_at, module_name);
12441 return false;
12444 if (sym->attr.pure != iface->attr.pure)
12446 gfc_error ("Mismatch in PURE attribute between MODULE "
12447 "PROCEDURE at %L and its interface in %s",
12448 &sym->declared_at, module_name);
12449 return false;
12452 if (sym->attr.recursive != iface->attr.recursive)
12454 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
12455 "PROCEDURE at %L and its interface in %s",
12456 &sym->declared_at, module_name);
12457 return false;
12460 /* Check the result characteristics. */
12461 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
12463 gfc_error ("%s between the MODULE PROCEDURE declaration "
12464 "in MODULE %qs and the declaration at %L in "
12465 "(SUB)MODULE %qs",
12466 errmsg, module_name, &sym->declared_at,
12467 submodule_name ? submodule_name : module_name);
12468 return false;
12471 check_formal:
12472 /* Check the characteristics of the formal arguments. */
12473 if (sym->formal && sym->formal_ns)
12475 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
12477 new_formal = arg;
12478 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
12482 return true;
12486 /* Resolve a list of finalizer procedures. That is, after they have hopefully
12487 been defined and we now know their defined arguments, check that they fulfill
12488 the requirements of the standard for procedures used as finalizers. */
12490 static bool
12491 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
12493 gfc_finalizer* list;
12494 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
12495 bool result = true;
12496 bool seen_scalar = false;
12497 gfc_symbol *vtab;
12498 gfc_component *c;
12499 gfc_symbol *parent = gfc_get_derived_super_type (derived);
12501 if (parent)
12502 gfc_resolve_finalizers (parent, finalizable);
12504 /* Ensure that derived-type components have a their finalizers resolved. */
12505 bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
12506 for (c = derived->components; c; c = c->next)
12507 if (c->ts.type == BT_DERIVED
12508 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
12510 bool has_final2 = false;
12511 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
12512 return false; /* Error. */
12513 has_final = has_final || has_final2;
12515 /* Return early if not finalizable. */
12516 if (!has_final)
12518 if (finalizable)
12519 *finalizable = false;
12520 return true;
12523 /* Walk over the list of finalizer-procedures, check them, and if any one
12524 does not fit in with the standard's definition, print an error and remove
12525 it from the list. */
12526 prev_link = &derived->f2k_derived->finalizers;
12527 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
12529 gfc_formal_arglist *dummy_args;
12530 gfc_symbol* arg;
12531 gfc_finalizer* i;
12532 int my_rank;
12534 /* Skip this finalizer if we already resolved it. */
12535 if (list->proc_tree)
12537 if (list->proc_tree->n.sym->formal->sym->as == NULL
12538 || list->proc_tree->n.sym->formal->sym->as->rank == 0)
12539 seen_scalar = true;
12540 prev_link = &(list->next);
12541 continue;
12544 /* Check this exists and is a SUBROUTINE. */
12545 if (!list->proc_sym->attr.subroutine)
12547 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
12548 list->proc_sym->name, &list->where);
12549 goto error;
12552 /* We should have exactly one argument. */
12553 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
12554 if (!dummy_args || dummy_args->next)
12556 gfc_error ("FINAL procedure at %L must have exactly one argument",
12557 &list->where);
12558 goto error;
12560 arg = dummy_args->sym;
12562 /* This argument must be of our type. */
12563 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
12565 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
12566 &arg->declared_at, derived->name);
12567 goto error;
12570 /* It must neither be a pointer nor allocatable nor optional. */
12571 if (arg->attr.pointer)
12573 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
12574 &arg->declared_at);
12575 goto error;
12577 if (arg->attr.allocatable)
12579 gfc_error ("Argument of FINAL procedure at %L must not be"
12580 " ALLOCATABLE", &arg->declared_at);
12581 goto error;
12583 if (arg->attr.optional)
12585 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
12586 &arg->declared_at);
12587 goto error;
12590 /* It must not be INTENT(OUT). */
12591 if (arg->attr.intent == INTENT_OUT)
12593 gfc_error ("Argument of FINAL procedure at %L must not be"
12594 " INTENT(OUT)", &arg->declared_at);
12595 goto error;
12598 /* Warn if the procedure is non-scalar and not assumed shape. */
12599 if (warn_surprising && arg->as && arg->as->rank != 0
12600 && arg->as->type != AS_ASSUMED_SHAPE)
12601 gfc_warning (OPT_Wsurprising,
12602 "Non-scalar FINAL procedure at %L should have assumed"
12603 " shape argument", &arg->declared_at);
12605 /* Check that it does not match in kind and rank with a FINAL procedure
12606 defined earlier. To really loop over the *earlier* declarations,
12607 we need to walk the tail of the list as new ones were pushed at the
12608 front. */
12609 /* TODO: Handle kind parameters once they are implemented. */
12610 my_rank = (arg->as ? arg->as->rank : 0);
12611 for (i = list->next; i; i = i->next)
12613 gfc_formal_arglist *dummy_args;
12615 /* Argument list might be empty; that is an error signalled earlier,
12616 but we nevertheless continued resolving. */
12617 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
12618 if (dummy_args)
12620 gfc_symbol* i_arg = dummy_args->sym;
12621 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
12622 if (i_rank == my_rank)
12624 gfc_error ("FINAL procedure %qs declared at %L has the same"
12625 " rank (%d) as %qs",
12626 list->proc_sym->name, &list->where, my_rank,
12627 i->proc_sym->name);
12628 goto error;
12633 /* Is this the/a scalar finalizer procedure? */
12634 if (my_rank == 0)
12635 seen_scalar = true;
12637 /* Find the symtree for this procedure. */
12638 gcc_assert (!list->proc_tree);
12639 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
12641 prev_link = &list->next;
12642 continue;
12644 /* Remove wrong nodes immediately from the list so we don't risk any
12645 troubles in the future when they might fail later expectations. */
12646 error:
12647 i = list;
12648 *prev_link = list->next;
12649 gfc_free_finalizer (i);
12650 result = false;
12653 if (result == false)
12654 return false;
12656 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
12657 were nodes in the list, must have been for arrays. It is surely a good
12658 idea to have a scalar version there if there's something to finalize. */
12659 if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
12660 gfc_warning (OPT_Wsurprising,
12661 "Only array FINAL procedures declared for derived type %qs"
12662 " defined at %L, suggest also scalar one",
12663 derived->name, &derived->declared_at);
12665 vtab = gfc_find_derived_vtab (derived);
12666 c = vtab->ts.u.derived->components->next->next->next->next->next;
12667 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
12669 if (finalizable)
12670 *finalizable = true;
12672 return true;
12676 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
12678 static bool
12679 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
12680 const char* generic_name, locus where)
12682 gfc_symbol *sym1, *sym2;
12683 const char *pass1, *pass2;
12684 gfc_formal_arglist *dummy_args;
12686 gcc_assert (t1->specific && t2->specific);
12687 gcc_assert (!t1->specific->is_generic);
12688 gcc_assert (!t2->specific->is_generic);
12689 gcc_assert (t1->is_operator == t2->is_operator);
12691 sym1 = t1->specific->u.specific->n.sym;
12692 sym2 = t2->specific->u.specific->n.sym;
12694 if (sym1 == sym2)
12695 return true;
12697 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
12698 if (sym1->attr.subroutine != sym2->attr.subroutine
12699 || sym1->attr.function != sym2->attr.function)
12701 gfc_error ("%qs and %qs can't be mixed FUNCTION/SUBROUTINE for"
12702 " GENERIC %qs at %L",
12703 sym1->name, sym2->name, generic_name, &where);
12704 return false;
12707 /* Determine PASS arguments. */
12708 if (t1->specific->nopass)
12709 pass1 = NULL;
12710 else if (t1->specific->pass_arg)
12711 pass1 = t1->specific->pass_arg;
12712 else
12714 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
12715 if (dummy_args)
12716 pass1 = dummy_args->sym->name;
12717 else
12718 pass1 = NULL;
12720 if (t2->specific->nopass)
12721 pass2 = NULL;
12722 else if (t2->specific->pass_arg)
12723 pass2 = t2->specific->pass_arg;
12724 else
12726 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
12727 if (dummy_args)
12728 pass2 = dummy_args->sym->name;
12729 else
12730 pass2 = NULL;
12733 /* Compare the interfaces. */
12734 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
12735 NULL, 0, pass1, pass2))
12737 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
12738 sym1->name, sym2->name, generic_name, &where);
12739 return false;
12742 return true;
12746 /* Worker function for resolving a generic procedure binding; this is used to
12747 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
12749 The difference between those cases is finding possible inherited bindings
12750 that are overridden, as one has to look for them in tb_sym_root,
12751 tb_uop_root or tb_op, respectively. Thus the caller must already find
12752 the super-type and set p->overridden correctly. */
12754 static bool
12755 resolve_tb_generic_targets (gfc_symbol* super_type,
12756 gfc_typebound_proc* p, const char* name)
12758 gfc_tbp_generic* target;
12759 gfc_symtree* first_target;
12760 gfc_symtree* inherited;
12762 gcc_assert (p && p->is_generic);
12764 /* Try to find the specific bindings for the symtrees in our target-list. */
12765 gcc_assert (p->u.generic);
12766 for (target = p->u.generic; target; target = target->next)
12767 if (!target->specific)
12769 gfc_typebound_proc* overridden_tbp;
12770 gfc_tbp_generic* g;
12771 const char* target_name;
12773 target_name = target->specific_st->name;
12775 /* Defined for this type directly. */
12776 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
12778 target->specific = target->specific_st->n.tb;
12779 goto specific_found;
12782 /* Look for an inherited specific binding. */
12783 if (super_type)
12785 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
12786 true, NULL);
12788 if (inherited)
12790 gcc_assert (inherited->n.tb);
12791 target->specific = inherited->n.tb;
12792 goto specific_found;
12796 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
12797 " at %L", target_name, name, &p->where);
12798 return false;
12800 /* Once we've found the specific binding, check it is not ambiguous with
12801 other specifics already found or inherited for the same GENERIC. */
12802 specific_found:
12803 gcc_assert (target->specific);
12805 /* This must really be a specific binding! */
12806 if (target->specific->is_generic)
12808 gfc_error ("GENERIC %qs at %L must target a specific binding,"
12809 " %qs is GENERIC, too", name, &p->where, target_name);
12810 return false;
12813 /* Check those already resolved on this type directly. */
12814 for (g = p->u.generic; g; g = g->next)
12815 if (g != target && g->specific
12816 && !check_generic_tbp_ambiguity (target, g, name, p->where))
12817 return false;
12819 /* Check for ambiguity with inherited specific targets. */
12820 for (overridden_tbp = p->overridden; overridden_tbp;
12821 overridden_tbp = overridden_tbp->overridden)
12822 if (overridden_tbp->is_generic)
12824 for (g = overridden_tbp->u.generic; g; g = g->next)
12826 gcc_assert (g->specific);
12827 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
12828 return false;
12833 /* If we attempt to "overwrite" a specific binding, this is an error. */
12834 if (p->overridden && !p->overridden->is_generic)
12836 gfc_error ("GENERIC %qs at %L can't overwrite specific binding with"
12837 " the same name", name, &p->where);
12838 return false;
12841 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
12842 all must have the same attributes here. */
12843 first_target = p->u.generic->specific->u.specific;
12844 gcc_assert (first_target);
12845 p->subroutine = first_target->n.sym->attr.subroutine;
12846 p->function = first_target->n.sym->attr.function;
12848 return true;
12852 /* Resolve a GENERIC procedure binding for a derived type. */
12854 static bool
12855 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
12857 gfc_symbol* super_type;
12859 /* Find the overridden binding if any. */
12860 st->n.tb->overridden = NULL;
12861 super_type = gfc_get_derived_super_type (derived);
12862 if (super_type)
12864 gfc_symtree* overridden;
12865 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
12866 true, NULL);
12868 if (overridden && overridden->n.tb)
12869 st->n.tb->overridden = overridden->n.tb;
12872 /* Resolve using worker function. */
12873 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
12877 /* Retrieve the target-procedure of an operator binding and do some checks in
12878 common for intrinsic and user-defined type-bound operators. */
12880 static gfc_symbol*
12881 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
12883 gfc_symbol* target_proc;
12885 gcc_assert (target->specific && !target->specific->is_generic);
12886 target_proc = target->specific->u.specific->n.sym;
12887 gcc_assert (target_proc);
12889 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
12890 if (target->specific->nopass)
12892 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
12893 return NULL;
12896 return target_proc;
12900 /* Resolve a type-bound intrinsic operator. */
12902 static bool
12903 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
12904 gfc_typebound_proc* p)
12906 gfc_symbol* super_type;
12907 gfc_tbp_generic* target;
12909 /* If there's already an error here, do nothing (but don't fail again). */
12910 if (p->error)
12911 return true;
12913 /* Operators should always be GENERIC bindings. */
12914 gcc_assert (p->is_generic);
12916 /* Look for an overridden binding. */
12917 super_type = gfc_get_derived_super_type (derived);
12918 if (super_type && super_type->f2k_derived)
12919 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
12920 op, true, NULL);
12921 else
12922 p->overridden = NULL;
12924 /* Resolve general GENERIC properties using worker function. */
12925 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
12926 goto error;
12928 /* Check the targets to be procedures of correct interface. */
12929 for (target = p->u.generic; target; target = target->next)
12931 gfc_symbol* target_proc;
12933 target_proc = get_checked_tb_operator_target (target, p->where);
12934 if (!target_proc)
12935 goto error;
12937 if (!gfc_check_operator_interface (target_proc, op, p->where))
12938 goto error;
12940 /* Add target to non-typebound operator list. */
12941 if (!target->specific->deferred && !derived->attr.use_assoc
12942 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
12944 gfc_interface *head, *intr;
12946 /* Preempt 'gfc_check_new_interface' for submodules, where the
12947 mechanism for handling module procedures winds up resolving
12948 operator interfaces twice and would otherwise cause an error. */
12949 for (intr = derived->ns->op[op]; intr; intr = intr->next)
12950 if (intr->sym == target_proc
12951 && target_proc->attr.used_in_submodule)
12952 return true;
12954 if (!gfc_check_new_interface (derived->ns->op[op],
12955 target_proc, p->where))
12956 return false;
12957 head = derived->ns->op[op];
12958 intr = gfc_get_interface ();
12959 intr->sym = target_proc;
12960 intr->where = p->where;
12961 intr->next = head;
12962 derived->ns->op[op] = intr;
12966 return true;
12968 error:
12969 p->error = 1;
12970 return false;
12974 /* Resolve a type-bound user operator (tree-walker callback). */
12976 static gfc_symbol* resolve_bindings_derived;
12977 static bool resolve_bindings_result;
12979 static bool check_uop_procedure (gfc_symbol* sym, locus where);
12981 static void
12982 resolve_typebound_user_op (gfc_symtree* stree)
12984 gfc_symbol* super_type;
12985 gfc_tbp_generic* target;
12987 gcc_assert (stree && stree->n.tb);
12989 if (stree->n.tb->error)
12990 return;
12992 /* Operators should always be GENERIC bindings. */
12993 gcc_assert (stree->n.tb->is_generic);
12995 /* Find overridden procedure, if any. */
12996 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12997 if (super_type && super_type->f2k_derived)
12999 gfc_symtree* overridden;
13000 overridden = gfc_find_typebound_user_op (super_type, NULL,
13001 stree->name, true, NULL);
13003 if (overridden && overridden->n.tb)
13004 stree->n.tb->overridden = overridden->n.tb;
13006 else
13007 stree->n.tb->overridden = NULL;
13009 /* Resolve basically using worker function. */
13010 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
13011 goto error;
13013 /* Check the targets to be functions of correct interface. */
13014 for (target = stree->n.tb->u.generic; target; target = target->next)
13016 gfc_symbol* target_proc;
13018 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
13019 if (!target_proc)
13020 goto error;
13022 if (!check_uop_procedure (target_proc, stree->n.tb->where))
13023 goto error;
13026 return;
13028 error:
13029 resolve_bindings_result = false;
13030 stree->n.tb->error = 1;
13034 /* Resolve the type-bound procedures for a derived type. */
13036 static void
13037 resolve_typebound_procedure (gfc_symtree* stree)
13039 gfc_symbol* proc;
13040 locus where;
13041 gfc_symbol* me_arg;
13042 gfc_symbol* super_type;
13043 gfc_component* comp;
13045 gcc_assert (stree);
13047 /* Undefined specific symbol from GENERIC target definition. */
13048 if (!stree->n.tb)
13049 return;
13051 if (stree->n.tb->error)
13052 return;
13054 /* If this is a GENERIC binding, use that routine. */
13055 if (stree->n.tb->is_generic)
13057 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
13058 goto error;
13059 return;
13062 /* Get the target-procedure to check it. */
13063 gcc_assert (!stree->n.tb->is_generic);
13064 gcc_assert (stree->n.tb->u.specific);
13065 proc = stree->n.tb->u.specific->n.sym;
13066 where = stree->n.tb->where;
13068 /* Default access should already be resolved from the parser. */
13069 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
13071 if (stree->n.tb->deferred)
13073 if (!check_proc_interface (proc, &where))
13074 goto error;
13076 else
13078 /* Check for F08:C465. */
13079 if ((!proc->attr.subroutine && !proc->attr.function)
13080 || (proc->attr.proc != PROC_MODULE
13081 && proc->attr.if_source != IFSRC_IFBODY)
13082 || proc->attr.abstract)
13084 gfc_error ("%qs must be a module procedure or an external procedure with"
13085 " an explicit interface at %L", proc->name, &where);
13086 goto error;
13090 stree->n.tb->subroutine = proc->attr.subroutine;
13091 stree->n.tb->function = proc->attr.function;
13093 /* Find the super-type of the current derived type. We could do this once and
13094 store in a global if speed is needed, but as long as not I believe this is
13095 more readable and clearer. */
13096 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
13098 /* If PASS, resolve and check arguments if not already resolved / loaded
13099 from a .mod file. */
13100 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
13102 gfc_formal_arglist *dummy_args;
13104 dummy_args = gfc_sym_get_dummy_args (proc);
13105 if (stree->n.tb->pass_arg)
13107 gfc_formal_arglist *i;
13109 /* If an explicit passing argument name is given, walk the arg-list
13110 and look for it. */
13112 me_arg = NULL;
13113 stree->n.tb->pass_arg_num = 1;
13114 for (i = dummy_args; i; i = i->next)
13116 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
13118 me_arg = i->sym;
13119 break;
13121 ++stree->n.tb->pass_arg_num;
13124 if (!me_arg)
13126 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
13127 " argument %qs",
13128 proc->name, stree->n.tb->pass_arg, &where,
13129 stree->n.tb->pass_arg);
13130 goto error;
13133 else
13135 /* Otherwise, take the first one; there should in fact be at least
13136 one. */
13137 stree->n.tb->pass_arg_num = 1;
13138 if (!dummy_args)
13140 gfc_error ("Procedure %qs with PASS at %L must have at"
13141 " least one argument", proc->name, &where);
13142 goto error;
13144 me_arg = dummy_args->sym;
13147 /* Now check that the argument-type matches and the passed-object
13148 dummy argument is generally fine. */
13150 gcc_assert (me_arg);
13152 if (me_arg->ts.type != BT_CLASS)
13154 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
13155 " at %L", proc->name, &where);
13156 goto error;
13159 if (CLASS_DATA (me_arg)->ts.u.derived
13160 != resolve_bindings_derived)
13162 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
13163 " the derived-type %qs", me_arg->name, proc->name,
13164 me_arg->name, &where, resolve_bindings_derived->name);
13165 goto error;
13168 gcc_assert (me_arg->ts.type == BT_CLASS);
13169 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
13171 gfc_error ("Passed-object dummy argument of %qs at %L must be"
13172 " scalar", proc->name, &where);
13173 goto error;
13175 if (CLASS_DATA (me_arg)->attr.allocatable)
13177 gfc_error ("Passed-object dummy argument of %qs at %L must not"
13178 " be ALLOCATABLE", proc->name, &where);
13179 goto error;
13181 if (CLASS_DATA (me_arg)->attr.class_pointer)
13183 gfc_error ("Passed-object dummy argument of %qs at %L must not"
13184 " be POINTER", proc->name, &where);
13185 goto error;
13189 /* If we are extending some type, check that we don't override a procedure
13190 flagged NON_OVERRIDABLE. */
13191 stree->n.tb->overridden = NULL;
13192 if (super_type)
13194 gfc_symtree* overridden;
13195 overridden = gfc_find_typebound_proc (super_type, NULL,
13196 stree->name, true, NULL);
13198 if (overridden)
13200 if (overridden->n.tb)
13201 stree->n.tb->overridden = overridden->n.tb;
13203 if (!gfc_check_typebound_override (stree, overridden))
13204 goto error;
13208 /* See if there's a name collision with a component directly in this type. */
13209 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
13210 if (!strcmp (comp->name, stree->name))
13212 gfc_error ("Procedure %qs at %L has the same name as a component of"
13213 " %qs",
13214 stree->name, &where, resolve_bindings_derived->name);
13215 goto error;
13218 /* Try to find a name collision with an inherited component. */
13219 if (super_type && gfc_find_component (super_type, stree->name, true, true,
13220 NULL))
13222 gfc_error ("Procedure %qs at %L has the same name as an inherited"
13223 " component of %qs",
13224 stree->name, &where, resolve_bindings_derived->name);
13225 goto error;
13228 stree->n.tb->error = 0;
13229 return;
13231 error:
13232 resolve_bindings_result = false;
13233 stree->n.tb->error = 1;
13237 static bool
13238 resolve_typebound_procedures (gfc_symbol* derived)
13240 int op;
13241 gfc_symbol* super_type;
13243 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
13244 return true;
13246 super_type = gfc_get_derived_super_type (derived);
13247 if (super_type)
13248 resolve_symbol (super_type);
13250 resolve_bindings_derived = derived;
13251 resolve_bindings_result = true;
13253 if (derived->f2k_derived->tb_sym_root)
13254 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
13255 &resolve_typebound_procedure);
13257 if (derived->f2k_derived->tb_uop_root)
13258 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
13259 &resolve_typebound_user_op);
13261 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
13263 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
13264 if (p && !resolve_typebound_intrinsic_op (derived,
13265 (gfc_intrinsic_op)op, p))
13266 resolve_bindings_result = false;
13269 return resolve_bindings_result;
13273 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
13274 to give all identical derived types the same backend_decl. */
13275 static void
13276 add_dt_to_dt_list (gfc_symbol *derived)
13278 gfc_dt_list *dt_list;
13280 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
13281 if (derived == dt_list->derived)
13282 return;
13284 dt_list = gfc_get_dt_list ();
13285 dt_list->next = gfc_derived_types;
13286 dt_list->derived = derived;
13287 gfc_derived_types = dt_list;
13291 /* Ensure that a derived-type is really not abstract, meaning that every
13292 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
13294 static bool
13295 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
13297 if (!st)
13298 return true;
13300 if (!ensure_not_abstract_walker (sub, st->left))
13301 return false;
13302 if (!ensure_not_abstract_walker (sub, st->right))
13303 return false;
13305 if (st->n.tb && st->n.tb->deferred)
13307 gfc_symtree* overriding;
13308 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
13309 if (!overriding)
13310 return false;
13311 gcc_assert (overriding->n.tb);
13312 if (overriding->n.tb->deferred)
13314 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
13315 " %qs is DEFERRED and not overridden",
13316 sub->name, &sub->declared_at, st->name);
13317 return false;
13321 return true;
13324 static bool
13325 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
13327 /* The algorithm used here is to recursively travel up the ancestry of sub
13328 and for each ancestor-type, check all bindings. If any of them is
13329 DEFERRED, look it up starting from sub and see if the found (overriding)
13330 binding is not DEFERRED.
13331 This is not the most efficient way to do this, but it should be ok and is
13332 clearer than something sophisticated. */
13334 gcc_assert (ancestor && !sub->attr.abstract);
13336 if (!ancestor->attr.abstract)
13337 return true;
13339 /* Walk bindings of this ancestor. */
13340 if (ancestor->f2k_derived)
13342 bool t;
13343 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
13344 if (!t)
13345 return false;
13348 /* Find next ancestor type and recurse on it. */
13349 ancestor = gfc_get_derived_super_type (ancestor);
13350 if (ancestor)
13351 return ensure_not_abstract (sub, ancestor);
13353 return true;
13357 /* This check for typebound defined assignments is done recursively
13358 since the order in which derived types are resolved is not always in
13359 order of the declarations. */
13361 static void
13362 check_defined_assignments (gfc_symbol *derived)
13364 gfc_component *c;
13366 for (c = derived->components; c; c = c->next)
13368 if (!gfc_bt_struct (c->ts.type)
13369 || c->attr.pointer
13370 || c->attr.allocatable
13371 || c->attr.proc_pointer_comp
13372 || c->attr.class_pointer
13373 || c->attr.proc_pointer)
13374 continue;
13376 if (c->ts.u.derived->attr.defined_assign_comp
13377 || (c->ts.u.derived->f2k_derived
13378 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
13380 derived->attr.defined_assign_comp = 1;
13381 return;
13384 check_defined_assignments (c->ts.u.derived);
13385 if (c->ts.u.derived->attr.defined_assign_comp)
13387 derived->attr.defined_assign_comp = 1;
13388 return;
13394 /* Resolve a single component of a derived type or structure. */
13396 static bool
13397 resolve_component (gfc_component *c, gfc_symbol *sym)
13399 gfc_symbol *super_type;
13401 if (c->attr.artificial)
13402 return true;
13404 /* F2008, C442. */
13405 if ((!sym->attr.is_class || c != sym->components)
13406 && c->attr.codimension
13407 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
13409 gfc_error ("Coarray component %qs at %L must be allocatable with "
13410 "deferred shape", c->name, &c->loc);
13411 return false;
13414 /* F2008, C443. */
13415 if (c->attr.codimension && c->ts.type == BT_DERIVED
13416 && c->ts.u.derived->ts.is_iso_c)
13418 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
13419 "shall not be a coarray", c->name, &c->loc);
13420 return false;
13423 /* F2008, C444. */
13424 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
13425 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
13426 || c->attr.allocatable))
13428 gfc_error ("Component %qs at %L with coarray component "
13429 "shall be a nonpointer, nonallocatable scalar",
13430 c->name, &c->loc);
13431 return false;
13434 /* F2008, C448. */
13435 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
13437 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
13438 "is not an array pointer", c->name, &c->loc);
13439 return false;
13442 if (c->attr.proc_pointer && c->ts.interface)
13444 gfc_symbol *ifc = c->ts.interface;
13446 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
13448 c->tb->error = 1;
13449 return false;
13452 if (ifc->attr.if_source || ifc->attr.intrinsic)
13454 /* Resolve interface and copy attributes. */
13455 if (ifc->formal && !ifc->formal_ns)
13456 resolve_symbol (ifc);
13457 if (ifc->attr.intrinsic)
13458 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
13460 if (ifc->result)
13462 c->ts = ifc->result->ts;
13463 c->attr.allocatable = ifc->result->attr.allocatable;
13464 c->attr.pointer = ifc->result->attr.pointer;
13465 c->attr.dimension = ifc->result->attr.dimension;
13466 c->as = gfc_copy_array_spec (ifc->result->as);
13467 c->attr.class_ok = ifc->result->attr.class_ok;
13469 else
13471 c->ts = ifc->ts;
13472 c->attr.allocatable = ifc->attr.allocatable;
13473 c->attr.pointer = ifc->attr.pointer;
13474 c->attr.dimension = ifc->attr.dimension;
13475 c->as = gfc_copy_array_spec (ifc->as);
13476 c->attr.class_ok = ifc->attr.class_ok;
13478 c->ts.interface = ifc;
13479 c->attr.function = ifc->attr.function;
13480 c->attr.subroutine = ifc->attr.subroutine;
13482 c->attr.pure = ifc->attr.pure;
13483 c->attr.elemental = ifc->attr.elemental;
13484 c->attr.recursive = ifc->attr.recursive;
13485 c->attr.always_explicit = ifc->attr.always_explicit;
13486 c->attr.ext_attr |= ifc->attr.ext_attr;
13487 /* Copy char length. */
13488 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
13490 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
13491 if (cl->length && !cl->resolved
13492 && !gfc_resolve_expr (cl->length))
13494 c->tb->error = 1;
13495 return false;
13497 c->ts.u.cl = cl;
13501 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
13503 /* Since PPCs are not implicitly typed, a PPC without an explicit
13504 interface must be a subroutine. */
13505 gfc_add_subroutine (&c->attr, c->name, &c->loc);
13508 /* Procedure pointer components: Check PASS arg. */
13509 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
13510 && !sym->attr.vtype)
13512 gfc_symbol* me_arg;
13514 if (c->tb->pass_arg)
13516 gfc_formal_arglist* i;
13518 /* If an explicit passing argument name is given, walk the arg-list
13519 and look for it. */
13521 me_arg = NULL;
13522 c->tb->pass_arg_num = 1;
13523 for (i = c->ts.interface->formal; i; i = i->next)
13525 if (!strcmp (i->sym->name, c->tb->pass_arg))
13527 me_arg = i->sym;
13528 break;
13530 c->tb->pass_arg_num++;
13533 if (!me_arg)
13535 gfc_error ("Procedure pointer component %qs with PASS(%s) "
13536 "at %L has no argument %qs", c->name,
13537 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
13538 c->tb->error = 1;
13539 return false;
13542 else
13544 /* Otherwise, take the first one; there should in fact be at least
13545 one. */
13546 c->tb->pass_arg_num = 1;
13547 if (!c->ts.interface->formal)
13549 gfc_error ("Procedure pointer component %qs with PASS at %L "
13550 "must have at least one argument",
13551 c->name, &c->loc);
13552 c->tb->error = 1;
13553 return false;
13555 me_arg = c->ts.interface->formal->sym;
13558 /* Now check that the argument-type matches. */
13559 gcc_assert (me_arg);
13560 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
13561 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
13562 || (me_arg->ts.type == BT_CLASS
13563 && CLASS_DATA (me_arg)->ts.u.derived != sym))
13565 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
13566 " the derived type %qs", me_arg->name, c->name,
13567 me_arg->name, &c->loc, sym->name);
13568 c->tb->error = 1;
13569 return false;
13572 /* Check for C453. */
13573 if (me_arg->attr.dimension)
13575 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13576 "must be scalar", me_arg->name, c->name, me_arg->name,
13577 &c->loc);
13578 c->tb->error = 1;
13579 return false;
13582 if (me_arg->attr.pointer)
13584 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13585 "may not have the POINTER attribute", me_arg->name,
13586 c->name, me_arg->name, &c->loc);
13587 c->tb->error = 1;
13588 return false;
13591 if (me_arg->attr.allocatable)
13593 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13594 "may not be ALLOCATABLE", me_arg->name, c->name,
13595 me_arg->name, &c->loc);
13596 c->tb->error = 1;
13597 return false;
13600 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
13602 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
13603 " at %L", c->name, &c->loc);
13604 return false;
13609 /* Check type-spec if this is not the parent-type component. */
13610 if (((sym->attr.is_class
13611 && (!sym->components->ts.u.derived->attr.extension
13612 || c != sym->components->ts.u.derived->components))
13613 || (!sym->attr.is_class
13614 && (!sym->attr.extension || c != sym->components)))
13615 && !sym->attr.vtype
13616 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
13617 return false;
13619 super_type = gfc_get_derived_super_type (sym);
13621 /* If this type is an extension, set the accessibility of the parent
13622 component. */
13623 if (super_type
13624 && ((sym->attr.is_class
13625 && c == sym->components->ts.u.derived->components)
13626 || (!sym->attr.is_class && c == sym->components))
13627 && strcmp (super_type->name, c->name) == 0)
13628 c->attr.access = super_type->attr.access;
13630 /* If this type is an extension, see if this component has the same name
13631 as an inherited type-bound procedure. */
13632 if (super_type && !sym->attr.is_class
13633 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
13635 gfc_error ("Component %qs of %qs at %L has the same name as an"
13636 " inherited type-bound procedure",
13637 c->name, sym->name, &c->loc);
13638 return false;
13641 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
13642 && !c->ts.deferred)
13644 if (c->ts.u.cl->length == NULL
13645 || (!resolve_charlen(c->ts.u.cl))
13646 || !gfc_is_constant_expr (c->ts.u.cl->length))
13648 gfc_error ("Character length of component %qs needs to "
13649 "be a constant specification expression at %L",
13650 c->name,
13651 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
13652 return false;
13656 if (c->ts.type == BT_CHARACTER && c->ts.deferred
13657 && !c->attr.pointer && !c->attr.allocatable)
13659 gfc_error ("Character component %qs of %qs at %L with deferred "
13660 "length must be a POINTER or ALLOCATABLE",
13661 c->name, sym->name, &c->loc);
13662 return false;
13665 /* Add the hidden deferred length field. */
13666 if (c->ts.type == BT_CHARACTER
13667 && (c->ts.deferred || c->attr.pdt_string)
13668 && !c->attr.function
13669 && !sym->attr.is_class)
13671 char name[GFC_MAX_SYMBOL_LEN+9];
13672 gfc_component *strlen;
13673 sprintf (name, "_%s_length", c->name);
13674 strlen = gfc_find_component (sym, name, true, true, NULL);
13675 if (strlen == NULL)
13677 if (!gfc_add_component (sym, name, &strlen))
13678 return false;
13679 strlen->ts.type = BT_INTEGER;
13680 strlen->ts.kind = gfc_charlen_int_kind;
13681 strlen->attr.access = ACCESS_PRIVATE;
13682 strlen->attr.artificial = 1;
13686 if (c->ts.type == BT_DERIVED
13687 && sym->component_access != ACCESS_PRIVATE
13688 && gfc_check_symbol_access (sym)
13689 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
13690 && !c->ts.u.derived->attr.use_assoc
13691 && !gfc_check_symbol_access (c->ts.u.derived)
13692 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
13693 "PRIVATE type and cannot be a component of "
13694 "%qs, which is PUBLIC at %L", c->name,
13695 sym->name, &sym->declared_at))
13696 return false;
13698 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
13700 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
13701 "type %s", c->name, &c->loc, sym->name);
13702 return false;
13705 if (sym->attr.sequence)
13707 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
13709 gfc_error ("Component %s of SEQUENCE type declared at %L does "
13710 "not have the SEQUENCE attribute",
13711 c->ts.u.derived->name, &sym->declared_at);
13712 return false;
13716 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
13717 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
13718 else if (c->ts.type == BT_CLASS && c->attr.class_ok
13719 && CLASS_DATA (c)->ts.u.derived->attr.generic)
13720 CLASS_DATA (c)->ts.u.derived
13721 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
13723 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
13724 && c->attr.pointer && c->ts.u.derived->components == NULL
13725 && !c->ts.u.derived->attr.zero_comp)
13727 gfc_error ("The pointer component %qs of %qs at %L is a type "
13728 "that has not been declared", c->name, sym->name,
13729 &c->loc);
13730 return false;
13733 if (c->ts.type == BT_CLASS && c->attr.class_ok
13734 && CLASS_DATA (c)->attr.class_pointer
13735 && CLASS_DATA (c)->ts.u.derived->components == NULL
13736 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
13737 && !UNLIMITED_POLY (c))
13739 gfc_error ("The pointer component %qs of %qs at %L is a type "
13740 "that has not been declared", c->name, sym->name,
13741 &c->loc);
13742 return false;
13745 /* If an allocatable component derived type is of the same type as
13746 the enclosing derived type, we need a vtable generating so that
13747 the __deallocate procedure is created. */
13748 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
13749 && c->ts.u.derived == sym && c->attr.allocatable == 1)
13750 gfc_find_vtab (&c->ts);
13752 /* Ensure that all the derived type components are put on the
13753 derived type list; even in formal namespaces, where derived type
13754 pointer components might not have been declared. */
13755 if (c->ts.type == BT_DERIVED
13756 && c->ts.u.derived
13757 && c->ts.u.derived->components
13758 && c->attr.pointer
13759 && sym != c->ts.u.derived)
13760 add_dt_to_dt_list (c->ts.u.derived);
13762 if (!gfc_resolve_array_spec (c->as,
13763 !(c->attr.pointer || c->attr.proc_pointer
13764 || c->attr.allocatable)))
13765 return false;
13767 if (c->initializer && !sym->attr.vtype
13768 && !c->attr.pdt_kind && !c->attr.pdt_len
13769 && !gfc_check_assign_symbol (sym, c, c->initializer))
13770 return false;
13772 return true;
13776 /* Be nice about the locus for a structure expression - show the locus of the
13777 first non-null sub-expression if we can. */
13779 static locus *
13780 cons_where (gfc_expr *struct_expr)
13782 gfc_constructor *cons;
13784 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
13786 cons = gfc_constructor_first (struct_expr->value.constructor);
13787 for (; cons; cons = gfc_constructor_next (cons))
13789 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
13790 return &cons->expr->where;
13793 return &struct_expr->where;
13796 /* Resolve the components of a structure type. Much less work than derived
13797 types. */
13799 static bool
13800 resolve_fl_struct (gfc_symbol *sym)
13802 gfc_component *c;
13803 gfc_expr *init = NULL;
13804 bool success;
13806 /* Make sure UNIONs do not have overlapping initializers. */
13807 if (sym->attr.flavor == FL_UNION)
13809 for (c = sym->components; c; c = c->next)
13811 if (init && c->initializer)
13813 gfc_error ("Conflicting initializers in union at %L and %L",
13814 cons_where (init), cons_where (c->initializer));
13815 gfc_free_expr (c->initializer);
13816 c->initializer = NULL;
13818 if (init == NULL)
13819 init = c->initializer;
13823 success = true;
13824 for (c = sym->components; c; c = c->next)
13825 if (!resolve_component (c, sym))
13826 success = false;
13828 if (!success)
13829 return false;
13831 if (sym->components)
13832 add_dt_to_dt_list (sym);
13834 return true;
13838 /* Resolve the components of a derived type. This does not have to wait until
13839 resolution stage, but can be done as soon as the dt declaration has been
13840 parsed. */
13842 static bool
13843 resolve_fl_derived0 (gfc_symbol *sym)
13845 gfc_symbol* super_type;
13846 gfc_component *c;
13847 bool success;
13849 if (sym->attr.unlimited_polymorphic)
13850 return true;
13852 super_type = gfc_get_derived_super_type (sym);
13854 /* F2008, C432. */
13855 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
13857 gfc_error ("As extending type %qs at %L has a coarray component, "
13858 "parent type %qs shall also have one", sym->name,
13859 &sym->declared_at, super_type->name);
13860 return false;
13863 /* Ensure the extended type gets resolved before we do. */
13864 if (super_type && !resolve_fl_derived0 (super_type))
13865 return false;
13867 /* An ABSTRACT type must be extensible. */
13868 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
13870 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
13871 sym->name, &sym->declared_at);
13872 return false;
13875 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
13876 : sym->components;
13878 success = true;
13879 for ( ; c != NULL; c = c->next)
13880 if (!resolve_component (c, sym))
13881 success = false;
13883 if (!success)
13884 return false;
13886 check_defined_assignments (sym);
13888 if (!sym->attr.defined_assign_comp && super_type)
13889 sym->attr.defined_assign_comp
13890 = super_type->attr.defined_assign_comp;
13892 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
13893 all DEFERRED bindings are overridden. */
13894 if (super_type && super_type->attr.abstract && !sym->attr.abstract
13895 && !sym->attr.is_class
13896 && !ensure_not_abstract (sym, super_type))
13897 return false;
13899 /* Add derived type to the derived type list. */
13900 add_dt_to_dt_list (sym);
13902 return true;
13906 /* The following procedure does the full resolution of a derived type,
13907 including resolution of all type-bound procedures (if present). In contrast
13908 to 'resolve_fl_derived0' this can only be done after the module has been
13909 parsed completely. */
13911 static bool
13912 resolve_fl_derived (gfc_symbol *sym)
13914 gfc_symbol *gen_dt = NULL;
13916 if (sym->attr.unlimited_polymorphic)
13917 return true;
13919 if (!sym->attr.is_class)
13920 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
13921 if (gen_dt && gen_dt->generic && gen_dt->generic->next
13922 && (!gen_dt->generic->sym->attr.use_assoc
13923 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
13924 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
13925 "%qs at %L being the same name as derived "
13926 "type at %L", sym->name,
13927 gen_dt->generic->sym == sym
13928 ? gen_dt->generic->next->sym->name
13929 : gen_dt->generic->sym->name,
13930 gen_dt->generic->sym == sym
13931 ? &gen_dt->generic->next->sym->declared_at
13932 : &gen_dt->generic->sym->declared_at,
13933 &sym->declared_at))
13934 return false;
13936 /* Resolve the finalizer procedures. */
13937 if (!gfc_resolve_finalizers (sym, NULL))
13938 return false;
13940 if (sym->attr.is_class && sym->ts.u.derived == NULL)
13942 /* Fix up incomplete CLASS symbols. */
13943 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
13944 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
13946 /* Nothing more to do for unlimited polymorphic entities. */
13947 if (data->ts.u.derived->attr.unlimited_polymorphic)
13948 return true;
13949 else if (vptr->ts.u.derived == NULL)
13951 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
13952 gcc_assert (vtab);
13953 vptr->ts.u.derived = vtab->ts.u.derived;
13954 if (!resolve_fl_derived0 (vptr->ts.u.derived))
13955 return false;
13959 if (!resolve_fl_derived0 (sym))
13960 return false;
13962 /* Resolve the type-bound procedures. */
13963 if (!resolve_typebound_procedures (sym))
13964 return false;
13966 return true;
13970 static bool
13971 resolve_fl_namelist (gfc_symbol *sym)
13973 gfc_namelist *nl;
13974 gfc_symbol *nlsym;
13976 for (nl = sym->namelist; nl; nl = nl->next)
13978 /* Check again, the check in match only works if NAMELIST comes
13979 after the decl. */
13980 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
13982 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
13983 "allowed", nl->sym->name, sym->name, &sym->declared_at);
13984 return false;
13987 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
13988 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13989 "with assumed shape in namelist %qs at %L",
13990 nl->sym->name, sym->name, &sym->declared_at))
13991 return false;
13993 if (is_non_constant_shape_array (nl->sym)
13994 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13995 "with nonconstant shape in namelist %qs at %L",
13996 nl->sym->name, sym->name, &sym->declared_at))
13997 return false;
13999 if (nl->sym->ts.type == BT_CHARACTER
14000 && (nl->sym->ts.u.cl->length == NULL
14001 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
14002 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
14003 "nonconstant character length in "
14004 "namelist %qs at %L", nl->sym->name,
14005 sym->name, &sym->declared_at))
14006 return false;
14010 /* Reject PRIVATE objects in a PUBLIC namelist. */
14011 if (gfc_check_symbol_access (sym))
14013 for (nl = sym->namelist; nl; nl = nl->next)
14015 if (!nl->sym->attr.use_assoc
14016 && !is_sym_host_assoc (nl->sym, sym->ns)
14017 && !gfc_check_symbol_access (nl->sym))
14019 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
14020 "cannot be member of PUBLIC namelist %qs at %L",
14021 nl->sym->name, sym->name, &sym->declared_at);
14022 return false;
14025 if (nl->sym->ts.type == BT_DERIVED
14026 && (nl->sym->ts.u.derived->attr.alloc_comp
14027 || nl->sym->ts.u.derived->attr.pointer_comp))
14029 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
14030 "namelist %qs at %L with ALLOCATABLE "
14031 "or POINTER components", nl->sym->name,
14032 sym->name, &sym->declared_at))
14033 return false;
14034 return true;
14037 /* Types with private components that came here by USE-association. */
14038 if (nl->sym->ts.type == BT_DERIVED
14039 && derived_inaccessible (nl->sym->ts.u.derived))
14041 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
14042 "components and cannot be member of namelist %qs at %L",
14043 nl->sym->name, sym->name, &sym->declared_at);
14044 return false;
14047 /* Types with private components that are defined in the same module. */
14048 if (nl->sym->ts.type == BT_DERIVED
14049 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
14050 && nl->sym->ts.u.derived->attr.private_comp)
14052 gfc_error ("NAMELIST object %qs has PRIVATE components and "
14053 "cannot be a member of PUBLIC namelist %qs at %L",
14054 nl->sym->name, sym->name, &sym->declared_at);
14055 return false;
14061 /* 14.1.2 A module or internal procedure represent local entities
14062 of the same type as a namelist member and so are not allowed. */
14063 for (nl = sym->namelist; nl; nl = nl->next)
14065 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
14066 continue;
14068 if (nl->sym->attr.function && nl->sym == nl->sym->result)
14069 if ((nl->sym == sym->ns->proc_name)
14071 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
14072 continue;
14074 nlsym = NULL;
14075 if (nl->sym->name)
14076 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
14077 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
14079 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
14080 "attribute in %qs at %L", nlsym->name,
14081 &sym->declared_at);
14082 return false;
14086 if (async_io_dt)
14088 for (nl = sym->namelist; nl; nl = nl->next)
14089 nl->sym->attr.asynchronous = 1;
14091 return true;
14095 static bool
14096 resolve_fl_parameter (gfc_symbol *sym)
14098 /* A parameter array's shape needs to be constant. */
14099 if (sym->as != NULL
14100 && (sym->as->type == AS_DEFERRED
14101 || is_non_constant_shape_array (sym)))
14103 gfc_error ("Parameter array %qs at %L cannot be automatic "
14104 "or of deferred shape", sym->name, &sym->declared_at);
14105 return false;
14108 /* Constraints on deferred type parameter. */
14109 if (!deferred_requirements (sym))
14110 return false;
14112 /* Make sure a parameter that has been implicitly typed still
14113 matches the implicit type, since PARAMETER statements can precede
14114 IMPLICIT statements. */
14115 if (sym->attr.implicit_type
14116 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
14117 sym->ns)))
14119 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
14120 "later IMPLICIT type", sym->name, &sym->declared_at);
14121 return false;
14124 /* Make sure the types of derived parameters are consistent. This
14125 type checking is deferred until resolution because the type may
14126 refer to a derived type from the host. */
14127 if (sym->ts.type == BT_DERIVED
14128 && !gfc_compare_types (&sym->ts, &sym->value->ts))
14130 gfc_error ("Incompatible derived type in PARAMETER at %L",
14131 &sym->value->where);
14132 return false;
14135 /* F03:C509,C514. */
14136 if (sym->ts.type == BT_CLASS)
14138 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
14139 sym->name, &sym->declared_at);
14140 return false;
14143 return true;
14147 /* Called by resolve_symbol to chack PDTs. */
14149 static void
14150 resolve_pdt (gfc_symbol* sym)
14152 gfc_symbol *derived = NULL;
14153 gfc_actual_arglist *param;
14154 gfc_component *c;
14155 bool const_len_exprs = true;
14156 bool assumed_len_exprs = false;
14158 if (sym->ts.type == BT_DERIVED)
14159 derived = sym->ts.u.derived;
14160 else if (sym->ts.type == BT_CLASS)
14161 derived = CLASS_DATA (sym)->ts.u.derived;
14162 else
14163 gcc_unreachable ();
14165 gcc_assert (derived->attr.pdt_type);
14167 for (param = sym->param_list; param; param = param->next)
14169 c = gfc_find_component (derived, param->name, false, true, NULL);
14170 gcc_assert (c);
14171 if (c->attr.pdt_kind)
14172 continue;
14174 if (param->expr && !gfc_is_constant_expr (param->expr)
14175 && c->attr.pdt_len)
14176 const_len_exprs = false;
14177 else if (param->spec_type == SPEC_ASSUMED)
14178 assumed_len_exprs = true;
14181 if (!const_len_exprs
14182 && (sym->ns->proc_name->attr.is_main_program
14183 || sym->ns->proc_name->attr.flavor == FL_MODULE
14184 || sym->attr.save != SAVE_NONE))
14185 gfc_error ("The AUTOMATIC object %qs at %L must not have the "
14186 "SAVE attribute or be a variable declared in the "
14187 "main program, a module or a submodule(F08/C513)",
14188 sym->name, &sym->declared_at);
14190 if (assumed_len_exprs && !(sym->attr.dummy
14191 || sym->attr.select_type_temporary || sym->attr.associate_var))
14192 gfc_error ("The object %qs at %L with ASSUMED type parameters "
14193 "must be a dummy or a SELECT TYPE selector(F08/4.2)",
14194 sym->name, &sym->declared_at);
14198 /* Do anything necessary to resolve a symbol. Right now, we just
14199 assume that an otherwise unknown symbol is a variable. This sort
14200 of thing commonly happens for symbols in module. */
14202 static void
14203 resolve_symbol (gfc_symbol *sym)
14205 int check_constant, mp_flag;
14206 gfc_symtree *symtree;
14207 gfc_symtree *this_symtree;
14208 gfc_namespace *ns;
14209 gfc_component *c;
14210 symbol_attribute class_attr;
14211 gfc_array_spec *as;
14212 bool saved_specification_expr;
14214 if (sym->resolved)
14215 return;
14216 sym->resolved = 1;
14218 /* No symbol will ever have union type; only components can be unions.
14219 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
14220 (just like derived type declaration symbols have flavor FL_DERIVED). */
14221 gcc_assert (sym->ts.type != BT_UNION);
14223 /* Coarrayed polymorphic objects with allocatable or pointer components are
14224 yet unsupported for -fcoarray=lib. */
14225 if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
14226 && sym->ts.u.derived && CLASS_DATA (sym)
14227 && CLASS_DATA (sym)->attr.codimension
14228 && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
14229 || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
14231 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
14232 "type coarrays at %L are unsupported", &sym->declared_at);
14233 return;
14236 if (sym->attr.artificial)
14237 return;
14239 if (sym->attr.unlimited_polymorphic)
14240 return;
14242 if (sym->attr.flavor == FL_UNKNOWN
14243 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
14244 && !sym->attr.generic && !sym->attr.external
14245 && sym->attr.if_source == IFSRC_UNKNOWN
14246 && sym->ts.type == BT_UNKNOWN))
14249 /* If we find that a flavorless symbol is an interface in one of the
14250 parent namespaces, find its symtree in this namespace, free the
14251 symbol and set the symtree to point to the interface symbol. */
14252 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
14254 symtree = gfc_find_symtree (ns->sym_root, sym->name);
14255 if (symtree && (symtree->n.sym->generic ||
14256 (symtree->n.sym->attr.flavor == FL_PROCEDURE
14257 && sym->ns->construct_entities)))
14259 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
14260 sym->name);
14261 if (this_symtree->n.sym == sym)
14263 symtree->n.sym->refs++;
14264 gfc_release_symbol (sym);
14265 this_symtree->n.sym = symtree->n.sym;
14266 return;
14271 /* Otherwise give it a flavor according to such attributes as
14272 it has. */
14273 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
14274 && sym->attr.intrinsic == 0)
14275 sym->attr.flavor = FL_VARIABLE;
14276 else if (sym->attr.flavor == FL_UNKNOWN)
14278 sym->attr.flavor = FL_PROCEDURE;
14279 if (sym->attr.dimension)
14280 sym->attr.function = 1;
14284 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
14285 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
14287 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
14288 && !resolve_procedure_interface (sym))
14289 return;
14291 if (sym->attr.is_protected && !sym->attr.proc_pointer
14292 && (sym->attr.procedure || sym->attr.external))
14294 if (sym->attr.external)
14295 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
14296 "at %L", &sym->declared_at);
14297 else
14298 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
14299 "at %L", &sym->declared_at);
14301 return;
14304 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
14305 return;
14307 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
14308 && !resolve_fl_struct (sym))
14309 return;
14311 /* Symbols that are module procedures with results (functions) have
14312 the types and array specification copied for type checking in
14313 procedures that call them, as well as for saving to a module
14314 file. These symbols can't stand the scrutiny that their results
14315 can. */
14316 mp_flag = (sym->result != NULL && sym->result != sym);
14318 /* Make sure that the intrinsic is consistent with its internal
14319 representation. This needs to be done before assigning a default
14320 type to avoid spurious warnings. */
14321 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
14322 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
14323 return;
14325 /* Resolve associate names. */
14326 if (sym->assoc)
14327 resolve_assoc_var (sym, true);
14329 /* Assign default type to symbols that need one and don't have one. */
14330 if (sym->ts.type == BT_UNKNOWN)
14332 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
14334 gfc_set_default_type (sym, 1, NULL);
14337 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
14338 && !sym->attr.function && !sym->attr.subroutine
14339 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
14340 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
14342 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
14344 /* The specific case of an external procedure should emit an error
14345 in the case that there is no implicit type. */
14346 if (!mp_flag)
14348 if (!sym->attr.mixed_entry_master)
14349 gfc_set_default_type (sym, sym->attr.external, NULL);
14351 else
14353 /* Result may be in another namespace. */
14354 resolve_symbol (sym->result);
14356 if (!sym->result->attr.proc_pointer)
14358 sym->ts = sym->result->ts;
14359 sym->as = gfc_copy_array_spec (sym->result->as);
14360 sym->attr.dimension = sym->result->attr.dimension;
14361 sym->attr.pointer = sym->result->attr.pointer;
14362 sym->attr.allocatable = sym->result->attr.allocatable;
14363 sym->attr.contiguous = sym->result->attr.contiguous;
14368 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
14370 bool saved_specification_expr = specification_expr;
14371 specification_expr = true;
14372 gfc_resolve_array_spec (sym->result->as, false);
14373 specification_expr = saved_specification_expr;
14376 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
14378 as = CLASS_DATA (sym)->as;
14379 class_attr = CLASS_DATA (sym)->attr;
14380 class_attr.pointer = class_attr.class_pointer;
14382 else
14384 class_attr = sym->attr;
14385 as = sym->as;
14388 /* F2008, C530. */
14389 if (sym->attr.contiguous
14390 && (!class_attr.dimension
14391 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
14392 && !class_attr.pointer)))
14394 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
14395 "array pointer or an assumed-shape or assumed-rank array",
14396 sym->name, &sym->declared_at);
14397 return;
14400 /* Assumed size arrays and assumed shape arrays must be dummy
14401 arguments. Array-spec's of implied-shape should have been resolved to
14402 AS_EXPLICIT already. */
14404 if (as)
14406 gcc_assert (as->type != AS_IMPLIED_SHAPE);
14407 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
14408 || as->type == AS_ASSUMED_SHAPE)
14409 && !sym->attr.dummy && !sym->attr.select_type_temporary)
14411 if (as->type == AS_ASSUMED_SIZE)
14412 gfc_error ("Assumed size array at %L must be a dummy argument",
14413 &sym->declared_at);
14414 else
14415 gfc_error ("Assumed shape array at %L must be a dummy argument",
14416 &sym->declared_at);
14417 return;
14419 /* TS 29113, C535a. */
14420 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
14421 && !sym->attr.select_type_temporary)
14423 gfc_error ("Assumed-rank array at %L must be a dummy argument",
14424 &sym->declared_at);
14425 return;
14427 if (as->type == AS_ASSUMED_RANK
14428 && (sym->attr.codimension || sym->attr.value))
14430 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
14431 "CODIMENSION attribute", &sym->declared_at);
14432 return;
14436 /* Make sure symbols with known intent or optional are really dummy
14437 variable. Because of ENTRY statement, this has to be deferred
14438 until resolution time. */
14440 if (!sym->attr.dummy
14441 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
14443 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
14444 return;
14447 if (sym->attr.value && !sym->attr.dummy)
14449 gfc_error ("%qs at %L cannot have the VALUE attribute because "
14450 "it is not a dummy argument", sym->name, &sym->declared_at);
14451 return;
14454 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
14456 gfc_charlen *cl = sym->ts.u.cl;
14457 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
14459 gfc_error ("Character dummy variable %qs at %L with VALUE "
14460 "attribute must have constant length",
14461 sym->name, &sym->declared_at);
14462 return;
14465 if (sym->ts.is_c_interop
14466 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
14468 gfc_error ("C interoperable character dummy variable %qs at %L "
14469 "with VALUE attribute must have length one",
14470 sym->name, &sym->declared_at);
14471 return;
14475 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
14476 && sym->ts.u.derived->attr.generic)
14478 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
14479 if (!sym->ts.u.derived)
14481 gfc_error ("The derived type %qs at %L is of type %qs, "
14482 "which has not been defined", sym->name,
14483 &sym->declared_at, sym->ts.u.derived->name);
14484 sym->ts.type = BT_UNKNOWN;
14485 return;
14489 /* Use the same constraints as TYPE(*), except for the type check
14490 and that only scalars and assumed-size arrays are permitted. */
14491 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
14493 if (!sym->attr.dummy)
14495 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
14496 "a dummy argument", sym->name, &sym->declared_at);
14497 return;
14500 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
14501 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
14502 && sym->ts.type != BT_COMPLEX)
14504 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
14505 "of type TYPE(*) or of an numeric intrinsic type",
14506 sym->name, &sym->declared_at);
14507 return;
14510 if (sym->attr.allocatable || sym->attr.codimension
14511 || sym->attr.pointer || sym->attr.value)
14513 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
14514 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
14515 "attribute", sym->name, &sym->declared_at);
14516 return;
14519 if (sym->attr.intent == INTENT_OUT)
14521 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
14522 "have the INTENT(OUT) attribute",
14523 sym->name, &sym->declared_at);
14524 return;
14526 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
14528 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
14529 "either be a scalar or an assumed-size array",
14530 sym->name, &sym->declared_at);
14531 return;
14534 /* Set the type to TYPE(*) and add a dimension(*) to ensure
14535 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
14536 packing. */
14537 sym->ts.type = BT_ASSUMED;
14538 sym->as = gfc_get_array_spec ();
14539 sym->as->type = AS_ASSUMED_SIZE;
14540 sym->as->rank = 1;
14541 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
14543 else if (sym->ts.type == BT_ASSUMED)
14545 /* TS 29113, C407a. */
14546 if (!sym->attr.dummy)
14548 gfc_error ("Assumed type of variable %s at %L is only permitted "
14549 "for dummy variables", sym->name, &sym->declared_at);
14550 return;
14552 if (sym->attr.allocatable || sym->attr.codimension
14553 || sym->attr.pointer || sym->attr.value)
14555 gfc_error ("Assumed-type variable %s at %L may not have the "
14556 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
14557 sym->name, &sym->declared_at);
14558 return;
14560 if (sym->attr.intent == INTENT_OUT)
14562 gfc_error ("Assumed-type variable %s at %L may not have the "
14563 "INTENT(OUT) attribute",
14564 sym->name, &sym->declared_at);
14565 return;
14567 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
14569 gfc_error ("Assumed-type variable %s at %L shall not be an "
14570 "explicit-shape array", sym->name, &sym->declared_at);
14571 return;
14575 /* If the symbol is marked as bind(c), that it is declared at module level
14576 scope and verify its type and kind. Do not do the latter for symbols
14577 that are implicitly typed because that is handled in
14578 gfc_set_default_type. Handle dummy arguments and procedure definitions
14579 separately. Also, anything that is use associated is not handled here
14580 but instead is handled in the module it is declared in. Finally, derived
14581 type definitions are allowed to be BIND(C) since that only implies that
14582 they're interoperable, and they are checked fully for interoperability
14583 when a variable is declared of that type. */
14584 if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
14585 && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
14586 && sym->attr.flavor != FL_DERIVED)
14588 bool t = true;
14590 /* First, make sure the variable is declared at the
14591 module-level scope (J3/04-007, Section 15.3). */
14592 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
14593 sym->attr.in_common == 0)
14595 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
14596 "is neither a COMMON block nor declared at the "
14597 "module level scope", sym->name, &(sym->declared_at));
14598 t = false;
14600 else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
14602 t = verify_com_block_vars_c_interop (sym->common_head);
14604 else if (sym->attr.implicit_type == 0)
14606 /* If type() declaration, we need to verify that the components
14607 of the given type are all C interoperable, etc. */
14608 if (sym->ts.type == BT_DERIVED &&
14609 sym->ts.u.derived->attr.is_c_interop != 1)
14611 /* Make sure the user marked the derived type as BIND(C). If
14612 not, call the verify routine. This could print an error
14613 for the derived type more than once if multiple variables
14614 of that type are declared. */
14615 if (sym->ts.u.derived->attr.is_bind_c != 1)
14616 verify_bind_c_derived_type (sym->ts.u.derived);
14617 t = false;
14620 /* Verify the variable itself as C interoperable if it
14621 is BIND(C). It is not possible for this to succeed if
14622 the verify_bind_c_derived_type failed, so don't have to handle
14623 any error returned by verify_bind_c_derived_type. */
14624 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
14625 sym->common_block);
14628 if (!t)
14630 /* clear the is_bind_c flag to prevent reporting errors more than
14631 once if something failed. */
14632 sym->attr.is_bind_c = 0;
14633 return;
14637 /* If a derived type symbol has reached this point, without its
14638 type being declared, we have an error. Notice that most
14639 conditions that produce undefined derived types have already
14640 been dealt with. However, the likes of:
14641 implicit type(t) (t) ..... call foo (t) will get us here if
14642 the type is not declared in the scope of the implicit
14643 statement. Change the type to BT_UNKNOWN, both because it is so
14644 and to prevent an ICE. */
14645 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
14646 && sym->ts.u.derived->components == NULL
14647 && !sym->ts.u.derived->attr.zero_comp)
14649 gfc_error ("The derived type %qs at %L is of type %qs, "
14650 "which has not been defined", sym->name,
14651 &sym->declared_at, sym->ts.u.derived->name);
14652 sym->ts.type = BT_UNKNOWN;
14653 return;
14656 /* Make sure that the derived type has been resolved and that the
14657 derived type is visible in the symbol's namespace, if it is a
14658 module function and is not PRIVATE. */
14659 if (sym->ts.type == BT_DERIVED
14660 && sym->ts.u.derived->attr.use_assoc
14661 && sym->ns->proc_name
14662 && sym->ns->proc_name->attr.flavor == FL_MODULE
14663 && !resolve_fl_derived (sym->ts.u.derived))
14664 return;
14666 /* Unless the derived-type declaration is use associated, Fortran 95
14667 does not allow public entries of private derived types.
14668 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
14669 161 in 95-006r3. */
14670 if (sym->ts.type == BT_DERIVED
14671 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
14672 && !sym->ts.u.derived->attr.use_assoc
14673 && gfc_check_symbol_access (sym)
14674 && !gfc_check_symbol_access (sym->ts.u.derived)
14675 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
14676 "derived type %qs",
14677 (sym->attr.flavor == FL_PARAMETER)
14678 ? "parameter" : "variable",
14679 sym->name, &sym->declared_at,
14680 sym->ts.u.derived->name))
14681 return;
14683 /* F2008, C1302. */
14684 if (sym->ts.type == BT_DERIVED
14685 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
14686 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
14687 || sym->ts.u.derived->attr.lock_comp)
14688 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
14690 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
14691 "type LOCK_TYPE must be a coarray", sym->name,
14692 &sym->declared_at);
14693 return;
14696 /* TS18508, C702/C703. */
14697 if (sym->ts.type == BT_DERIVED
14698 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
14699 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
14700 || sym->ts.u.derived->attr.event_comp)
14701 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
14703 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
14704 "type EVENT_TYPE must be a coarray", sym->name,
14705 &sym->declared_at);
14706 return;
14709 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
14710 default initialization is defined (5.1.2.4.4). */
14711 if (sym->ts.type == BT_DERIVED
14712 && sym->attr.dummy
14713 && sym->attr.intent == INTENT_OUT
14714 && sym->as
14715 && sym->as->type == AS_ASSUMED_SIZE)
14717 for (c = sym->ts.u.derived->components; c; c = c->next)
14719 if (c->initializer)
14721 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
14722 "ASSUMED SIZE and so cannot have a default initializer",
14723 sym->name, &sym->declared_at);
14724 return;
14729 /* F2008, C542. */
14730 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
14731 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
14733 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
14734 "INTENT(OUT)", sym->name, &sym->declared_at);
14735 return;
14738 /* TS18508. */
14739 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
14740 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
14742 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
14743 "INTENT(OUT)", sym->name, &sym->declared_at);
14744 return;
14747 /* F2008, C525. */
14748 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14749 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14750 && CLASS_DATA (sym)->attr.coarray_comp))
14751 || class_attr.codimension)
14752 && (sym->attr.result || sym->result == sym))
14754 gfc_error ("Function result %qs at %L shall not be a coarray or have "
14755 "a coarray component", sym->name, &sym->declared_at);
14756 return;
14759 /* F2008, C524. */
14760 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
14761 && sym->ts.u.derived->ts.is_iso_c)
14763 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14764 "shall not be a coarray", sym->name, &sym->declared_at);
14765 return;
14768 /* F2008, C525. */
14769 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14770 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14771 && CLASS_DATA (sym)->attr.coarray_comp))
14772 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
14773 || class_attr.allocatable))
14775 gfc_error ("Variable %qs at %L with coarray component shall be a "
14776 "nonpointer, nonallocatable scalar, which is not a coarray",
14777 sym->name, &sym->declared_at);
14778 return;
14781 /* F2008, C526. The function-result case was handled above. */
14782 if (class_attr.codimension
14783 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
14784 || sym->attr.select_type_temporary
14785 || sym->attr.associate_var
14786 || (sym->ns->save_all && !sym->attr.automatic)
14787 || sym->ns->proc_name->attr.flavor == FL_MODULE
14788 || sym->ns->proc_name->attr.is_main_program
14789 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
14791 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
14792 "nor a dummy argument", sym->name, &sym->declared_at);
14793 return;
14795 /* F2008, C528. */
14796 else if (class_attr.codimension && !sym->attr.select_type_temporary
14797 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
14799 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
14800 "deferred shape", sym->name, &sym->declared_at);
14801 return;
14803 else if (class_attr.codimension && class_attr.allocatable && as
14804 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
14806 gfc_error ("Allocatable coarray variable %qs at %L must have "
14807 "deferred shape", sym->name, &sym->declared_at);
14808 return;
14811 /* F2008, C541. */
14812 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14813 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14814 && CLASS_DATA (sym)->attr.coarray_comp))
14815 || (class_attr.codimension && class_attr.allocatable))
14816 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
14818 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
14819 "allocatable coarray or have coarray components",
14820 sym->name, &sym->declared_at);
14821 return;
14824 if (class_attr.codimension && sym->attr.dummy
14825 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
14827 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
14828 "procedure %qs", sym->name, &sym->declared_at,
14829 sym->ns->proc_name->name);
14830 return;
14833 if (sym->ts.type == BT_LOGICAL
14834 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
14835 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
14836 && sym->ns->proc_name->attr.is_bind_c)))
14838 int i;
14839 for (i = 0; gfc_logical_kinds[i].kind; i++)
14840 if (gfc_logical_kinds[i].kind == sym->ts.kind)
14841 break;
14842 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
14843 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
14844 "%L with non-C_Bool kind in BIND(C) procedure "
14845 "%qs", sym->name, &sym->declared_at,
14846 sym->ns->proc_name->name))
14847 return;
14848 else if (!gfc_logical_kinds[i].c_bool
14849 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
14850 "%qs at %L with non-C_Bool kind in "
14851 "BIND(C) procedure %qs", sym->name,
14852 &sym->declared_at,
14853 sym->attr.function ? sym->name
14854 : sym->ns->proc_name->name))
14855 return;
14858 switch (sym->attr.flavor)
14860 case FL_VARIABLE:
14861 if (!resolve_fl_variable (sym, mp_flag))
14862 return;
14863 break;
14865 case FL_PROCEDURE:
14866 if (sym->formal && !sym->formal_ns)
14868 /* Check that none of the arguments are a namelist. */
14869 gfc_formal_arglist *formal = sym->formal;
14871 for (; formal; formal = formal->next)
14872 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
14874 gfc_error ("Namelist %qs can not be an argument to "
14875 "subroutine or function at %L",
14876 formal->sym->name, &sym->declared_at);
14877 return;
14881 if (!resolve_fl_procedure (sym, mp_flag))
14882 return;
14883 break;
14885 case FL_NAMELIST:
14886 if (!resolve_fl_namelist (sym))
14887 return;
14888 break;
14890 case FL_PARAMETER:
14891 if (!resolve_fl_parameter (sym))
14892 return;
14893 break;
14895 default:
14896 break;
14899 /* Resolve array specifier. Check as well some constraints
14900 on COMMON blocks. */
14902 check_constant = sym->attr.in_common && !sym->attr.pointer;
14904 /* Set the formal_arg_flag so that check_conflict will not throw
14905 an error for host associated variables in the specification
14906 expression for an array_valued function. */
14907 if (sym->attr.function && sym->as)
14908 formal_arg_flag = true;
14910 saved_specification_expr = specification_expr;
14911 specification_expr = true;
14912 gfc_resolve_array_spec (sym->as, check_constant);
14913 specification_expr = saved_specification_expr;
14915 formal_arg_flag = false;
14917 /* Resolve formal namespaces. */
14918 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
14919 && !sym->attr.contained && !sym->attr.intrinsic)
14920 gfc_resolve (sym->formal_ns);
14922 /* Make sure the formal namespace is present. */
14923 if (sym->formal && !sym->formal_ns)
14925 gfc_formal_arglist *formal = sym->formal;
14926 while (formal && !formal->sym)
14927 formal = formal->next;
14929 if (formal)
14931 sym->formal_ns = formal->sym->ns;
14932 if (sym->ns != formal->sym->ns)
14933 sym->formal_ns->refs++;
14937 /* Check threadprivate restrictions. */
14938 if (sym->attr.threadprivate && !sym->attr.save
14939 && !(sym->ns->save_all && !sym->attr.automatic)
14940 && (!sym->attr.in_common
14941 && sym->module == NULL
14942 && (sym->ns->proc_name == NULL
14943 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
14944 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
14946 /* Check omp declare target restrictions. */
14947 if (sym->attr.omp_declare_target
14948 && sym->attr.flavor == FL_VARIABLE
14949 && !sym->attr.save
14950 && !(sym->ns->save_all && !sym->attr.automatic)
14951 && (!sym->attr.in_common
14952 && sym->module == NULL
14953 && (sym->ns->proc_name == NULL
14954 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
14955 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
14956 sym->name, &sym->declared_at);
14958 /* If we have come this far we can apply default-initializers, as
14959 described in 14.7.5, to those variables that have not already
14960 been assigned one. */
14961 if (sym->ts.type == BT_DERIVED
14962 && !sym->value
14963 && !sym->attr.allocatable
14964 && !sym->attr.alloc_comp)
14966 symbol_attribute *a = &sym->attr;
14968 if ((!a->save && !a->dummy && !a->pointer
14969 && !a->in_common && !a->use_assoc
14970 && !a->result && !a->function)
14971 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
14972 apply_default_init (sym);
14973 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
14974 && (sym->ts.u.derived->attr.alloc_comp
14975 || sym->ts.u.derived->attr.pointer_comp))
14976 /* Mark the result symbol to be referenced, when it has allocatable
14977 components. */
14978 sym->result->attr.referenced = 1;
14981 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
14982 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
14983 && !CLASS_DATA (sym)->attr.class_pointer
14984 && !CLASS_DATA (sym)->attr.allocatable)
14985 apply_default_init (sym);
14987 /* If this symbol has a type-spec, check it. */
14988 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
14989 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
14990 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
14991 return;
14993 if (sym->param_list)
14994 resolve_pdt (sym);
14998 /************* Resolve DATA statements *************/
15000 static struct
15002 gfc_data_value *vnode;
15003 mpz_t left;
15005 values;
15008 /* Advance the values structure to point to the next value in the data list. */
15010 static bool
15011 next_data_value (void)
15013 while (mpz_cmp_ui (values.left, 0) == 0)
15016 if (values.vnode->next == NULL)
15017 return false;
15019 values.vnode = values.vnode->next;
15020 mpz_set (values.left, values.vnode->repeat);
15023 return true;
15027 static bool
15028 check_data_variable (gfc_data_variable *var, locus *where)
15030 gfc_expr *e;
15031 mpz_t size;
15032 mpz_t offset;
15033 bool t;
15034 ar_type mark = AR_UNKNOWN;
15035 int i;
15036 mpz_t section_index[GFC_MAX_DIMENSIONS];
15037 gfc_ref *ref;
15038 gfc_array_ref *ar;
15039 gfc_symbol *sym;
15040 int has_pointer;
15042 if (!gfc_resolve_expr (var->expr))
15043 return false;
15045 ar = NULL;
15046 mpz_init_set_si (offset, 0);
15047 e = var->expr;
15049 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
15050 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
15051 e = e->value.function.actual->expr;
15053 if (e->expr_type != EXPR_VARIABLE)
15054 gfc_internal_error ("check_data_variable(): Bad expression");
15056 sym = e->symtree->n.sym;
15058 if (sym->ns->is_block_data && !sym->attr.in_common)
15060 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
15061 sym->name, &sym->declared_at);
15064 if (e->ref == NULL && sym->as)
15066 gfc_error ("DATA array %qs at %L must be specified in a previous"
15067 " declaration", sym->name, where);
15068 return false;
15071 has_pointer = sym->attr.pointer;
15073 if (gfc_is_coindexed (e))
15075 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
15076 where);
15077 return false;
15080 for (ref = e->ref; ref; ref = ref->next)
15082 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
15083 has_pointer = 1;
15085 if (has_pointer
15086 && ref->type == REF_ARRAY
15087 && ref->u.ar.type != AR_FULL)
15089 gfc_error ("DATA element %qs at %L is a pointer and so must "
15090 "be a full array", sym->name, where);
15091 return false;
15095 if (e->rank == 0 || has_pointer)
15097 mpz_init_set_ui (size, 1);
15098 ref = NULL;
15100 else
15102 ref = e->ref;
15104 /* Find the array section reference. */
15105 for (ref = e->ref; ref; ref = ref->next)
15107 if (ref->type != REF_ARRAY)
15108 continue;
15109 if (ref->u.ar.type == AR_ELEMENT)
15110 continue;
15111 break;
15113 gcc_assert (ref);
15115 /* Set marks according to the reference pattern. */
15116 switch (ref->u.ar.type)
15118 case AR_FULL:
15119 mark = AR_FULL;
15120 break;
15122 case AR_SECTION:
15123 ar = &ref->u.ar;
15124 /* Get the start position of array section. */
15125 gfc_get_section_index (ar, section_index, &offset);
15126 mark = AR_SECTION;
15127 break;
15129 default:
15130 gcc_unreachable ();
15133 if (!gfc_array_size (e, &size))
15135 gfc_error ("Nonconstant array section at %L in DATA statement",
15136 &e->where);
15137 mpz_clear (offset);
15138 return false;
15142 t = true;
15144 while (mpz_cmp_ui (size, 0) > 0)
15146 if (!next_data_value ())
15148 gfc_error ("DATA statement at %L has more variables than values",
15149 where);
15150 t = false;
15151 break;
15154 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
15155 if (!t)
15156 break;
15158 /* If we have more than one element left in the repeat count,
15159 and we have more than one element left in the target variable,
15160 then create a range assignment. */
15161 /* FIXME: Only done for full arrays for now, since array sections
15162 seem tricky. */
15163 if (mark == AR_FULL && ref && ref->next == NULL
15164 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
15166 mpz_t range;
15168 if (mpz_cmp (size, values.left) >= 0)
15170 mpz_init_set (range, values.left);
15171 mpz_sub (size, size, values.left);
15172 mpz_set_ui (values.left, 0);
15174 else
15176 mpz_init_set (range, size);
15177 mpz_sub (values.left, values.left, size);
15178 mpz_set_ui (size, 0);
15181 t = gfc_assign_data_value (var->expr, values.vnode->expr,
15182 offset, &range);
15184 mpz_add (offset, offset, range);
15185 mpz_clear (range);
15187 if (!t)
15188 break;
15191 /* Assign initial value to symbol. */
15192 else
15194 mpz_sub_ui (values.left, values.left, 1);
15195 mpz_sub_ui (size, size, 1);
15197 t = gfc_assign_data_value (var->expr, values.vnode->expr,
15198 offset, NULL);
15199 if (!t)
15200 break;
15202 if (mark == AR_FULL)
15203 mpz_add_ui (offset, offset, 1);
15205 /* Modify the array section indexes and recalculate the offset
15206 for next element. */
15207 else if (mark == AR_SECTION)
15208 gfc_advance_section (section_index, ar, &offset);
15212 if (mark == AR_SECTION)
15214 for (i = 0; i < ar->dimen; i++)
15215 mpz_clear (section_index[i]);
15218 mpz_clear (size);
15219 mpz_clear (offset);
15221 return t;
15225 static bool traverse_data_var (gfc_data_variable *, locus *);
15227 /* Iterate over a list of elements in a DATA statement. */
15229 static bool
15230 traverse_data_list (gfc_data_variable *var, locus *where)
15232 mpz_t trip;
15233 iterator_stack frame;
15234 gfc_expr *e, *start, *end, *step;
15235 bool retval = true;
15237 mpz_init (frame.value);
15238 mpz_init (trip);
15240 start = gfc_copy_expr (var->iter.start);
15241 end = gfc_copy_expr (var->iter.end);
15242 step = gfc_copy_expr (var->iter.step);
15244 if (!gfc_simplify_expr (start, 1)
15245 || start->expr_type != EXPR_CONSTANT)
15247 gfc_error ("start of implied-do loop at %L could not be "
15248 "simplified to a constant value", &start->where);
15249 retval = false;
15250 goto cleanup;
15252 if (!gfc_simplify_expr (end, 1)
15253 || end->expr_type != EXPR_CONSTANT)
15255 gfc_error ("end of implied-do loop at %L could not be "
15256 "simplified to a constant value", &start->where);
15257 retval = false;
15258 goto cleanup;
15260 if (!gfc_simplify_expr (step, 1)
15261 || step->expr_type != EXPR_CONSTANT)
15263 gfc_error ("step of implied-do loop at %L could not be "
15264 "simplified to a constant value", &start->where);
15265 retval = false;
15266 goto cleanup;
15269 mpz_set (trip, end->value.integer);
15270 mpz_sub (trip, trip, start->value.integer);
15271 mpz_add (trip, trip, step->value.integer);
15273 mpz_div (trip, trip, step->value.integer);
15275 mpz_set (frame.value, start->value.integer);
15277 frame.prev = iter_stack;
15278 frame.variable = var->iter.var->symtree;
15279 iter_stack = &frame;
15281 while (mpz_cmp_ui (trip, 0) > 0)
15283 if (!traverse_data_var (var->list, where))
15285 retval = false;
15286 goto cleanup;
15289 e = gfc_copy_expr (var->expr);
15290 if (!gfc_simplify_expr (e, 1))
15292 gfc_free_expr (e);
15293 retval = false;
15294 goto cleanup;
15297 mpz_add (frame.value, frame.value, step->value.integer);
15299 mpz_sub_ui (trip, trip, 1);
15302 cleanup:
15303 mpz_clear (frame.value);
15304 mpz_clear (trip);
15306 gfc_free_expr (start);
15307 gfc_free_expr (end);
15308 gfc_free_expr (step);
15310 iter_stack = frame.prev;
15311 return retval;
15315 /* Type resolve variables in the variable list of a DATA statement. */
15317 static bool
15318 traverse_data_var (gfc_data_variable *var, locus *where)
15320 bool t;
15322 for (; var; var = var->next)
15324 if (var->expr == NULL)
15325 t = traverse_data_list (var, where);
15326 else
15327 t = check_data_variable (var, where);
15329 if (!t)
15330 return false;
15333 return true;
15337 /* Resolve the expressions and iterators associated with a data statement.
15338 This is separate from the assignment checking because data lists should
15339 only be resolved once. */
15341 static bool
15342 resolve_data_variables (gfc_data_variable *d)
15344 for (; d; d = d->next)
15346 if (d->list == NULL)
15348 if (!gfc_resolve_expr (d->expr))
15349 return false;
15351 else
15353 if (!gfc_resolve_iterator (&d->iter, false, true))
15354 return false;
15356 if (!resolve_data_variables (d->list))
15357 return false;
15361 return true;
15365 /* Resolve a single DATA statement. We implement this by storing a pointer to
15366 the value list into static variables, and then recursively traversing the
15367 variables list, expanding iterators and such. */
15369 static void
15370 resolve_data (gfc_data *d)
15373 if (!resolve_data_variables (d->var))
15374 return;
15376 values.vnode = d->value;
15377 if (d->value == NULL)
15378 mpz_set_ui (values.left, 0);
15379 else
15380 mpz_set (values.left, d->value->repeat);
15382 if (!traverse_data_var (d->var, &d->where))
15383 return;
15385 /* At this point, we better not have any values left. */
15387 if (next_data_value ())
15388 gfc_error ("DATA statement at %L has more values than variables",
15389 &d->where);
15393 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
15394 accessed by host or use association, is a dummy argument to a pure function,
15395 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
15396 is storage associated with any such variable, shall not be used in the
15397 following contexts: (clients of this function). */
15399 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
15400 procedure. Returns zero if assignment is OK, nonzero if there is a
15401 problem. */
15403 gfc_impure_variable (gfc_symbol *sym)
15405 gfc_symbol *proc;
15406 gfc_namespace *ns;
15408 if (sym->attr.use_assoc || sym->attr.in_common)
15409 return 1;
15411 /* Check if the symbol's ns is inside the pure procedure. */
15412 for (ns = gfc_current_ns; ns; ns = ns->parent)
15414 if (ns == sym->ns)
15415 break;
15416 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
15417 return 1;
15420 proc = sym->ns->proc_name;
15421 if (sym->attr.dummy
15422 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
15423 || proc->attr.function))
15424 return 1;
15426 /* TODO: Sort out what can be storage associated, if anything, and include
15427 it here. In principle equivalences should be scanned but it does not
15428 seem to be possible to storage associate an impure variable this way. */
15429 return 0;
15433 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
15434 current namespace is inside a pure procedure. */
15437 gfc_pure (gfc_symbol *sym)
15439 symbol_attribute attr;
15440 gfc_namespace *ns;
15442 if (sym == NULL)
15444 /* Check if the current namespace or one of its parents
15445 belongs to a pure procedure. */
15446 for (ns = gfc_current_ns; ns; ns = ns->parent)
15448 sym = ns->proc_name;
15449 if (sym == NULL)
15450 return 0;
15451 attr = sym->attr;
15452 if (attr.flavor == FL_PROCEDURE && attr.pure)
15453 return 1;
15455 return 0;
15458 attr = sym->attr;
15460 return attr.flavor == FL_PROCEDURE && attr.pure;
15464 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
15465 checks if the current namespace is implicitly pure. Note that this
15466 function returns false for a PURE procedure. */
15469 gfc_implicit_pure (gfc_symbol *sym)
15471 gfc_namespace *ns;
15473 if (sym == NULL)
15475 /* Check if the current procedure is implicit_pure. Walk up
15476 the procedure list until we find a procedure. */
15477 for (ns = gfc_current_ns; ns; ns = ns->parent)
15479 sym = ns->proc_name;
15480 if (sym == NULL)
15481 return 0;
15483 if (sym->attr.flavor == FL_PROCEDURE)
15484 break;
15488 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
15489 && !sym->attr.pure;
15493 void
15494 gfc_unset_implicit_pure (gfc_symbol *sym)
15496 gfc_namespace *ns;
15498 if (sym == NULL)
15500 /* Check if the current procedure is implicit_pure. Walk up
15501 the procedure list until we find a procedure. */
15502 for (ns = gfc_current_ns; ns; ns = ns->parent)
15504 sym = ns->proc_name;
15505 if (sym == NULL)
15506 return;
15508 if (sym->attr.flavor == FL_PROCEDURE)
15509 break;
15513 if (sym->attr.flavor == FL_PROCEDURE)
15514 sym->attr.implicit_pure = 0;
15515 else
15516 sym->attr.pure = 0;
15520 /* Test whether the current procedure is elemental or not. */
15523 gfc_elemental (gfc_symbol *sym)
15525 symbol_attribute attr;
15527 if (sym == NULL)
15528 sym = gfc_current_ns->proc_name;
15529 if (sym == NULL)
15530 return 0;
15531 attr = sym->attr;
15533 return attr.flavor == FL_PROCEDURE && attr.elemental;
15537 /* Warn about unused labels. */
15539 static void
15540 warn_unused_fortran_label (gfc_st_label *label)
15542 if (label == NULL)
15543 return;
15545 warn_unused_fortran_label (label->left);
15547 if (label->defined == ST_LABEL_UNKNOWN)
15548 return;
15550 switch (label->referenced)
15552 case ST_LABEL_UNKNOWN:
15553 gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
15554 label->value, &label->where);
15555 break;
15557 case ST_LABEL_BAD_TARGET:
15558 gfc_warning (OPT_Wunused_label,
15559 "Label %d at %L defined but cannot be used",
15560 label->value, &label->where);
15561 break;
15563 default:
15564 break;
15567 warn_unused_fortran_label (label->right);
15571 /* Returns the sequence type of a symbol or sequence. */
15573 static seq_type
15574 sequence_type (gfc_typespec ts)
15576 seq_type result;
15577 gfc_component *c;
15579 switch (ts.type)
15581 case BT_DERIVED:
15583 if (ts.u.derived->components == NULL)
15584 return SEQ_NONDEFAULT;
15586 result = sequence_type (ts.u.derived->components->ts);
15587 for (c = ts.u.derived->components->next; c; c = c->next)
15588 if (sequence_type (c->ts) != result)
15589 return SEQ_MIXED;
15591 return result;
15593 case BT_CHARACTER:
15594 if (ts.kind != gfc_default_character_kind)
15595 return SEQ_NONDEFAULT;
15597 return SEQ_CHARACTER;
15599 case BT_INTEGER:
15600 if (ts.kind != gfc_default_integer_kind)
15601 return SEQ_NONDEFAULT;
15603 return SEQ_NUMERIC;
15605 case BT_REAL:
15606 if (!(ts.kind == gfc_default_real_kind
15607 || ts.kind == gfc_default_double_kind))
15608 return SEQ_NONDEFAULT;
15610 return SEQ_NUMERIC;
15612 case BT_COMPLEX:
15613 if (ts.kind != gfc_default_complex_kind)
15614 return SEQ_NONDEFAULT;
15616 return SEQ_NUMERIC;
15618 case BT_LOGICAL:
15619 if (ts.kind != gfc_default_logical_kind)
15620 return SEQ_NONDEFAULT;
15622 return SEQ_NUMERIC;
15624 default:
15625 return SEQ_NONDEFAULT;
15630 /* Resolve derived type EQUIVALENCE object. */
15632 static bool
15633 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
15635 gfc_component *c = derived->components;
15637 if (!derived)
15638 return true;
15640 /* Shall not be an object of nonsequence derived type. */
15641 if (!derived->attr.sequence)
15643 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
15644 "attribute to be an EQUIVALENCE object", sym->name,
15645 &e->where);
15646 return false;
15649 /* Shall not have allocatable components. */
15650 if (derived->attr.alloc_comp)
15652 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
15653 "components to be an EQUIVALENCE object",sym->name,
15654 &e->where);
15655 return false;
15658 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
15660 gfc_error ("Derived type variable %qs at %L with default "
15661 "initialization cannot be in EQUIVALENCE with a variable "
15662 "in COMMON", sym->name, &e->where);
15663 return false;
15666 for (; c ; c = c->next)
15668 if (gfc_bt_struct (c->ts.type)
15669 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
15670 return false;
15672 /* Shall not be an object of sequence derived type containing a pointer
15673 in the structure. */
15674 if (c->attr.pointer)
15676 gfc_error ("Derived type variable %qs at %L with pointer "
15677 "component(s) cannot be an EQUIVALENCE object",
15678 sym->name, &e->where);
15679 return false;
15682 return true;
15686 /* Resolve equivalence object.
15687 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
15688 an allocatable array, an object of nonsequence derived type, an object of
15689 sequence derived type containing a pointer at any level of component
15690 selection, an automatic object, a function name, an entry name, a result
15691 name, a named constant, a structure component, or a subobject of any of
15692 the preceding objects. A substring shall not have length zero. A
15693 derived type shall not have components with default initialization nor
15694 shall two objects of an equivalence group be initialized.
15695 Either all or none of the objects shall have an protected attribute.
15696 The simple constraints are done in symbol.c(check_conflict) and the rest
15697 are implemented here. */
15699 static void
15700 resolve_equivalence (gfc_equiv *eq)
15702 gfc_symbol *sym;
15703 gfc_symbol *first_sym;
15704 gfc_expr *e;
15705 gfc_ref *r;
15706 locus *last_where = NULL;
15707 seq_type eq_type, last_eq_type;
15708 gfc_typespec *last_ts;
15709 int object, cnt_protected;
15710 const char *msg;
15712 last_ts = &eq->expr->symtree->n.sym->ts;
15714 first_sym = eq->expr->symtree->n.sym;
15716 cnt_protected = 0;
15718 for (object = 1; eq; eq = eq->eq, object++)
15720 e = eq->expr;
15722 e->ts = e->symtree->n.sym->ts;
15723 /* match_varspec might not know yet if it is seeing
15724 array reference or substring reference, as it doesn't
15725 know the types. */
15726 if (e->ref && e->ref->type == REF_ARRAY)
15728 gfc_ref *ref = e->ref;
15729 sym = e->symtree->n.sym;
15731 if (sym->attr.dimension)
15733 ref->u.ar.as = sym->as;
15734 ref = ref->next;
15737 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
15738 if (e->ts.type == BT_CHARACTER
15739 && ref
15740 && ref->type == REF_ARRAY
15741 && ref->u.ar.dimen == 1
15742 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
15743 && ref->u.ar.stride[0] == NULL)
15745 gfc_expr *start = ref->u.ar.start[0];
15746 gfc_expr *end = ref->u.ar.end[0];
15747 void *mem = NULL;
15749 /* Optimize away the (:) reference. */
15750 if (start == NULL && end == NULL)
15752 if (e->ref == ref)
15753 e->ref = ref->next;
15754 else
15755 e->ref->next = ref->next;
15756 mem = ref;
15758 else
15760 ref->type = REF_SUBSTRING;
15761 if (start == NULL)
15762 start = gfc_get_int_expr (gfc_default_integer_kind,
15763 NULL, 1);
15764 ref->u.ss.start = start;
15765 if (end == NULL && e->ts.u.cl)
15766 end = gfc_copy_expr (e->ts.u.cl->length);
15767 ref->u.ss.end = end;
15768 ref->u.ss.length = e->ts.u.cl;
15769 e->ts.u.cl = NULL;
15771 ref = ref->next;
15772 free (mem);
15775 /* Any further ref is an error. */
15776 if (ref)
15778 gcc_assert (ref->type == REF_ARRAY);
15779 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
15780 &ref->u.ar.where);
15781 continue;
15785 if (!gfc_resolve_expr (e))
15786 continue;
15788 sym = e->symtree->n.sym;
15790 if (sym->attr.is_protected)
15791 cnt_protected++;
15792 if (cnt_protected > 0 && cnt_protected != object)
15794 gfc_error ("Either all or none of the objects in the "
15795 "EQUIVALENCE set at %L shall have the "
15796 "PROTECTED attribute",
15797 &e->where);
15798 break;
15801 /* Shall not equivalence common block variables in a PURE procedure. */
15802 if (sym->ns->proc_name
15803 && sym->ns->proc_name->attr.pure
15804 && sym->attr.in_common)
15806 gfc_error ("Common block member %qs at %L cannot be an EQUIVALENCE "
15807 "object in the pure procedure %qs",
15808 sym->name, &e->where, sym->ns->proc_name->name);
15809 break;
15812 /* Shall not be a named constant. */
15813 if (e->expr_type == EXPR_CONSTANT)
15815 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
15816 "object", sym->name, &e->where);
15817 continue;
15820 if (e->ts.type == BT_DERIVED
15821 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
15822 continue;
15824 /* Check that the types correspond correctly:
15825 Note 5.28:
15826 A numeric sequence structure may be equivalenced to another sequence
15827 structure, an object of default integer type, default real type, double
15828 precision real type, default logical type such that components of the
15829 structure ultimately only become associated to objects of the same
15830 kind. A character sequence structure may be equivalenced to an object
15831 of default character kind or another character sequence structure.
15832 Other objects may be equivalenced only to objects of the same type and
15833 kind parameters. */
15835 /* Identical types are unconditionally OK. */
15836 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
15837 goto identical_types;
15839 last_eq_type = sequence_type (*last_ts);
15840 eq_type = sequence_type (sym->ts);
15842 /* Since the pair of objects is not of the same type, mixed or
15843 non-default sequences can be rejected. */
15845 msg = "Sequence %s with mixed components in EQUIVALENCE "
15846 "statement at %L with different type objects";
15847 if ((object ==2
15848 && last_eq_type == SEQ_MIXED
15849 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
15850 || (eq_type == SEQ_MIXED
15851 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
15852 continue;
15854 msg = "Non-default type object or sequence %s in EQUIVALENCE "
15855 "statement at %L with objects of different type";
15856 if ((object ==2
15857 && last_eq_type == SEQ_NONDEFAULT
15858 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
15859 || (eq_type == SEQ_NONDEFAULT
15860 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
15861 continue;
15863 msg ="Non-CHARACTER object %qs in default CHARACTER "
15864 "EQUIVALENCE statement at %L";
15865 if (last_eq_type == SEQ_CHARACTER
15866 && eq_type != SEQ_CHARACTER
15867 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
15868 continue;
15870 msg ="Non-NUMERIC object %qs in default NUMERIC "
15871 "EQUIVALENCE statement at %L";
15872 if (last_eq_type == SEQ_NUMERIC
15873 && eq_type != SEQ_NUMERIC
15874 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
15875 continue;
15877 identical_types:
15878 last_ts =&sym->ts;
15879 last_where = &e->where;
15881 if (!e->ref)
15882 continue;
15884 /* Shall not be an automatic array. */
15885 if (e->ref->type == REF_ARRAY
15886 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
15888 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
15889 "an EQUIVALENCE object", sym->name, &e->where);
15890 continue;
15893 r = e->ref;
15894 while (r)
15896 /* Shall not be a structure component. */
15897 if (r->type == REF_COMPONENT)
15899 gfc_error ("Structure component %qs at %L cannot be an "
15900 "EQUIVALENCE object",
15901 r->u.c.component->name, &e->where);
15902 break;
15905 /* A substring shall not have length zero. */
15906 if (r->type == REF_SUBSTRING)
15908 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
15910 gfc_error ("Substring at %L has length zero",
15911 &r->u.ss.start->where);
15912 break;
15915 r = r->next;
15921 /* Function called by resolve_fntype to flag other symbol used in the
15922 length type parameter specification of function resuls. */
15924 static bool
15925 flag_fn_result_spec (gfc_expr *expr,
15926 gfc_symbol *sym ATTRIBUTE_UNUSED,
15927 int *f ATTRIBUTE_UNUSED)
15929 gfc_namespace *ns;
15930 gfc_symbol *s;
15932 if (expr->expr_type == EXPR_VARIABLE)
15934 s = expr->symtree->n.sym;
15935 for (ns = s->ns; ns; ns = ns->parent)
15936 if (!ns->parent)
15937 break;
15939 if (!s->fn_result_spec
15940 && s->attr.flavor == FL_PARAMETER)
15942 /* Function contained in a module.... */
15943 if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
15945 gfc_symtree *st;
15946 s->fn_result_spec = 1;
15947 /* Make sure that this symbol is translated as a module
15948 variable. */
15949 st = gfc_get_unique_symtree (ns);
15950 st->n.sym = s;
15951 s->refs++;
15953 /* ... which is use associated and called. */
15954 else if (s->attr.use_assoc || s->attr.used_in_submodule
15956 /* External function matched with an interface. */
15957 (s->ns->proc_name
15958 && ((s->ns == ns
15959 && s->ns->proc_name->attr.if_source == IFSRC_DECL)
15960 || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
15961 && s->ns->proc_name->attr.function))
15962 s->fn_result_spec = 1;
15965 return false;
15969 /* Resolve function and ENTRY types, issue diagnostics if needed. */
15971 static void
15972 resolve_fntype (gfc_namespace *ns)
15974 gfc_entry_list *el;
15975 gfc_symbol *sym;
15977 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
15978 return;
15980 /* If there are any entries, ns->proc_name is the entry master
15981 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
15982 if (ns->entries)
15983 sym = ns->entries->sym;
15984 else
15985 sym = ns->proc_name;
15986 if (sym->result == sym
15987 && sym->ts.type == BT_UNKNOWN
15988 && !gfc_set_default_type (sym, 0, NULL)
15989 && !sym->attr.untyped)
15991 gfc_error ("Function %qs at %L has no IMPLICIT type",
15992 sym->name, &sym->declared_at);
15993 sym->attr.untyped = 1;
15996 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
15997 && !sym->attr.contained
15998 && !gfc_check_symbol_access (sym->ts.u.derived)
15999 && gfc_check_symbol_access (sym))
16001 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
16002 "%L of PRIVATE type %qs", sym->name,
16003 &sym->declared_at, sym->ts.u.derived->name);
16006 if (ns->entries)
16007 for (el = ns->entries->next; el; el = el->next)
16009 if (el->sym->result == el->sym
16010 && el->sym->ts.type == BT_UNKNOWN
16011 && !gfc_set_default_type (el->sym, 0, NULL)
16012 && !el->sym->attr.untyped)
16014 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
16015 el->sym->name, &el->sym->declared_at);
16016 el->sym->attr.untyped = 1;
16020 if (sym->ts.type == BT_CHARACTER)
16021 gfc_traverse_expr (sym->ts.u.cl->length, NULL, flag_fn_result_spec, 0);
16025 /* 12.3.2.1.1 Defined operators. */
16027 static bool
16028 check_uop_procedure (gfc_symbol *sym, locus where)
16030 gfc_formal_arglist *formal;
16032 if (!sym->attr.function)
16034 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
16035 sym->name, &where);
16036 return false;
16039 if (sym->ts.type == BT_CHARACTER
16040 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
16041 && !(sym->result && ((sym->result->ts.u.cl
16042 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
16044 gfc_error ("User operator procedure %qs at %L cannot be assumed "
16045 "character length", sym->name, &where);
16046 return false;
16049 formal = gfc_sym_get_dummy_args (sym);
16050 if (!formal || !formal->sym)
16052 gfc_error ("User operator procedure %qs at %L must have at least "
16053 "one argument", sym->name, &where);
16054 return false;
16057 if (formal->sym->attr.intent != INTENT_IN)
16059 gfc_error ("First argument of operator interface at %L must be "
16060 "INTENT(IN)", &where);
16061 return false;
16064 if (formal->sym->attr.optional)
16066 gfc_error ("First argument of operator interface at %L cannot be "
16067 "optional", &where);
16068 return false;
16071 formal = formal->next;
16072 if (!formal || !formal->sym)
16073 return true;
16075 if (formal->sym->attr.intent != INTENT_IN)
16077 gfc_error ("Second argument of operator interface at %L must be "
16078 "INTENT(IN)", &where);
16079 return false;
16082 if (formal->sym->attr.optional)
16084 gfc_error ("Second argument of operator interface at %L cannot be "
16085 "optional", &where);
16086 return false;
16089 if (formal->next)
16091 gfc_error ("Operator interface at %L must have, at most, two "
16092 "arguments", &where);
16093 return false;
16096 return true;
16099 static void
16100 gfc_resolve_uops (gfc_symtree *symtree)
16102 gfc_interface *itr;
16104 if (symtree == NULL)
16105 return;
16107 gfc_resolve_uops (symtree->left);
16108 gfc_resolve_uops (symtree->right);
16110 for (itr = symtree->n.uop->op; itr; itr = itr->next)
16111 check_uop_procedure (itr->sym, itr->sym->declared_at);
16115 /* Examine all of the expressions associated with a program unit,
16116 assign types to all intermediate expressions, make sure that all
16117 assignments are to compatible types and figure out which names
16118 refer to which functions or subroutines. It doesn't check code
16119 block, which is handled by gfc_resolve_code. */
16121 static void
16122 resolve_types (gfc_namespace *ns)
16124 gfc_namespace *n;
16125 gfc_charlen *cl;
16126 gfc_data *d;
16127 gfc_equiv *eq;
16128 gfc_namespace* old_ns = gfc_current_ns;
16130 if (ns->types_resolved)
16131 return;
16133 /* Check that all IMPLICIT types are ok. */
16134 if (!ns->seen_implicit_none)
16136 unsigned letter;
16137 for (letter = 0; letter != GFC_LETTERS; ++letter)
16138 if (ns->set_flag[letter]
16139 && !resolve_typespec_used (&ns->default_type[letter],
16140 &ns->implicit_loc[letter], NULL))
16141 return;
16144 gfc_current_ns = ns;
16146 resolve_entries (ns);
16148 resolve_common_vars (&ns->blank_common, false);
16149 resolve_common_blocks (ns->common_root);
16151 resolve_contained_functions (ns);
16153 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
16154 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
16155 resolve_formal_arglist (ns->proc_name);
16157 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
16159 for (cl = ns->cl_list; cl; cl = cl->next)
16160 resolve_charlen (cl);
16162 gfc_traverse_ns (ns, resolve_symbol);
16164 resolve_fntype (ns);
16166 for (n = ns->contained; n; n = n->sibling)
16168 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
16169 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
16170 "also be PURE", n->proc_name->name,
16171 &n->proc_name->declared_at);
16173 resolve_types (n);
16176 forall_flag = 0;
16177 gfc_do_concurrent_flag = 0;
16178 gfc_check_interfaces (ns);
16180 gfc_traverse_ns (ns, resolve_values);
16182 if (ns->save_all)
16183 gfc_save_all (ns);
16185 iter_stack = NULL;
16186 for (d = ns->data; d; d = d->next)
16187 resolve_data (d);
16189 iter_stack = NULL;
16190 gfc_traverse_ns (ns, gfc_formalize_init_value);
16192 gfc_traverse_ns (ns, gfc_verify_binding_labels);
16194 for (eq = ns->equiv; eq; eq = eq->next)
16195 resolve_equivalence (eq);
16197 /* Warn about unused labels. */
16198 if (warn_unused_label)
16199 warn_unused_fortran_label (ns->st_labels);
16201 gfc_resolve_uops (ns->uop_root);
16203 gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
16205 gfc_resolve_omp_declare_simd (ns);
16207 gfc_resolve_omp_udrs (ns->omp_udr_root);
16209 ns->types_resolved = 1;
16211 gfc_current_ns = old_ns;
16215 /* Call gfc_resolve_code recursively. */
16217 static void
16218 resolve_codes (gfc_namespace *ns)
16220 gfc_namespace *n;
16221 bitmap_obstack old_obstack;
16223 if (ns->resolved == 1)
16224 return;
16226 for (n = ns->contained; n; n = n->sibling)
16227 resolve_codes (n);
16229 gfc_current_ns = ns;
16231 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
16232 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
16233 cs_base = NULL;
16235 /* Set to an out of range value. */
16236 current_entry_id = -1;
16238 old_obstack = labels_obstack;
16239 bitmap_obstack_initialize (&labels_obstack);
16241 gfc_resolve_oacc_declare (ns);
16242 gfc_resolve_code (ns->code, ns);
16244 bitmap_obstack_release (&labels_obstack);
16245 labels_obstack = old_obstack;
16249 /* This function is called after a complete program unit has been compiled.
16250 Its purpose is to examine all of the expressions associated with a program
16251 unit, assign types to all intermediate expressions, make sure that all
16252 assignments are to compatible types and figure out which names refer to
16253 which functions or subroutines. */
16255 void
16256 gfc_resolve (gfc_namespace *ns)
16258 gfc_namespace *old_ns;
16259 code_stack *old_cs_base;
16260 struct gfc_omp_saved_state old_omp_state;
16262 if (ns->resolved)
16263 return;
16265 ns->resolved = -1;
16266 old_ns = gfc_current_ns;
16267 old_cs_base = cs_base;
16269 /* As gfc_resolve can be called during resolution of an OpenMP construct
16270 body, we should clear any state associated to it, so that say NS's
16271 DO loops are not interpreted as OpenMP loops. */
16272 if (!ns->construct_entities)
16273 gfc_omp_save_and_clear_state (&old_omp_state);
16275 resolve_types (ns);
16276 component_assignment_level = 0;
16277 resolve_codes (ns);
16279 gfc_current_ns = old_ns;
16280 cs_base = old_cs_base;
16281 ns->resolved = 1;
16283 gfc_run_passes (ns);
16285 if (!ns->construct_entities)
16286 gfc_omp_restore_state (&old_omp_state);