2013-12-29 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / resolve.c
blob57e6cbb979ed72108cab1fbd4b782d9ef75dfc6a
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2013 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 "flags.h"
25 #include "gfortran.h"
26 #include "obstack.h"
27 #include "bitmap.h"
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
30 #include "data.h"
31 #include "target-memory.h" /* for gfc_simplify_transfer */
32 #include "constructor.h"
34 /* Types used in equivalence statements. */
36 typedef enum seq_type
38 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
40 seq_type;
42 /* Stack to keep track of the nesting of blocks as we move through the
43 code. See resolve_branch() and resolve_code(). */
45 typedef struct code_stack
47 struct gfc_code *head, *current;
48 struct code_stack *prev;
50 /* This bitmap keeps track of the targets valid for a branch from
51 inside this block except for END {IF|SELECT}s of enclosing
52 blocks. */
53 bitmap reachable_labels;
55 code_stack;
57 static code_stack *cs_base = NULL;
60 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
62 static int forall_flag;
63 int gfc_do_concurrent_flag;
65 /* True when we are resolving an expression that is an actual argument to
66 a procedure. */
67 static bool actual_arg = false;
68 /* True when we are resolving an expression that is the first actual argument
69 to a procedure. */
70 static bool first_actual_arg = false;
73 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
75 static int omp_workshare_flag;
77 /* Nonzero if we are processing a formal arglist. The corresponding function
78 resets the flag each time that it is read. */
79 static int formal_arg_flag = 0;
81 /* True if we are resolving a specification expression. */
82 static bool specification_expr = false;
84 /* The id of the last entry seen. */
85 static int current_entry_id;
87 /* We use bitmaps to determine if a branch target is valid. */
88 static bitmap_obstack labels_obstack;
90 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
91 static bool inquiry_argument = false;
94 int
95 gfc_is_formal_arg (void)
97 return formal_arg_flag;
100 /* Is the symbol host associated? */
101 static bool
102 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
104 for (ns = ns->parent; ns; ns = ns->parent)
106 if (sym->ns == ns)
107 return true;
110 return false;
113 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
114 an ABSTRACT derived-type. If where is not NULL, an error message with that
115 locus is printed, optionally using name. */
117 static bool
118 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
120 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
122 if (where)
124 if (name)
125 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
126 name, where, ts->u.derived->name);
127 else
128 gfc_error ("ABSTRACT type '%s' used at %L",
129 ts->u.derived->name, where);
132 return false;
135 return true;
139 static bool
140 check_proc_interface (gfc_symbol *ifc, locus *where)
142 /* Several checks for F08:C1216. */
143 if (ifc->attr.procedure)
145 gfc_error ("Interface '%s' at %L is declared "
146 "in a later PROCEDURE statement", ifc->name, where);
147 return false;
149 if (ifc->generic)
151 /* For generic interfaces, check if there is
152 a specific procedure with the same name. */
153 gfc_interface *gen = ifc->generic;
154 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
155 gen = gen->next;
156 if (!gen)
158 gfc_error ("Interface '%s' at %L may not be generic",
159 ifc->name, where);
160 return false;
163 if (ifc->attr.proc == PROC_ST_FUNCTION)
165 gfc_error ("Interface '%s' at %L may not be a statement function",
166 ifc->name, where);
167 return false;
169 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
170 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
171 ifc->attr.intrinsic = 1;
172 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
174 gfc_error ("Intrinsic procedure '%s' not allowed in "
175 "PROCEDURE statement at %L", ifc->name, where);
176 return false;
178 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
180 gfc_error ("Interface '%s' at %L must be explicit", ifc->name, where);
181 return false;
183 return true;
187 static void resolve_symbol (gfc_symbol *sym);
190 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
192 static bool
193 resolve_procedure_interface (gfc_symbol *sym)
195 gfc_symbol *ifc = sym->ts.interface;
197 if (!ifc)
198 return true;
200 if (ifc == sym)
202 gfc_error ("PROCEDURE '%s' at %L may not be used as its own interface",
203 sym->name, &sym->declared_at);
204 return false;
206 if (!check_proc_interface (ifc, &sym->declared_at))
207 return false;
209 if (ifc->attr.if_source || ifc->attr.intrinsic)
211 /* Resolve interface and copy attributes. */
212 resolve_symbol (ifc);
213 if (ifc->attr.intrinsic)
214 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
216 if (ifc->result)
218 sym->ts = ifc->result->ts;
219 sym->result = sym;
221 else
222 sym->ts = ifc->ts;
223 sym->ts.interface = ifc;
224 sym->attr.function = ifc->attr.function;
225 sym->attr.subroutine = ifc->attr.subroutine;
227 sym->attr.allocatable = ifc->attr.allocatable;
228 sym->attr.pointer = ifc->attr.pointer;
229 sym->attr.pure = ifc->attr.pure;
230 sym->attr.elemental = ifc->attr.elemental;
231 sym->attr.dimension = ifc->attr.dimension;
232 sym->attr.contiguous = ifc->attr.contiguous;
233 sym->attr.recursive = ifc->attr.recursive;
234 sym->attr.always_explicit = ifc->attr.always_explicit;
235 sym->attr.ext_attr |= ifc->attr.ext_attr;
236 sym->attr.is_bind_c = ifc->attr.is_bind_c;
237 sym->attr.class_ok = ifc->attr.class_ok;
238 /* Copy array spec. */
239 sym->as = gfc_copy_array_spec (ifc->as);
240 /* Copy char length. */
241 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
243 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
244 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
245 && !gfc_resolve_expr (sym->ts.u.cl->length))
246 return false;
250 return true;
254 /* Resolve types of formal argument lists. These have to be done early so that
255 the formal argument lists of module procedures can be copied to the
256 containing module before the individual procedures are resolved
257 individually. We also resolve argument lists of procedures in interface
258 blocks because they are self-contained scoping units.
260 Since a dummy argument cannot be a non-dummy procedure, the only
261 resort left for untyped names are the IMPLICIT types. */
263 static void
264 resolve_formal_arglist (gfc_symbol *proc)
266 gfc_formal_arglist *f;
267 gfc_symbol *sym;
268 bool saved_specification_expr;
269 int i;
271 if (proc->result != NULL)
272 sym = proc->result;
273 else
274 sym = proc;
276 if (gfc_elemental (proc)
277 || sym->attr.pointer || sym->attr.allocatable
278 || (sym->as && sym->as->rank != 0))
280 proc->attr.always_explicit = 1;
281 sym->attr.always_explicit = 1;
284 formal_arg_flag = 1;
286 for (f = proc->formal; f; f = f->next)
288 gfc_array_spec *as;
290 sym = f->sym;
292 if (sym == NULL)
294 /* Alternate return placeholder. */
295 if (gfc_elemental (proc))
296 gfc_error ("Alternate return specifier in elemental subroutine "
297 "'%s' at %L is not allowed", proc->name,
298 &proc->declared_at);
299 if (proc->attr.function)
300 gfc_error ("Alternate return specifier in function "
301 "'%s' at %L is not allowed", proc->name,
302 &proc->declared_at);
303 continue;
305 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
306 && !resolve_procedure_interface (sym))
307 return;
309 if (strcmp (proc->name, sym->name) == 0)
311 gfc_error ("Self-referential argument "
312 "'%s' at %L is not allowed", sym->name,
313 &proc->declared_at);
314 return;
317 if (sym->attr.if_source != IFSRC_UNKNOWN)
318 resolve_formal_arglist (sym);
320 if (sym->attr.subroutine || sym->attr.external)
322 if (sym->attr.flavor == FL_UNKNOWN)
323 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
325 else
327 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
328 && (!sym->attr.function || sym->result == sym))
329 gfc_set_default_type (sym, 1, sym->ns);
332 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
333 ? CLASS_DATA (sym)->as : sym->as;
335 saved_specification_expr = specification_expr;
336 specification_expr = true;
337 gfc_resolve_array_spec (as, 0);
338 specification_expr = saved_specification_expr;
340 /* We can't tell if an array with dimension (:) is assumed or deferred
341 shape until we know if it has the pointer or allocatable attributes.
343 if (as && as->rank > 0 && as->type == AS_DEFERRED
344 && ((sym->ts.type != BT_CLASS
345 && !(sym->attr.pointer || sym->attr.allocatable))
346 || (sym->ts.type == BT_CLASS
347 && !(CLASS_DATA (sym)->attr.class_pointer
348 || CLASS_DATA (sym)->attr.allocatable)))
349 && sym->attr.flavor != FL_PROCEDURE)
351 as->type = AS_ASSUMED_SHAPE;
352 for (i = 0; i < as->rank; i++)
353 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
356 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
357 || (as && as->type == AS_ASSUMED_RANK)
358 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
359 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
360 && (CLASS_DATA (sym)->attr.class_pointer
361 || CLASS_DATA (sym)->attr.allocatable
362 || CLASS_DATA (sym)->attr.target))
363 || sym->attr.optional)
365 proc->attr.always_explicit = 1;
366 if (proc->result)
367 proc->result->attr.always_explicit = 1;
370 /* If the flavor is unknown at this point, it has to be a variable.
371 A procedure specification would have already set the type. */
373 if (sym->attr.flavor == FL_UNKNOWN)
374 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
376 if (gfc_pure (proc))
378 if (sym->attr.flavor == FL_PROCEDURE)
380 /* F08:C1279. */
381 if (!gfc_pure (sym))
383 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
384 "also be PURE", sym->name, &sym->declared_at);
385 continue;
388 else if (!sym->attr.pointer)
390 if (proc->attr.function && sym->attr.intent != INTENT_IN)
392 if (sym->attr.value)
393 gfc_notify_std (GFC_STD_F2008, "Argument '%s'"
394 " of pure function '%s' at %L with VALUE "
395 "attribute but without INTENT(IN)",
396 sym->name, proc->name, &sym->declared_at);
397 else
398 gfc_error ("Argument '%s' of pure function '%s' at %L must "
399 "be INTENT(IN) or VALUE", sym->name, proc->name,
400 &sym->declared_at);
403 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
405 if (sym->attr.value)
406 gfc_notify_std (GFC_STD_F2008, "Argument '%s'"
407 " of pure subroutine '%s' at %L with VALUE "
408 "attribute but without INTENT", sym->name,
409 proc->name, &sym->declared_at);
410 else
411 gfc_error ("Argument '%s' of pure subroutine '%s' at %L "
412 "must have its INTENT specified or have the "
413 "VALUE attribute", sym->name, proc->name,
414 &sym->declared_at);
419 if (proc->attr.implicit_pure)
421 if (sym->attr.flavor == FL_PROCEDURE)
423 if (!gfc_pure (sym))
424 proc->attr.implicit_pure = 0;
426 else if (!sym->attr.pointer)
428 if (proc->attr.function && sym->attr.intent != INTENT_IN
429 && !sym->value)
430 proc->attr.implicit_pure = 0;
432 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
433 && !sym->value)
434 proc->attr.implicit_pure = 0;
438 if (gfc_elemental (proc))
440 /* F08:C1289. */
441 if (sym->attr.codimension
442 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
443 && CLASS_DATA (sym)->attr.codimension))
445 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
446 "procedure", sym->name, &sym->declared_at);
447 continue;
450 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
451 && CLASS_DATA (sym)->as))
453 gfc_error ("Argument '%s' of elemental procedure at %L must "
454 "be scalar", sym->name, &sym->declared_at);
455 continue;
458 if (sym->attr.allocatable
459 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
460 && CLASS_DATA (sym)->attr.allocatable))
462 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
463 "have the ALLOCATABLE attribute", sym->name,
464 &sym->declared_at);
465 continue;
468 if (sym->attr.pointer
469 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
470 && CLASS_DATA (sym)->attr.class_pointer))
472 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
473 "have the POINTER attribute", sym->name,
474 &sym->declared_at);
475 continue;
478 if (sym->attr.flavor == FL_PROCEDURE)
480 gfc_error ("Dummy procedure '%s' not allowed in elemental "
481 "procedure '%s' at %L", sym->name, proc->name,
482 &sym->declared_at);
483 continue;
486 /* Fortran 2008 Corrigendum 1, C1290a. */
487 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
489 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
490 "have its INTENT specified or have the VALUE "
491 "attribute", sym->name, proc->name,
492 &sym->declared_at);
493 continue;
497 /* Each dummy shall be specified to be scalar. */
498 if (proc->attr.proc == PROC_ST_FUNCTION)
500 if (sym->as != NULL)
502 gfc_error ("Argument '%s' of statement function at %L must "
503 "be scalar", sym->name, &sym->declared_at);
504 continue;
507 if (sym->ts.type == BT_CHARACTER)
509 gfc_charlen *cl = sym->ts.u.cl;
510 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
512 gfc_error ("Character-valued argument '%s' of statement "
513 "function at %L must have constant length",
514 sym->name, &sym->declared_at);
515 continue;
520 formal_arg_flag = 0;
524 /* Work function called when searching for symbols that have argument lists
525 associated with them. */
527 static void
528 find_arglists (gfc_symbol *sym)
530 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
531 || sym->attr.flavor == FL_DERIVED || sym->attr.intrinsic)
532 return;
534 resolve_formal_arglist (sym);
538 /* Given a namespace, resolve all formal argument lists within the namespace.
541 static void
542 resolve_formal_arglists (gfc_namespace *ns)
544 if (ns == NULL)
545 return;
547 gfc_traverse_ns (ns, find_arglists);
551 static void
552 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
554 bool t;
556 /* If this namespace is not a function or an entry master function,
557 ignore it. */
558 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
559 || sym->attr.entry_master)
560 return;
562 /* Try to find out of what the return type is. */
563 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
565 t = gfc_set_default_type (sym->result, 0, ns);
567 if (!t && !sym->result->attr.untyped)
569 if (sym->result == sym)
570 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
571 sym->name, &sym->declared_at);
572 else if (!sym->result->attr.proc_pointer)
573 gfc_error ("Result '%s' of contained function '%s' at %L has "
574 "no IMPLICIT type", sym->result->name, sym->name,
575 &sym->result->declared_at);
576 sym->result->attr.untyped = 1;
580 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
581 type, lists the only ways a character length value of * can be used:
582 dummy arguments of procedures, named constants, and function results
583 in external functions. Internal function results and results of module
584 procedures are not on this list, ergo, not permitted. */
586 if (sym->result->ts.type == BT_CHARACTER)
588 gfc_charlen *cl = sym->result->ts.u.cl;
589 if ((!cl || !cl->length) && !sym->result->ts.deferred)
591 /* See if this is a module-procedure and adapt error message
592 accordingly. */
593 bool module_proc;
594 gcc_assert (ns->parent && ns->parent->proc_name);
595 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
597 gfc_error ("Character-valued %s '%s' at %L must not be"
598 " assumed length",
599 module_proc ? _("module procedure")
600 : _("internal function"),
601 sym->name, &sym->declared_at);
607 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
608 introduce duplicates. */
610 static void
611 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
613 gfc_formal_arglist *f, *new_arglist;
614 gfc_symbol *new_sym;
616 for (; new_args != NULL; new_args = new_args->next)
618 new_sym = new_args->sym;
619 /* See if this arg is already in the formal argument list. */
620 for (f = proc->formal; f; f = f->next)
622 if (new_sym == f->sym)
623 break;
626 if (f)
627 continue;
629 /* Add a new argument. Argument order is not important. */
630 new_arglist = gfc_get_formal_arglist ();
631 new_arglist->sym = new_sym;
632 new_arglist->next = proc->formal;
633 proc->formal = new_arglist;
638 /* Flag the arguments that are not present in all entries. */
640 static void
641 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
643 gfc_formal_arglist *f, *head;
644 head = new_args;
646 for (f = proc->formal; f; f = f->next)
648 if (f->sym == NULL)
649 continue;
651 for (new_args = head; new_args; new_args = new_args->next)
653 if (new_args->sym == f->sym)
654 break;
657 if (new_args)
658 continue;
660 f->sym->attr.not_always_present = 1;
665 /* Resolve alternate entry points. If a symbol has multiple entry points we
666 create a new master symbol for the main routine, and turn the existing
667 symbol into an entry point. */
669 static void
670 resolve_entries (gfc_namespace *ns)
672 gfc_namespace *old_ns;
673 gfc_code *c;
674 gfc_symbol *proc;
675 gfc_entry_list *el;
676 char name[GFC_MAX_SYMBOL_LEN + 1];
677 static int master_count = 0;
679 if (ns->proc_name == NULL)
680 return;
682 /* No need to do anything if this procedure doesn't have alternate entry
683 points. */
684 if (!ns->entries)
685 return;
687 /* We may already have resolved alternate entry points. */
688 if (ns->proc_name->attr.entry_master)
689 return;
691 /* If this isn't a procedure something has gone horribly wrong. */
692 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
694 /* Remember the current namespace. */
695 old_ns = gfc_current_ns;
697 gfc_current_ns = ns;
699 /* Add the main entry point to the list of entry points. */
700 el = gfc_get_entry_list ();
701 el->sym = ns->proc_name;
702 el->id = 0;
703 el->next = ns->entries;
704 ns->entries = el;
705 ns->proc_name->attr.entry = 1;
707 /* If it is a module function, it needs to be in the right namespace
708 so that gfc_get_fake_result_decl can gather up the results. The
709 need for this arose in get_proc_name, where these beasts were
710 left in their own namespace, to keep prior references linked to
711 the entry declaration.*/
712 if (ns->proc_name->attr.function
713 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
714 el->sym->ns = ns;
716 /* Do the same for entries where the master is not a module
717 procedure. These are retained in the module namespace because
718 of the module procedure declaration. */
719 for (el = el->next; el; el = el->next)
720 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
721 && el->sym->attr.mod_proc)
722 el->sym->ns = ns;
723 el = ns->entries;
725 /* Add an entry statement for it. */
726 c = gfc_get_code (EXEC_ENTRY);
727 c->ext.entry = el;
728 c->next = ns->code;
729 ns->code = c;
731 /* Create a new symbol for the master function. */
732 /* Give the internal function a unique name (within this file).
733 Also include the function name so the user has some hope of figuring
734 out what is going on. */
735 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
736 master_count++, ns->proc_name->name);
737 gfc_get_ha_symbol (name, &proc);
738 gcc_assert (proc != NULL);
740 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
741 if (ns->proc_name->attr.subroutine)
742 gfc_add_subroutine (&proc->attr, proc->name, NULL);
743 else
745 gfc_symbol *sym;
746 gfc_typespec *ts, *fts;
747 gfc_array_spec *as, *fas;
748 gfc_add_function (&proc->attr, proc->name, NULL);
749 proc->result = proc;
750 fas = ns->entries->sym->as;
751 fas = fas ? fas : ns->entries->sym->result->as;
752 fts = &ns->entries->sym->result->ts;
753 if (fts->type == BT_UNKNOWN)
754 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
755 for (el = ns->entries->next; el; el = el->next)
757 ts = &el->sym->result->ts;
758 as = el->sym->as;
759 as = as ? as : el->sym->result->as;
760 if (ts->type == BT_UNKNOWN)
761 ts = gfc_get_default_type (el->sym->result->name, NULL);
763 if (! gfc_compare_types (ts, fts)
764 || (el->sym->result->attr.dimension
765 != ns->entries->sym->result->attr.dimension)
766 || (el->sym->result->attr.pointer
767 != ns->entries->sym->result->attr.pointer))
768 break;
769 else if (as && fas && ns->entries->sym->result != el->sym->result
770 && gfc_compare_array_spec (as, fas) == 0)
771 gfc_error ("Function %s at %L has entries with mismatched "
772 "array specifications", ns->entries->sym->name,
773 &ns->entries->sym->declared_at);
774 /* The characteristics need to match and thus both need to have
775 the same string length, i.e. both len=*, or both len=4.
776 Having both len=<variable> is also possible, but difficult to
777 check at compile time. */
778 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
779 && (((ts->u.cl->length && !fts->u.cl->length)
780 ||(!ts->u.cl->length && fts->u.cl->length))
781 || (ts->u.cl->length
782 && ts->u.cl->length->expr_type
783 != fts->u.cl->length->expr_type)
784 || (ts->u.cl->length
785 && ts->u.cl->length->expr_type == EXPR_CONSTANT
786 && mpz_cmp (ts->u.cl->length->value.integer,
787 fts->u.cl->length->value.integer) != 0)))
788 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
789 "entries returning variables of different "
790 "string lengths", ns->entries->sym->name,
791 &ns->entries->sym->declared_at);
794 if (el == NULL)
796 sym = ns->entries->sym->result;
797 /* All result types the same. */
798 proc->ts = *fts;
799 if (sym->attr.dimension)
800 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
801 if (sym->attr.pointer)
802 gfc_add_pointer (&proc->attr, NULL);
804 else
806 /* Otherwise the result will be passed through a union by
807 reference. */
808 proc->attr.mixed_entry_master = 1;
809 for (el = ns->entries; el; el = el->next)
811 sym = el->sym->result;
812 if (sym->attr.dimension)
814 if (el == ns->entries)
815 gfc_error ("FUNCTION result %s can't be an array in "
816 "FUNCTION %s at %L", sym->name,
817 ns->entries->sym->name, &sym->declared_at);
818 else
819 gfc_error ("ENTRY result %s can't be an array in "
820 "FUNCTION %s at %L", sym->name,
821 ns->entries->sym->name, &sym->declared_at);
823 else if (sym->attr.pointer)
825 if (el == ns->entries)
826 gfc_error ("FUNCTION result %s can't be a POINTER in "
827 "FUNCTION %s at %L", sym->name,
828 ns->entries->sym->name, &sym->declared_at);
829 else
830 gfc_error ("ENTRY result %s can't be a POINTER in "
831 "FUNCTION %s at %L", sym->name,
832 ns->entries->sym->name, &sym->declared_at);
834 else
836 ts = &sym->ts;
837 if (ts->type == BT_UNKNOWN)
838 ts = gfc_get_default_type (sym->name, NULL);
839 switch (ts->type)
841 case BT_INTEGER:
842 if (ts->kind == gfc_default_integer_kind)
843 sym = NULL;
844 break;
845 case BT_REAL:
846 if (ts->kind == gfc_default_real_kind
847 || ts->kind == gfc_default_double_kind)
848 sym = NULL;
849 break;
850 case BT_COMPLEX:
851 if (ts->kind == gfc_default_complex_kind)
852 sym = NULL;
853 break;
854 case BT_LOGICAL:
855 if (ts->kind == gfc_default_logical_kind)
856 sym = NULL;
857 break;
858 case BT_UNKNOWN:
859 /* We will issue error elsewhere. */
860 sym = NULL;
861 break;
862 default:
863 break;
865 if (sym)
867 if (el == ns->entries)
868 gfc_error ("FUNCTION result %s can't be of type %s "
869 "in FUNCTION %s at %L", sym->name,
870 gfc_typename (ts), ns->entries->sym->name,
871 &sym->declared_at);
872 else
873 gfc_error ("ENTRY result %s can't be of type %s "
874 "in FUNCTION %s at %L", sym->name,
875 gfc_typename (ts), ns->entries->sym->name,
876 &sym->declared_at);
882 proc->attr.access = ACCESS_PRIVATE;
883 proc->attr.entry_master = 1;
885 /* Merge all the entry point arguments. */
886 for (el = ns->entries; el; el = el->next)
887 merge_argument_lists (proc, el->sym->formal);
889 /* Check the master formal arguments for any that are not
890 present in all entry points. */
891 for (el = ns->entries; el; el = el->next)
892 check_argument_lists (proc, el->sym->formal);
894 /* Use the master function for the function body. */
895 ns->proc_name = proc;
897 /* Finalize the new symbols. */
898 gfc_commit_symbols ();
900 /* Restore the original namespace. */
901 gfc_current_ns = old_ns;
905 /* Resolve common variables. */
906 static void
907 resolve_common_vars (gfc_symbol *sym, bool named_common)
909 gfc_symbol *csym = sym;
911 for (; csym; csym = csym->common_next)
913 if (csym->value || csym->attr.data)
915 if (!csym->ns->is_block_data)
916 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
917 "but only in BLOCK DATA initialization is "
918 "allowed", csym->name, &csym->declared_at);
919 else if (!named_common)
920 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
921 "in a blank COMMON but initialization is only "
922 "allowed in named common blocks", csym->name,
923 &csym->declared_at);
926 if (UNLIMITED_POLY (csym))
927 gfc_error_now ("'%s' in cannot appear in COMMON at %L "
928 "[F2008:C5100]", csym->name, &csym->declared_at);
930 if (csym->ts.type != BT_DERIVED)
931 continue;
933 if (!(csym->ts.u.derived->attr.sequence
934 || csym->ts.u.derived->attr.is_bind_c))
935 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
936 "has neither the SEQUENCE nor the BIND(C) "
937 "attribute", csym->name, &csym->declared_at);
938 if (csym->ts.u.derived->attr.alloc_comp)
939 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
940 "has an ultimate component that is "
941 "allocatable", csym->name, &csym->declared_at);
942 if (gfc_has_default_initializer (csym->ts.u.derived))
943 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
944 "may not have default initializer", csym->name,
945 &csym->declared_at);
947 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
948 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
952 /* Resolve common blocks. */
953 static void
954 resolve_common_blocks (gfc_symtree *common_root)
956 gfc_symbol *sym;
957 gfc_gsymbol * gsym;
959 if (common_root == NULL)
960 return;
962 if (common_root->left)
963 resolve_common_blocks (common_root->left);
964 if (common_root->right)
965 resolve_common_blocks (common_root->right);
967 resolve_common_vars (common_root->n.common->head, true);
969 /* The common name is a global name - in Fortran 2003 also if it has a
970 C binding name, since Fortran 2008 only the C binding name is a global
971 identifier. */
972 if (!common_root->n.common->binding_label
973 || gfc_notification_std (GFC_STD_F2008))
975 gsym = gfc_find_gsymbol (gfc_gsym_root,
976 common_root->n.common->name);
978 if (gsym && gfc_notification_std (GFC_STD_F2008)
979 && gsym->type == GSYM_COMMON
980 && ((common_root->n.common->binding_label
981 && (!gsym->binding_label
982 || strcmp (common_root->n.common->binding_label,
983 gsym->binding_label) != 0))
984 || (!common_root->n.common->binding_label
985 && gsym->binding_label)))
987 gfc_error ("In Fortran 2003 COMMON '%s' block at %L is a global "
988 "identifier and must thus have the same binding name "
989 "as the same-named COMMON block at %L: %s vs %s",
990 common_root->n.common->name, &common_root->n.common->where,
991 &gsym->where,
992 common_root->n.common->binding_label
993 ? common_root->n.common->binding_label : "(blank)",
994 gsym->binding_label ? gsym->binding_label : "(blank)");
995 return;
998 if (gsym && gsym->type != GSYM_COMMON
999 && !common_root->n.common->binding_label)
1001 gfc_error ("COMMON block '%s' at %L uses the same global identifier "
1002 "as entity at %L",
1003 common_root->n.common->name, &common_root->n.common->where,
1004 &gsym->where);
1005 return;
1007 if (gsym && gsym->type != GSYM_COMMON)
1009 gfc_error ("Fortran 2008: COMMON block '%s' with binding label at "
1010 "%L sharing the identifier with global non-COMMON-block "
1011 "entity at %L", common_root->n.common->name,
1012 &common_root->n.common->where, &gsym->where);
1013 return;
1015 if (!gsym)
1017 gsym = gfc_get_gsymbol (common_root->n.common->name);
1018 gsym->type = GSYM_COMMON;
1019 gsym->where = common_root->n.common->where;
1020 gsym->defined = 1;
1022 gsym->used = 1;
1025 if (common_root->n.common->binding_label)
1027 gsym = gfc_find_gsymbol (gfc_gsym_root,
1028 common_root->n.common->binding_label);
1029 if (gsym && gsym->type != GSYM_COMMON)
1031 gfc_error ("COMMON block at %L with binding label %s uses the same "
1032 "global identifier as entity at %L",
1033 &common_root->n.common->where,
1034 common_root->n.common->binding_label, &gsym->where);
1035 return;
1037 if (!gsym)
1039 gsym = gfc_get_gsymbol (common_root->n.common->binding_label);
1040 gsym->type = GSYM_COMMON;
1041 gsym->where = common_root->n.common->where;
1042 gsym->defined = 1;
1044 gsym->used = 1;
1047 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1048 if (sym == NULL)
1049 return;
1051 if (sym->attr.flavor == FL_PARAMETER)
1052 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
1053 sym->name, &common_root->n.common->where, &sym->declared_at);
1055 if (sym->attr.external)
1056 gfc_error ("COMMON block '%s' at %L can not have the EXTERNAL attribute",
1057 sym->name, &common_root->n.common->where);
1059 if (sym->attr.intrinsic)
1060 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
1061 sym->name, &common_root->n.common->where);
1062 else if (sym->attr.result
1063 || gfc_is_function_return_value (sym, gfc_current_ns))
1064 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
1065 "that is also a function result", sym->name,
1066 &common_root->n.common->where);
1067 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1068 && sym->attr.proc != PROC_ST_FUNCTION)
1069 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
1070 "that is also a global procedure", sym->name,
1071 &common_root->n.common->where);
1075 /* Resolve contained function types. Because contained functions can call one
1076 another, they have to be worked out before any of the contained procedures
1077 can be resolved.
1079 The good news is that if a function doesn't already have a type, the only
1080 way it can get one is through an IMPLICIT type or a RESULT variable, because
1081 by definition contained functions are contained namespace they're contained
1082 in, not in a sibling or parent namespace. */
1084 static void
1085 resolve_contained_functions (gfc_namespace *ns)
1087 gfc_namespace *child;
1088 gfc_entry_list *el;
1090 resolve_formal_arglists (ns);
1092 for (child = ns->contained; child; child = child->sibling)
1094 /* Resolve alternate entry points first. */
1095 resolve_entries (child);
1097 /* Then check function return types. */
1098 resolve_contained_fntype (child->proc_name, child);
1099 for (el = child->entries; el; el = el->next)
1100 resolve_contained_fntype (el->sym, child);
1105 static bool resolve_fl_derived0 (gfc_symbol *sym);
1108 /* Resolve all of the elements of a structure constructor and make sure that
1109 the types are correct. The 'init' flag indicates that the given
1110 constructor is an initializer. */
1112 static bool
1113 resolve_structure_cons (gfc_expr *expr, int init)
1115 gfc_constructor *cons;
1116 gfc_component *comp;
1117 bool t;
1118 symbol_attribute a;
1120 t = true;
1122 if (expr->ts.type == BT_DERIVED)
1123 resolve_fl_derived0 (expr->ts.u.derived);
1125 cons = gfc_constructor_first (expr->value.constructor);
1127 /* A constructor may have references if it is the result of substituting a
1128 parameter variable. In this case we just pull out the component we
1129 want. */
1130 if (expr->ref)
1131 comp = expr->ref->u.c.sym->components;
1132 else
1133 comp = expr->ts.u.derived->components;
1135 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1137 int rank;
1139 if (!cons->expr)
1140 continue;
1142 if (!gfc_resolve_expr (cons->expr))
1144 t = false;
1145 continue;
1148 rank = comp->as ? comp->as->rank : 0;
1149 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1150 && (comp->attr.allocatable || cons->expr->rank))
1152 gfc_error ("The rank of the element in the structure "
1153 "constructor at %L does not match that of the "
1154 "component (%d/%d)", &cons->expr->where,
1155 cons->expr->rank, rank);
1156 t = false;
1159 /* If we don't have the right type, try to convert it. */
1161 if (!comp->attr.proc_pointer &&
1162 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1164 if (strcmp (comp->name, "_extends") == 0)
1166 /* Can afford to be brutal with the _extends initializer.
1167 The derived type can get lost because it is PRIVATE
1168 but it is not usage constrained by the standard. */
1169 cons->expr->ts = comp->ts;
1171 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1173 gfc_error ("The element in the structure constructor at %L, "
1174 "for pointer component '%s', is %s but should be %s",
1175 &cons->expr->where, comp->name,
1176 gfc_basic_typename (cons->expr->ts.type),
1177 gfc_basic_typename (comp->ts.type));
1178 t = false;
1180 else
1182 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1183 if (t)
1184 t = t2;
1188 /* For strings, the length of the constructor should be the same as
1189 the one of the structure, ensure this if the lengths are known at
1190 compile time and when we are dealing with PARAMETER or structure
1191 constructors. */
1192 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1193 && comp->ts.u.cl->length
1194 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1195 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1196 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1197 && cons->expr->rank != 0
1198 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1199 comp->ts.u.cl->length->value.integer) != 0)
1201 if (cons->expr->expr_type == EXPR_VARIABLE
1202 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1204 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1205 to make use of the gfc_resolve_character_array_constructor
1206 machinery. The expression is later simplified away to
1207 an array of string literals. */
1208 gfc_expr *para = cons->expr;
1209 cons->expr = gfc_get_expr ();
1210 cons->expr->ts = para->ts;
1211 cons->expr->where = para->where;
1212 cons->expr->expr_type = EXPR_ARRAY;
1213 cons->expr->rank = para->rank;
1214 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1215 gfc_constructor_append_expr (&cons->expr->value.constructor,
1216 para, &cons->expr->where);
1218 if (cons->expr->expr_type == EXPR_ARRAY)
1220 gfc_constructor *p;
1221 p = gfc_constructor_first (cons->expr->value.constructor);
1222 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1224 gfc_charlen *cl, *cl2;
1226 cl2 = NULL;
1227 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1229 if (cl == cons->expr->ts.u.cl)
1230 break;
1231 cl2 = cl;
1234 gcc_assert (cl);
1236 if (cl2)
1237 cl2->next = cl->next;
1239 gfc_free_expr (cl->length);
1240 free (cl);
1243 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1244 cons->expr->ts.u.cl->length_from_typespec = true;
1245 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1246 gfc_resolve_character_array_constructor (cons->expr);
1250 if (cons->expr->expr_type == EXPR_NULL
1251 && !(comp->attr.pointer || comp->attr.allocatable
1252 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1253 || (comp->ts.type == BT_CLASS
1254 && (CLASS_DATA (comp)->attr.class_pointer
1255 || CLASS_DATA (comp)->attr.allocatable))))
1257 t = false;
1258 gfc_error ("The NULL in the structure constructor at %L is "
1259 "being applied to component '%s', which is neither "
1260 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1261 comp->name);
1264 if (comp->attr.proc_pointer && comp->ts.interface)
1266 /* Check procedure pointer interface. */
1267 gfc_symbol *s2 = NULL;
1268 gfc_component *c2;
1269 const char *name;
1270 char err[200];
1272 c2 = gfc_get_proc_ptr_comp (cons->expr);
1273 if (c2)
1275 s2 = c2->ts.interface;
1276 name = c2->name;
1278 else if (cons->expr->expr_type == EXPR_FUNCTION)
1280 s2 = cons->expr->symtree->n.sym->result;
1281 name = cons->expr->symtree->n.sym->result->name;
1283 else if (cons->expr->expr_type != EXPR_NULL)
1285 s2 = cons->expr->symtree->n.sym;
1286 name = cons->expr->symtree->n.sym->name;
1289 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1290 err, sizeof (err), NULL, NULL))
1292 gfc_error ("Interface mismatch for procedure-pointer component "
1293 "'%s' in structure constructor at %L: %s",
1294 comp->name, &cons->expr->where, err);
1295 return false;
1299 if (!comp->attr.pointer || comp->attr.proc_pointer
1300 || cons->expr->expr_type == EXPR_NULL)
1301 continue;
1303 a = gfc_expr_attr (cons->expr);
1305 if (!a.pointer && !a.target)
1307 t = false;
1308 gfc_error ("The element in the structure constructor at %L, "
1309 "for pointer component '%s' should be a POINTER or "
1310 "a TARGET", &cons->expr->where, comp->name);
1313 if (init)
1315 /* F08:C461. Additional checks for pointer initialization. */
1316 if (a.allocatable)
1318 t = false;
1319 gfc_error ("Pointer initialization target at %L "
1320 "must not be ALLOCATABLE ", &cons->expr->where);
1322 if (!a.save)
1324 t = false;
1325 gfc_error ("Pointer initialization target at %L "
1326 "must have the SAVE attribute", &cons->expr->where);
1330 /* F2003, C1272 (3). */
1331 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
1332 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1333 || gfc_is_coindexed (cons->expr)))
1335 t = false;
1336 gfc_error ("Invalid expression in the structure constructor for "
1337 "pointer component '%s' at %L in PURE procedure",
1338 comp->name, &cons->expr->where);
1341 if (gfc_implicit_pure (NULL)
1342 && cons->expr->expr_type == EXPR_VARIABLE
1343 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1344 || gfc_is_coindexed (cons->expr)))
1345 gfc_current_ns->proc_name->attr.implicit_pure = 0;
1349 return t;
1353 /****************** Expression name resolution ******************/
1355 /* Returns 0 if a symbol was not declared with a type or
1356 attribute declaration statement, nonzero otherwise. */
1358 static int
1359 was_declared (gfc_symbol *sym)
1361 symbol_attribute a;
1363 a = sym->attr;
1365 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1366 return 1;
1368 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1369 || a.optional || a.pointer || a.save || a.target || a.volatile_
1370 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1371 || a.asynchronous || a.codimension)
1372 return 1;
1374 return 0;
1378 /* Determine if a symbol is generic or not. */
1380 static int
1381 generic_sym (gfc_symbol *sym)
1383 gfc_symbol *s;
1385 if (sym->attr.generic ||
1386 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1387 return 1;
1389 if (was_declared (sym) || sym->ns->parent == NULL)
1390 return 0;
1392 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1394 if (s != NULL)
1396 if (s == sym)
1397 return 0;
1398 else
1399 return generic_sym (s);
1402 return 0;
1406 /* Determine if a symbol is specific or not. */
1408 static int
1409 specific_sym (gfc_symbol *sym)
1411 gfc_symbol *s;
1413 if (sym->attr.if_source == IFSRC_IFBODY
1414 || sym->attr.proc == PROC_MODULE
1415 || sym->attr.proc == PROC_INTERNAL
1416 || sym->attr.proc == PROC_ST_FUNCTION
1417 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1418 || sym->attr.external)
1419 return 1;
1421 if (was_declared (sym) || sym->ns->parent == NULL)
1422 return 0;
1424 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1426 return (s == NULL) ? 0 : specific_sym (s);
1430 /* Figure out if the procedure is specific, generic or unknown. */
1432 typedef enum
1433 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1434 proc_type;
1436 static proc_type
1437 procedure_kind (gfc_symbol *sym)
1439 if (generic_sym (sym))
1440 return PTYPE_GENERIC;
1442 if (specific_sym (sym))
1443 return PTYPE_SPECIFIC;
1445 return PTYPE_UNKNOWN;
1448 /* Check references to assumed size arrays. The flag need_full_assumed_size
1449 is nonzero when matching actual arguments. */
1451 static int need_full_assumed_size = 0;
1453 static bool
1454 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1456 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1457 return false;
1459 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1460 What should it be? */
1461 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1462 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1463 && (e->ref->u.ar.type == AR_FULL))
1465 gfc_error ("The upper bound in the last dimension must "
1466 "appear in the reference to the assumed size "
1467 "array '%s' at %L", sym->name, &e->where);
1468 return true;
1470 return false;
1474 /* Look for bad assumed size array references in argument expressions
1475 of elemental and array valued intrinsic procedures. Since this is
1476 called from procedure resolution functions, it only recurses at
1477 operators. */
1479 static bool
1480 resolve_assumed_size_actual (gfc_expr *e)
1482 if (e == NULL)
1483 return false;
1485 switch (e->expr_type)
1487 case EXPR_VARIABLE:
1488 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1489 return true;
1490 break;
1492 case EXPR_OP:
1493 if (resolve_assumed_size_actual (e->value.op.op1)
1494 || resolve_assumed_size_actual (e->value.op.op2))
1495 return true;
1496 break;
1498 default:
1499 break;
1501 return false;
1505 /* Check a generic procedure, passed as an actual argument, to see if
1506 there is a matching specific name. If none, it is an error, and if
1507 more than one, the reference is ambiguous. */
1508 static int
1509 count_specific_procs (gfc_expr *e)
1511 int n;
1512 gfc_interface *p;
1513 gfc_symbol *sym;
1515 n = 0;
1516 sym = e->symtree->n.sym;
1518 for (p = sym->generic; p; p = p->next)
1519 if (strcmp (sym->name, p->sym->name) == 0)
1521 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1522 sym->name);
1523 n++;
1526 if (n > 1)
1527 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1528 &e->where);
1530 if (n == 0)
1531 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1532 "argument at %L", sym->name, &e->where);
1534 return n;
1538 /* See if a call to sym could possibly be a not allowed RECURSION because of
1539 a missing RECURSIVE declaration. This means that either sym is the current
1540 context itself, or sym is the parent of a contained procedure calling its
1541 non-RECURSIVE containing procedure.
1542 This also works if sym is an ENTRY. */
1544 static bool
1545 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1547 gfc_symbol* proc_sym;
1548 gfc_symbol* context_proc;
1549 gfc_namespace* real_context;
1551 if (sym->attr.flavor == FL_PROGRAM
1552 || sym->attr.flavor == FL_DERIVED)
1553 return false;
1555 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1557 /* If we've got an ENTRY, find real procedure. */
1558 if (sym->attr.entry && sym->ns->entries)
1559 proc_sym = sym->ns->entries->sym;
1560 else
1561 proc_sym = sym;
1563 /* If sym is RECURSIVE, all is well of course. */
1564 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1565 return false;
1567 /* Find the context procedure's "real" symbol if it has entries.
1568 We look for a procedure symbol, so recurse on the parents if we don't
1569 find one (like in case of a BLOCK construct). */
1570 for (real_context = context; ; real_context = real_context->parent)
1572 /* We should find something, eventually! */
1573 gcc_assert (real_context);
1575 context_proc = (real_context->entries ? real_context->entries->sym
1576 : real_context->proc_name);
1578 /* In some special cases, there may not be a proc_name, like for this
1579 invalid code:
1580 real(bad_kind()) function foo () ...
1581 when checking the call to bad_kind ().
1582 In these cases, we simply return here and assume that the
1583 call is ok. */
1584 if (!context_proc)
1585 return false;
1587 if (context_proc->attr.flavor != FL_LABEL)
1588 break;
1591 /* A call from sym's body to itself is recursion, of course. */
1592 if (context_proc == proc_sym)
1593 return true;
1595 /* The same is true if context is a contained procedure and sym the
1596 containing one. */
1597 if (context_proc->attr.contained)
1599 gfc_symbol* parent_proc;
1601 gcc_assert (context->parent);
1602 parent_proc = (context->parent->entries ? context->parent->entries->sym
1603 : context->parent->proc_name);
1605 if (parent_proc == proc_sym)
1606 return true;
1609 return false;
1613 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1614 its typespec and formal argument list. */
1616 bool
1617 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1619 gfc_intrinsic_sym* isym = NULL;
1620 const char* symstd;
1622 if (sym->formal)
1623 return true;
1625 /* Already resolved. */
1626 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1627 return true;
1629 /* We already know this one is an intrinsic, so we don't call
1630 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1631 gfc_find_subroutine directly to check whether it is a function or
1632 subroutine. */
1634 if (sym->intmod_sym_id && sym->attr.subroutine)
1636 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1637 isym = gfc_intrinsic_subroutine_by_id (id);
1639 else if (sym->intmod_sym_id)
1641 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1642 isym = gfc_intrinsic_function_by_id (id);
1644 else if (!sym->attr.subroutine)
1645 isym = gfc_find_function (sym->name);
1647 if (isym && !sym->attr.subroutine)
1649 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1650 && !sym->attr.implicit_type)
1651 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1652 " ignored", sym->name, &sym->declared_at);
1654 if (!sym->attr.function &&
1655 !gfc_add_function(&sym->attr, sym->name, loc))
1656 return false;
1658 sym->ts = isym->ts;
1660 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1662 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1664 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1665 " specifier", sym->name, &sym->declared_at);
1666 return false;
1669 if (!sym->attr.subroutine &&
1670 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1671 return false;
1673 else
1675 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1676 &sym->declared_at);
1677 return false;
1680 gfc_copy_formal_args_intr (sym, isym);
1682 sym->attr.pure = isym->pure;
1683 sym->attr.elemental = isym->elemental;
1685 /* Check it is actually available in the standard settings. */
1686 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1688 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1689 " available in the current standard settings but %s. Use"
1690 " an appropriate -std=* option or enable -fall-intrinsics"
1691 " in order to use it.",
1692 sym->name, &sym->declared_at, symstd);
1693 return false;
1696 return true;
1700 /* Resolve a procedure expression, like passing it to a called procedure or as
1701 RHS for a procedure pointer assignment. */
1703 static bool
1704 resolve_procedure_expression (gfc_expr* expr)
1706 gfc_symbol* sym;
1708 if (expr->expr_type != EXPR_VARIABLE)
1709 return true;
1710 gcc_assert (expr->symtree);
1712 sym = expr->symtree->n.sym;
1714 if (sym->attr.intrinsic)
1715 gfc_resolve_intrinsic (sym, &expr->where);
1717 if (sym->attr.flavor != FL_PROCEDURE
1718 || (sym->attr.function && sym->result == sym))
1719 return true;
1721 /* A non-RECURSIVE procedure that is used as procedure expression within its
1722 own body is in danger of being called recursively. */
1723 if (is_illegal_recursion (sym, gfc_current_ns))
1724 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1725 " itself recursively. Declare it RECURSIVE or use"
1726 " -frecursive", sym->name, &expr->where);
1728 return true;
1732 /* Resolve an actual argument list. Most of the time, this is just
1733 resolving the expressions in the list.
1734 The exception is that we sometimes have to decide whether arguments
1735 that look like procedure arguments are really simple variable
1736 references. */
1738 static bool
1739 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1740 bool no_formal_args)
1742 gfc_symbol *sym;
1743 gfc_symtree *parent_st;
1744 gfc_expr *e;
1745 int save_need_full_assumed_size;
1746 bool return_value = false;
1747 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1749 actual_arg = true;
1750 first_actual_arg = true;
1752 for (; arg; arg = arg->next)
1754 e = arg->expr;
1755 if (e == NULL)
1757 /* Check the label is a valid branching target. */
1758 if (arg->label)
1760 if (arg->label->defined == ST_LABEL_UNKNOWN)
1762 gfc_error ("Label %d referenced at %L is never defined",
1763 arg->label->value, &arg->label->where);
1764 goto cleanup;
1767 first_actual_arg = false;
1768 continue;
1771 if (e->expr_type == EXPR_VARIABLE
1772 && e->symtree->n.sym->attr.generic
1773 && no_formal_args
1774 && count_specific_procs (e) != 1)
1775 goto cleanup;
1777 if (e->ts.type != BT_PROCEDURE)
1779 save_need_full_assumed_size = need_full_assumed_size;
1780 if (e->expr_type != EXPR_VARIABLE)
1781 need_full_assumed_size = 0;
1782 if (!gfc_resolve_expr (e))
1783 goto cleanup;
1784 need_full_assumed_size = save_need_full_assumed_size;
1785 goto argument_list;
1788 /* See if the expression node should really be a variable reference. */
1790 sym = e->symtree->n.sym;
1792 if (sym->attr.flavor == FL_PROCEDURE
1793 || sym->attr.intrinsic
1794 || sym->attr.external)
1796 int actual_ok;
1798 /* If a procedure is not already determined to be something else
1799 check if it is intrinsic. */
1800 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1801 sym->attr.intrinsic = 1;
1803 if (sym->attr.proc == PROC_ST_FUNCTION)
1805 gfc_error ("Statement function '%s' at %L is not allowed as an "
1806 "actual argument", sym->name, &e->where);
1809 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1810 sym->attr.subroutine);
1811 if (sym->attr.intrinsic && actual_ok == 0)
1813 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1814 "actual argument", sym->name, &e->where);
1817 if (sym->attr.contained && !sym->attr.use_assoc
1818 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1820 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure '%s' is"
1821 " used as actual argument at %L",
1822 sym->name, &e->where))
1823 goto cleanup;
1826 if (sym->attr.elemental && !sym->attr.intrinsic)
1828 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1829 "allowed as an actual argument at %L", sym->name,
1830 &e->where);
1833 /* Check if a generic interface has a specific procedure
1834 with the same name before emitting an error. */
1835 if (sym->attr.generic && count_specific_procs (e) != 1)
1836 goto cleanup;
1838 /* Just in case a specific was found for the expression. */
1839 sym = e->symtree->n.sym;
1841 /* If the symbol is the function that names the current (or
1842 parent) scope, then we really have a variable reference. */
1844 if (gfc_is_function_return_value (sym, sym->ns))
1845 goto got_variable;
1847 /* If all else fails, see if we have a specific intrinsic. */
1848 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1850 gfc_intrinsic_sym *isym;
1852 isym = gfc_find_function (sym->name);
1853 if (isym == NULL || !isym->specific)
1855 gfc_error ("Unable to find a specific INTRINSIC procedure "
1856 "for the reference '%s' at %L", sym->name,
1857 &e->where);
1858 goto cleanup;
1860 sym->ts = isym->ts;
1861 sym->attr.intrinsic = 1;
1862 sym->attr.function = 1;
1865 if (!gfc_resolve_expr (e))
1866 goto cleanup;
1867 goto argument_list;
1870 /* See if the name is a module procedure in a parent unit. */
1872 if (was_declared (sym) || sym->ns->parent == NULL)
1873 goto got_variable;
1875 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1877 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1878 goto cleanup;
1881 if (parent_st == NULL)
1882 goto got_variable;
1884 sym = parent_st->n.sym;
1885 e->symtree = parent_st; /* Point to the right thing. */
1887 if (sym->attr.flavor == FL_PROCEDURE
1888 || sym->attr.intrinsic
1889 || sym->attr.external)
1891 if (!gfc_resolve_expr (e))
1892 goto cleanup;
1893 goto argument_list;
1896 got_variable:
1897 e->expr_type = EXPR_VARIABLE;
1898 e->ts = sym->ts;
1899 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1900 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1901 && CLASS_DATA (sym)->as))
1903 e->rank = sym->ts.type == BT_CLASS
1904 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
1905 e->ref = gfc_get_ref ();
1906 e->ref->type = REF_ARRAY;
1907 e->ref->u.ar.type = AR_FULL;
1908 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1909 ? CLASS_DATA (sym)->as : sym->as;
1912 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1913 primary.c (match_actual_arg). If above code determines that it
1914 is a variable instead, it needs to be resolved as it was not
1915 done at the beginning of this function. */
1916 save_need_full_assumed_size = need_full_assumed_size;
1917 if (e->expr_type != EXPR_VARIABLE)
1918 need_full_assumed_size = 0;
1919 if (!gfc_resolve_expr (e))
1920 goto cleanup;
1921 need_full_assumed_size = save_need_full_assumed_size;
1923 argument_list:
1924 /* Check argument list functions %VAL, %LOC and %REF. There is
1925 nothing to do for %REF. */
1926 if (arg->name && arg->name[0] == '%')
1928 if (strncmp ("%VAL", arg->name, 4) == 0)
1930 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1932 gfc_error ("By-value argument at %L is not of numeric "
1933 "type", &e->where);
1934 goto cleanup;
1937 if (e->rank)
1939 gfc_error ("By-value argument at %L cannot be an array or "
1940 "an array section", &e->where);
1941 goto cleanup;
1944 /* Intrinsics are still PROC_UNKNOWN here. However,
1945 since same file external procedures are not resolvable
1946 in gfortran, it is a good deal easier to leave them to
1947 intrinsic.c. */
1948 if (ptype != PROC_UNKNOWN
1949 && ptype != PROC_DUMMY
1950 && ptype != PROC_EXTERNAL
1951 && ptype != PROC_MODULE)
1953 gfc_error ("By-value argument at %L is not allowed "
1954 "in this context", &e->where);
1955 goto cleanup;
1959 /* Statement functions have already been excluded above. */
1960 else if (strncmp ("%LOC", arg->name, 4) == 0
1961 && e->ts.type == BT_PROCEDURE)
1963 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1965 gfc_error ("Passing internal procedure at %L by location "
1966 "not allowed", &e->where);
1967 goto cleanup;
1972 /* Fortran 2008, C1237. */
1973 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1974 && gfc_has_ultimate_pointer (e))
1976 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1977 "component", &e->where);
1978 goto cleanup;
1981 first_actual_arg = false;
1984 return_value = true;
1986 cleanup:
1987 actual_arg = actual_arg_sav;
1988 first_actual_arg = first_actual_arg_sav;
1990 return return_value;
1994 /* Do the checks of the actual argument list that are specific to elemental
1995 procedures. If called with c == NULL, we have a function, otherwise if
1996 expr == NULL, we have a subroutine. */
1998 static bool
1999 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2001 gfc_actual_arglist *arg0;
2002 gfc_actual_arglist *arg;
2003 gfc_symbol *esym = NULL;
2004 gfc_intrinsic_sym *isym = NULL;
2005 gfc_expr *e = NULL;
2006 gfc_intrinsic_arg *iformal = NULL;
2007 gfc_formal_arglist *eformal = NULL;
2008 bool formal_optional = false;
2009 bool set_by_optional = false;
2010 int i;
2011 int rank = 0;
2013 /* Is this an elemental procedure? */
2014 if (expr && expr->value.function.actual != NULL)
2016 if (expr->value.function.esym != NULL
2017 && expr->value.function.esym->attr.elemental)
2019 arg0 = expr->value.function.actual;
2020 esym = expr->value.function.esym;
2022 else if (expr->value.function.isym != NULL
2023 && expr->value.function.isym->elemental)
2025 arg0 = expr->value.function.actual;
2026 isym = expr->value.function.isym;
2028 else
2029 return true;
2031 else if (c && c->ext.actual != NULL)
2033 arg0 = c->ext.actual;
2035 if (c->resolved_sym)
2036 esym = c->resolved_sym;
2037 else
2038 esym = c->symtree->n.sym;
2039 gcc_assert (esym);
2041 if (!esym->attr.elemental)
2042 return true;
2044 else
2045 return true;
2047 /* The rank of an elemental is the rank of its array argument(s). */
2048 for (arg = arg0; arg; arg = arg->next)
2050 if (arg->expr != NULL && arg->expr->rank != 0)
2052 rank = arg->expr->rank;
2053 if (arg->expr->expr_type == EXPR_VARIABLE
2054 && arg->expr->symtree->n.sym->attr.optional)
2055 set_by_optional = true;
2057 /* Function specific; set the result rank and shape. */
2058 if (expr)
2060 expr->rank = rank;
2061 if (!expr->shape && arg->expr->shape)
2063 expr->shape = gfc_get_shape (rank);
2064 for (i = 0; i < rank; i++)
2065 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2068 break;
2072 /* If it is an array, it shall not be supplied as an actual argument
2073 to an elemental procedure unless an array of the same rank is supplied
2074 as an actual argument corresponding to a nonoptional dummy argument of
2075 that elemental procedure(12.4.1.5). */
2076 formal_optional = false;
2077 if (isym)
2078 iformal = isym->formal;
2079 else
2080 eformal = esym->formal;
2082 for (arg = arg0; arg; arg = arg->next)
2084 if (eformal)
2086 if (eformal->sym && eformal->sym->attr.optional)
2087 formal_optional = true;
2088 eformal = eformal->next;
2090 else if (isym && iformal)
2092 if (iformal->optional)
2093 formal_optional = true;
2094 iformal = iformal->next;
2096 else if (isym)
2097 formal_optional = true;
2099 if (pedantic && arg->expr != NULL
2100 && arg->expr->expr_type == EXPR_VARIABLE
2101 && arg->expr->symtree->n.sym->attr.optional
2102 && formal_optional
2103 && arg->expr->rank
2104 && (set_by_optional || arg->expr->rank != rank)
2105 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2107 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
2108 "MISSING, it cannot be the actual argument of an "
2109 "ELEMENTAL procedure unless there is a non-optional "
2110 "argument with the same rank (12.4.1.5)",
2111 arg->expr->symtree->n.sym->name, &arg->expr->where);
2115 for (arg = arg0; arg; arg = arg->next)
2117 if (arg->expr == NULL || arg->expr->rank == 0)
2118 continue;
2120 /* Being elemental, the last upper bound of an assumed size array
2121 argument must be present. */
2122 if (resolve_assumed_size_actual (arg->expr))
2123 return false;
2125 /* Elemental procedure's array actual arguments must conform. */
2126 if (e != NULL)
2128 if (!gfc_check_conformance (arg->expr, e, "elemental procedure"))
2129 return false;
2131 else
2132 e = arg->expr;
2135 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2136 is an array, the intent inout/out variable needs to be also an array. */
2137 if (rank > 0 && esym && expr == NULL)
2138 for (eformal = esym->formal, arg = arg0; arg && eformal;
2139 arg = arg->next, eformal = eformal->next)
2140 if ((eformal->sym->attr.intent == INTENT_OUT
2141 || eformal->sym->attr.intent == INTENT_INOUT)
2142 && arg->expr && arg->expr->rank == 0)
2144 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
2145 "ELEMENTAL subroutine '%s' is a scalar, but another "
2146 "actual argument is an array", &arg->expr->where,
2147 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2148 : "INOUT", eformal->sym->name, esym->name);
2149 return false;
2151 return true;
2155 /* This function does the checking of references to global procedures
2156 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2157 77 and 95 standards. It checks for a gsymbol for the name, making
2158 one if it does not already exist. If it already exists, then the
2159 reference being resolved must correspond to the type of gsymbol.
2160 Otherwise, the new symbol is equipped with the attributes of the
2161 reference. The corresponding code that is called in creating
2162 global entities is parse.c.
2164 In addition, for all but -std=legacy, the gsymbols are used to
2165 check the interfaces of external procedures from the same file.
2166 The namespace of the gsymbol is resolved and then, once this is
2167 done the interface is checked. */
2170 static bool
2171 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2173 if (!gsym_ns->proc_name->attr.recursive)
2174 return true;
2176 if (sym->ns == gsym_ns)
2177 return false;
2179 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2180 return false;
2182 return true;
2185 static bool
2186 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2188 if (gsym_ns->entries)
2190 gfc_entry_list *entry = gsym_ns->entries;
2192 for (; entry; entry = entry->next)
2194 if (strcmp (sym->name, entry->sym->name) == 0)
2196 if (strcmp (gsym_ns->proc_name->name,
2197 sym->ns->proc_name->name) == 0)
2198 return false;
2200 if (sym->ns->parent
2201 && strcmp (gsym_ns->proc_name->name,
2202 sym->ns->parent->proc_name->name) == 0)
2203 return false;
2207 return true;
2211 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2213 bool
2214 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2216 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2218 for ( ; arg; arg = arg->next)
2220 if (!arg->sym)
2221 continue;
2223 if (arg->sym->attr.allocatable) /* (2a) */
2225 strncpy (errmsg, _("allocatable argument"), err_len);
2226 return true;
2228 else if (arg->sym->attr.asynchronous)
2230 strncpy (errmsg, _("asynchronous argument"), err_len);
2231 return true;
2233 else if (arg->sym->attr.optional)
2235 strncpy (errmsg, _("optional argument"), err_len);
2236 return true;
2238 else if (arg->sym->attr.pointer)
2240 strncpy (errmsg, _("pointer argument"), err_len);
2241 return true;
2243 else if (arg->sym->attr.target)
2245 strncpy (errmsg, _("target argument"), err_len);
2246 return true;
2248 else if (arg->sym->attr.value)
2250 strncpy (errmsg, _("value argument"), err_len);
2251 return true;
2253 else if (arg->sym->attr.volatile_)
2255 strncpy (errmsg, _("volatile argument"), err_len);
2256 return true;
2258 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2260 strncpy (errmsg, _("assumed-shape argument"), err_len);
2261 return true;
2263 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2265 strncpy (errmsg, _("assumed-rank argument"), err_len);
2266 return true;
2268 else if (arg->sym->attr.codimension) /* (2c) */
2270 strncpy (errmsg, _("coarray argument"), err_len);
2271 return true;
2273 else if (false) /* (2d) TODO: parametrized derived type */
2275 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2276 return true;
2278 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2280 strncpy (errmsg, _("polymorphic argument"), err_len);
2281 return true;
2283 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2285 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2286 return true;
2288 else if (arg->sym->ts.type == BT_ASSUMED)
2290 /* As assumed-type is unlimited polymorphic (cf. above).
2291 See also TS 29113, Note 6.1. */
2292 strncpy (errmsg, _("assumed-type argument"), err_len);
2293 return true;
2297 if (sym->attr.function)
2299 gfc_symbol *res = sym->result ? sym->result : sym;
2301 if (res->attr.dimension) /* (3a) */
2303 strncpy (errmsg, _("array result"), err_len);
2304 return true;
2306 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2308 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2309 return true;
2311 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2312 && res->ts.u.cl->length
2313 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2315 strncpy (errmsg, _("result with non-constant character length"), err_len);
2316 return true;
2320 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2322 strncpy (errmsg, _("elemental procedure"), err_len);
2323 return true;
2325 else if (sym->attr.is_bind_c) /* (5) */
2327 strncpy (errmsg, _("bind(c) procedure"), err_len);
2328 return true;
2331 return false;
2335 static void
2336 resolve_global_procedure (gfc_symbol *sym, locus *where,
2337 gfc_actual_arglist **actual, int sub)
2339 gfc_gsymbol * gsym;
2340 gfc_namespace *ns;
2341 enum gfc_symbol_type type;
2342 char reason[200];
2344 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2346 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name);
2348 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2349 gfc_global_used (gsym, where);
2351 if ((sym->attr.if_source == IFSRC_UNKNOWN
2352 || sym->attr.if_source == IFSRC_IFBODY)
2353 && gsym->type != GSYM_UNKNOWN
2354 && gsym->ns
2355 && gsym->ns->resolved != -1
2356 && gsym->ns->proc_name
2357 && not_in_recursive (sym, gsym->ns)
2358 && not_entry_self_reference (sym, gsym->ns))
2360 gfc_symbol *def_sym;
2362 /* Resolve the gsymbol namespace if needed. */
2363 if (!gsym->ns->resolved)
2365 gfc_dt_list *old_dt_list;
2366 struct gfc_omp_saved_state old_omp_state;
2368 /* Stash away derived types so that the backend_decls do not
2369 get mixed up. */
2370 old_dt_list = gfc_derived_types;
2371 gfc_derived_types = NULL;
2372 /* And stash away openmp state. */
2373 gfc_omp_save_and_clear_state (&old_omp_state);
2375 gfc_resolve (gsym->ns);
2377 /* Store the new derived types with the global namespace. */
2378 if (gfc_derived_types)
2379 gsym->ns->derived_types = gfc_derived_types;
2381 /* Restore the derived types of this namespace. */
2382 gfc_derived_types = old_dt_list;
2383 /* And openmp state. */
2384 gfc_omp_restore_state (&old_omp_state);
2387 /* Make sure that translation for the gsymbol occurs before
2388 the procedure currently being resolved. */
2389 ns = gfc_global_ns_list;
2390 for (; ns && ns != gsym->ns; ns = ns->sibling)
2392 if (ns->sibling == gsym->ns)
2394 ns->sibling = gsym->ns->sibling;
2395 gsym->ns->sibling = gfc_global_ns_list;
2396 gfc_global_ns_list = gsym->ns;
2397 break;
2401 def_sym = gsym->ns->proc_name;
2403 /* This can happen if a binding name has been specified. */
2404 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2405 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2407 if (def_sym->attr.entry_master)
2409 gfc_entry_list *entry;
2410 for (entry = gsym->ns->entries; entry; entry = entry->next)
2411 if (strcmp (entry->sym->name, sym->name) == 0)
2413 def_sym = entry->sym;
2414 break;
2418 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2420 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2421 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2422 gfc_typename (&def_sym->ts));
2423 goto done;
2426 if (sym->attr.if_source == IFSRC_UNKNOWN
2427 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2429 gfc_error ("Explicit interface required for '%s' at %L: %s",
2430 sym->name, &sym->declared_at, reason);
2431 goto done;
2434 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2435 /* Turn erros into warnings with -std=gnu and -std=legacy. */
2436 gfc_errors_to_warnings (1);
2438 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2439 reason, sizeof(reason), NULL, NULL))
2441 gfc_error ("Interface mismatch in global procedure '%s' at %L: %s ",
2442 sym->name, &sym->declared_at, reason);
2443 goto done;
2446 if (!pedantic
2447 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2448 && !(gfc_option.warn_std & GFC_STD_GNU)))
2449 gfc_errors_to_warnings (1);
2451 if (sym->attr.if_source != IFSRC_IFBODY)
2452 gfc_procedure_use (def_sym, actual, where);
2455 done:
2456 gfc_errors_to_warnings (0);
2458 if (gsym->type == GSYM_UNKNOWN)
2460 gsym->type = type;
2461 gsym->where = *where;
2464 gsym->used = 1;
2468 /************* Function resolution *************/
2470 /* Resolve a function call known to be generic.
2471 Section 14.1.2.4.1. */
2473 static match
2474 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2476 gfc_symbol *s;
2478 if (sym->attr.generic)
2480 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2481 if (s != NULL)
2483 expr->value.function.name = s->name;
2484 expr->value.function.esym = s;
2486 if (s->ts.type != BT_UNKNOWN)
2487 expr->ts = s->ts;
2488 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2489 expr->ts = s->result->ts;
2491 if (s->as != NULL)
2492 expr->rank = s->as->rank;
2493 else if (s->result != NULL && s->result->as != NULL)
2494 expr->rank = s->result->as->rank;
2496 gfc_set_sym_referenced (expr->value.function.esym);
2498 return MATCH_YES;
2501 /* TODO: Need to search for elemental references in generic
2502 interface. */
2505 if (sym->attr.intrinsic)
2506 return gfc_intrinsic_func_interface (expr, 0);
2508 return MATCH_NO;
2512 static bool
2513 resolve_generic_f (gfc_expr *expr)
2515 gfc_symbol *sym;
2516 match m;
2517 gfc_interface *intr = NULL;
2519 sym = expr->symtree->n.sym;
2521 for (;;)
2523 m = resolve_generic_f0 (expr, sym);
2524 if (m == MATCH_YES)
2525 return true;
2526 else if (m == MATCH_ERROR)
2527 return false;
2529 generic:
2530 if (!intr)
2531 for (intr = sym->generic; intr; intr = intr->next)
2532 if (intr->sym->attr.flavor == FL_DERIVED)
2533 break;
2535 if (sym->ns->parent == NULL)
2536 break;
2537 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2539 if (sym == NULL)
2540 break;
2541 if (!generic_sym (sym))
2542 goto generic;
2545 /* Last ditch attempt. See if the reference is to an intrinsic
2546 that possesses a matching interface. 14.1.2.4 */
2547 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2549 gfc_error ("There is no specific function for the generic '%s' "
2550 "at %L", expr->symtree->n.sym->name, &expr->where);
2551 return false;
2554 if (intr)
2556 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2557 NULL, false))
2558 return false;
2559 return resolve_structure_cons (expr, 0);
2562 m = gfc_intrinsic_func_interface (expr, 0);
2563 if (m == MATCH_YES)
2564 return true;
2566 if (m == MATCH_NO)
2567 gfc_error ("Generic function '%s' at %L is not consistent with a "
2568 "specific intrinsic interface", expr->symtree->n.sym->name,
2569 &expr->where);
2571 return false;
2575 /* Resolve a function call known to be specific. */
2577 static match
2578 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2580 match m;
2582 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2584 if (sym->attr.dummy)
2586 sym->attr.proc = PROC_DUMMY;
2587 goto found;
2590 sym->attr.proc = PROC_EXTERNAL;
2591 goto found;
2594 if (sym->attr.proc == PROC_MODULE
2595 || sym->attr.proc == PROC_ST_FUNCTION
2596 || sym->attr.proc == PROC_INTERNAL)
2597 goto found;
2599 if (sym->attr.intrinsic)
2601 m = gfc_intrinsic_func_interface (expr, 1);
2602 if (m == MATCH_YES)
2603 return MATCH_YES;
2604 if (m == MATCH_NO)
2605 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2606 "with an intrinsic", sym->name, &expr->where);
2608 return MATCH_ERROR;
2611 return MATCH_NO;
2613 found:
2614 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2616 if (sym->result)
2617 expr->ts = sym->result->ts;
2618 else
2619 expr->ts = sym->ts;
2620 expr->value.function.name = sym->name;
2621 expr->value.function.esym = sym;
2622 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2623 expr->rank = CLASS_DATA (sym)->as->rank;
2624 else if (sym->as != NULL)
2625 expr->rank = sym->as->rank;
2627 return MATCH_YES;
2631 static bool
2632 resolve_specific_f (gfc_expr *expr)
2634 gfc_symbol *sym;
2635 match m;
2637 sym = expr->symtree->n.sym;
2639 for (;;)
2641 m = resolve_specific_f0 (sym, expr);
2642 if (m == MATCH_YES)
2643 return true;
2644 if (m == MATCH_ERROR)
2645 return false;
2647 if (sym->ns->parent == NULL)
2648 break;
2650 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2652 if (sym == NULL)
2653 break;
2656 gfc_error ("Unable to resolve the specific function '%s' at %L",
2657 expr->symtree->n.sym->name, &expr->where);
2659 return true;
2663 /* Resolve a procedure call not known to be generic nor specific. */
2665 static bool
2666 resolve_unknown_f (gfc_expr *expr)
2668 gfc_symbol *sym;
2669 gfc_typespec *ts;
2671 sym = expr->symtree->n.sym;
2673 if (sym->attr.dummy)
2675 sym->attr.proc = PROC_DUMMY;
2676 expr->value.function.name = sym->name;
2677 goto set_type;
2680 /* See if we have an intrinsic function reference. */
2682 if (gfc_is_intrinsic (sym, 0, expr->where))
2684 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2685 return true;
2686 return false;
2689 /* The reference is to an external name. */
2691 sym->attr.proc = PROC_EXTERNAL;
2692 expr->value.function.name = sym->name;
2693 expr->value.function.esym = expr->symtree->n.sym;
2695 if (sym->as != NULL)
2696 expr->rank = sym->as->rank;
2698 /* Type of the expression is either the type of the symbol or the
2699 default type of the symbol. */
2701 set_type:
2702 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2704 if (sym->ts.type != BT_UNKNOWN)
2705 expr->ts = sym->ts;
2706 else
2708 ts = gfc_get_default_type (sym->name, sym->ns);
2710 if (ts->type == BT_UNKNOWN)
2712 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2713 sym->name, &expr->where);
2714 return false;
2716 else
2717 expr->ts = *ts;
2720 return true;
2724 /* Return true, if the symbol is an external procedure. */
2725 static bool
2726 is_external_proc (gfc_symbol *sym)
2728 if (!sym->attr.dummy && !sym->attr.contained
2729 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2730 && sym->attr.proc != PROC_ST_FUNCTION
2731 && !sym->attr.proc_pointer
2732 && !sym->attr.use_assoc
2733 && sym->name)
2734 return true;
2736 return false;
2740 /* Figure out if a function reference is pure or not. Also set the name
2741 of the function for a potential error message. Return nonzero if the
2742 function is PURE, zero if not. */
2743 static int
2744 pure_stmt_function (gfc_expr *, gfc_symbol *);
2746 static int
2747 pure_function (gfc_expr *e, const char **name)
2749 int pure;
2751 *name = NULL;
2753 if (e->symtree != NULL
2754 && e->symtree->n.sym != NULL
2755 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2756 return pure_stmt_function (e, e->symtree->n.sym);
2758 if (e->value.function.esym)
2760 pure = gfc_pure (e->value.function.esym);
2761 *name = e->value.function.esym->name;
2763 else if (e->value.function.isym)
2765 pure = e->value.function.isym->pure
2766 || e->value.function.isym->elemental;
2767 *name = e->value.function.isym->name;
2769 else
2771 /* Implicit functions are not pure. */
2772 pure = 0;
2773 *name = e->value.function.name;
2776 return pure;
2780 static bool
2781 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2782 int *f ATTRIBUTE_UNUSED)
2784 const char *name;
2786 /* Don't bother recursing into other statement functions
2787 since they will be checked individually for purity. */
2788 if (e->expr_type != EXPR_FUNCTION
2789 || !e->symtree
2790 || e->symtree->n.sym == sym
2791 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2792 return false;
2794 return pure_function (e, &name) ? false : true;
2798 static int
2799 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2801 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2805 /* Resolve a function call, which means resolving the arguments, then figuring
2806 out which entity the name refers to. */
2808 static bool
2809 resolve_function (gfc_expr *expr)
2811 gfc_actual_arglist *arg;
2812 gfc_symbol *sym;
2813 const char *name;
2814 bool t;
2815 int temp;
2816 procedure_type p = PROC_INTRINSIC;
2817 bool no_formal_args;
2819 sym = NULL;
2820 if (expr->symtree)
2821 sym = expr->symtree->n.sym;
2823 /* If this is a procedure pointer component, it has already been resolved. */
2824 if (gfc_is_proc_ptr_comp (expr))
2825 return true;
2827 if (sym && sym->attr.intrinsic
2828 && !gfc_resolve_intrinsic (sym, &expr->where))
2829 return false;
2831 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2833 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2834 return false;
2837 /* If this ia a deferred TBP with an abstract interface (which may
2838 of course be referenced), expr->value.function.esym will be set. */
2839 if (sym && sym->attr.abstract && !expr->value.function.esym)
2841 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2842 sym->name, &expr->where);
2843 return false;
2846 /* Switch off assumed size checking and do this again for certain kinds
2847 of procedure, once the procedure itself is resolved. */
2848 need_full_assumed_size++;
2850 if (expr->symtree && expr->symtree->n.sym)
2851 p = expr->symtree->n.sym->attr.proc;
2853 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2854 inquiry_argument = true;
2855 no_formal_args = sym && is_external_proc (sym)
2856 && gfc_sym_get_dummy_args (sym) == NULL;
2858 if (!resolve_actual_arglist (expr->value.function.actual,
2859 p, no_formal_args))
2861 inquiry_argument = false;
2862 return false;
2865 inquiry_argument = false;
2867 /* Resume assumed_size checking. */
2868 need_full_assumed_size--;
2870 /* If the procedure is external, check for usage. */
2871 if (sym && is_external_proc (sym))
2872 resolve_global_procedure (sym, &expr->where,
2873 &expr->value.function.actual, 0);
2875 if (sym && sym->ts.type == BT_CHARACTER
2876 && sym->ts.u.cl
2877 && sym->ts.u.cl->length == NULL
2878 && !sym->attr.dummy
2879 && !sym->ts.deferred
2880 && expr->value.function.esym == NULL
2881 && !sym->attr.contained)
2883 /* Internal procedures are taken care of in resolve_contained_fntype. */
2884 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2885 "be used at %L since it is not a dummy argument",
2886 sym->name, &expr->where);
2887 return false;
2890 /* See if function is already resolved. */
2892 if (expr->value.function.name != NULL)
2894 if (expr->ts.type == BT_UNKNOWN)
2895 expr->ts = sym->ts;
2896 t = true;
2898 else
2900 /* Apply the rules of section 14.1.2. */
2902 switch (procedure_kind (sym))
2904 case PTYPE_GENERIC:
2905 t = resolve_generic_f (expr);
2906 break;
2908 case PTYPE_SPECIFIC:
2909 t = resolve_specific_f (expr);
2910 break;
2912 case PTYPE_UNKNOWN:
2913 t = resolve_unknown_f (expr);
2914 break;
2916 default:
2917 gfc_internal_error ("resolve_function(): bad function type");
2921 /* If the expression is still a function (it might have simplified),
2922 then we check to see if we are calling an elemental function. */
2924 if (expr->expr_type != EXPR_FUNCTION)
2925 return t;
2927 temp = need_full_assumed_size;
2928 need_full_assumed_size = 0;
2930 if (!resolve_elemental_actual (expr, NULL))
2931 return false;
2933 if (omp_workshare_flag
2934 && expr->value.function.esym
2935 && ! gfc_elemental (expr->value.function.esym))
2937 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2938 "in WORKSHARE construct", expr->value.function.esym->name,
2939 &expr->where);
2940 t = false;
2943 #define GENERIC_ID expr->value.function.isym->id
2944 else if (expr->value.function.actual != NULL
2945 && expr->value.function.isym != NULL
2946 && GENERIC_ID != GFC_ISYM_LBOUND
2947 && GENERIC_ID != GFC_ISYM_LEN
2948 && GENERIC_ID != GFC_ISYM_LOC
2949 && GENERIC_ID != GFC_ISYM_C_LOC
2950 && GENERIC_ID != GFC_ISYM_PRESENT)
2952 /* Array intrinsics must also have the last upper bound of an
2953 assumed size array argument. UBOUND and SIZE have to be
2954 excluded from the check if the second argument is anything
2955 than a constant. */
2957 for (arg = expr->value.function.actual; arg; arg = arg->next)
2959 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2960 && arg == expr->value.function.actual
2961 && arg->next != NULL && arg->next->expr)
2963 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2964 break;
2966 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
2967 break;
2969 if ((int)mpz_get_si (arg->next->expr->value.integer)
2970 < arg->expr->rank)
2971 break;
2974 if (arg->expr != NULL
2975 && arg->expr->rank > 0
2976 && resolve_assumed_size_actual (arg->expr))
2977 return false;
2980 #undef GENERIC_ID
2982 need_full_assumed_size = temp;
2983 name = NULL;
2985 if (!pure_function (expr, &name) && name)
2987 if (forall_flag)
2989 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2990 "FORALL %s", name, &expr->where,
2991 forall_flag == 2 ? "mask" : "block");
2992 t = false;
2994 else if (gfc_do_concurrent_flag)
2996 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2997 "DO CONCURRENT %s", name, &expr->where,
2998 gfc_do_concurrent_flag == 2 ? "mask" : "block");
2999 t = false;
3001 else if (gfc_pure (NULL))
3003 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3004 "procedure within a PURE procedure", name, &expr->where);
3005 t = false;
3008 if (gfc_implicit_pure (NULL))
3009 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3012 /* Functions without the RECURSIVE attribution are not allowed to
3013 * call themselves. */
3014 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3016 gfc_symbol *esym;
3017 esym = expr->value.function.esym;
3019 if (is_illegal_recursion (esym, gfc_current_ns))
3021 if (esym->attr.entry && esym->ns->entries)
3022 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3023 " function '%s' is not RECURSIVE",
3024 esym->name, &expr->where, esym->ns->entries->sym->name);
3025 else
3026 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3027 " is not RECURSIVE", esym->name, &expr->where);
3029 t = false;
3033 /* Character lengths of use associated functions may contains references to
3034 symbols not referenced from the current program unit otherwise. Make sure
3035 those symbols are marked as referenced. */
3037 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3038 && expr->value.function.esym->attr.use_assoc)
3040 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3043 /* Make sure that the expression has a typespec that works. */
3044 if (expr->ts.type == BT_UNKNOWN)
3046 if (expr->symtree->n.sym->result
3047 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3048 && !expr->symtree->n.sym->result->attr.proc_pointer)
3049 expr->ts = expr->symtree->n.sym->result->ts;
3052 return t;
3056 /************* Subroutine resolution *************/
3058 static void
3059 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3061 if (gfc_pure (sym))
3062 return;
3064 if (forall_flag)
3065 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3066 sym->name, &c->loc);
3067 else if (gfc_do_concurrent_flag)
3068 gfc_error ("Subroutine call to '%s' in DO CONCURRENT block at %L is not "
3069 "PURE", sym->name, &c->loc);
3070 else if (gfc_pure (NULL))
3071 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3072 &c->loc);
3074 if (gfc_implicit_pure (NULL))
3075 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3079 static match
3080 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3082 gfc_symbol *s;
3084 if (sym->attr.generic)
3086 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3087 if (s != NULL)
3089 c->resolved_sym = s;
3090 pure_subroutine (c, s);
3091 return MATCH_YES;
3094 /* TODO: Need to search for elemental references in generic interface. */
3097 if (sym->attr.intrinsic)
3098 return gfc_intrinsic_sub_interface (c, 0);
3100 return MATCH_NO;
3104 static bool
3105 resolve_generic_s (gfc_code *c)
3107 gfc_symbol *sym;
3108 match m;
3110 sym = c->symtree->n.sym;
3112 for (;;)
3114 m = resolve_generic_s0 (c, sym);
3115 if (m == MATCH_YES)
3116 return true;
3117 else if (m == MATCH_ERROR)
3118 return false;
3120 generic:
3121 if (sym->ns->parent == NULL)
3122 break;
3123 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3125 if (sym == NULL)
3126 break;
3127 if (!generic_sym (sym))
3128 goto generic;
3131 /* Last ditch attempt. See if the reference is to an intrinsic
3132 that possesses a matching interface. 14.1.2.4 */
3133 sym = c->symtree->n.sym;
3135 if (!gfc_is_intrinsic (sym, 1, c->loc))
3137 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3138 sym->name, &c->loc);
3139 return false;
3142 m = gfc_intrinsic_sub_interface (c, 0);
3143 if (m == MATCH_YES)
3144 return true;
3145 if (m == MATCH_NO)
3146 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3147 "intrinsic subroutine interface", sym->name, &c->loc);
3149 return false;
3153 /* Resolve a subroutine call known to be specific. */
3155 static match
3156 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3158 match m;
3160 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3162 if (sym->attr.dummy)
3164 sym->attr.proc = PROC_DUMMY;
3165 goto found;
3168 sym->attr.proc = PROC_EXTERNAL;
3169 goto found;
3172 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3173 goto found;
3175 if (sym->attr.intrinsic)
3177 m = gfc_intrinsic_sub_interface (c, 1);
3178 if (m == MATCH_YES)
3179 return MATCH_YES;
3180 if (m == MATCH_NO)
3181 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3182 "with an intrinsic", sym->name, &c->loc);
3184 return MATCH_ERROR;
3187 return MATCH_NO;
3189 found:
3190 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3192 c->resolved_sym = sym;
3193 pure_subroutine (c, sym);
3195 return MATCH_YES;
3199 static bool
3200 resolve_specific_s (gfc_code *c)
3202 gfc_symbol *sym;
3203 match m;
3205 sym = c->symtree->n.sym;
3207 for (;;)
3209 m = resolve_specific_s0 (c, sym);
3210 if (m == MATCH_YES)
3211 return true;
3212 if (m == MATCH_ERROR)
3213 return false;
3215 if (sym->ns->parent == NULL)
3216 break;
3218 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3220 if (sym == NULL)
3221 break;
3224 sym = c->symtree->n.sym;
3225 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3226 sym->name, &c->loc);
3228 return false;
3232 /* Resolve a subroutine call not known to be generic nor specific. */
3234 static bool
3235 resolve_unknown_s (gfc_code *c)
3237 gfc_symbol *sym;
3239 sym = c->symtree->n.sym;
3241 if (sym->attr.dummy)
3243 sym->attr.proc = PROC_DUMMY;
3244 goto found;
3247 /* See if we have an intrinsic function reference. */
3249 if (gfc_is_intrinsic (sym, 1, c->loc))
3251 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3252 return true;
3253 return false;
3256 /* The reference is to an external name. */
3258 found:
3259 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3261 c->resolved_sym = sym;
3263 pure_subroutine (c, sym);
3265 return true;
3269 /* Resolve a subroutine call. Although it was tempting to use the same code
3270 for functions, subroutines and functions are stored differently and this
3271 makes things awkward. */
3273 static bool
3274 resolve_call (gfc_code *c)
3276 bool t;
3277 procedure_type ptype = PROC_INTRINSIC;
3278 gfc_symbol *csym, *sym;
3279 bool no_formal_args;
3281 csym = c->symtree ? c->symtree->n.sym : NULL;
3283 if (csym && csym->ts.type != BT_UNKNOWN)
3285 gfc_error ("'%s' at %L has a type, which is not consistent with "
3286 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3287 return false;
3290 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3292 gfc_symtree *st;
3293 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3294 sym = st ? st->n.sym : NULL;
3295 if (sym && csym != sym
3296 && sym->ns == gfc_current_ns
3297 && sym->attr.flavor == FL_PROCEDURE
3298 && sym->attr.contained)
3300 sym->refs++;
3301 if (csym->attr.generic)
3302 c->symtree->n.sym = sym;
3303 else
3304 c->symtree = st;
3305 csym = c->symtree->n.sym;
3309 /* If this ia a deferred TBP, c->expr1 will be set. */
3310 if (!c->expr1 && csym)
3312 if (csym->attr.abstract)
3314 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3315 csym->name, &c->loc);
3316 return false;
3319 /* Subroutines without the RECURSIVE attribution are not allowed to
3320 call themselves. */
3321 if (is_illegal_recursion (csym, gfc_current_ns))
3323 if (csym->attr.entry && csym->ns->entries)
3324 gfc_error ("ENTRY '%s' at %L cannot be called recursively, "
3325 "as subroutine '%s' is not RECURSIVE",
3326 csym->name, &c->loc, csym->ns->entries->sym->name);
3327 else
3328 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, "
3329 "as it is not RECURSIVE", csym->name, &c->loc);
3331 t = false;
3335 /* Switch off assumed size checking and do this again for certain kinds
3336 of procedure, once the procedure itself is resolved. */
3337 need_full_assumed_size++;
3339 if (csym)
3340 ptype = csym->attr.proc;
3342 no_formal_args = csym && is_external_proc (csym)
3343 && gfc_sym_get_dummy_args (csym) == NULL;
3344 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3345 return false;
3347 /* Resume assumed_size checking. */
3348 need_full_assumed_size--;
3350 /* If external, check for usage. */
3351 if (csym && is_external_proc (csym))
3352 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3354 t = true;
3355 if (c->resolved_sym == NULL)
3357 c->resolved_isym = NULL;
3358 switch (procedure_kind (csym))
3360 case PTYPE_GENERIC:
3361 t = resolve_generic_s (c);
3362 break;
3364 case PTYPE_SPECIFIC:
3365 t = resolve_specific_s (c);
3366 break;
3368 case PTYPE_UNKNOWN:
3369 t = resolve_unknown_s (c);
3370 break;
3372 default:
3373 gfc_internal_error ("resolve_subroutine(): bad function type");
3377 /* Some checks of elemental subroutine actual arguments. */
3378 if (!resolve_elemental_actual (NULL, c))
3379 return false;
3381 return t;
3385 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3386 op1->shape and op2->shape are non-NULL return true if their shapes
3387 match. If both op1->shape and op2->shape are non-NULL return false
3388 if their shapes do not match. If either op1->shape or op2->shape is
3389 NULL, return true. */
3391 static bool
3392 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3394 bool t;
3395 int i;
3397 t = true;
3399 if (op1->shape != NULL && op2->shape != NULL)
3401 for (i = 0; i < op1->rank; i++)
3403 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3405 gfc_error ("Shapes for operands at %L and %L are not conformable",
3406 &op1->where, &op2->where);
3407 t = false;
3408 break;
3413 return t;
3417 /* Resolve an operator expression node. This can involve replacing the
3418 operation with a user defined function call. */
3420 static bool
3421 resolve_operator (gfc_expr *e)
3423 gfc_expr *op1, *op2;
3424 char msg[200];
3425 bool dual_locus_error;
3426 bool t;
3428 /* Resolve all subnodes-- give them types. */
3430 switch (e->value.op.op)
3432 default:
3433 if (!gfc_resolve_expr (e->value.op.op2))
3434 return false;
3436 /* Fall through... */
3438 case INTRINSIC_NOT:
3439 case INTRINSIC_UPLUS:
3440 case INTRINSIC_UMINUS:
3441 case INTRINSIC_PARENTHESES:
3442 if (!gfc_resolve_expr (e->value.op.op1))
3443 return false;
3444 break;
3447 /* Typecheck the new node. */
3449 op1 = e->value.op.op1;
3450 op2 = e->value.op.op2;
3451 dual_locus_error = false;
3453 if ((op1 && op1->expr_type == EXPR_NULL)
3454 || (op2 && op2->expr_type == EXPR_NULL))
3456 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3457 goto bad_op;
3460 switch (e->value.op.op)
3462 case INTRINSIC_UPLUS:
3463 case INTRINSIC_UMINUS:
3464 if (op1->ts.type == BT_INTEGER
3465 || op1->ts.type == BT_REAL
3466 || op1->ts.type == BT_COMPLEX)
3468 e->ts = op1->ts;
3469 break;
3472 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3473 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3474 goto bad_op;
3476 case INTRINSIC_PLUS:
3477 case INTRINSIC_MINUS:
3478 case INTRINSIC_TIMES:
3479 case INTRINSIC_DIVIDE:
3480 case INTRINSIC_POWER:
3481 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3483 gfc_type_convert_binary (e, 1);
3484 break;
3487 sprintf (msg,
3488 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3489 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3490 gfc_typename (&op2->ts));
3491 goto bad_op;
3493 case INTRINSIC_CONCAT:
3494 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3495 && op1->ts.kind == op2->ts.kind)
3497 e->ts.type = BT_CHARACTER;
3498 e->ts.kind = op1->ts.kind;
3499 break;
3502 sprintf (msg,
3503 _("Operands of string concatenation operator at %%L are %s/%s"),
3504 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3505 goto bad_op;
3507 case INTRINSIC_AND:
3508 case INTRINSIC_OR:
3509 case INTRINSIC_EQV:
3510 case INTRINSIC_NEQV:
3511 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3513 e->ts.type = BT_LOGICAL;
3514 e->ts.kind = gfc_kind_max (op1, op2);
3515 if (op1->ts.kind < e->ts.kind)
3516 gfc_convert_type (op1, &e->ts, 2);
3517 else if (op2->ts.kind < e->ts.kind)
3518 gfc_convert_type (op2, &e->ts, 2);
3519 break;
3522 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3523 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3524 gfc_typename (&op2->ts));
3526 goto bad_op;
3528 case INTRINSIC_NOT:
3529 if (op1->ts.type == BT_LOGICAL)
3531 e->ts.type = BT_LOGICAL;
3532 e->ts.kind = op1->ts.kind;
3533 break;
3536 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3537 gfc_typename (&op1->ts));
3538 goto bad_op;
3540 case INTRINSIC_GT:
3541 case INTRINSIC_GT_OS:
3542 case INTRINSIC_GE:
3543 case INTRINSIC_GE_OS:
3544 case INTRINSIC_LT:
3545 case INTRINSIC_LT_OS:
3546 case INTRINSIC_LE:
3547 case INTRINSIC_LE_OS:
3548 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3550 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3551 goto bad_op;
3554 /* Fall through... */
3556 case INTRINSIC_EQ:
3557 case INTRINSIC_EQ_OS:
3558 case INTRINSIC_NE:
3559 case INTRINSIC_NE_OS:
3560 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3561 && op1->ts.kind == op2->ts.kind)
3563 e->ts.type = BT_LOGICAL;
3564 e->ts.kind = gfc_default_logical_kind;
3565 break;
3568 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3570 gfc_type_convert_binary (e, 1);
3572 e->ts.type = BT_LOGICAL;
3573 e->ts.kind = gfc_default_logical_kind;
3575 if (gfc_option.warn_compare_reals)
3577 gfc_intrinsic_op op = e->value.op.op;
3579 /* Type conversion has made sure that the types of op1 and op2
3580 agree, so it is only necessary to check the first one. */
3581 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3582 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3583 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3585 const char *msg;
3587 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3588 msg = "Equality comparison for %s at %L";
3589 else
3590 msg = "Inequality comparison for %s at %L";
3592 gfc_warning (msg, gfc_typename (&op1->ts), &op1->where);
3596 break;
3599 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3600 sprintf (msg,
3601 _("Logicals at %%L must be compared with %s instead of %s"),
3602 (e->value.op.op == INTRINSIC_EQ
3603 || e->value.op.op == INTRINSIC_EQ_OS)
3604 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3605 else
3606 sprintf (msg,
3607 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3608 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3609 gfc_typename (&op2->ts));
3611 goto bad_op;
3613 case INTRINSIC_USER:
3614 if (e->value.op.uop->op == NULL)
3615 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3616 else if (op2 == NULL)
3617 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3618 e->value.op.uop->name, gfc_typename (&op1->ts));
3619 else
3621 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3622 e->value.op.uop->name, gfc_typename (&op1->ts),
3623 gfc_typename (&op2->ts));
3624 e->value.op.uop->op->sym->attr.referenced = 1;
3627 goto bad_op;
3629 case INTRINSIC_PARENTHESES:
3630 e->ts = op1->ts;
3631 if (e->ts.type == BT_CHARACTER)
3632 e->ts.u.cl = op1->ts.u.cl;
3633 break;
3635 default:
3636 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3639 /* Deal with arrayness of an operand through an operator. */
3641 t = true;
3643 switch (e->value.op.op)
3645 case INTRINSIC_PLUS:
3646 case INTRINSIC_MINUS:
3647 case INTRINSIC_TIMES:
3648 case INTRINSIC_DIVIDE:
3649 case INTRINSIC_POWER:
3650 case INTRINSIC_CONCAT:
3651 case INTRINSIC_AND:
3652 case INTRINSIC_OR:
3653 case INTRINSIC_EQV:
3654 case INTRINSIC_NEQV:
3655 case INTRINSIC_EQ:
3656 case INTRINSIC_EQ_OS:
3657 case INTRINSIC_NE:
3658 case INTRINSIC_NE_OS:
3659 case INTRINSIC_GT:
3660 case INTRINSIC_GT_OS:
3661 case INTRINSIC_GE:
3662 case INTRINSIC_GE_OS:
3663 case INTRINSIC_LT:
3664 case INTRINSIC_LT_OS:
3665 case INTRINSIC_LE:
3666 case INTRINSIC_LE_OS:
3668 if (op1->rank == 0 && op2->rank == 0)
3669 e->rank = 0;
3671 if (op1->rank == 0 && op2->rank != 0)
3673 e->rank = op2->rank;
3675 if (e->shape == NULL)
3676 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3679 if (op1->rank != 0 && op2->rank == 0)
3681 e->rank = op1->rank;
3683 if (e->shape == NULL)
3684 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3687 if (op1->rank != 0 && op2->rank != 0)
3689 if (op1->rank == op2->rank)
3691 e->rank = op1->rank;
3692 if (e->shape == NULL)
3694 t = compare_shapes (op1, op2);
3695 if (!t)
3696 e->shape = NULL;
3697 else
3698 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3701 else
3703 /* Allow higher level expressions to work. */
3704 e->rank = 0;
3706 /* Try user-defined operators, and otherwise throw an error. */
3707 dual_locus_error = true;
3708 sprintf (msg,
3709 _("Inconsistent ranks for operator at %%L and %%L"));
3710 goto bad_op;
3714 break;
3716 case INTRINSIC_PARENTHESES:
3717 case INTRINSIC_NOT:
3718 case INTRINSIC_UPLUS:
3719 case INTRINSIC_UMINUS:
3720 /* Simply copy arrayness attribute */
3721 e->rank = op1->rank;
3723 if (e->shape == NULL)
3724 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3726 break;
3728 default:
3729 break;
3732 /* Attempt to simplify the expression. */
3733 if (t)
3735 t = gfc_simplify_expr (e, 0);
3736 /* Some calls do not succeed in simplification and return false
3737 even though there is no error; e.g. variable references to
3738 PARAMETER arrays. */
3739 if (!gfc_is_constant_expr (e))
3740 t = true;
3742 return t;
3744 bad_op:
3747 match m = gfc_extend_expr (e);
3748 if (m == MATCH_YES)
3749 return true;
3750 if (m == MATCH_ERROR)
3751 return false;
3754 if (dual_locus_error)
3755 gfc_error (msg, &op1->where, &op2->where);
3756 else
3757 gfc_error (msg, &e->where);
3759 return false;
3763 /************** Array resolution subroutines **************/
3765 typedef enum
3766 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3767 comparison;
3769 /* Compare two integer expressions. */
3771 static comparison
3772 compare_bound (gfc_expr *a, gfc_expr *b)
3774 int i;
3776 if (a == NULL || a->expr_type != EXPR_CONSTANT
3777 || b == NULL || b->expr_type != EXPR_CONSTANT)
3778 return CMP_UNKNOWN;
3780 /* If either of the types isn't INTEGER, we must have
3781 raised an error earlier. */
3783 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3784 return CMP_UNKNOWN;
3786 i = mpz_cmp (a->value.integer, b->value.integer);
3788 if (i < 0)
3789 return CMP_LT;
3790 if (i > 0)
3791 return CMP_GT;
3792 return CMP_EQ;
3796 /* Compare an integer expression with an integer. */
3798 static comparison
3799 compare_bound_int (gfc_expr *a, int b)
3801 int i;
3803 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3804 return CMP_UNKNOWN;
3806 if (a->ts.type != BT_INTEGER)
3807 gfc_internal_error ("compare_bound_int(): Bad expression");
3809 i = mpz_cmp_si (a->value.integer, b);
3811 if (i < 0)
3812 return CMP_LT;
3813 if (i > 0)
3814 return CMP_GT;
3815 return CMP_EQ;
3819 /* Compare an integer expression with a mpz_t. */
3821 static comparison
3822 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3824 int i;
3826 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3827 return CMP_UNKNOWN;
3829 if (a->ts.type != BT_INTEGER)
3830 gfc_internal_error ("compare_bound_int(): Bad expression");
3832 i = mpz_cmp (a->value.integer, b);
3834 if (i < 0)
3835 return CMP_LT;
3836 if (i > 0)
3837 return CMP_GT;
3838 return CMP_EQ;
3842 /* Compute the last value of a sequence given by a triplet.
3843 Return 0 if it wasn't able to compute the last value, or if the
3844 sequence if empty, and 1 otherwise. */
3846 static int
3847 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3848 gfc_expr *stride, mpz_t last)
3850 mpz_t rem;
3852 if (start == NULL || start->expr_type != EXPR_CONSTANT
3853 || end == NULL || end->expr_type != EXPR_CONSTANT
3854 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3855 return 0;
3857 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3858 || (stride != NULL && stride->ts.type != BT_INTEGER))
3859 return 0;
3861 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
3863 if (compare_bound (start, end) == CMP_GT)
3864 return 0;
3865 mpz_set (last, end->value.integer);
3866 return 1;
3869 if (compare_bound_int (stride, 0) == CMP_GT)
3871 /* Stride is positive */
3872 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3873 return 0;
3875 else
3877 /* Stride is negative */
3878 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3879 return 0;
3882 mpz_init (rem);
3883 mpz_sub (rem, end->value.integer, start->value.integer);
3884 mpz_tdiv_r (rem, rem, stride->value.integer);
3885 mpz_sub (last, end->value.integer, rem);
3886 mpz_clear (rem);
3888 return 1;
3892 /* Compare a single dimension of an array reference to the array
3893 specification. */
3895 static bool
3896 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3898 mpz_t last_value;
3900 if (ar->dimen_type[i] == DIMEN_STAR)
3902 gcc_assert (ar->stride[i] == NULL);
3903 /* This implies [*] as [*:] and [*:3] are not possible. */
3904 if (ar->start[i] == NULL)
3906 gcc_assert (ar->end[i] == NULL);
3907 return true;
3911 /* Given start, end and stride values, calculate the minimum and
3912 maximum referenced indexes. */
3914 switch (ar->dimen_type[i])
3916 case DIMEN_VECTOR:
3917 case DIMEN_THIS_IMAGE:
3918 break;
3920 case DIMEN_STAR:
3921 case DIMEN_ELEMENT:
3922 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3924 if (i < as->rank)
3925 gfc_warning ("Array reference at %L is out of bounds "
3926 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3927 mpz_get_si (ar->start[i]->value.integer),
3928 mpz_get_si (as->lower[i]->value.integer), i+1);
3929 else
3930 gfc_warning ("Array reference at %L is out of bounds "
3931 "(%ld < %ld) in codimension %d", &ar->c_where[i],
3932 mpz_get_si (ar->start[i]->value.integer),
3933 mpz_get_si (as->lower[i]->value.integer),
3934 i + 1 - as->rank);
3935 return true;
3937 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3939 if (i < as->rank)
3940 gfc_warning ("Array reference at %L is out of bounds "
3941 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3942 mpz_get_si (ar->start[i]->value.integer),
3943 mpz_get_si (as->upper[i]->value.integer), i+1);
3944 else
3945 gfc_warning ("Array reference at %L is out of bounds "
3946 "(%ld > %ld) in codimension %d", &ar->c_where[i],
3947 mpz_get_si (ar->start[i]->value.integer),
3948 mpz_get_si (as->upper[i]->value.integer),
3949 i + 1 - as->rank);
3950 return true;
3953 break;
3955 case DIMEN_RANGE:
3957 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3958 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3960 comparison comp_start_end = compare_bound (AR_START, AR_END);
3962 /* Check for zero stride, which is not allowed. */
3963 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3965 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3966 return false;
3969 /* if start == len || (stride > 0 && start < len)
3970 || (stride < 0 && start > len),
3971 then the array section contains at least one element. In this
3972 case, there is an out-of-bounds access if
3973 (start < lower || start > upper). */
3974 if (compare_bound (AR_START, AR_END) == CMP_EQ
3975 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3976 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3977 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3978 && comp_start_end == CMP_GT))
3980 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3982 gfc_warning ("Lower array reference at %L is out of bounds "
3983 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3984 mpz_get_si (AR_START->value.integer),
3985 mpz_get_si (as->lower[i]->value.integer), i+1);
3986 return true;
3988 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3990 gfc_warning ("Lower array reference at %L is out of bounds "
3991 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3992 mpz_get_si (AR_START->value.integer),
3993 mpz_get_si (as->upper[i]->value.integer), i+1);
3994 return true;
3998 /* If we can compute the highest index of the array section,
3999 then it also has to be between lower and upper. */
4000 mpz_init (last_value);
4001 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4002 last_value))
4004 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4006 gfc_warning ("Upper array reference at %L is out of bounds "
4007 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4008 mpz_get_si (last_value),
4009 mpz_get_si (as->lower[i]->value.integer), i+1);
4010 mpz_clear (last_value);
4011 return true;
4013 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4015 gfc_warning ("Upper array reference at %L is out of bounds "
4016 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4017 mpz_get_si (last_value),
4018 mpz_get_si (as->upper[i]->value.integer), i+1);
4019 mpz_clear (last_value);
4020 return true;
4023 mpz_clear (last_value);
4025 #undef AR_START
4026 #undef AR_END
4028 break;
4030 default:
4031 gfc_internal_error ("check_dimension(): Bad array reference");
4034 return true;
4038 /* Compare an array reference with an array specification. */
4040 static bool
4041 compare_spec_to_ref (gfc_array_ref *ar)
4043 gfc_array_spec *as;
4044 int i;
4046 as = ar->as;
4047 i = as->rank - 1;
4048 /* TODO: Full array sections are only allowed as actual parameters. */
4049 if (as->type == AS_ASSUMED_SIZE
4050 && (/*ar->type == AR_FULL
4051 ||*/ (ar->type == AR_SECTION
4052 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4054 gfc_error ("Rightmost upper bound of assumed size array section "
4055 "not specified at %L", &ar->where);
4056 return false;
4059 if (ar->type == AR_FULL)
4060 return true;
4062 if (as->rank != ar->dimen)
4064 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4065 &ar->where, ar->dimen, as->rank);
4066 return false;
4069 /* ar->codimen == 0 is a local array. */
4070 if (as->corank != ar->codimen && ar->codimen != 0)
4072 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4073 &ar->where, ar->codimen, as->corank);
4074 return false;
4077 for (i = 0; i < as->rank; i++)
4078 if (!check_dimension (i, ar, as))
4079 return false;
4081 /* Local access has no coarray spec. */
4082 if (ar->codimen != 0)
4083 for (i = as->rank; i < as->rank + as->corank; i++)
4085 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4086 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4088 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4089 i + 1 - as->rank, &ar->where);
4090 return false;
4092 if (!check_dimension (i, ar, as))
4093 return false;
4096 return true;
4100 /* Resolve one part of an array index. */
4102 static bool
4103 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4104 int force_index_integer_kind)
4106 gfc_typespec ts;
4108 if (index == NULL)
4109 return true;
4111 if (!gfc_resolve_expr (index))
4112 return false;
4114 if (check_scalar && index->rank != 0)
4116 gfc_error ("Array index at %L must be scalar", &index->where);
4117 return false;
4120 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4122 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4123 &index->where, gfc_basic_typename (index->ts.type));
4124 return false;
4127 if (index->ts.type == BT_REAL)
4128 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4129 &index->where))
4130 return false;
4132 if ((index->ts.kind != gfc_index_integer_kind
4133 && force_index_integer_kind)
4134 || index->ts.type != BT_INTEGER)
4136 gfc_clear_ts (&ts);
4137 ts.type = BT_INTEGER;
4138 ts.kind = gfc_index_integer_kind;
4140 gfc_convert_type_warn (index, &ts, 2, 0);
4143 return true;
4146 /* Resolve one part of an array index. */
4148 bool
4149 gfc_resolve_index (gfc_expr *index, int check_scalar)
4151 return gfc_resolve_index_1 (index, check_scalar, 1);
4154 /* Resolve a dim argument to an intrinsic function. */
4156 bool
4157 gfc_resolve_dim_arg (gfc_expr *dim)
4159 if (dim == NULL)
4160 return true;
4162 if (!gfc_resolve_expr (dim))
4163 return false;
4165 if (dim->rank != 0)
4167 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4168 return false;
4172 if (dim->ts.type != BT_INTEGER)
4174 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4175 return false;
4178 if (dim->ts.kind != gfc_index_integer_kind)
4180 gfc_typespec ts;
4182 gfc_clear_ts (&ts);
4183 ts.type = BT_INTEGER;
4184 ts.kind = gfc_index_integer_kind;
4186 gfc_convert_type_warn (dim, &ts, 2, 0);
4189 return true;
4192 /* Given an expression that contains array references, update those array
4193 references to point to the right array specifications. While this is
4194 filled in during matching, this information is difficult to save and load
4195 in a module, so we take care of it here.
4197 The idea here is that the original array reference comes from the
4198 base symbol. We traverse the list of reference structures, setting
4199 the stored reference to references. Component references can
4200 provide an additional array specification. */
4202 static void
4203 find_array_spec (gfc_expr *e)
4205 gfc_array_spec *as;
4206 gfc_component *c;
4207 gfc_ref *ref;
4209 if (e->symtree->n.sym->ts.type == BT_CLASS)
4210 as = CLASS_DATA (e->symtree->n.sym)->as;
4211 else
4212 as = e->symtree->n.sym->as;
4214 for (ref = e->ref; ref; ref = ref->next)
4215 switch (ref->type)
4217 case REF_ARRAY:
4218 if (as == NULL)
4219 gfc_internal_error ("find_array_spec(): Missing spec");
4221 ref->u.ar.as = as;
4222 as = NULL;
4223 break;
4225 case REF_COMPONENT:
4226 c = ref->u.c.component;
4227 if (c->attr.dimension)
4229 if (as != NULL)
4230 gfc_internal_error ("find_array_spec(): unused as(1)");
4231 as = c->as;
4234 break;
4236 case REF_SUBSTRING:
4237 break;
4240 if (as != NULL)
4241 gfc_internal_error ("find_array_spec(): unused as(2)");
4245 /* Resolve an array reference. */
4247 static bool
4248 resolve_array_ref (gfc_array_ref *ar)
4250 int i, check_scalar;
4251 gfc_expr *e;
4253 for (i = 0; i < ar->dimen + ar->codimen; i++)
4255 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4257 /* Do not force gfc_index_integer_kind for the start. We can
4258 do fine with any integer kind. This avoids temporary arrays
4259 created for indexing with a vector. */
4260 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4261 return false;
4262 if (!gfc_resolve_index (ar->end[i], check_scalar))
4263 return false;
4264 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4265 return false;
4267 e = ar->start[i];
4269 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4270 switch (e->rank)
4272 case 0:
4273 ar->dimen_type[i] = DIMEN_ELEMENT;
4274 break;
4276 case 1:
4277 ar->dimen_type[i] = DIMEN_VECTOR;
4278 if (e->expr_type == EXPR_VARIABLE
4279 && e->symtree->n.sym->ts.type == BT_DERIVED)
4280 ar->start[i] = gfc_get_parentheses (e);
4281 break;
4283 default:
4284 gfc_error ("Array index at %L is an array of rank %d",
4285 &ar->c_where[i], e->rank);
4286 return false;
4289 /* Fill in the upper bound, which may be lower than the
4290 specified one for something like a(2:10:5), which is
4291 identical to a(2:7:5). Only relevant for strides not equal
4292 to one. Don't try a division by zero. */
4293 if (ar->dimen_type[i] == DIMEN_RANGE
4294 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4295 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4296 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4298 mpz_t size, end;
4300 if (gfc_ref_dimen_size (ar, i, &size, &end))
4302 if (ar->end[i] == NULL)
4304 ar->end[i] =
4305 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4306 &ar->where);
4307 mpz_set (ar->end[i]->value.integer, end);
4309 else if (ar->end[i]->ts.type == BT_INTEGER
4310 && ar->end[i]->expr_type == EXPR_CONSTANT)
4312 mpz_set (ar->end[i]->value.integer, end);
4314 else
4315 gcc_unreachable ();
4317 mpz_clear (size);
4318 mpz_clear (end);
4323 if (ar->type == AR_FULL)
4325 if (ar->as->rank == 0)
4326 ar->type = AR_ELEMENT;
4328 /* Make sure array is the same as array(:,:), this way
4329 we don't need to special case all the time. */
4330 ar->dimen = ar->as->rank;
4331 for (i = 0; i < ar->dimen; i++)
4333 ar->dimen_type[i] = DIMEN_RANGE;
4335 gcc_assert (ar->start[i] == NULL);
4336 gcc_assert (ar->end[i] == NULL);
4337 gcc_assert (ar->stride[i] == NULL);
4341 /* If the reference type is unknown, figure out what kind it is. */
4343 if (ar->type == AR_UNKNOWN)
4345 ar->type = AR_ELEMENT;
4346 for (i = 0; i < ar->dimen; i++)
4347 if (ar->dimen_type[i] == DIMEN_RANGE
4348 || ar->dimen_type[i] == DIMEN_VECTOR)
4350 ar->type = AR_SECTION;
4351 break;
4355 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4356 return false;
4358 if (ar->as->corank && ar->codimen == 0)
4360 int n;
4361 ar->codimen = ar->as->corank;
4362 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4363 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4366 return true;
4370 static bool
4371 resolve_substring (gfc_ref *ref)
4373 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4375 if (ref->u.ss.start != NULL)
4377 if (!gfc_resolve_expr (ref->u.ss.start))
4378 return false;
4380 if (ref->u.ss.start->ts.type != BT_INTEGER)
4382 gfc_error ("Substring start index at %L must be of type INTEGER",
4383 &ref->u.ss.start->where);
4384 return false;
4387 if (ref->u.ss.start->rank != 0)
4389 gfc_error ("Substring start index at %L must be scalar",
4390 &ref->u.ss.start->where);
4391 return false;
4394 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4395 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4396 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4398 gfc_error ("Substring start index at %L is less than one",
4399 &ref->u.ss.start->where);
4400 return false;
4404 if (ref->u.ss.end != NULL)
4406 if (!gfc_resolve_expr (ref->u.ss.end))
4407 return false;
4409 if (ref->u.ss.end->ts.type != BT_INTEGER)
4411 gfc_error ("Substring end index at %L must be of type INTEGER",
4412 &ref->u.ss.end->where);
4413 return false;
4416 if (ref->u.ss.end->rank != 0)
4418 gfc_error ("Substring end index at %L must be scalar",
4419 &ref->u.ss.end->where);
4420 return false;
4423 if (ref->u.ss.length != NULL
4424 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4425 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4426 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4428 gfc_error ("Substring end index at %L exceeds the string length",
4429 &ref->u.ss.start->where);
4430 return false;
4433 if (compare_bound_mpz_t (ref->u.ss.end,
4434 gfc_integer_kinds[k].huge) == CMP_GT
4435 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4436 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4438 gfc_error ("Substring end index at %L is too large",
4439 &ref->u.ss.end->where);
4440 return false;
4444 return true;
4448 /* This function supplies missing substring charlens. */
4450 void
4451 gfc_resolve_substring_charlen (gfc_expr *e)
4453 gfc_ref *char_ref;
4454 gfc_expr *start, *end;
4456 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4457 if (char_ref->type == REF_SUBSTRING)
4458 break;
4460 if (!char_ref)
4461 return;
4463 gcc_assert (char_ref->next == NULL);
4465 if (e->ts.u.cl)
4467 if (e->ts.u.cl->length)
4468 gfc_free_expr (e->ts.u.cl->length);
4469 else if (e->expr_type == EXPR_VARIABLE
4470 && e->symtree->n.sym->attr.dummy)
4471 return;
4474 e->ts.type = BT_CHARACTER;
4475 e->ts.kind = gfc_default_character_kind;
4477 if (!e->ts.u.cl)
4478 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4480 if (char_ref->u.ss.start)
4481 start = gfc_copy_expr (char_ref->u.ss.start);
4482 else
4483 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4485 if (char_ref->u.ss.end)
4486 end = gfc_copy_expr (char_ref->u.ss.end);
4487 else if (e->expr_type == EXPR_VARIABLE)
4488 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4489 else
4490 end = NULL;
4492 if (!start || !end)
4494 gfc_free_expr (start);
4495 gfc_free_expr (end);
4496 return;
4499 /* Length = (end - start +1). */
4500 e->ts.u.cl->length = gfc_subtract (end, start);
4501 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4502 gfc_get_int_expr (gfc_default_integer_kind,
4503 NULL, 1));
4505 e->ts.u.cl->length->ts.type = BT_INTEGER;
4506 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4508 /* Make sure that the length is simplified. */
4509 gfc_simplify_expr (e->ts.u.cl->length, 1);
4510 gfc_resolve_expr (e->ts.u.cl->length);
4514 /* Resolve subtype references. */
4516 static bool
4517 resolve_ref (gfc_expr *expr)
4519 int current_part_dimension, n_components, seen_part_dimension;
4520 gfc_ref *ref;
4522 for (ref = expr->ref; ref; ref = ref->next)
4523 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4525 find_array_spec (expr);
4526 break;
4529 for (ref = expr->ref; ref; ref = ref->next)
4530 switch (ref->type)
4532 case REF_ARRAY:
4533 if (!resolve_array_ref (&ref->u.ar))
4534 return false;
4535 break;
4537 case REF_COMPONENT:
4538 break;
4540 case REF_SUBSTRING:
4541 if (!resolve_substring (ref))
4542 return false;
4543 break;
4546 /* Check constraints on part references. */
4548 current_part_dimension = 0;
4549 seen_part_dimension = 0;
4550 n_components = 0;
4552 for (ref = expr->ref; ref; ref = ref->next)
4554 switch (ref->type)
4556 case REF_ARRAY:
4557 switch (ref->u.ar.type)
4559 case AR_FULL:
4560 /* Coarray scalar. */
4561 if (ref->u.ar.as->rank == 0)
4563 current_part_dimension = 0;
4564 break;
4566 /* Fall through. */
4567 case AR_SECTION:
4568 current_part_dimension = 1;
4569 break;
4571 case AR_ELEMENT:
4572 current_part_dimension = 0;
4573 break;
4575 case AR_UNKNOWN:
4576 gfc_internal_error ("resolve_ref(): Bad array reference");
4579 break;
4581 case REF_COMPONENT:
4582 if (current_part_dimension || seen_part_dimension)
4584 /* F03:C614. */
4585 if (ref->u.c.component->attr.pointer
4586 || ref->u.c.component->attr.proc_pointer
4587 || (ref->u.c.component->ts.type == BT_CLASS
4588 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4590 gfc_error ("Component to the right of a part reference "
4591 "with nonzero rank must not have the POINTER "
4592 "attribute at %L", &expr->where);
4593 return false;
4595 else if (ref->u.c.component->attr.allocatable
4596 || (ref->u.c.component->ts.type == BT_CLASS
4597 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4600 gfc_error ("Component to the right of a part reference "
4601 "with nonzero rank must not have the ALLOCATABLE "
4602 "attribute at %L", &expr->where);
4603 return false;
4607 n_components++;
4608 break;
4610 case REF_SUBSTRING:
4611 break;
4614 if (((ref->type == REF_COMPONENT && n_components > 1)
4615 || ref->next == NULL)
4616 && current_part_dimension
4617 && seen_part_dimension)
4619 gfc_error ("Two or more part references with nonzero rank must "
4620 "not be specified at %L", &expr->where);
4621 return false;
4624 if (ref->type == REF_COMPONENT)
4626 if (current_part_dimension)
4627 seen_part_dimension = 1;
4629 /* reset to make sure */
4630 current_part_dimension = 0;
4634 return true;
4638 /* Given an expression, determine its shape. This is easier than it sounds.
4639 Leaves the shape array NULL if it is not possible to determine the shape. */
4641 static void
4642 expression_shape (gfc_expr *e)
4644 mpz_t array[GFC_MAX_DIMENSIONS];
4645 int i;
4647 if (e->rank <= 0 || e->shape != NULL)
4648 return;
4650 for (i = 0; i < e->rank; i++)
4651 if (!gfc_array_dimen_size (e, i, &array[i]))
4652 goto fail;
4654 e->shape = gfc_get_shape (e->rank);
4656 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4658 return;
4660 fail:
4661 for (i--; i >= 0; i--)
4662 mpz_clear (array[i]);
4666 /* Given a variable expression node, compute the rank of the expression by
4667 examining the base symbol and any reference structures it may have. */
4669 static void
4670 expression_rank (gfc_expr *e)
4672 gfc_ref *ref;
4673 int i, rank;
4675 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4676 could lead to serious confusion... */
4677 gcc_assert (e->expr_type != EXPR_COMPCALL);
4679 if (e->ref == NULL)
4681 if (e->expr_type == EXPR_ARRAY)
4682 goto done;
4683 /* Constructors can have a rank different from one via RESHAPE(). */
4685 if (e->symtree == NULL)
4687 e->rank = 0;
4688 goto done;
4691 e->rank = (e->symtree->n.sym->as == NULL)
4692 ? 0 : e->symtree->n.sym->as->rank;
4693 goto done;
4696 rank = 0;
4698 for (ref = e->ref; ref; ref = ref->next)
4700 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4701 && ref->u.c.component->attr.function && !ref->next)
4702 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4704 if (ref->type != REF_ARRAY)
4705 continue;
4707 if (ref->u.ar.type == AR_FULL)
4709 rank = ref->u.ar.as->rank;
4710 break;
4713 if (ref->u.ar.type == AR_SECTION)
4715 /* Figure out the rank of the section. */
4716 if (rank != 0)
4717 gfc_internal_error ("expression_rank(): Two array specs");
4719 for (i = 0; i < ref->u.ar.dimen; i++)
4720 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4721 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4722 rank++;
4724 break;
4728 e->rank = rank;
4730 done:
4731 expression_shape (e);
4735 /* Resolve a variable expression. */
4737 static bool
4738 resolve_variable (gfc_expr *e)
4740 gfc_symbol *sym;
4741 bool t;
4743 t = true;
4745 if (e->symtree == NULL)
4746 return false;
4747 sym = e->symtree->n.sym;
4749 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4750 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4751 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4753 if (!actual_arg || inquiry_argument)
4755 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4756 "be used as actual argument", sym->name, &e->where);
4757 return false;
4760 /* TS 29113, 407b. */
4761 else if (e->ts.type == BT_ASSUMED)
4763 if (!actual_arg)
4765 gfc_error ("Assumed-type variable %s at %L may only be used "
4766 "as actual argument", sym->name, &e->where);
4767 return false;
4769 else if (inquiry_argument && !first_actual_arg)
4771 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4772 for all inquiry functions in resolve_function; the reason is
4773 that the function-name resolution happens too late in that
4774 function. */
4775 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4776 "an inquiry function shall be the first argument",
4777 sym->name, &e->where);
4778 return false;
4781 /* TS 29113, C535b. */
4782 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4783 && CLASS_DATA (sym)->as
4784 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4785 || (sym->ts.type != BT_CLASS && sym->as
4786 && sym->as->type == AS_ASSUMED_RANK))
4788 if (!actual_arg)
4790 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4791 "actual argument", sym->name, &e->where);
4792 return false;
4794 else if (inquiry_argument && !first_actual_arg)
4796 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4797 for all inquiry functions in resolve_function; the reason is
4798 that the function-name resolution happens too late in that
4799 function. */
4800 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4801 "to an inquiry function shall be the first argument",
4802 sym->name, &e->where);
4803 return false;
4807 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
4808 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4809 && e->ref->next == NULL))
4811 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4812 "a subobject reference", sym->name, &e->ref->u.ar.where);
4813 return false;
4815 /* TS 29113, 407b. */
4816 else if (e->ts.type == BT_ASSUMED && e->ref
4817 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4818 && e->ref->next == NULL))
4820 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4821 "reference", sym->name, &e->ref->u.ar.where);
4822 return false;
4825 /* TS 29113, C535b. */
4826 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4827 && CLASS_DATA (sym)->as
4828 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4829 || (sym->ts.type != BT_CLASS && sym->as
4830 && sym->as->type == AS_ASSUMED_RANK))
4831 && e->ref
4832 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4833 && e->ref->next == NULL))
4835 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
4836 "reference", sym->name, &e->ref->u.ar.where);
4837 return false;
4841 /* If this is an associate-name, it may be parsed with an array reference
4842 in error even though the target is scalar. Fail directly in this case.
4843 TODO Understand why class scalar expressions must be excluded. */
4844 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
4846 if (sym->ts.type == BT_CLASS)
4847 gfc_fix_class_refs (e);
4848 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
4849 return false;
4852 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
4853 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
4855 /* On the other hand, the parser may not have known this is an array;
4856 in this case, we have to add a FULL reference. */
4857 if (sym->assoc && sym->attr.dimension && !e->ref)
4859 e->ref = gfc_get_ref ();
4860 e->ref->type = REF_ARRAY;
4861 e->ref->u.ar.type = AR_FULL;
4862 e->ref->u.ar.dimen = 0;
4865 if (e->ref && !resolve_ref (e))
4866 return false;
4868 if (sym->attr.flavor == FL_PROCEDURE
4869 && (!sym->attr.function
4870 || (sym->attr.function && sym->result
4871 && sym->result->attr.proc_pointer
4872 && !sym->result->attr.function)))
4874 e->ts.type = BT_PROCEDURE;
4875 goto resolve_procedure;
4878 if (sym->ts.type != BT_UNKNOWN)
4879 gfc_variable_attr (e, &e->ts);
4880 else
4882 /* Must be a simple variable reference. */
4883 if (!gfc_set_default_type (sym, 1, sym->ns))
4884 return false;
4885 e->ts = sym->ts;
4888 if (check_assumed_size_reference (sym, e))
4889 return false;
4891 /* Deal with forward references to entries during resolve_code, to
4892 satisfy, at least partially, 12.5.2.5. */
4893 if (gfc_current_ns->entries
4894 && current_entry_id == sym->entry_id
4895 && cs_base
4896 && cs_base->current
4897 && cs_base->current->op != EXEC_ENTRY)
4899 gfc_entry_list *entry;
4900 gfc_formal_arglist *formal;
4901 int n;
4902 bool seen, saved_specification_expr;
4904 /* If the symbol is a dummy... */
4905 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4907 entry = gfc_current_ns->entries;
4908 seen = false;
4910 /* ...test if the symbol is a parameter of previous entries. */
4911 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4912 for (formal = entry->sym->formal; formal; formal = formal->next)
4914 if (formal->sym && sym->name == formal->sym->name)
4916 seen = true;
4917 break;
4921 /* If it has not been seen as a dummy, this is an error. */
4922 if (!seen)
4924 if (specification_expr)
4925 gfc_error ("Variable '%s', used in a specification expression"
4926 ", is referenced at %L before the ENTRY statement "
4927 "in which it is a parameter",
4928 sym->name, &cs_base->current->loc);
4929 else
4930 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4931 "statement in which it is a parameter",
4932 sym->name, &cs_base->current->loc);
4933 t = false;
4937 /* Now do the same check on the specification expressions. */
4938 saved_specification_expr = specification_expr;
4939 specification_expr = true;
4940 if (sym->ts.type == BT_CHARACTER
4941 && !gfc_resolve_expr (sym->ts.u.cl->length))
4942 t = false;
4944 if (sym->as)
4945 for (n = 0; n < sym->as->rank; n++)
4947 if (!gfc_resolve_expr (sym->as->lower[n]))
4948 t = false;
4949 if (!gfc_resolve_expr (sym->as->upper[n]))
4950 t = false;
4952 specification_expr = saved_specification_expr;
4954 if (t)
4955 /* Update the symbol's entry level. */
4956 sym->entry_id = current_entry_id + 1;
4959 /* If a symbol has been host_associated mark it. This is used latter,
4960 to identify if aliasing is possible via host association. */
4961 if (sym->attr.flavor == FL_VARIABLE
4962 && gfc_current_ns->parent
4963 && (gfc_current_ns->parent == sym->ns
4964 || (gfc_current_ns->parent->parent
4965 && gfc_current_ns->parent->parent == sym->ns)))
4966 sym->attr.host_assoc = 1;
4968 resolve_procedure:
4969 if (t && !resolve_procedure_expression (e))
4970 t = false;
4972 /* F2008, C617 and C1229. */
4973 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
4974 && gfc_is_coindexed (e))
4976 gfc_ref *ref, *ref2 = NULL;
4978 for (ref = e->ref; ref; ref = ref->next)
4980 if (ref->type == REF_COMPONENT)
4981 ref2 = ref;
4982 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4983 break;
4986 for ( ; ref; ref = ref->next)
4987 if (ref->type == REF_COMPONENT)
4988 break;
4990 /* Expression itself is not coindexed object. */
4991 if (ref && e->ts.type == BT_CLASS)
4993 gfc_error ("Polymorphic subobject of coindexed object at %L",
4994 &e->where);
4995 t = false;
4998 /* Expression itself is coindexed object. */
4999 if (ref == NULL)
5001 gfc_component *c;
5002 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5003 for ( ; c; c = c->next)
5004 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5006 gfc_error ("Coindexed object with polymorphic allocatable "
5007 "subcomponent at %L", &e->where);
5008 t = false;
5009 break;
5014 return t;
5018 /* Checks to see that the correct symbol has been host associated.
5019 The only situation where this arises is that in which a twice
5020 contained function is parsed after the host association is made.
5021 Therefore, on detecting this, change the symbol in the expression
5022 and convert the array reference into an actual arglist if the old
5023 symbol is a variable. */
5024 static bool
5025 check_host_association (gfc_expr *e)
5027 gfc_symbol *sym, *old_sym;
5028 gfc_symtree *st;
5029 int n;
5030 gfc_ref *ref;
5031 gfc_actual_arglist *arg, *tail = NULL;
5032 bool retval = e->expr_type == EXPR_FUNCTION;
5034 /* If the expression is the result of substitution in
5035 interface.c(gfc_extend_expr) because there is no way in
5036 which the host association can be wrong. */
5037 if (e->symtree == NULL
5038 || e->symtree->n.sym == NULL
5039 || e->user_operator)
5040 return retval;
5042 old_sym = e->symtree->n.sym;
5044 if (gfc_current_ns->parent
5045 && old_sym->ns != gfc_current_ns)
5047 /* Use the 'USE' name so that renamed module symbols are
5048 correctly handled. */
5049 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5051 if (sym && old_sym != sym
5052 && sym->ts.type == old_sym->ts.type
5053 && sym->attr.flavor == FL_PROCEDURE
5054 && sym->attr.contained)
5056 /* Clear the shape, since it might not be valid. */
5057 gfc_free_shape (&e->shape, e->rank);
5059 /* Give the expression the right symtree! */
5060 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5061 gcc_assert (st != NULL);
5063 if (old_sym->attr.flavor == FL_PROCEDURE
5064 || e->expr_type == EXPR_FUNCTION)
5066 /* Original was function so point to the new symbol, since
5067 the actual argument list is already attached to the
5068 expression. */
5069 e->value.function.esym = NULL;
5070 e->symtree = st;
5072 else
5074 /* Original was variable so convert array references into
5075 an actual arglist. This does not need any checking now
5076 since resolve_function will take care of it. */
5077 e->value.function.actual = NULL;
5078 e->expr_type = EXPR_FUNCTION;
5079 e->symtree = st;
5081 /* Ambiguity will not arise if the array reference is not
5082 the last reference. */
5083 for (ref = e->ref; ref; ref = ref->next)
5084 if (ref->type == REF_ARRAY && ref->next == NULL)
5085 break;
5087 gcc_assert (ref->type == REF_ARRAY);
5089 /* Grab the start expressions from the array ref and
5090 copy them into actual arguments. */
5091 for (n = 0; n < ref->u.ar.dimen; n++)
5093 arg = gfc_get_actual_arglist ();
5094 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5095 if (e->value.function.actual == NULL)
5096 tail = e->value.function.actual = arg;
5097 else
5099 tail->next = arg;
5100 tail = arg;
5104 /* Dump the reference list and set the rank. */
5105 gfc_free_ref_list (e->ref);
5106 e->ref = NULL;
5107 e->rank = sym->as ? sym->as->rank : 0;
5110 gfc_resolve_expr (e);
5111 sym->refs++;
5114 /* This might have changed! */
5115 return e->expr_type == EXPR_FUNCTION;
5119 static void
5120 gfc_resolve_character_operator (gfc_expr *e)
5122 gfc_expr *op1 = e->value.op.op1;
5123 gfc_expr *op2 = e->value.op.op2;
5124 gfc_expr *e1 = NULL;
5125 gfc_expr *e2 = NULL;
5127 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5129 if (op1->ts.u.cl && op1->ts.u.cl->length)
5130 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5131 else if (op1->expr_type == EXPR_CONSTANT)
5132 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5133 op1->value.character.length);
5135 if (op2->ts.u.cl && op2->ts.u.cl->length)
5136 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5137 else if (op2->expr_type == EXPR_CONSTANT)
5138 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5139 op2->value.character.length);
5141 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5143 if (!e1 || !e2)
5145 gfc_free_expr (e1);
5146 gfc_free_expr (e2);
5148 return;
5151 e->ts.u.cl->length = gfc_add (e1, e2);
5152 e->ts.u.cl->length->ts.type = BT_INTEGER;
5153 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5154 gfc_simplify_expr (e->ts.u.cl->length, 0);
5155 gfc_resolve_expr (e->ts.u.cl->length);
5157 return;
5161 /* Ensure that an character expression has a charlen and, if possible, a
5162 length expression. */
5164 static void
5165 fixup_charlen (gfc_expr *e)
5167 /* The cases fall through so that changes in expression type and the need
5168 for multiple fixes are picked up. In all circumstances, a charlen should
5169 be available for the middle end to hang a backend_decl on. */
5170 switch (e->expr_type)
5172 case EXPR_OP:
5173 gfc_resolve_character_operator (e);
5175 case EXPR_ARRAY:
5176 if (e->expr_type == EXPR_ARRAY)
5177 gfc_resolve_character_array_constructor (e);
5179 case EXPR_SUBSTRING:
5180 if (!e->ts.u.cl && e->ref)
5181 gfc_resolve_substring_charlen (e);
5183 default:
5184 if (!e->ts.u.cl)
5185 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5187 break;
5192 /* Update an actual argument to include the passed-object for type-bound
5193 procedures at the right position. */
5195 static gfc_actual_arglist*
5196 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5197 const char *name)
5199 gcc_assert (argpos > 0);
5201 if (argpos == 1)
5203 gfc_actual_arglist* result;
5205 result = gfc_get_actual_arglist ();
5206 result->expr = po;
5207 result->next = lst;
5208 if (name)
5209 result->name = name;
5211 return result;
5214 if (lst)
5215 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5216 else
5217 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5218 return lst;
5222 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5224 static gfc_expr*
5225 extract_compcall_passed_object (gfc_expr* e)
5227 gfc_expr* po;
5229 gcc_assert (e->expr_type == EXPR_COMPCALL);
5231 if (e->value.compcall.base_object)
5232 po = gfc_copy_expr (e->value.compcall.base_object);
5233 else
5235 po = gfc_get_expr ();
5236 po->expr_type = EXPR_VARIABLE;
5237 po->symtree = e->symtree;
5238 po->ref = gfc_copy_ref (e->ref);
5239 po->where = e->where;
5242 if (!gfc_resolve_expr (po))
5243 return NULL;
5245 return po;
5249 /* Update the arglist of an EXPR_COMPCALL expression to include the
5250 passed-object. */
5252 static bool
5253 update_compcall_arglist (gfc_expr* e)
5255 gfc_expr* po;
5256 gfc_typebound_proc* tbp;
5258 tbp = e->value.compcall.tbp;
5260 if (tbp->error)
5261 return false;
5263 po = extract_compcall_passed_object (e);
5264 if (!po)
5265 return false;
5267 if (tbp->nopass || e->value.compcall.ignore_pass)
5269 gfc_free_expr (po);
5270 return true;
5273 gcc_assert (tbp->pass_arg_num > 0);
5274 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5275 tbp->pass_arg_num,
5276 tbp->pass_arg);
5278 return true;
5282 /* Extract the passed object from a PPC call (a copy of it). */
5284 static gfc_expr*
5285 extract_ppc_passed_object (gfc_expr *e)
5287 gfc_expr *po;
5288 gfc_ref **ref;
5290 po = gfc_get_expr ();
5291 po->expr_type = EXPR_VARIABLE;
5292 po->symtree = e->symtree;
5293 po->ref = gfc_copy_ref (e->ref);
5294 po->where = e->where;
5296 /* Remove PPC reference. */
5297 ref = &po->ref;
5298 while ((*ref)->next)
5299 ref = &(*ref)->next;
5300 gfc_free_ref_list (*ref);
5301 *ref = NULL;
5303 if (!gfc_resolve_expr (po))
5304 return NULL;
5306 return po;
5310 /* Update the actual arglist of a procedure pointer component to include the
5311 passed-object. */
5313 static bool
5314 update_ppc_arglist (gfc_expr* e)
5316 gfc_expr* po;
5317 gfc_component *ppc;
5318 gfc_typebound_proc* tb;
5320 ppc = gfc_get_proc_ptr_comp (e);
5321 if (!ppc)
5322 return false;
5324 tb = ppc->tb;
5326 if (tb->error)
5327 return false;
5328 else if (tb->nopass)
5329 return true;
5331 po = extract_ppc_passed_object (e);
5332 if (!po)
5333 return false;
5335 /* F08:R739. */
5336 if (po->rank != 0)
5338 gfc_error ("Passed-object at %L must be scalar", &e->where);
5339 return false;
5342 /* F08:C611. */
5343 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5345 gfc_error ("Base object for procedure-pointer component call at %L is of"
5346 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
5347 return false;
5350 gcc_assert (tb->pass_arg_num > 0);
5351 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5352 tb->pass_arg_num,
5353 tb->pass_arg);
5355 return true;
5359 /* Check that the object a TBP is called on is valid, i.e. it must not be
5360 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5362 static bool
5363 check_typebound_baseobject (gfc_expr* e)
5365 gfc_expr* base;
5366 bool return_value = false;
5368 base = extract_compcall_passed_object (e);
5369 if (!base)
5370 return false;
5372 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5374 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
5375 return false;
5377 /* F08:C611. */
5378 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5380 gfc_error ("Base object for type-bound procedure call at %L is of"
5381 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5382 goto cleanup;
5385 /* F08:C1230. If the procedure called is NOPASS,
5386 the base object must be scalar. */
5387 if (e->value.compcall.tbp->nopass && base->rank != 0)
5389 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5390 " be scalar", &e->where);
5391 goto cleanup;
5394 return_value = true;
5396 cleanup:
5397 gfc_free_expr (base);
5398 return return_value;
5402 /* Resolve a call to a type-bound procedure, either function or subroutine,
5403 statically from the data in an EXPR_COMPCALL expression. The adapted
5404 arglist and the target-procedure symtree are returned. */
5406 static bool
5407 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5408 gfc_actual_arglist** actual)
5410 gcc_assert (e->expr_type == EXPR_COMPCALL);
5411 gcc_assert (!e->value.compcall.tbp->is_generic);
5413 /* Update the actual arglist for PASS. */
5414 if (!update_compcall_arglist (e))
5415 return false;
5417 *actual = e->value.compcall.actual;
5418 *target = e->value.compcall.tbp->u.specific;
5420 gfc_free_ref_list (e->ref);
5421 e->ref = NULL;
5422 e->value.compcall.actual = NULL;
5424 /* If we find a deferred typebound procedure, check for derived types
5425 that an overriding typebound procedure has not been missed. */
5426 if (e->value.compcall.name
5427 && !e->value.compcall.tbp->non_overridable
5428 && e->value.compcall.base_object
5429 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5431 gfc_symtree *st;
5432 gfc_symbol *derived;
5434 /* Use the derived type of the base_object. */
5435 derived = e->value.compcall.base_object->ts.u.derived;
5436 st = NULL;
5438 /* If necessary, go through the inheritance chain. */
5439 while (!st && derived)
5441 /* Look for the typebound procedure 'name'. */
5442 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5443 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5444 e->value.compcall.name);
5445 if (!st)
5446 derived = gfc_get_derived_super_type (derived);
5449 /* Now find the specific name in the derived type namespace. */
5450 if (st && st->n.tb && st->n.tb->u.specific)
5451 gfc_find_sym_tree (st->n.tb->u.specific->name,
5452 derived->ns, 1, &st);
5453 if (st)
5454 *target = st;
5456 return true;
5460 /* Get the ultimate declared type from an expression. In addition,
5461 return the last class/derived type reference and the copy of the
5462 reference list. If check_types is set true, derived types are
5463 identified as well as class references. */
5464 static gfc_symbol*
5465 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5466 gfc_expr *e, bool check_types)
5468 gfc_symbol *declared;
5469 gfc_ref *ref;
5471 declared = NULL;
5472 if (class_ref)
5473 *class_ref = NULL;
5474 if (new_ref)
5475 *new_ref = gfc_copy_ref (e->ref);
5477 for (ref = e->ref; ref; ref = ref->next)
5479 if (ref->type != REF_COMPONENT)
5480 continue;
5482 if ((ref->u.c.component->ts.type == BT_CLASS
5483 || (check_types && ref->u.c.component->ts.type == BT_DERIVED))
5484 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5486 declared = ref->u.c.component->ts.u.derived;
5487 if (class_ref)
5488 *class_ref = ref;
5492 if (declared == NULL)
5493 declared = e->symtree->n.sym->ts.u.derived;
5495 return declared;
5499 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5500 which of the specific bindings (if any) matches the arglist and transform
5501 the expression into a call of that binding. */
5503 static bool
5504 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5506 gfc_typebound_proc* genproc;
5507 const char* genname;
5508 gfc_symtree *st;
5509 gfc_symbol *derived;
5511 gcc_assert (e->expr_type == EXPR_COMPCALL);
5512 genname = e->value.compcall.name;
5513 genproc = e->value.compcall.tbp;
5515 if (!genproc->is_generic)
5516 return true;
5518 /* Try the bindings on this type and in the inheritance hierarchy. */
5519 for (; genproc; genproc = genproc->overridden)
5521 gfc_tbp_generic* g;
5523 gcc_assert (genproc->is_generic);
5524 for (g = genproc->u.generic; g; g = g->next)
5526 gfc_symbol* target;
5527 gfc_actual_arglist* args;
5528 bool matches;
5530 gcc_assert (g->specific);
5532 if (g->specific->error)
5533 continue;
5535 target = g->specific->u.specific->n.sym;
5537 /* Get the right arglist by handling PASS/NOPASS. */
5538 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5539 if (!g->specific->nopass)
5541 gfc_expr* po;
5542 po = extract_compcall_passed_object (e);
5543 if (!po)
5545 gfc_free_actual_arglist (args);
5546 return false;
5549 gcc_assert (g->specific->pass_arg_num > 0);
5550 gcc_assert (!g->specific->error);
5551 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5552 g->specific->pass_arg);
5554 resolve_actual_arglist (args, target->attr.proc,
5555 is_external_proc (target)
5556 && gfc_sym_get_dummy_args (target) == NULL);
5558 /* Check if this arglist matches the formal. */
5559 matches = gfc_arglist_matches_symbol (&args, target);
5561 /* Clean up and break out of the loop if we've found it. */
5562 gfc_free_actual_arglist (args);
5563 if (matches)
5565 e->value.compcall.tbp = g->specific;
5566 genname = g->specific_st->name;
5567 /* Pass along the name for CLASS methods, where the vtab
5568 procedure pointer component has to be referenced. */
5569 if (name)
5570 *name = genname;
5571 goto success;
5576 /* Nothing matching found! */
5577 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5578 " '%s' at %L", genname, &e->where);
5579 return false;
5581 success:
5582 /* Make sure that we have the right specific instance for the name. */
5583 derived = get_declared_from_expr (NULL, NULL, e, true);
5585 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5586 if (st)
5587 e->value.compcall.tbp = st->n.tb;
5589 return true;
5593 /* Resolve a call to a type-bound subroutine. */
5595 static bool
5596 resolve_typebound_call (gfc_code* c, const char **name)
5598 gfc_actual_arglist* newactual;
5599 gfc_symtree* target;
5601 /* Check that's really a SUBROUTINE. */
5602 if (!c->expr1->value.compcall.tbp->subroutine)
5604 gfc_error ("'%s' at %L should be a SUBROUTINE",
5605 c->expr1->value.compcall.name, &c->loc);
5606 return false;
5609 if (!check_typebound_baseobject (c->expr1))
5610 return false;
5612 /* Pass along the name for CLASS methods, where the vtab
5613 procedure pointer component has to be referenced. */
5614 if (name)
5615 *name = c->expr1->value.compcall.name;
5617 if (!resolve_typebound_generic_call (c->expr1, name))
5618 return false;
5620 /* Transform into an ordinary EXEC_CALL for now. */
5622 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5623 return false;
5625 c->ext.actual = newactual;
5626 c->symtree = target;
5627 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5629 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5631 gfc_free_expr (c->expr1);
5632 c->expr1 = gfc_get_expr ();
5633 c->expr1->expr_type = EXPR_FUNCTION;
5634 c->expr1->symtree = target;
5635 c->expr1->where = c->loc;
5637 return resolve_call (c);
5641 /* Resolve a component-call expression. */
5642 static bool
5643 resolve_compcall (gfc_expr* e, const char **name)
5645 gfc_actual_arglist* newactual;
5646 gfc_symtree* target;
5648 /* Check that's really a FUNCTION. */
5649 if (!e->value.compcall.tbp->function)
5651 gfc_error ("'%s' at %L should be a FUNCTION",
5652 e->value.compcall.name, &e->where);
5653 return false;
5656 /* These must not be assign-calls! */
5657 gcc_assert (!e->value.compcall.assign);
5659 if (!check_typebound_baseobject (e))
5660 return false;
5662 /* Pass along the name for CLASS methods, where the vtab
5663 procedure pointer component has to be referenced. */
5664 if (name)
5665 *name = e->value.compcall.name;
5667 if (!resolve_typebound_generic_call (e, name))
5668 return false;
5669 gcc_assert (!e->value.compcall.tbp->is_generic);
5671 /* Take the rank from the function's symbol. */
5672 if (e->value.compcall.tbp->u.specific->n.sym->as)
5673 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5675 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5676 arglist to the TBP's binding target. */
5678 if (!resolve_typebound_static (e, &target, &newactual))
5679 return false;
5681 e->value.function.actual = newactual;
5682 e->value.function.name = NULL;
5683 e->value.function.esym = target->n.sym;
5684 e->value.function.isym = NULL;
5685 e->symtree = target;
5686 e->ts = target->n.sym->ts;
5687 e->expr_type = EXPR_FUNCTION;
5689 /* Resolution is not necessary if this is a class subroutine; this
5690 function only has to identify the specific proc. Resolution of
5691 the call will be done next in resolve_typebound_call. */
5692 return gfc_resolve_expr (e);
5696 static bool resolve_fl_derived (gfc_symbol *sym);
5699 /* Resolve a typebound function, or 'method'. First separate all
5700 the non-CLASS references by calling resolve_compcall directly. */
5702 static bool
5703 resolve_typebound_function (gfc_expr* e)
5705 gfc_symbol *declared;
5706 gfc_component *c;
5707 gfc_ref *new_ref;
5708 gfc_ref *class_ref;
5709 gfc_symtree *st;
5710 const char *name;
5711 gfc_typespec ts;
5712 gfc_expr *expr;
5713 bool overridable;
5715 st = e->symtree;
5717 /* Deal with typebound operators for CLASS objects. */
5718 expr = e->value.compcall.base_object;
5719 overridable = !e->value.compcall.tbp->non_overridable;
5720 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5722 /* If the base_object is not a variable, the corresponding actual
5723 argument expression must be stored in e->base_expression so
5724 that the corresponding tree temporary can be used as the base
5725 object in gfc_conv_procedure_call. */
5726 if (expr->expr_type != EXPR_VARIABLE)
5728 gfc_actual_arglist *args;
5730 for (args= e->value.function.actual; args; args = args->next)
5732 if (expr == args->expr)
5733 expr = args->expr;
5737 /* Since the typebound operators are generic, we have to ensure
5738 that any delays in resolution are corrected and that the vtab
5739 is present. */
5740 ts = expr->ts;
5741 declared = ts.u.derived;
5742 c = gfc_find_component (declared, "_vptr", true, true);
5743 if (c->ts.u.derived == NULL)
5744 c->ts.u.derived = gfc_find_derived_vtab (declared);
5746 if (!resolve_compcall (e, &name))
5747 return false;
5749 /* Use the generic name if it is there. */
5750 name = name ? name : e->value.function.esym->name;
5751 e->symtree = expr->symtree;
5752 e->ref = gfc_copy_ref (expr->ref);
5753 get_declared_from_expr (&class_ref, NULL, e, false);
5755 /* Trim away the extraneous references that emerge from nested
5756 use of interface.c (extend_expr). */
5757 if (class_ref && class_ref->next)
5759 gfc_free_ref_list (class_ref->next);
5760 class_ref->next = NULL;
5762 else if (e->ref && !class_ref)
5764 gfc_free_ref_list (e->ref);
5765 e->ref = NULL;
5768 gfc_add_vptr_component (e);
5769 gfc_add_component_ref (e, name);
5770 e->value.function.esym = NULL;
5771 if (expr->expr_type != EXPR_VARIABLE)
5772 e->base_expr = expr;
5773 return true;
5776 if (st == NULL)
5777 return resolve_compcall (e, NULL);
5779 if (!resolve_ref (e))
5780 return false;
5782 /* Get the CLASS declared type. */
5783 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
5785 if (!resolve_fl_derived (declared))
5786 return false;
5788 /* Weed out cases of the ultimate component being a derived type. */
5789 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5790 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5792 gfc_free_ref_list (new_ref);
5793 return resolve_compcall (e, NULL);
5796 c = gfc_find_component (declared, "_data", true, true);
5797 declared = c->ts.u.derived;
5799 /* Treat the call as if it is a typebound procedure, in order to roll
5800 out the correct name for the specific function. */
5801 if (!resolve_compcall (e, &name))
5803 gfc_free_ref_list (new_ref);
5804 return false;
5806 ts = e->ts;
5808 if (overridable)
5810 /* Convert the expression to a procedure pointer component call. */
5811 e->value.function.esym = NULL;
5812 e->symtree = st;
5814 if (new_ref)
5815 e->ref = new_ref;
5817 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5818 gfc_add_vptr_component (e);
5819 gfc_add_component_ref (e, name);
5821 /* Recover the typespec for the expression. This is really only
5822 necessary for generic procedures, where the additional call
5823 to gfc_add_component_ref seems to throw the collection of the
5824 correct typespec. */
5825 e->ts = ts;
5827 else if (new_ref)
5828 gfc_free_ref_list (new_ref);
5830 return true;
5833 /* Resolve a typebound subroutine, or 'method'. First separate all
5834 the non-CLASS references by calling resolve_typebound_call
5835 directly. */
5837 static bool
5838 resolve_typebound_subroutine (gfc_code *code)
5840 gfc_symbol *declared;
5841 gfc_component *c;
5842 gfc_ref *new_ref;
5843 gfc_ref *class_ref;
5844 gfc_symtree *st;
5845 const char *name;
5846 gfc_typespec ts;
5847 gfc_expr *expr;
5848 bool overridable;
5850 st = code->expr1->symtree;
5852 /* Deal with typebound operators for CLASS objects. */
5853 expr = code->expr1->value.compcall.base_object;
5854 overridable = !code->expr1->value.compcall.tbp->non_overridable;
5855 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
5857 /* If the base_object is not a variable, the corresponding actual
5858 argument expression must be stored in e->base_expression so
5859 that the corresponding tree temporary can be used as the base
5860 object in gfc_conv_procedure_call. */
5861 if (expr->expr_type != EXPR_VARIABLE)
5863 gfc_actual_arglist *args;
5865 args= code->expr1->value.function.actual;
5866 for (; args; args = args->next)
5867 if (expr == args->expr)
5868 expr = args->expr;
5871 /* Since the typebound operators are generic, we have to ensure
5872 that any delays in resolution are corrected and that the vtab
5873 is present. */
5874 declared = expr->ts.u.derived;
5875 c = gfc_find_component (declared, "_vptr", true, true);
5876 if (c->ts.u.derived == NULL)
5877 c->ts.u.derived = gfc_find_derived_vtab (declared);
5879 if (!resolve_typebound_call (code, &name))
5880 return false;
5882 /* Use the generic name if it is there. */
5883 name = name ? name : code->expr1->value.function.esym->name;
5884 code->expr1->symtree = expr->symtree;
5885 code->expr1->ref = gfc_copy_ref (expr->ref);
5887 /* Trim away the extraneous references that emerge from nested
5888 use of interface.c (extend_expr). */
5889 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
5890 if (class_ref && class_ref->next)
5892 gfc_free_ref_list (class_ref->next);
5893 class_ref->next = NULL;
5895 else if (code->expr1->ref && !class_ref)
5897 gfc_free_ref_list (code->expr1->ref);
5898 code->expr1->ref = NULL;
5901 /* Now use the procedure in the vtable. */
5902 gfc_add_vptr_component (code->expr1);
5903 gfc_add_component_ref (code->expr1, name);
5904 code->expr1->value.function.esym = NULL;
5905 if (expr->expr_type != EXPR_VARIABLE)
5906 code->expr1->base_expr = expr;
5907 return true;
5910 if (st == NULL)
5911 return resolve_typebound_call (code, NULL);
5913 if (!resolve_ref (code->expr1))
5914 return false;
5916 /* Get the CLASS declared type. */
5917 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
5919 /* Weed out cases of the ultimate component being a derived type. */
5920 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5921 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5923 gfc_free_ref_list (new_ref);
5924 return resolve_typebound_call (code, NULL);
5927 if (!resolve_typebound_call (code, &name))
5929 gfc_free_ref_list (new_ref);
5930 return false;
5932 ts = code->expr1->ts;
5934 if (overridable)
5936 /* Convert the expression to a procedure pointer component call. */
5937 code->expr1->value.function.esym = NULL;
5938 code->expr1->symtree = st;
5940 if (new_ref)
5941 code->expr1->ref = new_ref;
5943 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5944 gfc_add_vptr_component (code->expr1);
5945 gfc_add_component_ref (code->expr1, name);
5947 /* Recover the typespec for the expression. This is really only
5948 necessary for generic procedures, where the additional call
5949 to gfc_add_component_ref seems to throw the collection of the
5950 correct typespec. */
5951 code->expr1->ts = ts;
5953 else if (new_ref)
5954 gfc_free_ref_list (new_ref);
5956 return true;
5960 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5962 static bool
5963 resolve_ppc_call (gfc_code* c)
5965 gfc_component *comp;
5967 comp = gfc_get_proc_ptr_comp (c->expr1);
5968 gcc_assert (comp != NULL);
5970 c->resolved_sym = c->expr1->symtree->n.sym;
5971 c->expr1->expr_type = EXPR_VARIABLE;
5973 if (!comp->attr.subroutine)
5974 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5976 if (!resolve_ref (c->expr1))
5977 return false;
5979 if (!update_ppc_arglist (c->expr1))
5980 return false;
5982 c->ext.actual = c->expr1->value.compcall.actual;
5984 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5985 !(comp->ts.interface
5986 && comp->ts.interface->formal)))
5987 return false;
5989 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5991 return true;
5995 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5997 static bool
5998 resolve_expr_ppc (gfc_expr* e)
6000 gfc_component *comp;
6002 comp = gfc_get_proc_ptr_comp (e);
6003 gcc_assert (comp != NULL);
6005 /* Convert to EXPR_FUNCTION. */
6006 e->expr_type = EXPR_FUNCTION;
6007 e->value.function.isym = NULL;
6008 e->value.function.actual = e->value.compcall.actual;
6009 e->ts = comp->ts;
6010 if (comp->as != NULL)
6011 e->rank = comp->as->rank;
6013 if (!comp->attr.function)
6014 gfc_add_function (&comp->attr, comp->name, &e->where);
6016 if (!resolve_ref (e))
6017 return false;
6019 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6020 !(comp->ts.interface
6021 && comp->ts.interface->formal)))
6022 return false;
6024 if (!update_ppc_arglist (e))
6025 return false;
6027 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6029 return true;
6033 static bool
6034 gfc_is_expandable_expr (gfc_expr *e)
6036 gfc_constructor *con;
6038 if (e->expr_type == EXPR_ARRAY)
6040 /* Traverse the constructor looking for variables that are flavor
6041 parameter. Parameters must be expanded since they are fully used at
6042 compile time. */
6043 con = gfc_constructor_first (e->value.constructor);
6044 for (; con; con = gfc_constructor_next (con))
6046 if (con->expr->expr_type == EXPR_VARIABLE
6047 && con->expr->symtree
6048 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6049 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6050 return true;
6051 if (con->expr->expr_type == EXPR_ARRAY
6052 && gfc_is_expandable_expr (con->expr))
6053 return true;
6057 return false;
6060 /* Resolve an expression. That is, make sure that types of operands agree
6061 with their operators, intrinsic operators are converted to function calls
6062 for overloaded types and unresolved function references are resolved. */
6064 bool
6065 gfc_resolve_expr (gfc_expr *e)
6067 bool t;
6068 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6070 if (e == NULL)
6071 return true;
6073 /* inquiry_argument only applies to variables. */
6074 inquiry_save = inquiry_argument;
6075 actual_arg_save = actual_arg;
6076 first_actual_arg_save = first_actual_arg;
6078 if (e->expr_type != EXPR_VARIABLE)
6080 inquiry_argument = false;
6081 actual_arg = false;
6082 first_actual_arg = false;
6085 switch (e->expr_type)
6087 case EXPR_OP:
6088 t = resolve_operator (e);
6089 break;
6091 case EXPR_FUNCTION:
6092 case EXPR_VARIABLE:
6094 if (check_host_association (e))
6095 t = resolve_function (e);
6096 else
6098 t = resolve_variable (e);
6099 if (t)
6100 expression_rank (e);
6103 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6104 && e->ref->type != REF_SUBSTRING)
6105 gfc_resolve_substring_charlen (e);
6107 break;
6109 case EXPR_COMPCALL:
6110 t = resolve_typebound_function (e);
6111 break;
6113 case EXPR_SUBSTRING:
6114 t = resolve_ref (e);
6115 break;
6117 case EXPR_CONSTANT:
6118 case EXPR_NULL:
6119 t = true;
6120 break;
6122 case EXPR_PPC:
6123 t = resolve_expr_ppc (e);
6124 break;
6126 case EXPR_ARRAY:
6127 t = false;
6128 if (!resolve_ref (e))
6129 break;
6131 t = gfc_resolve_array_constructor (e);
6132 /* Also try to expand a constructor. */
6133 if (t)
6135 expression_rank (e);
6136 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6137 gfc_expand_constructor (e, false);
6140 /* This provides the opportunity for the length of constructors with
6141 character valued function elements to propagate the string length
6142 to the expression. */
6143 if (t && e->ts.type == BT_CHARACTER)
6145 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6146 here rather then add a duplicate test for it above. */
6147 gfc_expand_constructor (e, false);
6148 t = gfc_resolve_character_array_constructor (e);
6151 break;
6153 case EXPR_STRUCTURE:
6154 t = resolve_ref (e);
6155 if (!t)
6156 break;
6158 t = resolve_structure_cons (e, 0);
6159 if (!t)
6160 break;
6162 t = gfc_simplify_expr (e, 0);
6163 break;
6165 default:
6166 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6169 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
6170 fixup_charlen (e);
6172 inquiry_argument = inquiry_save;
6173 actual_arg = actual_arg_save;
6174 first_actual_arg = first_actual_arg_save;
6176 return t;
6180 /* Resolve an expression from an iterator. They must be scalar and have
6181 INTEGER or (optionally) REAL type. */
6183 static bool
6184 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6185 const char *name_msgid)
6187 if (!gfc_resolve_expr (expr))
6188 return false;
6190 if (expr->rank != 0)
6192 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6193 return false;
6196 if (expr->ts.type != BT_INTEGER)
6198 if (expr->ts.type == BT_REAL)
6200 if (real_ok)
6201 return gfc_notify_std (GFC_STD_F95_DEL,
6202 "%s at %L must be integer",
6203 _(name_msgid), &expr->where);
6204 else
6206 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6207 &expr->where);
6208 return false;
6211 else
6213 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6214 return false;
6217 return true;
6221 /* Resolve the expressions in an iterator structure. If REAL_OK is
6222 false allow only INTEGER type iterators, otherwise allow REAL types.
6223 Set own_scope to true for ac-implied-do and data-implied-do as those
6224 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
6226 bool
6227 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
6229 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6230 return false;
6232 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6233 _("iterator variable")))
6234 return false;
6236 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6237 "Start expression in DO loop"))
6238 return false;
6240 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6241 "End expression in DO loop"))
6242 return false;
6244 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6245 "Step expression in DO loop"))
6246 return false;
6248 if (iter->step->expr_type == EXPR_CONSTANT)
6250 if ((iter->step->ts.type == BT_INTEGER
6251 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6252 || (iter->step->ts.type == BT_REAL
6253 && mpfr_sgn (iter->step->value.real) == 0))
6255 gfc_error ("Step expression in DO loop at %L cannot be zero",
6256 &iter->step->where);
6257 return false;
6261 /* Convert start, end, and step to the same type as var. */
6262 if (iter->start->ts.kind != iter->var->ts.kind
6263 || iter->start->ts.type != iter->var->ts.type)
6264 gfc_convert_type (iter->start, &iter->var->ts, 2);
6266 if (iter->end->ts.kind != iter->var->ts.kind
6267 || iter->end->ts.type != iter->var->ts.type)
6268 gfc_convert_type (iter->end, &iter->var->ts, 2);
6270 if (iter->step->ts.kind != iter->var->ts.kind
6271 || iter->step->ts.type != iter->var->ts.type)
6272 gfc_convert_type (iter->step, &iter->var->ts, 2);
6274 if (iter->start->expr_type == EXPR_CONSTANT
6275 && iter->end->expr_type == EXPR_CONSTANT
6276 && iter->step->expr_type == EXPR_CONSTANT)
6278 int sgn, cmp;
6279 if (iter->start->ts.type == BT_INTEGER)
6281 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6282 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6284 else
6286 sgn = mpfr_sgn (iter->step->value.real);
6287 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6289 if (gfc_option.warn_zerotrip &&
6290 ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
6291 gfc_warning ("DO loop at %L will be executed zero times"
6292 " (use -Wno-zerotrip to suppress)",
6293 &iter->step->where);
6296 return true;
6300 /* Traversal function for find_forall_index. f == 2 signals that
6301 that variable itself is not to be checked - only the references. */
6303 static bool
6304 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6306 if (expr->expr_type != EXPR_VARIABLE)
6307 return false;
6309 /* A scalar assignment */
6310 if (!expr->ref || *f == 1)
6312 if (expr->symtree->n.sym == sym)
6313 return true;
6314 else
6315 return false;
6318 if (*f == 2)
6319 *f = 1;
6320 return false;
6324 /* Check whether the FORALL index appears in the expression or not.
6325 Returns true if SYM is found in EXPR. */
6327 bool
6328 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6330 if (gfc_traverse_expr (expr, sym, forall_index, f))
6331 return true;
6332 else
6333 return false;
6337 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6338 to be a scalar INTEGER variable. The subscripts and stride are scalar
6339 INTEGERs, and if stride is a constant it must be nonzero.
6340 Furthermore "A subscript or stride in a forall-triplet-spec shall
6341 not contain a reference to any index-name in the
6342 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6344 static void
6345 resolve_forall_iterators (gfc_forall_iterator *it)
6347 gfc_forall_iterator *iter, *iter2;
6349 for (iter = it; iter; iter = iter->next)
6351 if (gfc_resolve_expr (iter->var)
6352 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6353 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6354 &iter->var->where);
6356 if (gfc_resolve_expr (iter->start)
6357 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6358 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6359 &iter->start->where);
6360 if (iter->var->ts.kind != iter->start->ts.kind)
6361 gfc_convert_type (iter->start, &iter->var->ts, 1);
6363 if (gfc_resolve_expr (iter->end)
6364 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6365 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6366 &iter->end->where);
6367 if (iter->var->ts.kind != iter->end->ts.kind)
6368 gfc_convert_type (iter->end, &iter->var->ts, 1);
6370 if (gfc_resolve_expr (iter->stride))
6372 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6373 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6374 &iter->stride->where, "INTEGER");
6376 if (iter->stride->expr_type == EXPR_CONSTANT
6377 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
6378 gfc_error ("FORALL stride expression at %L cannot be zero",
6379 &iter->stride->where);
6381 if (iter->var->ts.kind != iter->stride->ts.kind)
6382 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6385 for (iter = it; iter; iter = iter->next)
6386 for (iter2 = iter; iter2; iter2 = iter2->next)
6388 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6389 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6390 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
6391 gfc_error ("FORALL index '%s' may not appear in triplet "
6392 "specification at %L", iter->var->symtree->name,
6393 &iter2->start->where);
6398 /* Given a pointer to a symbol that is a derived type, see if it's
6399 inaccessible, i.e. if it's defined in another module and the components are
6400 PRIVATE. The search is recursive if necessary. Returns zero if no
6401 inaccessible components are found, nonzero otherwise. */
6403 static int
6404 derived_inaccessible (gfc_symbol *sym)
6406 gfc_component *c;
6408 if (sym->attr.use_assoc && sym->attr.private_comp)
6409 return 1;
6411 for (c = sym->components; c; c = c->next)
6413 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6414 return 1;
6417 return 0;
6421 /* Resolve the argument of a deallocate expression. The expression must be
6422 a pointer or a full array. */
6424 static bool
6425 resolve_deallocate_expr (gfc_expr *e)
6427 symbol_attribute attr;
6428 int allocatable, pointer;
6429 gfc_ref *ref;
6430 gfc_symbol *sym;
6431 gfc_component *c;
6432 bool unlimited;
6434 if (!gfc_resolve_expr (e))
6435 return false;
6437 if (e->expr_type != EXPR_VARIABLE)
6438 goto bad;
6440 sym = e->symtree->n.sym;
6441 unlimited = UNLIMITED_POLY(sym);
6443 if (sym->ts.type == BT_CLASS)
6445 allocatable = CLASS_DATA (sym)->attr.allocatable;
6446 pointer = CLASS_DATA (sym)->attr.class_pointer;
6448 else
6450 allocatable = sym->attr.allocatable;
6451 pointer = sym->attr.pointer;
6453 for (ref = e->ref; ref; ref = ref->next)
6455 switch (ref->type)
6457 case REF_ARRAY:
6458 if (ref->u.ar.type != AR_FULL
6459 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6460 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6461 allocatable = 0;
6462 break;
6464 case REF_COMPONENT:
6465 c = ref->u.c.component;
6466 if (c->ts.type == BT_CLASS)
6468 allocatable = CLASS_DATA (c)->attr.allocatable;
6469 pointer = CLASS_DATA (c)->attr.class_pointer;
6471 else
6473 allocatable = c->attr.allocatable;
6474 pointer = c->attr.pointer;
6476 break;
6478 case REF_SUBSTRING:
6479 allocatable = 0;
6480 break;
6484 attr = gfc_expr_attr (e);
6486 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
6488 bad:
6489 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6490 &e->where);
6491 return false;
6494 /* F2008, C644. */
6495 if (gfc_is_coindexed (e))
6497 gfc_error ("Coindexed allocatable object at %L", &e->where);
6498 return false;
6501 if (pointer
6502 && !gfc_check_vardef_context (e, true, true, false,
6503 _("DEALLOCATE object")))
6504 return false;
6505 if (!gfc_check_vardef_context (e, false, true, false,
6506 _("DEALLOCATE object")))
6507 return false;
6509 return true;
6513 /* Returns true if the expression e contains a reference to the symbol sym. */
6514 static bool
6515 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6517 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6518 return true;
6520 return false;
6523 bool
6524 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6526 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6530 /* Given the expression node e for an allocatable/pointer of derived type to be
6531 allocated, get the expression node to be initialized afterwards (needed for
6532 derived types with default initializers, and derived types with allocatable
6533 components that need nullification.) */
6535 gfc_expr *
6536 gfc_expr_to_initialize (gfc_expr *e)
6538 gfc_expr *result;
6539 gfc_ref *ref;
6540 int i;
6542 result = gfc_copy_expr (e);
6544 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6545 for (ref = result->ref; ref; ref = ref->next)
6546 if (ref->type == REF_ARRAY && ref->next == NULL)
6548 ref->u.ar.type = AR_FULL;
6550 for (i = 0; i < ref->u.ar.dimen; i++)
6551 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6553 break;
6556 gfc_free_shape (&result->shape, result->rank);
6558 /* Recalculate rank, shape, etc. */
6559 gfc_resolve_expr (result);
6560 return result;
6564 /* If the last ref of an expression is an array ref, return a copy of the
6565 expression with that one removed. Otherwise, a copy of the original
6566 expression. This is used for allocate-expressions and pointer assignment
6567 LHS, where there may be an array specification that needs to be stripped
6568 off when using gfc_check_vardef_context. */
6570 static gfc_expr*
6571 remove_last_array_ref (gfc_expr* e)
6573 gfc_expr* e2;
6574 gfc_ref** r;
6576 e2 = gfc_copy_expr (e);
6577 for (r = &e2->ref; *r; r = &(*r)->next)
6578 if ((*r)->type == REF_ARRAY && !(*r)->next)
6580 gfc_free_ref_list (*r);
6581 *r = NULL;
6582 break;
6585 return e2;
6589 /* Used in resolve_allocate_expr to check that a allocation-object and
6590 a source-expr are conformable. This does not catch all possible
6591 cases; in particular a runtime checking is needed. */
6593 static bool
6594 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6596 gfc_ref *tail;
6597 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6599 /* First compare rank. */
6600 if ((tail && e1->rank != tail->u.ar.as->rank)
6601 || (!tail && e1->rank != e2->rank))
6603 gfc_error ("Source-expr at %L must be scalar or have the "
6604 "same rank as the allocate-object at %L",
6605 &e1->where, &e2->where);
6606 return false;
6609 if (e1->shape)
6611 int i;
6612 mpz_t s;
6614 mpz_init (s);
6616 for (i = 0; i < e1->rank; i++)
6618 if (tail->u.ar.start[i] == NULL)
6619 break;
6621 if (tail->u.ar.end[i])
6623 mpz_set (s, tail->u.ar.end[i]->value.integer);
6624 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6625 mpz_add_ui (s, s, 1);
6627 else
6629 mpz_set (s, tail->u.ar.start[i]->value.integer);
6632 if (mpz_cmp (e1->shape[i], s) != 0)
6634 gfc_error ("Source-expr at %L and allocate-object at %L must "
6635 "have the same shape", &e1->where, &e2->where);
6636 mpz_clear (s);
6637 return false;
6641 mpz_clear (s);
6644 return true;
6648 /* Resolve the expression in an ALLOCATE statement, doing the additional
6649 checks to see whether the expression is OK or not. The expression must
6650 have a trailing array reference that gives the size of the array. */
6652 static bool
6653 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6655 int i, pointer, allocatable, dimension, is_abstract;
6656 int codimension;
6657 bool coindexed;
6658 bool unlimited;
6659 symbol_attribute attr;
6660 gfc_ref *ref, *ref2;
6661 gfc_expr *e2;
6662 gfc_array_ref *ar;
6663 gfc_symbol *sym = NULL;
6664 gfc_alloc *a;
6665 gfc_component *c;
6666 bool t;
6668 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
6669 checking of coarrays. */
6670 for (ref = e->ref; ref; ref = ref->next)
6671 if (ref->next == NULL)
6672 break;
6674 if (ref && ref->type == REF_ARRAY)
6675 ref->u.ar.in_allocate = true;
6677 if (!gfc_resolve_expr (e))
6678 goto failure;
6680 /* Make sure the expression is allocatable or a pointer. If it is
6681 pointer, the next-to-last reference must be a pointer. */
6683 ref2 = NULL;
6684 if (e->symtree)
6685 sym = e->symtree->n.sym;
6687 /* Check whether ultimate component is abstract and CLASS. */
6688 is_abstract = 0;
6690 /* Is the allocate-object unlimited polymorphic? */
6691 unlimited = UNLIMITED_POLY(e);
6693 if (e->expr_type != EXPR_VARIABLE)
6695 allocatable = 0;
6696 attr = gfc_expr_attr (e);
6697 pointer = attr.pointer;
6698 dimension = attr.dimension;
6699 codimension = attr.codimension;
6701 else
6703 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
6705 allocatable = CLASS_DATA (sym)->attr.allocatable;
6706 pointer = CLASS_DATA (sym)->attr.class_pointer;
6707 dimension = CLASS_DATA (sym)->attr.dimension;
6708 codimension = CLASS_DATA (sym)->attr.codimension;
6709 is_abstract = CLASS_DATA (sym)->attr.abstract;
6711 else
6713 allocatable = sym->attr.allocatable;
6714 pointer = sym->attr.pointer;
6715 dimension = sym->attr.dimension;
6716 codimension = sym->attr.codimension;
6719 coindexed = false;
6721 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6723 switch (ref->type)
6725 case REF_ARRAY:
6726 if (ref->u.ar.codimen > 0)
6728 int n;
6729 for (n = ref->u.ar.dimen;
6730 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6731 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6733 coindexed = true;
6734 break;
6738 if (ref->next != NULL)
6739 pointer = 0;
6740 break;
6742 case REF_COMPONENT:
6743 /* F2008, C644. */
6744 if (coindexed)
6746 gfc_error ("Coindexed allocatable object at %L",
6747 &e->where);
6748 goto failure;
6751 c = ref->u.c.component;
6752 if (c->ts.type == BT_CLASS)
6754 allocatable = CLASS_DATA (c)->attr.allocatable;
6755 pointer = CLASS_DATA (c)->attr.class_pointer;
6756 dimension = CLASS_DATA (c)->attr.dimension;
6757 codimension = CLASS_DATA (c)->attr.codimension;
6758 is_abstract = CLASS_DATA (c)->attr.abstract;
6760 else
6762 allocatable = c->attr.allocatable;
6763 pointer = c->attr.pointer;
6764 dimension = c->attr.dimension;
6765 codimension = c->attr.codimension;
6766 is_abstract = c->attr.abstract;
6768 break;
6770 case REF_SUBSTRING:
6771 allocatable = 0;
6772 pointer = 0;
6773 break;
6778 /* Check for F08:C628. */
6779 if (allocatable == 0 && pointer == 0 && !unlimited)
6781 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6782 &e->where);
6783 goto failure;
6786 /* Some checks for the SOURCE tag. */
6787 if (code->expr3)
6789 /* Check F03:C631. */
6790 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6792 gfc_error ("Type of entity at %L is type incompatible with "
6793 "source-expr at %L", &e->where, &code->expr3->where);
6794 goto failure;
6797 /* Check F03:C632 and restriction following Note 6.18. */
6798 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
6799 goto failure;
6801 /* Check F03:C633. */
6802 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
6804 gfc_error ("The allocate-object at %L and the source-expr at %L "
6805 "shall have the same kind type parameter",
6806 &e->where, &code->expr3->where);
6807 goto failure;
6810 /* Check F2008, C642. */
6811 if (code->expr3->ts.type == BT_DERIVED
6812 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
6813 || (code->expr3->ts.u.derived->from_intmod
6814 == INTMOD_ISO_FORTRAN_ENV
6815 && code->expr3->ts.u.derived->intmod_sym_id
6816 == ISOFORTRAN_LOCK_TYPE)))
6818 gfc_error ("The source-expr at %L shall neither be of type "
6819 "LOCK_TYPE nor have a LOCK_TYPE component if "
6820 "allocate-object at %L is a coarray",
6821 &code->expr3->where, &e->where);
6822 goto failure;
6826 /* Check F08:C629. */
6827 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6828 && !code->expr3)
6830 gcc_assert (e->ts.type == BT_CLASS);
6831 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6832 "type-spec or source-expr", sym->name, &e->where);
6833 goto failure;
6836 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred)
6838 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
6839 code->ext.alloc.ts.u.cl->length);
6840 if (cmp == 1 || cmp == -1 || cmp == -3)
6842 gfc_error ("Allocating %s at %L with type-spec requires the same "
6843 "character-length parameter as in the declaration",
6844 sym->name, &e->where);
6845 goto failure;
6849 /* In the variable definition context checks, gfc_expr_attr is used
6850 on the expression. This is fooled by the array specification
6851 present in e, thus we have to eliminate that one temporarily. */
6852 e2 = remove_last_array_ref (e);
6853 t = true;
6854 if (t && pointer)
6855 t = gfc_check_vardef_context (e2, true, true, false,
6856 _("ALLOCATE object"));
6857 if (t)
6858 t = gfc_check_vardef_context (e2, false, true, false,
6859 _("ALLOCATE object"));
6860 gfc_free_expr (e2);
6861 if (!t)
6862 goto failure;
6864 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
6865 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
6867 /* For class arrays, the initialization with SOURCE is done
6868 using _copy and trans_call. It is convenient to exploit that
6869 when the allocated type is different from the declared type but
6870 no SOURCE exists by setting expr3. */
6871 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
6873 else if (!code->expr3)
6875 /* Set up default initializer if needed. */
6876 gfc_typespec ts;
6877 gfc_expr *init_e;
6879 if (code->ext.alloc.ts.type == BT_DERIVED)
6880 ts = code->ext.alloc.ts;
6881 else
6882 ts = e->ts;
6884 if (ts.type == BT_CLASS)
6885 ts = ts.u.derived->components->ts;
6887 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
6889 gfc_code *init_st = gfc_get_code (EXEC_INIT_ASSIGN);
6890 init_st->loc = code->loc;
6891 init_st->expr1 = gfc_expr_to_initialize (e);
6892 init_st->expr2 = init_e;
6893 init_st->next = code->next;
6894 code->next = init_st;
6897 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
6899 /* Default initialization via MOLD (non-polymorphic). */
6900 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
6901 gfc_resolve_expr (rhs);
6902 gfc_free_expr (code->expr3);
6903 code->expr3 = rhs;
6906 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
6908 /* Make sure the vtab symbol is present when
6909 the module variables are generated. */
6910 gfc_typespec ts = e->ts;
6911 if (code->expr3)
6912 ts = code->expr3->ts;
6913 else if (code->ext.alloc.ts.type == BT_DERIVED)
6914 ts = code->ext.alloc.ts;
6916 gfc_find_derived_vtab (ts.u.derived);
6918 if (dimension)
6919 e = gfc_expr_to_initialize (e);
6921 else if (unlimited && !UNLIMITED_POLY (code->expr3))
6923 /* Again, make sure the vtab symbol is present when
6924 the module variables are generated. */
6925 gfc_typespec *ts = NULL;
6926 if (code->expr3)
6927 ts = &code->expr3->ts;
6928 else
6929 ts = &code->ext.alloc.ts;
6931 gcc_assert (ts);
6933 gfc_find_vtab (ts);
6935 if (dimension)
6936 e = gfc_expr_to_initialize (e);
6939 if (dimension == 0 && codimension == 0)
6940 goto success;
6942 /* Make sure the last reference node is an array specification. */
6944 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6945 || (dimension && ref2->u.ar.dimen == 0))
6947 gfc_error ("Array specification required in ALLOCATE statement "
6948 "at %L", &e->where);
6949 goto failure;
6952 /* Make sure that the array section reference makes sense in the
6953 context of an ALLOCATE specification. */
6955 ar = &ref2->u.ar;
6957 if (codimension)
6958 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
6959 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
6961 gfc_error ("Coarray specification required in ALLOCATE statement "
6962 "at %L", &e->where);
6963 goto failure;
6966 for (i = 0; i < ar->dimen; i++)
6968 if (ref2->u.ar.type == AR_ELEMENT)
6969 goto check_symbols;
6971 switch (ar->dimen_type[i])
6973 case DIMEN_ELEMENT:
6974 break;
6976 case DIMEN_RANGE:
6977 if (ar->start[i] != NULL
6978 && ar->end[i] != NULL
6979 && ar->stride[i] == NULL)
6980 break;
6982 /* Fall Through... */
6984 case DIMEN_UNKNOWN:
6985 case DIMEN_VECTOR:
6986 case DIMEN_STAR:
6987 case DIMEN_THIS_IMAGE:
6988 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6989 &e->where);
6990 goto failure;
6993 check_symbols:
6994 for (a = code->ext.alloc.list; a; a = a->next)
6996 sym = a->expr->symtree->n.sym;
6998 /* TODO - check derived type components. */
6999 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
7000 continue;
7002 if ((ar->start[i] != NULL
7003 && gfc_find_sym_in_expr (sym, ar->start[i]))
7004 || (ar->end[i] != NULL
7005 && gfc_find_sym_in_expr (sym, ar->end[i])))
7007 gfc_error ("'%s' must not appear in the array specification at "
7008 "%L in the same ALLOCATE statement where it is "
7009 "itself allocated", sym->name, &ar->where);
7010 goto failure;
7015 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7017 if (ar->dimen_type[i] == DIMEN_ELEMENT
7018 || ar->dimen_type[i] == DIMEN_RANGE)
7020 if (i == (ar->dimen + ar->codimen - 1))
7022 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7023 "statement at %L", &e->where);
7024 goto failure;
7026 continue;
7029 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7030 && ar->stride[i] == NULL)
7031 break;
7033 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7034 &e->where);
7035 goto failure;
7038 success:
7039 return true;
7041 failure:
7042 return false;
7045 static void
7046 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7048 gfc_expr *stat, *errmsg, *pe, *qe;
7049 gfc_alloc *a, *p, *q;
7051 stat = code->expr1;
7052 errmsg = code->expr2;
7054 /* Check the stat variable. */
7055 if (stat)
7057 gfc_check_vardef_context (stat, false, false, false,
7058 _("STAT variable"));
7060 if ((stat->ts.type != BT_INTEGER
7061 && !(stat->ref && (stat->ref->type == REF_ARRAY
7062 || stat->ref->type == REF_COMPONENT)))
7063 || stat->rank > 0)
7064 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7065 "variable", &stat->where);
7067 for (p = code->ext.alloc.list; p; p = p->next)
7068 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7070 gfc_ref *ref1, *ref2;
7071 bool found = true;
7073 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7074 ref1 = ref1->next, ref2 = ref2->next)
7076 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7077 continue;
7078 if (ref1->u.c.component->name != ref2->u.c.component->name)
7080 found = false;
7081 break;
7085 if (found)
7087 gfc_error ("Stat-variable at %L shall not be %sd within "
7088 "the same %s statement", &stat->where, fcn, fcn);
7089 break;
7094 /* Check the errmsg variable. */
7095 if (errmsg)
7097 if (!stat)
7098 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7099 &errmsg->where);
7101 gfc_check_vardef_context (errmsg, false, false, false,
7102 _("ERRMSG variable"));
7104 if ((errmsg->ts.type != BT_CHARACTER
7105 && !(errmsg->ref
7106 && (errmsg->ref->type == REF_ARRAY
7107 || errmsg->ref->type == REF_COMPONENT)))
7108 || errmsg->rank > 0 )
7109 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7110 "variable", &errmsg->where);
7112 for (p = code->ext.alloc.list; p; p = p->next)
7113 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7115 gfc_ref *ref1, *ref2;
7116 bool found = true;
7118 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7119 ref1 = ref1->next, ref2 = ref2->next)
7121 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7122 continue;
7123 if (ref1->u.c.component->name != ref2->u.c.component->name)
7125 found = false;
7126 break;
7130 if (found)
7132 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7133 "the same %s statement", &errmsg->where, fcn, fcn);
7134 break;
7139 /* Check that an allocate-object appears only once in the statement. */
7141 for (p = code->ext.alloc.list; p; p = p->next)
7143 pe = p->expr;
7144 for (q = p->next; q; q = q->next)
7146 qe = q->expr;
7147 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7149 /* This is a potential collision. */
7150 gfc_ref *pr = pe->ref;
7151 gfc_ref *qr = qe->ref;
7153 /* Follow the references until
7154 a) They start to differ, in which case there is no error;
7155 you can deallocate a%b and a%c in a single statement
7156 b) Both of them stop, which is an error
7157 c) One of them stops, which is also an error. */
7158 while (1)
7160 if (pr == NULL && qr == NULL)
7162 gfc_error ("Allocate-object at %L also appears at %L",
7163 &pe->where, &qe->where);
7164 break;
7166 else if (pr != NULL && qr == NULL)
7168 gfc_error ("Allocate-object at %L is subobject of"
7169 " object at %L", &pe->where, &qe->where);
7170 break;
7172 else if (pr == NULL && qr != NULL)
7174 gfc_error ("Allocate-object at %L is subobject of"
7175 " object at %L", &qe->where, &pe->where);
7176 break;
7178 /* Here, pr != NULL && qr != NULL */
7179 gcc_assert(pr->type == qr->type);
7180 if (pr->type == REF_ARRAY)
7182 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7183 which are legal. */
7184 gcc_assert (qr->type == REF_ARRAY);
7186 if (pr->next && qr->next)
7188 int i;
7189 gfc_array_ref *par = &(pr->u.ar);
7190 gfc_array_ref *qar = &(qr->u.ar);
7192 for (i=0; i<par->dimen; i++)
7194 if ((par->start[i] != NULL
7195 || qar->start[i] != NULL)
7196 && gfc_dep_compare_expr (par->start[i],
7197 qar->start[i]) != 0)
7198 goto break_label;
7202 else
7204 if (pr->u.c.component->name != qr->u.c.component->name)
7205 break;
7208 pr = pr->next;
7209 qr = qr->next;
7211 break_label:
7217 if (strcmp (fcn, "ALLOCATE") == 0)
7219 for (a = code->ext.alloc.list; a; a = a->next)
7220 resolve_allocate_expr (a->expr, code);
7222 else
7224 for (a = code->ext.alloc.list; a; a = a->next)
7225 resolve_deallocate_expr (a->expr);
7230 /************ SELECT CASE resolution subroutines ************/
7232 /* Callback function for our mergesort variant. Determines interval
7233 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7234 op1 > op2. Assumes we're not dealing with the default case.
7235 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7236 There are nine situations to check. */
7238 static int
7239 compare_cases (const gfc_case *op1, const gfc_case *op2)
7241 int retval;
7243 if (op1->low == NULL) /* op1 = (:L) */
7245 /* op2 = (:N), so overlap. */
7246 retval = 0;
7247 /* op2 = (M:) or (M:N), L < M */
7248 if (op2->low != NULL
7249 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7250 retval = -1;
7252 else if (op1->high == NULL) /* op1 = (K:) */
7254 /* op2 = (M:), so overlap. */
7255 retval = 0;
7256 /* op2 = (:N) or (M:N), K > N */
7257 if (op2->high != NULL
7258 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7259 retval = 1;
7261 else /* op1 = (K:L) */
7263 if (op2->low == NULL) /* op2 = (:N), K > N */
7264 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7265 ? 1 : 0;
7266 else if (op2->high == NULL) /* op2 = (M:), L < M */
7267 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7268 ? -1 : 0;
7269 else /* op2 = (M:N) */
7271 retval = 0;
7272 /* L < M */
7273 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7274 retval = -1;
7275 /* K > N */
7276 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7277 retval = 1;
7281 return retval;
7285 /* Merge-sort a double linked case list, detecting overlap in the
7286 process. LIST is the head of the double linked case list before it
7287 is sorted. Returns the head of the sorted list if we don't see any
7288 overlap, or NULL otherwise. */
7290 static gfc_case *
7291 check_case_overlap (gfc_case *list)
7293 gfc_case *p, *q, *e, *tail;
7294 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7296 /* If the passed list was empty, return immediately. */
7297 if (!list)
7298 return NULL;
7300 overlap_seen = 0;
7301 insize = 1;
7303 /* Loop unconditionally. The only exit from this loop is a return
7304 statement, when we've finished sorting the case list. */
7305 for (;;)
7307 p = list;
7308 list = NULL;
7309 tail = NULL;
7311 /* Count the number of merges we do in this pass. */
7312 nmerges = 0;
7314 /* Loop while there exists a merge to be done. */
7315 while (p)
7317 int i;
7319 /* Count this merge. */
7320 nmerges++;
7322 /* Cut the list in two pieces by stepping INSIZE places
7323 forward in the list, starting from P. */
7324 psize = 0;
7325 q = p;
7326 for (i = 0; i < insize; i++)
7328 psize++;
7329 q = q->right;
7330 if (!q)
7331 break;
7333 qsize = insize;
7335 /* Now we have two lists. Merge them! */
7336 while (psize > 0 || (qsize > 0 && q != NULL))
7338 /* See from which the next case to merge comes from. */
7339 if (psize == 0)
7341 /* P is empty so the next case must come from Q. */
7342 e = q;
7343 q = q->right;
7344 qsize--;
7346 else if (qsize == 0 || q == NULL)
7348 /* Q is empty. */
7349 e = p;
7350 p = p->right;
7351 psize--;
7353 else
7355 cmp = compare_cases (p, q);
7356 if (cmp < 0)
7358 /* The whole case range for P is less than the
7359 one for Q. */
7360 e = p;
7361 p = p->right;
7362 psize--;
7364 else if (cmp > 0)
7366 /* The whole case range for Q is greater than
7367 the case range for P. */
7368 e = q;
7369 q = q->right;
7370 qsize--;
7372 else
7374 /* The cases overlap, or they are the same
7375 element in the list. Either way, we must
7376 issue an error and get the next case from P. */
7377 /* FIXME: Sort P and Q by line number. */
7378 gfc_error ("CASE label at %L overlaps with CASE "
7379 "label at %L", &p->where, &q->where);
7380 overlap_seen = 1;
7381 e = p;
7382 p = p->right;
7383 psize--;
7387 /* Add the next element to the merged list. */
7388 if (tail)
7389 tail->right = e;
7390 else
7391 list = e;
7392 e->left = tail;
7393 tail = e;
7396 /* P has now stepped INSIZE places along, and so has Q. So
7397 they're the same. */
7398 p = q;
7400 tail->right = NULL;
7402 /* If we have done only one merge or none at all, we've
7403 finished sorting the cases. */
7404 if (nmerges <= 1)
7406 if (!overlap_seen)
7407 return list;
7408 else
7409 return NULL;
7412 /* Otherwise repeat, merging lists twice the size. */
7413 insize *= 2;
7418 /* Check to see if an expression is suitable for use in a CASE statement.
7419 Makes sure that all case expressions are scalar constants of the same
7420 type. Return false if anything is wrong. */
7422 static bool
7423 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7425 if (e == NULL) return true;
7427 if (e->ts.type != case_expr->ts.type)
7429 gfc_error ("Expression in CASE statement at %L must be of type %s",
7430 &e->where, gfc_basic_typename (case_expr->ts.type));
7431 return false;
7434 /* C805 (R808) For a given case-construct, each case-value shall be of
7435 the same type as case-expr. For character type, length differences
7436 are allowed, but the kind type parameters shall be the same. */
7438 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7440 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7441 &e->where, case_expr->ts.kind);
7442 return false;
7445 /* Convert the case value kind to that of case expression kind,
7446 if needed */
7448 if (e->ts.kind != case_expr->ts.kind)
7449 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7451 if (e->rank != 0)
7453 gfc_error ("Expression in CASE statement at %L must be scalar",
7454 &e->where);
7455 return false;
7458 return true;
7462 /* Given a completely parsed select statement, we:
7464 - Validate all expressions and code within the SELECT.
7465 - Make sure that the selection expression is not of the wrong type.
7466 - Make sure that no case ranges overlap.
7467 - Eliminate unreachable cases and unreachable code resulting from
7468 removing case labels.
7470 The standard does allow unreachable cases, e.g. CASE (5:3). But
7471 they are a hassle for code generation, and to prevent that, we just
7472 cut them out here. This is not necessary for overlapping cases
7473 because they are illegal and we never even try to generate code.
7475 We have the additional caveat that a SELECT construct could have
7476 been a computed GOTO in the source code. Fortunately we can fairly
7477 easily work around that here: The case_expr for a "real" SELECT CASE
7478 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7479 we have to do is make sure that the case_expr is a scalar integer
7480 expression. */
7482 static void
7483 resolve_select (gfc_code *code, bool select_type)
7485 gfc_code *body;
7486 gfc_expr *case_expr;
7487 gfc_case *cp, *default_case, *tail, *head;
7488 int seen_unreachable;
7489 int seen_logical;
7490 int ncases;
7491 bt type;
7492 bool t;
7494 if (code->expr1 == NULL)
7496 /* This was actually a computed GOTO statement. */
7497 case_expr = code->expr2;
7498 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7499 gfc_error ("Selection expression in computed GOTO statement "
7500 "at %L must be a scalar integer expression",
7501 &case_expr->where);
7503 /* Further checking is not necessary because this SELECT was built
7504 by the compiler, so it should always be OK. Just move the
7505 case_expr from expr2 to expr so that we can handle computed
7506 GOTOs as normal SELECTs from here on. */
7507 code->expr1 = code->expr2;
7508 code->expr2 = NULL;
7509 return;
7512 case_expr = code->expr1;
7513 type = case_expr->ts.type;
7515 /* F08:C830. */
7516 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7518 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7519 &case_expr->where, gfc_typename (&case_expr->ts));
7521 /* Punt. Going on here just produce more garbage error messages. */
7522 return;
7525 /* F08:R842. */
7526 if (!select_type && case_expr->rank != 0)
7528 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7529 "expression", &case_expr->where);
7531 /* Punt. */
7532 return;
7535 /* Raise a warning if an INTEGER case value exceeds the range of
7536 the case-expr. Later, all expressions will be promoted to the
7537 largest kind of all case-labels. */
7539 if (type == BT_INTEGER)
7540 for (body = code->block; body; body = body->block)
7541 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7543 if (cp->low
7544 && gfc_check_integer_range (cp->low->value.integer,
7545 case_expr->ts.kind) != ARITH_OK)
7546 gfc_warning ("Expression in CASE statement at %L is "
7547 "not in the range of %s", &cp->low->where,
7548 gfc_typename (&case_expr->ts));
7550 if (cp->high
7551 && cp->low != cp->high
7552 && gfc_check_integer_range (cp->high->value.integer,
7553 case_expr->ts.kind) != ARITH_OK)
7554 gfc_warning ("Expression in CASE statement at %L is "
7555 "not in the range of %s", &cp->high->where,
7556 gfc_typename (&case_expr->ts));
7559 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7560 of the SELECT CASE expression and its CASE values. Walk the lists
7561 of case values, and if we find a mismatch, promote case_expr to
7562 the appropriate kind. */
7564 if (type == BT_LOGICAL || type == BT_INTEGER)
7566 for (body = code->block; body; body = body->block)
7568 /* Walk the case label list. */
7569 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7571 /* Intercept the DEFAULT case. It does not have a kind. */
7572 if (cp->low == NULL && cp->high == NULL)
7573 continue;
7575 /* Unreachable case ranges are discarded, so ignore. */
7576 if (cp->low != NULL && cp->high != NULL
7577 && cp->low != cp->high
7578 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7579 continue;
7581 if (cp->low != NULL
7582 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7583 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7585 if (cp->high != NULL
7586 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7587 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7592 /* Assume there is no DEFAULT case. */
7593 default_case = NULL;
7594 head = tail = NULL;
7595 ncases = 0;
7596 seen_logical = 0;
7598 for (body = code->block; body; body = body->block)
7600 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7601 t = true;
7602 seen_unreachable = 0;
7604 /* Walk the case label list, making sure that all case labels
7605 are legal. */
7606 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7608 /* Count the number of cases in the whole construct. */
7609 ncases++;
7611 /* Intercept the DEFAULT case. */
7612 if (cp->low == NULL && cp->high == NULL)
7614 if (default_case != NULL)
7616 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7617 "by a second DEFAULT CASE at %L",
7618 &default_case->where, &cp->where);
7619 t = false;
7620 break;
7622 else
7624 default_case = cp;
7625 continue;
7629 /* Deal with single value cases and case ranges. Errors are
7630 issued from the validation function. */
7631 if (!validate_case_label_expr (cp->low, case_expr)
7632 || !validate_case_label_expr (cp->high, case_expr))
7634 t = false;
7635 break;
7638 if (type == BT_LOGICAL
7639 && ((cp->low == NULL || cp->high == NULL)
7640 || cp->low != cp->high))
7642 gfc_error ("Logical range in CASE statement at %L is not "
7643 "allowed", &cp->low->where);
7644 t = false;
7645 break;
7648 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7650 int value;
7651 value = cp->low->value.logical == 0 ? 2 : 1;
7652 if (value & seen_logical)
7654 gfc_error ("Constant logical value in CASE statement "
7655 "is repeated at %L",
7656 &cp->low->where);
7657 t = false;
7658 break;
7660 seen_logical |= value;
7663 if (cp->low != NULL && cp->high != NULL
7664 && cp->low != cp->high
7665 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7667 if (gfc_option.warn_surprising)
7668 gfc_warning ("Range specification at %L can never "
7669 "be matched", &cp->where);
7671 cp->unreachable = 1;
7672 seen_unreachable = 1;
7674 else
7676 /* If the case range can be matched, it can also overlap with
7677 other cases. To make sure it does not, we put it in a
7678 double linked list here. We sort that with a merge sort
7679 later on to detect any overlapping cases. */
7680 if (!head)
7682 head = tail = cp;
7683 head->right = head->left = NULL;
7685 else
7687 tail->right = cp;
7688 tail->right->left = tail;
7689 tail = tail->right;
7690 tail->right = NULL;
7695 /* It there was a failure in the previous case label, give up
7696 for this case label list. Continue with the next block. */
7697 if (!t)
7698 continue;
7700 /* See if any case labels that are unreachable have been seen.
7701 If so, we eliminate them. This is a bit of a kludge because
7702 the case lists for a single case statement (label) is a
7703 single forward linked lists. */
7704 if (seen_unreachable)
7706 /* Advance until the first case in the list is reachable. */
7707 while (body->ext.block.case_list != NULL
7708 && body->ext.block.case_list->unreachable)
7710 gfc_case *n = body->ext.block.case_list;
7711 body->ext.block.case_list = body->ext.block.case_list->next;
7712 n->next = NULL;
7713 gfc_free_case_list (n);
7716 /* Strip all other unreachable cases. */
7717 if (body->ext.block.case_list)
7719 for (cp = body->ext.block.case_list; cp->next; cp = cp->next)
7721 if (cp->next->unreachable)
7723 gfc_case *n = cp->next;
7724 cp->next = cp->next->next;
7725 n->next = NULL;
7726 gfc_free_case_list (n);
7733 /* See if there were overlapping cases. If the check returns NULL,
7734 there was overlap. In that case we don't do anything. If head
7735 is non-NULL, we prepend the DEFAULT case. The sorted list can
7736 then used during code generation for SELECT CASE constructs with
7737 a case expression of a CHARACTER type. */
7738 if (head)
7740 head = check_case_overlap (head);
7742 /* Prepend the default_case if it is there. */
7743 if (head != NULL && default_case)
7745 default_case->left = NULL;
7746 default_case->right = head;
7747 head->left = default_case;
7751 /* Eliminate dead blocks that may be the result if we've seen
7752 unreachable case labels for a block. */
7753 for (body = code; body && body->block; body = body->block)
7755 if (body->block->ext.block.case_list == NULL)
7757 /* Cut the unreachable block from the code chain. */
7758 gfc_code *c = body->block;
7759 body->block = c->block;
7761 /* Kill the dead block, but not the blocks below it. */
7762 c->block = NULL;
7763 gfc_free_statements (c);
7767 /* More than two cases is legal but insane for logical selects.
7768 Issue a warning for it. */
7769 if (gfc_option.warn_surprising && type == BT_LOGICAL
7770 && ncases > 2)
7771 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7772 &code->loc);
7776 /* Check if a derived type is extensible. */
7778 bool
7779 gfc_type_is_extensible (gfc_symbol *sym)
7781 return !(sym->attr.is_bind_c || sym->attr.sequence
7782 || (sym->attr.is_class
7783 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
7787 /* Resolve an associate-name: Resolve target and ensure the type-spec is
7788 correct as well as possibly the array-spec. */
7790 static void
7791 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7793 gfc_expr* target;
7795 gcc_assert (sym->assoc);
7796 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7798 /* If this is for SELECT TYPE, the target may not yet be set. In that
7799 case, return. Resolution will be called later manually again when
7800 this is done. */
7801 target = sym->assoc->target;
7802 if (!target)
7803 return;
7804 gcc_assert (!sym->assoc->dangling);
7806 if (resolve_target && !gfc_resolve_expr (target))
7807 return;
7809 /* For variable targets, we get some attributes from the target. */
7810 if (target->expr_type == EXPR_VARIABLE)
7812 gfc_symbol* tsym;
7814 gcc_assert (target->symtree);
7815 tsym = target->symtree->n.sym;
7817 sym->attr.asynchronous = tsym->attr.asynchronous;
7818 sym->attr.volatile_ = tsym->attr.volatile_;
7820 sym->attr.target = tsym->attr.target
7821 || gfc_expr_attr (target).pointer;
7824 /* Get type if this was not already set. Note that it can be
7825 some other type than the target in case this is a SELECT TYPE
7826 selector! So we must not update when the type is already there. */
7827 if (sym->ts.type == BT_UNKNOWN)
7828 sym->ts = target->ts;
7829 gcc_assert (sym->ts.type != BT_UNKNOWN);
7831 /* See if this is a valid association-to-variable. */
7832 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7833 && !gfc_has_vector_subscript (target));
7835 /* Finally resolve if this is an array or not. */
7836 if (sym->attr.dimension && target->rank == 0)
7838 gfc_error ("Associate-name '%s' at %L is used as array",
7839 sym->name, &sym->declared_at);
7840 sym->attr.dimension = 0;
7841 return;
7844 /* We cannot deal with class selectors that need temporaries. */
7845 if (target->ts.type == BT_CLASS
7846 && gfc_ref_needs_temporary_p (target->ref))
7848 gfc_error ("CLASS selector at %L needs a temporary which is not "
7849 "yet implemented", &target->where);
7850 return;
7853 if (target->ts.type != BT_CLASS && target->rank > 0)
7854 sym->attr.dimension = 1;
7855 else if (target->ts.type == BT_CLASS)
7856 gfc_fix_class_refs (target);
7858 /* The associate-name will have a correct type by now. Make absolutely
7859 sure that it has not picked up a dimension attribute. */
7860 if (sym->ts.type == BT_CLASS)
7861 sym->attr.dimension = 0;
7863 if (sym->attr.dimension)
7865 sym->as = gfc_get_array_spec ();
7866 sym->as->rank = target->rank;
7867 sym->as->type = AS_DEFERRED;
7869 /* Target must not be coindexed, thus the associate-variable
7870 has no corank. */
7871 sym->as->corank = 0;
7874 /* Mark this as an associate variable. */
7875 sym->attr.associate_var = 1;
7877 /* If the target is a good class object, so is the associate variable. */
7878 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
7879 sym->attr.class_ok = 1;
7883 /* Resolve a SELECT TYPE statement. */
7885 static void
7886 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
7888 gfc_symbol *selector_type;
7889 gfc_code *body, *new_st, *if_st, *tail;
7890 gfc_code *class_is = NULL, *default_case = NULL;
7891 gfc_case *c;
7892 gfc_symtree *st;
7893 char name[GFC_MAX_SYMBOL_LEN];
7894 gfc_namespace *ns;
7895 int error = 0;
7896 int charlen = 0;
7898 ns = code->ext.block.ns;
7899 gfc_resolve (ns);
7901 /* Check for F03:C813. */
7902 if (code->expr1->ts.type != BT_CLASS
7903 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7905 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7906 "at %L", &code->loc);
7907 return;
7910 if (!code->expr1->symtree->n.sym->attr.class_ok)
7911 return;
7913 if (code->expr2)
7915 if (code->expr1->symtree->n.sym->attr.untyped)
7916 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7917 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7919 /* F2008: C803 The selector expression must not be coindexed. */
7920 if (gfc_is_coindexed (code->expr2))
7922 gfc_error ("Selector at %L must not be coindexed",
7923 &code->expr2->where);
7924 return;
7928 else
7930 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7932 if (gfc_is_coindexed (code->expr1))
7934 gfc_error ("Selector at %L must not be coindexed",
7935 &code->expr1->where);
7936 return;
7940 /* Loop over TYPE IS / CLASS IS cases. */
7941 for (body = code->block; body; body = body->block)
7943 c = body->ext.block.case_list;
7945 /* Check F03:C815. */
7946 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7947 && !selector_type->attr.unlimited_polymorphic
7948 && !gfc_type_is_extensible (c->ts.u.derived))
7950 gfc_error ("Derived type '%s' at %L must be extensible",
7951 c->ts.u.derived->name, &c->where);
7952 error++;
7953 continue;
7956 /* Check F03:C816. */
7957 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
7958 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
7959 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
7961 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7962 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7963 c->ts.u.derived->name, &c->where, selector_type->name);
7964 else
7965 gfc_error ("Unexpected intrinsic type '%s' at %L",
7966 gfc_basic_typename (c->ts.type), &c->where);
7967 error++;
7968 continue;
7971 /* Check F03:C814. */
7972 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
7974 gfc_error ("The type-spec at %L shall specify that each length "
7975 "type parameter is assumed", &c->where);
7976 error++;
7977 continue;
7980 /* Intercept the DEFAULT case. */
7981 if (c->ts.type == BT_UNKNOWN)
7983 /* Check F03:C818. */
7984 if (default_case)
7986 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7987 "by a second DEFAULT CASE at %L",
7988 &default_case->ext.block.case_list->where, &c->where);
7989 error++;
7990 continue;
7993 default_case = body;
7997 if (error > 0)
7998 return;
8000 /* Transform SELECT TYPE statement to BLOCK and associate selector to
8001 target if present. If there are any EXIT statements referring to the
8002 SELECT TYPE construct, this is no problem because the gfc_code
8003 reference stays the same and EXIT is equally possible from the BLOCK
8004 it is changed to. */
8005 code->op = EXEC_BLOCK;
8006 if (code->expr2)
8008 gfc_association_list* assoc;
8010 assoc = gfc_get_association_list ();
8011 assoc->st = code->expr1->symtree;
8012 assoc->target = gfc_copy_expr (code->expr2);
8013 assoc->target->where = code->expr2->where;
8014 /* assoc->variable will be set by resolve_assoc_var. */
8016 code->ext.block.assoc = assoc;
8017 code->expr1->symtree->n.sym->assoc = assoc;
8019 resolve_assoc_var (code->expr1->symtree->n.sym, false);
8021 else
8022 code->ext.block.assoc = NULL;
8024 /* Add EXEC_SELECT to switch on type. */
8025 new_st = gfc_get_code (code->op);
8026 new_st->expr1 = code->expr1;
8027 new_st->expr2 = code->expr2;
8028 new_st->block = code->block;
8029 code->expr1 = code->expr2 = NULL;
8030 code->block = NULL;
8031 if (!ns->code)
8032 ns->code = new_st;
8033 else
8034 ns->code->next = new_st;
8035 code = new_st;
8036 code->op = EXEC_SELECT;
8038 gfc_add_vptr_component (code->expr1);
8039 gfc_add_hash_component (code->expr1);
8041 /* Loop over TYPE IS / CLASS IS cases. */
8042 for (body = code->block; body; body = body->block)
8044 c = body->ext.block.case_list;
8046 if (c->ts.type == BT_DERIVED)
8047 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8048 c->ts.u.derived->hash_value);
8049 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8051 gfc_symbol *ivtab;
8052 gfc_expr *e;
8054 ivtab = gfc_find_vtab (&c->ts);
8055 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
8056 e = CLASS_DATA (ivtab)->initializer;
8057 c->low = c->high = gfc_copy_expr (e);
8060 else if (c->ts.type == BT_UNKNOWN)
8061 continue;
8063 /* Associate temporary to selector. This should only be done
8064 when this case is actually true, so build a new ASSOCIATE
8065 that does precisely this here (instead of using the
8066 'global' one). */
8068 if (c->ts.type == BT_CLASS)
8069 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8070 else if (c->ts.type == BT_DERIVED)
8071 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8072 else if (c->ts.type == BT_CHARACTER)
8074 if (c->ts.u.cl && c->ts.u.cl->length
8075 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8076 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8077 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8078 charlen, c->ts.kind);
8080 else
8081 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8082 c->ts.kind);
8084 st = gfc_find_symtree (ns->sym_root, name);
8085 gcc_assert (st->n.sym->assoc);
8086 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
8087 st->n.sym->assoc->target->where = code->expr1->where;
8088 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8089 gfc_add_data_component (st->n.sym->assoc->target);
8091 new_st = gfc_get_code (EXEC_BLOCK);
8092 new_st->ext.block.ns = gfc_build_block_ns (ns);
8093 new_st->ext.block.ns->code = body->next;
8094 body->next = new_st;
8096 /* Chain in the new list only if it is marked as dangling. Otherwise
8097 there is a CASE label overlap and this is already used. Just ignore,
8098 the error is diagnosed elsewhere. */
8099 if (st->n.sym->assoc->dangling)
8101 new_st->ext.block.assoc = st->n.sym->assoc;
8102 st->n.sym->assoc->dangling = 0;
8105 resolve_assoc_var (st->n.sym, false);
8108 /* Take out CLASS IS cases for separate treatment. */
8109 body = code;
8110 while (body && body->block)
8112 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8114 /* Add to class_is list. */
8115 if (class_is == NULL)
8117 class_is = body->block;
8118 tail = class_is;
8120 else
8122 for (tail = class_is; tail->block; tail = tail->block) ;
8123 tail->block = body->block;
8124 tail = tail->block;
8126 /* Remove from EXEC_SELECT list. */
8127 body->block = body->block->block;
8128 tail->block = NULL;
8130 else
8131 body = body->block;
8134 if (class_is)
8136 gfc_symbol *vtab;
8138 if (!default_case)
8140 /* Add a default case to hold the CLASS IS cases. */
8141 for (tail = code; tail->block; tail = tail->block) ;
8142 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
8143 tail = tail->block;
8144 tail->ext.block.case_list = gfc_get_case ();
8145 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8146 tail->next = NULL;
8147 default_case = tail;
8150 /* More than one CLASS IS block? */
8151 if (class_is->block)
8153 gfc_code **c1,*c2;
8154 bool swapped;
8155 /* Sort CLASS IS blocks by extension level. */
8158 swapped = false;
8159 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8161 c2 = (*c1)->block;
8162 /* F03:C817 (check for doubles). */
8163 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8164 == c2->ext.block.case_list->ts.u.derived->hash_value)
8166 gfc_error ("Double CLASS IS block in SELECT TYPE "
8167 "statement at %L",
8168 &c2->ext.block.case_list->where);
8169 return;
8171 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8172 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8174 /* Swap. */
8175 (*c1)->block = c2->block;
8176 c2->block = *c1;
8177 *c1 = c2;
8178 swapped = true;
8182 while (swapped);
8185 /* Generate IF chain. */
8186 if_st = gfc_get_code (EXEC_IF);
8187 new_st = if_st;
8188 for (body = class_is; body; body = body->block)
8190 new_st->block = gfc_get_code (EXEC_IF);
8191 new_st = new_st->block;
8192 /* Set up IF condition: Call _gfortran_is_extension_of. */
8193 new_st->expr1 = gfc_get_expr ();
8194 new_st->expr1->expr_type = EXPR_FUNCTION;
8195 new_st->expr1->ts.type = BT_LOGICAL;
8196 new_st->expr1->ts.kind = 4;
8197 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8198 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8199 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8200 /* Set up arguments. */
8201 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8202 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8203 new_st->expr1->value.function.actual->expr->where = code->loc;
8204 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8205 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8206 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8207 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8208 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8209 new_st->next = body->next;
8211 if (default_case->next)
8213 new_st->block = gfc_get_code (EXEC_IF);
8214 new_st = new_st->block;
8215 new_st->next = default_case->next;
8218 /* Replace CLASS DEFAULT code by the IF chain. */
8219 default_case->next = if_st;
8222 /* Resolve the internal code. This can not be done earlier because
8223 it requires that the sym->assoc of selectors is set already. */
8224 gfc_current_ns = ns;
8225 gfc_resolve_blocks (code->block, gfc_current_ns);
8226 gfc_current_ns = old_ns;
8228 resolve_select (code, true);
8232 /* Resolve a transfer statement. This is making sure that:
8233 -- a derived type being transferred has only non-pointer components
8234 -- a derived type being transferred doesn't have private components, unless
8235 it's being transferred from the module where the type was defined
8236 -- we're not trying to transfer a whole assumed size array. */
8238 static void
8239 resolve_transfer (gfc_code *code)
8241 gfc_typespec *ts;
8242 gfc_symbol *sym;
8243 gfc_ref *ref;
8244 gfc_expr *exp;
8246 exp = code->expr1;
8248 while (exp != NULL && exp->expr_type == EXPR_OP
8249 && exp->value.op.op == INTRINSIC_PARENTHESES)
8250 exp = exp->value.op.op1;
8252 if (exp && exp->expr_type == EXPR_NULL
8253 && code->ext.dt)
8255 gfc_error ("Invalid context for NULL () intrinsic at %L",
8256 &exp->where);
8257 return;
8260 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8261 && exp->expr_type != EXPR_FUNCTION))
8262 return;
8264 /* If we are reading, the variable will be changed. Note that
8265 code->ext.dt may be NULL if the TRANSFER is related to
8266 an INQUIRE statement -- but in this case, we are not reading, either. */
8267 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8268 && !gfc_check_vardef_context (exp, false, false, false,
8269 _("item in READ")))
8270 return;
8272 sym = exp->symtree->n.sym;
8273 ts = &sym->ts;
8275 /* Go to actual component transferred. */
8276 for (ref = exp->ref; ref; ref = ref->next)
8277 if (ref->type == REF_COMPONENT)
8278 ts = &ref->u.c.component->ts;
8280 if (ts->type == BT_CLASS)
8282 /* FIXME: Test for defined input/output. */
8283 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8284 "it is processed by a defined input/output procedure",
8285 &code->loc);
8286 return;
8289 if (ts->type == BT_DERIVED)
8291 /* Check that transferred derived type doesn't contain POINTER
8292 components. */
8293 if (ts->u.derived->attr.pointer_comp)
8295 gfc_error ("Data transfer element at %L cannot have POINTER "
8296 "components unless it is processed by a defined "
8297 "input/output procedure", &code->loc);
8298 return;
8301 /* F08:C935. */
8302 if (ts->u.derived->attr.proc_pointer_comp)
8304 gfc_error ("Data transfer element at %L cannot have "
8305 "procedure pointer components", &code->loc);
8306 return;
8309 if (ts->u.derived->attr.alloc_comp)
8311 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8312 "components unless it is processed by a defined "
8313 "input/output procedure", &code->loc);
8314 return;
8317 /* C_PTR and C_FUNPTR have private components which means they can not
8318 be printed. However, if -std=gnu and not -pedantic, allow
8319 the component to be printed to help debugging. */
8320 if (ts->u.derived->ts.f90_type == BT_VOID)
8322 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8323 "cannot have PRIVATE components", &code->loc))
8324 return;
8326 else if (derived_inaccessible (ts->u.derived))
8328 gfc_error ("Data transfer element at %L cannot have "
8329 "PRIVATE components",&code->loc);
8330 return;
8334 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8335 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8337 gfc_error ("Data transfer element at %L cannot be a full reference to "
8338 "an assumed-size array", &code->loc);
8339 return;
8344 /*********** Toplevel code resolution subroutines ***********/
8346 /* Find the set of labels that are reachable from this block. We also
8347 record the last statement in each block. */
8349 static void
8350 find_reachable_labels (gfc_code *block)
8352 gfc_code *c;
8354 if (!block)
8355 return;
8357 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8359 /* Collect labels in this block. We don't keep those corresponding
8360 to END {IF|SELECT}, these are checked in resolve_branch by going
8361 up through the code_stack. */
8362 for (c = block; c; c = c->next)
8364 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8365 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8368 /* Merge with labels from parent block. */
8369 if (cs_base->prev)
8371 gcc_assert (cs_base->prev->reachable_labels);
8372 bitmap_ior_into (cs_base->reachable_labels,
8373 cs_base->prev->reachable_labels);
8378 static void
8379 resolve_lock_unlock (gfc_code *code)
8381 if (code->expr1->ts.type != BT_DERIVED
8382 || code->expr1->expr_type != EXPR_VARIABLE
8383 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8384 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8385 || code->expr1->rank != 0
8386 || (!gfc_is_coarray (code->expr1) && !gfc_is_coindexed (code->expr1)))
8387 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8388 &code->expr1->where);
8390 /* Check STAT. */
8391 if (code->expr2
8392 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8393 || code->expr2->expr_type != EXPR_VARIABLE))
8394 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8395 &code->expr2->where);
8397 if (code->expr2
8398 && !gfc_check_vardef_context (code->expr2, false, false, false,
8399 _("STAT variable")))
8400 return;
8402 /* Check ERRMSG. */
8403 if (code->expr3
8404 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8405 || code->expr3->expr_type != EXPR_VARIABLE))
8406 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8407 &code->expr3->where);
8409 if (code->expr3
8410 && !gfc_check_vardef_context (code->expr3, false, false, false,
8411 _("ERRMSG variable")))
8412 return;
8414 /* Check ACQUIRED_LOCK. */
8415 if (code->expr4
8416 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8417 || code->expr4->expr_type != EXPR_VARIABLE))
8418 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8419 "variable", &code->expr4->where);
8421 if (code->expr4
8422 && !gfc_check_vardef_context (code->expr4, false, false, false,
8423 _("ACQUIRED_LOCK variable")))
8424 return;
8428 static void
8429 resolve_sync (gfc_code *code)
8431 /* Check imageset. The * case matches expr1 == NULL. */
8432 if (code->expr1)
8434 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8435 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8436 "INTEGER expression", &code->expr1->where);
8437 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8438 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8439 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8440 &code->expr1->where);
8441 else if (code->expr1->expr_type == EXPR_ARRAY
8442 && gfc_simplify_expr (code->expr1, 0))
8444 gfc_constructor *cons;
8445 cons = gfc_constructor_first (code->expr1->value.constructor);
8446 for (; cons; cons = gfc_constructor_next (cons))
8447 if (cons->expr->expr_type == EXPR_CONSTANT
8448 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8449 gfc_error ("Imageset argument at %L must between 1 and "
8450 "num_images()", &cons->expr->where);
8454 /* Check STAT. */
8455 if (code->expr2
8456 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8457 || code->expr2->expr_type != EXPR_VARIABLE))
8458 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8459 &code->expr2->where);
8461 /* Check ERRMSG. */
8462 if (code->expr3
8463 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8464 || code->expr3->expr_type != EXPR_VARIABLE))
8465 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8466 &code->expr3->where);
8470 /* Given a branch to a label, see if the branch is conforming.
8471 The code node describes where the branch is located. */
8473 static void
8474 resolve_branch (gfc_st_label *label, gfc_code *code)
8476 code_stack *stack;
8478 if (label == NULL)
8479 return;
8481 /* Step one: is this a valid branching target? */
8483 if (label->defined == ST_LABEL_UNKNOWN)
8485 gfc_error ("Label %d referenced at %L is never defined", label->value,
8486 &label->where);
8487 return;
8490 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
8492 gfc_error ("Statement at %L is not a valid branch target statement "
8493 "for the branch statement at %L", &label->where, &code->loc);
8494 return;
8497 /* Step two: make sure this branch is not a branch to itself ;-) */
8499 if (code->here == label)
8501 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8502 return;
8505 /* Step three: See if the label is in the same block as the
8506 branching statement. The hard work has been done by setting up
8507 the bitmap reachable_labels. */
8509 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8511 /* Check now whether there is a CRITICAL construct; if so, check
8512 whether the label is still visible outside of the CRITICAL block,
8513 which is invalid. */
8514 for (stack = cs_base; stack; stack = stack->prev)
8516 if (stack->current->op == EXEC_CRITICAL
8517 && bitmap_bit_p (stack->reachable_labels, label->value))
8518 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8519 "label at %L", &code->loc, &label->where);
8520 else if (stack->current->op == EXEC_DO_CONCURRENT
8521 && bitmap_bit_p (stack->reachable_labels, label->value))
8522 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8523 "for label at %L", &code->loc, &label->where);
8526 return;
8529 /* Step four: If we haven't found the label in the bitmap, it may
8530 still be the label of the END of the enclosing block, in which
8531 case we find it by going up the code_stack. */
8533 for (stack = cs_base; stack; stack = stack->prev)
8535 if (stack->current->next && stack->current->next->here == label)
8536 break;
8537 if (stack->current->op == EXEC_CRITICAL)
8539 /* Note: A label at END CRITICAL does not leave the CRITICAL
8540 construct as END CRITICAL is still part of it. */
8541 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8542 " at %L", &code->loc, &label->where);
8543 return;
8545 else if (stack->current->op == EXEC_DO_CONCURRENT)
8547 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
8548 "label at %L", &code->loc, &label->where);
8549 return;
8553 if (stack)
8555 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
8556 return;
8559 /* The label is not in an enclosing block, so illegal. This was
8560 allowed in Fortran 66, so we allow it as extension. No
8561 further checks are necessary in this case. */
8562 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8563 "as the GOTO statement at %L", &label->where,
8564 &code->loc);
8565 return;
8569 /* Check whether EXPR1 has the same shape as EXPR2. */
8571 static bool
8572 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8574 mpz_t shape[GFC_MAX_DIMENSIONS];
8575 mpz_t shape2[GFC_MAX_DIMENSIONS];
8576 bool result = false;
8577 int i;
8579 /* Compare the rank. */
8580 if (expr1->rank != expr2->rank)
8581 return result;
8583 /* Compare the size of each dimension. */
8584 for (i=0; i<expr1->rank; i++)
8586 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
8587 goto ignore;
8589 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
8590 goto ignore;
8592 if (mpz_cmp (shape[i], shape2[i]))
8593 goto over;
8596 /* When either of the two expression is an assumed size array, we
8597 ignore the comparison of dimension sizes. */
8598 ignore:
8599 result = true;
8601 over:
8602 gfc_clear_shape (shape, i);
8603 gfc_clear_shape (shape2, i);
8604 return result;
8608 /* Check whether a WHERE assignment target or a WHERE mask expression
8609 has the same shape as the outmost WHERE mask expression. */
8611 static void
8612 resolve_where (gfc_code *code, gfc_expr *mask)
8614 gfc_code *cblock;
8615 gfc_code *cnext;
8616 gfc_expr *e = NULL;
8618 cblock = code->block;
8620 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8621 In case of nested WHERE, only the outmost one is stored. */
8622 if (mask == NULL) /* outmost WHERE */
8623 e = cblock->expr1;
8624 else /* inner WHERE */
8625 e = mask;
8627 while (cblock)
8629 if (cblock->expr1)
8631 /* Check if the mask-expr has a consistent shape with the
8632 outmost WHERE mask-expr. */
8633 if (!resolve_where_shape (cblock->expr1, e))
8634 gfc_error ("WHERE mask at %L has inconsistent shape",
8635 &cblock->expr1->where);
8638 /* the assignment statement of a WHERE statement, or the first
8639 statement in where-body-construct of a WHERE construct */
8640 cnext = cblock->next;
8641 while (cnext)
8643 switch (cnext->op)
8645 /* WHERE assignment statement */
8646 case EXEC_ASSIGN:
8648 /* Check shape consistent for WHERE assignment target. */
8649 if (e && !resolve_where_shape (cnext->expr1, e))
8650 gfc_error ("WHERE assignment target at %L has "
8651 "inconsistent shape", &cnext->expr1->where);
8652 break;
8655 case EXEC_ASSIGN_CALL:
8656 resolve_call (cnext);
8657 if (!cnext->resolved_sym->attr.elemental)
8658 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8659 &cnext->ext.actual->expr->where);
8660 break;
8662 /* WHERE or WHERE construct is part of a where-body-construct */
8663 case EXEC_WHERE:
8664 resolve_where (cnext, e);
8665 break;
8667 default:
8668 gfc_error ("Unsupported statement inside WHERE at %L",
8669 &cnext->loc);
8671 /* the next statement within the same where-body-construct */
8672 cnext = cnext->next;
8674 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8675 cblock = cblock->block;
8680 /* Resolve assignment in FORALL construct.
8681 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8682 FORALL index variables. */
8684 static void
8685 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8687 int n;
8689 for (n = 0; n < nvar; n++)
8691 gfc_symbol *forall_index;
8693 forall_index = var_expr[n]->symtree->n.sym;
8695 /* Check whether the assignment target is one of the FORALL index
8696 variable. */
8697 if ((code->expr1->expr_type == EXPR_VARIABLE)
8698 && (code->expr1->symtree->n.sym == forall_index))
8699 gfc_error ("Assignment to a FORALL index variable at %L",
8700 &code->expr1->where);
8701 else
8703 /* If one of the FORALL index variables doesn't appear in the
8704 assignment variable, then there could be a many-to-one
8705 assignment. Emit a warning rather than an error because the
8706 mask could be resolving this problem. */
8707 if (!find_forall_index (code->expr1, forall_index, 0))
8708 gfc_warning ("The FORALL with index '%s' is not used on the "
8709 "left side of the assignment at %L and so might "
8710 "cause multiple assignment to this object",
8711 var_expr[n]->symtree->name, &code->expr1->where);
8717 /* Resolve WHERE statement in FORALL construct. */
8719 static void
8720 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8721 gfc_expr **var_expr)
8723 gfc_code *cblock;
8724 gfc_code *cnext;
8726 cblock = code->block;
8727 while (cblock)
8729 /* the assignment statement of a WHERE statement, or the first
8730 statement in where-body-construct of a WHERE construct */
8731 cnext = cblock->next;
8732 while (cnext)
8734 switch (cnext->op)
8736 /* WHERE assignment statement */
8737 case EXEC_ASSIGN:
8738 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8739 break;
8741 /* WHERE operator assignment statement */
8742 case EXEC_ASSIGN_CALL:
8743 resolve_call (cnext);
8744 if (!cnext->resolved_sym->attr.elemental)
8745 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8746 &cnext->ext.actual->expr->where);
8747 break;
8749 /* WHERE or WHERE construct is part of a where-body-construct */
8750 case EXEC_WHERE:
8751 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8752 break;
8754 default:
8755 gfc_error ("Unsupported statement inside WHERE at %L",
8756 &cnext->loc);
8758 /* the next statement within the same where-body-construct */
8759 cnext = cnext->next;
8761 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8762 cblock = cblock->block;
8767 /* Traverse the FORALL body to check whether the following errors exist:
8768 1. For assignment, check if a many-to-one assignment happens.
8769 2. For WHERE statement, check the WHERE body to see if there is any
8770 many-to-one assignment. */
8772 static void
8773 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8775 gfc_code *c;
8777 c = code->block->next;
8778 while (c)
8780 switch (c->op)
8782 case EXEC_ASSIGN:
8783 case EXEC_POINTER_ASSIGN:
8784 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8785 break;
8787 case EXEC_ASSIGN_CALL:
8788 resolve_call (c);
8789 break;
8791 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8792 there is no need to handle it here. */
8793 case EXEC_FORALL:
8794 break;
8795 case EXEC_WHERE:
8796 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8797 break;
8798 default:
8799 break;
8801 /* The next statement in the FORALL body. */
8802 c = c->next;
8807 /* Counts the number of iterators needed inside a forall construct, including
8808 nested forall constructs. This is used to allocate the needed memory
8809 in gfc_resolve_forall. */
8811 static int
8812 gfc_count_forall_iterators (gfc_code *code)
8814 int max_iters, sub_iters, current_iters;
8815 gfc_forall_iterator *fa;
8817 gcc_assert(code->op == EXEC_FORALL);
8818 max_iters = 0;
8819 current_iters = 0;
8821 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8822 current_iters ++;
8824 code = code->block->next;
8826 while (code)
8828 if (code->op == EXEC_FORALL)
8830 sub_iters = gfc_count_forall_iterators (code);
8831 if (sub_iters > max_iters)
8832 max_iters = sub_iters;
8834 code = code->next;
8837 return current_iters + max_iters;
8841 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8842 gfc_resolve_forall_body to resolve the FORALL body. */
8844 static void
8845 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8847 static gfc_expr **var_expr;
8848 static int total_var = 0;
8849 static int nvar = 0;
8850 int old_nvar, tmp;
8851 gfc_forall_iterator *fa;
8852 int i;
8854 old_nvar = nvar;
8856 /* Start to resolve a FORALL construct */
8857 if (forall_save == 0)
8859 /* Count the total number of FORALL index in the nested FORALL
8860 construct in order to allocate the VAR_EXPR with proper size. */
8861 total_var = gfc_count_forall_iterators (code);
8863 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8864 var_expr = XCNEWVEC (gfc_expr *, total_var);
8867 /* The information about FORALL iterator, including FORALL index start, end
8868 and stride. The FORALL index can not appear in start, end or stride. */
8869 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8871 /* Check if any outer FORALL index name is the same as the current
8872 one. */
8873 for (i = 0; i < nvar; i++)
8875 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8877 gfc_error ("An outer FORALL construct already has an index "
8878 "with this name %L", &fa->var->where);
8882 /* Record the current FORALL index. */
8883 var_expr[nvar] = gfc_copy_expr (fa->var);
8885 nvar++;
8887 /* No memory leak. */
8888 gcc_assert (nvar <= total_var);
8891 /* Resolve the FORALL body. */
8892 gfc_resolve_forall_body (code, nvar, var_expr);
8894 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8895 gfc_resolve_blocks (code->block, ns);
8897 tmp = nvar;
8898 nvar = old_nvar;
8899 /* Free only the VAR_EXPRs allocated in this frame. */
8900 for (i = nvar; i < tmp; i++)
8901 gfc_free_expr (var_expr[i]);
8903 if (nvar == 0)
8905 /* We are in the outermost FORALL construct. */
8906 gcc_assert (forall_save == 0);
8908 /* VAR_EXPR is not needed any more. */
8909 free (var_expr);
8910 total_var = 0;
8915 /* Resolve a BLOCK construct statement. */
8917 static void
8918 resolve_block_construct (gfc_code* code)
8920 /* Resolve the BLOCK's namespace. */
8921 gfc_resolve (code->ext.block.ns);
8923 /* For an ASSOCIATE block, the associations (and their targets) are already
8924 resolved during resolve_symbol. */
8928 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8929 DO code nodes. */
8931 static void resolve_code (gfc_code *, gfc_namespace *);
8933 void
8934 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8936 bool t;
8938 for (; b; b = b->block)
8940 t = gfc_resolve_expr (b->expr1);
8941 if (!gfc_resolve_expr (b->expr2))
8942 t = false;
8944 switch (b->op)
8946 case EXEC_IF:
8947 if (t && b->expr1 != NULL
8948 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
8949 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8950 &b->expr1->where);
8951 break;
8953 case EXEC_WHERE:
8954 if (t
8955 && b->expr1 != NULL
8956 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
8957 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8958 &b->expr1->where);
8959 break;
8961 case EXEC_GOTO:
8962 resolve_branch (b->label1, b);
8963 break;
8965 case EXEC_BLOCK:
8966 resolve_block_construct (b);
8967 break;
8969 case EXEC_SELECT:
8970 case EXEC_SELECT_TYPE:
8971 case EXEC_FORALL:
8972 case EXEC_DO:
8973 case EXEC_DO_WHILE:
8974 case EXEC_DO_CONCURRENT:
8975 case EXEC_CRITICAL:
8976 case EXEC_READ:
8977 case EXEC_WRITE:
8978 case EXEC_IOLENGTH:
8979 case EXEC_WAIT:
8980 break;
8982 case EXEC_OMP_ATOMIC:
8983 case EXEC_OMP_CRITICAL:
8984 case EXEC_OMP_DO:
8985 case EXEC_OMP_MASTER:
8986 case EXEC_OMP_ORDERED:
8987 case EXEC_OMP_PARALLEL:
8988 case EXEC_OMP_PARALLEL_DO:
8989 case EXEC_OMP_PARALLEL_SECTIONS:
8990 case EXEC_OMP_PARALLEL_WORKSHARE:
8991 case EXEC_OMP_SECTIONS:
8992 case EXEC_OMP_SINGLE:
8993 case EXEC_OMP_TASK:
8994 case EXEC_OMP_TASKWAIT:
8995 case EXEC_OMP_TASKYIELD:
8996 case EXEC_OMP_WORKSHARE:
8997 break;
8999 default:
9000 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
9003 resolve_code (b->next, ns);
9008 /* Does everything to resolve an ordinary assignment. Returns true
9009 if this is an interface assignment. */
9010 static bool
9011 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9013 bool rval = false;
9014 gfc_expr *lhs;
9015 gfc_expr *rhs;
9016 int llen = 0;
9017 int rlen = 0;
9018 int n;
9019 gfc_ref *ref;
9020 symbol_attribute attr;
9022 if (gfc_extend_assign (code, ns))
9024 gfc_expr** rhsptr;
9026 if (code->op == EXEC_ASSIGN_CALL)
9028 lhs = code->ext.actual->expr;
9029 rhsptr = &code->ext.actual->next->expr;
9031 else
9033 gfc_actual_arglist* args;
9034 gfc_typebound_proc* tbp;
9036 gcc_assert (code->op == EXEC_COMPCALL);
9038 args = code->expr1->value.compcall.actual;
9039 lhs = args->expr;
9040 rhsptr = &args->next->expr;
9042 tbp = code->expr1->value.compcall.tbp;
9043 gcc_assert (!tbp->is_generic);
9046 /* Make a temporary rhs when there is a default initializer
9047 and rhs is the same symbol as the lhs. */
9048 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9049 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9050 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9051 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9052 *rhsptr = gfc_get_parentheses (*rhsptr);
9054 return true;
9057 lhs = code->expr1;
9058 rhs = code->expr2;
9060 if (rhs->is_boz
9061 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9062 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9063 &code->loc))
9064 return false;
9066 /* Handle the case of a BOZ literal on the RHS. */
9067 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9069 int rc;
9070 if (gfc_option.warn_surprising)
9071 gfc_warning ("BOZ literal at %L is bitwise transferred "
9072 "non-integer symbol '%s'", &code->loc,
9073 lhs->symtree->n.sym->name);
9075 if (!gfc_convert_boz (rhs, &lhs->ts))
9076 return false;
9077 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9079 if (rc == ARITH_UNDERFLOW)
9080 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9081 ". This check can be disabled with the option "
9082 "-fno-range-check", &rhs->where);
9083 else if (rc == ARITH_OVERFLOW)
9084 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9085 ". This check can be disabled with the option "
9086 "-fno-range-check", &rhs->where);
9087 else if (rc == ARITH_NAN)
9088 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9089 ". This check can be disabled with the option "
9090 "-fno-range-check", &rhs->where);
9091 return false;
9095 if (lhs->ts.type == BT_CHARACTER
9096 && gfc_option.warn_character_truncation)
9098 if (lhs->ts.u.cl != NULL
9099 && lhs->ts.u.cl->length != NULL
9100 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9101 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9103 if (rhs->expr_type == EXPR_CONSTANT)
9104 rlen = rhs->value.character.length;
9106 else if (rhs->ts.u.cl != NULL
9107 && rhs->ts.u.cl->length != NULL
9108 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9109 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9111 if (rlen && llen && rlen > llen)
9112 gfc_warning_now ("CHARACTER expression will be truncated "
9113 "in assignment (%d/%d) at %L",
9114 llen, rlen, &code->loc);
9117 /* Ensure that a vector index expression for the lvalue is evaluated
9118 to a temporary if the lvalue symbol is referenced in it. */
9119 if (lhs->rank)
9121 for (ref = lhs->ref; ref; ref= ref->next)
9122 if (ref->type == REF_ARRAY)
9124 for (n = 0; n < ref->u.ar.dimen; n++)
9125 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9126 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9127 ref->u.ar.start[n]))
9128 ref->u.ar.start[n]
9129 = gfc_get_parentheses (ref->u.ar.start[n]);
9133 if (gfc_pure (NULL))
9135 if (lhs->ts.type == BT_DERIVED
9136 && lhs->expr_type == EXPR_VARIABLE
9137 && lhs->ts.u.derived->attr.pointer_comp
9138 && rhs->expr_type == EXPR_VARIABLE
9139 && (gfc_impure_variable (rhs->symtree->n.sym)
9140 || gfc_is_coindexed (rhs)))
9142 /* F2008, C1283. */
9143 if (gfc_is_coindexed (rhs))
9144 gfc_error ("Coindexed expression at %L is assigned to "
9145 "a derived type variable with a POINTER "
9146 "component in a PURE procedure",
9147 &rhs->where);
9148 else
9149 gfc_error ("The impure variable at %L is assigned to "
9150 "a derived type variable with a POINTER "
9151 "component in a PURE procedure (12.6)",
9152 &rhs->where);
9153 return rval;
9156 /* Fortran 2008, C1283. */
9157 if (gfc_is_coindexed (lhs))
9159 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9160 "procedure", &rhs->where);
9161 return rval;
9165 if (gfc_implicit_pure (NULL))
9167 if (lhs->expr_type == EXPR_VARIABLE
9168 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9169 && lhs->symtree->n.sym->ns != gfc_current_ns)
9170 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9172 if (lhs->ts.type == BT_DERIVED
9173 && lhs->expr_type == EXPR_VARIABLE
9174 && lhs->ts.u.derived->attr.pointer_comp
9175 && rhs->expr_type == EXPR_VARIABLE
9176 && (gfc_impure_variable (rhs->symtree->n.sym)
9177 || gfc_is_coindexed (rhs)))
9178 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9180 /* Fortran 2008, C1283. */
9181 if (gfc_is_coindexed (lhs))
9182 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9185 /* F2008, 7.2.1.2. */
9186 attr = gfc_expr_attr (lhs);
9187 if (lhs->ts.type == BT_CLASS && attr.allocatable)
9189 if (attr.codimension)
9191 gfc_error ("Assignment to polymorphic coarray at %L is not "
9192 "permitted", &lhs->where);
9193 return false;
9195 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
9196 "polymorphic variable at %L", &lhs->where))
9197 return false;
9198 if (!gfc_option.flag_realloc_lhs)
9200 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9201 "requires -frealloc-lhs", &lhs->where);
9202 return false;
9204 /* See PR 43366. */
9205 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9206 "is not yet supported", &lhs->where);
9207 return false;
9209 else if (lhs->ts.type == BT_CLASS)
9211 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
9212 "assignment at %L - check that there is a matching specific "
9213 "subroutine for '=' operator", &lhs->where);
9214 return false;
9217 /* F2008, Section 7.2.1.2. */
9218 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
9220 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
9221 "component in assignment at %L", &lhs->where);
9222 return false;
9225 gfc_check_assign (lhs, rhs, 1);
9226 return false;
9230 /* Add a component reference onto an expression. */
9232 static void
9233 add_comp_ref (gfc_expr *e, gfc_component *c)
9235 gfc_ref **ref;
9236 ref = &(e->ref);
9237 while (*ref)
9238 ref = &((*ref)->next);
9239 *ref = gfc_get_ref ();
9240 (*ref)->type = REF_COMPONENT;
9241 (*ref)->u.c.sym = e->ts.u.derived;
9242 (*ref)->u.c.component = c;
9243 e->ts = c->ts;
9245 /* Add a full array ref, as necessary. */
9246 if (c->as)
9248 gfc_add_full_array_ref (e, c->as);
9249 e->rank = c->as->rank;
9254 /* Build an assignment. Keep the argument 'op' for future use, so that
9255 pointer assignments can be made. */
9257 static gfc_code *
9258 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9259 gfc_component *comp1, gfc_component *comp2, locus loc)
9261 gfc_code *this_code;
9263 this_code = gfc_get_code (op);
9264 this_code->next = NULL;
9265 this_code->expr1 = gfc_copy_expr (expr1);
9266 this_code->expr2 = gfc_copy_expr (expr2);
9267 this_code->loc = loc;
9268 if (comp1 && comp2)
9270 add_comp_ref (this_code->expr1, comp1);
9271 add_comp_ref (this_code->expr2, comp2);
9274 return this_code;
9278 /* Makes a temporary variable expression based on the characteristics of
9279 a given variable expression. */
9281 static gfc_expr*
9282 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9284 static int serial = 0;
9285 char name[GFC_MAX_SYMBOL_LEN];
9286 gfc_symtree *tmp;
9287 gfc_array_spec *as;
9288 gfc_array_ref *aref;
9289 gfc_ref *ref;
9291 sprintf (name, GFC_PREFIX("DA%d"), serial++);
9292 gfc_get_sym_tree (name, ns, &tmp, false);
9293 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9295 as = NULL;
9296 ref = NULL;
9297 aref = NULL;
9299 /* This function could be expanded to support other expression type
9300 but this is not needed here. */
9301 gcc_assert (e->expr_type == EXPR_VARIABLE);
9303 /* Obtain the arrayspec for the temporary. */
9304 if (e->rank)
9306 aref = gfc_find_array_ref (e);
9307 if (e->expr_type == EXPR_VARIABLE
9308 && e->symtree->n.sym->as == aref->as)
9309 as = aref->as;
9310 else
9312 for (ref = e->ref; ref; ref = ref->next)
9313 if (ref->type == REF_COMPONENT
9314 && ref->u.c.component->as == aref->as)
9316 as = aref->as;
9317 break;
9322 /* Add the attributes and the arrayspec to the temporary. */
9323 tmp->n.sym->attr = gfc_expr_attr (e);
9324 tmp->n.sym->attr.function = 0;
9325 tmp->n.sym->attr.result = 0;
9326 tmp->n.sym->attr.flavor = FL_VARIABLE;
9328 if (as)
9330 tmp->n.sym->as = gfc_copy_array_spec (as);
9331 if (!ref)
9332 ref = e->ref;
9333 if (as->type == AS_DEFERRED)
9334 tmp->n.sym->attr.allocatable = 1;
9336 else
9337 tmp->n.sym->attr.dimension = 0;
9339 gfc_set_sym_referenced (tmp->n.sym);
9340 gfc_commit_symbol (tmp->n.sym);
9341 e = gfc_lval_expr_from_sym (tmp->n.sym);
9343 /* Should the lhs be a section, use its array ref for the
9344 temporary expression. */
9345 if (aref && aref->type != AR_FULL)
9347 gfc_free_ref_list (e->ref);
9348 e->ref = gfc_copy_ref (ref);
9350 return e;
9354 /* Add one line of code to the code chain, making sure that 'head' and
9355 'tail' are appropriately updated. */
9357 static void
9358 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9360 gcc_assert (this_code);
9361 if (*head == NULL)
9362 *head = *tail = *this_code;
9363 else
9364 *tail = gfc_append_code (*tail, *this_code);
9365 *this_code = NULL;
9369 /* Counts the potential number of part array references that would
9370 result from resolution of typebound defined assignments. */
9372 static int
9373 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9375 gfc_component *c;
9376 int c_depth = 0, t_depth;
9378 for (c= derived->components; c; c = c->next)
9380 if ((c->ts.type != BT_DERIVED
9381 || c->attr.pointer
9382 || c->attr.allocatable
9383 || c->attr.proc_pointer_comp
9384 || c->attr.class_pointer
9385 || c->attr.proc_pointer)
9386 && !c->attr.defined_assign_comp)
9387 continue;
9389 if (c->as && c_depth == 0)
9390 c_depth = 1;
9392 if (c->ts.u.derived->attr.defined_assign_comp)
9393 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9394 c->as ? 1 : 0);
9395 else
9396 t_depth = 0;
9398 c_depth = t_depth > c_depth ? t_depth : c_depth;
9400 return depth + c_depth;
9404 /* Implement 7.2.1.3 of the F08 standard:
9405 "An intrinsic assignment where the variable is of derived type is
9406 performed as if each component of the variable were assigned from the
9407 corresponding component of expr using pointer assignment (7.2.2) for
9408 each pointer component, defined assignment for each nonpointer
9409 nonallocatable component of a type that has a type-bound defined
9410 assignment consistent with the component, intrinsic assignment for
9411 each other nonpointer nonallocatable component, ..."
9413 The pointer assignments are taken care of by the intrinsic
9414 assignment of the structure itself. This function recursively adds
9415 defined assignments where required. The recursion is accomplished
9416 by calling resolve_code.
9418 When the lhs in a defined assignment has intent INOUT, we need a
9419 temporary for the lhs. In pseudo-code:
9421 ! Only call function lhs once.
9422 if (lhs is not a constant or an variable)
9423 temp_x = expr2
9424 expr2 => temp_x
9425 ! Do the intrinsic assignment
9426 expr1 = expr2
9427 ! Now do the defined assignments
9428 do over components with typebound defined assignment [%cmp]
9429 #if one component's assignment procedure is INOUT
9430 t1 = expr1
9431 #if expr2 non-variable
9432 temp_x = expr2
9433 expr2 => temp_x
9434 # endif
9435 expr1 = expr2
9436 # for each cmp
9437 t1%cmp {defined=} expr2%cmp
9438 expr1%cmp = t1%cmp
9439 #else
9440 expr1 = expr2
9442 # for each cmp
9443 expr1%cmp {defined=} expr2%cmp
9444 #endif
9447 /* The temporary assignments have to be put on top of the additional
9448 code to avoid the result being changed by the intrinsic assignment.
9450 static int component_assignment_level = 0;
9451 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
9453 static void
9454 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
9456 gfc_component *comp1, *comp2;
9457 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
9458 gfc_expr *t1;
9459 int error_count, depth;
9461 gfc_get_errors (NULL, &error_count);
9463 /* Filter out continuing processing after an error. */
9464 if (error_count
9465 || (*code)->expr1->ts.type != BT_DERIVED
9466 || (*code)->expr2->ts.type != BT_DERIVED)
9467 return;
9469 /* TODO: Handle more than one part array reference in assignments. */
9470 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
9471 (*code)->expr1->rank ? 1 : 0);
9472 if (depth > 1)
9474 gfc_warning ("TODO: type-bound defined assignment(s) at %L not "
9475 "done because multiple part array references would "
9476 "occur in intermediate expressions.", &(*code)->loc);
9477 return;
9480 component_assignment_level++;
9482 /* Create a temporary so that functions get called only once. */
9483 if ((*code)->expr2->expr_type != EXPR_VARIABLE
9484 && (*code)->expr2->expr_type != EXPR_CONSTANT)
9486 gfc_expr *tmp_expr;
9488 /* Assign the rhs to the temporary. */
9489 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
9490 this_code = build_assignment (EXEC_ASSIGN,
9491 tmp_expr, (*code)->expr2,
9492 NULL, NULL, (*code)->loc);
9493 /* Add the code and substitute the rhs expression. */
9494 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
9495 gfc_free_expr ((*code)->expr2);
9496 (*code)->expr2 = tmp_expr;
9499 /* Do the intrinsic assignment. This is not needed if the lhs is one
9500 of the temporaries generated here, since the intrinsic assignment
9501 to the final result already does this. */
9502 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
9504 this_code = build_assignment (EXEC_ASSIGN,
9505 (*code)->expr1, (*code)->expr2,
9506 NULL, NULL, (*code)->loc);
9507 add_code_to_chain (&this_code, &head, &tail);
9510 comp1 = (*code)->expr1->ts.u.derived->components;
9511 comp2 = (*code)->expr2->ts.u.derived->components;
9513 t1 = NULL;
9514 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
9516 bool inout = false;
9518 /* The intrinsic assignment does the right thing for pointers
9519 of all kinds and allocatable components. */
9520 if (comp1->ts.type != BT_DERIVED
9521 || comp1->attr.pointer
9522 || comp1->attr.allocatable
9523 || comp1->attr.proc_pointer_comp
9524 || comp1->attr.class_pointer
9525 || comp1->attr.proc_pointer)
9526 continue;
9528 /* Make an assigment for this component. */
9529 this_code = build_assignment (EXEC_ASSIGN,
9530 (*code)->expr1, (*code)->expr2,
9531 comp1, comp2, (*code)->loc);
9533 /* Convert the assignment if there is a defined assignment for
9534 this type. Otherwise, using the call from resolve_code,
9535 recurse into its components. */
9536 resolve_code (this_code, ns);
9538 if (this_code->op == EXEC_ASSIGN_CALL)
9540 gfc_formal_arglist *dummy_args;
9541 gfc_symbol *rsym;
9542 /* Check that there is a typebound defined assignment. If not,
9543 then this must be a module defined assignment. We cannot
9544 use the defined_assign_comp attribute here because it must
9545 be this derived type that has the defined assignment and not
9546 a parent type. */
9547 if (!(comp1->ts.u.derived->f2k_derived
9548 && comp1->ts.u.derived->f2k_derived
9549 ->tb_op[INTRINSIC_ASSIGN]))
9551 gfc_free_statements (this_code);
9552 this_code = NULL;
9553 continue;
9556 /* If the first argument of the subroutine has intent INOUT
9557 a temporary must be generated and used instead. */
9558 rsym = this_code->resolved_sym;
9559 dummy_args = gfc_sym_get_dummy_args (rsym);
9560 if (dummy_args
9561 && dummy_args->sym->attr.intent == INTENT_INOUT)
9563 gfc_code *temp_code;
9564 inout = true;
9566 /* Build the temporary required for the assignment and put
9567 it at the head of the generated code. */
9568 if (!t1)
9570 t1 = get_temp_from_expr ((*code)->expr1, ns);
9571 temp_code = build_assignment (EXEC_ASSIGN,
9572 t1, (*code)->expr1,
9573 NULL, NULL, (*code)->loc);
9575 /* For allocatable LHS, check whether it is allocated. Note
9576 that allocatable components with defined assignment are
9577 not yet support. See PR 57696. */
9578 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
9580 gfc_code *block;
9581 gfc_expr *e =
9582 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
9583 block = gfc_get_code (EXEC_IF);
9584 block->block = gfc_get_code (EXEC_IF);
9585 block->block->expr1
9586 = gfc_build_intrinsic_call (ns,
9587 GFC_ISYM_ALLOCATED, "allocated",
9588 (*code)->loc, 1, e);
9589 block->block->next = temp_code;
9590 temp_code = block;
9592 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
9595 /* Replace the first actual arg with the component of the
9596 temporary. */
9597 gfc_free_expr (this_code->ext.actual->expr);
9598 this_code->ext.actual->expr = gfc_copy_expr (t1);
9599 add_comp_ref (this_code->ext.actual->expr, comp1);
9601 /* If the LHS variable is allocatable and wasn't allocated and
9602 the temporary is allocatable, pointer assign the address of
9603 the freshly allocated LHS to the temporary. */
9604 if ((*code)->expr1->symtree->n.sym->attr.allocatable
9605 && gfc_expr_attr ((*code)->expr1).allocatable)
9607 gfc_code *block;
9608 gfc_expr *cond;
9610 cond = gfc_get_expr ();
9611 cond->ts.type = BT_LOGICAL;
9612 cond->ts.kind = gfc_default_logical_kind;
9613 cond->expr_type = EXPR_OP;
9614 cond->where = (*code)->loc;
9615 cond->value.op.op = INTRINSIC_NOT;
9616 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
9617 GFC_ISYM_ALLOCATED, "allocated",
9618 (*code)->loc, 1, gfc_copy_expr (t1));
9619 block = gfc_get_code (EXEC_IF);
9620 block->block = gfc_get_code (EXEC_IF);
9621 block->block->expr1 = cond;
9622 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
9623 t1, (*code)->expr1,
9624 NULL, NULL, (*code)->loc);
9625 add_code_to_chain (&block, &head, &tail);
9629 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
9631 /* Don't add intrinsic assignments since they are already
9632 effected by the intrinsic assignment of the structure. */
9633 gfc_free_statements (this_code);
9634 this_code = NULL;
9635 continue;
9638 add_code_to_chain (&this_code, &head, &tail);
9640 if (t1 && inout)
9642 /* Transfer the value to the final result. */
9643 this_code = build_assignment (EXEC_ASSIGN,
9644 (*code)->expr1, t1,
9645 comp1, comp2, (*code)->loc);
9646 add_code_to_chain (&this_code, &head, &tail);
9650 /* Put the temporary assignments at the top of the generated code. */
9651 if (tmp_head && component_assignment_level == 1)
9653 gfc_append_code (tmp_head, head);
9654 head = tmp_head;
9655 tmp_head = tmp_tail = NULL;
9658 // If we did a pointer assignment - thus, we need to ensure that the LHS is
9659 // not accidentally deallocated. Hence, nullify t1.
9660 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
9661 && gfc_expr_attr ((*code)->expr1).allocatable)
9663 gfc_code *block;
9664 gfc_expr *cond;
9665 gfc_expr *e;
9667 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
9668 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
9669 (*code)->loc, 2, gfc_copy_expr (t1), e);
9670 block = gfc_get_code (EXEC_IF);
9671 block->block = gfc_get_code (EXEC_IF);
9672 block->block->expr1 = cond;
9673 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
9674 t1, gfc_get_null_expr (&(*code)->loc),
9675 NULL, NULL, (*code)->loc);
9676 gfc_append_code (tail, block);
9677 tail = block;
9680 /* Now attach the remaining code chain to the input code. Step on
9681 to the end of the new code since resolution is complete. */
9682 gcc_assert ((*code)->op == EXEC_ASSIGN);
9683 tail->next = (*code)->next;
9684 /* Overwrite 'code' because this would place the intrinsic assignment
9685 before the temporary for the lhs is created. */
9686 gfc_free_expr ((*code)->expr1);
9687 gfc_free_expr ((*code)->expr2);
9688 **code = *head;
9689 if (head != tail)
9690 free (head);
9691 *code = tail;
9693 component_assignment_level--;
9697 /* Given a block of code, recursively resolve everything pointed to by this
9698 code block. */
9700 static void
9701 resolve_code (gfc_code *code, gfc_namespace *ns)
9703 int omp_workshare_save;
9704 int forall_save, do_concurrent_save;
9705 code_stack frame;
9706 bool t;
9708 frame.prev = cs_base;
9709 frame.head = code;
9710 cs_base = &frame;
9712 find_reachable_labels (code);
9714 for (; code; code = code->next)
9716 frame.current = code;
9717 forall_save = forall_flag;
9718 do_concurrent_save = gfc_do_concurrent_flag;
9720 if (code->op == EXEC_FORALL)
9722 forall_flag = 1;
9723 gfc_resolve_forall (code, ns, forall_save);
9724 forall_flag = 2;
9726 else if (code->block)
9728 omp_workshare_save = -1;
9729 switch (code->op)
9731 case EXEC_OMP_PARALLEL_WORKSHARE:
9732 omp_workshare_save = omp_workshare_flag;
9733 omp_workshare_flag = 1;
9734 gfc_resolve_omp_parallel_blocks (code, ns);
9735 break;
9736 case EXEC_OMP_PARALLEL:
9737 case EXEC_OMP_PARALLEL_DO:
9738 case EXEC_OMP_PARALLEL_SECTIONS:
9739 case EXEC_OMP_TASK:
9740 omp_workshare_save = omp_workshare_flag;
9741 omp_workshare_flag = 0;
9742 gfc_resolve_omp_parallel_blocks (code, ns);
9743 break;
9744 case EXEC_OMP_DO:
9745 gfc_resolve_omp_do_blocks (code, ns);
9746 break;
9747 case EXEC_SELECT_TYPE:
9748 /* Blocks are handled in resolve_select_type because we have
9749 to transform the SELECT TYPE into ASSOCIATE first. */
9750 break;
9751 case EXEC_DO_CONCURRENT:
9752 gfc_do_concurrent_flag = 1;
9753 gfc_resolve_blocks (code->block, ns);
9754 gfc_do_concurrent_flag = 2;
9755 break;
9756 case EXEC_OMP_WORKSHARE:
9757 omp_workshare_save = omp_workshare_flag;
9758 omp_workshare_flag = 1;
9759 /* FALL THROUGH */
9760 default:
9761 gfc_resolve_blocks (code->block, ns);
9762 break;
9765 if (omp_workshare_save != -1)
9766 omp_workshare_flag = omp_workshare_save;
9769 t = true;
9770 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
9771 t = gfc_resolve_expr (code->expr1);
9772 forall_flag = forall_save;
9773 gfc_do_concurrent_flag = do_concurrent_save;
9775 if (!gfc_resolve_expr (code->expr2))
9776 t = false;
9778 if (code->op == EXEC_ALLOCATE
9779 && !gfc_resolve_expr (code->expr3))
9780 t = false;
9782 switch (code->op)
9784 case EXEC_NOP:
9785 case EXEC_END_BLOCK:
9786 case EXEC_END_NESTED_BLOCK:
9787 case EXEC_CYCLE:
9788 case EXEC_PAUSE:
9789 case EXEC_STOP:
9790 case EXEC_ERROR_STOP:
9791 case EXEC_EXIT:
9792 case EXEC_CONTINUE:
9793 case EXEC_DT_END:
9794 case EXEC_ASSIGN_CALL:
9795 case EXEC_CRITICAL:
9796 break;
9798 case EXEC_SYNC_ALL:
9799 case EXEC_SYNC_IMAGES:
9800 case EXEC_SYNC_MEMORY:
9801 resolve_sync (code);
9802 break;
9804 case EXEC_LOCK:
9805 case EXEC_UNLOCK:
9806 resolve_lock_unlock (code);
9807 break;
9809 case EXEC_ENTRY:
9810 /* Keep track of which entry we are up to. */
9811 current_entry_id = code->ext.entry->id;
9812 break;
9814 case EXEC_WHERE:
9815 resolve_where (code, NULL);
9816 break;
9818 case EXEC_GOTO:
9819 if (code->expr1 != NULL)
9821 if (code->expr1->ts.type != BT_INTEGER)
9822 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9823 "INTEGER variable", &code->expr1->where);
9824 else if (code->expr1->symtree->n.sym->attr.assign != 1)
9825 gfc_error ("Variable '%s' has not been assigned a target "
9826 "label at %L", code->expr1->symtree->n.sym->name,
9827 &code->expr1->where);
9829 else
9830 resolve_branch (code->label1, code);
9831 break;
9833 case EXEC_RETURN:
9834 if (code->expr1 != NULL
9835 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
9836 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
9837 "INTEGER return specifier", &code->expr1->where);
9838 break;
9840 case EXEC_INIT_ASSIGN:
9841 case EXEC_END_PROCEDURE:
9842 break;
9844 case EXEC_ASSIGN:
9845 if (!t)
9846 break;
9848 if (!gfc_check_vardef_context (code->expr1, false, false, false,
9849 _("assignment")))
9850 break;
9852 if (resolve_ordinary_assign (code, ns))
9854 if (code->op == EXEC_COMPCALL)
9855 goto compcall;
9856 else
9857 goto call;
9860 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
9861 if (code->expr1->ts.type == BT_DERIVED
9862 && code->expr1->ts.u.derived->attr.defined_assign_comp)
9863 generate_component_assignments (&code, ns);
9865 break;
9867 case EXEC_LABEL_ASSIGN:
9868 if (code->label1->defined == ST_LABEL_UNKNOWN)
9869 gfc_error ("Label %d referenced at %L is never defined",
9870 code->label1->value, &code->label1->where);
9871 if (t
9872 && (code->expr1->expr_type != EXPR_VARIABLE
9873 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
9874 || code->expr1->symtree->n.sym->ts.kind
9875 != gfc_default_integer_kind
9876 || code->expr1->symtree->n.sym->as != NULL))
9877 gfc_error ("ASSIGN statement at %L requires a scalar "
9878 "default INTEGER variable", &code->expr1->where);
9879 break;
9881 case EXEC_POINTER_ASSIGN:
9883 gfc_expr* e;
9885 if (!t)
9886 break;
9888 /* This is both a variable definition and pointer assignment
9889 context, so check both of them. For rank remapping, a final
9890 array ref may be present on the LHS and fool gfc_expr_attr
9891 used in gfc_check_vardef_context. Remove it. */
9892 e = remove_last_array_ref (code->expr1);
9893 t = gfc_check_vardef_context (e, true, false, false,
9894 _("pointer assignment"));
9895 if (t)
9896 t = gfc_check_vardef_context (e, false, false, false,
9897 _("pointer assignment"));
9898 gfc_free_expr (e);
9899 if (!t)
9900 break;
9902 gfc_check_pointer_assign (code->expr1, code->expr2);
9903 break;
9906 case EXEC_ARITHMETIC_IF:
9907 if (t
9908 && code->expr1->ts.type != BT_INTEGER
9909 && code->expr1->ts.type != BT_REAL)
9910 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9911 "expression", &code->expr1->where);
9913 resolve_branch (code->label1, code);
9914 resolve_branch (code->label2, code);
9915 resolve_branch (code->label3, code);
9916 break;
9918 case EXEC_IF:
9919 if (t && code->expr1 != NULL
9920 && (code->expr1->ts.type != BT_LOGICAL
9921 || code->expr1->rank != 0))
9922 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9923 &code->expr1->where);
9924 break;
9926 case EXEC_CALL:
9927 call:
9928 resolve_call (code);
9929 break;
9931 case EXEC_COMPCALL:
9932 compcall:
9933 resolve_typebound_subroutine (code);
9934 break;
9936 case EXEC_CALL_PPC:
9937 resolve_ppc_call (code);
9938 break;
9940 case EXEC_SELECT:
9941 /* Select is complicated. Also, a SELECT construct could be
9942 a transformed computed GOTO. */
9943 resolve_select (code, false);
9944 break;
9946 case EXEC_SELECT_TYPE:
9947 resolve_select_type (code, ns);
9948 break;
9950 case EXEC_BLOCK:
9951 resolve_block_construct (code);
9952 break;
9954 case EXEC_DO:
9955 if (code->ext.iterator != NULL)
9957 gfc_iterator *iter = code->ext.iterator;
9958 if (gfc_resolve_iterator (iter, true, false))
9959 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
9961 break;
9963 case EXEC_DO_WHILE:
9964 if (code->expr1 == NULL)
9965 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9966 if (t
9967 && (code->expr1->rank != 0
9968 || code->expr1->ts.type != BT_LOGICAL))
9969 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9970 "a scalar LOGICAL expression", &code->expr1->where);
9971 break;
9973 case EXEC_ALLOCATE:
9974 if (t)
9975 resolve_allocate_deallocate (code, "ALLOCATE");
9977 break;
9979 case EXEC_DEALLOCATE:
9980 if (t)
9981 resolve_allocate_deallocate (code, "DEALLOCATE");
9983 break;
9985 case EXEC_OPEN:
9986 if (!gfc_resolve_open (code->ext.open))
9987 break;
9989 resolve_branch (code->ext.open->err, code);
9990 break;
9992 case EXEC_CLOSE:
9993 if (!gfc_resolve_close (code->ext.close))
9994 break;
9996 resolve_branch (code->ext.close->err, code);
9997 break;
9999 case EXEC_BACKSPACE:
10000 case EXEC_ENDFILE:
10001 case EXEC_REWIND:
10002 case EXEC_FLUSH:
10003 if (!gfc_resolve_filepos (code->ext.filepos))
10004 break;
10006 resolve_branch (code->ext.filepos->err, code);
10007 break;
10009 case EXEC_INQUIRE:
10010 if (!gfc_resolve_inquire (code->ext.inquire))
10011 break;
10013 resolve_branch (code->ext.inquire->err, code);
10014 break;
10016 case EXEC_IOLENGTH:
10017 gcc_assert (code->ext.inquire != NULL);
10018 if (!gfc_resolve_inquire (code->ext.inquire))
10019 break;
10021 resolve_branch (code->ext.inquire->err, code);
10022 break;
10024 case EXEC_WAIT:
10025 if (!gfc_resolve_wait (code->ext.wait))
10026 break;
10028 resolve_branch (code->ext.wait->err, code);
10029 resolve_branch (code->ext.wait->end, code);
10030 resolve_branch (code->ext.wait->eor, code);
10031 break;
10033 case EXEC_READ:
10034 case EXEC_WRITE:
10035 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
10036 break;
10038 resolve_branch (code->ext.dt->err, code);
10039 resolve_branch (code->ext.dt->end, code);
10040 resolve_branch (code->ext.dt->eor, code);
10041 break;
10043 case EXEC_TRANSFER:
10044 resolve_transfer (code);
10045 break;
10047 case EXEC_DO_CONCURRENT:
10048 case EXEC_FORALL:
10049 resolve_forall_iterators (code->ext.forall_iterator);
10051 if (code->expr1 != NULL
10052 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
10053 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
10054 "expression", &code->expr1->where);
10055 break;
10057 case EXEC_OMP_ATOMIC:
10058 case EXEC_OMP_BARRIER:
10059 case EXEC_OMP_CRITICAL:
10060 case EXEC_OMP_FLUSH:
10061 case EXEC_OMP_DO:
10062 case EXEC_OMP_MASTER:
10063 case EXEC_OMP_ORDERED:
10064 case EXEC_OMP_SECTIONS:
10065 case EXEC_OMP_SINGLE:
10066 case EXEC_OMP_TASKWAIT:
10067 case EXEC_OMP_TASKYIELD:
10068 case EXEC_OMP_WORKSHARE:
10069 gfc_resolve_omp_directive (code, ns);
10070 break;
10072 case EXEC_OMP_PARALLEL:
10073 case EXEC_OMP_PARALLEL_DO:
10074 case EXEC_OMP_PARALLEL_SECTIONS:
10075 case EXEC_OMP_PARALLEL_WORKSHARE:
10076 case EXEC_OMP_TASK:
10077 omp_workshare_save = omp_workshare_flag;
10078 omp_workshare_flag = 0;
10079 gfc_resolve_omp_directive (code, ns);
10080 omp_workshare_flag = omp_workshare_save;
10081 break;
10083 default:
10084 gfc_internal_error ("resolve_code(): Bad statement code");
10088 cs_base = frame.prev;
10092 /* Resolve initial values and make sure they are compatible with
10093 the variable. */
10095 static void
10096 resolve_values (gfc_symbol *sym)
10098 bool t;
10100 if (sym->value == NULL)
10101 return;
10103 if (sym->value->expr_type == EXPR_STRUCTURE)
10104 t= resolve_structure_cons (sym->value, 1);
10105 else
10106 t = gfc_resolve_expr (sym->value);
10108 if (!t)
10109 return;
10111 gfc_check_assign_symbol (sym, NULL, sym->value);
10115 /* Verify any BIND(C) derived types in the namespace so we can report errors
10116 for them once, rather than for each variable declared of that type. */
10118 static void
10119 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10121 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10122 && derived_sym->attr.is_bind_c == 1)
10123 verify_bind_c_derived_type (derived_sym);
10125 return;
10129 /* Verify that any binding labels used in a given namespace do not collide
10130 with the names or binding labels of any global symbols. Multiple INTERFACE
10131 for the same procedure are permitted. */
10133 static void
10134 gfc_verify_binding_labels (gfc_symbol *sym)
10136 gfc_gsymbol *gsym;
10137 const char *module;
10139 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
10140 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
10141 return;
10143 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10145 if (sym->module)
10146 module = sym->module;
10147 else if (sym->ns && sym->ns->proc_name
10148 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10149 module = sym->ns->proc_name->name;
10150 else if (sym->ns && sym->ns->parent
10151 && sym->ns && sym->ns->parent->proc_name
10152 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10153 module = sym->ns->parent->proc_name->name;
10154 else
10155 module = NULL;
10157 if (!gsym
10158 || (!gsym->defined
10159 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
10161 if (!gsym)
10162 gsym = gfc_get_gsymbol (sym->binding_label);
10163 gsym->where = sym->declared_at;
10164 gsym->sym_name = sym->name;
10165 gsym->binding_label = sym->binding_label;
10166 gsym->binding_label = sym->binding_label;
10167 gsym->ns = sym->ns;
10168 gsym->mod_name = module;
10169 if (sym->attr.function)
10170 gsym->type = GSYM_FUNCTION;
10171 else if (sym->attr.subroutine)
10172 gsym->type = GSYM_SUBROUTINE;
10173 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
10174 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
10175 return;
10178 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
10180 gfc_error ("Variable %s with binding label %s at %L uses the same global "
10181 "identifier as entity at %L", sym->name,
10182 sym->binding_label, &sym->declared_at, &gsym->where);
10183 /* Clear the binding label to prevent checking multiple times. */
10184 sym->binding_label = NULL;
10187 else if (sym->attr.flavor == FL_VARIABLE
10188 && (strcmp (module, gsym->mod_name) != 0
10189 || strcmp (sym->name, gsym->sym_name) != 0))
10191 /* This can only happen if the variable is defined in a module - if it
10192 isn't the same module, reject it. */
10193 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
10194 "the same global identifier as entity at %L from module %s",
10195 sym->name, module, sym->binding_label,
10196 &sym->declared_at, &gsym->where, gsym->mod_name);
10197 sym->binding_label = NULL;
10199 else if ((sym->attr.function || sym->attr.subroutine)
10200 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
10201 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
10202 && sym != gsym->ns->proc_name
10203 && (strcmp (gsym->sym_name, sym->name) != 0
10204 || module != gsym->mod_name
10205 || (module && strcmp (module, gsym->mod_name) != 0)))
10207 /* Print an error if the procdure is defined multiple times; we have to
10208 exclude references to the same procedure via module association or
10209 multiple checks for the same procedure. */
10210 gfc_error ("Procedure %s with binding label %s at %L uses the same "
10211 "global identifier as entity at %L", sym->name,
10212 sym->binding_label, &sym->declared_at, &gsym->where);
10213 sym->binding_label = NULL;
10218 /* Resolve an index expression. */
10220 static bool
10221 resolve_index_expr (gfc_expr *e)
10223 if (!gfc_resolve_expr (e))
10224 return false;
10226 if (!gfc_simplify_expr (e, 0))
10227 return false;
10229 if (!gfc_specification_expr (e))
10230 return false;
10232 return true;
10236 /* Resolve a charlen structure. */
10238 static bool
10239 resolve_charlen (gfc_charlen *cl)
10241 int i, k;
10242 bool saved_specification_expr;
10244 if (cl->resolved)
10245 return true;
10247 cl->resolved = 1;
10248 saved_specification_expr = specification_expr;
10249 specification_expr = true;
10251 if (cl->length_from_typespec)
10253 if (!gfc_resolve_expr (cl->length))
10255 specification_expr = saved_specification_expr;
10256 return false;
10259 if (!gfc_simplify_expr (cl->length, 0))
10261 specification_expr = saved_specification_expr;
10262 return false;
10265 else
10268 if (!resolve_index_expr (cl->length))
10270 specification_expr = saved_specification_expr;
10271 return false;
10275 /* "If the character length parameter value evaluates to a negative
10276 value, the length of character entities declared is zero." */
10277 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
10279 if (gfc_option.warn_surprising)
10280 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
10281 " the length has been set to zero",
10282 &cl->length->where, i);
10283 gfc_replace_expr (cl->length,
10284 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
10287 /* Check that the character length is not too large. */
10288 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
10289 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
10290 && cl->length->ts.type == BT_INTEGER
10291 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
10293 gfc_error ("String length at %L is too large", &cl->length->where);
10294 specification_expr = saved_specification_expr;
10295 return false;
10298 specification_expr = saved_specification_expr;
10299 return true;
10303 /* Test for non-constant shape arrays. */
10305 static bool
10306 is_non_constant_shape_array (gfc_symbol *sym)
10308 gfc_expr *e;
10309 int i;
10310 bool not_constant;
10312 not_constant = false;
10313 if (sym->as != NULL)
10315 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
10316 has not been simplified; parameter array references. Do the
10317 simplification now. */
10318 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
10320 e = sym->as->lower[i];
10321 if (e && (!resolve_index_expr(e)
10322 || !gfc_is_constant_expr (e)))
10323 not_constant = true;
10324 e = sym->as->upper[i];
10325 if (e && (!resolve_index_expr(e)
10326 || !gfc_is_constant_expr (e)))
10327 not_constant = true;
10330 return not_constant;
10333 /* Given a symbol and an initialization expression, add code to initialize
10334 the symbol to the function entry. */
10335 static void
10336 build_init_assign (gfc_symbol *sym, gfc_expr *init)
10338 gfc_expr *lval;
10339 gfc_code *init_st;
10340 gfc_namespace *ns = sym->ns;
10342 /* Search for the function namespace if this is a contained
10343 function without an explicit result. */
10344 if (sym->attr.function && sym == sym->result
10345 && sym->name != sym->ns->proc_name->name)
10347 ns = ns->contained;
10348 for (;ns; ns = ns->sibling)
10349 if (strcmp (ns->proc_name->name, sym->name) == 0)
10350 break;
10353 if (ns == NULL)
10355 gfc_free_expr (init);
10356 return;
10359 /* Build an l-value expression for the result. */
10360 lval = gfc_lval_expr_from_sym (sym);
10362 /* Add the code at scope entry. */
10363 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
10364 init_st->next = ns->code;
10365 ns->code = init_st;
10367 /* Assign the default initializer to the l-value. */
10368 init_st->loc = sym->declared_at;
10369 init_st->expr1 = lval;
10370 init_st->expr2 = init;
10373 /* Assign the default initializer to a derived type variable or result. */
10375 static void
10376 apply_default_init (gfc_symbol *sym)
10378 gfc_expr *init = NULL;
10380 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10381 return;
10383 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
10384 init = gfc_default_initializer (&sym->ts);
10386 if (init == NULL && sym->ts.type != BT_CLASS)
10387 return;
10389 build_init_assign (sym, init);
10390 sym->attr.referenced = 1;
10393 /* Build an initializer for a local integer, real, complex, logical, or
10394 character variable, based on the command line flags finit-local-zero,
10395 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
10396 null if the symbol should not have a default initialization. */
10397 static gfc_expr *
10398 build_default_init_expr (gfc_symbol *sym)
10400 int char_len;
10401 gfc_expr *init_expr;
10402 int i;
10404 /* These symbols should never have a default initialization. */
10405 if (sym->attr.allocatable
10406 || sym->attr.external
10407 || sym->attr.dummy
10408 || sym->attr.pointer
10409 || sym->attr.in_equivalence
10410 || sym->attr.in_common
10411 || sym->attr.data
10412 || sym->module
10413 || sym->attr.cray_pointee
10414 || sym->attr.cray_pointer
10415 || sym->assoc)
10416 return NULL;
10418 /* Now we'll try to build an initializer expression. */
10419 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
10420 &sym->declared_at);
10422 /* We will only initialize integers, reals, complex, logicals, and
10423 characters, and only if the corresponding command-line flags
10424 were set. Otherwise, we free init_expr and return null. */
10425 switch (sym->ts.type)
10427 case BT_INTEGER:
10428 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
10429 mpz_set_si (init_expr->value.integer,
10430 gfc_option.flag_init_integer_value);
10431 else
10433 gfc_free_expr (init_expr);
10434 init_expr = NULL;
10436 break;
10438 case BT_REAL:
10439 switch (gfc_option.flag_init_real)
10441 case GFC_INIT_REAL_SNAN:
10442 init_expr->is_snan = 1;
10443 /* Fall through. */
10444 case GFC_INIT_REAL_NAN:
10445 mpfr_set_nan (init_expr->value.real);
10446 break;
10448 case GFC_INIT_REAL_INF:
10449 mpfr_set_inf (init_expr->value.real, 1);
10450 break;
10452 case GFC_INIT_REAL_NEG_INF:
10453 mpfr_set_inf (init_expr->value.real, -1);
10454 break;
10456 case GFC_INIT_REAL_ZERO:
10457 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
10458 break;
10460 default:
10461 gfc_free_expr (init_expr);
10462 init_expr = NULL;
10463 break;
10465 break;
10467 case BT_COMPLEX:
10468 switch (gfc_option.flag_init_real)
10470 case GFC_INIT_REAL_SNAN:
10471 init_expr->is_snan = 1;
10472 /* Fall through. */
10473 case GFC_INIT_REAL_NAN:
10474 mpfr_set_nan (mpc_realref (init_expr->value.complex));
10475 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
10476 break;
10478 case GFC_INIT_REAL_INF:
10479 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
10480 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
10481 break;
10483 case GFC_INIT_REAL_NEG_INF:
10484 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
10485 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
10486 break;
10488 case GFC_INIT_REAL_ZERO:
10489 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
10490 break;
10492 default:
10493 gfc_free_expr (init_expr);
10494 init_expr = NULL;
10495 break;
10497 break;
10499 case BT_LOGICAL:
10500 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
10501 init_expr->value.logical = 0;
10502 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
10503 init_expr->value.logical = 1;
10504 else
10506 gfc_free_expr (init_expr);
10507 init_expr = NULL;
10509 break;
10511 case BT_CHARACTER:
10512 /* For characters, the length must be constant in order to
10513 create a default initializer. */
10514 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10515 && sym->ts.u.cl->length
10516 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10518 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
10519 init_expr->value.character.length = char_len;
10520 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
10521 for (i = 0; i < char_len; i++)
10522 init_expr->value.character.string[i]
10523 = (unsigned char) gfc_option.flag_init_character_value;
10525 else
10527 gfc_free_expr (init_expr);
10528 init_expr = NULL;
10530 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10531 && sym->ts.u.cl->length)
10533 gfc_actual_arglist *arg;
10534 init_expr = gfc_get_expr ();
10535 init_expr->where = sym->declared_at;
10536 init_expr->ts = sym->ts;
10537 init_expr->expr_type = EXPR_FUNCTION;
10538 init_expr->value.function.isym =
10539 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
10540 init_expr->value.function.name = "repeat";
10541 arg = gfc_get_actual_arglist ();
10542 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
10543 NULL, 1);
10544 arg->expr->value.character.string[0]
10545 = gfc_option.flag_init_character_value;
10546 arg->next = gfc_get_actual_arglist ();
10547 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
10548 init_expr->value.function.actual = arg;
10550 break;
10552 default:
10553 gfc_free_expr (init_expr);
10554 init_expr = NULL;
10556 return init_expr;
10559 /* Add an initialization expression to a local variable. */
10560 static void
10561 apply_default_init_local (gfc_symbol *sym)
10563 gfc_expr *init = NULL;
10565 /* The symbol should be a variable or a function return value. */
10566 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10567 || (sym->attr.function && sym->result != sym))
10568 return;
10570 /* Try to build the initializer expression. If we can't initialize
10571 this symbol, then init will be NULL. */
10572 init = build_default_init_expr (sym);
10573 if (init == NULL)
10574 return;
10576 /* For saved variables, we don't want to add an initializer at function
10577 entry, so we just add a static initializer. Note that automatic variables
10578 are stack allocated even with -fno-automatic; we have also to exclude
10579 result variable, which are also nonstatic. */
10580 if (sym->attr.save || sym->ns->save_all
10581 || (gfc_option.flag_max_stack_var_size == 0 && !sym->attr.result
10582 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
10584 /* Don't clobber an existing initializer! */
10585 gcc_assert (sym->value == NULL);
10586 sym->value = init;
10587 return;
10590 build_init_assign (sym, init);
10594 /* Resolution of common features of flavors variable and procedure. */
10596 static bool
10597 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
10599 gfc_array_spec *as;
10601 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10602 as = CLASS_DATA (sym)->as;
10603 else
10604 as = sym->as;
10606 /* Constraints on deferred shape variable. */
10607 if (as == NULL || as->type != AS_DEFERRED)
10609 bool pointer, allocatable, dimension;
10611 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10613 pointer = CLASS_DATA (sym)->attr.class_pointer;
10614 allocatable = CLASS_DATA (sym)->attr.allocatable;
10615 dimension = CLASS_DATA (sym)->attr.dimension;
10617 else
10619 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
10620 allocatable = sym->attr.allocatable;
10621 dimension = sym->attr.dimension;
10624 if (allocatable)
10626 if (dimension && as->type != AS_ASSUMED_RANK)
10628 gfc_error ("Allocatable array '%s' at %L must have a deferred "
10629 "shape or assumed rank", sym->name, &sym->declared_at);
10630 return false;
10632 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
10633 "'%s' at %L may not be ALLOCATABLE",
10634 sym->name, &sym->declared_at))
10635 return false;
10638 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
10640 gfc_error ("Array pointer '%s' at %L must have a deferred shape or "
10641 "assumed rank", sym->name, &sym->declared_at);
10642 return false;
10645 else
10647 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
10648 && sym->ts.type != BT_CLASS && !sym->assoc)
10650 gfc_error ("Array '%s' at %L cannot have a deferred shape",
10651 sym->name, &sym->declared_at);
10652 return false;
10656 /* Constraints on polymorphic variables. */
10657 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
10659 /* F03:C502. */
10660 if (sym->attr.class_ok
10661 && !sym->attr.select_type_temporary
10662 && !UNLIMITED_POLY (sym)
10663 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
10665 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
10666 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
10667 &sym->declared_at);
10668 return false;
10671 /* F03:C509. */
10672 /* Assume that use associated symbols were checked in the module ns.
10673 Class-variables that are associate-names are also something special
10674 and excepted from the test. */
10675 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
10677 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
10678 "or pointer", sym->name, &sym->declared_at);
10679 return false;
10683 return true;
10687 /* Additional checks for symbols with flavor variable and derived
10688 type. To be called from resolve_fl_variable. */
10690 static bool
10691 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
10693 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
10695 /* Check to see if a derived type is blocked from being host
10696 associated by the presence of another class I symbol in the same
10697 namespace. 14.6.1.3 of the standard and the discussion on
10698 comp.lang.fortran. */
10699 if (sym->ns != sym->ts.u.derived->ns
10700 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
10702 gfc_symbol *s;
10703 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
10704 if (s && s->attr.generic)
10705 s = gfc_find_dt_in_generic (s);
10706 if (s && s->attr.flavor != FL_DERIVED)
10708 gfc_error ("The type '%s' cannot be host associated at %L "
10709 "because it is blocked by an incompatible object "
10710 "of the same name declared at %L",
10711 sym->ts.u.derived->name, &sym->declared_at,
10712 &s->declared_at);
10713 return false;
10717 /* 4th constraint in section 11.3: "If an object of a type for which
10718 component-initialization is specified (R429) appears in the
10719 specification-part of a module and does not have the ALLOCATABLE
10720 or POINTER attribute, the object shall have the SAVE attribute."
10722 The check for initializers is performed with
10723 gfc_has_default_initializer because gfc_default_initializer generates
10724 a hidden default for allocatable components. */
10725 if (!(sym->value || no_init_flag) && sym->ns->proc_name
10726 && sym->ns->proc_name->attr.flavor == FL_MODULE
10727 && !sym->ns->save_all && !sym->attr.save
10728 && !sym->attr.pointer && !sym->attr.allocatable
10729 && gfc_has_default_initializer (sym->ts.u.derived)
10730 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
10731 "'%s' at %L, needed due to the default "
10732 "initialization", sym->name, &sym->declared_at))
10733 return false;
10735 /* Assign default initializer. */
10736 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
10737 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
10739 sym->value = gfc_default_initializer (&sym->ts);
10742 return true;
10746 /* Resolve symbols with flavor variable. */
10748 static bool
10749 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
10751 int no_init_flag, automatic_flag;
10752 gfc_expr *e;
10753 const char *auto_save_msg;
10754 bool saved_specification_expr;
10756 auto_save_msg = "Automatic object '%s' at %L cannot have the "
10757 "SAVE attribute";
10759 if (!resolve_fl_var_and_proc (sym, mp_flag))
10760 return false;
10762 /* Set this flag to check that variables are parameters of all entries.
10763 This check is effected by the call to gfc_resolve_expr through
10764 is_non_constant_shape_array. */
10765 saved_specification_expr = specification_expr;
10766 specification_expr = true;
10768 if (sym->ns->proc_name
10769 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10770 || sym->ns->proc_name->attr.is_main_program)
10771 && !sym->attr.use_assoc
10772 && !sym->attr.allocatable
10773 && !sym->attr.pointer
10774 && is_non_constant_shape_array (sym))
10776 /* The shape of a main program or module array needs to be
10777 constant. */
10778 gfc_error ("The module or main program array '%s' at %L must "
10779 "have constant shape", sym->name, &sym->declared_at);
10780 specification_expr = saved_specification_expr;
10781 return false;
10784 /* Constraints on deferred type parameter. */
10785 if (sym->ts.deferred && !(sym->attr.pointer || sym->attr.allocatable))
10787 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10788 "requires either the pointer or allocatable attribute",
10789 sym->name, &sym->declared_at);
10790 specification_expr = saved_specification_expr;
10791 return false;
10794 if (sym->ts.type == BT_CHARACTER)
10796 /* Make sure that character string variables with assumed length are
10797 dummy arguments. */
10798 e = sym->ts.u.cl->length;
10799 if (e == NULL && !sym->attr.dummy && !sym->attr.result
10800 && !sym->ts.deferred && !sym->attr.select_type_temporary)
10802 gfc_error ("Entity with assumed character length at %L must be a "
10803 "dummy argument or a PARAMETER", &sym->declared_at);
10804 specification_expr = saved_specification_expr;
10805 return false;
10808 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
10810 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10811 specification_expr = saved_specification_expr;
10812 return false;
10815 if (!gfc_is_constant_expr (e)
10816 && !(e->expr_type == EXPR_VARIABLE
10817 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
10819 if (!sym->attr.use_assoc && sym->ns->proc_name
10820 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10821 || sym->ns->proc_name->attr.is_main_program))
10823 gfc_error ("'%s' at %L must have constant character length "
10824 "in this context", sym->name, &sym->declared_at);
10825 specification_expr = saved_specification_expr;
10826 return false;
10828 if (sym->attr.in_common)
10830 gfc_error ("COMMON variable '%s' at %L must have constant "
10831 "character length", sym->name, &sym->declared_at);
10832 specification_expr = saved_specification_expr;
10833 return false;
10838 if (sym->value == NULL && sym->attr.referenced)
10839 apply_default_init_local (sym); /* Try to apply a default initialization. */
10841 /* Determine if the symbol may not have an initializer. */
10842 no_init_flag = automatic_flag = 0;
10843 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
10844 || sym->attr.intrinsic || sym->attr.result)
10845 no_init_flag = 1;
10846 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
10847 && is_non_constant_shape_array (sym))
10849 no_init_flag = automatic_flag = 1;
10851 /* Also, they must not have the SAVE attribute.
10852 SAVE_IMPLICIT is checked below. */
10853 if (sym->as && sym->attr.codimension)
10855 int corank = sym->as->corank;
10856 sym->as->corank = 0;
10857 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
10858 sym->as->corank = corank;
10860 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
10862 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10863 specification_expr = saved_specification_expr;
10864 return false;
10868 /* Ensure that any initializer is simplified. */
10869 if (sym->value)
10870 gfc_simplify_expr (sym->value, 1);
10872 /* Reject illegal initializers. */
10873 if (!sym->mark && sym->value)
10875 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
10876 && CLASS_DATA (sym)->attr.allocatable))
10877 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10878 sym->name, &sym->declared_at);
10879 else if (sym->attr.external)
10880 gfc_error ("External '%s' at %L cannot have an initializer",
10881 sym->name, &sym->declared_at);
10882 else if (sym->attr.dummy
10883 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
10884 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10885 sym->name, &sym->declared_at);
10886 else if (sym->attr.intrinsic)
10887 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10888 sym->name, &sym->declared_at);
10889 else if (sym->attr.result)
10890 gfc_error ("Function result '%s' at %L cannot have an initializer",
10891 sym->name, &sym->declared_at);
10892 else if (automatic_flag)
10893 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10894 sym->name, &sym->declared_at);
10895 else
10896 goto no_init_error;
10897 specification_expr = saved_specification_expr;
10898 return false;
10901 no_init_error:
10902 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
10904 bool res = resolve_fl_variable_derived (sym, no_init_flag);
10905 specification_expr = saved_specification_expr;
10906 return res;
10909 specification_expr = saved_specification_expr;
10910 return true;
10914 /* Resolve a procedure. */
10916 static bool
10917 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
10919 gfc_formal_arglist *arg;
10921 if (sym->attr.function
10922 && !resolve_fl_var_and_proc (sym, mp_flag))
10923 return false;
10925 if (sym->ts.type == BT_CHARACTER)
10927 gfc_charlen *cl = sym->ts.u.cl;
10929 if (cl && cl->length && gfc_is_constant_expr (cl->length)
10930 && !resolve_charlen (cl))
10931 return false;
10933 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10934 && sym->attr.proc == PROC_ST_FUNCTION)
10936 gfc_error ("Character-valued statement function '%s' at %L must "
10937 "have constant length", sym->name, &sym->declared_at);
10938 return false;
10942 /* Ensure that derived type for are not of a private type. Internal
10943 module procedures are excluded by 2.2.3.3 - i.e., they are not
10944 externally accessible and can access all the objects accessible in
10945 the host. */
10946 if (!(sym->ns->parent
10947 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10948 && gfc_check_symbol_access (sym))
10950 gfc_interface *iface;
10952 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
10954 if (arg->sym
10955 && arg->sym->ts.type == BT_DERIVED
10956 && !arg->sym->ts.u.derived->attr.use_assoc
10957 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10958 && !gfc_notify_std (GFC_STD_F2003, "'%s' is of a PRIVATE type "
10959 "and cannot be a dummy argument"
10960 " of '%s', which is PUBLIC at %L",
10961 arg->sym->name, sym->name,
10962 &sym->declared_at))
10964 /* Stop this message from recurring. */
10965 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10966 return false;
10970 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10971 PRIVATE to the containing module. */
10972 for (iface = sym->generic; iface; iface = iface->next)
10974 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
10976 if (arg->sym
10977 && arg->sym->ts.type == BT_DERIVED
10978 && !arg->sym->ts.u.derived->attr.use_assoc
10979 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10980 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
10981 "PUBLIC interface '%s' at %L "
10982 "takes dummy arguments of '%s' which "
10983 "is PRIVATE", iface->sym->name,
10984 sym->name, &iface->sym->declared_at,
10985 gfc_typename(&arg->sym->ts)))
10987 /* Stop this message from recurring. */
10988 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10989 return false;
10994 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10995 PRIVATE to the containing module. */
10996 for (iface = sym->generic; iface; iface = iface->next)
10998 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
11000 if (arg->sym
11001 && arg->sym->ts.type == BT_DERIVED
11002 && !arg->sym->ts.u.derived->attr.use_assoc
11003 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
11004 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
11005 "PUBLIC interface '%s' at %L takes "
11006 "dummy arguments of '%s' which is "
11007 "PRIVATE", iface->sym->name,
11008 sym->name, &iface->sym->declared_at,
11009 gfc_typename(&arg->sym->ts)))
11011 /* Stop this message from recurring. */
11012 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
11013 return false;
11019 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
11020 && !sym->attr.proc_pointer)
11022 gfc_error ("Function '%s' at %L cannot have an initializer",
11023 sym->name, &sym->declared_at);
11024 return false;
11027 /* An external symbol may not have an initializer because it is taken to be
11028 a procedure. Exception: Procedure Pointers. */
11029 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
11031 gfc_error ("External object '%s' at %L may not have an initializer",
11032 sym->name, &sym->declared_at);
11033 return false;
11036 /* An elemental function is required to return a scalar 12.7.1 */
11037 if (sym->attr.elemental && sym->attr.function && sym->as)
11039 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
11040 "result", sym->name, &sym->declared_at);
11041 /* Reset so that the error only occurs once. */
11042 sym->attr.elemental = 0;
11043 return false;
11046 if (sym->attr.proc == PROC_ST_FUNCTION
11047 && (sym->attr.allocatable || sym->attr.pointer))
11049 gfc_error ("Statement function '%s' at %L may not have pointer or "
11050 "allocatable attribute", sym->name, &sym->declared_at);
11051 return false;
11054 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
11055 char-len-param shall not be array-valued, pointer-valued, recursive
11056 or pure. ....snip... A character value of * may only be used in the
11057 following ways: (i) Dummy arg of procedure - dummy associates with
11058 actual length; (ii) To declare a named constant; or (iii) External
11059 function - but length must be declared in calling scoping unit. */
11060 if (sym->attr.function
11061 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
11062 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
11064 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
11065 || (sym->attr.recursive) || (sym->attr.pure))
11067 if (sym->as && sym->as->rank)
11068 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11069 "array-valued", sym->name, &sym->declared_at);
11071 if (sym->attr.pointer)
11072 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11073 "pointer-valued", sym->name, &sym->declared_at);
11075 if (sym->attr.pure)
11076 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11077 "pure", sym->name, &sym->declared_at);
11079 if (sym->attr.recursive)
11080 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11081 "recursive", sym->name, &sym->declared_at);
11083 return false;
11086 /* Appendix B.2 of the standard. Contained functions give an
11087 error anyway. Fixed-form is likely to be F77/legacy. Deferred
11088 character length is an F2003 feature. */
11089 if (!sym->attr.contained
11090 && gfc_current_form != FORM_FIXED
11091 && !sym->ts.deferred)
11092 gfc_notify_std (GFC_STD_F95_OBS,
11093 "CHARACTER(*) function '%s' at %L",
11094 sym->name, &sym->declared_at);
11097 /* F2008, C1218. */
11098 if (sym->attr.elemental)
11100 if (sym->attr.proc_pointer)
11102 gfc_error ("Procedure pointer '%s' at %L shall not be elemental",
11103 sym->name, &sym->declared_at);
11104 return false;
11106 if (sym->attr.dummy)
11108 gfc_error ("Dummy procedure '%s' at %L shall not be elemental",
11109 sym->name, &sym->declared_at);
11110 return false;
11114 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11116 gfc_formal_arglist *curr_arg;
11117 int has_non_interop_arg = 0;
11119 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11120 sym->common_block))
11122 /* Clear these to prevent looking at them again if there was an
11123 error. */
11124 sym->attr.is_bind_c = 0;
11125 sym->attr.is_c_interop = 0;
11126 sym->ts.is_c_interop = 0;
11128 else
11130 /* So far, no errors have been found. */
11131 sym->attr.is_c_interop = 1;
11132 sym->ts.is_c_interop = 1;
11135 curr_arg = gfc_sym_get_dummy_args (sym);
11136 while (curr_arg != NULL)
11138 /* Skip implicitly typed dummy args here. */
11139 if (curr_arg->sym->attr.implicit_type == 0)
11140 if (!gfc_verify_c_interop_param (curr_arg->sym))
11141 /* If something is found to fail, record the fact so we
11142 can mark the symbol for the procedure as not being
11143 BIND(C) to try and prevent multiple errors being
11144 reported. */
11145 has_non_interop_arg = 1;
11147 curr_arg = curr_arg->next;
11150 /* See if any of the arguments were not interoperable and if so, clear
11151 the procedure symbol to prevent duplicate error messages. */
11152 if (has_non_interop_arg != 0)
11154 sym->attr.is_c_interop = 0;
11155 sym->ts.is_c_interop = 0;
11156 sym->attr.is_bind_c = 0;
11160 if (!sym->attr.proc_pointer)
11162 if (sym->attr.save == SAVE_EXPLICIT)
11164 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11165 "in '%s' at %L", sym->name, &sym->declared_at);
11166 return false;
11168 if (sym->attr.intent)
11170 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11171 "in '%s' at %L", sym->name, &sym->declared_at);
11172 return false;
11174 if (sym->attr.subroutine && sym->attr.result)
11176 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11177 "in '%s' at %L", sym->name, &sym->declared_at);
11178 return false;
11180 if (sym->attr.external && sym->attr.function
11181 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11182 || sym->attr.contained))
11184 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11185 "in '%s' at %L", sym->name, &sym->declared_at);
11186 return false;
11188 if (strcmp ("ppr@", sym->name) == 0)
11190 gfc_error ("Procedure pointer result '%s' at %L "
11191 "is missing the pointer attribute",
11192 sym->ns->proc_name->name, &sym->declared_at);
11193 return false;
11197 return true;
11201 /* Resolve a list of finalizer procedures. That is, after they have hopefully
11202 been defined and we now know their defined arguments, check that they fulfill
11203 the requirements of the standard for procedures used as finalizers. */
11205 static bool
11206 gfc_resolve_finalizers (gfc_symbol* derived)
11208 gfc_finalizer* list;
11209 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
11210 bool result = true;
11211 bool seen_scalar = false;
11213 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
11214 return true;
11216 /* Walk over the list of finalizer-procedures, check them, and if any one
11217 does not fit in with the standard's definition, print an error and remove
11218 it from the list. */
11219 prev_link = &derived->f2k_derived->finalizers;
11220 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
11222 gfc_formal_arglist *dummy_args;
11223 gfc_symbol* arg;
11224 gfc_finalizer* i;
11225 int my_rank;
11227 /* Skip this finalizer if we already resolved it. */
11228 if (list->proc_tree)
11230 prev_link = &(list->next);
11231 continue;
11234 /* Check this exists and is a SUBROUTINE. */
11235 if (!list->proc_sym->attr.subroutine)
11237 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
11238 list->proc_sym->name, &list->where);
11239 goto error;
11242 /* We should have exactly one argument. */
11243 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
11244 if (!dummy_args || dummy_args->next)
11246 gfc_error ("FINAL procedure at %L must have exactly one argument",
11247 &list->where);
11248 goto error;
11250 arg = dummy_args->sym;
11252 /* This argument must be of our type. */
11253 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
11255 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
11256 &arg->declared_at, derived->name);
11257 goto error;
11260 /* It must neither be a pointer nor allocatable nor optional. */
11261 if (arg->attr.pointer)
11263 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
11264 &arg->declared_at);
11265 goto error;
11267 if (arg->attr.allocatable)
11269 gfc_error ("Argument of FINAL procedure at %L must not be"
11270 " ALLOCATABLE", &arg->declared_at);
11271 goto error;
11273 if (arg->attr.optional)
11275 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
11276 &arg->declared_at);
11277 goto error;
11280 /* It must not be INTENT(OUT). */
11281 if (arg->attr.intent == INTENT_OUT)
11283 gfc_error ("Argument of FINAL procedure at %L must not be"
11284 " INTENT(OUT)", &arg->declared_at);
11285 goto error;
11288 /* Warn if the procedure is non-scalar and not assumed shape. */
11289 if (gfc_option.warn_surprising && arg->as && arg->as->rank != 0
11290 && arg->as->type != AS_ASSUMED_SHAPE)
11291 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
11292 " shape argument", &arg->declared_at);
11294 /* Check that it does not match in kind and rank with a FINAL procedure
11295 defined earlier. To really loop over the *earlier* declarations,
11296 we need to walk the tail of the list as new ones were pushed at the
11297 front. */
11298 /* TODO: Handle kind parameters once they are implemented. */
11299 my_rank = (arg->as ? arg->as->rank : 0);
11300 for (i = list->next; i; i = i->next)
11302 gfc_formal_arglist *dummy_args;
11304 /* Argument list might be empty; that is an error signalled earlier,
11305 but we nevertheless continued resolving. */
11306 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
11307 if (dummy_args)
11309 gfc_symbol* i_arg = dummy_args->sym;
11310 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
11311 if (i_rank == my_rank)
11313 gfc_error ("FINAL procedure '%s' declared at %L has the same"
11314 " rank (%d) as '%s'",
11315 list->proc_sym->name, &list->where, my_rank,
11316 i->proc_sym->name);
11317 goto error;
11322 /* Is this the/a scalar finalizer procedure? */
11323 if (!arg->as || arg->as->rank == 0)
11324 seen_scalar = true;
11326 /* Find the symtree for this procedure. */
11327 gcc_assert (!list->proc_tree);
11328 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
11330 prev_link = &list->next;
11331 continue;
11333 /* Remove wrong nodes immediately from the list so we don't risk any
11334 troubles in the future when they might fail later expectations. */
11335 error:
11336 result = false;
11337 i = list;
11338 *prev_link = list->next;
11339 gfc_free_finalizer (i);
11342 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
11343 were nodes in the list, must have been for arrays. It is surely a good
11344 idea to have a scalar version there if there's something to finalize. */
11345 if (gfc_option.warn_surprising && result && !seen_scalar)
11346 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
11347 " defined at %L, suggest also scalar one",
11348 derived->name, &derived->declared_at);
11350 gfc_find_derived_vtab (derived);
11351 return result;
11355 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
11357 static bool
11358 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
11359 const char* generic_name, locus where)
11361 gfc_symbol *sym1, *sym2;
11362 const char *pass1, *pass2;
11364 gcc_assert (t1->specific && t2->specific);
11365 gcc_assert (!t1->specific->is_generic);
11366 gcc_assert (!t2->specific->is_generic);
11367 gcc_assert (t1->is_operator == t2->is_operator);
11369 sym1 = t1->specific->u.specific->n.sym;
11370 sym2 = t2->specific->u.specific->n.sym;
11372 if (sym1 == sym2)
11373 return true;
11375 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
11376 if (sym1->attr.subroutine != sym2->attr.subroutine
11377 || sym1->attr.function != sym2->attr.function)
11379 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
11380 " GENERIC '%s' at %L",
11381 sym1->name, sym2->name, generic_name, &where);
11382 return false;
11385 /* Compare the interfaces. */
11386 if (t1->specific->nopass)
11387 pass1 = NULL;
11388 else if (t1->specific->pass_arg)
11389 pass1 = t1->specific->pass_arg;
11390 else
11391 pass1 = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym)->sym->name;
11392 if (t2->specific->nopass)
11393 pass2 = NULL;
11394 else if (t2->specific->pass_arg)
11395 pass2 = t2->specific->pass_arg;
11396 else
11397 pass2 = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym)->sym->name;
11398 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
11399 NULL, 0, pass1, pass2))
11401 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
11402 sym1->name, sym2->name, generic_name, &where);
11403 return false;
11406 return true;
11410 /* Worker function for resolving a generic procedure binding; this is used to
11411 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
11413 The difference between those cases is finding possible inherited bindings
11414 that are overridden, as one has to look for them in tb_sym_root,
11415 tb_uop_root or tb_op, respectively. Thus the caller must already find
11416 the super-type and set p->overridden correctly. */
11418 static bool
11419 resolve_tb_generic_targets (gfc_symbol* super_type,
11420 gfc_typebound_proc* p, const char* name)
11422 gfc_tbp_generic* target;
11423 gfc_symtree* first_target;
11424 gfc_symtree* inherited;
11426 gcc_assert (p && p->is_generic);
11428 /* Try to find the specific bindings for the symtrees in our target-list. */
11429 gcc_assert (p->u.generic);
11430 for (target = p->u.generic; target; target = target->next)
11431 if (!target->specific)
11433 gfc_typebound_proc* overridden_tbp;
11434 gfc_tbp_generic* g;
11435 const char* target_name;
11437 target_name = target->specific_st->name;
11439 /* Defined for this type directly. */
11440 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
11442 target->specific = target->specific_st->n.tb;
11443 goto specific_found;
11446 /* Look for an inherited specific binding. */
11447 if (super_type)
11449 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
11450 true, NULL);
11452 if (inherited)
11454 gcc_assert (inherited->n.tb);
11455 target->specific = inherited->n.tb;
11456 goto specific_found;
11460 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
11461 " at %L", target_name, name, &p->where);
11462 return false;
11464 /* Once we've found the specific binding, check it is not ambiguous with
11465 other specifics already found or inherited for the same GENERIC. */
11466 specific_found:
11467 gcc_assert (target->specific);
11469 /* This must really be a specific binding! */
11470 if (target->specific->is_generic)
11472 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
11473 " '%s' is GENERIC, too", name, &p->where, target_name);
11474 return false;
11477 /* Check those already resolved on this type directly. */
11478 for (g = p->u.generic; g; g = g->next)
11479 if (g != target && g->specific
11480 && !check_generic_tbp_ambiguity (target, g, name, p->where))
11481 return false;
11483 /* Check for ambiguity with inherited specific targets. */
11484 for (overridden_tbp = p->overridden; overridden_tbp;
11485 overridden_tbp = overridden_tbp->overridden)
11486 if (overridden_tbp->is_generic)
11488 for (g = overridden_tbp->u.generic; g; g = g->next)
11490 gcc_assert (g->specific);
11491 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
11492 return false;
11497 /* If we attempt to "overwrite" a specific binding, this is an error. */
11498 if (p->overridden && !p->overridden->is_generic)
11500 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
11501 " the same name", name, &p->where);
11502 return false;
11505 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
11506 all must have the same attributes here. */
11507 first_target = p->u.generic->specific->u.specific;
11508 gcc_assert (first_target);
11509 p->subroutine = first_target->n.sym->attr.subroutine;
11510 p->function = first_target->n.sym->attr.function;
11512 return true;
11516 /* Resolve a GENERIC procedure binding for a derived type. */
11518 static bool
11519 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
11521 gfc_symbol* super_type;
11523 /* Find the overridden binding if any. */
11524 st->n.tb->overridden = NULL;
11525 super_type = gfc_get_derived_super_type (derived);
11526 if (super_type)
11528 gfc_symtree* overridden;
11529 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
11530 true, NULL);
11532 if (overridden && overridden->n.tb)
11533 st->n.tb->overridden = overridden->n.tb;
11536 /* Resolve using worker function. */
11537 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
11541 /* Retrieve the target-procedure of an operator binding and do some checks in
11542 common for intrinsic and user-defined type-bound operators. */
11544 static gfc_symbol*
11545 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
11547 gfc_symbol* target_proc;
11549 gcc_assert (target->specific && !target->specific->is_generic);
11550 target_proc = target->specific->u.specific->n.sym;
11551 gcc_assert (target_proc);
11553 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
11554 if (target->specific->nopass)
11556 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
11557 return NULL;
11560 return target_proc;
11564 /* Resolve a type-bound intrinsic operator. */
11566 static bool
11567 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
11568 gfc_typebound_proc* p)
11570 gfc_symbol* super_type;
11571 gfc_tbp_generic* target;
11573 /* If there's already an error here, do nothing (but don't fail again). */
11574 if (p->error)
11575 return true;
11577 /* Operators should always be GENERIC bindings. */
11578 gcc_assert (p->is_generic);
11580 /* Look for an overridden binding. */
11581 super_type = gfc_get_derived_super_type (derived);
11582 if (super_type && super_type->f2k_derived)
11583 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
11584 op, true, NULL);
11585 else
11586 p->overridden = NULL;
11588 /* Resolve general GENERIC properties using worker function. */
11589 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
11590 goto error;
11592 /* Check the targets to be procedures of correct interface. */
11593 for (target = p->u.generic; target; target = target->next)
11595 gfc_symbol* target_proc;
11597 target_proc = get_checked_tb_operator_target (target, p->where);
11598 if (!target_proc)
11599 goto error;
11601 if (!gfc_check_operator_interface (target_proc, op, p->where))
11602 goto error;
11604 /* Add target to non-typebound operator list. */
11605 if (!target->specific->deferred && !derived->attr.use_assoc
11606 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
11608 gfc_interface *head, *intr;
11609 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
11610 return false;
11611 head = derived->ns->op[op];
11612 intr = gfc_get_interface ();
11613 intr->sym = target_proc;
11614 intr->where = p->where;
11615 intr->next = head;
11616 derived->ns->op[op] = intr;
11620 return true;
11622 error:
11623 p->error = 1;
11624 return false;
11628 /* Resolve a type-bound user operator (tree-walker callback). */
11630 static gfc_symbol* resolve_bindings_derived;
11631 static bool resolve_bindings_result;
11633 static bool check_uop_procedure (gfc_symbol* sym, locus where);
11635 static void
11636 resolve_typebound_user_op (gfc_symtree* stree)
11638 gfc_symbol* super_type;
11639 gfc_tbp_generic* target;
11641 gcc_assert (stree && stree->n.tb);
11643 if (stree->n.tb->error)
11644 return;
11646 /* Operators should always be GENERIC bindings. */
11647 gcc_assert (stree->n.tb->is_generic);
11649 /* Find overridden procedure, if any. */
11650 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11651 if (super_type && super_type->f2k_derived)
11653 gfc_symtree* overridden;
11654 overridden = gfc_find_typebound_user_op (super_type, NULL,
11655 stree->name, true, NULL);
11657 if (overridden && overridden->n.tb)
11658 stree->n.tb->overridden = overridden->n.tb;
11660 else
11661 stree->n.tb->overridden = NULL;
11663 /* Resolve basically using worker function. */
11664 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
11665 goto error;
11667 /* Check the targets to be functions of correct interface. */
11668 for (target = stree->n.tb->u.generic; target; target = target->next)
11670 gfc_symbol* target_proc;
11672 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11673 if (!target_proc)
11674 goto error;
11676 if (!check_uop_procedure (target_proc, stree->n.tb->where))
11677 goto error;
11680 return;
11682 error:
11683 resolve_bindings_result = false;
11684 stree->n.tb->error = 1;
11688 /* Resolve the type-bound procedures for a derived type. */
11690 static void
11691 resolve_typebound_procedure (gfc_symtree* stree)
11693 gfc_symbol* proc;
11694 locus where;
11695 gfc_symbol* me_arg;
11696 gfc_symbol* super_type;
11697 gfc_component* comp;
11699 gcc_assert (stree);
11701 /* Undefined specific symbol from GENERIC target definition. */
11702 if (!stree->n.tb)
11703 return;
11705 if (stree->n.tb->error)
11706 return;
11708 /* If this is a GENERIC binding, use that routine. */
11709 if (stree->n.tb->is_generic)
11711 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
11712 goto error;
11713 return;
11716 /* Get the target-procedure to check it. */
11717 gcc_assert (!stree->n.tb->is_generic);
11718 gcc_assert (stree->n.tb->u.specific);
11719 proc = stree->n.tb->u.specific->n.sym;
11720 where = stree->n.tb->where;
11722 /* Default access should already be resolved from the parser. */
11723 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11725 if (stree->n.tb->deferred)
11727 if (!check_proc_interface (proc, &where))
11728 goto error;
11730 else
11732 /* Check for F08:C465. */
11733 if ((!proc->attr.subroutine && !proc->attr.function)
11734 || (proc->attr.proc != PROC_MODULE
11735 && proc->attr.if_source != IFSRC_IFBODY)
11736 || proc->attr.abstract)
11738 gfc_error ("'%s' must be a module procedure or an external procedure with"
11739 " an explicit interface at %L", proc->name, &where);
11740 goto error;
11744 stree->n.tb->subroutine = proc->attr.subroutine;
11745 stree->n.tb->function = proc->attr.function;
11747 /* Find the super-type of the current derived type. We could do this once and
11748 store in a global if speed is needed, but as long as not I believe this is
11749 more readable and clearer. */
11750 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11752 /* If PASS, resolve and check arguments if not already resolved / loaded
11753 from a .mod file. */
11754 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11756 gfc_formal_arglist *dummy_args;
11758 dummy_args = gfc_sym_get_dummy_args (proc);
11759 if (stree->n.tb->pass_arg)
11761 gfc_formal_arglist *i;
11763 /* If an explicit passing argument name is given, walk the arg-list
11764 and look for it. */
11766 me_arg = NULL;
11767 stree->n.tb->pass_arg_num = 1;
11768 for (i = dummy_args; i; i = i->next)
11770 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
11772 me_arg = i->sym;
11773 break;
11775 ++stree->n.tb->pass_arg_num;
11778 if (!me_arg)
11780 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11781 " argument '%s'",
11782 proc->name, stree->n.tb->pass_arg, &where,
11783 stree->n.tb->pass_arg);
11784 goto error;
11787 else
11789 /* Otherwise, take the first one; there should in fact be at least
11790 one. */
11791 stree->n.tb->pass_arg_num = 1;
11792 if (!dummy_args)
11794 gfc_error ("Procedure '%s' with PASS at %L must have at"
11795 " least one argument", proc->name, &where);
11796 goto error;
11798 me_arg = dummy_args->sym;
11801 /* Now check that the argument-type matches and the passed-object
11802 dummy argument is generally fine. */
11804 gcc_assert (me_arg);
11806 if (me_arg->ts.type != BT_CLASS)
11808 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11809 " at %L", proc->name, &where);
11810 goto error;
11813 if (CLASS_DATA (me_arg)->ts.u.derived
11814 != resolve_bindings_derived)
11816 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11817 " the derived-type '%s'", me_arg->name, proc->name,
11818 me_arg->name, &where, resolve_bindings_derived->name);
11819 goto error;
11822 gcc_assert (me_arg->ts.type == BT_CLASS);
11823 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
11825 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11826 " scalar", proc->name, &where);
11827 goto error;
11829 if (CLASS_DATA (me_arg)->attr.allocatable)
11831 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11832 " be ALLOCATABLE", proc->name, &where);
11833 goto error;
11835 if (CLASS_DATA (me_arg)->attr.class_pointer)
11837 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11838 " be POINTER", proc->name, &where);
11839 goto error;
11843 /* If we are extending some type, check that we don't override a procedure
11844 flagged NON_OVERRIDABLE. */
11845 stree->n.tb->overridden = NULL;
11846 if (super_type)
11848 gfc_symtree* overridden;
11849 overridden = gfc_find_typebound_proc (super_type, NULL,
11850 stree->name, true, NULL);
11852 if (overridden)
11854 if (overridden->n.tb)
11855 stree->n.tb->overridden = overridden->n.tb;
11857 if (!gfc_check_typebound_override (stree, overridden))
11858 goto error;
11862 /* See if there's a name collision with a component directly in this type. */
11863 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11864 if (!strcmp (comp->name, stree->name))
11866 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11867 " '%s'",
11868 stree->name, &where, resolve_bindings_derived->name);
11869 goto error;
11872 /* Try to find a name collision with an inherited component. */
11873 if (super_type && gfc_find_component (super_type, stree->name, true, true))
11875 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11876 " component of '%s'",
11877 stree->name, &where, resolve_bindings_derived->name);
11878 goto error;
11881 stree->n.tb->error = 0;
11882 return;
11884 error:
11885 resolve_bindings_result = false;
11886 stree->n.tb->error = 1;
11890 static bool
11891 resolve_typebound_procedures (gfc_symbol* derived)
11893 int op;
11894 gfc_symbol* super_type;
11896 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
11897 return true;
11899 super_type = gfc_get_derived_super_type (derived);
11900 if (super_type)
11901 resolve_symbol (super_type);
11903 resolve_bindings_derived = derived;
11904 resolve_bindings_result = true;
11906 /* Make sure the vtab has been generated. */
11907 gfc_find_derived_vtab (derived);
11909 if (derived->f2k_derived->tb_sym_root)
11910 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
11911 &resolve_typebound_procedure);
11913 if (derived->f2k_derived->tb_uop_root)
11914 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
11915 &resolve_typebound_user_op);
11917 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
11919 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
11920 if (p && !resolve_typebound_intrinsic_op (derived,
11921 (gfc_intrinsic_op)op, p))
11922 resolve_bindings_result = false;
11925 return resolve_bindings_result;
11929 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11930 to give all identical derived types the same backend_decl. */
11931 static void
11932 add_dt_to_dt_list (gfc_symbol *derived)
11934 gfc_dt_list *dt_list;
11936 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
11937 if (derived == dt_list->derived)
11938 return;
11940 dt_list = gfc_get_dt_list ();
11941 dt_list->next = gfc_derived_types;
11942 dt_list->derived = derived;
11943 gfc_derived_types = dt_list;
11947 /* Ensure that a derived-type is really not abstract, meaning that every
11948 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11950 static bool
11951 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
11953 if (!st)
11954 return true;
11956 if (!ensure_not_abstract_walker (sub, st->left))
11957 return false;
11958 if (!ensure_not_abstract_walker (sub, st->right))
11959 return false;
11961 if (st->n.tb && st->n.tb->deferred)
11963 gfc_symtree* overriding;
11964 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
11965 if (!overriding)
11966 return false;
11967 gcc_assert (overriding->n.tb);
11968 if (overriding->n.tb->deferred)
11970 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11971 " '%s' is DEFERRED and not overridden",
11972 sub->name, &sub->declared_at, st->name);
11973 return false;
11977 return true;
11980 static bool
11981 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
11983 /* The algorithm used here is to recursively travel up the ancestry of sub
11984 and for each ancestor-type, check all bindings. If any of them is
11985 DEFERRED, look it up starting from sub and see if the found (overriding)
11986 binding is not DEFERRED.
11987 This is not the most efficient way to do this, but it should be ok and is
11988 clearer than something sophisticated. */
11990 gcc_assert (ancestor && !sub->attr.abstract);
11992 if (!ancestor->attr.abstract)
11993 return true;
11995 /* Walk bindings of this ancestor. */
11996 if (ancestor->f2k_derived)
11998 bool t;
11999 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
12000 if (!t)
12001 return false;
12004 /* Find next ancestor type and recurse on it. */
12005 ancestor = gfc_get_derived_super_type (ancestor);
12006 if (ancestor)
12007 return ensure_not_abstract (sub, ancestor);
12009 return true;
12013 /* This check for typebound defined assignments is done recursively
12014 since the order in which derived types are resolved is not always in
12015 order of the declarations. */
12017 static void
12018 check_defined_assignments (gfc_symbol *derived)
12020 gfc_component *c;
12022 for (c = derived->components; c; c = c->next)
12024 if (c->ts.type != BT_DERIVED
12025 || c->attr.pointer
12026 || c->attr.allocatable
12027 || c->attr.proc_pointer_comp
12028 || c->attr.class_pointer
12029 || c->attr.proc_pointer)
12030 continue;
12032 if (c->ts.u.derived->attr.defined_assign_comp
12033 || (c->ts.u.derived->f2k_derived
12034 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
12036 derived->attr.defined_assign_comp = 1;
12037 return;
12040 check_defined_assignments (c->ts.u.derived);
12041 if (c->ts.u.derived->attr.defined_assign_comp)
12043 derived->attr.defined_assign_comp = 1;
12044 return;
12050 /* Resolve the components of a derived type. This does not have to wait until
12051 resolution stage, but can be done as soon as the dt declaration has been
12052 parsed. */
12054 static bool
12055 resolve_fl_derived0 (gfc_symbol *sym)
12057 gfc_symbol* super_type;
12058 gfc_component *c;
12060 if (sym->attr.unlimited_polymorphic)
12061 return true;
12063 super_type = gfc_get_derived_super_type (sym);
12065 /* F2008, C432. */
12066 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
12068 gfc_error ("As extending type '%s' at %L has a coarray component, "
12069 "parent type '%s' shall also have one", sym->name,
12070 &sym->declared_at, super_type->name);
12071 return false;
12074 /* Ensure the extended type gets resolved before we do. */
12075 if (super_type && !resolve_fl_derived0 (super_type))
12076 return false;
12078 /* An ABSTRACT type must be extensible. */
12079 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
12081 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
12082 sym->name, &sym->declared_at);
12083 return false;
12086 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
12087 : sym->components;
12089 for ( ; c != NULL; c = c->next)
12091 if (c->attr.artificial)
12092 continue;
12094 /* See PRs 51550, 47545, 48654, 49050, 51075 - and 45170. */
12095 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function)
12097 gfc_error ("Deferred-length character component '%s' at %L is not "
12098 "yet supported", c->name, &c->loc);
12099 return false;
12102 /* F2008, C442. */
12103 if ((!sym->attr.is_class || c != sym->components)
12104 && c->attr.codimension
12105 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
12107 gfc_error ("Coarray component '%s' at %L must be allocatable with "
12108 "deferred shape", c->name, &c->loc);
12109 return false;
12112 /* F2008, C443. */
12113 if (c->attr.codimension && c->ts.type == BT_DERIVED
12114 && c->ts.u.derived->ts.is_iso_c)
12116 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12117 "shall not be a coarray", c->name, &c->loc);
12118 return false;
12121 /* F2008, C444. */
12122 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
12123 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12124 || c->attr.allocatable))
12126 gfc_error ("Component '%s' at %L with coarray component "
12127 "shall be a nonpointer, nonallocatable scalar",
12128 c->name, &c->loc);
12129 return false;
12132 /* F2008, C448. */
12133 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12135 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
12136 "is not an array pointer", c->name, &c->loc);
12137 return false;
12140 if (c->attr.proc_pointer && c->ts.interface)
12142 gfc_symbol *ifc = c->ts.interface;
12144 if (!sym->attr.vtype
12145 && !check_proc_interface (ifc, &c->loc))
12146 return false;
12148 if (ifc->attr.if_source || ifc->attr.intrinsic)
12150 /* Resolve interface and copy attributes. */
12151 if (ifc->formal && !ifc->formal_ns)
12152 resolve_symbol (ifc);
12153 if (ifc->attr.intrinsic)
12154 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
12156 if (ifc->result)
12158 c->ts = ifc->result->ts;
12159 c->attr.allocatable = ifc->result->attr.allocatable;
12160 c->attr.pointer = ifc->result->attr.pointer;
12161 c->attr.dimension = ifc->result->attr.dimension;
12162 c->as = gfc_copy_array_spec (ifc->result->as);
12163 c->attr.class_ok = ifc->result->attr.class_ok;
12165 else
12167 c->ts = ifc->ts;
12168 c->attr.allocatable = ifc->attr.allocatable;
12169 c->attr.pointer = ifc->attr.pointer;
12170 c->attr.dimension = ifc->attr.dimension;
12171 c->as = gfc_copy_array_spec (ifc->as);
12172 c->attr.class_ok = ifc->attr.class_ok;
12174 c->ts.interface = ifc;
12175 c->attr.function = ifc->attr.function;
12176 c->attr.subroutine = ifc->attr.subroutine;
12178 c->attr.pure = ifc->attr.pure;
12179 c->attr.elemental = ifc->attr.elemental;
12180 c->attr.recursive = ifc->attr.recursive;
12181 c->attr.always_explicit = ifc->attr.always_explicit;
12182 c->attr.ext_attr |= ifc->attr.ext_attr;
12183 /* Copy char length. */
12184 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
12186 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
12187 if (cl->length && !cl->resolved
12188 && !gfc_resolve_expr (cl->length))
12189 return false;
12190 c->ts.u.cl = cl;
12194 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
12196 /* Since PPCs are not implicitly typed, a PPC without an explicit
12197 interface must be a subroutine. */
12198 gfc_add_subroutine (&c->attr, c->name, &c->loc);
12201 /* Procedure pointer components: Check PASS arg. */
12202 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
12203 && !sym->attr.vtype)
12205 gfc_symbol* me_arg;
12207 if (c->tb->pass_arg)
12209 gfc_formal_arglist* i;
12211 /* If an explicit passing argument name is given, walk the arg-list
12212 and look for it. */
12214 me_arg = NULL;
12215 c->tb->pass_arg_num = 1;
12216 for (i = c->ts.interface->formal; i; i = i->next)
12218 if (!strcmp (i->sym->name, c->tb->pass_arg))
12220 me_arg = i->sym;
12221 break;
12223 c->tb->pass_arg_num++;
12226 if (!me_arg)
12228 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
12229 "at %L has no argument '%s'", c->name,
12230 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
12231 c->tb->error = 1;
12232 return false;
12235 else
12237 /* Otherwise, take the first one; there should in fact be at least
12238 one. */
12239 c->tb->pass_arg_num = 1;
12240 if (!c->ts.interface->formal)
12242 gfc_error ("Procedure pointer component '%s' with PASS at %L "
12243 "must have at least one argument",
12244 c->name, &c->loc);
12245 c->tb->error = 1;
12246 return false;
12248 me_arg = c->ts.interface->formal->sym;
12251 /* Now check that the argument-type matches. */
12252 gcc_assert (me_arg);
12253 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
12254 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
12255 || (me_arg->ts.type == BT_CLASS
12256 && CLASS_DATA (me_arg)->ts.u.derived != sym))
12258 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
12259 " the derived type '%s'", me_arg->name, c->name,
12260 me_arg->name, &c->loc, sym->name);
12261 c->tb->error = 1;
12262 return false;
12265 /* Check for C453. */
12266 if (me_arg->attr.dimension)
12268 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12269 "must be scalar", me_arg->name, c->name, me_arg->name,
12270 &c->loc);
12271 c->tb->error = 1;
12272 return false;
12275 if (me_arg->attr.pointer)
12277 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12278 "may not have the POINTER attribute", me_arg->name,
12279 c->name, me_arg->name, &c->loc);
12280 c->tb->error = 1;
12281 return false;
12284 if (me_arg->attr.allocatable)
12286 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12287 "may not be ALLOCATABLE", me_arg->name, c->name,
12288 me_arg->name, &c->loc);
12289 c->tb->error = 1;
12290 return false;
12293 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
12294 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
12295 " at %L", c->name, &c->loc);
12299 /* Check type-spec if this is not the parent-type component. */
12300 if (((sym->attr.is_class
12301 && (!sym->components->ts.u.derived->attr.extension
12302 || c != sym->components->ts.u.derived->components))
12303 || (!sym->attr.is_class
12304 && (!sym->attr.extension || c != sym->components)))
12305 && !sym->attr.vtype
12306 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
12307 return false;
12309 /* If this type is an extension, set the accessibility of the parent
12310 component. */
12311 if (super_type
12312 && ((sym->attr.is_class
12313 && c == sym->components->ts.u.derived->components)
12314 || (!sym->attr.is_class && c == sym->components))
12315 && strcmp (super_type->name, c->name) == 0)
12316 c->attr.access = super_type->attr.access;
12318 /* If this type is an extension, see if this component has the same name
12319 as an inherited type-bound procedure. */
12320 if (super_type && !sym->attr.is_class
12321 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
12323 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
12324 " inherited type-bound procedure",
12325 c->name, sym->name, &c->loc);
12326 return false;
12329 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
12330 && !c->ts.deferred)
12332 if (c->ts.u.cl->length == NULL
12333 || (!resolve_charlen(c->ts.u.cl))
12334 || !gfc_is_constant_expr (c->ts.u.cl->length))
12336 gfc_error ("Character length of component '%s' needs to "
12337 "be a constant specification expression at %L",
12338 c->name,
12339 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
12340 return false;
12344 if (c->ts.type == BT_CHARACTER && c->ts.deferred
12345 && !c->attr.pointer && !c->attr.allocatable)
12347 gfc_error ("Character component '%s' of '%s' at %L with deferred "
12348 "length must be a POINTER or ALLOCATABLE",
12349 c->name, sym->name, &c->loc);
12350 return false;
12353 if (c->ts.type == BT_DERIVED
12354 && sym->component_access != ACCESS_PRIVATE
12355 && gfc_check_symbol_access (sym)
12356 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
12357 && !c->ts.u.derived->attr.use_assoc
12358 && !gfc_check_symbol_access (c->ts.u.derived)
12359 && !gfc_notify_std (GFC_STD_F2003, "the component '%s' is a "
12360 "PRIVATE type and cannot be a component of "
12361 "'%s', which is PUBLIC at %L", c->name,
12362 sym->name, &sym->declared_at))
12363 return false;
12365 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
12367 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
12368 "type %s", c->name, &c->loc, sym->name);
12369 return false;
12372 if (sym->attr.sequence)
12374 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
12376 gfc_error ("Component %s of SEQUENCE type declared at %L does "
12377 "not have the SEQUENCE attribute",
12378 c->ts.u.derived->name, &sym->declared_at);
12379 return false;
12383 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
12384 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
12385 else if (c->ts.type == BT_CLASS && c->attr.class_ok
12386 && CLASS_DATA (c)->ts.u.derived->attr.generic)
12387 CLASS_DATA (c)->ts.u.derived
12388 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
12390 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
12391 && c->attr.pointer && c->ts.u.derived->components == NULL
12392 && !c->ts.u.derived->attr.zero_comp)
12394 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12395 "that has not been declared", c->name, sym->name,
12396 &c->loc);
12397 return false;
12400 if (c->ts.type == BT_CLASS && c->attr.class_ok
12401 && CLASS_DATA (c)->attr.class_pointer
12402 && CLASS_DATA (c)->ts.u.derived->components == NULL
12403 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
12404 && !UNLIMITED_POLY (c))
12406 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12407 "that has not been declared", c->name, sym->name,
12408 &c->loc);
12409 return false;
12412 /* C437. */
12413 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
12414 && (!c->attr.class_ok
12415 || !(CLASS_DATA (c)->attr.class_pointer
12416 || CLASS_DATA (c)->attr.allocatable)))
12418 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
12419 "or pointer", c->name, &c->loc);
12420 /* Prevent a recurrence of the error. */
12421 c->ts.type = BT_UNKNOWN;
12422 return false;
12425 /* Ensure that all the derived type components are put on the
12426 derived type list; even in formal namespaces, where derived type
12427 pointer components might not have been declared. */
12428 if (c->ts.type == BT_DERIVED
12429 && c->ts.u.derived
12430 && c->ts.u.derived->components
12431 && c->attr.pointer
12432 && sym != c->ts.u.derived)
12433 add_dt_to_dt_list (c->ts.u.derived);
12435 if (!gfc_resolve_array_spec (c->as,
12436 !(c->attr.pointer || c->attr.proc_pointer
12437 || c->attr.allocatable)))
12438 return false;
12440 if (c->initializer && !sym->attr.vtype
12441 && !gfc_check_assign_symbol (sym, c, c->initializer))
12442 return false;
12445 check_defined_assignments (sym);
12447 if (!sym->attr.defined_assign_comp && super_type)
12448 sym->attr.defined_assign_comp
12449 = super_type->attr.defined_assign_comp;
12451 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
12452 all DEFERRED bindings are overridden. */
12453 if (super_type && super_type->attr.abstract && !sym->attr.abstract
12454 && !sym->attr.is_class
12455 && !ensure_not_abstract (sym, super_type))
12456 return false;
12458 /* Add derived type to the derived type list. */
12459 add_dt_to_dt_list (sym);
12461 /* Check if the type is finalizable. This is done in order to ensure that the
12462 finalization wrapper is generated early enough. */
12463 gfc_is_finalizable (sym, NULL);
12465 return true;
12469 /* The following procedure does the full resolution of a derived type,
12470 including resolution of all type-bound procedures (if present). In contrast
12471 to 'resolve_fl_derived0' this can only be done after the module has been
12472 parsed completely. */
12474 static bool
12475 resolve_fl_derived (gfc_symbol *sym)
12477 gfc_symbol *gen_dt = NULL;
12479 if (sym->attr.unlimited_polymorphic)
12480 return true;
12482 if (!sym->attr.is_class)
12483 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
12484 if (gen_dt && gen_dt->generic && gen_dt->generic->next
12485 && (!gen_dt->generic->sym->attr.use_assoc
12486 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
12487 && !gfc_notify_std (GFC_STD_F2003, "Generic name '%s' of function "
12488 "'%s' at %L being the same name as derived "
12489 "type at %L", sym->name,
12490 gen_dt->generic->sym == sym
12491 ? gen_dt->generic->next->sym->name
12492 : gen_dt->generic->sym->name,
12493 gen_dt->generic->sym == sym
12494 ? &gen_dt->generic->next->sym->declared_at
12495 : &gen_dt->generic->sym->declared_at,
12496 &sym->declared_at))
12497 return false;
12499 /* Resolve the finalizer procedures. */
12500 if (!gfc_resolve_finalizers (sym))
12501 return false;
12503 if (sym->attr.is_class && sym->ts.u.derived == NULL)
12505 /* Fix up incomplete CLASS symbols. */
12506 gfc_component *data = gfc_find_component (sym, "_data", true, true);
12507 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
12509 /* Nothing more to do for unlimited polymorphic entities. */
12510 if (data->ts.u.derived->attr.unlimited_polymorphic)
12511 return true;
12512 else if (vptr->ts.u.derived == NULL)
12514 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
12515 gcc_assert (vtab);
12516 vptr->ts.u.derived = vtab->ts.u.derived;
12520 if (!resolve_fl_derived0 (sym))
12521 return false;
12523 /* Resolve the type-bound procedures. */
12524 if (!resolve_typebound_procedures (sym))
12525 return false;
12527 return true;
12531 static bool
12532 resolve_fl_namelist (gfc_symbol *sym)
12534 gfc_namelist *nl;
12535 gfc_symbol *nlsym;
12537 for (nl = sym->namelist; nl; nl = nl->next)
12539 /* Check again, the check in match only works if NAMELIST comes
12540 after the decl. */
12541 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
12543 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
12544 "allowed", nl->sym->name, sym->name, &sym->declared_at);
12545 return false;
12548 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
12549 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12550 "with assumed shape in namelist '%s' at %L",
12551 nl->sym->name, sym->name, &sym->declared_at))
12552 return false;
12554 if (is_non_constant_shape_array (nl->sym)
12555 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12556 "with nonconstant shape in namelist '%s' at %L",
12557 nl->sym->name, sym->name, &sym->declared_at))
12558 return false;
12560 if (nl->sym->ts.type == BT_CHARACTER
12561 && (nl->sym->ts.u.cl->length == NULL
12562 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
12563 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' with "
12564 "nonconstant character length in "
12565 "namelist '%s' at %L", nl->sym->name,
12566 sym->name, &sym->declared_at))
12567 return false;
12569 /* FIXME: Once UDDTIO is implemented, the following can be
12570 removed. */
12571 if (nl->sym->ts.type == BT_CLASS)
12573 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
12574 "polymorphic and requires a defined input/output "
12575 "procedure", nl->sym->name, sym->name, &sym->declared_at);
12576 return false;
12579 if (nl->sym->ts.type == BT_DERIVED
12580 && (nl->sym->ts.u.derived->attr.alloc_comp
12581 || nl->sym->ts.u.derived->attr.pointer_comp))
12583 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' in "
12584 "namelist '%s' at %L with ALLOCATABLE "
12585 "or POINTER components", nl->sym->name,
12586 sym->name, &sym->declared_at))
12587 return false;
12589 /* FIXME: Once UDDTIO is implemented, the following can be
12590 removed. */
12591 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
12592 "ALLOCATABLE or POINTER components and thus requires "
12593 "a defined input/output procedure", nl->sym->name,
12594 sym->name, &sym->declared_at);
12595 return false;
12599 /* Reject PRIVATE objects in a PUBLIC namelist. */
12600 if (gfc_check_symbol_access (sym))
12602 for (nl = sym->namelist; nl; nl = nl->next)
12604 if (!nl->sym->attr.use_assoc
12605 && !is_sym_host_assoc (nl->sym, sym->ns)
12606 && !gfc_check_symbol_access (nl->sym))
12608 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
12609 "cannot be member of PUBLIC namelist '%s' at %L",
12610 nl->sym->name, sym->name, &sym->declared_at);
12611 return false;
12614 /* Types with private components that came here by USE-association. */
12615 if (nl->sym->ts.type == BT_DERIVED
12616 && derived_inaccessible (nl->sym->ts.u.derived))
12618 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
12619 "components and cannot be member of namelist '%s' at %L",
12620 nl->sym->name, sym->name, &sym->declared_at);
12621 return false;
12624 /* Types with private components that are defined in the same module. */
12625 if (nl->sym->ts.type == BT_DERIVED
12626 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
12627 && nl->sym->ts.u.derived->attr.private_comp)
12629 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
12630 "cannot be a member of PUBLIC namelist '%s' at %L",
12631 nl->sym->name, sym->name, &sym->declared_at);
12632 return false;
12638 /* 14.1.2 A module or internal procedure represent local entities
12639 of the same type as a namelist member and so are not allowed. */
12640 for (nl = sym->namelist; nl; nl = nl->next)
12642 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
12643 continue;
12645 if (nl->sym->attr.function && nl->sym == nl->sym->result)
12646 if ((nl->sym == sym->ns->proc_name)
12648 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
12649 continue;
12651 nlsym = NULL;
12652 if (nl->sym->name)
12653 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
12654 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
12656 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
12657 "attribute in '%s' at %L", nlsym->name,
12658 &sym->declared_at);
12659 return false;
12663 return true;
12667 static bool
12668 resolve_fl_parameter (gfc_symbol *sym)
12670 /* A parameter array's shape needs to be constant. */
12671 if (sym->as != NULL
12672 && (sym->as->type == AS_DEFERRED
12673 || is_non_constant_shape_array (sym)))
12675 gfc_error ("Parameter array '%s' at %L cannot be automatic "
12676 "or of deferred shape", sym->name, &sym->declared_at);
12677 return false;
12680 /* Make sure a parameter that has been implicitly typed still
12681 matches the implicit type, since PARAMETER statements can precede
12682 IMPLICIT statements. */
12683 if (sym->attr.implicit_type
12684 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
12685 sym->ns)))
12687 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
12688 "later IMPLICIT type", sym->name, &sym->declared_at);
12689 return false;
12692 /* Make sure the types of derived parameters are consistent. This
12693 type checking is deferred until resolution because the type may
12694 refer to a derived type from the host. */
12695 if (sym->ts.type == BT_DERIVED
12696 && !gfc_compare_types (&sym->ts, &sym->value->ts))
12698 gfc_error ("Incompatible derived type in PARAMETER at %L",
12699 &sym->value->where);
12700 return false;
12702 return true;
12706 /* Do anything necessary to resolve a symbol. Right now, we just
12707 assume that an otherwise unknown symbol is a variable. This sort
12708 of thing commonly happens for symbols in module. */
12710 static void
12711 resolve_symbol (gfc_symbol *sym)
12713 int check_constant, mp_flag;
12714 gfc_symtree *symtree;
12715 gfc_symtree *this_symtree;
12716 gfc_namespace *ns;
12717 gfc_component *c;
12718 symbol_attribute class_attr;
12719 gfc_array_spec *as;
12720 bool saved_specification_expr;
12722 if (sym->resolved)
12723 return;
12724 sym->resolved = 1;
12726 if (sym->attr.artificial)
12727 return;
12729 if (sym->attr.unlimited_polymorphic)
12730 return;
12732 if (sym->attr.flavor == FL_UNKNOWN
12733 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
12734 && !sym->attr.generic && !sym->attr.external
12735 && sym->attr.if_source == IFSRC_UNKNOWN))
12738 /* If we find that a flavorless symbol is an interface in one of the
12739 parent namespaces, find its symtree in this namespace, free the
12740 symbol and set the symtree to point to the interface symbol. */
12741 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
12743 symtree = gfc_find_symtree (ns->sym_root, sym->name);
12744 if (symtree && (symtree->n.sym->generic ||
12745 (symtree->n.sym->attr.flavor == FL_PROCEDURE
12746 && sym->ns->construct_entities)))
12748 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12749 sym->name);
12750 gfc_release_symbol (sym);
12751 symtree->n.sym->refs++;
12752 this_symtree->n.sym = symtree->n.sym;
12753 return;
12757 /* Otherwise give it a flavor according to such attributes as
12758 it has. */
12759 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
12760 && sym->attr.intrinsic == 0)
12761 sym->attr.flavor = FL_VARIABLE;
12762 else if (sym->attr.flavor == FL_UNKNOWN)
12764 sym->attr.flavor = FL_PROCEDURE;
12765 if (sym->attr.dimension)
12766 sym->attr.function = 1;
12770 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
12771 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
12773 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
12774 && !resolve_procedure_interface (sym))
12775 return;
12777 if (sym->attr.is_protected && !sym->attr.proc_pointer
12778 && (sym->attr.procedure || sym->attr.external))
12780 if (sym->attr.external)
12781 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12782 "at %L", &sym->declared_at);
12783 else
12784 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12785 "at %L", &sym->declared_at);
12787 return;
12790 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
12791 return;
12793 /* Symbols that are module procedures with results (functions) have
12794 the types and array specification copied for type checking in
12795 procedures that call them, as well as for saving to a module
12796 file. These symbols can't stand the scrutiny that their results
12797 can. */
12798 mp_flag = (sym->result != NULL && sym->result != sym);
12800 /* Make sure that the intrinsic is consistent with its internal
12801 representation. This needs to be done before assigning a default
12802 type to avoid spurious warnings. */
12803 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
12804 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
12805 return;
12807 /* Resolve associate names. */
12808 if (sym->assoc)
12809 resolve_assoc_var (sym, true);
12811 /* Assign default type to symbols that need one and don't have one. */
12812 if (sym->ts.type == BT_UNKNOWN)
12814 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
12816 gfc_set_default_type (sym, 1, NULL);
12819 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
12820 && !sym->attr.function && !sym->attr.subroutine
12821 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
12822 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
12824 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12826 /* The specific case of an external procedure should emit an error
12827 in the case that there is no implicit type. */
12828 if (!mp_flag)
12829 gfc_set_default_type (sym, sym->attr.external, NULL);
12830 else
12832 /* Result may be in another namespace. */
12833 resolve_symbol (sym->result);
12835 if (!sym->result->attr.proc_pointer)
12837 sym->ts = sym->result->ts;
12838 sym->as = gfc_copy_array_spec (sym->result->as);
12839 sym->attr.dimension = sym->result->attr.dimension;
12840 sym->attr.pointer = sym->result->attr.pointer;
12841 sym->attr.allocatable = sym->result->attr.allocatable;
12842 sym->attr.contiguous = sym->result->attr.contiguous;
12847 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12849 bool saved_specification_expr = specification_expr;
12850 specification_expr = true;
12851 gfc_resolve_array_spec (sym->result->as, false);
12852 specification_expr = saved_specification_expr;
12855 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12857 as = CLASS_DATA (sym)->as;
12858 class_attr = CLASS_DATA (sym)->attr;
12859 class_attr.pointer = class_attr.class_pointer;
12861 else
12863 class_attr = sym->attr;
12864 as = sym->as;
12867 /* F2008, C530. */
12868 if (sym->attr.contiguous
12869 && (!class_attr.dimension
12870 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
12871 && !class_attr.pointer)))
12873 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
12874 "array pointer or an assumed-shape or assumed-rank array",
12875 sym->name, &sym->declared_at);
12876 return;
12879 /* Assumed size arrays and assumed shape arrays must be dummy
12880 arguments. Array-spec's of implied-shape should have been resolved to
12881 AS_EXPLICIT already. */
12883 if (as)
12885 gcc_assert (as->type != AS_IMPLIED_SHAPE);
12886 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
12887 || as->type == AS_ASSUMED_SHAPE)
12888 && !sym->attr.dummy && !sym->attr.select_type_temporary)
12890 if (as->type == AS_ASSUMED_SIZE)
12891 gfc_error ("Assumed size array at %L must be a dummy argument",
12892 &sym->declared_at);
12893 else
12894 gfc_error ("Assumed shape array at %L must be a dummy argument",
12895 &sym->declared_at);
12896 return;
12898 /* TS 29113, C535a. */
12899 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
12900 && !sym->attr.select_type_temporary)
12902 gfc_error ("Assumed-rank array at %L must be a dummy argument",
12903 &sym->declared_at);
12904 return;
12906 if (as->type == AS_ASSUMED_RANK
12907 && (sym->attr.codimension || sym->attr.value))
12909 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
12910 "CODIMENSION attribute", &sym->declared_at);
12911 return;
12915 /* Make sure symbols with known intent or optional are really dummy
12916 variable. Because of ENTRY statement, this has to be deferred
12917 until resolution time. */
12919 if (!sym->attr.dummy
12920 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
12922 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
12923 return;
12926 if (sym->attr.value && !sym->attr.dummy)
12928 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
12929 "it is not a dummy argument", sym->name, &sym->declared_at);
12930 return;
12933 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
12935 gfc_charlen *cl = sym->ts.u.cl;
12936 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12938 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12939 "attribute must have constant length",
12940 sym->name, &sym->declared_at);
12941 return;
12944 if (sym->ts.is_c_interop
12945 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
12947 gfc_error ("C interoperable character dummy variable '%s' at %L "
12948 "with VALUE attribute must have length one",
12949 sym->name, &sym->declared_at);
12950 return;
12954 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
12955 && sym->ts.u.derived->attr.generic)
12957 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
12958 if (!sym->ts.u.derived)
12960 gfc_error ("The derived type '%s' at %L is of type '%s', "
12961 "which has not been defined", sym->name,
12962 &sym->declared_at, sym->ts.u.derived->name);
12963 sym->ts.type = BT_UNKNOWN;
12964 return;
12968 /* Use the same constraints as TYPE(*), except for the type check
12969 and that only scalars and assumed-size arrays are permitted. */
12970 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
12972 if (!sym->attr.dummy)
12974 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
12975 "a dummy argument", sym->name, &sym->declared_at);
12976 return;
12979 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
12980 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
12981 && sym->ts.type != BT_COMPLEX)
12983 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
12984 "of type TYPE(*) or of an numeric intrinsic type",
12985 sym->name, &sym->declared_at);
12986 return;
12989 if (sym->attr.allocatable || sym->attr.codimension
12990 || sym->attr.pointer || sym->attr.value)
12992 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
12993 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
12994 "attribute", sym->name, &sym->declared_at);
12995 return;
12998 if (sym->attr.intent == INTENT_OUT)
13000 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13001 "have the INTENT(OUT) attribute",
13002 sym->name, &sym->declared_at);
13003 return;
13005 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
13007 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
13008 "either be a scalar or an assumed-size array",
13009 sym->name, &sym->declared_at);
13010 return;
13013 /* Set the type to TYPE(*) and add a dimension(*) to ensure
13014 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
13015 packing. */
13016 sym->ts.type = BT_ASSUMED;
13017 sym->as = gfc_get_array_spec ();
13018 sym->as->type = AS_ASSUMED_SIZE;
13019 sym->as->rank = 1;
13020 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
13022 else if (sym->ts.type == BT_ASSUMED)
13024 /* TS 29113, C407a. */
13025 if (!sym->attr.dummy)
13027 gfc_error ("Assumed type of variable %s at %L is only permitted "
13028 "for dummy variables", sym->name, &sym->declared_at);
13029 return;
13031 if (sym->attr.allocatable || sym->attr.codimension
13032 || sym->attr.pointer || sym->attr.value)
13034 gfc_error ("Assumed-type variable %s at %L may not have the "
13035 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
13036 sym->name, &sym->declared_at);
13037 return;
13039 if (sym->attr.intent == INTENT_OUT)
13041 gfc_error ("Assumed-type variable %s at %L may not have the "
13042 "INTENT(OUT) attribute",
13043 sym->name, &sym->declared_at);
13044 return;
13046 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
13048 gfc_error ("Assumed-type variable %s at %L shall not be an "
13049 "explicit-shape array", sym->name, &sym->declared_at);
13050 return;
13054 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
13055 do this for something that was implicitly typed because that is handled
13056 in gfc_set_default_type. Handle dummy arguments and procedure
13057 definitions separately. Also, anything that is use associated is not
13058 handled here but instead is handled in the module it is declared in.
13059 Finally, derived type definitions are allowed to be BIND(C) since that
13060 only implies that they're interoperable, and they are checked fully for
13061 interoperability when a variable is declared of that type. */
13062 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
13063 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
13064 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
13066 bool t = true;
13068 /* First, make sure the variable is declared at the
13069 module-level scope (J3/04-007, Section 15.3). */
13070 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
13071 sym->attr.in_common == 0)
13073 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
13074 "is neither a COMMON block nor declared at the "
13075 "module level scope", sym->name, &(sym->declared_at));
13076 t = false;
13078 else if (sym->common_head != NULL)
13080 t = verify_com_block_vars_c_interop (sym->common_head);
13082 else
13084 /* If type() declaration, we need to verify that the components
13085 of the given type are all C interoperable, etc. */
13086 if (sym->ts.type == BT_DERIVED &&
13087 sym->ts.u.derived->attr.is_c_interop != 1)
13089 /* Make sure the user marked the derived type as BIND(C). If
13090 not, call the verify routine. This could print an error
13091 for the derived type more than once if multiple variables
13092 of that type are declared. */
13093 if (sym->ts.u.derived->attr.is_bind_c != 1)
13094 verify_bind_c_derived_type (sym->ts.u.derived);
13095 t = false;
13098 /* Verify the variable itself as C interoperable if it
13099 is BIND(C). It is not possible for this to succeed if
13100 the verify_bind_c_derived_type failed, so don't have to handle
13101 any error returned by verify_bind_c_derived_type. */
13102 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13103 sym->common_block);
13106 if (!t)
13108 /* clear the is_bind_c flag to prevent reporting errors more than
13109 once if something failed. */
13110 sym->attr.is_bind_c = 0;
13111 return;
13115 /* If a derived type symbol has reached this point, without its
13116 type being declared, we have an error. Notice that most
13117 conditions that produce undefined derived types have already
13118 been dealt with. However, the likes of:
13119 implicit type(t) (t) ..... call foo (t) will get us here if
13120 the type is not declared in the scope of the implicit
13121 statement. Change the type to BT_UNKNOWN, both because it is so
13122 and to prevent an ICE. */
13123 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13124 && sym->ts.u.derived->components == NULL
13125 && !sym->ts.u.derived->attr.zero_comp)
13127 gfc_error ("The derived type '%s' at %L is of type '%s', "
13128 "which has not been defined", sym->name,
13129 &sym->declared_at, sym->ts.u.derived->name);
13130 sym->ts.type = BT_UNKNOWN;
13131 return;
13134 /* Make sure that the derived type has been resolved and that the
13135 derived type is visible in the symbol's namespace, if it is a
13136 module function and is not PRIVATE. */
13137 if (sym->ts.type == BT_DERIVED
13138 && sym->ts.u.derived->attr.use_assoc
13139 && sym->ns->proc_name
13140 && sym->ns->proc_name->attr.flavor == FL_MODULE
13141 && !resolve_fl_derived (sym->ts.u.derived))
13142 return;
13144 /* Unless the derived-type declaration is use associated, Fortran 95
13145 does not allow public entries of private derived types.
13146 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
13147 161 in 95-006r3. */
13148 if (sym->ts.type == BT_DERIVED
13149 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
13150 && !sym->ts.u.derived->attr.use_assoc
13151 && gfc_check_symbol_access (sym)
13152 && !gfc_check_symbol_access (sym->ts.u.derived)
13153 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s '%s' at %L of PRIVATE "
13154 "derived type '%s'",
13155 (sym->attr.flavor == FL_PARAMETER)
13156 ? "parameter" : "variable",
13157 sym->name, &sym->declared_at,
13158 sym->ts.u.derived->name))
13159 return;
13161 /* F2008, C1302. */
13162 if (sym->ts.type == BT_DERIVED
13163 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
13164 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
13165 || sym->ts.u.derived->attr.lock_comp)
13166 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
13168 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
13169 "type LOCK_TYPE must be a coarray", sym->name,
13170 &sym->declared_at);
13171 return;
13174 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
13175 default initialization is defined (5.1.2.4.4). */
13176 if (sym->ts.type == BT_DERIVED
13177 && sym->attr.dummy
13178 && sym->attr.intent == INTENT_OUT
13179 && sym->as
13180 && sym->as->type == AS_ASSUMED_SIZE)
13182 for (c = sym->ts.u.derived->components; c; c = c->next)
13184 if (c->initializer)
13186 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
13187 "ASSUMED SIZE and so cannot have a default initializer",
13188 sym->name, &sym->declared_at);
13189 return;
13194 /* F2008, C542. */
13195 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
13196 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
13198 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
13199 "INTENT(OUT)", sym->name, &sym->declared_at);
13200 return;
13203 /* F2008, C525. */
13204 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13205 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13206 && CLASS_DATA (sym)->attr.coarray_comp))
13207 || class_attr.codimension)
13208 && (sym->attr.result || sym->result == sym))
13210 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
13211 "a coarray component", sym->name, &sym->declared_at);
13212 return;
13215 /* F2008, C524. */
13216 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
13217 && sym->ts.u.derived->ts.is_iso_c)
13219 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
13220 "shall not be a coarray", sym->name, &sym->declared_at);
13221 return;
13224 /* F2008, C525. */
13225 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13226 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13227 && CLASS_DATA (sym)->attr.coarray_comp))
13228 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
13229 || class_attr.allocatable))
13231 gfc_error ("Variable '%s' at %L with coarray component shall be a "
13232 "nonpointer, nonallocatable scalar, which is not a coarray",
13233 sym->name, &sym->declared_at);
13234 return;
13237 /* F2008, C526. The function-result case was handled above. */
13238 if (class_attr.codimension
13239 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
13240 || sym->attr.select_type_temporary
13241 || sym->ns->save_all
13242 || sym->ns->proc_name->attr.flavor == FL_MODULE
13243 || sym->ns->proc_name->attr.is_main_program
13244 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
13246 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
13247 "nor a dummy argument", sym->name, &sym->declared_at);
13248 return;
13250 /* F2008, C528. */
13251 else if (class_attr.codimension && !sym->attr.select_type_temporary
13252 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
13254 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
13255 "deferred shape", sym->name, &sym->declared_at);
13256 return;
13258 else if (class_attr.codimension && class_attr.allocatable && as
13259 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
13261 gfc_error ("Allocatable coarray variable '%s' at %L must have "
13262 "deferred shape", sym->name, &sym->declared_at);
13263 return;
13266 /* F2008, C541. */
13267 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13268 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13269 && CLASS_DATA (sym)->attr.coarray_comp))
13270 || (class_attr.codimension && class_attr.allocatable))
13271 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
13273 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
13274 "allocatable coarray or have coarray components",
13275 sym->name, &sym->declared_at);
13276 return;
13279 if (class_attr.codimension && sym->attr.dummy
13280 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
13282 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
13283 "procedure '%s'", sym->name, &sym->declared_at,
13284 sym->ns->proc_name->name);
13285 return;
13288 if (sym->ts.type == BT_LOGICAL
13289 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
13290 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
13291 && sym->ns->proc_name->attr.is_bind_c)))
13293 int i;
13294 for (i = 0; gfc_logical_kinds[i].kind; i++)
13295 if (gfc_logical_kinds[i].kind == sym->ts.kind)
13296 break;
13297 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
13298 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument '%s' at "
13299 "%L with non-C_Bool kind in BIND(C) procedure "
13300 "'%s'", sym->name, &sym->declared_at,
13301 sym->ns->proc_name->name))
13302 return;
13303 else if (!gfc_logical_kinds[i].c_bool
13304 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
13305 "'%s' at %L with non-C_Bool kind in "
13306 "BIND(C) procedure '%s'", sym->name,
13307 &sym->declared_at,
13308 sym->attr.function ? sym->name
13309 : sym->ns->proc_name->name))
13310 return;
13313 switch (sym->attr.flavor)
13315 case FL_VARIABLE:
13316 if (!resolve_fl_variable (sym, mp_flag))
13317 return;
13318 break;
13320 case FL_PROCEDURE:
13321 if (!resolve_fl_procedure (sym, mp_flag))
13322 return;
13323 break;
13325 case FL_NAMELIST:
13326 if (!resolve_fl_namelist (sym))
13327 return;
13328 break;
13330 case FL_PARAMETER:
13331 if (!resolve_fl_parameter (sym))
13332 return;
13333 break;
13335 default:
13336 break;
13339 /* Resolve array specifier. Check as well some constraints
13340 on COMMON blocks. */
13342 check_constant = sym->attr.in_common && !sym->attr.pointer;
13344 /* Set the formal_arg_flag so that check_conflict will not throw
13345 an error for host associated variables in the specification
13346 expression for an array_valued function. */
13347 if (sym->attr.function && sym->as)
13348 formal_arg_flag = 1;
13350 saved_specification_expr = specification_expr;
13351 specification_expr = true;
13352 gfc_resolve_array_spec (sym->as, check_constant);
13353 specification_expr = saved_specification_expr;
13355 formal_arg_flag = 0;
13357 /* Resolve formal namespaces. */
13358 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
13359 && !sym->attr.contained && !sym->attr.intrinsic)
13360 gfc_resolve (sym->formal_ns);
13362 /* Make sure the formal namespace is present. */
13363 if (sym->formal && !sym->formal_ns)
13365 gfc_formal_arglist *formal = sym->formal;
13366 while (formal && !formal->sym)
13367 formal = formal->next;
13369 if (formal)
13371 sym->formal_ns = formal->sym->ns;
13372 if (sym->ns != formal->sym->ns)
13373 sym->formal_ns->refs++;
13377 /* Check threadprivate restrictions. */
13378 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
13379 && (!sym->attr.in_common
13380 && sym->module == NULL
13381 && (sym->ns->proc_name == NULL
13382 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
13383 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
13385 /* If we have come this far we can apply default-initializers, as
13386 described in 14.7.5, to those variables that have not already
13387 been assigned one. */
13388 if (sym->ts.type == BT_DERIVED
13389 && !sym->value
13390 && !sym->attr.allocatable
13391 && !sym->attr.alloc_comp)
13393 symbol_attribute *a = &sym->attr;
13395 if ((!a->save && !a->dummy && !a->pointer
13396 && !a->in_common && !a->use_assoc
13397 && (a->referenced || a->result)
13398 && !(a->function && sym != sym->result))
13399 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
13400 apply_default_init (sym);
13403 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
13404 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
13405 && !CLASS_DATA (sym)->attr.class_pointer
13406 && !CLASS_DATA (sym)->attr.allocatable)
13407 apply_default_init (sym);
13409 /* If this symbol has a type-spec, check it. */
13410 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
13411 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
13412 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
13413 return;
13417 /************* Resolve DATA statements *************/
13419 static struct
13421 gfc_data_value *vnode;
13422 mpz_t left;
13424 values;
13427 /* Advance the values structure to point to the next value in the data list. */
13429 static bool
13430 next_data_value (void)
13432 while (mpz_cmp_ui (values.left, 0) == 0)
13435 if (values.vnode->next == NULL)
13436 return false;
13438 values.vnode = values.vnode->next;
13439 mpz_set (values.left, values.vnode->repeat);
13442 return true;
13446 static bool
13447 check_data_variable (gfc_data_variable *var, locus *where)
13449 gfc_expr *e;
13450 mpz_t size;
13451 mpz_t offset;
13452 bool t;
13453 ar_type mark = AR_UNKNOWN;
13454 int i;
13455 mpz_t section_index[GFC_MAX_DIMENSIONS];
13456 gfc_ref *ref;
13457 gfc_array_ref *ar;
13458 gfc_symbol *sym;
13459 int has_pointer;
13461 if (!gfc_resolve_expr (var->expr))
13462 return false;
13464 ar = NULL;
13465 mpz_init_set_si (offset, 0);
13466 e = var->expr;
13468 if (e->expr_type != EXPR_VARIABLE)
13469 gfc_internal_error ("check_data_variable(): Bad expression");
13471 sym = e->symtree->n.sym;
13473 if (sym->ns->is_block_data && !sym->attr.in_common)
13475 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
13476 sym->name, &sym->declared_at);
13479 if (e->ref == NULL && sym->as)
13481 gfc_error ("DATA array '%s' at %L must be specified in a previous"
13482 " declaration", sym->name, where);
13483 return false;
13486 has_pointer = sym->attr.pointer;
13488 if (gfc_is_coindexed (e))
13490 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
13491 where);
13492 return false;
13495 for (ref = e->ref; ref; ref = ref->next)
13497 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
13498 has_pointer = 1;
13500 if (has_pointer
13501 && ref->type == REF_ARRAY
13502 && ref->u.ar.type != AR_FULL)
13504 gfc_error ("DATA element '%s' at %L is a pointer and so must "
13505 "be a full array", sym->name, where);
13506 return false;
13510 if (e->rank == 0 || has_pointer)
13512 mpz_init_set_ui (size, 1);
13513 ref = NULL;
13515 else
13517 ref = e->ref;
13519 /* Find the array section reference. */
13520 for (ref = e->ref; ref; ref = ref->next)
13522 if (ref->type != REF_ARRAY)
13523 continue;
13524 if (ref->u.ar.type == AR_ELEMENT)
13525 continue;
13526 break;
13528 gcc_assert (ref);
13530 /* Set marks according to the reference pattern. */
13531 switch (ref->u.ar.type)
13533 case AR_FULL:
13534 mark = AR_FULL;
13535 break;
13537 case AR_SECTION:
13538 ar = &ref->u.ar;
13539 /* Get the start position of array section. */
13540 gfc_get_section_index (ar, section_index, &offset);
13541 mark = AR_SECTION;
13542 break;
13544 default:
13545 gcc_unreachable ();
13548 if (!gfc_array_size (e, &size))
13550 gfc_error ("Nonconstant array section at %L in DATA statement",
13551 &e->where);
13552 mpz_clear (offset);
13553 return false;
13557 t = true;
13559 while (mpz_cmp_ui (size, 0) > 0)
13561 if (!next_data_value ())
13563 gfc_error ("DATA statement at %L has more variables than values",
13564 where);
13565 t = false;
13566 break;
13569 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
13570 if (!t)
13571 break;
13573 /* If we have more than one element left in the repeat count,
13574 and we have more than one element left in the target variable,
13575 then create a range assignment. */
13576 /* FIXME: Only done for full arrays for now, since array sections
13577 seem tricky. */
13578 if (mark == AR_FULL && ref && ref->next == NULL
13579 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
13581 mpz_t range;
13583 if (mpz_cmp (size, values.left) >= 0)
13585 mpz_init_set (range, values.left);
13586 mpz_sub (size, size, values.left);
13587 mpz_set_ui (values.left, 0);
13589 else
13591 mpz_init_set (range, size);
13592 mpz_sub (values.left, values.left, size);
13593 mpz_set_ui (size, 0);
13596 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13597 offset, &range);
13599 mpz_add (offset, offset, range);
13600 mpz_clear (range);
13602 if (!t)
13603 break;
13606 /* Assign initial value to symbol. */
13607 else
13609 mpz_sub_ui (values.left, values.left, 1);
13610 mpz_sub_ui (size, size, 1);
13612 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13613 offset, NULL);
13614 if (!t)
13615 break;
13617 if (mark == AR_FULL)
13618 mpz_add_ui (offset, offset, 1);
13620 /* Modify the array section indexes and recalculate the offset
13621 for next element. */
13622 else if (mark == AR_SECTION)
13623 gfc_advance_section (section_index, ar, &offset);
13627 if (mark == AR_SECTION)
13629 for (i = 0; i < ar->dimen; i++)
13630 mpz_clear (section_index[i]);
13633 mpz_clear (size);
13634 mpz_clear (offset);
13636 return t;
13640 static bool traverse_data_var (gfc_data_variable *, locus *);
13642 /* Iterate over a list of elements in a DATA statement. */
13644 static bool
13645 traverse_data_list (gfc_data_variable *var, locus *where)
13647 mpz_t trip;
13648 iterator_stack frame;
13649 gfc_expr *e, *start, *end, *step;
13650 bool retval = true;
13652 mpz_init (frame.value);
13653 mpz_init (trip);
13655 start = gfc_copy_expr (var->iter.start);
13656 end = gfc_copy_expr (var->iter.end);
13657 step = gfc_copy_expr (var->iter.step);
13659 if (!gfc_simplify_expr (start, 1)
13660 || start->expr_type != EXPR_CONSTANT)
13662 gfc_error ("start of implied-do loop at %L could not be "
13663 "simplified to a constant value", &start->where);
13664 retval = false;
13665 goto cleanup;
13667 if (!gfc_simplify_expr (end, 1)
13668 || end->expr_type != EXPR_CONSTANT)
13670 gfc_error ("end of implied-do loop at %L could not be "
13671 "simplified to a constant value", &start->where);
13672 retval = false;
13673 goto cleanup;
13675 if (!gfc_simplify_expr (step, 1)
13676 || step->expr_type != EXPR_CONSTANT)
13678 gfc_error ("step of implied-do loop at %L could not be "
13679 "simplified to a constant value", &start->where);
13680 retval = false;
13681 goto cleanup;
13684 mpz_set (trip, end->value.integer);
13685 mpz_sub (trip, trip, start->value.integer);
13686 mpz_add (trip, trip, step->value.integer);
13688 mpz_div (trip, trip, step->value.integer);
13690 mpz_set (frame.value, start->value.integer);
13692 frame.prev = iter_stack;
13693 frame.variable = var->iter.var->symtree;
13694 iter_stack = &frame;
13696 while (mpz_cmp_ui (trip, 0) > 0)
13698 if (!traverse_data_var (var->list, where))
13700 retval = false;
13701 goto cleanup;
13704 e = gfc_copy_expr (var->expr);
13705 if (!gfc_simplify_expr (e, 1))
13707 gfc_free_expr (e);
13708 retval = false;
13709 goto cleanup;
13712 mpz_add (frame.value, frame.value, step->value.integer);
13714 mpz_sub_ui (trip, trip, 1);
13717 cleanup:
13718 mpz_clear (frame.value);
13719 mpz_clear (trip);
13721 gfc_free_expr (start);
13722 gfc_free_expr (end);
13723 gfc_free_expr (step);
13725 iter_stack = frame.prev;
13726 return retval;
13730 /* Type resolve variables in the variable list of a DATA statement. */
13732 static bool
13733 traverse_data_var (gfc_data_variable *var, locus *where)
13735 bool t;
13737 for (; var; var = var->next)
13739 if (var->expr == NULL)
13740 t = traverse_data_list (var, where);
13741 else
13742 t = check_data_variable (var, where);
13744 if (!t)
13745 return false;
13748 return true;
13752 /* Resolve the expressions and iterators associated with a data statement.
13753 This is separate from the assignment checking because data lists should
13754 only be resolved once. */
13756 static bool
13757 resolve_data_variables (gfc_data_variable *d)
13759 for (; d; d = d->next)
13761 if (d->list == NULL)
13763 if (!gfc_resolve_expr (d->expr))
13764 return false;
13766 else
13768 if (!gfc_resolve_iterator (&d->iter, false, true))
13769 return false;
13771 if (!resolve_data_variables (d->list))
13772 return false;
13776 return true;
13780 /* Resolve a single DATA statement. We implement this by storing a pointer to
13781 the value list into static variables, and then recursively traversing the
13782 variables list, expanding iterators and such. */
13784 static void
13785 resolve_data (gfc_data *d)
13788 if (!resolve_data_variables (d->var))
13789 return;
13791 values.vnode = d->value;
13792 if (d->value == NULL)
13793 mpz_set_ui (values.left, 0);
13794 else
13795 mpz_set (values.left, d->value->repeat);
13797 if (!traverse_data_var (d->var, &d->where))
13798 return;
13800 /* At this point, we better not have any values left. */
13802 if (next_data_value ())
13803 gfc_error ("DATA statement at %L has more values than variables",
13804 &d->where);
13808 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
13809 accessed by host or use association, is a dummy argument to a pure function,
13810 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
13811 is storage associated with any such variable, shall not be used in the
13812 following contexts: (clients of this function). */
13814 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
13815 procedure. Returns zero if assignment is OK, nonzero if there is a
13816 problem. */
13818 gfc_impure_variable (gfc_symbol *sym)
13820 gfc_symbol *proc;
13821 gfc_namespace *ns;
13823 if (sym->attr.use_assoc || sym->attr.in_common)
13824 return 1;
13826 /* Check if the symbol's ns is inside the pure procedure. */
13827 for (ns = gfc_current_ns; ns; ns = ns->parent)
13829 if (ns == sym->ns)
13830 break;
13831 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
13832 return 1;
13835 proc = sym->ns->proc_name;
13836 if (sym->attr.dummy
13837 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
13838 || proc->attr.function))
13839 return 1;
13841 /* TODO: Sort out what can be storage associated, if anything, and include
13842 it here. In principle equivalences should be scanned but it does not
13843 seem to be possible to storage associate an impure variable this way. */
13844 return 0;
13848 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
13849 current namespace is inside a pure procedure. */
13852 gfc_pure (gfc_symbol *sym)
13854 symbol_attribute attr;
13855 gfc_namespace *ns;
13857 if (sym == NULL)
13859 /* Check if the current namespace or one of its parents
13860 belongs to a pure procedure. */
13861 for (ns = gfc_current_ns; ns; ns = ns->parent)
13863 sym = ns->proc_name;
13864 if (sym == NULL)
13865 return 0;
13866 attr = sym->attr;
13867 if (attr.flavor == FL_PROCEDURE && attr.pure)
13868 return 1;
13870 return 0;
13873 attr = sym->attr;
13875 return attr.flavor == FL_PROCEDURE && attr.pure;
13879 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
13880 checks if the current namespace is implicitly pure. Note that this
13881 function returns false for a PURE procedure. */
13884 gfc_implicit_pure (gfc_symbol *sym)
13886 gfc_namespace *ns;
13888 if (sym == NULL)
13890 /* Check if the current procedure is implicit_pure. Walk up
13891 the procedure list until we find a procedure. */
13892 for (ns = gfc_current_ns; ns; ns = ns->parent)
13894 sym = ns->proc_name;
13895 if (sym == NULL)
13896 return 0;
13898 if (sym->attr.flavor == FL_PROCEDURE)
13899 break;
13903 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
13904 && !sym->attr.pure;
13908 /* Test whether the current procedure is elemental or not. */
13911 gfc_elemental (gfc_symbol *sym)
13913 symbol_attribute attr;
13915 if (sym == NULL)
13916 sym = gfc_current_ns->proc_name;
13917 if (sym == NULL)
13918 return 0;
13919 attr = sym->attr;
13921 return attr.flavor == FL_PROCEDURE && attr.elemental;
13925 /* Warn about unused labels. */
13927 static void
13928 warn_unused_fortran_label (gfc_st_label *label)
13930 if (label == NULL)
13931 return;
13933 warn_unused_fortran_label (label->left);
13935 if (label->defined == ST_LABEL_UNKNOWN)
13936 return;
13938 switch (label->referenced)
13940 case ST_LABEL_UNKNOWN:
13941 gfc_warning ("Label %d at %L defined but not used", label->value,
13942 &label->where);
13943 break;
13945 case ST_LABEL_BAD_TARGET:
13946 gfc_warning ("Label %d at %L defined but cannot be used",
13947 label->value, &label->where);
13948 break;
13950 default:
13951 break;
13954 warn_unused_fortran_label (label->right);
13958 /* Returns the sequence type of a symbol or sequence. */
13960 static seq_type
13961 sequence_type (gfc_typespec ts)
13963 seq_type result;
13964 gfc_component *c;
13966 switch (ts.type)
13968 case BT_DERIVED:
13970 if (ts.u.derived->components == NULL)
13971 return SEQ_NONDEFAULT;
13973 result = sequence_type (ts.u.derived->components->ts);
13974 for (c = ts.u.derived->components->next; c; c = c->next)
13975 if (sequence_type (c->ts) != result)
13976 return SEQ_MIXED;
13978 return result;
13980 case BT_CHARACTER:
13981 if (ts.kind != gfc_default_character_kind)
13982 return SEQ_NONDEFAULT;
13984 return SEQ_CHARACTER;
13986 case BT_INTEGER:
13987 if (ts.kind != gfc_default_integer_kind)
13988 return SEQ_NONDEFAULT;
13990 return SEQ_NUMERIC;
13992 case BT_REAL:
13993 if (!(ts.kind == gfc_default_real_kind
13994 || ts.kind == gfc_default_double_kind))
13995 return SEQ_NONDEFAULT;
13997 return SEQ_NUMERIC;
13999 case BT_COMPLEX:
14000 if (ts.kind != gfc_default_complex_kind)
14001 return SEQ_NONDEFAULT;
14003 return SEQ_NUMERIC;
14005 case BT_LOGICAL:
14006 if (ts.kind != gfc_default_logical_kind)
14007 return SEQ_NONDEFAULT;
14009 return SEQ_NUMERIC;
14011 default:
14012 return SEQ_NONDEFAULT;
14017 /* Resolve derived type EQUIVALENCE object. */
14019 static bool
14020 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
14022 gfc_component *c = derived->components;
14024 if (!derived)
14025 return true;
14027 /* Shall not be an object of nonsequence derived type. */
14028 if (!derived->attr.sequence)
14030 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
14031 "attribute to be an EQUIVALENCE object", sym->name,
14032 &e->where);
14033 return false;
14036 /* Shall not have allocatable components. */
14037 if (derived->attr.alloc_comp)
14039 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
14040 "components to be an EQUIVALENCE object",sym->name,
14041 &e->where);
14042 return false;
14045 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
14047 gfc_error ("Derived type variable '%s' at %L with default "
14048 "initialization cannot be in EQUIVALENCE with a variable "
14049 "in COMMON", sym->name, &e->where);
14050 return false;
14053 for (; c ; c = c->next)
14055 if (c->ts.type == BT_DERIVED
14056 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
14057 return false;
14059 /* Shall not be an object of sequence derived type containing a pointer
14060 in the structure. */
14061 if (c->attr.pointer)
14063 gfc_error ("Derived type variable '%s' at %L with pointer "
14064 "component(s) cannot be an EQUIVALENCE object",
14065 sym->name, &e->where);
14066 return false;
14069 return true;
14073 /* Resolve equivalence object.
14074 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
14075 an allocatable array, an object of nonsequence derived type, an object of
14076 sequence derived type containing a pointer at any level of component
14077 selection, an automatic object, a function name, an entry name, a result
14078 name, a named constant, a structure component, or a subobject of any of
14079 the preceding objects. A substring shall not have length zero. A
14080 derived type shall not have components with default initialization nor
14081 shall two objects of an equivalence group be initialized.
14082 Either all or none of the objects shall have an protected attribute.
14083 The simple constraints are done in symbol.c(check_conflict) and the rest
14084 are implemented here. */
14086 static void
14087 resolve_equivalence (gfc_equiv *eq)
14089 gfc_symbol *sym;
14090 gfc_symbol *first_sym;
14091 gfc_expr *e;
14092 gfc_ref *r;
14093 locus *last_where = NULL;
14094 seq_type eq_type, last_eq_type;
14095 gfc_typespec *last_ts;
14096 int object, cnt_protected;
14097 const char *msg;
14099 last_ts = &eq->expr->symtree->n.sym->ts;
14101 first_sym = eq->expr->symtree->n.sym;
14103 cnt_protected = 0;
14105 for (object = 1; eq; eq = eq->eq, object++)
14107 e = eq->expr;
14109 e->ts = e->symtree->n.sym->ts;
14110 /* match_varspec might not know yet if it is seeing
14111 array reference or substring reference, as it doesn't
14112 know the types. */
14113 if (e->ref && e->ref->type == REF_ARRAY)
14115 gfc_ref *ref = e->ref;
14116 sym = e->symtree->n.sym;
14118 if (sym->attr.dimension)
14120 ref->u.ar.as = sym->as;
14121 ref = ref->next;
14124 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
14125 if (e->ts.type == BT_CHARACTER
14126 && ref
14127 && ref->type == REF_ARRAY
14128 && ref->u.ar.dimen == 1
14129 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
14130 && ref->u.ar.stride[0] == NULL)
14132 gfc_expr *start = ref->u.ar.start[0];
14133 gfc_expr *end = ref->u.ar.end[0];
14134 void *mem = NULL;
14136 /* Optimize away the (:) reference. */
14137 if (start == NULL && end == NULL)
14139 if (e->ref == ref)
14140 e->ref = ref->next;
14141 else
14142 e->ref->next = ref->next;
14143 mem = ref;
14145 else
14147 ref->type = REF_SUBSTRING;
14148 if (start == NULL)
14149 start = gfc_get_int_expr (gfc_default_integer_kind,
14150 NULL, 1);
14151 ref->u.ss.start = start;
14152 if (end == NULL && e->ts.u.cl)
14153 end = gfc_copy_expr (e->ts.u.cl->length);
14154 ref->u.ss.end = end;
14155 ref->u.ss.length = e->ts.u.cl;
14156 e->ts.u.cl = NULL;
14158 ref = ref->next;
14159 free (mem);
14162 /* Any further ref is an error. */
14163 if (ref)
14165 gcc_assert (ref->type == REF_ARRAY);
14166 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
14167 &ref->u.ar.where);
14168 continue;
14172 if (!gfc_resolve_expr (e))
14173 continue;
14175 sym = e->symtree->n.sym;
14177 if (sym->attr.is_protected)
14178 cnt_protected++;
14179 if (cnt_protected > 0 && cnt_protected != object)
14181 gfc_error ("Either all or none of the objects in the "
14182 "EQUIVALENCE set at %L shall have the "
14183 "PROTECTED attribute",
14184 &e->where);
14185 break;
14188 /* Shall not equivalence common block variables in a PURE procedure. */
14189 if (sym->ns->proc_name
14190 && sym->ns->proc_name->attr.pure
14191 && sym->attr.in_common)
14193 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
14194 "object in the pure procedure '%s'",
14195 sym->name, &e->where, sym->ns->proc_name->name);
14196 break;
14199 /* Shall not be a named constant. */
14200 if (e->expr_type == EXPR_CONSTANT)
14202 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
14203 "object", sym->name, &e->where);
14204 continue;
14207 if (e->ts.type == BT_DERIVED
14208 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
14209 continue;
14211 /* Check that the types correspond correctly:
14212 Note 5.28:
14213 A numeric sequence structure may be equivalenced to another sequence
14214 structure, an object of default integer type, default real type, double
14215 precision real type, default logical type such that components of the
14216 structure ultimately only become associated to objects of the same
14217 kind. A character sequence structure may be equivalenced to an object
14218 of default character kind or another character sequence structure.
14219 Other objects may be equivalenced only to objects of the same type and
14220 kind parameters. */
14222 /* Identical types are unconditionally OK. */
14223 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
14224 goto identical_types;
14226 last_eq_type = sequence_type (*last_ts);
14227 eq_type = sequence_type (sym->ts);
14229 /* Since the pair of objects is not of the same type, mixed or
14230 non-default sequences can be rejected. */
14232 msg = "Sequence %s with mixed components in EQUIVALENCE "
14233 "statement at %L with different type objects";
14234 if ((object ==2
14235 && last_eq_type == SEQ_MIXED
14236 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14237 || (eq_type == SEQ_MIXED
14238 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14239 continue;
14241 msg = "Non-default type object or sequence %s in EQUIVALENCE "
14242 "statement at %L with objects of different type";
14243 if ((object ==2
14244 && last_eq_type == SEQ_NONDEFAULT
14245 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14246 || (eq_type == SEQ_NONDEFAULT
14247 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14248 continue;
14250 msg ="Non-CHARACTER object '%s' in default CHARACTER "
14251 "EQUIVALENCE statement at %L";
14252 if (last_eq_type == SEQ_CHARACTER
14253 && eq_type != SEQ_CHARACTER
14254 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14255 continue;
14257 msg ="Non-NUMERIC object '%s' in default NUMERIC "
14258 "EQUIVALENCE statement at %L";
14259 if (last_eq_type == SEQ_NUMERIC
14260 && eq_type != SEQ_NUMERIC
14261 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14262 continue;
14264 identical_types:
14265 last_ts =&sym->ts;
14266 last_where = &e->where;
14268 if (!e->ref)
14269 continue;
14271 /* Shall not be an automatic array. */
14272 if (e->ref->type == REF_ARRAY
14273 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
14275 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
14276 "an EQUIVALENCE object", sym->name, &e->where);
14277 continue;
14280 r = e->ref;
14281 while (r)
14283 /* Shall not be a structure component. */
14284 if (r->type == REF_COMPONENT)
14286 gfc_error ("Structure component '%s' at %L cannot be an "
14287 "EQUIVALENCE object",
14288 r->u.c.component->name, &e->where);
14289 break;
14292 /* A substring shall not have length zero. */
14293 if (r->type == REF_SUBSTRING)
14295 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
14297 gfc_error ("Substring at %L has length zero",
14298 &r->u.ss.start->where);
14299 break;
14302 r = r->next;
14308 /* Resolve function and ENTRY types, issue diagnostics if needed. */
14310 static void
14311 resolve_fntype (gfc_namespace *ns)
14313 gfc_entry_list *el;
14314 gfc_symbol *sym;
14316 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
14317 return;
14319 /* If there are any entries, ns->proc_name is the entry master
14320 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
14321 if (ns->entries)
14322 sym = ns->entries->sym;
14323 else
14324 sym = ns->proc_name;
14325 if (sym->result == sym
14326 && sym->ts.type == BT_UNKNOWN
14327 && !gfc_set_default_type (sym, 0, NULL)
14328 && !sym->attr.untyped)
14330 gfc_error ("Function '%s' at %L has no IMPLICIT type",
14331 sym->name, &sym->declared_at);
14332 sym->attr.untyped = 1;
14335 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
14336 && !sym->attr.contained
14337 && !gfc_check_symbol_access (sym->ts.u.derived)
14338 && gfc_check_symbol_access (sym))
14340 gfc_notify_std (GFC_STD_F2003, "PUBLIC function '%s' at "
14341 "%L of PRIVATE type '%s'", sym->name,
14342 &sym->declared_at, sym->ts.u.derived->name);
14345 if (ns->entries)
14346 for (el = ns->entries->next; el; el = el->next)
14348 if (el->sym->result == el->sym
14349 && el->sym->ts.type == BT_UNKNOWN
14350 && !gfc_set_default_type (el->sym, 0, NULL)
14351 && !el->sym->attr.untyped)
14353 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
14354 el->sym->name, &el->sym->declared_at);
14355 el->sym->attr.untyped = 1;
14361 /* 12.3.2.1.1 Defined operators. */
14363 static bool
14364 check_uop_procedure (gfc_symbol *sym, locus where)
14366 gfc_formal_arglist *formal;
14368 if (!sym->attr.function)
14370 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
14371 sym->name, &where);
14372 return false;
14375 if (sym->ts.type == BT_CHARACTER
14376 && !(sym->ts.u.cl && sym->ts.u.cl->length)
14377 && !(sym->result && sym->result->ts.u.cl
14378 && sym->result->ts.u.cl->length))
14380 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
14381 "character length", sym->name, &where);
14382 return false;
14385 formal = gfc_sym_get_dummy_args (sym);
14386 if (!formal || !formal->sym)
14388 gfc_error ("User operator procedure '%s' at %L must have at least "
14389 "one argument", sym->name, &where);
14390 return false;
14393 if (formal->sym->attr.intent != INTENT_IN)
14395 gfc_error ("First argument of operator interface at %L must be "
14396 "INTENT(IN)", &where);
14397 return false;
14400 if (formal->sym->attr.optional)
14402 gfc_error ("First argument of operator interface at %L cannot be "
14403 "optional", &where);
14404 return false;
14407 formal = formal->next;
14408 if (!formal || !formal->sym)
14409 return true;
14411 if (formal->sym->attr.intent != INTENT_IN)
14413 gfc_error ("Second argument of operator interface at %L must be "
14414 "INTENT(IN)", &where);
14415 return false;
14418 if (formal->sym->attr.optional)
14420 gfc_error ("Second argument of operator interface at %L cannot be "
14421 "optional", &where);
14422 return false;
14425 if (formal->next)
14427 gfc_error ("Operator interface at %L must have, at most, two "
14428 "arguments", &where);
14429 return false;
14432 return true;
14435 static void
14436 gfc_resolve_uops (gfc_symtree *symtree)
14438 gfc_interface *itr;
14440 if (symtree == NULL)
14441 return;
14443 gfc_resolve_uops (symtree->left);
14444 gfc_resolve_uops (symtree->right);
14446 for (itr = symtree->n.uop->op; itr; itr = itr->next)
14447 check_uop_procedure (itr->sym, itr->sym->declared_at);
14451 /* Examine all of the expressions associated with a program unit,
14452 assign types to all intermediate expressions, make sure that all
14453 assignments are to compatible types and figure out which names
14454 refer to which functions or subroutines. It doesn't check code
14455 block, which is handled by resolve_code. */
14457 static void
14458 resolve_types (gfc_namespace *ns)
14460 gfc_namespace *n;
14461 gfc_charlen *cl;
14462 gfc_data *d;
14463 gfc_equiv *eq;
14464 gfc_namespace* old_ns = gfc_current_ns;
14466 /* Check that all IMPLICIT types are ok. */
14467 if (!ns->seen_implicit_none)
14469 unsigned letter;
14470 for (letter = 0; letter != GFC_LETTERS; ++letter)
14471 if (ns->set_flag[letter]
14472 && !resolve_typespec_used (&ns->default_type[letter],
14473 &ns->implicit_loc[letter], NULL))
14474 return;
14477 gfc_current_ns = ns;
14479 resolve_entries (ns);
14481 resolve_common_vars (ns->blank_common.head, false);
14482 resolve_common_blocks (ns->common_root);
14484 resolve_contained_functions (ns);
14486 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
14487 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
14488 resolve_formal_arglist (ns->proc_name);
14490 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
14492 for (cl = ns->cl_list; cl; cl = cl->next)
14493 resolve_charlen (cl);
14495 gfc_traverse_ns (ns, resolve_symbol);
14497 resolve_fntype (ns);
14499 for (n = ns->contained; n; n = n->sibling)
14501 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
14502 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
14503 "also be PURE", n->proc_name->name,
14504 &n->proc_name->declared_at);
14506 resolve_types (n);
14509 forall_flag = 0;
14510 gfc_do_concurrent_flag = 0;
14511 gfc_check_interfaces (ns);
14513 gfc_traverse_ns (ns, resolve_values);
14515 if (ns->save_all)
14516 gfc_save_all (ns);
14518 iter_stack = NULL;
14519 for (d = ns->data; d; d = d->next)
14520 resolve_data (d);
14522 iter_stack = NULL;
14523 gfc_traverse_ns (ns, gfc_formalize_init_value);
14525 gfc_traverse_ns (ns, gfc_verify_binding_labels);
14527 for (eq = ns->equiv; eq; eq = eq->next)
14528 resolve_equivalence (eq);
14530 /* Warn about unused labels. */
14531 if (warn_unused_label)
14532 warn_unused_fortran_label (ns->st_labels);
14534 gfc_resolve_uops (ns->uop_root);
14536 gfc_current_ns = old_ns;
14540 /* Call resolve_code recursively. */
14542 static void
14543 resolve_codes (gfc_namespace *ns)
14545 gfc_namespace *n;
14546 bitmap_obstack old_obstack;
14548 if (ns->resolved == 1)
14549 return;
14551 for (n = ns->contained; n; n = n->sibling)
14552 resolve_codes (n);
14554 gfc_current_ns = ns;
14556 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
14557 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
14558 cs_base = NULL;
14560 /* Set to an out of range value. */
14561 current_entry_id = -1;
14563 old_obstack = labels_obstack;
14564 bitmap_obstack_initialize (&labels_obstack);
14566 resolve_code (ns->code, ns);
14568 bitmap_obstack_release (&labels_obstack);
14569 labels_obstack = old_obstack;
14573 /* This function is called after a complete program unit has been compiled.
14574 Its purpose is to examine all of the expressions associated with a program
14575 unit, assign types to all intermediate expressions, make sure that all
14576 assignments are to compatible types and figure out which names refer to
14577 which functions or subroutines. */
14579 void
14580 gfc_resolve (gfc_namespace *ns)
14582 gfc_namespace *old_ns;
14583 code_stack *old_cs_base;
14585 if (ns->resolved)
14586 return;
14588 ns->resolved = -1;
14589 old_ns = gfc_current_ns;
14590 old_cs_base = cs_base;
14592 resolve_types (ns);
14593 component_assignment_level = 0;
14594 resolve_codes (ns);
14596 gfc_current_ns = old_ns;
14597 cs_base = old_cs_base;
14598 ns->resolved = 1;
14600 gfc_run_passes (ns);